Hi all:

Under studio 12.1, the following code snippet produces unexpected results when 
compiled with optimization in 64-bit mode (code seems to work fine in 32-bit 
mode, or without optimization):

#include <stdio.h>

#define rdtsc() \
        ({ unsigned long long __cycles; __asm__ __volatile__( \
                "rdtsc" \
                : "=A" (__cycles) ); __cycles; })


void
foo(unsigned a, unsigned b, unsigned c, unsigned d)
{
        unsigned long long tsc;

        tsc = rdtsc();
        printf("%d\n", a * b * c * d);
}

int
main(int argc, char **argv)
{
        unsigned a, b, c, d;

        a = b = c = d = 1;
        foo(a, b, c, d);
}


psg-solaris-01$ cc -V -m64 -O -o foo foo.c
cc: Sun C 5.10 SunOS_i386 2009/06/03
acomp: Sun C 5.10 SunOS_i386 Patch 142363-01 2009/07/15
iropt: Sun Compiler Common 12.1 SunOS_i386 Patch 141858-03 2009/09/21
ir2hf: Sun Compiler Common 12.1 SunOS_i386 Patch 141858-03 2009/09/21
ube: Sun Compiler Common 12.1 SunOS_i386 Patch 141858-03 2009/09/21
as: Sun Compiler Common 12.1 SunOS_i386 Patch 141858-03 2009/09/21
ld: Software Generation Utilities - Solaris Link Editors: 5.11-1.1685

psg-solaris-01$ ./foo
964055

It appears that the compiler is not recognizing that %eax/%edx will be 
clobbered in this case.  If I explicitly add those registers to the asm clobber 
list, things work fine:

#define rdtsc() \
        ({ unsigned long long __cycles; __asm__ __volatile__( \
                "rdtsc" \
                : "=A" (__cycles) : : "%eax","%edx" ); __cycles; })

psg-solaris-01$ cc -V -m32 -O -o foo foo.c
cc: Sun C 5.10 SunOS_i386 2009/06/03
acomp: Sun C 5.10 SunOS_i386 Patch 142363-01 2009/07/15
iropt: Sun Compiler Common 12.1 SunOS_i386 Patch 141858-03 2009/09/21
ir2hf: Sun Compiler Common 12.1 SunOS_i386 Patch 141858-03 2009/09/21
ube: Sun Compiler Common 12.1 SunOS_i386 Patch 141858-03 2009/09/21
as: Sun Compiler Common 12.1 SunOS_i386 Patch 141858-03 2009/09/21
ld: Software Generation Utilities - Solaris Link Editors: 5.11-1.1685

psg-solaris-01$ ./foo
1

Is this correct behavior, or is this a bug that should be reported?  My 
understanding of the A constraint is that it is an alias for %eax/%edx.

thanks
Mark


      
_______________________________________________
tools-compilers mailing list
[email protected]

Reply via email to