Nikos Kazazakis
Nikos Kazazakis

Reputation: 792

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

I'm wondering if/how it's possible to see how the compiler reformulates a piece of code with clang++/g++ when optimizations are turned on. I know that the Intel compiler has a flag to produce relevant output, but I can't seem to find the equivalent in the other compilers.

Upvotes: 13

Views: 2750

Answers (3)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

Compilers don't optimise C++ code; compilers translate C++ into an implementation-defined "internal representation" and optimise that. Then they translate it into code that your CPU can execute, and possibly optimise that even further.

(Remember: C++ is an abstraction; your C++ code defines the semantics of a program. Your computer doesn't use it as a series of actual instructions to execute.)

So, there is no "optimised C++" for you to read; you can only look at the resulting assembly and compare it with the assembly from a build in which you persuaded your compiler not to optimise so aggressively. With LLVM you may be able to get a look at the internal representation itself, but I don't know much about that.

Upvotes: 0

Nikos Kazazakis
Nikos Kazazakis

Reputation: 792

So, thanks to your directions I was able to discover something really cool, so I thought I'd share:

With Clang++-4.0, one can compile the executable 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 using llvm-opt-report-4.0, but its true power is if viewed using llvm/utils/opt-viewer.py.

In order to do that, clone the llvm repository, navigate to your source directory, and run the following after you generate myOptFile.yaml:

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

This will create a lot of html files that you can navigate using index.html (in the reportsDirectory folder).

The result is awesome, and looks like this:

enter image description here

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

Upvotes: 15

Robert Andrzejuk
Robert Andrzejuk

Reputation: 5222

View assembler output

Here is a website to view this side by side and function colouring: Compiler explorer
It is possible to compare more than 1 compiler (with version) at a time.

GCC: How to Save the Assembler Code Generated by GCC

Clang is engineered as a replacement for GCC, so the same parameters work.
-S outputs the assembler code into a .s file
-O3 is 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 have to check the "fdump" and "fopt" switches: GCC Debugging-Options
(example: -fopt-info-optimized -> Print information when an optimization is successfully applied. )

For Clang it is "emit" optimizations reports: Clang - Options to Emit Optimization Reports

Upvotes: 4

Related Questions