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

前端 未结 8 1779
闹比i
闹比i 2021-02-04 09:00

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

相关标签:
8条回答
  • 2021-02-04 09:35

    Adding the -L option (eg, gcc -L -ahl) may provide slightly more intelligible listings.

    The equivalent MSVC option is /FAcs (and it's a little better because it intersperses the source, machine language, and binary, and includes some helpful comments).


    About one third of my job consists of doing just what you're doing: juggling C code around and then looking at the assembly output to make sure it's been optimized correctly (which is preferred to just writing inline assembly all over the place).

    Game-development blogs and articles can be a good resource for the topic since games are effectively real-time applications in constant memory -- I have some notes on it, so does Mike Acton, and others. I usually like to keep Intel's instruction set reference up in a window while going through listings.

    The most helpful thing is to get a good ground-level understanding of assembly programming generally first -- not because you want to write assembly code, but because having done so makes reading disassembly much easier. I've had a hard time finding a good modern textbook though.

    0 讨论(0)
  • 2021-02-04 09:40

    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!)

    0 讨论(0)
提交回复
热议问题