diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h --- a/llvm/lib/Transforms/Vectorize/VPlan.h +++ b/llvm/lib/Transforms/Vectorize/VPlan.h @@ -1805,9 +1805,6 @@ }; /// Recipe to expand a SCEV expression. -/// TODO: Currently the recipe always expands the expression in the loop -/// pre-header, but the recipe is currently placed in the header; place it in -/// the pre-header once the latter is modeled in VPlan as a VPBasicBlock. class VPExpandSCEVRecipe : public VPRecipeBase, public VPValue { const SCEV *Expr; ScalarEvolution &SE; 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 @@ -1474,9 +1474,8 @@ const DataLayout &DL = State.CFG.PrevBB->getModule()->getDataLayout(); SCEVExpander Exp(SE, DL, "induction"); - BasicBlock *VectorPH = State.CFG.getPreheaderBBFor(this); - Value *Res = - Exp.expandCodeFor(Expr, Expr->getType(), VectorPH->getTerminator()); + Value *Res = Exp.expandCodeFor(Expr, Expr->getType(), + &*State.Builder.GetInsertPoint()); for (unsigned Part = 0, UF = State.UF; Part < UF; ++Part) State.set(this, Res, Part); diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp --- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp +++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp @@ -437,12 +437,9 @@ IV->getStartValue(), Step, TruncI ? TruncI->getType() : nullptr); HeaderVPBB->insert(Steps, HeaderVPBB->getFirstNonPhi()); - if (Step->getDef()) { - // TODO: Place the step in the preheader, once it is explicitly modeled in - // VPlan. - HeaderVPBB->insert(cast(Step->getDef()), - HeaderVPBB->getFirstNonPhi()); - } + if (Step->getDef()) + Plan.getEntry()->getEntryBasicBlock()->appendRecipe( + cast(Step->getDef())); // If there are no vector users of IV, simply update all users to use Step // instead. diff --git a/llvm/test/Transforms/LoopVectorize/vplan-printing.ll b/llvm/test/Transforms/LoopVectorize/vplan-printing.ll --- a/llvm/test/Transforms/LoopVectorize/vplan-printing.ll +++ b/llvm/test/Transforms/LoopVectorize/vplan-printing.ll @@ -418,6 +418,7 @@ ; CHECK-NEXT: Live-in vp<%0> = vector-trip-count ; CHECK-EMPTY: ; CHECK-NEXT: vector.ph: +; CHECK-NEXT: EMIT vp<[[EXP_SCEV:%.+]]> = EXPAND SCEV (1 + (%y /u 492802768830814060)) ; CHECK-NEXT: Successor(s): vector loop ; CHECK-EMPTY: ; CHECK-NEXT: vector loop: { @@ -426,7 +427,6 @@ ; CHECK-NEXT: WIDEN-INDUCTION\l" + ; CHECK-NEXT: " %iv = phi %iv.next, 0\l" + ; CHECK-NEXT: " ir<%v2> -; CHECK-NEXT: EMIT vp<[[EXP_SCEV:%.+]]> = EXPAND SCEV (1 + (%y /u 492802768830814060)) ; CHECK-NEXT: vp<[[STEPS:%.+]]> = SCALAR-STEPS vp<[[CAN_IV]]>, ir<0>, vp<[[EXP_SCEV]]> ; CHECK-NEXT: WIDEN ir<%v3> = add ir<%v2>, ir<1> ; CHECK-NEXT: REPLICATE ir<%gep> = getelementptr ir<%ptr>, vp<[[STEPS]]>