]> granicus.if.org Git - llvm/commitdiff
[InstCombine] canonicalize MUL with NEG operand
authorChen Zheng <czhengsz@cn.ibm.com>
Tue, 1 Jan 2019 01:09:20 +0000 (01:09 +0000)
committerChen Zheng <czhengsz@cn.ibm.com>
Tue, 1 Jan 2019 01:09:20 +0000 (01:09 +0000)
-X * Y --> -(X * Y)
X * -Y --> -(X * Y)

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@350185 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
test/Transforms/InstCombine/mul.ll
test/Transforms/InstCombine/operand-complexity.ll

index c348aecb2d44a9a6054316da902ce38b9fad2185..7e99f3e4e500fdf5513d847658e012b05e277f0b 100644 (file)
@@ -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
   {
index d97ba88dfd7b53a1e04268d5537fe588b2f3aaec..94d21714d1fccab14f6c6582527a8ac467d2d2bf 100644 (file)
@@ -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(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
@@ -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
@@ -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
index 20abe7b48f9fd6cb4b54e917201b4fdc7c48e3c7..2dc161cb0066545a6de772729347b3475dd257fd 100644 (file)
@@ -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> %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
 }