[fpc-devel] Does FPC optimize unused parameters ?

michael.vancanneyt at wisa.be michael.vancanneyt at wisa.be
Wed Aug 8 16:47:39 CEST 2012

On Wed, 8 Aug 2012, Jonas Maebe wrote:

> Martin wrote on Wed, 08 Aug 2012:
>> Out of curiosity. Is there an optimization like this
> [removing calculation/load of unused parameters]
> No.
>> (or is it possible?
> After the body of the called routine has been parsed, it would be possible in 
> theory (with indeed all the caveats the compiler would have to take care of 
> such as side effects -- note that these may include non-obvious side-effects, 
> such as potential overflow exceptions and invalid pointer accesses).

You'd need to check the caller routine as well, values for parameters might
still be used after the call to the function.

Dbg:=SomeCalculatingFunction+' Some message';

DebugLn(Dbg); // If debugln is empty, you don't need dbg for it, so the previous call is not needed.

Writeln(dbg); // but you need it here.

And you don't know what SomeCalculatingFunction may have as side effects.

Function SomeCalculatingFunction : String;


If the call to this function is optimized away, LastKnownAlive is not updated correctly
any more...

Of course, all highly theoretical.
I still use ifdefs for debug messages, and have a template to quickly insert :

{$ifdef debugmsg}SendDebug('|');{$endif debugmsg}

This way, there is no extra code in my final build.


More information about the fpc-devel mailing list