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