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

Anton Bikineev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 14 18:42:53 PST 2018


AntonBikineev updated this revision to Diff 129795.

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,16 @@
   ret i32 %mul
 }
 
+define i32 @sdiv6(i32 %x, i32 %y) {
+; CHECK-LABEL: @sdiv6(
+; CHECK:         ret i32 4
+;
+; (X << C1) / X -> 1 << C1
+  %mul = shl i32 %x, 2 
+  %r = sdiv i32 %mul, %x
+  ret i32 %r
+}
+
 
 define i32 @udiv1(i32 %x, i32 %y) {
 ; CHECK-LABEL: @udiv1(
@@ -211,6 +221,16 @@
   ret i32 %mul
 }
 
+define i32 @udiv6(i32 %x, i32 %y) {
+; CHECK-LABEL: @udiv6(
+; CHECK:         ret i32 4
+;
+; (X << C1) / X -> 1 << C1
+  %mul = shl i32 %x, 2 
+  %r = udiv i32 %mul, %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,11 @@
         return X;
   }
 
+  // (X << C1) / X -> 1 << C1
+  const APInt *C = nullptr;
+  if (match(Op0, m_Shl(m_Value(X), m_APInt(C))) && X == Op1)
+    return ConstantInt::get(X->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.129795.patch
Type: text/x-patch
Size: 1436 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180115/ff35b25e/attachment.bin>


More information about the llvm-commits mailing list