https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86605
Bug ID: 86605
Summary: Suboptimal code for pointer arithmetic with 'this'
Product: gcc
Version: 9.0
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: antoshkka at gmail dot com
Target Milestone: ---
In GCC 5.5 an optimization of following code was broken:
struct Test {
Test(Test* p) : i{this-p ? 10 : 20} {}
int i;
};
int f() {
Test test{(Test*)4};
return test.i;
}
GCC 5.4 generates optimal code:
f():
mov eax, 10
ret
GCC 5.5 generates:
f():
lea rax, [rsp-5]
cmp rax, 7
sbb eax, eax
and eax, 10
add eax, 10
ret
GCC (trunk) generates also suboptimal code:
f():
cmp rsp, 8
mov edx, 20
mov eax, 10
cmove eax, edx
ret
Note, that in GCC 5.5 the optimizer was fixed to generate optimal code for the
following:
struct Test {
Test(Test* p) : i{this-p ? 10 : 20} {}
int i;
};
int f() {
Test test{(Test*)3}; // 3 instead of 4
return test.i;
}
So that now GCC (trunk) generates optimal code for that case:
f():
mov eax, 10
ret
but fail if we change 3 back to 4