]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization/108076 - if-conversion and forced labels
authorRichard Biener <rguenther@suse.de>
Mon, 12 Dec 2022 16:52:46 +0000 (17:52 +0100)
committerRichard Biener <rguenther@suse.de>
Tue, 24 Jan 2023 13:36:36 +0000 (14:36 +0100)
When doing if-conversion we simply throw away labels without checking
whether they are possibly targets of non-local gotos or have their
address taken.  The following rectifies this and refuses to if-convert
such loops.

PR tree-optimization/108076
* tree-if-conv.cc (if_convertible_loop_p_1): Reject blocks
with non-local or forced labels that we later remove
labels from.

* gcc.dg/torture/pr108076.c: New testcase.

(cherry picked from commit b4fddbe9592e9feb37ce567d90af822b75995531)

gcc/testsuite/gcc.dg/torture/pr108076.c [new file with mode: 0644]
gcc/tree-if-conv.cc

diff --git a/gcc/testsuite/gcc.dg/torture/pr108076.c b/gcc/testsuite/gcc.dg/torture/pr108076.c
new file mode 100644 (file)
index 0000000..ebe2e51
--- /dev/null
@@ -0,0 +1,17 @@
+/* { dg-do link } */
+
+static void *j;
+int v, g;
+__attribute__((__leaf__)) int atoi (const char *);
+
+int
+main ()
+{
+  j = &&lab1;
+  &&lab2;
+  atoi ("42");
+lab1:
+lab2:
+  if (v)
+    goto *j;
+}
index 7495ed653c0fc2f52433b70a86fefdc0dde4aeae..3574c673ed87069bc0e952fe13c22f6f5aa823fb 100644 (file)
@@ -1431,10 +1431,20 @@ if_convertible_loop_p_1 (class loop *loop, vec<data_reference_p> *refs)
       basic_block bb = ifc_bbs[i];
       gimple_stmt_iterator gsi;
 
+      bool may_have_nonlocal_labels
+       = bb_with_exit_edge_p (loop, bb) || bb == loop->latch;
       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
        switch (gimple_code (gsi_stmt (gsi)))
          {
          case GIMPLE_LABEL:
+           if (!may_have_nonlocal_labels)
+             {
+               tree label
+                 = gimple_label_label (as_a <glabel *> (gsi_stmt (gsi)));
+               if (DECL_NONLOCAL (label) || FORCED_LABEL (label))
+                 return false;
+             }
+           /* Fallthru.  */
          case GIMPLE_ASSIGN:
          case GIMPLE_CALL:
          case GIMPLE_DEBUG:
@@ -2646,8 +2656,8 @@ remove_conditions_and_labels (loop_p loop)
       basic_block bb = ifc_bbs[i];
 
       if (bb_with_exit_edge_p (loop, bb)
-        || bb == loop->latch)
-      continue;
+         || bb == loop->latch)
+       continue;
 
       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
        switch (gimple_code (gsi_stmt (gsi)))