tree-optimization/112444 - avoid bougs PHI value-numbering

Message ID 20231109105114.3A7EC385B800@sourceware.org
State Accepted
Headers
Series tree-optimization/112444 - avoid bougs PHI value-numbering |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Richard Biener Nov. 9, 2023, 10:50 a.m. UTC
  With .DEFERRED_INIT ssa_undefined_value_p () can return true for
values we did not visit (because they proved unreachable) but
are not .VN_TOP.  Avoid using those as value which, because they
are not visited, are assumed to be defined outside of the region.

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

	PR tree-optimization/112444
	* tree-ssa-sccvn.cc (visit_phi): Avoid using not visited
	defs as undefined vals.

	* gcc.dg/torture/pr112444.c: New testcase.
---
 gcc/testsuite/gcc.dg/torture/pr112444.c | 65 +++++++++++++++++++++++++
 gcc/tree-ssa-sccvn.cc                   | 16 +++++-
 2 files changed, 79 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr112444.c
  

Patch

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;