public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: Fix further protected_set_expr_location related -fcompare-debug issues [PR94441]
@ 2020-04-02  7:48 Jakub Jelinek
  2020-04-03 19:02 ` Jason Merrill
  0 siblings, 1 reply; 4+ messages in thread
From: Jakub Jelinek @ 2020-04-02  7:48 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches

Hi!

My recent protected_set_expr_location changes work well when
that function is called unconditionally, but as the testcase shows, the C++
FE has a few spots that do:
  if (!EXPR_HAS_LOCATION (stmt))
    protected_set_expr_location (stmt, locus);
or similar.  Now, if we have for -g0 stmt of some expression that can
have location and has != UNKNOWN_LOCATION, while -g instead has
a STATEMENT_LIST containing some DEBUG_BEGIN_STMTs + that expression with
that location, we don't call protected_set_expr_location in the -g0 case,
but do call it in the -g case, because on the STATEMENT_LIST
!EXPR_HAS_LOCATION.
The following patch introduces a helper function which digs up the single
expression of a STATEMENT_LIST and uses that expression in the
EXPR_HAS_LOCATION check (plus changes protected_set_expr_location to
also use that helper).

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

Or do we want a further wrapper, perhaps C++ FE only, that would do this
protected_set_expr_location_if_unset (stmt, locus)?

2020-04-02  Jakub Jelinek  <jakub@redhat.com>

	PR debug/94441
	* tree-iterator.h (expr_single): Declare.
	* tree-iterator.c (expr_single): New function.
	* tree.c (protected_set_expr_location): Use it.

	* parser.c (cp_parser_omp_for_loop): Use expr_single.
	* cp-gimplify.c (genericize_if_stmt, genericize_cp_loop): Likewise.

	* g++.dg/opt/pr94441.C: New test.

--- gcc/tree-iterator.h.jj	2020-01-12 11:54:38.497381967 +0100
+++ gcc/tree-iterator.h	2020-04-01 16:58:00.034347283 +0200
@@ -119,5 +119,6 @@ extern void append_to_statement_list (tr
 extern void append_to_statement_list_force (tree, tree *);
 extern tree expr_first (tree);
 extern tree expr_last (tree);
+extern tree expr_single (tree);
 
 #endif /* GCC_TREE_ITERATOR_H  */
--- gcc/tree-iterator.c.jj	2020-01-12 11:54:38.497381967 +0100
+++ gcc/tree-iterator.c	2020-04-01 17:05:23.388920122 +0200
@@ -354,4 +354,45 @@ expr_last (tree expr)
   return expr;
 }
 
+/* If EXPR is a STATEMENT_LIST containing just DEBUG_BEGIN_STMTs and
+   a single other stmt, return that other stmt (recursively).
+   If it is a STATEMENT_LIST containing no non-DEBUG_BEGIN_STMTs or
+   multiple, return NULL_TREE.
+   Otherwise return EXPR.  */
+
+tree
+expr_single (tree expr)
+{
+  if (expr == NULL_TREE)
+    return expr;
+
+  if (TREE_CODE (expr) == STATEMENT_LIST)
+    {
+      /* With -gstatement-frontiers we could have a STATEMENT_LIST with
+	 DEBUG_BEGIN_STMT(s) and only a single other stmt, which with
+	 -g wouldn't be present and we'd have that single other stmt
+	 directly instead.  */
+      struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (expr);
+      if (!n)
+	return NULL_TREE;
+      while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT)
+	{
+	  n = n->next;
+	  if (!n)
+	    return NULL_TREE;
+	}
+      expr = n->stmt;
+      do
+	{
+	  n = n->next;
+	  if (!n)
+	    return expr_single (expr);
+	}
+      while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT);
+      return NULL_TREE;
+    }
+
+  return expr;
+}
+
 #include "gt-tree-iterator.h"
--- gcc/tree.c.jj	2020-03-26 10:35:29.984667559 +0100
+++ gcc/tree.c	2020-04-01 16:59:01.689453490 +0200
@@ -5148,30 +5148,9 @@ protected_set_expr_location (tree t, loc
     SET_EXPR_LOCATION (t, loc);
   else if (t && TREE_CODE (t) == STATEMENT_LIST)
     {
-      /* With -gstatement-frontiers we could have a STATEMENT_LIST with
-	 DEBUG_BEGIN_STMT(s) and only a single other stmt, which with
-	 -g wouldn't be present and we'd have that single other stmt
-	 directly instead.  */
-      struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (t);
-      if (!n)
-	return;
-      while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT)
-	{
-	  n = n->next;
-	  if (!n)
-	    return;
-	}
-      tree t2 = n->stmt;
-      do
-	{
-	  n = n->next;
-	  if (!n)
-	    {
-	      protected_set_expr_location (t2, loc);
-	      return;
-	    }
-	}
-      while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT);
+      t = expr_single (t);
+      if (t && CAN_HAVE_LOCATION_P (t))
+	SET_EXPR_LOCATION (t, loc);
     }
 }
 
--- gcc/cp/parser.c.jj	2020-03-29 11:14:41.087556278 +0200
+++ gcc/cp/parser.c	2020-04-01 17:03:00.730988186 +0200
@@ -39149,8 +39149,9 @@ cp_parser_omp_for_loop (cp_parser *parse
 	    incr = cp_parser_omp_for_incr (parser, real_decl);
 	  else
 	    incr = cp_parser_expression (parser);
-	  if (!EXPR_HAS_LOCATION (incr))
-	    protected_set_expr_location (incr, input_location);
+	  if (tree incrl = expr_single (incr))
+	    if (!EXPR_HAS_LOCATION (incrl))
+	      protected_set_expr_location (incrl, input_location);
 	}
 
     parse_close_paren:
--- gcc/cp/cp-gimplify.c.jj	2020-03-29 11:14:41.057556738 +0200
+++ gcc/cp/cp-gimplify.c	2020-04-01 17:01:28.916319192 +0200
@@ -226,8 +226,9 @@ genericize_if_stmt (tree *stmt_p)
     stmt = else_;
   else
     stmt = build3 (COND_EXPR, void_type_node, cond, then_, else_);
-  if (!EXPR_HAS_LOCATION (stmt))
-    protected_set_expr_location (stmt, locus);
+  if (tree stmtl = expr_single (stmt))
+    if (!EXPR_HAS_LOCATION (stmtl))
+      protected_set_expr_location (stmtl, locus);
   *stmt_p = stmt;
 }
 
@@ -248,8 +249,9 @@ genericize_cp_loop (tree *stmt_p, locati
   tree stmt_list = NULL;
   tree debug_begin = NULL;
 
-  if (EXPR_LOCATION (incr) == UNKNOWN_LOCATION)
-    protected_set_expr_location (incr, start_locus);
+  if (tree incrl = expr_single (incr))
+    if (!EXPR_HAS_LOCATION (incrl))
+      protected_set_expr_location (incrl, start_locus);
 
   cp_walk_tree (&cond, cp_genericize_r, data, NULL);
   cp_walk_tree (&incr, cp_genericize_r, data, NULL);
--- gcc/testsuite/g++.dg/opt/pr94441.C.jj	2020-04-01 17:13:43.014677438 +0200
+++ gcc/testsuite/g++.dg/opt/pr94441.C	2020-04-01 17:14:33.495946020 +0200
@@ -0,0 +1,16 @@
+// PR debug/94441
+// { dg-do compile }
+// { dg-options "-O3 -fno-forward-propagate --param=max-cse-insns=0 -flive-range-shrinkage -std=c++17 -fcompare-debug" }
+
+template <class,class> struct Same;
+template <class T> struct Same<T,T> {};
+
+auto f()
+{
+  if constexpr (sizeof(int)==3)
+    return 42;
+  else
+    return 42L;
+}
+
+Same<decltype(f()), long> s;

	Jakub


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] c++: Fix further protected_set_expr_location related -fcompare-debug issues [PR94441]
  2020-04-02  7:48 [PATCH] c++: Fix further protected_set_expr_location related -fcompare-debug issues [PR94441] Jakub Jelinek
@ 2020-04-03 19:02 ` Jason Merrill
  2020-04-03 20:12   ` [PATCH v2] " Jakub Jelinek
  0 siblings, 1 reply; 4+ messages in thread
From: Jason Merrill @ 2020-04-03 19:02 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: gcc-patches

On 4/2/20 3:48 AM, Jakub Jelinek wrote:
> Hi!
> 
> My recent protected_set_expr_location changes work well when
> that function is called unconditionally, but as the testcase shows, the C++
> FE has a few spots that do:
>    if (!EXPR_HAS_LOCATION (stmt))
>      protected_set_expr_location (stmt, locus);
> or similar.  Now, if we have for -g0 stmt of some expression that can
> have location and has != UNKNOWN_LOCATION, while -g instead has
> a STATEMENT_LIST containing some DEBUG_BEGIN_STMTs + that expression with
> that location, we don't call protected_set_expr_location in the -g0 case,
> but do call it in the -g case, because on the STATEMENT_LIST
> !EXPR_HAS_LOCATION.
> The following patch introduces a helper function which digs up the single
> expression of a STATEMENT_LIST and uses that expression in the
> EXPR_HAS_LOCATION check (plus changes protected_set_expr_location to
> also use that helper).
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
> 
> Or do we want a further wrapper, perhaps C++ FE only, that would do this
> protected_set_expr_location_if_unset (stmt, locus)?

That sounds good to me.

> 2020-04-02  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR debug/94441
> 	* tree-iterator.h (expr_single): Declare.
> 	* tree-iterator.c (expr_single): New function.
> 	* tree.c (protected_set_expr_location): Use it.
> 
> 	* parser.c (cp_parser_omp_for_loop): Use expr_single.
> 	* cp-gimplify.c (genericize_if_stmt, genericize_cp_loop): Likewise.
> 
> 	* g++.dg/opt/pr94441.C: New test.
> 
> --- gcc/tree-iterator.h.jj	2020-01-12 11:54:38.497381967 +0100
> +++ gcc/tree-iterator.h	2020-04-01 16:58:00.034347283 +0200
> @@ -119,5 +119,6 @@ extern void append_to_statement_list (tr
>   extern void append_to_statement_list_force (tree, tree *);
>   extern tree expr_first (tree);
>   extern tree expr_last (tree);
> +extern tree expr_single (tree);
>   
>   #endif /* GCC_TREE_ITERATOR_H  */
> --- gcc/tree-iterator.c.jj	2020-01-12 11:54:38.497381967 +0100
> +++ gcc/tree-iterator.c	2020-04-01 17:05:23.388920122 +0200
> @@ -354,4 +354,45 @@ expr_last (tree expr)
>     return expr;
>   }
>   
> +/* If EXPR is a STATEMENT_LIST containing just DEBUG_BEGIN_STMTs and
> +   a single other stmt, return that other stmt (recursively).
> +   If it is a STATEMENT_LIST containing no non-DEBUG_BEGIN_STMTs or
> +   multiple, return NULL_TREE.
> +   Otherwise return EXPR.  */
> +
> +tree
> +expr_single (tree expr)
> +{
> +  if (expr == NULL_TREE)
> +    return expr;
> +
> +  if (TREE_CODE (expr) == STATEMENT_LIST)
> +    {
> +      /* With -gstatement-frontiers we could have a STATEMENT_LIST with
> +	 DEBUG_BEGIN_STMT(s) and only a single other stmt, which with
> +	 -g wouldn't be present and we'd have that single other stmt
> +	 directly instead.  */
> +      struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (expr);
> +      if (!n)
> +	return NULL_TREE;
> +      while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT)
> +	{
> +	  n = n->next;
> +	  if (!n)
> +	    return NULL_TREE;
> +	}
> +      expr = n->stmt;
> +      do
> +	{
> +	  n = n->next;
> +	  if (!n)
> +	    return expr_single (expr);
> +	}
> +      while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT);
> +      return NULL_TREE;
> +    }
> +
> +  return expr;
> +}
> +
>   #include "gt-tree-iterator.h"
> --- gcc/tree.c.jj	2020-03-26 10:35:29.984667559 +0100
> +++ gcc/tree.c	2020-04-01 16:59:01.689453490 +0200
> @@ -5148,30 +5148,9 @@ protected_set_expr_location (tree t, loc
>       SET_EXPR_LOCATION (t, loc);
>     else if (t && TREE_CODE (t) == STATEMENT_LIST)
>       {
> -      /* With -gstatement-frontiers we could have a STATEMENT_LIST with
> -	 DEBUG_BEGIN_STMT(s) and only a single other stmt, which with
> -	 -g wouldn't be present and we'd have that single other stmt
> -	 directly instead.  */
> -      struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (t);
> -      if (!n)
> -	return;
> -      while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT)
> -	{
> -	  n = n->next;
> -	  if (!n)
> -	    return;
> -	}
> -      tree t2 = n->stmt;
> -      do
> -	{
> -	  n = n->next;
> -	  if (!n)
> -	    {
> -	      protected_set_expr_location (t2, loc);
> -	      return;
> -	    }
> -	}
> -      while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT);
> +      t = expr_single (t);
> +      if (t && CAN_HAVE_LOCATION_P (t))
> +	SET_EXPR_LOCATION (t, loc);
>       }
>   }
>   
> --- gcc/cp/parser.c.jj	2020-03-29 11:14:41.087556278 +0200
> +++ gcc/cp/parser.c	2020-04-01 17:03:00.730988186 +0200
> @@ -39149,8 +39149,9 @@ cp_parser_omp_for_loop (cp_parser *parse
>   	    incr = cp_parser_omp_for_incr (parser, real_decl);
>   	  else
>   	    incr = cp_parser_expression (parser);
> -	  if (!EXPR_HAS_LOCATION (incr))
> -	    protected_set_expr_location (incr, input_location);
> +	  if (tree incrl = expr_single (incr))
> +	    if (!EXPR_HAS_LOCATION (incrl))
> +	      protected_set_expr_location (incrl, input_location);
>   	}
>   
>       parse_close_paren:
> --- gcc/cp/cp-gimplify.c.jj	2020-03-29 11:14:41.057556738 +0200
> +++ gcc/cp/cp-gimplify.c	2020-04-01 17:01:28.916319192 +0200
> @@ -226,8 +226,9 @@ genericize_if_stmt (tree *stmt_p)
>       stmt = else_;
>     else
>       stmt = build3 (COND_EXPR, void_type_node, cond, then_, else_);
> -  if (!EXPR_HAS_LOCATION (stmt))
> -    protected_set_expr_location (stmt, locus);
> +  if (tree stmtl = expr_single (stmt))
> +    if (!EXPR_HAS_LOCATION (stmtl))
> +      protected_set_expr_location (stmtl, locus);
>     *stmt_p = stmt;
>   }
>   
> @@ -248,8 +249,9 @@ genericize_cp_loop (tree *stmt_p, locati
>     tree stmt_list = NULL;
>     tree debug_begin = NULL;
>   
> -  if (EXPR_LOCATION (incr) == UNKNOWN_LOCATION)
> -    protected_set_expr_location (incr, start_locus);
> +  if (tree incrl = expr_single (incr))
> +    if (!EXPR_HAS_LOCATION (incrl))
> +      protected_set_expr_location (incrl, start_locus);
>   
>     cp_walk_tree (&cond, cp_genericize_r, data, NULL);
>     cp_walk_tree (&incr, cp_genericize_r, data, NULL);
> --- gcc/testsuite/g++.dg/opt/pr94441.C.jj	2020-04-01 17:13:43.014677438 +0200
> +++ gcc/testsuite/g++.dg/opt/pr94441.C	2020-04-01 17:14:33.495946020 +0200
> @@ -0,0 +1,16 @@
> +// PR debug/94441
> +// { dg-do compile }
> +// { dg-options "-O3 -fno-forward-propagate --param=max-cse-insns=0 -flive-range-shrinkage -std=c++17 -fcompare-debug" }
> +
> +template <class,class> struct Same;
> +template <class T> struct Same<T,T> {};
> +
> +auto f()
> +{
> +  if constexpr (sizeof(int)==3)
> +    return 42;
> +  else
> +    return 42L;
> +}
> +
> +Same<decltype(f()), long> s;
> 
> 	Jakub
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2] c++: Fix further protected_set_expr_location related -fcompare-debug issues [PR94441]
  2020-04-03 19:02 ` Jason Merrill
@ 2020-04-03 20:12   ` Jakub Jelinek
  2020-04-04  4:13     ` Jason Merrill
  0 siblings, 1 reply; 4+ messages in thread
From: Jakub Jelinek @ 2020-04-03 20:12 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches

On Fri, Apr 03, 2020 at 03:02:58PM -0400, Jason Merrill via Gcc-patches wrote:
> > Or do we want a further wrapper, perhaps C++ FE only, that would do this
> > protected_set_expr_location_if_unset (stmt, locus)?
> 
> That sounds good to me.

So like this if it passes bootstrap/regtest?

2020-04-03  Jakub Jelinek  <jakub@redhat.com>

	PR debug/94441
	* tree-iterator.h (expr_single): Declare.
	* tree-iterator.c (expr_single): New function.
	* tree.h (protected_set_expr_location_if_unset): Declare.
	* tree.c (protected_set_expr_location): Use expr_single.
	(protected_set_expr_location_if_unset): New function.

	* parser.c (cp_parser_omp_for_loop): Use
	protected_set_expr_location_if_unset.
	* cp-gimplify.c (genericize_if_stmt, genericize_cp_loop): Likewise.

	* g++.dg/opt/pr94441.C: New test.

--- gcc/tree-iterator.h.jj	2020-04-02 12:53:41.556232418 +0200
+++ gcc/tree-iterator.h	2020-04-03 21:07:01.335933695 +0200
@@ -119,5 +119,6 @@ extern void append_to_statement_list (tr
 extern void append_to_statement_list_force (tree, tree *);
 extern tree expr_first (tree);
 extern tree expr_last (tree);
+extern tree expr_single (tree);
 
 #endif /* GCC_TREE_ITERATOR_H  */
--- gcc/tree-iterator.c.jj	2020-04-02 12:53:41.555232434 +0200
+++ gcc/tree-iterator.c	2020-04-03 21:07:01.335933695 +0200
@@ -354,4 +354,45 @@ expr_last (tree expr)
   return expr;
 }
 
+/* If EXPR is a STATEMENT_LIST containing just DEBUG_BEGIN_STMTs and
+   a single other stmt, return that other stmt (recursively).
+   If it is a STATEMENT_LIST containing no non-DEBUG_BEGIN_STMTs or
+   multiple, return NULL_TREE.
+   Otherwise return EXPR.  */
+
+tree
+expr_single (tree expr)
+{
+  if (expr == NULL_TREE)
+    return expr;
+
+  if (TREE_CODE (expr) == STATEMENT_LIST)
+    {
+      /* With -gstatement-frontiers we could have a STATEMENT_LIST with
+	 DEBUG_BEGIN_STMT(s) and only a single other stmt, which with
+	 -g wouldn't be present and we'd have that single other stmt
+	 directly instead.  */
+      struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (expr);
+      if (!n)
+	return NULL_TREE;
+      while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT)
+	{
+	  n = n->next;
+	  if (!n)
+	    return NULL_TREE;
+	}
+      expr = n->stmt;
+      do
+	{
+	  n = n->next;
+	  if (!n)
+	    return expr_single (expr);
+	}
+      while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT);
+      return NULL_TREE;
+    }
+
+  return expr;
+}
+
 #include "gt-tree-iterator.h"
--- gcc/tree.h.jj	2020-01-31 19:18:02.643900799 +0100
+++ gcc/tree.h	2020-04-03 21:09:26.134805791 +0200
@@ -1203,6 +1203,7 @@ get_expr_source_range (tree expr)
 }
 
 extern void protected_set_expr_location (tree, location_t);
+extern void protected_set_expr_location_if_unset (tree, location_t);
 
 extern tree maybe_wrap_with_location (tree, location_t);
 
--- gcc/tree.c.jj	2020-04-03 15:41:34.442645256 +0200
+++ gcc/tree.c	2020-04-03 21:08:57.422227740 +0200
@@ -5148,33 +5148,23 @@ protected_set_expr_location (tree t, loc
     SET_EXPR_LOCATION (t, loc);
   else if (t && TREE_CODE (t) == STATEMENT_LIST)
     {
-      /* With -gstatement-frontiers we could have a STATEMENT_LIST with
-	 DEBUG_BEGIN_STMT(s) and only a single other stmt, which with
-	 -g wouldn't be present and we'd have that single other stmt
-	 directly instead.  */
-      struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (t);
-      if (!n)
-	return;
-      while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT)
-	{
-	  n = n->next;
-	  if (!n)
-	    return;
-	}
-      tree t2 = n->stmt;
-      do
-	{
-	  n = n->next;
-	  if (!n)
-	    {
-	      protected_set_expr_location (t2, loc);
-	      return;
-	    }
-	}
-      while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT);
+      t = expr_single (t);
+      if (t && CAN_HAVE_LOCATION_P (t))
+	SET_EXPR_LOCATION (t, loc);
     }
 }
 
+/* Like PROTECTED_SET_EXPR_LOCATION, but only do that if T has
+   UNKNOWN_LOCATION.  */
+
+void
+protected_set_expr_location_if_unset (tree t, location_t loc)
+{
+  t = expr_single (t);
+  if (t && !EXPR_HAS_LOCATION (t))
+    protected_set_expr_location (t, loc);
+}
+
 /* Data used when collecting DECLs and TYPEs for language data removal.  */
 
 class free_lang_data_d
--- gcc/cp/parser.c.jj	2020-04-02 12:53:41.542232631 +0200
+++ gcc/cp/parser.c	2020-04-03 21:11:39.675843324 +0200
@@ -39149,8 +39149,7 @@ cp_parser_omp_for_loop (cp_parser *parse
 	    incr = cp_parser_omp_for_incr (parser, real_decl);
 	  else
 	    incr = cp_parser_expression (parser);
-	  if (!EXPR_HAS_LOCATION (incr))
-	    protected_set_expr_location (incr, input_location);
+	  protected_set_expr_location_if_unset (incr, input_location);
 	}
 
     parse_close_paren:
--- gcc/cp/cp-gimplify.c.jj	2020-04-02 12:53:41.530232814 +0200
+++ gcc/cp/cp-gimplify.c	2020-04-03 21:10:51.707548245 +0200
@@ -226,8 +226,7 @@ genericize_if_stmt (tree *stmt_p)
     stmt = else_;
   else
     stmt = build3 (COND_EXPR, void_type_node, cond, then_, else_);
-  if (!EXPR_HAS_LOCATION (stmt))
-    protected_set_expr_location (stmt, locus);
+  protected_set_expr_location_if_unset (stmt, locus);
   *stmt_p = stmt;
 }
 
@@ -248,8 +247,7 @@ genericize_cp_loop (tree *stmt_p, locati
   tree stmt_list = NULL;
   tree debug_begin = NULL;
 
-  if (EXPR_LOCATION (incr) == UNKNOWN_LOCATION)
-    protected_set_expr_location (incr, start_locus);
+  protected_set_expr_location_if_unset (incr, start_locus);
 
   cp_walk_tree (&cond, cp_genericize_r, data, NULL);
   cp_walk_tree (&incr, cp_genericize_r, data, NULL);
--- gcc/testsuite/g++.dg/opt/pr94441.C.jj	2020-04-03 21:07:01.346933532 +0200
+++ gcc/testsuite/g++.dg/opt/pr94441.C	2020-04-03 21:07:01.346933532 +0200
@@ -0,0 +1,16 @@
+// PR debug/94441
+// { dg-do compile }
+// { dg-options "-O3 -fno-forward-propagate --param=max-cse-insns=0 -flive-range-shrinkage -std=c++17 -fcompare-debug" }
+
+template <class,class> struct Same;
+template <class T> struct Same<T,T> {};
+
+auto f()
+{
+  if constexpr (sizeof(int)==3)
+    return 42;
+  else
+    return 42L;
+}
+
+Same<decltype(f()), long> s;

	Jakub


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] c++: Fix further protected_set_expr_location related -fcompare-debug issues [PR94441]
  2020-04-03 20:12   ` [PATCH v2] " Jakub Jelinek
@ 2020-04-04  4:13     ` Jason Merrill
  0 siblings, 0 replies; 4+ messages in thread
From: Jason Merrill @ 2020-04-04  4:13 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: gcc-patches

On 4/3/20 4:12 PM, Jakub Jelinek wrote:
> On Fri, Apr 03, 2020 at 03:02:58PM -0400, Jason Merrill via Gcc-patches wrote:
>>> Or do we want a further wrapper, perhaps C++ FE only, that would do this
>>> protected_set_expr_location_if_unset (stmt, locus)?
>>
>> That sounds good to me.
> 
> So like this if it passes bootstrap/regtest?

OK.

> 2020-04-03  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR debug/94441
> 	* tree-iterator.h (expr_single): Declare.
> 	* tree-iterator.c (expr_single): New function.
> 	* tree.h (protected_set_expr_location_if_unset): Declare.
> 	* tree.c (protected_set_expr_location): Use expr_single.
> 	(protected_set_expr_location_if_unset): New function.
> 
> 	* parser.c (cp_parser_omp_for_loop): Use
> 	protected_set_expr_location_if_unset.
> 	* cp-gimplify.c (genericize_if_stmt, genericize_cp_loop): Likewise.
> 
> 	* g++.dg/opt/pr94441.C: New test.
> 
> --- gcc/tree-iterator.h.jj	2020-04-02 12:53:41.556232418 +0200
> +++ gcc/tree-iterator.h	2020-04-03 21:07:01.335933695 +0200
> @@ -119,5 +119,6 @@ extern void append_to_statement_list (tr
>   extern void append_to_statement_list_force (tree, tree *);
>   extern tree expr_first (tree);
>   extern tree expr_last (tree);
> +extern tree expr_single (tree);
>   
>   #endif /* GCC_TREE_ITERATOR_H  */
> --- gcc/tree-iterator.c.jj	2020-04-02 12:53:41.555232434 +0200
> +++ gcc/tree-iterator.c	2020-04-03 21:07:01.335933695 +0200
> @@ -354,4 +354,45 @@ expr_last (tree expr)
>     return expr;
>   }
>   
> +/* If EXPR is a STATEMENT_LIST containing just DEBUG_BEGIN_STMTs and
> +   a single other stmt, return that other stmt (recursively).
> +   If it is a STATEMENT_LIST containing no non-DEBUG_BEGIN_STMTs or
> +   multiple, return NULL_TREE.
> +   Otherwise return EXPR.  */
> +
> +tree
> +expr_single (tree expr)
> +{
> +  if (expr == NULL_TREE)
> +    return expr;
> +
> +  if (TREE_CODE (expr) == STATEMENT_LIST)
> +    {
> +      /* With -gstatement-frontiers we could have a STATEMENT_LIST with
> +	 DEBUG_BEGIN_STMT(s) and only a single other stmt, which with
> +	 -g wouldn't be present and we'd have that single other stmt
> +	 directly instead.  */
> +      struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (expr);
> +      if (!n)
> +	return NULL_TREE;
> +      while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT)
> +	{
> +	  n = n->next;
> +	  if (!n)
> +	    return NULL_TREE;
> +	}
> +      expr = n->stmt;
> +      do
> +	{
> +	  n = n->next;
> +	  if (!n)
> +	    return expr_single (expr);
> +	}
> +      while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT);
> +      return NULL_TREE;
> +    }
> +
> +  return expr;
> +}
> +
>   #include "gt-tree-iterator.h"
> --- gcc/tree.h.jj	2020-01-31 19:18:02.643900799 +0100
> +++ gcc/tree.h	2020-04-03 21:09:26.134805791 +0200
> @@ -1203,6 +1203,7 @@ get_expr_source_range (tree expr)
>   }
>   
>   extern void protected_set_expr_location (tree, location_t);
> +extern void protected_set_expr_location_if_unset (tree, location_t);
>   
>   extern tree maybe_wrap_with_location (tree, location_t);
>   
> --- gcc/tree.c.jj	2020-04-03 15:41:34.442645256 +0200
> +++ gcc/tree.c	2020-04-03 21:08:57.422227740 +0200
> @@ -5148,33 +5148,23 @@ protected_set_expr_location (tree t, loc
>       SET_EXPR_LOCATION (t, loc);
>     else if (t && TREE_CODE (t) == STATEMENT_LIST)
>       {
> -      /* With -gstatement-frontiers we could have a STATEMENT_LIST with
> -	 DEBUG_BEGIN_STMT(s) and only a single other stmt, which with
> -	 -g wouldn't be present and we'd have that single other stmt
> -	 directly instead.  */
> -      struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (t);
> -      if (!n)
> -	return;
> -      while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT)
> -	{
> -	  n = n->next;
> -	  if (!n)
> -	    return;
> -	}
> -      tree t2 = n->stmt;
> -      do
> -	{
> -	  n = n->next;
> -	  if (!n)
> -	    {
> -	      protected_set_expr_location (t2, loc);
> -	      return;
> -	    }
> -	}
> -      while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT);
> +      t = expr_single (t);
> +      if (t && CAN_HAVE_LOCATION_P (t))
> +	SET_EXPR_LOCATION (t, loc);
>       }
>   }
>   
> +/* Like PROTECTED_SET_EXPR_LOCATION, but only do that if T has
> +   UNKNOWN_LOCATION.  */
> +
> +void
> +protected_set_expr_location_if_unset (tree t, location_t loc)
> +{
> +  t = expr_single (t);
> +  if (t && !EXPR_HAS_LOCATION (t))
> +    protected_set_expr_location (t, loc);
> +}
> +
>   /* Data used when collecting DECLs and TYPEs for language data removal.  */
>   
>   class free_lang_data_d
> --- gcc/cp/parser.c.jj	2020-04-02 12:53:41.542232631 +0200
> +++ gcc/cp/parser.c	2020-04-03 21:11:39.675843324 +0200
> @@ -39149,8 +39149,7 @@ cp_parser_omp_for_loop (cp_parser *parse
>   	    incr = cp_parser_omp_for_incr (parser, real_decl);
>   	  else
>   	    incr = cp_parser_expression (parser);
> -	  if (!EXPR_HAS_LOCATION (incr))
> -	    protected_set_expr_location (incr, input_location);
> +	  protected_set_expr_location_if_unset (incr, input_location);
>   	}
>   
>       parse_close_paren:
> --- gcc/cp/cp-gimplify.c.jj	2020-04-02 12:53:41.530232814 +0200
> +++ gcc/cp/cp-gimplify.c	2020-04-03 21:10:51.707548245 +0200
> @@ -226,8 +226,7 @@ genericize_if_stmt (tree *stmt_p)
>       stmt = else_;
>     else
>       stmt = build3 (COND_EXPR, void_type_node, cond, then_, else_);
> -  if (!EXPR_HAS_LOCATION (stmt))
> -    protected_set_expr_location (stmt, locus);
> +  protected_set_expr_location_if_unset (stmt, locus);
>     *stmt_p = stmt;
>   }
>   
> @@ -248,8 +247,7 @@ genericize_cp_loop (tree *stmt_p, locati
>     tree stmt_list = NULL;
>     tree debug_begin = NULL;
>   
> -  if (EXPR_LOCATION (incr) == UNKNOWN_LOCATION)
> -    protected_set_expr_location (incr, start_locus);
> +  protected_set_expr_location_if_unset (incr, start_locus);
>   
>     cp_walk_tree (&cond, cp_genericize_r, data, NULL);
>     cp_walk_tree (&incr, cp_genericize_r, data, NULL);
> --- gcc/testsuite/g++.dg/opt/pr94441.C.jj	2020-04-03 21:07:01.346933532 +0200
> +++ gcc/testsuite/g++.dg/opt/pr94441.C	2020-04-03 21:07:01.346933532 +0200
> @@ -0,0 +1,16 @@
> +// PR debug/94441
> +// { dg-do compile }
> +// { dg-options "-O3 -fno-forward-propagate --param=max-cse-insns=0 -flive-range-shrinkage -std=c++17 -fcompare-debug" }
> +
> +template <class,class> struct Same;
> +template <class T> struct Same<T,T> {};
> +
> +auto f()
> +{
> +  if constexpr (sizeof(int)==3)
> +    return 42;
> +  else
> +    return 42L;
> +}
> +
> +Same<decltype(f()), long> s;
> 
> 	Jakub
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-04-04  4:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-02  7:48 [PATCH] c++: Fix further protected_set_expr_location related -fcompare-debug issues [PR94441] Jakub Jelinek
2020-04-03 19:02 ` Jason Merrill
2020-04-03 20:12   ` [PATCH v2] " Jakub Jelinek
2020-04-04  4:13     ` Jason Merrill

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