Community
cancel
Showing results for 
Search instead for 
Did you mean: 
hydroxyprolin
Beginner
34 Views

Inlining of functions with GNU-style inline assembly may generate wrong code

Bug report: Inlining of functions returning a 'call-clobbered' 64-bit register variable assigned by the output of GNU-style inline assembly may generate wrong code

Compiler: Intel icc v11.1.056 (flags -O2 -use-asm)
Platform: Linux Intel64 (Debian 5.0, gcc 4.3.2)
[cpp]// Sample code: compile with -O2  -use-asm
#include 

long gnu_asm (void)
{
      // defining a specific register is usually a bad idea, but GNU gcc allows this
      register long regvar __asm__ ("rax");

      __asm__ (
            "      movq $12345, %0 "
            : "=r"(regvar)
      );

      // rax is a 'call-clobbered' register, but there are no function calls between
      // assignment and return statement. Therefore the return value must be 12345.

      return regvar;
}

int main(int argc, char *argv[])
{
      long k;

      k = gnu_asm();

      printf("A printf() function call to clobber the rax register.n");
      printf("Output here should be 12345, but it's the return value of previous printf(): '%ld'n", k);

      return 0;
}
[/cpp]

With -O2 flag the icc compiler inlines gnu_asm() and generates the following code:
[plain]        ...
        movq    $12345, %rax
        movl     $_2__STRING.0, %edi
        xorl      %eax, %eax
        call       printf
        movl     $_2__STRING.1, %edi
        movq    %rax, %rsi
        xorl       %eax, %eax
        call       printf
        ...
[/plain]

The icc compiler generates code like 'k' would be a 'call-clobbered' register variable (in that case it would be legal to overwrite the previous assignment of k by the first printf call). The use of rdi as the register also results in wrong code.
0 Kudos
2 Replies
JenniferJ
Moderator
34 Views

Thank you for reporting here with testcase.

I'll filea bug reportfor this. If I heard anything, I'll let you know.

Jennifer
34 Views

Hi,

I'm working through old open forum posts at the moment. Sorry for answering so late. I tested your example code again with the latest 11.1.075 compiler and it works now. The issue is fixed.

Thanks again for reporting this issue with such a nice testcase!

Alexander