I tried compiling the following code on Compiler Explorer:
int index(int *arr, unsigned int i) {
return arr[i];
}
With x86-64 gcc 11.2, on -O2 the following code is generated:
index(int*, unsigned int):
mov esi, esi
mov eax, DWORD PTR [rdi+rsi*4]
ret
I've seen this answer, which says that moving a register to itself is an 'inefficient NOP' and was deemed to be 'an artifact of code-generation from a non-optimizing compiler'.
Why does GCC generate mov esi, esi here on -O2?