| 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 | |
| 16 | #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h" |
| 17 | #include "llvm/ADT/SmallVector.h" |
| 18 | #include "llvm/ADT/Statistic.h" |
| 19 | #include "llvm/Analysis/DomTreeUpdater.h" |
| 20 | #include "llvm/Analysis/LoopInfo.h" |
| 21 | #include "llvm/Analysis/LoopIterator.h" |
| 22 | #include "llvm/Analysis/MemorySSA.h" |
| 23 | #include "llvm/Analysis/MemorySSAUpdater.h" |
| 24 | #include "llvm/Analysis/ScalarEvolution.h" |
| 25 | #include "llvm/IR/Dominators.h" |
| 26 | #include "llvm/IR/IRBuilder.h" |
| 27 | #include "llvm/Support/CommandLine.h" |
| 28 | #include "llvm/Transforms/Scalar.h" |
| 29 | #include "llvm/Transforms/Scalar/LoopPassManager.h" |
| 30 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 31 | #include "llvm/Transforms/Utils/LoopUtils.h" |
| 32 | #include <optional> |
| 33 | using namespace llvm; |
| 34 | |
| 35 | #define DEBUG_TYPE "loop-simplifycfg" |
| 36 | |
| 37 | static cl::opt<bool> EnableTermFolding("enable-loop-simplifycfg-term-folding" , |
| 38 | cl::init(Val: true)); |
| 39 | |
| 40 | STATISTIC(NumTerminatorsFolded, |
| 41 | "Number of terminators folded to unconditional branches" ); |
| 42 | STATISTIC(NumLoopBlocksDeleted, |
| 43 | "Number of loop blocks deleted" ); |
| 44 | STATISTIC(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. |
| 50 | static BasicBlock *getOnlyLiveSuccessor(BasicBlock *BB) { |
| 51 | Instruction *TI = BB->getTerminator(); |
| 52 | if (BranchInst *BI = dyn_cast<BranchInst>(Val: TI)) { |
| 53 | if (BI->isUnconditional()) |
| 54 | return nullptr; |
| 55 | if (BI->getSuccessor(i: 0) == BI->getSuccessor(i: 1)) |
| 56 | return BI->getSuccessor(i: 0); |
| 57 | ConstantInt *Cond = dyn_cast<ConstantInt>(Val: BI->getCondition()); |
| 58 | if (!Cond) |
| 59 | return nullptr; |
| 60 | return Cond->isZero() ? BI->getSuccessor(i: 1) : BI->getSuccessor(i: 0); |
| 61 | } |
| 62 | |
| 63 | if (SwitchInst *SI = dyn_cast<SwitchInst>(Val: TI)) { |
| 64 | auto *CI = dyn_cast<ConstantInt>(Val: 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. |
| 77 | static 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. |
| 89 | static Loop *getInnermostLoopFor(SmallPtrSetImpl<BasicBlock *> &BBs, |
| 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(BB: 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 | |
| 106 | namespace { |
| 107 | /// Helper class that can turn branches and switches with constant conditions |
| 108 | /// into unconditional branches. |
| 109 | class ConstantTerminatorFoldingImpl { |
| 110 | private: |
| 111 | Loop &L; |
| 112 | LoopInfo &LI; |
| 113 | DominatorTree &DT; |
| 114 | ScalarEvolution &SE; |
| 115 | MemorySSAUpdater *MSSAU; |
| 116 | LoopBlocksDFS DFS; |
| 117 | DomTreeUpdater DTU; |
| 118 | SmallVector<DominatorTree::UpdateType, 16> DTUpdates; |
| 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 | |
| 132 | // The blocks of the original loop that will still be reachable from entry |
| 133 | // after the constant folding. |
| 134 | SmallPtrSet<BasicBlock *, 8> LiveLoopBlocks; |
| 135 | // The blocks of the original loop that will become unreachable from entry |
| 136 | // after the constant folding. |
| 137 | SmallVector<BasicBlock *, 8> DeadLoopBlocks; |
| 138 | // The exits of the original loop that will still be reachable from entry |
| 139 | // after the constant folding. |
| 140 | SmallPtrSet<BasicBlock *, 8> LiveExitBlocks; |
| 141 | // The exits of the original loop that will become unreachable from entry |
| 142 | // after the constant folding. |
| 143 | SmallVector<BasicBlock *, 8> DeadExitBlocks; |
| 144 | // The blocks that will still be a part of the current loop after folding. |
| 145 | SmallPtrSet<BasicBlock *, 8> BlocksInLoopAfterFolding; |
| 146 | // The blocks that have terminators with constant condition that can be |
| 147 | // folded. Note: fold candidates should be in L but not in any of its |
| 148 | // subloops to avoid complex LI updates. |
| 149 | SmallVector<BasicBlock *, 8> FoldCandidates; |
| 150 | |
| 151 | void dump() const { |
| 152 | dbgs() << "Constant terminator folding for loop " << L << "\n" ; |
| 153 | dbgs() << "After terminator constant-folding, the loop will" ; |
| 154 | if (!DeleteCurrentLoop) |
| 155 | dbgs() << " not" ; |
| 156 | dbgs() << " be destroyed\n" ; |
| 157 | auto PrintOutVector = [&](const char *Message, |
| 158 | const SmallVectorImpl<BasicBlock *> &S) { |
| 159 | dbgs() << Message << "\n" ; |
| 160 | for (const BasicBlock *BB : S) |
| 161 | dbgs() << "\t" << BB->getName() << "\n" ; |
| 162 | }; |
| 163 | auto PrintOutSet = [&](const char *Message, |
| 164 | const SmallPtrSetImpl<BasicBlock *> &S) { |
| 165 | dbgs() << Message << "\n" ; |
| 166 | for (const BasicBlock *BB : S) |
| 167 | dbgs() << "\t" << BB->getName() << "\n" ; |
| 168 | }; |
| 169 | PrintOutVector("Blocks in which we can constant-fold terminator:" , |
| 170 | FoldCandidates); |
| 171 | PrintOutSet("Live blocks from the original loop:" , LiveLoopBlocks); |
| 172 | PrintOutVector("Dead blocks from the original loop:" , DeadLoopBlocks); |
| 173 | PrintOutSet("Live exit blocks:" , LiveExitBlocks); |
| 174 | PrintOutVector("Dead exit blocks:" , DeadExitBlocks); |
| 175 | if (!DeleteCurrentLoop) |
| 176 | PrintOutSet("The following blocks will still be part of the loop:" , |
| 177 | BlocksInLoopAfterFolding); |
| 178 | } |
| 179 | |
| 180 | /// Whether or not the current loop has irreducible CFG. |
| 181 | bool hasIrreducibleCFG(LoopBlocksDFS &DFS) { |
| 182 | assert(DFS.isComplete() && "DFS is expected to be finished" ); |
| 183 | // Index of a basic block in RPO traversal. |
| 184 | DenseMap<const BasicBlock *, unsigned> RPO; |
| 185 | unsigned Current = 0; |
| 186 | for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) |
| 187 | RPO[*I] = Current++; |
| 188 | |
| 189 | for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) { |
| 190 | BasicBlock *BB = *I; |
| 191 | for (auto *Succ : successors(BB)) |
| 192 | if (L.contains(BB: Succ) && !LI.isLoopHeader(BB: Succ) && RPO[BB] > RPO[Succ]) |
| 193 | // If an edge goes from a block with greater order number into a block |
| 194 | // with lesses number, and it is not a loop backedge, then it can only |
| 195 | // be a part of irreducible non-loop cycle. |
| 196 | return true; |
| 197 | } |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | /// Fill all information about status of blocks and exits of the current loop |
| 202 | /// if constant folding of all branches will be done. |
| 203 | void analyze() { |
| 204 | DFS.perform(LI: &LI); |
| 205 | assert(DFS.isComplete() && "DFS is expected to be finished" ); |
| 206 | |
| 207 | // TODO: The algorithm below relies on both RPO and Postorder traversals. |
| 208 | // When the loop has only reducible CFG inside, then the invariant "all |
| 209 | // predecessors of X are processed before X in RPO" is preserved. However |
| 210 | // an irreducible loop can break this invariant (e.g. latch does not have to |
| 211 | // be the last block in the traversal in this case, and the algorithm relies |
| 212 | // on this). We can later decide to support such cases by altering the |
| 213 | // algorithms, but so far we just give up analyzing them. |
| 214 | if (hasIrreducibleCFG(DFS)) { |
| 215 | HasIrreducibleCFG = true; |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | // Collect live and dead loop blocks and exits. |
| 220 | LiveLoopBlocks.insert(Ptr: L.getHeader()); |
| 221 | for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) { |
| 222 | BasicBlock *BB = *I; |
| 223 | |
| 224 | // If a loop block wasn't marked as live so far, then it's dead. |
| 225 | if (!LiveLoopBlocks.count(Ptr: BB)) { |
| 226 | DeadLoopBlocks.push_back(Elt: BB); |
| 227 | continue; |
| 228 | } |
| 229 | |
| 230 | BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(BB); |
| 231 | |
| 232 | // If a block has only one live successor, it's a candidate on constant |
| 233 | // folding. Only handle blocks from current loop: branches in child loops |
| 234 | // are skipped because if they can be folded, they should be folded during |
| 235 | // the processing of child loops. |
| 236 | bool TakeFoldCandidate = TheOnlySucc && LI.getLoopFor(BB) == &L; |
| 237 | if (TakeFoldCandidate) |
| 238 | FoldCandidates.push_back(Elt: BB); |
| 239 | |
| 240 | // Handle successors. |
| 241 | for (BasicBlock *Succ : successors(BB)) |
| 242 | if (!TakeFoldCandidate || TheOnlySucc == Succ) { |
| 243 | if (L.contains(BB: Succ)) |
| 244 | LiveLoopBlocks.insert(Ptr: Succ); |
| 245 | else |
| 246 | LiveExitBlocks.insert(Ptr: Succ); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | // Amount of dead and live loop blocks should match the total number of |
| 251 | // blocks in loop. |
| 252 | assert(L.getNumBlocks() == LiveLoopBlocks.size() + DeadLoopBlocks.size() && |
| 253 | "Malformed block sets?" ); |
| 254 | |
| 255 | // Now, all exit blocks that are not marked as live are dead, if all their |
| 256 | // predecessors are in the loop. This may not be the case, as the input loop |
| 257 | // may not by in loop-simplify/canonical form. |
| 258 | SmallVector<BasicBlock *, 8> ExitBlocks; |
| 259 | L.getExitBlocks(ExitBlocks); |
| 260 | SmallPtrSet<BasicBlock *, 8> UniqueDeadExits; |
| 261 | for (auto *ExitBlock : ExitBlocks) |
| 262 | if (!LiveExitBlocks.count(Ptr: ExitBlock) && |
| 263 | UniqueDeadExits.insert(Ptr: ExitBlock).second && |
| 264 | all_of(Range: predecessors(BB: ExitBlock), |
| 265 | P: [this](BasicBlock *Pred) { return L.contains(BB: Pred); })) |
| 266 | DeadExitBlocks.push_back(Elt: ExitBlock); |
| 267 | |
| 268 | // Whether or not the edge From->To will still be present in graph after the |
| 269 | // folding. |
| 270 | auto IsEdgeLive = [&](BasicBlock *From, BasicBlock *To) { |
| 271 | if (!LiveLoopBlocks.count(Ptr: From)) |
| 272 | return false; |
| 273 | BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(BB: From); |
| 274 | return !TheOnlySucc || TheOnlySucc == To || LI.getLoopFor(BB: From) != &L; |
| 275 | }; |
| 276 | |
| 277 | // The loop will not be destroyed if its latch is live. |
| 278 | DeleteCurrentLoop = !IsEdgeLive(L.getLoopLatch(), L.getHeader()); |
| 279 | |
| 280 | // If we are going to delete the current loop completely, no extra analysis |
| 281 | // is needed. |
| 282 | if (DeleteCurrentLoop) |
| 283 | return; |
| 284 | |
| 285 | // Otherwise, we should check which blocks will still be a part of the |
| 286 | // current loop after the transform. |
| 287 | BlocksInLoopAfterFolding.insert(Ptr: L.getLoopLatch()); |
| 288 | // If the loop is live, then we should compute what blocks are still in |
| 289 | // loop after all branch folding has been done. A block is in loop if |
| 290 | // it has a live edge to another block that is in the loop; by definition, |
| 291 | // latch is in the loop. |
| 292 | auto BlockIsInLoop = [&](BasicBlock *BB) { |
| 293 | return any_of(Range: successors(BB), P: [&](BasicBlock *Succ) { |
| 294 | return BlocksInLoopAfterFolding.count(Ptr: Succ) && IsEdgeLive(BB, Succ); |
| 295 | }); |
| 296 | }; |
| 297 | for (auto I = DFS.beginPostorder(), E = DFS.endPostorder(); I != E; ++I) { |
| 298 | BasicBlock *BB = *I; |
| 299 | if (BlockIsInLoop(BB)) |
| 300 | BlocksInLoopAfterFolding.insert(Ptr: BB); |
| 301 | } |
| 302 | |
| 303 | assert(BlocksInLoopAfterFolding.count(L.getHeader()) && |
| 304 | "Header not in loop?" ); |
| 305 | assert(BlocksInLoopAfterFolding.size() <= LiveLoopBlocks.size() && |
| 306 | "All blocks that stay in loop should be live!" ); |
| 307 | } |
| 308 | |
| 309 | /// We need to preserve static reachibility of all loop exit blocks (this is) |
| 310 | /// required by loop pass manager. In order to do it, we make the following |
| 311 | /// trick: |
| 312 | /// |
| 313 | /// preheader: |
| 314 | /// <preheader code> |
| 315 | /// br label %loop_header |
| 316 | /// |
| 317 | /// loop_header: |
| 318 | /// ... |
| 319 | /// br i1 false, label %dead_exit, label %loop_block |
| 320 | /// ... |
| 321 | /// |
| 322 | /// We cannot simply remove edge from the loop to dead exit because in this |
| 323 | /// case dead_exit (and its successors) may become unreachable. To avoid that, |
| 324 | /// we insert the following fictive preheader: |
| 325 | /// |
| 326 | /// preheader: |
| 327 | /// <preheader code> |
| 328 | /// switch i32 0, label %preheader-split, |
| 329 | /// [i32 1, label %dead_exit_1], |
| 330 | /// [i32 2, label %dead_exit_2], |
| 331 | /// ... |
| 332 | /// [i32 N, label %dead_exit_N], |
| 333 | /// |
| 334 | /// preheader-split: |
| 335 | /// br label %loop_header |
| 336 | /// |
| 337 | /// loop_header: |
| 338 | /// ... |
| 339 | /// br i1 false, label %dead_exit_N, label %loop_block |
| 340 | /// ... |
| 341 | /// |
| 342 | /// Doing so, we preserve static reachibility of all dead exits and can later |
| 343 | /// remove edges from the loop to these blocks. |
| 344 | void handleDeadExits() { |
| 345 | // If no dead exits, nothing to do. |
| 346 | if (DeadExitBlocks.empty()) |
| 347 | return; |
| 348 | |
| 349 | // Construct split preheader and the dummy switch to thread edges from it to |
| 350 | // dead exits. |
| 351 | BasicBlock * = L.getLoopPreheader(); |
| 352 | BasicBlock * = llvm::SplitBlock( |
| 353 | Old: Preheader, SplitPt: Preheader->getTerminator(), DT: &DT, LI: &LI, MSSAU); |
| 354 | |
| 355 | IRBuilder<> Builder(Preheader->getTerminator()); |
| 356 | SwitchInst *DummySwitch = |
| 357 | Builder.CreateSwitch(V: Builder.getInt32(C: 0), Dest: NewPreheader); |
| 358 | Preheader->getTerminator()->eraseFromParent(); |
| 359 | |
| 360 | unsigned DummyIdx = 1; |
| 361 | for (BasicBlock *BB : DeadExitBlocks) { |
| 362 | // Eliminate all Phis and LandingPads from dead exits. |
| 363 | // TODO: Consider removing all instructions in this dead block. |
| 364 | SmallVector<Instruction *, 4> DeadInstructions( |
| 365 | llvm::make_pointer_range(Range: BB->phis())); |
| 366 | |
| 367 | if (auto *LandingPad = dyn_cast<LandingPadInst>(Val: BB->getFirstNonPHIIt())) |
| 368 | DeadInstructions.emplace_back(Args&: LandingPad); |
| 369 | |
| 370 | for (Instruction *I : DeadInstructions) { |
| 371 | SE.forgetValue(V: I); |
| 372 | I->replaceAllUsesWith(V: PoisonValue::get(T: I->getType())); |
| 373 | I->eraseFromParent(); |
| 374 | } |
| 375 | |
| 376 | assert(DummyIdx != 0 && "Too many dead exits!" ); |
| 377 | DummySwitch->addCase(OnVal: Builder.getInt32(C: DummyIdx++), Dest: BB); |
| 378 | DTUpdates.push_back(Elt: {DominatorTree::Insert, Preheader, BB}); |
| 379 | ++NumLoopExitsDeleted; |
| 380 | } |
| 381 | |
| 382 | assert(L.getLoopPreheader() == NewPreheader && "Malformed CFG?" ); |
| 383 | if (Loop *OuterLoop = LI.getLoopFor(BB: Preheader)) { |
| 384 | // When we break dead edges, the outer loop may become unreachable from |
| 385 | // the current loop. We need to fix loop info accordingly. For this, we |
| 386 | // find the most nested loop that still contains L and remove L from all |
| 387 | // loops that are inside of it. |
| 388 | Loop *StillReachable = getInnermostLoopFor(BBs&: LiveExitBlocks, L, LI); |
| 389 | |
| 390 | // Okay, our loop is no longer in the outer loop (and maybe not in some of |
| 391 | // its parents as well). Make the fixup. |
| 392 | if (StillReachable != OuterLoop) { |
| 393 | LI.changeLoopFor(BB: NewPreheader, L: StillReachable); |
| 394 | removeBlockFromLoops(BB: NewPreheader, FirstLoop: OuterLoop, LastLoop: StillReachable); |
| 395 | for (auto *BB : L.blocks()) |
| 396 | removeBlockFromLoops(BB, FirstLoop: OuterLoop, LastLoop: StillReachable); |
| 397 | OuterLoop->removeChildLoop(Child: &L); |
| 398 | if (StillReachable) |
| 399 | StillReachable->addChildLoop(NewChild: &L); |
| 400 | else |
| 401 | LI.addTopLevelLoop(New: &L); |
| 402 | |
| 403 | // Some values from loops in [OuterLoop, StillReachable) could be used |
| 404 | // in the current loop. Now it is not their child anymore, so such uses |
| 405 | // require LCSSA Phis. |
| 406 | Loop *FixLCSSALoop = OuterLoop; |
| 407 | while (FixLCSSALoop->getParentLoop() != StillReachable) |
| 408 | FixLCSSALoop = FixLCSSALoop->getParentLoop(); |
| 409 | assert(FixLCSSALoop && "Should be a loop!" ); |
| 410 | // We need all DT updates to be done before forming LCSSA. |
| 411 | if (MSSAU) |
| 412 | MSSAU->applyUpdates(Updates: DTUpdates, DT, /*UpdateDT=*/UpdateDTFirst: true); |
| 413 | else |
| 414 | DTU.applyUpdates(Updates: DTUpdates); |
| 415 | DTUpdates.clear(); |
| 416 | formLCSSARecursively(L&: *FixLCSSALoop, DT, LI: &LI, SE: &SE); |
| 417 | SE.forgetBlockAndLoopDispositions(); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | if (MSSAU) { |
| 422 | // Clear all updates now. Facilitates deletes that follow. |
| 423 | MSSAU->applyUpdates(Updates: DTUpdates, DT, /*UpdateDT=*/UpdateDTFirst: true); |
| 424 | DTUpdates.clear(); |
| 425 | if (VerifyMemorySSA) |
| 426 | MSSAU->getMemorySSA()->verifyMemorySSA(); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | /// Delete loop blocks that have become unreachable after folding. Make all |
| 431 | /// relevant updates to DT and LI. |
| 432 | void deleteDeadLoopBlocks() { |
| 433 | if (MSSAU) { |
| 434 | SmallSetVector<BasicBlock *, 8> DeadLoopBlocksSet(DeadLoopBlocks.begin(), |
| 435 | DeadLoopBlocks.end()); |
| 436 | MSSAU->removeBlocks(DeadBlocks: DeadLoopBlocksSet); |
| 437 | } |
| 438 | |
| 439 | // The function LI.erase has some invariants that need to be preserved when |
| 440 | // it tries to remove a loop which is not the top-level loop. In particular, |
| 441 | // it requires loop's preheader to be strictly in loop's parent. We cannot |
| 442 | // just remove blocks one by one, because after removal of preheader we may |
| 443 | // break this invariant for the dead loop. So we detatch and erase all dead |
| 444 | // loops beforehand. |
| 445 | for (auto *BB : DeadLoopBlocks) |
| 446 | if (LI.isLoopHeader(BB)) { |
| 447 | assert(LI.getLoopFor(BB) != &L && "Attempt to remove current loop!" ); |
| 448 | Loop *DL = LI.getLoopFor(BB); |
| 449 | if (!DL->isOutermost()) { |
| 450 | for (auto *PL = DL->getParentLoop(); PL; PL = PL->getParentLoop()) |
| 451 | for (auto *BB : DL->getBlocks()) |
| 452 | PL->removeBlockFromLoop(BB); |
| 453 | DL->getParentLoop()->removeChildLoop(Child: DL); |
| 454 | LI.addTopLevelLoop(New: DL); |
| 455 | } |
| 456 | LI.erase(L: DL); |
| 457 | } |
| 458 | |
| 459 | for (auto *BB : DeadLoopBlocks) { |
| 460 | assert(BB != L.getHeader() && |
| 461 | "Header of the current loop cannot be dead!" ); |
| 462 | LLVM_DEBUG(dbgs() << "Deleting dead loop block " << BB->getName() |
| 463 | << "\n" ); |
| 464 | LI.removeBlock(BB); |
| 465 | } |
| 466 | |
| 467 | detachDeadBlocks(BBs: DeadLoopBlocks, Updates: &DTUpdates, /*KeepOneInputPHIs*/true); |
| 468 | DTU.applyUpdates(Updates: DTUpdates); |
| 469 | DTUpdates.clear(); |
| 470 | for (auto *BB : DeadLoopBlocks) |
| 471 | DTU.deleteBB(DelBB: BB); |
| 472 | |
| 473 | NumLoopBlocksDeleted += DeadLoopBlocks.size(); |
| 474 | } |
| 475 | |
| 476 | /// Constant-fold terminators of blocks accumulated in FoldCandidates into the |
| 477 | /// unconditional branches. |
| 478 | void foldTerminators() { |
| 479 | for (BasicBlock *BB : FoldCandidates) { |
| 480 | assert(LI.getLoopFor(BB) == &L && "Should be a loop block!" ); |
| 481 | BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(BB); |
| 482 | assert(TheOnlySucc && "Should have one live successor!" ); |
| 483 | |
| 484 | LLVM_DEBUG(dbgs() << "Replacing terminator of " << BB->getName() |
| 485 | << " with an unconditional branch to the block " |
| 486 | << TheOnlySucc->getName() << "\n" ); |
| 487 | |
| 488 | SmallPtrSet<BasicBlock *, 2> DeadSuccessors; |
| 489 | // Remove all BB's successors except for the live one. |
| 490 | unsigned TheOnlySuccDuplicates = 0; |
| 491 | for (auto *Succ : successors(BB)) |
| 492 | if (Succ != TheOnlySucc) { |
| 493 | DeadSuccessors.insert(Ptr: Succ); |
| 494 | // If our successor lies in a different loop, we don't want to remove |
| 495 | // the one-input Phi because it is a LCSSA Phi. |
| 496 | bool PreserveLCSSAPhi = !L.contains(BB: Succ); |
| 497 | Succ->removePredecessor(Pred: BB, KeepOneInputPHIs: PreserveLCSSAPhi); |
| 498 | if (MSSAU) |
| 499 | MSSAU->removeEdge(From: BB, To: Succ); |
| 500 | } else |
| 501 | ++TheOnlySuccDuplicates; |
| 502 | |
| 503 | assert(TheOnlySuccDuplicates > 0 && "Should be!" ); |
| 504 | // If TheOnlySucc was BB's successor more than once, after transform it |
| 505 | // will be its successor only once. Remove redundant inputs from |
| 506 | // TheOnlySucc's Phis. |
| 507 | bool PreserveLCSSAPhi = !L.contains(BB: TheOnlySucc); |
| 508 | for (unsigned Dup = 1; Dup < TheOnlySuccDuplicates; ++Dup) |
| 509 | TheOnlySucc->removePredecessor(Pred: BB, KeepOneInputPHIs: PreserveLCSSAPhi); |
| 510 | if (MSSAU && TheOnlySuccDuplicates > 1) |
| 511 | MSSAU->removeDuplicatePhiEdgesBetween(From: BB, To: TheOnlySucc); |
| 512 | |
| 513 | IRBuilder<> Builder(BB->getContext()); |
| 514 | Instruction *Term = BB->getTerminator(); |
| 515 | Builder.SetInsertPoint(Term); |
| 516 | Builder.CreateBr(Dest: TheOnlySucc); |
| 517 | Term->eraseFromParent(); |
| 518 | |
| 519 | for (auto *DeadSucc : DeadSuccessors) |
| 520 | DTUpdates.push_back(Elt: {DominatorTree::Delete, BB, DeadSucc}); |
| 521 | |
| 522 | ++NumTerminatorsFolded; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | public: |
| 527 | ConstantTerminatorFoldingImpl(Loop &L, LoopInfo &LI, DominatorTree &DT, |
| 528 | ScalarEvolution &SE, |
| 529 | MemorySSAUpdater *MSSAU) |
| 530 | : L(L), LI(LI), DT(DT), SE(SE), MSSAU(MSSAU), DFS(&L), |
| 531 | DTU(DT, DomTreeUpdater::UpdateStrategy::Eager) {} |
| 532 | bool run() { |
| 533 | assert(L.getLoopLatch() && "Should be single latch!" ); |
| 534 | |
| 535 | // Collect all available information about status of blocks after constant |
| 536 | // folding. |
| 537 | analyze(); |
| 538 | BasicBlock * = L.getHeader(); |
| 539 | (void)Header; |
| 540 | |
| 541 | LLVM_DEBUG(dbgs() << "In function " << Header->getParent()->getName() |
| 542 | << ": " ); |
| 543 | |
| 544 | if (HasIrreducibleCFG) { |
| 545 | LLVM_DEBUG(dbgs() << "Loops with irreducible CFG are not supported!\n" ); |
| 546 | return false; |
| 547 | } |
| 548 | |
| 549 | // Nothing to constant-fold. |
| 550 | if (FoldCandidates.empty()) { |
| 551 | LLVM_DEBUG( |
| 552 | dbgs() << "No constant terminator folding candidates found in loop " |
| 553 | << Header->getName() << "\n" ); |
| 554 | return false; |
| 555 | } |
| 556 | |
| 557 | // TODO: Support deletion of the current loop. |
| 558 | if (DeleteCurrentLoop) { |
| 559 | LLVM_DEBUG( |
| 560 | dbgs() |
| 561 | << "Give up constant terminator folding in loop " << Header->getName() |
| 562 | << ": we don't currently support deletion of the current loop.\n" ); |
| 563 | return false; |
| 564 | } |
| 565 | |
| 566 | // TODO: Support blocks that are not dead, but also not in loop after the |
| 567 | // folding. |
| 568 | if (BlocksInLoopAfterFolding.size() + DeadLoopBlocks.size() != |
| 569 | L.getNumBlocks()) { |
| 570 | LLVM_DEBUG( |
| 571 | dbgs() << "Give up constant terminator folding in loop " |
| 572 | << Header->getName() << ": we don't currently" |
| 573 | " support blocks that are not dead, but will stop " |
| 574 | "being a part of the loop after constant-folding.\n" ); |
| 575 | return false; |
| 576 | } |
| 577 | |
| 578 | // TODO: Tokens may breach LCSSA form by default. However, the transform for |
| 579 | // dead exit blocks requires LCSSA form to be maintained for all values, |
| 580 | // tokens included, otherwise it may break use-def dominance (see PR56243). |
| 581 | if (!DeadExitBlocks.empty() && !L.isLCSSAForm(DT, /*IgnoreTokens*/ false)) { |
| 582 | assert(L.isLCSSAForm(DT, /*IgnoreTokens*/ true) && |
| 583 | "LCSSA broken not by tokens?" ); |
| 584 | LLVM_DEBUG(dbgs() << "Give up constant terminator folding in loop " |
| 585 | << Header->getName() |
| 586 | << ": tokens uses potentially break LCSSA form.\n" ); |
| 587 | return false; |
| 588 | } |
| 589 | |
| 590 | SE.forgetTopmostLoop(L: &L); |
| 591 | // Dump analysis results. |
| 592 | LLVM_DEBUG(dump()); |
| 593 | |
| 594 | LLVM_DEBUG(dbgs() << "Constant-folding " << FoldCandidates.size() |
| 595 | << " terminators in loop " << Header->getName() << "\n" ); |
| 596 | |
| 597 | if (!DeadLoopBlocks.empty()) |
| 598 | SE.forgetBlockAndLoopDispositions(); |
| 599 | |
| 600 | // Make the actual transforms. |
| 601 | handleDeadExits(); |
| 602 | foldTerminators(); |
| 603 | |
| 604 | if (!DeadLoopBlocks.empty()) { |
| 605 | LLVM_DEBUG(dbgs() << "Deleting " << DeadLoopBlocks.size() |
| 606 | << " dead blocks in loop " << Header->getName() << "\n" ); |
| 607 | deleteDeadLoopBlocks(); |
| 608 | } else { |
| 609 | // If we didn't do updates inside deleteDeadLoopBlocks, do them here. |
| 610 | DTU.applyUpdates(Updates: DTUpdates); |
| 611 | DTUpdates.clear(); |
| 612 | } |
| 613 | |
| 614 | if (MSSAU && VerifyMemorySSA) |
| 615 | MSSAU->getMemorySSA()->verifyMemorySSA(); |
| 616 | |
| 617 | #ifndef NDEBUG |
| 618 | // Make sure that we have preserved all data structures after the transform. |
| 619 | #if defined(EXPENSIVE_CHECKS) |
| 620 | assert(DT.verify(DominatorTree::VerificationLevel::Full) && |
| 621 | "DT broken after transform!" ); |
| 622 | #else |
| 623 | assert(DT.verify(DominatorTree::VerificationLevel::Fast) && |
| 624 | "DT broken after transform!" ); |
| 625 | #endif |
| 626 | assert(DT.isReachableFromEntry(Header)); |
| 627 | LI.verify(DT); |
| 628 | #endif |
| 629 | |
| 630 | return true; |
| 631 | } |
| 632 | |
| 633 | bool foldingBreaksCurrentLoop() const { |
| 634 | return DeleteCurrentLoop; |
| 635 | } |
| 636 | }; |
| 637 | } // namespace |
| 638 | |
| 639 | /// Turn branches and switches with known constant conditions into unconditional |
| 640 | /// branches. |
| 641 | static bool constantFoldTerminators(Loop &L, DominatorTree &DT, LoopInfo &LI, |
| 642 | ScalarEvolution &SE, |
| 643 | MemorySSAUpdater *MSSAU, |
| 644 | bool &IsLoopDeleted) { |
| 645 | if (!EnableTermFolding) |
| 646 | return false; |
| 647 | |
| 648 | // To keep things simple, only process loops with single latch. We |
| 649 | // canonicalize most loops to this form. We can support multi-latch if needed. |
| 650 | if (!L.getLoopLatch()) |
| 651 | return false; |
| 652 | |
| 653 | ConstantTerminatorFoldingImpl BranchFolder(L, LI, DT, SE, MSSAU); |
| 654 | bool Changed = BranchFolder.run(); |
| 655 | IsLoopDeleted = Changed && BranchFolder.foldingBreaksCurrentLoop(); |
| 656 | return Changed; |
| 657 | } |
| 658 | |
| 659 | static bool mergeBlocksIntoPredecessors(Loop &L, DominatorTree &DT, |
| 660 | LoopInfo &LI, MemorySSAUpdater *MSSAU, |
| 661 | ScalarEvolution &SE) { |
| 662 | bool Changed = false; |
| 663 | DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); |
| 664 | // Copy blocks into a temporary array to avoid iterator invalidation issues |
| 665 | // as we remove them. |
| 666 | SmallVector<WeakTrackingVH, 16> Blocks(L.blocks()); |
| 667 | |
| 668 | for (auto &Block : Blocks) { |
| 669 | // Attempt to merge blocks in the trivial case. Don't modify blocks which |
| 670 | // belong to other loops. |
| 671 | BasicBlock *Succ = cast_or_null<BasicBlock>(Val&: Block); |
| 672 | if (!Succ) |
| 673 | continue; |
| 674 | |
| 675 | BasicBlock *Pred = Succ->getSinglePredecessor(); |
| 676 | if (!Pred || !Pred->getSingleSuccessor() || LI.getLoopFor(BB: Pred) != &L) |
| 677 | continue; |
| 678 | |
| 679 | // Merge Succ into Pred and delete it. |
| 680 | MergeBlockIntoPredecessor(BB: Succ, DTU: &DTU, LI: &LI, MSSAU); |
| 681 | |
| 682 | if (MSSAU && VerifyMemorySSA) |
| 683 | MSSAU->getMemorySSA()->verifyMemorySSA(); |
| 684 | |
| 685 | Changed = true; |
| 686 | } |
| 687 | |
| 688 | if (Changed) |
| 689 | SE.forgetBlockAndLoopDispositions(); |
| 690 | |
| 691 | return Changed; |
| 692 | } |
| 693 | |
| 694 | static bool simplifyLoopCFG(Loop &L, DominatorTree &DT, LoopInfo &LI, |
| 695 | ScalarEvolution &SE, MemorySSAUpdater *MSSAU, |
| 696 | bool &IsLoopDeleted) { |
| 697 | bool Changed = false; |
| 698 | |
| 699 | // Constant-fold terminators with known constant conditions. |
| 700 | Changed |= constantFoldTerminators(L, DT, LI, SE, MSSAU, IsLoopDeleted); |
| 701 | |
| 702 | if (IsLoopDeleted) |
| 703 | return true; |
| 704 | |
| 705 | // Eliminate unconditional branches by merging blocks into their predecessors. |
| 706 | Changed |= mergeBlocksIntoPredecessors(L, DT, LI, MSSAU, SE); |
| 707 | |
| 708 | if (Changed) |
| 709 | SE.forgetTopmostLoop(L: &L); |
| 710 | |
| 711 | return Changed; |
| 712 | } |
| 713 | |
| 714 | PreservedAnalyses LoopSimplifyCFGPass::run(Loop &L, LoopAnalysisManager &AM, |
| 715 | LoopStandardAnalysisResults &AR, |
| 716 | LPMUpdater &LPMU) { |
| 717 | std::optional<MemorySSAUpdater> MSSAU; |
| 718 | if (AR.MSSA) |
| 719 | MSSAU = MemorySSAUpdater(AR.MSSA); |
| 720 | bool DeleteCurrentLoop = false; |
| 721 | if (!simplifyLoopCFG(L, DT&: AR.DT, LI&: AR.LI, SE&: AR.SE, MSSAU: MSSAU ? &*MSSAU : nullptr, |
| 722 | IsLoopDeleted&: DeleteCurrentLoop)) |
| 723 | return PreservedAnalyses::all(); |
| 724 | |
| 725 | if (DeleteCurrentLoop) |
| 726 | LPMU.markLoopAsDeleted(L, Name: "loop-simplifycfg" ); |
| 727 | |
| 728 | auto PA = getLoopPassPreservedAnalyses(); |
| 729 | if (AR.MSSA) |
| 730 | PA.preserve<MemorySSAAnalysis>(); |
| 731 | return PA; |
| 732 | } |
| 733 | |