[llvm] 3cdd05e - [InstCombine] fold fnegs around select

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon May 17 11:53:57 PDT 2021


Author: Sanjay Patel
Date: 2021-05-17T14:53:49-04:00
New Revision: 3cdd05e519ddab7d4e4864cadb977a876cd19046

URL: https://github.com/llvm/llvm-project/commit/3cdd05e519ddab7d4e4864cadb977a876cd19046
DIFF: https://github.com/llvm/llvm-project/commit/3cdd05e519ddab7d4e4864cadb977a876cd19046.diff

LOG: [InstCombine] fold fnegs around select

This is one of the folds requested in:
https://llvm.org/PR39480

https://alive2.llvm.org/ce/z/NczU3V

Note - this uses the normal FMF propagation logic
(flags transfer from the final value to new/intermediate ops).
It's not clear if this matches what Alive2 implements,
so we may want to adjust one or the other.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
    llvm/test/Transforms/InstCombine/fneg.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index b1b77d1aaaee8..2a77f56e569c8 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -2187,6 +2187,25 @@ Instruction *InstCombinerImpl::visitFNeg(UnaryOperator &I) {
   if (Instruction *R = hoistFNegAboveFMulFDiv(I, Builder))
     return R;
 
+  Value *Cond;
+  if (match(Op, m_OneUse(m_Select(m_Value(Cond), m_Value(X), m_Value(Y))))) {
+    Value *P;
+    if (match(X, m_FNeg(m_Value(P)))) {
+      IRBuilder<>::FastMathFlagGuard FMFG(Builder);
+      Builder.setFastMathFlags(I.getFastMathFlags());
+      Value *NegY = Builder.CreateFNegFMF(Y, &I, Y->getName() + ".neg");
+      Value *NewSel = Builder.CreateSelect(Cond, P, NegY);
+      return replaceInstUsesWith(I, NewSel);
+    }
+    if (match(Y, m_FNeg(m_Value(P)))) {
+      IRBuilder<>::FastMathFlagGuard FMFG(Builder);
+      Builder.setFastMathFlags(I.getFastMathFlags());
+      Value *NegX = Builder.CreateFNegFMF(X, &I, X->getName() + ".neg");
+      Value *NewSel = Builder.CreateSelect(Cond, NegX, P);
+      return replaceInstUsesWith(I, NewSel);
+    }
+  }
+
   return nullptr;
 }
 

diff  --git a/llvm/test/Transforms/InstCombine/fneg.ll b/llvm/test/Transforms/InstCombine/fneg.ll
index ed25a0e741ede..0d56fbf50bdf4 100644
--- a/llvm/test/Transforms/InstCombine/fneg.ll
+++ b/llvm/test/Transforms/InstCombine/fneg.ll
@@ -506,10 +506,9 @@ define float @fake_fneg_nsz_fadd_constant_expr(float %x) {
 
 define float @select_fneg_true(float %x, float %y, i1 %b) {
 ; CHECK-LABEL: @select_fneg_true(
-; CHECK-NEXT:    [[NX:%.*]] = fneg float [[X:%.*]]
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[B:%.*]], float [[NX]], float [[Y:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = fneg float [[S]]
-; CHECK-NEXT:    ret float [[R]]
+; CHECK-NEXT:    [[Y_NEG:%.*]] = fneg float [[Y:%.*]]
+; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[B:%.*]], float [[X:%.*]], float [[Y_NEG]]
+; CHECK-NEXT:    ret float [[TMP1]]
 ;
   %nx = fneg float %x
   %s = select i1 %b, float %nx, float %y
@@ -519,10 +518,9 @@ define float @select_fneg_true(float %x, float %y, i1 %b) {
 
 define <2 x float> @select_fneg_false(<2 x float> %x, <2 x float> %y, <2 x i1> %b) {
 ; CHECK-LABEL: @select_fneg_false(
-; CHECK-NEXT:    [[NY:%.*]] = fneg nnan <2 x float> [[Y:%.*]]
-; CHECK-NEXT:    [[S:%.*]] = select ninf <2 x i1> [[B:%.*]], <2 x float> [[X:%.*]], <2 x float> [[NY]]
-; CHECK-NEXT:    [[R:%.*]] = fneg nnan nsz <2 x float> [[S]]
-; CHECK-NEXT:    ret <2 x float> [[R]]
+; CHECK-NEXT:    [[X_NEG:%.*]] = fneg nnan nsz <2 x float> [[X:%.*]]
+; CHECK-NEXT:    [[TMP1:%.*]] = select nnan nsz <2 x i1> [[B:%.*]], <2 x float> [[X_NEG]], <2 x float> [[Y:%.*]]
+; CHECK-NEXT:    ret <2 x float> [[TMP1]]
 ;
   %ny = fneg nnan <2 x float> %y
   %s = select ninf <2 x i1> %b, <2 x float> %x, <2 x float> %ny
@@ -546,9 +544,9 @@ define float @select_fneg_use1(float %x, float %y, i1 %b) {
 ; CHECK-LABEL: @select_fneg_use1(
 ; CHECK-NEXT:    [[NX:%.*]] = fneg ninf float [[X:%.*]]
 ; CHECK-NEXT:    call void @use(float [[NX]])
-; CHECK-NEXT:    [[S:%.*]] = select fast i1 [[B:%.*]], float [[NX]], float [[Y:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = fneg float [[S]]
-; CHECK-NEXT:    ret float [[R]]
+; CHECK-NEXT:    [[Y_NEG:%.*]] = fneg float [[Y:%.*]]
+; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[B:%.*]], float [[X]], float [[Y_NEG]]
+; CHECK-NEXT:    ret float [[TMP1]]
 ;
   %nx = fneg ninf float %x
   call void @use(float %nx)
@@ -560,10 +558,9 @@ define float @select_fneg_use1(float %x, float %y, i1 %b) {
 define float @select_fneg_use2(float %x, float %y, i1 %b) {
 ; CHECK-LABEL: @select_fneg_use2(
 ; CHECK-NEXT:    call void @use(float [[Y:%.*]])
-; CHECK-NEXT:    [[NX:%.*]] = fneg nsz float [[X:%.*]]
-; CHECK-NEXT:    [[S:%.*]] = select ninf i1 [[B:%.*]], float [[NX]], float [[Y]]
-; CHECK-NEXT:    [[R:%.*]] = fneg fast float [[S]]
-; CHECK-NEXT:    ret float [[R]]
+; CHECK-NEXT:    [[Y_NEG:%.*]] = fneg fast float [[Y]]
+; CHECK-NEXT:    [[TMP1:%.*]] = select fast i1 [[B:%.*]], float [[X:%.*]], float [[Y_NEG]]
+; CHECK-NEXT:    ret float [[TMP1]]
 ;
   call void @use(float %y)
   %nx = fneg nsz float %x
@@ -572,6 +569,8 @@ define float @select_fneg_use2(float %x, float %y, i1 %b) {
   ret float %r
 }
 
+; Negative test
+
 define float @select_fneg_use3(float %x, float %y, i1 %b) {
 ; CHECK-LABEL: @select_fneg_use3(
 ; CHECK-NEXT:    [[NX:%.*]] = fneg float [[X:%.*]]


        


More information about the llvm-commits mailing list