From: Bill Wendling Date: Fri, 11 Dec 2009 21:47:36 +0000 (+0000) Subject: Don't try to move a MBB into the fall-through position if it's a landing pad or X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=c42a0b75a41144be86d40872b57850ab578969ea;p=oota-llvm.git Don't try to move a MBB into the fall-through position if it's a landing pad or branches only to a landing pad. Without this check, the compiler would go into an infinite loop because the branch to a landing pad is an "abnormal" edge which wasn't being taken into account. This is the meat of that fix: if (!PrevBB.canFallThrough() && !MBB->BranchesToLandingPad(MBB)) { The other stuff is simplification of the "branches to a landing pad" code. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@91161 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/BranchFolding.cpp b/lib/CodeGen/BranchFolding.cpp index 3887e6d44e4..7ac8bda7a35 100644 --- a/lib/CodeGen/BranchFolding.cpp +++ b/lib/CodeGen/BranchFolding.cpp @@ -1205,11 +1205,11 @@ ReoptimizeBlock: } } - // If the prior block doesn't fall through into this block, and if this - // block doesn't fall through into some other block, see if we can find a - // place to move this block where a fall-through will happen. - if (!PrevBB.canFallThrough()) { - + // If the prior block doesn't fall through into this block and if this block + // doesn't fall through into some other block and it's not branching only to a + // landing pad, then see if we can find a place to move this block where a + // fall-through will happen. + if (!PrevBB.canFallThrough() && !MBB->BranchesToLandingPad(MBB)) { // Now we know that there was no fall-through into this block, check to // see if it has a fall-through into its successor. bool CurFallsThru = MBB->canFallThrough(); @@ -1221,28 +1221,32 @@ ReoptimizeBlock: E = MBB->pred_end(); PI != E; ++PI) { // Analyze the branch at the end of the pred. MachineBasicBlock *PredBB = *PI; - MachineFunction::iterator PredFallthrough = PredBB; ++PredFallthrough; + MachineFunction::iterator PredNextBB = PredBB; ++PredNextBB; MachineBasicBlock *PredTBB, *PredFBB; SmallVector PredCond; - if (PredBB != MBB && !PredBB->canFallThrough() && - !TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true) + if (PredBB != MBB && !PredBB->canFallThrough() + && !TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true) && (!CurFallsThru || !CurTBB || !CurFBB) && (!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. - // Also, if there are already 2 branches here, we cannot add a third; - // this means we have the case - // Bcc next - // B elsewhere - // next: + // 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. + // + // Also, if there are already 2 branches here, we cannot add a third. + // I.e. we have the case: + // + // Bcc next + // B elsewhere + // next: if (CurFallsThru) { - MachineBasicBlock *NextBB = llvm::next(MachineFunction::iterator(MBB)); + MachineBasicBlock *NextBB = + llvm::next(MachineFunction::iterator(MBB)); CurCond.clear(); TII->InsertBranch(*MBB, NextBB, 0, CurCond); } + MBB->moveAfter(PredBB); MadeChange = true; goto ReoptimizeBlock; diff --git a/lib/CodeGen/MachineBasicBlock.cpp b/lib/CodeGen/MachineBasicBlock.cpp index 0097dd16105..80b4b0ffd8d 100644 --- a/lib/CodeGen/MachineBasicBlock.cpp +++ b/lib/CodeGen/MachineBasicBlock.cpp @@ -457,16 +457,9 @@ MachineBasicBlock::BranchesToLandingPad(const MachineBasicBlock *MBB) const { SmallSet Visited; const MachineBasicBlock *CurMBB = MBB; - while (!Visited.count(CurMBB) && !CurMBB->isLandingPad()) { - if (CurMBB->size() != 1 || CurMBB->succ_empty() || CurMBB->succ_size() != 1) - break; - - const TargetInstrInfo *TII = - CurMBB->getParent()->getTarget().getInstrInfo(); - if (!TII->isUnpredicatedTerminator(CurMBB->begin())) - break; - - Visited.insert(CurMBB); + while (!CurMBB->isLandingPad()) { + if (CurMBB->succ_size() != 1) break; + if (!Visited.insert(CurMBB)) break; CurMBB = *CurMBB->succ_begin(); }