The way I planned to go about that particular optimization at the time was a quite general method. First of all I would find all innermost loops. For each one I'd check whether the number of < matches the number of >. If that is the case you can then simulate 1 iteration of the loop. For the loop to be optimizable it should decrease the square with index 0 by exactly 1. You can then use the result of that 1 iteration to determine what happens to all the other squares.
For example take the code
.
Find all inner loops:
Count number of < and > and compare
Determine that the numbers match.
Simulating one iteration then gives:
Code: Select all
d[0] = -1;
d[1] = 0;
d[2] = 1;
d[3] = 2;
Generate the actual code:
Code: Select all
d[1] += 0*d[0]; // Could be optimized away
d[2] += 1*d[0];
d[3] += 2*d[0];
d[0] = 0;
Hope that that gives the general idea.