OSDN Git Service

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