[clang] [llvm] [ConstantRange] Estimate tighter lower (upper) bounds for masked binary and (or) (PR #120352)

Stephen Senran Zhang via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 23 23:20:54 PST 2024


zsrkmyn wrote:

Ah, by applying the patch below, I just found the lower bound is still not optimal for non-wrapped cases. E.g., for 4-bit ints, given 2 ranges,

```
[0011, 1111]
[1101, 1111]
```

the optimal lower bound is 1, while the algorithm gives 0.

```diff
diff --git a/llvm/unittests/IR/ConstantRangeTest.cpp b/llvm/unittests/IR/ConstantRangeTest.cpp
index e1d9b3e387b2..74a49e6842cb 100644
--- a/llvm/unittests/IR/ConstantRangeTest.cpp
+++ b/llvm/unittests/IR/ConstantRangeTest.cpp
@@ -112,6 +112,10 @@ bool PreferSmallestNonFullSigned(const ConstantRange &CR1,
   return PreferSmallestSigned(CR1, CR2);
 }
 
+bool PreferSmallerLowerBound(const ConstantRange &CR1, const ConstantRange &CR2) {
+  return CR1.getLower().ult(CR2.getLower());
+}
+
 testing::AssertionResult rangeContains(const ConstantRange &CR, const APInt &N,
                                        ArrayRef<ConstantRange> Inputs) {
   if (CR.contains(N))
@@ -2726,6 +2730,13 @@ TEST_F(ConstantRangeTest, binaryAnd) {
       },
       [](const APInt &N1, const APInt &N2) { return N1 & N2; }, PreferSmallest,
       CheckSingleElementsOnly);
+
+  TestBinaryOpExhaustive(
+      [](const ConstantRange &CR1, const ConstantRange &CR2) {
+        return CR1.binaryAnd(CR2);
+      },
+      [](const APInt &N1, const APInt &N2) { return N1 & N2; },
+      PreferSmallerLowerBound, CheckNonWrappedOnly);
 }
 
 TEST_F(ConstantRangeTest, binaryOr) {
```

https://github.com/llvm/llvm-project/pull/120352


More information about the llvm-commits mailing list