https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98425
Bug ID: 98425
Summary: Superfluous sign-extend for constrained integer
Product: gcc
Version: 11.0
Status: UNCONFIRMED
Severity: enhancement
Priority: P3
Component: rtl-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: koenigni at gcc dot gnu.org
Target Milestone: ---
Hello everyone,
A small missed optimization I noticed while toying around with the difference
between signed and unsigned integers. The following code
int
baz(int *p, int i) {
int j;
if (i >= 0) {
j = i + 4;
return p[j];
} else
__builtin_unreachable();
}
is compiled with `gcc -O3 -S` to
baz:
addl $4, %esi
movslq %esi, %rsi
movl (%rdi,%rsi,4), %eax
ret
The movslq instruction is unnecessary since i is constrained to never be
negative and therefore no sign extension is needed. This probably also prevents
the addl instructions to be removed and the offset being put into the movl.
For comparison, clang (with the same options) compiles the code to
baz:
movl %esi, %eax
movl 16(%rdi, %rax, 4), %eax
ret
Optimal would probably be
baz:
movl 16(%rdi, %rsi, 4), %eax
ret