[llvm-branch-commits] [llvm] InstCombine: Handle multiple use fneg(fabs(x)) in SimplifyDemandedFPClass (PR #176149)

2026-01-15 Thread Yingwei Zheng via llvm-branch-commits

https://github.com/dtcxzyw edited 
https://github.com/llvm/llvm-project/pull/176149
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] InstCombine: Handle multiple use fneg(fabs(x)) in SimplifyDemandedFPClass (PR #176149)

2026-01-15 Thread Yingwei Zheng via llvm-branch-commits

https://github.com/dtcxzyw approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/176149
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] InstCombine: Handle multiple use fneg(fabs(x)) in SimplifyDemandedFPClass (PR #176149)

2026-01-15 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm ready_for_review 
https://github.com/llvm/llvm-project/pull/176149
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] InstCombine: Handle multiple use fneg(fabs(x)) in SimplifyDemandedFPClass (PR #176149)

2026-01-15 Thread Matt Arsenault via llvm-branch-commits

arsenm wrote:

> [!WARNING]
> This pull request is not mergeable via GitHub because a downstack PR is 
> open. Once all requirements are satisfied, merge this PR as a stack  href="https://app.graphite.com/github/pr/llvm/llvm-project/176149?utm_source=stack-comment-downstack-mergeability-warning";
>  >on Graphite.
> https://graphite.dev/docs/merge-pull-requests";>Learn more

* **#176149** https://app.graphite.com/github/pr/llvm/llvm-project/176149?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/> 👈 https://app.graphite.com/github/pr/llvm/llvm-project/176149?utm_source=stack-comment-view-in-graphite";
 target="_blank">(View in Graphite)
* **#176035** https://app.graphite.com/github/pr/llvm/llvm-project/176035?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* `main`




This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn 
more about https://stacking.dev/?utm_source=stack-comment";>stacking.


https://github.com/llvm/llvm-project/pull/176149
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] InstCombine: Handle multiple use fneg(fabs(x)) in SimplifyDemandedFPClass (PR #176149)

2026-01-15 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm created 
https://github.com/llvm/llvm-project/pull/176149

This ends up being smarter than the single use case, so these should
be merged at some point.

>From 1fae7b3eac2c508a5a0658d5d27dcff64b6e041c Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Thu, 15 Jan 2026 13:04:40 +0100
Subject: [PATCH] InstCombine: Handle multiple use fneg(fabs(x)) in
 SimplifyDemandedFPClass

This ends up being smarter than the single use case, so these should
be merged at some point.
---
 llvm/include/llvm/Support/KnownFPClass.h  |   6 +
 .../InstCombineSimplifyDemanded.cpp   |  53 --
 .../InstCombine/simplify-demanded-fpclass.ll  | 169 ++
 3 files changed, 218 insertions(+), 10 deletions(-)

diff --git a/llvm/include/llvm/Support/KnownFPClass.h 
b/llvm/include/llvm/Support/KnownFPClass.h
index 6c1a6a7e9b5c3..0b9412460af0c 100644
--- a/llvm/include/llvm/Support/KnownFPClass.h
+++ b/llvm/include/llvm/Support/KnownFPClass.h
@@ -192,6 +192,12 @@ struct KnownFPClass {
 signBitMustBeZero();
   }
 
+  static KnownFPClass fneg(const KnownFPClass &Src) {
+KnownFPClass Known = Src;
+Known.fneg();
+return Known;
+  }
+
   static KnownFPClass fabs(const KnownFPClass &Src) {
 KnownFPClass Known = Src;
 Known.fabs();
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp 
b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
index 86c229c667eea..78f2aa4815df9 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
@@ -2147,6 +2147,16 @@ inferFastMathValueFlagsBinOp(FastMathFlags FMF, 
FPClassTest ValidResults,
   return FMF;
 }
 
+static FPClassTest adjustDemandedMaskFromFlags(FPClassTest DemandedMask,
+   FastMathFlags FMF) {
+  if (FMF.noNaNs())
+DemandedMask &= ~fcNan;
+
+  if (FMF.noInfs())
+DemandedMask &= ~fcInf;
+  return DemandedMask;
+}
+
 Value *InstCombinerImpl::SimplifyDemandedUseFPClass(Instruction *I,
 FPClassTest DemandedMask,
 KnownFPClass &Known,
@@ -2161,11 +2171,7 @@ Value 
*InstCombinerImpl::SimplifyDemandedUseFPClass(Instruction *I,
   FastMathFlags FMF;
   if (auto *FPOp = dyn_cast(I)) {
 FMF = FPOp->getFastMathFlags();
-if (FMF.noNaNs())
-  DemandedMask &= ~fcNan;
-
-if (FMF.noInfs())
-  DemandedMask &= ~fcInf;
+DemandedMask = adjustDemandedMaskFromFlags(DemandedMask, FMF);
   }
 
   switch (I->getOpcode()) {
@@ -2958,11 +2964,7 @@ Value 
*InstCombinerImpl::SimplifyMultipleUseDemandedFPClass(
   FastMathFlags FMF;
   if (auto *FPOp = dyn_cast(I)) {
 FMF = FPOp->getFastMathFlags();
-if (FMF.noNaNs())
-  DemandedMask &= ~fcNan;
-
-if (FMF.noInfs())
-  DemandedMask &= ~fcInf;
+DemandedMask = adjustDemandedMaskFromFlags(DemandedMask, FMF);
   }
 
   switch (I->getOpcode()) {
@@ -2987,6 +2989,37 @@ Value 
*InstCombinerImpl::SimplifyMultipleUseDemandedFPClass(
 Known = KnownLHS.intersectWith(KnownRHS);
 break;
   }
+  case Instruction::FNeg: {
+// Special case fneg(fabs(x))
+Value *Src;
+
+Value *FNegSrc = I->getOperand(0);
+if (!match(FNegSrc, m_FAbs(m_Value(Src {
+  Known = computeKnownFPClass(I, DemandedMask, CxtI, Depth + 1);
+  break;
+}
+
+FastMathFlags FabsFMF = cast(FNegSrc))->getFastMathFlags();
+FPClassTest ThisDemandedMask =
+adjustDemandedMaskFromFlags(DemandedMask, FabsFMF);
+
+KnownFPClass KnownSrc =
+computeKnownFPClass(Src, fcAllFlags, CxtI, Depth + 1);
+
+if ((ThisDemandedMask & fcNan) == fcNone)
+  KnownSrc.knownNot(fcNan);
+if ((ThisDemandedMask & fcInf) == fcNone)
+  KnownSrc.knownNot(fcInf);
+
+// If the source value is known negative, we can directly fold to it.
+
+// TODO: If the only sign bit difference is for 0, ignore it for nsz.
+if (KnownSrc.SignBit == true)
+  return Src;
+
+Known = KnownFPClass::fneg(KnownFPClass::fabs(KnownSrc));
+break;
+  }
   case Instruction::Call: {
 const CallInst *CI = cast(I);
 const Intrinsic::ID IID = CI->getIntrinsicID();
diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass.ll 
b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass.ll
index 714e4ff4398e2..c53cf037c8078 100644
--- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass.ll
+++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass.ll
@@ -777,6 +777,175 @@ define nofpclass(snan) float 
@fabs_src_known_negative(float nofpclass(nan pinf p
   ret float %fabs
 }
 
+define nofpclass(snan) float @fneg_fabs_src_known_negative_multiple_uses(float 
nofpclass(nan pinf pnorm psub pzero) %always.negative, ptr %ptr) {
+; CHECK-LABEL: define nofpclass(snan) float 
@fneg_fabs_src_known_negative_multiple_uses
+; CHECK-SAME: (float nofpclass(nan pinf pzero