How to see reformulation of C ++ code compiler with optimization

I am wondering if / as you can see how the compiler reformulates a piece of code with clang ++ / g ++ when enabling optimizations. I know that the Intel compiler has a flag to generate the corresponding output, but I can not find the equivalent in other compilers.

+6
c ++ compiler-optimization g ++ clang
source share
3 answers

So, thanks to your instructions, I was able to find something really cool, so I decided to share with you:

With Clang ++ - 4.0, you can compile an executable file as follows:

clang++-4.0 -std=c++14 -O3 -fsave-optimization-record -foptimization-record-file=myOptfile.yaml sourceFile.cpp 

This saves a record of successful and unsuccessful optimizations in myOptfile.yaml. This can be viewed with llvm-opt-report-4.0, but its true power will be displayed using llvm / utils / opt-viewer.py.

To do this, clone the llvm repository, navigate to the source directory and run the following after creating myOptFile.yaml:

 python ~/myInstallDir/llvm/utils/opt-viewer/opt-viewer.py myOptFile.yaml reportsDirectory/ 

This will create many html files that can be moved using index.html (in the reportsDirectory folder).

The result looks amazing and looks like this:

enter image description here

Most things are viewable, so you can navigate html hyperlinks to other parts of the source code, including the C ++ libraries, and see what happened!

+4
source share

View assembler results

Here is a website to view this side by side and the coloring function: Compiler Explorer
You can compare more than one compiler (with version) at a time.

GCC: How to save assembler code generated by GCC

Clang is designed as a replacement for GCC, so the same options work.
-S prints assembler code to a .s file

-O3 - optimization level 3

Examples:

 clang -S test.cpp -o test_clang_O3.s g++ -S test.cpp -o test_gcc_O3.s clang -O3 -S test.cpp -o test_clang_O3.s g++ -O3 -S test.cpp -o test_gcc_O3.s 


View Optimizations

To see gcc optimizations, you need to check the "fdump" and "fopt" switches: GCC Debugging Options
(example: -fopt-info-optimized -> Print information when optimization is successfully applied.)

For Clang, this β€œemits” optimization reports: Clang - Options for creating optimization reports

+3
source share

Compilers do not optimize C ++ code; compilers translate C ++ into a "specific view" defined by the implementation, and optimize it. They then convert it into code that your processor can execute, and possibly optimize it even further.

(Remember: C ++ is an abstraction, your C ++ code defines the semantics of the program. Your computer does not use it as a sequence of actual instructions to execute.)

So, for you there is no "optimized C ++"; you can only view the assembled assembly and compare it with the assembly from the assembly in which you convinced your compiler not to optimize so aggressively. With LLVM, you can take a look at the internal representation, but I know little about it.

+1
source share

All Articles