<div dir="ltr"><div><span style="font-size:12.8000001907349px">It's not caused by "the insertion point is set to the default after". </span><br></div><div><span style="font-size:12.8000001907349px"><br></span></div>I should mention the reason somewhere earlier. "<span style="font-size:12.8000001907349px">Reversing the order of arg0~3 is not intentional. The user list of pixel_idx happens to have pixel_idx+3, pixel_idx+2, and pixel_idx+1 in this order, so LSR simply follows this order when collecting the LSRFixups."</span><div><div><span style="font-size:12.8000001907349px"><br></span></div><div><span style="font-size:12.8000001907349px">I'm not an expert on uselist orders, but after skimming Duncan Smith's recent work on preserving uselist orders in assembly, these orders are deterministic but arbitrary. So blindly following these orders sometimes cause funny behavior (as in my example). </span></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, May 18, 2015 at 10:10 AM, Daniel Berlin <span dir="ltr"><<a href="mailto:dberlin@dberlin.org" target="_blank">dberlin@dberlin.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Fri, May 8, 2015 at 10:22 PM, Jingyue Wu <<a href="mailto:jingyue@google.com">jingyue@google.com</a>> wrote:<br>
</span><span class="">> Hi,<br>
><br>
> I was tracking down a performance regression and noticed that<br>
> LoopStrengthReduce hoists loop invariants (e.g., the initial formulae of<br>
> indvars) in the reverse order of how they appear in the loop.<br>
</span>It has to to get maximized hoisting.<br>
<span class=""><br>
><br>
> This reverse order creates troubles for the StraightLineStrengthReduce pass<br>
> I recently add. While I understand ultimately SLSR should be able to sort<br>
> independent candidates in an optimal order, does it make sense to<br>
> canonicalizing the order of LSR-hoisted loop invariants back to the<br>
> "natural" order? IMO, the optimized code should in general resemble the<br>
> original code unless intentionally changed otherwise.<br>
<br>
</span>This is likely a mistake, unless i'm missing something.<br>
I suspect the insertion point is set to the default after, so that it<br>
ends up reversing the order, instead of before, so that it ends up in<br>
the original order.<br>
<div><div class="h5"><br>
><br>
> More specifically, I ran LSR on<br>
><br>
> void foo(float *input, int a, int b, int c, int n) {<br>
> for (int node_x = 0; node_x < n; ++node_x) {<br>
> int pixel_idx = (a + node_x) * b; // {a*b, +, b}<br>
> bar(pixel_idx * c); // {a*b*c, +, b*c}<br>
> bar((pixel_idx + 1) * c); // {(a*b+1)*c, +, b*c}<br>
> bar((pixel_idx + 2) * c); // {(a*b+2)*c, +, b*c}<br>
> bar((pixel_idx + 3) * c); // {(a*b+3)*c, +, b*c}<br>
> }<br>
> }<br>
><br>
> and LSR produced<br>
><br>
> void foo(float *input, int a, int b, int c, int n) {<br>
> int arg3 = (a * b + 3) * c;<br>
> int arg2 = (a * b + 2) * c;<br>
> int arg1 = (a * b + 1) * c;<br>
> int arg0 = a * b * c;<br>
> for (int node_x = 0; node_x < n; ++node_x) {<br>
> bar(arg0);<br>
> bar(arg1);<br>
> bar(arg2);<br>
> bar(arg3);<br>
> arg0 += b * c;<br>
> arg1 += b * c;<br>
> arg2 += b * c;<br>
> arg3 += b * c;<br>
> }<br>
> }<br>
> (with obvious redundant operations, i.e. a * b and b * c, combined). Note<br>
> that the order of arg0~3 is reversed.<br>
><br>
> Reversing the order of arg0~3 is not intentional. The user list of pixel_idx<br>
> happens to have pixel_idx+3, pixel_idx+2, and pixel_idx+1 in this order, so<br>
> LSR simply follows this order when collecting the LSRFixups.<br>
><br>
> This creates troubles for SLSR. Given the current order of arg0~arg3<br>
><br>
> int arg3 = (a * b + 3) * c;<br>
> int arg2 = (a * b + 2) * c;<br>
> int arg1 = (a * b + 1) * c;<br>
> int arg0 = a * b * c;<br>
><br>
> SLSR optimizes them to<br>
><br>
> int arg3 = (a * b + 3) * c;<br>
> int arg2 = arg3 - c;<br>
> int arg1 = arg2 - c;<br>
> int arg0 = arg1 - c;<br>
> // 2 muls and 4 adds/subs<br>
><br>
> In contrast, if arg0~3 were in the order of<br>
><br>
> int arg0 = a * b * c;<br>
> int arg1 = (a * b + 1) * c;<br>
> int arg2 = (a * b + 2) * c;<br>
> int arg3 = (a * b + 3) * c;<br>
><br>
> SLSR would optimize them to<br>
><br>
> int arg0 = a * b * c;<br>
> int arg1 = arg0 + c;<br>
> int arg2 = arg1 + c;<br>
> int arg3 = arg2 + c;<br>
> // 2 muls and 3 adds/subs. 1 add/sub less than with the reversed order<br>
><br>
> I have a proof-of-concept patch<br>
> (<a href="http://reviews.llvm.org/differential/diff/25402/" target="_blank">http://reviews.llvm.org/differential/diff/25402/</a>) that has<br>
> CollectFixupsAndInitialFormulae to sort initial formulae in a dominance<br>
> order (i.e. if A.getUser() dominates B.getUser(), then we put A before B).<br>
<br>
</div></div>Is there a reason to not just put it back in the original order?<br>
IE is dominance order better?<br>
</blockquote></div><br></div>