[llvm] 242fed9 - [InstCombine] convert fsub nsz with fneg operand to -(X + Y)

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 27 11:50:01 PST 2020


Author: Sanjay Patel
Date: 2020-01-27T14:49:15-05:00
New Revision: 242fed9d7fb96be691de5386627ee1eaa2f62b2b

URL: https://github.com/llvm/llvm-project/commit/242fed9d7fb96be691de5386627ee1eaa2f62b2b
DIFF: https://github.com/llvm/llvm-project/commit/242fed9d7fb96be691de5386627ee1eaa2f62b2b.diff

LOG: [InstCombine] convert fsub nsz with fneg operand to -(X + Y)

This was noted in D72521 - we need to match fneg specifically to
consistently handle that pattern along with (-0.0 - X).

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
    llvm/test/Transforms/InstCombine/fsub.ll
    llvm/test/Transforms/Reassociate/fast-basictest.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 2f404856b173..574662800941 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -2159,6 +2159,13 @@ Instruction *InstCombiner::visitFSub(BinaryOperator &I) {
     }
   }
 
+  // (-X) - Op1 --> -(X + Op1)
+  if (I.hasNoSignedZeros() && !isa<ConstantExpr>(Op0) &&
+      match(Op0, m_OneUse(m_FNeg(m_Value(X))))) {
+    Value *FAdd = Builder.CreateFAddFMF(X, Op1, &I);
+    return UnaryOperator::CreateFNegFMF(FAdd, &I);
+  }
+
   if (isa<Constant>(Op0))
     if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
       if (Instruction *NV = FoldOpIntoSelect(I, SI))

diff  --git a/llvm/test/Transforms/InstCombine/fsub.ll b/llvm/test/Transforms/InstCombine/fsub.ll
index b9163b98ce8e..f3473af81c8e 100644
--- a/llvm/test/Transforms/InstCombine/fsub.ll
+++ b/llvm/test/Transforms/InstCombine/fsub.ll
@@ -731,8 +731,8 @@ define float @fneg_fsub(float %x, float %y) {
 
 define float @fneg_fsub_nsz(float %x, float %y) {
 ; CHECK-LABEL: @fneg_fsub_nsz(
-; CHECK-NEXT:    [[NEGX:%.*]] = fneg float [[X:%.*]]
-; CHECK-NEXT:    [[SUB:%.*]] = fsub nsz float [[NEGX]], [[Y:%.*]]
+; CHECK-NEXT:    [[TMP1:%.*]] = fadd nsz float [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[SUB:%.*]] = fneg nsz float [[TMP1]]
 ; CHECK-NEXT:    ret float [[SUB]]
 ;
   %negx = fneg float %x
@@ -743,7 +743,7 @@ define float @fneg_fsub_nsz(float %x, float %y) {
 define float @fake_fneg_fsub_fast(float %x, float %y) {
 ; CHECK-LABEL: @fake_fneg_fsub_fast(
 ; CHECK-NEXT:    [[TMP1:%.*]] = fadd fast float [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[SUB:%.*]] = fsub fast float -0.000000e+00, [[TMP1]]
+; CHECK-NEXT:    [[SUB:%.*]] = fneg fast float [[TMP1]]
 ; CHECK-NEXT:    ret float [[SUB]]
 ;
   %negx = fsub float -0.0, %x
@@ -766,8 +766,8 @@ define float @fake_fneg_fsub_fast_extra_use(float %x, float %y) {
 
 define <2 x float> @fake_fneg_fsub_vec(<2 x float> %x, <2 x float> %y) {
 ; CHECK-LABEL: @fake_fneg_fsub_vec(
-; CHECK-NEXT:    [[NEGX:%.*]] = fsub <2 x float> <float -0.000000e+00, float -0.000000e+00>, [[X:%.*]]
-; CHECK-NEXT:    [[SUB:%.*]] = fsub nsz <2 x float> [[NEGX]], [[Y:%.*]]
+; CHECK-NEXT:    [[TMP1:%.*]] = fadd nsz <2 x float> [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[SUB:%.*]] = fneg nsz <2 x float> [[TMP1]]
 ; CHECK-NEXT:    ret <2 x float> [[SUB]]
 ;
   %negx = fsub <2 x float> <float -0.0, float -0.0>, %x

diff  --git a/llvm/test/Transforms/Reassociate/fast-basictest.ll b/llvm/test/Transforms/Reassociate/fast-basictest.ll
index 95f89102afe4..2d64d93bcaf1 100644
--- a/llvm/test/Transforms/Reassociate/fast-basictest.ll
+++ b/llvm/test/Transforms/Reassociate/fast-basictest.ll
@@ -671,7 +671,7 @@ define float @test19_reassoc(float %A, float %B) {
 define float @test20(float %a, float %b, float %c) nounwind  {
 ; CHECK-LABEL: @test20(
 ; CHECK-NEXT:    [[TMP1:%.*]] = fadd fast float [[B:%.*]], [[C:%.*]]
-; CHECK-NEXT:    [[T7:%.*]] = fsub fast float -0.000000e+00, [[TMP1]]
+; CHECK-NEXT:    [[T7:%.*]] = fneg fast float [[TMP1]]
 ; CHECK-NEXT:    ret float [[T7]]
 ;
   %t3 = fsub fast float %a, %b


        


More information about the llvm-commits mailing list