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