Analysis of programs and compilers in Compiler Explorer

If you have been programming for a long time, you probably were engaged in disassembling the code and asked the question “Why did the compiler optimize the code in one case, but not in the other?” Usually, to answer a question, you need to do a little research with small pieces of programs. Sometimes you need to compare the results of several compilers with different optimization flags.

If you teach computer disciplines, then it is very useful to bring the assembly code. For example, you told your students how variables are created on the stack or how the stack unwinds when an exception is thrown. It should be shown that not always arguments are passed through the stack, which means that the stack unwinding mechanism must take this into account for each function. This can be done using the following program:

int bar (int a, int b) {

return a + b;

}

I would like to collect it with different optimization flags and compare the results. A very handy tool for this godbolt.

Here you can see that in this online environment you can:

enter pieces of code (not necessarily the whole program). And get an assembly view for them;
select a compiler (I chose gcc10.2) and set compilation options;
select several compilers at the same time – it is convenient to compare the results;
enter the program code in different languages. Supported by Pascal, Python and a sea of ​​less popular languages ​​like D and Haskell.
simultaneously enter several fragments of source code, for each of which add their own compilers. Sometimes this is convenient – for example, to show how the code optimizer can take into account constness or something like noexcept in C ++.
If there is an entry point in the code (in C ++ the main function), then you can see the result of the code execution. In this, this online environment is not much different from other online compilers, except for the ability to connect popular libraries such as boost, Google Benchmark, Google Test, Doctest, and so on to the project.

In addition, the results obtained can be shared (there is a Share button), while you share not only the source, but also the configured compilers. If you insert results from godbolt into a presentation, then you should add links there as well – it’s convenient.