OSDN Git Service

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