
324 Compilers – Principles and Practice
If the common sub-expression invokes a function call, then the speed gain is even higher; consider
c = sqrt(1 – sin(x) * sin(x));
which can be rewritten as
t = sin(x);
c = sqrt(1 – t * t);
10.4.2 Loop Unwinding
Replace a loop over a small number of iterations by equivalent linear code. For example,
for(i = 0; i < 5; i++) a[i] = i;
is replaced by
a[0] = 0;
a[1] = 1;
a[2] = 2;
etc.
Here the compiler can pre-compute the addresses a[0], a[1], etc. and avoid generation of run-time
array addressing code. Also, the loop set-up and loop-control are eliminated as well.
10.4.3 Replace Index by Pointers
In this method, we replace ...