[llvm] [InstCombine] Simplification for (-a * b) / (a * b) and (a - b) / (b - a). (PR #71768)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 13 00:03:56 PST 2023
https://github.com/Z572 updated https://github.com/llvm/llvm-project/pull/71768
>From 93bac53f8cb25745a49474cb4b43dcc870da6fe1 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/2] [InstCombine] Simplification for (-a * b) / (a * b) and
(a - b) / (b - a).
---
.../InstCombine/InstCombineMulDivRem.cpp | 14 +++++++++
llvm/test/Transforms/InstCombine/div.ll | 30 +++++++++++++++++++
2 files changed, 44 insertions(+)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index db0804380855e3a..a4460f0b3020918 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1544,6 +1544,20 @@ Instruction *InstCombinerImpl::visitSDiv(BinaryOperator &I) {
}
}
+ // -(X * Y) / (X * Y) --> -1
+ if ((match(Op0, m_Neg(m_c_Mul(m_Value(X), m_Value(Y)))) &&
+ match(Op1, m_c_Mul(m_Specific(X), m_Specific(Y)))) ||
+ (match(Op0, m_c_Mul(m_Value(X), m_Value(Y))) &&
+ match(Op1, m_Neg(m_c_Mul(m_Specific(X), m_Specific(Y)))))) {
+ return BinaryOperator::CreateNeg(ConstantInt::get(Ty, 1));
+ };
+
+ // (Y - X) / (X - Y) --> -1
+ if (match(Op0, m_Sub(m_Value(Y), m_Value(X))) &&
+ match(Op1, m_Sub(m_Specific(X), m_Specific(Y)))) {
+ return BinaryOperator::CreateNeg(ConstantInt::get(Ty, 1));
+ }
+
return nullptr;
}
diff --git a/llvm/test/Transforms/InstCombine/div.ll b/llvm/test/Transforms/InstCombine/div.ll
index 1f0081befb07dba..099c12d60f248df 100644
--- a/llvm/test/Transforms/InstCombine/div.ll
+++ b/llvm/test/Transforms/InstCombine/div.ll
@@ -1432,6 +1432,36 @@ 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 i32 %1, %0
+ %4 = sub i32 0, %3
+ %5 = sdiv i32 %4, %3
+ ret i32 %5
+}
+
+; (X * Y) / -(X * Y) --> -1
+define i32 @sdiv_mul_neg_mul(i32 %0, i32 %1) {
+; CHECK-LABEL: @sdiv_mul_neg_mul(
+; CHECK-NEXT: ret i32 -1
+ %3 = mul i32 %1, %0
+ %4 = sub i32 0, %3
+ %5 = sdiv i32 %3, %4
+ ret i32 %5
+}
+
+; (Y - X) / (X - Y) --> -1
+define i32 @sdiv_sub_sub(i32 %0, i32 %1) {
+; CHECK-LABEL: @sdiv_sub_sub(
+; CHECK-NEXT: ret i32 -1
+ %3 = sub i32 %1, %0
+ %4 = sub nsw i32 %0, %1
+ %5 = sdiv i32 %3, %4
+ ret i32 %5
+}
+
; exact propagates
define i8 @sdiv_sdiv_mul_nsw_exact_exact(i8 %x, i8 %y, i8 %z) {
>From a4710e37b5faa56d19d83dbb1249252620579e51 Mon Sep 17 00:00:00 2001
From: Zheng Junjie <zhengjunjie at iscas.ac.cn>
Date: Mon, 13 Nov 2023 15:44:55 +0800
Subject: [PATCH 2/2] fixup! [InstCombine] Simplification for (-a * b) / (a *
b) and (a - b) / (b - a).
---
llvm/lib/Analysis/InstructionSimplify.cpp | 4 ++
.../InstCombine/InstCombineMulDivRem.cpp | 17 +++----
llvm/test/Transforms/InstCombine/div.ll | 45 ++++++++++++++-----
3 files changed, 42 insertions(+), 24 deletions(-)
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 2fff10df55038d9..bbb79df988a4792 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -1230,6 +1230,10 @@ static Value *simplifySDivInst(Value *Op0, Value *Op1, bool IsExact,
// If two operands are negated and no signed overflow, return -1.
if (isKnownNegation(Op0, Op1, /*NeedNSW=*/true))
return Constant::getAllOnesValue(Op0->getType());
+ Value *X,*Y;
+ if (match(Op0, m_NSWSub(m_Value(Y), m_Value(X))) &&
+ match(Op1, m_NSWSub(m_Specific(X), m_Specific(Y))))
+ return Constant::getAllOnesValue(Op0->getType());
return simplifyDiv(Instruction::SDiv, Op0, Op1, IsExact, Q, MaxRecurse);
}
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index a4460f0b3020918..b670d3e83457b9c 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1545,19 +1545,12 @@ Instruction *InstCombinerImpl::visitSDiv(BinaryOperator &I) {
}
// -(X * Y) / (X * Y) --> -1
- if ((match(Op0, m_Neg(m_c_Mul(m_Value(X), m_Value(Y)))) &&
+ if ((match(Op0, m_NSWNeg(m_c_Mul(m_Value(X), m_Value(Y)))) &&
match(Op1, m_c_Mul(m_Specific(X), m_Specific(Y)))) ||
- (match(Op0, m_c_Mul(m_Value(X), m_Value(Y))) &&
- match(Op1, m_Neg(m_c_Mul(m_Specific(X), m_Specific(Y)))))) {
- return BinaryOperator::CreateNeg(ConstantInt::get(Ty, 1));
- };
-
- // (Y - X) / (X - Y) --> -1
- if (match(Op0, m_Sub(m_Value(Y), m_Value(X))) &&
- match(Op1, m_Sub(m_Specific(X), m_Specific(Y)))) {
- return BinaryOperator::CreateNeg(ConstantInt::get(Ty, 1));
- }
-
+ (match(Op0, m_Mul(m_Value(X), m_Value(Y))) &&
+ match(Op1, m_NSWNeg(m_c_Mul(m_Specific(X), m_Specific(Y)))))) {
+ 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 099c12d60f248df..37dcd53b9af2e23 100644
--- a/llvm/test/Transforms/InstCombine/div.ll
+++ b/llvm/test/Transforms/InstCombine/div.ll
@@ -1436,30 +1436,51 @@ define <2 x i8> @sdiv_sdiv_mul_nsw(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) {
define i32 @sdiv_neg_mul_mul(i32 %0, i32 %1) {
; CHECK-LABEL: @sdiv_neg_mul_mul(
; CHECK-NEXT: ret i32 -1
- %3 = mul i32 %1, %0
- %4 = sub i32 0, %3
+ %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 %0, i32 %1) {
+define i32 @sdiv_mul_neg_mul(i32 %x, i32 %y) {
; CHECK-LABEL: @sdiv_mul_neg_mul(
; CHECK-NEXT: ret i32 -1
- %3 = mul i32 %1, %0
- %4 = sub i32 0, %3
- %5 = sdiv i32 %3, %4
- ret i32 %5
+ %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
}
; (Y - X) / (X - Y) --> -1
-define i32 @sdiv_sub_sub(i32 %0, i32 %1) {
+define i32 @sdiv_sub_sub(i32 %x, i32 %y) {
; CHECK-LABEL: @sdiv_sub_sub(
; CHECK-NEXT: ret i32 -1
- %3 = sub i32 %1, %0
- %4 = sub nsw i32 %0, %1
- %5 = sdiv i32 %3, %4
- ret i32 %5
+ %m = sub nsw i32 %y, %x
+ %d = sub nsw i32 %x, %y
+ %r = sdiv i32 %m, %d
+ ret i32 %r
}
; exact propagates
More information about the llvm-commits
mailing list