LLVM 23.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;
27template <typename T> class SmallVectorImpl;
28template <typename T> class SSAUpdaterTraits;
29class Type;
30class Use;
31class 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.
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 LLVM_ABI explicit SSAUpdater(
61 SmallVectorImpl<PHINode *> *InsertedPHIs = nullptr);
62 SSAUpdater(const SSAUpdater &) = delete;
63 SSAUpdater &operator=(const SSAUpdater &) = delete;
65
66 /// 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 LLVM_ABI void Initialize(Type *Ty, StringRef Name);
71
72 /// Indicate that a rewritten value is available in the specified block
73 /// with the specified value.
75
76 /// Return true if the SSAUpdater already has a value for the specified
77 /// block.
78 LLVM_ABI bool HasValueForBlock(BasicBlock *BB) const;
79
80 /// Return the value for the specified block if the SSAUpdater has one,
81 /// otherwise return nullptr.
83
84 /// Construct SSA form, materializing a value that is live at the end
85 /// of the specified block.
87
88 /// Construct SSA form, materializing a value that is live in the
89 /// middle of the specified block.
90 ///
91 /// \c GetValueInMiddleOfBlock is the same as \c GetValueAtEndOfBlock except
92 /// in one important case: if there is a definition of the rewritten value
93 /// after the 'use' in BB. Consider code like this:
94 ///
95 /// \code
96 /// X1 = ...
97 /// SomeBB:
98 /// use(X)
99 /// X2 = ...
100 /// br Cond, SomeBB, OutBB
101 /// \endcode
102 ///
103 /// In this case, there are two values (X1 and X2) added to the AvailableVals
104 /// set by the client of the rewriter, and those values are both live out of
105 /// their respective blocks. However, the use of X happens in the *middle* of
106 /// a block. Because of this, we need to insert a new PHI node in SomeBB to
107 /// merge the appropriate values, and this value isn't live out of the block.
109
110 /// Rewrite a use of the symbolic value.
111 ///
112 /// This handles PHI nodes, which use their value in the corresponding
113 /// predecessor. Note that this will not work if the use is supposed to be
114 /// rewritten to a value defined in the same block as the use, but above it.
115 /// Any 'AddAvailableValue's added for the use's block will be considered to
116 /// be below it.
117 LLVM_ABI void RewriteUse(Use &U);
118
119 /// Rewrite debug value intrinsics to conform to a new SSA form.
120 ///
121 /// This will scout out all the debug value intrinsics associated with
122 /// the instruction. Anything outside of its block will have its
123 /// value set to the new SSA value if available, and undef if not.
125 LLVM_ABI void
128
129 /// Rewrite a use like \c RewriteUse but handling in-block definitions.
130 ///
131 /// This version of the method can rewrite uses in the same block as
132 /// a definition, because it assumes that all uses of a value are below any
133 /// inserted values.
135
136private:
137 Value *GetValueAtEndOfBlockInternal(BasicBlock *BB);
138 void UpdateDebugValue(Instruction *I, DbgVariableRecord *DbgValue);
139};
140
141/// Helper class for promoting a collection of loads and stores into SSA
142/// Form using the SSAUpdater.
143///
144/// This handles complexities that SSAUpdater doesn't, such as multiple loads
145/// and stores in one block.
146///
147/// Clients of this class are expected to subclass this and implement the
148/// virtual methods.
150protected:
152
153public:
155 SSAUpdater &S, StringRef Name = StringRef());
156 virtual ~LoadAndStorePromoter() = default;
157
158 /// This does the promotion.
159 ///
160 /// Insts is a list of loads and stores to promote, and Name is the basename
161 /// for the PHIs to insert. After this is complete, the loads and stores are
162 /// removed from the code.
164
165 /// This hook is invoked after all the stores are found and inserted as
166 /// available values.
168
169 /// Clients can choose to implement this to get notified right before
170 /// a load is RAUW'd another value.
171 virtual void replaceLoadWithValue(LoadInst *LI, Value *V) const {}
172
173 /// Called before each instruction is deleted.
174 virtual void instructionDeleted(Instruction *I) const {}
175
176 /// Called to update debug info associated with the instruction.
177 virtual void updateDebugInfo(Instruction *I) const {}
178
179 /// Return false if a sub-class wants to keep one of the loads/stores
180 /// after the SSA construction.
181 virtual bool shouldDelete(Instruction *I) const { return true; }
182
183 /// Return the value to use for the point in the code that the alloca is
184 /// positioned. This will only be used if an Alloca is included in Insts,
185 /// otherwise the value of a uninitialized load will be assumed to be poison.
187 return nullptr;
188 }
189};
190
191} // end namespace llvm
192
193#endif // LLVM_TRANSFORMS_UTILS_SSAUPDATER_H
#define LLVM_ABI
Definition Compiler.h:213
#define I(x, y, z)
Definition MD5.cpp:57
Class recording the (high level) value of a variable.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Record of a variable value-assignment, aka a non instruction representation of the dbg....
virtual Value * getValueToUseForAlloca(Instruction *AI) const
Return the value to use for the point in the code that the alloca is positioned.
Definition SSAUpdater.h:186
virtual void instructionDeleted(Instruction *I) const
Called before each instruction is deleted.
Definition SSAUpdater.h:174
virtual void doExtraRewritesBeforeFinalDeletion()
This hook is invoked after all the stores are found and inserted as available values.
Definition SSAUpdater.h:167
LLVM_ABI LoadAndStorePromoter(ArrayRef< const Instruction * > Insts, SSAUpdater &S, StringRef Name=StringRef())
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:171
LLVM_ABI void run(const SmallVectorImpl< Instruction * > &Insts)
This does the promotion.
virtual ~LoadAndStorePromoter()=default
virtual void updateDebugInfo(Instruction *I) const
Called to update debug info associated with the instruction.
Definition SSAUpdater.h:177
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:181
An instruction for reading from memory.
Helper class for SSA formation on a set of values defined in multiple blocks.
Definition SSAUpdater.h:39
LLVM_ABI void RewriteUse(Use &U)
Rewrite a use of the symbolic value.
LLVM_ABI void RewriteUseAfterInsertions(Use &U)
Rewrite a use like RewriteUse but handling in-block definitions.
LLVM_ABI Value * FindValueForBlock(BasicBlock *BB) const
Return the value for the specified block if the SSAUpdater has one, otherwise return nullptr.
SSAUpdater & operator=(const SSAUpdater &)=delete
LLVM_ABI void Initialize(Type *Ty, StringRef Name)
Reset this object to get ready for a new set of SSA updates with type 'Ty'.
LLVM_ABI ~SSAUpdater()
LLVM_ABI Value * GetValueInMiddleOfBlock(BasicBlock *BB)
Construct SSA form, materializing a value that is live in the middle of the specified block.
LLVM_ABI SSAUpdater(SmallVectorImpl< PHINode * > *InsertedPHIs=nullptr)
If InsertedPHIs is specified, it will be filled in with all PHI Nodes created by rewriting.
LLVM_ABI void UpdateDebugValues(Instruction *I)
Rewrite debug value intrinsics to conform to a new SSA form.
SSAUpdater(const SSAUpdater &)=delete
LLVM_ABI bool HasValueForBlock(BasicBlock *BB) const
Return true if the SSAUpdater already has a value for the specified block.
LLVM_ABI Value * GetValueAtEndOfBlock(BasicBlock *BB)
Construct SSA form, materializing a value that is live at the end of the specified block.
LLVM_ABI void AddAvailableValue(BasicBlock *BB, Value *V)
Indicate that a rewritten value is available in the specified block with the specified value.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM Value Representation.
Definition Value.h:75
This is an optimization pass for GlobalISel generic memory operations.