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

    <tr>
        <th>Summary</th>
        <td>
            [Thumb-1] inefficient select in pre-v6 thumb 
        </td>
    </tr>

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

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

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

<pre>
    LLVM makes questionable decisions on Thumb-1 with selects pre-v6 (where `tMOVr` only works on hi registers).

```llvm
define i32 @foo(i32 %x, i32 %y, i32 %z) {
    %cmp = icmp ne i32 %x, 0
    %sel = select i1 %cmp, i32 %y, i32 %z
    ; breaks return x ? y : z pattern
    %sum = add i32 %sel, %z
    ret i32 %sum
}
```
`clang --target=thumbv4t-none-eabi -O2 -S foo.ll`

```asm
foo:
    cmp     r0, #0
    push    {r2}
    pop     {r0}
    beq     .LBB0_2
    movs    r0, r1
.LBB0_2:
    adds    r0, r0, r2
    bx      lr
```

The ideal solution would just be something like this, which is similar to what it would emit with the `return x ? y : z` pattern.
```asm
foo:
    cmp     r0, #0
    beq     .LBB0_2
    movs    r1, r2
.LBB0_2:
    adds    r0, r1, r2
    bx      lr
```

However, even putting the optimal code aside, it would be better to try and clobber an available hi register:
```asm
foo:
    cmp     r0, #0
    mov     r12, r2
    mov     r0, r12
    beq     .LBB0_2
    movs    r0, r1
.LBB0_2:
    adds    r0, r0, r2
    bx      lr
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzNVU2PmzAQ_TVwGREZE0Jy4LDpquphqx662mtlYBLcNTi1TbLpr-_YkK_VVq3aHoqQGXs8b2aex0Olm2P58PD0ETrxjBa-DWid1L2oFEKDtbQ0saB7eGyHrkpSOEjXgkWFtbOwM5jsFxDx5aFFgxAtmPv46cnQl2zUEQ7aPAfzVoLBrbQOjY34ahax-4jdTeOCja9S-25canAjewSZcYjmbKM1uQgTnr9E_B1M8vFK_k6wEBXrEQDoocW620GU3YP0wglwwmA3OymlsHNMDWQ6mf_U28U4W0NlUFCiBt1genihtfdwpPEOvsNOOMq6v_U2dMGbaJoTIjn2-LfYBHjWDxM3UXH_irfztFai30KSOGG26MiB86e2n7uk1z0mKCoJyScOyWcgTmdKXYxvEYWdnHnqs7tLQJ7HEBgbg82uWNwNtg35FWvDz1EGjR6tvIbdaCr8FjSzh_WafeEXRaf39uLIpKPmtO06JOLweuc4XiFVL8EDKPM2bWF8bKk4GhQKrFaDvwJUu4Nq4OtgHUVJyx26VhK9Sj4jkGi9o0Mr6xakBSs7qYQBp2lN0Km5CQA7L_pb49pwQ94qEn9hpjqZ_Yuj-DWt6TVNv0Fr-ke0ftAH3KPxtvTtqUSc8xx6KvTOyY4Ir3WDICyxH-7XiTfivELPiKfUmSOIvoFa6aqiJdGD2Asi3Pepq95yzuDv6COWRl3KX6d9Vk208P-plmMs08VivuRFnmdxU2bNKluJ2EmnsIzy9dTEo5w6Yo-bjawl9u7c8_pTQw9tA-LBqLJ1bmd9jPw9vVuq46Ga1bqjSWjX4yfZGf2VMGgqraWfCAn5MuWruC2rGvN52ojNqsnyZcHmrGKsyrGZF1lRII_pFFFZH1_EeY8HCBAkU5yxLDnjPGUs44xRVjNWF5t0k4klr-r5khX0f8COSmHm45hps41NGUKqhq0lpaLCsBelsFZuewx0eHwxuFabEoU9CruTWTqPg_syhP8D_xH23g">