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

    <tr>
        <th>Summary</th>
        <td>
            AMDGPU generates v_cndmask/readfirstlane for uniform select
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:AMDGPU,
            missed-optimization
      </td>
    </tr>

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

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

<pre>
    Test case:
```
define amdgpu_ps i32 @_amdgpu_ps_main(i32 inreg %arg) {
bb:
  %i = icmp eq i32 %arg, 0
  %i1 = zext i1 %i to i64
  %i2 = getelementptr i8, ptr addrspace(4) null, i64 %i1
  %i3 = load i32, ptr addrspace(4) %i2, align 8
  ret i32 %i3
}
```
If I compile with `llc -march=amdgcn -mcpu=gfx900` I get:
```
_amdgpu_ps_main: ; @_amdgpu_ps_main
; %bb.0:                                ; %bb
        s_cmp_eq_u32 s0, 0
        s_cselect_b64 s[2:3], -1, 0
        v_cndmask_b32_e64 v0, 0, 1, s[2:3]
        s_mov_b32 s1, 0
        v_readfirstlane_b32 s0, v0
        s_load_dword s0, s[0:1], 0x0
        s_waitcnt lgkmcnt(0)
        ; return to shader part epilog
```
All computations are uniform, so the use of v_cndmask_b32_e64 and v_readfirstlane_b32 is wasteful and inefficient.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyEVE1vpDgQ_TXmUuqWsZuvA4fOoF7lsNIeds_I2AXtibEZ23Qy8-tXpjtJZyfRIiQM79Wrqlc2IgQ9WcSWFA-k6DKxxrPz7Xfxc3RCZYNTP9u_MUSQIiDhR0I7Qo-kpLd7e1U4aosgZjUta78E0JwBOdD-7Us_C20JqxOgrccJCCuEnwhrgFQPV5lheEsACddAeAdazgvgj6vmLegb0HtevhF_4UuEtE6R0YEuD_cktpEmjGhwRhuX6EHXSSuthFI-LEIiYfUhVWVXYxKoy8M1x70W37SMEyqV9aXGljahwujJQv0q4TG-tqP5zdGq-9TaxxEeQbp50QbhWcczkJIaI2E3Cy_PhHfJY2lhN8tlJbybxpeGpnh4TM1-NbL_joYfgfCHz4Z2jU4YK4ZhTxP1f6439i2YNqGX89Ljj37lDAK9m-AVDGhQxn4oDxBI8cAIP3JSdIm3yz-yL720ahbhqR8467E8wOWmx77Bxv2g8J5kdpcUAuE3QY9CjdqHaITFK2dTu9zXmKbdq2fn1Q1NaZIb-a1Q-nJPfxY6ShvBTE-ztJGwmhLWvBGSRR7j6m3aquEsFHpYhI-AizZu-nRoR2O2vbBGEbWzAYRHWK0enZ-3ihzEM8IaENwIvxslrILPutUBnkWIOK5m42iL46ilRhv3mWq5angjMmzzsuK0bjgrsnM7SClUTQdVcCWHUam8lrmSY1FVtWCqynTLKOM0p1WeU0YP-6pSgub1WCqZN1g25EBxFtrsjbnMe-enTIewYls0ddlkRgxowvZfYmwQ8gmtIvx4_LP7469_CEvnijA26xBQ7dwS9ax_bbYkrOgy3ybZ3bBOgRyo0SGG90RRR4PtVQsmtOhFxPBuGWGnDzbB6Pyr03DdrNnqTXuOcQnpjLETYadJx_M67KWbCTulXLfHbvHuO8pI2GnrMBB22pr8NwAA__8qfZtB">