[LLVMdev] interesting possible compiler bug

Nick Lewycky nlewycky at google.com
Mon Oct 1 19:40:58 PDT 2012


On 1 October 2012 18:34, reed kotler <rkotler at mips.com> wrote:

>  This code is essentially from an LTP test ( http://ltp.sourceforge.net/).
>
>
> #include <stdlib.h>
>
> int main() {
>   void  *curr;
>
>   do {
>     curr = malloc(1);
>   } while (curr);
>
>   return 0;
>
> }
>
> If you compile it with no optimization, it will keep the malloc calls.
>
> If you compile it with -O2, it will create an infinite loop, i.e. assuming
> that malloc always returns a non zero result and the result is not used.
>

As far as I know, this optimization is legal. Fix the test with a volatile
pointer:

int main() {
  volatile char *curr;

  do {
    curr = malloc(1);
    int i = *curr;
    (void)i;
  } while (curr);

  return 0;
}

which produces:

        pushq   %rax
.Ltmp1:
        movl    $1, %edi
        callq   malloc
        movb    (%rax), %cl
        testq   %rax, %rax
        jne     .LBB0_1
# BB#2:                                 # %do.end
        xorl    %eax, %eax
        popq    %rdx
        ret

Oh how people don't appreciate the luxury of having an infinite memory
machine!

Nick

~/llvmpb/install/bin/clang loop.c -O2 -S
>
>     .file    "loop.c"
>     .text
>     .globl    main
>     .align    16, 0x90
>     .type    main, at function
> main:                                   # @main
>     .cfi_startproc
> # BB#0:                                 # %entry
>     .align    16, 0x90
> .LBB0_1:                                # %do.body
>                                         # =>This Inner Loop Header: Depth=1
>     jmp    .LBB0_1
> .Ltmp0:
>     .size    main, .Ltmp0-main
>     .cfi_endproc
>
>
>     .section    ".note.GNU-stack","", at progbits
>
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20121001/c986b9bf/attachment.html>


More information about the llvm-dev mailing list