2014-12-12 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / cgraphunit.c
blob53abd173fa67c1153f230c04287d9581890aa45f
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 "predict.h"
171 #include "vec.h"
172 #include "hashtab.h"
173 #include "hash-set.h"
174 #include "machmode.h"
175 #include "hard-reg-set.h"
176 #include "input.h"
177 #include "function.h"
178 #include "basic-block.h"
179 #include "tree-ssa-alias.h"
180 #include "internal-fn.h"
181 #include "gimple-fold.h"
182 #include "gimple-expr.h"
183 #include "is-a.h"
184 #include "gimple.h"
185 #include "gimplify.h"
186 #include "gimple-iterator.h"
187 #include "gimplify-me.h"
188 #include "gimple-ssa.h"
189 #include "tree-cfg.h"
190 #include "tree-into-ssa.h"
191 #include "tree-ssa.h"
192 #include "tree-inline.h"
193 #include "langhooks.h"
194 #include "toplev.h"
195 #include "flags.h"
196 #include "debug.h"
197 #include "target.h"
198 #include "diagnostic.h"
199 #include "params.h"
200 #include "intl.h"
201 #include "hash-map.h"
202 #include "plugin-api.h"
203 #include "ipa-ref.h"
204 #include "cgraph.h"
205 #include "alloc-pool.h"
206 #include "ipa-prop.h"
207 #include "tree-iterator.h"
208 #include "tree-pass.h"
209 #include "tree-dump.h"
210 #include "gimple-pretty-print.h"
211 #include "output.h"
212 #include "coverage.h"
213 #include "plugin.h"
214 #include "ipa-inline.h"
215 #include "ipa-utils.h"
216 #include "lto-streamer.h"
217 #include "except.h"
218 #include "cfgloop.h"
219 #include "regset.h" /* FIXME: For reg_obstack. */
220 #include "context.h"
221 #include "pass_manager.h"
222 #include "tree-nested.h"
223 #include "gimplify.h"
224 #include "dbgcnt.h"
225 #include "tree-chkp.h"
226 #include "lto-section-names.h"
227 #include "omp-low.h"
229 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
230 secondary queue used during optimization to accommodate passes that
231 may generate new functions that need to be optimized and expanded. */
232 vec<cgraph_node *> cgraph_new_nodes;
234 static void expand_all_functions (void);
235 static void mark_functions_to_output (void);
236 static void handle_alias_pairs (void);
238 /* Used for vtable lookup in thunk adjusting. */
239 static GTY (()) tree vtable_entry_type;
241 /* Determine if symbol declaration is needed. That is, visible to something
242 either outside this translation unit, something magic in the system
243 configury */
244 bool
245 symtab_node::needed_p (void)
247 /* Double check that no one output the function into assembly file
248 early. */
249 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)
250 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
252 if (!definition)
253 return false;
255 if (DECL_EXTERNAL (decl))
256 return false;
258 /* If the user told us it is used, then it must be so. */
259 if (force_output)
260 return true;
262 /* ABI forced symbols are needed when they are external. */
263 if (forced_by_abi && TREE_PUBLIC (decl))
264 return true;
266 /* Keep constructors, destructors and virtual functions. */
267 if (TREE_CODE (decl) == FUNCTION_DECL
268 && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)))
269 return true;
271 /* Externally visible variables must be output. The exception is
272 COMDAT variables that must be output only when they are needed. */
273 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
274 return true;
276 return false;
279 /* Head and terminator of the queue of nodes to be processed while building
280 callgraph. */
282 static symtab_node symtab_terminator;
283 static symtab_node *queued_nodes = &symtab_terminator;
285 /* Add NODE to queue starting at QUEUED_NODES.
286 The queue is linked via AUX pointers and terminated by pointer to 1. */
288 static void
289 enqueue_node (symtab_node *node)
291 if (node->aux)
292 return;
293 gcc_checking_assert (queued_nodes);
294 node->aux = queued_nodes;
295 queued_nodes = node;
298 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
299 functions into callgraph in a way so they look like ordinary reachable
300 functions inserted into callgraph already at construction time. */
302 void
303 symbol_table::process_new_functions (void)
305 tree fndecl;
307 if (!cgraph_new_nodes.exists ())
308 return;
310 handle_alias_pairs ();
311 /* Note that this queue may grow as its being processed, as the new
312 functions may generate new ones. */
313 for (unsigned i = 0; i < cgraph_new_nodes.length (); i++)
315 cgraph_node *node = cgraph_new_nodes[i];
316 fndecl = node->decl;
317 switch (state)
319 case CONSTRUCTION:
320 /* At construction time we just need to finalize function and move
321 it into reachable functions list. */
323 cgraph_node::finalize_function (fndecl, false);
324 call_cgraph_insertion_hooks (node);
325 enqueue_node (node);
326 break;
328 case IPA:
329 case IPA_SSA:
330 case IPA_SSA_AFTER_INLINING:
331 /* When IPA optimization already started, do all essential
332 transformations that has been already performed on the whole
333 cgraph but not on this function. */
335 gimple_register_cfg_hooks ();
336 if (!node->analyzed)
337 node->analyze ();
338 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
339 if ((state == IPA_SSA || state == IPA_SSA_AFTER_INLINING)
340 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
341 g->get_passes ()->execute_early_local_passes ();
342 else if (inline_summary_vec != NULL)
343 compute_inline_parameters (node, true);
344 free_dominance_info (CDI_POST_DOMINATORS);
345 free_dominance_info (CDI_DOMINATORS);
346 pop_cfun ();
347 call_cgraph_insertion_hooks (node);
348 break;
350 case EXPANSION:
351 /* Functions created during expansion shall be compiled
352 directly. */
353 node->process = 0;
354 call_cgraph_insertion_hooks (node);
355 node->expand ();
356 break;
358 default:
359 gcc_unreachable ();
360 break;
364 cgraph_new_nodes.release ();
367 /* As an GCC extension we allow redefinition of the function. The
368 semantics when both copies of bodies differ is not well defined.
369 We replace the old body with new body so in unit at a time mode
370 we always use new body, while in normal mode we may end up with
371 old body inlined into some functions and new body expanded and
372 inlined in others.
374 ??? It may make more sense to use one body for inlining and other
375 body for expanding the function but this is difficult to do. */
377 void
378 cgraph_node::reset (void)
380 /* If process is set, then we have already begun whole-unit analysis.
381 This is *not* testing for whether we've already emitted the function.
382 That case can be sort-of legitimately seen with real function redefinition
383 errors. I would argue that the front end should never present us with
384 such a case, but don't enforce that for now. */
385 gcc_assert (!process);
387 /* Reset our data structures so we can analyze the function again. */
388 memset (&local, 0, sizeof (local));
389 memset (&global, 0, sizeof (global));
390 memset (&rtl, 0, sizeof (rtl));
391 analyzed = false;
392 definition = false;
393 alias = false;
394 weakref = false;
395 cpp_implicit_alias = false;
397 remove_callees ();
398 remove_all_references ();
401 /* Return true when there are references to the node. */
403 bool
404 symtab_node::referred_to_p (void)
406 ipa_ref *ref = NULL;
408 /* See if there are any references at all. */
409 if (iterate_referring (0, ref))
410 return true;
411 /* For functions check also calls. */
412 cgraph_node *cn = dyn_cast <cgraph_node *> (this);
413 if (cn && cn->callers)
414 return true;
415 return false;
418 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
419 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
420 the garbage collector run at the moment. We would need to either create
421 a new GC context, or just not compile right now. */
423 void
424 cgraph_node::finalize_function (tree decl, bool no_collect)
426 cgraph_node *node = cgraph_node::get_create (decl);
428 if (node->definition)
430 /* Nested functions should only be defined once. */
431 gcc_assert (!DECL_CONTEXT (decl)
432 || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL);
433 node->reset ();
434 node->local.redefined_extern_inline = true;
437 notice_global_symbol (decl);
438 node->definition = true;
439 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
441 /* With -fkeep-inline-functions we are keeping all inline functions except
442 for extern inline ones. */
443 if (flag_keep_inline_functions
444 && DECL_DECLARED_INLINE_P (decl)
445 && !DECL_EXTERNAL (decl)
446 && !DECL_DISREGARD_INLINE_LIMITS (decl))
447 node->force_output = 1;
449 /* When not optimizing, also output the static functions. (see
450 PR24561), but don't do so for always_inline functions, functions
451 declared inline and nested functions. These were optimized out
452 in the original implementation and it is unclear whether we want
453 to change the behavior here. */
454 if ((!opt_for_fn (decl, optimize)
455 && !node->cpp_implicit_alias
456 && !DECL_DISREGARD_INLINE_LIMITS (decl)
457 && !DECL_DECLARED_INLINE_P (decl)
458 && !(DECL_CONTEXT (decl)
459 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
460 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
461 node->force_output = 1;
463 /* If we've not yet emitted decl, tell the debug info about it. */
464 if (!TREE_ASM_WRITTEN (decl))
465 (*debug_hooks->deferred_inline_function) (decl);
467 /* Possibly warn about unused parameters. */
468 if (warn_unused_parameter)
469 do_warn_unused_parameter (decl);
471 if (!no_collect)
472 ggc_collect ();
474 if (symtab->state == CONSTRUCTION
475 && (node->needed_p () || node->referred_to_p ()))
476 enqueue_node (node);
479 /* Add the function FNDECL to the call graph.
480 Unlike finalize_function, this function is intended to be used
481 by middle end and allows insertion of new function at arbitrary point
482 of compilation. The function can be either in high, low or SSA form
483 GIMPLE.
485 The function is assumed to be reachable and have address taken (so no
486 API breaking optimizations are performed on it).
488 Main work done by this function is to enqueue the function for later
489 processing to avoid need the passes to be re-entrant. */
491 void
492 cgraph_node::add_new_function (tree fndecl, bool lowered)
494 gcc::pass_manager *passes = g->get_passes ();
495 cgraph_node *node;
496 switch (symtab->state)
498 case PARSING:
499 cgraph_node::finalize_function (fndecl, false);
500 break;
501 case CONSTRUCTION:
502 /* Just enqueue function to be processed at nearest occurrence. */
503 node = cgraph_node::get_create (fndecl);
504 if (lowered)
505 node->lowered = true;
506 cgraph_new_nodes.safe_push (node);
507 break;
509 case IPA:
510 case IPA_SSA:
511 case IPA_SSA_AFTER_INLINING:
512 case EXPANSION:
513 /* Bring the function into finalized state and enqueue for later
514 analyzing and compilation. */
515 node = cgraph_node::get_create (fndecl);
516 node->local.local = false;
517 node->definition = true;
518 node->force_output = true;
519 if (!lowered && symtab->state == EXPANSION)
521 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
522 gimple_register_cfg_hooks ();
523 bitmap_obstack_initialize (NULL);
524 execute_pass_list (cfun, passes->all_lowering_passes);
525 passes->execute_early_local_passes ();
526 bitmap_obstack_release (NULL);
527 pop_cfun ();
529 lowered = true;
531 if (lowered)
532 node->lowered = true;
533 cgraph_new_nodes.safe_push (node);
534 break;
536 case FINISHED:
537 /* At the very end of compilation we have to do all the work up
538 to expansion. */
539 node = cgraph_node::create (fndecl);
540 if (lowered)
541 node->lowered = true;
542 node->definition = true;
543 node->analyze ();
544 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
545 gimple_register_cfg_hooks ();
546 bitmap_obstack_initialize (NULL);
547 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
548 g->get_passes ()->execute_early_local_passes ();
549 bitmap_obstack_release (NULL);
550 pop_cfun ();
551 node->expand ();
552 break;
554 default:
555 gcc_unreachable ();
558 /* Set a personality if required and we already passed EH lowering. */
559 if (lowered
560 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
561 == eh_personality_lang))
562 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
565 /* Analyze the function scheduled to be output. */
566 void
567 cgraph_node::analyze (void)
569 tree decl = this->decl;
570 location_t saved_loc = input_location;
571 input_location = DECL_SOURCE_LOCATION (decl);
573 if (thunk.thunk_p)
575 create_edge (cgraph_node::get (thunk.alias),
576 NULL, 0, CGRAPH_FREQ_BASE);
577 if (!expand_thunk (false, false))
579 thunk.alias = NULL;
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 (symtab_node *node, 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));
675 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
676 node->no_reorder = 1;
679 /* Look for externally_visible and used attributes and mark cgraph nodes
680 accordingly.
682 We cannot mark the nodes at the point the attributes are processed (in
683 handle_*_attribute) because the copy of the declarations available at that
684 point may not be canonical. For example, in:
686 void f();
687 void f() __attribute__((used));
689 the declaration we see in handle_used_attribute will be the second
690 declaration -- but the front end will subsequently merge that declaration
691 with the original declaration and discard the second declaration.
693 Furthermore, we can't mark these nodes in finalize_function because:
695 void f() {}
696 void f() __attribute__((externally_visible));
698 is valid.
700 So, we walk the nodes at the end of the translation unit, applying the
701 attributes at that point. */
703 static void
704 process_function_and_variable_attributes (cgraph_node *first,
705 varpool_node *first_var)
707 cgraph_node *node;
708 varpool_node *vnode;
710 for (node = symtab->first_function (); node != first;
711 node = symtab->next_function (node))
713 tree decl = node->decl;
714 if (DECL_PRESERVE_P (decl))
715 node->mark_force_output ();
716 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
718 if (! TREE_PUBLIC (node->decl))
719 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
720 "%<externally_visible%>"
721 " attribute have effect only on public objects");
723 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
724 && (node->definition && !node->alias))
726 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
727 "%<weakref%> attribute ignored"
728 " because function is defined");
729 DECL_WEAK (decl) = 0;
730 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
731 DECL_ATTRIBUTES (decl));
734 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
735 && !DECL_DECLARED_INLINE_P (decl)
736 /* redefining extern inline function makes it DECL_UNINLINABLE. */
737 && !DECL_UNINLINABLE (decl))
738 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
739 "always_inline function might not be inlinable");
741 process_common_attributes (node, decl);
743 for (vnode = symtab->first_variable (); vnode != first_var;
744 vnode = symtab->next_variable (vnode))
746 tree decl = vnode->decl;
747 if (DECL_EXTERNAL (decl)
748 && DECL_INITIAL (decl))
749 varpool_node::finalize_decl (decl);
750 if (DECL_PRESERVE_P (decl))
751 vnode->force_output = true;
752 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
754 if (! TREE_PUBLIC (vnode->decl))
755 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
756 "%<externally_visible%>"
757 " attribute have effect only on public objects");
759 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
760 && vnode->definition
761 && DECL_INITIAL (decl))
763 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
764 "%<weakref%> attribute ignored"
765 " because variable is initialized");
766 DECL_WEAK (decl) = 0;
767 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
768 DECL_ATTRIBUTES (decl));
770 process_common_attributes (vnode, decl);
774 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
775 middle end to output the variable to asm file, if needed or externally
776 visible. */
778 void
779 varpool_node::finalize_decl (tree decl)
781 varpool_node *node = varpool_node::get_create (decl);
783 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
785 if (node->definition)
786 return;
787 notice_global_symbol (decl);
788 node->definition = true;
789 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
790 /* Traditionally we do not eliminate static variables when not
791 optimizing and when not doing toplevel reoder. */
792 || node->no_reorder
793 || ((!flag_toplevel_reorder
794 && !DECL_COMDAT (node->decl)
795 && !DECL_ARTIFICIAL (node->decl))))
796 node->force_output = true;
798 if (symtab->state == CONSTRUCTION
799 && (node->needed_p () || node->referred_to_p ()))
800 enqueue_node (node);
801 if (symtab->state >= IPA_SSA)
802 node->analyze ();
803 /* Some frontends produce various interface variables after compilation
804 finished. */
805 if (symtab->state == FINISHED
806 || (!flag_toplevel_reorder
807 && symtab->state == EXPANSION))
808 node->assemble_decl ();
810 if (DECL_INITIAL (decl))
811 chkp_register_var_initializer (decl);
814 /* EDGE is an polymorphic call. Mark all possible targets as reachable
815 and if there is only one target, perform trivial devirtualization.
816 REACHABLE_CALL_TARGETS collects target lists we already walked to
817 avoid udplicate work. */
819 static void
820 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
821 cgraph_edge *edge)
823 unsigned int i;
824 void *cache_token;
825 bool final;
826 vec <cgraph_node *>targets
827 = possible_polymorphic_call_targets
828 (edge, &final, &cache_token);
830 if (!reachable_call_targets->add (cache_token))
832 if (symtab->dump_file)
833 dump_possible_polymorphic_call_targets
834 (symtab->dump_file, edge);
836 for (i = 0; i < targets.length (); i++)
838 /* Do not bother to mark virtual methods in anonymous namespace;
839 either we will find use of virtual table defining it, or it is
840 unused. */
841 if (targets[i]->definition
842 && TREE_CODE
843 (TREE_TYPE (targets[i]->decl))
844 == METHOD_TYPE
845 && !type_in_anonymous_namespace_p
846 (method_class_type
847 (TREE_TYPE (targets[i]->decl))))
848 enqueue_node (targets[i]);
852 /* Very trivial devirtualization; when the type is
853 final or anonymous (so we know all its derivation)
854 and there is only one possible virtual call target,
855 make the edge direct. */
856 if (final)
858 if (targets.length () <= 1 && dbg_cnt (devirt))
860 cgraph_node *target;
861 if (targets.length () == 1)
862 target = targets[0];
863 else
864 target = cgraph_node::create
865 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
867 if (symtab->dump_file)
869 fprintf (symtab->dump_file,
870 "Devirtualizing call: ");
871 print_gimple_stmt (symtab->dump_file,
872 edge->call_stmt, 0,
873 TDF_SLIM);
875 if (dump_enabled_p ())
877 location_t locus = gimple_location_safe (edge->call_stmt);
878 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
879 "devirtualizing call in %s to %s\n",
880 edge->caller->name (), target->name ());
883 edge->make_direct (target);
884 edge->redirect_call_stmt_to_callee ();
886 /* Call to __builtin_unreachable shouldn't be instrumented. */
887 if (!targets.length ())
888 gimple_call_set_with_bounds (edge->call_stmt, false);
890 if (symtab->dump_file)
892 fprintf (symtab->dump_file,
893 "Devirtualized as: ");
894 print_gimple_stmt (symtab->dump_file,
895 edge->call_stmt, 0,
896 TDF_SLIM);
903 /* Discover all functions and variables that are trivially needed, analyze
904 them as well as all functions and variables referred by them */
905 static cgraph_node *first_analyzed;
906 static varpool_node *first_analyzed_var;
908 static void
909 analyze_functions (void)
911 /* Keep track of already processed nodes when called multiple times for
912 intermodule optimization. */
913 cgraph_node *first_handled = first_analyzed;
914 varpool_node *first_handled_var = first_analyzed_var;
915 hash_set<void *> reachable_call_targets;
917 symtab_node *node;
918 symtab_node *next;
919 int i;
920 ipa_ref *ref;
921 bool changed = true;
922 location_t saved_loc = input_location;
924 bitmap_obstack_initialize (NULL);
925 symtab->state = CONSTRUCTION;
926 input_location = UNKNOWN_LOCATION;
928 /* Ugly, but the fixup can not happen at a time same body alias is created;
929 C++ FE is confused about the COMDAT groups being right. */
930 if (symtab->cpp_implicit_aliases_done)
931 FOR_EACH_SYMBOL (node)
932 if (node->cpp_implicit_alias)
933 node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
934 build_type_inheritance_graph ();
936 /* Analysis adds static variables that in turn adds references to new functions.
937 So we need to iterate the process until it stabilize. */
938 while (changed)
940 changed = false;
941 process_function_and_variable_attributes (first_analyzed,
942 first_analyzed_var);
944 /* First identify the trivially needed symbols. */
945 for (node = symtab->first_symbol ();
946 node != first_analyzed
947 && node != first_analyzed_var; node = node->next)
949 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
950 node->get_comdat_group_id ();
951 if (node->needed_p ())
953 enqueue_node (node);
954 if (!changed && symtab->dump_file)
955 fprintf (symtab->dump_file, "Trivially needed symbols:");
956 changed = true;
957 if (symtab->dump_file)
958 fprintf (symtab->dump_file, " %s", node->asm_name ());
959 if (!changed && symtab->dump_file)
960 fprintf (symtab->dump_file, "\n");
962 if (node == first_analyzed
963 || node == first_analyzed_var)
964 break;
966 symtab->process_new_functions ();
967 first_analyzed_var = symtab->first_variable ();
968 first_analyzed = symtab->first_function ();
970 if (changed && symtab->dump_file)
971 fprintf (symtab->dump_file, "\n");
973 /* Lower representation, build callgraph edges and references for all trivially
974 needed symbols and all symbols referred by them. */
975 while (queued_nodes != &symtab_terminator)
977 changed = true;
978 node = queued_nodes;
979 queued_nodes = (symtab_node *)queued_nodes->aux;
980 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
981 if (cnode && cnode->definition)
983 cgraph_edge *edge;
984 tree decl = cnode->decl;
986 /* ??? It is possible to create extern inline function
987 and later using weak alias attribute to kill its body.
988 See gcc.c-torture/compile/20011119-1.c */
989 if (!DECL_STRUCT_FUNCTION (decl)
990 && !cnode->alias
991 && !cnode->thunk.thunk_p
992 && !cnode->dispatcher_function)
994 cnode->reset ();
995 cnode->local.redefined_extern_inline = true;
996 continue;
999 if (!cnode->analyzed)
1000 cnode->analyze ();
1002 for (edge = cnode->callees; edge; edge = edge->next_callee)
1003 if (edge->callee->definition)
1004 enqueue_node (edge->callee);
1005 if (opt_for_fn (cnode->decl, optimize)
1006 && opt_for_fn (cnode->decl, flag_devirtualize))
1008 cgraph_edge *next;
1010 for (edge = cnode->indirect_calls; edge; edge = next)
1012 next = edge->next_callee;
1013 if (edge->indirect_info->polymorphic)
1014 walk_polymorphic_call_targets (&reachable_call_targets,
1015 edge);
1019 /* If decl is a clone of an abstract function,
1020 mark that abstract function so that we don't release its body.
1021 The DECL_INITIAL() of that abstract function declaration
1022 will be later needed to output debug info. */
1023 if (DECL_ABSTRACT_ORIGIN (decl))
1025 cgraph_node *origin_node
1026 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl));
1027 origin_node->used_as_abstract_origin = true;
1030 else
1032 varpool_node *vnode = dyn_cast <varpool_node *> (node);
1033 if (vnode && vnode->definition && !vnode->analyzed)
1034 vnode->analyze ();
1037 if (node->same_comdat_group)
1039 symtab_node *next;
1040 for (next = node->same_comdat_group;
1041 next != node;
1042 next = next->same_comdat_group)
1043 enqueue_node (next);
1045 for (i = 0; node->iterate_reference (i, ref); i++)
1046 if (ref->referred->definition)
1047 enqueue_node (ref->referred);
1048 symtab->process_new_functions ();
1051 update_type_inheritance_graph ();
1053 /* Collect entry points to the unit. */
1054 if (symtab->dump_file)
1056 fprintf (symtab->dump_file, "\n\nInitial ");
1057 symtab_node::dump_table (symtab->dump_file);
1060 if (symtab->dump_file)
1061 fprintf (symtab->dump_file, "\nRemoving unused symbols:");
1063 for (node = symtab->first_symbol ();
1064 node != first_handled
1065 && node != first_handled_var; node = next)
1067 next = node->next;
1068 if (!node->aux && !node->referred_to_p ())
1070 if (symtab->dump_file)
1071 fprintf (symtab->dump_file, " %s", node->name ());
1072 node->remove ();
1073 continue;
1075 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1077 tree decl = node->decl;
1079 if (cnode->definition && !gimple_has_body_p (decl)
1080 && !cnode->alias
1081 && !cnode->thunk.thunk_p)
1082 cnode->reset ();
1084 gcc_assert (!cnode->definition || cnode->thunk.thunk_p
1085 || cnode->alias
1086 || gimple_has_body_p (decl));
1087 gcc_assert (cnode->analyzed == cnode->definition);
1089 node->aux = NULL;
1091 for (;node; node = node->next)
1092 node->aux = NULL;
1093 first_analyzed = symtab->first_function ();
1094 first_analyzed_var = symtab->first_variable ();
1095 if (symtab->dump_file)
1097 fprintf (symtab->dump_file, "\n\nReclaimed ");
1098 symtab_node::dump_table (symtab->dump_file);
1100 bitmap_obstack_release (NULL);
1101 ggc_collect ();
1102 /* Initialize assembler name hash, in particular we want to trigger C++
1103 mangling and same body alias creation before we free DECL_ARGUMENTS
1104 used by it. */
1105 if (!seen_error ())
1106 symtab->symtab_initialize_asm_name_hash ();
1108 input_location = saved_loc;
1111 /* Translate the ugly representation of aliases as alias pairs into nice
1112 representation in callgraph. We don't handle all cases yet,
1113 unfortunately. */
1115 static void
1116 handle_alias_pairs (void)
1118 alias_pair *p;
1119 unsigned i;
1121 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1123 symtab_node *target_node = symtab_node::get_for_asmname (p->target);
1125 /* Weakrefs with target not defined in current unit are easy to handle:
1126 they behave just as external variables except we need to note the
1127 alias flag to later output the weakref pseudo op into asm file. */
1128 if (!target_node
1129 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1131 symtab_node *node = symtab_node::get (p->decl);
1132 if (node)
1134 node->alias_target = p->target;
1135 node->weakref = true;
1136 node->alias = true;
1138 alias_pairs->unordered_remove (i);
1139 continue;
1141 else if (!target_node)
1143 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1144 symtab_node *node = symtab_node::get (p->decl);
1145 if (node)
1146 node->alias = false;
1147 alias_pairs->unordered_remove (i);
1148 continue;
1151 if (DECL_EXTERNAL (target_node->decl)
1152 /* We use local aliases for C++ thunks to force the tailcall
1153 to bind locally. This is a hack - to keep it working do
1154 the following (which is not strictly correct). */
1155 && (TREE_CODE (target_node->decl) != FUNCTION_DECL
1156 || ! DECL_VIRTUAL_P (target_node->decl))
1157 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1159 error ("%q+D aliased to external symbol %qE",
1160 p->decl, p->target);
1163 if (TREE_CODE (p->decl) == FUNCTION_DECL
1164 && target_node && is_a <cgraph_node *> (target_node))
1166 cgraph_node *src_node = cgraph_node::get (p->decl);
1167 if (src_node && src_node->definition)
1168 src_node->reset ();
1169 cgraph_node::create_alias (p->decl, target_node->decl);
1170 alias_pairs->unordered_remove (i);
1172 else if (TREE_CODE (p->decl) == VAR_DECL
1173 && target_node && is_a <varpool_node *> (target_node))
1175 varpool_node::create_alias (p->decl, target_node->decl);
1176 alias_pairs->unordered_remove (i);
1178 else
1180 error ("%q+D alias in between function and variable is not supported",
1181 p->decl);
1182 warning (0, "%q+D aliased declaration",
1183 target_node->decl);
1184 alias_pairs->unordered_remove (i);
1187 vec_free (alias_pairs);
1191 /* Figure out what functions we want to assemble. */
1193 static void
1194 mark_functions_to_output (void)
1196 cgraph_node *node;
1197 #ifdef ENABLE_CHECKING
1198 bool check_same_comdat_groups = false;
1200 FOR_EACH_FUNCTION (node)
1201 gcc_assert (!node->process);
1202 #endif
1204 FOR_EACH_FUNCTION (node)
1206 tree decl = node->decl;
1208 gcc_assert (!node->process || node->same_comdat_group);
1209 if (node->process)
1210 continue;
1212 /* We need to output all local functions that are used and not
1213 always inlined, as well as those that are reachable from
1214 outside the current compilation unit. */
1215 if (node->analyzed
1216 && !node->thunk.thunk_p
1217 && !node->alias
1218 && !node->global.inlined_to
1219 && !TREE_ASM_WRITTEN (decl)
1220 && !DECL_EXTERNAL (decl))
1222 node->process = 1;
1223 if (node->same_comdat_group)
1225 cgraph_node *next;
1226 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
1227 next != node;
1228 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
1229 if (!next->thunk.thunk_p && !next->alias
1230 && !next->comdat_local_p ())
1231 next->process = 1;
1234 else if (node->same_comdat_group)
1236 #ifdef ENABLE_CHECKING
1237 check_same_comdat_groups = true;
1238 #endif
1240 else
1242 /* We should've reclaimed all functions that are not needed. */
1243 #ifdef ENABLE_CHECKING
1244 if (!node->global.inlined_to
1245 && gimple_has_body_p (decl)
1246 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1247 are inside partition, we can end up not removing the body since we no longer
1248 have analyzed node pointing to it. */
1249 && !node->in_other_partition
1250 && !node->alias
1251 && !node->clones
1252 && !DECL_EXTERNAL (decl))
1254 node->debug ();
1255 internal_error ("failed to reclaim unneeded function");
1257 #endif
1258 gcc_assert (node->global.inlined_to
1259 || !gimple_has_body_p (decl)
1260 || node->in_other_partition
1261 || node->clones
1262 || DECL_ARTIFICIAL (decl)
1263 || DECL_EXTERNAL (decl));
1268 #ifdef ENABLE_CHECKING
1269 if (check_same_comdat_groups)
1270 FOR_EACH_FUNCTION (node)
1271 if (node->same_comdat_group && !node->process)
1273 tree decl = node->decl;
1274 if (!node->global.inlined_to
1275 && gimple_has_body_p (decl)
1276 /* FIXME: in an ltrans unit when the offline copy is outside a
1277 partition but inline copies are inside a partition, we can
1278 end up not removing the body since we no longer have an
1279 analyzed node pointing to it. */
1280 && !node->in_other_partition
1281 && !node->clones
1282 && !DECL_EXTERNAL (decl))
1284 node->debug ();
1285 internal_error ("failed to reclaim unneeded function in same "
1286 "comdat group");
1289 #endif
1292 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1293 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1295 Set current_function_decl and cfun to newly constructed empty function body.
1296 return basic block in the function body. */
1298 basic_block
1299 init_lowered_empty_function (tree decl, bool in_ssa)
1301 basic_block bb;
1303 current_function_decl = decl;
1304 allocate_struct_function (decl, false);
1305 gimple_register_cfg_hooks ();
1306 init_empty_tree_cfg ();
1308 if (in_ssa)
1310 init_tree_ssa (cfun);
1311 init_ssa_operands (cfun);
1312 cfun->gimple_df->in_ssa_p = true;
1313 cfun->curr_properties |= PROP_ssa;
1316 DECL_INITIAL (decl) = make_node (BLOCK);
1318 DECL_SAVED_TREE (decl) = error_mark_node;
1319 cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1320 | PROP_cfg | PROP_loops);
1322 set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ());
1323 init_loops_structure (cfun, loops_for_fn (cfun), 1);
1324 loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1326 /* Create BB for body of the function and connect it properly. */
1327 bb = create_basic_block (NULL, (void *) 0, ENTRY_BLOCK_PTR_FOR_FN (cfun));
1328 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU);
1329 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1330 add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
1332 return bb;
1335 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1336 offset indicated by VIRTUAL_OFFSET, if that is
1337 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1338 zero for a result adjusting thunk. */
1340 static tree
1341 thunk_adjust (gimple_stmt_iterator * bsi,
1342 tree ptr, bool this_adjusting,
1343 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1345 gassign *stmt;
1346 tree ret;
1348 if (this_adjusting
1349 && fixed_offset != 0)
1351 stmt = gimple_build_assign
1352 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1353 ptr,
1354 fixed_offset));
1355 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1358 /* If there's a virtual offset, look up that value in the vtable and
1359 adjust the pointer again. */
1360 if (virtual_offset)
1362 tree vtabletmp;
1363 tree vtabletmp2;
1364 tree vtabletmp3;
1366 if (!vtable_entry_type)
1368 tree vfunc_type = make_node (FUNCTION_TYPE);
1369 TREE_TYPE (vfunc_type) = integer_type_node;
1370 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1371 layout_type (vfunc_type);
1373 vtable_entry_type = build_pointer_type (vfunc_type);
1376 vtabletmp =
1377 create_tmp_reg (build_pointer_type
1378 (build_pointer_type (vtable_entry_type)), "vptr");
1380 /* The vptr is always at offset zero in the object. */
1381 stmt = gimple_build_assign (vtabletmp,
1382 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1383 ptr));
1384 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1386 /* Form the vtable address. */
1387 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
1388 "vtableaddr");
1389 stmt = gimple_build_assign (vtabletmp2,
1390 build_simple_mem_ref (vtabletmp));
1391 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1393 /* Find the entry with the vcall offset. */
1394 stmt = gimple_build_assign (vtabletmp2,
1395 fold_build_pointer_plus_loc (input_location,
1396 vtabletmp2,
1397 virtual_offset));
1398 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1400 /* Get the offset itself. */
1401 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1402 "vcalloffset");
1403 stmt = gimple_build_assign (vtabletmp3,
1404 build_simple_mem_ref (vtabletmp2));
1405 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1407 /* Adjust the `this' pointer. */
1408 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1409 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1410 GSI_CONTINUE_LINKING);
1413 if (!this_adjusting
1414 && fixed_offset != 0)
1415 /* Adjust the pointer by the constant. */
1417 tree ptrtmp;
1419 if (TREE_CODE (ptr) == VAR_DECL)
1420 ptrtmp = ptr;
1421 else
1423 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
1424 stmt = gimple_build_assign (ptrtmp, ptr);
1425 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1427 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1428 ptrtmp, fixed_offset);
1431 /* Emit the statement and gimplify the adjustment expression. */
1432 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
1433 stmt = gimple_build_assign (ret, ptr);
1434 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1436 return ret;
1439 /* Expand thunk NODE to gimple if possible.
1440 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1441 no assembler is produced.
1442 When OUTPUT_ASM_THUNK is true, also produce assembler for
1443 thunks that are not lowered. */
1445 bool
1446 cgraph_node::expand_thunk (bool output_asm_thunks, bool force_gimple_thunk)
1448 bool this_adjusting = thunk.this_adjusting;
1449 HOST_WIDE_INT fixed_offset = thunk.fixed_offset;
1450 HOST_WIDE_INT virtual_value = thunk.virtual_value;
1451 tree virtual_offset = NULL;
1452 tree alias = callees->callee->decl;
1453 tree thunk_fndecl = decl;
1454 tree a;
1457 if (!force_gimple_thunk && this_adjusting
1458 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1459 virtual_value, alias))
1461 const char *fnname;
1462 tree fn_block;
1463 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1465 if (!output_asm_thunks)
1467 analyzed = true;
1468 return false;
1471 if (in_lto_p)
1472 get_untransformed_body ();
1473 a = DECL_ARGUMENTS (thunk_fndecl);
1475 current_function_decl = thunk_fndecl;
1477 /* Ensure thunks are emitted in their correct sections. */
1478 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1480 DECL_RESULT (thunk_fndecl)
1481 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1482 RESULT_DECL, 0, restype);
1483 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1484 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
1486 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1487 create one. */
1488 fn_block = make_node (BLOCK);
1489 BLOCK_VARS (fn_block) = a;
1490 DECL_INITIAL (thunk_fndecl) = fn_block;
1491 init_function_start (thunk_fndecl);
1492 cfun->is_thunk = 1;
1493 insn_locations_init ();
1494 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1495 prologue_location = curr_insn_location ();
1496 assemble_start_function (thunk_fndecl, fnname);
1498 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1499 fixed_offset, virtual_value, alias);
1501 assemble_end_function (thunk_fndecl, fnname);
1502 insn_locations_finalize ();
1503 init_insn_lengths ();
1504 free_after_compilation (cfun);
1505 set_cfun (NULL);
1506 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1507 thunk.thunk_p = false;
1508 analyzed = false;
1510 else
1512 tree restype;
1513 basic_block bb, then_bb, else_bb, return_bb;
1514 gimple_stmt_iterator bsi;
1515 int nargs = 0;
1516 tree arg;
1517 int i;
1518 tree resdecl;
1519 tree restmp = NULL;
1521 gcall *call;
1522 greturn *ret;
1524 if (in_lto_p)
1525 get_untransformed_body ();
1526 a = DECL_ARGUMENTS (thunk_fndecl);
1528 current_function_decl = thunk_fndecl;
1530 /* Ensure thunks are emitted in their correct sections. */
1531 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1533 DECL_IGNORED_P (thunk_fndecl) = 1;
1534 bitmap_obstack_initialize (NULL);
1536 if (thunk.virtual_offset_p)
1537 virtual_offset = size_int (virtual_value);
1539 /* Build the return declaration for the function. */
1540 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1541 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1543 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1544 DECL_ARTIFICIAL (resdecl) = 1;
1545 DECL_IGNORED_P (resdecl) = 1;
1546 DECL_RESULT (thunk_fndecl) = resdecl;
1547 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1549 else
1550 resdecl = DECL_RESULT (thunk_fndecl);
1552 bb = then_bb = else_bb = return_bb = init_lowered_empty_function (thunk_fndecl, true);
1554 bsi = gsi_start_bb (bb);
1556 /* Build call to the function being thunked. */
1557 if (!VOID_TYPE_P (restype))
1559 if (DECL_BY_REFERENCE (resdecl))
1561 restmp = gimple_fold_indirect_ref (resdecl);
1562 if (!restmp)
1563 restmp = build2 (MEM_REF,
1564 TREE_TYPE (TREE_TYPE (DECL_RESULT (alias))),
1565 resdecl,
1566 build_int_cst (TREE_TYPE
1567 (DECL_RESULT (alias)), 0));
1569 else if (!is_gimple_reg_type (restype))
1571 restmp = resdecl;
1573 if (TREE_CODE (restmp) == VAR_DECL)
1574 add_local_decl (cfun, restmp);
1575 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1577 else
1578 restmp = create_tmp_reg (restype, "retval");
1581 for (arg = a; arg; arg = DECL_CHAIN (arg))
1582 nargs++;
1583 auto_vec<tree> vargs (nargs);
1584 if (this_adjusting)
1585 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
1586 virtual_offset));
1587 else if (nargs)
1588 vargs.quick_push (a);
1590 if (nargs)
1591 for (i = 1, arg = DECL_CHAIN (a); i < nargs; i++, arg = DECL_CHAIN (arg))
1593 tree tmp = arg;
1594 if (!is_gimple_val (arg))
1596 tmp = create_tmp_reg (TYPE_MAIN_VARIANT
1597 (TREE_TYPE (arg)), "arg");
1598 gimple stmt = gimple_build_assign (tmp, arg);
1599 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1601 vargs.quick_push (tmp);
1603 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1604 callees->call_stmt = call;
1605 gimple_call_set_from_thunk (call, true);
1606 gimple_call_set_with_bounds (call, instrumentation_clone);
1607 if (restmp)
1609 gimple_call_set_lhs (call, restmp);
1610 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp),
1611 TREE_TYPE (TREE_TYPE (alias))));
1613 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
1614 if (!(gimple_call_flags (call) & ECF_NORETURN))
1616 if (restmp && !this_adjusting
1617 && (fixed_offset || virtual_offset))
1619 tree true_label = NULL_TREE;
1621 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1623 gimple stmt;
1624 /* If the return type is a pointer, we need to
1625 protect against NULL. We know there will be an
1626 adjustment, because that's why we're emitting a
1627 thunk. */
1628 then_bb = create_basic_block (NULL, (void *) 0, bb);
1629 return_bb = create_basic_block (NULL, (void *) 0, then_bb);
1630 else_bb = create_basic_block (NULL, (void *) 0, else_bb);
1631 add_bb_to_loop (then_bb, bb->loop_father);
1632 add_bb_to_loop (return_bb, bb->loop_father);
1633 add_bb_to_loop (else_bb, bb->loop_father);
1634 remove_edge (single_succ_edge (bb));
1635 true_label = gimple_block_label (then_bb);
1636 stmt = gimple_build_cond (NE_EXPR, restmp,
1637 build_zero_cst (TREE_TYPE (restmp)),
1638 NULL_TREE, NULL_TREE);
1639 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1640 make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1641 make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1642 make_edge (return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1643 make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1644 make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1645 bsi = gsi_last_bb (then_bb);
1648 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1649 fixed_offset, virtual_offset);
1650 if (true_label)
1652 gimple stmt;
1653 bsi = gsi_last_bb (else_bb);
1654 stmt = gimple_build_assign (restmp,
1655 build_zero_cst (TREE_TYPE (restmp)));
1656 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1657 bsi = gsi_last_bb (return_bb);
1660 else
1661 gimple_call_set_tail (call, true);
1663 /* Build return value. */
1664 if (!DECL_BY_REFERENCE (resdecl))
1665 ret = gimple_build_return (restmp);
1666 else
1667 ret = gimple_build_return (resdecl);
1669 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1671 else
1673 gimple_call_set_tail (call, true);
1674 remove_edge (single_succ_edge (bb));
1677 cfun->gimple_df->in_ssa_p = true;
1678 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
1679 TREE_ASM_WRITTEN (thunk_fndecl) = false;
1680 delete_unreachable_blocks ();
1681 update_ssa (TODO_update_ssa);
1682 #ifdef ENABLE_CHECKING
1683 verify_flow_info ();
1684 #endif
1685 free_dominance_info (CDI_DOMINATORS);
1687 /* Since we want to emit the thunk, we explicitly mark its name as
1688 referenced. */
1689 thunk.thunk_p = false;
1690 lowered = true;
1691 bitmap_obstack_release (NULL);
1693 current_function_decl = NULL;
1694 set_cfun (NULL);
1695 return true;
1698 /* Assemble thunks and aliases associated to node. */
1700 void
1701 cgraph_node::assemble_thunks_and_aliases (void)
1703 cgraph_edge *e;
1704 ipa_ref *ref;
1706 for (e = callers; e;)
1707 if (e->caller->thunk.thunk_p
1708 && !e->caller->thunk.add_pointer_bounds_args)
1710 cgraph_node *thunk = e->caller;
1712 e = e->next_caller;
1713 thunk->expand_thunk (true, false);
1714 thunk->assemble_thunks_and_aliases ();
1716 else
1717 e = e->next_caller;
1719 FOR_EACH_ALIAS (this, ref)
1721 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1722 bool saved_written = TREE_ASM_WRITTEN (decl);
1724 /* Force assemble_alias to really output the alias this time instead
1725 of buffering it in same alias pairs. */
1726 TREE_ASM_WRITTEN (decl) = 1;
1727 do_assemble_alias (alias->decl,
1728 DECL_ASSEMBLER_NAME (decl));
1729 alias->assemble_thunks_and_aliases ();
1730 TREE_ASM_WRITTEN (decl) = saved_written;
1734 /* Expand function specified by node. */
1736 void
1737 cgraph_node::expand (void)
1739 location_t saved_loc;
1741 /* We ought to not compile any inline clones. */
1742 gcc_assert (!global.inlined_to);
1744 announce_function (decl);
1745 process = 0;
1746 gcc_assert (lowered);
1747 get_untransformed_body ();
1749 /* Generate RTL for the body of DECL. */
1751 timevar_push (TV_REST_OF_COMPILATION);
1753 gcc_assert (symtab->global_info_ready);
1755 /* Initialize the default bitmap obstack. */
1756 bitmap_obstack_initialize (NULL);
1758 /* Initialize the RTL code for the function. */
1759 current_function_decl = decl;
1760 saved_loc = input_location;
1761 input_location = DECL_SOURCE_LOCATION (decl);
1762 init_function_start (decl);
1764 gimple_register_cfg_hooks ();
1766 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1768 execute_all_ipa_transforms ();
1770 /* Perform all tree transforms and optimizations. */
1772 /* Signal the start of passes. */
1773 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1775 execute_pass_list (cfun, g->get_passes ()->all_passes);
1777 /* Signal the end of passes. */
1778 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1780 bitmap_obstack_release (&reg_obstack);
1782 /* Release the default bitmap obstack. */
1783 bitmap_obstack_release (NULL);
1785 /* If requested, warn about function definitions where the function will
1786 return a value (usually of some struct or union type) which itself will
1787 take up a lot of stack space. */
1788 if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl))
1790 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
1792 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1793 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1794 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1795 larger_than_size))
1797 unsigned int size_as_int
1798 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1800 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1801 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
1802 decl, size_as_int);
1803 else
1804 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
1805 decl, larger_than_size);
1809 gimple_set_body (decl, NULL);
1810 if (DECL_STRUCT_FUNCTION (decl) == 0
1811 && !cgraph_node::get (decl)->origin)
1813 /* Stop pointing to the local nodes about to be freed.
1814 But DECL_INITIAL must remain nonzero so we know this
1815 was an actual function definition.
1816 For a nested function, this is done in c_pop_function_context.
1817 If rest_of_compilation set this to 0, leave it 0. */
1818 if (DECL_INITIAL (decl) != 0)
1819 DECL_INITIAL (decl) = error_mark_node;
1822 input_location = saved_loc;
1824 ggc_collect ();
1825 timevar_pop (TV_REST_OF_COMPILATION);
1827 /* Make sure that BE didn't give up on compiling. */
1828 gcc_assert (TREE_ASM_WRITTEN (decl));
1829 set_cfun (NULL);
1830 current_function_decl = NULL;
1832 /* It would make a lot more sense to output thunks before function body to get more
1833 forward and lest backwarding jumps. This however would need solving problem
1834 with comdats. See PR48668. Also aliases must come after function itself to
1835 make one pass assemblers, like one on AIX, happy. See PR 50689.
1836 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1837 groups. */
1838 assemble_thunks_and_aliases ();
1839 release_body ();
1840 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1841 points to the dead function body. */
1842 remove_callees ();
1843 remove_all_references ();
1846 /* Node comparer that is responsible for the order that corresponds
1847 to time when a function was launched for the first time. */
1849 static int
1850 node_cmp (const void *pa, const void *pb)
1852 const cgraph_node *a = *(const cgraph_node * const *) pa;
1853 const cgraph_node *b = *(const cgraph_node * const *) pb;
1855 /* Functions with time profile must be before these without profile. */
1856 if (!a->tp_first_run || !b->tp_first_run)
1857 return a->tp_first_run - b->tp_first_run;
1859 return a->tp_first_run != b->tp_first_run
1860 ? b->tp_first_run - a->tp_first_run
1861 : b->order - a->order;
1864 /* Expand all functions that must be output.
1866 Attempt to topologically sort the nodes so function is output when
1867 all called functions are already assembled to allow data to be
1868 propagated across the callgraph. Use a stack to get smaller distance
1869 between a function and its callees (later we may choose to use a more
1870 sophisticated algorithm for function reordering; we will likely want
1871 to use subsections to make the output functions appear in top-down
1872 order). */
1874 static void
1875 expand_all_functions (void)
1877 cgraph_node *node;
1878 cgraph_node **order = XCNEWVEC (cgraph_node *,
1879 symtab->cgraph_count);
1880 unsigned int expanded_func_count = 0, profiled_func_count = 0;
1881 int order_pos, new_order_pos = 0;
1882 int i;
1884 order_pos = ipa_reverse_postorder (order);
1885 gcc_assert (order_pos == symtab->cgraph_count);
1887 /* Garbage collector may remove inline clones we eliminate during
1888 optimization. So we must be sure to not reference them. */
1889 for (i = 0; i < order_pos; i++)
1890 if (order[i]->process)
1891 order[new_order_pos++] = order[i];
1893 if (flag_profile_reorder_functions)
1894 qsort (order, new_order_pos, sizeof (cgraph_node *), node_cmp);
1896 for (i = new_order_pos - 1; i >= 0; i--)
1898 node = order[i];
1900 if (node->process)
1902 expanded_func_count++;
1903 if(node->tp_first_run)
1904 profiled_func_count++;
1906 if (symtab->dump_file)
1907 fprintf (symtab->dump_file,
1908 "Time profile order in expand_all_functions:%s:%d\n",
1909 node->asm_name (), node->tp_first_run);
1910 node->process = 0;
1911 node->expand ();
1915 if (dump_file)
1916 fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
1917 main_input_filename, profiled_func_count, expanded_func_count);
1919 if (symtab->dump_file && flag_profile_reorder_functions)
1920 fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n",
1921 profiled_func_count, expanded_func_count);
1923 symtab->process_new_functions ();
1924 free_gimplify_stack ();
1926 free (order);
1929 /* This is used to sort the node types by the cgraph order number. */
1931 enum cgraph_order_sort_kind
1933 ORDER_UNDEFINED = 0,
1934 ORDER_FUNCTION,
1935 ORDER_VAR,
1936 ORDER_ASM
1939 struct cgraph_order_sort
1941 enum cgraph_order_sort_kind kind;
1942 union
1944 cgraph_node *f;
1945 varpool_node *v;
1946 asm_node *a;
1947 } u;
1950 /* Output all functions, variables, and asm statements in the order
1951 according to their order fields, which is the order in which they
1952 appeared in the file. This implements -fno-toplevel-reorder. In
1953 this mode we may output functions and variables which don't really
1954 need to be output.
1955 When NO_REORDER is true only do this for symbols marked no reorder. */
1957 static void
1958 output_in_order (bool no_reorder)
1960 int max;
1961 cgraph_order_sort *nodes;
1962 int i;
1963 cgraph_node *pf;
1964 varpool_node *pv;
1965 asm_node *pa;
1966 max = symtab->order;
1967 nodes = XCNEWVEC (cgraph_order_sort, max);
1969 FOR_EACH_DEFINED_FUNCTION (pf)
1971 if (pf->process && !pf->thunk.thunk_p && !pf->alias)
1973 if (no_reorder && !pf->no_reorder)
1974 continue;
1975 i = pf->order;
1976 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1977 nodes[i].kind = ORDER_FUNCTION;
1978 nodes[i].u.f = pf;
1982 FOR_EACH_DEFINED_VARIABLE (pv)
1983 if (!DECL_EXTERNAL (pv->decl))
1985 if (no_reorder && !pv->no_reorder)
1986 continue;
1987 i = pv->order;
1988 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1989 nodes[i].kind = ORDER_VAR;
1990 nodes[i].u.v = pv;
1993 for (pa = symtab->first_asm_symbol (); pa; pa = pa->next)
1995 i = pa->order;
1996 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1997 nodes[i].kind = ORDER_ASM;
1998 nodes[i].u.a = pa;
2001 /* In toplevel reorder mode we output all statics; mark them as needed. */
2003 for (i = 0; i < max; ++i)
2004 if (nodes[i].kind == ORDER_VAR)
2005 nodes[i].u.v->finalize_named_section_flags ();
2007 for (i = 0; i < max; ++i)
2009 switch (nodes[i].kind)
2011 case ORDER_FUNCTION:
2012 nodes[i].u.f->process = 0;
2013 nodes[i].u.f->expand ();
2014 break;
2016 case ORDER_VAR:
2017 nodes[i].u.v->assemble_decl ();
2018 break;
2020 case ORDER_ASM:
2021 assemble_asm (nodes[i].u.a->asm_str);
2022 break;
2024 case ORDER_UNDEFINED:
2025 break;
2027 default:
2028 gcc_unreachable ();
2032 symtab->clear_asm_symbols ();
2034 free (nodes);
2037 static void
2038 ipa_passes (void)
2040 gcc::pass_manager *passes = g->get_passes ();
2042 set_cfun (NULL);
2043 current_function_decl = NULL;
2044 gimple_register_cfg_hooks ();
2045 bitmap_obstack_initialize (NULL);
2047 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2049 if (!in_lto_p)
2051 execute_ipa_pass_list (passes->all_small_ipa_passes);
2052 if (seen_error ())
2053 return;
2056 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2057 devirtualization and other changes where removal iterate. */
2058 symtab->remove_unreachable_nodes (symtab->dump_file);
2060 /* If pass_all_early_optimizations was not scheduled, the state of
2061 the cgraph will not be properly updated. Update it now. */
2062 if (symtab->state < IPA_SSA)
2063 symtab->state = IPA_SSA;
2065 if (!in_lto_p)
2067 /* Generate coverage variables and constructors. */
2068 coverage_finish ();
2070 /* Process new functions added. */
2071 set_cfun (NULL);
2072 current_function_decl = NULL;
2073 symtab->process_new_functions ();
2075 execute_ipa_summary_passes
2076 ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2079 /* Some targets need to handle LTO assembler output specially. */
2080 if (flag_generate_lto || flag_generate_offload)
2081 targetm.asm_out.lto_start ();
2083 if (!in_lto_p)
2085 if (g->have_offload)
2087 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2088 ipa_write_summaries (true);
2090 if (flag_lto)
2092 section_name_prefix = LTO_SECTION_NAME_PREFIX;
2093 ipa_write_summaries (false);
2097 if (flag_generate_lto || flag_generate_offload)
2098 targetm.asm_out.lto_end ();
2100 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
2101 execute_ipa_pass_list (passes->all_regular_ipa_passes);
2102 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2104 bitmap_obstack_release (NULL);
2108 /* Return string alias is alias of. */
2110 static tree
2111 get_alias_symbol (tree decl)
2113 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
2114 return get_identifier (TREE_STRING_POINTER
2115 (TREE_VALUE (TREE_VALUE (alias))));
2119 /* Weakrefs may be associated to external decls and thus not output
2120 at expansion time. Emit all necessary aliases. */
2122 void
2123 symbol_table::output_weakrefs (void)
2125 symtab_node *node;
2126 cgraph_node *cnode;
2127 FOR_EACH_SYMBOL (node)
2128 if (node->alias
2129 && !TREE_ASM_WRITTEN (node->decl)
2130 && (!(cnode = dyn_cast <cgraph_node *> (node))
2131 || !cnode->instrumented_version
2132 || !TREE_ASM_WRITTEN (cnode->instrumented_version->decl))
2133 && node->weakref)
2135 tree target;
2137 /* Weakrefs are special by not requiring target definition in current
2138 compilation unit. It is thus bit hard to work out what we want to
2139 alias.
2140 When alias target is defined, we need to fetch it from symtab reference,
2141 otherwise it is pointed to by alias_target. */
2142 if (node->alias_target)
2143 target = (DECL_P (node->alias_target)
2144 ? DECL_ASSEMBLER_NAME (node->alias_target)
2145 : node->alias_target);
2146 else if (node->analyzed)
2147 target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
2148 else
2150 gcc_unreachable ();
2151 target = get_alias_symbol (node->decl);
2153 do_assemble_alias (node->decl, target);
2157 /* Perform simple optimizations based on callgraph. */
2159 void
2160 symbol_table::compile (void)
2162 if (seen_error ())
2163 return;
2165 #ifdef ENABLE_CHECKING
2166 symtab_node::verify_symtab_nodes ();
2167 #endif
2169 timevar_push (TV_CGRAPHOPT);
2170 if (pre_ipa_mem_report)
2172 fprintf (stderr, "Memory consumption before IPA\n");
2173 dump_memory_report (false);
2175 if (!quiet_flag)
2176 fprintf (stderr, "Performing interprocedural optimizations\n");
2177 state = IPA;
2179 /* Offloading requires LTO infrastructure. */
2180 if (!in_lto_p && g->have_offload)
2181 flag_generate_offload = 1;
2183 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2184 if (flag_generate_lto || flag_generate_offload)
2185 lto_streamer_hooks_init ();
2187 /* Don't run the IPA passes if there was any error or sorry messages. */
2188 if (!seen_error ())
2189 ipa_passes ();
2191 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2192 if (seen_error ()
2193 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
2195 timevar_pop (TV_CGRAPHOPT);
2196 return;
2199 global_info_ready = true;
2200 if (dump_file)
2202 fprintf (dump_file, "Optimized ");
2203 symtab_node:: dump_table (dump_file);
2205 if (post_ipa_mem_report)
2207 fprintf (stderr, "Memory consumption after IPA\n");
2208 dump_memory_report (false);
2210 timevar_pop (TV_CGRAPHOPT);
2212 /* Output everything. */
2213 (*debug_hooks->assembly_start) ();
2214 if (!quiet_flag)
2215 fprintf (stderr, "Assembling functions:\n");
2216 #ifdef ENABLE_CHECKING
2217 symtab_node::verify_symtab_nodes ();
2218 #endif
2220 materialize_all_clones ();
2221 bitmap_obstack_initialize (NULL);
2222 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2223 bitmap_obstack_release (NULL);
2224 mark_functions_to_output ();
2226 /* When weakref support is missing, we autmatically translate all
2227 references to NODE to references to its ultimate alias target.
2228 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2229 TREE_CHAIN.
2231 Set up this mapping before we output any assembler but once we are sure
2232 that all symbol renaming is done.
2234 FIXME: All this uglyness can go away if we just do renaming at gimple
2235 level by physically rewritting the IL. At the moment we can only redirect
2236 calls, so we need infrastructure for renaming references as well. */
2237 #ifndef ASM_OUTPUT_WEAKREF
2238 symtab_node *node;
2240 FOR_EACH_SYMBOL (node)
2241 if (node->alias
2242 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2244 IDENTIFIER_TRANSPARENT_ALIAS
2245 (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2246 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2247 = (node->alias_target ? node->alias_target
2248 : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
2250 #endif
2252 state = EXPANSION;
2254 if (!flag_toplevel_reorder)
2255 output_in_order (false);
2256 else
2258 /* Output first asm statements and anything ordered. The process
2259 flag is cleared for these nodes, so we skip them later. */
2260 output_in_order (true);
2261 expand_all_functions ();
2262 output_variables ();
2265 process_new_functions ();
2266 state = FINISHED;
2267 output_weakrefs ();
2269 if (dump_file)
2271 fprintf (dump_file, "\nFinal ");
2272 symtab_node::dump_table (dump_file);
2274 #ifdef ENABLE_CHECKING
2275 symtab_node::verify_symtab_nodes ();
2276 /* Double check that all inline clones are gone and that all
2277 function bodies have been released from memory. */
2278 if (!seen_error ())
2280 cgraph_node *node;
2281 bool error_found = false;
2283 FOR_EACH_DEFINED_FUNCTION (node)
2284 if (node->global.inlined_to
2285 || gimple_has_body_p (node->decl))
2287 error_found = true;
2288 node->debug ();
2290 if (error_found)
2291 internal_error ("nodes with unreleased memory found");
2293 #endif
2297 /* Analyze the whole compilation unit once it is parsed completely. */
2299 void
2300 symbol_table::finalize_compilation_unit (void)
2302 timevar_push (TV_CGRAPH);
2304 /* If we're here there's no current function anymore. Some frontends
2305 are lazy in clearing these. */
2306 current_function_decl = NULL;
2307 set_cfun (NULL);
2309 /* Do not skip analyzing the functions if there were errors, we
2310 miss diagnostics for following functions otherwise. */
2312 /* Emit size functions we didn't inline. */
2313 finalize_size_functions ();
2315 /* Mark alias targets necessary and emit diagnostics. */
2316 handle_alias_pairs ();
2318 if (!quiet_flag)
2320 fprintf (stderr, "\nAnalyzing compilation unit\n");
2321 fflush (stderr);
2324 if (flag_dump_passes)
2325 dump_passes ();
2327 /* Gimplify and lower all functions, compute reachability and
2328 remove unreachable nodes. */
2329 analyze_functions ();
2331 /* Mark alias targets necessary and emit diagnostics. */
2332 handle_alias_pairs ();
2334 /* Gimplify and lower thunks. */
2335 analyze_functions ();
2337 /* Finally drive the pass manager. */
2338 compile ();
2340 timevar_pop (TV_CGRAPH);
2343 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2344 within the same process. For use by toplev::finalize. */
2346 void
2347 cgraphunit_c_finalize (void)
2349 gcc_assert (cgraph_new_nodes.length () == 0);
2350 cgraph_new_nodes.truncate (0);
2352 vtable_entry_type = NULL;
2353 queued_nodes = &symtab_terminator;
2355 first_analyzed = NULL;
2356 first_analyzed_var = NULL;
2359 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2360 kind of wrapper method. */
2362 void
2363 cgraph_node::create_wrapper (cgraph_node *target)
2365 /* Preserve DECL_RESULT so we get right by reference flag. */
2366 tree decl_result = DECL_RESULT (decl);
2368 /* Remove the function's body but keep arguments to be reused
2369 for thunk. */
2370 release_body (true);
2371 reset ();
2373 DECL_RESULT (decl) = decl_result;
2374 DECL_INITIAL (decl) = NULL;
2375 allocate_struct_function (decl, false);
2376 set_cfun (NULL);
2378 /* Turn alias into thunk and expand it into GIMPLE representation. */
2379 definition = true;
2380 thunk.thunk_p = true;
2381 thunk.this_adjusting = false;
2383 cgraph_edge *e = create_edge (target, NULL, 0, CGRAPH_FREQ_BASE);
2385 tree arguments = DECL_ARGUMENTS (decl);
2387 while (arguments)
2389 TREE_ADDRESSABLE (arguments) = false;
2390 arguments = TREE_CHAIN (arguments);
2393 expand_thunk (false, true);
2394 e->call_stmt_cannot_inline_p = true;
2396 /* Inline summary set-up. */
2397 analyze ();
2398 inline_analyze_function (this);
2401 #include "gt-cgraphunit.h"