[llvm] r250746 - [InstCombine] Optimize icmp of inc/dec at RHS

David Majnemer via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 21 14:36:58 PDT 2015


On Mon, Oct 19, 2015 at 3:08 PM, Michael Liao via llvm-commits <
llvm-commits at lists.llvm.org> wrote:

> Author: hliao
> Date: Mon Oct 19 17:08:14 2015
> New Revision: 250746
>
> URL: http://llvm.org/viewvc/llvm-project?rev=250746&view=rev
> Log:
> [InstCombine] Optimize icmp of inc/dec at RHS
>
> Allow LLVM to optimize the sequence like the following:
>
>   %inc = add nsw i32 %i, 1
>   %cmp = icmp slt %n, %inc
>
> into:
>
>   %cmp = icmp sle i32 %n, %i
>
> The case is not handled previously due to the complexity of compuation of
> %n.
> Hence, LLVM cannot swap operands of icmp accordingly.
>
>
> Modified:
>     llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
>     llvm/trunk/test/Transforms/InstCombine/icmp.ll
>
> Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=250746&r1=250745&r2=250746&view=diff
>
> ==============================================================================
> --- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
> (original)
> +++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Mon Oct
> 19 17:08:14 2015
> @@ -3426,6 +3426,26 @@ Instruction *InstCombiner::visitICmpInst
>          match(B, m_One()))
>        return new ICmpInst(CmpInst::ICMP_SGE, A, Op1);
>
> +    // icmp sgt X, (Y + -1) -> icmp sge X, Y
> +    if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGT &&
> +        match(D, m_AllOnes()))
> +      return new ICmpInst(CmpInst::ICMP_SGE, Op0, C);
> +
> +    // icmp sle X, (Y + -1) -> icmp slt X, Y
> +    if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLE &&
> +        match(D, m_AllOnes()))
> +      return new ICmpInst(CmpInst::ICMP_SLT, Op0, C);
> +
> +    // icmp sge X, (Y + 1) -> icmp sgt X, Y
> +    if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGE &&
> +        match(D, m_One()))
> +      return new ICmpInst(CmpInst::ICMP_SGT, Op0, C);
> +
> +    // icmp slt X, (Y + 1) -> icmp sle X, Y
> +    if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLT &&
> +        match(D, m_One()))
> +      return new ICmpInst(CmpInst::ICMP_SLE, Op0, C);
> +
>      // if C1 has greater magnitude than C2:
>      //  icmp (X + C1), (Y + C2) -> icmp (X + C3), Y
>      //  s.t. C3 = C1 - C2
>
> Modified: llvm/trunk/test/Transforms/InstCombine/icmp.ll
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/icmp.ll?rev=250746&r1=250745&r2=250746&view=diff
>
> ==============================================================================
> --- llvm/trunk/test/Transforms/InstCombine/icmp.ll (original)
> +++ llvm/trunk/test/Transforms/InstCombine/icmp.ll Mon Oct 19 17:08:14 2015
> @@ -1632,3 +1632,43 @@ entry:
>    %cmp580 = icmp ule i16 mul (i16 zext (i8 ptrtoint (i1 (i16)* @f10 to
> i8) to i16), i16 zext (i8 ptrtoint (i1 (i16)* @f10 to i8) to i16)), %p
>    ret i1 %cmp580
>  }
> +
> +; CHECK-LABEL: @cmp_sgt_rhs_dec
> +; CHECK-NOT: sub
> +; CHECK: icmp sge
>

Please completely match the icmp sequence including, not just the opcode.


> +define i1 @cmp_sgt_rhs_dec(float %x, i32 %i) {
> +  %conv = fptosi float %x to i32
>

Please remove the 'fptosi' in these tests, they are not relevant to the
transform.


> +  %dec = sub nsw i32 %i, 1
> +  %cmp = icmp sgt i32 %conv, %dec
> +  ret i1 %cmp
> +}
> +
> +; CHECK-LABEL: @cmp_sle_rhs_dec
> +; CHECK-NOT: sub
> +; CHECK: icmp slt
> +define i1 @cmp_sle_rhs_dec(float %x, i32 %i) {
> +  %conv = fptosi float %x to i32
> +  %dec = sub nsw i32 %i, 1
> +  %cmp = icmp sle i32 %conv, %dec
> +  ret i1 %cmp
> +}
> +
> +; CHECK-LABEL: @cmp_sge_rhs_inc
> +; CHECK-NOT: add
> +; CHECK: icmp sgt
> +define i1 @cmp_sge_rhs_inc(float %x, i32 %i) {
> +  %conv = fptosi float %x to i32
> +  %inc = add nsw i32 %i, 1
> +  %cmp = icmp sge i32 %conv, %inc
> +  ret i1 %cmp
> +}
> +
> +; CHECK-LABEL: @cmp_slt_rhs_inc
> +; CHECK-NOT: add
> +; CHECK: icmp sle
> +define i1 @cmp_slt_rhs_inc(float %x, i32 %i) {
> +  %conv = fptosi float %x to i32
> +  %inc = add nsw i32 %i, 1
> +  %cmp = icmp slt i32 %conv, %inc
> +  ret i1 %cmp
> +}
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151021/a0dcbbf8/attachment.html>


More information about the llvm-commits mailing list