<div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Yes, it looks like we are missing an IR canonicalization for 'mul' like this:</div><div><a href="https://rise4fun.com/Alive/ARs">https://rise4fun.com/Alive/ARs</a></div><div><br></div><div>  %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></div><div><br></div><div>What other opcodes do you see where this transform applies?<br></div><div><br></div><div>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></div></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Dec 18, 2018 at 1:26 AM Zheng CZ Chen via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</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 Roman,</font><br><br><font size="2">Thanks for your good idea. I think it can solve the abs issue very well. I can continue with my work now^-^.</font><br><br><font size="2">But if it is not abs and there is no select, </font><br><tt><font size="2"> %res = OP i32 %b, %a<br> %sub = sub i32 0, %b<br> %res2 = OP i32 %sub, %a<br></font></tt><br><tt><font size="2">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><br><font size="2">Not sure whether we can do it in instCombine.</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:167c24e9db7308fca931" 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" border="0"><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 <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:</font><br><br><font size="2" color="#5F5F5F">From:        </font><font size="2">Roman Lebedev <<a href="mailto:lebedev.ri@gmail.com" target="_blank">lebedev.ri@gmail.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"><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/18 03:45 PM</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><tt><font size="2">On Tue, Dec 18, 2018 at 10:18 AM Zheng CZ Chen via llvm-dev<br><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> 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>> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>> </font></tt><tt><font size="2"><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a></font></tt><tt><font size="2"><br><br></font></tt><br><br><br>
<p></p></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>