Heroic constant folding (was micro-optimizing loops etc.)

Stephen Clamage steve at taumet.com
Tue Mar 5 02:18:24 AEST 1991


ckp at grebyn.com (Checkpoint Technologies) writes:

>int fib_array[1024];

>void init_fib(void)
>{
	... dynamic initialization of fib_array ...
>}

>int main(int argc, char **argv)
>{
>init_fib();
>...

>...into...

>int fib_array[1024] = {1, 1, 2, 3, 5, 8, 13, 21, /* you get the idea */

I certainly would NOT want the compiler to do this, as it changes the
meaning of the program.

1. fib_array is a global array which might be set and used elsewhere in the
program before init_fib() is called.  Replacing the runtime initialization
with a static initialization could well result in completely different
program behavior. 

2. init_fib() is a global function which might be called elsewhere.  If
the compiler deletes the function, the program will fail to link.

You could argue for replacing the loop in init_fib() with a series of
constant assignments.  ('Loop unrolling' is a standard optimization
technique on parallelizing computers with lots of memory.)  This would
preserve the program's semantics, but might result in a larger program.
Depending on instruction caching and look-ahead, the unrolled loop might
execute more slowly than the original loop, apart from being larger.
-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.lang.c mailing list