[llvm] [KnownBits] Simplify and optimize isConstant (NFC) (PR #191919)
Max Graey via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 14 01:40:16 PDT 2026
https://github.com/MaxGraey updated https://github.com/llvm/llvm-project/pull/191919
>From 9d46fe170eb503419bb2d02fdd50325c4f375f60 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] simplify and optimize
---
llvm/include/llvm/Support/KnownBits.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index ecedccc9afd95..ee2477efa153f 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 {
- return Zero.popcount() + One.popcount() == getBitWidth();
+ return (Zero | One).isAllOnes();
}
/// Returns the value when all bits have a known value. This just returns One
>From 11194c63a83f743af5f4ba105c5c4dc8d425ccfc Mon Sep 17 00:00:00 2001
From: MaxGraey <maxgraey at gmail.com>
Date: Tue, 14 Apr 2026 04:23:51 +0300
Subject: [PATCH 2/3] format
---
llvm/include/llvm/Support/KnownBits.h | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index ee2477efa153f..96de46d821e8a 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -51,9 +51,7 @@ struct KnownBits {
bool hasConflict() const { return Zero.intersects(One); }
/// Returns true if we know the value of all bits.
- bool isConstant() const {
- return (Zero | One).isAllOnes();
- }
+ bool isConstant() const { return (Zero | One).isAllOnes(); }
/// Returns the value when all bits have a known value. This just returns One
/// with a protective assertion.
>From 7819d75d6eeb17e381398d5d86cb28410e7d07fe Mon Sep 17 00:00:00 2001
From: MaxGraey <maxgraey at gmail.com>
Date: Tue, 14 Apr 2026 11:40:01 +0300
Subject: [PATCH 3/3] use hybrid approach
---
llvm/include/llvm/Support/KnownBits.h | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index 96de46d821e8a..7f56149be6631 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -51,7 +51,13 @@ struct KnownBits {
bool hasConflict() const { return Zero.intersects(One); }
/// Returns true if we know the value of all bits.
- bool isConstant() const { return (Zero | One).isAllOnes(); }
+ bool isConstant() const {
+ if (Zero.isSingleWord()) {
+ return (Zero.getZExtValue() | One.getZExtValue()) ==
+ llvm::maskTrailingOnes<uint64_t>(getBitWidth());
+ }
+ return Zero.popcount() + One.popcount() == getBitWidth();
+ }
/// Returns the value when all bits have a known value. This just returns One
/// with a protective assertion.
More information about the llvm-commits
mailing list