I am testing the following patch which re-instantiates behavior to emit DW_AT_const_value attributes for optimized out non-readonly global statics. Those are readonly by means of the ability to remove them early (before any optimization such as removing stores to write-only global vars). Before my change to make the late_global_decl debug hook not go via add_location_or_const_value_attribute but only tree_add_const_value_attribute_for_decl during the early debug phase the former function ran into rtl_for_decl_location doing
/* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant, and will have been substituted directly into all expressions that use it. C does not have such a concept, but C++ and other languages do. */ if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl)) rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl)); where obviously at this point of the compilation no DECL_RTL was created yet (the above code looks fishy to me -- tree_add_const_value_attribute_for_decl checks for TREE_READONLY on the decl). Anyway, bootstrapping and regtesting the following on x86_64-unknown-linux-gnu (it makes gcc.dg/debug/dwarf2/const-2b.c PASS again). Comments welcome. Richard. 2016-09-23 Richard Biener <rguent...@suse.de> PR debug/77692 * cgraphunit.c (analyze_functions): Before early removing global vars calls the late_global_decl debug handler mark the variable as readonly. Index: gcc/cgraphunit.c =================================================================== --- gcc/cgraphunit.c (revision 240388) +++ gcc/cgraphunit.c (working copy) @@ -1194,8 +1194,15 @@ analyze_functions (bool first_time) at looking at optimized away DECLs, since late_global_decl will subsequently be called from the contents of the now pruned symbol table. */ - if (!decl_function_context (node->decl)) - (*debug_hooks->late_global_decl) (node->decl); + if (TREE_CODE (node->decl) == VAR_DECL + && !decl_function_context (node->decl)) + { + /* We are reclaiming totally unreachable code and variables + so they effectively appear as readonly. Show that to + the debug machinery. */ + TREE_READONLY (node->decl) = 1; + (*debug_hooks->late_global_decl) (node->decl); + } node->remove (); continue;