https://bugs.llvm.org/show_bug.cgi?id=47438

            Bug ID: 47438
           Summary: swap goes through memory when it could stay in
                    registers
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: [email protected]
          Reporter: [email protected]
                CC: [email protected]

Godbolt: https://godbolt.org/z/Yez1Mc

$ cat t.cc
#include <utility>

struct foo {
    float x, y, z, w;
};

void bar(foo& a, foo& b) { std::swap(a, b); }

$ gcc-11 -O3 -S -o - t.cc
bar(foo&, foo&):
        movups  (%rdi), %xmm0
        movdqu  (%rsi), %xmm1
        movups  %xmm1, (%rdi)
        movups  %xmm0, (%rsi)
        ret

$ clang-11 -O3 -S -o - t.cc
bar(foo&, foo&):                         # @bar(foo&, foo&)
        movups  (%rdi), %xmm0
        movaps  %xmm0, -24(%rsp)
        movups  (%rsi), %xmm0
        movups  %xmm0, (%rdi)
        movaps  -24(%rsp), %xmm0
        movups  %xmm0, (%rsi)
        retq

The std::swap expands into an alloca and 3 memcpy's.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to