【问题标题】:gcc -O2 optimize to wrong behavior, is it a bug?gcc -O2 优化错误行为,这是一个错误吗?
【发布时间】:2016-03-01 07:18:06
【问题描述】:

我编写了一个非常简单的程序来测试 gcc -O2 选项,它会产生错误的行为。是gcc的bug吗?

我的 C 程序:

#include <stdio.h>

int main(int argc, char **argv)
{
    unsigned long i = 0;
    while (1) {
        if (++i > 0x1fffffffUL) {
            printf("hello\n");
            i = 0;
        }
    }
}

当我用 -O1 编译它时,一切正常,i 在每个循环中加 1。但是当我用 -O2 编译它时,我会被忽略,每个循环都会输出“hello”。为什么 -O2 会导致这种错误行为? gcc的明显错误吗?我的 gcc 版本是 4.8.3 for cygwin,我已经尝试了一个新的,它也有同样的问题。

用-O1编译,汇编代码为:

Disassembly of section .text:

    00000000 <_main>:
    #include <stdio.h>

    int main(int argc, char **argv)
    {
       0:   55                      push   %ebp
       1:   89 e5                   mov    %esp,%ebp
       3:   83 e4 f0                and    $0xfffffff0,%esp
       6:   83 ec 10                sub    $0x10,%esp
       9:   e8 00 00 00 00          call   e <_main+0xe>
       e:   b8 00 00 00 20          mov    $0x20000000,%eax
        unsigned long i = 0;
        while (1) {
            if (++i > 0x1fffffffUL) {
      13:   83 e8 01                sub    $0x1,%eax
      16:   75 fb                   jne    13 <_main+0x13>
                printf("hello\n");
      18:   c7 04 24 00 00 00 00    movl   $0x0,(%esp)
      1f:   e8 00 00 00 00          call   24 <_main+0x24>
      24:   eb e8                   jmp    e <_main+0xe>
      26:   90                      nop
      27:   90                      nop

用-O2编译,汇编代码为:

Disassembly of section .text.startup:

00000000 <_main>:
#include <stdio.h>

int main(int argc, char **argv)
{
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 e4 f0                and    $0xfffffff0,%esp
   6:   83 ec 10                sub    $0x10,%esp
   9:   e8 00 00 00 00          call   e <_main+0xe>
   e:   66 90                   xchg   %ax,%ax
  10:   c7 04 24 00 00 00 00    movl   $0x0,(%esp)
  17:   e8 00 00 00 00          call   1c <_main+0x1c>
  1c:   eb f2                   jmp    10 <_main+0x10>
  1e:   90                      nop
  1f:   90                      nop

【问题讨论】:

  • 该变量不是volatile,除了创建无限循环之外,您的程序不会使用它。如果它没有优化掉,我会称之为错误。
  • @Ivan:请避免更改/扩展您的问题。创建一个新的(也许参考这个问题)。我回滚了最后一次添加。
  • 好的。我已将我的新问题添加到 here

标签: c gcc optimization


【解决方案1】:

这个优化是正确的。

在-O1,编译器实质上是将您的函数重写为:

for (;;) {
    for (int i = 0; i < 0x20000000; i++) {
    }
    printf("hello\n");
}

在-O2,正在优化空循环。

【讨论】:

    【解决方案2】:

    对我来说听起来不像是一个错误。当程序的可观察行为是在无限循环中输出“hello\n”时,编译器为什么要生成会浪费时间的代码。你不需要增加一个无用的变量。

    【讨论】:

      【解决方案3】:

      它注意到你没有在循环中做任何其他事情,因此优化它们。你仍然得到相同的输出。这就是优化的重点:只要生成相同的输出,它就会消除内部发生的无用内容。

      编辑:如this post 中所述,如果您想防止空循环被优化掉,您可以在while 循环的主体中包含asm("");,一个空的汇编命令。这使 GCC 无法对其进行优化。

      【讨论】:

      • 感谢您的解释。
      【解决方案4】:

      您期望什么行为?基本上i++ &gt; 0x1FFFFFFFUL 条件将无限次为真,并且每次为真时都会打印“hello”。

      外部可观察的行为是无限次打印“hello”。编译器的优化过于巧妙,只是产生这种效果。

      如果您希望一个空循环实际被循环,您可能不应该告诉编译器进行优化,或者更好地确保您在每次循环运行时都有外部可观察的行为。根据定义,这可以通过 volatile 声明 i 来完成:

      #include <stdio.h>
      
      int main(int argc, char **argv)
      {
          static volatile unsigned long i = 0;
          while (1) {
              if (++i > 0x1fffffffUL) {
                  printf("hello\n");
                  i = 0;
              }
          }
      }
      

      volatile 声明基本上意味着编译器必须生成实际执行对该变量的每次写入的代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-05
        • 2018-09-20
        • 1970-01-01
        • 1970-01-01
        • 2017-08-31
        • 2013-06-30
        • 2017-06-04
        相关资源
        最近更新 更多