The following avoids spurious uninit diagnostics for SSA name
copies which mostly appear when the source is marked as abnormal
which prevents copy propagation.

To prevent regressions I remove the bail out for anonymous SSA
names in the PHI arg place from warn_uninitialized_phi leaving
that to warn_uninit where I handle SSA copies from a SSA name
which isn't anonymous.  In theory this might cause more
valid and false positive diagnostics to pop up.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.

        PR tree-optimization/112909
        * tree-ssa-uninit.cc (find_uninit_use): Look through a
        single level of SSA name copies with single use.

        * gcc.dg/uninit-pr112909.c: New testcase.
---
 gcc/testsuite/gcc.dg/uninit-pr112909.c | 28 +++++++++++++++
 gcc/tree-ssa-uninit.cc                 | 47 ++++++++++++++++++++------
 2 files changed, 64 insertions(+), 11 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/uninit-pr112909.c

diff --git a/gcc/testsuite/gcc.dg/uninit-pr112909.c 
b/gcc/testsuite/gcc.dg/uninit-pr112909.c
new file mode 100644
index 00000000000..d2998f715aa
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/uninit-pr112909.c
@@ -0,0 +1,28 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -Wuninitialized" } */
+
+struct machine_thread_all_state {
+  int set;
+} _hurd_setup_sighandler_state;
+int _hurd_setup_sighandler_ss_0;
+struct {
+  int ctx;
+} *_hurd_setup_sighandler_stackframe;
+void _setjmp();
+void __thread_get_state();
+int machine_get_basic_state(struct machine_thread_all_state *state) {
+  if (state->set)
+    __thread_get_state();
+  return 1;
+}
+int *_hurd_setup_sighandler() {
+  int *scp;                            /* { dg-bogus "used uninitialized" } */
+  if (_hurd_setup_sighandler_ss_0) {
+    _setjmp();
+    _hurd_setup_sighandler_state.set |= 5;
+  }
+  machine_get_basic_state(&_hurd_setup_sighandler_state);
+  scp = &_hurd_setup_sighandler_stackframe->ctx;
+  _setjmp();
+  return scp;
+}
diff --git a/gcc/tree-ssa-uninit.cc b/gcc/tree-ssa-uninit.cc
index f42f76cd5c6..9a7c7d12dd8 100644
--- a/gcc/tree-ssa-uninit.cc
+++ b/gcc/tree-ssa-uninit.cc
@@ -204,14 +204,29 @@ warn_uninit (opt_code opt, tree t, tree var, gimple 
*context,
     {
       var_def_stmt = SSA_NAME_DEF_STMT (t);
 
-      if (is_gimple_assign (var_def_stmt)
-         && gimple_assign_rhs_code (var_def_stmt) == COMPLEX_EXPR)
+      if (gassign *ass = dyn_cast <gassign *> (var_def_stmt))
        {
-         tree v = gimple_assign_rhs1 (var_def_stmt);
-         if (TREE_CODE (v) == SSA_NAME
-             && has_undefined_value_p (v)
-             && zerop (gimple_assign_rhs2 (var_def_stmt)))
-           var = SSA_NAME_VAR (v);
+         switch (gimple_assign_rhs_code (var_def_stmt))
+           {
+           case COMPLEX_EXPR:
+             {
+               tree v = gimple_assign_rhs1 (ass);
+               if (TREE_CODE (v) == SSA_NAME
+                   && has_undefined_value_p (v)
+                   && zerop (gimple_assign_rhs2 (ass)))
+                 var = SSA_NAME_VAR (v);
+               break;
+             }
+           case SSA_NAME:
+             {
+               tree v = gimple_assign_rhs1 (ass);
+               if (TREE_CODE (v) == SSA_NAME
+                   && SSA_NAME_VAR (v))
+                 var = SSA_NAME_VAR (v);
+               break;
+             }
+           default:;
+           }
        }
 
       if (gimple_call_internal_p (var_def_stmt, IFN_DEFERRED_INIT))
@@ -1229,6 +1244,18 @@ find_uninit_use (gphi *phi, unsigned uninit_opnds, int 
*bb_to_rpo)
       if (is_gimple_debug (use_stmt))
        continue;
 
+      /* Look through a single level of SSA name copies.  This is
+        important for copies involving abnormals which we can't always
+        proapgate out but which result in spurious unguarded uses.  */
+      use_operand_p use2_p;
+      gimple *use2_stmt;
+      if (gimple_assign_ssa_name_copy_p (use_stmt)
+         && single_imm_use (gimple_assign_lhs (use_stmt), &use2_p, &use2_stmt))
+       {
+         use_p = use2_p;
+         use_stmt = use2_stmt;
+       }
+
       if (gphi *use_phi = dyn_cast<gphi *> (use_stmt))
        {
          unsigned idx = PHI_ARG_INDEX_FROM_USE (use_p);
@@ -1262,9 +1289,9 @@ find_uninit_use (gphi *phi, unsigned uninit_opnds, int 
*bb_to_rpo)
                       e->src->index, e->dest->index);
              print_gimple_stmt (dump_file, use_stmt, 0);
            }
-         /* Found a phi use that is not guarded, mark the phi_result as
+         /* Found a phi use that is not guarded, mark the use as
             possibly undefined.  */
-         possibly_undefined_names->add (phi_result);
+         possibly_undefined_names->add (USE_FROM_PTR (use_p));
        }
       else
        cands.safe_push (use_stmt);
@@ -1318,8 +1345,6 @@ warn_uninitialized_phi (gphi *phi, unsigned uninit_opnds, 
int *bb_to_rpo)
 
   unsigned phiarg_index = MASK_FIRST_SET_BIT (uninit_opnds);
   tree uninit_op = gimple_phi_arg_def (phi, phiarg_index);
-  if (SSA_NAME_VAR (uninit_op) == NULL_TREE)
-    return;
 
   location_t loc = UNKNOWN_LOCATION;
   if (gimple_phi_arg_has_location (phi, phiarg_index))
-- 
2.35.3

Reply via email to