Issue 84277
Summary [missed optimization] repeat load operation when returning a struct
Labels new issue
Assignees
Reporter Absoler
    I have found a test case (reduced from https://github.com/llvm/llvm-project/issues/83812) which showes a missed optimization of clang-17.0.6 -O2
```
struct S0 {
   int  f0;
 int  f1;
   long long  f2;
};

int copy = 0;
struct S0 global_s = {1, 1, 1};
struct S0 value = {2, 2, 2};
int other = 1;

struct S0 get_struct() { return value; }

void func_bad(struct S0 d) {
  struct S0 local_s;
  global_s = local_s = get_struct();
  copy = local_s.f1;
  if (copy)
 --other;
}
```
clang-17~15 fail to reuse the value loaded from `global_s` while clang-14 could. here's the comparison of generated binaries:
clang-17: ( https://godbolt.org/z/Gx98vzoYP )
```
0000000000001140 <func_bad>:
    1140:	movaps 0x2ef9(%rip),%xmm0        # 4040 <value>
    1147:	mov 0x2ef2(%rip),%rax        # 4040 <value>
func_bad():
    114e:	shr $0x20,%rax
    1152:	movaps %xmm0,0x2ed7(%rip)        # 4030 <global_s>
    1159:	mov    %eax,0x2ef9(%rip)        # 4058 <copy>
 115f:	je     1167 <func_bad+0x27>
    1161:	decl   0x2ee9(%rip)        # 4050 <other>
```
clang-14:
```
0000000000401120 <func_bad>:
 401120:	movdqa 0x2f18(%rip),%xmm0        # 404040 <value>
func_bad():
 401128:	movq   %xmm0,%rax
  40112d:	shr    $0x20,%rax
 401131:	movdqa %xmm0,0x2ef7(%rip)        # 404030 <global_s>
 401139:	mov    %eax,0x2f19(%rip)        # 404058 <copy>
  40113f:	je 401148 <func_bad+0x28>
  401141:	addl   $0xffffffff,0x2f08(%rip)        # 404050 <other>
```

we can see that in clang-17's output the load operation at 0x1147 could be removed, and it's better to reuse the value in %xmm0 just like clang-14's output dose at 0x401128.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to