C编译器优化(gcc) : Automatically Inline a non-static function vs static function

标签 c gcc static

我发现 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/

相关文章:

c - 从管道读取时返回不正确的数据

C: 用户输入字符串包括 "' "-character

c - 逐行读取文件并捕获该行的字

c++如何将类声明为文件的本地类

javascript - Koa.js - 提供静态文件和 REST API

c - 字段的 __attribute__((packed)) 如何影响包含该字段的结构?

c++ - 使用gcc在C中链接C++静态库

gcc - gcc 4.5 中引入的关于链接的变化?

c - 库中函数的 "undefined reference to function"错误

static - 使用 2 个网络服务器(静态/动态)对网站来说是否是一种过度杀伤力?