Get optimized source code from GCC

I have a task to create optimized C ++ source code and pass it to a friend for compilation. This means that I do not control the final compilation, I just write the source code of the program in C ++.

I know that a can do optimization at compile time with the -O1 (and -O2 and others) GCC options. But how can I get this optimized source code instead of a compiled program? I can’t configure my friend’s compiler options, so I need to make a good source on my side.

+4
source share
3 answers

You can set the GCC to dump its internal (Gimple, ...) representation , at different "stages". The middle end of GCC consists of hundreds of passes, and you can ask GCC to reset them with arguments , such as -fdump-tree-all or -fdump-gimple-all ; that you can get hundreds of dump files for one compilation!

However, the internal views of GCC are rather low, and you should not expect to understand them without reading a lot of material.

The dump options that I mention are mostly useful for those who work inside GCC, or extend it to plugins encoded in C or extensions encoded in MELT (a high-level domain language for the GCC extension). I'm not sure that they will be very useful for your friend. However, they can be useful for you to understand that optimization transitions do a lot of complex processing.

And do not forget that premature optimization is evil : you must first run your program correctly, and then check and profile it, and finally, optimize several advantages of your efforts. You probably won’t be able to write the right and effective programs without testing and running them yourself before passing them on to your friend.

+3
source

The optimizations performed by GCC are low levels, which means that you will not get the C ++ code again, but the build code is at best. But you cannot convert it or anything like that.

In sum: optimize the source code at the code level rather than at the object level.

+5
source

Easy - choose the best algorithm, let the rest be processed by the optimizer.

Source code optimization is different from binary file optimization. You optimize the source code, the compiler will optimize the binary.

For anything more than choosing an algorithm, you need to do profiling. Of course, there are methods that can speed up the speed of the code, but some make the code less readable. Optimize only when necessary, and after you have measured.

+1
source

Source: https://habr.com/ru/post/1387001/


All Articles