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 398636 : callback_build_attr (unsigned fn_idx, unsigned arg_count...)
44 : {
45 398636 : va_list args;
46 398636 : va_start (args, arg_count);
47 :
48 398636 : tree cblist = NULL_TREE;
49 398636 : tree *pp = &cblist;
50 398636 : unsigned i;
51 797272 : for (i = 0; i < arg_count; i++)
52 : {
53 398636 : int num = va_arg (args, int);
54 398636 : tree tnum = build_int_cst (integer_type_node, num);
55 398636 : *pp = build_tree_list (NULL, tnum);
56 398636 : pp = &TREE_CHAIN (*pp);
57 : }
58 398636 : cblist
59 398636 : = tree_cons (NULL_TREE, build_int_cst (integer_type_node, fn_idx), cblist);
60 398636 : tree attr
61 398636 : = tree_cons (get_identifier ("callback_only"), cblist, NULL_TREE);
62 398636 : 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 2801794 : callback_is_special_cased (tree decl, gcall *stmt)
71 : {
72 2801794 : if (fndecl_built_in_p (decl, BUILT_IN_GOMP_TASK))
73 : {
74 18618 : if (stmt)
75 18618 : 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 3584 : callback_special_case_attr (tree decl)
84 : {
85 3584 : if (fndecl_built_in_p (decl, BUILT_IN_GOMP_TASK))
86 3584 : 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 2662338 : callback_edge_callee_has_attr (cgraph_edge *e)
93 : {
94 2662338 : return lookup_attribute ("callback_only",
95 2662338 : DECL_ATTRIBUTES (e->callee->decl))
96 2662338 : || 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 30019 : callback_get_fn_index (tree cb_attr)
103 : {
104 30019 : tree args = TREE_VALUE (cb_attr);
105 30019 : int idx = TREE_INT_CST_LOW (TREE_VALUE (args)) - 1;
106 30019 : 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 15903 : callback_fetch_attr_by_edge (cgraph_edge *e, cgraph_edge *carrying)
113 : {
114 15903 : gcc_checking_assert (e->call_stmt == carrying->call_stmt
115 : && e->lto_stmt_uid == carrying->lto_stmt_uid);
116 :
117 15903 : if (callback_is_special_cased (carrying->callee->decl, e->call_stmt))
118 1792 : return callback_special_case_attr (carrying->callee->decl);
119 :
120 14111 : tree cb_attr = lookup_attribute ("callback_only",
121 14111 : DECL_ATTRIBUTES (carrying->callee->decl));
122 14111 : gcc_checking_assert (cb_attr);
123 14111 : callback_info *ci = callback_info_sum->get (e);
124 14111 : tree res = NULL_TREE;
125 14112 : for (; cb_attr;
126 1 : cb_attr = lookup_attribute ("callback_only", TREE_CHAIN (cb_attr)))
127 : {
128 14112 : unsigned fn_idx = callback_get_fn_index (cb_attr);
129 14112 : if (fn_idx == ci->get_id ())
130 : {
131 : res = cb_attr;
132 : break;
133 : }
134 : }
135 14111 : 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 15903 : callback_get_arg_mapping_from_attr (tree attr)
143 : {
144 15903 : tree args = TREE_VALUE (attr);
145 15903 : auto_vec<int> res;
146 15903 : tree it;
147 :
148 : /* Skip over the first argument, which denotes
149 : which argument is the called function. */
150 31807 : for (it = TREE_CHAIN (args); it != NULL_TREE; it = TREE_CHAIN (it))
151 : {
152 15904 : 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 15904 : idx = idx == CB_UNKNOWN_POS ? ARG_MAPPING_UNKNOWN_IDX : idx - 1;
156 15904 : res.safe_push (idx);
157 : }
158 :
159 15903 : 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 14425 : callback_edge_useful_p (cgraph_edge *e)
167 : {
168 14425 : gcc_checking_assert (e->callback);
169 14425 : callback_info *ci = callback_info_sum->get (e);
170 14425 : return ci->redirected;
171 : }
172 :
173 : /* Returns the number of arguments the callback function described by ATTR
174 : takes. */
175 :
176 : size_t
177 15903 : callback_num_args (tree attr)
178 : {
179 15903 : tree args = TREE_VALUE (attr);
180 15903 : size_t res = 0;
181 15903 : tree it;
182 :
183 31807 : for (it = TREE_CHAIN (args); it != NULL_TREE; it = TREE_CHAIN (it), ++res)
184 : ;
185 15903 : 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 3429 : callback_redirect_edge (cgraph_edge *e)
193 : {
194 3429 : callback_info *ci = callback_info_sum->get (e);
195 3429 : tree previous_arg = gimple_call_arg (e->call_stmt, ci->fn_idx);
196 3429 : location_t loc = EXPR_LOCATION (previous_arg);
197 3429 : tree new_addr = build_fold_addr_expr_loc (loc, e->callee->decl);
198 3429 : gimple_call_set_arg (e->call_stmt, ci->fn_idx, new_addr);
199 3429 : }
|