Inline asm - unexpected optimization

Georg-Johann Lay avr@gjlay.de
Wed Aug 24 10:12:00 GMT 2011


Jeremy Hall schrieb:
> Hi,
> Looking at the assembler for this speculative code, I saw that without
> asm volatile, the second call to rdrand was removed and the value
> "low" used twice.
> gcc 4.6.1 with any optimization turned on.  It looks to me like this
> optimization will stop the code working.
> 
> My question is:  is this my ignorance of inline assembler (most
> likely) or is it a bug in gcc which I should report?

It's not a bug in gcc.

The inlne asm has side effects you didn't report to gcc, so you need the 
volatile here.

> I know its a new instruction.  The retry loop etc is removed to
> simplify the test case.
> 
> #include <stdio.h>
> typedef unsigned long long qword;
> // #define asm __asm__ __volatile__
> 
> int
> main( int argc, char *argv[] )
> {
>   qword result;
> 
>     /* for 32 bit mode call rdrand twice */
>   unsigned int low, high;
>   asm( "rdrand %0" : "=r" (low) );
>   asm( "rdrand %0" : "=r" (high) );
>   result = ((qword)high << 32U) | low;
> 
>   printf("64 bit random number: %llu\n", result );
> }



More information about the Gcc-help mailing list