Verifying compiler optimizations in gcc/g++ by analyzing assembly listings

I just asked a question related to how the compiler optimizes certain C++ code, and I was looking around SO for any questions about how to verify that the compiler has performed certain optimizations. I was trying to look at the assembly listing generated with g++ ( g++ -c -g -O2 -Wa,-ahl=file.s file.c ) to possibly see what is going on under the hood, but the output is too cryptic to me. What techniques do people use to tackle this problem, and are there any good references on how to interpret the assembly listings of optimized code or articles specific to the GCC toolchain that talk about this problem?


GCC's optimization passes work on an intermediary representation of your code in a format called GIMPLE.

Using the -fdump-* family of options, you can ask GCC to output intermediary states of the tree.

For example, feed this to gcc -c -fdump-tree-all -O3

unsigned fib(unsigned n) {
    if (n < 2) return n;
    return fib(n - 2) + fib(n - 1);
}

and watch as it gradually transforms from simple exponential algorithm into a complex polynomial algorithm. (Really!)


A useful technique is to run the code under a good sampling profiler, eg Zoom under Linux or Instruments (with Time Profiler instrument) under Mac OS X. These profilers not only show you the hotspots in your code but also map source code to disassembled object code. Highlighting a source line shows the (not necessarily contiguous) lines of generated code that map to the source line (and vice versa). Online opcode references and optimization tips are a nice bonus.

  • Instruments: developer.apple.com
  • Zoom: www.rotateright.com

  • Not gcc, but when debugging in Visual Studio you have the option to intersperse assembly and source, which gives a good idea of what has been generated for what statement. But sometimes it's not quite aligned correctly.

    The output of the gcc tool chain and objdump -dS isn't at the same granularity. This article on getting gcc to output source and assembly has the same options as you are using.

    链接地址: http://www.djcxy.com/p/80742.html

    上一篇: 怀疑'gdb'下的尾部优化代码

    下一篇: 通过分析汇编列表来验证gcc / g ++中的编译器优化