public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Richard Biener <rguenth@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-9064] tree-optimization/108076 - if-conversion and forced labels
Date: Tue, 24 Jan 2023 14:27:28 +0000 (GMT)	[thread overview]
Message-ID: <20230124142728.CC946385802F@sourceware.org> (raw)

https://gcc.gnu.org/g:a3dfcaac560f5589028fdd967bfaa60d84c265ac

commit r12-9064-ga3dfcaac560f5589028fdd967bfaa60d84c265ac
Author: Richard Biener <rguenther@suse.de>
Date:   Mon Dec 12 17:52:46 2022 +0100

    tree-optimization/108076 - if-conversion and forced labels
    
    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)

Diff:
---
 gcc/testsuite/gcc.dg/torture/pr108076.c | 17 +++++++++++++++++
 gcc/tree-if-conv.cc                     | 14 ++++++++++++--
 2 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/torture/pr108076.c b/gcc/testsuite/gcc.dg/torture/pr108076.c
new file mode 100644
index 00000000000..ebe2e51bee0
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr108076.c
@@ -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;
+}
diff --git a/gcc/tree-if-conv.cc b/gcc/tree-if-conv.cc
index 7495ed653c0..3574c673ed8 100644
--- a/gcc/tree-if-conv.cc
+++ b/gcc/tree-if-conv.cc
@@ -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)))

                 reply	other threads:[~2023-01-24 14:27 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230124142728.CC946385802F@sourceware.org \
    --to=rguenth@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).