Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 496 Vote(s) - 3.41 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get Assembly code after every optimization GCC makes?

#1
From [Optimization Compiler](

[To see links please register here]

) 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](

[To see links please register here]

) 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.
Reply

#2
If you would like to study compiler optimization and are agnostic to the compiler, then take a look at the Clang/LLVM projects. Clang is a C compiler that can output LLVM IR and LLVM commands can apply specific optimization passes individually.

Output LLVM IR:

clang test.c -S -emit-llvm -o test.ll

Perform optimization pass:

opt test.ll -<optimization_pass> -S -o test_opt.ll

Compile to assembly:

llc test.ll -o test.s

Reply

#3
Whilst it is possible to take a small piece of code, compile it with `-S` and with a variety of options, the difficulty is understanding what actually changed. It only takes a small change to make code quite different - one variable going into a register means that register is no longer available for something, causing knock-on effects to all of the remaining code in the function.

I was comparing the same code from two nearly identical functions earlier today (to do with a question on C++), and there was ONE difference in the source code. One change in which variable was used for the termination condition inside one for-loop led to over lines of assembler code changing. Because the compiler decided to arrange the registers in a different way, using a different register for one of the main variables, and then everything else changed as a consequence.

I've seen cases where adding a small change to a function moves it from being inlined to not being inlined, which in turn makes big changes to ALL of the code in the program that calls that code.

So, yes, by all means, compile very simple code with different optimisations, and use -S to inspect the compiler generated code. Then compare the different variants to see what effect it has. But unless you are used to reading assembler code, and understand what you are actually looking for, it can often be hard to see the forest for the trees.

It is also worth considering that optimisation steps often work in conjunction - one step allows another step to do its work (inline leads to branch merging, register usage, and so on).
Reply

#4
Compile with switch `-S` to get the assembly code. This should work for any level of optimization.
For instance, to get the assembly code generated in `O2` mode, try:

g++/gcc -S -O2 input.cpp

a corresponding `input.s` will be generated, which contains the assembly code generated. Repeat this for any optimization level you want.
Reply

#5
gcc/clang performs optimizations on the intermediate representations (IR), which can be printed after each optimization pass.

for gcc it is (-fdump-tree-all) 'http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html'
with clang it is (-llvm -print-after-all).

Clang/gcc offers many more options to analyze the optimizations. It is easy to turn on/off an optimization from the command line (

[To see links please register here]

,

[To see links please register here]

)

with clang-llvm you can also list the optimization passes which were performed using the command line option (-mllvm -debug-pass=Structure)
Reply

#6
`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).*
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through