OSDN Git Service

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