[llvm] 2de38b4 - [InstCombine] Fold comparisons of urem with dividend (#214933)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 9 08:48:34 PDT 2026
Author: Hadong Lee
Date: 2026-09-09T23:48:29+08:00
New Revision: 2de38b4861ed4a7be7ddd42fc803e91d1fe81b14
URL: https://github.com/llvm/llvm-project/commit/2de38b4861ed4a7be7ddd42fc803e91d1fe81b14
DIFF: https://github.com/llvm/llvm-project/commit/2de38b4861ed4a7be7ddd42fc803e91d1fe81b14.diff
LOG: [InstCombine] Fold comparisons of urem with dividend (#214933)
This folds equality comparisons of `X urem Y` with `X`
- `(X urem Y) == X` to `X ult Y`
- `(X urem Y) != X` to `X uge Y`
For a defined `urem`, let `Q = X udiv Y` and `R = X urem Y`. Unsigned
division gives `X = Q * Y + R`, so:
```
R == X
<=> Q * Y == 0
<=> Q == 0 (since Y != 0)
<=> X < Y
```
Division by zero is undefined behavior, so no separate handling for `Y
== 0` is required.
This handles commuted compare operands, scalar and vector types, and
remainders with additional uses
Alive2: https://alive2.llvm.org/ce/z/3ryQaE
simplified proof with both folds: https://alive2.llvm.org/ce/z/v39F4d
Fixes #214480
Added:
llvm/test/Transforms/InstCombine/icmp-urem.ll
Modified:
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 270d3596ad679..a6e5bd6296d3f 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -5312,6 +5312,18 @@ Instruction *InstCombinerImpl::foldICmpBinOp(ICmpInst &I,
return NewICmp;
const CmpInst::Predicate Pred = I.getPredicate();
+
+ // (X urem Y) == X --> X u< Y
+ // (X urem Y) != X --> X u>= Y
+ Value *Dividend, *Divisor;
+ if (I.isEquality() &&
+ match(&I, m_c_ICmp(m_URem(m_Value(Dividend), m_Value(Divisor)),
+ m_Deferred(Dividend)))) {
+ CmpInst::Predicate NewPred =
+ Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGE;
+ return new ICmpInst(NewPred, Dividend, Divisor);
+ }
+
Value *X;
// Convert add-with-unsigned-overflow comparisons into a 'not' with compare.
diff --git a/llvm/test/Transforms/InstCombine/icmp-urem.ll b/llvm/test/Transforms/InstCombine/icmp-urem.ll
new file mode 100644
index 0000000000000..8d9803dede045
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/icmp-urem.ll
@@ -0,0 +1,149 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+; (urem X, Y) == X --> X ult Y
+; (urem X, Y) != X --> X uge Y
+
+; Scalar tests.
+
+define i1 @icmp_eq_urem_dividend(i32 %x, i32 %y) {
+; CHECK-LABEL: define i1 @icmp_eq_urem_dividend(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X]], [[Y]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %rem = urem i32 %x, %y
+ %cmp = icmp eq i32 %rem, %x
+ ret i1 %cmp
+}
+
+; The compare operands may be commuted.
+
+define i1 @icmp_ne_urem_dividend_commuted(i32 %x, i32 %y) {
+; CHECK-LABEL: define i1 @icmp_ne_urem_dividend_commuted(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
+; CHECK-NEXT: [[CMP:%.*]] = icmp uge i32 [[X]], [[Y]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %rem = urem i32 %x, %y
+ %cmp = icmp ne i32 %x, %rem
+ ret i1 %cmp
+}
+
+; Vector tests.
+
+define <2 x i1> @icmp_eq_urem_dividend_v2i32(<2 x i32> %x, <2 x i32> %y) {
+; CHECK-LABEL: define <2 x i1> @icmp_eq_urem_dividend_v2i32(
+; CHECK-SAME: <2 x i32> [[X:%.*]], <2 x i32> [[Y:%.*]]) {
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult <2 x i32> [[X]], [[Y]]
+; CHECK-NEXT: ret <2 x i1> [[CMP]]
+;
+ %rem = urem <2 x i32> %x, %y
+ %cmp = icmp eq <2 x i32> %rem, %x
+ ret <2 x i1> %cmp
+}
+
+define <vscale x 2 x i1> @icmp_ne_urem_dividend_nxv2i32(
+; CHECK-LABEL: define <vscale x 2 x i1> @icmp_ne_urem_dividend_nxv2i32(
+; CHECK-SAME: <vscale x 2 x i32> [[X:%.*]], <vscale x 2 x i32> [[Y:%.*]]) {
+; CHECK-NEXT: [[CMP:%.*]] = icmp uge <vscale x 2 x i32> [[X]], [[Y]]
+; CHECK-NEXT: ret <vscale x 2 x i1> [[CMP]]
+;
+ <vscale x 2 x i32> %x, <vscale x 2 x i32> %y) {
+ %rem = urem <vscale x 2 x i32> %x, %y
+ %cmp = icmp ne <vscale x 2 x i32> %rem, %x
+ ret <vscale x 2 x i1> %cmp
+}
+
+; The fold does not require the urem to have one use.
+
+declare void @use_i32(i32)
+
+define i1 @icmp_eq_urem_dividend_extra_use(i32 %x, i32 %y) {
+; CHECK-LABEL: define i1 @icmp_eq_urem_dividend_extra_use(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
+; CHECK-NEXT: [[REM:%.*]] = urem i32 [[X]], [[Y]]
+; CHECK-NEXT: call void @use_i32(i32 [[REM]])
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X]], [[Y]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %rem = urem i32 %x, %y
+ call void @use_i32(i32 %rem)
+ %cmp = icmp eq i32 %rem, %x
+ ret i1 %cmp
+}
+
+; Preserve the umax simplification after replacing the comparison.
+
+declare i64 @llvm.umax.i64(i64, i64)
+
+define i64 @icmp_eq_urem_dividend_umax(i64 %x, i64 %y) {
+; CHECK-LABEL: define i64 @icmp_eq_urem_dividend_umax(
+; CHECK-SAME: i64 [[X:%.*]], i64 [[Y:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[X]], [[Y]]
+; CHECK-NEXT: br i1 [[CMP]], label %[[EXIT:.*]], label %[[BODY:.*]]
+; CHECK: [[BODY]]:
+; CHECK-NEXT: [[REM:%.*]] = urem i64 [[X]], [[Y]]
+; CHECK-NEXT: [[SUB:%.*]] = sub nuw i64 [[X]], [[REM]]
+; CHECK-NEXT: ret i64 [[SUB]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret i64 0
+;
+entry:
+ %rem = urem i64 %x, %y
+ %sub = sub nuw i64 %x, %rem
+ %cmp = icmp eq i64 %x, %rem
+ br i1 %cmp, label %exit, label %body
+
+body:
+ %umax = call i64 @llvm.umax.i64(i64 %sub, i64 1)
+ ret i64 %umax
+
+exit:
+ ret i64 0
+}
+
+; Tests that must not fold.
+
+; The value compared with the remainder must be its dividend.
+
+define i1 @not_icmp_eq_urem_
diff erent_dividend(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i1 @not_icmp_eq_urem_
diff erent_dividend(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT: [[REM:%.*]] = urem i32 [[X]], [[Y]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[REM]], [[Z]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %rem = urem i32 %x, %y
+ %cmp = icmp eq i32 %rem, %z
+ ret i1 %cmp
+}
+
+; The analogous signed-remainder fold requires comparing magnitudes.
+
+define i1 @not_icmp_eq_srem_dividend(i32 %x, i32 %y) {
+; CHECK-LABEL: define i1 @not_icmp_eq_srem_dividend(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
+; CHECK-NEXT: [[REM:%.*]] = srem i32 [[X]], [[Y]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[REM]], [[X]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %rem = srem i32 %x, %y
+ %cmp = icmp eq i32 %rem, %x
+ ret i1 %cmp
+}
+
+; This fold is only valid for equality predicates.
+
+define i1 @not_icmp_slt_urem_dividend(i32 %x, i32 %y) {
+; CHECK-LABEL: define i1 @not_icmp_slt_urem_dividend(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
+; CHECK-NEXT: [[REM:%.*]] = urem i32 [[X]], [[Y]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[REM]], [[X]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %rem = urem i32 %x, %y
+ %cmp = icmp slt i32 %rem, %x
+ ret i1 %cmp
+}
More information about the llvm-commits
mailing list