[PATCH] InstCombine: merge constants in both operands of icmp.

Erik Verbruggen erik.verbruggen at me.com
Wed Mar 26 03:05:02 PDT 2014


Attached is a new patch that fixes all issues you mentioned.

-- Erik.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-InstCombine-merge-constants-in-both-operands-of-icmp.patch
Type: application/octet-stream
Size: 6250 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140326/e2b48aa9/attachment.obj>
-------------- next part --------------


On 25 Mar 2014, at 20:19, Benjamin Kramer <benny.kra at gmail.com> wrote:

> 
> On 25.03.2014, at 16:33, Erik Verbruggen <erik.verbruggen at me.com> wrote:
> 
>> From 5290972d0df27a30a789a3c7a56a794e4bdf059d Mon Sep 17 00:00:00 2001
>> From: Erik Verbruggen <erik.verbruggen at me.com>
>> Date: Mon, 17 Mar 2014 15:26:08 +0100
>> Subject: [PATCH] InstCombine: merge constants in both operands of icmp.
>> 
>> E.g.: x + 10 < 0 --> x < -10 (if add has nsw)
>>      x - 10 < 0 --> x < 10 (if sub has nsw)
>> ---
>> lib/Target/README.txt                              | 12 -----
>> lib/Transforms/InstCombine/InstCombine.h           |  2 +
>> lib/Transforms/InstCombine/InstCombineCompares.cpp | 33 ++++++++++++++
>> test/Transforms/InstCombine/icmp.ll                | 53 ++++++++++++++++++++++
>> 4 files changed, 88 insertions(+), 12 deletions(-)
>> 
>> diff --git a/lib/Target/README.txt b/lib/Target/README.txt
>> index a9aab86..4ecacf9 100644
>> --- a/lib/Target/README.txt
>> +++ b/lib/Target/README.txt
>> @@ -930,18 +930,6 @@ optimized with "clang -emit-llvm-bc | opt -std-compile-opts".
>> 
>> //===---------------------------------------------------------------------===//
>> 
>> -int g(int x) { return (x - 10) < 0; }
>> -Should combine to "x <= 9" (the sub has nsw).  Currently not
>> -optimized with "clang -emit-llvm-bc | opt -std-compile-opts".
>> -
>> -//===---------------------------------------------------------------------===//
>> -
>> -int g(int x) { return (x + 10) < 0; }
>> -Should combine to "x < -10" (the add has nsw).  Currently not
>> -optimized with "clang -emit-llvm-bc | opt -std-compile-opts".
>> -
>> -//===---------------------------------------------------------------------===//
>> -
>> int f(int i, int j) { return i < j + 1; }
>> int g(int i, int j) { return j > i - 1; }
>> Should combine to "i <= j" (the add/sub has nsw).  Currently not
>> diff --git a/lib/Transforms/InstCombine/InstCombine.h b/lib/Transforms/InstCombine/InstCombine.h
>> index 822e146..bc646db 100644
>> --- a/lib/Transforms/InstCombine/InstCombine.h
>> +++ b/lib/Transforms/InstCombine/InstCombine.h
>> @@ -169,6 +169,8 @@ public:
>>                               ConstantInt *DivRHS);
>>   Instruction *FoldICmpAddOpCst(Instruction &ICI, Value *X, ConstantInt *CI,
>>                                 ICmpInst::Predicate Pred);
>> +  Instruction *FoldIcmpBinopConst(BinaryOperator *BO, ConstantInt *Cst,
>> +                                  ICmpInst::Predicate Pred);
>>   Instruction *FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
>>                            ICmpInst::Predicate Cond, Instruction &I);
>>   Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
>> diff --git a/lib/Transforms/InstCombine/InstCombineCompares.cpp b/lib/Transforms/InstCombine/InstCombineCompares.cpp
>> index 8c0ad52..fd365ea 100644
>> --- a/lib/Transforms/InstCombine/InstCombineCompares.cpp
>> +++ b/lib/Transforms/InstCombine/InstCombineCompares.cpp
>> @@ -795,6 +795,29 @@ Instruction *InstCombiner::FoldICmpAddOpCst(Instruction &ICI,
>>   return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantExpr::getSub(SMax, C));
>> }
>> 
>> +Instruction *InstCombiner::FoldIcmpBinopConst(BinaryOperator *BO,
>> +                                              ConstantInt *Cst,
>> +                                              ICmpInst::Predicate Pred) {
>> +  Value *X; ConstantInt *Cst2;
>> +  if (match(BO, m_Add(m_Value(X), m_ConstantInt(Cst2))) ||
>> +      match(BO, m_Add(m_ConstantInt(Cst2), m_Value(X)))) {
> 
> This case should never happen, InstCombine canonicalizes constants to the right side.
> 
>> +    if (!BO->hasNoSignedWrap())
>> +      return nullptr;
>> +    // Transform:
>> +    //   icmp X+Cst2, Cst
>> +    // or:
>> +    //   icmp Cst2+X, Cst
>> +    // into:
>> +    //   icmp X, Cst-Cst2
>> +    // when Cst-Cst2 does not overflow
>> +    bool overflow;
>> +    APInt newCst = Cst->getValue().ssub_ov(Cst2->getValue(), overflow);
> 
> I'd prefer to use consistent PascalCase variable names.
> 
>> +    if (!overflow)
>> +      return new ICmpInst(Pred, X, ConstantInt::get(Cst->getType(), newCst));
>> +  }
>> +  return nullptr;
>> +}
>> +
>> /// FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS
>> /// and CmpRHS are both known to be integer constants.
>> Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
>> @@ -3008,6 +3031,16 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
>>     // icmp X, X+Cst
>>     if (match(Op1, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op0 == X)
>>       return FoldICmpAddOpCst(I, X, Cst, I.getSwappedPredicate());
>> +
>> +    if (match(Op1, m_ConstantInt(Cst))) {
>> +      if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0))
>> +        // icmp binop, Cst
>> +        return FoldIcmpBinopConst(BO, Cst, I.getPredicate());
>> +    } else if (match(Op0, m_ConstantInt(Cst))) {
>> +      if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op1))
>> +        // icmp Cst, binop
>> +        return FoldIcmpBinopConst(BO, Cst, I.getInversePredicate());
>> +    }
>>   }
>>   return Changed ? &I : 0;
>> }
>> diff --git a/test/Transforms/InstCombine/icmp.ll b/test/Transforms/InstCombine/icmp.ll
>> index 12a4744..a78d010 100644
>> --- a/test/Transforms/InstCombine/icmp.ll
>> +++ b/test/Transforms/InstCombine/icmp.ll
>> @@ -1356,3 +1356,56 @@ define i1 @icmp_ashr_ashr_ne(i32 %a, i32 %b) nounwind {
>>  %z = icmp ne i32 %x, %y
>>  ret i1 %z
>> }
>> +
>> +; CHECK-LABEL: icmp_add_const_const1
>> +; CHECK: %cmp = icmp slt i32 %x, -10
>> +; CHEKC-NOT: %add = add nsw i32 %x, 10
> 
> CHEKC?
> 
> - Ben
> 
>> +define i32 @icmp_add_const_const1(i32 %x) nounwind ssp uwtable {
>> +entry:
>> +  %add = add nsw i32 %x, 10
>> +  %cmp = icmp slt i32 %add, 0
>> +  %conv = zext i1 %cmp to i32
>> +  ret i32 %conv
>> +}
>> +
>> +; CHECK-LABEL: icmp_add_const_const2
>> +; CHECK: %cmp = icmp slt i32 %x, -10
>> +; CHEKC-NOT: %add = add nsw i32 %x, 10
>> +define i32 @icmp_add_const_const2(i32 %x) nounwind ssp uwtable {
>> +entry:
>> +  %add = add nsw i32 %x, 10
>> +  %cmp = icmp sgt i32 0, %add
>> +  %conv = zext i1 %cmp to i32
>> +  ret i32 %conv
>> +}
>> +
>> +; CHECK-LABEL: icmp_add_const_intmin
>> +; CHECK: %cmp = icmp ne i32 %x, 2147483638
>> +define i32 @icmp_add_const_intmin(i32 %x) nounwind ssp uwtable {
>> +entry:
>> +  %add = add nsw i32 %x, 10
>> +  %cmp = icmp sgt i32 %add, -2147483648
>> +  %conv = zext i1 %cmp to i32
>> +  ret i32 %conv
>> +}
>> +
>> +; CHECK-LABEL: icmp_add_const_intmax
>> +; CHECK: %cmp = icmp ne i32 %x, 2147483637
>> +define i32 @icmp_add_const_intmax(i32 %x) nounwind ssp uwtable {
>> +entry:
>> +  %add = add nsw i32 %x, 10
>> +  %cmp = icmp slt i32 %add, 2147483647
>> +  %conv = zext i1 %cmp to i32
>> +  ret i32 %conv
>> +}
>> +
>> +; CHECK-LABEL: icmp_sub_const_const
>> +; CHECK: %cmp = icmp slt i32 %x, 10
>> +; CHECK-NOT: %sub = add nsw i32 %x, -10
>> +define i32 @icmp_sub_const_const(i32 %x) nounwind ssp uwtable {
>> +entry:
>> +  %sub = sub nsw i32 %x, 10
>> +  %cmp = icmp slt i32 %sub, 0
>> +  %conv = zext i1 %cmp to i32
>> +  ret i32 %conv
>> +}
>> -- 
>> 1.8.5.4
>> 
> 



More information about the llvm-commits mailing list