<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=http://email.email.llvm.org/c/eJzlWEtzm0gQ_jXoMiWKh0DooIMi2Ulqk60km4rtXFwDtIAIGHZmsOz99ds9IAnLsp3XJodVIQlm-vH1k4ZYpHfzZcnrjN2AVIWoWVGzVoHlL5jr247tMMuLilppXpaQsrUUFXsJtRbC8qaKNTzZ8AxYxWv8k5Y3s7wli1vNdA6skSIuoWKFYrXQPd9YNZAU6yJhvE7Za5aItkyZBCRO2wTYttA5S1opkZpp2dYbxhXbQlnahOWlSGNRagKYa90oPLG8czyyJLGzbtMWMsOVf_Abi9u__whkmBI0Z2U5i-53KaqmKAs0nICuRVmKLV0tLe8FHogq7aFYoZOQh_qNccLGmssMNCv8KBwjVyq2it3aSdMgLVvzolR7VpBSSHJqjJ6COiXcRIG-1II1INdCVkzjCkvQxQzKoipqrikUePBuVRWafCw3yFa1ShM96TLSbXaRQ80MSHJ1YkxDSoOBKwWSxCmKDBnbQapAKYocMqCkndBOHV1h8CABdALH2OheFmJqSBYvWVzohCuNvHbv19DpjqTzVL9qYtN7Z7jy9T41XAozIdHsklnTXjLD07NLzAiKrL9fvBEFpunRsjVdHc7NryHLjsnMr4aqKTk63PKX-q6BmlfAPlr-2T0kV0MkWB4aE9oIzXuhe5B7KsZ6uPYxPtqzghd4dG7BnPYX-0AHKzwoCq2sH2AmDavHrNybsgONJhFIMmZPuYvbkPEj5okEqseU0iNus0YUtR73a2NVoGAsYkjtODFZT40Bi7WmIhpwsDEujssywWB2lJhwbVMKnqLoHCQ8rOQtxLaWoIQstI3ZjEslfj99unh5Z3n-2Sy5m36eifCKb9L311-qj-rdu-0Q_UXONct5g7FTpE_TNaU-6r_t8gtyihkQoDdvPr3FRqaQtKZaMf_GX1Q1LAa9Bayvnv9epRA7dbF-D0NkLBywU9Ft8yLJsXw3oAyl6S1FfcPLIt0Vj79MgcSqfZ7hkmorLPo7XHr9gaVt1SAY7BdgdPK1Bmnk3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qlwrxi3Qq5YVvzOvDwglOYR5z-qWdk0M4N1H8BX49xvQ>53271</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Musttail calls do not work when compiling to x86 Windows
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          ngg
      </td>
    </tr>
</table>

<pre>
    Clang version in use: 13.0.0 (installed from Gentoo's package manager), but the problem is not Gentoo-specific and I could reproduce with current trunk as well. (Godbolt: https://gcc.godbolt.org/z/boxqK5r6d)

Compiling the following C++ code with `clang++ -c -target i386-windows x.cpp` fails with `error in backend: failed to perform tail call elimination on a call site marked musttail` error. When clang is compiled with assertions, the error message is `musttail call must precede a ret with an optional bitcast`.
```c++
// x.cpp
// clang++ -c -target i386-windows x.cpp
struct X {
  ~X();
  void f();
};

void g();

template <typename T>
struct Y {
  static void h()
  {
    X().f();
    [[clang::musttail]] return g();
  }
};

template struct Y<void>;
```

The reduced `bugpoint-reduced-simplified.bc` from running `bugpoint -run-llc x.bc` is uploaded here: https://web.tresorit.com/l/VVWGy#E9cy7Z9o6YakdQ_jmTsPPw

What happens is that the `x86-winehstate` LLVM pass inserts instructions between the `musttail call` and the `ret` instructions, which makes the code invalid.
<details>
<summary>IR dump before and after the x86-winehstate pass</summary>

```
*** IR Dump Before Windows 32-bit x86 EH state insertion (x86-winehstate) ***
define dso_local void @"?h@?$Y@X@@SAXXZ"() #0 comdat align 2 personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
  invoke x86_thiscallcc void @"?f@X@@QAEXXZ"()
          to label %1 unwind label %2

1:                                                ; preds = %0
  musttail call void @"?g@@YAXXZ"()
  ret void

2:                                                ; preds = %0
  %3 = cleanuppad within none []
  cleanupret from %3 unwind to caller

*** IR Dump After Windows 32-bit x86 EH state insertion (x86-winehstate) ***
define dso_local void @"?h@?$Y@X@@SAXXZ"() #0 comdat align 2 personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
  %1 = alloca %CXXExceptionRegistration, align 4
  %2 = bitcast %CXXExceptionRegistration* %1 to i8*
  call void @llvm.x86.seh.ehregnode(i8* %2)
  %3 = call i8* @llvm.stacksave()
  %4 = getelementptr inbounds %CXXExceptionRegistration, %CXXExceptionRegistration* %1, i32 0, i32 0
  store i8* %3, i8** %4, align 4
  %5 = getelementptr inbounds %CXXExceptionRegistration, %CXXExceptionRegistration* %1, i32 0, i32 2
  store i32 -1, i32* %5, align 4
  %6 = getelementptr inbounds %CXXExceptionRegistration, %CXXExceptionRegistration* %1, i32 0, i32 1
  %7 = getelementptr inbounds %EHRegistrationNode, %EHRegistrationNode* %6, i32 0, i32 1
  store i8* bitcast (i32 (i8*, i8*, i8*, i8*)* @"__ehhandler$?h@?$Y@X@@SAXXZ" to i8*), i8** %7, align 4
  %8 = load %EHRegistrationNode*, %EHRegistrationNode* addrspace(257)* null, align 4
  %9 = getelementptr inbounds %EHRegistrationNode, %EHRegistrationNode* %6, i32 0, i32 0
  store %EHRegistrationNode* %8, %EHRegistrationNode** %9, align 4
  store %EHRegistrationNode* %6, %EHRegistrationNode* addrspace(257)* null, align 4
  %10 = getelementptr inbounds %CXXExceptionRegistration, %CXXExceptionRegistration* %1, i32 0, i32 2
  store i32 0, i32* %10, align 4
  invoke x86_thiscallcc void @"?f@X@@QAEXXZ"()
          to label %11 unwind label %16

11:                                               ; preds = %0
  %12 = getelementptr inbounds %CXXExceptionRegistration, %CXXExceptionRegistration* %1, i32 0, i32 2
  store i32 -1, i32* %12, align 4
  musttail call void @"?g@@YAXXZ"()
  %13 = getelementptr inbounds %CXXExceptionRegistration, %CXXExceptionRegistration* %1, i32 0, i32 1
  %14 = getelementptr inbounds %EHRegistrationNode, %EHRegistrationNode* %13, i32 0, i32 0
  %15 = load %EHRegistrationNode*, %EHRegistrationNode** %14, align 4
  store %EHRegistrationNode* %15, %EHRegistrationNode* addrspace(257)* null, align 4
  ret void

16:                                               ; preds = %0
  %17 = cleanuppad within none []
  cleanupret from %17 unwind to caller

```
</details>

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzlWEtzm0gQ_jXoMiUKBj0POiiSnaQ22UqyqdjOxTUMLSBCDDszWPb--u0ekIRl2c5rk8OqkAQz_fj6SUOskrvZohBlym5Am1yVLC9ZbcCL5iyM_MAPmMcneWmsKApI2EqrDXsJpVXK42PDKiHXIgW2ESX-aY9PPb5gcW2ZzYBVWsUFbFhuWKlsy9c3Fch8lUsmyoS9ZlLVRcI0IHFSS2Db3GZM1lojNbO6LtdMGLaFovAJy0uVxKqwBDCztjJ44vFzPFIp_bTZ9JVOceUf_Mbq9u8_hnqUELRg6QXz5nehNlVe5Gg4AV2polBbulp4_AUeiCppoXijQJKH2o2-ZH0rdAqW5dFk1EeuRG0Nu_VlVSEtW4m8MHtW0FppcmqMnoIyIdxEgb60ilWgV0pvmMUVJtHFDIp8k5fCUijwEM2qyS35WK-RbVMbS_Sky0n32UUGJXMgydXSmYaUDoMwBjSJMxQZMraBtAFjKHLIgJJ2Qht1dIXBAwnoBIGxsa0sxFSRLFGwOLdSGIu8fuvXUdAcsvFUu-pi03qnu_L1PnVcBjNBWnbJvHErmeHp2SVmBEU22i_eqBzT9GjZGy8P5-7XkaXHZO7XwqYqBDrcixb2roJSbIB99KKze0iuukiwPCwmtBOatUL3IPdUjLVw_WN8tOcNX-DRuAVzOprvAz1c4kFRqHX5ADNpWD5m5d6UHWg0iUCSMXvKXdy6jB8xTzRQPSaUHnGdViovbb9d65scBWMRQ-LH0mU9NQYs1pKKqMPB-rjYLwqJwWwoMeHqqlAiQdEZaHhYyVuIfavBKJ1bH7MZlwr8fvp08fLO49HZVN6NP0_V6Eqsk_fXXzYfzbt32y76i0xYlokKY2dIn6VrSn3Uf9vkF2QUMyBAb958eouNzCBpSbXi_p2_qGpYDHYLWF8t_71KIXbqYu0ehshZ2GGnottmucywfNdgHKXrLXl5I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