http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57551
Jan Hubicka <hubicka at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |jason at redhat dot com --- Comment #2 from Jan Hubicka <hubicka at gcc dot gnu.org> --- You are right, I added attribute used to the variable and forgot about it. The testcase is bogus by expecting unused local static to not be optmized away. Looking closer, I am however confused. While the attribute fixes the testcase, i think something wrong is still going on hre. We get the following symbol out of FE: _ZN12_GLOBAL__N_11BIiE1cE/1 (const int {anonymous}::B<int>::c) @0x7ffff75141a0 Type: variable definition analyzed Visibility: force_output comdat Aux: @0x1 References: Referring: Availability: not-ready Varpool flags: initialized read-only const-value-known i.e. it is COMDAT but not public. It is the force_output flag that keeps variable alive and that is there because of keying. The variable is output as static object: jh@gcc10:~/trunk/build3/gcc$ more anon6.s .file "anon6.C" .section .rodata .align 4 .type _ZN12_GLOBAL__N_11BIiE1cE, @object .size _ZN12_GLOBAL__N_11BIiE1cE, 4 _ZN12_GLOBAL__N_11BIiE1cE: .zero 4 .ident "GCC: (GNU) 4.9.0 20130602 (experimental)" .section .note.GNU-stack,"",@progbits i.e. no comdat visibility. This flag stays through the compilation and seems to be ignored by output routines. And now adding the attribute used: Index: /home/jh/trunk/gcc/testsuite/g++.dg/ext/visibility/anon6.C =================================================================== --- /home/jh/trunk/gcc/testsuite/g++.dg/ext/visibility/anon6.C (revision 199698) +++ /home/jh/trunk/gcc/testsuite/g++.dg/ext/visibility/anon6.C (working copy) @@ -18,7 +18,7 @@ namespace template <typename T> class B { - static const T c = 0; + __attribute__ ((__used__)) static const T c = 0; }; template <typename T> const T B<T>::c; changes object to: _ZN12_GLOBAL__N_11BIiE1cE/1 (const int {anonymous}::B<int>::c) @0x7ffff75141a0 Type: variable definition analyzed Visibility: force_output forced_by_abi public weak comdat comdat_group:_ZN12_GLOBAL__N_11BIiE1cE one_only Aux: @0x7ffff7402850 References: Referring: Availability: not-ready Varpool flags: initialized read-only const-value-known which changes assembly to: jh@gcc10:~/trunk/build/gcc$ ./xgcc -B ./ -O2 /home/jh/trunk/gcc/testsuite/g++.dg/ext/visibility/anon6.C -S -std=c++98 -pedantic-errors -Wno-long-long -S -fdump-ipa-all-details -fdump-ipa-cgraph jh@gcc10:~/trunk/build/gcc$ more anon6.s .file "anon6.C" .weak _ZN12_GLOBAL__N_11BIiE1cE .section .rodata._ZN12_GLOBAL__N_11BIiE1cE,"aG",@progbits,_ZN12_GLOBAL__N_11BIiE1cE,comdat .align 4 .type _ZN12_GLOBAL__N_11BIiE1cE, @gnu_unique_object .size _ZN12_GLOBAL__N_11BIiE1cE, 4 _ZN12_GLOBAL__N_11BIiE1cE: .zero 4 .ident "GCC: (GNU) 4.9.0 20130605 (experimental)" .section .note.GNU-stack,"",@progbits Here we now disagree with GCC 4.5 that still outputs this as static. Jason, why C++ FE behaves this way now? clang also optimizes out the variable in testcase and adding attribute used makes the var to be static. I think the testcase should be fixed by adding the used flag and by checking that weaks/comdats are not involved in the assembly and C++ FE should be fixed. or do I miss something? Honza