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

前端 未结 8 1933
闹比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: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!)

提交回复
热议问题