[llvm] r339357 - extend folding fsub/fadd to fneg for FMF

Michael Berg via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 9 10:00:04 PDT 2018


Author: mcberg2017
Date: Thu Aug  9 10:00:03 2018
New Revision: 339357

URL: http://llvm.org/viewvc/llvm-project?rev=339357&view=rev
Log:
extend folding fsub/fadd to fneg for FMF

Summary: This change provides a common optimization path for both Unsafe and FMF driven optimization for this fsub fold adding reassociation, as it the flag that most closely represents the translation

Reviewers: spatel, wristow, arsenm

Reviewed By: spatel

Subscribers: wdng

Differential Revision: https://reviews.llvm.org/D50195

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=339357&r1=339356&r2=339357&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp Thu Aug  9 10:00:03 2018
@@ -10935,20 +10935,22 @@ SDValue DAGCombiner::visitFSUB(SDNode *N
     }
   }
 
-  // fold (fsub A, (fneg B)) -> (fadd A, B)
-  if (isNegatibleForFree(N1, LegalOperations, TLI, &Options))
-    return DAG.getNode(ISD::FADD, DL, VT, N0,
-                       GetNegatedExpression(N1, DAG, LegalOperations), Flags);
-
-  if (Options.UnsafeFPMath && N1.getOpcode() == ISD::FADD) {
+  if ((Options.UnsafeFPMath ||
+      (Flags.hasAllowReassociation() && Flags.hasNoSignedZeros()))
+      && N1.getOpcode() == ISD::FADD) {
     // X - (X + Y) -> -Y
     if (N0 == N1->getOperand(0))
-      return DAG.getNode(ISD::FNEG, DL, VT, N1->getOperand(1));
+      return DAG.getNode(ISD::FNEG, DL, VT, N1->getOperand(1), Flags);
     // X - (Y + X) -> -Y
     if (N0 == N1->getOperand(1))
-      return DAG.getNode(ISD::FNEG, DL, VT, N1->getOperand(0));
+      return DAG.getNode(ISD::FNEG, DL, VT, N1->getOperand(0), Flags);
   }
 
+  // fold (fsub A, (fneg B)) -> (fadd A, B)
+  if (isNegatibleForFree(N1, LegalOperations, TLI, &Options))
+    return DAG.getNode(ISD::FADD, DL, VT, N0,
+                       GetNegatedExpression(N1, DAG, LegalOperations), Flags);
+
   // FSUB -> FMA combines:
   if (SDValue Fused = visitFSUBForFMACombine(N)) {
     AddToWorklist(Fused.getNode());

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=339357&r1=339356&r2=339357&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/X86/fp-fold.ll (original)
+++ llvm/trunk/test/CodeGen/X86/fp-fold.ll Thu Aug  9 10:00:03 2018
@@ -89,20 +89,10 @@ define float @fsub_neg_x_y(float %x, flo
 }
 
 define float @fsub_neg_y(float %x, float %y) {
-; STRICT-LABEL: fsub_neg_y:
-; STRICT:       # %bb.0:
-; STRICT-NEXT:    mulss {{.*}}(%rip), %xmm0
-; STRICT-NEXT:    addss %xmm1, %xmm0
-; STRICT-NEXT:    subss %xmm0, %xmm1
-; STRICT-NEXT:    movaps %xmm1, %xmm0
-; STRICT-NEXT:    retq
-;
-; UNSAFE-LABEL: fsub_neg_y:
-; UNSAFE:       # %bb.0:
-; UNSAFE-NEXT:    mulss {{.*}}(%rip), %xmm0
-; UNSAFE-NEXT:    subss %xmm1, %xmm0
-; UNSAFE-NEXT:    addss %xmm1, %xmm0
-; UNSAFE-NEXT:    retq
+; ANY-LABEL: fsub_neg_y:
+; ANY:       # %bb.0:
+; ANY-NEXT:    mulss {{.*}}(%rip), %xmm0
+; ANY-NEXT:    retq
   %mul = fmul float %x, 5.0
   %add = fadd float %mul, %y
   %r = fsub nsz reassoc float %y, %add
@@ -110,20 +100,10 @@ define float @fsub_neg_y(float %x, float
 }
 
 define float @fsub_neg_y_commute(float %x, float %y) {
-; STRICT-LABEL: fsub_neg_y_commute:
-; STRICT:       # %bb.0:
-; STRICT-NEXT:    mulss {{.*}}(%rip), %xmm0
-; STRICT-NEXT:    addss %xmm1, %xmm0
-; STRICT-NEXT:    subss %xmm0, %xmm1
-; STRICT-NEXT:    movaps %xmm1, %xmm0
-; STRICT-NEXT:    retq
-;
-; UNSAFE-LABEL: fsub_neg_y_commute:
-; UNSAFE:       # %bb.0:
-; UNSAFE-NEXT:    mulss {{.*}}(%rip), %xmm0
-; UNSAFE-NEXT:    subss %xmm1, %xmm0
-; UNSAFE-NEXT:    addss %xmm1, %xmm0
-; UNSAFE-NEXT:    retq
+; ANY-LABEL: fsub_neg_y_commute:
+; ANY:       # %bb.0:
+; ANY-NEXT:    mulss {{.*}}(%rip), %xmm0
+; ANY-NEXT:    retq
   %mul = fmul float %x, 5.0
   %add = fadd float %y, %mul
   %r = fsub nsz reassoc float %y, %add
@@ -132,17 +112,10 @@ define float @fsub_neg_y_commute(float %
 ; Y - (X + Y) --> -X
 
 define float @fsub_fadd_common_op_fneg(float %x, float %y) {
-; 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
+; ANY-LABEL: fsub_fadd_common_op_fneg:
+; ANY:       # %bb.0:
+; ANY-NEXT:    xorps {{.*}}(%rip), %xmm0
+; ANY-NEXT:    retq
   %a = fadd float %x, %y
   %r = fsub reassoc nsz float %y, %a
   ret float %r
@@ -151,17 +124,10 @@ 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) {
-; 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
+; ANY-LABEL: fsub_fadd_common_op_fneg_vec:
+; ANY:       # %bb.0:
+; ANY-NEXT:    xorps {{.*}}(%rip), %xmm0
+; ANY-NEXT:    retq
   %a = fadd <4 x float> %x, %y
   %r = fsub nsz reassoc <4 x float> %y, %a
   ret <4 x float> %r
@@ -171,17 +137,10 @@ 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) {
-; 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
+; ANY-LABEL: fsub_fadd_common_op_fneg_commute:
+; ANY:       # %bb.0:
+; ANY-NEXT:    xorps {{.*}}(%rip), %xmm0
+; ANY-NEXT:    retq
   %a = fadd float %y, %x
   %r = fsub reassoc nsz float %y, %a
   ret float %r
@@ -190,17 +149,10 @@ 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) {
-; 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
+; ANY-LABEL: fsub_fadd_common_op_fneg_commute_vec:
+; ANY:       # %bb.0:
+; ANY-NEXT:    xorps {{.*}}(%rip), %xmm0
+; ANY-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