Index: include/llvm/IR/IRBuilder.h =================================================================== --- include/llvm/IR/IRBuilder.h +++ include/llvm/IR/IRBuilder.h @@ -188,6 +188,24 @@ /// \brief Set the fast-math flags to be used with generated fp-math operators void SetFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; } + class Marker { + BasicBlock::iterator Prev; + BasicBlock *BB; + public: + Marker(BasicBlock::iterator InsertionPoint) : BB(nullptr) { + if (InsertionPoint == InsertionPoint->getParent()->begin()) + BB = InsertionPoint->getParent(); + else + Prev = std::prev(InsertionPoint); + } + BasicBlock::iterator get() const { + if (BB) + return BB->begin(); + else + return std::next(Prev); + } + }; + //===--------------------------------------------------------------------===// // RAII helpers. //===--------------------------------------------------------------------===// Index: lib/Transforms/Vectorize/LoopVectorize.cpp =================================================================== --- lib/Transforms/Vectorize/LoopVectorize.cpp +++ lib/Transforms/Vectorize/LoopVectorize.cpp @@ -2050,6 +2050,8 @@ } IRBuilder<> ChkBuilder(Loc); + IRBuilder<>::Marker BeforeInsertionMarker(Loc); + // Our instructions might fold to a constant. Value *MemoryRuntimeCheck = nullptr; for (unsigned i = 0; i < NumPointers; ++i) { @@ -2102,6 +2104,14 @@ ConstantInt::getTrue(Ctx)); ChkBuilder.Insert(Check, "memcheck.conflict"); FirstInst = getFirstInst(FirstInst, Check, Loc); + + Instruction *AnotherFirst = BeforeInsertionMarker.get(); + assert(std::find_if(BasicBlock::iterator(AnotherFirst), + Loc->getParent()->end(), + [&](Instruction &I) { + return &I == FirstInst; + }) != Loc->getParent()->end()); + return std::make_pair(FirstInst, Check); }