In GCC 10.2, -O2 optimization enables more than docs suggest

Thu Jan 21 03:53:07 GMT 2021

On 1/20/21 1:17 PM, David Brown wrote:

> I /never/ use -O0, precisely because I find it absolutely terrible for
> assembly level debugging.  You can't see the wood for the trees, as all
> local variables are on the stack, and even the simplest of C expressions
> ends up with large numbers of assembly instructions.  In my experience -
> and this is obviously very subjective - using -O1 gives far more
> readable assembly code while avoiding the kinds of code re-arrangement
> and re-ordering of -O2 that makes assembly-level debugging difficult.
> (-Og is an alternative for modern gcc versions, which can give most of
> the speed of -O2 but is a little easier for debugging).

Interesting. My recollection is that -O0, regardless of variables being 
on the stack, was more "linear": Each C statement was followed 
more-or-less by the assembly code required to implement it, then the 
next C statement and so on. In particular, variables in -O1 can get 
tucked away into registers and "disappear" for long stretches of 
assembly before popping up again, and the spaghetti-code jumping for 
common code block elimination. Which is of course all good optimization, 
but makes things hard to follow.

But I'll have to revisit the issue again.

> Another major benefit of -O1 is that it enables much more code analysis,
> which in turn enables much better static checking - I am a big fan of
> warning flags and having the compiler tell me of likely problems before
> I get as far as testing and debugging.

Me, too ("big fan").

> (Your project here looks very interesting - I'm going to have a good
> look at it when I get the chance.  I won't be able to use it directly,
> as a pure GPL license basically makes it unusable for anything but
> learning or hobby use, but as it matches ideas I have had myself I am
> interested in how it works.)

Thanks. Yes, it's basically a simple idea, and I found out recently that 
others have attempted something similar (which I wish I'd known when I 
started doing it myself). This is now very off-topic for this list, but 
I'd like to get your input, including the GPL vs LPGPL issue (ironic 
given that this is a GNU mailing list). Maybe open an issue at the 
Github repository and we can discuss it there?

More information about the Gcc-help mailing list