diff options
author | Dale Johannesen <dalej@apple.com> | 2007-02-17 00:44:34 +0000 |
---|---|---|
committer | Dale Johannesen <dalej@apple.com> | 2007-02-17 00:44:34 +0000 |
commit | 6b896cec8b703e08e5f3d809e086a39b6ebe6589 (patch) | |
tree | f1780e4eb84384f95d795e4b07a9e394496790fe /lib/CodeGen/BranchFolding.cpp | |
parent | affeb56480ab7cb2e2eed871fbf6701875934c04 (diff) |
Fixes PR 1200
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@34359 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen/BranchFolding.cpp')
-rw-r--r-- | lib/CodeGen/BranchFolding.cpp | 42 |
1 files changed, 29 insertions, 13 deletions
diff --git a/lib/CodeGen/BranchFolding.cpp b/lib/CodeGen/BranchFolding.cpp index 10262f2854..9145418cc3 100644 --- a/lib/CodeGen/BranchFolding.cpp +++ b/lib/CodeGen/BranchFolding.cpp @@ -431,6 +431,9 @@ bool BranchFolder::TailMergeBlocks(MachineFunction &MF) { bool BranchFolder::OptimizeBranches(MachineFunction &MF) { MadeChange = false; + // Make sure blocks are numbered in order + MF.RenumberBlocks(); + for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ) { MachineBasicBlock *MBB = I++; OptimizeBlock(MBB); @@ -849,22 +852,35 @@ void BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) { PriorTBB, PriorFBB, PriorCond)) { // Now we know that there was no fall-through into this block, check to // see if it has a fall-through into its successor. - if (!CanFallThrough(MBB, CurUnAnalyzable, CurTBB, CurFBB, CurCond)) { - // Check all the predecessors of this block. If one of them has no fall - // throughs, move this block right after it. - for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), - E = MBB->pred_end(); PI != E; ++PI) { - // Analyze the branch at the end of the pred. - MachineBasicBlock *PredBB = *PI; - MachineFunction::iterator PredFallthrough = PredBB; ++PredFallthrough; - std::vector<MachineOperand> PredCond; - if (PredBB != MBB && !CanFallThrough(PredBB)) { - MBB->moveAfter(PredBB); - MadeChange = true; - return OptimizeBlock(MBB); + bool CurFallsThru = CanFallThrough(MBB, CurUnAnalyzable, CurTBB, CurFBB, + CurCond); + + // Check all the predecessors of this block. If one of them has no fall + // throughs, move this block right after it. + for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), + E = MBB->pred_end(); PI != E; ++PI) { + // Analyze the branch at the end of the pred. + MachineBasicBlock *PredBB = *PI; + MachineFunction::iterator PredFallthrough = PredBB; ++PredFallthrough; + if (PredBB != MBB && !CanFallThrough(PredBB) + && (!CurFallsThru || MBB->getNumber() >= PredBB->getNumber())) { + // If the current block doesn't fall through, just move it. + // If the current block can fall through and does not end with a + // conditional branch, we need to append an unconditional jump to + // the (current) next block. To avoid a possible compile-time + // infinite loop, move blocks only backward in this case. + if (CurFallsThru) { + MachineBasicBlock *NextBB = next(MachineFunction::iterator(MBB)); + CurCond.clear(); + TII->InsertBranch(*MBB, NextBB, 0, CurCond); } + MBB->moveAfter(PredBB); + MadeChange = true; + return OptimizeBlock(MBB); } + } + if (!CurFallsThru) { // Check all successors to see if we can move this block before it. for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), E = MBB->succ_end(); SI != E; ++SI) { |