<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/93898>93898</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Bad x86-64 codegen in switch statement with -O0 and -mllvm -x86-speculative-load-hardening causes Segmentation fault
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          samuel-lee-msft
      </td>
    </tr>
</table>

<pre>
    Simple repro is here, with a switch statement with >=5 cases:
https://godbolt.org/z/9ozTKGP48

Clang-11 compiles this fine, but since Clang-12.0.0 until at least Clang-18.1.0 there is some confusion in the compiler.

In the code associated with the case statements, an 8-byte value is "reloaded" from `rbp - 56`.
Unfortunately, this is not a location to which a register has previously been spilled!

This then causes `cmp rdx, offset .LBB0_x` to be not equal, and `0xffff800000000000` is or-ed into rsp in the epilogue of the function, generating a Segmentation fault (I believe this is supposed to only or in a non-zero value non-architecturally as part of speculative load hardening).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJxsVM1u4zYQfhr6MpBAUZZXPuiw2dTFogVaYLfngiJHEguKVDlDJ87TF5STTdDWBwvUkN_fcKSJ3BwQB9E9iO7xoDMvMQ2k14y-8ojVShMfxmhvwze3bh4h4ZYiOIIFEwr1BZ4cL6CBnhybBYg144qB7-9F-5NoHzswmpBE-1nIRyE_L8zbvlIXoS5ztGP0XMc0C3V5Eepyji_ff_n592N_337__-J1mKumARPXzXkk4MURTC7sMsbMQC4YhNeNqpa1hBzYedAMHjXxW62vm1oCFwvFCsUVwcQwZXIxgAul9MaT6o8qvr7VLIImisZpRns3uxc04XsIVJTpAH013hjhqn3eCYVSCX3UFq1QCqYUVxAnmcYNKuhO4iRfSf8IU0ycg2b0twK2e3YEITJo8NFoLpI5wtPiTGlEwtkRY4JFE2wJry5m8jcYEQPQ5rwvpM1HU98LKC8YwOhMSEWLWTdI9rlwxmkiZKh_fXiQfz6Lkyx0I-4a8O-s_d2lLcfk8zRNUy_ff2W_I4ipQgsucIRE21vGuDkf54wQp3095WCKoYI4Y8Ck2YUZNHzDuQR6dzvp7BmE6r_CiN7hFX_kQnnbIqEtEmPwN4ipcGkIMVQvmOJrE8pSJ7M4RsM5ae9vUPLSiYsW2tBkr9ldEUqfYNHJYnBhFupcH-zQ2nN71gccmk_Np6ZRXdcflqE7Hbtj12o5NV17lHZq7Nh3DbZG2v5s7MENSqqj7Fqp2qNq-_qou86epVWnzti-l-IocdXO195f1zISB0eUcTi3_bk_eD2ip31alQr4BHtRKFWGNw3lTDXmmcRRekdM7yjs2OPwoC0896fqdNxv8Iz7Zf__0a1-k3tTq7WAQFXOfYilKrFUP2J5uzn_7dMhJz_8a-IdL3msTVyFuhT010e1pfgXGhbqshuj8i3YjV8H9U8AAAD__6gai8o">