OSDN Git Service

* cgraphbuild.c: Include ipa-inline.h.
[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 /* Maximal pid used for profiling */
146 int cgraph_max_pid;
147
148 /* Set when whole unit has been analyzed so we can access global info.  */
149 bool cgraph_global_info_ready = false;
150
151 /* What state callgraph is in right now.  */
152 enum cgraph_state cgraph_state = CGRAPH_STATE_CONSTRUCTION;
153
154 /* Set when the cgraph is fully build and the basic flags are computed.  */
155 bool cgraph_function_flags_ready = false;
156
157 /* Linked list of cgraph asm nodes.  */
158 struct cgraph_asm_node *cgraph_asm_nodes;
159
160 /* Last node in cgraph_asm_nodes.  */
161 static GTY(()) struct cgraph_asm_node *cgraph_asm_last_node;
162
163 /* The order index of the next cgraph node to be created.  This is
164    used so that we can sort the cgraph nodes in order by when we saw
165    them, to support -fno-toplevel-reorder.  */
166 int cgraph_order;
167
168 /* List of hooks triggered on cgraph_edge events.  */
169 struct cgraph_edge_hook_list {
170   cgraph_edge_hook hook;
171   void *data;
172   struct cgraph_edge_hook_list *next;
173 };
174
175 /* List of hooks triggered on cgraph_node events.  */
176 struct cgraph_node_hook_list {
177   cgraph_node_hook hook;
178   void *data;
179   struct cgraph_node_hook_list *next;
180 };
181
182 /* List of hooks triggered on events involving two cgraph_edges.  */
183 struct cgraph_2edge_hook_list {
184   cgraph_2edge_hook hook;
185   void *data;
186   struct cgraph_2edge_hook_list *next;
187 };
188
189 /* List of hooks triggered on events involving two cgraph_nodes.  */
190 struct cgraph_2node_hook_list {
191   cgraph_2node_hook hook;
192   void *data;
193   struct cgraph_2node_hook_list *next;
194 };
195
196 /* List of hooks triggered when an edge is removed.  */
197 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
198 /* List of hooks triggered when a node is removed.  */
199 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
200 /* List of hooks triggered when an edge is duplicated.  */
201 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
202 /* List of hooks triggered when a node is duplicated.  */
203 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
204 /* List of hooks triggered when an function is inserted.  */
205 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
206
207 /* Head of a linked list of unused (freed) call graph nodes.
208    Do not GTY((delete)) this list so UIDs gets reliably recycled.  */
209 static GTY(()) struct cgraph_node *free_nodes;
210 /* Head of a linked list of unused (freed) call graph edges.
211    Do not GTY((delete)) this list so UIDs gets reliably recycled.  */
212 static GTY(()) struct cgraph_edge *free_edges;
213
214 /* Macros to access the next item in the list of free cgraph nodes and
215    edges. */
216 #define NEXT_FREE_NODE(NODE) (NODE)->next
217 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
218
219 /* Register HOOK to be called with DATA on each removed edge.  */
220 struct cgraph_edge_hook_list *
221 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
222 {
223   struct cgraph_edge_hook_list *entry;
224   struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
225
226   entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
227   entry->hook = hook;
228   entry->data = data;
229   entry->next = NULL;
230   while (*ptr)
231     ptr = &(*ptr)->next;
232   *ptr = entry;
233   return entry;
234 }
235
236 /* Remove ENTRY from the list of hooks called on removing edges.  */
237 void
238 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
239 {
240   struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
241
242   while (*ptr != entry)
243     ptr = &(*ptr)->next;
244   *ptr = entry->next;
245   free (entry);
246 }
247
248 /* Call all edge removal hooks.  */
249 static void
250 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
251 {
252   struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
253   while (entry)
254   {
255     entry->hook (e, entry->data);
256     entry = entry->next;
257   }
258 }
259
260 /* Register HOOK to be called with DATA on each removed node.  */
261 struct cgraph_node_hook_list *
262 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
263 {
264   struct cgraph_node_hook_list *entry;
265   struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
266
267   entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
268   entry->hook = hook;
269   entry->data = data;
270   entry->next = NULL;
271   while (*ptr)
272     ptr = &(*ptr)->next;
273   *ptr = entry;
274   return entry;
275 }
276
277 /* Remove ENTRY from the list of hooks called on removing nodes.  */
278 void
279 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
280 {
281   struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
282
283   while (*ptr != entry)
284     ptr = &(*ptr)->next;
285   *ptr = entry->next;
286   free (entry);
287 }
288
289 /* Call all node removal hooks.  */
290 static void
291 cgraph_call_node_removal_hooks (struct cgraph_node *node)
292 {
293   struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
294   while (entry)
295   {
296     entry->hook (node, entry->data);
297     entry = entry->next;
298   }
299 }
300
301 /* Register HOOK to be called with DATA on each inserted node.  */
302 struct cgraph_node_hook_list *
303 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
304 {
305   struct cgraph_node_hook_list *entry;
306   struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
307
308   entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
309   entry->hook = hook;
310   entry->data = data;
311   entry->next = NULL;
312   while (*ptr)
313     ptr = &(*ptr)->next;
314   *ptr = entry;
315   return entry;
316 }
317
318 /* Remove ENTRY from the list of hooks called on inserted nodes.  */
319 void
320 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
321 {
322   struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
323
324   while (*ptr != entry)
325     ptr = &(*ptr)->next;
326   *ptr = entry->next;
327   free (entry);
328 }
329
330 /* Call all node insertion hooks.  */
331 void
332 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
333 {
334   struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
335   while (entry)
336   {
337     entry->hook (node, entry->data);
338     entry = entry->next;
339   }
340 }
341
342 /* Register HOOK to be called with DATA on each duplicated edge.  */
343 struct cgraph_2edge_hook_list *
344 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
345 {
346   struct cgraph_2edge_hook_list *entry;
347   struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
348
349   entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
350   entry->hook = hook;
351   entry->data = data;
352   entry->next = NULL;
353   while (*ptr)
354     ptr = &(*ptr)->next;
355   *ptr = entry;
356   return entry;
357 }
358
359 /* Remove ENTRY from the list of hooks called on duplicating edges.  */
360 void
361 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
362 {
363   struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
364
365   while (*ptr != entry)
366     ptr = &(*ptr)->next;
367   *ptr = entry->next;
368   free (entry);
369 }
370
371 /* Call all edge duplication hooks.  */
372 static void
373 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
374                                     struct cgraph_edge *cs2)
375 {
376   struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
377   while (entry)
378   {
379     entry->hook (cs1, cs2, entry->data);
380     entry = entry->next;
381   }
382 }
383
384 /* Register HOOK to be called with DATA on each duplicated node.  */
385 struct cgraph_2node_hook_list *
386 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
387 {
388   struct cgraph_2node_hook_list *entry;
389   struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
390
391   entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
392   entry->hook = hook;
393   entry->data = data;
394   entry->next = NULL;
395   while (*ptr)
396     ptr = &(*ptr)->next;
397   *ptr = entry;
398   return entry;
399 }
400
401 /* Remove ENTRY from the list of hooks called on duplicating nodes.  */
402 void
403 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
404 {
405   struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
406
407   while (*ptr != entry)
408     ptr = &(*ptr)->next;
409   *ptr = entry->next;
410   free (entry);
411 }
412
413 /* Call all node duplication hooks.  */
414 static void
415 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
416                                     struct cgraph_node *node2)
417 {
418   struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
419   while (entry)
420   {
421     entry->hook (node1, node2, entry->data);
422     entry = entry->next;
423   }
424 }
425
426 /* Returns a hash code for P.  */
427
428 static hashval_t
429 hash_node (const void *p)
430 {
431   const struct cgraph_node *n = (const struct cgraph_node *) p;
432   return (hashval_t) DECL_UID (n->decl);
433 }
434
435
436 /* Returns nonzero if P1 and P2 are equal.  */
437
438 static int
439 eq_node (const void *p1, const void *p2)
440 {
441   const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
442   const struct cgraph_node *n2 = (const struct cgraph_node *) p2;
443   return DECL_UID (n1->decl) == DECL_UID (n2->decl);
444 }
445
446 /* Allocate new callgraph node.  */
447
448 static inline struct cgraph_node *
449 cgraph_allocate_node (void)
450 {
451   struct cgraph_node *node;
452
453   if (free_nodes)
454     {
455       node = free_nodes;
456       free_nodes = NEXT_FREE_NODE (node);
457     }
458   else
459     {
460       node = ggc_alloc_cleared_cgraph_node ();
461       node->uid = cgraph_max_uid++;
462     }
463
464   return node;
465 }
466
467 /* Allocate new callgraph node and insert it into basic data structures.  */
468
469 static struct cgraph_node *
470 cgraph_create_node_1 (void)
471 {
472   struct cgraph_node *node = cgraph_allocate_node ();
473
474   node->next = cgraph_nodes;
475   node->pid = -1;
476   node->order = cgraph_order++;
477   if (cgraph_nodes)
478     cgraph_nodes->previous = node;
479   node->previous = NULL;
480   node->frequency = NODE_FREQUENCY_NORMAL;
481   node->count_materialization_scale = REG_BR_PROB_BASE;
482   ipa_empty_ref_list (&node->ref_list);
483   cgraph_nodes = node;
484   cgraph_n_nodes++;
485   return node;
486 }
487
488 /* Return cgraph node assigned to DECL.  Create new one when needed.  */
489
490 struct cgraph_node *
491 cgraph_create_node (tree decl)
492 {
493   struct cgraph_node key, *node, **slot;
494
495   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
496
497   if (!cgraph_hash)
498     cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
499
500   key.decl = decl;
501   slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
502   gcc_assert (!*slot);
503
504   node = cgraph_create_node_1 ();
505   node->decl = decl;
506   *slot = node;
507   if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
508     {
509       node->origin = cgraph_get_create_node (DECL_CONTEXT (decl));
510       node->next_nested = node->origin->nested;
511       node->origin->nested = node;
512     }
513   if (assembler_name_hash)
514     {
515       void **aslot;
516       tree name = DECL_ASSEMBLER_NAME (decl);
517
518       aslot = htab_find_slot_with_hash (assembler_name_hash, name,
519                                         decl_assembler_name_hash (name),
520                                         INSERT);
521       /* We can have multiple declarations with same assembler name. For C++
522          it is __builtin_strlen and strlen, for instance.  Do we need to
523          record them all?  Original implementation marked just first one
524          so lets hope for the best.  */
525       if (*aslot == NULL)
526         *aslot = node;
527     }
528   return node;
529 }
530
531 /* Try to find a call graph node for declaration DECL and if it does not exist,
532    create it.  */
533
534 struct cgraph_node *
535 cgraph_get_create_node (tree decl)
536 {
537   struct cgraph_node *node;
538
539   node = cgraph_get_node (decl);
540   if (node)
541     return node;
542
543   return cgraph_create_node (decl);
544 }
545
546 /* Mark ALIAS as an alias to DECL.  DECL_NODE is cgraph node representing
547    the function body is associated with (not neccesarily cgraph_node (DECL).  */
548
549 static struct cgraph_node *
550 cgraph_same_body_alias_1 (struct cgraph_node *decl_node, tree alias, tree decl)
551 {
552   struct cgraph_node key, *alias_node, **slot;
553
554   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
555   gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
556
557   key.decl = alias;
558
559   slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
560
561   /* If the cgraph_node has been already created, fail.  */
562   if (*slot)
563     return NULL;
564
565   alias_node = cgraph_allocate_node ();
566   alias_node->decl = alias;
567   alias_node->same_body_alias = 1;
568   alias_node->same_body = decl_node;
569   alias_node->previous = NULL;
570   if (decl_node->same_body)
571     decl_node->same_body->previous = alias_node;
572   alias_node->next = decl_node->same_body;
573   alias_node->thunk.alias = decl;
574   decl_node->same_body = alias_node;
575   *slot = alias_node;
576   return alias_node;
577 }
578
579 /* Attempt to mark ALIAS as an alias to DECL.  Return alias node if successful
580    and NULL otherwise.
581    Same body aliases are output whenever the body of DECL is output,
582    and cgraph_get_node (ALIAS) transparently returns cgraph_get_node (DECL).  */
583
584 struct cgraph_node *
585 cgraph_same_body_alias (struct cgraph_node *decl_node, tree alias, tree decl)
586 {
587 #ifndef ASM_OUTPUT_DEF
588   /* If aliases aren't supported by the assembler, fail.  */
589   return NULL;
590 #endif
591
592   /*gcc_assert (!assembler_name_hash);*/
593
594   return cgraph_same_body_alias_1 (decl_node, alias, decl);
595 }
596
597 /* Add thunk alias into callgraph.  The alias declaration is ALIAS and it
598    aliases DECL with an adjustments made into the first parameter.
599    See comments in thunk_adjust for detail on the parameters.  */
600
601 struct cgraph_node *
602 cgraph_add_thunk (struct cgraph_node *decl_node, tree alias, tree decl,
603                   bool this_adjusting,
604                   HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value,
605                   tree virtual_offset,
606                   tree real_alias)
607 {
608   struct cgraph_node *node = cgraph_get_node (alias);
609
610   if (node)
611     {
612       gcc_assert (node->local.finalized);
613       gcc_assert (!node->same_body);
614       cgraph_remove_node (node);
615     }
616   
617   node = cgraph_same_body_alias_1 (decl_node, alias, decl);
618   gcc_assert (node);
619   gcc_checking_assert (!virtual_offset
620                        || tree_int_cst_equal (virtual_offset,
621                                               size_int (virtual_value)));
622   node->thunk.fixed_offset = fixed_offset;
623   node->thunk.this_adjusting = this_adjusting;
624   node->thunk.virtual_value = virtual_value;
625   node->thunk.virtual_offset_p = virtual_offset != NULL;
626   node->thunk.alias = real_alias;
627   node->thunk.thunk_p = true;
628   return node;
629 }
630
631 /* Returns the cgraph node assigned to DECL or NULL if no cgraph node
632    is assigned.  */
633
634 struct cgraph_node *
635 cgraph_get_node_or_alias (const_tree decl)
636 {
637   struct cgraph_node key, *node = NULL, **slot;
638
639   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
640
641   if (!cgraph_hash)
642     return NULL;
643
644   key.decl = CONST_CAST2 (tree, const_tree, decl);
645
646   slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key,
647                                                  NO_INSERT);
648
649   if (slot && *slot)
650     node = *slot;
651   return node;
652 }
653
654 /* Returns the cgraph node assigned to DECL or NULL if no cgraph node
655    is assigned.  */
656
657 struct cgraph_node *
658 cgraph_get_node (const_tree decl)
659 {
660   struct cgraph_node key, *node = NULL, **slot;
661
662   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
663
664   if (!cgraph_hash)
665     return NULL;
666
667   key.decl = CONST_CAST2 (tree, const_tree, decl);
668
669   slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key,
670                                                  NO_INSERT);
671
672   if (slot && *slot)
673     {
674       node = *slot;
675       if (node->same_body_alias)
676         node = node->same_body;
677     }
678   return node;
679 }
680
681 /* Insert already constructed node into hashtable.  */
682
683 void
684 cgraph_insert_node_to_hashtable (struct cgraph_node *node)
685 {
686   struct cgraph_node **slot;
687
688   slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, node, INSERT);
689
690   gcc_assert (!*slot);
691   *slot = node;
692 }
693
694 /* Returns a hash code for P.  */
695
696 static hashval_t
697 hash_node_by_assembler_name (const void *p)
698 {
699   const struct cgraph_node *n = (const struct cgraph_node *) p;
700   return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
701 }
702
703 /* Returns nonzero if P1 and P2 are equal.  */
704
705 static int
706 eq_assembler_name (const void *p1, const void *p2)
707 {
708   const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
709   const_tree name = (const_tree)p2;
710   return (decl_assembler_name_equal (n1->decl, name));
711 }
712
713 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
714    Return NULL if there's no such node.  */
715
716 struct cgraph_node *
717 cgraph_node_for_asm (tree asmname)
718 {
719   struct cgraph_node *node;
720   void **slot;
721
722   if (!assembler_name_hash)
723     {
724       assembler_name_hash =
725         htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
726                          NULL);
727       for (node = cgraph_nodes; node; node = node->next)
728         if (!node->global.inlined_to)
729           {
730             tree name = DECL_ASSEMBLER_NAME (node->decl);
731             slot = htab_find_slot_with_hash (assembler_name_hash, name,
732                                              decl_assembler_name_hash (name),
733                                              INSERT);
734             /* We can have multiple declarations with same assembler name. For C++
735                it is __builtin_strlen and strlen, for instance.  Do we need to
736                record them all?  Original implementation marked just first one
737                so lets hope for the best.  */
738             if (!*slot)
739               *slot = node;
740             if (node->same_body)
741               {
742                 struct cgraph_node *alias;
743
744                 for (alias = node->same_body; alias; alias = alias->next)
745                   {
746                     hashval_t hash;
747                     name = DECL_ASSEMBLER_NAME (alias->decl);
748                     hash = decl_assembler_name_hash (name);
749                     slot = htab_find_slot_with_hash (assembler_name_hash, name,
750                                                      hash,  INSERT);
751                     if (!*slot)
752                       *slot = alias;
753                   }
754               }
755           }
756     }
757
758   slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
759                                    decl_assembler_name_hash (asmname),
760                                    NO_INSERT);
761
762   if (slot)
763     {
764       node = (struct cgraph_node *) *slot;
765       if (node->same_body_alias)
766         node = node->same_body;
767       return node;
768     }
769   return NULL;
770 }
771
772 /* Returns a hash value for X (which really is a die_struct).  */
773
774 static hashval_t
775 edge_hash (const void *x)
776 {
777   return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
778 }
779
780 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
781
782 static int
783 edge_eq (const void *x, const void *y)
784 {
785   return ((const struct cgraph_edge *) x)->call_stmt == y;
786 }
787
788 /* Add call graph edge E to call site hash of its caller.  */
789
790 static inline void
791 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
792 {
793   void **slot;
794   slot = htab_find_slot_with_hash (e->caller->call_site_hash,
795                                    e->call_stmt,
796                                    htab_hash_pointer (e->call_stmt),
797                                    INSERT);
798   gcc_assert (!*slot);
799   *slot = e;
800 }
801
802 /* Return the callgraph edge representing the GIMPLE_CALL statement
803    CALL_STMT.  */
804
805 struct cgraph_edge *
806 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
807 {
808   struct cgraph_edge *e, *e2;
809   int n = 0;
810
811   if (node->call_site_hash)
812     return (struct cgraph_edge *)
813       htab_find_with_hash (node->call_site_hash, call_stmt,
814                            htab_hash_pointer (call_stmt));
815
816   /* This loop may turn out to be performance problem.  In such case adding
817      hashtables into call nodes with very many edges is probably best
818      solution.  It is not good idea to add pointer into CALL_EXPR itself
819      because we want to make possible having multiple cgraph nodes representing
820      different clones of the same body before the body is actually cloned.  */
821   for (e = node->callees; e; e = e->next_callee)
822     {
823       if (e->call_stmt == call_stmt)
824         break;
825       n++;
826     }
827
828   if (!e)
829     for (e = node->indirect_calls; e; e = e->next_callee)
830       {
831         if (e->call_stmt == call_stmt)
832           break;
833         n++;
834       }
835
836   if (n > 100)
837     {
838       node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
839       for (e2 = node->callees; e2; e2 = e2->next_callee)
840         cgraph_add_edge_to_call_site_hash (e2);
841       for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
842         cgraph_add_edge_to_call_site_hash (e2);
843     }
844
845   return e;
846 }
847
848
849 /* Change field call_stmt of edge E to NEW_STMT.  */
850
851 void
852 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
853 {
854   tree decl;
855
856   if (e->caller->call_site_hash)
857     {
858       htab_remove_elt_with_hash (e->caller->call_site_hash,
859                                  e->call_stmt,
860                                  htab_hash_pointer (e->call_stmt));
861     }
862
863   e->call_stmt = new_stmt;
864   if (e->indirect_unknown_callee
865       && (decl = gimple_call_fndecl (new_stmt)))
866     {
867       /* Constant propagation (and possibly also inlining?) can turn an
868          indirect call into a direct one.  */
869       struct cgraph_node *new_callee = cgraph_get_node (decl);
870
871       gcc_checking_assert (new_callee);
872       cgraph_make_edge_direct (e, new_callee, 0);
873     }
874
875   push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
876   e->can_throw_external = stmt_can_throw_external (new_stmt);
877   pop_cfun ();
878   if (e->caller->call_site_hash)
879     cgraph_add_edge_to_call_site_hash (e);
880 }
881
882 /* Like cgraph_set_call_stmt but walk the clone tree and update all
883    clones sharing the same function body.  */
884
885 void
886 cgraph_set_call_stmt_including_clones (struct cgraph_node *orig,
887                                        gimple old_stmt, gimple new_stmt)
888 {
889   struct cgraph_node *node;
890   struct cgraph_edge *edge = cgraph_edge (orig, old_stmt);
891
892   if (edge)
893     cgraph_set_call_stmt (edge, new_stmt);
894
895   node = orig->clones;
896   if (node)
897     while (node != orig)
898       {
899         struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
900         if (edge)
901           cgraph_set_call_stmt (edge, new_stmt);
902         if (node->clones)
903           node = node->clones;
904         else if (node->next_sibling_clone)
905           node = node->next_sibling_clone;
906         else
907           {
908             while (node != orig && !node->next_sibling_clone)
909               node = node->clone_of;
910             if (node != orig)
911               node = node->next_sibling_clone;
912           }
913       }
914 }
915
916 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
917    same function body.  If clones already have edge for OLD_STMT; only
918    update the edge same way as cgraph_set_call_stmt_including_clones does.
919
920    TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
921    frequencies of the clones.  */
922
923 void
924 cgraph_create_edge_including_clones (struct cgraph_node *orig,
925                                      struct cgraph_node *callee,
926                                      gimple old_stmt,
927                                      gimple stmt, gcov_type count,
928                                      int freq, int loop_depth,
929                                      cgraph_inline_failed_t reason)
930 {
931   struct cgraph_node *node;
932   struct cgraph_edge *edge;
933
934   if (!cgraph_edge (orig, stmt))
935     {
936       edge = cgraph_create_edge (orig, callee, stmt, count, freq, loop_depth);
937       edge->inline_failed = reason;
938     }
939
940   node = orig->clones;
941   if (node)
942     while (node != orig)
943       {
944         struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
945
946         /* It is possible that clones already contain the edge while
947            master didn't.  Either we promoted indirect call into direct
948            call in the clone or we are processing clones of unreachable
949            master where edges has been removed.  */
950         if (edge)
951           cgraph_set_call_stmt (edge, stmt);
952         else if (!cgraph_edge (node, stmt))
953           {
954             edge = cgraph_create_edge (node, callee, stmt, count,
955                                        freq, loop_depth);
956             edge->inline_failed = reason;
957           }
958
959         if (node->clones)
960           node = node->clones;
961         else if (node->next_sibling_clone)
962           node = node->next_sibling_clone;
963         else
964           {
965             while (node != orig && !node->next_sibling_clone)
966               node = node->clone_of;
967             if (node != orig)
968               node = node->next_sibling_clone;
969           }
970       }
971 }
972
973 /* Allocate a cgraph_edge structure and fill it with data according to the
974    parameters of which only CALLEE can be NULL (when creating an indirect call
975    edge).  */
976
977 static struct cgraph_edge *
978 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
979                        gimple call_stmt, gcov_type count, int freq, int nest)
980 {
981   struct cgraph_edge *edge;
982
983   /* LTO does not actually have access to the call_stmt since these
984      have not been loaded yet.  */
985   if (call_stmt)
986     {
987       /* This is a rather expensive check possibly triggering
988          construction of call stmt hashtable.  */
989       gcc_checking_assert (!cgraph_edge (caller, call_stmt));
990
991       gcc_assert (is_gimple_call (call_stmt));
992     }
993
994   if (free_edges)
995     {
996       edge = free_edges;
997       free_edges = NEXT_FREE_EDGE (edge);
998     }
999   else
1000     {
1001       edge = ggc_alloc_cgraph_edge ();
1002       edge->uid = cgraph_edge_max_uid++;
1003     }
1004
1005   edge->aux = NULL;
1006   edge->caller = caller;
1007   edge->callee = callee;
1008   edge->prev_caller = NULL;
1009   edge->next_caller = NULL;
1010   edge->prev_callee = NULL;
1011   edge->next_callee = NULL;
1012
1013   edge->count = count;
1014   gcc_assert (count >= 0);
1015   edge->frequency = freq;
1016   gcc_assert (freq >= 0);
1017   gcc_assert (freq <= CGRAPH_FREQ_MAX);
1018   edge->loop_nest = nest;
1019
1020   edge->call_stmt = call_stmt;
1021   edge->call_stmt_size = 0;
1022   edge->call_stmt_time = 0;
1023   push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
1024   edge->can_throw_external
1025     = call_stmt ? stmt_can_throw_external (call_stmt) : false;
1026   pop_cfun ();
1027   edge->call_stmt_cannot_inline_p =
1028     (call_stmt ? gimple_call_cannot_inline_p (call_stmt) : false);
1029   if (call_stmt && caller->call_site_hash)
1030     cgraph_add_edge_to_call_site_hash (edge);
1031
1032   edge->indirect_info = NULL;
1033   edge->indirect_inlining_edge = 0;
1034
1035   return edge;
1036 }
1037
1038 /* Create edge from CALLER to CALLEE in the cgraph.  */
1039
1040 struct cgraph_edge *
1041 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
1042                     gimple call_stmt, gcov_type count, int freq, int nest)
1043 {
1044   struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
1045                                                    count, freq, nest);
1046
1047   edge->indirect_unknown_callee = 0;
1048   initialize_inline_failed (edge);
1049
1050   edge->next_caller = callee->callers;
1051   if (callee->callers)
1052     callee->callers->prev_caller = edge;
1053   edge->next_callee = caller->callees;
1054   if (caller->callees)
1055     caller->callees->prev_callee = edge;
1056   caller->callees = edge;
1057   callee->callers = edge;
1058
1059   return edge;
1060 }
1061
1062 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
1063
1064 struct cgraph_indirect_call_info *
1065 cgraph_allocate_init_indirect_info (void)
1066 {
1067   struct cgraph_indirect_call_info *ii;
1068
1069   ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
1070   ii->param_index = -1;
1071   return ii;
1072 }
1073
1074 /* Create an indirect edge with a yet-undetermined callee where the call
1075    statement destination is a formal parameter of the caller with index
1076    PARAM_INDEX. */
1077
1078 struct cgraph_edge *
1079 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
1080                              int ecf_flags,
1081                              gcov_type count, int freq, int nest)
1082 {
1083   struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
1084                                                    count, freq, nest);
1085
1086   edge->indirect_unknown_callee = 1;
1087   initialize_inline_failed (edge);
1088
1089   edge->indirect_info = cgraph_allocate_init_indirect_info ();
1090   edge->indirect_info->ecf_flags = ecf_flags;
1091
1092   edge->next_callee = caller->indirect_calls;
1093   if (caller->indirect_calls)
1094     caller->indirect_calls->prev_callee = edge;
1095   caller->indirect_calls = edge;
1096
1097   return edge;
1098 }
1099
1100 /* Remove the edge E from the list of the callers of the callee.  */
1101
1102 static inline void
1103 cgraph_edge_remove_callee (struct cgraph_edge *e)
1104 {
1105   gcc_assert (!e->indirect_unknown_callee);
1106   if (e->prev_caller)
1107     e->prev_caller->next_caller = e->next_caller;
1108   if (e->next_caller)
1109     e->next_caller->prev_caller = e->prev_caller;
1110   if (!e->prev_caller)
1111     e->callee->callers = e->next_caller;
1112 }
1113
1114 /* Remove the edge E from the list of the callees of the caller.  */
1115
1116 static inline void
1117 cgraph_edge_remove_caller (struct cgraph_edge *e)
1118 {
1119   if (e->prev_callee)
1120     e->prev_callee->next_callee = e->next_callee;
1121   if (e->next_callee)
1122     e->next_callee->prev_callee = e->prev_callee;
1123   if (!e->prev_callee)
1124     {
1125       if (e->indirect_unknown_callee)
1126         e->caller->indirect_calls = e->next_callee;
1127       else
1128         e->caller->callees = e->next_callee;
1129     }
1130   if (e->caller->call_site_hash)
1131     htab_remove_elt_with_hash (e->caller->call_site_hash,
1132                                e->call_stmt,
1133                                htab_hash_pointer (e->call_stmt));
1134 }
1135
1136 /* Put the edge onto the free list.  */
1137
1138 static void
1139 cgraph_free_edge (struct cgraph_edge *e)
1140 {
1141   int uid = e->uid;
1142
1143   /* Clear out the edge so we do not dangle pointers.  */
1144   memset (e, 0, sizeof (*e));
1145   e->uid = uid;
1146   NEXT_FREE_EDGE (e) = free_edges;
1147   free_edges = e;
1148 }
1149
1150 /* Remove the edge E in the cgraph.  */
1151
1152 void
1153 cgraph_remove_edge (struct cgraph_edge *e)
1154 {
1155   /* Call all edge removal hooks.  */
1156   cgraph_call_edge_removal_hooks (e);
1157
1158   if (!e->indirect_unknown_callee)
1159     /* Remove from callers list of the callee.  */
1160     cgraph_edge_remove_callee (e);
1161
1162   /* Remove from callees list of the callers.  */
1163   cgraph_edge_remove_caller (e);
1164
1165   /* Put the edge onto the free list.  */
1166   cgraph_free_edge (e);
1167 }
1168
1169 /* Set callee of call graph edge E and add it to the corresponding set of
1170    callers. */
1171
1172 static void
1173 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1174 {
1175   e->prev_caller = NULL;
1176   if (n->callers)
1177     n->callers->prev_caller = e;
1178   e->next_caller = n->callers;
1179   n->callers = e;
1180   e->callee = n;
1181 }
1182
1183 /* Redirect callee of E to N.  The function does not update underlying
1184    call expression.  */
1185
1186 void
1187 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1188 {
1189   /* Remove from callers list of the current callee.  */
1190   cgraph_edge_remove_callee (e);
1191
1192   /* Insert to callers list of the new callee.  */
1193   cgraph_set_edge_callee (e, n);
1194 }
1195
1196 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1197    CALLEE.  DELTA is an integer constant that is to be added to the this
1198    pointer (first parameter) to compensate for skipping a thunk adjustment.  */
1199
1200 void
1201 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee,
1202                          HOST_WIDE_INT delta)
1203 {
1204   edge->indirect_unknown_callee = 0;
1205   edge->indirect_info->thunk_delta = delta;
1206
1207   /* Get the edge out of the indirect edge list. */
1208   if (edge->prev_callee)
1209     edge->prev_callee->next_callee = edge->next_callee;
1210   if (edge->next_callee)
1211     edge->next_callee->prev_callee = edge->prev_callee;
1212   if (!edge->prev_callee)
1213     edge->caller->indirect_calls = edge->next_callee;
1214
1215   /* Put it into the normal callee list */
1216   edge->prev_callee = NULL;
1217   edge->next_callee = edge->caller->callees;
1218   if (edge->caller->callees)
1219     edge->caller->callees->prev_callee = edge;
1220   edge->caller->callees = edge;
1221
1222   /* Insert to callers list of the new callee.  */
1223   cgraph_set_edge_callee (edge, callee);
1224
1225   /* We need to re-determine the inlining status of the edge.  */
1226   initialize_inline_failed (edge);
1227 }
1228
1229
1230 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1231    OLD_STMT changed into NEW_STMT.  OLD_CALL is gimple_call_fndecl
1232    of OLD_STMT if it was previously call statement.  */
1233
1234 static void
1235 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1236                                         gimple old_stmt, tree old_call, gimple new_stmt)
1237 {
1238   tree new_call = (is_gimple_call (new_stmt)) ? gimple_call_fndecl (new_stmt) : 0;
1239
1240   /* We are seeing indirect calls, then there is nothing to update.  */
1241   if (!new_call && !old_call)
1242     return;
1243   /* See if we turned indirect call into direct call or folded call to one builtin
1244      into different builtin.  */
1245   if (old_call != new_call)
1246     {
1247       struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1248       struct cgraph_edge *ne = NULL;
1249       gcov_type count;
1250       int frequency;
1251       int loop_nest;
1252
1253       if (e)
1254         {
1255           /* See if the edge is already there and has the correct callee.  It
1256              might be so because of indirect inlining has already updated
1257              it.  We also might've cloned and redirected the edge.  */
1258           if (new_call && e->callee)
1259             {
1260               struct cgraph_node *callee = e->callee;
1261               while (callee)
1262                 {
1263                   if (callee->decl == new_call
1264                       || callee->former_clone_of == new_call)
1265                     return;
1266                   callee = callee->clone_of;
1267                 }
1268             }
1269
1270           /* Otherwise remove edge and create new one; we can't simply redirect
1271              since function has changed, so inline plan and other information
1272              attached to edge is invalid.  */
1273           count = e->count;
1274           frequency = e->frequency;
1275           loop_nest = e->loop_nest;
1276           cgraph_remove_edge (e);
1277         }
1278       else
1279         {
1280           /* We are seeing new direct call; compute profile info based on BB.  */
1281           basic_block bb = gimple_bb (new_stmt);
1282           count = bb->count;
1283           frequency = compute_call_stmt_bb_frequency (current_function_decl,
1284                                                       bb);
1285           loop_nest = bb->loop_depth;
1286         }
1287
1288       if (new_call)
1289         {
1290           ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1291                                    new_stmt, count, frequency,
1292                                    loop_nest);
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(%i)", cgraph_node_name (node), node->uid,
1831            node->pid);
1832   dump_addr (f, " @", (void *)node);
1833   if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
1834     fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
1835   if (node->global.inlined_to)
1836     fprintf (f, " (inline copy in %s/%i)",
1837              cgraph_node_name (node->global.inlined_to),
1838              node->global.inlined_to->uid);
1839   if (node->same_comdat_group)
1840     fprintf (f, " (same comdat group as %s/%i)",
1841              cgraph_node_name (node->same_comdat_group),
1842              node->same_comdat_group->uid);
1843   if (node->clone_of)
1844     fprintf (f, " (clone of %s/%i)",
1845              cgraph_node_name (node->clone_of),
1846              node->clone_of->uid);
1847   if (cgraph_function_flags_ready)
1848     fprintf (f, " availability:%s",
1849              cgraph_availability_names [cgraph_function_body_availability (node)]);
1850   if (node->analyzed)
1851     fprintf (f, " analyzed");
1852   if (node->in_other_partition)
1853     fprintf (f, " in_other_partition");
1854   if (node->count)
1855     fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1856              (HOST_WIDEST_INT)node->count);
1857   if (node->origin)
1858     fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
1859   if (node->needed)
1860     fprintf (f, " needed");
1861   if (node->address_taken)
1862     fprintf (f, " address_taken");
1863   else if (node->reachable)
1864     fprintf (f, " reachable");
1865   else if (node->reachable_from_other_partition)
1866     fprintf (f, " reachable_from_other_partition");
1867   if (gimple_has_body_p (node->decl))
1868     fprintf (f, " body");
1869   if (node->process)
1870     fprintf (f, " process");
1871   if (node->local.local)
1872     fprintf (f, " local");
1873   if (node->local.externally_visible)
1874     fprintf (f, " externally_visible");
1875   if (node->resolution != LDPR_UNKNOWN)
1876     fprintf (f, " %s",
1877              ld_plugin_symbol_resolution_names[(int)node->resolution]);
1878   if (node->local.finalized)
1879     fprintf (f, " finalized");
1880   if (node->local.redefined_extern_inline)
1881     fprintf (f, " redefined_extern_inline");
1882   if (TREE_ASM_WRITTEN (node->decl))
1883     fprintf (f, " asm_written");
1884   if (node->only_called_at_startup)
1885     fprintf (f, " only_called_at_startup");
1886   if (node->only_called_at_exit)
1887     fprintf (f, " only_called_at_exit");
1888
1889   fprintf (f, "\n  called by: ");
1890   for (edge = node->callers; edge; edge = edge->next_caller)
1891     {
1892       fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
1893                edge->caller->uid);
1894       if (edge->count)
1895         fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1896                  (HOST_WIDEST_INT)edge->count);
1897       if (edge->frequency)
1898         fprintf (f, "(%.2f per call) ",
1899                  edge->frequency / (double)CGRAPH_FREQ_BASE);
1900       if (!edge->inline_failed)
1901         fprintf(f, "(inlined) ");
1902       if (edge->indirect_inlining_edge)
1903         fprintf(f, "(indirect_inlining) ");
1904       if (edge->can_throw_external)
1905         fprintf(f, "(can throw external) ");
1906     }
1907
1908   fprintf (f, "\n  calls: ");
1909   for (edge = node->callees; edge; edge = edge->next_callee)
1910     {
1911       fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
1912                edge->callee->uid);
1913       if (!edge->inline_failed)
1914         fprintf(f, "(inlined) ");
1915       if (edge->indirect_inlining_edge)
1916         fprintf(f, "(indirect_inlining) ");
1917       if (edge->count)
1918         fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1919                  (HOST_WIDEST_INT)edge->count);
1920       if (edge->frequency)
1921         fprintf (f, "(%.2f per call) ",
1922                  edge->frequency / (double)CGRAPH_FREQ_BASE);
1923       if (edge->loop_nest)
1924         fprintf (f, "(nested in %i loops) ", edge->loop_nest);
1925       if (edge->can_throw_external)
1926         fprintf(f, "(can throw external) ");
1927     }
1928   fprintf (f, "\n");
1929   fprintf (f, "  References: ");
1930   ipa_dump_references (f, &node->ref_list);
1931   fprintf (f, "  Refering this function: ");
1932   ipa_dump_refering (f, &node->ref_list);
1933
1934   for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1935     indirect_calls_count++;
1936   if (indirect_calls_count)
1937     fprintf (f, "  has %i outgoing edges for indirect calls.\n",
1938              indirect_calls_count);
1939
1940   if (node->same_body)
1941     {
1942       struct cgraph_node *n;
1943       fprintf (f, "  aliases & thunks:");
1944       for (n = node->same_body; n; n = n->next)
1945         {
1946           fprintf (f, " %s/%i", cgraph_node_name (n), n->uid);
1947           if (n->thunk.thunk_p)
1948             {
1949               fprintf (f, " (thunk of %s fixed offset %i virtual value %i has "
1950                        "virtual offset %i",
1951                        lang_hooks.decl_printable_name (n->thunk.alias, 2),
1952                        (int)n->thunk.fixed_offset,
1953                        (int)n->thunk.virtual_value,
1954                        (int)n->thunk.virtual_offset_p);
1955               fprintf (f, ")");
1956             }
1957           if (DECL_ASSEMBLER_NAME_SET_P (n->decl))
1958             fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
1959         }
1960       fprintf (f, "\n");
1961     }
1962 }
1963
1964
1965 /* Dump call graph node NODE to stderr.  */
1966
1967 DEBUG_FUNCTION void
1968 debug_cgraph_node (struct cgraph_node *node)
1969 {
1970   dump_cgraph_node (stderr, node);
1971 }
1972
1973
1974 /* Dump the callgraph to file F.  */
1975
1976 void
1977 dump_cgraph (FILE *f)
1978 {
1979   struct cgraph_node *node;
1980
1981   fprintf (f, "callgraph:\n\n");
1982   for (node = cgraph_nodes; node; node = node->next)
1983     dump_cgraph_node (f, node);
1984 }
1985
1986
1987 /* Dump the call graph to stderr.  */
1988
1989 DEBUG_FUNCTION void
1990 debug_cgraph (void)
1991 {
1992   dump_cgraph (stderr);
1993 }
1994
1995
1996 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables.  */
1997
1998 void
1999 change_decl_assembler_name (tree decl, tree name)
2000 {
2001   struct cgraph_node *node;
2002   void **slot;
2003   if (!DECL_ASSEMBLER_NAME_SET_P (decl))
2004     SET_DECL_ASSEMBLER_NAME (decl, name);
2005   else
2006     {
2007       if (name == DECL_ASSEMBLER_NAME (decl))
2008         return;
2009
2010       if (assembler_name_hash
2011           && TREE_CODE (decl) == FUNCTION_DECL
2012           && (node = cgraph_get_node_or_alias (decl)) != NULL)
2013         {
2014           tree old_name = DECL_ASSEMBLER_NAME (decl);
2015           slot = htab_find_slot_with_hash (assembler_name_hash, old_name,
2016                                            decl_assembler_name_hash (old_name),
2017                                            NO_INSERT);
2018           /* Inline clones are not hashed.  */
2019           if (slot && *slot == node)
2020             htab_clear_slot (assembler_name_hash, slot);
2021         }
2022       if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
2023           && DECL_RTL_SET_P (decl))
2024         warning (0, "%D renamed after being referenced in assembly", decl);
2025
2026       SET_DECL_ASSEMBLER_NAME (decl, name);
2027     }
2028   if (assembler_name_hash
2029       && TREE_CODE (decl) == FUNCTION_DECL
2030       && (node = cgraph_get_node_or_alias (decl)) != NULL)
2031     {
2032       slot = htab_find_slot_with_hash (assembler_name_hash, name,
2033                                        decl_assembler_name_hash (name),
2034                                        INSERT);
2035       gcc_assert (!*slot);
2036       *slot = node;
2037     }
2038 }
2039
2040 /* Add a top-level asm statement to the list.  */
2041
2042 struct cgraph_asm_node *
2043 cgraph_add_asm_node (tree asm_str)
2044 {
2045   struct cgraph_asm_node *node;
2046
2047   node = ggc_alloc_cleared_cgraph_asm_node ();
2048   node->asm_str = asm_str;
2049   node->order = cgraph_order++;
2050   node->next = NULL;
2051   if (cgraph_asm_nodes == NULL)
2052     cgraph_asm_nodes = node;
2053   else
2054     cgraph_asm_last_node->next = node;
2055   cgraph_asm_last_node = node;
2056   return node;
2057 }
2058
2059 /* Return true when the DECL can possibly be inlined.  */
2060 bool
2061 cgraph_function_possibly_inlined_p (tree decl)
2062 {
2063   if (!cgraph_global_info_ready)
2064     return !DECL_UNINLINABLE (decl);
2065   return DECL_POSSIBLY_INLINED (decl);
2066 }
2067
2068 /* Create clone of E in the node N represented by CALL_EXPR the callgraph.  */
2069 struct cgraph_edge *
2070 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
2071                    gimple call_stmt, unsigned stmt_uid, gcov_type count_scale,
2072                    int freq_scale, int loop_nest, bool update_original)
2073 {
2074   struct cgraph_edge *new_edge;
2075   gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
2076   gcov_type freq;
2077
2078   /* We do not want to ignore loop nest after frequency drops to 0.  */
2079   if (!freq_scale)
2080     freq_scale = 1;
2081   freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
2082   if (freq > CGRAPH_FREQ_MAX)
2083     freq = CGRAPH_FREQ_MAX;
2084
2085   if (e->indirect_unknown_callee)
2086     {
2087       tree decl;
2088
2089       if (call_stmt && (decl = gimple_call_fndecl (call_stmt)))
2090         {
2091           struct cgraph_node *callee = cgraph_get_node (decl);
2092           gcc_checking_assert (callee);
2093           new_edge = cgraph_create_edge (n, callee, call_stmt, count, freq,
2094                                          e->loop_nest + loop_nest);
2095         }
2096       else
2097         {
2098           new_edge = cgraph_create_indirect_edge (n, call_stmt,
2099                                                   e->indirect_info->ecf_flags,
2100                                                   count, freq,
2101                                                   e->loop_nest + loop_nest);
2102           *new_edge->indirect_info = *e->indirect_info;
2103         }
2104     }
2105   else
2106     {
2107       new_edge = cgraph_create_edge (n, e->callee, call_stmt, count, freq,
2108                                      e->loop_nest + loop_nest);
2109       if (e->indirect_info)
2110         {
2111           new_edge->indirect_info
2112             = ggc_alloc_cleared_cgraph_indirect_call_info ();
2113           *new_edge->indirect_info = *e->indirect_info;
2114         }
2115     }
2116
2117   new_edge->call_stmt_size = e->call_stmt_size;
2118   new_edge->call_stmt_time = e->call_stmt_time;
2119   new_edge->inline_failed = e->inline_failed;
2120   new_edge->indirect_inlining_edge = e->indirect_inlining_edge;
2121   new_edge->lto_stmt_uid = stmt_uid;
2122   /* Clone flags that depend on call_stmt availability manually.  */
2123   new_edge->can_throw_external = e->can_throw_external;
2124   new_edge->call_stmt_cannot_inline_p = e->call_stmt_cannot_inline_p;
2125   if (update_original)
2126     {
2127       e->count -= new_edge->count;
2128       if (e->count < 0)
2129         e->count = 0;
2130     }
2131   cgraph_call_edge_duplication_hooks (e, new_edge);
2132   return new_edge;
2133 }
2134
2135 /* Create node representing clone of N executed COUNT times.  Decrease
2136    the execution counts from original node too.
2137    The new clone will have decl set to DECL that may or may not be the same
2138    as decl of N.
2139
2140    When UPDATE_ORIGINAL is true, the counts are subtracted from the original
2141    function's profile to reflect the fact that part of execution is handled
2142    by node.  */
2143 struct cgraph_node *
2144 cgraph_clone_node (struct cgraph_node *n, tree decl, gcov_type count, int freq,
2145                    int loop_nest, bool update_original,
2146                    VEC(cgraph_edge_p,heap) *redirect_callers)
2147 {
2148   struct cgraph_node *new_node = cgraph_create_node_1 ();
2149   struct cgraph_edge *e;
2150   gcov_type count_scale;
2151   unsigned i;
2152
2153   new_node->decl = decl;
2154   new_node->origin = n->origin;
2155   if (new_node->origin)
2156     {
2157       new_node->next_nested = new_node->origin->nested;
2158       new_node->origin->nested = new_node;
2159     }
2160   new_node->analyzed = n->analyzed;
2161   new_node->local = n->local;
2162   new_node->local.externally_visible = false;
2163   new_node->local.local = true;
2164   new_node->local.vtable_method = false;
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, loop_nest, 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, loop_nest, 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   cgraph_call_node_duplication_hooks (n, new_node);
2211   if (n->decl != decl)
2212     {
2213       struct cgraph_node **slot;
2214       slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, new_node, INSERT);
2215       gcc_assert (!*slot);
2216       *slot = new_node;
2217       if (assembler_name_hash)
2218         {
2219           void **aslot;
2220           tree name = DECL_ASSEMBLER_NAME (decl);
2221
2222           aslot = htab_find_slot_with_hash (assembler_name_hash, name,
2223                                             decl_assembler_name_hash (name),
2224                                             INSERT);
2225           gcc_assert (!*aslot);
2226           *aslot = new_node;
2227         }
2228     }
2229   return new_node;
2230 }
2231
2232 /* Create a new name for clone of DECL, add SUFFIX.  Returns an identifier.  */
2233
2234 static GTY(()) unsigned int clone_fn_id_num;
2235
2236 tree
2237 clone_function_name (tree decl, const char *suffix)
2238 {
2239   tree name = DECL_ASSEMBLER_NAME (decl);
2240   size_t len = IDENTIFIER_LENGTH (name);
2241   char *tmp_name, *prefix;
2242
2243   prefix = XALLOCAVEC (char, len + strlen (suffix) + 2);
2244   memcpy (prefix, IDENTIFIER_POINTER (name), len);
2245   strcpy (prefix + len + 1, suffix);
2246 #ifndef NO_DOT_IN_LABEL
2247   prefix[len] = '.';
2248 #elif !defined NO_DOLLAR_IN_LABEL
2249   prefix[len] = '$';
2250 #else
2251   prefix[len] = '_';
2252 #endif
2253   ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++);
2254   return get_identifier (tmp_name);
2255 }
2256
2257 /* Create callgraph node clone with new declaration.  The actual body will
2258    be copied later at compilation stage.
2259
2260    TODO: after merging in ipa-sra use function call notes instead of args_to_skip
2261    bitmap interface.
2262    */
2263 struct cgraph_node *
2264 cgraph_create_virtual_clone (struct cgraph_node *old_node,
2265                              VEC(cgraph_edge_p,heap) *redirect_callers,
2266                              VEC(ipa_replace_map_p,gc) *tree_map,
2267                              bitmap args_to_skip,
2268                              const char * suffix)
2269 {
2270   tree old_decl = old_node->decl;
2271   struct cgraph_node *new_node = NULL;
2272   tree new_decl;
2273   size_t i;
2274   struct ipa_replace_map *map;
2275
2276   if (!flag_wpa)
2277     gcc_checking_assert  (tree_versionable_function_p (old_decl));
2278
2279   gcc_assert (old_node->local.can_change_signature || !args_to_skip);
2280
2281   /* Make a new FUNCTION_DECL tree node */
2282   if (!args_to_skip)
2283     new_decl = copy_node (old_decl);
2284   else
2285     new_decl = build_function_decl_skip_args (old_decl, args_to_skip);
2286   DECL_STRUCT_FUNCTION (new_decl) = NULL;
2287
2288   /* Generate a new name for the new version. */
2289   DECL_NAME (new_decl) = clone_function_name (old_decl, suffix);
2290   SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
2291   SET_DECL_RTL (new_decl, NULL);
2292
2293   new_node = cgraph_clone_node (old_node, new_decl, old_node->count,
2294                                 CGRAPH_FREQ_BASE, 0, false,
2295                                 redirect_callers);
2296   /* Update the properties.
2297      Make clone visible only within this translation unit.  Make sure
2298      that is not weak also.
2299      ??? We cannot use COMDAT linkage because there is no
2300      ABI support for this.  */
2301   DECL_EXTERNAL (new_node->decl) = 0;
2302   if (DECL_ONE_ONLY (old_decl))
2303     DECL_SECTION_NAME (new_node->decl) = NULL;
2304   DECL_COMDAT_GROUP (new_node->decl) = 0;
2305   TREE_PUBLIC (new_node->decl) = 0;
2306   DECL_COMDAT (new_node->decl) = 0;
2307   DECL_WEAK (new_node->decl) = 0;
2308   new_node->clone.tree_map = tree_map;
2309   new_node->clone.args_to_skip = args_to_skip;
2310   FOR_EACH_VEC_ELT (ipa_replace_map_p, tree_map, i, map)
2311     {
2312       tree var = map->new_tree;
2313
2314       STRIP_NOPS (var);
2315       if (TREE_CODE (var) != ADDR_EXPR)
2316         continue;
2317       var = get_base_var (var);
2318       if (!var)
2319         continue;
2320
2321       /* Record references of the future statement initializing the constant
2322          argument.  */
2323       if (TREE_CODE (var) == FUNCTION_DECL)
2324         {
2325           struct cgraph_node *ref_node = cgraph_get_node (var);
2326           gcc_checking_assert (ref_node);
2327           ipa_record_reference (new_node, NULL, ref_node, NULL, IPA_REF_ADDR,
2328                                 NULL);
2329         }
2330       else if (TREE_CODE (var) == VAR_DECL)
2331         ipa_record_reference (new_node, NULL, NULL, varpool_node (var),
2332                               IPA_REF_ADDR, NULL);
2333     }
2334   if (!args_to_skip)
2335     new_node->clone.combined_args_to_skip = old_node->clone.combined_args_to_skip;
2336   else if (old_node->clone.combined_args_to_skip)
2337     {
2338       int newi = 0, oldi = 0;
2339       tree arg;
2340       bitmap new_args_to_skip = BITMAP_GGC_ALLOC ();
2341       struct cgraph_node *orig_node;
2342       for (orig_node = old_node; orig_node->clone_of; orig_node = orig_node->clone_of)
2343         ;
2344       for (arg = DECL_ARGUMENTS (orig_node->decl); arg; arg = DECL_CHAIN (arg), oldi++)
2345         {
2346           if (bitmap_bit_p (old_node->clone.combined_args_to_skip, oldi))
2347             {
2348               bitmap_set_bit (new_args_to_skip, oldi);
2349               continue;
2350             }
2351           if (bitmap_bit_p (args_to_skip, newi))
2352             bitmap_set_bit (new_args_to_skip, oldi);
2353           newi++;
2354         }
2355       new_node->clone.combined_args_to_skip = new_args_to_skip;
2356     }
2357   else
2358     new_node->clone.combined_args_to_skip = args_to_skip;
2359   new_node->local.externally_visible = 0;
2360   new_node->local.local = 1;
2361   new_node->lowered = true;
2362   new_node->reachable = true;
2363
2364
2365   return new_node;
2366 }
2367
2368 /* NODE is no longer nested function; update cgraph accordingly.  */
2369 void
2370 cgraph_unnest_node (struct cgraph_node *node)
2371 {
2372   struct cgraph_node **node2 = &node->origin->nested;
2373   gcc_assert (node->origin);
2374
2375   while (*node2 != node)
2376     node2 = &(*node2)->next_nested;
2377   *node2 = node->next_nested;
2378   node->origin = NULL;
2379 }
2380
2381 /* Return function availability.  See cgraph.h for description of individual
2382    return values.  */
2383 enum availability
2384 cgraph_function_body_availability (struct cgraph_node *node)
2385 {
2386   enum availability avail;
2387   gcc_assert (cgraph_function_flags_ready);
2388   if (!node->analyzed)
2389     avail = AVAIL_NOT_AVAILABLE;
2390   else if (node->local.local)
2391     avail = AVAIL_LOCAL;
2392   else if (!node->local.externally_visible)
2393     avail = AVAIL_AVAILABLE;
2394   /* Inline functions are safe to be analyzed even if their symbol can
2395      be overwritten at runtime.  It is not meaningful to enforce any sane
2396      behaviour on replacing inline function by different body.  */
2397   else if (DECL_DECLARED_INLINE_P (node->decl))
2398     avail = AVAIL_AVAILABLE;
2399
2400   /* If the function can be overwritten, return OVERWRITABLE.  Take
2401      care at least of two notable extensions - the COMDAT functions
2402      used to share template instantiations in C++ (this is symmetric
2403      to code cp_cannot_inline_tree_fn and probably shall be shared and
2404      the inlinability hooks completely eliminated).
2405
2406      ??? Does the C++ one definition rule allow us to always return
2407      AVAIL_AVAILABLE here?  That would be good reason to preserve this
2408      bit.  */
2409
2410   else if (decl_replaceable_p (node->decl) && !DECL_EXTERNAL (node->decl))
2411     avail = AVAIL_OVERWRITABLE;
2412   else avail = AVAIL_AVAILABLE;
2413
2414   return avail;
2415 }
2416
2417 /* Add the function FNDECL to the call graph.
2418    Unlike cgraph_finalize_function, this function is intended to be used
2419    by middle end and allows insertion of new function at arbitrary point
2420    of compilation.  The function can be either in high, low or SSA form
2421    GIMPLE.
2422
2423    The function is assumed to be reachable and have address taken (so no
2424    API breaking optimizations are performed on it).
2425
2426    Main work done by this function is to enqueue the function for later
2427    processing to avoid need the passes to be re-entrant.  */
2428
2429 void
2430 cgraph_add_new_function (tree fndecl, bool lowered)
2431 {
2432   struct cgraph_node *node;
2433   switch (cgraph_state)
2434     {
2435       case CGRAPH_STATE_CONSTRUCTION:
2436         /* Just enqueue function to be processed at nearest occurrence.  */
2437         node = cgraph_create_node (fndecl);
2438         node->next_needed = cgraph_new_nodes;
2439         if (lowered)
2440           node->lowered = true;
2441         cgraph_new_nodes = node;
2442         break;
2443
2444       case CGRAPH_STATE_IPA:
2445       case CGRAPH_STATE_IPA_SSA:
2446       case CGRAPH_STATE_EXPANSION:
2447         /* Bring the function into finalized state and enqueue for later
2448            analyzing and compilation.  */
2449         node = cgraph_get_create_node (fndecl);
2450         node->local.local = false;
2451         node->local.finalized = true;
2452         node->reachable = node->needed = true;
2453         if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
2454           {
2455             push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2456             current_function_decl = fndecl;
2457             gimple_register_cfg_hooks ();
2458             tree_lowering_passes (fndecl);
2459             bitmap_obstack_initialize (NULL);
2460             if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2461               execute_pass_list (pass_early_local_passes.pass.sub);
2462             bitmap_obstack_release (NULL);
2463             pop_cfun ();
2464             current_function_decl = NULL;
2465
2466             lowered = true;
2467           }
2468         if (lowered)
2469           node->lowered = true;
2470         node->next_needed = cgraph_new_nodes;
2471         cgraph_new_nodes = node;
2472         break;
2473
2474       case CGRAPH_STATE_FINISHED:
2475         /* At the very end of compilation we have to do all the work up
2476            to expansion.  */
2477         node = cgraph_create_node (fndecl);
2478         if (lowered)
2479           node->lowered = true;
2480         cgraph_analyze_function (node);
2481         push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2482         current_function_decl = fndecl;
2483         gimple_register_cfg_hooks ();
2484         bitmap_obstack_initialize (NULL);
2485         if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2486           execute_pass_list (pass_early_local_passes.pass.sub);
2487         bitmap_obstack_release (NULL);
2488         tree_rest_of_compilation (fndecl);
2489         pop_cfun ();
2490         current_function_decl = NULL;
2491         break;
2492     }
2493
2494   /* Set a personality if required and we already passed EH lowering.  */
2495   if (lowered
2496       && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
2497           == eh_personality_lang))
2498     DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
2499 }
2500
2501 /* Return true if NODE can be made local for API change.
2502    Extern inline functions and C++ COMDAT functions can be made local
2503    at the expense of possible code size growth if function is used in multiple
2504    compilation units.  */
2505 bool
2506 cgraph_node_can_be_local_p (struct cgraph_node *node)
2507 {
2508   return (!node->needed && !node->address_taken
2509           && ((DECL_COMDAT (node->decl) && !node->same_comdat_group)
2510               || !node->local.externally_visible));
2511 }
2512
2513 /* Make DECL local.  FIXME: We shouldn't need to mess with rtl this early,
2514    but other code such as notice_global_symbol generates rtl.  */
2515 void
2516 cgraph_make_decl_local (tree decl)
2517 {
2518   rtx rtl, symbol;
2519
2520   if (TREE_CODE (decl) == VAR_DECL)
2521     DECL_COMMON (decl) = 0;
2522   else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
2523
2524   if (DECL_COMDAT (decl))
2525     {
2526       /* It is possible that we are linking against library defining same COMDAT
2527          function.  To avoid conflict we need to rename our local name of the
2528          function just in the case WHOPR partitioning decide to make it hidden
2529          to avoid cross partition references.  */
2530       if (flag_wpa)
2531         {
2532           const char *old_name;
2533
2534           old_name  = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2535           if (TREE_CODE (decl) == FUNCTION_DECL)
2536             {
2537               struct cgraph_node *node = cgraph_get_node_or_alias (decl);
2538               change_decl_assembler_name (decl,
2539                                           clone_function_name (decl, "local"));
2540               if (node->local.lto_file_data)
2541                 lto_record_renamed_decl (node->local.lto_file_data,
2542                                          old_name,
2543                                          IDENTIFIER_POINTER
2544                                            (DECL_ASSEMBLER_NAME (decl)));
2545             }
2546           else if (TREE_CODE (decl) == VAR_DECL)
2547             {
2548               struct varpool_node *vnode = varpool_get_node (decl);
2549               /* change_decl_assembler_name will warn here on vtables because
2550                  C++ frontend still sets TREE_SYMBOL_REFERENCED on them.  */
2551               SET_DECL_ASSEMBLER_NAME (decl,
2552                                        clone_function_name (decl, "local"));
2553               if (vnode->lto_file_data)
2554                 lto_record_renamed_decl (vnode->lto_file_data,
2555                                          old_name,
2556                                          IDENTIFIER_POINTER
2557                                            (DECL_ASSEMBLER_NAME (decl)));
2558             }
2559         }
2560       DECL_SECTION_NAME (decl) = 0;
2561       DECL_COMDAT (decl) = 0;
2562     }
2563   DECL_COMDAT_GROUP (decl) = 0;
2564   DECL_WEAK (decl) = 0;
2565   DECL_EXTERNAL (decl) = 0;
2566   TREE_PUBLIC (decl) = 0;
2567   if (!DECL_RTL_SET_P (decl))
2568     return;
2569
2570   /* Update rtl flags.  */
2571   make_decl_rtl (decl);
2572
2573   rtl = DECL_RTL (decl);
2574   if (!MEM_P (rtl))
2575     return;
2576
2577   symbol = XEXP (rtl, 0);
2578   if (GET_CODE (symbol) != SYMBOL_REF)
2579     return;
2580
2581   SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
2582 }
2583
2584 /* Bring NODE local.  */
2585 void
2586 cgraph_make_node_local (struct cgraph_node *node)
2587 {
2588   gcc_assert (cgraph_node_can_be_local_p (node));
2589   if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2590     {
2591       struct cgraph_node *alias;
2592       cgraph_make_decl_local (node->decl);
2593
2594       for (alias = node->same_body; alias; alias = alias->next)
2595         cgraph_make_decl_local (alias->decl);
2596
2597       node->local.externally_visible = false;
2598       node->local.local = true;
2599       node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2600       gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2601     }
2602 }
2603
2604 /* Set TREE_NOTHROW on NODE's decl and on same_body aliases of NODE
2605    if any to NOTHROW.  */
2606
2607 void
2608 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
2609 {
2610   struct cgraph_node *alias;
2611   TREE_NOTHROW (node->decl) = nothrow;
2612   for (alias = node->same_body; alias; alias = alias->next)
2613     TREE_NOTHROW (alias->decl) = nothrow;
2614 }
2615
2616 /* Set TREE_READONLY on NODE's decl and on same_body aliases of NODE
2617    if any to READONLY.  */
2618
2619 void
2620 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
2621 {
2622   struct cgraph_node *alias;
2623   /* Static constructors and destructors without a side effect can be
2624      optimized out.  */
2625   if (!looping && readonly)
2626     {
2627       if (DECL_STATIC_CONSTRUCTOR (node->decl))
2628         DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2629       if (DECL_STATIC_DESTRUCTOR (node->decl))
2630         DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2631     }
2632   TREE_READONLY (node->decl) = readonly;
2633   DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2634   for (alias = node->same_body; alias; alias = alias->next)
2635     {
2636       TREE_READONLY (alias->decl) = readonly;
2637       DECL_LOOPING_CONST_OR_PURE_P (alias->decl) = looping;
2638     }
2639 }
2640
2641 /* Set DECL_PURE_P on NODE's decl and on same_body aliases of NODE
2642    if any to PURE.  */
2643
2644 void
2645 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
2646 {
2647   struct cgraph_node *alias;
2648   /* Static constructors and destructors without a side effect can be
2649      optimized out.  */
2650   if (!looping && pure)
2651     {
2652       if (DECL_STATIC_CONSTRUCTOR (node->decl))
2653         DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2654       if (DECL_STATIC_DESTRUCTOR (node->decl))
2655         DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2656     }
2657   DECL_PURE_P (node->decl) = pure;
2658   DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2659   for (alias = node->same_body; alias; alias = alias->next)
2660     {
2661       DECL_PURE_P (alias->decl) = pure;
2662       DECL_LOOPING_CONST_OR_PURE_P (alias->decl) = looping;
2663     }
2664 }
2665
2666 /* See if the frequency of NODE can be updated based on frequencies of its
2667    callers.  */
2668 bool
2669 cgraph_propagate_frequency (struct cgraph_node *node)
2670 {
2671   bool maybe_unlikely_executed = true, maybe_executed_once = true;
2672   bool only_called_at_startup = true;
2673   bool only_called_at_exit = true;
2674   bool changed = false;
2675   struct cgraph_edge *edge;
2676
2677   if (!node->local.local)
2678     return false;
2679   gcc_assert (node->analyzed);
2680   if (dump_file && (dump_flags & TDF_DETAILS))
2681     fprintf (dump_file, "Processing frequency %s\n", cgraph_node_name (node));
2682
2683   for (edge = node->callers;
2684        edge && (maybe_unlikely_executed || maybe_executed_once
2685                 || only_called_at_startup || only_called_at_exit);
2686        edge = edge->next_caller)
2687     {
2688       if (edge->caller != node)
2689         {
2690           only_called_at_startup &= edge->caller->only_called_at_startup;
2691           /* It makes sense to put main() together with the static constructors.
2692              It will be executed for sure, but rest of functions called from
2693              main are definitely not at startup only.  */
2694           if (MAIN_NAME_P (DECL_NAME (edge->caller->decl)))
2695             only_called_at_startup = 0;
2696           only_called_at_exit &= edge->caller->only_called_at_exit;
2697         }
2698       if (!edge->frequency)
2699         continue;
2700       switch (edge->caller->frequency)
2701         {
2702         case NODE_FREQUENCY_UNLIKELY_EXECUTED:
2703           break;
2704         case NODE_FREQUENCY_EXECUTED_ONCE:
2705           if (dump_file && (dump_flags & TDF_DETAILS))
2706             fprintf (dump_file, "  Called by %s that is executed once\n",
2707                      cgraph_node_name (edge->caller));
2708           maybe_unlikely_executed = false;
2709           if (edge->loop_nest)
2710             {
2711               maybe_executed_once = false;
2712               if (dump_file && (dump_flags & TDF_DETAILS))
2713                 fprintf (dump_file, "  Called in loop\n");
2714             }
2715           break;
2716         case NODE_FREQUENCY_HOT:
2717         case NODE_FREQUENCY_NORMAL:
2718           if (dump_file && (dump_flags & TDF_DETAILS))
2719             fprintf (dump_file, "  Called by %s that is normal or hot\n",
2720                      cgraph_node_name (edge->caller));
2721           maybe_unlikely_executed = false;
2722           maybe_executed_once = false;
2723           break;
2724         }
2725     }
2726   if ((only_called_at_startup && !only_called_at_exit)
2727       && !node->only_called_at_startup)
2728     {
2729        node->only_called_at_startup = true;
2730        if (dump_file)
2731          fprintf (dump_file, "Node %s promoted to only called at startup.\n",
2732                   cgraph_node_name (node));
2733        changed = true;
2734     }
2735   if ((only_called_at_exit && !only_called_at_startup)
2736       && !node->only_called_at_exit)
2737     {
2738        node->only_called_at_exit = true;
2739        if (dump_file)
2740          fprintf (dump_file, "Node %s promoted to only called at exit.\n",
2741                   cgraph_node_name (node));
2742        changed = true;
2743     }
2744   /* These come either from profile or user hints; never update them.  */
2745   if (node->frequency == NODE_FREQUENCY_HOT
2746       || node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2747     return changed;
2748   if (maybe_unlikely_executed)
2749     {
2750       node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
2751       if (dump_file)
2752         fprintf (dump_file, "Node %s promoted to unlikely executed.\n",
2753                  cgraph_node_name (node));
2754       changed = true;
2755     }
2756   else if (maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE)
2757     {
2758       node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
2759       if (dump_file)
2760         fprintf (dump_file, "Node %s promoted to executed once.\n",
2761                  cgraph_node_name (node));
2762       changed = true;
2763     }
2764   return changed;
2765 }
2766
2767 /* Return true when NODE can not return or throw and thus
2768    it is safe to ignore its side effects for IPA analysis.  */
2769
2770 bool
2771 cgraph_node_cannot_return (struct cgraph_node *node)
2772 {
2773   int flags = flags_from_decl_or_type (node->decl);
2774   if (!flag_exceptions)
2775     return (flags & ECF_NORETURN) != 0;
2776   else
2777     return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2778              == (ECF_NORETURN | ECF_NOTHROW));
2779 }
2780
2781 /* Return true when call of E can not lead to return from caller
2782    and thus it is safe to ignore its side effects for IPA analysis
2783    when computing side effects of the caller.
2784    FIXME: We could actually mark all edges that have no reaching
2785    patch to EXIT_BLOCK_PTR or throw to get better results.  */
2786 bool
2787 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2788 {
2789   if (cgraph_node_cannot_return (e->caller))
2790     return true;
2791   if (e->indirect_unknown_callee)
2792     {
2793       int flags = e->indirect_info->ecf_flags;
2794       if (!flag_exceptions)
2795         return (flags & ECF_NORETURN) != 0;
2796       else
2797         return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2798                  == (ECF_NORETURN | ECF_NOTHROW));
2799     }
2800   else
2801     return cgraph_node_cannot_return (e->callee);
2802 }
2803
2804 /* Return true when function NODE can be removed from callgraph
2805    if all direct calls are eliminated.  */
2806
2807 bool
2808 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2809 {
2810   gcc_assert (!node->global.inlined_to);
2811   /* Extern inlines can always go, we will use the external definition.  */
2812   if (DECL_EXTERNAL (node->decl))
2813     return true;
2814   /* When function is needed, we can not remove it.  */
2815   if (node->needed || node->reachable_from_other_partition)
2816     return false;
2817   if (DECL_STATIC_CONSTRUCTOR (node->decl)
2818       || DECL_STATIC_DESTRUCTOR (node->decl))
2819     return false;
2820   /* Only COMDAT functions can be removed if externally visible.  */
2821   if (node->local.externally_visible
2822       && (!DECL_COMDAT (node->decl)
2823           || cgraph_used_from_object_file_p (node)))
2824     return false;
2825   return true;
2826 }
2827
2828 /* Return true when function NODE can be expected to be removed
2829    from program when direct calls in this compilation unit are removed.
2830
2831    As a special case COMDAT functions are
2832    cgraph_can_remove_if_no_direct_calls_p while the are not
2833    cgraph_only_called_directly_p (it is possible they are called from other
2834    unit)
2835
2836    This function behaves as cgraph_only_called_directly_p because eliminating
2837    all uses of COMDAT function does not make it necessarily disappear from
2838    the program unless we are compiling whole program or we do LTO.  In this
2839    case we know we win since dynamic linking will not really discard the
2840    linkonce section.  */
2841
2842 bool
2843 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2844 {
2845   gcc_assert (!node->global.inlined_to);
2846   if (cgraph_used_from_object_file_p (node))
2847     return false;
2848   if (!in_lto_p && !flag_whole_program)
2849     return cgraph_only_called_directly_p (node);
2850   else
2851     {
2852        if (DECL_EXTERNAL (node->decl))
2853          return true;
2854       return cgraph_can_remove_if_no_direct_calls_p (node);
2855     }
2856 }
2857
2858 /* Return true when RESOLUTION indicate that linker will use
2859    the symbol from non-LTO object files.  */
2860
2861 bool
2862 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
2863 {
2864   return (resolution == LDPR_PREVAILING_DEF
2865           || resolution == LDPR_PREEMPTED_REG
2866           || resolution == LDPR_RESOLVED_EXEC
2867           || resolution == LDPR_RESOLVED_DYN);
2868 }
2869
2870 /* Return true when NODE is known to be used from other (non-LTO) object file.
2871    Known only when doing LTO via linker plugin.  */
2872
2873 bool
2874 cgraph_used_from_object_file_p (struct cgraph_node *node)
2875 {
2876   struct cgraph_node *alias;
2877
2878   gcc_assert (!node->global.inlined_to);
2879   if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
2880     return false;
2881   if (resolution_used_from_other_file_p (node->resolution))
2882     return true;
2883   for (alias = node->same_body; alias; alias = alias->next)
2884     if (TREE_PUBLIC (alias->decl)
2885         && resolution_used_from_other_file_p (alias->resolution))
2886       return true;
2887   return false;
2888 }
2889
2890 #include "gt-cgraph.h"