OSDN Git Service

* toplev.c (compile_file): Move call to coverage_finish ...
[pf3gnuchains/gcc-fork.git] / gcc / cgraphunit.c
1 /* Callgraph based interprocedural optimizations.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 #include "coverage.h"
138
139 static void cgraph_expand_all_functions (void);
140 static void cgraph_mark_functions_to_output (void);
141 static void cgraph_expand_function (struct cgraph_node *);
142 static void cgraph_output_pending_asms (void);
143
144 static FILE *cgraph_dump_file;
145
146 /* A vector of FUNCTION_DECLs declared as static constructors.  */
147 static GTY (()) VEC(tree, gc) *static_ctors;
148 /* A vector of FUNCTION_DECLs declared as static destructors.  */
149 static GTY (()) VEC(tree, gc) *static_dtors;
150
151 /* When target does not have ctors and dtors, we call all constructor
152    and destructor by special initialization/destruction function
153    recognized by collect2.  
154    
155    When we are going to build this function, collect all constructors and
156    destructors and turn them into normal functions.  */
157
158 static void
159 record_cdtor_fn (tree fndecl)
160 {
161   struct cgraph_node *node;
162   if (targetm.have_ctors_dtors
163       || (!DECL_STATIC_CONSTRUCTOR (fndecl)
164           && !DECL_STATIC_DESTRUCTOR (fndecl)))
165     return;
166
167   if (DECL_STATIC_CONSTRUCTOR (fndecl))
168     {
169       VEC_safe_push (tree, gc, static_ctors, fndecl);
170       DECL_STATIC_CONSTRUCTOR (fndecl) = 0;
171     }
172   if (DECL_STATIC_DESTRUCTOR (fndecl))
173     {
174       VEC_safe_push (tree, gc, static_dtors, fndecl);
175       DECL_STATIC_DESTRUCTOR (fndecl) = 0;
176     }
177   node = cgraph_node (fndecl);
178   node->local.disregard_inline_limits = 1;
179   cgraph_mark_reachable_node (node);
180 }
181
182 /* Define global constructors/destructor functions for the CDTORS, of
183    which they are LEN.  The CDTORS are sorted by initialization
184    priority.  If CTOR_P is true, these are constructors; otherwise,
185    they are destructors.  */
186
187 static void
188 build_cdtor (bool ctor_p, tree *cdtors, size_t len)
189 {
190   size_t i;
191
192   i = 0;
193   while (i < len)
194     {
195       tree body;
196       tree fn;
197       priority_type priority;
198
199       priority = 0;
200       body = NULL_TREE;
201       /* Find the next batch of constructors/destructors with the same
202          initialization priority.  */
203       do
204         {
205           priority_type p;
206           fn = cdtors[i];
207           p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
208           if (!body)
209             priority = p;
210           else if (p != priority)
211             break;
212           append_to_statement_list (build_function_call_expr (fn, 0),
213                                     &body);
214           ++i;
215         }
216       while (i < len);
217       gcc_assert (body != NULL_TREE);
218       /* Generate a function to call all the function of like
219          priority.  */
220       cgraph_build_static_cdtor (ctor_p ? 'I' : 'D', body, priority);
221     }
222 }
223
224 /* Comparison function for qsort.  P1 and P2 are actually of type
225    "tree *" and point to static constructors.  DECL_INIT_PRIORITY is
226    used to determine the sort order.  */
227
228 static int
229 compare_ctor (const void *p1, const void *p2)
230 {
231   tree f1;
232   tree f2;
233   int priority1;
234   int priority2;
235
236   f1 = *(const tree *)p1;
237   f2 = *(const tree *)p2;
238   priority1 = DECL_INIT_PRIORITY (f1);
239   priority2 = DECL_INIT_PRIORITY (f2);
240   
241   if (priority1 < priority2)
242     return -1;
243   else if (priority1 > priority2)
244     return 1;
245   else
246     /* Ensure a stable sort.  */
247     return (const tree *)p1 - (const tree *)p2;
248 }
249
250 /* Comparison function for qsort.  P1 and P2 are actually of type
251    "tree *" and point to static destructors.  DECL_FINI_PRIORITY is
252    used to determine the sort order.  */
253
254 static int
255 compare_dtor (const void *p1, const void *p2)
256 {
257   tree f1;
258   tree f2;
259   int priority1;
260   int priority2;
261
262   f1 = *(const tree *)p1;
263   f2 = *(const tree *)p2;
264   priority1 = DECL_FINI_PRIORITY (f1);
265   priority2 = DECL_FINI_PRIORITY (f2);
266   
267   if (priority1 < priority2)
268     return -1;
269   else if (priority1 > priority2)
270     return 1;
271   else
272     /* Ensure a stable sort.  */
273     return (const tree *)p1 - (const tree *)p2;
274 }
275
276 /* Generate functions to call static constructors and destructors
277    for targets that do not support .ctors/.dtors sections.  These
278    functions have magic names which are detected by collect2.  */
279
280 static void
281 cgraph_build_cdtor_fns (void)
282 {
283   if (!VEC_empty (tree, static_ctors))
284     {
285       gcc_assert (!targetm.have_ctors_dtors);
286       qsort (VEC_address (tree, static_ctors),
287              VEC_length (tree, static_ctors), 
288              sizeof (tree),
289              compare_ctor);
290       build_cdtor (/*ctor_p=*/true,
291                    VEC_address (tree, static_ctors),
292                    VEC_length (tree, static_ctors)); 
293       VEC_truncate (tree, static_ctors, 0);
294     }
295
296   if (!VEC_empty (tree, static_dtors))
297     {
298       gcc_assert (!targetm.have_ctors_dtors);
299       qsort (VEC_address (tree, static_dtors),
300              VEC_length (tree, static_dtors), 
301              sizeof (tree),
302              compare_dtor);
303       build_cdtor (/*ctor_p=*/false,
304                    VEC_address (tree, static_dtors),
305                    VEC_length (tree, static_dtors)); 
306       VEC_truncate (tree, static_dtors, 0);
307     }
308 }
309
310 /* Determine if function DECL is needed.  That is, visible to something
311    either outside this translation unit, something magic in the system
312    configury.  */
313
314 static bool
315 decide_is_function_needed (struct cgraph_node *node, tree decl)
316 {
317   if (MAIN_NAME_P (DECL_NAME (decl))
318       && TREE_PUBLIC (decl))
319     {
320       node->local.externally_visible = true;
321       return true;
322     }
323
324   /* If the user told us it is used, then it must be so.  */
325   if (node->local.externally_visible)
326     return true;
327
328   /* ??? If the assembler name is set by hand, it is possible to assemble
329      the name later after finalizing the function and the fact is noticed
330      in assemble_name then.  This is arguably a bug.  */
331   if (DECL_ASSEMBLER_NAME_SET_P (decl)
332       && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
333     return true;
334
335   /* With -fkeep-inline-functions we are keeping all inline functions except
336      for extern inline ones.  */
337   if (flag_keep_inline_functions
338       && DECL_DECLARED_INLINE_P (decl)
339       && !DECL_EXTERNAL (decl)
340       && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl)))
341      return true;
342
343   /* If we decided it was needed before, but at the time we didn't have
344      the body of the function available, then it's still needed.  We have
345      to go back and re-check its dependencies now.  */
346   if (node->needed)
347     return true;
348
349   /* Externally visible functions must be output.  The exception is
350      COMDAT functions that must be output only when they are needed.
351
352      When not optimizing, also output the static functions. (see
353      PR24561), but don't do so for always_inline functions, functions
354      declared inline and nested functions.  These was optimized out
355      in the original implementation and it is unclear whether we want
356      to change the behavior here.  */
357   if (((TREE_PUBLIC (decl)
358         || (!optimize && !node->local.disregard_inline_limits
359             && !DECL_DECLARED_INLINE_P (decl)
360             && !node->origin))
361       && !flag_whole_program)
362       && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
363     return true;
364
365   /* Constructors and destructors are reachable from the runtime by
366      some mechanism.  */
367   if (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl))
368     return true;
369
370   return false;
371 }
372
373 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
374    functions into callgraph in a way so they look like ordinary reachable
375    functions inserted into callgraph already at construction time.  */
376
377 bool
378 cgraph_process_new_functions (void)
379 {
380   bool output = false;
381   tree fndecl;
382   struct cgraph_node *node;
383
384   /*  Note that this queue may grow as its being processed, as the new
385       functions may generate new ones.  */
386   while (cgraph_new_nodes)
387     {
388       node = cgraph_new_nodes;
389       fndecl = node->decl;
390       cgraph_new_nodes = cgraph_new_nodes->next_needed;
391       switch (cgraph_state)
392         {
393         case CGRAPH_STATE_CONSTRUCTION:
394           /* At construction time we just need to finalize function and move
395              it into reachable functions list.  */
396
397           node->next_needed = NULL;
398           cgraph_finalize_function (fndecl, false);
399           cgraph_mark_reachable_node (node);
400           output = true;
401           break;
402
403         case CGRAPH_STATE_IPA:
404         case CGRAPH_STATE_IPA_SSA:
405           /* When IPA optimization already started, do all essential
406              transformations that has been already performed on the whole
407              cgraph but not on this function.  */
408
409           gimple_register_cfg_hooks ();
410           if (!node->analyzed)
411             cgraph_analyze_function (node);
412           push_cfun (DECL_STRUCT_FUNCTION (fndecl));
413           current_function_decl = fndecl;
414           compute_inline_parameters (node);
415           if ((cgraph_state == CGRAPH_STATE_IPA_SSA
416               && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
417               /* When not optimizing, be sure we run early local passes anyway
418                  to expand OMP.  */
419               || !optimize)
420             execute_pass_list (pass_early_local_passes.pass.sub);
421           free_dominance_info (CDI_POST_DOMINATORS);
422           free_dominance_info (CDI_DOMINATORS);
423           pop_cfun ();
424           current_function_decl = NULL;
425           break;
426
427         case CGRAPH_STATE_EXPANSION:
428           /* Functions created during expansion shall be compiled
429              directly.  */
430           node->process = 0;
431           cgraph_expand_function (node);
432           break;
433
434         default:
435           gcc_unreachable ();
436           break;
437         }
438       cgraph_call_function_insertion_hooks (node);
439     }
440   return output;
441 }
442
443 /* As an GCC extension we allow redefinition of the function.  The
444    semantics when both copies of bodies differ is not well defined.
445    We replace the old body with new body so in unit at a time mode
446    we always use new body, while in normal mode we may end up with
447    old body inlined into some functions and new body expanded and
448    inlined in others.
449
450    ??? It may make more sense to use one body for inlining and other
451    body for expanding the function but this is difficult to do.  */
452
453 static void
454 cgraph_reset_node (struct cgraph_node *node)
455 {
456   /* If node->process is set, then we have already begun whole-unit analysis.
457      This is *not* testing for whether we've already emitted the function.
458      That case can be sort-of legitimately seen with real function redefinition
459      errors.  I would argue that the front end should never present us with
460      such a case, but don't enforce that for now.  */
461   gcc_assert (!node->process);
462
463   /* Reset our data structures so we can analyze the function again.  */
464   memset (&node->local, 0, sizeof (node->local));
465   memset (&node->global, 0, sizeof (node->global));
466   memset (&node->rtl, 0, sizeof (node->rtl));
467   node->analyzed = false;
468   node->local.redefined_extern_inline = true;
469   node->local.finalized = false;
470
471   cgraph_node_remove_callees (node);
472
473   /* We may need to re-queue the node for assembling in case
474      we already proceeded it and ignored as not needed or got
475      a re-declaration in IMA mode.  */
476   if (node->reachable)
477     {
478       struct cgraph_node *n;
479
480       for (n = cgraph_nodes_queue; n; n = n->next_needed)
481         if (n == node)
482           break;
483       if (!n)
484         node->reachable = 0;
485     }
486 }
487
488 static void
489 cgraph_lower_function (struct cgraph_node *node)
490 {
491   if (node->lowered)
492     return;
493   tree_lowering_passes (node->decl);
494   node->lowered = true;
495 }
496
497 /* DECL has been parsed.  Take it, queue it, compile it at the whim of the
498    logic in effect.  If NESTED is true, then our caller cannot stand to have
499    the garbage collector run at the moment.  We would need to either create
500    a new GC context, or just not compile right now.  */
501
502 void
503 cgraph_finalize_function (tree decl, bool nested)
504 {
505   struct cgraph_node *node = cgraph_node (decl);
506
507   if (node->local.finalized)
508     cgraph_reset_node (node);
509
510   node->pid = cgraph_max_pid ++;
511   notice_global_symbol (decl);
512   node->local.finalized = true;
513   node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
514   record_cdtor_fn (node->decl);
515   if (node->nested)
516     lower_nested_functions (decl);
517   gcc_assert (!node->nested);
518
519   if (decide_is_function_needed (node, decl))
520     cgraph_mark_needed_node (node);
521
522   /* Since we reclaim unreachable nodes at the end of every language
523      level unit, we need to be conservative about possible entry points
524      there.  */
525   if ((TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl)))
526     cgraph_mark_reachable_node (node);
527
528   /* If we've not yet emitted decl, tell the debug info about it.  */
529   if (!TREE_ASM_WRITTEN (decl))
530     (*debug_hooks->deferred_inline_function) (decl);
531
532   /* Possibly warn about unused parameters.  */
533   if (warn_unused_parameter)
534     do_warn_unused_parameter (decl);
535
536   if (!nested)
537     ggc_collect ();
538 }
539
540 /* C99 extern inline keywords allow changing of declaration after function
541    has been finalized.  We need to re-decide if we want to mark the function as
542    needed then.   */
543
544 void
545 cgraph_mark_if_needed (tree decl)
546 {
547   struct cgraph_node *node = cgraph_node (decl);
548   if (node->local.finalized && decide_is_function_needed (node, decl))
549     cgraph_mark_needed_node (node);
550 }
551
552 /* Verify cgraph nodes of given cgraph node.  */
553 void
554 verify_cgraph_node (struct cgraph_node *node)
555 {
556   struct cgraph_edge *e;
557   struct cgraph_node *main_clone;
558   struct function *this_cfun = DECL_STRUCT_FUNCTION (node->decl);
559   struct function *saved_cfun = cfun;
560   basic_block this_block;
561   gimple_stmt_iterator gsi;
562   bool error_found = false;
563
564   if (errorcount || sorrycount)
565     return;
566
567   timevar_push (TV_CGRAPH_VERIFY);
568   /* debug_generic_stmt needs correct cfun */
569   set_cfun (this_cfun);
570   for (e = node->callees; e; e = e->next_callee)
571     if (e->aux)
572       {
573         error ("aux field set for edge %s->%s",
574                cgraph_node_name (e->caller), cgraph_node_name (e->callee));
575         error_found = true;
576       }
577   if (node->count < 0)
578     {
579       error ("Execution count is negative");
580       error_found = true;
581     }
582   for (e = node->callers; e; e = e->next_caller)
583     {
584       if (e->count < 0)
585         {
586           error ("caller edge count is negative");
587           error_found = true;
588         }
589       if (e->frequency < 0)
590         {
591           error ("caller edge frequency is negative");
592           error_found = true;
593         }
594       if (e->frequency > CGRAPH_FREQ_MAX)
595         {
596           error ("caller edge frequency is too large");
597           error_found = true;
598         }
599       if (!e->inline_failed)
600         {
601           if (node->global.inlined_to
602               != (e->caller->global.inlined_to
603                   ? e->caller->global.inlined_to : e->caller))
604             {
605               error ("inlined_to pointer is wrong");
606               error_found = true;
607             }
608           if (node->callers->next_caller)
609             {
610               error ("multiple inline callers");
611               error_found = true;
612             }
613         }
614       else
615         if (node->global.inlined_to)
616           {
617             error ("inlined_to pointer set for noninline callers");
618             error_found = true;
619           }
620     }
621   if (!node->callers && node->global.inlined_to)
622     {
623       error ("inlined_to pointer is set but no predecessors found");
624       error_found = true;
625     }
626   if (node->global.inlined_to == node)
627     {
628       error ("inlined_to pointer refers to itself");
629       error_found = true;
630     }
631
632   for (main_clone = cgraph_node (node->decl); main_clone;
633        main_clone = main_clone->next_clone)
634     if (main_clone == node)
635       break;
636   if (!cgraph_node (node->decl))
637     {
638       error ("node not found in cgraph_hash");
639       error_found = true;
640     }
641
642   if (node->analyzed
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)
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       /* If decl is a clone of an abstract function, mark that abstract
901          function so that we don't release its body. The DECL_INITIAL() of that
902          abstract function declaration will be later needed to output debug info.  */
903       if (DECL_ABSTRACT_ORIGIN (decl))
904         {
905           struct cgraph_node *origin_node = cgraph_node (DECL_ABSTRACT_ORIGIN (decl));
906           origin_node->abstract_and_needed = true;
907         }
908
909       /* We finalize local static variables during constructing callgraph
910          edges.  Process their attributes too.  */
911       process_function_and_variable_attributes (first_processed,
912                                                 first_analyzed_var);
913       first_processed = cgraph_nodes;
914       first_analyzed_var = varpool_nodes;
915       varpool_analyze_pending_decls ();
916       cgraph_process_new_functions ();
917     }
918
919   /* Collect entry points to the unit.  */
920   if (cgraph_dump_file)
921     {
922       fprintf (cgraph_dump_file, "Unit entry points:");
923       for (node = cgraph_nodes; node != first_analyzed; node = node->next)
924         if (node->needed)
925           fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
926       fprintf (cgraph_dump_file, "\n\nInitial ");
927       dump_cgraph (cgraph_dump_file);
928     }
929
930   if (cgraph_dump_file)
931     fprintf (cgraph_dump_file, "\nReclaiming functions:");
932
933   for (node = cgraph_nodes; node != first_analyzed; node = next)
934     {
935       tree decl = node->decl;
936       next = node->next;
937
938       if (node->local.finalized && !gimple_has_body_p (decl))
939         cgraph_reset_node (node);
940
941       if (!node->reachable && gimple_has_body_p (decl))
942         {
943           if (cgraph_dump_file)
944             fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
945           cgraph_remove_node (node);
946           continue;
947         }
948       else
949         node->next_needed = NULL;
950       gcc_assert (!node->local.finalized || gimple_has_body_p (decl));
951       gcc_assert (node->analyzed == node->local.finalized);
952     }
953   if (cgraph_dump_file)
954     {
955       fprintf (cgraph_dump_file, "\n\nReclaimed ");
956       dump_cgraph (cgraph_dump_file);
957     }
958   first_analyzed = cgraph_nodes;
959   ggc_collect ();
960 }
961
962 /* Analyze the whole compilation unit once it is parsed completely.  */
963
964 void
965 cgraph_finalize_compilation_unit (void)
966 {
967   if (errorcount || sorrycount)
968     return;
969
970   finish_aliases_1 ();
971
972   if (!quiet_flag)
973     {
974       fprintf (stderr, "\nAnalyzing compilation unit\n");
975       fflush (stderr);
976     }
977
978   timevar_push (TV_CGRAPH);
979   cgraph_analyze_functions ();
980   timevar_pop (TV_CGRAPH);
981 }
982
983
984 /* Figure out what functions we want to assemble.  */
985
986 static void
987 cgraph_mark_functions_to_output (void)
988 {
989   struct cgraph_node *node;
990
991   for (node = cgraph_nodes; node; node = node->next)
992     {
993       tree decl = node->decl;
994       struct cgraph_edge *e;
995
996       gcc_assert (!node->process);
997
998       for (e = node->callers; e; e = e->next_caller)
999         if (e->inline_failed)
1000           break;
1001
1002       /* We need to output all local functions that are used and not
1003          always inlined, as well as those that are reachable from
1004          outside the current compilation unit.  */
1005       if (node->analyzed
1006           && !node->global.inlined_to
1007           && (node->needed
1008               || (e && node->reachable))
1009           && !TREE_ASM_WRITTEN (decl)
1010           && !DECL_EXTERNAL (decl))
1011         node->process = 1;
1012       else
1013         {
1014           /* We should've reclaimed all functions that are not needed.  */
1015 #ifdef ENABLE_CHECKING
1016           if (!node->global.inlined_to
1017               && gimple_has_body_p (decl)
1018               && !DECL_EXTERNAL (decl))
1019             {
1020               dump_cgraph_node (stderr, node);
1021               internal_error ("failed to reclaim unneeded function");
1022             }
1023 #endif
1024           gcc_assert (node->global.inlined_to
1025                       || !gimple_has_body_p (decl)
1026                       || DECL_EXTERNAL (decl));
1027
1028         }
1029
1030     }
1031 }
1032
1033 /* Expand function specified by NODE.  */
1034
1035 static void
1036 cgraph_expand_function (struct cgraph_node *node)
1037 {
1038   tree decl = node->decl;
1039
1040   /* We ought to not compile any inline clones.  */
1041   gcc_assert (!node->global.inlined_to);
1042
1043   announce_function (decl);
1044   node->process = 0;
1045
1046   gcc_assert (node->lowered);
1047
1048   /* Generate RTL for the body of DECL.  */
1049   if (lang_hooks.callgraph.emit_associated_thunks)
1050     lang_hooks.callgraph.emit_associated_thunks (decl);
1051   tree_rest_of_compilation (decl);
1052
1053   /* Make sure that BE didn't give up on compiling.  */
1054   gcc_assert (TREE_ASM_WRITTEN (decl));
1055   current_function_decl = NULL;
1056   gcc_assert (!cgraph_preserve_function_body_p (decl));
1057   cgraph_release_function_body (node);
1058   /* Eliminate all call edges.  This is important so the GIMPLE_CALL no longer
1059      points to the dead function body.  */
1060   cgraph_node_remove_callees (node);
1061
1062   cgraph_function_flags_ready = true;
1063 }
1064
1065 /* Return true when CALLER_DECL should be inlined into CALLEE_DECL.  */
1066
1067 bool
1068 cgraph_inline_p (struct cgraph_edge *e, cgraph_inline_failed_t *reason)
1069 {
1070   *reason = e->inline_failed;
1071   return !e->inline_failed;
1072 }
1073
1074
1075
1076 /* Expand all functions that must be output.
1077
1078    Attempt to topologically sort the nodes so function is output when
1079    all called functions are already assembled to allow data to be
1080    propagated across the callgraph.  Use a stack to get smaller distance
1081    between a function and its callees (later we may choose to use a more
1082    sophisticated algorithm for function reordering; we will likely want
1083    to use subsections to make the output functions appear in top-down
1084    order).  */
1085
1086 static void
1087 cgraph_expand_all_functions (void)
1088 {
1089   struct cgraph_node *node;
1090   struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1091   int order_pos, new_order_pos = 0;
1092   int i;
1093
1094   order_pos = cgraph_postorder (order);
1095   gcc_assert (order_pos == cgraph_n_nodes);
1096
1097   /* Garbage collector may remove inline clones we eliminate during
1098      optimization.  So we must be sure to not reference them.  */
1099   for (i = 0; i < order_pos; i++)
1100     if (order[i]->process)
1101       order[new_order_pos++] = order[i];
1102
1103   for (i = new_order_pos - 1; i >= 0; i--)
1104     {
1105       node = order[i];
1106       if (node->process)
1107         {
1108           gcc_assert (node->reachable);
1109           node->process = 0;
1110           cgraph_expand_function (node);
1111         }
1112     }
1113   cgraph_process_new_functions ();
1114
1115   free (order);
1116
1117 }
1118
1119 /* This is used to sort the node types by the cgraph order number.  */
1120
1121 struct cgraph_order_sort
1122 {
1123   enum { ORDER_UNDEFINED = 0, ORDER_FUNCTION, ORDER_VAR, ORDER_ASM } kind;
1124   union
1125   {
1126     struct cgraph_node *f;
1127     struct varpool_node *v;
1128     struct cgraph_asm_node *a;
1129   } u;
1130 };
1131
1132 /* Output all functions, variables, and asm statements in the order
1133    according to their order fields, which is the order in which they
1134    appeared in the file.  This implements -fno-toplevel-reorder.  In
1135    this mode we may output functions and variables which don't really
1136    need to be output.  */
1137
1138 static void
1139 cgraph_output_in_order (void)
1140 {
1141   int max;
1142   size_t size;
1143   struct cgraph_order_sort *nodes;
1144   int i;
1145   struct cgraph_node *pf;
1146   struct varpool_node *pv;
1147   struct cgraph_asm_node *pa;
1148
1149   max = cgraph_order;
1150   size = max * sizeof (struct cgraph_order_sort);
1151   nodes = (struct cgraph_order_sort *) alloca (size);
1152   memset (nodes, 0, size);
1153
1154   varpool_analyze_pending_decls ();
1155
1156   for (pf = cgraph_nodes; pf; pf = pf->next)
1157     {
1158       if (pf->process)
1159         {
1160           i = pf->order;
1161           gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1162           nodes[i].kind = ORDER_FUNCTION;
1163           nodes[i].u.f = pf;
1164         }
1165     }
1166
1167   for (pv = varpool_nodes_queue; pv; pv = pv->next_needed)
1168     {
1169       i = pv->order;
1170       gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1171       nodes[i].kind = ORDER_VAR;
1172       nodes[i].u.v = pv;
1173     }
1174
1175   for (pa = cgraph_asm_nodes; pa; pa = pa->next)
1176     {
1177       i = pa->order;
1178       gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1179       nodes[i].kind = ORDER_ASM;
1180       nodes[i].u.a = pa;
1181     }
1182
1183   /* In toplevel reorder mode we output all statics; mark them as needed.  */
1184   for (i = 0; i < max; ++i)
1185     {
1186       if (nodes[i].kind == ORDER_VAR)
1187         {
1188           varpool_mark_needed_node (nodes[i].u.v);
1189         }
1190     }
1191   varpool_empty_needed_queue ();
1192
1193   for (i = 0; i < max; ++i)
1194     {
1195       switch (nodes[i].kind)
1196         {
1197         case ORDER_FUNCTION:
1198           nodes[i].u.f->process = 0;
1199           cgraph_expand_function (nodes[i].u.f);
1200           break;
1201
1202         case ORDER_VAR:
1203           varpool_assemble_decl (nodes[i].u.v);
1204           break;
1205
1206         case ORDER_ASM:
1207           assemble_asm (nodes[i].u.a->asm_str);
1208           break;
1209
1210         case ORDER_UNDEFINED:
1211           break;
1212
1213         default:
1214           gcc_unreachable ();
1215         }
1216     }
1217
1218   cgraph_asm_nodes = NULL;
1219 }
1220
1221 /* Return true when function body of DECL still needs to be kept around
1222    for later re-use.  */
1223 bool
1224 cgraph_preserve_function_body_p (tree decl)
1225 {
1226   struct cgraph_node *node;
1227
1228   gcc_assert (cgraph_global_info_ready);
1229   /* Look if there is any clone around.  */
1230   for (node = cgraph_node (decl); node; node = node->next_clone)
1231     if (node->global.inlined_to)
1232       return true;
1233   return false;
1234 }
1235
1236 static void
1237 ipa_passes (void)
1238 {
1239   set_cfun (NULL);
1240   current_function_decl = NULL;
1241   gimple_register_cfg_hooks ();
1242   bitmap_obstack_initialize (NULL);
1243   execute_ipa_pass_list (all_ipa_passes);
1244
1245   /* Generate coverage variables and constructors.  */
1246   coverage_finish ();
1247
1248   /* Process new functions added.  */
1249   set_cfun (NULL);
1250   current_function_decl = NULL;
1251   cgraph_process_new_functions ();
1252
1253   bitmap_obstack_release (NULL);
1254 }
1255
1256 /* Perform simple optimizations based on callgraph.  */
1257
1258 void
1259 cgraph_optimize (void)
1260 {
1261   if (errorcount || sorrycount)
1262     return;
1263
1264 #ifdef ENABLE_CHECKING
1265   verify_cgraph ();
1266 #endif
1267
1268   /* Call functions declared with the "constructor" or "destructor"
1269      attribute.  */
1270   cgraph_build_cdtor_fns ();
1271
1272   /* Frontend may output common variables after the unit has been finalized.
1273      It is safe to deal with them here as they are always zero initialized.  */
1274   varpool_analyze_pending_decls ();
1275   cgraph_analyze_functions ();
1276
1277   timevar_push (TV_CGRAPHOPT);
1278   if (pre_ipa_mem_report)
1279     {
1280       fprintf (stderr, "Memory consumption before IPA\n");
1281       dump_memory_report (false);
1282     }
1283   if (!quiet_flag)
1284     fprintf (stderr, "Performing interprocedural optimizations\n");
1285   cgraph_state = CGRAPH_STATE_IPA;
1286
1287   /* Don't run the IPA passes if there was any error or sorry messages.  */
1288   if (errorcount == 0 && sorrycount == 0)
1289     ipa_passes ();
1290
1291   /* This pass remove bodies of extern inline functions we never inlined.
1292      Do this later so other IPA passes see what is really going on.  */
1293   cgraph_remove_unreachable_nodes (false, dump_file);
1294   cgraph_global_info_ready = true;
1295   if (cgraph_dump_file)
1296     {
1297       fprintf (cgraph_dump_file, "Optimized ");
1298       dump_cgraph (cgraph_dump_file);
1299       dump_varpool (cgraph_dump_file);
1300     }
1301   if (post_ipa_mem_report)
1302     {
1303       fprintf (stderr, "Memory consumption after IPA\n");
1304       dump_memory_report (false);
1305     }
1306   timevar_pop (TV_CGRAPHOPT);
1307
1308   /* Output everything.  */
1309   if (!quiet_flag)
1310     fprintf (stderr, "Assembling functions:\n");
1311 #ifdef ENABLE_CHECKING
1312   verify_cgraph ();
1313 #endif
1314
1315   cgraph_mark_functions_to_output ();
1316
1317   cgraph_state = CGRAPH_STATE_EXPANSION;
1318   if (!flag_toplevel_reorder)
1319     cgraph_output_in_order ();
1320   else
1321     {
1322       cgraph_output_pending_asms ();
1323
1324       cgraph_expand_all_functions ();
1325       varpool_remove_unreferenced_decls ();
1326
1327       varpool_assemble_pending_decls ();
1328     }
1329   cgraph_process_new_functions ();
1330   cgraph_state = CGRAPH_STATE_FINISHED;
1331
1332   if (cgraph_dump_file)
1333     {
1334       fprintf (cgraph_dump_file, "\nFinal ");
1335       dump_cgraph (cgraph_dump_file);
1336     }
1337 #ifdef ENABLE_CHECKING
1338   verify_cgraph ();
1339   /* Double check that all inline clones are gone and that all
1340      function bodies have been released from memory.  */
1341   if (!(sorrycount || errorcount))
1342     {
1343       struct cgraph_node *node;
1344       bool error_found = false;
1345
1346       for (node = cgraph_nodes; node; node = node->next)
1347         if (node->analyzed
1348             && (node->global.inlined_to
1349                 || gimple_has_body_p (node->decl)))
1350           {
1351             error_found = true;
1352             dump_cgraph_node (stderr, node);
1353           }
1354       if (error_found)
1355         internal_error ("nodes with unreleased memory found");
1356     }
1357 #endif
1358 }
1359 /* Generate and emit a static constructor or destructor.  WHICH must
1360    be one of 'I' (for a constructor) or 'D' (for a destructor).  BODY
1361    is a STATEMENT_LIST containing GENERIC statements.  PRIORITY is the
1362    initialization priority for this constructor or destructor.  */
1363
1364 void
1365 cgraph_build_static_cdtor (char which, tree body, int priority)
1366 {
1367   static int counter = 0;
1368   char which_buf[16];
1369   tree decl, name, resdecl;
1370
1371   /* The priority is encoded in the constructor or destructor name.
1372      collect2 will sort the names and arrange that they are called at
1373      program startup.  */
1374   sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
1375   name = get_file_function_name (which_buf);
1376
1377   decl = build_decl (FUNCTION_DECL, name,
1378                      build_function_type (void_type_node, void_list_node));
1379   current_function_decl = decl;
1380
1381   resdecl = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
1382   DECL_ARTIFICIAL (resdecl) = 1;
1383   DECL_RESULT (decl) = resdecl;
1384   DECL_CONTEXT (resdecl) = decl;
1385
1386   allocate_struct_function (decl, false);
1387
1388   TREE_STATIC (decl) = 1;
1389   TREE_USED (decl) = 1;
1390   DECL_ARTIFICIAL (decl) = 1;
1391   DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1392   DECL_SAVED_TREE (decl) = body;
1393   TREE_PUBLIC (decl) = ! targetm.have_ctors_dtors;
1394   DECL_UNINLINABLE (decl) = 1;
1395
1396   DECL_INITIAL (decl) = make_node (BLOCK);
1397   TREE_USED (DECL_INITIAL (decl)) = 1;
1398
1399   DECL_SOURCE_LOCATION (decl) = input_location;
1400   cfun->function_end_locus = input_location;
1401
1402   switch (which)
1403     {
1404     case 'I':
1405       DECL_STATIC_CONSTRUCTOR (decl) = 1;
1406       decl_init_priority_insert (decl, priority);
1407       break;
1408     case 'D':
1409       DECL_STATIC_DESTRUCTOR (decl) = 1;
1410       decl_fini_priority_insert (decl, priority);
1411       break;
1412     default:
1413       gcc_unreachable ();
1414     }
1415
1416   gimplify_function_tree (decl);
1417
1418   cgraph_add_new_function (decl, false);
1419   cgraph_mark_needed_node (cgraph_node (decl));
1420   set_cfun (NULL);
1421 }
1422
1423 void
1424 init_cgraph (void)
1425 {
1426   cgraph_dump_file = dump_begin (TDI_cgraph, NULL);
1427 }
1428
1429 /* The edges representing the callers of the NEW_VERSION node were
1430    fixed by cgraph_function_versioning (), now the call_expr in their
1431    respective tree code should be updated to call the NEW_VERSION.  */
1432
1433 static void
1434 update_call_expr (struct cgraph_node *new_version)
1435 {
1436   struct cgraph_edge *e;
1437
1438   gcc_assert (new_version);
1439
1440   /* Update the call expr on the edges to call the new version.  */
1441   for (e = new_version->callers; e; e = e->next_caller)
1442     {
1443       struct function *inner_function = DECL_STRUCT_FUNCTION (e->caller->decl);
1444       gimple_call_set_fndecl (e->call_stmt, new_version->decl);
1445       /* Update EH information too, just in case.  */
1446       if (!stmt_could_throw_p (e->call_stmt)
1447           && lookup_stmt_eh_region_fn (inner_function, e->call_stmt))
1448         remove_stmt_from_eh_region_fn (inner_function, e->call_stmt);
1449     }
1450 }
1451
1452
1453 /* Create a new cgraph node which is the new version of
1454    OLD_VERSION node.  REDIRECT_CALLERS holds the callers
1455    edges which should be redirected to point to
1456    NEW_VERSION.  ALL the callees edges of OLD_VERSION
1457    are cloned to the new version node.  Return the new
1458    version node.  */
1459
1460 static struct cgraph_node *
1461 cgraph_copy_node_for_versioning (struct cgraph_node *old_version,
1462                                  tree new_decl,
1463                                  VEC(cgraph_edge_p,heap) *redirect_callers)
1464  {
1465    struct cgraph_node *new_version;
1466    struct cgraph_edge *e, *new_e;
1467    struct cgraph_edge *next_callee;
1468    unsigned i;
1469
1470    gcc_assert (old_version);
1471
1472    new_version = cgraph_node (new_decl);
1473
1474    new_version->analyzed = true;
1475    new_version->local = old_version->local;
1476    new_version->global = old_version->global;
1477    new_version->rtl = new_version->rtl;
1478    new_version->reachable = true;
1479    new_version->count = old_version->count;
1480
1481    /* Clone the old node callees.  Recursive calls are
1482       also cloned.  */
1483    for (e = old_version->callees;e; e=e->next_callee)
1484      {
1485        new_e = cgraph_clone_edge (e, new_version, e->call_stmt, 0, e->frequency,
1486                                   e->loop_nest, true);
1487        new_e->count = e->count;
1488      }
1489    /* Fix recursive calls.
1490       If OLD_VERSION has a recursive call after the
1491       previous edge cloning, the new version will have an edge
1492       pointing to the old version, which is wrong;
1493       Redirect it to point to the new version. */
1494    for (e = new_version->callees ; e; e = next_callee)
1495      {
1496        next_callee = e->next_callee;
1497        if (e->callee == old_version)
1498          cgraph_redirect_edge_callee (e, new_version);
1499
1500        if (!next_callee)
1501          break;
1502      }
1503    for (i = 0; VEC_iterate (cgraph_edge_p, redirect_callers, i, e); i++)
1504      {
1505        /* Redirect calls to the old version node to point to its new
1506           version.  */
1507        cgraph_redirect_edge_callee (e, new_version);
1508      }
1509
1510    return new_version;
1511  }
1512
1513  /* Perform function versioning.
1514     Function versioning includes copying of the tree and
1515     a callgraph update (creating a new cgraph node and updating
1516     its callees and callers).
1517
1518     REDIRECT_CALLERS varray includes the edges to be redirected
1519     to the new version.
1520
1521     TREE_MAP is a mapping of tree nodes we want to replace with
1522     new ones (according to results of prior analysis).
1523     OLD_VERSION_NODE is the node that is versioned.
1524     It returns the new version's cgraph node. 
1525     ARGS_TO_SKIP lists arguments to be omitted from functions
1526     */
1527
1528 struct cgraph_node *
1529 cgraph_function_versioning (struct cgraph_node *old_version_node,
1530                             VEC(cgraph_edge_p,heap) *redirect_callers,
1531                             varray_type tree_map,
1532                             bitmap args_to_skip)
1533 {
1534   tree old_decl = old_version_node->decl;
1535   struct cgraph_node *new_version_node = NULL;
1536   tree new_decl;
1537
1538   if (!tree_versionable_function_p (old_decl))
1539     return NULL;
1540
1541   /* Make a new FUNCTION_DECL tree node for the
1542      new version. */
1543   if (!args_to_skip)
1544     new_decl = copy_node (old_decl);
1545   else
1546     new_decl = build_function_decl_skip_args (old_decl, args_to_skip);
1547
1548   /* Create the new version's call-graph node.
1549      and update the edges of the new node. */
1550   new_version_node =
1551     cgraph_copy_node_for_versioning (old_version_node, new_decl,
1552                                      redirect_callers);
1553
1554   /* Copy the OLD_VERSION_NODE function tree to the new version.  */
1555   tree_function_versioning (old_decl, new_decl, tree_map, false, args_to_skip);
1556
1557   /* Update the new version's properties.
1558      Make The new version visible only within this translation unit.  Make sure
1559      that is not weak also.
1560      ??? We cannot use COMDAT linkage because there is no
1561      ABI support for this.  */
1562   DECL_EXTERNAL (new_version_node->decl) = 0;
1563   DECL_ONE_ONLY (new_version_node->decl) = 0;
1564   TREE_PUBLIC (new_version_node->decl) = 0;
1565   DECL_COMDAT (new_version_node->decl) = 0;
1566   DECL_WEAK (new_version_node->decl) = 0;
1567   DECL_VIRTUAL_P (new_version_node->decl) = 0;
1568   new_version_node->local.externally_visible = 0;
1569   new_version_node->local.local = 1;
1570   new_version_node->lowered = true;
1571
1572   /* Update the call_expr on the edges to call the new version node. */
1573   update_call_expr (new_version_node);
1574   
1575   cgraph_call_function_insertion_hooks (new_version_node);
1576   return new_version_node;
1577 }
1578
1579 /* Produce separate function body for inline clones so the offline copy can be
1580    modified without affecting them.  */
1581 struct cgraph_node *
1582 save_inline_function_body (struct cgraph_node *node)
1583 {
1584   struct cgraph_node *first_clone;
1585
1586   gcc_assert (node == cgraph_node (node->decl));
1587
1588   cgraph_lower_function (node);
1589
1590   first_clone = node->next_clone;
1591
1592   first_clone->decl = copy_node (node->decl);
1593   node->next_clone = NULL;
1594   first_clone->prev_clone = NULL;
1595   cgraph_insert_node_to_hashtable (first_clone);
1596   gcc_assert (first_clone == cgraph_node (first_clone->decl));
1597
1598   /* Copy the OLD_VERSION_NODE function tree to the new version.  */
1599   tree_function_versioning (node->decl, first_clone->decl, NULL, true, NULL);
1600
1601   DECL_EXTERNAL (first_clone->decl) = 0;
1602   DECL_ONE_ONLY (first_clone->decl) = 0;
1603   TREE_PUBLIC (first_clone->decl) = 0;
1604   DECL_COMDAT (first_clone->decl) = 0;
1605
1606   for (node = first_clone->next_clone; node; node = node->next_clone)
1607     node->decl = first_clone->decl;
1608 #ifdef ENABLE_CHECKING
1609   verify_cgraph_node (first_clone);
1610 #endif
1611   return first_clone;
1612 }
1613
1614 #include "gt-cgraphunit.h"