OSDN Git Service

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