Skip to content

Commit 904dbdb

Browse files
committed
Merge 64124 frm mainline.
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. llvm-svn: 64518
1 parent 7792a83 commit 904dbdb

16 files changed

+60
-30
lines changed

llvm/include/llvm/Target/TargetInstrInfo.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,13 @@ class TargetInstrInfo {
195195
/// Note that RemoveBranch and InsertBranch must be implemented to support
196196
/// cases where this method returns success.
197197
///
198+
/// If AllowModify is true, then this routine is allowed to modify the basic
199+
/// block (e.g. delete instructions after the unconditional branch).
200+
///
198201
virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
199202
MachineBasicBlock *&FBB,
200-
SmallVectorImpl<MachineOperand> &Cond) const {
203+
SmallVectorImpl<MachineOperand> &Cond,
204+
bool AllowModify = false) const {
201205
return true;
202206
}
203207

llvm/lib/CodeGen/BranchFolding.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ bool BranchFolder::runOnMachineFunction(MachineFunction &MF) {
191191
for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; I++) {
192192
MachineBasicBlock *MBB = I, *TBB = 0, *FBB = 0;
193193
SmallVector<MachineOperand, 4> Cond;
194-
if (!TII->AnalyzeBranch(*MBB, TBB, FBB, Cond))
194+
if (!TII->AnalyzeBranch(*MBB, TBB, FBB, Cond, true))
195195
EverMadeChange |= MBB->CorrectExtraCFGEdges(TBB, FBB, !Cond.empty());
196196
EverMadeChange |= OptimizeImpDefsBlock(MBB);
197197
}
@@ -434,7 +434,7 @@ static void FixTail(MachineBasicBlock* CurMBB, MachineBasicBlock *SuccBB,
434434
MachineBasicBlock *TBB = 0, *FBB = 0;
435435
SmallVector<MachineOperand, 4> Cond;
436436
if (I != MF->end() &&
437-
!TII->AnalyzeBranch(*CurMBB, TBB, FBB, Cond)) {
437+
!TII->AnalyzeBranch(*CurMBB, TBB, FBB, Cond, true)) {
438438
MachineBasicBlock *NextBB = I;
439439
if (TBB == NextBB && !Cond.empty() && !FBB) {
440440
if (!TII->ReverseBranchCondition(Cond)) {
@@ -711,7 +711,7 @@ bool BranchFolder::TailMergeBlocks(MachineFunction &MF) {
711711
continue;
712712
MachineBasicBlock *TBB = 0, *FBB = 0;
713713
SmallVector<MachineOperand, 4> Cond;
714-
if (!TII->AnalyzeBranch(*PBB, TBB, FBB, Cond)) {
714+
if (!TII->AnalyzeBranch(*PBB, TBB, FBB, Cond, true)) {
715715
// Failing case: IBB is the target of a cbr, and
716716
// we cannot reverse the branch.
717717
SmallVector<MachineOperand, 4> NewCond(Cond);
@@ -845,7 +845,7 @@ bool BranchFolder::CanFallThrough(MachineBasicBlock *CurBB,
845845
bool BranchFolder::CanFallThrough(MachineBasicBlock *CurBB) {
846846
MachineBasicBlock *TBB = 0, *FBB = 0;
847847
SmallVector<MachineOperand, 4> Cond;
848-
bool CurUnAnalyzable = TII->AnalyzeBranch(*CurBB, TBB, FBB, Cond);
848+
bool CurUnAnalyzable = TII->AnalyzeBranch(*CurBB, TBB, FBB, Cond, true);
849849
return CanFallThrough(CurBB, CurUnAnalyzable, TBB, FBB, Cond);
850850
}
851851

@@ -910,7 +910,7 @@ void BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
910910
MachineBasicBlock *PriorTBB = 0, *PriorFBB = 0;
911911
SmallVector<MachineOperand, 4> PriorCond;
912912
bool PriorUnAnalyzable =
913-
TII->AnalyzeBranch(PrevBB, PriorTBB, PriorFBB, PriorCond);
913+
TII->AnalyzeBranch(PrevBB, PriorTBB, PriorFBB, PriorCond, true);
914914
if (!PriorUnAnalyzable) {
915915
// If the CFG for the prior block has extra edges, remove them.
916916
MadeChange |= PrevBB.CorrectExtraCFGEdges(PriorTBB, PriorFBB,
@@ -1023,7 +1023,7 @@ void BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
10231023
// Analyze the branch in the current block.
10241024
MachineBasicBlock *CurTBB = 0, *CurFBB = 0;
10251025
SmallVector<MachineOperand, 4> CurCond;
1026-
bool CurUnAnalyzable = TII->AnalyzeBranch(*MBB, CurTBB, CurFBB, CurCond);
1026+
bool CurUnAnalyzable= TII->AnalyzeBranch(*MBB, CurTBB, CurFBB, CurCond, true);
10271027
if (!CurUnAnalyzable) {
10281028
// If the CFG for the prior block has extra edges, remove them.
10291029
MadeChange |= MBB->CorrectExtraCFGEdges(CurTBB, CurFBB, !CurCond.empty());

llvm/lib/Target/ARM/ARMInstrInfo.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ ARMInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
338338
// Branch analysis.
339339
bool ARMInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
340340
MachineBasicBlock *&FBB,
341-
SmallVectorImpl<MachineOperand> &Cond) const {
341+
SmallVectorImpl<MachineOperand> &Cond,
342+
bool AllowModify) const {
342343
// If the block has no terminators, it just falls into the block after it.
343344
MachineBasicBlock::iterator I = MBB.end();
344345
if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
@@ -388,7 +389,8 @@ bool ARMInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
388389
(LastOpc == ARM::B || LastOpc == ARM::tB)) {
389390
TBB = SecondLastInst->getOperand(0).getMBB();
390391
I = LastInst;
391-
I->eraseFromParent();
392+
if (AllowModify)
393+
I->eraseFromParent();
392394
return false;
393395
}
394396

@@ -399,7 +401,8 @@ bool ARMInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
399401
SecondLastOpc == ARM::BR_JTadd || SecondLastOpc==ARM::tBR_JTr) &&
400402
(LastOpc == ARM::B || LastOpc == ARM::tB)) {
401403
I = LastInst;
402-
I->eraseFromParent();
404+
if (AllowModify)
405+
I->eraseFromParent();
403406
return true;
404407
}
405408

llvm/lib/Target/ARM/ARMInstrInfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ class ARMInstrInfo : public TargetInstrInfoImpl {
176176
// Branch analysis.
177177
virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
178178
MachineBasicBlock *&FBB,
179-
SmallVectorImpl<MachineOperand> &Cond) const;
179+
SmallVectorImpl<MachineOperand> &Cond,
180+
bool AllowModify) const;
180181
virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const;
181182
virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
182183
MachineBasicBlock *FBB,

llvm/lib/Target/Alpha/AlphaInstrInfo.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,9 @@ static unsigned AlphaRevCondCode(unsigned Opcode) {
321321

322322
// Branch analysis.
323323
bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
324-
MachineBasicBlock *&FBB,
325-
SmallVectorImpl<MachineOperand> &Cond) const {
324+
MachineBasicBlock *&FBB,
325+
SmallVectorImpl<MachineOperand> &Cond,
326+
bool AllowModify) const {
326327
// If the block has no terminators, it just falls into the block after it.
327328
MachineBasicBlock::iterator I = MBB.end();
328329
if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
@@ -373,7 +374,8 @@ bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TB
373374
LastInst->getOpcode() == Alpha::BR) {
374375
TBB = SecondLastInst->getOperand(0).getMBB();
375376
I = LastInst;
376-
I->eraseFromParent();
377+
if (AllowModify)
378+
I->eraseFromParent();
377379
return false;
378380
}
379381

llvm/lib/Target/Alpha/AlphaInstrInfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ class AlphaInstrInfo : public TargetInstrInfoImpl {
8383

8484
bool AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
8585
MachineBasicBlock *&FBB,
86-
SmallVectorImpl<MachineOperand> &Cond) const;
86+
SmallVectorImpl<MachineOperand> &Cond,
87+
bool AllowModify) const;
8788
unsigned RemoveBranch(MachineBasicBlock &MBB) const;
8889
void insertNoop(MachineBasicBlock &MBB,
8990
MachineBasicBlock::iterator MI) const;

llvm/lib/Target/CellSPU/SPUInstrInfo.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,8 @@ SPUInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
532532
bool
533533
SPUInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
534534
MachineBasicBlock *&FBB,
535-
SmallVectorImpl<MachineOperand> &Cond) const {
535+
SmallVectorImpl<MachineOperand> &Cond,
536+
bool AllowModify) const {
536537
// If the block has no terminators, it just falls into the block after it.
537538
MachineBasicBlock::iterator I = MBB.end();
538539
if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
@@ -583,7 +584,8 @@ SPUInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
583584
if (isUncondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
584585
TBB = SecondLastInst->getOperand(0).getMBB();
585586
I = LastInst;
586-
I->eraseFromParent();
587+
if (AllowModify)
588+
I->eraseFromParent();
587589
return false;
588590
}
589591

llvm/lib/Target/CellSPU/SPUInstrInfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ namespace llvm {
104104

105105
virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
106106
MachineBasicBlock *&FBB,
107-
SmallVectorImpl<MachineOperand> &Cond) const;
107+
SmallVectorImpl<MachineOperand> &Cond,
108+
bool AllowModify) const;
108109

109110
virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const;
110111

llvm/lib/Target/Mips/MipsInstrInfo.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,8 @@ Mips::CondCode Mips::GetOppositeBranchCondition(Mips::CondCode CC)
453453
bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
454454
MachineBasicBlock *&TBB,
455455
MachineBasicBlock *&FBB,
456-
SmallVectorImpl<MachineOperand> &Cond) const
456+
SmallVectorImpl<MachineOperand> &Cond,
457+
bool AllowModify) const
457458
{
458459
// If the block has no terminators, it just falls into the block after it.
459460
MachineBasicBlock::iterator I = MBB.end();
@@ -525,7 +526,8 @@ bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
525526
if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) {
526527
TBB = SecondLastInst->getOperand(0).getMBB();
527528
I = LastInst;
528-
I->eraseFromParent();
529+
if (AllowModify)
530+
I->eraseFromParent();
529531
return false;
530532
}
531533

llvm/lib/Target/Mips/MipsInstrInfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ class MipsInstrInfo : public TargetInstrInfoImpl {
166166
/// Branch Analysis
167167
virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
168168
MachineBasicBlock *&FBB,
169-
SmallVectorImpl<MachineOperand> &Cond) const;
169+
SmallVectorImpl<MachineOperand> &Cond,
170+
bool AllowModify) const;
170171
virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const;
171172
virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
172173
MachineBasicBlock *FBB,

0 commit comments

Comments
 (0)