[PATCH] [InstCombine] Constant comparison involving "lshr exact"
Rahul Jain
rahul1.jain at samsung.com
Mon Jun 2 08:33:06 PDT 2014
Hi Rafael,
Thanks for your valuable comments.
As per your suggestion, I implemented a helper function.
Please help review the same.
Thanks,
Rahul
http://reviews.llvm.org/D3987
Files:
lib/Transforms/InstCombine/InstCombineCompares.cpp
test/Transforms/InstCombine/icmp.ll
Index: test/Transforms/InstCombine/icmp.ll
===================================================================
--- test/Transforms/InstCombine/icmp.ll
+++ test/Transforms/InstCombine/icmp.ll
@@ -1373,3 +1373,11 @@
%cmp = icmp eq i32 %shr, -15
ret i1 %cmp
}
+
+; CHECK-LABEL: @exact_lhsr
+; CHECK-NEXT: icmp eq i32 %a, 3
+define i1 @exact_lhsr(i32 %a) {
+ %shr = lshr exact i32 80, %a
+ %cmp = icmp eq i32 %shr, 10
+ ret i1 %cmp
+}
Index: lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -2318,6 +2318,32 @@
return GlobalSwapBenefits > 0;
}
+// Helper function to check whether Op represents a lshr/ashr exact
+// instruction. For example:
+// (icmp (ashr exact const2, A), const1) -> icmp A, Log2(const2/const1)
+// Here if Op represents -> (ashr exact const2, A), and CI represents
+// const1, we compute Quotient as const2/const1.
+
+static bool CheckShrExact(Value *Op, APInt &Quotient, const ConstantInt *CI,
+ Value *&A) {
+
+ ConstantInt *CI2;
+ if (match(Op, m_AShr(m_ConstantInt(CI2), m_Value(A))) &&
+ (cast<BinaryOperator>(Op)->isExact())) {
+ Quotient = CI2->getValue().sdiv(CI->getValue());
+ return true;
+ }
+
+ // Handle the case for lhsr.
+ else if (match(Op, m_LShr(m_ConstantInt(CI2), m_Value(A))) &&
+ (cast<BinaryOperator>(Op)->isExact())) {
+ Quotient = CI2->getValue().udiv(CI->getValue());
+ return true;
+ }
+
+ return false;
+}
+
Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
bool Changed = false;
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
@@ -2443,13 +2469,10 @@
// (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.
+ // return false. Similar for lshr.
{
- ConstantInt *CI2;
- if (match(Op0, m_AShr(m_ConstantInt(CI2), m_Value(A))) &&
- (cast<BinaryOperator>(Op0)->isExact())) {
- APInt Quotient = CI2->getValue().sdiv(CI->getValue());
+ APInt Quotient;
+ if (CheckShrExact(Op0, Quotient, CI, A)) {
unsigned shift = Quotient.logBase2();
return new ICmpInst(I.getPredicate(), A,
ConstantInt::get(A->getType(), shift));
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D3987.10019.patch
Type: text/x-patch
Size: 2570 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140602/3e92adcb/attachment.bin>
More information about the llvm-commits
mailing list