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