Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Assembly code after every optimization GCC makes?

From Optimization Compiler on Wikipedia,

Compiler optimization is generally implemented using a sequence of optimizing transformations, algorithms which take a program and transform it to produce a semantically equivalent output program that uses fewer resources.

and GCC has a lot of optimization options.

I'd like to study the generated assembly (the one -S gives) after each optimization GCC performs when compiling with different flags like -O1, -O2, -O3, etc.

How can I do this?

Edit: My input will be C code.

like image 279
Dogbert Avatar asked Mar 05 '13 14:03

Dogbert


People also ask

Does GCC optimize assembly?

No. GCC passes your assembly source through the preprocessor and then to the assembler. At no time are any optimisations performed.

Does GCC optimize code?

GCC has a range of optimization levels, plus individual options to enable or disable particular optimizations. The overall compiler optimization level is controlled by the command line option -On, where n is the required optimization level, as follows: -O0 .

Does GCC compile to assembly or machine code?

Writing and Compiling Assembly Code Programmers can write their own assembly code by hand and compile it with gcc into a binary executable program. For example, to implement a function in assembly, add code to a . s file and use gcc to compile it.

What is #pragma GCC optimize Ofast?

#pragma GCC optimize ("Ofast") will make GCC auto-vectorize for loops and optimizes floating points better (assumes associativity and turns off denormals). #pragma GCC target ("avx,avx2") can double performance of vectorized code, but causes crashes on old machines.


1 Answers

gcc -S (Capital S)

gives asm output, but the assembler can change things so I prefer to just make an object

gcc -c -o myfile.o myfile.c

then disassemble

objdump -D myfile.o

Understand that this is not linked so external branch destinations and other external addresses will have a placeholder instead of a real number. If you want to see the optimizations compile with no optimizations (-O0) then compile with -O1 then -O2 and -O3 and see what if anything changes. There are other optimzation flags as well you can play with. To see the difference you need to compile with and without the flags and compare the differences yourself.

diff won't work, you will see why (register allocation changes).

like image 82
old_timer Avatar answered Oct 25 '22 02:10

old_timer