[llvm] r339299 - [DAGCombiner] loosen constraints for fsub+fadd fold

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 8 16:04:44 PDT 2018


Author: spatel
Date: Wed Aug  8 16:04:43 2018
New Revision: 339299

URL: http://llvm.org/viewvc/llvm-project?rev=339299&view=rev
Log:
[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.

Modified:
    llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
    llvm/trunk/test/CodeGen/X86/fp-fold.ll

Modified: llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp?rev=339299&r1=339298&r2=339299&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp Wed Aug  8 16:04:43 2018
@@ -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:

Modified: llvm/trunk/test/CodeGen/X86/fp-fold.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/fp-fold.ll?rev=339299&r1=339298&r2=339299&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/X86/fp-fold.ll (original)
+++ llvm/trunk/test/CodeGen/X86/fp-fold.ll Wed Aug  8 16:04:43 2018
@@ -113,12 +113,17 @@ define float @fsub_neg_y(float %x, float
 ; 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(f
 ; 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_
 ; 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_c
 ; 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




More information about the llvm-commits mailing list