[llvm] r350185 - [InstCombine] canonicalize MUL with NEG operand
Chen Zheng via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 31 17:09:20 PST 2018
Author: shchenz
Date: Mon Dec 31 17:09:20 2018
New Revision: 350185
URL: http://llvm.org/viewvc/llvm-project?rev=350185&view=rev
Log:
[InstCombine] canonicalize MUL with NEG operand
-X * Y --> -(X * Y)
X * -Y --> -(X * Y)
Differential Revision: https://reviews.llvm.org/D55961
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
llvm/trunk/test/Transforms/InstCombine/mul.ll
llvm/trunk/test/Transforms/InstCombine/operand-complexity.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp?rev=350185&r1=350184&r2=350185&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp Mon Dec 31 17:09:20 2018
@@ -244,6 +244,11 @@ Instruction *InstCombiner::visitMul(Bina
return NewMul;
}
+ // -X * Y --> -(X * Y)
+ // X * -Y --> -(X * Y)
+ if (match(&I, m_c_Mul(m_OneUse(m_Neg(m_Value(X))), m_Value(Y))))
+ return BinaryOperator::CreateNeg(Builder.CreateMul(X, Y));
+
// (X / Y) * Y = X - (X % Y)
// (X / Y) * -Y = (X % Y) - X
{
Modified: llvm/trunk/test/Transforms/InstCombine/mul.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/mul.ll?rev=350185&r1=350184&r2=350185&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/mul.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/mul.ll Mon Dec 31 17:09:20 2018
@@ -445,9 +445,9 @@ define i128 @test34(i128 %X) {
define i32 @test_mul_canonicalize_op0(i32 %x, i32 %y) {
; CHECK-LABEL: @test_mul_canonicalize_op0(
-; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[X:%.*]]
-; CHECK-NEXT: [[MUL:%.*]] = mul i32 [[NEG]], [[Y:%.*]]
-; CHECK-NEXT: ret i32 [[MUL]]
+; CHECK-NEXT: [[MUL:%.*]] = mul i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[MUL]]
+; CHECK-NEXT: ret i32 [[NEG]]
;
%neg = sub i32 0, %x
%mul = mul i32 %neg, %y
@@ -457,9 +457,9 @@ define i32 @test_mul_canonicalize_op0(i3
define i32 @test_mul_canonicalize_op1(i32 %x, i32 %z) {
; CHECK-LABEL: @test_mul_canonicalize_op1(
; CHECK-NEXT: [[Y:%.*]] = mul i32 [[Z:%.*]], 3
-; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[X:%.*]]
-; CHECK-NEXT: [[MUL:%.*]] = mul i32 [[Y]], [[NEG]]
-; CHECK-NEXT: ret i32 [[MUL]]
+; CHECK-NEXT: [[MUL:%.*]] = mul i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[MUL]]
+; CHECK-NEXT: ret i32 [[NEG]]
;
%y = mul i32 %z, 3
%neg = sub i32 0, %x
@@ -469,9 +469,9 @@ define i32 @test_mul_canonicalize_op1(i3
define i32 @test_mul_canonicalize_nsw(i32 %x, i32 %y) {
; CHECK-LABEL: @test_mul_canonicalize_nsw(
-; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[X:%.*]]
-; CHECK-NEXT: [[MUL:%.*]] = mul nsw i32 [[NEG]], [[Y:%.*]]
-; CHECK-NEXT: ret i32 [[MUL]]
+; CHECK-NEXT: [[MUL:%.*]] = mul i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[MUL]]
+; CHECK-NEXT: ret i32 [[NEG]]
;
%neg = sub nsw i32 0, %x
%mul = mul nsw i32 %neg, %y
@@ -480,9 +480,9 @@ define i32 @test_mul_canonicalize_nsw(i3
define <2 x i32> @test_mul_canonicalize_vec(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @test_mul_canonicalize_vec(
-; CHECK-NEXT: [[NEG:%.*]] = sub <2 x i32> zeroinitializer, [[X:%.*]]
-; CHECK-NEXT: [[MUL:%.*]] = mul <2 x i32> [[NEG]], [[Y:%.*]]
-; CHECK-NEXT: ret <2 x i32> [[MUL]]
+; CHECK-NEXT: [[MUL:%.*]] = mul <2 x i32> [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[NEG:%.*]] = sub <2 x i32> zeroinitializer, [[MUL]]
+; CHECK-NEXT: ret <2 x i32> [[NEG]]
;
%neg = sub <2 x i32> <i32 0, i32 0>, %x
%mul = mul <2 x i32> %neg, %y
Modified: llvm/trunk/test/Transforms/InstCombine/operand-complexity.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/operand-complexity.ll?rev=350185&r1=350184&r2=350185&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/operand-complexity.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/operand-complexity.ll Mon Dec 31 17:09:20 2018
@@ -7,12 +7,12 @@ define i8 @neg(i8 %x) {
; CHECK-LABEL: @neg(
; CHECK-NEXT: [[BO:%.*]] = udiv i8 [[X:%.*]], 42
; CHECK-NEXT: [[NEGX:%.*]] = sub i8 0, [[X]]
-; CHECK-NEXT: [[R:%.*]] = mul i8 [[BO]], [[NEGX]]
+; CHECK-NEXT: [[R:%.*]] = xor i8 [[BO]], [[NEGX]]
; CHECK-NEXT: ret i8 [[R]]
;
%bo = udiv i8 %x, 42
%negx = sub i8 0, %x
- %r = mul i8 %negx, %bo
+ %r = xor i8 %negx, %bo
ret i8 %r
}
@@ -20,12 +20,12 @@ define <2 x i8> @neg_vec(<2 x i8> %x) {
; CHECK-LABEL: @neg_vec(
; CHECK-NEXT: [[BO:%.*]] = udiv <2 x i8> [[X:%.*]], <i8 42, i8 -42>
; CHECK-NEXT: [[NEGX:%.*]] = sub <2 x i8> zeroinitializer, [[X]]
-; CHECK-NEXT: [[R:%.*]] = mul <2 x i8> [[BO]], [[NEGX]]
+; CHECK-NEXT: [[R:%.*]] = xor <2 x i8> [[BO]], [[NEGX]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%bo = udiv <2 x i8> %x, <i8 42, i8 -42>
%negx = sub <2 x i8> <i8 0, i8 0>, %x
- %r = mul <2 x i8> %negx, %bo
+ %r = xor <2 x i8> %negx, %bo
ret <2 x i8> %r
}
@@ -33,12 +33,12 @@ define <2 x i8> @neg_vec_undef(<2 x i8>
; CHECK-LABEL: @neg_vec_undef(
; CHECK-NEXT: [[BO:%.*]] = udiv <2 x i8> [[X:%.*]], <i8 42, i8 -42>
; CHECK-NEXT: [[NEGX:%.*]] = sub <2 x i8> <i8 0, i8 undef>, [[X]]
-; CHECK-NEXT: [[R:%.*]] = mul <2 x i8> [[BO]], [[NEGX]]
+; CHECK-NEXT: [[R:%.*]] = xor <2 x i8> [[BO]], [[NEGX]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%bo = udiv <2 x i8> %x, <i8 42, i8 -42>
%negx = sub <2 x i8> <i8 0, i8 undef>, %x
- %r = mul <2 x i8> %negx, %bo
+ %r = xor <2 x i8> %negx, %bo
ret <2 x i8> %r
}
More information about the llvm-commits
mailing list