LLVM API Documentation

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