Issue 58129
Summary [Thumb-1] inefficient select in pre-v6 thumb
Labels new issue
Assignees
Reporter easyaspi314
    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
```

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to