llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.82k stars 11.91k forks source link

clang-cl --target=aarch64-pc-windows-msvc generates unnecessary stack read/write instructions #88012

Open mcfi opened 6 months ago

mcfi commented 6 months ago

Build the following with clang18.1.1 with this command clang-cl c.cpp --target=aarch64-pc-windows-msvc /c /O2 /FA.

#include <windows.h>

extern "C"
int
WINAPI
CompareStringExStub(
    __in_opt LPCWSTR lpLocaleName,
    __in DWORD dwCmpFlags,
    __in_ecount(cchCount1) LPCWCH lpString1,
    __in int cchCount1,
    __in_ecount(cchCount2) LPCWCH lpString2,
    __in int cchCount2,
    __in_opt LPNLSVERSIONINFO lpVersionInformation,
    __in_opt LPVOID lpReserved,
    __in_opt LPARAM lParam
)
{
    return CompareStringEx(
                         lpLocaleName,
                         dwCmpFlags,
                         lpString1,
                         cchCount1,
                         lpString2,
                         cchCount2,
                         lpVersionInformation,
                         lpReserved,
                         lParam
                         );
}

In the generated code, see below, ldr x9, [sp] and str x9, [sp] are not needed.

CompareStringExStub:                    // @CompareStringExStub
// %bb.0:
    adrp    x8, __imp_CompareStringEx
    ldr x8, [x8, :lo12:__imp_CompareStringEx]
    ldr x9, [sp]
    str x9, [sp]
    br  x8
llvmbot commented 5 months ago

@llvm/issue-subscribers-backend-aarch64

Author: Ben Niu (mcfi)

Build the following with clang18.1.1 with this command `clang-cl c.cpp --target=aarch64-pc-windows-msvc /c /O2 /FA`. ``` #include <windows.h> extern "C" int WINAPI CompareStringExStub( __in_opt LPCWSTR lpLocaleName, __in DWORD dwCmpFlags, __in_ecount(cchCount1) LPCWCH lpString1, __in int cchCount1, __in_ecount(cchCount2) LPCWCH lpString2, __in int cchCount2, __in_opt LPNLSVERSIONINFO lpVersionInformation, __in_opt LPVOID lpReserved, __in_opt LPARAM lParam ) { return CompareStringEx( lpLocaleName, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2, lpVersionInformation, lpReserved, lParam ); } ``` In the generated code, see below, `ldr x9, [sp]` and `str x9, [sp]` are not needed. ``` CompareStringExStub: // @CompareStringExStub // %bb.0: adrp x8, __imp_CompareStringEx ldr x8, [x8, :lo12:__imp_CompareStringEx] ldr x9, [sp] str x9, [sp] br x8 ```