1 //===- PHITransAddr.h - PHI Translation for Addresses -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the PHITransAddr class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_PHITRANSADDR_H
15 #define LLVM_ANALYSIS_PHITRANSADDR_H
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/IR/Instruction.h"
23 class TargetLibraryInfo;
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.
32 /// This is designed to be a relatively small object that lives on the stack and
36 /// Addr - The actual address we're analyzing.
39 /// The DataLayout we are playing with if known, otherwise null.
42 /// TLI - The target library info if known, otherwise null.
43 const TargetLibraryInfo *TLI;
45 /// InstInputs - The inputs for our symbolic address.
46 SmallVector<Instruction*, 4> InstInputs;
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);
55 Value *getAddr() const { return Addr; }
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
62 for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
63 if (InstInputs[i]->getParent() == BB)
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;
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);
80 /// PHITranslateWithInsertion - PHI translate this value into the specified
81 /// predecessor block, inserting a computation of the value if it is
84 /// All newly created instructions are added to the NewInsts list. This
85 /// returns null on failure.
87 Value *PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB,
88 const DominatorTree &DT,
89 SmallVectorImpl<Instruction*> &NewInsts);
93 /// Verify - Check internal consistency of this data structure. If the
94 /// structure is valid, it returns true. If invalid, it prints errors and
98 Value *PHITranslateSubExpr(Value *V, BasicBlock *CurBB, BasicBlock *PredBB,
99 const DominatorTree *DT);
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.
106 Value *InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB,
107 BasicBlock *PredBB, const DominatorTree &DT,
108 SmallVectorImpl<Instruction*> &NewInsts);
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);
120 } // end namespace llvm