diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp --- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp +++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -9589,22 +9589,19 @@ NewPointerPhi->addIncoming(ScalarStartValue, State.CFG.VectorPreHeader); // A pointer induction, performed by using a gep - BasicBlock *LoopLatch = - State.LI->getLoopFor(State.CFG.PrevBB)->getLoopLatch(); - - const DataLayout &DL = LoopLatch->getModule()->getDataLayout(); - Instruction *InductionLoc = LoopLatch->getTerminator(); + const DataLayout &DL = Phi->getModule()->getDataLayout(); const SCEV *ScalarStep = IndDesc.getStep(); SCEVExpander Exp(SE, DL, "induction"); - Value *ScalarStepValue = Exp.expandCodeFor(ScalarStep, PhiType, InductionLoc); + Value *ScalarStepValue = + Exp.expandCodeFor(ScalarStep, PhiType, &*State.Builder.GetInsertPoint()); Value *RuntimeVF = getRuntimeVF(State.Builder, PhiType, State.VF); Value *NumUnrolledElems = State.Builder.CreateMul(RuntimeVF, ConstantInt::get(PhiType, State.UF)); Value *InductionGEP = GetElementPtrInst::Create( IndDesc.getElementType(), NewPointerPhi, State.Builder.CreateMul(ScalarStepValue, NumUnrolledElems), "ptr.ind", - InductionLoc); - NewPointerPhi->addIncoming(InductionGEP, LoopLatch); + &*State.Builder.GetInsertPoint()); + NewPointerPhi->addIncoming(InductionGEP, State.CFG.VectorPreHeader); // Create UF many actual address geps that use the pointer // phi as base and a vectorized version of the step value diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp --- a/llvm/lib/Transforms/Vectorize/VPlan.cpp +++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp @@ -984,13 +984,23 @@ for (VPRecipeBase &R : Header->phis()) { // Skip phi-like recipes that generate their backedege values themselves. // TODO: Model their backedge values explicitly. - if (isa(&R) || isa(&R)) + if (isa(&R)) continue; - // Set the correct incoming block for backedge values and move induction to - // latch. - if (auto *IndR = dyn_cast(&R)) { - auto *Phi = cast(State->get(IndR, 0)); + if (isa(&R) || + isa(&R)) { + PHINode *Phi = nullptr; + if (auto *WidenPhi = dyn_cast(&R)) { + if (all_of(WidenPhi->users(), [WidenPhi](const VPUser *U) { + return cast(U)->usesScalars(WidenPhi); + })) + continue; + + auto *GEP = cast(State->get(WidenPhi, 0)); + Phi = cast(GEP->getPointerOperand()); + } else + Phi = cast(State->get(R.getVPSingleValue(), 0)); + Phi->setIncomingBlock(1, VectorLatchBB); // Move the last step to the end of the latch block. This ensures