Issue 144613
Summary Copying of adjacent fields can be merged
Labels missed-optimization
Assignees
Reporter hiraditya
    Saw this on X(https://x.com/joseph_h_garvin/status/1934705584705843340) Assigning adjacent field of struct can be merged as a copy but clang does not recognize it when the data types are different.

Repro:

```cpp
#include <functional>

struct handle {
    void* data; // Replacing it with int data works.
    int index;
};

void remove(handle* p, int idx, int& end)
{
    auto& item = p[idx];
    auto& last = p[end];
    item = last;
    --end;
}

void remove2(handle* p, int idx, int& end)
{
 auto& item = p[idx];
    auto& last = p[end];
    item.data = ""
 item.index = last.index;
    --end;
}
```

$ clang -O2
```asm
remove(handle*, int, int&):
        ldrsw   x8, [x2]
 ldr     q0, [x0, x8, lsl #4]
        str     q0, [x0, w1, sxtw #4]
 ldr     w8, [x2]
        sub     w8, w8, #1
        str     w8, [x2]
 ret

remove2(handle*, int, int&):
        ldrsw   x8, [x2]
        add x9, x0, w1, sxtw #4
        add     x8, x0, x8, lsl #4
        ldr w10, [x8, #8]
        ldr     x8, [x8]
        str     w10, [x9, #8]
 ldr     w10, [x2]
        str     x8, [x9]
        sub     w8, w10, #1
 str     w8, [x2]
        ret
```

https://godbolt.org/z/61avd145T
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to