Scalar: Avoid dereferencing end() in IndVarSimplify

IndVarSimplify::sinkUnusedInvariants calls
BasicBlock::getFirstInsertionPt on the ExitBlock and moves instructions
before it.  This can return end(), so it's not safe to dereference.  Add
an iterator-based overload to Instruction::moveBefore to avoid the UB.

llvm-svn: 278886
This commit is contained in:
Duncan P. N. Exon Smith 2016-08-17 01:54:41 +00:00
parent 67c5885d09
commit 362d120488
3 changed files with 15 additions and 5 deletions

View File

@ -94,6 +94,11 @@ public:
/// the basic block that MovePos lives in, right before MovePos.
void moveBefore(Instruction *MovePos);
/// Unlink this instruction and insert into BB before I.
///
/// \pre I is a valid iterator into BB.
void moveBefore(BasicBlock &BB, SymbolTableList<Instruction>::iterator I);
//===--------------------------------------------------------------------===//
// Subclass classification.
//===--------------------------------------------------------------------===//

View File

@ -92,8 +92,13 @@ void Instruction::insertAfter(Instruction *InsertPos) {
/// Unlink this instruction from its current basic block and insert it into the
/// basic block that MovePos lives in, right before MovePos.
void Instruction::moveBefore(Instruction *MovePos) {
MovePos->getParent()->getInstList().splice(
MovePos->getIterator(), getParent()->getInstList(), getIterator());
moveBefore(*MovePos->getParent(), MovePos->getIterator());
}
void Instruction::moveBefore(BasicBlock &BB,
SymbolTableList<Instruction>::iterator I) {
assert(I == BB.end() || I->getParent() == &BB);
BB.getInstList().splice(I, getParent()->getInstList(), getIterator());
}
void Instruction::setHasNoUnsignedWrap(bool b) {

View File

@ -2058,7 +2058,7 @@ void IndVarSimplify::sinkUnusedInvariants(Loop *L) {
BasicBlock *Preheader = L->getLoopPreheader();
if (!Preheader) return;
Instruction *InsertPt = &*ExitBlock->getFirstInsertionPt();
BasicBlock::iterator InsertPt = ExitBlock->getFirstInsertionPt();
BasicBlock::iterator I(Preheader->getTerminator());
while (I != Preheader->begin()) {
--I;
@ -2127,9 +2127,9 @@ void IndVarSimplify::sinkUnusedInvariants(Loop *L) {
Done = true;
}
ToMove->moveBefore(InsertPt);
ToMove->moveBefore(*ExitBlock, InsertPt);
if (Done) break;
InsertPt = ToMove;
InsertPt = ToMove->getIterator();
}
}