LLVM API Documentation
00001 //===-- SSAUpdater.h - Unstructured SSA Update Tool -------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file declares the SSAUpdater class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_TRANSFORMS_UTILS_SSAUPDATER_H 00015 #define LLVM_TRANSFORMS_UTILS_SSAUPDATER_H 00016 00017 #include "llvm/ADT/StringRef.h" 00018 #include "llvm/Support/Compiler.h" 00019 00020 namespace llvm { 00021 class BasicBlock; 00022 class Instruction; 00023 class LoadInst; 00024 template<typename T> class SmallVectorImpl; 00025 template<typename T> class SSAUpdaterTraits; 00026 class PHINode; 00027 class Type; 00028 class Use; 00029 class Value; 00030 00031 /// SSAUpdater - This class updates SSA form for a set of values defined in 00032 /// multiple blocks. This is used when code duplication or another unstructured 00033 /// transformation wants to rewrite a set of uses of one value with uses of a 00034 /// set of values. 00035 class SSAUpdater { 00036 friend class SSAUpdaterTraits<SSAUpdater>; 00037 00038 private: 00039 /// AvailableVals - This keeps track of which value to use on a per-block 00040 /// basis. When we insert PHI nodes, we keep track of them here. 00041 //typedef DenseMap<BasicBlock*, Value*> AvailableValsTy; 00042 void *AV; 00043 00044 /// ProtoType holds the type of the values being rewritten. 00045 Type *ProtoType; 00046 00047 // PHI nodes are given a name based on ProtoName. 00048 std::string ProtoName; 00049 00050 /// InsertedPHIs - If this is non-null, the SSAUpdater adds all PHI nodes that 00051 /// it creates to the vector. 00052 SmallVectorImpl<PHINode*> *InsertedPHIs; 00053 00054 public: 00055 /// SSAUpdater constructor. If InsertedPHIs is specified, it will be filled 00056 /// in with all PHI Nodes created by rewriting. 00057 explicit SSAUpdater(SmallVectorImpl<PHINode*> *InsertedPHIs = 0); 00058 ~SSAUpdater(); 00059 00060 /// Initialize - Reset this object to get ready for a new set of SSA 00061 /// updates with type 'Ty'. PHI nodes get a name based on 'Name'. 00062 void Initialize(Type *Ty, StringRef Name); 00063 00064 /// AddAvailableValue - Indicate that a rewritten value is available at the 00065 /// end of the specified block with the specified value. 00066 void AddAvailableValue(BasicBlock *BB, Value *V); 00067 00068 /// HasValueForBlock - Return true if the SSAUpdater already has a value for 00069 /// the specified block. 00070 bool HasValueForBlock(BasicBlock *BB) const; 00071 00072 /// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is 00073 /// live at the end of the specified block. 00074 Value *GetValueAtEndOfBlock(BasicBlock *BB); 00075 00076 /// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that 00077 /// is live in the middle of the specified block. 00078 /// 00079 /// GetValueInMiddleOfBlock is the same as GetValueAtEndOfBlock except in one 00080 /// important case: if there is a definition of the rewritten value after the 00081 /// 'use' in BB. Consider code like this: 00082 /// 00083 /// X1 = ... 00084 /// SomeBB: 00085 /// use(X) 00086 /// X2 = ... 00087 /// br Cond, SomeBB, OutBB 00088 /// 00089 /// In this case, there are two values (X1 and X2) added to the AvailableVals 00090 /// set by the client of the rewriter, and those values are both live out of 00091 /// their respective blocks. However, the use of X happens in the *middle* of 00092 /// a block. Because of this, we need to insert a new PHI node in SomeBB to 00093 /// merge the appropriate values, and this value isn't live out of the block. 00094 /// 00095 Value *GetValueInMiddleOfBlock(BasicBlock *BB); 00096 00097 /// RewriteUse - Rewrite a use of the symbolic value. This handles PHI nodes, 00098 /// which use their value in the corresponding predecessor. Note that this 00099 /// will not work if the use is supposed to be rewritten to a value defined in 00100 /// the same block as the use, but above it. Any 'AddAvailableValue's added 00101 /// for the use's block will be considered to be below it. 00102 void RewriteUse(Use &U); 00103 00104 /// RewriteUseAfterInsertions - Rewrite a use, just like RewriteUse. However, 00105 /// this version of the method can rewrite uses in the same block as a 00106 /// definition, because it assumes that all uses of a value are below any 00107 /// inserted values. 00108 void RewriteUseAfterInsertions(Use &U); 00109 00110 private: 00111 Value *GetValueAtEndOfBlockInternal(BasicBlock *BB); 00112 00113 void operator=(const SSAUpdater&) LLVM_DELETED_FUNCTION; 00114 SSAUpdater(const SSAUpdater&) LLVM_DELETED_FUNCTION; 00115 }; 00116 00117 /// LoadAndStorePromoter - This little helper class provides a convenient way to 00118 /// promote a collection of loads and stores into SSA Form using the SSAUpdater. 00119 /// This handles complexities that SSAUpdater doesn't, such as multiple loads 00120 /// and stores in one block. 00121 /// 00122 /// Clients of this class are expected to subclass this and implement the 00123 /// virtual methods. 00124 /// 00125 class LoadAndStorePromoter { 00126 protected: 00127 SSAUpdater &SSA; 00128 public: 00129 LoadAndStorePromoter(const SmallVectorImpl<Instruction*> &Insts, 00130 SSAUpdater &S, StringRef Name = StringRef()); 00131 virtual ~LoadAndStorePromoter() {} 00132 00133 /// run - This does the promotion. Insts is a list of loads and stores to 00134 /// promote, and Name is the basename for the PHIs to insert. After this is 00135 /// complete, the loads and stores are removed from the code. 00136 void run(const SmallVectorImpl<Instruction*> &Insts) const; 00137 00138 00139 /// Return true if the specified instruction is in the Inst list (which was 00140 /// passed into the run method). Clients should implement this with a more 00141 /// efficient version if possible. 00142 virtual bool isInstInList(Instruction *I, 00143 const SmallVectorImpl<Instruction*> &Insts) const; 00144 00145 /// doExtraRewritesBeforeFinalDeletion - This hook is invoked after all the 00146 /// stores are found and inserted as available values, but 00147 virtual void doExtraRewritesBeforeFinalDeletion() const { 00148 } 00149 00150 /// replaceLoadWithValue - Clients can choose to implement this to get 00151 /// notified right before a load is RAUW'd another value. 00152 virtual void replaceLoadWithValue(LoadInst *LI, Value *V) const { 00153 } 00154 00155 /// This is called before each instruction is deleted. 00156 virtual void instructionDeleted(Instruction *I) const { 00157 } 00158 00159 /// updateDebugInfo - This is called to update debug info associated with the 00160 /// instruction. 00161 virtual void updateDebugInfo(Instruction *I) const { 00162 } 00163 }; 00164 00165 } // End llvm namespace 00166 00167 #endif