diff mbox series

c++: REF_PARENTHESIZED_P wrapper inhibiting NRVO [PR67302]

Message ID 20210619194544.3360999-1-ppalka@redhat.com
State New
Headers show
Series c++: REF_PARENTHESIZED_P wrapper inhibiting NRVO [PR67302] | expand

Commit Message

Patrick Palka June 19, 2021, 7:45 p.m. UTC
Here, in C++14 or later, we remember the parentheses around 'a' in the
return statement by using a REF_PARENTHESIZED_P wrapper, which ends up
inhibiting NRVO because we don't look through this wrapper before
checking the conditions for NRVO.  This patch fixes this by calling
maybe_undo_parenthesized_ref sooner in check_return_expr.

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?

	PR c++/67302

gcc/cp/ChangeLog:

	* typeck.c (check_return_expr): Call maybe_undo_parenthesized_ref
	sooner, before the NRVO handling.

gcc/testsuite/ChangeLog:

	* g++.dg/opt/nrv21.C: New test.
---
 gcc/cp/typeck.c                  |  9 ++++-----
 gcc/testsuite/g++.dg/opt/nrv21.C | 14 ++++++++++++++
 2 files changed, 18 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/opt/nrv21.C

Comments

Jason Merrill June 21, 2021, 4:13 a.m. UTC | #1
On 6/19/21 3:45 PM, Patrick Palka wrote:
> Here, in C++14 or later, we remember the parentheses around 'a' in the
> return statement by using a REF_PARENTHESIZED_P wrapper, which ends up
> inhibiting NRVO because we don't look through this wrapper before
> checking the conditions for NRVO.  This patch fixes this by calling
> maybe_undo_parenthesized_ref sooner in check_return_expr.
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?

OK.

> 	PR c++/67302
> 
> gcc/cp/ChangeLog:
> 
> 	* typeck.c (check_return_expr): Call maybe_undo_parenthesized_ref
> 	sooner, before the NRVO handling.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/opt/nrv21.C: New test.
> ---
>   gcc/cp/typeck.c                  |  9 ++++-----
>   gcc/testsuite/g++.dg/opt/nrv21.C | 14 ++++++++++++++
>   2 files changed, 18 insertions(+), 5 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/opt/nrv21.C
> 
> diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
> index dbb2370510c..aa014c3812a 100644
> --- a/gcc/cp/typeck.c
> +++ b/gcc/cp/typeck.c
> @@ -10306,7 +10306,10 @@ check_return_expr (tree retval, bool *no_warning)
>   
>        See finish_function and finalize_nrv for the rest of this optimization.  */
>     if (retval)
> -    STRIP_ANY_LOCATION_WRAPPER (retval);
> +    {
> +      retval = maybe_undo_parenthesized_ref (retval);
> +      STRIP_ANY_LOCATION_WRAPPER (retval);
> +    }
>   
>     bool named_return_value_okay_p = can_do_nrvo_p (retval, functype);
>     if (fn_returns_value_p && flag_elide_constructors)
> @@ -10340,10 +10343,6 @@ check_return_expr (tree retval, bool *no_warning)
>         if (VOID_TYPE_P (functype))
>   	return error_mark_node;
>   
> -      /* If we had an id-expression obfuscated by force_paren_expr, we need
> -	 to undo it so we can try to treat it as an rvalue below.  */
> -      retval = maybe_undo_parenthesized_ref (retval);
> -
>         if (processing_template_decl)
>   	retval = build_non_dependent_expr (retval);
>   
> diff --git a/gcc/testsuite/g++.dg/opt/nrv21.C b/gcc/testsuite/g++.dg/opt/nrv21.C
> new file mode 100644
> index 00000000000..31bff79afc1
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/opt/nrv21.C
> @@ -0,0 +1,14 @@
> +// PR c++/67302
> +// { dg-additional-options -fdump-tree-gimple }
> +// { dg-final { scan-tree-dump-not "<retval> = a" "gimple" } }
> +
> +struct A
> +{
> +  int ar[42];
> +  A();
> +};
> +
> +A f() {
> +  A a;
> +  return (a);
> +}
>
diff mbox series

Patch

diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index dbb2370510c..aa014c3812a 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -10306,7 +10306,10 @@  check_return_expr (tree retval, bool *no_warning)
 
      See finish_function and finalize_nrv for the rest of this optimization.  */
   if (retval)
-    STRIP_ANY_LOCATION_WRAPPER (retval);
+    {
+      retval = maybe_undo_parenthesized_ref (retval);
+      STRIP_ANY_LOCATION_WRAPPER (retval);
+    }
 
   bool named_return_value_okay_p = can_do_nrvo_p (retval, functype);
   if (fn_returns_value_p && flag_elide_constructors)
@@ -10340,10 +10343,6 @@  check_return_expr (tree retval, bool *no_warning)
       if (VOID_TYPE_P (functype))
 	return error_mark_node;
 
-      /* If we had an id-expression obfuscated by force_paren_expr, we need
-	 to undo it so we can try to treat it as an rvalue below.  */
-      retval = maybe_undo_parenthesized_ref (retval);
-
       if (processing_template_decl)
 	retval = build_non_dependent_expr (retval);
 
diff --git a/gcc/testsuite/g++.dg/opt/nrv21.C b/gcc/testsuite/g++.dg/opt/nrv21.C
new file mode 100644
index 00000000000..31bff79afc1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/opt/nrv21.C
@@ -0,0 +1,14 @@ 
+// PR c++/67302
+// { dg-additional-options -fdump-tree-gimple }
+// { dg-final { scan-tree-dump-not "<retval> = a" "gimple" } }
+
+struct A
+{
+  int ar[42];
+  A();
+};
+
+A f() {
+  A a;
+  return (a);
+}