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

           Summary: wrong code in union access
           Product: clang
           Version: trunk
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
        AssignedTo: [email protected]
        ReportedBy: [email protected]
                CC: [email protected]


The test case below (plain C) is miscompiled by clang when using -O2.

clang version 3.0 (trunk 131747)
Target: x86_64-unknown-linux-gnu

typedef union {
    unsigned long long dword[2];
    unsigned short half[8];
} vector_t;

void vrlh(vector_t *va, vector_t *vb, vector_t *vd)
{
    vector_t t;
    for (int i = 0; i < 8; i++) {
        int sh = vb->half[7 - i] & 0xf;
        t.half[7 - i] = va->half[7 - i] << sh | va->half[7 - i] >> (16 - sh);
    }
    vd->dword[1] = t.dword[1];        // These lines will be
    vd->dword[0] = t.dword[0];        // ignored by the compiler.
}

I presume that clang should work like gcc with respects to unions (setting one
member and reading another defined in the punning way).

As you can see, the two last lines seem to have disappeared:

0000000000000000 <vrlh>:
   0:   ba 07 00 00 00          mov    $0x7,%edx
   5:   66 66 2e 0f 1f 84 00    nopw   %cs:0x0(%rax,%rax,1)
   c:   00 00 00 00 
  10:   0f b7 04 57             movzwl (%rdi,%rdx,2),%eax
  14:   44 0f b7 04 56          movzwl (%rsi,%rdx,2),%r8d
  19:   41 83 e0 0f             and    $0xf,%r8d
  1d:   44 88 c1                mov    %r8b,%cl
  20:   41 89 c1                mov    %eax,%r9d
  23:   41 d3 e1                shl    %cl,%r9d
  26:   b9 10 00 00 00          mov    $0x10,%ecx
  2b:   44 29 c1                sub    %r8d,%ecx
  2e:   d3 e8                   shr    %cl,%eax
  30:   44 09 c8                or     %r9d,%eax
  33:   66 89 44 54 f0          mov    %ax,-0x10(%rsp,%rdx,2)
  38:   48 85 d2                test   %rdx,%rdx
  3b:   48 8d 52 ff             lea    -0x1(%rdx),%rdx
  3f:   75 cf                   jne    10 <vrlh+0x10>
  41:   c3                      retq

-- 
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.
_______________________________________________
LLVMbugs mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/llvmbugs

Reply via email to