我发现 GCC 编译器使用优化级别 3 内联静态函数,但在一种情况下不内联非静态函数。而在其他情况下,无论函数是静态还是非静态,它都会内联函数。 我想知道静态或非静态函数会选择哪些参数进行内联。
最佳答案
来自 gcc 手册:
-O3 Optimize yet more. -O3 turns on all optimizations specified by -O2 and also turns on the -finline-functions, -funswitch-loops, -fpredictive-commoning, -fgcse-after-reload, -ftree-loop-vectorize, -ftree-loop-distribute-patterns, -fsplit-paths -ftree-slp-vectorize, -fvect-cost-model, -ftree-partial-pre and -fipa-cp-clone options.
看来您的评论来自 -finline-functions 选项:
-finline-functions
Consider all functions for inlining, even if they are not declared inline. The compiler heuristically decides which functions are worth integrating in this way.
If all calls to a given function are integrated, and the function is declared "static", then the function is normally not output as assembler code in its own right.
Enabled at level -O3.
事实上,所有函数都会被 gcc 在 -O3 优化模式下内联,无论声明为内联、静态、两者都不是或两者都声明。
这是 gcc 手册页的另一部分(-Winline 选项):
The compiler uses a variety of heuristics to determine whether or not to inline a function. For example, the compiler takes into account the size of the function being inlined and the amount of inlining that has already been done in the current function.
所以gcc使用函数的大小和函数中内联的数量来选择是否内联它。如果您想了解更多有关这些启发式的信息,恐怕您可能需要查看 gcc 的源代码:)
关于C编译器优化(gcc) : Automatically Inline a non-static function vs static function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40778557/