[llvm] [InstCombine] Simplification for (-a * b) / (a * b) and (a - b) / (b - a). (PR #71768)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 8 21:12:41 PST 2023
https://github.com/Z572 created https://github.com/llvm/llvm-project/pull/71768
Fixes #69574
>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] [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) {
More information about the llvm-commits
mailing list