Index: lib/Transforms/Vectorize/LoopVectorize.cpp =================================================================== --- lib/Transforms/Vectorize/LoopVectorize.cpp +++ lib/Transforms/Vectorize/LoopVectorize.cpp @@ -467,7 +467,7 @@ /// Create a new induction variable inside L. PHINode *createInductionVariable(Loop *L, Value *Start, Value *End, - Value *Step, Instruction *DL); + Value *Step, DebugLoc *OldInductionDL); /// Handle all cross-iteration phis in the header. void fixCrossIterationPHIs(); @@ -2558,9 +2558,8 @@ PredicatedInstructions.push_back(Cloned); } -PHINode *InnerLoopVectorizer::createInductionVariable(Loop *L, Value *Start, - Value *End, Value *Step, - Instruction *DL) { +PHINode *InnerLoopVectorizer::createInductionVariable( + Loop *L, Value *Start, Value *End, Value *Step, DebugLoc *OldInductionDL) { BasicBlock *Header = L->getHeader(); BasicBlock *Latch = L->getLoopLatch(); // As we're just creating this loop, it's possible no latch exists @@ -2569,12 +2568,11 @@ Latch = Header; IRBuilder<> Builder(&*Header->getFirstInsertionPt()); - Instruction *OldInst = getDebugLocFromInstOrOperands(OldInduction); - setDebugLocFromInst(Builder, OldInst); + Builder.SetCurrentDebugLocation(*OldInductionDL); auto *Induction = Builder.CreatePHI(Start->getType(), 2, "index"); Builder.SetInsertPoint(Latch->getTerminator()); - setDebugLocFromInst(Builder, OldInst); + Builder.SetCurrentDebugLocation(*OldInductionDL); // Create i+1 and fill the PHINode. Value *Next = Builder.CreateAdd(Induction, Step, "index.next"); @@ -2886,14 +2884,27 @@ // faster. emitMemRuntimeChecks(Lp, ScalarPH); + // OldInduction could be nullptr. In that case we want the Debug Location + // from the old induction phi to be passed on to the new one. + DebugLoc InductionDL; + if (!OldInduction) { + for (const auto &phi : OldBasicBlock->phis()) { + if (Legal->isInductionPhi(&phi)) { + InductionDL = phi.getDebugLoc(); + break; + } + } + } else { + InductionDL = getDebugLocFromInstOrOperands(OldInduction)->getDebugLoc(); + } + // Generate the induction variable. // The loop step is equal to the vectorization factor (num of SIMD elements) // times the unroll factor (num of SIMD instructions). Value *CountRoundDown = getOrCreateVectorTripCount(Lp); Constant *Step = ConstantInt::get(IdxTy, VF * UF); Induction = - createInductionVariable(Lp, StartIdx, CountRoundDown, Step, - getDebugLocFromInstOrOperands(OldInduction)); + createInductionVariable(Lp, StartIdx, CountRoundDown, Step, &InductionDL); // We are going to resume the execution of the scalar loop. // Go over all of the induction variables that we found and fix the Index: test/Transforms/LoopVectorize/discriminator.ll =================================================================== --- test/Transforms/LoopVectorize/discriminator.ll +++ test/Transforms/LoopVectorize/discriminator.ll @@ -47,8 +47,8 @@ ;LOOPUNROLL_5: discriminator: 21 ; When unrolling after loop vectorize, both vec_body and remainder loop ; are unrolled. -;LOOPVEC_UNROLL: discriminator: 385 ;LOOPVEC_UNROLL: discriminator: 9 +;LOOPVEC_UNROLL: discriminator: 385 ;DBG_VALUE: ![[DBG]] = {{.*}}, scope: ![[TOP]] !llvm.dbg.cu = !{!0} Index: test/Transforms/LoopVectorize/i8-induction.ll =================================================================== --- test/Transforms/LoopVectorize/i8-induction.ll +++ test/Transforms/LoopVectorize/i8-induction.ll @@ -10,7 +10,9 @@ ; Check that the induction phis and adds have debug location. ; ; DEBUGLOC-LABEL: vector.body: -; DEBUGLOC: %vec.ind = phi {{.*}}, !dbg ![[DbgLoc:[0-9]+]] +; DEBUGLOC: %index = phi {{.*}}, !dbg ![[DbgLoc:[0-9]+]] +; DEBUGLOC: %vec.ind = phi {{.*}}, !dbg ![[DbgLoc]] +; DEBUGLOC: %index.next = add {{.*}}, !dbg ![[DbgLoc]] ; DEBUGLOC: %vec.ind.next = add {{.*}}, !dbg ![[DbgLoc]] scalar.ph: