[cfe-dev] Left-over empty loop after optimization

rico rico.tilgner at gmail.com
Sat Nov 9 07:16:45 PST 2013


Hi,

Compiling the following program with clang3.3 and -O3 --std=c++11 correctly
determines that the whole if(), the bool[], and the inner for() is without
any effect, but still leaves the (now empty) outer for() in place.

*prog.cpp:*
int main(int argc, char *argv[]) {
  for (int j = 0; j < 0x7fffffff; j++) {
    for (int i = 0; i <= 2; i++) {
      bool arr[] { false, false, false };
      if (arr[i]) {
        arr[i] = true;
      }
    }
  }
  return 0;
}

*prog.s:*
    .file   "prog.cpp"
    .text
    .globl  main
    .align  16, 0x90
    .type   main, at function
main:                                   # @main
    .cfi_startproc
# BB#0:
    movl    $2147483647, %eax       # imm = 0x7FFFFFFF
    .align  16, 0x90
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
    decl    %eax
    jne .LBB0_1
# BB#2:
    xorl    %eax, %eax
    ret 
.Ltmp0:
    .size   main, .Ltmp0-main
    .cfi_endproc


    .section    ".note.GNU-stack","", at progbits

Why is Clang not removing the empty loop? e.g. GCC 4.8.2 does so, even in a
lot more complicated situations.

Thanks,
Rico



--
View this message in context: http://clang-developers.42468.n3.nabble.com/Left-over-empty-loop-after-optimization-tp4035795.html
Sent from the Clang Developers mailing list archive at Nabble.com.



More information about the cfe-dev mailing list