Line data Source code
1 : /* Callback attribute handling
2 : Copyright (C) 2025-2026 Free Software Foundation, Inc.
3 : Contributed by Josef Melcr <jmelcr@gcc.gnu.org>
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify
8 : under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3 of the License, or
10 : (at your option) any later version.
11 :
12 : GCC is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "backend.h"
25 : #include "tree.h"
26 : #include "gimple.h"
27 : #include "fold-const.h"
28 : #include "alloc-pool.h"
29 : #include "cgraph.h"
30 : #include "diagnostic.h"
31 : #include "builtins.h"
32 : #include "options.h"
33 : #include "gimple-range.h"
34 : #include "attribs.h"
35 : #include "symbol-summary.h"
36 : #include "lto-streamer.h"
37 : #include "attr-callback.h"
38 : #include "callback-info.h"
39 :
40 : /* Returns a callback attribute with callback index FN_IDX, and ARG_COUNT
41 : arguments specified by VA_ARGS. */
42 : tree
43 395658 : callback_build_attr (unsigned fn_idx, unsigned arg_count...)
44 : {
45 395658 : va_list args;
46 395658 : va_start (args, arg_count);
47 :
48 395658 : tree cblist = NULL_TREE;
49 395658 : tree *pp = &cblist;
50 395658 : unsigned i;
51 791316 : for (i = 0; i < arg_count; i++)
52 : {
53 395658 : int num = va_arg (args, int);
54 395658 : tree tnum = build_int_cst (integer_type_node, num);
55 395658 : *pp = build_tree_list (NULL, tnum);
56 395658 : pp = &TREE_CHAIN (*pp);
57 : }
58 395658 : cblist
59 395658 : = tree_cons (NULL_TREE, build_int_cst (integer_type_node, fn_idx), cblist);
60 395658 : tree attr
61 395658 : = tree_cons (get_identifier ("callback_only"), cblist, NULL_TREE);
62 395658 : return attr;
63 : }
64 :
65 : /* Returns TRUE if a function should be treated as if it had a callback
66 : attribute despite the DECL not having it. STMT can be passed NULL
67 : if the call statement is not available at the time, for example WPA, but it
68 : should be called with the statement itself whenever possible. */
69 : bool
70 2789883 : callback_is_special_cased (tree decl, gcall *stmt)
71 : {
72 2789883 : if (fndecl_built_in_p (decl, BUILT_IN_GOMP_TASK))
73 : {
74 18592 : if (stmt)
75 18592 : return gimple_call_arg (stmt, 2) == null_pointer_node;
76 : return true;
77 : }
78 : return false;
79 : }
80 :
81 : /* Returns an attribute for a special cased function. */
82 : tree
83 3582 : callback_special_case_attr (tree decl)
84 : {
85 3582 : if (fndecl_built_in_p (decl, BUILT_IN_GOMP_TASK))
86 3582 : return callback_build_attr (1, 1, 2);
87 0 : gcc_unreachable ();
88 : }
89 :
90 : /* Returns TRUE if the callee of E has a callback attribute. */
91 : bool
92 2655236 : callback_edge_callee_has_attr (cgraph_edge *e)
93 : {
94 2655236 : return lookup_attribute ("callback_only",
95 2655236 : DECL_ATTRIBUTES (e->callee->decl))
96 2655236 : || callback_is_special_cased (e->callee->decl, e->call_stmt);
97 : }
98 :
99 : /* Given an instance of callback attribute, return the 0-based
100 : index of the called function in question. */
101 : int
102 28624 : callback_get_fn_index (tree cb_attr)
103 : {
104 28624 : tree args = TREE_VALUE (cb_attr);
105 28624 : int idx = TREE_INT_CST_LOW (TREE_VALUE (args)) - 1;
106 28624 : return idx;
107 : }
108 :
109 : /* For a given callback pair, retrieves the callback attribute used
110 : to create E from the callee of CARRYING. */
111 : tree
112 15205 : callback_fetch_attr_by_edge (cgraph_edge *e, cgraph_edge *carrying)
113 : {
114 15205 : gcc_checking_assert (e->call_stmt == carrying->call_stmt
115 : && e->lto_stmt_uid == carrying->lto_stmt_uid);
116 :
117 15205 : if (callback_is_special_cased (carrying->callee->decl, e->call_stmt))
118 1791 : return callback_special_case_attr (carrying->callee->decl);
119 :
120 13414 : tree cb_attr = lookup_attribute ("callback_only",
121 13414 : DECL_ATTRIBUTES (carrying->callee->decl));
122 13414 : gcc_checking_assert (cb_attr);
123 13414 : callback_info *ci = callback_info_sum->get (e);
124 13414 : tree res = NULL_TREE;
125 13415 : for (; cb_attr;
126 1 : cb_attr = lookup_attribute ("callback_only", TREE_CHAIN (cb_attr)))
127 : {
128 13415 : unsigned fn_idx = callback_get_fn_index (cb_attr);
129 13415 : if (fn_idx == ci->get_id ())
130 : {
131 : res = cb_attr;
132 : break;
133 : }
134 : }
135 13414 : gcc_checking_assert (res != NULL_TREE);
136 : return res;
137 : }
138 :
139 : /* Returns the argument mapping from the dispatching function to the callback
140 : function parsed from the attribute. */
141 : auto_vec<int>
142 15205 : callback_get_arg_mapping_from_attr (tree attr)
143 : {
144 15205 : tree args = TREE_VALUE (attr);
145 15205 : auto_vec<int> res;
146 15205 : tree it;
147 :
148 : /* Skip over the first argument, which denotes
149 : which argument is the called function. */
150 30411 : for (it = TREE_CHAIN (args); it != NULL_TREE; it = TREE_CHAIN (it))
151 : {
152 15206 : int idx = TREE_INT_CST_LOW (TREE_VALUE (it));
153 : /* Subtract 1 to account for 1-based indexing. If the value is unknown,
154 : use ARG_MAPPING_UNKNOWN_IDX instead. */
155 15206 : idx = idx == CB_UNKNOWN_POS ? ARG_MAPPING_UNKNOWN_IDX : idx - 1;
156 15206 : res.safe_push (idx);
157 : }
158 :
159 15205 : return res;
160 : }
161 :
162 : /* Returns TRUE if E is considered useful in the callgraph, FALSE otherwise. If
163 : this predicate returns FALSE, then E wasn't used to optimize its callee and
164 : can be safely removed from the callgraph. */
165 : bool
166 13739 : callback_edge_useful_p (cgraph_edge *e)
167 : {
168 13739 : gcc_checking_assert (e->callback);
169 13739 : callback_info *ci = callback_info_sum->get (e);
170 13739 : return ci->redirected;
171 : }
172 :
173 : /* Returns the number of arguments the callback function described by ATTR
174 : takes. */
175 :
176 : size_t
177 15205 : callback_num_args (tree attr)
178 : {
179 15205 : tree args = TREE_VALUE (attr);
180 15205 : size_t res = 0;
181 15205 : tree it;
182 :
183 30411 : for (it = TREE_CHAIN (args); it != NULL_TREE; it = TREE_CHAIN (it), ++res)
184 : ;
185 15205 : return res;
186 : }
187 :
188 : /* Redirect call statement of E to the callee of E. Used in
189 : cgraph_edge::redirect_call_stmt_to_callee. Edge E must be a callback
190 : edge. */
191 : void
192 3352 : callback_redirect_edge (cgraph_edge *e)
193 : {
194 3352 : callback_info *ci = callback_info_sum->get (e);
195 3352 : tree previous_arg = gimple_call_arg (e->call_stmt, ci->fn_idx);
196 3352 : location_t loc = EXPR_LOCATION (previous_arg);
197 3352 : tree new_addr = build_fold_addr_expr_loc (loc, e->callee->decl);
198 3352 : gimple_call_set_arg (e->call_stmt, ci->fn_idx, new_addr);
199 3352 : }
|