OSDN Git Service

/c-family
[pf3gnuchains/gcc-fork.git] / gcc / cgraph.c
1 /* Callgraph handling code.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 all indirect calls or calls
38     from other compilation units.  Flag NEEDED is set for each node that may be
39     accessed in such an invisible way and it shall be considered an entry point
40     to the callgraph.
41
42     On the other hand, the callgraph currently does contain some edges for
43     indirect calls with unknown callees which can be accessed through
44     indirect_calls field of a node.  It should be noted however that at the
45     moment only calls which are potential candidates for indirect inlining are
46     added there.
47
48     Interprocedural information:
49
50       Callgraph is place to store data needed for interprocedural optimization.
51       All data structures are divided into three components: local_info that
52       is produced while analyzing the function, global_info that is result
53       of global walking of the callgraph on the end of compilation and
54       rtl_info used by RTL backend to propagate data from already compiled
55       functions to their callers.
56
57       Moreover, each node has a uid which can be used to keep information in
58       on-the-side arrays.  UIDs are reused and therefore reasonably dense.
59
60     Inlining plans:
61
62       The function inlining information is decided in advance and maintained
63       in the callgraph as so called inline plan.
64       For each inlined call, the callee's node is cloned to represent the
65       new function copy produced by inliner.
66       Each inlined call gets a unique corresponding clone node of the callee
67       and the data structure is updated while inlining is performed, so
68       the clones are eliminated and their callee edges redirected to the
69       caller.
70
71       Each edge has "inline_failed" field.  When the field is set to NULL,
72       the call will be inlined.  When it is non-NULL it contains a reason
73       why inlining wasn't performed.  */
74
75 #include "config.h"
76 #include "system.h"
77 #include "coretypes.h"
78 #include "tm.h"
79 #include "tree.h"
80 #include "tree-inline.h"
81 #include "langhooks.h"
82 #include "hashtab.h"
83 #include "toplev.h"
84 #include "flags.h"
85 #include "ggc.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "basic-block.h"
89 #include "cgraph.h"
90 #include "output.h"
91 #include "intl.h"
92 #include "gimple.h"
93 #include "tree-dump.h"
94 #include "tree-flow.h"
95 #include "value-prof.h"
96 #include "except.h"
97 #include "diagnostic-core.h"
98 #include "rtl.h"
99 #include "ipa-utils.h"
100 #include "lto-streamer.h"
101 #include "ipa-inline.h"
102
103 const char * const ld_plugin_symbol_resolution_names[]=
104 {
105   "",
106   "undef",
107   "prevailing_def",
108   "prevailing_def_ironly",
109   "preempted_reg",
110   "preempted_ir",
111   "resolved_ir",
112   "resolved_exec",
113   "resolved_dyn"
114 };
115
116 static void cgraph_node_remove_callers (struct cgraph_node *node);
117 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
118 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
119
120 /* Hash table used to convert declarations into nodes.  */
121 static GTY((param_is (struct cgraph_node))) htab_t cgraph_hash;
122 /* Hash table used to convert assembler names into nodes.  */
123 static GTY((param_is (struct cgraph_node))) htab_t assembler_name_hash;
124
125 /* The linked list of cgraph nodes.  */
126 struct cgraph_node *cgraph_nodes;
127
128 /* Queue of cgraph nodes scheduled to be lowered.  */
129 struct cgraph_node *cgraph_nodes_queue;
130
131 /* Queue of cgraph nodes scheduled to be added into cgraph.  This is a
132    secondary queue used during optimization to accommodate passes that
133    may generate new functions that need to be optimized and expanded.  */
134 struct cgraph_node *cgraph_new_nodes;
135
136 /* Number of nodes in existence.  */
137 int cgraph_n_nodes;
138
139 /* Maximal uid used in cgraph nodes.  */
140 int cgraph_max_uid;
141
142 /* Maximal uid used in cgraph edges.  */
143 int cgraph_edge_max_uid;
144
145 /* Set when whole unit has been analyzed so we can access global info.  */
146 bool cgraph_global_info_ready = false;
147
148 /* What state callgraph is in right now.  */
149 enum cgraph_state cgraph_state = CGRAPH_STATE_CONSTRUCTION;
150
151 /* Set when the cgraph is fully build and the basic flags are computed.  */
152 bool cgraph_function_flags_ready = false;
153
154 /* Linked list of cgraph asm nodes.  */
155 struct cgraph_asm_node *cgraph_asm_nodes;
156
157 /* Last node in cgraph_asm_nodes.  */
158 static GTY(()) struct cgraph_asm_node *cgraph_asm_last_node;
159
160 /* The order index of the next cgraph node to be created.  This is
161    used so that we can sort the cgraph nodes in order by when we saw
162    them, to support -fno-toplevel-reorder.  */
163 int cgraph_order;
164
165 /* List of hooks triggered on cgraph_edge events.  */
166 struct cgraph_edge_hook_list {
167   cgraph_edge_hook hook;
168   void *data;
169   struct cgraph_edge_hook_list *next;
170 };
171
172 /* List of hooks triggered on cgraph_node events.  */
173 struct cgraph_node_hook_list {
174   cgraph_node_hook hook;
175   void *data;
176   struct cgraph_node_hook_list *next;
177 };
178
179 /* List of hooks triggered on events involving two cgraph_edges.  */
180 struct cgraph_2edge_hook_list {
181   cgraph_2edge_hook hook;
182   void *data;
183   struct cgraph_2edge_hook_list *next;
184 };
185
186 /* List of hooks triggered on events involving two cgraph_nodes.  */
187 struct cgraph_2node_hook_list {
188   cgraph_2node_hook hook;
189   void *data;
190   struct cgraph_2node_hook_list *next;
191 };
192
193 /* List of hooks triggered when an edge is removed.  */
194 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
195 /* List of hooks triggered when a node is removed.  */
196 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
197 /* List of hooks triggered when an edge is duplicated.  */
198 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
199 /* List of hooks triggered when a node is duplicated.  */
200 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
201 /* List of hooks triggered when an function is inserted.  */
202 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
203
204 /* Head of a linked list of unused (freed) call graph nodes.
205    Do not GTY((delete)) this list so UIDs gets reliably recycled.  */
206 static GTY(()) struct cgraph_node *free_nodes;
207 /* Head of a linked list of unused (freed) call graph edges.
208    Do not GTY((delete)) this list so UIDs gets reliably recycled.  */
209 static GTY(()) struct cgraph_edge *free_edges;
210
211 /* Macros to access the next item in the list of free cgraph nodes and
212    edges. */
213 #define NEXT_FREE_NODE(NODE) (NODE)->next
214 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
215
216 /* Register HOOK to be called with DATA on each removed edge.  */
217 struct cgraph_edge_hook_list *
218 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
219 {
220   struct cgraph_edge_hook_list *entry;
221   struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
222
223   entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
224   entry->hook = hook;
225   entry->data = data;
226   entry->next = NULL;
227   while (*ptr)
228     ptr = &(*ptr)->next;
229   *ptr = entry;
230   return entry;
231 }
232
233 /* Remove ENTRY from the list of hooks called on removing edges.  */
234 void
235 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
236 {
237   struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
238
239   while (*ptr != entry)
240     ptr = &(*ptr)->next;
241   *ptr = entry->next;
242   free (entry);
243 }
244
245 /* Call all edge removal hooks.  */
246 static void
247 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
248 {
249   struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
250   while (entry)
251   {
252     entry->hook (e, entry->data);
253     entry = entry->next;
254   }
255 }
256
257 /* Register HOOK to be called with DATA on each removed node.  */
258 struct cgraph_node_hook_list *
259 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
260 {
261   struct cgraph_node_hook_list *entry;
262   struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
263
264   entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
265   entry->hook = hook;
266   entry->data = data;
267   entry->next = NULL;
268   while (*ptr)
269     ptr = &(*ptr)->next;
270   *ptr = entry;
271   return entry;
272 }
273
274 /* Remove ENTRY from the list of hooks called on removing nodes.  */
275 void
276 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
277 {
278   struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
279
280   while (*ptr != entry)
281     ptr = &(*ptr)->next;
282   *ptr = entry->next;
283   free (entry);
284 }
285
286 /* Call all node removal hooks.  */
287 static void
288 cgraph_call_node_removal_hooks (struct cgraph_node *node)
289 {
290   struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
291   while (entry)
292   {
293     entry->hook (node, entry->data);
294     entry = entry->next;
295   }
296 }
297
298 /* Register HOOK to be called with DATA on each inserted node.  */
299 struct cgraph_node_hook_list *
300 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
301 {
302   struct cgraph_node_hook_list *entry;
303   struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
304
305   entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
306   entry->hook = hook;
307   entry->data = data;
308   entry->next = NULL;
309   while (*ptr)
310     ptr = &(*ptr)->next;
311   *ptr = entry;
312   return entry;
313 }
314
315 /* Remove ENTRY from the list of hooks called on inserted nodes.  */
316 void
317 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
318 {
319   struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
320
321   while (*ptr != entry)
322     ptr = &(*ptr)->next;
323   *ptr = entry->next;
324   free (entry);
325 }
326
327 /* Call all node insertion hooks.  */
328 void
329 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
330 {
331   struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
332   while (entry)
333   {
334     entry->hook (node, entry->data);
335     entry = entry->next;
336   }
337 }
338
339 /* Register HOOK to be called with DATA on each duplicated edge.  */
340 struct cgraph_2edge_hook_list *
341 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
342 {
343   struct cgraph_2edge_hook_list *entry;
344   struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
345
346   entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
347   entry->hook = hook;
348   entry->data = data;
349   entry->next = NULL;
350   while (*ptr)
351     ptr = &(*ptr)->next;
352   *ptr = entry;
353   return entry;
354 }
355
356 /* Remove ENTRY from the list of hooks called on duplicating edges.  */
357 void
358 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
359 {
360   struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
361
362   while (*ptr != entry)
363     ptr = &(*ptr)->next;
364   *ptr = entry->next;
365   free (entry);
366 }
367
368 /* Call all edge duplication hooks.  */
369 static void
370 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
371                                     struct cgraph_edge *cs2)
372 {
373   struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
374   while (entry)
375   {
376     entry->hook (cs1, cs2, entry->data);
377     entry = entry->next;
378   }
379 }
380
381 /* Register HOOK to be called with DATA on each duplicated node.  */
382 struct cgraph_2node_hook_list *
383 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
384 {
385   struct cgraph_2node_hook_list *entry;
386   struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
387
388   entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
389   entry->hook = hook;
390   entry->data = data;
391   entry->next = NULL;
392   while (*ptr)
393     ptr = &(*ptr)->next;
394   *ptr = entry;
395   return entry;
396 }
397
398 /* Remove ENTRY from the list of hooks called on duplicating nodes.  */
399 void
400 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
401 {
402   struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
403
404   while (*ptr != entry)
405     ptr = &(*ptr)->next;
406   *ptr = entry->next;
407   free (entry);
408 }
409
410 /* Call all node duplication hooks.  */
411 static void
412 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
413                                     struct cgraph_node *node2)
414 {
415   struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
416   while (entry)
417   {
418     entry->hook (node1, node2, entry->data);
419     entry = entry->next;
420   }
421 }
422
423 /* Returns a hash code for P.  */
424
425 static hashval_t
426 hash_node (const void *p)
427 {
428   const struct cgraph_node *n = (const struct cgraph_node *) p;
429   return (hashval_t) DECL_UID (n->decl);
430 }
431
432
433 /* Returns nonzero if P1 and P2 are equal.  */
434
435 static int
436 eq_node (const void *p1, const void *p2)
437 {
438   const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
439   const struct cgraph_node *n2 = (const struct cgraph_node *) p2;
440   return DECL_UID (n1->decl) == DECL_UID (n2->decl);
441 }
442
443 /* Allocate new callgraph node.  */
444
445 static inline struct cgraph_node *
446 cgraph_allocate_node (void)
447 {
448   struct cgraph_node *node;
449
450   if (free_nodes)
451     {
452       node = free_nodes;
453       free_nodes = NEXT_FREE_NODE (node);
454     }
455   else
456     {
457       node = ggc_alloc_cleared_cgraph_node ();
458       node->uid = cgraph_max_uid++;
459     }
460
461   return node;
462 }
463
464 /* Allocate new callgraph node and insert it into basic data structures.  */
465
466 static struct cgraph_node *
467 cgraph_create_node_1 (void)
468 {
469   struct cgraph_node *node = cgraph_allocate_node ();
470
471   node->next = cgraph_nodes;
472   node->order = cgraph_order++;
473   if (cgraph_nodes)
474     cgraph_nodes->previous = node;
475   node->previous = NULL;
476   node->frequency = NODE_FREQUENCY_NORMAL;
477   node->count_materialization_scale = REG_BR_PROB_BASE;
478   ipa_empty_ref_list (&node->ref_list);
479   cgraph_nodes = node;
480   cgraph_n_nodes++;
481   return node;
482 }
483
484 /* Return cgraph node assigned to DECL.  Create new one when needed.  */
485
486 struct cgraph_node *
487 cgraph_create_node (tree decl)
488 {
489   struct cgraph_node key, *node, **slot;
490
491   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
492
493   if (!cgraph_hash)
494     cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
495
496   key.decl = decl;
497   slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
498   gcc_assert (!*slot);
499
500   node = cgraph_create_node_1 ();
501   node->decl = decl;
502   *slot = node;
503   if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
504     {
505       node->origin = cgraph_get_create_node (DECL_CONTEXT (decl));
506       node->next_nested = node->origin->nested;
507       node->origin->nested = node;
508     }
509   if (assembler_name_hash)
510     {
511       void **aslot;
512       tree name = DECL_ASSEMBLER_NAME (decl);
513
514       aslot = htab_find_slot_with_hash (assembler_name_hash, name,
515                                         decl_assembler_name_hash (name),
516                                         INSERT);
517       /* We can have multiple declarations with same assembler name. For C++
518          it is __builtin_strlen and strlen, for instance.  Do we need to
519          record them all?  Original implementation marked just first one
520          so lets hope for the best.  */
521       if (*aslot == NULL)
522         *aslot = node;
523     }
524   return node;
525 }
526
527 /* Try to find a call graph node for declaration DECL and if it does not exist,
528    create it.  */
529
530 struct cgraph_node *
531 cgraph_get_create_node (tree decl)
532 {
533   struct cgraph_node *node;
534
535   node = cgraph_get_node (decl);
536   if (node)
537     return node;
538
539   return cgraph_create_node (decl);
540 }
541
542 /* Mark ALIAS as an alias to DECL.  DECL_NODE is cgraph node representing
543    the function body is associated with (not neccesarily cgraph_node (DECL).  */
544
545 static struct cgraph_node *
546 cgraph_same_body_alias_1 (struct cgraph_node *decl_node, tree alias, tree decl)
547 {
548   struct cgraph_node key, *alias_node, **slot;
549
550   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
551   gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
552
553   key.decl = alias;
554
555   slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
556
557   /* If the cgraph_node has been already created, fail.  */
558   if (*slot)
559     return NULL;
560
561   alias_node = cgraph_allocate_node ();
562   alias_node->decl = alias;
563   alias_node->same_body_alias = 1;
564   alias_node->same_body = decl_node;
565   alias_node->previous = NULL;
566   if (decl_node->same_body)
567     decl_node->same_body->previous = alias_node;
568   alias_node->next = decl_node->same_body;
569   alias_node->thunk.alias = decl;
570   decl_node->same_body = alias_node;
571   *slot = alias_node;
572   return alias_node;
573 }
574
575 /* Attempt to mark ALIAS as an alias to DECL.  Return alias node if successful
576    and NULL otherwise.
577    Same body aliases are output whenever the body of DECL is output,
578    and cgraph_get_node (ALIAS) transparently returns cgraph_get_node (DECL).  */
579
580 struct cgraph_node *
581 cgraph_same_body_alias (struct cgraph_node *decl_node, tree alias, tree decl)
582 {
583 #ifndef ASM_OUTPUT_DEF
584   /* If aliases aren't supported by the assembler, fail.  */
585   return NULL;
586 #endif
587
588   /*gcc_assert (!assembler_name_hash);*/
589
590   return cgraph_same_body_alias_1 (decl_node, alias, decl);
591 }
592
593 /* Add thunk alias into callgraph.  The alias declaration is ALIAS and it
594    aliases DECL with an adjustments made into the first parameter.
595    See comments in thunk_adjust for detail on the parameters.  */
596
597 struct cgraph_node *
598 cgraph_add_thunk (struct cgraph_node *decl_node ATTRIBUTE_UNUSED,
599                   tree alias, tree decl,
600                   bool this_adjusting,
601                   HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value,
602                   tree virtual_offset,
603                   tree real_alias)
604 {
605   struct cgraph_node *node;
606
607   node = cgraph_get_node (alias);
608   if (node)
609     {
610       gcc_assert (node->local.finalized);
611       gcc_assert (!node->same_body);
612       cgraph_remove_node (node);
613     }
614   
615   node = cgraph_create_node (alias);
616   gcc_checking_assert (!virtual_offset
617                        || double_int_equal_p
618                             (tree_to_double_int (virtual_offset),
619                              shwi_to_double_int (virtual_value)));
620   node->thunk.fixed_offset = fixed_offset;
621   node->thunk.this_adjusting = this_adjusting;
622   node->thunk.virtual_value = virtual_value;
623   node->thunk.virtual_offset_p = virtual_offset != NULL;
624   node->thunk.alias = real_alias;
625   node->thunk.thunk_p = true;
626   node->local.finalized = true;
627
628   if (cgraph_decide_is_function_needed (node, decl))
629     cgraph_mark_needed_node (node);
630
631   if ((TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
632       || (DECL_VIRTUAL_P (decl)
633           && (DECL_COMDAT (decl) || DECL_EXTERNAL (decl))))
634     cgraph_mark_reachable_node (node);
635   return node;
636 }
637
638 /* Returns the cgraph node assigned to DECL or NULL if no cgraph node
639    is assigned.  */
640
641 struct cgraph_node *
642 cgraph_get_node_or_alias (const_tree decl)
643 {
644   struct cgraph_node key, *node = NULL, **slot;
645
646   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
647
648   if (!cgraph_hash)
649     return NULL;
650
651   key.decl = CONST_CAST2 (tree, const_tree, decl);
652
653   slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key,
654                                                  NO_INSERT);
655
656   if (slot && *slot)
657     node = *slot;
658   return node;
659 }
660
661 /* Returns the cgraph node assigned to DECL or NULL if no cgraph node
662    is assigned.  */
663
664 struct cgraph_node *
665 cgraph_get_node (const_tree decl)
666 {
667   struct cgraph_node key, *node = NULL, **slot;
668
669   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
670
671   if (!cgraph_hash)
672     return NULL;
673
674   key.decl = CONST_CAST2 (tree, const_tree, decl);
675
676   slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key,
677                                                  NO_INSERT);
678
679   if (slot && *slot)
680     {
681       node = *slot;
682       if (node->same_body_alias)
683         node = node->same_body;
684     }
685   return node;
686 }
687
688 /* Insert already constructed node into hashtable.  */
689
690 void
691 cgraph_insert_node_to_hashtable (struct cgraph_node *node)
692 {
693   struct cgraph_node **slot;
694
695   slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, node, INSERT);
696
697   gcc_assert (!*slot);
698   *slot = node;
699 }
700
701 /* Returns a hash code for P.  */
702
703 static hashval_t
704 hash_node_by_assembler_name (const void *p)
705 {
706   const struct cgraph_node *n = (const struct cgraph_node *) p;
707   return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
708 }
709
710 /* Returns nonzero if P1 and P2 are equal.  */
711
712 static int
713 eq_assembler_name (const void *p1, const void *p2)
714 {
715   const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
716   const_tree name = (const_tree)p2;
717   return (decl_assembler_name_equal (n1->decl, name));
718 }
719
720 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
721    Return NULL if there's no such node.  */
722
723 struct cgraph_node *
724 cgraph_node_for_asm (tree asmname)
725 {
726   struct cgraph_node *node;
727   void **slot;
728
729   if (!assembler_name_hash)
730     {
731       assembler_name_hash =
732         htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
733                          NULL);
734       for (node = cgraph_nodes; node; node = node->next)
735         if (!node->global.inlined_to)
736           {
737             tree name = DECL_ASSEMBLER_NAME (node->decl);
738             slot = htab_find_slot_with_hash (assembler_name_hash, name,
739                                              decl_assembler_name_hash (name),
740                                              INSERT);
741             /* We can have multiple declarations with same assembler name. For C++
742                it is __builtin_strlen and strlen, for instance.  Do we need to
743                record them all?  Original implementation marked just first one
744                so lets hope for the best.  */
745             if (!*slot)
746               *slot = node;
747             if (node->same_body)
748               {
749                 struct cgraph_node *alias;
750
751                 for (alias = node->same_body; alias; alias = alias->next)
752                   {
753                     hashval_t hash;
754                     name = DECL_ASSEMBLER_NAME (alias->decl);
755                     hash = decl_assembler_name_hash (name);
756                     slot = htab_find_slot_with_hash (assembler_name_hash, name,
757                                                      hash,  INSERT);
758                     if (!*slot)
759                       *slot = alias;
760                   }
761               }
762           }
763     }
764
765   slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
766                                    decl_assembler_name_hash (asmname),
767                                    NO_INSERT);
768
769   if (slot)
770     {
771       node = (struct cgraph_node *) *slot;
772       if (node->same_body_alias)
773         node = node->same_body;
774       return node;
775     }
776   return NULL;
777 }
778
779 /* Returns a hash value for X (which really is a die_struct).  */
780
781 static hashval_t
782 edge_hash (const void *x)
783 {
784   return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
785 }
786
787 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
788
789 static int
790 edge_eq (const void *x, const void *y)
791 {
792   return ((const struct cgraph_edge *) x)->call_stmt == y;
793 }
794
795 /* Add call graph edge E to call site hash of its caller.  */
796
797 static inline void
798 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
799 {
800   void **slot;
801   slot = htab_find_slot_with_hash (e->caller->call_site_hash,
802                                    e->call_stmt,
803                                    htab_hash_pointer (e->call_stmt),
804                                    INSERT);
805   gcc_assert (!*slot);
806   *slot = e;
807 }
808
809 /* Return the callgraph edge representing the GIMPLE_CALL statement
810    CALL_STMT.  */
811
812 struct cgraph_edge *
813 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
814 {
815   struct cgraph_edge *e, *e2;
816   int n = 0;
817
818   if (node->call_site_hash)
819     return (struct cgraph_edge *)
820       htab_find_with_hash (node->call_site_hash, call_stmt,
821                            htab_hash_pointer (call_stmt));
822
823   /* This loop may turn out to be performance problem.  In such case adding
824      hashtables into call nodes with very many edges is probably best
825      solution.  It is not good idea to add pointer into CALL_EXPR itself
826      because we want to make possible having multiple cgraph nodes representing
827      different clones of the same body before the body is actually cloned.  */
828   for (e = node->callees; e; e = e->next_callee)
829     {
830       if (e->call_stmt == call_stmt)
831         break;
832       n++;
833     }
834
835   if (!e)
836     for (e = node->indirect_calls; e; e = e->next_callee)
837       {
838         if (e->call_stmt == call_stmt)
839           break;
840         n++;
841       }
842
843   if (n > 100)
844     {
845       node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
846       for (e2 = node->callees; e2; e2 = e2->next_callee)
847         cgraph_add_edge_to_call_site_hash (e2);
848       for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
849         cgraph_add_edge_to_call_site_hash (e2);
850     }
851
852   return e;
853 }
854
855
856 /* Change field call_stmt of edge E to NEW_STMT.  */
857
858 void
859 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
860 {
861   tree decl;
862
863   if (e->caller->call_site_hash)
864     {
865       htab_remove_elt_with_hash (e->caller->call_site_hash,
866                                  e->call_stmt,
867                                  htab_hash_pointer (e->call_stmt));
868     }
869
870   e->call_stmt = new_stmt;
871   if (e->indirect_unknown_callee
872       && (decl = gimple_call_fndecl (new_stmt)))
873     {
874       /* Constant propagation (and possibly also inlining?) can turn an
875          indirect call into a direct one.  */
876       struct cgraph_node *new_callee = cgraph_get_node (decl);
877
878       gcc_checking_assert (new_callee);
879       cgraph_make_edge_direct (e, new_callee, 0);
880     }
881
882   push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
883   e->can_throw_external = stmt_can_throw_external (new_stmt);
884   pop_cfun ();
885   if (e->caller->call_site_hash)
886     cgraph_add_edge_to_call_site_hash (e);
887 }
888
889 /* Like cgraph_set_call_stmt but walk the clone tree and update all
890    clones sharing the same function body.  */
891
892 void
893 cgraph_set_call_stmt_including_clones (struct cgraph_node *orig,
894                                        gimple old_stmt, gimple new_stmt)
895 {
896   struct cgraph_node *node;
897   struct cgraph_edge *edge = cgraph_edge (orig, old_stmt);
898
899   if (edge)
900     cgraph_set_call_stmt (edge, new_stmt);
901
902   node = orig->clones;
903   if (node)
904     while (node != orig)
905       {
906         struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
907         if (edge)
908           cgraph_set_call_stmt (edge, new_stmt);
909         if (node->clones)
910           node = node->clones;
911         else if (node->next_sibling_clone)
912           node = node->next_sibling_clone;
913         else
914           {
915             while (node != orig && !node->next_sibling_clone)
916               node = node->clone_of;
917             if (node != orig)
918               node = node->next_sibling_clone;
919           }
920       }
921 }
922
923 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
924    same function body.  If clones already have edge for OLD_STMT; only
925    update the edge same way as cgraph_set_call_stmt_including_clones does.
926
927    TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
928    frequencies of the clones.  */
929
930 void
931 cgraph_create_edge_including_clones (struct cgraph_node *orig,
932                                      struct cgraph_node *callee,
933                                      gimple old_stmt,
934                                      gimple stmt, gcov_type count,
935                                      int freq,
936                                      cgraph_inline_failed_t reason)
937 {
938   struct cgraph_node *node;
939   struct cgraph_edge *edge;
940
941   if (!cgraph_edge (orig, stmt))
942     {
943       edge = cgraph_create_edge (orig, callee, stmt, count, freq);
944       edge->inline_failed = reason;
945     }
946
947   node = orig->clones;
948   if (node)
949     while (node != orig)
950       {
951         struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
952
953         /* It is possible that clones already contain the edge while
954            master didn't.  Either we promoted indirect call into direct
955            call in the clone or we are processing clones of unreachable
956            master where edges has been removed.  */
957         if (edge)
958           cgraph_set_call_stmt (edge, stmt);
959         else if (!cgraph_edge (node, stmt))
960           {
961             edge = cgraph_create_edge (node, callee, stmt, count,
962                                        freq);
963             edge->inline_failed = reason;
964           }
965
966         if (node->clones)
967           node = node->clones;
968         else if (node->next_sibling_clone)
969           node = node->next_sibling_clone;
970         else
971           {
972             while (node != orig && !node->next_sibling_clone)
973               node = node->clone_of;
974             if (node != orig)
975               node = node->next_sibling_clone;
976           }
977       }
978 }
979
980 /* Allocate a cgraph_edge structure and fill it with data according to the
981    parameters of which only CALLEE can be NULL (when creating an indirect call
982    edge).  */
983
984 static struct cgraph_edge *
985 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
986                        gimple call_stmt, gcov_type count, int freq)
987 {
988   struct cgraph_edge *edge;
989
990   /* LTO does not actually have access to the call_stmt since these
991      have not been loaded yet.  */
992   if (call_stmt)
993     {
994       /* This is a rather expensive check possibly triggering
995          construction of call stmt hashtable.  */
996       gcc_checking_assert (!cgraph_edge (caller, call_stmt));
997
998       gcc_assert (is_gimple_call (call_stmt));
999     }
1000
1001   if (free_edges)
1002     {
1003       edge = free_edges;
1004       free_edges = NEXT_FREE_EDGE (edge);
1005     }
1006   else
1007     {
1008       edge = ggc_alloc_cgraph_edge ();
1009       edge->uid = cgraph_edge_max_uid++;
1010     }
1011
1012   edge->aux = NULL;
1013   edge->caller = caller;
1014   edge->callee = callee;
1015   edge->prev_caller = NULL;
1016   edge->next_caller = NULL;
1017   edge->prev_callee = NULL;
1018   edge->next_callee = NULL;
1019
1020   edge->count = count;
1021   gcc_assert (count >= 0);
1022   edge->frequency = freq;
1023   gcc_assert (freq >= 0);
1024   gcc_assert (freq <= CGRAPH_FREQ_MAX);
1025
1026   edge->call_stmt = call_stmt;
1027   push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
1028   edge->can_throw_external
1029     = call_stmt ? stmt_can_throw_external (call_stmt) : false;
1030   pop_cfun ();
1031   edge->call_stmt_cannot_inline_p =
1032     (call_stmt ? gimple_call_cannot_inline_p (call_stmt) : false);
1033   if (call_stmt && caller->call_site_hash)
1034     cgraph_add_edge_to_call_site_hash (edge);
1035
1036   edge->indirect_info = NULL;
1037   edge->indirect_inlining_edge = 0;
1038
1039   return edge;
1040 }
1041
1042 /* Create edge from CALLER to CALLEE in the cgraph.  */
1043
1044 struct cgraph_edge *
1045 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
1046                     gimple call_stmt, gcov_type count, int freq)
1047 {
1048   struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
1049                                                    count, freq);
1050
1051   edge->indirect_unknown_callee = 0;
1052   initialize_inline_failed (edge);
1053
1054   edge->next_caller = callee->callers;
1055   if (callee->callers)
1056     callee->callers->prev_caller = edge;
1057   edge->next_callee = caller->callees;
1058   if (caller->callees)
1059     caller->callees->prev_callee = edge;
1060   caller->callees = edge;
1061   callee->callers = edge;
1062
1063   return edge;
1064 }
1065
1066 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
1067
1068 struct cgraph_indirect_call_info *
1069 cgraph_allocate_init_indirect_info (void)
1070 {
1071   struct cgraph_indirect_call_info *ii;
1072
1073   ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
1074   ii->param_index = -1;
1075   return ii;
1076 }
1077
1078 /* Create an indirect edge with a yet-undetermined callee where the call
1079    statement destination is a formal parameter of the caller with index
1080    PARAM_INDEX. */
1081
1082 struct cgraph_edge *
1083 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
1084                              int ecf_flags,
1085                              gcov_type count, int freq)
1086 {
1087   struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
1088                                                    count, freq);
1089
1090   edge->indirect_unknown_callee = 1;
1091   initialize_inline_failed (edge);
1092
1093   edge->indirect_info = cgraph_allocate_init_indirect_info ();
1094   edge->indirect_info->ecf_flags = ecf_flags;
1095
1096   edge->next_callee = caller->indirect_calls;
1097   if (caller->indirect_calls)
1098     caller->indirect_calls->prev_callee = edge;
1099   caller->indirect_calls = edge;
1100
1101   return edge;
1102 }
1103
1104 /* Remove the edge E from the list of the callers of the callee.  */
1105
1106 static inline void
1107 cgraph_edge_remove_callee (struct cgraph_edge *e)
1108 {
1109   gcc_assert (!e->indirect_unknown_callee);
1110   if (e->prev_caller)
1111     e->prev_caller->next_caller = e->next_caller;
1112   if (e->next_caller)
1113     e->next_caller->prev_caller = e->prev_caller;
1114   if (!e->prev_caller)
1115     e->callee->callers = e->next_caller;
1116 }
1117
1118 /* Remove the edge E from the list of the callees of the caller.  */
1119
1120 static inline void
1121 cgraph_edge_remove_caller (struct cgraph_edge *e)
1122 {
1123   if (e->prev_callee)
1124     e->prev_callee->next_callee = e->next_callee;
1125   if (e->next_callee)
1126     e->next_callee->prev_callee = e->prev_callee;
1127   if (!e->prev_callee)
1128     {
1129       if (e->indirect_unknown_callee)
1130         e->caller->indirect_calls = e->next_callee;
1131       else
1132         e->caller->callees = e->next_callee;
1133     }
1134   if (e->caller->call_site_hash)
1135     htab_remove_elt_with_hash (e->caller->call_site_hash,
1136                                e->call_stmt,
1137                                htab_hash_pointer (e->call_stmt));
1138 }
1139
1140 /* Put the edge onto the free list.  */
1141
1142 static void
1143 cgraph_free_edge (struct cgraph_edge *e)
1144 {
1145   int uid = e->uid;
1146
1147   /* Clear out the edge so we do not dangle pointers.  */
1148   memset (e, 0, sizeof (*e));
1149   e->uid = uid;
1150   NEXT_FREE_EDGE (e) = free_edges;
1151   free_edges = e;
1152 }
1153
1154 /* Remove the edge E in the cgraph.  */
1155
1156 void
1157 cgraph_remove_edge (struct cgraph_edge *e)
1158 {
1159   /* Call all edge removal hooks.  */
1160   cgraph_call_edge_removal_hooks (e);
1161
1162   if (!e->indirect_unknown_callee)
1163     /* Remove from callers list of the callee.  */
1164     cgraph_edge_remove_callee (e);
1165
1166   /* Remove from callees list of the callers.  */
1167   cgraph_edge_remove_caller (e);
1168
1169   /* Put the edge onto the free list.  */
1170   cgraph_free_edge (e);
1171 }
1172
1173 /* Set callee of call graph edge E and add it to the corresponding set of
1174    callers. */
1175
1176 static void
1177 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1178 {
1179   e->prev_caller = NULL;
1180   if (n->callers)
1181     n->callers->prev_caller = e;
1182   e->next_caller = n->callers;
1183   n->callers = e;
1184   e->callee = n;
1185 }
1186
1187 /* Redirect callee of E to N.  The function does not update underlying
1188    call expression.  */
1189
1190 void
1191 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1192 {
1193   /* Remove from callers list of the current callee.  */
1194   cgraph_edge_remove_callee (e);
1195
1196   /* Insert to callers list of the new callee.  */
1197   cgraph_set_edge_callee (e, n);
1198 }
1199
1200 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1201    CALLEE.  DELTA is an integer constant that is to be added to the this
1202    pointer (first parameter) to compensate for skipping a thunk adjustment.  */
1203
1204 void
1205 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee,
1206                          HOST_WIDE_INT delta)
1207 {
1208   edge->indirect_unknown_callee = 0;
1209   edge->indirect_info->thunk_delta = delta;
1210
1211   /* Get the edge out of the indirect edge list. */
1212   if (edge->prev_callee)
1213     edge->prev_callee->next_callee = edge->next_callee;
1214   if (edge->next_callee)
1215     edge->next_callee->prev_callee = edge->prev_callee;
1216   if (!edge->prev_callee)
1217     edge->caller->indirect_calls = edge->next_callee;
1218
1219   /* Put it into the normal callee list */
1220   edge->prev_callee = NULL;
1221   edge->next_callee = edge->caller->callees;
1222   if (edge->caller->callees)
1223     edge->caller->callees->prev_callee = edge;
1224   edge->caller->callees = edge;
1225
1226   /* Insert to callers list of the new callee.  */
1227   cgraph_set_edge_callee (edge, callee);
1228
1229   /* We need to re-determine the inlining status of the edge.  */
1230   initialize_inline_failed (edge);
1231 }
1232
1233
1234 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1235    OLD_STMT changed into NEW_STMT.  OLD_CALL is gimple_call_fndecl
1236    of OLD_STMT if it was previously call statement.  */
1237
1238 static void
1239 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1240                                         gimple old_stmt, tree old_call, gimple new_stmt)
1241 {
1242   tree new_call = (is_gimple_call (new_stmt)) ? gimple_call_fndecl (new_stmt) : 0;
1243
1244   /* We are seeing indirect calls, then there is nothing to update.  */
1245   if (!new_call && !old_call)
1246     return;
1247   /* See if we turned indirect call into direct call or folded call to one builtin
1248      into different builtin.  */
1249   if (old_call != new_call)
1250     {
1251       struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1252       struct cgraph_edge *ne = NULL;
1253       gcov_type count;
1254       int frequency;
1255
1256       if (e)
1257         {
1258           /* See if the edge is already there and has the correct callee.  It
1259              might be so because of indirect inlining has already updated
1260              it.  We also might've cloned and redirected the edge.  */
1261           if (new_call && e->callee)
1262             {
1263               struct cgraph_node *callee = e->callee;
1264               while (callee)
1265                 {
1266                   if (callee->decl == new_call
1267                       || callee->former_clone_of == new_call)
1268                     return;
1269                   callee = callee->clone_of;
1270                 }
1271             }
1272
1273           /* Otherwise remove edge and create new one; we can't simply redirect
1274              since function has changed, so inline plan and other information
1275              attached to edge is invalid.  */
1276           count = e->count;
1277           frequency = e->frequency;
1278           cgraph_remove_edge (e);
1279         }
1280       else
1281         {
1282           /* We are seeing new direct call; compute profile info based on BB.  */
1283           basic_block bb = gimple_bb (new_stmt);
1284           count = bb->count;
1285           frequency = compute_call_stmt_bb_frequency (current_function_decl,
1286                                                       bb);
1287         }
1288
1289       if (new_call)
1290         {
1291           ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1292                                    new_stmt, count, frequency);
1293           gcc_assert (ne->inline_failed);
1294         }
1295     }
1296   /* We only updated the call stmt; update pointer in cgraph edge..  */
1297   else if (old_stmt != new_stmt)
1298     cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1299 }
1300
1301 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1302    OLD_STMT changed into NEW_STMT.  OLD_DECL is gimple_call_fndecl
1303    of OLD_STMT before it was updated (updating can happen inplace).  */
1304
1305 void
1306 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1307 {
1308   struct cgraph_node *orig = cgraph_get_node (cfun->decl);
1309   struct cgraph_node *node;
1310
1311   gcc_checking_assert (orig);
1312   cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1313   if (orig->clones)
1314     for (node = orig->clones; node != orig;)
1315       {
1316         cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1317         if (node->clones)
1318           node = node->clones;
1319         else if (node->next_sibling_clone)
1320           node = node->next_sibling_clone;
1321         else
1322           {
1323             while (node != orig && !node->next_sibling_clone)
1324               node = node->clone_of;
1325             if (node != orig)
1326               node = node->next_sibling_clone;
1327           }
1328       }
1329 }
1330
1331
1332 /* Remove all callees from the node.  */
1333
1334 void
1335 cgraph_node_remove_callees (struct cgraph_node *node)
1336 {
1337   struct cgraph_edge *e, *f;
1338
1339   /* It is sufficient to remove the edges from the lists of callers of
1340      the callees.  The callee list of the node can be zapped with one
1341      assignment.  */
1342   for (e = node->callees; e; e = f)
1343     {
1344       f = e->next_callee;
1345       cgraph_call_edge_removal_hooks (e);
1346       if (!e->indirect_unknown_callee)
1347         cgraph_edge_remove_callee (e);
1348       cgraph_free_edge (e);
1349     }
1350   for (e = node->indirect_calls; e; e = f)
1351     {
1352       f = e->next_callee;
1353       cgraph_call_edge_removal_hooks (e);
1354       if (!e->indirect_unknown_callee)
1355         cgraph_edge_remove_callee (e);
1356       cgraph_free_edge (e);
1357     }
1358   node->indirect_calls = NULL;
1359   node->callees = NULL;
1360   if (node->call_site_hash)
1361     {
1362       htab_delete (node->call_site_hash);
1363       node->call_site_hash = NULL;
1364     }
1365 }
1366
1367 /* Remove all callers from the node.  */
1368
1369 static void
1370 cgraph_node_remove_callers (struct cgraph_node *node)
1371 {
1372   struct cgraph_edge *e, *f;
1373
1374   /* It is sufficient to remove the edges from the lists of callees of
1375      the callers.  The caller list of the node can be zapped with one
1376      assignment.  */
1377   for (e = node->callers; e; e = f)
1378     {
1379       f = e->next_caller;
1380       cgraph_call_edge_removal_hooks (e);
1381       cgraph_edge_remove_caller (e);
1382       cgraph_free_edge (e);
1383     }
1384   node->callers = NULL;
1385 }
1386
1387 /* Release memory used to represent body of function NODE.  */
1388
1389 void
1390 cgraph_release_function_body (struct cgraph_node *node)
1391 {
1392   if (DECL_STRUCT_FUNCTION (node->decl))
1393     {
1394       tree old_decl = current_function_decl;
1395       push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1396       if (cfun->gimple_df)
1397         {
1398           current_function_decl = node->decl;
1399           delete_tree_ssa ();
1400           delete_tree_cfg_annotations ();
1401           cfun->eh = NULL;
1402           current_function_decl = old_decl;
1403         }
1404       if (cfun->cfg)
1405         {
1406           gcc_assert (dom_computed[0] == DOM_NONE);
1407           gcc_assert (dom_computed[1] == DOM_NONE);
1408           clear_edges ();
1409         }
1410       if (cfun->value_histograms)
1411         free_histograms ();
1412       gcc_assert (!current_loops);
1413       pop_cfun();
1414       gimple_set_body (node->decl, NULL);
1415       VEC_free (ipa_opt_pass, heap,
1416                 node->ipa_transforms_to_apply);
1417       /* Struct function hangs a lot of data that would leak if we didn't
1418          removed all pointers to it.   */
1419       ggc_free (DECL_STRUCT_FUNCTION (node->decl));
1420       DECL_STRUCT_FUNCTION (node->decl) = NULL;
1421     }
1422   DECL_SAVED_TREE (node->decl) = NULL;
1423   /* If the node is abstract and needed, then do not clear DECL_INITIAL
1424      of its associated function function declaration because it's
1425      needed to emit debug info later.  */
1426   if (!node->abstract_and_needed)
1427     DECL_INITIAL (node->decl) = error_mark_node;
1428 }
1429
1430 /* Remove same body alias node.  */
1431
1432 void
1433 cgraph_remove_same_body_alias (struct cgraph_node *node)
1434 {
1435   void **slot;
1436   int uid = node->uid;
1437
1438   gcc_assert (node->same_body_alias);
1439   if (node->previous)
1440     node->previous->next = node->next;
1441   else
1442     node->same_body->same_body = node->next;
1443   if (node->next)
1444     node->next->previous = node->previous;
1445   node->next = NULL;
1446   node->previous = NULL;
1447   slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1448   if (*slot == node)
1449     htab_clear_slot (cgraph_hash, slot);
1450   if (assembler_name_hash)
1451     {
1452       tree name = DECL_ASSEMBLER_NAME (node->decl);
1453       slot = htab_find_slot_with_hash (assembler_name_hash, name,
1454                                        decl_assembler_name_hash (name),
1455                                        NO_INSERT);
1456       if (slot && *slot == node)
1457         htab_clear_slot (assembler_name_hash, slot);
1458     }
1459
1460   /* Clear out the node to NULL all pointers and add the node to the free
1461      list.  */
1462   memset (node, 0, sizeof(*node));
1463   node->uid = uid;
1464   NEXT_FREE_NODE (node) = free_nodes;
1465   free_nodes = node;
1466 }
1467
1468 /* Remove the node from cgraph.  */
1469
1470 void
1471 cgraph_remove_node (struct cgraph_node *node)
1472 {
1473   void **slot;
1474   bool kill_body = false;
1475   struct cgraph_node *n;
1476   int uid = node->uid;
1477
1478   cgraph_call_node_removal_hooks (node);
1479   cgraph_node_remove_callers (node);
1480   cgraph_node_remove_callees (node);
1481   ipa_remove_all_references (&node->ref_list);
1482   ipa_remove_all_refering (&node->ref_list);
1483   VEC_free (ipa_opt_pass, heap,
1484             node->ipa_transforms_to_apply);
1485
1486   /* Incremental inlining access removed nodes stored in the postorder list.
1487      */
1488   node->needed = node->reachable = false;
1489   for (n = node->nested; n; n = n->next_nested)
1490     n->origin = NULL;
1491   node->nested = NULL;
1492   if (node->origin)
1493     {
1494       struct cgraph_node **node2 = &node->origin->nested;
1495
1496       while (*node2 != node)
1497         node2 = &(*node2)->next_nested;
1498       *node2 = node->next_nested;
1499     }
1500   if (node->previous)
1501     node->previous->next = node->next;
1502   else
1503     cgraph_nodes = node->next;
1504   if (node->next)
1505     node->next->previous = node->previous;
1506   node->next = NULL;
1507   node->previous = NULL;
1508   slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1509   if (*slot == node)
1510     {
1511       struct cgraph_node *next_inline_clone;
1512
1513       for (next_inline_clone = node->clones;
1514            next_inline_clone && next_inline_clone->decl != node->decl;
1515            next_inline_clone = next_inline_clone->next_sibling_clone)
1516         ;
1517
1518       /* If there is inline clone of the node being removed, we need
1519          to put it into the position of removed node and reorganize all
1520          other clones to be based on it.  */
1521       if (next_inline_clone)
1522         {
1523           struct cgraph_node *n;
1524           struct cgraph_node *new_clones;
1525
1526           *slot = next_inline_clone;
1527
1528           /* Unlink inline clone from the list of clones of removed node.  */
1529           if (next_inline_clone->next_sibling_clone)
1530             next_inline_clone->next_sibling_clone->prev_sibling_clone
1531               = next_inline_clone->prev_sibling_clone;
1532           if (next_inline_clone->prev_sibling_clone)
1533             {
1534               gcc_assert (node->clones != next_inline_clone);
1535               next_inline_clone->prev_sibling_clone->next_sibling_clone
1536                 = next_inline_clone->next_sibling_clone;
1537             }
1538           else
1539             {
1540               gcc_assert (node->clones == next_inline_clone);
1541               node->clones = next_inline_clone->next_sibling_clone;
1542             }
1543
1544           new_clones = node->clones;
1545           node->clones = NULL;
1546
1547           /* Copy clone info.  */
1548           next_inline_clone->clone = node->clone;
1549
1550           /* Now place it into clone tree at same level at NODE.  */
1551           next_inline_clone->clone_of = node->clone_of;
1552           next_inline_clone->prev_sibling_clone = NULL;
1553           next_inline_clone->next_sibling_clone = NULL;
1554           if (node->clone_of)
1555             {
1556               if (node->clone_of->clones)
1557                 node->clone_of->clones->prev_sibling_clone = next_inline_clone;
1558               next_inline_clone->next_sibling_clone = node->clone_of->clones;
1559               node->clone_of->clones = next_inline_clone;
1560             }
1561
1562           /* Merge the clone list.  */
1563           if (new_clones)
1564             {
1565               if (!next_inline_clone->clones)
1566                 next_inline_clone->clones = new_clones;
1567               else
1568                 {
1569                   n = next_inline_clone->clones;
1570                   while (n->next_sibling_clone)
1571                     n =  n->next_sibling_clone;
1572                   n->next_sibling_clone = new_clones;
1573                   new_clones->prev_sibling_clone = n;
1574                 }
1575             }
1576
1577           /* Update clone_of pointers.  */
1578           n = new_clones;
1579           while (n)
1580             {
1581               n->clone_of = next_inline_clone;
1582               n = n->next_sibling_clone;
1583             }
1584         }
1585       else
1586         {
1587           htab_clear_slot (cgraph_hash, slot);
1588           kill_body = true;
1589         }
1590
1591     }
1592   if (node->prev_sibling_clone)
1593     node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1594   else if (node->clone_of)
1595     node->clone_of->clones = node->next_sibling_clone;
1596   if (node->next_sibling_clone)
1597     node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1598   if (node->clones)
1599     {
1600       struct cgraph_node *n, *next;
1601
1602       if (node->clone_of)
1603         {
1604           for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1605             n->clone_of = node->clone_of;
1606           n->clone_of = node->clone_of;
1607           n->next_sibling_clone = node->clone_of->clones;
1608           if (node->clone_of->clones)
1609             node->clone_of->clones->prev_sibling_clone = n;
1610           node->clone_of->clones = node->clones;
1611         }
1612       else
1613         {
1614           /* We are removing node with clones.  this makes clones inconsistent,
1615              but assume they will be removed subsequently and just keep clone
1616              tree intact.  This can happen in unreachable function removal since
1617              we remove unreachable functions in random order, not by bottom-up
1618              walk of clone trees.  */
1619           for (n = node->clones; n; n = next)
1620             {
1621                next = n->next_sibling_clone;
1622                n->next_sibling_clone = NULL;
1623                n->prev_sibling_clone = NULL;
1624                n->clone_of = NULL;
1625             }
1626         }
1627     }
1628
1629   while (node->same_body)
1630     cgraph_remove_same_body_alias (node->same_body);
1631
1632   if (node->same_comdat_group)
1633     {
1634       struct cgraph_node *prev;
1635       for (prev = node->same_comdat_group;
1636            prev->same_comdat_group != node;
1637            prev = prev->same_comdat_group)
1638         ;
1639       if (node->same_comdat_group == prev)
1640         prev->same_comdat_group = NULL;
1641       else
1642         prev->same_comdat_group = node->same_comdat_group;
1643       node->same_comdat_group = NULL;
1644     }
1645
1646   /* While all the clones are removed after being proceeded, the function
1647      itself is kept in the cgraph even after it is compiled.  Check whether
1648      we are done with this body and reclaim it proactively if this is the case.
1649      */
1650   if (!kill_body && *slot)
1651     {
1652       struct cgraph_node *n = (struct cgraph_node *) *slot;
1653       if (!n->clones && !n->clone_of && !n->global.inlined_to
1654           && (cgraph_global_info_ready
1655               && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl)
1656                   || n->in_other_partition)))
1657         kill_body = true;
1658     }
1659   if (assembler_name_hash)
1660     {
1661       tree name = DECL_ASSEMBLER_NAME (node->decl);
1662       slot = htab_find_slot_with_hash (assembler_name_hash, name,
1663                                        decl_assembler_name_hash (name),
1664                                        NO_INSERT);
1665       /* Inline clones are not hashed.  */
1666       if (slot && *slot == node)
1667         htab_clear_slot (assembler_name_hash, slot);
1668     }
1669
1670   if (kill_body)
1671     cgraph_release_function_body (node);
1672   node->decl = NULL;
1673   if (node->call_site_hash)
1674     {
1675       htab_delete (node->call_site_hash);
1676       node->call_site_hash = NULL;
1677     }
1678   cgraph_n_nodes--;
1679
1680   /* Clear out the node to NULL all pointers and add the node to the free
1681      list.  */
1682   memset (node, 0, sizeof(*node));
1683   node->uid = uid;
1684   NEXT_FREE_NODE (node) = free_nodes;
1685   free_nodes = node;
1686 }
1687
1688 /* Remove the node from cgraph.  */
1689
1690 void
1691 cgraph_remove_node_and_inline_clones (struct cgraph_node *node)
1692 {
1693   struct cgraph_edge *e, *next;
1694   for (e = node->callees; e; e = next)
1695     {
1696       next = e->next_callee;
1697       if (!e->inline_failed)
1698         cgraph_remove_node_and_inline_clones (e->callee);
1699     }
1700   cgraph_remove_node (node);
1701 }
1702
1703 /* Notify finalize_compilation_unit that given node is reachable.  */
1704
1705 void
1706 cgraph_mark_reachable_node (struct cgraph_node *node)
1707 {
1708   if (!node->reachable && node->local.finalized)
1709     {
1710       if (cgraph_global_info_ready)
1711         {
1712           /* Verify that function does not appear to be needed out of blue
1713              during the optimization process.  This can happen for extern
1714              inlines when bodies was removed after inlining.  */
1715           gcc_assert ((node->analyzed || node->in_other_partition
1716                        || DECL_EXTERNAL (node->decl)));
1717         }
1718       else
1719         notice_global_symbol (node->decl);
1720       node->reachable = 1;
1721
1722       node->next_needed = cgraph_nodes_queue;
1723       cgraph_nodes_queue = node;
1724     }
1725 }
1726
1727 /* Likewise indicate that a node is needed, i.e. reachable via some
1728    external means.  */
1729
1730 void
1731 cgraph_mark_needed_node (struct cgraph_node *node)
1732 {
1733   node->needed = 1;
1734   gcc_assert (!node->global.inlined_to);
1735   cgraph_mark_reachable_node (node);
1736 }
1737
1738 /* Likewise indicate that a node is having address taken.  */
1739
1740 void
1741 cgraph_mark_address_taken_node (struct cgraph_node *node)
1742 {
1743   gcc_assert (!node->global.inlined_to);
1744   cgraph_mark_reachable_node (node);
1745   node->address_taken = 1;
1746 }
1747
1748 /* Return local info for the compiled function.  */
1749
1750 struct cgraph_local_info *
1751 cgraph_local_info (tree decl)
1752 {
1753   struct cgraph_node *node;
1754
1755   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1756   node = cgraph_get_node (decl);
1757   if (!node)
1758     return NULL;
1759   return &node->local;
1760 }
1761
1762 /* Return local info for the compiled function.  */
1763
1764 struct cgraph_global_info *
1765 cgraph_global_info (tree decl)
1766 {
1767   struct cgraph_node *node;
1768
1769   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1770   node = cgraph_get_node (decl);
1771   if (!node)
1772     return NULL;
1773   return &node->global;
1774 }
1775
1776 /* Return local info for the compiled function.  */
1777
1778 struct cgraph_rtl_info *
1779 cgraph_rtl_info (tree decl)
1780 {
1781   struct cgraph_node *node;
1782
1783   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1784   node = cgraph_get_node (decl);
1785   if (!node
1786       || (decl != current_function_decl
1787           && !TREE_ASM_WRITTEN (node->decl)))
1788     return NULL;
1789   return &node->rtl;
1790 }
1791
1792 /* Return a string describing the failure REASON.  */
1793
1794 const char*
1795 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1796 {
1797 #undef DEFCIFCODE
1798 #define DEFCIFCODE(code, string)        string,
1799
1800   static const char *cif_string_table[CIF_N_REASONS] = {
1801 #include "cif-code.def"
1802   };
1803
1804   /* Signedness of an enum type is implementation defined, so cast it
1805      to unsigned before testing. */
1806   gcc_assert ((unsigned) reason < CIF_N_REASONS);
1807   return cif_string_table[reason];
1808 }
1809
1810 /* Return name of the node used in debug output.  */
1811 const char *
1812 cgraph_node_name (struct cgraph_node *node)
1813 {
1814   return lang_hooks.decl_printable_name (node->decl, 2);
1815 }
1816
1817 /* Names used to print out the availability enum.  */
1818 const char * const cgraph_availability_names[] =
1819   {"unset", "not_available", "overwritable", "available", "local"};
1820
1821
1822 /* Dump call graph node NODE to file F.  */
1823
1824 void
1825 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1826 {
1827   struct cgraph_edge *edge;
1828   int indirect_calls_count = 0;
1829
1830   fprintf (f, "%s/%i", cgraph_node_name (node), node->uid);
1831   dump_addr (f, " @", (void *)node);
1832   if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
1833     fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
1834   if (node->global.inlined_to)
1835     fprintf (f, " (inline copy in %s/%i)",
1836              cgraph_node_name (node->global.inlined_to),
1837              node->global.inlined_to->uid);
1838   if (node->same_comdat_group)
1839     fprintf (f, " (same comdat group as %s/%i)",
1840              cgraph_node_name (node->same_comdat_group),
1841              node->same_comdat_group->uid);
1842   if (node->clone_of)
1843     fprintf (f, " (clone of %s/%i)",
1844              cgraph_node_name (node->clone_of),
1845              node->clone_of->uid);
1846   if (cgraph_function_flags_ready)
1847     fprintf (f, " availability:%s",
1848              cgraph_availability_names [cgraph_function_body_availability (node)]);
1849   if (node->analyzed)
1850     fprintf (f, " analyzed");
1851   if (node->in_other_partition)
1852     fprintf (f, " in_other_partition");
1853   if (node->count)
1854     fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1855              (HOST_WIDEST_INT)node->count);
1856   if (node->origin)
1857     fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
1858   if (node->needed)
1859     fprintf (f, " needed");
1860   if (node->address_taken)
1861     fprintf (f, " address_taken");
1862   else if (node->reachable)
1863     fprintf (f, " reachable");
1864   else if (node->reachable_from_other_partition)
1865     fprintf (f, " reachable_from_other_partition");
1866   if (gimple_has_body_p (node->decl))
1867     fprintf (f, " body");
1868   if (node->process)
1869     fprintf (f, " process");
1870   if (node->local.local)
1871     fprintf (f, " local");
1872   if (node->local.externally_visible)
1873     fprintf (f, " externally_visible");
1874   if (node->resolution != LDPR_UNKNOWN)
1875     fprintf (f, " %s",
1876              ld_plugin_symbol_resolution_names[(int)node->resolution]);
1877   if (node->local.finalized)
1878     fprintf (f, " finalized");
1879   if (node->local.redefined_extern_inline)
1880     fprintf (f, " redefined_extern_inline");
1881   if (TREE_ASM_WRITTEN (node->decl))
1882     fprintf (f, " asm_written");
1883   if (node->only_called_at_startup)
1884     fprintf (f, " only_called_at_startup");
1885   if (node->only_called_at_exit)
1886     fprintf (f, " only_called_at_exit");
1887
1888   fprintf (f, "\n");
1889
1890   if (node->thunk.thunk_p)
1891     {
1892       fprintf (f, "  thunk of %s (asm: %s) fixed offset %i virtual value %i has "
1893                "virtual offset %i)\n",
1894                lang_hooks.decl_printable_name (node->thunk.alias, 2),
1895                IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)),
1896                (int)node->thunk.fixed_offset,
1897                (int)node->thunk.virtual_value,
1898                (int)node->thunk.virtual_offset_p);
1899     }
1900   
1901   fprintf (f, "  called by: ");
1902
1903   for (edge = node->callers; edge; edge = edge->next_caller)
1904     {
1905       fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
1906                edge->caller->uid);
1907       if (edge->count)
1908         fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1909                  (HOST_WIDEST_INT)edge->count);
1910       if (edge->frequency)
1911         fprintf (f, "(%.2f per call) ",
1912                  edge->frequency / (double)CGRAPH_FREQ_BASE);
1913       if (!edge->inline_failed)
1914         fprintf(f, "(inlined) ");
1915       if (edge->indirect_inlining_edge)
1916         fprintf(f, "(indirect_inlining) ");
1917       if (edge->can_throw_external)
1918         fprintf(f, "(can throw external) ");
1919     }
1920
1921   fprintf (f, "\n  calls: ");
1922   for (edge = node->callees; edge; edge = edge->next_callee)
1923     {
1924       fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
1925                edge->callee->uid);
1926       if (!edge->inline_failed)
1927         fprintf(f, "(inlined) ");
1928       if (edge->indirect_inlining_edge)
1929         fprintf(f, "(indirect_inlining) ");
1930       if (edge->count)
1931         fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1932                  (HOST_WIDEST_INT)edge->count);
1933       if (edge->frequency)
1934         fprintf (f, "(%.2f per call) ",
1935                  edge->frequency / (double)CGRAPH_FREQ_BASE);
1936       if (edge->can_throw_external)
1937         fprintf(f, "(can throw external) ");
1938     }
1939   fprintf (f, "\n");
1940   fprintf (f, "  References: ");
1941   ipa_dump_references (f, &node->ref_list);
1942   fprintf (f, "  Refering this function: ");
1943   ipa_dump_refering (f, &node->ref_list);
1944
1945   for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1946     indirect_calls_count++;
1947   if (indirect_calls_count)
1948     fprintf (f, "  has %i outgoing edges for indirect calls.\n",
1949              indirect_calls_count);
1950
1951   if (node->same_body)
1952     {
1953       struct cgraph_node *n;
1954       fprintf (f, "  aliases:");
1955       for (n = node->same_body; n; n = n->next)
1956         {
1957           fprintf (f, " %s/%i", cgraph_node_name (n), n->uid);
1958           if (DECL_ASSEMBLER_NAME_SET_P (n->decl))
1959             fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
1960         }
1961       fprintf (f, "\n");
1962     }
1963 }
1964
1965
1966 /* Dump call graph node NODE to stderr.  */
1967
1968 DEBUG_FUNCTION void
1969 debug_cgraph_node (struct cgraph_node *node)
1970 {
1971   dump_cgraph_node (stderr, node);
1972 }
1973
1974
1975 /* Dump the callgraph to file F.  */
1976
1977 void
1978 dump_cgraph (FILE *f)
1979 {
1980   struct cgraph_node *node;
1981
1982   fprintf (f, "callgraph:\n\n");
1983   for (node = cgraph_nodes; node; node = node->next)
1984     dump_cgraph_node (f, node);
1985 }
1986
1987
1988 /* Dump the call graph to stderr.  */
1989
1990 DEBUG_FUNCTION void
1991 debug_cgraph (void)
1992 {
1993   dump_cgraph (stderr);
1994 }
1995
1996
1997 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables.  */
1998
1999 void
2000 change_decl_assembler_name (tree decl, tree name)
2001 {
2002   struct cgraph_node *node;
2003   void **slot;
2004   if (!DECL_ASSEMBLER_NAME_SET_P (decl))
2005     SET_DECL_ASSEMBLER_NAME (decl, name);
2006   else
2007     {
2008       if (name == DECL_ASSEMBLER_NAME (decl))
2009         return;
2010
2011       if (assembler_name_hash
2012           && TREE_CODE (decl) == FUNCTION_DECL
2013           && (node = cgraph_get_node_or_alias (decl)) != NULL)
2014         {
2015           tree old_name = DECL_ASSEMBLER_NAME (decl);
2016           slot = htab_find_slot_with_hash (assembler_name_hash, old_name,
2017                                            decl_assembler_name_hash (old_name),
2018                                            NO_INSERT);
2019           /* Inline clones are not hashed.  */
2020           if (slot && *slot == node)
2021             htab_clear_slot (assembler_name_hash, slot);
2022         }
2023       if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
2024           && DECL_RTL_SET_P (decl))
2025         warning (0, "%D renamed after being referenced in assembly", decl);
2026
2027       SET_DECL_ASSEMBLER_NAME (decl, name);
2028     }
2029   if (assembler_name_hash
2030       && TREE_CODE (decl) == FUNCTION_DECL
2031       && (node = cgraph_get_node_or_alias (decl)) != NULL)
2032     {
2033       slot = htab_find_slot_with_hash (assembler_name_hash, name,
2034                                        decl_assembler_name_hash (name),
2035                                        INSERT);
2036       gcc_assert (!*slot);
2037       *slot = node;
2038     }
2039 }
2040
2041 /* Add a top-level asm statement to the list.  */
2042
2043 struct cgraph_asm_node *
2044 cgraph_add_asm_node (tree asm_str)
2045 {
2046   struct cgraph_asm_node *node;
2047
2048   node = ggc_alloc_cleared_cgraph_asm_node ();
2049   node->asm_str = asm_str;
2050   node->order = cgraph_order++;
2051   node->next = NULL;
2052   if (cgraph_asm_nodes == NULL)
2053     cgraph_asm_nodes = node;
2054   else
2055     cgraph_asm_last_node->next = node;
2056   cgraph_asm_last_node = node;
2057   return node;
2058 }
2059
2060 /* Return true when the DECL can possibly be inlined.  */
2061 bool
2062 cgraph_function_possibly_inlined_p (tree decl)
2063 {
2064   if (!cgraph_global_info_ready)
2065     return !DECL_UNINLINABLE (decl);
2066   return DECL_POSSIBLY_INLINED (decl);
2067 }
2068
2069 /* Create clone of E in the node N represented by CALL_EXPR the callgraph.  */
2070 struct cgraph_edge *
2071 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
2072                    gimple call_stmt, unsigned stmt_uid, gcov_type count_scale,
2073                    int freq_scale, bool update_original)
2074 {
2075   struct cgraph_edge *new_edge;
2076   gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
2077   gcov_type freq;
2078
2079   /* We do not want to ignore loop nest after frequency drops to 0.  */
2080   if (!freq_scale)
2081     freq_scale = 1;
2082   freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
2083   if (freq > CGRAPH_FREQ_MAX)
2084     freq = CGRAPH_FREQ_MAX;
2085
2086   if (e->indirect_unknown_callee)
2087     {
2088       tree decl;
2089
2090       if (call_stmt && (decl = gimple_call_fndecl (call_stmt)))
2091         {
2092           struct cgraph_node *callee = cgraph_get_node (decl);
2093           gcc_checking_assert (callee);
2094           new_edge = cgraph_create_edge (n, callee, call_stmt, count, freq);
2095         }
2096       else
2097         {
2098           new_edge = cgraph_create_indirect_edge (n, call_stmt,
2099                                                   e->indirect_info->ecf_flags,
2100                                                   count, freq);
2101           *new_edge->indirect_info = *e->indirect_info;
2102         }
2103     }
2104   else
2105     {
2106       new_edge = cgraph_create_edge (n, e->callee, call_stmt, count, freq);
2107       if (e->indirect_info)
2108         {
2109           new_edge->indirect_info
2110             = ggc_alloc_cleared_cgraph_indirect_call_info ();
2111           *new_edge->indirect_info = *e->indirect_info;
2112         }
2113     }
2114
2115   new_edge->inline_failed = e->inline_failed;
2116   new_edge->indirect_inlining_edge = e->indirect_inlining_edge;
2117   new_edge->lto_stmt_uid = stmt_uid;
2118   /* Clone flags that depend on call_stmt availability manually.  */
2119   new_edge->can_throw_external = e->can_throw_external;
2120   new_edge->call_stmt_cannot_inline_p = e->call_stmt_cannot_inline_p;
2121   if (update_original)
2122     {
2123       e->count -= new_edge->count;
2124       if (e->count < 0)
2125         e->count = 0;
2126     }
2127   cgraph_call_edge_duplication_hooks (e, new_edge);
2128   return new_edge;
2129 }
2130
2131
2132 /* Create node representing clone of N executed COUNT times.  Decrease
2133    the execution counts from original node too.
2134    The new clone will have decl set to DECL that may or may not be the same
2135    as decl of N.
2136
2137    When UPDATE_ORIGINAL is true, the counts are subtracted from the original
2138    function's profile to reflect the fact that part of execution is handled
2139    by node.  
2140    When CALL_DUPLICATOIN_HOOK is true, the ipa passes are acknowledged about
2141    the new clone. Otherwise the caller is responsible for doing so later.  */
2142
2143 struct cgraph_node *
2144 cgraph_clone_node (struct cgraph_node *n, tree decl, gcov_type count, int freq,
2145                    bool update_original,
2146                    VEC(cgraph_edge_p,heap) *redirect_callers,
2147                    bool call_duplication_hook)
2148 {
2149   struct cgraph_node *new_node = cgraph_create_node_1 ();
2150   struct cgraph_edge *e;
2151   gcov_type count_scale;
2152   unsigned i;
2153
2154   new_node->decl = decl;
2155   new_node->origin = n->origin;
2156   if (new_node->origin)
2157     {
2158       new_node->next_nested = new_node->origin->nested;
2159       new_node->origin->nested = new_node;
2160     }
2161   new_node->analyzed = n->analyzed;
2162   new_node->local = n->local;
2163   new_node->local.externally_visible = false;
2164   new_node->local.local = true;
2165   new_node->global = n->global;
2166   new_node->rtl = n->rtl;
2167   new_node->count = count;
2168   new_node->frequency = n->frequency;
2169   new_node->clone = n->clone;
2170   new_node->clone.tree_map = 0;
2171   if (n->count)
2172     {
2173       if (new_node->count > n->count)
2174         count_scale = REG_BR_PROB_BASE;
2175       else
2176         count_scale = new_node->count * REG_BR_PROB_BASE / n->count;
2177     }
2178   else
2179     count_scale = 0;
2180   if (update_original)
2181     {
2182       n->count -= count;
2183       if (n->count < 0)
2184         n->count = 0;
2185     }
2186
2187   FOR_EACH_VEC_ELT (cgraph_edge_p, redirect_callers, i, e)
2188     {
2189       /* Redirect calls to the old version node to point to its new
2190          version.  */
2191       cgraph_redirect_edge_callee (e, new_node);
2192     }
2193
2194
2195   for (e = n->callees;e; e=e->next_callee)
2196     cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
2197                        count_scale, freq, update_original);
2198
2199   for (e = n->indirect_calls; e; e = e->next_callee)
2200     cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
2201                        count_scale, freq, update_original);
2202   ipa_clone_references (new_node, NULL, &n->ref_list);
2203
2204   new_node->next_sibling_clone = n->clones;
2205   if (n->clones)
2206     n->clones->prev_sibling_clone = new_node;
2207   n->clones = new_node;
2208   new_node->clone_of = n;
2209
2210   if (n->decl != decl)
2211     {
2212       struct cgraph_node **slot;
2213       slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, new_node, INSERT);
2214       gcc_assert (!*slot);
2215       *slot = new_node;
2216       if (assembler_name_hash)
2217         {
2218           void **aslot;
2219           tree name = DECL_ASSEMBLER_NAME (decl);
2220
2221           aslot = htab_find_slot_with_hash (assembler_name_hash, name,
2222                                             decl_assembler_name_hash (name),
2223                                             INSERT);
2224           gcc_assert (!*aslot);
2225           *aslot = new_node;
2226         }
2227     }
2228
2229   if (call_duplication_hook)
2230     cgraph_call_node_duplication_hooks (n, new_node);
2231   return new_node;
2232 }
2233
2234 /* Create a new name for clone of DECL, add SUFFIX.  Returns an identifier.  */
2235
2236 static GTY(()) unsigned int clone_fn_id_num;
2237
2238 tree
2239 clone_function_name (tree decl, const char *suffix)
2240 {
2241   tree name = DECL_ASSEMBLER_NAME (decl);
2242   size_t len = IDENTIFIER_LENGTH (name);
2243   char *tmp_name, *prefix;
2244
2245   prefix = XALLOCAVEC (char, len + strlen (suffix) + 2);
2246   memcpy (prefix, IDENTIFIER_POINTER (name), len);
2247   strcpy (prefix + len + 1, suffix);
2248 #ifndef NO_DOT_IN_LABEL
2249   prefix[len] = '.';
2250 #elif !defined NO_DOLLAR_IN_LABEL
2251   prefix[len] = '$';
2252 #else
2253   prefix[len] = '_';
2254 #endif
2255   ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++);
2256   return get_identifier (tmp_name);
2257 }
2258
2259 /* Create callgraph node clone with new declaration.  The actual body will
2260    be copied later at compilation stage.
2261
2262    TODO: after merging in ipa-sra use function call notes instead of args_to_skip
2263    bitmap interface.
2264    */
2265 struct cgraph_node *
2266 cgraph_create_virtual_clone (struct cgraph_node *old_node,
2267                              VEC(cgraph_edge_p,heap) *redirect_callers,
2268                              VEC(ipa_replace_map_p,gc) *tree_map,
2269                              bitmap args_to_skip,
2270                              const char * suffix)
2271 {
2272   tree old_decl = old_node->decl;
2273   struct cgraph_node *new_node = NULL;
2274   tree new_decl;
2275   size_t i;
2276   struct ipa_replace_map *map;
2277
2278   if (!flag_wpa)
2279     gcc_checking_assert  (tree_versionable_function_p (old_decl));
2280
2281   gcc_assert (old_node->local.can_change_signature || !args_to_skip);
2282
2283   /* Make a new FUNCTION_DECL tree node */
2284   if (!args_to_skip)
2285     new_decl = copy_node (old_decl);
2286   else
2287     new_decl = build_function_decl_skip_args (old_decl, args_to_skip);
2288   DECL_STRUCT_FUNCTION (new_decl) = NULL;
2289
2290   /* Generate a new name for the new version. */
2291   DECL_NAME (new_decl) = clone_function_name (old_decl, suffix);
2292   SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
2293   SET_DECL_RTL (new_decl, NULL);
2294
2295   new_node = cgraph_clone_node (old_node, new_decl, old_node->count,
2296                                 CGRAPH_FREQ_BASE, false,
2297                                 redirect_callers, false);
2298   /* Update the properties.
2299      Make clone visible only within this translation unit.  Make sure
2300      that is not weak also.
2301      ??? We cannot use COMDAT linkage because there is no
2302      ABI support for this.  */
2303   DECL_EXTERNAL (new_node->decl) = 0;
2304   if (DECL_ONE_ONLY (old_decl))
2305     DECL_SECTION_NAME (new_node->decl) = NULL;
2306   DECL_COMDAT_GROUP (new_node->decl) = 0;
2307   TREE_PUBLIC (new_node->decl) = 0;
2308   DECL_COMDAT (new_node->decl) = 0;
2309   DECL_WEAK (new_node->decl) = 0;
2310   DECL_STATIC_CONSTRUCTOR (new_node->decl) = 0;
2311   DECL_STATIC_DESTRUCTOR (new_node->decl) = 0;
2312   new_node->clone.tree_map = tree_map;
2313   new_node->clone.args_to_skip = args_to_skip;
2314   FOR_EACH_VEC_ELT (ipa_replace_map_p, tree_map, i, map)
2315     {
2316       tree var = map->new_tree;
2317
2318       STRIP_NOPS (var);
2319       if (TREE_CODE (var) != ADDR_EXPR)
2320         continue;
2321       var = get_base_var (var);
2322       if (!var)
2323         continue;
2324
2325       /* Record references of the future statement initializing the constant
2326          argument.  */
2327       if (TREE_CODE (var) == FUNCTION_DECL)
2328         {
2329           struct cgraph_node *ref_node = cgraph_get_node (var);
2330           gcc_checking_assert (ref_node);
2331           ipa_record_reference (new_node, NULL, ref_node, NULL, IPA_REF_ADDR,
2332                                 NULL);
2333         }
2334       else if (TREE_CODE (var) == VAR_DECL)
2335         ipa_record_reference (new_node, NULL, NULL, varpool_node (var),
2336                               IPA_REF_ADDR, NULL);
2337     }
2338   if (!args_to_skip)
2339     new_node->clone.combined_args_to_skip = old_node->clone.combined_args_to_skip;
2340   else if (old_node->clone.combined_args_to_skip)
2341     {
2342       int newi = 0, oldi = 0;
2343       tree arg;
2344       bitmap new_args_to_skip = BITMAP_GGC_ALLOC ();
2345       struct cgraph_node *orig_node;
2346       for (orig_node = old_node; orig_node->clone_of; orig_node = orig_node->clone_of)
2347         ;
2348       for (arg = DECL_ARGUMENTS (orig_node->decl); arg; arg = DECL_CHAIN (arg), oldi++)
2349         {
2350           if (bitmap_bit_p (old_node->clone.combined_args_to_skip, oldi))
2351             {
2352               bitmap_set_bit (new_args_to_skip, oldi);
2353               continue;
2354             }
2355           if (bitmap_bit_p (args_to_skip, newi))
2356             bitmap_set_bit (new_args_to_skip, oldi);
2357           newi++;
2358         }
2359       new_node->clone.combined_args_to_skip = new_args_to_skip;
2360     }
2361   else
2362     new_node->clone.combined_args_to_skip = args_to_skip;
2363   new_node->local.externally_visible = 0;
2364   new_node->local.local = 1;
2365   new_node->lowered = true;
2366   new_node->reachable = true;
2367
2368   cgraph_call_node_duplication_hooks (old_node, new_node);
2369
2370
2371   return new_node;
2372 }
2373
2374 /* NODE is no longer nested function; update cgraph accordingly.  */
2375 void
2376 cgraph_unnest_node (struct cgraph_node *node)
2377 {
2378   struct cgraph_node **node2 = &node->origin->nested;
2379   gcc_assert (node->origin);
2380
2381   while (*node2 != node)
2382     node2 = &(*node2)->next_nested;
2383   *node2 = node->next_nested;
2384   node->origin = NULL;
2385 }
2386
2387 /* Return function availability.  See cgraph.h for description of individual
2388    return values.  */
2389 enum availability
2390 cgraph_function_body_availability (struct cgraph_node *node)
2391 {
2392   enum availability avail;
2393   gcc_assert (cgraph_function_flags_ready);
2394   if (!node->analyzed)
2395     avail = AVAIL_NOT_AVAILABLE;
2396   else if (node->local.local)
2397     avail = AVAIL_LOCAL;
2398   else if (!node->local.externally_visible)
2399     avail = AVAIL_AVAILABLE;
2400   /* Inline functions are safe to be analyzed even if their symbol can
2401      be overwritten at runtime.  It is not meaningful to enforce any sane
2402      behaviour on replacing inline function by different body.  */
2403   else if (DECL_DECLARED_INLINE_P (node->decl))
2404     avail = AVAIL_AVAILABLE;
2405
2406   /* If the function can be overwritten, return OVERWRITABLE.  Take
2407      care at least of two notable extensions - the COMDAT functions
2408      used to share template instantiations in C++ (this is symmetric
2409      to code cp_cannot_inline_tree_fn and probably shall be shared and
2410      the inlinability hooks completely eliminated).
2411
2412      ??? Does the C++ one definition rule allow us to always return
2413      AVAIL_AVAILABLE here?  That would be good reason to preserve this
2414      bit.  */
2415
2416   else if (decl_replaceable_p (node->decl) && !DECL_EXTERNAL (node->decl))
2417     avail = AVAIL_OVERWRITABLE;
2418   else avail = AVAIL_AVAILABLE;
2419
2420   return avail;
2421 }
2422
2423 /* Add the function FNDECL to the call graph.
2424    Unlike cgraph_finalize_function, this function is intended to be used
2425    by middle end and allows insertion of new function at arbitrary point
2426    of compilation.  The function can be either in high, low or SSA form
2427    GIMPLE.
2428
2429    The function is assumed to be reachable and have address taken (so no
2430    API breaking optimizations are performed on it).
2431
2432    Main work done by this function is to enqueue the function for later
2433    processing to avoid need the passes to be re-entrant.  */
2434
2435 void
2436 cgraph_add_new_function (tree fndecl, bool lowered)
2437 {
2438   struct cgraph_node *node;
2439   switch (cgraph_state)
2440     {
2441       case CGRAPH_STATE_CONSTRUCTION:
2442         /* Just enqueue function to be processed at nearest occurrence.  */
2443         node = cgraph_create_node (fndecl);
2444         node->next_needed = cgraph_new_nodes;
2445         if (lowered)
2446           node->lowered = true;
2447         cgraph_new_nodes = node;
2448         break;
2449
2450       case CGRAPH_STATE_IPA:
2451       case CGRAPH_STATE_IPA_SSA:
2452       case CGRAPH_STATE_EXPANSION:
2453         /* Bring the function into finalized state and enqueue for later
2454            analyzing and compilation.  */
2455         node = cgraph_get_create_node (fndecl);
2456         node->local.local = false;
2457         node->local.finalized = true;
2458         node->reachable = node->needed = true;
2459         if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
2460           {
2461             push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2462             current_function_decl = fndecl;
2463             gimple_register_cfg_hooks ();
2464             tree_lowering_passes (fndecl);
2465             bitmap_obstack_initialize (NULL);
2466             if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2467               execute_pass_list (pass_early_local_passes.pass.sub);
2468             bitmap_obstack_release (NULL);
2469             pop_cfun ();
2470             current_function_decl = NULL;
2471
2472             lowered = true;
2473           }
2474         if (lowered)
2475           node->lowered = true;
2476         node->next_needed = cgraph_new_nodes;
2477         cgraph_new_nodes = node;
2478         break;
2479
2480       case CGRAPH_STATE_FINISHED:
2481         /* At the very end of compilation we have to do all the work up
2482            to expansion.  */
2483         node = cgraph_create_node (fndecl);
2484         if (lowered)
2485           node->lowered = true;
2486         cgraph_analyze_function (node);
2487         push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2488         current_function_decl = fndecl;
2489         gimple_register_cfg_hooks ();
2490         bitmap_obstack_initialize (NULL);
2491         if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2492           execute_pass_list (pass_early_local_passes.pass.sub);
2493         bitmap_obstack_release (NULL);
2494         tree_rest_of_compilation (fndecl);
2495         pop_cfun ();
2496         current_function_decl = NULL;
2497         break;
2498     }
2499
2500   /* Set a personality if required and we already passed EH lowering.  */
2501   if (lowered
2502       && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
2503           == eh_personality_lang))
2504     DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
2505 }
2506
2507 /* Return true if NODE can be made local for API change.
2508    Extern inline functions and C++ COMDAT functions can be made local
2509    at the expense of possible code size growth if function is used in multiple
2510    compilation units.  */
2511 bool
2512 cgraph_node_can_be_local_p (struct cgraph_node *node)
2513 {
2514   return (!node->needed && !node->address_taken
2515           && ((DECL_COMDAT (node->decl) && !node->same_comdat_group)
2516               || !node->local.externally_visible));
2517 }
2518
2519 /* Make DECL local.  FIXME: We shouldn't need to mess with rtl this early,
2520    but other code such as notice_global_symbol generates rtl.  */
2521 void
2522 cgraph_make_decl_local (tree decl)
2523 {
2524   rtx rtl, symbol;
2525
2526   if (TREE_CODE (decl) == VAR_DECL)
2527     DECL_COMMON (decl) = 0;
2528   else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
2529
2530   if (DECL_COMDAT (decl))
2531     {
2532       /* It is possible that we are linking against library defining same COMDAT
2533          function.  To avoid conflict we need to rename our local name of the
2534          function just in the case WHOPR partitioning decide to make it hidden
2535          to avoid cross partition references.  */
2536       if (flag_wpa)
2537         {
2538           const char *old_name;
2539
2540           old_name  = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2541           if (TREE_CODE (decl) == FUNCTION_DECL)
2542             {
2543               struct cgraph_node *node = cgraph_get_node_or_alias (decl);
2544               change_decl_assembler_name (decl,
2545                                           clone_function_name (decl, "local"));
2546               if (node->local.lto_file_data)
2547                 lto_record_renamed_decl (node->local.lto_file_data,
2548                                          old_name,
2549                                          IDENTIFIER_POINTER
2550                                            (DECL_ASSEMBLER_NAME (decl)));
2551             }
2552           else if (TREE_CODE (decl) == VAR_DECL)
2553             {
2554               struct varpool_node *vnode = varpool_get_node (decl);
2555               /* change_decl_assembler_name will warn here on vtables because
2556                  C++ frontend still sets TREE_SYMBOL_REFERENCED on them.  */
2557               SET_DECL_ASSEMBLER_NAME (decl,
2558                                        clone_function_name (decl, "local"));
2559               if (vnode->lto_file_data)
2560                 lto_record_renamed_decl (vnode->lto_file_data,
2561                                          old_name,
2562                                          IDENTIFIER_POINTER
2563                                            (DECL_ASSEMBLER_NAME (decl)));
2564             }
2565         }
2566       DECL_SECTION_NAME (decl) = 0;
2567       DECL_COMDAT (decl) = 0;
2568     }
2569   DECL_COMDAT_GROUP (decl) = 0;
2570   DECL_WEAK (decl) = 0;
2571   DECL_EXTERNAL (decl) = 0;
2572   TREE_PUBLIC (decl) = 0;
2573   if (!DECL_RTL_SET_P (decl))
2574     return;
2575
2576   /* Update rtl flags.  */
2577   make_decl_rtl (decl);
2578
2579   rtl = DECL_RTL (decl);
2580   if (!MEM_P (rtl))
2581     return;
2582
2583   symbol = XEXP (rtl, 0);
2584   if (GET_CODE (symbol) != SYMBOL_REF)
2585     return;
2586
2587   SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
2588 }
2589
2590 /* Bring NODE local.  */
2591 void
2592 cgraph_make_node_local (struct cgraph_node *node)
2593 {
2594   gcc_assert (cgraph_node_can_be_local_p (node));
2595   if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2596     {
2597       struct cgraph_node *alias;
2598       cgraph_make_decl_local (node->decl);
2599
2600       for (alias = node->same_body; alias; alias = alias->next)
2601         cgraph_make_decl_local (alias->decl);
2602
2603       node->local.externally_visible = false;
2604       node->local.local = true;
2605       node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2606       gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2607     }
2608 }
2609
2610 /* Set TREE_NOTHROW on NODE's decl and on same_body aliases of NODE
2611    if any to NOTHROW.  */
2612
2613 void
2614 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
2615 {
2616   struct cgraph_node *alias;
2617   TREE_NOTHROW (node->decl) = nothrow;
2618   for (alias = node->same_body; alias; alias = alias->next)
2619     TREE_NOTHROW (alias->decl) = nothrow;
2620 }
2621
2622 /* Set TREE_READONLY on NODE's decl and on same_body aliases of NODE
2623    if any to READONLY.  */
2624
2625 void
2626 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
2627 {
2628   struct cgraph_node *alias;
2629   /* Static constructors and destructors without a side effect can be
2630      optimized out.  */
2631   if (!looping && readonly)
2632     {
2633       if (DECL_STATIC_CONSTRUCTOR (node->decl))
2634         DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2635       if (DECL_STATIC_DESTRUCTOR (node->decl))
2636         DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2637     }
2638   TREE_READONLY (node->decl) = readonly;
2639   DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2640   for (alias = node->same_body; alias; alias = alias->next)
2641     {
2642       TREE_READONLY (alias->decl) = readonly;
2643       DECL_LOOPING_CONST_OR_PURE_P (alias->decl) = looping;
2644     }
2645 }
2646
2647 /* Set DECL_PURE_P on NODE's decl and on same_body aliases of NODE
2648    if any to PURE.  */
2649
2650 void
2651 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
2652 {
2653   struct cgraph_node *alias;
2654   /* Static constructors and destructors without a side effect can be
2655      optimized out.  */
2656   if (!looping && pure)
2657     {
2658       if (DECL_STATIC_CONSTRUCTOR (node->decl))
2659         DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2660       if (DECL_STATIC_DESTRUCTOR (node->decl))
2661         DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2662     }
2663   DECL_PURE_P (node->decl) = pure;
2664   DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2665   for (alias = node->same_body; alias; alias = alias->next)
2666     {
2667       DECL_PURE_P (alias->decl) = pure;
2668       DECL_LOOPING_CONST_OR_PURE_P (alias->decl) = looping;
2669     }
2670 }
2671
2672 /* See if the frequency of NODE can be updated based on frequencies of its
2673    callers.  */
2674 bool
2675 cgraph_propagate_frequency (struct cgraph_node *node)
2676 {
2677   bool maybe_unlikely_executed = true, maybe_executed_once = true;
2678   bool only_called_at_startup = true;
2679   bool only_called_at_exit = true;
2680   bool changed = false;
2681   struct cgraph_edge *edge;
2682
2683   if (!node->local.local)
2684     return false;
2685   gcc_assert (node->analyzed);
2686   if (dump_file && (dump_flags & TDF_DETAILS))
2687     fprintf (dump_file, "Processing frequency %s\n", cgraph_node_name (node));
2688
2689   for (edge = node->callers;
2690        edge && (maybe_unlikely_executed || maybe_executed_once
2691                 || only_called_at_startup || only_called_at_exit);
2692        edge = edge->next_caller)
2693     {
2694       if (edge->caller != node)
2695         {
2696           only_called_at_startup &= edge->caller->only_called_at_startup;
2697           /* It makes sense to put main() together with the static constructors.
2698              It will be executed for sure, but rest of functions called from
2699              main are definitely not at startup only.  */
2700           if (MAIN_NAME_P (DECL_NAME (edge->caller->decl)))
2701             only_called_at_startup = 0;
2702           only_called_at_exit &= edge->caller->only_called_at_exit;
2703         }
2704       if (!edge->frequency)
2705         continue;
2706       switch (edge->caller->frequency)
2707         {
2708         case NODE_FREQUENCY_UNLIKELY_EXECUTED:
2709           break;
2710         case NODE_FREQUENCY_EXECUTED_ONCE:
2711           if (dump_file && (dump_flags & TDF_DETAILS))
2712             fprintf (dump_file, "  Called by %s that is executed once\n",
2713                      cgraph_node_name (edge->caller));
2714           maybe_unlikely_executed = false;
2715           if (inline_edge_summary (edge)->loop_depth)
2716             {
2717               maybe_executed_once = false;
2718               if (dump_file && (dump_flags & TDF_DETAILS))
2719                 fprintf (dump_file, "  Called in loop\n");
2720             }
2721           break;
2722         case NODE_FREQUENCY_HOT:
2723         case NODE_FREQUENCY_NORMAL:
2724           if (dump_file && (dump_flags & TDF_DETAILS))
2725             fprintf (dump_file, "  Called by %s that is normal or hot\n",
2726                      cgraph_node_name (edge->caller));
2727           maybe_unlikely_executed = false;
2728           maybe_executed_once = false;
2729           break;
2730         }
2731     }
2732   if ((only_called_at_startup && !only_called_at_exit)
2733       && !node->only_called_at_startup)
2734     {
2735        node->only_called_at_startup = true;
2736        if (dump_file)
2737          fprintf (dump_file, "Node %s promoted to only called at startup.\n",
2738                   cgraph_node_name (node));
2739        changed = true;
2740     }
2741   if ((only_called_at_exit && !only_called_at_startup)
2742       && !node->only_called_at_exit)
2743     {
2744        node->only_called_at_exit = true;
2745        if (dump_file)
2746          fprintf (dump_file, "Node %s promoted to only called at exit.\n",
2747                   cgraph_node_name (node));
2748        changed = true;
2749     }
2750   /* These come either from profile or user hints; never update them.  */
2751   if (node->frequency == NODE_FREQUENCY_HOT
2752       || node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2753     return changed;
2754   if (maybe_unlikely_executed)
2755     {
2756       node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
2757       if (dump_file)
2758         fprintf (dump_file, "Node %s promoted to unlikely executed.\n",
2759                  cgraph_node_name (node));
2760       changed = true;
2761     }
2762   else if (maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE)
2763     {
2764       node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
2765       if (dump_file)
2766         fprintf (dump_file, "Node %s promoted to executed once.\n",
2767                  cgraph_node_name (node));
2768       changed = true;
2769     }
2770   return changed;
2771 }
2772
2773 /* Return true when NODE can not return or throw and thus
2774    it is safe to ignore its side effects for IPA analysis.  */
2775
2776 bool
2777 cgraph_node_cannot_return (struct cgraph_node *node)
2778 {
2779   int flags = flags_from_decl_or_type (node->decl);
2780   if (!flag_exceptions)
2781     return (flags & ECF_NORETURN) != 0;
2782   else
2783     return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2784              == (ECF_NORETURN | ECF_NOTHROW));
2785 }
2786
2787 /* Return true when call of E can not lead to return from caller
2788    and thus it is safe to ignore its side effects for IPA analysis
2789    when computing side effects of the caller.
2790    FIXME: We could actually mark all edges that have no reaching
2791    patch to EXIT_BLOCK_PTR or throw to get better results.  */
2792 bool
2793 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2794 {
2795   if (cgraph_node_cannot_return (e->caller))
2796     return true;
2797   if (e->indirect_unknown_callee)
2798     {
2799       int flags = e->indirect_info->ecf_flags;
2800       if (!flag_exceptions)
2801         return (flags & ECF_NORETURN) != 0;
2802       else
2803         return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2804                  == (ECF_NORETURN | ECF_NOTHROW));
2805     }
2806   else
2807     return cgraph_node_cannot_return (e->callee);
2808 }
2809
2810 /* Return true when function NODE can be removed from callgraph
2811    if all direct calls are eliminated.  */
2812
2813 bool
2814 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2815 {
2816   gcc_assert (!node->global.inlined_to);
2817   /* Extern inlines can always go, we will use the external definition.  */
2818   if (DECL_EXTERNAL (node->decl))
2819     return true;
2820   /* When function is needed, we can not remove it.  */
2821   if (node->needed || node->reachable_from_other_partition)
2822     return false;
2823   if (DECL_STATIC_CONSTRUCTOR (node->decl)
2824       || DECL_STATIC_DESTRUCTOR (node->decl))
2825     return false;
2826   /* Only COMDAT functions can be removed if externally visible.  */
2827   if (node->local.externally_visible
2828       && (!DECL_COMDAT (node->decl)
2829           || cgraph_used_from_object_file_p (node)))
2830     return false;
2831   return true;
2832 }
2833
2834 /* Return true when function NODE can be expected to be removed
2835    from program when direct calls in this compilation unit are removed.
2836
2837    As a special case COMDAT functions are
2838    cgraph_can_remove_if_no_direct_calls_p while the are not
2839    cgraph_only_called_directly_p (it is possible they are called from other
2840    unit)
2841
2842    This function behaves as cgraph_only_called_directly_p because eliminating
2843    all uses of COMDAT function does not make it necessarily disappear from
2844    the program unless we are compiling whole program or we do LTO.  In this
2845    case we know we win since dynamic linking will not really discard the
2846    linkonce section.  */
2847
2848 bool
2849 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2850 {
2851   gcc_assert (!node->global.inlined_to);
2852   if (cgraph_used_from_object_file_p (node))
2853     return false;
2854   if (!in_lto_p && !flag_whole_program)
2855     return cgraph_only_called_directly_p (node);
2856   else
2857     {
2858        if (DECL_EXTERNAL (node->decl))
2859          return true;
2860       return cgraph_can_remove_if_no_direct_calls_p (node);
2861     }
2862 }
2863
2864 /* Return true when RESOLUTION indicate that linker will use
2865    the symbol from non-LTO object files.  */
2866
2867 bool
2868 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
2869 {
2870   return (resolution == LDPR_PREVAILING_DEF
2871           || resolution == LDPR_PREEMPTED_REG
2872           || resolution == LDPR_RESOLVED_EXEC
2873           || resolution == LDPR_RESOLVED_DYN);
2874 }
2875
2876 /* Return true when NODE is known to be used from other (non-LTO) object file.
2877    Known only when doing LTO via linker plugin.  */
2878
2879 bool
2880 cgraph_used_from_object_file_p (struct cgraph_node *node)
2881 {
2882   struct cgraph_node *alias;
2883
2884   gcc_assert (!node->global.inlined_to);
2885   if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
2886     return false;
2887   if (resolution_used_from_other_file_p (node->resolution))
2888     return true;
2889   for (alias = node->same_body; alias; alias = alias->next)
2890     if (TREE_PUBLIC (alias->decl)
2891         && resolution_used_from_other_file_p (alias->resolution))
2892       return true;
2893   return false;
2894 }
2895
2896 #include "gt-cgraph.h"