OSDN Git Service

Backport from mainline
[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    If NEW_STMT is NULL, the call has been dropped without any
1246    replacement.  */
1247
1248 static void
1249 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1250                                         gimple old_stmt, tree old_call,
1251                                         gimple new_stmt)
1252 {
1253   tree new_call = (new_stmt && is_gimple_call (new_stmt))
1254                   ? gimple_call_fndecl (new_stmt) : 0;
1255
1256   /* We are seeing indirect calls, then there is nothing to update.  */
1257   if (!new_call && !old_call)
1258     return;
1259   /* See if we turned indirect call into direct call or folded call to one builtin
1260      into different builtin.  */
1261   if (old_call != new_call)
1262     {
1263       struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1264       struct cgraph_edge *ne = NULL;
1265       gcov_type count;
1266       int frequency;
1267       int loop_nest;
1268
1269       if (e)
1270         {
1271           /* See if the edge is already there and has the correct callee.  It
1272              might be so because of indirect inlining has already updated
1273              it.  We also might've cloned and redirected the edge.  */
1274           if (new_call && e->callee)
1275             {
1276               struct cgraph_node *callee = e->callee;
1277               while (callee)
1278                 {
1279                   if (callee->decl == new_call
1280                       || callee->former_clone_of == new_call)
1281                     return;
1282                   callee = callee->clone_of;
1283                 }
1284             }
1285
1286           /* Otherwise remove edge and create new one; we can't simply redirect
1287              since function has changed, so inline plan and other information
1288              attached to edge is invalid.  */
1289           count = e->count;
1290           frequency = e->frequency;
1291           loop_nest = e->loop_nest;
1292           cgraph_remove_edge (e);
1293         }
1294       else if (new_call)
1295         {
1296           /* We are seeing new direct call; compute profile info based on BB.  */
1297           basic_block bb = gimple_bb (new_stmt);
1298           count = bb->count;
1299           frequency = compute_call_stmt_bb_frequency (current_function_decl,
1300                                                       bb);
1301           loop_nest = bb->loop_depth;
1302         }
1303
1304       if (new_call)
1305         {
1306           ne = cgraph_create_edge (node, cgraph_node (new_call),
1307                                    new_stmt, count, frequency,
1308                                    loop_nest);
1309           gcc_assert (ne->inline_failed);
1310         }
1311     }
1312   /* We only updated the call stmt; update pointer in cgraph edge..  */
1313   else if (old_stmt != new_stmt)
1314     cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1315 }
1316
1317 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1318    OLD_STMT changed into NEW_STMT.  OLD_DECL is gimple_call_fndecl
1319    of OLD_STMT before it was updated (updating can happen inplace).  */
1320
1321 void
1322 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1323 {
1324   struct cgraph_node *orig = cgraph_node (cfun->decl);
1325   struct cgraph_node *node;
1326
1327   cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1328   if (orig->clones)
1329     for (node = orig->clones; node != orig;)
1330       {
1331         cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1332         if (node->clones)
1333           node = node->clones;
1334         else if (node->next_sibling_clone)
1335           node = node->next_sibling_clone;
1336         else
1337           {
1338             while (node != orig && !node->next_sibling_clone)
1339               node = node->clone_of;
1340             if (node != orig)
1341               node = node->next_sibling_clone;
1342           }
1343       }
1344 }
1345
1346
1347 /* Remove all callees from the node.  */
1348
1349 void
1350 cgraph_node_remove_callees (struct cgraph_node *node)
1351 {
1352   struct cgraph_edge *e, *f;
1353
1354   /* It is sufficient to remove the edges from the lists of callers of
1355      the callees.  The callee list of the node can be zapped with one
1356      assignment.  */
1357   for (e = node->callees; e; e = f)
1358     {
1359       f = e->next_callee;
1360       cgraph_call_edge_removal_hooks (e);
1361       if (!e->indirect_unknown_callee)
1362         cgraph_edge_remove_callee (e);
1363       cgraph_free_edge (e);
1364     }
1365   for (e = node->indirect_calls; e; e = f)
1366     {
1367       f = e->next_callee;
1368       cgraph_call_edge_removal_hooks (e);
1369       if (!e->indirect_unknown_callee)
1370         cgraph_edge_remove_callee (e);
1371       cgraph_free_edge (e);
1372     }
1373   node->indirect_calls = NULL;
1374   node->callees = NULL;
1375   if (node->call_site_hash)
1376     {
1377       htab_delete (node->call_site_hash);
1378       node->call_site_hash = NULL;
1379     }
1380 }
1381
1382 /* Remove all callers from the node.  */
1383
1384 static void
1385 cgraph_node_remove_callers (struct cgraph_node *node)
1386 {
1387   struct cgraph_edge *e, *f;
1388
1389   /* It is sufficient to remove the edges from the lists of callees of
1390      the callers.  The caller list of the node can be zapped with one
1391      assignment.  */
1392   for (e = node->callers; e; e = f)
1393     {
1394       f = e->next_caller;
1395       cgraph_call_edge_removal_hooks (e);
1396       cgraph_edge_remove_caller (e);
1397       cgraph_free_edge (e);
1398     }
1399   node->callers = NULL;
1400 }
1401
1402 /* Release memory used to represent body of function NODE.  */
1403
1404 void
1405 cgraph_release_function_body (struct cgraph_node *node)
1406 {
1407   if (DECL_STRUCT_FUNCTION (node->decl))
1408     {
1409       tree old_decl = current_function_decl;
1410       push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1411       if (cfun->gimple_df)
1412         {
1413           current_function_decl = node->decl;
1414           delete_tree_ssa ();
1415           delete_tree_cfg_annotations ();
1416           cfun->eh = NULL;
1417           current_function_decl = old_decl;
1418         }
1419       if (cfun->cfg)
1420         {
1421           gcc_assert (dom_computed[0] == DOM_NONE);
1422           gcc_assert (dom_computed[1] == DOM_NONE);
1423           clear_edges ();
1424         }
1425       if (cfun->value_histograms)
1426         free_histograms ();
1427       gcc_assert (!current_loops);
1428       pop_cfun();
1429       gimple_set_body (node->decl, NULL);
1430       VEC_free (ipa_opt_pass, heap,
1431                 node->ipa_transforms_to_apply);
1432       /* Struct function hangs a lot of data that would leak if we didn't
1433          removed all pointers to it.   */
1434       ggc_free (DECL_STRUCT_FUNCTION (node->decl));
1435       DECL_STRUCT_FUNCTION (node->decl) = NULL;
1436     }
1437   DECL_SAVED_TREE (node->decl) = NULL;
1438   /* If the node is abstract and needed, then do not clear DECL_INITIAL
1439      of its associated function function declaration because it's
1440      needed to emit debug info later.  */
1441   if (!node->abstract_and_needed)
1442     DECL_INITIAL (node->decl) = error_mark_node;
1443 }
1444
1445 /* Remove same body alias node.  */
1446
1447 void
1448 cgraph_remove_same_body_alias (struct cgraph_node *node)
1449 {
1450   void **slot;
1451   int uid = node->uid;
1452
1453   gcc_assert (node->same_body_alias);
1454   if (node->previous)
1455     node->previous->next = node->next;
1456   else
1457     node->same_body->same_body = node->next;
1458   if (node->next)
1459     node->next->previous = node->previous;
1460   node->next = NULL;
1461   node->previous = NULL;
1462   slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1463   if (*slot == node)
1464     htab_clear_slot (cgraph_hash, slot);
1465   if (assembler_name_hash)
1466     {
1467       tree name = DECL_ASSEMBLER_NAME (node->decl);
1468       slot = htab_find_slot_with_hash (assembler_name_hash, name,
1469                                        decl_assembler_name_hash (name),
1470                                        NO_INSERT);
1471       if (slot && *slot == node)
1472         htab_clear_slot (assembler_name_hash, slot);
1473     }
1474
1475   /* Clear out the node to NULL all pointers and add the node to the free
1476      list.  */
1477   memset (node, 0, sizeof(*node));
1478   node->uid = uid;
1479   NEXT_FREE_NODE (node) = free_nodes;
1480   free_nodes = node;
1481 }
1482
1483 /* Remove the node from cgraph.  */
1484
1485 void
1486 cgraph_remove_node (struct cgraph_node *node)
1487 {
1488   void **slot;
1489   bool kill_body = false;
1490   struct cgraph_node *n;
1491   int uid = node->uid;
1492
1493   cgraph_call_node_removal_hooks (node);
1494   cgraph_node_remove_callers (node);
1495   cgraph_node_remove_callees (node);
1496   ipa_remove_all_references (&node->ref_list);
1497   ipa_remove_all_refering (&node->ref_list);
1498   VEC_free (ipa_opt_pass, heap,
1499             node->ipa_transforms_to_apply);
1500
1501   /* Incremental inlining access removed nodes stored in the postorder list.
1502      */
1503   node->needed = node->reachable = false;
1504   for (n = node->nested; n; n = n->next_nested)
1505     n->origin = NULL;
1506   node->nested = NULL;
1507   if (node->origin)
1508     {
1509       struct cgraph_node **node2 = &node->origin->nested;
1510
1511       while (*node2 != node)
1512         node2 = &(*node2)->next_nested;
1513       *node2 = node->next_nested;
1514     }
1515   if (node->previous)
1516     node->previous->next = node->next;
1517   else
1518     cgraph_nodes = node->next;
1519   if (node->next)
1520     node->next->previous = node->previous;
1521   node->next = NULL;
1522   node->previous = NULL;
1523   slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1524   if (*slot == node)
1525     {
1526       struct cgraph_node *next_inline_clone;
1527
1528       for (next_inline_clone = node->clones;
1529            next_inline_clone && next_inline_clone->decl != node->decl;
1530            next_inline_clone = next_inline_clone->next_sibling_clone)
1531         ;
1532
1533       /* If there is inline clone of the node being removed, we need
1534          to put it into the position of removed node and reorganize all
1535          other clones to be based on it.  */
1536       if (next_inline_clone)
1537         {
1538           struct cgraph_node *n;
1539           struct cgraph_node *new_clones;
1540
1541           *slot = next_inline_clone;
1542
1543           /* Unlink inline clone from the list of clones of removed node.  */
1544           if (next_inline_clone->next_sibling_clone)
1545             next_inline_clone->next_sibling_clone->prev_sibling_clone
1546               = next_inline_clone->prev_sibling_clone;
1547           if (next_inline_clone->prev_sibling_clone)
1548             {
1549               gcc_assert (node->clones != next_inline_clone);
1550               next_inline_clone->prev_sibling_clone->next_sibling_clone
1551                 = next_inline_clone->next_sibling_clone;
1552             }
1553           else
1554             {
1555               gcc_assert (node->clones == next_inline_clone);
1556               node->clones = next_inline_clone->next_sibling_clone;
1557             }
1558
1559           new_clones = node->clones;
1560           node->clones = NULL;
1561
1562           /* Copy clone info.  */
1563           next_inline_clone->clone = node->clone;
1564
1565           /* Now place it into clone tree at same level at NODE.  */
1566           next_inline_clone->clone_of = node->clone_of;
1567           next_inline_clone->prev_sibling_clone = NULL;
1568           next_inline_clone->next_sibling_clone = NULL;
1569           if (node->clone_of)
1570             {
1571               if (node->clone_of->clones)
1572                 node->clone_of->clones->prev_sibling_clone = next_inline_clone;
1573               next_inline_clone->next_sibling_clone = node->clone_of->clones;
1574               node->clone_of->clones = next_inline_clone;
1575             }
1576
1577           /* Merge the clone list.  */
1578           if (new_clones)
1579             {
1580               if (!next_inline_clone->clones)
1581                 next_inline_clone->clones = new_clones;
1582               else
1583                 {
1584                   n = next_inline_clone->clones;
1585                   while (n->next_sibling_clone)
1586                     n =  n->next_sibling_clone;
1587                   n->next_sibling_clone = new_clones;
1588                   new_clones->prev_sibling_clone = n;
1589                 }
1590             }
1591
1592           /* Update clone_of pointers.  */
1593           n = new_clones;
1594           while (n)
1595             {
1596               n->clone_of = next_inline_clone;
1597               n = n->next_sibling_clone;
1598             }
1599         }
1600       else
1601         {
1602           htab_clear_slot (cgraph_hash, slot);
1603           kill_body = true;
1604         }
1605
1606     }
1607   if (node->prev_sibling_clone)
1608     node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1609   else if (node->clone_of)
1610     node->clone_of->clones = node->next_sibling_clone;
1611   if (node->next_sibling_clone)
1612     node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1613   if (node->clones)
1614     {
1615       struct cgraph_node *n, *next;
1616
1617       if (node->clone_of)
1618         {
1619           for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1620             n->clone_of = node->clone_of;
1621           n->clone_of = node->clone_of;
1622           n->next_sibling_clone = node->clone_of->clones;
1623           if (node->clone_of->clones)
1624             node->clone_of->clones->prev_sibling_clone = n;
1625           node->clone_of->clones = node->clones;
1626         }
1627       else
1628         {
1629           /* We are removing node with clones.  this makes clones inconsistent,
1630              but assume they will be removed subsequently and just keep clone
1631              tree intact.  This can happen in unreachable function removal since
1632              we remove unreachable functions in random order, not by bottom-up
1633              walk of clone trees.  */
1634           for (n = node->clones; n; n = next)
1635             {
1636                next = n->next_sibling_clone;
1637                n->next_sibling_clone = NULL;
1638                n->prev_sibling_clone = NULL;
1639                n->clone_of = NULL;
1640             }
1641         }
1642     }
1643
1644   while (node->same_body)
1645     cgraph_remove_same_body_alias (node->same_body);
1646
1647   if (node->same_comdat_group)
1648     {
1649       struct cgraph_node *prev;
1650       for (prev = node->same_comdat_group;
1651            prev->same_comdat_group != node;
1652            prev = prev->same_comdat_group)
1653         ;
1654       if (node->same_comdat_group == prev)
1655         prev->same_comdat_group = NULL;
1656       else
1657         prev->same_comdat_group = node->same_comdat_group;
1658       node->same_comdat_group = NULL;
1659     }
1660
1661   /* While all the clones are removed after being proceeded, the function
1662      itself is kept in the cgraph even after it is compiled.  Check whether
1663      we are done with this body and reclaim it proactively if this is the case.
1664      */
1665   if (!kill_body && *slot)
1666     {
1667       struct cgraph_node *n = (struct cgraph_node *) *slot;
1668       if (!n->clones && !n->clone_of && !n->global.inlined_to
1669           && (cgraph_global_info_ready
1670               && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl)
1671                   || n->in_other_partition)))
1672         kill_body = true;
1673     }
1674   if (assembler_name_hash)
1675     {
1676       tree name = DECL_ASSEMBLER_NAME (node->decl);
1677       slot = htab_find_slot_with_hash (assembler_name_hash, name,
1678                                        decl_assembler_name_hash (name),
1679                                        NO_INSERT);
1680       /* Inline clones are not hashed.  */
1681       if (slot && *slot == node)
1682         htab_clear_slot (assembler_name_hash, slot);
1683     }
1684
1685   if (kill_body)
1686     cgraph_release_function_body (node);
1687   node->decl = NULL;
1688   if (node->call_site_hash)
1689     {
1690       htab_delete (node->call_site_hash);
1691       node->call_site_hash = NULL;
1692     }
1693   cgraph_n_nodes--;
1694
1695   /* Clear out the node to NULL all pointers and add the node to the free
1696      list.  */
1697   memset (node, 0, sizeof(*node));
1698   node->uid = uid;
1699   NEXT_FREE_NODE (node) = free_nodes;
1700   free_nodes = node;
1701 }
1702
1703 /* Remove the node from cgraph and all inline clones inlined into it.
1704    Skip however removal of FORBIDDEN_NODE and return true if it needs to be
1705    removed.  This allows to call the function from outer loop walking clone
1706    tree.  */
1707
1708 bool
1709 cgraph_remove_node_and_inline_clones (struct cgraph_node *node, struct cgraph_node *forbidden_node)
1710 {
1711   struct cgraph_edge *e, *next;
1712   bool found = false;
1713
1714   if (node == forbidden_node)
1715     return true;
1716   for (e = node->callees; e; e = next)
1717     {
1718       next = e->next_callee;
1719       if (!e->inline_failed)
1720         found |= cgraph_remove_node_and_inline_clones (e->callee, forbidden_node);
1721     }
1722   cgraph_remove_node (node);
1723   return found;
1724 }
1725
1726 /* Notify finalize_compilation_unit that given node is reachable.  */
1727
1728 void
1729 cgraph_mark_reachable_node (struct cgraph_node *node)
1730 {
1731   if (!node->reachable && node->local.finalized)
1732     {
1733       if (cgraph_global_info_ready)
1734         {
1735           /* Verify that function does not appear to be needed out of blue
1736              during the optimization process.  This can happen for extern
1737              inlines when bodies was removed after inlining.  */
1738           gcc_assert ((node->analyzed || node->in_other_partition
1739                        || DECL_EXTERNAL (node->decl)));
1740         }
1741       else
1742         notice_global_symbol (node->decl);
1743       node->reachable = 1;
1744
1745       node->next_needed = cgraph_nodes_queue;
1746       cgraph_nodes_queue = node;
1747     }
1748 }
1749
1750 /* Likewise indicate that a node is needed, i.e. reachable via some
1751    external means.  */
1752
1753 void
1754 cgraph_mark_needed_node (struct cgraph_node *node)
1755 {
1756   node->needed = 1;
1757   gcc_assert (!node->global.inlined_to);
1758   cgraph_mark_reachable_node (node);
1759 }
1760
1761 /* Likewise indicate that a node is having address taken.  */
1762
1763 void
1764 cgraph_mark_address_taken_node (struct cgraph_node *node)
1765 {
1766   gcc_assert (!node->global.inlined_to);
1767   cgraph_mark_reachable_node (node);
1768   node->address_taken = 1;
1769 }
1770
1771 /* Return local info for the compiled function.  */
1772
1773 struct cgraph_local_info *
1774 cgraph_local_info (tree decl)
1775 {
1776   struct cgraph_node *node;
1777
1778   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1779   node = cgraph_node (decl);
1780   return &node->local;
1781 }
1782
1783 /* Return local info for the compiled function.  */
1784
1785 struct cgraph_global_info *
1786 cgraph_global_info (tree decl)
1787 {
1788   struct cgraph_node *node;
1789
1790   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1791   node = cgraph_node (decl);
1792   return &node->global;
1793 }
1794
1795 /* Return local info for the compiled function.  */
1796
1797 struct cgraph_rtl_info *
1798 cgraph_rtl_info (tree decl)
1799 {
1800   struct cgraph_node *node;
1801
1802   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1803   node = cgraph_node (decl);
1804   if (decl != current_function_decl
1805       && !TREE_ASM_WRITTEN (node->decl))
1806     return NULL;
1807   return &node->rtl;
1808 }
1809
1810 /* Return a string describing the failure REASON.  */
1811
1812 const char*
1813 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1814 {
1815 #undef DEFCIFCODE
1816 #define DEFCIFCODE(code, string)        string,
1817
1818   static const char *cif_string_table[CIF_N_REASONS] = {
1819 #include "cif-code.def"
1820   };
1821
1822   /* Signedness of an enum type is implementation defined, so cast it
1823      to unsigned before testing. */
1824   gcc_assert ((unsigned) reason < CIF_N_REASONS);
1825   return cif_string_table[reason];
1826 }
1827
1828 /* Return name of the node used in debug output.  */
1829 const char *
1830 cgraph_node_name (struct cgraph_node *node)
1831 {
1832   return lang_hooks.decl_printable_name (node->decl, 2);
1833 }
1834
1835 /* Names used to print out the availability enum.  */
1836 const char * const cgraph_availability_names[] =
1837   {"unset", "not_available", "overwritable", "available", "local"};
1838
1839
1840 /* Dump call graph node NODE to file F.  */
1841
1842 void
1843 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1844 {
1845   struct cgraph_edge *edge;
1846   int indirect_calls_count = 0;
1847
1848   fprintf (f, "%s/%i(%i)", cgraph_node_name (node), node->uid,
1849            node->pid);
1850   dump_addr (f, " @", (void *)node);
1851   if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
1852     fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
1853   if (node->global.inlined_to)
1854     fprintf (f, " (inline copy in %s/%i)",
1855              cgraph_node_name (node->global.inlined_to),
1856              node->global.inlined_to->uid);
1857   if (node->same_comdat_group)
1858     fprintf (f, " (same comdat group as %s/%i)",
1859              cgraph_node_name (node->same_comdat_group),
1860              node->same_comdat_group->uid);
1861   if (node->clone_of)
1862     fprintf (f, " (clone of %s/%i)",
1863              cgraph_node_name (node->clone_of),
1864              node->clone_of->uid);
1865   if (cgraph_function_flags_ready)
1866     fprintf (f, " availability:%s",
1867              cgraph_availability_names [cgraph_function_body_availability (node)]);
1868   if (node->analyzed)
1869     fprintf (f, " analyzed");
1870   if (node->in_other_partition)
1871     fprintf (f, " in_other_partition");
1872   if (node->count)
1873     fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1874              (HOST_WIDEST_INT)node->count);
1875   if (node->local.inline_summary.self_time)
1876     fprintf (f, " %i time, %i benefit", node->local.inline_summary.self_time,
1877                                         node->local.inline_summary.time_inlining_benefit);
1878   if (node->global.time && node->global.time
1879       != node->local.inline_summary.self_time)
1880     fprintf (f, " (%i after inlining)", node->global.time);
1881   if (node->local.inline_summary.self_size)
1882     fprintf (f, " %i size, %i benefit", node->local.inline_summary.self_size,
1883                                         node->local.inline_summary.size_inlining_benefit);
1884   if (node->global.size && node->global.size
1885       != node->local.inline_summary.self_size)
1886     fprintf (f, " (%i after inlining)", node->global.size);
1887   if (node->local.inline_summary.estimated_self_stack_size)
1888     fprintf (f, " %i bytes stack usage", (int)node->local.inline_summary.estimated_self_stack_size);
1889   if (node->global.estimated_stack_size != node->local.inline_summary.estimated_self_stack_size)
1890     fprintf (f, " %i bytes after inlining", (int)node->global.estimated_stack_size);
1891   if (node->origin)
1892     fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
1893   if (node->needed)
1894     fprintf (f, " needed");
1895   if (node->address_taken)
1896     fprintf (f, " address_taken");
1897   else if (node->reachable)
1898     fprintf (f, " reachable");
1899   else if (node->reachable_from_other_partition)
1900     fprintf (f, " reachable_from_other_partition");
1901   if (gimple_has_body_p (node->decl))
1902     fprintf (f, " body");
1903   if (node->process)
1904     fprintf (f, " process");
1905   if (node->local.local)
1906     fprintf (f, " local");
1907   if (node->local.externally_visible)
1908     fprintf (f, " externally_visible");
1909   if (node->resolution != LDPR_UNKNOWN)
1910     fprintf (f, " %s",
1911              ld_plugin_symbol_resolution_names[(int)node->resolution]);
1912   if (node->local.finalized)
1913     fprintf (f, " finalized");
1914   if (node->local.disregard_inline_limits)
1915     fprintf (f, " always_inline");
1916   else if (node->local.inlinable)
1917     fprintf (f, " inlinable");
1918   else if (node->local.versionable)
1919     fprintf (f, " versionable");
1920   if (node->local.redefined_extern_inline)
1921     fprintf (f, " redefined_extern_inline");
1922   if (TREE_ASM_WRITTEN (node->decl))
1923     fprintf (f, " asm_written");
1924   if (node->only_called_at_startup)
1925     fprintf (f, " only_called_at_startup");
1926   if (node->only_called_at_exit)
1927     fprintf (f, " only_called_at_exit");
1928
1929   fprintf (f, "\n  called by: ");
1930   for (edge = node->callers; edge; edge = edge->next_caller)
1931     {
1932       fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
1933                edge->caller->uid);
1934       if (edge->count)
1935         fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1936                  (HOST_WIDEST_INT)edge->count);
1937       if (edge->frequency)
1938         fprintf (f, "(%.2f per call) ",
1939                  edge->frequency / (double)CGRAPH_FREQ_BASE);
1940       if (!edge->inline_failed)
1941         fprintf(f, "(inlined) ");
1942       if (edge->indirect_inlining_edge)
1943         fprintf(f, "(indirect_inlining) ");
1944       if (edge->can_throw_external)
1945         fprintf(f, "(can throw external) ");
1946     }
1947
1948   fprintf (f, "\n  calls: ");
1949   for (edge = node->callees; edge; edge = edge->next_callee)
1950     {
1951       fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
1952                edge->callee->uid);
1953       if (!edge->inline_failed)
1954         fprintf(f, "(inlined) ");
1955       if (edge->indirect_inlining_edge)
1956         fprintf(f, "(indirect_inlining) ");
1957       if (edge->count)
1958         fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1959                  (HOST_WIDEST_INT)edge->count);
1960       if (edge->frequency)
1961         fprintf (f, "(%.2f per call) ",
1962                  edge->frequency / (double)CGRAPH_FREQ_BASE);
1963       if (edge->loop_nest)
1964         fprintf (f, "(nested in %i loops) ", edge->loop_nest);
1965       if (edge->can_throw_external)
1966         fprintf(f, "(can throw external) ");
1967     }
1968   fprintf (f, "\n");
1969   fprintf (f, "  References: ");
1970   ipa_dump_references (f, &node->ref_list);
1971   fprintf (f, "  Refering this function: ");
1972   ipa_dump_refering (f, &node->ref_list);
1973
1974   for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1975     indirect_calls_count++;
1976   if (indirect_calls_count)
1977     fprintf (f, "  has %i outgoing edges for indirect calls.\n",
1978              indirect_calls_count);
1979
1980   if (node->same_body)
1981     {
1982       struct cgraph_node *n;
1983       fprintf (f, "  aliases & thunks:");
1984       for (n = node->same_body; n; n = n->next)
1985         {
1986           fprintf (f, " %s/%i", cgraph_node_name (n), n->uid);
1987           if (n->thunk.thunk_p)
1988             {
1989               fprintf (f, " (thunk of %s fixed offset %i virtual value %i has "
1990                        "virtual offset %i",
1991                        lang_hooks.decl_printable_name (n->thunk.alias, 2),
1992                        (int)n->thunk.fixed_offset,
1993                        (int)n->thunk.virtual_value,
1994                        (int)n->thunk.virtual_offset_p);
1995               fprintf (f, ")");
1996             }
1997           if (DECL_ASSEMBLER_NAME_SET_P (n->decl))
1998             fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
1999         }
2000       fprintf (f, "\n");
2001     }
2002 }
2003
2004
2005 /* Dump call graph node NODE to stderr.  */
2006
2007 DEBUG_FUNCTION void
2008 debug_cgraph_node (struct cgraph_node *node)
2009 {
2010   dump_cgraph_node (stderr, node);
2011 }
2012
2013
2014 /* Dump the callgraph to file F.  */
2015
2016 void
2017 dump_cgraph (FILE *f)
2018 {
2019   struct cgraph_node *node;
2020
2021   fprintf (f, "callgraph:\n\n");
2022   for (node = cgraph_nodes; node; node = node->next)
2023     dump_cgraph_node (f, node);
2024 }
2025
2026
2027 /* Dump the call graph to stderr.  */
2028
2029 DEBUG_FUNCTION void
2030 debug_cgraph (void)
2031 {
2032   dump_cgraph (stderr);
2033 }
2034
2035
2036 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables.  */
2037
2038 void
2039 change_decl_assembler_name (tree decl, tree name)
2040 {
2041   struct cgraph_node *node;
2042   void **slot;
2043   if (!DECL_ASSEMBLER_NAME_SET_P (decl))
2044     SET_DECL_ASSEMBLER_NAME (decl, name);
2045   else
2046     {
2047       if (name == DECL_ASSEMBLER_NAME (decl))
2048         return;
2049
2050       if (assembler_name_hash
2051           && TREE_CODE (decl) == FUNCTION_DECL
2052           && (node = cgraph_get_node_or_alias (decl)) != NULL)
2053         {
2054           tree old_name = DECL_ASSEMBLER_NAME (decl);
2055           slot = htab_find_slot_with_hash (assembler_name_hash, old_name,
2056                                            decl_assembler_name_hash (old_name),
2057                                            NO_INSERT);
2058           /* Inline clones are not hashed.  */
2059           if (slot && *slot == node)
2060             htab_clear_slot (assembler_name_hash, slot);
2061         }
2062       if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
2063           && DECL_RTL_SET_P (decl))
2064         warning (0, "%D renamed after being referenced in assembly", decl);
2065
2066       SET_DECL_ASSEMBLER_NAME (decl, name);
2067     }
2068   if (assembler_name_hash
2069       && TREE_CODE (decl) == FUNCTION_DECL
2070       && (node = cgraph_get_node_or_alias (decl)) != NULL)
2071     {
2072       slot = htab_find_slot_with_hash (assembler_name_hash, name,
2073                                        decl_assembler_name_hash (name),
2074                                        INSERT);
2075       gcc_assert (!*slot);
2076       *slot = node;
2077     }
2078 }
2079
2080 /* Add a top-level asm statement to the list.  */
2081
2082 struct cgraph_asm_node *
2083 cgraph_add_asm_node (tree asm_str)
2084 {
2085   struct cgraph_asm_node *node;
2086
2087   node = ggc_alloc_cleared_cgraph_asm_node ();
2088   node->asm_str = asm_str;
2089   node->order = cgraph_order++;
2090   node->next = NULL;
2091   if (cgraph_asm_nodes == NULL)
2092     cgraph_asm_nodes = node;
2093   else
2094     cgraph_asm_last_node->next = node;
2095   cgraph_asm_last_node = node;
2096   return node;
2097 }
2098
2099 /* Return true when the DECL can possibly be inlined.  */
2100 bool
2101 cgraph_function_possibly_inlined_p (tree decl)
2102 {
2103   if (!cgraph_global_info_ready)
2104     return !DECL_UNINLINABLE (decl);
2105   return DECL_POSSIBLY_INLINED (decl);
2106 }
2107
2108 /* Create clone of E in the node N represented by CALL_EXPR the callgraph.  */
2109 struct cgraph_edge *
2110 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
2111                    gimple call_stmt, unsigned stmt_uid, gcov_type count_scale,
2112                    int freq_scale, int loop_nest, bool update_original)
2113 {
2114   struct cgraph_edge *new_edge;
2115   gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
2116   gcov_type freq;
2117
2118   /* We do not want to ignore loop nest after frequency drops to 0.  */
2119   if (!freq_scale)
2120     freq_scale = 1;
2121   freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
2122   if (freq > CGRAPH_FREQ_MAX)
2123     freq = CGRAPH_FREQ_MAX;
2124
2125   if (e->indirect_unknown_callee)
2126     {
2127       tree decl;
2128
2129       if (call_stmt && (decl = gimple_call_fndecl (call_stmt)))
2130         {
2131           struct cgraph_node *callee = cgraph_node (decl);
2132           new_edge = cgraph_create_edge (n, callee, call_stmt, count, freq,
2133                                          e->loop_nest + loop_nest);
2134         }
2135       else
2136         {
2137           new_edge = cgraph_create_indirect_edge (n, call_stmt,
2138                                                   e->indirect_info->ecf_flags,
2139                                                   count, freq,
2140                                                   e->loop_nest + loop_nest);
2141           *new_edge->indirect_info = *e->indirect_info;
2142         }
2143     }
2144   else
2145     {
2146       new_edge = cgraph_create_edge (n, e->callee, call_stmt, count, freq,
2147                                      e->loop_nest + loop_nest);
2148       if (e->indirect_info)
2149         {
2150           new_edge->indirect_info
2151             = ggc_alloc_cleared_cgraph_indirect_call_info ();
2152           *new_edge->indirect_info = *e->indirect_info;
2153         }
2154     }
2155
2156   new_edge->inline_failed = e->inline_failed;
2157   new_edge->indirect_inlining_edge = e->indirect_inlining_edge;
2158   new_edge->lto_stmt_uid = stmt_uid;
2159   /* Clone flags that depend on call_stmt availability manually.  */
2160   new_edge->can_throw_external = e->can_throw_external;
2161   new_edge->call_stmt_cannot_inline_p = e->call_stmt_cannot_inline_p;
2162   if (update_original)
2163     {
2164       e->count -= new_edge->count;
2165       if (e->count < 0)
2166         e->count = 0;
2167     }
2168   cgraph_call_edge_duplication_hooks (e, new_edge);
2169   return new_edge;
2170 }
2171
2172 /* Create node representing clone of N executed COUNT times.  Decrease
2173    the execution counts from original node too.
2174    The new clone will have decl set to DECL that may or may not be the same
2175    as decl of N.
2176
2177    When UPDATE_ORIGINAL is true, the counts are subtracted from the original
2178    function's profile to reflect the fact that part of execution is handled
2179    by node.  */
2180 struct cgraph_node *
2181 cgraph_clone_node (struct cgraph_node *n, tree decl, gcov_type count, int freq,
2182                    int loop_nest, bool update_original,
2183                    VEC(cgraph_edge_p,heap) *redirect_callers)
2184 {
2185   struct cgraph_node *new_node = cgraph_create_node ();
2186   struct cgraph_edge *e;
2187   gcov_type count_scale;
2188   unsigned i;
2189
2190   new_node->decl = decl;
2191   new_node->origin = n->origin;
2192   if (new_node->origin)
2193     {
2194       new_node->next_nested = new_node->origin->nested;
2195       new_node->origin->nested = new_node;
2196     }
2197   new_node->analyzed = n->analyzed;
2198   new_node->local = n->local;
2199   new_node->local.externally_visible = false;
2200   new_node->local.local = true;
2201   new_node->local.vtable_method = false;
2202   new_node->global = n->global;
2203   new_node->rtl = n->rtl;
2204   new_node->count = count;
2205   new_node->frequency = n->frequency;
2206   new_node->clone = n->clone;
2207   new_node->clone.tree_map = 0;
2208   if (n->count)
2209     {
2210       if (new_node->count > n->count)
2211         count_scale = REG_BR_PROB_BASE;
2212       else
2213         count_scale = new_node->count * REG_BR_PROB_BASE / n->count;
2214     }
2215   else
2216     count_scale = 0;
2217   if (update_original)
2218     {
2219       n->count -= count;
2220       if (n->count < 0)
2221         n->count = 0;
2222     }
2223
2224   FOR_EACH_VEC_ELT (cgraph_edge_p, redirect_callers, i, e)
2225     {
2226       /* Redirect calls to the old version node to point to its new
2227          version.  */
2228       cgraph_redirect_edge_callee (e, new_node);
2229     }
2230
2231
2232   for (e = n->callees;e; e=e->next_callee)
2233     cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
2234                        count_scale, freq, loop_nest, update_original);
2235
2236   for (e = n->indirect_calls; e; e = e->next_callee)
2237     cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
2238                        count_scale, freq, loop_nest, update_original);
2239   ipa_clone_references (new_node, NULL, &n->ref_list);
2240
2241   new_node->next_sibling_clone = n->clones;
2242   if (n->clones)
2243     n->clones->prev_sibling_clone = new_node;
2244   n->clones = new_node;
2245   new_node->clone_of = n;
2246
2247   cgraph_call_node_duplication_hooks (n, new_node);
2248   if (n->decl != decl)
2249     {
2250       struct cgraph_node **slot;
2251       slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, new_node, INSERT);
2252       gcc_assert (!*slot);
2253       *slot = new_node;
2254       if (assembler_name_hash)
2255         {
2256           void **aslot;
2257           tree name = DECL_ASSEMBLER_NAME (decl);
2258
2259           aslot = htab_find_slot_with_hash (assembler_name_hash, name,
2260                                             decl_assembler_name_hash (name),
2261                                             INSERT);
2262           gcc_assert (!*aslot);
2263           *aslot = new_node;
2264         }
2265     }
2266   return new_node;
2267 }
2268
2269 /* Create a new name for clone of DECL, add SUFFIX.  Returns an identifier.  */
2270
2271 static GTY(()) unsigned int clone_fn_id_num;
2272
2273 tree
2274 clone_function_name (tree decl, const char *suffix)
2275 {
2276   tree name = DECL_ASSEMBLER_NAME (decl);
2277   size_t len = IDENTIFIER_LENGTH (name);
2278   char *tmp_name, *prefix;
2279
2280   prefix = XALLOCAVEC (char, len + strlen (suffix) + 2);
2281   memcpy (prefix, IDENTIFIER_POINTER (name), len);
2282   strcpy (prefix + len + 1, suffix);
2283 #ifndef NO_DOT_IN_LABEL
2284   prefix[len] = '.';
2285 #elif !defined NO_DOLLAR_IN_LABEL
2286   prefix[len] = '$';
2287 #else
2288   prefix[len] = '_';
2289 #endif
2290   ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++);
2291   return get_identifier (tmp_name);
2292 }
2293
2294 /* Create callgraph node clone with new declaration.  The actual body will
2295    be copied later at compilation stage.
2296
2297    TODO: after merging in ipa-sra use function call notes instead of args_to_skip
2298    bitmap interface.
2299    */
2300 struct cgraph_node *
2301 cgraph_create_virtual_clone (struct cgraph_node *old_node,
2302                              VEC(cgraph_edge_p,heap) *redirect_callers,
2303                              VEC(ipa_replace_map_p,gc) *tree_map,
2304                              bitmap args_to_skip,
2305                              const char * suffix)
2306 {
2307   tree old_decl = old_node->decl;
2308   struct cgraph_node *new_node = NULL;
2309   tree new_decl;
2310   size_t i;
2311   struct ipa_replace_map *map;
2312
2313   if (!flag_wpa)
2314     gcc_checking_assert  (tree_versionable_function_p (old_decl));
2315
2316   gcc_assert (old_node->local.can_change_signature || !args_to_skip);
2317
2318   /* Make a new FUNCTION_DECL tree node */
2319   if (!args_to_skip)
2320     new_decl = copy_node (old_decl);
2321   else
2322     new_decl = build_function_decl_skip_args (old_decl, args_to_skip);
2323   DECL_STRUCT_FUNCTION (new_decl) = NULL;
2324
2325   /* Generate a new name for the new version. */
2326   DECL_NAME (new_decl) = clone_function_name (old_decl, suffix);
2327   SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
2328   SET_DECL_RTL (new_decl, NULL);
2329
2330   new_node = cgraph_clone_node (old_node, new_decl, old_node->count,
2331                                 CGRAPH_FREQ_BASE, 0, false,
2332                                 redirect_callers);
2333   /* Update the properties.
2334      Make clone visible only within this translation unit.  Make sure
2335      that is not weak also.
2336      ??? We cannot use COMDAT linkage because there is no
2337      ABI support for this.  */
2338   DECL_EXTERNAL (new_node->decl) = 0;
2339   if (DECL_ONE_ONLY (old_decl))
2340     DECL_SECTION_NAME (new_node->decl) = NULL;
2341   DECL_COMDAT_GROUP (new_node->decl) = 0;
2342   TREE_PUBLIC (new_node->decl) = 0;
2343   DECL_COMDAT (new_node->decl) = 0;
2344   DECL_WEAK (new_node->decl) = 0;
2345   DECL_VIRTUAL_P (new_node->decl) = 0;
2346   new_node->clone.tree_map = tree_map;
2347   new_node->clone.args_to_skip = args_to_skip;
2348   FOR_EACH_VEC_ELT (ipa_replace_map_p, tree_map, i, map)
2349     {
2350       tree var = map->new_tree;
2351
2352       STRIP_NOPS (var);
2353       if (TREE_CODE (var) != ADDR_EXPR)
2354         continue;
2355       var = get_base_var (var);
2356       if (!var)
2357         continue;
2358
2359       /* Record references of the future statement initializing the constant
2360          argument.  */
2361       if (TREE_CODE (var) == FUNCTION_DECL)
2362         ipa_record_reference (new_node, NULL, cgraph_node (var),
2363                               NULL, IPA_REF_ADDR, NULL);
2364       else if (TREE_CODE (var) == VAR_DECL)
2365         ipa_record_reference (new_node, NULL, NULL, varpool_node (var),
2366                               IPA_REF_ADDR, NULL);
2367     }
2368   if (!args_to_skip)
2369     new_node->clone.combined_args_to_skip = old_node->clone.combined_args_to_skip;
2370   else if (old_node->clone.combined_args_to_skip)
2371     {
2372       int newi = 0, oldi = 0;
2373       tree arg;
2374       bitmap new_args_to_skip = BITMAP_GGC_ALLOC ();
2375       struct cgraph_node *orig_node;
2376       for (orig_node = old_node; orig_node->clone_of; orig_node = orig_node->clone_of)
2377         ;
2378       for (arg = DECL_ARGUMENTS (orig_node->decl); arg; arg = DECL_CHAIN (arg), oldi++)
2379         {
2380           if (bitmap_bit_p (old_node->clone.combined_args_to_skip, oldi))
2381             {
2382               bitmap_set_bit (new_args_to_skip, oldi);
2383               continue;
2384             }
2385           if (bitmap_bit_p (args_to_skip, newi))
2386             bitmap_set_bit (new_args_to_skip, oldi);
2387           newi++;
2388         }
2389       new_node->clone.combined_args_to_skip = new_args_to_skip;
2390     }
2391   else
2392     new_node->clone.combined_args_to_skip = args_to_skip;
2393   new_node->local.externally_visible = 0;
2394   new_node->local.local = 1;
2395   new_node->lowered = true;
2396   new_node->reachable = true;
2397
2398
2399   return new_node;
2400 }
2401
2402 /* NODE is no longer nested function; update cgraph accordingly.  */
2403 void
2404 cgraph_unnest_node (struct cgraph_node *node)
2405 {
2406   struct cgraph_node **node2 = &node->origin->nested;
2407   gcc_assert (node->origin);
2408
2409   while (*node2 != node)
2410     node2 = &(*node2)->next_nested;
2411   *node2 = node->next_nested;
2412   node->origin = NULL;
2413 }
2414
2415 /* Return function availability.  See cgraph.h for description of individual
2416    return values.  */
2417 enum availability
2418 cgraph_function_body_availability (struct cgraph_node *node)
2419 {
2420   enum availability avail;
2421   gcc_assert (cgraph_function_flags_ready);
2422   if (!node->analyzed)
2423     avail = AVAIL_NOT_AVAILABLE;
2424   else if (node->local.local)
2425     avail = AVAIL_LOCAL;
2426   else if (!node->local.externally_visible)
2427     avail = AVAIL_AVAILABLE;
2428   /* Inline functions are safe to be analyzed even if their symbol can
2429      be overwritten at runtime.  It is not meaningful to enforce any sane
2430      behaviour on replacing inline function by different body.  */
2431   else if (DECL_DECLARED_INLINE_P (node->decl))
2432     avail = AVAIL_AVAILABLE;
2433
2434   /* If the function can be overwritten, return OVERWRITABLE.  Take
2435      care at least of two notable extensions - the COMDAT functions
2436      used to share template instantiations in C++ (this is symmetric
2437      to code cp_cannot_inline_tree_fn and probably shall be shared and
2438      the inlinability hooks completely eliminated).
2439
2440      ??? Does the C++ one definition rule allow us to always return
2441      AVAIL_AVAILABLE here?  That would be good reason to preserve this
2442      bit.  */
2443
2444   else if (decl_replaceable_p (node->decl) && !DECL_EXTERNAL (node->decl))
2445     avail = AVAIL_OVERWRITABLE;
2446   else avail = AVAIL_AVAILABLE;
2447
2448   return avail;
2449 }
2450
2451 /* Add the function FNDECL to the call graph.
2452    Unlike cgraph_finalize_function, this function is intended to be used
2453    by middle end and allows insertion of new function at arbitrary point
2454    of compilation.  The function can be either in high, low or SSA form
2455    GIMPLE.
2456
2457    The function is assumed to be reachable and have address taken (so no
2458    API breaking optimizations are performed on it).
2459
2460    Main work done by this function is to enqueue the function for later
2461    processing to avoid need the passes to be re-entrant.  */
2462
2463 void
2464 cgraph_add_new_function (tree fndecl, bool lowered)
2465 {
2466   struct cgraph_node *node;
2467   switch (cgraph_state)
2468     {
2469       case CGRAPH_STATE_CONSTRUCTION:
2470         /* Just enqueue function to be processed at nearest occurrence.  */
2471         node = cgraph_node (fndecl);
2472         node->next_needed = cgraph_new_nodes;
2473         if (lowered)
2474           node->lowered = true;
2475         cgraph_new_nodes = node;
2476         break;
2477
2478       case CGRAPH_STATE_IPA:
2479       case CGRAPH_STATE_IPA_SSA:
2480       case CGRAPH_STATE_EXPANSION:
2481         /* Bring the function into finalized state and enqueue for later
2482            analyzing and compilation.  */
2483         node = cgraph_node (fndecl);
2484         node->local.local = false;
2485         node->local.finalized = true;
2486         node->reachable = node->needed = true;
2487         if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
2488           {
2489             push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2490             current_function_decl = fndecl;
2491             gimple_register_cfg_hooks ();
2492             tree_lowering_passes (fndecl);
2493             bitmap_obstack_initialize (NULL);
2494             if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2495               execute_pass_list (pass_early_local_passes.pass.sub);
2496             bitmap_obstack_release (NULL);
2497             pop_cfun ();
2498             current_function_decl = NULL;
2499
2500             lowered = true;
2501           }
2502         if (lowered)
2503           node->lowered = true;
2504         node->next_needed = cgraph_new_nodes;
2505         cgraph_new_nodes = node;
2506         break;
2507
2508       case CGRAPH_STATE_FINISHED:
2509         /* At the very end of compilation we have to do all the work up
2510            to expansion.  */
2511         push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2512         current_function_decl = fndecl;
2513         gimple_register_cfg_hooks ();
2514         if (!lowered)
2515           tree_lowering_passes (fndecl);
2516         bitmap_obstack_initialize (NULL);
2517         if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2518           execute_pass_list (pass_early_local_passes.pass.sub);
2519         bitmap_obstack_release (NULL);
2520         tree_rest_of_compilation (fndecl);
2521         pop_cfun ();
2522         current_function_decl = NULL;
2523         break;
2524     }
2525
2526   /* Set a personality if required and we already passed EH lowering.  */
2527   if (lowered
2528       && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
2529           == eh_personality_lang))
2530     DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
2531 }
2532
2533 /* Return true if NODE can be made local for API change.
2534    Extern inline functions and C++ COMDAT functions can be made local
2535    at the expense of possible code size growth if function is used in multiple
2536    compilation units.  */
2537 bool
2538 cgraph_node_can_be_local_p (struct cgraph_node *node)
2539 {
2540   return (!node->needed && !node->address_taken
2541           && ((DECL_COMDAT (node->decl) && !node->same_comdat_group)
2542               || !node->local.externally_visible));
2543 }
2544
2545 /* Make DECL local.  FIXME: We shouldn't need to mess with rtl this early,
2546    but other code such as notice_global_symbol generates rtl.  */
2547 void
2548 cgraph_make_decl_local (tree decl)
2549 {
2550   rtx rtl, symbol;
2551
2552   if (TREE_CODE (decl) == VAR_DECL)
2553     DECL_COMMON (decl) = 0;
2554   else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
2555
2556   if (DECL_COMDAT (decl))
2557     {
2558       /* It is possible that we are linking against library defining same COMDAT
2559          function.  To avoid conflict we need to rename our local name of the
2560          function just in the case WHOPR partitioning decide to make it hidden
2561          to avoid cross partition references.  */
2562       if (flag_wpa)
2563         {
2564           const char *old_name;
2565
2566           old_name  = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2567           if (TREE_CODE (decl) == FUNCTION_DECL)
2568             {
2569               struct cgraph_node *node = cgraph_get_node_or_alias (decl);
2570               change_decl_assembler_name (decl,
2571                                           clone_function_name (decl, "local"));
2572               if (node->local.lto_file_data)
2573                 lto_record_renamed_decl (node->local.lto_file_data,
2574                                          old_name,
2575                                          IDENTIFIER_POINTER
2576                                            (DECL_ASSEMBLER_NAME (decl)));
2577             }
2578           else if (TREE_CODE (decl) == VAR_DECL)
2579             {
2580               struct varpool_node *vnode = varpool_get_node (decl);
2581               /* change_decl_assembler_name will warn here on vtables because
2582                  C++ frontend still sets TREE_SYMBOL_REFERENCED on them.  */
2583               SET_DECL_ASSEMBLER_NAME (decl,
2584                                        clone_function_name (decl, "local"));
2585               if (vnode->lto_file_data)
2586                 lto_record_renamed_decl (vnode->lto_file_data,
2587                                          old_name,
2588                                          IDENTIFIER_POINTER
2589                                            (DECL_ASSEMBLER_NAME (decl)));
2590             }
2591         }
2592       DECL_SECTION_NAME (decl) = 0;
2593       DECL_COMDAT (decl) = 0;
2594     }
2595   DECL_COMDAT_GROUP (decl) = 0;
2596   DECL_WEAK (decl) = 0;
2597   DECL_EXTERNAL (decl) = 0;
2598   TREE_PUBLIC (decl) = 0;
2599   if (!DECL_RTL_SET_P (decl))
2600     return;
2601
2602   /* Update rtl flags.  */
2603   make_decl_rtl (decl);
2604
2605   rtl = DECL_RTL (decl);
2606   if (!MEM_P (rtl))
2607     return;
2608
2609   symbol = XEXP (rtl, 0);
2610   if (GET_CODE (symbol) != SYMBOL_REF)
2611     return;
2612
2613   SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
2614 }
2615
2616 /* Bring NODE local.  */
2617 void
2618 cgraph_make_node_local (struct cgraph_node *node)
2619 {
2620   gcc_assert (cgraph_node_can_be_local_p (node));
2621   if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2622     {
2623       struct cgraph_node *alias;
2624       cgraph_make_decl_local (node->decl);
2625
2626       for (alias = node->same_body; alias; alias = alias->next)
2627         cgraph_make_decl_local (alias->decl);
2628
2629       node->local.externally_visible = false;
2630       node->local.local = true;
2631       node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2632       gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2633     }
2634 }
2635
2636 /* Set TREE_NOTHROW on NODE's decl and on same_body aliases of NODE
2637    if any to NOTHROW.  */
2638
2639 void
2640 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
2641 {
2642   struct cgraph_node *alias;
2643   TREE_NOTHROW (node->decl) = nothrow;
2644   for (alias = node->same_body; alias; alias = alias->next)
2645     TREE_NOTHROW (alias->decl) = nothrow;
2646 }
2647
2648 /* Set TREE_READONLY on NODE's decl and on same_body aliases of NODE
2649    if any to READONLY.  */
2650
2651 void
2652 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
2653 {
2654   struct cgraph_node *alias;
2655   /* Static constructors and destructors without a side effect can be
2656      optimized out.  */
2657   if (!looping && readonly)
2658     {
2659       if (DECL_STATIC_CONSTRUCTOR (node->decl))
2660         DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2661       if (DECL_STATIC_DESTRUCTOR (node->decl))
2662         DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2663     }
2664   TREE_READONLY (node->decl) = readonly;
2665   DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2666   for (alias = node->same_body; alias; alias = alias->next)
2667     {
2668       TREE_READONLY (alias->decl) = readonly;
2669       DECL_LOOPING_CONST_OR_PURE_P (alias->decl) = looping;
2670     }
2671 }
2672
2673 /* Set DECL_PURE_P on NODE's decl and on same_body aliases of NODE
2674    if any to PURE.  */
2675
2676 void
2677 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
2678 {
2679   struct cgraph_node *alias;
2680   /* Static constructors and destructors without a side effect can be
2681      optimized out.  */
2682   if (!looping && pure)
2683     {
2684       if (DECL_STATIC_CONSTRUCTOR (node->decl))
2685         DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2686       if (DECL_STATIC_DESTRUCTOR (node->decl))
2687         DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2688     }
2689   DECL_PURE_P (node->decl) = pure;
2690   DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2691   for (alias = node->same_body; alias; alias = alias->next)
2692     {
2693       DECL_PURE_P (alias->decl) = pure;
2694       DECL_LOOPING_CONST_OR_PURE_P (alias->decl) = looping;
2695     }
2696 }
2697
2698 /* See if the frequency of NODE can be updated based on frequencies of its
2699    callers.  */
2700 bool
2701 cgraph_propagate_frequency (struct cgraph_node *node)
2702 {
2703   bool maybe_unlikely_executed = true, maybe_executed_once = true;
2704   bool only_called_at_startup = true;
2705   bool only_called_at_exit = true;
2706   bool changed = false;
2707   struct cgraph_edge *edge;
2708
2709   if (!node->local.local)
2710     return false;
2711   gcc_assert (node->analyzed);
2712   if (dump_file && (dump_flags & TDF_DETAILS))
2713     fprintf (dump_file, "Processing frequency %s\n", cgraph_node_name (node));
2714
2715   for (edge = node->callers;
2716        edge && (maybe_unlikely_executed || maybe_executed_once
2717                 || only_called_at_startup || only_called_at_exit);
2718        edge = edge->next_caller)
2719     {
2720       if (edge->caller != node)
2721         {
2722           only_called_at_startup &= edge->caller->only_called_at_startup;
2723           /* It makes sense to put main() together with the static constructors.
2724              It will be executed for sure, but rest of functions called from
2725              main are definitely not at startup only.  */
2726           if (MAIN_NAME_P (DECL_NAME (edge->caller->decl)))
2727             only_called_at_startup = 0;
2728           only_called_at_exit &= edge->caller->only_called_at_exit;
2729         }
2730       if (!edge->frequency)
2731         continue;
2732       switch (edge->caller->frequency)
2733         {
2734         case NODE_FREQUENCY_UNLIKELY_EXECUTED:
2735           break;
2736         case NODE_FREQUENCY_EXECUTED_ONCE:
2737           if (dump_file && (dump_flags & TDF_DETAILS))
2738             fprintf (dump_file, "  Called by %s that is executed once\n",
2739                      cgraph_node_name (edge->caller));
2740           maybe_unlikely_executed = false;
2741           if (edge->loop_nest)
2742             {
2743               maybe_executed_once = false;
2744               if (dump_file && (dump_flags & TDF_DETAILS))
2745                 fprintf (dump_file, "  Called in loop\n");
2746             }
2747           break;
2748         case NODE_FREQUENCY_HOT:
2749         case NODE_FREQUENCY_NORMAL:
2750           if (dump_file && (dump_flags & TDF_DETAILS))
2751             fprintf (dump_file, "  Called by %s that is normal or hot\n",
2752                      cgraph_node_name (edge->caller));
2753           maybe_unlikely_executed = false;
2754           maybe_executed_once = false;
2755           break;
2756         }
2757     }
2758   if ((only_called_at_startup && !only_called_at_exit)
2759       && !node->only_called_at_startup)
2760     {
2761        node->only_called_at_startup = true;
2762        if (dump_file)
2763          fprintf (dump_file, "Node %s promoted to only called at startup.\n",
2764                   cgraph_node_name (node));
2765        changed = true;
2766     }
2767   if ((only_called_at_exit && !only_called_at_startup)
2768       && !node->only_called_at_exit)
2769     {
2770        node->only_called_at_exit = true;
2771        if (dump_file)
2772          fprintf (dump_file, "Node %s promoted to only called at exit.\n",
2773                   cgraph_node_name (node));
2774        changed = true;
2775     }
2776   /* These come either from profile or user hints; never update them.  */
2777   if (node->frequency == NODE_FREQUENCY_HOT
2778       || node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2779     return changed;
2780   if (maybe_unlikely_executed)
2781     {
2782       node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
2783       if (dump_file)
2784         fprintf (dump_file, "Node %s promoted to unlikely executed.\n",
2785                  cgraph_node_name (node));
2786       changed = true;
2787     }
2788   else if (maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE)
2789     {
2790       node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
2791       if (dump_file)
2792         fprintf (dump_file, "Node %s promoted to executed once.\n",
2793                  cgraph_node_name (node));
2794       changed = true;
2795     }
2796   return changed;
2797 }
2798
2799 /* Return true when NODE can not return or throw and thus
2800    it is safe to ignore its side effects for IPA analysis.  */
2801
2802 bool
2803 cgraph_node_cannot_return (struct cgraph_node *node)
2804 {
2805   int flags = flags_from_decl_or_type (node->decl);
2806   if (!flag_exceptions)
2807     return (flags & ECF_NORETURN) != 0;
2808   else
2809     return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2810              == (ECF_NORETURN | ECF_NOTHROW));
2811 }
2812
2813 /* Return true when call of E can not lead to return from caller
2814    and thus it is safe to ignore its side effects for IPA analysis
2815    when computing side effects of the caller.
2816    FIXME: We could actually mark all edges that have no reaching
2817    patch to EXIT_BLOCK_PTR or throw to get better results.  */
2818 bool
2819 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2820 {
2821   if (cgraph_node_cannot_return (e->caller))
2822     return true;
2823   if (e->indirect_unknown_callee)
2824     {
2825       int flags = e->indirect_info->ecf_flags;
2826       if (!flag_exceptions)
2827         return (flags & ECF_NORETURN) != 0;
2828       else
2829         return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2830                  == (ECF_NORETURN | ECF_NOTHROW));
2831     }
2832   else
2833     return cgraph_node_cannot_return (e->callee);
2834 }
2835
2836 /* Return true when function NODE can be removed from callgraph
2837    if all direct calls are eliminated.  */
2838
2839 bool
2840 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2841 {
2842   gcc_assert (!node->global.inlined_to);
2843   /* Extern inlines can always go, we will use the external definition.  */
2844   if (DECL_EXTERNAL (node->decl))
2845     return true;
2846   /* When function is needed, we can not remove it.  */
2847   if (node->needed || node->reachable_from_other_partition)
2848     return false;
2849   if (DECL_STATIC_CONSTRUCTOR (node->decl)
2850       || DECL_STATIC_DESTRUCTOR (node->decl))
2851     return false;
2852   /* Only COMDAT functions can be removed if externally visible.  */
2853   if (node->local.externally_visible
2854       && (!DECL_COMDAT (node->decl)
2855           || cgraph_used_from_object_file_p (node)))
2856     return false;
2857   return true;
2858 }
2859
2860 /* Return true when function NODE can be expected to be removed
2861    from program when direct calls in this compilation unit are removed.
2862
2863    As a special case COMDAT functions are
2864    cgraph_can_remove_if_no_direct_calls_p while the are not
2865    cgraph_only_called_directly_p (it is possible they are called from other
2866    unit)
2867
2868    This function behaves as cgraph_only_called_directly_p because eliminating
2869    all uses of COMDAT function does not make it necessarily disappear from
2870    the program unless we are compiling whole program or we do LTO.  In this
2871    case we know we win since dynamic linking will not really discard the
2872    linkonce section.  */
2873
2874 bool
2875 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2876 {
2877   gcc_assert (!node->global.inlined_to);
2878   if (cgraph_used_from_object_file_p (node))
2879     return false;
2880   if (!in_lto_p && !flag_whole_program)
2881     return cgraph_only_called_directly_p (node);
2882   else
2883     {
2884        if (DECL_EXTERNAL (node->decl))
2885          return true;
2886       return cgraph_can_remove_if_no_direct_calls_p (node);
2887     }
2888 }
2889
2890 /* Return true when RESOLUTION indicate that linker will use
2891    the symbol from non-LTO object files.  */
2892
2893 bool
2894 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
2895 {
2896   return (resolution == LDPR_PREVAILING_DEF
2897           || resolution == LDPR_PREEMPTED_REG
2898           || resolution == LDPR_RESOLVED_EXEC
2899           || resolution == LDPR_RESOLVED_DYN);
2900 }
2901
2902 /* Return true when NODE is known to be used from other (non-LTO) object file.
2903    Known only when doing LTO via linker plugin.  */
2904
2905 bool
2906 cgraph_used_from_object_file_p (struct cgraph_node *node)
2907 {
2908   struct cgraph_node *alias;
2909
2910   gcc_assert (!node->global.inlined_to);
2911   if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
2912     return false;
2913   if (resolution_used_from_other_file_p (node->resolution))
2914     return true;
2915   for (alias = node->same_body; alias; alias = alias->next)
2916     if (TREE_PUBLIC (alias->decl)
2917         && resolution_used_from_other_file_p (alias->resolution))
2918       return true;
2919   return false;
2920 }
2921
2922 #include "gt-cgraph.h"