Skip to content

Commit d815d8f

Browse files
committed
Merge from mainline.
Fold x-0 to x in unsafe-fp-math mode. This comes up in the testcase from PR3376, and in fact is sufficient to completely avoid the problem in that testcase. There's an underlying problem though; TLI.isOperationLegal considers Custom to be Legal, which might be ok in some cases, but that's what DAGCombiner is using in many places to test if something is legal when LegalOperations is true. When DAGCombiner is running after legalize, this isn't sufficient. I'll address this in a separate commit. llvm-svn: 63207
1 parent c4048b4 commit d815d8f

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3851,6 +3851,9 @@ SDValue DAGCombiner::visitFSUB(SDNode *N) {
38513851
// fold (fsub c1, c2) -> c1-c2
38523852
if (N0CFP && N1CFP && VT != MVT::ppcf128)
38533853
return DAG.getNode(ISD::FSUB, VT, N0, N1);
3854+
// fold (A-0) -> A
3855+
if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
3856+
return N0;
38543857
// fold (0-B) -> -B
38553858
if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
38563859
if (isNegatibleForFree(N1, LegalOperations))

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,15 +2387,22 @@ SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
23872387
case ISD::FMUL:
23882388
case ISD::FDIV:
23892389
case ISD::FREM:
2390-
if (UnsafeFPMath && Opcode == ISD::FADD) {
2391-
// 0+x --> x
2392-
if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2393-
if (CFP->getValueAPF().isZero())
2394-
return N2;
2395-
// x+0 --> x
2396-
if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2397-
if (CFP->getValueAPF().isZero())
2398-
return N1;
2390+
if (UnsafeFPMath) {
2391+
if (Opcode == ISD::FADD) {
2392+
// 0+x --> x
2393+
if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2394+
if (CFP->getValueAPF().isZero())
2395+
return N2;
2396+
// x+0 --> x
2397+
if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2398+
if (CFP->getValueAPF().isZero())
2399+
return N1;
2400+
} else if (Opcode == ISD::FSUB) {
2401+
// x-0 --> x
2402+
if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2403+
if (CFP->getValueAPF().isZero())
2404+
return N1;
2405+
}
23992406
}
24002407
assert(N1.getValueType() == N2.getValueType() &&
24012408
N1.getValueType() == VT && "Binary operator types must match!");

0 commit comments

Comments
 (0)