<div dir="ltr">Hello, folks!<br> <br>I need help with LSR optimization.<br><br>I've spent pretty much trying to understand what's going on here, but still don’t understand some things.<br><br><b>First</b>, what is Scale in Formula? Consider <b>for(i=0; i < n; i++)</b>. The initial formula for ICMP use will be:<br><br>(1)    reg({%n,+,-1}<nw><%for.body>)<br><br>Next we build other forms:<div><br>    (2)     reg(%n) + 1*reg({0,+,-1}<br>    (3)     reg({(-1 * %n),+,1}<br>    (4)     reg((-1 * %n)) + 1*reg({0,+,1}<br>    (5)     reg(%n) + -1*reg({0,+,1}<br>    (6)     reg((-1 * %n)) + -1*reg({0,+,-1} <br><br>I’m wondering about (4) and (6). What’s negative scale? It affects formula’s cost, so, for example, (4) loses to (5).<br><br><b>Second</b>, we change ICMP to compare against zero, so we get <b>for(i=n; i > 0; i--)</b> and (1) for initial formula.<br><br>But we’re not considering<br>    (7)     reg((-1 * %n)) + reg({0,+,1} <br><br>which corresponds to our initial <b>for(i=0; i < n; i++)</b>.<br><br>Again, I don’t know what’s the difference between this and (4) and (6), bit it will win (5) at least.<br><br> Now, I also want (7) to win (1), in cases like this <br><br><i>void foo( int n,  int red[], int green[],  int blue[], int alpha[],<br>                int rdest[],  int gdest[], int bdest[],  int adest[] )<br>{<br>         for (int i=0;i<n;i++) {<br>               red[i]   = rdest[i];<br>               green[i] = gdest[i];<br>               blue[i]  = bdest[i];<br>               alpha[i] = adest[i];           <br>        }<br>}</i><br> <br>Just compile it with GCC and CLANG and see how bad the things are. (.text section is 64 bytes for GCC and 184 for CLANG). I'm talking about x86, not sure for other architectures.<br><br>The problem is that we shouldn’t change loop direction, i.e. apply (1). If you replace <b>n</b> in example above with constant, everything will be fine. (because </div><div><i>-1024 + reg({0,+,1}</i> will win <i>reg({1024,+,-1}</i>)<br><br> Even with (7) it will always lose to (1), because (1) has less registers. <br><br>So, any ideas on this? <br><br>P.S. I was thinking about a hack, when in certain conditions we just explicitly lower formula’s cost (NumRegs -= 1), but this is probably not the best way.<br><br> <br>Thanks, <br><font color="#000000">> Anton</font></div></div>