Issue 174933
Summary Missed optimization x86-64 clang 21.1.0: lock xadd not replaced with lock add
Labels clang
Assignees
Reporter piotr-topnotch
    
```
#include <atomic>

bool fn_sub(std::atomic_uint64_t& a, std::uint64_t n) noexcept {
    return a.fetch_sub(n, std::memory_order_relaxed) == n;
}

bool fn_add(std::atomic_uint64_t& a, std::uint64_t n) noexcept {
    return a.fetch_add(-n, std::memory_order_relaxed) == n;
}
```

The first function correctly undergoes strength reduction replacing xadd with sub:

```
fn_sub(std::atomic<unsigned long>&, unsigned long):
 lock            sub     qword ptr [rdi], rsi
        sete    al
 ret

```
Whereas the second pattern is not recognized:

```
fn_add(std::atomic<unsigned long>&, unsigned long):
 mov     rax, rsi
        neg     rax
        lock            xadd qword ptr [rdi], rax
        cmp     rax, rsi
        sete    al
 ret
```

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

Reply via email to