OSDN Git Service

2008-07-28 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / cgraph.c
1 /* Callgraph handling code.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
3    Free Software Foundation, Inc.
4    Contributed by Jan Hubicka
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>.  */
21
22 /*  This file contains basic routines manipulating call graph
23
24 The callgraph:
25
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.
29
30     The call-graph consist of nodes and edges represented via linked lists.
31     Each function (external or not) corresponds to the unique node.
32
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.
36
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.
41
42     Interprocedural information:
43
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.
50
51     Inlining plans:
52
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.
61
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.  */
65
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"
87
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);
91
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;
96
97 /* The linked list of cgraph nodes.  */
98 struct cgraph_node *cgraph_nodes;
99
100 /* Queue of cgraph nodes scheduled to be lowered.  */
101 struct cgraph_node *cgraph_nodes_queue;
102
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;
107
108 /* Number of nodes in existence.  */
109 int cgraph_n_nodes;
110
111 /* Maximal uid used in cgraph nodes.  */
112 int cgraph_max_uid;
113
114 /* Maximal uid used in cgraph edges.  */
115 int cgraph_edge_max_uid;
116
117 /* Maximal pid used for profiling */
118 int cgraph_max_pid;
119
120 /* Set when whole unit has been analyzed so we can access global info.  */
121 bool cgraph_global_info_ready = false;
122
123 /* What state callgraph is in right now.  */
124 enum cgraph_state cgraph_state = CGRAPH_STATE_CONSTRUCTION;
125
126 /* Set when the cgraph is fully build and the basic flags are computed.  */
127 bool cgraph_function_flags_ready = false;
128
129 /* Linked list of cgraph asm nodes.  */
130 struct cgraph_asm_node *cgraph_asm_nodes;
131
132 /* Last node in cgraph_asm_nodes.  */
133 static GTY(()) struct cgraph_asm_node *cgraph_asm_last_node;
134
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;
139
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;
145 };
146
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;
152 };
153
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;
159 };
160
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;
166 };
167
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
177
178 /* Register HOOK to be called with DATA on each removed edge.  */
179 struct cgraph_edge_hook_list *
180 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
181 {
182   struct cgraph_edge_hook_list *entry;
183   struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
184
185   entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
186   entry->hook = hook;
187   entry->data = data;
188   entry->next = NULL;
189   while (*ptr)
190     ptr = &(*ptr)->next;
191   *ptr = entry;
192   return entry;
193 }
194
195 /* Remove ENTRY from the list of hooks called on removing edges.  */
196 void
197 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
198 {
199   struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
200
201   while (*ptr != entry)
202     ptr = &(*ptr)->next;
203   *ptr = entry->next;
204 }
205
206 /* Call all edge removal hooks.  */
207 static void
208 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
209 {
210   struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
211   while (entry)
212   {
213     entry->hook (e, entry->data);
214     entry = entry->next;
215   }
216 }
217
218 /* Register HOOK to be called with DATA on each removed node.  */
219 struct cgraph_node_hook_list *
220 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
221 {
222   struct cgraph_node_hook_list *entry;
223   struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
224
225   entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
226   entry->hook = hook;
227   entry->data = data;
228   entry->next = NULL;
229   while (*ptr)
230     ptr = &(*ptr)->next;
231   *ptr = entry;
232   return entry;
233 }
234
235 /* Remove ENTRY from the list of hooks called on removing nodes.  */
236 void
237 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
238 {
239   struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
240
241   while (*ptr != entry)
242     ptr = &(*ptr)->next;
243   *ptr = entry->next;
244 }
245
246 /* Call all node removal hooks.  */
247 static void
248 cgraph_call_node_removal_hooks (struct cgraph_node *node)
249 {
250   struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
251   while (entry)
252   {
253     entry->hook (node, entry->data);
254     entry = entry->next;
255   }
256 }
257
258 /* Register HOOK to be called with DATA on each duplicated edge.  */
259 struct cgraph_2edge_hook_list *
260 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
261 {
262   struct cgraph_2edge_hook_list *entry;
263   struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
264
265   entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
266   entry->hook = hook;
267   entry->data = data;
268   entry->next = NULL;
269   while (*ptr)
270     ptr = &(*ptr)->next;
271   *ptr = entry;
272   return entry;
273 }
274
275 /* Remove ENTRY from the list of hooks called on duplicating edges.  */
276 void
277 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
278 {
279   struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
280
281   while (*ptr != entry)
282     ptr = &(*ptr)->next;
283   *ptr = entry->next;
284 }
285
286 /* Call all edge duplication hooks.  */
287 static void
288 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
289                                     struct cgraph_edge *cs2)
290 {
291   struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
292   while (entry)
293   {
294     entry->hook (cs1, cs2, entry->data);
295     entry = entry->next;
296   }
297 }
298
299 /* Register HOOK to be called with DATA on each duplicated node.  */
300 struct cgraph_2node_hook_list *
301 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
302 {
303   struct cgraph_2node_hook_list *entry;
304   struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
305
306   entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
307   entry->hook = hook;
308   entry->data = data;
309   entry->next = NULL;
310   while (*ptr)
311     ptr = &(*ptr)->next;
312   *ptr = entry;
313   return entry;
314 }
315
316 /* Remove ENTRY from the list of hooks called on duplicating nodes.  */
317 void
318 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
319 {
320   struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
321
322   while (*ptr != entry)
323     ptr = &(*ptr)->next;
324   *ptr = entry->next;
325 }
326
327 /* Call all node duplication hooks.  */
328 static void
329 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
330                                     struct cgraph_node *node2)
331 {
332   struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
333   while (entry)
334   {
335     entry->hook (node1, node2, entry->data);
336     entry = entry->next;
337   }
338 }
339
340 /* Returns a hash code for P.  */
341
342 static hashval_t
343 hash_node (const void *p)
344 {
345   const struct cgraph_node *n = (const struct cgraph_node *) p;
346   return (hashval_t) DECL_UID (n->decl);
347 }
348
349 /* Returns nonzero if P1 and P2 are equal.  */
350
351 static int
352 eq_node (const void *p1, const void *p2)
353 {
354   const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
355   const struct cgraph_node *n2 = (const struct cgraph_node *) p2;
356   return DECL_UID (n1->decl) == DECL_UID (n2->decl);
357 }
358
359 /* Allocate new callgraph node and insert it into basic data structures.  */
360
361 static struct cgraph_node *
362 cgraph_create_node (void)
363 {
364   struct cgraph_node *node;
365
366   node = GGC_CNEW (struct cgraph_node);
367   node->next = cgraph_nodes;
368   node->uid = cgraph_max_uid++;
369   node->pid = -1;
370   node->order = cgraph_order++;
371   if (cgraph_nodes)
372     cgraph_nodes->previous = node;
373   node->previous = NULL;
374   node->global.estimated_growth = INT_MIN;
375   cgraph_nodes = node;
376   cgraph_n_nodes++;
377   return node;
378 }
379
380 /* Return cgraph node assigned to DECL.  Create new one when needed.  */
381
382 struct cgraph_node *
383 cgraph_node (tree decl)
384 {
385   struct cgraph_node key, *node, **slot;
386
387   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
388
389   if (!cgraph_hash)
390     cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
391
392   key.decl = decl;
393
394   slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
395
396   if (*slot)
397     {
398       node = *slot;
399       if (!node->master_clone)
400         node->master_clone = node;
401       return node;
402     }
403
404   node = cgraph_create_node ();
405   node->decl = decl;
406   *slot = node;
407   if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
408     {
409       node->origin = cgraph_node (DECL_CONTEXT (decl));
410       node->next_nested = node->origin->nested;
411       node->origin->nested = node;
412       node->master_clone = node;
413     }
414
415   return node;
416 }
417
418 /* Insert already constructed node into hashtable.  */
419
420 void
421 cgraph_insert_node_to_hashtable (struct cgraph_node *node)
422 {
423   struct cgraph_node **slot;
424
425   slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, node, INSERT);
426
427   gcc_assert (!*slot);
428   *slot = node;
429 }
430
431 /* Returns a hash code for P.  */
432
433 static hashval_t
434 hash_node_by_assembler_name (const void *p)
435 {
436   const struct cgraph_node *n = (const struct cgraph_node *) p;
437   return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
438 }
439
440 /* Returns nonzero if P1 and P2 are equal.  */
441
442 static int
443 eq_assembler_name (const void *p1, const void *p2)
444 {
445   const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
446   const_tree name = (const_tree)p2;
447   return (decl_assembler_name_equal (n1->decl, name));
448 }
449
450 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
451    Return NULL if there's no such node.  */
452
453 struct cgraph_node *
454 cgraph_node_for_asm (tree asmname)
455 {
456   struct cgraph_node *node;
457   void **slot;
458
459   if (!assembler_name_hash)
460     {
461       assembler_name_hash =
462         htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
463                          NULL);
464       for (node = cgraph_nodes; node; node = node->next)
465         if (!node->global.inlined_to)
466           {
467             tree name = DECL_ASSEMBLER_NAME (node->decl);
468             slot = htab_find_slot_with_hash (assembler_name_hash, name,
469                                              decl_assembler_name_hash (name),
470                                              INSERT);
471             /* We can have multiple declarations with same assembler name. For C++
472                it is __builtin_strlen and strlen, for instance.  Do we need to
473                record them all?  Original implementation marked just first one
474                so lets hope for the best.  */
475             if (*slot)
476               continue;
477             *slot = node;
478           }
479     }
480
481   slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
482                                    decl_assembler_name_hash (asmname),
483                                    NO_INSERT);
484
485   if (slot)
486     return (struct cgraph_node *) *slot;
487   return NULL;
488 }
489
490 /* Returns a hash value for X (which really is a die_struct).  */
491
492 static hashval_t
493 edge_hash (const void *x)
494 {
495   return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
496 }
497
498 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
499
500 static int
501 edge_eq (const void *x, const void *y)
502 {
503   return ((const struct cgraph_edge *) x)->call_stmt == y;
504 }
505
506
507 /* Return the callgraph edge representing the GIMPLE_CALL statement
508    CALL_STMT.  */
509
510 struct cgraph_edge *
511 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
512 {
513   struct cgraph_edge *e, *e2;
514   int n = 0;
515
516   if (node->call_site_hash)
517     return (struct cgraph_edge *)
518       htab_find_with_hash (node->call_site_hash, call_stmt,
519                            htab_hash_pointer (call_stmt));
520
521   /* This loop may turn out to be performance problem.  In such case adding
522      hashtables into call nodes with very many edges is probably best
523      solution.  It is not good idea to add pointer into CALL_EXPR itself
524      because we want to make possible having multiple cgraph nodes representing
525      different clones of the same body before the body is actually cloned.  */
526   for (e = node->callees; e; e= e->next_callee)
527     {
528       if (e->call_stmt == call_stmt)
529         break;
530       n++;
531     }
532
533   if (n > 100)
534     {
535       node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
536       for (e2 = node->callees; e2; e2 = e2->next_callee)
537         {
538           void **slot;
539           slot = htab_find_slot_with_hash (node->call_site_hash,
540                                            e2->call_stmt,
541                                            htab_hash_pointer (e2->call_stmt),
542                                            INSERT);
543           gcc_assert (!*slot);
544           *slot = e2;
545         }
546     }
547
548   return e;
549 }
550
551
552 /* Change field call_smt of edge E to NEW_STMT.  */
553
554 void
555 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
556 {
557   if (e->caller->call_site_hash)
558     {
559       htab_remove_elt_with_hash (e->caller->call_site_hash,
560                                  e->call_stmt,
561                                  htab_hash_pointer (e->call_stmt));
562     }
563   e->call_stmt = new_stmt;
564   if (e->caller->call_site_hash)
565     {
566       void **slot;
567       slot = htab_find_slot_with_hash (e->caller->call_site_hash,
568                                        e->call_stmt,
569                                        htab_hash_pointer
570                                        (e->call_stmt), INSERT);
571       gcc_assert (!*slot);
572       *slot = e;
573     }
574 }
575
576 /* Create edge from CALLER to CALLEE in the cgraph.  */
577
578 struct cgraph_edge *
579 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
580                     gimple call_stmt, gcov_type count, int freq, int nest)
581 {
582   struct cgraph_edge *edge = GGC_NEW (struct cgraph_edge);
583 #ifdef ENABLE_CHECKING
584   struct cgraph_edge *e;
585
586   for (e = caller->callees; e; e = e->next_callee)
587     gcc_assert (e->call_stmt != call_stmt);
588 #endif
589
590   gcc_assert (is_gimple_call (call_stmt));
591
592   if (!gimple_body (callee->decl))
593     edge->inline_failed = N_("function body not available");
594   else if (callee->local.redefined_extern_inline)
595     edge->inline_failed = N_("redefined extern inline functions are not "
596                              "considered for inlining");
597   else if (callee->local.inlinable)
598     edge->inline_failed = N_("function not considered for inlining");
599   else
600     edge->inline_failed = N_("function not inlinable");
601
602   edge->aux = NULL;
603
604   edge->caller = caller;
605   edge->callee = callee;
606   edge->call_stmt = call_stmt;
607   edge->prev_caller = NULL;
608   edge->next_caller = callee->callers;
609   if (callee->callers)
610     callee->callers->prev_caller = edge;
611   edge->prev_callee = NULL;
612   edge->next_callee = caller->callees;
613   if (caller->callees)
614     caller->callees->prev_callee = edge;
615   caller->callees = edge;
616   callee->callers = edge;
617   edge->count = count;
618   gcc_assert (count >= 0);
619   edge->frequency = freq;
620   gcc_assert (freq >= 0);
621   gcc_assert (freq <= CGRAPH_FREQ_MAX);
622   edge->loop_nest = nest;
623   edge->indirect_call = 0;
624   edge->uid = cgraph_edge_max_uid++;
625   if (caller->call_site_hash)
626     {
627       void **slot;
628       slot = htab_find_slot_with_hash (caller->call_site_hash,
629                                        edge->call_stmt,
630                                        htab_hash_pointer
631                                          (edge->call_stmt),
632                                        INSERT);
633       gcc_assert (!*slot);
634       *slot = edge;
635     }
636   return edge;
637 }
638
639 /* Remove the edge E from the list of the callers of the callee.  */
640
641 static inline void
642 cgraph_edge_remove_callee (struct cgraph_edge *e)
643 {
644   if (e->prev_caller)
645     e->prev_caller->next_caller = e->next_caller;
646   if (e->next_caller)
647     e->next_caller->prev_caller = e->prev_caller;
648   if (!e->prev_caller)
649     e->callee->callers = e->next_caller;
650 }
651
652 /* Remove the edge E from the list of the callees of the caller.  */
653
654 static inline void
655 cgraph_edge_remove_caller (struct cgraph_edge *e)
656 {
657   if (e->prev_callee)
658     e->prev_callee->next_callee = e->next_callee;
659   if (e->next_callee)
660     e->next_callee->prev_callee = e->prev_callee;
661   if (!e->prev_callee)
662     e->caller->callees = e->next_callee;
663   if (e->caller->call_site_hash)
664     htab_remove_elt_with_hash (e->caller->call_site_hash,
665                                e->call_stmt,
666                                htab_hash_pointer (e->call_stmt));
667 }
668
669 /* Remove the edge E in the cgraph.  */
670
671 void
672 cgraph_remove_edge (struct cgraph_edge *e)
673 {
674   cgraph_call_edge_removal_hooks (e);
675   /* Remove from callers list of the callee.  */
676   cgraph_edge_remove_callee (e);
677
678   /* Remove from callees list of the callers.  */
679   cgraph_edge_remove_caller (e);
680 }
681
682 /* Redirect callee of E to N.  The function does not update underlying
683    call expression.  */
684
685 void
686 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
687 {
688   /* Remove from callers list of the current callee.  */
689   cgraph_edge_remove_callee (e);
690
691   /* Insert to callers list of the new callee.  */
692   e->prev_caller = NULL;
693   if (n->callers)
694     n->callers->prev_caller = e;
695   e->next_caller = n->callers;
696   n->callers = e;
697   e->callee = n;
698 }
699
700
701 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
702    OLD_STMT changed into NEW_STMT.  */
703
704 void
705 cgraph_update_edges_for_call_stmt (gimple old_stmt, gimple new_stmt)
706 {
707   tree new_call = (is_gimple_call (new_stmt)) ? gimple_call_fn (new_stmt) : 0;
708   tree old_call = (is_gimple_call (old_stmt)) ? gimple_call_fn (old_stmt) : 0;
709   struct cgraph_node *node = cgraph_node (cfun->decl);
710
711   if (old_call != new_call)
712     {
713       struct cgraph_edge *e = cgraph_edge (node, old_stmt);
714       struct cgraph_edge *ne = NULL;
715       tree new_decl;
716
717       if (e)
718         {
719           gcov_type count = e->count;
720           int frequency = e->frequency;
721           int loop_nest = e->loop_nest;
722
723           cgraph_remove_edge (e);
724           if (new_call)
725             {
726               new_decl = gimple_call_fndecl (new_stmt);
727               if (new_decl)
728                 {
729                   ne = cgraph_create_edge (node, cgraph_node (new_decl),
730                                            new_stmt, count, frequency,
731                                            loop_nest);
732                   gcc_assert (ne->inline_failed);
733                 }
734             }
735         }
736     }
737   else if (old_stmt != new_stmt)
738     {
739       struct cgraph_edge *e = cgraph_edge (node, old_stmt);
740
741       if (e)
742         cgraph_set_call_stmt (e, new_stmt);
743     }
744 }
745
746
747 /* Remove all callees from the node.  */
748
749 void
750 cgraph_node_remove_callees (struct cgraph_node *node)
751 {
752   struct cgraph_edge *e;
753
754   /* It is sufficient to remove the edges from the lists of callers of
755      the callees.  The callee list of the node can be zapped with one
756      assignment.  */
757   for (e = node->callees; e; e = e->next_callee)
758     {
759       cgraph_call_edge_removal_hooks (e);
760       cgraph_edge_remove_callee (e);
761     }
762   node->callees = NULL;
763   if (node->call_site_hash)
764     {
765       htab_delete (node->call_site_hash);
766       node->call_site_hash = NULL;
767     }
768 }
769
770 /* Remove all callers from the node.  */
771
772 static void
773 cgraph_node_remove_callers (struct cgraph_node *node)
774 {
775   struct cgraph_edge *e;
776
777   /* It is sufficient to remove the edges from the lists of callees of
778      the callers.  The caller list of the node can be zapped with one
779      assignment.  */
780   for (e = node->callers; e; e = e->next_caller)
781     {
782       cgraph_call_edge_removal_hooks (e);
783       cgraph_edge_remove_caller (e);
784     }
785   node->callers = NULL;
786 }
787
788 /* Release memory used to represent body of function NODE.  */
789
790 void
791 cgraph_release_function_body (struct cgraph_node *node)
792 {
793   if (DECL_STRUCT_FUNCTION (node->decl)
794       && DECL_STRUCT_FUNCTION (node->decl)->gimple_df)
795     {
796       tree old_decl = current_function_decl;
797       push_cfun (DECL_STRUCT_FUNCTION (node->decl));
798       current_function_decl = node->decl;
799       delete_tree_ssa ();
800       delete_tree_cfg_annotations ();
801       cfun->eh = NULL;
802       gimple_set_body (node->decl, NULL);
803       current_function_decl = old_decl;
804       pop_cfun();
805     }
806   DECL_SAVED_TREE (node->decl) = NULL;
807   DECL_STRUCT_FUNCTION (node->decl) = NULL;
808   DECL_INITIAL (node->decl) = error_mark_node;
809 }
810
811 /* Remove the node from cgraph.  */
812
813 void
814 cgraph_remove_node (struct cgraph_node *node)
815 {
816   void **slot;
817   bool kill_body = false;
818
819   cgraph_call_node_removal_hooks (node);
820   cgraph_node_remove_callers (node);
821   cgraph_node_remove_callees (node);
822
823   /* Incremental inlining access removed nodes stored in the postorder list.
824      */
825   node->needed = node->reachable = false;
826   while (node->nested)
827     cgraph_remove_node (node->nested);
828   if (node->origin)
829     {
830       struct cgraph_node **node2 = &node->origin->nested;
831
832       while (*node2 != node)
833         node2 = &(*node2)->next_nested;
834       *node2 = node->next_nested;
835     }
836   if (node->previous)
837     node->previous->next = node->next;
838   else
839     cgraph_nodes = node->next;
840   if (node->next)
841     node->next->previous = node->previous;
842   node->next = NULL;
843   node->previous = NULL;
844   slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
845   if (*slot == node)
846     {
847       if (node->next_clone)
848       {
849         struct cgraph_node *new_node = node->next_clone;
850         struct cgraph_node *n;
851
852         /* Make the next clone be the master clone */
853         for (n = new_node; n; n = n->next_clone)
854           n->master_clone = new_node;
855
856         *slot = new_node;
857         node->next_clone->prev_clone = NULL;
858       }
859       else
860         {
861           htab_clear_slot (cgraph_hash, slot);
862           kill_body = true;
863         }
864     }
865   else
866     {
867       node->prev_clone->next_clone = node->next_clone;
868       if (node->next_clone)
869         node->next_clone->prev_clone = node->prev_clone;
870     }
871
872   /* While all the clones are removed after being proceeded, the function
873      itself is kept in the cgraph even after it is compiled.  Check whether
874      we are done with this body and reclaim it proactively if this is the case.
875      */
876   if (!kill_body && *slot)
877     {
878       struct cgraph_node *n = (struct cgraph_node *) *slot;
879       if (!n->next_clone && !n->global.inlined_to
880           && (cgraph_global_info_ready
881               && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl))))
882         kill_body = true;
883     }
884   if (assembler_name_hash)
885     {
886       tree name = DECL_ASSEMBLER_NAME (node->decl);
887       slot = htab_find_slot_with_hash (assembler_name_hash, name,
888                                        decl_assembler_name_hash (name),
889                                        NO_INSERT);
890       /* Inline clones are not hashed.  */
891       if (slot && *slot == node)
892         htab_clear_slot (assembler_name_hash, slot);
893     }
894
895   if (kill_body)
896     cgraph_release_function_body (node);
897   node->decl = NULL;
898   if (node->call_site_hash)
899     {
900       htab_delete (node->call_site_hash);
901       node->call_site_hash = NULL;
902     }
903   cgraph_n_nodes--;
904   /* Do not free the structure itself so the walk over chain can continue.  */
905 }
906
907 /* Notify finalize_compilation_unit that given node is reachable.  */
908
909 void
910 cgraph_mark_reachable_node (struct cgraph_node *node)
911 {
912   if (!node->reachable && node->local.finalized)
913     {
914       notice_global_symbol (node->decl);
915       node->reachable = 1;
916       gcc_assert (!cgraph_global_info_ready);
917
918       node->next_needed = cgraph_nodes_queue;
919       cgraph_nodes_queue = node;
920     }
921 }
922
923 /* Likewise indicate that a node is needed, i.e. reachable via some
924    external means.  */
925
926 void
927 cgraph_mark_needed_node (struct cgraph_node *node)
928 {
929   node->needed = 1;
930   cgraph_mark_reachable_node (node);
931 }
932
933 /* Return local info for the compiled function.  */
934
935 struct cgraph_local_info *
936 cgraph_local_info (tree decl)
937 {
938   struct cgraph_node *node;
939
940   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
941   node = cgraph_node (decl);
942   return &node->local;
943 }
944
945 /* Return local info for the compiled function.  */
946
947 struct cgraph_global_info *
948 cgraph_global_info (tree decl)
949 {
950   struct cgraph_node *node;
951
952   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
953   node = cgraph_node (decl);
954   return &node->global;
955 }
956
957 /* Return local info for the compiled function.  */
958
959 struct cgraph_rtl_info *
960 cgraph_rtl_info (tree decl)
961 {
962   struct cgraph_node *node;
963
964   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
965   node = cgraph_node (decl);
966   if (decl != current_function_decl
967       && !TREE_ASM_WRITTEN (node->decl))
968     return NULL;
969   return &node->rtl;
970 }
971
972 /* Return name of the node used in debug output.  */
973 const char *
974 cgraph_node_name (struct cgraph_node *node)
975 {
976   return lang_hooks.decl_printable_name (node->decl, 2);
977 }
978
979 /* Names used to print out the availability enum.  */
980 const char * const cgraph_availability_names[] =
981   {"unset", "not_available", "overwritable", "available", "local"};
982
983
984 /* Dump call graph node NODE to file F.  */
985
986 void
987 dump_cgraph_node (FILE *f, struct cgraph_node *node)
988 {
989   struct cgraph_edge *edge;
990   fprintf (f, "%s/%i(%i):", cgraph_node_name (node), node->uid, node->pid);
991   if (node->global.inlined_to)
992     fprintf (f, " (inline copy in %s/%i)",
993              cgraph_node_name (node->global.inlined_to),
994              node->global.inlined_to->uid);
995   if (cgraph_function_flags_ready)
996     fprintf (f, " availability:%s",
997              cgraph_availability_names [cgraph_function_body_availability (node)]);
998   if (node->master_clone && node->master_clone->uid != node->uid)
999     fprintf (f, "(%i)", node->master_clone->uid);
1000   if (node->count)
1001     fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1002              (HOST_WIDEST_INT)node->count);
1003   if (node->local.inline_summary.self_insns)
1004     fprintf (f, " %i insns", node->local.inline_summary.self_insns);
1005   if (node->global.insns && node->global.insns
1006       != node->local.inline_summary.self_insns)
1007     fprintf (f, " (%i after inlining)", node->global.insns);
1008   if (node->local.inline_summary.estimated_self_stack_size)
1009     fprintf (f, " %i bytes stack usage", (int)node->local.inline_summary.estimated_self_stack_size);
1010   if (node->global.estimated_stack_size != node->local.inline_summary.estimated_self_stack_size)
1011     fprintf (f, " %i bytes after inlining", (int)node->global.estimated_stack_size);
1012   if (node->origin)
1013     fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
1014   if (node->needed)
1015     fprintf (f, " needed");
1016   else if (node->reachable)
1017     fprintf (f, " reachable");
1018   if (gimple_body (node->decl))
1019     fprintf (f, " body");
1020   if (node->output)
1021     fprintf (f, " output");
1022   if (node->local.local)
1023     fprintf (f, " local");
1024   if (node->local.externally_visible)
1025     fprintf (f, " externally_visible");
1026   if (node->local.finalized)
1027     fprintf (f, " finalized");
1028   if (node->local.disregard_inline_limits)
1029     fprintf (f, " always_inline");
1030   else if (node->local.inlinable)
1031     fprintf (f, " inlinable");
1032   if (node->local.redefined_extern_inline)
1033     fprintf (f, " redefined_extern_inline");
1034   if (TREE_ASM_WRITTEN (node->decl))
1035     fprintf (f, " asm_written");
1036
1037   fprintf (f, "\n  called by: ");
1038   for (edge = node->callers; edge; edge = edge->next_caller)
1039     {
1040       fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
1041                edge->caller->uid);
1042       if (edge->count)
1043         fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1044                  (HOST_WIDEST_INT)edge->count);
1045       if (edge->frequency)
1046         fprintf (f, "(%.2f per call) ",
1047                  edge->frequency / (double)CGRAPH_FREQ_BASE);
1048       if (!edge->inline_failed)
1049         fprintf(f, "(inlined) ");
1050       if (edge->indirect_call)
1051         fprintf(f, "(indirect) ");
1052     }
1053
1054   fprintf (f, "\n  calls: ");
1055   for (edge = node->callees; edge; edge = edge->next_callee)
1056     {
1057       fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
1058                edge->callee->uid);
1059       if (!edge->inline_failed)
1060         fprintf(f, "(inlined) ");
1061       if (edge->indirect_call)
1062         fprintf(f, "(indirect) ");
1063       if (edge->count)
1064         fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1065                  (HOST_WIDEST_INT)edge->count);
1066       if (edge->frequency)
1067         fprintf (f, "(%.2f per call) ",
1068                  edge->frequency / (double)CGRAPH_FREQ_BASE);
1069       if (edge->loop_nest)
1070         fprintf (f, "(nested in %i loops) ", edge->loop_nest);
1071     }
1072   fprintf (f, "\n");
1073 }
1074
1075
1076 /* Dump call graph node NODE to stderr.  */
1077
1078 void
1079 debug_cgraph_node (struct cgraph_node *node)
1080 {
1081   dump_cgraph_node (stderr, node);
1082 }
1083
1084
1085 /* Dump the callgraph to file F.  */
1086
1087 void
1088 dump_cgraph (FILE *f)
1089 {
1090   struct cgraph_node *node;
1091
1092   fprintf (f, "callgraph:\n\n");
1093   for (node = cgraph_nodes; node; node = node->next)
1094     dump_cgraph_node (f, node);
1095 }
1096
1097
1098 /* Dump the call graph to stderr.  */
1099
1100 void
1101 debug_cgraph (void)
1102 {
1103   dump_cgraph (stderr);
1104 }
1105
1106
1107 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables.  */
1108
1109 void
1110 change_decl_assembler_name (tree decl, tree name)
1111 {
1112   gcc_assert (!assembler_name_hash);
1113   if (!DECL_ASSEMBLER_NAME_SET_P (decl))
1114     {
1115       SET_DECL_ASSEMBLER_NAME (decl, name);
1116       return;
1117     }
1118   if (name == DECL_ASSEMBLER_NAME (decl))
1119     return;
1120
1121   if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
1122       && DECL_RTL_SET_P (decl))
1123     warning (0, "%D renamed after being referenced in assembly", decl);
1124
1125   SET_DECL_ASSEMBLER_NAME (decl, name);
1126 }
1127
1128 /* Add a top-level asm statement to the list.  */
1129
1130 struct cgraph_asm_node *
1131 cgraph_add_asm_node (tree asm_str)
1132 {
1133   struct cgraph_asm_node *node;
1134
1135   node = GGC_CNEW (struct cgraph_asm_node);
1136   node->asm_str = asm_str;
1137   node->order = cgraph_order++;
1138   node->next = NULL;
1139   if (cgraph_asm_nodes == NULL)
1140     cgraph_asm_nodes = node;
1141   else
1142     cgraph_asm_last_node->next = node;
1143   cgraph_asm_last_node = node;
1144   return node;
1145 }
1146
1147 /* Return true when the DECL can possibly be inlined.  */
1148 bool
1149 cgraph_function_possibly_inlined_p (tree decl)
1150 {
1151   if (!cgraph_global_info_ready)
1152     return !DECL_UNINLINABLE (decl) && !flag_really_no_inline;
1153   return DECL_POSSIBLY_INLINED (decl);
1154 }
1155
1156 /* Create clone of E in the node N represented by CALL_EXPR the callgraph.  */
1157 struct cgraph_edge *
1158 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
1159                    gimple call_stmt, gcov_type count_scale, int freq_scale,
1160                    int loop_nest, bool update_original)
1161 {
1162   struct cgraph_edge *new;
1163   gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
1164   gcov_type freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
1165
1166   if (freq > CGRAPH_FREQ_MAX)
1167     freq = CGRAPH_FREQ_MAX;
1168   new = cgraph_create_edge (n, e->callee, call_stmt, count, freq,
1169                             e->loop_nest + loop_nest);
1170
1171   new->inline_failed = e->inline_failed;
1172   new->indirect_call = e->indirect_call;
1173   if (update_original)
1174     {
1175       e->count -= new->count;
1176       if (e->count < 0)
1177         e->count = 0;
1178     }
1179   cgraph_call_edge_duplication_hooks (e, new);
1180   return new;
1181 }
1182
1183 /* Create node representing clone of N executed COUNT times.  Decrease
1184    the execution counts from original node too.
1185
1186    When UPDATE_ORIGINAL is true, the counts are subtracted from the original
1187    function's profile to reflect the fact that part of execution is handled
1188    by node.  */
1189 struct cgraph_node *
1190 cgraph_clone_node (struct cgraph_node *n, gcov_type count, int freq,
1191                    int loop_nest, bool update_original)
1192 {
1193   struct cgraph_node *new = cgraph_create_node ();
1194   struct cgraph_edge *e;
1195   gcov_type count_scale;
1196
1197   new->decl = n->decl;
1198   new->origin = n->origin;
1199   if (new->origin)
1200     {
1201       new->next_nested = new->origin->nested;
1202       new->origin->nested = new;
1203     }
1204   new->analyzed = n->analyzed;
1205   new->local = n->local;
1206   new->global = n->global;
1207   new->rtl = n->rtl;
1208   new->master_clone = n->master_clone;
1209   new->count = count;
1210   if (n->count)
1211     count_scale = new->count * REG_BR_PROB_BASE / n->count;
1212   else
1213     count_scale = 0;
1214   if (update_original)
1215     {
1216       n->count -= count;
1217       if (n->count < 0)
1218         n->count = 0;
1219     }
1220
1221   for (e = n->callees;e; e=e->next_callee)
1222     cgraph_clone_edge (e, new, e->call_stmt, count_scale, freq, loop_nest,
1223                        update_original);
1224
1225   new->next_clone = n->next_clone;
1226   new->prev_clone = n;
1227   n->next_clone = new;
1228   if (new->next_clone)
1229     new->next_clone->prev_clone = new;
1230
1231   cgraph_call_node_duplication_hooks (n, new);
1232   return new;
1233 }
1234
1235 /* Return true if N is an master_clone, (see cgraph_master_clone).  */
1236
1237 bool
1238 cgraph_is_master_clone (struct cgraph_node *n)
1239 {
1240   return (n == cgraph_master_clone (n));
1241 }
1242
1243 struct cgraph_node *
1244 cgraph_master_clone (struct cgraph_node *n)
1245 {
1246   enum availability avail = cgraph_function_body_availability (n);
1247
1248   if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
1249     return NULL;
1250
1251   if (!n->master_clone)
1252     n->master_clone = cgraph_node (n->decl);
1253
1254   return n->master_clone;
1255 }
1256
1257 /* NODE is no longer nested function; update cgraph accordingly.  */
1258 void
1259 cgraph_unnest_node (struct cgraph_node *node)
1260 {
1261   struct cgraph_node **node2 = &node->origin->nested;
1262   gcc_assert (node->origin);
1263
1264   while (*node2 != node)
1265     node2 = &(*node2)->next_nested;
1266   *node2 = node->next_nested;
1267   node->origin = NULL;
1268 }
1269
1270 /* Return function availability.  See cgraph.h for description of individual
1271    return values.  */
1272 enum availability
1273 cgraph_function_body_availability (struct cgraph_node *node)
1274 {
1275   enum availability avail;
1276   gcc_assert (cgraph_function_flags_ready);
1277   if (!node->analyzed)
1278     avail = AVAIL_NOT_AVAILABLE;
1279   else if (node->local.local)
1280     avail = AVAIL_LOCAL;
1281   else if (node->local.externally_visible)
1282     avail = AVAIL_AVAILABLE;
1283
1284   /* If the function can be overwritten, return OVERWRITABLE.  Take
1285      care at least of two notable extensions - the COMDAT functions
1286      used to share template instantiations in C++ (this is symmetric
1287      to code cp_cannot_inline_tree_fn and probably shall be shared and
1288      the inlinability hooks completely eliminated).
1289
1290      ??? Does the C++ one definition rule allow us to always return
1291      AVAIL_AVAILABLE here?  That would be good reason to preserve this
1292      hook Similarly deal with extern inline functions - this is again
1293      necessary to get C++ shared functions having keyed templates
1294      right and in the C extension documentation we probably should
1295      document the requirement of both versions of function (extern
1296      inline and offline) having same side effect characteristics as
1297      good optimization is what this optimization is about.  */
1298
1299   else if (!(*targetm.binds_local_p) (node->decl)
1300            && !DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl))
1301     avail = AVAIL_OVERWRITABLE;
1302   else avail = AVAIL_AVAILABLE;
1303
1304   return avail;
1305 }
1306
1307 /* Add the function FNDECL to the call graph.
1308    Unlike cgraph_finalize_function, this function is intended to be used
1309    by middle end and allows insertion of new function at arbitrary point
1310    of compilation.  The function can be either in high, low or SSA form
1311    GIMPLE.
1312
1313    The function is assumed to be reachable and have address taken (so no
1314    API breaking optimizations are performed on it).  
1315
1316    Main work done by this function is to enqueue the function for later
1317    processing to avoid need the passes to be re-entrant.  */
1318
1319 void
1320 cgraph_add_new_function (tree fndecl, bool lowered)
1321 {
1322   struct cgraph_node *node;
1323   switch (cgraph_state)
1324     {
1325       case CGRAPH_STATE_CONSTRUCTION:
1326         /* Just enqueue function to be processed at nearest occurrence.  */
1327         node = cgraph_node (fndecl);
1328         node->next_needed = cgraph_new_nodes;
1329         if (lowered)
1330           node->lowered = true;
1331         cgraph_new_nodes = node;
1332         break;
1333
1334       case CGRAPH_STATE_IPA:
1335       case CGRAPH_STATE_IPA_SSA:
1336       case CGRAPH_STATE_EXPANSION:
1337         /* Bring the function into finalized state and enqueue for later
1338            analyzing and compilation.  */
1339         node = cgraph_node (fndecl);
1340         node->local.local = false;
1341         node->local.finalized = true;
1342         node->reachable = node->needed = true;
1343         if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
1344           {
1345             push_cfun (DECL_STRUCT_FUNCTION (fndecl));
1346             current_function_decl = fndecl;
1347             gimple_register_cfg_hooks ();
1348             tree_lowering_passes (fndecl);
1349             bitmap_obstack_initialize (NULL);
1350             if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
1351               execute_pass_list (pass_early_local_passes.pass.sub);
1352             bitmap_obstack_release (NULL);
1353             pop_cfun ();
1354             current_function_decl = NULL;
1355
1356             lowered = true;
1357           }
1358         if (lowered)
1359           node->lowered = true;
1360         node->next_needed = cgraph_new_nodes;
1361         cgraph_new_nodes = node;
1362         break;
1363
1364       case CGRAPH_STATE_FINISHED:
1365         /* At the very end of compilation we have to do all the work up
1366            to expansion.  */
1367         push_cfun (DECL_STRUCT_FUNCTION (fndecl));
1368         current_function_decl = fndecl;
1369         gimple_register_cfg_hooks ();
1370         if (!lowered)
1371           tree_lowering_passes (fndecl);
1372         bitmap_obstack_initialize (NULL);
1373         if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
1374           execute_pass_list (pass_early_local_passes.pass.sub);
1375         bitmap_obstack_release (NULL);
1376         tree_rest_of_compilation (fndecl);
1377         pop_cfun ();
1378         current_function_decl = NULL;
1379         break;
1380     }
1381 }
1382
1383 #include "gt-cgraph.h"