[llvm] [InstCombine] Simplifiy `(-x * y * -x)` into `(x * y * x)` (PR #72953)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 20 20:46:42 PST 2023
https://github.com/Z572 created https://github.com/llvm/llvm-project/pull/72953
fix https://github.com/llvm/llvm-project/issues/72259
proof: https://alive2.llvm.org/ce/z/B9kej-
>From b07ca5fca430424d074146032b3d49f87d7bbec2 Mon Sep 17 00:00:00 2001
From: Zheng Junjie <zhengjunjie at iscas.ac.cn>
Date: Tue, 21 Nov 2023 11:41:20 +0800
Subject: [PATCH] [InstCombine] Simplifiy `(-x * y * -x)` into `(x * y * x)`
proof: https://alive2.llvm.org/ce/z/B9kej-
---
.../InstCombine/InstCombineMulDivRem.cpp | 2 +-
.../InstCombine/mul-inseltpoison.ll | 5 +-
llvm/test/Transforms/InstCombine/mul.ll | 53 +++++++++++++++++--
.../sub-of-negatible-inseltpoison.ll | 5 +-
.../InstCombine/sub-of-negatible.ll | 5 +-
5 files changed, 59 insertions(+), 11 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index 40156726c7038b9..458341b8900ca45 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -349,7 +349,7 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
// -X * Y --> -(X * Y)
// X * -Y --> -(X * Y)
- if (match(&I, m_c_Mul(m_OneUse(m_Neg(m_Value(X))), m_Value(Y))))
+ if (match(&I, m_c_Mul(m_Neg(m_Value(X)), m_Value(Y))))
return BinaryOperator::CreateNeg(Builder.CreateMul(X, Y));
// (X / Y) * Y = X - (X % Y)
diff --git a/llvm/test/Transforms/InstCombine/mul-inseltpoison.ll b/llvm/test/Transforms/InstCombine/mul-inseltpoison.ll
index 448558d755a5763..ed0b74d44ace1b1 100644
--- a/llvm/test/Transforms/InstCombine/mul-inseltpoison.ll
+++ b/llvm/test/Transforms/InstCombine/mul-inseltpoison.ll
@@ -672,9 +672,8 @@ define <2 x i32> @test_mul_canonicalize_vec(<2 x i32> %x, <2 x i32> %y) {
define i32 @test_mul_canonicalize_multiple_uses(i32 %x, i32 %y) {
; CHECK-LABEL: @test_mul_canonicalize_multiple_uses(
-; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[X:%.*]]
-; CHECK-NEXT: [[MUL:%.*]] = mul i32 [[NEG]], [[Y:%.*]]
-; CHECK-NEXT: [[MUL2:%.*]] = mul i32 [[MUL]], [[NEG]]
+; CHECK-NEXT: [[TMP1:%.*]] = mul i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[MUL2:%.*]] = mul i32 [[TMP1]], [[X]]
; CHECK-NEXT: ret i32 [[MUL2]]
;
%neg = sub i32 0, %x
diff --git a/llvm/test/Transforms/InstCombine/mul.ll b/llvm/test/Transforms/InstCombine/mul.ll
index 42698b5102bc5b7..3b610cf1cf4dd5b 100644
--- a/llvm/test/Transforms/InstCombine/mul.ll
+++ b/llvm/test/Transforms/InstCombine/mul.ll
@@ -1233,9 +1233,8 @@ define <2 x i32> @test_mul_canonicalize_vec(<2 x i32> %x, <2 x i32> %y) {
define i32 @test_mul_canonicalize_multiple_uses(i32 %x, i32 %y) {
; CHECK-LABEL: @test_mul_canonicalize_multiple_uses(
-; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[X:%.*]]
-; CHECK-NEXT: [[MUL:%.*]] = mul i32 [[NEG]], [[Y:%.*]]
-; CHECK-NEXT: [[MUL2:%.*]] = mul i32 [[MUL]], [[NEG]]
+; CHECK-NEXT: [[TMP1:%.*]] = mul i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[MUL2:%.*]] = mul i32 [[TMP1]], [[X]]
; CHECK-NEXT: ret i32 [[MUL2]]
;
%neg = sub i32 0, %x
@@ -1244,6 +1243,54 @@ define i32 @test_mul_canonicalize_multiple_uses(i32 %x, i32 %y) {
ret i32 %mul2
}
+define i32 @mul_nsw_mul_nsw_neg(i32 %x, i32 %y) {
+; CHECK-LABEL: @mul_nsw_mul_nsw_neg(
+; CHECK-NEXT: [[TMP1:%.*]] = mul i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[MUL2:%.*]] = mul i32 [[TMP1]], [[X]]
+; CHECK-NEXT: ret i32 [[MUL2]]
+;
+ %neg = sub i32 0, %x
+ %mul = mul nsw i32 %neg, %y
+ %mul2 = mul nsw i32 %mul, %neg
+ ret i32 %mul2
+}
+
+define i32 @mul_mul_nsw_neg(i32 %x,i32 %y) {
+; CHECK-LABEL: @mul_mul_nsw_neg(
+; CHECK-NEXT: [[TMP1:%.*]] = mul i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[MUL2:%.*]] = mul i32 [[TMP1]], [[X]]
+; CHECK-NEXT: ret i32 [[MUL2]]
+;
+ %neg = sub i32 0, %x
+ %mul = mul nsw i32 %neg, %y
+ %mul2 = mul i32 %mul, %neg
+ ret i32 %mul2
+}
+
+define i32 @mul_nsw_mul_neg(i32 %x,i32 %y) {
+; CHECK-LABEL: @mul_nsw_mul_neg(
+; CHECK-NEXT: [[TMP1:%.*]] = mul i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[MUL2:%.*]] = mul i32 [[TMP1]], [[X]]
+; CHECK-NEXT: ret i32 [[MUL2]]
+;
+ %neg = sub i32 0, %x
+ %mul = mul i32 %neg, %y
+ %mul2 = mul nsw i32 %mul, %neg
+ ret i32 %mul2
+}
+
+define i32 @mul_mul_neg(i32 %x) {
+; CHECK-LABEL: @mul_mul_neg(
+; CHECK-NEXT: [[TMP1:%.*]] = mul i32 [[X:%.*]], [[X]]
+; CHECK-NEXT: [[MUL2:%.*]] = mul i32 [[TMP1]], [[X]]
+; CHECK-NEXT: ret i32 [[MUL2]]
+;
+ %neg = sub i32 0, %x
+ %mul = mul i32 %neg, %x
+ %mul2 = mul i32 %mul, %neg
+ ret i32 %mul2
+}
+
@X = global i32 5
define i64 @test_mul_canonicalize_neg_is_not_undone(i64 %L1) {
diff --git a/llvm/test/Transforms/InstCombine/sub-of-negatible-inseltpoison.ll b/llvm/test/Transforms/InstCombine/sub-of-negatible-inseltpoison.ll
index d76564dd7a67edd..dfed19f7ba1e4c5 100644
--- a/llvm/test/Transforms/InstCombine/sub-of-negatible-inseltpoison.ll
+++ b/llvm/test/Transforms/InstCombine/sub-of-negatible-inseltpoison.ll
@@ -399,9 +399,10 @@ define i8 @n16(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @n16(
; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[Y:%.*]]
; CHECK-NEXT: call void @use8(i8 [[T0]])
-; CHECK-NEXT: [[T1:%.*]] = mul i8 [[T0]], [[Z:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = mul i8 [[Y]], [[Z:%.*]]
+; CHECK-NEXT: [[T1:%.*]] = sub i8 0, [[TMP1]]
; CHECK-NEXT: call void @use8(i8 [[T1]])
-; CHECK-NEXT: [[T2:%.*]] = sub i8 [[X:%.*]], [[T1]]
+; CHECK-NEXT: [[T2:%.*]] = add i8 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i8 [[T2]]
;
%t0 = sub i8 0, %y
diff --git a/llvm/test/Transforms/InstCombine/sub-of-negatible.ll b/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
index aacc83eba006150..7fcc7020785e6f3 100644
--- a/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
+++ b/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
@@ -423,9 +423,10 @@ define i8 @n16(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @n16(
; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[Y:%.*]]
; CHECK-NEXT: call void @use8(i8 [[T0]])
-; CHECK-NEXT: [[T1:%.*]] = mul i8 [[T0]], [[Z:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = mul i8 [[Y]], [[Z:%.*]]
+; CHECK-NEXT: [[T1:%.*]] = sub i8 0, [[TMP1]]
; CHECK-NEXT: call void @use8(i8 [[T1]])
-; CHECK-NEXT: [[T2:%.*]] = sub i8 [[X:%.*]], [[T1]]
+; CHECK-NEXT: [[T2:%.*]] = add i8 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i8 [[T2]]
;
%t0 = sub i8 0, %y
More information about the llvm-commits
mailing list