PHITransAddr.h revision 280031
1200581Srdivacky//===- PHITransAddr.h - PHI Translation for Addresses -----------*- C++ -*-===//
2200581Srdivacky//
3200581Srdivacky//                     The LLVM Compiler Infrastructure
4200581Srdivacky//
5200581Srdivacky// This file is distributed under the University of Illinois Open Source
6200581Srdivacky// License. See LICENSE.TXT for details.
7200581Srdivacky//
8200581Srdivacky//===----------------------------------------------------------------------===//
9200581Srdivacky//
10200581Srdivacky// This file declares the PHITransAddr class.
11200581Srdivacky//
12200581Srdivacky//===----------------------------------------------------------------------===//
13200581Srdivacky
14200581Srdivacky#ifndef LLVM_ANALYSIS_PHITRANSADDR_H
15200581Srdivacky#define LLVM_ANALYSIS_PHITRANSADDR_H
16200581Srdivacky
17200581Srdivacky#include "llvm/ADT/SmallVector.h"
18249423Sdim#include "llvm/IR/Instruction.h"
19200581Srdivacky
20200581Srdivackynamespace llvm {
21280031Sdim  class AssumptionCache;
22200581Srdivacky  class DominatorTree;
23243830Sdim  class DataLayout;
24234353Sdim  class TargetLibraryInfo;
25234353Sdim
26200581Srdivacky/// PHITransAddr - An address value which tracks and handles phi translation.
27200581Srdivacky/// As we walk "up" the CFG through predecessors, we need to ensure that the
28200581Srdivacky/// address we're tracking is kept up to date.  For example, if we're analyzing
29200581Srdivacky/// an address of "&A[i]" and walk through the definition of 'i' which is a PHI
30200581Srdivacky/// node, we *must* phi translate i to get "&A[j]" or else we will analyze an
31200581Srdivacky/// incorrect pointer in the predecessor block.
32200581Srdivacky///
33200581Srdivacky/// This is designed to be a relatively small object that lives on the stack and
34200581Srdivacky/// is copyable.
35200581Srdivacky///
36200581Srdivackyclass PHITransAddr {
37200581Srdivacky  /// Addr - The actual address we're analyzing.
38200581Srdivacky  Value *Addr;
39200581Srdivacky
40276479Sdim  /// The DataLayout we are playing with if known, otherwise null.
41276479Sdim  const DataLayout *DL;
42234353Sdim
43234353Sdim  /// TLI - The target library info if known, otherwise null.
44234353Sdim  const TargetLibraryInfo *TLI;
45280031Sdim
46280031Sdim  /// A cache of @llvm.assume calls used by SimplifyInstruction.
47280031Sdim  AssumptionCache *AC;
48280031Sdim
49200581Srdivacky  /// InstInputs - The inputs for our symbolic address.
50200581Srdivacky  SmallVector<Instruction*, 4> InstInputs;
51200581Srdivackypublic:
52280031Sdim  PHITransAddr(Value *addr, const DataLayout *DL, AssumptionCache *AC)
53280031Sdim      : Addr(addr), DL(DL), TLI(nullptr), AC(AC) {
54200581Srdivacky    // If the address is an instruction, the whole thing is considered an input.
55200581Srdivacky    if (Instruction *I = dyn_cast<Instruction>(Addr))
56200581Srdivacky      InstInputs.push_back(I);
57200581Srdivacky  }
58200581Srdivacky
59200581Srdivacky  Value *getAddr() const { return Addr; }
60200581Srdivacky
61200581Srdivacky  /// NeedsPHITranslationFromBlock - Return true if moving from the specified
62200581Srdivacky  /// BasicBlock to its predecessors requires PHI translation.
63200581Srdivacky  bool NeedsPHITranslationFromBlock(BasicBlock *BB) const {
64200581Srdivacky    // We do need translation if one of our input instructions is defined in
65200581Srdivacky    // this block.
66200581Srdivacky    for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
67200581Srdivacky      if (InstInputs[i]->getParent() == BB)
68200581Srdivacky        return true;
69200581Srdivacky    return false;
70200581Srdivacky  }
71200581Srdivacky
72200581Srdivacky  /// IsPotentiallyPHITranslatable - If this needs PHI translation, return true
73200581Srdivacky  /// if we have some hope of doing it.  This should be used as a filter to
74200581Srdivacky  /// avoid calling PHITranslateValue in hopeless situations.
75200581Srdivacky  bool IsPotentiallyPHITranslatable() const;
76200581Srdivacky
77200581Srdivacky  /// PHITranslateValue - PHI translate the current address up the CFG from
78204642Srdivacky  /// CurBB to Pred, updating our state to reflect any needed changes.  If the
79204642Srdivacky  /// dominator tree DT is non-null, the translated value must dominate
80204642Srdivacky  /// PredBB.  This returns true on failure and sets Addr to null.
81204642Srdivacky  bool PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB,
82204642Srdivacky                         const DominatorTree *DT);
83200581Srdivacky
84200581Srdivacky  /// PHITranslateWithInsertion - PHI translate this value into the specified
85200581Srdivacky  /// predecessor block, inserting a computation of the value if it is
86200581Srdivacky  /// unavailable.
87200581Srdivacky  ///
88200581Srdivacky  /// All newly created instructions are added to the NewInsts list.  This
89200581Srdivacky  /// returns null on failure.
90200581Srdivacky  ///
91200581Srdivacky  Value *PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB,
92200581Srdivacky                                   const DominatorTree &DT,
93200581Srdivacky                                   SmallVectorImpl<Instruction*> &NewInsts);
94200581Srdivacky
95200581Srdivacky  void dump() const;
96200581Srdivacky
97200581Srdivacky  /// Verify - Check internal consistency of this data structure.  If the
98200581Srdivacky  /// structure is valid, it returns true.  If invalid, it prints errors and
99200581Srdivacky  /// returns false.
100200581Srdivacky  bool Verify() const;
101200581Srdivackyprivate:
102204642Srdivacky  Value *PHITranslateSubExpr(Value *V, BasicBlock *CurBB, BasicBlock *PredBB,
103204642Srdivacky                             const DominatorTree *DT);
104200581Srdivacky
105200581Srdivacky  /// InsertPHITranslatedSubExpr - Insert a computation of the PHI translated
106200581Srdivacky  /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
107200581Srdivacky  /// block.  All newly created instructions are added to the NewInsts list.
108200581Srdivacky  /// This returns null on failure.
109200581Srdivacky  ///
110200581Srdivacky  Value *InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB,
111200581Srdivacky                                    BasicBlock *PredBB, const DominatorTree &DT,
112200581Srdivacky                                    SmallVectorImpl<Instruction*> &NewInsts);
113200581Srdivacky
114200581Srdivacky  /// AddAsInput - If the specified value is an instruction, add it as an input.
115200581Srdivacky  Value *AddAsInput(Value *V) {
116200581Srdivacky    // If V is an instruction, it is now an input.
117200581Srdivacky    if (Instruction *VI = dyn_cast<Instruction>(V))
118200581Srdivacky      InstInputs.push_back(VI);
119200581Srdivacky    return V;
120200581Srdivacky  }
121200581Srdivacky
122200581Srdivacky};
123200581Srdivacky
124200581Srdivacky} // end namespace llvm
125200581Srdivacky
126200581Srdivacky#endif
127