@@ -625,13 +625,14 @@ static SmallVector<VPUser *> collectUsersRecursively(VPValue *V) {
625
625
static void legalizeAndOptimizeInductions (VPlan &Plan) {
626
626
using namespace llvm ::VPlanPatternMatch;
627
627
VPBasicBlock *HeaderVPBB = Plan.getVectorLoopRegion ()->getEntryBasicBlock ();
628
- bool HasOnlyVectorVFs = !Plan.hasScalarVFOnly ();
629
- VPBuilder Builder (HeaderVPBB, HeaderVPBB->getFirstNonPhi ());
630
- for (VPRecipeBase &Phi : HeaderVPBB->phis ()) {
631
- auto *PhiR = dyn_cast<VPWidenInductionRecipe>(&Phi);
632
- if (!PhiR)
633
- continue ;
628
+ SmallVector<VPWidenInductionRecipe *, 4 > InductionPhis;
629
+ for (VPRecipeBase &R : HeaderVPBB->phis ())
630
+ if (auto *IV = dyn_cast<VPWidenInductionRecipe>(&R))
631
+ InductionPhis.push_back (IV);
634
632
633
+ bool HasOnlyVectorVFs = !Plan.hasScalarVFOnly ();
634
+ VPBuilder Builder;
635
+ for (VPWidenInductionRecipe *PhiR : reverse (InductionPhis)) {
635
636
// Try to narrow wide and replicating recipes to uniform recipes, based on
636
637
// VPlan analysis.
637
638
// TODO: Apply to all recipes in the future, to replace legacy uniformity
@@ -641,7 +642,8 @@ static void legalizeAndOptimizeInductions(VPlan &Plan) {
641
642
auto *Def = dyn_cast<VPSingleDefRecipe>(U);
642
643
auto *RepR = dyn_cast<VPReplicateRecipe>(U);
643
644
// Skip recipes that shouldn't be narrowed.
644
- if (!Def || !isa<VPReplicateRecipe, VPWidenRecipe>(Def) ||
645
+ if (!Def ||
646
+ !isa<VPReplicateRecipe, VPWidenRecipe, VPWidenGEPRecipe>(Def) ||
645
647
Def->getNumUsers () == 0 || !Def->getUnderlyingValue () ||
646
648
(RepR && (RepR->isUniform () || RepR->isPredicated ())))
647
649
continue ;
@@ -655,11 +657,13 @@ static void legalizeAndOptimizeInductions(VPlan &Plan) {
655
657
Def->operands (), /* IsUniform*/ true );
656
658
Clone->insertAfter (Def);
657
659
Def->replaceAllUsesWith (Clone);
660
+ Def->eraseFromParent ();
658
661
}
659
662
663
+ Builder.setInsertPoint (HeaderVPBB, HeaderVPBB->getFirstNonPhi ());
660
664
// Replace wide pointer inductions which have only their scalars used by
661
665
// PtrAdd(IndStart, ScalarIVSteps (0, Step)).
662
- if (auto *PtrIV = dyn_cast<VPWidenPointerInductionRecipe>(&Phi )) {
666
+ if (auto *PtrIV = dyn_cast<VPWidenPointerInductionRecipe>(PhiR )) {
663
667
if (!PtrIV->onlyScalarsGenerated (Plan.hasScalableVF ()))
664
668
continue ;
665
669
@@ -680,7 +684,7 @@ static void legalizeAndOptimizeInductions(VPlan &Plan) {
680
684
681
685
// Replace widened induction with scalar steps for users that only use
682
686
// scalars.
683
- auto *WideIV = cast<VPWidenIntOrFpInductionRecipe>(&Phi );
687
+ auto *WideIV = cast<VPWidenIntOrFpInductionRecipe>(PhiR );
684
688
if (HasOnlyVectorVFs && none_of (WideIV->users (), [WideIV](VPUser *U) {
685
689
return U->usesScalars (WideIV);
686
690
}))
0 commit comments