Skip to content

Missed optimization: not tail calling memset #75455

Closed
@dvyukov

Description

@dvyukov

The code is (extracted from calloc implementation):

void* foo(void* x, long s) {
    if (__builtin_expect(x != nullptr, true))
      return __builtin_memset(x, 0, s);
    return x;
}

Clang generages:

foo(void*, long):                              # @foo(void*, long)
        pushq   %rbx
        movq    %rdi, %rbx
        testq   %rdi, %rdi
        je      .LBB0_2
        movq    %rsi, %rdx
        movq    %rbx, %rdi
        xorl    %esi, %esi
        callq   memset@PLT
.LBB0_2:
        movq    %rbx, %rax
        popq    %rbx
        retq

https://godbolt.org/z/YojTG44bs

This should be:

foo(void*, long):                              # @foo(void*, long)
        testq   %rdi, %rdi
        je      .LBB0_2
        movq    %rsi, %rdx
        xorl    %esi, %esi
        jmp     memset@PLT
.LBB0_2:
        retq

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions