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

    <tr>
        <th>Summary</th>
        <td>
            [LLVM][RISC-V] Use AUIPC instruction in conjunction with more than one JALR instruction where possible
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:RISC-V,
            llvm:codesize
      </td>
    </tr>

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

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

<pre>
    The sample code in https://godbolt.org/z/ad4qn1xTa has repeated calls to function _foo_ and looking at the assembly generated by LLVM trunk, it seems the address calculation of _foo_ (AUIPC) can be re-used with all the calls to _foo_ (JALR) but the compiler is not able to do that. And this leads to increase in code size which can be avoided.

I found that this is happening because RISC-V uses _PseudoCALL_ which is expanded into the AUIPC and JALR instruction pair very late in the MCCodeEmitter. Here are the [patch](https://github.com/llvm/llvm-project/commit/98f9389f65e630a7243f9c99717009107758d492) which made this change and the llvm-dev [thread](https://lists.llvm.org/pipermail/llvm-dev/2018-July/124542.html) having @asb's comments on late expansion of _PseudoCALL_.

Since @asb says _There may be better ways of structuring the current logic to achieve that aim of course_ and the RISC-V psABI says:

_The AUIPC instruction is used in conjunction with one or more I-Type instructions (add immediate or load) with R_RISCV_PCREL_LO12_I relocations or S-Type instructions (store) with R_RISCV_PCREL_LO12_S relocations._

_Note the compiler emitted instructions for PC-relative symbol addresses are not necessarily sequential or in pairs. There is a constraint is that the instruction with the R_RISCV_PCREL_LO12_I or R_RISCV_PCREL_LO12_S relocation label points to a valid HI20 PC-relative relocation pointing to the symbol._

Does it make sense to revisit the implementation/handling of _PseudoCALL_ and change it so that the above-mentioned code size increase can be avoided? If yes, I can take up this work.

CC: @topperc @jrtc27 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVU9v2z4S_TT0ZRBDpvxPBx8cp0ZduN0gSX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