]> granicus.if.org Git - llvm/commitdiff
[InstCombine] Canonicalize (-X s/ Y) to -(X s/ Y).
authorChen Zheng <czhengsz@cn.ibm.com>
Wed, 10 Apr 2019 06:52:09 +0000 (06:52 +0000)
committerChen Zheng <czhengsz@cn.ibm.com>
Wed, 10 Apr 2019 06:52:09 +0000 (06:52 +0000)
Differential Revision: https://reviews.llvm.org/D60395

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

lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
test/Transforms/InstCombine/div.ll
test/Transforms/InstCombine/sdiv-canonicalize.ll

index cb8708446b282f7e38187145b9489e1156013cf9..e53eac2dd4ed1c9028172f0c52e3c2ed8b6b1181 100644 (file)
@@ -1056,6 +1056,12 @@ Instruction *InstCombiner::visitSDiv(BinaryOperator &I) {
     }
   }
 
+  // -X / Y --> -(X / Y)
+  Value *Y;
+  if (match(&I, m_SDiv(m_OneUse(m_NSWSub(m_Zero(), m_Value(X))), m_Value(Y))))
+    return BinaryOperator::CreateNSWNeg(
+        Builder.CreateSDiv(X, Y, I.getName(), I.isExact()));
+
   // If the sign bits of both operands are zero (i.e. we can prove they are
   // unsigned inputs), turn this into a udiv.
   APInt Mask(APInt::getSignMask(I.getType()->getScalarSizeInBits()));
index 6b373328016a2f8ca8dc5e685e8b570ef6accb66..4c4308151e7f4d6751d200f4bffd9d4e44c5a8d7 100644 (file)
@@ -522,8 +522,8 @@ define <2 x i8> @sdiv_negated_dividend_constant_divisor_vec_undef(<2 x i8> %x) {
 
 define <2 x i64> @sdiv_negated_dividend_constant_divisor_vec(<2 x i64> %x) {
 ; CHECK-LABEL: @sdiv_negated_dividend_constant_divisor_vec(
-; CHECK-NEXT:    [[NEG:%.*]] = sub nsw <2 x i64> zeroinitializer, [[X:%.*]]
-; CHECK-NEXT:    [[DIV:%.*]] = sdiv <2 x i64> [[NEG]], <i64 3, i64 4>
+; CHECK-NEXT:    [[DIV1:%.*]] = sdiv <2 x i64> [[X:%.*]], <i64 3, i64 4>
+; CHECK-NEXT:    [[DIV:%.*]] = sub nsw <2 x i64> zeroinitializer, [[DIV1]]
 ; CHECK-NEXT:    ret <2 x i64> [[DIV]]
 ;
   %neg = sub nsw <2 x i64> zeroinitializer, %x
@@ -533,8 +533,8 @@ define <2 x i64> @sdiv_negated_dividend_constant_divisor_vec(<2 x i64> %x) {
 
 define <2 x i64> @sdiv_exact_negated_dividend_constant_divisor_vec(<2 x i64> %x) {
 ; CHECK-LABEL: @sdiv_exact_negated_dividend_constant_divisor_vec(
-; CHECK-NEXT:    [[NEG:%.*]] = sub nsw <2 x i64> zeroinitializer, [[X:%.*]]
-; CHECK-NEXT:    [[DIV:%.*]] = sdiv exact <2 x i64> [[NEG]], <i64 3, i64 4>
+; CHECK-NEXT:    [[DIV1:%.*]] = sdiv exact <2 x i64> [[X:%.*]], <i64 3, i64 4>
+; CHECK-NEXT:    [[DIV:%.*]] = sub nsw <2 x i64> zeroinitializer, [[DIV1]]
 ; CHECK-NEXT:    ret <2 x i64> [[DIV]]
 ;
   %neg = sub nsw <2 x i64> zeroinitializer, %x
@@ -546,8 +546,8 @@ define <2 x i64> @sdiv_exact_negated_dividend_constant_divisor_vec(<2 x i64> %x)
 
 define <2 x i8> @sdiv_exact_negated_dividend_constant_divisor_vec_overflow(<2 x i8> %x) {
 ; CHECK-LABEL: @sdiv_exact_negated_dividend_constant_divisor_vec_overflow(
-; CHECK-NEXT:    [[NEG:%.*]] = sub nsw <2 x i8> zeroinitializer, [[X:%.*]]
-; CHECK-NEXT:    [[DIV:%.*]] = sdiv exact <2 x i8> [[NEG]], <i8 -128, i8 42>
+; CHECK-NEXT:    [[DIV1:%.*]] = sdiv exact <2 x i8> [[X:%.*]], <i8 -128, i8 42>
+; CHECK-NEXT:    [[DIV:%.*]] = sub nsw <2 x i8> zeroinitializer, [[DIV1]]
 ; CHECK-NEXT:    ret <2 x i8> [[DIV]]
 ;
   %neg = sub nsw <2 x i8> zeroinitializer, %x
index 8e1497b900a74401b51139634413dc886ac7f691..39ba5120ed62f34243932b14284244cb878f2880 100644 (file)
@@ -3,8 +3,8 @@
 
 define i32 @test_sdiv_canonicalize_op0(i32 %x, i32 %y) {
 ; CHECK-LABEL: @test_sdiv_canonicalize_op0(
-; CHECK-NEXT:    [[NEG:%.*]] = sub nsw i32 0, [[X:%.*]]
-; CHECK-NEXT:    [[SDIV:%.*]] = sdiv i32 [[NEG]], [[Y:%.*]]
+; CHECK-NEXT:    [[SDIV1:%.*]] = sdiv i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[SDIV:%.*]] = sub nsw i32 0, [[SDIV1]]
 ; CHECK-NEXT:    ret i32 [[SDIV]]
 ;
   %neg = sub nsw i32 0, %x
@@ -14,8 +14,8 @@ define i32 @test_sdiv_canonicalize_op0(i32 %x, i32 %y) {
 
 define i32 @test_sdiv_canonicalize_op0_exact(i32 %x, i32 %y) {
 ; CHECK-LABEL: @test_sdiv_canonicalize_op0_exact(
-; CHECK-NEXT:    [[NEG:%.*]] = sub nsw i32 0, [[X:%.*]]
-; CHECK-NEXT:    [[SDIV:%.*]] = sdiv exact i32 [[NEG]], [[Y:%.*]]
+; CHECK-NEXT:    [[SDIV1:%.*]] = sdiv exact i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[SDIV:%.*]] = sub nsw i32 0, [[SDIV1]]
 ; CHECK-NEXT:    ret i32 [[SDIV]]
 ;
   %neg = sub nsw i32 0, %x
@@ -23,6 +23,7 @@ define i32 @test_sdiv_canonicalize_op0_exact(i32 %x, i32 %y) {
   ret i32 %sdiv
 }
 
+; (X/-Y) is not equal to -(X/Y), don't canonicalize.
 define i32 @test_sdiv_canonicalize_op1(i32 %x, i32 %z) {
 ; CHECK-LABEL: @test_sdiv_canonicalize_op1(
 ; CHECK-NEXT:    [[Y:%.*]] = mul i32 [[Z:%.*]], 3
@@ -49,8 +50,8 @@ define i32 @test_sdiv_canonicalize_nonsw(i32 %x, i32 %y) {
 
 define <2 x i32> @test_sdiv_canonicalize_vec(<2 x i32> %x, <2 x i32> %y) {
 ; CHECK-LABEL: @test_sdiv_canonicalize_vec(
-; CHECK-NEXT:    [[NEG:%.*]] = sub nsw <2 x i32> zeroinitializer, [[X:%.*]]
-; CHECK-NEXT:    [[SDIV:%.*]] = sdiv <2 x i32> [[NEG]], [[Y:%.*]]
+; CHECK-NEXT:    [[SDIV1:%.*]] = sdiv <2 x i32> [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[SDIV:%.*]] = sub nsw <2 x i32> zeroinitializer, [[SDIV1]]
 ; CHECK-NEXT:    ret <2 x i32> [[SDIV]]
 ;
   %neg = sub nsw <2 x i32> <i32 0, i32 0>, %x
@@ -71,9 +72,8 @@ define i32 @test_sdiv_canonicalize_multiple_uses(i32 %x, i32 %y) {
   ret i32 %sdiv2
 }
 
-; There is combination: (X/-CE) -> -(X/CE)
-; There is another combination: -(X/CE) -> (X/-CE)
-; Make sure don't combine them endless.
+; There is combination: -(X/CE) -> (X/-CE).
+; If combines (X/-CE) to -(X/CE), make sure don't combine them endless.
 
 @X = global i32 5