<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Oct 19, 2015 at 3:08 PM, Michael Liao via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Author: hliao<br>
Date: Mon Oct 19 17:08:14 2015<br>
New Revision: 250746<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=250746&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=250746&view=rev</a><br>
Log:<br>
[InstCombine] Optimize icmp of inc/dec at RHS<br>
<br>
Allow LLVM to optimize the sequence like the following:<br>
<br>
%inc = add nsw i32 %i, 1<br>
%cmp = icmp slt %n, %inc<br>
<br>
into:<br>
<br>
%cmp = icmp sle i32 %n, %i<br>
<br>
The case is not handled previously due to the complexity of compuation of %n.<br>
Hence, LLVM cannot swap operands of icmp accordingly.<br>
<br>
<br>
Modified:<br>
llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp<br>
llvm/trunk/test/Transforms/InstCombine/icmp.ll<br>
<br>
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=250746&r1=250745&r2=250746&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=250746&r1=250745&r2=250746&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Mon Oct 19 17:08:14 2015<br>
@@ -3426,6 +3426,26 @@ Instruction *InstCombiner::visitICmpInst<br>
match(B, m_One()))<br>
return new ICmpInst(CmpInst::ICMP_SGE, A, Op1);<br>
<br>
+ // icmp sgt X, (Y + -1) -> icmp sge X, Y<br>
+ if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGT &&<br>
+ match(D, m_AllOnes()))<br>
+ return new ICmpInst(CmpInst::ICMP_SGE, Op0, C);<br>
+<br>
+ // icmp sle X, (Y + -1) -> icmp slt X, Y<br>
+ if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLE &&<br>
+ match(D, m_AllOnes()))<br>
+ return new ICmpInst(CmpInst::ICMP_SLT, Op0, C);<br>
+<br>
+ // icmp sge X, (Y + 1) -> icmp sgt X, Y<br>
+ if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGE &&<br>
+ match(D, m_One()))<br>
+ return new ICmpInst(CmpInst::ICMP_SGT, Op0, C);<br>
+<br>
+ // icmp slt X, (Y + 1) -> icmp sle X, Y<br>
+ if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLT &&<br>
+ match(D, m_One()))<br>
+ return new ICmpInst(CmpInst::ICMP_SLE, Op0, C);<br>
+<br>
// if C1 has greater magnitude than C2:<br>
// icmp (X + C1), (Y + C2) -> icmp (X + C3), Y<br>
// s.t. C3 = C1 - C2<br>
<br>
Modified: llvm/trunk/test/Transforms/InstCombine/icmp.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/icmp.ll?rev=250746&r1=250745&r2=250746&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/icmp.ll?rev=250746&r1=250745&r2=250746&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/InstCombine/icmp.ll (original)<br>
+++ llvm/trunk/test/Transforms/InstCombine/icmp.ll Mon Oct 19 17:08:14 2015<br>
@@ -1632,3 +1632,43 @@ entry:<br>
%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<br>
ret i1 %cmp580<br>
}<br>
+<br>
+; CHECK-LABEL: @cmp_sgt_rhs_dec<br>
+; CHECK-NOT: sub<br>
+; CHECK: icmp sge<br></blockquote><div><br></div><div>Please completely match the icmp sequence including, not just the opcode.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
+define i1 @cmp_sgt_rhs_dec(float %x, i32 %i) {<br>
+ %conv = fptosi float %x to i32<br></blockquote><div><br></div><div>Please remove the 'fptosi' in these tests, they are not relevant to the transform.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
+ %dec = sub nsw i32 %i, 1<br>
+ %cmp = icmp sgt i32 %conv, %dec<br>
+ ret i1 %cmp<br>
+}<br>
+<br>
+; CHECK-LABEL: @cmp_sle_rhs_dec<br>
+; CHECK-NOT: sub<br>
+; CHECK: icmp slt<br>
+define i1 @cmp_sle_rhs_dec(float %x, i32 %i) {<br>
+ %conv = fptosi float %x to i32<br>
+ %dec = sub nsw i32 %i, 1<br>
+ %cmp = icmp sle i32 %conv, %dec<br>
+ ret i1 %cmp<br>
+}<br>
+<br>
+; CHECK-LABEL: @cmp_sge_rhs_inc<br>
+; CHECK-NOT: add<br>
+; CHECK: icmp sgt<br>
+define i1 @cmp_sge_rhs_inc(float %x, i32 %i) {<br>
+ %conv = fptosi float %x to i32<br>
+ %inc = add nsw i32 %i, 1<br>
+ %cmp = icmp sge i32 %conv, %inc<br>
+ ret i1 %cmp<br>
+}<br>
+<br>
+; CHECK-LABEL: @cmp_slt_rhs_inc<br>
+; CHECK-NOT: add<br>
+; CHECK: icmp sle<br>
+define i1 @cmp_slt_rhs_inc(float %x, i32 %i) {<br>
+ %conv = fptosi float %x to i32<br>
+ %inc = add nsw i32 %i, 1<br>
+ %cmp = icmp slt i32 %conv, %inc<br>
+ ret i1 %cmp<br>
+}<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>