[LLVMbugs] [Bug 7444] New: llvm can generate incorrect code for switch statement on x86_64

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Mon Jun 21 17:05:42 CDT 2010


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

           Summary: llvm can generate incorrect code for switch statement
                    on x86_64
           Product: new-bugs
           Version: trunk
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Severity: normal
          Priority: P
         Component: new bugs
        AssignedTo: unassignedbugs at nondot.org
        ReportedBy: dimitry at andric.com
                CC: llvmbugs at cs.uiuc.edu


Created an attachment (id=5083)
 --> (http://llvm.org/bugs/attachment.cgi?id=5083)
Test for switch code generation bug

While diagnosing a crash during the building of the file(1) command on
FreeBSD/x86_64, I found that llvm r106479 can apparently generate
incorrect code for a switch statement.

The attached testcase in C outputs the following .ll fragment for the
switch:

  switch i32 %conv, label %sw.default [
    i32 62, label %sw.bb
    i32 60, label %sw.bb
    i32 38, label %sw.bb2
    i32 94, label %sw.bb2
    i32 61, label %sw.bb2
    i32 33, label %sw.bb4
  ]

with an oldish clang/llvm (r104832) this becomes in assembly (only the
first part shown):

        movb    (%rax), %al
        movsbl  %al, %eax
        leal    -33(%rax), %eax
        movl    %eax, %ecx
        cmpl    $61, %eax
        movq    %rcx, -16(%rbp)
        ja      .LBB0_4
        movabsq $2305843009482129440, %rax // bitmask 2000000010000020
        movq    -16(%rbp), %rcx
        btq     %rcx, %rax
        jb      .LBB0_2
        movl    $671088640, %eax
        movq    -16(%rbp), %rcx
        btq     %rcx, %rax
        jb      .LBB0_1
        movl    $1, %eax
        movq    -16(%rbp), %rcx
        btq     %rcx, %rax
        jb      .LBB0_3
        jmp     .LBB0_4

with clang/llvm r106479 it becomes:

        movb    (%rdi), %al
        movsbl  %al, %ecx
        leal    -33(%rcx), %ecx
        movl    %ecx, %edi
        cmpl    $61, %ecx
        movq    %rdi, -16(%rbp)
        ja      .LBB0_4
        movabsq $2305843009482129440, %rax // bitmask 2000000010000020
        movq    -16(%rbp), %rcx
        btq     %rcx, %rax
        jb      .LBB0_2
        movl    $671088640, %eax
        movq    -16(%rbp), %rcx
        btq     %rcx, %rax
        jb      .LBB0_1
        movl    $1, %eax
        movq    -16(%rbp), %rcx
        movl    %ecx, %edx    // suddenly it uses ecx here?
        btl     %edx, %eax    // and btl instead of btq?
        jb      .LBB0_3
        jmp     .LBB0_4

The end result is that it jumps to an incorrect case label.  This is
ONLY the case on x86_64, though; i386 works fine.

-- 
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 LLVMbugs mailing list