[llvm] [LVI] Generalize mask not equal conditions handling (PR #92946)
Antonio Frighetto via llvm-commits
llvm-commits at lists.llvm.org
Sat Jun 8 08:00:03 PDT 2024
https://github.com/antoniofrighetto updated https://github.com/llvm/llvm-project/pull/92946
>From 79c7858fdfa68f94b100678d5bcfe68dc0be9834 Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Thu, 6 Jun 2024 08:26:40 +0200
Subject: [PATCH] [LVI][ConstantRange] Generalize mask not equal conditions
handling
Extend `V & Mask != 0` for non-zero constants if satisfiable, when
retrieving constraint value information from a non-equality comparison.
Proofs: https://alive2.llvm.org/ce/z/PNDN3K.
Motivating example: https://github.com/gcc-mirror/gcc/blob/master/gcc/testsuite/gcc.dg/tree-ssa/vrp76.c.
---
llvm/include/llvm/IR/ConstantRange.h | 5 +++
llvm/lib/Analysis/LazyValueInfo.cpp | 11 ++----
llvm/lib/IR/ConstantRange.cpp | 23 +++++++++++
.../CorrelatedValuePropagation/icmp.ll | 38 ++++++++++++++++++-
llvm/unittests/IR/ConstantRangeTest.cpp | 27 +++++++++++++
5 files changed, 96 insertions(+), 8 deletions(-)
diff --git a/llvm/include/llvm/IR/ConstantRange.h b/llvm/include/llvm/IR/ConstantRange.h
index a5e2f809ab411..65cf8188606c8 100644
--- a/llvm/include/llvm/IR/ConstantRange.h
+++ b/llvm/include/llvm/IR/ConstantRange.h
@@ -176,6 +176,11 @@ class [[nodiscard]] ConstantRange {
const APInt &Other,
unsigned NoWrapKind);
+ /// Initialize a range containing all values X that satisfy `(X & Mask)
+ /// != C`. Note that the range returned may still contain values where `(X &
+ /// Mask) == C` holds, making it less precise, but still conservative.
+ static ConstantRange makeMaskNotEqualRange(const APInt &Mask, const APInt &C);
+
/// Returns true if ConstantRange calculations are supported for intrinsic
/// with \p IntrinsicID.
static bool isIntrinsicSupported(Intrinsic::ID IntrinsicID);
diff --git a/llvm/lib/Analysis/LazyValueInfo.cpp b/llvm/lib/Analysis/LazyValueInfo.cpp
index 6cded828c25f4..4b78e5894a019 100644
--- a/llvm/lib/Analysis/LazyValueInfo.cpp
+++ b/llvm/lib/Analysis/LazyValueInfo.cpp
@@ -1191,13 +1191,10 @@ std::optional<ValueLatticeElement> LazyValueInfoImpl::getValueFromICmpCondition(
return ValueLatticeElement::getRange(
ConstantRange::fromKnownBits(Known, /*IsSigned*/ false));
}
- // If (Val & Mask) != 0 then the value must be larger than the lowest set
- // bit of Mask.
- if (EdgePred == ICmpInst::ICMP_NE && !Mask->isZero() && C->isZero()) {
- return ValueLatticeElement::getRange(ConstantRange::getNonEmpty(
- APInt::getOneBitSet(BitWidth, Mask->countr_zero()),
- APInt::getZero(BitWidth)));
- }
+
+ if (EdgePred == ICmpInst::ICMP_NE && !Mask->isZero())
+ return ValueLatticeElement::getRange(
+ ConstantRange::makeMaskNotEqualRange(*Mask, *C));
}
// If (X urem Modulus) >= C, then X >= C.
diff --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp
index 08041c96ffe5a..80bcf09710724 100644
--- a/llvm/lib/IR/ConstantRange.cpp
+++ b/llvm/lib/IR/ConstantRange.cpp
@@ -364,6 +364,29 @@ ConstantRange ConstantRange::makeExactNoWrapRegion(Instruction::BinaryOps BinOp,
return makeGuaranteedNoWrapRegion(BinOp, ConstantRange(Other), NoWrapKind);
}
+ConstantRange ConstantRange::makeMaskNotEqualRange(const APInt &Mask,
+ const APInt &C) {
+ assert(!Mask.isZero() && "Mask cannot be zero.");
+ unsigned BitWidth = Mask.getBitWidth();
+ unsigned TrailingZeroesOfMask = Mask.countr_zero();
+
+ // If (Val & Mask) != 0 then the value must be larger than the lowest set
+ // bit of Mask.
+ if (C.isZero())
+ return ConstantRange::getNonEmpty(
+ APInt::getOneBitSet(BitWidth, TrailingZeroesOfMask),
+ APInt::getZero(BitWidth));
+
+ // If (Val & Mask) != C, constrained to the non-equality being
+ // satisfiable, then the value must be larger than the lowest set bit of
+ // Mask, offset by constant C.
+ if ((Mask & C) == C)
+ return ConstantRange::getNonEmpty(
+ APInt::getOneBitSet(BitWidth, TrailingZeroesOfMask) + C, C);
+
+ return getFull(BitWidth);
+}
+
bool ConstantRange::isFullSet() const {
return Lower == Upper && Lower.isMaxValue();
}
diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll b/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
index b5337b9ddc248..ca70713440219 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
@@ -595,7 +595,7 @@ define i1 @test_assume_cmp_with_offset_or(i64 %idx, i1 %other) {
; CHECK: T:
; CHECK-NEXT: ret i1 true
; CHECK: F:
-; CHECK-NEXT: ret i1 [[CMP2:%.*]]
+; CHECK-NEXT: ret i1 [[OTHER:%.*]]
;
%idx.off1 = or disjoint i64 %idx, 5
%cmp1 = icmp ugt i64 %idx.off1, 10
@@ -1475,3 +1475,39 @@ entry:
%select = select i1 %cmp1, i1 %cmp2, i1 false
ret i1 %select
}
+
+declare void @opaque()
+
+define void @test_icmp_ne_from_implied_range(i32 noundef %arg) {
+; CHECK-LABEL: @test_icmp_ne_from_implied_range(
+; CHECK-NEXT: [[AND_MASK:%.*]] = and i32 [[ARG:%.*]], -8
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[AND_MASK]], -16
+; CHECK-NEXT: br i1 [[CMP]], label [[END:%.*]], label [[ELSE:%.*]]
+; CHECK: else:
+; CHECK-NEXT: br label [[END]]
+; CHECK: sw.case:
+; CHECK-NEXT: call void @opaque()
+; CHECK-NEXT: br label [[END]]
+; CHECK: end:
+; CHECK-NEXT: ret void
+;
+ %and.mask = and i32 %arg, -8
+ %cmp = icmp eq i32 %and.mask, -16
+ br i1 %cmp, label %end, label %else
+
+else:
+ ; %arg is within [-8, -16).
+ switch i32 %arg, label %end [
+ i32 -16, label %sw.case
+ i32 -12, label %sw.case
+ i32 -9, label %sw.case
+ ]
+
+sw.case:
+ call void @opaque()
+ br label %end
+
+end:
+ ; %arg is within [-16, -8).
+ ret void
+}
diff --git a/llvm/unittests/IR/ConstantRangeTest.cpp b/llvm/unittests/IR/ConstantRangeTest.cpp
index ac2075cb4af47..314d07a2f1245 100644
--- a/llvm/unittests/IR/ConstantRangeTest.cpp
+++ b/llvm/unittests/IR/ConstantRangeTest.cpp
@@ -2788,4 +2788,31 @@ TEST_F(ConstantRangeTest, isSizeLargerThan) {
EXPECT_FALSE(One.isSizeLargerThan(1));
}
+TEST_F(ConstantRangeTest, MakeMaskNotEqualRangeExhaustive) {
+ unsigned Bits = 4;
+ unsigned Max = 1 << Bits;
+
+ for (unsigned MaskVal = 1; MaskVal < Max; ++MaskVal) {
+ APInt Mask(Bits, MaskVal);
+ for (unsigned CVal = 0; CVal < Max; ++CVal) {
+ APInt C(Bits, CVal);
+
+ SmallBitVector Elems(Max);
+ for (unsigned N = 0; N < Max; ++N) {
+ APInt Num(Bits, N);
+ if ((Num & Mask) == C)
+ continue;
+ Elems.set(Num.getZExtValue());
+ }
+
+ // Do not check for optimality, as levelling off for efficiency. E.g.,
+ // given Mask = 0b0011, C = 0b0000, the optimal range would be FullSet ∖
+ // {0, 4, 8, 12}, however we conservatively return [1, 0).
+ auto CR = ConstantRange::makeMaskNotEqualRange(Mask, C);
+ TestRange(CR, Elems, PreferSmallestUnsigned, {}, false);
+ TestRange(CR, Elems, PreferSmallestSigned, {}, false);
+ }
+ }
+}
+
} // anonymous namespace
More information about the llvm-commits
mailing list