<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/62060>62060</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[Wrong Code][inline-asm] output operands overlap with output
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
youngbe
</td>
</tr>
</table>
<pre>
For code:
```c
#include <stdint.h>
struct Test
{
uint64_t var1;
uint64_t var2;
};
void ppp(struct Test *tt)
{
__asm__ volatile (""::"a"(tt):"rdi");
uint64_t temp;
__asm__ volatile (
"movq $0, %0\n\t"
"movq $1, %1"
:"=r"(temp), "=m"(tt->var2)
:
:);
}
```
After compile:
```as
ppp: # @ppp
movq %rdi, %rax
movq $0, %rax
movq $1, 8(%rax) # %rax overlap, abort
retq
```
clang version is 16.0.0.
gcc works properly.
According to gcc's doc, an output operand will only overlap to input operands:
> Use the ‘&’ constraint modifier (see [Modifiers](https://gcc.gnu.org/onlinedocs/gcc/Modifiers.html#Modifiers)) on all output operands that must not overlap an input. Otherwise, GCC may allocate the output operand in the same register as an unrelated input operand, on the assumption that the assembler code consumes its inputs before producing outputs. This assumption may be false if the assembler code actually consists of more than one instruction.
>
> The same problem can occur if one output parameter (a) allows a register constraint and another output parameter (b) allows a memory constraint. The code generated by GCC to access the memory address in b can contain registers which might be shared by a, and GCC considers those registers to be inputs to the asm. As above, GCC assumes that such input registers are consumed before any outputs are written. This assumption may result in incorrect behavior if the asm statement writes to a before using b. Combining the ‘&’ modifier with the register constraint on a ensures that modifying a does not affect the address referenced by b. Otherwise, the location of b is undefined if a is modified before using b.
from https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html .
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVk1v67gO_TXKhqjhyPlcZJEmzVs9vM19mGUhS0ysGUvKlejk5t8PKDtfnfQCExiuLYqH5OERa5WSPX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