public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-9910] c++: Fix ANNOTATE_EXPR instantiation [PR114409]
@ 2024-04-11  7:48 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2024-04-11  7:48 UTC (permalink / raw)
  To: gcc-cvs

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

commit r14-9910-gcb46aca0a07355abf2f0b04f52087bca8f848524
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Apr 11 09:46:00 2024 +0200

    c++: Fix ANNOTATE_EXPR instantiation [PR114409]
    
    The following testcase ICEs starting with the r14-4229 PR111529
    change which moved ANNOTATE_EXPR handling from tsubst_expr to
    tsubst_copy_and_build.
    ANNOTATE_EXPR is only allowed in the IL to wrap a loop condition,
    and the loop condition of while/for loops can be a COMPOUND_EXPR
    with DECL_EXPR in the first operand and the corresponding VAR_DECL
    in the second, as created by finish_cond
          else if (!empty_expr_stmt_p (cond))
            expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
    Since then Patrick reworked the instantiation, so that we have now
    tsubst_stmt and tsubst_expr and ANNOTATE_EXPR ended up in the latter,
    while only tsubst_stmt can handle DECL_EXPR.
    
    Now, the reason why the while/for loops with variable declaration
    in the condition works in templates without the pragmas (i.e. without
    ANNOTATE_EXPR) is that both the FOR_STMT and WHILE_STMT handling uses
    RECUR aka tsubst_stmt in handling of the *_COND operand:
        case FOR_STMT:
          stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
          RECUR (FOR_INIT_STMT (t));
          finish_init_stmt (stmt);
          tmp = RECUR (FOR_COND (t));
          finish_for_cond (tmp, stmt, false, 0, false);
    and
        case WHILE_STMT:
          stmt = begin_while_stmt ();
          tmp = RECUR (WHILE_COND (t));
          finish_while_stmt_cond (tmp, stmt, false, 0, false);
    Therefore, it will handle DECL_EXPR embedded in COMPOUND_EXPR of the
    {WHILE,FOR}_COND just fine.
    But if that COMPOUND_EXPR with DECL_EXPR is wrapped with one or more
    ANNOTATE_EXPRs, because ANNOTATE_EXPR is now done solely in tsubst_expr
    and uses RECUR there (i.e. tsubst_expr), it will ICE on DECL_EXPR in there.
    
    This could be fixed by keeping ANNOTATE_EXPR handling in tsubst_expr but
    using tsubst_stmt for the first operand, but this patch instead
    moves ANNOTATE_EXPR handling to tsubst_stmt (and uses tsubst_expr for the
    second/third operand).
    
    2024-04-11  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/114409
            * pt.cc (tsubst_expr) <case ANNOTATE_EXPR>: Move to ...
            (tsubst_stmt) <case ANNOTATE_EXPR>: ... here.  Use tsubst_expr
            instead of RECUR for the last 2 arguments.
    
            * g++.dg/ext/pr114409-2.C: New test.

Diff:
---
 gcc/cp/pt.cc                          | 30 ++++++++++++++++-------------
 gcc/testsuite/g++.dg/ext/pr114409-2.C | 36 +++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 13 deletions(-)

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index bf4b89d8413..767778e53ef 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -19433,6 +19433,23 @@ tsubst_stmt (tree t, tree args, tsubst_flags_t complain, tree in_decl)
     case PREDICT_EXPR:
       RETURN (add_stmt (copy_node (t)));
 
+    case ANNOTATE_EXPR:
+      {
+	/* Although ANNOTATE_EXPR is an expression, it can only appear in
+	   WHILE_COND, DO_COND or FOR_COND expressions, which are tsubsted
+	   using tsubst_stmt rather than tsubst_expr and can contain
+	   DECL_EXPRs.  */
+	tree op1 = RECUR (TREE_OPERAND (t, 0));
+	tree op2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
+	tree op3 = tsubst_expr (TREE_OPERAND (t, 2), args, complain, in_decl);
+	if (TREE_CODE (op2) == INTEGER_CST
+	    && wi::to_widest (op2) == (int) annot_expr_unroll_kind)
+	  op3 = cp_check_pragma_unroll (EXPR_LOCATION (TREE_OPERAND (t, 2)),
+					op3);
+	RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
+			    TREE_TYPE (op1), op1, op2, op3));
+      }
+
     default:
       gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
 
@@ -21772,19 +21789,6 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
 	RETURN (op);
       }
 
-    case ANNOTATE_EXPR:
-      {
-	op1 = RECUR (TREE_OPERAND (t, 0));
-	tree op2 = RECUR (TREE_OPERAND (t, 1));
-	tree op3 = RECUR (TREE_OPERAND (t, 2));
-	if (TREE_CODE (op2) == INTEGER_CST
-	    && wi::to_widest (op2) == (int) annot_expr_unroll_kind)
-	  op3 = cp_check_pragma_unroll (EXPR_LOCATION (TREE_OPERAND (t, 2)),
-					op3);
-	RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
-			    TREE_TYPE (op1), op1, op2, op3));
-      }
-
     default:
       /* Handle Objective-C++ constructs, if appropriate.  */
       if (tree subst = objcp_tsubst_expr (t, args, complain, in_decl))
diff --git a/gcc/testsuite/g++.dg/ext/pr114409-2.C b/gcc/testsuite/g++.dg/ext/pr114409-2.C
new file mode 100644
index 00000000000..3c2bb984fc3
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/pr114409-2.C
@@ -0,0 +1,36 @@
+// PR c++/114409
+// { dg-do compile }
+// { dg-options "-O2" }
+
+template <typename T>
+T
+foo (T)
+{
+  static T t;
+  return 42 - ++t;
+}
+
+template <typename T>
+void
+bar (T x)
+{
+  #pragma GCC novector
+  while (T y = foo (x))
+    ++y;
+}
+
+template <typename T>
+void
+baz (T x)
+{
+  #pragma GCC novector
+  for (; T y = foo (x); )
+    ++y;
+}
+
+void
+qux ()
+{
+  bar (0);
+  baz (0);
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-04-11  7:48 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-11  7:48 [gcc r14-9910] c++: Fix ANNOTATE_EXPR instantiation [PR114409] Jakub Jelinek

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).