[llvm] [Float2Int] Fix pessimization in the MinBW calculation. (PR #86051)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 21 08:58:35 PDT 2024


https://github.com/topperc updated https://github.com/llvm/llvm-project/pull/86051

>From 5ef1a1604686c234442bf8b85463b2e1a6b43f26 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Wed, 20 Mar 2024 17:50:14 -0700
Subject: [PATCH 1/2] [Float2Int] Fix pessimization in the MinBW calculation.

The MinBW was being calculated using the significant bits of the
upper and lower bounds. The upper bound is 1 past the last value in
the range so I don't think it should be included. Instead get the
min and max signed values from the range and use the significant
bits of those.

I'm still not sure if the +1 is needed after the std::max.

I haven't investigated if its possible to add a test for this. We
only look at whether the MinBW is > 32 right now. So the ranges
need to be carefully crafted to see a difference.
---
 llvm/lib/Transforms/Scalar/Float2Int.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/Float2Int.cpp b/llvm/lib/Transforms/Scalar/Float2Int.cpp
index ccca8bcc1a56ac..47042d96b46571 100644
--- a/llvm/lib/Transforms/Scalar/Float2Int.cpp
+++ b/llvm/lib/Transforms/Scalar/Float2Int.cpp
@@ -359,8 +359,8 @@ bool Float2IntPass::validateAndTransform() {
 
     // The number of bits required is the maximum of the upper and
     // lower limits, plus one so it can be signed.
-    unsigned MinBW = std::max(R.getLower().getSignificantBits(),
-                              R.getUpper().getSignificantBits()) +
+    unsigned MinBW = std::max(R.getSignedMin().getSignificantBits(),
+                              R.getSignedMax().getSignificantBits()) +
                      1;
     LLVM_DEBUG(dbgs() << "F2I: MinBitwidth=" << MinBW << ", R: " << R << "\n");
 

>From 50e93251413b8fa8c7389062ae552e435d7d41d6 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Thu, 21 Mar 2024 08:58:18 -0700
Subject: [PATCH 2/2] fixup! Use ConstantRange::getMinSignedBits().

---
 llvm/lib/Transforms/Scalar/Float2Int.cpp | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/Float2Int.cpp b/llvm/lib/Transforms/Scalar/Float2Int.cpp
index 47042d96b46571..de8c05d5689f5b 100644
--- a/llvm/lib/Transforms/Scalar/Float2Int.cpp
+++ b/llvm/lib/Transforms/Scalar/Float2Int.cpp
@@ -359,9 +359,7 @@ bool Float2IntPass::validateAndTransform() {
 
     // The number of bits required is the maximum of the upper and
     // lower limits, plus one so it can be signed.
-    unsigned MinBW = std::max(R.getSignedMin().getSignificantBits(),
-                              R.getSignedMax().getSignificantBits()) +
-                     1;
+    unsigned MinBW = R.getMinSignedBits() + 1;
     LLVM_DEBUG(dbgs() << "F2I: MinBitwidth=" << MinBW << ", R: " << R << "\n");
 
     // If we've run off the realms of the exactly representable integers,



More information about the llvm-commits mailing list