OSDN Git Service

* tree.c (build_function_type_skip_args, build_function_decl_skip_args):
[pf3gnuchains/gcc-fork.git] / gcc / cgraphunit.c
1 /* Callgraph based interprocedural optimizations.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
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 module implements main driver of compilation process as well as
23    few basic interprocedural optimizers.
24
25    The main scope of this file is to act as an interface in between
26    tree based frontends and the backend (and middle end)
27
28    The front-end is supposed to use following functionality:
29
30     - cgraph_finalize_function
31
32       This function is called once front-end has parsed whole body of function
33       and it is certain that the function body nor the declaration will change.
34
35       (There is one exception needed for implementing GCC extern inline
36         function.)
37
38     - varpool_finalize_variable
39
40       This function has same behavior as the above but is used for static
41       variables.
42
43     - cgraph_finalize_compilation_unit
44
45       This function is called once (source level) compilation unit is finalized
46       and it will no longer change.
47
48       In the the call-graph construction and local function
49       analysis takes place here.  Bodies of unreachable functions are released
50       to conserve memory usage.
51
52       The function can be called multiple times when multiple source level
53       compilation units are combined (such as in C frontend)
54
55     - cgraph_optimize
56
57       In this unit-at-a-time compilation the intra procedural analysis takes
58       place here.  In particular the static functions whose address is never
59       taken are marked as local.  Backend can then use this information to
60       modify calling conventions, do better inlining or similar optimizations.
61
62     - cgraph_mark_needed_node
63     - varpool_mark_needed_node
64
65       When function or variable is referenced by some hidden way the call-graph
66       data structure must be updated accordingly by this function.
67       There should be little need to call this function and all the references
68       should be made explicit to cgraph code.  At present these functions are
69       used by C++ frontend to explicitly mark the keyed methods.
70
71     - analyze_expr callback
72
73       This function is responsible for lowering tree nodes not understood by
74       generic code into understandable ones or alternatively marking
75       callgraph and varpool nodes referenced by the as needed.
76
77       ??? On the tree-ssa genericizing should take place here and we will avoid
78       need for these hooks (replacing them by genericizing hook)
79
80         Analyzing of all functions is deferred
81         to cgraph_finalize_compilation_unit and expansion into cgraph_optimize.
82
83         In cgraph_finalize_compilation_unit the reachable functions are
84         analyzed.  During analysis the call-graph edges from reachable
85         functions are constructed and their destinations are marked as
86         reachable.  References to functions and variables are discovered too
87         and variables found to be needed output to the assembly file.  Via
88         mark_referenced call in assemble_variable functions referenced by
89         static variables are noticed too.
90
91         The intra-procedural information is produced and its existence
92         indicated by global_info_ready.  Once this flag is set it is impossible
93         to change function from !reachable to reachable and thus
94         assemble_variable no longer call mark_referenced.
95
96         Finally the call-graph is topologically sorted and all reachable functions
97         that has not been completely inlined or are not external are output.
98
99         ??? It is possible that reference to function or variable is optimized
100         out.  We can not deal with this nicely because topological order is not
101         suitable for it.  For tree-ssa we may consider another pass doing
102         optimization and re-discovering reachable functions.
103
104         ??? Reorganize code so variables are output very last and only if they
105         really has been referenced by produced code, so we catch more cases
106         where reference has been optimized out.  */
107
108
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "tm.h"
113 #include "tree.h"
114 #include "rtl.h"
115 #include "tree-flow.h"
116 #include "tree-inline.h"
117 #include "langhooks.h"
118 #include "pointer-set.h"
119 #include "toplev.h"
120 #include "flags.h"
121 #include "ggc.h"
122 #include "debug.h"
123 #include "target.h"
124 #include "cgraph.h"
125 #include "diagnostic.h"
126 #include "timevar.h"
127 #include "params.h"
128 #include "fibheap.h"
129 #include "c-common.h"
130 #include "intl.h"
131 #include "function.h"
132 #include "ipa-prop.h"
133 #include "gimple.h"
134 #include "tree-iterator.h"
135 #include "tree-pass.h"
136 #include "output.h"
137
138 static void cgraph_expand_all_functions (void);
139 static void cgraph_mark_functions_to_output (void);
140 static void cgraph_expand_function (struct cgraph_node *);
141 static void cgraph_output_pending_asms (void);
142
143 static FILE *cgraph_dump_file;
144
145 /* A vector of FUNCTION_DECLs declared as static constructors.  */
146 static GTY (()) VEC(tree, gc) *static_ctors;
147 /* A vector of FUNCTION_DECLs declared as static destructors.  */
148 static GTY (()) VEC(tree, gc) *static_dtors;
149
150 /* When target does not have ctors and dtors, we call all constructor
151    and destructor by special initialization/destruction function
152    recognized by collect2.  
153    
154    When we are going to build this function, collect all constructors and
155    destructors and turn them into normal functions.  */
156
157 static void
158 record_cdtor_fn (tree fndecl)
159 {
160   struct cgraph_node *node;
161   if (targetm.have_ctors_dtors
162       || (!DECL_STATIC_CONSTRUCTOR (fndecl)
163           && !DECL_STATIC_DESTRUCTOR (fndecl)))
164     return;
165
166   if (DECL_STATIC_CONSTRUCTOR (fndecl))
167     {
168       VEC_safe_push (tree, gc, static_ctors, fndecl);
169       DECL_STATIC_CONSTRUCTOR (fndecl) = 0;
170     }
171   if (DECL_STATIC_DESTRUCTOR (fndecl))
172     {
173       VEC_safe_push (tree, gc, static_dtors, fndecl);
174       DECL_STATIC_DESTRUCTOR (fndecl) = 0;
175     }
176   node = cgraph_node (fndecl);
177   node->local.disregard_inline_limits = 1;
178   cgraph_mark_reachable_node (node);
179 }
180
181 /* Define global constructors/destructor functions for the CDTORS, of
182    which they are LEN.  The CDTORS are sorted by initialization
183    priority.  If CTOR_P is true, these are constructors; otherwise,
184    they are destructors.  */
185
186 static void
187 build_cdtor (bool ctor_p, tree *cdtors, size_t len)
188 {
189   size_t i;
190
191   i = 0;
192   while (i < len)
193     {
194       tree body;
195       tree fn;
196       priority_type priority;
197
198       priority = 0;
199       body = NULL_TREE;
200       /* Find the next batch of constructors/destructors with the same
201          initialization priority.  */
202       do
203         {
204           priority_type p;
205           fn = cdtors[i];
206           p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
207           if (!body)
208             priority = p;
209           else if (p != priority)
210             break;
211           append_to_statement_list (build_function_call_expr (fn, 0),
212                                     &body);
213           ++i;
214         }
215       while (i < len);
216       gcc_assert (body != NULL_TREE);
217       /* Generate a function to call all the function of like
218          priority.  */
219       cgraph_build_static_cdtor (ctor_p ? 'I' : 'D', body, priority);
220     }
221 }
222
223 /* Comparison function for qsort.  P1 and P2 are actually of type
224    "tree *" and point to static constructors.  DECL_INIT_PRIORITY is
225    used to determine the sort order.  */
226
227 static int
228 compare_ctor (const void *p1, const void *p2)
229 {
230   tree f1;
231   tree f2;
232   int priority1;
233   int priority2;
234
235   f1 = *(const tree *)p1;
236   f2 = *(const tree *)p2;
237   priority1 = DECL_INIT_PRIORITY (f1);
238   priority2 = DECL_INIT_PRIORITY (f2);
239   
240   if (priority1 < priority2)
241     return -1;
242   else if (priority1 > priority2)
243     return 1;
244   else
245     /* Ensure a stable sort.  */
246     return (const tree *)p1 - (const tree *)p2;
247 }
248
249 /* Comparison function for qsort.  P1 and P2 are actually of type
250    "tree *" and point to static destructors.  DECL_FINI_PRIORITY is
251    used to determine the sort order.  */
252
253 static int
254 compare_dtor (const void *p1, const void *p2)
255 {
256   tree f1;
257   tree f2;
258   int priority1;
259   int priority2;
260
261   f1 = *(const tree *)p1;
262   f2 = *(const tree *)p2;
263   priority1 = DECL_FINI_PRIORITY (f1);
264   priority2 = DECL_FINI_PRIORITY (f2);
265   
266   if (priority1 < priority2)
267     return -1;
268   else if (priority1 > priority2)
269     return 1;
270   else
271     /* Ensure a stable sort.  */
272     return (const tree *)p1 - (const tree *)p2;
273 }
274
275 /* Generate functions to call static constructors and destructors
276    for targets that do not support .ctors/.dtors sections.  These
277    functions have magic names which are detected by collect2.  */
278
279 static void
280 cgraph_build_cdtor_fns (void)
281 {
282   if (!VEC_empty (tree, static_ctors))
283     {
284       gcc_assert (!targetm.have_ctors_dtors);
285       qsort (VEC_address (tree, static_ctors),
286              VEC_length (tree, static_ctors), 
287              sizeof (tree),
288              compare_ctor);
289       build_cdtor (/*ctor_p=*/true,
290                    VEC_address (tree, static_ctors),
291                    VEC_length (tree, static_ctors)); 
292       VEC_truncate (tree, static_ctors, 0);
293     }
294
295   if (!VEC_empty (tree, static_dtors))
296     {
297       gcc_assert (!targetm.have_ctors_dtors);
298       qsort (VEC_address (tree, static_dtors),
299              VEC_length (tree, static_dtors), 
300              sizeof (tree),
301              compare_dtor);
302       build_cdtor (/*ctor_p=*/false,
303                    VEC_address (tree, static_dtors),
304                    VEC_length (tree, static_dtors)); 
305       VEC_truncate (tree, static_dtors, 0);
306     }
307 }
308
309 /* Determine if function DECL is needed.  That is, visible to something
310    either outside this translation unit, something magic in the system
311    configury.  */
312
313 static bool
314 decide_is_function_needed (struct cgraph_node *node, tree decl)
315 {
316   if (MAIN_NAME_P (DECL_NAME (decl))
317       && TREE_PUBLIC (decl))
318     {
319       node->local.externally_visible = true;
320       return true;
321     }
322
323   /* If the user told us it is used, then it must be so.  */
324   if (node->local.externally_visible)
325     return true;
326
327   /* ??? If the assembler name is set by hand, it is possible to assemble
328      the name later after finalizing the function and the fact is noticed
329      in assemble_name then.  This is arguably a bug.  */
330   if (DECL_ASSEMBLER_NAME_SET_P (decl)
331       && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
332     return true;
333
334   /* With -fkeep-inline-functions we are keeping all inline functions except
335      for extern inline ones.  */
336   if (flag_keep_inline_functions
337       && DECL_DECLARED_INLINE_P (decl)
338       && !DECL_EXTERNAL (decl)
339       && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl)))
340      return true;
341
342   /* If we decided it was needed before, but at the time we didn't have
343      the body of the function available, then it's still needed.  We have
344      to go back and re-check its dependencies now.  */
345   if (node->needed)
346     return true;
347
348   /* Externally visible functions must be output.  The exception is
349      COMDAT functions that must be output only when they are needed.
350
351      When not optimizing, also output the static functions. (see
352      PR24561), but don't do so for always_inline functions, functions
353      declared inline and nested functions.  These was optimized out
354      in the original implementation and it is unclear whether we want
355      to change the behavior here.  */
356   if (((TREE_PUBLIC (decl)
357         || (!optimize && !node->local.disregard_inline_limits
358             && !DECL_DECLARED_INLINE_P (decl)
359             && !node->origin))
360       && !flag_whole_program)
361       && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
362     return true;
363
364   /* Constructors and destructors are reachable from the runtime by
365      some mechanism.  */
366   if (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl))
367     return true;
368
369   return false;
370 }
371
372 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
373    functions into callgraph in a way so they look like ordinary reachable
374    functions inserted into callgraph already at construction time.  */
375
376 bool
377 cgraph_process_new_functions (void)
378 {
379   bool output = false;
380   tree fndecl;
381   struct cgraph_node *node;
382
383   /*  Note that this queue may grow as its being processed, as the new
384       functions may generate new ones.  */
385   while (cgraph_new_nodes)
386     {
387       node = cgraph_new_nodes;
388       fndecl = node->decl;
389       cgraph_new_nodes = cgraph_new_nodes->next_needed;
390       switch (cgraph_state)
391         {
392         case CGRAPH_STATE_CONSTRUCTION:
393           /* At construction time we just need to finalize function and move
394              it into reachable functions list.  */
395
396           node->next_needed = NULL;
397           cgraph_finalize_function (fndecl, false);
398           cgraph_mark_reachable_node (node);
399           output = true;
400           break;
401
402         case CGRAPH_STATE_IPA:
403         case CGRAPH_STATE_IPA_SSA:
404           /* When IPA optimization already started, do all essential
405              transformations that has been already performed on the whole
406              cgraph but not on this function.  */
407
408           gimple_register_cfg_hooks ();
409           if (!node->analyzed)
410             cgraph_analyze_function (node);
411           push_cfun (DECL_STRUCT_FUNCTION (fndecl));
412           current_function_decl = fndecl;
413           compute_inline_parameters (node);
414           if ((cgraph_state == CGRAPH_STATE_IPA_SSA
415               && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
416               /* When not optimizing, be sure we run early local passes anyway
417                  to expand OMP.  */
418               || !optimize)
419             execute_pass_list (pass_early_local_passes.pass.sub);
420           free_dominance_info (CDI_POST_DOMINATORS);
421           free_dominance_info (CDI_DOMINATORS);
422           pop_cfun ();
423           current_function_decl = NULL;
424           break;
425
426         case CGRAPH_STATE_EXPANSION:
427           /* Functions created during expansion shall be compiled
428              directly.  */
429           node->output = 0;
430           cgraph_expand_function (node);
431           break;
432
433         default:
434           gcc_unreachable ();
435           break;
436         }
437       cgraph_call_function_insertion_hooks (node);
438     }
439   return output;
440 }
441
442 /* As an GCC extension we allow redefinition of the function.  The
443    semantics when both copies of bodies differ is not well defined.
444    We replace the old body with new body so in unit at a time mode
445    we always use new body, while in normal mode we may end up with
446    old body inlined into some functions and new body expanded and
447    inlined in others.
448
449    ??? It may make more sense to use one body for inlining and other
450    body for expanding the function but this is difficult to do.  */
451
452 static void
453 cgraph_reset_node (struct cgraph_node *node)
454 {
455   /* If node->output is set, then we have already begun whole-unit analysis.
456      This is *not* testing for whether we've already emitted the function.
457      That case can be sort-of legitimately seen with real function redefinition
458      errors.  I would argue that the front end should never present us with
459      such a case, but don't enforce that for now.  */
460   gcc_assert (!node->output);
461
462   /* Reset our data structures so we can analyze the function again.  */
463   memset (&node->local, 0, sizeof (node->local));
464   memset (&node->global, 0, sizeof (node->global));
465   memset (&node->rtl, 0, sizeof (node->rtl));
466   node->analyzed = false;
467   node->local.redefined_extern_inline = true;
468   node->local.finalized = false;
469
470   cgraph_node_remove_callees (node);
471
472   /* We may need to re-queue the node for assembling in case
473      we already proceeded it and ignored as not needed or got
474      a re-declaration in IMA mode.  */
475   if (node->reachable)
476     {
477       struct cgraph_node *n;
478
479       for (n = cgraph_nodes_queue; n; n = n->next_needed)
480         if (n == node)
481           break;
482       if (!n)
483         node->reachable = 0;
484     }
485 }
486
487 static void
488 cgraph_lower_function (struct cgraph_node *node)
489 {
490   if (node->lowered)
491     return;
492   tree_lowering_passes (node->decl);
493   node->lowered = true;
494 }
495
496 /* DECL has been parsed.  Take it, queue it, compile it at the whim of the
497    logic in effect.  If NESTED is true, then our caller cannot stand to have
498    the garbage collector run at the moment.  We would need to either create
499    a new GC context, or just not compile right now.  */
500
501 void
502 cgraph_finalize_function (tree decl, bool nested)
503 {
504   struct cgraph_node *node = cgraph_node (decl);
505
506   if (node->local.finalized)
507     cgraph_reset_node (node);
508
509   node->pid = cgraph_max_pid ++;
510   notice_global_symbol (decl);
511   node->local.finalized = true;
512   node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
513   record_cdtor_fn (node->decl);
514   if (node->nested)
515     lower_nested_functions (decl);
516   gcc_assert (!node->nested);
517
518   if (decide_is_function_needed (node, decl))
519     cgraph_mark_needed_node (node);
520
521   /* Since we reclaim unreachable nodes at the end of every language
522      level unit, we need to be conservative about possible entry points
523      there.  */
524   if ((TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl)))
525     cgraph_mark_reachable_node (node);
526
527   /* If we've not yet emitted decl, tell the debug info about it.  */
528   if (!TREE_ASM_WRITTEN (decl))
529     (*debug_hooks->deferred_inline_function) (decl);
530
531   /* Possibly warn about unused parameters.  */
532   if (warn_unused_parameter)
533     do_warn_unused_parameter (decl);
534
535   if (!nested)
536     ggc_collect ();
537 }
538
539 /* C99 extern inline keywords allow changing of declaration after function
540    has been finalized.  We need to re-decide if we want to mark the function as
541    needed then.   */
542
543 void
544 cgraph_mark_if_needed (tree decl)
545 {
546   struct cgraph_node *node = cgraph_node (decl);
547   if (node->local.finalized && decide_is_function_needed (node, decl))
548     cgraph_mark_needed_node (node);
549 }
550
551 /* Verify cgraph nodes of given cgraph node.  */
552 void
553 verify_cgraph_node (struct cgraph_node *node)
554 {
555   struct cgraph_edge *e;
556   struct cgraph_node *main_clone;
557   struct function *this_cfun = DECL_STRUCT_FUNCTION (node->decl);
558   struct function *saved_cfun = cfun;
559   basic_block this_block;
560   gimple_stmt_iterator gsi;
561   bool error_found = false;
562
563   if (errorcount || sorrycount)
564     return;
565
566   timevar_push (TV_CGRAPH_VERIFY);
567   /* debug_generic_stmt needs correct cfun */
568   set_cfun (this_cfun);
569   for (e = node->callees; e; e = e->next_callee)
570     if (e->aux)
571       {
572         error ("aux field set for edge %s->%s",
573                cgraph_node_name (e->caller), cgraph_node_name (e->callee));
574         error_found = true;
575       }
576   if (node->count < 0)
577     {
578       error ("Execution count is negative");
579       error_found = true;
580     }
581   for (e = node->callers; e; e = e->next_caller)
582     {
583       if (e->count < 0)
584         {
585           error ("caller edge count is negative");
586           error_found = true;
587         }
588       if (e->frequency < 0)
589         {
590           error ("caller edge frequency is negative");
591           error_found = true;
592         }
593       if (e->frequency > CGRAPH_FREQ_MAX)
594         {
595           error ("caller edge frequency is too large");
596           error_found = true;
597         }
598       if (!e->inline_failed)
599         {
600           if (node->global.inlined_to
601               != (e->caller->global.inlined_to
602                   ? e->caller->global.inlined_to : e->caller))
603             {
604               error ("inlined_to pointer is wrong");
605               error_found = true;
606             }
607           if (node->callers->next_caller)
608             {
609               error ("multiple inline callers");
610               error_found = true;
611             }
612         }
613       else
614         if (node->global.inlined_to)
615           {
616             error ("inlined_to pointer set for noninline callers");
617             error_found = true;
618           }
619     }
620   if (!node->callers && node->global.inlined_to)
621     {
622       error ("inlined_to pointer is set but no predecessors found");
623       error_found = true;
624     }
625   if (node->global.inlined_to == node)
626     {
627       error ("inlined_to pointer refers to itself");
628       error_found = true;
629     }
630
631   for (main_clone = cgraph_node (node->decl); main_clone;
632        main_clone = main_clone->next_clone)
633     if (main_clone == node)
634       break;
635   if (!cgraph_node (node->decl))
636     {
637       error ("node not found in cgraph_hash");
638       error_found = true;
639     }
640
641   if (node->analyzed
642       && gimple_body (node->decl)
643       && !TREE_ASM_WRITTEN (node->decl)
644       && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to))
645     {
646       if (this_cfun->cfg)
647         {
648           /* The nodes we're interested in are never shared, so walk
649              the tree ignoring duplicates.  */
650           struct pointer_set_t *visited_nodes = pointer_set_create ();
651           /* Reach the trees by walking over the CFG, and note the
652              enclosing basic-blocks in the call edges.  */
653           FOR_EACH_BB_FN (this_block, this_cfun)
654             for (gsi = gsi_start_bb (this_block);
655                  !gsi_end_p (gsi);
656                  gsi_next (&gsi))
657               {
658                 gimple stmt = gsi_stmt (gsi);
659                 tree decl;
660                 if (is_gimple_call (stmt) && (decl = gimple_call_fndecl (stmt)))
661                   {
662                     struct cgraph_edge *e = cgraph_edge (node, stmt);
663                     if (e)
664                       {
665                         if (e->aux)
666                           {
667                             error ("shared call_stmt:");
668                             debug_gimple_stmt (stmt);
669                             error_found = true;
670                           }
671                         if (e->callee->decl != cgraph_node (decl)->decl
672                             && e->inline_failed)
673                           {
674                             error ("edge points to wrong declaration:");
675                             debug_tree (e->callee->decl);
676                             fprintf (stderr," Instead of:");
677                             debug_tree (decl);
678                           }
679                         e->aux = (void *)1;
680                       }
681                     else
682                       {
683                         error ("missing callgraph edge for call stmt:");
684                         debug_gimple_stmt (stmt);
685                         error_found = true;
686                       }
687                   }
688               }
689           pointer_set_destroy (visited_nodes);
690         }
691       else
692         /* No CFG available?!  */
693         gcc_unreachable ();
694
695       for (e = node->callees; e; e = e->next_callee)
696         {
697           if (!e->aux && !e->indirect_call)
698             {
699               error ("edge %s->%s has no corresponding call_stmt",
700                      cgraph_node_name (e->caller),
701                      cgraph_node_name (e->callee));
702               debug_gimple_stmt (e->call_stmt);
703               error_found = true;
704             }
705           e->aux = 0;
706         }
707     }
708   if (error_found)
709     {
710       dump_cgraph_node (stderr, node);
711       internal_error ("verify_cgraph_node failed");
712     }
713   set_cfun (saved_cfun);
714   timevar_pop (TV_CGRAPH_VERIFY);
715 }
716
717 /* Verify whole cgraph structure.  */
718 void
719 verify_cgraph (void)
720 {
721   struct cgraph_node *node;
722
723   if (sorrycount || errorcount)
724     return;
725
726   for (node = cgraph_nodes; node; node = node->next)
727     verify_cgraph_node (node);
728 }
729
730 /* Output all asm statements we have stored up to be output.  */
731
732 static void
733 cgraph_output_pending_asms (void)
734 {
735   struct cgraph_asm_node *can;
736
737   if (errorcount || sorrycount)
738     return;
739
740   for (can = cgraph_asm_nodes; can; can = can->next)
741     assemble_asm (can->asm_str);
742   cgraph_asm_nodes = NULL;
743 }
744
745 /* Analyze the function scheduled to be output.  */
746 void
747 cgraph_analyze_function (struct cgraph_node *node)
748 {
749   tree decl = node->decl;
750
751   current_function_decl = decl;
752   push_cfun (DECL_STRUCT_FUNCTION (decl));
753   cgraph_lower_function (node);
754   node->analyzed = true;
755
756   pop_cfun ();
757   current_function_decl = NULL;
758 }
759
760 /* Look for externally_visible and used attributes and mark cgraph nodes
761    accordingly.
762
763    We cannot mark the nodes at the point the attributes are processed (in
764    handle_*_attribute) because the copy of the declarations available at that
765    point may not be canonical.  For example, in:
766
767     void f();
768     void f() __attribute__((used));
769
770    the declaration we see in handle_used_attribute will be the second
771    declaration -- but the front end will subsequently merge that declaration
772    with the original declaration and discard the second declaration.
773
774    Furthermore, we can't mark these nodes in cgraph_finalize_function because:
775
776     void f() {}
777     void f() __attribute__((externally_visible));
778
779    is valid.
780
781    So, we walk the nodes at the end of the translation unit, applying the
782    attributes at that point.  */
783
784 static void
785 process_function_and_variable_attributes (struct cgraph_node *first,
786                                           struct varpool_node *first_var)
787 {
788   struct cgraph_node *node;
789   struct varpool_node *vnode;
790
791   for (node = cgraph_nodes; node != first; node = node->next)
792     {
793       tree decl = node->decl;
794       if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
795         {
796           mark_decl_referenced (decl);
797           if (node->local.finalized)
798              cgraph_mark_needed_node (node);
799         }
800       if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
801         {
802           if (! TREE_PUBLIC (node->decl))
803             warning (OPT_Wattributes,
804                      "%J%<externally_visible%> attribute have effect only on public objects",
805                      node->decl);
806           else
807             {
808               if (node->local.finalized)
809                 cgraph_mark_needed_node (node);
810               node->local.externally_visible = true;
811             }
812         }
813     }
814   for (vnode = varpool_nodes; vnode != first_var; vnode = vnode->next)
815     {
816       tree decl = vnode->decl;
817       if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
818         {
819           mark_decl_referenced (decl);
820           if (vnode->finalized)
821             varpool_mark_needed_node (vnode);
822         }
823       if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
824         {
825           if (! TREE_PUBLIC (vnode->decl))
826             warning (OPT_Wattributes,
827                      "%J%<externally_visible%> attribute have effect only on public objects",
828                      vnode->decl);
829           else
830             {
831               if (vnode->finalized)
832                 varpool_mark_needed_node (vnode);
833               vnode->externally_visible = true;
834             }
835         }
836     }
837 }
838
839 /* Process CGRAPH_NODES_NEEDED queue, analyze each function (and transitively
840    each reachable functions) and build cgraph.
841    The function can be called multiple times after inserting new nodes
842    into beginning of queue.  Just the new part of queue is re-scanned then.  */
843
844 static void
845 cgraph_analyze_functions (void)
846 {
847   /* Keep track of already processed nodes when called multiple times for
848      intermodule optimization.  */
849   static struct cgraph_node *first_analyzed;
850   struct cgraph_node *first_processed = first_analyzed;
851   static struct varpool_node *first_analyzed_var;
852   struct cgraph_node *node, *next;
853
854   process_function_and_variable_attributes (first_processed,
855                                             first_analyzed_var);
856   first_processed = cgraph_nodes;
857   first_analyzed_var = varpool_nodes;
858   varpool_analyze_pending_decls ();
859   if (cgraph_dump_file)
860     {
861       fprintf (cgraph_dump_file, "Initial entry points:");
862       for (node = cgraph_nodes; node != first_analyzed; node = node->next)
863         if (node->needed && gimple_body (node->decl))
864           fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
865       fprintf (cgraph_dump_file, "\n");
866     }
867   cgraph_process_new_functions ();
868
869   /* Propagate reachability flag and lower representation of all reachable
870      functions.  In the future, lowering will introduce new functions and
871      new entry points on the way (by template instantiation and virtual
872      method table generation for instance).  */
873   while (cgraph_nodes_queue)
874     {
875       struct cgraph_edge *edge;
876       tree decl = cgraph_nodes_queue->decl;
877
878       node = cgraph_nodes_queue;
879       cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
880       node->next_needed = NULL;
881
882       /* ??? It is possible to create extern inline function and later using
883          weak alias attribute to kill its body. See
884          gcc.c-torture/compile/20011119-1.c  */
885       if (!DECL_STRUCT_FUNCTION (decl))
886         {
887           cgraph_reset_node (node);
888           continue;
889         }
890
891       gcc_assert (!node->analyzed && node->reachable);
892       gcc_assert (gimple_body (decl));
893
894       cgraph_analyze_function (node);
895
896       for (edge = node->callees; edge; edge = edge->next_callee)
897         if (!edge->callee->reachable)
898           cgraph_mark_reachable_node (edge->callee);
899
900       /* We finalize local static variables during constructing callgraph
901          edges.  Process their attributes too.  */
902       process_function_and_variable_attributes (first_processed,
903                                                 first_analyzed_var);
904       first_processed = cgraph_nodes;
905       first_analyzed_var = varpool_nodes;
906       varpool_analyze_pending_decls ();
907       cgraph_process_new_functions ();
908     }
909
910   /* Collect entry points to the unit.  */
911   if (cgraph_dump_file)
912     {
913       fprintf (cgraph_dump_file, "Unit entry points:");
914       for (node = cgraph_nodes; node != first_analyzed; node = node->next)
915         if (node->needed && gimple_body (node->decl))
916           fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
917       fprintf (cgraph_dump_file, "\n\nInitial ");
918       dump_cgraph (cgraph_dump_file);
919     }
920
921   if (cgraph_dump_file)
922     fprintf (cgraph_dump_file, "\nReclaiming functions:");
923
924   for (node = cgraph_nodes; node != first_analyzed; node = next)
925     {
926       tree decl = node->decl;
927       next = node->next;
928
929       if (node->local.finalized && !gimple_body (decl))
930         cgraph_reset_node (node);
931
932       if (!node->reachable && gimple_body (decl))
933         {
934           if (cgraph_dump_file)
935             fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
936           cgraph_remove_node (node);
937           continue;
938         }
939       else
940         node->next_needed = NULL;
941       gcc_assert (!node->local.finalized || gimple_body (decl));
942       gcc_assert (node->analyzed == node->local.finalized);
943     }
944   if (cgraph_dump_file)
945     {
946       fprintf (cgraph_dump_file, "\n\nReclaimed ");
947       dump_cgraph (cgraph_dump_file);
948     }
949   first_analyzed = cgraph_nodes;
950   ggc_collect ();
951 }
952
953 /* Analyze the whole compilation unit once it is parsed completely.  */
954
955 void
956 cgraph_finalize_compilation_unit (void)
957 {
958   if (errorcount || sorrycount)
959     return;
960
961   finish_aliases_1 ();
962
963   if (!quiet_flag)
964     {
965       fprintf (stderr, "\nAnalyzing compilation unit\n");
966       fflush (stderr);
967     }
968
969   timevar_push (TV_CGRAPH);
970   cgraph_analyze_functions ();
971   timevar_pop (TV_CGRAPH);
972 }
973 /* Figure out what functions we want to assemble.  */
974
975 static void
976 cgraph_mark_functions_to_output (void)
977 {
978   struct cgraph_node *node;
979
980   for (node = cgraph_nodes; node; node = node->next)
981     {
982       tree decl = node->decl;
983       struct cgraph_edge *e;
984
985       gcc_assert (!node->output);
986
987       for (e = node->callers; e; e = e->next_caller)
988         if (e->inline_failed)
989           break;
990
991       /* We need to output all local functions that are used and not
992          always inlined, as well as those that are reachable from
993          outside the current compilation unit.  */
994       if (gimple_body (decl)
995           && !node->global.inlined_to
996           && (node->needed
997               || (e && node->reachable))
998           && !TREE_ASM_WRITTEN (decl)
999           && !DECL_EXTERNAL (decl))
1000         node->output = 1;
1001       else
1002         {
1003           /* We should've reclaimed all functions that are not needed.  */
1004 #ifdef ENABLE_CHECKING
1005           if (!node->global.inlined_to
1006               && gimple_body (decl)
1007               && !DECL_EXTERNAL (decl))
1008             {
1009               dump_cgraph_node (stderr, node);
1010               internal_error ("failed to reclaim unneeded function");
1011             }
1012 #endif
1013           gcc_assert (node->global.inlined_to
1014                       || !gimple_body (decl)
1015                       || DECL_EXTERNAL (decl));
1016
1017         }
1018
1019     }
1020 }
1021
1022 /* Expand function specified by NODE.  */
1023
1024 static void
1025 cgraph_expand_function (struct cgraph_node *node)
1026 {
1027   tree decl = node->decl;
1028
1029   /* We ought to not compile any inline clones.  */
1030   gcc_assert (!node->global.inlined_to);
1031
1032   announce_function (decl);
1033
1034   gcc_assert (node->lowered);
1035
1036   /* Generate RTL for the body of DECL.  */
1037   if (lang_hooks.callgraph.emit_associated_thunks)
1038     lang_hooks.callgraph.emit_associated_thunks (decl);
1039   tree_rest_of_compilation (decl);
1040
1041   /* Make sure that BE didn't give up on compiling.  */
1042   /* ??? Can happen with nested function of extern inline.  */
1043   gcc_assert (TREE_ASM_WRITTEN (decl));
1044   current_function_decl = NULL;
1045   if (!cgraph_preserve_function_body_p (decl))
1046     {
1047       cgraph_release_function_body (node);
1048       /* Eliminate all call edges.  This is important so the call_expr no longer
1049          points to the dead function body.  */
1050       cgraph_node_remove_callees (node);
1051     }
1052
1053   cgraph_function_flags_ready = true;
1054 }
1055
1056 /* Return true when CALLER_DECL should be inlined into CALLEE_DECL.  */
1057
1058 bool
1059 cgraph_inline_p (struct cgraph_edge *e, const char **reason)
1060 {
1061   *reason = e->inline_failed;
1062   return !e->inline_failed;
1063 }
1064
1065
1066
1067 /* Expand all functions that must be output.
1068
1069    Attempt to topologically sort the nodes so function is output when
1070    all called functions are already assembled to allow data to be
1071    propagated across the callgraph.  Use a stack to get smaller distance
1072    between a function and its callees (later we may choose to use a more
1073    sophisticated algorithm for function reordering; we will likely want
1074    to use subsections to make the output functions appear in top-down
1075    order).  */
1076
1077 static void
1078 cgraph_expand_all_functions (void)
1079 {
1080   struct cgraph_node *node;
1081   struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1082   int order_pos, new_order_pos = 0;
1083   int i;
1084
1085   order_pos = cgraph_postorder (order);
1086   gcc_assert (order_pos == cgraph_n_nodes);
1087
1088   /* Garbage collector may remove inline clones we eliminate during
1089      optimization.  So we must be sure to not reference them.  */
1090   for (i = 0; i < order_pos; i++)
1091     if (order[i]->output)
1092       order[new_order_pos++] = order[i];
1093
1094   for (i = new_order_pos - 1; i >= 0; i--)
1095     {
1096       node = order[i];
1097       if (node->output)
1098         {
1099           gcc_assert (node->reachable);
1100           node->output = 0;
1101           cgraph_expand_function (node);
1102         }
1103     }
1104   cgraph_process_new_functions ();
1105
1106   free (order);
1107
1108 }
1109
1110 /* This is used to sort the node types by the cgraph order number.  */
1111
1112 struct cgraph_order_sort
1113 {
1114   enum { ORDER_UNDEFINED = 0, ORDER_FUNCTION, ORDER_VAR, ORDER_ASM } kind;
1115   union
1116   {
1117     struct cgraph_node *f;
1118     struct varpool_node *v;
1119     struct cgraph_asm_node *a;
1120   } u;
1121 };
1122
1123 /* Output all functions, variables, and asm statements in the order
1124    according to their order fields, which is the order in which they
1125    appeared in the file.  This implements -fno-toplevel-reorder.  In
1126    this mode we may output functions and variables which don't really
1127    need to be output.  */
1128
1129 static void
1130 cgraph_output_in_order (void)
1131 {
1132   int max;
1133   size_t size;
1134   struct cgraph_order_sort *nodes;
1135   int i;
1136   struct cgraph_node *pf;
1137   struct varpool_node *pv;
1138   struct cgraph_asm_node *pa;
1139
1140   max = cgraph_order;
1141   size = max * sizeof (struct cgraph_order_sort);
1142   nodes = (struct cgraph_order_sort *) alloca (size);
1143   memset (nodes, 0, size);
1144
1145   varpool_analyze_pending_decls ();
1146
1147   for (pf = cgraph_nodes; pf; pf = pf->next)
1148     {
1149       if (pf->output)
1150         {
1151           i = pf->order;
1152           gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1153           nodes[i].kind = ORDER_FUNCTION;
1154           nodes[i].u.f = pf;
1155         }
1156     }
1157
1158   for (pv = varpool_nodes_queue; pv; pv = pv->next_needed)
1159     {
1160       i = pv->order;
1161       gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1162       nodes[i].kind = ORDER_VAR;
1163       nodes[i].u.v = pv;
1164     }
1165
1166   for (pa = cgraph_asm_nodes; pa; pa = pa->next)
1167     {
1168       i = pa->order;
1169       gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1170       nodes[i].kind = ORDER_ASM;
1171       nodes[i].u.a = pa;
1172     }
1173
1174   /* In toplevel reorder mode we output all statics; mark them as needed.  */
1175   for (i = 0; i < max; ++i)
1176     {
1177       if (nodes[i].kind == ORDER_VAR)
1178         {
1179           varpool_mark_needed_node (nodes[i].u.v);
1180         }
1181     }
1182   varpool_empty_needed_queue ();
1183
1184   for (i = 0; i < max; ++i)
1185     {
1186       switch (nodes[i].kind)
1187         {
1188         case ORDER_FUNCTION:
1189           nodes[i].u.f->output = 0;
1190           cgraph_expand_function (nodes[i].u.f);
1191           break;
1192
1193         case ORDER_VAR:
1194           varpool_assemble_decl (nodes[i].u.v);
1195           break;
1196
1197         case ORDER_ASM:
1198           assemble_asm (nodes[i].u.a->asm_str);
1199           break;
1200
1201         case ORDER_UNDEFINED:
1202           break;
1203
1204         default:
1205           gcc_unreachable ();
1206         }
1207     }
1208
1209   cgraph_asm_nodes = NULL;
1210 }
1211
1212 /* Return true when function body of DECL still needs to be kept around
1213    for later re-use.  */
1214 bool
1215 cgraph_preserve_function_body_p (tree decl)
1216 {
1217   struct cgraph_node *node;
1218
1219   gcc_assert (cgraph_global_info_ready);
1220   /* Look if there is any clone around.  */
1221   for (node = cgraph_node (decl); node; node = node->next_clone)
1222     if (node->global.inlined_to)
1223       return true;
1224   return false;
1225 }
1226
1227 static void
1228 ipa_passes (void)
1229 {
1230   set_cfun (NULL);
1231   current_function_decl = NULL;
1232   gimple_register_cfg_hooks ();
1233   bitmap_obstack_initialize (NULL);
1234   execute_ipa_pass_list (all_ipa_passes);
1235   bitmap_obstack_release (NULL);
1236 }
1237
1238 /* Perform simple optimizations based on callgraph.  */
1239
1240 void
1241 cgraph_optimize (void)
1242 {
1243   if (errorcount || sorrycount)
1244     return;
1245
1246 #ifdef ENABLE_CHECKING
1247   verify_cgraph ();
1248 #endif
1249
1250   /* Call functions declared with the "constructor" or "destructor"
1251      attribute.  */
1252   cgraph_build_cdtor_fns ();
1253
1254   /* Frontend may output common variables after the unit has been finalized.
1255      It is safe to deal with them here as they are always zero initialized.  */
1256   varpool_analyze_pending_decls ();
1257   cgraph_analyze_functions ();
1258
1259   timevar_push (TV_CGRAPHOPT);
1260   if (pre_ipa_mem_report)
1261     {
1262       fprintf (stderr, "Memory consumption before IPA\n");
1263       dump_memory_report (false);
1264     }
1265   if (!quiet_flag)
1266     fprintf (stderr, "Performing interprocedural optimizations\n");
1267   cgraph_state = CGRAPH_STATE_IPA;
1268
1269   /* Don't run the IPA passes if there was any error or sorry messages.  */
1270   if (errorcount == 0 && sorrycount == 0)
1271     ipa_passes ();
1272
1273   /* This pass remove bodies of extern inline functions we never inlined.
1274      Do this later so other IPA passes see what is really going on.  */
1275   cgraph_remove_unreachable_nodes (false, dump_file);
1276   cgraph_global_info_ready = true;
1277   if (cgraph_dump_file)
1278     {
1279       fprintf (cgraph_dump_file, "Optimized ");
1280       dump_cgraph (cgraph_dump_file);
1281       dump_varpool (cgraph_dump_file);
1282     }
1283   if (post_ipa_mem_report)
1284     {
1285       fprintf (stderr, "Memory consumption after IPA\n");
1286       dump_memory_report (false);
1287     }
1288   timevar_pop (TV_CGRAPHOPT);
1289
1290   /* Output everything.  */
1291   if (!quiet_flag)
1292     fprintf (stderr, "Assembling functions:\n");
1293 #ifdef ENABLE_CHECKING
1294   verify_cgraph ();
1295 #endif
1296
1297   cgraph_mark_functions_to_output ();
1298
1299   cgraph_state = CGRAPH_STATE_EXPANSION;
1300   if (!flag_toplevel_reorder)
1301     cgraph_output_in_order ();
1302   else
1303     {
1304       cgraph_output_pending_asms ();
1305
1306       cgraph_expand_all_functions ();
1307       varpool_remove_unreferenced_decls ();
1308
1309       varpool_assemble_pending_decls ();
1310     }
1311   varpool_output_debug_info ();
1312   cgraph_process_new_functions ();
1313   cgraph_state = CGRAPH_STATE_FINISHED;
1314
1315   if (cgraph_dump_file)
1316     {
1317       fprintf (cgraph_dump_file, "\nFinal ");
1318       dump_cgraph (cgraph_dump_file);
1319     }
1320 #ifdef ENABLE_CHECKING
1321   verify_cgraph ();
1322   /* Double check that all inline clones are gone and that all
1323      function bodies have been released from memory.  */
1324   if (!(sorrycount || errorcount))
1325     {
1326       struct cgraph_node *node;
1327       bool error_found = false;
1328
1329       for (node = cgraph_nodes; node; node = node->next)
1330         if (node->analyzed
1331             && (node->global.inlined_to
1332                 || gimple_body (node->decl)))
1333           {
1334             error_found = true;
1335             dump_cgraph_node (stderr, node);
1336           }
1337       if (error_found)
1338         internal_error ("nodes with unreleased memory found");
1339     }
1340 #endif
1341 }
1342 /* Generate and emit a static constructor or destructor.  WHICH must
1343    be one of 'I' (for a constructor) or 'D' (for a destructor).  BODY
1344    is a STATEMENT_LIST containing GENERIC statements.  PRIORITY is the
1345    initialization priority for this constructor or destructor.  */
1346
1347 void
1348 cgraph_build_static_cdtor (char which, tree body, int priority)
1349 {
1350   static int counter = 0;
1351   char which_buf[16];
1352   tree decl, name, resdecl;
1353
1354   /* The priority is encoded in the constructor or destructor name.
1355      collect2 will sort the names and arrange that they are called at
1356      program startup.  */
1357   sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
1358   name = get_file_function_name (which_buf);
1359
1360   decl = build_decl (FUNCTION_DECL, name,
1361                      build_function_type (void_type_node, void_list_node));
1362   current_function_decl = decl;
1363
1364   resdecl = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
1365   DECL_ARTIFICIAL (resdecl) = 1;
1366   DECL_RESULT (decl) = resdecl;
1367
1368   allocate_struct_function (decl, false);
1369
1370   TREE_STATIC (decl) = 1;
1371   TREE_USED (decl) = 1;
1372   DECL_ARTIFICIAL (decl) = 1;
1373   DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1374   DECL_SAVED_TREE (decl) = body;
1375   TREE_PUBLIC (decl) = ! targetm.have_ctors_dtors;
1376   DECL_UNINLINABLE (decl) = 1;
1377
1378   DECL_INITIAL (decl) = make_node (BLOCK);
1379   TREE_USED (DECL_INITIAL (decl)) = 1;
1380
1381   DECL_SOURCE_LOCATION (decl) = input_location;
1382   cfun->function_end_locus = input_location;
1383
1384   switch (which)
1385     {
1386     case 'I':
1387       DECL_STATIC_CONSTRUCTOR (decl) = 1;
1388       decl_init_priority_insert (decl, priority);
1389       break;
1390     case 'D':
1391       DECL_STATIC_DESTRUCTOR (decl) = 1;
1392       decl_fini_priority_insert (decl, priority);
1393       break;
1394     default:
1395       gcc_unreachable ();
1396     }
1397
1398   gimplify_function_tree (decl);
1399
1400   cgraph_add_new_function (decl, false);
1401   cgraph_mark_needed_node (cgraph_node (decl));
1402   set_cfun (NULL);
1403 }
1404
1405 void
1406 init_cgraph (void)
1407 {
1408   cgraph_dump_file = dump_begin (TDI_cgraph, NULL);
1409 }
1410
1411 /* The edges representing the callers of the NEW_VERSION node were
1412    fixed by cgraph_function_versioning (), now the call_expr in their
1413    respective tree code should be updated to call the NEW_VERSION.  */
1414
1415 static void
1416 update_call_expr (struct cgraph_node *new_version)
1417 {
1418   struct cgraph_edge *e;
1419
1420   gcc_assert (new_version);
1421
1422   /* Update the call expr on the edges to call the new version.  */
1423   for (e = new_version->callers; e; e = e->next_caller)
1424     gimple_call_set_fndecl (e->call_stmt, new_version->decl);
1425 }
1426
1427
1428 /* Create a new cgraph node which is the new version of
1429    OLD_VERSION node.  REDIRECT_CALLERS holds the callers
1430    edges which should be redirected to point to
1431    NEW_VERSION.  ALL the callees edges of OLD_VERSION
1432    are cloned to the new version node.  Return the new
1433    version node.  */
1434
1435 static struct cgraph_node *
1436 cgraph_copy_node_for_versioning (struct cgraph_node *old_version,
1437                                  tree new_decl,
1438                                  VEC(cgraph_edge_p,heap) *redirect_callers)
1439  {
1440    struct cgraph_node *new_version;
1441    struct cgraph_edge *e, *new_e;
1442    struct cgraph_edge *next_callee;
1443    unsigned i;
1444
1445    gcc_assert (old_version);
1446
1447    new_version = cgraph_node (new_decl);
1448
1449    new_version->analyzed = true;
1450    new_version->local = old_version->local;
1451    new_version->global = old_version->global;
1452    new_version->rtl = new_version->rtl;
1453    new_version->reachable = true;
1454    new_version->count = old_version->count;
1455
1456    /* Clone the old node callees.  Recursive calls are
1457       also cloned.  */
1458    for (e = old_version->callees;e; e=e->next_callee)
1459      {
1460        new_e = cgraph_clone_edge (e, new_version, e->call_stmt, 0, e->frequency,
1461                                   e->loop_nest, true);
1462        new_e->count = e->count;
1463      }
1464    /* Fix recursive calls.
1465       If OLD_VERSION has a recursive call after the
1466       previous edge cloning, the new version will have an edge
1467       pointing to the old version, which is wrong;
1468       Redirect it to point to the new version. */
1469    for (e = new_version->callees ; e; e = next_callee)
1470      {
1471        next_callee = e->next_callee;
1472        if (e->callee == old_version)
1473          cgraph_redirect_edge_callee (e, new_version);
1474
1475        if (!next_callee)
1476          break;
1477      }
1478    for (i = 0; VEC_iterate (cgraph_edge_p, redirect_callers, i, e); i++)
1479      {
1480        /* Redirect calls to the old version node to point to its new
1481           version.  */
1482        cgraph_redirect_edge_callee (e, new_version);
1483      }
1484
1485    return new_version;
1486  }
1487
1488  /* Perform function versioning.
1489     Function versioning includes copying of the tree and
1490     a callgraph update (creating a new cgraph node and updating
1491     its callees and callers).
1492
1493     REDIRECT_CALLERS varray includes the edges to be redirected
1494     to the new version.
1495
1496     TREE_MAP is a mapping of tree nodes we want to replace with
1497     new ones (according to results of prior analysis).
1498     OLD_VERSION_NODE is the node that is versioned.
1499     It returns the new version's cgraph node. 
1500     ARGS_TO_SKIP lists arguments to be omitted from functions
1501     */
1502
1503 struct cgraph_node *
1504 cgraph_function_versioning (struct cgraph_node *old_version_node,
1505                             VEC(cgraph_edge_p,heap) *redirect_callers,
1506                             varray_type tree_map,
1507                             bitmap args_to_skip)
1508 {
1509   tree old_decl = old_version_node->decl;
1510   struct cgraph_node *new_version_node = NULL;
1511   tree new_decl;
1512
1513   if (!tree_versionable_function_p (old_decl))
1514     return NULL;
1515
1516   /* Make a new FUNCTION_DECL tree node for the
1517      new version. */
1518   if (!args_to_skip)
1519     new_decl = copy_node (old_decl);
1520   else
1521     new_decl = build_function_decl_skip_args (old_decl, args_to_skip);
1522
1523   /* Create the new version's call-graph node.
1524      and update the edges of the new node. */
1525   new_version_node =
1526     cgraph_copy_node_for_versioning (old_version_node, new_decl,
1527                                      redirect_callers);
1528
1529   /* Copy the OLD_VERSION_NODE function tree to the new version.  */
1530   tree_function_versioning (old_decl, new_decl, tree_map, false, args_to_skip);
1531   /* Update the call_expr on the edges to call the new version node. */
1532   update_call_expr (new_version_node);
1533
1534   /* Update the new version's properties.
1535      Make The new version visible only within this translation unit.
1536      ??? We cannot use COMDAT linkage because there is no
1537      ABI support for this.  */
1538   DECL_EXTERNAL (new_version_node->decl) = 0;
1539   DECL_ONE_ONLY (new_version_node->decl) = 0;
1540   TREE_PUBLIC (new_version_node->decl) = 0;
1541   DECL_COMDAT (new_version_node->decl) = 0;
1542   new_version_node->local.externally_visible = 0;
1543   new_version_node->local.local = 1;
1544   new_version_node->lowered = true;
1545   cgraph_call_function_insertion_hooks (new_version_node);
1546   return new_version_node;
1547 }
1548
1549 /* Produce separate function body for inline clones so the offline copy can be
1550    modified without affecting them.  */
1551 struct cgraph_node *
1552 save_inline_function_body (struct cgraph_node *node)
1553 {
1554   struct cgraph_node *first_clone;
1555
1556   gcc_assert (node == cgraph_node (node->decl));
1557
1558   cgraph_lower_function (node);
1559
1560   first_clone = node->next_clone;
1561
1562   first_clone->decl = copy_node (node->decl);
1563   node->next_clone = NULL;
1564   first_clone->prev_clone = NULL;
1565   cgraph_insert_node_to_hashtable (first_clone);
1566   gcc_assert (first_clone == cgraph_node (first_clone->decl));
1567
1568   /* Copy the OLD_VERSION_NODE function tree to the new version.  */
1569   tree_function_versioning (node->decl, first_clone->decl, NULL, true, NULL);
1570
1571   DECL_EXTERNAL (first_clone->decl) = 0;
1572   DECL_ONE_ONLY (first_clone->decl) = 0;
1573   TREE_PUBLIC (first_clone->decl) = 0;
1574   DECL_COMDAT (first_clone->decl) = 0;
1575
1576   for (node = first_clone->next_clone; node; node = node->next_clone)
1577     node->decl = first_clone->decl;
1578 #ifdef ENABLE_CHECKING
1579   verify_cgraph_node (first_clone);
1580 #endif
1581   return first_clone;
1582 }
1583
1584 #include "gt-cgraphunit.h"