OSDN Git Service

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