diff options
| author | Rafael Espindola <rafael.espindola@gmail.com> | 2014-05-30 15:54:32 +0000 |
|---|---|---|
| committer | Rafael Espindola <rafael.espindola@gmail.com> | 2014-05-30 15:54:32 +0000 |
| commit | c323952cb4f123c507d8b57bbc5cc7b241651d8e (patch) | |
| tree | 42949270fba1a9850e7dcd888956e209c93e620d | |
| parent | 92945eee80d8d39a18b4033f7d589c92e6f50f39 (diff) | |
| download | bcm5719-llvm-c323952cb4f123c507d8b57bbc5cc7b241651d8e.tar.gz bcm5719-llvm-c323952cb4f123c507d8b57bbc5cc7b241651d8e.zip | |
PR19753: Optimize comparisons with "ashr exact" of a constanst.
Patch by suyog sarda.
llvm-svn: 209903
| -rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 17 | ||||
| -rw-r--r-- | llvm/test/Transforms/InstCombine/icmp.ll | 8 |
2 files changed, 25 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index 02e8bf10135..79cd1fba85f 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -2439,6 +2439,23 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { return new ICmpInst(I.getPredicate(), A, B); } + // PR19753: + // (icmp (ashr exact const2, A), const1) -> icmp A, Log2(const2/const1) + // Cases where const1 doesn't divide const2 exactly or Quotient is not + // exact of log2 are handled by SimplifyICmpInst call above where we + // return false. + // TODO: Handle this for lshr exact with udiv. + { + ConstantInt *CI2; + if (match(Op0, m_AShr(m_ConstantInt(CI2), m_Value(A))) && + (cast<BinaryOperator>(Op0)->isExact())) { + APInt Quotient = CI2->getValue().sdiv(CI->getValue()); + unsigned shift = Quotient.logBase2(); + return new ICmpInst(I.getPredicate(), A, + ConstantInt::get(A->getType(), shift)); + } + } + // If we have an icmp le or icmp ge instruction, turn it into the // appropriate icmp lt or icmp gt instruction. This allows us to rely on // them being folded in the code below. The SimplifyICmpInst code has diff --git a/llvm/test/Transforms/InstCombine/icmp.ll b/llvm/test/Transforms/InstCombine/icmp.ll index f45897cd3ee..fd7514d9e17 100644 --- a/llvm/test/Transforms/InstCombine/icmp.ll +++ b/llvm/test/Transforms/InstCombine/icmp.ll @@ -1365,3 +1365,11 @@ define i1 @icmp_neg_cst_slt(i32 %a) { %2 = icmp slt i32 %1, -10 ret i1 %2 } + +; CHECK-LABEL: @exact_ashr_eq_false +; CHECK-NEXT: icmp eq i32 %a, 1 +define i1 @exact_ashr_eq_false(i32 %a) { + %shr = ashr exact i32 -30, %a + %cmp = icmp eq i32 %shr, -15 + ret i1 %cmp +} |

