[LLVMbugs] [Bug 6941] New: miscompilation - partial result optimized away

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Mon Apr 26 08:32:41 PDT 2010


           Summary: miscompilation - partial result optimized away
           Product: libraries
           Version: trunk
          Platform: PC
        OS/Version: FreeBSD
            Status: NEW
          Severity: normal
          Priority: P
         Component: Backend: X86
        AssignedTo: unassignedbugs at nondot.org
        ReportedBy: rdivacky at freebsd.org
                CC: llvmbugs at cs.uiuc.edu

on amd64:

pes ~$ cat buf.c
typedef unsigned int __attribute__((__mode__(__DI__))) u_int64_t;

unsigned int
snd_xbytes(unsigned int v, unsigned int from, unsigned int to)
 unsigned int w, x, y;

 x = from;
 y = to;

 while (y != 0) {
    w = x % y;
    x = y;
    y = w;
                            // [1]
 from /= x;
 to /= x;

 // printf("v = %u, from = %u, to = %u\n", v, from, to); // [2]

 return (unsigned int)(((u_int64_t)v * to) / from);

int main(int argc, char **argv) {
   printf("%u\n", snd_xbytes(atoi(argv[1]), atoi(argv[2]), atoi(argv[3])));

pes ~$ clang -O1 buf.c -o a.out.clang && ./a.out.clang 25050482 2094268276



pes ~$ gcc -O1 buf.c -o a.out.gcc && ./a.out.gcc 25050482 2094268276 1210547630



the problem is that clang -O1 compiles the code starting at [1] to:

        movl    %ecx, %eax
        xorl    %edx, %edx
        divl    %r8d
        movl    %edi, %ecx
        imulq   %rcx, %rax        // [3]
        movl    %esi, %eax        // [4]
        xorl    %edx, %edx
        divl    %r8d
        movl    %eax, %ecx
        xorl    %edx, %edx
        divq    %rcx
        popq    %rbp

the imulq at [3] does basically %rax = %rax * %rcx but the movl at [4] rewrites
the value in %rax which is then never used. Clang -O0 does not exhibit this 
behaviour. When I uncomment the printf at [2] the miscompilation is gone.

I guess this is backend issue as opt -O2 still shows the multiplication as live

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