@@ -16619,8 +16619,8 @@ SDValue DAGCombiner::visitFADDForFMACombine(SDNode *N) {
16619
16619
if (!HasFMAD && !HasFMA)
16620
16620
return SDValue();
16621
16621
16622
- bool AllowFusionGlobally = (Options.AllowFPOpFusion == FPOpFusion::Fast ||
16623
- Options.UnsafeFPMath || HasFMAD) ;
16622
+ bool AllowFusionGlobally =
16623
+ Options.AllowFPOpFusion == FPOpFusion::Fast || HasFMAD;
16624
16624
// If the addition is not contractable, do not combine.
16625
16625
if (!AllowFusionGlobally && !N->getFlags().hasAllowContract())
16626
16626
return SDValue();
@@ -17826,6 +17826,7 @@ template <class MatchContextClass> SDValue DAGCombiner::visitFMA(SDNode *N) {
17826
17826
SDValue N2 = N->getOperand(2);
17827
17827
ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
17828
17828
ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
17829
+ ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
17829
17830
EVT VT = N->getValueType(0);
17830
17831
SDLoc DL(N);
17831
17832
const TargetOptions &Options = DAG.getTarget().Options;
@@ -17855,12 +17856,26 @@ template <class MatchContextClass> SDValue DAGCombiner::visitFMA(SDNode *N) {
17855
17856
}
17856
17857
17857
17858
// FIXME: use fast math flags instead of Options.UnsafeFPMath
17858
- if (Options.UnsafeFPMath) {
17859
+ // TODO: Finally migrate away from global TargetOptions.
17860
+ if (Options.AllowFPOpFusion == FPOpFusion::Fast ||
17861
+ Options.NoSignedZerosFPMath || N->getFlags().hasNoSignedZeros()) {
17859
17862
if (N0CFP && N0CFP->isZero())
17860
17863
return N2;
17861
17864
if (N1CFP && N1CFP->isZero())
17862
17865
return N2;
17863
17866
}
17867
+ // Handle (fma x, 0.0, c) and (fma 0.0, x, c)
17868
+ if (Options.AllowFPOpFusion == FPOpFusion::Fast ||
17869
+ (Options.NoInfsFPMath && Options.NoNaNsFPMath) ||
17870
+ (N->getFlags().hasNoInfs() && N->getFlags().hasNoNaNs())) {
17871
+ // Fold to c only when c is not -0.0.
17872
+ if (N2CFP && !N2CFP->isExactlyValue(-0.0)) {
17873
+ if (N0CFP && N0CFP->isZero())
17874
+ return N2;
17875
+ if (N1CFP && N1CFP->isZero())
17876
+ return N2;
17877
+ }
17878
+ }
17864
17879
17865
17880
// FIXME: Support splat of constant.
17866
17881
if (N0CFP && N0CFP->isExactlyValue(1.0))
0 commit comments