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

Erik Verbruggen erik.verbruggen at me.com
Wed Mar 26 09:18:33 PDT 2014


Next attempt :)

-- 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: 4436 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140326/1ab900ea/attachment.obj>
-------------- next part --------------



On 26 Mar 2014, at 12:33, Benjamin Kramer <benny.kra at gmail.com> wrote:

> 
> On 26.03.2014, at 11:11, Erik Verbruggen <erik.verbruggen at me.com> wrote:
> 
>> From bdd7dee6632445ce66d827db110fe4cbfd58c56d 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.
>> 
>> Transform:
>>    icmp X+Cst2, Cst
>> into:
>>    icmp X, Cst-Cst2
>> when Cst-Cst2 does not overflow, and the add has nsw.
>> ---
>> lib/Target/README.txt                              | 12 -----
>> lib/Transforms/InstCombine/InstCombine.h           |  2 +
>> lib/Transforms/InstCombine/InstCombineCompares.cpp | 30 ++++++++++++
>> test/Transforms/InstCombine/icmp.ll                | 53 ++++++++++++++++++++++
>> 4 files changed, 85 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..105b32d 100644
>> --- a/lib/Transforms/InstCombine/InstCombineCompares.cpp
>> +++ b/lib/Transforms/InstCombine/InstCombineCompares.cpp
>> @@ -795,6 +795,26 @@ 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)))) {
>> +    if (!BO->hasNoSignedWrap())
>> +      return nullptr;
>> +    // Transform:
>> +    //   icmp X+Cst2, Cst
>> +    // into:
>> +    //   icmp X, Cst-Cst2
>> +    // when Cst-Cst2 does not overflow
>> +    bool Overflow;
>> +    APInt NewCst = Cst->getValue().ssub_ov(Cst2->getValue(), Overflow);
>> +    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 +3028,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());
> 
> getInversePredicate isn't the right function here, it returns the negated predicate. I also think that this part of the code is unnecessary because instcombine will always canonicalize "icmp cst, binop" into "icmp binop, cst" so this pattern would never match.
> 
> - Ben
> 
>> +    }
>>   }
>>   return Changed ? &I : 0;
>> }
>> diff --git a/test/Transforms/InstCombine/icmp.ll b/test/Transforms/InstCombine/icmp.ll
>> index 12a4744..2e3ff24 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
>> +; CHECK-NOT: %add = add nsw i32 %x, 10
>> +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
>> +; CHECK-NOT: %add = add nsw i32 %x, 10
>> +define i32 @icmp_add_const_const2(i32 %x) nounwind ssp uwtable {
>> +entry:
>> +  %add = add nsw i32 10, %x
>> +  %cmp = icmp sgt i32 0, %add
>> +  %conv = zext i1 %cmp to i32
>> +  ret i32 %conv
>> +}
>> +
>> +; CHECK-LABEL: icmp_add_const_const3
>> +; CHECK: %cmp = icmp slt i32 %x, 20
>> +; CHECK-NOT: %sub = add nsw i32 %x, -10
>> +define i32 @icmp_add_const_const3(i32 %x) nounwind ssp uwtable {
>> +entry:
>> +  %add = add nsw i32 -10, %x
>> +  %cmp = icmp sgt i32 10, %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
>> +}
>> -- 
>> 1.8.5.4
>> 
> 



More information about the llvm-commits mailing list