OSDN Git Service

2011-08-17 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / cgraphunit.c
1 /* Callgraph based interprocedural optimizations.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
3    2011 Free Software Foundation, Inc.
4    Contributed by Jan Hubicka
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 /* This module implements main driver of compilation process as well as
23    few basic interprocedural optimizers.
24
25    The main scope of this file is to act as an interface in between
26    tree based frontends and the backend (and middle end)
27
28    The front-end is supposed to use following functionality:
29
30     - cgraph_finalize_function
31
32       This function is called once front-end has parsed whole body of function
33       and it is certain that the function body nor the declaration will change.
34
35       (There is one exception needed for implementing GCC extern inline
36         function.)
37
38     - varpool_finalize_variable
39
40       This function has same behavior as the above but is used for static
41       variables.
42
43     - cgraph_finalize_compilation_unit
44
45       This function is called once (source level) compilation unit is finalized
46       and it will no longer change.
47
48       In the call-graph construction and local function analysis takes
49       place here.  Bodies of unreachable functions are released to
50       conserve memory usage.
51
52       The function can be called multiple times when multiple source level
53       compilation units are combined (such as in C frontend)
54
55     - cgraph_optimize
56
57       In this unit-at-a-time compilation the intra procedural analysis takes
58       place here.  In particular the static functions whose address is never
59       taken are marked as local.  Backend can then use this information to
60       modify calling conventions, do better inlining or similar optimizations.
61
62     - cgraph_mark_needed_node
63     - varpool_mark_needed_node
64
65       When function or variable is referenced by some hidden way the call-graph
66       data structure must be updated accordingly by this function.
67       There should be little need to call this function and all the references
68       should be made explicit to cgraph code.  At present these functions are
69       used by C++ frontend to explicitly mark the keyed methods.
70
71     - analyze_expr callback
72
73       This function is responsible for lowering tree nodes not understood by
74       generic code into understandable ones or alternatively marking
75       callgraph and varpool nodes referenced by the as needed.
76
77       ??? On the tree-ssa genericizing should take place here and we will avoid
78       need for these hooks (replacing them by genericizing hook)
79
80         Analyzing of all functions is deferred
81         to cgraph_finalize_compilation_unit and expansion into cgraph_optimize.
82
83         In cgraph_finalize_compilation_unit the reachable functions are
84         analyzed.  During analysis the call-graph edges from reachable
85         functions are constructed and their destinations are marked as
86         reachable.  References to functions and variables are discovered too
87         and variables found to be needed output to the assembly file.  Via
88         mark_referenced call in assemble_variable functions referenced by
89         static variables are noticed too.
90
91         The intra-procedural information is produced and its existence
92         indicated by global_info_ready.  Once this flag is set it is impossible
93         to change function from !reachable to reachable and thus
94         assemble_variable no longer call mark_referenced.
95
96         Finally the call-graph is topologically sorted and all reachable functions
97         that has not been completely inlined or are not external are output.
98
99         ??? It is possible that reference to function or variable is optimized
100         out.  We can not deal with this nicely because topological order is not
101         suitable for it.  For tree-ssa we may consider another pass doing
102         optimization and re-discovering reachable functions.
103
104         ??? Reorganize code so variables are output very last and only if they
105         really has been referenced by produced code, so we catch more cases
106         where reference has been optimized out.  */
107
108
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "tm.h"
113 #include "tree.h"
114 #include "rtl.h"
115 #include "tree-flow.h"
116 #include "tree-inline.h"
117 #include "langhooks.h"
118 #include "pointer-set.h"
119 #include "toplev.h"
120 #include "flags.h"
121 #include "ggc.h"
122 #include "debug.h"
123 #include "target.h"
124 #include "cgraph.h"
125 #include "diagnostic.h"
126 #include "tree-pretty-print.h"
127 #include "gimple-pretty-print.h"
128 #include "timevar.h"
129 #include "params.h"
130 #include "fibheap.h"
131 #include "intl.h"
132 #include "function.h"
133 #include "ipa-prop.h"
134 #include "gimple.h"
135 #include "tree-iterator.h"
136 #include "tree-pass.h"
137 #include "tree-dump.h"
138 #include "output.h"
139 #include "coverage.h"
140 #include "plugin.h"
141 #include "ipa-inline.h"
142 #include "ipa-utils.h"
143 #include "lto-streamer.h"
144
145 static void cgraph_expand_all_functions (void);
146 static void cgraph_mark_functions_to_output (void);
147 static void cgraph_expand_function (struct cgraph_node *);
148 static void cgraph_output_pending_asms (void);
149
150 FILE *cgraph_dump_file;
151
152 /* Used for vtable lookup in thunk adjusting.  */
153 static GTY (()) tree vtable_entry_type;
154
155 /* Determine if function DECL is needed.  That is, visible to something
156    either outside this translation unit, something magic in the system
157    configury.  */
158
159 bool
160 cgraph_decide_is_function_needed (struct cgraph_node *node, tree decl)
161 {
162   /* If the user told us it is used, then it must be so.  */
163   if (node->local.externally_visible)
164     return true;
165
166   /* ??? If the assembler name is set by hand, it is possible to assemble
167      the name later after finalizing the function and the fact is noticed
168      in assemble_name then.  This is arguably a bug.  */
169   if (DECL_ASSEMBLER_NAME_SET_P (decl)
170       && (!node->thunk.thunk_p && !node->same_body_alias)
171       && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
172     return true;
173
174   /* With -fkeep-inline-functions we are keeping all inline functions except
175      for extern inline ones.  */
176   if (flag_keep_inline_functions
177       && DECL_DECLARED_INLINE_P (decl)
178       && !DECL_EXTERNAL (decl)
179       && !DECL_DISREGARD_INLINE_LIMITS (decl))
180      return true;
181
182   /* If we decided it was needed before, but at the time we didn't have
183      the body of the function available, then it's still needed.  We have
184      to go back and re-check its dependencies now.  */
185   if (node->needed)
186     return true;
187
188   /* Externally visible functions must be output.  The exception is
189      COMDAT functions that must be output only when they are needed.
190
191      When not optimizing, also output the static functions. (see
192      PR24561), but don't do so for always_inline functions, functions
193      declared inline and nested functions.  These were optimized out
194      in the original implementation and it is unclear whether we want
195      to change the behavior here.  */
196   if (((TREE_PUBLIC (decl)
197         || (!optimize
198             && !DECL_DISREGARD_INLINE_LIMITS (decl)
199             && !DECL_DECLARED_INLINE_P (decl)
200             && !(DECL_CONTEXT (decl)
201                  && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)))
202        && !flag_whole_program
203        && !flag_lto)
204       && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
205     return true;
206
207   return false;
208 }
209
210 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
211    functions into callgraph in a way so they look like ordinary reachable
212    functions inserted into callgraph already at construction time.  */
213
214 bool
215 cgraph_process_new_functions (void)
216 {
217   bool output = false;
218   tree fndecl;
219   struct cgraph_node *node;
220
221   varpool_analyze_pending_decls ();
222   /*  Note that this queue may grow as its being processed, as the new
223       functions may generate new ones.  */
224   while (cgraph_new_nodes)
225     {
226       node = cgraph_new_nodes;
227       fndecl = node->decl;
228       cgraph_new_nodes = cgraph_new_nodes->next_needed;
229       switch (cgraph_state)
230         {
231         case CGRAPH_STATE_CONSTRUCTION:
232           /* At construction time we just need to finalize function and move
233              it into reachable functions list.  */
234
235           node->next_needed = NULL;
236           cgraph_finalize_function (fndecl, false);
237           cgraph_mark_reachable_node (node);
238           output = true;
239           cgraph_call_function_insertion_hooks (node);
240           break;
241
242         case CGRAPH_STATE_IPA:
243         case CGRAPH_STATE_IPA_SSA:
244           /* When IPA optimization already started, do all essential
245              transformations that has been already performed on the whole
246              cgraph but not on this function.  */
247
248           gimple_register_cfg_hooks ();
249           if (!node->analyzed)
250             cgraph_analyze_function (node);
251           push_cfun (DECL_STRUCT_FUNCTION (fndecl));
252           current_function_decl = fndecl;
253           if ((cgraph_state == CGRAPH_STATE_IPA_SSA
254               && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
255               /* When not optimizing, be sure we run early local passes anyway
256                  to expand OMP.  */
257               || !optimize)
258             execute_pass_list (pass_early_local_passes.pass.sub);
259           else
260             compute_inline_parameters (node, true);
261           free_dominance_info (CDI_POST_DOMINATORS);
262           free_dominance_info (CDI_DOMINATORS);
263           pop_cfun ();
264           current_function_decl = NULL;
265           cgraph_call_function_insertion_hooks (node);
266           break;
267
268         case CGRAPH_STATE_EXPANSION:
269           /* Functions created during expansion shall be compiled
270              directly.  */
271           node->process = 0;
272           cgraph_call_function_insertion_hooks (node);
273           cgraph_expand_function (node);
274           break;
275
276         default:
277           gcc_unreachable ();
278           break;
279         }
280       varpool_analyze_pending_decls ();
281     }
282   return output;
283 }
284
285 /* As an GCC extension we allow redefinition of the function.  The
286    semantics when both copies of bodies differ is not well defined.
287    We replace the old body with new body so in unit at a time mode
288    we always use new body, while in normal mode we may end up with
289    old body inlined into some functions and new body expanded and
290    inlined in others.
291
292    ??? It may make more sense to use one body for inlining and other
293    body for expanding the function but this is difficult to do.  */
294
295 static void
296 cgraph_reset_node (struct cgraph_node *node)
297 {
298   /* If node->process is set, then we have already begun whole-unit analysis.
299      This is *not* testing for whether we've already emitted the function.
300      That case can be sort-of legitimately seen with real function redefinition
301      errors.  I would argue that the front end should never present us with
302      such a case, but don't enforce that for now.  */
303   gcc_assert (!node->process);
304
305   /* Reset our data structures so we can analyze the function again.  */
306   memset (&node->local, 0, sizeof (node->local));
307   memset (&node->global, 0, sizeof (node->global));
308   memset (&node->rtl, 0, sizeof (node->rtl));
309   node->analyzed = false;
310   node->local.finalized = false;
311
312   cgraph_node_remove_callees (node);
313 }
314
315 static void
316 cgraph_lower_function (struct cgraph_node *node)
317 {
318   if (node->lowered)
319     return;
320
321   if (node->nested)
322     lower_nested_functions (node->decl);
323   gcc_assert (!node->nested);
324
325   tree_lowering_passes (node->decl);
326   node->lowered = true;
327 }
328
329 /* DECL has been parsed.  Take it, queue it, compile it at the whim of the
330    logic in effect.  If NESTED is true, then our caller cannot stand to have
331    the garbage collector run at the moment.  We would need to either create
332    a new GC context, or just not compile right now.  */
333
334 void
335 cgraph_finalize_function (tree decl, bool nested)
336 {
337   struct cgraph_node *node = cgraph_get_create_node (decl);
338
339   if (node->local.finalized)
340     {
341       cgraph_reset_node (node);
342       node->local.redefined_extern_inline = true;
343     }
344
345   notice_global_symbol (decl);
346   node->local.finalized = true;
347   node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
348
349   if (cgraph_decide_is_function_needed (node, decl))
350     cgraph_mark_needed_node (node);
351
352   /* Since we reclaim unreachable nodes at the end of every language
353      level unit, we need to be conservative about possible entry points
354      there.  */
355   if ((TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
356       || DECL_STATIC_CONSTRUCTOR (decl)
357       || DECL_STATIC_DESTRUCTOR (decl)
358       /* COMDAT virtual functions may be referenced by vtable from
359          other compilation unit.  Still we want to devirtualize calls
360          to those so we need to analyze them.
361          FIXME: We should introduce may edges for this purpose and update
362          their handling in unreachable function removal and inliner too.  */
363       || (DECL_VIRTUAL_P (decl)
364           && optimize && (DECL_COMDAT (decl) || DECL_EXTERNAL (decl))))
365     cgraph_mark_reachable_node (node);
366
367   /* If we've not yet emitted decl, tell the debug info about it.  */
368   if (!TREE_ASM_WRITTEN (decl))
369     (*debug_hooks->deferred_inline_function) (decl);
370
371   /* Possibly warn about unused parameters.  */
372   if (warn_unused_parameter)
373     do_warn_unused_parameter (decl);
374
375   if (!nested)
376     ggc_collect ();
377 }
378
379 /* C99 extern inline keywords allow changing of declaration after function
380    has been finalized.  We need to re-decide if we want to mark the function as
381    needed then.   */
382
383 void
384 cgraph_mark_if_needed (tree decl)
385 {
386   struct cgraph_node *node = cgraph_get_node (decl);
387   if (node->local.finalized && cgraph_decide_is_function_needed (node, decl))
388     cgraph_mark_needed_node (node);
389 }
390
391 /* Return TRUE if NODE2 is equivalent to NODE or its clone.  */
392 static bool
393 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
394 {
395   node = cgraph_function_or_thunk_node (node, NULL);
396   node2 = cgraph_function_or_thunk_node (node2, NULL);
397   while (node != node2 && node2)
398     node2 = node2->clone_of;
399   return node2 != NULL;
400 }
401
402 /* Verify edge E count and frequency.  */
403
404 static bool
405 verify_edge_count_and_frequency (struct cgraph_edge *e)
406 {
407   bool error_found = false;
408   if (e->count < 0)
409     {
410       error ("caller edge count is negative");
411       error_found = true;
412     }
413   if (e->frequency < 0)
414     {
415       error ("caller edge frequency is negative");
416       error_found = true;
417     }
418   if (e->frequency > CGRAPH_FREQ_MAX)
419     {
420       error ("caller edge frequency is too large");
421       error_found = true;
422     }
423   if (gimple_has_body_p (e->caller->decl)
424       && !e->caller->global.inlined_to
425       /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
426          Remove this once edges are actualy removed from the function at that time.  */
427       && (e->frequency
428           || (inline_edge_summary_vec
429               && !inline_edge_summary (e)->predicate))
430       && (e->frequency
431           != compute_call_stmt_bb_frequency (e->caller->decl,
432                                              gimple_bb (e->call_stmt))))
433     {
434       error ("caller edge frequency %i does not match BB frequency %i",
435              e->frequency,
436              compute_call_stmt_bb_frequency (e->caller->decl,
437                                              gimple_bb (e->call_stmt)));
438       error_found = true;
439     }
440   return error_found;
441 }
442
443 /* Switch to THIS_CFUN if needed and print STMT to stderr.  */
444 static void
445 cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
446 {
447   /* debug_gimple_stmt needs correct cfun */
448   if (cfun != this_cfun)
449     set_cfun (this_cfun);
450   debug_gimple_stmt (stmt);
451 }
452
453 /* Verify that call graph edge E corresponds to DECL from the associated
454    statement.  Return true if the verification should fail.  */
455
456 static bool
457 verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl)
458 {
459   struct cgraph_node *node;
460
461   if (!decl || e->callee->global.inlined_to)
462     return false;
463   node = cgraph_get_node (decl);
464
465   /* We do not know if a node from a different partition is an alias or what it
466      aliases and therefore cannot do the former_clone_of check reliably.  */
467   if (!node || node->in_other_partition)
468     return false;
469   node = cgraph_function_or_thunk_node (node, NULL);
470
471   if ((e->callee->former_clone_of != node->decl)
472       /* IPA-CP sometimes redirect edge to clone and then back to the former
473          function.  This ping-pong has to go, eventaully.  */
474       && (node != cgraph_function_or_thunk_node (e->callee, NULL))
475       && !clone_of_p (node, e->callee))
476     return true;
477   else
478     return false;
479 }
480
481 /* Verify cgraph nodes of given cgraph node.  */
482 DEBUG_FUNCTION void
483 verify_cgraph_node (struct cgraph_node *node)
484 {
485   struct cgraph_edge *e;
486   struct function *this_cfun = DECL_STRUCT_FUNCTION (node->decl);
487   basic_block this_block;
488   gimple_stmt_iterator gsi;
489   bool error_found = false;
490
491   if (seen_error ())
492     return;
493
494   timevar_push (TV_CGRAPH_VERIFY);
495   for (e = node->callees; e; e = e->next_callee)
496     if (e->aux)
497       {
498         error ("aux field set for edge %s->%s",
499                identifier_to_locale (cgraph_node_name (e->caller)),
500                identifier_to_locale (cgraph_node_name (e->callee)));
501         error_found = true;
502       }
503   if (node->count < 0)
504     {
505       error ("execution count is negative");
506       error_found = true;
507     }
508   if (node->global.inlined_to && node->local.externally_visible)
509     {
510       error ("externally visible inline clone");
511       error_found = true;
512     }
513   if (node->global.inlined_to && node->address_taken)
514     {
515       error ("inline clone with address taken");
516       error_found = true;
517     }
518   if (node->global.inlined_to && node->needed)
519     {
520       error ("inline clone is needed");
521       error_found = true;
522     }
523   for (e = node->indirect_calls; e; e = e->next_callee)
524     {
525       if (e->aux)
526         {
527           error ("aux field set for indirect edge from %s",
528                  identifier_to_locale (cgraph_node_name (e->caller)));
529           error_found = true;
530         }
531       if (!e->indirect_unknown_callee
532           || !e->indirect_info)
533         {
534           error ("An indirect edge from %s is not marked as indirect or has "
535                  "associated indirect_info, the corresponding statement is: ",
536                  identifier_to_locale (cgraph_node_name (e->caller)));
537           cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
538           error_found = true;
539         }
540     }
541   for (e = node->callers; e; e = e->next_caller)
542     {
543       if (verify_edge_count_and_frequency (e))
544         error_found = true;
545       if (!e->inline_failed)
546         {
547           if (node->global.inlined_to
548               != (e->caller->global.inlined_to
549                   ? e->caller->global.inlined_to : e->caller))
550             {
551               error ("inlined_to pointer is wrong");
552               error_found = true;
553             }
554           if (node->callers->next_caller)
555             {
556               error ("multiple inline callers");
557               error_found = true;
558             }
559         }
560       else
561         if (node->global.inlined_to)
562           {
563             error ("inlined_to pointer set for noninline callers");
564             error_found = true;
565           }
566     }
567   for (e = node->indirect_calls; e; e = e->next_callee)
568     if (verify_edge_count_and_frequency (e))
569       error_found = true;
570   if (!node->callers && node->global.inlined_to)
571     {
572       error ("inlined_to pointer is set but no predecessors found");
573       error_found = true;
574     }
575   if (node->global.inlined_to == node)
576     {
577       error ("inlined_to pointer refers to itself");
578       error_found = true;
579     }
580
581   if (!cgraph_get_node (node->decl))
582     {
583       error ("node not found in cgraph_hash");
584       error_found = true;
585     }
586
587   if (node->clone_of)
588     {
589       struct cgraph_node *n;
590       for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
591         if (n == node)
592           break;
593       if (!n)
594         {
595           error ("node has wrong clone_of");
596           error_found = true;
597         }
598     }
599   if (node->clones)
600     {
601       struct cgraph_node *n;
602       for (n = node->clones; n; n = n->next_sibling_clone)
603         if (n->clone_of != node)
604           break;
605       if (n)
606         {
607           error ("node has wrong clone list");
608           error_found = true;
609         }
610     }
611   if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
612     {
613        error ("node is in clone list but it is not clone");
614        error_found = true;
615     }
616   if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
617     {
618       error ("node has wrong prev_clone pointer");
619       error_found = true;
620     }
621   if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
622     {
623       error ("double linked list of clones corrupted");
624       error_found = true;
625     }
626   if (node->same_comdat_group)
627     {
628       struct cgraph_node *n = node->same_comdat_group;
629
630       if (!DECL_ONE_ONLY (node->decl))
631         {
632           error ("non-DECL_ONE_ONLY node in a same_comdat_group list");
633           error_found = true;
634         }
635       if (n == node)
636         {
637           error ("node is alone in a comdat group");
638           error_found = true;
639         }
640       do
641         {
642           if (!n->same_comdat_group)
643             {
644               error ("same_comdat_group is not a circular list");
645               error_found = true;
646               break;
647             }
648           n = n->same_comdat_group;
649         }
650       while (n != node);
651     }
652
653   if (node->analyzed && node->alias)
654     {
655       bool ref_found = false;
656       int i;
657       struct ipa_ref *ref;
658
659       if (node->callees)
660         {
661           error ("Alias has call edges");
662           error_found = true;
663         }
664       for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
665         if (ref->use != IPA_REF_ALIAS)
666           {
667             error ("Alias has non-alias refernece");
668             error_found = true;
669           }
670         else if (ref_found)
671           {
672             error ("Alias has more than one alias reference");
673             error_found = true;
674           }
675         else
676           ref_found = true;
677         if (!ref_found)
678           {
679             error ("Analyzed alias has no reference");
680             error_found = true;
681           }
682     }
683   if (node->analyzed && node->thunk.thunk_p)
684     {
685       if (!node->callees)
686         {
687           error ("No edge out of thunk node");
688           error_found = true;
689         }
690       else if (node->callees->next_callee)
691         {
692           error ("More than one edge out of thunk node");
693           error_found = true;
694         }
695       if (gimple_has_body_p (node->decl))
696         {
697           error ("Thunk is not supposed to have body");
698           error_found = true;
699         }
700     }
701   else if (node->analyzed && gimple_has_body_p (node->decl)
702            && !TREE_ASM_WRITTEN (node->decl)
703            && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to)
704            && !flag_wpa)
705     {
706       if (this_cfun->cfg)
707         {
708           /* The nodes we're interested in are never shared, so walk
709              the tree ignoring duplicates.  */
710           struct pointer_set_t *visited_nodes = pointer_set_create ();
711           /* Reach the trees by walking over the CFG, and note the
712              enclosing basic-blocks in the call edges.  */
713           FOR_EACH_BB_FN (this_block, this_cfun)
714             for (gsi = gsi_start_bb (this_block);
715                  !gsi_end_p (gsi);
716                  gsi_next (&gsi))
717               {
718                 gimple stmt = gsi_stmt (gsi);
719                 if (is_gimple_call (stmt))
720                   {
721                     struct cgraph_edge *e = cgraph_edge (node, stmt);
722                     tree decl = gimple_call_fndecl (stmt);
723                     if (e)
724                       {
725                         if (e->aux)
726                           {
727                             error ("shared call_stmt:");
728                             cgraph_debug_gimple_stmt (this_cfun, stmt);
729                             error_found = true;
730                           }
731                         if (!e->indirect_unknown_callee)
732                           {
733                             if (verify_edge_corresponds_to_fndecl (e, decl))
734                               {
735                                 error ("edge points to wrong declaration:");
736                                 debug_tree (e->callee->decl);
737                                 fprintf (stderr," Instead of:");
738                                 debug_tree (decl);
739                                 error_found = true;
740                               }
741                           }
742                         else if (decl)
743                           {
744                             error ("an indirect edge with unknown callee "
745                                    "corresponding to a call_stmt with "
746                                    "a known declaration:");
747                             error_found = true;
748                             cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
749                           }
750                         e->aux = (void *)1;
751                       }
752                     else if (decl)
753                       {
754                         error ("missing callgraph edge for call stmt:");
755                         cgraph_debug_gimple_stmt (this_cfun, stmt);
756                         error_found = true;
757                       }
758                   }
759               }
760           pointer_set_destroy (visited_nodes);
761         }
762       else
763         /* No CFG available?!  */
764         gcc_unreachable ();
765
766       for (e = node->callees; e; e = e->next_callee)
767         {
768           if (!e->aux)
769             {
770               error ("edge %s->%s has no corresponding call_stmt",
771                      identifier_to_locale (cgraph_node_name (e->caller)),
772                      identifier_to_locale (cgraph_node_name (e->callee)));
773               cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
774               error_found = true;
775             }
776           e->aux = 0;
777         }
778       for (e = node->indirect_calls; e; e = e->next_callee)
779         {
780           if (!e->aux)
781             {
782               error ("an indirect edge from %s has no corresponding call_stmt",
783                      identifier_to_locale (cgraph_node_name (e->caller)));
784               cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
785               error_found = true;
786             }
787           e->aux = 0;
788         }
789     }
790   if (error_found)
791     {
792       dump_cgraph_node (stderr, node);
793       internal_error ("verify_cgraph_node failed");
794     }
795   timevar_pop (TV_CGRAPH_VERIFY);
796 }
797
798 /* Verify whole cgraph structure.  */
799 DEBUG_FUNCTION void
800 verify_cgraph (void)
801 {
802   struct cgraph_node *node;
803
804   if (seen_error ())
805     return;
806
807   for (node = cgraph_nodes; node; node = node->next)
808     verify_cgraph_node (node);
809 }
810
811 /* Output all asm statements we have stored up to be output.  */
812
813 static void
814 cgraph_output_pending_asms (void)
815 {
816   struct cgraph_asm_node *can;
817
818   if (seen_error ())
819     return;
820
821   for (can = cgraph_asm_nodes; can; can = can->next)
822     assemble_asm (can->asm_str);
823   cgraph_asm_nodes = NULL;
824 }
825
826 /* Analyze the function scheduled to be output.  */
827 void
828 cgraph_analyze_function (struct cgraph_node *node)
829 {
830   tree save = current_function_decl;
831   tree decl = node->decl;
832
833   if (node->alias && node->thunk.alias)
834     {
835       struct cgraph_node *tgt = cgraph_get_node (node->thunk.alias);
836       if (!VEC_length (ipa_ref_t, node->ref_list.references))
837         ipa_record_reference (node, NULL, tgt, NULL, IPA_REF_ALIAS, NULL);
838       if (node->same_body_alias)
839         { 
840           DECL_VIRTUAL_P (node->decl) = DECL_VIRTUAL_P (node->thunk.alias);
841           DECL_DECLARED_INLINE_P (node->decl)
842              = DECL_DECLARED_INLINE_P (node->thunk.alias);
843           DECL_DISREGARD_INLINE_LIMITS (node->decl)
844              = DECL_DISREGARD_INLINE_LIMITS (node->thunk.alias);
845         }
846
847       /* Fixup visibility nonsences C++ frontend produce on same body aliases.  */
848       if (TREE_PUBLIC (node->decl) && node->same_body_alias)
849         {
850           DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (node->thunk.alias);
851           if (DECL_ONE_ONLY (node->thunk.alias))
852             {
853               DECL_COMDAT (node->decl) = DECL_COMDAT (node->thunk.alias);
854               DECL_COMDAT_GROUP (node->decl) = DECL_COMDAT_GROUP (node->thunk.alias);
855               if (DECL_ONE_ONLY (node->thunk.alias) && !node->same_comdat_group)
856                 {
857                   struct cgraph_node *tgt = cgraph_get_node (node->thunk.alias);
858                   node->same_comdat_group = tgt;
859                   if (!tgt->same_comdat_group)
860                     tgt->same_comdat_group = node;
861                   else
862                     {
863                       struct cgraph_node *n;
864                       for (n = tgt->same_comdat_group;
865                            n->same_comdat_group != tgt;
866                            n = n->same_comdat_group)
867                         ;
868                       n->same_comdat_group = node;
869                     }
870                 }
871             }
872         }
873       cgraph_mark_reachable_node (cgraph_alias_aliased_node (node));
874       if (node->address_taken)
875         cgraph_mark_address_taken_node (cgraph_alias_aliased_node (node));
876       if (cgraph_decide_is_function_needed (node, node->decl))
877         cgraph_mark_needed_node (node);
878     }
879   else if (node->thunk.thunk_p)
880     {
881       cgraph_create_edge (node, cgraph_get_node (node->thunk.alias),
882                           NULL, 0, CGRAPH_FREQ_BASE);
883     }
884   else
885     {
886       current_function_decl = decl;
887       push_cfun (DECL_STRUCT_FUNCTION (decl));
888
889       assign_assembler_name_if_neeeded (node->decl);
890
891       /* Make sure to gimplify bodies only once.  During analyzing a
892          function we lower it, which will require gimplified nested
893          functions, so we can end up here with an already gimplified
894          body.  */
895       if (!gimple_body (decl))
896         gimplify_function_tree (decl);
897       dump_function (TDI_generic, decl);
898
899       cgraph_lower_function (node);
900       pop_cfun ();
901     }
902   node->analyzed = true;
903
904   current_function_decl = save;
905 }
906
907 /* C++ frontend produce same body aliases all over the place, even before PCH
908    gets streamed out. It relies on us linking the aliases with their function
909    in order to do the fixups, but ipa-ref is not PCH safe.  Consequentely we
910    first produce aliases without links, but once C++ FE is sure he won't sream
911    PCH we build the links via this function.  */
912
913 void
914 cgraph_process_same_body_aliases (void)
915 {
916   struct cgraph_node *node;
917   for (node = cgraph_nodes; node; node = node->next)
918     if (node->same_body_alias
919         && !VEC_length (ipa_ref_t, node->ref_list.references))
920       {
921         struct cgraph_node *tgt = cgraph_get_node (node->thunk.alias);
922         ipa_record_reference (node, NULL, tgt, NULL, IPA_REF_ALIAS, NULL);
923       }
924   same_body_aliases_done = true;
925 }
926
927 /* Process attributes common for vars and functions.  */
928
929 static void
930 process_common_attributes (tree decl)
931 {
932   tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
933
934   if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
935     {
936       warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
937                   "%<weakref%> attribute should be accompanied with"
938                   " an %<alias%> attribute");
939       DECL_WEAK (decl) = 0;
940       DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
941                                                  DECL_ATTRIBUTES (decl));
942     }
943 }
944
945 /* Look for externally_visible and used attributes and mark cgraph nodes
946    accordingly.
947
948    We cannot mark the nodes at the point the attributes are processed (in
949    handle_*_attribute) because the copy of the declarations available at that
950    point may not be canonical.  For example, in:
951
952     void f();
953     void f() __attribute__((used));
954
955    the declaration we see in handle_used_attribute will be the second
956    declaration -- but the front end will subsequently merge that declaration
957    with the original declaration and discard the second declaration.
958
959    Furthermore, we can't mark these nodes in cgraph_finalize_function because:
960
961     void f() {}
962     void f() __attribute__((externally_visible));
963
964    is valid.
965
966    So, we walk the nodes at the end of the translation unit, applying the
967    attributes at that point.  */
968
969 static void
970 process_function_and_variable_attributes (struct cgraph_node *first,
971                                           struct varpool_node *first_var)
972 {
973   struct cgraph_node *node;
974   struct varpool_node *vnode;
975
976   for (node = cgraph_nodes; node != first; node = node->next)
977     {
978       tree decl = node->decl;
979       if (DECL_PRESERVE_P (decl))
980         cgraph_mark_needed_node (node);
981       if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
982           && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl))
983           && TREE_PUBLIC (node->decl))
984         {
985           if (node->local.finalized)
986             cgraph_mark_needed_node (node);
987         }
988       else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
989         {
990           if (! TREE_PUBLIC (node->decl))
991             warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
992                         "%<externally_visible%>"
993                         " attribute have effect only on public objects");
994           else if (node->local.finalized)
995              cgraph_mark_needed_node (node);
996         }
997       if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
998           && (node->local.finalized && !node->alias))
999         {
1000           warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
1001                       "%<weakref%> attribute ignored"
1002                       " because function is defined");
1003           DECL_WEAK (decl) = 0;
1004           DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
1005                                                      DECL_ATTRIBUTES (decl));
1006         }
1007
1008       if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
1009           && !DECL_DECLARED_INLINE_P (decl)
1010           /* redefining extern inline function makes it DECL_UNINLINABLE.  */
1011           && !DECL_UNINLINABLE (decl))
1012         warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
1013                     "always_inline function might not be inlinable");
1014      
1015       process_common_attributes (decl);
1016     }
1017   for (vnode = varpool_nodes; vnode != first_var; vnode = vnode->next)
1018     {
1019       tree decl = vnode->decl;
1020       if (DECL_PRESERVE_P (decl))
1021         {
1022           vnode->force_output = true;
1023           if (vnode->finalized)
1024             varpool_mark_needed_node (vnode);
1025         }
1026       if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
1027           && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl))
1028           && TREE_PUBLIC (vnode->decl))
1029         {
1030           if (vnode->finalized)
1031             varpool_mark_needed_node (vnode);
1032         }
1033       else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
1034         {
1035           if (! TREE_PUBLIC (vnode->decl))
1036             warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
1037                         "%<externally_visible%>"
1038                         " attribute have effect only on public objects");
1039           else if (vnode->finalized)
1040             varpool_mark_needed_node (vnode);
1041         }
1042       if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
1043           && vnode->finalized
1044           && DECL_INITIAL (decl))
1045         {
1046           warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
1047                       "%<weakref%> attribute ignored"
1048                       " because variable is initialized");
1049           DECL_WEAK (decl) = 0;
1050           DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
1051                                                       DECL_ATTRIBUTES (decl));
1052         }
1053       process_common_attributes (decl);
1054     }
1055 }
1056
1057 /* Process CGRAPH_NODES_NEEDED queue, analyze each function (and transitively
1058    each reachable functions) and build cgraph.
1059    The function can be called multiple times after inserting new nodes
1060    into beginning of queue.  Just the new part of queue is re-scanned then.  */
1061
1062 static void
1063 cgraph_analyze_functions (void)
1064 {
1065   /* Keep track of already processed nodes when called multiple times for
1066      intermodule optimization.  */
1067   static struct cgraph_node *first_analyzed;
1068   struct cgraph_node *first_processed = first_analyzed;
1069   static struct varpool_node *first_analyzed_var;
1070   struct cgraph_node *node, *next;
1071
1072   bitmap_obstack_initialize (NULL);
1073   process_function_and_variable_attributes (first_processed,
1074                                             first_analyzed_var);
1075   first_processed = cgraph_nodes;
1076   first_analyzed_var = varpool_nodes;
1077   varpool_analyze_pending_decls ();
1078   if (cgraph_dump_file)
1079     {
1080       fprintf (cgraph_dump_file, "Initial entry points:");
1081       for (node = cgraph_nodes; node != first_analyzed; node = node->next)
1082         if (node->needed)
1083           fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
1084       fprintf (cgraph_dump_file, "\n");
1085     }
1086   cgraph_process_new_functions ();
1087
1088   /* Propagate reachability flag and lower representation of all reachable
1089      functions.  In the future, lowering will introduce new functions and
1090      new entry points on the way (by template instantiation and virtual
1091      method table generation for instance).  */
1092   while (cgraph_nodes_queue)
1093     {
1094       struct cgraph_edge *edge;
1095       tree decl = cgraph_nodes_queue->decl;
1096
1097       node = cgraph_nodes_queue;
1098       cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
1099       node->next_needed = NULL;
1100
1101       /* ??? It is possible to create extern inline function and later using
1102          weak alias attribute to kill its body. See
1103          gcc.c-torture/compile/20011119-1.c  */
1104       if (!DECL_STRUCT_FUNCTION (decl)
1105           && (!node->alias || !node->thunk.alias)
1106           && !node->thunk.thunk_p)
1107         {
1108           cgraph_reset_node (node);
1109           node->local.redefined_extern_inline = true;
1110           continue;
1111         }
1112
1113       if (!node->analyzed)
1114         cgraph_analyze_function (node);
1115
1116       for (edge = node->callees; edge; edge = edge->next_callee)
1117         if (!edge->callee->reachable)
1118           cgraph_mark_reachable_node (edge->callee);
1119       for (edge = node->callers; edge; edge = edge->next_caller)
1120         if (!edge->caller->reachable && edge->caller->thunk.thunk_p)
1121           cgraph_mark_reachable_node (edge->caller);
1122
1123       if (node->same_comdat_group)
1124         {
1125           for (next = node->same_comdat_group;
1126                next != node;
1127                next = next->same_comdat_group)
1128             cgraph_mark_reachable_node (next);
1129         }
1130
1131       /* If decl is a clone of an abstract function, mark that abstract
1132          function so that we don't release its body. The DECL_INITIAL() of that
1133          abstract function declaration will be later needed to output debug
1134          info.  */
1135       if (DECL_ABSTRACT_ORIGIN (decl))
1136         {
1137           struct cgraph_node *origin_node;
1138           origin_node = cgraph_get_node (DECL_ABSTRACT_ORIGIN (decl));
1139           origin_node->abstract_and_needed = true;
1140         }
1141
1142       /* We finalize local static variables during constructing callgraph
1143          edges.  Process their attributes too.  */
1144       process_function_and_variable_attributes (first_processed,
1145                                                 first_analyzed_var);
1146       first_processed = cgraph_nodes;
1147       first_analyzed_var = varpool_nodes;
1148       varpool_analyze_pending_decls ();
1149       cgraph_process_new_functions ();
1150     }
1151
1152   /* Collect entry points to the unit.  */
1153   if (cgraph_dump_file)
1154     {
1155       fprintf (cgraph_dump_file, "Unit entry points:");
1156       for (node = cgraph_nodes; node != first_analyzed; node = node->next)
1157         if (node->needed)
1158           fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
1159       fprintf (cgraph_dump_file, "\n\nInitial ");
1160       dump_cgraph (cgraph_dump_file);
1161       dump_varpool (cgraph_dump_file);
1162     }
1163
1164   if (cgraph_dump_file)
1165     fprintf (cgraph_dump_file, "\nReclaiming functions:");
1166
1167   for (node = cgraph_nodes; node != first_analyzed; node = next)
1168     {
1169       tree decl = node->decl;
1170       next = node->next;
1171
1172       if (node->local.finalized && !gimple_has_body_p (decl)
1173           && (!node->alias || !node->thunk.alias)
1174           && !node->thunk.thunk_p)
1175         cgraph_reset_node (node);
1176
1177       if (!node->reachable
1178           && (gimple_has_body_p (decl) || node->thunk.thunk_p
1179               || (node->alias && node->thunk.alias)))
1180         {
1181           if (cgraph_dump_file)
1182             fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
1183           cgraph_remove_node (node);
1184           continue;
1185         }
1186       else
1187         node->next_needed = NULL;
1188       gcc_assert (!node->local.finalized || node->thunk.thunk_p
1189                   || node->alias
1190                   || gimple_has_body_p (decl));
1191       gcc_assert (node->analyzed == node->local.finalized);
1192     }
1193   if (cgraph_dump_file)
1194     {
1195       fprintf (cgraph_dump_file, "\n\nReclaimed ");
1196       dump_cgraph (cgraph_dump_file);
1197       dump_varpool (cgraph_dump_file);
1198     }
1199   bitmap_obstack_release (NULL);
1200   first_analyzed = cgraph_nodes;
1201   ggc_collect ();
1202 }
1203
1204 /* Translate the ugly representation of aliases as alias pairs into nice
1205    representation in callgraph.  We don't handle all cases yet,
1206    unforutnately.  */
1207
1208 static void
1209 handle_alias_pairs (void)
1210 {
1211   alias_pair *p;
1212   unsigned i;
1213   struct cgraph_node *target_node;
1214   struct cgraph_node *src_node;
1215   struct varpool_node *target_vnode;
1216   
1217   for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p);)
1218     {
1219       if (TREE_CODE (p->decl) == FUNCTION_DECL
1220            && !lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl))
1221           && (target_node = cgraph_node_for_asm (p->target)) != NULL)
1222         {
1223           src_node = cgraph_get_node (p->decl);
1224           if (src_node && src_node->local.finalized)
1225             cgraph_reset_node (src_node);
1226           /* Normally EXTERNAL flag is used to mark external inlines,
1227              however for aliases it seems to be allowed to use it w/o
1228              any meaning. See gcc.dg/attr-alias-3.c  
1229              However for weakref we insist on EXTERNAL flag being set.
1230              See gcc.dg/attr-alias-5.c  */
1231           if (DECL_EXTERNAL (p->decl))
1232             DECL_EXTERNAL (p->decl) = 0;
1233           cgraph_create_function_alias (p->decl, target_node->decl);
1234           VEC_unordered_remove (alias_pair, alias_pairs, i);
1235         }
1236       else if (TREE_CODE (p->decl) == VAR_DECL
1237                && !lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl))
1238                && (target_vnode = varpool_node_for_asm (p->target)) != NULL)
1239         {
1240           /* Normally EXTERNAL flag is used to mark external inlines,
1241              however for aliases it seems to be allowed to use it w/o
1242              any meaning. See gcc.dg/attr-alias-3.c  
1243              However for weakref we insist on EXTERNAL flag being set.
1244              See gcc.dg/attr-alias-5.c  */
1245           if (DECL_EXTERNAL (p->decl))
1246             DECL_EXTERNAL (p->decl) = 0;
1247           varpool_create_variable_alias (p->decl, target_vnode->decl);
1248           VEC_unordered_remove (alias_pair, alias_pairs, i);
1249         }
1250       else
1251         {
1252           if (dump_file)
1253             fprintf (dump_file, "Unhandled alias %s->%s\n",
1254                      IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (p->decl)),
1255                      IDENTIFIER_POINTER (p->target));
1256
1257           i++;
1258         }
1259     }
1260 }
1261
1262
1263 /* Analyze the whole compilation unit once it is parsed completely.  */
1264
1265 void
1266 cgraph_finalize_compilation_unit (void)
1267 {
1268   timevar_push (TV_CGRAPH);
1269
1270   /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE.  */
1271   if (flag_lto)
1272     lto_streamer_hooks_init ();
1273
1274   /* If we're here there's no current function anymore.  Some frontends
1275      are lazy in clearing these.  */
1276   current_function_decl = NULL;
1277   set_cfun (NULL);
1278
1279   /* Do not skip analyzing the functions if there were errors, we
1280      miss diagnostics for following functions otherwise.  */
1281
1282   /* Emit size functions we didn't inline.  */
1283   finalize_size_functions ();
1284
1285   /* Mark alias targets necessary and emit diagnostics.  */
1286   finish_aliases_1 ();
1287   handle_alias_pairs ();
1288
1289   if (!quiet_flag)
1290     {
1291       fprintf (stderr, "\nAnalyzing compilation unit\n");
1292       fflush (stderr);
1293     }
1294
1295   if (flag_dump_passes)
1296     dump_passes ();
1297
1298   /* Gimplify and lower all functions, compute reachability and
1299      remove unreachable nodes.  */
1300   cgraph_analyze_functions ();
1301
1302   /* Mark alias targets necessary and emit diagnostics.  */
1303   finish_aliases_1 ();
1304   handle_alias_pairs ();
1305
1306   /* Gimplify and lower thunks.  */
1307   cgraph_analyze_functions ();
1308
1309   /* Finally drive the pass manager.  */
1310   cgraph_optimize ();
1311
1312   timevar_pop (TV_CGRAPH);
1313 }
1314
1315
1316 /* Figure out what functions we want to assemble.  */
1317
1318 static void
1319 cgraph_mark_functions_to_output (void)
1320 {
1321   struct cgraph_node *node;
1322 #ifdef ENABLE_CHECKING
1323   bool check_same_comdat_groups = false;
1324
1325   for (node = cgraph_nodes; node; node = node->next)
1326     gcc_assert (!node->process);
1327 #endif
1328
1329   for (node = cgraph_nodes; node; node = node->next)
1330     {
1331       tree decl = node->decl;
1332       struct cgraph_edge *e;
1333
1334       gcc_assert (!node->process || node->same_comdat_group);
1335       if (node->process)
1336         continue;
1337
1338       for (e = node->callers; e; e = e->next_caller)
1339         if (e->inline_failed)
1340           break;
1341
1342       /* We need to output all local functions that are used and not
1343          always inlined, as well as those that are reachable from
1344          outside the current compilation unit.  */
1345       if (node->analyzed
1346           && !node->thunk.thunk_p
1347           && !node->alias
1348           && !node->global.inlined_to
1349           && (!cgraph_only_called_directly_p (node)
1350               || ((e || ipa_ref_has_aliases_p (&node->ref_list))
1351                   && node->reachable))
1352           && !TREE_ASM_WRITTEN (decl)
1353           && !DECL_EXTERNAL (decl))
1354         {
1355           node->process = 1;
1356           if (node->same_comdat_group)
1357             {
1358               struct cgraph_node *next;
1359               for (next = node->same_comdat_group;
1360                    next != node;
1361                    next = next->same_comdat_group)
1362                 if (!next->thunk.thunk_p && !next->alias)
1363                   next->process = 1;
1364             }
1365         }
1366       else if (node->same_comdat_group)
1367         {
1368 #ifdef ENABLE_CHECKING
1369           check_same_comdat_groups = true;
1370 #endif
1371         }
1372       else
1373         {
1374           /* We should've reclaimed all functions that are not needed.  */
1375 #ifdef ENABLE_CHECKING
1376           if (!node->global.inlined_to
1377               && gimple_has_body_p (decl)
1378               /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1379                  are inside partition, we can end up not removing the body since we no longer
1380                  have analyzed node pointing to it.  */
1381               && !node->in_other_partition
1382               && !node->alias
1383               && !DECL_EXTERNAL (decl))
1384             {
1385               dump_cgraph_node (stderr, node);
1386               internal_error ("failed to reclaim unneeded function");
1387             }
1388 #endif
1389           gcc_assert (node->global.inlined_to
1390                       || !gimple_has_body_p (decl)
1391                       || node->in_other_partition
1392                       || DECL_EXTERNAL (decl));
1393
1394         }
1395
1396     }
1397 #ifdef ENABLE_CHECKING
1398   if (check_same_comdat_groups)
1399     for (node = cgraph_nodes; node; node = node->next)
1400       if (node->same_comdat_group && !node->process)
1401         {
1402           tree decl = node->decl;
1403           if (!node->global.inlined_to
1404               && gimple_has_body_p (decl)
1405               /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1406                  are inside partition, we can end up not removing the body since we no longer
1407                  have analyzed node pointing to it.  */
1408               && !node->in_other_partition
1409               && !DECL_EXTERNAL (decl))
1410             {
1411               dump_cgraph_node (stderr, node);
1412               internal_error ("failed to reclaim unneeded functionin same comdat group");
1413             }
1414         }
1415 #endif
1416 }
1417
1418 /* DECL is FUNCTION_DECL.  Initialize datastructures so DECL is a function
1419    in lowered gimple form.
1420    
1421    Set current_function_decl and cfun to newly constructed empty function body.
1422    return basic block in the function body.  */
1423
1424 static basic_block
1425 init_lowered_empty_function (tree decl)
1426 {
1427   basic_block bb;
1428
1429   current_function_decl = decl;
1430   allocate_struct_function (decl, false);
1431   gimple_register_cfg_hooks ();
1432   init_empty_tree_cfg ();
1433   init_tree_ssa (cfun);
1434   init_ssa_operands ();
1435   cfun->gimple_df->in_ssa_p = true;
1436   DECL_INITIAL (decl) = make_node (BLOCK);
1437
1438   DECL_SAVED_TREE (decl) = error_mark_node;
1439   cfun->curr_properties |=
1440     (PROP_gimple_lcf | PROP_gimple_leh | PROP_cfg | PROP_referenced_vars |
1441      PROP_ssa | PROP_gimple_any);
1442
1443   /* Create BB for body of the function and connect it properly.  */
1444   bb = create_basic_block (NULL, (void *) 0, ENTRY_BLOCK_PTR);
1445   make_edge (ENTRY_BLOCK_PTR, bb, 0);
1446   make_edge (bb, EXIT_BLOCK_PTR, 0);
1447
1448   return bb;
1449 }
1450
1451 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1452    offset indicated by VIRTUAL_OFFSET, if that is
1453    non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1454    zero for a result adjusting thunk.  */
1455
1456 static tree
1457 thunk_adjust (gimple_stmt_iterator * bsi,
1458               tree ptr, bool this_adjusting,
1459               HOST_WIDE_INT fixed_offset, tree virtual_offset)
1460 {
1461   gimple stmt;
1462   tree ret;
1463
1464   if (this_adjusting
1465       && fixed_offset != 0)
1466     {
1467       stmt = gimple_build_assign
1468                 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1469                                                        ptr,
1470                                                        fixed_offset));
1471       gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1472     }
1473
1474   /* If there's a virtual offset, look up that value in the vtable and
1475      adjust the pointer again.  */
1476   if (virtual_offset)
1477     {
1478       tree vtabletmp;
1479       tree vtabletmp2;
1480       tree vtabletmp3;
1481
1482       if (!vtable_entry_type)
1483         {
1484           tree vfunc_type = make_node (FUNCTION_TYPE);
1485           TREE_TYPE (vfunc_type) = integer_type_node;
1486           TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1487           layout_type (vfunc_type);
1488
1489           vtable_entry_type = build_pointer_type (vfunc_type);
1490         }
1491
1492       vtabletmp =
1493         create_tmp_var (build_pointer_type
1494                         (build_pointer_type (vtable_entry_type)), "vptr");
1495
1496       /* The vptr is always at offset zero in the object.  */
1497       stmt = gimple_build_assign (vtabletmp,
1498                                   build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1499                                           ptr));
1500       gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1501       mark_symbols_for_renaming (stmt);
1502       find_referenced_vars_in (stmt);
1503
1504       /* Form the vtable address.  */
1505       vtabletmp2 = create_tmp_var (TREE_TYPE (TREE_TYPE (vtabletmp)),
1506                                    "vtableaddr");
1507       stmt = gimple_build_assign (vtabletmp2,
1508                                   build_simple_mem_ref (vtabletmp));
1509       gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1510       mark_symbols_for_renaming (stmt);
1511       find_referenced_vars_in (stmt);
1512
1513       /* Find the entry with the vcall offset.  */
1514       stmt = gimple_build_assign (vtabletmp2,
1515                                   fold_build_pointer_plus_loc (input_location,
1516                                                                vtabletmp2,
1517                                                                virtual_offset));
1518       gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1519
1520       /* Get the offset itself.  */
1521       vtabletmp3 = create_tmp_var (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1522                                    "vcalloffset");
1523       stmt = gimple_build_assign (vtabletmp3,
1524                                   build_simple_mem_ref (vtabletmp2));
1525       gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1526       mark_symbols_for_renaming (stmt);
1527       find_referenced_vars_in (stmt);
1528
1529       /* Adjust the `this' pointer.  */
1530       ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1531       ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1532                                       GSI_CONTINUE_LINKING);
1533     }
1534
1535   if (!this_adjusting
1536       && fixed_offset != 0)
1537     /* Adjust the pointer by the constant.  */
1538     {
1539       tree ptrtmp;
1540
1541       if (TREE_CODE (ptr) == VAR_DECL)
1542         ptrtmp = ptr;
1543       else
1544         {
1545           ptrtmp = create_tmp_var (TREE_TYPE (ptr), "ptr");
1546           stmt = gimple_build_assign (ptrtmp, ptr);
1547           gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1548           mark_symbols_for_renaming (stmt);
1549           find_referenced_vars_in (stmt);
1550         }
1551       ptr = fold_build_pointer_plus_hwi_loc (input_location,
1552                                              ptrtmp, fixed_offset);
1553     }
1554
1555   /* Emit the statement and gimplify the adjustment expression.  */
1556   ret = create_tmp_var (TREE_TYPE (ptr), "adjusted_this");
1557   stmt = gimple_build_assign (ret, ptr);
1558   mark_symbols_for_renaming (stmt);
1559   find_referenced_vars_in (stmt);
1560   gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1561
1562   return ret;
1563 }
1564
1565 /* Produce assembler for thunk NODE.  */
1566
1567 static void
1568 assemble_thunk (struct cgraph_node *node)
1569 {
1570   bool this_adjusting = node->thunk.this_adjusting;
1571   HOST_WIDE_INT fixed_offset = node->thunk.fixed_offset;
1572   HOST_WIDE_INT virtual_value = node->thunk.virtual_value;
1573   tree virtual_offset = NULL;
1574   tree alias = node->thunk.alias;
1575   tree thunk_fndecl = node->decl;
1576   tree a = DECL_ARGUMENTS (thunk_fndecl);
1577
1578   current_function_decl = thunk_fndecl;
1579
1580   /* Ensure thunks are emitted in their correct sections.  */
1581   resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1582
1583   if (this_adjusting
1584       && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1585                                               virtual_value, alias))
1586     {
1587       const char *fnname;
1588       tree fn_block;
1589       tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1590       
1591       DECL_RESULT (thunk_fndecl)
1592         = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1593                       RESULT_DECL, 0, restype);
1594       fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
1595
1596       /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1597          create one.  */
1598       fn_block = make_node (BLOCK);
1599       BLOCK_VARS (fn_block) = a;
1600       DECL_INITIAL (thunk_fndecl) = fn_block;
1601       init_function_start (thunk_fndecl);
1602       cfun->is_thunk = 1;
1603       assemble_start_function (thunk_fndecl, fnname);
1604
1605       targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1606                                        fixed_offset, virtual_value, alias);
1607
1608       assemble_end_function (thunk_fndecl, fnname);
1609       init_insn_lengths ();
1610       free_after_compilation (cfun);
1611       set_cfun (NULL);
1612       TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1613       node->thunk.thunk_p = false;
1614       node->analyzed = false;
1615     }
1616   else
1617     {
1618       tree restype;
1619       basic_block bb, then_bb, else_bb, return_bb;
1620       gimple_stmt_iterator bsi;
1621       int nargs = 0;
1622       tree arg;
1623       int i;
1624       tree resdecl;
1625       tree restmp = NULL;
1626       VEC(tree, heap) *vargs;
1627
1628       gimple call;
1629       gimple ret;
1630
1631       DECL_IGNORED_P (thunk_fndecl) = 1;
1632       bitmap_obstack_initialize (NULL);
1633
1634       if (node->thunk.virtual_offset_p)
1635         virtual_offset = size_int (virtual_value);
1636
1637       /* Build the return declaration for the function.  */
1638       restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1639       if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1640         {
1641           resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1642           DECL_ARTIFICIAL (resdecl) = 1;
1643           DECL_IGNORED_P (resdecl) = 1;
1644           DECL_RESULT (thunk_fndecl) = resdecl;
1645         }
1646       else
1647         resdecl = DECL_RESULT (thunk_fndecl);
1648
1649       bb = then_bb = else_bb = return_bb = init_lowered_empty_function (thunk_fndecl);
1650
1651       bsi = gsi_start_bb (bb);
1652
1653       /* Build call to the function being thunked.  */
1654       if (!VOID_TYPE_P (restype))
1655         {
1656           if (!is_gimple_reg_type (restype))
1657             {
1658               restmp = resdecl;
1659               add_local_decl (cfun, restmp);
1660               BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1661             }
1662           else
1663             restmp = create_tmp_var_raw (restype, "retval");
1664         }
1665
1666       for (arg = a; arg; arg = DECL_CHAIN (arg))
1667         nargs++;
1668       vargs = VEC_alloc (tree, heap, nargs);
1669       if (this_adjusting)
1670         VEC_quick_push (tree, vargs,
1671                         thunk_adjust (&bsi,
1672                                       a, 1, fixed_offset,
1673                                       virtual_offset));
1674       else
1675         VEC_quick_push (tree, vargs, a);
1676       for (i = 1, arg = DECL_CHAIN (a); i < nargs; i++, arg = DECL_CHAIN (arg))
1677         VEC_quick_push (tree, vargs, arg);
1678       call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1679       VEC_free (tree, heap, vargs);
1680       gimple_call_set_cannot_inline (call, true);
1681       gimple_call_set_from_thunk (call, true);
1682       if (restmp)
1683         gimple_call_set_lhs (call, restmp);
1684       gsi_insert_after (&bsi, call, GSI_NEW_STMT);
1685       mark_symbols_for_renaming (call);
1686       find_referenced_vars_in (call);
1687       update_stmt (call);
1688
1689       if (restmp && !this_adjusting)
1690         {
1691           tree true_label = NULL_TREE;
1692
1693           if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1694             {
1695               gimple stmt;
1696               /* If the return type is a pointer, we need to
1697                  protect against NULL.  We know there will be an
1698                  adjustment, because that's why we're emitting a
1699                  thunk.  */
1700               then_bb = create_basic_block (NULL, (void *) 0, bb);
1701               return_bb = create_basic_block (NULL, (void *) 0, then_bb);
1702               else_bb = create_basic_block (NULL, (void *) 0, else_bb);
1703               remove_edge (single_succ_edge (bb));
1704               true_label = gimple_block_label (then_bb);
1705               stmt = gimple_build_cond (NE_EXPR, restmp,
1706                                         build_zero_cst (TREE_TYPE (restmp)),
1707                                         NULL_TREE, NULL_TREE);
1708               gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1709               make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1710               make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1711               make_edge (return_bb, EXIT_BLOCK_PTR, 0);
1712               make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1713               make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1714               bsi = gsi_last_bb (then_bb);
1715             }
1716
1717           restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1718                                  fixed_offset, virtual_offset);
1719           if (true_label)
1720             {
1721               gimple stmt;
1722               bsi = gsi_last_bb (else_bb);
1723               stmt = gimple_build_assign (restmp,
1724                                           build_zero_cst (TREE_TYPE (restmp)));
1725               gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1726               bsi = gsi_last_bb (return_bb);
1727             }
1728         }
1729       else
1730         gimple_call_set_tail (call, true);
1731
1732       /* Build return value.  */
1733       ret = gimple_build_return (restmp);
1734       gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1735
1736       delete_unreachable_blocks ();
1737       update_ssa (TODO_update_ssa);
1738
1739       /* Since we want to emit the thunk, we explicitly mark its name as
1740          referenced.  */
1741       node->thunk.thunk_p = false;
1742       cgraph_node_remove_callees (node);
1743       cgraph_add_new_function (thunk_fndecl, true);
1744       bitmap_obstack_release (NULL);
1745     }
1746   current_function_decl = NULL;
1747 }
1748
1749
1750
1751 /* Assemble thunks and aliases asociated to NODE.  */
1752
1753 static void
1754 assemble_thunks_and_aliases (struct cgraph_node *node)
1755 {
1756   struct cgraph_edge *e;
1757   int i;
1758   struct ipa_ref *ref;
1759
1760   for (e = node->callers; e;)
1761     if (e->caller->thunk.thunk_p)
1762       {
1763         struct cgraph_node *thunk = e->caller;
1764
1765         e = e->next_caller;
1766         assemble_thunks_and_aliases (thunk);
1767         assemble_thunk (thunk);
1768       }
1769     else
1770       e = e->next_caller;
1771   for (i = 0; ipa_ref_list_refering_iterate (&node->ref_list, i, ref); i++)
1772     if (ref->use == IPA_REF_ALIAS)
1773       {
1774         struct cgraph_node *alias = ipa_ref_refering_node (ref);
1775         assemble_alias (alias->decl,
1776                         DECL_ASSEMBLER_NAME (alias->thunk.alias));
1777         assemble_thunks_and_aliases (alias);
1778       }
1779 }
1780
1781 /* Expand function specified by NODE.  */
1782
1783 static void
1784 cgraph_expand_function (struct cgraph_node *node)
1785 {
1786   tree decl = node->decl;
1787
1788   /* We ought to not compile any inline clones.  */
1789   gcc_assert (!node->global.inlined_to);
1790
1791   announce_function (decl);
1792   node->process = 0;
1793   assemble_thunks_and_aliases (node);
1794   gcc_assert (node->lowered);
1795
1796   /* Generate RTL for the body of DECL.  */
1797   tree_rest_of_compilation (decl);
1798
1799   /* Make sure that BE didn't give up on compiling.  */
1800   gcc_assert (TREE_ASM_WRITTEN (decl));
1801   current_function_decl = NULL;
1802   gcc_assert (!cgraph_preserve_function_body_p (node));
1803   cgraph_release_function_body (node);
1804   /* Eliminate all call edges.  This is important so the GIMPLE_CALL no longer
1805      points to the dead function body.  */
1806   cgraph_node_remove_callees (node);
1807
1808   cgraph_function_flags_ready = true;
1809 }
1810
1811 /* Return true when CALLER_DECL should be inlined into CALLEE_DECL.  */
1812
1813 bool
1814 cgraph_inline_p (struct cgraph_edge *e, cgraph_inline_failed_t *reason)
1815 {
1816   *reason = e->inline_failed;
1817   return !e->inline_failed;
1818 }
1819
1820
1821
1822 /* Expand all functions that must be output.
1823
1824    Attempt to topologically sort the nodes so function is output when
1825    all called functions are already assembled to allow data to be
1826    propagated across the callgraph.  Use a stack to get smaller distance
1827    between a function and its callees (later we may choose to use a more
1828    sophisticated algorithm for function reordering; we will likely want
1829    to use subsections to make the output functions appear in top-down
1830    order).  */
1831
1832 static void
1833 cgraph_expand_all_functions (void)
1834 {
1835   struct cgraph_node *node;
1836   struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1837   int order_pos, new_order_pos = 0;
1838   int i;
1839
1840   order_pos = ipa_reverse_postorder (order);
1841   gcc_assert (order_pos == cgraph_n_nodes);
1842
1843   /* Garbage collector may remove inline clones we eliminate during
1844      optimization.  So we must be sure to not reference them.  */
1845   for (i = 0; i < order_pos; i++)
1846     if (order[i]->process)
1847       order[new_order_pos++] = order[i];
1848
1849   for (i = new_order_pos - 1; i >= 0; i--)
1850     {
1851       node = order[i];
1852       if (node->process)
1853         {
1854           gcc_assert (node->reachable);
1855           node->process = 0;
1856           cgraph_expand_function (node);
1857         }
1858     }
1859   cgraph_process_new_functions ();
1860
1861   free (order);
1862
1863 }
1864
1865 /* This is used to sort the node types by the cgraph order number.  */
1866
1867 enum cgraph_order_sort_kind
1868 {
1869   ORDER_UNDEFINED = 0,
1870   ORDER_FUNCTION,
1871   ORDER_VAR,
1872   ORDER_ASM
1873 };
1874
1875 struct cgraph_order_sort
1876 {
1877   enum cgraph_order_sort_kind kind;
1878   union
1879   {
1880     struct cgraph_node *f;
1881     struct varpool_node *v;
1882     struct cgraph_asm_node *a;
1883   } u;
1884 };
1885
1886 /* Output all functions, variables, and asm statements in the order
1887    according to their order fields, which is the order in which they
1888    appeared in the file.  This implements -fno-toplevel-reorder.  In
1889    this mode we may output functions and variables which don't really
1890    need to be output.  */
1891
1892 static void
1893 cgraph_output_in_order (void)
1894 {
1895   int max;
1896   struct cgraph_order_sort *nodes;
1897   int i;
1898   struct cgraph_node *pf;
1899   struct varpool_node *pv;
1900   struct cgraph_asm_node *pa;
1901
1902   max = cgraph_order;
1903   nodes = XCNEWVEC (struct cgraph_order_sort, max);
1904
1905   varpool_analyze_pending_decls ();
1906
1907   for (pf = cgraph_nodes; pf; pf = pf->next)
1908     {
1909       if (pf->process && !pf->thunk.thunk_p && !pf->alias)
1910         {
1911           i = pf->order;
1912           gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1913           nodes[i].kind = ORDER_FUNCTION;
1914           nodes[i].u.f = pf;
1915         }
1916     }
1917
1918   for (pv = varpool_nodes_queue; pv; pv = pv->next_needed)
1919     {
1920       i = pv->order;
1921       gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1922       nodes[i].kind = ORDER_VAR;
1923       nodes[i].u.v = pv;
1924     }
1925
1926   for (pa = cgraph_asm_nodes; pa; pa = pa->next)
1927     {
1928       i = pa->order;
1929       gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1930       nodes[i].kind = ORDER_ASM;
1931       nodes[i].u.a = pa;
1932     }
1933
1934   /* In toplevel reorder mode we output all statics; mark them as needed.  */
1935   for (i = 0; i < max; ++i)
1936     {
1937       if (nodes[i].kind == ORDER_VAR)
1938         {
1939           varpool_mark_needed_node (nodes[i].u.v);
1940         }
1941     }
1942   varpool_empty_needed_queue ();
1943
1944   for (i = 0; i < max; ++i)
1945     if (nodes[i].kind == ORDER_VAR)
1946       varpool_finalize_named_section_flags (nodes[i].u.v);
1947
1948   for (i = 0; i < max; ++i)
1949     {
1950       switch (nodes[i].kind)
1951         {
1952         case ORDER_FUNCTION:
1953           nodes[i].u.f->process = 0;
1954           cgraph_expand_function (nodes[i].u.f);
1955           break;
1956
1957         case ORDER_VAR:
1958           varpool_assemble_decl (nodes[i].u.v);
1959           break;
1960
1961         case ORDER_ASM:
1962           assemble_asm (nodes[i].u.a->asm_str);
1963           break;
1964
1965         case ORDER_UNDEFINED:
1966           break;
1967
1968         default:
1969           gcc_unreachable ();
1970         }
1971     }
1972
1973   cgraph_asm_nodes = NULL;
1974   free (nodes);
1975 }
1976
1977 /* Return true when function body of DECL still needs to be kept around
1978    for later re-use.  */
1979 bool
1980 cgraph_preserve_function_body_p (struct cgraph_node *node)
1981 {
1982   gcc_assert (cgraph_global_info_ready);
1983   gcc_assert (!node->alias && !node->thunk.thunk_p);
1984
1985   /* Look if there is any clone around.  */
1986   if (node->clones)
1987     return true;
1988   return false;
1989 }
1990
1991 static void
1992 ipa_passes (void)
1993 {
1994   set_cfun (NULL);
1995   current_function_decl = NULL;
1996   gimple_register_cfg_hooks ();
1997   bitmap_obstack_initialize (NULL);
1998
1999   invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2000
2001   if (!in_lto_p)
2002     {
2003       execute_ipa_pass_list (all_small_ipa_passes);
2004       if (seen_error ())
2005         return;
2006     }
2007
2008   /* If pass_all_early_optimizations was not scheduled, the state of
2009      the cgraph will not be properly updated.  Update it now.  */
2010   if (cgraph_state < CGRAPH_STATE_IPA_SSA)
2011     cgraph_state = CGRAPH_STATE_IPA_SSA;
2012
2013   if (!in_lto_p)
2014     {
2015       /* Generate coverage variables and constructors.  */
2016       coverage_finish ();
2017
2018       /* Process new functions added.  */
2019       set_cfun (NULL);
2020       current_function_decl = NULL;
2021       cgraph_process_new_functions ();
2022
2023       execute_ipa_summary_passes
2024         ((struct ipa_opt_pass_d *) all_regular_ipa_passes);
2025     }
2026
2027   /* Some targets need to handle LTO assembler output specially.  */
2028   if (flag_generate_lto)
2029     targetm.asm_out.lto_start ();
2030
2031   execute_ipa_summary_passes ((struct ipa_opt_pass_d *) all_lto_gen_passes);
2032
2033   if (!in_lto_p)
2034     ipa_write_summaries ();
2035
2036   if (flag_generate_lto)
2037     targetm.asm_out.lto_end ();
2038
2039   if (!flag_ltrans)
2040     execute_ipa_pass_list (all_regular_ipa_passes);
2041   invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2042
2043   bitmap_obstack_release (NULL);
2044 }
2045
2046
2047 /* Perform simple optimizations based on callgraph.  */
2048
2049 void
2050 cgraph_optimize (void)
2051 {
2052   if (seen_error ())
2053     return;
2054
2055 #ifdef ENABLE_CHECKING
2056   verify_cgraph ();
2057 #endif
2058
2059   /* Frontend may output common variables after the unit has been finalized.
2060      It is safe to deal with them here as they are always zero initialized.  */
2061   varpool_analyze_pending_decls ();
2062
2063   timevar_push (TV_CGRAPHOPT);
2064   if (pre_ipa_mem_report)
2065     {
2066       fprintf (stderr, "Memory consumption before IPA\n");
2067       dump_memory_report (false);
2068     }
2069   if (!quiet_flag)
2070     fprintf (stderr, "Performing interprocedural optimizations\n");
2071   cgraph_state = CGRAPH_STATE_IPA;
2072
2073   /* Don't run the IPA passes if there was any error or sorry messages.  */
2074   if (!seen_error ())
2075     ipa_passes ();
2076
2077   /* Do nothing else if any IPA pass found errors.  */
2078   if (seen_error ())
2079     {
2080       timevar_pop (TV_CGRAPHOPT);
2081       return;
2082     }
2083
2084   /* This pass remove bodies of extern inline functions we never inlined.
2085      Do this later so other IPA passes see what is really going on.  */
2086   cgraph_remove_unreachable_nodes (false, dump_file);
2087   cgraph_global_info_ready = true;
2088   if (cgraph_dump_file)
2089     {
2090       fprintf (cgraph_dump_file, "Optimized ");
2091       dump_cgraph (cgraph_dump_file);
2092       dump_varpool (cgraph_dump_file);
2093     }
2094   if (post_ipa_mem_report)
2095     {
2096       fprintf (stderr, "Memory consumption after IPA\n");
2097       dump_memory_report (false);
2098     }
2099   timevar_pop (TV_CGRAPHOPT);
2100
2101   /* Output everything.  */
2102   (*debug_hooks->assembly_start) ();
2103   if (!quiet_flag)
2104     fprintf (stderr, "Assembling functions:\n");
2105 #ifdef ENABLE_CHECKING
2106   verify_cgraph ();
2107 #endif
2108
2109   cgraph_materialize_all_clones ();
2110   bitmap_obstack_initialize (NULL);
2111   execute_ipa_pass_list (all_late_ipa_passes);
2112   cgraph_remove_unreachable_nodes (true, dump_file);
2113 #ifdef ENABLE_CHECKING
2114   verify_cgraph ();
2115 #endif
2116   bitmap_obstack_release (NULL);
2117   cgraph_mark_functions_to_output ();
2118
2119   cgraph_state = CGRAPH_STATE_EXPANSION;
2120   if (!flag_toplevel_reorder)
2121     cgraph_output_in_order ();
2122   else
2123     {
2124       cgraph_output_pending_asms ();
2125
2126       cgraph_expand_all_functions ();
2127       varpool_remove_unreferenced_decls ();
2128
2129       varpool_assemble_pending_decls ();
2130     }
2131   cgraph_process_new_functions ();
2132   cgraph_state = CGRAPH_STATE_FINISHED;
2133
2134   if (cgraph_dump_file)
2135     {
2136       fprintf (cgraph_dump_file, "\nFinal ");
2137       dump_cgraph (cgraph_dump_file);
2138       dump_varpool (cgraph_dump_file);
2139     }
2140 #ifdef ENABLE_CHECKING
2141   verify_cgraph ();
2142   /* Double check that all inline clones are gone and that all
2143      function bodies have been released from memory.  */
2144   if (!seen_error ())
2145     {
2146       struct cgraph_node *node;
2147       bool error_found = false;
2148
2149       for (node = cgraph_nodes; node; node = node->next)
2150         if (node->analyzed
2151             && (node->global.inlined_to
2152                 || gimple_has_body_p (node->decl)))
2153           {
2154             error_found = true;
2155             dump_cgraph_node (stderr, node);
2156           }
2157       if (error_found)
2158         internal_error ("nodes with unreleased memory found");
2159     }
2160 #endif
2161 }
2162
2163 void
2164 init_cgraph (void)
2165 {
2166   if (!cgraph_dump_file)
2167     cgraph_dump_file = dump_begin (TDI_cgraph, NULL);
2168 }
2169
2170 /* The edges representing the callers of the NEW_VERSION node were
2171    fixed by cgraph_function_versioning (), now the call_expr in their
2172    respective tree code should be updated to call the NEW_VERSION.  */
2173
2174 static void
2175 update_call_expr (struct cgraph_node *new_version)
2176 {
2177   struct cgraph_edge *e;
2178
2179   gcc_assert (new_version);
2180
2181   /* Update the call expr on the edges to call the new version.  */
2182   for (e = new_version->callers; e; e = e->next_caller)
2183     {
2184       struct function *inner_function = DECL_STRUCT_FUNCTION (e->caller->decl);
2185       gimple_call_set_fndecl (e->call_stmt, new_version->decl);
2186       maybe_clean_eh_stmt_fn (inner_function, e->call_stmt);
2187     }
2188 }
2189
2190
2191 /* Create a new cgraph node which is the new version of
2192    OLD_VERSION node.  REDIRECT_CALLERS holds the callers
2193    edges which should be redirected to point to
2194    NEW_VERSION.  ALL the callees edges of OLD_VERSION
2195    are cloned to the new version node.  Return the new
2196    version node. 
2197
2198    If non-NULL BLOCK_TO_COPY determine what basic blocks 
2199    was copied to prevent duplications of calls that are dead
2200    in the clone.  */
2201
2202 static struct cgraph_node *
2203 cgraph_copy_node_for_versioning (struct cgraph_node *old_version,
2204                                  tree new_decl,
2205                                  VEC(cgraph_edge_p,heap) *redirect_callers,
2206                                  bitmap bbs_to_copy)
2207  {
2208    struct cgraph_node *new_version;
2209    struct cgraph_edge *e;
2210    unsigned i;
2211
2212    gcc_assert (old_version);
2213
2214    new_version = cgraph_create_node (new_decl);
2215
2216    new_version->analyzed = true;
2217    new_version->local = old_version->local;
2218    new_version->local.externally_visible = false;
2219    new_version->local.local = true;
2220    new_version->global = old_version->global;
2221    new_version->rtl = old_version->rtl;
2222    new_version->reachable = true;
2223    new_version->count = old_version->count;
2224
2225    for (e = old_version->callees; e; e=e->next_callee)
2226      if (!bbs_to_copy
2227          || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
2228        cgraph_clone_edge (e, new_version, e->call_stmt,
2229                           e->lto_stmt_uid, REG_BR_PROB_BASE,
2230                           CGRAPH_FREQ_BASE,
2231                           true);
2232    for (e = old_version->indirect_calls; e; e=e->next_callee)
2233      if (!bbs_to_copy
2234          || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
2235        cgraph_clone_edge (e, new_version, e->call_stmt,
2236                           e->lto_stmt_uid, REG_BR_PROB_BASE,
2237                           CGRAPH_FREQ_BASE,
2238                           true);
2239    FOR_EACH_VEC_ELT (cgraph_edge_p, redirect_callers, i, e)
2240      {
2241        /* Redirect calls to the old version node to point to its new
2242           version.  */
2243        cgraph_redirect_edge_callee (e, new_version);
2244      }
2245
2246    return new_version;
2247  }
2248
2249  /* Perform function versioning.
2250     Function versioning includes copying of the tree and
2251     a callgraph update (creating a new cgraph node and updating
2252     its callees and callers).
2253
2254     REDIRECT_CALLERS varray includes the edges to be redirected
2255     to the new version.
2256
2257     TREE_MAP is a mapping of tree nodes we want to replace with
2258     new ones (according to results of prior analysis).
2259     OLD_VERSION_NODE is the node that is versioned.
2260     It returns the new version's cgraph node.
2261     If non-NULL ARGS_TO_SKIP determine function parameters to remove
2262     from new version.
2263     If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
2264     If non_NULL NEW_ENTRY determine new entry BB of the clone.  */
2265
2266 struct cgraph_node *
2267 cgraph_function_versioning (struct cgraph_node *old_version_node,
2268                             VEC(cgraph_edge_p,heap) *redirect_callers,
2269                             VEC (ipa_replace_map_p,gc)* tree_map,
2270                             bitmap args_to_skip,
2271                             bitmap bbs_to_copy,
2272                             basic_block new_entry_block,
2273                             const char *clone_name)
2274 {
2275   tree old_decl = old_version_node->decl;
2276   struct cgraph_node *new_version_node = NULL;
2277   tree new_decl;
2278
2279   if (!tree_versionable_function_p (old_decl))
2280     return NULL;
2281
2282   gcc_assert (old_version_node->local.can_change_signature || !args_to_skip);
2283
2284   /* Make a new FUNCTION_DECL tree node for the
2285      new version. */
2286   if (!args_to_skip)
2287     new_decl = copy_node (old_decl);
2288   else
2289     new_decl = build_function_decl_skip_args (old_decl, args_to_skip);
2290
2291   /* Generate a new name for the new version. */
2292   DECL_NAME (new_decl) = clone_function_name (old_decl, clone_name);
2293   SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
2294   SET_DECL_RTL (new_decl, NULL);
2295
2296   /* Create the new version's call-graph node.
2297      and update the edges of the new node. */
2298   new_version_node =
2299     cgraph_copy_node_for_versioning (old_version_node, new_decl,
2300                                      redirect_callers, bbs_to_copy);
2301
2302   /* Copy the OLD_VERSION_NODE function tree to the new version.  */
2303   tree_function_versioning (old_decl, new_decl, tree_map, false, args_to_skip,
2304                             bbs_to_copy, new_entry_block);
2305
2306   /* Update the new version's properties.
2307      Make The new version visible only within this translation unit.  Make sure
2308      that is not weak also.
2309      ??? We cannot use COMDAT linkage because there is no
2310      ABI support for this.  */
2311   cgraph_make_decl_local (new_version_node->decl);
2312   DECL_VIRTUAL_P (new_version_node->decl) = 0;
2313   new_version_node->local.externally_visible = 0;
2314   new_version_node->local.local = 1;
2315   new_version_node->lowered = true;
2316
2317   /* Update the call_expr on the edges to call the new version node. */
2318   update_call_expr (new_version_node);
2319
2320   cgraph_call_function_insertion_hooks (new_version_node);
2321   return new_version_node;
2322 }
2323
2324 /* Given virtual clone, turn it into actual clone.  */
2325 static void
2326 cgraph_materialize_clone (struct cgraph_node *node)
2327 {
2328   bitmap_obstack_initialize (NULL);
2329   node->former_clone_of = node->clone_of->decl;
2330   if (node->clone_of->former_clone_of)
2331     node->former_clone_of = node->clone_of->former_clone_of;
2332   /* Copy the OLD_VERSION_NODE function tree to the new version.  */
2333   tree_function_versioning (node->clone_of->decl, node->decl,
2334                             node->clone.tree_map, true,
2335                             node->clone.args_to_skip, NULL, NULL);
2336   if (cgraph_dump_file)
2337     {
2338       dump_function_to_file (node->clone_of->decl, cgraph_dump_file, dump_flags);
2339       dump_function_to_file (node->decl, cgraph_dump_file, dump_flags);
2340     }
2341
2342   /* Function is no longer clone.  */
2343   if (node->next_sibling_clone)
2344     node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
2345   if (node->prev_sibling_clone)
2346     node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
2347   else
2348     node->clone_of->clones = node->next_sibling_clone;
2349   node->next_sibling_clone = NULL;
2350   node->prev_sibling_clone = NULL;
2351   if (!node->clone_of->analyzed && !node->clone_of->clones)
2352     {
2353       cgraph_release_function_body (node->clone_of);
2354       cgraph_node_remove_callees (node->clone_of);
2355       ipa_remove_all_references (&node->clone_of->ref_list);
2356     }
2357   node->clone_of = NULL;
2358   bitmap_obstack_release (NULL);
2359 }
2360
2361 /* If necessary, change the function declaration in the call statement
2362    associated with E so that it corresponds to the edge callee.  */
2363
2364 gimple
2365 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
2366 {
2367   tree decl = gimple_call_fndecl (e->call_stmt);
2368   gimple new_stmt;
2369   gimple_stmt_iterator gsi;
2370   bool gsi_computed = false;
2371 #ifdef ENABLE_CHECKING
2372   struct cgraph_node *node;
2373 #endif
2374
2375   if (e->indirect_unknown_callee
2376       || decl == e->callee->decl
2377       /* Don't update call from same body alias to the real function.  */
2378       || (decl && cgraph_get_node (decl) == cgraph_get_node (e->callee->decl)))
2379     return e->call_stmt;
2380
2381 #ifdef ENABLE_CHECKING
2382   if (decl)
2383     {
2384       node = cgraph_get_node (decl);
2385       gcc_assert (!node || !node->clone.combined_args_to_skip);
2386     }
2387 #endif
2388
2389   if (cgraph_dump_file)
2390     {
2391       fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
2392                cgraph_node_name (e->caller), e->caller->uid,
2393                cgraph_node_name (e->callee), e->callee->uid);
2394       print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
2395       if (e->callee->clone.combined_args_to_skip)
2396         {
2397           fprintf (cgraph_dump_file, " combined args to skip: ");
2398           dump_bitmap (cgraph_dump_file,
2399                        e->callee->clone.combined_args_to_skip);
2400         }
2401     }
2402
2403   if (e->indirect_info &&
2404       e->indirect_info->thunk_delta != 0
2405       && (!e->callee->clone.combined_args_to_skip
2406           || !bitmap_bit_p (e->callee->clone.combined_args_to_skip, 0)))
2407     {
2408       if (cgraph_dump_file)
2409         fprintf (cgraph_dump_file, "          Thunk delta is "
2410                  HOST_WIDE_INT_PRINT_DEC "\n", e->indirect_info->thunk_delta);
2411       gsi = gsi_for_stmt (e->call_stmt);
2412       gsi_computed = true;
2413       gimple_adjust_this_by_delta (&gsi,
2414                                    size_int (e->indirect_info->thunk_delta));
2415       e->indirect_info->thunk_delta = 0;
2416     }
2417
2418   if (e->callee->clone.combined_args_to_skip)
2419     {
2420       int lp_nr;
2421
2422       new_stmt
2423         = gimple_call_copy_skip_args (e->call_stmt,
2424                                       e->callee->clone.combined_args_to_skip);
2425       gimple_call_set_fndecl (new_stmt, e->callee->decl);
2426
2427       if (gimple_vdef (new_stmt)
2428           && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
2429         SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
2430
2431       if (!gsi_computed)
2432         gsi = gsi_for_stmt (e->call_stmt);
2433       gsi_replace (&gsi, new_stmt, false);
2434       /* We need to defer cleaning EH info on the new statement to
2435          fixup-cfg.  We may not have dominator information at this point
2436          and thus would end up with unreachable blocks and have no way
2437          to communicate that we need to run CFG cleanup then.  */
2438       lp_nr = lookup_stmt_eh_lp (e->call_stmt);
2439       if (lp_nr != 0)
2440         {
2441           remove_stmt_from_eh_lp (e->call_stmt);
2442           add_stmt_to_eh_lp (new_stmt, lp_nr);
2443         }
2444     }
2445   else
2446     {
2447       new_stmt = e->call_stmt;
2448       gimple_call_set_fndecl (new_stmt, e->callee->decl);
2449       update_stmt (new_stmt);
2450     }
2451
2452   cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt, new_stmt);
2453
2454   if (cgraph_dump_file)
2455     {
2456       fprintf (cgraph_dump_file, "  updated to:");
2457       print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
2458     }
2459   return new_stmt;
2460 }
2461
2462 /* Once all functions from compilation unit are in memory, produce all clones
2463    and update all calls.  We might also do this on demand if we don't want to
2464    bring all functions to memory prior compilation, but current WHOPR
2465    implementation does that and it is is bit easier to keep everything right in
2466    this order.  */
2467 void
2468 cgraph_materialize_all_clones (void)
2469 {
2470   struct cgraph_node *node;
2471   bool stabilized = false;
2472
2473   if (cgraph_dump_file)
2474     fprintf (cgraph_dump_file, "Materializing clones\n");
2475 #ifdef ENABLE_CHECKING
2476   verify_cgraph ();
2477 #endif
2478
2479   /* We can also do topological order, but number of iterations should be
2480      bounded by number of IPA passes since single IPA pass is probably not
2481      going to create clones of clones it created itself.  */
2482   while (!stabilized)
2483     {
2484       stabilized = true;
2485       for (node = cgraph_nodes; node; node = node->next)
2486         {
2487           if (node->clone_of && node->decl != node->clone_of->decl
2488               && !gimple_has_body_p (node->decl))
2489             {
2490               if (gimple_has_body_p (node->clone_of->decl))
2491                 {
2492                   if (cgraph_dump_file)
2493                     {
2494                       fprintf (cgraph_dump_file, "cloning %s to %s\n",
2495                                cgraph_node_name (node->clone_of),
2496                                cgraph_node_name (node));
2497                       if (node->clone.tree_map)
2498                         {
2499                           unsigned int i;
2500                           fprintf (cgraph_dump_file, "   replace map: ");
2501                           for (i = 0; i < VEC_length (ipa_replace_map_p,
2502                                                       node->clone.tree_map);
2503                                                       i++)
2504                             {
2505                               struct ipa_replace_map *replace_info;
2506                               replace_info = VEC_index (ipa_replace_map_p,
2507                                                         node->clone.tree_map,
2508                                                         i);
2509                               print_generic_expr (cgraph_dump_file, replace_info->old_tree, 0);
2510                               fprintf (cgraph_dump_file, " -> ");
2511                               print_generic_expr (cgraph_dump_file, replace_info->new_tree, 0);
2512                               fprintf (cgraph_dump_file, "%s%s;",
2513                                        replace_info->replace_p ? "(replace)":"",
2514                                        replace_info->ref_p ? "(ref)":"");
2515                             }
2516                           fprintf (cgraph_dump_file, "\n");
2517                         }
2518                       if (node->clone.args_to_skip)
2519                         {
2520                           fprintf (cgraph_dump_file, "   args_to_skip: ");
2521                           dump_bitmap (cgraph_dump_file, node->clone.args_to_skip);
2522                         }
2523                       if (node->clone.args_to_skip)
2524                         {
2525                           fprintf (cgraph_dump_file, "   combined_args_to_skip:");
2526                           dump_bitmap (cgraph_dump_file, node->clone.combined_args_to_skip);
2527                         }
2528                     }
2529                   cgraph_materialize_clone (node);
2530                   stabilized = false;
2531                 }
2532             }
2533         }
2534     }
2535   for (node = cgraph_nodes; node; node = node->next)
2536     if (!node->analyzed && node->callees)
2537       cgraph_node_remove_callees (node);
2538   if (cgraph_dump_file)
2539     fprintf (cgraph_dump_file, "Materialization Call site updates done.\n");
2540 #ifdef ENABLE_CHECKING
2541   verify_cgraph ();
2542 #endif
2543   cgraph_remove_unreachable_nodes (false, cgraph_dump_file);
2544 }
2545
2546 #include "gt-cgraphunit.h"