@@ -709,15 +709,20 @@ bool VPlanTransforms::handleFMaxReductionsWithoutFastMath(VPlan &Plan) {
709
709
// Find the first index of with the maximum value. This is used to extract the
710
710
// lane with the final max value and is needed to handle signed zeros and NaNs
711
711
// in the input.
712
- auto *MiddleVPBB = Plan.getMiddleBlock ();
713
- auto *OrigRdxResult = cast<VPSingleDefRecipe>(&MiddleVPBB->front ());
714
- VPBuilder Builder (OrigRdxResult->getParent (),
715
- std::next (OrigRdxResult->getIterator ()));
712
+ auto *MaxResult = find_singleton<VPSingleDefRecipe>(
713
+ RedPhiR->users (), [](VPUser *U, bool ) -> VPSingleDefRecipe * {
714
+ auto *VPI = dyn_cast<VPInstruction>(U);
715
+ if (VPI && VPI->getOpcode () == VPInstruction::ComputeReductionResult)
716
+ return VPI;
717
+ return nullptr ;
718
+ });
719
+ VPBuilder Builder (MaxResult->getParent (),
720
+ std::next (MaxResult->getIterator ()));
716
721
717
722
// Create mask for lanes that have the max value and use it to mask out
718
723
// indices that don't contain maximum values.
719
724
auto *MaskFinalMaxValue = Builder.createNaryOp (
720
- Instruction::FCmp, {OrigRdxResult ->getOperand (1 ), OrigRdxResult },
725
+ Instruction::FCmp, {MaxResult ->getOperand (1 ), MaxResult },
721
726
VPIRFlags (CmpInst::FCMP_OEQ));
722
727
auto *IndicesWithMaxValue = Builder.createNaryOp (
723
728
Instruction::Select, {MaskFinalMaxValue, MinIdxSel, UMinSentinel});
@@ -731,11 +736,10 @@ bool VPlanTransforms::handleFMaxReductionsWithoutFastMath(VPlan &Plan) {
731
736
Builder.createNaryOp (Instruction::URem, {FirstMaxIdx, &Plan.getVFxUF ()});
732
737
733
738
// Extract the final max value and update the users.
734
- auto *Res = Builder.createNaryOp (
735
- VPInstruction::ExtractLane, {FirstMaxLane, OrigRdxResult->getOperand (1 )});
736
- OrigRdxResult->replaceUsesWithIf (Res,
737
- [MaskFinalMaxValue](VPUser &U, unsigned ) {
738
- return &U != MaskFinalMaxValue;
739
- });
739
+ auto *Res = Builder.createNaryOp (VPInstruction::ExtractLane,
740
+ {FirstMaxLane, MaxResult->getOperand (1 )});
741
+ MaxResult->replaceUsesWithIf (Res, [MaskFinalMaxValue](VPUser &U, unsigned ) {
742
+ return &U != MaskFinalMaxValue;
743
+ });
740
744
return true ;
741
745
}
0 commit comments