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

    <tr>
        <th>Summary</th>
        <td>
            AMDGPU pass does not respect register class constraint when folding immediate into copies
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:AMDGPU,
            crash-on-valid
      </td>
    </tr>

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

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

<pre>
    ```
# RUN: llc -mtriple=amdgcn -mcpu=gfx90a -run-pass=si-fold-operands -verify-machineinstrs -o - %s

# av_class constraint is not respected, and copy is replaced with an illegal v_mov_b32

---
name: s_mov_b32_imm_literal_copy_s_to_av_32
tracksRegLiveness: true
body: |
  bb.0:
    %0:sreg_32 = S_MOV_B32 999
    %1:av_32 = COPY %0
 $agpr0 = COPY %1
    S_ENDPGM 0
...

---
name: v_mov_b32_imm_literal_copy_v_to_av_32
tracksRegLiveness: true
body: |
  bb.0:
    %0:vgpr_32 = V_MOV_B32_e32 999, implicit $exec
 %1:av_32 = COPY %0
    $agpr0 = COPY %1
    S_ENDPGM 0

...

```

This hits a machine verifier failure:
```
Append normal operand 1
  %1:av_32 = V_MOV_B32_e32 %0:vgpr_32
Folded source from %0:vgpr_32 = V_MOV_B32_e32 999, implicit $exec
 into OpNo 1 of %1:av_32 = V_MOV_B32_e32 999, implicit $exec

# After SI Fold Operands
# Machine code for function v_mov_b32_imm_literal_copy_v_to_av_32: IsSSA, NoPHIs, TracksLiveness

bb.0:
  %1:av_32 = V_MOV_B32_e32 999, implicit $exec
 $agpr0 = COPY %1:av_32
  S_ENDPGM 0

# End machine code for function v_mov_b32_imm_literal_copy_v_to_av_32.

*** Bad machine code: Illegal virtual register for instruction ***
- function: v_mov_b32_imm_literal_copy_v_to_av_32
- basic block: %bb.0  (0x13b009928)
- instruction: %1:av_32 = V_MOV_B32_e32 999, implicit $exec
- operand 0: %1:av_32
Expected a VGPR_32 register, but got a AV_32 register

```

It is not possible to directly write a literal value to an AGPR, so it is not legal to fold these 
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysVVFvozgQ_jXOy4jIMaEtDzywzaZX6dpG7W6le0LGDMS3xka2oe2_PxlI2uxu7_Z6JyEF4vE3833-xsOdk41GzEjyiSSbBe_93tiMW4e6XZSmesnIGZ0fmhMWw_3XWxLnoJSAqPVWdgpJvOFt1QgNUSu6nsSbpn5OKYfI9jrquHMk3jgZ1UZVkenQcl05iAa0sn6JWi72UqPUzlsHkYEICEtcSDdl5EMhFHcOhAkxXGoP0oE2Hiy6DoXHirBL4LoCYbqXsGixU1xgBU_S74FrkEphwxUMRWuGoozZhB9FEaG55i0GUu6wWMi2LZT0aLkqAmbhCm8KPhTjRm-5-ObusfldDqgx8MvB2x4JzYNo4ZOcXxKaA5TlkpI4H98hMAtfzmJTxAxIvIGH4ubusfgUM0jT9DVsReJ8zDcGXd7t_pg20xwIW_Oms_RkZTVvfSg-3252VzcQQpfL5c94Du_zHP5XnkPT2QOFxwPPAmeu7BJk2ykppA-c8BnFRO_vyI_gv8r_rQQnRqb5l710sJfeAYfZgjA6UqKFmkvVW5z4vN2Ydx3qCrSxLVcwexmm3D_Ufcr4VBJC861RFVbgTG8FQm1N-59kk9obuOtuDazA1P9UzftAc9fltUcLD9cQyoS7uWvnxZtZMGEqhNpYqHstvDT6F60V53DtHh7yUMKt2f127cLbl9FuR6-Npby11YcpveeYGWsE_943geZnXR298UGqB_Ox-YFP_BRz1OJwOUnre67AYiNdkD_kGy_Gfkp5hAktfazk37R0BCV3UkCpjPg29i9LgsZB3Qv6vIpLStOUXRCWjtFvss_RHzuC6Ngs9HscQvPPz9M1Dhwer3b3AfygQcArew-N8cAhfzxZ-1ljXx-nQ2eck6VC8AYqaVF49QJPVnoEDrNEMHDVjxFcQ361uw_5nAF5RJmOxhsI4wv8Hh3CosriKo1TvsBsdb5esWR1sUo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