OSDN Git Service

3e5b8466d8c178ba954fc3a6240a5b2b9ba6fe06
[pf3gnuchains/gcc-fork.git] / gcc / cgraph.c
1 /* Callgraph handling code.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 "output.h"
82 #include "intl.h"
83 #include "gimple.h"
84 #include "tree-dump.h"
85 #include "tree-flow.h"
86 #include "value-prof.h"
87 #include "except.h"
88
89 static void cgraph_node_remove_callers (struct cgraph_node *node);
90 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
91 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
92
93 /* Hash table used to convert declarations into nodes.  */
94 static GTY((param_is (struct cgraph_node))) htab_t cgraph_hash;
95 /* Hash table used to convert assembler names into nodes.  */
96 static GTY((param_is (struct cgraph_node))) htab_t assembler_name_hash;
97
98 /* The linked list of cgraph nodes.  */
99 struct cgraph_node *cgraph_nodes;
100
101 /* Queue of cgraph nodes scheduled to be lowered.  */
102 struct cgraph_node *cgraph_nodes_queue;
103
104 /* Queue of cgraph nodes scheduled to be added into cgraph.  This is a
105    secondary queue used during optimization to accommodate passes that
106    may generate new functions that need to be optimized and expanded.  */
107 struct cgraph_node *cgraph_new_nodes;
108
109 /* Number of nodes in existence.  */
110 int cgraph_n_nodes;
111
112 /* Maximal uid used in cgraph nodes.  */
113 int cgraph_max_uid;
114
115 /* Maximal uid used in cgraph edges.  */
116 int cgraph_edge_max_uid;
117
118 /* Maximal pid used for profiling */
119 int cgraph_max_pid;
120
121 /* Set when whole unit has been analyzed so we can access global info.  */
122 bool cgraph_global_info_ready = false;
123
124 /* What state callgraph is in right now.  */
125 enum cgraph_state cgraph_state = CGRAPH_STATE_CONSTRUCTION;
126
127 /* Set when the cgraph is fully build and the basic flags are computed.  */
128 bool cgraph_function_flags_ready = false;
129
130 /* Linked list of cgraph asm nodes.  */
131 struct cgraph_asm_node *cgraph_asm_nodes;
132
133 /* Last node in cgraph_asm_nodes.  */
134 static GTY(()) struct cgraph_asm_node *cgraph_asm_last_node;
135
136 /* The order index of the next cgraph node to be created.  This is
137    used so that we can sort the cgraph nodes in order by when we saw
138    them, to support -fno-toplevel-reorder.  */
139 int cgraph_order;
140
141 /* List of hooks trigerred on cgraph_edge events.  */
142 struct cgraph_edge_hook_list {
143   cgraph_edge_hook hook;
144   void *data;
145   struct cgraph_edge_hook_list *next;
146 };
147
148 /* List of hooks trigerred on cgraph_node events.  */
149 struct cgraph_node_hook_list {
150   cgraph_node_hook hook;
151   void *data;
152   struct cgraph_node_hook_list *next;
153 };
154
155 /* List of hooks trigerred on events involving two cgraph_edges.  */
156 struct cgraph_2edge_hook_list {
157   cgraph_2edge_hook hook;
158   void *data;
159   struct cgraph_2edge_hook_list *next;
160 };
161
162 /* List of hooks trigerred on events involving two cgraph_nodes.  */
163 struct cgraph_2node_hook_list {
164   cgraph_2node_hook hook;
165   void *data;
166   struct cgraph_2node_hook_list *next;
167 };
168
169 /* List of hooks triggered when an edge is removed.  */
170 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
171 /* List of hooks triggered when a node is removed.  */
172 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
173 /* List of hooks triggered when an edge is duplicated.  */
174 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
175 /* List of hooks triggered when a node is duplicated.  */
176 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
177 /* List of hooks triggered when an function is inserted.  */
178 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
179
180 /* Head of a linked list of unused (freed) call graph nodes.
181    Do not GTY((delete)) this list so UIDs gets reliably recycled.  */
182 static GTY(()) struct cgraph_node *free_nodes;
183 /* Head of a linked list of unused (freed) call graph edges.
184    Do not GTY((delete)) this list so UIDs gets reliably recycled.  */
185 static GTY(()) struct cgraph_edge *free_edges;
186
187 /* Macros to access the next item in the list of free cgraph nodes and
188    edges. */
189 #define NEXT_FREE_NODE(NODE) (NODE)->next
190 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
191
192 /* Register HOOK to be called with DATA on each removed edge.  */
193 struct cgraph_edge_hook_list *
194 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
195 {
196   struct cgraph_edge_hook_list *entry;
197   struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
198
199   entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
200   entry->hook = hook;
201   entry->data = data;
202   entry->next = NULL;
203   while (*ptr)
204     ptr = &(*ptr)->next;
205   *ptr = entry;
206   return entry;
207 }
208
209 /* Remove ENTRY from the list of hooks called on removing edges.  */
210 void
211 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
212 {
213   struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
214
215   while (*ptr != entry)
216     ptr = &(*ptr)->next;
217   *ptr = entry->next;
218   free (entry);
219 }
220
221 /* Call all edge removal hooks.  */
222 static void
223 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
224 {
225   struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
226   while (entry)
227   {
228     entry->hook (e, entry->data);
229     entry = entry->next;
230   }
231 }
232
233 /* Register HOOK to be called with DATA on each removed node.  */
234 struct cgraph_node_hook_list *
235 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
236 {
237   struct cgraph_node_hook_list *entry;
238   struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
239
240   entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
241   entry->hook = hook;
242   entry->data = data;
243   entry->next = NULL;
244   while (*ptr)
245     ptr = &(*ptr)->next;
246   *ptr = entry;
247   return entry;
248 }
249
250 /* Remove ENTRY from the list of hooks called on removing nodes.  */
251 void
252 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
253 {
254   struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
255
256   while (*ptr != entry)
257     ptr = &(*ptr)->next;
258   *ptr = entry->next;
259   free (entry);
260 }
261
262 /* Call all node removal hooks.  */
263 static void
264 cgraph_call_node_removal_hooks (struct cgraph_node *node)
265 {
266   struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
267   while (entry)
268   {
269     entry->hook (node, entry->data);
270     entry = entry->next;
271   }
272 }
273
274 /* Register HOOK to be called with DATA on each removed node.  */
275 struct cgraph_node_hook_list *
276 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
277 {
278   struct cgraph_node_hook_list *entry;
279   struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
280
281   entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
282   entry->hook = hook;
283   entry->data = data;
284   entry->next = NULL;
285   while (*ptr)
286     ptr = &(*ptr)->next;
287   *ptr = entry;
288   return entry;
289 }
290
291 /* Remove ENTRY from the list of hooks called on removing nodes.  */
292 void
293 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
294 {
295   struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
296
297   while (*ptr != entry)
298     ptr = &(*ptr)->next;
299   *ptr = entry->next;
300   free (entry);
301 }
302
303 /* Call all node removal hooks.  */
304 void
305 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
306 {
307   struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
308   while (entry)
309   {
310     entry->hook (node, entry->data);
311     entry = entry->next;
312   }
313 }
314
315 /* Register HOOK to be called with DATA on each duplicated edge.  */
316 struct cgraph_2edge_hook_list *
317 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
318 {
319   struct cgraph_2edge_hook_list *entry;
320   struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
321
322   entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
323   entry->hook = hook;
324   entry->data = data;
325   entry->next = NULL;
326   while (*ptr)
327     ptr = &(*ptr)->next;
328   *ptr = entry;
329   return entry;
330 }
331
332 /* Remove ENTRY from the list of hooks called on duplicating edges.  */
333 void
334 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
335 {
336   struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
337
338   while (*ptr != entry)
339     ptr = &(*ptr)->next;
340   *ptr = entry->next;
341   free (entry);
342 }
343
344 /* Call all edge duplication hooks.  */
345 static void
346 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
347                                     struct cgraph_edge *cs2)
348 {
349   struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
350   while (entry)
351   {
352     entry->hook (cs1, cs2, entry->data);
353     entry = entry->next;
354   }
355 }
356
357 /* Register HOOK to be called with DATA on each duplicated node.  */
358 struct cgraph_2node_hook_list *
359 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
360 {
361   struct cgraph_2node_hook_list *entry;
362   struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
363
364   entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
365   entry->hook = hook;
366   entry->data = data;
367   entry->next = NULL;
368   while (*ptr)
369     ptr = &(*ptr)->next;
370   *ptr = entry;
371   return entry;
372 }
373
374 /* Remove ENTRY from the list of hooks called on duplicating nodes.  */
375 void
376 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
377 {
378   struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
379
380   while (*ptr != entry)
381     ptr = &(*ptr)->next;
382   *ptr = entry->next;
383   free (entry);
384 }
385
386 /* Call all node duplication hooks.  */
387 static void
388 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
389                                     struct cgraph_node *node2)
390 {
391   struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
392   while (entry)
393   {
394     entry->hook (node1, node2, entry->data);
395     entry = entry->next;
396   }
397 }
398
399 /* Returns a hash code for P.  */
400
401 static hashval_t
402 hash_node (const void *p)
403 {
404   const struct cgraph_node *n = (const struct cgraph_node *) p;
405   return (hashval_t) DECL_UID (n->decl);
406 }
407
408
409 /* Returns nonzero if P1 and P2 are equal.  */
410
411 static int
412 eq_node (const void *p1, const void *p2)
413 {
414   const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
415   const struct cgraph_node *n2 = (const struct cgraph_node *) p2;
416   return DECL_UID (n1->decl) == DECL_UID (n2->decl);
417 }
418
419 /* Allocate new callgraph node.  */
420
421 static inline struct cgraph_node *
422 cgraph_allocate_node (void)
423 {
424   struct cgraph_node *node;
425
426   if (free_nodes)
427     {
428       node = free_nodes;
429       free_nodes = NEXT_FREE_NODE (node);
430     }
431   else
432     {
433       node = GGC_CNEW (struct cgraph_node);
434       node->uid = cgraph_max_uid++;
435     }
436
437   return node;
438 }
439
440 /* Allocate new callgraph node and insert it into basic data structures.  */
441
442 static struct cgraph_node *
443 cgraph_create_node (void)
444 {
445   struct cgraph_node *node = cgraph_allocate_node ();
446
447   node->next = cgraph_nodes;
448   node->pid = -1;
449   node->order = cgraph_order++;
450   if (cgraph_nodes)
451     cgraph_nodes->previous = node;
452   node->previous = NULL;
453   node->global.estimated_growth = INT_MIN;
454   cgraph_nodes = node;
455   cgraph_n_nodes++;
456   return node;
457 }
458
459 /* Return cgraph node assigned to DECL.  Create new one when needed.  */
460
461 struct cgraph_node *
462 cgraph_node (tree decl)
463 {
464   struct cgraph_node key, *node, **slot;
465
466   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
467
468   if (!cgraph_hash)
469     cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
470
471   key.decl = decl;
472
473   slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
474
475   if (*slot)
476     {
477       node = *slot;
478       if (node->same_body_alias)
479         node = node->same_body;
480       return node;
481     }
482
483   node = cgraph_create_node ();
484   node->decl = decl;
485   *slot = node;
486   if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
487     {
488       node->origin = cgraph_node (DECL_CONTEXT (decl));
489       node->next_nested = node->origin->nested;
490       node->origin->nested = node;
491     }
492   if (assembler_name_hash)
493     {
494       void **aslot;
495       tree name = DECL_ASSEMBLER_NAME (decl);
496
497       aslot = htab_find_slot_with_hash (assembler_name_hash, name,
498                                         decl_assembler_name_hash (name),
499                                         INSERT);
500       /* We can have multiple declarations with same assembler name. For C++
501          it is __builtin_strlen and strlen, for instance.  Do we need to
502          record them all?  Original implementation marked just first one
503          so lets hope for the best.  */
504       if (*aslot == NULL)
505         *aslot = node;
506     }
507   return node;
508 }
509
510 /* Attempt to mark ALIAS as an alias to DECL.  Return TRUE if successful.
511    Same body aliases are output whenever the body of DECL is output,
512    and cgraph_node (ALIAS) transparently returns cgraph_node (DECL).  */
513
514 bool
515 cgraph_same_body_alias (tree alias, tree decl)
516 {
517   struct cgraph_node key, *alias_node, *decl_node, **slot;
518
519   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
520   gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
521   gcc_assert (!assembler_name_hash);
522
523 #ifndef ASM_OUTPUT_DEF
524   /* If aliases aren't supported by the assembler, fail.  */
525   return false;
526 #endif
527
528   /* Comdat same body aliases are only supported when comdat groups
529      are supported and the symbols are weak.  */
530   if (DECL_ONE_ONLY (decl) && (!HAVE_COMDAT_GROUP || !DECL_WEAK (decl)))
531     return false;
532
533   decl_node = cgraph_node (decl);
534
535   key.decl = alias;
536
537   slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
538
539   /* If the cgraph_node has been already created, fail.  */
540   if (*slot)
541     return false;
542
543   alias_node = cgraph_allocate_node ();
544   alias_node->decl = alias;
545   alias_node->same_body_alias = 1;
546   alias_node->same_body = decl_node;
547   alias_node->previous = NULL;
548   if (decl_node->same_body)
549     decl_node->same_body->previous = alias_node;
550   alias_node->next = decl_node->same_body;
551   decl_node->same_body = alias_node;
552   *slot = alias_node;
553   return true;
554 }
555
556 /* Returns the cgraph node assigned to DECL or NULL if no cgraph node
557    is assigned.  */
558
559 struct cgraph_node *
560 cgraph_get_node (tree decl)
561 {
562   struct cgraph_node key, *node = NULL, **slot;
563
564   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
565
566   if (!cgraph_hash)
567     return NULL;
568
569   key.decl = decl;
570
571   slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key,
572                                                  NO_INSERT);
573
574   if (slot && *slot)
575     {
576       node = *slot;
577       if (node->same_body_alias)
578         node = node->same_body;
579     }
580   return node;
581 }
582
583 /* Insert already constructed node into hashtable.  */
584
585 void
586 cgraph_insert_node_to_hashtable (struct cgraph_node *node)
587 {
588   struct cgraph_node **slot;
589
590   slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, node, INSERT);
591
592   gcc_assert (!*slot);
593   *slot = node;
594 }
595
596 /* Returns a hash code for P.  */
597
598 static hashval_t
599 hash_node_by_assembler_name (const void *p)
600 {
601   const struct cgraph_node *n = (const struct cgraph_node *) p;
602   return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
603 }
604
605 /* Returns nonzero if P1 and P2 are equal.  */
606
607 static int
608 eq_assembler_name (const void *p1, const void *p2)
609 {
610   const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
611   const_tree name = (const_tree)p2;
612   return (decl_assembler_name_equal (n1->decl, name));
613 }
614
615 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
616    Return NULL if there's no such node.  */
617
618 struct cgraph_node *
619 cgraph_node_for_asm (tree asmname)
620 {
621   struct cgraph_node *node;
622   void **slot;
623
624   if (!assembler_name_hash)
625     {
626       assembler_name_hash =
627         htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
628                          NULL);
629       for (node = cgraph_nodes; node; node = node->next)
630         if (!node->global.inlined_to)
631           {
632             tree name = DECL_ASSEMBLER_NAME (node->decl);
633             slot = htab_find_slot_with_hash (assembler_name_hash, name,
634                                              decl_assembler_name_hash (name),
635                                              INSERT);
636             /* We can have multiple declarations with same assembler name. For C++
637                it is __builtin_strlen and strlen, for instance.  Do we need to
638                record them all?  Original implementation marked just first one
639                so lets hope for the best.  */
640             if (!*slot)
641               *slot = node;
642             if (node->same_body)
643               {
644                 struct cgraph_node *alias;
645
646                 for (alias = node->same_body; alias; alias = alias->next)
647                   {
648                     hashval_t hash;
649                     name = DECL_ASSEMBLER_NAME (alias->decl);
650                     hash = decl_assembler_name_hash (name);
651                     slot = htab_find_slot_with_hash (assembler_name_hash, name,
652                                                      hash,  INSERT);
653                     if (!*slot)
654                       *slot = alias;
655                   }
656               }
657           }
658     }
659
660   slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
661                                    decl_assembler_name_hash (asmname),
662                                    NO_INSERT);
663
664   if (slot)
665     {
666       node = (struct cgraph_node *) *slot;
667       if (node->same_body_alias)
668         node = node->same_body;
669       return node;
670     }
671   return NULL;
672 }
673
674 /* Returns a hash value for X (which really is a die_struct).  */
675
676 static hashval_t
677 edge_hash (const void *x)
678 {
679   return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
680 }
681
682 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
683
684 static int
685 edge_eq (const void *x, const void *y)
686 {
687   return ((const struct cgraph_edge *) x)->call_stmt == y;
688 }
689
690
691 /* Return the callgraph edge representing the GIMPLE_CALL statement
692    CALL_STMT.  */
693
694 struct cgraph_edge *
695 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
696 {
697   struct cgraph_edge *e, *e2;
698   int n = 0;
699
700   if (node->call_site_hash)
701     return (struct cgraph_edge *)
702       htab_find_with_hash (node->call_site_hash, call_stmt,
703                            htab_hash_pointer (call_stmt));
704
705   /* This loop may turn out to be performance problem.  In such case adding
706      hashtables into call nodes with very many edges is probably best
707      solution.  It is not good idea to add pointer into CALL_EXPR itself
708      because we want to make possible having multiple cgraph nodes representing
709      different clones of the same body before the body is actually cloned.  */
710   for (e = node->callees; e; e= e->next_callee)
711     {
712       if (e->call_stmt == call_stmt)
713         break;
714       n++;
715     }
716
717   if (n > 100)
718     {
719       node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
720       for (e2 = node->callees; e2; e2 = e2->next_callee)
721         {
722           void **slot;
723           slot = htab_find_slot_with_hash (node->call_site_hash,
724                                            e2->call_stmt,
725                                            htab_hash_pointer (e2->call_stmt),
726                                            INSERT);
727           gcc_assert (!*slot);
728           *slot = e2;
729         }
730     }
731
732   return e;
733 }
734
735
736 /* Change field call_stmt of edge E to NEW_STMT.  */
737
738 void
739 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
740 {
741   if (e->caller->call_site_hash)
742     {
743       htab_remove_elt_with_hash (e->caller->call_site_hash,
744                                  e->call_stmt,
745                                  htab_hash_pointer (e->call_stmt));
746     }
747   e->call_stmt = new_stmt;
748   push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
749   e->can_throw_external = stmt_can_throw_external (new_stmt);
750   pop_cfun ();
751   if (e->caller->call_site_hash)
752     {
753       void **slot;
754       slot = htab_find_slot_with_hash (e->caller->call_site_hash,
755                                        e->call_stmt,
756                                        htab_hash_pointer
757                                        (e->call_stmt), INSERT);
758       gcc_assert (!*slot);
759       *slot = e;
760     }
761 }
762
763 /* Like cgraph_set_call_stmt but walk the clone tree and update all
764    clones sharing the same function body.  */
765
766 void
767 cgraph_set_call_stmt_including_clones (struct cgraph_node *orig,
768                                        gimple old_stmt, gimple new_stmt)
769 {
770   struct cgraph_node *node;
771   struct cgraph_edge *edge = cgraph_edge (orig, old_stmt);
772
773   if (edge)
774     cgraph_set_call_stmt (edge, new_stmt);
775
776   node = orig->clones;
777   if (node)
778     while (node != orig)
779       {
780         struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
781         if (edge)
782           cgraph_set_call_stmt (edge, new_stmt);
783         if (node->clones)
784           node = node->clones;
785         else if (node->next_sibling_clone)
786           node = node->next_sibling_clone;
787         else
788           {
789             while (node != orig && !node->next_sibling_clone)
790               node = node->clone_of;
791             if (node != orig)
792               node = node->next_sibling_clone;
793           }
794       }
795 }
796
797 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
798    same function body.  
799    
800    TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
801    frequencies of the clones.  */
802
803 void
804 cgraph_create_edge_including_clones (struct cgraph_node *orig,
805                                      struct cgraph_node *callee,
806                                      gimple stmt, gcov_type count,
807                                      int freq, int loop_depth,
808                                      cgraph_inline_failed_t reason)
809 {
810   struct cgraph_node *node;
811   struct cgraph_edge *edge;
812
813   if (!cgraph_edge (orig, stmt))
814     {
815       edge = cgraph_create_edge (orig, callee, stmt, count, freq, loop_depth);
816       edge->inline_failed = reason;
817     }
818
819   node = orig->clones;
820   if (node)
821     while (node != orig)
822       {
823         /* It is possible that we already constant propagated into the clone
824            and turned indirect call into dirrect call.  */
825         if (!cgraph_edge (node, stmt))
826           {
827             edge = cgraph_create_edge (node, callee, stmt, count,
828                                        freq, loop_depth);
829             edge->inline_failed = reason;
830           }
831
832         if (node->clones)
833           node = node->clones;
834         else if (node->next_sibling_clone)
835           node = node->next_sibling_clone;
836         else
837           {
838             while (node != orig && !node->next_sibling_clone)
839               node = node->clone_of;
840             if (node != orig)
841               node = node->next_sibling_clone;
842           }
843       }
844 }
845
846 /* Give initial reasons why inlining would fail on EDGE.  This gets either
847    nullified or usually overwritten by more precise reasons later.  */
848
849 static void
850 initialize_inline_failed (struct cgraph_edge *e)
851 {
852   struct cgraph_node *callee = e->callee;
853
854   if (!callee->analyzed)
855     e->inline_failed = CIF_BODY_NOT_AVAILABLE;
856   else if (callee->local.redefined_extern_inline)
857     e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
858   else if (!callee->local.inlinable)
859     e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
860   else if (e->call_stmt && gimple_call_cannot_inline_p (e->call_stmt))
861     e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
862   else
863     e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
864 }
865
866 /* Create edge from CALLER to CALLEE in the cgraph.  */
867
868 struct cgraph_edge *
869 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
870                     gimple call_stmt, gcov_type count, int freq, int nest)
871 {
872   struct cgraph_edge *edge;
873
874
875   /* LTO does not actually have access to the call_stmt since these
876      have not been loaded yet.  */
877   if (call_stmt)
878     {
879 #ifdef ENABLE_CHECKING
880       /* This is rather pricely check possibly trigerring construction of
881          call stmt hashtable.  */
882       gcc_assert (!cgraph_edge (caller, call_stmt));
883 #endif
884
885       gcc_assert (is_gimple_call (call_stmt));
886     }
887
888   if (free_edges)
889     {
890       edge = free_edges;
891       free_edges = NEXT_FREE_EDGE (edge);
892     }
893   else
894     {
895       edge = GGC_NEW (struct cgraph_edge);
896       edge->uid = cgraph_edge_max_uid++;
897     }
898
899   edge->aux = NULL;
900
901   edge->caller = caller;
902   edge->callee = callee;
903   edge->call_stmt = call_stmt;
904   push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
905   edge->can_throw_external
906     = call_stmt ? stmt_can_throw_external (call_stmt) : false;
907   pop_cfun ();
908   edge->prev_caller = NULL;
909   edge->next_caller = callee->callers;
910   if (callee->callers)
911     callee->callers->prev_caller = edge;
912   edge->prev_callee = NULL;
913   edge->next_callee = caller->callees;
914   if (caller->callees)
915     caller->callees->prev_callee = edge;
916   caller->callees = edge;
917   callee->callers = edge;
918   edge->count = count;
919   gcc_assert (count >= 0);
920   edge->frequency = freq;
921   gcc_assert (freq >= 0);
922   gcc_assert (freq <= CGRAPH_FREQ_MAX);
923   edge->loop_nest = nest;
924   edge->indirect_call = 0;
925   edge->call_stmt_cannot_inline_p =
926     (call_stmt ? gimple_call_cannot_inline_p (call_stmt) : false);
927   if (call_stmt && caller->call_site_hash)
928     {
929       void **slot;
930       slot = htab_find_slot_with_hash (caller->call_site_hash,
931                                        edge->call_stmt,
932                                        htab_hash_pointer
933                                          (edge->call_stmt),
934                                        INSERT);
935       gcc_assert (!*slot);
936       *slot = edge;
937     }
938
939   initialize_inline_failed (edge);
940
941   return edge;
942 }
943
944 /* Remove the edge E from the list of the callers of the callee.  */
945
946 static inline void
947 cgraph_edge_remove_callee (struct cgraph_edge *e)
948 {
949   if (e->prev_caller)
950     e->prev_caller->next_caller = e->next_caller;
951   if (e->next_caller)
952     e->next_caller->prev_caller = e->prev_caller;
953   if (!e->prev_caller)
954     e->callee->callers = e->next_caller;
955 }
956
957 /* Remove the edge E from the list of the callees of the caller.  */
958
959 static inline void
960 cgraph_edge_remove_caller (struct cgraph_edge *e)
961 {
962   if (e->prev_callee)
963     e->prev_callee->next_callee = e->next_callee;
964   if (e->next_callee)
965     e->next_callee->prev_callee = e->prev_callee;
966   if (!e->prev_callee)
967     e->caller->callees = e->next_callee;
968   if (e->caller->call_site_hash)
969     htab_remove_elt_with_hash (e->caller->call_site_hash,
970                                e->call_stmt,
971                                htab_hash_pointer (e->call_stmt));
972 }
973
974 /* Put the edge onto the free list.  */
975
976 static void
977 cgraph_free_edge (struct cgraph_edge *e)
978 {
979   int uid = e->uid;
980
981   /* Clear out the edge so we do not dangle pointers.  */
982   memset (e, 0, sizeof (*e));
983   e->uid = uid;
984   NEXT_FREE_EDGE (e) = free_edges;
985   free_edges = e;
986 }
987
988 /* Remove the edge E in the cgraph.  */
989
990 void
991 cgraph_remove_edge (struct cgraph_edge *e)
992 {
993   /* Call all edge removal hooks.  */
994   cgraph_call_edge_removal_hooks (e);
995
996   /* Remove from callers list of the callee.  */
997   cgraph_edge_remove_callee (e);
998
999   /* Remove from callees list of the callers.  */
1000   cgraph_edge_remove_caller (e);
1001
1002   /* Put the edge onto the free list.  */
1003   cgraph_free_edge (e);
1004 }
1005
1006 /* Redirect callee of E to N.  The function does not update underlying
1007    call expression.  */
1008
1009 void
1010 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1011 {
1012   /* Remove from callers list of the current callee.  */
1013   cgraph_edge_remove_callee (e);
1014
1015   /* Insert to callers list of the new callee.  */
1016   e->prev_caller = NULL;
1017   if (n->callers)
1018     n->callers->prev_caller = e;
1019   e->next_caller = n->callers;
1020   n->callers = e;
1021   e->callee = n;
1022 }
1023
1024
1025 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1026    OLD_STMT changed into NEW_STMT.  OLD_CALL is gimple_call_fndecl
1027    of OLD_STMT if it was previously call statement.  */
1028
1029 static void
1030 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1031                                         gimple old_stmt, tree old_call, gimple new_stmt)
1032 {
1033   tree new_call = (is_gimple_call (new_stmt)) ? gimple_call_fndecl (new_stmt) : 0;
1034
1035   /* We are seeing indirect calls, then there is nothing to update.  */
1036   if (!new_call && !old_call)
1037     return;
1038   /* See if we turned indirect call into direct call or folded call to one builtin
1039      into different bultin.  */
1040   if (old_call != new_call)
1041     {
1042       struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1043       struct cgraph_edge *ne = NULL;
1044       gcov_type count;
1045       int frequency;
1046       int loop_nest;
1047
1048       if (e)
1049         {
1050           /* See if the call is already there.  It might be because of indirect
1051              inlining already found it.  */
1052           if (new_call && e->callee->decl == new_call)
1053             return;
1054
1055           /* Otherwise remove edge and create new one; we can't simply redirect
1056              since function has changed, so inline plan and other information
1057              attached to edge is invalid.  */
1058           count = e->count;
1059           frequency = e->frequency;
1060           loop_nest = e->loop_nest;
1061           cgraph_remove_edge (e);
1062         }
1063       else
1064         {
1065           /* We are seeing new direct call; compute profile info based on BB.  */
1066           basic_block bb = gimple_bb (new_stmt);
1067           count = bb->count;
1068           frequency = compute_call_stmt_bb_frequency (current_function_decl,
1069                                                       bb);
1070           loop_nest = bb->loop_depth;
1071         }
1072
1073       if (new_call)
1074         {
1075           ne = cgraph_create_edge (node, cgraph_node (new_call),
1076                                    new_stmt, count, frequency,
1077                                    loop_nest);
1078           gcc_assert (ne->inline_failed);
1079         }
1080     }
1081   /* We only updated the call stmt; update pointer in cgraph edge..  */
1082   else if (old_stmt != new_stmt)
1083     cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1084 }
1085
1086 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1087    OLD_STMT changed into NEW_STMT.  OLD_DECL is gimple_call_fndecl
1088    of OLD_STMT before it was updated (updating can happen inplace).  */
1089
1090 void
1091 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1092 {
1093   struct cgraph_node *orig = cgraph_node (cfun->decl);
1094   struct cgraph_node *node;
1095
1096   cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1097   if (orig->clones)
1098     for (node = orig->clones; node != orig;)
1099       {
1100         cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1101         if (node->clones)
1102           node = node->clones;
1103         else if (node->next_sibling_clone)
1104           node = node->next_sibling_clone;
1105         else
1106           {
1107             while (node != orig && !node->next_sibling_clone)
1108               node = node->clone_of;
1109             if (node != orig)
1110               node = node->next_sibling_clone;
1111           }
1112       }
1113 }
1114
1115
1116 /* Remove all callees from the node.  */
1117
1118 void
1119 cgraph_node_remove_callees (struct cgraph_node *node)
1120 {
1121   struct cgraph_edge *e, *f;
1122
1123   /* It is sufficient to remove the edges from the lists of callers of
1124      the callees.  The callee list of the node can be zapped with one
1125      assignment.  */
1126   for (e = node->callees; e; e = f)
1127     {
1128       f = e->next_callee;
1129       cgraph_call_edge_removal_hooks (e);
1130       cgraph_edge_remove_callee (e);
1131       cgraph_free_edge (e);
1132     }
1133   node->callees = NULL;
1134   if (node->call_site_hash)
1135     {
1136       htab_delete (node->call_site_hash);
1137       node->call_site_hash = NULL;
1138     }
1139 }
1140
1141 /* Remove all callers from the node.  */
1142
1143 static void
1144 cgraph_node_remove_callers (struct cgraph_node *node)
1145 {
1146   struct cgraph_edge *e, *f;
1147
1148   /* It is sufficient to remove the edges from the lists of callees of
1149      the callers.  The caller list of the node can be zapped with one
1150      assignment.  */
1151   for (e = node->callers; e; e = f)
1152     {
1153       f = e->next_caller;
1154       cgraph_call_edge_removal_hooks (e);
1155       cgraph_edge_remove_caller (e);
1156       cgraph_free_edge (e);
1157     }
1158   node->callers = NULL;
1159 }
1160
1161 /* Release memory used to represent body of function NODE.  */
1162
1163 void
1164 cgraph_release_function_body (struct cgraph_node *node)
1165 {
1166   if (DECL_STRUCT_FUNCTION (node->decl))
1167     {
1168       tree old_decl = current_function_decl;
1169       push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1170       if (cfun->gimple_df)
1171         {
1172           current_function_decl = node->decl;
1173           delete_tree_ssa ();
1174           delete_tree_cfg_annotations ();
1175           cfun->eh = NULL;
1176           current_function_decl = old_decl;
1177         }
1178       if (cfun->cfg)
1179         {
1180           gcc_assert (dom_computed[0] == DOM_NONE);
1181           gcc_assert (dom_computed[1] == DOM_NONE);
1182           clear_edges ();
1183         }
1184       if (cfun->value_histograms)
1185         free_histograms ();
1186       gcc_assert (!current_loops);
1187       pop_cfun();
1188       gimple_set_body (node->decl, NULL);
1189       VEC_free (ipa_opt_pass, heap,
1190                 node->ipa_transforms_to_apply);
1191       /* Struct function hangs a lot of data that would leak if we didn't
1192          removed all pointers to it.   */
1193       ggc_free (DECL_STRUCT_FUNCTION (node->decl));
1194       DECL_STRUCT_FUNCTION (node->decl) = NULL;
1195     }
1196   DECL_SAVED_TREE (node->decl) = NULL;
1197   /* If the node is abstract and needed, then do not clear DECL_INITIAL
1198      of its associated function function declaration because it's
1199      needed to emit debug info later.  */
1200   if (!node->abstract_and_needed)
1201     DECL_INITIAL (node->decl) = error_mark_node;
1202 }
1203
1204 /* Remove same body alias node.  */
1205
1206 void
1207 cgraph_remove_same_body_alias (struct cgraph_node *node)
1208 {
1209   void **slot;
1210   int uid = node->uid;
1211
1212   gcc_assert (node->same_body_alias);
1213   if (node->previous)
1214     node->previous->next = node->next;
1215   else
1216     node->same_body->same_body = node->next;
1217   if (node->next)
1218     node->next->previous = node->previous;
1219   node->next = NULL;
1220   node->previous = NULL;
1221   slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1222   if (*slot == node)
1223     htab_clear_slot (cgraph_hash, slot);
1224   if (assembler_name_hash)
1225     {
1226       tree name = DECL_ASSEMBLER_NAME (node->decl);
1227       slot = htab_find_slot_with_hash (assembler_name_hash, name,
1228                                        decl_assembler_name_hash (name),
1229                                        NO_INSERT);
1230       if (slot && *slot == node)
1231         htab_clear_slot (assembler_name_hash, slot);
1232     }
1233
1234   /* Clear out the node to NULL all pointers and add the node to the free
1235      list.  */
1236   memset (node, 0, sizeof(*node));
1237   node->uid = uid;
1238   NEXT_FREE_NODE (node) = free_nodes;
1239   free_nodes = node;
1240 }
1241
1242 /* Remove the node from cgraph.  */
1243
1244 void
1245 cgraph_remove_node (struct cgraph_node *node)
1246 {
1247   void **slot;
1248   bool kill_body = false;
1249   struct cgraph_node *n;
1250   int uid = node->uid;
1251
1252   cgraph_call_node_removal_hooks (node);
1253   cgraph_node_remove_callers (node);
1254   cgraph_node_remove_callees (node);
1255   VEC_free (ipa_opt_pass, heap,
1256             node->ipa_transforms_to_apply);
1257
1258   /* Incremental inlining access removed nodes stored in the postorder list.
1259      */
1260   node->needed = node->reachable = false;
1261   for (n = node->nested; n; n = n->next_nested)
1262     n->origin = NULL;
1263   node->nested = NULL;
1264   if (node->origin)
1265     {
1266       struct cgraph_node **node2 = &node->origin->nested;
1267
1268       while (*node2 != node)
1269         node2 = &(*node2)->next_nested;
1270       *node2 = node->next_nested;
1271     }
1272   if (node->previous)
1273     node->previous->next = node->next;
1274   else
1275     cgraph_nodes = node->next;
1276   if (node->next)
1277     node->next->previous = node->previous;
1278   node->next = NULL;
1279   node->previous = NULL;
1280   slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1281   if (*slot == node)
1282     {
1283       struct cgraph_node *next_inline_clone;
1284
1285       for (next_inline_clone = node->clones;
1286            next_inline_clone && next_inline_clone->decl != node->decl;
1287            next_inline_clone = next_inline_clone->next_sibling_clone)
1288         ;
1289
1290       /* If there is inline clone of the node being removed, we need
1291          to put it into the position of removed node and reorganize all
1292          other clones to be based on it.  */
1293       if (next_inline_clone)
1294         {
1295           struct cgraph_node *n;
1296           struct cgraph_node *new_clones;
1297
1298           *slot = next_inline_clone;
1299
1300           /* Unlink inline clone from the list of clones of removed node.  */
1301           if (next_inline_clone->next_sibling_clone)
1302             next_inline_clone->next_sibling_clone->prev_sibling_clone
1303               = next_inline_clone->prev_sibling_clone;
1304           if (next_inline_clone->prev_sibling_clone)
1305             {
1306               next_inline_clone->prev_sibling_clone->next_sibling_clone
1307                 = next_inline_clone->next_sibling_clone;
1308             }
1309           else
1310            node->clones = next_inline_clone->next_sibling_clone;
1311
1312           new_clones = node->clones;
1313           node->clones = NULL;
1314
1315           /* Copy clone info.  */
1316           next_inline_clone->clone = node->clone;
1317
1318           /* Now place it into clone tree at same level at NODE.  */
1319           next_inline_clone->clone_of = node->clone_of;
1320           next_inline_clone->prev_sibling_clone = NULL;
1321           next_inline_clone->next_sibling_clone = NULL;
1322           if (node->clone_of)
1323             {
1324               next_inline_clone->next_sibling_clone = node->clone_of->clones;
1325               node->clone_of->clones = next_inline_clone;
1326             }
1327
1328           /* Merge the clone list.  */
1329           if (new_clones)
1330             {
1331               if (!next_inline_clone->clones)
1332                 next_inline_clone->clones = new_clones;
1333               else
1334                 {
1335                   n = next_inline_clone->clones;
1336                   while (n->next_sibling_clone)
1337                     n =  n->next_sibling_clone;
1338                   n->next_sibling_clone = new_clones;
1339                   new_clones->prev_sibling_clone = n;
1340                 }
1341             }
1342
1343           /* Update clone_of pointers.  */
1344           n = new_clones;
1345           while (n)
1346             {
1347               n->clone_of = next_inline_clone;
1348               n = n->next_sibling_clone;
1349             }
1350         }
1351       else
1352         {
1353           htab_clear_slot (cgraph_hash, slot);
1354           kill_body = true;
1355         }
1356
1357     }
1358   else
1359     gcc_assert (node->clone_of);
1360   if (node->prev_sibling_clone)
1361     node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1362   else if (node->clone_of)
1363     node->clone_of->clones = node->next_sibling_clone;
1364   if (node->next_sibling_clone)
1365     node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1366   if (node->clones)
1367     {
1368       struct cgraph_node *n;
1369
1370       for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1371         n->clone_of = node->clone_of;
1372       n->clone_of = node->clone_of;
1373       n->next_sibling_clone = node->clone_of->clones;
1374       if (node->clone_of->clones)
1375         node->clone_of->clones->prev_sibling_clone = n;
1376       node->clone_of->clones = node->clones;
1377     }
1378
1379   while (node->same_body)
1380     cgraph_remove_same_body_alias (node->same_body);
1381
1382   /* While all the clones are removed after being proceeded, the function
1383      itself is kept in the cgraph even after it is compiled.  Check whether
1384      we are done with this body and reclaim it proactively if this is the case.
1385      */
1386   if (!kill_body && *slot)
1387     {
1388       struct cgraph_node *n = (struct cgraph_node *) *slot;
1389       if (!n->clones && !n->clone_of && !n->global.inlined_to
1390           && (cgraph_global_info_ready
1391               && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl))))
1392         kill_body = true;
1393     }
1394   if (assembler_name_hash)
1395     {
1396       tree name = DECL_ASSEMBLER_NAME (node->decl);
1397       slot = htab_find_slot_with_hash (assembler_name_hash, name,
1398                                        decl_assembler_name_hash (name),
1399                                        NO_INSERT);
1400       /* Inline clones are not hashed.  */
1401       if (slot && *slot == node)
1402         htab_clear_slot (assembler_name_hash, slot);
1403     }
1404
1405   if (kill_body)
1406     cgraph_release_function_body (node);
1407   node->decl = NULL;
1408   if (node->call_site_hash)
1409     {
1410       htab_delete (node->call_site_hash);
1411       node->call_site_hash = NULL;
1412     }
1413   cgraph_n_nodes--;
1414
1415   /* Clear out the node to NULL all pointers and add the node to the free
1416      list.  */
1417   memset (node, 0, sizeof(*node));
1418   node->uid = uid;
1419   NEXT_FREE_NODE (node) = free_nodes;
1420   free_nodes = node;
1421 }
1422
1423 /* Remove the node from cgraph.  */
1424
1425 void
1426 cgraph_remove_node_and_inline_clones (struct cgraph_node *node)
1427 {
1428   struct cgraph_edge *e, *next;
1429   for (e = node->callees; e; e = next)
1430     {
1431       next = e->next_callee;
1432       if (!e->inline_failed)
1433         cgraph_remove_node_and_inline_clones (e->callee);
1434     }
1435   cgraph_remove_node (node);
1436 }
1437
1438 /* Notify finalize_compilation_unit that given node is reachable.  */
1439
1440 void
1441 cgraph_mark_reachable_node (struct cgraph_node *node)
1442 {
1443   if (!node->reachable && node->local.finalized)
1444     {
1445       notice_global_symbol (node->decl);
1446       node->reachable = 1;
1447       gcc_assert (!cgraph_global_info_ready);
1448
1449       node->next_needed = cgraph_nodes_queue;
1450       cgraph_nodes_queue = node;
1451     }
1452 }
1453
1454 /* Likewise indicate that a node is needed, i.e. reachable via some
1455    external means.  */
1456
1457 void
1458 cgraph_mark_needed_node (struct cgraph_node *node)
1459 {
1460   node->needed = 1;
1461   gcc_assert (!node->global.inlined_to);
1462   cgraph_mark_reachable_node (node);
1463 }
1464
1465 /* Likewise indicate that a node is having address taken.  */
1466
1467 void
1468 cgraph_mark_address_taken_node (struct cgraph_node *node)
1469 {
1470   node->address_taken = 1;
1471   cgraph_mark_needed_node (node);
1472 }
1473
1474 /* Return local info for the compiled function.  */
1475
1476 struct cgraph_local_info *
1477 cgraph_local_info (tree decl)
1478 {
1479   struct cgraph_node *node;
1480
1481   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1482   node = cgraph_node (decl);
1483   return &node->local;
1484 }
1485
1486 /* Return local info for the compiled function.  */
1487
1488 struct cgraph_global_info *
1489 cgraph_global_info (tree decl)
1490 {
1491   struct cgraph_node *node;
1492
1493   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1494   node = cgraph_node (decl);
1495   return &node->global;
1496 }
1497
1498 /* Return local info for the compiled function.  */
1499
1500 struct cgraph_rtl_info *
1501 cgraph_rtl_info (tree decl)
1502 {
1503   struct cgraph_node *node;
1504
1505   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1506   node = cgraph_node (decl);
1507   if (decl != current_function_decl
1508       && !TREE_ASM_WRITTEN (node->decl))
1509     return NULL;
1510   return &node->rtl;
1511 }
1512
1513 /* Return a string describing the failure REASON.  */
1514
1515 const char*
1516 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1517 {
1518 #undef DEFCIFCODE
1519 #define DEFCIFCODE(code, string)        string,
1520
1521   static const char *cif_string_table[CIF_N_REASONS] = {
1522 #include "cif-code.def"
1523   };
1524
1525   /* Signedness of an enum type is implementation defined, so cast it
1526      to unsigned before testing. */
1527   gcc_assert ((unsigned) reason < CIF_N_REASONS);
1528   return cif_string_table[reason];
1529 }
1530
1531 /* Return name of the node used in debug output.  */
1532 const char *
1533 cgraph_node_name (struct cgraph_node *node)
1534 {
1535   return lang_hooks.decl_printable_name (node->decl, 2);
1536 }
1537
1538 /* Names used to print out the availability enum.  */
1539 const char * const cgraph_availability_names[] =
1540   {"unset", "not_available", "overwritable", "available", "local"};
1541
1542
1543 /* Dump call graph node NODE to file F.  */
1544
1545 void
1546 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1547 {
1548   struct cgraph_edge *edge;
1549   fprintf (f, "%s/%i(%i)", cgraph_node_name (node), node->uid,
1550            node->pid);
1551   dump_addr (f, " @", (void *)node);
1552   if (node->global.inlined_to)
1553     fprintf (f, " (inline copy in %s/%i)",
1554              cgraph_node_name (node->global.inlined_to),
1555              node->global.inlined_to->uid);
1556   if (node->clone_of)
1557     fprintf (f, " (clone of %s/%i)",
1558              cgraph_node_name (node->clone_of),
1559              node->clone_of->uid);
1560   if (cgraph_function_flags_ready)
1561     fprintf (f, " availability:%s",
1562              cgraph_availability_names [cgraph_function_body_availability (node)]);
1563   if (node->count)
1564     fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1565              (HOST_WIDEST_INT)node->count);
1566   if (node->local.inline_summary.self_time)
1567     fprintf (f, " %i time, %i benefit", node->local.inline_summary.self_time,
1568                                         node->local.inline_summary.time_inlining_benefit);
1569   if (node->global.time && node->global.time
1570       != node->local.inline_summary.self_time)
1571     fprintf (f, " (%i after inlining)", node->global.time);
1572   if (node->local.inline_summary.self_size)
1573     fprintf (f, " %i size, %i benefit", node->local.inline_summary.self_size,
1574                                         node->local.inline_summary.size_inlining_benefit);
1575   if (node->global.size && node->global.size
1576       != node->local.inline_summary.self_size)
1577     fprintf (f, " (%i after inlining)", node->global.size);
1578   if (node->local.inline_summary.estimated_self_stack_size)
1579     fprintf (f, " %i bytes stack usage", (int)node->local.inline_summary.estimated_self_stack_size);
1580   if (node->global.estimated_stack_size != node->local.inline_summary.estimated_self_stack_size)
1581     fprintf (f, " %i bytes after inlining", (int)node->global.estimated_stack_size);
1582   if (node->origin)
1583     fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
1584   if (node->needed)
1585     fprintf (f, " needed");
1586   if (node->address_taken)
1587     fprintf (f, " address_taken");
1588   else if (node->reachable)
1589     fprintf (f, " reachable");
1590   if (gimple_has_body_p (node->decl))
1591     fprintf (f, " body");
1592   if (node->process)
1593     fprintf (f, " process");
1594   if (node->local.local)
1595     fprintf (f, " local");
1596   if (node->local.externally_visible)
1597     fprintf (f, " externally_visible");
1598   if (node->local.finalized)
1599     fprintf (f, " finalized");
1600   if (node->local.disregard_inline_limits)
1601     fprintf (f, " always_inline");
1602   else if (node->local.inlinable)
1603     fprintf (f, " inlinable");
1604   if (node->local.redefined_extern_inline)
1605     fprintf (f, " redefined_extern_inline");
1606   if (TREE_ASM_WRITTEN (node->decl))
1607     fprintf (f, " asm_written");
1608
1609   fprintf (f, "\n  called by: ");
1610   for (edge = node->callers; edge; edge = edge->next_caller)
1611     {
1612       fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
1613                edge->caller->uid);
1614       if (edge->count)
1615         fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1616                  (HOST_WIDEST_INT)edge->count);
1617       if (edge->frequency)
1618         fprintf (f, "(%.2f per call) ",
1619                  edge->frequency / (double)CGRAPH_FREQ_BASE);
1620       if (!edge->inline_failed)
1621         fprintf(f, "(inlined) ");
1622       if (edge->indirect_call)
1623         fprintf(f, "(indirect) ");
1624       if (edge->can_throw_external)
1625         fprintf(f, "(can throw external) ");
1626     }
1627
1628   fprintf (f, "\n  calls: ");
1629   for (edge = node->callees; edge; edge = edge->next_callee)
1630     {
1631       fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
1632                edge->callee->uid);
1633       if (!edge->inline_failed)
1634         fprintf(f, "(inlined) ");
1635       if (edge->indirect_call)
1636         fprintf(f, "(indirect) ");
1637       if (edge->count)
1638         fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1639                  (HOST_WIDEST_INT)edge->count);
1640       if (edge->frequency)
1641         fprintf (f, "(%.2f per call) ",
1642                  edge->frequency / (double)CGRAPH_FREQ_BASE);
1643       if (edge->loop_nest)
1644         fprintf (f, "(nested in %i loops) ", edge->loop_nest);
1645       if (edge->can_throw_external)
1646         fprintf(f, "(can throw external) ");
1647     }
1648   fprintf (f, "\n");
1649 }
1650
1651
1652 /* Dump call graph node NODE to stderr.  */
1653
1654 void
1655 debug_cgraph_node (struct cgraph_node *node)
1656 {
1657   dump_cgraph_node (stderr, node);
1658 }
1659
1660
1661 /* Dump the callgraph to file F.  */
1662
1663 void
1664 dump_cgraph (FILE *f)
1665 {
1666   struct cgraph_node *node;
1667
1668   fprintf (f, "callgraph:\n\n");
1669   for (node = cgraph_nodes; node; node = node->next)
1670     dump_cgraph_node (f, node);
1671 }
1672
1673
1674 /* Dump the call graph to stderr.  */
1675
1676 void
1677 debug_cgraph (void)
1678 {
1679   dump_cgraph (stderr);
1680 }
1681
1682
1683 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables.  */
1684
1685 void
1686 change_decl_assembler_name (tree decl, tree name)
1687 {
1688   gcc_assert (!assembler_name_hash);
1689   if (!DECL_ASSEMBLER_NAME_SET_P (decl))
1690     {
1691       SET_DECL_ASSEMBLER_NAME (decl, name);
1692       return;
1693     }
1694   if (name == DECL_ASSEMBLER_NAME (decl))
1695     return;
1696
1697   if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
1698       && DECL_RTL_SET_P (decl))
1699     warning (0, "%D renamed after being referenced in assembly", decl);
1700
1701   SET_DECL_ASSEMBLER_NAME (decl, name);
1702 }
1703
1704 /* Add a top-level asm statement to the list.  */
1705
1706 struct cgraph_asm_node *
1707 cgraph_add_asm_node (tree asm_str)
1708 {
1709   struct cgraph_asm_node *node;
1710
1711   node = GGC_CNEW (struct cgraph_asm_node);
1712   node->asm_str = asm_str;
1713   node->order = cgraph_order++;
1714   node->next = NULL;
1715   if (cgraph_asm_nodes == NULL)
1716     cgraph_asm_nodes = node;
1717   else
1718     cgraph_asm_last_node->next = node;
1719   cgraph_asm_last_node = node;
1720   return node;
1721 }
1722
1723 /* Return true when the DECL can possibly be inlined.  */
1724 bool
1725 cgraph_function_possibly_inlined_p (tree decl)
1726 {
1727   if (!cgraph_global_info_ready)
1728     return !DECL_UNINLINABLE (decl);
1729   return DECL_POSSIBLY_INLINED (decl);
1730 }
1731
1732 /* Create clone of E in the node N represented by CALL_EXPR the callgraph.  */
1733 struct cgraph_edge *
1734 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
1735                    gimple call_stmt, unsigned stmt_uid, gcov_type count_scale,
1736                    int freq_scale, int loop_nest, bool update_original)
1737 {
1738   struct cgraph_edge *new_edge;
1739   gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
1740   gcov_type freq;
1741
1742   /* We do not want to ignore loop nest after frequency drops to 0.  */
1743   if (!freq_scale)
1744     freq_scale = 1;
1745   freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
1746   if (freq > CGRAPH_FREQ_MAX)
1747     freq = CGRAPH_FREQ_MAX;
1748   new_edge = cgraph_create_edge (n, e->callee, call_stmt, count, freq,
1749                             e->loop_nest + loop_nest);
1750
1751   new_edge->inline_failed = e->inline_failed;
1752   new_edge->indirect_call = e->indirect_call;
1753   new_edge->lto_stmt_uid = stmt_uid;
1754   if (update_original)
1755     {
1756       e->count -= new_edge->count;
1757       if (e->count < 0)
1758         e->count = 0;
1759     }
1760   cgraph_call_edge_duplication_hooks (e, new_edge);
1761   return new_edge;
1762 }
1763
1764 /* Create node representing clone of N executed COUNT times.  Decrease
1765    the execution counts from original node too.
1766
1767    When UPDATE_ORIGINAL is true, the counts are subtracted from the original
1768    function's profile to reflect the fact that part of execution is handled
1769    by node.  */
1770 struct cgraph_node *
1771 cgraph_clone_node (struct cgraph_node *n, gcov_type count, int freq,
1772                    int loop_nest, bool update_original,
1773                    VEC(cgraph_edge_p,heap) *redirect_callers)
1774 {
1775   struct cgraph_node *new_node = cgraph_create_node ();
1776   struct cgraph_edge *e;
1777   gcov_type count_scale;
1778   unsigned i;
1779
1780   new_node->decl = n->decl;
1781   new_node->origin = n->origin;
1782   if (new_node->origin)
1783     {
1784       new_node->next_nested = new_node->origin->nested;
1785       new_node->origin->nested = new_node;
1786     }
1787   new_node->analyzed = n->analyzed;
1788   new_node->local = n->local;
1789   new_node->local.externally_visible = false;
1790   new_node->global = n->global;
1791   new_node->rtl = n->rtl;
1792   new_node->count = count;
1793   new_node->clone = n->clone;
1794   if (n->count)
1795     {
1796       if (new_node->count > n->count)
1797         count_scale = REG_BR_PROB_BASE;
1798       else
1799         count_scale = new_node->count * REG_BR_PROB_BASE / n->count;
1800     }
1801   else
1802     count_scale = 0;
1803   if (update_original)
1804     {
1805       n->count -= count;
1806       if (n->count < 0)
1807         n->count = 0;
1808     }
1809
1810   for (i = 0; VEC_iterate (cgraph_edge_p, redirect_callers, i, e); i++)
1811     {
1812       /* Redirect calls to the old version node to point to its new
1813          version.  */
1814       cgraph_redirect_edge_callee (e, new_node);
1815     }
1816
1817
1818   for (e = n->callees;e; e=e->next_callee)
1819     cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
1820                        count_scale, freq, loop_nest, update_original);
1821
1822   new_node->next_sibling_clone = n->clones;
1823   if (n->clones)
1824     n->clones->prev_sibling_clone = new_node;
1825   n->clones = new_node;
1826   new_node->clone_of = n;
1827
1828   cgraph_call_node_duplication_hooks (n, new_node);
1829   return new_node;
1830 }
1831
1832 /* Create a new name for omp child function.  Returns an identifier.  */
1833
1834 static GTY(()) unsigned int clone_fn_id_num;
1835
1836 static tree
1837 clone_function_name (tree decl)
1838 {
1839   tree name = DECL_ASSEMBLER_NAME (decl);
1840   size_t len = IDENTIFIER_LENGTH (name);
1841   char *tmp_name, *prefix;
1842
1843   prefix = XALLOCAVEC (char, len + strlen ("_clone") + 1);
1844   memcpy (prefix, IDENTIFIER_POINTER (name), len);
1845   strcpy (prefix + len, "_clone");
1846 #ifndef NO_DOT_IN_LABEL
1847   prefix[len] = '.';
1848 #elif !defined NO_DOLLAR_IN_LABEL
1849   prefix[len] = '$';
1850 #endif
1851   ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++);
1852   return get_identifier (tmp_name);
1853 }
1854
1855 /* Create callgraph node clone with new declaration.  The actual body will
1856    be copied later at compilation stage.  
1857
1858    TODO: after merging in ipa-sra use function call notes instead of args_to_skip
1859    bitmap interface.
1860    */
1861 struct cgraph_node *
1862 cgraph_create_virtual_clone (struct cgraph_node *old_node,
1863                              VEC(cgraph_edge_p,heap) *redirect_callers,
1864                              VEC(ipa_replace_map_p,gc) *tree_map,
1865                              bitmap args_to_skip)
1866 {
1867   tree old_decl = old_node->decl;
1868   struct cgraph_node *new_node = NULL;
1869   tree new_decl;
1870   struct cgraph_node key, **slot;
1871
1872   gcc_assert  (tree_versionable_function_p (old_decl));
1873
1874   /* Make a new FUNCTION_DECL tree node */
1875   if (!args_to_skip)
1876     new_decl = copy_node (old_decl);
1877   else
1878     new_decl = build_function_decl_skip_args (old_decl, args_to_skip);
1879   DECL_STRUCT_FUNCTION (new_decl) = NULL;
1880
1881   /* Generate a new name for the new version. */
1882   DECL_NAME (new_decl) = clone_function_name (old_decl);
1883   SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1884   SET_DECL_RTL (new_decl, NULL);
1885
1886   new_node = cgraph_clone_node (old_node, old_node->count,
1887                                 CGRAPH_FREQ_BASE, 0, false,
1888                                 redirect_callers);
1889   new_node->decl = new_decl;
1890   /* Update the properties.
1891      Make clone visible only within this translation unit.  Make sure
1892      that is not weak also.
1893      ??? We cannot use COMDAT linkage because there is no
1894      ABI support for this.  */
1895   DECL_EXTERNAL (new_node->decl) = 0;
1896   DECL_COMDAT_GROUP (new_node->decl) = 0;
1897   TREE_PUBLIC (new_node->decl) = 0;
1898   DECL_COMDAT (new_node->decl) = 0;
1899   DECL_WEAK (new_node->decl) = 0;
1900   new_node->clone.tree_map = tree_map;
1901   new_node->clone.args_to_skip = args_to_skip;
1902   if (!args_to_skip)
1903     new_node->clone.combined_args_to_skip = old_node->clone.combined_args_to_skip;
1904   else if (old_node->clone.combined_args_to_skip)
1905     {
1906       int newi = 0, oldi = 0;
1907       tree arg;
1908       bitmap new_args_to_skip = BITMAP_GGC_ALLOC ();
1909       struct cgraph_node *orig_node;
1910       for (orig_node = old_node; orig_node->clone_of; orig_node = orig_node->clone_of)
1911         ;
1912       for (arg = DECL_ARGUMENTS (orig_node->decl); arg; arg = TREE_CHAIN (arg), oldi++)
1913         {
1914           if (bitmap_bit_p (old_node->clone.combined_args_to_skip, oldi))
1915             {
1916               bitmap_set_bit (new_args_to_skip, oldi);
1917               continue;
1918             }
1919           if (bitmap_bit_p (args_to_skip, newi))
1920             bitmap_set_bit (new_args_to_skip, oldi);
1921           newi++;
1922         }
1923       new_node->clone.combined_args_to_skip = new_args_to_skip;
1924     }
1925   else
1926     new_node->clone.combined_args_to_skip = args_to_skip;
1927   new_node->local.externally_visible = 0;
1928   new_node->local.local = 1;
1929   new_node->lowered = true;
1930   new_node->reachable = true;
1931
1932   key.decl = new_decl;
1933   slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
1934   gcc_assert (!*slot);
1935   *slot = new_node;
1936   if (assembler_name_hash)
1937     {
1938       void **aslot;
1939       tree name = DECL_ASSEMBLER_NAME (new_decl);
1940
1941       aslot = htab_find_slot_with_hash (assembler_name_hash, name,
1942                                         decl_assembler_name_hash (name),
1943                                         INSERT);
1944       gcc_assert (!*aslot);
1945       *aslot = new_node;
1946     }
1947
1948   return new_node;
1949 }
1950
1951 /* NODE is no longer nested function; update cgraph accordingly.  */
1952 void
1953 cgraph_unnest_node (struct cgraph_node *node)
1954 {
1955   struct cgraph_node **node2 = &node->origin->nested;
1956   gcc_assert (node->origin);
1957
1958   while (*node2 != node)
1959     node2 = &(*node2)->next_nested;
1960   *node2 = node->next_nested;
1961   node->origin = NULL;
1962 }
1963
1964 /* Return function availability.  See cgraph.h for description of individual
1965    return values.  */
1966 enum availability
1967 cgraph_function_body_availability (struct cgraph_node *node)
1968 {
1969   enum availability avail;
1970   gcc_assert (cgraph_function_flags_ready);
1971   if (!node->analyzed)
1972     avail = AVAIL_NOT_AVAILABLE;
1973   else if (node->local.local)
1974     avail = AVAIL_LOCAL;
1975   else if (!node->local.externally_visible)
1976     avail = AVAIL_AVAILABLE;
1977   /* Inline functions are safe to be analyzed even if their sybol can
1978      be overwritten at runtime.  It is not meaningful to enfore any sane
1979      behaviour on replacing inline function by different body.  */
1980   else if (DECL_DECLARED_INLINE_P (node->decl))
1981     avail = AVAIL_AVAILABLE;
1982
1983   /* If the function can be overwritten, return OVERWRITABLE.  Take
1984      care at least of two notable extensions - the COMDAT functions
1985      used to share template instantiations in C++ (this is symmetric
1986      to code cp_cannot_inline_tree_fn and probably shall be shared and
1987      the inlinability hooks completely eliminated).
1988
1989      ??? Does the C++ one definition rule allow us to always return
1990      AVAIL_AVAILABLE here?  That would be good reason to preserve this
1991      bit.  */
1992
1993   else if (DECL_REPLACEABLE_P (node->decl) && !DECL_EXTERNAL (node->decl))
1994     avail = AVAIL_OVERWRITABLE;
1995   else avail = AVAIL_AVAILABLE;
1996
1997   return avail;
1998 }
1999
2000 /* Add the function FNDECL to the call graph.
2001    Unlike cgraph_finalize_function, this function is intended to be used
2002    by middle end and allows insertion of new function at arbitrary point
2003    of compilation.  The function can be either in high, low or SSA form
2004    GIMPLE.
2005
2006    The function is assumed to be reachable and have address taken (so no
2007    API breaking optimizations are performed on it).  
2008
2009    Main work done by this function is to enqueue the function for later
2010    processing to avoid need the passes to be re-entrant.  */
2011
2012 void
2013 cgraph_add_new_function (tree fndecl, bool lowered)
2014 {
2015   struct cgraph_node *node;
2016   switch (cgraph_state)
2017     {
2018       case CGRAPH_STATE_CONSTRUCTION:
2019         /* Just enqueue function to be processed at nearest occurrence.  */
2020         node = cgraph_node (fndecl);
2021         node->next_needed = cgraph_new_nodes;
2022         if (lowered)
2023           node->lowered = true;
2024         cgraph_new_nodes = node;
2025         break;
2026
2027       case CGRAPH_STATE_IPA:
2028       case CGRAPH_STATE_IPA_SSA:
2029       case CGRAPH_STATE_EXPANSION:
2030         /* Bring the function into finalized state and enqueue for later
2031            analyzing and compilation.  */
2032         node = cgraph_node (fndecl);
2033         node->local.local = false;
2034         node->local.finalized = true;
2035         node->reachable = node->needed = true;
2036         if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
2037           {
2038             push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2039             current_function_decl = fndecl;
2040             gimple_register_cfg_hooks ();
2041             tree_lowering_passes (fndecl);
2042             bitmap_obstack_initialize (NULL);
2043             if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2044               execute_pass_list (pass_early_local_passes.pass.sub);
2045             bitmap_obstack_release (NULL);
2046             pop_cfun ();
2047             current_function_decl = NULL;
2048
2049             lowered = true;
2050           }
2051         if (lowered)
2052           node->lowered = true;
2053         node->next_needed = cgraph_new_nodes;
2054         cgraph_new_nodes = node;
2055         break;
2056
2057       case CGRAPH_STATE_FINISHED:
2058         /* At the very end of compilation we have to do all the work up
2059            to expansion.  */
2060         push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2061         current_function_decl = fndecl;
2062         gimple_register_cfg_hooks ();
2063         if (!lowered)
2064           tree_lowering_passes (fndecl);
2065         bitmap_obstack_initialize (NULL);
2066         if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2067           execute_pass_list (pass_early_local_passes.pass.sub);
2068         bitmap_obstack_release (NULL);
2069         tree_rest_of_compilation (fndecl);
2070         pop_cfun ();
2071         current_function_decl = NULL;
2072         break;
2073     }
2074
2075   /* Set a personality if required and we already passed EH lowering.  */
2076   if (lowered
2077       && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
2078           == eh_personality_lang))
2079     DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
2080 }
2081
2082 /* Return true if NODE can be made local for API change.
2083    Extern inline functions and C++ COMDAT functions can be made local
2084    at the expense of possible code size growth if function is used in multiple
2085    compilation units.  */
2086 bool
2087 cgraph_node_can_be_local_p (struct cgraph_node *node)
2088 {
2089   return (!node->needed
2090           && (DECL_COMDAT (node->decl) || !node->local.externally_visible));
2091 }
2092
2093 /* Bring NODE local.  */
2094 void
2095 cgraph_make_node_local (struct cgraph_node *node)
2096 {
2097   gcc_assert (cgraph_node_can_be_local_p (node));
2098   if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2099     {
2100       DECL_COMDAT (node->decl) = 0;
2101       DECL_COMDAT_GROUP (node->decl) = 0;
2102       TREE_PUBLIC (node->decl) = 0;
2103       DECL_WEAK (node->decl) = 0;
2104       DECL_EXTERNAL (node->decl) = 0;
2105       node->local.externally_visible = false;
2106       node->local.local = true;
2107       gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2108     }
2109 }
2110
2111 #include "gt-cgraph.h"