LLVM 18.0.0git
SSAUpdater.h
Go to the documentation of this file.
1//===- SSAUpdater.h - Unstructured SSA Update Tool --------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares the SSAUpdater class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_TRANSFORMS_UTILS_SSAUPDATER_H
14#define LLVM_TRANSFORMS_UTILS_SSAUPDATER_H
15
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/StringRef.h"
18#include <string>
19
20namespace llvm {
21
22class BasicBlock;
23class Instruction;
24class LoadInst;
25class PHINode;
26template <typename T> class SmallVectorImpl;
27template <typename T> class SSAUpdaterTraits;
28class Type;
29class Use;
30class Value;
31class DbgValueInst;
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.
40 friend class SSAUpdaterTraits<SSAUpdater>;
41
42private:
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
57public:
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;
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.
74
75 /// Return true if the SSAUpdater already has a value for the specified
76 /// block.
77 bool HasValueForBlock(BasicBlock *BB) const;
78
79 /// Return the value for the specified block if the SSAUpdater has one,
80 /// otherwise return nullptr.
82
83 /// Construct SSA form, materializing a value that is live at the end
84 /// of the specified block.
86
87 /// Construct SSA form, materializing a value that is live in the
88 /// middle of the specified block.
89 ///
90 /// \c GetValueInMiddleOfBlock is the same as \c GetValueAtEndOfBlock except
91 /// in one important case: if there is a definition of the rewritten value
92 /// after the 'use' in BB. Consider code like this:
93 ///
94 /// \code
95 /// X1 = ...
96 /// SomeBB:
97 /// use(X)
98 /// X2 = ...
99 /// br Cond, SomeBB, OutBB
100 /// \endcode
101 ///
102 /// In this case, there are two values (X1 and X2) added to the AvailableVals
103 /// set by the client of the rewriter, and those values are both live out of
104 /// their respective blocks. However, the use of X happens in the *middle* of
105 /// a block. Because of this, we need to insert a new PHI node in SomeBB to
106 /// merge the appropriate values, and this value isn't live out of the block.
108
109 /// Rewrite a use of the symbolic value.
110 ///
111 /// This handles PHI nodes, which use their value in the corresponding
112 /// predecessor. Note that this will not work if the use is supposed to be
113 /// rewritten to a value defined in the same block as the use, but above it.
114 /// Any 'AddAvailableValue's added for the use's block will be considered to
115 /// be below it.
116 void RewriteUse(Use &U);
117
118 /// Rewrite debug value intrinsics to conform to a new SSA form.
119 ///
120 /// This will scout out all the debug value instrinsics associated with
121 /// the instruction. Anything outside of its block will have its
122 /// value set to the new SSA value if available, and undef if not.
126
127 /// Rewrite a use like \c RewriteUse but handling in-block definitions.
128 ///
129 /// This version of the method can rewrite uses in the same block as
130 /// a definition, because it assumes that all uses of a value are below any
131 /// inserted values.
133
134private:
135 Value *GetValueAtEndOfBlockInternal(BasicBlock *BB);
136 void UpdateDebugValue(Instruction *I, DbgValueInst *DbgValue);
137};
138
139/// Helper class for promoting a collection of loads and stores into SSA
140/// Form using the SSAUpdater.
141///
142/// This handles complexities that SSAUpdater doesn't, such as multiple loads
143/// and stores in one block.
144///
145/// Clients of this class are expected to subclass this and implement the
146/// virtual methods.
148protected:
150
151public:
154 virtual ~LoadAndStorePromoter() = default;
155
156 /// This does the promotion.
157 ///
158 /// Insts is a list of loads and stores to promote, and Name is the basename
159 /// for the PHIs to insert. After this is complete, the loads and stores are
160 /// removed from the code.
161 void run(const SmallVectorImpl<Instruction *> &Insts);
162
163 /// Return true if the specified instruction is in the Inst list.
164 ///
165 /// The Insts list is the one passed into the constructor. Clients should
166 /// implement this with a more efficient version if possible.
167 virtual bool isInstInList(Instruction *I,
168 const SmallVectorImpl<Instruction *> &Insts) const;
169
170 /// This hook is invoked after all the stores are found and inserted as
171 /// available values.
173
174 /// Clients can choose to implement this to get notified right before
175 /// a load is RAUW'd another value.
176 virtual void replaceLoadWithValue(LoadInst *LI, Value *V) const {}
177
178 /// Called before each instruction is deleted.
179 virtual void instructionDeleted(Instruction *I) const {}
180
181 /// Called to update debug info associated with the instruction.
182 virtual void updateDebugInfo(Instruction *I) const {}
183
184 /// Return false if a sub-class wants to keep one of the loads/stores
185 /// after the SSA construction.
186 virtual bool shouldDelete(Instruction *I) const { return true; }
187};
188
189} // end namespace llvm
190
191#endif // LLVM_TRANSFORMS_UTILS_SSAUPDATER_H
std::string Name
#define I(x, y, z)
Definition: MD5.cpp:58
Class recording the (high level) value of a variable.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
This represents the llvm.dbg.value instruction.
Helper class for promoting a collection of loads and stores into SSA Form using the SSAUpdater.
Definition: SSAUpdater.h:147
virtual void instructionDeleted(Instruction *I) const
Called before each instruction is deleted.
Definition: SSAUpdater.h:179
virtual void doExtraRewritesBeforeFinalDeletion()
This hook is invoked after all the stores are found and inserted as available values.
Definition: SSAUpdater.h:172
virtual bool isInstInList(Instruction *I, const SmallVectorImpl< Instruction * > &Insts) const
Return true if the specified instruction is in the Inst list.
Definition: SSAUpdater.cpp:508
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:176
void run(const SmallVectorImpl< Instruction * > &Insts)
This does the promotion.
Definition: SSAUpdater.cpp:366
virtual ~LoadAndStorePromoter()=default
virtual void updateDebugInfo(Instruction *I) const
Called to update debug info associated with the instruction.
Definition: SSAUpdater.h:182
virtual bool shouldDelete(Instruction *I) const
Return false if a sub-class wants to keep one of the loads/stores after the SSA construction.
Definition: SSAUpdater.h:186
An instruction for reading from memory.
Definition: Instructions.h:177
Helper class for SSA formation on a set of values defined in multiple blocks.
Definition: SSAUpdater.h:39
void RewriteUse(Use &U)
Rewrite a use of the symbolic value.
Definition: SSAUpdater.cpp:188
void RewriteUseAfterInsertions(Use &U)
Rewrite a use like RewriteUse but handling in-block definitions.
Definition: SSAUpdater.cpp:227
Value * FindValueForBlock(BasicBlock *BB) const
Return the value for the specified block if the SSAUpdater has one, otherwise return nullptr.
Definition: SSAUpdater.cpp:66
SSAUpdater & operator=(const SSAUpdater &)=delete
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:53
Value * GetValueInMiddleOfBlock(BasicBlock *BB)
Construct SSA form, materializing a value that is live in the middle of the specified block.
Definition: SSAUpdater.cpp:98
void UpdateDebugValues(Instruction *I)
Rewrite debug value intrinsics to conform to a new SSA form.
Definition: SSAUpdater.cpp:200
SSAUpdater(const SSAUpdater &)=delete
bool HasValueForBlock(BasicBlock *BB) const
Return true if the SSAUpdater already has a value for the specified block.
Definition: SSAUpdater.cpp:62
Value * GetValueAtEndOfBlock(BasicBlock *BB)
Construct SSA form, materializing a value that is live at the end of the specified block.
Definition: SSAUpdater.cpp:93
void AddAvailableValue(BasicBlock *BB, Value *V)
Indicate that a rewritten value is available in the specified block with the specified value.
Definition: SSAUpdater.cpp:70
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
LLVM Value Representation.
Definition: Value.h:74
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18