How can I indicate that the memory *pointed* to by an inline ASM argument may be used?

0 votes
asked Jun 3, 2019 by beeonrope

Consider the following small function:

void foo(int* iptr) { iptr[10] = 1; __asm__ volatile ("nop"::"r"(iptr):); iptr[10] = 2;

Using gcc, this compiles to:

foo: nop mov DWORD PTR [rdi+40], 2 ret

Note in particular, that the first write to iptr, iptr[10] = 1 doesn't occur at all: the inline asm nop is the first thing in the function, and only the final write of 2 appears (after the ASM call). Apparently the compiler decides that it only needs to provide an up-to-date version of the value of iptr itself, but not the memory it points to.

I can tell the compiler that memory must be up to date with a memory clobber, like so:

void foo(int* iptr) { iptr[10] = 1; __asm__ volatile ("nop"::"r"(iptr):"memory"); iptr[10] = 2;

which results in the expected code:

foo: mov DWORD PTR [rdi+40], 1 nop mov DWORD PTR [rdi+40], 2 ret

However, this is too strong of a condition, since it tells the compiler all memory has to be written. For example, in the following function:

void foo2(int* iptr, long* lptr) { iptr[10] = 1; lptr[20] = 100; __asm__ volatile ("nop"::"r"(iptr):); iptr[10] = 2; lptr[20] = 200;

The desired behavior is to let the compiler optimize away the first write to lptr[20], but not the first write to iptr[10]. The "memory" clobber cannot achieve this because it means both writes have to occur:

foo2: mov DWORD PTR [rdi+40], 1 mov QWORD PTR [rsi+160], 100 ; lptr[10] written unecessarily nop mov DWORD PTR [rdi+40], 2 mov QWORD PTR [rsi+160], 200 ret

Is there some way to tell compilers accepting gcc extended asm syntax that the input to the asm includes the pointer and anything it can point to?

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter