re PR c++/41090 (Using static label reference in c++ class constructor produces wrong...
[platform/upstream/gcc.git] / gcc / cgraph.c
1 /* Callgraph handling code.
2    Copyright (C) 2003-2013 Free Software Foundation, Inc.
3    Contributed by Jan Hubicka
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 /*  This file contains basic routines manipulating call graph
22
23     The call-graph is a data structure designed for intra-procedural optimization.
24     It represents a multi-graph where nodes are functions and edges are call sites. */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "varasm.h"
32 #include "calls.h"
33 #include "print-tree.h"
34 #include "tree-inline.h"
35 #include "langhooks.h"
36 #include "hashtab.h"
37 #include "toplev.h"
38 #include "flags.h"
39 #include "debug.h"
40 #include "target.h"
41 #include "cgraph.h"
42 #include "intl.h"
43 #include "tree-ssa-alias.h"
44 #include "internal-fn.h"
45 #include "tree-eh.h"
46 #include "gimple-expr.h"
47 #include "gimple.h"
48 #include "gimple-iterator.h"
49 #include "timevar.h"
50 #include "dumpfile.h"
51 #include "gimple-ssa.h"
52 #include "cgraph.h"
53 #include "tree-cfg.h"
54 #include "tree-ssa.h"
55 #include "value-prof.h"
56 #include "except.h"
57 #include "diagnostic-core.h"
58 #include "rtl.h"
59 #include "ipa-utils.h"
60 #include "lto-streamer.h"
61 #include "ipa-inline.h"
62 #include "cfgloop.h"
63 #include "gimple-pretty-print.h"
64
65 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this.  */
66 #include "tree-pass.h"
67
68 static void cgraph_node_remove_callers (struct cgraph_node *node);
69 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
70 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
71
72 /* Queue of cgraph nodes scheduled to be lowered.  */
73 symtab_node *x_cgraph_nodes_queue;
74 #define cgraph_nodes_queue ((struct cgraph_node *)x_cgraph_nodes_queue)
75
76 /* Number of nodes in existence.  */
77 int cgraph_n_nodes;
78
79 /* Maximal uid used in cgraph nodes.  */
80 int cgraph_max_uid;
81
82 /* Maximal uid used in cgraph edges.  */
83 int cgraph_edge_max_uid;
84
85 /* Set when whole unit has been analyzed so we can access global info.  */
86 bool cgraph_global_info_ready = false;
87
88 /* What state callgraph is in right now.  */
89 enum cgraph_state cgraph_state = CGRAPH_STATE_PARSING;
90
91 /* Set when the cgraph is fully build and the basic flags are computed.  */
92 bool cgraph_function_flags_ready = false;
93
94 /* List of hooks triggered on cgraph_edge events.  */
95 struct cgraph_edge_hook_list {
96   cgraph_edge_hook hook;
97   void *data;
98   struct cgraph_edge_hook_list *next;
99 };
100
101 /* List of hooks triggered on cgraph_node events.  */
102 struct cgraph_node_hook_list {
103   cgraph_node_hook hook;
104   void *data;
105   struct cgraph_node_hook_list *next;
106 };
107
108 /* List of hooks triggered on events involving two cgraph_edges.  */
109 struct cgraph_2edge_hook_list {
110   cgraph_2edge_hook hook;
111   void *data;
112   struct cgraph_2edge_hook_list *next;
113 };
114
115 /* List of hooks triggered on events involving two cgraph_nodes.  */
116 struct cgraph_2node_hook_list {
117   cgraph_2node_hook hook;
118   void *data;
119   struct cgraph_2node_hook_list *next;
120 };
121
122 /* List of hooks triggered when an edge is removed.  */
123 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
124 /* List of hooks triggered when a node is removed.  */
125 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
126 /* List of hooks triggered when an edge is duplicated.  */
127 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
128 /* List of hooks triggered when a node is duplicated.  */
129 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
130 /* List of hooks triggered when an function is inserted.  */
131 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
132
133 /* Head of a linked list of unused (freed) call graph nodes.
134    Do not GTY((delete)) this list so UIDs gets reliably recycled.  */
135 static GTY(()) struct cgraph_node *free_nodes;
136 /* Head of a linked list of unused (freed) call graph edges.
137    Do not GTY((delete)) this list so UIDs gets reliably recycled.  */
138 static GTY(()) struct cgraph_edge *free_edges;
139
140 /* Did procss_same_body_aliases run?  */
141 bool cpp_implicit_aliases_done;
142
143 /* Map a cgraph_node to cgraph_function_version_info using this htab.
144    The cgraph_function_version_info has a THIS_NODE field that is the
145    corresponding cgraph_node..  */
146
147 static GTY((param_is (struct cgraph_function_version_info))) htab_t
148   cgraph_fnver_htab = NULL;
149
150 /* Hash function for cgraph_fnver_htab.  */
151 static hashval_t
152 cgraph_fnver_htab_hash (const void *ptr)
153 {
154   int uid = ((const struct cgraph_function_version_info *)ptr)->this_node->uid;
155   return (hashval_t)(uid);
156 }
157
158 /* eq function for cgraph_fnver_htab.  */
159 static int
160 cgraph_fnver_htab_eq (const void *p1, const void *p2)
161 {
162   const struct cgraph_function_version_info *n1
163     = (const struct cgraph_function_version_info *)p1;
164   const struct cgraph_function_version_info *n2
165     = (const struct cgraph_function_version_info *)p2;
166
167   return n1->this_node->uid == n2->this_node->uid;
168 }
169
170 /* Mark as GC root all allocated nodes.  */
171 static GTY(()) struct cgraph_function_version_info *
172   version_info_node = NULL;
173
174 /* Get the cgraph_function_version_info node corresponding to node.  */
175 struct cgraph_function_version_info *
176 get_cgraph_node_version (struct cgraph_node *node)
177 {
178   struct cgraph_function_version_info *ret;
179   struct cgraph_function_version_info key;
180   key.this_node = node;
181
182   if (cgraph_fnver_htab == NULL)
183     return NULL;
184
185   ret = (struct cgraph_function_version_info *)
186     htab_find (cgraph_fnver_htab, &key);
187
188   return ret;
189 }
190
191 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
192    corresponding to cgraph_node NODE.  */
193 struct cgraph_function_version_info *
194 insert_new_cgraph_node_version (struct cgraph_node *node)
195 {
196   void **slot;
197   
198   version_info_node = NULL;
199   version_info_node = ggc_alloc_cleared_cgraph_function_version_info ();
200   version_info_node->this_node = node;
201
202   if (cgraph_fnver_htab == NULL)
203     cgraph_fnver_htab = htab_create_ggc (2, cgraph_fnver_htab_hash,
204                                          cgraph_fnver_htab_eq, NULL);
205
206   slot = htab_find_slot (cgraph_fnver_htab, version_info_node, INSERT);
207   gcc_assert (slot != NULL);
208   *slot = version_info_node;
209   return version_info_node;
210 }
211
212 /* Remove the cgraph_function_version_info and cgraph_node for DECL.  This
213    DECL is a duplicate declaration.  */
214 void
215 delete_function_version (tree decl)
216 {
217   struct cgraph_node *decl_node = cgraph_get_node (decl);
218   struct cgraph_function_version_info *decl_v = NULL;
219
220   if (decl_node == NULL)
221     return;
222
223   decl_v = get_cgraph_node_version (decl_node);
224
225   if (decl_v == NULL)
226     return;
227
228   if (decl_v->prev != NULL)
229    decl_v->prev->next = decl_v->next;
230
231   if (decl_v->next != NULL)
232     decl_v->next->prev = decl_v->prev;
233
234   if (cgraph_fnver_htab != NULL)
235     htab_remove_elt (cgraph_fnver_htab, decl_v);
236
237   cgraph_remove_node (decl_node);
238 }
239
240 /* Record that DECL1 and DECL2 are semantically identical function
241    versions.  */
242 void
243 record_function_versions (tree decl1, tree decl2)
244 {
245   struct cgraph_node *decl1_node = cgraph_get_create_node (decl1);
246   struct cgraph_node *decl2_node = cgraph_get_create_node (decl2);
247   struct cgraph_function_version_info *decl1_v = NULL;
248   struct cgraph_function_version_info *decl2_v = NULL;
249   struct cgraph_function_version_info *before;
250   struct cgraph_function_version_info *after;
251
252   gcc_assert (decl1_node != NULL && decl2_node != NULL);
253   decl1_v = get_cgraph_node_version (decl1_node);
254   decl2_v = get_cgraph_node_version (decl2_node);
255
256   if (decl1_v != NULL && decl2_v != NULL)
257     return;
258
259   if (decl1_v == NULL)
260     decl1_v = insert_new_cgraph_node_version (decl1_node);
261
262   if (decl2_v == NULL)
263     decl2_v = insert_new_cgraph_node_version (decl2_node);
264
265   /* Chain decl2_v and decl1_v.  All semantically identical versions
266      will be chained together.  */
267
268   before = decl1_v;
269   after = decl2_v;
270
271   while (before->next != NULL)
272     before = before->next;
273
274   while (after->prev != NULL)
275     after= after->prev;
276
277   before->next = after;
278   after->prev = before;
279 }
280
281 /* Macros to access the next item in the list of free cgraph nodes and
282    edges. */
283 #define NEXT_FREE_NODE(NODE) cgraph ((NODE)->next)
284 #define SET_NEXT_FREE_NODE(NODE,NODE2) ((NODE))->next = NODE2
285 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
286
287 /* Register HOOK to be called with DATA on each removed edge.  */
288 struct cgraph_edge_hook_list *
289 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
290 {
291   struct cgraph_edge_hook_list *entry;
292   struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
293
294   entry = (struct cgraph_edge_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 removing edges.  */
305 void
306 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
307 {
308   struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
309
310   while (*ptr != entry)
311     ptr = &(*ptr)->next;
312   *ptr = entry->next;
313   free (entry);
314 }
315
316 /* Call all edge removal hooks.  */
317 static void
318 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
319 {
320   struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
321   while (entry)
322   {
323     entry->hook (e, entry->data);
324     entry = entry->next;
325   }
326 }
327
328 /* Register HOOK to be called with DATA on each removed node.  */
329 struct cgraph_node_hook_list *
330 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
331 {
332   struct cgraph_node_hook_list *entry;
333   struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
334
335   entry = (struct cgraph_node_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 removing nodes.  */
346 void
347 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
348 {
349   struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
350
351   while (*ptr != entry)
352     ptr = &(*ptr)->next;
353   *ptr = entry->next;
354   free (entry);
355 }
356
357 /* Call all node removal hooks.  */
358 static void
359 cgraph_call_node_removal_hooks (struct cgraph_node *node)
360 {
361   struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
362   while (entry)
363   {
364     entry->hook (node, entry->data);
365     entry = entry->next;
366   }
367 }
368
369 /* Register HOOK to be called with DATA on each inserted node.  */
370 struct cgraph_node_hook_list *
371 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
372 {
373   struct cgraph_node_hook_list *entry;
374   struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
375
376   entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
377   entry->hook = hook;
378   entry->data = data;
379   entry->next = NULL;
380   while (*ptr)
381     ptr = &(*ptr)->next;
382   *ptr = entry;
383   return entry;
384 }
385
386 /* Remove ENTRY from the list of hooks called on inserted nodes.  */
387 void
388 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
389 {
390   struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
391
392   while (*ptr != entry)
393     ptr = &(*ptr)->next;
394   *ptr = entry->next;
395   free (entry);
396 }
397
398 /* Call all node insertion hooks.  */
399 void
400 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
401 {
402   struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
403   while (entry)
404   {
405     entry->hook (node, entry->data);
406     entry = entry->next;
407   }
408 }
409
410 /* Register HOOK to be called with DATA on each duplicated edge.  */
411 struct cgraph_2edge_hook_list *
412 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
413 {
414   struct cgraph_2edge_hook_list *entry;
415   struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
416
417   entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
418   entry->hook = hook;
419   entry->data = data;
420   entry->next = NULL;
421   while (*ptr)
422     ptr = &(*ptr)->next;
423   *ptr = entry;
424   return entry;
425 }
426
427 /* Remove ENTRY from the list of hooks called on duplicating edges.  */
428 void
429 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
430 {
431   struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
432
433   while (*ptr != entry)
434     ptr = &(*ptr)->next;
435   *ptr = entry->next;
436   free (entry);
437 }
438
439 /* Call all edge duplication hooks.  */
440 void
441 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
442                                     struct cgraph_edge *cs2)
443 {
444   struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
445   while (entry)
446   {
447     entry->hook (cs1, cs2, entry->data);
448     entry = entry->next;
449   }
450 }
451
452 /* Register HOOK to be called with DATA on each duplicated node.  */
453 struct cgraph_2node_hook_list *
454 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
455 {
456   struct cgraph_2node_hook_list *entry;
457   struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
458
459   entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
460   entry->hook = hook;
461   entry->data = data;
462   entry->next = NULL;
463   while (*ptr)
464     ptr = &(*ptr)->next;
465   *ptr = entry;
466   return entry;
467 }
468
469 /* Remove ENTRY from the list of hooks called on duplicating nodes.  */
470 void
471 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
472 {
473   struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
474
475   while (*ptr != entry)
476     ptr = &(*ptr)->next;
477   *ptr = entry->next;
478   free (entry);
479 }
480
481 /* Call all node duplication hooks.  */
482 void
483 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
484                                     struct cgraph_node *node2)
485 {
486   struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
487   while (entry)
488   {
489     entry->hook (node1, node2, entry->data);
490     entry = entry->next;
491   }
492 }
493
494 /* Allocate new callgraph node.  */
495
496 static inline struct cgraph_node *
497 cgraph_allocate_node (void)
498 {
499   struct cgraph_node *node;
500
501   if (free_nodes)
502     {
503       node = free_nodes;
504       free_nodes = NEXT_FREE_NODE (node);
505     }
506   else
507     {
508       node = ggc_alloc_cleared_cgraph_node ();
509       node->uid = cgraph_max_uid++;
510     }
511
512   return node;
513 }
514
515 /* Allocate new callgraph node and insert it into basic data structures.  */
516
517 struct cgraph_node *
518 cgraph_create_empty_node (void)
519 {
520   struct cgraph_node *node = cgraph_allocate_node ();
521
522   node->type = SYMTAB_FUNCTION;
523   node->frequency = NODE_FREQUENCY_NORMAL;
524   node->count_materialization_scale = REG_BR_PROB_BASE;
525   cgraph_n_nodes++;
526   return node;
527 }
528
529 /* Return cgraph node assigned to DECL.  Create new one when needed.  */
530
531 struct cgraph_node *
532 cgraph_create_node (tree decl)
533 {
534   struct cgraph_node *node = cgraph_create_empty_node ();
535   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
536
537   node->decl = decl;
538   symtab_register_node (node);
539
540   if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
541     {
542       node->origin = cgraph_get_create_node (DECL_CONTEXT (decl));
543       node->next_nested = node->origin->nested;
544       node->origin->nested = node;
545     }
546   return node;
547 }
548
549 /* Try to find a call graph node for declaration DECL and if it does not exist
550    or if it corresponds to an inline clone, create a new one.  */
551
552 struct cgraph_node *
553 cgraph_get_create_node (tree decl)
554 {
555   struct cgraph_node *first_clone = cgraph_get_node (decl);
556
557   if (first_clone && !first_clone->global.inlined_to)
558     return first_clone;
559
560   struct cgraph_node *node = cgraph_create_node (decl);
561   if (first_clone)
562     {
563       first_clone->clone_of = node;
564       node->clones = first_clone;
565       symtab_prevail_in_asm_name_hash (node);
566       symtab_insert_node_to_hashtable (node);
567       if (dump_file)
568         fprintf (dump_file, "Introduced new external node "
569                  "(%s/%i) and turned into root of the clone tree.\n",
570                  xstrdup (node->name ()), node->order);
571     }
572   else if (dump_file)
573     fprintf (dump_file, "Introduced new external node "
574              "(%s/%i).\n", xstrdup (node->name ()),
575              node->order);
576   return node;
577 }
578
579 /* Mark ALIAS as an alias to DECL.  DECL_NODE is cgraph node representing
580    the function body is associated with (not necessarily cgraph_node (DECL).  */
581
582 struct cgraph_node *
583 cgraph_create_function_alias (tree alias, tree target)
584 {
585   struct cgraph_node *alias_node;
586
587   gcc_assert (TREE_CODE (target) == FUNCTION_DECL
588               || TREE_CODE (target) == IDENTIFIER_NODE);
589   gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
590   alias_node = cgraph_get_create_node (alias);
591   gcc_assert (!alias_node->definition);
592   alias_node->alias_target = target;
593   alias_node->definition = true;
594   alias_node->alias = true;
595   if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
596     alias_node->weakref = true;
597   return alias_node;
598 }
599
600 /* Attempt to mark ALIAS as an alias to DECL.  Return alias node if successful
601    and NULL otherwise.
602    Same body aliases are output whenever the body of DECL is output,
603    and cgraph_get_node (ALIAS) transparently returns cgraph_get_node (DECL).  */
604
605 struct cgraph_node *
606 cgraph_same_body_alias (struct cgraph_node *decl_node ATTRIBUTE_UNUSED, tree alias, tree decl)
607 {
608   struct cgraph_node *n;
609 #ifndef ASM_OUTPUT_DEF
610   /* If aliases aren't supported by the assembler, fail.  */
611   return NULL;
612 #endif
613   /* Langhooks can create same body aliases of symbols not defined.
614      Those are useless. Drop them on the floor.  */
615   if (cgraph_global_info_ready)
616     return NULL;
617
618   n = cgraph_create_function_alias (alias, decl);
619   n->cpp_implicit_alias = true;
620   if (cpp_implicit_aliases_done)
621     symtab_resolve_alias (n,
622                           cgraph_get_node (decl));
623   return n;
624 }
625
626 /* Add thunk alias into callgraph.  The alias declaration is ALIAS and it
627    aliases DECL with an adjustments made into the first parameter.
628    See comments in thunk_adjust for detail on the parameters.  */
629
630 struct cgraph_node *
631 cgraph_add_thunk (struct cgraph_node *decl_node ATTRIBUTE_UNUSED,
632                   tree alias, tree decl ATTRIBUTE_UNUSED,
633                   bool this_adjusting,
634                   HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value,
635                   tree virtual_offset,
636                   tree real_alias)
637 {
638   struct cgraph_node *node;
639
640   node = cgraph_get_node (alias);
641   if (node)
642     {
643       gcc_assert (node->definition);
644       gcc_assert (!node->alias);
645       gcc_assert (!node->thunk.thunk_p);
646       cgraph_remove_node (node);
647     }
648   
649   node = cgraph_create_node (alias);
650   gcc_checking_assert (!virtual_offset
651                        || tree_to_double_int (virtual_offset) ==
652                              double_int::from_shwi (virtual_value));
653   node->thunk.fixed_offset = fixed_offset;
654   node->thunk.this_adjusting = this_adjusting;
655   node->thunk.virtual_value = virtual_value;
656   node->thunk.virtual_offset_p = virtual_offset != NULL;
657   node->thunk.alias = real_alias;
658   node->thunk.thunk_p = true;
659   node->definition = true;
660
661   return node;
662 }
663
664 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
665    Return NULL if there's no such node.  */
666
667 struct cgraph_node *
668 cgraph_node_for_asm (tree asmname)
669 {
670   /* We do not want to look at inline clones.  */
671   for (symtab_node *node = symtab_node_for_asm (asmname);
672        node;
673        node = node->next_sharing_asm_name)
674     {
675       cgraph_node *cn = dyn_cast <cgraph_node> (node);
676       if (cn && !cn->global.inlined_to)
677         return cn;
678     }
679   return NULL;
680 }
681
682 /* Returns a hash value for X (which really is a cgraph_edge).  */
683
684 static hashval_t
685 edge_hash (const void *x)
686 {
687   return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
688 }
689
690 /* Return nonzero if the call_stmt of of cgraph_edge X is stmt *Y.  */
691
692 static int
693 edge_eq (const void *x, const void *y)
694 {
695   return ((const struct cgraph_edge *) x)->call_stmt == y;
696 }
697
698 /* Add call graph edge E to call site hash of its caller.  */
699
700 static inline void
701 cgraph_update_edge_in_call_site_hash (struct cgraph_edge *e)
702 {
703   void **slot;
704   slot = htab_find_slot_with_hash (e->caller->call_site_hash,
705                                    e->call_stmt,
706                                    htab_hash_pointer (e->call_stmt),
707                                    INSERT);
708   *slot = e;
709 }
710
711 /* Add call graph edge E to call site hash of its caller.  */
712
713 static inline void
714 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
715 {
716   void **slot;
717   /* There are two speculative edges for every statement (one direct,
718      one indirect); always hash the direct one.  */
719   if (e->speculative && e->indirect_unknown_callee)
720     return;
721   slot = htab_find_slot_with_hash (e->caller->call_site_hash,
722                                    e->call_stmt,
723                                    htab_hash_pointer (e->call_stmt),
724                                    INSERT);
725   if (*slot)
726     {
727       gcc_assert (((struct cgraph_edge *)*slot)->speculative);
728       if (e->callee)
729         *slot = e;
730       return;
731     }
732   gcc_assert (!*slot || e->speculative);
733   *slot = e;
734 }
735
736 /* Return the callgraph edge representing the GIMPLE_CALL statement
737    CALL_STMT.  */
738
739 struct cgraph_edge *
740 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
741 {
742   struct cgraph_edge *e, *e2;
743   int n = 0;
744
745   if (node->call_site_hash)
746     return (struct cgraph_edge *)
747       htab_find_with_hash (node->call_site_hash, call_stmt,
748                            htab_hash_pointer (call_stmt));
749
750   /* This loop may turn out to be performance problem.  In such case adding
751      hashtables into call nodes with very many edges is probably best
752      solution.  It is not good idea to add pointer into CALL_EXPR itself
753      because we want to make possible having multiple cgraph nodes representing
754      different clones of the same body before the body is actually cloned.  */
755   for (e = node->callees; e; e = e->next_callee)
756     {
757       if (e->call_stmt == call_stmt)
758         break;
759       n++;
760     }
761
762   if (!e)
763     for (e = node->indirect_calls; e; e = e->next_callee)
764       {
765         if (e->call_stmt == call_stmt)
766           break;
767         n++;
768       }
769
770   if (n > 100)
771     {
772       node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
773       for (e2 = node->callees; e2; e2 = e2->next_callee)
774         cgraph_add_edge_to_call_site_hash (e2);
775       for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
776         cgraph_add_edge_to_call_site_hash (e2);
777     }
778
779   return e;
780 }
781
782
783 /* Change field call_stmt of edge E to NEW_STMT.
784    If UPDATE_SPECULATIVE and E is any component of speculative
785    edge, then update all components.  */
786
787 void
788 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt,
789                       bool update_speculative)
790 {
791   tree decl;
792
793   /* Speculative edges has three component, update all of them
794      when asked to.  */
795   if (update_speculative && e->speculative)
796     {
797       struct cgraph_edge *direct, *indirect;
798       struct ipa_ref *ref;
799
800       cgraph_speculative_call_info (e, direct, indirect, ref);
801       cgraph_set_call_stmt (direct, new_stmt, false);
802       cgraph_set_call_stmt (indirect, new_stmt, false);
803       ref->stmt = new_stmt;
804       return;
805     }
806
807   /* Only direct speculative edges go to call_site_hash.  */
808   if (e->caller->call_site_hash
809       && (!e->speculative || !e->indirect_unknown_callee))
810     {
811       htab_remove_elt_with_hash (e->caller->call_site_hash,
812                                  e->call_stmt,
813                                  htab_hash_pointer (e->call_stmt));
814     }
815
816   e->call_stmt = new_stmt;
817   if (e->indirect_unknown_callee
818       && (decl = gimple_call_fndecl (new_stmt)))
819     {
820       /* Constant propagation (and possibly also inlining?) can turn an
821          indirect call into a direct one.  */
822       struct cgraph_node *new_callee = cgraph_get_node (decl);
823
824       gcc_checking_assert (new_callee);
825       e = cgraph_make_edge_direct (e, new_callee);
826     }
827
828   push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
829   e->can_throw_external = stmt_can_throw_external (new_stmt);
830   pop_cfun ();
831   if (e->caller->call_site_hash)
832     cgraph_add_edge_to_call_site_hash (e);
833 }
834
835 /* Allocate a cgraph_edge structure and fill it with data according to the
836    parameters of which only CALLEE can be NULL (when creating an indirect call
837    edge).  */
838
839 static struct cgraph_edge *
840 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
841                        gimple call_stmt, gcov_type count, int freq,
842                        bool indir_unknown_callee)
843 {
844   struct cgraph_edge *edge;
845
846   /* LTO does not actually have access to the call_stmt since these
847      have not been loaded yet.  */
848   if (call_stmt)
849     {
850       /* This is a rather expensive check possibly triggering
851          construction of call stmt hashtable.  */
852 #ifdef ENABLE_CHECKING
853       struct cgraph_edge *e;
854       gcc_checking_assert (!(e=cgraph_edge (caller, call_stmt)) || e->speculative);
855 #endif
856
857       gcc_assert (is_gimple_call (call_stmt));
858     }
859
860   if (free_edges)
861     {
862       edge = free_edges;
863       free_edges = NEXT_FREE_EDGE (edge);
864     }
865   else
866     {
867       edge = ggc_alloc_cgraph_edge ();
868       edge->uid = cgraph_edge_max_uid++;
869     }
870
871   edge->aux = NULL;
872   edge->caller = caller;
873   edge->callee = callee;
874   edge->prev_caller = NULL;
875   edge->next_caller = NULL;
876   edge->prev_callee = NULL;
877   edge->next_callee = NULL;
878   edge->lto_stmt_uid = 0;
879
880   edge->count = count;
881   gcc_assert (count >= 0);
882   edge->frequency = freq;
883   gcc_assert (freq >= 0);
884   gcc_assert (freq <= CGRAPH_FREQ_MAX);
885
886   edge->call_stmt = call_stmt;
887   push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
888   edge->can_throw_external
889     = call_stmt ? stmt_can_throw_external (call_stmt) : false;
890   pop_cfun ();
891   if (call_stmt
892       && callee && callee->decl
893       && !gimple_check_call_matching_types (call_stmt, callee->decl,
894                                             false))
895     edge->call_stmt_cannot_inline_p = true;
896   else
897     edge->call_stmt_cannot_inline_p = false;
898
899   edge->indirect_info = NULL;
900   edge->indirect_inlining_edge = 0;
901   edge->speculative = false;
902   edge->indirect_unknown_callee = indir_unknown_callee;
903   if (call_stmt && caller->call_site_hash)
904     cgraph_add_edge_to_call_site_hash (edge);
905
906   return edge;
907 }
908
909 /* Create edge from CALLER to CALLEE in the cgraph.  */
910
911 struct cgraph_edge *
912 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
913                     gimple call_stmt, gcov_type count, int freq)
914 {
915   struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
916                                                    count, freq, false);
917
918   initialize_inline_failed (edge);
919
920   edge->next_caller = callee->callers;
921   if (callee->callers)
922     callee->callers->prev_caller = edge;
923   edge->next_callee = caller->callees;
924   if (caller->callees)
925     caller->callees->prev_callee = edge;
926   caller->callees = edge;
927   callee->callers = edge;
928
929   return edge;
930 }
931
932 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
933
934 struct cgraph_indirect_call_info *
935 cgraph_allocate_init_indirect_info (void)
936 {
937   struct cgraph_indirect_call_info *ii;
938
939   ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
940   ii->param_index = -1;
941   return ii;
942 }
943
944 /* Create an indirect edge with a yet-undetermined callee where the call
945    statement destination is a formal parameter of the caller with index
946    PARAM_INDEX. */
947
948 struct cgraph_edge *
949 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
950                              int ecf_flags,
951                              gcov_type count, int freq)
952 {
953   struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
954                                                    count, freq, true);
955   tree target;
956
957   initialize_inline_failed (edge);
958
959   edge->indirect_info = cgraph_allocate_init_indirect_info ();
960   edge->indirect_info->ecf_flags = ecf_flags;
961
962   /* Record polymorphic call info.  */
963   if (call_stmt
964       && (target = gimple_call_fn (call_stmt))
965       && virtual_method_call_p (target))
966     {
967       tree otr_type;
968       HOST_WIDE_INT otr_token;
969       ipa_polymorphic_call_context context;
970
971       get_polymorphic_call_info (caller->decl,
972                                  target,
973                                  &otr_type, &otr_token,
974                                  &context);
975
976       /* Only record types can have virtual calls.  */
977       gcc_assert (TREE_CODE (otr_type) == RECORD_TYPE);
978       edge->indirect_info->polymorphic = true;
979       edge->indirect_info->param_index = -1;
980       edge->indirect_info->otr_token = otr_token;
981       edge->indirect_info->otr_type = otr_type;
982       edge->indirect_info->outer_type = context.outer_type;
983       edge->indirect_info->offset = context.offset;
984       edge->indirect_info->maybe_in_construction
985          = context.maybe_in_construction;
986       edge->indirect_info->maybe_derived_type = context.maybe_derived_type;
987     }
988
989   edge->next_callee = caller->indirect_calls;
990   if (caller->indirect_calls)
991     caller->indirect_calls->prev_callee = edge;
992   caller->indirect_calls = edge;
993
994   return edge;
995 }
996
997 /* Remove the edge E from the list of the callers of the callee.  */
998
999 static inline void
1000 cgraph_edge_remove_callee (struct cgraph_edge *e)
1001 {
1002   gcc_assert (!e->indirect_unknown_callee);
1003   if (e->prev_caller)
1004     e->prev_caller->next_caller = e->next_caller;
1005   if (e->next_caller)
1006     e->next_caller->prev_caller = e->prev_caller;
1007   if (!e->prev_caller)
1008     e->callee->callers = e->next_caller;
1009 }
1010
1011 /* Remove the edge E from the list of the callees of the caller.  */
1012
1013 static inline void
1014 cgraph_edge_remove_caller (struct cgraph_edge *e)
1015 {
1016   if (e->prev_callee)
1017     e->prev_callee->next_callee = e->next_callee;
1018   if (e->next_callee)
1019     e->next_callee->prev_callee = e->prev_callee;
1020   if (!e->prev_callee)
1021     {
1022       if (e->indirect_unknown_callee)
1023         e->caller->indirect_calls = e->next_callee;
1024       else
1025         e->caller->callees = e->next_callee;
1026     }
1027   if (e->caller->call_site_hash)
1028     htab_remove_elt_with_hash (e->caller->call_site_hash,
1029                                e->call_stmt,
1030                                htab_hash_pointer (e->call_stmt));
1031 }
1032
1033 /* Put the edge onto the free list.  */
1034
1035 static void
1036 cgraph_free_edge (struct cgraph_edge *e)
1037 {
1038   int uid = e->uid;
1039
1040   if (e->indirect_info)
1041     ggc_free (e->indirect_info);
1042
1043   /* Clear out the edge so we do not dangle pointers.  */
1044   memset (e, 0, sizeof (*e));
1045   e->uid = uid;
1046   NEXT_FREE_EDGE (e) = free_edges;
1047   free_edges = e;
1048 }
1049
1050 /* Remove the edge E in the cgraph.  */
1051
1052 void
1053 cgraph_remove_edge (struct cgraph_edge *e)
1054 {
1055   /* Call all edge removal hooks.  */
1056   cgraph_call_edge_removal_hooks (e);
1057
1058   if (!e->indirect_unknown_callee)
1059     /* Remove from callers list of the callee.  */
1060     cgraph_edge_remove_callee (e);
1061
1062   /* Remove from callees list of the callers.  */
1063   cgraph_edge_remove_caller (e);
1064
1065   /* Put the edge onto the free list.  */
1066   cgraph_free_edge (e);
1067 }
1068
1069 /* Set callee of call graph edge E and add it to the corresponding set of
1070    callers. */
1071
1072 static void
1073 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1074 {
1075   e->prev_caller = NULL;
1076   if (n->callers)
1077     n->callers->prev_caller = e;
1078   e->next_caller = n->callers;
1079   n->callers = e;
1080   e->callee = n;
1081 }
1082
1083 /* Turn edge E into speculative call calling N2. Update
1084    the profile so the direct call is taken COUNT times
1085    with FREQUENCY.  
1086
1087    At clone materialization time, the indirect call E will
1088    be expanded as:
1089
1090    if (call_dest == N2)
1091      n2 ();
1092    else
1093      call call_dest
1094
1095    At this time the function just creates the direct call,
1096    the referencd representing the if conditional and attaches
1097    them all to the orginal indirect call statement.  
1098
1099    Return direct edge created.  */
1100
1101 struct cgraph_edge *
1102 cgraph_turn_edge_to_speculative (struct cgraph_edge *e,
1103                                  struct cgraph_node *n2,
1104                                  gcov_type direct_count,
1105                                  int direct_frequency)
1106 {
1107   struct cgraph_node *n = e->caller;
1108   struct ipa_ref *ref;
1109   struct cgraph_edge *e2;
1110
1111   if (dump_file)
1112     {
1113       fprintf (dump_file, "Indirect call -> speculative call"
1114                " %s/%i => %s/%i\n",
1115                xstrdup (n->name ()), n->order,
1116                xstrdup (n2->name ()), n2->order);
1117     }
1118   e->speculative = true;
1119   e2 = cgraph_create_edge (n, n2, e->call_stmt, direct_count, direct_frequency);
1120   initialize_inline_failed (e2);
1121   e2->speculative = true;
1122   if (TREE_NOTHROW (n2->decl))
1123     e2->can_throw_external = false;
1124   else
1125     e2->can_throw_external = e->can_throw_external;
1126   e2->lto_stmt_uid = e->lto_stmt_uid;
1127   e->count -= e2->count;
1128   e->frequency -= e2->frequency;
1129   cgraph_call_edge_duplication_hooks (e, e2);
1130   ref = ipa_record_reference (n, n2,
1131                               IPA_REF_ADDR, e->call_stmt);
1132   ref->lto_stmt_uid = e->lto_stmt_uid;
1133   ref->speculative = e->speculative;
1134   cgraph_mark_address_taken_node (n2);
1135   return e2;
1136 }
1137
1138 /* Speculative call consist of three components:
1139    1) an indirect edge representing the original call
1140    2) an direct edge representing the new call
1141    3) ADDR_EXPR reference representing the speculative check.
1142    All three components are attached to single statement (the indirect
1143    call) and if one of them exists, all of them must exist.
1144
1145    Given speculative call edge E, return all three components. 
1146  */
1147
1148 void
1149 cgraph_speculative_call_info (struct cgraph_edge *e,
1150                               struct cgraph_edge *&direct,
1151                               struct cgraph_edge *&indirect,
1152                               struct ipa_ref *&reference)
1153 {
1154   struct ipa_ref *ref;
1155   int i;
1156   struct cgraph_edge *e2;
1157
1158   if (!e->indirect_unknown_callee)
1159     for (e2 = e->caller->indirect_calls;
1160          e2->call_stmt != e->call_stmt || e2->lto_stmt_uid != e->lto_stmt_uid;
1161          e2 = e2->next_callee)
1162       ;
1163   else
1164     {
1165       e2 = e;
1166       /* We can take advantage of the call stmt hash.  */
1167       if (e2->call_stmt)
1168         {
1169           e = cgraph_edge (e->caller, e2->call_stmt);
1170           gcc_assert (e->speculative && !e->indirect_unknown_callee);
1171         }
1172       else
1173         for (e = e->caller->callees; 
1174              e2->call_stmt != e->call_stmt
1175              || e2->lto_stmt_uid != e->lto_stmt_uid;
1176              e = e->next_callee)
1177           ;
1178     }
1179   gcc_assert (e->speculative && e2->speculative);
1180   direct = e;
1181   indirect = e2;
1182
1183   reference = NULL;
1184   for (i = 0; ipa_ref_list_reference_iterate (&e->caller->ref_list,
1185                                               i, ref); i++)
1186     if (ref->speculative
1187         && ((ref->stmt && ref->stmt == e->call_stmt)
1188             || (!ref->stmt && ref->lto_stmt_uid == e->lto_stmt_uid)))
1189       {
1190         reference = ref;
1191         break;
1192       }
1193
1194   /* Speculative edge always consist of all three components - direct edge,
1195      indirect and reference.  */
1196   
1197   gcc_assert (e && e2 && ref);
1198 }
1199
1200 /* Redirect callee of E to N.  The function does not update underlying
1201    call expression.  */
1202
1203 void
1204 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1205 {
1206   /* Remove from callers list of the current callee.  */
1207   cgraph_edge_remove_callee (e);
1208
1209   /* Insert to callers list of the new callee.  */
1210   cgraph_set_edge_callee (e, n);
1211 }
1212
1213 /* Speculative call EDGE turned out to be direct call to CALLE_DECL.
1214    Remove the speculative call sequence and return edge representing the call.
1215    It is up to caller to redirect the call as appropriate. */
1216
1217 struct cgraph_edge *
1218 cgraph_resolve_speculation (struct cgraph_edge *edge, tree callee_decl)
1219 {
1220   struct cgraph_edge *e2;
1221   struct ipa_ref *ref;
1222
1223   gcc_assert (edge->speculative);
1224   cgraph_speculative_call_info (edge, e2, edge, ref);
1225   if (!callee_decl
1226       || !symtab_semantically_equivalent_p (ref->referred,
1227                                             symtab_get_node (callee_decl)))
1228     {
1229       if (dump_file)
1230         {
1231           if (callee_decl)
1232             {
1233               fprintf (dump_file, "Speculative indirect call %s/%i => %s/%i has "
1234                        "turned out to have contradicting known target ",
1235                        xstrdup (edge->caller->name ()), edge->caller->order,
1236                        xstrdup (e2->callee->name ()), e2->callee->order);
1237               print_generic_expr (dump_file, callee_decl, 0);
1238               fprintf (dump_file, "\n");
1239             }
1240           else
1241             {
1242               fprintf (dump_file, "Removing speculative call %s/%i => %s/%i\n",
1243                        xstrdup (edge->caller->name ()), edge->caller->order,
1244                        xstrdup (e2->callee->name ()), e2->callee->order);
1245             }
1246         }
1247     }
1248   else
1249     {
1250       struct cgraph_edge *tmp = edge;
1251       if (dump_file)
1252         fprintf (dump_file, "Speculative call turned into direct call.\n");
1253       edge = e2;
1254       e2 = tmp;
1255       /* FIXME:  If EDGE is inlined, we should scale up the frequencies and counts
1256          in the functions inlined through it.  */
1257     }
1258   edge->count += e2->count;
1259   edge->frequency += e2->frequency;
1260   if (edge->frequency > CGRAPH_FREQ_MAX)
1261     edge->frequency = CGRAPH_FREQ_MAX;
1262   edge->speculative = false;
1263   e2->speculative = false;
1264   ipa_remove_reference (ref);
1265   if (e2->indirect_unknown_callee || e2->inline_failed)
1266     cgraph_remove_edge (e2);
1267   else
1268     cgraph_remove_node_and_inline_clones (e2->callee, NULL);
1269   if (edge->caller->call_site_hash)
1270     cgraph_update_edge_in_call_site_hash (edge);
1271   return edge;
1272 }
1273
1274 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1275    CALLEE.  DELTA is an integer constant that is to be added to the this
1276    pointer (first parameter) to compensate for skipping a thunk adjustment.  */
1277
1278 struct cgraph_edge *
1279 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee)
1280 {
1281   gcc_assert (edge->indirect_unknown_callee);
1282
1283   /* If we are redirecting speculative call, make it non-speculative.  */
1284   if (edge->indirect_unknown_callee && edge->speculative)
1285     {
1286       edge = cgraph_resolve_speculation (edge, callee->decl);
1287
1288       /* On successful speculation just return the pre existing direct edge.  */
1289       if (!edge->indirect_unknown_callee)
1290         return edge;
1291     }
1292
1293   edge->indirect_unknown_callee = 0;
1294   ggc_free (edge->indirect_info);
1295   edge->indirect_info = NULL;
1296
1297   /* Get the edge out of the indirect edge list. */
1298   if (edge->prev_callee)
1299     edge->prev_callee->next_callee = edge->next_callee;
1300   if (edge->next_callee)
1301     edge->next_callee->prev_callee = edge->prev_callee;
1302   if (!edge->prev_callee)
1303     edge->caller->indirect_calls = edge->next_callee;
1304
1305   /* Put it into the normal callee list */
1306   edge->prev_callee = NULL;
1307   edge->next_callee = edge->caller->callees;
1308   if (edge->caller->callees)
1309     edge->caller->callees->prev_callee = edge;
1310   edge->caller->callees = edge;
1311
1312   /* Insert to callers list of the new callee.  */
1313   cgraph_set_edge_callee (edge, callee);
1314
1315   if (edge->call_stmt)
1316     edge->call_stmt_cannot_inline_p
1317       = !gimple_check_call_matching_types (edge->call_stmt, callee->decl,
1318                                            false);
1319
1320   /* We need to re-determine the inlining status of the edge.  */
1321   initialize_inline_failed (edge);
1322   return edge;
1323 }
1324
1325 /* If necessary, change the function declaration in the call statement
1326    associated with E so that it corresponds to the edge callee.  */
1327
1328 gimple
1329 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
1330 {
1331   tree decl = gimple_call_fndecl (e->call_stmt);
1332   gimple new_stmt;
1333   gimple_stmt_iterator gsi;
1334 #ifdef ENABLE_CHECKING
1335   struct cgraph_node *node;
1336 #endif
1337
1338   if (e->speculative)
1339     {
1340       struct cgraph_edge *e2;
1341       gimple new_stmt;
1342       struct ipa_ref *ref;
1343
1344       cgraph_speculative_call_info (e, e, e2, ref);
1345       /* If there already is an direct call (i.e. as a result of inliner's
1346          substitution), forget about speculating.  */
1347       if (decl)
1348         e = cgraph_resolve_speculation (e, decl);
1349       /* If types do not match, speculation was likely wrong. 
1350          The direct edge was posisbly redirected to the clone with a different
1351          signature.  We did not update the call statement yet, so compare it 
1352          with the reference that still points to the proper type.  */
1353       else if (!gimple_check_call_matching_types (e->call_stmt,
1354                                                   ref->referred->decl,
1355                                                   true))
1356         {
1357           if (dump_file)
1358             fprintf (dump_file, "Not expanding speculative call of %s/%i -> %s/%i\n"
1359                      "Type mismatch.\n",
1360                      xstrdup (e->caller->name ()),
1361                      e->caller->order,
1362                      xstrdup (e->callee->name ()),
1363                      e->callee->order);
1364           e = cgraph_resolve_speculation (e, NULL);
1365           /* We are producing the final function body and will throw away the
1366              callgraph edges really soon.  Reset the counts/frequencies to
1367              keep verifier happy in the case of roundoff errors.  */
1368           e->count = gimple_bb (e->call_stmt)->count;
1369           e->frequency = compute_call_stmt_bb_frequency
1370                           (e->caller->decl, gimple_bb (e->call_stmt));
1371         }
1372       /* Expand speculation into GIMPLE code.  */
1373       else
1374         {
1375           if (dump_file)
1376             fprintf (dump_file,
1377                      "Expanding speculative call of %s/%i -> %s/%i count:"
1378                      HOST_WIDEST_INT_PRINT_DEC"\n",
1379                      xstrdup (e->caller->name ()),
1380                      e->caller->order,
1381                      xstrdup (e->callee->name ()),
1382                      e->callee->order,
1383                      (HOST_WIDEST_INT)e->count);
1384           gcc_assert (e2->speculative);
1385           push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1386           new_stmt = gimple_ic (e->call_stmt, cgraph (ref->referred),
1387                                 e->count || e2->count
1388                                 ?  RDIV (e->count * REG_BR_PROB_BASE,
1389                                          e->count + e2->count)
1390                                 : e->frequency || e2->frequency
1391                                 ? RDIV (e->frequency * REG_BR_PROB_BASE,
1392                                         e->frequency + e2->frequency)
1393                                 : REG_BR_PROB_BASE / 2,
1394                                 e->count, e->count + e2->count);
1395           e->speculative = false;
1396           cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt,
1397                                                  new_stmt, false);
1398           e->frequency = compute_call_stmt_bb_frequency
1399                            (e->caller->decl, gimple_bb (e->call_stmt));
1400           e2->frequency = compute_call_stmt_bb_frequency
1401                            (e2->caller->decl, gimple_bb (e2->call_stmt));
1402           e2->speculative = false;
1403           ref->speculative = false;
1404           ref->stmt = NULL;
1405           /* Indirect edges are not both in the call site hash.
1406              get it updated.  */
1407           if (e->caller->call_site_hash)
1408             cgraph_update_edge_in_call_site_hash (e2);
1409           pop_cfun ();
1410           /* Continue redirecting E to proper target.  */
1411         }
1412     }
1413
1414   if (e->indirect_unknown_callee
1415       || decl == e->callee->decl)
1416     return e->call_stmt;
1417
1418 #ifdef ENABLE_CHECKING
1419   if (decl)
1420     {
1421       node = cgraph_get_node (decl);
1422       gcc_assert (!node || !node->clone.combined_args_to_skip);
1423     }
1424 #endif
1425
1426   if (cgraph_dump_file)
1427     {
1428       fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
1429                xstrdup (e->caller->name ()), e->caller->order,
1430                xstrdup (e->callee->name ()), e->callee->order);
1431       print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1432       if (e->callee->clone.combined_args_to_skip)
1433         {
1434           fprintf (cgraph_dump_file, " combined args to skip: ");
1435           dump_bitmap (cgraph_dump_file,
1436                        e->callee->clone.combined_args_to_skip);
1437         }
1438     }
1439
1440   if (e->callee->clone.combined_args_to_skip)
1441     {
1442       int lp_nr;
1443
1444       new_stmt
1445         = gimple_call_copy_skip_args (e->call_stmt,
1446                                       e->callee->clone.combined_args_to_skip);
1447       gimple_call_set_fndecl (new_stmt, e->callee->decl);
1448       gimple_call_set_fntype (new_stmt, gimple_call_fntype (e->call_stmt));
1449
1450       if (gimple_vdef (new_stmt)
1451           && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1452         SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1453
1454       gsi = gsi_for_stmt (e->call_stmt);
1455       gsi_replace (&gsi, new_stmt, false);
1456       /* We need to defer cleaning EH info on the new statement to
1457          fixup-cfg.  We may not have dominator information at this point
1458          and thus would end up with unreachable blocks and have no way
1459          to communicate that we need to run CFG cleanup then.  */
1460       lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1461       if (lp_nr != 0)
1462         {
1463           remove_stmt_from_eh_lp (e->call_stmt);
1464           add_stmt_to_eh_lp (new_stmt, lp_nr);
1465         }
1466     }
1467   else
1468     {
1469       new_stmt = e->call_stmt;
1470       gimple_call_set_fndecl (new_stmt, e->callee->decl);
1471       update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1472     }
1473
1474   cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt, new_stmt, false);
1475
1476   if (cgraph_dump_file)
1477     {
1478       fprintf (cgraph_dump_file, "  updated to:");
1479       print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1480     }
1481   return new_stmt;
1482 }
1483
1484 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1485    OLD_STMT changed into NEW_STMT.  OLD_CALL is gimple_call_fndecl
1486    of OLD_STMT if it was previously call statement.
1487    If NEW_STMT is NULL, the call has been dropped without any
1488    replacement.  */
1489
1490 static void
1491 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1492                                         gimple old_stmt, tree old_call,
1493                                         gimple new_stmt)
1494 {
1495   tree new_call = (new_stmt && is_gimple_call (new_stmt))
1496                   ? gimple_call_fndecl (new_stmt) : 0;
1497
1498   /* We are seeing indirect calls, then there is nothing to update.  */
1499   if (!new_call && !old_call)
1500     return;
1501   /* See if we turned indirect call into direct call or folded call to one builtin
1502      into different builtin.  */
1503   if (old_call != new_call)
1504     {
1505       struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1506       struct cgraph_edge *ne = NULL;
1507       gcov_type count;
1508       int frequency;
1509
1510       if (e)
1511         {
1512           /* See if the edge is already there and has the correct callee.  It
1513              might be so because of indirect inlining has already updated
1514              it.  We also might've cloned and redirected the edge.  */
1515           if (new_call && e->callee)
1516             {
1517               struct cgraph_node *callee = e->callee;
1518               while (callee)
1519                 {
1520                   if (callee->decl == new_call
1521                       || callee->former_clone_of == new_call)
1522                     return;
1523                   callee = callee->clone_of;
1524                 }
1525             }
1526
1527           /* Otherwise remove edge and create new one; we can't simply redirect
1528              since function has changed, so inline plan and other information
1529              attached to edge is invalid.  */
1530           count = e->count;
1531           frequency = e->frequency;
1532           cgraph_remove_edge (e);
1533         }
1534       else if (new_call)
1535         {
1536           /* We are seeing new direct call; compute profile info based on BB.  */
1537           basic_block bb = gimple_bb (new_stmt);
1538           count = bb->count;
1539           frequency = compute_call_stmt_bb_frequency (current_function_decl,
1540                                                       bb);
1541         }
1542
1543       if (new_call)
1544         {
1545           ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1546                                    new_stmt, count, frequency);
1547           gcc_assert (ne->inline_failed);
1548         }
1549     }
1550   /* We only updated the call stmt; update pointer in cgraph edge..  */
1551   else if (old_stmt != new_stmt)
1552     cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1553 }
1554
1555 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1556    OLD_STMT changed into NEW_STMT.  OLD_DECL is gimple_call_fndecl
1557    of OLD_STMT before it was updated (updating can happen inplace).  */
1558
1559 void
1560 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1561 {
1562   struct cgraph_node *orig = cgraph_get_node (cfun->decl);
1563   struct cgraph_node *node;
1564
1565   gcc_checking_assert (orig);
1566   cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1567   if (orig->clones)
1568     for (node = orig->clones; node != orig;)
1569       {
1570         cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1571         if (node->clones)
1572           node = node->clones;
1573         else if (node->next_sibling_clone)
1574           node = node->next_sibling_clone;
1575         else
1576           {
1577             while (node != orig && !node->next_sibling_clone)
1578               node = node->clone_of;
1579             if (node != orig)
1580               node = node->next_sibling_clone;
1581           }
1582       }
1583 }
1584
1585
1586 /* Remove all callees from the node.  */
1587
1588 void
1589 cgraph_node_remove_callees (struct cgraph_node *node)
1590 {
1591   struct cgraph_edge *e, *f;
1592
1593   /* It is sufficient to remove the edges from the lists of callers of
1594      the callees.  The callee list of the node can be zapped with one
1595      assignment.  */
1596   for (e = node->callees; e; e = f)
1597     {
1598       f = e->next_callee;
1599       cgraph_call_edge_removal_hooks (e);
1600       if (!e->indirect_unknown_callee)
1601         cgraph_edge_remove_callee (e);
1602       cgraph_free_edge (e);
1603     }
1604   for (e = node->indirect_calls; e; e = f)
1605     {
1606       f = e->next_callee;
1607       cgraph_call_edge_removal_hooks (e);
1608       if (!e->indirect_unknown_callee)
1609         cgraph_edge_remove_callee (e);
1610       cgraph_free_edge (e);
1611     }
1612   node->indirect_calls = NULL;
1613   node->callees = NULL;
1614   if (node->call_site_hash)
1615     {
1616       htab_delete (node->call_site_hash);
1617       node->call_site_hash = NULL;
1618     }
1619 }
1620
1621 /* Remove all callers from the node.  */
1622
1623 static void
1624 cgraph_node_remove_callers (struct cgraph_node *node)
1625 {
1626   struct cgraph_edge *e, *f;
1627
1628   /* It is sufficient to remove the edges from the lists of callees of
1629      the callers.  The caller list of the node can be zapped with one
1630      assignment.  */
1631   for (e = node->callers; e; e = f)
1632     {
1633       f = e->next_caller;
1634       cgraph_call_edge_removal_hooks (e);
1635       cgraph_edge_remove_caller (e);
1636       cgraph_free_edge (e);
1637     }
1638   node->callers = NULL;
1639 }
1640
1641 /* Helper function for cgraph_release_function_body and free_lang_data.
1642    It releases body from function DECL without having to inspect its
1643    possibly non-existent symtab node.  */
1644
1645 void
1646 release_function_body (tree decl)
1647 {
1648   if (DECL_STRUCT_FUNCTION (decl))
1649     {
1650       push_cfun (DECL_STRUCT_FUNCTION (decl));
1651       if (cfun->cfg
1652           && current_loops)
1653         {
1654           cfun->curr_properties &= ~PROP_loops;
1655           loop_optimizer_finalize ();
1656         }
1657       if (cfun->gimple_df)
1658         {
1659           delete_tree_ssa ();
1660           delete_tree_cfg_annotations ();
1661           cfun->eh = NULL;
1662         }
1663       if (cfun->cfg)
1664         {
1665           gcc_assert (dom_computed[0] == DOM_NONE);
1666           gcc_assert (dom_computed[1] == DOM_NONE);
1667           clear_edges ();
1668           cfun->cfg = NULL;
1669         }
1670       if (cfun->value_histograms)
1671         free_histograms ();
1672       pop_cfun ();
1673       gimple_set_body (decl, NULL);
1674       /* Struct function hangs a lot of data that would leak if we didn't
1675          removed all pointers to it.   */
1676       ggc_free (DECL_STRUCT_FUNCTION (decl));
1677       DECL_STRUCT_FUNCTION (decl) = NULL;
1678     }
1679   DECL_SAVED_TREE (decl) = NULL;
1680 }
1681
1682 /* Release memory used to represent body of function NODE.
1683    Use this only for functions that are released before being translated to
1684    target code (i.e. RTL).  Functions that are compiled to RTL and beyond
1685    are free'd in final.c via free_after_compilation().  */
1686
1687 void
1688 cgraph_release_function_body (struct cgraph_node *node)
1689 {
1690   node->ipa_transforms_to_apply.release ();
1691   if (!node->used_as_abstract_origin && cgraph_state != CGRAPH_STATE_PARSING)
1692     {
1693       DECL_RESULT (node->decl) = NULL;
1694       DECL_ARGUMENTS (node->decl) = NULL;
1695     }
1696   /* If the node is abstract and needed, then do not clear DECL_INITIAL
1697      of its associated function function declaration because it's
1698      needed to emit debug info later.  */
1699   if (!node->used_as_abstract_origin && DECL_INITIAL (node->decl))
1700     DECL_INITIAL (node->decl) = error_mark_node;
1701   release_function_body (node->decl);
1702   if (node->lto_file_data)
1703     lto_free_function_in_decl_state_for_node (node);
1704 }
1705
1706 /* Remove the node from cgraph.  */
1707
1708 void
1709 cgraph_remove_node (struct cgraph_node *node)
1710 {
1711   struct cgraph_node *n;
1712   int uid = node->uid;
1713
1714   cgraph_call_node_removal_hooks (node);
1715   cgraph_node_remove_callers (node);
1716   cgraph_node_remove_callees (node);
1717   node->ipa_transforms_to_apply.release ();
1718
1719   /* Incremental inlining access removed nodes stored in the postorder list.
1720      */
1721   node->force_output = false;
1722   node->forced_by_abi = false;
1723   for (n = node->nested; n; n = n->next_nested)
1724     n->origin = NULL;
1725   node->nested = NULL;
1726   if (node->origin)
1727     {
1728       struct cgraph_node **node2 = &node->origin->nested;
1729
1730       while (*node2 != node)
1731         node2 = &(*node2)->next_nested;
1732       *node2 = node->next_nested;
1733     }
1734   symtab_unregister_node (node);
1735   if (node->prev_sibling_clone)
1736     node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1737   else if (node->clone_of)
1738     node->clone_of->clones = node->next_sibling_clone;
1739   if (node->next_sibling_clone)
1740     node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1741   if (node->clones)
1742     {
1743       struct cgraph_node *n, *next;
1744
1745       if (node->clone_of)
1746         {
1747           for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1748             n->clone_of = node->clone_of;
1749           n->clone_of = node->clone_of;
1750           n->next_sibling_clone = node->clone_of->clones;
1751           if (node->clone_of->clones)
1752             node->clone_of->clones->prev_sibling_clone = n;
1753           node->clone_of->clones = node->clones;
1754         }
1755       else
1756         {
1757           /* We are removing node with clones.  This makes clones inconsistent,
1758              but assume they will be removed subsequently and just keep clone
1759              tree intact.  This can happen in unreachable function removal since
1760              we remove unreachable functions in random order, not by bottom-up
1761              walk of clone trees.  */
1762           for (n = node->clones; n; n = next)
1763             {
1764                next = n->next_sibling_clone;
1765                n->next_sibling_clone = NULL;
1766                n->prev_sibling_clone = NULL;
1767                n->clone_of = NULL;
1768             }
1769         }
1770     }
1771
1772   /* While all the clones are removed after being proceeded, the function
1773      itself is kept in the cgraph even after it is compiled.  Check whether
1774      we are done with this body and reclaim it proactively if this is the case.
1775      */
1776   if (cgraph_state != CGRAPH_LTO_STREAMING)
1777     {
1778       n = cgraph_get_node (node->decl);
1779       if (!n
1780           || (!n->clones && !n->clone_of && !n->global.inlined_to
1781               && (cgraph_global_info_ready
1782                   && (TREE_ASM_WRITTEN (n->decl)
1783                       || DECL_EXTERNAL (n->decl)
1784                       || !n->analyzed
1785                       || (!flag_wpa && n->in_other_partition)))))
1786         cgraph_release_function_body (node);
1787     }
1788
1789   node->decl = NULL;
1790   if (node->call_site_hash)
1791     {
1792       htab_delete (node->call_site_hash);
1793       node->call_site_hash = NULL;
1794     }
1795   cgraph_n_nodes--;
1796
1797   /* Clear out the node to NULL all pointers and add the node to the free
1798      list.  */
1799   memset (node, 0, sizeof (*node));
1800   node->type = SYMTAB_FUNCTION;
1801   node->uid = uid;
1802   SET_NEXT_FREE_NODE (node, free_nodes);
1803   free_nodes = node;
1804 }
1805
1806 /* Likewise indicate that a node is having address taken.  */
1807
1808 void
1809 cgraph_mark_address_taken_node (struct cgraph_node *node)
1810 {
1811   /* Indirect inlining can figure out that all uses of the address are
1812      inlined.  */
1813   if (node->global.inlined_to)
1814     {
1815       gcc_assert (cfun->after_inlining);
1816       gcc_assert (node->callers->indirect_inlining_edge);
1817       return;
1818     }
1819   /* FIXME: address_taken flag is used both as a shortcut for testing whether
1820      IPA_REF_ADDR reference exists (and thus it should be set on node
1821      representing alias we take address of) and as a test whether address
1822      of the object was taken (and thus it should be set on node alias is
1823      referring to).  We should remove the first use and the remove the
1824      following set.  */
1825   node->address_taken = 1;
1826   node = cgraph_function_or_thunk_node (node, NULL);
1827   node->address_taken = 1;
1828 }
1829
1830 /* Return local info for the compiled function.  */
1831
1832 struct cgraph_local_info *
1833 cgraph_local_info (tree decl)
1834 {
1835   struct cgraph_node *node;
1836
1837   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1838   node = cgraph_get_node (decl);
1839   if (!node)
1840     return NULL;
1841   return &node->local;
1842 }
1843
1844 /* Return local info for the compiled function.  */
1845
1846 struct cgraph_global_info *
1847 cgraph_global_info (tree decl)
1848 {
1849   struct cgraph_node *node;
1850
1851   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1852   node = cgraph_get_node (decl);
1853   if (!node)
1854     return NULL;
1855   return &node->global;
1856 }
1857
1858 /* Return local info for the compiled function.  */
1859
1860 struct cgraph_rtl_info *
1861 cgraph_rtl_info (tree decl)
1862 {
1863   struct cgraph_node *node;
1864
1865   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1866   node = cgraph_get_node (decl);
1867   if (!node
1868       || (decl != current_function_decl
1869           && !TREE_ASM_WRITTEN (node->decl)))
1870     return NULL;
1871   return &node->rtl;
1872 }
1873
1874 /* Return a string describing the failure REASON.  */
1875
1876 const char*
1877 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1878 {
1879 #undef DEFCIFCODE
1880 #define DEFCIFCODE(code, string)        string,
1881
1882   static const char *cif_string_table[CIF_N_REASONS] = {
1883 #include "cif-code.def"
1884   };
1885
1886   /* Signedness of an enum type is implementation defined, so cast it
1887      to unsigned before testing. */
1888   gcc_assert ((unsigned) reason < CIF_N_REASONS);
1889   return cif_string_table[reason];
1890 }
1891
1892 /* Names used to print out the availability enum.  */
1893 const char * const cgraph_availability_names[] =
1894   {"unset", "not_available", "overwritable", "available", "local"};
1895
1896
1897 /* Dump call graph node NODE to file F.  */
1898
1899 void
1900 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1901 {
1902   struct cgraph_edge *edge;
1903   int indirect_calls_count = 0;
1904
1905   dump_symtab_base (f, node);
1906
1907   if (node->global.inlined_to)
1908     fprintf (f, "  Function %s/%i is inline copy in %s/%i\n",
1909              xstrdup (node->name ()),
1910              node->order,
1911              xstrdup (node->global.inlined_to->name ()),
1912              node->global.inlined_to->order);
1913   if (node->clone_of)
1914     fprintf (f, "  Clone of %s/%i\n",
1915              node->clone_of->asm_name (),
1916              node->clone_of->order);
1917   if (cgraph_function_flags_ready)
1918     fprintf (f, "  Availability: %s\n",
1919              cgraph_availability_names [cgraph_function_body_availability (node)]);
1920
1921   if (node->profile_id)
1922     fprintf (f, "  Profile id: %i\n",
1923              node->profile_id);
1924   fprintf (f, "  First run: %i\n", node->tp_first_run);
1925   fprintf (f, "  Function flags:");
1926   if (node->count)
1927     fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1928              (HOST_WIDEST_INT)node->count);
1929   if (node->origin)
1930     fprintf (f, " nested in: %s", node->origin->asm_name ());
1931   if (gimple_has_body_p (node->decl))
1932     fprintf (f, " body");
1933   if (node->process)
1934     fprintf (f, " process");
1935   if (node->local.local)
1936     fprintf (f, " local");
1937   if (node->local.redefined_extern_inline)
1938     fprintf (f, " redefined_extern_inline");
1939   if (node->only_called_at_startup)
1940     fprintf (f, " only_called_at_startup");
1941   if (node->only_called_at_exit)
1942     fprintf (f, " only_called_at_exit");
1943   if (node->tm_clone)
1944     fprintf (f, " tm_clone");
1945
1946   fprintf (f, "\n");
1947
1948   if (node->thunk.thunk_p)
1949     {
1950       fprintf (f, "  Thunk");
1951       if (node->thunk.alias)
1952         fprintf (f, "  of %s (asm: %s)",
1953                  lang_hooks.decl_printable_name (node->thunk.alias, 2),
1954                  IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1955       fprintf (f, " fixed offset %i virtual value %i has "
1956                "virtual offset %i)\n",
1957                (int)node->thunk.fixed_offset,
1958                (int)node->thunk.virtual_value,
1959                (int)node->thunk.virtual_offset_p);
1960     }
1961   if (node->alias && node->thunk.alias
1962       && DECL_P (node->thunk.alias))
1963     {
1964       fprintf (f, "  Alias of %s",
1965                lang_hooks.decl_printable_name (node->thunk.alias, 2));
1966       if (DECL_ASSEMBLER_NAME_SET_P (node->thunk.alias))
1967         fprintf (f, " (asm: %s)",
1968                  IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1969       fprintf (f, "\n");
1970     }
1971   
1972   fprintf (f, "  Called by: ");
1973
1974   for (edge = node->callers; edge; edge = edge->next_caller)
1975     {
1976       fprintf (f, "%s/%i ", edge->caller->asm_name (),
1977                edge->caller->order);
1978       if (edge->count)
1979         fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1980                  (HOST_WIDEST_INT)edge->count);
1981       if (edge->frequency)
1982         fprintf (f, "(%.2f per call) ",
1983                  edge->frequency / (double)CGRAPH_FREQ_BASE);
1984       if (edge->speculative)
1985         fprintf (f, "(speculative) ");
1986       if (!edge->inline_failed)
1987         fprintf (f, "(inlined) ");
1988       if (edge->indirect_inlining_edge)
1989         fprintf (f, "(indirect_inlining) ");
1990       if (edge->can_throw_external)
1991         fprintf (f, "(can throw external) ");
1992     }
1993
1994   fprintf (f, "\n  Calls: ");
1995   for (edge = node->callees; edge; edge = edge->next_callee)
1996     {
1997       fprintf (f, "%s/%i ", edge->callee->asm_name (),
1998                edge->callee->order);
1999       if (edge->speculative)
2000         fprintf (f, "(speculative) ");
2001       if (!edge->inline_failed)
2002         fprintf (f, "(inlined) ");
2003       if (edge->indirect_inlining_edge)
2004         fprintf (f, "(indirect_inlining) ");
2005       if (edge->count)
2006         fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
2007                  (HOST_WIDEST_INT)edge->count);
2008       if (edge->frequency)
2009         fprintf (f, "(%.2f per call) ",
2010                  edge->frequency / (double)CGRAPH_FREQ_BASE);
2011       if (edge->can_throw_external)
2012         fprintf (f, "(can throw external) ");
2013     }
2014   fprintf (f, "\n");
2015
2016   for (edge = node->indirect_calls; edge; edge = edge->next_callee)
2017     indirect_calls_count++;
2018   if (indirect_calls_count)
2019     fprintf (f, "  Has %i outgoing edges for indirect calls.\n",
2020              indirect_calls_count);
2021 }
2022
2023
2024 /* Dump call graph node NODE to stderr.  */
2025
2026 DEBUG_FUNCTION void
2027 debug_cgraph_node (struct cgraph_node *node)
2028 {
2029   dump_cgraph_node (stderr, node);
2030 }
2031
2032
2033 /* Dump the callgraph to file F.  */
2034
2035 void
2036 dump_cgraph (FILE *f)
2037 {
2038   struct cgraph_node *node;
2039
2040   fprintf (f, "callgraph:\n\n");
2041   FOR_EACH_FUNCTION (node)
2042     dump_cgraph_node (f, node);
2043 }
2044
2045
2046 /* Dump the call graph to stderr.  */
2047
2048 DEBUG_FUNCTION void
2049 debug_cgraph (void)
2050 {
2051   dump_cgraph (stderr);
2052 }
2053
2054 /* Return true when the DECL can possibly be inlined.  */
2055 bool
2056 cgraph_function_possibly_inlined_p (tree decl)
2057 {
2058   if (!cgraph_global_info_ready)
2059     return !DECL_UNINLINABLE (decl);
2060   return DECL_POSSIBLY_INLINED (decl);
2061 }
2062
2063 /* NODE is no longer nested function; update cgraph accordingly.  */
2064 void
2065 cgraph_unnest_node (struct cgraph_node *node)
2066 {
2067   struct cgraph_node **node2 = &node->origin->nested;
2068   gcc_assert (node->origin);
2069
2070   while (*node2 != node)
2071     node2 = &(*node2)->next_nested;
2072   *node2 = node->next_nested;
2073   node->origin = NULL;
2074 }
2075
2076 /* Return function availability.  See cgraph.h for description of individual
2077    return values.  */
2078 enum availability
2079 cgraph_function_body_availability (struct cgraph_node *node)
2080 {
2081   enum availability avail;
2082   if (!node->analyzed)
2083     avail = AVAIL_NOT_AVAILABLE;
2084   else if (node->local.local)
2085     avail = AVAIL_LOCAL;
2086   else if (node->alias && node->weakref)
2087     cgraph_function_or_thunk_node (node, &avail);
2088   else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (node->decl)))
2089     avail = AVAIL_OVERWRITABLE;
2090   else if (!node->externally_visible)
2091     avail = AVAIL_AVAILABLE;
2092   /* Inline functions are safe to be analyzed even if their symbol can
2093      be overwritten at runtime.  It is not meaningful to enforce any sane
2094      behaviour on replacing inline function by different body.  */
2095   else if (DECL_DECLARED_INLINE_P (node->decl))
2096     avail = AVAIL_AVAILABLE;
2097
2098   /* If the function can be overwritten, return OVERWRITABLE.  Take
2099      care at least of two notable extensions - the COMDAT functions
2100      used to share template instantiations in C++ (this is symmetric
2101      to code cp_cannot_inline_tree_fn and probably shall be shared and
2102      the inlinability hooks completely eliminated).
2103
2104      ??? Does the C++ one definition rule allow us to always return
2105      AVAIL_AVAILABLE here?  That would be good reason to preserve this
2106      bit.  */
2107
2108   else if (decl_replaceable_p (node->decl)
2109            && !DECL_EXTERNAL (node->decl))
2110     avail = AVAIL_OVERWRITABLE;
2111   else avail = AVAIL_AVAILABLE;
2112
2113   return avail;
2114 }
2115
2116 /* Worker for cgraph_node_can_be_local_p.  */
2117 static bool
2118 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node,
2119                                  void *data ATTRIBUTE_UNUSED)
2120 {
2121   return !(!node->force_output
2122            && ((DECL_COMDAT (node->decl)
2123                 && !node->forced_by_abi
2124                 && !symtab_used_from_object_file_p (node)
2125                 && !node->same_comdat_group)
2126                || !node->externally_visible));
2127 }
2128
2129 /* Return true if NODE can be made local for API change.
2130    Extern inline functions and C++ COMDAT functions can be made local
2131    at the expense of possible code size growth if function is used in multiple
2132    compilation units.  */
2133 bool
2134 cgraph_node_can_be_local_p (struct cgraph_node *node)
2135 {
2136   return (!node->address_taken
2137           && !cgraph_for_node_and_aliases (node,
2138                                            cgraph_node_cannot_be_local_p_1,
2139                                            NULL, true));
2140 }
2141
2142 /* Call calback on NODE, thunks and aliases associated to NODE. 
2143    When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2144    skipped. */
2145
2146 bool
2147 cgraph_for_node_thunks_and_aliases (struct cgraph_node *node,
2148                                     bool (*callback) (struct cgraph_node *, void *),
2149                                     void *data,
2150                                     bool include_overwritable)
2151 {
2152   struct cgraph_edge *e;
2153   int i;
2154   struct ipa_ref *ref;
2155
2156   if (callback (node, data))
2157     return true;
2158   for (e = node->callers; e; e = e->next_caller)
2159     if (e->caller->thunk.thunk_p
2160         && (include_overwritable
2161             || cgraph_function_body_availability (e->caller) > AVAIL_OVERWRITABLE))
2162       if (cgraph_for_node_thunks_and_aliases (e->caller, callback, data,
2163                                               include_overwritable))
2164         return true;
2165   for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
2166     if (ref->use == IPA_REF_ALIAS)
2167       {
2168         struct cgraph_node *alias = ipa_ref_referring_node (ref);
2169         if (include_overwritable
2170             || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
2171           if (cgraph_for_node_thunks_and_aliases (alias, callback, data,
2172                                                   include_overwritable))
2173             return true;
2174       }
2175   return false;
2176 }
2177
2178 /* Call calback on NODE and aliases associated to NODE. 
2179    When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2180    skipped. */
2181
2182 bool
2183 cgraph_for_node_and_aliases (struct cgraph_node *node,
2184                              bool (*callback) (struct cgraph_node *, void *),
2185                              void *data,
2186                              bool include_overwritable)
2187 {
2188   int i;
2189   struct ipa_ref *ref;
2190
2191   if (callback (node, data))
2192     return true;
2193   for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
2194     if (ref->use == IPA_REF_ALIAS)
2195       {
2196         struct cgraph_node *alias = ipa_ref_referring_node (ref);
2197         if (include_overwritable
2198             || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
2199           if (cgraph_for_node_and_aliases (alias, callback, data,
2200                                            include_overwritable))
2201             return true;
2202       }
2203   return false;
2204 }
2205
2206 /* Worker to bring NODE local.  */
2207
2208 static bool
2209 cgraph_make_node_local_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2210 {
2211   gcc_checking_assert (cgraph_node_can_be_local_p (node));
2212   if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2213     {
2214       symtab_make_decl_local (node->decl);
2215
2216       node->externally_visible = false;
2217       node->forced_by_abi = false;
2218       node->local.local = true;
2219       node->unique_name = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
2220                                   || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
2221       node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2222       gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2223     }
2224   return false;
2225 }
2226
2227 /* Bring NODE local.  */
2228
2229 void
2230 cgraph_make_node_local (struct cgraph_node *node)
2231 {
2232   cgraph_for_node_thunks_and_aliases (node, cgraph_make_node_local_1,
2233                                       NULL, true);
2234 }
2235
2236 /* Worker to set nothrow flag.  */
2237
2238 static bool
2239 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
2240 {
2241   struct cgraph_edge *e;
2242
2243   TREE_NOTHROW (node->decl) = data != NULL;
2244
2245   if (data != NULL)
2246     for (e = node->callers; e; e = e->next_caller)
2247       e->can_throw_external = false;
2248   return false;
2249 }
2250
2251 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2252    if any to NOTHROW.  */
2253
2254 void
2255 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
2256 {
2257   cgraph_for_node_thunks_and_aliases (node, cgraph_set_nothrow_flag_1,
2258                                       (void *)(size_t)nothrow, false);
2259 }
2260
2261 /* Worker to set const flag.  */
2262
2263 static bool
2264 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
2265 {
2266   /* Static constructors and destructors without a side effect can be
2267      optimized out.  */
2268   if (data && !((size_t)data & 2))
2269     {
2270       if (DECL_STATIC_CONSTRUCTOR (node->decl))
2271         DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2272       if (DECL_STATIC_DESTRUCTOR (node->decl))
2273         DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2274     }
2275   TREE_READONLY (node->decl) = data != NULL;
2276   DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2277   return false;
2278 }
2279
2280 /* Set TREE_READONLY on NODE's decl and on aliases of NODE
2281    if any to READONLY.  */
2282
2283 void
2284 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
2285 {
2286   cgraph_for_node_thunks_and_aliases (node, cgraph_set_const_flag_1,
2287                                       (void *)(size_t)(readonly + (int)looping * 2),
2288                                       false);
2289 }
2290
2291 /* Worker to set pure flag.  */
2292
2293 static bool
2294 cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
2295 {
2296   /* Static constructors and destructors without a side effect can be
2297      optimized out.  */
2298   if (data && !((size_t)data & 2))
2299     {
2300       if (DECL_STATIC_CONSTRUCTOR (node->decl))
2301         DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2302       if (DECL_STATIC_DESTRUCTOR (node->decl))
2303         DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2304     }
2305   DECL_PURE_P (node->decl) = data != NULL;
2306   DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2307   return false;
2308 }
2309
2310 /* Set DECL_PURE_P on NODE's decl and on aliases of NODE
2311    if any to PURE.  */
2312
2313 void
2314 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
2315 {
2316   cgraph_for_node_thunks_and_aliases (node, cgraph_set_pure_flag_1,
2317                                       (void *)(size_t)(pure + (int)looping * 2),
2318                                       false);
2319 }
2320
2321 /* Return true when NODE can not return or throw and thus
2322    it is safe to ignore its side effects for IPA analysis.  */
2323
2324 bool
2325 cgraph_node_cannot_return (struct cgraph_node *node)
2326 {
2327   int flags = flags_from_decl_or_type (node->decl);
2328   if (!flag_exceptions)
2329     return (flags & ECF_NORETURN) != 0;
2330   else
2331     return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2332              == (ECF_NORETURN | ECF_NOTHROW));
2333 }
2334
2335 /* Return true when call of E can not lead to return from caller
2336    and thus it is safe to ignore its side effects for IPA analysis
2337    when computing side effects of the caller.
2338    FIXME: We could actually mark all edges that have no reaching
2339    patch to the exit block or throw to get better results.  */
2340 bool
2341 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2342 {
2343   if (cgraph_node_cannot_return (e->caller))
2344     return true;
2345   if (e->indirect_unknown_callee)
2346     {
2347       int flags = e->indirect_info->ecf_flags;
2348       if (!flag_exceptions)
2349         return (flags & ECF_NORETURN) != 0;
2350       else
2351         return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2352                  == (ECF_NORETURN | ECF_NOTHROW));
2353     }
2354   else
2355     return cgraph_node_cannot_return (e->callee);
2356 }
2357
2358 /* Return true when function NODE can be removed from callgraph
2359    if all direct calls are eliminated.  */
2360
2361 bool
2362 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2363 {
2364   gcc_assert (!node->global.inlined_to);
2365   /* Extern inlines can always go, we will use the external definition.  */
2366   if (DECL_EXTERNAL (node->decl))
2367     return true;
2368   /* When function is needed, we can not remove it.  */
2369   if (node->force_output || node->used_from_other_partition)
2370     return false;
2371   if (DECL_STATIC_CONSTRUCTOR (node->decl)
2372       || DECL_STATIC_DESTRUCTOR (node->decl))
2373     return false;
2374   /* Only COMDAT functions can be removed if externally visible.  */
2375   if (node->externally_visible
2376       && (!DECL_COMDAT (node->decl)
2377           || node->forced_by_abi
2378           || symtab_used_from_object_file_p (node)))
2379     return false;
2380   return true;
2381 }
2382
2383 /* Worker for cgraph_can_remove_if_no_direct_calls_p.  */
2384
2385 static bool
2386 nonremovable_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2387 {
2388   return !cgraph_can_remove_if_no_direct_calls_and_refs_p (node);
2389 }
2390
2391 /* Return true when function NODE and its aliases can be removed from callgraph
2392    if all direct calls are eliminated.  */
2393
2394 bool
2395 cgraph_can_remove_if_no_direct_calls_p (struct cgraph_node *node)
2396 {
2397   /* Extern inlines can always go, we will use the external definition.  */
2398   if (DECL_EXTERNAL (node->decl))
2399     return true;
2400   if (node->address_taken)
2401     return false;
2402   return !cgraph_for_node_and_aliases (node, nonremovable_p, NULL, true);
2403 }
2404
2405 /* Worker for cgraph_can_remove_if_no_direct_calls_p.  */
2406
2407 static bool
2408 used_from_object_file_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2409 {
2410   return symtab_used_from_object_file_p (node);
2411 }
2412
2413 /* Return true when function NODE can be expected to be removed
2414    from program when direct calls in this compilation unit are removed.
2415
2416    As a special case COMDAT functions are
2417    cgraph_can_remove_if_no_direct_calls_p while the are not
2418    cgraph_only_called_directly_p (it is possible they are called from other
2419    unit)
2420
2421    This function behaves as cgraph_only_called_directly_p because eliminating
2422    all uses of COMDAT function does not make it necessarily disappear from
2423    the program unless we are compiling whole program or we do LTO.  In this
2424    case we know we win since dynamic linking will not really discard the
2425    linkonce section.  */
2426
2427 bool
2428 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2429 {
2430   gcc_assert (!node->global.inlined_to);
2431   if (cgraph_for_node_and_aliases (node, used_from_object_file_p, NULL, true))
2432     return false;
2433   if (!in_lto_p && !flag_whole_program)
2434     return cgraph_only_called_directly_p (node);
2435   else
2436     {
2437        if (DECL_EXTERNAL (node->decl))
2438          return true;
2439       return cgraph_can_remove_if_no_direct_calls_p (node);
2440     }
2441 }
2442
2443
2444 /* Worker for cgraph_only_called_directly_p.  */
2445
2446 static bool
2447 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2448 {
2449   return !cgraph_only_called_directly_or_aliased_p (node);
2450 }
2451
2452 /* Return true when function NODE and all its aliases are only called
2453    directly.
2454    i.e. it is not externally visible, address was not taken and
2455    it is not used in any other non-standard way.  */
2456
2457 bool
2458 cgraph_only_called_directly_p (struct cgraph_node *node)
2459 {
2460   gcc_assert (cgraph_function_or_thunk_node (node, NULL) == node);
2461   return !cgraph_for_node_and_aliases (node, cgraph_not_only_called_directly_p_1,
2462                                        NULL, true);
2463 }
2464
2465
2466 /* Collect all callers of NODE.  Worker for collect_callers_of_node.  */
2467
2468 static bool
2469 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
2470 {
2471   vec<cgraph_edge_p> *redirect_callers = (vec<cgraph_edge_p> *)data;
2472   struct cgraph_edge *cs;
2473   enum availability avail;
2474   cgraph_function_or_thunk_node (node, &avail);
2475
2476   if (avail > AVAIL_OVERWRITABLE)
2477     for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2478       if (!cs->indirect_inlining_edge)
2479         redirect_callers->safe_push (cs);
2480   return false;
2481 }
2482
2483 /* Collect all callers of NODE and its aliases that are known to lead to NODE
2484    (i.e. are not overwritable).  */
2485
2486 vec<cgraph_edge_p> 
2487 collect_callers_of_node (struct cgraph_node *node)
2488 {
2489   vec<cgraph_edge_p> redirect_callers = vNULL;
2490   cgraph_for_node_and_aliases (node, collect_callers_of_node_1,
2491                                &redirect_callers, false);
2492   return redirect_callers;
2493 }
2494
2495 /* Return TRUE if NODE2 is equivalent to NODE or its clone.  */
2496 static bool
2497 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
2498 {
2499   node = cgraph_function_or_thunk_node (node, NULL);
2500   node2 = cgraph_function_or_thunk_node (node2, NULL);
2501   while (node != node2 && node2)
2502     node2 = node2->clone_of;
2503   return node2 != NULL;
2504 }
2505
2506 /* Verify edge E count and frequency.  */
2507
2508 static bool
2509 verify_edge_count_and_frequency (struct cgraph_edge *e)
2510 {
2511   bool error_found = false;
2512   if (e->count < 0)
2513     {
2514       error ("caller edge count is negative");
2515       error_found = true;
2516     }
2517   if (e->frequency < 0)
2518     {
2519       error ("caller edge frequency is negative");
2520       error_found = true;
2521     }
2522   if (e->frequency > CGRAPH_FREQ_MAX)
2523     {
2524       error ("caller edge frequency is too large");
2525       error_found = true;
2526     }
2527   if (gimple_has_body_p (e->caller->decl)
2528       && !e->caller->global.inlined_to
2529       && !e->speculative
2530       /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2531          Remove this once edges are actually removed from the function at that time.  */
2532       && (e->frequency
2533           || (inline_edge_summary_vec.exists ()
2534               && ((inline_edge_summary_vec.length () <= (unsigned) e->uid)
2535                   || !inline_edge_summary (e)->predicate)))
2536       && (e->frequency
2537           != compute_call_stmt_bb_frequency (e->caller->decl,
2538                                              gimple_bb (e->call_stmt))))
2539     {
2540       error ("caller edge frequency %i does not match BB frequency %i",
2541              e->frequency,
2542              compute_call_stmt_bb_frequency (e->caller->decl,
2543                                              gimple_bb (e->call_stmt)));
2544       error_found = true;
2545     }
2546   return error_found;
2547 }
2548
2549 /* Switch to THIS_CFUN if needed and print STMT to stderr.  */
2550 static void
2551 cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
2552 {
2553   bool fndecl_was_null = false;
2554   /* debug_gimple_stmt needs correct cfun */
2555   if (cfun != this_cfun)
2556     set_cfun (this_cfun);
2557   /* ...and an actual current_function_decl */
2558   if (!current_function_decl)
2559     {
2560       current_function_decl = this_cfun->decl;
2561       fndecl_was_null = true;
2562     }
2563   debug_gimple_stmt (stmt);
2564   if (fndecl_was_null)
2565     current_function_decl = NULL;
2566 }
2567
2568 /* Verify that call graph edge E corresponds to DECL from the associated
2569    statement.  Return true if the verification should fail.  */
2570
2571 static bool
2572 verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl)
2573 {
2574   struct cgraph_node *node;
2575
2576   if (!decl || e->callee->global.inlined_to)
2577     return false;
2578   if (cgraph_state == CGRAPH_LTO_STREAMING)
2579     return false;
2580   node = cgraph_get_node (decl);
2581
2582   /* We do not know if a node from a different partition is an alias or what it
2583      aliases and therefore cannot do the former_clone_of check reliably.  */
2584   if (!node || node->in_other_partition || e->callee->in_other_partition)
2585     return false;
2586   node = cgraph_function_or_thunk_node (node, NULL);
2587
2588   if (e->callee->former_clone_of != node->decl
2589       /* IPA-CP sometimes redirect edge to clone and then back to the former
2590          function.  This ping-pong has to go, eventually.  */
2591       && (node != cgraph_function_or_thunk_node (e->callee, NULL))
2592       && !clone_of_p (cgraph_function_or_thunk_node (node, NULL), e->callee))
2593     return true;
2594   else
2595     return false;
2596 }
2597
2598 /* Verify cgraph nodes of given cgraph node.  */
2599 DEBUG_FUNCTION void
2600 verify_cgraph_node (struct cgraph_node *node)
2601 {
2602   struct cgraph_edge *e;
2603   struct function *this_cfun = DECL_STRUCT_FUNCTION (node->decl);
2604   basic_block this_block;
2605   gimple_stmt_iterator gsi;
2606   bool error_found = false;
2607
2608   if (seen_error ())
2609     return;
2610
2611   timevar_push (TV_CGRAPH_VERIFY);
2612   error_found |= verify_symtab_base (node);
2613   for (e = node->callees; e; e = e->next_callee)
2614     if (e->aux)
2615       {
2616         error ("aux field set for edge %s->%s",
2617                identifier_to_locale (e->caller->name ()),
2618                identifier_to_locale (e->callee->name ()));
2619         error_found = true;
2620       }
2621   if (node->count < 0)
2622     {
2623       error ("execution count is negative");
2624       error_found = true;
2625     }
2626   if (node->global.inlined_to && node->same_comdat_group)
2627     {
2628       error ("inline clone in same comdat group list");
2629       error_found = true;
2630     }
2631   if (!node->definition && !node->in_other_partition && node->local.local)
2632     {
2633       error ("local symbols must be defined");
2634       error_found = true;
2635     }
2636   if (node->global.inlined_to && node->externally_visible)
2637     {
2638       error ("externally visible inline clone");
2639       error_found = true;
2640     }
2641   if (node->global.inlined_to && node->address_taken)
2642     {
2643       error ("inline clone with address taken");
2644       error_found = true;
2645     }
2646   if (node->global.inlined_to && node->force_output)
2647     {
2648       error ("inline clone is forced to output");
2649       error_found = true;
2650     }
2651   for (e = node->indirect_calls; e; e = e->next_callee)
2652     {
2653       if (e->aux)
2654         {
2655           error ("aux field set for indirect edge from %s",
2656                  identifier_to_locale (e->caller->name ()));
2657           error_found = true;
2658         }
2659       if (!e->indirect_unknown_callee
2660           || !e->indirect_info)
2661         {
2662           error ("An indirect edge from %s is not marked as indirect or has "
2663                  "associated indirect_info, the corresponding statement is: ",
2664                  identifier_to_locale (e->caller->name ()));
2665           cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2666           error_found = true;
2667         }
2668     }
2669   bool check_comdat = symtab_comdat_local_p (node);
2670   for (e = node->callers; e; e = e->next_caller)
2671     {
2672       if (verify_edge_count_and_frequency (e))
2673         error_found = true;
2674       if (check_comdat
2675           && !symtab_in_same_comdat_p (e->caller, node))
2676         {
2677           error ("comdat-local function called by %s outside its comdat",
2678                  identifier_to_locale (e->caller->name ()));
2679           error_found = true;
2680         }
2681       if (!e->inline_failed)
2682         {
2683           if (node->global.inlined_to
2684               != (e->caller->global.inlined_to
2685                   ? e->caller->global.inlined_to : e->caller))
2686             {
2687               error ("inlined_to pointer is wrong");
2688               error_found = true;
2689             }
2690           if (node->callers->next_caller)
2691             {
2692               error ("multiple inline callers");
2693               error_found = true;
2694             }
2695         }
2696       else
2697         if (node->global.inlined_to)
2698           {
2699             error ("inlined_to pointer set for noninline callers");
2700             error_found = true;
2701           }
2702     }
2703   for (e = node->indirect_calls; e; e = e->next_callee)
2704     if (verify_edge_count_and_frequency (e))
2705       error_found = true;
2706   if (!node->callers && node->global.inlined_to)
2707     {
2708       error ("inlined_to pointer is set but no predecessors found");
2709       error_found = true;
2710     }
2711   if (node->global.inlined_to == node)
2712     {
2713       error ("inlined_to pointer refers to itself");
2714       error_found = true;
2715     }
2716
2717   if (node->clone_of)
2718     {
2719       struct cgraph_node *n;
2720       for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
2721         if (n == node)
2722           break;
2723       if (!n)
2724         {
2725           error ("node has wrong clone_of");
2726           error_found = true;
2727         }
2728     }
2729   if (node->clones)
2730     {
2731       struct cgraph_node *n;
2732       for (n = node->clones; n; n = n->next_sibling_clone)
2733         if (n->clone_of != node)
2734           break;
2735       if (n)
2736         {
2737           error ("node has wrong clone list");
2738           error_found = true;
2739         }
2740     }
2741   if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
2742     {
2743        error ("node is in clone list but it is not clone");
2744        error_found = true;
2745     }
2746   if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
2747     {
2748       error ("node has wrong prev_clone pointer");
2749       error_found = true;
2750     }
2751   if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
2752     {
2753       error ("double linked list of clones corrupted");
2754       error_found = true;
2755     }
2756
2757   if (node->analyzed && node->alias)
2758     {
2759       bool ref_found = false;
2760       int i;
2761       struct ipa_ref *ref;
2762
2763       if (node->callees)
2764         {
2765           error ("Alias has call edges");
2766           error_found = true;
2767         }
2768       for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list,
2769                                                   i, ref); i++)
2770         if (ref->use != IPA_REF_ALIAS)
2771           {
2772             error ("Alias has non-alias reference");
2773             error_found = true;
2774           }
2775         else if (ref_found)
2776           {
2777             error ("Alias has more than one alias reference");
2778             error_found = true;
2779           }
2780         else
2781           ref_found = true;
2782         if (!ref_found)
2783           {
2784             error ("Analyzed alias has no reference");
2785             error_found = true;
2786           }
2787     }
2788   if (node->analyzed && node->thunk.thunk_p)
2789     {
2790       if (!node->callees)
2791         {
2792           error ("No edge out of thunk node");
2793           error_found = true;
2794         }
2795       else if (node->callees->next_callee)
2796         {
2797           error ("More than one edge out of thunk node");
2798           error_found = true;
2799         }
2800       if (gimple_has_body_p (node->decl))
2801         {
2802           error ("Thunk is not supposed to have body");
2803           error_found = true;
2804         }
2805     }
2806   else if (node->analyzed && gimple_has_body_p (node->decl)
2807            && !TREE_ASM_WRITTEN (node->decl)
2808            && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to)
2809            && !flag_wpa)
2810     {
2811       if (this_cfun->cfg)
2812         {
2813           pointer_set_t *stmts = pointer_set_create ();
2814           int i;
2815           struct ipa_ref *ref;
2816
2817           /* Reach the trees by walking over the CFG, and note the
2818              enclosing basic-blocks in the call edges.  */
2819           FOR_EACH_BB_FN (this_block, this_cfun)
2820             {
2821               for (gsi = gsi_start_phis (this_block);
2822                    !gsi_end_p (gsi); gsi_next (&gsi))
2823                 pointer_set_insert (stmts, gsi_stmt (gsi));
2824               for (gsi = gsi_start_bb (this_block);
2825                    !gsi_end_p (gsi);
2826                    gsi_next (&gsi))
2827                 {
2828                   gimple stmt = gsi_stmt (gsi);
2829                   pointer_set_insert (stmts, stmt);
2830                   if (is_gimple_call (stmt))
2831                     {
2832                       struct cgraph_edge *e = cgraph_edge (node, stmt);
2833                       tree decl = gimple_call_fndecl (stmt);
2834                       if (e)
2835                         {
2836                           if (e->aux)
2837                             {
2838                               error ("shared call_stmt:");
2839                               cgraph_debug_gimple_stmt (this_cfun, stmt);
2840                               error_found = true;
2841                             }
2842                           if (!e->indirect_unknown_callee)
2843                             {
2844                               if (verify_edge_corresponds_to_fndecl (e, decl))
2845                                 {
2846                                   error ("edge points to wrong declaration:");
2847                                   debug_tree (e->callee->decl);
2848                                   fprintf (stderr," Instead of:");
2849                                   debug_tree (decl);
2850                                   error_found = true;
2851                                 }
2852                             }
2853                           else if (decl)
2854                             {
2855                               error ("an indirect edge with unknown callee "
2856                                      "corresponding to a call_stmt with "
2857                                      "a known declaration:");
2858                               error_found = true;
2859                               cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2860                             }
2861                           e->aux = (void *)1;
2862                         }
2863                       else if (decl)
2864                         {
2865                           error ("missing callgraph edge for call stmt:");
2866                           cgraph_debug_gimple_stmt (this_cfun, stmt);
2867                           error_found = true;
2868                         }
2869                     }
2870                 }
2871               }
2872             for (i = 0;
2873                  ipa_ref_list_reference_iterate (&node->ref_list, i, ref);
2874                  i++)
2875               if (ref->stmt && !pointer_set_contains (stmts, ref->stmt))
2876                 {
2877                   error ("reference to dead statement");
2878                   cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
2879                   error_found = true;
2880                 }
2881             pointer_set_destroy (stmts);
2882         }
2883       else
2884         /* No CFG available?!  */
2885         gcc_unreachable ();
2886
2887       for (e = node->callees; e; e = e->next_callee)
2888         {
2889           if (!e->aux)
2890             {
2891               error ("edge %s->%s has no corresponding call_stmt",
2892                      identifier_to_locale (e->caller->name ()),
2893                      identifier_to_locale (e->callee->name ()));
2894               cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2895               error_found = true;
2896             }
2897           e->aux = 0;
2898         }
2899       for (e = node->indirect_calls; e; e = e->next_callee)
2900         {
2901           if (!e->aux && !e->speculative)
2902             {
2903               error ("an indirect edge from %s has no corresponding call_stmt",
2904                      identifier_to_locale (e->caller->name ()));
2905               cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2906               error_found = true;
2907             }
2908           e->aux = 0;
2909         }
2910     }
2911   if (error_found)
2912     {
2913       dump_cgraph_node (stderr, node);
2914       internal_error ("verify_cgraph_node failed");
2915     }
2916   timevar_pop (TV_CGRAPH_VERIFY);
2917 }
2918
2919 /* Verify whole cgraph structure.  */
2920 DEBUG_FUNCTION void
2921 verify_cgraph (void)
2922 {
2923   struct cgraph_node *node;
2924
2925   if (seen_error ())
2926     return;
2927
2928   FOR_EACH_FUNCTION (node)
2929     verify_cgraph_node (node);
2930 }
2931
2932 /* Given NODE, walk the alias chain to return the function NODE is alias of.
2933    Walk through thunk, too.
2934    When AVAILABILITY is non-NULL, get minimal availability in the chain.  */
2935
2936 struct cgraph_node *
2937 cgraph_function_node (struct cgraph_node *node, enum availability *availability)
2938 {
2939   do
2940     {
2941       node = cgraph_function_or_thunk_node (node, availability);
2942       if (node->thunk.thunk_p)
2943         {
2944           node = node->callees->callee;
2945           if (availability)
2946             {
2947               enum availability a;
2948               a = cgraph_function_body_availability (node);
2949               if (a < *availability)
2950                 *availability = a;
2951             }
2952           node = cgraph_function_or_thunk_node (node, availability);
2953         }
2954     } while (node && node->thunk.thunk_p);
2955   return node;
2956 }
2957
2958 /* When doing LTO, read NODE's body from disk if it is not already present.  */
2959
2960 bool
2961 cgraph_get_body (struct cgraph_node *node)
2962 {
2963   struct lto_file_decl_data *file_data;
2964   const char *data, *name;
2965   size_t len;
2966   tree decl = node->decl;
2967
2968   if (DECL_RESULT (decl))
2969     return false;
2970
2971   gcc_assert (in_lto_p);
2972
2973   file_data = node->lto_file_data;
2974   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2975
2976   /* We may have renamed the declaration, e.g., a static function.  */
2977   name = lto_get_decl_name_mapping (file_data, name);
2978
2979   data = lto_get_section_data (file_data, LTO_section_function_body,
2980                                name, &len);
2981   if (!data)
2982     {
2983         dump_cgraph_node (stderr, node);
2984     fatal_error ("%s: section %s is missing",
2985                  file_data->file_name,
2986                  name);
2987     }
2988
2989   gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
2990
2991   lto_input_function_body (file_data, node, data);
2992   lto_stats.num_function_bodies++;
2993   lto_free_section_data (file_data, LTO_section_function_body, name,
2994                          data, len);
2995   lto_free_function_in_decl_state_for_node (node);
2996   return true;
2997 }
2998
2999 /* Verify if the type of the argument matches that of the function
3000    declaration.  If we cannot verify this or there is a mismatch,
3001    return false.  */
3002
3003 static bool
3004 gimple_check_call_args (gimple stmt, tree fndecl, bool args_count_match)
3005 {
3006   tree parms, p;
3007   unsigned int i, nargs;
3008
3009   /* Calls to internal functions always match their signature.  */
3010   if (gimple_call_internal_p (stmt))
3011     return true;
3012
3013   nargs = gimple_call_num_args (stmt);
3014
3015   /* Get argument types for verification.  */
3016   if (fndecl)
3017     parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3018   else
3019     parms = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
3020
3021   /* Verify if the type of the argument matches that of the function
3022      declaration.  If we cannot verify this or there is a mismatch,
3023      return false.  */
3024   if (fndecl && DECL_ARGUMENTS (fndecl))
3025     {
3026       for (i = 0, p = DECL_ARGUMENTS (fndecl);
3027            i < nargs;
3028            i++, p = DECL_CHAIN (p))
3029         {
3030           tree arg;
3031           /* We cannot distinguish a varargs function from the case
3032              of excess parameters, still deferring the inlining decision
3033              to the callee is possible.  */
3034           if (!p)
3035             break;
3036           arg = gimple_call_arg (stmt, i);
3037           if (p == error_mark_node
3038               || arg == error_mark_node
3039               || (!types_compatible_p (DECL_ARG_TYPE (p), TREE_TYPE (arg))
3040                   && !fold_convertible_p (DECL_ARG_TYPE (p), arg)))
3041             return false;
3042         }
3043       if (args_count_match && p)
3044         return false;
3045     }
3046   else if (parms)
3047     {
3048       for (i = 0, p = parms; i < nargs; i++, p = TREE_CHAIN (p))
3049         {
3050           tree arg;
3051           /* If this is a varargs function defer inlining decision
3052              to callee.  */
3053           if (!p)
3054             break;
3055           arg = gimple_call_arg (stmt, i);
3056           if (TREE_VALUE (p) == error_mark_node
3057               || arg == error_mark_node
3058               || TREE_CODE (TREE_VALUE (p)) == VOID_TYPE
3059               || (!types_compatible_p (TREE_VALUE (p), TREE_TYPE (arg))
3060                   && !fold_convertible_p (TREE_VALUE (p), arg)))
3061             return false;
3062         }
3063     }
3064   else
3065     {
3066       if (nargs != 0)
3067         return false;
3068     }
3069   return true;
3070 }
3071
3072 /* Verify if the type of the argument and lhs of CALL_STMT matches
3073    that of the function declaration CALLEE. If ARGS_COUNT_MATCH is
3074    true, the arg count needs to be the same.
3075    If we cannot verify this or there is a mismatch, return false.  */
3076
3077 bool
3078 gimple_check_call_matching_types (gimple call_stmt, tree callee,
3079                                   bool args_count_match)
3080 {
3081   tree lhs;
3082
3083   if ((DECL_RESULT (callee)
3084        && !DECL_BY_REFERENCE (DECL_RESULT (callee))
3085        && (lhs = gimple_call_lhs (call_stmt)) != NULL_TREE
3086        && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee)),
3087                                       TREE_TYPE (lhs))
3088        && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee)), lhs))
3089       || !gimple_check_call_args (call_stmt, callee, args_count_match))
3090     return false;
3091   return true;
3092 }
3093
3094 #include "gt-cgraph.h"