Skip to content

Commit

Permalink
[InstCombine] canonicalize MUL with NEG operand
Browse files Browse the repository at this point in the history
-X * Y --> -(X * Y)
X * -Y --> -(X * Y)

Differential Revision: https://reviews.llvm.org/D55961

llvm-svn: 350185
  • Loading branch information
chenzheng1030 committed Jan 1, 2019
1 parent 745983b commit 4952e66
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
5 changes: 5 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
Expand Up @@ -244,6 +244,11 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) {
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
{
Expand Down
24 changes: 12 additions & 12 deletions llvm/test/Transforms/InstCombine/mul.ll
Expand Up @@ -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
Expand All @@ -457,9 +457,9 @@ define i32 @test_mul_canonicalize_op0(i32 %x, i32 %y) {
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
Expand All @@ -469,9 +469,9 @@ define i32 @test_mul_canonicalize_op1(i32 %x, i32 %z) {

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
Expand All @@ -480,9 +480,9 @@ define i32 @test_mul_canonicalize_nsw(i32 %x, i32 %y) {

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
Expand Down
12 changes: 6 additions & 6 deletions llvm/test/Transforms/InstCombine/operand-complexity.ll
Expand Up @@ -7,38 +7,38 @@ 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
}

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
}

define <2 x i8> @neg_vec_undef(<2 x i8> %x) {
; 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
}

Expand Down

0 comments on commit 4952e66

Please sign in to comment.