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