diff --git a/llvm/lib/CodeGen/BranchFolding.h b/llvm/lib/CodeGen/BranchFolding.h --- a/llvm/lib/CodeGen/BranchFolding.h +++ b/llvm/lib/CodeGen/BranchFolding.h @@ -160,6 +160,8 @@ MachineBasicBlock *SuccBB, MachineBasicBlock *PredBB); + unsigned HashEndOfMBB(const MachineBasicBlock &MBB) const; + /// Remove all blocks with hash CurHash from MergePotentials, restoring /// branches at ends of blocks as appropriate. void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB, diff --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp --- a/llvm/lib/CodeGen/BranchFolding.cpp +++ b/llvm/lib/CodeGen/BranchFolding.cpp @@ -289,12 +289,20 @@ } /// HashEndOfMBB - Hash the last instruction in the MBB. -static unsigned HashEndOfMBB(const MachineBasicBlock &MBB) { +unsigned BranchFolder::HashEndOfMBB(const MachineBasicBlock &MBB) const { + unsigned LastInstrHash = 0; MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr(false); - if (I == MBB.end()) - return 0; + if (I != MBB.end()) + LastInstrHash = HashMachineInstr(*I); - return HashMachineInstr(*I); + // Avoid trying to tail merge across EH scopes. + unsigned EHScope = 0; + if (!EHScopeMembership.empty()) { + assert(EHScopeMembership.count(&MBB)); + EHScope = EHScopeMembership.find(&MBB)->second; + llvm::errs() << "EHScope for " << printMBBReference(MBB) << ": " << EHScope << '\n'; + } + return LastInstrHash + EHScope; } /// Whether MI should be counted as an instruction when calculating common tail. @@ -1051,6 +1059,8 @@ // a compile-time infinite loop repeatedly doing and undoing the same // transformations.) + llvm::errs() << "after TryTailMergeBlocks\n"; + for (MachineFunction::iterator I = std::next(MF.begin()), E = MF.end(); I != E; ++I) { if (I->pred_size() < 2) continue; @@ -1150,6 +1160,7 @@ MergePotentials.begin()->getBlock() != PredBB) FixTail(MergePotentials.begin()->getBlock(), IBB, TII); } + llvm::errs() << "after after TryTailMergeBlocks\n"; return MadeChange; } @@ -1216,6 +1227,8 @@ // If it is dead, remove it. if (MBB.pred_empty() && !MBB.isMachineBlockAddressTaken()) { + LLVM_DEBUG(dbgs() << "OptimizeBlock removed predecessors of " + << printMBBReference(MBB) << '\n'); RemoveDeadBlock(&MBB); MadeChange = true; ++NumDeadBlocks; @@ -1350,7 +1363,7 @@ // points to this block. Blocks with their addresses taken shouldn't be // optimized away. if (IsEmptyBlock(MBB) && !MBB->isEHPad() && !MBB->hasAddressTaken() && - SameEHScope) { + SameEHScope && !MBB->hasEHPadSuccessor()) { salvageDebugInfoFromEmptyBlock(TII, *MBB); // Dead block? Leave for cleanup later. if (MBB->pred_empty()) return MadeChange;