@@ -7209,6 +7209,13 @@ static void addRuntimeUnrollDisableMetaData(Loop *L) {
72097209 }
72107210}
72117211
7212+ static Value *getStartValueFromReductionResult (VPInstruction *RdxResult) {
7213+ using namespace VPlanPatternMatch ;
7214+ VPValue *StartVPV = RdxResult->getOperand (1 );
7215+ match (StartVPV, m_Freeze (m_VPValue (StartVPV)));
7216+ return StartVPV->getLiveInIRValue ();
7217+ }
7218+
72127219// If \p R is a ComputeReductionResult when vectorizing the epilog loop,
72137220// fix the reduction's scalar PHI node by adding the incoming value from the
72147221// main vector loop.
@@ -7217,7 +7224,8 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72177224 BasicBlock *BypassBlock) {
72187225 auto *EpiRedResult = dyn_cast<VPInstruction>(R);
72197226 if (!EpiRedResult ||
7220- (EpiRedResult->getOpcode () != VPInstruction::ComputeReductionResult &&
7227+ (EpiRedResult->getOpcode () != VPInstruction::ComputeAnyOfResult &&
7228+ EpiRedResult->getOpcode () != VPInstruction::ComputeReductionResult &&
72217229 EpiRedResult->getOpcode () != VPInstruction::ComputeFindLastIVResult))
72227230 return ;
72237231
@@ -7229,15 +7237,19 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72297237 EpiRedHeaderPhi->getStartValue ()->getUnderlyingValue ();
72307238 if (RecurrenceDescriptor::isAnyOfRecurrenceKind (
72317239 RdxDesc.getRecurrenceKind ())) {
7240+ Value *StartV = EpiRedResult->getOperand (1 )->getLiveInIRValue ();
7241+ (void )StartV;
72327242 auto *Cmp = cast<ICmpInst>(MainResumeValue);
72337243 assert (Cmp->getPredicate () == CmpInst::ICMP_NE &&
72347244 " AnyOf expected to start with ICMP_NE" );
7235- assert (Cmp->getOperand (1 ) == RdxDesc. getRecurrenceStartValue () &&
7245+ assert (Cmp->getOperand (1 ) == StartV &&
72367246 " AnyOf expected to start by comparing main resume value to original "
72377247 " start value" );
72387248 MainResumeValue = Cmp->getOperand (0 );
72397249 } else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind (
72407250 RdxDesc.getRecurrenceKind ())) {
7251+ Value *StartV = getStartValueFromReductionResult (EpiRedResult);
7252+ (void )StartV;
72417253 using namespace llvm ::PatternMatch;
72427254 Value *Cmp, *OrigResumeV, *CmpOp;
72437255 bool IsExpectedPattern =
@@ -7246,10 +7258,7 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72467258 m_Value (OrigResumeV))) &&
72477259 (match (Cmp, m_SpecificICmp (ICmpInst::ICMP_EQ, m_Specific (OrigResumeV),
72487260 m_Value (CmpOp))) &&
7249- (match (CmpOp,
7250- m_Freeze (m_Specific (RdxDesc.getRecurrenceStartValue ()))) ||
7251- (CmpOp == RdxDesc.getRecurrenceStartValue () &&
7252- isGuaranteedNotToBeUndefOrPoison (CmpOp))));
7261+ ((CmpOp == StartV && isGuaranteedNotToBeUndefOrPoison (CmpOp))));
72537262 assert (IsExpectedPattern && " Unexpected reduction resume pattern" );
72547263 (void )IsExpectedPattern;
72557264 MainResumeValue = OrigResumeV;
@@ -9184,7 +9193,10 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
91849193 OrigExitingVPV->replaceUsesWithIf (NewExitingVPV, [](VPUser &U, unsigned ) {
91859194 return isa<VPInstruction>(&U) &&
91869195 (cast<VPInstruction>(&U)->getOpcode () ==
9196+ VPInstruction::ComputeAnyOfResult ||
9197+ cast<VPInstruction>(&U)->getOpcode () ==
91879198 VPInstruction::ComputeReductionResult ||
9199+
91889200 cast<VPInstruction>(&U)->getOpcode () ==
91899201 VPInstruction::ComputeFindLastIVResult);
91909202 });
@@ -9236,6 +9248,12 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
92369248 FinalReductionResult =
92379249 Builder.createNaryOp (VPInstruction::ComputeFindLastIVResult,
92389250 {PhiR, Start, NewExitingVPV}, ExitDL);
9251+ } else if (RecurrenceDescriptor::isAnyOfRecurrenceKind (
9252+ RdxDesc.getRecurrenceKind ())) {
9253+ VPValue *Start = PhiR->getStartValue ();
9254+ FinalReductionResult =
9255+ Builder.createNaryOp (VPInstruction::ComputeAnyOfResult,
9256+ {PhiR, Start, NewExitingVPV}, ExitDL);
92399257 } else {
92409258 VPIRFlags Flags = RecurrenceDescriptor::isFloatingPointRecurrenceKind (
92419259 RdxDesc.getRecurrenceKind ())
@@ -9764,23 +9782,36 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
97649782 Value *ResumeV = nullptr ;
97659783 // TODO: Move setting of resume values to prepareToExecute.
97669784 if (auto *ReductionPhi = dyn_cast<VPReductionPHIRecipe>(&R)) {
9785+ auto *RdxResult =
9786+ cast<VPInstruction>(*find_if (ReductionPhi->users (), [](VPUser *U) {
9787+ auto *VPI = dyn_cast<VPInstruction>(U);
9788+ return VPI &&
9789+ (VPI->getOpcode () == VPInstruction::ComputeReductionResult ||
9790+ VPI->getOpcode () == VPInstruction::ComputeFindLastIVResult);
9791+ }));
97679792 ResumeV = cast<PHINode>(ReductionPhi->getUnderlyingInstr ())
97689793 ->getIncomingValueForBlock (L->getLoopPreheader ());
97699794 const RecurrenceDescriptor &RdxDesc =
97709795 ReductionPhi->getRecurrenceDescriptor ();
97719796 RecurKind RK = RdxDesc.getRecurrenceKind ();
97729797 if (RecurrenceDescriptor::isAnyOfRecurrenceKind (RK)) {
9798+ Value *StartV = RdxResult->getOperand (1 )->getLiveInIRValue ();
9799+ assert (RdxDesc.getRecurrenceStartValue () == StartV &&
9800+ " start value from ComputeAnyOfResult must match" );
9801+
97739802 // VPReductionPHIRecipes for AnyOf reductions expect a boolean as
97749803 // start value; compare the final value from the main vector loop
97759804 // to the start value.
97769805 BasicBlock *PBB = cast<Instruction>(ResumeV)->getParent ();
97779806 IRBuilder<> Builder (PBB, PBB->getFirstNonPHIIt ());
9778- ResumeV =
9779- Builder.CreateICmpNE (ResumeV, RdxDesc.getRecurrenceStartValue ());
9807+ ResumeV = Builder.CreateICmpNE (ResumeV, StartV);
97809808 } else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind (RK)) {
9781- ToFrozen[RdxDesc.getRecurrenceStartValue ()] =
9782- cast<PHINode>(ResumeV)->getIncomingValueForBlock (
9783- EPI.MainLoopIterationCountCheck );
9809+ Value *StartV = getStartValueFromReductionResult (RdxResult);
9810+ assert (RdxDesc.getRecurrenceStartValue () == StartV &&
9811+ " start value from ComputeFindLastIVResult must match" );
9812+
9813+ ToFrozen[StartV] = cast<PHINode>(ResumeV)->getIncomingValueForBlock (
9814+ EPI.MainLoopIterationCountCheck );
97849815
97859816 // VPReductionPHIRecipe for FindLastIV reductions requires an adjustment
97869817 // to the resume value. The resume value is adjusted to the sentinel
@@ -9790,8 +9821,7 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
97909821 // variable.
97919822 BasicBlock *ResumeBB = cast<Instruction>(ResumeV)->getParent ();
97929823 IRBuilder<> Builder (ResumeBB, ResumeBB->getFirstNonPHIIt ());
9793- Value *Cmp = Builder.CreateICmpEQ (
9794- ResumeV, ToFrozen[RdxDesc.getRecurrenceStartValue ()]);
9824+ Value *Cmp = Builder.CreateICmpEQ (ResumeV, ToFrozen[StartV]);
97959825 ResumeV =
97969826 Builder.CreateSelect (Cmp, RdxDesc.getSentinelValue (), ResumeV);
97979827 }
0 commit comments