aboutsummaryrefslogtreecommitdiff
path: root/lib/CodeGen/BranchFolding.cpp
diff options
context:
space:
mode:
authorJim Laskey <jlaskey@mac.com>2007-02-21 22:42:20 +0000
committerJim Laskey <jlaskey@mac.com>2007-02-21 22:42:20 +0000
commit02b3f5ec4ac120d14a63f7fd4f4388b7d0ab6c22 (patch)
treef75eb4e7b53c60806319115a6f896923ceab1f40 /lib/CodeGen/BranchFolding.cpp
parent9b25b8ca24d6df2e097741dcc15016772ee4eda7 (diff)
Make branch folding behave in the presence of landing pads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@34476 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen/BranchFolding.cpp')
-rw-r--r--lib/CodeGen/BranchFolding.cpp55
1 files changed, 30 insertions, 25 deletions
diff --git a/lib/CodeGen/BranchFolding.cpp b/lib/CodeGen/BranchFolding.cpp
index 9145418cc3..6c0434180e 100644
--- a/lib/CodeGen/BranchFolding.cpp
+++ b/lib/CodeGen/BranchFolding.cpp
@@ -67,7 +67,8 @@ FunctionPass *llvm::createBranchFoldingPass() { return new BranchFolder(); }
/// RemoveDeadBlock - Remove the specified dead machine basic block from the
/// function, updating the CFG.
void BranchFolder::RemoveDeadBlock(MachineBasicBlock *MBB) {
- assert(MBB->pred_empty() && "MBB must be dead!");
+ assert(!MBB->isAccessable() && "MBB must be dead!");
+ DOUT << "\nRemoving MBB: " << *MBB;
MachineFunction *MF = MBB->getParent();
// drop all successors.
@@ -439,7 +440,7 @@ bool BranchFolder::OptimizeBranches(MachineFunction &MF) {
OptimizeBlock(MBB);
// If it is dead, remove it.
- if (MBB->pred_empty()) {
+ if (!MBB->isAccessable()) {
RemoveDeadBlock(MBB);
MadeChange = true;
++NumDeadBlocks;
@@ -485,6 +486,8 @@ static bool CorrectExtraCFGEdges(MachineBasicBlock &MBB,
} else if (*SI == DestB) {
DestB = 0;
++SI;
+ } else if ((*SI)->isLandingPad()) {
+ ++SI;
} else {
// Otherwise, this is a superfluous edge, remove it.
MBB.removeSuccessor(SI);
@@ -615,14 +618,14 @@ void BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
// explicitly.
if (MBB->empty()) {
// Dead block? Leave for cleanup later.
- if (MBB->pred_empty()) return;
+ if (!MBB->isAccessable()) return;
if (FallThrough == MBB->getParent()->end()) {
// TODO: Simplify preds to not branch here if possible!
} else {
// Rewrite all predecessors of the old block to go to the fallthrough
// instead.
- while (!MBB->pred_empty()) {
+ while (MBB->isAccessable()) {
MachineBasicBlock *Pred = *(MBB->pred_end()-1);
ReplaceUsesOfBlockWith(Pred, MBB, FallThrough, TII);
}
@@ -855,28 +858,30 @@ void BranchFolder::OptimizeBlock(MachineBasicBlock *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);
+ if (!MBB->isLandingPad()) {
+ // 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);
}
- MBB->moveAfter(PredBB);
- MadeChange = true;
- return OptimizeBlock(MBB);
}
}