LLVM  3.7.0
SSAUpdater.h
Go to the documentation of this file.
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/ArrayRef.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/Compiler.h"
20 
21 namespace llvm {
22  class BasicBlock;
23  class Instruction;
24  class LoadInst;
25  template<typename T> class SmallVectorImpl;
26  template<typename T> class SSAUpdaterTraits;
27  class PHINode;
28  class Type;
29  class Use;
30  class Value;
31 
32 /// \brief Helper class for SSA formation on a set of values defined in
33 /// multiple blocks.
34 ///
35 /// This is used when code duplication or another unstructured
36 /// transformation wants to rewrite a set of uses of one value with uses of a
37 /// set of values.
38 class SSAUpdater {
40 
41 private:
42  /// This keeps track of which value to use on a per-block basis. When we
43  /// insert PHI nodes, we keep track of them here.
44  //typedef DenseMap<BasicBlock*, Value*> AvailableValsTy;
45  void *AV;
46 
47  /// ProtoType holds the type of the values being rewritten.
48  Type *ProtoType;
49 
50  /// PHI nodes are given a name based on ProtoName.
51  std::string ProtoName;
52 
53  /// If this is non-null, the SSAUpdater adds all PHI nodes that it creates to
54  /// the vector.
55  SmallVectorImpl<PHINode*> *InsertedPHIs;
56 
57 public:
58  /// If InsertedPHIs is specified, it will be filled
59  /// in with all PHI Nodes created by rewriting.
60  explicit SSAUpdater(SmallVectorImpl<PHINode*> *InsertedPHIs = nullptr);
61  ~SSAUpdater();
62 
63  /// \brief Reset this object to get ready for a new set of SSA updates with
64  /// type 'Ty'.
65  ///
66  /// PHI nodes get a name based on 'Name'.
67  void Initialize(Type *Ty, StringRef Name);
68 
69  /// \brief Indicate that a rewritten value is available in the specified block
70  /// with the specified value.
71  void AddAvailableValue(BasicBlock *BB, Value *V);
72 
73  /// \brief Return true if the SSAUpdater already has a value for the specified
74  /// block.
75  bool HasValueForBlock(BasicBlock *BB) const;
76 
77  /// \brief Construct SSA form, materializing a value that is live at the end
78  /// of the specified block.
80 
81  /// \brief Construct SSA form, materializing a value that is live in the
82  /// middle of the specified block.
83  ///
84  /// \c GetValueInMiddleOfBlock is the same as \c GetValueAtEndOfBlock except
85  /// in one important case: if there is a definition of the rewritten value
86  /// after the 'use' in BB. Consider code like this:
87  ///
88  /// \code
89  /// X1 = ...
90  /// SomeBB:
91  /// use(X)
92  /// X2 = ...
93  /// br Cond, SomeBB, OutBB
94  /// \endcode
95  ///
96  /// In this case, there are two values (X1 and X2) added to the AvailableVals
97  /// set by the client of the rewriter, and those values are both live out of
98  /// their respective blocks. However, the use of X happens in the *middle* of
99  /// a block. Because of this, we need to insert a new PHI node in SomeBB to
100  /// merge the appropriate values, and this value isn't live out of the block.
102 
103  /// \brief Rewrite a use of the symbolic value.
104  ///
105  /// This handles PHI nodes, which use their value in the corresponding
106  /// predecessor. Note that this will not work if the use is supposed to be
107  /// rewritten to a value defined in the same block as the use, but above it.
108  /// Any 'AddAvailableValue's added for the use's block will be considered to
109  /// be below it.
110  void RewriteUse(Use &U);
111 
112  /// \brief Rewrite a use like \c RewriteUse but handling in-block definitions.
113  ///
114  /// This version of the method can rewrite uses in the same block as
115  /// a definition, because it assumes that all uses of a value are below any
116  /// inserted values.
118 
119 private:
120  Value *GetValueAtEndOfBlockInternal(BasicBlock *BB);
121 
122  void operator=(const SSAUpdater&) = delete;
123  SSAUpdater(const SSAUpdater&) = delete;
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.
135 protected:
137 
138 public:
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 llvm namespace
177 
178 #endif
Helper class for SSA formation on a set of values defined in multiple blocks.
Definition: SSAUpdater.h:38
void run(const SmallVectorImpl< Instruction * > &Insts) const
This does the promotion.
Definition: SSAUpdater.cpp:342
Various leaf nodes.
Definition: ISDOpcodes.h:60
void Initialize(Type *Ty, StringRef Name)
Reset this object to get ready for a new set of SSA updates with type 'Ty'.
Definition: SSAUpdater.cpp:45
void AddAvailableValue(BasicBlock *BB, Value *V)
Indicate that a rewritten value is available in the specified block with the specified value...
Definition: SSAUpdater.cpp:58
virtual void updateDebugInfo(Instruction *I) const
Called to update debug info associated with the instruction.
Definition: SSAUpdater.h:172
LoadInst - an instruction for reading from memory.
Definition: Instructions.h:177
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APInt.h:33
virtual void doExtraRewritesBeforeFinalDeletion() const
This hook is invoked after all the stores are found and inserted as available values.
Definition: SSAUpdater.h:159
SSAUpdater(SmallVectorImpl< PHINode * > *InsertedPHIs=nullptr)
If InsertedPHIs is specified, it will be filled in with all PHI Nodes created by rewriting.
Definition: SSAUpdater.cpp:38
virtual void replaceLoadWithValue(LoadInst *LI, Value *V) const
Clients can choose to implement this to get notified right before a load is RAUW'd another value...
Definition: SSAUpdater.h:164
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
virtual void instructionDeleted(Instruction *I) const
Called before each instruction is deleted.
Definition: SSAUpdater.h:168
Value * GetValueInMiddleOfBlock(BasicBlock *BB)
Construct SSA form, materializing a value that is live in the middle of the specified block...
Definition: SSAUpdater.cpp:86
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
void RewriteUseAfterInsertions(Use &U)
Rewrite a use like RewriteUse but handling in-block definitions.
Definition: SSAUpdater.cpp:195
bool HasValueForBlock(BasicBlock *BB) const
Return true if the SSAUpdater already has a value for the specified block.
Definition: SSAUpdater.cpp:54
Helper class for promoting a collection of loads and stores into SSA Form using the SSAUpdater...
Definition: SSAUpdater.h:134
virtual bool isInstInList(Instruction *I, const SmallVectorImpl< Instruction * > &Insts) const
Return true if the specified instruction is in the Inst list.
Definition: SSAUpdater.cpp:482
#define I(x, y, z)
Definition: MD5.cpp:54
LLVM Value Representation.
Definition: Value.h:69
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
LoadAndStorePromoter(ArrayRef< const Instruction * > Insts, SSAUpdater &S, StringRef Name=StringRef())
Definition: SSAUpdater.cpp:325
Value * GetValueAtEndOfBlock(BasicBlock *BB)
Construct SSA form, materializing a value that is live at the end of the specified block...
Definition: SSAUpdater.cpp:81
void RewriteUse(Use &U)
Rewrite a use of the symbolic value.
Definition: SSAUpdater.cpp:178