[coldfire-gnu-discuss] Optimizing problem

David Brown david at westcontrol.com
Fri Jan 5 11:22:54 UTC 2007


DMS Tech, John wrote:
> When I use the optimize option –O1 I have a problem that a small delay 
> loop, used in a boot-loader program, gets optimized out of the code. 
> Only the function call stays but the actual while loop is removed. Is 
> there any compiler option to make sure this loop stays intact or a 
> ‘__attribute__’, which will make sure this function is not optimized?
> 
>  

You have to remember that the compiler generates code to give you the 
effects you ask for - it does not do a blind translation of C code to 
assembly (unless you use no optimization).  So if you write something like:

void delay(void) {
	int i;

	for (i = 0; i < 1000; i++);
}

The compiler is smart enough to see that this does nothing, and 
generates no code.

If you want to force a delay, and don't want to do it properly with a 
timer or something (such as when you are starting up and have not got 
timers running, or for very short delays), then the easiest method is to 
declare the loop variable to be "volatile".

Regarding optimisation - the most sensible choice for most work is -O2 
optimisation, which gives good code while still being clear enough for 
debugging.  Sometimes you might want -O1 if you want to examine 
generated assembly, but I have never found a use for turning off 
optimisation.  If your code works with optimisation off, but fails with 
it enabled, then your code (or your compiler :-) is broken.

mvh.,

David


> 
> John.
> 
>  
> 




More information about the coldfire-gnu-discuss mailing list