Hi, this patch fixes both problems by using the same condition as add_referenced_var uses to guard walking into initializers. I've considered some other solutions but the real nice one (merging local_decls and referenced_vars, and not using annotations for the used flag) doesn't seem appropriate for stage 3, and all the other ones would just look similar.
I've deviated from richis proposed patch in 50741 in that I guard only walking into initializers of non-local vars, but still add those vars itself (so the invariant that all variables that are somehow mentioned in any instruction are in referenced_vars still holds). This doesn't fix the fortran PR50640 (select_type_12 segfault). This testcase shows two problems, one is fixed by this patch, the other remains. As the audit trail explains the fortran frontend really should present different code. Regstrapping for x86_64-linux in progress (all languages+Ada). Okay if that passes? Ciao, Michael. ---------------- PR middle-end/50644 PR middle-end/50741 * tree-ssa-live.c (mark_all_vars_used_1): Recurse only for decls of current function. (remove_unused_locals): Ditto. testsuite/ PR middle-end/50644 PR middle-end/50741 * g++.dg/tree-ssa/pr50741.C: New. Index: tree-ssa-live.c =================================================================== --- tree-ssa-live.c (revision 181172) +++ tree-ssa-live.c (working copy) @@ -374,7 +374,8 @@ mark_all_vars_used_1 (tree *tp, int *wal eliminated as unused. */ if (TREE_CODE (t) == VAR_DECL) { - if (data != NULL && bitmap_clear_bit ((bitmap) data, DECL_UID (t))) + if (data != NULL && bitmap_clear_bit ((bitmap) data, DECL_UID (t)) + && DECL_CONTEXT (t) == current_function_decl) mark_all_vars_used (&DECL_INITIAL (t), data); set_is_used (t); } @@ -836,7 +837,8 @@ remove_unused_locals (void) if (TREE_CODE (var) == VAR_DECL && is_global_var (var) && var_ann (var) != NULL - && is_used_p (var)) + && is_used_p (var) + && DECL_CONTEXT (var) == current_function_decl) mark_all_vars_used (&DECL_INITIAL (var), global_unused_vars); num = VEC_length (tree, cfun->local_decls); Index: testsuite/g++.dg/tree-ssa/pr50741.C =================================================================== --- testsuite/g++.dg/tree-ssa/pr50741.C (revision 0) +++ testsuite/g++.dg/tree-ssa/pr50741.C (revision 0) @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -g" } */ +/* PR middle-end/50741 */ + +struct PublishLo +{ + const char *functionName; + ~PublishLo(); +}; +struct A { A(); }; +A::A() +{ + static PublishLo _rL_53 = {__FUNCTION__}; +}