[llvm] [InstCombine] Simplifiy `sdiv -X, X` into `X == INT_MIN ? 1 : -1` (PR #71768)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 15 01:31:21 PST 2023
https://github.com/Z572 updated https://github.com/llvm/llvm-project/pull/71768
>From 0dffd8ded9fa72f4150f7cbc619e6384d8e250a8 Mon Sep 17 00:00:00 2001
From: Zheng Junjie <zhengjunjie at iscas.ac.cn>
Date: Wed, 8 Nov 2023 11:25:15 +0800
Subject: [PATCH 1/5] [InstCombine] Simplification for (-a * b) / (a * b)
---
.../InstCombine/InstCombineMulDivRem.cpp | 4 ++
llvm/test/Transforms/InstCombine/div.ll | 41 +++++++++++++++++++
2 files changed, 45 insertions(+)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index db0804380855e3a..2f4ec126458d05d 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1544,6 +1544,10 @@ Instruction *InstCombinerImpl::visitSDiv(BinaryOperator &I) {
}
}
+ // -(X * Y) / (X * Y) --> -1
+ if (isKnownNegation(Op0, Op1, true)) {
+ return replaceInstUsesWith(I, ConstantInt::getAllOnesValue(Ty));
+ };
return nullptr;
}
diff --git a/llvm/test/Transforms/InstCombine/div.ll b/llvm/test/Transforms/InstCombine/div.ll
index 1f0081befb07dba..70d570a70562405 100644
--- a/llvm/test/Transforms/InstCombine/div.ll
+++ b/llvm/test/Transforms/InstCombine/div.ll
@@ -1432,6 +1432,47 @@ define <2 x i8> @sdiv_sdiv_mul_nsw(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) {
ret <2 x i8> %r
}
+; -(X * Y) / (X * Y) --> -1
+define i32 @sdiv_neg_mul_mul(i32 %0, i32 %1) {
+; CHECK-LABEL: @sdiv_neg_mul_mul(
+; CHECK-NEXT: ret i32 -1
+ %3 = mul nsw i32 %1, %0
+ %4 = sub nsw i32 0, %3
+ %5 = sdiv i32 %4, %3
+ ret i32 %5
+}
+
+define i32 @sdiv_neg_mul_mul2(i32 %x, i32 %y) {
+; CHECK-LABEL: @sdiv_neg_mul_mul2(
+; CHECK-NEXT: [[M:%.*]] = mul i32 [[Y:%.*]], [[X:%.*]]
+; CHECK-NEXT: [[D:%.*]] = sub i32 0, [[M:%.*]]
+; CHECK-NEXT: [[R:%.*]] = sdiv i32 [[D:%.*]], [[M:%.*]]
+; CHECK-NEXT: ret i32 [[R:%.*]]
+ %m = mul i32 %y, %x
+ %d = sub i32 0, %m
+ %r = sdiv i32 %d, %m
+ ret i32 %r
+}
+
+; (X * Y) / -(X * Y) --> -1
+define i32 @sdiv_mul_neg_mul(i32 %x, i32 %y) {
+; CHECK-LABEL: @sdiv_mul_neg_mul(
+; CHECK-NEXT: ret i32 -1
+ %m = mul i32 %y, %x
+ %n = sub nsw i32 0, %m
+ %d = sdiv i32 %m, %n
+ ret i32 %d
+}
+
+define i32 @sdiv_mul_neg_mul2(i32 %x, i32 %y) {
+; CHECK-LABEL: @sdiv_mul_neg_mul2(
+; CHECK-NEXT: ret i32 -1
+ %m = mul nsw i32 %y, %x
+ %n = sub nsw i32 0, %m
+ %d = sdiv i32 %m, %n
+ ret i32 %d
+}
+
; exact propagates
define i8 @sdiv_sdiv_mul_nsw_exact_exact(i8 %x, i8 %y, i8 %z) {
>From 89cbf9158c497bd2a2084aa576dd26110c217811 Mon Sep 17 00:00:00 2001
From: Zheng Junjie <zhengjunjie at iscas.ac.cn>
Date: Wed, 15 Nov 2023 16:25:27 +0800
Subject: [PATCH 2/5] fixup! [InstCombine] Simplification for (-a * b) / (a *
b)
---
.../InstCombine/InstCombineMulDivRem.cpp | 12 ++++--
llvm/test/Transforms/InstCombine/div.ll | 39 ++++++++++---------
2 files changed, 29 insertions(+), 22 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index 2f4ec126458d05d..486302261e6b3a6 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1544,10 +1544,14 @@ Instruction *InstCombinerImpl::visitSDiv(BinaryOperator &I) {
}
}
- // -(X * Y) / (X * Y) --> -1
- if (isKnownNegation(Op0, Op1, true)) {
- return replaceInstUsesWith(I, ConstantInt::getAllOnesValue(Ty));
- };
+ // -X / X --> X == INT_MIN ? 1 : -1
+ if ((match(Op0, m_Sub(m_Zero(), m_Value(X))) && match(Op1, m_Specific(X)))) {
+ APInt MinVal = APInt::getSignedMinValue(Ty->getScalarSizeInBits());
+ Value *Cond =
+ Builder.CreateICmpEQ(X, ConstantExpr::getIntegerValue(Ty, MinVal));
+ return SelectInst::Create(Cond, ConstantInt::get(Ty, 1),
+ ConstantInt::getAllOnesValue(Ty));
+ }
return nullptr;
}
diff --git a/llvm/test/Transforms/InstCombine/div.ll b/llvm/test/Transforms/InstCombine/div.ll
index 70d570a70562405..bc6e1700d2205a5 100644
--- a/llvm/test/Transforms/InstCombine/div.ll
+++ b/llvm/test/Transforms/InstCombine/div.ll
@@ -1432,41 +1432,44 @@ define <2 x i8> @sdiv_sdiv_mul_nsw(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) {
ret <2 x i8> %r
}
-; -(X * Y) / (X * Y) --> -1
-define i32 @sdiv_neg_mul_mul(i32 %0, i32 %1) {
-; CHECK-LABEL: @sdiv_neg_mul_mul(
-; CHECK-NEXT: ret i32 -1
- %3 = mul nsw i32 %1, %0
- %4 = sub nsw i32 0, %3
- %5 = sdiv i32 %4, %3
- ret i32 %5
+define i32 @sdiv_sub(i32 %arg) {
+; CHECK-LABEL: @sdiv_sub(
+; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[ARG:%.*]], -2147483648
+; CHECK-NEXT: [[DIV:%.*]] = select i1 [[TMP1]], i32 1, i32 -1
+; CHECK-NEXT: ret i32 [[DIV]]
+;
+ %neg = sub i32 0, %arg
+ %div = sdiv i32 %neg, %arg
+ ret i32 %div
}
-define i32 @sdiv_neg_mul_mul2(i32 %x, i32 %y) {
-; CHECK-LABEL: @sdiv_neg_mul_mul2(
+define i32 @sdiv_mul_sub(i32 %x, i32 %y) {
+; CHECK-LABEL: @sdiv_mul_sub(
; CHECK-NEXT: [[M:%.*]] = mul i32 [[Y:%.*]], [[X:%.*]]
-; CHECK-NEXT: [[D:%.*]] = sub i32 0, [[M:%.*]]
-; CHECK-NEXT: [[R:%.*]] = sdiv i32 [[D:%.*]], [[M:%.*]]
-; CHECK-NEXT: ret i32 [[R:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[M]], -2147483648
+; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i32 1, i32 -1
+; CHECK-NEXT: ret i32 [[R]]
+;
%m = mul i32 %y, %x
%d = sub i32 0, %m
%r = sdiv i32 %d, %m
ret i32 %r
}
-; (X * Y) / -(X * Y) --> -1
-define i32 @sdiv_mul_neg_mul(i32 %x, i32 %y) {
-; CHECK-LABEL: @sdiv_mul_neg_mul(
+define i32 @sdiv_mul_sub_nsw(i32 %x, i32 %y) {
+; CHECK-LABEL: @sdiv_mul_sub_nsw(
; CHECK-NEXT: ret i32 -1
+;
%m = mul i32 %y, %x
%n = sub nsw i32 0, %m
%d = sdiv i32 %m, %n
ret i32 %d
}
-define i32 @sdiv_mul_neg_mul2(i32 %x, i32 %y) {
-; CHECK-LABEL: @sdiv_mul_neg_mul2(
+define i32 @sdiv_mul_nsw_sub_nsw(i32 %x, i32 %y) {
+; CHECK-LABEL: @sdiv_mul_nsw_sub_nsw(
; CHECK-NEXT: ret i32 -1
+;
%m = mul nsw i32 %y, %x
%n = sub nsw i32 0, %m
%d = sdiv i32 %m, %n
>From 08bb142b9e75e20587553ce27f06991947ff7dbb Mon Sep 17 00:00:00 2001
From: Zheng Junjie <zhengjunjie at iscas.ac.cn>
Date: Wed, 15 Nov 2023 16:53:21 +0800
Subject: [PATCH 3/5] fixup! fixup! [InstCombine] Simplification for (-a * b) /
(a * b)
---
.../InstCombine/InstCombineMulDivRem.cpp | 5 ++---
llvm/test/Transforms/InstCombine/div.ll | 15 +++++++++++++--
2 files changed, 15 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index 486302261e6b3a6..d699fdd9918acfb 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1545,10 +1545,9 @@ Instruction *InstCombinerImpl::visitSDiv(BinaryOperator &I) {
}
// -X / X --> X == INT_MIN ? 1 : -1
- if ((match(Op0, m_Sub(m_Zero(), m_Value(X))) && match(Op1, m_Specific(X)))) {
+ if (isKnownNegation(Op0, Op1)) {
APInt MinVal = APInt::getSignedMinValue(Ty->getScalarSizeInBits());
- Value *Cond =
- Builder.CreateICmpEQ(X, ConstantExpr::getIntegerValue(Ty, MinVal));
+ Value *Cond = Builder.CreateICmpEQ(X, ConstantInt::get(Ty, MinVal));
return SelectInst::Create(Cond, ConstantInt::get(Ty, 1),
ConstantInt::getAllOnesValue(Ty));
}
diff --git a/llvm/test/Transforms/InstCombine/div.ll b/llvm/test/Transforms/InstCombine/div.ll
index bc6e1700d2205a5..67d4d72b270e002 100644
--- a/llvm/test/Transforms/InstCombine/div.ll
+++ b/llvm/test/Transforms/InstCombine/div.ll
@@ -1432,8 +1432,8 @@ define <2 x i8> @sdiv_sdiv_mul_nsw(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) {
ret <2 x i8> %r
}
-define i32 @sdiv_sub(i32 %arg) {
-; CHECK-LABEL: @sdiv_sub(
+define i32 @sdiv_sub1(i32 %arg) {
+; CHECK-LABEL: @sdiv_sub1(
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[ARG:%.*]], -2147483648
; CHECK-NEXT: [[DIV:%.*]] = select i1 [[TMP1]], i32 1, i32 -1
; CHECK-NEXT: ret i32 [[DIV]]
@@ -1443,6 +1443,17 @@ define i32 @sdiv_sub(i32 %arg) {
ret i32 %div
}
+define i32 @sdiv_sub2(i32 %arg) {
+; CHECK-LABEL: @sdiv_sub2(
+; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[ARG:%.*]]
+; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[ARG]], [[NEG]]
+; CHECK-NEXT: ret i32 [[DIV]]
+;
+ %neg = sub i32 0, %arg
+ %div = sdiv i32 %arg, %neg
+ ret i32 %div
+}
+
define i32 @sdiv_mul_sub(i32 %x, i32 %y) {
; CHECK-LABEL: @sdiv_mul_sub(
; CHECK-NEXT: [[M:%.*]] = mul i32 [[Y:%.*]], [[X:%.*]]
>From 3a83d1e9d520f2afe79fd8aa94608109f64dd087 Mon Sep 17 00:00:00 2001
From: Zheng Junjie <zhengjunjie at iscas.ac.cn>
Date: Wed, 15 Nov 2023 17:12:15 +0800
Subject: [PATCH 4/5] fixup! fixup! fixup! [InstCombine] Simplification for (-a
* b) / (a * b)
---
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index d699fdd9918acfb..aeec14851868b16 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1545,7 +1545,7 @@ Instruction *InstCombinerImpl::visitSDiv(BinaryOperator &I) {
}
// -X / X --> X == INT_MIN ? 1 : -1
- if (isKnownNegation(Op0, Op1)) {
+ if ((match(Op0, m_Sub(m_Zero(), m_Value(X))) && match(Op1, m_Specific(X)))) {
APInt MinVal = APInt::getSignedMinValue(Ty->getScalarSizeInBits());
Value *Cond = Builder.CreateICmpEQ(X, ConstantInt::get(Ty, MinVal));
return SelectInst::Create(Cond, ConstantInt::get(Ty, 1),
>From 849aef02b3e473fe46433d123c79cd23391e6f0a Mon Sep 17 00:00:00 2001
From: Zheng Junjie <zhengjunjie at iscas.ac.cn>
Date: Wed, 15 Nov 2023 17:30:26 +0800
Subject: [PATCH 5/5] fixup! fixup! fixup! fixup! [InstCombine] Simplification
for (-a * b) / (a * b)
---
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp | 4 ++--
llvm/test/Transforms/InstCombine/div.ll | 4 ++--
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index aeec14851868b16..80c67f7b5571ddb 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1545,9 +1545,9 @@ Instruction *InstCombinerImpl::visitSDiv(BinaryOperator &I) {
}
// -X / X --> X == INT_MIN ? 1 : -1
- if ((match(Op0, m_Sub(m_Zero(), m_Value(X))) && match(Op1, m_Specific(X)))) {
+ if (isKnownNegation(Op0,Op1)) {
APInt MinVal = APInt::getSignedMinValue(Ty->getScalarSizeInBits());
- Value *Cond = Builder.CreateICmpEQ(X, ConstantInt::get(Ty, MinVal));
+ Value *Cond = Builder.CreateICmpEQ(Op0, ConstantInt::get(Ty, MinVal));
return SelectInst::Create(Cond, ConstantInt::get(Ty, 1),
ConstantInt::getAllOnesValue(Ty));
}
diff --git a/llvm/test/Transforms/InstCombine/div.ll b/llvm/test/Transforms/InstCombine/div.ll
index 67d4d72b270e002..69c02bcebeada77 100644
--- a/llvm/test/Transforms/InstCombine/div.ll
+++ b/llvm/test/Transforms/InstCombine/div.ll
@@ -1445,8 +1445,8 @@ define i32 @sdiv_sub1(i32 %arg) {
define i32 @sdiv_sub2(i32 %arg) {
; CHECK-LABEL: @sdiv_sub2(
-; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[ARG:%.*]]
-; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[ARG]], [[NEG]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[ARG:%.*]], -2147483648
+; CHECK-NEXT: [[DIV:%.*]] = select i1 [[TMP1]], i32 1, i32 -1
; CHECK-NEXT: ret i32 [[DIV]]
;
%neg = sub i32 0, %arg
More information about the llvm-commits
mailing list