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

    <tr>
        <th>Summary</th>
        <td>
            Use of slti to reduce code size with compressed branch
        </td>
    </tr>

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

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

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

<pre>
    We currently emit the following for a less than comparison against a constant:
```
        li      a1, 254
        blt     a1, a0, .LBB0_2
```
An alternate sequence we could emit would be:
```
        slti a1, a0, 254
        bnez a1, .LBB0_2
```
bnez is a compressible instruction; none of the others are.  By using the later form, we can save 2 bytes in the case where LBB0_2 is nearby.  If a0 is not otherwise used, this also improves register pressure.  This does lengthen the critical path.  For this to be profitable (code size wise), the constant must fit in the slti encoding.  

To reproduce:
`$ ./llc -O3 -march=riscv64 -mattr=+c < compare.ll`
```
declare void @foo()

define void @bar(i64 %a) {
  %c = icmp slt i64 %a, 255
  br i1 %c, label %taken, label %return
taken:
  call void @foo()
  br label %return
return:
  ret void
}
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx9VNtu2zAM_RrlhUhgy7GzPPihaVCgwIABW7c9DrJM29pkKZPkBOnXj1JuDdAVCHwhj8lzDqk0tj3WPxHk5ByaoI-AowoQBoTOam0PyvT05ECARu8pIQxIO-6EU94aEL1QxgdKS0t3YQIrHli2ZdkDq7Lz7_SarbWii8gZfwReLq_hRodrXGTxuvi82WS_-Lt1HqipDuiMCAge_05oJMKBJNhJtyf6h_TY4AdcvA4K3va8Y2Tw9Zz8iEqCKZ_EjztH_qhGI0RD3CSDsoYVGzDWINgueWrp4ugDhwuAzREmH_2NGU1yXHR6jF2jHDLaiz0Ch-YY0FPZBJTCk1wqg3CiFhkYFK45Us3njuSkiA2nbgdF-MljG-uGIdLV3oIiwnZPZR32ysfeScCUmL1EWGspq9H0VOXc2qmgpNCwE2Eg2BPtRaoYLJlNBWyngogWMP5J2pbmo16JLDFgfH3qj9dFgXGixaEvLsrSSGiatiVTqPxlHPH6YokoNWgneT9VvoQF409aS5h_KWA-CicHVmxpPeW-WsZACI4CjG8ksOLxvL240Pq2D_dzbVFqQsDeqhbYMuusJUFRwhtGLXbK3DCNcIRR1JHxUhAW2GpzQkIMxd5bUHLcRZ1wA8bNKy_AxoHKEzwmtGhQx7cg_qC5izgMkzOnz07ZiylAG6L1_7inFu9WOT_fylAgVTmLXm3f9WrW1kW7LtZiFlTQWH_3adnTLEMcWpwYvF2GMFyPC9IhdcLIYTY5XQ8h7HwkwJ_o1xNwahYETePdX25z2oLfKAO9KtpX9PRQVsW6mA31UlbY5EUui6aoZN7yDld8na3WoqSZZjhLyn3Nyg3jvBGSjGup49fnb4_zHxSKHtNthy6eRGKGMVhuZ6rmGefZKivzT2WV54tq1eSCY5mtq2VTrTqyGkeh9CJyXFjXz1yd6DZT7ymp6ZD5W1LQf0VvEBMVqi-mMFhXkytiRD9Lyuok6x9PRbeF">