[llvm] [KnownBits] Add KnownBits::makeConstantRange to determine the known bits from a pair of unsigned lo/hi bound values (PR #107080)
Simon Pilgrim via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 3 03:43:33 PDT 2024
https://github.com/RKSimon created https://github.com/llvm/llvm-project/pull/107080
If we have the lower/upper bounds for a value, determine the known bits shared by all values within those bounds.
Unlike llvm::ConstantRange, these bounds values are INCLUSIVE (to stop overflow issues in some future use cases).
>From 32c031b3a9880d2d409b02c0a1bab1823823409f Mon Sep 17 00:00:00 2001
From: Simon Pilgrim <llvm-dev at redking.me.uk>
Date: Tue, 3 Sep 2024 11:40:35 +0100
Subject: [PATCH] [KnownBits] Add KnownBits::makeConstantRange to determine the
known bits from a pair of unsigned lo/hi bound values
If we have the lower/upper bounds for a value, determine the known bits shared by all values within those bounds.
Unlike llvm::ConstantRange, these bounds values are INCLUSIVE (to stop overflow issues in some future use cases).
---
llvm/include/llvm/Support/KnownBits.h | 3 +++
llvm/lib/Support/KnownBits.cpp | 14 ++++++++++++++
llvm/unittests/Support/KnownBitsTest.cpp | 17 +++++++++++++++++
3 files changed, 34 insertions(+)
diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index e4ec202f36aae0..b169a0aba3edac 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -291,6 +291,9 @@ struct KnownBits {
return KnownBits(~C, C);
}
+ /// Create known bits from a known INCLUSIVE pair of unsigned constants.
+ static KnownBits makeConstantRange(const APInt &Lower, const APInt &Upper);
+
/// Returns KnownBits information that is known to be true for both this and
/// RHS.
///
diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 8e31e0ced2d731..5c37575d157358 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -750,6 +750,20 @@ static KnownBits computeForSatAddSub(bool Add, bool Signed,
return Res;
}
+KnownBits KnownBits::makeConstantRange(const APInt &Lower, const APInt &Upper) {
+ assert(Lower.getBitWidth() == Upper.getBitWidth() && "Bitwidth mismatch");
+ assert(Lower.ule(Upper) && "Constant range mismatch");
+
+ KnownBits Known = KnownBits::makeConstant(Lower);
+ if (std::optional<unsigned> DifferentBit =
+ APIntOps::GetMostSignificantDifferentBit(Lower, Upper)) {
+ Known.Zero.clearLowBits(*DifferentBit + 1);
+ Known.One.clearLowBits(*DifferentBit + 1);
+ }
+
+ return Known;
+}
+
KnownBits KnownBits::sadd_sat(const KnownBits &LHS, const KnownBits &RHS) {
return computeForSatAddSub(/*Add*/ true, /*Signed*/ true, LHS, RHS);
}
diff --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp
index b6e16f809ea779..f10a153225ab2c 100644
--- a/llvm/unittests/Support/KnownBitsTest.cpp
+++ b/llvm/unittests/Support/KnownBitsTest.cpp
@@ -668,6 +668,23 @@ TEST(KnownBitsTest, ICmpExhaustive) {
});
}
+TEST(KnownBitsTest, ConstantRange) {
+ unsigned Bits = 4;
+ unsigned MaxValue = (1U << Bits) - 1;
+ for (unsigned Lo = 0; Lo <= MaxValue; ++Lo) {
+ const APInt LoInt(Bits, Lo);
+ for (unsigned Hi = Lo; Hi <= MaxValue; ++Hi) {
+ KnownBits Ref = KnownBits::makeConstant(LoInt);
+ for (unsigned I = Lo + 1; I <= Hi; ++I)
+ Ref = Ref.intersectWith(KnownBits::makeConstant(APInt(Bits, I)));
+
+ KnownBits Known = KnownBits::makeConstantRange(LoInt, APInt(Bits, Hi));
+ EXPECT_EQ(Ref.Zero, Known.Zero);
+ EXPECT_EQ(Ref.One, Known.One);
+ }
+ }
+}
+
TEST(KnownBitsTest, GetMinMaxVal) {
unsigned Bits = 4;
ForeachKnownBits(Bits, [&](const KnownBits &Known) {
More information about the llvm-commits
mailing list