<div dir="ltr"><div dir="ltr"><div>Be careful with sdiv/srem - the transform is not valid on the minval edge cases:</div><div><a href="https://rise4fun.com/Alive/cWl">https://rise4fun.com/Alive/cWl</a></div><div><br></div><div><br></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Dec 18, 2018 at 6:48 PM Zheng CZ Chen <<a href="mailto:czhengsz@cn.ibm.com">czhengsz@cn.ibm.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><p><font size="2">Hi Sanjay,</font><br><br><font size="2">for the original abs issue, I tested following pattern:</font><br><br><font size="2">return (a+b) >= 0 ? (a+b) : -(a+b);  //works now, can be recognized as abs</font><br><font size="2">return (a+b) >= 0 ? (a+b) : -a-b;    //works now</font><br><br><font size="2">return (a-b) >= 0 ? (a-b) : -(a-b);  //works now</font><br><font size="2">return (a-b) >= 0 ? (a-b) : -a+b;   //works now</font><br><br><font size="2">return (a*b) >= 0 ? (a*b) : -(a*b);  //works now</font><br><b><font size="2">return (a-b) >= 0 ? (a*b) : -a*b;   //doesn't work</font></b><br><br><font size="2">return (a/b) >= 0 ? (a/b) : -(a/b);  //works now</font><br><b><font size="2">return (a/b) >= 0 ? (a/b) : -a/b;   //doesn't work</font></b><br><br><b><font size="2">return (a%b) >= 0 ? (a%b) : -a%b;   //doesn't work</font></b><br><br><font size="2">I think I can start from MUL.</font><br><br><font size="2">Thanks.</font><br><br><font size="2">BRS//</font><br><font size="2">Chen Zheng</font><br><font size="2">Power Compiler Backend Developer</font><br><br><br><img src="cid:167c7138100308fca931" alt="Inactive hide details for Sanjay Patel ---2018/12/19 01:16:24 AM---Yes, it looks like we are missing an IR canonicalization for" width="16" height="16" border="0"><font size="2" color="#424282">Sanjay Patel ---2018/12/19 01:16:24 AM---Yes, it looks like we are missing an IR canonicalization for 'mul' like this:</font><br><br><font size="2" color="#5F5F5F">From:        </font><font size="2">Sanjay Patel <<a href="mailto:spatel@rotateright.com" target="_blank">spatel@rotateright.com</a>></font><br><font size="2" color="#5F5F5F">To:        </font><font size="2">Zheng CZ Chen <<a href="mailto:czhengsz@cn.ibm.com" target="_blank">czhengsz@cn.ibm.com</a>></font><br><font size="2" color="#5F5F5F">Cc:        </font><font size="2">Roman Lebedev <<a href="mailto:lebedev.ri@gmail.com" target="_blank">lebedev.ri@gmail.com</a>>, llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></font><br><font size="2" color="#5F5F5F">Date:        </font><font size="2">2018/12/19 01:16 AM</font><br><font size="2" color="#5F5F5F">Subject:        </font><font size="2">Re: [llvm-dev] should we do this time-consuming transform in InstCombine?</font><br></p><hr style="color:rgb(128,145,165)" width="100%" size="2" noshade align="left"><br><br><br>Yes, it looks like we are missing an IR canonicalization for 'mul' like this:<br><a href="https://rise4fun.com/Alive/ARs" target="_blank"><u><font color="#0000FF">https://rise4fun.com/Alive/ARs</font></u></a><br><br>  %neg = sub i8 0, %x<br>  %r = mul i8 %neg, %y<br>  =><br>  %mul2 = mul i8 %x, %y<br>  %r = sub i8 0, %mul2<br><br>What other opcodes do you see where this transform applies?<br><br>If adding that transform is enough to solve the original problem, then we don't need to add the larger pattern match that includes the select?<br><br>On Tue, Dec 18, 2018 at 1:26 AM Zheng CZ Chen via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank"><u><font color="#0000FF">llvm-dev@lists.llvm.org</font></u></a>> wrote:
<ul><font size="2">Hi Roman,</font><br><font size="2"><br>Thanks for your good idea. I think it can solve the abs issue very well. I can continue with my work now^-^.</font><br><font size="2"><br>But if it is not abs and there is no select, </font><tt><font size="2"><br> %res = OP i32 %b, %a<br>%sub = sub i32 0, %b<br>%res2 = OP i32 %sub, %a</font></tt><br><tt><font size="2"><br>theoretically, we can still do the following transform for the above pattern:<br>%res2 = OP i32 %sub, %a ==> %res2 = sub i32 0, %res</font></tt><br><font size="2"><br>Not sure whether we can do it in instCombine.</font><br><font size="2"><br>Thanks.</font><br><font size="2"><br>BRS//<br>Chen Zheng<br>Power Compiler Backend Developer</font><br><br><br><img src="cid:167c7138100308fca931" alt="Inactive hide details for Roman Lebedev ---2018/12/18 03:45:06 PM---On Tue, Dec 18, 2018 at 10:18 AM Zheng CZ Chen via llvm-dev" width="16" height="16"><font size="2" color="#424282">Roman Lebedev ---2018/12/18 03:45:06 PM---On Tue, Dec 18, 2018 at 10:18 AM Zheng CZ Chen via llvm-dev <</font><a href="mailto:llvm-dev@lists.llvm.org" target="_blank"><u><font size="2" color="#0000FF">llvm-dev@lists.llvm.org</font></u></a><font size="2" color="#424282">> wrote:</font><br><font size="2" color="#5F5F5F"><br>From: </font><font size="2">Roman Lebedev <</font><a href="mailto:lebedev.ri@gmail.com" target="_blank"><u><font size="2" color="#0000FF">lebedev.ri@gmail.com</font></u></a><font size="2">></font><font size="2" color="#5F5F5F"><br>To: </font><font size="2">Zheng CZ Chen <</font><a href="mailto:czhengsz@cn.ibm.com" target="_blank"><u><font size="2" color="#0000FF">czhengsz@cn.ibm.com</font></u></a><font size="2">></font><font size="2" color="#5F5F5F"><br>Cc: </font><a href="mailto:llvm-dev@lists.llvm.org" target="_blank"><u><font size="2" color="#0000FF">llvm-dev@lists.llvm.org</font></u></a><font size="2" color="#5F5F5F"><br>Date: </font><font size="2">2018/12/18 03:45 PM</font><font size="2" color="#5F5F5F"><br>Subject: </font><font size="2">Re: [llvm-dev] should we do this time-consuming transform in InstCombine?</font><br><hr width="100%" size="2" noshade align="left"><br><br><tt><font size="2"><br>On Tue, Dec 18, 2018 at 10:18 AM Zheng CZ Chen via llvm-dev<br><</font></tt><a href="mailto:llvm-dev@lists.llvm.org" target="_blank"><tt><u><font size="2" color="#0000FF">llvm-dev@lists.llvm.org</font></u></tt></a><tt><font size="2">> wrote:<br>><br>> Hi,<br>Hi.<br><br>> There is an opportunity in instCombine for following instruction pattern:<br>><br>> %mul = mul nsw i32 %b, %a<br>> %cmp = icmp sgt i32 %mul, -1<br>> %sub = sub i32 0, %a<br>> %mul2 = mul nsw i32 %sub, %b<br>> %cond = select i1 %cmp, i32 %mul, i32 %mul2<br>><br>> Source code for above pattern:<br>> return (a*b) >=0 ? (a*b) : -a*b;<br>><br>> Currently, llvm(-O3) can not recognize this as abs(a*b).<br>><br>> I initially think we could do this in instCombine phase in opt. Below is what I think:<br>><br>> %res = OP i32 %b, %a<br>> %sub = sub i32 0, %b<br>> %res2 = OP i32 %sub, %a<br>><br>> We could do the transform:<br>> %res2 = OP i32 %sub, %a ==> %res2 = sub i32 0, %res<br>><br>> Then we can get the advantage:<br>> 1: if %res2 is the only user of %sub, %sub can be eliminated;<br>> 2: if %res2 is not the only user of %sub, we could change some heavy instruction like div to sub;<br>> 3: expose more abs opportunity for later pass.<br>><br>> But my concern is finding %res is a little compiling time-consuming.<br>> At least we need MIN(user_count(%b), user_count(%a)) times to check if instruction with same opcode and same operands exists.<br>In instcombine, no user checking is performed/allowed.<br>This should match that *specific* pattern (other than verifying the<br>correct equal binop types), although i have not tested it:<br><br> ICmpInst::Predicate Pred;<br> Value *A, *B, *Mul, *Sub, *Mul2;<br> if (match(&SI,<br>           m_Select(m_ICmp(Pred,<br>                           m_CombineAnd(m_BinOp(m_Value(A), m_Value(B)),<br>                                        m_Value(Mul)),<br>                           m_AllOnes()),<br>                    m_Deferred(Mul),<br>                    m_CombineAnd(<br>                        m_c_BinOp(m_CombineAnd(m_Sub(m_Zero(), m_Deferred(A)),<br>                                               m_Value(Sub)),<br>                                  m_Deferred(B)),<br>                        m_Value(Mul2)))) &&<br>     Pred == ICmpInst::Predicate::ICMP_SGT) {<br> }<br><br>> Could you guys give some comment? Is there any better idea for this transform?<br>><br>> Thanks.<br>><br>> BRS//<br>> Chen Zheng<br>> Power Compiler Backend Developer<br>Roman.<br><br>> _______________________________________________<br>> LLVM Developers mailing list<br>> </font></tt><a href="mailto:llvm-dev@lists.llvm.org" target="_blank"><tt><u><font size="2" color="#0000FF">llvm-dev@lists.llvm.org</font></u></tt></a><tt><font size="2"><br>> </font></tt><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank"><tt><u><font size="2" color="#0000FF">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</font></u></tt></a><tt><font size="2"><br></font></tt><br><br><br><br>_______________________________________________<br>LLVM Developers mailing list<u><font color="#0000FF"><br></font></u><a href="mailto:llvm-dev@lists.llvm.org" target="_blank"><u><font color="#0000FF">llvm-dev@lists.llvm.org</font></u></a><u><font color="#0000FF"><br></font></u><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank"><u><font color="#0000FF">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</font></u></a><br><br></ul><br>
<p></p></div>
</blockquote></div>