2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / cgraphunit.c
blob3aadf28dd314690d54aaf777fc5adbd51a8d178c
1 /* Driver of optimization process
2 Copyright (C) 2003-2015 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 "input.h"
165 #include "alias.h"
166 #include "symtab.h"
167 #include "tree.h"
168 #include "fold-const.h"
169 #include "varasm.h"
170 #include "stor-layout.h"
171 #include "stringpool.h"
172 #include "output.h"
173 #include "rtl.h"
174 #include "predict.h"
175 #include "hard-reg-set.h"
176 #include "input.h"
177 #include "function.h"
178 #include "basic-block.h"
179 #include "dominance.h"
180 #include "cfgcleanup.h"
181 #include "cfg.h"
182 #include "tree-ssa-alias.h"
183 #include "internal-fn.h"
184 #include "gimple-fold.h"
185 #include "gimple-expr.h"
186 #include "is-a.h"
187 #include "gimple.h"
188 #include "gimplify.h"
189 #include "gimple-iterator.h"
190 #include "gimplify-me.h"
191 #include "gimple-ssa.h"
192 #include "tree-cfg.h"
193 #include "tree-into-ssa.h"
194 #include "tree-ssa.h"
195 #include "tree-inline.h"
196 #include "langhooks.h"
197 #include "toplev.h"
198 #include "flags.h"
199 #include "debug.h"
200 #include "target.h"
201 #include "diagnostic.h"
202 #include "params.h"
203 #include "intl.h"
204 #include "plugin-api.h"
205 #include "ipa-ref.h"
206 #include "cgraph.h"
207 #include "alloc-pool.h"
208 #include "symbol-summary.h"
209 #include "ipa-prop.h"
210 #include "tree-iterator.h"
211 #include "tree-pass.h"
212 #include "tree-dump.h"
213 #include "gimple-pretty-print.h"
214 #include "output.h"
215 #include "coverage.h"
216 #include "plugin.h"
217 #include "ipa-inline.h"
218 #include "ipa-utils.h"
219 #include "lto-streamer.h"
220 #include "except.h"
221 #include "cfgloop.h"
222 #include "regset.h" /* FIXME: For reg_obstack. */
223 #include "context.h"
224 #include "pass_manager.h"
225 #include "tree-nested.h"
226 #include "gimplify.h"
227 #include "dbgcnt.h"
228 #include "tree-chkp.h"
229 #include "lto-section-names.h"
230 #include "omp-low.h"
231 #include "print-tree.h"
233 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
234 secondary queue used during optimization to accommodate passes that
235 may generate new functions that need to be optimized and expanded. */
236 vec<cgraph_node *> cgraph_new_nodes;
238 static void expand_all_functions (void);
239 static void mark_functions_to_output (void);
240 static void handle_alias_pairs (void);
242 /* Used for vtable lookup in thunk adjusting. */
243 static GTY (()) tree vtable_entry_type;
245 /* Determine if symbol declaration is needed. That is, visible to something
246 either outside this translation unit, something magic in the system
247 configury */
248 bool
249 symtab_node::needed_p (void)
251 /* Double check that no one output the function into assembly file
252 early. */
253 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)
254 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
256 if (!definition)
257 return false;
259 if (DECL_EXTERNAL (decl))
260 return false;
262 /* If the user told us it is used, then it must be so. */
263 if (force_output)
264 return true;
266 /* ABI forced symbols are needed when they are external. */
267 if (forced_by_abi && TREE_PUBLIC (decl))
268 return true;
270 /* Keep constructors, destructors and virtual functions. */
271 if (TREE_CODE (decl) == FUNCTION_DECL
272 && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)))
273 return true;
275 /* Externally visible variables must be output. The exception is
276 COMDAT variables that must be output only when they are needed. */
277 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
278 return true;
280 return false;
283 /* Head and terminator of the queue of nodes to be processed while building
284 callgraph. */
286 static symtab_node symtab_terminator;
287 static symtab_node *queued_nodes = &symtab_terminator;
289 /* Add NODE to queue starting at QUEUED_NODES.
290 The queue is linked via AUX pointers and terminated by pointer to 1. */
292 static void
293 enqueue_node (symtab_node *node)
295 if (node->aux)
296 return;
297 gcc_checking_assert (queued_nodes);
298 node->aux = queued_nodes;
299 queued_nodes = node;
302 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
303 functions into callgraph in a way so they look like ordinary reachable
304 functions inserted into callgraph already at construction time. */
306 void
307 symbol_table::process_new_functions (void)
309 tree fndecl;
311 if (!cgraph_new_nodes.exists ())
312 return;
314 handle_alias_pairs ();
315 /* Note that this queue may grow as its being processed, as the new
316 functions may generate new ones. */
317 for (unsigned i = 0; i < cgraph_new_nodes.length (); i++)
319 cgraph_node *node = cgraph_new_nodes[i];
320 fndecl = node->decl;
321 switch (state)
323 case CONSTRUCTION:
324 /* At construction time we just need to finalize function and move
325 it into reachable functions list. */
327 cgraph_node::finalize_function (fndecl, false);
328 call_cgraph_insertion_hooks (node);
329 enqueue_node (node);
330 break;
332 case IPA:
333 case IPA_SSA:
334 case IPA_SSA_AFTER_INLINING:
335 /* When IPA optimization already started, do all essential
336 transformations that has been already performed on the whole
337 cgraph but not on this function. */
339 gimple_register_cfg_hooks ();
340 if (!node->analyzed)
341 node->analyze ();
342 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
343 if ((state == IPA_SSA || state == IPA_SSA_AFTER_INLINING)
344 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
345 g->get_passes ()->execute_early_local_passes ();
346 else if (inline_summaries != NULL)
347 compute_inline_parameters (node, true);
348 free_dominance_info (CDI_POST_DOMINATORS);
349 free_dominance_info (CDI_DOMINATORS);
350 pop_cfun ();
351 call_cgraph_insertion_hooks (node);
352 break;
354 case EXPANSION:
355 /* Functions created during expansion shall be compiled
356 directly. */
357 node->process = 0;
358 call_cgraph_insertion_hooks (node);
359 node->expand ();
360 break;
362 default:
363 gcc_unreachable ();
364 break;
368 cgraph_new_nodes.release ();
371 /* As an GCC extension we allow redefinition of the function. The
372 semantics when both copies of bodies differ is not well defined.
373 We replace the old body with new body so in unit at a time mode
374 we always use new body, while in normal mode we may end up with
375 old body inlined into some functions and new body expanded and
376 inlined in others.
378 ??? It may make more sense to use one body for inlining and other
379 body for expanding the function but this is difficult to do. */
381 void
382 cgraph_node::reset (void)
384 /* If process is set, then we have already begun whole-unit analysis.
385 This is *not* testing for whether we've already emitted the function.
386 That case can be sort-of legitimately seen with real function redefinition
387 errors. I would argue that the front end should never present us with
388 such a case, but don't enforce that for now. */
389 gcc_assert (!process);
391 /* Reset our data structures so we can analyze the function again. */
392 memset (&local, 0, sizeof (local));
393 memset (&global, 0, sizeof (global));
394 memset (&rtl, 0, sizeof (rtl));
395 analyzed = false;
396 definition = false;
397 alias = false;
398 weakref = false;
399 cpp_implicit_alias = false;
401 remove_callees ();
402 remove_all_references ();
405 /* Return true when there are references to the node. INCLUDE_SELF is
406 true if a self reference counts as a reference. */
408 bool
409 symtab_node::referred_to_p (bool include_self)
411 ipa_ref *ref = NULL;
413 /* See if there are any references at all. */
414 if (iterate_referring (0, ref))
415 return true;
416 /* For functions check also calls. */
417 cgraph_node *cn = dyn_cast <cgraph_node *> (this);
418 if (cn && cn->callers)
420 if (include_self)
421 return true;
422 for (cgraph_edge *e = cn->callers; e; e = e->next_caller)
423 if (e->caller != this)
424 return true;
426 return false;
429 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
430 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
431 the garbage collector run at the moment. We would need to either create
432 a new GC context, or just not compile right now. */
434 void
435 cgraph_node::finalize_function (tree decl, bool no_collect)
437 cgraph_node *node = cgraph_node::get_create (decl);
439 if (node->definition)
441 /* Nested functions should only be defined once. */
442 gcc_assert (!DECL_CONTEXT (decl)
443 || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL);
444 node->reset ();
445 node->local.redefined_extern_inline = true;
448 /* Set definition first before calling notice_global_symbol so that
449 it is available to notice_global_symbol. */
450 node->definition = true;
451 notice_global_symbol (decl);
452 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
454 /* With -fkeep-inline-functions we are keeping all inline functions except
455 for extern inline ones. */
456 if (flag_keep_inline_functions
457 && DECL_DECLARED_INLINE_P (decl)
458 && !DECL_EXTERNAL (decl)
459 && !DECL_DISREGARD_INLINE_LIMITS (decl))
460 node->force_output = 1;
462 /* When not optimizing, also output the static functions. (see
463 PR24561), but don't do so for always_inline functions, functions
464 declared inline and nested functions. These were optimized out
465 in the original implementation and it is unclear whether we want
466 to change the behavior here. */
467 if ((!opt_for_fn (decl, optimize)
468 && !node->cpp_implicit_alias
469 && !DECL_DISREGARD_INLINE_LIMITS (decl)
470 && !DECL_DECLARED_INLINE_P (decl)
471 && !(DECL_CONTEXT (decl)
472 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
473 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
474 node->force_output = 1;
476 /* If we've not yet emitted decl, tell the debug info about it. */
477 if (!TREE_ASM_WRITTEN (decl))
478 (*debug_hooks->deferred_inline_function) (decl);
480 /* Possibly warn about unused parameters. */
481 if (warn_unused_parameter)
482 do_warn_unused_parameter (decl);
484 if (!no_collect)
485 ggc_collect ();
487 if (symtab->state == CONSTRUCTION
488 && (node->needed_p () || node->referred_to_p ()))
489 enqueue_node (node);
492 /* Add the function FNDECL to the call graph.
493 Unlike finalize_function, this function is intended to be used
494 by middle end and allows insertion of new function at arbitrary point
495 of compilation. The function can be either in high, low or SSA form
496 GIMPLE.
498 The function is assumed to be reachable and have address taken (so no
499 API breaking optimizations are performed on it).
501 Main work done by this function is to enqueue the function for later
502 processing to avoid need the passes to be re-entrant. */
504 void
505 cgraph_node::add_new_function (tree fndecl, bool lowered)
507 gcc::pass_manager *passes = g->get_passes ();
508 cgraph_node *node;
510 if (dump_file)
512 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
513 const char *function_type = ((gimple_has_body_p (fndecl))
514 ? (lowered
515 ? (gimple_in_ssa_p (fn)
516 ? "ssa gimple"
517 : "low gimple")
518 : "high gimple")
519 : "to-be-gimplified");
520 fprintf (dump_file,
521 "Added new %s function %s to callgraph\n",
522 function_type,
523 fndecl_name (fndecl));
526 switch (symtab->state)
528 case PARSING:
529 cgraph_node::finalize_function (fndecl, false);
530 break;
531 case CONSTRUCTION:
532 /* Just enqueue function to be processed at nearest occurrence. */
533 node = cgraph_node::get_create (fndecl);
534 if (lowered)
535 node->lowered = true;
536 cgraph_new_nodes.safe_push (node);
537 break;
539 case IPA:
540 case IPA_SSA:
541 case IPA_SSA_AFTER_INLINING:
542 case EXPANSION:
543 /* Bring the function into finalized state and enqueue for later
544 analyzing and compilation. */
545 node = cgraph_node::get_create (fndecl);
546 node->local.local = false;
547 node->definition = true;
548 node->force_output = true;
549 if (!lowered && symtab->state == EXPANSION)
551 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
552 gimple_register_cfg_hooks ();
553 bitmap_obstack_initialize (NULL);
554 execute_pass_list (cfun, passes->all_lowering_passes);
555 passes->execute_early_local_passes ();
556 bitmap_obstack_release (NULL);
557 pop_cfun ();
559 lowered = true;
561 if (lowered)
562 node->lowered = true;
563 cgraph_new_nodes.safe_push (node);
564 break;
566 case FINISHED:
567 /* At the very end of compilation we have to do all the work up
568 to expansion. */
569 node = cgraph_node::create (fndecl);
570 if (lowered)
571 node->lowered = true;
572 node->definition = true;
573 node->analyze ();
574 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
575 gimple_register_cfg_hooks ();
576 bitmap_obstack_initialize (NULL);
577 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
578 g->get_passes ()->execute_early_local_passes ();
579 bitmap_obstack_release (NULL);
580 pop_cfun ();
581 node->expand ();
582 break;
584 default:
585 gcc_unreachable ();
588 /* Set a personality if required and we already passed EH lowering. */
589 if (lowered
590 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
591 == eh_personality_lang))
592 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
595 /* Analyze the function scheduled to be output. */
596 void
597 cgraph_node::analyze (void)
599 tree decl = this->decl;
600 location_t saved_loc = input_location;
601 input_location = DECL_SOURCE_LOCATION (decl);
603 if (thunk.thunk_p)
605 cgraph_node *t = cgraph_node::get (thunk.alias);
607 create_edge (t, NULL, 0, CGRAPH_FREQ_BASE);
608 /* Target code in expand_thunk may need the thunk's target
609 to be analyzed, so recurse here. */
610 if (!t->analyzed)
611 t->analyze ();
612 if (t->alias)
614 t = t->get_alias_target ();
615 if (!t->analyzed)
616 t->analyze ();
618 if (!expand_thunk (false, false))
620 thunk.alias = NULL;
621 return;
623 thunk.alias = NULL;
625 if (alias)
626 resolve_alias (cgraph_node::get (alias_target));
627 else if (dispatcher_function)
629 /* Generate the dispatcher body of multi-versioned functions. */
630 cgraph_function_version_info *dispatcher_version_info
631 = function_version ();
632 if (dispatcher_version_info != NULL
633 && (dispatcher_version_info->dispatcher_resolver
634 == NULL_TREE))
636 tree resolver = NULL_TREE;
637 gcc_assert (targetm.generate_version_dispatcher_body);
638 resolver = targetm.generate_version_dispatcher_body (this);
639 gcc_assert (resolver != NULL_TREE);
642 else
644 push_cfun (DECL_STRUCT_FUNCTION (decl));
646 assign_assembler_name_if_neeeded (decl);
648 /* Make sure to gimplify bodies only once. During analyzing a
649 function we lower it, which will require gimplified nested
650 functions, so we can end up here with an already gimplified
651 body. */
652 if (!gimple_has_body_p (decl))
653 gimplify_function_tree (decl);
655 /* Lower the function. */
656 if (!lowered)
658 if (nested)
659 lower_nested_functions (decl);
660 gcc_assert (!nested);
662 gimple_register_cfg_hooks ();
663 bitmap_obstack_initialize (NULL);
664 execute_pass_list (cfun, g->get_passes ()->all_lowering_passes);
665 free_dominance_info (CDI_POST_DOMINATORS);
666 free_dominance_info (CDI_DOMINATORS);
667 compact_blocks ();
668 bitmap_obstack_release (NULL);
669 lowered = true;
672 pop_cfun ();
674 analyzed = true;
676 input_location = saved_loc;
679 /* C++ frontend produce same body aliases all over the place, even before PCH
680 gets streamed out. It relies on us linking the aliases with their function
681 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
682 first produce aliases without links, but once C++ FE is sure he won't sream
683 PCH we build the links via this function. */
685 void
686 symbol_table::process_same_body_aliases (void)
688 symtab_node *node;
689 FOR_EACH_SYMBOL (node)
690 if (node->cpp_implicit_alias && !node->analyzed)
691 node->resolve_alias
692 (TREE_CODE (node->alias_target) == VAR_DECL
693 ? (symtab_node *)varpool_node::get_create (node->alias_target)
694 : (symtab_node *)cgraph_node::get_create (node->alias_target));
695 cpp_implicit_aliases_done = true;
698 /* Process attributes common for vars and functions. */
700 static void
701 process_common_attributes (symtab_node *node, tree decl)
703 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
705 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
707 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
708 "%<weakref%> attribute should be accompanied with"
709 " an %<alias%> attribute");
710 DECL_WEAK (decl) = 0;
711 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
712 DECL_ATTRIBUTES (decl));
715 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
716 node->no_reorder = 1;
719 /* Look for externally_visible and used attributes and mark cgraph nodes
720 accordingly.
722 We cannot mark the nodes at the point the attributes are processed (in
723 handle_*_attribute) because the copy of the declarations available at that
724 point may not be canonical. For example, in:
726 void f();
727 void f() __attribute__((used));
729 the declaration we see in handle_used_attribute will be the second
730 declaration -- but the front end will subsequently merge that declaration
731 with the original declaration and discard the second declaration.
733 Furthermore, we can't mark these nodes in finalize_function because:
735 void f() {}
736 void f() __attribute__((externally_visible));
738 is valid.
740 So, we walk the nodes at the end of the translation unit, applying the
741 attributes at that point. */
743 static void
744 process_function_and_variable_attributes (cgraph_node *first,
745 varpool_node *first_var)
747 cgraph_node *node;
748 varpool_node *vnode;
750 for (node = symtab->first_function (); node != first;
751 node = symtab->next_function (node))
753 tree decl = node->decl;
754 if (DECL_PRESERVE_P (decl))
755 node->mark_force_output ();
756 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
758 if (! TREE_PUBLIC (node->decl))
759 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
760 "%<externally_visible%>"
761 " attribute have effect only on public objects");
763 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
764 && (node->definition && !node->alias))
766 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
767 "%<weakref%> attribute ignored"
768 " because function is defined");
769 DECL_WEAK (decl) = 0;
770 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
771 DECL_ATTRIBUTES (decl));
774 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
775 && !DECL_DECLARED_INLINE_P (decl)
776 /* redefining extern inline function makes it DECL_UNINLINABLE. */
777 && !DECL_UNINLINABLE (decl))
778 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
779 "always_inline function might not be inlinable");
781 process_common_attributes (node, decl);
783 for (vnode = symtab->first_variable (); vnode != first_var;
784 vnode = symtab->next_variable (vnode))
786 tree decl = vnode->decl;
787 if (DECL_EXTERNAL (decl)
788 && DECL_INITIAL (decl))
789 varpool_node::finalize_decl (decl);
790 if (DECL_PRESERVE_P (decl))
791 vnode->force_output = true;
792 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
794 if (! TREE_PUBLIC (vnode->decl))
795 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
796 "%<externally_visible%>"
797 " attribute have effect only on public objects");
799 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
800 && vnode->definition
801 && DECL_INITIAL (decl))
803 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
804 "%<weakref%> attribute ignored"
805 " because variable is initialized");
806 DECL_WEAK (decl) = 0;
807 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
808 DECL_ATTRIBUTES (decl));
810 process_common_attributes (vnode, decl);
814 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
815 middle end to output the variable to asm file, if needed or externally
816 visible. */
818 void
819 varpool_node::finalize_decl (tree decl)
821 varpool_node *node = varpool_node::get_create (decl);
823 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
825 if (node->definition)
826 return;
827 /* Set definition first before calling notice_global_symbol so that
828 it is available to notice_global_symbol. */
829 node->definition = true;
830 notice_global_symbol (decl);
831 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
832 /* Traditionally we do not eliminate static variables when not
833 optimizing and when not doing toplevel reoder. */
834 || node->no_reorder
835 || ((!flag_toplevel_reorder
836 && !DECL_COMDAT (node->decl)
837 && !DECL_ARTIFICIAL (node->decl))))
838 node->force_output = true;
840 if (symtab->state == CONSTRUCTION
841 && (node->needed_p () || node->referred_to_p ()))
842 enqueue_node (node);
843 if (symtab->state >= IPA_SSA)
844 node->analyze ();
845 /* Some frontends produce various interface variables after compilation
846 finished. */
847 if (symtab->state == FINISHED
848 || (!flag_toplevel_reorder
849 && symtab->state == EXPANSION))
850 node->assemble_decl ();
852 if (DECL_INITIAL (decl))
853 chkp_register_var_initializer (decl);
856 /* EDGE is an polymorphic call. Mark all possible targets as reachable
857 and if there is only one target, perform trivial devirtualization.
858 REACHABLE_CALL_TARGETS collects target lists we already walked to
859 avoid udplicate work. */
861 static void
862 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
863 cgraph_edge *edge)
865 unsigned int i;
866 void *cache_token;
867 bool final;
868 vec <cgraph_node *>targets
869 = possible_polymorphic_call_targets
870 (edge, &final, &cache_token);
872 if (!reachable_call_targets->add (cache_token))
874 if (symtab->dump_file)
875 dump_possible_polymorphic_call_targets
876 (symtab->dump_file, edge);
878 for (i = 0; i < targets.length (); i++)
880 /* Do not bother to mark virtual methods in anonymous namespace;
881 either we will find use of virtual table defining it, or it is
882 unused. */
883 if (targets[i]->definition
884 && TREE_CODE
885 (TREE_TYPE (targets[i]->decl))
886 == METHOD_TYPE
887 && !type_in_anonymous_namespace_p
888 (TYPE_METHOD_BASETYPE (TREE_TYPE (targets[i]->decl))))
889 enqueue_node (targets[i]);
893 /* Very trivial devirtualization; when the type is
894 final or anonymous (so we know all its derivation)
895 and there is only one possible virtual call target,
896 make the edge direct. */
897 if (final)
899 if (targets.length () <= 1 && dbg_cnt (devirt))
901 cgraph_node *target;
902 if (targets.length () == 1)
903 target = targets[0];
904 else
905 target = cgraph_node::create
906 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
908 if (symtab->dump_file)
910 fprintf (symtab->dump_file,
911 "Devirtualizing call: ");
912 print_gimple_stmt (symtab->dump_file,
913 edge->call_stmt, 0,
914 TDF_SLIM);
916 if (dump_enabled_p ())
918 location_t locus = gimple_location_safe (edge->call_stmt);
919 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
920 "devirtualizing call in %s to %s\n",
921 edge->caller->name (), target->name ());
924 edge->make_direct (target);
925 edge->redirect_call_stmt_to_callee ();
927 /* Call to __builtin_unreachable shouldn't be instrumented. */
928 if (!targets.length ())
929 gimple_call_set_with_bounds (edge->call_stmt, false);
931 if (symtab->dump_file)
933 fprintf (symtab->dump_file,
934 "Devirtualized as: ");
935 print_gimple_stmt (symtab->dump_file,
936 edge->call_stmt, 0,
937 TDF_SLIM);
944 /* Discover all functions and variables that are trivially needed, analyze
945 them as well as all functions and variables referred by them */
946 static cgraph_node *first_analyzed;
947 static varpool_node *first_analyzed_var;
949 /* FIRST_TIME is set to TRUE for the first time we are called for a
950 translation unit from finalize_compilation_unit() or false
951 otherwise. */
953 static void
954 analyze_functions (bool first_time)
956 /* Keep track of already processed nodes when called multiple times for
957 intermodule optimization. */
958 cgraph_node *first_handled = first_analyzed;
959 varpool_node *first_handled_var = first_analyzed_var;
960 hash_set<void *> reachable_call_targets;
962 symtab_node *node;
963 symtab_node *next;
964 int i;
965 ipa_ref *ref;
966 bool changed = true;
967 location_t saved_loc = input_location;
969 bitmap_obstack_initialize (NULL);
970 symtab->state = CONSTRUCTION;
971 input_location = UNKNOWN_LOCATION;
973 /* Ugly, but the fixup can not happen at a time same body alias is created;
974 C++ FE is confused about the COMDAT groups being right. */
975 if (symtab->cpp_implicit_aliases_done)
976 FOR_EACH_SYMBOL (node)
977 if (node->cpp_implicit_alias)
978 node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
979 build_type_inheritance_graph ();
981 /* Analysis adds static variables that in turn adds references to new functions.
982 So we need to iterate the process until it stabilize. */
983 while (changed)
985 changed = false;
986 process_function_and_variable_attributes (first_analyzed,
987 first_analyzed_var);
989 /* First identify the trivially needed symbols. */
990 for (node = symtab->first_symbol ();
991 node != first_analyzed
992 && node != first_analyzed_var; node = node->next)
994 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
995 node->get_comdat_group_id ();
996 if (node->needed_p ())
998 enqueue_node (node);
999 if (!changed && symtab->dump_file)
1000 fprintf (symtab->dump_file, "Trivially needed symbols:");
1001 changed = true;
1002 if (symtab->dump_file)
1003 fprintf (symtab->dump_file, " %s", node->asm_name ());
1004 if (!changed && symtab->dump_file)
1005 fprintf (symtab->dump_file, "\n");
1007 if (node == first_analyzed
1008 || node == first_analyzed_var)
1009 break;
1011 symtab->process_new_functions ();
1012 first_analyzed_var = symtab->first_variable ();
1013 first_analyzed = symtab->first_function ();
1015 if (changed && symtab->dump_file)
1016 fprintf (symtab->dump_file, "\n");
1018 /* Lower representation, build callgraph edges and references for all trivially
1019 needed symbols and all symbols referred by them. */
1020 while (queued_nodes != &symtab_terminator)
1022 changed = true;
1023 node = queued_nodes;
1024 queued_nodes = (symtab_node *)queued_nodes->aux;
1025 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1026 if (cnode && cnode->definition)
1028 cgraph_edge *edge;
1029 tree decl = cnode->decl;
1031 /* ??? It is possible to create extern inline function
1032 and later using weak alias attribute to kill its body.
1033 See gcc.c-torture/compile/20011119-1.c */
1034 if (!DECL_STRUCT_FUNCTION (decl)
1035 && !cnode->alias
1036 && !cnode->thunk.thunk_p
1037 && !cnode->dispatcher_function)
1039 cnode->reset ();
1040 cnode->local.redefined_extern_inline = true;
1041 continue;
1044 if (!cnode->analyzed)
1045 cnode->analyze ();
1047 for (edge = cnode->callees; edge; edge = edge->next_callee)
1048 if (edge->callee->definition
1049 && (!DECL_EXTERNAL (edge->callee->decl)
1050 /* When not optimizing, do not try to analyze extern
1051 inline functions. Doing so is pointless. */
1052 || opt_for_fn (edge->callee->decl, optimize)
1053 /* Weakrefs needs to be preserved. */
1054 || edge->callee->alias
1055 /* always_inline functions are inlined aven at -O0. */
1056 || lookup_attribute
1057 ("always_inline",
1058 DECL_ATTRIBUTES (edge->callee->decl))
1059 /* Multiversioned functions needs the dispatcher to
1060 be produced locally even for extern functions. */
1061 || edge->callee->function_version ()))
1062 enqueue_node (edge->callee);
1063 if (opt_for_fn (cnode->decl, optimize)
1064 && opt_for_fn (cnode->decl, flag_devirtualize))
1066 cgraph_edge *next;
1068 for (edge = cnode->indirect_calls; edge; edge = next)
1070 next = edge->next_callee;
1071 if (edge->indirect_info->polymorphic)
1072 walk_polymorphic_call_targets (&reachable_call_targets,
1073 edge);
1077 /* If decl is a clone of an abstract function,
1078 mark that abstract function so that we don't release its body.
1079 The DECL_INITIAL() of that abstract function declaration
1080 will be later needed to output debug info. */
1081 if (DECL_ABSTRACT_ORIGIN (decl))
1083 cgraph_node *origin_node
1084 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl));
1085 origin_node->used_as_abstract_origin = true;
1088 else
1090 varpool_node *vnode = dyn_cast <varpool_node *> (node);
1091 if (vnode && vnode->definition && !vnode->analyzed)
1092 vnode->analyze ();
1095 if (node->same_comdat_group)
1097 symtab_node *next;
1098 for (next = node->same_comdat_group;
1099 next != node;
1100 next = next->same_comdat_group)
1101 if (!next->comdat_local_p ())
1102 enqueue_node (next);
1104 for (i = 0; node->iterate_reference (i, ref); i++)
1105 if (ref->referred->definition
1106 && (!DECL_EXTERNAL (ref->referred->decl)
1107 || ((TREE_CODE (ref->referred->decl) != FUNCTION_DECL
1108 && optimize)
1109 || (TREE_CODE (ref->referred->decl) == FUNCTION_DECL
1110 && opt_for_fn (ref->referred->decl, optimize))
1111 || node->alias
1112 || ref->referred->alias)))
1113 enqueue_node (ref->referred);
1114 symtab->process_new_functions ();
1117 update_type_inheritance_graph ();
1119 /* Collect entry points to the unit. */
1120 if (symtab->dump_file)
1122 fprintf (symtab->dump_file, "\n\nInitial ");
1123 symtab_node::dump_table (symtab->dump_file);
1126 if (first_time)
1128 symtab_node *snode;
1129 FOR_EACH_SYMBOL (snode)
1130 check_global_declaration (snode->decl);
1133 if (symtab->dump_file)
1134 fprintf (symtab->dump_file, "\nRemoving unused symbols:");
1136 for (node = symtab->first_symbol ();
1137 node != first_handled
1138 && node != first_handled_var; node = next)
1140 next = node->next;
1141 if (!node->aux && !node->referred_to_p ())
1143 if (symtab->dump_file)
1144 fprintf (symtab->dump_file, " %s", node->name ());
1146 /* See if the debugger can use anything before the DECL
1147 passes away. Perhaps it can notice a DECL that is now a
1148 constant and can tag the early DIE with an appropriate
1149 attribute.
1151 Otherwise, this is the last chance the debug_hooks have
1152 at looking at optimized away DECLs, since
1153 late_global_decl will subsequently be called from the
1154 contents of the now pruned symbol table. */
1155 if (!decl_function_context (node->decl))
1156 (*debug_hooks->late_global_decl) (node->decl);
1158 node->remove ();
1159 continue;
1161 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1163 tree decl = node->decl;
1165 if (cnode->definition && !gimple_has_body_p (decl)
1166 && !cnode->alias
1167 && !cnode->thunk.thunk_p)
1168 cnode->reset ();
1170 gcc_assert (!cnode->definition || cnode->thunk.thunk_p
1171 || cnode->alias
1172 || gimple_has_body_p (decl));
1173 gcc_assert (cnode->analyzed == cnode->definition);
1175 node->aux = NULL;
1177 for (;node; node = node->next)
1178 node->aux = NULL;
1179 first_analyzed = symtab->first_function ();
1180 first_analyzed_var = symtab->first_variable ();
1181 if (symtab->dump_file)
1183 fprintf (symtab->dump_file, "\n\nReclaimed ");
1184 symtab_node::dump_table (symtab->dump_file);
1186 bitmap_obstack_release (NULL);
1187 ggc_collect ();
1188 /* Initialize assembler name hash, in particular we want to trigger C++
1189 mangling and same body alias creation before we free DECL_ARGUMENTS
1190 used by it. */
1191 if (!seen_error ())
1192 symtab->symtab_initialize_asm_name_hash ();
1194 input_location = saved_loc;
1197 /* Translate the ugly representation of aliases as alias pairs into nice
1198 representation in callgraph. We don't handle all cases yet,
1199 unfortunately. */
1201 static void
1202 handle_alias_pairs (void)
1204 alias_pair *p;
1205 unsigned i;
1207 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1209 symtab_node *target_node = symtab_node::get_for_asmname (p->target);
1211 /* Weakrefs with target not defined in current unit are easy to handle:
1212 they behave just as external variables except we need to note the
1213 alias flag to later output the weakref pseudo op into asm file. */
1214 if (!target_node
1215 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1217 symtab_node *node = symtab_node::get (p->decl);
1218 if (node)
1220 node->alias_target = p->target;
1221 node->weakref = true;
1222 node->alias = true;
1224 alias_pairs->unordered_remove (i);
1225 continue;
1227 else if (!target_node)
1229 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1230 symtab_node *node = symtab_node::get (p->decl);
1231 if (node)
1232 node->alias = false;
1233 alias_pairs->unordered_remove (i);
1234 continue;
1237 if (DECL_EXTERNAL (target_node->decl)
1238 /* We use local aliases for C++ thunks to force the tailcall
1239 to bind locally. This is a hack - to keep it working do
1240 the following (which is not strictly correct). */
1241 && (TREE_CODE (target_node->decl) != FUNCTION_DECL
1242 || ! DECL_VIRTUAL_P (target_node->decl))
1243 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1245 error ("%q+D aliased to external symbol %qE",
1246 p->decl, p->target);
1249 if (TREE_CODE (p->decl) == FUNCTION_DECL
1250 && target_node && is_a <cgraph_node *> (target_node))
1252 cgraph_node *src_node = cgraph_node::get (p->decl);
1253 if (src_node && src_node->definition)
1254 src_node->reset ();
1255 cgraph_node::create_alias (p->decl, target_node->decl);
1256 alias_pairs->unordered_remove (i);
1258 else if (TREE_CODE (p->decl) == VAR_DECL
1259 && target_node && is_a <varpool_node *> (target_node))
1261 varpool_node::create_alias (p->decl, target_node->decl);
1262 alias_pairs->unordered_remove (i);
1264 else
1266 error ("%q+D alias in between function and variable is not supported",
1267 p->decl);
1268 warning (0, "%q+D aliased declaration",
1269 target_node->decl);
1270 alias_pairs->unordered_remove (i);
1273 vec_free (alias_pairs);
1277 /* Figure out what functions we want to assemble. */
1279 static void
1280 mark_functions_to_output (void)
1282 cgraph_node *node;
1283 #ifdef ENABLE_CHECKING
1284 bool check_same_comdat_groups = false;
1286 FOR_EACH_FUNCTION (node)
1287 gcc_assert (!node->process);
1288 #endif
1290 FOR_EACH_FUNCTION (node)
1292 tree decl = node->decl;
1294 gcc_assert (!node->process || node->same_comdat_group);
1295 if (node->process)
1296 continue;
1298 /* We need to output all local functions that are used and not
1299 always inlined, as well as those that are reachable from
1300 outside the current compilation unit. */
1301 if (node->analyzed
1302 && !node->thunk.thunk_p
1303 && !node->alias
1304 && !node->global.inlined_to
1305 && !TREE_ASM_WRITTEN (decl)
1306 && !DECL_EXTERNAL (decl))
1308 node->process = 1;
1309 if (node->same_comdat_group)
1311 cgraph_node *next;
1312 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
1313 next != node;
1314 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
1315 if (!next->thunk.thunk_p && !next->alias
1316 && !next->comdat_local_p ())
1317 next->process = 1;
1320 else if (node->same_comdat_group)
1322 #ifdef ENABLE_CHECKING
1323 check_same_comdat_groups = true;
1324 #endif
1326 else
1328 /* We should've reclaimed all functions that are not needed. */
1329 #ifdef ENABLE_CHECKING
1330 if (!node->global.inlined_to
1331 && gimple_has_body_p (decl)
1332 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1333 are inside partition, we can end up not removing the body since we no longer
1334 have analyzed node pointing to it. */
1335 && !node->in_other_partition
1336 && !node->alias
1337 && !node->clones
1338 && !DECL_EXTERNAL (decl))
1340 node->debug ();
1341 internal_error ("failed to reclaim unneeded function");
1343 #endif
1344 gcc_assert (node->global.inlined_to
1345 || !gimple_has_body_p (decl)
1346 || node->in_other_partition
1347 || node->clones
1348 || DECL_ARTIFICIAL (decl)
1349 || DECL_EXTERNAL (decl));
1354 #ifdef ENABLE_CHECKING
1355 if (check_same_comdat_groups)
1356 FOR_EACH_FUNCTION (node)
1357 if (node->same_comdat_group && !node->process)
1359 tree decl = node->decl;
1360 if (!node->global.inlined_to
1361 && gimple_has_body_p (decl)
1362 /* FIXME: in an ltrans unit when the offline copy is outside a
1363 partition but inline copies are inside a partition, we can
1364 end up not removing the body since we no longer have an
1365 analyzed node pointing to it. */
1366 && !node->in_other_partition
1367 && !node->clones
1368 && !DECL_EXTERNAL (decl))
1370 node->debug ();
1371 internal_error ("failed to reclaim unneeded function in same "
1372 "comdat group");
1375 #endif
1378 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1379 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1381 Set current_function_decl and cfun to newly constructed empty function body.
1382 return basic block in the function body. */
1384 basic_block
1385 init_lowered_empty_function (tree decl, bool in_ssa, gcov_type count)
1387 basic_block bb;
1388 edge e;
1390 current_function_decl = decl;
1391 allocate_struct_function (decl, false);
1392 gimple_register_cfg_hooks ();
1393 init_empty_tree_cfg ();
1395 if (in_ssa)
1397 init_tree_ssa (cfun);
1398 init_ssa_operands (cfun);
1399 cfun->gimple_df->in_ssa_p = true;
1400 cfun->curr_properties |= PROP_ssa;
1403 DECL_INITIAL (decl) = make_node (BLOCK);
1405 DECL_SAVED_TREE (decl) = error_mark_node;
1406 cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1407 | PROP_cfg | PROP_loops);
1409 set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ());
1410 init_loops_structure (cfun, loops_for_fn (cfun), 1);
1411 loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1413 /* Create BB for body of the function and connect it properly. */
1414 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = count;
1415 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency = REG_BR_PROB_BASE;
1416 EXIT_BLOCK_PTR_FOR_FN (cfun)->count = count;
1417 EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency = REG_BR_PROB_BASE;
1418 bb = create_basic_block (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun));
1419 bb->count = count;
1420 bb->frequency = BB_FREQ_MAX;
1421 e = make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU);
1422 e->count = count;
1423 e->probability = REG_BR_PROB_BASE;
1424 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1425 e->count = count;
1426 e->probability = REG_BR_PROB_BASE;
1427 add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
1429 return bb;
1432 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1433 offset indicated by VIRTUAL_OFFSET, if that is
1434 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1435 zero for a result adjusting thunk. */
1437 static tree
1438 thunk_adjust (gimple_stmt_iterator * bsi,
1439 tree ptr, bool this_adjusting,
1440 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1442 gassign *stmt;
1443 tree ret;
1445 if (this_adjusting
1446 && fixed_offset != 0)
1448 stmt = gimple_build_assign
1449 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1450 ptr,
1451 fixed_offset));
1452 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1455 /* If there's a virtual offset, look up that value in the vtable and
1456 adjust the pointer again. */
1457 if (virtual_offset)
1459 tree vtabletmp;
1460 tree vtabletmp2;
1461 tree vtabletmp3;
1463 if (!vtable_entry_type)
1465 tree vfunc_type = make_node (FUNCTION_TYPE);
1466 TREE_TYPE (vfunc_type) = integer_type_node;
1467 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1468 layout_type (vfunc_type);
1470 vtable_entry_type = build_pointer_type (vfunc_type);
1473 vtabletmp =
1474 create_tmp_reg (build_pointer_type
1475 (build_pointer_type (vtable_entry_type)), "vptr");
1477 /* The vptr is always at offset zero in the object. */
1478 stmt = gimple_build_assign (vtabletmp,
1479 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1480 ptr));
1481 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1483 /* Form the vtable address. */
1484 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
1485 "vtableaddr");
1486 stmt = gimple_build_assign (vtabletmp2,
1487 build_simple_mem_ref (vtabletmp));
1488 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1490 /* Find the entry with the vcall offset. */
1491 stmt = gimple_build_assign (vtabletmp2,
1492 fold_build_pointer_plus_loc (input_location,
1493 vtabletmp2,
1494 virtual_offset));
1495 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1497 /* Get the offset itself. */
1498 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1499 "vcalloffset");
1500 stmt = gimple_build_assign (vtabletmp3,
1501 build_simple_mem_ref (vtabletmp2));
1502 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1504 /* Adjust the `this' pointer. */
1505 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1506 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1507 GSI_CONTINUE_LINKING);
1510 if (!this_adjusting
1511 && fixed_offset != 0)
1512 /* Adjust the pointer by the constant. */
1514 tree ptrtmp;
1516 if (TREE_CODE (ptr) == VAR_DECL)
1517 ptrtmp = ptr;
1518 else
1520 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
1521 stmt = gimple_build_assign (ptrtmp, ptr);
1522 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1524 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1525 ptrtmp, fixed_offset);
1528 /* Emit the statement and gimplify the adjustment expression. */
1529 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
1530 stmt = gimple_build_assign (ret, ptr);
1531 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1533 return ret;
1536 /* Expand thunk NODE to gimple if possible.
1537 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1538 no assembler is produced.
1539 When OUTPUT_ASM_THUNK is true, also produce assembler for
1540 thunks that are not lowered. */
1542 bool
1543 cgraph_node::expand_thunk (bool output_asm_thunks, bool force_gimple_thunk)
1545 bool this_adjusting = thunk.this_adjusting;
1546 HOST_WIDE_INT fixed_offset = thunk.fixed_offset;
1547 HOST_WIDE_INT virtual_value = thunk.virtual_value;
1548 tree virtual_offset = NULL;
1549 tree alias = callees->callee->decl;
1550 tree thunk_fndecl = decl;
1551 tree a;
1553 /* Instrumentation thunk is the same function with
1554 a different signature. Never need to expand it. */
1555 if (thunk.add_pointer_bounds_args)
1556 return false;
1558 if (!force_gimple_thunk && this_adjusting
1559 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1560 virtual_value, alias))
1562 const char *fnname;
1563 tree fn_block;
1564 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1566 if (!output_asm_thunks)
1568 analyzed = true;
1569 return false;
1572 if (in_lto_p)
1573 get_untransformed_body ();
1574 a = DECL_ARGUMENTS (thunk_fndecl);
1576 current_function_decl = thunk_fndecl;
1578 /* Ensure thunks are emitted in their correct sections. */
1579 resolve_unique_section (thunk_fndecl, 0,
1580 flag_function_sections);
1582 DECL_RESULT (thunk_fndecl)
1583 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1584 RESULT_DECL, 0, restype);
1585 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1586 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
1588 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1589 create one. */
1590 fn_block = make_node (BLOCK);
1591 BLOCK_VARS (fn_block) = a;
1592 DECL_INITIAL (thunk_fndecl) = fn_block;
1593 init_function_start (thunk_fndecl);
1594 cfun->is_thunk = 1;
1595 insn_locations_init ();
1596 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1597 prologue_location = curr_insn_location ();
1598 assemble_start_function (thunk_fndecl, fnname);
1600 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1601 fixed_offset, virtual_value, alias);
1603 assemble_end_function (thunk_fndecl, fnname);
1604 insn_locations_finalize ();
1605 init_insn_lengths ();
1606 free_after_compilation (cfun);
1607 set_cfun (NULL);
1608 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1609 thunk.thunk_p = false;
1610 analyzed = false;
1612 else if (stdarg_p (TREE_TYPE (thunk_fndecl)))
1614 error ("generic thunk code fails for method %qD which uses %<...%>",
1615 thunk_fndecl);
1616 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1617 analyzed = true;
1618 return false;
1620 else
1622 tree restype;
1623 basic_block bb, then_bb, else_bb, return_bb;
1624 gimple_stmt_iterator bsi;
1625 int nargs = 0;
1626 tree arg;
1627 int i;
1628 tree resdecl;
1629 tree restmp = NULL;
1630 tree resbnd = NULL;
1632 gcall *call;
1633 greturn *ret;
1634 bool alias_is_noreturn = TREE_THIS_VOLATILE (alias);
1636 if (in_lto_p)
1637 get_untransformed_body ();
1638 a = DECL_ARGUMENTS (thunk_fndecl);
1640 current_function_decl = thunk_fndecl;
1642 /* Ensure thunks are emitted in their correct sections. */
1643 resolve_unique_section (thunk_fndecl, 0,
1644 flag_function_sections);
1646 DECL_IGNORED_P (thunk_fndecl) = 1;
1647 bitmap_obstack_initialize (NULL);
1649 if (thunk.virtual_offset_p)
1650 virtual_offset = size_int (virtual_value);
1652 /* Build the return declaration for the function. */
1653 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1654 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1656 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1657 DECL_ARTIFICIAL (resdecl) = 1;
1658 DECL_IGNORED_P (resdecl) = 1;
1659 DECL_RESULT (thunk_fndecl) = resdecl;
1660 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1662 else
1663 resdecl = DECL_RESULT (thunk_fndecl);
1665 bb = then_bb = else_bb = return_bb
1666 = init_lowered_empty_function (thunk_fndecl, true, count);
1668 bsi = gsi_start_bb (bb);
1670 /* Build call to the function being thunked. */
1671 if (!VOID_TYPE_P (restype) && !alias_is_noreturn)
1673 if (DECL_BY_REFERENCE (resdecl))
1675 restmp = gimple_fold_indirect_ref (resdecl);
1676 if (!restmp)
1677 restmp = build2 (MEM_REF,
1678 TREE_TYPE (TREE_TYPE (DECL_RESULT (alias))),
1679 resdecl,
1680 build_int_cst (TREE_TYPE
1681 (DECL_RESULT (alias)), 0));
1683 else if (!is_gimple_reg_type (restype))
1685 if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl)))
1687 restmp = resdecl;
1689 if (TREE_CODE (restmp) == VAR_DECL)
1690 add_local_decl (cfun, restmp);
1691 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1693 else
1694 restmp = create_tmp_var (restype, "retval");
1696 else
1697 restmp = create_tmp_reg (restype, "retval");
1700 for (arg = a; arg; arg = DECL_CHAIN (arg))
1701 nargs++;
1702 auto_vec<tree> vargs (nargs);
1703 i = 0;
1704 arg = a;
1705 if (this_adjusting)
1707 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
1708 virtual_offset));
1709 arg = DECL_CHAIN (a);
1710 i = 1;
1713 if (nargs)
1714 for (; i < nargs; i++, arg = DECL_CHAIN (arg))
1716 tree tmp = arg;
1717 if (!is_gimple_val (arg))
1719 tmp = create_tmp_reg (TYPE_MAIN_VARIANT
1720 (TREE_TYPE (arg)), "arg");
1721 gimple stmt = gimple_build_assign (tmp, arg);
1722 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1724 vargs.quick_push (tmp);
1726 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1727 callees->call_stmt = call;
1728 gimple_call_set_from_thunk (call, true);
1729 gimple_call_set_with_bounds (call, instrumentation_clone);
1731 /* Return slot optimization is always possible and in fact requred to
1732 return values with DECL_BY_REFERENCE. */
1733 if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl))
1734 && (!is_gimple_reg_type (TREE_TYPE (resdecl))
1735 || DECL_BY_REFERENCE (resdecl)))
1736 gimple_call_set_return_slot_opt (call, true);
1738 if (restmp && !alias_is_noreturn)
1740 gimple_call_set_lhs (call, restmp);
1741 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp),
1742 TREE_TYPE (TREE_TYPE (alias))));
1744 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
1745 if (!alias_is_noreturn)
1747 if (instrumentation_clone
1748 && !DECL_BY_REFERENCE (resdecl)
1749 && restmp
1750 && BOUNDED_P (restmp))
1752 resbnd = chkp_insert_retbnd_call (NULL, restmp, &bsi);
1753 create_edge (get_create (gimple_call_fndecl (gsi_stmt (bsi))),
1754 as_a <gcall *> (gsi_stmt (bsi)),
1755 callees->count, callees->frequency);
1758 if (restmp && !this_adjusting
1759 && (fixed_offset || virtual_offset))
1761 tree true_label = NULL_TREE;
1763 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1765 gimple stmt;
1766 edge e;
1767 /* If the return type is a pointer, we need to
1768 protect against NULL. We know there will be an
1769 adjustment, because that's why we're emitting a
1770 thunk. */
1771 then_bb = create_basic_block (NULL, bb);
1772 then_bb->count = count - count / 16;
1773 then_bb->frequency = BB_FREQ_MAX - BB_FREQ_MAX / 16;
1774 return_bb = create_basic_block (NULL, then_bb);
1775 return_bb->count = count;
1776 return_bb->frequency = BB_FREQ_MAX;
1777 else_bb = create_basic_block (NULL, else_bb);
1778 then_bb->count = count / 16;
1779 then_bb->frequency = BB_FREQ_MAX / 16;
1780 add_bb_to_loop (then_bb, bb->loop_father);
1781 add_bb_to_loop (return_bb, bb->loop_father);
1782 add_bb_to_loop (else_bb, bb->loop_father);
1783 remove_edge (single_succ_edge (bb));
1784 true_label = gimple_block_label (then_bb);
1785 stmt = gimple_build_cond (NE_EXPR, restmp,
1786 build_zero_cst (TREE_TYPE (restmp)),
1787 NULL_TREE, NULL_TREE);
1788 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1789 e = make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1790 e->probability = REG_BR_PROB_BASE - REG_BR_PROB_BASE / 16;
1791 e->count = count - count / 16;
1792 e = make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1793 e->probability = REG_BR_PROB_BASE / 16;
1794 e->count = count / 16;
1795 e = make_edge (return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1796 e->probability = REG_BR_PROB_BASE;
1797 e->count = count;
1798 e = make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1799 e->probability = REG_BR_PROB_BASE;
1800 e->count = count - count / 16;
1801 e = make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1802 e->probability = REG_BR_PROB_BASE;
1803 e->count = count / 16;
1804 bsi = gsi_last_bb (then_bb);
1807 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1808 fixed_offset, virtual_offset);
1809 if (true_label)
1811 gimple stmt;
1812 bsi = gsi_last_bb (else_bb);
1813 stmt = gimple_build_assign (restmp,
1814 build_zero_cst (TREE_TYPE (restmp)));
1815 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1816 bsi = gsi_last_bb (return_bb);
1819 else
1820 gimple_call_set_tail (call, true);
1822 /* Build return value. */
1823 if (!DECL_BY_REFERENCE (resdecl))
1824 ret = gimple_build_return (restmp);
1825 else
1826 ret = gimple_build_return (resdecl);
1827 gimple_return_set_retbnd (ret, resbnd);
1829 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1831 else
1833 gimple_call_set_tail (call, true);
1834 remove_edge (single_succ_edge (bb));
1837 cfun->gimple_df->in_ssa_p = true;
1838 profile_status_for_fn (cfun)
1839 = count ? PROFILE_READ : PROFILE_GUESSED;
1840 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
1841 TREE_ASM_WRITTEN (thunk_fndecl) = false;
1842 delete_unreachable_blocks ();
1843 update_ssa (TODO_update_ssa);
1844 #ifdef ENABLE_CHECKING
1845 verify_flow_info ();
1846 #endif
1847 free_dominance_info (CDI_DOMINATORS);
1849 /* Since we want to emit the thunk, we explicitly mark its name as
1850 referenced. */
1851 thunk.thunk_p = false;
1852 lowered = true;
1853 bitmap_obstack_release (NULL);
1855 current_function_decl = NULL;
1856 set_cfun (NULL);
1857 return true;
1860 /* Assemble thunks and aliases associated to node. */
1862 void
1863 cgraph_node::assemble_thunks_and_aliases (void)
1865 cgraph_edge *e;
1866 ipa_ref *ref;
1868 for (e = callers; e;)
1869 if (e->caller->thunk.thunk_p
1870 && !e->caller->thunk.add_pointer_bounds_args)
1872 cgraph_node *thunk = e->caller;
1874 e = e->next_caller;
1875 thunk->expand_thunk (true, false);
1876 thunk->assemble_thunks_and_aliases ();
1878 else
1879 e = e->next_caller;
1881 FOR_EACH_ALIAS (this, ref)
1883 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1884 bool saved_written = TREE_ASM_WRITTEN (decl);
1886 /* Force assemble_alias to really output the alias this time instead
1887 of buffering it in same alias pairs. */
1888 TREE_ASM_WRITTEN (decl) = 1;
1889 do_assemble_alias (alias->decl,
1890 DECL_ASSEMBLER_NAME (decl));
1891 alias->assemble_thunks_and_aliases ();
1892 TREE_ASM_WRITTEN (decl) = saved_written;
1896 /* Expand function specified by node. */
1898 void
1899 cgraph_node::expand (void)
1901 location_t saved_loc;
1903 /* We ought to not compile any inline clones. */
1904 gcc_assert (!global.inlined_to);
1906 announce_function (decl);
1907 process = 0;
1908 gcc_assert (lowered);
1909 get_untransformed_body ();
1911 /* Generate RTL for the body of DECL. */
1913 timevar_push (TV_REST_OF_COMPILATION);
1915 gcc_assert (symtab->global_info_ready);
1917 /* Initialize the default bitmap obstack. */
1918 bitmap_obstack_initialize (NULL);
1920 /* Initialize the RTL code for the function. */
1921 current_function_decl = decl;
1922 saved_loc = input_location;
1923 input_location = DECL_SOURCE_LOCATION (decl);
1924 init_function_start (decl);
1926 gimple_register_cfg_hooks ();
1928 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1930 execute_all_ipa_transforms ();
1932 /* Perform all tree transforms and optimizations. */
1934 /* Signal the start of passes. */
1935 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1937 execute_pass_list (cfun, g->get_passes ()->all_passes);
1939 /* Signal the end of passes. */
1940 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1942 bitmap_obstack_release (&reg_obstack);
1944 /* Release the default bitmap obstack. */
1945 bitmap_obstack_release (NULL);
1947 /* If requested, warn about function definitions where the function will
1948 return a value (usually of some struct or union type) which itself will
1949 take up a lot of stack space. */
1950 if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl))
1952 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
1954 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1955 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1956 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1957 larger_than_size))
1959 unsigned int size_as_int
1960 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1962 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1963 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
1964 decl, size_as_int);
1965 else
1966 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
1967 decl, larger_than_size);
1971 gimple_set_body (decl, NULL);
1972 if (DECL_STRUCT_FUNCTION (decl) == 0
1973 && !cgraph_node::get (decl)->origin)
1975 /* Stop pointing to the local nodes about to be freed.
1976 But DECL_INITIAL must remain nonzero so we know this
1977 was an actual function definition.
1978 For a nested function, this is done in c_pop_function_context.
1979 If rest_of_compilation set this to 0, leave it 0. */
1980 if (DECL_INITIAL (decl) != 0)
1981 DECL_INITIAL (decl) = error_mark_node;
1984 input_location = saved_loc;
1986 ggc_collect ();
1987 timevar_pop (TV_REST_OF_COMPILATION);
1989 /* Make sure that BE didn't give up on compiling. */
1990 gcc_assert (TREE_ASM_WRITTEN (decl));
1991 set_cfun (NULL);
1992 current_function_decl = NULL;
1994 /* It would make a lot more sense to output thunks before function body to get more
1995 forward and lest backwarding jumps. This however would need solving problem
1996 with comdats. See PR48668. Also aliases must come after function itself to
1997 make one pass assemblers, like one on AIX, happy. See PR 50689.
1998 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1999 groups. */
2000 assemble_thunks_and_aliases ();
2001 release_body ();
2002 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
2003 points to the dead function body. */
2004 remove_callees ();
2005 remove_all_references ();
2008 /* Node comparer that is responsible for the order that corresponds
2009 to time when a function was launched for the first time. */
2011 static int
2012 node_cmp (const void *pa, const void *pb)
2014 const cgraph_node *a = *(const cgraph_node * const *) pa;
2015 const cgraph_node *b = *(const cgraph_node * const *) pb;
2017 /* Functions with time profile must be before these without profile. */
2018 if (!a->tp_first_run || !b->tp_first_run)
2019 return a->tp_first_run - b->tp_first_run;
2021 return a->tp_first_run != b->tp_first_run
2022 ? b->tp_first_run - a->tp_first_run
2023 : b->order - a->order;
2026 /* Expand all functions that must be output.
2028 Attempt to topologically sort the nodes so function is output when
2029 all called functions are already assembled to allow data to be
2030 propagated across the callgraph. Use a stack to get smaller distance
2031 between a function and its callees (later we may choose to use a more
2032 sophisticated algorithm for function reordering; we will likely want
2033 to use subsections to make the output functions appear in top-down
2034 order). */
2036 static void
2037 expand_all_functions (void)
2039 cgraph_node *node;
2040 cgraph_node **order = XCNEWVEC (cgraph_node *,
2041 symtab->cgraph_count);
2042 unsigned int expanded_func_count = 0, profiled_func_count = 0;
2043 int order_pos, new_order_pos = 0;
2044 int i;
2046 order_pos = ipa_reverse_postorder (order);
2047 gcc_assert (order_pos == symtab->cgraph_count);
2049 /* Garbage collector may remove inline clones we eliminate during
2050 optimization. So we must be sure to not reference them. */
2051 for (i = 0; i < order_pos; i++)
2052 if (order[i]->process)
2053 order[new_order_pos++] = order[i];
2055 if (flag_profile_reorder_functions)
2056 qsort (order, new_order_pos, sizeof (cgraph_node *), node_cmp);
2058 for (i = new_order_pos - 1; i >= 0; i--)
2060 node = order[i];
2062 if (node->process)
2064 expanded_func_count++;
2065 if(node->tp_first_run)
2066 profiled_func_count++;
2068 if (symtab->dump_file)
2069 fprintf (symtab->dump_file,
2070 "Time profile order in expand_all_functions:%s:%d\n",
2071 node->asm_name (), node->tp_first_run);
2072 node->process = 0;
2073 node->expand ();
2077 if (dump_file)
2078 fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
2079 main_input_filename, profiled_func_count, expanded_func_count);
2081 if (symtab->dump_file && flag_profile_reorder_functions)
2082 fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n",
2083 profiled_func_count, expanded_func_count);
2085 symtab->process_new_functions ();
2086 free_gimplify_stack ();
2088 free (order);
2091 /* This is used to sort the node types by the cgraph order number. */
2093 enum cgraph_order_sort_kind
2095 ORDER_UNDEFINED = 0,
2096 ORDER_FUNCTION,
2097 ORDER_VAR,
2098 ORDER_ASM
2101 struct cgraph_order_sort
2103 enum cgraph_order_sort_kind kind;
2104 union
2106 cgraph_node *f;
2107 varpool_node *v;
2108 asm_node *a;
2109 } u;
2112 /* Output all functions, variables, and asm statements in the order
2113 according to their order fields, which is the order in which they
2114 appeared in the file. This implements -fno-toplevel-reorder. In
2115 this mode we may output functions and variables which don't really
2116 need to be output.
2117 When NO_REORDER is true only do this for symbols marked no reorder. */
2119 static void
2120 output_in_order (bool no_reorder)
2122 int max;
2123 cgraph_order_sort *nodes;
2124 int i;
2125 cgraph_node *pf;
2126 varpool_node *pv;
2127 asm_node *pa;
2128 max = symtab->order;
2129 nodes = XCNEWVEC (cgraph_order_sort, max);
2131 FOR_EACH_DEFINED_FUNCTION (pf)
2133 if (pf->process && !pf->thunk.thunk_p && !pf->alias)
2135 if (no_reorder && !pf->no_reorder)
2136 continue;
2137 i = pf->order;
2138 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2139 nodes[i].kind = ORDER_FUNCTION;
2140 nodes[i].u.f = pf;
2144 FOR_EACH_DEFINED_VARIABLE (pv)
2145 if (!DECL_EXTERNAL (pv->decl))
2147 if (no_reorder && !pv->no_reorder)
2148 continue;
2149 i = pv->order;
2150 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2151 nodes[i].kind = ORDER_VAR;
2152 nodes[i].u.v = pv;
2155 for (pa = symtab->first_asm_symbol (); pa; pa = pa->next)
2157 i = pa->order;
2158 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2159 nodes[i].kind = ORDER_ASM;
2160 nodes[i].u.a = pa;
2163 /* In toplevel reorder mode we output all statics; mark them as needed. */
2165 for (i = 0; i < max; ++i)
2166 if (nodes[i].kind == ORDER_VAR)
2167 nodes[i].u.v->finalize_named_section_flags ();
2169 for (i = 0; i < max; ++i)
2171 switch (nodes[i].kind)
2173 case ORDER_FUNCTION:
2174 nodes[i].u.f->process = 0;
2175 nodes[i].u.f->expand ();
2176 break;
2178 case ORDER_VAR:
2179 nodes[i].u.v->assemble_decl ();
2180 break;
2182 case ORDER_ASM:
2183 assemble_asm (nodes[i].u.a->asm_str);
2184 break;
2186 case ORDER_UNDEFINED:
2187 break;
2189 default:
2190 gcc_unreachable ();
2194 symtab->clear_asm_symbols ();
2196 free (nodes);
2199 static void
2200 ipa_passes (void)
2202 gcc::pass_manager *passes = g->get_passes ();
2204 set_cfun (NULL);
2205 current_function_decl = NULL;
2206 gimple_register_cfg_hooks ();
2207 bitmap_obstack_initialize (NULL);
2209 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2211 if (!in_lto_p)
2213 execute_ipa_pass_list (passes->all_small_ipa_passes);
2214 if (seen_error ())
2215 return;
2218 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2219 devirtualization and other changes where removal iterate. */
2220 symtab->remove_unreachable_nodes (symtab->dump_file);
2222 /* If pass_all_early_optimizations was not scheduled, the state of
2223 the cgraph will not be properly updated. Update it now. */
2224 if (symtab->state < IPA_SSA)
2225 symtab->state = IPA_SSA;
2227 if (!in_lto_p)
2229 /* Generate coverage variables and constructors. */
2230 coverage_finish ();
2232 /* Process new functions added. */
2233 set_cfun (NULL);
2234 current_function_decl = NULL;
2235 symtab->process_new_functions ();
2237 execute_ipa_summary_passes
2238 ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2241 /* Some targets need to handle LTO assembler output specially. */
2242 if (flag_generate_lto || flag_generate_offload)
2243 targetm.asm_out.lto_start ();
2245 if (!in_lto_p)
2247 if (g->have_offload)
2249 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2250 lto_stream_offload_p = true;
2251 ipa_write_summaries ();
2252 lto_stream_offload_p = false;
2254 if (flag_lto)
2256 section_name_prefix = LTO_SECTION_NAME_PREFIX;
2257 lto_stream_offload_p = false;
2258 ipa_write_summaries ();
2262 if (flag_generate_lto || flag_generate_offload)
2263 targetm.asm_out.lto_end ();
2265 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
2266 execute_ipa_pass_list (passes->all_regular_ipa_passes);
2267 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2269 bitmap_obstack_release (NULL);
2273 /* Return string alias is alias of. */
2275 static tree
2276 get_alias_symbol (tree decl)
2278 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
2279 return get_identifier (TREE_STRING_POINTER
2280 (TREE_VALUE (TREE_VALUE (alias))));
2284 /* Weakrefs may be associated to external decls and thus not output
2285 at expansion time. Emit all necessary aliases. */
2287 void
2288 symbol_table::output_weakrefs (void)
2290 symtab_node *node;
2291 cgraph_node *cnode;
2292 FOR_EACH_SYMBOL (node)
2293 if (node->alias
2294 && !TREE_ASM_WRITTEN (node->decl)
2295 && (!(cnode = dyn_cast <cgraph_node *> (node))
2296 || !cnode->instrumented_version
2297 || !TREE_ASM_WRITTEN (cnode->instrumented_version->decl))
2298 && node->weakref)
2300 tree target;
2302 /* Weakrefs are special by not requiring target definition in current
2303 compilation unit. It is thus bit hard to work out what we want to
2304 alias.
2305 When alias target is defined, we need to fetch it from symtab reference,
2306 otherwise it is pointed to by alias_target. */
2307 if (node->alias_target)
2308 target = (DECL_P (node->alias_target)
2309 ? DECL_ASSEMBLER_NAME (node->alias_target)
2310 : node->alias_target);
2311 else if (node->analyzed)
2312 target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
2313 else
2315 gcc_unreachable ();
2316 target = get_alias_symbol (node->decl);
2318 do_assemble_alias (node->decl, target);
2322 /* Perform simple optimizations based on callgraph. */
2324 void
2325 symbol_table::compile (void)
2327 if (seen_error ())
2328 return;
2330 #ifdef ENABLE_CHECKING
2331 symtab_node::verify_symtab_nodes ();
2332 #endif
2334 timevar_push (TV_CGRAPHOPT);
2335 if (pre_ipa_mem_report)
2337 fprintf (stderr, "Memory consumption before IPA\n");
2338 dump_memory_report (false);
2340 if (!quiet_flag)
2341 fprintf (stderr, "Performing interprocedural optimizations\n");
2342 state = IPA;
2344 /* Offloading requires LTO infrastructure. */
2345 if (!in_lto_p && g->have_offload)
2346 flag_generate_offload = 1;
2348 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2349 if (flag_generate_lto || flag_generate_offload)
2350 lto_streamer_hooks_init ();
2352 /* Don't run the IPA passes if there was any error or sorry messages. */
2353 if (!seen_error ())
2354 ipa_passes ();
2356 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2357 if (seen_error ()
2358 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
2360 timevar_pop (TV_CGRAPHOPT);
2361 return;
2364 global_info_ready = true;
2365 if (dump_file)
2367 fprintf (dump_file, "Optimized ");
2368 symtab_node:: dump_table (dump_file);
2370 if (post_ipa_mem_report)
2372 fprintf (stderr, "Memory consumption after IPA\n");
2373 dump_memory_report (false);
2375 timevar_pop (TV_CGRAPHOPT);
2377 /* Output everything. */
2378 (*debug_hooks->assembly_start) ();
2379 if (!quiet_flag)
2380 fprintf (stderr, "Assembling functions:\n");
2381 #ifdef ENABLE_CHECKING
2382 symtab_node::verify_symtab_nodes ();
2383 #endif
2385 materialize_all_clones ();
2386 bitmap_obstack_initialize (NULL);
2387 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2388 bitmap_obstack_release (NULL);
2389 mark_functions_to_output ();
2391 /* When weakref support is missing, we autmatically translate all
2392 references to NODE to references to its ultimate alias target.
2393 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2394 TREE_CHAIN.
2396 Set up this mapping before we output any assembler but once we are sure
2397 that all symbol renaming is done.
2399 FIXME: All this uglyness can go away if we just do renaming at gimple
2400 level by physically rewritting the IL. At the moment we can only redirect
2401 calls, so we need infrastructure for renaming references as well. */
2402 #ifndef ASM_OUTPUT_WEAKREF
2403 symtab_node *node;
2405 FOR_EACH_SYMBOL (node)
2406 if (node->alias
2407 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2409 IDENTIFIER_TRANSPARENT_ALIAS
2410 (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2411 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2412 = (node->alias_target ? node->alias_target
2413 : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
2415 #endif
2417 state = EXPANSION;
2419 if (!flag_toplevel_reorder)
2420 output_in_order (false);
2421 else
2423 /* Output first asm statements and anything ordered. The process
2424 flag is cleared for these nodes, so we skip them later. */
2425 output_in_order (true);
2426 expand_all_functions ();
2427 output_variables ();
2430 process_new_functions ();
2431 state = FINISHED;
2432 output_weakrefs ();
2434 if (dump_file)
2436 fprintf (dump_file, "\nFinal ");
2437 symtab_node::dump_table (dump_file);
2439 #ifdef ENABLE_CHECKING
2440 symtab_node::verify_symtab_nodes ();
2441 /* Double check that all inline clones are gone and that all
2442 function bodies have been released from memory. */
2443 if (!seen_error ())
2445 cgraph_node *node;
2446 bool error_found = false;
2448 FOR_EACH_DEFINED_FUNCTION (node)
2449 if (node->global.inlined_to
2450 || gimple_has_body_p (node->decl))
2452 error_found = true;
2453 node->debug ();
2455 if (error_found)
2456 internal_error ("nodes with unreleased memory found");
2458 #endif
2462 /* Analyze the whole compilation unit once it is parsed completely. */
2464 void
2465 symbol_table::finalize_compilation_unit (void)
2467 timevar_push (TV_CGRAPH);
2469 /* If we're here there's no current function anymore. Some frontends
2470 are lazy in clearing these. */
2471 current_function_decl = NULL;
2472 set_cfun (NULL);
2474 /* Do not skip analyzing the functions if there were errors, we
2475 miss diagnostics for following functions otherwise. */
2477 /* Emit size functions we didn't inline. */
2478 finalize_size_functions ();
2480 /* Mark alias targets necessary and emit diagnostics. */
2481 handle_alias_pairs ();
2483 if (!quiet_flag)
2485 fprintf (stderr, "\nAnalyzing compilation unit\n");
2486 fflush (stderr);
2489 if (flag_dump_passes)
2490 dump_passes ();
2492 /* Gimplify and lower all functions, compute reachability and
2493 remove unreachable nodes. */
2494 analyze_functions (/*first_time=*/true);
2496 /* Mark alias targets necessary and emit diagnostics. */
2497 handle_alias_pairs ();
2499 /* Gimplify and lower thunks. */
2500 analyze_functions (/*first_time=*/false);
2502 /* Emit early debug for reachable functions, and by consequence,
2503 locally scoped symbols. */
2504 struct cgraph_node *cnode;
2505 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (cnode)
2506 (*debug_hooks->early_global_decl) (cnode->decl);
2508 /* Clean up anything that needs cleaning up after initial debug
2509 generation. */
2510 (*debug_hooks->early_finish) ();
2512 /* Finally drive the pass manager. */
2513 compile ();
2515 timevar_pop (TV_CGRAPH);
2518 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2519 within the same process. For use by toplev::finalize. */
2521 void
2522 cgraphunit_c_finalize (void)
2524 gcc_assert (cgraph_new_nodes.length () == 0);
2525 cgraph_new_nodes.truncate (0);
2527 vtable_entry_type = NULL;
2528 queued_nodes = &symtab_terminator;
2530 first_analyzed = NULL;
2531 first_analyzed_var = NULL;
2534 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2535 kind of wrapper method. */
2537 void
2538 cgraph_node::create_wrapper (cgraph_node *target)
2540 /* Preserve DECL_RESULT so we get right by reference flag. */
2541 tree decl_result = DECL_RESULT (decl);
2543 /* Remove the function's body but keep arguments to be reused
2544 for thunk. */
2545 release_body (true);
2546 reset ();
2548 DECL_UNINLINABLE (decl) = false;
2549 DECL_RESULT (decl) = decl_result;
2550 DECL_INITIAL (decl) = NULL;
2551 allocate_struct_function (decl, false);
2552 set_cfun (NULL);
2554 /* Turn alias into thunk and expand it into GIMPLE representation. */
2555 definition = true;
2557 memset (&thunk, 0, sizeof (cgraph_thunk_info));
2558 thunk.thunk_p = true;
2559 create_edge (target, NULL, count, CGRAPH_FREQ_BASE);
2561 tree arguments = DECL_ARGUMENTS (decl);
2563 while (arguments)
2565 TREE_ADDRESSABLE (arguments) = false;
2566 arguments = TREE_CHAIN (arguments);
2569 expand_thunk (false, true);
2571 /* Inline summary set-up. */
2572 analyze ();
2573 inline_analyze_function (this);
2576 #include "gt-cgraphunit.h"