diff options
| author | Sanjay Patel <spatel@rotateright.com> | 2018-08-08 23:04:43 +0000 |
|---|---|---|
| committer | Sanjay Patel <spatel@rotateright.com> | 2018-08-08 23:04:43 +0000 |
| commit | e47dc1a40522761752a4bdcb5e23f8147c1da0e2 (patch) | |
| tree | 24505b08baeef3434429ef42ba04729feabb13f0 | |
| parent | e327266d45b14b693630be50c0bfa1fce18c66f2 (diff) | |
| download | bcm5719-llvm-e47dc1a40522761752a4bdcb5e23f8147c1da0e2.tar.gz bcm5719-llvm-e47dc1a40522761752a4bdcb5e23f8147c1da0e2.zip | |
[DAGCombiner] loosen constraints for fsub+fadd fold
isNegatibleForFree() should not matter here (as the test diffs show)
because it's always a win to replace an fsub+fadd with fneg. The
problem in D50195 persists because either (1) we are doing these
folds in the wrong order or (2) we're missing another fold for fadd.
llvm-svn: 339299
| -rw-r--r-- | llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 21 | ||||
| -rw-r--r-- | llvm/test/CodeGen/X86/fp-fold.ll | 68 |
2 files changed, 51 insertions, 38 deletions
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp index 9690d2c205a..f78688787dd 100644 --- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -10940,20 +10940,13 @@ SDValue DAGCombiner::visitFSUB(SDNode *N) { return DAG.getNode(ISD::FADD, DL, VT, N0, GetNegatedExpression(N1, DAG, LegalOperations), Flags); - // If 'unsafe math' is enabled, fold lots of things. - if (Options.UnsafeFPMath) { - // (fsub x, (fadd x, y)) -> (fneg y) - // (fsub x, (fadd y, x)) -> (fneg y) - if (N1.getOpcode() == ISD::FADD) { - SDValue N10 = N1->getOperand(0); - SDValue N11 = N1->getOperand(1); - - if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI, &Options)) - return GetNegatedExpression(N11, DAG, LegalOperations); - - if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI, &Options)) - return GetNegatedExpression(N10, DAG, LegalOperations); - } + if (Options.UnsafeFPMath && N1.getOpcode() == ISD::FADD) { + // X - (X + Y) -> -Y + if (N0 == N1->getOperand(0)) + return DAG.getNode(ISD::FNEG, DL, VT, N1->getOperand(1)); + // X - (Y + X) -> -Y + if (N0 == N1->getOperand(1)) + return DAG.getNode(ISD::FNEG, DL, VT, N1->getOperand(0)); } // FSUB -> FMA combines: diff --git a/llvm/test/CodeGen/X86/fp-fold.ll b/llvm/test/CodeGen/X86/fp-fold.ll index 917b5d368dd..1102b755130 100644 --- a/llvm/test/CodeGen/X86/fp-fold.ll +++ b/llvm/test/CodeGen/X86/fp-fold.ll @@ -113,12 +113,17 @@ define float @fsub_neg_y(float %x, float %y) { ; Y - (X + Y) --> -X define float @fsub_fadd_common_op_fneg(float %x, float %y) { -; ANY-LABEL: fsub_fadd_common_op_fneg: -; ANY: # %bb.0: -; ANY-NEXT: addss %xmm1, %xmm0 -; ANY-NEXT: subss %xmm0, %xmm1 -; ANY-NEXT: movaps %xmm1, %xmm0 -; ANY-NEXT: retq +; STRICT-LABEL: fsub_fadd_common_op_fneg: +; STRICT: # %bb.0: +; STRICT-NEXT: addss %xmm1, %xmm0 +; STRICT-NEXT: subss %xmm0, %xmm1 +; STRICT-NEXT: movaps %xmm1, %xmm0 +; STRICT-NEXT: retq +; +; UNSAFE-LABEL: fsub_fadd_common_op_fneg: +; UNSAFE: # %bb.0: +; UNSAFE-NEXT: xorps {{.*}}(%rip), %xmm0 +; UNSAFE-NEXT: retq %a = fadd float %x, %y %r = fsub reassoc nsz float %y, %a ret float %r @@ -127,12 +132,17 @@ define float @fsub_fadd_common_op_fneg(float %x, float %y) { ; Y - (X + Y) --> -X define <4 x float> @fsub_fadd_common_op_fneg_vec(<4 x float> %x, <4 x float> %y) { -; ANY-LABEL: fsub_fadd_common_op_fneg_vec: -; ANY: # %bb.0: -; ANY-NEXT: addps %xmm1, %xmm0 -; ANY-NEXT: subps %xmm0, %xmm1 -; ANY-NEXT: movaps %xmm1, %xmm0 -; ANY-NEXT: retq +; STRICT-LABEL: fsub_fadd_common_op_fneg_vec: +; STRICT: # %bb.0: +; STRICT-NEXT: addps %xmm1, %xmm0 +; STRICT-NEXT: subps %xmm0, %xmm1 +; STRICT-NEXT: movaps %xmm1, %xmm0 +; STRICT-NEXT: retq +; +; UNSAFE-LABEL: fsub_fadd_common_op_fneg_vec: +; UNSAFE: # %bb.0: +; UNSAFE-NEXT: xorps {{.*}}(%rip), %xmm0 +; UNSAFE-NEXT: retq %a = fadd <4 x float> %x, %y %r = fsub nsz reassoc <4 x float> %y, %a ret <4 x float> %r @@ -142,12 +152,17 @@ define <4 x float> @fsub_fadd_common_op_fneg_vec(<4 x float> %x, <4 x float> %y) ; Commute operands of the 'add'. define float @fsub_fadd_common_op_fneg_commute(float %x, float %y) { -; ANY-LABEL: fsub_fadd_common_op_fneg_commute: -; ANY: # %bb.0: -; ANY-NEXT: addss %xmm1, %xmm0 -; ANY-NEXT: subss %xmm0, %xmm1 -; ANY-NEXT: movaps %xmm1, %xmm0 -; ANY-NEXT: retq +; STRICT-LABEL: fsub_fadd_common_op_fneg_commute: +; STRICT: # %bb.0: +; STRICT-NEXT: addss %xmm1, %xmm0 +; STRICT-NEXT: subss %xmm0, %xmm1 +; STRICT-NEXT: movaps %xmm1, %xmm0 +; STRICT-NEXT: retq +; +; UNSAFE-LABEL: fsub_fadd_common_op_fneg_commute: +; UNSAFE: # %bb.0: +; UNSAFE-NEXT: xorps {{.*}}(%rip), %xmm0 +; UNSAFE-NEXT: retq %a = fadd float %y, %x %r = fsub reassoc nsz float %y, %a ret float %r @@ -156,12 +171,17 @@ define float @fsub_fadd_common_op_fneg_commute(float %x, float %y) { ; Y - (Y + X) --> -X define <4 x float> @fsub_fadd_common_op_fneg_commute_vec(<4 x float> %x, <4 x float> %y) { -; ANY-LABEL: fsub_fadd_common_op_fneg_commute_vec: -; ANY: # %bb.0: -; ANY-NEXT: addps %xmm1, %xmm0 -; ANY-NEXT: subps %xmm0, %xmm1 -; ANY-NEXT: movaps %xmm1, %xmm0 -; ANY-NEXT: retq +; STRICT-LABEL: fsub_fadd_common_op_fneg_commute_vec: +; STRICT: # %bb.0: +; STRICT-NEXT: addps %xmm1, %xmm0 +; STRICT-NEXT: subps %xmm0, %xmm1 +; STRICT-NEXT: movaps %xmm1, %xmm0 +; STRICT-NEXT: retq +; +; UNSAFE-LABEL: fsub_fadd_common_op_fneg_commute_vec: +; UNSAFE: # %bb.0: +; UNSAFE-NEXT: xorps {{.*}}(%rip), %xmm0 +; UNSAFE-NEXT: retq %a = fadd <4 x float> %y, %x %r = fsub reassoc nsz <4 x float> %y, %a ret <4 x float> %r |

