[llvm] d2012d9 - [InstCombine] fix nsz (fast-math) propagation from fneg-of-select

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 8 14:05:20 PDT 2021


Author: Sanjay Patel
Date: 2021-06-08T17:04:30-04:00
New Revision: d2012d965d60c3258b3a69d024491698f8aec386

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

LOG: [InstCombine] fix nsz (fast-math) propagation from fneg-of-select

As discussed in the post-commit comments for:
3cdd05e519dd

It seems to be safe to propagate all flags from the final fneg
except for 'nsz' to the new select:
https://alive2.llvm.org/ce/z/J_APDc

nsz has unique FMF semantics: it is not poison, it is only
"insignificant" in the calculation according to the LangRef.

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 2ed616f4ed7f4..79b5531b77614 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -2200,22 +2200,32 @@ Instruction *InstCombinerImpl::visitFNeg(UnaryOperator &I) {
   if (Instruction *R = hoistFNegAboveFMulFDiv(I, Builder))
     return R;
 
+  // Try to eliminate fneg if at least 1 arm of the select is negated.
   Value *Cond;
   if (match(Op, m_OneUse(m_Select(m_Value(Cond), m_Value(X), m_Value(Y))))) {
+    // Unlike most transforms, this one is not safe to propagate nsz unless
+    // it is present on the original select. (We are conservatively intersecting
+    // the nsz flags from the select and root fneg instruction.)
+    auto propagateSelectFMF = [&](SelectInst *S) {
+      S->copyFastMathFlags(&I);
+      if (auto *OldSel = dyn_cast<SelectInst>(Op))
+        if (!OldSel->hasNoSignedZeros())
+          S->setHasNoSignedZeros(false);
+    };
+    // -(Cond ? -P : Y) --> Cond ? P : -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);
+      SelectInst *NewSel = SelectInst::Create(Cond, P, NegY);
+      propagateSelectFMF(NewSel);
+      return NewSel;
     }
+    // -(Cond ? X : -P) --> Cond ? -X : P
     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);
+      SelectInst *NewSel = SelectInst::Create(Cond, NegX, P);
+      propagateSelectFMF(NewSel);
+      return NewSel;
     }
   }
 

diff  --git a/llvm/test/Transforms/InstCombine/fneg.ll b/llvm/test/Transforms/InstCombine/fneg.ll
index 6d507bdcd90a0..60aea51de01bc 100644
--- a/llvm/test/Transforms/InstCombine/fneg.ll
+++ b/llvm/test/Transforms/InstCombine/fneg.ll
@@ -567,8 +567,8 @@ 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:    [[Y_NEG:%.*]] = fneg float [[Y:%.*]]
-; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[B:%.*]], float [[X:%.*]], float [[Y_NEG]]
-; CHECK-NEXT:    ret float [[TMP1]]
+; CHECK-NEXT:    [[R:%.*]] = select i1 [[B:%.*]], float [[X:%.*]], float [[Y_NEG]]
+; CHECK-NEXT:    ret float [[R]]
 ;
   %nx = fneg float %x
   %s = select i1 %b, float %nx, float %y
@@ -579,8 +579,8 @@ 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:    [[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]]
+; CHECK-NEXT:    [[R:%.*]] = select nnan <2 x i1> [[B:%.*]], <2 x float> [[X_NEG]], <2 x float> [[Y:%.*]]
+; CHECK-NEXT:    ret <2 x float> [[R]]
 ;
   %ny = fneg nnan <2 x float> %y
   %s = select ninf <2 x i1> %b, <2 x float> %x, <2 x float> %ny
@@ -591,8 +591,8 @@ define <2 x float> @select_fneg_false(<2 x float> %x, <2 x float> %y, <2 x i1> %
 define float @select_fneg_false_no_nsz(float %x, float %y, i1 %b) {
 ; CHECK-LABEL: @select_fneg_false_no_nsz(
 ; CHECK-NEXT:    [[X_NEG:%.*]] = fneg nnan ninf nsz float [[X:%.*]]
-; CHECK-NEXT:    [[TMP1:%.*]] = select nnan ninf nsz i1 [[B:%.*]], float [[X_NEG]], float [[Y:%.*]]
-; CHECK-NEXT:    ret float [[TMP1]]
+; CHECK-NEXT:    [[R:%.*]] = select nnan ninf i1 [[B:%.*]], float [[X_NEG]], float [[Y:%.*]]
+; CHECK-NEXT:    ret float [[R]]
 ;
   %ny = fneg float %y
   %s = select i1 %b, float %x, float %ny
@@ -603,8 +603,8 @@ define float @select_fneg_false_no_nsz(float %x, float %y, i1 %b) {
 define float @select_fneg_false_nsz(float %x, float %y, i1 %b) {
 ; CHECK-LABEL: @select_fneg_false_nsz(
 ; CHECK-NEXT:    [[X_NEG:%.*]] = fneg nnan ninf nsz float [[X:%.*]]
-; CHECK-NEXT:    [[TMP1:%.*]] = select nnan ninf nsz i1 [[B:%.*]], float [[X_NEG]], float [[Y:%.*]]
-; CHECK-NEXT:    ret float [[TMP1]]
+; CHECK-NEXT:    [[R:%.*]] = select nnan ninf nsz i1 [[B:%.*]], float [[X_NEG]], float [[Y:%.*]]
+; CHECK-NEXT:    ret float [[R]]
 ;
   %ny = fneg float %y
   %s = select nsz i1 %b, float %x, float %ny
@@ -629,8 +629,8 @@ define float @select_fneg_use1(float %x, float %y, i1 %b) {
 ; CHECK-NEXT:    [[NX:%.*]] = fneg ninf float [[X:%.*]]
 ; CHECK-NEXT:    call void @use(float [[NX]])
 ; CHECK-NEXT:    [[Y_NEG:%.*]] = fneg float [[Y:%.*]]
-; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[B:%.*]], float [[X]], float [[Y_NEG]]
-; CHECK-NEXT:    ret float [[TMP1]]
+; CHECK-NEXT:    [[R:%.*]] = select i1 [[B:%.*]], float [[X]], float [[Y_NEG]]
+; CHECK-NEXT:    ret float [[R]]
 ;
   %nx = fneg ninf float %x
   call void @use(float %nx)
@@ -643,8 +643,8 @@ 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:    [[Y_NEG:%.*]] = fneg fast float [[Y]]
-; CHECK-NEXT:    [[TMP1:%.*]] = select fast i1 [[B:%.*]], float [[X:%.*]], float [[Y_NEG]]
-; CHECK-NEXT:    ret float [[TMP1]]
+; CHECK-NEXT:    [[R:%.*]] = select reassoc nnan ninf arcp contract afn i1 [[B:%.*]], float [[X:%.*]], float [[Y_NEG]]
+; CHECK-NEXT:    ret float [[R]]
 ;
   call void @use(float %y)
   %nx = fneg nsz float %x


        


More information about the llvm-commits mailing list