LLVM  7.0.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 <string>
20 
21 namespace llvm {
22 
23 class BasicBlock;
24 class Instruction;
25 class LoadInst;
26 class PHINode;
27 template <typename T> class SmallVectorImpl;
28 template <typename T> class SSAUpdaterTraits;
29 class Type;
30 class Use;
31 class Value;
32 
33 /// 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 {
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  void *AV = nullptr;
46 
47  /// ProtoType holds the type of the values being rewritten.
48  Type *ProtoType = nullptr;
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(const SSAUpdater &) = delete;
62  SSAUpdater &operator=(const SSAUpdater &) = delete;
63  ~SSAUpdater();
64 
65  /// Reset this object to get ready for a new set of SSA updates with
66  /// type 'Ty'.
67  ///
68  /// PHI nodes get a name based on 'Name'.
69  void Initialize(Type *Ty, StringRef Name);
70 
71  /// Indicate that a rewritten value is available in the specified block
72  /// with the specified value.
73  void AddAvailableValue(BasicBlock *BB, Value *V);
74 
75  /// Return true if the SSAUpdater already has a value for the specified
76  /// block.
77  bool HasValueForBlock(BasicBlock *BB) const;
78 
79  /// Construct SSA form, materializing a value that is live at the end
80  /// of the specified block.
82 
83  /// Construct SSA form, materializing a value that is live in the
84  /// middle of the specified block.
85  ///
86  /// \c GetValueInMiddleOfBlock is the same as \c GetValueAtEndOfBlock except
87  /// in one important case: if there is a definition of the rewritten value
88  /// after the 'use' in BB. Consider code like this:
89  ///
90  /// \code
91  /// X1 = ...
92  /// SomeBB:
93  /// use(X)
94  /// X2 = ...
95  /// br Cond, SomeBB, OutBB
96  /// \endcode
97  ///
98  /// In this case, there are two values (X1 and X2) added to the AvailableVals
99  /// set by the client of the rewriter, and those values are both live out of
100  /// their respective blocks. However, the use of X happens in the *middle* of
101  /// a block. Because of this, we need to insert a new PHI node in SomeBB to
102  /// merge the appropriate values, and this value isn't live out of the block.
104 
105  /// Rewrite a use of the symbolic value.
106  ///
107  /// This handles PHI nodes, which use their value in the corresponding
108  /// predecessor. Note that this will not work if the use is supposed to be
109  /// rewritten to a value defined in the same block as the use, but above it.
110  /// Any 'AddAvailableValue's added for the use's block will be considered to
111  /// be below it.
112  void RewriteUse(Use &U);
113 
114  /// Rewrite a use like \c RewriteUse but handling in-block definitions.
115  ///
116  /// This version of the method can rewrite uses in the same block as
117  /// a definition, because it assumes that all uses of a value are below any
118  /// inserted values.
120 
121 private:
122  Value *GetValueAtEndOfBlockInternal(BasicBlock *BB);
123 };
124 
125 /// Helper class for promoting a collection of loads and stores into SSA
126 /// Form using the SSAUpdater.
127 ///
128 /// This handles complexities that SSAUpdater doesn't, such as multiple loads
129 /// and stores in one block.
130 ///
131 /// Clients of this class are expected to subclass this and implement the
132 /// virtual methods.
134 protected:
136 
137 public:
140  virtual ~LoadAndStorePromoter() = default;
141 
142  /// This does the promotion.
143  ///
144  /// Insts is a list of loads and stores to promote, and Name is the basename
145  /// for the PHIs to insert. After this is complete, the loads and stores are
146  /// removed from the code.
147  void run(const SmallVectorImpl<Instruction *> &Insts) const;
148 
149  /// Return true if the specified instruction is in the Inst list.
150  ///
151  /// The Insts list is the one passed into the constructor. Clients should
152  /// implement this with a more efficient version if possible.
153  virtual bool isInstInList(Instruction *I,
154  const SmallVectorImpl<Instruction *> &Insts) const;
155 
156  /// This hook is invoked after all the stores are found and inserted as
157  /// available values.
158  virtual void doExtraRewritesBeforeFinalDeletion() const {}
159 
160  /// Clients can choose to implement this to get notified right before
161  /// a load is RAUW'd another value.
162  virtual void replaceLoadWithValue(LoadInst *LI, Value *V) const {}
163 
164  /// Called before each instruction is deleted.
165  virtual void instructionDeleted(Instruction *I) const {}
166 
167  /// Called to update debug info associated with the instruction.
168  virtual void updateDebugInfo(Instruction *I) const {}
169 };
170 
171 } // end namespace llvm
172 
173 #endif // LLVM_TRANSFORMS_UTILS_SSAUPDATER_H
Helper class for SSA formation on a set of values defined in multiple blocks.
Definition: SSAUpdater.h:39
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
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 &#39;Ty&#39;.
Definition: SSAUpdater.cpp:54
virtual void instructionDeleted(Instruction *I) const
Called before each instruction is deleted.
Definition: SSAUpdater.h:165
void AddAvailableValue(BasicBlock *BB, Value *V)
Indicate that a rewritten value is available in the specified block with the specified value...
Definition: SSAUpdater.cpp:67
An instruction for reading from memory.
Definition: Instructions.h:168
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
SSAUpdater & operator=(const SSAUpdater &)=delete
virtual void doExtraRewritesBeforeFinalDeletion() const
This hook is invoked after all the stores are found and inserted as available values.
Definition: SSAUpdater.h:158
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
virtual void updateDebugInfo(Instruction *I) const
Called to update debug info associated with the instruction.
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:95
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
void RewriteUseAfterInsertions(Use &U)
Rewrite a use like RewriteUse but handling in-block definitions.
Definition: SSAUpdater.cpp:202
SSAUpdater(SmallVectorImpl< PHINode *> *InsertedPHIs=nullptr)
If InsertedPHIs is specified, it will be filled in with all PHI Nodes created by rewriting.
Definition: SSAUpdater.cpp:47
Helper class for promoting a collection of loads and stores into SSA Form using the SSAUpdater...
Definition: SSAUpdater.h:133
virtual void replaceLoadWithValue(LoadInst *LI, Value *V) const
Clients can choose to implement this to get notified right before a load is RAUW&#39;d another value...
Definition: SSAUpdater.h:162
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM Value Representation.
Definition: Value.h:73
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Value * GetValueAtEndOfBlock(BasicBlock *BB)
Construct SSA form, materializing a value that is live at the end of the specified block...
Definition: SSAUpdater.cpp:90
void RewriteUse(Use &U)
Rewrite a use of the symbolic value.
Definition: SSAUpdater.cpp:185
bool HasValueForBlock(BasicBlock *BB) const
Return true if the SSAUpdater already has a value for the specified block.
Definition: SSAUpdater.cpp:63