https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64827
Bug ID: 64827 Summary: LTO doesn't propogate/optimise initialised global variable value passed by reference Product: gcc Version: 4.9.3 Status: UNCONFIRMED Severity: normal Priority: P3 Component: lto Assignee: unassigned at gcc dot gnu.org Reporter: justynbutlerspam at gmail dot com Sorry if this is already well-known or I'm misinformed, but I couldn't find any reference to it. Consider the following code: #include <stdio.h> int doublefunc (int *input) { return *input *2; } int foo = 3; int main(void) { if ( doublefunc(&foo) == 2 ) printf( "operation result was 2" ); } Compiled with: gcc -ffunction-sections -fdata-sections -O3 -flto -Xlinker --gc-sections -O3 -flto -o main main.c With the doublefunc defined in the same translation unit as above, the compiler correctly removes the printf statement as dead code (unless foo is initialised as 1). But if the function is put into a separate file and compiled with the same options (including -flto), the optimisation doesn't happen. This seems to me like an LTO failure. I've found that in several situations the optimisation will successfully occur even when the function is in a separate unit: * if the global variable is initialised with const * if the value of the variable is set inside main * if a second (global or otherwise) variable is assigned the value of the first variable inside main and that is passed to doublefunc The last one makes it appear particularly strange, since using the following (with doublefunc in a separate file) will lead the compiler to optimise correctly: int foo = 3; int bar; int main(void) { bar=foo; if ( doublefunc(&bar) == 2 ) printf( "operation result was 2" ); } The problem only seems to affect variables passed by reference, the optimisation works if the variable is passed by value instead. I've tested this with gcc 4.8.2 and also arm-none-eabi-gcc 4.9.3 on Ubuntu 14.04 64-bit. Am I missing something fundamental or is it a bug?