<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">Hi,<div><br></div><div>Here is a patch to expose more subtract and comparison fusion opportunities.</div><div>I am not sure the approach I have taken is the best one, so let me know if you think I should proceed differently.</div><div><br></div><div>Thanks for the feedbacks/reviews.</div><div><br></div><div>** Context **</div><div><br></div><div>Several architectures use the same instruction to perform both a comparison and a subtract. Our current instruction selection framework does not allow to consider different basic blocks to expose such fusion opportunities. As it is, these instructions are “merged” by CSE in the MI IR level.</div><div><br></div><div><br></div><div>** Proposed Solution **</div><div><br></div><div>To increase the likelihood of CSE to apply in such situation, the proposed patch reorders the operands of the comparison so that they matches the order of the most frequent subtract.</div><div>E.g.,</div><div><br></div><div>icmp A, B</div><div>…</div><div>sub B, A</div><div><br></div><div>B is used as the first argument of a sub with A. The comparison should match this for CSE to come into play.</div><div><br></div><div>This is done during instcombine. </div><div><br></div><div><br></div><div>** Motivating Example **</div><div><br></div><div>The attached <a href="http://scratch.cc">scratch.cc</a> shows two different functions that do exactly the same thing (the order of the operands in the ‘if’ and the condition are inversed) but produces different code quality.</div><div>Function PASS has its comparison and subtract merged, function FAIL does not.</div><div><br></div><div>To reproduce:</div><div>clang -O3 -arch x86_64 <a href="http://scratch.cc">scratch.cc</a> -S -o -</div><div><br></div><div><br></div><div>** Notes **</div><div><br></div><div>1. Why LLVM IR for this “low-level” optimization?</div><div>As already stated, several architectures expose such opportunities, therefore, I thought it may be best to do it as a target independent optimization. LLVM IR makes more sense for that. Doing this at MI IR level would require several additional target hooks or a specific pass for each target.</div><div><br></div><div>2. What about the canonical form?</div><div>The optimization is only performed when both operands have the same complexity.</div><div>We might want to break the complexity assumption (operands ordered from most to less complex) but I am not sure it will bring new opportunities.</div><div>Thus, assuming we want to do that transformation at LLVM IR level, is it desirable to break that assumption and if yes, in which pass?</div><div><br></div><div>Cheers,<br><div apple-content-edited="true">
<div style="color: rgb(0, 0, 0); font-family: Helvetica;  font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">-Quentin</div>

</div>
</div></body></html>