Skip to content

[DAGCombiner] Remove UnsafeFPMath usage in visitFSUBForFMACombine etc. #145637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16737,7 +16737,7 @@ ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
static bool isContractableFMUL(const TargetOptions &Options, SDValue N) {
assert(N.getOpcode() == ISD::FMUL);

return Options.AllowFPOpFusion == FPOpFusion::Fast || Options.UnsafeFPMath ||
return Options.AllowFPOpFusion == FPOpFusion::Fast ||
N->getFlags().hasAllowContract();
}

Expand Down Expand Up @@ -17010,8 +17010,8 @@ SDValue DAGCombiner::visitFSUBForFMACombine(SDNode *N) {
return SDValue();

const SDNodeFlags Flags = N->getFlags();
bool AllowFusionGlobally = (Options.AllowFPOpFusion == FPOpFusion::Fast ||
Options.UnsafeFPMath || HasFMAD);
bool AllowFusionGlobally =
(Options.AllowFPOpFusion == FPOpFusion::Fast || HasFMAD);

// If the subtraction is not contractable, do not combine.
if (!AllowFusionGlobally && !N->getFlags().hasAllowContract())
Expand Down Expand Up @@ -17166,22 +17166,17 @@ SDValue DAGCombiner::visitFSUBForFMACombine(SDNode *N) {
}
}

auto isReassociable = [&Options](SDNode *N) {
return Options.UnsafeFPMath || N->getFlags().hasAllowReassociation();
};

auto isContractableAndReassociableFMUL = [&isContractableFMUL,
&isReassociable](SDValue N) {
return isContractableFMUL(N) && isReassociable(N.getNode());
auto isContractableAndReassociableFMUL = [&isContractableFMUL](SDValue N) {
return isContractableFMUL(N) && N->getFlags().hasAllowReassociation();
};

auto isFusedOp = [&](SDValue N) {
return matcher.match(N, ISD::FMA) || matcher.match(N, ISD::FMAD);
};

// More folding opportunities when target permits.
if (Aggressive && isReassociable(N)) {
bool CanFuse = Options.UnsafeFPMath || N->getFlags().hasAllowContract();
if (Aggressive && N->getFlags().hasAllowReassociation()) {
bool CanFuse = N->getFlags().hasAllowContract();
// fold (fsub (fma x, y, (fmul u, v)), z)
// -> (fma x, y (fma u, v, (fneg z)))
if (CanFuse && isFusedOp(N0) &&
Expand Down Expand Up @@ -17338,8 +17333,7 @@ SDValue DAGCombiner::visitFMULForFMADistributiveCombine(SDNode *N) {

// Floating-point multiply-add with intermediate rounding. This can result
// in a less precise result due to the changed rounding order.
bool HasFMAD = Options.UnsafeFPMath &&
(LegalOperations && TLI.isFMADLegal(DAG, N));
bool HasFMAD = LegalOperations && TLI.isFMADLegal(DAG, N);

// No valid opcode, do not combine.
if (!HasFMAD && !HasFMA)
Expand Down Expand Up @@ -18238,8 +18232,7 @@ SDValue DAGCombiner::visitFDIV(SDNode *N) {
// Only do the transform if the reciprocal is a legal fp immediate that
// isn't too nasty (eg NaN, denormal, ...).
if (((st == APFloat::opOK && !Recip.isDenormal()) ||
(st == APFloat::opInexact &&
(Options.UnsafeFPMath || Flags.hasAllowReciprocal()))) &&
(st == APFloat::opInexact && Flags.hasAllowReciprocal())) &&
(!LegalOperations ||
// FIXME: custom lowering of ConstantFP might fail (see e.g. ARM
// backend)... we should handle this gracefully after Legalize.
Expand All @@ -18250,7 +18243,7 @@ SDValue DAGCombiner::visitFDIV(SDNode *N) {
DAG.getConstantFP(Recip, DL, VT));
}

if (Options.UnsafeFPMath || Flags.hasAllowReciprocal()) {
if (Flags.hasAllowReciprocal()) {
// If this FDIV is part of a reciprocal square root, it may be folded
// into a target-specific square root estimate instruction.
if (N1.getOpcode() == ISD::FSQRT) {
Expand Down Expand Up @@ -18325,7 +18318,7 @@ SDValue DAGCombiner::visitFDIV(SDNode *N) {

// Fold X/Sqrt(X) -> Sqrt(X)
if ((Options.NoSignedZerosFPMath || Flags.hasNoSignedZeros()) &&
(Options.UnsafeFPMath || Flags.hasAllowReassociation()))
Flags.hasAllowReassociation())
if (N1.getOpcode() == ISD::FSQRT && N0 == N1.getOperand(0))
return N1;

Expand Down
15 changes: 8 additions & 7 deletions llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2857,15 +2857,16 @@ static SDValue lowerFREM(SDValue Op, SelectionDAG &DAG,
SDValue X = Op->getOperand(0);
SDValue Y = Op->getOperand(1);
EVT Ty = Op.getValueType();
SDNodeFlags Flags = Op->getFlags();

SDValue Div = DAG.getNode(ISD::FDIV, DL, Ty, X, Y);
SDValue Trunc = DAG.getNode(ISD::FTRUNC, DL, Ty, Div);
SDValue Mul =
DAG.getNode(ISD::FMUL, DL, Ty, Trunc, Y, SDNodeFlags::AllowContract);
SDValue Sub =
DAG.getNode(ISD::FSUB, DL, Ty, X, Mul, SDNodeFlags::AllowContract);
SDValue Div = DAG.getNode(ISD::FDIV, DL, Ty, X, Y, Flags);
SDValue Trunc = DAG.getNode(ISD::FTRUNC, DL, Ty, Div, Flags);
SDValue Mul = DAG.getNode(ISD::FMUL, DL, Ty, Trunc, Y,
Flags | SDNodeFlags::AllowContract);
SDValue Sub = DAG.getNode(ISD::FSUB, DL, Ty, X, Mul,
Flags | SDNodeFlags::AllowContract);

if (AllowUnsafeFPMath || Op->getFlags().hasNoInfs())
if (AllowUnsafeFPMath || Flags.hasNoInfs())
return Sub;

// If Y is infinite, return X
Expand Down
Loading