SSAUpdater.h 6.1 KB

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