@@ -670,15 +670,20 @@ bool VPlanTransforms::handleFMaxReductionsWithoutFastMath(VPlan &Plan) {
670
670
// Find the first index of with the maximum value. This is used to extract the
671
671
// lane with the final max value and is needed to handle signed zeros and NaNs
672
672
// in the input.
673
- auto *MiddleVPBB = Plan.getMiddleBlock ();
674
- auto *OrigRdxResult = cast<VPSingleDefRecipe>(&MiddleVPBB->front ());
675
- VPBuilder Builder (OrigRdxResult->getParent (),
676
- std::next (OrigRdxResult->getIterator ()));
673
+ auto *MaxResult = find_singleton<VPSingleDefRecipe>(
674
+ RedPhiR->users (), [](VPUser *U, bool ) -> VPSingleDefRecipe * {
675
+ auto *VPI = dyn_cast<VPInstruction>(U);
676
+ if (VPI && VPI->getOpcode () == VPInstruction::ComputeReductionResult)
677
+ return VPI;
678
+ return nullptr ;
679
+ });
680
+ VPBuilder Builder (MaxResult->getParent (),
681
+ std::next (MaxResult->getIterator ()));
677
682
678
683
// Create mask for lanes that have the max value and use it to mask out
679
684
// indices that don't contain maximum values.
680
685
auto *MaskFinalMaxValue = Builder.createNaryOp (
681
- Instruction::FCmp, {OrigRdxResult ->getOperand (1 ), OrigRdxResult },
686
+ Instruction::FCmp, {MaxResult ->getOperand (1 ), MaxResult },
682
687
VPIRFlags (CmpInst::FCMP_OEQ));
683
688
auto *IndicesWithMaxValue = Builder.createNaryOp (
684
689
Instruction::Select, {MaskFinalMaxValue, MinIdxSel, UMinSentinel});
@@ -692,11 +697,10 @@ bool VPlanTransforms::handleFMaxReductionsWithoutFastMath(VPlan &Plan) {
692
697
Builder.createNaryOp (Instruction::URem, {FirstMaxIdx, &Plan.getVFxUF ()});
693
698
694
699
// Extract the final max value and update the users.
695
- auto *Res = Builder.createNaryOp (
696
- VPInstruction::ExtractLane, {FirstMaxLane, OrigRdxResult->getOperand (1 )});
697
- OrigRdxResult->replaceUsesWithIf (Res,
698
- [MaskFinalMaxValue](VPUser &U, unsigned ) {
699
- return &U != MaskFinalMaxValue;
700
- });
700
+ auto *Res = Builder.createNaryOp (VPInstruction::ExtractLane,
701
+ {FirstMaxLane, MaxResult->getOperand (1 )});
702
+ MaxResult->replaceUsesWithIf (Res, [MaskFinalMaxValue](VPUser &U, unsigned ) {
703
+ return &U != MaskFinalMaxValue;
704
+ });
701
705
return true ;
702
706
}
0 commit comments