消除GOT寄存器的冗余负载? [英] Eliminating redundant loads of GOT register?
问题描述
我正在处理一些代码,这些代码在编译为PIC(与位置无关的代码)时会变慢70-80%,并在寻找缓解该问题的方法.问题的很大一部分是gcc坚持在每个函数中插入以下内容:
I'm dealing with some code that's getting 70-80% slower when compiled as PIC (position independent code), and looking for ways to alleviate the problem. A big part of the problem is that gcc insists on inserting the following in every single function:
call __i686.get_pc_thunk.bx
addl $_GLOBAL_OFFSET_TABLE_,%ebx
即使最后只占函数内容的20%.现在,ebx
是一个保留呼叫的寄存器,并且相关转换单元(源文件)中的每一个函数正在将GOT地址加载到该寄存器中,并且很容易检测到static
不能从翻译单元外部调用函数(从不使用其地址).那么,为什么gcc不能只在大型外部链接函数的开头加载一次ebx
并生成静态链接函数,以使它们假定ebx
已经加载了GOT的地址?我是否可以使用任何优化标志来强制gcc进行明显而大规模的优化,而没有将内联限制提高到很高的水平,从而使所有内容都内联到外部函数中?
even if that ends up being 20% of the content of the function. Now, ebx
is a call-preserved register, and every function in the relevant translation unit (source file) is loading it with the address of the GOT, and it's easily detectable that the static
functions cannot be called from outside the translation unit (their addresses are never taken). So why can't gcc just load ebx
once at the beginning of the big external-linkage functions, and generate the static-linkage functions so that they assume ebx
has already been loaded with the address of the GOT? Is there any optimization flag I can use to force gcc to make this obvious and massive optimization, short of turning the inline limits up sky-high so everything gets inlined into the external functions?
推荐答案
对此可能没有通用的解决方法,但是您可以尝试使用内联选项.我猜想编译单元中的static
函数没有太多的调用者,因此代码复制的开销不会太糟糕.
There is probably no generic cure for this, but you could try to play around with inlining options. I'd guess that static
functions in a compilation unit don't have too many callers, so the overhead in code replication wouldn't be too bad.
使用gcc强制执行此类操作的最简单方法是设置attribute((always_inline))
.您可以使用依赖于gcc的宏来确保可移植性.
The easiest way to force such things with gcc would be to set an attribute((always_inline))
. You could play around with a gcc dependent macro to ensure portability.
如果您不想修改代码(但是无论如何static inline
都可以),您可以使用-finline-limit
选项进行微调.
If you don't want to modify the code (but static inline
would be good anyhow) you could use the -finline-limit
option to fine tune that.
这篇关于消除GOT寄存器的冗余负载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!