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 : 287132 : function_version_hasher::hash (cgraph_function_version_info *ptr)
130 : : {
131 : 287132 : int uid = ptr->this_node->get_uid ();
132 : 287132 : return (hashval_t)(uid);
133 : : }
134 : :
135 : : /* eq function for cgraph_fnver_htab. */
136 : : bool
137 : 260208 : function_version_hasher::equal (cgraph_function_version_info *n1,
138 : : cgraph_function_version_info *n2)
139 : : {
140 : 260208 : 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 : 5002493 : symtab_node::address_can_be_compared_p ()
151 : : {
152 : : /* Address of virtual tables and functions is never compared. */
153 : 5002493 : if (DECL_VIRTUAL_P (decl))
154 : : return false;
155 : : /* Address of C++ cdtors is never compared. */
156 : 4912910 : if (is_a <cgraph_node *> (this)
157 : 536243 : && (DECL_CXX_CONSTRUCTOR_P (decl)
158 : 531999 : || 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 : 4907265 : if (is_a <varpool_node *> (this)
163 : 4376667 : && (DECL_IN_CONSTANT_POOL (decl)
164 : 4376664 : || ((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 : 114241159 : cgraph_node::function_version (void)
173 : : {
174 : 114241159 : cgraph_function_version_info key;
175 : 114241159 : key.this_node = this;
176 : :
177 : 114241159 : if (cgraph_fnver_htab == NULL)
178 : : return NULL;
179 : :
180 : 46133 : 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 : 1622539 : cgraph_node::apply_scale (profile_count num, profile_count den)
266 : : {
267 : 1736854 : if (num == den && !(num == profile_count::zero ()))
268 : 112362 : return;
269 : :
270 : 2713469 : for (cgraph_edge *e = callees; e; e = e->next_callee)
271 : : {
272 : 1203292 : if (!e->inline_failed)
273 : 154309 : e->callee->apply_scale (num, den);
274 : 1203292 : e->count = e->count.apply_scale (num, den);
275 : : }
276 : 1540064 : for (cgraph_edge *e = indirect_calls; e; e = e->next_callee)
277 : 29887 : e->count = e->count.apply_scale (num, den);
278 : 1510177 : 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 : 1520 : cgraph_node::insert_new_function_version (void)
322 : : {
323 : 1520 : version_info_node = NULL;
324 : 1520 : version_info_node = ggc_cleared_alloc<cgraph_function_version_info> ();
325 : 1520 : version_info_node->this_node = this;
326 : 1520 : version_info_node->assembler_name = DECL_ASSEMBLER_NAME (this->decl);
327 : :
328 : 1520 : if (cgraph_fnver_htab == NULL)
329 : 197 : cgraph_fnver_htab = hash_table<function_version_hasher>::create_ggc (2);
330 : :
331 : 1520 : *cgraph_fnver_htab->find_slot (version_info_node, INSERT)
332 : 1520 : = version_info_node;
333 : 1520 : return version_info_node;
334 : : }
335 : :
336 : : /* Remove the cgraph_function_version_info node given by DECL_V. */
337 : : void
338 : 94225219 : cgraph_node::delete_function_version (cgraph_function_version_info *decl_v)
339 : : {
340 : 94225219 : 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 : 8827 : cgraph_node::add_function_version (cgraph_function_version_info *fn_v,
376 : : tree decl)
377 : : {
378 : 8827 : cgraph_node *decl_node = cgraph_node::get_create (decl);
379 : 8827 : cgraph_function_version_info *decl_v = NULL;
380 : :
381 : 8827 : gcc_assert (decl_node != NULL);
382 : :
383 : 8827 : decl_v = decl_node->function_version ();
384 : :
385 : : /* If the nodes are already linked, skip. */
386 : 8827 : 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 : 1054 : gcc_assert (fn_v);
394 : :
395 : : /* Go to start of the FMV structure. */
396 : 5434 : while (fn_v->prev)
397 : : fn_v = fn_v->prev;
398 : :
399 : 1054 : cgraph_function_version_info *insert_point_before = NULL;
400 : 1054 : 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 : 1054 : if (!is_function_default_version (decl))
405 : : while (insert_point_after
406 : 9837 : && (targetm.compare_version_priority
407 : 4727 : (decl, insert_point_after->this_node->decl) > 0
408 : 693 : || is_function_default_version
409 : 693 : (insert_point_after->this_node->decl)
410 : 572 : || lookup_attribute
411 : 572 : ("target_clones",
412 : 572 : DECL_ATTRIBUTES (insert_point_after->this_node->decl))))
413 : : {
414 : 4155 : insert_point_before = insert_point_after;
415 : 4155 : insert_point_after = insert_point_after->next;
416 : : }
417 : :
418 : 1054 : decl_v->prev = insert_point_before;
419 : 1054 : decl_v->next= insert_point_after;
420 : :
421 : 1054 : if (insert_point_before)
422 : 919 : insert_point_before->next = decl_v;
423 : 1054 : if (insert_point_after)
424 : 671 : insert_point_after->prev = decl_v;
425 : : }
426 : :
427 : : /* Initialize callgraph dump file. */
428 : :
429 : : void
430 : 304510 : symbol_table::initialize (void)
431 : : {
432 : 304510 : if (!dump_file)
433 : 304508 : dump_file = dump_begin (TDI_cgraph, NULL);
434 : :
435 : 304510 : if (!ipa_clones_dump_file)
436 : 304510 : ipa_clones_dump_file = dump_begin (TDI_clones, NULL);
437 : 304510 : }
438 : :
439 : : /* Allocate new callgraph node and insert it into basic data structures. */
440 : :
441 : : cgraph_node *
442 : 97950467 : symbol_table::create_empty (void)
443 : : {
444 : 97950467 : cgraph_count++;
445 : 97950467 : 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 : 1896541 : symbol_table::add_edge_removal_hook (cgraph_edge_hook hook, void *data)
451 : : {
452 : 1896541 : cgraph_edge_hook_list *entry;
453 : 3793082 : cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
454 : :
455 : 1896541 : entry = (cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
456 : 1896541 : entry->hook = hook;
457 : 1896541 : entry->data = data;
458 : 1896541 : entry->next = NULL;
459 : 6556074 : while (*ptr)
460 : 4659533 : ptr = &(*ptr)->next;
461 : 1896541 : *ptr = entry;
462 : 1896541 : return entry;
463 : : }
464 : :
465 : : /* Remove ENTRY from the list of hooks called on removing edges. */
466 : : void
467 : 1896527 : symbol_table::remove_edge_removal_hook (cgraph_edge_hook_list *entry)
468 : : {
469 : 1896527 : cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
470 : :
471 : 5168082 : while (*ptr != entry)
472 : 3271555 : ptr = &(*ptr)->next;
473 : 1896527 : *ptr = entry->next;
474 : 1896527 : free (entry);
475 : 1896527 : }
476 : :
477 : : /* Call all edge removal hooks. */
478 : : void
479 : 44323012 : symbol_table::call_edge_removal_hooks (cgraph_edge *e)
480 : : {
481 : 44323012 : cgraph_edge_hook_list *entry = m_first_edge_removal_hook;
482 : 71975310 : while (entry)
483 : : {
484 : 27652298 : entry->hook (e, entry->data);
485 : 27652298 : entry = entry->next;
486 : : }
487 : 44323012 : }
488 : :
489 : : /* Register HOOK to be called with DATA on each removed node. */
490 : : cgraph_node_hook_list *
491 : 8051871 : symbol_table::add_cgraph_removal_hook (cgraph_node_hook hook, void *data)
492 : : {
493 : 8051871 : cgraph_node_hook_list *entry;
494 : 16103742 : cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
495 : :
496 : 8051871 : entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
497 : 8051871 : entry->hook = hook;
498 : 8051871 : entry->data = data;
499 : 8051871 : entry->next = NULL;
500 : 42090828 : while (*ptr)
501 : 34038957 : ptr = &(*ptr)->next;
502 : 8051871 : *ptr = entry;
503 : 8051871 : return entry;
504 : : }
505 : :
506 : : /* Remove ENTRY from the list of hooks called on removing nodes. */
507 : : void
508 : 7944651 : symbol_table::remove_cgraph_removal_hook (cgraph_node_hook_list *entry)
509 : : {
510 : 7944651 : cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
511 : :
512 : 37579994 : while (*ptr != entry)
513 : 29635343 : ptr = &(*ptr)->next;
514 : 7944651 : *ptr = entry->next;
515 : 7944651 : free (entry);
516 : 7944651 : }
517 : :
518 : : /* Call all node removal hooks. */
519 : : void
520 : 94257529 : symbol_table::call_cgraph_removal_hooks (cgraph_node *node)
521 : : {
522 : 94257529 : cgraph_node_hook_list *entry = m_first_cgraph_removal_hook;
523 : 134228562 : while (entry)
524 : : {
525 : 39971033 : entry->hook (node, entry->data);
526 : 39971033 : entry = entry->next;
527 : : }
528 : 94257529 : }
529 : :
530 : : /* Call all node removal hooks. */
531 : : void
532 : 118573 : symbol_table::call_cgraph_insertion_hooks (cgraph_node *node)
533 : : {
534 : 118573 : cgraph_node_hook_list *entry = m_first_cgraph_insertion_hook;
535 : 357429 : while (entry)
536 : : {
537 : 238856 : entry->hook (node, entry->data);
538 : 238856 : entry = entry->next;
539 : : }
540 : 118573 : }
541 : :
542 : :
543 : : /* Register HOOK to be called with DATA on each inserted node. */
544 : : cgraph_node_hook_list *
545 : 8536865 : symbol_table::add_cgraph_insertion_hook (cgraph_node_hook hook, void *data)
546 : : {
547 : 8536865 : cgraph_node_hook_list *entry;
548 : 17073730 : cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
549 : :
550 : 8536865 : entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
551 : 8536865 : entry->hook = hook;
552 : 8536865 : entry->data = data;
553 : 8536865 : entry->next = NULL;
554 : 25273515 : while (*ptr)
555 : 16736650 : ptr = &(*ptr)->next;
556 : 8536865 : *ptr = entry;
557 : 8536865 : return entry;
558 : : }
559 : :
560 : : /* Remove ENTRY from the list of hooks called on inserted nodes. */
561 : : void
562 : 8390409 : symbol_table::remove_cgraph_insertion_hook (cgraph_node_hook_list *entry)
563 : : {
564 : 8390409 : cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
565 : :
566 : 23161956 : while (*ptr != entry)
567 : 14771547 : ptr = &(*ptr)->next;
568 : 8390409 : *ptr = entry->next;
569 : 8390409 : free (entry);
570 : 8390409 : }
571 : :
572 : : /* Register HOOK to be called with DATA on each duplicated edge. */
573 : : cgraph_2edge_hook_list *
574 : 1665741 : symbol_table::add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
575 : : {
576 : 1665741 : cgraph_2edge_hook_list *entry;
577 : 3331482 : cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
578 : :
579 : 1665741 : entry = (cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
580 : 1665741 : entry->hook = hook;
581 : 1665741 : entry->data = data;
582 : 1665741 : entry->next = NULL;
583 : 5343036 : while (*ptr)
584 : 3677295 : ptr = &(*ptr)->next;
585 : 1665741 : *ptr = entry;
586 : 1665741 : return entry;
587 : : }
588 : :
589 : : /* Remove ENTRY from the list of hooks called on duplicating edges. */
590 : : void
591 : 1665727 : symbol_table::remove_edge_duplication_hook (cgraph_2edge_hook_list *entry)
592 : : {
593 : 1665727 : cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
594 : :
595 : 3955044 : while (*ptr != entry)
596 : 2289317 : ptr = &(*ptr)->next;
597 : 1665727 : *ptr = entry->next;
598 : 1665727 : free (entry);
599 : 1665727 : }
600 : :
601 : : /* Call all edge duplication hooks. */
602 : : void
603 : 6824868 : symbol_table::call_edge_duplication_hooks (cgraph_edge *cs1, cgraph_edge *cs2)
604 : : {
605 : 6824868 : cgraph_2edge_hook_list *entry = m_first_edge_duplicated_hook;
606 : 20157616 : while (entry)
607 : : {
608 : 13332748 : entry->hook (cs1, cs2, entry->data);
609 : 13332748 : entry = entry->next;
610 : : }
611 : 6824868 : }
612 : :
613 : : /* Register HOOK to be called with DATA on each duplicated node. */
614 : : cgraph_2node_hook_list *
615 : 7910969 : symbol_table::add_cgraph_duplication_hook (cgraph_2node_hook hook, void *data)
616 : : {
617 : 7910969 : cgraph_2node_hook_list *entry;
618 : 15821938 : cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
619 : :
620 : 7910969 : entry = (cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
621 : 7910969 : entry->hook = hook;
622 : 7910969 : entry->data = data;
623 : 7910969 : entry->next = NULL;
624 : 39115834 : while (*ptr)
625 : 31204865 : ptr = &(*ptr)->next;
626 : 7910969 : *ptr = entry;
627 : 7910969 : return entry;
628 : : }
629 : :
630 : : /* Remove ENTRY from the list of hooks called on duplicating nodes. */
631 : : void
632 : 7816615 : symbol_table::remove_cgraph_duplication_hook (cgraph_2node_hook_list *entry)
633 : : {
634 : 7816615 : cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
635 : :
636 : 35559567 : while (*ptr != entry)
637 : 27742952 : ptr = &(*ptr)->next;
638 : 7816615 : *ptr = entry->next;
639 : 7816615 : free (entry);
640 : 7816615 : }
641 : :
642 : : /* Call all node duplication hooks. */
643 : : void
644 : 3138815 : symbol_table::call_cgraph_duplication_hooks (cgraph_node *node,
645 : : cgraph_node *node2)
646 : : {
647 : 3138815 : cgraph_2node_hook_list *entry = m_first_cgraph_duplicated_hook;
648 : 22208707 : while (entry)
649 : : {
650 : 19069892 : entry->hook (node, node2, entry->data);
651 : 19069892 : entry = entry->next;
652 : : }
653 : 3138815 : }
654 : :
655 : : /* Return cgraph node assigned to DECL. Create new one when needed. */
656 : :
657 : : cgraph_node *
658 : 94610461 : cgraph_node::create (tree decl)
659 : : {
660 : 94610461 : cgraph_node *node = symtab->create_empty ();
661 : 94610461 : gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
662 : :
663 : 94610461 : node->decl = decl;
664 : 94610461 : node->semantic_interposition = opt_for_fn (decl, flag_semantic_interposition);
665 : :
666 : 94555358 : if ((flag_openacc || flag_openmp)
667 : 94844263 : && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)))
668 : : {
669 : 8392 : node->offloadable = 1;
670 : 8392 : if (ENABLE_OFFLOADING)
671 : : g->have_offload = true;
672 : : }
673 : :
674 : 94610461 : if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
675 : 124 : node->ifunc_resolver = true;
676 : :
677 : 94610461 : node->register_symbol ();
678 : 94610461 : maybe_record_nested_function (node);
679 : :
680 : 94610461 : 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 : 519376220 : cgraph_node::get_create (tree decl)
688 : : {
689 : 519376220 : cgraph_node *first_clone = cgraph_node::get (decl);
690 : :
691 : 519376220 : if (first_clone && !first_clone->inlined_to)
692 : : return first_clone;
693 : :
694 : 94544534 : cgraph_node *node = cgraph_node::create (decl);
695 : 94544534 : 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 : 94544526 : 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 : 6831948 : cgraph_node::create_alias (tree alias, tree target)
719 : : {
720 : 6831948 : cgraph_node *alias_node;
721 : :
722 : 6831948 : gcc_assert (TREE_CODE (target) == FUNCTION_DECL
723 : : || TREE_CODE (target) == IDENTIFIER_NODE);
724 : 6831948 : gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
725 : 6831948 : alias_node = cgraph_node::get_create (alias);
726 : 6831948 : gcc_assert (!alias_node->definition);
727 : 6831948 : alias_node->alias_target = target;
728 : 6831948 : alias_node->definition = true;
729 : 6831948 : alias_node->alias = true;
730 : 6831948 : if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
731 : 47 : alias_node->transparent_alias = alias_node->weakref = true;
732 : 6831948 : if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (alias)))
733 : 319 : alias_node->ifunc_resolver = true;
734 : 6831948 : 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 : 6812777 : cgraph_node::create_same_body_alias (tree alias, tree decl)
745 : : {
746 : 6812777 : cgraph_node *n;
747 : :
748 : : /* If aliases aren't supported by the assembler, fail. */
749 : 6812777 : 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 : 6812777 : if (symtab->global_info_ready)
755 : : return NULL;
756 : :
757 : 6812777 : n = cgraph_node::create_alias (alias, decl);
758 : 6812777 : n->cpp_implicit_alias = true;
759 : 6812777 : if (symtab->cpp_implicit_aliases_done)
760 : 3574379 : 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 : 4402 : 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 : 4402 : cgraph_node *node;
777 : :
778 : 4402 : node = cgraph_node::get (alias);
779 : 4402 : if (node)
780 : 3668 : 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 : 4402 : gcc_checking_assert (virtual_offset
786 : : ? virtual_value == wi::to_wide (virtual_offset)
787 : : : virtual_value == 0);
788 : :
789 : 4402 : node->thunk = true;
790 : 4402 : node->definition = true;
791 : :
792 : 4402 : thunk_info *i;
793 : 4402 : thunk_info local_info;
794 : 4402 : if (symtab->state < CONSTRUCTION)
795 : : i = &local_info;
796 : : else
797 : 0 : i = thunk_info::get_create (node);
798 : 4402 : i->fixed_offset = fixed_offset;
799 : 4402 : i->virtual_value = virtual_value;
800 : 4402 : i->indirect_offset = indirect_offset;
801 : 4402 : i->alias = real_alias;
802 : 4402 : i->this_adjusting = this_adjusting;
803 : 4402 : i->virtual_offset_p = virtual_offset != NULL;
804 : 4402 : if (symtab->state < CONSTRUCTION)
805 : 4402 : i->register_early (node);
806 : :
807 : 4402 : 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 : 215683236 : 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 : 215683236 : 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 : 42773829 : 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 : 42773829 : 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 : 263609194 : cgraph_edge_hasher::equal (cgraph_edge *x, gimple *y)
852 : : {
853 : 263609194 : 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 : 3576 : cgraph_update_edge_in_call_site_hash (cgraph_edge *e)
860 : : {
861 : 3576 : gimple *call = e->call_stmt;
862 : 3576 : *e->caller->call_site_hash->find_slot_with_hash
863 : 3576 : (call, cgraph_edge_hasher::hash (call), INSERT) = e;
864 : 3576 : }
865 : :
866 : : /* Add call graph edge E to call site hash of its caller. */
867 : :
868 : : static inline void
869 : 8084443 : 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 : 8084443 : 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 : 8084432 : if (e->callback)
879 : : return;
880 : 8084432 : cgraph_edge **slot = e->caller->call_site_hash->find_slot_with_hash
881 : 8084432 : (e->call_stmt, cgraph_edge_hasher::hash (e->call_stmt), INSERT);
882 : 8084432 : if (*slot)
883 : : {
884 : 3565 : cgraph_edge *edge = (cgraph_edge *) *slot;
885 : 3565 : gcc_assert (edge->speculative || edge->has_callback);
886 : 3565 : 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, so we can safely
889 : : return. */
890 : 0 : gcc_checking_assert (edge == e);
891 : 3565 : if (e->callee && (!e->prev_callee
892 : 4 : || !e->prev_callee->speculative
893 : 4 : || e->prev_callee->call_stmt != e->call_stmt))
894 : 509 : *slot = e;
895 : 3565 : return;
896 : : }
897 : 8080867 : gcc_assert (!*slot || e->speculative);
898 : 8080867 : *slot = e;
899 : : }
900 : :
901 : : /* Return the callgraph edge representing the GIMPLE_CALL statement
902 : : CALL_STMT. */
903 : :
904 : : cgraph_edge *
905 : 207046589 : cgraph_node::get_edge (gimple *call_stmt)
906 : : {
907 : 207046589 : cgraph_edge *e, *e2;
908 : 207046589 : int n = 0;
909 : :
910 : 207046589 : if (call_site_hash)
911 : 33665834 : return call_site_hash->find_with_hash
912 : 33665834 : (call_stmt, cgraph_edge_hasher::hash (call_stmt));
913 : :
914 : : /* This loop may turn out to be performance problem. In such case adding
915 : : hashtables into call nodes with very many edges is probably best
916 : : solution. It is not good idea to add pointer into CALL_EXPR itself
917 : : because we want to make possible having multiple cgraph nodes representing
918 : : different clones of the same body before the body is actually cloned. */
919 : 1696186341 : for (e = callees; e; e = e->next_callee)
920 : : {
921 : 1648992713 : if (e->call_stmt == call_stmt)
922 : : break;
923 : 1522805586 : n++;
924 : : }
925 : :
926 : 173380755 : if (!e)
927 : 60917762 : for (e = indirect_calls; e; e = e->next_callee)
928 : : {
929 : 16643101 : if (e->call_stmt == call_stmt)
930 : : break;
931 : 13724134 : n++;
932 : : }
933 : :
934 : : /* We want to work with the callback-carrying edge whenever possible. When it
935 : : comes to callback edges, a call statement might have multiple callback
936 : : edges attached to it. These can be easily obtained from the carrying edge
937 : : instead. */
938 : 173380755 : if (e && e->callback)
939 : 37 : e = e->get_callback_carrying_edge ();
940 : :
941 : 173380755 : if (n > 100)
942 : : {
943 : 29795 : call_site_hash = hash_table<cgraph_edge_hasher>::create_ggc (120);
944 : 3059616 : for (e2 = callees; e2; e2 = e2->next_callee)
945 : 3029821 : cgraph_add_edge_to_call_site_hash (e2);
946 : 113550 : for (e2 = indirect_calls; e2; e2 = e2->next_callee)
947 : 83755 : cgraph_add_edge_to_call_site_hash (e2);
948 : : }
949 : :
950 : : return e;
951 : : }
952 : :
953 : : /* Change field call_stmt of edge E to NEW_STMT. If UPDATE_DERIVED_EDGES and E
954 : : is any component of speculative edge, then update all components.
955 : : speculations can be resolved in the process and edge can be removed and
956 : : deallocated. if update_derived_edges and e is a part of a callback pair,
957 : : update all associated edges and return their carrying edge. return the edge
958 : : that now represents the call. */
959 : :
960 : : cgraph_edge *
961 : 2638097 : cgraph_edge::set_call_stmt (cgraph_edge *e, gcall *new_stmt,
962 : : bool update_derived_edges)
963 : : {
964 : 2638097 : tree decl;
965 : :
966 : 2638097 : cgraph_node *new_direct_callee = NULL;
967 : 2603104 : if ((e->indirect_unknown_callee || e->speculative)
968 : 2688668 : && (decl = gimple_call_fndecl (new_stmt)))
969 : : {
970 : : /* Constant propagation and especially inlining can turn an indirect call
971 : : into a direct one. */
972 : 0 : new_direct_callee = cgraph_node::get (decl);
973 : 0 : gcc_checking_assert (new_direct_callee);
974 : : }
975 : :
976 : : /* Speculative edges has three component, update all of them
977 : : when asked to. */
978 : 2638097 : if (update_derived_edges && e->speculative
979 : : /* If we are about to resolve the speculation by calling make_direct
980 : : below, do not bother going over all the speculative edges now. */
981 : 6876 : && !new_direct_callee)
982 : : {
983 : 6876 : cgraph_edge *direct, *indirect, *next;
984 : 6876 : ipa_ref *ref;
985 : 6876 : bool e_indirect = e->indirect_unknown_callee;
986 : 6876 : int n = 0;
987 : :
988 : 6876 : direct = e->first_speculative_call_target ();
989 : 6876 : indirect = e->speculative_call_indirect_edge ();
990 : :
991 : 6876 : gcall *old_stmt = direct->call_stmt;
992 : 15578 : for (cgraph_edge *d = direct; d; d = next)
993 : : {
994 : 8702 : next = d->next_speculative_call_target ();
995 : 8702 : cgraph_edge *d2 = set_call_stmt (d, new_stmt, false);
996 : 8702 : gcc_assert (d2 == d);
997 : 8702 : n++;
998 : : }
999 : 6876 : gcc_checking_assert (indirect->num_speculative_call_targets_p () == n);
1000 : 19713 : for (unsigned int i = 0; e->caller->iterate_reference (i, ref); i++)
1001 : 12837 : if (ref->speculative && ref->stmt == old_stmt)
1002 : : {
1003 : 8702 : ref->stmt = new_stmt;
1004 : 8702 : n--;
1005 : : }
1006 : :
1007 : 6876 : indirect = set_call_stmt (indirect, new_stmt, false);
1008 : 6876 : return e_indirect ? indirect : direct;
1009 : : }
1010 : :
1011 : 2631221 : if (new_direct_callee)
1012 : 0 : e = make_direct (e, new_direct_callee);
1013 : :
1014 : : /* When updating a callback or a callback-carrying edge, update every edge
1015 : : involved. */
1016 : 2631221 : if (update_derived_edges && (e->callback || e->has_callback))
1017 : : {
1018 : 0 : cgraph_edge *current, *next, *carrying;
1019 : 0 : carrying = e->has_callback ? e : e->get_callback_carrying_edge ();
1020 : :
1021 : 0 : current = e->first_callback_edge ();
1022 : 0 : if (current)
1023 : : {
1024 : 0 : for (cgraph_edge *d = current; d; d = next)
1025 : : {
1026 : 0 : next = d->next_callback_edge ();
1027 : 0 : cgraph_edge *d2 = set_call_stmt (d, new_stmt, false);
1028 : 0 : gcc_assert (d2 == d);
1029 : : }
1030 : : }
1031 : 0 : carrying = set_call_stmt (carrying, new_stmt, false);
1032 : 0 : return carrying;
1033 : : }
1034 : :
1035 : : /* Only direct speculative edges go to call_site_hash. */
1036 : 2631221 : if (e->caller->call_site_hash
1037 : 458814 : && (!e->speculative || !e->indirect_unknown_callee)
1038 : : /* It is possible that edge was previously speculative. In this case
1039 : : we have different value in call stmt hash which needs preserving. */
1040 : 3090035 : && e->caller->get_edge (e->call_stmt) == e)
1041 : 455238 : e->caller->call_site_hash->remove_elt_with_hash
1042 : 455238 : (e->call_stmt, cgraph_edge_hasher::hash (e->call_stmt));
1043 : :
1044 : 2631221 : e->call_stmt = new_stmt;
1045 : :
1046 : 2631221 : function *fun = DECL_STRUCT_FUNCTION (e->caller->decl);
1047 : 2631221 : e->can_throw_external = stmt_can_throw_external (fun, new_stmt);
1048 : : /* Update call stite hash. For speculative calls we only record the first
1049 : : direct edge. */
1050 : 2631221 : if (e->caller->call_site_hash
1051 : 458814 : && (!e->speculative
1052 : 0 : || (e->callee
1053 : 0 : && (!e->prev_callee || !e->prev_callee->speculative
1054 : 0 : || e->prev_callee->call_stmt != e->call_stmt))
1055 : 0 : || (e->speculative && !e->callee)))
1056 : 458814 : cgraph_add_edge_to_call_site_hash (e);
1057 : : return e;
1058 : : }
1059 : :
1060 : : /* Allocate a cgraph_edge structure and fill it with data according to the
1061 : : parameters of which only CALLEE can be NULL (when creating an indirect call
1062 : : edge). CLONING_P should be set if properties that are copied from an
1063 : : original edge should not be calculated. */
1064 : :
1065 : : cgraph_edge *
1066 : 44761373 : symbol_table::create_edge (cgraph_node *caller, cgraph_node *callee,
1067 : : gcall *call_stmt, profile_count count,
1068 : : bool indir_unknown_callee, bool cloning_p)
1069 : : {
1070 : 44761373 : cgraph_edge *edge;
1071 : :
1072 : : /* LTO does not actually have access to the call_stmt since these
1073 : : have not been loaded yet. */
1074 : 44761373 : if (call_stmt)
1075 : : {
1076 : : /* This is a rather expensive check possibly triggering
1077 : : construction of call stmt hashtable. */
1078 : 43906116 : cgraph_edge *e;
1079 : 43906116 : gcc_checking_assert (!(e = caller->get_edge (call_stmt))
1080 : : || e->speculative || e->has_callback || e->callback);
1081 : :
1082 : 43906116 : gcc_assert (is_gimple_call (call_stmt));
1083 : : }
1084 : :
1085 : 44761373 : edge = ggc_alloc<cgraph_edge> ();
1086 : 44761373 : edge->m_summary_id = -1;
1087 : 44761373 : edges_count++;
1088 : :
1089 : 44761373 : ++edges_max_uid;
1090 : 44761373 : gcc_assert (edges_max_uid != 0);
1091 : 44761373 : edge->m_uid = edges_max_uid;
1092 : 44761373 : edge->aux = NULL;
1093 : 44761373 : edge->caller = caller;
1094 : 44761373 : edge->callee = callee;
1095 : 44761373 : edge->prev_caller = NULL;
1096 : 44761373 : edge->next_caller = NULL;
1097 : 44761373 : edge->prev_callee = NULL;
1098 : 44761373 : edge->next_callee = NULL;
1099 : 44761373 : edge->lto_stmt_uid = 0;
1100 : 44761373 : edge->speculative_id = 0;
1101 : :
1102 : 44761373 : edge->count = count;
1103 : 44761373 : edge->call_stmt = call_stmt;
1104 : 44761373 : edge->indirect_info = NULL;
1105 : 44761373 : edge->indirect_inlining_edge = 0;
1106 : 44761373 : edge->speculative = false;
1107 : 44761373 : edge->has_callback = false;
1108 : 44761373 : edge->callback = false;
1109 : 44761373 : edge->callback_id = 0;
1110 : 44761373 : edge->indirect_unknown_callee = indir_unknown_callee;
1111 : 44761373 : if (call_stmt && caller->call_site_hash)
1112 : 4512053 : cgraph_add_edge_to_call_site_hash (edge);
1113 : :
1114 : 44761373 : if (cloning_p)
1115 : : return edge;
1116 : :
1117 : 37956180 : edge->can_throw_external
1118 : 37956180 : = call_stmt ? stmt_can_throw_external (DECL_STRUCT_FUNCTION (caller->decl),
1119 : : call_stmt) : false;
1120 : 37956180 : edge->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
1121 : 37956180 : edge->call_stmt_cannot_inline_p = false;
1122 : :
1123 : 37956180 : if (opt_for_fn (edge->caller->decl, flag_devirtualize)
1124 : 37956180 : && call_stmt && DECL_STRUCT_FUNCTION (caller->decl))
1125 : 29869964 : edge->in_polymorphic_cdtor
1126 : 29869964 : = decl_maybe_in_construction_p (NULL, NULL, call_stmt,
1127 : : caller->decl);
1128 : : else
1129 : 8086216 : edge->in_polymorphic_cdtor = caller->thunk;
1130 : :
1131 : 37284635 : if (callee && symtab->state != LTO_STREAMING
1132 : 74645642 : && edge->callee->comdat_local_p ())
1133 : 7600 : edge->caller->calls_comdat_local = true;
1134 : :
1135 : : return edge;
1136 : : }
1137 : :
1138 : : /* Create edge from a given function to CALLEE in the cgraph. CLONING_P should
1139 : : be set if properties that are copied from an original edge should not be
1140 : : calculated. */
1141 : :
1142 : : cgraph_edge *
1143 : 43905698 : cgraph_node::create_edge (cgraph_node *callee,
1144 : : gcall *call_stmt, profile_count count, bool cloning_p)
1145 : : {
1146 : 43905698 : cgraph_edge *edge = symtab->create_edge (this, callee, call_stmt, count,
1147 : : false, cloning_p);
1148 : :
1149 : 43905698 : if (!cloning_p)
1150 : 37284635 : initialize_inline_failed (edge);
1151 : :
1152 : 43905698 : edge->next_caller = callee->callers;
1153 : 43905698 : if (callee->callers)
1154 : 34142300 : callee->callers->prev_caller = edge;
1155 : 43905698 : edge->next_callee = callees;
1156 : 43905698 : if (callees)
1157 : 33974486 : callees->prev_callee = edge;
1158 : 43905698 : callees = edge;
1159 : 43905698 : callee->callers = edge;
1160 : :
1161 : 43905698 : return edge;
1162 : : }
1163 : :
1164 : : /* Allocate cgraph_indirect_call_info and set its fields to default values. */
1165 : :
1166 : : cgraph_indirect_call_info *
1167 : 855675 : cgraph_allocate_init_indirect_info (void)
1168 : : {
1169 : 855675 : cgraph_indirect_call_info *ii;
1170 : :
1171 : 855675 : ii = ggc_cleared_alloc<cgraph_indirect_call_info> ();
1172 : 855675 : ii->param_index = -1;
1173 : 855675 : return ii;
1174 : : }
1175 : :
1176 : : /* Create an indirect edge with a yet-undetermined callee where the call
1177 : : statement destination is a formal parameter of the caller with index
1178 : : PARAM_INDEX. CLONING_P should be set if properties that are copied from an
1179 : : original edge should not be calculated and indirect_info structure should
1180 : : not be calculated. */
1181 : :
1182 : : cgraph_edge *
1183 : 855675 : cgraph_node::create_indirect_edge (gcall *call_stmt, int ecf_flags,
1184 : : profile_count count,
1185 : : bool cloning_p)
1186 : : {
1187 : 855675 : cgraph_edge *edge = symtab->create_edge (this, NULL, call_stmt, count, true,
1188 : : cloning_p);
1189 : 855675 : tree target;
1190 : :
1191 : 855675 : if (!cloning_p)
1192 : 671545 : initialize_inline_failed (edge);
1193 : :
1194 : 855675 : edge->indirect_info = cgraph_allocate_init_indirect_info ();
1195 : 855675 : edge->indirect_info->ecf_flags = ecf_flags;
1196 : 855675 : edge->indirect_info->vptr_changed = true;
1197 : :
1198 : : /* Record polymorphic call info. */
1199 : 855675 : if (!cloning_p
1200 : 855675 : && call_stmt
1201 : 669188 : && (target = gimple_call_fn (call_stmt))
1202 : 1524863 : && virtual_method_call_p (target))
1203 : : {
1204 : 100698 : ipa_polymorphic_call_context context (decl, target, call_stmt);
1205 : :
1206 : : /* Only record types can have virtual calls. */
1207 : 100698 : edge->indirect_info->polymorphic = true;
1208 : 100698 : edge->indirect_info->param_index = -1;
1209 : 100698 : edge->indirect_info->otr_token
1210 : 100698 : = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target));
1211 : 100698 : edge->indirect_info->otr_type = obj_type_ref_class (target);
1212 : 100698 : gcc_assert (TREE_CODE (edge->indirect_info->otr_type) == RECORD_TYPE);
1213 : 100698 : edge->indirect_info->context = context;
1214 : : }
1215 : :
1216 : 855675 : edge->next_callee = indirect_calls;
1217 : 855675 : if (indirect_calls)
1218 : 409082 : indirect_calls->prev_callee = edge;
1219 : 855675 : indirect_calls = edge;
1220 : :
1221 : 855675 : return edge;
1222 : : }
1223 : :
1224 : : /* Remove the edge from the list of the callees of the caller. */
1225 : :
1226 : : void
1227 : 4597635 : cgraph_edge::remove_caller (void)
1228 : : {
1229 : 4597635 : if (prev_callee)
1230 : 3731555 : prev_callee->next_callee = next_callee;
1231 : 4597635 : if (next_callee)
1232 : 3120786 : next_callee->prev_callee = prev_callee;
1233 : 4597635 : if (!prev_callee)
1234 : : {
1235 : 866080 : if (indirect_unknown_callee)
1236 : 1496 : caller->indirect_calls = next_callee;
1237 : : else
1238 : 864584 : caller->callees = next_callee;
1239 : : }
1240 : 4597635 : if (caller->call_site_hash
1241 : 4597635 : && this == caller->get_edge (call_stmt))
1242 : 564749 : caller->call_site_hash->remove_elt_with_hash
1243 : 564749 : (call_stmt, cgraph_edge_hasher::hash (call_stmt));
1244 : 4597635 : }
1245 : :
1246 : : /* Put the edge onto the free list. */
1247 : :
1248 : : void
1249 : 44323012 : symbol_table::free_edge (cgraph_edge *e)
1250 : : {
1251 : 44323012 : edges_count--;
1252 : 44323012 : if (e->m_summary_id != -1)
1253 : 20710652 : edge_released_summary_ids.safe_push (e->m_summary_id);
1254 : :
1255 : 44323012 : if (e->indirect_info)
1256 : 849042 : ggc_free (e->indirect_info);
1257 : 44323012 : ggc_free (e);
1258 : 44323012 : }
1259 : :
1260 : : /* Remove the edge in the cgraph. */
1261 : :
1262 : : void
1263 : 102293 : cgraph_edge::remove (cgraph_edge *edge)
1264 : : {
1265 : : /* Call all edge removal hooks. */
1266 : 102293 : symtab->call_edge_removal_hooks (edge);
1267 : :
1268 : 102293 : if (!edge->indirect_unknown_callee)
1269 : : /* Remove from callers list of the callee. */
1270 : 99405 : edge->remove_callee ();
1271 : :
1272 : : /* Remove from callees list of the callers. */
1273 : 102293 : edge->remove_caller ();
1274 : :
1275 : : /* Put the edge onto the free list. */
1276 : 102293 : symtab->free_edge (edge);
1277 : 102293 : }
1278 : :
1279 : : /* Turn edge into speculative call calling N2. Update
1280 : : the profile so the direct call is taken COUNT times
1281 : : with FREQUENCY.
1282 : :
1283 : : At clone materialization time, the indirect call E will
1284 : : be expanded as:
1285 : :
1286 : : if (call_dest == N2)
1287 : : n2 ();
1288 : : else
1289 : : call call_dest
1290 : :
1291 : : At this time the function just creates the direct call,
1292 : : the reference representing the if conditional and attaches
1293 : : them all to the original indirect call statement.
1294 : :
1295 : : speculative_id is used to link direct calls with their corresponding
1296 : : IPA_REF_ADDR references when representing speculative calls.
1297 : :
1298 : : Return direct edge created. */
1299 : :
1300 : : cgraph_edge *
1301 : 19644 : cgraph_edge::make_speculative (cgraph_node *n2, profile_count direct_count,
1302 : : unsigned int speculative_id)
1303 : : {
1304 : 19644 : cgraph_node *n = caller;
1305 : 19644 : ipa_ref *ref = NULL;
1306 : 19644 : cgraph_edge *e2;
1307 : :
1308 : 19644 : if (dump_file)
1309 : 26 : fprintf (dump_file, "Indirect call -> speculative call %s => %s\n",
1310 : : n->dump_name (), n2->dump_name ());
1311 : 19644 : speculative = true;
1312 : 19644 : e2 = n->create_edge (n2, call_stmt, direct_count);
1313 : 19644 : initialize_inline_failed (e2);
1314 : 19644 : e2->speculative = true;
1315 : 19644 : if (TREE_NOTHROW (n2->decl))
1316 : 9419 : e2->can_throw_external = false;
1317 : : else
1318 : 10225 : e2->can_throw_external = can_throw_external;
1319 : 19644 : e2->lto_stmt_uid = lto_stmt_uid;
1320 : 19644 : e2->speculative_id = speculative_id;
1321 : 19644 : e2->in_polymorphic_cdtor = in_polymorphic_cdtor;
1322 : 19644 : indirect_info->num_speculative_call_targets++;
1323 : 19644 : count -= e2->count;
1324 : 19644 : symtab->call_edge_duplication_hooks (this, e2);
1325 : 19644 : ref = n->create_reference (n2, IPA_REF_ADDR, call_stmt);
1326 : 19644 : ref->lto_stmt_uid = lto_stmt_uid;
1327 : 19644 : ref->speculative_id = speculative_id;
1328 : 19644 : ref->speculative = speculative;
1329 : 19644 : n2->mark_address_taken ();
1330 : 19644 : return e2;
1331 : : }
1332 : :
1333 : : /* Create a callback edge calling N2. Callback edges
1334 : : never get turned into actual calls, they are just used
1335 : : as clues and allow for optimizing functions which do not
1336 : : have any callsites during compile time, e.g. functions
1337 : : passed to standard library functions.
1338 : :
1339 : : The edge will be attached to the same call statement as
1340 : : the callback-carrying edge, which is the instance this method
1341 : : is called on.
1342 : :
1343 : : callback_id is used to pair the returned edge with the attribute that
1344 : : originated it.
1345 : :
1346 : : Return the resulting callback edge. */
1347 : :
1348 : : cgraph_edge *
1349 : 16 : cgraph_edge::make_callback (cgraph_node *n2, unsigned int callback_id)
1350 : : {
1351 : 16 : cgraph_node *n = caller;
1352 : 16 : cgraph_edge *e2;
1353 : :
1354 : 16 : has_callback = true;
1355 : 16 : e2 = n->create_edge (n2, call_stmt, count);
1356 : 16 : if (dump_file)
1357 : 3 : fprintf (
1358 : : dump_file,
1359 : : "Created callback edge %s -> %s belonging to carrying edge %s -> %s\n",
1360 : 3 : e2->caller->dump_name (), e2->callee->dump_name (), caller->dump_name (),
1361 : 3 : callee->dump_name ());
1362 : 16 : e2->inline_failed = CIF_CALLBACK_EDGE;
1363 : 16 : e2->callback = true;
1364 : 16 : e2->callback_id = callback_id;
1365 : 16 : if (TREE_NOTHROW (n2->decl))
1366 : 16 : e2->can_throw_external = false;
1367 : : else
1368 : 0 : e2->can_throw_external = can_throw_external;
1369 : 16 : e2->lto_stmt_uid = lto_stmt_uid;
1370 : 16 : n2->mark_address_taken ();
1371 : 16 : return e2;
1372 : : }
1373 : :
1374 : : /* Returns the callback_carrying edge of a callback edge on which
1375 : : it is called on or NULL when no such edge can be found.
1376 : :
1377 : : An edge is taken to be the callback-carrying if it has it's has_callback
1378 : : flag set and the edges share their call statements. */
1379 : :
1380 : : cgraph_edge *
1381 : 42 : cgraph_edge::get_callback_carrying_edge ()
1382 : : {
1383 : 42 : gcc_checking_assert (callback);
1384 : 42 : cgraph_edge *e;
1385 : 150 : for (e = caller->callees; e; e = e->next_callee)
1386 : : {
1387 : 150 : if (e->has_callback && e->call_stmt == call_stmt
1388 : 42 : && e->lto_stmt_uid == lto_stmt_uid)
1389 : : break;
1390 : : }
1391 : 42 : return e;
1392 : : }
1393 : :
1394 : : /* Returns the first callback edge in the list of callees of the caller node.
1395 : : Note that the edges might be in arbitrary order. Must be called on a
1396 : : callback or callback-carrying edge. */
1397 : :
1398 : : cgraph_edge *
1399 : 35 : cgraph_edge::first_callback_edge ()
1400 : : {
1401 : 35 : gcc_checking_assert (has_callback || callback);
1402 : 35 : cgraph_edge *e = NULL;
1403 : 66 : for (e = caller->callees; e; e = e->next_callee)
1404 : : {
1405 : 56 : if (e->callback && e->call_stmt == call_stmt
1406 : 25 : && e->lto_stmt_uid == lto_stmt_uid)
1407 : : break;
1408 : : }
1409 : 35 : return e;
1410 : : }
1411 : :
1412 : : /* Given a callback edge, returns the next callback edge belonging to the same
1413 : : carrying edge. Must be called on a callback edge, not the callback-carrying
1414 : : edge. */
1415 : :
1416 : : cgraph_edge *
1417 : 25 : cgraph_edge::next_callback_edge ()
1418 : : {
1419 : 25 : gcc_checking_assert (callback);
1420 : 25 : cgraph_edge *e = NULL;
1421 : 92 : for (e = next_callee; e; e = e->next_callee)
1422 : : {
1423 : 67 : if (e->callback && e->call_stmt == call_stmt
1424 : 0 : && e->lto_stmt_uid == lto_stmt_uid)
1425 : : break;
1426 : : }
1427 : 25 : return e;
1428 : : }
1429 : :
1430 : : /* When called on a callback-carrying edge, removes all of its attached callback
1431 : : edges and sets has_callback to FALSE. */
1432 : :
1433 : : void
1434 : 0 : cgraph_edge::purge_callback_edges ()
1435 : : {
1436 : 0 : gcc_checking_assert (has_callback);
1437 : 0 : cgraph_edge *e, *next;
1438 : 0 : for (e = first_callback_edge (); e; e = next)
1439 : : {
1440 : 0 : next = e->next_callback_edge ();
1441 : 0 : cgraph_edge::remove (e);
1442 : : }
1443 : 0 : has_callback = false;
1444 : 0 : }
1445 : :
1446 : : /* Speculative call consists of an indirect edge and one or more
1447 : : direct edge+ref pairs.
1448 : :
1449 : : Given an edge which is part of speculative call, return the first
1450 : : direct call edge in the speculative call sequence. */
1451 : :
1452 : : cgraph_edge *
1453 : 46487 : cgraph_edge::first_speculative_call_target ()
1454 : : {
1455 : 46487 : cgraph_edge *e = this;
1456 : :
1457 : 46487 : gcc_checking_assert (e->speculative);
1458 : 46487 : if (e->callee)
1459 : : {
1460 : 14532 : while (e->prev_callee && e->prev_callee->speculative
1461 : 1582 : && e->prev_callee->call_stmt == e->call_stmt
1462 : 37457 : && e->prev_callee->lto_stmt_uid == e->lto_stmt_uid)
1463 : : e = e->prev_callee;
1464 : : return e;
1465 : : }
1466 : : /* Call stmt site hash always points to the first target of the
1467 : : speculative call sequence. */
1468 : 9030 : if (e->call_stmt)
1469 : 9018 : return e->caller->get_edge (e->call_stmt);
1470 : 17 : for (cgraph_edge *e2 = e->caller->callees; true; e2 = e2->next_callee)
1471 : 17 : if (e2->speculative
1472 : 12 : && e->call_stmt == e2->call_stmt
1473 : 12 : && e->lto_stmt_uid == e2->lto_stmt_uid)
1474 : : return e2;
1475 : : }
1476 : :
1477 : : /* We always maintain first direct edge in the call site hash, if one
1478 : : exists. E is going to be removed. See if it is first one and update
1479 : : hash accordingly. INDIRECT is the indirect edge of speculative call.
1480 : : We assume that INDIRECT->num_speculative_call_targets_p () is already
1481 : : updated for removal of E. */
1482 : : static void
1483 : 39423 : update_call_stmt_hash_for_removing_direct_edge (cgraph_edge *e,
1484 : : cgraph_edge *indirect)
1485 : : {
1486 : 39423 : if (e->caller->call_site_hash)
1487 : : {
1488 : 3576 : if (e->caller->get_edge (e->call_stmt) != e)
1489 : : ;
1490 : 3576 : else if (!indirect->num_speculative_call_targets_p ())
1491 : 3071 : cgraph_update_edge_in_call_site_hash (indirect);
1492 : : else
1493 : : {
1494 : 505 : gcc_checking_assert (e->next_callee && e->next_callee->speculative
1495 : : && e->next_callee->call_stmt == e->call_stmt);
1496 : 505 : cgraph_update_edge_in_call_site_hash (e->next_callee);
1497 : : }
1498 : : }
1499 : 39423 : }
1500 : :
1501 : : /* Speculative call EDGE turned out to be direct call to CALLEE_DECL. Remove
1502 : : the speculative call sequence and return edge representing the call, the
1503 : : original EDGE can be removed and deallocated. Return the edge that now
1504 : : represents the call.
1505 : :
1506 : : For "speculative" indirect call that contains multiple "speculative"
1507 : : targets (i.e. edge->indirect_info->num_speculative_call_targets > 1),
1508 : : decrease the count and only remove current direct edge.
1509 : :
1510 : : If no speculative direct call left to the speculative indirect call, remove
1511 : : the speculative of both the indirect call and corresponding direct edge.
1512 : :
1513 : : It is up to caller to iteratively resolve each "speculative" direct call and
1514 : : redirect the call as appropriate. */
1515 : :
1516 : : cgraph_edge *
1517 : 8842 : cgraph_edge::resolve_speculation (cgraph_edge *edge, tree callee_decl)
1518 : : {
1519 : 8842 : cgraph_edge *e2;
1520 : 8842 : ipa_ref *ref;
1521 : :
1522 : 8842 : gcc_assert (edge->speculative && (!callee_decl || edge->callee));
1523 : 8842 : if (!edge->callee)
1524 : 0 : e2 = edge->first_speculative_call_target ();
1525 : : else
1526 : : e2 = edge;
1527 : 8842 : ref = e2->speculative_call_target_ref ();
1528 : 8842 : edge = edge->speculative_call_indirect_edge ();
1529 : 8842 : if (!callee_decl
1530 : 10245 : || !ref->referred->semantically_equivalent_p
1531 : 1403 : (symtab_node::get (callee_decl)))
1532 : : {
1533 : 7743 : if (dump_file)
1534 : : {
1535 : 78 : if (callee_decl)
1536 : : {
1537 : 0 : fprintf (dump_file, "Speculative indirect call %s => %s has "
1538 : : "turned out to have contradicting known target ",
1539 : 0 : edge->caller->dump_name (),
1540 : 0 : e2->callee->dump_name ());
1541 : 0 : print_generic_expr (dump_file, callee_decl);
1542 : 0 : fprintf (dump_file, "\n");
1543 : : }
1544 : : else
1545 : : {
1546 : 78 : fprintf (dump_file, "Removing speculative call %s => %s\n",
1547 : 78 : edge->caller->dump_name (),
1548 : 78 : e2->callee->dump_name ());
1549 : : }
1550 : : }
1551 : : }
1552 : : else
1553 : : {
1554 : 1099 : cgraph_edge *tmp = edge;
1555 : 1099 : if (dump_file)
1556 : 87 : fprintf (dump_file, "Speculative call turned into direct call.\n");
1557 : : edge = e2;
1558 : : e2 = tmp;
1559 : : /* FIXME: If EDGE is inlined, we should scale up the frequencies
1560 : : and counts in the functions inlined through it. */
1561 : : }
1562 : 8842 : edge->count += e2->count;
1563 : 8842 : if (edge->num_speculative_call_targets_p ())
1564 : : {
1565 : : /* The indirect edge has multiple speculative targets, don't remove
1566 : : speculative until all related direct edges are resolved. */
1567 : 7743 : edge->indirect_info->num_speculative_call_targets--;
1568 : 7743 : if (!edge->indirect_info->num_speculative_call_targets)
1569 : 909 : edge->speculative = false;
1570 : : }
1571 : : else
1572 : 1099 : edge->speculative = false;
1573 : 8842 : e2->speculative = false;
1574 : 8842 : update_call_stmt_hash_for_removing_direct_edge (e2, edge);
1575 : 8842 : ref->remove_reference ();
1576 : 8842 : if (e2->indirect_unknown_callee || e2->inline_failed)
1577 : 7982 : remove (e2);
1578 : : else
1579 : 860 : e2->callee->remove_symbol_and_inline_clones ();
1580 : 8842 : return edge;
1581 : : }
1582 : :
1583 : : /* Return edge corresponding to speculative call to a given target.
1584 : : NULL if speculative call does not have one. */
1585 : :
1586 : : cgraph_edge *
1587 : 0 : cgraph_edge::speculative_call_for_target (cgraph_node *target)
1588 : : {
1589 : 0 : for (cgraph_edge *direct = first_speculative_call_target ();
1590 : 0 : direct;
1591 : 0 : direct = direct->next_speculative_call_target ())
1592 : 0 : if (direct->speculative_call_target_ref ()
1593 : 0 : ->referred->semantically_equivalent_p (target))
1594 : : return direct;
1595 : : return NULL;
1596 : : }
1597 : :
1598 : : /* Make an indirect or speculative EDGE with an unknown callee an ordinary edge
1599 : : leading to CALLEE. Speculations can be resolved in the process and EDGE can
1600 : : be removed and deallocated. Return the edge that now represents the
1601 : : call. */
1602 : :
1603 : : cgraph_edge *
1604 : 4900 : cgraph_edge::make_direct (cgraph_edge *edge, cgraph_node *callee)
1605 : : {
1606 : 4900 : gcc_assert (edge->indirect_unknown_callee || edge->speculative);
1607 : :
1608 : : /* If we are redirecting speculative call, make it non-speculative. */
1609 : 4900 : if (edge->speculative)
1610 : : {
1611 : 1242 : cgraph_edge *found = NULL;
1612 : 1242 : cgraph_edge *direct, *next;
1613 : :
1614 : 1242 : edge = edge->speculative_call_indirect_edge ();
1615 : :
1616 : : /* Look all speculative targets and remove all but one corresponding
1617 : : to callee (if it exists). */
1618 : 1242 : for (direct = edge->first_speculative_call_target ();
1619 : 3240 : direct;
1620 : : direct = next)
1621 : : {
1622 : 1998 : next = direct->next_speculative_call_target ();
1623 : :
1624 : : /* Compare ref not direct->callee. Direct edge is possibly
1625 : : inlined or redirected. */
1626 : 1998 : if (!direct->speculative_call_target_ref ()
1627 : 1998 : ->referred->semantically_equivalent_p (callee)
1628 : 1998 : || found)
1629 : 899 : edge = direct->resolve_speculation (direct, NULL);
1630 : : else
1631 : : found = direct;
1632 : : }
1633 : :
1634 : : /* On successful speculation just remove the indirect edge and
1635 : : return the pre existing direct edge.
1636 : : It is important to not remove it and redirect because the direct
1637 : : edge may be inlined or redirected. */
1638 : 1242 : if (found)
1639 : : {
1640 : 1099 : cgraph_edge *e2 = resolve_speculation (found, callee->decl);
1641 : 1099 : gcc_checking_assert (!found->speculative && e2 == found);
1642 : : return found;
1643 : : }
1644 : 143 : gcc_checking_assert (!edge->speculative);
1645 : : }
1646 : :
1647 : 3801 : edge->indirect_unknown_callee = 0;
1648 : 3801 : ggc_free (edge->indirect_info);
1649 : 3801 : edge->indirect_info = NULL;
1650 : :
1651 : : /* Get the edge out of the indirect edge list. */
1652 : 3801 : if (edge->prev_callee)
1653 : 98 : edge->prev_callee->next_callee = edge->next_callee;
1654 : 3801 : if (edge->next_callee)
1655 : 493 : edge->next_callee->prev_callee = edge->prev_callee;
1656 : 3801 : if (!edge->prev_callee)
1657 : 3703 : edge->caller->indirect_calls = edge->next_callee;
1658 : :
1659 : : /* Put it into the normal callee list */
1660 : 3801 : edge->prev_callee = NULL;
1661 : 3801 : edge->next_callee = edge->caller->callees;
1662 : 3801 : if (edge->caller->callees)
1663 : 2403 : edge->caller->callees->prev_callee = edge;
1664 : 3801 : edge->caller->callees = edge;
1665 : :
1666 : : /* Insert to callers list of the new callee. */
1667 : 3801 : edge->set_callee (callee);
1668 : :
1669 : : /* We need to re-determine the inlining status of the edge. */
1670 : 3801 : initialize_inline_failed (edge);
1671 : 3801 : return edge;
1672 : : }
1673 : :
1674 : : /* Redirect callee of the edge to N. The function does not update underlying
1675 : : call expression. */
1676 : :
1677 : : void
1678 : 4328888 : cgraph_edge::redirect_callee (cgraph_node *n)
1679 : : {
1680 : 4328888 : bool loc = callee->comdat_local_p ();
1681 : 4328888 : cgraph_node *old_callee = callee;
1682 : :
1683 : : /* Remove from callers list of the current callee. */
1684 : 4328888 : remove_callee ();
1685 : :
1686 : : /* Insert to callers list of the new callee. */
1687 : 4328888 : set_callee (n);
1688 : :
1689 : 4328888 : if (callback)
1690 : : {
1691 : : /* When redirecting a callback callee, redirect its ref as well. */
1692 : 0 : ipa_ref *old_ref = caller->find_reference (old_callee, call_stmt,
1693 : : lto_stmt_uid, IPA_REF_ADDR);
1694 : 0 : gcc_checking_assert(old_ref);
1695 : 0 : old_ref->remove_reference ();
1696 : 0 : ipa_ref *new_ref = caller->create_reference (n, IPA_REF_ADDR, call_stmt);
1697 : 0 : new_ref->lto_stmt_uid = lto_stmt_uid;
1698 : 0 : if (!old_callee->referred_to_p ())
1699 : 0 : old_callee->address_taken = 0;
1700 : : }
1701 : :
1702 : 4328888 : if (!inline_failed)
1703 : : return;
1704 : 571456 : if (!loc && n->comdat_local_p ())
1705 : : {
1706 : 56 : cgraph_node *to = caller->inlined_to ? caller->inlined_to : caller;
1707 : 56 : to->calls_comdat_local = true;
1708 : : }
1709 : 571400 : else if (loc && !n->comdat_local_p ())
1710 : : {
1711 : 97 : cgraph_node *to = caller->inlined_to ? caller->inlined_to : caller;
1712 : 97 : gcc_checking_assert (to->calls_comdat_local);
1713 : 97 : to->calls_comdat_local = to->check_calls_comdat_local_p ();
1714 : : }
1715 : : }
1716 : :
1717 : : /* If necessary, change the function declaration in the call statement
1718 : : associated with E so that it corresponds to the edge callee. Speculations
1719 : : can be resolved in the process and EDGE can be removed and deallocated.
1720 : :
1721 : : The edge could be one of speculative direct call generated from speculative
1722 : : indirect call. In this circumstance, decrease the speculative targets
1723 : : count (i.e. num_speculative_call_targets) and redirect call stmt to the
1724 : : corresponding i-th target. If no speculative direct call left to the
1725 : : speculative indirect call, remove "speculative" of the indirect call and
1726 : : also redirect stmt to it's final direct target.
1727 : :
1728 : : When called from within tree-inline, KILLED_SSAs has to contain the pointer
1729 : : to killed_new_ssa_names within the copy_body_data structure and SSAs
1730 : : discovered to be useless (if LHS is removed) will be added to it, otherwise
1731 : : it needs to be NULL.
1732 : :
1733 : : It is up to caller to iteratively transform each "speculative"
1734 : : direct call as appropriate. */
1735 : :
1736 : : gimple *
1737 : 9607940 : cgraph_edge::redirect_call_stmt_to_callee (cgraph_edge *e,
1738 : : hash_set <tree> *killed_ssas)
1739 : : {
1740 : 9607940 : tree decl = gimple_call_fndecl (e->call_stmt);
1741 : 9607940 : gcall *new_stmt;
1742 : :
1743 : 9607940 : if (e->speculative)
1744 : : {
1745 : : /* If there already is an direct call (i.e. as a result of inliner's
1746 : : substitution), forget about speculating. */
1747 : 30581 : if (decl)
1748 : 0 : e = make_direct (e->speculative_call_indirect_edge (),
1749 : : cgraph_node::get (decl));
1750 : : else
1751 : : {
1752 : : /* Be sure we redirect all speculative targets before poking
1753 : : about indirect edge. */
1754 : 30581 : gcc_checking_assert (e->callee);
1755 : 30581 : cgraph_edge *indirect = e->speculative_call_indirect_edge ();
1756 : 30581 : gcall *new_stmt;
1757 : 30581 : ipa_ref *ref;
1758 : :
1759 : : /* Expand speculation into GIMPLE code. */
1760 : 30581 : if (dump_file)
1761 : : {
1762 : 150 : fprintf (dump_file,
1763 : : "Expanding speculative call of %s -> %s count: ",
1764 : 75 : e->caller->dump_name (),
1765 : : e->callee->dump_name ());
1766 : 75 : e->count.dump (dump_file);
1767 : 75 : fprintf (dump_file, "\n");
1768 : : }
1769 : 30581 : push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1770 : :
1771 : 30581 : profile_count all = indirect->count;
1772 : 30581 : for (cgraph_edge *e2 = e->first_speculative_call_target ();
1773 : 71127 : e2;
1774 : 40546 : e2 = e2->next_speculative_call_target ())
1775 : 40546 : all = all + e2->count;
1776 : 30581 : profile_probability prob = e->count.probability_in (all);
1777 : 30581 : if (!prob.initialized_p ())
1778 : 135 : prob = profile_probability::even ();
1779 : 30581 : ref = e->speculative_call_target_ref ();
1780 : 61162 : new_stmt = gimple_ic (e->call_stmt,
1781 : : dyn_cast<cgraph_node *> (ref->referred),
1782 : : prob);
1783 : 30581 : e->speculative = false;
1784 : 30581 : if (indirect->num_speculative_call_targets_p ())
1785 : : {
1786 : : /* The indirect edge has multiple speculative targets, don't
1787 : : remove speculative until all related direct edges are
1788 : : redirected. */
1789 : 30581 : indirect->indirect_info->num_speculative_call_targets--;
1790 : 30581 : if (!indirect->indirect_info->num_speculative_call_targets)
1791 : 22831 : indirect->speculative = false;
1792 : : }
1793 : : else
1794 : 0 : indirect->speculative = false;
1795 : : /* Indirect edges are not both in the call site hash.
1796 : : get it updated. */
1797 : 30581 : update_call_stmt_hash_for_removing_direct_edge (e, indirect);
1798 : 30581 : cgraph_edge::set_call_stmt (e, new_stmt, false);
1799 : 30581 : e->count = gimple_bb (e->call_stmt)->count;
1800 : :
1801 : : /* Once we are done with expanding the sequence, update also indirect
1802 : : call probability. Until then the basic block accounts for the
1803 : : sum of indirect edge and all non-expanded speculations. */
1804 : 30581 : if (!indirect->speculative)
1805 : 22831 : indirect->count = gimple_bb (indirect->call_stmt)->count;
1806 : 30581 : ref->speculative = false;
1807 : 30581 : ref->stmt = NULL;
1808 : 30581 : pop_cfun ();
1809 : : /* Continue redirecting E to proper target. */
1810 : : }
1811 : : }
1812 : :
1813 : :
1814 : 9607940 : if (e->indirect_unknown_callee
1815 : 9513898 : || decl == e->callee->decl)
1816 : 8635889 : return e->call_stmt;
1817 : :
1818 : : /* When redirecting a callback edge, all we need to do is replace
1819 : : the original address with the address of the function we are
1820 : : redirecting to. */
1821 : 972051 : if (e->callback)
1822 : : {
1823 : 0 : cgraph_edge *carrying = e->get_callback_carrying_edge ();
1824 : 0 : if (!callback_is_special_cased (carrying->callee->decl, e->call_stmt)
1825 : 0 : && !lookup_attribute (CALLBACK_ATTR_IDENT,
1826 : 0 : DECL_ATTRIBUTES (carrying->callee->decl)))
1827 : : /* Callback attribute is removed if the dispatching function changes
1828 : : signature, as the indices wouldn't be correct anymore. These edges
1829 : : will get cleaned up later, ignore their redirection for now. */
1830 : 0 : return e->call_stmt;
1831 : 0 : int fn_idx = callback_fetch_fn_position (e, carrying);
1832 : 0 : tree previous_arg = gimple_call_arg (e->call_stmt, fn_idx);
1833 : 0 : location_t loc = EXPR_LOCATION (previous_arg);
1834 : 0 : tree new_addr = build_fold_addr_expr_loc (loc, e->callee->decl);
1835 : 0 : gimple_call_set_arg (e->call_stmt, fn_idx, new_addr);
1836 : 0 : return e->call_stmt;
1837 : : }
1838 : :
1839 : 972051 : if (decl && ipa_saved_clone_sources)
1840 : : {
1841 : 788780 : tree *p = ipa_saved_clone_sources->get (e->callee);
1842 : 788780 : if (p && decl == *p)
1843 : : {
1844 : 33437 : gimple_call_set_fndecl (e->call_stmt, e->callee->decl);
1845 : 33437 : return e->call_stmt;
1846 : : }
1847 : : }
1848 : 938614 : if (flag_checking && decl)
1849 : : {
1850 : 904292 : if (cgraph_node *node = cgraph_node::get (decl))
1851 : : {
1852 : 771222 : clone_info *info = clone_info::get (node);
1853 : 771222 : gcc_assert (!info || !info->param_adjustments);
1854 : : }
1855 : : }
1856 : :
1857 : 938614 : clone_info *callee_info = clone_info::get (e->callee);
1858 : 938614 : if (symtab->dump_file)
1859 : : {
1860 : 0 : fprintf (symtab->dump_file, "updating call of %s -> %s: ",
1861 : 0 : e->caller->dump_name (), e->callee->dump_name ());
1862 : 0 : print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1863 : 0 : if (callee_info && callee_info->param_adjustments)
1864 : 0 : callee_info->param_adjustments->dump (symtab->dump_file);
1865 : : }
1866 : :
1867 : 480142 : if (ipa_param_adjustments *padjs
1868 : 938614 : = callee_info ? callee_info->param_adjustments : NULL)
1869 : : {
1870 : : /* We need to defer cleaning EH info on the new statement to
1871 : : fixup-cfg. We may not have dominator information at this point
1872 : : and thus would end up with unreachable blocks and have no way
1873 : : to communicate that we need to run CFG cleanup then. */
1874 : 451609 : int lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1875 : 451609 : if (lp_nr != 0)
1876 : 133113 : remove_stmt_from_eh_lp (e->call_stmt);
1877 : :
1878 : 451609 : tree old_fntype = gimple_call_fntype (e->call_stmt);
1879 : 451609 : new_stmt = padjs->modify_call (e, false, killed_ssas);
1880 : 451609 : cgraph_node *origin = e->callee;
1881 : 666732 : while (origin->clone_of)
1882 : : origin = origin->clone_of;
1883 : :
1884 : 451609 : if ((origin->former_clone_of
1885 : 348357 : && old_fntype == TREE_TYPE (origin->former_clone_of))
1886 : 453774 : || old_fntype == TREE_TYPE (origin->decl))
1887 : 346194 : gimple_call_set_fntype (new_stmt, TREE_TYPE (e->callee->decl));
1888 : : else
1889 : : {
1890 : 105415 : tree new_fntype = padjs->build_new_function_type (old_fntype, true);
1891 : 105415 : gimple_call_set_fntype (new_stmt, new_fntype);
1892 : : }
1893 : :
1894 : 451609 : if (lp_nr != 0)
1895 : 133113 : add_stmt_to_eh_lp (new_stmt, lp_nr);
1896 : : }
1897 : : else
1898 : : {
1899 : 487005 : if (flag_checking
1900 : 487005 : && !fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE,
1901 : : BUILT_IN_UNREACHABLE_TRAP))
1902 : 330233 : ipa_verify_edge_has_no_modifications (e);
1903 : 487005 : new_stmt = e->call_stmt;
1904 : 487005 : gimple_call_set_fndecl (new_stmt, e->callee->decl);
1905 : 487005 : update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1906 : : }
1907 : :
1908 : : /* If changing the call to __cxa_pure_virtual or similar noreturn function,
1909 : : adjust gimple_call_fntype too. */
1910 : 938614 : if (gimple_call_noreturn_p (new_stmt)
1911 : 159857 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (e->callee->decl)))
1912 : 159785 : && TYPE_ARG_TYPES (TREE_TYPE (e->callee->decl))
1913 : 1098393 : && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (e->callee->decl)))
1914 : 159779 : == void_type_node))
1915 : 159062 : gimple_call_set_fntype (new_stmt, TREE_TYPE (e->callee->decl));
1916 : :
1917 : : /* If the call becomes noreturn, remove the LHS if possible. */
1918 : 938614 : tree lhs = gimple_call_lhs (new_stmt);
1919 : 938614 : if (lhs
1920 : 275631 : && gimple_call_noreturn_p (new_stmt)
1921 : 984698 : && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (new_stmt)))
1922 : 59 : || should_remove_lhs_p (lhs)))
1923 : : {
1924 : 46043 : gimple_call_set_lhs (new_stmt, NULL_TREE);
1925 : : /* We need to fix up the SSA name to avoid checking errors. */
1926 : 46043 : if (TREE_CODE (lhs) == SSA_NAME)
1927 : : {
1928 : 35921 : tree var = create_tmp_reg_fn (DECL_STRUCT_FUNCTION (e->caller->decl),
1929 : 35921 : TREE_TYPE (lhs), NULL);
1930 : 35921 : SET_SSA_NAME_VAR_OR_IDENTIFIER (lhs, var);
1931 : 35921 : SSA_NAME_DEF_STMT (lhs) = gimple_build_nop ();
1932 : 35921 : set_ssa_default_def (DECL_STRUCT_FUNCTION (e->caller->decl),
1933 : : var, lhs);
1934 : : }
1935 : 46043 : update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1936 : : }
1937 : :
1938 : : /* If new callee has no static chain, remove it. */
1939 : 938614 : if (gimple_call_chain (new_stmt) && !DECL_STATIC_CHAIN (e->callee->decl))
1940 : : {
1941 : 56 : gimple_call_set_chain (new_stmt, NULL);
1942 : 56 : update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1943 : : }
1944 : :
1945 : 938614 : maybe_remove_unused_call_args (DECL_STRUCT_FUNCTION (e->caller->decl),
1946 : : new_stmt);
1947 : :
1948 : : /* Update callback edges if setting the carrying edge's statement, or else
1949 : : their pairing would fall apart. */
1950 : 938614 : e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt, e->has_callback);
1951 : :
1952 : 938614 : if (symtab->dump_file)
1953 : : {
1954 : 0 : fprintf (symtab->dump_file, " updated to:");
1955 : 0 : print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1956 : : }
1957 : : return new_stmt;
1958 : : }
1959 : :
1960 : : /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1961 : : OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1962 : : of OLD_STMT if it was previously call statement.
1963 : : If NEW_STMT is NULL, the call has been dropped without any
1964 : : replacement. */
1965 : :
1966 : : static void
1967 : 117426 : cgraph_update_edges_for_call_stmt_node (cgraph_node *node,
1968 : : gimple *old_stmt, tree old_call,
1969 : : gimple *new_stmt)
1970 : : {
1971 : 117426 : tree new_call = (new_stmt && is_gimple_call (new_stmt))
1972 : 122502 : ? gimple_call_fndecl (new_stmt) : 0;
1973 : :
1974 : : /* We are seeing indirect calls, then there is nothing to update. */
1975 : 117426 : if (!new_call && !old_call)
1976 : : return;
1977 : : /* See if we turned indirect call into direct call or folded call to one builtin
1978 : : into different builtin. */
1979 : 115503 : if (old_call != new_call)
1980 : : {
1981 : 114502 : cgraph_edge *e = node->get_edge (old_stmt);
1982 : 114502 : cgraph_edge *ne = NULL;
1983 : 114502 : profile_count count;
1984 : :
1985 : 114502 : if (e)
1986 : : {
1987 : : /* If call was devirtualized during cloning, mark edge
1988 : : as resolved. */
1989 : 93239 : if (e->speculative)
1990 : : {
1991 : 1 : if (new_stmt && is_gimple_call (new_stmt))
1992 : : {
1993 : 1 : tree decl = gimple_call_fndecl (new_stmt);
1994 : 1 : if (decl)
1995 : 1 : e = cgraph_edge::resolve_speculation (e, decl);
1996 : : }
1997 : : else
1998 : 0 : e = cgraph_edge::resolve_speculation (e, NULL);
1999 : : }
2000 : : /* Keep calls marked as dead dead. */
2001 : 93239 : if (new_stmt && is_gimple_call (new_stmt) && e->callee
2002 : 93812 : && fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE,
2003 : : BUILT_IN_UNREACHABLE_TRAP))
2004 : : {
2005 : 5 : cgraph_edge::set_call_stmt (node->get_edge (old_stmt),
2006 : : as_a <gcall *> (new_stmt));
2007 : 32 : return;
2008 : : }
2009 : : /* See if the edge is already there and has the correct callee. It
2010 : : might be so because of indirect inlining has already updated
2011 : : it. We also might've cloned and redirected the edge. */
2012 : 93234 : if (new_call && e->callee)
2013 : : {
2014 : : cgraph_node *callee = e->callee;
2015 : 1137 : while (callee)
2016 : : {
2017 : 591 : if (callee->decl == new_call
2018 : 591 : || callee->former_clone_of == new_call)
2019 : : {
2020 : 22 : cgraph_edge::set_call_stmt (e, as_a <gcall *> (new_stmt));
2021 : 22 : return;
2022 : : }
2023 : 569 : callee = callee->clone_of;
2024 : : }
2025 : : }
2026 : :
2027 : : /* Otherwise remove edge and create new one; we can't simply redirect
2028 : : since function has changed, so inline plan and other information
2029 : : attached to edge is invalid. */
2030 : 93212 : count = e->count;
2031 : 93212 : if (e->indirect_unknown_callee || e->inline_failed)
2032 : 93212 : cgraph_edge::remove (e);
2033 : : else
2034 : 0 : e->callee->remove_symbol_and_inline_clones ();
2035 : : }
2036 : 21263 : else if (new_call)
2037 : : {
2038 : : /* We are seeing new direct call; compute profile info based on BB. */
2039 : 4 : basic_block bb = gimple_bb (new_stmt);
2040 : 4 : count = bb->count;
2041 : : }
2042 : :
2043 : 93216 : if (new_call)
2044 : : {
2045 : 2323 : ne = node->create_edge (cgraph_node::get_create (new_call),
2046 : : as_a <gcall *> (new_stmt), count);
2047 : 2323 : gcc_assert (ne->inline_failed);
2048 : : }
2049 : : }
2050 : : /* We only updated the call stmt; update pointer in cgraph edge.. */
2051 : 1001 : else if (old_stmt != new_stmt)
2052 : 0 : cgraph_edge::set_call_stmt (node->get_edge (old_stmt),
2053 : : as_a <gcall *> (new_stmt));
2054 : : }
2055 : :
2056 : : /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
2057 : : OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
2058 : : of OLD_STMT before it was updated (updating can happen inplace). */
2059 : :
2060 : : void
2061 : 95786 : cgraph_update_edges_for_call_stmt (gimple *old_stmt, tree old_decl,
2062 : : gimple *new_stmt)
2063 : : {
2064 : 95786 : cgraph_node *orig = cgraph_node::get (cfun->decl);
2065 : 95786 : cgraph_node *node;
2066 : :
2067 : 95786 : gcc_checking_assert (orig);
2068 : 95786 : gcc_assert (!orig->thunk);
2069 : 95786 : cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
2070 : 95786 : if (orig->clones)
2071 : 42639 : for (node = orig->clones; node != orig;)
2072 : : {
2073 : : /* Do not attempt to adjust bodies of yet unexpanded thunks. */
2074 : 21642 : if (!node->thunk)
2075 : 21640 : cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl,
2076 : : new_stmt);
2077 : 21642 : if (node->clones)
2078 : : node = node->clones;
2079 : 21630 : else if (node->next_sibling_clone)
2080 : : node = node->next_sibling_clone;
2081 : : else
2082 : : {
2083 : 42006 : while (node != orig && !node->next_sibling_clone)
2084 : 21009 : node = node->clone_of;
2085 : 20997 : if (node != orig)
2086 : 0 : node = node->next_sibling_clone;
2087 : : }
2088 : : }
2089 : 95786 : }
2090 : :
2091 : :
2092 : : /* Remove all callees from the node. */
2093 : :
2094 : : void
2095 : 211083344 : cgraph_node::remove_callees (void)
2096 : : {
2097 : 211083344 : cgraph_edge *e, *f;
2098 : :
2099 : 211083344 : calls_comdat_local = false;
2100 : :
2101 : : /* It is sufficient to remove the edges from the lists of callers of
2102 : : the callees. The callee list of the node can be zapped with one
2103 : : assignment. */
2104 : 249962567 : for (e = callees; e; e = f)
2105 : : {
2106 : 38879223 : f = e->next_callee;
2107 : 38879223 : symtab->call_edge_removal_hooks (e);
2108 : 38879223 : if (!e->indirect_unknown_callee)
2109 : 38879223 : e->remove_callee ();
2110 : 38879223 : symtab->free_edge (e);
2111 : : }
2112 : 211929498 : for (e = indirect_calls; e; e = f)
2113 : : {
2114 : 846154 : f = e->next_callee;
2115 : 846154 : symtab->call_edge_removal_hooks (e);
2116 : 846154 : if (!e->indirect_unknown_callee)
2117 : 0 : e->remove_callee ();
2118 : 846154 : symtab->free_edge (e);
2119 : : }
2120 : 211083344 : indirect_calls = NULL;
2121 : 211083344 : callees = NULL;
2122 : 211083344 : if (call_site_hash)
2123 : : {
2124 : 29722 : call_site_hash->empty ();
2125 : 29722 : call_site_hash = NULL;
2126 : : }
2127 : 211083344 : }
2128 : :
2129 : : /* Remove all callers from the node. */
2130 : :
2131 : : void
2132 : 94225015 : cgraph_node::remove_callers (void)
2133 : : {
2134 : 94225015 : cgraph_edge *e, *f;
2135 : :
2136 : : /* It is sufficient to remove the edges from the lists of callees of
2137 : : the callers. The caller list of the node can be zapped with one
2138 : : assignment. */
2139 : 98720357 : for (e = callers; e; e = f)
2140 : : {
2141 : 4495342 : f = e->next_caller;
2142 : : /* When removing a callback-carrying edge, remove all its attached edges
2143 : : as well. */
2144 : 4495342 : if (e->has_callback)
2145 : : {
2146 : 0 : cgraph_edge *cbe, *next_cbe = NULL;
2147 : 0 : for (cbe = e->first_callback_edge (); cbe; cbe = next_cbe)
2148 : : {
2149 : 0 : next_cbe = cbe->next_callback_edge ();
2150 : 0 : cgraph_edge::remove (cbe);
2151 : : }
2152 : : }
2153 : 4495342 : symtab->call_edge_removal_hooks (e);
2154 : 4495342 : e->remove_caller ();
2155 : 4495342 : symtab->free_edge (e);
2156 : : }
2157 : 94225015 : callers = NULL;
2158 : 94225015 : }
2159 : :
2160 : : /* Helper function for cgraph_release_function_body and free_lang_data.
2161 : : It releases body from function DECL without having to inspect its
2162 : : possibly non-existent symtab node. */
2163 : :
2164 : : void
2165 : 104310839 : release_function_body (tree decl)
2166 : : {
2167 : 104310839 : function *fn = DECL_STRUCT_FUNCTION (decl);
2168 : 104310839 : if (fn)
2169 : : {
2170 : 92746370 : if (fn->cfg
2171 : 92746370 : && loops_for_fn (fn))
2172 : : {
2173 : 1727621 : fn->curr_properties &= ~PROP_loops;
2174 : 1727621 : loop_optimizer_finalize (fn);
2175 : : }
2176 : 92746370 : if (fn->gimple_df)
2177 : : {
2178 : 1736255 : delete_tree_ssa (fn);
2179 : 1736255 : fn->eh = NULL;
2180 : : }
2181 : 92746370 : if (fn->cfg)
2182 : : {
2183 : 1727622 : gcc_assert (!dom_info_available_p (fn, CDI_DOMINATORS));
2184 : 1727622 : gcc_assert (!dom_info_available_p (fn, CDI_POST_DOMINATORS));
2185 : 1727622 : delete_tree_cfg_annotations (fn);
2186 : 1727622 : free_cfg (fn);
2187 : 1727622 : fn->cfg = NULL;
2188 : : }
2189 : 92746370 : if (fn->value_histograms)
2190 : 12 : free_histograms (fn);
2191 : 92746370 : gimple_set_body (decl, NULL);
2192 : : /* Struct function hangs a lot of data that would leak if we didn't
2193 : : removed all pointers to it. */
2194 : 92746370 : ggc_free (fn);
2195 : 92746370 : DECL_STRUCT_FUNCTION (decl) = NULL;
2196 : : }
2197 : 104310839 : DECL_SAVED_TREE (decl) = NULL;
2198 : 104310839 : }
2199 : :
2200 : : /* Release memory used to represent body of function.
2201 : : Use this only for functions that are released before being translated to
2202 : : target code (i.e. RTL). Functions that are compiled to RTL and beyond
2203 : : are free'd in final.cc via free_after_compilation().
2204 : : KEEP_ARGUMENTS are useful only if you want to rebuild body as thunk. */
2205 : :
2206 : : void
2207 : 104299301 : cgraph_node::release_body (bool keep_arguments)
2208 : : {
2209 : 104299301 : ipa_transforms_to_apply.release ();
2210 : 104299301 : if (!used_as_abstract_origin && symtab->state != PARSING)
2211 : : {
2212 : 103707529 : DECL_RESULT (decl) = NULL;
2213 : :
2214 : 103707529 : if (!keep_arguments)
2215 : 103677082 : DECL_ARGUMENTS (decl) = NULL;
2216 : : }
2217 : : /* If the node is abstract and needed, then do not clear
2218 : : DECL_INITIAL of its associated function declaration because it's
2219 : : needed to emit debug info later. */
2220 : 104299301 : if (!used_as_abstract_origin && DECL_INITIAL (decl))
2221 : 92195749 : DECL_INITIAL (decl) = error_mark_node;
2222 : 104299301 : release_function_body (decl);
2223 : 104299301 : if (lto_file_data)
2224 : : {
2225 : 49694 : lto_free_function_in_decl_state_for_node (this);
2226 : 49694 : lto_file_data = NULL;
2227 : : }
2228 : 104299301 : if (flag_checking && clones)
2229 : : {
2230 : : /* It is invalid to release body before materializing clones except
2231 : : for thunks that don't really need a body. Verify also that we do
2232 : : not leak pointers to the call statements. */
2233 : 42 : for (cgraph_node *node = clones; node;
2234 : 24 : node = node->next_sibling_clone)
2235 : 24 : gcc_assert (node->thunk && !node->callees->call_stmt);
2236 : : }
2237 : 104299301 : remove_callees ();
2238 : 104299301 : remove_all_references ();
2239 : 104299301 : }
2240 : :
2241 : : /* Remove function from symbol table. */
2242 : :
2243 : : void
2244 : 94225015 : cgraph_node::remove (void)
2245 : : {
2246 : 94225015 : bool clone_info_set = false;
2247 : 94225015 : clone_info *info, saved_info;
2248 : 94225015 : if (symtab->ipa_clones_dump_file && symtab->cloned_nodes.contains (this))
2249 : 4 : fprintf (symtab->ipa_clones_dump_file,
2250 : : "Callgraph removal;%s;%d;%s;%d;%d\n", asm_name (), get_uid (),
2251 : 4 : DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
2252 : 8 : DECL_SOURCE_COLUMN (decl));
2253 : :
2254 : 94225015 : if ((info = clone_info::get (this)) != NULL)
2255 : : {
2256 : 373368 : saved_info = *info;
2257 : 373368 : clone_info_set = true;
2258 : : }
2259 : 94225015 : symtab->call_cgraph_removal_hooks (this);
2260 : 94225015 : remove_callers ();
2261 : 94225015 : remove_callees ();
2262 : 94225015 : ipa_transforms_to_apply.release ();
2263 : 94225015 : delete_function_version (function_version ());
2264 : :
2265 : : /* Incremental inlining access removed nodes stored in the postorder list.
2266 : : */
2267 : 94225015 : force_output = false;
2268 : 94225015 : forced_by_abi = false;
2269 : :
2270 : 188076662 : unregister (clone_info_set ? &saved_info : NULL);
2271 : 94225015 : if (prev_sibling_clone)
2272 : 742277 : prev_sibling_clone->next_sibling_clone = next_sibling_clone;
2273 : 93482738 : else if (clone_of)
2274 : : {
2275 : 1803299 : clone_of->clones = next_sibling_clone;
2276 : 1803299 : if (!clones)
2277 : : {
2278 : 1799417 : bool need_body = false;
2279 : 1799417 : for (cgraph_node *n = clone_of; n; n = n->clone_of)
2280 : 1794259 : if (n->analyzed || n->clones)
2281 : : {
2282 : : need_body = true;
2283 : : break;
2284 : : }
2285 : 1794255 : if (!need_body)
2286 : 5158 : clone_of->release_body ();
2287 : : }
2288 : : }
2289 : 94225015 : if (next_sibling_clone)
2290 : 936927 : next_sibling_clone->prev_sibling_clone = prev_sibling_clone;
2291 : 94225015 : if (clones)
2292 : : {
2293 : 39185 : cgraph_node *n, *next;
2294 : :
2295 : 39185 : if (clone_of)
2296 : : {
2297 : 184777 : for (n = clones; n->next_sibling_clone; n = n->next_sibling_clone)
2298 : 145592 : n->clone_of = clone_of;
2299 : 39185 : n->clone_of = clone_of;
2300 : 39185 : n->next_sibling_clone = clone_of->clones;
2301 : 39185 : if (clone_of->clones)
2302 : 34069 : clone_of->clones->prev_sibling_clone = n;
2303 : 39185 : clone_of->clones = clones;
2304 : : }
2305 : : else
2306 : : {
2307 : : /* We are removing node with clones. This makes clones inconsistent,
2308 : : but assume they will be removed subsequently and just keep clone
2309 : : tree intact. This can happen in unreachable function removal since
2310 : : we remove unreachable functions in random order, not by bottom-up
2311 : : walk of clone trees. */
2312 : 0 : for (n = clones; n; n = next)
2313 : : {
2314 : 0 : next = n->next_sibling_clone;
2315 : 0 : n->next_sibling_clone = NULL;
2316 : 0 : n->prev_sibling_clone = NULL;
2317 : 0 : n->clone_of = NULL;
2318 : : }
2319 : : }
2320 : : }
2321 : :
2322 : : /* While all the clones are removed after being proceeded, the function
2323 : : itself is kept in the cgraph even after it is compiled. Check whether
2324 : : we are done with this body and reclaim it proactively if this is the case.
2325 : : */
2326 : 94225015 : if (symtab->state != LTO_STREAMING)
2327 : : {
2328 : 94222974 : cgraph_node *n = cgraph_node::get (decl);
2329 : 94222974 : if (!n
2330 : 94222974 : || (!n->clones && !n->clone_of && !n->inlined_to
2331 : 1151506 : && ((symtab->global_info_ready || in_lto_p)
2332 : 9669 : && (TREE_ASM_WRITTEN (n->decl)
2333 : 9638 : || DECL_EXTERNAL (n->decl)
2334 : 5279 : || !n->analyzed
2335 : 5215 : || (!flag_wpa && n->in_other_partition)))))
2336 : 91264981 : release_body ();
2337 : : }
2338 : : else
2339 : : {
2340 : 2041 : lto_free_function_in_decl_state_for_node (this);
2341 : 2041 : lto_file_data = NULL;
2342 : : }
2343 : :
2344 : 94225015 : decl = NULL;
2345 : 94225015 : if (call_site_hash)
2346 : : {
2347 : 0 : call_site_hash->empty ();
2348 : 0 : call_site_hash = NULL;
2349 : : }
2350 : :
2351 : 94225015 : symtab->release_symbol (this);
2352 : 94225015 : }
2353 : :
2354 : : /* Likewise indicate that a node is having address taken. */
2355 : :
2356 : : void
2357 : 4769150 : cgraph_node::mark_address_taken (void)
2358 : : {
2359 : : /* Indirect inlining can figure out that all uses of the address are
2360 : : inlined. */
2361 : 4769150 : if (inlined_to)
2362 : : {
2363 : 0 : gcc_assert (cfun->after_inlining);
2364 : 0 : gcc_assert (callers->indirect_inlining_edge);
2365 : : return;
2366 : : }
2367 : : /* FIXME: address_taken flag is used both as a shortcut for testing whether
2368 : : IPA_REF_ADDR reference exists (and thus it should be set on node
2369 : : representing alias we take address of) and as a test whether address
2370 : : of the object was taken (and thus it should be set on node alias is
2371 : : referring to). We should remove the first use and the remove the
2372 : : following set. */
2373 : 4769150 : address_taken = 1;
2374 : 4769150 : cgraph_node *node = ultimate_alias_target ();
2375 : 4769150 : node->address_taken = 1;
2376 : : }
2377 : :
2378 : : /* Return local info node for the compiled function. */
2379 : :
2380 : : cgraph_node *
2381 : 12597648 : cgraph_node::local_info_node (tree decl)
2382 : : {
2383 : 12597648 : gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
2384 : 12597648 : cgraph_node *node = get (decl);
2385 : 12597648 : if (!node)
2386 : : return NULL;
2387 : 12597648 : return node->ultimate_alias_target ();
2388 : : }
2389 : :
2390 : : /* Return RTL info for the compiled function. */
2391 : :
2392 : : cgraph_rtl_info *
2393 : 59053531 : cgraph_node::rtl_info (const_tree decl)
2394 : : {
2395 : 59053531 : gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
2396 : 59053531 : cgraph_node *node = get (decl);
2397 : 59053531 : if (!node)
2398 : : return NULL;
2399 : 58914184 : enum availability avail;
2400 : 58914184 : node = node->ultimate_alias_target (&avail);
2401 : 58914184 : if (decl != current_function_decl
2402 : 55928592 : && (avail < AVAIL_AVAILABLE
2403 : 50807514 : || (node->decl != current_function_decl
2404 : 50734163 : && !TREE_ASM_WRITTEN (node->decl))))
2405 : : return NULL;
2406 : : /* Allocate if it doesn't exist. */
2407 : 50250275 : if (node->rtl == NULL)
2408 : : {
2409 : 1341354 : node->rtl = ggc_cleared_alloc<cgraph_rtl_info> ();
2410 : 1341354 : SET_HARD_REG_SET (node->rtl->function_used_regs);
2411 : : }
2412 : 50250275 : return node->rtl;
2413 : : }
2414 : :
2415 : : /* Return a string describing the failure REASON. */
2416 : :
2417 : : const char*
2418 : 9831 : cgraph_inline_failed_string (cgraph_inline_failed_t reason)
2419 : : {
2420 : : #undef DEFCIFCODE
2421 : : #define DEFCIFCODE(code, type, string) string,
2422 : :
2423 : 9831 : static const char *cif_string_table[CIF_N_REASONS] = {
2424 : : #include "cif-code.def"
2425 : : };
2426 : :
2427 : : /* Signedness of an enum type is implementation defined, so cast it
2428 : : to unsigned before testing. */
2429 : 9831 : gcc_assert ((unsigned) reason < CIF_N_REASONS);
2430 : 9831 : return cif_string_table[reason];
2431 : : }
2432 : :
2433 : : /* Return a type describing the failure REASON. */
2434 : :
2435 : : cgraph_inline_failed_type_t
2436 : 72904496 : cgraph_inline_failed_type (cgraph_inline_failed_t reason)
2437 : : {
2438 : : #undef DEFCIFCODE
2439 : : #define DEFCIFCODE(code, type, string) type,
2440 : :
2441 : 72904496 : static cgraph_inline_failed_type_t cif_type_table[CIF_N_REASONS] = {
2442 : : #include "cif-code.def"
2443 : : };
2444 : :
2445 : : /* Signedness of an enum type is implementation defined, so cast it
2446 : : to unsigned before testing. */
2447 : 72904496 : gcc_assert ((unsigned) reason < CIF_N_REASONS);
2448 : 72904496 : return cif_type_table[reason];
2449 : : }
2450 : :
2451 : : /* Names used to print out the availability enum. */
2452 : : const char * const cgraph_availability_names[] =
2453 : : {"unset", "not_available", "overwritable", "available", "local"};
2454 : :
2455 : : /* Output flags of edge to a file F. */
2456 : :
2457 : : void
2458 : 22634 : cgraph_edge::dump_edge_flags (FILE *f)
2459 : : {
2460 : 22634 : if (speculative)
2461 : 282 : fprintf (f, "(speculative) ");
2462 : 22634 : if (callback)
2463 : 0 : fprintf (f, "(callback) ");
2464 : 22634 : if (has_callback)
2465 : 0 : fprintf (f, "(has_callback) ");
2466 : 22634 : if (!inline_failed)
2467 : 1789 : fprintf (f, "(inlined) ");
2468 : 22634 : if (call_stmt_cannot_inline_p)
2469 : 0 : fprintf (f, "(call_stmt_cannot_inline_p) ");
2470 : 22634 : if (indirect_inlining_edge)
2471 : 325 : fprintf (f, "(indirect_inlining) ");
2472 : 22634 : if (count.initialized_p ())
2473 : : {
2474 : 21875 : fprintf (f, "(");
2475 : 21875 : count.dump (f);
2476 : 21875 : fprintf (f, ",");
2477 : 21875 : fprintf (f, "%.2f per call) ", sreal_frequency ().to_double ());
2478 : : }
2479 : 22634 : if (can_throw_external)
2480 : 2684 : fprintf (f, "(can throw external) ");
2481 : 22634 : }
2482 : :
2483 : : /* Dump edge to stderr. */
2484 : :
2485 : : void
2486 : 0 : cgraph_edge::debug (void)
2487 : : {
2488 : 0 : fprintf (stderr, "%s -> %s ", caller->dump_asm_name (),
2489 : 0 : callee == NULL ? "(null)" : callee->dump_asm_name ());
2490 : 0 : dump_edge_flags (stderr);
2491 : 0 : fprintf (stderr, "\n\n");
2492 : 0 : caller->debug ();
2493 : 0 : if (callee != NULL)
2494 : 0 : callee->debug ();
2495 : 0 : }
2496 : :
2497 : : /* Dump call graph node to file F. */
2498 : :
2499 : : void
2500 : 5835 : cgraph_node::dump (FILE *f)
2501 : : {
2502 : 5835 : cgraph_edge *edge;
2503 : :
2504 : 5835 : dump_base (f);
2505 : :
2506 : 5835 : if (inlined_to)
2507 : 776 : fprintf (f, " Function %s is inline copy in %s\n",
2508 : : dump_name (),
2509 : : inlined_to->dump_name ());
2510 : 5835 : if (clone_of)
2511 : 760 : fprintf (f, " Clone of %s\n", clone_of->dump_asm_name ());
2512 : 5835 : if (symtab->function_flags_ready)
2513 : 10534 : fprintf (f, " Availability: %s\n",
2514 : 5267 : cgraph_availability_names [get_availability ()]);
2515 : :
2516 : 5835 : if (profile_id)
2517 : 155 : fprintf (f, " Profile id: %i\n",
2518 : : profile_id);
2519 : 5835 : if (unit_id)
2520 : 134 : fprintf (f, " Unit id: %i\n",
2521 : : unit_id);
2522 : 5835 : cgraph_function_version_info *vi = function_version ();
2523 : 5835 : if (vi != NULL)
2524 : : {
2525 : 0 : fprintf (f, " Version info: ");
2526 : 0 : if (vi->prev != NULL)
2527 : : {
2528 : 0 : fprintf (f, "prev: ");
2529 : 0 : fprintf (f, "%s ", vi->prev->this_node->dump_asm_name ());
2530 : : }
2531 : 0 : if (vi->next != NULL)
2532 : : {
2533 : 0 : fprintf (f, "next: ");
2534 : 0 : fprintf (f, "%s ", vi->next->this_node->dump_asm_name ());
2535 : : }
2536 : 0 : if (vi->dispatcher_resolver != NULL_TREE)
2537 : 0 : fprintf (f, "dispatcher: %s",
2538 : 0 : lang_hooks.decl_printable_name (vi->dispatcher_resolver, 2));
2539 : :
2540 : 0 : fprintf (f, "\n");
2541 : : }
2542 : 5835 : fprintf (f, " Function flags:");
2543 : 5835 : if (count.initialized_p ())
2544 : : {
2545 : 3608 : fprintf (f, " count:");
2546 : 3608 : count.dump (f);
2547 : : }
2548 : 5835 : if (tp_first_run > 0)
2549 : 70 : fprintf (f, " first_run:%" PRId64, (int64_t) tp_first_run);
2550 : 5835 : if (cgraph_node *origin = nested_function_origin (this))
2551 : 0 : fprintf (f, " nested in:%s", origin->dump_asm_name ());
2552 : 5835 : if (gimple_has_body_p (decl))
2553 : 3850 : fprintf (f, " body");
2554 : 5835 : if (process)
2555 : 0 : fprintf (f, " process");
2556 : 5835 : if (local)
2557 : 1137 : fprintf (f, " local");
2558 : 5835 : if (redefined_extern_inline)
2559 : 0 : fprintf (f, " redefined_extern_inline");
2560 : 5835 : if (only_called_at_startup)
2561 : 397 : fprintf (f, " only_called_at_startup");
2562 : 5835 : if (only_called_at_exit)
2563 : 7 : fprintf (f, " only_called_at_exit");
2564 : 5835 : if (tm_clone)
2565 : 0 : fprintf (f, " tm_clone");
2566 : 5835 : if (calls_comdat_local)
2567 : 9 : fprintf (f, " calls_comdat_local");
2568 : 5835 : if (icf_merged)
2569 : 24 : fprintf (f, " icf_merged");
2570 : 5835 : if (merged_comdat)
2571 : 0 : fprintf (f, " merged_comdat");
2572 : 5835 : if (merged_extern_inline)
2573 : 0 : fprintf (f, " merged_extern_inline");
2574 : 5835 : if (split_part)
2575 : 23 : fprintf (f, " split_part");
2576 : 5835 : if (indirect_call_target)
2577 : 218 : fprintf (f, " indirect_call_target");
2578 : 5835 : if (nonfreeing_fn)
2579 : 343 : fprintf (f, " nonfreeing_fn");
2580 : 5835 : if (DECL_STATIC_CONSTRUCTOR (decl))
2581 : 48 : fprintf (f," static_constructor (priority:%i)", get_init_priority ());
2582 : 5835 : if (DECL_STATIC_DESTRUCTOR (decl))
2583 : 7 : fprintf (f," static_destructor (priority:%i)", get_fini_priority ());
2584 : 5835 : if (frequency == NODE_FREQUENCY_HOT)
2585 : 62 : fprintf (f, " hot");
2586 : 5835 : if (frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2587 : 40 : fprintf (f, " unlikely_executed");
2588 : 5835 : if (frequency == NODE_FREQUENCY_EXECUTED_ONCE)
2589 : 690 : fprintf (f, " executed_once");
2590 : 5835 : if (opt_for_fn (decl, optimize_size))
2591 : 176 : fprintf (f, " optimize_size");
2592 : 5835 : if (parallelized_function)
2593 : 0 : fprintf (f, " parallelized_function");
2594 : 5835 : if (DECL_IS_MALLOC (decl))
2595 : 72 : fprintf (f, " decl_is_malloc");
2596 : 5835 : if (DECL_IS_OPERATOR_NEW_P (decl))
2597 : 35 : fprintf (f, " %soperator_new",
2598 : 35 : DECL_IS_REPLACEABLE_OPERATOR (decl) ? "replaceable_" : "");
2599 : 5835 : if (DECL_IS_OPERATOR_DELETE_P (decl))
2600 : 26 : fprintf (f, " %soperator_delete",
2601 : 26 : DECL_IS_REPLACEABLE_OPERATOR (decl) ? "replaceable_" : "");
2602 : :
2603 : 5835 : if (DECL_STATIC_CHAIN (decl))
2604 : 6 : fprintf (f, " static_chain");
2605 : :
2606 : 5835 : fprintf (f, "\n");
2607 : :
2608 : 5835 : if (thunk)
2609 : : {
2610 : 49 : fprintf (f, " Thunk");
2611 : 49 : thunk_info::get (this)->dump (f);
2612 : : }
2613 : 5786 : else if (former_thunk_p ())
2614 : : {
2615 : 21 : fprintf (f, " Former thunk ");
2616 : 21 : thunk_info::get (this)->dump (f);
2617 : : }
2618 : 5765 : else gcc_checking_assert (!thunk_info::get (this));
2619 : :
2620 : 5835 : fprintf (f, " Called by: ");
2621 : :
2622 : 5835 : profile_count sum = profile_count::zero ();
2623 : 13531 : for (edge = callers; edge; edge = edge->next_caller)
2624 : : {
2625 : 7696 : fprintf (f, "%s ", edge->caller->dump_asm_name ());
2626 : 7696 : edge->dump_edge_flags (f);
2627 : 7696 : if (edge->count.initialized_p ())
2628 : 7436 : sum += edge->count.ipa ();
2629 : : }
2630 : :
2631 : 5835 : fprintf (f, "\n Calls: ");
2632 : 20006 : for (edge = callees; edge; edge = edge->next_callee)
2633 : : {
2634 : 14171 : fprintf (f, "%s ", edge->callee->dump_asm_name ());
2635 : 14171 : edge->dump_edge_flags (f);
2636 : : }
2637 : 5835 : fprintf (f, "\n");
2638 : :
2639 : 5835 : if (!body_removed && count.ipa ().initialized_p ())
2640 : : {
2641 : 110 : bool ok = true;
2642 : 110 : bool min = false;
2643 : : ipa_ref *ref;
2644 : :
2645 : 110 : FOR_EACH_ALIAS (this, ref)
2646 : 0 : if (dyn_cast <cgraph_node *> (ref->referring)->count.initialized_p ())
2647 : 0 : sum += dyn_cast <cgraph_node *> (ref->referring)->count.ipa ();
2648 : :
2649 : 110 : if (inlined_to
2650 : 110 : || (symtab->state < EXPANSION
2651 : 110 : && ultimate_alias_target () == this && only_called_directly_p ()))
2652 : 3 : ok = !count.ipa ().differs_from_p (sum);
2653 : 107 : else if (count.ipa () > profile_count::from_gcov_type (100)
2654 : 107 : && count.ipa () < sum.apply_scale (99, 100))
2655 : 0 : ok = false, min = true;
2656 : 110 : if (!ok)
2657 : : {
2658 : 0 : fprintf (f, " Invalid sum of caller counts ");
2659 : 0 : sum.dump (f);
2660 : 0 : if (min)
2661 : 0 : fprintf (f, ", should be at most ");
2662 : : else
2663 : 0 : fprintf (f, ", should be ");
2664 : 0 : count.ipa ().dump (f);
2665 : 0 : fprintf (f, "\n");
2666 : : }
2667 : : }
2668 : :
2669 : 6602 : for (edge = indirect_calls; edge; edge = edge->next_callee)
2670 : : {
2671 : 767 : if (edge->indirect_info->polymorphic)
2672 : : {
2673 : 293 : fprintf (f, " Polymorphic indirect call of type ");
2674 : 293 : print_generic_expr (f, edge->indirect_info->otr_type, TDF_SLIM);
2675 : 293 : fprintf (f, " token:%i", (int) edge->indirect_info->otr_token);
2676 : : }
2677 : : else
2678 : 474 : fprintf (f, " Indirect call");
2679 : 767 : edge->dump_edge_flags (f);
2680 : 767 : if (edge->indirect_info->param_index != -1)
2681 : : {
2682 : 217 : fprintf (f, "of param:%i ", edge->indirect_info->param_index);
2683 : 217 : if (edge->indirect_info->agg_contents)
2684 : 19 : fprintf (f, "loaded from %s %s at offset %i ",
2685 : 19 : edge->indirect_info->member_ptr ? "member ptr" : "aggregate",
2686 : 19 : edge->indirect_info->by_ref ? "passed by reference" : "",
2687 : 19 : (int)edge->indirect_info->offset);
2688 : 217 : if (edge->indirect_info->vptr_changed)
2689 : 23 : fprintf (f, "(vptr maybe changed) ");
2690 : : }
2691 : 767 : fprintf (f, "num speculative call targets: %i\n",
2692 : 767 : edge->indirect_info->num_speculative_call_targets);
2693 : 767 : if (edge->indirect_info->polymorphic)
2694 : 293 : edge->indirect_info->context.dump (f);
2695 : : }
2696 : 5835 : }
2697 : :
2698 : : /* Dump call graph node to file F in graphviz format. */
2699 : :
2700 : : void
2701 : 0 : cgraph_node::dump_graphviz (FILE *f)
2702 : : {
2703 : 0 : cgraph_edge *edge;
2704 : :
2705 : 0 : for (edge = callees; edge; edge = edge->next_callee)
2706 : : {
2707 : 0 : cgraph_node *callee = edge->callee;
2708 : :
2709 : 0 : fprintf (f, "\t\"%s\" -> \"%s\"\n", dump_name (), callee->dump_name ());
2710 : : }
2711 : 0 : }
2712 : :
2713 : :
2714 : : /* Dump call graph node NODE to stderr. */
2715 : :
2716 : : DEBUG_FUNCTION void
2717 : 0 : cgraph_node::debug (void)
2718 : : {
2719 : 0 : dump (stderr);
2720 : 0 : }
2721 : :
2722 : : /* Dump the callgraph to file F. */
2723 : :
2724 : : void
2725 : 77 : cgraph_node::dump_cgraph (FILE *f)
2726 : : {
2727 : 77 : cgraph_node *node;
2728 : :
2729 : 77 : fprintf (f, "callgraph:\n\n");
2730 : 724 : FOR_EACH_FUNCTION (node)
2731 : 285 : node->dump (f);
2732 : 77 : }
2733 : :
2734 : : /* Return true when the DECL can possibly be inlined. */
2735 : :
2736 : : bool
2737 : 84605721 : cgraph_function_possibly_inlined_p (tree decl)
2738 : : {
2739 : 84605721 : if (!symtab->global_info_ready)
2740 : 77108371 : return !DECL_UNINLINABLE (decl);
2741 : 7497350 : return DECL_POSSIBLY_INLINED (decl);
2742 : : }
2743 : :
2744 : : /* Return function availability. See cgraph.h for description of individual
2745 : : return values. */
2746 : : enum availability
2747 : 806985841 : cgraph_node::get_availability (symtab_node *ref)
2748 : : {
2749 : 806985841 : if (ref)
2750 : : {
2751 : 570089931 : cgraph_node *cref = dyn_cast <cgraph_node *> (ref);
2752 : 570089931 : if (cref)
2753 : 570089931 : ref = cref->inlined_to;
2754 : : }
2755 : 806985841 : enum availability avail;
2756 : 806985841 : if (!analyzed && !in_other_partition)
2757 : 482137976 : avail = AVAIL_NOT_AVAILABLE;
2758 : 324847865 : else if (local)
2759 : 91500261 : avail = AVAIL_LOCAL;
2760 : 233347604 : else if (inlined_to)
2761 : 2235557 : avail = AVAIL_AVAILABLE;
2762 : 231112047 : else if (transparent_alias)
2763 : 134 : ultimate_alias_target (&avail, ref);
2764 : 231111913 : else if (ifunc_resolver
2765 : 231111913 : || lookup_attribute ("noipa", DECL_ATTRIBUTES (decl)))
2766 : 3422910 : avail = AVAIL_INTERPOSABLE;
2767 : 227689003 : else if (!externally_visible)
2768 : 28671272 : avail = AVAIL_AVAILABLE;
2769 : : /* If this is a reference from symbol itself and there are no aliases, we
2770 : : may be sure that the symbol was not interposed by something else because
2771 : : the symbol itself would be unreachable otherwise.
2772 : :
2773 : : Also comdat groups are always resolved in groups. */
2774 : 34777 : else if ((this == ref && !has_aliases_p ())
2775 : 199018476 : || (ref && get_comdat_group ()
2776 : 1512501 : && get_comdat_group () == ref->get_comdat_group ()))
2777 : 37541 : avail = AVAIL_AVAILABLE;
2778 : : /* Inline functions are safe to be analyzed even if their symbol can
2779 : : be overwritten at runtime. It is not meaningful to enforce any sane
2780 : : behavior on replacing inline function by different body. */
2781 : 198980190 : else if (DECL_DECLARED_INLINE_P (decl))
2782 : 69279584 : avail = AVAIL_AVAILABLE;
2783 : :
2784 : : /* If the function can be overwritten, return OVERWRITABLE. Take
2785 : : care at least of two notable extensions - the COMDAT functions
2786 : : used to share template instantiations in C++ (this is symmetric
2787 : : to code cp_cannot_inline_tree_fn and probably shall be shared and
2788 : : the inlinability hooks completely eliminated). */
2789 : :
2790 : 129700606 : else if (decl_replaceable_p (decl, semantic_interposition)
2791 : 129700606 : && !DECL_EXTERNAL (decl))
2792 : 9348658 : avail = AVAIL_INTERPOSABLE;
2793 : 120351948 : else avail = AVAIL_AVAILABLE;
2794 : :
2795 : 806985841 : return avail;
2796 : : }
2797 : :
2798 : : /* Worker for cgraph_node_can_be_local_p. */
2799 : : static bool
2800 : 853079 : cgraph_node_cannot_be_local_p_1 (cgraph_node *node, void *)
2801 : : {
2802 : 853079 : return !(!node->force_output
2803 : 832356 : && !node->ifunc_resolver
2804 : : /* Limitation of gas requires us to output targets of symver aliases
2805 : : as global symbols. This is binutils PR 25295. */
2806 : 832316 : && !node->symver
2807 : 832316 : && ((DECL_COMDAT (node->decl)
2808 : 355347 : && !node->forced_by_abi
2809 : 332934 : && !node->used_from_object_file_p ()
2810 : 332934 : && !node->same_comdat_group)
2811 : 547806 : || !node->externally_visible)
2812 : 428958 : && !DECL_STATIC_CONSTRUCTOR (node->decl)
2813 : 427207 : && !DECL_STATIC_DESTRUCTOR (node->decl));
2814 : : }
2815 : :
2816 : : /* Return true if cgraph_node can be made local for API change.
2817 : : Extern inline functions and C++ COMDAT functions can be made local
2818 : : at the expense of possible code size growth if function is used in multiple
2819 : : compilation units. */
2820 : : bool
2821 : 1161016 : cgraph_node::can_be_local_p (void)
2822 : : {
2823 : 1161016 : return (!address_taken
2824 : 1161016 : && !call_for_symbol_thunks_and_aliases (cgraph_node_cannot_be_local_p_1,
2825 : 1161016 : NULL, true));
2826 : : }
2827 : :
2828 : : /* Call callback on cgraph_node, thunks and aliases associated to cgraph_node.
2829 : : When INCLUDE_OVERWRITABLE is false, overwritable symbols are
2830 : : skipped. When EXCLUDE_VIRTUAL_THUNKS is true, virtual thunks are
2831 : : skipped. */
2832 : : bool
2833 : 126580793 : cgraph_node::call_for_symbol_thunks_and_aliases (bool (*callback)
2834 : : (cgraph_node *, void *),
2835 : : void *data,
2836 : : bool include_overwritable,
2837 : : bool exclude_virtual_thunks)
2838 : : {
2839 : 126580793 : cgraph_edge *e;
2840 : 126580793 : ipa_ref *ref;
2841 : 126580793 : enum availability avail = AVAIL_AVAILABLE;
2842 : :
2843 : 126580793 : if (include_overwritable
2844 : 126580793 : || (avail = get_availability ()) > AVAIL_INTERPOSABLE)
2845 : : {
2846 : 126569263 : if (callback (this, data))
2847 : : return true;
2848 : : }
2849 : 133701807 : FOR_EACH_ALIAS (this, ref)
2850 : : {
2851 : 12457107 : cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2852 : 12457107 : if (include_overwritable
2853 : 12457107 : || alias->get_availability () > AVAIL_INTERPOSABLE)
2854 : 12456865 : if (alias->call_for_symbol_thunks_and_aliases (callback, data,
2855 : : include_overwritable,
2856 : : exclude_virtual_thunks))
2857 : : return true;
2858 : : }
2859 : 121244700 : if (avail <= AVAIL_INTERPOSABLE)
2860 : : return false;
2861 : 126743949 : for (e = callers; e; e = e->next_caller)
2862 : 5510779 : if (e->caller->thunk
2863 : 2786 : && (include_overwritable
2864 : 426 : || e->caller->get_availability () > AVAIL_INTERPOSABLE)
2865 : 5513565 : && !(exclude_virtual_thunks
2866 : 22 : && thunk_info::get (e->caller)->virtual_offset_p))
2867 : 2774 : if (e->caller->call_for_symbol_thunks_and_aliases (callback, data,
2868 : : include_overwritable,
2869 : : exclude_virtual_thunks))
2870 : : return true;
2871 : :
2872 : : return false;
2873 : : }
2874 : :
2875 : : /* Worker to bring NODE local. */
2876 : :
2877 : : bool
2878 : 0 : cgraph_node::make_local (cgraph_node *node, void *)
2879 : : {
2880 : 0 : gcc_checking_assert (node->can_be_local_p ());
2881 : 0 : if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2882 : : {
2883 : 0 : node->make_decl_local ();
2884 : 0 : node->set_section (NULL);
2885 : 0 : node->set_comdat_group (NULL);
2886 : 0 : node->externally_visible = false;
2887 : 0 : node->forced_by_abi = false;
2888 : 0 : node->local = true;
2889 : 0 : node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
2890 : 0 : || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
2891 : 0 : && !flag_incremental_link);
2892 : 0 : node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2893 : 0 : gcc_assert (node->get_availability () == AVAIL_LOCAL);
2894 : : }
2895 : 0 : return false;
2896 : : }
2897 : :
2898 : : /* Bring cgraph node local. */
2899 : :
2900 : : void
2901 : 0 : cgraph_node::make_local (void)
2902 : : {
2903 : 0 : call_for_symbol_thunks_and_aliases (cgraph_node::make_local, NULL, true);
2904 : 0 : }
2905 : :
2906 : : /* Worker to set nothrow flag. */
2907 : :
2908 : : static void
2909 : 924835 : set_nothrow_flag_1 (cgraph_node *node, bool nothrow, bool non_call,
2910 : : bool *changed)
2911 : : {
2912 : 924835 : cgraph_edge *e;
2913 : :
2914 : 924835 : if (nothrow && !TREE_NOTHROW (node->decl))
2915 : : {
2916 : : /* With non-call exceptions we can't say for sure if other function body
2917 : : was not possibly optimized to still throw. */
2918 : 924810 : if (!non_call || node->binds_to_current_def_p ())
2919 : : {
2920 : 919088 : TREE_NOTHROW (node->decl) = true;
2921 : 919088 : *changed = true;
2922 : 2257032 : for (e = node->callers; e; e = e->next_caller)
2923 : 1337944 : e->can_throw_external = false;
2924 : : }
2925 : : }
2926 : 0 : else if (!nothrow && TREE_NOTHROW (node->decl))
2927 : : {
2928 : 0 : TREE_NOTHROW (node->decl) = false;
2929 : 0 : *changed = true;
2930 : : }
2931 : : ipa_ref *ref;
2932 : 979736 : FOR_EACH_ALIAS (node, ref)
2933 : : {
2934 : 54901 : cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2935 : 54901 : if (!nothrow || alias->get_availability () > AVAIL_INTERPOSABLE)
2936 : 54476 : set_nothrow_flag_1 (alias, nothrow, non_call, changed);
2937 : : }
2938 : 2300223 : for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2939 : 1375388 : if (e->caller->thunk
2940 : 1375388 : && (!nothrow || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2941 : 135 : set_nothrow_flag_1 (e->caller, nothrow, non_call, changed);
2942 : 924835 : }
2943 : :
2944 : : /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2945 : : if any to NOTHROW. */
2946 : :
2947 : : bool
2948 : 882139 : cgraph_node::set_nothrow_flag (bool nothrow)
2949 : : {
2950 : 882139 : bool changed = false;
2951 : 882139 : bool non_call = opt_for_fn (decl, flag_non_call_exceptions);
2952 : :
2953 : 882139 : if (!nothrow || get_availability () > AVAIL_INTERPOSABLE)
2954 : 870115 : set_nothrow_flag_1 (this, nothrow, non_call, &changed);
2955 : : else
2956 : : {
2957 : : ipa_ref *ref;
2958 : :
2959 : 19357 : FOR_EACH_ALIAS (this, ref)
2960 : : {
2961 : 7333 : cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2962 : 7333 : if (!nothrow || alias->get_availability () > AVAIL_INTERPOSABLE)
2963 : 109 : set_nothrow_flag_1 (alias, nothrow, non_call, &changed);
2964 : : }
2965 : : }
2966 : 882139 : return changed;
2967 : : }
2968 : :
2969 : : /* Worker to set malloc flag. */
2970 : : static void
2971 : 23196 : set_malloc_flag_1 (cgraph_node *node, bool malloc_p, bool *changed)
2972 : : {
2973 : 23196 : if (malloc_p && !DECL_IS_MALLOC (node->decl))
2974 : : {
2975 : 22782 : DECL_IS_MALLOC (node->decl) = true;
2976 : 22782 : *changed = true;
2977 : : }
2978 : :
2979 : : ipa_ref *ref;
2980 : 23197 : FOR_EACH_ALIAS (node, ref)
2981 : : {
2982 : 1 : cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
2983 : 1 : if (!malloc_p || alias->get_availability () > AVAIL_INTERPOSABLE)
2984 : 1 : set_malloc_flag_1 (alias, malloc_p, changed);
2985 : : }
2986 : :
2987 : 51411 : for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2988 : 28215 : if (e->caller->thunk
2989 : 28215 : && (!malloc_p || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2990 : 0 : set_malloc_flag_1 (e->caller, malloc_p, changed);
2991 : 23196 : }
2992 : :
2993 : : /* Set DECL_IS_MALLOC on NODE's decl and on NODE's aliases if any. */
2994 : :
2995 : : bool
2996 : 23195 : cgraph_node::set_malloc_flag (bool malloc_p)
2997 : : {
2998 : 23195 : bool changed = false;
2999 : :
3000 : 23195 : if (!malloc_p || get_availability () > AVAIL_INTERPOSABLE)
3001 : 23195 : set_malloc_flag_1 (this, malloc_p, &changed);
3002 : : else
3003 : : {
3004 : : ipa_ref *ref;
3005 : :
3006 : 0 : FOR_EACH_ALIAS (this, ref)
3007 : : {
3008 : 0 : cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
3009 : 0 : if (!malloc_p || alias->get_availability () > AVAIL_INTERPOSABLE)
3010 : 0 : set_malloc_flag_1 (alias, malloc_p, &changed);
3011 : : }
3012 : : }
3013 : 23195 : return changed;
3014 : : }
3015 : :
3016 : : /* Worker to set malloc flag. */
3017 : : static void
3018 : 230403 : add_detected_attribute_1 (cgraph_node *node, const char *attr, bool *changed)
3019 : : {
3020 : 230403 : if (!lookup_attribute (attr, DECL_ATTRIBUTES (node->decl)))
3021 : : {
3022 : 205810 : DECL_ATTRIBUTES (node->decl) = tree_cons (get_identifier (attr),
3023 : 205810 : NULL_TREE, DECL_ATTRIBUTES (node->decl));
3024 : 205810 : *changed = true;
3025 : : }
3026 : :
3027 : : ipa_ref *ref;
3028 : 230880 : FOR_EACH_ALIAS (node, ref)
3029 : : {
3030 : 477 : cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
3031 : 477 : if (alias->get_availability () > AVAIL_INTERPOSABLE)
3032 : 68 : add_detected_attribute_1 (alias, attr, changed);
3033 : : }
3034 : :
3035 : 744279 : for (cgraph_edge *e = node->callers; e; e = e->next_caller)
3036 : 513876 : if (e->caller->thunk
3037 : 513876 : && (e->caller->get_availability () > AVAIL_INTERPOSABLE))
3038 : 14 : add_detected_attribute_1 (e->caller, attr, changed);
3039 : 230403 : }
3040 : :
3041 : : /* Add attribyte ATTR to function and its aliases. */
3042 : :
3043 : : bool
3044 : 234081 : cgraph_node::add_detected_attribute (const char *attr)
3045 : : {
3046 : 234081 : bool changed = false;
3047 : :
3048 : 234081 : if (get_availability () > AVAIL_INTERPOSABLE)
3049 : 230321 : add_detected_attribute_1 (this, attr, &changed);
3050 : : else
3051 : : {
3052 : : ipa_ref *ref;
3053 : :
3054 : 3784 : FOR_EACH_ALIAS (this, ref)
3055 : : {
3056 : 24 : cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
3057 : 24 : if (alias->get_availability () > AVAIL_INTERPOSABLE)
3058 : 0 : add_detected_attribute_1 (alias, attr, &changed);
3059 : : }
3060 : : }
3061 : 234081 : return changed;
3062 : : }
3063 : :
3064 : : /* Worker to set noreturng flag. */
3065 : : static void
3066 : 28108 : set_noreturn_flag_1 (cgraph_node *node, bool noreturn_p, bool *changed)
3067 : : {
3068 : 28108 : if (noreturn_p && !TREE_THIS_VOLATILE (node->decl))
3069 : : {
3070 : 28108 : TREE_THIS_VOLATILE (node->decl) = true;
3071 : 28108 : *changed = true;
3072 : : }
3073 : :
3074 : : ipa_ref *ref;
3075 : 28771 : FOR_EACH_ALIAS (node, ref)
3076 : : {
3077 : 663 : cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
3078 : 663 : if (!noreturn_p || alias->get_availability () > AVAIL_INTERPOSABLE)
3079 : 663 : set_noreturn_flag_1 (alias, noreturn_p, changed);
3080 : : }
3081 : :
3082 : 45312 : for (cgraph_edge *e = node->callers; e; e = e->next_caller)
3083 : 17204 : if (e->caller->thunk
3084 : 17204 : && (!noreturn_p || e->caller->get_availability () > AVAIL_INTERPOSABLE))
3085 : 20 : set_noreturn_flag_1 (e->caller, noreturn_p, changed);
3086 : 28108 : }
3087 : :
3088 : : /* Set TREE_THIS_VOLATILE on NODE's decl and on NODE's aliases if any. */
3089 : :
3090 : : bool
3091 : 27579 : cgraph_node::set_noreturn_flag (bool noreturn_p)
3092 : : {
3093 : 27579 : bool changed = false;
3094 : :
3095 : 27579 : if (!noreturn_p || get_availability () > AVAIL_INTERPOSABLE)
3096 : 27416 : set_noreturn_flag_1 (this, noreturn_p, &changed);
3097 : : else
3098 : : {
3099 : : ipa_ref *ref;
3100 : :
3101 : 180 : FOR_EACH_ALIAS (this, ref)
3102 : : {
3103 : 17 : cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
3104 : 17 : if (!noreturn_p || alias->get_availability () > AVAIL_INTERPOSABLE)
3105 : 9 : set_noreturn_flag_1 (alias, noreturn_p, &changed);
3106 : : }
3107 : : }
3108 : 27579 : return changed;
3109 : : }
3110 : :
3111 : : /* Worker to set_const_flag. */
3112 : :
3113 : : static void
3114 : 1078789 : set_const_flag_1 (cgraph_node *node, bool set_const, bool looping,
3115 : : bool *changed)
3116 : : {
3117 : : /* Static constructors and destructors without a side effect can be
3118 : : optimized out. */
3119 : 1078789 : if (set_const && !looping)
3120 : : {
3121 : 1072837 : if (DECL_STATIC_CONSTRUCTOR (node->decl))
3122 : : {
3123 : 231 : DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
3124 : 231 : *changed = true;
3125 : : }
3126 : 1072837 : if (DECL_STATIC_DESTRUCTOR (node->decl))
3127 : : {
3128 : 1 : DECL_STATIC_DESTRUCTOR (node->decl) = 0;
3129 : 1 : *changed = true;
3130 : : }
3131 : : }
3132 : 1078789 : if (!set_const)
3133 : : {
3134 : 2194 : if (TREE_READONLY (node->decl))
3135 : : {
3136 : 159 : TREE_READONLY (node->decl) = 0;
3137 : 159 : DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
3138 : 159 : *changed = true;
3139 : : }
3140 : : }
3141 : : else
3142 : : {
3143 : : /* Consider function:
3144 : :
3145 : : bool a(int *p)
3146 : : {
3147 : : return *p==*p;
3148 : : }
3149 : :
3150 : : During early optimization we will turn this into:
3151 : :
3152 : : bool a(int *p)
3153 : : {
3154 : : return true;
3155 : : }
3156 : :
3157 : : Now if this function will be detected as CONST however when interposed
3158 : : it may end up being just pure. We always must assume the worst
3159 : : scenario here. */
3160 : 1076595 : if (TREE_READONLY (node->decl))
3161 : : {
3162 : 737 : if (!looping && DECL_LOOPING_CONST_OR_PURE_P (node->decl))
3163 : : {
3164 : 420 : DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
3165 : 420 : *changed = true;
3166 : : }
3167 : : }
3168 : 1075858 : else if (node->binds_to_current_def_p ())
3169 : : {
3170 : 171881 : TREE_READONLY (node->decl) = true;
3171 : 171881 : DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
3172 : 171881 : DECL_PURE_P (node->decl) = false;
3173 : 171881 : *changed = true;
3174 : : }
3175 : : else
3176 : : {
3177 : 903977 : if (dump_file && (dump_flags & TDF_DETAILS))
3178 : 0 : fprintf (dump_file, "Dropping state to PURE because function does "
3179 : : "not bind to current def.\n");
3180 : 903977 : if (!DECL_PURE_P (node->decl))
3181 : : {
3182 : 432717 : DECL_PURE_P (node->decl) = true;
3183 : 432717 : DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
3184 : 432717 : *changed = true;
3185 : : }
3186 : 471260 : else if (!looping && DECL_LOOPING_CONST_OR_PURE_P (node->decl))
3187 : : {
3188 : 143 : DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
3189 : 143 : *changed = true;
3190 : : }
3191 : : }
3192 : : }
3193 : :
3194 : : ipa_ref *ref;
3195 : 1218788 : FOR_EACH_ALIAS (node, ref)
3196 : : {
3197 : 139999 : cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
3198 : 139999 : if (!set_const || alias->get_availability () > AVAIL_INTERPOSABLE)
3199 : 139936 : set_const_flag_1 (alias, set_const, looping, changed);
3200 : : }
3201 : 1078885 : for (struct cgraph_node *n = node->simd_clones; n != NULL;
3202 : 96 : n = n->simdclone->next_clone)
3203 : 96 : set_const_flag_1 (n, set_const, looping, changed);
3204 : 2810433 : for (cgraph_edge *e = node->callers; e; e = e->next_caller)
3205 : 1731644 : if (e->caller->thunk
3206 : 1731644 : && (!set_const || e->caller->get_availability () > AVAIL_INTERPOSABLE))
3207 : : {
3208 : : /* Virtual thunks access virtual offset in the vtable, so they can
3209 : : only be pure, never const. */
3210 : 369 : if (set_const
3211 : 369 : && (thunk_info::get (e->caller)->virtual_offset_p
3212 : 238 : || !node->binds_to_current_def_p (e->caller)))
3213 : 131 : *changed |= e->caller->set_pure_flag (true, looping);
3214 : : else
3215 : 238 : set_const_flag_1 (e->caller, set_const, looping, changed);
3216 : : }
3217 : 1078789 : }
3218 : :
3219 : : /* If SET_CONST is true, mark function, aliases and thunks to be ECF_CONST.
3220 : : If SET_CONST if false, clear the flag.
3221 : :
3222 : : When setting the flag be careful about possible interposition and
3223 : : do not set the flag for functions that can be interposed and set pure
3224 : : flag for functions that can bind to other definition.
3225 : :
3226 : : Return true if any change was done. */
3227 : :
3228 : : bool
3229 : 964315 : cgraph_node::set_const_flag (bool set_const, bool looping)
3230 : : {
3231 : 964315 : bool changed = false;
3232 : 964315 : if (!set_const || get_availability () > AVAIL_INTERPOSABLE)
3233 : 938324 : set_const_flag_1 (this, set_const, looping, &changed);
3234 : : else
3235 : : {
3236 : : ipa_ref *ref;
3237 : :
3238 : 26870 : FOR_EACH_ALIAS (this, ref)
3239 : : {
3240 : 879 : cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
3241 : 879 : if (!set_const || alias->get_availability () > AVAIL_INTERPOSABLE)
3242 : 195 : set_const_flag_1 (alias, set_const, looping, &changed);
3243 : : }
3244 : : }
3245 : 964315 : return changed;
3246 : : }
3247 : :
3248 : : /* Info used by set_pure_flag_1. */
3249 : :
3250 : : struct set_pure_flag_info
3251 : : {
3252 : : bool pure;
3253 : : bool looping;
3254 : : bool changed;
3255 : : };
3256 : :
3257 : : /* Worker to set_pure_flag. */
3258 : :
3259 : : static bool
3260 : 361501 : set_pure_flag_1 (cgraph_node *node, void *data)
3261 : : {
3262 : 361501 : struct set_pure_flag_info *info = (struct set_pure_flag_info *)data;
3263 : : /* Static constructors and destructors without a side effect can be
3264 : : optimized out. */
3265 : 361501 : if (info->pure && !info->looping)
3266 : : {
3267 : 295373 : if (DECL_STATIC_CONSTRUCTOR (node->decl))
3268 : : {
3269 : 0 : DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
3270 : 0 : info->changed = true;
3271 : : }
3272 : 295373 : if (DECL_STATIC_DESTRUCTOR (node->decl))
3273 : : {
3274 : 0 : DECL_STATIC_DESTRUCTOR (node->decl) = 0;
3275 : 0 : info->changed = true;
3276 : : }
3277 : : }
3278 : 361501 : if (info->pure)
3279 : : {
3280 : 359307 : if (!DECL_PURE_P (node->decl) && !TREE_READONLY (node->decl))
3281 : : {
3282 : 358719 : DECL_PURE_P (node->decl) = true;
3283 : 358719 : DECL_LOOPING_CONST_OR_PURE_P (node->decl) = info->looping;
3284 : 358719 : info->changed = true;
3285 : : }
3286 : 588 : else if (DECL_LOOPING_CONST_OR_PURE_P (node->decl)
3287 : 588 : && !info->looping)
3288 : : {
3289 : 311 : DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
3290 : 311 : info->changed = true;
3291 : : }
3292 : : }
3293 : : else
3294 : : {
3295 : 2194 : if (DECL_PURE_P (node->decl))
3296 : : {
3297 : 75 : DECL_PURE_P (node->decl) = false;
3298 : 75 : DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
3299 : 75 : info->changed = true;
3300 : : }
3301 : : }
3302 : 361501 : return false;
3303 : : }
3304 : :
3305 : : /* Set DECL_PURE_P on cgraph_node's decl and on aliases of the node
3306 : : if any to PURE.
3307 : :
3308 : : When setting the flag, be careful about possible interposition.
3309 : : Return true if any change was done. */
3310 : :
3311 : : bool
3312 : 372143 : cgraph_node::set_pure_flag (bool pure, bool looping)
3313 : : {
3314 : 372143 : struct set_pure_flag_info info = {pure, looping, false};
3315 : 372143 : call_for_symbol_thunks_and_aliases (set_pure_flag_1, &info, !pure, true);
3316 : 372143 : for (struct cgraph_node *n = simd_clones; n != NULL;
3317 : 0 : n = n->simdclone->next_clone)
3318 : 0 : set_pure_flag_1 (n, &info);
3319 : 372143 : return info.changed;
3320 : : }
3321 : :
3322 : : /* Return true when cgraph_node cannot return or throw and thus
3323 : : it is safe to ignore its side effects for IPA analysis. */
3324 : :
3325 : : bool
3326 : 14228251 : cgraph_node::cannot_return_p (void)
3327 : : {
3328 : 14228251 : int flags = flags_from_decl_or_type (decl);
3329 : 14228251 : if (!opt_for_fn (decl, flag_exceptions))
3330 : 4670711 : return (flags & ECF_NORETURN) != 0;
3331 : : else
3332 : 9557540 : return ((flags & (ECF_NORETURN | ECF_NOTHROW))
3333 : 9557540 : == (ECF_NORETURN | ECF_NOTHROW));
3334 : : }
3335 : :
3336 : : /* Return true when call of edge cannot lead to return from caller
3337 : : and thus it is safe to ignore its side effects for IPA analysis
3338 : : when computing side effects of the caller.
3339 : : FIXME: We could actually mark all edges that have no reaching
3340 : : patch to the exit block or throw to get better results. */
3341 : : bool
3342 : 2979729 : cgraph_edge::cannot_lead_to_return_p (void)
3343 : : {
3344 : 2979729 : if (caller->cannot_return_p ())
3345 : : return true;
3346 : 2893109 : if (indirect_unknown_callee)
3347 : : {
3348 : 99754 : int flags = indirect_info->ecf_flags;
3349 : 99754 : if (!opt_for_fn (caller->decl, flag_exceptions))
3350 : 19307 : return (flags & ECF_NORETURN) != 0;
3351 : : else
3352 : 80447 : return ((flags & (ECF_NORETURN | ECF_NOTHROW))
3353 : 80447 : == (ECF_NORETURN | ECF_NOTHROW));
3354 : : }
3355 : : else
3356 : 2793355 : return callee->cannot_return_p ();
3357 : : }
3358 : :
3359 : : /* Return true if the edge after scaling it profile by SCALE
3360 : : may be considered hot. */
3361 : :
3362 : : bool
3363 : 5367078 : cgraph_edge::maybe_hot_p (sreal scale)
3364 : : {
3365 : : /* Never consider calls in functions optimized for size hot. */
3366 : 5367078 : if (opt_for_fn (caller->decl, optimize_size))
3367 : : return false;
3368 : :
3369 : : /* If reliable IPA count is available, just use it. */
3370 : 5312616 : profile_count c = count.ipa ();
3371 : 5312616 : if (c.reliable_p ()
3372 : 5312616 : || (c.quality () == AFDO && c.nonzero_p ()))
3373 : 707753 : return maybe_hot_count_p (NULL, c * scale);
3374 : :
3375 : : /* In auto-FDO, count 0 may lead to hot code in case the
3376 : : call is simply not called often enough to receive some samples. */
3377 : 4604863 : if ((c.quality () == AFDO
3378 : 4604863 : || count.quality () == GUESSED_GLOBAL0_ADJUSTED)
3379 : 4604863 : && callee && callee->count.quality () == AFDO)
3380 : 0 : return maybe_hot_count_p (NULL, c.force_nonzero () * scale);
3381 : :
3382 : : /* See if we can determine hotness using caller frequency. */
3383 : 4604863 : if (caller->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED
3384 : 4601791 : || (callee
3385 : 4174997 : && callee->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED))
3386 : : return false;
3387 : 4598200 : if (caller->frequency > NODE_FREQUENCY_UNLIKELY_EXECUTED
3388 : 4598200 : && (callee
3389 : 4171406 : && callee->frequency <= NODE_FREQUENCY_EXECUTED_ONCE))
3390 : : return false;
3391 : : /* ??? This may make sense for hot functions determined by
3392 : : user attribute, but if function is hot by profile, it may
3393 : : contains non-hot calls. In most practical cases this case
3394 : : is handled by the reliable ipa count above, but i.e. after
3395 : : inlining function with no profile to function with profile
3396 : : we get here.. */
3397 : 4544473 : if (caller->frequency == NODE_FREQUENCY_HOT)
3398 : : return true;
3399 : :
3400 : : /* Use IPA count and if it s not available appy local heuristics. */
3401 : 4544471 : if (c.initialized_p ())
3402 : : {
3403 : : /* A special case; AFDO zero means that function may quite possibly
3404 : : be executed few times per execution. If scale is large, we still
3405 : : want to consider the call hot. */
3406 : 0 : if (c.quality () == AFDO)
3407 : 0 : c = c.force_nonzero ();
3408 : 0 : return maybe_hot_count_p (NULL, c * scale);
3409 : : }
3410 : 4544471 : if (!count.initialized_p ())
3411 : : return true;
3412 : 3307705 : cgraph_node *where = caller->inlined_to ? caller->inlined_to : caller;
3413 : 3307705 : if (!where->count.initialized_p ())
3414 : : return true;
3415 : 3307705 : c = count * scale;
3416 : 3307705 : if (caller->frequency == NODE_FREQUENCY_EXECUTED_ONCE)
3417 : : {
3418 : 69834 : if (c * 2 < where->count * 3)
3419 : : return false;
3420 : : }
3421 : 3237871 : else if (c * param_hot_bb_frequency_fraction < where->count)
3422 : : return false;
3423 : : return true;
3424 : : }
3425 : :
3426 : : /* Return true if the edge may be considered hot. */
3427 : :
3428 : : bool
3429 : 2178910 : cgraph_edge::maybe_hot_p ()
3430 : : {
3431 : 2178910 : return maybe_hot_p (1);
3432 : : }
3433 : :
3434 : : /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
3435 : :
3436 : : static bool
3437 : 829383 : nonremovable_p (cgraph_node *node, void *)
3438 : : {
3439 : 829383 : return !node->can_remove_if_no_direct_calls_and_refs_p ();
3440 : : }
3441 : :
3442 : : /* Return true if whole comdat group can be removed if there are no direct
3443 : : calls to THIS. */
3444 : :
3445 : : bool
3446 : 1047873 : cgraph_node::can_remove_if_no_direct_calls_p (bool will_inline)
3447 : : {
3448 : 1047873 : struct ipa_ref *ref;
3449 : :
3450 : : /* For local symbols or non-comdat group it is the same as
3451 : : can_remove_if_no_direct_calls_p. */
3452 : 1047873 : if (!externally_visible || !same_comdat_group)
3453 : : {
3454 : 838599 : if (DECL_EXTERNAL (decl))
3455 : : return true;
3456 : 838599 : if (address_taken)
3457 : : return false;
3458 : 802430 : return !call_for_symbol_and_aliases (nonremovable_p, NULL, true);
3459 : : }
3460 : :
3461 : 209274 : if (will_inline && address_taken)
3462 : : return false;
3463 : :
3464 : : /* Otherwise check if we can remove the symbol itself and then verify
3465 : : that only uses of the comdat groups are direct call to THIS
3466 : : or its aliases. */
3467 : 209274 : if (!can_remove_if_no_direct_calls_and_refs_p ())
3468 : : return false;
3469 : :
3470 : : /* Check that all refs come from within the comdat group. */
3471 : 400149 : for (int i = 0; iterate_referring (i, ref); i++)
3472 : 204259 : if (ref->referring->get_comdat_group () != get_comdat_group ())
3473 : : return false;
3474 : :
3475 : 195890 : struct cgraph_node *target = ultimate_alias_target ();
3476 : 195890 : for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
3477 : 594136 : next != this; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
3478 : : {
3479 : 210516 : if (!externally_visible)
3480 : 0 : continue;
3481 : 210516 : if (!next->alias
3482 : 210516 : && !next->can_remove_if_no_direct_calls_and_refs_p ())
3483 : : return false;
3484 : :
3485 : : /* If we see different symbol than THIS, be sure to check calls. */
3486 : 210516 : if (next->ultimate_alias_target () != target)
3487 : 23204 : for (cgraph_edge *e = next->callers; e; e = e->next_caller)
3488 : 4925 : if (e->caller->get_comdat_group () != get_comdat_group ()
3489 : 4925 : || will_inline)
3490 : : return false;
3491 : :
3492 : : /* If function is not being inlined, we care only about
3493 : : references outside of the comdat group. */
3494 : 208443 : if (!will_inline)
3495 : 209746 : for (int i = 0; next->iterate_referring (i, ref); i++)
3496 : 10623 : if (ref->referring->get_comdat_group () != get_comdat_group ())
3497 : : return false;
3498 : : }
3499 : : return true;
3500 : : }
3501 : :
3502 : : /* Return true when function cgraph_node can be expected to be removed
3503 : : from program when direct calls in this compilation unit are removed.
3504 : :
3505 : : As a special case COMDAT functions are
3506 : : cgraph_can_remove_if_no_direct_calls_p while the are not
3507 : : cgraph_only_called_directly_p (it is possible they are called from other
3508 : : unit)
3509 : :
3510 : : This function behaves as cgraph_only_called_directly_p because eliminating
3511 : : all uses of COMDAT function does not make it necessarily disappear from
3512 : : the program unless we are compiling whole program or we do LTO. In this
3513 : : case we know we win since dynamic linking will not really discard the
3514 : : linkonce section. */
3515 : :
3516 : : bool
3517 : 3213860 : cgraph_node::will_be_removed_from_program_if_no_direct_calls_p
3518 : : (bool will_inline)
3519 : : {
3520 : 3213860 : gcc_assert (!inlined_to);
3521 : 3213860 : if (DECL_EXTERNAL (decl))
3522 : : return true;
3523 : :
3524 : 3213860 : if (!in_lto_p && !flag_whole_program)
3525 : : {
3526 : : /* If the symbol is in comdat group, we need to verify that whole comdat
3527 : : group becomes unreachable. Technically we could skip references from
3528 : : within the group, too. */
3529 : 2960689 : if (!only_called_directly_p ())
3530 : : return false;
3531 : 715776 : if (same_comdat_group && externally_visible)
3532 : : {
3533 : 0 : struct cgraph_node *target = ultimate_alias_target ();
3534 : :
3535 : 0 : if (will_inline && address_taken)
3536 : : return true;
3537 : 0 : for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
3538 : 0 : next != this;
3539 : 0 : next = dyn_cast<cgraph_node *> (next->same_comdat_group))
3540 : : {
3541 : 0 : if (!externally_visible)
3542 : 0 : continue;
3543 : 0 : if (!next->alias
3544 : 0 : && !next->only_called_directly_p ())
3545 : : return false;
3546 : :
3547 : : /* If we see different symbol than THIS,
3548 : : be sure to check calls. */
3549 : 0 : if (next->ultimate_alias_target () != target)
3550 : 0 : for (cgraph_edge *e = next->callers; e; e = e->next_caller)
3551 : 0 : if (e->caller->get_comdat_group () != get_comdat_group ()
3552 : 0 : || will_inline)
3553 : : return false;
3554 : : }
3555 : : }
3556 : 715776 : return true;
3557 : : }
3558 : : else
3559 : 253171 : return can_remove_if_no_direct_calls_p (will_inline);
3560 : : }
3561 : :
3562 : :
3563 : : /* Worker for cgraph_only_called_directly_p. */
3564 : :
3565 : : static bool
3566 : 15860487 : cgraph_not_only_called_directly_p_1 (cgraph_node *node, void *)
3567 : : {
3568 : 15860487 : return !node->only_called_directly_or_aliased_p ();
3569 : : }
3570 : :
3571 : : /* Return true when function cgraph_node and all its aliases are only called
3572 : : directly.
3573 : : i.e. it is not externally visible, address was not taken and
3574 : : it is not used in any other non-standard way. */
3575 : :
3576 : : bool
3577 : 15793701 : cgraph_node::only_called_directly_p (void)
3578 : : {
3579 : 15793701 : gcc_assert (ultimate_alias_target () == this);
3580 : 15793701 : return !call_for_symbol_and_aliases (cgraph_not_only_called_directly_p_1,
3581 : 15793701 : NULL, true);
3582 : : }
3583 : :
3584 : :
3585 : : /* Collect all callers of NODE. Worker for collect_callers_of_node. */
3586 : :
3587 : : static bool
3588 : 128024 : collect_callers_of_node_1 (cgraph_node *node, void *data)
3589 : : {
3590 : 128024 : vec<cgraph_edge *> *redirect_callers = (vec<cgraph_edge *> *)data;
3591 : 128024 : cgraph_edge *cs;
3592 : 128024 : enum availability avail;
3593 : 128024 : node->ultimate_alias_target (&avail);
3594 : :
3595 : 128024 : if (avail > AVAIL_INTERPOSABLE)
3596 : 444300 : for (cs = node->callers; cs != NULL; cs = cs->next_caller)
3597 : 316276 : if (!cs->indirect_inlining_edge
3598 : 316276 : && !cs->caller->thunk)
3599 : 316230 : redirect_callers->safe_push (cs);
3600 : 128024 : return false;
3601 : : }
3602 : :
3603 : : /* Collect all callers of cgraph_node and its aliases that are known to lead to
3604 : : cgraph_node (i.e. are not overwritable). */
3605 : :
3606 : : auto_vec<cgraph_edge *>
3607 : 126045 : cgraph_node::collect_callers (void)
3608 : : {
3609 : 126045 : auto_vec<cgraph_edge *> redirect_callers;
3610 : 126045 : call_for_symbol_thunks_and_aliases (collect_callers_of_node_1,
3611 : : &redirect_callers, false);
3612 : 126045 : return redirect_callers;
3613 : : }
3614 : :
3615 : :
3616 : : /* Return TRUE if NODE2 a clone of NODE or is equivalent to it. Return
3617 : : optimistically true if this cannot be determined. */
3618 : :
3619 : : static bool
3620 : 34291 : clone_of_p (cgraph_node *node, cgraph_node *node2)
3621 : : {
3622 : 34291 : node = node->ultimate_alias_target ();
3623 : 34291 : node2 = node2->ultimate_alias_target ();
3624 : :
3625 : 34291 : if (node2->clone_of == node
3626 : 4041 : || node2->former_clone_of == node->decl)
3627 : : return true;
3628 : :
3629 : 4103 : if (!node->thunk && !node->former_thunk_p ())
3630 : : {
3631 : : while (node2
3632 : 11936 : && node->decl != node2->decl
3633 : 19894 : && node->decl != node2->former_clone_of)
3634 : 7957 : node2 = node2->clone_of;
3635 : 3979 : return node2 != NULL;
3636 : : }
3637 : :
3638 : : /* There are no virtual clones of thunks so check former_clone_of or if we
3639 : : might have skipped thunks because this adjustments are no longer
3640 : : necessary. */
3641 : 62 : while (node->thunk || node->former_thunk_p ())
3642 : : {
3643 : 62 : if (!thunk_info::get (node)->this_adjusting)
3644 : : return false;
3645 : : /* In case of instrumented expanded thunks, which can have multiple calls
3646 : : in them, we do not know how to continue and just have to be
3647 : : optimistic. The same applies if all calls have already been inlined
3648 : : into the thunk. */
3649 : 62 : if (!node->callees || node->callees->next_callee)
3650 : : return true;
3651 : 59 : node = node->callees->callee->ultimate_alias_target ();
3652 : :
3653 : 59 : clone_info *info = clone_info::get (node2);
3654 : 59 : if (!info || !info->param_adjustments
3655 : 118 : || info->param_adjustments->first_param_intact_p ())
3656 : 0 : return false;
3657 : 59 : if (node2->former_clone_of == node->decl
3658 : 59 : || node2->former_clone_of == node->former_clone_of)
3659 : : return true;
3660 : :
3661 : : cgraph_node *n2 = node2;
3662 : 0 : while (n2 && node->decl != n2->decl)
3663 : 0 : n2 = n2->clone_of;
3664 : 0 : if (n2)
3665 : : return true;
3666 : : }
3667 : :
3668 : : return false;
3669 : : }
3670 : :
3671 : : /* Verify edge count and frequency. */
3672 : :
3673 : : bool
3674 : 196836038 : cgraph_edge::verify_count ()
3675 : : {
3676 : 196836038 : bool error_found = false;
3677 : 196836038 : if (!count.verify ())
3678 : : {
3679 : 0 : error ("caller edge count invalid");
3680 : 0 : error_found = true;
3681 : : }
3682 : 196836038 : return error_found;
3683 : : }
3684 : :
3685 : : /* Switch to THIS_CFUN if needed and print STMT to stderr. */
3686 : : static void
3687 : 0 : cgraph_debug_gimple_stmt (function *this_cfun, gimple *stmt)
3688 : : {
3689 : 0 : bool fndecl_was_null = false;
3690 : : /* debug_gimple_stmt needs correct cfun */
3691 : 0 : if (cfun != this_cfun)
3692 : 0 : set_cfun (this_cfun);
3693 : : /* ...and an actual current_function_decl */
3694 : 0 : if (!current_function_decl)
3695 : : {
3696 : 0 : current_function_decl = this_cfun->decl;
3697 : 0 : fndecl_was_null = true;
3698 : : }
3699 : 0 : debug_gimple_stmt (stmt);
3700 : 0 : if (fndecl_was_null)
3701 : 0 : current_function_decl = NULL;
3702 : 0 : }
3703 : :
3704 : : /* Verify that call graph edge corresponds to DECL from the associated
3705 : : statement. Return true if the verification should fail. */
3706 : :
3707 : : bool
3708 : 97554442 : cgraph_edge::verify_corresponds_to_fndecl (tree decl)
3709 : : {
3710 : 97554442 : cgraph_node *node;
3711 : :
3712 : 97554442 : if (!decl || callee->inlined_to)
3713 : : return false;
3714 : 93735014 : if (symtab->state == LTO_STREAMING)
3715 : : return false;
3716 : 93735014 : node = cgraph_node::get (decl);
3717 : :
3718 : : /* We do not know if a node from a different partition is an alias or what it
3719 : : aliases and therefore cannot do the former_clone_of check reliably. When
3720 : : body_removed is set, we have lost all information about what was alias or
3721 : : thunk of and also cannot proceed. */
3722 : 93735014 : if (!node
3723 : 93640891 : || node->body_removed
3724 : 92903911 : || node->in_other_partition
3725 : 92903908 : || callee->icf_merged
3726 : 92807588 : || callee->in_other_partition)
3727 : : return false;
3728 : :
3729 : 92807588 : node = node->ultimate_alias_target ();
3730 : :
3731 : : /* Optimizers can redirect unreachable calls or calls triggering undefined
3732 : : behavior to __builtin_unreachable or __builtin_unreachable trap. */
3733 : :
3734 : 92807588 : if (fndecl_built_in_p (callee->decl, BUILT_IN_UNREACHABLE,
3735 : : BUILT_IN_UNREACHABLE_TRAP))
3736 : : return false;
3737 : :
3738 : 90090161 : if (callee->former_clone_of != node->decl
3739 : 90088473 : && (node != callee->ultimate_alias_target ())
3740 : 90124452 : && !clone_of_p (node, callee))
3741 : : return true;
3742 : : else
3743 : 90090161 : return false;
3744 : : }
3745 : :
3746 : : /* Disable warnings about missing quoting in GCC diagnostics for
3747 : : the verification errors. Their format strings don't follow GCC
3748 : : diagnostic conventions and the calls are ultimately followed by
3749 : : one to internal_error. */
3750 : : #if __GNUC__ >= 10
3751 : : # pragma GCC diagnostic push
3752 : : # pragma GCC diagnostic ignored "-Wformat-diag"
3753 : : #endif
3754 : :
3755 : : /* Verify consistency of speculative call in NODE corresponding to STMT
3756 : : and LTO_STMT_UID. If INDIRECT is set, assume that it is the indirect
3757 : : edge of call sequence. Return true if error is found.
3758 : :
3759 : : This function is called to every component of indirect call (direct edges,
3760 : : indirect edge and refs). To save duplicated work, do full testing only
3761 : : in that case. */
3762 : : static bool
3763 : 453443 : verify_speculative_call (struct cgraph_node *node, gimple *stmt,
3764 : : unsigned int lto_stmt_uid,
3765 : : struct cgraph_edge *indirect)
3766 : : {
3767 : 453443 : if (indirect == NULL)
3768 : : {
3769 : 449948 : for (indirect = node->indirect_calls; indirect;
3770 : 109136 : indirect = indirect->next_callee)
3771 : 449948 : if (indirect->call_stmt == stmt
3772 : 340930 : && indirect->lto_stmt_uid == lto_stmt_uid)
3773 : : break;
3774 : 340812 : if (!indirect)
3775 : : {
3776 : 0 : error ("missing indirect call in speculative call sequence");
3777 : 0 : return true;
3778 : : }
3779 : 340812 : if (!indirect->speculative)
3780 : : {
3781 : 0 : error ("indirect call in speculative call sequence has no "
3782 : : "speculative flag");
3783 : 0 : return true;
3784 : : }
3785 : : return false;
3786 : : }
3787 : :
3788 : : /* Maximal number of targets. We probably will never want to have more than
3789 : : this. */
3790 : : const unsigned int num = 256;
3791 : : cgraph_edge *direct_calls[num];
3792 : : ipa_ref *refs[num];
3793 : :
3794 : 28946167 : for (unsigned int i = 0; i < num; i++)
3795 : : {
3796 : 28833536 : direct_calls[i] = NULL;
3797 : 28833536 : refs[i] = NULL;
3798 : : }
3799 : :
3800 : 112631 : cgraph_edge *first_call = NULL;
3801 : 112631 : cgraph_edge *prev_call = NULL;
3802 : :
3803 : 658240 : for (cgraph_edge *direct = node->callees; direct;
3804 : 545609 : direct = direct->next_callee)
3805 : 545609 : if (direct->call_stmt == stmt && direct->lto_stmt_uid == lto_stmt_uid)
3806 : : {
3807 : 170406 : if (!first_call)
3808 : 112631 : first_call = direct;
3809 : 170406 : if (prev_call && direct != prev_call->next_callee)
3810 : : {
3811 : 0 : error ("speculative edges are not adjacent");
3812 : 0 : return true;
3813 : : }
3814 : 170406 : prev_call = direct;
3815 : 170406 : if (!direct->speculative)
3816 : : {
3817 : 0 : error ("direct call to %s in speculative call sequence has no "
3818 : 0 : "speculative flag", direct->callee->dump_name ());
3819 : 0 : return true;
3820 : : }
3821 : 170406 : if (direct->speculative_id >= num)
3822 : : {
3823 : 0 : error ("direct call to %s in speculative call sequence has "
3824 : : "speculative_id %i out of range",
3825 : 0 : direct->callee->dump_name (), direct->speculative_id);
3826 : 0 : return true;
3827 : : }
3828 : 170406 : if (direct_calls[direct->speculative_id])
3829 : : {
3830 : 0 : error ("duplicate direct call to %s in speculative call sequence "
3831 : : "with speculative_id %i",
3832 : 0 : direct->callee->dump_name (), direct->speculative_id);
3833 : 0 : return true;
3834 : : }
3835 : 170406 : direct_calls[direct->speculative_id] = direct;
3836 : : }
3837 : :
3838 : 112631 : if (first_call->call_stmt
3839 : 112631 : && first_call != node->get_edge (first_call->call_stmt))
3840 : : {
3841 : 0 : error ("call stmt hash does not point to first direct edge of "
3842 : : "speculative call sequence");
3843 : 0 : return true;
3844 : : }
3845 : :
3846 : : ipa_ref *ref;
3847 : 433471 : for (int i = 0; node->iterate_reference (i, ref); i++)
3848 : 320840 : if (ref->speculative
3849 : 262333 : && ref->stmt == stmt && ref->lto_stmt_uid == lto_stmt_uid)
3850 : : {
3851 : 170406 : if (ref->speculative_id >= num)
3852 : : {
3853 : 0 : error ("direct call to %s in speculative call sequence has "
3854 : : "speculative_id %i out of range",
3855 : 0 : ref->referred->dump_name (), ref->speculative_id);
3856 : 0 : return true;
3857 : : }
3858 : 170406 : if (refs[ref->speculative_id])
3859 : : {
3860 : 0 : error ("duplicate reference %s in speculative call sequence "
3861 : : "with speculative_id %i",
3862 : 0 : ref->referred->dump_name (), ref->speculative_id);
3863 : 0 : return true;
3864 : : }
3865 : 170406 : refs[ref->speculative_id] = ref;
3866 : : }
3867 : :
3868 : : int num_targets = 0;
3869 : 28946167 : for (unsigned int i = 0 ; i < num ; i++)
3870 : : {
3871 : 28833536 : if (refs[i] && !direct_calls[i])
3872 : : {
3873 : 0 : error ("missing direct call for speculation %i", i);
3874 : 0 : return true;
3875 : : }
3876 : 28833536 : if (!refs[i] && direct_calls[i])
3877 : : {
3878 : 0 : error ("missing ref for speculation %i", i);
3879 : 0 : return true;
3880 : : }
3881 : 28833536 : if (refs[i] != NULL)
3882 : 170406 : num_targets++;
3883 : : }
3884 : :
3885 : 112631 : if (num_targets != indirect->num_speculative_call_targets_p ())
3886 : : {
3887 : 0 : error ("number of speculative targets %i mismatched with "
3888 : : "num_speculative_call_targets %i",
3889 : : num_targets,
3890 : : indirect->num_speculative_call_targets_p ());
3891 : 0 : return true;
3892 : : }
3893 : : return false;
3894 : : }
3895 : :
3896 : : /* Verify cgraph nodes of given cgraph node. */
3897 : : DEBUG_FUNCTION void
3898 : 52238114 : cgraph_node::verify_node (void)
3899 : : {
3900 : 52238114 : cgraph_edge *e;
3901 : 52238114 : function *this_cfun = DECL_STRUCT_FUNCTION (decl);
3902 : 52238114 : basic_block this_block;
3903 : 52238114 : gimple_stmt_iterator gsi;
3904 : 52238114 : bool error_found = false;
3905 : 52238114 : int i;
3906 : 52238114 : ipa_ref *ref = NULL;
3907 : :
3908 : 52238114 : if (seen_error ())
3909 : 52238114 : return;
3910 : :
3911 : 52238114 : timevar_push (TV_CGRAPH_VERIFY);
3912 : 52238114 : error_found |= verify_base ();
3913 : 157481152 : for (e = callees; e; e = e->next_callee)
3914 : 105243038 : if (e->aux)
3915 : : {
3916 : 0 : error ("aux field set for edge %s->%s",
3917 : 0 : identifier_to_locale (e->caller->name ()),
3918 : 0 : identifier_to_locale (e->callee->name ()));
3919 : 0 : error_found = true;
3920 : : }
3921 : 52238114 : if (!count.verify ())
3922 : : {
3923 : 0 : error ("cgraph count invalid");
3924 : 0 : error_found = true;
3925 : : }
3926 : 52238114 : if (inlined_to && same_comdat_group)
3927 : : {
3928 : 0 : error ("inline clone in same comdat group list");
3929 : 0 : error_found = true;
3930 : : }
3931 : 52238114 : if (inlined_to && !count.compatible_p (inlined_to->count))
3932 : : {
3933 : 0 : error ("inline clone count is not compatible");
3934 : 0 : count.debug ();
3935 : 0 : inlined_to->count.debug ();
3936 : 0 : error_found = true;
3937 : : }
3938 : 52238114 : if (tp_first_run < 0)
3939 : : {
3940 : 0 : error ("tp_first_run must be non-negative");
3941 : 0 : error_found = true;
3942 : : }
3943 : 52238114 : if (!definition && !in_other_partition && local)
3944 : : {
3945 : 0 : error ("local symbols must be defined");
3946 : 0 : error_found = true;
3947 : : }
3948 : 52238114 : if (inlined_to && externally_visible)
3949 : : {
3950 : 0 : error ("externally visible inline clone");
3951 : 0 : error_found = true;
3952 : : }
3953 : 52238114 : if (inlined_to && address_taken)
3954 : : {
3955 : 0 : error ("inline clone with address taken");
3956 : 0 : error_found = true;
3957 : : }
3958 : 52238114 : if (inlined_to && force_output)
3959 : : {
3960 : 0 : error ("inline clone is forced to output");
3961 : 0 : error_found = true;
3962 : : }
3963 : 52238114 : if (symtab->state != LTO_STREAMING)
3964 : : {
3965 : 52127893 : if (calls_comdat_local && !same_comdat_group)
3966 : : {
3967 : 0 : error ("calls_comdat_local is set outside of a comdat group");
3968 : 0 : error_found = true;
3969 : : }
3970 : 52127893 : if (!inlined_to && calls_comdat_local != check_calls_comdat_local_p ())
3971 : : {
3972 : 0 : error ("invalid calls_comdat_local flag");
3973 : 0 : error_found = true;
3974 : : }
3975 : : }
3976 : 52238114 : if (DECL_IS_MALLOC (decl)
3977 : 52238114 : && !POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
3978 : : {
3979 : 0 : error ("malloc attribute should be used for a function that "
3980 : : "returns a pointer");
3981 : 0 : error_found = true;
3982 : : }
3983 : 52238114 : if (definition
3984 : 35293958 : && externally_visible
3985 : : /* For aliases in lto1 free_lang_data doesn't guarantee preservation
3986 : : of opt_for_fn (decl, flag_semantic_interposition). See PR105399. */
3987 : 19197956 : && (!alias || !in_lto_p)
3988 : 52238114 : && semantic_interposition
3989 : 19195036 : != opt_for_fn (decl, flag_semantic_interposition))
3990 : : {
3991 : 0 : error ("semantic interposition mismatch");
3992 : 0 : error_found = true;
3993 : : }
3994 : 54645374 : for (e = indirect_calls; e; e = e->next_callee)
3995 : : {
3996 : 2407260 : if (e->aux)
3997 : : {
3998 : 0 : error ("aux field set for indirect edge from %s",
3999 : 0 : identifier_to_locale (e->caller->name ()));
4000 : 0 : error_found = true;
4001 : : }
4002 : 2407260 : if (!e->count.compatible_p (count))
4003 : : {
4004 : 0 : error ("edge count is not compatible with function count");
4005 : 0 : e->count.debug ();
4006 : 0 : count.debug ();
4007 : 0 : error_found = true;
4008 : : }
4009 : 2407260 : if (inlined_to && !e->count.compatible_p (inlined_to->count))
4010 : : {
4011 : 0 : error ("edge count is not compatible with inlined to function count");
4012 : 0 : e->count.debug ();
4013 : 0 : count.debug ();
4014 : 0 : error_found = true;
4015 : : }
4016 : 2407260 : if (!e->indirect_unknown_callee
4017 : 2407260 : || !e->indirect_info)
4018 : : {
4019 : 0 : error ("An indirect edge from %s is not marked as indirect or has "
4020 : : "associated indirect_info, the corresponding statement is: ",
4021 : 0 : identifier_to_locale (e->caller->name ()));
4022 : 0 : cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
4023 : 0 : error_found = true;
4024 : : }
4025 : 2407260 : if (e->call_stmt && e->lto_stmt_uid)
4026 : : {
4027 : 0 : error ("edge has both call_stmt and lto_stmt_uid set");
4028 : 0 : error_found = true;
4029 : : }
4030 : : }
4031 : 52238114 : bool check_comdat = comdat_local_p ();
4032 : 141423854 : for (e = callers; e; e = e->next_caller)
4033 : : {
4034 : 89185740 : if (e->verify_count ())
4035 : 0 : error_found = true;
4036 : 89185740 : if (check_comdat
4037 : 89185740 : && !in_same_comdat_group_p (e->caller))
4038 : : {
4039 : 0 : error ("comdat-local function called by %s outside its comdat",
4040 : : identifier_to_locale (e->caller->name ()));
4041 : 0 : error_found = true;
4042 : : }
4043 : 89185740 : if (!e->inline_failed)
4044 : : {
4045 : 8321813 : if (inlined_to
4046 : 8321813 : != (e->caller->inlined_to
4047 : 8321813 : ? e->caller->inlined_to : e->caller))
4048 : : {
4049 : 0 : error ("inlined_to pointer is wrong");
4050 : 0 : error_found = true;
4051 : : }
4052 : 8321813 : if (callers->next_caller)
4053 : : {
4054 : 0 : error ("multiple inline callers");
4055 : 0 : error_found = true;
4056 : : }
4057 : : }
4058 : : else
4059 : 80863927 : if (inlined_to)
4060 : : {
4061 : 0 : error ("inlined_to pointer set for noninline callers");
4062 : 0 : error_found = true;
4063 : : }
4064 : : }
4065 : 157481152 : for (e = callees; e; e = e->next_callee)
4066 : : {
4067 : 105243038 : if (e->verify_count ())
4068 : 0 : error_found = true;
4069 : 105243038 : if (!e->count.compatible_p (count))
4070 : : {
4071 : 0 : error ("edge count is not compatible with function count");
4072 : 0 : e->count.debug ();
4073 : 0 : count.debug ();
4074 : 0 : error_found = true;
4075 : : }
4076 : 105243038 : if (gimple_has_body_p (e->caller->decl)
4077 : 99238398 : && !e->caller->inlined_to
4078 : 90632070 : && !e->speculative
4079 : 90570039 : && !e->callback
4080 : 90570034 : && !e->has_callback
4081 : : /* Optimized out calls are redirected to __builtin_unreachable. */
4082 : 90569999 : && (e->count.nonzero_p ()
4083 : 50612524 : || ! e->callee->decl
4084 : 50612524 : || !fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE,
4085 : : BUILT_IN_UNREACHABLE_TRAP))
4086 : : && count
4087 : 90091141 : == ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (decl))->count
4088 : 195325775 : && (!e->count.ipa_p ()
4089 : 41880091 : && e->count.differs_from_p (gimple_bb (e->call_stmt)->count)))
4090 : : {
4091 : 0 : error ("caller edge count does not match BB count");
4092 : 0 : fprintf (stderr, "edge count: ");
4093 : 0 : e->count.dump (stderr);
4094 : 0 : fprintf (stderr, "\n bb count: ");
4095 : 0 : gimple_bb (e->call_stmt)->count.dump (stderr);
4096 : 0 : fprintf (stderr, "\n");
4097 : 0 : error_found = true;
4098 : : }
4099 : 105243038 : if (e->call_stmt && e->lto_stmt_uid)
4100 : : {
4101 : 0 : error ("edge has both call_stmt and lto_stmt_uid set");
4102 : 0 : error_found = true;
4103 : : }
4104 : 105243038 : if (e->speculative
4105 : 105243038 : && verify_speculative_call (e->caller, e->call_stmt, e->lto_stmt_uid,
4106 : : NULL))
4107 : : error_found = true;
4108 : : }
4109 : 54645374 : for (e = indirect_calls; e; e = e->next_callee)
4110 : : {
4111 : 2407260 : if (e->verify_count ())
4112 : 0 : error_found = true;
4113 : 2407260 : if (gimple_has_body_p (e->caller->decl)
4114 : 2353046 : && !e->caller->inlined_to
4115 : 2090591 : && !e->speculative
4116 : 2053477 : && e->count.ipa_p ()
4117 : : && count
4118 : 757252 : == ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (decl))->count
4119 : 3164508 : && (!e->count.ipa_p ()
4120 : 0 : && e->count.differs_from_p (gimple_bb (e->call_stmt)->count)))
4121 : : {
4122 : 0 : error ("indirect call count does not match BB count");
4123 : 0 : fprintf (stderr, "edge count: ");
4124 : 0 : e->count.dump (stderr);
4125 : 0 : fprintf (stderr, "\n bb count: ");
4126 : 0 : gimple_bb (e->call_stmt)->count.dump (stderr);
4127 : 0 : fprintf (stderr, "\n");
4128 : 0 : error_found = true;
4129 : : }
4130 : 2407260 : if (e->speculative
4131 : 2407260 : && verify_speculative_call (e->caller, e->call_stmt, e->lto_stmt_uid,
4132 : : e))
4133 : : error_found = true;
4134 : : }
4135 : 118026920 : for (i = 0; iterate_reference (i, ref); i++)
4136 : : {
4137 : 65788806 : if (ref->stmt && ref->lto_stmt_uid)
4138 : : {
4139 : 0 : error ("reference has both stmt and lto_stmt_uid set");
4140 : 0 : error_found = true;
4141 : : }
4142 : 65788806 : if (ref->speculative
4143 : 65788806 : && verify_speculative_call (this, ref->stmt,
4144 : : ref->lto_stmt_uid, NULL))
4145 : : error_found = true;
4146 : : }
4147 : :
4148 : 52238114 : if (!callers && inlined_to)
4149 : : {
4150 : 0 : error ("inlined_to pointer is set but no predecessors found");
4151 : 0 : error_found = true;
4152 : : }
4153 : 52238114 : if (inlined_to == this)
4154 : : {
4155 : 0 : error ("inlined_to pointer refers to itself");
4156 : 0 : error_found = true;
4157 : : }
4158 : :
4159 : 52238114 : if (clone_of)
4160 : : {
4161 : 6024889 : cgraph_node *first_clone = clone_of->clones;
4162 : 6024889 : if (first_clone != this)
4163 : : {
4164 : 3078393 : if (prev_sibling_clone->clone_of != clone_of)
4165 : : {
4166 : 0 : error ("cgraph_node has wrong clone_of");
4167 : 0 : error_found = true;
4168 : : }
4169 : : }
4170 : : }
4171 : 52238114 : if (clones)
4172 : : {
4173 : : cgraph_node *n;
4174 : 8400955 : for (n = clones; n; n = n->next_sibling_clone)
4175 : 6766866 : if (n->clone_of != this)
4176 : : break;
4177 : 1634089 : if (n)
4178 : : {
4179 : 0 : error ("cgraph_node has wrong clone list");
4180 : 0 : error_found = true;
4181 : : }
4182 : : }
4183 : 52238114 : if ((prev_sibling_clone || next_sibling_clone) && !clone_of)
4184 : : {
4185 : 0 : error ("cgraph_node is in clone list but it is not clone");
4186 : 0 : error_found = true;
4187 : : }
4188 : 52238114 : if (!prev_sibling_clone && clone_of && clone_of->clones != this)
4189 : : {
4190 : 0 : error ("cgraph_node has wrong prev_clone pointer");
4191 : 0 : error_found = true;
4192 : : }
4193 : 52238114 : if (prev_sibling_clone && prev_sibling_clone->next_sibling_clone != this)
4194 : : {
4195 : 0 : error ("double linked list of clones corrupted");
4196 : 0 : error_found = true;
4197 : : }
4198 : :
4199 : 52238114 : if (analyzed && alias)
4200 : : {
4201 : 1443916 : bool ref_found = false;
4202 : 1443916 : int i;
4203 : 1443916 : ipa_ref *ref = NULL;
4204 : :
4205 : 1443916 : if (callees)
4206 : : {
4207 : 0 : error ("Alias has call edges");
4208 : 0 : error_found = true;
4209 : : }
4210 : 2887832 : for (i = 0; iterate_reference (i, ref); i++)
4211 : 1443916 : if (ref->use != IPA_REF_ALIAS)
4212 : : {
4213 : 0 : error ("Alias has non-alias reference");
4214 : 0 : error_found = true;
4215 : : }
4216 : 1443916 : else if (ref_found)
4217 : : {
4218 : 0 : error ("Alias has more than one alias reference");
4219 : 0 : error_found = true;
4220 : : }
4221 : : else
4222 : : ref_found = true;
4223 : 1443916 : if (!ref_found)
4224 : : {
4225 : 0 : error ("Analyzed alias has no reference");
4226 : 0 : error_found = true;
4227 : : }
4228 : : }
4229 : :
4230 : 52238114 : if (analyzed && thunk)
4231 : : {
4232 : 22198 : if (!callees)
4233 : : {
4234 : 0 : error ("No edge out of thunk node");
4235 : 0 : error_found = true;
4236 : : }
4237 : 22198 : else if (callees->next_callee)
4238 : : {
4239 : 0 : error ("More than one edge out of thunk node");
4240 : 0 : error_found = true;
4241 : : }
4242 : 22198 : if (gimple_has_body_p (decl) && !inlined_to)
4243 : : {
4244 : 0 : error ("Thunk is not supposed to have body");
4245 : 0 : error_found = true;
4246 : : }
4247 : : }
4248 : 35269798 : else if (analyzed && gimple_has_body_p (decl)
4249 : 30479438 : && !TREE_ASM_WRITTEN (decl)
4250 : 30479438 : && (!DECL_EXTERNAL (decl) || inlined_to)
4251 : 81839949 : && !flag_wpa)
4252 : : {
4253 : 29597689 : if ((this_cfun->curr_properties & PROP_assumptions_done) != 0)
4254 : : ;
4255 : 29597581 : else if (this_cfun->cfg)
4256 : : {
4257 : 29597581 : hash_set<gimple *> stmts;
4258 : :
4259 : : /* Reach the trees by walking over the CFG, and note the
4260 : : enclosing basic-blocks in the call edges. */
4261 : 246278849 : FOR_EACH_BB_FN (this_block, this_cfun)
4262 : : {
4263 : 216681268 : for (gsi = gsi_start_phis (this_block);
4264 : 262613144 : !gsi_end_p (gsi); gsi_next (&gsi))
4265 : 45931876 : stmts.add (gsi_stmt (gsi));
4266 : 433362536 : for (gsi = gsi_start_bb (this_block);
4267 : 1305184995 : !gsi_end_p (gsi);
4268 : 1088503727 : gsi_next (&gsi))
4269 : : {
4270 : 1088503727 : gimple *stmt = gsi_stmt (gsi);
4271 : 1088503727 : stmts.add (stmt);
4272 : 1088503727 : if (is_gimple_call (stmt))
4273 : : {
4274 : 104626674 : cgraph_edge *e = get_edge (stmt);
4275 : 104626674 : tree decl = gimple_call_fndecl (stmt);
4276 : 104626674 : if (e)
4277 : : {
4278 : 99783185 : if (e->aux)
4279 : : {
4280 : 0 : error ("shared call_stmt:");
4281 : 0 : cgraph_debug_gimple_stmt (this_cfun, stmt);
4282 : 0 : error_found = true;
4283 : : }
4284 : 99783185 : if (!e->indirect_unknown_callee)
4285 : : {
4286 : : /* Callback edges violate this assertion
4287 : : because their call statement doesn't exist,
4288 : : their associated statement belongs to the
4289 : : callback-dispatching function. */
4290 : 97554442 : if (!e->callback
4291 : 97554442 : && e->verify_corresponds_to_fndecl (decl))
4292 : : {
4293 : 0 : error ("edge points to wrong declaration:");
4294 : 0 : debug_tree (e->callee->decl);
4295 : 0 : fprintf (stderr," Instead of:");
4296 : 0 : debug_tree (decl);
4297 : 0 : error_found = true;
4298 : : }
4299 : : }
4300 : 2228743 : else if (decl)
4301 : : {
4302 : 0 : error ("an indirect edge with unknown callee "
4303 : : "corresponding to a call_stmt with "
4304 : : "a known declaration:");
4305 : 0 : error_found = true;
4306 : 0 : cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
4307 : : }
4308 : 99783185 : e->aux = (void *)1;
4309 : : }
4310 : 4843489 : else if (decl)
4311 : : {
4312 : 0 : error ("missing callgraph edge for call stmt:");
4313 : 0 : cgraph_debug_gimple_stmt (this_cfun, stmt);
4314 : 0 : error_found = true;
4315 : : }
4316 : : }
4317 : : }
4318 : : }
4319 : 99845508 : for (i = 0; iterate_reference (i, ref); i++)
4320 : 61260950 : if (ref->stmt && !stmts.contains (ref->stmt))
4321 : : {
4322 : 0 : error ("reference to dead statement");
4323 : 0 : cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
4324 : 0 : error_found = true;
4325 : : }
4326 : 29597581 : }
4327 : : else
4328 : : /* No CFG available?! */
4329 : 0 : gcc_unreachable ();
4330 : :
4331 : 127201904 : for (e = callees; e; e = e->next_callee)
4332 : : {
4333 : 97604215 : if (!e->callback && e->callback_id)
4334 : : {
4335 : 0 : error ("non-callback edge has callback_id set");
4336 : 0 : error_found = true;
4337 : : }
4338 : :
4339 : 97604215 : if (e->callback && e->has_callback)
4340 : : {
4341 : 0 : error ("edge has both callback and has_callback set");
4342 : 0 : error_found = true;
4343 : : }
4344 : :
4345 : 97604215 : if (e->callback)
4346 : : {
4347 : 5 : if (!e->get_callback_carrying_edge ())
4348 : : {
4349 : 0 : error ("callback edge %s->%s has no callback-carrying",
4350 : 0 : identifier_to_locale (e->caller->name ()),
4351 : 0 : identifier_to_locale (e->callee->name ()));
4352 : 0 : error_found = true;
4353 : : }
4354 : : }
4355 : :
4356 : 97604215 : if (e->has_callback
4357 : 97604215 : && !callback_is_special_cased (e->callee->decl, e->call_stmt))
4358 : : {
4359 : 30 : int ncallbacks = 0;
4360 : 30 : int nfound_edges = 0;
4361 : 30 : for (tree cb = lookup_attribute (CALLBACK_ATTR_IDENT, DECL_ATTRIBUTES (
4362 : : e->callee->decl));
4363 : 60 : cb; cb = lookup_attribute (CALLBACK_ATTR_IDENT, TREE_CHAIN (cb)),
4364 : : ncallbacks++)
4365 : : ;
4366 : 116 : for (cgraph_edge *cbe = callees; cbe; cbe = cbe->next_callee)
4367 : : {
4368 : 86 : if (cbe->callback && cbe->call_stmt == e->call_stmt
4369 : 4 : && cbe->lto_stmt_uid == e->lto_stmt_uid)
4370 : : {
4371 : 4 : nfound_edges++;
4372 : : }
4373 : : }
4374 : 30 : if (ncallbacks < nfound_edges)
4375 : : {
4376 : 0 : error ("callback edge %s->%s callback edge count mismatch, "
4377 : : "expected at most %d, found %d",
4378 : 0 : identifier_to_locale (e->caller->name ()),
4379 : 0 : identifier_to_locale (e->callee->name ()), ncallbacks,
4380 : : nfound_edges);
4381 : : }
4382 : : }
4383 : :
4384 : 97604215 : if (!e->aux && !e->speculative && !e->callback && !e->has_callback)
4385 : : {
4386 : 0 : error ("edge %s->%s has no corresponding call_stmt",
4387 : 0 : identifier_to_locale (e->caller->name ()),
4388 : 0 : identifier_to_locale (e->callee->name ()));
4389 : 0 : cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
4390 : 0 : error_found = true;
4391 : : }
4392 : 97604215 : e->aux = 0;
4393 : : }
4394 : 31925622 : for (e = indirect_calls; e; e = e->next_callee)
4395 : : {
4396 : 2327933 : if (!e->aux && !e->speculative)
4397 : : {
4398 : 0 : error ("an indirect edge from %s has no corresponding call_stmt",
4399 : 0 : identifier_to_locale (e->caller->name ()));
4400 : 0 : cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
4401 : 0 : error_found = true;
4402 : : }
4403 : 2327933 : e->aux = 0;
4404 : : }
4405 : : }
4406 : :
4407 : 52238114 : if (nested_function_info *info = nested_function_info::get (this))
4408 : : {
4409 : 0 : if (info->nested != NULL)
4410 : : {
4411 : 0 : for (cgraph_node *n = info->nested; n != NULL;
4412 : 0 : n = next_nested_function (n))
4413 : : {
4414 : 0 : nested_function_info *ninfo = nested_function_info::get (n);
4415 : 0 : if (ninfo->origin == NULL)
4416 : : {
4417 : 0 : error ("missing origin for a node in a nested list");
4418 : 0 : error_found = true;
4419 : : }
4420 : 0 : else if (ninfo->origin != this)
4421 : : {
4422 : 0 : error ("origin points to a different parent");
4423 : 0 : error_found = true;
4424 : 0 : break;
4425 : : }
4426 : : }
4427 : : }
4428 : 0 : if (info->next_nested != NULL && info->origin == NULL)
4429 : : {
4430 : 0 : error ("missing origin for a node in a nested list");
4431 : 0 : error_found = true;
4432 : : }
4433 : : }
4434 : :
4435 : 52238114 : if (error_found)
4436 : : {
4437 : 0 : dump (stderr);
4438 : 0 : internal_error ("verify_cgraph_node failed");
4439 : : }
4440 : 52238114 : timevar_pop (TV_CGRAPH_VERIFY);
4441 : : }
4442 : :
4443 : : /* Verify whole cgraph structure. */
4444 : : DEBUG_FUNCTION void
4445 : 884 : cgraph_node::verify_cgraph_nodes (void)
4446 : : {
4447 : 884 : cgraph_node *node;
4448 : :
4449 : 884 : if (seen_error ())
4450 : : return;
4451 : :
4452 : 10550 : FOR_EACH_FUNCTION (node)
4453 : 4415 : node->verify ();
4454 : : }
4455 : :
4456 : : #if __GNUC__ >= 10
4457 : : # pragma GCC diagnostic pop
4458 : : #endif
4459 : :
4460 : : /* Walk the alias chain to return the function cgraph_node is alias of.
4461 : : Walk through thunks, too.
4462 : : When AVAILABILITY is non-NULL, get minimal availability in the chain.
4463 : : When REF is non-NULL, assume that reference happens in symbol REF
4464 : : when determining the availability. */
4465 : :
4466 : : cgraph_node *
4467 : 131140303 : cgraph_node::function_symbol (enum availability *availability,
4468 : : struct symtab_node *ref)
4469 : : {
4470 : 131140303 : cgraph_node *node = ultimate_alias_target (availability, ref);
4471 : :
4472 : 262285640 : while (node->thunk)
4473 : : {
4474 : 5034 : enum availability a;
4475 : :
4476 : 5034 : ref = node;
4477 : 5034 : node = node->callees->callee;
4478 : 8978 : node = node->ultimate_alias_target (availability ? &a : NULL, ref);
4479 : 5034 : if (availability && a < *availability)
4480 : 38 : *availability = a;
4481 : : }
4482 : 131140303 : return node;
4483 : : }
4484 : :
4485 : : /* Walk the alias chain to return the function cgraph_node is alias of.
4486 : : Walk through non virtual thunks, too. Thus we return either a function
4487 : : or a virtual thunk node.
4488 : : When AVAILABILITY is non-NULL, get minimal availability in the chain.
4489 : : When REF is non-NULL, assume that reference happens in symbol REF
4490 : : when determining the availability. */
4491 : :
4492 : : cgraph_node *
4493 : 33008036 : cgraph_node::function_or_virtual_thunk_symbol
4494 : : (enum availability *availability,
4495 : : struct symtab_node *ref)
4496 : : {
4497 : 33008036 : cgraph_node *node = ultimate_alias_target (availability, ref);
4498 : :
4499 : 66017128 : while (node->thunk && !thunk_info::get (node)->virtual_offset_p)
4500 : : {
4501 : 1056 : enum availability a;
4502 : :
4503 : 1056 : ref = node;
4504 : 1056 : node = node->callees->callee;
4505 : 1056 : node = node->ultimate_alias_target (availability ? &a : NULL, ref);
4506 : 1056 : if (availability && a < *availability)
4507 : 320 : *availability = a;
4508 : : }
4509 : 33008036 : return node;
4510 : : }
4511 : :
4512 : : /* When doing LTO, read cgraph_node's body from disk if it is not already
4513 : : present. Also perform any necessary clone materializations. */
4514 : :
4515 : : bool
4516 : 6215323 : cgraph_node::get_untransformed_body ()
4517 : : {
4518 : 6215323 : lto_file_decl_data *file_data;
4519 : 6215323 : const char *data, *name;
4520 : 6215323 : size_t len;
4521 : 6215323 : tree decl = this->decl;
4522 : :
4523 : : /* See if there is clone to be materialized.
4524 : : (inline clones does not need materialization, but we can be seeing
4525 : : an inline clone of real clone). */
4526 : 6215323 : cgraph_node *p = this;
4527 : 8920631 : for (cgraph_node *c = clone_of; c; c = c->clone_of)
4528 : : {
4529 : 2705308 : if (c->decl != decl)
4530 : 124852 : p->materialize_clone ();
4531 : 2705308 : p = c;
4532 : : }
4533 : :
4534 : : /* Check if body is already there. Either we have gimple body or
4535 : : the function is thunk and in that case we set DECL_ARGUMENTS. */
4536 : 6215323 : if (DECL_ARGUMENTS (decl) || gimple_has_body_p (decl))
4537 : 6131098 : return false;
4538 : :
4539 : 168450 : gcc_assert (in_lto_p && !DECL_RESULT (decl));
4540 : :
4541 : 84225 : timevar_push (TV_IPA_LTO_GIMPLE_IN);
4542 : :
4543 : 84225 : file_data = lto_file_data;
4544 : 84225 : name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4545 : :
4546 : : /* We may have renamed the declaration, e.g., a static function. */
4547 : 84225 : name = lto_get_decl_name_mapping (file_data, name);
4548 : 84225 : struct lto_in_decl_state *decl_state
4549 : 84225 : = lto_get_function_in_decl_state (file_data, decl);
4550 : :
4551 : 84225 : cgraph_node *origin = this;
4552 : 168624 : while (origin->clone_of)
4553 : : origin = origin->clone_of;
4554 : :
4555 : 84225 : int stream_order = origin->order - file_data->order_base;
4556 : 168450 : data = lto_get_section_data (file_data, LTO_section_function_body,
4557 : : name, stream_order, &len,
4558 : 84225 : decl_state->compressed);
4559 : 84225 : if (!data)
4560 : 0 : fatal_error (input_location, "%s: section %s.%d is missing",
4561 : : file_data->file_name, name, stream_order);
4562 : :
4563 : 84225 : gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
4564 : :
4565 : 84225 : if (!quiet_flag)
4566 : 0 : fprintf (stderr, " in:%s", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
4567 : 84225 : lto_input_function_body (file_data, this, data);
4568 : 84225 : lto_stats.num_function_bodies++;
4569 : 84225 : lto_free_section_data (file_data, LTO_section_function_body, name,
4570 : 84225 : data, len, decl_state->compressed);
4571 : 84225 : lto_free_function_in_decl_state_for_node (this);
4572 : : /* Keep lto file data so ipa-inline-analysis knows about cross module
4573 : : inlining. */
4574 : :
4575 : 84225 : timevar_pop (TV_IPA_LTO_GIMPLE_IN);
4576 : :
4577 : 84225 : return true;
4578 : : }
4579 : :
4580 : : /* Prepare function body. When doing LTO, read cgraph_node's body from disk
4581 : : if it is not already present. When some IPA transformations are scheduled,
4582 : : apply them. */
4583 : :
4584 : : bool
4585 : 28493 : cgraph_node::get_body (void)
4586 : : {
4587 : 28493 : bool updated;
4588 : :
4589 : 28493 : updated = get_untransformed_body ();
4590 : :
4591 : : /* Getting transformed body makes no sense for inline clones;
4592 : : we should never use this on real clones because they are materialized
4593 : : early.
4594 : : TODO: Materializing clones here will likely lead to smaller LTRANS
4595 : : footprint. */
4596 : 28493 : gcc_assert (!inlined_to && !clone_of);
4597 : 28493 : if (ipa_transforms_to_apply.exists ())
4598 : : {
4599 : 12204 : opt_pass *saved_current_pass = current_pass;
4600 : 12204 : FILE *saved_dump_file = dump_file;
4601 : 12204 : const char *saved_dump_file_name = dump_file_name;
4602 : 12204 : dump_flags_t saved_dump_flags = dump_flags;
4603 : 12204 : dump_file_name = NULL;
4604 : 12204 : set_dump_file (NULL);
4605 : :
4606 : 12204 : push_cfun (DECL_STRUCT_FUNCTION (decl));
4607 : :
4608 : 12204 : update_ssa (TODO_update_ssa_only_virtuals);
4609 : 12204 : execute_all_ipa_transforms (true);
4610 : 12204 : cgraph_edge::rebuild_edges ();
4611 : 12204 : free_dominance_info (CDI_DOMINATORS);
4612 : 12204 : free_dominance_info (CDI_POST_DOMINATORS);
4613 : 12204 : pop_cfun ();
4614 : 12204 : updated = true;
4615 : :
4616 : 12204 : current_pass = saved_current_pass;
4617 : 12204 : set_dump_file (saved_dump_file);
4618 : 12204 : dump_file_name = saved_dump_file_name;
4619 : 12204 : dump_flags = saved_dump_flags;
4620 : : }
4621 : 28493 : return updated;
4622 : : }
4623 : :
4624 : : /* Return the DECL_STRUCT_FUNCTION of the function. */
4625 : :
4626 : : struct function *
4627 : 219352 : cgraph_node::get_fun () const
4628 : : {
4629 : 219352 : const cgraph_node *node = this;
4630 : 219352 : struct function *fun = DECL_STRUCT_FUNCTION (node->decl);
4631 : :
4632 : 219352 : while (!fun && node->clone_of)
4633 : : {
4634 : 0 : node = node->clone_of;
4635 : 0 : fun = DECL_STRUCT_FUNCTION (node->decl);
4636 : : }
4637 : :
4638 : 219352 : return fun;
4639 : : }
4640 : :
4641 : : /* Reset all state within cgraph.cc so that we can rerun the compiler
4642 : : within the same process. For use by toplev::finalize. */
4643 : :
4644 : : void
4645 : 260181 : cgraph_cc_finalize (void)
4646 : : {
4647 : 260181 : nested_function_info::release ();
4648 : 260181 : thunk_info::release ();
4649 : 260181 : clone_info::release ();
4650 : 260181 : symtab = NULL;
4651 : :
4652 : 260181 : x_cgraph_nodes_queue = NULL;
4653 : :
4654 : 260181 : cgraph_fnver_htab = NULL;
4655 : 260181 : version_info_node = NULL;
4656 : 260181 : }
4657 : :
4658 : : /* A worker for call_for_symbol_and_aliases. */
4659 : :
4660 : : bool
4661 : 731969 : cgraph_node::call_for_symbol_and_aliases_1 (bool (*callback) (cgraph_node *,
4662 : : void *),
4663 : : void *data,
4664 : : bool include_overwritable)
4665 : : {
4666 : 731969 : ipa_ref *ref;
4667 : 1417450 : FOR_EACH_ALIAS (this, ref)
4668 : : {
4669 : 784365 : cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
4670 : 784365 : if (include_overwritable
4671 : 784365 : || alias->get_availability () > AVAIL_INTERPOSABLE)
4672 : 784365 : if (alias->call_for_symbol_and_aliases (callback, data,
4673 : : include_overwritable))
4674 : : return true;
4675 : : }
4676 : : return false;
4677 : : }
4678 : :
4679 : : /* Return true if NODE has thunk. */
4680 : :
4681 : : bool
4682 : 39111 : cgraph_node::has_thunk_p (cgraph_node *node, void *)
4683 : : {
4684 : 73262 : for (cgraph_edge *e = node->callers; e; e = e->next_caller)
4685 : 34151 : if (e->caller->thunk)
4686 : : return true;
4687 : : return false;
4688 : : }
4689 : :
4690 : : /* Expected frequency of executions within the function. */
4691 : :
4692 : : sreal
4693 : 201344717 : cgraph_edge::sreal_frequency ()
4694 : : {
4695 : 201344717 : return count.to_sreal_scale (caller->inlined_to
4696 : 201344717 : ? caller->inlined_to->count
4697 : 201344717 : : caller->count);
4698 : : }
4699 : :
4700 : : /* Expected frequency of executions within the function.
4701 : : If edge is speculative, sum all its indirect targets. */
4702 : :
4703 : : sreal
4704 : 920 : cgraph_edge::combined_sreal_frequency ()
4705 : : {
4706 : 920 : if (!speculative)
4707 : 853 : return sreal_frequency ();
4708 : 67 : cgraph_edge *e = this;
4709 : 67 : if (e->callee)
4710 : 0 : e = e->speculative_call_indirect_edge ();
4711 : 67 : sreal sum = e->sreal_frequency ();
4712 : 67 : for (e = e->first_speculative_call_target ();
4713 : 141 : e;
4714 : 74 : e = e->next_speculative_call_target ())
4715 : 74 : sum += e->sreal_frequency ();
4716 : 67 : return sum;
4717 : : }
4718 : :
4719 : :
4720 : : /* During LTO stream in this can be used to check whether call can possibly
4721 : : be internal to the current translation unit. */
4722 : :
4723 : : bool
4724 : 477888 : cgraph_edge::possibly_call_in_translation_unit_p (void)
4725 : : {
4726 : 477888 : gcc_checking_assert (in_lto_p && caller->prevailing_p ());
4727 : :
4728 : : /* While incremental linking we may end up getting function body later. */
4729 : 477888 : if (flag_incremental_link == INCREMENTAL_LINK_LTO)
4730 : : return true;
4731 : :
4732 : : /* We may be smarter here and avoid streaming in indirect calls we can't
4733 : : track, but that would require arranging streaming the indirect call
4734 : : summary first. */
4735 : 477659 : if (!callee)
4736 : : return true;
4737 : :
4738 : : /* If callee is local to the original translation unit, it will be
4739 : : defined. */
4740 : 474943 : if (!TREE_PUBLIC (callee->decl) && !DECL_EXTERNAL (callee->decl))
4741 : : return true;
4742 : :
4743 : : /* Otherwise we need to lookup prevailing symbol (symbol table is not merged,
4744 : : yet) and see if it is a definition. In fact we may also resolve aliases,
4745 : : but that is probably not too important. */
4746 : 479000 : symtab_node *node = callee;
4747 : 479000 : for (int n = 10; node->previous_sharing_asm_name && n ; n--)
4748 : 10096 : node = node->previous_sharing_asm_name;
4749 : 468904 : if (node->previous_sharing_asm_name)
4750 : 234 : node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (callee->decl));
4751 : 468904 : gcc_assert (TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl));
4752 : 468904 : return node->get_availability () >= AVAIL_INTERPOSABLE;
4753 : : }
4754 : :
4755 : : /* Return num_speculative_targets of this edge. */
4756 : :
4757 : : int
4758 : 181493 : cgraph_edge::num_speculative_call_targets_p (void)
4759 : : {
4760 : 181493 : return indirect_info ? indirect_info->num_speculative_call_targets : 0;
4761 : : }
4762 : :
4763 : : /* Check if function calls comdat local. This is used to recompute
4764 : : calls_comdat_local flag after function transformations. */
4765 : : bool
4766 : 49040402 : cgraph_node::check_calls_comdat_local_p ()
4767 : : {
4768 : 157179332 : for (cgraph_edge *e = callees; e; e = e->next_callee)
4769 : 3790418 : if (e->inline_failed
4770 : 116837169 : ? e->callee->comdat_local_p ()
4771 : 3790418 : : e->callee->check_calls_comdat_local_p ())
4772 : 50677 : return true;
4773 : : return false;
4774 : : }
4775 : :
4776 : : /* Return true if this node represents a former, i.e. an expanded, thunk. */
4777 : :
4778 : : bool
4779 : 5043320 : cgraph_node::former_thunk_p (void)
4780 : : {
4781 : 5043320 : if (thunk)
4782 : : return false;
4783 : 5043320 : thunk_info *i = thunk_info::get (this);
4784 : 5043320 : if (!i)
4785 : : return false;
4786 : 67 : gcc_checking_assert (i->fixed_offset || i->virtual_offset_p
4787 : : || i->indirect_offset);
4788 : : return true;
4789 : : }
4790 : :
4791 : : /* A stashed copy of "symtab" for use by selftest::symbol_table_test.
4792 : : This needs to be a global so that it can be a GC root, and thus
4793 : : prevent the stashed copy from being garbage-collected if the GC runs
4794 : : during a symbol_table_test. */
4795 : :
4796 : : symbol_table *saved_symtab;
4797 : :
4798 : : #if CHECKING_P
4799 : :
4800 : : namespace selftest {
4801 : :
4802 : : /* class selftest::symbol_table_test. */
4803 : :
4804 : : /* Constructor. Store the old value of symtab, and create a new one. */
4805 : :
4806 : 64 : symbol_table_test::symbol_table_test ()
4807 : : {
4808 : 64 : gcc_assert (saved_symtab == NULL);
4809 : 64 : saved_symtab = symtab;
4810 : 64 : symtab = new (ggc_alloc<symbol_table> ()) symbol_table ();
4811 : 64 : }
4812 : :
4813 : : /* Destructor. Restore the old value of symtab. */
4814 : :
4815 : 64 : symbol_table_test::~symbol_table_test ()
4816 : : {
4817 : 64 : gcc_assert (saved_symtab != NULL);
4818 : 64 : symtab = saved_symtab;
4819 : 64 : saved_symtab = NULL;
4820 : 64 : }
4821 : :
4822 : : /* Verify that symbol_table_test works. */
4823 : :
4824 : : static void
4825 : 4 : test_symbol_table_test ()
4826 : : {
4827 : : /* Simulate running two selftests involving symbol tables. */
4828 : 12 : for (int i = 0; i < 2; i++)
4829 : : {
4830 : 8 : symbol_table_test stt;
4831 : 8 : tree test_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
4832 : : get_identifier ("test_decl"),
4833 : : build_function_type_list (void_type_node,
4834 : : NULL_TREE));
4835 : 8 : cgraph_node *node = cgraph_node::get_create (test_decl);
4836 : 8 : gcc_assert (node);
4837 : :
4838 : : /* Verify that the node has order 0 on both iterations,
4839 : : and thus that nodes have predictable dump names in selftests. */
4840 : 8 : ASSERT_EQ (node->order, 0);
4841 : 8 : ASSERT_STREQ (node->dump_name (), "test_decl/1");
4842 : 8 : }
4843 : 4 : }
4844 : :
4845 : : /* Run all of the selftests within this file. */
4846 : :
4847 : : void
4848 : 4 : cgraph_cc_tests ()
4849 : : {
4850 : 4 : test_symbol_table_test ();
4851 : 4 : }
4852 : :
4853 : : } // namespace selftest
4854 : :
4855 : : #endif /* CHECKING_P */
4856 : :
4857 : : #include "gt-cgraph.h"
|