@@ -7405,7 +7405,6 @@ static void createAndCollectMergePhiForReduction(
7405
7405
auto *PhiR = cast<VPReductionPHIRecipe>(RedResult->getOperand (0 ));
7406
7406
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor ();
7407
7407
7408
- TrackingVH<Value> ReductionStartValue = RdxDesc.getRecurrenceStartValue ();
7409
7408
Value *FinalValue =
7410
7409
State.get (RedResult, VPIteration (State.UF - 1 , VPLane::getFirstLane ()));
7411
7410
auto *ResumePhi =
@@ -7430,7 +7429,7 @@ static void createAndCollectMergePhiForReduction(
7430
7429
BCBlockPhi->addIncoming (ResumePhi->getIncomingValueForBlock (Incoming),
7431
7430
Incoming);
7432
7431
else
7433
- BCBlockPhi->addIncoming (ReductionStartValue , Incoming);
7432
+ BCBlockPhi->addIncoming (RdxDesc. getRecurrenceStartValue () , Incoming);
7434
7433
}
7435
7434
7436
7435
auto *OrigPhi = cast<PHINode>(PhiR->getUnderlyingValue ());
@@ -8854,6 +8853,10 @@ VPlanPtr LoopVectorizationPlanner::buildVPlan(VFRange &Range) {
8854
8853
// A ComputeReductionResult recipe is added to the middle block, also for
8855
8854
// in-loop reductions which compute their result in-loop, because generating
8856
8855
// the subsequent bc.merge.rdx phi is driven by ComputeReductionResult recipes.
8856
+ //
8857
+ // Adjust AnyOf reductions; replace the reduction phi for the selected value
8858
+ // with a boolean reduction phi node to check if the condition is true in any
8859
+ // iteration. The final value is selected by the final ComputeReductionResult.
8857
8860
void LoopVectorizationPlanner::adjustRecipesForReductions (
8858
8861
VPBasicBlock *LatchVPBB, VPlanPtr &Plan, VPRecipeBuilder &RecipeBuilder,
8859
8862
ElementCount MinVF) {
@@ -9027,6 +9030,41 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
9027
9030
continue ;
9028
9031
9029
9032
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor ();
9033
+ // Adjust AnyOf reductions; replace the reduction phi for the selected value
9034
+ // with a boolean reduction phi node to check if the condition is true in
9035
+ // any iteration. The final value is selected by the final
9036
+ // ComputeReductionResult.
9037
+ if (RecurrenceDescriptor::isAnyOfRecurrenceKind (
9038
+ RdxDesc.getRecurrenceKind ())) {
9039
+ auto *Select = cast<VPRecipeBase>(*find_if (PhiR->users (), [](VPUser *U) {
9040
+ return isa<VPWidenSelectRecipe>(U) ||
9041
+ (isa<VPReplicateRecipe>(U) &&
9042
+ cast<VPReplicateRecipe>(U)->getUnderlyingInstr ()->getOpcode () ==
9043
+ Instruction::Select);
9044
+ }));
9045
+ VPValue *Cmp = Select->getOperand (0 );
9046
+ // If the compare is checking the reduction PHI node, adjust it to check
9047
+ // the start value.
9048
+ if (VPRecipeBase *CmpR = Cmp->getDefiningRecipe ()) {
9049
+ for (unsigned I = 0 ; I != CmpR->getNumOperands (); ++I)
9050
+ if (CmpR->getOperand (I) == PhiR)
9051
+ CmpR->setOperand (I, PhiR->getStartValue ());
9052
+ }
9053
+ VPBuilder::InsertPointGuard Guard (Builder);
9054
+ Builder.setInsertPoint (Select);
9055
+
9056
+ // If the true value of the select is the reduction phi, the new value is
9057
+ // selected if the negated condition is true in any iteration.
9058
+ if (Select->getOperand (1 ) == PhiR)
9059
+ Cmp = Builder.createNot (Cmp);
9060
+ VPValue *Or = Builder.createOr (PhiR, Cmp);
9061
+ Select->getVPSingleValue ()->replaceAllUsesWith (Or);
9062
+
9063
+ // Convert the reduction phi to operate on bools.
9064
+ PhiR->setOperand (0 , Plan->getVPValueOrAddLiveIn (ConstantInt::getFalse (
9065
+ OrigLoop->getHeader ()->getContext ())));
9066
+ }
9067
+
9030
9068
// If tail is folded by masking, introduce selects between the phi
9031
9069
// and the live-out instruction of each reduction, at the beginning of the
9032
9070
// dedicated latch block.
@@ -9059,7 +9097,9 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
9059
9097
// then extend the loop exit value to enable InstCombine to evaluate the
9060
9098
// entire expression in the smaller type.
9061
9099
Type *PhiTy = PhiR->getStartValue ()->getLiveInIRValue ()->getType ();
9062
- if (MinVF.isVector () && PhiTy != RdxDesc.getRecurrenceType ()) {
9100
+ if (MinVF.isVector () && PhiTy != RdxDesc.getRecurrenceType () &&
9101
+ !RecurrenceDescriptor::isAnyOfRecurrenceKind (
9102
+ RdxDesc.getRecurrenceKind ())) {
9063
9103
assert (!PhiR->isInLoop () && " Unexpected truncated inloop reduction!" );
9064
9104
Type *RdxTy = RdxDesc.getRecurrenceType ();
9065
9105
auto *Trunc =
0 commit comments