This addresses sth I needed to address with the early LTO debug patches
(you might now figure I'm piecemail merging stuff from that patch).

When the cgraph code optimizes out a global we call the late_global_decl
debug hook to eventually add a DW_AT_const_value to its DIE (we don't
really expect a location as that will be invalid after optimizing out
and will be pruned).

With the early LTO debug patches I have introduced a early_dwarf_finished
flag (mainly for consistency checking) and I figured I can use that to
detect the call to the late hook during the early phase and provide
the following cleaned up variant of avoiding to create locations that
require later pruning (which doesn't work with emitting the early DIEs).

Bootstrapped on x86_64-unknown-linux-gnu, testing in progress.

I verified it does the correct thing for a unit like

static const int i = 2;

(but ISTR we do have at least one testcase in the testsuite as well).

Will commit if testing finishes successfully.

Richard.

2016-09-15  Richard Biener  <rguent...@suse.de>

        * dwarf2out.c (early_dwarf_finished): New global.
        (set_early_dwarf::set_early_dwarf): Assert early_dwarf_finished
        is false.
        (dwarf2out_early_finish): Set early_dwarf_finished at the end.
        (gen_variable_die): When being invoked late during the early
        debug phase do not add locations but only const value attributes.

Index: gcc/dwarf2out.c
===================================================================
--- gcc/dwarf2out.c     (revision 240153)
+++ gcc/dwarf2out.c     (working copy)
@@ -2711,9 +2711,14 @@ die_node;
 
 /* Set to TRUE while dwarf2out_early_global_decl is running.  */
 static bool early_dwarf;
+static bool early_dwarf_finished;
 struct set_early_dwarf {
   bool saved;
-  set_early_dwarf () : saved(early_dwarf) { early_dwarf = true; }
+  set_early_dwarf () : saved(early_dwarf)
+    {
+      gcc_assert (! early_dwarf_finished);
+      early_dwarf = true;
+    }
   ~set_early_dwarf () { early_dwarf = saved; }
 };
 
@@ -21464,8 +21469,17 @@ gen_variable_die (tree decl, tree origin
       if (early_dwarf)
        add_pubname (decl_or_origin, var_die);
       else
-       add_location_or_const_value_attribute (var_die, decl_or_origin,
-                                              decl == NULL);
+       {
+         /* We get called during the early debug phase via the symtab
+            code invoking late_global_decl for symbols that are optimized
+            out.  When the early phase is not finished, do not add
+            locations.  */
+         if (! early_dwarf_finished)
+           tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
+         else
+           add_location_or_const_value_attribute (var_die, decl_or_origin,
+                                                  decl == NULL);
+       }
     }
   else
     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
@@ -28163,6 +28177,9 @@ dwarf2out_early_finish (void)
        }
     }
   deferred_asm_name = NULL;
+
+  /* The early debug phase is now finished.  */
+  early_dwarf_finished = true;
 }
 
 /* Reset all state within dwarf2out.c so that we can rerun the compiler

Reply via email to