diff options
author | Andi Kleen <ak@linux.intel.com> | 2024-01-23 23:44:48 -0800 |
---|---|---|
committer | Andi Kleen <ak@gcc.gnu.org> | 2024-07-23 13:27:12 -0700 |
commit | 2bd8177256b6d87f6e75819218cf22c2c0bfc1ac (patch) | |
tree | d98a0be8f20f9ce828b9db11d68112e8d90ba4e1 /gcc/cp/pt.cc | |
parent | 2861eb34e30973cb991a7964af7cfeae014a98b0 (diff) | |
download | gcc-2bd8177256b6d87f6e75819218cf22c2c0bfc1ac.zip gcc-2bd8177256b6d87f6e75819218cf22c2c0bfc1ac.tar.gz gcc-2bd8177256b6d87f6e75819218cf22c2c0bfc1ac.tar.bz2 |
C++: Support clang compatible [[musttail]] (PR83324)
This patch implements a clang compatible [[musttail]] attribute for
returns.
musttail is useful as an alternative to computed goto for interpreters.
With computed goto the interpreter function usually ends up very big
which causes problems with register allocation and other per function
optimizations not scaling. With musttail the interpreter can be instead
written as a sequence of smaller functions that call each other. To
avoid unbounded stack growth this requires forcing a sibling call, which
this attribute does. It guarantees an error if the call cannot be tail
called which allows the programmer to fix it instead of risking a stack
overflow. Unlike computed goto it is also type-safe.
It turns out that David Malcolm had already implemented middle/backend
support for a musttail attribute back in 2016, but it wasn't exposed
to any frontend other than a special plugin.
This patch adds a [[gnu::musttail]] attribute for C++ that can be added
to return statements. The return statement must be a direct call
(it does not follow dependencies), which is similar to what clang
implements. It then uses the existing must tail infrastructure.
For compatibility it also detects clang::musttail
Passes bootstrap and full test
gcc/c-family/ChangeLog:
* c-attribs.cc (set_musttail_on_return): New function.
* c-common.h (set_musttail_on_return): Declare new function.
gcc/cp/ChangeLog:
PR c/83324
* cp-tree.h (AGGR_INIT_EXPR_MUST_TAIL): Add.
* parser.cc (cp_parser_statement): Handle musttail.
(cp_parser_jump_statement): Dito.
* pt.cc (tsubst_expr): Copy CALL_EXPR_MUST_TAIL_CALL.
* semantics.cc (simplify_aggr_init_expr): Handle musttail.
Diffstat (limited to 'gcc/cp/pt.cc')
-rw-r--r-- | gcc/cp/pt.cc | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 3939132..e102e3e 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -21094,12 +21094,19 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl) bool op = CALL_EXPR_OPERATOR_SYNTAX (t); bool ord = CALL_EXPR_ORDERED_ARGS (t); bool rev = CALL_EXPR_REVERSE_ARGS (t); - if (op || ord || rev) + bool mtc = false; + if (TREE_CODE (t) == CALL_EXPR) + mtc = CALL_EXPR_MUST_TAIL_CALL (t); + if (op || ord || rev || mtc) if (tree call = extract_call_expr (ret)) { CALL_EXPR_OPERATOR_SYNTAX (call) = op; CALL_EXPR_ORDERED_ARGS (call) = ord; CALL_EXPR_REVERSE_ARGS (call) = rev; + if (TREE_CODE (call) == CALL_EXPR) + CALL_EXPR_MUST_TAIL_CALL (call) = mtc; + else if (TREE_CODE (call) == AGGR_INIT_EXPR) + AGGR_INIT_EXPR_MUST_TAIL (call) = mtc; } if (warning_suppressed_p (t, OPT_Wpessimizing_move)) /* This also suppresses -Wredundant-move. */ |