Debian Codegraph @

external/llvm/releases/22.0.0/patches_external/make-getPreviousDefRecursive-iterative.patch

raw
1 /*========================== begin_copyright_notice ============================
2
3 Copyright (C) 2026 Intel Corporation
4
5 SPDX-License-Identifier: MIT
6
7 ============================= end_copyright_notice ===========================*/
8
9 # Description : Refactor getPreviousDefRecursive to getPreviousDefIterative
10
11 diff --git a/llvm/include/llvm/Analysis/MemorySSAUpdater.h b/llvm/include/llvm/Analysis/MemorySSAUpdater.h
12 --- a/llvm/include/llvm/Analysis/MemorySSAUpdater.h
13 +++ b/llvm/include/llvm/Analysis/MemorySSAUpdater.h
14 @@ -251,10 +251,7 @@ private:
15 MemoryAccess *getPreviousDef(MemoryAccess *);
16 MemoryAccess *getPreviousDefInBlock(MemoryAccess *);
17 MemoryAccess *
18 - getPreviousDefFromEnd(BasicBlock *,
19 - DenseMap<BasicBlock *, TrackingVH<MemoryAccess>> &);
20 - MemoryAccess *
21 - getPreviousDefRecursive(BasicBlock *,
22 + getPreviousDefIterative(BasicBlock *,
23 DenseMap<BasicBlock *, TrackingVH<MemoryAccess>> &);
24 MemoryAccess *recursePhi(MemoryAccess *Phi);
25 MemoryAccess *tryRemoveTrivialPhi(MemoryPhi *Phi);
26 diff --git a/llvm/lib/Analysis/MemorySSAUpdater.cpp b/llvm/lib/Analysis/MemorySSAUpdater.cpp
27 --- a/llvm/lib/Analysis/MemorySSAUpdater.cpp
28 +++ b/llvm/lib/Analysis/MemorySSAUpdater.cpp
29 @@ -20,6 +20,7 @@
30 #include "llvm/IR/Dominators.h"
31 #include "llvm/Support/Debug.h"
32 #include <algorithm>
33 +#include <stack>
34
35 #define DEBUG_TYPE "memoryssa"
36 using namespace llvm;
37 @@ -33,66 +34,42 @@ using namespace llvm;
38 // that there are two or more definitions needing to be merged.
39 // This still will leave non-minimal form in the case of irreducible control
40 // flow, where phi nodes may be in cycles with themselves, but unnecessary.
41 -MemoryAccess *MemorySSAUpdater::getPreviousDefRecursive(
42 - BasicBlock *BB,
43 +MemoryAccess *MemorySSAUpdater::getPreviousDefIterative(
44 + BasicBlock *BBB,
45 DenseMap<BasicBlock *, TrackingVH<MemoryAccess>> &CachedPreviousDef) {
46 - // First, do a cache lookup. Without this cache, certain CFG structures
47 - // (like a series of if statements) take exponential time to visit.
48 - auto Cached = CachedPreviousDef.find(BB);
49 - if (Cached != CachedPreviousDef.end())
50 - return Cached->second;
51 -
52 - // If this method is called from an unreachable block, return LoE.
53 - if (!MSSA->DT->isReachableFromEntry(BB))
54 - return MSSA->getLiveOnEntryDef();
55
56 - if (BasicBlock *Pred = BB->getUniquePredecessor()) {
57 - VisitedBlocks.insert(BB);
58 - // Single predecessor case, just recurse, we can only have one definition.
59 - MemoryAccess *Result = getPreviousDefFromEnd(Pred, CachedPreviousDef);
60 - CachedPreviousDef.insert({BB, Result});
61 - return Result;
62 - }
63 + // There're 5 cases, case 3 (easy) and case 5 (hard) has recursives.
64 + // We need special states to handle their recursive returns
65 + enum State {COMMON, CASE3, CASE5};
66
67 - if (VisitedBlocks.count(BB)) {
68 - // We hit our node again, meaning we had a cycle, we must insert a phi
69 - // node to break it so we have an operand. The only case this will
70 - // insert useless phis is if we have irreducible control flow.
71 - MemoryAccess *Result = MSSA->createMemoryPhi(BB);
72 - CachedPreviousDef.insert({BB, Result});
73 - return Result;
74 - }
75 + // This is the common frame required for everything
76 + struct Frame {
77 + BasicBlock *bb;
78 + MemoryAccess *rtn;
79 + State st;
80 + };
81
82 - if (VisitedBlocks.insert(BB).second) {
83 - // Mark us visited so we can detect a cycle
84 + // This is the additional info only required by Case 5
85 + struct FrameCase5 {
86 SmallVector<TrackingVH<MemoryAccess>, 8> PhiOps;
87 + bool UniqueIncomingAccess;
88 + MemoryAccess *SingleAccess;
89 + pred_iterator PredIt;
90 + };
91
92 - // Recurse to get the values in our predecessors for placement of a
93 - // potential phi node. This will insert phi nodes if we cycle in order to
94 - // break the cycle and have an operand.
95 - bool UniqueIncomingAccess = true;
96 - MemoryAccess *SingleAccess = nullptr;
97 - for (auto *Pred : predecessors(BB)) {
98 - if (MSSA->DT->isReachableFromEntry(Pred)) {
99 - auto *IncomingAccess = getPreviousDefFromEnd(Pred, CachedPreviousDef);
100 - if (!SingleAccess)
101 - SingleAccess = IncomingAccess;
102 - else if (IncomingAccess != SingleAccess)
103 - UniqueIncomingAccess = false;
104 - PhiOps.push_back(IncomingAccess);
105 - } else
106 - PhiOps.push_back(MSSA->getLiveOnEntryDef());
107 - }
108 -
109 + auto Case5AfterLoop = [&](SmallVector<TrackingVH<MemoryAccess>, 8> & PhiOps,
110 + bool & UniqueIncomingAccess, MemoryAccess *& SingleAccess,
111 + BasicBlock * BB) -> MemoryAccess * {
112 // Now try to simplify the ops to avoid placing a phi.
113 // This may return null if we never created a phi yet, that's okay
114 MemoryPhi *Phi = dyn_cast_or_null<MemoryPhi>(MSSA->getMemoryAccess(BB));
115
116 // See if we can avoid the phi by simplifying it.
117 - auto *Result = tryRemoveTrivialPhi(Phi, PhiOps);
118 + MemoryAccess *Result = tryRemoveTrivialPhi(Phi, PhiOps);
119 // If we couldn't simplify, we may have to create a phi
120 if (Result == Phi && UniqueIncomingAccess && SingleAccess) {
121 - // A concrete Phi only exists if we created an empty one to break a cycle.
122 + // A concrete Phi only exists if we created an empty one to break a
123 + // cycle.
124 if (Phi) {
125 assert(Phi->operands().empty() && "Expected empty Phi");
126 Phi->replaceAllUsesWith(SingleAccess);
127 @@ -104,12 +81,13 @@ MemoryAccess *MemorySSAUpdater::getPreviousDefRecursive(
128 Phi = MSSA->createMemoryPhi(BB);
129
130 // See if the existing phi operands match what we need.
131 - // Unlike normal SSA, we only allow one phi node per block, so we can't just
132 - // create a new one.
133 + // Unlike normal SSA, we only allow one phi node per block, so we
134 + // can't just create a new one.
135 if (Phi->getNumOperands() != 0) {
136 // FIXME: Figure out whether this is dead code and if so remove it.
137 if (!std::equal(Phi->op_begin(), Phi->op_end(), PhiOps.begin())) {
138 - // These will have been filled in by the recursive read we did above.
139 + // These will have been filled in by the recursive read we did
140 + // above.
141 llvm::copy(PhiOps, Phi->op_begin());
142 std::copy(pred_begin(BB), pred_end(BB), Phi->block_begin());
143 }
144 @@ -126,8 +104,170 @@ MemoryAccess *MemorySSAUpdater::getPreviousDefRecursive(
145 VisitedBlocks.erase(BB);
146 CachedPreviousDef.insert({BB, Result});
147 return Result;
148 + };
149 +
150 + // We may want to switch to vector to boot performance
151 + std::stack<Frame> SF;
152 + std::stack<FrameCase5> SF5;
153 + // The return frame
154 + SF.push({nullptr, nullptr, COMMON});
155 + // The entry frame
156 + SF.push({BBB, nullptr, COMMON});
157 +
158 + while (SF.size() > 1) {
159 +
160 + if (COMMON == SF.top().st) {
161 + auto BB = SF.top().bb;
162 + auto Cached = CachedPreviousDef.find(BB);
163 + if (Cached != CachedPreviousDef.end()) {
164 + SF.pop();
165 + SF.top().rtn = Cached->second;
166 + continue;
167 + } else if (!MSSA->DT->isReachableFromEntry(BB)) {
168 + SF.pop();
169 + SF.top().rtn = MSSA->getLiveOnEntryDef();
170 + continue;
171 + } else if (BasicBlock *Pred = BB->getUniquePredecessor()) {
172 + VisitedBlocks.insert(BB);
173 + // Single predecessor case, just recurse, we can only have one
174 + // definition.
175 + MemoryAccess *prevDefFromEnd = nullptr;
176 + auto *Defs = MSSA->getWritableBlockDefs(Pred);
177 + if (Defs) {
178 + CachedPreviousDef.insert({Pred, &*Defs->rbegin()});
179 + prevDefFromEnd = &*Defs->rbegin();
180 + } else {
181 + SF.top().st = CASE3;
182 + SF.push({Pred, nullptr, COMMON});
183 + continue;
184 + }
185 + MemoryAccess *Result = prevDefFromEnd;
186 + CachedPreviousDef.insert({BB, Result});
187 + SF.pop();
188 + SF.top().rtn = Result;
189 + continue;
190 + } else if (VisitedBlocks.count(BB)) {
191 + // We hit our node again, meaning we had a cycle, we must insert a phi
192 + // node to break it so we have an operand. The only case this will
193 + // insert useless phis is if we have irreducible control flow.
194 + MemoryAccess *Result = MSSA->createMemoryPhi(BB);
195 + CachedPreviousDef.insert({BB, Result});
196 + SF.pop();
197 + SF.top().rtn = Result;
198 + continue;
199 + } else if (VisitedBlocks.insert(BB).second) {
200 + // Mark us visited so we can detect a cycle
201 + SmallVector<TrackingVH<MemoryAccess>, 8> PhiOps;
202 +
203 + // Recurse to get the values in our predecessors for placement of a
204 + // potential phi node. This will insert phi nodes if we cycle in order
205 + // to break the cycle and have an operand.
206 + bool UniqueIncomingAccess = true;
207 + MemoryAccess *SingleAccess = nullptr;
208 + bool halt = false;
209 + for (auto PredIt = predecessors(BB).begin();
210 + PredIt != predecessors(BB).end(); PredIt++) {
211 + auto Pred = *PredIt;
212 + if (MSSA->DT->isReachableFromEntry(Pred)) {
213 + MemoryAccess *prevDefFromEnd = nullptr;
214 + auto *Defs = MSSA->getWritableBlockDefs(Pred);
215 + if (Defs) {
216 + CachedPreviousDef.insert({Pred, &*Defs->rbegin()});
217 + prevDefFromEnd = &*Defs->rbegin();
218 + } else {
219 + SF.top().st = CASE5;
220 + SF.push({Pred, nullptr, COMMON});
221 + SF5.push({
222 + std::move(PhiOps), UniqueIncomingAccess, SingleAccess,
223 + std::move(PredIt)
224 + });
225 + halt = true;
226 + break;
227 + }
228 + auto *IncomingAccess = prevDefFromEnd;
229 + if (!SingleAccess)
230 + SingleAccess = IncomingAccess;
231 + else if (IncomingAccess != SingleAccess)
232 + UniqueIncomingAccess = false;
233 + PhiOps.push_back(IncomingAccess);
234 + } else
235 + PhiOps.push_back(MSSA->getLiveOnEntryDef());
236 + }
237 + if (halt)
238 + continue;
239 +
240 + auto Result =
241 + Case5AfterLoop(PhiOps, UniqueIncomingAccess, SingleAccess, BB);
242 +
243 + // Set ourselves up for the next variable by resetting visited state.
244 + VisitedBlocks.erase(BB);
245 + CachedPreviousDef.insert({BB, Result});
246 + SF.pop();
247 + SF.top().rtn = Result;
248 + continue;
249 + }
250 + llvm_unreachable("Should have hit one of the five cases above");
251 + } else if (CASE3 == SF.top().st) {
252 + auto Result = SF.top().rtn;
253 + CachedPreviousDef.insert({SF.top().bb, Result});
254 + SF.pop();
255 + SF.top().rtn = Result;
256 + continue;
257 + } else { // CASE5
258 + // recover header
259 + auto &PhiOps = SF5.top().PhiOps;
260 + auto &UniqueIncomingAccess = SF5.top().UniqueIncomingAccess;
261 + auto &SingleAccess = SF5.top().SingleAccess;
262 + auto &PredIt = SF5.top().PredIt;
263 + auto IncomingAccess = SF.top().rtn;
264 + auto BB = SF.top().bb;
265 +
266 + // in-loop remaining code
267 + if (!SingleAccess)
268 + SingleAccess = IncomingAccess;
269 + else if (IncomingAccess != SingleAccess)
270 + UniqueIncomingAccess = false;
271 + PhiOps.push_back(IncomingAccess);
272 +
273 + // remaining loop
274 + bool halt = false;
275 + for (PredIt++; PredIt != predecessors(BB).end(); PredIt++) {
276 + auto Pred = *PredIt;
277 + if (MSSA->DT->isReachableFromEntry(Pred)) {
278 + MemoryAccess *prevDefFromEnd = nullptr;
279 + auto *Defs = MSSA->getWritableBlockDefs(Pred);
280 + if (Defs) {
281 + CachedPreviousDef.insert({Pred, &*Defs->rbegin()});
282 + prevDefFromEnd = &*Defs->rbegin();
283 + } else {
284 + SF.push({Pred, nullptr, COMMON});
285 + halt = true;
286 + break;
287 + }
288 + auto *IncomingAccess = prevDefFromEnd;
289 + if (!SingleAccess)
290 + SingleAccess = IncomingAccess;
291 + else if (IncomingAccess != SingleAccess)
292 + UniqueIncomingAccess = false;
293 + PhiOps.push_back(IncomingAccess);
294 + } else
295 + PhiOps.push_back(MSSA->getLiveOnEntryDef());
296 + }
297 + if (halt)
298 + continue;
299 + // after loop
300 + auto Result =
301 + Case5AfterLoop(PhiOps, UniqueIncomingAccess, SingleAccess, BB);
302 + SF.pop();
303 + SF.top().rtn = Result;
304 + SF5.pop();
305 + continue;
306 + }
307 +
308 + llvm_unreachable("Should have hit one of the three cases above");
309 }
310 - llvm_unreachable("Should have hit one of the three cases above");
311 + assert(0 == SF5.size());
312 + return SF.top().rtn;
313 }
314
315 // This starts at the memory access, and goes backwards in the block to find the
316 @@ -138,7 +278,7 @@ MemoryAccess *MemorySSAUpdater::getPreviousDef(MemoryAccess *MA) {
317 if (auto *LocalResult = getPreviousDefInBlock(MA))
318 return LocalResult;
319 DenseMap<BasicBlock *, TrackingVH<MemoryAccess>> CachedPreviousDef;
320 - return getPreviousDefRecursive(MA->getBlock(), CachedPreviousDef);
321 + return getPreviousDefIterative(MA->getBlock(), CachedPreviousDef);
322 }
323
324 // This starts at the memory access, and goes backwards in the block to the find
325 @@ -168,19 +308,6 @@ MemoryAccess *MemorySSAUpdater::getPreviousDefInBlock(MemoryAccess *MA) {
326 return nullptr;
327 }
328
329 -// This starts at the end of block
330 -MemoryAccess *MemorySSAUpdater::getPreviousDefFromEnd(
331 - BasicBlock *BB,
332 - DenseMap<BasicBlock *, TrackingVH<MemoryAccess>> &CachedPreviousDef) {
333 - auto *Defs = MSSA->getWritableBlockDefs(BB);
334 -
335 - if (Defs) {
336 - CachedPreviousDef.insert({BB, &*Defs->rbegin()});
337 - return &*Defs->rbegin();
338 - }
339 -
340 - return getPreviousDefRecursive(BB, CachedPreviousDef);
341 -}
342 // Recurse over a set of phi uses to eliminate the trivial ones
343 MemoryAccess *MemorySSAUpdater::recursePhi(MemoryAccess *Phi) {
344 if (!Phi)
345 @@ -396,7 +523,17 @@ void MemorySSAUpdater::insertDef(MemoryDef *MD, bool RenameUses) {
346 auto *BBIDF = MPhi->getBlock();
347 for (auto *Pred : predecessors(BBIDF)) {
348 DenseMap<BasicBlock *, TrackingVH<MemoryAccess>> CachedPreviousDef;
349 - MPhi->addIncoming(getPreviousDefFromEnd(Pred, CachedPreviousDef), Pred);
350 + // inline getPreviousDefFromEnd start
351 + MemoryAccess *prevDefFromEnd = nullptr;
352 + auto *Defs = MSSA->getWritableBlockDefs(Pred);
353 + if (Defs) {
354 + CachedPreviousDef.insert({Pred, &*Defs->rbegin()});
355 + prevDefFromEnd = & * Defs->rbegin();
356 + } else {
357 + prevDefFromEnd = getPreviousDefIterative(Pred, CachedPreviousDef);
358 + }
359 + // inline getPreviousDefFromEnd end
360 + MPhi->addIncoming(prevDefFromEnd, Pred);
361 }
362 }
363
364
365 --
366 2.43.0
367