OSDN Git Service

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