llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Missing optimization] Atomic add or subtract a pointer

Open youngbe opened this issue 2 years ago • 1 comments

src:

#include <stdatomic.h>
#include <stdint.h>
_Atomic(uintptr_t) y;
_Atomic(uint8_t *) x;
void ppp()
{
    ++x;
    ++y;
}

GCC 12 -Ofast:

ppp:
    lock addq   $1, x(%rip)
    lock addq   $1, y(%rip)
    ret

Clang 15.0.6 -Ofast:

ppp:                                    # @ppp
.Lppp$local:
    movq    .Lx$local(%rip), %rax
    .p2align    4, 0x90
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
    leaq    1(%rax), %rcx
    lock        cmpxchgq    %rcx, .Lx$local(%rip)
    jne .LBB0_1
    lock        incq    .Ly$local(%rip)
    retq

youngbe avatar Feb 23 '23 08:02 youngbe

@llvm/issue-subscribers-clang-codegen

Author: None (youngbe)

src: ```c #include <stdatomic.h> #include <stdint.h> _Atomic(uintptr_t) y; _Atomic(uint8_t *) x; void ppp() { ++x; ++y; } ```

GCC 12 -Ofast:

ppp:
    lock addq   $1, x(%rip)
    lock addq   $1, y(%rip)
    ret

Clang 15.0.6 -Ofast:

ppp:                                    # @<!-- -->ppp
.Lppp$local:
    movq    .Lx$local(%rip), %rax
    .p2align    4, 0x90
.LBB0_1:                                # =&gt;This Inner Loop Header: Depth=1
    leaq    1(%rax), %rcx
    lock        cmpxchgq    %rcx, .Lx$local(%rip)
    jne .LBB0_1
    lock        incq    .Ly$local(%rip)
    retq

llvmbot avatar Mar 24 '24 12:03 llvmbot