Skip to content

Conversation

@arsenm
Copy link
Contributor

@arsenm arsenm commented Dec 22, 2025

The fmul case already tries to match a literal value, we don't
need to match it twice.

@arsenm arsenm added floating-point Floating-point math llvm:analysis Includes value tracking, cost tables and constant folding llvm:transforms labels Dec 22, 2025 — with Graphite App
@arsenm arsenm marked this pull request as ready for review December 22, 2025 12:12
Copy link
Contributor Author

arsenm commented Dec 22, 2025

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 on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@llvmbot
Copy link
Member

llvmbot commented Dec 22, 2025

@llvm/pr-subscribers-llvm-support
@llvm/pr-subscribers-llvm-analysis

@llvm/pr-subscribers-llvm-transforms

Author: Matt Arsenault (arsenm)

Changes

The fmul case already tries to match a literal value, we don't
need to match it twice.


Full diff: https://github.com/llvm/llvm-project/pull/173248.diff

3 Files Affected:

  • (modified) llvm/include/llvm/Support/KnownFPClass.h (+4)
  • (modified) llvm/lib/Analysis/ValueTracking.cpp (+6-4)
  • (modified) llvm/lib/Support/KnownFPClass.cpp (+4)
diff --git a/llvm/include/llvm/Support/KnownFPClass.h b/llvm/include/llvm/Support/KnownFPClass.h
index b3c18bcf6b34b..a34e5eb7a1698 100644
--- a/llvm/include/llvm/Support/KnownFPClass.h
+++ b/llvm/include/llvm/Support/KnownFPClass.h
@@ -19,6 +19,7 @@
 #include <optional>
 
 namespace llvm {
+class APFloat;
 
 struct KnownFPClass {
   /// Floating-point classes the value could be one of.
@@ -28,6 +29,9 @@ struct KnownFPClass {
   /// definitely set or false if the sign bit is definitely unset.
   std::optional<bool> SignBit;
 
+  KnownFPClass() = default;
+  KnownFPClass(const APFloat &C);
+
   bool operator==(KnownFPClass Other) const {
     return KnownFPClasses == Other.KnownFPClasses && SignBit == Other.SignBit;
   }
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index c4ad71f668506..d657883634df2 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -4920,8 +4920,7 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
   assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
 
   if (auto *CFP = dyn_cast<ConstantFP>(V)) {
-    Known.KnownFPClasses = CFP->getValueAPF().classify();
-    Known.SignBit = CFP->isNegative();
+    Known = KnownFPClass(CFP->getValueAPF());
     return;
   }
 
@@ -5723,8 +5722,6 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
       Known.knownNot(fcNegative);
 
     KnownFPClass KnownLHS, KnownRHS;
-    computeKnownFPClass(Op->getOperand(1), DemandedElts, fcAllFlags, KnownRHS,
-                        Q, Depth + 1);
 
     const APFloat *CRHS;
     if (match(Op->getOperand(1), m_APFloat(CRHS))) {
@@ -5741,6 +5738,11 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
       int MinKnownExponent = ilogb(*CRHS);
       if (MinKnownExponent >= MantissaBits)
         Known.knownNot(fcSubnormal);
+
+      KnownRHS = KnownFPClass(*CRHS);
+    } else {
+      computeKnownFPClass(Op->getOperand(1), DemandedElts, fcAllFlags, KnownRHS,
+                          Q, Depth + 1);
     }
 
     computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
diff --git a/llvm/lib/Support/KnownFPClass.cpp b/llvm/lib/Support/KnownFPClass.cpp
index 43fb2e7108d2b..88de3a57ba415 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -12,10 +12,14 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Support/KnownFPClass.h"
+#include "llvm/ADT/APFloat.h"
 #include "llvm/Support/ErrorHandling.h"
 
 using namespace llvm;
 
+KnownFPClass::KnownFPClass(const APFloat &C)
+    : KnownFPClasses(C.classify()), SignBit(C.isNegative()) {}
+
 /// Return true if it's possible to assume IEEE treatment of input denormals in
 /// \p F for \p Val.
 static bool inputDenormalIsIEEE(DenormalMode Mode) {

@github-actions
Copy link

github-actions bot commented Dec 22, 2025

🪟 Windows x64 Test Results

  • 128866 tests passed
  • 2839 tests skipped

✅ The build succeeded and all tests passed.

@github-actions
Copy link

github-actions bot commented Dec 22, 2025

🐧 Linux x64 Test Results

  • 187781 tests passed
  • 4982 tests skipped

✅ The build succeeded and all tests passed.

The fmul case already tries to match a literal value, we don't
need to match it twice.
@arsenm arsenm force-pushed the users/arsenm/valuetracking/improve-fmul-0-handling branch from eb5db7e to ab51313 Compare December 22, 2025 12:56
@arsenm arsenm force-pushed the users/arsenm/valuetracking/avoid-computeKnownFPClass-constant-fmul branch from 578dde3 to 57ea81c Compare December 22, 2025 12:56
Copy link
Member

@dtcxzyw dtcxzyw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

floating-point Floating-point math llvm:analysis Includes value tracking, cost tables and constant folding llvm:support llvm:transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants