OSDN Git Service

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