OSDN Git Service

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