1 //===-- SSAUpdater.h - Unstructured SSA Update Tool -------------*- 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 SSAUpdater class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_TRANSFORMS_UTILS_SSAUPDATER_H 15 #define LLVM_TRANSFORMS_UTILS_SSAUPDATER_H 16 17 namespace llvm { 18 class Value; 19 class BasicBlock; 20 class Use; 21 class PHINode; 22 template<typename T> class SmallVectorImpl; 23 template<typename T> class SSAUpdaterTraits; 24 class DbgDeclareInst; 25 class DIBuilder; 26 class BumpPtrAllocator; 27 28 /// SSAUpdater - This class updates SSA form for a set of values defined in 29 /// multiple blocks. This is used when code duplication or another unstructured 30 /// transformation wants to rewrite a set of uses of one value with uses of a 31 /// set of values. 32 class SSAUpdater { 33 friend class SSAUpdaterTraits<SSAUpdater>; 34 35 private: 36 /// AvailableVals - This keeps track of which value to use on a per-block 37 /// basis. When we insert PHI nodes, we keep track of them here. 38 //typedef DenseMap<BasicBlock*, Value*> AvailableValsTy; 39 void *AV; 40 41 /// ProtoType holds the type of the values being rewritten. 42 Type *ProtoType; 43 44 // PHI nodes are given a name based on ProtoName. 45 std::string ProtoName; 46 47 /// InsertedPHIs - If this is non-null, the SSAUpdater adds all PHI nodes that 48 /// it creates to the vector. 49 SmallVectorImpl<PHINode*> *InsertedPHIs; 50 51 public: 52 /// SSAUpdater constructor. If InsertedPHIs is specified, it will be filled 53 /// in with all PHI Nodes created by rewriting. 54 explicit SSAUpdater(SmallVectorImpl<PHINode*> *InsertedPHIs = 0); 55 ~SSAUpdater(); 56 57 /// Initialize - Reset this object to get ready for a new set of SSA 58 /// updates with type 'Ty'. PHI nodes get a name based on 'Name'. 59 void Initialize(Type *Ty, StringRef Name); 60 61 /// AddAvailableValue - Indicate that a rewritten value is available at the 62 /// end of the specified block with the specified value. 63 void AddAvailableValue(BasicBlock *BB, Value *V); 64 65 /// HasValueForBlock - Return true if the SSAUpdater already has a value for 66 /// the specified block. 67 bool HasValueForBlock(BasicBlock *BB) const; 68 69 /// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is 70 /// live at the end of the specified block. 71 Value *GetValueAtEndOfBlock(BasicBlock *BB); 72 73 /// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that 74 /// is live in the middle of the specified block. 75 /// 76 /// GetValueInMiddleOfBlock is the same as GetValueAtEndOfBlock except in one 77 /// important case: if there is a definition of the rewritten value after the 78 /// 'use' in BB. Consider code like this: 79 /// 80 /// X1 = ... 81 /// SomeBB: 82 /// use(X) 83 /// X2 = ... 84 /// br Cond, SomeBB, OutBB 85 /// 86 /// In this case, there are two values (X1 and X2) added to the AvailableVals 87 /// set by the client of the rewriter, and those values are both live out of 88 /// their respective blocks. However, the use of X happens in the *middle* of 89 /// a block. Because of this, we need to insert a new PHI node in SomeBB to 90 /// merge the appropriate values, and this value isn't live out of the block. 91 /// 92 Value *GetValueInMiddleOfBlock(BasicBlock *BB); 93 94 /// RewriteUse - Rewrite a use of the symbolic value. This handles PHI nodes, 95 /// which use their value in the corresponding predecessor. Note that this 96 /// will not work if the use is supposed to be rewritten to a value defined in 97 /// the same block as the use, but above it. Any 'AddAvailableValue's added 98 /// for the use's block will be considered to be below it. 99 void RewriteUse(Use &U); 100 101 /// RewriteUseAfterInsertions - Rewrite a use, just like RewriteUse. However, 102 /// this version of the method can rewrite uses in the same block as a 103 /// definition, because it assumes that all uses of a value are below any 104 /// inserted values. 105 void RewriteUseAfterInsertions(Use &U); 106 107 private: 108 Value *GetValueAtEndOfBlockInternal(BasicBlock *BB); 109 110 void operator=(const SSAUpdater&); // DO NOT IMPLEMENT 111 SSAUpdater(const SSAUpdater&); // DO NOT IMPLEMENT 112 }; 113 114 /// LoadAndStorePromoter - This little helper class provides a convenient way to 115 /// promote a collection of loads and stores into SSA Form using the SSAUpdater. 116 /// This handles complexities that SSAUpdater doesn't, such as multiple loads 117 /// and stores in one block. 118 /// 119 /// Clients of this class are expected to subclass this and implement the 120 /// virtual methods. 121 /// 122 class LoadAndStorePromoter { 123 protected: 124 SSAUpdater &SSA; 125 public: 126 LoadAndStorePromoter(const SmallVectorImpl<Instruction*> &Insts, 127 SSAUpdater &S, StringRef Name = StringRef()); ~LoadAndStorePromoter()128 virtual ~LoadAndStorePromoter() {} 129 130 /// run - This does the promotion. Insts is a list of loads and stores to 131 /// promote, and Name is the basename for the PHIs to insert. After this is 132 /// complete, the loads and stores are removed from the code. 133 void run(const SmallVectorImpl<Instruction*> &Insts) const; 134 135 136 /// Return true if the specified instruction is in the Inst list (which was 137 /// passed into the run method). Clients should implement this with a more 138 /// efficient version if possible. isInstInList(Instruction * I,const SmallVectorImpl<Instruction * > & Insts)139 virtual bool isInstInList(Instruction *I, 140 const SmallVectorImpl<Instruction*> &Insts) const { 141 for (unsigned i = 0, e = Insts.size(); i != e; ++i) 142 if (Insts[i] == I) 143 return true; 144 return false; 145 } 146 147 /// doExtraRewritesBeforeFinalDeletion - This hook is invoked after all the 148 /// stores are found and inserted as available values, but doExtraRewritesBeforeFinalDeletion()149 virtual void doExtraRewritesBeforeFinalDeletion() const { 150 } 151 152 /// replaceLoadWithValue - Clients can choose to implement this to get 153 /// notified right before a load is RAUW'd another value. replaceLoadWithValue(LoadInst * LI,Value * V)154 virtual void replaceLoadWithValue(LoadInst *LI, Value *V) const { 155 } 156 157 /// This is called before each instruction is deleted. instructionDeleted(Instruction * I)158 virtual void instructionDeleted(Instruction *I) const { 159 } 160 161 /// updateDebugInfo - This is called to update debug info associated with the 162 /// instruction. updateDebugInfo(Instruction * I)163 virtual void updateDebugInfo(Instruction *I) const { 164 } 165 }; 166 167 } // End llvm namespace 168 169 #endif 170