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 16114 : invert_arg_mapping (cgraph_edge *e, callback_info *ci)
44 : {
45 16114 : auto &mapping = ci->arg_mapping;
46 16114 : auto &inverted_mapping = ci->inverted_mapping;
47 16114 : unsigned num = gimple_call_num_args (e->call_stmt);
48 16114 : inverted_mapping.reserve (num);
49 111730 : for (unsigned i = 0; i < num; i++)
50 79502 : inverted_mapping.quick_push (ARG_MAPPING_UNKNOWN_IDX);
51 :
52 32331 : for (unsigned i = 0; i < mapping.length (); i++)
53 16217 : if (mapping[i] != ARG_MAPPING_UNKNOWN_IDX)
54 16217 : inverted_mapping[mapping[i]] = i;
55 16114 : }
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 14112 : callback_info::get_id () const
108 : {
109 14112 : return fn_idx;
110 : }
111 :
112 : void
113 15903 : callback_info::init (cgraph_edge *e, unsigned fn_idx, tree attr)
114 : {
115 15903 : this->fn_idx = fn_idx;
116 15903 : arg_mapping = callback_get_arg_mapping_from_attr (attr);
117 15903 : invert_arg_mapping (e, this);
118 15903 : redirected = false;
119 15903 : }
120 :
121 : /* Initialize the summary for a callback call CBE of internal function IFN. */
122 : callback_info *
123 211 : callback_info::create_internal_summary (cgraph_edge *cbe, internal_fn ifn)
124 : {
125 211 : callback_info_sum_t::check_create_info_sum ();
126 211 : callback_info *ci = callback_info_sum->get_create (cbe);
127 211 : switch (ifn)
128 : {
129 211 : case IFN_ASSUME:
130 211 : {
131 211 : ci->fn_idx = 0;
132 211 : unsigned n = gimple_call_num_args (cbe->call_stmt);
133 524 : for (unsigned i = 0; i < n - 1; i++)
134 313 : ci->arg_mapping.safe_push (i + 1);
135 211 : invert_arg_mapping (cbe, ci);
136 211 : break;
137 : }
138 0 : default:
139 0 : gcc_unreachable ();
140 : }
141 211 : ci->redirected = false;
142 211 : return ci;
143 : }
144 :
145 : /* Populates the callback_info_sum if it's NULL. */
146 : void
147 3474411 : callback_info_sum_t::check_create_info_sum (void)
148 : {
149 3474411 : if (!callback_info_sum)
150 265730 : callback_info_sum = new callback_info_sum_t (symtab, false);
151 3474411 : }
152 :
153 : /* Frees the callback_info_sum pointer. */
154 : void
155 272484 : callback_info_sum_t::free_info_sum (void)
156 : {
157 272484 : if (callback_info_sum)
158 265328 : delete callback_info_sum;
159 272484 : callback_info_sum = NULL;
160 272484 : }
161 :
162 : /* Duplication function for the cgraph_edge duplication hook. */
163 : void
164 705 : callback_info_sum_t::duplicate (cgraph_edge *, cgraph_edge *,
165 : callback_info *src_s, callback_info *dst_s)
166 : {
167 705 : dst_s->fn_idx = src_s->fn_idx;
168 : /* Might need to be adjusted in the future, if argument modifications are
169 : implemented. */
170 705 : dst_s->arg_mapping = src_s->arg_mapping.copy ();
171 705 : dst_s->inverted_mapping = src_s->inverted_mapping.copy ();
172 705 : dst_s->redirected = src_s->redirected;
173 705 : }
174 :
175 : /* Return the number of arguments of STMT if CI is null,
176 : ci->arg_mapping.length() otherwise. */
177 : unsigned
178 3078320 : callback_call_num_args_summary (const gcall *stmt, callback_info *ci)
179 : {
180 3078320 : if (ci)
181 211 : return ci->arg_mapping.length ();
182 3078109 : return gimple_call_num_args (stmt);
183 : }
184 :
185 : /* Returns TRUE iff STMT is an internal call statement to IFN_ASSUME. */
186 : bool
187 6169134 : callback_ifn_assume_call_p (const gimple *stmt)
188 : {
189 6169134 : return is_gimple_call (stmt) && gimple_call_internal_p (stmt)
190 6596140 : && (gimple_call_internal_fn (stmt) == IFN_ASSUME);
191 : }
192 :
193 : /* Returns TRUE iff STMT is an internal call statement to IFN_ASSUME with a
194 : callback edge pointing to the outlined body. */
195 : bool
196 192213 : callback_ifn_assume_call_with_edge_p (cgraph_node *caller, gimple *stmt)
197 : {
198 : /* Fast path for most cases. */
199 192213 : if (!callback_ifn_assume_call_p (stmt))
200 : return false;
201 :
202 213 : cgraph_edge *cbei = caller->get_edge (stmt);
203 :
204 213 : return cbei != nullptr;
205 : }
|