Issue 234665
Summary [X86] Tail call that forwards the sret pointer is missed outside the entry block
Labels backend:X86, missed-optimization
Assignees
Reporter chfast
    A call that passes the caller's sret pointer on is tail-called only when it is in the entry block. Behind a branch the same call becomes `call` + `ret` with a frame.

The case comes from multi-precision integer code (intx), where a division function dispatches to helpers returning the same struct.

https://godbolt.org/z/o9ETc5rz7

```c
typedef unsigned long long u64;
struct R { u64 q[4], r[4]; };
struct R f1(const u64 *a, u64 d);
struct R f2(const u64 *a, const u64 *d);

// Tail calls are missed: call + ret with a frame.
struct R dispatch(const u64 *a, const u64 *d) {
 if (d[1] == 0)
        return f1(a, d[0]);
    return f2(a, d);
}

// Single call in the entry block: jmp.
struct R single(const u64 *a, const u64 *d) {
    return f1(a, d[0]);
}
```

`clang -O2`:

```asm
dispatch:
 pushq   %rbx
        movq    %rdi, %rbx
        cmpq    $0, 8(%rdx)
        je      .LBB0_1
        movq    %rbx, %rdi
        callq f2@PLT
        jmp     .LBB0_3
.LBB0_1:
        movq    (%rdx), %rdx
 movq    %rbx, %rdi
        callq   f1@PLT
.LBB0_3:
        movq %rbx, %rax
        popq    %rbx
        retq

single:
        movq (%rdx), %rdx
        jmp     f1@PLT
```

After CodeGenPrepare both calls get `%agg.result` as their sret argument and are directly followed by `ret void`. The same IR without `sret` gives two `jmp`s. `mayBeSRetTailCallCompatible()` finds the forwarded pointer through the `CopyToReg` into the sret return register, which only the entry block has.

Related: #222274 (sret caller, callee without sret).

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

Reply via email to