10b57cec5SDimitry Andric //===--------- LoopSimplifyCFG.cpp - Loop CFG Simplification Pass ---------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements the Loop SimplifyCFG Pass. This pass is responsible for 100b57cec5SDimitry Andric // basic loop CFG cleanup, primarily to assist other loop passes. If you 110b57cec5SDimitry Andric // encounter a noncanonical CFG construct that causes another loop pass to 120b57cec5SDimitry Andric // perform suboptimally, this is the place to fix it up. 130b57cec5SDimitry Andric // 140b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h" 170b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 180b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 190b57cec5SDimitry Andric #include "llvm/Analysis/DomTreeUpdater.h" 200b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfo.h" 215ffd83dbSDimitry Andric #include "llvm/Analysis/LoopIterator.h" 220b57cec5SDimitry Andric #include "llvm/Analysis/MemorySSA.h" 230b57cec5SDimitry Andric #include "llvm/Analysis/MemorySSAUpdater.h" 240b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolution.h" 250b57cec5SDimitry Andric #include "llvm/IR/Dominators.h" 265ffd83dbSDimitry Andric #include "llvm/IR/IRBuilder.h" 27480093f4SDimitry Andric #include "llvm/Support/CommandLine.h" 280b57cec5SDimitry Andric #include "llvm/Transforms/Scalar.h" 290b57cec5SDimitry Andric #include "llvm/Transforms/Scalar/LoopPassManager.h" 300b57cec5SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h" 310b57cec5SDimitry Andric #include "llvm/Transforms/Utils/LoopUtils.h" 32*bdd1243dSDimitry Andric #include <optional> 330b57cec5SDimitry Andric using namespace llvm; 340b57cec5SDimitry Andric 350b57cec5SDimitry Andric #define DEBUG_TYPE "loop-simplifycfg" 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric static cl::opt<bool> EnableTermFolding("enable-loop-simplifycfg-term-folding", 380b57cec5SDimitry Andric cl::init(true)); 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric STATISTIC(NumTerminatorsFolded, 410b57cec5SDimitry Andric "Number of terminators folded to unconditional branches"); 420b57cec5SDimitry Andric STATISTIC(NumLoopBlocksDeleted, 430b57cec5SDimitry Andric "Number of loop blocks deleted"); 440b57cec5SDimitry Andric STATISTIC(NumLoopExitsDeleted, 450b57cec5SDimitry Andric "Number of loop exiting edges deleted"); 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric /// If \p BB is a switch or a conditional branch, but only one of its successors 480b57cec5SDimitry Andric /// can be reached from this block in runtime, return this successor. Otherwise, 490b57cec5SDimitry Andric /// return nullptr. 500b57cec5SDimitry Andric static BasicBlock *getOnlyLiveSuccessor(BasicBlock *BB) { 510b57cec5SDimitry Andric Instruction *TI = BB->getTerminator(); 520b57cec5SDimitry Andric if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { 530b57cec5SDimitry Andric if (BI->isUnconditional()) 540b57cec5SDimitry Andric return nullptr; 550b57cec5SDimitry Andric if (BI->getSuccessor(0) == BI->getSuccessor(1)) 560b57cec5SDimitry Andric return BI->getSuccessor(0); 570b57cec5SDimitry Andric ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition()); 580b57cec5SDimitry Andric if (!Cond) 590b57cec5SDimitry Andric return nullptr; 600b57cec5SDimitry Andric return Cond->isZero() ? BI->getSuccessor(1) : BI->getSuccessor(0); 610b57cec5SDimitry Andric } 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { 640b57cec5SDimitry Andric auto *CI = dyn_cast<ConstantInt>(SI->getCondition()); 650b57cec5SDimitry Andric if (!CI) 660b57cec5SDimitry Andric return nullptr; 670b57cec5SDimitry Andric for (auto Case : SI->cases()) 680b57cec5SDimitry Andric if (Case.getCaseValue() == CI) 690b57cec5SDimitry Andric return Case.getCaseSuccessor(); 700b57cec5SDimitry Andric return SI->getDefaultDest(); 710b57cec5SDimitry Andric } 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric return nullptr; 740b57cec5SDimitry Andric } 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric /// Removes \p BB from all loops from [FirstLoop, LastLoop) in parent chain. 770b57cec5SDimitry Andric static void removeBlockFromLoops(BasicBlock *BB, Loop *FirstLoop, 780b57cec5SDimitry Andric Loop *LastLoop = nullptr) { 790b57cec5SDimitry Andric assert((!LastLoop || LastLoop->contains(FirstLoop->getHeader())) && 800b57cec5SDimitry Andric "First loop is supposed to be inside of last loop!"); 810b57cec5SDimitry Andric assert(FirstLoop->contains(BB) && "Must be a loop block!"); 820b57cec5SDimitry Andric for (Loop *Current = FirstLoop; Current != LastLoop; 830b57cec5SDimitry Andric Current = Current->getParentLoop()) 840b57cec5SDimitry Andric Current->removeBlockFromLoop(BB); 850b57cec5SDimitry Andric } 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric /// Find innermost loop that contains at least one block from \p BBs and 880b57cec5SDimitry Andric /// contains the header of loop \p L. 890b57cec5SDimitry Andric static Loop *getInnermostLoopFor(SmallPtrSetImpl<BasicBlock *> &BBs, 900b57cec5SDimitry Andric Loop &L, LoopInfo &LI) { 910b57cec5SDimitry Andric Loop *Innermost = nullptr; 920b57cec5SDimitry Andric for (BasicBlock *BB : BBs) { 930b57cec5SDimitry Andric Loop *BBL = LI.getLoopFor(BB); 940b57cec5SDimitry Andric while (BBL && !BBL->contains(L.getHeader())) 950b57cec5SDimitry Andric BBL = BBL->getParentLoop(); 960b57cec5SDimitry Andric if (BBL == &L) 970b57cec5SDimitry Andric BBL = BBL->getParentLoop(); 980b57cec5SDimitry Andric if (!BBL) 990b57cec5SDimitry Andric continue; 1000b57cec5SDimitry Andric if (!Innermost || BBL->getLoopDepth() > Innermost->getLoopDepth()) 1010b57cec5SDimitry Andric Innermost = BBL; 1020b57cec5SDimitry Andric } 1030b57cec5SDimitry Andric return Innermost; 1040b57cec5SDimitry Andric } 1050b57cec5SDimitry Andric 1060b57cec5SDimitry Andric namespace { 1070b57cec5SDimitry Andric /// Helper class that can turn branches and switches with constant conditions 1080b57cec5SDimitry Andric /// into unconditional branches. 1090b57cec5SDimitry Andric class ConstantTerminatorFoldingImpl { 1100b57cec5SDimitry Andric private: 1110b57cec5SDimitry Andric Loop &L; 1120b57cec5SDimitry Andric LoopInfo &LI; 1130b57cec5SDimitry Andric DominatorTree &DT; 1140b57cec5SDimitry Andric ScalarEvolution &SE; 1150b57cec5SDimitry Andric MemorySSAUpdater *MSSAU; 1160b57cec5SDimitry Andric LoopBlocksDFS DFS; 1170b57cec5SDimitry Andric DomTreeUpdater DTU; 1180b57cec5SDimitry Andric SmallVector<DominatorTree::UpdateType, 16> DTUpdates; 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric // Whether or not the current loop has irreducible CFG. 1210b57cec5SDimitry Andric bool HasIrreducibleCFG = false; 1220b57cec5SDimitry Andric // Whether or not the current loop will still exist after terminator constant 1230b57cec5SDimitry Andric // folding will be done. In theory, there are two ways how it can happen: 1240b57cec5SDimitry Andric // 1. Loop's latch(es) become unreachable from loop header; 1250b57cec5SDimitry Andric // 2. Loop's header becomes unreachable from method entry. 1260b57cec5SDimitry Andric // In practice, the second situation is impossible because we only modify the 1270b57cec5SDimitry Andric // current loop and its preheader and do not affect preheader's reachibility 1280b57cec5SDimitry Andric // from any other block. So this variable set to true means that loop's latch 1290b57cec5SDimitry Andric // has become unreachable from loop header. 1300b57cec5SDimitry Andric bool DeleteCurrentLoop = false; 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric // The blocks of the original loop that will still be reachable from entry 1330b57cec5SDimitry Andric // after the constant folding. 1340b57cec5SDimitry Andric SmallPtrSet<BasicBlock *, 8> LiveLoopBlocks; 1350b57cec5SDimitry Andric // The blocks of the original loop that will become unreachable from entry 1360b57cec5SDimitry Andric // after the constant folding. 1370b57cec5SDimitry Andric SmallVector<BasicBlock *, 8> DeadLoopBlocks; 1380b57cec5SDimitry Andric // The exits of the original loop that will still be reachable from entry 1390b57cec5SDimitry Andric // after the constant folding. 1400b57cec5SDimitry Andric SmallPtrSet<BasicBlock *, 8> LiveExitBlocks; 1410b57cec5SDimitry Andric // The exits of the original loop that will become unreachable from entry 1420b57cec5SDimitry Andric // after the constant folding. 1430b57cec5SDimitry Andric SmallVector<BasicBlock *, 8> DeadExitBlocks; 1440b57cec5SDimitry Andric // The blocks that will still be a part of the current loop after folding. 1450b57cec5SDimitry Andric SmallPtrSet<BasicBlock *, 8> BlocksInLoopAfterFolding; 1460b57cec5SDimitry Andric // The blocks that have terminators with constant condition that can be 1470b57cec5SDimitry Andric // folded. Note: fold candidates should be in L but not in any of its 1480b57cec5SDimitry Andric // subloops to avoid complex LI updates. 1490b57cec5SDimitry Andric SmallVector<BasicBlock *, 8> FoldCandidates; 1500b57cec5SDimitry Andric 1510b57cec5SDimitry Andric void dump() const { 1520b57cec5SDimitry Andric dbgs() << "Constant terminator folding for loop " << L << "\n"; 1530b57cec5SDimitry Andric dbgs() << "After terminator constant-folding, the loop will"; 1540b57cec5SDimitry Andric if (!DeleteCurrentLoop) 1550b57cec5SDimitry Andric dbgs() << " not"; 1560b57cec5SDimitry Andric dbgs() << " be destroyed\n"; 1570b57cec5SDimitry Andric auto PrintOutVector = [&](const char *Message, 1580b57cec5SDimitry Andric const SmallVectorImpl<BasicBlock *> &S) { 1590b57cec5SDimitry Andric dbgs() << Message << "\n"; 1600b57cec5SDimitry Andric for (const BasicBlock *BB : S) 1610b57cec5SDimitry Andric dbgs() << "\t" << BB->getName() << "\n"; 1620b57cec5SDimitry Andric }; 1630b57cec5SDimitry Andric auto PrintOutSet = [&](const char *Message, 1640b57cec5SDimitry Andric const SmallPtrSetImpl<BasicBlock *> &S) { 1650b57cec5SDimitry Andric dbgs() << Message << "\n"; 1660b57cec5SDimitry Andric for (const BasicBlock *BB : S) 1670b57cec5SDimitry Andric dbgs() << "\t" << BB->getName() << "\n"; 1680b57cec5SDimitry Andric }; 1690b57cec5SDimitry Andric PrintOutVector("Blocks in which we can constant-fold terminator:", 1700b57cec5SDimitry Andric FoldCandidates); 1710b57cec5SDimitry Andric PrintOutSet("Live blocks from the original loop:", LiveLoopBlocks); 1720b57cec5SDimitry Andric PrintOutVector("Dead blocks from the original loop:", DeadLoopBlocks); 1730b57cec5SDimitry Andric PrintOutSet("Live exit blocks:", LiveExitBlocks); 1740b57cec5SDimitry Andric PrintOutVector("Dead exit blocks:", DeadExitBlocks); 1750b57cec5SDimitry Andric if (!DeleteCurrentLoop) 1760b57cec5SDimitry Andric PrintOutSet("The following blocks will still be part of the loop:", 1770b57cec5SDimitry Andric BlocksInLoopAfterFolding); 1780b57cec5SDimitry Andric } 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric /// Whether or not the current loop has irreducible CFG. 1810b57cec5SDimitry Andric bool hasIrreducibleCFG(LoopBlocksDFS &DFS) { 1820b57cec5SDimitry Andric assert(DFS.isComplete() && "DFS is expected to be finished"); 1830b57cec5SDimitry Andric // Index of a basic block in RPO traversal. 1840b57cec5SDimitry Andric DenseMap<const BasicBlock *, unsigned> RPO; 1850b57cec5SDimitry Andric unsigned Current = 0; 1860b57cec5SDimitry Andric for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) 1870b57cec5SDimitry Andric RPO[*I] = Current++; 1880b57cec5SDimitry Andric 1890b57cec5SDimitry Andric for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) { 1900b57cec5SDimitry Andric BasicBlock *BB = *I; 1910b57cec5SDimitry Andric for (auto *Succ : successors(BB)) 1920b57cec5SDimitry Andric if (L.contains(Succ) && !LI.isLoopHeader(Succ) && RPO[BB] > RPO[Succ]) 1930b57cec5SDimitry Andric // If an edge goes from a block with greater order number into a block 1940b57cec5SDimitry Andric // with lesses number, and it is not a loop backedge, then it can only 1950b57cec5SDimitry Andric // be a part of irreducible non-loop cycle. 1960b57cec5SDimitry Andric return true; 1970b57cec5SDimitry Andric } 1980b57cec5SDimitry Andric return false; 1990b57cec5SDimitry Andric } 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric /// Fill all information about status of blocks and exits of the current loop 2020b57cec5SDimitry Andric /// if constant folding of all branches will be done. 2030b57cec5SDimitry Andric void analyze() { 2040b57cec5SDimitry Andric DFS.perform(&LI); 2050b57cec5SDimitry Andric assert(DFS.isComplete() && "DFS is expected to be finished"); 2060b57cec5SDimitry Andric 2070b57cec5SDimitry Andric // TODO: The algorithm below relies on both RPO and Postorder traversals. 2080b57cec5SDimitry Andric // When the loop has only reducible CFG inside, then the invariant "all 2090b57cec5SDimitry Andric // predecessors of X are processed before X in RPO" is preserved. However 2100b57cec5SDimitry Andric // an irreducible loop can break this invariant (e.g. latch does not have to 2110b57cec5SDimitry Andric // be the last block in the traversal in this case, and the algorithm relies 2120b57cec5SDimitry Andric // on this). We can later decide to support such cases by altering the 2130b57cec5SDimitry Andric // algorithms, but so far we just give up analyzing them. 2140b57cec5SDimitry Andric if (hasIrreducibleCFG(DFS)) { 2150b57cec5SDimitry Andric HasIrreducibleCFG = true; 2160b57cec5SDimitry Andric return; 2170b57cec5SDimitry Andric } 2180b57cec5SDimitry Andric 2190b57cec5SDimitry Andric // Collect live and dead loop blocks and exits. 2200b57cec5SDimitry Andric LiveLoopBlocks.insert(L.getHeader()); 2210b57cec5SDimitry Andric for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) { 2220b57cec5SDimitry Andric BasicBlock *BB = *I; 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric // If a loop block wasn't marked as live so far, then it's dead. 2250b57cec5SDimitry Andric if (!LiveLoopBlocks.count(BB)) { 2260b57cec5SDimitry Andric DeadLoopBlocks.push_back(BB); 2270b57cec5SDimitry Andric continue; 2280b57cec5SDimitry Andric } 2290b57cec5SDimitry Andric 2300b57cec5SDimitry Andric BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(BB); 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric // If a block has only one live successor, it's a candidate on constant 2330b57cec5SDimitry Andric // folding. Only handle blocks from current loop: branches in child loops 2340b57cec5SDimitry Andric // are skipped because if they can be folded, they should be folded during 2350b57cec5SDimitry Andric // the processing of child loops. 2360b57cec5SDimitry Andric bool TakeFoldCandidate = TheOnlySucc && LI.getLoopFor(BB) == &L; 2370b57cec5SDimitry Andric if (TakeFoldCandidate) 2380b57cec5SDimitry Andric FoldCandidates.push_back(BB); 2390b57cec5SDimitry Andric 2400b57cec5SDimitry Andric // Handle successors. 2410b57cec5SDimitry Andric for (BasicBlock *Succ : successors(BB)) 2420b57cec5SDimitry Andric if (!TakeFoldCandidate || TheOnlySucc == Succ) { 2430b57cec5SDimitry Andric if (L.contains(Succ)) 2440b57cec5SDimitry Andric LiveLoopBlocks.insert(Succ); 2450b57cec5SDimitry Andric else 2460b57cec5SDimitry Andric LiveExitBlocks.insert(Succ); 2470b57cec5SDimitry Andric } 2480b57cec5SDimitry Andric } 2490b57cec5SDimitry Andric 2504824e7fdSDimitry Andric // Amount of dead and live loop blocks should match the total number of 2514824e7fdSDimitry Andric // blocks in loop. 2520b57cec5SDimitry Andric assert(L.getNumBlocks() == LiveLoopBlocks.size() + DeadLoopBlocks.size() && 2530b57cec5SDimitry Andric "Malformed block sets?"); 2540b57cec5SDimitry Andric 25581ad6265SDimitry Andric // Now, all exit blocks that are not marked as live are dead, if all their 25681ad6265SDimitry Andric // predecessors are in the loop. This may not be the case, as the input loop 25781ad6265SDimitry Andric // may not by in loop-simplify/canonical form. 2580b57cec5SDimitry Andric SmallVector<BasicBlock *, 8> ExitBlocks; 2590b57cec5SDimitry Andric L.getExitBlocks(ExitBlocks); 2600b57cec5SDimitry Andric SmallPtrSet<BasicBlock *, 8> UniqueDeadExits; 2610b57cec5SDimitry Andric for (auto *ExitBlock : ExitBlocks) 2620b57cec5SDimitry Andric if (!LiveExitBlocks.count(ExitBlock) && 26381ad6265SDimitry Andric UniqueDeadExits.insert(ExitBlock).second && 26481ad6265SDimitry Andric all_of(predecessors(ExitBlock), 26581ad6265SDimitry Andric [this](BasicBlock *Pred) { return L.contains(Pred); })) 2660b57cec5SDimitry Andric DeadExitBlocks.push_back(ExitBlock); 2670b57cec5SDimitry Andric 2680b57cec5SDimitry Andric // Whether or not the edge From->To will still be present in graph after the 2690b57cec5SDimitry Andric // folding. 2700b57cec5SDimitry Andric auto IsEdgeLive = [&](BasicBlock *From, BasicBlock *To) { 2710b57cec5SDimitry Andric if (!LiveLoopBlocks.count(From)) 2720b57cec5SDimitry Andric return false; 2730b57cec5SDimitry Andric BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(From); 2740b57cec5SDimitry Andric return !TheOnlySucc || TheOnlySucc == To || LI.getLoopFor(From) != &L; 2750b57cec5SDimitry Andric }; 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric // The loop will not be destroyed if its latch is live. 2780b57cec5SDimitry Andric DeleteCurrentLoop = !IsEdgeLive(L.getLoopLatch(), L.getHeader()); 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric // If we are going to delete the current loop completely, no extra analysis 2810b57cec5SDimitry Andric // is needed. 2820b57cec5SDimitry Andric if (DeleteCurrentLoop) 2830b57cec5SDimitry Andric return; 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric // Otherwise, we should check which blocks will still be a part of the 2860b57cec5SDimitry Andric // current loop after the transform. 2870b57cec5SDimitry Andric BlocksInLoopAfterFolding.insert(L.getLoopLatch()); 2880b57cec5SDimitry Andric // If the loop is live, then we should compute what blocks are still in 2890b57cec5SDimitry Andric // loop after all branch folding has been done. A block is in loop if 2900b57cec5SDimitry Andric // it has a live edge to another block that is in the loop; by definition, 2910b57cec5SDimitry Andric // latch is in the loop. 2920b57cec5SDimitry Andric auto BlockIsInLoop = [&](BasicBlock *BB) { 2930b57cec5SDimitry Andric return any_of(successors(BB), [&](BasicBlock *Succ) { 2940b57cec5SDimitry Andric return BlocksInLoopAfterFolding.count(Succ) && IsEdgeLive(BB, Succ); 2950b57cec5SDimitry Andric }); 2960b57cec5SDimitry Andric }; 2970b57cec5SDimitry Andric for (auto I = DFS.beginPostorder(), E = DFS.endPostorder(); I != E; ++I) { 2980b57cec5SDimitry Andric BasicBlock *BB = *I; 2990b57cec5SDimitry Andric if (BlockIsInLoop(BB)) 3000b57cec5SDimitry Andric BlocksInLoopAfterFolding.insert(BB); 3010b57cec5SDimitry Andric } 3020b57cec5SDimitry Andric 3030b57cec5SDimitry Andric assert(BlocksInLoopAfterFolding.count(L.getHeader()) && 3040b57cec5SDimitry Andric "Header not in loop?"); 3050b57cec5SDimitry Andric assert(BlocksInLoopAfterFolding.size() <= LiveLoopBlocks.size() && 3060b57cec5SDimitry Andric "All blocks that stay in loop should be live!"); 3070b57cec5SDimitry Andric } 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric /// We need to preserve static reachibility of all loop exit blocks (this is) 3100b57cec5SDimitry Andric /// required by loop pass manager. In order to do it, we make the following 3110b57cec5SDimitry Andric /// trick: 3120b57cec5SDimitry Andric /// 3130b57cec5SDimitry Andric /// preheader: 3140b57cec5SDimitry Andric /// <preheader code> 3150b57cec5SDimitry Andric /// br label %loop_header 3160b57cec5SDimitry Andric /// 3170b57cec5SDimitry Andric /// loop_header: 3180b57cec5SDimitry Andric /// ... 3190b57cec5SDimitry Andric /// br i1 false, label %dead_exit, label %loop_block 3200b57cec5SDimitry Andric /// ... 3210b57cec5SDimitry Andric /// 3220b57cec5SDimitry Andric /// We cannot simply remove edge from the loop to dead exit because in this 3230b57cec5SDimitry Andric /// case dead_exit (and its successors) may become unreachable. To avoid that, 3240b57cec5SDimitry Andric /// we insert the following fictive preheader: 3250b57cec5SDimitry Andric /// 3260b57cec5SDimitry Andric /// preheader: 3270b57cec5SDimitry Andric /// <preheader code> 3280b57cec5SDimitry Andric /// switch i32 0, label %preheader-split, 3290b57cec5SDimitry Andric /// [i32 1, label %dead_exit_1], 3300b57cec5SDimitry Andric /// [i32 2, label %dead_exit_2], 3310b57cec5SDimitry Andric /// ... 3320b57cec5SDimitry Andric /// [i32 N, label %dead_exit_N], 3330b57cec5SDimitry Andric /// 3340b57cec5SDimitry Andric /// preheader-split: 3350b57cec5SDimitry Andric /// br label %loop_header 3360b57cec5SDimitry Andric /// 3370b57cec5SDimitry Andric /// loop_header: 3380b57cec5SDimitry Andric /// ... 3390b57cec5SDimitry Andric /// br i1 false, label %dead_exit_N, label %loop_block 3400b57cec5SDimitry Andric /// ... 3410b57cec5SDimitry Andric /// 3420b57cec5SDimitry Andric /// Doing so, we preserve static reachibility of all dead exits and can later 3430b57cec5SDimitry Andric /// remove edges from the loop to these blocks. 3440b57cec5SDimitry Andric void handleDeadExits() { 3450b57cec5SDimitry Andric // If no dead exits, nothing to do. 3460b57cec5SDimitry Andric if (DeadExitBlocks.empty()) 3470b57cec5SDimitry Andric return; 3480b57cec5SDimitry Andric 3490b57cec5SDimitry Andric // Construct split preheader and the dummy switch to thread edges from it to 3500b57cec5SDimitry Andric // dead exits. 3510b57cec5SDimitry Andric BasicBlock *Preheader = L.getLoopPreheader(); 3520b57cec5SDimitry Andric BasicBlock *NewPreheader = llvm::SplitBlock( 3530b57cec5SDimitry Andric Preheader, Preheader->getTerminator(), &DT, &LI, MSSAU); 3540b57cec5SDimitry Andric 3550b57cec5SDimitry Andric IRBuilder<> Builder(Preheader->getTerminator()); 3560b57cec5SDimitry Andric SwitchInst *DummySwitch = 3570b57cec5SDimitry Andric Builder.CreateSwitch(Builder.getInt32(0), NewPreheader); 3580b57cec5SDimitry Andric Preheader->getTerminator()->eraseFromParent(); 3590b57cec5SDimitry Andric 3600b57cec5SDimitry Andric unsigned DummyIdx = 1; 3610b57cec5SDimitry Andric for (BasicBlock *BB : DeadExitBlocks) { 362e8d8bef9SDimitry Andric // Eliminate all Phis and LandingPads from dead exits. 363e8d8bef9SDimitry Andric // TODO: Consider removing all instructions in this dead block. 364e8d8bef9SDimitry Andric SmallVector<Instruction *, 4> DeadInstructions; 3650b57cec5SDimitry Andric for (auto &PN : BB->phis()) 366e8d8bef9SDimitry Andric DeadInstructions.push_back(&PN); 3670b57cec5SDimitry Andric 368e8d8bef9SDimitry Andric if (auto *LandingPad = dyn_cast<LandingPadInst>(BB->getFirstNonPHI())) 369e8d8bef9SDimitry Andric DeadInstructions.emplace_back(LandingPad); 370e8d8bef9SDimitry Andric 371e8d8bef9SDimitry Andric for (Instruction *I : DeadInstructions) { 372*bdd1243dSDimitry Andric SE.forgetBlockAndLoopDispositions(I); 37381ad6265SDimitry Andric I->replaceAllUsesWith(PoisonValue::get(I->getType())); 374e8d8bef9SDimitry Andric I->eraseFromParent(); 3750b57cec5SDimitry Andric } 376e8d8bef9SDimitry Andric 3770b57cec5SDimitry Andric assert(DummyIdx != 0 && "Too many dead exits!"); 3780b57cec5SDimitry Andric DummySwitch->addCase(Builder.getInt32(DummyIdx++), BB); 3790b57cec5SDimitry Andric DTUpdates.push_back({DominatorTree::Insert, Preheader, BB}); 3800b57cec5SDimitry Andric ++NumLoopExitsDeleted; 3810b57cec5SDimitry Andric } 3820b57cec5SDimitry Andric 3830b57cec5SDimitry Andric assert(L.getLoopPreheader() == NewPreheader && "Malformed CFG?"); 3840b57cec5SDimitry Andric if (Loop *OuterLoop = LI.getLoopFor(Preheader)) { 3850b57cec5SDimitry Andric // When we break dead edges, the outer loop may become unreachable from 3860b57cec5SDimitry Andric // the current loop. We need to fix loop info accordingly. For this, we 3870b57cec5SDimitry Andric // find the most nested loop that still contains L and remove L from all 3880b57cec5SDimitry Andric // loops that are inside of it. 3890b57cec5SDimitry Andric Loop *StillReachable = getInnermostLoopFor(LiveExitBlocks, L, LI); 3900b57cec5SDimitry Andric 3910b57cec5SDimitry Andric // Okay, our loop is no longer in the outer loop (and maybe not in some of 3920b57cec5SDimitry Andric // its parents as well). Make the fixup. 3930b57cec5SDimitry Andric if (StillReachable != OuterLoop) { 3940b57cec5SDimitry Andric LI.changeLoopFor(NewPreheader, StillReachable); 3950b57cec5SDimitry Andric removeBlockFromLoops(NewPreheader, OuterLoop, StillReachable); 3960b57cec5SDimitry Andric for (auto *BB : L.blocks()) 3970b57cec5SDimitry Andric removeBlockFromLoops(BB, OuterLoop, StillReachable); 3980b57cec5SDimitry Andric OuterLoop->removeChildLoop(&L); 3990b57cec5SDimitry Andric if (StillReachable) 4000b57cec5SDimitry Andric StillReachable->addChildLoop(&L); 4010b57cec5SDimitry Andric else 4020b57cec5SDimitry Andric LI.addTopLevelLoop(&L); 4030b57cec5SDimitry Andric 4040b57cec5SDimitry Andric // Some values from loops in [OuterLoop, StillReachable) could be used 4050b57cec5SDimitry Andric // in the current loop. Now it is not their child anymore, so such uses 4060b57cec5SDimitry Andric // require LCSSA Phis. 4070b57cec5SDimitry Andric Loop *FixLCSSALoop = OuterLoop; 4080b57cec5SDimitry Andric while (FixLCSSALoop->getParentLoop() != StillReachable) 4090b57cec5SDimitry Andric FixLCSSALoop = FixLCSSALoop->getParentLoop(); 4100b57cec5SDimitry Andric assert(FixLCSSALoop && "Should be a loop!"); 4110b57cec5SDimitry Andric // We need all DT updates to be done before forming LCSSA. 4120b57cec5SDimitry Andric if (MSSAU) 413e8d8bef9SDimitry Andric MSSAU->applyUpdates(DTUpdates, DT, /*UpdateDT=*/true); 414e8d8bef9SDimitry Andric else 415e8d8bef9SDimitry Andric DTU.applyUpdates(DTUpdates); 4160b57cec5SDimitry Andric DTUpdates.clear(); 4170b57cec5SDimitry Andric formLCSSARecursively(*FixLCSSALoop, DT, &LI, &SE); 418*bdd1243dSDimitry Andric SE.forgetBlockAndLoopDispositions(); 4190b57cec5SDimitry Andric } 4200b57cec5SDimitry Andric } 4210b57cec5SDimitry Andric 4220b57cec5SDimitry Andric if (MSSAU) { 4230b57cec5SDimitry Andric // Clear all updates now. Facilitates deletes that follow. 424e8d8bef9SDimitry Andric MSSAU->applyUpdates(DTUpdates, DT, /*UpdateDT=*/true); 4250b57cec5SDimitry Andric DTUpdates.clear(); 4260b57cec5SDimitry Andric if (VerifyMemorySSA) 4270b57cec5SDimitry Andric MSSAU->getMemorySSA()->verifyMemorySSA(); 4280b57cec5SDimitry Andric } 4290b57cec5SDimitry Andric } 4300b57cec5SDimitry Andric 4310b57cec5SDimitry Andric /// Delete loop blocks that have become unreachable after folding. Make all 4320b57cec5SDimitry Andric /// relevant updates to DT and LI. 4330b57cec5SDimitry Andric void deleteDeadLoopBlocks() { 4340b57cec5SDimitry Andric if (MSSAU) { 4350b57cec5SDimitry Andric SmallSetVector<BasicBlock *, 8> DeadLoopBlocksSet(DeadLoopBlocks.begin(), 4360b57cec5SDimitry Andric DeadLoopBlocks.end()); 4370b57cec5SDimitry Andric MSSAU->removeBlocks(DeadLoopBlocksSet); 4380b57cec5SDimitry Andric } 4390b57cec5SDimitry Andric 4400b57cec5SDimitry Andric // The function LI.erase has some invariants that need to be preserved when 4410b57cec5SDimitry Andric // it tries to remove a loop which is not the top-level loop. In particular, 4420b57cec5SDimitry Andric // it requires loop's preheader to be strictly in loop's parent. We cannot 4430b57cec5SDimitry Andric // just remove blocks one by one, because after removal of preheader we may 4440b57cec5SDimitry Andric // break this invariant for the dead loop. So we detatch and erase all dead 4450b57cec5SDimitry Andric // loops beforehand. 4460b57cec5SDimitry Andric for (auto *BB : DeadLoopBlocks) 4470b57cec5SDimitry Andric if (LI.isLoopHeader(BB)) { 4480b57cec5SDimitry Andric assert(LI.getLoopFor(BB) != &L && "Attempt to remove current loop!"); 4490b57cec5SDimitry Andric Loop *DL = LI.getLoopFor(BB); 450e8d8bef9SDimitry Andric if (!DL->isOutermost()) { 4510b57cec5SDimitry Andric for (auto *PL = DL->getParentLoop(); PL; PL = PL->getParentLoop()) 4520b57cec5SDimitry Andric for (auto *BB : DL->getBlocks()) 4530b57cec5SDimitry Andric PL->removeBlockFromLoop(BB); 4540b57cec5SDimitry Andric DL->getParentLoop()->removeChildLoop(DL); 4550b57cec5SDimitry Andric LI.addTopLevelLoop(DL); 4560b57cec5SDimitry Andric } 4570b57cec5SDimitry Andric LI.erase(DL); 4580b57cec5SDimitry Andric } 4590b57cec5SDimitry Andric 4600b57cec5SDimitry Andric for (auto *BB : DeadLoopBlocks) { 4610b57cec5SDimitry Andric assert(BB != L.getHeader() && 4620b57cec5SDimitry Andric "Header of the current loop cannot be dead!"); 4630b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Deleting dead loop block " << BB->getName() 4640b57cec5SDimitry Andric << "\n"); 4650b57cec5SDimitry Andric LI.removeBlock(BB); 4660b57cec5SDimitry Andric } 4670b57cec5SDimitry Andric 4681fd87a68SDimitry Andric detachDeadBlocks(DeadLoopBlocks, &DTUpdates, /*KeepOneInputPHIs*/true); 4690b57cec5SDimitry Andric DTU.applyUpdates(DTUpdates); 4700b57cec5SDimitry Andric DTUpdates.clear(); 4710b57cec5SDimitry Andric for (auto *BB : DeadLoopBlocks) 4720b57cec5SDimitry Andric DTU.deleteBB(BB); 4730b57cec5SDimitry Andric 4740b57cec5SDimitry Andric NumLoopBlocksDeleted += DeadLoopBlocks.size(); 4750b57cec5SDimitry Andric } 4760b57cec5SDimitry Andric 477*bdd1243dSDimitry Andric /// Constant-fold terminators of blocks accumulated in FoldCandidates into the 4780b57cec5SDimitry Andric /// unconditional branches. 4790b57cec5SDimitry Andric void foldTerminators() { 4800b57cec5SDimitry Andric for (BasicBlock *BB : FoldCandidates) { 4810b57cec5SDimitry Andric assert(LI.getLoopFor(BB) == &L && "Should be a loop block!"); 4820b57cec5SDimitry Andric BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(BB); 4830b57cec5SDimitry Andric assert(TheOnlySucc && "Should have one live successor!"); 4840b57cec5SDimitry Andric 4850b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Replacing terminator of " << BB->getName() 4860b57cec5SDimitry Andric << " with an unconditional branch to the block " 4870b57cec5SDimitry Andric << TheOnlySucc->getName() << "\n"); 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric SmallPtrSet<BasicBlock *, 2> DeadSuccessors; 4900b57cec5SDimitry Andric // Remove all BB's successors except for the live one. 4910b57cec5SDimitry Andric unsigned TheOnlySuccDuplicates = 0; 4920b57cec5SDimitry Andric for (auto *Succ : successors(BB)) 4930b57cec5SDimitry Andric if (Succ != TheOnlySucc) { 4940b57cec5SDimitry Andric DeadSuccessors.insert(Succ); 4950b57cec5SDimitry Andric // If our successor lies in a different loop, we don't want to remove 4960b57cec5SDimitry Andric // the one-input Phi because it is a LCSSA Phi. 4970b57cec5SDimitry Andric bool PreserveLCSSAPhi = !L.contains(Succ); 4980b57cec5SDimitry Andric Succ->removePredecessor(BB, PreserveLCSSAPhi); 4990b57cec5SDimitry Andric if (MSSAU) 5000b57cec5SDimitry Andric MSSAU->removeEdge(BB, Succ); 5010b57cec5SDimitry Andric } else 5020b57cec5SDimitry Andric ++TheOnlySuccDuplicates; 5030b57cec5SDimitry Andric 5040b57cec5SDimitry Andric assert(TheOnlySuccDuplicates > 0 && "Should be!"); 5050b57cec5SDimitry Andric // If TheOnlySucc was BB's successor more than once, after transform it 5060b57cec5SDimitry Andric // will be its successor only once. Remove redundant inputs from 5070b57cec5SDimitry Andric // TheOnlySucc's Phis. 5080b57cec5SDimitry Andric bool PreserveLCSSAPhi = !L.contains(TheOnlySucc); 5090b57cec5SDimitry Andric for (unsigned Dup = 1; Dup < TheOnlySuccDuplicates; ++Dup) 5100b57cec5SDimitry Andric TheOnlySucc->removePredecessor(BB, PreserveLCSSAPhi); 5110b57cec5SDimitry Andric if (MSSAU && TheOnlySuccDuplicates > 1) 5120b57cec5SDimitry Andric MSSAU->removeDuplicatePhiEdgesBetween(BB, TheOnlySucc); 5130b57cec5SDimitry Andric 5140b57cec5SDimitry Andric IRBuilder<> Builder(BB->getContext()); 5150b57cec5SDimitry Andric Instruction *Term = BB->getTerminator(); 5160b57cec5SDimitry Andric Builder.SetInsertPoint(Term); 5170b57cec5SDimitry Andric Builder.CreateBr(TheOnlySucc); 5180b57cec5SDimitry Andric Term->eraseFromParent(); 5190b57cec5SDimitry Andric 5200b57cec5SDimitry Andric for (auto *DeadSucc : DeadSuccessors) 5210b57cec5SDimitry Andric DTUpdates.push_back({DominatorTree::Delete, BB, DeadSucc}); 5220b57cec5SDimitry Andric 5230b57cec5SDimitry Andric ++NumTerminatorsFolded; 5240b57cec5SDimitry Andric } 5250b57cec5SDimitry Andric } 5260b57cec5SDimitry Andric 5270b57cec5SDimitry Andric public: 5280b57cec5SDimitry Andric ConstantTerminatorFoldingImpl(Loop &L, LoopInfo &LI, DominatorTree &DT, 5290b57cec5SDimitry Andric ScalarEvolution &SE, 5300b57cec5SDimitry Andric MemorySSAUpdater *MSSAU) 5310b57cec5SDimitry Andric : L(L), LI(LI), DT(DT), SE(SE), MSSAU(MSSAU), DFS(&L), 5320b57cec5SDimitry Andric DTU(DT, DomTreeUpdater::UpdateStrategy::Eager) {} 5330b57cec5SDimitry Andric bool run() { 5340b57cec5SDimitry Andric assert(L.getLoopLatch() && "Should be single latch!"); 5350b57cec5SDimitry Andric 5360b57cec5SDimitry Andric // Collect all available information about status of blocks after constant 5370b57cec5SDimitry Andric // folding. 5380b57cec5SDimitry Andric analyze(); 5390b57cec5SDimitry Andric BasicBlock *Header = L.getHeader(); 5400b57cec5SDimitry Andric (void)Header; 5410b57cec5SDimitry Andric 5420b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "In function " << Header->getParent()->getName() 5430b57cec5SDimitry Andric << ": "); 5440b57cec5SDimitry Andric 5450b57cec5SDimitry Andric if (HasIrreducibleCFG) { 5460b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Loops with irreducible CFG are not supported!\n"); 5470b57cec5SDimitry Andric return false; 5480b57cec5SDimitry Andric } 5490b57cec5SDimitry Andric 5500b57cec5SDimitry Andric // Nothing to constant-fold. 5510b57cec5SDimitry Andric if (FoldCandidates.empty()) { 5520b57cec5SDimitry Andric LLVM_DEBUG( 5530b57cec5SDimitry Andric dbgs() << "No constant terminator folding candidates found in loop " 5540b57cec5SDimitry Andric << Header->getName() << "\n"); 5550b57cec5SDimitry Andric return false; 5560b57cec5SDimitry Andric } 5570b57cec5SDimitry Andric 5580b57cec5SDimitry Andric // TODO: Support deletion of the current loop. 5590b57cec5SDimitry Andric if (DeleteCurrentLoop) { 5600b57cec5SDimitry Andric LLVM_DEBUG( 5610b57cec5SDimitry Andric dbgs() 5620b57cec5SDimitry Andric << "Give up constant terminator folding in loop " << Header->getName() 5630b57cec5SDimitry Andric << ": we don't currently support deletion of the current loop.\n"); 5640b57cec5SDimitry Andric return false; 5650b57cec5SDimitry Andric } 5660b57cec5SDimitry Andric 5670b57cec5SDimitry Andric // TODO: Support blocks that are not dead, but also not in loop after the 5680b57cec5SDimitry Andric // folding. 5690b57cec5SDimitry Andric if (BlocksInLoopAfterFolding.size() + DeadLoopBlocks.size() != 5700b57cec5SDimitry Andric L.getNumBlocks()) { 5710b57cec5SDimitry Andric LLVM_DEBUG( 5720b57cec5SDimitry Andric dbgs() << "Give up constant terminator folding in loop " 5730b57cec5SDimitry Andric << Header->getName() << ": we don't currently" 5740b57cec5SDimitry Andric " support blocks that are not dead, but will stop " 5750b57cec5SDimitry Andric "being a part of the loop after constant-folding.\n"); 5760b57cec5SDimitry Andric return false; 5770b57cec5SDimitry Andric } 5780b57cec5SDimitry Andric 579fcaf7f86SDimitry Andric // TODO: Tokens may breach LCSSA form by default. However, the transform for 580fcaf7f86SDimitry Andric // dead exit blocks requires LCSSA form to be maintained for all values, 581fcaf7f86SDimitry Andric // tokens included, otherwise it may break use-def dominance (see PR56243). 582fcaf7f86SDimitry Andric if (!DeadExitBlocks.empty() && !L.isLCSSAForm(DT, /*IgnoreTokens*/ false)) { 583fcaf7f86SDimitry Andric assert(L.isLCSSAForm(DT, /*IgnoreTokens*/ true) && 584fcaf7f86SDimitry Andric "LCSSA broken not by tokens?"); 585fcaf7f86SDimitry Andric LLVM_DEBUG(dbgs() << "Give up constant terminator folding in loop " 586fcaf7f86SDimitry Andric << Header->getName() 587fcaf7f86SDimitry Andric << ": tokens uses potentially break LCSSA form.\n"); 588fcaf7f86SDimitry Andric return false; 589fcaf7f86SDimitry Andric } 590fcaf7f86SDimitry Andric 5910b57cec5SDimitry Andric SE.forgetTopmostLoop(&L); 5920b57cec5SDimitry Andric // Dump analysis results. 5930b57cec5SDimitry Andric LLVM_DEBUG(dump()); 5940b57cec5SDimitry Andric 5950b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Constant-folding " << FoldCandidates.size() 5960b57cec5SDimitry Andric << " terminators in loop " << Header->getName() << "\n"); 5970b57cec5SDimitry Andric 598*bdd1243dSDimitry Andric if (!DeadLoopBlocks.empty()) 599*bdd1243dSDimitry Andric SE.forgetBlockAndLoopDispositions(); 600*bdd1243dSDimitry Andric 6010b57cec5SDimitry Andric // Make the actual transforms. 6020b57cec5SDimitry Andric handleDeadExits(); 6030b57cec5SDimitry Andric foldTerminators(); 6040b57cec5SDimitry Andric 6050b57cec5SDimitry Andric if (!DeadLoopBlocks.empty()) { 6060b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Deleting " << DeadLoopBlocks.size() 6070b57cec5SDimitry Andric << " dead blocks in loop " << Header->getName() << "\n"); 6080b57cec5SDimitry Andric deleteDeadLoopBlocks(); 6090b57cec5SDimitry Andric } else { 6100b57cec5SDimitry Andric // If we didn't do updates inside deleteDeadLoopBlocks, do them here. 6110b57cec5SDimitry Andric DTU.applyUpdates(DTUpdates); 6120b57cec5SDimitry Andric DTUpdates.clear(); 6130b57cec5SDimitry Andric } 6140b57cec5SDimitry Andric 6150b57cec5SDimitry Andric if (MSSAU && VerifyMemorySSA) 6160b57cec5SDimitry Andric MSSAU->getMemorySSA()->verifyMemorySSA(); 6170b57cec5SDimitry Andric 6180b57cec5SDimitry Andric #ifndef NDEBUG 6190b57cec5SDimitry Andric // Make sure that we have preserved all data structures after the transform. 6200b57cec5SDimitry Andric #if defined(EXPENSIVE_CHECKS) 6210b57cec5SDimitry Andric assert(DT.verify(DominatorTree::VerificationLevel::Full) && 6220b57cec5SDimitry Andric "DT broken after transform!"); 6230b57cec5SDimitry Andric #else 6240b57cec5SDimitry Andric assert(DT.verify(DominatorTree::VerificationLevel::Fast) && 6250b57cec5SDimitry Andric "DT broken after transform!"); 6260b57cec5SDimitry Andric #endif 6270b57cec5SDimitry Andric assert(DT.isReachableFromEntry(Header)); 6280b57cec5SDimitry Andric LI.verify(DT); 6290b57cec5SDimitry Andric #endif 6300b57cec5SDimitry Andric 6310b57cec5SDimitry Andric return true; 6320b57cec5SDimitry Andric } 6330b57cec5SDimitry Andric 6340b57cec5SDimitry Andric bool foldingBreaksCurrentLoop() const { 6350b57cec5SDimitry Andric return DeleteCurrentLoop; 6360b57cec5SDimitry Andric } 6370b57cec5SDimitry Andric }; 6380b57cec5SDimitry Andric } // namespace 6390b57cec5SDimitry Andric 6400b57cec5SDimitry Andric /// Turn branches and switches with known constant conditions into unconditional 6410b57cec5SDimitry Andric /// branches. 6420b57cec5SDimitry Andric static bool constantFoldTerminators(Loop &L, DominatorTree &DT, LoopInfo &LI, 6430b57cec5SDimitry Andric ScalarEvolution &SE, 6440b57cec5SDimitry Andric MemorySSAUpdater *MSSAU, 6450b57cec5SDimitry Andric bool &IsLoopDeleted) { 6460b57cec5SDimitry Andric if (!EnableTermFolding) 6470b57cec5SDimitry Andric return false; 6480b57cec5SDimitry Andric 6490b57cec5SDimitry Andric // To keep things simple, only process loops with single latch. We 6500b57cec5SDimitry Andric // canonicalize most loops to this form. We can support multi-latch if needed. 6510b57cec5SDimitry Andric if (!L.getLoopLatch()) 6520b57cec5SDimitry Andric return false; 6530b57cec5SDimitry Andric 6540b57cec5SDimitry Andric ConstantTerminatorFoldingImpl BranchFolder(L, LI, DT, SE, MSSAU); 6550b57cec5SDimitry Andric bool Changed = BranchFolder.run(); 6560b57cec5SDimitry Andric IsLoopDeleted = Changed && BranchFolder.foldingBreaksCurrentLoop(); 6570b57cec5SDimitry Andric return Changed; 6580b57cec5SDimitry Andric } 6590b57cec5SDimitry Andric 6600b57cec5SDimitry Andric static bool mergeBlocksIntoPredecessors(Loop &L, DominatorTree &DT, 661*bdd1243dSDimitry Andric LoopInfo &LI, MemorySSAUpdater *MSSAU, 662*bdd1243dSDimitry Andric ScalarEvolution &SE) { 6630b57cec5SDimitry Andric bool Changed = false; 6640b57cec5SDimitry Andric DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); 6650b57cec5SDimitry Andric // Copy blocks into a temporary array to avoid iterator invalidation issues 6660b57cec5SDimitry Andric // as we remove them. 6670b57cec5SDimitry Andric SmallVector<WeakTrackingVH, 16> Blocks(L.blocks()); 6680b57cec5SDimitry Andric 6690b57cec5SDimitry Andric for (auto &Block : Blocks) { 6700b57cec5SDimitry Andric // Attempt to merge blocks in the trivial case. Don't modify blocks which 6710b57cec5SDimitry Andric // belong to other loops. 6720b57cec5SDimitry Andric BasicBlock *Succ = cast_or_null<BasicBlock>(Block); 6730b57cec5SDimitry Andric if (!Succ) 6740b57cec5SDimitry Andric continue; 6750b57cec5SDimitry Andric 6760b57cec5SDimitry Andric BasicBlock *Pred = Succ->getSinglePredecessor(); 6770b57cec5SDimitry Andric if (!Pred || !Pred->getSingleSuccessor() || LI.getLoopFor(Pred) != &L) 6780b57cec5SDimitry Andric continue; 6790b57cec5SDimitry Andric 6800b57cec5SDimitry Andric // Merge Succ into Pred and delete it. 6810b57cec5SDimitry Andric MergeBlockIntoPredecessor(Succ, &DTU, &LI, MSSAU); 6820b57cec5SDimitry Andric 683480093f4SDimitry Andric if (MSSAU && VerifyMemorySSA) 684480093f4SDimitry Andric MSSAU->getMemorySSA()->verifyMemorySSA(); 685480093f4SDimitry Andric 6860b57cec5SDimitry Andric Changed = true; 6870b57cec5SDimitry Andric } 6880b57cec5SDimitry Andric 689*bdd1243dSDimitry Andric if (Changed) 690*bdd1243dSDimitry Andric SE.forgetBlockAndLoopDispositions(); 691*bdd1243dSDimitry Andric 6920b57cec5SDimitry Andric return Changed; 6930b57cec5SDimitry Andric } 6940b57cec5SDimitry Andric 6950b57cec5SDimitry Andric static bool simplifyLoopCFG(Loop &L, DominatorTree &DT, LoopInfo &LI, 6960b57cec5SDimitry Andric ScalarEvolution &SE, MemorySSAUpdater *MSSAU, 6975ffd83dbSDimitry Andric bool &IsLoopDeleted) { 6980b57cec5SDimitry Andric bool Changed = false; 6990b57cec5SDimitry Andric 7000b57cec5SDimitry Andric // Constant-fold terminators with known constant conditions. 7015ffd83dbSDimitry Andric Changed |= constantFoldTerminators(L, DT, LI, SE, MSSAU, IsLoopDeleted); 7020b57cec5SDimitry Andric 7035ffd83dbSDimitry Andric if (IsLoopDeleted) 7040b57cec5SDimitry Andric return true; 7050b57cec5SDimitry Andric 7060b57cec5SDimitry Andric // Eliminate unconditional branches by merging blocks into their predecessors. 707*bdd1243dSDimitry Andric Changed |= mergeBlocksIntoPredecessors(L, DT, LI, MSSAU, SE); 7080b57cec5SDimitry Andric 7090b57cec5SDimitry Andric if (Changed) 7100b57cec5SDimitry Andric SE.forgetTopmostLoop(&L); 7110b57cec5SDimitry Andric 7120b57cec5SDimitry Andric return Changed; 7130b57cec5SDimitry Andric } 7140b57cec5SDimitry Andric 7150b57cec5SDimitry Andric PreservedAnalyses LoopSimplifyCFGPass::run(Loop &L, LoopAnalysisManager &AM, 7160b57cec5SDimitry Andric LoopStandardAnalysisResults &AR, 7170b57cec5SDimitry Andric LPMUpdater &LPMU) { 718*bdd1243dSDimitry Andric std::optional<MemorySSAUpdater> MSSAU; 7198bcb0991SDimitry Andric if (AR.MSSA) 7200b57cec5SDimitry Andric MSSAU = MemorySSAUpdater(AR.MSSA); 7210b57cec5SDimitry Andric bool DeleteCurrentLoop = false; 722*bdd1243dSDimitry Andric if (!simplifyLoopCFG(L, AR.DT, AR.LI, AR.SE, MSSAU ? &*MSSAU : nullptr, 723*bdd1243dSDimitry Andric DeleteCurrentLoop)) 7240b57cec5SDimitry Andric return PreservedAnalyses::all(); 7250b57cec5SDimitry Andric 7260b57cec5SDimitry Andric if (DeleteCurrentLoop) 7270b57cec5SDimitry Andric LPMU.markLoopAsDeleted(L, "loop-simplifycfg"); 7280b57cec5SDimitry Andric 7290b57cec5SDimitry Andric auto PA = getLoopPassPreservedAnalyses(); 7308bcb0991SDimitry Andric if (AR.MSSA) 7310b57cec5SDimitry Andric PA.preserve<MemorySSAAnalysis>(); 7320b57cec5SDimitry Andric return PA; 7330b57cec5SDimitry Andric } 734