Line data Source code
1 : /* Callback attribute summary
2 : Copyright (C) 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 "alloc-pool.h"
28 : #include "cgraph.h"
29 : #include "diagnostic.h"
30 : #include "builtins.h"
31 : #include "options.h"
32 : #include "gimple-range.h"
33 : #include "attribs.h"
34 : #include "symbol-summary.h"
35 : #include "data-streamer.h"
36 : #include "attr-callback.h"
37 : #include "callback-info.h"
38 :
39 : callback_info_sum_t *callback_info_sum = NULL;
40 :
41 : /* Calculates the CI->INVERTED_MAPPING field from CI->ARG_MAPPING. */
42 : static void
43 15380 : invert_arg_mapping (cgraph_edge *e, callback_info *ci)
44 : {
45 15380 : auto &mapping = ci->arg_mapping;
46 15380 : auto &inverted_mapping = ci->inverted_mapping;
47 15380 : unsigned num = gimple_call_num_args (e->call_stmt);
48 15380 : inverted_mapping.reserve (num);
49 107324 : for (unsigned i = 0; i < num; i++)
50 76564 : inverted_mapping.quick_push (ARG_MAPPING_UNKNOWN_IDX);
51 :
52 30794 : for (unsigned i = 0; i < mapping.length (); i++)
53 15414 : if (mapping[i] != ARG_MAPPING_UNKNOWN_IDX)
54 15414 : inverted_mapping[mapping[i]] = i;
55 15380 : }
56 :
57 : /* Streams a vector into an output block, in format N ELEM_1 ... ELEM_N*/
58 : static void
59 72 : stream_vector_out (lto_simple_output_block *ob, const auto_vec<int> &v)
60 : {
61 144 : streamer_write_uhwi_stream (ob->main_stream, v.length ());
62 378 : for (int elem : v)
63 162 : streamer_write_hwi_stream (ob->main_stream, elem);
64 72 : }
65 :
66 : /* Reads a vector from an LTO input block. The format is supposed to be the
67 : same as stream_vector_out. */
68 : static void
69 42 : stream_vector_in (lto_input_block *ib, auto_vec<int> &v)
70 : {
71 42 : unsigned length = streamer_read_uhwi (ib);
72 42 : v.reserve (length);
73 :
74 169 : for (unsigned i = 0; i < length; i++)
75 : {
76 85 : int elem = streamer_read_hwi (ib);
77 85 : v.safe_push (elem);
78 : }
79 42 : }
80 :
81 : /* Stream out callback_info. */
82 : void
83 36 : callback_info::stream_out (lto_simple_output_block *ob) const
84 : {
85 36 : streamer_write_uhwi_stream (ob->main_stream, fn_idx);
86 36 : stream_vector_out (ob, arg_mapping);
87 36 : stream_vector_out (ob, inverted_mapping);
88 36 : bitpack_d bp = bitpack_create (ob->main_stream);
89 36 : bp_pack_value (&bp, redirected, 1);
90 36 : streamer_write_bitpack (&bp);
91 36 : }
92 :
93 : /* Stream in callback_info. */
94 : void
95 21 : callback_info::stream_in (lto_input_block *ib)
96 : {
97 21 : fn_idx = streamer_read_uhwi (ib);
98 21 : stream_vector_in (ib, arg_mapping);
99 21 : stream_vector_in (ib, inverted_mapping);
100 21 : bitpack_d bp = streamer_read_bitpack (ib);
101 21 : redirected = bp_unpack_value (&bp, 1);
102 21 : }
103 :
104 : /* Returns the id of the associated callback attribute. See the comment of the
105 : fn_idx field. */
106 : unsigned
107 13415 : callback_info::get_id () const
108 : {
109 13415 : return fn_idx;
110 : }
111 :
112 : void
113 15205 : callback_info::init (cgraph_edge *e, unsigned fn_idx, tree attr)
114 : {
115 15205 : this->fn_idx = fn_idx;
116 15205 : arg_mapping = callback_get_arg_mapping_from_attr (attr);
117 15205 : invert_arg_mapping (e, this);
118 15205 : redirected = false;
119 15205 : }
120 :
121 : /* Initialize the summary for a callback call CBE of internal function IFN. */
122 : callback_info *
123 175 : callback_info::create_internal_summary (cgraph_edge *cbe, internal_fn ifn)
124 : {
125 175 : callback_info_sum_t::check_create_info_sum ();
126 175 : callback_info *ci = callback_info_sum->get_create (cbe);
127 175 : switch (ifn)
128 : {
129 175 : case IFN_ASSUME:
130 175 : {
131 175 : ci->fn_idx = 0;
132 175 : unsigned n = gimple_call_num_args (cbe->call_stmt);
133 383 : for (unsigned i = 0; i < n - 1; i++)
134 208 : ci->arg_mapping.safe_push (i + 1);
135 175 : invert_arg_mapping (cbe, ci);
136 175 : break;
137 : }
138 0 : default:
139 0 : gcc_unreachable ();
140 : }
141 175 : ci->redirected = false;
142 175 : return ci;
143 : }
144 :
145 : /* Populates the callback_info_sum if it's NULL. */
146 : void
147 3454252 : callback_info_sum_t::check_create_info_sum (void)
148 : {
149 3454252 : if (!callback_info_sum)
150 260238 : callback_info_sum = new callback_info_sum_t (symtab, false);
151 3454252 : }
152 :
153 : /* Frees the callback_info_sum pointer. */
154 : void
155 266934 : callback_info_sum_t::free_info_sum (void)
156 : {
157 266934 : if (callback_info_sum)
158 259837 : delete callback_info_sum;
159 266934 : callback_info_sum = NULL;
160 266934 : }
161 :
162 : /* Duplication function for the cgraph_edge duplication hook. */
163 : void
164 664 : callback_info_sum_t::duplicate (cgraph_edge *, cgraph_edge *,
165 : callback_info *src_s, callback_info *dst_s)
166 : {
167 664 : dst_s->fn_idx = src_s->fn_idx;
168 : /* Might need to be adjusted in the future, if argument modifications are
169 : implemented. */
170 664 : dst_s->arg_mapping = src_s->arg_mapping.copy ();
171 664 : dst_s->inverted_mapping = src_s->inverted_mapping.copy ();
172 664 : dst_s->redirected = src_s->redirected;
173 664 : }
174 :
175 : /* Return the argument at IDX if CI is null, otherwise use the mapping in CI and
176 : return the remapped argument. Presumes that the remapped index is not
177 : ARG_MAPPING_UNKNOWN_IDX. */
178 : tree
179 1371108 : callback_call_arg_summary (const gimple *stmt, int idx, callback_info *ci)
180 : {
181 1371108 : if (ci)
182 45 : idx = ci->arg_mapping[idx];
183 1371108 : gcc_checking_assert (idx != ARG_MAPPING_UNKNOWN_IDX);
184 1371108 : return gimple_call_arg (stmt, idx);
185 : }
186 :
187 : /* Return the number of arguments of STMT if CI is null,
188 : ci->arg_mapping.length() otherwise. */
189 : unsigned
190 3701048 : callback_call_num_args_summary (const gcall *stmt, callback_info *ci)
191 : {
192 3701048 : if (ci)
193 239 : return ci->arg_mapping.length ();
194 3700809 : return gimple_call_num_args (stmt);
195 : }
196 :
197 : /* Returns TRUE iff STMT is an internal call statement to IFN_ASSUME. */
198 : bool
199 6858913 : callback_ifn_assume_call_p (const gimple *stmt)
200 : {
201 6858913 : return is_gimple_call (stmt) && gimple_call_internal_p (stmt)
202 7292398 : && (gimple_call_internal_fn (stmt) == IFN_ASSUME);
203 : }
204 :
205 : /* Returns TRUE iff STMT is an internal call statement to IFN_ASSUME with a
206 : callback edge pointing to the outlined body. */
207 : bool
208 190183 : callback_ifn_assume_call_with_edge_p (cgraph_node *caller, gimple *stmt)
209 : {
210 : /* Fast path for most cases. */
211 190183 : if (!callback_ifn_assume_call_p (stmt))
212 : return false;
213 :
214 177 : cgraph_edge *cbei = caller->get_edge (stmt);
215 :
216 177 : return cbei != nullptr;
217 : }
|