Optimization with inline assembly

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I've found that the attached code behaves differently when compiling with or without -O in GCC 7.2.

$ gcc asm.cpp ; ./a.out
1
$ gcc -O asm.cpp ; ./a.out
0

Looking at the compiled program in the second case, GCC seems to omit the inline assembly:

00000000004004e7 <f(long*)>:
  4004e7:	f3 c3                	repz retq

My understanding is that the "memory" cobbler should tell GCC to assume that the assembly changes the memory (which it does). Am I missing something here?

Best regards,
Marcel

#include <stdio.h>

void f(long* z)
{
    long res;
    asm(
            "addl $1,(%1)"
            : "=r"(res)
            : "rdi"(z)
            : "memory"
    );
}

int main()
{
    long z[] = {0};
    for (int i = 0; i < 1; i++)
    {
        f(z);
    }
    printf("%d\n", z[0]);
}

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux