<html><body><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 width="16" height="16" src="cid:1__=C7BB09F4DFBE6E268f9e8a93df938690918cC7B@" border="0" 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"><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 <llvm-dev@lists.llvm.org> wrote:</font><br><br><font size="2" color="#5F5F5F">From: </font><font size="2">Roman Lebedev <lebedev.ri@gmail.com></font><br><font size="2" color="#5F5F5F">To: </font><font size="2">Zheng CZ Chen <czhengsz@cn.ibm.com></font><br><font size="2" color="#5F5F5F">Cc: </font><font size="2">llvm-dev@lists.llvm.org</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><hr width="100%" size="2" align="left" noshade style="color:#8091A5; "><br><br><br><tt><font size="2">On Tue, Dec 18, 2018 at 10:18 AM Zheng CZ Chen via llvm-dev<br><llvm-dev@lists.llvm.org> 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>> llvm-dev@lists.llvm.org<br>> </font></tt><tt><font size="2"><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a></font></tt><tt><font size="2"><br><br></font></tt><br><br><BR>
</body></html>