I was playing around with Godbolt and I was surprised to see that the following simple C++ code had different compiled outputs on x86-64 gcc 10.2 and x86-64 clang 11.0.0 with -O2 level optimization.
static int x = 0;
int const y = x;
int main()
{
x = 0; // should be optimised out?
}
gcc:
main:
mov DWORD PTR x[rip], 0
xor eax, eax
ret
_GLOBAL__sub_I_main:
ret
clang:
main: # @main
xor eax, eax
ret
Why can clang prove that x is unused and optimise it out but not gcc?