2008-09-09 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / cgraph.c
bloba12ed155b458088cd44b81d65b424a788737bb13
1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This file contains basic routines manipulating call graph
24 The callgraph:
26 The call-graph is data structure designed for intra-procedural optimization
27 but it is also used in non-unit-at-a-time compilation to allow easier code
28 sharing.
30 The call-graph consist of nodes and edges represented via linked lists.
31 Each function (external or not) corresponds to the unique node.
33 The mapping from declarations to call-graph nodes is done using hash table
34 based on DECL_UID. The call-graph nodes are created lazily using
35 cgraph_node function when called for unknown declaration.
37 The callgraph at the moment does not represent indirect calls or calls
38 from other compilation unit. Flag NEEDED is set for each node that may
39 be accessed in such an invisible way and it shall be considered an
40 entry point to the callgraph.
42 Interprocedural information:
44 Callgraph is place to store data needed for interprocedural optimization.
45 All data structures are divided into three components: local_info that
46 is produced while analyzing the function, global_info that is result
47 of global walking of the callgraph on the end of compilation and
48 rtl_info used by RTL backend to propagate data from already compiled
49 functions to their callers.
51 Inlining plans:
53 The function inlining information is decided in advance and maintained
54 in the callgraph as so called inline plan.
55 For each inlined call, the callee's node is cloned to represent the
56 new function copy produced by inliner.
57 Each inlined call gets a unique corresponding clone node of the callee
58 and the data structure is updated while inlining is performed, so
59 the clones are eliminated and their callee edges redirected to the
60 caller.
62 Each edge has "inline_failed" field. When the field is set to NULL,
63 the call will be inlined. When it is non-NULL it contains a reason
64 why inlining wasn't performed. */
66 #include "config.h"
67 #include "system.h"
68 #include "coretypes.h"
69 #include "tm.h"
70 #include "tree.h"
71 #include "tree-inline.h"
72 #include "langhooks.h"
73 #include "hashtab.h"
74 #include "toplev.h"
75 #include "flags.h"
76 #include "ggc.h"
77 #include "debug.h"
78 #include "target.h"
79 #include "basic-block.h"
80 #include "cgraph.h"
81 #include "varray.h"
82 #include "output.h"
83 #include "intl.h"
84 #include "gimple.h"
85 #include "tree-dump.h"
86 #include "tree-flow.h"
88 static void cgraph_node_remove_callers (struct cgraph_node *node);
89 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
90 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
92 /* Hash table used to convert declarations into nodes. */
93 static GTY((param_is (struct cgraph_node))) htab_t cgraph_hash;
94 /* Hash table used to convert assembler names into nodes. */
95 static GTY((param_is (struct cgraph_node))) htab_t assembler_name_hash;
97 /* The linked list of cgraph nodes. */
98 struct cgraph_node *cgraph_nodes;
100 /* Queue of cgraph nodes scheduled to be lowered. */
101 struct cgraph_node *cgraph_nodes_queue;
103 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
104 secondary queue used during optimization to accommodate passes that
105 may generate new functions that need to be optimized and expanded. */
106 struct cgraph_node *cgraph_new_nodes;
108 /* Number of nodes in existence. */
109 int cgraph_n_nodes;
111 /* Maximal uid used in cgraph nodes. */
112 int cgraph_max_uid;
114 /* Maximal uid used in cgraph edges. */
115 int cgraph_edge_max_uid;
117 /* Maximal pid used for profiling */
118 int cgraph_max_pid;
120 /* Set when whole unit has been analyzed so we can access global info. */
121 bool cgraph_global_info_ready = false;
123 /* What state callgraph is in right now. */
124 enum cgraph_state cgraph_state = CGRAPH_STATE_CONSTRUCTION;
126 /* Set when the cgraph is fully build and the basic flags are computed. */
127 bool cgraph_function_flags_ready = false;
129 /* Linked list of cgraph asm nodes. */
130 struct cgraph_asm_node *cgraph_asm_nodes;
132 /* Last node in cgraph_asm_nodes. */
133 static GTY(()) struct cgraph_asm_node *cgraph_asm_last_node;
135 /* The order index of the next cgraph node to be created. This is
136 used so that we can sort the cgraph nodes in order by when we saw
137 them, to support -fno-toplevel-reorder. */
138 int cgraph_order;
140 /* List of hooks trigerred on cgraph_edge events. */
141 struct cgraph_edge_hook_list {
142 cgraph_edge_hook hook;
143 void *data;
144 struct cgraph_edge_hook_list *next;
147 /* List of hooks trigerred on cgraph_node events. */
148 struct cgraph_node_hook_list {
149 cgraph_node_hook hook;
150 void *data;
151 struct cgraph_node_hook_list *next;
154 /* List of hooks trigerred on events involving two cgraph_edges. */
155 struct cgraph_2edge_hook_list {
156 cgraph_2edge_hook hook;
157 void *data;
158 struct cgraph_2edge_hook_list *next;
161 /* List of hooks trigerred on events involving two cgraph_nodes. */
162 struct cgraph_2node_hook_list {
163 cgraph_2node_hook hook;
164 void *data;
165 struct cgraph_2node_hook_list *next;
168 /* List of hooks triggered when an edge is removed. */
169 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
170 /* List of hooks triggered when a node is removed. */
171 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
172 /* List of hooks triggered when an edge is duplicated. */
173 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
174 /* List of hooks triggered when a node is duplicated. */
175 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
176 /* List of hooks triggered when an function is inserted. */
177 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
180 /* Register HOOK to be called with DATA on each removed edge. */
181 struct cgraph_edge_hook_list *
182 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
184 struct cgraph_edge_hook_list *entry;
185 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
187 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
188 entry->hook = hook;
189 entry->data = data;
190 entry->next = NULL;
191 while (*ptr)
192 ptr = &(*ptr)->next;
193 *ptr = entry;
194 return entry;
197 /* Remove ENTRY from the list of hooks called on removing edges. */
198 void
199 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
201 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
203 while (*ptr != entry)
204 ptr = &(*ptr)->next;
205 *ptr = entry->next;
208 /* Call all edge removal hooks. */
209 static void
210 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
212 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
213 while (entry)
215 entry->hook (e, entry->data);
216 entry = entry->next;
220 /* Register HOOK to be called with DATA on each removed node. */
221 struct cgraph_node_hook_list *
222 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
224 struct cgraph_node_hook_list *entry;
225 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
227 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
228 entry->hook = hook;
229 entry->data = data;
230 entry->next = NULL;
231 while (*ptr)
232 ptr = &(*ptr)->next;
233 *ptr = entry;
234 return entry;
237 /* Remove ENTRY from the list of hooks called on removing nodes. */
238 void
239 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
241 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
243 while (*ptr != entry)
244 ptr = &(*ptr)->next;
245 *ptr = entry->next;
248 /* Call all node removal hooks. */
249 static void
250 cgraph_call_node_removal_hooks (struct cgraph_node *node)
252 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
253 while (entry)
255 entry->hook (node, entry->data);
256 entry = entry->next;
260 /* Register HOOK to be called with DATA on each removed node. */
261 struct cgraph_node_hook_list *
262 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
264 struct cgraph_node_hook_list *entry;
265 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
267 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
268 entry->hook = hook;
269 entry->data = data;
270 entry->next = NULL;
271 while (*ptr)
272 ptr = &(*ptr)->next;
273 *ptr = entry;
274 return entry;
277 /* Remove ENTRY from the list of hooks called on removing nodes. */
278 void
279 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
281 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
283 while (*ptr != entry)
284 ptr = &(*ptr)->next;
285 *ptr = entry->next;
288 /* Call all node removal hooks. */
289 void
290 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
292 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
293 while (entry)
295 entry->hook (node, entry->data);
296 entry = entry->next;
300 /* Register HOOK to be called with DATA on each duplicated edge. */
301 struct cgraph_2edge_hook_list *
302 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
304 struct cgraph_2edge_hook_list *entry;
305 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
307 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
308 entry->hook = hook;
309 entry->data = data;
310 entry->next = NULL;
311 while (*ptr)
312 ptr = &(*ptr)->next;
313 *ptr = entry;
314 return entry;
317 /* Remove ENTRY from the list of hooks called on duplicating edges. */
318 void
319 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
321 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
323 while (*ptr != entry)
324 ptr = &(*ptr)->next;
325 *ptr = entry->next;
328 /* Call all edge duplication hooks. */
329 static void
330 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
331 struct cgraph_edge *cs2)
333 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
334 while (entry)
336 entry->hook (cs1, cs2, entry->data);
337 entry = entry->next;
341 /* Register HOOK to be called with DATA on each duplicated node. */
342 struct cgraph_2node_hook_list *
343 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
345 struct cgraph_2node_hook_list *entry;
346 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
348 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
349 entry->hook = hook;
350 entry->data = data;
351 entry->next = NULL;
352 while (*ptr)
353 ptr = &(*ptr)->next;
354 *ptr = entry;
355 return entry;
358 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
359 void
360 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
362 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
364 while (*ptr != entry)
365 ptr = &(*ptr)->next;
366 *ptr = entry->next;
369 /* Call all node duplication hooks. */
370 static void
371 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
372 struct cgraph_node *node2)
374 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
375 while (entry)
377 entry->hook (node1, node2, entry->data);
378 entry = entry->next;
382 /* Returns a hash code for P. */
384 static hashval_t
385 hash_node (const void *p)
387 const struct cgraph_node *n = (const struct cgraph_node *) p;
388 return (hashval_t) DECL_UID (n->decl);
391 /* Returns nonzero if P1 and P2 are equal. */
393 static int
394 eq_node (const void *p1, const void *p2)
396 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
397 const struct cgraph_node *n2 = (const struct cgraph_node *) p2;
398 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
401 /* Allocate new callgraph node and insert it into basic data structures. */
403 static struct cgraph_node *
404 cgraph_create_node (void)
406 struct cgraph_node *node;
408 node = GGC_CNEW (struct cgraph_node);
409 node->next = cgraph_nodes;
410 node->uid = cgraph_max_uid++;
411 node->pid = -1;
412 node->order = cgraph_order++;
413 if (cgraph_nodes)
414 cgraph_nodes->previous = node;
415 node->previous = NULL;
416 node->global.estimated_growth = INT_MIN;
417 cgraph_nodes = node;
418 cgraph_n_nodes++;
419 return node;
422 /* Return cgraph node assigned to DECL. Create new one when needed. */
424 struct cgraph_node *
425 cgraph_node (tree decl)
427 struct cgraph_node key, *node, **slot;
429 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
431 if (!cgraph_hash)
432 cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
434 key.decl = decl;
436 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
438 if (*slot)
440 node = *slot;
441 if (!node->master_clone)
442 node->master_clone = node;
443 return node;
446 node = cgraph_create_node ();
447 node->decl = decl;
448 *slot = node;
449 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
451 node->origin = cgraph_node (DECL_CONTEXT (decl));
452 node->next_nested = node->origin->nested;
453 node->origin->nested = node;
454 node->master_clone = node;
456 if (assembler_name_hash)
458 void **aslot;
459 tree name = DECL_ASSEMBLER_NAME (decl);
461 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
462 decl_assembler_name_hash (name),
463 INSERT);
464 /* We can have multiple declarations with same assembler name. For C++
465 it is __builtin_strlen and strlen, for instance. Do we need to
466 record them all? Original implementation marked just first one
467 so lets hope for the best. */
468 if (*aslot == NULL)
469 *aslot = node;
471 return node;
474 /* Insert already constructed node into hashtable. */
476 void
477 cgraph_insert_node_to_hashtable (struct cgraph_node *node)
479 struct cgraph_node **slot;
481 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, node, INSERT);
483 gcc_assert (!*slot);
484 *slot = node;
487 /* Returns a hash code for P. */
489 static hashval_t
490 hash_node_by_assembler_name (const void *p)
492 const struct cgraph_node *n = (const struct cgraph_node *) p;
493 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
496 /* Returns nonzero if P1 and P2 are equal. */
498 static int
499 eq_assembler_name (const void *p1, const void *p2)
501 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
502 const_tree name = (const_tree)p2;
503 return (decl_assembler_name_equal (n1->decl, name));
506 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
507 Return NULL if there's no such node. */
509 struct cgraph_node *
510 cgraph_node_for_asm (tree asmname)
512 struct cgraph_node *node;
513 void **slot;
515 if (!assembler_name_hash)
517 assembler_name_hash =
518 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
519 NULL);
520 for (node = cgraph_nodes; node; node = node->next)
521 if (!node->global.inlined_to)
523 tree name = DECL_ASSEMBLER_NAME (node->decl);
524 slot = htab_find_slot_with_hash (assembler_name_hash, name,
525 decl_assembler_name_hash (name),
526 INSERT);
527 /* We can have multiple declarations with same assembler name. For C++
528 it is __builtin_strlen and strlen, for instance. Do we need to
529 record them all? Original implementation marked just first one
530 so lets hope for the best. */
531 if (*slot)
532 continue;
533 *slot = node;
537 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
538 decl_assembler_name_hash (asmname),
539 NO_INSERT);
541 if (slot)
542 return (struct cgraph_node *) *slot;
543 return NULL;
546 /* Returns a hash value for X (which really is a die_struct). */
548 static hashval_t
549 edge_hash (const void *x)
551 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
554 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
556 static int
557 edge_eq (const void *x, const void *y)
559 return ((const struct cgraph_edge *) x)->call_stmt == y;
563 /* Return the callgraph edge representing the GIMPLE_CALL statement
564 CALL_STMT. */
566 struct cgraph_edge *
567 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
569 struct cgraph_edge *e, *e2;
570 int n = 0;
572 if (node->call_site_hash)
573 return (struct cgraph_edge *)
574 htab_find_with_hash (node->call_site_hash, call_stmt,
575 htab_hash_pointer (call_stmt));
577 /* This loop may turn out to be performance problem. In such case adding
578 hashtables into call nodes with very many edges is probably best
579 solution. It is not good idea to add pointer into CALL_EXPR itself
580 because we want to make possible having multiple cgraph nodes representing
581 different clones of the same body before the body is actually cloned. */
582 for (e = node->callees; e; e= e->next_callee)
584 if (e->call_stmt == call_stmt)
585 break;
586 n++;
589 if (n > 100)
591 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
592 for (e2 = node->callees; e2; e2 = e2->next_callee)
594 void **slot;
595 slot = htab_find_slot_with_hash (node->call_site_hash,
596 e2->call_stmt,
597 htab_hash_pointer (e2->call_stmt),
598 INSERT);
599 gcc_assert (!*slot);
600 *slot = e2;
604 return e;
608 /* Change field call_smt of edge E to NEW_STMT. */
610 void
611 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
613 if (e->caller->call_site_hash)
615 htab_remove_elt_with_hash (e->caller->call_site_hash,
616 e->call_stmt,
617 htab_hash_pointer (e->call_stmt));
619 e->call_stmt = new_stmt;
620 if (e->caller->call_site_hash)
622 void **slot;
623 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
624 e->call_stmt,
625 htab_hash_pointer
626 (e->call_stmt), INSERT);
627 gcc_assert (!*slot);
628 *slot = e;
632 /* Create edge from CALLER to CALLEE in the cgraph. */
634 struct cgraph_edge *
635 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
636 gimple call_stmt, gcov_type count, int freq, int nest)
638 struct cgraph_edge *edge = GGC_NEW (struct cgraph_edge);
639 #ifdef ENABLE_CHECKING
640 struct cgraph_edge *e;
642 for (e = caller->callees; e; e = e->next_callee)
643 gcc_assert (e->call_stmt != call_stmt);
644 #endif
646 gcc_assert (is_gimple_call (call_stmt));
648 if (!callee->analyzed)
649 edge->inline_failed = N_("function body not available");
650 else if (callee->local.redefined_extern_inline)
651 edge->inline_failed = N_("redefined extern inline functions are not "
652 "considered for inlining");
653 else if (callee->local.inlinable)
654 edge->inline_failed = N_("function not considered for inlining");
655 else
656 edge->inline_failed = N_("function not inlinable");
658 edge->aux = NULL;
660 edge->caller = caller;
661 edge->callee = callee;
662 edge->call_stmt = call_stmt;
663 edge->prev_caller = NULL;
664 edge->next_caller = callee->callers;
665 if (callee->callers)
666 callee->callers->prev_caller = edge;
667 edge->prev_callee = NULL;
668 edge->next_callee = caller->callees;
669 if (caller->callees)
670 caller->callees->prev_callee = edge;
671 caller->callees = edge;
672 callee->callers = edge;
673 edge->count = count;
674 gcc_assert (count >= 0);
675 edge->frequency = freq;
676 gcc_assert (freq >= 0);
677 gcc_assert (freq <= CGRAPH_FREQ_MAX);
678 edge->loop_nest = nest;
679 edge->indirect_call = 0;
680 edge->uid = cgraph_edge_max_uid++;
681 if (caller->call_site_hash)
683 void **slot;
684 slot = htab_find_slot_with_hash (caller->call_site_hash,
685 edge->call_stmt,
686 htab_hash_pointer
687 (edge->call_stmt),
688 INSERT);
689 gcc_assert (!*slot);
690 *slot = edge;
692 return edge;
695 /* Remove the edge E from the list of the callers of the callee. */
697 static inline void
698 cgraph_edge_remove_callee (struct cgraph_edge *e)
700 if (e->prev_caller)
701 e->prev_caller->next_caller = e->next_caller;
702 if (e->next_caller)
703 e->next_caller->prev_caller = e->prev_caller;
704 if (!e->prev_caller)
705 e->callee->callers = e->next_caller;
708 /* Remove the edge E from the list of the callees of the caller. */
710 static inline void
711 cgraph_edge_remove_caller (struct cgraph_edge *e)
713 if (e->prev_callee)
714 e->prev_callee->next_callee = e->next_callee;
715 if (e->next_callee)
716 e->next_callee->prev_callee = e->prev_callee;
717 if (!e->prev_callee)
718 e->caller->callees = e->next_callee;
719 if (e->caller->call_site_hash)
720 htab_remove_elt_with_hash (e->caller->call_site_hash,
721 e->call_stmt,
722 htab_hash_pointer (e->call_stmt));
725 /* Remove the edge E in the cgraph. */
727 void
728 cgraph_remove_edge (struct cgraph_edge *e)
730 cgraph_call_edge_removal_hooks (e);
731 /* Remove from callers list of the callee. */
732 cgraph_edge_remove_callee (e);
734 /* Remove from callees list of the callers. */
735 cgraph_edge_remove_caller (e);
738 /* Redirect callee of E to N. The function does not update underlying
739 call expression. */
741 void
742 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
744 /* Remove from callers list of the current callee. */
745 cgraph_edge_remove_callee (e);
747 /* Insert to callers list of the new callee. */
748 e->prev_caller = NULL;
749 if (n->callers)
750 n->callers->prev_caller = e;
751 e->next_caller = n->callers;
752 n->callers = e;
753 e->callee = n;
757 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
758 OLD_STMT changed into NEW_STMT. */
760 void
761 cgraph_update_edges_for_call_stmt (gimple old_stmt, gimple new_stmt)
763 tree new_call = (is_gimple_call (new_stmt)) ? gimple_call_fn (new_stmt) : 0;
764 tree old_call = (is_gimple_call (old_stmt)) ? gimple_call_fn (old_stmt) : 0;
765 struct cgraph_node *node = cgraph_node (cfun->decl);
767 if (old_call != new_call)
769 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
770 struct cgraph_edge *ne = NULL;
771 tree new_decl;
773 if (e)
775 gcov_type count = e->count;
776 int frequency = e->frequency;
777 int loop_nest = e->loop_nest;
779 cgraph_remove_edge (e);
780 if (new_call)
782 new_decl = gimple_call_fndecl (new_stmt);
783 if (new_decl)
785 ne = cgraph_create_edge (node, cgraph_node (new_decl),
786 new_stmt, count, frequency,
787 loop_nest);
788 gcc_assert (ne->inline_failed);
793 else if (old_stmt != new_stmt)
795 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
797 if (e)
798 cgraph_set_call_stmt (e, new_stmt);
803 /* Remove all callees from the node. */
805 void
806 cgraph_node_remove_callees (struct cgraph_node *node)
808 struct cgraph_edge *e;
810 /* It is sufficient to remove the edges from the lists of callers of
811 the callees. The callee list of the node can be zapped with one
812 assignment. */
813 for (e = node->callees; e; e = e->next_callee)
815 cgraph_call_edge_removal_hooks (e);
816 cgraph_edge_remove_callee (e);
818 node->callees = NULL;
819 if (node->call_site_hash)
821 htab_delete (node->call_site_hash);
822 node->call_site_hash = NULL;
826 /* Remove all callers from the node. */
828 static void
829 cgraph_node_remove_callers (struct cgraph_node *node)
831 struct cgraph_edge *e;
833 /* It is sufficient to remove the edges from the lists of callees of
834 the callers. The caller list of the node can be zapped with one
835 assignment. */
836 for (e = node->callers; e; e = e->next_caller)
838 cgraph_call_edge_removal_hooks (e);
839 cgraph_edge_remove_caller (e);
841 node->callers = NULL;
844 /* Release memory used to represent body of function NODE. */
846 void
847 cgraph_release_function_body (struct cgraph_node *node)
849 if (DECL_STRUCT_FUNCTION (node->decl)
850 && DECL_STRUCT_FUNCTION (node->decl)->gimple_df)
852 tree old_decl = current_function_decl;
853 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
854 current_function_decl = node->decl;
855 delete_tree_ssa ();
856 delete_tree_cfg_annotations ();
857 cfun->eh = NULL;
858 gimple_set_body (node->decl, NULL);
859 current_function_decl = old_decl;
860 pop_cfun();
862 DECL_SAVED_TREE (node->decl) = NULL;
863 DECL_STRUCT_FUNCTION (node->decl) = NULL;
864 DECL_INITIAL (node->decl) = error_mark_node;
867 /* Remove the node from cgraph. */
869 void
870 cgraph_remove_node (struct cgraph_node *node)
872 void **slot;
873 bool kill_body = false;
874 struct cgraph_node *n;
876 cgraph_call_node_removal_hooks (node);
877 cgraph_node_remove_callers (node);
878 cgraph_node_remove_callees (node);
880 /* Incremental inlining access removed nodes stored in the postorder list.
882 node->needed = node->reachable = false;
883 for (n = node->nested; n; n = n->next_nested)
884 n->origin = NULL;
885 node->nested = NULL;
886 if (node->origin)
888 struct cgraph_node **node2 = &node->origin->nested;
890 while (*node2 != node)
891 node2 = &(*node2)->next_nested;
892 *node2 = node->next_nested;
894 if (node->previous)
895 node->previous->next = node->next;
896 else
897 cgraph_nodes = node->next;
898 if (node->next)
899 node->next->previous = node->previous;
900 node->next = NULL;
901 node->previous = NULL;
902 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
903 if (*slot == node)
905 if (node->next_clone)
907 struct cgraph_node *new_node = node->next_clone;
908 struct cgraph_node *n;
910 /* Make the next clone be the master clone */
911 for (n = new_node; n; n = n->next_clone)
912 n->master_clone = new_node;
914 *slot = new_node;
915 node->next_clone->prev_clone = NULL;
917 else
919 htab_clear_slot (cgraph_hash, slot);
920 kill_body = true;
923 else
925 node->prev_clone->next_clone = node->next_clone;
926 if (node->next_clone)
927 node->next_clone->prev_clone = node->prev_clone;
930 /* While all the clones are removed after being proceeded, the function
931 itself is kept in the cgraph even after it is compiled. Check whether
932 we are done with this body and reclaim it proactively if this is the case.
934 if (!kill_body && *slot)
936 struct cgraph_node *n = (struct cgraph_node *) *slot;
937 if (!n->next_clone && !n->global.inlined_to
938 && (cgraph_global_info_ready
939 && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl))))
940 kill_body = true;
942 if (assembler_name_hash)
944 tree name = DECL_ASSEMBLER_NAME (node->decl);
945 slot = htab_find_slot_with_hash (assembler_name_hash, name,
946 decl_assembler_name_hash (name),
947 NO_INSERT);
948 /* Inline clones are not hashed. */
949 if (slot && *slot == node)
950 htab_clear_slot (assembler_name_hash, slot);
953 if (kill_body)
954 cgraph_release_function_body (node);
955 node->decl = NULL;
956 if (node->call_site_hash)
958 htab_delete (node->call_site_hash);
959 node->call_site_hash = NULL;
961 cgraph_n_nodes--;
962 /* Do not free the structure itself so the walk over chain can continue. */
965 /* Notify finalize_compilation_unit that given node is reachable. */
967 void
968 cgraph_mark_reachable_node (struct cgraph_node *node)
970 if (!node->reachable && node->local.finalized)
972 notice_global_symbol (node->decl);
973 node->reachable = 1;
974 gcc_assert (!cgraph_global_info_ready);
976 node->next_needed = cgraph_nodes_queue;
977 cgraph_nodes_queue = node;
981 /* Likewise indicate that a node is needed, i.e. reachable via some
982 external means. */
984 void
985 cgraph_mark_needed_node (struct cgraph_node *node)
987 node->needed = 1;
988 cgraph_mark_reachable_node (node);
991 /* Return local info for the compiled function. */
993 struct cgraph_local_info *
994 cgraph_local_info (tree decl)
996 struct cgraph_node *node;
998 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
999 node = cgraph_node (decl);
1000 return &node->local;
1003 /* Return local info for the compiled function. */
1005 struct cgraph_global_info *
1006 cgraph_global_info (tree decl)
1008 struct cgraph_node *node;
1010 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1011 node = cgraph_node (decl);
1012 return &node->global;
1015 /* Return local info for the compiled function. */
1017 struct cgraph_rtl_info *
1018 cgraph_rtl_info (tree decl)
1020 struct cgraph_node *node;
1022 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1023 node = cgraph_node (decl);
1024 if (decl != current_function_decl
1025 && !TREE_ASM_WRITTEN (node->decl))
1026 return NULL;
1027 return &node->rtl;
1030 /* Return name of the node used in debug output. */
1031 const char *
1032 cgraph_node_name (struct cgraph_node *node)
1034 return lang_hooks.decl_printable_name (node->decl, 2);
1037 /* Names used to print out the availability enum. */
1038 const char * const cgraph_availability_names[] =
1039 {"unset", "not_available", "overwritable", "available", "local"};
1042 /* Dump call graph node NODE to file F. */
1044 void
1045 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1047 struct cgraph_edge *edge;
1048 fprintf (f, "%s/%i(%i):", cgraph_node_name (node), node->uid, node->pid);
1049 if (node->global.inlined_to)
1050 fprintf (f, " (inline copy in %s/%i)",
1051 cgraph_node_name (node->global.inlined_to),
1052 node->global.inlined_to->uid);
1053 if (cgraph_function_flags_ready)
1054 fprintf (f, " availability:%s",
1055 cgraph_availability_names [cgraph_function_body_availability (node)]);
1056 if (node->master_clone && node->master_clone->uid != node->uid)
1057 fprintf (f, "(%i)", node->master_clone->uid);
1058 if (node->count)
1059 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1060 (HOST_WIDEST_INT)node->count);
1061 if (node->local.inline_summary.self_insns)
1062 fprintf (f, " %i insns", node->local.inline_summary.self_insns);
1063 if (node->global.insns && node->global.insns
1064 != node->local.inline_summary.self_insns)
1065 fprintf (f, " (%i after inlining)", node->global.insns);
1066 if (node->local.inline_summary.estimated_self_stack_size)
1067 fprintf (f, " %i bytes stack usage", (int)node->local.inline_summary.estimated_self_stack_size);
1068 if (node->global.estimated_stack_size != node->local.inline_summary.estimated_self_stack_size)
1069 fprintf (f, " %i bytes after inlining", (int)node->global.estimated_stack_size);
1070 if (node->origin)
1071 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
1072 if (node->needed)
1073 fprintf (f, " needed");
1074 else if (node->reachable)
1075 fprintf (f, " reachable");
1076 if (gimple_has_body_p (node->decl))
1077 fprintf (f, " body");
1078 if (node->output)
1079 fprintf (f, " output");
1080 if (node->local.local)
1081 fprintf (f, " local");
1082 if (node->local.externally_visible)
1083 fprintf (f, " externally_visible");
1084 if (node->local.finalized)
1085 fprintf (f, " finalized");
1086 if (node->local.disregard_inline_limits)
1087 fprintf (f, " always_inline");
1088 else if (node->local.inlinable)
1089 fprintf (f, " inlinable");
1090 if (node->local.redefined_extern_inline)
1091 fprintf (f, " redefined_extern_inline");
1092 if (TREE_ASM_WRITTEN (node->decl))
1093 fprintf (f, " asm_written");
1095 fprintf (f, "\n called by: ");
1096 for (edge = node->callers; edge; edge = edge->next_caller)
1098 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
1099 edge->caller->uid);
1100 if (edge->count)
1101 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1102 (HOST_WIDEST_INT)edge->count);
1103 if (edge->frequency)
1104 fprintf (f, "(%.2f per call) ",
1105 edge->frequency / (double)CGRAPH_FREQ_BASE);
1106 if (!edge->inline_failed)
1107 fprintf(f, "(inlined) ");
1108 if (edge->indirect_call)
1109 fprintf(f, "(indirect) ");
1112 fprintf (f, "\n calls: ");
1113 for (edge = node->callees; edge; edge = edge->next_callee)
1115 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
1116 edge->callee->uid);
1117 if (!edge->inline_failed)
1118 fprintf(f, "(inlined) ");
1119 if (edge->indirect_call)
1120 fprintf(f, "(indirect) ");
1121 if (edge->count)
1122 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1123 (HOST_WIDEST_INT)edge->count);
1124 if (edge->frequency)
1125 fprintf (f, "(%.2f per call) ",
1126 edge->frequency / (double)CGRAPH_FREQ_BASE);
1127 if (edge->loop_nest)
1128 fprintf (f, "(nested in %i loops) ", edge->loop_nest);
1130 fprintf (f, "\n");
1134 /* Dump call graph node NODE to stderr. */
1136 void
1137 debug_cgraph_node (struct cgraph_node *node)
1139 dump_cgraph_node (stderr, node);
1143 /* Dump the callgraph to file F. */
1145 void
1146 dump_cgraph (FILE *f)
1148 struct cgraph_node *node;
1150 fprintf (f, "callgraph:\n\n");
1151 for (node = cgraph_nodes; node; node = node->next)
1152 dump_cgraph_node (f, node);
1156 /* Dump the call graph to stderr. */
1158 void
1159 debug_cgraph (void)
1161 dump_cgraph (stderr);
1165 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
1167 void
1168 change_decl_assembler_name (tree decl, tree name)
1170 gcc_assert (!assembler_name_hash);
1171 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
1173 SET_DECL_ASSEMBLER_NAME (decl, name);
1174 return;
1176 if (name == DECL_ASSEMBLER_NAME (decl))
1177 return;
1179 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
1180 && DECL_RTL_SET_P (decl))
1181 warning (0, "%D renamed after being referenced in assembly", decl);
1183 SET_DECL_ASSEMBLER_NAME (decl, name);
1186 /* Add a top-level asm statement to the list. */
1188 struct cgraph_asm_node *
1189 cgraph_add_asm_node (tree asm_str)
1191 struct cgraph_asm_node *node;
1193 node = GGC_CNEW (struct cgraph_asm_node);
1194 node->asm_str = asm_str;
1195 node->order = cgraph_order++;
1196 node->next = NULL;
1197 if (cgraph_asm_nodes == NULL)
1198 cgraph_asm_nodes = node;
1199 else
1200 cgraph_asm_last_node->next = node;
1201 cgraph_asm_last_node = node;
1202 return node;
1205 /* Return true when the DECL can possibly be inlined. */
1206 bool
1207 cgraph_function_possibly_inlined_p (tree decl)
1209 if (!cgraph_global_info_ready)
1210 return !DECL_UNINLINABLE (decl);
1211 return DECL_POSSIBLY_INLINED (decl);
1214 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
1215 struct cgraph_edge *
1216 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
1217 gimple call_stmt, gcov_type count_scale, int freq_scale,
1218 int loop_nest, bool update_original)
1220 struct cgraph_edge *new_edge;
1221 gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
1222 gcov_type freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
1224 if (freq > CGRAPH_FREQ_MAX)
1225 freq = CGRAPH_FREQ_MAX;
1226 new_edge = cgraph_create_edge (n, e->callee, call_stmt, count, freq,
1227 e->loop_nest + loop_nest);
1229 new_edge->inline_failed = e->inline_failed;
1230 new_edge->indirect_call = e->indirect_call;
1231 if (update_original)
1233 e->count -= new_edge->count;
1234 if (e->count < 0)
1235 e->count = 0;
1237 cgraph_call_edge_duplication_hooks (e, new_edge);
1238 return new_edge;
1241 /* Create node representing clone of N executed COUNT times. Decrease
1242 the execution counts from original node too.
1244 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
1245 function's profile to reflect the fact that part of execution is handled
1246 by node. */
1247 struct cgraph_node *
1248 cgraph_clone_node (struct cgraph_node *n, gcov_type count, int freq,
1249 int loop_nest, bool update_original)
1251 struct cgraph_node *new_node = cgraph_create_node ();
1252 struct cgraph_edge *e;
1253 gcov_type count_scale;
1255 new_node->decl = n->decl;
1256 new_node->origin = n->origin;
1257 if (new_node->origin)
1259 new_node->next_nested = new_node->origin->nested;
1260 new_node->origin->nested = new_node;
1262 new_node->analyzed = n->analyzed;
1263 new_node->local = n->local;
1264 new_node->global = n->global;
1265 new_node->rtl = n->rtl;
1266 new_node->master_clone = n->master_clone;
1267 new_node->count = count;
1268 if (n->count)
1270 if (new_node->count > n->count)
1271 count_scale = REG_BR_PROB_BASE;
1272 else
1273 count_scale = new_node->count * REG_BR_PROB_BASE / n->count;
1275 else
1276 count_scale = 0;
1277 if (update_original)
1279 n->count -= count;
1280 if (n->count < 0)
1281 n->count = 0;
1284 for (e = n->callees;e; e=e->next_callee)
1285 cgraph_clone_edge (e, new_node, e->call_stmt, count_scale, freq, loop_nest,
1286 update_original);
1288 new_node->next_clone = n->next_clone;
1289 new_node->prev_clone = n;
1290 n->next_clone = new_node;
1291 if (new_node->next_clone)
1292 new_node->next_clone->prev_clone = new_node;
1294 cgraph_call_node_duplication_hooks (n, new_node);
1295 return new_node;
1298 /* Return true if N is an master_clone, (see cgraph_master_clone). */
1300 bool
1301 cgraph_is_master_clone (struct cgraph_node *n)
1303 return (n == cgraph_master_clone (n));
1306 struct cgraph_node *
1307 cgraph_master_clone (struct cgraph_node *n)
1309 enum availability avail = cgraph_function_body_availability (n);
1311 if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
1312 return NULL;
1314 if (!n->master_clone)
1315 n->master_clone = cgraph_node (n->decl);
1317 return n->master_clone;
1320 /* NODE is no longer nested function; update cgraph accordingly. */
1321 void
1322 cgraph_unnest_node (struct cgraph_node *node)
1324 struct cgraph_node **node2 = &node->origin->nested;
1325 gcc_assert (node->origin);
1327 while (*node2 != node)
1328 node2 = &(*node2)->next_nested;
1329 *node2 = node->next_nested;
1330 node->origin = NULL;
1333 /* Return function availability. See cgraph.h for description of individual
1334 return values. */
1335 enum availability
1336 cgraph_function_body_availability (struct cgraph_node *node)
1338 enum availability avail;
1339 gcc_assert (cgraph_function_flags_ready);
1340 if (!node->analyzed)
1341 avail = AVAIL_NOT_AVAILABLE;
1342 else if (node->local.local)
1343 avail = AVAIL_LOCAL;
1344 else if (node->local.externally_visible)
1345 avail = AVAIL_AVAILABLE;
1347 /* If the function can be overwritten, return OVERWRITABLE. Take
1348 care at least of two notable extensions - the COMDAT functions
1349 used to share template instantiations in C++ (this is symmetric
1350 to code cp_cannot_inline_tree_fn and probably shall be shared and
1351 the inlinability hooks completely eliminated).
1353 ??? Does the C++ one definition rule allow us to always return
1354 AVAIL_AVAILABLE here? That would be good reason to preserve this
1355 hook Similarly deal with extern inline functions - this is again
1356 necessary to get C++ shared functions having keyed templates
1357 right and in the C extension documentation we probably should
1358 document the requirement of both versions of function (extern
1359 inline and offline) having same side effect characteristics as
1360 good optimization is what this optimization is about. */
1362 else if (!(*targetm.binds_local_p) (node->decl)
1363 && !DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl))
1364 avail = AVAIL_OVERWRITABLE;
1365 else avail = AVAIL_AVAILABLE;
1367 return avail;
1370 /* Add the function FNDECL to the call graph.
1371 Unlike cgraph_finalize_function, this function is intended to be used
1372 by middle end and allows insertion of new function at arbitrary point
1373 of compilation. The function can be either in high, low or SSA form
1374 GIMPLE.
1376 The function is assumed to be reachable and have address taken (so no
1377 API breaking optimizations are performed on it).
1379 Main work done by this function is to enqueue the function for later
1380 processing to avoid need the passes to be re-entrant. */
1382 void
1383 cgraph_add_new_function (tree fndecl, bool lowered)
1385 struct cgraph_node *node;
1386 switch (cgraph_state)
1388 case CGRAPH_STATE_CONSTRUCTION:
1389 /* Just enqueue function to be processed at nearest occurrence. */
1390 node = cgraph_node (fndecl);
1391 node->next_needed = cgraph_new_nodes;
1392 if (lowered)
1393 node->lowered = true;
1394 cgraph_new_nodes = node;
1395 break;
1397 case CGRAPH_STATE_IPA:
1398 case CGRAPH_STATE_IPA_SSA:
1399 case CGRAPH_STATE_EXPANSION:
1400 /* Bring the function into finalized state and enqueue for later
1401 analyzing and compilation. */
1402 node = cgraph_node (fndecl);
1403 node->local.local = false;
1404 node->local.finalized = true;
1405 node->reachable = node->needed = true;
1406 if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
1408 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
1409 current_function_decl = fndecl;
1410 gimple_register_cfg_hooks ();
1411 tree_lowering_passes (fndecl);
1412 bitmap_obstack_initialize (NULL);
1413 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
1414 execute_pass_list (pass_early_local_passes.pass.sub);
1415 bitmap_obstack_release (NULL);
1416 pop_cfun ();
1417 current_function_decl = NULL;
1419 lowered = true;
1421 if (lowered)
1422 node->lowered = true;
1423 node->next_needed = cgraph_new_nodes;
1424 cgraph_new_nodes = node;
1425 break;
1427 case CGRAPH_STATE_FINISHED:
1428 /* At the very end of compilation we have to do all the work up
1429 to expansion. */
1430 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
1431 current_function_decl = fndecl;
1432 gimple_register_cfg_hooks ();
1433 if (!lowered)
1434 tree_lowering_passes (fndecl);
1435 bitmap_obstack_initialize (NULL);
1436 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
1437 execute_pass_list (pass_early_local_passes.pass.sub);
1438 bitmap_obstack_release (NULL);
1439 tree_rest_of_compilation (fndecl);
1440 pop_cfun ();
1441 current_function_decl = NULL;
1442 break;
1446 #include "gt-cgraph.h"