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-19 23:28:51 -0700 |
commit | 59dd1d7ab21ad9a7ebf641ec9aeea609c003ad2f (patch) | |
tree | 07b2c25abf073eef868d653fcc6b1763b4f42411 /gcc/cp/parser.cc | |
parent | 5c4c1fe6df0f752764cdfd7404a60bfd2b4f5057 (diff) | |
download | gcc-59dd1d7ab21ad9a7ebf641ec9aeea609c003ad2f.zip gcc-59dd1d7ab21ad9a7ebf641ec9aeea609c003ad2f.tar.gz gcc-59dd1d7ab21ad9a7ebf641ec9aeea609c003ad2f.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/parser.cc')
-rw-r--r-- | gcc/cp/parser.cc | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index efd5d6f..1fa0780 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -2467,7 +2467,7 @@ static tree cp_parser_perform_range_for_lookup static tree cp_parser_range_for_member_function (tree, tree); static tree cp_parser_jump_statement - (cp_parser *); + (cp_parser *, tree &); static void cp_parser_declaration_statement (cp_parser *); @@ -12757,7 +12757,7 @@ cp_parser_statement (cp_parser* parser, tree in_statement_expr, case RID_CO_RETURN: case RID_GOTO: std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc); - statement = cp_parser_jump_statement (parser); + statement = cp_parser_jump_statement (parser, std_attrs); break; /* Objective-C++ exception-handling constructs. */ @@ -14845,10 +14845,11 @@ cp_parser_init_statement (cp_parser *parser, tree *decl) jump-statement: goto * expression ; + STD_ATTRS are the statement attributes. They can be modified. Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */ static tree -cp_parser_jump_statement (cp_parser* parser) +cp_parser_jump_statement (cp_parser* parser, tree &std_attrs) { tree statement = error_mark_node; cp_token *token; @@ -14925,6 +14926,31 @@ cp_parser_jump_statement (cp_parser* parser) /* If the next token is a `;', then there is no expression. */ expr = NULL_TREE; + + if (keyword == RID_RETURN) + { + bool musttail_p = false; + if (lookup_attribute ("gnu", "musttail", std_attrs)) + { + musttail_p = true; + std_attrs = remove_attribute ("gnu", "musttail", std_attrs); + } + /* Support this for compatibility. */ + if (lookup_attribute ("clang", "musttail", std_attrs)) + { + musttail_p = true; + std_attrs = remove_attribute ("clang", "musttail", std_attrs); + } + + tree ret_expr = expr; + if (ret_expr && TREE_CODE (ret_expr) == TARGET_EXPR) + ret_expr = TARGET_EXPR_INITIAL (ret_expr); + if (ret_expr && TREE_CODE (ret_expr) == AGGR_INIT_EXPR) + AGGR_INIT_EXPR_MUST_TAIL (ret_expr) = musttail_p; + else + set_musttail_on_return (expr, token->location, musttail_p); + } + /* Build the return-statement, check co-return first, since type deduction is not valid there. */ if (keyword == RID_CO_RETURN) |