LLVM 22.0.0git
SSAUpdaterBulk.cpp
Go to the documentation of this file.
1//===- SSAUpdaterBulk.cpp - Unstructured SSA Update Tool ------------------===//
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 implements the SSAUpdaterBulk class.
10//
11//===----------------------------------------------------------------------===//
12
16#include "llvm/IR/BasicBlock.h"
17#include "llvm/IR/Dominators.h"
18#include "llvm/IR/IRBuilder.h"
19#include "llvm/IR/Use.h"
20#include "llvm/IR/Value.h"
21
22using namespace llvm;
23
24#define DEBUG_TYPE "ssaupdaterbulk"
25
26/// Helper function for finding a block which should have a value for the given
27/// user. For PHI-nodes this block is the corresponding predecessor, for other
28/// instructions it's their parent block.
30 auto *User = cast<Instruction>(U->getUser());
31
32 if (auto *UserPN = dyn_cast<PHINode>(User))
33 return UserPN->getIncomingBlock(*U);
34 else
35 return User->getParent();
36}
37
38/// Add a new variable to the SSA rewriter. This needs to be called before
39/// AddAvailableValue or AddUse calls.
41 unsigned Var = Rewrites.size();
42 LLVM_DEBUG(dbgs() << "SSAUpdater: Var=" << Var << ": initialized with Ty = "
43 << *Ty << ", Name = " << Name << "\n");
44 RewriteInfo RI(Name, Ty);
45 Rewrites.push_back(RI);
46 return Var;
47}
48
49/// Indicate that a rewritten value is available in the specified block with the
50/// specified value.
52 assert(Var < Rewrites.size() && "Variable not found!");
53 LLVM_DEBUG(dbgs() << "SSAUpdater: Var=" << Var
54 << ": added new available value " << *V << " in "
55 << BB->getName() << "\n");
56 Rewrites[Var].Defines.emplace_back(BB, V);
57}
58
59/// Record a use of the symbolic value. This use will be updated with a
60/// rewritten value when RewriteAllUses is called.
61void SSAUpdaterBulk::AddUse(unsigned Var, Use *U) {
62 assert(Var < Rewrites.size() && "Variable not found!");
63 LLVM_DEBUG(dbgs() << "SSAUpdater: Var=" << Var << ": added a use" << *U->get()
64 << " in " << getUserBB(U)->getName() << "\n");
65 Rewrites[Var].Uses.push_back(U);
66}
67
68/// Given sets of UsingBlocks and DefBlocks, compute the set of LiveInBlocks.
69/// This is basically a subgraph limited by DefBlocks and UsingBlocks.
70static void
72 const SmallPtrSetImpl<BasicBlock *> &DefBlocks,
74 PredIteratorCache &PredCache) {
75 // To determine liveness, we must iterate through the predecessors of blocks
76 // where the def is live. Blocks are added to the worklist if we need to
77 // check their predecessors. Start with all the using blocks.
78 SmallVector<BasicBlock *, 64> LiveInBlockWorklist(UsingBlocks.begin(),
79 UsingBlocks.end());
80
81 // Now that we have a set of blocks where the phi is live-in, recursively add
82 // their predecessors until we find the full region the value is live.
83 while (!LiveInBlockWorklist.empty()) {
84 BasicBlock *BB = LiveInBlockWorklist.pop_back_val();
85
86 // The block really is live in here, insert it into the set. If already in
87 // the set, then it has already been processed.
88 if (!LiveInBlocks.insert(BB).second)
89 continue;
90
91 // Since the value is live into BB, it is either defined in a predecessor or
92 // live into it to. Add the preds to the worklist unless they are a
93 // defining block.
94 for (BasicBlock *P : PredCache.get(BB)) {
95 // The value is not live into a predecessor if it defines the value.
96 if (DefBlocks.count(P))
97 continue;
98
99 // Otherwise it is, add to the worklist.
100 LiveInBlockWorklist.push_back(P);
101 }
102 }
103}
104
106 Value *LiveInValue = nullptr;
107 Value *LiveOutValue = nullptr;
108};
109
110/// Perform all the necessary updates, including new PHI-nodes insertion and the
111/// requested uses update.
113 SmallVectorImpl<PHINode *> *InsertedPHIs) {
115 for (RewriteInfo &R : Rewrites) {
116 BBInfos.clear();
117
118 // Compute locations for new phi-nodes.
119 // For that we need to initialize DefBlocks from definitions in R.Defines,
120 // UsingBlocks from uses in R.Uses, then compute LiveInBlocks, and then use
121 // this set for computing iterated dominance frontier (IDF).
122 // The IDF blocks are the blocks where we need to insert new phi-nodes.
123 ForwardIDFCalculator IDF(*DT);
124 LLVM_DEBUG(dbgs() << "SSAUpdater: rewriting " << R.Uses.size()
125 << " use(s)\n");
126
128 llvm::make_first_range(R.Defines));
129 IDF.setDefiningBlocks(DefBlocks);
130
132 for (Use *U : R.Uses)
133 UsingBlocks.insert(getUserBB(U));
134
137 ComputeLiveInBlocks(UsingBlocks, DefBlocks, LiveInBlocks, PredCache);
138 IDF.setLiveInBlocks(LiveInBlocks);
139 IDF.calculate(IDFBlocks);
140
141 // Reserve sufficient buckets to prevent map growth. [1]
142 BBInfos.reserve(LiveInBlocks.size() + DefBlocks.size());
143
144 for (auto [BB, V] : R.Defines)
145 BBInfos[BB].LiveOutValue = V;
146
147 // We've computed IDF, now insert new phi-nodes there.
148 for (BasicBlock *FrontierBB : IDFBlocks) {
149 IRBuilder<> B(FrontierBB, FrontierBB->begin());
150 PHINode *PN = B.CreatePHI(R.Ty, 0, R.Name);
151 BBInfos[FrontierBB].LiveInValue = PN;
152 if (InsertedPHIs)
153 InsertedPHIs->push_back(PN);
154 }
155
156 // IsLiveOut indicates whether we are computing live-out values (true) or
157 // live-in values (false).
158 auto ComputeValue = [&](BasicBlock *BB, bool IsLiveOut) -> Value * {
159 BBValueInfo *BBInfo = &BBInfos[BB];
160
161 if (IsLiveOut && BBInfo->LiveOutValue)
162 return BBInfo->LiveOutValue;
163
164 if (BBInfo->LiveInValue)
165 return BBInfo->LiveInValue;
166
167 SmallVector<BBValueInfo *, 4> Stack = {BBInfo};
168 Value *V = nullptr;
169
170 while (DT->isReachableFromEntry(BB) && !PredCache.get(BB).empty() &&
171 (BB = DT->getNode(BB)->getIDom()->getBlock())) {
172 BBInfo = &BBInfos[BB];
173
174 if (BBInfo->LiveOutValue) {
175 V = BBInfo->LiveOutValue;
176 break;
177 }
178
179 if (BBInfo->LiveInValue) {
180 V = BBInfo->LiveInValue;
181 break;
182 }
183
184 Stack.emplace_back(BBInfo);
185 }
186
187 if (!V)
188 V = UndefValue::get(R.Ty);
189
190 for (BBValueInfo *BBInfo : Stack)
191 // Loop above can insert new entries into the BBInfos map: assume the
192 // map shouldn't grow due to [1] and BBInfo references are valid.
193 BBInfo->LiveInValue = V;
194
195 return V;
196 };
197
198 // Fill in arguments of the inserted PHIs.
199 for (BasicBlock *BB : IDFBlocks) {
200 auto *PHI = cast<PHINode>(&BB->front());
201 for (BasicBlock *Pred : PredCache.get(BB))
202 PHI->addIncoming(ComputeValue(Pred, /*IsLiveOut=*/true), Pred);
203 }
204
205 // Rewrite actual uses with the inserted definitions.
206 SmallPtrSet<Use *, 4> ProcessedUses;
207 for (Use *U : R.Uses) {
208 if (!ProcessedUses.insert(U).second)
209 continue;
210
211 auto *User = cast<Instruction>(U->getUser());
212 BasicBlock *BB = getUserBB(U);
213 Value *V = ComputeValue(BB, /*IsLiveOut=*/BB != User->getParent());
214 Value *OldVal = U->get();
215 assert(OldVal && "Invalid use!");
216 // Notify that users of the existing value that it is being replaced.
217 if (OldVal != V && OldVal->hasValueHandle())
219 LLVM_DEBUG(dbgs() << "SSAUpdater: replacing " << *OldVal << " with " << *V
220 << "\n");
221 U->set(V);
222 }
223 }
224}
225
226// Perform a single pass of simplification over the worklist of PHIs.
227// This should be called after RewriteAllUses() because simplifying PHIs
228// immediately after creation would require updating all references to those
229// PHIs in the BBValueInfo structures, which would necessitate additional
230// reference tracking overhead.
232 const DataLayout &DL) {
233 for (PHINode *&PHI : Worklist) {
234 if (Value *Simplified = simplifyInstruction(PHI, DL)) {
235 PHI->replaceAllUsesWith(Simplified);
236 PHI->eraseFromParent();
237 PHI = nullptr; // Mark as removed.
238 }
239 }
240}
241
242#ifndef NDEBUG // Should this be under EXPENSIVE_CHECKS?
243// New PHI nodes should not reference one another but they may reference
244// themselves or existing PHI nodes, and existing PHI nodes may reference new
245// PHI nodes.
246static bool
249 for (PHINode &PN : NewPHIs)
250 NewPHISet.insert(&PN);
251 for (PHINode &PHI : NewPHIs) {
252 for (Value *V : PHI.incoming_values()) {
253 PHINode *IncPHI = dyn_cast<PHINode>(V);
254 if (IncPHI && IncPHI != &PHI && NewPHISet.contains(IncPHI))
255 return true;
256 }
257 }
258 return false;
259}
260#endif
261
262static bool replaceIfIdentical(PHINode &PHI, PHINode &ReplPHI) {
263 if (!PHI.isIdenticalToWhenDefined(&ReplPHI))
264 return false;
265 PHI.replaceAllUsesWith(&ReplPHI);
266 PHI.eraseFromParent();
267 return true;
268}
269
271 BasicBlock::phi_iterator FirstExistingPN) {
272 assert(!PHIAreRefEachOther(make_range(BB->phis().begin(), FirstExistingPN)));
273
274 // Deduplicate new PHIs first to reduce the number of comparisons on the
275 // following new -> existing pass.
276 bool Changed = false;
277 for (auto I = BB->phis().begin(); I != FirstExistingPN; ++I) {
278 for (auto J = std::next(I); J != FirstExistingPN;) {
279 Changed |= replaceIfIdentical(*J++, *I);
280 }
281 }
282
283 // Iterate over existing PHIs and replace identical new PHIs.
284 for (PHINode &ExistingPHI : make_range(FirstExistingPN, BB->phis().end())) {
285 auto I = BB->phis().begin();
286 assert(I != FirstExistingPN); // Should be at least one new PHI.
287 do {
288 Changed |= replaceIfIdentical(*I++, ExistingPHI);
289 } while (I != FirstExistingPN);
290 if (BB->phis().begin() == FirstExistingPN)
291 return Changed;
292 }
293 return Changed;
294}
295
298 for (PHINode *PHI : Worklist) {
299 if (PHI)
300 ++BBs[PHI->getParent()];
301 }
302
303 for (auto [BB, NumNewPHIs] : BBs) {
304 auto FirstExistingPN = std::next(BB->phis().begin(), NumNewPHIs);
305 EliminateNewDuplicatePHINodes(BB, FirstExistingPN);
306 }
307}
308
311 RewriteAllUses(&DT, &PHIs);
312 if (PHIs.empty())
313 return;
314
315 simplifyPass(PHIs, PHIs.front()->getParent()->getDataLayout());
316 deduplicatePass(PHIs);
317}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Rewrite undef for PHI
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This defines the Use class.
#define I(x, y, z)
Definition MD5.cpp:58
#define P(N)
static StringRef getName(Value *V)
static BasicBlock * getUserBB(Use *U)
Helper function for finding a block which should have a value for the given user.
bool EliminateNewDuplicatePHINodes(BasicBlock *BB, BasicBlock::phi_iterator FirstExistingPN)
static void simplifyPass(MutableArrayRef< PHINode * > Worklist, const DataLayout &DL)
static void deduplicatePass(ArrayRef< PHINode * > Worklist)
static bool PHIAreRefEachOther(const iterator_range< BasicBlock::phi_iterator > NewPHIs)
static void ComputeLiveInBlocks(const SmallPtrSetImpl< BasicBlock * > &UsingBlocks, const SmallPtrSetImpl< BasicBlock * > &DefBlocks, SmallPtrSetImpl< BasicBlock * > &LiveInBlocks, PredIteratorCache &PredCache)
Given sets of UsingBlocks and DefBlocks, compute the set of LiveInBlocks.
static bool replaceIfIdentical(PHINode &PHI, PHINode &ReplPHI)
#define LLVM_DEBUG(...)
Definition Debug.h:114
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:62
phi_iterator_impl<> phi_iterator
Definition BasicBlock.h:521
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition BasicBlock.h:528
const Instruction & front() const
Definition BasicBlock.h:482
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition DenseMap.h:114
DomTreeNodeBase * getIDom() const
NodeT * getBlock() const
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:165
LLVM_ABI bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
void calculate(SmallVectorImpl< NodeTy * > &IDFBlocks)
Calculate iterated dominance frontiers.
void setLiveInBlocks(const SmallPtrSetImpl< NodeTy * > &Blocks)
Give the IDF calculator the set of blocks in which the value is live on entry to the block.
void setDefiningBlocks(const SmallPtrSetImpl< NodeTy * > &Blocks)
Give the IDF calculator the set of blocks in which the value is defined.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2788
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:303
PredIteratorCache - This class is an extremely trivial cache for predecessor iterator queries.
ArrayRef< BasicBlock * > get(BasicBlock *BB)
LLVM_ABI unsigned AddVariable(StringRef Name, Type *Ty)
Add a new variable to the SSA rewriter.
LLVM_ABI void AddAvailableValue(unsigned Var, BasicBlock *BB, Value *V)
Indicate that a rewritten value is available in the specified block with the specified value.
LLVM_ABI void RewriteAllUses(DominatorTree *DT, SmallVectorImpl< PHINode * > *InsertedPHIs=nullptr)
Perform all the necessary updates, including new PHI-nodes insertion and the requested uses update.
LLVM_ABI void AddUse(unsigned Var, Use *U)
Record a use of the symbolic value.
void RewriteAndOptimizeAllUses(DominatorTree &DT)
Rewrite all uses and simplify the inserted PHI nodes.
size_type size() const
Definition SmallPtrSet.h:99
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
iterator end() const
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
iterator begin() const
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
static LLVM_ABI void ValueIsRAUWd(Value *Old, Value *New)
Definition Value.cpp:1279
LLVM Value Representation.
Definition Value.h:75
bool hasValueHandle() const
Return true if there is a value handle associated with this value.
Definition Value.h:565
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
A range adaptor for a pair of iterators.
Changed
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:644
constexpr from_range_t from_range
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
auto make_first_range(ContainerTy &&c)
Given a container of pairs, return a range over the first elements.
Definition STLExtras.h:1397
IDFCalculator< false > ForwardIDFCalculator
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:560