PHITransAddr.h revision 200581
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;
22200581Srdivacky  class TargetData;
23200581Srdivacky
24200581Srdivacky/// PHITransAddr - An address value which tracks and handles phi translation.
25200581Srdivacky/// As we walk "up" the CFG through predecessors, we need to ensure that the
26200581Srdivacky/// address we're tracking is kept up to date.  For example, if we're analyzing
27200581Srdivacky/// an address of "&A[i]" and walk through the definition of 'i' which is a PHI
28200581Srdivacky/// node, we *must* phi translate i to get "&A[j]" or else we will analyze an
29200581Srdivacky/// incorrect pointer in the predecessor block.
30200581Srdivacky///
31200581Srdivacky/// This is designed to be a relatively small object that lives on the stack and
32200581Srdivacky/// is copyable.
33200581Srdivacky///
34200581Srdivackyclass PHITransAddr {
35200581Srdivacky  /// Addr - The actual address we're analyzing.
36200581Srdivacky  Value *Addr;
37200581Srdivacky
38200581Srdivacky  /// TD - The target data we are playing with if known, otherwise null.
39200581Srdivacky  const TargetData *TD;
40200581Srdivacky
41200581Srdivacky  /// InstInputs - The inputs for our symbolic address.
42200581Srdivacky  SmallVector<Instruction*, 4> InstInputs;
43200581Srdivackypublic:
44200581Srdivacky  PHITransAddr(Value *addr, const TargetData *td) : Addr(addr), TD(td) {
45200581Srdivacky    // If the address is an instruction, the whole thing is considered an input.
46200581Srdivacky    if (Instruction *I = dyn_cast<Instruction>(Addr))
47200581Srdivacky      InstInputs.push_back(I);
48200581Srdivacky  }
49200581Srdivacky
50200581Srdivacky  Value *getAddr() const { return Addr; }
51200581Srdivacky
52200581Srdivacky  /// NeedsPHITranslationFromBlock - Return true if moving from the specified
53200581Srdivacky  /// BasicBlock to its predecessors requires PHI translation.
54200581Srdivacky  bool NeedsPHITranslationFromBlock(BasicBlock *BB) const {
55200581Srdivacky    // We do need translation if one of our input instructions is defined in
56200581Srdivacky    // this block.
57200581Srdivacky    for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
58200581Srdivacky      if (InstInputs[i]->getParent() == BB)
59200581Srdivacky        return true;
60200581Srdivacky    return false;
61200581Srdivacky  }
62200581Srdivacky
63200581Srdivacky  /// IsPotentiallyPHITranslatable - If this needs PHI translation, return true
64200581Srdivacky  /// if we have some hope of doing it.  This should be used as a filter to
65200581Srdivacky  /// avoid calling PHITranslateValue in hopeless situations.
66200581Srdivacky  bool IsPotentiallyPHITranslatable() const;
67200581Srdivacky
68200581Srdivacky  /// PHITranslateValue - PHI translate the current address up the CFG from
69200581Srdivacky  /// CurBB to Pred, updating our state the reflect any needed changes.  This
70200581Srdivacky  /// returns true on failure and sets Addr to null.
71200581Srdivacky  bool PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB);
72200581Srdivacky
73200581Srdivacky  /// PHITranslateWithInsertion - PHI translate this value into the specified
74200581Srdivacky  /// predecessor block, inserting a computation of the value if it is
75200581Srdivacky  /// unavailable.
76200581Srdivacky  ///
77200581Srdivacky  /// All newly created instructions are added to the NewInsts list.  This
78200581Srdivacky  /// returns null on failure.
79200581Srdivacky  ///
80200581Srdivacky  Value *PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB,
81200581Srdivacky                                   const DominatorTree &DT,
82200581Srdivacky                                   SmallVectorImpl<Instruction*> &NewInsts);
83200581Srdivacky
84200581Srdivacky  void dump() const;
85200581Srdivacky
86200581Srdivacky  /// Verify - Check internal consistency of this data structure.  If the
87200581Srdivacky  /// structure is valid, it returns true.  If invalid, it prints errors and
88200581Srdivacky  /// returns false.
89200581Srdivacky  bool Verify() const;
90200581Srdivackyprivate:
91200581Srdivacky  Value *PHITranslateSubExpr(Value *V, BasicBlock *CurBB, BasicBlock *PredBB);
92200581Srdivacky
93200581Srdivacky
94200581Srdivacky  /// GetAvailablePHITranslatedSubExpr - Return the value computed by
95200581Srdivacky  /// PHITranslateSubExpr if it dominates PredBB, otherwise return null.
96200581Srdivacky  Value *GetAvailablePHITranslatedSubExpr(Value *V,
97200581Srdivacky                                          BasicBlock *CurBB, BasicBlock *PredBB,
98200581Srdivacky                                          const DominatorTree &DT) const;
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