Branch data Line data Source code
1 : : /* Callgraph handling code.
2 : : Copyright (C) 2003-2025 Free Software Foundation, Inc.
3 : : Contributed by Jan Hubicka
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it under
8 : : the terms of the GNU General Public License as published by the Free
9 : : Software Foundation; either version 3, or (at your option) any later
10 : : version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : : 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 : : /* This file contains basic routines manipulating call graph
22 : :
23 : : The call-graph is a data structure designed for inter-procedural
24 : : optimization. It represents a multi-graph where nodes are functions
25 : : (symbols within symbol table) and edges are call sites. */
26 : :
27 : : #include "config.h"
28 : : #include "system.h"
29 : : #include "coretypes.h"
30 : : #include "backend.h"
31 : : #include "target.h"
32 : : #include "rtl.h"
33 : : #include "tree.h"
34 : : #include "gimple.h"
35 : : #include "predict.h"
36 : : #include "alloc-pool.h"
37 : : #include "gimple-ssa.h"
38 : : #include "cgraph.h"
39 : : #include "lto-streamer.h"
40 : : #include "fold-const.h"
41 : : #include "varasm.h"
42 : : #include "calls.h"
43 : : #include "print-tree.h"
44 : : #include "langhooks.h"
45 : : #include "intl.h"
46 : : #include "tree-eh.h"
47 : : #include "gimple-iterator.h"
48 : : #include "tree-cfg.h"
49 : : #include "tree-ssa.h"
50 : : #include "value-prof.h"
51 : : #include "ipa-utils.h"
52 : : #include "symbol-summary.h"
53 : : #include "tree-vrp.h"
54 : : #include "sreal.h"
55 : : #include "ipa-cp.h"
56 : : #include "ipa-prop.h"
57 : : #include "ipa-fnsummary.h"
58 : : #include "cfgloop.h"
59 : : #include "gimple-pretty-print.h"
60 : : #include "tree-dfa.h"
61 : : #include "profile.h"
62 : : #include "context.h"
63 : : #include "gimplify.h"
64 : : #include "stringpool.h"
65 : : #include "attribs.h"
66 : : #include "selftest.h"
67 : : #include "tree-into-ssa.h"
68 : : #include "ipa-inline.h"
69 : : #include "tree-nested.h"
70 : : #include "symtab-thunks.h"
71 : : #include "symtab-clones.h"
72 : : #include "attr-callback.h"
73 : :
74 : : /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
75 : : #include "tree-pass.h"
76 : :
77 : : /* Queue of cgraph nodes scheduled to be lowered. */
78 : : symtab_node *x_cgraph_nodes_queue;
79 : : #define cgraph_nodes_queue ((cgraph_node *)x_cgraph_nodes_queue)
80 : :
81 : : /* Symbol table global context. */
82 : : symbol_table *symtab;
83 : :
84 : : /* List of hooks triggered on cgraph_edge events. */
85 : : struct cgraph_edge_hook_list {
86 : : cgraph_edge_hook hook;
87 : : void *data;
88 : : struct cgraph_edge_hook_list *next;
89 : : };
90 : :
91 : : /* List of hooks triggered on cgraph_node events. */
92 : : struct cgraph_node_hook_list {
93 : : cgraph_node_hook hook;
94 : : void *data;
95 : : struct cgraph_node_hook_list *next;
96 : : };
97 : :
98 : : /* List of hooks triggered on events involving two cgraph_edges. */
99 : : struct cgraph_2edge_hook_list {
100 : : cgraph_2edge_hook hook;
101 : : void *data;
102 : : struct cgraph_2edge_hook_list *next;
103 : : };
104 : :
105 : : /* List of hooks triggered on events involving two cgraph_nodes. */
106 : : struct cgraph_2node_hook_list {
107 : : cgraph_2node_hook hook;
108 : : void *data;
109 : : struct cgraph_2node_hook_list *next;
110 : : };
111 : :
112 : : /* Hash descriptor for cgraph_function_version_info. */
113 : :
114 : : struct function_version_hasher : ggc_ptr_hash<cgraph_function_version_info>
115 : : {
116 : : static hashval_t hash (cgraph_function_version_info *);
117 : : static bool equal (cgraph_function_version_info *,
118 : : cgraph_function_version_info *);
119 : : };
120 : :
121 : : /* Map a cgraph_node to cgraph_function_version_info using this htab.
122 : : The cgraph_function_version_info has a THIS_NODE field that is the
123 : : corresponding cgraph_node.. */
124 : :
125 : : static GTY(()) hash_table<function_version_hasher> *cgraph_fnver_htab = NULL;
126 : :
127 : : /* Hash function for cgraph_fnver_htab. */
128 : : hashval_t
129 : 289052 : function_version_hasher::hash (cgraph_function_version_info *ptr)
130 : : {
131 : 289052 : int uid = ptr->this_node->get_uid ();
132 : 289052 : return (hashval_t)(uid);
133 : : }
134 : :
135 : : /* eq function for cgraph_fnver_htab. */
136 : : bool
137 : 262095 : function_version_hasher::equal (cgraph_function_version_info *n1,
138 : : cgraph_function_version_info *n2)
139 : : {
140 : 262095 : return n1->this_node->get_uid () == n2->this_node->get_uid ();
141 : : }
142 : :
143 : : /* Mark as GC root all allocated nodes. */
144 : : static GTY(()) struct cgraph_function_version_info *
145 : : version_info_node = NULL;
146 : :
147 : : /* Return true if NODE's address can be compared. */
148 : :
149 : : bool
150 : 5006055 : symtab_node::address_can_be_compared_p ()
151 : : {
152 : : /* Address of virtual tables and functions is never compared. */
153 : 5006055 : if (DECL_VIRTUAL_P (decl))
154 : : return false;
155 : : /* Address of C++ cdtors is never compared. */
156 : 4917368 : if (is_a <cgraph_node *> (this)
157 : 538442 : && (DECL_CXX_CONSTRUCTOR_P (decl)
158 : 534152 : || DECL_CXX_DESTRUCTOR_P (decl)))
159 : : return false;
160 : : /* Constant pool symbols addresses are never compared.
161 : : flag_merge_constants permits us to assume the same on readonly vars. */
162 : 4911663 : if (is_a <varpool_node *> (this)
163 : 4378926 : && (DECL_IN_CONSTANT_POOL (decl)
164 : 4378923 : || ((flag_merge_constants >= 2 || DECL_MERGEABLE (decl))
165 : 2275 : && TREE_READONLY (decl) && !TREE_THIS_VOLATILE (decl))))
166 : 2271 : return false;
167 : : return true;
168 : : }
169 : :
170 : : /* Get the cgraph_function_version_info node corresponding to node. */
171 : : cgraph_function_version_info *
172 : 115805227 : cgraph_node::function_version (void)
173 : : {
174 : 115805227 : cgraph_function_version_info key;
175 : 115805227 : key.this_node = this;
176 : :
177 : 115805227 : if (cgraph_fnver_htab == NULL)
178 : : return NULL;
179 : :
180 : 46322 : return cgraph_fnver_htab->find (&key);
181 : : }
182 : :
183 : : /* If profile is IPA, turn it into local one. */
184 : : void
185 : 0 : cgraph_node::make_profile_local ()
186 : : {
187 : 0 : if (!count.ipa ().initialized_p ())
188 : : return;
189 : 0 : if (!(count == profile_count::zero ()))
190 : 0 : count = count.guessed_local ();
191 : 0 : for (cgraph_edge *e = callees; e; e = e->next_callee)
192 : : {
193 : 0 : if (!e->inline_failed)
194 : 0 : e->callee->make_profile_local ();
195 : 0 : if (!(e->count == profile_count::zero ()))
196 : 0 : e->count = e->count.guessed_local ();
197 : : }
198 : 0 : for (cgraph_edge *e = indirect_calls; e; e = e->next_callee)
199 : 0 : if (!(e->count == profile_count::zero ()))
200 : 0 : e->count = e->count.guessed_local ();
201 : : }
202 : :
203 : : /* Turn profile to global0. Walk into inlined functions.
204 : : QUALITY must be GUESSED_GLOBAL0, GUESSED_GLOBAL0_ADJUSTED
205 : : or GUESSED_GLOBAL0_AFDO */
206 : : void
207 : 5 : cgraph_node::make_profile_global0 (profile_quality quality)
208 : : {
209 : 5 : if (count == profile_count::zero ())
210 : : ;
211 : 5 : else if (quality == GUESSED_GLOBAL0)
212 : : {
213 : 5 : if (count.quality () == GUESSED_GLOBAL0)
214 : : return;
215 : 5 : count = count.global0 ();
216 : : }
217 : 0 : else if (quality == GUESSED_GLOBAL0_ADJUSTED)
218 : : {
219 : 0 : if (count.quality () == GUESSED_GLOBAL0
220 : 0 : || count.quality () == GUESSED_GLOBAL0_ADJUSTED)
221 : : return;
222 : 0 : count = count.global0adjusted ();
223 : : }
224 : 0 : else if (quality == GUESSED_GLOBAL0_AFDO)
225 : : {
226 : 0 : if (count.quality () == GUESSED_GLOBAL0
227 : 0 : || count.quality () == GUESSED_GLOBAL0_ADJUSTED
228 : 0 : || count.quality () == GUESSED_GLOBAL0_AFDO)
229 : : return;
230 : 0 : count = count.global0afdo ();
231 : : }
232 : : else
233 : 0 : gcc_unreachable ();
234 : 6 : for (cgraph_edge *e = callees; e; e = e->next_callee)
235 : : {
236 : 1 : if (!e->inline_failed)
237 : 0 : e->callee->make_profile_global0 (quality);
238 : 1 : if (e->count == profile_count::zero ())
239 : : ;
240 : 1 : else if (quality == GUESSED_GLOBAL0)
241 : 1 : e->count = e->count.global0 ();
242 : 0 : else if (quality == GUESSED_GLOBAL0_ADJUSTED)
243 : 0 : e->count = e->count.global0adjusted ();
244 : 0 : else if (quality == GUESSED_GLOBAL0_AFDO)
245 : 0 : e->count = e->count.global0afdo ();
246 : : else
247 : 0 : gcc_unreachable ();
248 : : }
249 : 5 : for (cgraph_edge *e = indirect_calls; e; e = e->next_callee)
250 : 0 : if (e->count == profile_count::zero ())
251 : : ;
252 : 0 : else if (quality == GUESSED_GLOBAL0)
253 : 0 : e->count = e->count.global0 ();
254 : 0 : else if (quality == GUESSED_GLOBAL0_ADJUSTED)
255 : 0 : e->count = e->count.global0adjusted ();
256 : 0 : else if (quality == GUESSED_GLOBAL0_AFDO)
257 : 0 : e->count = e->count.global0afdo ();
258 : : else
259 : 0 : gcc_unreachable ();
260 : : }
261 : :
262 : : /* Scale profile by NUM/DEN. Walk into inlined functions. */
263 : :
264 : : void
265 : 1638684 : cgraph_node::apply_scale (profile_count num, profile_count den)
266 : : {
267 : 1754269 : if (num == den && !(num == profile_count::zero ()))
268 : 113720 : return;
269 : :
270 : 2741973 : for (cgraph_edge *e = callees; e; e = e->next_callee)
271 : : {
272 : 1217009 : if (!e->inline_failed)
273 : 156066 : e->callee->apply_scale (num, den);
274 : 1217009 : e->count = e->count.apply_scale (num, den);
275 : : }
276 : 1554982 : for (cgraph_edge *e = indirect_calls; e; e = e->next_callee)
277 : 30018 : e->count = e->count.apply_scale (num, den);
278 : 1524964 : count = count.apply_scale (num, den);
279 : : }
280 : :
281 : : /* Scale profile to given IPA_COUNT.
282 : : IPA_COUNT should pass ipa_p () with a single exception.
283 : : It can be also GUESSED_LOCAL in case we want to
284 : : drop any IPA info about the profile. */
285 : :
286 : : void
287 : 30 : cgraph_node::scale_profile_to (profile_count ipa_count)
288 : : {
289 : : /* If we do not know the adjustment, it is better to keep profile
290 : : as it is. */
291 : 30 : if (!ipa_count.initialized_p ()
292 : 30 : || ipa_count == count)
293 : 10 : return;
294 : : /* ipa-cp converts value to guessed-local in case it believes
295 : : that we lost track of IPA profile. */
296 : 25 : if (ipa_count.quality () == GUESSED_LOCAL)
297 : : {
298 : 0 : make_profile_local ();
299 : 0 : return;
300 : : }
301 : 25 : if (ipa_count == profile_count::zero ())
302 : : {
303 : 5 : make_profile_global0 (GUESSED_GLOBAL0);
304 : 5 : return;
305 : : }
306 : 20 : if (ipa_count == profile_count::adjusted_zero ())
307 : : {
308 : 0 : make_profile_global0 (GUESSED_GLOBAL0_ADJUSTED);
309 : 0 : return;
310 : : }
311 : 40 : gcc_assert (ipa_count.ipa () == ipa_count
312 : : && !inlined_to);
313 : 20 : profile_count num = count.combine_with_ipa_count (ipa_count);
314 : 20 : profile_count den = count;
315 : 20 : profile_count::adjust_for_ipa_scaling (&num, &den);
316 : : }
317 : :
318 : : /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
319 : : corresponding to cgraph_node NODE. */
320 : : cgraph_function_version_info *
321 : 1523 : cgraph_node::insert_new_function_version (void)
322 : : {
323 : 1523 : version_info_node = NULL;
324 : 1523 : version_info_node = ggc_cleared_alloc<cgraph_function_version_info> ();
325 : 1523 : version_info_node->this_node = this;
326 : 1523 : version_info_node->assembler_name = DECL_ASSEMBLER_NAME (this->decl);
327 : :
328 : 1523 : if (cgraph_fnver_htab == NULL)
329 : 197 : cgraph_fnver_htab = hash_table<function_version_hasher>::create_ggc (2);
330 : :
331 : 1523 : *cgraph_fnver_htab->find_slot (version_info_node, INSERT)
332 : 1523 : = version_info_node;
333 : 1523 : return version_info_node;
334 : : }
335 : :
336 : : /* Remove the cgraph_function_version_info node given by DECL_V. */
337 : : void
338 : 95662580 : cgraph_node::delete_function_version (cgraph_function_version_info *decl_v)
339 : : {
340 : 95662580 : if (decl_v == NULL)
341 : : return;
342 : :
343 : 268 : if (version_info_node == decl_v)
344 : 205 : version_info_node = NULL;
345 : :
346 : 268 : if (decl_v->prev != NULL)
347 : 162 : decl_v->prev->next = decl_v->next;
348 : :
349 : 268 : if (decl_v->next != NULL)
350 : 226 : decl_v->next->prev = decl_v->prev;
351 : :
352 : 268 : if (cgraph_fnver_htab != NULL)
353 : 268 : cgraph_fnver_htab->remove_elt (decl_v);
354 : : }
355 : :
356 : : /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
357 : : DECL is a duplicate declaration. */
358 : : void
359 : 258 : cgraph_node::delete_function_version_by_decl (tree decl)
360 : : {
361 : 258 : cgraph_node *decl_node = cgraph_node::get (decl);
362 : :
363 : 258 : if (decl_node == NULL)
364 : : return;
365 : :
366 : 204 : delete_function_version (decl_node->function_version ());
367 : :
368 : 204 : decl_node->remove ();
369 : : }
370 : :
371 : : /* Add decl to the structure of semantically identical function versions.
372 : : The node is inserted at the point maintaining the priority ordering on the
373 : : versions. */
374 : : void
375 : 8908 : cgraph_node::add_function_version (cgraph_function_version_info *fn_v,
376 : : tree decl)
377 : : {
378 : 8908 : cgraph_node *decl_node = cgraph_node::get_create (decl);
379 : 8908 : cgraph_function_version_info *decl_v = NULL;
380 : :
381 : 8908 : gcc_assert (decl_node != NULL);
382 : :
383 : 8908 : decl_v = decl_node->function_version ();
384 : :
385 : : /* If the nodes are already linked, skip. */
386 : 8908 : if (decl_v != NULL && (decl_v->next || decl_v->prev))
387 : : return;
388 : :
389 : 175 : if (decl_v == NULL)
390 : 175 : decl_v = decl_node->insert_new_function_version ();
391 : :
392 : 175 : gcc_assert (decl_v);
393 : 1057 : gcc_assert (fn_v);
394 : :
395 : : /* Go to start of the FMV structure. */
396 : 5509 : while (fn_v->prev)
397 : : fn_v = fn_v->prev;
398 : :
399 : 1057 : cgraph_function_version_info *insert_point_before = NULL;
400 : 1057 : cgraph_function_version_info *insert_point_after = fn_v;
401 : :
402 : : /* Find the insertion point for the new version to maintain ordering.
403 : : The default node must always go at the beginning. */
404 : 1057 : if (!is_function_default_version (decl))
405 : : while (insert_point_after
406 : 9987 : && (targetm.compare_version_priority
407 : 4802 : (decl, insert_point_after->this_node->decl) > 0
408 : 696 : || is_function_default_version
409 : 696 : (insert_point_after->this_node->decl)
410 : 575 : || lookup_attribute
411 : 575 : ("target_clones",
412 : 575 : DECL_ATTRIBUTES (insert_point_after->this_node->decl))))
413 : : {
414 : 4227 : insert_point_before = insert_point_after;
415 : 4227 : insert_point_after = insert_point_after->next;
416 : : }
417 : :
418 : 1057 : decl_v->prev = insert_point_before;
419 : 1057 : decl_v->next= insert_point_after;
420 : :
421 : 1057 : if (insert_point_before)
422 : 922 : insert_point_before->next = decl_v;
423 : 1057 : if (insert_point_after)
424 : 674 : insert_point_after->prev = decl_v;
425 : : }
426 : :
427 : : /* Initialize callgraph dump file. */
428 : :
429 : : void
430 : 304539 : symbol_table::initialize (void)
431 : : {
432 : 304539 : if (!dump_file)
433 : 304537 : dump_file = dump_begin (TDI_cgraph, NULL);
434 : :
435 : 304539 : if (!ipa_clones_dump_file)
436 : 304539 : ipa_clones_dump_file = dump_begin (TDI_clones, NULL);
437 : 304539 : }
438 : :
439 : : /* Allocate new callgraph node and insert it into basic data structures. */
440 : :
441 : : cgraph_node *
442 : 99395574 : symbol_table::create_empty (void)
443 : : {
444 : 99395574 : cgraph_count++;
445 : 99395574 : return new (ggc_alloc<cgraph_node> ()) cgraph_node ();
446 : : }
447 : :
448 : : /* Register HOOK to be called with DATA on each removed edge. */
449 : : cgraph_edge_hook_list *
450 : 1901237 : symbol_table::add_edge_removal_hook (cgraph_edge_hook hook, void *data)
451 : : {
452 : 1901237 : cgraph_edge_hook_list *entry;
453 : 3802474 : cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
454 : :
455 : 1901237 : entry = (cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
456 : 1901237 : entry->hook = hook;
457 : 1901237 : entry->data = data;
458 : 1901237 : entry->next = NULL;
459 : 6577714 : while (*ptr)
460 : 4676477 : ptr = &(*ptr)->next;
461 : 1901237 : *ptr = entry;
462 : 1901237 : return entry;
463 : : }
464 : :
465 : : /* Remove ENTRY from the list of hooks called on removing edges. */
466 : : void
467 : 1901223 : symbol_table::remove_edge_removal_hook (cgraph_edge_hook_list *entry)
468 : : {
469 : 1901223 : cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
470 : :
471 : 5184064 : while (*ptr != entry)
472 : 3282841 : ptr = &(*ptr)->next;
473 : 1901223 : *ptr = entry->next;
474 : 1901223 : free (entry);
475 : 1901223 : }
476 : :
477 : : /* Call all edge removal hooks. */
478 : : void
479 : 44594872 : symbol_table::call_edge_removal_hooks (cgraph_edge *e)
480 : : {
481 : 44594872 : cgraph_edge_hook_list *entry = m_first_edge_removal_hook;
482 : 72448410 : while (entry)
483 : : {
484 : 27853538 : entry->hook (e, entry->data);
485 : 27853538 : entry = entry->next;
486 : : }
487 : 44594872 : }
488 : :
489 : : /* Register HOOK to be called with DATA on each removed node. */
490 : : cgraph_node_hook_list *
491 : 8099189 : symbol_table::add_cgraph_removal_hook (cgraph_node_hook hook, void *data)
492 : : {
493 : 8099189 : cgraph_node_hook_list *entry;
494 : 16198378 : cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
495 : :
496 : 8099189 : entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
497 : 8099189 : entry->hook = hook;
498 : 8099189 : entry->data = data;
499 : 8099189 : entry->next = NULL;
500 : 42382258 : while (*ptr)
501 : 34283069 : ptr = &(*ptr)->next;
502 : 8099189 : *ptr = entry;
503 : 8099189 : return entry;
504 : : }
505 : :
506 : : /* Remove ENTRY from the list of hooks called on removing nodes. */
507 : : void
508 : 7991309 : symbol_table::remove_cgraph_removal_hook (cgraph_node_hook_list *entry)
509 : : {
510 : 7991309 : cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
511 : :
512 : 37850838 : while (*ptr != entry)
513 : 29859529 : ptr = &(*ptr)->next;
514 : 7991309 : *ptr = entry->next;
515 : 7991309 : free (entry);
516 : 7991309 : }
517 : :
518 : : /* Call all node removal hooks. */
519 : : void
520 : 95695148 : symbol_table::call_cgraph_removal_hooks (cgraph_node *node)
521 : : {
522 : 95695148 : cgraph_node_hook_list *entry = m_first_cgraph_removal_hook;
523 : 136043517 : while (entry)
524 : : {
525 : 40348369 : entry->hook (node, entry->data);
526 : 40348369 : entry = entry->next;
527 : : }
528 : 95695148 : }
529 : :
530 : : /* Call all node removal hooks. */
531 : : void
532 : 120443 : symbol_table::call_cgraph_insertion_hooks (cgraph_node *node)
533 : : {
534 : 120443 : cgraph_node_hook_list *entry = m_first_cgraph_insertion_hook;
535 : 362771 : while (entry)
536 : : {
537 : 242328 : entry->hook (node, entry->data);
538 : 242328 : entry = entry->next;
539 : : }
540 : 120443 : }
541 : :
542 : :
543 : : /* Register HOOK to be called with DATA on each inserted node. */
544 : : cgraph_node_hook_list *
545 : 8584577 : symbol_table::add_cgraph_insertion_hook (cgraph_node_hook hook, void *data)
546 : : {
547 : 8584577 : cgraph_node_hook_list *entry;
548 : 17169154 : cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
549 : :
550 : 8584577 : entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
551 : 8584577 : entry->hook = hook;
552 : 8584577 : entry->data = data;
553 : 8584577 : entry->next = NULL;
554 : 25429256 : while (*ptr)
555 : 16844679 : ptr = &(*ptr)->next;
556 : 8584577 : *ptr = entry;
557 : 8584577 : return entry;
558 : : }
559 : :
560 : : /* Remove ENTRY from the list of hooks called on inserted nodes. */
561 : : void
562 : 8437505 : symbol_table::remove_cgraph_insertion_hook (cgraph_node_hook_list *entry)
563 : : {
564 : 8437505 : cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
565 : :
566 : 23308022 : while (*ptr != entry)
567 : 14870517 : ptr = &(*ptr)->next;
568 : 8437505 : *ptr = entry->next;
569 : 8437505 : free (entry);
570 : 8437505 : }
571 : :
572 : : /* Register HOOK to be called with DATA on each duplicated edge. */
573 : : cgraph_2edge_hook_list *
574 : 1670037 : symbol_table::add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
575 : : {
576 : 1670037 : cgraph_2edge_hook_list *entry;
577 : 3340074 : cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
578 : :
579 : 1670037 : entry = (cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
580 : 1670037 : entry->hook = hook;
581 : 1670037 : entry->data = data;
582 : 1670037 : entry->next = NULL;
583 : 5361852 : while (*ptr)
584 : 3691815 : ptr = &(*ptr)->next;
585 : 1670037 : *ptr = entry;
586 : 1670037 : return entry;
587 : : }
588 : :
589 : : /* Remove ENTRY from the list of hooks called on duplicating edges. */
590 : : void
591 : 1670023 : symbol_table::remove_edge_duplication_hook (cgraph_2edge_hook_list *entry)
592 : : {
593 : 1670023 : cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
594 : :
595 : 3968202 : while (*ptr != entry)
596 : 2298179 : ptr = &(*ptr)->next;
597 : 1670023 : *ptr = entry->next;
598 : 1670023 : free (entry);
599 : 1670023 : }
600 : :
601 : : /* Call all edge duplication hooks. */
602 : : void
603 : 6916851 : symbol_table::call_edge_duplication_hooks (cgraph_edge *cs1, cgraph_edge *cs2)
604 : : {
605 : 6916851 : cgraph_2edge_hook_list *entry = m_first_edge_duplicated_hook;
606 : 20408399 : while (entry)
607 : : {
608 : 13491548 : entry->hook (cs1, cs2, entry->data);
609 : 13491548 : entry = entry->next;
610 : : }
611 : 6916851 : }
612 : :
613 : : /* Register HOOK to be called with DATA on each duplicated node. */
614 : : cgraph_2node_hook_list *
615 : 7957643 : symbol_table::add_cgraph_duplication_hook (cgraph_2node_hook hook, void *data)
616 : : {
617 : 7957643 : cgraph_2node_hook_list *entry;
618 : 15915286 : cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
619 : :
620 : 7957643 : entry = (cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
621 : 7957643 : entry->hook = hook;
622 : 7957643 : entry->data = data;
623 : 7957643 : entry->next = NULL;
624 : 39379831 : while (*ptr)
625 : 31422188 : ptr = &(*ptr)->next;
626 : 7957643 : *ptr = entry;
627 : 7957643 : return entry;
628 : : }
629 : :
630 : : /* Remove ENTRY from the list of hooks called on duplicating nodes. */
631 : : void
632 : 7862723 : symbol_table::remove_cgraph_duplication_hook (cgraph_2node_hook_list *entry)
633 : : {
634 : 7862723 : cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
635 : :
636 : 35808027 : while (*ptr != entry)
637 : 27945304 : ptr = &(*ptr)->next;
638 : 7862723 : *ptr = entry->next;
639 : 7862723 : free (entry);
640 : 7862723 : }
641 : :
642 : : /* Call all node duplication hooks. */
643 : : void
644 : 3171837 : symbol_table::call_cgraph_duplication_hooks (cgraph_node *node,
645 : : cgraph_node *node2)
646 : : {
647 : 3171837 : cgraph_2node_hook_list *entry = m_first_cgraph_duplicated_hook;
648 : 22459772 : while (entry)
649 : : {
650 : 19287935 : entry->hook (node, node2, entry->data);
651 : 19287935 : entry = entry->next;
652 : : }
653 : 3171837 : }
654 : :
655 : : /* Return cgraph node assigned to DECL. Create new one when needed. */
656 : :
657 : : cgraph_node *
658 : 96022495 : cgraph_node::create (tree decl)
659 : : {
660 : 96022495 : cgraph_node *node = symtab->create_empty ();
661 : 96022495 : gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
662 : :
663 : 96022495 : node->decl = decl;
664 : 96022495 : node->semantic_interposition = opt_for_fn (decl, flag_semantic_interposition);
665 : :
666 : 95967409 : if ((flag_openacc || flag_openmp)
667 : 96256454 : && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)))
668 : : {
669 : 8403 : node->offloadable = 1;
670 : 8403 : if (ENABLE_OFFLOADING)
671 : : g->have_offload = true;
672 : : }
673 : :
674 : 96022495 : if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
675 : 120 : node->ifunc_resolver = true;
676 : :
677 : 96022495 : node->register_symbol ();
678 : 96022495 : maybe_record_nested_function (node);
679 : :
680 : 96022495 : return node;
681 : : }
682 : :
683 : : /* Try to find a call graph node for declaration DECL and if it does not exist
684 : : or if it corresponds to an inline clone, create a new one. */
685 : :
686 : : cgraph_node *
687 : 524710329 : cgraph_node::get_create (tree decl)
688 : : {
689 : 524710329 : cgraph_node *first_clone = cgraph_node::get (decl);
690 : :
691 : 524710329 : if (first_clone && !first_clone->inlined_to)
692 : : return first_clone;
693 : :
694 : 95954564 : cgraph_node *node = cgraph_node::create (decl);
695 : 95954564 : if (first_clone)
696 : : {
697 : 8 : first_clone->clone_of = node;
698 : 8 : node->clones = first_clone;
699 : 8 : node->order = first_clone->order;
700 : 8 : symtab->symtab_prevail_in_asm_name_hash (node);
701 : 8 : node->decl->decl_with_vis.symtab_node = node;
702 : 8 : if (dump_file && symtab->state != PARSING)
703 : 2 : fprintf (dump_file, "Introduced new external node "
704 : : "(%s) and turned into root of the clone tree.\n",
705 : : node->dump_name ());
706 : : }
707 : 95954556 : else if (dump_file && symtab->state != PARSING)
708 : 1138 : fprintf (dump_file, "Introduced new external node "
709 : : "(%s).\n", node->dump_name ());
710 : : return node;
711 : : }
712 : :
713 : : /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
714 : : the function body is associated with
715 : : (not necessarily cgraph_node (DECL)). */
716 : :
717 : : cgraph_node *
718 : 6887672 : cgraph_node::create_alias (tree alias, tree target)
719 : : {
720 : 6887672 : cgraph_node *alias_node;
721 : :
722 : 6887672 : gcc_assert (TREE_CODE (target) == FUNCTION_DECL
723 : : || TREE_CODE (target) == IDENTIFIER_NODE);
724 : 6887672 : gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
725 : 6887672 : alias_node = cgraph_node::get_create (alias);
726 : 6887672 : gcc_assert (!alias_node->definition);
727 : 6887672 : alias_node->alias_target = target;
728 : 6887672 : alias_node->definition = true;
729 : 6887672 : alias_node->alias = true;
730 : 6887672 : if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
731 : 47 : alias_node->transparent_alias = alias_node->weakref = true;
732 : 6887672 : if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (alias)))
733 : 315 : alias_node->ifunc_resolver = true;
734 : 6887672 : return alias_node;
735 : : }
736 : :
737 : : /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
738 : : and NULL otherwise.
739 : : Same body aliases are output whenever the body of DECL is output,
740 : : and cgraph_node::get (ALIAS) transparently returns
741 : : cgraph_node::get (DECL). */
742 : :
743 : : cgraph_node *
744 : 6868157 : cgraph_node::create_same_body_alias (tree alias, tree decl)
745 : : {
746 : 6868157 : cgraph_node *n;
747 : :
748 : : /* If aliases aren't supported by the assembler, fail. */
749 : 6868157 : if (!TARGET_SUPPORTS_ALIASES)
750 : : return NULL;
751 : :
752 : : /* Langhooks can create same body aliases of symbols not defined.
753 : : Those are useless. Drop them on the floor. */
754 : 6868157 : if (symtab->global_info_ready)
755 : : return NULL;
756 : :
757 : 6868157 : n = cgraph_node::create_alias (alias, decl);
758 : 6868157 : n->cpp_implicit_alias = true;
759 : 6868157 : if (symtab->cpp_implicit_aliases_done)
760 : 3608708 : n->resolve_alias (cgraph_node::get (decl));
761 : : return n;
762 : : }
763 : :
764 : : /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
765 : : aliases DECL with an adjustments made into the first parameter.
766 : : See comments in struct cgraph_thunk_info for detail on the parameters. */
767 : :
768 : : cgraph_node *
769 : 4398 : cgraph_node::create_thunk (tree alias, tree, bool this_adjusting,
770 : : HOST_WIDE_INT fixed_offset,
771 : : HOST_WIDE_INT virtual_value,
772 : : HOST_WIDE_INT indirect_offset,
773 : : tree virtual_offset,
774 : : tree real_alias)
775 : : {
776 : 4398 : cgraph_node *node;
777 : :
778 : 4398 : node = cgraph_node::get (alias);
779 : 4398 : if (node)
780 : 3664 : node->reset ();
781 : : else
782 : 734 : node = cgraph_node::create (alias);
783 : :
784 : : /* Make sure that if VIRTUAL_OFFSET is in sync with VIRTUAL_VALUE. */
785 : 4398 : gcc_checking_assert (virtual_offset
786 : : ? virtual_value == wi::to_wide (virtual_offset)
787 : : : virtual_value == 0);
788 : :
789 : 4398 : node->thunk = true;
790 : 4398 : node->definition = true;
791 : :
792 : 4398 : thunk_info *i;
793 : 4398 : thunk_info local_info;
794 : 4398 : if (symtab->state < CONSTRUCTION)
795 : : i = &local_info;
796 : : else
797 : 0 : i = thunk_info::get_create (node);
798 : 4398 : i->fixed_offset = fixed_offset;
799 : 4398 : i->virtual_value = virtual_value;
800 : 4398 : i->indirect_offset = indirect_offset;
801 : 4398 : i->alias = real_alias;
802 : 4398 : i->this_adjusting = this_adjusting;
803 : 4398 : i->virtual_offset_p = virtual_offset != NULL;
804 : 4398 : if (symtab->state < CONSTRUCTION)
805 : 4398 : i->register_early (node);
806 : :
807 : 4398 : return node;
808 : : }
809 : :
810 : : /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
811 : : Return NULL if there's no such node. */
812 : :
813 : : cgraph_node *
814 : 0 : cgraph_node::get_for_asmname (tree asmname)
815 : : {
816 : : /* We do not want to look at inline clones. */
817 : 0 : for (symtab_node *node = symtab_node::get_for_asmname (asmname);
818 : 0 : node;
819 : 0 : node = node->next_sharing_asm_name)
820 : : {
821 : 0 : cgraph_node *cn = dyn_cast <cgraph_node *> (node);
822 : 0 : if (cn && !cn->inlined_to)
823 : : return cn;
824 : : }
825 : : return NULL;
826 : : }
827 : :
828 : : /* Returns a hash value for X (which really is a cgraph_edge). */
829 : :
830 : : hashval_t
831 : 218409909 : cgraph_edge_hasher::hash (cgraph_edge *e)
832 : : {
833 : : /* This is a really poor hash function, but it is what htab_hash_pointer
834 : : uses. */
835 : 218409909 : return (hashval_t) ((intptr_t)e->call_stmt >> 3);
836 : : }
837 : :
838 : : /* Returns a hash value for X (which really is a cgraph_edge). */
839 : :
840 : : hashval_t
841 : 43148919 : cgraph_edge_hasher::hash (gimple *call_stmt)
842 : : {
843 : : /* This is a really poor hash function, but it is what htab_hash_pointer
844 : : uses. */
845 : 43148919 : return (hashval_t) ((intptr_t)call_stmt >> 3);
846 : : }
847 : :
848 : : /* Return nonzero if the call_stmt of cgraph_edge X is stmt *Y. */
849 : :
850 : : inline bool
851 : 267367036 : cgraph_edge_hasher::equal (cgraph_edge *x, gimple *y)
852 : : {
853 : 267367036 : return x->call_stmt == y;
854 : : }
855 : :
856 : : /* Add call graph edge E to call site hash of its caller. */
857 : :
858 : : static inline void
859 : 3882 : cgraph_update_edge_in_call_site_hash (cgraph_edge *e)
860 : : {
861 : 3882 : gimple *call = e->call_stmt;
862 : 3882 : *e->caller->call_site_hash->find_slot_with_hash
863 : 3882 : (call, cgraph_edge_hasher::hash (call), INSERT) = e;
864 : 3882 : }
865 : :
866 : : /* Add call graph edge E to call site hash of its caller. */
867 : :
868 : : static inline void
869 : 8146028 : cgraph_add_edge_to_call_site_hash (cgraph_edge *e)
870 : : {
871 : : /* There are two speculative edges for every statement (one direct,
872 : : one indirect); always hash the direct one. */
873 : 8146028 : if (e->speculative && e->indirect_unknown_callee)
874 : : return;
875 : : /* We always want to hash the carrying edge of a callback, not the edges
876 : : pointing to the callbacks themselves, as their call statement doesn't
877 : : exist. */
878 : 8146017 : if (e->callback)
879 : : return;
880 : 8146005 : cgraph_edge **slot = e->caller->call_site_hash->find_slot_with_hash
881 : 8146005 : (e->call_stmt, cgraph_edge_hasher::hash (e->call_stmt), INSERT);
882 : 8146005 : if (*slot)
883 : : {
884 : 3963 : cgraph_edge *edge = (cgraph_edge *) *slot;
885 : 3963 : gcc_assert (edge->speculative || edge->has_callback);
886 : 3963 : if (edge->has_callback)
887 : : /* If the slot is already occupied, then the hashed edge is the
888 : : callback-carrying edge, which is desired behavior. In some cases,
889 : : the callback flag of E is not set yet and so the early exit above is
890 : : not taken. */
891 : : return;
892 : 3871 : if (e->callee && (!e->prev_callee
893 : 4 : || !e->prev_callee->speculative
894 : 4 : || e->prev_callee->call_stmt != e->call_stmt))
895 : 507 : *slot = e;
896 : 3871 : return;
897 : : }
898 : 8142042 : gcc_assert (!*slot || e->speculative);
899 : 8142042 : *slot = e;
900 : : }
901 : :
902 : : /* Return the callgraph edge representing the GIMPLE_CALL statement
903 : : CALL_STMT. */
904 : :
905 : : cgraph_edge *
906 : 208322721 : cgraph_node::get_edge (gimple *call_stmt)
907 : : {
908 : 208322721 : cgraph_edge *e, *e2;
909 : 208322721 : int n = 0;
910 : :
911 : 208322721 : if (call_site_hash)
912 : 33955883 : return call_site_hash->find_with_hash
913 : 33955883 : (call_stmt, cgraph_edge_hasher::hash (call_stmt));
914 : :
915 : : /* This loop may turn out to be performance problem. In such case adding
916 : : hashtables into call nodes with very many edges is probably best
917 : : solution. It is not good idea to add pointer into CALL_EXPR itself
918 : : because we want to make possible having multiple cgraph nodes representing
919 : : different clones of the same body before the body is actually cloned. */
920 : 1704570220 : for (e = callees; e; e = e->next_callee)
921 : : {
922 : 1657140609 : if (e->call_stmt == call_stmt)
923 : : break;
924 : 1530203382 : n++;
925 : : }
926 : :
927 : 174366838 : if (!e)
928 : 61255669 : for (e = indirect_calls; e; e = e->next_callee)
929 : : {
930 : 16761690 : if (e->call_stmt == call_stmt)
931 : : break;
932 : 13826058 : n++;
933 : : }
934 : :
935 : : /* We want to work with the callback-carrying edge whenever possible. When it
936 : : comes to callback edges, a call statement might have multiple callback
937 : : edges attached to it. These can be easily obtained from the carrying edge
938 : : instead. */
939 : 174366838 : if (e && e->callback)
940 : 55380 : e = e->get_callback_carrying_edge ();
941 : :
942 : 174366838 : if (n > 100)
943 : : {
944 : 29916 : call_site_hash = hash_table<cgraph_edge_hasher>::create_ggc (120);
945 : 3071910 : for (e2 = callees; e2; e2 = e2->next_callee)
946 : 3041994 : cgraph_add_edge_to_call_site_hash (e2);
947 : 113719 : for (e2 = indirect_calls; e2; e2 = e2->next_callee)
948 : 83803 : cgraph_add_edge_to_call_site_hash (e2);
949 : : }
950 : :
951 : : return e;
952 : : }
953 : :
954 : : /* Change field call_stmt of edge E to NEW_STMT. If UPDATE_DERIVED_EDGES and E
955 : : is any component of speculative edge, then update all components.
956 : : speculations can be resolved in the process and edge can be removed and
957 : : deallocated. if update_derived_edges and e is a part of a callback pair,
958 : : update all associated edges and return their carrying edge. return the edge
959 : : that now represents the call. */
960 : :
961 : : cgraph_edge *
962 : 2670861 : cgraph_edge::set_call_stmt (cgraph_edge *e, gcall *new_stmt,
963 : : bool update_derived_edges)
964 : : {
965 : 2670861 : tree decl;
966 : :
967 : 2670861 : cgraph_node *new_direct_callee = NULL;
968 : 2635234 : if ((e->indirect_unknown_callee || e->speculative)
969 : 2722019 : && (decl = gimple_call_fndecl (new_stmt)))
970 : : {
971 : : /* Constant propagation and especially inlining can turn an indirect call
972 : : into a direct one. */
973 : 0 : new_direct_callee = cgraph_node::get (decl);
974 : 0 : gcc_checking_assert (new_direct_callee);
975 : : }
976 : :
977 : : /* Speculative edges has three component, update all of them
978 : : when asked to. */
979 : 2670861 : if (update_derived_edges && e->speculative
980 : : /* If we are about to resolve the speculation by calling make_direct
981 : : below, do not bother going over all the speculative edges now. */
982 : 6862 : && !new_direct_callee)
983 : : {
984 : 6862 : cgraph_edge *direct, *indirect, *next;
985 : 6862 : ipa_ref *ref;
986 : 6862 : bool e_indirect = e->indirect_unknown_callee;
987 : 6862 : int n = 0;
988 : :
989 : 6862 : direct = e->first_speculative_call_target ();
990 : 6862 : indirect = e->speculative_call_indirect_edge ();
991 : :
992 : 6862 : gcall *old_stmt = direct->call_stmt;
993 : 15531 : for (cgraph_edge *d = direct; d; d = next)
994 : : {
995 : 8669 : next = d->next_speculative_call_target ();
996 : 8669 : cgraph_edge *d2 = set_call_stmt (d, new_stmt, false);
997 : 8669 : gcc_assert (d2 == d);
998 : 8669 : n++;
999 : : }
1000 : 6862 : gcc_checking_assert (indirect->num_speculative_call_targets_p () == n);
1001 : 19666 : for (unsigned int i = 0; e->caller->iterate_reference (i, ref); i++)
1002 : 12804 : if (ref->speculative && ref->stmt == old_stmt)
1003 : : {
1004 : 8669 : ref->stmt = new_stmt;
1005 : 8669 : n--;
1006 : : }
1007 : :
1008 : 6862 : indirect = set_call_stmt (indirect, new_stmt, false);
1009 : 6862 : return e_indirect ? indirect : direct;
1010 : : }
1011 : :
1012 : 2663999 : if (new_direct_callee)
1013 : 0 : e = make_direct (e, new_direct_callee);
1014 : :
1015 : : /* When updating a callback or a callback-carrying edge, update every edge
1016 : : involved. */
1017 : 2663999 : if (update_derived_edges && (e->callback || e->has_callback))
1018 : : {
1019 : 753 : cgraph_edge *current, *next, *carrying;
1020 : 753 : carrying = e->has_callback ? e : e->get_callback_carrying_edge ();
1021 : :
1022 : 753 : current = e->first_callback_edge ();
1023 : 753 : if (current)
1024 : : {
1025 : 106 : for (cgraph_edge *d = current; d; d = next)
1026 : : {
1027 : 53 : next = d->next_callback_edge ();
1028 : 53 : cgraph_edge *d2 = set_call_stmt (d, new_stmt, false);
1029 : 53 : gcc_assert (d2 == d);
1030 : : }
1031 : : }
1032 : 753 : carrying = set_call_stmt (carrying, new_stmt, false);
1033 : 753 : return carrying;
1034 : : }
1035 : :
1036 : : /* Only direct speculative edges go to call_site_hash. */
1037 : 2663246 : if (e->caller->call_site_hash
1038 : 465232 : && (!e->speculative || !e->indirect_unknown_callee)
1039 : : /* It is possible that edge was previously speculative. In this case
1040 : : we have different value in call stmt hash which needs preserving. */
1041 : 3128478 : && e->caller->get_edge (e->call_stmt) == e)
1042 : 461348 : e->caller->call_site_hash->remove_elt_with_hash
1043 : 461348 : (e->call_stmt, cgraph_edge_hasher::hash (e->call_stmt));
1044 : :
1045 : 2663246 : e->call_stmt = new_stmt;
1046 : :
1047 : 2663246 : function *fun = DECL_STRUCT_FUNCTION (e->caller->decl);
1048 : 2663246 : e->can_throw_external = stmt_can_throw_external (fun, new_stmt);
1049 : : /* Update call stite hash. For speculative calls we only record the first
1050 : : direct edge. */
1051 : 2663246 : if (e->caller->call_site_hash
1052 : 465232 : && (!e->speculative
1053 : 0 : || (e->callee
1054 : 0 : && (!e->prev_callee || !e->prev_callee->speculative
1055 : 0 : || e->prev_callee->call_stmt != e->call_stmt))
1056 : 0 : || (e->speculative && !e->callee)))
1057 : 465232 : cgraph_add_edge_to_call_site_hash (e);
1058 : : return e;
1059 : : }
1060 : :
1061 : : /* Allocate a cgraph_edge structure and fill it with data according to the
1062 : : parameters of which only CALLEE can be NULL (when creating an indirect call
1063 : : edge). CLONING_P should be set if properties that are copied from an
1064 : : original edge should not be calculated. */
1065 : :
1066 : : cgraph_edge *
1067 : 45033648 : symbol_table::create_edge (cgraph_node *caller, cgraph_node *callee,
1068 : : gcall *call_stmt, profile_count count,
1069 : : bool indir_unknown_callee, bool cloning_p)
1070 : : {
1071 : 45033648 : cgraph_edge *edge;
1072 : :
1073 : : /* LTO does not actually have access to the call_stmt since these
1074 : : have not been loaded yet. */
1075 : 45033648 : if (call_stmt)
1076 : : {
1077 : : /* This is a rather expensive check possibly triggering
1078 : : construction of call stmt hashtable. */
1079 : 44178086 : cgraph_edge *e;
1080 : 44178086 : gcc_checking_assert (!(e = caller->get_edge (call_stmt))
1081 : : || e->speculative || e->has_callback || e->callback);
1082 : :
1083 : 44178086 : gcc_assert (is_gimple_call (call_stmt));
1084 : : }
1085 : :
1086 : 45033648 : edge = ggc_alloc<cgraph_edge> ();
1087 : 45033648 : edge->m_summary_id = -1;
1088 : 45033648 : edges_count++;
1089 : :
1090 : 45033648 : ++edges_max_uid;
1091 : 45033648 : gcc_assert (edges_max_uid != 0);
1092 : 45033648 : edge->m_uid = edges_max_uid;
1093 : 45033648 : edge->aux = NULL;
1094 : 45033648 : edge->caller = caller;
1095 : 45033648 : edge->callee = callee;
1096 : 45033648 : edge->prev_caller = NULL;
1097 : 45033648 : edge->next_caller = NULL;
1098 : 45033648 : edge->prev_callee = NULL;
1099 : 45033648 : edge->next_callee = NULL;
1100 : 45033648 : edge->lto_stmt_uid = 0;
1101 : 45033648 : edge->speculative_id = 0;
1102 : :
1103 : 45033648 : edge->count = count;
1104 : 45033648 : edge->call_stmt = call_stmt;
1105 : 45033648 : edge->indirect_info = NULL;
1106 : 45033648 : edge->indirect_inlining_edge = 0;
1107 : 45033648 : edge->speculative = false;
1108 : 45033648 : edge->has_callback = false;
1109 : 45033648 : edge->callback = false;
1110 : 45033648 : edge->callback_id = 0;
1111 : 45033648 : edge->indirect_unknown_callee = indir_unknown_callee;
1112 : 45033648 : if (call_stmt && caller->call_site_hash)
1113 : 4554999 : cgraph_add_edge_to_call_site_hash (edge);
1114 : :
1115 : 45033648 : if (cloning_p)
1116 : : return edge;
1117 : :
1118 : 38136524 : edge->can_throw_external
1119 : 38136524 : = call_stmt ? stmt_can_throw_external (DECL_STRUCT_FUNCTION (caller->decl),
1120 : : call_stmt) : false;
1121 : 38136524 : edge->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
1122 : 38136524 : edge->call_stmt_cannot_inline_p = false;
1123 : :
1124 : 38136524 : if (opt_for_fn (edge->caller->decl, flag_devirtualize)
1125 : 38136524 : && call_stmt && DECL_STRUCT_FUNCTION (caller->decl))
1126 : 30053886 : edge->in_polymorphic_cdtor
1127 : 30053886 : = decl_maybe_in_construction_p (NULL, NULL, call_stmt,
1128 : : caller->decl);
1129 : : else
1130 : 8082638 : edge->in_polymorphic_cdtor = caller->thunk;
1131 : :
1132 : 37463421 : if (callee && symtab->state != LTO_STREAMING
1133 : 75004397 : && edge->callee->comdat_local_p ())
1134 : 7610 : edge->caller->calls_comdat_local = true;
1135 : :
1136 : : return edge;
1137 : : }
1138 : :
1139 : : /* Create edge from a given function to CALLEE in the cgraph. CLONING_P should
1140 : : be set if properties that are copied from an original edge should not be
1141 : : calculated. */
1142 : :
1143 : : cgraph_edge *
1144 : 44173162 : cgraph_node::create_edge (cgraph_node *callee,
1145 : : gcall *call_stmt, profile_count count, bool cloning_p)
1146 : : {
1147 : 44173162 : cgraph_edge *edge = symtab->create_edge (this, callee, call_stmt, count,
1148 : : false, cloning_p);
1149 : :
1150 : 44173162 : if (!cloning_p)
1151 : 37463421 : initialize_inline_failed (edge);
1152 : :
1153 : 44173162 : edge->next_caller = callee->callers;
1154 : 44173162 : if (callee->callers)
1155 : 34333980 : callee->callers->prev_caller = edge;
1156 : 44173162 : edge->next_callee = callees;
1157 : 44173162 : if (callees)
1158 : 34174716 : callees->prev_callee = edge;
1159 : 44173162 : callees = edge;
1160 : 44173162 : callee->callers = edge;
1161 : :
1162 : 44173162 : return edge;
1163 : : }
1164 : :
1165 : : /* Allocate cgraph_indirect_call_info and set its fields to default values. */
1166 : :
1167 : : cgraph_indirect_call_info *
1168 : 860486 : cgraph_allocate_init_indirect_info (void)
1169 : : {
1170 : 860486 : cgraph_indirect_call_info *ii;
1171 : :
1172 : 860486 : ii = ggc_cleared_alloc<cgraph_indirect_call_info> ();
1173 : 860486 : ii->param_index = -1;
1174 : 860486 : return ii;
1175 : : }
1176 : :
1177 : : /* Create an indirect edge with a yet-undetermined callee where the call
1178 : : statement destination is a formal parameter of the caller with index
1179 : : PARAM_INDEX. CLONING_P should be set if properties that are copied from an
1180 : : original edge should not be calculated and indirect_info structure should
1181 : : not be calculated. */
1182 : :
1183 : : cgraph_edge *
1184 : 860486 : cgraph_node::create_indirect_edge (gcall *call_stmt, int ecf_flags,
1185 : : profile_count count,
1186 : : bool cloning_p)
1187 : : {
1188 : 860486 : cgraph_edge *edge = symtab->create_edge (this, NULL, call_stmt, count, true,
1189 : : cloning_p);
1190 : 860486 : tree target;
1191 : :
1192 : 860486 : if (!cloning_p)
1193 : 673103 : initialize_inline_failed (edge);
1194 : :
1195 : 860486 : edge->indirect_info = cgraph_allocate_init_indirect_info ();
1196 : 860486 : edge->indirect_info->ecf_flags = ecf_flags;
1197 : 860486 : edge->indirect_info->vptr_changed = true;
1198 : :
1199 : : /* Record polymorphic call info. */
1200 : 860486 : if (!cloning_p
1201 : 860486 : && call_stmt
1202 : 670746 : && (target = gimple_call_fn (call_stmt))
1203 : 1531232 : && virtual_method_call_p (target))
1204 : : {
1205 : 101023 : ipa_polymorphic_call_context context (decl, target, call_stmt);
1206 : :
1207 : : /* Only record types can have virtual calls. */
1208 : 101023 : edge->indirect_info->polymorphic = true;
1209 : 101023 : edge->indirect_info->param_index = -1;
1210 : 101023 : edge->indirect_info->otr_token
1211 : 101023 : = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target));
1212 : 101023 : edge->indirect_info->otr_type = obj_type_ref_class (target);
1213 : 101023 : gcc_assert (TREE_CODE (edge->indirect_info->otr_type) == RECORD_TYPE);
1214 : 101023 : edge->indirect_info->context = context;
1215 : : }
1216 : :
1217 : 860486 : edge->next_callee = indirect_calls;
1218 : 860486 : if (indirect_calls)
1219 : 411205 : indirect_calls->prev_callee = edge;
1220 : 860486 : indirect_calls = edge;
1221 : :
1222 : 860486 : return edge;
1223 : : }
1224 : :
1225 : : /* Remove the edge from the list of the callees of the caller. */
1226 : :
1227 : : void
1228 : 4659548 : cgraph_edge::remove_caller (void)
1229 : : {
1230 : 4659548 : if (prev_callee)
1231 : 3774073 : prev_callee->next_callee = next_callee;
1232 : 4659548 : if (next_callee)
1233 : 3173254 : next_callee->prev_callee = prev_callee;
1234 : 4659548 : if (!prev_callee)
1235 : : {
1236 : 885475 : if (indirect_unknown_callee)
1237 : 1495 : caller->indirect_calls = next_callee;
1238 : : else
1239 : 883980 : caller->callees = next_callee;
1240 : : }
1241 : 4659548 : if (caller->call_site_hash
1242 : 4659548 : && this == caller->get_edge (call_stmt))
1243 : 581801 : caller->call_site_hash->remove_elt_with_hash
1244 : 581801 : (call_stmt, cgraph_edge_hasher::hash (call_stmt));
1245 : 4659548 : }
1246 : :
1247 : : /* Put the edge onto the free list. */
1248 : :
1249 : : void
1250 : 44594872 : symbol_table::free_edge (cgraph_edge *e)
1251 : : {
1252 : 44594872 : edges_count--;
1253 : 44594872 : if (e->m_summary_id != -1)
1254 : 20839636 : edge_released_summary_ids.safe_push (e->m_summary_id);
1255 : :
1256 : 44594872 : if (e->indirect_info)
1257 : 853797 : ggc_free (e->indirect_info);
1258 : 44594872 : ggc_free (e);
1259 : 44594872 : }
1260 : :
1261 : : /* Remove the edge in the cgraph. */
1262 : :
1263 : : void
1264 : 117114 : cgraph_edge::remove (cgraph_edge *edge)
1265 : : {
1266 : : /* Call all edge removal hooks. */
1267 : 117114 : symtab->call_edge_removal_hooks (edge);
1268 : :
1269 : 117114 : if (!edge->indirect_unknown_callee)
1270 : : /* Remove from callers list of the callee. */
1271 : 114227 : edge->remove_callee ();
1272 : :
1273 : : /* Remove from callees list of the callers. */
1274 : 117114 : edge->remove_caller ();
1275 : :
1276 : : /* Put the edge onto the free list. */
1277 : 117114 : symtab->free_edge (edge);
1278 : 117114 : }
1279 : :
1280 : : /* Turn edge into speculative call calling N2. Update
1281 : : the profile so the direct call is taken COUNT times
1282 : : with FREQUENCY.
1283 : :
1284 : : At clone materialization time, the indirect call E will
1285 : : be expanded as:
1286 : :
1287 : : if (call_dest == N2)
1288 : : n2 ();
1289 : : else
1290 : : call call_dest
1291 : :
1292 : : At this time the function just creates the direct call,
1293 : : the reference representing the if conditional and attaches
1294 : : them all to the original indirect call statement.
1295 : :
1296 : : speculative_id is used to link direct calls with their corresponding
1297 : : IPA_REF_ADDR references when representing speculative calls.
1298 : :
1299 : : Return direct edge created. */
1300 : :
1301 : : cgraph_edge *
1302 : 19696 : cgraph_edge::make_speculative (cgraph_node *n2, profile_count direct_count,
1303 : : unsigned int speculative_id)
1304 : : {
1305 : 19696 : cgraph_node *n = caller;
1306 : 19696 : ipa_ref *ref = NULL;
1307 : 19696 : cgraph_edge *e2;
1308 : :
1309 : 19696 : if (dump_file)
1310 : 26 : fprintf (dump_file, "Indirect call -> speculative call %s => %s\n",
1311 : : n->dump_name (), n2->dump_name ());
1312 : 19696 : speculative = true;
1313 : 19696 : e2 = n->create_edge (n2, call_stmt, direct_count);
1314 : 19696 : initialize_inline_failed (e2);
1315 : 19696 : e2->speculative = true;
1316 : 19696 : if (TREE_NOTHROW (n2->decl))
1317 : 9483 : e2->can_throw_external = false;
1318 : : else
1319 : 10213 : e2->can_throw_external = can_throw_external;
1320 : 19696 : e2->lto_stmt_uid = lto_stmt_uid;
1321 : 19696 : e2->speculative_id = speculative_id;
1322 : 19696 : e2->in_polymorphic_cdtor = in_polymorphic_cdtor;
1323 : 19696 : indirect_info->num_speculative_call_targets++;
1324 : 19696 : count -= e2->count;
1325 : 19696 : symtab->call_edge_duplication_hooks (this, e2);
1326 : 19696 : ref = n->create_reference (n2, IPA_REF_ADDR, call_stmt);
1327 : 19696 : ref->lto_stmt_uid = lto_stmt_uid;
1328 : 19696 : ref->speculative_id = speculative_id;
1329 : 19696 : ref->speculative = speculative;
1330 : 19696 : n2->mark_address_taken ();
1331 : 19696 : return e2;
1332 : : }
1333 : :
1334 : : /* Create a callback edge calling N2. Callback edges
1335 : : never get turned into actual calls, they are just used
1336 : : as clues and allow for optimizing functions which do not
1337 : : have any callsites during compile time, e.g. functions
1338 : : passed to standard library functions.
1339 : :
1340 : : The edge will be attached to the same call statement as
1341 : : the callback-carrying edge, which is the instance this method
1342 : : is called on.
1343 : :
1344 : : callback_id is used to pair the returned edge with the attribute that
1345 : : originated it.
1346 : :
1347 : : Return the resulting callback edge. */
1348 : :
1349 : : cgraph_edge *
1350 : 15054 : cgraph_edge::make_callback (cgraph_node *n2, unsigned int callback_id)
1351 : : {
1352 : 15054 : cgraph_node *n = caller;
1353 : 15054 : cgraph_edge *e2;
1354 : :
1355 : 15054 : has_callback = true;
1356 : 15054 : e2 = n->create_edge (n2, call_stmt, count);
1357 : 15054 : if (dump_file)
1358 : 3 : fprintf (
1359 : : dump_file,
1360 : : "Created callback edge %s -> %s belonging to carrying edge %s -> %s\n",
1361 : 3 : e2->caller->dump_name (), e2->callee->dump_name (), caller->dump_name (),
1362 : 3 : callee->dump_name ());
1363 : 15054 : e2->inline_failed = CIF_CALLBACK_EDGE;
1364 : 15054 : e2->callback = true;
1365 : 15054 : e2->callback_id = callback_id;
1366 : 15054 : if (TREE_NOTHROW (n2->decl))
1367 : 14803 : e2->can_throw_external = false;
1368 : : else
1369 : 251 : e2->can_throw_external = can_throw_external;
1370 : 15054 : e2->lto_stmt_uid = lto_stmt_uid;
1371 : 15054 : n2->mark_address_taken ();
1372 : 15054 : return e2;
1373 : : }
1374 : :
1375 : : /* Returns the callback_carrying edge of a callback edge on which
1376 : : it is called on or NULL when no such edge can be found.
1377 : :
1378 : : An edge is taken to be the callback-carrying if it has it's has_callback
1379 : : flag set and the edges share their call statements. */
1380 : :
1381 : : cgraph_edge *
1382 : 82869 : cgraph_edge::get_callback_carrying_edge ()
1383 : : {
1384 : 82869 : gcc_checking_assert (callback);
1385 : 82869 : cgraph_edge *e;
1386 : 653371 : for (e = caller->callees; e; e = e->next_callee)
1387 : : {
1388 : 653181 : if (e->has_callback && e->call_stmt == call_stmt
1389 : 82679 : && e->lto_stmt_uid == lto_stmt_uid)
1390 : : break;
1391 : : }
1392 : 82869 : return e;
1393 : : }
1394 : :
1395 : : /* Returns the first callback edge in the list of callees of the caller node.
1396 : : Note that the edges might be in arbitrary order. Must be called on a
1397 : : callback or callback-carrying edge. */
1398 : :
1399 : : cgraph_edge *
1400 : 47431 : cgraph_edge::first_callback_edge ()
1401 : : {
1402 : 47431 : gcc_checking_assert (has_callback || callback);
1403 : 47431 : cgraph_edge *e = NULL;
1404 : 216859 : for (e = caller->callees; e; e = e->next_callee)
1405 : : {
1406 : 200645 : if (e->callback && e->call_stmt == call_stmt
1407 : 31217 : && e->lto_stmt_uid == lto_stmt_uid)
1408 : : break;
1409 : : }
1410 : 47431 : return e;
1411 : : }
1412 : :
1413 : : /* Given a callback edge, returns the next callback edge belonging to the same
1414 : : carrying edge. Must be called on a callback edge, not the callback-carrying
1415 : : edge. */
1416 : :
1417 : : cgraph_edge *
1418 : 31217 : cgraph_edge::next_callback_edge ()
1419 : : {
1420 : 31217 : gcc_checking_assert (callback);
1421 : 31217 : cgraph_edge *e = NULL;
1422 : 320957 : for (e = next_callee; e; e = e->next_callee)
1423 : : {
1424 : 289740 : if (e->callback && e->call_stmt == call_stmt
1425 : 0 : && e->lto_stmt_uid == lto_stmt_uid)
1426 : : break;
1427 : : }
1428 : 31217 : return e;
1429 : : }
1430 : :
1431 : : /* When called on a callback-carrying edge, removes all of its attached callback
1432 : : edges and sets has_callback to FALSE. */
1433 : :
1434 : : void
1435 : 2 : cgraph_edge::purge_callback_edges ()
1436 : : {
1437 : 2 : gcc_checking_assert (has_callback);
1438 : 2 : cgraph_edge *e, *next;
1439 : 2 : for (e = first_callback_edge (); e; e = next)
1440 : : {
1441 : 0 : next = e->next_callback_edge ();
1442 : 0 : cgraph_edge::remove (e);
1443 : : }
1444 : 2 : has_callback = false;
1445 : 2 : }
1446 : :
1447 : : /* Speculative call consists of an indirect edge and one or more
1448 : : direct edge+ref pairs.
1449 : :
1450 : : Given an edge which is part of speculative call, return the first
1451 : : direct call edge in the speculative call sequence. */
1452 : :
1453 : : cgraph_edge *
1454 : 47160 : cgraph_edge::first_speculative_call_target ()
1455 : : {
1456 : 47160 : cgraph_edge *e = this;
1457 : :
1458 : 47160 : gcc_checking_assert (e->speculative);
1459 : 47160 : if (e->callee)
1460 : : {
1461 : 14532 : while (e->prev_callee && e->prev_callee->speculative
1462 : 1580 : && e->prev_callee->call_stmt == e->call_stmt
1463 : 38064 : && e->prev_callee->lto_stmt_uid == e->lto_stmt_uid)
1464 : : e = e->prev_callee;
1465 : : return e;
1466 : : }
1467 : : /* Call stmt site hash always points to the first target of the
1468 : : speculative call sequence. */
1469 : 9096 : if (e->call_stmt)
1470 : 9084 : return e->caller->get_edge (e->call_stmt);
1471 : 17 : for (cgraph_edge *e2 = e->caller->callees; true; e2 = e2->next_callee)
1472 : 17 : if (e2->speculative
1473 : 12 : && e->call_stmt == e2->call_stmt
1474 : 12 : && e->lto_stmt_uid == e2->lto_stmt_uid)
1475 : : return e2;
1476 : : }
1477 : :
1478 : : /* We always maintain first direct edge in the call site hash, if one
1479 : : exists. E is going to be removed. See if it is first one and update
1480 : : hash accordingly. INDIRECT is the indirect edge of speculative call.
1481 : : We assume that INDIRECT->num_speculative_call_targets_p () is already
1482 : : updated for removal of E. */
1483 : : static void
1484 : 40042 : update_call_stmt_hash_for_removing_direct_edge (cgraph_edge *e,
1485 : : cgraph_edge *indirect)
1486 : : {
1487 : 40042 : if (e->caller->call_site_hash)
1488 : : {
1489 : 3882 : if (e->caller->get_edge (e->call_stmt) != e)
1490 : : ;
1491 : 3882 : else if (!indirect->num_speculative_call_targets_p ())
1492 : 3379 : cgraph_update_edge_in_call_site_hash (indirect);
1493 : : else
1494 : : {
1495 : 503 : gcc_checking_assert (e->next_callee && e->next_callee->speculative
1496 : : && e->next_callee->call_stmt == e->call_stmt);
1497 : 503 : cgraph_update_edge_in_call_site_hash (e->next_callee);
1498 : : }
1499 : : }
1500 : 40042 : }
1501 : :
1502 : : /* Speculative call EDGE turned out to be direct call to CALLEE_DECL. Remove
1503 : : the speculative call sequence and return edge representing the call, the
1504 : : original EDGE can be removed and deallocated. Return the edge that now
1505 : : represents the call.
1506 : :
1507 : : For "speculative" indirect call that contains multiple "speculative"
1508 : : targets (i.e. edge->indirect_info->num_speculative_call_targets > 1),
1509 : : decrease the count and only remove current direct edge.
1510 : :
1511 : : If no speculative direct call left to the speculative indirect call, remove
1512 : : the speculative of both the indirect call and corresponding direct edge.
1513 : :
1514 : : It is up to caller to iteratively resolve each "speculative" direct call and
1515 : : redirect the call as appropriate. */
1516 : :
1517 : : cgraph_edge *
1518 : 8840 : cgraph_edge::resolve_speculation (cgraph_edge *edge, tree callee_decl)
1519 : : {
1520 : 8840 : cgraph_edge *e2;
1521 : 8840 : ipa_ref *ref;
1522 : :
1523 : 8840 : gcc_assert (edge->speculative && (!callee_decl || edge->callee));
1524 : 8840 : if (!edge->callee)
1525 : 0 : e2 = edge->first_speculative_call_target ();
1526 : : else
1527 : : e2 = edge;
1528 : 8840 : ref = e2->speculative_call_target_ref ();
1529 : 8840 : edge = edge->speculative_call_indirect_edge ();
1530 : 8840 : if (!callee_decl
1531 : 10248 : || !ref->referred->semantically_equivalent_p
1532 : 1408 : (symtab_node::get (callee_decl)))
1533 : : {
1534 : 7741 : if (dump_file)
1535 : : {
1536 : 78 : if (callee_decl)
1537 : : {
1538 : 0 : fprintf (dump_file, "Speculative indirect call %s => %s has "
1539 : : "turned out to have contradicting known target ",
1540 : 0 : edge->caller->dump_name (),
1541 : 0 : e2->callee->dump_name ());
1542 : 0 : print_generic_expr (dump_file, callee_decl);
1543 : 0 : fprintf (dump_file, "\n");
1544 : : }
1545 : : else
1546 : : {
1547 : 78 : fprintf (dump_file, "Removing speculative call %s => %s\n",
1548 : 78 : edge->caller->dump_name (),
1549 : 78 : e2->callee->dump_name ());
1550 : : }
1551 : : }
1552 : : }
1553 : : else
1554 : : {
1555 : 1099 : cgraph_edge *tmp = edge;
1556 : 1099 : if (dump_file)
1557 : 87 : fprintf (dump_file, "Speculative call turned into direct call.\n");
1558 : : edge = e2;
1559 : : e2 = tmp;
1560 : : /* FIXME: If EDGE is inlined, we should scale up the frequencies
1561 : : and counts in the functions inlined through it. */
1562 : : }
1563 : 8840 : edge->count += e2->count;
1564 : 8840 : if (edge->num_speculative_call_targets_p ())
1565 : : {
1566 : : /* The indirect edge has multiple speculative targets, don't remove
1567 : : speculative until all related direct edges are resolved. */
1568 : 7741 : edge->indirect_info->num_speculative_call_targets--;
1569 : 7741 : if (!edge->indirect_info->num_speculative_call_targets)
1570 : 918 : edge->speculative = false;
1571 : : }
1572 : : else
1573 : 1099 : edge->speculative = false;
1574 : 8840 : e2->speculative = false;
1575 : 8840 : update_call_stmt_hash_for_removing_direct_edge (e2, edge);
1576 : 8840 : ref->remove_reference ();
1577 : 8840 : if (e2->indirect_unknown_callee || e2->inline_failed)
1578 : 7981 : remove (e2);
1579 : : else
1580 : 859 : e2->callee->remove_symbol_and_inline_clones ();
1581 : 8840 : return edge;
1582 : : }
1583 : :
1584 : : /* Return edge corresponding to speculative call to a given target.
1585 : : NULL if speculative call does not have one. */
1586 : :
1587 : : cgraph_edge *
1588 : 0 : cgraph_edge::speculative_call_for_target (cgraph_node *target)
1589 : : {
1590 : 0 : for (cgraph_edge *direct = first_speculative_call_target ();
1591 : 0 : direct;
1592 : 0 : direct = direct->next_speculative_call_target ())
1593 : 0 : if (direct->speculative_call_target_ref ()
1594 : 0 : ->referred->semantically_equivalent_p (target))
1595 : : return direct;
1596 : : return NULL;
1597 : : }
1598 : :
1599 : : /* Make an indirect or speculative EDGE with an unknown callee an ordinary edge
1600 : : leading to CALLEE. Speculations can be resolved in the process and EDGE can
1601 : : be removed and deallocated. Return the edge that now represents the
1602 : : call. */
1603 : :
1604 : : cgraph_edge *
1605 : 4956 : cgraph_edge::make_direct (cgraph_edge *edge, cgraph_node *callee)
1606 : : {
1607 : 4956 : gcc_assert (edge->indirect_unknown_callee || edge->speculative);
1608 : :
1609 : : /* If we are redirecting speculative call, make it non-speculative. */
1610 : 4956 : if (edge->speculative)
1611 : : {
1612 : 1242 : cgraph_edge *found = NULL;
1613 : 1242 : cgraph_edge *direct, *next;
1614 : :
1615 : 1242 : edge = edge->speculative_call_indirect_edge ();
1616 : :
1617 : : /* Look all speculative targets and remove all but one corresponding
1618 : : to callee (if it exists). */
1619 : 1242 : for (direct = edge->first_speculative_call_target ();
1620 : 3240 : direct;
1621 : : direct = next)
1622 : : {
1623 : 1998 : next = direct->next_speculative_call_target ();
1624 : :
1625 : : /* Compare ref not direct->callee. Direct edge is possibly
1626 : : inlined or redirected. */
1627 : 1998 : if (!direct->speculative_call_target_ref ()
1628 : 1998 : ->referred->semantically_equivalent_p (callee)
1629 : 1998 : || found)
1630 : 899 : edge = direct->resolve_speculation (direct, NULL);
1631 : : else
1632 : : found = direct;
1633 : : }
1634 : :
1635 : : /* On successful speculation just remove the indirect edge and
1636 : : return the pre existing direct edge.
1637 : : It is important to not remove it and redirect because the direct
1638 : : edge may be inlined or redirected. */
1639 : 1242 : if (found)
1640 : : {
1641 : 1099 : cgraph_edge *e2 = resolve_speculation (found, callee->decl);
1642 : 1099 : gcc_checking_assert (!found->speculative && e2 == found);
1643 : : return found;
1644 : : }
1645 : 143 : gcc_checking_assert (!edge->speculative);
1646 : : }
1647 : :
1648 : 3857 : edge->indirect_unknown_callee = 0;
1649 : 3857 : ggc_free (edge->indirect_info);
1650 : 3857 : edge->indirect_info = NULL;
1651 : :
1652 : : /* Get the edge out of the indirect edge list. */
1653 : 3857 : if (edge->prev_callee)
1654 : 98 : edge->prev_callee->next_callee = edge->next_callee;
1655 : 3857 : if (edge->next_callee)
1656 : 493 : edge->next_callee->prev_callee = edge->prev_callee;
1657 : 3857 : if (!edge->prev_callee)
1658 : 3759 : edge->caller->indirect_calls = edge->next_callee;
1659 : :
1660 : : /* Put it into the normal callee list */
1661 : 3857 : edge->prev_callee = NULL;
1662 : 3857 : edge->next_callee = edge->caller->callees;
1663 : 3857 : if (edge->caller->callees)
1664 : 2453 : edge->caller->callees->prev_callee = edge;
1665 : 3857 : edge->caller->callees = edge;
1666 : :
1667 : : /* Insert to callers list of the new callee. */
1668 : 3857 : edge->set_callee (callee);
1669 : :
1670 : : /* We need to re-determine the inlining status of the edge. */
1671 : 3857 : initialize_inline_failed (edge);
1672 : 3857 : return edge;
1673 : : }
1674 : :
1675 : : /* Redirect callee of the edge to N. The function does not update underlying
1676 : : call expression. */
1677 : :
1678 : : void
1679 : 4378064 : cgraph_edge::redirect_callee (cgraph_node *n)
1680 : : {
1681 : 4378064 : bool loc = callee->comdat_local_p ();
1682 : 4378064 : cgraph_node *old_callee = callee;
1683 : :
1684 : : /* Remove from callers list of the current callee. */
1685 : 4378064 : remove_callee ();
1686 : :
1687 : : /* Insert to callers list of the new callee. */
1688 : 4378064 : set_callee (n);
1689 : :
1690 : 4378064 : if (callback)
1691 : : {
1692 : : /* When redirecting a callback callee, redirect its ref as well. */
1693 : 281 : ipa_ref *old_ref = caller->find_reference (old_callee, call_stmt,
1694 : : lto_stmt_uid, IPA_REF_ADDR);
1695 : 281 : gcc_checking_assert(old_ref);
1696 : 281 : old_ref->remove_reference ();
1697 : 281 : ipa_ref *new_ref = caller->create_reference (n, IPA_REF_ADDR, call_stmt);
1698 : 281 : new_ref->lto_stmt_uid = lto_stmt_uid;
1699 : 281 : if (!old_callee->referred_to_p ())
1700 : 239 : old_callee->address_taken = 0;
1701 : : }
1702 : :
1703 : 4378064 : if (!inline_failed)
1704 : : return;
1705 : 579987 : if (!loc && n->comdat_local_p ())
1706 : : {
1707 : 56 : cgraph_node *to = caller->inlined_to ? caller->inlined_to : caller;
1708 : 56 : to->calls_comdat_local = true;
1709 : : }
1710 : 579931 : else if (loc && !n->comdat_local_p ())
1711 : : {
1712 : 97 : cgraph_node *to = caller->inlined_to ? caller->inlined_to : caller;
1713 : 97 : gcc_checking_assert (to->calls_comdat_local);
1714 : 97 : to->calls_comdat_local = to->check_calls_comdat_local_p ();
1715 : : }
1716 : : }
1717 : :
1718 : : /* If necessary, change the function declaration in the call statement
1719 : : associated with E so that it corresponds to the edge callee. Speculations
1720 : : can be resolved in the process and EDGE can be removed and deallocated.
1721 : :
1722 : : The edge could be one of speculative direct call generated from speculative
1723 : : indirect call. In this circumstance, decrease the speculative targets
1724 : : count (i.e. num_speculative_call_targets) and redirect call stmt to the
1725 : : corresponding i-th target. If no speculative direct call left to the
1726 : : speculative indirect call, remove "speculative" of the indirect call and
1727 : : also redirect stmt to it's final direct target.
1728 : :
1729 : : When called from within tree-inline, KILLED_SSAs has to contain the pointer
1730 : : to killed_new_ssa_names within the copy_body_data structure and SSAs
1731 : : discovered to be useless (if LHS is removed) will be added to it, otherwise
1732 : : it needs to be NULL.
1733 : :
1734 : : It is up to caller to iteratively transform each "speculative"
1735 : : direct call as appropriate. */
1736 : :
1737 : : gimple *
1738 : 9679526 : cgraph_edge::redirect_call_stmt_to_callee (cgraph_edge *e,
1739 : : hash_set <tree> *killed_ssas)
1740 : : {
1741 : 9679526 : tree decl = gimple_call_fndecl (e->call_stmt);
1742 : 9679526 : gcall *new_stmt;
1743 : :
1744 : 9679526 : if (e->speculative)
1745 : : {
1746 : : /* If there already is an direct call (i.e. as a result of inliner's
1747 : : substitution), forget about speculating. */
1748 : 31202 : if (decl)
1749 : 0 : e = make_direct (e->speculative_call_indirect_edge (),
1750 : : cgraph_node::get (decl));
1751 : : else
1752 : : {
1753 : : /* Be sure we redirect all speculative targets before poking
1754 : : about indirect edge. */
1755 : 31202 : gcc_checking_assert (e->callee);
1756 : 31202 : cgraph_edge *indirect = e->speculative_call_indirect_edge ();
1757 : 31202 : gcall *new_stmt;
1758 : 31202 : ipa_ref *ref;
1759 : :
1760 : : /* Expand speculation into GIMPLE code. */
1761 : 31202 : if (dump_file)
1762 : : {
1763 : 150 : fprintf (dump_file,
1764 : : "Expanding speculative call of %s -> %s count: ",
1765 : 75 : e->caller->dump_name (),
1766 : : e->callee->dump_name ());
1767 : 75 : e->count.dump (dump_file);
1768 : 75 : fprintf (dump_file, "\n");
1769 : : }
1770 : 31202 : push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1771 : :
1772 : 31202 : profile_count all = indirect->count;
1773 : 31202 : for (cgraph_edge *e2 = e->first_speculative_call_target ();
1774 : 72362 : e2;
1775 : 41160 : e2 = e2->next_speculative_call_target ())
1776 : 41160 : all = all + e2->count;
1777 : 31202 : profile_probability prob = e->count.probability_in (all);
1778 : 31202 : if (!prob.initialized_p ())
1779 : 135 : prob = profile_probability::even ();
1780 : 31202 : ref = e->speculative_call_target_ref ();
1781 : 62404 : new_stmt = gimple_ic (e->call_stmt,
1782 : : dyn_cast<cgraph_node *> (ref->referred),
1783 : : prob);
1784 : 31202 : e->speculative = false;
1785 : 31202 : if (indirect->num_speculative_call_targets_p ())
1786 : : {
1787 : : /* The indirect edge has multiple speculative targets, don't
1788 : : remove speculative until all related direct edges are
1789 : : redirected. */
1790 : 31202 : indirect->indirect_info->num_speculative_call_targets--;
1791 : 31202 : if (!indirect->indirect_info->num_speculative_call_targets)
1792 : 23464 : indirect->speculative = false;
1793 : : }
1794 : : else
1795 : 0 : indirect->speculative = false;
1796 : : /* Indirect edges are not both in the call site hash.
1797 : : get it updated. */
1798 : 31202 : update_call_stmt_hash_for_removing_direct_edge (e, indirect);
1799 : 31202 : cgraph_edge::set_call_stmt (e, new_stmt, false);
1800 : 31202 : e->count = gimple_bb (e->call_stmt)->count;
1801 : :
1802 : : /* Once we are done with expanding the sequence, update also indirect
1803 : : call probability. Until then the basic block accounts for the
1804 : : sum of indirect edge and all non-expanded speculations. */
1805 : 31202 : if (!indirect->speculative)
1806 : 23464 : indirect->count = gimple_bb (indirect->call_stmt)->count;
1807 : 31202 : ref->speculative = false;
1808 : 31202 : ref->stmt = NULL;
1809 : 31202 : pop_cfun ();
1810 : : /* Continue redirecting E to proper target. */
1811 : : }
1812 : : }
1813 : :
1814 : :
1815 : 9679526 : if (e->indirect_unknown_callee
1816 : 9583970 : || decl == e->callee->decl)
1817 : 8691468 : return e->call_stmt;
1818 : :
1819 : : /* When redirecting a callback edge, all we need to do is replace
1820 : : the original address with the address of the function we are
1821 : : redirecting to. */
1822 : 988058 : if (e->callback)
1823 : : {
1824 : 3422 : cgraph_edge *carrying = e->get_callback_carrying_edge ();
1825 : 3422 : if (!callback_is_special_cased (carrying->callee->decl, e->call_stmt)
1826 : 6076 : && !lookup_attribute (CALLBACK_ATTR_IDENT,
1827 : 2654 : DECL_ATTRIBUTES (carrying->callee->decl)))
1828 : : /* Callback attribute is removed if the dispatching function changes
1829 : : signature, as the indices wouldn't be correct anymore. These edges
1830 : : will get cleaned up later, ignore their redirection for now. */
1831 : 0 : return e->call_stmt;
1832 : 3422 : int fn_idx = callback_fetch_fn_position (e, carrying);
1833 : 3422 : tree previous_arg = gimple_call_arg (e->call_stmt, fn_idx);
1834 : 3422 : location_t loc = EXPR_LOCATION (previous_arg);
1835 : 3422 : tree new_addr = build_fold_addr_expr_loc (loc, e->callee->decl);
1836 : 3422 : gimple_call_set_arg (e->call_stmt, fn_idx, new_addr);
1837 : 3422 : return e->call_stmt;
1838 : : }
1839 : :
1840 : 984636 : if (decl && ipa_saved_clone_sources)
1841 : : {
1842 : 799139 : tree *p = ipa_saved_clone_sources->get (e->callee);
1843 : 799139 : if (p && decl == *p)
1844 : : {
1845 : 33660 : gimple_call_set_fndecl (e->call_stmt, e->callee->decl);
1846 : 33660 : return e->call_stmt;
1847 : : }
1848 : : }
1849 : 950976 : if (flag_checking && decl)
1850 : : {
1851 : 916016 : if (cgraph_node *node = cgraph_node::get (decl))
1852 : : {
1853 : 780493 : clone_info *info = clone_info::get (node);
1854 : 780493 : gcc_assert (!info || !info->param_adjustments);
1855 : : }
1856 : : }
1857 : :
1858 : 950976 : clone_info *callee_info = clone_info::get (e->callee);
1859 : 950976 : if (symtab->dump_file)
1860 : : {
1861 : 0 : fprintf (symtab->dump_file, "updating call of %s -> %s: ",
1862 : 0 : e->caller->dump_name (), e->callee->dump_name ());
1863 : 0 : print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1864 : 0 : if (callee_info && callee_info->param_adjustments)
1865 : 0 : callee_info->param_adjustments->dump (symtab->dump_file);
1866 : : }
1867 : :
1868 : 487073 : if (ipa_param_adjustments *padjs
1869 : 950976 : = callee_info ? callee_info->param_adjustments : NULL)
1870 : : {
1871 : : /* We need to defer cleaning EH info on the new statement to
1872 : : fixup-cfg. We may not have dominator information at this point
1873 : : and thus would end up with unreachable blocks and have no way
1874 : : to communicate that we need to run CFG cleanup then. */
1875 : 458537 : int lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1876 : 458537 : if (lp_nr != 0)
1877 : 136162 : remove_stmt_from_eh_lp (e->call_stmt);
1878 : :
1879 : 458537 : tree old_fntype = gimple_call_fntype (e->call_stmt);
1880 : 458537 : new_stmt = padjs->modify_call (e, false, killed_ssas);
1881 : 458537 : cgraph_node *origin = e->callee;
1882 : 677993 : while (origin->clone_of)
1883 : : origin = origin->clone_of;
1884 : :
1885 : 458537 : if ((origin->former_clone_of
1886 : 353750 : && old_fntype == TREE_TYPE (origin->former_clone_of))
1887 : 460726 : || old_fntype == TREE_TYPE (origin->decl))
1888 : 351563 : gimple_call_set_fntype (new_stmt, TREE_TYPE (e->callee->decl));
1889 : : else
1890 : : {
1891 : 106974 : tree new_fntype = padjs->build_new_function_type (old_fntype, true);
1892 : 106974 : gimple_call_set_fntype (new_stmt, new_fntype);
1893 : : }
1894 : :
1895 : 458537 : if (lp_nr != 0)
1896 : 136162 : add_stmt_to_eh_lp (new_stmt, lp_nr);
1897 : : }
1898 : : else
1899 : : {
1900 : 492439 : if (flag_checking
1901 : 492439 : && !fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE,
1902 : : BUILT_IN_UNREACHABLE_TRAP))
1903 : 334059 : ipa_verify_edge_has_no_modifications (e);
1904 : 492439 : new_stmt = e->call_stmt;
1905 : 492439 : gimple_call_set_fndecl (new_stmt, e->callee->decl);
1906 : 492439 : update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1907 : : }
1908 : :
1909 : : /* If changing the call to __cxa_pure_virtual or similar noreturn function,
1910 : : adjust gimple_call_fntype too. */
1911 : 950976 : if (gimple_call_noreturn_p (new_stmt)
1912 : 161372 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (e->callee->decl)))
1913 : 161300 : && TYPE_ARG_TYPES (TREE_TYPE (e->callee->decl))
1914 : 1112270 : && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (e->callee->decl)))
1915 : 161294 : == void_type_node))
1916 : 160577 : gimple_call_set_fntype (new_stmt, TREE_TYPE (e->callee->decl));
1917 : :
1918 : : /* If the call becomes noreturn, remove the LHS if possible. */
1919 : 950976 : tree lhs = gimple_call_lhs (new_stmt);
1920 : 950976 : if (lhs
1921 : 279344 : && gimple_call_noreturn_p (new_stmt)
1922 : 997170 : && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (new_stmt)))
1923 : 59 : || should_remove_lhs_p (lhs)))
1924 : : {
1925 : 46153 : gimple_call_set_lhs (new_stmt, NULL_TREE);
1926 : : /* We need to fix up the SSA name to avoid checking errors. */
1927 : 46153 : if (TREE_CODE (lhs) == SSA_NAME)
1928 : : {
1929 : 36031 : tree var = create_tmp_reg_fn (DECL_STRUCT_FUNCTION (e->caller->decl),
1930 : 36031 : TREE_TYPE (lhs), NULL);
1931 : 36031 : SET_SSA_NAME_VAR_OR_IDENTIFIER (lhs, var);
1932 : 36031 : SSA_NAME_DEF_STMT (lhs) = gimple_build_nop ();
1933 : 36031 : set_ssa_default_def (DECL_STRUCT_FUNCTION (e->caller->decl),
1934 : : var, lhs);
1935 : : }
1936 : 46153 : update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1937 : : }
1938 : :
1939 : : /* If new callee has no static chain, remove it. */
1940 : 950976 : if (gimple_call_chain (new_stmt) && !DECL_STATIC_CHAIN (e->callee->decl))
1941 : : {
1942 : 56 : gimple_call_set_chain (new_stmt, NULL);
1943 : 56 : update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1944 : : }
1945 : :
1946 : 950976 : maybe_remove_unused_call_args (DECL_STRUCT_FUNCTION (e->caller->decl),
1947 : : new_stmt);
1948 : :
1949 : : /* Update callback edges if setting the carrying edge's statement, or else
1950 : : their pairing would fall apart. */
1951 : 950976 : e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt, e->has_callback);
1952 : :
1953 : 950976 : if (symtab->dump_file)
1954 : : {
1955 : 0 : fprintf (symtab->dump_file, " updated to:");
1956 : 0 : print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1957 : : }
1958 : : return new_stmt;
1959 : : }
1960 : :
1961 : : /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1962 : : OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1963 : : of OLD_STMT if it was previously call statement.
1964 : : If NEW_STMT is NULL, the call has been dropped without any
1965 : : replacement. */
1966 : :
1967 : : static void
1968 : 119017 : cgraph_update_edges_for_call_stmt_node (cgraph_node *node,
1969 : : gimple *old_stmt, tree old_call,
1970 : : gimple *new_stmt)
1971 : : {
1972 : 119017 : tree new_call = (new_stmt && is_gimple_call (new_stmt))
1973 : 124128 : ? gimple_call_fndecl (new_stmt) : 0;
1974 : :
1975 : : /* We are seeing indirect calls, then there is nothing to update. */
1976 : 119017 : if (!new_call && !old_call)
1977 : : return;
1978 : : /* See if we turned indirect call into direct call or folded call to one builtin
1979 : : into different builtin. */
1980 : 117062 : if (old_call != new_call)
1981 : : {
1982 : 116058 : cgraph_edge *e = node->get_edge (old_stmt);
1983 : 116058 : cgraph_edge *ne = NULL;
1984 : 116058 : profile_count count;
1985 : :
1986 : 116058 : if (e)
1987 : : {
1988 : : /* If call was devirtualized during cloning, mark edge
1989 : : as resolved. */
1990 : 94795 : if (e->speculative)
1991 : : {
1992 : 0 : if (new_stmt && is_gimple_call (new_stmt))
1993 : : {
1994 : 0 : tree decl = gimple_call_fndecl (new_stmt);
1995 : 0 : if (decl)
1996 : 0 : e = cgraph_edge::resolve_speculation (e, decl);
1997 : : }
1998 : : else
1999 : 0 : e = cgraph_edge::resolve_speculation (e, NULL);
2000 : : }
2001 : : /* Keep calls marked as dead dead. */
2002 : 94795 : if (new_stmt && is_gimple_call (new_stmt) && e->callee
2003 : 95371 : && fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE,
2004 : : BUILT_IN_UNREACHABLE_TRAP))
2005 : : {
2006 : 5 : cgraph_edge::set_call_stmt (node->get_edge (old_stmt),
2007 : : as_a <gcall *> (new_stmt));
2008 : 32 : return;
2009 : : }
2010 : : /* See if the edge is already there and has the correct callee. It
2011 : : might be so because of indirect inlining has already updated
2012 : : it. We also might've cloned and redirected the edge. */
2013 : 94790 : if (new_call && e->callee)
2014 : : {
2015 : : cgraph_node *callee = e->callee;
2016 : 1143 : while (callee)
2017 : : {
2018 : 594 : if (callee->decl == new_call
2019 : 594 : || callee->former_clone_of == new_call)
2020 : : {
2021 : 22 : cgraph_edge::set_call_stmt (e, as_a <gcall *> (new_stmt));
2022 : 22 : return;
2023 : : }
2024 : 572 : callee = callee->clone_of;
2025 : : }
2026 : : }
2027 : :
2028 : : /* Otherwise remove edge and create new one; we can't simply redirect
2029 : : since function has changed, so inline plan and other information
2030 : : attached to edge is invalid. */
2031 : 94768 : count = e->count;
2032 : 94768 : if (e->indirect_unknown_callee || e->inline_failed)
2033 : 94768 : cgraph_edge::remove (e);
2034 : : else
2035 : 0 : e->callee->remove_symbol_and_inline_clones ();
2036 : : }
2037 : 21263 : else if (new_call)
2038 : : {
2039 : : /* We are seeing new direct call; compute profile info based on BB. */
2040 : 4 : basic_block bb = gimple_bb (new_stmt);
2041 : 4 : count = bb->count;
2042 : : }
2043 : :
2044 : 94772 : if (new_call)
2045 : : {
2046 : 2325 : ne = node->create_edge (cgraph_node::get_create (new_call),
2047 : : as_a <gcall *> (new_stmt), count);
2048 : 2325 : gcc_assert (ne->inline_failed);
2049 : : }
2050 : : }
2051 : : /* We only updated the call stmt; update pointer in cgraph edge.. */
2052 : 1004 : else if (old_stmt != new_stmt)
2053 : 0 : cgraph_edge::set_call_stmt (node->get_edge (old_stmt),
2054 : : as_a <gcall *> (new_stmt));
2055 : : }
2056 : :
2057 : : /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
2058 : : OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
2059 : : of OLD_STMT before it was updated (updating can happen inplace). */
2060 : :
2061 : : void
2062 : 97375 : cgraph_update_edges_for_call_stmt (gimple *old_stmt, tree old_decl,
2063 : : gimple *new_stmt)
2064 : : {
2065 : 97375 : cgraph_node *orig = cgraph_node::get (cfun->decl);
2066 : 97375 : cgraph_node *node;
2067 : :
2068 : 97375 : gcc_checking_assert (orig);
2069 : 97375 : gcc_assert (!orig->thunk);
2070 : 97375 : cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
2071 : 97375 : if (orig->clones)
2072 : 42641 : for (node = orig->clones; node != orig;)
2073 : : {
2074 : : /* Do not attempt to adjust bodies of yet unexpanded thunks. */
2075 : 21644 : if (!node->thunk)
2076 : 21642 : cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl,
2077 : : new_stmt);
2078 : 21644 : if (node->clones)
2079 : : node = node->clones;
2080 : 21632 : else if (node->next_sibling_clone)
2081 : : node = node->next_sibling_clone;
2082 : : else
2083 : : {
2084 : 42006 : while (node != orig && !node->next_sibling_clone)
2085 : 21009 : node = node->clone_of;
2086 : 20997 : if (node != orig)
2087 : 0 : node = node->next_sibling_clone;
2088 : : }
2089 : : }
2090 : 97375 : }
2091 : :
2092 : :
2093 : : /* Remove all callees from the node. */
2094 : :
2095 : : void
2096 : 214030350 : cgraph_node::remove_callees (void)
2097 : : {
2098 : 214030350 : cgraph_edge *e, *f;
2099 : :
2100 : 214030350 : calls_comdat_local = false;
2101 : :
2102 : : /* It is sufficient to remove the edges from the lists of callers of
2103 : : the callees. The callee list of the node can be zapped with one
2104 : : assignment. */
2105 : 253114764 : for (e = callees; e; e = f)
2106 : : {
2107 : 39084414 : f = e->next_callee;
2108 : 39084414 : symtab->call_edge_removal_hooks (e);
2109 : 39084414 : if (!e->indirect_unknown_callee)
2110 : 39084414 : e->remove_callee ();
2111 : 39084414 : symtab->free_edge (e);
2112 : : }
2113 : 214881260 : for (e = indirect_calls; e; e = f)
2114 : : {
2115 : 850910 : f = e->next_callee;
2116 : 850910 : symtab->call_edge_removal_hooks (e);
2117 : 850910 : if (!e->indirect_unknown_callee)
2118 : 0 : e->remove_callee ();
2119 : 850910 : symtab->free_edge (e);
2120 : : }
2121 : 214030350 : indirect_calls = NULL;
2122 : 214030350 : callees = NULL;
2123 : 214030350 : if (call_site_hash)
2124 : : {
2125 : 29843 : call_site_hash->empty ();
2126 : 29843 : call_site_hash = NULL;
2127 : : }
2128 : 214030350 : }
2129 : :
2130 : : /* Remove all callers from the node. */
2131 : :
2132 : : void
2133 : 95662376 : cgraph_node::remove_callers (void)
2134 : : {
2135 : 95662376 : cgraph_edge *e, *f;
2136 : :
2137 : : /* It is sufficient to remove the edges from the lists of callees of
2138 : : the callers. The caller list of the node can be zapped with one
2139 : : assignment. */
2140 : 100204810 : for (e = callers; e; e = f)
2141 : : {
2142 : 4542434 : f = e->next_caller;
2143 : : /* When removing a callback-carrying edge, remove all its attached edges
2144 : : as well. */
2145 : 4542434 : if (e->has_callback)
2146 : : {
2147 : 0 : cgraph_edge *cbe, *next_cbe = NULL;
2148 : 0 : for (cbe = e->first_callback_edge (); cbe; cbe = next_cbe)
2149 : : {
2150 : 0 : next_cbe = cbe->next_callback_edge ();
2151 : 0 : cgraph_edge::remove (cbe);
2152 : : }
2153 : : }
2154 : 4542434 : symtab->call_edge_removal_hooks (e);
2155 : 4542434 : e->remove_caller ();
2156 : 4542434 : symtab->free_edge (e);
2157 : : }
2158 : 95662376 : callers = NULL;
2159 : 95662376 : }
2160 : :
2161 : : /* Helper function for cgraph_release_function_body and free_lang_data.
2162 : : It releases body from function DECL without having to inspect its
2163 : : possibly non-existent symtab node. */
2164 : :
2165 : : void
2166 : 105739833 : release_function_body (tree decl)
2167 : : {
2168 : 105739833 : function *fn = DECL_STRUCT_FUNCTION (decl);
2169 : 105739833 : if (fn)
2170 : : {
2171 : 94158103 : if (fn->cfg
2172 : 94158103 : && loops_for_fn (fn))
2173 : : {
2174 : 1744156 : fn->curr_properties &= ~PROP_loops;
2175 : 1744156 : loop_optimizer_finalize (fn);
2176 : : }
2177 : 94158103 : if (fn->gimple_df)
2178 : : {
2179 : 1752626 : delete_tree_ssa (fn);
2180 : 1752626 : fn->eh = NULL;
2181 : : }
2182 : 94158103 : if (fn->cfg)
2183 : : {
2184 : 1744157 : gcc_assert (!dom_info_available_p (fn, CDI_DOMINATORS));
2185 : 1744157 : gcc_assert (!dom_info_available_p (fn, CDI_POST_DOMINATORS));
2186 : 1744157 : delete_tree_cfg_annotations (fn);
2187 : 1744157 : free_cfg (fn);
2188 : 1744157 : fn->cfg = NULL;
2189 : : }
2190 : 94158103 : if (fn->value_histograms)
2191 : 12 : free_histograms (fn);
2192 : 94158103 : gimple_set_body (decl, NULL);
2193 : : /* Struct function hangs a lot of data that would leak if we didn't
2194 : : removed all pointers to it. */
2195 : 94158103 : ggc_free (fn);
2196 : 94158103 : DECL_STRUCT_FUNCTION (decl) = NULL;
2197 : : }
2198 : 105739833 : DECL_SAVED_TREE (decl) = NULL;
2199 : 105739833 : }
2200 : :
2201 : : /* Release memory used to represent body of function.
2202 : : Use this only for functions that are released before being translated to
2203 : : target code (i.e. RTL). Functions that are compiled to RTL and beyond
2204 : : are free'd in final.cc via free_after_compilation().
2205 : : KEEP_ARGUMENTS are useful only if you want to rebuild body as thunk. */
2206 : :
2207 : : void
2208 : 105728319 : cgraph_node::release_body (bool keep_arguments)
2209 : : {
2210 : 105728319 : ipa_transforms_to_apply.release ();
2211 : 105728319 : if (!used_as_abstract_origin && symtab->state != PARSING)
2212 : : {
2213 : 105130870 : DECL_RESULT (decl) = NULL;
2214 : :
2215 : 105130870 : if (!keep_arguments)
2216 : 105100162 : DECL_ARGUMENTS (decl) = NULL;
2217 : : }
2218 : : /* If the node is abstract and needed, then do not clear
2219 : : DECL_INITIAL of its associated function declaration because it's
2220 : : needed to emit debug info later. */
2221 : 105728319 : if (!used_as_abstract_origin && DECL_INITIAL (decl))
2222 : 93602786 : DECL_INITIAL (decl) = error_mark_node;
2223 : 105728319 : release_function_body (decl);
2224 : 105728319 : lto_free_function_in_decl_state_for_node (this);
2225 : 105728319 : if (flag_checking && clones)
2226 : : {
2227 : : /* It is invalid to release body before materializing clones except
2228 : : for thunks that don't really need a body. Verify also that we do
2229 : : not leak pointers to the call statements. */
2230 : 42 : for (cgraph_node *node = clones; node;
2231 : 24 : node = node->next_sibling_clone)
2232 : 24 : gcc_assert (node->thunk && !node->callees->call_stmt);
2233 : : }
2234 : 105728319 : remove_callees ();
2235 : 105728319 : remove_all_references ();
2236 : 105728319 : }
2237 : :
2238 : : /* Remove function from symbol table. */
2239 : :
2240 : : void
2241 : 95662376 : cgraph_node::remove (void)
2242 : : {
2243 : 95662376 : bool clone_info_set = false;
2244 : 95662376 : clone_info *info, saved_info;
2245 : 95662376 : if (symtab->ipa_clones_dump_file && symtab->cloned_nodes.contains (this))
2246 : 4 : fprintf (symtab->ipa_clones_dump_file,
2247 : : "Callgraph removal;%s;%d;%s;%d;%d\n", asm_name (), get_uid (),
2248 : 4 : DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
2249 : 8 : DECL_SOURCE_COLUMN (decl));
2250 : :
2251 : 95662376 : if ((info = clone_info::get (this)) != NULL)
2252 : : {
2253 : 378920 : saved_info = *info;
2254 : 378920 : clone_info_set = true;
2255 : : }
2256 : 95662376 : symtab->call_cgraph_removal_hooks (this);
2257 : 95662376 : remove_callers ();
2258 : 95662376 : remove_callees ();
2259 : 95662376 : ipa_transforms_to_apply.release ();
2260 : 95662376 : delete_function_version (function_version ());
2261 : :
2262 : : /* Incremental inlining access removed nodes stored in the postorder list.
2263 : : */
2264 : 95662376 : force_output = false;
2265 : 95662376 : forced_by_abi = false;
2266 : :
2267 : 190945832 : unregister (clone_info_set ? &saved_info : NULL);
2268 : 95662376 : if (prev_sibling_clone)
2269 : 755477 : prev_sibling_clone->next_sibling_clone = next_sibling_clone;
2270 : 94906899 : else if (clone_of)
2271 : : {
2272 : 1815167 : clone_of->clones = next_sibling_clone;
2273 : 1815167 : if (!clones)
2274 : : {
2275 : 1811190 : bool need_body = false;
2276 : 1811190 : for (cgraph_node *n = clone_of; n; n = n->clone_of)
2277 : 1806084 : if (n->analyzed || n->clones)
2278 : : {
2279 : : need_body = true;
2280 : : break;
2281 : : }
2282 : 1806080 : if (!need_body)
2283 : 5106 : clone_of->release_body ();
2284 : : }
2285 : : }
2286 : 95662376 : if (next_sibling_clone)
2287 : 951890 : next_sibling_clone->prev_sibling_clone = prev_sibling_clone;
2288 : 95662376 : if (clones)
2289 : : {
2290 : 38834 : cgraph_node *n, *next;
2291 : :
2292 : 38834 : if (clone_of)
2293 : : {
2294 : 189790 : for (n = clones; n->next_sibling_clone; n = n->next_sibling_clone)
2295 : 150956 : n->clone_of = clone_of;
2296 : 38834 : n->clone_of = clone_of;
2297 : 38834 : n->next_sibling_clone = clone_of->clones;
2298 : 38834 : if (clone_of->clones)
2299 : 33654 : clone_of->clones->prev_sibling_clone = n;
2300 : 38834 : clone_of->clones = clones;
2301 : : }
2302 : : else
2303 : : {
2304 : : /* We are removing node with clones. This makes clones inconsistent,
2305 : : but assume they will be removed subsequently and just keep clone
2306 : : tree intact. This can happen in unreachable function removal since
2307 : : we remove unreachable functions in random order, not by bottom-up
2308 : : walk of clone trees. */
2309 : 0 : for (n = clones; n; n = next)
2310 : : {
2311 : 0 : next = n->next_sibling_clone;
2312 : 0 : n->next_sibling_clone = NULL;
2313 : 0 : n->prev_sibling_clone = NULL;
2314 : 0 : n->clone_of = NULL;
2315 : : }
2316 : : }
2317 : : }
2318 : :
2319 : : /* While all the clones are removed after being proceeded, the function
2320 : : itself is kept in the cgraph even after it is compiled. Check whether
2321 : : we are done with this body and reclaim it proactively if this is the case.
2322 : : */
2323 : 95662376 : if (symtab->state != LTO_STREAMING)
2324 : : {
2325 : 95660330 : cgraph_node *n = cgraph_node::get (decl);
2326 : 95660330 : if (!n
2327 : 95660330 : || (!n->clones && !n->clone_of && !n->inlined_to
2328 : 1156072 : && ((symtab->global_info_ready || in_lto_p)
2329 : 9623 : && (TREE_ASM_WRITTEN (n->decl)
2330 : 9592 : || DECL_EXTERNAL (n->decl)
2331 : 5287 : || !n->analyzed
2332 : 5223 : || (!flag_wpa && n->in_other_partition)))))
2333 : 92670772 : release_body ();
2334 : : }
2335 : : else
2336 : 2046 : lto_free_function_in_decl_state_for_node (this);
2337 : :
2338 : 95662376 : decl = NULL;
2339 : 95662376 : if (call_site_hash)
2340 : : {
2341 : 0 : call_site_hash->empty ();
2342 : 0 : call_site_hash = NULL;
2343 : : }
2344 : :
2345 : 95662376 : symtab->release_symbol (this);
2346 : 95662376 : }
2347 : :
2348 : : /* Likewise indicate that a node is having address taken. */
2349 : :
2350 : : void
2351 : 4798603 : cgraph_node::mark_address_taken (void)
2352 : : {
2353 : : /* Indirect inlining can figure out that all uses of the address are
2354 : : inlined. */
2355 : 4798603 : if (inlined_to)
2356 : : {
2357 : 0 : gcc_assert (cfun->after_inlining);
2358 : 0 : gcc_assert (callers->indirect_inlining_edge);
2359 : : return;
2360 : : }
2361 : : /* FIXME: address_taken flag is used both as a shortcut for testing whether
2362 : : IPA_REF_ADDR reference exists (and thus it should be set on node
2363 : : representing alias we take address of) and as a test whether address
2364 : : of the object was taken (and thus it should be set on node alias is
2365 : : referring to). We should remove the first use and the remove the
2366 : : following set. */
2367 : 4798603 : address_taken = 1;
2368 : 4798603 : cgraph_node *node = ultimate_alias_target ();
2369 : 4798603 : node->address_taken = 1;
2370 : : }
2371 : :
2372 : : /* Return local info node for the compiled function. */
2373 : :
2374 : : cgraph_node *
2375 : 12647867 : cgraph_node::local_info_node (tree decl)
2376 : : {
2377 : 12647867 : gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
2378 : 12647867 : cgraph_node *node = get (decl);
2379 : 12647867 : if (!node)
2380 : : return NULL;
2381 : 12647867 : return node->ultimate_alias_target ();
2382 : : }
2383 : :
2384 : : /* Return RTL info for the compiled function. */
2385 : :
2386 : : cgraph_rtl_info *
2387 : 59400392 : cgraph_node::rtl_info (const_tree decl)
2388 : : {
2389 : 59400392 : gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
2390 : 59400392 : cgraph_node *node = get (decl);
2391 : 59400392 : if (!node)
2392 : : return NULL;
2393 : 59260240 : enum availability avail;
2394 : 59260240 : node = node->ultimate_alias_target (&avail);
2395 : 59260240 : if (decl != current_function_decl
2396 : 56258312 : && (avail < AVAIL_AVAILABLE
2397 : 51121597 : || (node->decl != current_function_decl
2398 : 51047832 : && !TREE_ASM_WRITTEN (node->decl))))
2399 : : return NULL;
2400 : : /* Allocate if it doesn't exist. */
2401 : 50578404 : if (node->rtl == NULL)
2402 : : {
2403 : 1344615 : node->rtl = ggc_cleared_alloc<cgraph_rtl_info> ();
2404 : 1344615 : SET_HARD_REG_SET (node->rtl->function_used_regs);
2405 : : }
2406 : 50578404 : return node->rtl;
2407 : : }
2408 : :
2409 : : /* Return a string describing the failure REASON. */
2410 : :
2411 : : const char*
2412 : 9831 : cgraph_inline_failed_string (cgraph_inline_failed_t reason)
2413 : : {
2414 : : #undef DEFCIFCODE
2415 : : #define DEFCIFCODE(code, type, string) string,
2416 : :
2417 : 9831 : static const char *cif_string_table[CIF_N_REASONS] = {
2418 : : #include "cif-code.def"
2419 : : };
2420 : :
2421 : : /* Signedness of an enum type is implementation defined, so cast it
2422 : : to unsigned before testing. */
2423 : 9831 : gcc_assert ((unsigned) reason < CIF_N_REASONS);
2424 : 9831 : return cif_string_table[reason];
2425 : : }
2426 : :
2427 : : /* Return a type describing the failure REASON. */
2428 : :
2429 : : cgraph_inline_failed_type_t
2430 : 73385592 : cgraph_inline_failed_type (cgraph_inline_failed_t reason)
2431 : : {
2432 : : #undef DEFCIFCODE
2433 : : #define DEFCIFCODE(code, type, string) type,
2434 : :
2435 : 73385592 : static cgraph_inline_failed_type_t cif_type_table[CIF_N_REASONS] = {
2436 : : #include "cif-code.def"
2437 : : };
2438 : :
2439 : : /* Signedness of an enum type is implementation defined, so cast it
2440 : : to unsigned before testing. */
2441 : 73385592 : gcc_assert ((unsigned) reason < CIF_N_REASONS);
2442 : 73385592 : return cif_type_table[reason];
2443 : : }
2444 : :
2445 : : /* Names used to print out the availability enum. */
2446 : : const char * const cgraph_availability_names[] =
2447 : : {"unset", "not_available", "overwritable", "available", "local"};
2448 : :
2449 : : /* Output flags of edge to a file F. */
2450 : :
2451 : : void
2452 : 22668 : cgraph_edge::dump_edge_flags (FILE *f)
2453 : : {
2454 : 22668 : if (speculative)
2455 : 282 : fprintf (f, "(speculative) ");
2456 : 22668 : if (callback)
2457 : 4 : fprintf (f, "(callback) ");
2458 : 22668 : if (has_callback)
2459 : 6 : fprintf (f, "(has_callback) ");
2460 : 22668 : if (!inline_failed)
2461 : 1789 : fprintf (f, "(inlined) ");
2462 : 22668 : if (call_stmt_cannot_inline_p)
2463 : 0 : fprintf (f, "(call_stmt_cannot_inline_p) ");
2464 : 22668 : if (indirect_inlining_edge)
2465 : 325 : fprintf (f, "(indirect_inlining) ");
2466 : 22668 : if (count.initialized_p ())
2467 : : {
2468 : 21909 : fprintf (f, "(");
2469 : 21909 : count.dump (f);
2470 : 21909 : fprintf (f, ",");
2471 : 21909 : fprintf (f, "%.2f per call) ", sreal_frequency ().to_double ());
2472 : : }
2473 : 22668 : if (can_throw_external)
2474 : 2684 : fprintf (f, "(can throw external) ");
2475 : 22668 : }
2476 : :
2477 : : /* Dump edge to stderr. */
2478 : :
2479 : : void
2480 : 0 : cgraph_edge::debug (void)
2481 : : {
2482 : 0 : fprintf (stderr, "%s -> %s ", caller->dump_asm_name (),
2483 : 0 : callee == NULL ? "(null)" : callee->dump_asm_name ());
2484 : 0 : dump_edge_flags (stderr);
2485 : 0 : fprintf (stderr, "\n\n");
2486 : 0 : caller->debug ();
2487 : 0 : if (callee != NULL)
2488 : 0 : callee->debug ();
2489 : 0 : }
2490 : :
2491 : : /* Dump call graph node to file F. */
2492 : :
2493 : : void
2494 : 5857 : cgraph_node::dump (FILE *f)
2495 : : {
2496 : 5857 : cgraph_edge *edge;
2497 : :
2498 : 5857 : dump_base (f);
2499 : :
2500 : 5857 : if (inlined_to)
2501 : 776 : fprintf (f, " Function %s is inline copy in %s\n",
2502 : : dump_name (),
2503 : : inlined_to->dump_name ());
2504 : 5857 : if (clone_of)
2505 : 762 : fprintf (f, " Clone of %s\n", clone_of->dump_asm_name ());
2506 : 5857 : if (symtab->function_flags_ready)
2507 : 10578 : fprintf (f, " Availability: %s\n",
2508 : 5289 : cgraph_availability_names [get_availability ()]);
2509 : :
2510 : 5857 : if (profile_id)
2511 : 155 : fprintf (f, " Profile id: %i\n",
2512 : : profile_id);
2513 : 5857 : if (unit_id)
2514 : 134 : fprintf (f, " Unit id: %i\n",
2515 : : unit_id);
2516 : 5857 : cgraph_function_version_info *vi = function_version ();
2517 : 5857 : if (vi != NULL)
2518 : : {
2519 : 0 : fprintf (f, " Version info: ");
2520 : 0 : if (vi->prev != NULL)
2521 : : {
2522 : 0 : fprintf (f, "prev: ");
2523 : 0 : fprintf (f, "%s ", vi->prev->this_node->dump_asm_name ());
2524 : : }
2525 : 0 : if (vi->next != NULL)
2526 : : {
2527 : 0 : fprintf (f, "next: ");
2528 : 0 : fprintf (f, "%s ", vi->next->this_node->dump_asm_name ());
2529 : : }
2530 : 0 : if (vi->dispatcher_resolver != NULL_TREE)
2531 : 0 : fprintf (f, "dispatcher: %s",
2532 : 0 : lang_hooks.decl_printable_name (vi->dispatcher_resolver, 2));
2533 : :
2534 : 0 : fprintf (f, "\n");
2535 : : }
2536 : 5857 : fprintf (f, " Function flags:");
2537 : 5857 : if (count.initialized_p ())
2538 : : {
2539 : 3620 : fprintf (f, " count:");
2540 : 3620 : count.dump (f);
2541 : : }
2542 : 5857 : if (tp_first_run > 0)
2543 : 70 : fprintf (f, " first_run:%" PRId64, (int64_t) tp_first_run);
2544 : 5857 : if (cgraph_node *origin = nested_function_origin (this))
2545 : 0 : fprintf (f, " nested in:%s", origin->dump_asm_name ());
2546 : 5857 : if (gimple_has_body_p (decl))
2547 : 3860 : fprintf (f, " body");
2548 : 5857 : if (process)
2549 : 0 : fprintf (f, " process");
2550 : 5857 : if (local)
2551 : 1139 : fprintf (f, " local");
2552 : 5857 : if (redefined_extern_inline)
2553 : 0 : fprintf (f, " redefined_extern_inline");
2554 : 5857 : if (only_called_at_startup)
2555 : 400 : fprintf (f, " only_called_at_startup");
2556 : 5857 : if (only_called_at_exit)
2557 : 7 : fprintf (f, " only_called_at_exit");
2558 : 5857 : if (tm_clone)
2559 : 0 : fprintf (f, " tm_clone");
2560 : 5857 : if (calls_comdat_local)
2561 : 9 : fprintf (f, " calls_comdat_local");
2562 : 5857 : if (icf_merged)
2563 : 24 : fprintf (f, " icf_merged");
2564 : 5857 : if (merged_comdat)
2565 : 0 : fprintf (f, " merged_comdat");
2566 : 5857 : if (merged_extern_inline)
2567 : 0 : fprintf (f, " merged_extern_inline");
2568 : 5857 : if (split_part)
2569 : 23 : fprintf (f, " split_part");
2570 : 5857 : if (indirect_call_target)
2571 : 218 : fprintf (f, " indirect_call_target");
2572 : 5857 : if (nonfreeing_fn)
2573 : 343 : fprintf (f, " nonfreeing_fn");
2574 : 5857 : if (DECL_STATIC_CONSTRUCTOR (decl))
2575 : 48 : fprintf (f," static_constructor (priority:%i)", get_init_priority ());
2576 : 5857 : if (DECL_STATIC_DESTRUCTOR (decl))
2577 : 7 : fprintf (f," static_destructor (priority:%i)", get_fini_priority ());
2578 : 5857 : if (frequency == NODE_FREQUENCY_HOT)
2579 : 62 : fprintf (f, " hot");
2580 : 5857 : if (frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2581 : 40 : fprintf (f, " unlikely_executed");
2582 : 5857 : if (frequency == NODE_FREQUENCY_EXECUTED_ONCE)
2583 : 694 : fprintf (f, " executed_once");
2584 : 5857 : if (opt_for_fn (decl, optimize_size))
2585 : 176 : fprintf (f, " optimize_size");
2586 : 5857 : if (parallelized_function)
2587 : 4 : fprintf (f, " parallelized_function");
2588 : 5857 : if (DECL_IS_MALLOC (decl))
2589 : 72 : fprintf (f, " decl_is_malloc");
2590 : 5857 : if (DECL_IS_OPERATOR_NEW_P (decl))
2591 : 35 : fprintf (f, " %soperator_new",
2592 : 35 : DECL_IS_REPLACEABLE_OPERATOR (decl) ? "replaceable_" : "");
2593 : 5857 : if (DECL_IS_OPERATOR_DELETE_P (decl))
2594 : 26 : fprintf (f, " %soperator_delete",
2595 : 26 : DECL_IS_REPLACEABLE_OPERATOR (decl) ? "replaceable_" : "");
2596 : :
2597 : 5857 : if (DECL_STATIC_CHAIN (decl))
2598 : 6 : fprintf (f, " static_chain");
2599 : :
2600 : 5857 : fprintf (f, "\n");
2601 : :
2602 : 5857 : if (thunk)
2603 : : {
2604 : 49 : fprintf (f, " Thunk");
2605 : 49 : thunk_info::get (this)->dump (f);
2606 : : }
2607 : 5808 : else if (former_thunk_p ())
2608 : : {
2609 : 21 : fprintf (f, " Former thunk ");
2610 : 21 : thunk_info::get (this)->dump (f);
2611 : : }
2612 : 5787 : else gcc_checking_assert (!thunk_info::get (this));
2613 : :
2614 : 5857 : fprintf (f, " Called by: ");
2615 : :
2616 : 5857 : profile_count sum = profile_count::zero ();
2617 : 13570 : for (edge = callers; edge; edge = edge->next_caller)
2618 : : {
2619 : 7713 : fprintf (f, "%s ", edge->caller->dump_asm_name ());
2620 : 7713 : edge->dump_edge_flags (f);
2621 : 7713 : if (edge->count.initialized_p ())
2622 : 7453 : sum += edge->count.ipa ();
2623 : : }
2624 : :
2625 : 5857 : fprintf (f, "\n Calls: ");
2626 : 20045 : for (edge = callees; edge; edge = edge->next_callee)
2627 : : {
2628 : 14188 : fprintf (f, "%s ", edge->callee->dump_asm_name ());
2629 : 14188 : edge->dump_edge_flags (f);
2630 : : }
2631 : 5857 : fprintf (f, "\n");
2632 : :
2633 : 5857 : if (!body_removed && count.ipa ().initialized_p ())
2634 : : {
2635 : 110 : bool ok = true;
2636 : 110 : bool min = false;
2637 : : ipa_ref *ref;
2638 : :
2639 : 110 : FOR_EACH_ALIAS (this, ref)
2640 : 0 : if (dyn_cast <cgraph_node *> (ref->referring)->count.initialized_p ())
2641 : 0 : sum += dyn_cast <cgraph_node *> (ref->referring)->count.ipa ();
2642 : :
2643 : 110 : if (inlined_to
2644 : 110 : || (symtab->state < EXPANSION
2645 : 110 : && ultimate_alias_target () == this && only_called_directly_p ()))
2646 : 3 : ok = !count.ipa ().differs_from_p (sum);
2647 : 107 : else if (count.ipa () > profile_count::from_gcov_type (100)
2648 : 107 : && count.ipa () < sum.apply_scale (99, 100))
2649 : 0 : ok = false, min = true;
2650 : 110 : if (!ok)
2651 : : {
2652 : 0 : fprintf (f, " Invalid sum of caller counts ");
2653 : 0 : sum.dump (f);
2654 : 0 : if (min)
2655 : 0 : fprintf (f, ", should be at most ");
2656 : : else
2657 : 0 : fprintf (f, ", should be ");
2658 : 0 : count.ipa ().dump (f);
2659 : 0 : fprintf (f, "\n");
2660 : : }
2661 : : }
2662 : :
2663 : 6624 : for (edge = indirect_calls; edge; edge = edge->next_callee)
2664 : : {
2665 : 767 : if (edge->indirect_info->polymorphic)
2666 : : {
2667 : 293 : fprintf (f, " Polymorphic indirect call of type ");
2668 : 293 : print_generic_expr (f, edge->indirect_info->otr_type, TDF_SLIM);
2669 : 293 : fprintf (f, " token:%i", (int) edge->indirect_info->otr_token);
2670 : : }
2671 : : else
2672 : 474 : fprintf (f, " Indirect call");
2673 : 767 : edge->dump_edge_flags (f);
2674 : 767 : if (edge->indirect_info->param_index != -1)
2675 : : {
2676 : 217 : fprintf (f, "of param:%i ", edge->indirect_info->param_index);
2677 : 217 : if (edge->indirect_info->agg_contents)
2678 : 19 : fprintf (f, "loaded from %s %s at offset %i ",
2679 : 19 : edge->indirect_info->member_ptr ? "member ptr" : "aggregate",
2680 : 19 : edge->indirect_info->by_ref ? "passed by reference" : "",
2681 : 19 : (int)edge->indirect_info->offset);
2682 : 217 : if (edge->indirect_info->vptr_changed)
2683 : 23 : fprintf (f, "(vptr maybe changed) ");
2684 : : }
2685 : 767 : fprintf (f, "num speculative call targets: %i\n",
2686 : 767 : edge->indirect_info->num_speculative_call_targets);
2687 : 767 : if (edge->indirect_info->polymorphic)
2688 : 293 : edge->indirect_info->context.dump (f);
2689 : : }
2690 : 5857 : }
2691 : :
2692 : : /* Dump call graph node to file F in graphviz format. */
2693 : :
2694 : : void
2695 : 0 : cgraph_node::dump_graphviz (FILE *f)
2696 : : {
2697 : 0 : cgraph_edge *edge;
2698 : :
2699 : 0 : for (edge = callees; edge; edge = edge->next_callee)
2700 : : {
2701 : 0 : cgraph_node *callee = edge->callee;
2702 : :
2703 : 0 : fprintf (f, "\t\"%s\" -> \"%s\"\n", dump_name (), callee->dump_name ());
2704 : : }
2705 : 0 : }
2706 : :
2707 : :
2708 : : /* Dump call graph node NODE to stderr. */
2709 : :
2710 : : DEBUG_FUNCTION void
2711 : 0 : cgraph_node::debug (void)
2712 : : {
2713 : 0 : dump (stderr);
2714 : 0 : }
2715 : :
2716 : : /* Dump the callgraph to file F. */
2717 : :
2718 : : void
2719 : 77 : cgraph_node::dump_cgraph (FILE *f)
2720 : : {
2721 : 77 : cgraph_node *node;
2722 : :
2723 : 77 : fprintf (f, "callgraph:\n\n");
2724 : 724 : FOR_EACH_FUNCTION (node)
2725 : 285 : node->dump (f);
2726 : 77 : }
2727 : :
2728 : : /* Return true when the DECL can possibly be inlined. */
2729 : :
2730 : : bool
2731 : 85444574 : cgraph_function_possibly_inlined_p (tree decl)
2732 : : {
2733 : 85444574 : if (!symtab->global_info_ready)
2734 : 77871472 : return !DECL_UNINLINABLE (decl);
2735 : 7573102 : return DECL_POSSIBLY_INLINED (decl);
2736 : : }
2737 : :
2738 : : /* Return function availability. See cgraph.h for description of individual
2739 : : return values. */
2740 : : enum availability
2741 : 812600770 : cgraph_node::get_availability (symtab_node *ref)
2742 : : {
2743 : 812600770 : if (ref)
2744 : : {
2745 : 574005846 : cgraph_node *cref = dyn_cast <cgraph_node *> (ref);
2746 : 574005846 : if (cref)
2747 : 574005846 : ref = cref->inlined_to;
2748 : : }
2749 : 812600770 : enum availability avail;
2750 : 812600770 : if (!analyzed && !in_other_partition)
2751 : 484430788 : avail = AVAIL_NOT_AVAILABLE;
2752 : 328169982 : else if (local)
2753 : 92834653 : avail = AVAIL_LOCAL;
2754 : 235335329 : else if (inlined_to)
2755 : 2248200 : avail = AVAIL_AVAILABLE;
2756 : 233087129 : else if (transparent_alias)
2757 : 134 : ultimate_alias_target (&avail, ref);
2758 : 233086995 : else if (ifunc_resolver
2759 : 233086995 : || lookup_attribute ("noipa", DECL_ATTRIBUTES (decl)))
2760 : 3442142 : avail = AVAIL_INTERPOSABLE;
2761 : 229644853 : else if (!externally_visible)
2762 : 29082113 : avail = AVAIL_AVAILABLE;
2763 : : /* If this is a reference from symbol itself and there are no aliases, we
2764 : : may be sure that the symbol was not interposed by something else because
2765 : : the symbol itself would be unreachable otherwise.
2766 : :
2767 : : Also comdat groups are always resolved in groups. */
2768 : 34777 : else if ((this == ref && !has_aliases_p ())
2769 : 200563485 : || (ref && get_comdat_group ()
2770 : 1526841 : && get_comdat_group () == ref->get_comdat_group ()))
2771 : 37541 : avail = AVAIL_AVAILABLE;
2772 : : /* Inline functions are safe to be analyzed even if their symbol can
2773 : : be overwritten at runtime. It is not meaningful to enforce any sane
2774 : : behavior on replacing inline function by different body. */
2775 : 200525199 : else if (DECL_DECLARED_INLINE_P (decl))
2776 : 70077632 : avail = AVAIL_AVAILABLE;
2777 : :
2778 : : /* If the function can be overwritten, return OVERWRITABLE. Take
2779 : : care at least of two notable extensions - the COMDAT functions
2780 : : used to share template instantiations in C++ (this is symmetric
2781 : : to code cp_cannot_inline_tree_fn and probably shall be shared and
2782 : : the inlinability hooks completely eliminated). */
2783 : :
2784 : 130447567 : else if (decl_replaceable_p (decl, semantic_interposition)
2785 : 130447567 : && !DECL_EXTERNAL (decl))
2786 : 9381172 : avail = AVAIL_INTERPOSABLE;
2787 : 121066395 : else avail = AVAIL_AVAILABLE;
2788 : :
2789 : 812600770 : return avail;
2790 : : }
2791 : :
2792 : : /* Worker for cgraph_node_can_be_local_p. */
2793 : : static bool
2794 : 860900 : cgraph_node_cannot_be_local_p_1 (cgraph_node *node, void *)
2795 : : {
2796 : 860900 : return !(!node->force_output
2797 : 840147 : && !node->ifunc_resolver
2798 : : /* Limitation of gas requires us to output targets of symver aliases
2799 : : as global symbols. This is binutils PR 25295. */
2800 : 840107 : && !node->symver
2801 : 840107 : && ((DECL_COMDAT (node->decl)
2802 : 358003 : && !node->forced_by_abi
2803 : 335590 : && !node->used_from_object_file_p ()
2804 : 335590 : && !node->same_comdat_group)
2805 : 553600 : || !node->externally_visible)
2806 : 434057 : && !DECL_STATIC_CONSTRUCTOR (node->decl)
2807 : 432304 : && !DECL_STATIC_DESTRUCTOR (node->decl));
2808 : : }
2809 : :
2810 : : /* Return true if cgraph_node can be made local for API change.
2811 : : Extern inline functions and C++ COMDAT functions can be made local
2812 : : at the expense of possible code size growth if function is used in multiple
2813 : : compilation units. */
2814 : : bool
2815 : 1169067 : cgraph_node::can_be_local_p (void)
2816 : : {
2817 : 1169067 : return (!address_taken
2818 : 1169067 : && !call_for_symbol_thunks_and_aliases (cgraph_node_cannot_be_local_p_1,
2819 : 1169067 : NULL, true));
2820 : : }
2821 : :
2822 : : /* Call callback on cgraph_node, thunks and aliases associated to cgraph_node.
2823 : : When INCLUDE_OVERWRITABLE is false, overwritable symbols are
2824 : : skipped. When EXCLUDE_VIRTUAL_THUNKS is true, virtual thunks are
2825 : : skipped. */
2826 : : bool
2827 : 127832835 : cgraph_node::call_for_symbol_thunks_and_aliases (bool (*callback)
2828 : : (cgraph_node *, void *),
2829 : : void *data,
2830 : : bool include_overwritable,
2831 : : bool exclude_virtual_thunks)
2832 : : {
2833 : 127832835 : cgraph_edge *e;
2834 : 127832835 : ipa_ref *ref;
2835 : 127832835 : enum availability avail = AVAIL_AVAILABLE;
2836 : :
2837 : 127832835 : if (include_overwritable
2838 : 127832835 : || (avail = get_availability ()) > AVAIL_INTERPOSABLE)
2839 : : {
2840 : 127821202 : if (callback (this, data))
2841 : : return true;
2842 : : }
2843 : 135097393 : FOR_EACH_ALIAS (this, ref)
2844 : : {
2845 : 12627032 : cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2846 : 12627032 : if (include_overwritable
2847 : 12627032 : || alias->get_availability () > AVAIL_INTERPOSABLE)
2848 : 12626790 : if (alias->call_for_symbol_thunks_and_aliases (callback, data,
2849 : : include_overwritable,
2850 : : exclude_virtual_thunks))
2851 : : return true;
2852 : : }
2853 : 122470361 : if (avail <= AVAIL_INTERPOSABLE)
2854 : : return false;
2855 : 128027457 : for (e = callers; e; e = e->next_caller)
2856 : 5568729 : if (e->caller->thunk
2857 : 2782 : && (include_overwritable
2858 : 426 : || e->caller->get_availability () > AVAIL_INTERPOSABLE)
2859 : 5571511 : && !(exclude_virtual_thunks
2860 : 22 : && thunk_info::get (e->caller)->virtual_offset_p))
2861 : 2770 : if (e->caller->call_for_symbol_thunks_and_aliases (callback, data,
2862 : : include_overwritable,
2863 : : exclude_virtual_thunks))
2864 : : return true;
2865 : :
2866 : : return false;
2867 : : }
2868 : :
2869 : : /* Worker to bring NODE local. */
2870 : :
2871 : : bool
2872 : 0 : cgraph_node::make_local (cgraph_node *node, void *)
2873 : : {
2874 : 0 : gcc_checking_assert (node->can_be_local_p ());
2875 : 0 : if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2876 : : {
2877 : 0 : node->make_decl_local ();
2878 : 0 : node->set_section (NULL);
2879 : 0 : node->set_comdat_group (NULL);
2880 : 0 : node->externally_visible = false;
2881 : 0 : node->forced_by_abi = false;
2882 : 0 : node->local = true;
2883 : 0 : node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
2884 : 0 : || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
2885 : 0 : && !flag_incremental_link);
2886 : 0 : node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2887 : 0 : gcc_assert (node->get_availability () == AVAIL_LOCAL);
2888 : : }
2889 : 0 : return false;
2890 : : }
2891 : :
2892 : : /* Bring cgraph node local. */
2893 : :
2894 : : void
2895 : 0 : cgraph_node::make_local (void)
2896 : : {
2897 : 0 : call_for_symbol_thunks_and_aliases (cgraph_node::make_local, NULL, true);
2898 : 0 : }
2899 : :
2900 : : /* Worker to set nothrow flag. */
2901 : :
2902 : : static void
2903 : 933136 : set_nothrow_flag_1 (cgraph_node *node, bool nothrow, bool non_call,
2904 : : bool *changed)
2905 : : {
2906 : 933136 : cgraph_edge *e;
2907 : :
2908 : 933136 : if (nothrow && !TREE_NOTHROW (node->decl))
2909 : : {
2910 : : /* With non-call exceptions we can't say for sure if other function body
2911 : : was not possibly optimized to still throw. */
2912 : 933111 : if (!non_call || node->binds_to_current_def_p ())
2913 : : {
2914 : 927389 : TREE_NOTHROW (node->decl) = true;
2915 : 927389 : *changed = true;
2916 : 2278970 : for (e = node->callers; e; e = e->next_caller)
2917 : 1351581 : e->can_throw_external = false;
2918 : : }
2919 : : }
2920 : 0 : else if (!nothrow && TREE_NOTHROW (node->decl))
2921 : : {
2922 : 0 : TREE_NOTHROW (node->decl) = false;
2923 : 0 : *changed = true;
2924 : : }
2925 : : ipa_ref *ref;
2926 : 988501 : FOR_EACH_ALIAS (node, ref)
2927 : : {
2928 : 55365 : cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2929 : 55365 : if (!nothrow || alias->get_availability () > AVAIL_INTERPOSABLE)
2930 : 54940 : set_nothrow_flag_1 (alias, nothrow, non_call, changed);
2931 : : }
2932 : 2322161 : for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2933 : 1389025 : if (e->caller->thunk
2934 : 1389025 : && (!nothrow || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2935 : 135 : set_nothrow_flag_1 (e->caller, nothrow, non_call, changed);
2936 : 933136 : }
2937 : :
2938 : : /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2939 : : if any to NOTHROW. */
2940 : :
2941 : : bool
2942 : 890020 : cgraph_node::set_nothrow_flag (bool nothrow)
2943 : : {
2944 : 890020 : bool changed = false;
2945 : 890020 : bool non_call = opt_for_fn (decl, flag_non_call_exceptions);
2946 : :
2947 : 890020 : if (!nothrow || get_availability () > AVAIL_INTERPOSABLE)
2948 : 877952 : set_nothrow_flag_1 (this, nothrow, non_call, &changed);
2949 : : else
2950 : : {
2951 : : ipa_ref *ref;
2952 : :
2953 : 19401 : FOR_EACH_ALIAS (this, ref)
2954 : : {
2955 : 7333 : cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2956 : 7333 : if (!nothrow || alias->get_availability () > AVAIL_INTERPOSABLE)
2957 : 109 : set_nothrow_flag_1 (alias, nothrow, non_call, &changed);
2958 : : }
2959 : : }
2960 : 890020 : return changed;
2961 : : }
2962 : :
2963 : : /* Worker to set malloc flag. */
2964 : : static void
2965 : 23514 : set_malloc_flag_1 (cgraph_node *node, bool malloc_p, bool *changed)
2966 : : {
2967 : 23514 : if (malloc_p && !DECL_IS_MALLOC (node->decl))
2968 : : {
2969 : 23095 : DECL_IS_MALLOC (node->decl) = true;
2970 : 23095 : *changed = true;
2971 : : }
2972 : :
2973 : : ipa_ref *ref;
2974 : 23515 : FOR_EACH_ALIAS (node, ref)
2975 : : {
2976 : 1 : cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
2977 : 1 : if (!malloc_p || alias->get_availability () > AVAIL_INTERPOSABLE)
2978 : 1 : set_malloc_flag_1 (alias, malloc_p, changed);
2979 : : }
2980 : :
2981 : 52087 : for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2982 : 28573 : if (e->caller->thunk
2983 : 28573 : && (!malloc_p || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2984 : 0 : set_malloc_flag_1 (e->caller, malloc_p, changed);
2985 : 23514 : }
2986 : :
2987 : : /* Set DECL_IS_MALLOC on NODE's decl and on NODE's aliases if any. */
2988 : :
2989 : : bool
2990 : 23513 : cgraph_node::set_malloc_flag (bool malloc_p)
2991 : : {
2992 : 23513 : bool changed = false;
2993 : :
2994 : 23513 : if (!malloc_p || get_availability () > AVAIL_INTERPOSABLE)
2995 : 23513 : set_malloc_flag_1 (this, malloc_p, &changed);
2996 : : else
2997 : : {
2998 : : ipa_ref *ref;
2999 : :
3000 : 0 : FOR_EACH_ALIAS (this, ref)
3001 : : {
3002 : 0 : cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
3003 : 0 : if (!malloc_p || alias->get_availability () > AVAIL_INTERPOSABLE)
3004 : 0 : set_malloc_flag_1 (alias, malloc_p, &changed);
3005 : : }
3006 : : }
3007 : 23513 : return changed;
3008 : : }
3009 : :
3010 : : /* Worker to set malloc flag. */
3011 : : static void
3012 : 232530 : add_detected_attribute_1 (cgraph_node *node, const char *attr, bool *changed)
3013 : : {
3014 : 232530 : if (!lookup_attribute (attr, DECL_ATTRIBUTES (node->decl)))
3015 : : {
3016 : 207478 : DECL_ATTRIBUTES (node->decl) = tree_cons (get_identifier (attr),
3017 : 207478 : NULL_TREE, DECL_ATTRIBUTES (node->decl));
3018 : 207478 : *changed = true;
3019 : : }
3020 : :
3021 : : ipa_ref *ref;
3022 : 233007 : FOR_EACH_ALIAS (node, ref)
3023 : : {
3024 : 477 : cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
3025 : 477 : if (alias->get_availability () > AVAIL_INTERPOSABLE)
3026 : 68 : add_detected_attribute_1 (alias, attr, changed);
3027 : : }
3028 : :
3029 : 750377 : for (cgraph_edge *e = node->callers; e; e = e->next_caller)
3030 : 517847 : if (e->caller->thunk
3031 : 517847 : && (e->caller->get_availability () > AVAIL_INTERPOSABLE))
3032 : 14 : add_detected_attribute_1 (e->caller, attr, changed);
3033 : 232530 : }
3034 : :
3035 : : /* Add attribyte ATTR to function and its aliases. */
3036 : :
3037 : : bool
3038 : 236214 : cgraph_node::add_detected_attribute (const char *attr)
3039 : : {
3040 : 236214 : bool changed = false;
3041 : :
3042 : 236214 : if (get_availability () > AVAIL_INTERPOSABLE)
3043 : 232448 : add_detected_attribute_1 (this, attr, &changed);
3044 : : else
3045 : : {
3046 : : ipa_ref *ref;
3047 : :
3048 : 3790 : FOR_EACH_ALIAS (this, ref)
3049 : : {
3050 : 24 : cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
3051 : 24 : if (alias->get_availability () > AVAIL_INTERPOSABLE)
3052 : 0 : add_detected_attribute_1 (alias, attr, &changed);
3053 : : }
3054 : : }
3055 : 236214 : return changed;
3056 : : }
3057 : :
3058 : : /* Worker to set noreturng flag. */
3059 : : static void
3060 : 28182 : set_noreturn_flag_1 (cgraph_node *node, bool noreturn_p, bool *changed)
3061 : : {
3062 : 28182 : if (noreturn_p && !TREE_THIS_VOLATILE (node->decl))
3063 : : {
3064 : 28182 : TREE_THIS_VOLATILE (node->decl) = true;
3065 : 28182 : *changed = true;
3066 : : }
3067 : :
3068 : : ipa_ref *ref;
3069 : 28845 : FOR_EACH_ALIAS (node, ref)
3070 : : {
3071 : 663 : cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
3072 : 663 : if (!noreturn_p || alias->get_availability () > AVAIL_INTERPOSABLE)
3073 : 663 : set_noreturn_flag_1 (alias, noreturn_p, changed);
3074 : : }
3075 : :
3076 : 45490 : for (cgraph_edge *e = node->callers; e; e = e->next_caller)
3077 : 17308 : if (e->caller->thunk
3078 : 17308 : && (!noreturn_p || e->caller->get_availability () > AVAIL_INTERPOSABLE))
3079 : 20 : set_noreturn_flag_1 (e->caller, noreturn_p, changed);
3080 : 28182 : }
3081 : :
3082 : : /* Set TREE_THIS_VOLATILE on NODE's decl and on NODE's aliases if any. */
3083 : :
3084 : : bool
3085 : 27653 : cgraph_node::set_noreturn_flag (bool noreturn_p)
3086 : : {
3087 : 27653 : bool changed = false;
3088 : :
3089 : 27653 : if (!noreturn_p || get_availability () > AVAIL_INTERPOSABLE)
3090 : 27490 : set_noreturn_flag_1 (this, noreturn_p, &changed);
3091 : : else
3092 : : {
3093 : : ipa_ref *ref;
3094 : :
3095 : 180 : FOR_EACH_ALIAS (this, ref)
3096 : : {
3097 : 17 : cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
3098 : 17 : if (!noreturn_p || alias->get_availability () > AVAIL_INTERPOSABLE)
3099 : 9 : set_noreturn_flag_1 (alias, noreturn_p, &changed);
3100 : : }
3101 : : }
3102 : 27653 : return changed;
3103 : : }
3104 : :
3105 : : /* Worker to set_const_flag. */
3106 : :
3107 : : static void
3108 : 1094198 : set_const_flag_1 (cgraph_node *node, bool set_const, bool looping,
3109 : : bool *changed)
3110 : : {
3111 : : /* Static constructors and destructors without a side effect can be
3112 : : optimized out. */
3113 : 1094198 : if (set_const && !looping)
3114 : : {
3115 : 1088249 : if (DECL_STATIC_CONSTRUCTOR (node->decl))
3116 : : {
3117 : 243 : DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
3118 : 243 : *changed = true;
3119 : : }
3120 : 1088249 : if (DECL_STATIC_DESTRUCTOR (node->decl))
3121 : : {
3122 : 1 : DECL_STATIC_DESTRUCTOR (node->decl) = 0;
3123 : 1 : *changed = true;
3124 : : }
3125 : : }
3126 : 1094198 : if (!set_const)
3127 : : {
3128 : 2194 : if (TREE_READONLY (node->decl))
3129 : : {
3130 : 159 : TREE_READONLY (node->decl) = 0;
3131 : 159 : DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
3132 : 159 : *changed = true;
3133 : : }
3134 : : }
3135 : : else
3136 : : {
3137 : : /* Consider function:
3138 : :
3139 : : bool a(int *p)
3140 : : {
3141 : : return *p==*p;
3142 : : }
3143 : :
3144 : : During early optimization we will turn this into:
3145 : :
3146 : : bool a(int *p)
3147 : : {
3148 : : return true;
3149 : : }
3150 : :
3151 : : Now if this function will be detected as CONST however when interposed
3152 : : it may end up being just pure. We always must assume the worst
3153 : : scenario here. */
3154 : 1092004 : if (TREE_READONLY (node->decl))
3155 : : {
3156 : 737 : if (!looping && DECL_LOOPING_CONST_OR_PURE_P (node->decl))
3157 : : {
3158 : 420 : DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
3159 : 420 : *changed = true;
3160 : : }
3161 : : }
3162 : 1091267 : else if (node->binds_to_current_def_p ())
3163 : : {
3164 : 173290 : TREE_READONLY (node->decl) = true;
3165 : 173290 : DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
3166 : 173290 : DECL_PURE_P (node->decl) = false;
3167 : 173290 : *changed = true;
3168 : : }
3169 : : else
3170 : : {
3171 : 917977 : if (dump_file && (dump_flags & TDF_DETAILS))
3172 : 0 : fprintf (dump_file, "Dropping state to PURE because function does "
3173 : : "not bind to current def.\n");
3174 : 917977 : if (!DECL_PURE_P (node->decl))
3175 : : {
3176 : 439423 : DECL_PURE_P (node->decl) = true;
3177 : 439423 : DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
3178 : 439423 : *changed = true;
3179 : : }
3180 : 478554 : else if (!looping && DECL_LOOPING_CONST_OR_PURE_P (node->decl))
3181 : : {
3182 : 143 : DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
3183 : 143 : *changed = true;
3184 : : }
3185 : : }
3186 : : }
3187 : :
3188 : : ipa_ref *ref;
3189 : 1236375 : FOR_EACH_ALIAS (node, ref)
3190 : : {
3191 : 142177 : cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
3192 : 142177 : if (!set_const || alias->get_availability () > AVAIL_INTERPOSABLE)
3193 : 142114 : set_const_flag_1 (alias, set_const, looping, changed);
3194 : : }
3195 : 1094294 : for (struct cgraph_node *n = node->simd_clones; n != NULL;
3196 : 96 : n = n->simdclone->next_clone)
3197 : 96 : set_const_flag_1 (n, set_const, looping, changed);
3198 : 2848278 : for (cgraph_edge *e = node->callers; e; e = e->next_caller)
3199 : 1754080 : if (e->caller->thunk
3200 : 1754080 : && (!set_const || e->caller->get_availability () > AVAIL_INTERPOSABLE))
3201 : : {
3202 : : /* Virtual thunks access virtual offset in the vtable, so they can
3203 : : only be pure, never const. */
3204 : 369 : if (set_const
3205 : 369 : && (thunk_info::get (e->caller)->virtual_offset_p
3206 : 238 : || !node->binds_to_current_def_p (e->caller)))
3207 : 131 : *changed |= e->caller->set_pure_flag (true, looping);
3208 : : else
3209 : 238 : set_const_flag_1 (e->caller, set_const, looping, changed);
3210 : : }
3211 : 1094198 : }
3212 : :
3213 : : /* If SET_CONST is true, mark function, aliases and thunks to be ECF_CONST.
3214 : : If SET_CONST if false, clear the flag.
3215 : :
3216 : : When setting the flag be careful about possible interposition and
3217 : : do not set the flag for functions that can be interposed and set pure
3218 : : flag for functions that can bind to other definition.
3219 : :
3220 : : Return true if any change was done. */
3221 : :
3222 : : bool
3223 : 977588 : cgraph_node::set_const_flag (bool set_const, bool looping)
3224 : : {
3225 : 977588 : bool changed = false;
3226 : 977588 : if (!set_const || get_availability () > AVAIL_INTERPOSABLE)
3227 : 951555 : set_const_flag_1 (this, set_const, looping, &changed);
3228 : : else
3229 : : {
3230 : : ipa_ref *ref;
3231 : :
3232 : 26912 : FOR_EACH_ALIAS (this, ref)
3233 : : {
3234 : 879 : cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
3235 : 879 : if (!set_const || alias->get_availability () > AVAIL_INTERPOSABLE)
3236 : 195 : set_const_flag_1 (alias, set_const, looping, &changed);
3237 : : }
3238 : : }
3239 : 977588 : return changed;
3240 : : }
3241 : :
3242 : : /* Info used by set_pure_flag_1. */
3243 : :
3244 : : struct set_pure_flag_info
3245 : : {
3246 : : bool pure;
3247 : : bool looping;
3248 : : bool changed;
3249 : : };
3250 : :
3251 : : /* Worker to set_pure_flag. */
3252 : :
3253 : : static bool
3254 : 365537 : set_pure_flag_1 (cgraph_node *node, void *data)
3255 : : {
3256 : 365537 : struct set_pure_flag_info *info = (struct set_pure_flag_info *)data;
3257 : : /* Static constructors and destructors without a side effect can be
3258 : : optimized out. */
3259 : 365537 : if (info->pure && !info->looping)
3260 : : {
3261 : 298890 : if (DECL_STATIC_CONSTRUCTOR (node->decl))
3262 : : {
3263 : 0 : DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
3264 : 0 : info->changed = true;
3265 : : }
3266 : 298890 : if (DECL_STATIC_DESTRUCTOR (node->decl))
3267 : : {
3268 : 0 : DECL_STATIC_DESTRUCTOR (node->decl) = 0;
3269 : 0 : info->changed = true;
3270 : : }
3271 : : }
3272 : 365537 : if (info->pure)
3273 : : {
3274 : 363343 : if (!DECL_PURE_P (node->decl) && !TREE_READONLY (node->decl))
3275 : : {
3276 : 362754 : DECL_PURE_P (node->decl) = true;
3277 : 362754 : DECL_LOOPING_CONST_OR_PURE_P (node->decl) = info->looping;
3278 : 362754 : info->changed = true;
3279 : : }
3280 : 589 : else if (DECL_LOOPING_CONST_OR_PURE_P (node->decl)
3281 : 589 : && !info->looping)
3282 : : {
3283 : 312 : DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
3284 : 312 : info->changed = true;
3285 : : }
3286 : : }
3287 : : else
3288 : : {
3289 : 2194 : if (DECL_PURE_P (node->decl))
3290 : : {
3291 : 75 : DECL_PURE_P (node->decl) = false;
3292 : 75 : DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
3293 : 75 : info->changed = true;
3294 : : }
3295 : : }
3296 : 365537 : return false;
3297 : : }
3298 : :
3299 : : /* Set DECL_PURE_P on cgraph_node's decl and on aliases of the node
3300 : : if any to PURE.
3301 : :
3302 : : When setting the flag, be careful about possible interposition.
3303 : : Return true if any change was done. */
3304 : :
3305 : : bool
3306 : 376282 : cgraph_node::set_pure_flag (bool pure, bool looping)
3307 : : {
3308 : 376282 : struct set_pure_flag_info info = {pure, looping, false};
3309 : 376282 : call_for_symbol_thunks_and_aliases (set_pure_flag_1, &info, !pure, true);
3310 : 376282 : for (struct cgraph_node *n = simd_clones; n != NULL;
3311 : 0 : n = n->simdclone->next_clone)
3312 : 0 : set_pure_flag_1 (n, &info);
3313 : 376282 : return info.changed;
3314 : : }
3315 : :
3316 : : /* Return true when cgraph_node cannot return or throw and thus
3317 : : it is safe to ignore its side effects for IPA analysis. */
3318 : :
3319 : : bool
3320 : 14357086 : cgraph_node::cannot_return_p (void)
3321 : : {
3322 : 14357086 : int flags = flags_from_decl_or_type (decl);
3323 : 14357086 : if (!opt_for_fn (decl, flag_exceptions))
3324 : 4696881 : return (flags & ECF_NORETURN) != 0;
3325 : : else
3326 : 9660205 : return ((flags & (ECF_NORETURN | ECF_NOTHROW))
3327 : 9660205 : == (ECF_NORETURN | ECF_NOTHROW));
3328 : : }
3329 : :
3330 : : /* Return true when call of edge cannot lead to return from caller
3331 : : and thus it is safe to ignore its side effects for IPA analysis
3332 : : when computing side effects of the caller.
3333 : : FIXME: We could actually mark all edges that have no reaching
3334 : : patch to the exit block or throw to get better results. */
3335 : : bool
3336 : 3010230 : cgraph_edge::cannot_lead_to_return_p (void)
3337 : : {
3338 : 3010230 : if (caller->cannot_return_p ())
3339 : : return true;
3340 : 2923512 : if (indirect_unknown_callee)
3341 : : {
3342 : 100884 : int flags = indirect_info->ecf_flags;
3343 : 100884 : if (!opt_for_fn (caller->decl, flag_exceptions))
3344 : 19383 : return (flags & ECF_NORETURN) != 0;
3345 : : else
3346 : 81501 : return ((flags & (ECF_NORETURN | ECF_NOTHROW))
3347 : 81501 : == (ECF_NORETURN | ECF_NOTHROW));
3348 : : }
3349 : : else
3350 : 2822628 : return callee->cannot_return_p ();
3351 : : }
3352 : :
3353 : : /* Return true if the edge after scaling it profile by SCALE
3354 : : may be considered hot. */
3355 : :
3356 : : bool
3357 : 5410741 : cgraph_edge::maybe_hot_p (sreal scale)
3358 : : {
3359 : : /* Never consider calls in functions optimized for size hot. */
3360 : 5410741 : if (opt_for_fn (caller->decl, optimize_size))
3361 : : return false;
3362 : :
3363 : : /* If reliable IPA count is available, just use it. */
3364 : 5356033 : profile_count c = count.ipa ();
3365 : 5356033 : if (c.reliable_p ()
3366 : 5356033 : || (c.quality () == AFDO && c.nonzero_p ()))
3367 : 716526 : return maybe_hot_count_p (NULL, c * scale);
3368 : :
3369 : : /* In auto-FDO, count 0 may lead to hot code in case the
3370 : : call is simply not called often enough to receive some samples. */
3371 : 4639507 : if ((c.quality () == AFDO
3372 : 4639507 : || count.quality () == GUESSED_GLOBAL0_ADJUSTED)
3373 : 4639507 : && callee && callee->count.quality () == AFDO)
3374 : 0 : return maybe_hot_count_p (NULL, c.force_nonzero () * scale);
3375 : :
3376 : : /* See if we can determine hotness using caller frequency. */
3377 : 4639507 : if (caller->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED
3378 : 4636455 : || (callee
3379 : 4208170 : && callee->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED))
3380 : : return false;
3381 : 4632864 : if (caller->frequency > NODE_FREQUENCY_UNLIKELY_EXECUTED
3382 : 4632864 : && (callee
3383 : 4204579 : && callee->frequency <= NODE_FREQUENCY_EXECUTED_ONCE))
3384 : : return false;
3385 : : /* ??? This may make sense for hot functions determined by
3386 : : user attribute, but if function is hot by profile, it may
3387 : : contains non-hot calls. In most practical cases this case
3388 : : is handled by the reliable ipa count above, but i.e. after
3389 : : inlining function with no profile to function with profile
3390 : : we get here.. */
3391 : 4578630 : if (caller->frequency == NODE_FREQUENCY_HOT)
3392 : : return true;
3393 : :
3394 : : /* Use IPA count and if it s not available appy local heuristics. */
3395 : 4578628 : if (c.initialized_p ())
3396 : : {
3397 : : /* A special case; AFDO zero means that function may quite possibly
3398 : : be executed few times per execution. If scale is large, we still
3399 : : want to consider the call hot. */
3400 : 0 : if (c.quality () == AFDO)
3401 : 0 : c = c.force_nonzero ();
3402 : 0 : return maybe_hot_count_p (NULL, c * scale);
3403 : : }
3404 : 4578628 : if (!count.initialized_p ())
3405 : : return true;
3406 : 3336070 : cgraph_node *where = caller->inlined_to ? caller->inlined_to : caller;
3407 : 3336070 : if (!where->count.initialized_p ())
3408 : : return true;
3409 : 3336070 : c = count * scale;
3410 : 3336070 : if (caller->frequency == NODE_FREQUENCY_EXECUTED_ONCE)
3411 : : {
3412 : 70116 : if (c * 2 < where->count * 3)
3413 : : return false;
3414 : : }
3415 : 3265954 : else if (c * param_hot_bb_frequency_fraction < where->count)
3416 : : return false;
3417 : : return true;
3418 : : }
3419 : :
3420 : : /* Return true if the edge may be considered hot. */
3421 : :
3422 : : bool
3423 : 2189398 : cgraph_edge::maybe_hot_p ()
3424 : : {
3425 : 2189398 : return maybe_hot_p (1);
3426 : : }
3427 : :
3428 : : /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
3429 : :
3430 : : static bool
3431 : 837186 : nonremovable_p (cgraph_node *node, void *)
3432 : : {
3433 : 837186 : return !node->can_remove_if_no_direct_calls_and_refs_p ();
3434 : : }
3435 : :
3436 : : /* Return true if whole comdat group can be removed if there are no direct
3437 : : calls to THIS. */
3438 : :
3439 : : bool
3440 : 1059219 : cgraph_node::can_remove_if_no_direct_calls_p (bool will_inline)
3441 : : {
3442 : 1059219 : struct ipa_ref *ref;
3443 : :
3444 : : /* For local symbols or non-comdat group it is the same as
3445 : : can_remove_if_no_direct_calls_p. */
3446 : 1059219 : if (!externally_visible || !same_comdat_group)
3447 : : {
3448 : 846298 : if (DECL_EXTERNAL (decl))
3449 : : return true;
3450 : 846298 : if (address_taken)
3451 : : return false;
3452 : 810233 : return !call_for_symbol_and_aliases (nonremovable_p, NULL, true);
3453 : : }
3454 : :
3455 : 212921 : if (will_inline && address_taken)
3456 : : return false;
3457 : :
3458 : : /* Otherwise check if we can remove the symbol itself and then verify
3459 : : that only uses of the comdat groups are direct call to THIS
3460 : : or its aliases. */
3461 : 212921 : if (!can_remove_if_no_direct_calls_and_refs_p ())
3462 : : return false;
3463 : :
3464 : : /* Check that all refs come from within the comdat group. */
3465 : 407511 : for (int i = 0; iterate_referring (i, ref); i++)
3466 : 208019 : if (ref->referring->get_comdat_group () != get_comdat_group ())
3467 : : return false;
3468 : :
3469 : 199492 : struct cgraph_node *target = ultimate_alias_target ();
3470 : 199492 : for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
3471 : 603104 : next != this; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
3472 : : {
3473 : 213358 : if (!externally_visible)
3474 : 0 : continue;
3475 : 213358 : if (!next->alias
3476 : 213358 : && !next->can_remove_if_no_direct_calls_and_refs_p ())
3477 : : return false;
3478 : :
3479 : : /* If we see different symbol than THIS, be sure to check calls. */
3480 : 213358 : if (next->ultimate_alias_target () != target)
3481 : 22355 : for (cgraph_edge *e = next->callers; e; e = e->next_caller)
3482 : 4820 : if (e->caller->get_comdat_group () != get_comdat_group ()
3483 : 4820 : || will_inline)
3484 : : return false;
3485 : :
3486 : : /* If function is not being inlined, we care only about
3487 : : references outside of the comdat group. */
3488 : 211402 : if (!will_inline)
3489 : 212588 : for (int i = 0; next->iterate_referring (i, ref); i++)
3490 : 10782 : if (ref->referring->get_comdat_group () != get_comdat_group ())
3491 : : return false;
3492 : : }
3493 : : return true;
3494 : : }
3495 : :
3496 : : /* Return true when function cgraph_node can be expected to be removed
3497 : : from program when direct calls in this compilation unit are removed.
3498 : :
3499 : : As a special case COMDAT functions are
3500 : : cgraph_can_remove_if_no_direct_calls_p while the are not
3501 : : cgraph_only_called_directly_p (it is possible they are called from other
3502 : : unit)
3503 : :
3504 : : This function behaves as cgraph_only_called_directly_p because eliminating
3505 : : all uses of COMDAT function does not make it necessarily disappear from
3506 : : the program unless we are compiling whole program or we do LTO. In this
3507 : : case we know we win since dynamic linking will not really discard the
3508 : : linkonce section. */
3509 : :
3510 : : bool
3511 : 3241084 : cgraph_node::will_be_removed_from_program_if_no_direct_calls_p
3512 : : (bool will_inline)
3513 : : {
3514 : 3241084 : gcc_assert (!inlined_to);
3515 : 3241084 : if (DECL_EXTERNAL (decl))
3516 : : return true;
3517 : :
3518 : 3241084 : if (!in_lto_p && !flag_whole_program)
3519 : : {
3520 : : /* If the symbol is in comdat group, we need to verify that whole comdat
3521 : : group becomes unreachable. Technically we could skip references from
3522 : : within the group, too. */
3523 : 2987754 : if (!only_called_directly_p ())
3524 : : return false;
3525 : 724283 : if (same_comdat_group && externally_visible)
3526 : : {
3527 : 0 : struct cgraph_node *target = ultimate_alias_target ();
3528 : :
3529 : 0 : if (will_inline && address_taken)
3530 : : return true;
3531 : 0 : for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
3532 : 0 : next != this;
3533 : 0 : next = dyn_cast<cgraph_node *> (next->same_comdat_group))
3534 : : {
3535 : 0 : if (!externally_visible)
3536 : 0 : continue;
3537 : 0 : if (!next->alias
3538 : 0 : && !next->only_called_directly_p ())
3539 : : return false;
3540 : :
3541 : : /* If we see different symbol than THIS,
3542 : : be sure to check calls. */
3543 : 0 : if (next->ultimate_alias_target () != target)
3544 : 0 : for (cgraph_edge *e = next->callers; e; e = e->next_caller)
3545 : 0 : if (e->caller->get_comdat_group () != get_comdat_group ()
3546 : 0 : || will_inline)
3547 : : return false;
3548 : : }
3549 : : }
3550 : 724283 : return true;
3551 : : }
3552 : : else
3553 : 253330 : return can_remove_if_no_direct_calls_p (will_inline);
3554 : : }
3555 : :
3556 : :
3557 : : /* Worker for cgraph_only_called_directly_p. */
3558 : :
3559 : : static bool
3560 : 15949150 : cgraph_not_only_called_directly_p_1 (cgraph_node *node, void *)
3561 : : {
3562 : 15949150 : return !node->only_called_directly_or_aliased_p ();
3563 : : }
3564 : :
3565 : : /* Return true when function cgraph_node and all its aliases are only called
3566 : : directly.
3567 : : i.e. it is not externally visible, address was not taken and
3568 : : it is not used in any other non-standard way. */
3569 : :
3570 : : bool
3571 : 15882212 : cgraph_node::only_called_directly_p (void)
3572 : : {
3573 : 15882212 : gcc_assert (ultimate_alias_target () == this);
3574 : 15882212 : return !call_for_symbol_and_aliases (cgraph_not_only_called_directly_p_1,
3575 : 15882212 : NULL, true);
3576 : : }
3577 : :
3578 : :
3579 : : /* Collect all callers of NODE. Worker for collect_callers_of_node. */
3580 : :
3581 : : static bool
3582 : 128983 : collect_callers_of_node_1 (cgraph_node *node, void *data)
3583 : : {
3584 : 128983 : vec<cgraph_edge *> *redirect_callers = (vec<cgraph_edge *> *)data;
3585 : 128983 : cgraph_edge *cs;
3586 : 128983 : enum availability avail;
3587 : 128983 : node->ultimate_alias_target (&avail);
3588 : :
3589 : 128983 : if (avail > AVAIL_INTERPOSABLE)
3590 : 448539 : for (cs = node->callers; cs != NULL; cs = cs->next_caller)
3591 : 319556 : if (!cs->indirect_inlining_edge
3592 : 319556 : && !cs->caller->thunk)
3593 : 319510 : redirect_callers->safe_push (cs);
3594 : 128983 : return false;
3595 : : }
3596 : :
3597 : : /* Collect all callers of cgraph_node and its aliases that are known to lead to
3598 : : cgraph_node (i.e. are not overwritable). */
3599 : :
3600 : : auto_vec<cgraph_edge *>
3601 : 127096 : cgraph_node::collect_callers (void)
3602 : : {
3603 : 127096 : auto_vec<cgraph_edge *> redirect_callers;
3604 : 127096 : call_for_symbol_thunks_and_aliases (collect_callers_of_node_1,
3605 : : &redirect_callers, false);
3606 : 127096 : return redirect_callers;
3607 : : }
3608 : :
3609 : :
3610 : : /* Return TRUE if NODE2 a clone of NODE or is equivalent to it. Return
3611 : : optimistically true if this cannot be determined. */
3612 : :
3613 : : static bool
3614 : 34081 : clone_of_p (cgraph_node *node, cgraph_node *node2)
3615 : : {
3616 : 34081 : node = node->ultimate_alias_target ();
3617 : 34081 : node2 = node2->ultimate_alias_target ();
3618 : :
3619 : 34081 : if (node2->clone_of == node
3620 : 4063 : || node2->former_clone_of == node->decl)
3621 : : return true;
3622 : :
3623 : 4125 : if (!node->thunk && !node->former_thunk_p ())
3624 : : {
3625 : : while (node2
3626 : 12002 : && node->decl != node2->decl
3627 : 20004 : && node->decl != node2->former_clone_of)
3628 : 8001 : node2 = node2->clone_of;
3629 : 4001 : return node2 != NULL;
3630 : : }
3631 : :
3632 : : /* There are no virtual clones of thunks so check former_clone_of or if we
3633 : : might have skipped thunks because this adjustments are no longer
3634 : : necessary. */
3635 : 62 : while (node->thunk || node->former_thunk_p ())
3636 : : {
3637 : 62 : if (!thunk_info::get (node)->this_adjusting)
3638 : : return false;
3639 : : /* In case of instrumented expanded thunks, which can have multiple calls
3640 : : in them, we do not know how to continue and just have to be
3641 : : optimistic. The same applies if all calls have already been inlined
3642 : : into the thunk. */
3643 : 62 : if (!node->callees || node->callees->next_callee)
3644 : : return true;
3645 : 59 : node = node->callees->callee->ultimate_alias_target ();
3646 : :
3647 : 59 : clone_info *info = clone_info::get (node2);
3648 : 59 : if (!info || !info->param_adjustments
3649 : 118 : || info->param_adjustments->first_param_intact_p ())
3650 : 0 : return false;
3651 : 59 : if (node2->former_clone_of == node->decl
3652 : 59 : || node2->former_clone_of == node->former_clone_of)
3653 : : return true;
3654 : :
3655 : : cgraph_node *n2 = node2;
3656 : 0 : while (n2 && node->decl != n2->decl)
3657 : 0 : n2 = n2->clone_of;
3658 : 0 : if (n2)
3659 : : return true;
3660 : : }
3661 : :
3662 : : return false;
3663 : : }
3664 : :
3665 : : /* Verify edge count and frequency. */
3666 : :
3667 : : bool
3668 : 197952940 : cgraph_edge::verify_count ()
3669 : : {
3670 : 197952940 : bool error_found = false;
3671 : 197952940 : if (!count.verify ())
3672 : : {
3673 : 0 : error ("caller edge count invalid");
3674 : 0 : error_found = true;
3675 : : }
3676 : 197952940 : return error_found;
3677 : : }
3678 : :
3679 : : /* Switch to THIS_CFUN if needed and print STMT to stderr. */
3680 : : static void
3681 : 0 : cgraph_debug_gimple_stmt (function *this_cfun, gimple *stmt)
3682 : : {
3683 : 0 : bool fndecl_was_null = false;
3684 : : /* debug_gimple_stmt needs correct cfun */
3685 : 0 : if (cfun != this_cfun)
3686 : 0 : set_cfun (this_cfun);
3687 : : /* ...and an actual current_function_decl */
3688 : 0 : if (!current_function_decl)
3689 : : {
3690 : 0 : current_function_decl = this_cfun->decl;
3691 : 0 : fndecl_was_null = true;
3692 : : }
3693 : 0 : debug_gimple_stmt (stmt);
3694 : 0 : if (fndecl_was_null)
3695 : 0 : current_function_decl = NULL;
3696 : 0 : }
3697 : :
3698 : : /* Verify that call graph edge corresponds to DECL from the associated
3699 : : statement. Return true if the verification should fail. */
3700 : :
3701 : : bool
3702 : 98109577 : cgraph_edge::verify_corresponds_to_fndecl (tree decl)
3703 : : {
3704 : 98109577 : cgraph_node *node;
3705 : :
3706 : 98109577 : if (!decl || callee->inlined_to)
3707 : : return false;
3708 : 94220722 : if (symtab->state == LTO_STREAMING)
3709 : : return false;
3710 : 94220722 : node = cgraph_node::get (decl);
3711 : :
3712 : : /* We do not know if a node from a different partition is an alias or what it
3713 : : aliases and therefore cannot do the former_clone_of check reliably. When
3714 : : body_removed is set, we have lost all information about what was alias or
3715 : : thunk of and also cannot proceed. */
3716 : 94220722 : if (!node
3717 : 94124176 : || node->body_removed
3718 : 93370372 : || node->in_other_partition
3719 : 93370372 : || callee->icf_merged
3720 : 93271951 : || callee->in_other_partition)
3721 : : return false;
3722 : :
3723 : 93271951 : node = node->ultimate_alias_target ();
3724 : :
3725 : : /* Optimizers can redirect unreachable calls or calls triggering undefined
3726 : : behavior to __builtin_unreachable or __builtin_unreachable trap. */
3727 : :
3728 : 93271951 : if (fndecl_built_in_p (callee->decl, BUILT_IN_UNREACHABLE,
3729 : : BUILT_IN_UNREACHABLE_TRAP))
3730 : : return false;
3731 : :
3732 : 90517017 : if (callee->former_clone_of != node->decl
3733 : 90515415 : && (node != callee->ultimate_alias_target ())
3734 : 90551098 : && !clone_of_p (node, callee))
3735 : : return true;
3736 : : else
3737 : 90517017 : return false;
3738 : : }
3739 : :
3740 : : /* Disable warnings about missing quoting in GCC diagnostics for
3741 : : the verification errors. Their format strings don't follow GCC
3742 : : diagnostic conventions and the calls are ultimately followed by
3743 : : one to internal_error. */
3744 : : #if __GNUC__ >= 10
3745 : : # pragma GCC diagnostic push
3746 : : # pragma GCC diagnostic ignored "-Wformat-diag"
3747 : : #endif
3748 : :
3749 : : /* Verify consistency of speculative call in NODE corresponding to STMT
3750 : : and LTO_STMT_UID. If INDIRECT is set, assume that it is the indirect
3751 : : edge of call sequence. Return true if error is found.
3752 : :
3753 : : This function is called to every component of indirect call (direct edges,
3754 : : indirect edge and refs). To save duplicated work, do full testing only
3755 : : in that case. */
3756 : : static bool
3757 : 460760 : verify_speculative_call (struct cgraph_node *node, gimple *stmt,
3758 : : unsigned int lto_stmt_uid,
3759 : : struct cgraph_edge *indirect)
3760 : : {
3761 : 460760 : if (indirect == NULL)
3762 : : {
3763 : 454282 : for (indirect = node->indirect_calls; indirect;
3764 : 108658 : indirect = indirect->next_callee)
3765 : 454282 : if (indirect->call_stmt == stmt
3766 : 345742 : && indirect->lto_stmt_uid == lto_stmt_uid)
3767 : : break;
3768 : 345624 : if (!indirect)
3769 : : {
3770 : 0 : error ("missing indirect call in speculative call sequence");
3771 : 0 : return true;
3772 : : }
3773 : 345624 : if (!indirect->speculative)
3774 : : {
3775 : 0 : error ("indirect call in speculative call sequence has no "
3776 : : "speculative flag");
3777 : 0 : return true;
3778 : : }
3779 : : return false;
3780 : : }
3781 : :
3782 : : /* Maximal number of targets. We probably will never want to have more than
3783 : : this. */
3784 : : const unsigned int num = 256;
3785 : : cgraph_edge *direct_calls[num];
3786 : : ipa_ref *refs[num];
3787 : :
3788 : 29589952 : for (unsigned int i = 0; i < num; i++)
3789 : : {
3790 : 29474816 : direct_calls[i] = NULL;
3791 : 29474816 : refs[i] = NULL;
3792 : : }
3793 : :
3794 : 115136 : cgraph_edge *first_call = NULL;
3795 : 115136 : cgraph_edge *prev_call = NULL;
3796 : :
3797 : 663947 : for (cgraph_edge *direct = node->callees; direct;
3798 : 548811 : direct = direct->next_callee)
3799 : 548811 : if (direct->call_stmt == stmt && direct->lto_stmt_uid == lto_stmt_uid)
3800 : : {
3801 : 172812 : if (!first_call)
3802 : 115136 : first_call = direct;
3803 : 172812 : if (prev_call && direct != prev_call->next_callee)
3804 : : {
3805 : 0 : error ("speculative edges are not adjacent");
3806 : 0 : return true;
3807 : : }
3808 : 172812 : prev_call = direct;
3809 : 172812 : if (!direct->speculative)
3810 : : {
3811 : 0 : error ("direct call to %s in speculative call sequence has no "
3812 : 0 : "speculative flag", direct->callee->dump_name ());
3813 : 0 : return true;
3814 : : }
3815 : 172812 : if (direct->speculative_id >= num)
3816 : : {
3817 : 0 : error ("direct call to %s in speculative call sequence has "
3818 : : "speculative_id %i out of range",
3819 : 0 : direct->callee->dump_name (), direct->speculative_id);
3820 : 0 : return true;
3821 : : }
3822 : 172812 : if (direct_calls[direct->speculative_id])
3823 : : {
3824 : 0 : error ("duplicate direct call to %s in speculative call sequence "
3825 : : "with speculative_id %i",
3826 : 0 : direct->callee->dump_name (), direct->speculative_id);
3827 : 0 : return true;
3828 : : }
3829 : 172812 : direct_calls[direct->speculative_id] = direct;
3830 : : }
3831 : :
3832 : 115136 : if (first_call->call_stmt
3833 : 115136 : && first_call != node->get_edge (first_call->call_stmt))
3834 : : {
3835 : 0 : error ("call stmt hash does not point to first direct edge of "
3836 : : "speculative call sequence");
3837 : 0 : return true;
3838 : : }
3839 : :
3840 : : ipa_ref *ref;
3841 : 438114 : for (int i = 0; node->iterate_reference (i, ref); i++)
3842 : 322978 : if (ref->speculative
3843 : 264256 : && ref->stmt == stmt && ref->lto_stmt_uid == lto_stmt_uid)
3844 : : {
3845 : 172812 : if (ref->speculative_id >= num)
3846 : : {
3847 : 0 : error ("direct call to %s in speculative call sequence has "
3848 : : "speculative_id %i out of range",
3849 : 0 : ref->referred->dump_name (), ref->speculative_id);
3850 : 0 : return true;
3851 : : }
3852 : 172812 : if (refs[ref->speculative_id])
3853 : : {
3854 : 0 : error ("duplicate reference %s in speculative call sequence "
3855 : : "with speculative_id %i",
3856 : 0 : ref->referred->dump_name (), ref->speculative_id);
3857 : 0 : return true;
3858 : : }
3859 : 172812 : refs[ref->speculative_id] = ref;
3860 : : }
3861 : :
3862 : : int num_targets = 0;
3863 : 29589952 : for (unsigned int i = 0 ; i < num ; i++)
3864 : : {
3865 : 29474816 : if (refs[i] && !direct_calls[i])
3866 : : {
3867 : 0 : error ("missing direct call for speculation %i", i);
3868 : 0 : return true;
3869 : : }
3870 : 29474816 : if (!refs[i] && direct_calls[i])
3871 : : {
3872 : 0 : error ("missing ref for speculation %i", i);
3873 : 0 : return true;
3874 : : }
3875 : 29474816 : if (refs[i] != NULL)
3876 : 172812 : num_targets++;
3877 : : }
3878 : :
3879 : 115136 : if (num_targets != indirect->num_speculative_call_targets_p ())
3880 : : {
3881 : 0 : error ("number of speculative targets %i mismatched with "
3882 : : "num_speculative_call_targets %i",
3883 : : num_targets,
3884 : : indirect->num_speculative_call_targets_p ());
3885 : 0 : return true;
3886 : : }
3887 : : return false;
3888 : : }
3889 : :
3890 : : /* Verify cgraph nodes of given cgraph node. */
3891 : : DEBUG_FUNCTION void
3892 : 52511846 : cgraph_node::verify_node (void)
3893 : : {
3894 : 52511846 : cgraph_edge *e;
3895 : 52511846 : function *this_cfun = DECL_STRUCT_FUNCTION (decl);
3896 : 52511846 : basic_block this_block;
3897 : 52511846 : gimple_stmt_iterator gsi;
3898 : 52511846 : bool error_found = false;
3899 : 52511846 : int i;
3900 : 52511846 : ipa_ref *ref = NULL;
3901 : :
3902 : 52511846 : if (seen_error ())
3903 : 52511846 : return;
3904 : :
3905 : 52511846 : timevar_push (TV_CGRAPH_VERIFY);
3906 : 52511846 : error_found |= verify_base ();
3907 : 158364711 : for (e = callees; e; e = e->next_callee)
3908 : 105852865 : if (e->aux)
3909 : : {
3910 : 0 : error ("aux field set for edge %s->%s",
3911 : 0 : identifier_to_locale (e->caller->name ()),
3912 : 0 : identifier_to_locale (e->callee->name ()));
3913 : 0 : error_found = true;
3914 : : }
3915 : 52511846 : if (!count.verify ())
3916 : : {
3917 : 0 : error ("cgraph count invalid");
3918 : 0 : error_found = true;
3919 : : }
3920 : 52511846 : if (inlined_to && same_comdat_group)
3921 : : {
3922 : 0 : error ("inline clone in same comdat group list");
3923 : 0 : error_found = true;
3924 : : }
3925 : 52511846 : if (inlined_to && !count.compatible_p (inlined_to->count))
3926 : : {
3927 : 0 : error ("inline clone count is not compatible");
3928 : 0 : count.debug ();
3929 : 0 : inlined_to->count.debug ();
3930 : 0 : error_found = true;
3931 : : }
3932 : 52511846 : if (tp_first_run < 0)
3933 : : {
3934 : 0 : error ("tp_first_run must be non-negative");
3935 : 0 : error_found = true;
3936 : : }
3937 : 52511846 : if (!definition && !in_other_partition && local)
3938 : : {
3939 : 0 : error ("local symbols must be defined");
3940 : 0 : error_found = true;
3941 : : }
3942 : 52511846 : if (inlined_to && externally_visible)
3943 : : {
3944 : 0 : error ("externally visible inline clone");
3945 : 0 : error_found = true;
3946 : : }
3947 : 52511846 : if (inlined_to && address_taken)
3948 : : {
3949 : 0 : error ("inline clone with address taken");
3950 : 0 : error_found = true;
3951 : : }
3952 : 52511846 : if (inlined_to && force_output)
3953 : : {
3954 : 0 : error ("inline clone is forced to output");
3955 : 0 : error_found = true;
3956 : : }
3957 : 52511846 : if (symtab->state != LTO_STREAMING)
3958 : : {
3959 : 52401564 : if (calls_comdat_local && !same_comdat_group)
3960 : : {
3961 : 0 : error ("calls_comdat_local is set outside of a comdat group");
3962 : 0 : error_found = true;
3963 : : }
3964 : 52401564 : if (!inlined_to && calls_comdat_local != check_calls_comdat_local_p ())
3965 : : {
3966 : 0 : error ("invalid calls_comdat_local flag");
3967 : 0 : error_found = true;
3968 : : }
3969 : : }
3970 : 52511846 : if (DECL_IS_MALLOC (decl)
3971 : 52511846 : && !POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
3972 : : {
3973 : 0 : error ("malloc attribute should be used for a function that "
3974 : : "returns a pointer");
3975 : 0 : error_found = true;
3976 : : }
3977 : 52511846 : if (definition
3978 : 35539009 : && externally_visible
3979 : : /* For aliases in lto1 free_lang_data doesn't guarantee preservation
3980 : : of opt_for_fn (decl, flag_semantic_interposition). See PR105399. */
3981 : 19291283 : && (!alias || !in_lto_p)
3982 : 52511846 : && semantic_interposition
3983 : 19288363 : != opt_for_fn (decl, flag_semantic_interposition))
3984 : : {
3985 : 0 : error ("semantic interposition mismatch");
3986 : 0 : error_found = true;
3987 : : }
3988 : 54931924 : for (e = indirect_calls; e; e = e->next_callee)
3989 : : {
3990 : 2420078 : if (e->aux)
3991 : : {
3992 : 0 : error ("aux field set for indirect edge from %s",
3993 : 0 : identifier_to_locale (e->caller->name ()));
3994 : 0 : error_found = true;
3995 : : }
3996 : 2420078 : if (!e->count.compatible_p (count))
3997 : : {
3998 : 0 : error ("edge count is not compatible with function count");
3999 : 0 : e->count.debug ();
4000 : 0 : count.debug ();
4001 : 0 : error_found = true;
4002 : : }
4003 : 2420078 : if (inlined_to && !e->count.compatible_p (inlined_to->count))
4004 : : {
4005 : 0 : error ("edge count is not compatible with inlined to function count");
4006 : 0 : e->count.debug ();
4007 : 0 : count.debug ();
4008 : 0 : error_found = true;
4009 : : }
4010 : 2420078 : if (!e->indirect_unknown_callee
4011 : 2420078 : || !e->indirect_info)
4012 : : {
4013 : 0 : error ("An indirect edge from %s is not marked as indirect or has "
4014 : : "associated indirect_info, the corresponding statement is: ",
4015 : 0 : identifier_to_locale (e->caller->name ()));
4016 : 0 : cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
4017 : 0 : error_found = true;
4018 : : }
4019 : 2420078 : if (e->call_stmt && e->lto_stmt_uid)
4020 : : {
4021 : 0 : error ("edge has both call_stmt and lto_stmt_uid set");
4022 : 0 : error_found = true;
4023 : : }
4024 : : }
4025 : 52511846 : bool check_comdat = comdat_local_p ();
4026 : 142191843 : for (e = callers; e; e = e->next_caller)
4027 : : {
4028 : 89679997 : if (e->verify_count ())
4029 : 0 : error_found = true;
4030 : 89679997 : if (check_comdat
4031 : 89679997 : && !in_same_comdat_group_p (e->caller))
4032 : : {
4033 : 0 : error ("comdat-local function called by %s outside its comdat",
4034 : : identifier_to_locale (e->caller->name ()));
4035 : 0 : error_found = true;
4036 : : }
4037 : 89679997 : if (!e->inline_failed)
4038 : : {
4039 : 8429211 : if (inlined_to
4040 : 8429211 : != (e->caller->inlined_to
4041 : 8429211 : ? e->caller->inlined_to : e->caller))
4042 : : {
4043 : 0 : error ("inlined_to pointer is wrong");
4044 : 0 : error_found = true;
4045 : : }
4046 : 8429211 : if (callers->next_caller)
4047 : : {
4048 : 0 : error ("multiple inline callers");
4049 : 0 : error_found = true;
4050 : : }
4051 : : }
4052 : : else
4053 : 81250786 : if (inlined_to)
4054 : : {
4055 : 0 : error ("inlined_to pointer set for noninline callers");
4056 : 0 : error_found = true;
4057 : : }
4058 : : }
4059 : 158364711 : for (e = callees; e; e = e->next_callee)
4060 : : {
4061 : 105852865 : if (e->verify_count ())
4062 : 0 : error_found = true;
4063 : 105852865 : if (!e->count.compatible_p (count))
4064 : : {
4065 : 0 : error ("edge count is not compatible with function count");
4066 : 0 : e->count.debug ();
4067 : 0 : count.debug ();
4068 : 0 : error_found = true;
4069 : : }
4070 : 105852865 : if (gimple_has_body_p (e->caller->decl)
4071 : 99814479 : && !e->caller->inlined_to
4072 : 91082312 : && !e->speculative
4073 : 91020234 : && !e->callback
4074 : 90997653 : && !e->has_callback
4075 : : /* Optimized out calls are redirected to __builtin_unreachable. */
4076 : 90909608 : && (e->count.nonzero_p ()
4077 : 50825737 : || ! e->callee->decl
4078 : 50825737 : || !fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE,
4079 : : BUILT_IN_UNREACHABLE_TRAP))
4080 : : && count
4081 : 90425492 : == ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (decl))->count
4082 : 196269981 : && (!e->count.ipa_p ()
4083 : 42004326 : && e->count.differs_from_p (gimple_bb (e->call_stmt)->count)))
4084 : : {
4085 : 0 : error ("caller edge count does not match BB count");
4086 : 0 : fprintf (stderr, "edge count: ");
4087 : 0 : e->count.dump (stderr);
4088 : 0 : fprintf (stderr, "\n bb count: ");
4089 : 0 : gimple_bb (e->call_stmt)->count.dump (stderr);
4090 : 0 : fprintf (stderr, "\n");
4091 : 0 : error_found = true;
4092 : : }
4093 : 105852865 : if (e->call_stmt && e->lto_stmt_uid)
4094 : : {
4095 : 0 : error ("edge has both call_stmt and lto_stmt_uid set");
4096 : 0 : error_found = true;
4097 : : }
4098 : 105852865 : if (e->speculative
4099 : 105852865 : && verify_speculative_call (e->caller, e->call_stmt, e->lto_stmt_uid,
4100 : : NULL))
4101 : : error_found = true;
4102 : : }
4103 : 54931924 : for (e = indirect_calls; e; e = e->next_callee)
4104 : : {
4105 : 2420078 : if (e->verify_count ())
4106 : 0 : error_found = true;
4107 : 2420078 : if (gimple_has_body_p (e->caller->decl)
4108 : 2365871 : && !e->caller->inlined_to
4109 : 2097858 : && !e->speculative
4110 : 2060615 : && e->count.ipa_p ()
4111 : : && count
4112 : 758887 : == ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (decl))->count
4113 : 3178961 : && (!e->count.ipa_p ()
4114 : 0 : && e->count.differs_from_p (gimple_bb (e->call_stmt)->count)))
4115 : : {
4116 : 0 : error ("indirect call count does not match BB count");
4117 : 0 : fprintf (stderr, "edge count: ");
4118 : 0 : e->count.dump (stderr);
4119 : 0 : fprintf (stderr, "\n bb count: ");
4120 : 0 : gimple_bb (e->call_stmt)->count.dump (stderr);
4121 : 0 : fprintf (stderr, "\n");
4122 : 0 : error_found = true;
4123 : : }
4124 : 2420078 : if (e->speculative
4125 : 2420078 : && verify_speculative_call (e->caller, e->call_stmt, e->lto_stmt_uid,
4126 : : e))
4127 : : error_found = true;
4128 : : }
4129 : 118386255 : for (i = 0; iterate_reference (i, ref); i++)
4130 : : {
4131 : 65874409 : if (ref->stmt && ref->lto_stmt_uid)
4132 : : {
4133 : 0 : error ("reference has both stmt and lto_stmt_uid set");
4134 : 0 : error_found = true;
4135 : : }
4136 : 65874409 : if (ref->speculative
4137 : 65874409 : && verify_speculative_call (this, ref->stmt,
4138 : : ref->lto_stmt_uid, NULL))
4139 : : error_found = true;
4140 : : }
4141 : :
4142 : 52511846 : if (!callers && inlined_to)
4143 : : {
4144 : 0 : error ("inlined_to pointer is set but no predecessors found");
4145 : 0 : error_found = true;
4146 : : }
4147 : 52511846 : if (inlined_to == this)
4148 : : {
4149 : 0 : error ("inlined_to pointer refers to itself");
4150 : 0 : error_found = true;
4151 : : }
4152 : :
4153 : 52511846 : if (clone_of)
4154 : : {
4155 : 6106688 : cgraph_node *first_clone = clone_of->clones;
4156 : 6106688 : if (first_clone != this)
4157 : : {
4158 : 3139151 : if (prev_sibling_clone->clone_of != clone_of)
4159 : : {
4160 : 0 : error ("cgraph_node has wrong clone_of");
4161 : 0 : error_found = true;
4162 : : }
4163 : : }
4164 : : }
4165 : 52511846 : if (clones)
4166 : : {
4167 : : cgraph_node *n;
4168 : 8558488 : for (n = clones; n; n = n->next_sibling_clone)
4169 : 6908572 : if (n->clone_of != this)
4170 : : break;
4171 : 1649916 : if (n)
4172 : : {
4173 : 0 : error ("cgraph_node has wrong clone list");
4174 : 0 : error_found = true;
4175 : : }
4176 : : }
4177 : 52511846 : if ((prev_sibling_clone || next_sibling_clone) && !clone_of)
4178 : : {
4179 : 0 : error ("cgraph_node is in clone list but it is not clone");
4180 : 0 : error_found = true;
4181 : : }
4182 : 52511846 : if (!prev_sibling_clone && clone_of && clone_of->clones != this)
4183 : : {
4184 : 0 : error ("cgraph_node has wrong prev_clone pointer");
4185 : 0 : error_found = true;
4186 : : }
4187 : 52511846 : if (prev_sibling_clone && prev_sibling_clone->next_sibling_clone != this)
4188 : : {
4189 : 0 : error ("double linked list of clones corrupted");
4190 : 0 : error_found = true;
4191 : : }
4192 : :
4193 : 52511846 : if (analyzed && alias)
4194 : : {
4195 : 1454870 : bool ref_found = false;
4196 : 1454870 : int i;
4197 : 1454870 : ipa_ref *ref = NULL;
4198 : :
4199 : 1454870 : if (callees)
4200 : : {
4201 : 0 : error ("Alias has call edges");
4202 : 0 : error_found = true;
4203 : : }
4204 : 2909740 : for (i = 0; iterate_reference (i, ref); i++)
4205 : 1454870 : if (ref->use != IPA_REF_ALIAS)
4206 : : {
4207 : 0 : error ("Alias has non-alias reference");
4208 : 0 : error_found = true;
4209 : : }
4210 : 1454870 : else if (ref_found)
4211 : : {
4212 : 0 : error ("Alias has more than one alias reference");
4213 : 0 : error_found = true;
4214 : : }
4215 : : else
4216 : : ref_found = true;
4217 : 1454870 : if (!ref_found)
4218 : : {
4219 : 0 : error ("Analyzed alias has no reference");
4220 : 0 : error_found = true;
4221 : : }
4222 : : }
4223 : :
4224 : 52511846 : if (analyzed && thunk)
4225 : : {
4226 : 22180 : if (!callees)
4227 : : {
4228 : 0 : error ("No edge out of thunk node");
4229 : 0 : error_found = true;
4230 : : }
4231 : 22180 : else if (callees->next_callee)
4232 : : {
4233 : 0 : error ("More than one edge out of thunk node");
4234 : 0 : error_found = true;
4235 : : }
4236 : 22180 : if (gimple_has_body_p (decl) && !inlined_to)
4237 : : {
4238 : 0 : error ("Thunk is not supposed to have body");
4239 : 0 : error_found = true;
4240 : : }
4241 : : }
4242 : 35514866 : else if (analyzed && gimple_has_body_p (decl)
4243 : 30691680 : && !TREE_ASM_WRITTEN (decl)
4244 : 30691680 : && (!DECL_EXTERNAL (decl) || inlined_to)
4245 : 82320428 : && !flag_wpa)
4246 : : {
4247 : 29804418 : if ((this_cfun->curr_properties & PROP_assumptions_done) != 0)
4248 : : ;
4249 : 29804310 : else if (this_cfun->cfg)
4250 : : {
4251 : 29804310 : hash_set<gimple *> stmts;
4252 : :
4253 : : /* Reach the trees by walking over the CFG, and note the
4254 : : enclosing basic-blocks in the call edges. */
4255 : 247178590 : FOR_EACH_BB_FN (this_block, this_cfun)
4256 : : {
4257 : 217374280 : for (gsi = gsi_start_phis (this_block);
4258 : 263044232 : !gsi_end_p (gsi); gsi_next (&gsi))
4259 : 45669952 : stmts.add (gsi_stmt (gsi));
4260 : 434748560 : for (gsi = gsi_start_bb (this_block);
4261 : 1312911015 : !gsi_end_p (gsi);
4262 : 1095536735 : gsi_next (&gsi))
4263 : : {
4264 : 1095536735 : gimple *stmt = gsi_stmt (gsi);
4265 : 1095536735 : stmts.add (stmt);
4266 : 1095536735 : if (is_gimple_call (stmt))
4267 : : {
4268 : 105198519 : cgraph_edge *e = get_edge (stmt);
4269 : 105198519 : tree decl = gimple_call_fndecl (stmt);
4270 : 105198519 : if (e)
4271 : : {
4272 : 100348881 : if (e->aux)
4273 : : {
4274 : 0 : error ("shared call_stmt:");
4275 : 0 : cgraph_debug_gimple_stmt (this_cfun, stmt);
4276 : 0 : error_found = true;
4277 : : }
4278 : 100348881 : if (!e->indirect_unknown_callee)
4279 : : {
4280 : : /* Callback edges violate this assertion
4281 : : because their call statement doesn't exist,
4282 : : their associated statement belongs to the
4283 : : callback-dispatching function. */
4284 : 98109577 : if (!e->callback
4285 : 98109577 : && e->verify_corresponds_to_fndecl (decl))
4286 : : {
4287 : 0 : error ("edge points to wrong declaration:");
4288 : 0 : debug_tree (e->callee->decl);
4289 : 0 : fprintf (stderr," Instead of:");
4290 : 0 : debug_tree (decl);
4291 : 0 : error_found = true;
4292 : : }
4293 : : }
4294 : 2239304 : else if (decl)
4295 : : {
4296 : 0 : error ("an indirect edge with unknown callee "
4297 : : "corresponding to a call_stmt with "
4298 : : "a known declaration:");
4299 : 0 : error_found = true;
4300 : 0 : cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
4301 : : }
4302 : 100348881 : e->aux = (void *)1;
4303 : : }
4304 : 4849638 : else if (decl)
4305 : : {
4306 : 0 : error ("missing callgraph edge for call stmt:");
4307 : 0 : cgraph_debug_gimple_stmt (this_cfun, stmt);
4308 : 0 : error_found = true;
4309 : : }
4310 : : }
4311 : : }
4312 : : }
4313 : 100174273 : for (i = 0; iterate_reference (i, ref); i++)
4314 : 61348129 : if (ref->stmt && !stmts.contains (ref->stmt))
4315 : : {
4316 : 0 : error ("reference to dead statement");
4317 : 0 : cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
4318 : 0 : error_found = true;
4319 : : }
4320 : 29804310 : }
4321 : : else
4322 : : /* No CFG available?! */
4323 : 0 : gcc_unreachable ();
4324 : :
4325 : 127987732 : for (e = callees; e; e = e->next_callee)
4326 : : {
4327 : 98183314 : if (!e->callback && e->callback_id)
4328 : : {
4329 : 0 : error ("non-callback edge has callback_id set");
4330 : 0 : error_found = true;
4331 : : }
4332 : :
4333 : 98183314 : if (e->callback && e->has_callback)
4334 : : {
4335 : 0 : error ("edge has both callback and has_callback set");
4336 : 0 : error_found = true;
4337 : : }
4338 : :
4339 : 98183314 : if (e->callback)
4340 : : {
4341 : 24067 : if (!e->get_callback_carrying_edge ())
4342 : : {
4343 : 0 : error ("callback edge %s->%s has no callback-carrying",
4344 : 0 : identifier_to_locale (e->caller->name ()),
4345 : 0 : identifier_to_locale (e->callee->name ()));
4346 : 0 : error_found = true;
4347 : : }
4348 : : }
4349 : :
4350 : 98183314 : if (e->has_callback
4351 : 98183314 : && !callback_is_special_cased (e->callee->decl, e->call_stmt))
4352 : : {
4353 : 84400 : int ncallbacks = 0;
4354 : 84400 : int nfound_edges = 0;
4355 : 84400 : for (tree cb = lookup_attribute (CALLBACK_ATTR_IDENT, DECL_ATTRIBUTES (
4356 : : e->callee->decl));
4357 : 168800 : cb; cb = lookup_attribute (CALLBACK_ATTR_IDENT, TREE_CHAIN (cb)),
4358 : : ncallbacks++)
4359 : : ;
4360 : 774296 : for (cgraph_edge *cbe = callees; cbe; cbe = cbe->next_callee)
4361 : : {
4362 : 689896 : if (cbe->callback && cbe->call_stmt == e->call_stmt
4363 : 20709 : && cbe->lto_stmt_uid == e->lto_stmt_uid)
4364 : : {
4365 : 20709 : nfound_edges++;
4366 : : }
4367 : : }
4368 : 84400 : if (ncallbacks < nfound_edges)
4369 : : {
4370 : 0 : error ("callback edge %s->%s callback edge count mismatch, "
4371 : : "expected at most %d, found %d",
4372 : 0 : identifier_to_locale (e->caller->name ()),
4373 : 0 : identifier_to_locale (e->callee->name ()), ncallbacks,
4374 : : nfound_edges);
4375 : : }
4376 : : }
4377 : :
4378 : 98183314 : if (!e->aux && !e->speculative && !e->callback && !e->has_callback)
4379 : : {
4380 : 0 : error ("edge %s->%s has no corresponding call_stmt",
4381 : 0 : identifier_to_locale (e->caller->name ()),
4382 : 0 : identifier_to_locale (e->callee->name ()));
4383 : 0 : cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
4384 : 0 : error_found = true;
4385 : : }
4386 : 98183314 : e->aux = 0;
4387 : : }
4388 : 32145422 : for (e = indirect_calls; e; e = e->next_callee)
4389 : : {
4390 : 2341004 : if (!e->aux && !e->speculative)
4391 : : {
4392 : 0 : error ("an indirect edge from %s has no corresponding call_stmt",
4393 : 0 : identifier_to_locale (e->caller->name ()));
4394 : 0 : cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
4395 : 0 : error_found = true;
4396 : : }
4397 : 2341004 : e->aux = 0;
4398 : : }
4399 : : }
4400 : :
4401 : 52511846 : if (nested_function_info *info = nested_function_info::get (this))
4402 : : {
4403 : 0 : if (info->nested != NULL)
4404 : : {
4405 : 0 : for (cgraph_node *n = info->nested; n != NULL;
4406 : 0 : n = next_nested_function (n))
4407 : : {
4408 : 0 : nested_function_info *ninfo = nested_function_info::get (n);
4409 : 0 : if (ninfo->origin == NULL)
4410 : : {
4411 : 0 : error ("missing origin for a node in a nested list");
4412 : 0 : error_found = true;
4413 : : }
4414 : 0 : else if (ninfo->origin != this)
4415 : : {
4416 : 0 : error ("origin points to a different parent");
4417 : 0 : error_found = true;
4418 : 0 : break;
4419 : : }
4420 : : }
4421 : : }
4422 : 0 : if (info->next_nested != NULL && info->origin == NULL)
4423 : : {
4424 : 0 : error ("missing origin for a node in a nested list");
4425 : 0 : error_found = true;
4426 : : }
4427 : : }
4428 : :
4429 : 52511846 : if (error_found)
4430 : : {
4431 : 0 : dump (stderr);
4432 : 0 : internal_error ("verify_cgraph_node failed");
4433 : : }
4434 : 52511846 : timevar_pop (TV_CGRAPH_VERIFY);
4435 : : }
4436 : :
4437 : : /* Verify whole cgraph structure. */
4438 : : DEBUG_FUNCTION void
4439 : 862 : cgraph_node::verify_cgraph_nodes (void)
4440 : : {
4441 : 862 : cgraph_node *node;
4442 : :
4443 : 862 : if (seen_error ())
4444 : : return;
4445 : :
4446 : 10462 : FOR_EACH_FUNCTION (node)
4447 : 4393 : node->verify ();
4448 : : }
4449 : :
4450 : : #if __GNUC__ >= 10
4451 : : # pragma GCC diagnostic pop
4452 : : #endif
4453 : :
4454 : : /* Walk the alias chain to return the function cgraph_node is alias of.
4455 : : Walk through thunks, too.
4456 : : When AVAILABILITY is non-NULL, get minimal availability in the chain.
4457 : : When REF is non-NULL, assume that reference happens in symbol REF
4458 : : when determining the availability. */
4459 : :
4460 : : cgraph_node *
4461 : 131914603 : cgraph_node::function_symbol (enum availability *availability,
4462 : : struct symtab_node *ref)
4463 : : {
4464 : 131914603 : cgraph_node *node = ultimate_alias_target (availability, ref);
4465 : :
4466 : 263834243 : while (node->thunk)
4467 : : {
4468 : 5037 : enum availability a;
4469 : :
4470 : 5037 : ref = node;
4471 : 5037 : node = node->callees->callee;
4472 : 8984 : node = node->ultimate_alias_target (availability ? &a : NULL, ref);
4473 : 5037 : if (availability && a < *availability)
4474 : 38 : *availability = a;
4475 : : }
4476 : 131914603 : return node;
4477 : : }
4478 : :
4479 : : /* Walk the alias chain to return the function cgraph_node is alias of.
4480 : : Walk through non virtual thunks, too. Thus we return either a function
4481 : : or a virtual thunk node.
4482 : : When AVAILABILITY is non-NULL, get minimal availability in the chain.
4483 : : When REF is non-NULL, assume that reference happens in symbol REF
4484 : : when determining the availability. */
4485 : :
4486 : : cgraph_node *
4487 : 33258148 : cgraph_node::function_or_virtual_thunk_symbol
4488 : : (enum availability *availability,
4489 : : struct symtab_node *ref)
4490 : : {
4491 : 33258148 : cgraph_node *node = ultimate_alias_target (availability, ref);
4492 : :
4493 : 66517352 : while (node->thunk && !thunk_info::get (node)->virtual_offset_p)
4494 : : {
4495 : 1056 : enum availability a;
4496 : :
4497 : 1056 : ref = node;
4498 : 1056 : node = node->callees->callee;
4499 : 1056 : node = node->ultimate_alias_target (availability ? &a : NULL, ref);
4500 : 1056 : if (availability && a < *availability)
4501 : 320 : *availability = a;
4502 : : }
4503 : 33258148 : return node;
4504 : : }
4505 : :
4506 : : /* When doing LTO, read cgraph_node's body from disk if it is not already
4507 : : present. Also perform any necessary clone materializations. */
4508 : :
4509 : : bool
4510 : 6267506 : cgraph_node::get_untransformed_body ()
4511 : : {
4512 : 6267506 : lto_file_decl_data *file_data;
4513 : 6267506 : const char *data, *name;
4514 : 6267506 : size_t len;
4515 : 6267506 : tree decl = this->decl;
4516 : :
4517 : : /* See if there is clone to be materialized.
4518 : : (inline clones does not need materialization, but we can be seeing
4519 : : an inline clone of real clone). */
4520 : 6267506 : cgraph_node *p = this;
4521 : 8999753 : for (cgraph_node *c = clone_of; c; c = c->clone_of)
4522 : : {
4523 : 2732247 : if (c->decl != decl)
4524 : 125961 : p->materialize_clone ();
4525 : 2732247 : p = c;
4526 : : }
4527 : :
4528 : : /* Check if body is already there. Either we have gimple body or
4529 : : the function is thunk and in that case we set DECL_ARGUMENTS. */
4530 : 6267506 : if (DECL_ARGUMENTS (decl) || gimple_has_body_p (decl))
4531 : 6183378 : return false;
4532 : :
4533 : 168256 : gcc_assert (in_lto_p && !DECL_RESULT (decl));
4534 : :
4535 : 84128 : timevar_push (TV_IPA_LTO_GIMPLE_IN);
4536 : :
4537 : 84128 : file_data = lto_file_data;
4538 : 84128 : name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4539 : :
4540 : : /* We may have renamed the declaration, e.g., a static function. */
4541 : 84128 : name = lto_get_decl_name_mapping (file_data, name);
4542 : 84128 : struct lto_in_decl_state *decl_state
4543 : 84128 : = lto_get_function_in_decl_state (file_data, decl);
4544 : :
4545 : 84128 : cgraph_node *origin = this;
4546 : 168430 : while (origin->clone_of)
4547 : : origin = origin->clone_of;
4548 : :
4549 : 84128 : int stream_order = origin->order - file_data->order_base;
4550 : 168256 : data = lto_get_section_data (file_data, LTO_section_function_body,
4551 : : name, stream_order, &len,
4552 : 84128 : decl_state->compressed);
4553 : 84128 : if (!data)
4554 : 0 : fatal_error (input_location, "%s: section %s.%d is missing",
4555 : : file_data->file_name, name, stream_order);
4556 : :
4557 : 84128 : gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
4558 : :
4559 : 84128 : if (!quiet_flag)
4560 : 0 : fprintf (stderr, " in:%s", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
4561 : 84128 : lto_input_function_body (file_data, this, data);
4562 : 84128 : lto_stats.num_function_bodies++;
4563 : 84128 : lto_free_section_data (file_data, LTO_section_function_body, name,
4564 : 84128 : data, len, decl_state->compressed);
4565 : 84128 : lto_free_function_in_decl_state_for_node (this);
4566 : : /* Keep lto file data so ipa-inline-analysis knows about cross module
4567 : : inlining. */
4568 : :
4569 : 84128 : timevar_pop (TV_IPA_LTO_GIMPLE_IN);
4570 : :
4571 : 84128 : return true;
4572 : : }
4573 : :
4574 : : /* Prepare function body. When doing LTO, read cgraph_node's body from disk
4575 : : if it is not already present. When some IPA transformations are scheduled,
4576 : : apply them. */
4577 : :
4578 : : bool
4579 : 28521 : cgraph_node::get_body (void)
4580 : : {
4581 : 28521 : bool updated;
4582 : :
4583 : 28521 : updated = get_untransformed_body ();
4584 : :
4585 : : /* Getting transformed body makes no sense for inline clones;
4586 : : we should never use this on real clones because they are materialized
4587 : : early.
4588 : : TODO: Materializing clones here will likely lead to smaller LTRANS
4589 : : footprint. */
4590 : 28521 : gcc_assert (!inlined_to && !clone_of);
4591 : 28521 : if (ipa_transforms_to_apply.exists ())
4592 : : {
4593 : 12208 : opt_pass *saved_current_pass = current_pass;
4594 : 12208 : FILE *saved_dump_file = dump_file;
4595 : 12208 : const char *saved_dump_file_name = dump_file_name;
4596 : 12208 : dump_flags_t saved_dump_flags = dump_flags;
4597 : 12208 : dump_file_name = NULL;
4598 : 12208 : set_dump_file (NULL);
4599 : :
4600 : 12208 : push_cfun (DECL_STRUCT_FUNCTION (decl));
4601 : :
4602 : 12208 : update_ssa (TODO_update_ssa_only_virtuals);
4603 : 12208 : execute_all_ipa_transforms (true);
4604 : 12208 : cgraph_edge::rebuild_edges ();
4605 : 12208 : free_dominance_info (CDI_DOMINATORS);
4606 : 12208 : free_dominance_info (CDI_POST_DOMINATORS);
4607 : 12208 : pop_cfun ();
4608 : 12208 : updated = true;
4609 : :
4610 : 12208 : current_pass = saved_current_pass;
4611 : 12208 : set_dump_file (saved_dump_file);
4612 : 12208 : dump_file_name = saved_dump_file_name;
4613 : 12208 : dump_flags = saved_dump_flags;
4614 : : }
4615 : 28521 : return updated;
4616 : : }
4617 : :
4618 : : /* Return the DECL_STRUCT_FUNCTION of the function. */
4619 : :
4620 : : struct function *
4621 : 221392 : cgraph_node::get_fun () const
4622 : : {
4623 : 221392 : const cgraph_node *node = this;
4624 : 221392 : struct function *fun = DECL_STRUCT_FUNCTION (node->decl);
4625 : :
4626 : 221392 : while (!fun && node->clone_of)
4627 : : {
4628 : 0 : node = node->clone_of;
4629 : 0 : fun = DECL_STRUCT_FUNCTION (node->decl);
4630 : : }
4631 : :
4632 : 221392 : return fun;
4633 : : }
4634 : :
4635 : : /* Reset all state within cgraph.cc so that we can rerun the compiler
4636 : : within the same process. For use by toplev::finalize. */
4637 : :
4638 : : void
4639 : 260333 : cgraph_cc_finalize (void)
4640 : : {
4641 : 260333 : nested_function_info::release ();
4642 : 260333 : thunk_info::release ();
4643 : 260333 : clone_info::release ();
4644 : 260333 : symtab = NULL;
4645 : :
4646 : 260333 : x_cgraph_nodes_queue = NULL;
4647 : :
4648 : 260333 : cgraph_fnver_htab = NULL;
4649 : 260333 : version_info_node = NULL;
4650 : 260333 : }
4651 : :
4652 : : /* A worker for call_for_symbol_and_aliases. */
4653 : :
4654 : : bool
4655 : 737270 : cgraph_node::call_for_symbol_and_aliases_1 (bool (*callback) (cgraph_node *,
4656 : : void *),
4657 : : void *data,
4658 : : bool include_overwritable)
4659 : : {
4660 : 737270 : ipa_ref *ref;
4661 : 1427933 : FOR_EACH_ALIAS (this, ref)
4662 : : {
4663 : 790405 : cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
4664 : 790405 : if (include_overwritable
4665 : 790405 : || alias->get_availability () > AVAIL_INTERPOSABLE)
4666 : 790405 : if (alias->call_for_symbol_and_aliases (callback, data,
4667 : : include_overwritable))
4668 : : return true;
4669 : : }
4670 : : return false;
4671 : : }
4672 : :
4673 : : /* Return true if NODE has thunk. */
4674 : :
4675 : : bool
4676 : 39111 : cgraph_node::has_thunk_p (cgraph_node *node, void *)
4677 : : {
4678 : 73578 : for (cgraph_edge *e = node->callers; e; e = e->next_caller)
4679 : 34467 : if (e->caller->thunk)
4680 : : return true;
4681 : : return false;
4682 : : }
4683 : :
4684 : : /* Expected frequency of executions within the function. */
4685 : :
4686 : : sreal
4687 : 206254228 : cgraph_edge::sreal_frequency ()
4688 : : {
4689 : 206254228 : return count.to_sreal_scale (caller->inlined_to
4690 : 206254228 : ? caller->inlined_to->count
4691 : 206254228 : : caller->count);
4692 : : }
4693 : :
4694 : : /* Expected frequency of executions within the function.
4695 : : If edge is speculative, sum all its indirect targets. */
4696 : :
4697 : : sreal
4698 : 934 : cgraph_edge::combined_sreal_frequency ()
4699 : : {
4700 : 934 : if (!speculative)
4701 : 867 : return sreal_frequency ();
4702 : 67 : cgraph_edge *e = this;
4703 : 67 : if (e->callee)
4704 : 0 : e = e->speculative_call_indirect_edge ();
4705 : 67 : sreal sum = e->sreal_frequency ();
4706 : 67 : for (e = e->first_speculative_call_target ();
4707 : 141 : e;
4708 : 74 : e = e->next_speculative_call_target ())
4709 : 74 : sum += e->sreal_frequency ();
4710 : 67 : return sum;
4711 : : }
4712 : :
4713 : :
4714 : : /* During LTO stream in this can be used to check whether call can possibly
4715 : : be internal to the current translation unit. */
4716 : :
4717 : : bool
4718 : 478155 : cgraph_edge::possibly_call_in_translation_unit_p (void)
4719 : : {
4720 : 478155 : gcc_checking_assert (in_lto_p && caller->prevailing_p ());
4721 : :
4722 : : /* While incremental linking we may end up getting function body later. */
4723 : 478155 : if (flag_incremental_link == INCREMENTAL_LINK_LTO)
4724 : : return true;
4725 : :
4726 : : /* We may be smarter here and avoid streaming in indirect calls we can't
4727 : : track, but that would require arranging streaming the indirect call
4728 : : summary first. */
4729 : 477926 : if (!callee)
4730 : : return true;
4731 : :
4732 : : /* If callee is local to the original translation unit, it will be
4733 : : defined. */
4734 : 475210 : if (!TREE_PUBLIC (callee->decl) && !DECL_EXTERNAL (callee->decl))
4735 : : return true;
4736 : :
4737 : : /* Otherwise we need to lookup prevailing symbol (symbol table is not merged,
4738 : : yet) and see if it is a definition. In fact we may also resolve aliases,
4739 : : but that is probably not too important. */
4740 : 479280 : symtab_node *node = callee;
4741 : 479280 : for (int n = 10; node->previous_sharing_asm_name && n ; n--)
4742 : 10105 : node = node->previous_sharing_asm_name;
4743 : 469175 : if (node->previous_sharing_asm_name)
4744 : 234 : node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (callee->decl));
4745 : 469175 : gcc_assert (TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl));
4746 : 469175 : return node->get_availability () >= AVAIL_INTERPOSABLE;
4747 : : }
4748 : :
4749 : : /* Return num_speculative_targets of this edge. */
4750 : :
4751 : : int
4752 : 185547 : cgraph_edge::num_speculative_call_targets_p (void)
4753 : : {
4754 : 185547 : return indirect_info ? indirect_info->num_speculative_call_targets : 0;
4755 : : }
4756 : :
4757 : : /* Check if function calls comdat local. This is used to recompute
4758 : : calls_comdat_local flag after function transformations. */
4759 : : bool
4760 : 49280531 : cgraph_node::check_calls_comdat_local_p ()
4761 : : {
4762 : 158041003 : for (cgraph_edge *e = callees; e; e = e->next_callee)
4763 : 3850717 : if (e->inline_failed
4764 : 117565661 : ? e->callee->comdat_local_p ()
4765 : 3850717 : : e->callee->check_calls_comdat_local_p ())
4766 : 50836 : return true;
4767 : : return false;
4768 : : }
4769 : :
4770 : : /* Return true if this node represents a former, i.e. an expanded, thunk. */
4771 : :
4772 : : bool
4773 : 5047006 : cgraph_node::former_thunk_p (void)
4774 : : {
4775 : 5047006 : if (thunk)
4776 : : return false;
4777 : 5047006 : thunk_info *i = thunk_info::get (this);
4778 : 5047006 : if (!i)
4779 : : return false;
4780 : 67 : gcc_checking_assert (i->fixed_offset || i->virtual_offset_p
4781 : : || i->indirect_offset);
4782 : : return true;
4783 : : }
4784 : :
4785 : : /* A stashed copy of "symtab" for use by selftest::symbol_table_test.
4786 : : This needs to be a global so that it can be a GC root, and thus
4787 : : prevent the stashed copy from being garbage-collected if the GC runs
4788 : : during a symbol_table_test. */
4789 : :
4790 : : symbol_table *saved_symtab;
4791 : :
4792 : : #if CHECKING_P
4793 : :
4794 : : namespace selftest {
4795 : :
4796 : : /* class selftest::symbol_table_test. */
4797 : :
4798 : : /* Constructor. Store the old value of symtab, and create a new one. */
4799 : :
4800 : 64 : symbol_table_test::symbol_table_test ()
4801 : : {
4802 : 64 : gcc_assert (saved_symtab == NULL);
4803 : 64 : saved_symtab = symtab;
4804 : 64 : symtab = new (ggc_alloc<symbol_table> ()) symbol_table ();
4805 : 64 : }
4806 : :
4807 : : /* Destructor. Restore the old value of symtab. */
4808 : :
4809 : 64 : symbol_table_test::~symbol_table_test ()
4810 : : {
4811 : 64 : gcc_assert (saved_symtab != NULL);
4812 : 64 : symtab = saved_symtab;
4813 : 64 : saved_symtab = NULL;
4814 : 64 : }
4815 : :
4816 : : /* Verify that symbol_table_test works. */
4817 : :
4818 : : static void
4819 : 4 : test_symbol_table_test ()
4820 : : {
4821 : : /* Simulate running two selftests involving symbol tables. */
4822 : 12 : for (int i = 0; i < 2; i++)
4823 : : {
4824 : 8 : symbol_table_test stt;
4825 : 8 : tree test_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
4826 : : get_identifier ("test_decl"),
4827 : : build_function_type_list (void_type_node,
4828 : : NULL_TREE));
4829 : 8 : cgraph_node *node = cgraph_node::get_create (test_decl);
4830 : 8 : gcc_assert (node);
4831 : :
4832 : : /* Verify that the node has order 0 on both iterations,
4833 : : and thus that nodes have predictable dump names in selftests. */
4834 : 8 : ASSERT_EQ (node->order, 0);
4835 : 8 : ASSERT_STREQ (node->dump_name (), "test_decl/1");
4836 : 8 : }
4837 : 4 : }
4838 : :
4839 : : /* Run all of the selftests within this file. */
4840 : :
4841 : : void
4842 : 4 : cgraph_cc_tests ()
4843 : : {
4844 : 4 : test_symbol_table_test ();
4845 : 4 : }
4846 : :
4847 : : } // namespace selftest
4848 : :
4849 : : #endif /* CHECKING_P */
4850 : :
4851 : : #include "gt-cgraph.h"
|