Issue 123120
Summary Missed shrink-wrap opportunity compared to GCC
Labels missed-optimization
Assignees
Reporter Kmeakin
    https://godbolt.org/z/KjTe5PhMP

```c++
#include <cstdint>
#include <vector>

auto push_back(std::vector<uint8_t>& xs, uint8_t x) -> void {
    xs.push_back(x);
}
```

GCC moves the code for saving/restoring registers via the stack into the subsequent basic blocks, leaving the happy path free from stack traffic. LLVM sadly does not.

```asm
; GCC output:
push_back(std::vector<unsigned char, std::allocator<unsigned char> >&, unsigned char):
        ldp     x2, x4, [x0, 8]
        mov     x3, x0
        cmp     x2, x4
        beq .L2
        strb    w1, [x2]
        ldr     x0, [x0, 8]
        add x0, x0, 1
        str     x0, [x3, 8]
        ret
.L2:
        ; grow the vector ...

; LLVM output:
push_back(std::vector<unsigned char, std::allocator<unsigned char>>&, unsigned char):
        stp     x29, x30, [sp, #-64]!
        stp     x24, x23, [sp, #16]
        stp     x22, x21, [sp, #32]
        stp     x20, x19, [sp, #48]
        mov     x29, sp
 ldp     x8, x9, [x0, #8]
        mov     x19, x0
        cmp     x8, x9
        b.eq    .LBB0_2
        strb    w1, [x8]
        ldr     x8, [x19, #8]
        add     x8, x8, #1
        str     x8, [x19, #8]
 ldp     x20, x19, [sp, #48]
        ldp     x22, x21, [sp, #32]
        ldp x24, x23, [sp, #16]
        ldp     x29, x30, [sp], #64
 ret
.LBB0_2:
        ; grow the vector ...
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to