OSDN Git Service

e20c8d5336e9bcae9a86795bead8d87135b24128
[pf3gnuchains/gcc-fork.git] / gcc / cgraph.c
1 /* Callgraph handling code.
2    Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3    Contributed by Jan Hubicka
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.  */
21
22 /*  This file contains basic routines manipulating call graph and variable pool
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 (in
32     contrast to tree DECL nodes where we can have multiple nodes for each
33     function).
34
35     The mapping from declarations to call-graph nodes is done using hash table
36     based on DECL_ASSEMBLER_NAME, so it is essential for assembler name to
37     not change once the declaration is inserted into the call-graph.
38     The call-graph nodes are created lazily using cgraph_node function when
39     called for unknown declaration.
40
41     When built, there is one edge for each direct call.  It is possible that
42     the reference will be later optimized out.  The call-graph is built
43     conservatively in order to make conservative data flow analysis possible.
44
45     The callgraph at the moment does not represent indirect calls or calls
46     from other compilation unit.  Flag NEEDED is set for each node that may
47     be accessed in such an invisible way and it shall be considered an
48     entry point to the callgraph.
49
50     Intraprocedural information:
51
52       Callgraph is place to store data needed for intraprocedural optimization.
53       All data structures are divided into three components: local_info that
54       is produced while analyzing the function, global_info that is result
55       of global walking of the callgraph on the end of compilation and
56       rtl_info used by RTL backend to propagate data from already compiled
57       functions to their callers.
58
59     Inlining plans:
60
61       The function inlining information is decided in advance and maintained
62       in the callgraph as so called inline plan.
63       For each inlined call, the callee's node is cloned to represent the
64       new function copy produced by inliner.
65       Each inlined call gets a unique corresponding clone node of the callee
66       and the data structure is updated while inlining is performed, so
67       the clones are eliminated and their callee edges redirected to the
68       caller.
69
70       Each edge has "inline_failed" field.  When the field is set to NULL,
71       the call will be inlined.  When it is non-NULL it contains a reason
72       why inlining wasn't performed.
73
74
75 The varpool data structure:
76
77     Varpool is used to maintain variables in similar manner as call-graph
78     is used for functions.  Most of the API is symmetric replacing cgraph
79     function prefix by cgraph_varpool  */
80
81
82 #include "config.h"
83 #include "system.h"
84 #include "coretypes.h"
85 #include "tm.h"
86 #include "tree.h"
87 #include "tree-inline.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "toplev.h"
91 #include "flags.h"
92 #include "ggc.h"
93 #include "debug.h"
94 #include "target.h"
95 #include "basic-block.h"
96 #include "cgraph.h"
97 #include "varray.h"
98 #include "output.h"
99 #include "intl.h"
100 #include "tree-gimple.h"
101 #include "tree-dump.h"
102
103 static void cgraph_node_remove_callers (struct cgraph_node *node);
104 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
105 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
106
107 /* Hash table used to convert declarations into nodes.  */
108 static GTY((param_is (struct cgraph_node))) htab_t cgraph_hash;
109
110 /* The linked list of cgraph nodes.  */
111 struct cgraph_node *cgraph_nodes;
112
113 /* Queue of cgraph nodes scheduled to be lowered.  */
114 struct cgraph_node *cgraph_nodes_queue;
115
116 /* Queue of cgraph nodes scheduled to be expanded.  This is a
117    secondary queue used during optimization to accommodate passes that
118    may generate new functions that need to be optimized and expanded.  */
119 struct cgraph_node *cgraph_expand_queue;
120
121 /* Number of nodes in existence.  */
122 int cgraph_n_nodes;
123
124 /* Maximal uid used in cgraph nodes.  */
125 int cgraph_max_uid;
126
127 /* Set when whole unit has been analyzed so we can access global info.  */
128 bool cgraph_global_info_ready = false;
129
130 /* Set when the cgraph is fully build and the basic flags are computed.  */
131 bool cgraph_function_flags_ready = false;
132
133 /* Hash table used to convert declarations into nodes.  */
134 static GTY((param_is (struct cgraph_varpool_node))) htab_t cgraph_varpool_hash;
135
136 /* Queue of cgraph nodes scheduled to be lowered and output.  */
137 struct cgraph_varpool_node *cgraph_varpool_nodes_queue, *cgraph_varpool_first_unanalyzed_node;
138
139 /* The linked list of cgraph varpool nodes.  */
140 struct cgraph_varpool_node *cgraph_varpool_nodes;
141
142 /* End of the varpool queue.  Needs to be QTYed to work with PCH.  */
143 static GTY(()) struct cgraph_varpool_node *cgraph_varpool_last_needed_node;
144
145 /* Linked list of cgraph asm nodes.  */
146 struct cgraph_asm_node *cgraph_asm_nodes;
147
148 /* Last node in cgraph_asm_nodes.  */
149 static GTY(()) struct cgraph_asm_node *cgraph_asm_last_node;
150
151 /* The order index of the next cgraph node to be created.  This is
152    used so that we can sort the cgraph nodes in order by when we saw
153    them, to support -fno-toplevel-reorder.  */
154 int cgraph_order;
155
156 static hashval_t hash_node (const void *);
157 static int eq_node (const void *, const void *);
158
159 /* Returns a hash code for P.  */
160
161 static hashval_t
162 hash_node (const void *p)
163 {
164   const struct cgraph_node *n = (const struct cgraph_node *) p;
165   return (hashval_t) DECL_UID (n->decl);
166 }
167
168 /* Returns nonzero if P1 and P2 are equal.  */
169
170 static int
171 eq_node (const void *p1, const void *p2)
172 {
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);
176 }
177
178 /* Allocate new callgraph node and insert it into basic data structures.  */
179 static struct cgraph_node *
180 cgraph_create_node (void)
181 {
182   struct cgraph_node *node;
183
184   node = GGC_CNEW (struct cgraph_node);
185   node->next = cgraph_nodes;
186   node->uid = cgraph_max_uid++;
187   node->order = cgraph_order++;
188   if (cgraph_nodes)
189     cgraph_nodes->previous = node;
190   node->previous = NULL;
191   node->global.estimated_growth = INT_MIN;
192   cgraph_nodes = node;
193   cgraph_n_nodes++;
194   return node;
195 }
196
197 /* Return cgraph node assigned to DECL.  Create new one when needed.  */
198 struct cgraph_node *
199 cgraph_node (tree decl)
200 {
201   struct cgraph_node key, *node, **slot;
202
203   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
204
205   if (!cgraph_hash)
206     cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
207
208   key.decl = decl;
209
210   slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
211
212   if (*slot)
213     {
214       node = *slot;
215       if (!node->master_clone)
216         node->master_clone = node;
217       return node;
218     }
219
220   node = cgraph_create_node ();
221   node->decl = decl;
222   *slot = node;
223   if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
224     {
225       node->origin = cgraph_node (DECL_CONTEXT (decl));
226       node->next_nested = node->origin->nested;
227       node->origin->nested = node;
228       node->master_clone = node;
229     }
230   return node;
231 }
232
233 /* Insert already constructed node into hashtable.  */
234
235 void
236 cgraph_insert_node_to_hashtable (struct cgraph_node *node)
237 {
238   struct cgraph_node **slot;
239
240   slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, node, INSERT);
241
242   gcc_assert (!*slot);
243   *slot = node;
244 }
245
246 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL.  */
247
248 static bool
249 decl_assembler_name_equal (tree decl, tree asmname)
250 {
251   tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
252
253   if (decl_asmname == asmname)
254     return true;
255
256   /* If the target assembler name was set by the user, things are trickier.
257      We have a leading '*' to begin with.  After that, it's arguable what
258      is the correct thing to do with -fleading-underscore.  Arguably, we've
259      historically been doing the wrong thing in assemble_alias by always
260      printing the leading underscore.  Since we're not changing that, make
261      sure user_label_prefix follows the '*' before matching.  */
262   if (IDENTIFIER_POINTER (decl_asmname)[0] == '*')
263     {
264       const char *decl_str = IDENTIFIER_POINTER (decl_asmname) + 1;
265       size_t ulp_len = strlen (user_label_prefix);
266
267       if (ulp_len == 0)
268         ;
269       else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
270         decl_str += ulp_len;
271       else
272         return false;
273
274       return strcmp (decl_str, IDENTIFIER_POINTER (asmname)) == 0;
275     }
276
277   return false;
278 }
279
280
281 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
282    Return NULL if there's no such node.  */
283
284 struct cgraph_node *
285 cgraph_node_for_asm (tree asmname)
286 {
287   struct cgraph_node *node;
288
289   for (node = cgraph_nodes; node ; node = node->next)
290     if (decl_assembler_name_equal (node->decl, asmname))
291       return node;
292
293   return NULL;
294 }
295
296 /* Return callgraph edge representing CALL_EXPR statement.  */
297 struct cgraph_edge *
298 cgraph_edge (struct cgraph_node *node, tree call_stmt)
299 {
300   struct cgraph_edge *e;
301
302   /* This loop may turn out to be performance problem.  In such case adding
303      hashtables into call nodes with very many edges is probably best
304      solution.  It is not good idea to add pointer into CALL_EXPR itself
305      because we want to make possible having multiple cgraph nodes representing
306      different clones of the same body before the body is actually cloned.  */
307   for (e = node->callees; e; e= e->next_callee)
308     if (e->call_stmt == call_stmt)
309       break;
310   return e;
311 }
312
313 /* Create edge from CALLER to CALLEE in the cgraph.  */
314
315 struct cgraph_edge *
316 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
317                     tree call_stmt, gcov_type count, int nest)
318 {
319   struct cgraph_edge *edge = GGC_NEW (struct cgraph_edge);
320 #ifdef ENABLE_CHECKING
321   struct cgraph_edge *e;
322
323   for (e = caller->callees; e; e = e->next_callee)
324     gcc_assert (e->call_stmt != call_stmt);
325 #endif
326
327   gcc_assert (get_call_expr_in (call_stmt));
328
329   if (!DECL_SAVED_TREE (callee->decl))
330     edge->inline_failed = N_("function body not available");
331   else if (callee->local.redefined_extern_inline)
332     edge->inline_failed = N_("redefined extern inline functions are not "
333                              "considered for inlining");
334   else if (callee->local.inlinable)
335     edge->inline_failed = N_("function not considered for inlining");
336   else
337     edge->inline_failed = N_("function not inlinable");
338
339   edge->aux = NULL;
340
341   edge->caller = caller;
342   edge->callee = callee;
343   edge->call_stmt = call_stmt;
344   edge->prev_caller = NULL;
345   edge->next_caller = callee->callers;
346   if (callee->callers)
347     callee->callers->prev_caller = edge;
348   edge->prev_callee = NULL;
349   edge->next_callee = caller->callees;
350   if (caller->callees)
351     caller->callees->prev_callee = edge;
352   caller->callees = edge;
353   callee->callers = edge;
354   edge->count = count;
355   edge->loop_nest = nest;
356   return edge;
357 }
358
359 /* Remove the edge E from the list of the callers of the callee.  */
360
361 static inline void
362 cgraph_edge_remove_callee (struct cgraph_edge *e)
363 {
364   if (e->prev_caller)
365     e->prev_caller->next_caller = e->next_caller;
366   if (e->next_caller)
367     e->next_caller->prev_caller = e->prev_caller;
368   if (!e->prev_caller)
369     e->callee->callers = e->next_caller;
370 }
371
372 /* Remove the edge E from the list of the callees of the caller.  */
373
374 static inline void
375 cgraph_edge_remove_caller (struct cgraph_edge *e)
376 {
377   if (e->prev_callee)
378     e->prev_callee->next_callee = e->next_callee;
379   if (e->next_callee)
380     e->next_callee->prev_callee = e->prev_callee;
381   if (!e->prev_callee)
382     e->caller->callees = e->next_callee;
383 }
384
385 /* Remove the edge E in the cgraph.  */
386
387 void
388 cgraph_remove_edge (struct cgraph_edge *e)
389 {
390   /* Remove from callers list of the callee.  */
391   cgraph_edge_remove_callee (e);
392
393   /* Remove from callees list of the callers.  */
394   cgraph_edge_remove_caller (e);
395 }
396
397 /* Redirect callee of E to N.  The function does not update underlying
398    call expression.  */
399
400 void
401 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
402 {
403   /* Remove from callers list of the current callee.  */
404   cgraph_edge_remove_callee (e);
405
406   /* Insert to callers list of the new callee.  */
407   e->prev_caller = NULL;
408   if (n->callers)
409     n->callers->prev_caller = e;
410   e->next_caller = n->callers;
411   n->callers = e;
412   e->callee = n;
413 }
414
415 /* Remove all callees from the node.  */
416
417 void
418 cgraph_node_remove_callees (struct cgraph_node *node)
419 {
420   struct cgraph_edge *e;
421
422   /* It is sufficient to remove the edges from the lists of callers of
423      the callees.  The callee list of the node can be zapped with one
424      assignment.  */
425   for (e = node->callees; e; e = e->next_callee)
426     cgraph_edge_remove_callee (e);
427   node->callees = NULL;
428 }
429
430 /* Remove all callers from the node.  */
431
432 static void
433 cgraph_node_remove_callers (struct cgraph_node *node)
434 {
435   struct cgraph_edge *e;
436
437   /* It is sufficient to remove the edges from the lists of callees of
438      the callers.  The caller list of the node can be zapped with one
439      assignment.  */
440   for (e = node->callers; e; e = e->next_caller)
441     cgraph_edge_remove_caller (e);
442   node->callers = NULL;
443 }
444
445 /* Remove the node from cgraph.  */
446
447 void
448 cgraph_remove_node (struct cgraph_node *node)
449 {
450   void **slot;
451   bool kill_body = false;
452
453   cgraph_node_remove_callers (node);
454   cgraph_node_remove_callees (node);
455   while (node->nested)
456     cgraph_remove_node (node->nested);
457   if (node->origin)
458     {
459       struct cgraph_node **node2 = &node->origin->nested;
460
461       while (*node2 != node)
462         node2 = &(*node2)->next_nested;
463       *node2 = node->next_nested;
464     }
465   if (node->previous)
466     node->previous->next = node->next;
467   else
468     cgraph_nodes = node->next;
469   if (node->next)
470     node->next->previous = node->previous;
471   slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
472   if (*slot == node)
473     {
474       if (node->next_clone)
475       {
476         struct cgraph_node *new_node = node->next_clone;
477         struct cgraph_node *n;
478
479         /* Make the next clone be the master clone */
480         for (n = new_node; n; n = n->next_clone)
481           n->master_clone = new_node;
482
483         *slot = new_node;
484         node->next_clone->prev_clone = NULL;
485       }
486       else
487         {
488           htab_clear_slot (cgraph_hash, slot);
489           kill_body = true;
490         }
491     }
492   else
493     {
494       node->prev_clone->next_clone = node->next_clone;
495       if (node->next_clone)
496         node->next_clone->prev_clone = node->prev_clone;
497     }
498
499   /* While all the clones are removed after being proceeded, the function
500      itself is kept in the cgraph even after it is compiled.  Check whether
501      we are done with this body and reclaim it proactively if this is the case.
502      */
503   if (!kill_body && *slot)
504     {
505       struct cgraph_node *n = (struct cgraph_node *) *slot;
506       if (!n->next_clone && !n->global.inlined_to
507           && (cgraph_global_info_ready
508               && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl))))
509         kill_body = true;
510     }
511
512   if (kill_body && flag_unit_at_a_time)
513     {
514       DECL_SAVED_TREE (node->decl) = NULL;
515       DECL_STRUCT_FUNCTION (node->decl) = NULL;
516       DECL_INITIAL (node->decl) = error_mark_node;
517     }
518   cgraph_n_nodes--;
519   /* Do not free the structure itself so the walk over chain can continue.  */
520 }
521
522 /* Notify finalize_compilation_unit that given node is reachable.  */
523
524 void
525 cgraph_mark_reachable_node (struct cgraph_node *node)
526 {
527   if (!node->reachable && node->local.finalized)
528     {
529       notice_global_symbol (node->decl);
530       node->reachable = 1;
531       gcc_assert (!cgraph_global_info_ready);
532
533       node->next_needed = cgraph_nodes_queue;
534       cgraph_nodes_queue = node;
535     }
536 }
537
538 /* Likewise indicate that a node is needed, i.e. reachable via some
539    external means.  */
540
541 void
542 cgraph_mark_needed_node (struct cgraph_node *node)
543 {
544   node->needed = 1;
545   cgraph_mark_reachable_node (node);
546 }
547
548 /* Return local info for the compiled function.  */
549
550 struct cgraph_local_info *
551 cgraph_local_info (tree decl)
552 {
553   struct cgraph_node *node;
554
555   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
556   node = cgraph_node (decl);
557   return &node->local;
558 }
559
560 /* Return local info for the compiled function.  */
561
562 struct cgraph_global_info *
563 cgraph_global_info (tree decl)
564 {
565   struct cgraph_node *node;
566
567   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
568   node = cgraph_node (decl);
569   return &node->global;
570 }
571
572 /* Return local info for the compiled function.  */
573
574 struct cgraph_rtl_info *
575 cgraph_rtl_info (tree decl)
576 {
577   struct cgraph_node *node;
578
579   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
580   node = cgraph_node (decl);
581   if (decl != current_function_decl
582       && !TREE_ASM_WRITTEN (node->decl))
583     return NULL;
584   return &node->rtl;
585 }
586
587 /* Return name of the node used in debug output.  */
588 const char *
589 cgraph_node_name (struct cgraph_node *node)
590 {
591   return lang_hooks.decl_printable_name (node->decl, 2);
592 }
593
594 /* Return name of the node used in debug output.  */
595 static const char *
596 cgraph_varpool_node_name (struct cgraph_varpool_node *node)
597 {
598   return lang_hooks.decl_printable_name (node->decl, 2);
599 }
600
601 /* Names used to print out the availability enum.  */
602 static const char * const availability_names[] =
603   {"unset", "not_available", "overwrittable", "available", "local"};
604
605 /* Dump given cgraph node.  */
606 void
607 dump_cgraph_node (FILE *f, struct cgraph_node *node)
608 {
609   struct cgraph_edge *edge;
610   fprintf (f, "%s/%i:", cgraph_node_name (node), node->uid);
611   if (node->global.inlined_to)
612     fprintf (f, " (inline copy in %s/%i)",
613              cgraph_node_name (node->global.inlined_to),
614              node->global.inlined_to->uid);
615   if (cgraph_function_flags_ready)
616     fprintf (f, " availability:%s",
617              availability_names [cgraph_function_body_availability (node)]);
618   if (node->master_clone && node->master_clone->uid != node->uid)
619     fprintf (f, "(%i)", node->master_clone->uid);
620   if (node->count)
621     fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
622              (HOST_WIDEST_INT)node->count);
623   if (node->local.self_insns)
624     fprintf (f, " %i insns", node->local.self_insns);
625   if (node->global.insns && node->global.insns != node->local.self_insns)
626     fprintf (f, " (%i after inlining)", node->global.insns);
627   if (node->origin)
628     fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
629   if (node->needed)
630     fprintf (f, " needed");
631   else if (node->reachable)
632     fprintf (f, " reachable");
633   if (DECL_SAVED_TREE (node->decl))
634     fprintf (f, " tree");
635   if (node->output)
636     fprintf (f, " output");
637   if (node->local.local)
638     fprintf (f, " local");
639   if (node->local.externally_visible)
640     fprintf (f, " externally_visible");
641   if (node->local.finalized)
642     fprintf (f, " finalized");
643   if (node->local.disregard_inline_limits)
644     fprintf (f, " always_inline");
645   else if (node->local.inlinable)
646     fprintf (f, " inlinable");
647   if (node->local.redefined_extern_inline)
648     fprintf (f, " redefined_extern_inline");
649   if (TREE_ASM_WRITTEN (node->decl))
650     fprintf (f, " asm_written");
651
652   fprintf (f, "\n  called by: ");
653   for (edge = node->callers; edge; edge = edge->next_caller)
654     {
655       fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
656                edge->caller->uid);
657       if (edge->count)
658         fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
659                  (HOST_WIDEST_INT)edge->count);
660       if (!edge->inline_failed)
661         fprintf(f, "(inlined) ");
662     }
663
664   fprintf (f, "\n  calls: ");
665   for (edge = node->callees; edge; edge = edge->next_callee)
666     {
667       fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
668                edge->callee->uid);
669       if (!edge->inline_failed)
670         fprintf(f, "(inlined) ");
671       if (edge->count)
672         fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
673                  (HOST_WIDEST_INT)edge->count);
674       if (edge->loop_nest)
675         fprintf (f, "(nested in %i loops) ", edge->loop_nest);
676     }
677   fprintf (f, "\n");
678 }
679
680 /* Dump the callgraph.  */
681
682 void
683 dump_cgraph (FILE *f)
684 {
685   struct cgraph_node *node;
686
687   fprintf (f, "callgraph:\n\n");
688   for (node = cgraph_nodes; node; node = node->next)
689     dump_cgraph_node (f, node);
690 }
691
692 /* Dump given cgraph node.  */
693 void
694 dump_cgraph_varpool_node (FILE *f, struct cgraph_varpool_node *node)
695 {
696   fprintf (f, "%s:", cgraph_varpool_node_name (node));
697   fprintf (f, " availability:%s", availability_names [cgraph_variable_initializer_availability (node)]);
698   if (DECL_INITIAL (node->decl))
699     fprintf (f, " initialized");
700   if (node->needed)
701     fprintf (f, " needed");
702   if (node->analyzed)
703     fprintf (f, " analyzed");
704   if (node->finalized)
705     fprintf (f, " finalized");
706   if (node->output)
707     fprintf (f, " output");
708   if (node->externally_visible)
709     fprintf (f, " externally_visible");
710   fprintf (f, "\n");
711 }
712
713 /* Dump the callgraph.  */
714
715 void
716 dump_varpool (FILE *f)
717 {
718   struct cgraph_varpool_node *node;
719
720   fprintf (f, "variable pool:\n\n");
721   for (node = cgraph_varpool_nodes; node; node = node->next_needed)
722     dump_cgraph_varpool_node (f, node);
723 }
724
725 /* Returns a hash code for P.  */
726
727 static hashval_t
728 hash_varpool_node (const void *p)
729 {
730   const struct cgraph_varpool_node *n = (const struct cgraph_varpool_node *) p;
731   return (hashval_t) DECL_UID (n->decl);
732 }
733
734 /* Returns nonzero if P1 and P2 are equal.  */
735
736 static int
737 eq_varpool_node (const void *p1, const void *p2)
738 {
739   const struct cgraph_varpool_node *n1 =
740     (const struct cgraph_varpool_node *) p1;
741   const struct cgraph_varpool_node *n2 =
742     (const struct cgraph_varpool_node *) p2;
743   return DECL_UID (n1->decl) == DECL_UID (n2->decl);
744 }
745
746 /* Return cgraph_varpool node assigned to DECL.  Create new one when needed.  */
747 struct cgraph_varpool_node *
748 cgraph_varpool_node (tree decl)
749 {
750   struct cgraph_varpool_node key, *node, **slot;
751
752   gcc_assert (DECL_P (decl) && TREE_CODE (decl) != FUNCTION_DECL);
753
754   if (!cgraph_varpool_hash)
755     cgraph_varpool_hash = htab_create_ggc (10, hash_varpool_node,
756                                            eq_varpool_node, NULL);
757   key.decl = decl;
758   slot = (struct cgraph_varpool_node **)
759     htab_find_slot (cgraph_varpool_hash, &key, INSERT);
760   if (*slot)
761     return *slot;
762   node = GGC_CNEW (struct cgraph_varpool_node);
763   node->decl = decl;
764   node->order = cgraph_order++;
765   node->next = cgraph_varpool_nodes;
766   cgraph_varpool_nodes = node;
767   *slot = node;
768   return node;
769 }
770
771 struct cgraph_varpool_node *
772 cgraph_varpool_node_for_asm (tree asmname)
773 {
774   struct cgraph_varpool_node *node;
775
776   for (node = cgraph_varpool_nodes; node ; node = node->next)
777     if (decl_assembler_name_equal (node->decl, asmname))
778       return node;
779
780   return NULL;
781 }
782
783 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables.  */
784 void
785 change_decl_assembler_name (tree decl, tree name)
786 {
787   if (!DECL_ASSEMBLER_NAME_SET_P (decl))
788     {
789       SET_DECL_ASSEMBLER_NAME (decl, name);
790       return;
791     }
792   if (name == DECL_ASSEMBLER_NAME (decl))
793     return;
794
795   if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
796       && DECL_RTL_SET_P (decl))
797     warning (0, "%D renamed after being referenced in assembly", decl);
798
799   SET_DECL_ASSEMBLER_NAME (decl, name);
800 }
801
802 /* Helper function for finalization code - add node into lists so it will
803    be analyzed and compiled.  */
804 void
805 cgraph_varpool_enqueue_needed_node (struct cgraph_varpool_node *node)
806 {
807   if (cgraph_varpool_last_needed_node)
808     cgraph_varpool_last_needed_node->next_needed = node;
809   cgraph_varpool_last_needed_node = node;
810   node->next_needed = NULL;
811   if (!cgraph_varpool_nodes_queue)
812     cgraph_varpool_nodes_queue = node;
813   if (!cgraph_varpool_first_unanalyzed_node)
814     cgraph_varpool_first_unanalyzed_node = node;
815   notice_global_symbol (node->decl);
816 }
817
818 /* Reset the queue of needed nodes.  */
819 void
820 cgraph_varpool_reset_queue (void)
821 {
822   cgraph_varpool_last_needed_node = NULL;
823   cgraph_varpool_nodes_queue = NULL;
824   cgraph_varpool_first_unanalyzed_node = NULL;
825 }
826
827 /* Notify finalize_compilation_unit that given node is reachable
828    or needed.  */
829 void
830 cgraph_varpool_mark_needed_node (struct cgraph_varpool_node *node)
831 {
832   if (!node->needed && node->finalized)
833     cgraph_varpool_enqueue_needed_node (node);
834   node->needed = 1;
835 }
836
837 /* Determine if variable DECL is needed.  That is, visible to something
838    either outside this translation unit, something magic in the system
839    configury, or (if not doing unit-at-a-time) to something we haven't
840    seen yet.  */
841
842 bool
843 decide_is_variable_needed (struct cgraph_varpool_node *node, tree decl)
844 {
845   /* If the user told us it is used, then it must be so.  */
846   if (node->externally_visible)
847     return true;
848   if (!flag_unit_at_a_time
849       && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
850     return true;
851
852   /* ??? If the assembler name is set by hand, it is possible to assemble
853      the name later after finalizing the function and the fact is noticed
854      in assemble_name then.  This is arguably a bug.  */
855   if (DECL_ASSEMBLER_NAME_SET_P (decl)
856       && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
857     return true;
858
859   /* If we decided it was needed before, but at the time we didn't have
860      the definition available, then it's still needed.  */
861   if (node->needed)
862     return true;
863
864   /* Externally visible variables must be output.  The exception is
865      COMDAT variables that must be output only when they are needed.  */
866   if (TREE_PUBLIC (decl) && !flag_whole_program && !DECL_COMDAT (decl)
867       && !DECL_EXTERNAL (decl))
868     return true;
869
870   /* When not reordering top level variables, we have to assume that
871      we are going to keep everything.  */
872   if (flag_unit_at_a_time && flag_toplevel_reorder)
873     return false;
874
875   /* We want to emit COMDAT variables only when absolutely necessary.  */
876   if (DECL_COMDAT (decl))
877     return false;
878   return true;
879 }
880
881 void
882 cgraph_varpool_finalize_decl (tree decl)
883 {
884   struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
885
886   /* The first declaration of a variable that comes through this function
887      decides whether it is global (in C, has external linkage)
888      or local (in C, has internal linkage).  So do nothing more
889      if this function has already run.  */
890   if (node->finalized)
891     {
892       if (cgraph_global_info_ready || (!flag_unit_at_a_time && !flag_openmp))
893         cgraph_varpool_assemble_pending_decls ();
894       return;
895     }
896   if (node->needed)
897     cgraph_varpool_enqueue_needed_node (node);
898   node->finalized = true;
899
900   if (decide_is_variable_needed (node, decl))
901     cgraph_varpool_mark_needed_node (node);
902   /* Since we reclaim unreachable nodes at the end of every language
903      level unit, we need to be conservative about possible entry points
904      there.  */
905   else if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
906     cgraph_varpool_mark_needed_node (node);
907   if (cgraph_global_info_ready || (!flag_unit_at_a_time && !flag_openmp))
908     cgraph_varpool_assemble_pending_decls ();
909 }
910
911 /* Add a top-level asm statement to the list.  */
912
913 struct cgraph_asm_node *
914 cgraph_add_asm_node (tree asm_str)
915 {
916   struct cgraph_asm_node *node;
917
918   node = GGC_CNEW (struct cgraph_asm_node);
919   node->asm_str = asm_str;
920   node->order = cgraph_order++;
921   node->next = NULL;
922   if (cgraph_asm_nodes == NULL)
923     cgraph_asm_nodes = node;
924   else
925     cgraph_asm_last_node->next = node;
926   cgraph_asm_last_node = node;
927   return node;
928 }
929
930 /* Return true when the DECL can possibly be inlined.  */
931 bool
932 cgraph_function_possibly_inlined_p (tree decl)
933 {
934   if (!cgraph_global_info_ready)
935     return (DECL_INLINE (decl) && !flag_really_no_inline);
936   return DECL_POSSIBLY_INLINED (decl);
937 }
938
939 /* Create clone of E in the node N represented by CALL_EXPR the callgraph.  */
940 struct cgraph_edge *
941 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
942                    tree call_stmt, gcov_type count_scale, int loop_nest,
943                    bool update_original)
944 {
945   struct cgraph_edge *new;
946
947   new = cgraph_create_edge (n, e->callee, call_stmt,
948                             e->count * count_scale / REG_BR_PROB_BASE,
949                             e->loop_nest + loop_nest);
950
951   new->inline_failed = e->inline_failed;
952   if (update_original)
953     {
954       e->count -= new->count;
955       if (e->count < 0)
956         e->count = 0;
957     }
958   return new;
959 }
960
961 /* Create node representing clone of N executed COUNT times.  Decrease
962    the execution counts from original node too.
963
964    When UPDATE_ORIGINAL is true, the counts are subtracted from the original
965    function's profile to reflect the fact that part of execution is handled
966    by node.  */
967 struct cgraph_node *
968 cgraph_clone_node (struct cgraph_node *n, gcov_type count, int loop_nest,
969                    bool update_original)
970 {
971   struct cgraph_node *new = cgraph_create_node ();
972   struct cgraph_edge *e;
973   gcov_type count_scale;
974
975   new->decl = n->decl;
976   new->origin = n->origin;
977   if (new->origin)
978     {
979       new->next_nested = new->origin->nested;
980       new->origin->nested = new;
981     }
982   new->analyzed = n->analyzed;
983   new->local = n->local;
984   new->global = n->global;
985   new->rtl = n->rtl;
986   new->master_clone = n->master_clone;
987   new->count = count;
988   if (n->count)
989     count_scale = new->count * REG_BR_PROB_BASE / n->count;
990   else
991     count_scale = 0;
992   if (update_original)
993     {
994       n->count -= count;
995       if (n->count < 0)
996         n->count = 0;
997     }
998
999   for (e = n->callees;e; e=e->next_callee)
1000     cgraph_clone_edge (e, new, e->call_stmt, count_scale, loop_nest,
1001                        update_original);
1002
1003   new->next_clone = n->next_clone;
1004   new->prev_clone = n;
1005   n->next_clone = new;
1006   if (new->next_clone)
1007     new->next_clone->prev_clone = new;
1008
1009   return new;
1010 }
1011
1012 /* Return true if N is an master_clone, (see cgraph_master_clone).  */
1013
1014 bool
1015 cgraph_is_master_clone (struct cgraph_node *n)
1016 {
1017   return (n == cgraph_master_clone (n));
1018 }
1019
1020 struct cgraph_node *
1021 cgraph_master_clone (struct cgraph_node *n)
1022 {
1023   enum availability avail = cgraph_function_body_availability (n);
1024
1025   if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
1026     return NULL;
1027
1028   if (!n->master_clone)
1029     n->master_clone = cgraph_node (n->decl);
1030
1031   return n->master_clone;
1032 }
1033
1034 /* NODE is no longer nested function; update cgraph accordingly.  */
1035 void
1036 cgraph_unnest_node (struct cgraph_node *node)
1037 {
1038   struct cgraph_node **node2 = &node->origin->nested;
1039   gcc_assert (node->origin);
1040
1041   while (*node2 != node)
1042     node2 = &(*node2)->next_nested;
1043   *node2 = node->next_nested;
1044   node->origin = NULL;
1045 }
1046
1047 /* Return function availability.  See cgraph.h for description of individual
1048    return values.  */
1049 enum availability
1050 cgraph_function_body_availability (struct cgraph_node *node)
1051 {
1052   enum availability avail;
1053   gcc_assert (cgraph_function_flags_ready);
1054   if (!node->analyzed)
1055     avail = AVAIL_NOT_AVAILABLE;
1056   else if (node->local.local)
1057     avail = AVAIL_LOCAL;
1058   else if (node->local.externally_visible)
1059     avail = AVAIL_AVAILABLE;
1060
1061   /* If the function can be overwritten, return OVERWRITABLE.  Take
1062      care at least of two notable extensions - the COMDAT functions
1063      used to share template instantiations in C++ (this is symmetric
1064      to code cp_cannot_inline_tree_fn and probably shall be shared and
1065      the inlinability hooks completely eliminated).
1066
1067      ??? Does the C++ one definition rule allow us to always return
1068      AVAIL_AVAILABLE here?  That would be good reason to preserve this
1069      hook Similarly deal with extern inline functions - this is again
1070      necessary to get C++ shared functions having keyed templates
1071      right and in the C extension documentation we probably should
1072      document the requirement of both versions of function (extern
1073      inline and offline) having same side effect characteristics as
1074      good optimization is what this optimization is about.  */
1075
1076   else if (!(*targetm.binds_local_p) (node->decl)
1077            && !DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl))
1078     avail = AVAIL_OVERWRITABLE;
1079   else avail = AVAIL_AVAILABLE;
1080
1081   return avail;
1082 }
1083
1084 /* Return variable availability.  See cgraph.h for description of individual
1085    return values.  */
1086 enum availability
1087 cgraph_variable_initializer_availability (struct cgraph_varpool_node *node)
1088 {
1089   gcc_assert (cgraph_function_flags_ready);
1090   if (!node->finalized)
1091     return AVAIL_NOT_AVAILABLE;
1092   if (!TREE_PUBLIC (node->decl))
1093     return AVAIL_AVAILABLE;
1094   /* If the variable can be overwritten, return OVERWRITABLE.  Takes
1095      care of at least two notable extensions - the COMDAT variables
1096      used to share template instantiations in C++.  */
1097   if (!(*targetm.binds_local_p) (node->decl) && !DECL_COMDAT (node->decl))
1098     return AVAIL_OVERWRITABLE;
1099   return AVAIL_AVAILABLE;
1100 }
1101
1102
1103 /* Add the function FNDECL to the call graph.  FNDECL is assumed to be
1104    in low GIMPLE form and ready to be processed by cgraph_finalize_function.
1105
1106    When operating in unit-at-a-time, a new callgraph node is added to
1107    CGRAPH_EXPAND_QUEUE, which is processed after all the original
1108    functions in the call graph .
1109
1110    When not in unit-at-a-time, the new callgraph node is added to
1111    CGRAPH_NODES_QUEUE for cgraph_assemble_pending_functions to
1112    process.  */
1113
1114 void
1115 cgraph_add_new_function (tree fndecl)
1116 {
1117   struct cgraph_node *n = cgraph_node (fndecl);
1118   n->next_needed = cgraph_expand_queue;
1119   cgraph_expand_queue = n;
1120 }
1121
1122 #include "gt-cgraph.h"