69f59071f94fc56e6a2a52d01dce854e0b33de55
[oota-llvm.git] / include / llvm / Analysis / PHITransAddr.h
1 //===- PHITransAddr.h - PHI Translation for Addresses -----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the PHITransAddr class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ANALYSIS_PHITRANSADDR_H
15 #define LLVM_ANALYSIS_PHITRANSADDR_H
16
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/IR/Instruction.h"
19
20 namespace llvm {
21   class DominatorTree;
22   class DataLayout;
23   class TargetLibraryInfo;
24
25 /// PHITransAddr - An address value which tracks and handles phi translation.
26 /// As we walk "up" the CFG through predecessors, we need to ensure that the
27 /// address we're tracking is kept up to date.  For example, if we're analyzing
28 /// an address of "&A[i]" and walk through the definition of 'i' which is a PHI
29 /// node, we *must* phi translate i to get "&A[j]" or else we will analyze an
30 /// incorrect pointer in the predecessor block.
31 ///
32 /// This is designed to be a relatively small object that lives on the stack and
33 /// is copyable.
34 ///
35 class PHITransAddr {
36   /// Addr - The actual address we're analyzing.
37   Value *Addr;
38   
39   /// The DataLayout we are playing with if known, otherwise null.
40   const DataLayout *DL;
41
42   /// TLI - The target library info if known, otherwise null.
43   const TargetLibraryInfo *TLI;
44   
45   /// InstInputs - The inputs for our symbolic address.
46   SmallVector<Instruction*, 4> InstInputs;
47 public:
48   PHITransAddr(Value *addr, const DataLayout *DL)
49       : Addr(addr), DL(DL), TLI(nullptr) {
50     // If the address is an instruction, the whole thing is considered an input.
51     if (Instruction *I = dyn_cast<Instruction>(Addr))
52       InstInputs.push_back(I);
53   }
54   
55   Value *getAddr() const { return Addr; }
56   
57   /// NeedsPHITranslationFromBlock - Return true if moving from the specified
58   /// BasicBlock to its predecessors requires PHI translation.
59   bool NeedsPHITranslationFromBlock(BasicBlock *BB) const {
60     // We do need translation if one of our input instructions is defined in
61     // this block.
62     for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
63       if (InstInputs[i]->getParent() == BB)
64         return true;
65     return false;
66   }
67   
68   /// IsPotentiallyPHITranslatable - If this needs PHI translation, return true
69   /// if we have some hope of doing it.  This should be used as a filter to
70   /// avoid calling PHITranslateValue in hopeless situations.
71   bool IsPotentiallyPHITranslatable() const;
72   
73   /// PHITranslateValue - PHI translate the current address up the CFG from
74   /// CurBB to Pred, updating our state to reflect any needed changes.  If the
75   /// dominator tree DT is non-null, the translated value must dominate
76   /// PredBB.  This returns true on failure and sets Addr to null.
77   bool PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB,
78                          const DominatorTree *DT);
79   
80   /// PHITranslateWithInsertion - PHI translate this value into the specified
81   /// predecessor block, inserting a computation of the value if it is
82   /// unavailable.
83   ///
84   /// All newly created instructions are added to the NewInsts list.  This
85   /// returns null on failure.
86   ///
87   Value *PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB,
88                                    const DominatorTree &DT,
89                                    SmallVectorImpl<Instruction*> &NewInsts);
90   
91   void dump() const;
92   
93   /// Verify - Check internal consistency of this data structure.  If the
94   /// structure is valid, it returns true.  If invalid, it prints errors and
95   /// returns false.
96   bool Verify() const;
97 private:
98   Value *PHITranslateSubExpr(Value *V, BasicBlock *CurBB, BasicBlock *PredBB,
99                              const DominatorTree *DT);
100   
101   /// InsertPHITranslatedSubExpr - Insert a computation of the PHI translated
102   /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
103   /// block.  All newly created instructions are added to the NewInsts list.
104   /// This returns null on failure.
105   ///
106   Value *InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB,
107                                     BasicBlock *PredBB, const DominatorTree &DT,
108                                     SmallVectorImpl<Instruction*> &NewInsts);
109   
110   /// AddAsInput - If the specified value is an instruction, add it as an input.
111   Value *AddAsInput(Value *V) {
112     // If V is an instruction, it is now an input.
113     if (Instruction *VI = dyn_cast<Instruction>(V))
114       InstInputs.push_back(VI);
115     return V;
116   }
117   
118 };
119
120 } // end namespace llvm
121
122 #endif