[MachineBasicBlock] Add getFirstNonDebugInstr to complement getLastNonDebugInstr

Use it in CodeGen where applicable. No functionality change intended.

llvm-svn: 240414
This commit is contained in:
Benjamin Kramer 2015-06-23 14:47:29 +00:00
parent 9c956b33d7
commit 6b568964ba
5 changed files with 37 additions and 70 deletions

View File

@ -469,6 +469,13 @@ public:
/// and return an instr_iterator instead. /// and return an instr_iterator instead.
instr_iterator getFirstInstrTerminator(); instr_iterator getFirstInstrTerminator();
/// getFirstNonDebugInstr - returns an iterator to the first non-debug
/// instruction in the basic block, or end()
iterator getFirstNonDebugInstr();
const_iterator getFirstNonDebugInstr() const {
return const_cast<MachineBasicBlock *>(this)->getFirstNonDebugInstr();
}
/// getLastNonDebugInstr - returns an iterator to the last non-debug /// getLastNonDebugInstr - returns an iterator to the last non-debug
/// instruction in the basic block, or end() /// instruction in the basic block, or end()
iterator getLastNonDebugInstr(); iterator getLastNonDebugInstr();

View File

@ -304,17 +304,9 @@ static unsigned HashMachineInstr(const MachineInstr *MI) {
/// HashEndOfMBB - Hash the last instruction in the MBB. /// HashEndOfMBB - Hash the last instruction in the MBB.
static unsigned HashEndOfMBB(const MachineBasicBlock *MBB) { static unsigned HashEndOfMBB(const MachineBasicBlock *MBB) {
MachineBasicBlock::const_iterator I = MBB->end(); MachineBasicBlock::const_iterator I = MBB->getLastNonDebugInstr();
if (I == MBB->begin()) if (I == MBB->end())
return 0; // Empty MBB. return 0;
--I;
// Skip debug info so it will not affect codegen.
while (I->isDebugValue()) {
if (I == MBB->begin())
return 0; // MBB empty except for debug info.
--I;
}
return HashMachineInstr(I); return HashMachineInstr(I);
} }
@ -1123,25 +1115,15 @@ bool BranchFolder::OptimizeBranches(MachineFunction &MF) {
// Blocks should be considered empty if they contain only debug info; // Blocks should be considered empty if they contain only debug info;
// else the debug info would affect codegen. // else the debug info would affect codegen.
static bool IsEmptyBlock(MachineBasicBlock *MBB) { static bool IsEmptyBlock(MachineBasicBlock *MBB) {
if (MBB->empty()) return MBB->getFirstNonDebugInstr() == MBB->end();
return true;
for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
MBBI!=MBBE; ++MBBI) {
if (!MBBI->isDebugValue())
return false;
}
return true;
} }
// Blocks with only debug info and branches should be considered the same // Blocks with only debug info and branches should be considered the same
// as blocks with only branches. // as blocks with only branches.
static bool IsBranchOnlyBlock(MachineBasicBlock *MBB) { static bool IsBranchOnlyBlock(MachineBasicBlock *MBB) {
MachineBasicBlock::iterator MBBI, MBBE; MachineBasicBlock::iterator I = MBB->getFirstNonDebugInstr();
for (MBBI = MBB->begin(), MBBE = MBB->end(); MBBI!=MBBE; ++MBBI) { assert(I != MBB->end() && "empty block!");
if (!MBBI->isDebugValue()) return I->isBranch();
break;
}
return (MBBI->isBranch());
} }
/// IsBetterFallthrough - Return true if it would be clearly better to /// IsBetterFallthrough - Return true if it would be clearly better to
@ -1154,36 +1136,24 @@ static bool IsBetterFallthrough(MachineBasicBlock *MBB1,
// MBB1 doesn't, we prefer to fall through into MBB1. This allows us to // MBB1 doesn't, we prefer to fall through into MBB1. This allows us to
// optimize branches that branch to either a return block or an assert block // optimize branches that branch to either a return block or an assert block
// into a fallthrough to the return. // into a fallthrough to the return.
if (IsEmptyBlock(MBB1) || IsEmptyBlock(MBB2)) return false; MachineBasicBlock::iterator MBB1I = MBB1->getLastNonDebugInstr();
MachineBasicBlock::iterator MBB2I = MBB2->getLastNonDebugInstr();
if (MBB1I == MBB1->end() || MBB2I == MBB2->end())
return false;
// If there is a clear successor ordering we make sure that one block // If there is a clear successor ordering we make sure that one block
// will fall through to the next // will fall through to the next
if (MBB1->isSuccessor(MBB2)) return true; if (MBB1->isSuccessor(MBB2)) return true;
if (MBB2->isSuccessor(MBB1)) return false; if (MBB2->isSuccessor(MBB1)) return false;
// Neither block consists entirely of debug info (per IsEmptyBlock check),
// so we needn't test for falling off the beginning here.
MachineBasicBlock::iterator MBB1I = --MBB1->end();
while (MBB1I->isDebugValue())
--MBB1I;
MachineBasicBlock::iterator MBB2I = --MBB2->end();
while (MBB2I->isDebugValue())
--MBB2I;
return MBB2I->isCall() && !MBB1I->isCall(); return MBB2I->isCall() && !MBB1I->isCall();
} }
/// getBranchDebugLoc - Find and return, if any, the DebugLoc of the branch /// getBranchDebugLoc - Find and return, if any, the DebugLoc of the branch
/// instructions on the block. Always use the DebugLoc of the first /// instructions on the block.
/// branching instruction found unless its absent, in which case use the
/// DebugLoc of the second if present.
static DebugLoc getBranchDebugLoc(MachineBasicBlock &MBB) { static DebugLoc getBranchDebugLoc(MachineBasicBlock &MBB) {
MachineBasicBlock::iterator I = MBB.end(); MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
if (I == MBB.begin()) if (I != MBB.end() && I->isBranch())
return DebugLoc();
--I;
while (I->isDebugValue() && I != MBB.begin())
--I;
if (I->isBranch())
return I->getDebugLoc(); return I->getDebugLoc();
return DebugLoc(); return DebugLoc();
} }
@ -1408,19 +1378,10 @@ ReoptimizeBlock:
// If the only things remaining in the block are debug info, remove these // If the only things remaining in the block are debug info, remove these
// as well, so this will behave the same as an empty block in non-debug // as well, so this will behave the same as an empty block in non-debug
// mode. // mode.
if (!MBB->empty()) { if (IsEmptyBlock(MBB)) {
bool NonDebugInfoFound = false; // Make the block empty, losing the debug info (we could probably
for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); // improve this in some cases.)
I != E; ++I) { MBB->erase(MBB->begin(), MBB->end());
if (!I->isDebugValue()) {
NonDebugInfoFound = true;
break;
}
}
if (!NonDebugInfoFound)
// Make the block empty, losing the debug info (we could probably
// improve this in some cases.)
MBB->erase(MBB->begin(), MBB->end());
} }
// If this block is just an unconditional branch to CurTBB, we can // If this block is just an unconditional branch to CurTBB, we can
// usually completely eliminate the block. The only case we cannot // usually completely eliminate the block. The only case we cannot

View File

@ -1355,15 +1355,9 @@ bool IfConverter::IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
Redefs.addLiveIns(BBI1->BB); Redefs.addLiveIns(BBI1->BB);
// Remove the duplicated instructions at the beginnings of both paths. // Remove the duplicated instructions at the beginnings of both paths.
MachineBasicBlock::iterator DI1 = BBI1->BB->begin();
MachineBasicBlock::iterator DI2 = BBI2->BB->begin();
MachineBasicBlock::iterator DIE1 = BBI1->BB->end();
MachineBasicBlock::iterator DIE2 = BBI2->BB->end();
// Skip dbg_value instructions // Skip dbg_value instructions
while (DI1 != DIE1 && DI1->isDebugValue()) MachineBasicBlock::iterator DI1 = BBI1->BB->getFirstNonDebugInstr();
++DI1; MachineBasicBlock::iterator DI2 = BBI2->BB->getFirstNonDebugInstr();
while (DI2 != DIE2 && DI2->isDebugValue())
++DI2;
BBI1->NonPredSize -= NumDups1; BBI1->NonPredSize -= NumDups1;
BBI2->NonPredSize -= NumDups1; BBI2->NonPredSize -= NumDups1;

View File

@ -180,6 +180,14 @@ MachineBasicBlock::instr_iterator MachineBasicBlock::getFirstInstrTerminator() {
return I; return I;
} }
MachineBasicBlock::iterator MachineBasicBlock::getFirstNonDebugInstr() {
// Skip over begin-of-block dbg_value instructions.
iterator I = begin(), E = end();
while (I != E && I->isDebugValue())
++I;
return I;
}
MachineBasicBlock::iterator MachineBasicBlock::getLastNonDebugInstr() { MachineBasicBlock::iterator MachineBasicBlock::getLastNonDebugInstr() {
// Skip over end-of-block dbg_value instructions. // Skip over end-of-block dbg_value instructions.
instr_iterator B = instr_begin(), I = instr_end(); instr_iterator B = instr_begin(), I = instr_end();

View File

@ -627,11 +627,8 @@ TailDuplicatePass::isSimpleBB(MachineBasicBlock *TailBB) {
return false; return false;
if (TailBB->pred_empty()) if (TailBB->pred_empty())
return false; return false;
MachineBasicBlock::iterator I = TailBB->begin(); MachineBasicBlock::iterator I = TailBB->getFirstNonDebugInstr();
MachineBasicBlock::iterator E = TailBB->end(); if (I == TailBB->end())
while (I != E && I->isDebugValue())
++I;
if (I == E)
return true; return true;
return I->isUnconditionalBranch(); return I->isUnconditionalBranch();
} }