public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [C++ PATCH] Avoid constexpr garbage for implicit conversion to void.
@ 2019-06-07 21:08 Jason Merrill
  2019-06-11  4:29 ` Jason Merrill
  0 siblings, 1 reply; 4+ messages in thread
From: Jason Merrill @ 2019-06-07 21:08 UTC (permalink / raw)
  To: gcc-patches

All expression statements and some other places express implicit conversion to
void with a CONVERT_EXPR.  There's no reason to build up a new one as part of
constexpr evaluation.

The ADDR_EXPR change also avoids a bit of garbage by discarding an ADDR_EXPR we
just built but didn't end up using.

The location wrapper change doesn't affect garbage, it's just a minor
optimization.

Tested x86_64-pc-linux-gnu, applying to trunk.

	* constexpr.c (cxx_eval_constant_expression): Call
	STRIP_ANY_LOCATION_WRAPPER early.
	[CONVERT_EXPR]: Don't build anything for conversion to void.
	[ADDR_EXPR]: ggc_free unused ADDR_EXPR.
---
 gcc/cp/constexpr.c | 19 +++++++++++++------
 gcc/cp/ChangeLog   |  7 +++++++
 2 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index 15bfb6d8148..10afc419a33 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -4382,9 +4382,6 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
 			      bool *non_constant_p, bool *overflow_p,
 			      tree *jump_target /* = NULL */)
 {
-  constexpr_ctx new_ctx;
-  tree r = t;
-
   if (jump_target && *jump_target)
     {
       /* If we are jumping, ignore all statements/expressions except those
@@ -4415,6 +4412,9 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
       *non_constant_p = true;
       return t;
     }
+
+  STRIP_ANY_LOCATION_WRAPPER (t);
+
   if (CONSTANT_CLASS_P (t))
     {
       if (TREE_OVERFLOW (t))
@@ -4439,8 +4439,7 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
     }
 
   /* Avoid excessively long constexpr evaluations.  */
-  if (!location_wrapper_p (t)
-      && ++*ctx->constexpr_ops_count >= constexpr_ops_limit)
+  if (++*ctx->constexpr_ops_count >= constexpr_ops_limit)
     {
       if (!ctx->quiet)
 	error_at (cp_expr_loc_or_loc (t, input_location),
@@ -4452,6 +4451,9 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
       return t;
     }
 
+  constexpr_ctx new_ctx;
+  tree r = t;
+
   tree_code tcode = TREE_CODE (t);
   switch (tcode)
     {
@@ -4752,7 +4754,10 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
 	/* This function does more aggressive folding than fold itself.  */
 	r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
 	if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
-	  return t;
+	  {
+	    ggc_free (r);
+	    return t;
+	  }
 	break;
       }
 
@@ -5065,6 +5070,8 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
 	  r = op;
 	else if (tcode == UNARY_PLUS_EXPR)
 	  r = fold_convert (TREE_TYPE (t), op);
+	else if (VOID_TYPE_P (type))
+	  r = void_node;
 	else
 	  r = fold_build1 (tcode, type, op);
 
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 1489238b716..c0720404b75 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,10 @@
+2019-06-07  Jason Merrill  <jason@redhat.com>
+
+	* constexpr.c (cxx_eval_constant_expression): Call
+	STRIP_ANY_LOCATION_WRAPPER early.
+	[CONVERT_EXPR]: Don't build anything for conversion to void.
+	[ADDR_EXPR]: ggc_free unused ADDR_EXPR.
+
 2019-06-05  Martin Sebor  <msebor@redhat.com>
 
 	PR c/90737

base-commit: 92964728f22989296756c1ece2dbbed323007c8e
-- 
2.20.1

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

end of thread, other threads:[~2019-06-19  8:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-07 21:08 [C++ PATCH] Avoid constexpr garbage for implicit conversion to void Jason Merrill
2019-06-11  4:29 ` Jason Merrill
2019-06-18 16:07   ` Jason Merrill
2019-06-19  8:23     ` 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).