[gcc r16-610] Add std::to_underlying to the set of stdlib functions that are always folded

2025-05-14 Thread Ville Voutilainen via Gcc-cvs
https://gcc.gnu.org/g:3ecca8f3ad7e7fe3da0e600fb66fca3dc6bae006

commit r16-610-g3ecca8f3ad7e7fe3da0e600fb66fca3dc6bae006
Author: Ville Voutilainen 
Date:   Mon May 12 23:16:46 2025 +0300

Add std::to_underlying to the set of stdlib functions that are always folded

gcc/cp/ChangeLog:
* cp-gimplify.cc (cp_fold): Add to_underlying.

gcc/testsuite/ChangeLog:
* g++.dg/opt/pr96780_cpp23.C: New.

libstdc++-v3/ChangeLog:
* include/std/utility (to_underlying): Add the __always_inline__ 
attribute.

Signed-off-by: Ville Voutilainen 

Diff:
---
 gcc/cp/cp-gimplify.cc| 13 +
 gcc/testsuite/g++.dg/opt/pr96780_cpp23.C | 16 
 libstdc++-v3/include/std/utility |  2 +-
 3 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc
index d2423fd1848a..4e5d383daeb6 100644
--- a/gcc/cp/cp-gimplify.cc
+++ b/gcc/cp/cp-gimplify.cc
@@ -3343,19 +3343,16 @@ 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), "to_underlying")
|| id_equal (DECL_NAME (callee), "as_const")))
  {
r = CALL_EXPR_ARG (x, 0);
/* Check that the return and argument types are sane before
   folding.  */
-   if (INDIRECT_TYPE_P (TREE_TYPE (x))
-   && INDIRECT_TYPE_P (TREE_TYPE (r)))
- {
-   if (!same_type_p (TREE_TYPE (x), TREE_TYPE (r)))
- r = build_nop (TREE_TYPE (x), r);
-   x = cp_fold (r, flags);
-   break;
- }
+   if (!same_type_p (TREE_TYPE (x), TREE_TYPE (r)))
+ r = build_nop (TREE_TYPE (x), r);
+   x = cp_fold (r, flags);
+   break;
  }
 
int sv = optimize, nw = sv;
diff --git a/gcc/testsuite/g++.dg/opt/pr96780_cpp23.C 
b/gcc/testsuite/g++.dg/opt/pr96780_cpp23.C
new file mode 100644
index ..ba4a837ddedb
--- /dev/null
+++ b/gcc/testsuite/g++.dg/opt/pr96780_cpp23.C
@@ -0,0 +1,16 @@
+// PR c++/96780
+// Verify calls to std::move/forward are folded away by the frontend.
+// { dg-do compile { target c++23 } }
+// { dg-additional-options "-ffold-simple-inlines -fdump-tree-gimple" }
+
+#include 
+
+enum class A : char {a};
+
+extern A& x;
+
+void f() {
+  auto&& x1 = std::to_underlying(x);
+}
+
+// { dg-final { scan-tree-dump-not "= std::to_underlying" "gimple" } }
diff --git a/libstdc++-v3/include/std/utility b/libstdc++-v3/include/std/utility
index 1c15c75f92b0..8a85ccfd09ba 100644
--- a/libstdc++-v3/include/std/utility
+++ b/libstdc++-v3/include/std/utility
@@ -201,7 +201,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #ifdef __cpp_lib_to_underlying // C++ >= 23
   /// Convert an object of enumeration type to its underlying type.
   template
-[[nodiscard]]
+[[nodiscard, __gnu__::__always_inline__]]
 constexpr underlying_type_t<_Tp>
 to_underlying(_Tp __value) noexcept
 { return static_cast>(__value); }


[gcc r16-621] Remove a sanity check comment now that the sanity check has been removed

2025-05-14 Thread Ville Voutilainen via Gcc-cvs
https://gcc.gnu.org/g:871099ae65a4f528496c7550331364866f9806bb

commit r16-621-g871099ae65a4f528496c7550331364866f9806bb
Author: Ville Voutilainen 
Date:   Wed May 14 16:39:09 2025 +0300

Remove a sanity check comment now that the sanity check has been removed

gcc/cp/ChangeLog:

* cp-gimplify.cc (cp_fold): Remove a remnant comment.

Diff:
---
 gcc/cp/cp-gimplify.cc | 2 --
 1 file changed, 2 deletions(-)

diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc
index 4e5d383daeb6..eab55504b050 100644
--- a/gcc/cp/cp-gimplify.cc
+++ b/gcc/cp/cp-gimplify.cc
@@ -3347,8 +3347,6 @@ cp_fold (tree x, fold_flags_t flags)
|| id_equal (DECL_NAME (callee), "as_const")))
  {
r = CALL_EXPR_ARG (x, 0);
-   /* Check that the return and argument types are sane before
-  folding.  */
if (!same_type_p (TREE_TYPE (x), TREE_TYPE (r)))
  r = build_nop (TREE_TYPE (x), r);
x = cp_fold (r, flags);


[gcc r16-695] Further simplify the stdlib inline folding

2025-05-16 Thread Ville Voutilainen via Gcc-cvs
https://gcc.gnu.org/g:fa365450352ae5cc7c0078e37e641341fb48e6b6

commit r16-695-gfa365450352ae5cc7c0078e37e641341fb48e6b6
Author: Ville Voutilainen 
Date:   Thu May 15 16:26:52 2025 +0300

Further simplify the stdlib inline folding

gcc/cp/ChangeLog:
* cp-gimplify.cc (cp_fold): Do the conversion unconditionally, even 
for same-type cases.

gcc/ChangeLog:
* doc/invoke.texi: Add to_underlying to -ffold-simple-inlines.

Diff:
---
 gcc/cp/cp-gimplify.cc | 3 +--
 gcc/doc/invoke.texi   | 3 ++-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc
index eab55504b050..f7bd453bc5eb 100644
--- a/gcc/cp/cp-gimplify.cc
+++ b/gcc/cp/cp-gimplify.cc
@@ -3347,8 +3347,7 @@ cp_fold (tree x, fold_flags_t flags)
|| id_equal (DECL_NAME (callee), "as_const")))
  {
r = CALL_EXPR_ARG (x, 0);
-   if (!same_type_p (TREE_TYPE (x), TREE_TYPE (r)))
- r = build_nop (TREE_TYPE (x), r);
+   r = build_nop (TREE_TYPE (x), r);
x = cp_fold (r, flags);
break;
  }
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index ee7180110e19..83c63ce6ae53 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -3348,7 +3348,8 @@ aliases, the default is @option{-fno-extern-tls-init}.
 @item -ffold-simple-inlines
 @itemx -fno-fold-simple-inlines
 Permit the C++ frontend to fold calls to @code{std::move}, @code{std::forward},
-@code{std::addressof} and @code{std::as_const}.  In contrast to inlining, this
+@code{std::addressof}, @code{std::to_underlying}
+and @code{std::as_const}.  In contrast to inlining, this
 means no debug information will be generated for such calls.  Since these
 functions are rarely interesting to debug, this flag is enabled by default
 unless @option{-fno-inline} is active.