@@ -467,11 +467,12 @@ class InnerLoopVectorizer {
467
467
ElementCount MinProfitableTripCount,
468
468
unsigned UnrollFactor, LoopVectorizationLegality *LVL,
469
469
LoopVectorizationCostModel *CM, BlockFrequencyInfo *BFI,
470
- ProfileSummaryInfo *PSI, GeneratedRTChecks &RTChecks)
470
+ ProfileSummaryInfo *PSI, GeneratedRTChecks &RTChecks,
471
+ VPlan &Plan)
471
472
: OrigLoop(OrigLoop), PSE(PSE), LI(LI), DT(DT), TLI(TLI), TTI(TTI),
472
473
AC (AC), ORE(ORE), VF(VecWidth), UF(UnrollFactor),
473
474
Builder(PSE.getSE()->getContext()), Legal(LVL), Cost(CM), BFI(BFI),
474
- PSI(PSI), RTChecks(RTChecks) {
475
+ PSI(PSI), RTChecks(RTChecks), Plan(Plan) {
475
476
// Query this against the original loop and save it here because the profile
476
477
// of the original loop header may change as the transformation happens.
477
478
OptForSizeBasedOnProfile = llvm::shouldOptimizeForSize (
@@ -522,7 +523,7 @@ class InnerLoopVectorizer {
522
523
// / and the resume values can come from an additional bypass block, the \p
523
524
// / AdditionalBypass pair provides information about the bypass block and the
524
525
// / end value on the edge from bypass to this loop.
525
- PHINode * createInductionResumeValue (
526
+ void createInductionResumeValue (
526
527
PHINode *OrigPhi, const InductionDescriptor &ID, Value *Step,
527
528
ArrayRef<BasicBlock *> BypassBlocks,
528
529
std::pair<BasicBlock *, Value *> AdditionalBypass = {nullptr , nullptr });
@@ -535,6 +536,11 @@ class InnerLoopVectorizer {
535
536
// / count of the original loop for both main loop and epilogue vectorization.
536
537
void setTripCount (Value *TC) { TripCount = TC; }
537
538
539
+ std::pair<BasicBlock *, Value *>
540
+ getInductionBypassValue (PHINode *OrigPhi) const {
541
+ return InductionBypassValues.find (OrigPhi)->second ;
542
+ }
543
+
538
544
protected:
539
545
friend class LoopVectorizationPlanner ;
540
546
@@ -674,6 +680,11 @@ class InnerLoopVectorizer {
674
680
// / Structure to hold information about generated runtime checks, responsible
675
681
// / for cleaning the checks, if vectorization turns out unprofitable.
676
682
GeneratedRTChecks &RTChecks;
683
+
684
+ // / Mapping of induction phis to their bypass values and bypass blocks.
685
+ DenseMap<PHINode *, std::pair<BasicBlock *, Value *>> InductionBypassValues;
686
+
687
+ VPlan &Plan;
677
688
};
678
689
679
690
// / Encapsulate information regarding vectorization of a loop and its epilogue.
@@ -715,10 +726,10 @@ class InnerLoopAndEpilogueVectorizer : public InnerLoopVectorizer {
715
726
OptimizationRemarkEmitter *ORE, EpilogueLoopVectorizationInfo &EPI,
716
727
LoopVectorizationLegality *LVL, llvm::LoopVectorizationCostModel *CM,
717
728
BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI,
718
- GeneratedRTChecks &Checks)
729
+ GeneratedRTChecks &Checks, VPlan &Plan )
719
730
: InnerLoopVectorizer(OrigLoop, PSE, LI, DT, TLI, TTI, AC, ORE,
720
731
EPI.MainLoopVF, EPI.MainLoopVF, EPI.MainLoopUF, LVL,
721
- CM, BFI, PSI, Checks),
732
+ CM, BFI, PSI, Checks, Plan ),
722
733
EPI (EPI) {}
723
734
724
735
// Override this function to handle the more complex control flow around the
@@ -755,9 +766,9 @@ class EpilogueVectorizerMainLoop : public InnerLoopAndEpilogueVectorizer {
755
766
OptimizationRemarkEmitter *ORE, EpilogueLoopVectorizationInfo &EPI,
756
767
LoopVectorizationLegality *LVL, llvm::LoopVectorizationCostModel *CM,
757
768
BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI,
758
- GeneratedRTChecks &Check)
769
+ GeneratedRTChecks &Check, VPlan &Plan )
759
770
: InnerLoopAndEpilogueVectorizer(OrigLoop, PSE, LI, DT, TLI, TTI, AC, ORE,
760
- EPI, LVL, CM, BFI, PSI, Check) {}
771
+ EPI, LVL, CM, BFI, PSI, Check, Plan ) {}
761
772
// / Implements the interface for creating a vectorized skeleton using the
762
773
// / *main loop* strategy (ie the first pass of vplan execution).
763
774
std::pair<BasicBlock *, Value *>
@@ -789,9 +800,9 @@ class EpilogueVectorizerEpilogueLoop : public InnerLoopAndEpilogueVectorizer {
789
800
OptimizationRemarkEmitter *ORE, EpilogueLoopVectorizationInfo &EPI,
790
801
LoopVectorizationLegality *LVL, llvm::LoopVectorizationCostModel *CM,
791
802
BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI,
792
- GeneratedRTChecks &Checks)
803
+ GeneratedRTChecks &Checks, VPlan &Plan )
793
804
: InnerLoopAndEpilogueVectorizer(OrigLoop, PSE, LI, DT, TLI, TTI, AC, ORE,
794
- EPI, LVL, CM, BFI, PSI, Checks) {
805
+ EPI, LVL, CM, BFI, PSI, Checks, Plan ) {
795
806
TripCount = EPI.TripCount ;
796
807
}
797
808
// / Implements the interface for creating a vectorized skeleton using the
@@ -2586,7 +2597,18 @@ void InnerLoopVectorizer::createVectorLoopSkeleton(StringRef Prefix) {
2586
2597
nullptr , Twine (Prefix) + " scalar.ph" );
2587
2598
}
2588
2599
2589
- PHINode *InnerLoopVectorizer::createInductionResumeValue (
2600
+ static void addOperandToPhiInVPIRBasicBlock (VPIRBasicBlock *VPBB, PHINode *P,
2601
+ VPValue *Op) {
2602
+ for (VPRecipeBase &R : *VPBB) {
2603
+ auto *IRI = cast<VPIRInstruction>(&R);
2604
+ if (&IRI->getInstruction () == P) {
2605
+ IRI->addOperand (Op);
2606
+ break ;
2607
+ }
2608
+ }
2609
+ }
2610
+
2611
+ void InnerLoopVectorizer::createInductionResumeValue (
2590
2612
PHINode *OrigPhi, const InductionDescriptor &II, Value *Step,
2591
2613
ArrayRef<BasicBlock *> BypassBlocks,
2592
2614
std::pair<BasicBlock *, Value *> AdditionalBypass) {
@@ -2621,27 +2643,28 @@ PHINode *InnerLoopVectorizer::createInductionResumeValue(
2621
2643
}
2622
2644
}
2623
2645
2624
- // Create phi nodes to merge from the backedge-taken check block.
2625
- PHINode *BCResumeVal =
2626
- PHINode::Create (OrigPhi->getType (), 3 , " bc.resume.val" ,
2627
- LoopScalarPreHeader->getFirstNonPHIIt ());
2628
- // Copy original phi DL over to the new one.
2629
- BCResumeVal->setDebugLoc (OrigPhi->getDebugLoc ());
2646
+ VPBasicBlock *MiddleVPBB =
2647
+ cast<VPBasicBlock>(Plan.getVectorLoopRegion ()->getSingleSuccessor ());
2630
2648
2631
- // The new PHI merges the original incoming value, in case of a bypass,
2632
- // or the value at the end of the vectorized loop.
2633
- BCResumeVal->addIncoming (EndValue, LoopMiddleBlock);
2649
+ VPBasicBlock *ScalarPHVPBB = nullptr ;
2650
+ if (MiddleVPBB->getNumSuccessors () == 2 ) {
2651
+ // Order is strict: first is the exit block, second is the scalar preheader.
2652
+ ScalarPHVPBB = cast<VPBasicBlock>(MiddleVPBB->getSuccessors ()[1 ]);
2653
+ } else {
2654
+ ScalarPHVPBB = cast<VPBasicBlock>(MiddleVPBB->getSingleSuccessor ());
2655
+ }
2634
2656
2635
- // Fix the scalar body counter (PHI node).
2636
- // The old induction's phi node in the scalar body needs the truncated
2637
- // value.
2638
- for (BasicBlock *BB : BypassBlocks)
2639
- BCResumeVal-> addIncoming (II. getStartValue ( ), BB );
2657
+ VPBuilder ScalarPHBuilder (ScalarPHVPBB);
2658
+ auto *ResumePhiRecipe = ScalarPHBuilder. createNaryOp (
2659
+ VPInstruction::ResumePhi,
2660
+ {Plan. getOrAddLiveIn (EndValue), Plan. getOrAddLiveIn (II. getStartValue ())},
2661
+ OrigPhi-> getDebugLoc ( ), " bc.resume.val " );
2640
2662
2641
- if (AdditionalBypass.first )
2642
- BCResumeVal->setIncomingValueForBlock (AdditionalBypass.first ,
2643
- EndValueFromAdditionalBypass);
2644
- return BCResumeVal;
2663
+ auto *ScalarLoopHeader =
2664
+ cast<VPIRBasicBlock>(ScalarPHVPBB->getSingleSuccessor ());
2665
+ addOperandToPhiInVPIRBasicBlock (ScalarLoopHeader, OrigPhi, ResumePhiRecipe);
2666
+ InductionBypassValues[OrigPhi] = {AdditionalBypass.first ,
2667
+ EndValueFromAdditionalBypass};
2645
2668
}
2646
2669
2647
2670
// / Return the expanded step for \p ID using \p ExpandedSCEVs to look up SCEV
@@ -2674,10 +2697,8 @@ void InnerLoopVectorizer::createInductionResumeValues(
2674
2697
for (const auto &InductionEntry : Legal->getInductionVars ()) {
2675
2698
PHINode *OrigPhi = InductionEntry.first ;
2676
2699
const InductionDescriptor &II = InductionEntry.second ;
2677
- PHINode *BCResumeVal = createInductionResumeValue (
2678
- OrigPhi, II, getExpandedStep (II, ExpandedSCEVs), LoopBypassBlocks,
2679
- AdditionalBypass);
2680
- OrigPhi->setIncomingValueForBlock (LoopScalarPreHeader, BCResumeVal);
2700
+ createInductionResumeValue (OrigPhi, II, getExpandedStep (II, ExpandedSCEVs),
2701
+ LoopBypassBlocks, AdditionalBypass);
2681
2702
}
2682
2703
}
2683
2704
@@ -7805,6 +7826,25 @@ EpilogueVectorizerMainLoop::createEpilogueVectorizedLoopSkeleton(
7805
7826
// the second pass for the scalar loop. The induction resume values for the
7806
7827
// inductions in the epilogue loop are created before executing the plan for
7807
7828
// the epilogue loop.
7829
+ for (VPRecipeBase &R :
7830
+ Plan.getVectorLoopRegion ()->getEntryBasicBlock ()->phis ()) {
7831
+ // Create induction resume values for both widened pointer and
7832
+ // integer/fp inductions and update the start value of the induction
7833
+ // recipes to use the resume value.
7834
+ PHINode *IndPhi = nullptr ;
7835
+ const InductionDescriptor *ID;
7836
+ if (auto *Ind = dyn_cast<VPWidenPointerInductionRecipe>(&R)) {
7837
+ IndPhi = cast<PHINode>(Ind->getUnderlyingValue ());
7838
+ ID = &Ind->getInductionDescriptor ();
7839
+ } else if (auto *WidenInd = dyn_cast<VPWidenIntOrFpInductionRecipe>(&R)) {
7840
+ IndPhi = WidenInd->getPHINode ();
7841
+ ID = &WidenInd->getInductionDescriptor ();
7842
+ } else
7843
+ continue ;
7844
+
7845
+ createInductionResumeValue (IndPhi, *ID, getExpandedStep (*ID, ExpandedSCEVs),
7846
+ LoopBypassBlocks);
7847
+ }
7808
7848
7809
7849
return {LoopVectorPreHeader, nullptr };
7810
7850
}
@@ -8963,14 +9003,9 @@ static void addLiveOutsForFirstOrderRecurrences(
8963
9003
VPInstruction::ResumePhi, {Resume, FOR->getStartValue ()}, {},
8964
9004
" scalar.recur.init" );
8965
9005
auto *FORPhi = cast<PHINode>(FOR->getUnderlyingInstr ());
8966
- for (VPRecipeBase &R :
8967
- *cast<VPIRBasicBlock>(ScalarPHVPBB->getSingleSuccessor ())) {
8968
- auto *IRI = cast<VPIRInstruction>(&R);
8969
- if (&IRI->getInstruction () == FORPhi) {
8970
- IRI->addOperand (ResumePhiRecipe);
8971
- break ;
8972
- }
8973
- }
9006
+ addOperandToPhiInVPIRBasicBlock (
9007
+ cast<VPIRBasicBlock>(ScalarPHVPBB->getSingleSuccessor ()), FORPhi,
9008
+ ResumePhiRecipe);
8974
9009
8975
9010
// Now update VPIRInstructions modeling LCSSA phis in the exit block.
8976
9011
// Extract the penultimate value of the recurrence and use it as operand for
@@ -9707,7 +9742,7 @@ static bool processLoopInVPlanNativePath(
9707
9742
GeneratedRTChecks Checks (PSE, DT, LI, TTI, F->getDataLayout (),
9708
9743
AddBranchWeights);
9709
9744
InnerLoopVectorizer LB (L, PSE, LI, DT, TLI, TTI, AC, ORE, VF.Width ,
9710
- VF.Width , 1 , LVL, &CM, BFI, PSI, Checks);
9745
+ VF.Width , 1 , LVL, &CM, BFI, PSI, Checks, BestPlan );
9711
9746
LLVM_DEBUG (dbgs () << " Vectorizing outer loop in \" "
9712
9747
<< L->getHeader ()->getParent ()->getName () << " \"\n " );
9713
9748
LVP.executePlan (VF.Width , 1 , BestPlan, LB, DT, false );
@@ -10195,11 +10230,11 @@ bool LoopVectorizePass::processLoop(Loop *L) {
10195
10230
assert (IC > 1 && " interleave count should not be 1 or 0" );
10196
10231
// If we decided that it is not legal to vectorize the loop, then
10197
10232
// interleave it.
10233
+ VPlan &BestPlan = LVP.getPlanFor (VF.Width );
10198
10234
InnerLoopVectorizer Unroller (
10199
10235
L, PSE, LI, DT, TLI, TTI, AC, ORE, ElementCount::getFixed (1 ),
10200
- ElementCount::getFixed (1 ), IC, &LVL, &CM, BFI, PSI, Checks);
10236
+ ElementCount::getFixed (1 ), IC, &LVL, &CM, BFI, PSI, Checks, BestPlan );
10201
10237
10202
- VPlan &BestPlan = LVP.getPlanFor (VF.Width );
10203
10238
LVP.executePlan (VF.Width , IC, BestPlan, Unroller, DT, false );
10204
10239
10205
10240
ORE->emit ([&]() {
@@ -10221,10 +10256,11 @@ bool LoopVectorizePass::processLoop(Loop *L) {
10221
10256
// to be vectorized by executing the plan (potentially with a different
10222
10257
// factor) again shortly afterwards.
10223
10258
EpilogueLoopVectorizationInfo EPI (VF.Width , IC, EpilogueVF.Width , 1 );
10259
+ std::unique_ptr<VPlan> BestMainPlan (BestPlan.duplicate ());
10224
10260
EpilogueVectorizerMainLoop MainILV (L, PSE, LI, DT, TLI, TTI, AC, ORE,
10225
- EPI, &LVL, &CM, BFI, PSI, Checks);
10261
+ EPI, &LVL, &CM, BFI, PSI, Checks,
10262
+ *BestMainPlan);
10226
10263
10227
- std::unique_ptr<VPlan> BestMainPlan (BestPlan.duplicate ());
10228
10264
auto ExpandedSCEVs = LVP.executePlan (EPI.MainLoopVF , EPI.MainLoopUF ,
10229
10265
*BestMainPlan, MainILV, DT, true );
10230
10266
++LoopsVectorized;
@@ -10233,11 +10269,11 @@ bool LoopVectorizePass::processLoop(Loop *L) {
10233
10269
// edges from the first pass.
10234
10270
EPI.MainLoopVF = EPI.EpilogueVF ;
10235
10271
EPI.MainLoopUF = EPI.EpilogueUF ;
10272
+ VPlan &BestEpiPlan = LVP.getPlanFor (EPI.EpilogueVF );
10236
10273
EpilogueVectorizerEpilogueLoop EpilogILV (L, PSE, LI, DT, TLI, TTI, AC,
10237
10274
ORE, EPI, &LVL, &CM, BFI, PSI,
10238
- Checks);
10275
+ Checks, BestEpiPlan );
10239
10276
10240
- VPlan &BestEpiPlan = LVP.getPlanFor (EPI.EpilogueVF );
10241
10277
VPRegionBlock *VectorLoop = BestEpiPlan.getVectorLoopRegion ();
10242
10278
VPBasicBlock *Header = VectorLoop->getEntryBasicBlock ();
10243
10279
Header->setName (" vec.epilog.vector.body" );
@@ -10286,23 +10322,16 @@ bool LoopVectorizePass::processLoop(Loop *L) {
10286
10322
RdxDesc.getRecurrenceStartValue ());
10287
10323
}
10288
10324
} else {
10289
- // Create induction resume values for both widened pointer and
10290
- // integer/fp inductions and update the start value of the induction
10291
- // recipes to use the resume value.
10325
+ // Retrive the induction resume values for wide inductions from
10326
+ // their original phi nodes in the scalar loop
10292
10327
PHINode *IndPhi = nullptr ;
10293
- const InductionDescriptor *ID;
10294
10328
if (auto *Ind = dyn_cast<VPWidenPointerInductionRecipe>(&R)) {
10295
10329
IndPhi = cast<PHINode>(Ind->getUnderlyingValue ());
10296
- ID = &Ind->getInductionDescriptor ();
10297
10330
} else {
10298
10331
auto *WidenInd = cast<VPWidenIntOrFpInductionRecipe>(&R);
10299
10332
IndPhi = WidenInd->getPHINode ();
10300
- ID = &WidenInd->getInductionDescriptor ();
10301
10333
}
10302
-
10303
- ResumeV = MainILV.createInductionResumeValue (
10304
- IndPhi, *ID, getExpandedStep (*ID, ExpandedSCEVs),
10305
- {EPI.MainLoopIterationCountCheck });
10334
+ ResumeV = IndPhi->getIncomingValueForBlock (L->getLoopPreheader ());
10306
10335
}
10307
10336
assert (ResumeV && " Must have a resume value" );
10308
10337
VPValue *StartVal = BestEpiPlan.getOrAddLiveIn (ResumeV);
@@ -10314,13 +10343,19 @@ bool LoopVectorizePass::processLoop(Loop *L) {
10314
10343
LVP.executePlan (EPI.EpilogueVF , EPI.EpilogueUF , BestEpiPlan, EpilogILV,
10315
10344
DT, true , &ExpandedSCEVs);
10316
10345
++LoopsEpilogueVectorized;
10346
+ BasicBlock *PH = L->getLoopPreheader ();
10317
10347
10348
+ for (const auto &[IVPhi, _] : LVL.getInductionVars ()) {
10349
+ auto *Inc = cast<PHINode>(IVPhi->getIncomingValueForBlock (PH));
10350
+ const auto &[BB, V] = EpilogILV.getInductionBypassValue (IVPhi);
10351
+ Inc->setIncomingValueForBlock (BB, V);
10352
+ }
10318
10353
if (!MainILV.areSafetyChecksAdded ())
10319
10354
DisableRuntimeUnroll = true ;
10320
10355
} else {
10321
10356
InnerLoopVectorizer LB (L, PSE, LI, DT, TLI, TTI, AC, ORE, VF.Width ,
10322
10357
VF.MinProfitableTripCount , IC, &LVL, &CM, BFI,
10323
- PSI, Checks);
10358
+ PSI, Checks, BestPlan );
10324
10359
LVP.executePlan (VF.Width , IC, BestPlan, LB, DT, false );
10325
10360
++LoopsVectorized;
10326
10361
0 commit comments