OSDN Git Service

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