Issue 241566
Summary [AArch64] LoopIdiomRecognize turns a 16-64 B copy loop in an aarch64_vector_pcs function into a memcpy call, forcing q8-q23 saves
Labels new issue
Assignees
Reporter Tomarty
    ```c
typedef float v4 __attribute__((vector_size(16)));
__attribute__((aarch64_vector_pcs)) unsigned f(v4* restrict d, const v4* restrict s, unsigned x)
{
 unsigned n = (x & 3) + 1;
        for (unsigned i = 0; i != n; ++i) d[i] = s[i];
        return n;
}
```
flags: `-O2 -march=armv8.2-a`

https://godbolt.org/z/KKr4eWe5T
```
f:
        stp q23, q22, [sp, #-288]!
        stp     q21, q20, [sp, #32]
        stp q19, q18, [sp, #64]
        stp     q17, q16, [sp, #96]
        stp q15, q14, [sp, #128]
        stp     q13, q12, [sp, #160]
        stp q11, q10, [sp, #192]
        stp     q9, q8, [sp, #224]
        stp x29, x30, [sp, #256]
        stp     x28, x19, [sp, #272]
        add x29, sp, #256
        ubfiz   w8, w2, #4, #2
        mov     w19, w2
 add     w2, w8, #16
        bl      memcpy
        and     w8, w19, #0x3
        add     w0, w8, #1
        ldp     x28, x19, [sp, #272]
 ldp     x29, x30, [sp, #256]
        ldp     q9, q8, [sp, #224]
 ldp     q11, q10, [sp, #192]
        ldp     q13, q12, [sp, #160]
 ldp     q15, q14, [sp, #128]
        ldp     q17, q16, [sp, #96]
 ldp     q19, q18, [sp, #64]
        ldp     q21, q20, [sp, #32]
        ldp q23, q22, [sp], #288
        ret
```

The trip count is 1-4, so the copy is at most 64 bytes. LoopIdiomRecognize turns the loop into `llvm.memcpy`, which becomes a libcall because the length is not constant. memcpy uses the base PCS, and f must preserve q8-q23 for its vector-PCS callers, so the call forces 16 q-register saves and restores in a 288 B frame. The loop itself uses no callee-saved registers.

- `-mllvm -disable-loop-idiom-memcpy` gives four guarded `ldr q`/`str q` pairs: no call, no frame.
- The same function without `aarch64_vector_pcs` still calls memcpy, but with only an x29/x30 frame.

Expected: LoopIdiomRecognize should not form memcpy/memset in an aarch64_vector_pcs function when the call is more expensive than the loop. Here the maximum trip count is provably small.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to