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