OSDN Git Service

8d841b46e0d398d135929249b709496ddfd53e96
[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   /* Incremental inlining access removed nodes stored in the postorder list.
456      */
457   node->needed = node->reachable = false;
458   while (node->nested)
459     cgraph_remove_node (node->nested);
460   if (node->origin)
461     {
462       struct cgraph_node **node2 = &node->origin->nested;
463
464       while (*node2 != node)
465         node2 = &(*node2)->next_nested;
466       *node2 = node->next_nested;
467     }
468   if (node->previous)
469     node->previous->next = node->next;
470   else
471     cgraph_nodes = node->next;
472   if (node->next)
473     node->next->previous = node->previous;
474   node->next = NULL;
475   node->previous = NULL;
476   slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
477   if (*slot == node)
478     {
479       if (node->next_clone)
480       {
481         struct cgraph_node *new_node = node->next_clone;
482         struct cgraph_node *n;
483
484         /* Make the next clone be the master clone */
485         for (n = new_node; n; n = n->next_clone)
486           n->master_clone = new_node;
487
488         *slot = new_node;
489         node->next_clone->prev_clone = NULL;
490       }
491       else
492         {
493           htab_clear_slot (cgraph_hash, slot);
494           kill_body = true;
495         }
496     }
497   else
498     {
499       node->prev_clone->next_clone = node->next_clone;
500       if (node->next_clone)
501         node->next_clone->prev_clone = node->prev_clone;
502     }
503
504   /* While all the clones are removed after being proceeded, the function
505      itself is kept in the cgraph even after it is compiled.  Check whether
506      we are done with this body and reclaim it proactively if this is the case.
507      */
508   if (!kill_body && *slot)
509     {
510       struct cgraph_node *n = (struct cgraph_node *) *slot;
511       if (!n->next_clone && !n->global.inlined_to
512           && (cgraph_global_info_ready
513               && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl))))
514         kill_body = true;
515     }
516
517   if (kill_body && flag_unit_at_a_time)
518     {
519       DECL_SAVED_TREE (node->decl) = NULL;
520       DECL_STRUCT_FUNCTION (node->decl) = NULL;
521       DECL_INITIAL (node->decl) = error_mark_node;
522     }
523   node->decl = NULL;
524   cgraph_n_nodes--;
525   /* Do not free the structure itself so the walk over chain can continue.  */
526 }
527
528 /* Notify finalize_compilation_unit that given node is reachable.  */
529
530 void
531 cgraph_mark_reachable_node (struct cgraph_node *node)
532 {
533   if (!node->reachable && node->local.finalized)
534     {
535       notice_global_symbol (node->decl);
536       node->reachable = 1;
537       gcc_assert (!cgraph_global_info_ready);
538
539       node->next_needed = cgraph_nodes_queue;
540       cgraph_nodes_queue = node;
541     }
542 }
543
544 /* Likewise indicate that a node is needed, i.e. reachable via some
545    external means.  */
546
547 void
548 cgraph_mark_needed_node (struct cgraph_node *node)
549 {
550   node->needed = 1;
551   cgraph_mark_reachable_node (node);
552 }
553
554 /* Return local info for the compiled function.  */
555
556 struct cgraph_local_info *
557 cgraph_local_info (tree decl)
558 {
559   struct cgraph_node *node;
560
561   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
562   node = cgraph_node (decl);
563   return &node->local;
564 }
565
566 /* Return local info for the compiled function.  */
567
568 struct cgraph_global_info *
569 cgraph_global_info (tree decl)
570 {
571   struct cgraph_node *node;
572
573   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
574   node = cgraph_node (decl);
575   return &node->global;
576 }
577
578 /* Return local info for the compiled function.  */
579
580 struct cgraph_rtl_info *
581 cgraph_rtl_info (tree decl)
582 {
583   struct cgraph_node *node;
584
585   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
586   node = cgraph_node (decl);
587   if (decl != current_function_decl
588       && !TREE_ASM_WRITTEN (node->decl))
589     return NULL;
590   return &node->rtl;
591 }
592
593 /* Return name of the node used in debug output.  */
594 const char *
595 cgraph_node_name (struct cgraph_node *node)
596 {
597   return lang_hooks.decl_printable_name (node->decl, 2);
598 }
599
600 /* Return name of the node used in debug output.  */
601 static const char *
602 cgraph_varpool_node_name (struct cgraph_varpool_node *node)
603 {
604   return lang_hooks.decl_printable_name (node->decl, 2);
605 }
606
607 /* Names used to print out the availability enum.  */
608 static const char * const availability_names[] =
609   {"unset", "not_available", "overwrittable", "available", "local"};
610
611 /* Dump given cgraph node.  */
612 void
613 dump_cgraph_node (FILE *f, struct cgraph_node *node)
614 {
615   struct cgraph_edge *edge;
616   fprintf (f, "%s/%i:", cgraph_node_name (node), node->uid);
617   if (node->global.inlined_to)
618     fprintf (f, " (inline copy in %s/%i)",
619              cgraph_node_name (node->global.inlined_to),
620              node->global.inlined_to->uid);
621   if (cgraph_function_flags_ready)
622     fprintf (f, " availability:%s",
623              availability_names [cgraph_function_body_availability (node)]);
624   if (node->master_clone && node->master_clone->uid != node->uid)
625     fprintf (f, "(%i)", node->master_clone->uid);
626   if (node->count)
627     fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
628              (HOST_WIDEST_INT)node->count);
629   if (node->local.self_insns)
630     fprintf (f, " %i insns", node->local.self_insns);
631   if (node->global.insns && node->global.insns != node->local.self_insns)
632     fprintf (f, " (%i after inlining)", node->global.insns);
633   if (node->origin)
634     fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
635   if (node->needed)
636     fprintf (f, " needed");
637   else if (node->reachable)
638     fprintf (f, " reachable");
639   if (DECL_SAVED_TREE (node->decl))
640     fprintf (f, " tree");
641   if (node->output)
642     fprintf (f, " output");
643   if (node->local.local)
644     fprintf (f, " local");
645   if (node->local.externally_visible)
646     fprintf (f, " externally_visible");
647   if (node->local.finalized)
648     fprintf (f, " finalized");
649   if (node->local.disregard_inline_limits)
650     fprintf (f, " always_inline");
651   else if (node->local.inlinable)
652     fprintf (f, " inlinable");
653   if (node->local.redefined_extern_inline)
654     fprintf (f, " redefined_extern_inline");
655   if (TREE_ASM_WRITTEN (node->decl))
656     fprintf (f, " asm_written");
657
658   fprintf (f, "\n  called by: ");
659   for (edge = node->callers; edge; edge = edge->next_caller)
660     {
661       fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
662                edge->caller->uid);
663       if (edge->count)
664         fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
665                  (HOST_WIDEST_INT)edge->count);
666       if (!edge->inline_failed)
667         fprintf(f, "(inlined) ");
668     }
669
670   fprintf (f, "\n  calls: ");
671   for (edge = node->callees; edge; edge = edge->next_callee)
672     {
673       fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
674                edge->callee->uid);
675       if (!edge->inline_failed)
676         fprintf(f, "(inlined) ");
677       if (edge->count)
678         fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
679                  (HOST_WIDEST_INT)edge->count);
680       if (edge->loop_nest)
681         fprintf (f, "(nested in %i loops) ", edge->loop_nest);
682     }
683   fprintf (f, "\n");
684 }
685
686 /* Dump the callgraph.  */
687
688 void
689 dump_cgraph (FILE *f)
690 {
691   struct cgraph_node *node;
692
693   fprintf (f, "callgraph:\n\n");
694   for (node = cgraph_nodes; node; node = node->next)
695     dump_cgraph_node (f, node);
696 }
697
698 /* Dump given cgraph node.  */
699 void
700 dump_cgraph_varpool_node (FILE *f, struct cgraph_varpool_node *node)
701 {
702   fprintf (f, "%s:", cgraph_varpool_node_name (node));
703   fprintf (f, " availability:%s", availability_names [cgraph_variable_initializer_availability (node)]);
704   if (DECL_INITIAL (node->decl))
705     fprintf (f, " initialized");
706   if (node->needed)
707     fprintf (f, " needed");
708   if (node->analyzed)
709     fprintf (f, " analyzed");
710   if (node->finalized)
711     fprintf (f, " finalized");
712   if (node->output)
713     fprintf (f, " output");
714   if (node->externally_visible)
715     fprintf (f, " externally_visible");
716   fprintf (f, "\n");
717 }
718
719 /* Dump the callgraph.  */
720
721 void
722 dump_varpool (FILE *f)
723 {
724   struct cgraph_varpool_node *node;
725
726   fprintf (f, "variable pool:\n\n");
727   for (node = cgraph_varpool_nodes; node; node = node->next_needed)
728     dump_cgraph_varpool_node (f, node);
729 }
730
731 /* Returns a hash code for P.  */
732
733 static hashval_t
734 hash_varpool_node (const void *p)
735 {
736   const struct cgraph_varpool_node *n = (const struct cgraph_varpool_node *) p;
737   return (hashval_t) DECL_UID (n->decl);
738 }
739
740 /* Returns nonzero if P1 and P2 are equal.  */
741
742 static int
743 eq_varpool_node (const void *p1, const void *p2)
744 {
745   const struct cgraph_varpool_node *n1 =
746     (const struct cgraph_varpool_node *) p1;
747   const struct cgraph_varpool_node *n2 =
748     (const struct cgraph_varpool_node *) p2;
749   return DECL_UID (n1->decl) == DECL_UID (n2->decl);
750 }
751
752 /* Return cgraph_varpool node assigned to DECL.  Create new one when needed.  */
753 struct cgraph_varpool_node *
754 cgraph_varpool_node (tree decl)
755 {
756   struct cgraph_varpool_node key, *node, **slot;
757
758   gcc_assert (DECL_P (decl) && TREE_CODE (decl) != FUNCTION_DECL);
759
760   if (!cgraph_varpool_hash)
761     cgraph_varpool_hash = htab_create_ggc (10, hash_varpool_node,
762                                            eq_varpool_node, NULL);
763   key.decl = decl;
764   slot = (struct cgraph_varpool_node **)
765     htab_find_slot (cgraph_varpool_hash, &key, INSERT);
766   if (*slot)
767     return *slot;
768   node = GGC_CNEW (struct cgraph_varpool_node);
769   node->decl = decl;
770   node->order = cgraph_order++;
771   node->next = cgraph_varpool_nodes;
772   cgraph_varpool_nodes = node;
773   *slot = node;
774   return node;
775 }
776
777 struct cgraph_varpool_node *
778 cgraph_varpool_node_for_asm (tree asmname)
779 {
780   struct cgraph_varpool_node *node;
781
782   for (node = cgraph_varpool_nodes; node ; node = node->next)
783     if (decl_assembler_name_equal (node->decl, asmname))
784       return node;
785
786   return NULL;
787 }
788
789 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables.  */
790 void
791 change_decl_assembler_name (tree decl, tree name)
792 {
793   if (!DECL_ASSEMBLER_NAME_SET_P (decl))
794     {
795       SET_DECL_ASSEMBLER_NAME (decl, name);
796       return;
797     }
798   if (name == DECL_ASSEMBLER_NAME (decl))
799     return;
800
801   if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
802       && DECL_RTL_SET_P (decl))
803     warning (0, "%D renamed after being referenced in assembly", decl);
804
805   SET_DECL_ASSEMBLER_NAME (decl, name);
806 }
807
808 /* Helper function for finalization code - add node into lists so it will
809    be analyzed and compiled.  */
810 void
811 cgraph_varpool_enqueue_needed_node (struct cgraph_varpool_node *node)
812 {
813   if (cgraph_varpool_last_needed_node)
814     cgraph_varpool_last_needed_node->next_needed = node;
815   cgraph_varpool_last_needed_node = node;
816   node->next_needed = NULL;
817   if (!cgraph_varpool_nodes_queue)
818     cgraph_varpool_nodes_queue = node;
819   if (!cgraph_varpool_first_unanalyzed_node)
820     cgraph_varpool_first_unanalyzed_node = node;
821   notice_global_symbol (node->decl);
822 }
823
824 /* Reset the queue of needed nodes.  */
825 void
826 cgraph_varpool_reset_queue (void)
827 {
828   cgraph_varpool_last_needed_node = NULL;
829   cgraph_varpool_nodes_queue = NULL;
830   cgraph_varpool_first_unanalyzed_node = NULL;
831 }
832
833 /* Notify finalize_compilation_unit that given node is reachable
834    or needed.  */
835 void
836 cgraph_varpool_mark_needed_node (struct cgraph_varpool_node *node)
837 {
838   if (!node->needed && node->finalized)
839     cgraph_varpool_enqueue_needed_node (node);
840   node->needed = 1;
841 }
842
843 /* Determine if variable DECL is needed.  That is, visible to something
844    either outside this translation unit, something magic in the system
845    configury, or (if not doing unit-at-a-time) to something we haven't
846    seen yet.  */
847
848 bool
849 decide_is_variable_needed (struct cgraph_varpool_node *node, tree decl)
850 {
851   /* If the user told us it is used, then it must be so.  */
852   if (node->externally_visible)
853     return true;
854   if (!flag_unit_at_a_time
855       && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
856     return true;
857
858   /* ??? If the assembler name is set by hand, it is possible to assemble
859      the name later after finalizing the function and the fact is noticed
860      in assemble_name then.  This is arguably a bug.  */
861   if (DECL_ASSEMBLER_NAME_SET_P (decl)
862       && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
863     return true;
864
865   /* If we decided it was needed before, but at the time we didn't have
866      the definition available, then it's still needed.  */
867   if (node->needed)
868     return true;
869
870   /* Externally visible variables must be output.  The exception is
871      COMDAT variables that must be output only when they are needed.  */
872   if (TREE_PUBLIC (decl) && !flag_whole_program && !DECL_COMDAT (decl)
873       && !DECL_EXTERNAL (decl))
874     return true;
875
876   /* When not reordering top level variables, we have to assume that
877      we are going to keep everything.  */
878   if (flag_unit_at_a_time && flag_toplevel_reorder)
879     return false;
880
881   /* We want to emit COMDAT variables only when absolutely necessary.  */
882   if (DECL_COMDAT (decl))
883     return false;
884   return true;
885 }
886
887 void
888 cgraph_varpool_finalize_decl (tree decl)
889 {
890   struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
891
892   /* The first declaration of a variable that comes through this function
893      decides whether it is global (in C, has external linkage)
894      or local (in C, has internal linkage).  So do nothing more
895      if this function has already run.  */
896   if (node->finalized)
897     {
898       if (cgraph_global_info_ready || (!flag_unit_at_a_time && !flag_openmp))
899         cgraph_varpool_assemble_pending_decls ();
900       return;
901     }
902   if (node->needed)
903     cgraph_varpool_enqueue_needed_node (node);
904   node->finalized = true;
905
906   if (decide_is_variable_needed (node, decl))
907     cgraph_varpool_mark_needed_node (node);
908   /* Since we reclaim unreachable nodes at the end of every language
909      level unit, we need to be conservative about possible entry points
910      there.  */
911   else if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
912     cgraph_varpool_mark_needed_node (node);
913   if (cgraph_global_info_ready || (!flag_unit_at_a_time && !flag_openmp))
914     cgraph_varpool_assemble_pending_decls ();
915 }
916
917 /* Add a top-level asm statement to the list.  */
918
919 struct cgraph_asm_node *
920 cgraph_add_asm_node (tree asm_str)
921 {
922   struct cgraph_asm_node *node;
923
924   node = GGC_CNEW (struct cgraph_asm_node);
925   node->asm_str = asm_str;
926   node->order = cgraph_order++;
927   node->next = NULL;
928   if (cgraph_asm_nodes == NULL)
929     cgraph_asm_nodes = node;
930   else
931     cgraph_asm_last_node->next = node;
932   cgraph_asm_last_node = node;
933   return node;
934 }
935
936 /* Return true when the DECL can possibly be inlined.  */
937 bool
938 cgraph_function_possibly_inlined_p (tree decl)
939 {
940   if (!cgraph_global_info_ready)
941     return (DECL_INLINE (decl) && !flag_really_no_inline);
942   return DECL_POSSIBLY_INLINED (decl);
943 }
944
945 /* Create clone of E in the node N represented by CALL_EXPR the callgraph.  */
946 struct cgraph_edge *
947 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
948                    tree call_stmt, gcov_type count_scale, int loop_nest,
949                    bool update_original)
950 {
951   struct cgraph_edge *new;
952
953   new = cgraph_create_edge (n, e->callee, call_stmt,
954                             e->count * count_scale / REG_BR_PROB_BASE,
955                             e->loop_nest + loop_nest);
956
957   new->inline_failed = e->inline_failed;
958   if (update_original)
959     {
960       e->count -= new->count;
961       if (e->count < 0)
962         e->count = 0;
963     }
964   return new;
965 }
966
967 /* Create node representing clone of N executed COUNT times.  Decrease
968    the execution counts from original node too.
969
970    When UPDATE_ORIGINAL is true, the counts are subtracted from the original
971    function's profile to reflect the fact that part of execution is handled
972    by node.  */
973 struct cgraph_node *
974 cgraph_clone_node (struct cgraph_node *n, gcov_type count, int loop_nest,
975                    bool update_original)
976 {
977   struct cgraph_node *new = cgraph_create_node ();
978   struct cgraph_edge *e;
979   gcov_type count_scale;
980
981   new->decl = n->decl;
982   new->origin = n->origin;
983   if (new->origin)
984     {
985       new->next_nested = new->origin->nested;
986       new->origin->nested = new;
987     }
988   new->analyzed = n->analyzed;
989   new->local = n->local;
990   new->global = n->global;
991   new->rtl = n->rtl;
992   new->master_clone = n->master_clone;
993   new->count = count;
994   if (n->count)
995     count_scale = new->count * REG_BR_PROB_BASE / n->count;
996   else
997     count_scale = 0;
998   if (update_original)
999     {
1000       n->count -= count;
1001       if (n->count < 0)
1002         n->count = 0;
1003     }
1004
1005   for (e = n->callees;e; e=e->next_callee)
1006     cgraph_clone_edge (e, new, e->call_stmt, count_scale, loop_nest,
1007                        update_original);
1008
1009   new->next_clone = n->next_clone;
1010   new->prev_clone = n;
1011   n->next_clone = new;
1012   if (new->next_clone)
1013     new->next_clone->prev_clone = new;
1014
1015   return new;
1016 }
1017
1018 /* Return true if N is an master_clone, (see cgraph_master_clone).  */
1019
1020 bool
1021 cgraph_is_master_clone (struct cgraph_node *n)
1022 {
1023   return (n == cgraph_master_clone (n));
1024 }
1025
1026 struct cgraph_node *
1027 cgraph_master_clone (struct cgraph_node *n)
1028 {
1029   enum availability avail = cgraph_function_body_availability (n);
1030
1031   if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
1032     return NULL;
1033
1034   if (!n->master_clone)
1035     n->master_clone = cgraph_node (n->decl);
1036
1037   return n->master_clone;
1038 }
1039
1040 /* NODE is no longer nested function; update cgraph accordingly.  */
1041 void
1042 cgraph_unnest_node (struct cgraph_node *node)
1043 {
1044   struct cgraph_node **node2 = &node->origin->nested;
1045   gcc_assert (node->origin);
1046
1047   while (*node2 != node)
1048     node2 = &(*node2)->next_nested;
1049   *node2 = node->next_nested;
1050   node->origin = NULL;
1051 }
1052
1053 /* Return function availability.  See cgraph.h for description of individual
1054    return values.  */
1055 enum availability
1056 cgraph_function_body_availability (struct cgraph_node *node)
1057 {
1058   enum availability avail;
1059   gcc_assert (cgraph_function_flags_ready);
1060   if (!node->analyzed)
1061     avail = AVAIL_NOT_AVAILABLE;
1062   else if (node->local.local)
1063     avail = AVAIL_LOCAL;
1064   else if (node->local.externally_visible)
1065     avail = AVAIL_AVAILABLE;
1066
1067   /* If the function can be overwritten, return OVERWRITABLE.  Take
1068      care at least of two notable extensions - the COMDAT functions
1069      used to share template instantiations in C++ (this is symmetric
1070      to code cp_cannot_inline_tree_fn and probably shall be shared and
1071      the inlinability hooks completely eliminated).
1072
1073      ??? Does the C++ one definition rule allow us to always return
1074      AVAIL_AVAILABLE here?  That would be good reason to preserve this
1075      hook Similarly deal with extern inline functions - this is again
1076      necessary to get C++ shared functions having keyed templates
1077      right and in the C extension documentation we probably should
1078      document the requirement of both versions of function (extern
1079      inline and offline) having same side effect characteristics as
1080      good optimization is what this optimization is about.  */
1081
1082   else if (!(*targetm.binds_local_p) (node->decl)
1083            && !DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl))
1084     avail = AVAIL_OVERWRITABLE;
1085   else avail = AVAIL_AVAILABLE;
1086
1087   return avail;
1088 }
1089
1090 /* Return variable availability.  See cgraph.h for description of individual
1091    return values.  */
1092 enum availability
1093 cgraph_variable_initializer_availability (struct cgraph_varpool_node *node)
1094 {
1095   gcc_assert (cgraph_function_flags_ready);
1096   if (!node->finalized)
1097     return AVAIL_NOT_AVAILABLE;
1098   if (!TREE_PUBLIC (node->decl))
1099     return AVAIL_AVAILABLE;
1100   /* If the variable can be overwritten, return OVERWRITABLE.  Takes
1101      care of at least two notable extensions - the COMDAT variables
1102      used to share template instantiations in C++.  */
1103   if (!(*targetm.binds_local_p) (node->decl) && !DECL_COMDAT (node->decl))
1104     return AVAIL_OVERWRITABLE;
1105   return AVAIL_AVAILABLE;
1106 }
1107
1108
1109 /* Add the function FNDECL to the call graph.  FNDECL is assumed to be
1110    in low GIMPLE form and ready to be processed by cgraph_finalize_function.
1111
1112    When operating in unit-at-a-time, a new callgraph node is added to
1113    CGRAPH_EXPAND_QUEUE, which is processed after all the original
1114    functions in the call graph .
1115
1116    When not in unit-at-a-time, the new callgraph node is added to
1117    CGRAPH_NODES_QUEUE for cgraph_assemble_pending_functions to
1118    process.  */
1119
1120 void
1121 cgraph_add_new_function (tree fndecl)
1122 {
1123   struct cgraph_node *n = cgraph_node (fndecl);
1124   n->next_needed = cgraph_expand_queue;
1125   cgraph_expand_queue = n;
1126 }
1127
1128 #include "gt-cgraph.h"