Index: lib/Transforms/Scalar/LoopSimplifyCFG.cpp =================================================================== --- lib/Transforms/Scalar/LoopSimplifyCFG.cpp +++ lib/Transforms/Scalar/LoopSimplifyCFG.cpp @@ -41,6 +41,271 @@ #define DEBUG_TYPE "loop-simplifycfg" +STATISTIC(NumTerminatorsFolded, + "Number of terminators folded to unconditional branches"); + +/// If \p BB has multiple successors, but only one of them can be reached from +/// this block in runtime, return this successor. Otherwise, return nullptr. +static BasicBlock *getOnlyLiveSuccessor(BasicBlock *BB) { + Instruction *TI = BB->getTerminator(); + if (BranchInst *BI = dyn_cast(TI)) { + if (BI->isUnconditional()) + return nullptr; + if (BI->getSuccessor(0) == BI->getSuccessor(1)) + return BI->getSuccessor(0); + ConstantInt *Cond = dyn_cast(BI->getCondition()); + if (!Cond) + return nullptr; + return Cond->isZero() ? BI->getSuccessor(1) : BI->getSuccessor(0); + } + + if (SwitchInst *SI = dyn_cast(TI)) { + auto *CI = dyn_cast(SI->getCondition()); + if (!SI) + return nullptr; + BasicBlock *LiveSucc = nullptr; + for (auto i = SI->case_begin(), e = SI->case_end(); i != e && !LiveSucc; + ++i) + if (i->getCaseValue() == CI) + LiveSucc = i->getCaseSuccessor(); + return LiveSucc ? LiveSucc : SI->getDefaultDest(); + } + + return nullptr; +} + +/// Helper class that can turn branches and switches with constant conditions +/// into unconditional branches. +class ConstantTerminatorFoldingImpl { +private: + Loop &L; + LoopInfo &LI; + DominatorTree &DT; + + // Whether or not the current loop will still exist after terminator constant + // folding has been done. + bool LoopIsLive = true; + + // The blocks of the original loop that will still be reachable from entry + // after the constant folding. + SmallPtrSet LiveLoopBlocks; + // The blocks of the original loop that will become unreachable from entry + // after the constant folding. + SmallPtrSet DeadLoopBlocks; + // The exits of the original loop that will still be reachable from entry + // after the constant folding. + SmallPtrSet LiveExitBlocks; + // The exits of the original loop that will become unreachable from entry + // after the constant folding. + SmallPtrSet DeadExitBlocks; + // The blocks that have terminators with constant condition that can be + // folded. + SmallPtrSet FoldCandidates; + // The blocks that will still be a part of the current loop after folding. + SmallPtrSet BlocksInLoopAfterFolding; + + void dump() const { + dbgs() << "Constant terminator folding for loop " << L << "\n"; + dbgs() << "After terminator constant-folding, the loop will"; + if (LoopIsLive) + dbgs() << " not"; + dbgs() << " be destroyed\n"; + dbgs() << "Blocks in which we can constant-fold terminator:\n"; + for (const BasicBlock *BB : FoldCandidates) + dbgs() << "\t" << BB->getName() << "\n"; + dbgs() << "Live blocks from the original loop:\n"; + for (const BasicBlock *BB : LiveLoopBlocks) + dbgs() << "\t" << BB->getName() << "\n"; + dbgs() << "Dead blocks from the original loop:\n"; + for (const BasicBlock *BB : LiveLoopBlocks) + dbgs() << "\t" << BB->getName() << "\n"; + dbgs() << "Live exit blocks:\n"; + for (const BasicBlock *BB : LiveExitBlocks) + dbgs() << "\t" << BB->getName() << "\n"; + dbgs() << "Dead exit blocks:\n"; + for (const BasicBlock *BB : DeadExitBlocks) + dbgs() << "\t" << BB->getName() << "\n"; + if (LoopIsLive) { + dbgs() << "The following blocks will still be part of loop:\n"; + for (const BasicBlock *BB : BlocksInLoopAfterFolding) + dbgs() << "\t" << BB->getName() << "\n"; + } + } + + /// Fill all information about status of blocks and exits of the current loop + /// after constant folding has been done. + void analyze() { + LoopBlocksDFS DFS(&L); + DFS.perform(&LI); + assert(DFS.isComplete() && "DFS is expected to be finished"); + + // Collect live and dead loop blocks and exits. + SmallPtrSet ExitBlocks; + LiveLoopBlocks.insert(L.getHeader()); + for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) { + BasicBlock *BB = *I; + + // If a loop block wasn't marked as live so far, then it's dead. + if (!LiveLoopBlocks.count(BB)) { + DeadLoopBlocks.insert(BB); + continue; + } + + BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(BB); + + // If a block has only one live successor, it's a candidate on constant + // folding. Only handle blocks from current loop: branches in child loops + // are skipped because if they can be folded, they should be folded during + // the processing of child loops. + if (TheOnlySucc && LI.getLoopFor(BB) == &L) + FoldCandidates.insert(BB); + + // Handle successors. + auto ProcessSuccessor = [&](BasicBlock *Succ, bool IsLive) { + if (!L.contains(Succ)) { + if (IsLive) + LiveExitBlocks.insert(Succ); + ExitBlocks.insert(Succ); + } else if (IsLive) + LiveLoopBlocks.insert(Succ); + }; + for (BasicBlock *Succ : successors(BB)) + ProcessSuccessor(Succ, !TheOnlySucc || TheOnlySucc == Succ); + } + + // Sanity check: amount of dead and live loop blocks should match the total + // number of blocks in loop. + assert(L.getNumBlocks() == LiveLoopBlocks.size() + DeadLoopBlocks.size() && + "Malformed block sets?"); + + // Now, all exit blocks that are not marked as live are dead. + for (auto *ExitBlock : ExitBlocks) + if (!LiveExitBlocks.count(ExitBlock)) + DeadExitBlocks.insert(ExitBlock); + + // Whether or not the edge From->To will still be present in graph after the + // folding. + auto IsEdgeLive = [&](BasicBlock *From, BasicBlock *To) { + if (!LiveLoopBlocks.count(From)) + return false; + BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(From); + return !TheOnlySucc || TheOnlySucc == To; + }; + + // The loop is live is its latch is live. + LoopIsLive = IsEdgeLive(L.getLoopLatch(), L.getHeader()); + + if (LoopIsLive) { + BlocksInLoopAfterFolding.insert(L.getLoopLatch()); + // If the loop is live, then we should compute what blocks are still in + // loop after all branch folding has been done. A block is in loop if + // it has a live edge to another block that is in the loop; by definition, + // latch is in the loop. + auto BlockIsInLoop = [&](BasicBlock *BB) { + return any_of(successors(BB), [&](BasicBlock *Succ) { + return BlocksInLoopAfterFolding.count(Succ) && IsEdgeLive(BB, Succ); + }); + }; + for (auto I = DFS.beginPostorder(), E = DFS.endPostorder(); I != E; ++I) { + BasicBlock *BB = *I; + if (BlockIsInLoop(BB)) + BlocksInLoopAfterFolding.insert(BB); + } + + // Sanity check: header must be in loop. + assert(BlocksInLoopAfterFolding.count(L.getHeader()) && + "Header not in loop?"); + } + } + + /// Constant-fold terminators of blocks acculumated in FoldCandidates into the + /// unconditional branches. + void foldTerminators() { + DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); + + for (BasicBlock *BB : FoldCandidates) { + assert(LI.getLoopFor(BB) == &L && "Should be a loop block!"); + BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(BB); + assert(TheOnlySucc && "Should have one live successor!"); + + LLVM_DEBUG(dbgs() << "Replacing terminator of " << BB->getName() + << " with an unconditional branch to the block " + << TheOnlySucc->getName() << "\n"); + + SmallPtrSet DeadSuccessors; + // Remove all BB's successors except for the live one. + for (auto *Succ : successors(BB)) + if (Succ != TheOnlySucc) + DeadSuccessors.insert(Succ); + + IRBuilder<> Builder(BB->getContext()); + Instruction *Term = BB->getTerminator(); + Builder.SetInsertPoint(Term); + Builder.CreateBr(TheOnlySucc); + Term->eraseFromParent(); + + for (auto *DeadSucc : DeadSuccessors) + DTU.deleteEdge(BB, DeadSucc); + + ++NumTerminatorsFolded; + } + } + +public: + ConstantTerminatorFoldingImpl(Loop &L, LoopInfo &LI, DominatorTree &DT) + : L(L), LI(LI), DT(DT) { + analyze(); + } + bool run() { + assert(L.getLoopLatch() && "Should be single latch!"); + + // TODO: Support deletion of dead loop blocks. + if (!DeadLoopBlocks.empty()) + return false; + + // TODO: Support dead loop exits. + if (!DeadExitBlocks.empty()) + return false; + + // TODO: Support deletion of the current loop. + if (!LoopIsLive) + return false; + + // TODO: Support blocks that are not dead, but also not in loop after the + // folding. + if (BlocksInLoopAfterFolding.size() != L.getNumBlocks()) + return false; + + // Nothing to constant-fold. + if (FoldCandidates.empty()) + return false; + + // Dump analysis results. + LLVM_DEBUG(dump()); + + // Make the actual transforms. + foldTerminators(); + + return true; + } +}; + +/// Turn branches and switches with known constant conditions into unconditional +/// branches. +static bool constantFoldTerminators(Loop &L, DominatorTree &DT, LoopInfo &LI) { + // To keep things simple, only process loops with single latch. We + // canonicalize most loops to this form. + if (!L.getLoopLatch()) + return false; + + bool Changed = false; + + ConstantTerminatorFoldingImpl BranchFolder(L, LI, DT); + Changed |= BranchFolder.run(); + + return Changed; +} + static bool mergeBlocksIntoPredecessors(Loop &L, DominatorTree &DT, LoopInfo &LI, MemorySSAUpdater *MSSAU) { bool Changed = false; @@ -73,6 +338,9 @@ ScalarEvolution &SE, MemorySSAUpdater *MSSAU) { bool Changed = false; + // Constant-fold terminators with known constant conditions. + Changed |= constantFoldTerminators(L, DT, LI); + // Eliminate unconditional branches by merging blocks into their predecessors. Changed |= mergeBlocksIntoPredecessors(L, DT, LI, MSSAU); Index: test/Transforms/LoopSimplifyCFG/constant-fold-branch.ll =================================================================== --- test/Transforms/LoopSimplifyCFG/constant-fold-branch.ll +++ test/Transforms/LoopSimplifyCFG/constant-fold-branch.ll @@ -668,7 +668,7 @@ ; CHECK-NEXT: [[I:%.*]] = phi i32 [ 0, [[PREHEADER:%.*]] ], [ [[I_INC:%.*]], [[BACKEDGE:%.*]] ] ; CHECK-NEXT: br i1 [[C:%.*]], label [[CHECK:%.*]], label [[LIVE:%.*]] ; CHECK: check: -; CHECK-NEXT: br i1 true, label [[BACKEDGE]], label [[LIVE]] +; CHECK-NEXT: br label [[BACKEDGE]] ; CHECK: live: ; CHECK-NEXT: [[I_2:%.*]] = add i32 [[I]], 1 ; CHECK-NEXT: br label [[BACKEDGE]] @@ -713,11 +713,7 @@ ; CHECK-NEXT: [[I:%.*]] = phi i32 [ 0, [[PREHEADER:%.*]] ], [ [[I_INC:%.*]], [[BACKEDGE:%.*]] ] ; CHECK-NEXT: br i1 [[C:%.*]], label [[CHECK:%.*]], label [[LIVE:%.*]] ; CHECK: check: -; CHECK-NEXT: switch i32 1, label [[LIVE]] [ -; CHECK-NEXT: i32 0, label [[LIVE]] -; CHECK-NEXT: i32 1, label [[BACKEDGE]] -; CHECK-NEXT: i32 2, label [[LIVE]] -; CHECK-NEXT: ] +; CHECK-NEXT: br label [[BACKEDGE]] ; CHECK: live: ; CHECK-NEXT: [[I_2:%.*]] = add i32 [[I]], 1 ; CHECK-NEXT: br label [[BACKEDGE]]