Turns out AnalyzeBranch can modify the mbb being analyzed. This is a nasty

suprise to some callers, e.g. register coalescer. For now, add an parameter
that tells AnalyzeBranch whether it's safe to modify the mbb. A better
solution is out there, but I don't have time to deal with it right now.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@64124 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Evan Cheng 2009-02-09 07:14:22 +00:00
parent 0de5d6fce0
commit dc54d317e7
16 changed files with 60 additions and 30 deletions

View File

@ -195,9 +195,13 @@ public:
/// Note that RemoveBranch and InsertBranch must be implemented to support /// Note that RemoveBranch and InsertBranch must be implemented to support
/// cases where this method returns success. /// cases where this method returns success.
/// ///
/// If AllowModify is true, then this routine is allowed to modify the basic
/// block (e.g. delete instructions after the unconditional branch).
///
virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
MachineBasicBlock *&FBB, MachineBasicBlock *&FBB,
SmallVectorImpl<MachineOperand> &Cond) const { SmallVectorImpl<MachineOperand> &Cond,
bool AllowModify = false) const {
return true; return true;
} }

View File

@ -191,7 +191,7 @@ bool BranchFolder::runOnMachineFunction(MachineFunction &MF) {
for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; I++) { for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; I++) {
MachineBasicBlock *MBB = I, *TBB = 0, *FBB = 0; MachineBasicBlock *MBB = I, *TBB = 0, *FBB = 0;
SmallVector<MachineOperand, 4> Cond; SmallVector<MachineOperand, 4> Cond;
if (!TII->AnalyzeBranch(*MBB, TBB, FBB, Cond)) if (!TII->AnalyzeBranch(*MBB, TBB, FBB, Cond, true))
EverMadeChange |= MBB->CorrectExtraCFGEdges(TBB, FBB, !Cond.empty()); EverMadeChange |= MBB->CorrectExtraCFGEdges(TBB, FBB, !Cond.empty());
EverMadeChange |= OptimizeImpDefsBlock(MBB); EverMadeChange |= OptimizeImpDefsBlock(MBB);
} }
@ -434,7 +434,7 @@ static void FixTail(MachineBasicBlock* CurMBB, MachineBasicBlock *SuccBB,
MachineBasicBlock *TBB = 0, *FBB = 0; MachineBasicBlock *TBB = 0, *FBB = 0;
SmallVector<MachineOperand, 4> Cond; SmallVector<MachineOperand, 4> Cond;
if (I != MF->end() && if (I != MF->end() &&
!TII->AnalyzeBranch(*CurMBB, TBB, FBB, Cond)) { !TII->AnalyzeBranch(*CurMBB, TBB, FBB, Cond, true)) {
MachineBasicBlock *NextBB = I; MachineBasicBlock *NextBB = I;
if (TBB == NextBB && !Cond.empty() && !FBB) { if (TBB == NextBB && !Cond.empty() && !FBB) {
if (!TII->ReverseBranchCondition(Cond)) { if (!TII->ReverseBranchCondition(Cond)) {
@ -711,7 +711,7 @@ bool BranchFolder::TailMergeBlocks(MachineFunction &MF) {
continue; continue;
MachineBasicBlock *TBB = 0, *FBB = 0; MachineBasicBlock *TBB = 0, *FBB = 0;
SmallVector<MachineOperand, 4> Cond; SmallVector<MachineOperand, 4> Cond;
if (!TII->AnalyzeBranch(*PBB, TBB, FBB, Cond)) { if (!TII->AnalyzeBranch(*PBB, TBB, FBB, Cond, true)) {
// Failing case: IBB is the target of a cbr, and // Failing case: IBB is the target of a cbr, and
// we cannot reverse the branch. // we cannot reverse the branch.
SmallVector<MachineOperand, 4> NewCond(Cond); SmallVector<MachineOperand, 4> NewCond(Cond);
@ -845,7 +845,7 @@ bool BranchFolder::CanFallThrough(MachineBasicBlock *CurBB,
bool BranchFolder::CanFallThrough(MachineBasicBlock *CurBB) { bool BranchFolder::CanFallThrough(MachineBasicBlock *CurBB) {
MachineBasicBlock *TBB = 0, *FBB = 0; MachineBasicBlock *TBB = 0, *FBB = 0;
SmallVector<MachineOperand, 4> Cond; SmallVector<MachineOperand, 4> Cond;
bool CurUnAnalyzable = TII->AnalyzeBranch(*CurBB, TBB, FBB, Cond); bool CurUnAnalyzable = TII->AnalyzeBranch(*CurBB, TBB, FBB, Cond, true);
return CanFallThrough(CurBB, CurUnAnalyzable, TBB, FBB, Cond); return CanFallThrough(CurBB, CurUnAnalyzable, TBB, FBB, Cond);
} }
@ -910,7 +910,7 @@ void BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
MachineBasicBlock *PriorTBB = 0, *PriorFBB = 0; MachineBasicBlock *PriorTBB = 0, *PriorFBB = 0;
SmallVector<MachineOperand, 4> PriorCond; SmallVector<MachineOperand, 4> PriorCond;
bool PriorUnAnalyzable = bool PriorUnAnalyzable =
TII->AnalyzeBranch(PrevBB, PriorTBB, PriorFBB, PriorCond); TII->AnalyzeBranch(PrevBB, PriorTBB, PriorFBB, PriorCond, true);
if (!PriorUnAnalyzable) { if (!PriorUnAnalyzable) {
// If the CFG for the prior block has extra edges, remove them. // If the CFG for the prior block has extra edges, remove them.
MadeChange |= PrevBB.CorrectExtraCFGEdges(PriorTBB, PriorFBB, MadeChange |= PrevBB.CorrectExtraCFGEdges(PriorTBB, PriorFBB,
@ -1023,7 +1023,7 @@ void BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
// Analyze the branch in the current block. // Analyze the branch in the current block.
MachineBasicBlock *CurTBB = 0, *CurFBB = 0; MachineBasicBlock *CurTBB = 0, *CurFBB = 0;
SmallVector<MachineOperand, 4> CurCond; SmallVector<MachineOperand, 4> CurCond;
bool CurUnAnalyzable = TII->AnalyzeBranch(*MBB, CurTBB, CurFBB, CurCond); bool CurUnAnalyzable= TII->AnalyzeBranch(*MBB, CurTBB, CurFBB, CurCond, true);
if (!CurUnAnalyzable) { if (!CurUnAnalyzable) {
// If the CFG for the prior block has extra edges, remove them. // If the CFG for the prior block has extra edges, remove them.
MadeChange |= MBB->CorrectExtraCFGEdges(CurTBB, CurFBB, !CurCond.empty()); MadeChange |= MBB->CorrectExtraCFGEdges(CurTBB, CurFBB, !CurCond.empty());

View File

@ -335,7 +335,8 @@ ARMInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
// Branch analysis. // Branch analysis.
bool ARMInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB, bool ARMInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
MachineBasicBlock *&FBB, MachineBasicBlock *&FBB,
SmallVectorImpl<MachineOperand> &Cond) const { SmallVectorImpl<MachineOperand> &Cond,
bool AllowModify) const {
// If the block has no terminators, it just falls into the block after it. // If the block has no terminators, it just falls into the block after it.
MachineBasicBlock::iterator I = MBB.end(); MachineBasicBlock::iterator I = MBB.end();
if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
@ -385,7 +386,8 @@ bool ARMInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
(LastOpc == ARM::B || LastOpc == ARM::tB)) { (LastOpc == ARM::B || LastOpc == ARM::tB)) {
TBB = SecondLastInst->getOperand(0).getMBB(); TBB = SecondLastInst->getOperand(0).getMBB();
I = LastInst; I = LastInst;
I->eraseFromParent(); if (AllowModify)
I->eraseFromParent();
return false; return false;
} }
@ -396,7 +398,8 @@ bool ARMInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
SecondLastOpc == ARM::BR_JTadd || SecondLastOpc==ARM::tBR_JTr) && SecondLastOpc == ARM::BR_JTadd || SecondLastOpc==ARM::tBR_JTr) &&
(LastOpc == ARM::B || LastOpc == ARM::tB)) { (LastOpc == ARM::B || LastOpc == ARM::tB)) {
I = LastInst; I = LastInst;
I->eraseFromParent(); if (AllowModify)
I->eraseFromParent();
return true; return true;
} }

View File

@ -172,7 +172,8 @@ public:
// Branch analysis. // Branch analysis.
virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
MachineBasicBlock *&FBB, MachineBasicBlock *&FBB,
SmallVectorImpl<MachineOperand> &Cond) const; SmallVectorImpl<MachineOperand> &Cond,
bool AllowModify) const;
virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const; virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const;
virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
MachineBasicBlock *FBB, MachineBasicBlock *FBB,

View File

@ -321,8 +321,9 @@ static unsigned AlphaRevCondCode(unsigned Opcode) {
// Branch analysis. // Branch analysis.
bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB, bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
MachineBasicBlock *&FBB, MachineBasicBlock *&FBB,
SmallVectorImpl<MachineOperand> &Cond) const { SmallVectorImpl<MachineOperand> &Cond,
bool AllowModify) const {
// If the block has no terminators, it just falls into the block after it. // If the block has no terminators, it just falls into the block after it.
MachineBasicBlock::iterator I = MBB.end(); MachineBasicBlock::iterator I = MBB.end();
if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
@ -373,7 +374,8 @@ bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TB
LastInst->getOpcode() == Alpha::BR) { LastInst->getOpcode() == Alpha::BR) {
TBB = SecondLastInst->getOperand(0).getMBB(); TBB = SecondLastInst->getOperand(0).getMBB();
I = LastInst; I = LastInst;
I->eraseFromParent(); if (AllowModify)
I->eraseFromParent();
return false; return false;
} }

View File

@ -83,7 +83,8 @@ public:
bool AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB, bool AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
MachineBasicBlock *&FBB, MachineBasicBlock *&FBB,
SmallVectorImpl<MachineOperand> &Cond) const; SmallVectorImpl<MachineOperand> &Cond,
bool AllowModify) const;
unsigned RemoveBranch(MachineBasicBlock &MBB) const; unsigned RemoveBranch(MachineBasicBlock &MBB) const;
void insertNoop(MachineBasicBlock &MBB, void insertNoop(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI) const; MachineBasicBlock::iterator MI) const;

View File

@ -524,7 +524,8 @@ SPUInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
bool bool
SPUInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, SPUInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
MachineBasicBlock *&FBB, MachineBasicBlock *&FBB,
SmallVectorImpl<MachineOperand> &Cond) const { SmallVectorImpl<MachineOperand> &Cond,
bool AllowModify) const {
// If the block has no terminators, it just falls into the block after it. // If the block has no terminators, it just falls into the block after it.
MachineBasicBlock::iterator I = MBB.end(); MachineBasicBlock::iterator I = MBB.end();
if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
@ -575,7 +576,8 @@ SPUInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
if (isUncondBranch(SecondLastInst) && isUncondBranch(LastInst)) { if (isUncondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
TBB = SecondLastInst->getOperand(0).getMBB(); TBB = SecondLastInst->getOperand(0).getMBB();
I = LastInst; I = LastInst;
I->eraseFromParent(); if (AllowModify)
I->eraseFromParent();
return false; return false;
} }

View File

@ -100,7 +100,8 @@ namespace llvm {
virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
MachineBasicBlock *&FBB, MachineBasicBlock *&FBB,
SmallVectorImpl<MachineOperand> &Cond) const; SmallVectorImpl<MachineOperand> &Cond,
bool AllowModify) const;
virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const; virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const;

View File

@ -453,7 +453,8 @@ Mips::CondCode Mips::GetOppositeBranchCondition(Mips::CondCode CC)
bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
MachineBasicBlock *&TBB, MachineBasicBlock *&TBB,
MachineBasicBlock *&FBB, MachineBasicBlock *&FBB,
SmallVectorImpl<MachineOperand> &Cond) const SmallVectorImpl<MachineOperand> &Cond,
bool AllowModify) const
{ {
// If the block has no terminators, it just falls into the block after it. // If the block has no terminators, it just falls into the block after it.
MachineBasicBlock::iterator I = MBB.end(); MachineBasicBlock::iterator I = MBB.end();
@ -525,7 +526,8 @@ bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) { if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) {
TBB = SecondLastInst->getOperand(0).getMBB(); TBB = SecondLastInst->getOperand(0).getMBB();
I = LastInst; I = LastInst;
I->eraseFromParent(); if (AllowModify)
I->eraseFromParent();
return false; return false;
} }

View File

@ -166,7 +166,8 @@ public:
/// Branch Analysis /// Branch Analysis
virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
MachineBasicBlock *&FBB, MachineBasicBlock *&FBB,
SmallVectorImpl<MachineOperand> &Cond) const; SmallVectorImpl<MachineOperand> &Cond,
bool AllowModify) const;
virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const; virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const;
virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
MachineBasicBlock *FBB, MachineBasicBlock *FBB,

View File

@ -204,7 +204,8 @@ void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
// Branch analysis. // Branch analysis.
bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB, bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
MachineBasicBlock *&FBB, MachineBasicBlock *&FBB,
SmallVectorImpl<MachineOperand> &Cond) const { SmallVectorImpl<MachineOperand> &Cond,
bool AllowModify) const {
// If the block has no terminators, it just falls into the block after it. // If the block has no terminators, it just falls into the block after it.
MachineBasicBlock::iterator I = MBB.end(); MachineBasicBlock::iterator I = MBB.end();
if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
@ -253,7 +254,8 @@ bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
LastInst->getOpcode() == PPC::B) { LastInst->getOpcode() == PPC::B) {
TBB = SecondLastInst->getOperand(0).getMBB(); TBB = SecondLastInst->getOperand(0).getMBB();
I = LastInst; I = LastInst;
I->eraseFromParent(); if (AllowModify)
I->eraseFromParent();
return false; return false;
} }

View File

@ -104,7 +104,8 @@ public:
// Branch analysis. // Branch analysis.
virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
MachineBasicBlock *&FBB, MachineBasicBlock *&FBB,
SmallVectorImpl<MachineOperand> &Cond) const; SmallVectorImpl<MachineOperand> &Cond,
bool AllowModify) const;
virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const; virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const;
virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
MachineBasicBlock *FBB, MachineBasicBlock *FBB,

View File

@ -1488,7 +1488,8 @@ static bool isBrAnalysisUnpredicatedTerminator(const MachineInstr *MI,
bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
MachineBasicBlock *&TBB, MachineBasicBlock *&TBB,
MachineBasicBlock *&FBB, MachineBasicBlock *&FBB,
SmallVectorImpl<MachineOperand> &Cond) const { SmallVectorImpl<MachineOperand> &Cond,
bool AllowModify) const {
// Start from the bottom of the block and work up, examining the // Start from the bottom of the block and work up, examining the
// terminator instructions. // terminator instructions.
MachineBasicBlock::iterator I = MBB.end(); MachineBasicBlock::iterator I = MBB.end();
@ -1504,6 +1505,11 @@ bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
return true; return true;
// Handle unconditional branches. // Handle unconditional branches.
if (I->getOpcode() == X86::JMP) { if (I->getOpcode() == X86::JMP) {
if (!AllowModify) {
TBB = I->getOperand(0).getMBB();
return false;
}
// If the block has any instructions after a JMP, delete them. // If the block has any instructions after a JMP, delete them.
while (next(I) != MBB.end()) while (next(I) != MBB.end())
next(I)->eraseFromParent(); next(I)->eraseFromParent();

View File

@ -323,7 +323,8 @@ public:
virtual bool isUnpredicatedTerminator(const MachineInstr* MI) const; virtual bool isUnpredicatedTerminator(const MachineInstr* MI) const;
virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
MachineBasicBlock *&FBB, MachineBasicBlock *&FBB,
SmallVectorImpl<MachineOperand> &Cond) const; SmallVectorImpl<MachineOperand> &Cond,
bool AllowModify) const;
virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const; virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const;
virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
MachineBasicBlock *FBB, MachineBasicBlock *FBB,

View File

@ -228,8 +228,9 @@ static inline XCore::CondCode GetOppositeBranchCondition(XCore::CondCode CC)
/// ///
bool bool
XCoreInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, XCoreInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
MachineBasicBlock *&FBB, MachineBasicBlock *&FBB,
SmallVectorImpl<MachineOperand> &Cond) const { SmallVectorImpl<MachineOperand> &Cond,
bool AllowModify) const {
// If the block has no terminators, it just falls into the block after it. // If the block has no terminators, it just falls into the block after it.
MachineBasicBlock::iterator I = MBB.end(); MachineBasicBlock::iterator I = MBB.end();
if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
@ -288,7 +289,8 @@ XCoreInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
IsBRU(LastInst->getOpcode())) { IsBRU(LastInst->getOpcode())) {
TBB = SecondLastInst->getOperand(0).getMBB(); TBB = SecondLastInst->getOperand(0).getMBB();
I = LastInst; I = LastInst;
I->eraseFromParent(); if (AllowModify)
I->eraseFromParent();
return false; return false;
} }

View File

@ -56,7 +56,8 @@ public:
virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
MachineBasicBlock *&FBB, MachineBasicBlock *&FBB,
SmallVectorImpl<MachineOperand> &Cond) const; SmallVectorImpl<MachineOperand> &Cond,
bool AllowModify) const;
virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
MachineBasicBlock *FBB, MachineBasicBlock *FBB,