[LLVMbugs] [Bug 7569] New: clang optimizer produces bad instruction -O1 but not at -O0

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Mon Jul 5 08:45:00 PDT 2010


http://llvm.org/bugs/show_bug.cgi?id=7569

           Summary: clang optimizer produces bad instruction -O1 but not
                    at -O0
           Product: clang
           Version: trunk
          Platform: Macintosh
        OS/Version: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
        AssignedTo: unassignedclangbugs at nondot.org
        ReportedBy: sean at rogue-research.com
                CC: llvmbugs at cs.uiuc.edu


$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.6.2
BuildVersion:   10C2234

$ clang --version
clang version 2.8 (trunk 107463)
Target: x86_64-apple-darwin10
Thread model: posix

$ cat crash.c
int main() {
    *(int*)0 = 0;
    return 0;
}
-------------------------------------------------
$ gcc crash.c -O0 -o crash-gcc-O0
$ gcc crash.c -O0 -o crash-gcc-O0.s -S
$ gcc crash.c -O1 -o crash-gcc-O1
$ gcc crash.c -O1 -o crash-gcc-O1.s -S
$ ./crash-gcc-O0
[1]    23639 segmentation fault  ./crash-gcc-O0
$ ./crash-gcc-O1
[1]    23641 segmentation fault  ./crash-gcc-O1
$ diff crash-gcc-O0.s crash-gcc-O1.s
9,10c9
<       movl    $0, %eax
<       movl    $0, (%rax)
---
>       movl    $0, 0
-------------------------------------------------
$ clang crash.c -O0 -o crash-clang-O0
$ clang crash.c -O0 -o crash-clang-O0.s -S
$ clang crash.c -O1 -o crash-clang-O1
$ clang crash.c -O1 -o crash-clang-O1.s -S
$ ./crash-clang-O0
[1]    23642 segmentation fault  ./crash-clang-O0
$ ./crash-clang-O1
[1]    23644 illegal hardware instruction  ./crash-clang-O1
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ???

The optimizer seems to get confused and produces
a bad instruction:

$ diff crash-clang-O0.s crash-clang-O1.s
11,16c11
<       movl    $0, -4(%rbp)
<       movabsq $0, %rax
<       movl    $0, (%rax)
<       xorl    %eax, %eax
<       popq    %rbp
<       ret
---
>       ud2
        ^^^ Undefined Instruction!

Interestingly, changing the bad "pointer" value to '1'
fixes it:

$ cat crash.c
int main() {
    *(int*)1 = 0;
    return 0;
}

$ clang crash.c -O0 -o crash-clang-O0
$ clang crash.c -O0 -o crash-clang-O0.s -S
$ clang crash.c -O1 -o crash-clang-O1
$ clang crash.c -O1 -o crash-clang-O1.s -S
$ ./crash-clang-O0
[1]    23726 segmentation fault  ./crash-clang-O0
$ ./crash-clang-O1
[1]    23728 segmentation fault  ./crash-clang-O1
$ diff crash-clang-O0.s crash-clang-O1.s
11,13c11
<       movl    $0, -4(%rbp)
<       movabsq $1, %rax
<       movl    $0, (%rax)
---
>       movl    $0, 1

-- 
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.



More information about the llvm-bugs mailing list