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/AliasAnalysis.h" 20 #include "llvm/Analysis/AssumptionCache.h" 21 #include "llvm/Analysis/BasicAliasAnalysis.h" 22 #include "llvm/Analysis/DependenceAnalysis.h" 23 #include "llvm/Analysis/DomTreeUpdater.h" 24 #include "llvm/Analysis/GlobalsModRef.h" 25 #include "llvm/Analysis/LoopInfo.h" 26 #include "llvm/Analysis/LoopPass.h" 27 #include "llvm/Analysis/MemorySSA.h" 28 #include "llvm/Analysis/MemorySSAUpdater.h" 29 #include "llvm/Analysis/ScalarEvolution.h" 30 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" 31 #include "llvm/Analysis/TargetTransformInfo.h" 32 #include "llvm/IR/Dominators.h" 33 #include "llvm/IR/IRBuilder.h" 34 #include "llvm/InitializePasses.h" 35 #include "llvm/Support/CommandLine.h" 36 #include "llvm/Transforms/Scalar.h" 37 #include "llvm/Transforms/Scalar/LoopPassManager.h" 38 #include "llvm/Transforms/Utils.h" 39 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 40 #include "llvm/Transforms/Utils/Local.h" 41 #include "llvm/Transforms/Utils/LoopUtils.h" 42 using namespace llvm; 43 44 #define DEBUG_TYPE "loop-simplifycfg" 45 46 static cl::opt<bool> EnableTermFolding("enable-loop-simplifycfg-term-folding", 47 cl::init(true)); 48 49 STATISTIC(NumTerminatorsFolded, 50 "Number of terminators folded to unconditional branches"); 51 STATISTIC(NumLoopBlocksDeleted, 52 "Number of loop blocks deleted"); 53 STATISTIC(NumLoopExitsDeleted, 54 "Number of loop exiting edges deleted"); 55 56 /// If \p BB is a switch or a conditional branch, but only one of its successors 57 /// can be reached from this block in runtime, return this successor. Otherwise, 58 /// return nullptr. 59 static BasicBlock *getOnlyLiveSuccessor(BasicBlock *BB) { 60 Instruction *TI = BB->getTerminator(); 61 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { 62 if (BI->isUnconditional()) 63 return nullptr; 64 if (BI->getSuccessor(0) == BI->getSuccessor(1)) 65 return BI->getSuccessor(0); 66 ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition()); 67 if (!Cond) 68 return nullptr; 69 return Cond->isZero() ? BI->getSuccessor(1) : BI->getSuccessor(0); 70 } 71 72 if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { 73 auto *CI = dyn_cast<ConstantInt>(SI->getCondition()); 74 if (!CI) 75 return nullptr; 76 for (auto Case : SI->cases()) 77 if (Case.getCaseValue() == CI) 78 return Case.getCaseSuccessor(); 79 return SI->getDefaultDest(); 80 } 81 82 return nullptr; 83 } 84 85 /// Removes \p BB from all loops from [FirstLoop, LastLoop) in parent chain. 86 static void removeBlockFromLoops(BasicBlock *BB, Loop *FirstLoop, 87 Loop *LastLoop = nullptr) { 88 assert((!LastLoop || LastLoop->contains(FirstLoop->getHeader())) && 89 "First loop is supposed to be inside of last loop!"); 90 assert(FirstLoop->contains(BB) && "Must be a loop block!"); 91 for (Loop *Current = FirstLoop; Current != LastLoop; 92 Current = Current->getParentLoop()) 93 Current->removeBlockFromLoop(BB); 94 } 95 96 /// Find innermost loop that contains at least one block from \p BBs and 97 /// contains the header of loop \p L. 98 static Loop *getInnermostLoopFor(SmallPtrSetImpl<BasicBlock *> &BBs, 99 Loop &L, LoopInfo &LI) { 100 Loop *Innermost = nullptr; 101 for (BasicBlock *BB : BBs) { 102 Loop *BBL = LI.getLoopFor(BB); 103 while (BBL && !BBL->contains(L.getHeader())) 104 BBL = BBL->getParentLoop(); 105 if (BBL == &L) 106 BBL = BBL->getParentLoop(); 107 if (!BBL) 108 continue; 109 if (!Innermost || BBL->getLoopDepth() > Innermost->getLoopDepth()) 110 Innermost = BBL; 111 } 112 return Innermost; 113 } 114 115 namespace { 116 /// Helper class that can turn branches and switches with constant conditions 117 /// into unconditional branches. 118 class ConstantTerminatorFoldingImpl { 119 private: 120 Loop &L; 121 LoopInfo &LI; 122 DominatorTree &DT; 123 ScalarEvolution &SE; 124 MemorySSAUpdater *MSSAU; 125 LoopBlocksDFS DFS; 126 DomTreeUpdater DTU; 127 SmallVector<DominatorTree::UpdateType, 16> DTUpdates; 128 129 // Whether or not the current loop has irreducible CFG. 130 bool HasIrreducibleCFG = false; 131 // Whether or not the current loop will still exist after terminator constant 132 // folding will be done. In theory, there are two ways how it can happen: 133 // 1. Loop's latch(es) become unreachable from loop header; 134 // 2. Loop's header becomes unreachable from method entry. 135 // In practice, the second situation is impossible because we only modify the 136 // current loop and its preheader and do not affect preheader's reachibility 137 // from any other block. So this variable set to true means that loop's latch 138 // has become unreachable from loop header. 139 bool DeleteCurrentLoop = false; 140 141 // The blocks of the original loop that will still be reachable from entry 142 // after the constant folding. 143 SmallPtrSet<BasicBlock *, 8> LiveLoopBlocks; 144 // The blocks of the original loop that will become unreachable from entry 145 // after the constant folding. 146 SmallVector<BasicBlock *, 8> DeadLoopBlocks; 147 // The exits of the original loop that will still be reachable from entry 148 // after the constant folding. 149 SmallPtrSet<BasicBlock *, 8> LiveExitBlocks; 150 // The exits of the original loop that will become unreachable from entry 151 // after the constant folding. 152 SmallVector<BasicBlock *, 8> DeadExitBlocks; 153 // The blocks that will still be a part of the current loop after folding. 154 SmallPtrSet<BasicBlock *, 8> BlocksInLoopAfterFolding; 155 // The blocks that have terminators with constant condition that can be 156 // folded. Note: fold candidates should be in L but not in any of its 157 // subloops to avoid complex LI updates. 158 SmallVector<BasicBlock *, 8> FoldCandidates; 159 160 void dump() const { 161 dbgs() << "Constant terminator folding for loop " << L << "\n"; 162 dbgs() << "After terminator constant-folding, the loop will"; 163 if (!DeleteCurrentLoop) 164 dbgs() << " not"; 165 dbgs() << " be destroyed\n"; 166 auto PrintOutVector = [&](const char *Message, 167 const SmallVectorImpl<BasicBlock *> &S) { 168 dbgs() << Message << "\n"; 169 for (const BasicBlock *BB : S) 170 dbgs() << "\t" << BB->getName() << "\n"; 171 }; 172 auto PrintOutSet = [&](const char *Message, 173 const SmallPtrSetImpl<BasicBlock *> &S) { 174 dbgs() << Message << "\n"; 175 for (const BasicBlock *BB : S) 176 dbgs() << "\t" << BB->getName() << "\n"; 177 }; 178 PrintOutVector("Blocks in which we can constant-fold terminator:", 179 FoldCandidates); 180 PrintOutSet("Live blocks from the original loop:", LiveLoopBlocks); 181 PrintOutVector("Dead blocks from the original loop:", DeadLoopBlocks); 182 PrintOutSet("Live exit blocks:", LiveExitBlocks); 183 PrintOutVector("Dead exit blocks:", DeadExitBlocks); 184 if (!DeleteCurrentLoop) 185 PrintOutSet("The following blocks will still be part of the loop:", 186 BlocksInLoopAfterFolding); 187 } 188 189 /// Whether or not the current loop has irreducible CFG. 190 bool hasIrreducibleCFG(LoopBlocksDFS &DFS) { 191 assert(DFS.isComplete() && "DFS is expected to be finished"); 192 // Index of a basic block in RPO traversal. 193 DenseMap<const BasicBlock *, unsigned> RPO; 194 unsigned Current = 0; 195 for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) 196 RPO[*I] = Current++; 197 198 for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) { 199 BasicBlock *BB = *I; 200 for (auto *Succ : successors(BB)) 201 if (L.contains(Succ) && !LI.isLoopHeader(Succ) && RPO[BB] > RPO[Succ]) 202 // If an edge goes from a block with greater order number into a block 203 // with lesses number, and it is not a loop backedge, then it can only 204 // be a part of irreducible non-loop cycle. 205 return true; 206 } 207 return false; 208 } 209 210 /// Fill all information about status of blocks and exits of the current loop 211 /// if constant folding of all branches will be done. 212 void analyze() { 213 DFS.perform(&LI); 214 assert(DFS.isComplete() && "DFS is expected to be finished"); 215 216 // TODO: The algorithm below relies on both RPO and Postorder traversals. 217 // When the loop has only reducible CFG inside, then the invariant "all 218 // predecessors of X are processed before X in RPO" is preserved. However 219 // an irreducible loop can break this invariant (e.g. latch does not have to 220 // be the last block in the traversal in this case, and the algorithm relies 221 // on this). We can later decide to support such cases by altering the 222 // algorithms, but so far we just give up analyzing them. 223 if (hasIrreducibleCFG(DFS)) { 224 HasIrreducibleCFG = true; 225 return; 226 } 227 228 // Collect live and dead loop blocks and exits. 229 LiveLoopBlocks.insert(L.getHeader()); 230 for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) { 231 BasicBlock *BB = *I; 232 233 // If a loop block wasn't marked as live so far, then it's dead. 234 if (!LiveLoopBlocks.count(BB)) { 235 DeadLoopBlocks.push_back(BB); 236 continue; 237 } 238 239 BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(BB); 240 241 // If a block has only one live successor, it's a candidate on constant 242 // folding. Only handle blocks from current loop: branches in child loops 243 // are skipped because if they can be folded, they should be folded during 244 // the processing of child loops. 245 bool TakeFoldCandidate = TheOnlySucc && LI.getLoopFor(BB) == &L; 246 if (TakeFoldCandidate) 247 FoldCandidates.push_back(BB); 248 249 // Handle successors. 250 for (BasicBlock *Succ : successors(BB)) 251 if (!TakeFoldCandidate || TheOnlySucc == Succ) { 252 if (L.contains(Succ)) 253 LiveLoopBlocks.insert(Succ); 254 else 255 LiveExitBlocks.insert(Succ); 256 } 257 } 258 259 // Sanity check: amount of dead and live loop blocks should match the total 260 // number of blocks in loop. 261 assert(L.getNumBlocks() == LiveLoopBlocks.size() + DeadLoopBlocks.size() && 262 "Malformed block sets?"); 263 264 // Now, all exit blocks that are not marked as live are dead. 265 SmallVector<BasicBlock *, 8> ExitBlocks; 266 L.getExitBlocks(ExitBlocks); 267 SmallPtrSet<BasicBlock *, 8> UniqueDeadExits; 268 for (auto *ExitBlock : ExitBlocks) 269 if (!LiveExitBlocks.count(ExitBlock) && 270 UniqueDeadExits.insert(ExitBlock).second) 271 DeadExitBlocks.push_back(ExitBlock); 272 273 // Whether or not the edge From->To will still be present in graph after the 274 // folding. 275 auto IsEdgeLive = [&](BasicBlock *From, BasicBlock *To) { 276 if (!LiveLoopBlocks.count(From)) 277 return false; 278 BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(From); 279 return !TheOnlySucc || TheOnlySucc == To || LI.getLoopFor(From) != &L; 280 }; 281 282 // The loop will not be destroyed if its latch is live. 283 DeleteCurrentLoop = !IsEdgeLive(L.getLoopLatch(), L.getHeader()); 284 285 // If we are going to delete the current loop completely, no extra analysis 286 // is needed. 287 if (DeleteCurrentLoop) 288 return; 289 290 // Otherwise, we should check which blocks will still be a part of the 291 // current loop after the transform. 292 BlocksInLoopAfterFolding.insert(L.getLoopLatch()); 293 // If the loop is live, then we should compute what blocks are still in 294 // loop after all branch folding has been done. A block is in loop if 295 // it has a live edge to another block that is in the loop; by definition, 296 // latch is in the loop. 297 auto BlockIsInLoop = [&](BasicBlock *BB) { 298 return any_of(successors(BB), [&](BasicBlock *Succ) { 299 return BlocksInLoopAfterFolding.count(Succ) && IsEdgeLive(BB, Succ); 300 }); 301 }; 302 for (auto I = DFS.beginPostorder(), E = DFS.endPostorder(); I != E; ++I) { 303 BasicBlock *BB = *I; 304 if (BlockIsInLoop(BB)) 305 BlocksInLoopAfterFolding.insert(BB); 306 } 307 308 // Sanity check: header must be in loop. 309 assert(BlocksInLoopAfterFolding.count(L.getHeader()) && 310 "Header not in loop?"); 311 assert(BlocksInLoopAfterFolding.size() <= LiveLoopBlocks.size() && 312 "All blocks that stay in loop should be live!"); 313 } 314 315 /// We need to preserve static reachibility of all loop exit blocks (this is) 316 /// required by loop pass manager. In order to do it, we make the following 317 /// trick: 318 /// 319 /// preheader: 320 /// <preheader code> 321 /// br label %loop_header 322 /// 323 /// loop_header: 324 /// ... 325 /// br i1 false, label %dead_exit, label %loop_block 326 /// ... 327 /// 328 /// We cannot simply remove edge from the loop to dead exit because in this 329 /// case dead_exit (and its successors) may become unreachable. To avoid that, 330 /// we insert the following fictive preheader: 331 /// 332 /// preheader: 333 /// <preheader code> 334 /// switch i32 0, label %preheader-split, 335 /// [i32 1, label %dead_exit_1], 336 /// [i32 2, label %dead_exit_2], 337 /// ... 338 /// [i32 N, label %dead_exit_N], 339 /// 340 /// preheader-split: 341 /// br label %loop_header 342 /// 343 /// loop_header: 344 /// ... 345 /// br i1 false, label %dead_exit_N, label %loop_block 346 /// ... 347 /// 348 /// Doing so, we preserve static reachibility of all dead exits and can later 349 /// remove edges from the loop to these blocks. 350 void handleDeadExits() { 351 // If no dead exits, nothing to do. 352 if (DeadExitBlocks.empty()) 353 return; 354 355 // Construct split preheader and the dummy switch to thread edges from it to 356 // dead exits. 357 BasicBlock *Preheader = L.getLoopPreheader(); 358 BasicBlock *NewPreheader = llvm::SplitBlock( 359 Preheader, Preheader->getTerminator(), &DT, &LI, MSSAU); 360 361 IRBuilder<> Builder(Preheader->getTerminator()); 362 SwitchInst *DummySwitch = 363 Builder.CreateSwitch(Builder.getInt32(0), NewPreheader); 364 Preheader->getTerminator()->eraseFromParent(); 365 366 unsigned DummyIdx = 1; 367 for (BasicBlock *BB : DeadExitBlocks) { 368 SmallVector<Instruction *, 4> DeadPhis; 369 for (auto &PN : BB->phis()) 370 DeadPhis.push_back(&PN); 371 372 // Eliminate all Phis from dead exits. 373 for (Instruction *PN : DeadPhis) { 374 PN->replaceAllUsesWith(UndefValue::get(PN->getType())); 375 PN->eraseFromParent(); 376 } 377 assert(DummyIdx != 0 && "Too many dead exits!"); 378 DummySwitch->addCase(Builder.getInt32(DummyIdx++), BB); 379 DTUpdates.push_back({DominatorTree::Insert, Preheader, BB}); 380 ++NumLoopExitsDeleted; 381 } 382 383 assert(L.getLoopPreheader() == NewPreheader && "Malformed CFG?"); 384 if (Loop *OuterLoop = LI.getLoopFor(Preheader)) { 385 // When we break dead edges, the outer loop may become unreachable from 386 // the current loop. We need to fix loop info accordingly. For this, we 387 // find the most nested loop that still contains L and remove L from all 388 // loops that are inside of it. 389 Loop *StillReachable = getInnermostLoopFor(LiveExitBlocks, L, LI); 390 391 // Okay, our loop is no longer in the outer loop (and maybe not in some of 392 // its parents as well). Make the fixup. 393 if (StillReachable != OuterLoop) { 394 LI.changeLoopFor(NewPreheader, StillReachable); 395 removeBlockFromLoops(NewPreheader, OuterLoop, StillReachable); 396 for (auto *BB : L.blocks()) 397 removeBlockFromLoops(BB, OuterLoop, StillReachable); 398 OuterLoop->removeChildLoop(&L); 399 if (StillReachable) 400 StillReachable->addChildLoop(&L); 401 else 402 LI.addTopLevelLoop(&L); 403 404 // Some values from loops in [OuterLoop, StillReachable) could be used 405 // in the current loop. Now it is not their child anymore, so such uses 406 // require LCSSA Phis. 407 Loop *FixLCSSALoop = OuterLoop; 408 while (FixLCSSALoop->getParentLoop() != StillReachable) 409 FixLCSSALoop = FixLCSSALoop->getParentLoop(); 410 assert(FixLCSSALoop && "Should be a loop!"); 411 // We need all DT updates to be done before forming LCSSA. 412 DTU.applyUpdates(DTUpdates); 413 if (MSSAU) 414 MSSAU->applyUpdates(DTUpdates, DT); 415 DTUpdates.clear(); 416 formLCSSARecursively(*FixLCSSALoop, DT, &LI, &SE); 417 } 418 } 419 420 if (MSSAU) { 421 // Clear all updates now. Facilitates deletes that follow. 422 DTU.applyUpdates(DTUpdates); 423 MSSAU->applyUpdates(DTUpdates, DT); 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(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->getParentLoop()) { 450 for (auto *PL = DL->getParentLoop(); PL; PL = PL->getParentLoop()) 451 for (auto *BB : DL->getBlocks()) 452 PL->removeBlockFromLoop(BB); 453 DL->getParentLoop()->removeChildLoop(DL); 454 LI.addTopLevelLoop(DL); 455 } 456 LI.erase(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 DetatchDeadBlocks(DeadLoopBlocks, &DTUpdates, /*KeepOneInputPHIs*/true); 468 DTU.applyUpdates(DTUpdates); 469 DTUpdates.clear(); 470 for (auto *BB : DeadLoopBlocks) 471 DTU.deleteBB(BB); 472 473 NumLoopBlocksDeleted += DeadLoopBlocks.size(); 474 } 475 476 /// Constant-fold terminators of blocks acculumated 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(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(Succ); 497 Succ->removePredecessor(BB, PreserveLCSSAPhi); 498 if (MSSAU) 499 MSSAU->removeEdge(BB, 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(TheOnlySucc); 508 for (unsigned Dup = 1; Dup < TheOnlySuccDuplicates; ++Dup) 509 TheOnlySucc->removePredecessor(BB, PreserveLCSSAPhi); 510 if (MSSAU && TheOnlySuccDuplicates > 1) 511 MSSAU->removeDuplicatePhiEdgesBetween(BB, TheOnlySucc); 512 513 IRBuilder<> Builder(BB->getContext()); 514 Instruction *Term = BB->getTerminator(); 515 Builder.SetInsertPoint(Term); 516 Builder.CreateBr(TheOnlySucc); 517 Term->eraseFromParent(); 518 519 for (auto *DeadSucc : DeadSuccessors) 520 DTUpdates.push_back({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 *Header = 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 SE.forgetTopmostLoop(&L); 579 // Dump analysis results. 580 LLVM_DEBUG(dump()); 581 582 LLVM_DEBUG(dbgs() << "Constant-folding " << FoldCandidates.size() 583 << " terminators in loop " << Header->getName() << "\n"); 584 585 // Make the actual transforms. 586 handleDeadExits(); 587 foldTerminators(); 588 589 if (!DeadLoopBlocks.empty()) { 590 LLVM_DEBUG(dbgs() << "Deleting " << DeadLoopBlocks.size() 591 << " dead blocks in loop " << Header->getName() << "\n"); 592 deleteDeadLoopBlocks(); 593 } else { 594 // If we didn't do updates inside deleteDeadLoopBlocks, do them here. 595 DTU.applyUpdates(DTUpdates); 596 DTUpdates.clear(); 597 } 598 599 if (MSSAU && VerifyMemorySSA) 600 MSSAU->getMemorySSA()->verifyMemorySSA(); 601 602 #ifndef NDEBUG 603 // Make sure that we have preserved all data structures after the transform. 604 #if defined(EXPENSIVE_CHECKS) 605 assert(DT.verify(DominatorTree::VerificationLevel::Full) && 606 "DT broken after transform!"); 607 #else 608 assert(DT.verify(DominatorTree::VerificationLevel::Fast) && 609 "DT broken after transform!"); 610 #endif 611 assert(DT.isReachableFromEntry(Header)); 612 LI.verify(DT); 613 #endif 614 615 return true; 616 } 617 618 bool foldingBreaksCurrentLoop() const { 619 return DeleteCurrentLoop; 620 } 621 }; 622 } // namespace 623 624 /// Turn branches and switches with known constant conditions into unconditional 625 /// branches. 626 static bool constantFoldTerminators(Loop &L, DominatorTree &DT, LoopInfo &LI, 627 ScalarEvolution &SE, 628 MemorySSAUpdater *MSSAU, 629 bool &IsLoopDeleted) { 630 if (!EnableTermFolding) 631 return false; 632 633 // To keep things simple, only process loops with single latch. We 634 // canonicalize most loops to this form. We can support multi-latch if needed. 635 if (!L.getLoopLatch()) 636 return false; 637 638 ConstantTerminatorFoldingImpl BranchFolder(L, LI, DT, SE, MSSAU); 639 bool Changed = BranchFolder.run(); 640 IsLoopDeleted = Changed && BranchFolder.foldingBreaksCurrentLoop(); 641 return Changed; 642 } 643 644 static bool mergeBlocksIntoPredecessors(Loop &L, DominatorTree &DT, 645 LoopInfo &LI, MemorySSAUpdater *MSSAU) { 646 bool Changed = false; 647 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); 648 // Copy blocks into a temporary array to avoid iterator invalidation issues 649 // as we remove them. 650 SmallVector<WeakTrackingVH, 16> Blocks(L.blocks()); 651 652 for (auto &Block : Blocks) { 653 // Attempt to merge blocks in the trivial case. Don't modify blocks which 654 // belong to other loops. 655 BasicBlock *Succ = cast_or_null<BasicBlock>(Block); 656 if (!Succ) 657 continue; 658 659 BasicBlock *Pred = Succ->getSinglePredecessor(); 660 if (!Pred || !Pred->getSingleSuccessor() || LI.getLoopFor(Pred) != &L) 661 continue; 662 663 // Merge Succ into Pred and delete it. 664 MergeBlockIntoPredecessor(Succ, &DTU, &LI, MSSAU); 665 666 if (MSSAU && VerifyMemorySSA) 667 MSSAU->getMemorySSA()->verifyMemorySSA(); 668 669 Changed = true; 670 } 671 672 return Changed; 673 } 674 675 static bool simplifyLoopCFG(Loop &L, DominatorTree &DT, LoopInfo &LI, 676 ScalarEvolution &SE, MemorySSAUpdater *MSSAU, 677 bool &IsLoopDeleted) { 678 bool Changed = false; 679 680 // Constant-fold terminators with known constant conditions. 681 Changed |= constantFoldTerminators(L, DT, LI, SE, MSSAU, IsLoopDeleted); 682 683 if (IsLoopDeleted) 684 return true; 685 686 // Eliminate unconditional branches by merging blocks into their predecessors. 687 Changed |= mergeBlocksIntoPredecessors(L, DT, LI, MSSAU); 688 689 if (Changed) 690 SE.forgetTopmostLoop(&L); 691 692 return Changed; 693 } 694 695 PreservedAnalyses LoopSimplifyCFGPass::run(Loop &L, LoopAnalysisManager &AM, 696 LoopStandardAnalysisResults &AR, 697 LPMUpdater &LPMU) { 698 Optional<MemorySSAUpdater> MSSAU; 699 if (AR.MSSA) 700 MSSAU = MemorySSAUpdater(AR.MSSA); 701 bool DeleteCurrentLoop = false; 702 if (!simplifyLoopCFG(L, AR.DT, AR.LI, AR.SE, 703 MSSAU.hasValue() ? MSSAU.getPointer() : nullptr, 704 DeleteCurrentLoop)) 705 return PreservedAnalyses::all(); 706 707 if (DeleteCurrentLoop) 708 LPMU.markLoopAsDeleted(L, "loop-simplifycfg"); 709 710 auto PA = getLoopPassPreservedAnalyses(); 711 if (AR.MSSA) 712 PA.preserve<MemorySSAAnalysis>(); 713 return PA; 714 } 715 716 namespace { 717 class LoopSimplifyCFGLegacyPass : public LoopPass { 718 public: 719 static char ID; // Pass ID, replacement for typeid 720 LoopSimplifyCFGLegacyPass() : LoopPass(ID) { 721 initializeLoopSimplifyCFGLegacyPassPass(*PassRegistry::getPassRegistry()); 722 } 723 724 bool runOnLoop(Loop *L, LPPassManager &LPM) override { 725 if (skipLoop(L)) 726 return false; 727 728 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 729 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 730 ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); 731 Optional<MemorySSAUpdater> MSSAU; 732 if (EnableMSSALoopDependency) { 733 MemorySSA *MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA(); 734 MSSAU = MemorySSAUpdater(MSSA); 735 if (VerifyMemorySSA) 736 MSSA->verifyMemorySSA(); 737 } 738 bool DeleteCurrentLoop = false; 739 bool Changed = simplifyLoopCFG( 740 *L, DT, LI, SE, MSSAU.hasValue() ? MSSAU.getPointer() : nullptr, 741 DeleteCurrentLoop); 742 if (DeleteCurrentLoop) 743 LPM.markLoopAsDeleted(*L); 744 return Changed; 745 } 746 747 void getAnalysisUsage(AnalysisUsage &AU) const override { 748 if (EnableMSSALoopDependency) { 749 AU.addRequired<MemorySSAWrapperPass>(); 750 AU.addPreserved<MemorySSAWrapperPass>(); 751 } 752 AU.addPreserved<DependenceAnalysisWrapperPass>(); 753 getLoopAnalysisUsage(AU); 754 } 755 }; 756 } // end namespace 757 758 char LoopSimplifyCFGLegacyPass::ID = 0; 759 INITIALIZE_PASS_BEGIN(LoopSimplifyCFGLegacyPass, "loop-simplifycfg", 760 "Simplify loop CFG", false, false) 761 INITIALIZE_PASS_DEPENDENCY(LoopPass) 762 INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass) 763 INITIALIZE_PASS_END(LoopSimplifyCFGLegacyPass, "loop-simplifycfg", 764 "Simplify loop CFG", false, false) 765 766 Pass *llvm::createLoopSimplifyCFGPass() { 767 return new LoopSimplifyCFGLegacyPass(); 768 } 769