From cf420cca572b061fdd63587cb90904c641b6e216 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 28 Oct 2006 17:32:47 +0000 Subject: [PATCH] improve deletion of blocks that just contain branches by knowing that the pred block doesn't fall through into them if it's a jumptable. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@31263 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/BranchFolding.cpp | 71 +++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/lib/CodeGen/BranchFolding.cpp b/lib/CodeGen/BranchFolding.cpp index 5cad24845de..07d44dc5d72 100644 --- a/lib/CodeGen/BranchFolding.cpp +++ b/lib/CodeGen/BranchFolding.cpp @@ -534,43 +534,48 @@ void BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) { // completely eliminate the block is when the block before this one // falls through into MBB and we can't understand the prior block's branch // condition. - if (MBB->empty() && (!PriorUnAnalyzable || !PrevBB.isSuccessor(MBB))) { - // If the prior block falls through into us, turn it into an - // explicit branch to us to make updates simpler. - if (PrevBB.isSuccessor(MBB) && PriorTBB != MBB && PriorFBB != MBB) { - if (PriorTBB == 0) { - assert(PriorCond.empty() && PriorFBB == 0 && "Bad branch analysis"); - PriorTBB = MBB; - } else { - assert(PriorFBB == 0 && "Machine CFG out of date!"); - PriorFBB = MBB; + if (MBB->empty()) { + bool PredHasNoFallThrough = TII->BlockHasNoFallThrough(PrevBB); + if (PredHasNoFallThrough || !PriorUnAnalyzable || + !PrevBB.isSuccessor(MBB)) { + // If the prior block falls through into us, turn it into an + // explicit branch to us to make updates simpler. + if (!PredHasNoFallThrough && PrevBB.isSuccessor(MBB) && + PriorTBB != MBB && PriorFBB != MBB) { + if (PriorTBB == 0) { + assert(PriorCond.empty() && PriorFBB == 0 && "Bad branch analysis"); + PriorTBB = MBB; + } else { + assert(PriorFBB == 0 && "Machine CFG out of date!"); + PriorFBB = MBB; + } + TII->RemoveBranch(PrevBB); + TII->InsertBranch(PrevBB, PriorTBB, PriorFBB, PriorCond); } - TII->RemoveBranch(PrevBB); - TII->InsertBranch(PrevBB, PriorTBB, PriorFBB, PriorCond); - } - // Iterate through all the predecessors, revectoring each in-turn. - MachineBasicBlock::pred_iterator PI = MBB->pred_begin(); - bool DidChange = false; - bool HasBranchToSelf = false; - while (PI != MBB->pred_end()) { - if (*PI == MBB) { - // If this block has an uncond branch to itself, leave it. - ++PI; - HasBranchToSelf = true; - } else { - DidChange = true; - ReplaceUsesOfBlockWith(*PI, MBB, CurTBB, TII); + // Iterate through all the predecessors, revectoring each in-turn. + MachineBasicBlock::pred_iterator PI = MBB->pred_begin(); + bool DidChange = false; + bool HasBranchToSelf = false; + while (PI != MBB->pred_end()) { + if (*PI == MBB) { + // If this block has an uncond branch to itself, leave it. + ++PI; + HasBranchToSelf = true; + } else { + DidChange = true; + ReplaceUsesOfBlockWith(*PI, MBB, CurTBB, TII); + } } - } - // Change any jumptables to go to the new MBB. - MBB->getParent()->getJumpTableInfo()->ReplaceMBBInJumpTables(MBB, - CurTBB); - if (DidChange) { - ++NumBranchOpts; - MadeChange = true; - if (!HasBranchToSelf) return; + // Change any jumptables to go to the new MBB. + MBB->getParent()->getJumpTableInfo()->ReplaceMBBInJumpTables(MBB, + CurTBB); + if (DidChange) { + ++NumBranchOpts; + MadeChange = true; + if (!HasBranchToSelf) return; + } } }