[llvm] [InstCombine] Fold square into comparison with constant (PR #196480)
Macsen Casaus via llvm-commits
llvm-commits at lists.llvm.org
Fri May 8 09:18:35 PDT 2026
https://github.com/macsencasaus updated https://github.com/llvm/llvm-project/pull/196480
>From b228b05c2c4040989f32991c086449bf75075f48 Mon Sep 17 00:00:00 2001
From: Macsen Casaus <macsencasaus at gmail.com>
Date: Fri, 8 May 2026 07:04:22 +0000
Subject: [PATCH 1/3] pre-commit test
---
llvm/test/Transforms/InstCombine/icmp-mul.ll | 48 ++++++++++++++++++++
1 file changed, 48 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/icmp-mul.ll b/llvm/test/Transforms/InstCombine/icmp-mul.ll
index 1e4876d5cd569..d484eb4cf9923 100644
--- a/llvm/test/Transforms/InstCombine/icmp-mul.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-mul.ll
@@ -91,6 +91,54 @@ define i1 @squared_nsw_sgt0(i5 %x) {
ret i1 %r
}
+define i1 @squared_nuw_ult_sqr(i8 %x) {
+; CHECK-LABEL: @squared_nuw_ult_sqr(
+; CHECK-NEXT: [[M:%.*]] = mul nuw i8 [[X:%.*]], [[X]]
+; CHECK-NEXT: [[R:%.*]] = icmp ult i8 [[M]], 9
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %m = mul nuw i8 %x, %x
+ %r = icmp ult i8 %m, 9
+ ret i1 %r
+}
+
+define i1 @squared_nuw_eq_sqr(i8 %x) {
+; CHECK-LABEL: @squared_nuw_eq_sqr(
+; CHECK-NEXT: [[M:%.*]] = mul nuw i8 [[X:%.*]], [[X]]
+; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[M]], 9
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %m = mul nuw i8 %x, %x
+ %r = icmp eq i8 %m, 9
+ ret i1 %r
+}
+
+; negative test - signed compare
+
+define i1 @squared_nuw_slt_sqr(i8 %x) {
+; CHECK-LABEL: @squared_nuw_slt_sqr(
+; CHECK-NEXT: [[M:%.*]] = mul nuw i8 [[X:%.*]], [[X]]
+; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[M]], 9
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %m = mul nuw i8 %x, %x
+ %r = icmp slt i8 %m, 9
+ ret i1 %r
+}
+
+; negative test - no nuw
+
+define i1 @squared_ult_sqr(i8 %x) {
+; CHECK-LABEL: @squared_ult_sqr(
+; CHECK-NEXT: [[M:%.*]] = mul i8 [[X:%.*]], [[X]]
+; CHECK-NEXT: [[R:%.*]] = icmp ult i8 [[M]], 9
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %m = mul i8 %x, %x
+ %r = icmp ult i8 %m, 9
+ ret i1 %r
+}
+
; Tests for slt/ult
define i1 @slt_positive_multip_rem_zero(i8 %x) {
>From f26a6f25c41f40007734c73cc4f24df1612d1ff4 Mon Sep 17 00:00:00 2001
From: Macsen Casaus <macsencasaus at gmail.com>
Date: Fri, 8 May 2026 07:12:38 +0000
Subject: [PATCH 2/3] [InstCombine] Fold square into comparison with constant
---
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 9 +++++++++
llvm/test/Transforms/InstCombine/icmp-mul.ll | 6 ++----
2 files changed, 11 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 0b1f683e10408..4426ea346949f 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -2195,6 +2195,15 @@ Instruction *InstCombinerImpl::foldICmpMulConstant(ICmpInst &Cmp,
(Mul->hasNoUnsignedWrap() || Mul->hasNoSignedWrap()))
return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy));
+ // If unsigned compare or equality comparison of self multiply and square,
+ // compare square roots
+ if (!Cmp.isSigned() && X == Mul->getOperand(1) && Mul->hasNoUnsignedWrap() &&
+ !C.isNegative()) {
+ APInt R = C.sqrt();
+ if (C == R * R)
+ return new ICmpInst(Pred, X, ConstantInt::get(MulTy, R));
+ }
+
const APInt *MulC;
if (!match(Mul->getOperand(1), m_APInt(MulC)))
return nullptr;
diff --git a/llvm/test/Transforms/InstCombine/icmp-mul.ll b/llvm/test/Transforms/InstCombine/icmp-mul.ll
index d484eb4cf9923..40259cbe6977f 100644
--- a/llvm/test/Transforms/InstCombine/icmp-mul.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-mul.ll
@@ -93,8 +93,7 @@ define i1 @squared_nsw_sgt0(i5 %x) {
define i1 @squared_nuw_ult_sqr(i8 %x) {
; CHECK-LABEL: @squared_nuw_ult_sqr(
-; CHECK-NEXT: [[M:%.*]] = mul nuw i8 [[X:%.*]], [[X]]
-; CHECK-NEXT: [[R:%.*]] = icmp ult i8 [[M]], 9
+; CHECK-NEXT: [[R:%.*]] = icmp ult i8 [[X:%.*]], 3
; CHECK-NEXT: ret i1 [[R]]
;
%m = mul nuw i8 %x, %x
@@ -104,8 +103,7 @@ define i1 @squared_nuw_ult_sqr(i8 %x) {
define i1 @squared_nuw_eq_sqr(i8 %x) {
; CHECK-LABEL: @squared_nuw_eq_sqr(
-; CHECK-NEXT: [[M:%.*]] = mul nuw i8 [[X:%.*]], [[X]]
-; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[M]], 9
+; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[X:%.*]], 3
; CHECK-NEXT: ret i1 [[R]]
;
%m = mul nuw i8 %x, %x
>From c2c34c2df412c1cc99f34abd6ad9d74b530df822 Mon Sep 17 00:00:00 2001
From: Macsen Casaus <macsencasaus at gmail.com>
Date: Fri, 8 May 2026 16:17:55 +0000
Subject: [PATCH 3/3] Remove negativity check
---
.../lib/Transforms/InstCombine/InstCombineCompares.cpp | 3 +--
llvm/test/Transforms/InstCombine/icmp-mul.ll | 10 ++++++++++
2 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 4426ea346949f..2a7d15ed4d274 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -2197,8 +2197,7 @@ Instruction *InstCombinerImpl::foldICmpMulConstant(ICmpInst &Cmp,
// If unsigned compare or equality comparison of self multiply and square,
// compare square roots
- if (!Cmp.isSigned() && X == Mul->getOperand(1) && Mul->hasNoUnsignedWrap() &&
- !C.isNegative()) {
+ if (!Cmp.isSigned() && X == Mul->getOperand(1) && Mul->hasNoUnsignedWrap()) {
APInt R = C.sqrt();
if (C == R * R)
return new ICmpInst(Pred, X, ConstantInt::get(MulTy, R));
diff --git a/llvm/test/Transforms/InstCombine/icmp-mul.ll b/llvm/test/Transforms/InstCombine/icmp-mul.ll
index 40259cbe6977f..ba2fe44243f71 100644
--- a/llvm/test/Transforms/InstCombine/icmp-mul.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-mul.ll
@@ -111,6 +111,16 @@ define i1 @squared_nuw_eq_sqr(i8 %x) {
ret i1 %r
}
+define i1 @squared_nuw_eq_negative_sqr(i8 %x) {
+; CHECK-LABEL: @squared_nuw_eq_negative_sqr(
+; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[X:%.*]], 15
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %m = mul nuw i8 %x, %x
+ %r = icmp eq i8 %m, -31
+ ret i1 %r
+}
+
; negative test - signed compare
define i1 @squared_nuw_slt_sqr(i8 %x) {
More information about the llvm-commits
mailing list