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