langhooks.h (struct lang_hooks_for_callgraph): Remove third argument from function...
[official-gcc.git] / gcc / cgraph.c
blob8c7bc5da44221c7f73650e135918e1450997f347
1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
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.
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.
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/>. */
21 /* This file contains basic routines manipulating call graph
23 The callgraph:
25 The call-graph is data structure designed for intra-procedural optimization
26 but it is also used in non-unit-at-a-time compilation to allow easier code
27 sharing.
29 The call-graph consist of nodes and edges represented via linked lists.
30 Each function (external or not) corresponds to the unique node.
32 The mapping from declarations to call-graph nodes is done using hash table
33 based on DECL_UID. The call-graph nodes are created lazily using
34 cgraph_node function when called for unknown declaration.
36 The callgraph at the moment does not represent indirect calls or calls
37 from other compilation unit. Flag NEEDED is set for each node that may
38 be accessed in such an invisible way and it shall be considered an
39 entry point to the callgraph.
41 Interprocedural information:
43 Callgraph is place to store data needed for interprocedural optimization.
44 All data structures are divided into three components: local_info that
45 is produced while analyzing the function, global_info that is result
46 of global walking of the callgraph on the end of compilation and
47 rtl_info used by RTL backend to propagate data from already compiled
48 functions to their callers.
50 Inlining plans:
52 The function inlining information is decided in advance and maintained
53 in the callgraph as so called inline plan.
54 For each inlined call, the callee's node is cloned to represent the
55 new function copy produced by inliner.
56 Each inlined call gets a unique corresponding clone node of the callee
57 and the data structure is updated while inlining is performed, so
58 the clones are eliminated and their callee edges redirected to the
59 caller.
61 Each edge has "inline_failed" field. When the field is set to NULL,
62 the call will be inlined. When it is non-NULL it contains a reason
63 why inlining wasn't performed. */
65 #include "config.h"
66 #include "system.h"
67 #include "coretypes.h"
68 #include "tm.h"
69 #include "tree.h"
70 #include "tree-inline.h"
71 #include "langhooks.h"
72 #include "hashtab.h"
73 #include "toplev.h"
74 #include "flags.h"
75 #include "ggc.h"
76 #include "debug.h"
77 #include "target.h"
78 #include "basic-block.h"
79 #include "cgraph.h"
80 #include "varray.h"
81 #include "output.h"
82 #include "intl.h"
83 #include "tree-gimple.h"
84 #include "tree-dump.h"
85 #include "tree-flow.h"
87 static void cgraph_node_remove_callers (struct cgraph_node *node);
88 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
89 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
91 /* Hash table used to convert declarations into nodes. */
92 static GTY((param_is (struct cgraph_node))) htab_t cgraph_hash;
94 /* The linked list of cgraph nodes. */
95 struct cgraph_node *cgraph_nodes;
97 /* Queue of cgraph nodes scheduled to be lowered. */
98 struct cgraph_node *cgraph_nodes_queue;
100 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
101 secondary queue used during optimization to accommodate passes that
102 may generate new functions that need to be optimized and expanded. */
103 struct cgraph_node *cgraph_new_nodes;
105 /* Number of nodes in existence. */
106 int cgraph_n_nodes;
108 /* Maximal uid used in cgraph nodes. */
109 int cgraph_max_uid;
111 /* Maximal pid used for profiling */
112 int cgraph_max_pid;
114 /* Set when whole unit has been analyzed so we can access global info. */
115 bool cgraph_global_info_ready = false;
117 /* What state callgraph is in right now. */
118 enum cgraph_state cgraph_state = CGRAPH_STATE_CONSTRUCTION;
120 /* Set when the cgraph is fully build and the basic flags are computed. */
121 bool cgraph_function_flags_ready = false;
123 /* Linked list of cgraph asm nodes. */
124 struct cgraph_asm_node *cgraph_asm_nodes;
126 /* Last node in cgraph_asm_nodes. */
127 static GTY(()) struct cgraph_asm_node *cgraph_asm_last_node;
129 /* The order index of the next cgraph node to be created. This is
130 used so that we can sort the cgraph nodes in order by when we saw
131 them, to support -fno-toplevel-reorder. */
132 int cgraph_order;
134 static hashval_t hash_node (const void *);
135 static int eq_node (const void *, const void *);
137 /* Returns a hash code for P. */
139 static hashval_t
140 hash_node (const void *p)
142 const struct cgraph_node *n = (const struct cgraph_node *) p;
143 return (hashval_t) DECL_UID (n->decl);
146 /* Returns nonzero if P1 and P2 are equal. */
148 static int
149 eq_node (const void *p1, const void *p2)
151 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
152 const struct cgraph_node *n2 = (const struct cgraph_node *) p2;
153 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
156 /* Allocate new callgraph node and insert it into basic data structures. */
158 static struct cgraph_node *
159 cgraph_create_node (void)
161 struct cgraph_node *node;
163 node = GGC_CNEW (struct cgraph_node);
164 node->next = cgraph_nodes;
165 node->uid = cgraph_max_uid++;
166 node->pid = -1;
167 node->order = cgraph_order++;
168 if (cgraph_nodes)
169 cgraph_nodes->previous = node;
170 node->previous = NULL;
171 node->global.estimated_growth = INT_MIN;
172 cgraph_nodes = node;
173 cgraph_n_nodes++;
174 return node;
177 /* Return cgraph node assigned to DECL. Create new one when needed. */
179 struct cgraph_node *
180 cgraph_node (tree decl)
182 struct cgraph_node key, *node, **slot;
184 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
186 if (!cgraph_hash)
187 cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
189 key.decl = decl;
191 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
193 if (*slot)
195 node = *slot;
196 if (!node->master_clone)
197 node->master_clone = node;
198 return node;
201 node = cgraph_create_node ();
202 node->decl = decl;
203 *slot = node;
204 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
206 node->origin = cgraph_node (DECL_CONTEXT (decl));
207 node->next_nested = node->origin->nested;
208 node->origin->nested = node;
209 node->master_clone = node;
211 return node;
214 /* Insert already constructed node into hashtable. */
216 void
217 cgraph_insert_node_to_hashtable (struct cgraph_node *node)
219 struct cgraph_node **slot;
221 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, node, INSERT);
223 gcc_assert (!*slot);
224 *slot = node;
228 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
229 Return NULL if there's no such node. */
231 struct cgraph_node *
232 cgraph_node_for_asm (tree asmname)
234 struct cgraph_node *node;
236 for (node = cgraph_nodes; node ; node = node->next)
237 if (decl_assembler_name_equal (node->decl, asmname))
238 return node;
240 return NULL;
243 /* Returns a hash value for X (which really is a die_struct). */
245 static hashval_t
246 edge_hash (const void *x)
248 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
251 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
253 static int
254 edge_eq (const void *x, const void *y)
256 return ((const struct cgraph_edge *) x)->call_stmt == y;
259 /* Return callgraph edge representing CALL_EXPR statement. */
260 struct cgraph_edge *
261 cgraph_edge (struct cgraph_node *node, tree call_stmt)
263 struct cgraph_edge *e, *e2;
264 int n = 0;
266 if (node->call_site_hash)
267 return htab_find_with_hash (node->call_site_hash, call_stmt,
268 htab_hash_pointer (call_stmt));
270 /* This loop may turn out to be performance problem. In such case adding
271 hashtables into call nodes with very many edges is probably best
272 solution. It is not good idea to add pointer into CALL_EXPR itself
273 because we want to make possible having multiple cgraph nodes representing
274 different clones of the same body before the body is actually cloned. */
275 for (e = node->callees; e; e= e->next_callee)
277 if (e->call_stmt == call_stmt)
278 break;
279 n++;
281 if (n > 100)
283 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
284 for (e2 = node->callees; e2; e2 = e2->next_callee)
286 void **slot;
287 slot = htab_find_slot_with_hash (node->call_site_hash,
288 e2->call_stmt,
289 htab_hash_pointer (e2->call_stmt),
290 INSERT);
291 gcc_assert (!*slot);
292 *slot = e2;
295 return e;
298 /* Change call_smtt of edge E to NEW_STMT. */
300 void
301 cgraph_set_call_stmt (struct cgraph_edge *e, tree new_stmt)
303 if (e->caller->call_site_hash)
305 htab_remove_elt_with_hash (e->caller->call_site_hash,
306 e->call_stmt,
307 htab_hash_pointer (e->call_stmt));
309 e->call_stmt = new_stmt;
310 if (e->caller->call_site_hash)
312 void **slot;
313 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
314 e->call_stmt,
315 htab_hash_pointer
316 (e->call_stmt), INSERT);
317 gcc_assert (!*slot);
318 *slot = e;
322 /* Create edge from CALLER to CALLEE in the cgraph. */
324 struct cgraph_edge *
325 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
326 tree call_stmt, gcov_type count, int freq, int nest)
328 struct cgraph_edge *edge = GGC_NEW (struct cgraph_edge);
329 #ifdef ENABLE_CHECKING
330 struct cgraph_edge *e;
332 for (e = caller->callees; e; e = e->next_callee)
333 gcc_assert (e->call_stmt != call_stmt);
334 #endif
336 gcc_assert (get_call_expr_in (call_stmt));
338 if (!DECL_SAVED_TREE (callee->decl))
339 edge->inline_failed = N_("function body not available");
340 else if (callee->local.redefined_extern_inline)
341 edge->inline_failed = N_("redefined extern inline functions are not "
342 "considered for inlining");
343 else if (callee->local.inlinable)
344 edge->inline_failed = N_("function not considered for inlining");
345 else
346 edge->inline_failed = N_("function not inlinable");
348 edge->aux = NULL;
350 edge->caller = caller;
351 edge->callee = callee;
352 edge->call_stmt = call_stmt;
353 edge->prev_caller = NULL;
354 edge->next_caller = callee->callers;
355 if (callee->callers)
356 callee->callers->prev_caller = edge;
357 edge->prev_callee = NULL;
358 edge->next_callee = caller->callees;
359 if (caller->callees)
360 caller->callees->prev_callee = edge;
361 caller->callees = edge;
362 callee->callers = edge;
363 edge->count = count;
364 gcc_assert (count >= 0);
365 edge->frequency = freq;
366 gcc_assert (freq >= 0);
367 gcc_assert (freq <= CGRAPH_FREQ_MAX);
368 edge->loop_nest = nest;
369 if (caller->call_site_hash)
371 void **slot;
372 slot = htab_find_slot_with_hash (caller->call_site_hash,
373 edge->call_stmt,
374 htab_hash_pointer
375 (edge->call_stmt),
376 INSERT);
377 gcc_assert (!*slot);
378 *slot = edge;
380 return edge;
383 /* Remove the edge E from the list of the callers of the callee. */
385 static inline void
386 cgraph_edge_remove_callee (struct cgraph_edge *e)
388 if (e->prev_caller)
389 e->prev_caller->next_caller = e->next_caller;
390 if (e->next_caller)
391 e->next_caller->prev_caller = e->prev_caller;
392 if (!e->prev_caller)
393 e->callee->callers = e->next_caller;
396 /* Remove the edge E from the list of the callees of the caller. */
398 static inline void
399 cgraph_edge_remove_caller (struct cgraph_edge *e)
401 if (e->prev_callee)
402 e->prev_callee->next_callee = e->next_callee;
403 if (e->next_callee)
404 e->next_callee->prev_callee = e->prev_callee;
405 if (!e->prev_callee)
406 e->caller->callees = e->next_callee;
407 if (e->caller->call_site_hash)
408 htab_remove_elt_with_hash (e->caller->call_site_hash,
409 e->call_stmt,
410 htab_hash_pointer (e->call_stmt));
413 /* Remove the edge E in the cgraph. */
415 void
416 cgraph_remove_edge (struct cgraph_edge *e)
418 /* Remove from callers list of the callee. */
419 cgraph_edge_remove_callee (e);
421 /* Remove from callees list of the callers. */
422 cgraph_edge_remove_caller (e);
425 /* Redirect callee of E to N. The function does not update underlying
426 call expression. */
428 void
429 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
431 /* Remove from callers list of the current callee. */
432 cgraph_edge_remove_callee (e);
434 /* Insert to callers list of the new callee. */
435 e->prev_caller = NULL;
436 if (n->callers)
437 n->callers->prev_caller = e;
438 e->next_caller = n->callers;
439 n->callers = e;
440 e->callee = n;
443 /* Remove all callees from the node. */
445 void
446 cgraph_node_remove_callees (struct cgraph_node *node)
448 struct cgraph_edge *e;
450 /* It is sufficient to remove the edges from the lists of callers of
451 the callees. The callee list of the node can be zapped with one
452 assignment. */
453 for (e = node->callees; e; e = e->next_callee)
454 cgraph_edge_remove_callee (e);
455 node->callees = NULL;
456 if (node->call_site_hash)
458 htab_delete (node->call_site_hash);
459 node->call_site_hash = NULL;
463 /* Remove all callers from the node. */
465 static void
466 cgraph_node_remove_callers (struct cgraph_node *node)
468 struct cgraph_edge *e;
470 /* It is sufficient to remove the edges from the lists of callees of
471 the callers. The caller list of the node can be zapped with one
472 assignment. */
473 for (e = node->callers; e; e = e->next_caller)
474 cgraph_edge_remove_caller (e);
475 node->callers = NULL;
478 /* Release memory used to represent body of function NODE. */
480 void
481 cgraph_release_function_body (struct cgraph_node *node)
483 if (DECL_STRUCT_FUNCTION (node->decl)
484 && DECL_STRUCT_FUNCTION (node->decl)->gimple_df)
486 tree old_decl = current_function_decl;
487 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
488 current_function_decl = node->decl;
489 delete_tree_ssa ();
490 delete_tree_cfg_annotations ();
491 cfun->eh = NULL;
492 current_function_decl = old_decl;
493 pop_cfun();
495 DECL_SAVED_TREE (node->decl) = NULL;
496 DECL_STRUCT_FUNCTION (node->decl) = NULL;
497 DECL_INITIAL (node->decl) = error_mark_node;
500 /* Remove the node from cgraph. */
502 void
503 cgraph_remove_node (struct cgraph_node *node)
505 void **slot;
506 bool kill_body = false;
508 cgraph_node_remove_callers (node);
509 cgraph_node_remove_callees (node);
510 /* Incremental inlining access removed nodes stored in the postorder list.
512 node->needed = node->reachable = false;
513 while (node->nested)
514 cgraph_remove_node (node->nested);
515 if (node->origin)
517 struct cgraph_node **node2 = &node->origin->nested;
519 while (*node2 != node)
520 node2 = &(*node2)->next_nested;
521 *node2 = node->next_nested;
523 if (node->previous)
524 node->previous->next = node->next;
525 else
526 cgraph_nodes = node->next;
527 if (node->next)
528 node->next->previous = node->previous;
529 node->next = NULL;
530 node->previous = NULL;
531 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
532 if (*slot == node)
534 if (node->next_clone)
536 struct cgraph_node *new_node = node->next_clone;
537 struct cgraph_node *n;
539 /* Make the next clone be the master clone */
540 for (n = new_node; n; n = n->next_clone)
541 n->master_clone = new_node;
543 *slot = new_node;
544 node->next_clone->prev_clone = NULL;
546 else
548 htab_clear_slot (cgraph_hash, slot);
549 kill_body = true;
552 else
554 node->prev_clone->next_clone = node->next_clone;
555 if (node->next_clone)
556 node->next_clone->prev_clone = node->prev_clone;
559 /* While all the clones are removed after being proceeded, the function
560 itself is kept in the cgraph even after it is compiled. Check whether
561 we are done with this body and reclaim it proactively if this is the case.
563 if (!kill_body && *slot)
565 struct cgraph_node *n = (struct cgraph_node *) *slot;
566 if (!n->next_clone && !n->global.inlined_to
567 && (cgraph_global_info_ready
568 && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl))))
569 kill_body = true;
572 if (kill_body && flag_unit_at_a_time)
573 cgraph_release_function_body (node);
574 node->decl = NULL;
575 if (node->call_site_hash)
577 htab_delete (node->call_site_hash);
578 node->call_site_hash = NULL;
580 cgraph_n_nodes--;
581 /* Do not free the structure itself so the walk over chain can continue. */
584 /* Notify finalize_compilation_unit that given node is reachable. */
586 void
587 cgraph_mark_reachable_node (struct cgraph_node *node)
589 if (!node->reachable && node->local.finalized)
591 notice_global_symbol (node->decl);
592 node->reachable = 1;
593 gcc_assert (!cgraph_global_info_ready);
595 node->next_needed = cgraph_nodes_queue;
596 cgraph_nodes_queue = node;
600 /* Likewise indicate that a node is needed, i.e. reachable via some
601 external means. */
603 void
604 cgraph_mark_needed_node (struct cgraph_node *node)
606 node->needed = 1;
607 cgraph_mark_reachable_node (node);
610 /* Return local info for the compiled function. */
612 struct cgraph_local_info *
613 cgraph_local_info (tree decl)
615 struct cgraph_node *node;
617 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
618 node = cgraph_node (decl);
619 return &node->local;
622 /* Return local info for the compiled function. */
624 struct cgraph_global_info *
625 cgraph_global_info (tree decl)
627 struct cgraph_node *node;
629 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
630 node = cgraph_node (decl);
631 return &node->global;
634 /* Return local info for the compiled function. */
636 struct cgraph_rtl_info *
637 cgraph_rtl_info (tree decl)
639 struct cgraph_node *node;
641 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
642 node = cgraph_node (decl);
643 if (decl != current_function_decl
644 && !TREE_ASM_WRITTEN (node->decl))
645 return NULL;
646 return &node->rtl;
649 /* Return name of the node used in debug output. */
650 const char *
651 cgraph_node_name (struct cgraph_node *node)
653 return lang_hooks.decl_printable_name (node->decl, 2);
656 /* Names used to print out the availability enum. */
657 const char * const cgraph_availability_names[] =
658 {"unset", "not_available", "overwrittable", "available", "local"};
661 /* Dump call graph node NODE to file F. */
663 void
664 dump_cgraph_node (FILE *f, struct cgraph_node *node)
666 struct cgraph_edge *edge;
667 fprintf (f, "%s/%i(%i):", cgraph_node_name (node), node->uid, node->pid);
668 if (node->global.inlined_to)
669 fprintf (f, " (inline copy in %s/%i)",
670 cgraph_node_name (node->global.inlined_to),
671 node->global.inlined_to->uid);
672 if (cgraph_function_flags_ready)
673 fprintf (f, " availability:%s",
674 cgraph_availability_names [cgraph_function_body_availability (node)]);
675 if (node->master_clone && node->master_clone->uid != node->uid)
676 fprintf (f, "(%i)", node->master_clone->uid);
677 if (node->count)
678 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
679 (HOST_WIDEST_INT)node->count);
680 if (node->local.self_insns)
681 fprintf (f, " %i insns", node->local.self_insns);
682 if (node->global.insns && node->global.insns != node->local.self_insns)
683 fprintf (f, " (%i after inlining)", node->global.insns);
684 if (node->local.estimated_self_stack_size)
685 fprintf (f, " %i bytes stack usage", (int)node->local.estimated_self_stack_size);
686 if (node->global.estimated_stack_size != node->local.estimated_self_stack_size)
687 fprintf (f, " %i bytes after inlining", (int)node->global.estimated_stack_size);
688 if (node->origin)
689 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
690 if (node->needed)
691 fprintf (f, " needed");
692 else if (node->reachable)
693 fprintf (f, " reachable");
694 if (DECL_SAVED_TREE (node->decl))
695 fprintf (f, " tree");
696 if (node->output)
697 fprintf (f, " output");
698 if (node->local.local)
699 fprintf (f, " local");
700 if (node->local.externally_visible)
701 fprintf (f, " externally_visible");
702 if (node->local.finalized)
703 fprintf (f, " finalized");
704 if (node->local.disregard_inline_limits)
705 fprintf (f, " always_inline");
706 else if (node->local.inlinable)
707 fprintf (f, " inlinable");
708 if (node->local.redefined_extern_inline)
709 fprintf (f, " redefined_extern_inline");
710 if (TREE_ASM_WRITTEN (node->decl))
711 fprintf (f, " asm_written");
713 fprintf (f, "\n called by: ");
714 for (edge = node->callers; edge; edge = edge->next_caller)
716 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
717 edge->caller->uid);
718 if (edge->count)
719 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
720 (HOST_WIDEST_INT)edge->count);
721 if (edge->frequency)
722 fprintf (f, "(%.2f per call) ",
723 edge->frequency / (double)CGRAPH_FREQ_BASE);
724 if (!edge->inline_failed)
725 fprintf(f, "(inlined) ");
728 fprintf (f, "\n calls: ");
729 for (edge = node->callees; edge; edge = edge->next_callee)
731 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
732 edge->callee->uid);
733 if (!edge->inline_failed)
734 fprintf(f, "(inlined) ");
735 if (edge->count)
736 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
737 (HOST_WIDEST_INT)edge->count);
738 if (edge->frequency)
739 fprintf (f, "(%.2f per call) ",
740 edge->frequency / (double)CGRAPH_FREQ_BASE);
741 if (edge->loop_nest)
742 fprintf (f, "(nested in %i loops) ", edge->loop_nest);
744 fprintf (f, "\n");
748 /* Dump call graph node NODE to stderr. */
750 void
751 debug_cgraph_node (struct cgraph_node *node)
753 dump_cgraph_node (stderr, node);
757 /* Dump the callgraph to file F. */
759 void
760 dump_cgraph (FILE *f)
762 struct cgraph_node *node;
764 fprintf (f, "callgraph:\n\n");
765 for (node = cgraph_nodes; node; node = node->next)
766 dump_cgraph_node (f, node);
770 /* Dump the call graph to stderr. */
772 void
773 debug_cgraph (void)
775 dump_cgraph (stderr);
779 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
781 void
782 change_decl_assembler_name (tree decl, tree name)
784 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
786 SET_DECL_ASSEMBLER_NAME (decl, name);
787 return;
789 if (name == DECL_ASSEMBLER_NAME (decl))
790 return;
792 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
793 && DECL_RTL_SET_P (decl))
794 warning (0, "%D renamed after being referenced in assembly", decl);
796 SET_DECL_ASSEMBLER_NAME (decl, name);
799 /* Add a top-level asm statement to the list. */
801 struct cgraph_asm_node *
802 cgraph_add_asm_node (tree asm_str)
804 struct cgraph_asm_node *node;
806 node = GGC_CNEW (struct cgraph_asm_node);
807 node->asm_str = asm_str;
808 node->order = cgraph_order++;
809 node->next = NULL;
810 if (cgraph_asm_nodes == NULL)
811 cgraph_asm_nodes = node;
812 else
813 cgraph_asm_last_node->next = node;
814 cgraph_asm_last_node = node;
815 return node;
818 /* Return true when the DECL can possibly be inlined. */
819 bool
820 cgraph_function_possibly_inlined_p (tree decl)
822 if (!cgraph_global_info_ready)
823 return (DECL_INLINE (decl) && !flag_really_no_inline);
824 return DECL_POSSIBLY_INLINED (decl);
827 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
828 struct cgraph_edge *
829 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
830 tree call_stmt, gcov_type count_scale, int freq_scale,
831 int loop_nest, bool update_original)
833 struct cgraph_edge *new;
834 gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
835 gcov_type freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
837 if (freq > CGRAPH_FREQ_MAX)
838 freq = CGRAPH_FREQ_MAX;
839 new = cgraph_create_edge (n, e->callee, call_stmt, count, freq,
840 e->loop_nest + loop_nest);
842 new->inline_failed = e->inline_failed;
843 if (update_original)
845 e->count -= new->count;
846 if (e->count < 0)
847 e->count = 0;
849 return new;
852 /* Create node representing clone of N executed COUNT times. Decrease
853 the execution counts from original node too.
855 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
856 function's profile to reflect the fact that part of execution is handled
857 by node. */
858 struct cgraph_node *
859 cgraph_clone_node (struct cgraph_node *n, gcov_type count, int freq, int loop_nest,
860 bool update_original)
862 struct cgraph_node *new = cgraph_create_node ();
863 struct cgraph_edge *e;
864 gcov_type count_scale;
866 new->decl = n->decl;
867 new->origin = n->origin;
868 if (new->origin)
870 new->next_nested = new->origin->nested;
871 new->origin->nested = new;
873 new->analyzed = n->analyzed;
874 new->local = n->local;
875 new->global = n->global;
876 new->rtl = n->rtl;
877 new->master_clone = n->master_clone;
878 new->count = count;
879 if (n->count)
880 count_scale = new->count * REG_BR_PROB_BASE / n->count;
881 else
882 count_scale = 0;
883 if (update_original)
885 n->count -= count;
886 if (n->count < 0)
887 n->count = 0;
890 for (e = n->callees;e; e=e->next_callee)
891 cgraph_clone_edge (e, new, e->call_stmt, count_scale, freq, loop_nest,
892 update_original);
894 new->next_clone = n->next_clone;
895 new->prev_clone = n;
896 n->next_clone = new;
897 if (new->next_clone)
898 new->next_clone->prev_clone = new;
900 return new;
903 /* Return true if N is an master_clone, (see cgraph_master_clone). */
905 bool
906 cgraph_is_master_clone (struct cgraph_node *n)
908 return (n == cgraph_master_clone (n));
911 struct cgraph_node *
912 cgraph_master_clone (struct cgraph_node *n)
914 enum availability avail = cgraph_function_body_availability (n);
916 if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
917 return NULL;
919 if (!n->master_clone)
920 n->master_clone = cgraph_node (n->decl);
922 return n->master_clone;
925 /* NODE is no longer nested function; update cgraph accordingly. */
926 void
927 cgraph_unnest_node (struct cgraph_node *node)
929 struct cgraph_node **node2 = &node->origin->nested;
930 gcc_assert (node->origin);
932 while (*node2 != node)
933 node2 = &(*node2)->next_nested;
934 *node2 = node->next_nested;
935 node->origin = NULL;
938 /* Return function availability. See cgraph.h for description of individual
939 return values. */
940 enum availability
941 cgraph_function_body_availability (struct cgraph_node *node)
943 enum availability avail;
944 gcc_assert (cgraph_function_flags_ready);
945 if (!node->analyzed)
946 avail = AVAIL_NOT_AVAILABLE;
947 else if (node->local.local)
948 avail = AVAIL_LOCAL;
949 else if (node->local.externally_visible)
950 avail = AVAIL_AVAILABLE;
952 /* If the function can be overwritten, return OVERWRITABLE. Take
953 care at least of two notable extensions - the COMDAT functions
954 used to share template instantiations in C++ (this is symmetric
955 to code cp_cannot_inline_tree_fn and probably shall be shared and
956 the inlinability hooks completely eliminated).
958 ??? Does the C++ one definition rule allow us to always return
959 AVAIL_AVAILABLE here? That would be good reason to preserve this
960 hook Similarly deal with extern inline functions - this is again
961 necessary to get C++ shared functions having keyed templates
962 right and in the C extension documentation we probably should
963 document the requirement of both versions of function (extern
964 inline and offline) having same side effect characteristics as
965 good optimization is what this optimization is about. */
967 else if (!(*targetm.binds_local_p) (node->decl)
968 && !DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl))
969 avail = AVAIL_OVERWRITABLE;
970 else avail = AVAIL_AVAILABLE;
972 return avail;
975 /* Add the function FNDECL to the call graph.
976 Unlike cgraph_finalize_function, this function is intended to be used
977 by middle end and allows insertion of new function at arbitrary point
978 of compilation. The function can be either in high, low or SSA form
979 GIMPLE.
981 The function is assumed to be reachable and have address taken (so no
982 API breaking optimizations are performed on it).
984 Main work done by this function is to enqueue the function for later
985 processing to avoid need the passes to be re-entrant. */
987 void
988 cgraph_add_new_function (tree fndecl, bool lowered)
990 struct cgraph_node *node;
991 switch (cgraph_state)
993 case CGRAPH_STATE_CONSTRUCTION:
994 /* Just enqueue function to be processed at nearest occurence. */
995 node = cgraph_node (fndecl);
996 node->next_needed = cgraph_new_nodes;
997 if (lowered)
998 node->lowered = true;
999 cgraph_new_nodes = node;
1000 break;
1002 case CGRAPH_STATE_IPA:
1003 case CGRAPH_STATE_IPA_SSA:
1004 case CGRAPH_STATE_EXPANSION:
1005 /* Bring the function into finalized state and enqueue for later
1006 analyzing and compilation. */
1007 node = cgraph_node (fndecl);
1008 node->local.local = false;
1009 node->local.finalized = true;
1010 node->reachable = node->needed = true;
1011 if (lowered)
1012 node->lowered = true;
1013 node->next_needed = cgraph_new_nodes;
1014 cgraph_new_nodes = node;
1015 break;
1017 case CGRAPH_STATE_FINISHED:
1018 /* At the very end of compilation we have to do all the work up
1019 to expansion. */
1020 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
1021 current_function_decl = fndecl;
1022 tree_register_cfg_hooks ();
1023 if (!lowered)
1024 tree_lowering_passes (fndecl);
1025 bitmap_obstack_initialize (NULL);
1026 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)) && optimize)
1027 execute_pass_list (pass_early_local_passes.sub);
1028 bitmap_obstack_release (NULL);
1029 tree_rest_of_compilation (fndecl);
1030 pop_cfun ();
1031 current_function_decl = NULL;
1032 break;
1036 #include "gt-cgraph.h"