2014-09-08 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / cgraphunit.c
blob3a9972917ecd879123853b8d85f893668de3db88
1 /* Driver of optimization process
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This module implements main driver of compilation process.
23 The main scope of this file is to act as an interface in between
24 tree based frontends and the backend.
26 The front-end is supposed to use following functionality:
28 - finalize_function
30 This function is called once front-end has parsed whole body of function
31 and it is certain that the function body nor the declaration will change.
33 (There is one exception needed for implementing GCC extern inline
34 function.)
36 - varpool_finalize_decl
38 This function has same behavior as the above but is used for static
39 variables.
41 - add_asm_node
43 Insert new toplevel ASM statement
45 - finalize_compilation_unit
47 This function is called once (source level) compilation unit is finalized
48 and it will no longer change.
50 The symbol table is constructed starting from the trivially needed
51 symbols finalized by the frontend. Functions are lowered into
52 GIMPLE representation and callgraph/reference lists are constructed.
53 Those are used to discover other necessary functions and variables.
55 At the end the bodies of unreachable functions are removed.
57 The function can be called multiple times when multiple source level
58 compilation units are combined.
60 - compile
62 This passes control to the back-end. Optimizations are performed and
63 final assembler is generated. This is done in the following way. Note
64 that with link time optimization the process is split into three
65 stages (compile time, linktime analysis and parallel linktime as
66 indicated bellow).
68 Compile time:
70 1) Inter-procedural optimization.
71 (ipa_passes)
73 This part is further split into:
75 a) early optimizations. These are local passes executed in
76 the topological order on the callgraph.
78 The purpose of early optimiations is to optimize away simple
79 things that may otherwise confuse IP analysis. Very simple
80 propagation across the callgraph is done i.e. to discover
81 functions without side effects and simple inlining is performed.
83 b) early small interprocedural passes.
85 Those are interprocedural passes executed only at compilation
86 time. These include, for example, transational memory lowering,
87 unreachable code removal and other simple transformations.
89 c) IP analysis stage. All interprocedural passes do their
90 analysis.
92 Interprocedural passes differ from small interprocedural
93 passes by their ability to operate across whole program
94 at linktime. Their analysis stage is performed early to
95 both reduce linking times and linktime memory usage by
96 not having to represent whole program in memory.
98 d) LTO sreaming. When doing LTO, everything important gets
99 streamed into the object file.
101 Compile time and or linktime analysis stage (WPA):
103 At linktime units gets streamed back and symbol table is
104 merged. Function bodies are not streamed in and not
105 available.
106 e) IP propagation stage. All IP passes execute their
107 IP propagation. This is done based on the earlier analysis
108 without having function bodies at hand.
109 f) Ltrans streaming. When doing WHOPR LTO, the program
110 is partitioned and streamed into multple object files.
112 Compile time and/or parallel linktime stage (ltrans)
114 Each of the object files is streamed back and compiled
115 separately. Now the function bodies becomes available
116 again.
118 2) Virtual clone materialization
119 (cgraph_materialize_clone)
121 IP passes can produce copies of existing functoins (such
122 as versioned clones or inline clones) without actually
123 manipulating their bodies by creating virtual clones in
124 the callgraph. At this time the virtual clones are
125 turned into real functions
126 3) IP transformation
128 All IP passes transform function bodies based on earlier
129 decision of the IP propagation.
131 4) late small IP passes
133 Simple IP passes working within single program partition.
135 5) Expansion
136 (expand_all_functions)
138 At this stage functions that needs to be output into
139 assembler are identified and compiled in topological order
140 6) Output of variables and aliases
141 Now it is known what variable references was not optimized
142 out and thus all variables are output to the file.
144 Note that with -fno-toplevel-reorder passes 5 and 6
145 are combined together in cgraph_output_in_order.
147 Finally there are functions to manipulate the callgraph from
148 backend.
149 - cgraph_add_new_function is used to add backend produced
150 functions introduced after the unit is finalized.
151 The functions are enqueue for later processing and inserted
152 into callgraph with cgraph_process_new_functions.
154 - cgraph_function_versioning
156 produces a copy of function into new one (a version)
157 and apply simple transformations
160 #include "config.h"
161 #include "system.h"
162 #include "coretypes.h"
163 #include "tm.h"
164 #include "tree.h"
165 #include "varasm.h"
166 #include "stor-layout.h"
167 #include "stringpool.h"
168 #include "output.h"
169 #include "rtl.h"
170 #include "basic-block.h"
171 #include "tree-ssa-alias.h"
172 #include "internal-fn.h"
173 #include "gimple-fold.h"
174 #include "gimple-expr.h"
175 #include "is-a.h"
176 #include "gimple.h"
177 #include "gimplify.h"
178 #include "gimple-iterator.h"
179 #include "gimplify-me.h"
180 #include "gimple-ssa.h"
181 #include "tree-cfg.h"
182 #include "tree-into-ssa.h"
183 #include "tree-ssa.h"
184 #include "tree-inline.h"
185 #include "langhooks.h"
186 #include "toplev.h"
187 #include "flags.h"
188 #include "debug.h"
189 #include "target.h"
190 #include "diagnostic.h"
191 #include "params.h"
192 #include "fibheap.h"
193 #include "intl.h"
194 #include "function.h"
195 #include "ipa-prop.h"
196 #include "tree-iterator.h"
197 #include "tree-pass.h"
198 #include "tree-dump.h"
199 #include "gimple-pretty-print.h"
200 #include "output.h"
201 #include "coverage.h"
202 #include "plugin.h"
203 #include "ipa-inline.h"
204 #include "ipa-utils.h"
205 #include "lto-streamer.h"
206 #include "except.h"
207 #include "cfgloop.h"
208 #include "regset.h" /* FIXME: For reg_obstack. */
209 #include "context.h"
210 #include "pass_manager.h"
211 #include "tree-nested.h"
212 #include "gimplify.h"
213 #include "dbgcnt.h"
215 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
216 secondary queue used during optimization to accommodate passes that
217 may generate new functions that need to be optimized and expanded. */
218 vec<cgraph_node *> cgraph_new_nodes;
220 static void expand_all_functions (void);
221 static void mark_functions_to_output (void);
222 static void handle_alias_pairs (void);
224 /* Used for vtable lookup in thunk adjusting. */
225 static GTY (()) tree vtable_entry_type;
227 /* Determine if symbol declaration is needed. That is, visible to something
228 either outside this translation unit, something magic in the system
229 configury */
230 bool
231 symtab_node::needed_p (void)
233 /* Double check that no one output the function into assembly file
234 early. */
235 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)
236 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
238 if (!definition)
239 return false;
241 if (DECL_EXTERNAL (decl))
242 return false;
244 /* If the user told us it is used, then it must be so. */
245 if (force_output)
246 return true;
248 /* ABI forced symbols are needed when they are external. */
249 if (forced_by_abi && TREE_PUBLIC (decl))
250 return true;
252 /* Keep constructors, destructors and virtual functions. */
253 if (TREE_CODE (decl) == FUNCTION_DECL
254 && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)))
255 return true;
257 /* Externally visible variables must be output. The exception is
258 COMDAT variables that must be output only when they are needed. */
259 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
260 return true;
262 return false;
265 /* Head and terminator of the queue of nodes to be processed while building
266 callgraph. */
268 static symtab_node symtab_terminator;
269 static symtab_node *queued_nodes = &symtab_terminator;
271 /* Add NODE to queue starting at QUEUED_NODES.
272 The queue is linked via AUX pointers and terminated by pointer to 1. */
274 static void
275 enqueue_node (symtab_node *node)
277 if (node->aux)
278 return;
279 gcc_checking_assert (queued_nodes);
280 node->aux = queued_nodes;
281 queued_nodes = node;
284 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
285 functions into callgraph in a way so they look like ordinary reachable
286 functions inserted into callgraph already at construction time. */
288 void
289 symbol_table::process_new_functions (void)
291 tree fndecl;
293 if (!cgraph_new_nodes.exists ())
294 return;
296 handle_alias_pairs ();
297 /* Note that this queue may grow as its being processed, as the new
298 functions may generate new ones. */
299 for (unsigned i = 0; i < cgraph_new_nodes.length (); i++)
301 cgraph_node *node = cgraph_new_nodes[i];
302 fndecl = node->decl;
303 switch (state)
305 case CONSTRUCTION:
306 /* At construction time we just need to finalize function and move
307 it into reachable functions list. */
309 cgraph_node::finalize_function (fndecl, false);
310 call_cgraph_insertion_hooks (node);
311 enqueue_node (node);
312 break;
314 case IPA:
315 case IPA_SSA:
316 /* When IPA optimization already started, do all essential
317 transformations that has been already performed on the whole
318 cgraph but not on this function. */
320 gimple_register_cfg_hooks ();
321 if (!node->analyzed)
322 node->analyze ();
323 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
324 if (state == IPA_SSA
325 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
326 g->get_passes ()->execute_early_local_passes ();
327 else if (inline_summary_vec != NULL)
328 compute_inline_parameters (node, true);
329 free_dominance_info (CDI_POST_DOMINATORS);
330 free_dominance_info (CDI_DOMINATORS);
331 pop_cfun ();
332 break;
334 case EXPANSION:
335 /* Functions created during expansion shall be compiled
336 directly. */
337 node->process = 0;
338 call_cgraph_insertion_hooks (node);
339 node->expand ();
340 break;
342 default:
343 gcc_unreachable ();
344 break;
348 cgraph_new_nodes.release ();
351 /* As an GCC extension we allow redefinition of the function. The
352 semantics when both copies of bodies differ is not well defined.
353 We replace the old body with new body so in unit at a time mode
354 we always use new body, while in normal mode we may end up with
355 old body inlined into some functions and new body expanded and
356 inlined in others.
358 ??? It may make more sense to use one body for inlining and other
359 body for expanding the function but this is difficult to do. */
361 void
362 cgraph_node::reset (void)
364 /* If process is set, then we have already begun whole-unit analysis.
365 This is *not* testing for whether we've already emitted the function.
366 That case can be sort-of legitimately seen with real function redefinition
367 errors. I would argue that the front end should never present us with
368 such a case, but don't enforce that for now. */
369 gcc_assert (!process);
371 /* Reset our data structures so we can analyze the function again. */
372 memset (&local, 0, sizeof (local));
373 memset (&global, 0, sizeof (global));
374 memset (&rtl, 0, sizeof (rtl));
375 analyzed = false;
376 definition = false;
377 alias = false;
378 weakref = false;
379 cpp_implicit_alias = false;
381 remove_callees ();
382 remove_all_references ();
385 /* Return true when there are references to the node. */
387 bool
388 symtab_node::referred_to_p (void)
390 ipa_ref *ref = NULL;
392 /* See if there are any references at all. */
393 if (iterate_referring (0, ref))
394 return true;
395 /* For functions check also calls. */
396 cgraph_node *cn = dyn_cast <cgraph_node *> (this);
397 if (cn && cn->callers)
398 return true;
399 return false;
402 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
403 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
404 the garbage collector run at the moment. We would need to either create
405 a new GC context, or just not compile right now. */
407 void
408 cgraph_node::finalize_function (tree decl, bool no_collect)
410 cgraph_node *node = cgraph_node::get_create (decl);
412 if (node->definition)
414 /* Nested functions should only be defined once. */
415 gcc_assert (!DECL_CONTEXT (decl)
416 || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL);
417 node->reset ();
418 node->local.redefined_extern_inline = true;
421 notice_global_symbol (decl);
422 node->definition = true;
423 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
425 /* With -fkeep-inline-functions we are keeping all inline functions except
426 for extern inline ones. */
427 if (flag_keep_inline_functions
428 && DECL_DECLARED_INLINE_P (decl)
429 && !DECL_EXTERNAL (decl)
430 && !DECL_DISREGARD_INLINE_LIMITS (decl))
431 node->force_output = 1;
433 /* When not optimizing, also output the static functions. (see
434 PR24561), but don't do so for always_inline functions, functions
435 declared inline and nested functions. These were optimized out
436 in the original implementation and it is unclear whether we want
437 to change the behavior here. */
438 if ((!optimize
439 && !node->cpp_implicit_alias
440 && !DECL_DISREGARD_INLINE_LIMITS (decl)
441 && !DECL_DECLARED_INLINE_P (decl)
442 && !(DECL_CONTEXT (decl)
443 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
444 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
445 node->force_output = 1;
447 /* If we've not yet emitted decl, tell the debug info about it. */
448 if (!TREE_ASM_WRITTEN (decl))
449 (*debug_hooks->deferred_inline_function) (decl);
451 /* Possibly warn about unused parameters. */
452 if (warn_unused_parameter)
453 do_warn_unused_parameter (decl);
455 if (!no_collect)
456 ggc_collect ();
458 if (symtab->state == CONSTRUCTION
459 && (node->needed_p () || node->referred_to_p ()))
460 enqueue_node (node);
463 /* Add the function FNDECL to the call graph.
464 Unlike finalize_function, this function is intended to be used
465 by middle end and allows insertion of new function at arbitrary point
466 of compilation. The function can be either in high, low or SSA form
467 GIMPLE.
469 The function is assumed to be reachable and have address taken (so no
470 API breaking optimizations are performed on it).
472 Main work done by this function is to enqueue the function for later
473 processing to avoid need the passes to be re-entrant. */
475 void
476 cgraph_node::add_new_function (tree fndecl, bool lowered)
478 gcc::pass_manager *passes = g->get_passes ();
479 cgraph_node *node;
480 switch (symtab->state)
482 case PARSING:
483 cgraph_node::finalize_function (fndecl, false);
484 break;
485 case CONSTRUCTION:
486 /* Just enqueue function to be processed at nearest occurrence. */
487 node = cgraph_node::get_create (fndecl);
488 if (lowered)
489 node->lowered = true;
490 cgraph_new_nodes.safe_push (node);
491 break;
493 case IPA:
494 case IPA_SSA:
495 case EXPANSION:
496 /* Bring the function into finalized state and enqueue for later
497 analyzing and compilation. */
498 node = cgraph_node::get_create (fndecl);
499 node->local.local = false;
500 node->definition = true;
501 node->force_output = true;
502 if (!lowered && symtab->state == EXPANSION)
504 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
505 gimple_register_cfg_hooks ();
506 bitmap_obstack_initialize (NULL);
507 execute_pass_list (cfun, passes->all_lowering_passes);
508 passes->execute_early_local_passes ();
509 bitmap_obstack_release (NULL);
510 pop_cfun ();
512 lowered = true;
514 if (lowered)
515 node->lowered = true;
516 cgraph_new_nodes.safe_push (node);
517 break;
519 case FINISHED:
520 /* At the very end of compilation we have to do all the work up
521 to expansion. */
522 node = cgraph_node::create (fndecl);
523 if (lowered)
524 node->lowered = true;
525 node->definition = true;
526 node->analyze ();
527 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
528 gimple_register_cfg_hooks ();
529 bitmap_obstack_initialize (NULL);
530 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
531 g->get_passes ()->execute_early_local_passes ();
532 bitmap_obstack_release (NULL);
533 pop_cfun ();
534 node->expand ();
535 break;
537 default:
538 gcc_unreachable ();
541 /* Set a personality if required and we already passed EH lowering. */
542 if (lowered
543 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
544 == eh_personality_lang))
545 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
548 /* Output all asm statements we have stored up to be output. */
550 void
551 symbol_table::output_asm_statements (void)
553 asm_node *can;
555 if (seen_error ())
556 return;
558 for (can = first_asm_symbol (); can; can = can->next)
559 assemble_asm (can->asm_str);
561 clear_asm_symbols ();
564 /* Analyze the function scheduled to be output. */
565 void
566 cgraph_node::analyze (void)
568 tree decl = this->decl;
569 location_t saved_loc = input_location;
570 input_location = DECL_SOURCE_LOCATION (decl);
572 if (thunk.thunk_p)
574 create_edge (cgraph_node::get (thunk.alias),
575 NULL, 0, CGRAPH_FREQ_BASE);
576 if (!expand_thunk (false, false))
578 thunk.alias = NULL;
579 analyzed = true;
580 return;
582 thunk.alias = NULL;
584 if (alias)
585 resolve_alias (cgraph_node::get (alias_target));
586 else if (dispatcher_function)
588 /* Generate the dispatcher body of multi-versioned functions. */
589 cgraph_function_version_info *dispatcher_version_info
590 = function_version ();
591 if (dispatcher_version_info != NULL
592 && (dispatcher_version_info->dispatcher_resolver
593 == NULL_TREE))
595 tree resolver = NULL_TREE;
596 gcc_assert (targetm.generate_version_dispatcher_body);
597 resolver = targetm.generate_version_dispatcher_body (this);
598 gcc_assert (resolver != NULL_TREE);
601 else
603 push_cfun (DECL_STRUCT_FUNCTION (decl));
605 assign_assembler_name_if_neeeded (decl);
607 /* Make sure to gimplify bodies only once. During analyzing a
608 function we lower it, which will require gimplified nested
609 functions, so we can end up here with an already gimplified
610 body. */
611 if (!gimple_has_body_p (decl))
612 gimplify_function_tree (decl);
613 dump_function (TDI_generic, decl);
615 /* Lower the function. */
616 if (!lowered)
618 if (nested)
619 lower_nested_functions (decl);
620 gcc_assert (!nested);
622 gimple_register_cfg_hooks ();
623 bitmap_obstack_initialize (NULL);
624 execute_pass_list (cfun, g->get_passes ()->all_lowering_passes);
625 free_dominance_info (CDI_POST_DOMINATORS);
626 free_dominance_info (CDI_DOMINATORS);
627 compact_blocks ();
628 bitmap_obstack_release (NULL);
629 lowered = true;
632 pop_cfun ();
634 analyzed = true;
636 input_location = saved_loc;
639 /* C++ frontend produce same body aliases all over the place, even before PCH
640 gets streamed out. It relies on us linking the aliases with their function
641 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
642 first produce aliases without links, but once C++ FE is sure he won't sream
643 PCH we build the links via this function. */
645 void
646 symbol_table::process_same_body_aliases (void)
648 symtab_node *node;
649 FOR_EACH_SYMBOL (node)
650 if (node->cpp_implicit_alias && !node->analyzed)
651 node->resolve_alias
652 (TREE_CODE (node->alias_target) == VAR_DECL
653 ? (symtab_node *)varpool_node::get_create (node->alias_target)
654 : (symtab_node *)cgraph_node::get_create (node->alias_target));
655 cpp_implicit_aliases_done = true;
658 /* Process attributes common for vars and functions. */
660 static void
661 process_common_attributes (tree decl)
663 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
665 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
667 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
668 "%<weakref%> attribute should be accompanied with"
669 " an %<alias%> attribute");
670 DECL_WEAK (decl) = 0;
671 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
672 DECL_ATTRIBUTES (decl));
676 /* Look for externally_visible and used attributes and mark cgraph nodes
677 accordingly.
679 We cannot mark the nodes at the point the attributes are processed (in
680 handle_*_attribute) because the copy of the declarations available at that
681 point may not be canonical. For example, in:
683 void f();
684 void f() __attribute__((used));
686 the declaration we see in handle_used_attribute will be the second
687 declaration -- but the front end will subsequently merge that declaration
688 with the original declaration and discard the second declaration.
690 Furthermore, we can't mark these nodes in finalize_function because:
692 void f() {}
693 void f() __attribute__((externally_visible));
695 is valid.
697 So, we walk the nodes at the end of the translation unit, applying the
698 attributes at that point. */
700 static void
701 process_function_and_variable_attributes (cgraph_node *first,
702 varpool_node *first_var)
704 cgraph_node *node;
705 varpool_node *vnode;
707 for (node = symtab->first_function (); node != first;
708 node = symtab->next_function (node))
710 tree decl = node->decl;
711 if (DECL_PRESERVE_P (decl))
712 node->mark_force_output ();
713 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
715 if (! TREE_PUBLIC (node->decl))
716 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
717 "%<externally_visible%>"
718 " attribute have effect only on public objects");
720 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
721 && (node->definition && !node->alias))
723 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
724 "%<weakref%> attribute ignored"
725 " because function is defined");
726 DECL_WEAK (decl) = 0;
727 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
728 DECL_ATTRIBUTES (decl));
731 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
732 && !DECL_DECLARED_INLINE_P (decl)
733 /* redefining extern inline function makes it DECL_UNINLINABLE. */
734 && !DECL_UNINLINABLE (decl))
735 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
736 "always_inline function might not be inlinable");
738 process_common_attributes (decl);
740 for (vnode = symtab->first_variable (); vnode != first_var;
741 vnode = symtab->next_variable (vnode))
743 tree decl = vnode->decl;
744 if (DECL_EXTERNAL (decl)
745 && DECL_INITIAL (decl))
746 varpool_node::finalize_decl (decl);
747 if (DECL_PRESERVE_P (decl))
748 vnode->force_output = true;
749 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
751 if (! TREE_PUBLIC (vnode->decl))
752 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
753 "%<externally_visible%>"
754 " attribute have effect only on public objects");
756 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
757 && vnode->definition
758 && DECL_INITIAL (decl))
760 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
761 "%<weakref%> attribute ignored"
762 " because variable is initialized");
763 DECL_WEAK (decl) = 0;
764 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
765 DECL_ATTRIBUTES (decl));
767 process_common_attributes (decl);
771 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
772 middle end to output the variable to asm file, if needed or externally
773 visible. */
775 void
776 varpool_node::finalize_decl (tree decl)
778 varpool_node *node = varpool_node::get_create (decl);
780 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
782 if (node->definition)
783 return;
784 notice_global_symbol (decl);
785 node->definition = true;
786 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
787 /* Traditionally we do not eliminate static variables when not
788 optimizing and when not doing toplevel reoder. */
789 || (!flag_toplevel_reorder && !DECL_COMDAT (node->decl)
790 && !DECL_ARTIFICIAL (node->decl)))
791 node->force_output = true;
793 if (symtab->state == CONSTRUCTION
794 && (node->needed_p () || node->referred_to_p ()))
795 enqueue_node (node);
796 if (symtab->state >= IPA_SSA)
797 node->analyze ();
798 /* Some frontends produce various interface variables after compilation
799 finished. */
800 if (symtab->state == FINISHED
801 || (!flag_toplevel_reorder
802 && symtab->state == EXPANSION))
803 node->assemble_decl ();
806 /* EDGE is an polymorphic call. Mark all possible targets as reachable
807 and if there is only one target, perform trivial devirtualization.
808 REACHABLE_CALL_TARGETS collects target lists we already walked to
809 avoid udplicate work. */
811 static void
812 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
813 cgraph_edge *edge)
815 unsigned int i;
816 void *cache_token;
817 bool final;
818 vec <cgraph_node *>targets
819 = possible_polymorphic_call_targets
820 (edge, &final, &cache_token);
822 if (!reachable_call_targets->add (cache_token))
824 if (symtab->dump_file)
825 dump_possible_polymorphic_call_targets
826 (symtab->dump_file, edge);
828 for (i = 0; i < targets.length (); i++)
830 /* Do not bother to mark virtual methods in anonymous namespace;
831 either we will find use of virtual table defining it, or it is
832 unused. */
833 if (targets[i]->definition
834 && TREE_CODE
835 (TREE_TYPE (targets[i]->decl))
836 == METHOD_TYPE
837 && !type_in_anonymous_namespace_p
838 (method_class_type
839 (TREE_TYPE (targets[i]->decl))))
840 enqueue_node (targets[i]);
844 /* Very trivial devirtualization; when the type is
845 final or anonymous (so we know all its derivation)
846 and there is only one possible virtual call target,
847 make the edge direct. */
848 if (final)
850 if (targets.length () <= 1 && dbg_cnt (devirt))
852 cgraph_node *target;
853 if (targets.length () == 1)
854 target = targets[0];
855 else
856 target = cgraph_node::create
857 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
859 if (symtab->dump_file)
861 fprintf (symtab->dump_file,
862 "Devirtualizing call: ");
863 print_gimple_stmt (symtab->dump_file,
864 edge->call_stmt, 0,
865 TDF_SLIM);
867 if (dump_enabled_p ())
869 location_t locus = gimple_location_safe (edge->call_stmt);
870 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
871 "devirtualizing call in %s to %s\n",
872 edge->caller->name (), target->name ());
875 edge->make_direct (target);
876 edge->redirect_call_stmt_to_callee ();
877 if (symtab->dump_file)
879 fprintf (symtab->dump_file,
880 "Devirtualized as: ");
881 print_gimple_stmt (symtab->dump_file,
882 edge->call_stmt, 0,
883 TDF_SLIM);
890 /* Discover all functions and variables that are trivially needed, analyze
891 them as well as all functions and variables referred by them */
893 static void
894 analyze_functions (void)
896 /* Keep track of already processed nodes when called multiple times for
897 intermodule optimization. */
898 static cgraph_node *first_analyzed;
899 cgraph_node *first_handled = first_analyzed;
900 static varpool_node *first_analyzed_var;
901 varpool_node *first_handled_var = first_analyzed_var;
902 hash_set<void *> reachable_call_targets;
904 symtab_node *node;
905 symtab_node *next;
906 int i;
907 ipa_ref *ref;
908 bool changed = true;
909 location_t saved_loc = input_location;
911 bitmap_obstack_initialize (NULL);
912 symtab->state = CONSTRUCTION;
913 input_location = UNKNOWN_LOCATION;
915 /* Ugly, but the fixup can not happen at a time same body alias is created;
916 C++ FE is confused about the COMDAT groups being right. */
917 if (symtab->cpp_implicit_aliases_done)
918 FOR_EACH_SYMBOL (node)
919 if (node->cpp_implicit_alias)
920 node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
921 if (optimize && flag_devirtualize)
922 build_type_inheritance_graph ();
924 /* Analysis adds static variables that in turn adds references to new functions.
925 So we need to iterate the process until it stabilize. */
926 while (changed)
928 changed = false;
929 process_function_and_variable_attributes (first_analyzed,
930 first_analyzed_var);
932 /* First identify the trivially needed symbols. */
933 for (node = symtab->first_symbol ();
934 node != first_analyzed
935 && node != first_analyzed_var; node = node->next)
937 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
938 node->get_comdat_group_id ();
939 if (node->needed_p ())
941 enqueue_node (node);
942 if (!changed && symtab->dump_file)
943 fprintf (symtab->dump_file, "Trivially needed symbols:");
944 changed = true;
945 if (symtab->dump_file)
946 fprintf (symtab->dump_file, " %s", node->asm_name ());
947 if (!changed && symtab->dump_file)
948 fprintf (symtab->dump_file, "\n");
950 if (node == first_analyzed
951 || node == first_analyzed_var)
952 break;
954 symtab->process_new_functions ();
955 first_analyzed_var = symtab->first_variable ();
956 first_analyzed = symtab->first_function ();
958 if (changed && symtab->dump_file)
959 fprintf (symtab->dump_file, "\n");
961 /* Lower representation, build callgraph edges and references for all trivially
962 needed symbols and all symbols referred by them. */
963 while (queued_nodes != &symtab_terminator)
965 changed = true;
966 node = queued_nodes;
967 queued_nodes = (symtab_node *)queued_nodes->aux;
968 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
969 if (cnode && cnode->definition)
971 cgraph_edge *edge;
972 tree decl = cnode->decl;
974 /* ??? It is possible to create extern inline function
975 and later using weak alias attribute to kill its body.
976 See gcc.c-torture/compile/20011119-1.c */
977 if (!DECL_STRUCT_FUNCTION (decl)
978 && !cnode->alias
979 && !cnode->thunk.thunk_p
980 && !cnode->dispatcher_function)
982 cnode->reset ();
983 cnode->local.redefined_extern_inline = true;
984 continue;
987 if (!cnode->analyzed)
988 cnode->analyze ();
990 for (edge = cnode->callees; edge; edge = edge->next_callee)
991 if (edge->callee->definition)
992 enqueue_node (edge->callee);
993 if (optimize && flag_devirtualize)
995 cgraph_edge *next;
997 for (edge = cnode->indirect_calls; edge; edge = next)
999 next = edge->next_callee;
1000 if (edge->indirect_info->polymorphic)
1001 walk_polymorphic_call_targets (&reachable_call_targets,
1002 edge);
1006 /* If decl is a clone of an abstract function,
1007 mark that abstract function so that we don't release its body.
1008 The DECL_INITIAL() of that abstract function declaration
1009 will be later needed to output debug info. */
1010 if (DECL_ABSTRACT_ORIGIN (decl))
1012 cgraph_node *origin_node
1013 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl));
1014 origin_node->used_as_abstract_origin = true;
1017 else
1019 varpool_node *vnode = dyn_cast <varpool_node *> (node);
1020 if (vnode && vnode->definition && !vnode->analyzed)
1021 vnode->analyze ();
1024 if (node->same_comdat_group)
1026 symtab_node *next;
1027 for (next = node->same_comdat_group;
1028 next != node;
1029 next = next->same_comdat_group)
1030 enqueue_node (next);
1032 for (i = 0; node->iterate_reference (i, ref); i++)
1033 if (ref->referred->definition)
1034 enqueue_node (ref->referred);
1035 symtab->process_new_functions ();
1038 if (optimize && flag_devirtualize)
1039 update_type_inheritance_graph ();
1041 /* Collect entry points to the unit. */
1042 if (symtab->dump_file)
1044 fprintf (symtab->dump_file, "\n\nInitial ");
1045 symtab_node::dump_table (symtab->dump_file);
1048 if (symtab->dump_file)
1049 fprintf (symtab->dump_file, "\nRemoving unused symbols:");
1051 for (node = symtab->first_symbol ();
1052 node != first_handled
1053 && node != first_handled_var; node = next)
1055 next = node->next;
1056 if (!node->aux && !node->referred_to_p ())
1058 if (symtab->dump_file)
1059 fprintf (symtab->dump_file, " %s", node->name ());
1060 node->remove ();
1061 continue;
1063 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1065 tree decl = node->decl;
1067 if (cnode->definition && !gimple_has_body_p (decl)
1068 && !cnode->alias
1069 && !cnode->thunk.thunk_p)
1070 cnode->reset ();
1072 gcc_assert (!cnode->definition || cnode->thunk.thunk_p
1073 || cnode->alias
1074 || gimple_has_body_p (decl));
1075 gcc_assert (cnode->analyzed == cnode->definition);
1077 node->aux = NULL;
1079 for (;node; node = node->next)
1080 node->aux = NULL;
1081 first_analyzed = symtab->first_function ();
1082 first_analyzed_var = symtab->first_variable ();
1083 if (symtab->dump_file)
1085 fprintf (symtab->dump_file, "\n\nReclaimed ");
1086 symtab_node::dump_table (symtab->dump_file);
1088 bitmap_obstack_release (NULL);
1089 ggc_collect ();
1090 /* Initialize assembler name hash, in particular we want to trigger C++
1091 mangling and same body alias creation before we free DECL_ARGUMENTS
1092 used by it. */
1093 if (!seen_error ())
1094 symtab->symtab_initialize_asm_name_hash ();
1096 input_location = saved_loc;
1099 /* Translate the ugly representation of aliases as alias pairs into nice
1100 representation in callgraph. We don't handle all cases yet,
1101 unfortunately. */
1103 static void
1104 handle_alias_pairs (void)
1106 alias_pair *p;
1107 unsigned i;
1109 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1111 symtab_node *target_node = symtab_node::get_for_asmname (p->target);
1113 /* Weakrefs with target not defined in current unit are easy to handle:
1114 they behave just as external variables except we need to note the
1115 alias flag to later output the weakref pseudo op into asm file. */
1116 if (!target_node
1117 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1119 symtab_node *node = symtab_node::get (p->decl);
1120 if (node)
1122 node->alias_target = p->target;
1123 node->weakref = true;
1124 node->alias = true;
1126 alias_pairs->unordered_remove (i);
1127 continue;
1129 else if (!target_node)
1131 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1132 symtab_node *node = symtab_node::get (p->decl);
1133 if (node)
1134 node->alias = false;
1135 alias_pairs->unordered_remove (i);
1136 continue;
1139 if (DECL_EXTERNAL (target_node->decl)
1140 /* We use local aliases for C++ thunks to force the tailcall
1141 to bind locally. This is a hack - to keep it working do
1142 the following (which is not strictly correct). */
1143 && (TREE_CODE (target_node->decl) != FUNCTION_DECL
1144 || ! DECL_VIRTUAL_P (target_node->decl))
1145 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1147 error ("%q+D aliased to external symbol %qE",
1148 p->decl, p->target);
1151 if (TREE_CODE (p->decl) == FUNCTION_DECL
1152 && target_node && is_a <cgraph_node *> (target_node))
1154 cgraph_node *src_node = cgraph_node::get (p->decl);
1155 if (src_node && src_node->definition)
1156 src_node->reset ();
1157 cgraph_node::create_alias (p->decl, target_node->decl);
1158 alias_pairs->unordered_remove (i);
1160 else if (TREE_CODE (p->decl) == VAR_DECL
1161 && target_node && is_a <varpool_node *> (target_node))
1163 varpool_node::create_alias (p->decl, target_node->decl);
1164 alias_pairs->unordered_remove (i);
1166 else
1168 error ("%q+D alias in between function and variable is not supported",
1169 p->decl);
1170 warning (0, "%q+D aliased declaration",
1171 target_node->decl);
1172 alias_pairs->unordered_remove (i);
1175 vec_free (alias_pairs);
1179 /* Figure out what functions we want to assemble. */
1181 static void
1182 mark_functions_to_output (void)
1184 cgraph_node *node;
1185 #ifdef ENABLE_CHECKING
1186 bool check_same_comdat_groups = false;
1188 FOR_EACH_FUNCTION (node)
1189 gcc_assert (!node->process);
1190 #endif
1192 FOR_EACH_FUNCTION (node)
1194 tree decl = node->decl;
1196 gcc_assert (!node->process || node->same_comdat_group);
1197 if (node->process)
1198 continue;
1200 /* We need to output all local functions that are used and not
1201 always inlined, as well as those that are reachable from
1202 outside the current compilation unit. */
1203 if (node->analyzed
1204 && !node->thunk.thunk_p
1205 && !node->alias
1206 && !node->global.inlined_to
1207 && !TREE_ASM_WRITTEN (decl)
1208 && !DECL_EXTERNAL (decl))
1210 node->process = 1;
1211 if (node->same_comdat_group)
1213 cgraph_node *next;
1214 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
1215 next != node;
1216 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
1217 if (!next->thunk.thunk_p && !next->alias
1218 && !next->comdat_local_p ())
1219 next->process = 1;
1222 else if (node->same_comdat_group)
1224 #ifdef ENABLE_CHECKING
1225 check_same_comdat_groups = true;
1226 #endif
1228 else
1230 /* We should've reclaimed all functions that are not needed. */
1231 #ifdef ENABLE_CHECKING
1232 if (!node->global.inlined_to
1233 && gimple_has_body_p (decl)
1234 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1235 are inside partition, we can end up not removing the body since we no longer
1236 have analyzed node pointing to it. */
1237 && !node->in_other_partition
1238 && !node->alias
1239 && !node->clones
1240 && !DECL_EXTERNAL (decl))
1242 node->debug ();
1243 internal_error ("failed to reclaim unneeded function");
1245 #endif
1246 gcc_assert (node->global.inlined_to
1247 || !gimple_has_body_p (decl)
1248 || node->in_other_partition
1249 || node->clones
1250 || DECL_ARTIFICIAL (decl)
1251 || DECL_EXTERNAL (decl));
1256 #ifdef ENABLE_CHECKING
1257 if (check_same_comdat_groups)
1258 FOR_EACH_FUNCTION (node)
1259 if (node->same_comdat_group && !node->process)
1261 tree decl = node->decl;
1262 if (!node->global.inlined_to
1263 && gimple_has_body_p (decl)
1264 /* FIXME: in an ltrans unit when the offline copy is outside a
1265 partition but inline copies are inside a partition, we can
1266 end up not removing the body since we no longer have an
1267 analyzed node pointing to it. */
1268 && !node->in_other_partition
1269 && !node->clones
1270 && !DECL_EXTERNAL (decl))
1272 node->debug ();
1273 internal_error ("failed to reclaim unneeded function in same "
1274 "comdat group");
1277 #endif
1280 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1281 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1283 Set current_function_decl and cfun to newly constructed empty function body.
1284 return basic block in the function body. */
1286 basic_block
1287 init_lowered_empty_function (tree decl, bool in_ssa)
1289 basic_block bb;
1291 current_function_decl = decl;
1292 allocate_struct_function (decl, false);
1293 gimple_register_cfg_hooks ();
1294 init_empty_tree_cfg ();
1296 if (in_ssa)
1298 init_tree_ssa (cfun);
1299 init_ssa_operands (cfun);
1300 cfun->gimple_df->in_ssa_p = true;
1301 cfun->curr_properties |= PROP_ssa;
1304 DECL_INITIAL (decl) = make_node (BLOCK);
1306 DECL_SAVED_TREE (decl) = error_mark_node;
1307 cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1308 | PROP_cfg | PROP_loops);
1310 set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ());
1311 init_loops_structure (cfun, loops_for_fn (cfun), 1);
1312 loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1314 /* Create BB for body of the function and connect it properly. */
1315 bb = create_basic_block (NULL, (void *) 0, ENTRY_BLOCK_PTR_FOR_FN (cfun));
1316 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU);
1317 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1318 add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
1320 return bb;
1323 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1324 offset indicated by VIRTUAL_OFFSET, if that is
1325 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1326 zero for a result adjusting thunk. */
1328 static tree
1329 thunk_adjust (gimple_stmt_iterator * bsi,
1330 tree ptr, bool this_adjusting,
1331 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1333 gimple stmt;
1334 tree ret;
1336 if (this_adjusting
1337 && fixed_offset != 0)
1339 stmt = gimple_build_assign
1340 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1341 ptr,
1342 fixed_offset));
1343 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1346 /* If there's a virtual offset, look up that value in the vtable and
1347 adjust the pointer again. */
1348 if (virtual_offset)
1350 tree vtabletmp;
1351 tree vtabletmp2;
1352 tree vtabletmp3;
1354 if (!vtable_entry_type)
1356 tree vfunc_type = make_node (FUNCTION_TYPE);
1357 TREE_TYPE (vfunc_type) = integer_type_node;
1358 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1359 layout_type (vfunc_type);
1361 vtable_entry_type = build_pointer_type (vfunc_type);
1364 vtabletmp =
1365 create_tmp_reg (build_pointer_type
1366 (build_pointer_type (vtable_entry_type)), "vptr");
1368 /* The vptr is always at offset zero in the object. */
1369 stmt = gimple_build_assign (vtabletmp,
1370 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1371 ptr));
1372 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1374 /* Form the vtable address. */
1375 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
1376 "vtableaddr");
1377 stmt = gimple_build_assign (vtabletmp2,
1378 build_simple_mem_ref (vtabletmp));
1379 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1381 /* Find the entry with the vcall offset. */
1382 stmt = gimple_build_assign (vtabletmp2,
1383 fold_build_pointer_plus_loc (input_location,
1384 vtabletmp2,
1385 virtual_offset));
1386 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1388 /* Get the offset itself. */
1389 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1390 "vcalloffset");
1391 stmt = gimple_build_assign (vtabletmp3,
1392 build_simple_mem_ref (vtabletmp2));
1393 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1395 /* Adjust the `this' pointer. */
1396 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1397 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1398 GSI_CONTINUE_LINKING);
1401 if (!this_adjusting
1402 && fixed_offset != 0)
1403 /* Adjust the pointer by the constant. */
1405 tree ptrtmp;
1407 if (TREE_CODE (ptr) == VAR_DECL)
1408 ptrtmp = ptr;
1409 else
1411 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
1412 stmt = gimple_build_assign (ptrtmp, ptr);
1413 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1415 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1416 ptrtmp, fixed_offset);
1419 /* Emit the statement and gimplify the adjustment expression. */
1420 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
1421 stmt = gimple_build_assign (ret, ptr);
1422 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1424 return ret;
1427 /* Expand thunk NODE to gimple if possible.
1428 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1429 no assembler is produced.
1430 When OUTPUT_ASM_THUNK is true, also produce assembler for
1431 thunks that are not lowered. */
1433 bool
1434 cgraph_node::expand_thunk (bool output_asm_thunks, bool force_gimple_thunk)
1436 bool this_adjusting = thunk.this_adjusting;
1437 HOST_WIDE_INT fixed_offset = thunk.fixed_offset;
1438 HOST_WIDE_INT virtual_value = thunk.virtual_value;
1439 tree virtual_offset = NULL;
1440 tree alias = callees->callee->decl;
1441 tree thunk_fndecl = decl;
1442 tree a;
1445 if (!force_gimple_thunk && this_adjusting
1446 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1447 virtual_value, alias))
1449 const char *fnname;
1450 tree fn_block;
1451 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1453 if (!output_asm_thunks)
1454 return false;
1456 if (in_lto_p)
1457 get_body ();
1458 a = DECL_ARGUMENTS (thunk_fndecl);
1460 current_function_decl = thunk_fndecl;
1462 /* Ensure thunks are emitted in their correct sections. */
1463 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1465 DECL_RESULT (thunk_fndecl)
1466 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1467 RESULT_DECL, 0, restype);
1468 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1469 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
1471 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1472 create one. */
1473 fn_block = make_node (BLOCK);
1474 BLOCK_VARS (fn_block) = a;
1475 DECL_INITIAL (thunk_fndecl) = fn_block;
1476 init_function_start (thunk_fndecl);
1477 cfun->is_thunk = 1;
1478 insn_locations_init ();
1479 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1480 prologue_location = curr_insn_location ();
1481 assemble_start_function (thunk_fndecl, fnname);
1483 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1484 fixed_offset, virtual_value, alias);
1486 assemble_end_function (thunk_fndecl, fnname);
1487 insn_locations_finalize ();
1488 init_insn_lengths ();
1489 free_after_compilation (cfun);
1490 set_cfun (NULL);
1491 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1492 thunk.thunk_p = false;
1493 analyzed = false;
1495 else
1497 tree restype;
1498 basic_block bb, then_bb, else_bb, return_bb;
1499 gimple_stmt_iterator bsi;
1500 int nargs = 0;
1501 tree arg;
1502 int i;
1503 tree resdecl;
1504 tree restmp = NULL;
1506 gimple call;
1507 gimple ret;
1509 if (in_lto_p)
1510 get_body ();
1511 a = DECL_ARGUMENTS (thunk_fndecl);
1513 current_function_decl = thunk_fndecl;
1515 /* Ensure thunks are emitted in their correct sections. */
1516 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1518 DECL_IGNORED_P (thunk_fndecl) = 1;
1519 bitmap_obstack_initialize (NULL);
1521 if (thunk.virtual_offset_p)
1522 virtual_offset = size_int (virtual_value);
1524 /* Build the return declaration for the function. */
1525 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1526 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1528 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1529 DECL_ARTIFICIAL (resdecl) = 1;
1530 DECL_IGNORED_P (resdecl) = 1;
1531 DECL_RESULT (thunk_fndecl) = resdecl;
1532 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1534 else
1535 resdecl = DECL_RESULT (thunk_fndecl);
1537 bb = then_bb = else_bb = return_bb = init_lowered_empty_function (thunk_fndecl, true);
1539 bsi = gsi_start_bb (bb);
1541 /* Build call to the function being thunked. */
1542 if (!VOID_TYPE_P (restype))
1544 if (DECL_BY_REFERENCE (resdecl))
1545 restmp = gimple_fold_indirect_ref (resdecl);
1546 else if (!is_gimple_reg_type (restype))
1548 restmp = resdecl;
1549 add_local_decl (cfun, restmp);
1550 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1552 else
1553 restmp = create_tmp_reg (restype, "retval");
1556 for (arg = a; arg; arg = DECL_CHAIN (arg))
1557 nargs++;
1558 auto_vec<tree> vargs (nargs);
1559 if (this_adjusting)
1560 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
1561 virtual_offset));
1562 else if (nargs)
1563 vargs.quick_push (a);
1565 if (nargs)
1566 for (i = 1, arg = DECL_CHAIN (a); i < nargs; i++, arg = DECL_CHAIN (arg))
1568 tree tmp = arg;
1569 if (!is_gimple_val (arg))
1571 tmp = create_tmp_reg (TYPE_MAIN_VARIANT
1572 (TREE_TYPE (arg)), "arg");
1573 gimple stmt = gimple_build_assign (tmp, arg);
1574 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1576 vargs.quick_push (tmp);
1578 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1579 callees->call_stmt = call;
1580 gimple_call_set_from_thunk (call, true);
1581 if (restmp)
1583 gimple_call_set_lhs (call, restmp);
1584 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp),
1585 TREE_TYPE (TREE_TYPE (alias))));
1587 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
1588 if (!(gimple_call_flags (call) & ECF_NORETURN))
1590 if (restmp && !this_adjusting
1591 && (fixed_offset || virtual_offset))
1593 tree true_label = NULL_TREE;
1595 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1597 gimple stmt;
1598 /* If the return type is a pointer, we need to
1599 protect against NULL. We know there will be an
1600 adjustment, because that's why we're emitting a
1601 thunk. */
1602 then_bb = create_basic_block (NULL, (void *) 0, bb);
1603 return_bb = create_basic_block (NULL, (void *) 0, then_bb);
1604 else_bb = create_basic_block (NULL, (void *) 0, else_bb);
1605 add_bb_to_loop (then_bb, bb->loop_father);
1606 add_bb_to_loop (return_bb, bb->loop_father);
1607 add_bb_to_loop (else_bb, bb->loop_father);
1608 remove_edge (single_succ_edge (bb));
1609 true_label = gimple_block_label (then_bb);
1610 stmt = gimple_build_cond (NE_EXPR, restmp,
1611 build_zero_cst (TREE_TYPE (restmp)),
1612 NULL_TREE, NULL_TREE);
1613 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1614 make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1615 make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1616 make_edge (return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1617 make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1618 make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1619 bsi = gsi_last_bb (then_bb);
1622 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1623 fixed_offset, virtual_offset);
1624 if (true_label)
1626 gimple stmt;
1627 bsi = gsi_last_bb (else_bb);
1628 stmt = gimple_build_assign (restmp,
1629 build_zero_cst (TREE_TYPE (restmp)));
1630 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1631 bsi = gsi_last_bb (return_bb);
1634 else
1635 gimple_call_set_tail (call, true);
1637 /* Build return value. */
1638 ret = gimple_build_return (restmp);
1639 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1641 else
1643 gimple_call_set_tail (call, true);
1644 remove_edge (single_succ_edge (bb));
1647 cfun->gimple_df->in_ssa_p = true;
1648 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
1649 TREE_ASM_WRITTEN (thunk_fndecl) = false;
1650 delete_unreachable_blocks ();
1651 update_ssa (TODO_update_ssa);
1652 #ifdef ENABLE_CHECKING
1653 verify_flow_info ();
1654 #endif
1655 free_dominance_info (CDI_DOMINATORS);
1657 /* Since we want to emit the thunk, we explicitly mark its name as
1658 referenced. */
1659 thunk.thunk_p = false;
1660 lowered = true;
1661 bitmap_obstack_release (NULL);
1663 current_function_decl = NULL;
1664 set_cfun (NULL);
1665 return true;
1668 /* Assemble thunks and aliases associated to node. */
1670 void
1671 cgraph_node::assemble_thunks_and_aliases (void)
1673 cgraph_edge *e;
1674 ipa_ref *ref;
1676 for (e = callers; e;)
1677 if (e->caller->thunk.thunk_p)
1679 cgraph_node *thunk = e->caller;
1681 e = e->next_caller;
1682 thunk->expand_thunk (true, false);
1683 thunk->assemble_thunks_and_aliases ();
1685 else
1686 e = e->next_caller;
1688 FOR_EACH_ALIAS (this, ref)
1690 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1691 bool saved_written = TREE_ASM_WRITTEN (decl);
1693 /* Force assemble_alias to really output the alias this time instead
1694 of buffering it in same alias pairs. */
1695 TREE_ASM_WRITTEN (decl) = 1;
1696 do_assemble_alias (alias->decl,
1697 DECL_ASSEMBLER_NAME (decl));
1698 alias->assemble_thunks_and_aliases ();
1699 TREE_ASM_WRITTEN (decl) = saved_written;
1703 /* Expand function specified by node. */
1705 void
1706 cgraph_node::expand (void)
1708 location_t saved_loc;
1710 /* We ought to not compile any inline clones. */
1711 gcc_assert (!global.inlined_to);
1713 announce_function (decl);
1714 process = 0;
1715 gcc_assert (lowered);
1716 get_body ();
1718 /* Generate RTL for the body of DECL. */
1720 timevar_push (TV_REST_OF_COMPILATION);
1722 gcc_assert (symtab->global_info_ready);
1724 /* Initialize the default bitmap obstack. */
1725 bitmap_obstack_initialize (NULL);
1727 /* Initialize the RTL code for the function. */
1728 current_function_decl = decl;
1729 saved_loc = input_location;
1730 input_location = DECL_SOURCE_LOCATION (decl);
1731 init_function_start (decl);
1733 gimple_register_cfg_hooks ();
1735 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1737 execute_all_ipa_transforms ();
1739 /* Perform all tree transforms and optimizations. */
1741 /* Signal the start of passes. */
1742 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1744 execute_pass_list (cfun, g->get_passes ()->all_passes);
1746 /* Signal the end of passes. */
1747 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1749 bitmap_obstack_release (&reg_obstack);
1751 /* Release the default bitmap obstack. */
1752 bitmap_obstack_release (NULL);
1754 /* If requested, warn about function definitions where the function will
1755 return a value (usually of some struct or union type) which itself will
1756 take up a lot of stack space. */
1757 if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl))
1759 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
1761 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1762 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1763 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1764 larger_than_size))
1766 unsigned int size_as_int
1767 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1769 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1770 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
1771 decl, size_as_int);
1772 else
1773 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
1774 decl, larger_than_size);
1778 gimple_set_body (decl, NULL);
1779 if (DECL_STRUCT_FUNCTION (decl) == 0
1780 && !cgraph_node::get (decl)->origin)
1782 /* Stop pointing to the local nodes about to be freed.
1783 But DECL_INITIAL must remain nonzero so we know this
1784 was an actual function definition.
1785 For a nested function, this is done in c_pop_function_context.
1786 If rest_of_compilation set this to 0, leave it 0. */
1787 if (DECL_INITIAL (decl) != 0)
1788 DECL_INITIAL (decl) = error_mark_node;
1791 input_location = saved_loc;
1793 ggc_collect ();
1794 timevar_pop (TV_REST_OF_COMPILATION);
1796 /* Make sure that BE didn't give up on compiling. */
1797 gcc_assert (TREE_ASM_WRITTEN (decl));
1798 set_cfun (NULL);
1799 current_function_decl = NULL;
1801 /* It would make a lot more sense to output thunks before function body to get more
1802 forward and lest backwarding jumps. This however would need solving problem
1803 with comdats. See PR48668. Also aliases must come after function itself to
1804 make one pass assemblers, like one on AIX, happy. See PR 50689.
1805 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1806 groups. */
1807 assemble_thunks_and_aliases ();
1808 release_body ();
1809 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1810 points to the dead function body. */
1811 remove_callees ();
1812 remove_all_references ();
1815 /* Node comparer that is responsible for the order that corresponds
1816 to time when a function was launched for the first time. */
1818 static int
1819 node_cmp (const void *pa, const void *pb)
1821 const cgraph_node *a = *(const cgraph_node * const *) pa;
1822 const cgraph_node *b = *(const cgraph_node * const *) pb;
1824 /* Functions with time profile must be before these without profile. */
1825 if (!a->tp_first_run || !b->tp_first_run)
1826 return a->tp_first_run - b->tp_first_run;
1828 return a->tp_first_run != b->tp_first_run
1829 ? b->tp_first_run - a->tp_first_run
1830 : b->order - a->order;
1833 /* Expand all functions that must be output.
1835 Attempt to topologically sort the nodes so function is output when
1836 all called functions are already assembled to allow data to be
1837 propagated across the callgraph. Use a stack to get smaller distance
1838 between a function and its callees (later we may choose to use a more
1839 sophisticated algorithm for function reordering; we will likely want
1840 to use subsections to make the output functions appear in top-down
1841 order). */
1843 static void
1844 expand_all_functions (void)
1846 cgraph_node *node;
1847 cgraph_node **order = XCNEWVEC (cgraph_node *,
1848 symtab->cgraph_count);
1849 unsigned int expanded_func_count = 0, profiled_func_count = 0;
1850 int order_pos, new_order_pos = 0;
1851 int i;
1853 order_pos = ipa_reverse_postorder (order);
1854 gcc_assert (order_pos == symtab->cgraph_count);
1856 /* Garbage collector may remove inline clones we eliminate during
1857 optimization. So we must be sure to not reference them. */
1858 for (i = 0; i < order_pos; i++)
1859 if (order[i]->process)
1860 order[new_order_pos++] = order[i];
1862 if (flag_profile_reorder_functions)
1863 qsort (order, new_order_pos, sizeof (cgraph_node *), node_cmp);
1865 for (i = new_order_pos - 1; i >= 0; i--)
1867 node = order[i];
1869 if (node->process)
1871 expanded_func_count++;
1872 if(node->tp_first_run)
1873 profiled_func_count++;
1875 if (symtab->dump_file)
1876 fprintf (symtab->dump_file,
1877 "Time profile order in expand_all_functions:%s:%d\n",
1878 node->asm_name (), node->tp_first_run);
1879 node->process = 0;
1880 node->expand ();
1884 if (dump_file)
1885 fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
1886 main_input_filename, profiled_func_count, expanded_func_count);
1888 if (symtab->dump_file && flag_profile_reorder_functions)
1889 fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n",
1890 profiled_func_count, expanded_func_count);
1892 symtab->process_new_functions ();
1893 free_gimplify_stack ();
1895 free (order);
1898 /* This is used to sort the node types by the cgraph order number. */
1900 enum cgraph_order_sort_kind
1902 ORDER_UNDEFINED = 0,
1903 ORDER_FUNCTION,
1904 ORDER_VAR,
1905 ORDER_ASM
1908 struct cgraph_order_sort
1910 enum cgraph_order_sort_kind kind;
1911 union
1913 cgraph_node *f;
1914 varpool_node *v;
1915 asm_node *a;
1916 } u;
1919 /* Output all functions, variables, and asm statements in the order
1920 according to their order fields, which is the order in which they
1921 appeared in the file. This implements -fno-toplevel-reorder. In
1922 this mode we may output functions and variables which don't really
1923 need to be output. */
1925 static void
1926 output_in_order (void)
1928 int max;
1929 cgraph_order_sort *nodes;
1930 int i;
1931 cgraph_node *pf;
1932 varpool_node *pv;
1933 asm_node *pa;
1934 max = symtab->order;
1935 nodes = XCNEWVEC (cgraph_order_sort, max);
1937 FOR_EACH_DEFINED_FUNCTION (pf)
1939 if (pf->process && !pf->thunk.thunk_p && !pf->alias)
1941 i = pf->order;
1942 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1943 nodes[i].kind = ORDER_FUNCTION;
1944 nodes[i].u.f = pf;
1948 FOR_EACH_DEFINED_VARIABLE (pv)
1949 if (!DECL_EXTERNAL (pv->decl))
1951 i = pv->order;
1952 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1953 nodes[i].kind = ORDER_VAR;
1954 nodes[i].u.v = pv;
1957 for (pa = symtab->first_asm_symbol (); pa; pa = pa->next)
1959 i = pa->order;
1960 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1961 nodes[i].kind = ORDER_ASM;
1962 nodes[i].u.a = pa;
1965 /* In toplevel reorder mode we output all statics; mark them as needed. */
1967 for (i = 0; i < max; ++i)
1968 if (nodes[i].kind == ORDER_VAR)
1969 nodes[i].u.v->finalize_named_section_flags ();
1971 for (i = 0; i < max; ++i)
1973 switch (nodes[i].kind)
1975 case ORDER_FUNCTION:
1976 nodes[i].u.f->process = 0;
1977 nodes[i].u.f->expand ();
1978 break;
1980 case ORDER_VAR:
1981 nodes[i].u.v->assemble_decl ();
1982 break;
1984 case ORDER_ASM:
1985 assemble_asm (nodes[i].u.a->asm_str);
1986 break;
1988 case ORDER_UNDEFINED:
1989 break;
1991 default:
1992 gcc_unreachable ();
1996 symtab->clear_asm_symbols ();
1998 free (nodes);
2001 static void
2002 ipa_passes (void)
2004 gcc::pass_manager *passes = g->get_passes ();
2006 set_cfun (NULL);
2007 current_function_decl = NULL;
2008 gimple_register_cfg_hooks ();
2009 bitmap_obstack_initialize (NULL);
2011 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2013 if (!in_lto_p)
2015 execute_ipa_pass_list (passes->all_small_ipa_passes);
2016 if (seen_error ())
2017 return;
2020 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2021 devirtualization and other changes where removal iterate. */
2022 symtab->remove_unreachable_nodes (true, symtab->dump_file);
2024 /* If pass_all_early_optimizations was not scheduled, the state of
2025 the cgraph will not be properly updated. Update it now. */
2026 if (symtab->state < IPA_SSA)
2027 symtab->state = IPA_SSA;
2029 if (!in_lto_p)
2031 /* Generate coverage variables and constructors. */
2032 coverage_finish ();
2034 /* Process new functions added. */
2035 set_cfun (NULL);
2036 current_function_decl = NULL;
2037 symtab->process_new_functions ();
2039 execute_ipa_summary_passes
2040 ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2043 /* Some targets need to handle LTO assembler output specially. */
2044 if (flag_generate_lto)
2045 targetm.asm_out.lto_start ();
2047 if (!in_lto_p)
2048 ipa_write_summaries ();
2050 if (flag_generate_lto)
2051 targetm.asm_out.lto_end ();
2053 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
2054 execute_ipa_pass_list (passes->all_regular_ipa_passes);
2055 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2057 bitmap_obstack_release (NULL);
2061 /* Return string alias is alias of. */
2063 static tree
2064 get_alias_symbol (tree decl)
2066 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
2067 return get_identifier (TREE_STRING_POINTER
2068 (TREE_VALUE (TREE_VALUE (alias))));
2072 /* Weakrefs may be associated to external decls and thus not output
2073 at expansion time. Emit all necessary aliases. */
2075 void
2076 symbol_table::output_weakrefs (void)
2078 symtab_node *node;
2079 FOR_EACH_SYMBOL (node)
2080 if (node->alias
2081 && !TREE_ASM_WRITTEN (node->decl)
2082 && node->weakref)
2084 tree target;
2086 /* Weakrefs are special by not requiring target definition in current
2087 compilation unit. It is thus bit hard to work out what we want to
2088 alias.
2089 When alias target is defined, we need to fetch it from symtab reference,
2090 otherwise it is pointed to by alias_target. */
2091 if (node->alias_target)
2092 target = (DECL_P (node->alias_target)
2093 ? DECL_ASSEMBLER_NAME (node->alias_target)
2094 : node->alias_target);
2095 else if (node->analyzed)
2096 target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
2097 else
2099 gcc_unreachable ();
2100 target = get_alias_symbol (node->decl);
2102 do_assemble_alias (node->decl, target);
2106 /* Perform simple optimizations based on callgraph. */
2108 void
2109 symbol_table::compile (void)
2111 if (seen_error ())
2112 return;
2114 #ifdef ENABLE_CHECKING
2115 symtab_node::verify_symtab_nodes ();
2116 #endif
2118 timevar_push (TV_CGRAPHOPT);
2119 if (pre_ipa_mem_report)
2121 fprintf (stderr, "Memory consumption before IPA\n");
2122 dump_memory_report (false);
2124 if (!quiet_flag)
2125 fprintf (stderr, "Performing interprocedural optimizations\n");
2126 state = IPA;
2128 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2129 if (flag_lto)
2130 lto_streamer_hooks_init ();
2132 /* Don't run the IPA passes if there was any error or sorry messages. */
2133 if (!seen_error ())
2134 ipa_passes ();
2136 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2137 if (seen_error ()
2138 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
2140 timevar_pop (TV_CGRAPHOPT);
2141 return;
2144 /* This pass remove bodies of extern inline functions we never inlined.
2145 Do this later so other IPA passes see what is really going on.
2146 FIXME: This should be run just after inlining by pasmanager. */
2147 remove_unreachable_nodes (false, dump_file);
2148 global_info_ready = true;
2149 if (dump_file)
2151 fprintf (dump_file, "Optimized ");
2152 symtab_node:: dump_table (dump_file);
2154 if (post_ipa_mem_report)
2156 fprintf (stderr, "Memory consumption after IPA\n");
2157 dump_memory_report (false);
2159 timevar_pop (TV_CGRAPHOPT);
2161 /* Output everything. */
2162 (*debug_hooks->assembly_start) ();
2163 if (!quiet_flag)
2164 fprintf (stderr, "Assembling functions:\n");
2165 #ifdef ENABLE_CHECKING
2166 symtab_node::verify_symtab_nodes ();
2167 #endif
2169 materialize_all_clones ();
2170 bitmap_obstack_initialize (NULL);
2171 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2172 bitmap_obstack_release (NULL);
2173 mark_functions_to_output ();
2175 /* When weakref support is missing, we autmatically translate all
2176 references to NODE to references to its ultimate alias target.
2177 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2178 TREE_CHAIN.
2180 Set up this mapping before we output any assembler but once we are sure
2181 that all symbol renaming is done.
2183 FIXME: All this uglyness can go away if we just do renaming at gimple
2184 level by physically rewritting the IL. At the moment we can only redirect
2185 calls, so we need infrastructure for renaming references as well. */
2186 #ifndef ASM_OUTPUT_WEAKREF
2187 symtab_node *node;
2189 FOR_EACH_SYMBOL (node)
2190 if (node->alias
2191 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2193 IDENTIFIER_TRANSPARENT_ALIAS
2194 (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2195 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2196 = (node->alias_target ? node->alias_target
2197 : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
2199 #endif
2201 state = EXPANSION;
2203 if (!flag_toplevel_reorder)
2204 output_in_order ();
2205 else
2207 output_asm_statements ();
2209 expand_all_functions ();
2210 output_variables ();
2213 process_new_functions ();
2214 state = FINISHED;
2215 output_weakrefs ();
2217 if (dump_file)
2219 fprintf (dump_file, "\nFinal ");
2220 symtab_node::dump_table (dump_file);
2222 #ifdef ENABLE_CHECKING
2223 symtab_node::verify_symtab_nodes ();
2224 /* Double check that all inline clones are gone and that all
2225 function bodies have been released from memory. */
2226 if (!seen_error ())
2228 cgraph_node *node;
2229 bool error_found = false;
2231 FOR_EACH_DEFINED_FUNCTION (node)
2232 if (node->global.inlined_to
2233 || gimple_has_body_p (node->decl))
2235 error_found = true;
2236 node->debug ();
2238 if (error_found)
2239 internal_error ("nodes with unreleased memory found");
2241 #endif
2245 /* Analyze the whole compilation unit once it is parsed completely. */
2247 void
2248 symbol_table::finalize_compilation_unit (void)
2250 timevar_push (TV_CGRAPH);
2252 /* If we're here there's no current function anymore. Some frontends
2253 are lazy in clearing these. */
2254 current_function_decl = NULL;
2255 set_cfun (NULL);
2257 /* Do not skip analyzing the functions if there were errors, we
2258 miss diagnostics for following functions otherwise. */
2260 /* Emit size functions we didn't inline. */
2261 finalize_size_functions ();
2263 /* Mark alias targets necessary and emit diagnostics. */
2264 handle_alias_pairs ();
2266 if (!quiet_flag)
2268 fprintf (stderr, "\nAnalyzing compilation unit\n");
2269 fflush (stderr);
2272 if (flag_dump_passes)
2273 dump_passes ();
2275 /* Gimplify and lower all functions, compute reachability and
2276 remove unreachable nodes. */
2277 analyze_functions ();
2279 /* Mark alias targets necessary and emit diagnostics. */
2280 handle_alias_pairs ();
2282 /* Gimplify and lower thunks. */
2283 analyze_functions ();
2285 /* Finally drive the pass manager. */
2286 compile ();
2288 timevar_pop (TV_CGRAPH);
2291 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2292 kind of wrapper method. */
2294 void
2295 cgraph_node::create_wrapper (cgraph_node *target)
2297 /* Preserve DECL_RESULT so we get right by reference flag. */
2298 tree decl_result = DECL_RESULT (decl);
2300 /* Remove the function's body. */
2301 release_body ();
2302 reset ();
2304 DECL_RESULT (decl) = decl_result;
2305 DECL_INITIAL (decl) = NULL;
2306 allocate_struct_function (decl, false);
2307 set_cfun (NULL);
2309 /* Turn alias into thunk and expand it into GIMPLE representation. */
2310 definition = true;
2311 thunk.thunk_p = true;
2312 thunk.this_adjusting = false;
2314 cgraph_edge *e = create_edge (target, NULL, 0, CGRAPH_FREQ_BASE);
2316 if (!expand_thunk (false, true))
2317 analyzed = true;
2319 e->call_stmt_cannot_inline_p = true;
2321 /* Inline summary set-up. */
2322 analyze ();
2323 inline_analyze_function (this);
2326 #include "gt-cgraphunit.h"