PHITransAddr.h revision 243830
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/Instruction.h"
18200581Srdivacky#include "llvm/ADT/SmallVector.h"
19200581Srdivacky
20200581Srdivackynamespace llvm {
21200581Srdivacky  class DominatorTree;
22243830Sdim  class DataLayout;
23234353Sdim  class TargetLibraryInfo;
24234353Sdim
25200581Srdivacky/// PHITransAddr - An address value which tracks and handles phi translation.
26200581Srdivacky/// As we walk "up" the CFG through predecessors, we need to ensure that the
27200581Srdivacky/// address we're tracking is kept up to date.  For example, if we're analyzing
28200581Srdivacky/// an address of "&A[i]" and walk through the definition of 'i' which is a PHI
29200581Srdivacky/// node, we *must* phi translate i to get "&A[j]" or else we will analyze an
30200581Srdivacky/// incorrect pointer in the predecessor block.
31200581Srdivacky///
32200581Srdivacky/// This is designed to be a relatively small object that lives on the stack and
33200581Srdivacky/// is copyable.
34200581Srdivacky///
35200581Srdivackyclass PHITransAddr {
36200581Srdivacky  /// Addr - The actual address we're analyzing.
37200581Srdivacky  Value *Addr;
38200581Srdivacky
39200581Srdivacky  /// TD - The target data we are playing with if known, otherwise null.
40243830Sdim  const DataLayout *TD;
41234353Sdim
42234353Sdim  /// TLI - The target library info if known, otherwise null.
43234353Sdim  const TargetLibraryInfo *TLI;
44200581Srdivacky
45200581Srdivacky  /// InstInputs - The inputs for our symbolic address.
46200581Srdivacky  SmallVector<Instruction*, 4> InstInputs;
47200581Srdivackypublic:
48243830Sdim  PHITransAddr(Value *addr, const DataLayout *td) : Addr(addr), TD(td), TLI(0) {
49200581Srdivacky    // If the address is an instruction, the whole thing is considered an input.
50200581Srdivacky    if (Instruction *I = dyn_cast<Instruction>(Addr))
51200581Srdivacky      InstInputs.push_back(I);
52200581Srdivacky  }
53200581Srdivacky
54200581Srdivacky  Value *getAddr() const { return Addr; }
55200581Srdivacky
56200581Srdivacky  /// NeedsPHITranslationFromBlock - Return true if moving from the specified
57200581Srdivacky  /// BasicBlock to its predecessors requires PHI translation.
58200581Srdivacky  bool NeedsPHITranslationFromBlock(BasicBlock *BB) const {
59200581Srdivacky    // We do need translation if one of our input instructions is defined in
60200581Srdivacky    // this block.
61200581Srdivacky    for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
62200581Srdivacky      if (InstInputs[i]->getParent() == BB)
63200581Srdivacky        return true;
64200581Srdivacky    return false;
65200581Srdivacky  }
66200581Srdivacky
67200581Srdivacky  /// IsPotentiallyPHITranslatable - If this needs PHI translation, return true
68200581Srdivacky  /// if we have some hope of doing it.  This should be used as a filter to
69200581Srdivacky  /// avoid calling PHITranslateValue in hopeless situations.
70200581Srdivacky  bool IsPotentiallyPHITranslatable() const;
71200581Srdivacky
72200581Srdivacky  /// PHITranslateValue - PHI translate the current address up the CFG from
73204642Srdivacky  /// CurBB to Pred, updating our state to reflect any needed changes.  If the
74204642Srdivacky  /// dominator tree DT is non-null, the translated value must dominate
75204642Srdivacky  /// PredBB.  This returns true on failure and sets Addr to null.
76204642Srdivacky  bool PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB,
77204642Srdivacky                         const DominatorTree *DT);
78200581Srdivacky
79200581Srdivacky  /// PHITranslateWithInsertion - PHI translate this value into the specified
80200581Srdivacky  /// predecessor block, inserting a computation of the value if it is
81200581Srdivacky  /// unavailable.
82200581Srdivacky  ///
83200581Srdivacky  /// All newly created instructions are added to the NewInsts list.  This
84200581Srdivacky  /// returns null on failure.
85200581Srdivacky  ///
86200581Srdivacky  Value *PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB,
87200581Srdivacky                                   const DominatorTree &DT,
88200581Srdivacky                                   SmallVectorImpl<Instruction*> &NewInsts);
89200581Srdivacky
90200581Srdivacky  void dump() const;
91200581Srdivacky
92200581Srdivacky  /// Verify - Check internal consistency of this data structure.  If the
93200581Srdivacky  /// structure is valid, it returns true.  If invalid, it prints errors and
94200581Srdivacky  /// returns false.
95200581Srdivacky  bool Verify() const;
96200581Srdivackyprivate:
97204642Srdivacky  Value *PHITranslateSubExpr(Value *V, BasicBlock *CurBB, BasicBlock *PredBB,
98204642Srdivacky                             const DominatorTree *DT);
99200581Srdivacky
100200581Srdivacky  /// InsertPHITranslatedSubExpr - Insert a computation of the PHI translated
101200581Srdivacky  /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
102200581Srdivacky  /// block.  All newly created instructions are added to the NewInsts list.
103200581Srdivacky  /// This returns null on failure.
104200581Srdivacky  ///
105200581Srdivacky  Value *InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB,
106200581Srdivacky                                    BasicBlock *PredBB, const DominatorTree &DT,
107200581Srdivacky                                    SmallVectorImpl<Instruction*> &NewInsts);
108200581Srdivacky
109200581Srdivacky  /// AddAsInput - If the specified value is an instruction, add it as an input.
110200581Srdivacky  Value *AddAsInput(Value *V) {
111200581Srdivacky    // If V is an instruction, it is now an input.
112200581Srdivacky    if (Instruction *VI = dyn_cast<Instruction>(V))
113200581Srdivacky      InstInputs.push_back(VI);
114200581Srdivacky    return V;
115200581Srdivacky  }
116200581Srdivacky
117200581Srdivacky};
118200581Srdivacky
119200581Srdivacky} // end namespace llvm
120200581Srdivacky
121200581Srdivacky#endif
122