]> granicus.if.org Git - llvm/commitdiff
Simplify `x >=u x >> y` and `x >=u x udiv y`
authorSanjoy Das <sanjoy@playingwithpointers.com>
Wed, 26 Oct 2016 19:18:43 +0000 (19:18 +0000)
committerSanjoy Das <sanjoy@playingwithpointers.com>
Wed, 26 Oct 2016 19:18:43 +0000 (19:18 +0000)
Summary:
Extends InstSimplify to handle both `x >=u x >> y` and `x >=u x udiv y`.

This is a folloup of rL258422 and
https://github.com/rust-lang/rust/pull/30917 where llvm failed to
optimize away the bounds checking in a binary search.

Patch by Arthur Silva!

Reviewers: sanjoy

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D25941

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

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

index 1679d3a466794687da43883e3c7f3a94cfe424cf..398ac52c39caf29f64abb87ecc4247c8aeda3aa8 100644 (file)
@@ -2857,6 +2857,17 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
       return getTrue(ITy);
   }
 
+  // x >=u x >> y
+  // x >=u x udiv y.
+  if (RBO && (match(RBO, m_LShr(m_Specific(LHS), m_Value())) ||
+              match(RBO, m_UDiv(m_Specific(LHS), m_Value())))) {
+    // icmp pred X, (X op Y)
+    if (Pred == ICmpInst::ICMP_ULT)
+      return getFalse(ITy);
+    if (Pred == ICmpInst::ICMP_UGE)
+      return getTrue(ITy);
+  }
+
   // handle:
   //   CI2 << X == CI
   //   CI2 << X != CI
index ac8e2649a0ca85b45d18f2a982119e0439ad6879..e5aea26b5aaab81a512fb619e1ad81c41e812b05 100644 (file)
@@ -409,6 +409,22 @@ define i1 @lshr5(i32 %X, i32 %Y) {
 ; CHECK: ret i1 false
 }
 
+define i1 @lshr6(i32 %X, i32 %Y) {
+; CHECK-LABEL: @lshr6(
+  %A = lshr i32 %X, %Y
+  %C = icmp ult i32 %X, %A
+  ret i1 %C
+; CHECK: ret i1 false
+}
+
+define i1 @lshr7(i32 %X, i32 %Y) {
+; CHECK-LABEL: @lshr7(
+  %A = lshr i32 %X, %Y
+  %C = icmp uge i32 %X, %A
+  ret i1 %C
+; CHECK: ret i1 true
+}
+
 define i1 @ashr1(i32 %x) {
 ; CHECK-LABEL: @ashr1(
   %s = ashr i32 -1, %x
@@ -583,6 +599,22 @@ define i1 @udiv6(i32 %X) nounwind {
 ; CHECK: ret i1 %C
 }
 
+define i1 @udiv7(i32 %X, i32 %Y) {
+; CHECK-LABEL: @udiv7(
+  %A = udiv i32 %X, %Y
+  %C = icmp ult i32 %X, %A
+  ret i1 %C
+; CHECK: ret i1 false
+}
+
+define i1 @udiv8(i32 %X, i32 %Y) {
+; CHECK-LABEL: @udiv8(
+  %A = udiv i32 %X, %Y
+  %C = icmp uge i32 %X, %A
+  ret i1 %C
+; CHECK: ret i1 true
+}
+
 define i1 @mul1(i32 %X) {
 ; CHECK-LABEL: @mul1(
 ; Square of a non-zero number is non-zero if there is no overflow.