考虑下面的x86代码示例:
#include <stdlib.h> static int i; static inline __attribute__((always_inline)) test(int x) { asm volatile("mov %1, %0" : "=r"(i): "i"(x)); } int main(void) { test(5); return i; }如果我用以下方式构建它:
gcc -O test.c它建立得很好。
如果我用它构建它(没有优化):
gcc test.c它在汇编阶段失败,因为值'5'不会作为内联函数测试的立即值传播,所以我们失败了约束。
我希望能够编译此代码而无需启用其他非相关优化,以便更轻松地进行调试。
从理论上讲, -O只是一个捷径,可以同时启用一堆GCC优化选项,这在GCC手册中有详细介绍 。 不幸的是,我无法找到能够改变这种行为的特定GCC标志。
有任何想法吗?
澄清 :为了解决任何疑问,代码片段仅仅是一个例子。 除了展示我想要做的事之外,它本身并没有什么意义。 实际的用例涉及到一个自定义处理器的指令,它只能立即作为我试图用C构造包装的参数。 一个宏确实可以做到这一点,但是却忍受着宏的所有常见缺陷,因此我试图避免它。
更新 :对于那些想知道的人,一个宏也不会工作。 看起来内联函数根本不起作用。 例如,这也不起作用:
void foo (void) { int i = 6; asm volatile ("" : : "i" (i)); }我还修正了问题标题以反映这一点。
Consider the following x86 code example:
#include <stdlib.h> static int i; static inline __attribute__((always_inline)) test(int x) { asm volatile("mov %1, %0" : "=r"(i): "i"(x)); } int main(void) { test(5); return i; }If I build it with:
gcc -O test.cIt builds fine.
If I build it with (no optimization):
gcc test.cIt fails during assembly phase, because the value '5' is not propagated as an immediate value to the inline function test, so we fail the constraint.
I wish to be able to compile this code without turning on other non related optimization, in order to make debugging easier.
In theory, -O is simply a short cut to enable a bunch of GCC optimization options at once that are documented in the fine GCC manual. Unfortunately, I was not able to find the specific GCC flag that turns this behavior on.
Any ideas?
Clarification: To relieve any doubt, the code snippet is just an example. It does not make much sense by itself except to show what I am trying to do. The actual use case involves an instruction on a custom processor that can only take an immediate as an argument which I am trying to wrap in a C construct. A macro will indeed do the trick, but suffers from all the usual draw backs of a macro, hence I am trying to avoid it.
Update: For those who wondered, a macro wont work either. It seems the inline function doesn't play a part here at all. E.g., this doesn't work either:
void foo (void) { int i = 6; asm volatile ("" : : "i" (i)); }I also fixed the question title to reflect this.
最满意答案
看起来像-ftree-ter (替换-ftree-ter >正常传递中的临时表达式 - 不管那是什么)是否-ftree-ter :
gcc -ftree-ter test.c # no errors这是我如何确定的:
gcc -Q --help=optimizers告诉您默认启用/禁用哪些优化(某些已启用) gcc -O -Q --help=optimizers告诉你为-O启用/禁用了哪些优化 将这些命令的输出重定向到文件并进行区分。 尝试仅在指定-O时才启用的优化,直到一个工作。Looks like -ftree-ter (Replace temporary expressions in the SSA->normal pass - whatever that is) does the trick:
gcc -ftree-ter test.c # no errorsHere's how I determined that:
gcc -Q --help=optimizers tells you what optimizations are enabled/disabled by default (some are enabled) gcc -O -Q --help=optimizers tells you what optimizations are enabled/disabled for -O redirect the output of those commands to files and diff them. try the optimizations that are enabled only when -O is specified until one works.更多推荐
发布评论