https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89922
Bug ID: 89922
Summary: Loop on fixed size array is not unrolled and poorly
optimized
Product: gcc
Version: 9.0
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: antoshkka at gmail dot com
Target Milestone: ---
Consider the example:
struct array {
int data[5];
};
array test(int i) {
array a = {1, i, 2, 3, 4};
for (int j = 0; j < 5; ++j) {
a.data[j] += j;
}
return a;
}
GCC-9 generates ~20 instructions with jmps.
Rewriting the same function with unrolled loop makes the assembly much better:
array test2(int i) {
array a = {1, i, 2, 3, 4};
a.data[0] += 0;
a.data[1] += 1;
a.data[2] += 2;
a.data[3] += 3;
a.data[4] += 4;
return a;
}
Assembly for `test2` takes only ~8 instructions:
test2(int):
add esi, 1
mov DWORD PTR [rdi], 1
mov rax, rdi
movabs rdx, 25769803780
mov DWORD PTR [rdi+4], esi
mov QWORD PTR [rdi+8], rdx
mov DWORD PTR [rdi+16], 8
ret