我最近遇到了很多的功能,用GCC产生在x86非常糟糕code。他们都适合的模式:
I've lately encountered a lot of functions where gcc generates really bad code on x86. They all fit a pattern of:
if (some_condition) { /* do something really simple and return */ } else { /* something complex that needs lots of registers */ }想简单的情况下如此之小,工作的一半以上都花在推动和弹出不会在所有修改寄存器东西。如果我手写了ASM,我会保存和恢复保存-跨调用寄存器中的复杂情况下的内部,避免在简单情况下,在所有接触堆栈指针。
Think of simple case as something so small that half or more of the work is spent pushing and popping registers that won't be modified at all. If I were writing the asm by hand, I would save and restore the saved-across-calls registers inside the complex case, and avoid touching the stack pointer at all in the simple case.
有没有什么办法让GCC是一个有点聪明,这样做本身? preferably命令行选项,而不是难看黑客源...
Is there any way to get gcc to be a little bit smarter and do this itself? Preferably with command line options rather than ugly hacks in the source...
编辑:以使其具体,这里的东西很接近一些我处理的功能:
To make it concrete, here's something very close to some of the functions I'm dealing with:
if (buf->pos < buf->end) { return *buf->pos++; } else { /* fill buffer */ }和另一个问题:
if (!initialized) { /* complex initialization procedure */ } return &initialized_object;和另:
if (mutex->type == SIMPLE) { return atomic_swap(&mutex->lock, 1); } else { /* deal with ownership, etc. */ }编辑2:我应该提到以开始:这些功能不能被内联。他们有外部链接,他们是库code。使他们能够在应用程序中会导致各种问题进行内联。
Edit 2: I should have mentioned to begin with: these functions cannot be inlined. They have external linkage and they're library code. Allowing them to be inlined in the application would result in all kinds of problems.
推荐答案也许升级GCC的版本? 4.6刚刚发布。据我了解,它具有偏内联的可能性。也就是说,一个功能的易于结合外部部分是内联和昂贵的部分被转换成一个呼叫。但我必须承认,我没有尝试它自己,但
Perhaps upgrade your version of gcc? 4.6 has just been released. As far as I understand, it has the possibility of "partial inline". That is, an easily integratable outer part of a function is inlined and the expensive part is transformed into a call. But I have to admit that I didn't try it myself, yet.
编辑:我是从更新日志所指的语句:
The statement I was referring to from the ChangeLog:
部分内联和 默认情况下,在-O2和更大的功能。 该功能可通过控制 -fpartial内联。
Partial inlining is now supported and enabled by default at -O2 and greater. The feature can be controlled via -fpartial-inlining.
部分内联函数分裂与 短期热点路径返回。这使得 炎热的更积极的内联 路径的领先以更好的性能和 常,以减少code尺寸(因为 功能冷部分不 重复)。
Partial inlining splits functions with short hot path to return. This allows more aggressive inlining of the hot path leading to better performance and often to code size reductions (because cold parts of functions are not duplicated).
...
优化大小时,内联 (无论是在一个节目的寒冷地区 或者-Os编译时)被 改进,更好地处理C ++程序 较大的抽象点球, 导致更小,更快code。
Inlining when optimizing for size (either in cold regions of a program or when compiling with -Os) was improved to better handle C++ programs with larger abstraction penalty, leading to smaller and faster code.
更多推荐
避免GCC函数序言开销?
发布评论