aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Support/KnownBitsTest.cpp
diff options
context:
space:
mode:
authorNoah Goldstein <goldstein.w.n@gmail.com>2024-03-05 22:03:44 -0600
committerNoah Goldstein <goldstein.w.n@gmail.com>2024-03-11 15:51:07 -0500
commitd81db0e5f5b1404ff4813af3050d671528ad45cc (patch)
tree4fa74264813641b7cde900b7a803b7fb64bfa538 /llvm/unittests/Support/KnownBitsTest.cpp
parenta9d913ebcd567ad14ffdc8c8684c4f0611e1e2da (diff)
downloadllvm-d81db0e5f5b1404ff4813af3050d671528ad45cc.zip
llvm-d81db0e5f5b1404ff4813af3050d671528ad45cc.tar.gz
llvm-d81db0e5f5b1404ff4813af3050d671528ad45cc.tar.bz2
[KnownBits] Implement knownbits `lshr`/`ashr` with exact flag
The exact flag basically allows us to set an upper bound on shift amount when we have a known 1 in `LHS`. Typically we deduce exact using knownbits (on non-exact incoming shifts), so this is particularly impactful, but may be useful in some circumstances. Closes #84254
Diffstat (limited to 'llvm/unittests/Support/KnownBitsTest.cpp')
-rw-r--r--llvm/unittests/Support/KnownBitsTest.cpp26
1 files changed, 26 insertions, 0 deletions
diff --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp
index 658f379..7c183e9 100644
--- a/llvm/unittests/Support/KnownBitsTest.cpp
+++ b/llvm/unittests/Support/KnownBitsTest.cpp
@@ -518,6 +518,19 @@ TEST(KnownBitsTest, BinaryExhaustive) {
checkOptimalityBinary, /* RefinePoisonToZero */ true);
testBinaryOpExhaustive(
[](const KnownBits &Known1, const KnownBits &Known2) {
+ return KnownBits::lshr(Known1, Known2, /*ShAmtNonZero=*/false,
+ /*Exact=*/true);
+ },
+ [](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
+ if (N2.uge(N2.getBitWidth()))
+ return std::nullopt;
+ if (!N1.extractBits(N2.getZExtValue(), 0).isZero())
+ return std::nullopt;
+ return N1.lshr(N2);
+ },
+ checkOptimalityBinary, /* RefinePoisonToZero */ true);
+ testBinaryOpExhaustive(
+ [](const KnownBits &Known1, const KnownBits &Known2) {
return KnownBits::ashr(Known1, Known2);
},
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
@@ -526,6 +539,19 @@ TEST(KnownBitsTest, BinaryExhaustive) {
return N1.ashr(N2);
},
checkOptimalityBinary, /* RefinePoisonToZero */ true);
+ testBinaryOpExhaustive(
+ [](const KnownBits &Known1, const KnownBits &Known2) {
+ return KnownBits::ashr(Known1, Known2, /*ShAmtNonZero=*/false,
+ /*Exact=*/true);
+ },
+ [](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
+ if (N2.uge(N2.getBitWidth()))
+ return std::nullopt;
+ if (!N1.extractBits(N2.getZExtValue(), 0).isZero())
+ return std::nullopt;
+ return N1.ashr(N2);
+ },
+ checkOptimalityBinary, /* RefinePoisonToZero */ true);
testBinaryOpExhaustive(
[](const KnownBits &Known1, const KnownBits &Known2) {