* c-parser.c (c_parser_lex_all): Don't enforce location constraint
[official-gcc.git] / gcc / cgraph.c
bloba592eed660d9aa69e0c563f54f2ec8efb292817f
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 "tree-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;
95 /* The linked list of cgraph nodes. */
96 struct cgraph_node *cgraph_nodes;
98 /* Queue of cgraph nodes scheduled to be lowered. */
99 struct cgraph_node *cgraph_nodes_queue;
101 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
102 secondary queue used during optimization to accommodate passes that
103 may generate new functions that need to be optimized and expanded. */
104 struct cgraph_node *cgraph_new_nodes;
106 /* Number of nodes in existence. */
107 int cgraph_n_nodes;
109 /* Maximal uid used in cgraph nodes. */
110 int cgraph_max_uid;
112 /* Maximal pid used for profiling */
113 int cgraph_max_pid;
115 /* Set when whole unit has been analyzed so we can access global info. */
116 bool cgraph_global_info_ready = false;
118 /* What state callgraph is in right now. */
119 enum cgraph_state cgraph_state = CGRAPH_STATE_CONSTRUCTION;
121 /* Set when the cgraph is fully build and the basic flags are computed. */
122 bool cgraph_function_flags_ready = false;
124 /* Linked list of cgraph asm nodes. */
125 struct cgraph_asm_node *cgraph_asm_nodes;
127 /* Last node in cgraph_asm_nodes. */
128 static GTY(()) struct cgraph_asm_node *cgraph_asm_last_node;
130 /* The order index of the next cgraph node to be created. This is
131 used so that we can sort the cgraph nodes in order by when we saw
132 them, to support -fno-toplevel-reorder. */
133 int cgraph_order;
136 /* Reset this module in preparation for the next compilation unit. */
138 void
139 cgraph_reset (void)
141 cgraph_hash = NULL;
142 cgraph_nodes = NULL;
143 cgraph_nodes_queue = NULL;
144 cgraph_new_nodes = NULL;
145 cgraph_n_nodes = 0;
146 cgraph_max_uid = 0;
147 cgraph_max_pid = 0;
148 cgraph_global_info_ready = false;
149 cgraph_state = CGRAPH_STATE_CONSTRUCTION;
150 cgraph_function_flags_ready = false;
151 cgraph_asm_nodes = NULL;
152 cgraph_asm_last_node = NULL;
153 cgraph_order = 0;
156 static hashval_t hash_node (const void *);
157 static int eq_node (const void *, const void *);
159 /* Returns a hash code for P. */
161 static hashval_t
162 hash_node (const void *p)
164 const struct cgraph_node *n = (const struct cgraph_node *) p;
165 return (hashval_t) DECL_UID (n->decl);
168 /* Returns nonzero if P1 and P2 are equal. */
170 static int
171 eq_node (const void *p1, const void *p2)
173 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
174 const struct cgraph_node *n2 = (const struct cgraph_node *) p2;
175 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
178 /* Allocate new callgraph node and insert it into basic data structures. */
180 static struct cgraph_node *
181 cgraph_create_node (void)
183 struct cgraph_node *node;
185 node = GGC_CNEW (struct cgraph_node);
186 node->next = cgraph_nodes;
187 node->uid = cgraph_max_uid++;
188 node->pid = -1;
189 node->order = cgraph_order++;
190 if (cgraph_nodes)
191 cgraph_nodes->previous = node;
192 node->previous = NULL;
193 node->global.estimated_growth = INT_MIN;
194 cgraph_nodes = node;
195 cgraph_n_nodes++;
196 return node;
199 /* Return cgraph node assigned to DECL. Create new one when needed. */
201 struct cgraph_node *
202 cgraph_node (tree decl)
204 struct cgraph_node key, *node, **slot;
206 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
208 if (!cgraph_hash)
209 cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
211 key.decl = decl;
213 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
215 if (*slot)
217 node = *slot;
218 if (!node->master_clone)
219 node->master_clone = node;
220 return node;
223 node = cgraph_create_node ();
224 node->decl = decl;
225 *slot = node;
226 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
228 node->origin = cgraph_node (DECL_CONTEXT (decl));
229 node->next_nested = node->origin->nested;
230 node->origin->nested = node;
231 node->master_clone = node;
233 return node;
236 /* Insert already constructed node into hashtable. */
238 void
239 cgraph_insert_node_to_hashtable (struct cgraph_node *node)
241 struct cgraph_node **slot;
243 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, node, INSERT);
245 gcc_assert (!*slot);
246 *slot = node;
250 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
251 Return NULL if there's no such node. */
253 struct cgraph_node *
254 cgraph_node_for_asm (tree asmname)
256 struct cgraph_node *node;
258 for (node = cgraph_nodes; node ; node = node->next)
259 if (decl_assembler_name_equal (node->decl, asmname))
260 return node;
262 return NULL;
265 /* Returns a hash value for X (which really is a die_struct). */
267 static hashval_t
268 edge_hash (const void *x)
270 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
273 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
275 static int
276 edge_eq (const void *x, const void *y)
278 return ((const struct cgraph_edge *) x)->call_stmt == y;
281 /* Return callgraph edge representing CALL_EXPR statement. */
282 struct cgraph_edge *
283 cgraph_edge (struct cgraph_node *node, tree call_stmt)
285 struct cgraph_edge *e, *e2;
286 int n = 0;
288 if (node->call_site_hash)
289 return htab_find_with_hash (node->call_site_hash, call_stmt,
290 htab_hash_pointer (call_stmt));
292 /* This loop may turn out to be performance problem. In such case adding
293 hashtables into call nodes with very many edges is probably best
294 solution. It is not good idea to add pointer into CALL_EXPR itself
295 because we want to make possible having multiple cgraph nodes representing
296 different clones of the same body before the body is actually cloned. */
297 for (e = node->callees; e; e= e->next_callee)
299 if (e->call_stmt == call_stmt)
300 break;
301 n++;
303 if (n > 100)
305 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
306 for (e2 = node->callees; e2; e2 = e2->next_callee)
308 void **slot;
309 slot = htab_find_slot_with_hash (node->call_site_hash,
310 e2->call_stmt,
311 htab_hash_pointer (e2->call_stmt),
312 INSERT);
313 gcc_assert (!*slot);
314 *slot = e2;
317 return e;
320 /* Change call_smtt of edge E to NEW_STMT. */
322 void
323 cgraph_set_call_stmt (struct cgraph_edge *e, tree new_stmt)
325 if (e->caller->call_site_hash)
327 htab_remove_elt_with_hash (e->caller->call_site_hash,
328 e->call_stmt,
329 htab_hash_pointer (e->call_stmt));
331 e->call_stmt = new_stmt;
332 if (e->caller->call_site_hash)
334 void **slot;
335 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
336 e->call_stmt,
337 htab_hash_pointer
338 (e->call_stmt), INSERT);
339 gcc_assert (!*slot);
340 *slot = e;
344 /* Create edge from CALLER to CALLEE in the cgraph. */
346 struct cgraph_edge *
347 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
348 tree call_stmt, gcov_type count, int freq, int nest)
350 struct cgraph_edge *edge = GGC_NEW (struct cgraph_edge);
351 #ifdef ENABLE_CHECKING
352 struct cgraph_edge *e;
354 for (e = caller->callees; e; e = e->next_callee)
355 gcc_assert (e->call_stmt != call_stmt);
356 #endif
358 gcc_assert (get_call_expr_in (call_stmt));
360 if (!DECL_SAVED_TREE (callee->decl))
361 edge->inline_failed = N_("function body not available");
362 else if (callee->local.redefined_extern_inline)
363 edge->inline_failed = N_("redefined extern inline functions are not "
364 "considered for inlining");
365 else if (callee->local.inlinable)
366 edge->inline_failed = N_("function not considered for inlining");
367 else
368 edge->inline_failed = N_("function not inlinable");
370 edge->aux = NULL;
372 edge->caller = caller;
373 edge->callee = callee;
374 edge->call_stmt = call_stmt;
375 edge->prev_caller = NULL;
376 edge->next_caller = callee->callers;
377 if (callee->callers)
378 callee->callers->prev_caller = edge;
379 edge->prev_callee = NULL;
380 edge->next_callee = caller->callees;
381 if (caller->callees)
382 caller->callees->prev_callee = edge;
383 caller->callees = edge;
384 callee->callers = edge;
385 edge->count = count;
386 gcc_assert (count >= 0);
387 edge->frequency = freq;
388 gcc_assert (freq >= 0);
389 gcc_assert (freq <= CGRAPH_FREQ_MAX);
390 edge->loop_nest = nest;
391 if (caller->call_site_hash)
393 void **slot;
394 slot = htab_find_slot_with_hash (caller->call_site_hash,
395 edge->call_stmt,
396 htab_hash_pointer
397 (edge->call_stmt),
398 INSERT);
399 gcc_assert (!*slot);
400 *slot = edge;
402 return edge;
405 /* Remove the edge E from the list of the callers of the callee. */
407 static inline void
408 cgraph_edge_remove_callee (struct cgraph_edge *e)
410 if (e->prev_caller)
411 e->prev_caller->next_caller = e->next_caller;
412 if (e->next_caller)
413 e->next_caller->prev_caller = e->prev_caller;
414 if (!e->prev_caller)
415 e->callee->callers = e->next_caller;
418 /* Remove the edge E from the list of the callees of the caller. */
420 static inline void
421 cgraph_edge_remove_caller (struct cgraph_edge *e)
423 if (e->prev_callee)
424 e->prev_callee->next_callee = e->next_callee;
425 if (e->next_callee)
426 e->next_callee->prev_callee = e->prev_callee;
427 if (!e->prev_callee)
428 e->caller->callees = e->next_callee;
429 if (e->caller->call_site_hash)
430 htab_remove_elt_with_hash (e->caller->call_site_hash,
431 e->call_stmt,
432 htab_hash_pointer (e->call_stmt));
435 /* Remove the edge E in the cgraph. */
437 void
438 cgraph_remove_edge (struct cgraph_edge *e)
440 /* Remove from callers list of the callee. */
441 cgraph_edge_remove_callee (e);
443 /* Remove from callees list of the callers. */
444 cgraph_edge_remove_caller (e);
447 /* Redirect callee of E to N. The function does not update underlying
448 call expression. */
450 void
451 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
453 /* Remove from callers list of the current callee. */
454 cgraph_edge_remove_callee (e);
456 /* Insert to callers list of the new callee. */
457 e->prev_caller = NULL;
458 if (n->callers)
459 n->callers->prev_caller = e;
460 e->next_caller = n->callers;
461 n->callers = e;
462 e->callee = n;
465 /* Update or remove corresponding cgraph edge if a call OLD_CALL
466 in OLD_STMT changed into NEW_STMT. */
468 void
469 cgraph_update_edges_for_call_stmt (tree old_stmt, tree old_call,
470 tree new_stmt)
472 tree new_call = get_call_expr_in (new_stmt);
473 struct cgraph_node *node = cgraph_node (cfun->decl);
475 if (old_call != new_call)
477 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
478 struct cgraph_edge *ne = NULL;
479 tree new_decl;
481 if (e)
483 gcov_type count = e->count;
484 int frequency = e->frequency;
485 int loop_nest = e->loop_nest;
487 cgraph_remove_edge (e);
488 if (new_call)
490 new_decl = get_callee_fndecl (new_call);
491 if (new_decl)
493 ne = cgraph_create_edge (node, cgraph_node (new_decl),
494 new_stmt, count, frequency,
495 loop_nest);
496 gcc_assert (ne->inline_failed);
501 else if (old_stmt != new_stmt)
503 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
505 if (e)
506 cgraph_set_call_stmt (e, new_stmt);
510 /* Remove all callees from the node. */
512 void
513 cgraph_node_remove_callees (struct cgraph_node *node)
515 struct cgraph_edge *e;
517 /* It is sufficient to remove the edges from the lists of callers of
518 the callees. The callee list of the node can be zapped with one
519 assignment. */
520 for (e = node->callees; e; e = e->next_callee)
521 cgraph_edge_remove_callee (e);
522 node->callees = NULL;
523 if (node->call_site_hash)
525 htab_delete (node->call_site_hash);
526 node->call_site_hash = NULL;
530 /* Remove all callers from the node. */
532 static void
533 cgraph_node_remove_callers (struct cgraph_node *node)
535 struct cgraph_edge *e;
537 /* It is sufficient to remove the edges from the lists of callees of
538 the callers. The caller list of the node can be zapped with one
539 assignment. */
540 for (e = node->callers; e; e = e->next_caller)
541 cgraph_edge_remove_caller (e);
542 node->callers = NULL;
545 /* Release memory used to represent body of function NODE. */
547 void
548 cgraph_release_function_body (struct cgraph_node *node)
550 if (DECL_STRUCT_FUNCTION (node->decl)
551 && DECL_STRUCT_FUNCTION (node->decl)->gimple_df)
553 tree old_decl = current_function_decl;
554 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
555 current_function_decl = node->decl;
556 delete_tree_ssa ();
557 delete_tree_cfg_annotations ();
558 cfun->eh = NULL;
559 current_function_decl = old_decl;
560 pop_cfun();
562 DECL_SAVED_TREE (node->decl) = NULL;
563 DECL_STRUCT_FUNCTION (node->decl) = NULL;
564 DECL_INITIAL (node->decl) = error_mark_node;
567 /* Remove the node from cgraph. */
569 void
570 cgraph_remove_node (struct cgraph_node *node)
572 void **slot;
573 bool kill_body = false;
575 cgraph_node_remove_callers (node);
576 cgraph_node_remove_callees (node);
577 /* Incremental inlining access removed nodes stored in the postorder list.
579 node->needed = node->reachable = false;
580 while (node->nested)
581 cgraph_remove_node (node->nested);
582 if (node->origin)
584 struct cgraph_node **node2 = &node->origin->nested;
586 while (*node2 != node)
587 node2 = &(*node2)->next_nested;
588 *node2 = node->next_nested;
590 if (node->previous)
591 node->previous->next = node->next;
592 else
593 cgraph_nodes = node->next;
594 if (node->next)
595 node->next->previous = node->previous;
596 node->next = NULL;
597 node->previous = NULL;
598 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
599 if (*slot == node)
601 if (node->next_clone)
603 struct cgraph_node *new_node = node->next_clone;
604 struct cgraph_node *n;
606 /* Make the next clone be the master clone */
607 for (n = new_node; n; n = n->next_clone)
608 n->master_clone = new_node;
610 *slot = new_node;
611 node->next_clone->prev_clone = NULL;
613 else
615 htab_clear_slot (cgraph_hash, slot);
616 kill_body = true;
619 else
621 node->prev_clone->next_clone = node->next_clone;
622 if (node->next_clone)
623 node->next_clone->prev_clone = node->prev_clone;
626 /* While all the clones are removed after being proceeded, the function
627 itself is kept in the cgraph even after it is compiled. Check whether
628 we are done with this body and reclaim it proactively if this is the case.
630 if (!kill_body && *slot)
632 struct cgraph_node *n = (struct cgraph_node *) *slot;
633 if (!n->next_clone && !n->global.inlined_to
634 && (cgraph_global_info_ready
635 && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl))))
636 kill_body = true;
639 if (kill_body && flag_unit_at_a_time)
640 cgraph_release_function_body (node);
641 node->decl = NULL;
642 if (node->call_site_hash)
644 htab_delete (node->call_site_hash);
645 node->call_site_hash = NULL;
647 cgraph_n_nodes--;
648 /* Do not free the structure itself so the walk over chain can continue. */
651 /* Notify finalize_compilation_unit that given node is reachable. */
653 void
654 cgraph_mark_reachable_node (struct cgraph_node *node)
656 if (!node->reachable && node->local.finalized)
658 notice_global_symbol (node->decl);
659 node->reachable = 1;
660 gcc_assert (!cgraph_global_info_ready);
662 node->next_needed = cgraph_nodes_queue;
663 cgraph_nodes_queue = node;
667 /* Likewise indicate that a node is needed, i.e. reachable via some
668 external means. */
670 void
671 cgraph_mark_needed_node (struct cgraph_node *node)
673 node->needed = 1;
674 cgraph_mark_reachable_node (node);
677 /* Return local info for the compiled function. */
679 struct cgraph_local_info *
680 cgraph_local_info (tree decl)
682 struct cgraph_node *node;
684 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
685 node = cgraph_node (decl);
686 return &node->local;
689 /* Return local info for the compiled function. */
691 struct cgraph_global_info *
692 cgraph_global_info (tree decl)
694 struct cgraph_node *node;
696 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
697 node = cgraph_node (decl);
698 return &node->global;
701 /* Return local info for the compiled function. */
703 struct cgraph_rtl_info *
704 cgraph_rtl_info (tree decl)
706 struct cgraph_node *node;
708 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
709 node = cgraph_node (decl);
710 if (decl != current_function_decl
711 && !TREE_ASM_WRITTEN (node->decl))
712 return NULL;
713 return &node->rtl;
716 /* Return name of the node used in debug output. */
717 const char *
718 cgraph_node_name (struct cgraph_node *node)
720 return lang_hooks.decl_printable_name (node->decl, 2);
723 /* Names used to print out the availability enum. */
724 const char * const cgraph_availability_names[] =
725 {"unset", "not_available", "overwrittable", "available", "local"};
728 /* Dump call graph node NODE to file F. */
730 void
731 dump_cgraph_node (FILE *f, struct cgraph_node *node)
733 struct cgraph_edge *edge;
734 fprintf (f, "%s/%i(%i):", cgraph_node_name (node), node->uid, node->pid);
735 if (node->global.inlined_to)
736 fprintf (f, " (inline copy in %s/%i)",
737 cgraph_node_name (node->global.inlined_to),
738 node->global.inlined_to->uid);
739 if (cgraph_function_flags_ready)
740 fprintf (f, " availability:%s",
741 cgraph_availability_names [cgraph_function_body_availability (node)]);
742 if (node->master_clone && node->master_clone->uid != node->uid)
743 fprintf (f, "(%i)", node->master_clone->uid);
744 if (node->count)
745 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
746 (HOST_WIDEST_INT)node->count);
747 if (node->local.self_insns)
748 fprintf (f, " %i insns", node->local.self_insns);
749 if (node->global.insns && node->global.insns != node->local.self_insns)
750 fprintf (f, " (%i after inlining)", node->global.insns);
751 if (node->local.estimated_self_stack_size)
752 fprintf (f, " %i bytes stack usage", (int)node->local.estimated_self_stack_size);
753 if (node->global.estimated_stack_size != node->local.estimated_self_stack_size)
754 fprintf (f, " %i bytes after inlining", (int)node->global.estimated_stack_size);
755 if (node->origin)
756 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
757 if (node->needed)
758 fprintf (f, " needed");
759 else if (node->reachable)
760 fprintf (f, " reachable");
761 if (DECL_SAVED_TREE (node->decl))
762 fprintf (f, " tree");
763 if (node->output)
764 fprintf (f, " output");
765 if (node->local.local)
766 fprintf (f, " local");
767 if (node->local.externally_visible)
768 fprintf (f, " externally_visible");
769 if (node->local.finalized)
770 fprintf (f, " finalized");
771 if (node->local.disregard_inline_limits)
772 fprintf (f, " always_inline");
773 else if (node->local.inlinable)
774 fprintf (f, " inlinable");
775 if (node->local.redefined_extern_inline)
776 fprintf (f, " redefined_extern_inline");
777 if (TREE_ASM_WRITTEN (node->decl))
778 fprintf (f, " asm_written");
780 fprintf (f, "\n called by: ");
781 for (edge = node->callers; edge; edge = edge->next_caller)
783 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
784 edge->caller->uid);
785 if (edge->count)
786 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
787 (HOST_WIDEST_INT)edge->count);
788 if (edge->frequency)
789 fprintf (f, "(%.2f per call) ",
790 edge->frequency / (double)CGRAPH_FREQ_BASE);
791 if (!edge->inline_failed)
792 fprintf(f, "(inlined) ");
795 fprintf (f, "\n calls: ");
796 for (edge = node->callees; edge; edge = edge->next_callee)
798 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
799 edge->callee->uid);
800 if (!edge->inline_failed)
801 fprintf(f, "(inlined) ");
802 if (edge->count)
803 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
804 (HOST_WIDEST_INT)edge->count);
805 if (edge->frequency)
806 fprintf (f, "(%.2f per call) ",
807 edge->frequency / (double)CGRAPH_FREQ_BASE);
808 if (edge->loop_nest)
809 fprintf (f, "(nested in %i loops) ", edge->loop_nest);
811 fprintf (f, "\n");
815 /* Dump call graph node NODE to stderr. */
817 void
818 debug_cgraph_node (struct cgraph_node *node)
820 dump_cgraph_node (stderr, node);
824 /* Dump the callgraph to file F. */
826 void
827 dump_cgraph (FILE *f)
829 struct cgraph_node *node;
831 fprintf (f, "callgraph:\n\n");
832 for (node = cgraph_nodes; node; node = node->next)
833 dump_cgraph_node (f, node);
837 /* Dump the call graph to stderr. */
839 void
840 debug_cgraph (void)
842 dump_cgraph (stderr);
846 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
848 void
849 change_decl_assembler_name (tree decl, tree name)
851 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
853 SET_DECL_ASSEMBLER_NAME (decl, name);
854 return;
856 if (name == DECL_ASSEMBLER_NAME (decl))
857 return;
859 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
860 && DECL_RTL_SET_P (decl))
861 warning (0, "%D renamed after being referenced in assembly", decl);
863 SET_DECL_ASSEMBLER_NAME (decl, name);
866 /* Add a top-level asm statement to the list. */
868 struct cgraph_asm_node *
869 cgraph_add_asm_node (tree asm_str)
871 struct cgraph_asm_node *node;
873 node = GGC_CNEW (struct cgraph_asm_node);
874 node->asm_str = asm_str;
875 node->order = cgraph_order++;
876 node->next = NULL;
877 if (cgraph_asm_nodes == NULL)
878 cgraph_asm_nodes = node;
879 else
880 cgraph_asm_last_node->next = node;
881 cgraph_asm_last_node = node;
882 return node;
885 /* Return true when the DECL can possibly be inlined. */
886 bool
887 cgraph_function_possibly_inlined_p (tree decl)
889 if (!cgraph_global_info_ready)
890 return (DECL_INLINE (decl) && !flag_really_no_inline);
891 return DECL_POSSIBLY_INLINED (decl);
894 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
895 struct cgraph_edge *
896 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
897 tree call_stmt, gcov_type count_scale, int freq_scale,
898 int loop_nest, bool update_original)
900 struct cgraph_edge *new;
901 gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
902 gcov_type freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
904 if (freq > CGRAPH_FREQ_MAX)
905 freq = CGRAPH_FREQ_MAX;
906 new = cgraph_create_edge (n, e->callee, call_stmt, count, freq,
907 e->loop_nest + loop_nest);
909 new->inline_failed = e->inline_failed;
910 if (update_original)
912 e->count -= new->count;
913 if (e->count < 0)
914 e->count = 0;
916 return new;
919 /* Create node representing clone of N executed COUNT times. Decrease
920 the execution counts from original node too.
922 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
923 function's profile to reflect the fact that part of execution is handled
924 by node. */
925 struct cgraph_node *
926 cgraph_clone_node (struct cgraph_node *n, gcov_type count, int freq, int loop_nest,
927 bool update_original)
929 struct cgraph_node *new = cgraph_create_node ();
930 struct cgraph_edge *e;
931 gcov_type count_scale;
933 new->decl = n->decl;
934 new->origin = n->origin;
935 if (new->origin)
937 new->next_nested = new->origin->nested;
938 new->origin->nested = new;
940 new->analyzed = n->analyzed;
941 new->local = n->local;
942 new->global = n->global;
943 new->rtl = n->rtl;
944 new->master_clone = n->master_clone;
945 new->count = count;
946 if (n->count)
947 count_scale = new->count * REG_BR_PROB_BASE / n->count;
948 else
949 count_scale = 0;
950 if (update_original)
952 n->count -= count;
953 if (n->count < 0)
954 n->count = 0;
957 for (e = n->callees;e; e=e->next_callee)
958 cgraph_clone_edge (e, new, e->call_stmt, count_scale, freq, loop_nest,
959 update_original);
961 new->next_clone = n->next_clone;
962 new->prev_clone = n;
963 n->next_clone = new;
964 if (new->next_clone)
965 new->next_clone->prev_clone = new;
967 return new;
970 /* Return true if N is an master_clone, (see cgraph_master_clone). */
972 bool
973 cgraph_is_master_clone (struct cgraph_node *n)
975 return (n == cgraph_master_clone (n));
978 struct cgraph_node *
979 cgraph_master_clone (struct cgraph_node *n)
981 enum availability avail = cgraph_function_body_availability (n);
983 if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
984 return NULL;
986 if (!n->master_clone)
987 n->master_clone = cgraph_node (n->decl);
989 return n->master_clone;
992 /* NODE is no longer nested function; update cgraph accordingly. */
993 void
994 cgraph_unnest_node (struct cgraph_node *node)
996 struct cgraph_node **node2 = &node->origin->nested;
997 gcc_assert (node->origin);
999 while (*node2 != node)
1000 node2 = &(*node2)->next_nested;
1001 *node2 = node->next_nested;
1002 node->origin = NULL;
1005 /* Return function availability. See cgraph.h for description of individual
1006 return values. */
1007 enum availability
1008 cgraph_function_body_availability (struct cgraph_node *node)
1010 enum availability avail;
1011 gcc_assert (cgraph_function_flags_ready);
1012 if (!node->analyzed)
1013 avail = AVAIL_NOT_AVAILABLE;
1014 else if (node->local.local)
1015 avail = AVAIL_LOCAL;
1016 else if (node->local.externally_visible)
1017 avail = AVAIL_AVAILABLE;
1019 /* If the function can be overwritten, return OVERWRITABLE. Take
1020 care at least of two notable extensions - the COMDAT functions
1021 used to share template instantiations in C++ (this is symmetric
1022 to code cp_cannot_inline_tree_fn and probably shall be shared and
1023 the inlinability hooks completely eliminated).
1025 ??? Does the C++ one definition rule allow us to always return
1026 AVAIL_AVAILABLE here? That would be good reason to preserve this
1027 hook Similarly deal with extern inline functions - this is again
1028 necessary to get C++ shared functions having keyed templates
1029 right and in the C extension documentation we probably should
1030 document the requirement of both versions of function (extern
1031 inline and offline) having same side effect characteristics as
1032 good optimization is what this optimization is about. */
1034 else if (!(*targetm.binds_local_p) (node->decl)
1035 && !DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl))
1036 avail = AVAIL_OVERWRITABLE;
1037 else avail = AVAIL_AVAILABLE;
1039 return avail;
1042 /* Add the function FNDECL to the call graph.
1043 Unlike cgraph_finalize_function, this function is intended to be used
1044 by middle end and allows insertion of new function at arbitrary point
1045 of compilation. The function can be either in high, low or SSA form
1046 GIMPLE.
1048 The function is assumed to be reachable and have address taken (so no
1049 API breaking optimizations are performed on it).
1051 Main work done by this function is to enqueue the function for later
1052 processing to avoid need the passes to be re-entrant. */
1054 void
1055 cgraph_add_new_function (tree fndecl, bool lowered)
1057 struct cgraph_node *node;
1058 switch (cgraph_state)
1060 case CGRAPH_STATE_CONSTRUCTION:
1061 /* Just enqueue function to be processed at nearest occurence. */
1062 node = cgraph_node (fndecl);
1063 node->next_needed = cgraph_new_nodes;
1064 if (lowered)
1065 node->lowered = true;
1066 cgraph_new_nodes = node;
1067 break;
1069 case CGRAPH_STATE_IPA:
1070 case CGRAPH_STATE_IPA_SSA:
1071 case CGRAPH_STATE_EXPANSION:
1072 /* Bring the function into finalized state and enqueue for later
1073 analyzing and compilation. */
1074 node = cgraph_node (fndecl);
1075 node->local.local = false;
1076 node->local.finalized = true;
1077 node->reachable = node->needed = true;
1078 if (lowered)
1079 node->lowered = true;
1080 node->next_needed = cgraph_new_nodes;
1081 cgraph_new_nodes = node;
1082 break;
1084 case CGRAPH_STATE_FINISHED:
1085 /* At the very end of compilation we have to do all the work up
1086 to expansion. */
1087 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
1088 current_function_decl = fndecl;
1089 tree_register_cfg_hooks ();
1090 if (!lowered)
1091 tree_lowering_passes (fndecl);
1092 bitmap_obstack_initialize (NULL);
1093 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)) && optimize)
1094 execute_pass_list (pass_early_local_passes.sub);
1095 bitmap_obstack_release (NULL);
1096 tree_rest_of_compilation (fndecl);
1097 pop_cfun ();
1098 current_function_decl = NULL;
1099 break;
1103 #include "gt-cgraph.h"