[llvm] [KnownBits] Optimize isConstant (NFC) (PR #191919)
Max Graey via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 15 12:46:11 PDT 2026
https://github.com/MaxGraey updated https://github.com/llvm/llvm-project/pull/191919
>From 7bd5aaeffb247c23a051ffca097d011ea36dde5c Mon Sep 17 00:00:00 2001
From: MaxGraey <maxgraey at gmail.com>
Date: Tue, 14 Apr 2026 04:18:19 +0300
Subject: [PATCH 1/3] init
---
llvm/include/llvm/Support/KnownBits.h | 8 +++++++-
llvm/lib/Support/KnownBits.cpp | 14 ++++++++++++++
2 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index ecedccc9afd95..d3ce50c7f78d1 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -52,7 +52,10 @@ struct KnownBits {
/// Returns true if we know the value of all bits.
bool isConstant() const {
- return Zero.popcount() + One.popcount() == getBitWidth();
+ if (Zero.isSingleWord())
+ return (Zero.getZExtValue() | One.getZExtValue()) ==
+ llvm::maskTrailingOnes<uint64_t>(getBitWidth());
+ return isConstantSlowCase();
}
/// Returns the value when all bits have a known value. This just returns One
@@ -576,6 +579,9 @@ struct KnownBits {
// Internal helper for getting the initial KnownBits for an `srem` or `urem`
// operation with the low-bits set.
static KnownBits remGetLowBits(const KnownBits &LHS, const KnownBits &RHS);
+
+ /// Multi-word case helper for isConstant().
+ bool isConstantSlowCase() const;
};
inline KnownBits operator&(KnownBits LHS, const KnownBits &RHS) {
diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 07e7781d0839d..fc84f7b4100a7 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -19,6 +19,20 @@
using namespace llvm;
+bool KnownBits::isConstantSlowCase() const {
+ const unsigned LastWord = Zero.getNumWords() - 1;
+ const uint64_t *Zeros = Zero.getRawData();
+ const uint64_t *Ones = One.getRawData();
+
+ for (unsigned I = 0; I != LastWord; ++I)
+ if ((Zeros[I] | Ones[I]) != UINT64_MAX)
+ return false;
+
+ unsigned TailBits = getBitWidth() - LastWord * APInt::APINT_BITS_PER_WORD;
+ uint64_t TailMask = llvm::maskTrailingOnes<uint64_t>(TailBits);
+ return ((Zeros[LastWord] | Ones[LastWord]) & TailMask) == TailMask;
+}
+
KnownBits KnownBits::flipSignBit(const KnownBits &Val) {
unsigned SignBitPosition = Val.getBitWidth() - 1;
APInt Zero = Val.Zero;
>From 1b92e666d8effe816fd64c2a21459b33e3409dcf Mon Sep 17 00:00:00 2001
From: MaxGraey <maxgraey at gmail.com>
Date: Tue, 14 Apr 2026 13:19:12 +0300
Subject: [PATCH 2/3] minor tuning
---
llvm/include/llvm/Support/KnownBits.h | 2 +-
llvm/lib/Support/KnownBits.cpp | 8 ++++----
2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index d3ce50c7f78d1..c27eb651aa64d 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -52,7 +52,7 @@ struct KnownBits {
/// Returns true if we know the value of all bits.
bool isConstant() const {
- if (Zero.isSingleWord())
+ if (LLVM_LIKELY(Zero.isSingleWord()))
return (Zero.getZExtValue() | One.getZExtValue()) ==
llvm::maskTrailingOnes<uint64_t>(getBitWidth());
return isConstantSlowCase();
diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index fc84f7b4100a7..b22acc1a2f3eb 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -20,17 +20,17 @@
using namespace llvm;
bool KnownBits::isConstantSlowCase() const {
- const unsigned LastWord = Zero.getNumWords() - 1;
+ const unsigned Last = Zero.getNumWords() - 1;
const uint64_t *Zeros = Zero.getRawData();
const uint64_t *Ones = One.getRawData();
- for (unsigned I = 0; I != LastWord; ++I)
+ for (unsigned I = 0; I != Last; ++I)
if ((Zeros[I] | Ones[I]) != UINT64_MAX)
return false;
- unsigned TailBits = getBitWidth() - LastWord * APInt::APINT_BITS_PER_WORD;
+ unsigned TailBits = getBitWidth() - Last * APInt::APINT_BITS_PER_WORD;
uint64_t TailMask = llvm::maskTrailingOnes<uint64_t>(TailBits);
- return ((Zeros[LastWord] | Ones[LastWord]) & TailMask) == TailMask;
+ return ((Zeros[Last] | Ones[Last]) & TailMask) == TailMask;
}
KnownBits KnownBits::flipSignBit(const KnownBits &Val) {
>From d13e8dcf125575903f1cc4bfb7e5d6ccc952fd93 Mon Sep 17 00:00:00 2001
From: MaxGraey <maxgraey at gmail.com>
Date: Wed, 15 Apr 2026 22:45:48 +0300
Subject: [PATCH 3/3] Revert "minor tuning"
This reverts commit 1b92e666d8effe816fd64c2a21459b33e3409dcf.
---
llvm/include/llvm/Support/KnownBits.h | 2 +-
llvm/lib/Support/KnownBits.cpp | 8 ++++----
2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index c27eb651aa64d..d3ce50c7f78d1 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -52,7 +52,7 @@ struct KnownBits {
/// Returns true if we know the value of all bits.
bool isConstant() const {
- if (LLVM_LIKELY(Zero.isSingleWord()))
+ if (Zero.isSingleWord())
return (Zero.getZExtValue() | One.getZExtValue()) ==
llvm::maskTrailingOnes<uint64_t>(getBitWidth());
return isConstantSlowCase();
diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index b22acc1a2f3eb..fc84f7b4100a7 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -20,17 +20,17 @@
using namespace llvm;
bool KnownBits::isConstantSlowCase() const {
- const unsigned Last = Zero.getNumWords() - 1;
+ const unsigned LastWord = Zero.getNumWords() - 1;
const uint64_t *Zeros = Zero.getRawData();
const uint64_t *Ones = One.getRawData();
- for (unsigned I = 0; I != Last; ++I)
+ for (unsigned I = 0; I != LastWord; ++I)
if ((Zeros[I] | Ones[I]) != UINT64_MAX)
return false;
- unsigned TailBits = getBitWidth() - Last * APInt::APINT_BITS_PER_WORD;
+ unsigned TailBits = getBitWidth() - LastWord * APInt::APINT_BITS_PER_WORD;
uint64_t TailMask = llvm::maskTrailingOnes<uint64_t>(TailBits);
- return ((Zeros[Last] | Ones[Last]) & TailMask) == TailMask;
+ return ((Zeros[LastWord] | Ones[LastWord]) & TailMask) == TailMask;
}
KnownBits KnownBits::flipSignBit(const KnownBits &Val) {
More information about the llvm-commits
mailing list