diff mbox series

c++: fold calls to std::forward_like [PR96780]

Message ID 20240806140122.144017-1-ppalka@redhat.com
State New
Headers show
Series c++: fold calls to std::forward_like [PR96780] | expand

Commit Message

Patrick Palka Aug. 6, 2024, 2:01 p.m. UTC
Bootstrapped and regtested on x86_64-pc-linux-gnu, does this
look OK  for trunk?

-- >8 --

This extends our folding of cast-like standard library functions
to also include C++23's std::forward_like.

	PR c++/96780

gcc/cp/ChangeLog:

	* cp-gimplify.cc (cp_fold) <case CALL_EXPR>: Fold calls
	to std::forward_like as well.

gcc/testsuite/ChangeLog:

	* g++.dg/opt/pr96780.C: Test std::forward_like folding.
---
 gcc/cp/cp-gimplify.cc              | 3 ++-
 gcc/testsuite/g++.dg/opt/pr96780.C | 5 +++++
 2 files changed, 7 insertions(+), 1 deletion(-)

Comments

Marek Polacek Aug. 6, 2024, 2:06 p.m. UTC | #1
On Tue, Aug 06, 2024 at 10:01:22AM -0400, Patrick Palka wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this
> look OK  for trunk?

Looks simple & good.

Reviewed-by: Marek Polacek <polacek@redhat.com>
 
> -- >8 --
> 
> This extends our folding of cast-like standard library functions
> to also include C++23's std::forward_like.
> 
> 	PR c++/96780
> 
> gcc/cp/ChangeLog:
> 
> 	* cp-gimplify.cc (cp_fold) <case CALL_EXPR>: Fold calls
> 	to std::forward_like as well.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/opt/pr96780.C: Test std::forward_like folding.
> ---
>  gcc/cp/cp-gimplify.cc              | 3 ++-
>  gcc/testsuite/g++.dg/opt/pr96780.C | 5 +++++
>  2 files changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc
> index b88c3b7f370..3db9657ae93 100644
> --- a/gcc/cp/cp-gimplify.cc
> +++ b/gcc/cp/cp-gimplify.cc
> @@ -3316,7 +3316,8 @@ cp_fold (tree x, fold_flags_t flags)
>  		|| id_equal (DECL_NAME (callee), "addressof")
>  		/* This addressof equivalent is used heavily in libstdc++.  */
>  		|| id_equal (DECL_NAME (callee), "__addressof")
> -		|| id_equal (DECL_NAME (callee), "as_const")))
> +		|| id_equal (DECL_NAME (callee), "as_const")
> +		|| id_equal (DECL_NAME (callee), "forward_like")))
>  	  {
>  	    r = CALL_EXPR_ARG (x, 0);
>  	    /* Check that the return and argument types are sane before
> diff --git a/gcc/testsuite/g++.dg/opt/pr96780.C b/gcc/testsuite/g++.dg/opt/pr96780.C
> index 61e11855eeb..a29cda8b836 100644
> --- a/gcc/testsuite/g++.dg/opt/pr96780.C
> +++ b/gcc/testsuite/g++.dg/opt/pr96780.C
> @@ -29,6 +29,10 @@ void f() {
>    auto&& x11 = std::as_const(a);
>    auto&& x12 = std::as_const(ca);
>  #endif
> +#if __cpp_lib_forward_like
> +  auto&& x13 = std::forward_like<int&&>(a);
> +  auto&& x14 = std::forward_like<int&&>(ca);
> +#endif
>  }
>  
>  // { dg-final { scan-tree-dump-not "= std::move" "gimple" } }
> @@ -36,3 +40,4 @@ void f() {
>  // { dg-final { scan-tree-dump-not "= std::addressof" "gimple" } }
>  // { dg-final { scan-tree-dump-not "= std::__addressof" "gimple" } }
>  // { dg-final { scan-tree-dump-not "= std::as_const" "gimple" } }
> +// { dg-final { scan-tree-dump-not "= std::forward_like" "gimple" } }
> -- 
> 2.46.0.39.g891ee3b9db
> 

Marek
Jason Merrill Aug. 6, 2024, 3:38 p.m. UTC | #2
On 8/6/24 10:01 AM, Patrick Palka wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this
> look OK  for trunk?

I might add it after std::forward instead of at the bottom?  OK either way.

> -- >8 --
> 
> This extends our folding of cast-like standard library functions
> to also include C++23's std::forward_like.
> 
> 	PR c++/96780
> 
> gcc/cp/ChangeLog:
> 
> 	* cp-gimplify.cc (cp_fold) <case CALL_EXPR>: Fold calls
> 	to std::forward_like as well.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/opt/pr96780.C: Test std::forward_like folding.
> ---
>   gcc/cp/cp-gimplify.cc              | 3 ++-
>   gcc/testsuite/g++.dg/opt/pr96780.C | 5 +++++
>   2 files changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc
> index b88c3b7f370..3db9657ae93 100644
> --- a/gcc/cp/cp-gimplify.cc
> +++ b/gcc/cp/cp-gimplify.cc
> @@ -3316,7 +3316,8 @@ cp_fold (tree x, fold_flags_t flags)
>   		|| id_equal (DECL_NAME (callee), "addressof")
>   		/* This addressof equivalent is used heavily in libstdc++.  */
>   		|| id_equal (DECL_NAME (callee), "__addressof")
> -		|| id_equal (DECL_NAME (callee), "as_const")))
> +		|| id_equal (DECL_NAME (callee), "as_const")
> +		|| id_equal (DECL_NAME (callee), "forward_like")))
>   	  {
>   	    r = CALL_EXPR_ARG (x, 0);
>   	    /* Check that the return and argument types are sane before
> diff --git a/gcc/testsuite/g++.dg/opt/pr96780.C b/gcc/testsuite/g++.dg/opt/pr96780.C
> index 61e11855eeb..a29cda8b836 100644
> --- a/gcc/testsuite/g++.dg/opt/pr96780.C
> +++ b/gcc/testsuite/g++.dg/opt/pr96780.C
> @@ -29,6 +29,10 @@ void f() {
>     auto&& x11 = std::as_const(a);
>     auto&& x12 = std::as_const(ca);
>   #endif
> +#if __cpp_lib_forward_like
> +  auto&& x13 = std::forward_like<int&&>(a);
> +  auto&& x14 = std::forward_like<int&&>(ca);
> +#endif
>   }
>   
>   // { dg-final { scan-tree-dump-not "= std::move" "gimple" } }
> @@ -36,3 +40,4 @@ void f() {
>   // { dg-final { scan-tree-dump-not "= std::addressof" "gimple" } }
>   // { dg-final { scan-tree-dump-not "= std::__addressof" "gimple" } }
>   // { dg-final { scan-tree-dump-not "= std::as_const" "gimple" } }
> +// { dg-final { scan-tree-dump-not "= std::forward_like" "gimple" } }
diff mbox series

Patch

diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc
index b88c3b7f370..3db9657ae93 100644
--- a/gcc/cp/cp-gimplify.cc
+++ b/gcc/cp/cp-gimplify.cc
@@ -3316,7 +3316,8 @@  cp_fold (tree x, fold_flags_t flags)
 		|| id_equal (DECL_NAME (callee), "addressof")
 		/* This addressof equivalent is used heavily in libstdc++.  */
 		|| id_equal (DECL_NAME (callee), "__addressof")
-		|| id_equal (DECL_NAME (callee), "as_const")))
+		|| id_equal (DECL_NAME (callee), "as_const")
+		|| id_equal (DECL_NAME (callee), "forward_like")))
 	  {
 	    r = CALL_EXPR_ARG (x, 0);
 	    /* Check that the return and argument types are sane before
diff --git a/gcc/testsuite/g++.dg/opt/pr96780.C b/gcc/testsuite/g++.dg/opt/pr96780.C
index 61e11855eeb..a29cda8b836 100644
--- a/gcc/testsuite/g++.dg/opt/pr96780.C
+++ b/gcc/testsuite/g++.dg/opt/pr96780.C
@@ -29,6 +29,10 @@  void f() {
   auto&& x11 = std::as_const(a);
   auto&& x12 = std::as_const(ca);
 #endif
+#if __cpp_lib_forward_like
+  auto&& x13 = std::forward_like<int&&>(a);
+  auto&& x14 = std::forward_like<int&&>(ca);
+#endif
 }
 
 // { dg-final { scan-tree-dump-not "= std::move" "gimple" } }
@@ -36,3 +40,4 @@  void f() {
 // { dg-final { scan-tree-dump-not "= std::addressof" "gimple" } }
 // { dg-final { scan-tree-dump-not "= std::__addressof" "gimple" } }
 // { dg-final { scan-tree-dump-not "= std::as_const" "gimple" } }
+// { dg-final { scan-tree-dump-not "= std::forward_like" "gimple" } }