Why does GCC compiler optimize out variable foo
entirely?
- it can reason that it will never return from an infinite loop and thus there is no possibility of this variable being used in any other translation unit past this point
- the variable is NOT volatile qualified meaning that accessing it will not change the observable behavior
Code:
int foo;
int main(void)
{
foo = 10;
while(1)
{
// Infinite loop never returns
}
}
https://godbolt.org/z/5ebvbszE9