[llvm] [InstCombine] Fold unsigned range checks with small complements (PR #223482)
Lakshay Chauhan via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 14 11:10:59 PDT 2026
https://github.com/nos1dot618 created https://github.com/llvm/llvm-project/pull/223482
## Summary
Fold unsigned range checks with a small complement into direct inequality checks.
For example:
```llvm
%v1 = add i8 %x, -8
%v3 = icmp ugt i8 %v1, 2
%v5 = icmp eq i8 %v1, 1
%r = or i1 %v3, %v5
```
can be simplified to:
```llvm
%a = icmp ne i8 %x, 8
%b = icmp ne i8 %x, 10
%r = and i1 %a, %b
```
The original expression is false only when `%v1` is `0` or `2`, corresponding to `%x` being `8` or `10`.
## Changes
* Add an InstCombine fold for unsigned `ugt` + `eq` OR patterns.
* Use `APInt` for modular arithmetic when translating values through the offset.
* Limit the transformation to small complements to avoid excessive IR growth.
* Restrict the fold to bitwise OR to preserve logical OR semantics.
* Add regression tests covering the reproducer, commuted operands, wraparound, and large complements.
Fixes #223251.
## AI Disclosure
This PR was developed with assistance from AI tools.
>From 02370676e99ea7c0e9f0162fbc1364ea842a8386 Mon Sep 17 00:00:00 2001
From: ninthcircle <mainlakshayhoon at gmail.com>
Date: Mon, 14 Sep 2026 23:36:03 +0530
Subject: [PATCH] [InstCombine] Fold unsigned range checks with small
complements
Fold patterns of the form
(icmp ugt (X + Offset), N) | (icmp eq (X + Offset), K)
when the complement of the unsigned range is small.
The transformation converts the excluded values into a conjunction of icmp ne checks on the original value. Limit the number of generated comparisons to avoid increasing IR size excessively.
Handle modular arithmetic with APInt so that wrapped offsets are transformed correctly.
---
.../InstCombine/InstCombineAndOrXor.cpp | 81 +++++++++++++++++++
.../test/Transforms/InstCombine/and-xor-or.ll | 72 +++++++++++++++++
2 files changed, 153 insertions(+)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 7fd49370fab28..8165431c51980 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -3439,6 +3439,81 @@ static Value *foldAndOrOfICmpEqConstantAndICmp(CmpPredicate PredL, Value *LHS0,
Other);
}
+// (icmp ugt (X + Offset), N) | (icmp eq (X + Offset), K)
+// -> conjunction of != comparisons for the small set of values
+// where the original expression is false.
+static Value *foldUnsignedRangeWithSmallComplement(CmpPredicate PredL,
+ Value *LHS0, Value *LHS1,
+ CmpPredicate PredR,
+ Value *RHS0, Value *RHS1,
+ IRBuilderBase &Builder) {
+ if (!((PredL == ICmpInst::ICMP_UGT && PredR == ICmpInst::ICMP_EQ) ||
+ (PredR == ICmpInst::ICMP_UGT && PredL == ICmpInst::ICMP_EQ)))
+ return nullptr;
+
+ Value *RangeValue;
+ Value *EqValue;
+ Value *RangeLimit;
+ Value *EqConstant;
+
+ if (PredL == ICmpInst::ICMP_UGT) {
+ RangeValue = LHS0;
+ RangeLimit = LHS1;
+ EqValue = RHS0;
+ EqConstant = RHS1;
+ } else {
+ RangeValue = RHS0;
+ RangeLimit = RHS1;
+ EqValue = LHS0;
+ EqConstant = LHS1;
+ }
+
+ const APInt *N;
+ const APInt *K;
+ if (!match(RangeLimit, m_APInt(N)) || !match(EqConstant, m_APInt(K)) ||
+ RangeValue != EqValue)
+ return nullptr;
+
+ Value *X;
+ const APInt *Offset;
+
+ if (!match(RangeValue, m_Add(m_Value(X), m_APInt(Offset))) &&
+ !match(RangeValue, m_Add(m_APInt(Offset), m_Value(X))))
+ return nullptr;
+
+ // (Y >u N) is false for Y = 0..N. Remove K from that set when
+ // the equality makes the complete expression true at K.
+ if (N->isMaxValue())
+ return nullptr;
+
+ APInt NumExcluded = *N + 1;
+ if (K->ule(*N))
+ --NumExcluded;
+
+ constexpr unsigned MaxExcludedValues = 4;
+ if (NumExcluded.ugt(MaxExcludedValues))
+ return nullptr;
+
+ if (NumExcluded.isZero())
+ return ConstantInt::getTrue(Builder.getContext());
+
+ Value *Result = nullptr;
+ APInt Y = APInt::getZero(N->getBitWidth());
+
+ for (unsigned I = 0, E = NumExcluded.getZExtValue(); I != E; ++I, ++Y) {
+ if (Y == *K)
+ continue;
+
+ APInt XValue = Y - *Offset;
+ Value *Cmp = Builder.CreateICmp(ICmpInst::ICMP_NE, X,
+ ConstantInt::get(X->getType(), XValue));
+
+ Result = Result ? Builder.CreateAnd(Result, Cmp) : Cmp;
+ }
+
+ return Result;
+}
+
/// Fold (icmp)&(icmp) or (icmp)|(icmp) if possible.
/// If IsLogical is true, then the and/or is in select form and the transform
/// must be poison-safe.
@@ -3527,6 +3602,12 @@ Value *InstCombinerImpl::foldAndOrOfICmps(Value *LHS, Value *RHS,
return V;
}
+ if (!IsAnd && !IsLogical) {
+ if (Value *V = foldUnsignedRangeWithSmallComplement(
+ PredL, LHS0, LHS1, PredR, RHS0, RHS1, Builder))
+ return V;
+ }
+
// TODO: Add conjugated or fold, check whether it is safe for logical and/or.
if (IsAnd && !IsLogical)
if (Value *V = foldSignedTruncationCheck(PredL, LHS0, LHS1, PredR, RHS0,
diff --git a/llvm/test/Transforms/InstCombine/and-xor-or.ll b/llvm/test/Transforms/InstCombine/and-xor-or.ll
index 1750aea8e1eaa..3e0c2e88396c5 100644
--- a/llvm/test/Transforms/InstCombine/and-xor-or.ll
+++ b/llvm/test/Transforms/InstCombine/and-xor-or.ll
@@ -5313,3 +5313,75 @@ define i1 @test_and_xor_freely_invertable_multiuse(i32 %x, i32 %y, i1 %z) {
%and = and i1 %xor, %z
ret i1 %and
}
+
+; CHECK-LABEL: @fold_unsigned_range_with_small_complement(
+; CHECK-NEXT: [[A:%.*]] = icmp ne i8 [[X:%.*]], 8
+; CHECK-NEXT: [[B:%.*]] = icmp ne i8 [[X]], 10
+; CHECK-NEXT: [[R:%.*]] = and i1 [[A]], [[B]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+define i1 @fold_unsigned_range_with_small_complement(i8 %x) {
+ %v1 = add i8 %x, -8
+ %v3 = icmp ugt i8 %v1, 2
+ %v5 = icmp eq i8 %v1, 1
+ %r = or i1 %v3, %v5
+ ret i1 %r
+}
+
+; CHECK-LABEL: @fold_unsigned_range_with_small_complement_commuted(
+; CHECK-NEXT: [[A:%.*]] = icmp ne i8 [[X:%.*]], 8
+; CHECK-NEXT: [[B:%.*]] = icmp ne i8 [[X]], 10
+; CHECK-NEXT: [[R:%.*]] = and i1 [[A]], [[B]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+define i1 @fold_unsigned_range_with_small_complement_commuted(i8 %x) {
+ %v1 = add i8 %x, -8
+ %v3 = icmp ugt i8 %v1, 2
+ %v5 = icmp eq i8 %v1, 1
+ %r = or i1 %v5, %v3
+ ret i1 %r
+}
+
+; CHECK-LABEL: @fold_unsigned_range_with_wraparound(
+; CHECK-NEXT: [[A:%.*]] = icmp ne i8 [[X:%.*]], 246
+; CHECK-NEXT: [[B:%.*]] = icmp ne i8 [[X]], 248
+; CHECK-NEXT: [[R:%.*]] = and i1 [[A]], [[B]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+define i1 @fold_unsigned_range_with_wraparound(i8 %x) {
+ %v1 = add i8 %x, 10
+ %v3 = icmp ugt i8 %v1, 2
+ %v5 = icmp eq i8 %v1, 1
+ %r = or i1 %v3, %v5
+ ret i1 %r
+}
+
+; CHECK-LABEL: @fold_unsigned_range_without_hole(
+; CHECK: [[A:%.*]] = icmp ne i8 [[X:%.*]], 8
+; CHECK: [[B:%.*]] = icmp ne i8 [[X]], 9
+; CHECK: [[C:%.*]] = icmp ne i8 [[X]], 10
+; CHECK: [[R:%.*]] = and i1
+; CHECK: ret i1
+;
+define i1 @fold_unsigned_range_without_hole(i8 %x) {
+ %v1 = add i8 %x, -8
+ %v3 = icmp ugt i8 %v1, 2
+ %v5 = icmp eq i8 %v1, 5
+ %r = or i1 %v3, %v5
+ ret i1 %r
+}
+
+; CHECK-LABEL: @do_not_expand_large_complement(
+; CHECK: %[[V:%.*]] = add i8 %x, -8
+; CHECK: %[[A:%.*]] = icmp ugt i8 %[[V]], 4
+; CHECK: %[[B:%.*]] = icmp eq i8 %[[V]], 0
+; CHECK: %[[R:%.*]] = or i1 %[[A]], %[[B]]
+; CHECK: ret i1 %[[R]]
+;
+define i1 @do_not_expand_large_complement(i8 %x) {
+ %v1 = add i8 %x, -8
+ %v3 = icmp ugt i8 %v1, 4
+ %v5 = icmp eq i8 %v1, 0
+ %r = or i1 %v3, %v5
+ ret i1 %r
+}
More information about the llvm-commits
mailing list