[Alignment][NFC] Migrate Instructions to Align
[llvm-core.git] / include / llvm / Transforms / Utils / SSAUpdater.h
blob22b2295cc9d70e49804e2cb32793dc70af11f438
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.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_TRANSFORMS_UTILS_SSAUPDATER_H
14 #define LLVM_TRANSFORMS_UTILS_SSAUPDATER_H
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/StringRef.h"
18 #include <string>
20 namespace llvm {
22 class BasicBlock;
23 class Instruction;
24 class LoadInst;
25 class PHINode;
26 template <typename T> class SmallVectorImpl;
27 template <typename T> class SSAUpdaterTraits;
28 class Type;
29 class Use;
30 class Value;
32 /// 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 {
39 friend class SSAUpdaterTraits<SSAUpdater>;
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 void *AV = nullptr;
46 /// ProtoType holds the type of the values being rewritten.
47 Type *ProtoType = nullptr;
49 /// PHI nodes are given a name based on ProtoName.
50 std::string ProtoName;
52 /// If this is non-null, the SSAUpdater adds all PHI nodes that it creates to
53 /// the vector.
54 SmallVectorImpl<PHINode *> *InsertedPHIs;
56 public:
57 /// If InsertedPHIs is specified, it will be filled
58 /// in with all PHI Nodes created by rewriting.
59 explicit SSAUpdater(SmallVectorImpl<PHINode *> *InsertedPHIs = nullptr);
60 SSAUpdater(const SSAUpdater &) = delete;
61 SSAUpdater &operator=(const SSAUpdater &) = delete;
62 ~SSAUpdater();
64 /// Reset this object to get ready for a new set of SSA updates with
65 /// type 'Ty'.
66 ///
67 /// PHI nodes get a name based on 'Name'.
68 void Initialize(Type *Ty, StringRef Name);
70 /// Indicate that a rewritten value is available in the specified block
71 /// with the specified value.
72 void AddAvailableValue(BasicBlock *BB, Value *V);
74 /// Return true if the SSAUpdater already has a value for the specified
75 /// block.
76 bool HasValueForBlock(BasicBlock *BB) const;
78 /// Return the value for the specified block if the SSAUpdater has one,
79 /// otherwise return nullptr.
80 Value *FindValueForBlock(BasicBlock *BB) const;
82 /// Construct SSA form, materializing a value that is live at the end
83 /// of the specified block.
84 Value *GetValueAtEndOfBlock(BasicBlock *BB);
86 /// Construct SSA form, materializing a value that is live in the
87 /// middle of the specified block.
88 ///
89 /// \c GetValueInMiddleOfBlock is the same as \c GetValueAtEndOfBlock except
90 /// in one important case: if there is a definition of the rewritten value
91 /// after the 'use' in BB. Consider code like this:
92 ///
93 /// \code
94 /// X1 = ...
95 /// SomeBB:
96 /// use(X)
97 /// X2 = ...
98 /// br Cond, SomeBB, OutBB
99 /// \endcode
101 /// In this case, there are two values (X1 and X2) added to the AvailableVals
102 /// set by the client of the rewriter, and those values are both live out of
103 /// their respective blocks. However, the use of X happens in the *middle* of
104 /// a block. Because of this, we need to insert a new PHI node in SomeBB to
105 /// merge the appropriate values, and this value isn't live out of the block.
106 Value *GetValueInMiddleOfBlock(BasicBlock *BB);
108 /// Rewrite a use of the symbolic value.
110 /// This handles PHI nodes, which use their value in the corresponding
111 /// predecessor. Note that this will not work if the use is supposed to be
112 /// rewritten to a value defined in the same block as the use, but above it.
113 /// Any 'AddAvailableValue's added for the use's block will be considered to
114 /// be below it.
115 void RewriteUse(Use &U);
117 /// Rewrite a use like \c RewriteUse but handling in-block definitions.
119 /// This version of the method can rewrite uses in the same block as
120 /// a definition, because it assumes that all uses of a value are below any
121 /// inserted values.
122 void RewriteUseAfterInsertions(Use &U);
124 private:
125 Value *GetValueAtEndOfBlockInternal(BasicBlock *BB);
128 /// Helper class for promoting a collection of loads and stores into SSA
129 /// Form using the SSAUpdater.
131 /// This handles complexities that SSAUpdater doesn't, such as multiple loads
132 /// and stores in one block.
134 /// Clients of this class are expected to subclass this and implement the
135 /// virtual methods.
136 class LoadAndStorePromoter {
137 protected:
138 SSAUpdater &SSA;
140 public:
141 LoadAndStorePromoter(ArrayRef<const Instruction *> Insts,
142 SSAUpdater &S, StringRef Name = StringRef());
143 virtual ~LoadAndStorePromoter() = default;
145 /// This does the promotion.
147 /// Insts is a list of loads and stores to promote, and Name is the basename
148 /// for the PHIs to insert. After this is complete, the loads and stores are
149 /// removed from the code.
150 void run(const SmallVectorImpl<Instruction *> &Insts);
152 /// Return true if the specified instruction is in the Inst list.
154 /// The Insts list is the one passed into the constructor. Clients should
155 /// implement this with a more efficient version if possible.
156 virtual bool isInstInList(Instruction *I,
157 const SmallVectorImpl<Instruction *> &Insts) const;
159 /// This hook is invoked after all the stores are found and inserted as
160 /// available values.
161 virtual void doExtraRewritesBeforeFinalDeletion() {}
163 /// Clients can choose to implement this to get notified right before
164 /// a load is RAUW'd another value.
165 virtual void replaceLoadWithValue(LoadInst *LI, Value *V) const {}
167 /// Called before each instruction is deleted.
168 virtual void instructionDeleted(Instruction *I) const {}
170 /// Called to update debug info associated with the instruction.
171 virtual void updateDebugInfo(Instruction *I) const {}
174 } // end namespace llvm
176 #endif // LLVM_TRANSFORMS_UTILS_SSAUPDATER_H