[PATCH] D16402: Simplify (x >> y) <= x

Andrea Canciani via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 21 06:02:18 PST 2016


ranma42 created this revision.
ranma42 added a subscriber: llvm-commits.
ranma42 set the repository for this revision to rL LLVM.

This commit extends the patterns recognised by InstSimplify to also handle (x >> y) <= x in the same way as (x /u y) <= x.

The missing optimisation was found investigating why LLVM did not optimise away bound checks in a binary search: https://github.com/rust-lang/rust/pull/30917

I was did not like duplicating the code for udiv and lshr, so I tried to replicate the pattern match as in ValueTracking.cpp:1803.

Repository:
  rL LLVM

http://reviews.llvm.org/D16402

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

Index: test/Transforms/InstSimplify/compare.ll
===================================================================
--- test/Transforms/InstSimplify/compare.ll
+++ test/Transforms/InstSimplify/compare.ll
@@ -397,6 +397,22 @@
 ; CHECK: ret i1 true
 }
 
+define i1 @lshr4(i32 %X, i32 %Y) {
+; CHECK-LABEL: @lshr4(
+  %A = lshr i32 %X, %Y
+  %C = icmp ule i32 %A, %X
+  ret i1 %C
+; CHECK: ret i1 true
+}
+
+define i1 @lshr5(i32 %X, i32 %Y) {
+; CHECK-LABEL: @lshr5(
+  %A = lshr i32 %X, %Y
+  %C = icmp ugt i32 %A, %X
+  ret i1 %C
+; CHECK: ret i1 false
+}
+
 define i1 @ashr1(i32 %x) {
 ; CHECK-LABEL: @ashr1(
   %s = ashr i32 -1, %x
Index: lib/Analysis/InstructionSimplify.cpp
===================================================================
--- lib/Analysis/InstructionSimplify.cpp
+++ lib/Analysis/InstructionSimplify.cpp
@@ -2727,9 +2727,10 @@
     }
   }
 
-  // x udiv y <=u x.
-  if (LBO && match(LBO, m_UDiv(m_Specific(RHS), m_Value()))) {
-    // icmp pred (X /u Y), X
+  // x >> y <=u x; x udiv y <=u x.
+  if (LBO && (match(LBO, m_LShr(m_Specific(RHS), m_Value())) ||
+              match(LBO, m_UDiv(m_Specific(RHS), m_Value())))) {
+    // icmp pred (X op Y), X
     if (Pred == ICmpInst::ICMP_UGT)
       return getFalse(ITy);
     if (Pred == ICmpInst::ICMP_ULE)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D16402.45520.patch
Type: text/x-patch
Size: 1279 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160121/9bafdfed/attachment.bin>


More information about the llvm-commits mailing list