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

    <tr>
        <th>Summary</th>
        <td>
            Branch address should be loaded earlier for indirect branch, including switch statement
        </td>
    </tr>

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

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

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

<pre>
    C code:

  <computation>
  (*actionfn)();

This generates (in x64 architecture on clang 12)

  <computation>
  call [actionfn]

In many (probably most) situations, the branch predictor will not guess the branch address correctly.  With the address loaded at the time of the call, the misprediction will not be detected until the load of actionfn has finished.  That will be several cycles of wasted execution.  It would be better to generate

  mov reg,[actionfn]
  <computation>
  call reg

to start the pipeline break sooner.

The code for switch statements exhibits similarly poor scheduling: it loads the jump address just before it jumps to it; it would be better to fill the time after the load with other work, to reduce the time spent executing after a misprediction.




</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyNVMtu2zAQ_BrpQtRQKMuRDjrk0SK5FgV6psiVtAlFCnzE8d93ydiOXQRoAYOmSO7scGakwapD_8CkVVDUd0X1WFTHkbGifpB2WWMQAa0p6u_nDd4W_E7ItDyagnd5oSvq-0uAXzN6NoEBJwL4VISGve-2TDg5YwAZogNmDZNamInd8ATxfwSk0JoVzf2ZQvN4Wfls2CLMIbVcnR3EoA9ssT5QA-YxxAznC_7AwgxscMLIma0OFMpgHdsjoRsb2BTB-8szQimXlqR1jvjrw4ax3xjmfOa0qa1QoJgIeTXgQpcc8zzRPnVd0B87EpfPlgMwBUkbQogmoM6HE2QCOd2XzcKzEQ36GRRx-DVTt4xB9R7eSHLN5EFq0p3K9sInPHgHGRMAVTzTeRu1SgUDhACOBXt269qGxb4xBxMx_0ryf9mUKi_gqIsPwn2Is-IKGk3SF8Qr89ZS_811iCCHk41kjN9jIBeoPsACJni60owD0sTjglo4Mnq16aQkYSJBT5RqhiEr-GHlS1zWs1cv0SfNCRzSqbTnkxAYKMxp5QuRxqTz2Vox5uWTSfuUBkuPFCPrXrPbljRQUcJnkV-J_ckPyv4HiLgOxZUOl2MJ_c2u6ba3N-3utlR9rbq6E2XAoKG_v06qn08XOMYSSCSkZklPNApTjo_xTmTRSB1V4vS32GV0up9DWH36UPAf9JvosnHYkPn0oPXb6e8bvXUvhEuP6D29RDRpdm3blXPfDo0YVdOCGpsRQNZK7UANdSWG6nar2lKLAbTvKWoF5wb2LEPQnBJXYs8rzqs2DXzLdxsutt3Am6q7aUmOeiy2FSwC9Sbx2Fg3la7PlIY4edrU6IP_3BTe42QAcjvCFzHM1vVPYNzh6SfKuczd-8z-D9Ogtto">