[PATCH] D60647: [InstCombine] Canonicalize (-X srem Y) to -(X srem Y)

ChenZheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 13 00:59:57 PDT 2019


shchenz created this revision.
shchenz added reviewers: lebedev.ri, spatel.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.

Similar with https://reviews.llvm.org/D60395.

This is for operator `srem`.

Fix https://bugs.llvm.org/show_bug.cgi?id=41443


https://reviews.llvm.org/D60647

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


Index: llvm/test/Transforms/InstCombine/srem-canonicalize.ll
===================================================================
--- llvm/test/Transforms/InstCombine/srem-canonicalize.ll
+++ llvm/test/Transforms/InstCombine/srem-canonicalize.ll
@@ -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 <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
Index: llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1351,6 +1351,11 @@
     }
   }
 
+  // -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()));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60647.195003.patch
Type: text/x-patch
Size: 1878 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190413/bce705ec/attachment.bin>


More information about the llvm-commits mailing list