PHITransAddr.h revision 255f89faee13dc491cb64fbeae3c763e7e2ea4e6
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/Instruction.h"
19
20namespace 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///
35class PHITransAddr {
36  /// Addr - The actual address we're analyzing.
37  Value *Addr;
38
39  /// TD - The target data we are playing with if known, otherwise null.
40  const DataLayout *TD;
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;
47public:
48  PHITransAddr(Value *addr, const DataLayout *td) : Addr(addr), TD(td), TLI(0) {
49    // If the address is an instruction, the whole thing is considered an input.
50    if (Instruction *I = dyn_cast<Instruction>(Addr))
51      InstInputs.push_back(I);
52  }
53
54  Value *getAddr() const { return Addr; }
55
56  /// NeedsPHITranslationFromBlock - Return true if moving from the specified
57  /// BasicBlock to its predecessors requires PHI translation.
58  bool NeedsPHITranslationFromBlock(BasicBlock *BB) const {
59    // We do need translation if one of our input instructions is defined in
60    // this block.
61    for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
62      if (InstInputs[i]->getParent() == BB)
63        return true;
64    return false;
65  }
66
67  /// IsPotentiallyPHITranslatable - If this needs PHI translation, return true
68  /// if we have some hope of doing it.  This should be used as a filter to
69  /// avoid calling PHITranslateValue in hopeless situations.
70  bool IsPotentiallyPHITranslatable() const;
71
72  /// PHITranslateValue - PHI translate the current address up the CFG from
73  /// CurBB to Pred, updating our state to reflect any needed changes.  If the
74  /// dominator tree DT is non-null, the translated value must dominate
75  /// PredBB.  This returns true on failure and sets Addr to null.
76  bool PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB,
77                         const DominatorTree *DT);
78
79  /// PHITranslateWithInsertion - PHI translate this value into the specified
80  /// predecessor block, inserting a computation of the value if it is
81  /// unavailable.
82  ///
83  /// All newly created instructions are added to the NewInsts list.  This
84  /// returns null on failure.
85  ///
86  Value *PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB,
87                                   const DominatorTree &DT,
88                                   SmallVectorImpl<Instruction*> &NewInsts);
89
90  void dump() const;
91
92  /// Verify - Check internal consistency of this data structure.  If the
93  /// structure is valid, it returns true.  If invalid, it prints errors and
94  /// returns false.
95  bool Verify() const;
96private:
97  Value *PHITranslateSubExpr(Value *V, BasicBlock *CurBB, BasicBlock *PredBB,
98                             const DominatorTree *DT);
99
100  /// InsertPHITranslatedSubExpr - Insert a computation of the PHI translated
101  /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
102  /// block.  All newly created instructions are added to the NewInsts list.
103  /// This returns null on failure.
104  ///
105  Value *InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB,
106                                    BasicBlock *PredBB, const DominatorTree &DT,
107                                    SmallVectorImpl<Instruction*> &NewInsts);
108
109  /// AddAsInput - If the specified value is an instruction, add it as an input.
110  Value *AddAsInput(Value *V) {
111    // If V is an instruction, it is now an input.
112    if (Instruction *VI = dyn_cast<Instruction>(V))
113      InstInputs.push_back(VI);
114    return V;
115  }
116
117};
118
119} // end namespace llvm
120
121#endif
122