[PATCH] D42032: [LLVM][PASSES][InstCombine] Fix (a + a + ...) / a cases

Anton Bikineev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 17 17:41:55 PST 2018


AntonBikineev updated this revision to Diff 130338.
AntonBikineev added a comment.

@lebedev.ri 
My pleasure. Thanks for reviewing this!
@majnemer
Done.


https://reviews.llvm.org/D42032

Files:
  lib/Analysis/InstructionSimplify.cpp
  test/Transforms/InstSimplify/reassociate.ll


Index: test/Transforms/InstSimplify/reassociate.ll
===================================================================
--- test/Transforms/InstSimplify/reassociate.ll
+++ test/Transforms/InstSimplify/reassociate.ll
@@ -158,6 +158,29 @@
   ret i32 %mul
 }
 
+define i32 @sdiv6(i32 %x, i32 %y) {
+; CHECK-LABEL: @sdiv6(
+; CHECK:         ret i32 4
+;
+; (X << C1) / X -> 1 << C1
+  %shl = shl nsw i32 %x, 2 
+  %r = sdiv i32 %shl, %x
+  ret i32 %r
+}
+
+; make sure the previous opt doesn't take place for wrapped shifts
+
+define i32 @sdiv7(i32 %x, i32 %y) {
+; CHECK-LABEL: @sdiv7(
+; CHECK:         %shl = shl i32 %x, 2
+; CHECK-NEXT:    %r = sdiv i32 %shl, %x
+; CHECK-NEXT:    ret i32 %r
+;
+  %shl = shl i32 %x, 2 
+  %r = sdiv i32 %shl, %x
+  ret i32 %r
+}
+
 
 define i32 @udiv1(i32 %x, i32 %y) {
 ; CHECK-LABEL: @udiv1(
@@ -211,6 +234,29 @@
   ret i32 %mul
 }
 
+define i32 @udiv6(i32 %x, i32 %y) {
+; CHECK-LABEL: @udiv6(
+; CHECK:         ret i32 4
+;
+; (X << C1) / X -> 1 << C1
+  %shl = shl nuw i32 %x, 2 
+  %r = udiv i32 %shl, %x
+  ret i32 %r
+}
+
+; make sure the previous opt doesn't take place for wrapped shifts
+
+define i32 @udiv7(i32 %x, i32 %y) {
+; CHECK-LABEL: @udiv7(
+; CHECK:         %shl = shl i32 %x, 2
+; CHECK-NEXT:    %r = udiv i32 %shl, %x
+; CHECK-NEXT:    ret i32 %r
+;
+  %shl = shl i32 %x, 2 
+  %r = udiv i32 %shl, %x
+  ret i32 %r
+}
+
 define i16 @trunc1(i32 %x) {
 ; CHECK-LABEL: @trunc1(
 ; CHECK:         ret i16 1
Index: lib/Analysis/InstructionSimplify.cpp
===================================================================
--- lib/Analysis/InstructionSimplify.cpp
+++ lib/Analysis/InstructionSimplify.cpp
@@ -992,6 +992,12 @@
         return X;
   }
 
+  // (X << C1) / X -> 1 << C1
+  const APInt *C = nullptr;
+  if ((IsSigned && match(Op0, m_NSWShl(m_Specific(Op1), m_APInt(C)))) ||
+      (!IsSigned && match(Op0, m_NUWShl(m_Specific(Op1), m_APInt(C)))))
+    return ConstantInt::get(Op1->getType(), APInt(C->getBitWidth(), 1).shl(*C));
+
   // (X rem Y) / Y -> 0
   if ((IsSigned && match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) ||
       (!IsSigned && match(Op0, m_URem(m_Value(), m_Specific(Op1)))))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42032.130338.patch
Type: text/x-patch
Size: 2157 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180118/0099e6fa/attachment.bin>


More information about the llvm-commits mailing list