diff --git a/gcc/testsuite/gcc.dg/torture/pr112444.c b/gcc/testsuite/gcc.dg/torture/pr112444.c
new file mode 100644
index 00000000000..61c7ec9b22e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr112444.c
@@ -0,0 +1,65 @@
+/* { dg-do compile } */
+/* { dg-additional-options "-ftrivial-auto-var-init=zero" } */
+
+int qmi_message_pbm_get_all_capabilities_output_capability_basic_information_get_printable_message_offset,
+    qmi_message_pbm_get_all_capabilities_output_capability_basic_information_get_printable_message_init_offset,
+    qmi_message_pbm_get_all_capabilities_output_capability_basic_information_get_printable_message_error,
+    qmi_message_pbm_get_all_capabilities_output_capability_basic_information_get_printable_message_phonebooks_i;
+
+int g_string_new(), g_string_append_len(), qmi_message_tlv_read_guint8();
+
+void qmi_message_pbm_get_all_capabilities_output_capability_basic_information_get_printable_message() {
+  int printable = g_string_new();
+  for (;;) {
+    {
+      if (__builtin_expect(({
+                             int _g_boolean_var_4;
+                             if (printable)
+                               _g_boolean_var_4 = 1;
+                             else
+                               _g_boolean_var_4 = 0;
+                             _g_boolean_var_4;
+                           }),
+                           0))
+        g_string_append_len();
+    }
+    unsigned char tmp;
+    qmi_message_tlv_read_guint8(
+        qmi_message_pbm_get_all_capabilities_output_capability_basic_information_get_printable_message_init_offset,
+        qmi_message_pbm_get_all_capabilities_output_capability_basic_information_get_printable_message_offset,
+        tmp,
+        qmi_message_pbm_get_all_capabilities_output_capability_basic_information_get_printable_message_error);
+    {
+      {
+        if (__builtin_expect(({
+                               int _g_boolean_var_4;
+                               if (printable)
+                                 _g_boolean_var_4 = 1;
+                               else
+                                 _g_boolean_var_4 = 0;
+                               _g_boolean_var_4;
+                             }),
+                             0))
+          g_string_append_len();
+      }
+    }
+    for (
+        ;
+        qmi_message_pbm_get_all_capabilities_output_capability_basic_information_get_printable_message_phonebooks_i;) {
+      unsigned char tmp;
+      qmi_message_tlv_read_guint8(&tmp);
+      {
+        if (__builtin_expect(({
+                               int _g_boolean_var_4;
+                               if (printable)
+                                 _g_boolean_var_4 = 1;
+                               else
+                                 _g_boolean_var_4 = 0;
+                               _g_boolean_var_4;
+                             }),
+                             0))
+          g_string_append_len();
+      }
+    }
+  }
+}
diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc
index 0b2c10dcc1a..11537fa3e0b 100644
--- a/gcc/tree-ssa-sccvn.cc
+++ b/gcc/tree-ssa-sccvn.cc
@@ -5911,6 +5911,7 @@ static bool
 visit_phi (gimple *phi, bool *inserted, bool backedges_varying_p)
 {
   tree result, sameval = VN_TOP, seen_undef = NULL_TREE;
+  bool seen_undef_visited = false;
   tree backedge_val = NULL_TREE;
   bool seen_non_backedge = false;
   tree sameval_base = NULL_TREE;
@@ -5941,10 +5942,12 @@ visit_phi (gimple *phi, bool *inserted, bool backedges_varying_p)
 	if (def == PHI_RESULT (phi))
 	  continue;
 	++n_executable;
+	bool visited = true;
 	if (TREE_CODE (def) == SSA_NAME)
 	  {
+	    tree val = SSA_VAL (def, &visited);
 	    if (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK))
-	      def = SSA_VAL (def);
+	      def = val;
 	    if (e->flags & EDGE_DFS_BACK)
 	      backedge_val = def;
 	  }
@@ -5956,7 +5959,16 @@ visit_phi (gimple *phi, bool *inserted, bool backedges_varying_p)
 	else if (TREE_CODE (def) == SSA_NAME
 		 && ! virtual_operand_p (def)
 		 && ssa_undefined_value_p (def, false))
-	  seen_undef = def;
+	  {
+	    if (!seen_undef
+		/* Avoid having not visited undefined defs if we also have
+		   a visited one.  */
+		|| (!seen_undef_visited && visited))
+	      {
+		seen_undef = def;
+		seen_undef_visited = visited;
+	      }
+	  }
 	else if (sameval == VN_TOP)
 	  {
 	    sameval = def;
