lto-cgraph.c (get_alias_symbol): Remove weakref sanity check.
[gcc.git] / gcc / cgraph.c
1 /* Callgraph handling code.
2 Copyright (C) 2003-2013 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 intra-procedural optimization.
24 It represents a multi-graph where nodes are functions and edges are call sites. */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "tree-inline.h"
32 #include "langhooks.h"
33 #include "hashtab.h"
34 #include "toplev.h"
35 #include "flags.h"
36 #include "ggc.h"
37 #include "debug.h"
38 #include "target.h"
39 #include "basic-block.h"
40 #include "cgraph.h"
41 #include "intl.h"
42 #include "gimple.h"
43 #include "timevar.h"
44 #include "dumpfile.h"
45 #include "tree-flow.h"
46 #include "value-prof.h"
47 #include "except.h"
48 #include "diagnostic-core.h"
49 #include "rtl.h"
50 #include "ipa-utils.h"
51 #include "lto-streamer.h"
52 #include "ipa-inline.h"
53 #include "cfgloop.h"
54 #include "gimple-pretty-print.h"
55
56 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
57 #include "tree-pass.h"
58
59 static void cgraph_node_remove_callers (struct cgraph_node *node);
60 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
61 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
62
63 /* Queue of cgraph nodes scheduled to be lowered. */
64 symtab_node x_cgraph_nodes_queue;
65 #define cgraph_nodes_queue ((struct cgraph_node *)x_cgraph_nodes_queue)
66
67 /* Number of nodes in existence. */
68 int cgraph_n_nodes;
69
70 /* Maximal uid used in cgraph nodes. */
71 int cgraph_max_uid;
72
73 /* Maximal uid used in cgraph edges. */
74 int cgraph_edge_max_uid;
75
76 /* Set when whole unit has been analyzed so we can access global info. */
77 bool cgraph_global_info_ready = false;
78
79 /* What state callgraph is in right now. */
80 enum cgraph_state cgraph_state = CGRAPH_STATE_PARSING;
81
82 /* Set when the cgraph is fully build and the basic flags are computed. */
83 bool cgraph_function_flags_ready = false;
84
85 /* List of hooks triggered on cgraph_edge events. */
86 struct cgraph_edge_hook_list {
87 cgraph_edge_hook hook;
88 void *data;
89 struct cgraph_edge_hook_list *next;
90 };
91
92 /* List of hooks triggered on cgraph_node events. */
93 struct cgraph_node_hook_list {
94 cgraph_node_hook hook;
95 void *data;
96 struct cgraph_node_hook_list *next;
97 };
98
99 /* List of hooks triggered on events involving two cgraph_edges. */
100 struct cgraph_2edge_hook_list {
101 cgraph_2edge_hook hook;
102 void *data;
103 struct cgraph_2edge_hook_list *next;
104 };
105
106 /* List of hooks triggered on events involving two cgraph_nodes. */
107 struct cgraph_2node_hook_list {
108 cgraph_2node_hook hook;
109 void *data;
110 struct cgraph_2node_hook_list *next;
111 };
112
113 /* List of hooks triggered when an edge is removed. */
114 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
115 /* List of hooks triggered when a node is removed. */
116 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
117 /* List of hooks triggered when an edge is duplicated. */
118 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
119 /* List of hooks triggered when a node is duplicated. */
120 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
121 /* List of hooks triggered when an function is inserted. */
122 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
123
124 /* Head of a linked list of unused (freed) call graph nodes.
125 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
126 static GTY(()) struct cgraph_node *free_nodes;
127 /* Head of a linked list of unused (freed) call graph edges.
128 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
129 static GTY(()) struct cgraph_edge *free_edges;
130
131 /* Did procss_same_body_aliases run? */
132 bool cpp_implicit_aliases_done;
133
134 /* Map a cgraph_node to cgraph_function_version_info using this htab.
135 The cgraph_function_version_info has a THIS_NODE field that is the
136 corresponding cgraph_node.. */
137
138 static htab_t GTY((param_is (struct cgraph_function_version_info *)))
139 cgraph_fnver_htab = NULL;
140
141 /* Hash function for cgraph_fnver_htab. */
142 static hashval_t
143 cgraph_fnver_htab_hash (const void *ptr)
144 {
145 int uid = ((const struct cgraph_function_version_info *)ptr)->this_node->uid;
146 return (hashval_t)(uid);
147 }
148
149 /* eq function for cgraph_fnver_htab. */
150 static int
151 cgraph_fnver_htab_eq (const void *p1, const void *p2)
152 {
153 const struct cgraph_function_version_info *n1
154 = (const struct cgraph_function_version_info *)p1;
155 const struct cgraph_function_version_info *n2
156 = (const struct cgraph_function_version_info *)p2;
157
158 return n1->this_node->uid == n2->this_node->uid;
159 }
160
161 /* Mark as GC root all allocated nodes. */
162 static GTY(()) struct cgraph_function_version_info *
163 version_info_node = NULL;
164
165 /* Get the cgraph_function_version_info node corresponding to node. */
166 struct cgraph_function_version_info *
167 get_cgraph_node_version (struct cgraph_node *node)
168 {
169 struct cgraph_function_version_info *ret;
170 struct cgraph_function_version_info key;
171 key.this_node = node;
172
173 if (cgraph_fnver_htab == NULL)
174 return NULL;
175
176 ret = (struct cgraph_function_version_info *)
177 htab_find (cgraph_fnver_htab, &key);
178
179 return ret;
180 }
181
182 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
183 corresponding to cgraph_node NODE. */
184 struct cgraph_function_version_info *
185 insert_new_cgraph_node_version (struct cgraph_node *node)
186 {
187 void **slot;
188
189 version_info_node = NULL;
190 version_info_node = ggc_alloc_cleared_cgraph_function_version_info ();
191 version_info_node->this_node = node;
192
193 if (cgraph_fnver_htab == NULL)
194 cgraph_fnver_htab = htab_create_ggc (2, cgraph_fnver_htab_hash,
195 cgraph_fnver_htab_eq, NULL);
196
197 slot = htab_find_slot (cgraph_fnver_htab, version_info_node, INSERT);
198 gcc_assert (slot != NULL);
199 *slot = version_info_node;
200 return version_info_node;
201 }
202
203 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
204 DECL is a duplicate declaration. */
205 void
206 delete_function_version (tree decl)
207 {
208 struct cgraph_node *decl_node = cgraph_get_node (decl);
209 struct cgraph_function_version_info *decl_v = NULL;
210
211 if (decl_node == NULL)
212 return;
213
214 decl_v = get_cgraph_node_version (decl_node);
215
216 if (decl_v == NULL)
217 return;
218
219 if (decl_v->prev != NULL)
220 decl_v->prev->next = decl_v->next;
221
222 if (decl_v->next != NULL)
223 decl_v->next->prev = decl_v->prev;
224
225 if (cgraph_fnver_htab != NULL)
226 htab_remove_elt (cgraph_fnver_htab, decl_v);
227
228 cgraph_remove_node (decl_node);
229 }
230
231 /* Record that DECL1 and DECL2 are semantically identical function
232 versions. */
233 void
234 record_function_versions (tree decl1, tree decl2)
235 {
236 struct cgraph_node *decl1_node = cgraph_get_create_node (decl1);
237 struct cgraph_node *decl2_node = cgraph_get_create_node (decl2);
238 struct cgraph_function_version_info *decl1_v = NULL;
239 struct cgraph_function_version_info *decl2_v = NULL;
240 struct cgraph_function_version_info *before;
241 struct cgraph_function_version_info *after;
242
243 gcc_assert (decl1_node != NULL && decl2_node != NULL);
244 decl1_v = get_cgraph_node_version (decl1_node);
245 decl2_v = get_cgraph_node_version (decl2_node);
246
247 if (decl1_v != NULL && decl2_v != NULL)
248 return;
249
250 if (decl1_v == NULL)
251 decl1_v = insert_new_cgraph_node_version (decl1_node);
252
253 if (decl2_v == NULL)
254 decl2_v = insert_new_cgraph_node_version (decl2_node);
255
256 /* Chain decl2_v and decl1_v. All semantically identical versions
257 will be chained together. */
258
259 before = decl1_v;
260 after = decl2_v;
261
262 while (before->next != NULL)
263 before = before->next;
264
265 while (after->prev != NULL)
266 after= after->prev;
267
268 before->next = after;
269 after->prev = before;
270 }
271
272 /* Macros to access the next item in the list of free cgraph nodes and
273 edges. */
274 #define NEXT_FREE_NODE(NODE) cgraph ((NODE)->symbol.next)
275 #define SET_NEXT_FREE_NODE(NODE,NODE2) ((NODE))->symbol.next = (symtab_node)NODE2
276 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
277
278 /* Register HOOK to be called with DATA on each removed edge. */
279 struct cgraph_edge_hook_list *
280 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
281 {
282 struct cgraph_edge_hook_list *entry;
283 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
284
285 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
286 entry->hook = hook;
287 entry->data = data;
288 entry->next = NULL;
289 while (*ptr)
290 ptr = &(*ptr)->next;
291 *ptr = entry;
292 return entry;
293 }
294
295 /* Remove ENTRY from the list of hooks called on removing edges. */
296 void
297 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
298 {
299 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
300
301 while (*ptr != entry)
302 ptr = &(*ptr)->next;
303 *ptr = entry->next;
304 free (entry);
305 }
306
307 /* Call all edge removal hooks. */
308 static void
309 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
310 {
311 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
312 while (entry)
313 {
314 entry->hook (e, entry->data);
315 entry = entry->next;
316 }
317 }
318
319 /* Register HOOK to be called with DATA on each removed node. */
320 struct cgraph_node_hook_list *
321 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
322 {
323 struct cgraph_node_hook_list *entry;
324 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
325
326 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
327 entry->hook = hook;
328 entry->data = data;
329 entry->next = NULL;
330 while (*ptr)
331 ptr = &(*ptr)->next;
332 *ptr = entry;
333 return entry;
334 }
335
336 /* Remove ENTRY from the list of hooks called on removing nodes. */
337 void
338 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
339 {
340 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
341
342 while (*ptr != entry)
343 ptr = &(*ptr)->next;
344 *ptr = entry->next;
345 free (entry);
346 }
347
348 /* Call all node removal hooks. */
349 static void
350 cgraph_call_node_removal_hooks (struct cgraph_node *node)
351 {
352 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
353 while (entry)
354 {
355 entry->hook (node, entry->data);
356 entry = entry->next;
357 }
358 }
359
360 /* Register HOOK to be called with DATA on each inserted node. */
361 struct cgraph_node_hook_list *
362 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
363 {
364 struct cgraph_node_hook_list *entry;
365 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
366
367 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
368 entry->hook = hook;
369 entry->data = data;
370 entry->next = NULL;
371 while (*ptr)
372 ptr = &(*ptr)->next;
373 *ptr = entry;
374 return entry;
375 }
376
377 /* Remove ENTRY from the list of hooks called on inserted nodes. */
378 void
379 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
380 {
381 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
382
383 while (*ptr != entry)
384 ptr = &(*ptr)->next;
385 *ptr = entry->next;
386 free (entry);
387 }
388
389 /* Call all node insertion hooks. */
390 void
391 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
392 {
393 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
394 while (entry)
395 {
396 entry->hook (node, entry->data);
397 entry = entry->next;
398 }
399 }
400
401 /* Register HOOK to be called with DATA on each duplicated edge. */
402 struct cgraph_2edge_hook_list *
403 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
404 {
405 struct cgraph_2edge_hook_list *entry;
406 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
407
408 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
409 entry->hook = hook;
410 entry->data = data;
411 entry->next = NULL;
412 while (*ptr)
413 ptr = &(*ptr)->next;
414 *ptr = entry;
415 return entry;
416 }
417
418 /* Remove ENTRY from the list of hooks called on duplicating edges. */
419 void
420 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
421 {
422 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
423
424 while (*ptr != entry)
425 ptr = &(*ptr)->next;
426 *ptr = entry->next;
427 free (entry);
428 }
429
430 /* Call all edge duplication hooks. */
431 void
432 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
433 struct cgraph_edge *cs2)
434 {
435 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
436 while (entry)
437 {
438 entry->hook (cs1, cs2, entry->data);
439 entry = entry->next;
440 }
441 }
442
443 /* Register HOOK to be called with DATA on each duplicated node. */
444 struct cgraph_2node_hook_list *
445 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
446 {
447 struct cgraph_2node_hook_list *entry;
448 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
449
450 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
451 entry->hook = hook;
452 entry->data = data;
453 entry->next = NULL;
454 while (*ptr)
455 ptr = &(*ptr)->next;
456 *ptr = entry;
457 return entry;
458 }
459
460 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
461 void
462 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
463 {
464 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
465
466 while (*ptr != entry)
467 ptr = &(*ptr)->next;
468 *ptr = entry->next;
469 free (entry);
470 }
471
472 /* Call all node duplication hooks. */
473 void
474 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
475 struct cgraph_node *node2)
476 {
477 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
478 while (entry)
479 {
480 entry->hook (node1, node2, entry->data);
481 entry = entry->next;
482 }
483 }
484
485 /* Allocate new callgraph node. */
486
487 static inline struct cgraph_node *
488 cgraph_allocate_node (void)
489 {
490 struct cgraph_node *node;
491
492 if (free_nodes)
493 {
494 node = free_nodes;
495 free_nodes = NEXT_FREE_NODE (node);
496 }
497 else
498 {
499 node = ggc_alloc_cleared_cgraph_node ();
500 node->uid = cgraph_max_uid++;
501 }
502
503 return node;
504 }
505
506 /* Allocate new callgraph node and insert it into basic data structures. */
507
508 struct cgraph_node *
509 cgraph_create_empty_node (void)
510 {
511 struct cgraph_node *node = cgraph_allocate_node ();
512
513 node->symbol.type = SYMTAB_FUNCTION;
514 node->frequency = NODE_FREQUENCY_NORMAL;
515 node->count_materialization_scale = REG_BR_PROB_BASE;
516 cgraph_n_nodes++;
517 return node;
518 }
519
520 /* Return cgraph node assigned to DECL. Create new one when needed. */
521
522 struct cgraph_node *
523 cgraph_create_node (tree decl)
524 {
525 struct cgraph_node *node = cgraph_create_empty_node ();
526 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
527
528 node->symbol.decl = decl;
529 symtab_register_node ((symtab_node) node);
530
531 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
532 {
533 node->origin = cgraph_get_create_node (DECL_CONTEXT (decl));
534 node->next_nested = node->origin->nested;
535 node->origin->nested = node;
536 }
537 return node;
538 }
539
540 /* Try to find a call graph node for declaration DECL and if it does not exist,
541 create it. */
542
543 struct cgraph_node *
544 cgraph_get_create_node (tree decl)
545 {
546 struct cgraph_node *node;
547
548 node = cgraph_get_node (decl);
549 if (node)
550 return node;
551
552 return cgraph_create_node (decl);
553 }
554
555 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
556 the function body is associated with (not necessarily cgraph_node (DECL). */
557
558 struct cgraph_node *
559 cgraph_create_function_alias (tree alias, tree target)
560 {
561 struct cgraph_node *alias_node;
562
563 gcc_assert (TREE_CODE (target) == FUNCTION_DECL
564 || TREE_CODE (target) == IDENTIFIER_NODE);
565 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
566 alias_node = cgraph_get_create_node (alias);
567 gcc_assert (!alias_node->symbol.definition);
568 alias_node->symbol.alias_target = target;
569 alias_node->symbol.definition = true;
570 alias_node->symbol.alias = true;
571 return alias_node;
572 }
573
574 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
575 and NULL otherwise.
576 Same body aliases are output whenever the body of DECL is output,
577 and cgraph_get_node (ALIAS) transparently returns cgraph_get_node (DECL). */
578
579 struct cgraph_node *
580 cgraph_same_body_alias (struct cgraph_node *decl_node ATTRIBUTE_UNUSED, tree alias, tree decl)
581 {
582 struct cgraph_node *n;
583 #ifndef ASM_OUTPUT_DEF
584 /* If aliases aren't supported by the assembler, fail. */
585 return NULL;
586 #endif
587 /* Langhooks can create same body aliases of symbols not defined.
588 Those are useless. Drop them on the floor. */
589 if (cgraph_global_info_ready)
590 return NULL;
591
592 n = cgraph_create_function_alias (alias, decl);
593 n->symbol.cpp_implicit_alias = true;
594 if (cpp_implicit_aliases_done)
595 symtab_resolve_alias ((symtab_node)n,
596 (symtab_node)cgraph_get_node (decl));
597 return n;
598 }
599
600 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
601 aliases DECL with an adjustments made into the first parameter.
602 See comments in thunk_adjust for detail on the parameters. */
603
604 struct cgraph_node *
605 cgraph_add_thunk (struct cgraph_node *decl_node ATTRIBUTE_UNUSED,
606 tree alias, tree decl ATTRIBUTE_UNUSED,
607 bool this_adjusting,
608 HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value,
609 tree virtual_offset,
610 tree real_alias)
611 {
612 struct cgraph_node *node;
613
614 node = cgraph_get_node (alias);
615 if (node)
616 {
617 gcc_assert (node->symbol.definition);
618 gcc_assert (!node->symbol.alias);
619 gcc_assert (!node->thunk.thunk_p);
620 cgraph_remove_node (node);
621 }
622
623 node = cgraph_create_node (alias);
624 gcc_checking_assert (!virtual_offset
625 || tree_to_double_int (virtual_offset) ==
626 double_int::from_shwi (virtual_value));
627 node->thunk.fixed_offset = fixed_offset;
628 node->thunk.this_adjusting = this_adjusting;
629 node->thunk.virtual_value = virtual_value;
630 node->thunk.virtual_offset_p = virtual_offset != NULL;
631 node->thunk.alias = real_alias;
632 node->thunk.thunk_p = true;
633 node->symbol.definition = true;
634
635 return node;
636 }
637
638 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
639 Return NULL if there's no such node. */
640
641 struct cgraph_node *
642 cgraph_node_for_asm (tree asmname)
643 {
644 /* We do not want to look at inline clones. */
645 for (symtab_node node = symtab_node_for_asm (asmname);
646 node;
647 node = node->symbol.next_sharing_asm_name)
648 {
649 cgraph_node *cn = dyn_cast <cgraph_node> (node);
650 if (cn && !cn->global.inlined_to)
651 return cn;
652 }
653 return NULL;
654 }
655
656 /* Returns a hash value for X (which really is a cgraph_edge). */
657
658 static hashval_t
659 edge_hash (const void *x)
660 {
661 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
662 }
663
664 /* Return nonzero if the call_stmt of of cgraph_edge X is stmt *Y. */
665
666 static int
667 edge_eq (const void *x, const void *y)
668 {
669 return ((const struct cgraph_edge *) x)->call_stmt == y;
670 }
671
672 /* Add call graph edge E to call site hash of its caller. */
673
674 static inline void
675 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
676 {
677 void **slot;
678 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
679 e->call_stmt,
680 htab_hash_pointer (e->call_stmt),
681 INSERT);
682 gcc_assert (!*slot);
683 *slot = e;
684 }
685
686 /* Return the callgraph edge representing the GIMPLE_CALL statement
687 CALL_STMT. */
688
689 struct cgraph_edge *
690 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
691 {
692 struct cgraph_edge *e, *e2;
693 int n = 0;
694
695 if (node->call_site_hash)
696 return (struct cgraph_edge *)
697 htab_find_with_hash (node->call_site_hash, call_stmt,
698 htab_hash_pointer (call_stmt));
699
700 /* This loop may turn out to be performance problem. In such case adding
701 hashtables into call nodes with very many edges is probably best
702 solution. It is not good idea to add pointer into CALL_EXPR itself
703 because we want to make possible having multiple cgraph nodes representing
704 different clones of the same body before the body is actually cloned. */
705 for (e = node->callees; e; e = e->next_callee)
706 {
707 if (e->call_stmt == call_stmt)
708 break;
709 n++;
710 }
711
712 if (!e)
713 for (e = node->indirect_calls; e; e = e->next_callee)
714 {
715 if (e->call_stmt == call_stmt)
716 break;
717 n++;
718 }
719
720 if (n > 100)
721 {
722 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
723 for (e2 = node->callees; e2; e2 = e2->next_callee)
724 cgraph_add_edge_to_call_site_hash (e2);
725 for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
726 cgraph_add_edge_to_call_site_hash (e2);
727 }
728
729 return e;
730 }
731
732
733 /* Change field call_stmt of edge E to NEW_STMT. */
734
735 void
736 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
737 {
738 tree decl;
739
740 if (e->caller->call_site_hash)
741 {
742 htab_remove_elt_with_hash (e->caller->call_site_hash,
743 e->call_stmt,
744 htab_hash_pointer (e->call_stmt));
745 }
746
747 e->call_stmt = new_stmt;
748 if (e->indirect_unknown_callee
749 && (decl = gimple_call_fndecl (new_stmt)))
750 {
751 /* Constant propagation (and possibly also inlining?) can turn an
752 indirect call into a direct one. */
753 struct cgraph_node *new_callee = cgraph_get_node (decl);
754
755 gcc_checking_assert (new_callee);
756 cgraph_make_edge_direct (e, new_callee);
757 }
758
759 push_cfun (DECL_STRUCT_FUNCTION (e->caller->symbol.decl));
760 e->can_throw_external = stmt_can_throw_external (new_stmt);
761 pop_cfun ();
762 if (e->caller->call_site_hash)
763 cgraph_add_edge_to_call_site_hash (e);
764 }
765
766 /* Allocate a cgraph_edge structure and fill it with data according to the
767 parameters of which only CALLEE can be NULL (when creating an indirect call
768 edge). */
769
770 static struct cgraph_edge *
771 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
772 gimple call_stmt, gcov_type count, int freq)
773 {
774 struct cgraph_edge *edge;
775
776 /* LTO does not actually have access to the call_stmt since these
777 have not been loaded yet. */
778 if (call_stmt)
779 {
780 /* This is a rather expensive check possibly triggering
781 construction of call stmt hashtable. */
782 gcc_checking_assert (!cgraph_edge (caller, call_stmt));
783
784 gcc_assert (is_gimple_call (call_stmt));
785 }
786
787 if (free_edges)
788 {
789 edge = free_edges;
790 free_edges = NEXT_FREE_EDGE (edge);
791 }
792 else
793 {
794 edge = ggc_alloc_cgraph_edge ();
795 edge->uid = cgraph_edge_max_uid++;
796 }
797
798 edge->aux = NULL;
799 edge->caller = caller;
800 edge->callee = callee;
801 edge->prev_caller = NULL;
802 edge->next_caller = NULL;
803 edge->prev_callee = NULL;
804 edge->next_callee = NULL;
805
806 edge->count = count;
807 gcc_assert (count >= 0);
808 edge->frequency = freq;
809 gcc_assert (freq >= 0);
810 gcc_assert (freq <= CGRAPH_FREQ_MAX);
811
812 edge->call_stmt = call_stmt;
813 push_cfun (DECL_STRUCT_FUNCTION (caller->symbol.decl));
814 edge->can_throw_external
815 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
816 pop_cfun ();
817 if (call_stmt
818 && callee && callee->symbol.decl
819 && !gimple_check_call_matching_types (call_stmt, callee->symbol.decl))
820 edge->call_stmt_cannot_inline_p = true;
821 else
822 edge->call_stmt_cannot_inline_p = false;
823 if (call_stmt && caller->call_site_hash)
824 cgraph_add_edge_to_call_site_hash (edge);
825
826 edge->indirect_info = NULL;
827 edge->indirect_inlining_edge = 0;
828
829 return edge;
830 }
831
832 /* Create edge from CALLER to CALLEE in the cgraph. */
833
834 struct cgraph_edge *
835 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
836 gimple call_stmt, gcov_type count, int freq)
837 {
838 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
839 count, freq);
840
841 edge->indirect_unknown_callee = 0;
842 initialize_inline_failed (edge);
843
844 edge->next_caller = callee->callers;
845 if (callee->callers)
846 callee->callers->prev_caller = edge;
847 edge->next_callee = caller->callees;
848 if (caller->callees)
849 caller->callees->prev_callee = edge;
850 caller->callees = edge;
851 callee->callers = edge;
852
853 return edge;
854 }
855
856 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
857
858 struct cgraph_indirect_call_info *
859 cgraph_allocate_init_indirect_info (void)
860 {
861 struct cgraph_indirect_call_info *ii;
862
863 ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
864 ii->param_index = -1;
865 return ii;
866 }
867
868 /* Create an indirect edge with a yet-undetermined callee where the call
869 statement destination is a formal parameter of the caller with index
870 PARAM_INDEX. */
871
872 struct cgraph_edge *
873 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
874 int ecf_flags,
875 gcov_type count, int freq)
876 {
877 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
878 count, freq);
879
880 edge->indirect_unknown_callee = 1;
881 initialize_inline_failed (edge);
882
883 edge->indirect_info = cgraph_allocate_init_indirect_info ();
884 edge->indirect_info->ecf_flags = ecf_flags;
885
886 edge->next_callee = caller->indirect_calls;
887 if (caller->indirect_calls)
888 caller->indirect_calls->prev_callee = edge;
889 caller->indirect_calls = edge;
890
891 return edge;
892 }
893
894 /* Remove the edge E from the list of the callers of the callee. */
895
896 static inline void
897 cgraph_edge_remove_callee (struct cgraph_edge *e)
898 {
899 gcc_assert (!e->indirect_unknown_callee);
900 if (e->prev_caller)
901 e->prev_caller->next_caller = e->next_caller;
902 if (e->next_caller)
903 e->next_caller->prev_caller = e->prev_caller;
904 if (!e->prev_caller)
905 e->callee->callers = e->next_caller;
906 }
907
908 /* Remove the edge E from the list of the callees of the caller. */
909
910 static inline void
911 cgraph_edge_remove_caller (struct cgraph_edge *e)
912 {
913 if (e->prev_callee)
914 e->prev_callee->next_callee = e->next_callee;
915 if (e->next_callee)
916 e->next_callee->prev_callee = e->prev_callee;
917 if (!e->prev_callee)
918 {
919 if (e->indirect_unknown_callee)
920 e->caller->indirect_calls = e->next_callee;
921 else
922 e->caller->callees = e->next_callee;
923 }
924 if (e->caller->call_site_hash)
925 htab_remove_elt_with_hash (e->caller->call_site_hash,
926 e->call_stmt,
927 htab_hash_pointer (e->call_stmt));
928 }
929
930 /* Put the edge onto the free list. */
931
932 static void
933 cgraph_free_edge (struct cgraph_edge *e)
934 {
935 int uid = e->uid;
936
937 /* Clear out the edge so we do not dangle pointers. */
938 memset (e, 0, sizeof (*e));
939 e->uid = uid;
940 NEXT_FREE_EDGE (e) = free_edges;
941 free_edges = e;
942 }
943
944 /* Remove the edge E in the cgraph. */
945
946 void
947 cgraph_remove_edge (struct cgraph_edge *e)
948 {
949 /* Call all edge removal hooks. */
950 cgraph_call_edge_removal_hooks (e);
951
952 if (!e->indirect_unknown_callee)
953 /* Remove from callers list of the callee. */
954 cgraph_edge_remove_callee (e);
955
956 /* Remove from callees list of the callers. */
957 cgraph_edge_remove_caller (e);
958
959 /* Put the edge onto the free list. */
960 cgraph_free_edge (e);
961 }
962
963 /* Set callee of call graph edge E and add it to the corresponding set of
964 callers. */
965
966 static void
967 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
968 {
969 e->prev_caller = NULL;
970 if (n->callers)
971 n->callers->prev_caller = e;
972 e->next_caller = n->callers;
973 n->callers = e;
974 e->callee = n;
975 }
976
977 /* Redirect callee of E to N. The function does not update underlying
978 call expression. */
979
980 void
981 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
982 {
983 /* Remove from callers list of the current callee. */
984 cgraph_edge_remove_callee (e);
985
986 /* Insert to callers list of the new callee. */
987 cgraph_set_edge_callee (e, n);
988 }
989
990 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
991 CALLEE. DELTA is an integer constant that is to be added to the this
992 pointer (first parameter) to compensate for skipping a thunk adjustment. */
993
994 void
995 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee)
996 {
997 edge->indirect_unknown_callee = 0;
998
999 /* Get the edge out of the indirect edge list. */
1000 if (edge->prev_callee)
1001 edge->prev_callee->next_callee = edge->next_callee;
1002 if (edge->next_callee)
1003 edge->next_callee->prev_callee = edge->prev_callee;
1004 if (!edge->prev_callee)
1005 edge->caller->indirect_calls = edge->next_callee;
1006
1007 /* Put it into the normal callee list */
1008 edge->prev_callee = NULL;
1009 edge->next_callee = edge->caller->callees;
1010 if (edge->caller->callees)
1011 edge->caller->callees->prev_callee = edge;
1012 edge->caller->callees = edge;
1013
1014 /* Insert to callers list of the new callee. */
1015 cgraph_set_edge_callee (edge, callee);
1016
1017 if (edge->call_stmt)
1018 edge->call_stmt_cannot_inline_p
1019 = !gimple_check_call_matching_types (edge->call_stmt, callee->symbol.decl);
1020
1021 /* We need to re-determine the inlining status of the edge. */
1022 initialize_inline_failed (edge);
1023 }
1024
1025 /* If necessary, change the function declaration in the call statement
1026 associated with E so that it corresponds to the edge callee. */
1027
1028 gimple
1029 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
1030 {
1031 tree decl = gimple_call_fndecl (e->call_stmt);
1032 gimple new_stmt;
1033 gimple_stmt_iterator gsi;
1034 #ifdef ENABLE_CHECKING
1035 struct cgraph_node *node;
1036 #endif
1037
1038 if (e->indirect_unknown_callee
1039 || decl == e->callee->symbol.decl)
1040 return e->call_stmt;
1041
1042 #ifdef ENABLE_CHECKING
1043 if (decl)
1044 {
1045 node = cgraph_get_node (decl);
1046 gcc_assert (!node || !node->clone.combined_args_to_skip);
1047 }
1048 #endif
1049
1050 if (cgraph_dump_file)
1051 {
1052 fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
1053 xstrdup (cgraph_node_name (e->caller)), e->caller->symbol.order,
1054 xstrdup (cgraph_node_name (e->callee)), e->callee->symbol.order);
1055 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1056 if (e->callee->clone.combined_args_to_skip)
1057 {
1058 fprintf (cgraph_dump_file, " combined args to skip: ");
1059 dump_bitmap (cgraph_dump_file,
1060 e->callee->clone.combined_args_to_skip);
1061 }
1062 }
1063
1064 if (e->callee->clone.combined_args_to_skip)
1065 {
1066 int lp_nr;
1067
1068 new_stmt
1069 = gimple_call_copy_skip_args (e->call_stmt,
1070 e->callee->clone.combined_args_to_skip);
1071 gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl);
1072 gimple_call_set_fntype (new_stmt, gimple_call_fntype (e->call_stmt));
1073
1074 if (gimple_vdef (new_stmt)
1075 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1076 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1077
1078 gsi = gsi_for_stmt (e->call_stmt);
1079 gsi_replace (&gsi, new_stmt, false);
1080 /* We need to defer cleaning EH info on the new statement to
1081 fixup-cfg. We may not have dominator information at this point
1082 and thus would end up with unreachable blocks and have no way
1083 to communicate that we need to run CFG cleanup then. */
1084 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1085 if (lp_nr != 0)
1086 {
1087 remove_stmt_from_eh_lp (e->call_stmt);
1088 add_stmt_to_eh_lp (new_stmt, lp_nr);
1089 }
1090 }
1091 else
1092 {
1093 new_stmt = e->call_stmt;
1094 gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl);
1095 update_stmt (new_stmt);
1096 }
1097
1098 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt, new_stmt);
1099
1100 if (cgraph_dump_file)
1101 {
1102 fprintf (cgraph_dump_file, " updated to:");
1103 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1104 }
1105 return new_stmt;
1106 }
1107
1108 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1109 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1110 of OLD_STMT if it was previously call statement.
1111 If NEW_STMT is NULL, the call has been dropped without any
1112 replacement. */
1113
1114 static void
1115 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1116 gimple old_stmt, tree old_call,
1117 gimple new_stmt)
1118 {
1119 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1120 ? gimple_call_fndecl (new_stmt) : 0;
1121
1122 /* We are seeing indirect calls, then there is nothing to update. */
1123 if (!new_call && !old_call)
1124 return;
1125 /* See if we turned indirect call into direct call or folded call to one builtin
1126 into different builtin. */
1127 if (old_call != new_call)
1128 {
1129 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1130 struct cgraph_edge *ne = NULL;
1131 gcov_type count;
1132 int frequency;
1133
1134 if (e)
1135 {
1136 /* See if the edge is already there and has the correct callee. It
1137 might be so because of indirect inlining has already updated
1138 it. We also might've cloned and redirected the edge. */
1139 if (new_call && e->callee)
1140 {
1141 struct cgraph_node *callee = e->callee;
1142 while (callee)
1143 {
1144 if (callee->symbol.decl == new_call
1145 || callee->former_clone_of == new_call)
1146 return;
1147 callee = callee->clone_of;
1148 }
1149 }
1150
1151 /* Otherwise remove edge and create new one; we can't simply redirect
1152 since function has changed, so inline plan and other information
1153 attached to edge is invalid. */
1154 count = e->count;
1155 frequency = e->frequency;
1156 cgraph_remove_edge (e);
1157 }
1158 else if (new_call)
1159 {
1160 /* We are seeing new direct call; compute profile info based on BB. */
1161 basic_block bb = gimple_bb (new_stmt);
1162 count = bb->count;
1163 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1164 bb);
1165 }
1166
1167 if (new_call)
1168 {
1169 ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1170 new_stmt, count, frequency);
1171 gcc_assert (ne->inline_failed);
1172 }
1173 }
1174 /* We only updated the call stmt; update pointer in cgraph edge.. */
1175 else if (old_stmt != new_stmt)
1176 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1177 }
1178
1179 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1180 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1181 of OLD_STMT before it was updated (updating can happen inplace). */
1182
1183 void
1184 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1185 {
1186 struct cgraph_node *orig = cgraph_get_node (cfun->decl);
1187 struct cgraph_node *node;
1188
1189 gcc_checking_assert (orig);
1190 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1191 if (orig->clones)
1192 for (node = orig->clones; node != orig;)
1193 {
1194 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1195 if (node->clones)
1196 node = node->clones;
1197 else if (node->next_sibling_clone)
1198 node = node->next_sibling_clone;
1199 else
1200 {
1201 while (node != orig && !node->next_sibling_clone)
1202 node = node->clone_of;
1203 if (node != orig)
1204 node = node->next_sibling_clone;
1205 }
1206 }
1207 }
1208
1209
1210 /* Remove all callees from the node. */
1211
1212 void
1213 cgraph_node_remove_callees (struct cgraph_node *node)
1214 {
1215 struct cgraph_edge *e, *f;
1216
1217 /* It is sufficient to remove the edges from the lists of callers of
1218 the callees. The callee list of the node can be zapped with one
1219 assignment. */
1220 for (e = node->callees; e; e = f)
1221 {
1222 f = e->next_callee;
1223 cgraph_call_edge_removal_hooks (e);
1224 if (!e->indirect_unknown_callee)
1225 cgraph_edge_remove_callee (e);
1226 cgraph_free_edge (e);
1227 }
1228 for (e = node->indirect_calls; e; e = f)
1229 {
1230 f = e->next_callee;
1231 cgraph_call_edge_removal_hooks (e);
1232 if (!e->indirect_unknown_callee)
1233 cgraph_edge_remove_callee (e);
1234 cgraph_free_edge (e);
1235 }
1236 node->indirect_calls = NULL;
1237 node->callees = NULL;
1238 if (node->call_site_hash)
1239 {
1240 htab_delete (node->call_site_hash);
1241 node->call_site_hash = NULL;
1242 }
1243 }
1244
1245 /* Remove all callers from the node. */
1246
1247 static void
1248 cgraph_node_remove_callers (struct cgraph_node *node)
1249 {
1250 struct cgraph_edge *e, *f;
1251
1252 /* It is sufficient to remove the edges from the lists of callees of
1253 the callers. The caller list of the node can be zapped with one
1254 assignment. */
1255 for (e = node->callers; e; e = f)
1256 {
1257 f = e->next_caller;
1258 cgraph_call_edge_removal_hooks (e);
1259 cgraph_edge_remove_caller (e);
1260 cgraph_free_edge (e);
1261 }
1262 node->callers = NULL;
1263 }
1264
1265 /* Release memory used to represent body of function NODE.
1266 Use this only for functions that are released before being translated to
1267 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1268 are free'd in final.c via free_after_compilation(). */
1269
1270 void
1271 cgraph_release_function_body (struct cgraph_node *node)
1272 {
1273 if (DECL_STRUCT_FUNCTION (node->symbol.decl))
1274 {
1275 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1276 if (cfun->cfg
1277 && current_loops)
1278 {
1279 cfun->curr_properties &= ~PROP_loops;
1280 loop_optimizer_finalize ();
1281 }
1282 if (cfun->gimple_df)
1283 {
1284 delete_tree_ssa ();
1285 delete_tree_cfg_annotations ();
1286 cfun->eh = NULL;
1287 }
1288 if (cfun->cfg)
1289 {
1290 gcc_assert (dom_computed[0] == DOM_NONE);
1291 gcc_assert (dom_computed[1] == DOM_NONE);
1292 clear_edges ();
1293 cfun->cfg = NULL;
1294 }
1295 if (cfun->value_histograms)
1296 free_histograms ();
1297 pop_cfun();
1298 gimple_set_body (node->symbol.decl, NULL);
1299 node->ipa_transforms_to_apply.release ();
1300 /* Struct function hangs a lot of data that would leak if we didn't
1301 removed all pointers to it. */
1302 ggc_free (DECL_STRUCT_FUNCTION (node->symbol.decl));
1303 DECL_STRUCT_FUNCTION (node->symbol.decl) = NULL;
1304 }
1305 DECL_SAVED_TREE (node->symbol.decl) = NULL;
1306 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1307 of its associated function function declaration because it's
1308 needed to emit debug info later. */
1309 if (!node->abstract_and_needed && DECL_INITIAL (node->symbol.decl))
1310 DECL_INITIAL (node->symbol.decl) = error_mark_node;
1311 }
1312
1313 /* Remove the node from cgraph. */
1314
1315 void
1316 cgraph_remove_node (struct cgraph_node *node)
1317 {
1318 struct cgraph_node *n;
1319 int uid = node->uid;
1320
1321 cgraph_call_node_removal_hooks (node);
1322 cgraph_node_remove_callers (node);
1323 cgraph_node_remove_callees (node);
1324 node->ipa_transforms_to_apply.release ();
1325
1326 /* Incremental inlining access removed nodes stored in the postorder list.
1327 */
1328 node->symbol.force_output = false;
1329 for (n = node->nested; n; n = n->next_nested)
1330 n->origin = NULL;
1331 node->nested = NULL;
1332 if (node->origin)
1333 {
1334 struct cgraph_node **node2 = &node->origin->nested;
1335
1336 while (*node2 != node)
1337 node2 = &(*node2)->next_nested;
1338 *node2 = node->next_nested;
1339 }
1340 symtab_unregister_node ((symtab_node)node);
1341 if (node->prev_sibling_clone)
1342 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1343 else if (node->clone_of)
1344 node->clone_of->clones = node->next_sibling_clone;
1345 if (node->next_sibling_clone)
1346 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1347 if (node->clones)
1348 {
1349 struct cgraph_node *n, *next;
1350
1351 if (node->clone_of)
1352 {
1353 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1354 n->clone_of = node->clone_of;
1355 n->clone_of = node->clone_of;
1356 n->next_sibling_clone = node->clone_of->clones;
1357 if (node->clone_of->clones)
1358 node->clone_of->clones->prev_sibling_clone = n;
1359 node->clone_of->clones = node->clones;
1360 }
1361 else
1362 {
1363 /* We are removing node with clones. This makes clones inconsistent,
1364 but assume they will be removed subsequently and just keep clone
1365 tree intact. This can happen in unreachable function removal since
1366 we remove unreachable functions in random order, not by bottom-up
1367 walk of clone trees. */
1368 for (n = node->clones; n; n = next)
1369 {
1370 next = n->next_sibling_clone;
1371 n->next_sibling_clone = NULL;
1372 n->prev_sibling_clone = NULL;
1373 n->clone_of = NULL;
1374 }
1375 }
1376 }
1377
1378 /* While all the clones are removed after being proceeded, the function
1379 itself is kept in the cgraph even after it is compiled. Check whether
1380 we are done with this body and reclaim it proactively if this is the case.
1381 */
1382 n = cgraph_get_node (node->symbol.decl);
1383 if (!n
1384 || (!n->clones && !n->clone_of && !n->global.inlined_to
1385 && (cgraph_global_info_ready
1386 && (TREE_ASM_WRITTEN (n->symbol.decl)
1387 || DECL_EXTERNAL (n->symbol.decl)
1388 || !n->symbol.analyzed
1389 || n->symbol.in_other_partition))))
1390 cgraph_release_function_body (node);
1391
1392 node->symbol.decl = NULL;
1393 if (node->call_site_hash)
1394 {
1395 htab_delete (node->call_site_hash);
1396 node->call_site_hash = NULL;
1397 }
1398 cgraph_n_nodes--;
1399
1400 /* Clear out the node to NULL all pointers and add the node to the free
1401 list. */
1402 memset (node, 0, sizeof(*node));
1403 node->symbol.type = SYMTAB_FUNCTION;
1404 node->uid = uid;
1405 SET_NEXT_FREE_NODE (node, free_nodes);
1406 free_nodes = node;
1407 }
1408
1409 /* Likewise indicate that a node is having address taken. */
1410
1411 void
1412 cgraph_mark_address_taken_node (struct cgraph_node *node)
1413 {
1414 /* Indirect inlining can figure out that all uses of the address are
1415 inlined. */
1416 if (node->global.inlined_to)
1417 {
1418 gcc_assert (cfun->after_inlining);
1419 gcc_assert (node->callers->indirect_inlining_edge);
1420 return;
1421 }
1422 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1423 IPA_REF_ADDR reference exists (and thus it should be set on node
1424 representing alias we take address of) and as a test whether address
1425 of the object was taken (and thus it should be set on node alias is
1426 referring to). We should remove the first use and the remove the
1427 following set. */
1428 node->symbol.address_taken = 1;
1429 node = cgraph_function_or_thunk_node (node, NULL);
1430 node->symbol.address_taken = 1;
1431 }
1432
1433 /* Return local info for the compiled function. */
1434
1435 struct cgraph_local_info *
1436 cgraph_local_info (tree decl)
1437 {
1438 struct cgraph_node *node;
1439
1440 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1441 node = cgraph_get_node (decl);
1442 if (!node)
1443 return NULL;
1444 return &node->local;
1445 }
1446
1447 /* Return local info for the compiled function. */
1448
1449 struct cgraph_global_info *
1450 cgraph_global_info (tree decl)
1451 {
1452 struct cgraph_node *node;
1453
1454 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1455 node = cgraph_get_node (decl);
1456 if (!node)
1457 return NULL;
1458 return &node->global;
1459 }
1460
1461 /* Return local info for the compiled function. */
1462
1463 struct cgraph_rtl_info *
1464 cgraph_rtl_info (tree decl)
1465 {
1466 struct cgraph_node *node;
1467
1468 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1469 node = cgraph_get_node (decl);
1470 if (!node
1471 || (decl != current_function_decl
1472 && !TREE_ASM_WRITTEN (node->symbol.decl)))
1473 return NULL;
1474 return &node->rtl;
1475 }
1476
1477 /* Return a string describing the failure REASON. */
1478
1479 const char*
1480 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1481 {
1482 #undef DEFCIFCODE
1483 #define DEFCIFCODE(code, string) string,
1484
1485 static const char *cif_string_table[CIF_N_REASONS] = {
1486 #include "cif-code.def"
1487 };
1488
1489 /* Signedness of an enum type is implementation defined, so cast it
1490 to unsigned before testing. */
1491 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1492 return cif_string_table[reason];
1493 }
1494
1495 /* Names used to print out the availability enum. */
1496 const char * const cgraph_availability_names[] =
1497 {"unset", "not_available", "overwritable", "available", "local"};
1498
1499
1500 /* Dump call graph node NODE to file F. */
1501
1502 void
1503 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1504 {
1505 struct cgraph_edge *edge;
1506 int indirect_calls_count = 0;
1507
1508 dump_symtab_base (f, (symtab_node) node);
1509
1510 if (node->global.inlined_to)
1511 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
1512 xstrdup (cgraph_node_name (node)),
1513 node->symbol.order,
1514 xstrdup (cgraph_node_name (node->global.inlined_to)),
1515 node->global.inlined_to->symbol.order);
1516 if (node->clone_of)
1517 fprintf (f, " Clone of %s/%i\n",
1518 cgraph_node_asm_name (node->clone_of),
1519 node->clone_of->symbol.order);
1520 if (cgraph_function_flags_ready)
1521 fprintf (f, " Availability: %s\n",
1522 cgraph_availability_names [cgraph_function_body_availability (node)]);
1523
1524 fprintf (f, " Function flags:");
1525 if (node->count)
1526 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1527 (HOST_WIDEST_INT)node->count);
1528 if (node->origin)
1529 fprintf (f, " nested in: %s", cgraph_node_asm_name (node->origin));
1530 if (gimple_has_body_p (node->symbol.decl))
1531 fprintf (f, " body");
1532 if (node->process)
1533 fprintf (f, " process");
1534 if (node->local.local)
1535 fprintf (f, " local");
1536 if (node->local.redefined_extern_inline)
1537 fprintf (f, " redefined_extern_inline");
1538 if (node->only_called_at_startup)
1539 fprintf (f, " only_called_at_startup");
1540 if (node->only_called_at_exit)
1541 fprintf (f, " only_called_at_exit");
1542 if (node->tm_clone)
1543 fprintf (f, " tm_clone");
1544
1545 fprintf (f, "\n");
1546
1547 if (node->thunk.thunk_p)
1548 {
1549 fprintf (f, " Thunk");
1550 if (node->thunk.alias)
1551 fprintf (f, " of %s (asm: %s)",
1552 lang_hooks.decl_printable_name (node->thunk.alias, 2),
1553 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1554 fprintf (f, " fixed offset %i virtual value %i has "
1555 "virtual offset %i)\n",
1556 (int)node->thunk.fixed_offset,
1557 (int)node->thunk.virtual_value,
1558 (int)node->thunk.virtual_offset_p);
1559 }
1560 if (node->symbol.alias && node->thunk.alias
1561 && DECL_P (node->thunk.alias))
1562 {
1563 fprintf (f, " Alias of %s",
1564 lang_hooks.decl_printable_name (node->thunk.alias, 2));
1565 if (DECL_ASSEMBLER_NAME_SET_P (node->thunk.alias))
1566 fprintf (f, " (asm: %s)",
1567 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1568 fprintf (f, "\n");
1569 }
1570
1571 fprintf (f, " Called by: ");
1572
1573 for (edge = node->callers; edge; edge = edge->next_caller)
1574 {
1575 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->caller),
1576 edge->caller->symbol.order);
1577 if (edge->count)
1578 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1579 (HOST_WIDEST_INT)edge->count);
1580 if (edge->frequency)
1581 fprintf (f, "(%.2f per call) ",
1582 edge->frequency / (double)CGRAPH_FREQ_BASE);
1583 if (!edge->inline_failed)
1584 fprintf(f, "(inlined) ");
1585 if (edge->indirect_inlining_edge)
1586 fprintf(f, "(indirect_inlining) ");
1587 if (edge->can_throw_external)
1588 fprintf(f, "(can throw external) ");
1589 }
1590
1591 fprintf (f, "\n Calls: ");
1592 for (edge = node->callees; edge; edge = edge->next_callee)
1593 {
1594 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->callee),
1595 edge->callee->symbol.order);
1596 if (!edge->inline_failed)
1597 fprintf(f, "(inlined) ");
1598 if (edge->indirect_inlining_edge)
1599 fprintf(f, "(indirect_inlining) ");
1600 if (edge->count)
1601 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1602 (HOST_WIDEST_INT)edge->count);
1603 if (edge->frequency)
1604 fprintf (f, "(%.2f per call) ",
1605 edge->frequency / (double)CGRAPH_FREQ_BASE);
1606 if (edge->can_throw_external)
1607 fprintf(f, "(can throw external) ");
1608 }
1609 fprintf (f, "\n");
1610
1611 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1612 indirect_calls_count++;
1613 if (indirect_calls_count)
1614 fprintf (f, " Has %i outgoing edges for indirect calls.\n",
1615 indirect_calls_count);
1616 }
1617
1618
1619 /* Dump call graph node NODE to stderr. */
1620
1621 DEBUG_FUNCTION void
1622 debug_cgraph_node (struct cgraph_node *node)
1623 {
1624 dump_cgraph_node (stderr, node);
1625 }
1626
1627
1628 /* Dump the callgraph to file F. */
1629
1630 void
1631 dump_cgraph (FILE *f)
1632 {
1633 struct cgraph_node *node;
1634
1635 fprintf (f, "callgraph:\n\n");
1636 FOR_EACH_FUNCTION (node)
1637 dump_cgraph_node (f, node);
1638 }
1639
1640
1641 /* Dump the call graph to stderr. */
1642
1643 DEBUG_FUNCTION void
1644 debug_cgraph (void)
1645 {
1646 dump_cgraph (stderr);
1647 }
1648
1649 /* Return true when the DECL can possibly be inlined. */
1650 bool
1651 cgraph_function_possibly_inlined_p (tree decl)
1652 {
1653 if (!cgraph_global_info_ready)
1654 return !DECL_UNINLINABLE (decl);
1655 return DECL_POSSIBLY_INLINED (decl);
1656 }
1657
1658 /* NODE is no longer nested function; update cgraph accordingly. */
1659 void
1660 cgraph_unnest_node (struct cgraph_node *node)
1661 {
1662 struct cgraph_node **node2 = &node->origin->nested;
1663 gcc_assert (node->origin);
1664
1665 while (*node2 != node)
1666 node2 = &(*node2)->next_nested;
1667 *node2 = node->next_nested;
1668 node->origin = NULL;
1669 }
1670
1671 /* Return function availability. See cgraph.h for description of individual
1672 return values. */
1673 enum availability
1674 cgraph_function_body_availability (struct cgraph_node *node)
1675 {
1676 enum availability avail;
1677 gcc_assert (cgraph_function_flags_ready);
1678 if (!node->symbol.analyzed)
1679 avail = AVAIL_NOT_AVAILABLE;
1680 else if (node->local.local)
1681 avail = AVAIL_LOCAL;
1682 else if (!node->symbol.externally_visible)
1683 avail = AVAIL_AVAILABLE;
1684 /* Inline functions are safe to be analyzed even if their symbol can
1685 be overwritten at runtime. It is not meaningful to enforce any sane
1686 behaviour on replacing inline function by different body. */
1687 else if (DECL_DECLARED_INLINE_P (node->symbol.decl))
1688 avail = AVAIL_AVAILABLE;
1689
1690 /* If the function can be overwritten, return OVERWRITABLE. Take
1691 care at least of two notable extensions - the COMDAT functions
1692 used to share template instantiations in C++ (this is symmetric
1693 to code cp_cannot_inline_tree_fn and probably shall be shared and
1694 the inlinability hooks completely eliminated).
1695
1696 ??? Does the C++ one definition rule allow us to always return
1697 AVAIL_AVAILABLE here? That would be good reason to preserve this
1698 bit. */
1699
1700 else if (decl_replaceable_p (node->symbol.decl)
1701 && !DECL_EXTERNAL (node->symbol.decl))
1702 avail = AVAIL_OVERWRITABLE;
1703 else avail = AVAIL_AVAILABLE;
1704
1705 return avail;
1706 }
1707
1708 /* Worker for cgraph_node_can_be_local_p. */
1709 static bool
1710 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node,
1711 void *data ATTRIBUTE_UNUSED)
1712 {
1713 return !(!node->symbol.force_output
1714 && ((DECL_COMDAT (node->symbol.decl)
1715 && !node->symbol.same_comdat_group)
1716 || !node->symbol.externally_visible));
1717 }
1718
1719 /* Return true if NODE can be made local for API change.
1720 Extern inline functions and C++ COMDAT functions can be made local
1721 at the expense of possible code size growth if function is used in multiple
1722 compilation units. */
1723 bool
1724 cgraph_node_can_be_local_p (struct cgraph_node *node)
1725 {
1726 return (!node->symbol.address_taken
1727 && !cgraph_for_node_and_aliases (node,
1728 cgraph_node_cannot_be_local_p_1,
1729 NULL, true));
1730 }
1731
1732 /* Call calback on NODE, thunks and aliases associated to NODE.
1733 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1734 skipped. */
1735
1736 bool
1737 cgraph_for_node_thunks_and_aliases (struct cgraph_node *node,
1738 bool (*callback) (struct cgraph_node *, void *),
1739 void *data,
1740 bool include_overwritable)
1741 {
1742 struct cgraph_edge *e;
1743 int i;
1744 struct ipa_ref *ref;
1745
1746 if (callback (node, data))
1747 return true;
1748 for (e = node->callers; e; e = e->next_caller)
1749 if (e->caller->thunk.thunk_p
1750 && (include_overwritable
1751 || cgraph_function_body_availability (e->caller) > AVAIL_OVERWRITABLE))
1752 if (cgraph_for_node_thunks_and_aliases (e->caller, callback, data,
1753 include_overwritable))
1754 return true;
1755 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
1756 if (ref->use == IPA_REF_ALIAS)
1757 {
1758 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1759 if (include_overwritable
1760 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
1761 if (cgraph_for_node_thunks_and_aliases (alias, callback, data,
1762 include_overwritable))
1763 return true;
1764 }
1765 return false;
1766 }
1767
1768 /* Call calback on NODE and aliases associated to NODE.
1769 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1770 skipped. */
1771
1772 bool
1773 cgraph_for_node_and_aliases (struct cgraph_node *node,
1774 bool (*callback) (struct cgraph_node *, void *),
1775 void *data,
1776 bool include_overwritable)
1777 {
1778 int i;
1779 struct ipa_ref *ref;
1780
1781 if (callback (node, data))
1782 return true;
1783 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
1784 if (ref->use == IPA_REF_ALIAS)
1785 {
1786 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1787 if (include_overwritable
1788 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
1789 if (cgraph_for_node_and_aliases (alias, callback, data,
1790 include_overwritable))
1791 return true;
1792 }
1793 return false;
1794 }
1795
1796 /* Worker to bring NODE local. */
1797
1798 static bool
1799 cgraph_make_node_local_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1800 {
1801 gcc_checking_assert (cgraph_node_can_be_local_p (node));
1802 if (DECL_COMDAT (node->symbol.decl) || DECL_EXTERNAL (node->symbol.decl))
1803 {
1804 symtab_make_decl_local (node->symbol.decl);
1805
1806 node->symbol.externally_visible = false;
1807 node->local.local = true;
1808 node->symbol.unique_name = (node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
1809 || node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
1810 node->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
1811 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
1812 }
1813 return false;
1814 }
1815
1816 /* Bring NODE local. */
1817
1818 void
1819 cgraph_make_node_local (struct cgraph_node *node)
1820 {
1821 cgraph_for_node_thunks_and_aliases (node, cgraph_make_node_local_1,
1822 NULL, true);
1823 }
1824
1825 /* Worker to set nothrow flag. */
1826
1827 static bool
1828 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
1829 {
1830 struct cgraph_edge *e;
1831
1832 TREE_NOTHROW (node->symbol.decl) = data != NULL;
1833
1834 if (data != NULL)
1835 for (e = node->callers; e; e = e->next_caller)
1836 e->can_throw_external = false;
1837 return false;
1838 }
1839
1840 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
1841 if any to NOTHROW. */
1842
1843 void
1844 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
1845 {
1846 cgraph_for_node_thunks_and_aliases (node, cgraph_set_nothrow_flag_1,
1847 (void *)(size_t)nothrow, false);
1848 }
1849
1850 /* Worker to set const flag. */
1851
1852 static bool
1853 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
1854 {
1855 /* Static constructors and destructors without a side effect can be
1856 optimized out. */
1857 if (data && !((size_t)data & 2))
1858 {
1859 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
1860 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
1861 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1862 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
1863 }
1864 TREE_READONLY (node->symbol.decl) = data != NULL;
1865 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
1866 return false;
1867 }
1868
1869 /* Set TREE_READONLY on NODE's decl and on aliases of NODE
1870 if any to READONLY. */
1871
1872 void
1873 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
1874 {
1875 cgraph_for_node_thunks_and_aliases (node, cgraph_set_const_flag_1,
1876 (void *)(size_t)(readonly + (int)looping * 2),
1877 false);
1878 }
1879
1880 /* Worker to set pure flag. */
1881
1882 static bool
1883 cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
1884 {
1885 /* Static constructors and destructors without a side effect can be
1886 optimized out. */
1887 if (data && !((size_t)data & 2))
1888 {
1889 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
1890 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
1891 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1892 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
1893 }
1894 DECL_PURE_P (node->symbol.decl) = data != NULL;
1895 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
1896 return false;
1897 }
1898
1899 /* Set DECL_PURE_P on NODE's decl and on aliases of NODE
1900 if any to PURE. */
1901
1902 void
1903 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
1904 {
1905 cgraph_for_node_thunks_and_aliases (node, cgraph_set_pure_flag_1,
1906 (void *)(size_t)(pure + (int)looping * 2),
1907 false);
1908 }
1909
1910 /* Data used by cgraph_propagate_frequency. */
1911
1912 struct cgraph_propagate_frequency_data
1913 {
1914 bool maybe_unlikely_executed;
1915 bool maybe_executed_once;
1916 bool only_called_at_startup;
1917 bool only_called_at_exit;
1918 };
1919
1920 /* Worker for cgraph_propagate_frequency_1. */
1921
1922 static bool
1923 cgraph_propagate_frequency_1 (struct cgraph_node *node, void *data)
1924 {
1925 struct cgraph_propagate_frequency_data *d;
1926 struct cgraph_edge *edge;
1927
1928 d = (struct cgraph_propagate_frequency_data *)data;
1929 for (edge = node->callers;
1930 edge && (d->maybe_unlikely_executed || d->maybe_executed_once
1931 || d->only_called_at_startup || d->only_called_at_exit);
1932 edge = edge->next_caller)
1933 {
1934 if (edge->caller != node)
1935 {
1936 d->only_called_at_startup &= edge->caller->only_called_at_startup;
1937 /* It makes sense to put main() together with the static constructors.
1938 It will be executed for sure, but rest of functions called from
1939 main are definitely not at startup only. */
1940 if (MAIN_NAME_P (DECL_NAME (edge->caller->symbol.decl)))
1941 d->only_called_at_startup = 0;
1942 d->only_called_at_exit &= edge->caller->only_called_at_exit;
1943 }
1944 if (!edge->frequency)
1945 continue;
1946 switch (edge->caller->frequency)
1947 {
1948 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
1949 break;
1950 case NODE_FREQUENCY_EXECUTED_ONCE:
1951 if (dump_file && (dump_flags & TDF_DETAILS))
1952 fprintf (dump_file, " Called by %s that is executed once\n",
1953 cgraph_node_name (edge->caller));
1954 d->maybe_unlikely_executed = false;
1955 if (inline_edge_summary (edge)->loop_depth)
1956 {
1957 d->maybe_executed_once = false;
1958 if (dump_file && (dump_flags & TDF_DETAILS))
1959 fprintf (dump_file, " Called in loop\n");
1960 }
1961 break;
1962 case NODE_FREQUENCY_HOT:
1963 case NODE_FREQUENCY_NORMAL:
1964 if (dump_file && (dump_flags & TDF_DETAILS))
1965 fprintf (dump_file, " Called by %s that is normal or hot\n",
1966 cgraph_node_name (edge->caller));
1967 d->maybe_unlikely_executed = false;
1968 d->maybe_executed_once = false;
1969 break;
1970 }
1971 }
1972 return edge != NULL;
1973 }
1974
1975 /* See if the frequency of NODE can be updated based on frequencies of its
1976 callers. */
1977 bool
1978 cgraph_propagate_frequency (struct cgraph_node *node)
1979 {
1980 struct cgraph_propagate_frequency_data d = {true, true, true, true};
1981 bool changed = false;
1982
1983 if (!node->local.local)
1984 return false;
1985 gcc_assert (node->symbol.analyzed);
1986 if (dump_file && (dump_flags & TDF_DETAILS))
1987 fprintf (dump_file, "Processing frequency %s\n", cgraph_node_name (node));
1988
1989 cgraph_for_node_and_aliases (node, cgraph_propagate_frequency_1, &d, true);
1990
1991 if ((d.only_called_at_startup && !d.only_called_at_exit)
1992 && !node->only_called_at_startup)
1993 {
1994 node->only_called_at_startup = true;
1995 if (dump_file)
1996 fprintf (dump_file, "Node %s promoted to only called at startup.\n",
1997 cgraph_node_name (node));
1998 changed = true;
1999 }
2000 if ((d.only_called_at_exit && !d.only_called_at_startup)
2001 && !node->only_called_at_exit)
2002 {
2003 node->only_called_at_exit = true;
2004 if (dump_file)
2005 fprintf (dump_file, "Node %s promoted to only called at exit.\n",
2006 cgraph_node_name (node));
2007 changed = true;
2008 }
2009 /* These come either from profile or user hints; never update them. */
2010 if (node->frequency == NODE_FREQUENCY_HOT
2011 || node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2012 return changed;
2013 if (d.maybe_unlikely_executed)
2014 {
2015 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
2016 if (dump_file)
2017 fprintf (dump_file, "Node %s promoted to unlikely executed.\n",
2018 cgraph_node_name (node));
2019 changed = true;
2020 }
2021 else if (d.maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE)
2022 {
2023 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
2024 if (dump_file)
2025 fprintf (dump_file, "Node %s promoted to executed once.\n",
2026 cgraph_node_name (node));
2027 changed = true;
2028 }
2029 return changed;
2030 }
2031
2032 /* Return true when NODE can not return or throw and thus
2033 it is safe to ignore its side effects for IPA analysis. */
2034
2035 bool
2036 cgraph_node_cannot_return (struct cgraph_node *node)
2037 {
2038 int flags = flags_from_decl_or_type (node->symbol.decl);
2039 if (!flag_exceptions)
2040 return (flags & ECF_NORETURN) != 0;
2041 else
2042 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2043 == (ECF_NORETURN | ECF_NOTHROW));
2044 }
2045
2046 /* Return true when call of E can not lead to return from caller
2047 and thus it is safe to ignore its side effects for IPA analysis
2048 when computing side effects of the caller.
2049 FIXME: We could actually mark all edges that have no reaching
2050 patch to EXIT_BLOCK_PTR or throw to get better results. */
2051 bool
2052 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2053 {
2054 if (cgraph_node_cannot_return (e->caller))
2055 return true;
2056 if (e->indirect_unknown_callee)
2057 {
2058 int flags = e->indirect_info->ecf_flags;
2059 if (!flag_exceptions)
2060 return (flags & ECF_NORETURN) != 0;
2061 else
2062 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2063 == (ECF_NORETURN | ECF_NOTHROW));
2064 }
2065 else
2066 return cgraph_node_cannot_return (e->callee);
2067 }
2068
2069 /* Return true when function NODE can be removed from callgraph
2070 if all direct calls are eliminated. */
2071
2072 bool
2073 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2074 {
2075 gcc_assert (!node->global.inlined_to);
2076 /* Extern inlines can always go, we will use the external definition. */
2077 if (DECL_EXTERNAL (node->symbol.decl))
2078 return true;
2079 /* When function is needed, we can not remove it. */
2080 if (node->symbol.force_output || node->symbol.used_from_other_partition)
2081 return false;
2082 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl)
2083 || DECL_STATIC_DESTRUCTOR (node->symbol.decl))
2084 return false;
2085 /* Only COMDAT functions can be removed if externally visible. */
2086 if (node->symbol.externally_visible
2087 && (!DECL_COMDAT (node->symbol.decl)
2088 || symtab_used_from_object_file_p ((symtab_node) node)))
2089 return false;
2090 return true;
2091 }
2092
2093 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2094
2095 static bool
2096 nonremovable_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2097 {
2098 return !cgraph_can_remove_if_no_direct_calls_and_refs_p (node);
2099 }
2100
2101 /* Return true when function NODE and its aliases can be removed from callgraph
2102 if all direct calls are eliminated. */
2103
2104 bool
2105 cgraph_can_remove_if_no_direct_calls_p (struct cgraph_node *node)
2106 {
2107 /* Extern inlines can always go, we will use the external definition. */
2108 if (DECL_EXTERNAL (node->symbol.decl))
2109 return true;
2110 if (node->symbol.address_taken)
2111 return false;
2112 return !cgraph_for_node_and_aliases (node, nonremovable_p, NULL, true);
2113 }
2114
2115 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2116
2117 static bool
2118 used_from_object_file_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2119 {
2120 return symtab_used_from_object_file_p ((symtab_node) node);
2121 }
2122
2123 /* Return true when function NODE can be expected to be removed
2124 from program when direct calls in this compilation unit are removed.
2125
2126 As a special case COMDAT functions are
2127 cgraph_can_remove_if_no_direct_calls_p while the are not
2128 cgraph_only_called_directly_p (it is possible they are called from other
2129 unit)
2130
2131 This function behaves as cgraph_only_called_directly_p because eliminating
2132 all uses of COMDAT function does not make it necessarily disappear from
2133 the program unless we are compiling whole program or we do LTO. In this
2134 case we know we win since dynamic linking will not really discard the
2135 linkonce section. */
2136
2137 bool
2138 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2139 {
2140 gcc_assert (!node->global.inlined_to);
2141 if (cgraph_for_node_and_aliases (node, used_from_object_file_p, NULL, true))
2142 return false;
2143 if (!in_lto_p && !flag_whole_program)
2144 return cgraph_only_called_directly_p (node);
2145 else
2146 {
2147 if (DECL_EXTERNAL (node->symbol.decl))
2148 return true;
2149 return cgraph_can_remove_if_no_direct_calls_p (node);
2150 }
2151 }
2152
2153
2154 /* Worker for cgraph_only_called_directly_p. */
2155
2156 static bool
2157 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2158 {
2159 return !cgraph_only_called_directly_or_aliased_p (node);
2160 }
2161
2162 /* Return true when function NODE and all its aliases are only called
2163 directly.
2164 i.e. it is not externally visible, address was not taken and
2165 it is not used in any other non-standard way. */
2166
2167 bool
2168 cgraph_only_called_directly_p (struct cgraph_node *node)
2169 {
2170 gcc_assert (cgraph_function_or_thunk_node (node, NULL) == node);
2171 return !cgraph_for_node_and_aliases (node, cgraph_not_only_called_directly_p_1,
2172 NULL, true);
2173 }
2174
2175
2176 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2177
2178 static bool
2179 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
2180 {
2181 vec<cgraph_edge_p> *redirect_callers = (vec<cgraph_edge_p> *)data;
2182 struct cgraph_edge *cs;
2183 enum availability avail;
2184 cgraph_function_or_thunk_node (node, &avail);
2185
2186 if (avail > AVAIL_OVERWRITABLE)
2187 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2188 if (!cs->indirect_inlining_edge)
2189 redirect_callers->safe_push (cs);
2190 return false;
2191 }
2192
2193 /* Collect all callers of NODE and its aliases that are known to lead to NODE
2194 (i.e. are not overwritable). */
2195
2196 vec<cgraph_edge_p>
2197 collect_callers_of_node (struct cgraph_node *node)
2198 {
2199 vec<cgraph_edge_p> redirect_callers = vNULL;
2200 cgraph_for_node_and_aliases (node, collect_callers_of_node_1,
2201 &redirect_callers, false);
2202 return redirect_callers;
2203 }
2204
2205 /* Return TRUE if NODE2 is equivalent to NODE or its clone. */
2206 static bool
2207 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
2208 {
2209 node = cgraph_function_or_thunk_node (node, NULL);
2210 node2 = cgraph_function_or_thunk_node (node2, NULL);
2211 while (node != node2 && node2)
2212 node2 = node2->clone_of;
2213 return node2 != NULL;
2214 }
2215
2216 /* Verify edge E count and frequency. */
2217
2218 static bool
2219 verify_edge_count_and_frequency (struct cgraph_edge *e)
2220 {
2221 bool error_found = false;
2222 if (e->count < 0)
2223 {
2224 error ("caller edge count is negative");
2225 error_found = true;
2226 }
2227 if (e->frequency < 0)
2228 {
2229 error ("caller edge frequency is negative");
2230 error_found = true;
2231 }
2232 if (e->frequency > CGRAPH_FREQ_MAX)
2233 {
2234 error ("caller edge frequency is too large");
2235 error_found = true;
2236 }
2237 if (gimple_has_body_p (e->caller->symbol.decl)
2238 && !e->caller->global.inlined_to
2239 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2240 Remove this once edges are actually removed from the function at that time. */
2241 && (e->frequency
2242 || (inline_edge_summary_vec.exists ()
2243 && ((inline_edge_summary_vec.length () <= (unsigned) e->uid)
2244 || !inline_edge_summary (e)->predicate)))
2245 && (e->frequency
2246 != compute_call_stmt_bb_frequency (e->caller->symbol.decl,
2247 gimple_bb (e->call_stmt))))
2248 {
2249 error ("caller edge frequency %i does not match BB frequency %i",
2250 e->frequency,
2251 compute_call_stmt_bb_frequency (e->caller->symbol.decl,
2252 gimple_bb (e->call_stmt)));
2253 error_found = true;
2254 }
2255 return error_found;
2256 }
2257
2258 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2259 static void
2260 cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
2261 {
2262 bool fndecl_was_null = false;
2263 /* debug_gimple_stmt needs correct cfun */
2264 if (cfun != this_cfun)
2265 set_cfun (this_cfun);
2266 /* ...and an actual current_function_decl */
2267 if (!current_function_decl)
2268 {
2269 current_function_decl = this_cfun->decl;
2270 fndecl_was_null = true;
2271 }
2272 debug_gimple_stmt (stmt);
2273 if (fndecl_was_null)
2274 current_function_decl = NULL;
2275 }
2276
2277 /* Verify that call graph edge E corresponds to DECL from the associated
2278 statement. Return true if the verification should fail. */
2279
2280 static bool
2281 verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl)
2282 {
2283 struct cgraph_node *node;
2284
2285 if (!decl || e->callee->global.inlined_to)
2286 return false;
2287 node = cgraph_get_node (decl);
2288
2289 /* We do not know if a node from a different partition is an alias or what it
2290 aliases and therefore cannot do the former_clone_of check reliably. */
2291 if (!node || node->symbol.in_other_partition)
2292 return false;
2293 node = cgraph_function_or_thunk_node (node, NULL);
2294
2295 if (e->callee->former_clone_of != node->symbol.decl
2296 /* IPA-CP sometimes redirect edge to clone and then back to the former
2297 function. This ping-pong has to go, eventually. */
2298 && (node != cgraph_function_or_thunk_node (e->callee, NULL))
2299 && !clone_of_p (cgraph_function_or_thunk_node (node, NULL), e->callee))
2300 return true;
2301 else
2302 return false;
2303 }
2304
2305 /* Verify cgraph nodes of given cgraph node. */
2306 DEBUG_FUNCTION void
2307 verify_cgraph_node (struct cgraph_node *node)
2308 {
2309 struct cgraph_edge *e;
2310 struct function *this_cfun = DECL_STRUCT_FUNCTION (node->symbol.decl);
2311 basic_block this_block;
2312 gimple_stmt_iterator gsi;
2313 bool error_found = false;
2314
2315 if (seen_error ())
2316 return;
2317
2318 timevar_push (TV_CGRAPH_VERIFY);
2319 error_found |= verify_symtab_base ((symtab_node) node);
2320 for (e = node->callees; e; e = e->next_callee)
2321 if (e->aux)
2322 {
2323 error ("aux field set for edge %s->%s",
2324 identifier_to_locale (cgraph_node_name (e->caller)),
2325 identifier_to_locale (cgraph_node_name (e->callee)));
2326 error_found = true;
2327 }
2328 if (node->count < 0)
2329 {
2330 error ("execution count is negative");
2331 error_found = true;
2332 }
2333 if (node->global.inlined_to && node->symbol.same_comdat_group)
2334 {
2335 error ("inline clone in same comdat group list");
2336 error_found = true;
2337 }
2338 if (!node->symbol.definition && node->local.local)
2339 {
2340 error ("local symbols must be defined");
2341 error_found = true;
2342 }
2343 if (node->global.inlined_to && node->symbol.externally_visible)
2344 {
2345 error ("externally visible inline clone");
2346 error_found = true;
2347 }
2348 if (node->global.inlined_to && node->symbol.address_taken)
2349 {
2350 error ("inline clone with address taken");
2351 error_found = true;
2352 }
2353 if (node->global.inlined_to && node->symbol.force_output)
2354 {
2355 error ("inline clone is forced to output");
2356 error_found = true;
2357 }
2358 for (e = node->indirect_calls; e; e = e->next_callee)
2359 {
2360 if (e->aux)
2361 {
2362 error ("aux field set for indirect edge from %s",
2363 identifier_to_locale (cgraph_node_name (e->caller)));
2364 error_found = true;
2365 }
2366 if (!e->indirect_unknown_callee
2367 || !e->indirect_info)
2368 {
2369 error ("An indirect edge from %s is not marked as indirect or has "
2370 "associated indirect_info, the corresponding statement is: ",
2371 identifier_to_locale (cgraph_node_name (e->caller)));
2372 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2373 error_found = true;
2374 }
2375 }
2376 for (e = node->callers; e; e = e->next_caller)
2377 {
2378 if (verify_edge_count_and_frequency (e))
2379 error_found = true;
2380 if (!e->inline_failed)
2381 {
2382 if (node->global.inlined_to
2383 != (e->caller->global.inlined_to
2384 ? e->caller->global.inlined_to : e->caller))
2385 {
2386 error ("inlined_to pointer is wrong");
2387 error_found = true;
2388 }
2389 if (node->callers->next_caller)
2390 {
2391 error ("multiple inline callers");
2392 error_found = true;
2393 }
2394 }
2395 else
2396 if (node->global.inlined_to)
2397 {
2398 error ("inlined_to pointer set for noninline callers");
2399 error_found = true;
2400 }
2401 }
2402 for (e = node->indirect_calls; e; e = e->next_callee)
2403 if (verify_edge_count_and_frequency (e))
2404 error_found = true;
2405 if (!node->callers && node->global.inlined_to)
2406 {
2407 error ("inlined_to pointer is set but no predecessors found");
2408 error_found = true;
2409 }
2410 if (node->global.inlined_to == node)
2411 {
2412 error ("inlined_to pointer refers to itself");
2413 error_found = true;
2414 }
2415
2416 if (node->clone_of)
2417 {
2418 struct cgraph_node *n;
2419 for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
2420 if (n == node)
2421 break;
2422 if (!n)
2423 {
2424 error ("node has wrong clone_of");
2425 error_found = true;
2426 }
2427 }
2428 if (node->clones)
2429 {
2430 struct cgraph_node *n;
2431 for (n = node->clones; n; n = n->next_sibling_clone)
2432 if (n->clone_of != node)
2433 break;
2434 if (n)
2435 {
2436 error ("node has wrong clone list");
2437 error_found = true;
2438 }
2439 }
2440 if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
2441 {
2442 error ("node is in clone list but it is not clone");
2443 error_found = true;
2444 }
2445 if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
2446 {
2447 error ("node has wrong prev_clone pointer");
2448 error_found = true;
2449 }
2450 if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
2451 {
2452 error ("double linked list of clones corrupted");
2453 error_found = true;
2454 }
2455
2456 if (node->symbol.analyzed && node->symbol.alias)
2457 {
2458 bool ref_found = false;
2459 int i;
2460 struct ipa_ref *ref;
2461
2462 if (node->callees)
2463 {
2464 error ("Alias has call edges");
2465 error_found = true;
2466 }
2467 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list,
2468 i, ref); i++)
2469 if (ref->use != IPA_REF_ALIAS)
2470 {
2471 error ("Alias has non-alias reference");
2472 error_found = true;
2473 }
2474 else if (ref_found)
2475 {
2476 error ("Alias has more than one alias reference");
2477 error_found = true;
2478 }
2479 else
2480 ref_found = true;
2481 if (!ref_found)
2482 {
2483 error ("Analyzed alias has no reference");
2484 error_found = true;
2485 }
2486 }
2487 if (node->symbol.analyzed && node->thunk.thunk_p)
2488 {
2489 if (!node->callees)
2490 {
2491 error ("No edge out of thunk node");
2492 error_found = true;
2493 }
2494 else if (node->callees->next_callee)
2495 {
2496 error ("More than one edge out of thunk node");
2497 error_found = true;
2498 }
2499 if (gimple_has_body_p (node->symbol.decl))
2500 {
2501 error ("Thunk is not supposed to have body");
2502 error_found = true;
2503 }
2504 }
2505 else if (node->symbol.analyzed && gimple_has_body_p (node->symbol.decl)
2506 && !TREE_ASM_WRITTEN (node->symbol.decl)
2507 && (!DECL_EXTERNAL (node->symbol.decl) || node->global.inlined_to)
2508 && !flag_wpa)
2509 {
2510 if (this_cfun->cfg)
2511 {
2512 /* Reach the trees by walking over the CFG, and note the
2513 enclosing basic-blocks in the call edges. */
2514 FOR_EACH_BB_FN (this_block, this_cfun)
2515 for (gsi = gsi_start_bb (this_block);
2516 !gsi_end_p (gsi);
2517 gsi_next (&gsi))
2518 {
2519 gimple stmt = gsi_stmt (gsi);
2520 if (is_gimple_call (stmt))
2521 {
2522 struct cgraph_edge *e = cgraph_edge (node, stmt);
2523 tree decl = gimple_call_fndecl (stmt);
2524 if (e)
2525 {
2526 if (e->aux)
2527 {
2528 error ("shared call_stmt:");
2529 cgraph_debug_gimple_stmt (this_cfun, stmt);
2530 error_found = true;
2531 }
2532 if (!e->indirect_unknown_callee)
2533 {
2534 if (verify_edge_corresponds_to_fndecl (e, decl))
2535 {
2536 error ("edge points to wrong declaration:");
2537 debug_tree (e->callee->symbol.decl);
2538 fprintf (stderr," Instead of:");
2539 debug_tree (decl);
2540 error_found = true;
2541 }
2542 }
2543 else if (decl)
2544 {
2545 error ("an indirect edge with unknown callee "
2546 "corresponding to a call_stmt with "
2547 "a known declaration:");
2548 error_found = true;
2549 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2550 }
2551 e->aux = (void *)1;
2552 }
2553 else if (decl)
2554 {
2555 error ("missing callgraph edge for call stmt:");
2556 cgraph_debug_gimple_stmt (this_cfun, stmt);
2557 error_found = true;
2558 }
2559 }
2560 }
2561 }
2562 else
2563 /* No CFG available?! */
2564 gcc_unreachable ();
2565
2566 for (e = node->callees; e; e = e->next_callee)
2567 {
2568 if (!e->aux)
2569 {
2570 error ("edge %s->%s has no corresponding call_stmt",
2571 identifier_to_locale (cgraph_node_name (e->caller)),
2572 identifier_to_locale (cgraph_node_name (e->callee)));
2573 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2574 error_found = true;
2575 }
2576 e->aux = 0;
2577 }
2578 for (e = node->indirect_calls; e; e = e->next_callee)
2579 {
2580 if (!e->aux)
2581 {
2582 error ("an indirect edge from %s has no corresponding call_stmt",
2583 identifier_to_locale (cgraph_node_name (e->caller)));
2584 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2585 error_found = true;
2586 }
2587 e->aux = 0;
2588 }
2589 }
2590 if (error_found)
2591 {
2592 dump_cgraph_node (stderr, node);
2593 internal_error ("verify_cgraph_node failed");
2594 }
2595 timevar_pop (TV_CGRAPH_VERIFY);
2596 }
2597
2598 /* Verify whole cgraph structure. */
2599 DEBUG_FUNCTION void
2600 verify_cgraph (void)
2601 {
2602 struct cgraph_node *node;
2603
2604 if (seen_error ())
2605 return;
2606
2607 FOR_EACH_FUNCTION (node)
2608 verify_cgraph_node (node);
2609 }
2610
2611 /* Create external decl node for DECL.
2612 The difference i nbetween cgraph_get_create_node and
2613 cgraph_get_create_real_symbol_node is that cgraph_get_create_node
2614 may return inline clone, while cgraph_get_create_real_symbol_node
2615 will create a new node in this case.
2616 FIXME: This function should be removed once clones are put out of decl
2617 hash. */
2618
2619 struct cgraph_node *
2620 cgraph_get_create_real_symbol_node (tree decl)
2621 {
2622 struct cgraph_node *first_clone = cgraph_get_node (decl);
2623 struct cgraph_node *node;
2624 /* create symbol table node. even if inline clone exists, we can not take
2625 it as a target of non-inlined call. */
2626 node = cgraph_get_node (decl);
2627 if (node && !node->global.inlined_to)
2628 return node;
2629
2630 node = cgraph_create_node (decl);
2631
2632 /* ok, we previously inlined the function, then removed the offline copy and
2633 now we want it back for external call. this can happen when devirtualizing
2634 while inlining function called once that happens after extern inlined and
2635 virtuals are already removed. in this case introduce the external node
2636 and make it available for call. */
2637 if (first_clone)
2638 {
2639 first_clone->clone_of = node;
2640 node->clones = first_clone;
2641 symtab_prevail_in_asm_name_hash ((symtab_node) node);
2642 symtab_insert_node_to_hashtable ((symtab_node) node);
2643 if (dump_file)
2644 fprintf (dump_file, "Introduced new external node "
2645 "(%s/%i) and turned into root of the clone tree.\n",
2646 xstrdup (cgraph_node_name (node)), node->symbol.order);
2647 }
2648 else if (dump_file)
2649 fprintf (dump_file, "Introduced new external node "
2650 "(%s/%i).\n", xstrdup (cgraph_node_name (node)),
2651 node->symbol.order);
2652 return node;
2653 }
2654
2655
2656 /* Given NODE, walk the alias chain to return the function NODE is alias of.
2657 Walk through thunk, too.
2658 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
2659
2660 struct cgraph_node *
2661 cgraph_function_node (struct cgraph_node *node, enum availability *availability)
2662 {
2663 do
2664 {
2665 node = cgraph_function_or_thunk_node (node, availability);
2666 if (node->thunk.thunk_p)
2667 {
2668 node = node->callees->callee;
2669 if (availability)
2670 {
2671 enum availability a;
2672 a = cgraph_function_body_availability (node);
2673 if (a < *availability)
2674 *availability = a;
2675 }
2676 node = cgraph_function_or_thunk_node (node, availability);
2677 }
2678 } while (node && node->thunk.thunk_p);
2679 return node;
2680 }
2681
2682 #include "gt-cgraph.h"