]> granicus.if.org Git - llvm/commitdiff
[InstCombine] Canonicalize (-X srem Y) to -(X srem Y).
authorChen Zheng <czhengsz@cn.ibm.com>
Sat, 13 Apr 2019 09:21:22 +0000 (09:21 +0000)
committerChen Zheng <czhengsz@cn.ibm.com>
Sat, 13 Apr 2019 09:21:22 +0000 (09:21 +0000)
Differential Revision: https://reviews.llvm.org/D60647

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

lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
test/Transforms/InstCombine/srem-canonicalize.ll

index e53eac2dd4ed1c9028172f0c52e3c2ed8b6b1181..7edae55cb3e4b320dba808c3abe0a07ed0d98f5e 100644 (file)
@@ -1351,6 +1351,11 @@ Instruction *InstCombiner::visitSRem(BinaryOperator &I) {
     }
   }
 
+  // -X srem Y --> -(X srem Y)
+  Value *X, *Y;
+  if (match(&I, m_SRem(m_OneUse(m_NSWSub(m_Zero(), m_Value(X))), m_Value(Y))))
+    return BinaryOperator::CreateNSWNeg(Builder.CreateSRem(X, Y)); 
+
   // If the sign bits of both operands are zero (i.e. we can prove they are
   // unsigned inputs), turn this into a urem.
   APInt Mask(APInt::getSignMask(I.getType()->getScalarSizeInBits()));
index af27fd3affae008b9a671358556e1c95d4b45fcd..dc6b15c287c7d64b52f00a6279e9cc79cc6a4e75 100644 (file)
@@ -3,8 +3,8 @@
 
 define i32 @test_srem_canonicalize_op0(i32 %x, i32 %y) {
 ; CHECK-LABEL: @test_srem_canonicalize_op0(
-; CHECK-NEXT:    [[NEG:%.*]] = sub nsw i32 0, [[X:%.*]]
-; CHECK-NEXT:    [[SREM:%.*]] = srem i32 [[NEG]], [[Y:%.*]]
+; CHECK-NEXT:    [[TMP1:%.*]] = srem i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[SREM:%.*]] = sub nsw i32 0, [[TMP1]]
 ; CHECK-NEXT:    ret i32 [[SREM]]
 ;
   %neg = sub nsw i32 0, %x
@@ -39,8 +39,8 @@ define i32 @test_srem_canonicalize_nonsw(i32 %x, i32 %y) {
 
 define <2 x i32> @test_srem_canonicalize_vec(<2 x i32> %x, <2 x i32> %y) {
 ; CHECK-LABEL: @test_srem_canonicalize_vec(
-; CHECK-NEXT:    [[NEG:%.*]] = sub nsw <2 x i32> zeroinitializer, [[X:%.*]]
-; CHECK-NEXT:    [[SREM:%.*]] = srem <2 x i32> [[NEG]], [[Y:%.*]]
+; CHECK-NEXT:    [[TMP1:%.*]] = srem <2 x i32> [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[SREM:%.*]] = sub nsw <2 x i32> zeroinitializer, [[TMP1]]
 ; CHECK-NEXT:    ret <2 x i32> [[SREM]]
 ;
   %neg = sub nsw <2 x i32> <i32 0, i32 0>, %x