@@ -7259,7 +7259,10 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72597259 // Get the VPInstruction computing the reduction result in the middle block.
72607260 // The first operand may not be from the middle block if it is not connected
72617261 // to the scalar preheader. In that case, there's nothing to fix.
7262- auto *EpiRedResult = dyn_cast<VPInstruction>(EpiResumePhiR->getOperand (0 ));
7262+ VPValue *Incoming = EpiResumePhiR->getOperand (0 );
7263+ match (Incoming, VPlanPatternMatch::m_ZExtOrSExt (
7264+ VPlanPatternMatch::m_VPValue (Incoming)));
7265+ auto *EpiRedResult = dyn_cast<VPInstruction>(Incoming);
72637266 if (!EpiRedResult ||
72647267 (EpiRedResult->getOpcode () != VPInstruction::ComputeAnyOfResult &&
72657268 EpiRedResult->getOpcode () != VPInstruction::ComputeReductionResult &&
@@ -9195,28 +9198,6 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
91959198 PhiR->setOperand (1 , NewExitingVPV);
91969199 }
91979200
9198- // If the vector reduction can be performed in a smaller type, we truncate
9199- // then extend the loop exit value to enable InstCombine to evaluate the
9200- // entire expression in the smaller type.
9201- if (MinVF.isVector () && PhiTy != RdxDesc.getRecurrenceType () &&
9202- !RecurrenceDescriptor::isAnyOfRecurrenceKind (
9203- RdxDesc.getRecurrenceKind ())) {
9204- assert (!PhiR->isInLoop () && " Unexpected truncated inloop reduction!" );
9205- Type *RdxTy = RdxDesc.getRecurrenceType ();
9206- auto *Trunc =
9207- new VPWidenCastRecipe (Instruction::Trunc, NewExitingVPV, RdxTy);
9208- auto *Extnd =
9209- RdxDesc.isSigned ()
9210- ? new VPWidenCastRecipe (Instruction::SExt, Trunc, PhiTy)
9211- : new VPWidenCastRecipe (Instruction::ZExt, Trunc, PhiTy);
9212-
9213- Trunc->insertAfter (NewExitingVPV->getDefiningRecipe ());
9214- Extnd->insertAfter (Trunc);
9215- if (PhiR->getOperand (1 ) == NewExitingVPV)
9216- PhiR->setOperand (1 , Extnd->getVPSingleValue ());
9217- NewExitingVPV = Extnd;
9218- }
9219-
92209201 // We want code in the middle block to appear to execute on the location of
92219202 // the scalar loop's latch terminator because: (a) it is all compiler
92229203 // generated, (b) these instructions are always executed after evaluating
@@ -9255,6 +9236,31 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
92559236 Builder.createNaryOp (VPInstruction::ComputeReductionResult,
92569237 {PhiR, NewExitingVPV}, Flags, ExitDL);
92579238 }
9239+ // If the vector reduction can be performed in a smaller type, we truncate
9240+ // then extend the loop exit value to enable InstCombine to evaluate the
9241+ // entire expression in the smaller type.
9242+ if (MinVF.isVector () && PhiTy != RdxDesc.getRecurrenceType () &&
9243+ !RecurrenceDescriptor::isAnyOfRecurrenceKind (
9244+ RdxDesc.getRecurrenceKind ())) {
9245+ assert (!PhiR->isInLoop () && " Unexpected truncated inloop reduction!" );
9246+ Type *RdxTy = RdxDesc.getRecurrenceType ();
9247+ auto *Trunc =
9248+ new VPWidenCastRecipe (Instruction::Trunc, NewExitingVPV, RdxTy);
9249+ Instruction::CastOps ExtendOpc =
9250+ RdxDesc.isSigned () ? Instruction::SExt : Instruction::ZExt;
9251+ auto *Extnd = new VPWidenCastRecipe (ExtendOpc, Trunc, PhiTy);
9252+ Trunc->insertAfter (NewExitingVPV->getDefiningRecipe ());
9253+ Extnd->insertAfter (Trunc);
9254+ if (PhiR->getOperand (1 ) == NewExitingVPV)
9255+ PhiR->setOperand (1 , Extnd->getVPSingleValue ());
9256+
9257+ // Update ComputeReductionResult with the truncated exiting value and
9258+ // extend its result.
9259+ FinalReductionResult->setOperand (1 , Trunc);
9260+ FinalReductionResult =
9261+ Builder.createScalarCast (ExtendOpc, FinalReductionResult, PhiTy, {});
9262+ }
9263+
92589264 // Update all users outside the vector region.
92599265 OrigExitingVPV->replaceUsesWithIf (
92609266 FinalReductionResult, [FinalReductionResult](VPUser &User, unsigned ) {
0 commit comments