[PATCH] D123830: [InstCombine] Optimize folding of fneg-of-fabs

Chenbing.Zheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 14 19:40:52 PDT 2022


Chenbing.Zheng created this revision.
Chenbing.Zheng added reviewers: spatel, Krishnakariya, RKSimon, benshi001.
Chenbing.Zheng added a project: LLVM.
Herald added a subscriber: hiraditya.
Herald added a project: All.
Chenbing.Zheng requested review of this revision.
Herald added subscribers: llvm-commits, jacquesguan.

The 'fneg' with nsz flag can propagate it's flag to 'select', when select satisfies that TrueVal = -FalseVal.

fneg nsz (select cond ? X : -X) -->  fneg nsz (select nsz cond ? X : -X)
https://alive2.llvm.org/ce/z/PeSsVA

I'm not sure if this spread is always beneficial, so this patch is only done when it can help fold fneg-of-fabs.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D123830

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


Index: llvm/test/Transforms/InstCombine/fneg.ll
===================================================================
--- llvm/test/Transforms/InstCombine/fneg.ll
+++ llvm/test/Transforms/InstCombine/fneg.ll
@@ -714,13 +714,10 @@
   ret float %fneg1
 }
 
-; TODO: This should reduce to fneg-of-fabs.
-
 define float @fnabs(float %a) {
 ; CHECK-LABEL: @fnabs(
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp olt float [[A:%.*]], 0.000000e+00
-; CHECK-NEXT:    [[A_NEG:%.*]] = fneg fast float [[A]]
-; CHECK-NEXT:    [[FNEG1:%.*]] = select fast i1 [[CMP]], float [[A]], float [[A_NEG]]
+; CHECK-NEXT:    [[TMP1:%.*]] = call nsz float @llvm.fabs.f32(float [[A:%.*]])
+; CHECK-NEXT:    [[FNEG1:%.*]] = fneg fast float [[TMP1]]
 ; CHECK-NEXT:    ret float [[FNEG1]]
 ;
   %fneg = fneg float %a
Index: llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -2275,6 +2275,34 @@
   // 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))))) {
+    // The 'fneg' with nsz flag can propagate it's flag to 'select', when select
+    // satisfies that TrueVal = -FalseVal. fneg nsz (select cond ? X : -X) -->
+    // fneg nsz (select nsz cond ? X : -X) fneg nsz (select cond ? -X : X) -->
+    // fneg nsz (select nsz cond ? -X : X)
+    if (I.hasNoSignedZeros()) {
+      CmpInst::Predicate Pred;
+      // This conditions similar with (X <  +/-0.0) ? -X : X --> fabs(X),
+      // except 'select' hasNoSignedZeros(). We propagate 'nsz' from 'fneg' to
+      // 'select', so that it can be fold to 'fabs' in visitSelectInst.
+      if (match(Cond, m_FCmp(Pred, m_Specific(Y), m_AnyZeroFP())) &&
+          match(X, m_FNeg(m_Specific(Y))) &&
+          (Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_OLE ||
+           Pred == FCmpInst::FCMP_ULT || Pred == FCmpInst::FCMP_ULE)) {
+        auto *Sel = dyn_cast<SelectInst>(Op);
+        Sel->setHasNoSignedZeros(true);
+        return nullptr;
+      }
+      // Similar to the above, deal with (X >  +/-0.0) ? X : -X --> fabs(X).
+      if (match(Cond, m_FCmp(Pred, m_Specific(X), m_AnyZeroFP())) &&
+          match(Y, m_FNeg(m_Specific(X))) &&
+          (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_OGE ||
+           Pred == FCmpInst::FCMP_UGT || Pred == FCmpInst::FCMP_UGE)) {
+        auto *Sel = dyn_cast<SelectInst>(Op);
+        Sel->setHasNoSignedZeros(true);
+        return nullptr;
+      }
+    }
+
     // 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.)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D123830.422998.patch
Type: text/x-patch
Size: 2868 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220415/4886817d/attachment.bin>


More information about the llvm-commits mailing list