From 4981d2326ac1a1899879e6acc23fb8f36255f17f Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Thu, 21 Jan 2016 18:55:54 +0000 Subject: [PATCH] [InstCombine] Simplify (x >> y) <= x 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 Patch by Andrea Canciani! Differential Revision: http://reviews.llvm.org/D16402 llvm-svn: 258422 --- lib/Analysis/InstructionSimplify.cpp | 6 ++++-- test/Transforms/InstSimplify/compare.ll | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/Analysis/InstructionSimplify.cpp b/lib/Analysis/InstructionSimplify.cpp index 2b577f0342b..1300ec9560c 100644 --- a/lib/Analysis/InstructionSimplify.cpp +++ b/lib/Analysis/InstructionSimplify.cpp @@ -2727,9 +2727,11 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, } } + // x >> y <=u x // x udiv y <=u x. - if (LBO && match(LBO, m_UDiv(m_Specific(RHS), m_Value()))) { - // icmp pred (X /u Y), 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) diff --git a/test/Transforms/InstSimplify/compare.ll b/test/Transforms/InstSimplify/compare.ll index 6e66fbfede9..356019fb398 100644 --- a/test/Transforms/InstSimplify/compare.ll +++ b/test/Transforms/InstSimplify/compare.ll @@ -397,6 +397,22 @@ define i1 @lshr3(i32 %x) { ; 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