<div dir="ltr">On Wed, Jun 26, 2013 at 6:56 AM, Tim Northover <span dir="ltr"><<a href="mailto:tnorthover@apple.com" target="_blank">tnorthover@apple.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
The attached patch adds a heuristic to ShrinkDemandedConstant so that small negative constants are preferred over large ones even if as an unsigned value the transformation would be an improvement.<br>
<br>
The motivating case is added as a test:<br>
define i32 @foo(i64 %in) {<br>
  %sub = sub i64 %in, 4<br>
  %shift = lshr i64 %sub, 2<br>
  %res = trunc i64 %shift to i32<br>
<br>
  ret i32 %res<br>
}<br>
<br>
On x86-64 this currently generates:<br>
<br>
_foo:<br>
        movabsq $17179869180, %rax      ## imm = 0x3FFFFFFFC<br>
        addq    %rdi, %rax<br>
        shrq    $2, %rax<br>
        ret<br>
<br>
with an unnecessary and large movabsq. With this change the fact that we’re really subtracting 4 survives to CodeGen and we just emit<br>
<br>
_foo:<br>
        leaq -4(%rdi), %rax<br>
        shrq $2, %rax<br>
        ret<br>
<br>
Does anyone have any objections to this change? Is it likely to cause more damage than good for later passes?<br><br></blockquote><div><br></div><div>This seems like a big change that could affect all sorts of optimizations; I'd like to see performance numbers from the nightly test suite.</div>
<div><br></div><div>Also, I'm not sure this code is actually performing the check you want it to; it appears that, for example, it would prefer -3 over -4.</div><div><br></div><div>-Eli </div></div><br></div></div>