<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/122648>122648</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
LLVM 19.1.6 [[clang::preserve_most]] targeting x86-32 can clobbers result register
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Nick-Kooij
</td>
</tr>
</table>
<pre>
Consider the following function, attributed with `[[clang::preserve_most]]`:
````
extern void bar( int* );
[[clang::preserve_most]] [[clang::noinline]] int foo( int x ) {
bar( &x );
return x * x;
}
````
When targeting x86-32, this emits the following incorrect code, which saves and restores the result register (EAX) breaking the return value:
````
<int foo(int)>:
push edx
push ecx
push eax ; RESULT REGISTER SAVED
push edi
push esi
push edx
push ecx
push ebp
push edi
push esi
push ebp
push esp
push ebx
lea eax, [esp + 0x38]
push eax
call <L0>
IMAGE_REL_I386_REL32 ?bar@@YAXPAH@Z
<L0>:
add esp, 0x4
mov eax, dword ptr [esp + 0x38]
imul eax, eax
pop ebx
pop esp
pop ebp
pop esi
pop edi
pop ebp
pop ecx
pop edx
pop esi
pop edi
pop eax ; RESULT CLOBBERED
pop ecx
pop edx
ret
````
The following attached project, should print "64" and exit. x86-32 builds instead print the arbitrary value of whatever was in the EAX register when bar was called.
*Note:* I'm using LLVM through clang-cl under Visual Studio, bug I suspect the issue is more fundamental than the compiler driver.
## Steps to reproduce:
* Obtained [LLVM-19.1.6-win64.exe](https://github.com/llvm/llvm-project/releases/download/llvmorg-19.1.6/LLVM-19.1.6-win64.exe) from the official Assets.
* Install on **Windows 11** with **Visual Studio 2022 17.12.3**
* Must build a *x86-32* target
* x64 targets link and run properly
* [PreserveMostBug.zip](https://github.com/user-attachments/files/18391006/PreserveMostBug.zip)
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysVttu2zgTfhr6ZhCDomTZuvCFnNj9gz9pi6Tbdv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