Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between -O0 and -O1 in GCC

Tags:

c

gcc

embedded

arm

While compiling some code I noticed big differences in the assembler created between -O0 and -O1. I wanted to run through enabling/disabling optimisations until I found out what was causing a certain change in the assembler.

If I use -fverbose-asm to find out exactly which flags O1 is enabling compared to O0, and then disable them manually, why is the assembler produced still so massively different? Even if I run gcc with O0 and manually add all the flags that fverbose-asm said were enabled with O1, I don't get the same assembler that I would have got just by using O1.

Is there anything apart from '-f...' and '-m...' that can be changed?

Or is is just that 'O1' has some magic compared with 'O0' that cannot be turned off.


Sorry for the crypticness - this was related to Reducing stack usage during recursion with GCC + ARM however the mention of it was making the question a bit hard to understand.

like image 642
Gordon Williams Avatar asked Oct 31 '12 11:10

Gordon Williams


People also ask

What is difference between G ++ and GCC?

Difference between GCC and G++ We use the G++ command for compiling the C++ program. We use the GCC command for compiling the C program. The G++ command is capable of compiling the . cpp or .

What is difference between GCC and CC?

CC is the name given to the UNIX Compiler Command. It is used as the default compiler command for your operating system and also is executable with the same command. GCC, on the other hand, is the GNU Compiler operating system.

Does g ++ come with GCC?

As of May 2021, the recent 11.1 release of GCC includes front ends for C ( gcc ), C++ ( g++ ), Objective-C, Fortran ( gfortran ), Ada (GNAT), Go ( gccgo ) and D ( gdc , since 9.1) programming languages, with the OpenMP and OpenACC parallel language extensions being supported since GCC 5.1.

What is G ++ used for?

g++ command is a GNU c++ compiler invocation command, which is used for preprocessing, compilation, assembly and linking of source code to generate an executable file.


2 Answers

If all you want is to see which passes are enabled at O1 which are not enabled at O0 you could run something like:

gcc -O0 test.c -fdump-tree-all -da
ls > O0
rm -f test.c.*
gcc -O1 test.c -fdump-tree-all -da
ls > O1
diff O0 O1

A similar process, using the set of flags which you discovered, will let you see what extra magic passes not controlled by flags are undertaken by GCC at O1.

EDIT:

A less messy way might be to compare the output of -fdump-passes, which will list which passes are ON or OFF to stderr.

So something like:

gcc -O0 test.c -fdump-passes |& grep ON > O0
gcc -O1 test.c -fdump-passes |& grep ON > O1
diff O0 O1
like image 124
James Greenhalgh Avatar answered Oct 10 '22 00:10

James Greenhalgh


Not that this helps, other than providing some evidence for your suspicions about -O1 magic that can't be turned off:

  • From http://gcc.gnu.org/ml/gcc-help/2007-11/msg00214.html:

    CAVEAT, not all optimizations enabled by -O1 have a command-line toggle flag to disable them.

  • From Hagen's "Definitive Guide to GCC, 2nd Ed":

    Note: Not all of GCC’s optimizations can be controlled using a flag. GCC performs some optimizations automatically and, short of modifying the source code, you cannot disable these optimizations when you request optimization using -O

Unfortunately, I haven't found any clear statement about what these hard-coded optimizations might be. Hopefully someone who is knowlegable about GCC's internals might post an answer with some information about that.

like image 20
Michael Burr Avatar answered Oct 10 '22 00:10

Michael Burr