PHITransAddr.h revision 36b56886974eae4f9c5ebc96befd3e7bfe5de338
1210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner//===- PHITransAddr.h - PHI Translation for Addresses -----------*- C++ -*-===//
2210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner//
3210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner//                     The LLVM Compiler Infrastructure
4210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner//
5210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner// This file is distributed under the University of Illinois Open Source
6210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner// License. See LICENSE.TXT for details.
7210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner//
8210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner//===----------------------------------------------------------------------===//
9210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner//
10210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner// This file declares the PHITransAddr class.
11210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner//
12210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner//===----------------------------------------------------------------------===//
13210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner
14210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner#ifndef LLVM_ANALYSIS_PHITRANSADDR_H
15210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner#define LLVM_ANALYSIS_PHITRANSADDR_H
16210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner
17210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner#include "llvm/ADT/SmallVector.h"
180b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Instruction.h"
19210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner
20210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattnernamespace llvm {
21210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner  class DominatorTree;
223574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow  class DataLayout;
23618c1dbd293d15ee19f61b1156ab8086ad28311aChad Rosier  class TargetLibraryInfo;
24618c1dbd293d15ee19f61b1156ab8086ad28311aChad Rosier
25210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner/// PHITransAddr - An address value which tracks and handles phi translation.
26210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner/// As we walk "up" the CFG through predecessors, we need to ensure that the
27210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner/// address we're tracking is kept up to date.  For example, if we're analyzing
28210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner/// an address of "&A[i]" and walk through the definition of 'i' which is a PHI
29210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner/// node, we *must* phi translate i to get "&A[j]" or else we will analyze an
30210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner/// incorrect pointer in the predecessor block.
31210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner///
32210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner/// This is designed to be a relatively small object that lives on the stack and
33210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner/// is copyable.
34210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner///
35210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattnerclass PHITransAddr {
36210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner  /// Addr - The actual address we're analyzing.
37210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner  Value *Addr;
38210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner
3936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// The DataLayout we are playing with if known, otherwise null.
4036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  const DataLayout *DL;
41618c1dbd293d15ee19f61b1156ab8086ad28311aChad Rosier
42618c1dbd293d15ee19f61b1156ab8086ad28311aChad Rosier  /// TLI - The target library info if known, otherwise null.
43618c1dbd293d15ee19f61b1156ab8086ad28311aChad Rosier  const TargetLibraryInfo *TLI;
449a8641201b2db8427be2a6531c043f384562c081Chris Lattner
45210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner  /// InstInputs - The inputs for our symbolic address.
46210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner  SmallVector<Instruction*, 4> InstInputs;
47210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattnerpublic:
4836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  PHITransAddr(Value *addr, const DataLayout *DL) : Addr(addr), DL(DL), TLI(0) {
49210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner    // If the address is an instruction, the whole thing is considered an input.
50210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner    if (Instruction *I = dyn_cast<Instruction>(Addr))
51210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner      InstInputs.push_back(I);
52210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner  }
53210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner
54e05a188cd630448cc25143ee8e69a36ab2e69544Chris Lattner  Value *getAddr() const { return Addr; }
55e05a188cd630448cc25143ee8e69a36ab2e69544Chris Lattner
56210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner  /// NeedsPHITranslationFromBlock - Return true if moving from the specified
57210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner  /// BasicBlock to its predecessors requires PHI translation.
58210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner  bool NeedsPHITranslationFromBlock(BasicBlock *BB) const {
59210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner    // We do need translation if one of our input instructions is defined in
60210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner    // this block.
61210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner    for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
62210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner      if (InstInputs[i]->getParent() == BB)
63210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner        return true;
64210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner    return false;
65210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner  }
66210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner
679a8641201b2db8427be2a6531c043f384562c081Chris Lattner  /// IsPotentiallyPHITranslatable - If this needs PHI translation, return true
689a8641201b2db8427be2a6531c043f384562c081Chris Lattner  /// if we have some hope of doing it.  This should be used as a filter to
699a8641201b2db8427be2a6531c043f384562c081Chris Lattner  /// avoid calling PHITranslateValue in hopeless situations.
709a8641201b2db8427be2a6531c043f384562c081Chris Lattner  bool IsPotentiallyPHITranslatable() const;
719a8641201b2db8427be2a6531c043f384562c081Chris Lattner
729a8641201b2db8427be2a6531c043f384562c081Chris Lattner  /// PHITranslateValue - PHI translate the current address up the CFG from
736d8f2ca646bc283c31f48b6816d5194c836dfec6Daniel Dunbar  /// CurBB to Pred, updating our state to reflect any needed changes.  If the
746d8f2ca646bc283c31f48b6816d5194c836dfec6Daniel Dunbar  /// dominator tree DT is non-null, the translated value must dominate
756d8f2ca646bc283c31f48b6816d5194c836dfec6Daniel Dunbar  /// PredBB.  This returns true on failure and sets Addr to null.
766d8f2ca646bc283c31f48b6816d5194c836dfec6Daniel Dunbar  bool PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB,
776d8f2ca646bc283c31f48b6816d5194c836dfec6Daniel Dunbar                         const DominatorTree *DT);
789a8641201b2db8427be2a6531c043f384562c081Chris Lattner
799a8641201b2db8427be2a6531c043f384562c081Chris Lattner  /// PHITranslateWithInsertion - PHI translate this value into the specified
809a8641201b2db8427be2a6531c043f384562c081Chris Lattner  /// predecessor block, inserting a computation of the value if it is
819a8641201b2db8427be2a6531c043f384562c081Chris Lattner  /// unavailable.
829a8641201b2db8427be2a6531c043f384562c081Chris Lattner  ///
839a8641201b2db8427be2a6531c043f384562c081Chris Lattner  /// All newly created instructions are added to the NewInsts list.  This
849a8641201b2db8427be2a6531c043f384562c081Chris Lattner  /// returns null on failure.
859a8641201b2db8427be2a6531c043f384562c081Chris Lattner  ///
869a8641201b2db8427be2a6531c043f384562c081Chris Lattner  Value *PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB,
879a8641201b2db8427be2a6531c043f384562c081Chris Lattner                                   const DominatorTree &DT,
889a8641201b2db8427be2a6531c043f384562c081Chris Lattner                                   SmallVectorImpl<Instruction*> &NewInsts);
897dedbf4ce3e1b62b4e0b000b38d244b50029c315Chris Lattner
907dedbf4ce3e1b62b4e0b000b38d244b50029c315Chris Lattner  void dump() const;
917dedbf4ce3e1b62b4e0b000b38d244b50029c315Chris Lattner
92af50315a29600188a6ff8b935beca6f1b59edf48Chris Lattner  /// Verify - Check internal consistency of this data structure.  If the
93af50315a29600188a6ff8b935beca6f1b59edf48Chris Lattner  /// structure is valid, it returns true.  If invalid, it prints errors and
94af50315a29600188a6ff8b935beca6f1b59edf48Chris Lattner  /// returns false.
957dedbf4ce3e1b62b4e0b000b38d244b50029c315Chris Lattner  bool Verify() const;
969a8641201b2db8427be2a6531c043f384562c081Chris Lattnerprivate:
976d8f2ca646bc283c31f48b6816d5194c836dfec6Daniel Dunbar  Value *PHITranslateSubExpr(Value *V, BasicBlock *CurBB, BasicBlock *PredBB,
986d8f2ca646bc283c31f48b6816d5194c836dfec6Daniel Dunbar                             const DominatorTree *DT);
99210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner
1009a8641201b2db8427be2a6531c043f384562c081Chris Lattner  /// InsertPHITranslatedSubExpr - Insert a computation of the PHI translated
101210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner  /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
102210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner  /// block.  All newly created instructions are added to the NewInsts list.
103210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner  /// This returns null on failure.
104210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner  ///
1059a8641201b2db8427be2a6531c043f384562c081Chris Lattner  Value *InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB,
1069a8641201b2db8427be2a6531c043f384562c081Chris Lattner                                    BasicBlock *PredBB, const DominatorTree &DT,
1079a8641201b2db8427be2a6531c043f384562c081Chris Lattner                                    SmallVectorImpl<Instruction*> &NewInsts);
10843678f41a37c077f28517c2e4889cca88cada6ceChris Lattner
1096200e53f55536f812153ad910e6a69139592301bChris Lattner  /// AddAsInput - If the specified value is an instruction, add it as an input.
1106200e53f55536f812153ad910e6a69139592301bChris Lattner  Value *AddAsInput(Value *V) {
1116200e53f55536f812153ad910e6a69139592301bChris Lattner    // If V is an instruction, it is now an input.
1126200e53f55536f812153ad910e6a69139592301bChris Lattner    if (Instruction *VI = dyn_cast<Instruction>(V))
1136200e53f55536f812153ad910e6a69139592301bChris Lattner      InstInputs.push_back(VI);
1146200e53f55536f812153ad910e6a69139592301bChris Lattner    return V;
1156200e53f55536f812153ad910e6a69139592301bChris Lattner  }
1167dedbf4ce3e1b62b4e0b000b38d244b50029c315Chris Lattner
117210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner};
118210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner
119210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner} // end namespace llvm
120210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner
121210e45af3a579beeefb001c8f13c94e80407aad5Chris Lattner#endif
122