summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>2016-08-17 01:54:41 +0000
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>2016-08-17 01:54:41 +0000
commit362d120488722bf91824f8e74748459525d10614 (patch)
tree4cce742540e5e290c9c28203de44b17661219825 /llvm/lib
parent67c5885d091b6630040ee0f47af0e4a119389007 (diff)
downloadbcm5719-llvm-362d120488722bf91824f8e74748459525d10614.tar.gz
bcm5719-llvm-362d120488722bf91824f8e74748459525d10614.zip
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
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/IR/Instruction.cpp9
-rw-r--r--llvm/lib/Transforms/Scalar/IndVarSimplify.cpp6
2 files changed, 10 insertions, 5 deletions
diff --git a/llvm/lib/IR/Instruction.cpp b/llvm/lib/IR/Instruction.cpp
index ed08f85c60b..cfddbf78f0c 100644
--- a/llvm/lib/IR/Instruction.cpp
+++ b/llvm/lib/IR/Instruction.cpp
@@ -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) {
diff --git a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
index b014747cbc1..a11bef5d784 100644
--- a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
+++ b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
@@ -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();
}
}
OpenPOWER on IntegriCloud