OSDN Git Service

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