@@ -709,15 +709,20 @@ bool VPlanTransforms::handleFMaxReductionsWithoutFastMath(VPlan &Plan) {
709709 // Find the first index of with the maximum value. This is used to extract the
710710 // lane with the final max value and is needed to handle signed zeros and NaNs
711711 // 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 ()));
716721
717722 // Create mask for lanes that have the max value and use it to mask out
718723 // indices that don't contain maximum values.
719724 auto *MaskFinalMaxValue = Builder.createNaryOp (
720- Instruction::FCmp, {OrigRdxResult ->getOperand (1 ), OrigRdxResult },
725+ Instruction::FCmp, {MaxResult ->getOperand (1 ), MaxResult },
721726 VPIRFlags (CmpInst::FCMP_OEQ));
722727 auto *IndicesWithMaxValue = Builder.createNaryOp (
723728 Instruction::Select, {MaskFinalMaxValue, MinIdxSel, UMinSentinel});
@@ -731,11 +736,10 @@ bool VPlanTransforms::handleFMaxReductionsWithoutFastMath(VPlan &Plan) {
731736 Builder.createNaryOp (Instruction::URem, {FirstMaxIdx, &Plan.getVFxUF ()});
732737
733738 // 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+ });
740744 return true ;
741745}
0 commit comments