PHITransAddr.h revision 296417
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;
39234353Sdim
40288943Sdim  /// The DataLayout we are playing with.
41288943Sdim  const DataLayout &DL;
42288943Sdim
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;
51296417Sdim
52200581Srdivackypublic:
53288943Sdim  PHITransAddr(Value *addr, const DataLayout &DL, AssumptionCache *AC)
54280031Sdim      : Addr(addr), DL(DL), TLI(nullptr), AC(AC) {
55200581Srdivacky    // If the address is an instruction, the whole thing is considered an input.
56200581Srdivacky    if (Instruction *I = dyn_cast<Instruction>(Addr))
57200581Srdivacky      InstInputs.push_back(I);
58200581Srdivacky  }
59296417Sdim
60200581Srdivacky  Value *getAddr() const { return Addr; }
61296417Sdim
62200581Srdivacky  /// NeedsPHITranslationFromBlock - Return true if moving from the specified
63200581Srdivacky  /// BasicBlock to its predecessors requires PHI translation.
64200581Srdivacky  bool NeedsPHITranslationFromBlock(BasicBlock *BB) const {
65200581Srdivacky    // We do need translation if one of our input instructions is defined in
66200581Srdivacky    // this block.
67200581Srdivacky    for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
68200581Srdivacky      if (InstInputs[i]->getParent() == BB)
69200581Srdivacky        return true;
70200581Srdivacky    return false;
71200581Srdivacky  }
72296417Sdim
73200581Srdivacky  /// IsPotentiallyPHITranslatable - If this needs PHI translation, return true
74200581Srdivacky  /// if we have some hope of doing it.  This should be used as a filter to
75200581Srdivacky  /// avoid calling PHITranslateValue in hopeless situations.
76200581Srdivacky  bool IsPotentiallyPHITranslatable() const;
77296417Sdim
78200581Srdivacky  /// PHITranslateValue - PHI translate the current address up the CFG from
79288943Sdim  /// CurBB to Pred, updating our state to reflect any needed changes.  If
80288943Sdim  /// 'MustDominate' is true, the translated value must dominate
81204642Srdivacky  /// PredBB.  This returns true on failure and sets Addr to null.
82204642Srdivacky  bool PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB,
83288943Sdim                         const DominatorTree *DT, bool MustDominate);
84288943Sdim
85200581Srdivacky  /// PHITranslateWithInsertion - PHI translate this value into the specified
86200581Srdivacky  /// predecessor block, inserting a computation of the value if it is
87200581Srdivacky  /// unavailable.
88200581Srdivacky  ///
89200581Srdivacky  /// All newly created instructions are added to the NewInsts list.  This
90200581Srdivacky  /// returns null on failure.
91200581Srdivacky  ///
92200581Srdivacky  Value *PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB,
93200581Srdivacky                                   const DominatorTree &DT,
94296417Sdim                                   SmallVectorImpl<Instruction *> &NewInsts);
95296417Sdim
96200581Srdivacky  void dump() const;
97296417Sdim
98200581Srdivacky  /// Verify - Check internal consistency of this data structure.  If the
99200581Srdivacky  /// structure is valid, it returns true.  If invalid, it prints errors and
100200581Srdivacky  /// returns false.
101200581Srdivacky  bool Verify() const;
102296417Sdim
103200581Srdivackyprivate:
104204642Srdivacky  Value *PHITranslateSubExpr(Value *V, BasicBlock *CurBB, BasicBlock *PredBB,
105204642Srdivacky                             const DominatorTree *DT);
106296417Sdim
107200581Srdivacky  /// InsertPHITranslatedSubExpr - Insert a computation of the PHI translated
108200581Srdivacky  /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
109200581Srdivacky  /// block.  All newly created instructions are added to the NewInsts list.
110200581Srdivacky  /// This returns null on failure.
111200581Srdivacky  ///
112200581Srdivacky  Value *InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB,
113200581Srdivacky                                    BasicBlock *PredBB, const DominatorTree &DT,
114296417Sdim                                    SmallVectorImpl<Instruction *> &NewInsts);
115296417Sdim
116200581Srdivacky  /// AddAsInput - If the specified value is an instruction, add it as an input.
117200581Srdivacky  Value *AddAsInput(Value *V) {
118200581Srdivacky    // If V is an instruction, it is now an input.
119200581Srdivacky    if (Instruction *VI = dyn_cast<Instruction>(V))
120200581Srdivacky      InstInputs.push_back(VI);
121200581Srdivacky    return V;
122200581Srdivacky  }
123200581Srdivacky};
124200581Srdivacky
125200581Srdivacky} // end namespace llvm
126200581Srdivacky
127200581Srdivacky#endif
128