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

    <tr>
        <th>Summary</th>
        <td>
            RISC-V addressing mode with offset larger than maximum immediate could reuse materialised constant
        </td>
    </tr>

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

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

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

<pre>
    ```llvm
define void @foo(ptr %dest, i64 %i) {
  %p1 = getelementptr i8, ptr %dest, i32 2048
  store i8 1, ptr %p1
  %p2 = getelementptr i8, ptr %dest, i32 2049
  store i8 1, ptr %p2
  %p3 = getelementptr i8, ptr %dest, i32 2050
  store i8 1, ptr %p3
  %p4 = getelementptr i8, ptr %dest, i32 2051
  store i8 1, ptr %p4
  ret void
}
```
produces

```asm
g3:
        addi    a0, a0, 2047
        addi    a1, a0, 1
        li      a2, 1
        sb      a2, 1(a0)
        sb      a2, 1(a1)
        sb      a2, 2(a1)
        sb      a2, 3(a1)
        ret
```

Because 2047 is the largest signed offset that can fit into the immediate. However, we could avoid the second `addi` by changing the offsets:

```asm
g3:
        addi    a0, a0, 2047
        li      a2, 1
        sb      a2, 1(a0)
        sb      a2, 2(a0)
        sb      a2, 3(a0)
        sb      a2, 4(a0)
        ret
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyslN9v2yAQx_8a_HJqhA__fPBD2yjaXjdp72DONpttIsDt-t9P2MmaLmukapMiO_C5-2L4Hie9N_1M1LD8geX7RC5hsK5Ri1IyUVa_NKzg228cnybG94zfa-rMTPBkjQaW8c5ahtUxOGCYa_KB4SOYIotDw7AGVj5siRCnjikwsYeeAo000Rxipqli0p8aAgF5Vp2TfbCOwFSQXgQf00tt_KB2fVsbL7XFx7RzfltbXGpnH9ROb2tnZ-worD5tY1buT3_Opm7Do7N6acmf4NsQ6U-294KJM-S11NrEF4_rbk_kWXnF01ee_objivDtpFcXk1jFnPpdmL4D8RYUV9BR-OuJbM8HauXiad0YGA9hIBil68kHWO-NBtt1ngKEQQZo5QydCWDmYNdYM02kjQy0g0_2mZ7Ixa94JmjtMmqQ6w2KgZ5aO2uIpx2PreCgXqAd5NybuV8jtnX8hQP_w6R_8gFvQXELZlfw2odEN0LXopYJNWlRZSiwzKtkaMqO61qlFVeyE3mqlKxL6uoqk0K2KleJaZCj4HlapBzzvNpVeVZoJYoy55J4VrCM0yTNuIstbWddnxjvF2oKLEWWjFLR6NeGiKhk-4NmzcT9l89fH---McTYJl0TU-_U0nuW8dH44F_FggkjNVs8SK0deR9tnKwmeDZhOBfNWkou1s4Mk_xppmV6LZlTjTiKBTjJQM7I0XjS0NrZBzmHZHFjM4RwXKsCDwwPvQnDonatnRge1oa9ve6Ozn6nNjA8rFv1DA_rbn8FAAD___shqMY">