[llvm] [KnownBits] Optimize isConstant (NFC) (PR #191919)
Max Graey via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 14 02:22:09 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] 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;
More information about the llvm-commits
mailing list