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

    <tr>
        <th>Summary</th>
        <td>
            [LoongArch] Incorrect register allocation for `{G}CSRXCHG` - rj must not be R0 ro R1
        </td>
    </tr>

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

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

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

<pre>
    In the LoongArch target, the `{G}CSRXCHG` instruction has a special encoding constraint:

If the `rj` operand is assigned to register R0 or R1, the instruction encoding will be interpreted as `{G}CSRRD` or `{G}CSRWR` respectively, not `{G}CSRXCHG`.

Currently, LLVM’s register allocator may assign R0 or R1 to the `rj` operand of a `{G}CSRXCHG` instruction, which leads to incorrect code generation and unintended semantics.

Reproduce case:

```ll
declare i64 @llvm.loongarch.csrxchg.d(i64, i64, i32 immarg);

define dso_local i64 @csrxchg() {
entry:
  %0 = tail call i64 asm sideeffect "", "=r,r,i,{$r4},{$r5},{$r6},{$r7},{$r8},{$r9},{$r10},{$r11},{$r12},{$r13},{$r14},{$r15},{$r16},{$r17},{$r18},{$r19},{$r20},{$r23},{$r24},{$r25},{$r26},{$r27},{$r28},{$r29},{$r30},{$r31},0,~{memory}"(i32 4, i32 0, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i64 0)
  %1 = tail call i64 @llvm.loongarch.csrxchg.d(i64 %0, i64 4, i32 0)
  %2 = tail call i64 asm sideeffect "", "=r,r,i,{$r4},{$r5},{$r6},{$r7},{$r8},{$r9},{$r10},{$r11},{$r12},{$r13},{$r14},{$r15},{$r16},{$r17},{$r18},{$r19},{$r20},{$r23},{$r24},{$r25},{$r26},{$r27},{$r28},{$r29},{$r30},{$r31},0,~{memory}"(i32 4, i32 0, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i64 %1)
  ret i64 %2
}
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsVkFv4zYT_TX0ZRCDHEmWfdAhsb_sFyC9uEDbW0GTI4kBJRoklTSX_vaCsuWEiwB7bxegYQ5FvfdmNIMZGYLpRqKGVQ-sOqzkFHvnm55MT351cvq9eRoh9gTPzo3dvVc9ROk7igz38znbcFY_fGP1Yf_r8Y_9_7-xDQczhugnFY0boZcBJIQzKSMt0KicNmMHyqU70oyRFfeMp_XULoj-JaG4M3k5ajABrjo1RAeeOhMieThycB6OYpHymfXG82ashVN6GMmfPUXSIEMu-3iY6Xx--vsxnXpK0qN5JfueiEYXv_R5ffFhP3lPY7zcfX7-7Rf2P2Rbzna78CFcWuuUjM7DIN-vvt28SS5-GQbXgvxhuBPtW29UD5akDgnMjMp5TyqCcpqgo5G8nIOUUKcxRWbUpCHQIMdoVLj6cqSzd3pSBEoGun2mJGFe1jJ-r0lZ6QnMpgRWcmtfh7VNuSK96tcq-L9U3601w63ZlEnd8lcgmGGQvmO4Y8XDBVtTa0YCHdyfKUR2gb3iMNwy3AGr03Uao3-_qAJgWHFgxQGiNBaUtJdXZRggGE3UtikADHFe-3lXHDzDffoZhvsEiqUvWX34sKrM2mRWnVnbzNplluC5KXITc7PIzVyQyBWJXJLINYlclMhVYa4Kc17MeTHnxZwXc17MeTHnLXLe4hoNznD_N6sfBhqcf5_PMGVNgXBLGL5sxH9jsymTy7slw8UXGf7DmptLY0H7HMkbLP4snJ-F8-_aXNJeLEnuKS5nmBpNffjcx1a6KfSu2MkVNaIu6y0veVGv-kZzsdHVVmyxKmquRKvbE522FeKu5aTblWmQY8UrFLzitajXWvC64oj8pE5YV8hKToM0dj3XqPPdyoQwUSNKvi1xZeWJbJiHL8SR3mB-mr5fdVj5Jr10d5q6kIrchBg-YKKJdp7abnMZqw7wdGv1388aqd233084y_xwB_4FhinEebo5UZpFvIOjWE3eNn2M55D6LD4yfOxM7KfTWrmB4WOSc_27O3v3QioyfJydCAwfr16-NvhPAAAA__-SFba_">