LLVM 22.0.0git
LoopSimplifyCFG.cpp
Go to the documentation of this file.
1//===--------- LoopSimplifyCFG.cpp - Loop CFG Simplification Pass ---------===//
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 Loop SimplifyCFG Pass. This pass is responsible for
10// basic loop CFG cleanup, primarily to assist other loop passes. If you
11// encounter a noncanonical CFG construct that causes another loop pass to
12// perform suboptimally, this is the place to fix it up.
13//
14//===----------------------------------------------------------------------===//
15
18#include "llvm/ADT/Statistic.h"
25#include "llvm/IR/Dominators.h"
26#include "llvm/IR/IRBuilder.h"
32#include <optional>
33using namespace llvm;
34
35#define DEBUG_TYPE "loop-simplifycfg"
36
37static cl::opt<bool> EnableTermFolding("enable-loop-simplifycfg-term-folding",
38 cl::init(true));
39
40STATISTIC(NumTerminatorsFolded,
41 "Number of terminators folded to unconditional branches");
42STATISTIC(NumLoopBlocksDeleted,
43 "Number of loop blocks deleted");
44STATISTIC(NumLoopExitsDeleted,
45 "Number of loop exiting edges deleted");
46
47/// If \p BB is a switch or a conditional branch, but only one of its successors
48/// can be reached from this block in runtime, return this successor. Otherwise,
49/// return nullptr.
51 Instruction *TI = BB->getTerminator();
52 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
53 if (BI->isUnconditional())
54 return nullptr;
55 if (BI->getSuccessor(0) == BI->getSuccessor(1))
56 return BI->getSuccessor(0);
57 ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
58 if (!Cond)
59 return nullptr;
60 return Cond->isZero() ? BI->getSuccessor(1) : BI->getSuccessor(0);
61 }
62
64 auto *CI = dyn_cast<ConstantInt>(SI->getCondition());
65 if (!CI)
66 return nullptr;
67 for (auto Case : SI->cases())
68 if (Case.getCaseValue() == CI)
69 return Case.getCaseSuccessor();
70 return SI->getDefaultDest();
71 }
72
73 return nullptr;
74}
75
76/// Removes \p BB from all loops from [FirstLoop, LastLoop) in parent chain.
77static void removeBlockFromLoops(BasicBlock *BB, Loop *FirstLoop,
78 Loop *LastLoop = nullptr) {
79 assert((!LastLoop || LastLoop->contains(FirstLoop->getHeader())) &&
80 "First loop is supposed to be inside of last loop!");
81 assert(FirstLoop->contains(BB) && "Must be a loop block!");
82 for (Loop *Current = FirstLoop; Current != LastLoop;
83 Current = Current->getParentLoop())
84 Current->removeBlockFromLoop(BB);
85}
86
87/// Find innermost loop that contains at least one block from \p BBs and
88/// contains the header of loop \p L.
90 Loop &L, LoopInfo &LI) {
91 Loop *Innermost = nullptr;
92 for (BasicBlock *BB : BBs) {
93 Loop *BBL = LI.getLoopFor(BB);
94 while (BBL && !BBL->contains(L.getHeader()))
95 BBL = BBL->getParentLoop();
96 if (BBL == &L)
97 BBL = BBL->getParentLoop();
98 if (!BBL)
99 continue;
100 if (!Innermost || BBL->getLoopDepth() > Innermost->getLoopDepth())
101 Innermost = BBL;
102 }
103 return Innermost;
104}
105
106namespace {
107/// Helper class that can turn branches and switches with constant conditions
108/// into unconditional branches.
109class ConstantTerminatorFoldingImpl {
110private:
111 Loop &L;
112 LoopInfo &LI;
113 DominatorTree &DT;
114 ScalarEvolution &SE;
115 MemorySSAUpdater *MSSAU;
116 LoopBlocksDFS DFS;
117 DomTreeUpdater DTU;
119
120 // Whether or not the current loop has irreducible CFG.
121 bool HasIrreducibleCFG = false;
122 // Whether or not the current loop will still exist after terminator constant
123 // folding will be done. In theory, there are two ways how it can happen:
124 // 1. Loop's latch(es) become unreachable from loop header;
125 // 2. Loop's header becomes unreachable from method entry.
126 // In practice, the second situation is impossible because we only modify the
127 // current loop and its preheader and do not affect preheader's reachibility
128 // from any other block. So this variable set to true means that loop's latch
129 // has become unreachable from loop header.
130 bool DeleteCurrentLoop = false;
131 // Whether or not we enter the loop through an indirectbr.
132 bool HasIndirectEntry = false;
133
134 // The blocks of the original loop that will still be reachable from entry
135 // after the constant folding.
136 SmallPtrSet<BasicBlock *, 8> LiveLoopBlocks;
137 // The blocks of the original loop that will become unreachable from entry
138 // after the constant folding.
139 SmallVector<BasicBlock *, 8> DeadLoopBlocks;
140 // The exits of the original loop that will still be reachable from entry
141 // after the constant folding.
142 SmallPtrSet<BasicBlock *, 8> LiveExitBlocks;
143 // The exits of the original loop that will become unreachable from entry
144 // after the constant folding.
145 SmallVector<BasicBlock *, 8> DeadExitBlocks;
146 // The blocks that will still be a part of the current loop after folding.
147 SmallPtrSet<BasicBlock *, 8> BlocksInLoopAfterFolding;
148 // The blocks that have terminators with constant condition that can be
149 // folded. Note: fold candidates should be in L but not in any of its
150 // subloops to avoid complex LI updates.
151 SmallVector<BasicBlock *, 8> FoldCandidates;
152
153 void dump() const {
154 dbgs() << "Constant terminator folding for loop " << L << "\n";
155 dbgs() << "After terminator constant-folding, the loop will";
156 if (!DeleteCurrentLoop)
157 dbgs() << " not";
158 dbgs() << " be destroyed\n";
159 auto PrintOutVector = [&](const char *Message,
160 const SmallVectorImpl<BasicBlock *> &S) {
161 dbgs() << Message << "\n";
162 for (const BasicBlock *BB : S)
163 dbgs() << "\t" << BB->getName() << "\n";
164 };
165 auto PrintOutSet = [&](const char *Message,
166 const SmallPtrSetImpl<BasicBlock *> &S) {
167 dbgs() << Message << "\n";
168 for (const BasicBlock *BB : S)
169 dbgs() << "\t" << BB->getName() << "\n";
170 };
171 PrintOutVector("Blocks in which we can constant-fold terminator:",
172 FoldCandidates);
173 PrintOutSet("Live blocks from the original loop:", LiveLoopBlocks);
174 PrintOutVector("Dead blocks from the original loop:", DeadLoopBlocks);
175 PrintOutSet("Live exit blocks:", LiveExitBlocks);
176 PrintOutVector("Dead exit blocks:", DeadExitBlocks);
177 if (!DeleteCurrentLoop)
178 PrintOutSet("The following blocks will still be part of the loop:",
179 BlocksInLoopAfterFolding);
180 }
181
182 /// Whether or not the current loop has irreducible CFG.
183 bool hasIrreducibleCFG(LoopBlocksDFS &DFS) {
184 assert(DFS.isComplete() && "DFS is expected to be finished");
185 // Index of a basic block in RPO traversal.
186 DenseMap<const BasicBlock *, unsigned> RPO;
187 unsigned Current = 0;
188 for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I)
189 RPO[*I] = Current++;
190
191 for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) {
192 BasicBlock *BB = *I;
193 for (auto *Succ : successors(BB))
194 if (L.contains(Succ) && !LI.isLoopHeader(Succ) && RPO[BB] > RPO[Succ])
195 // If an edge goes from a block with greater order number into a block
196 // with lesses number, and it is not a loop backedge, then it can only
197 // be a part of irreducible non-loop cycle.
198 return true;
199 }
200 return false;
201 }
202
203 /// Fill all information about status of blocks and exits of the current loop
204 /// if constant folding of all branches will be done.
205 void analyze() {
206 DFS.perform(&LI);
207 assert(DFS.isComplete() && "DFS is expected to be finished");
208
209 // TODO: The algorithm below relies on both RPO and Postorder traversals.
210 // When the loop has only reducible CFG inside, then the invariant "all
211 // predecessors of X are processed before X in RPO" is preserved. However
212 // an irreducible loop can break this invariant (e.g. latch does not have to
213 // be the last block in the traversal in this case, and the algorithm relies
214 // on this). We can later decide to support such cases by altering the
215 // algorithms, but so far we just give up analyzing them.
216 if (hasIrreducibleCFG(DFS)) {
217 HasIrreducibleCFG = true;
218 return;
219 }
220
221 // We need a loop preheader to split in handleDeadExits(). If LoopSimplify
222 // wasn't able to form one because the loop can be entered through an
223 // indirectbr we cannot continue.
224 if (!L.getLoopPreheader()) {
225 assert(any_of(predecessors(L.getHeader()),
226 [&](BasicBlock *Pred) {
227 return isa<IndirectBrInst>(Pred->getTerminator());
228 }) &&
229 "Loop should have preheader if it is not entered indirectly");
230 HasIndirectEntry = true;
231 return;
232 }
233
234 // Collect live and dead loop blocks and exits.
235 LiveLoopBlocks.insert(L.getHeader());
236 for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) {
237 BasicBlock *BB = *I;
238
239 // If a loop block wasn't marked as live so far, then it's dead.
240 if (!LiveLoopBlocks.count(BB)) {
241 DeadLoopBlocks.push_back(BB);
242 continue;
243 }
244
245 BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(BB);
246
247 // If a block has only one live successor, it's a candidate on constant
248 // folding. Only handle blocks from current loop: branches in child loops
249 // are skipped because if they can be folded, they should be folded during
250 // the processing of child loops.
251 bool TakeFoldCandidate = TheOnlySucc && LI.getLoopFor(BB) == &L;
252 if (TakeFoldCandidate)
253 FoldCandidates.push_back(BB);
254
255 // Handle successors.
256 for (BasicBlock *Succ : successors(BB))
257 if (!TakeFoldCandidate || TheOnlySucc == Succ) {
258 if (L.contains(Succ))
259 LiveLoopBlocks.insert(Succ);
260 else
261 LiveExitBlocks.insert(Succ);
262 }
263 }
264
265 // Amount of dead and live loop blocks should match the total number of
266 // blocks in loop.
267 assert(L.getNumBlocks() == LiveLoopBlocks.size() + DeadLoopBlocks.size() &&
268 "Malformed block sets?");
269
270 // Now, all exit blocks that are not marked as live are dead, if all their
271 // predecessors are in the loop. This may not be the case, as the input loop
272 // may not by in loop-simplify/canonical form.
273 SmallVector<BasicBlock *, 8> ExitBlocks;
274 L.getExitBlocks(ExitBlocks);
275 SmallPtrSet<BasicBlock *, 8> UniqueDeadExits;
276 for (auto *ExitBlock : ExitBlocks)
277 if (!LiveExitBlocks.count(ExitBlock) &&
278 UniqueDeadExits.insert(ExitBlock).second &&
279 all_of(predecessors(ExitBlock),
280 [this](BasicBlock *Pred) { return L.contains(Pred); }))
281 DeadExitBlocks.push_back(ExitBlock);
282
283 // Whether or not the edge From->To will still be present in graph after the
284 // folding.
285 auto IsEdgeLive = [&](BasicBlock *From, BasicBlock *To) {
286 if (!LiveLoopBlocks.count(From))
287 return false;
288 BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(From);
289 return !TheOnlySucc || TheOnlySucc == To || LI.getLoopFor(From) != &L;
290 };
291
292 // The loop will not be destroyed if its latch is live.
293 DeleteCurrentLoop = !IsEdgeLive(L.getLoopLatch(), L.getHeader());
294
295 // If we are going to delete the current loop completely, no extra analysis
296 // is needed.
297 if (DeleteCurrentLoop)
298 return;
299
300 // Otherwise, we should check which blocks will still be a part of the
301 // current loop after the transform.
302 BlocksInLoopAfterFolding.insert(L.getLoopLatch());
303 // If the loop is live, then we should compute what blocks are still in
304 // loop after all branch folding has been done. A block is in loop if
305 // it has a live edge to another block that is in the loop; by definition,
306 // latch is in the loop.
307 auto BlockIsInLoop = [&](BasicBlock *BB) {
308 return any_of(successors(BB), [&](BasicBlock *Succ) {
309 return BlocksInLoopAfterFolding.count(Succ) && IsEdgeLive(BB, Succ);
310 });
311 };
312 for (auto I = DFS.beginPostorder(), E = DFS.endPostorder(); I != E; ++I) {
313 BasicBlock *BB = *I;
314 if (BlockIsInLoop(BB))
315 BlocksInLoopAfterFolding.insert(BB);
316 }
317
318 assert(BlocksInLoopAfterFolding.count(L.getHeader()) &&
319 "Header not in loop?");
320 assert(BlocksInLoopAfterFolding.size() <= LiveLoopBlocks.size() &&
321 "All blocks that stay in loop should be live!");
322 }
323
324 /// We need to preserve static reachibility of all loop exit blocks (this is)
325 /// required by loop pass manager. In order to do it, we make the following
326 /// trick:
327 ///
328 /// preheader:
329 /// <preheader code>
330 /// br label %loop_header
331 ///
332 /// loop_header:
333 /// ...
334 /// br i1 false, label %dead_exit, label %loop_block
335 /// ...
336 ///
337 /// We cannot simply remove edge from the loop to dead exit because in this
338 /// case dead_exit (and its successors) may become unreachable. To avoid that,
339 /// we insert the following fictive preheader:
340 ///
341 /// preheader:
342 /// <preheader code>
343 /// switch i32 0, label %preheader-split,
344 /// [i32 1, label %dead_exit_1],
345 /// [i32 2, label %dead_exit_2],
346 /// ...
347 /// [i32 N, label %dead_exit_N],
348 ///
349 /// preheader-split:
350 /// br label %loop_header
351 ///
352 /// loop_header:
353 /// ...
354 /// br i1 false, label %dead_exit_N, label %loop_block
355 /// ...
356 ///
357 /// Doing so, we preserve static reachibility of all dead exits and can later
358 /// remove edges from the loop to these blocks.
359 void handleDeadExits() {
360 // If no dead exits, nothing to do.
361 if (DeadExitBlocks.empty())
362 return;
363
364 // Construct split preheader and the dummy switch to thread edges from it to
365 // dead exits.
366 BasicBlock *Preheader = L.getLoopPreheader();
367 BasicBlock *NewPreheader = llvm::SplitBlock(
368 Preheader, Preheader->getTerminator(), &DT, &LI, MSSAU);
369
370 IRBuilder<> Builder(Preheader->getTerminator());
371 SwitchInst *DummySwitch =
372 Builder.CreateSwitch(Builder.getInt32(0), NewPreheader);
373 Preheader->getTerminator()->eraseFromParent();
374
375 unsigned DummyIdx = 1;
376 for (BasicBlock *BB : DeadExitBlocks) {
377 // Eliminate all Phis and LandingPads from dead exits.
378 // TODO: Consider removing all instructions in this dead block.
379 SmallVector<Instruction *, 4> DeadInstructions(
381
382 if (auto *LandingPad = dyn_cast<LandingPadInst>(BB->getFirstNonPHIIt()))
383 DeadInstructions.emplace_back(LandingPad);
384
385 for (Instruction *I : DeadInstructions) {
386 SE.forgetValue(I);
387 I->replaceAllUsesWith(PoisonValue::get(I->getType()));
388 I->eraseFromParent();
389 }
390
391 assert(DummyIdx != 0 && "Too many dead exits!");
392 DummySwitch->addCase(Builder.getInt32(DummyIdx++), BB);
393 DTUpdates.push_back({DominatorTree::Insert, Preheader, BB});
394 ++NumLoopExitsDeleted;
395 }
396
397 assert(L.getLoopPreheader() == NewPreheader && "Malformed CFG?");
398 if (Loop *OuterLoop = LI.getLoopFor(Preheader)) {
399 // When we break dead edges, the outer loop may become unreachable from
400 // the current loop. We need to fix loop info accordingly. For this, we
401 // find the most nested loop that still contains L and remove L from all
402 // loops that are inside of it.
403 Loop *StillReachable = getInnermostLoopFor(LiveExitBlocks, L, LI);
404
405 // Okay, our loop is no longer in the outer loop (and maybe not in some of
406 // its parents as well). Make the fixup.
407 if (StillReachable != OuterLoop) {
408 LI.changeLoopFor(NewPreheader, StillReachable);
409 removeBlockFromLoops(NewPreheader, OuterLoop, StillReachable);
410 for (auto *BB : L.blocks())
411 removeBlockFromLoops(BB, OuterLoop, StillReachable);
412 OuterLoop->removeChildLoop(&L);
413 if (StillReachable)
414 StillReachable->addChildLoop(&L);
415 else
416 LI.addTopLevelLoop(&L);
417
418 // Some values from loops in [OuterLoop, StillReachable) could be used
419 // in the current loop. Now it is not their child anymore, so such uses
420 // require LCSSA Phis.
421 Loop *FixLCSSALoop = OuterLoop;
422 while (FixLCSSALoop->getParentLoop() != StillReachable)
423 FixLCSSALoop = FixLCSSALoop->getParentLoop();
424 assert(FixLCSSALoop && "Should be a loop!");
425 // We need all DT updates to be done before forming LCSSA.
426 if (MSSAU)
427 MSSAU->applyUpdates(DTUpdates, DT, /*UpdateDT=*/true);
428 else
429 DTU.applyUpdates(DTUpdates);
430 DTUpdates.clear();
431 formLCSSARecursively(*FixLCSSALoop, DT, &LI, &SE);
432 SE.forgetBlockAndLoopDispositions();
433 }
434 }
435
436 if (MSSAU) {
437 // Clear all updates now. Facilitates deletes that follow.
438 MSSAU->applyUpdates(DTUpdates, DT, /*UpdateDT=*/true);
439 DTUpdates.clear();
440 if (VerifyMemorySSA)
441 MSSAU->getMemorySSA()->verifyMemorySSA();
442 }
443 }
444
445 /// Delete loop blocks that have become unreachable after folding. Make all
446 /// relevant updates to DT and LI.
447 void deleteDeadLoopBlocks() {
448 if (MSSAU) {
449 SmallSetVector<BasicBlock *, 8> DeadLoopBlocksSet(DeadLoopBlocks.begin(),
450 DeadLoopBlocks.end());
451 MSSAU->removeBlocks(DeadLoopBlocksSet);
452 }
453
454 // The function LI.erase has some invariants that need to be preserved when
455 // it tries to remove a loop which is not the top-level loop. In particular,
456 // it requires loop's preheader to be strictly in loop's parent. We cannot
457 // just remove blocks one by one, because after removal of preheader we may
458 // break this invariant for the dead loop. So we detatch and erase all dead
459 // loops beforehand.
460 for (auto *BB : DeadLoopBlocks)
461 if (LI.isLoopHeader(BB)) {
462 assert(LI.getLoopFor(BB) != &L && "Attempt to remove current loop!");
463 Loop *DL = LI.getLoopFor(BB);
464 if (!DL->isOutermost()) {
465 for (auto *PL = DL->getParentLoop(); PL; PL = PL->getParentLoop())
466 for (auto *BB : DL->getBlocks())
467 PL->removeBlockFromLoop(BB);
468 DL->getParentLoop()->removeChildLoop(DL);
469 LI.addTopLevelLoop(DL);
470 }
471 LI.erase(DL);
472 }
473
474 for (auto *BB : DeadLoopBlocks) {
475 assert(BB != L.getHeader() &&
476 "Header of the current loop cannot be dead!");
477 LLVM_DEBUG(dbgs() << "Deleting dead loop block " << BB->getName()
478 << "\n");
479 LI.removeBlock(BB);
480 }
481
482 detachDeadBlocks(DeadLoopBlocks, &DTUpdates, /*KeepOneInputPHIs*/true);
483 DTU.applyUpdates(DTUpdates);
484 DTUpdates.clear();
485 for (auto *BB : DeadLoopBlocks)
486 DTU.deleteBB(BB);
487
488 NumLoopBlocksDeleted += DeadLoopBlocks.size();
489 }
490
491 /// Constant-fold terminators of blocks accumulated in FoldCandidates into the
492 /// unconditional branches.
493 void foldTerminators() {
494 for (BasicBlock *BB : FoldCandidates) {
495 assert(LI.getLoopFor(BB) == &L && "Should be a loop block!");
496 BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(BB);
497 assert(TheOnlySucc && "Should have one live successor!");
498
499 LLVM_DEBUG(dbgs() << "Replacing terminator of " << BB->getName()
500 << " with an unconditional branch to the block "
501 << TheOnlySucc->getName() << "\n");
502
503 SmallPtrSet<BasicBlock *, 2> DeadSuccessors;
504 // Remove all BB's successors except for the live one.
505 unsigned TheOnlySuccDuplicates = 0;
506 for (auto *Succ : successors(BB))
507 if (Succ != TheOnlySucc) {
508 DeadSuccessors.insert(Succ);
509 // If our successor lies in a different loop, we don't want to remove
510 // the one-input Phi because it is a LCSSA Phi.
511 bool PreserveLCSSAPhi = !L.contains(Succ);
512 Succ->removePredecessor(BB, PreserveLCSSAPhi);
513 if (MSSAU)
514 MSSAU->removeEdge(BB, Succ);
515 } else
516 ++TheOnlySuccDuplicates;
517
518 assert(TheOnlySuccDuplicates > 0 && "Should be!");
519 // If TheOnlySucc was BB's successor more than once, after transform it
520 // will be its successor only once. Remove redundant inputs from
521 // TheOnlySucc's Phis.
522 bool PreserveLCSSAPhi = !L.contains(TheOnlySucc);
523 for (unsigned Dup = 1; Dup < TheOnlySuccDuplicates; ++Dup)
524 TheOnlySucc->removePredecessor(BB, PreserveLCSSAPhi);
525 if (MSSAU && TheOnlySuccDuplicates > 1)
526 MSSAU->removeDuplicatePhiEdgesBetween(BB, TheOnlySucc);
527
528 IRBuilder<> Builder(BB->getContext());
530 Builder.SetInsertPoint(Term);
531 Builder.CreateBr(TheOnlySucc);
532 Term->eraseFromParent();
533
534 for (auto *DeadSucc : DeadSuccessors)
535 DTUpdates.push_back({DominatorTree::Delete, BB, DeadSucc});
536
537 ++NumTerminatorsFolded;
538 }
539 }
540
541public:
542 ConstantTerminatorFoldingImpl(Loop &L, LoopInfo &LI, DominatorTree &DT,
543 ScalarEvolution &SE,
544 MemorySSAUpdater *MSSAU)
545 : L(L), LI(LI), DT(DT), SE(SE), MSSAU(MSSAU), DFS(&L),
546 DTU(DT, DomTreeUpdater::UpdateStrategy::Eager) {}
547 bool run() {
548 assert(L.getLoopLatch() && "Should be single latch!");
549
550 // Collect all available information about status of blocks after constant
551 // folding.
552 analyze();
553 BasicBlock *Header = L.getHeader();
554 (void)Header;
555
556 LLVM_DEBUG(dbgs() << "In function " << Header->getParent()->getName()
557 << ": ");
558
559 if (HasIrreducibleCFG) {
560 LLVM_DEBUG(dbgs() << "Loops with irreducible CFG are not supported!\n");
561 return false;
562 }
563
564 if (HasIndirectEntry) {
565 LLVM_DEBUG(dbgs() << "Loops which can be entered indirectly are not"
566 " supported!\n");
567 return false;
568 }
569
570 // Nothing to constant-fold.
571 if (FoldCandidates.empty()) {
573 dbgs() << "No constant terminator folding candidates found in loop "
574 << Header->getName() << "\n");
575 return false;
576 }
577
578 // TODO: Support deletion of the current loop.
579 if (DeleteCurrentLoop) {
581 dbgs()
582 << "Give up constant terminator folding in loop " << Header->getName()
583 << ": we don't currently support deletion of the current loop.\n");
584 return false;
585 }
586
587 // TODO: Support blocks that are not dead, but also not in loop after the
588 // folding.
589 if (BlocksInLoopAfterFolding.size() + DeadLoopBlocks.size() !=
590 L.getNumBlocks()) {
592 dbgs() << "Give up constant terminator folding in loop "
593 << Header->getName() << ": we don't currently"
594 " support blocks that are not dead, but will stop "
595 "being a part of the loop after constant-folding.\n");
596 return false;
597 }
598
599 // TODO: Tokens may breach LCSSA form by default. However, the transform for
600 // dead exit blocks requires LCSSA form to be maintained for all values,
601 // tokens included, otherwise it may break use-def dominance (see PR56243).
602 if (!DeadExitBlocks.empty() && !L.isLCSSAForm(DT, /*IgnoreTokens*/ false)) {
603 assert(L.isLCSSAForm(DT, /*IgnoreTokens*/ true) &&
604 "LCSSA broken not by tokens?");
605 LLVM_DEBUG(dbgs() << "Give up constant terminator folding in loop "
606 << Header->getName()
607 << ": tokens uses potentially break LCSSA form.\n");
608 return false;
609 }
610
611 SE.forgetTopmostLoop(&L);
612 // Dump analysis results.
613 LLVM_DEBUG(dump());
614
615 LLVM_DEBUG(dbgs() << "Constant-folding " << FoldCandidates.size()
616 << " terminators in loop " << Header->getName() << "\n");
617
618 if (!DeadLoopBlocks.empty())
619 SE.forgetBlockAndLoopDispositions();
620
621 // Make the actual transforms.
622 handleDeadExits();
623 foldTerminators();
624
625 if (!DeadLoopBlocks.empty()) {
626 LLVM_DEBUG(dbgs() << "Deleting " << DeadLoopBlocks.size()
627 << " dead blocks in loop " << Header->getName() << "\n");
628 deleteDeadLoopBlocks();
629 } else {
630 // If we didn't do updates inside deleteDeadLoopBlocks, do them here.
631 DTU.applyUpdates(DTUpdates);
632 DTUpdates.clear();
633 }
634
635 if (MSSAU && VerifyMemorySSA)
636 MSSAU->getMemorySSA()->verifyMemorySSA();
637
638#ifndef NDEBUG
639 // Make sure that we have preserved all data structures after the transform.
640#if defined(EXPENSIVE_CHECKS)
641 assert(DT.verify(DominatorTree::VerificationLevel::Full) &&
642 "DT broken after transform!");
643#else
644 assert(DT.verify(DominatorTree::VerificationLevel::Fast) &&
645 "DT broken after transform!");
646#endif
647 assert(DT.isReachableFromEntry(Header));
648 LI.verify(DT);
649#endif
650
651 return true;
652 }
653
654 bool foldingBreaksCurrentLoop() const {
655 return DeleteCurrentLoop;
656 }
657};
658} // namespace
659
660/// Turn branches and switches with known constant conditions into unconditional
661/// branches.
663 ScalarEvolution &SE,
664 MemorySSAUpdater *MSSAU,
665 bool &IsLoopDeleted) {
667 return false;
668
669 // To keep things simple, only process loops with single latch. We
670 // canonicalize most loops to this form. We can support multi-latch if needed.
671 if (!L.getLoopLatch())
672 return false;
673
674 ConstantTerminatorFoldingImpl BranchFolder(L, LI, DT, SE, MSSAU);
675 bool Changed = BranchFolder.run();
676 IsLoopDeleted = Changed && BranchFolder.foldingBreaksCurrentLoop();
677 return Changed;
678}
679
681 LoopInfo &LI, MemorySSAUpdater *MSSAU,
682 ScalarEvolution &SE) {
683 bool Changed = false;
684 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
685 // Copy blocks into a temporary array to avoid iterator invalidation issues
686 // as we remove them.
687 SmallVector<WeakTrackingVH, 16> Blocks(L.blocks());
688
689 for (auto &Block : Blocks) {
690 // Attempt to merge blocks in the trivial case. Don't modify blocks which
691 // belong to other loops.
693 if (!Succ)
694 continue;
695
696 BasicBlock *Pred = Succ->getSinglePredecessor();
697 if (!Pred || !Pred->getSingleSuccessor() || LI.getLoopFor(Pred) != &L)
698 continue;
699
700 // Merge Succ into Pred and delete it.
701 MergeBlockIntoPredecessor(Succ, &DTU, &LI, MSSAU);
702
703 if (MSSAU && VerifyMemorySSA)
704 MSSAU->getMemorySSA()->verifyMemorySSA();
705
706 Changed = true;
707 }
708
709 if (Changed)
711
712 return Changed;
713}
714
717 bool &IsLoopDeleted) {
718 bool Changed = false;
719
720 // Constant-fold terminators with known constant conditions.
721 Changed |= constantFoldTerminators(L, DT, LI, SE, MSSAU, IsLoopDeleted);
722
723 if (IsLoopDeleted)
724 return true;
725
726 // Eliminate unconditional branches by merging blocks into their predecessors.
727 Changed |= mergeBlocksIntoPredecessors(L, DT, LI, MSSAU, SE);
728
729 if (Changed)
730 SE.forgetTopmostLoop(&L);
731
732 return Changed;
733}
734
737 LPMUpdater &LPMU) {
738 std::optional<MemorySSAUpdater> MSSAU;
739 if (AR.MSSA)
740 MSSAU = MemorySSAUpdater(AR.MSSA);
741 bool DeleteCurrentLoop = false;
742 if (!simplifyLoopCFG(L, AR.DT, AR.LI, AR.SE, MSSAU ? &*MSSAU : nullptr,
743 DeleteCurrentLoop))
744 return PreservedAnalyses::all();
745
746 if (DeleteCurrentLoop)
747 LPMU.markLoopAsDeleted(L, "loop-simplifycfg");
748
750 if (AR.MSSA)
751 PA.preserve<MemorySSAAnalysis>();
752 return PA;
753}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This header provides classes for managing a pipeline of passes over loops in LLVM IR.
static BasicBlock * getOnlyLiveSuccessor(BasicBlock *BB)
If BB is a switch or a conditional branch, but only one of its successors can be reached from this bl...
static bool constantFoldTerminators(Loop &L, DominatorTree &DT, LoopInfo &LI, ScalarEvolution &SE, MemorySSAUpdater *MSSAU, bool &IsLoopDeleted)
Turn branches and switches with known constant conditions into unconditional branches.
static Loop * getInnermostLoopFor(SmallPtrSetImpl< BasicBlock * > &BBs, Loop &L, LoopInfo &LI)
Find innermost loop that contains at least one block from BBs and contains the header of loop L.
static bool mergeBlocksIntoPredecessors(Loop &L, DominatorTree &DT, LoopInfo &LI, MemorySSAUpdater *MSSAU, ScalarEvolution &SE)
static bool simplifyLoopCFG(Loop &L, DominatorTree &DT, LoopInfo &LI, ScalarEvolution &SE, MemorySSAUpdater *MSSAU, bool &IsLoopDeleted)
static cl::opt< bool > EnableTermFolding("enable-loop-simplifycfg-term-folding", cl::init(true))
static void removeBlockFromLoops(BasicBlock *BB, Loop *FirstLoop, Loop *LastLoop=nullptr)
Removes BB from all loops from [FirstLoop, LastLoop) in parent chain.
#define I(x, y, z)
Definition MD5.cpp:58
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
const SmallVectorImpl< MachineOperand > & Cond
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:167
#define LLVM_DEBUG(...)
Definition Debug.h:119
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition BasicBlock.h:528
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
LLVM_ABI void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs=false)
Update PHI nodes in this BasicBlock before removal of predecessor Pred.
Conditional or Unconditional Branch instruction.
This is the shared class of boolean and integer constants.
Definition Constants.h:87
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:165
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
void markLoopAsDeleted(Loop &L, llvm::StringRef Name)
Loop passes should use this method to indicate they have deleted a loop from the nest.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
BlockT * getHeader() const
unsigned getLoopDepth() const
Return the nesting level of this loop.
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
LoopT * getParentLoop() const
Return the parent loop if it exists or nullptr for top level loops.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
An analysis that produces MemorySSA for a function.
Definition MemorySSA.h:936
MemorySSA * getMemorySSA() const
Get handle on MemorySSA.
LLVM_ABI void verifyMemorySSA(VerificationLevel=VerificationLevel::Fast) const
Verify that MemorySSA is self consistent (IE definitions dominate all uses, uses appear in the right ...
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
The main scalar evolution driver.
LLVM_ABI void forgetTopmostLoop(const Loop *L)
LLVM_ABI void forgetBlockAndLoopDispositions(Value *V=nullptr)
Called when the client has changed the disposition of values in a loop or block.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Multiway switch.
LLVM_ABI void addCase(ConstantInt *OnVal, BasicBlock *Dest)
Add an entry to the switch instruction.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
Changed
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
initializer< Ty > init(const Ty &Val)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1727
LLVM_ABI void detachDeadBlocks(ArrayRef< BasicBlock * > BBs, SmallVectorImpl< DominatorTree::UpdateType > *Updates, bool KeepOneInputPHIs=false)
Replace contents of every block in BBs with single unreachable instruction.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
auto successors(const MachineBasicBlock *BB)
LLVM_ABI bool formLCSSARecursively(Loop &L, const DominatorTree &DT, const LoopInfo *LI, ScalarEvolution *SE)
Put a loop nest into LCSSA form.
Definition LCSSA.cpp:449
auto cast_or_null(const Y &Val)
Definition Casting.h:720
AnalysisManager< Loop, LoopStandardAnalysisResults & > LoopAnalysisManager
The loop analysis manager.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1734
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
LLVM_ABI bool VerifyMemorySSA
Enables verification of MemorySSA.
Definition MemorySSA.cpp:84
LLVM_ABI bool MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, MemoryDependenceResults *MemDep=nullptr, bool PredecessorWithTwoSuccessors=false, DominatorTree *DT=nullptr)
Attempts to merge a block into its predecessor, if possible.
LLVM_ABI BasicBlock * SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="", bool Before=false)
Split the specified block at the specified instruction.
LLVM_ABI PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
auto predecessors(const MachineBasicBlock *BB)
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
Definition iterator.h:363
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...