[llvm] [InstCombine] simplify `(X * C0) / (X * C1)` into `C0 / C1`. (PR #73204)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 22 20:10:21 PST 2023
https://github.com/Z572 updated https://github.com/llvm/llvm-project/pull/73204
>From 6a4b73675ea03e85ff2ba1f46620b9f2da8a0381 Mon Sep 17 00:00:00 2001
From: Zheng Junjie <zhengjunjie at iscas.ac.cn>
Date: Thu, 23 Nov 2023 11:30:54 +0800
Subject: [PATCH] [InstCombine] Simplify `(X * C0) / (X * C1)` into `C0 / C1`.
proof: https://alive2.llvm.org/ce/z/IIEKgf
---
.../InstCombine/InstCombineMulDivRem.cpp | 12 +++
llvm/test/Transforms/InstCombine/div.ll | 93 +++++++++++++++++++
2 files changed, 105 insertions(+)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index 40156726c7038b9..0032422cd3ce7c7 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1207,6 +1207,18 @@ Instruction *InstCombinerImpl::commonIDivTransforms(BinaryOperator &I) {
}
}
+ // (X * C0) / (X * C1) --> C0 / C1
+ Constant *C0, *C1;
+ if (match(Op0, m_c_Mul(m_Value(X), m_Constant(C0))) &&
+ match(Op1, m_c_Mul(m_Specific(X), m_Constant(C1)))) {
+ auto OB0HasNSW = cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap();
+ auto OB0HasNUW = cast<OverflowingBinaryOperator>(Op0)->hasNoUnsignedWrap();
+ if ((IsSigned && OB0HasNSW) || (!IsSigned && OB0HasNUW)) {
+ replaceOperand(I, 0, C0);
+ replaceOperand(I, 1, C1);
+ return &I;
+ };
+ }
return nullptr;
}
diff --git a/llvm/test/Transforms/InstCombine/div.ll b/llvm/test/Transforms/InstCombine/div.ll
index cd17d10d0e1de07..2fb4873c72f5832 100644
--- a/llvm/test/Transforms/InstCombine/div.ll
+++ b/llvm/test/Transforms/InstCombine/div.ll
@@ -1432,6 +1432,99 @@ 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 * C0) / (X * C1) --> C0 / C1
+define i8 @sdiv_mul_nsw_mul(i8 %x) {
+; CHECK-LABEL: @sdiv_mul_nsw_mul(
+; CHECK-NEXT: ret i8 2
+;
+ %add4 = mul i8 %x, 6
+ %add5 = mul nsw i8 %x, 12
+ %div = sdiv i8 %add5, %add4
+ ret i8 %div
+}
+
+define i8 @sdiv_mul_nsw_mul_nsw(i8 %x) {
+; CHECK-LABEL: @sdiv_mul_nsw_mul_nsw(
+; CHECK-NEXT: ret i8 2
+;
+ %add4 = mul nsw i8 %x, 5
+ %add5 = mul nsw i8 %x, 10
+ %div = sdiv i8 %add5, %add4
+ ret i8 %div
+}
+
+define i8 @sdiv_mul_mul_nsw(i8 %x) {
+; CHECK-LABEL: @sdiv_mul_mul_nsw(
+; CHECK-NEXT: [[ADD4:%.*]] = mul nsw i8 [[X:%.*]], 10
+; CHECK-NEXT: [[ADD5:%.*]] = mul i8 [[X]], 20
+; CHECK-NEXT: [[DIV:%.*]] = sdiv i8 [[ADD5]], [[ADD4]]
+; CHECK-NEXT: ret i8 [[DIV]]
+;
+ %add4 = mul nsw i8 %x, 10
+ %add5 = mul i8 %x, 20
+ %div = sdiv i8 %add5, %add4
+ ret i8 %div
+}
+
+define i8 @sdiv_mul_mul(i8 %x) {
+; CHECK-LABEL: @sdiv_mul_mul(
+; CHECK-NEXT: [[ADD4:%.*]] = mul i8 [[X:%.*]], 10
+; CHECK-NEXT: [[ADD5:%.*]] = mul i8 [[X]], 20
+; CHECK-NEXT: [[DIV:%.*]] = sdiv i8 [[ADD5]], [[ADD4]]
+; CHECK-NEXT: ret i8 [[DIV]]
+;
+ %add4 = mul i8 %x, 10
+ %add5 = mul i8 %x, 20
+ %div = sdiv i8 %add5, %add4
+ ret i8 %div
+}
+
+define i8 @udiv_mul_mul(i8 %x) {
+; CHECK-LABEL: @udiv_mul_mul(
+; CHECK-NEXT: [[ADD4:%.*]] = mul i8 [[X:%.*]], 10
+; CHECK-NEXT: [[ADD5:%.*]] = mul i8 [[X]], 20
+; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[ADD5]], [[ADD4]]
+; CHECK-NEXT: ret i8 [[DIV]]
+;
+ %add4 = mul i8 %x, 10
+ %add5 = mul i8 %x, 20
+ %div = udiv i8 %add5, %add4
+ ret i8 %div
+}
+
+define i8 @udiv_mul_nuw_mul_nuw(i8 %x) {
+; CHECK-LABEL: @udiv_mul_nuw_mul_nuw(
+; CHECK-NEXT: ret i8 1
+;
+ %add4 = mul nuw i8 %x, 10
+ %add5 = mul nuw i8 %x, 10
+ %div = udiv i8 %add5, %add4
+ ret i8 %div
+}
+
+define i8 @udiv_mul_mul_nuw(i8 %x) {
+; CHECK-LABEL: @udiv_mul_mul_nuw(
+; CHECK-NEXT: [[ADD4:%.*]] = mul nuw i8 [[X:%.*]], 10
+; CHECK-NEXT: [[ADD5:%.*]] = mul i8 [[X]], 20
+; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[ADD5]], [[ADD4]]
+; CHECK-NEXT: ret i8 [[DIV]]
+;
+ %add4 = mul nuw i8 %x, 10
+ %add5 = mul i8 %x, 20
+ %div = udiv i8 %add5, %add4
+ ret i8 %div
+}
+
+define i8 @udiv_mul_nuw_mul(i8 %x) {
+; CHECK-LABEL: @udiv_mul_nuw_mul(
+; CHECK-NEXT: ret i8 2
+;
+ %add4 = mul i8 %x, 10
+ %add5 = mul nuw i8 %x, 20
+ %div = udiv i8 %add5, %add4
+ ret i8 %div
+}
+
define i32 @sdiv_sub1(i32 %arg) {
; CHECK-LABEL: @sdiv_sub1(
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[ARG:%.*]], -2147483648
More information about the llvm-commits
mailing list