llvm / llvm/llvm-project

Missed optimization: movaps initialization of repetitive data

Open
#201,704 3 comments 0 reactions 1 assignee Claimed by @xiongzile View on GitHub
llvm:optimizations missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

```c++
// Type your code here, or load an example.
int cube(int* num);
int square(int num) {
int nums[16] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
return cube(nums);
}
```
-O2 or -O3

Result:

```asm
square(int):
sub rsp, 72
movaps xmm0, xmmword ptr [rip + .L__const.square(int) (.nums)+48]
movaps xmmword ptr [rsp + 48], xmm0
movaps xmm0, xmmword ptr [rip + .L__const.square(int) (.nums)+32]
movaps xmmword ptr [rsp + 32], xmm0
movaps xmm0, xmmword ptr [rip + .L__const.square(int) (.nums)+16]
movaps xmmword ptr [rsp + 16], xmm0
movaps xmm0, xmmword ptr [rip + .L__const.square(int) (.nums)]
movaps xmmword ptr [rsp], xmm0
mov rdi, rsp
call cube(int*)@PLT
add rsp, 72
ret

.L__const.square(int) (.nums):
.long 1
.long 1
.long 1
.long 1
.long 1
.long 1
.long 1
.long 1
.long 1
.long 1
.long 1
.long 1
.long 1
.long 1
.long 1
.long 1
```

Expected: Deduplicate the 16-byte chunks in that table, like MSVC and GCC do. https://godbolt.org/z/z3bGf7Y5h

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.