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