PR c++/64359
[official-gcc.git] / gcc / cgraphunit.c
blobb0f78ef908ebd6fc476a6308f174a3cd1e582658
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"
228 #include "print-tree.h"
230 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
231 secondary queue used during optimization to accommodate passes that
232 may generate new functions that need to be optimized and expanded. */
233 vec<cgraph_node *> cgraph_new_nodes;
235 static void expand_all_functions (void);
236 static void mark_functions_to_output (void);
237 static void handle_alias_pairs (void);
239 /* Used for vtable lookup in thunk adjusting. */
240 static GTY (()) tree vtable_entry_type;
242 /* Determine if symbol declaration is needed. That is, visible to something
243 either outside this translation unit, something magic in the system
244 configury */
245 bool
246 symtab_node::needed_p (void)
248 /* Double check that no one output the function into assembly file
249 early. */
250 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)
251 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
253 if (!definition)
254 return false;
256 if (DECL_EXTERNAL (decl))
257 return false;
259 /* If the user told us it is used, then it must be so. */
260 if (force_output)
261 return true;
263 /* ABI forced symbols are needed when they are external. */
264 if (forced_by_abi && TREE_PUBLIC (decl))
265 return true;
267 /* Keep constructors, destructors and virtual functions. */
268 if (TREE_CODE (decl) == FUNCTION_DECL
269 && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)))
270 return true;
272 /* Externally visible variables must be output. The exception is
273 COMDAT variables that must be output only when they are needed. */
274 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
275 return true;
277 return false;
280 /* Head and terminator of the queue of nodes to be processed while building
281 callgraph. */
283 static symtab_node symtab_terminator;
284 static symtab_node *queued_nodes = &symtab_terminator;
286 /* Add NODE to queue starting at QUEUED_NODES.
287 The queue is linked via AUX pointers and terminated by pointer to 1. */
289 static void
290 enqueue_node (symtab_node *node)
292 if (node->aux)
293 return;
294 gcc_checking_assert (queued_nodes);
295 node->aux = queued_nodes;
296 queued_nodes = node;
299 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
300 functions into callgraph in a way so they look like ordinary reachable
301 functions inserted into callgraph already at construction time. */
303 void
304 symbol_table::process_new_functions (void)
306 tree fndecl;
308 if (!cgraph_new_nodes.exists ())
309 return;
311 handle_alias_pairs ();
312 /* Note that this queue may grow as its being processed, as the new
313 functions may generate new ones. */
314 for (unsigned i = 0; i < cgraph_new_nodes.length (); i++)
316 cgraph_node *node = cgraph_new_nodes[i];
317 fndecl = node->decl;
318 switch (state)
320 case CONSTRUCTION:
321 /* At construction time we just need to finalize function and move
322 it into reachable functions list. */
324 cgraph_node::finalize_function (fndecl, false);
325 call_cgraph_insertion_hooks (node);
326 enqueue_node (node);
327 break;
329 case IPA:
330 case IPA_SSA:
331 case IPA_SSA_AFTER_INLINING:
332 /* When IPA optimization already started, do all essential
333 transformations that has been already performed on the whole
334 cgraph but not on this function. */
336 gimple_register_cfg_hooks ();
337 if (!node->analyzed)
338 node->analyze ();
339 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
340 if ((state == IPA_SSA || state == IPA_SSA_AFTER_INLINING)
341 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
342 g->get_passes ()->execute_early_local_passes ();
343 else if (inline_summary_vec != NULL)
344 compute_inline_parameters (node, true);
345 free_dominance_info (CDI_POST_DOMINATORS);
346 free_dominance_info (CDI_DOMINATORS);
347 pop_cfun ();
348 call_cgraph_insertion_hooks (node);
349 break;
351 case EXPANSION:
352 /* Functions created during expansion shall be compiled
353 directly. */
354 node->process = 0;
355 call_cgraph_insertion_hooks (node);
356 node->expand ();
357 break;
359 default:
360 gcc_unreachable ();
361 break;
365 cgraph_new_nodes.release ();
368 /* As an GCC extension we allow redefinition of the function. The
369 semantics when both copies of bodies differ is not well defined.
370 We replace the old body with new body so in unit at a time mode
371 we always use new body, while in normal mode we may end up with
372 old body inlined into some functions and new body expanded and
373 inlined in others.
375 ??? It may make more sense to use one body for inlining and other
376 body for expanding the function but this is difficult to do. */
378 void
379 cgraph_node::reset (void)
381 /* If process is set, then we have already begun whole-unit analysis.
382 This is *not* testing for whether we've already emitted the function.
383 That case can be sort-of legitimately seen with real function redefinition
384 errors. I would argue that the front end should never present us with
385 such a case, but don't enforce that for now. */
386 gcc_assert (!process);
388 /* Reset our data structures so we can analyze the function again. */
389 memset (&local, 0, sizeof (local));
390 memset (&global, 0, sizeof (global));
391 memset (&rtl, 0, sizeof (rtl));
392 analyzed = false;
393 definition = false;
394 alias = false;
395 weakref = false;
396 cpp_implicit_alias = false;
398 remove_callees ();
399 remove_all_references ();
402 /* Return true when there are references to the node. */
404 bool
405 symtab_node::referred_to_p (void)
407 ipa_ref *ref = NULL;
409 /* See if there are any references at all. */
410 if (iterate_referring (0, ref))
411 return true;
412 /* For functions check also calls. */
413 cgraph_node *cn = dyn_cast <cgraph_node *> (this);
414 if (cn && cn->callers)
415 return true;
416 return false;
419 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
420 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
421 the garbage collector run at the moment. We would need to either create
422 a new GC context, or just not compile right now. */
424 void
425 cgraph_node::finalize_function (tree decl, bool no_collect)
427 cgraph_node *node = cgraph_node::get_create (decl);
429 if (node->definition)
431 /* Nested functions should only be defined once. */
432 gcc_assert (!DECL_CONTEXT (decl)
433 || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL);
434 node->reset ();
435 node->local.redefined_extern_inline = true;
438 notice_global_symbol (decl);
439 node->definition = true;
440 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
442 /* With -fkeep-inline-functions we are keeping all inline functions except
443 for extern inline ones. */
444 if (flag_keep_inline_functions
445 && DECL_DECLARED_INLINE_P (decl)
446 && !DECL_EXTERNAL (decl)
447 && !DECL_DISREGARD_INLINE_LIMITS (decl))
448 node->force_output = 1;
450 /* When not optimizing, also output the static functions. (see
451 PR24561), but don't do so for always_inline functions, functions
452 declared inline and nested functions. These were optimized out
453 in the original implementation and it is unclear whether we want
454 to change the behavior here. */
455 if ((!opt_for_fn (decl, optimize)
456 && !node->cpp_implicit_alias
457 && !DECL_DISREGARD_INLINE_LIMITS (decl)
458 && !DECL_DECLARED_INLINE_P (decl)
459 && !(DECL_CONTEXT (decl)
460 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
461 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
462 node->force_output = 1;
464 /* If we've not yet emitted decl, tell the debug info about it. */
465 if (!TREE_ASM_WRITTEN (decl))
466 (*debug_hooks->deferred_inline_function) (decl);
468 /* Possibly warn about unused parameters. */
469 if (warn_unused_parameter)
470 do_warn_unused_parameter (decl);
472 if (!no_collect)
473 ggc_collect ();
475 if (symtab->state == CONSTRUCTION
476 && (node->needed_p () || node->referred_to_p ()))
477 enqueue_node (node);
480 /* Add the function FNDECL to the call graph.
481 Unlike finalize_function, this function is intended to be used
482 by middle end and allows insertion of new function at arbitrary point
483 of compilation. The function can be either in high, low or SSA form
484 GIMPLE.
486 The function is assumed to be reachable and have address taken (so no
487 API breaking optimizations are performed on it).
489 Main work done by this function is to enqueue the function for later
490 processing to avoid need the passes to be re-entrant. */
492 void
493 cgraph_node::add_new_function (tree fndecl, bool lowered)
495 gcc::pass_manager *passes = g->get_passes ();
496 cgraph_node *node;
497 switch (symtab->state)
499 case PARSING:
500 cgraph_node::finalize_function (fndecl, false);
501 break;
502 case CONSTRUCTION:
503 /* Just enqueue function to be processed at nearest occurrence. */
504 node = cgraph_node::get_create (fndecl);
505 if (lowered)
506 node->lowered = true;
507 cgraph_new_nodes.safe_push (node);
508 break;
510 case IPA:
511 case IPA_SSA:
512 case IPA_SSA_AFTER_INLINING:
513 case EXPANSION:
514 /* Bring the function into finalized state and enqueue for later
515 analyzing and compilation. */
516 node = cgraph_node::get_create (fndecl);
517 node->local.local = false;
518 node->definition = true;
519 node->force_output = true;
520 if (!lowered && symtab->state == EXPANSION)
522 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
523 gimple_register_cfg_hooks ();
524 bitmap_obstack_initialize (NULL);
525 execute_pass_list (cfun, passes->all_lowering_passes);
526 passes->execute_early_local_passes ();
527 bitmap_obstack_release (NULL);
528 pop_cfun ();
530 lowered = true;
532 if (lowered)
533 node->lowered = true;
534 cgraph_new_nodes.safe_push (node);
535 break;
537 case FINISHED:
538 /* At the very end of compilation we have to do all the work up
539 to expansion. */
540 node = cgraph_node::create (fndecl);
541 if (lowered)
542 node->lowered = true;
543 node->definition = true;
544 node->analyze ();
545 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
546 gimple_register_cfg_hooks ();
547 bitmap_obstack_initialize (NULL);
548 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
549 g->get_passes ()->execute_early_local_passes ();
550 bitmap_obstack_release (NULL);
551 pop_cfun ();
552 node->expand ();
553 break;
555 default:
556 gcc_unreachable ();
559 /* Set a personality if required and we already passed EH lowering. */
560 if (lowered
561 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
562 == eh_personality_lang))
563 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
566 /* Analyze the function scheduled to be output. */
567 void
568 cgraph_node::analyze (void)
570 tree decl = this->decl;
571 location_t saved_loc = input_location;
572 input_location = DECL_SOURCE_LOCATION (decl);
574 if (thunk.thunk_p)
576 create_edge (cgraph_node::get (thunk.alias),
577 NULL, 0, CGRAPH_FREQ_BASE);
578 if (!expand_thunk (false, false))
580 thunk.alias = NULL;
581 return;
583 thunk.alias = NULL;
585 if (alias)
586 resolve_alias (cgraph_node::get (alias_target));
587 else if (dispatcher_function)
589 /* Generate the dispatcher body of multi-versioned functions. */
590 cgraph_function_version_info *dispatcher_version_info
591 = function_version ();
592 if (dispatcher_version_info != NULL
593 && (dispatcher_version_info->dispatcher_resolver
594 == NULL_TREE))
596 tree resolver = NULL_TREE;
597 gcc_assert (targetm.generate_version_dispatcher_body);
598 resolver = targetm.generate_version_dispatcher_body (this);
599 gcc_assert (resolver != NULL_TREE);
602 else
604 push_cfun (DECL_STRUCT_FUNCTION (decl));
606 assign_assembler_name_if_neeeded (decl);
608 /* Make sure to gimplify bodies only once. During analyzing a
609 function we lower it, which will require gimplified nested
610 functions, so we can end up here with an already gimplified
611 body. */
612 if (!gimple_has_body_p (decl))
613 gimplify_function_tree (decl);
614 dump_function (TDI_generic, decl);
616 /* Lower the function. */
617 if (!lowered)
619 if (nested)
620 lower_nested_functions (decl);
621 gcc_assert (!nested);
623 gimple_register_cfg_hooks ();
624 bitmap_obstack_initialize (NULL);
625 execute_pass_list (cfun, g->get_passes ()->all_lowering_passes);
626 free_dominance_info (CDI_POST_DOMINATORS);
627 free_dominance_info (CDI_DOMINATORS);
628 compact_blocks ();
629 bitmap_obstack_release (NULL);
630 lowered = true;
633 pop_cfun ();
635 analyzed = true;
637 input_location = saved_loc;
640 /* C++ frontend produce same body aliases all over the place, even before PCH
641 gets streamed out. It relies on us linking the aliases with their function
642 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
643 first produce aliases without links, but once C++ FE is sure he won't sream
644 PCH we build the links via this function. */
646 void
647 symbol_table::process_same_body_aliases (void)
649 symtab_node *node;
650 FOR_EACH_SYMBOL (node)
651 if (node->cpp_implicit_alias && !node->analyzed)
652 node->resolve_alias
653 (TREE_CODE (node->alias_target) == VAR_DECL
654 ? (symtab_node *)varpool_node::get_create (node->alias_target)
655 : (symtab_node *)cgraph_node::get_create (node->alias_target));
656 cpp_implicit_aliases_done = true;
659 /* Process attributes common for vars and functions. */
661 static void
662 process_common_attributes (symtab_node *node, tree decl)
664 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
666 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
668 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
669 "%<weakref%> attribute should be accompanied with"
670 " an %<alias%> attribute");
671 DECL_WEAK (decl) = 0;
672 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
673 DECL_ATTRIBUTES (decl));
676 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
677 node->no_reorder = 1;
680 /* Look for externally_visible and used attributes and mark cgraph nodes
681 accordingly.
683 We cannot mark the nodes at the point the attributes are processed (in
684 handle_*_attribute) because the copy of the declarations available at that
685 point may not be canonical. For example, in:
687 void f();
688 void f() __attribute__((used));
690 the declaration we see in handle_used_attribute will be the second
691 declaration -- but the front end will subsequently merge that declaration
692 with the original declaration and discard the second declaration.
694 Furthermore, we can't mark these nodes in finalize_function because:
696 void f() {}
697 void f() __attribute__((externally_visible));
699 is valid.
701 So, we walk the nodes at the end of the translation unit, applying the
702 attributes at that point. */
704 static void
705 process_function_and_variable_attributes (cgraph_node *first,
706 varpool_node *first_var)
708 cgraph_node *node;
709 varpool_node *vnode;
711 for (node = symtab->first_function (); node != first;
712 node = symtab->next_function (node))
714 tree decl = node->decl;
715 if (DECL_PRESERVE_P (decl))
716 node->mark_force_output ();
717 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
719 if (! TREE_PUBLIC (node->decl))
720 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
721 "%<externally_visible%>"
722 " attribute have effect only on public objects");
724 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
725 && (node->definition && !node->alias))
727 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
728 "%<weakref%> attribute ignored"
729 " because function is defined");
730 DECL_WEAK (decl) = 0;
731 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
732 DECL_ATTRIBUTES (decl));
735 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
736 && !DECL_DECLARED_INLINE_P (decl)
737 /* redefining extern inline function makes it DECL_UNINLINABLE. */
738 && !DECL_UNINLINABLE (decl))
739 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
740 "always_inline function might not be inlinable");
742 process_common_attributes (node, decl);
744 for (vnode = symtab->first_variable (); vnode != first_var;
745 vnode = symtab->next_variable (vnode))
747 tree decl = vnode->decl;
748 if (DECL_EXTERNAL (decl)
749 && DECL_INITIAL (decl))
750 varpool_node::finalize_decl (decl);
751 if (DECL_PRESERVE_P (decl))
752 vnode->force_output = true;
753 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
755 if (! TREE_PUBLIC (vnode->decl))
756 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
757 "%<externally_visible%>"
758 " attribute have effect only on public objects");
760 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
761 && vnode->definition
762 && DECL_INITIAL (decl))
764 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
765 "%<weakref%> attribute ignored"
766 " because variable is initialized");
767 DECL_WEAK (decl) = 0;
768 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
769 DECL_ATTRIBUTES (decl));
771 process_common_attributes (vnode, decl);
775 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
776 middle end to output the variable to asm file, if needed or externally
777 visible. */
779 void
780 varpool_node::finalize_decl (tree decl)
782 varpool_node *node = varpool_node::get_create (decl);
784 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
786 if (node->definition)
787 return;
788 notice_global_symbol (decl);
789 node->definition = true;
790 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
791 /* Traditionally we do not eliminate static variables when not
792 optimizing and when not doing toplevel reoder. */
793 || node->no_reorder
794 || ((!flag_toplevel_reorder
795 && !DECL_COMDAT (node->decl)
796 && !DECL_ARTIFICIAL (node->decl))))
797 node->force_output = true;
799 if (symtab->state == CONSTRUCTION
800 && (node->needed_p () || node->referred_to_p ()))
801 enqueue_node (node);
802 if (symtab->state >= IPA_SSA)
803 node->analyze ();
804 /* Some frontends produce various interface variables after compilation
805 finished. */
806 if (symtab->state == FINISHED
807 || (!flag_toplevel_reorder
808 && symtab->state == EXPANSION))
809 node->assemble_decl ();
811 if (DECL_INITIAL (decl))
812 chkp_register_var_initializer (decl);
815 /* EDGE is an polymorphic call. Mark all possible targets as reachable
816 and if there is only one target, perform trivial devirtualization.
817 REACHABLE_CALL_TARGETS collects target lists we already walked to
818 avoid udplicate work. */
820 static void
821 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
822 cgraph_edge *edge)
824 unsigned int i;
825 void *cache_token;
826 bool final;
827 vec <cgraph_node *>targets
828 = possible_polymorphic_call_targets
829 (edge, &final, &cache_token);
831 if (!reachable_call_targets->add (cache_token))
833 if (symtab->dump_file)
834 dump_possible_polymorphic_call_targets
835 (symtab->dump_file, edge);
837 for (i = 0; i < targets.length (); i++)
839 /* Do not bother to mark virtual methods in anonymous namespace;
840 either we will find use of virtual table defining it, or it is
841 unused. */
842 if (targets[i]->definition
843 && TREE_CODE
844 (TREE_TYPE (targets[i]->decl))
845 == METHOD_TYPE
846 && !type_in_anonymous_namespace_p
847 (method_class_type
848 (TREE_TYPE (targets[i]->decl))))
849 enqueue_node (targets[i]);
853 /* Very trivial devirtualization; when the type is
854 final or anonymous (so we know all its derivation)
855 and there is only one possible virtual call target,
856 make the edge direct. */
857 if (final)
859 if (targets.length () <= 1 && dbg_cnt (devirt))
861 cgraph_node *target;
862 if (targets.length () == 1)
863 target = targets[0];
864 else
865 target = cgraph_node::create
866 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
868 if (symtab->dump_file)
870 fprintf (symtab->dump_file,
871 "Devirtualizing call: ");
872 print_gimple_stmt (symtab->dump_file,
873 edge->call_stmt, 0,
874 TDF_SLIM);
876 if (dump_enabled_p ())
878 location_t locus = gimple_location_safe (edge->call_stmt);
879 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
880 "devirtualizing call in %s to %s\n",
881 edge->caller->name (), target->name ());
884 edge->make_direct (target);
885 edge->redirect_call_stmt_to_callee ();
887 /* Call to __builtin_unreachable shouldn't be instrumented. */
888 if (!targets.length ())
889 gimple_call_set_with_bounds (edge->call_stmt, false);
891 if (symtab->dump_file)
893 fprintf (symtab->dump_file,
894 "Devirtualized as: ");
895 print_gimple_stmt (symtab->dump_file,
896 edge->call_stmt, 0,
897 TDF_SLIM);
904 /* Discover all functions and variables that are trivially needed, analyze
905 them as well as all functions and variables referred by them */
906 static cgraph_node *first_analyzed;
907 static varpool_node *first_analyzed_var;
909 static void
910 analyze_functions (void)
912 /* Keep track of already processed nodes when called multiple times for
913 intermodule optimization. */
914 cgraph_node *first_handled = first_analyzed;
915 varpool_node *first_handled_var = first_analyzed_var;
916 hash_set<void *> reachable_call_targets;
918 symtab_node *node;
919 symtab_node *next;
920 int i;
921 ipa_ref *ref;
922 bool changed = true;
923 location_t saved_loc = input_location;
925 bitmap_obstack_initialize (NULL);
926 symtab->state = CONSTRUCTION;
927 input_location = UNKNOWN_LOCATION;
929 /* Ugly, but the fixup can not happen at a time same body alias is created;
930 C++ FE is confused about the COMDAT groups being right. */
931 if (symtab->cpp_implicit_aliases_done)
932 FOR_EACH_SYMBOL (node)
933 if (node->cpp_implicit_alias)
934 node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
935 build_type_inheritance_graph ();
937 /* Analysis adds static variables that in turn adds references to new functions.
938 So we need to iterate the process until it stabilize. */
939 while (changed)
941 changed = false;
942 process_function_and_variable_attributes (first_analyzed,
943 first_analyzed_var);
945 /* First identify the trivially needed symbols. */
946 for (node = symtab->first_symbol ();
947 node != first_analyzed
948 && node != first_analyzed_var; node = node->next)
950 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
951 node->get_comdat_group_id ();
952 if (node->needed_p ())
954 enqueue_node (node);
955 if (!changed && symtab->dump_file)
956 fprintf (symtab->dump_file, "Trivially needed symbols:");
957 changed = true;
958 if (symtab->dump_file)
959 fprintf (symtab->dump_file, " %s", node->asm_name ());
960 if (!changed && symtab->dump_file)
961 fprintf (symtab->dump_file, "\n");
963 if (node == first_analyzed
964 || node == first_analyzed_var)
965 break;
967 symtab->process_new_functions ();
968 first_analyzed_var = symtab->first_variable ();
969 first_analyzed = symtab->first_function ();
971 if (changed && symtab->dump_file)
972 fprintf (symtab->dump_file, "\n");
974 /* Lower representation, build callgraph edges and references for all trivially
975 needed symbols and all symbols referred by them. */
976 while (queued_nodes != &symtab_terminator)
978 changed = true;
979 node = queued_nodes;
980 queued_nodes = (symtab_node *)queued_nodes->aux;
981 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
982 if (cnode && cnode->definition)
984 cgraph_edge *edge;
985 tree decl = cnode->decl;
987 /* ??? It is possible to create extern inline function
988 and later using weak alias attribute to kill its body.
989 See gcc.c-torture/compile/20011119-1.c */
990 if (!DECL_STRUCT_FUNCTION (decl)
991 && !cnode->alias
992 && !cnode->thunk.thunk_p
993 && !cnode->dispatcher_function)
995 cnode->reset ();
996 cnode->local.redefined_extern_inline = true;
997 continue;
1000 if (!cnode->analyzed)
1001 cnode->analyze ();
1003 for (edge = cnode->callees; edge; edge = edge->next_callee)
1004 if (edge->callee->definition
1005 && (!DECL_EXTERNAL (edge->callee->decl)
1006 /* When not optimizing, do not try to analyze extern
1007 inline functions. Doing so is pointless. */
1008 || opt_for_fn (edge->callee->decl, optimize)
1009 /* Weakrefs needs to be preserved. */
1010 || edge->callee->alias
1011 /* always_inline functions are inlined aven at -O0. */
1012 || lookup_attribute
1013 ("always_inline",
1014 DECL_ATTRIBUTES (edge->callee->decl))
1015 /* Multiversioned functions needs the dispatcher to
1016 be produced locally even for extern functions. */
1017 || edge->callee->function_version ()))
1018 enqueue_node (edge->callee);
1019 if (opt_for_fn (cnode->decl, optimize)
1020 && opt_for_fn (cnode->decl, flag_devirtualize))
1022 cgraph_edge *next;
1024 for (edge = cnode->indirect_calls; edge; edge = next)
1026 next = edge->next_callee;
1027 if (edge->indirect_info->polymorphic)
1028 walk_polymorphic_call_targets (&reachable_call_targets,
1029 edge);
1033 /* If decl is a clone of an abstract function,
1034 mark that abstract function so that we don't release its body.
1035 The DECL_INITIAL() of that abstract function declaration
1036 will be later needed to output debug info. */
1037 if (DECL_ABSTRACT_ORIGIN (decl))
1039 cgraph_node *origin_node
1040 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl));
1041 origin_node->used_as_abstract_origin = true;
1044 else
1046 varpool_node *vnode = dyn_cast <varpool_node *> (node);
1047 if (vnode && vnode->definition && !vnode->analyzed)
1048 vnode->analyze ();
1051 if (node->same_comdat_group)
1053 symtab_node *next;
1054 for (next = node->same_comdat_group;
1055 next != node;
1056 next = next->same_comdat_group)
1057 if (!next->comdat_local_p ())
1058 enqueue_node (next);
1060 for (i = 0; node->iterate_reference (i, ref); i++)
1061 if (ref->referred->definition
1062 && (!DECL_EXTERNAL (ref->referred->decl)
1063 || ((TREE_CODE (ref->referred->decl) != FUNCTION_DECL
1064 && optimize)
1065 || (TREE_CODE (ref->referred->decl) == FUNCTION_DECL
1066 && opt_for_fn (ref->referred->decl, optimize))
1067 || node->alias
1068 || ref->referred->alias)))
1069 enqueue_node (ref->referred);
1070 symtab->process_new_functions ();
1073 update_type_inheritance_graph ();
1075 /* Collect entry points to the unit. */
1076 if (symtab->dump_file)
1078 fprintf (symtab->dump_file, "\n\nInitial ");
1079 symtab_node::dump_table (symtab->dump_file);
1082 if (symtab->dump_file)
1083 fprintf (symtab->dump_file, "\nRemoving unused symbols:");
1085 for (node = symtab->first_symbol ();
1086 node != first_handled
1087 && node != first_handled_var; node = next)
1089 next = node->next;
1090 if (!node->aux && !node->referred_to_p ())
1092 if (symtab->dump_file)
1093 fprintf (symtab->dump_file, " %s", node->name ());
1094 node->remove ();
1095 continue;
1097 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1099 tree decl = node->decl;
1101 if (cnode->definition && !gimple_has_body_p (decl)
1102 && !cnode->alias
1103 && !cnode->thunk.thunk_p)
1104 cnode->reset ();
1106 gcc_assert (!cnode->definition || cnode->thunk.thunk_p
1107 || cnode->alias
1108 || gimple_has_body_p (decl));
1109 gcc_assert (cnode->analyzed == cnode->definition);
1111 node->aux = NULL;
1113 for (;node; node = node->next)
1114 node->aux = NULL;
1115 first_analyzed = symtab->first_function ();
1116 first_analyzed_var = symtab->first_variable ();
1117 if (symtab->dump_file)
1119 fprintf (symtab->dump_file, "\n\nReclaimed ");
1120 symtab_node::dump_table (symtab->dump_file);
1122 bitmap_obstack_release (NULL);
1123 ggc_collect ();
1124 /* Initialize assembler name hash, in particular we want to trigger C++
1125 mangling and same body alias creation before we free DECL_ARGUMENTS
1126 used by it. */
1127 if (!seen_error ())
1128 symtab->symtab_initialize_asm_name_hash ();
1130 input_location = saved_loc;
1133 /* Translate the ugly representation of aliases as alias pairs into nice
1134 representation in callgraph. We don't handle all cases yet,
1135 unfortunately. */
1137 static void
1138 handle_alias_pairs (void)
1140 alias_pair *p;
1141 unsigned i;
1143 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1145 symtab_node *target_node = symtab_node::get_for_asmname (p->target);
1147 /* Weakrefs with target not defined in current unit are easy to handle:
1148 they behave just as external variables except we need to note the
1149 alias flag to later output the weakref pseudo op into asm file. */
1150 if (!target_node
1151 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1153 symtab_node *node = symtab_node::get (p->decl);
1154 if (node)
1156 node->alias_target = p->target;
1157 node->weakref = true;
1158 node->alias = true;
1160 alias_pairs->unordered_remove (i);
1161 continue;
1163 else if (!target_node)
1165 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1166 symtab_node *node = symtab_node::get (p->decl);
1167 if (node)
1168 node->alias = false;
1169 alias_pairs->unordered_remove (i);
1170 continue;
1173 if (DECL_EXTERNAL (target_node->decl)
1174 /* We use local aliases for C++ thunks to force the tailcall
1175 to bind locally. This is a hack - to keep it working do
1176 the following (which is not strictly correct). */
1177 && (TREE_CODE (target_node->decl) != FUNCTION_DECL
1178 || ! DECL_VIRTUAL_P (target_node->decl))
1179 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1181 error ("%q+D aliased to external symbol %qE",
1182 p->decl, p->target);
1185 if (TREE_CODE (p->decl) == FUNCTION_DECL
1186 && target_node && is_a <cgraph_node *> (target_node))
1188 cgraph_node *src_node = cgraph_node::get (p->decl);
1189 if (src_node && src_node->definition)
1190 src_node->reset ();
1191 cgraph_node::create_alias (p->decl, target_node->decl);
1192 alias_pairs->unordered_remove (i);
1194 else if (TREE_CODE (p->decl) == VAR_DECL
1195 && target_node && is_a <varpool_node *> (target_node))
1197 varpool_node::create_alias (p->decl, target_node->decl);
1198 alias_pairs->unordered_remove (i);
1200 else
1202 error ("%q+D alias in between function and variable is not supported",
1203 p->decl);
1204 warning (0, "%q+D aliased declaration",
1205 target_node->decl);
1206 alias_pairs->unordered_remove (i);
1209 vec_free (alias_pairs);
1213 /* Figure out what functions we want to assemble. */
1215 static void
1216 mark_functions_to_output (void)
1218 cgraph_node *node;
1219 #ifdef ENABLE_CHECKING
1220 bool check_same_comdat_groups = false;
1222 FOR_EACH_FUNCTION (node)
1223 gcc_assert (!node->process);
1224 #endif
1226 FOR_EACH_FUNCTION (node)
1228 tree decl = node->decl;
1230 gcc_assert (!node->process || node->same_comdat_group);
1231 if (node->process)
1232 continue;
1234 /* We need to output all local functions that are used and not
1235 always inlined, as well as those that are reachable from
1236 outside the current compilation unit. */
1237 if (node->analyzed
1238 && !node->thunk.thunk_p
1239 && !node->alias
1240 && !node->global.inlined_to
1241 && !TREE_ASM_WRITTEN (decl)
1242 && !DECL_EXTERNAL (decl))
1244 node->process = 1;
1245 if (node->same_comdat_group)
1247 cgraph_node *next;
1248 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
1249 next != node;
1250 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
1251 if (!next->thunk.thunk_p && !next->alias
1252 && !next->comdat_local_p ())
1253 next->process = 1;
1256 else if (node->same_comdat_group)
1258 #ifdef ENABLE_CHECKING
1259 check_same_comdat_groups = true;
1260 #endif
1262 else
1264 /* We should've reclaimed all functions that are not needed. */
1265 #ifdef ENABLE_CHECKING
1266 if (!node->global.inlined_to
1267 && gimple_has_body_p (decl)
1268 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1269 are inside partition, we can end up not removing the body since we no longer
1270 have analyzed node pointing to it. */
1271 && !node->in_other_partition
1272 && !node->alias
1273 && !node->clones
1274 && !DECL_EXTERNAL (decl))
1276 node->debug ();
1277 internal_error ("failed to reclaim unneeded function");
1279 #endif
1280 gcc_assert (node->global.inlined_to
1281 || !gimple_has_body_p (decl)
1282 || node->in_other_partition
1283 || node->clones
1284 || DECL_ARTIFICIAL (decl)
1285 || DECL_EXTERNAL (decl));
1290 #ifdef ENABLE_CHECKING
1291 if (check_same_comdat_groups)
1292 FOR_EACH_FUNCTION (node)
1293 if (node->same_comdat_group && !node->process)
1295 tree decl = node->decl;
1296 if (!node->global.inlined_to
1297 && gimple_has_body_p (decl)
1298 /* FIXME: in an ltrans unit when the offline copy is outside a
1299 partition but inline copies are inside a partition, we can
1300 end up not removing the body since we no longer have an
1301 analyzed node pointing to it. */
1302 && !node->in_other_partition
1303 && !node->clones
1304 && !DECL_EXTERNAL (decl))
1306 node->debug ();
1307 internal_error ("failed to reclaim unneeded function in same "
1308 "comdat group");
1311 #endif
1314 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1315 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1317 Set current_function_decl and cfun to newly constructed empty function body.
1318 return basic block in the function body. */
1320 basic_block
1321 init_lowered_empty_function (tree decl, bool in_ssa)
1323 basic_block bb;
1325 current_function_decl = decl;
1326 allocate_struct_function (decl, false);
1327 gimple_register_cfg_hooks ();
1328 init_empty_tree_cfg ();
1330 if (in_ssa)
1332 init_tree_ssa (cfun);
1333 init_ssa_operands (cfun);
1334 cfun->gimple_df->in_ssa_p = true;
1335 cfun->curr_properties |= PROP_ssa;
1338 DECL_INITIAL (decl) = make_node (BLOCK);
1340 DECL_SAVED_TREE (decl) = error_mark_node;
1341 cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1342 | PROP_cfg | PROP_loops);
1344 set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ());
1345 init_loops_structure (cfun, loops_for_fn (cfun), 1);
1346 loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1348 /* Create BB for body of the function and connect it properly. */
1349 bb = create_basic_block (NULL, (void *) 0, ENTRY_BLOCK_PTR_FOR_FN (cfun));
1350 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU);
1351 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1352 add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
1354 return bb;
1357 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1358 offset indicated by VIRTUAL_OFFSET, if that is
1359 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1360 zero for a result adjusting thunk. */
1362 static tree
1363 thunk_adjust (gimple_stmt_iterator * bsi,
1364 tree ptr, bool this_adjusting,
1365 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1367 gassign *stmt;
1368 tree ret;
1370 if (this_adjusting
1371 && fixed_offset != 0)
1373 stmt = gimple_build_assign
1374 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1375 ptr,
1376 fixed_offset));
1377 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1380 /* If there's a virtual offset, look up that value in the vtable and
1381 adjust the pointer again. */
1382 if (virtual_offset)
1384 tree vtabletmp;
1385 tree vtabletmp2;
1386 tree vtabletmp3;
1388 if (!vtable_entry_type)
1390 tree vfunc_type = make_node (FUNCTION_TYPE);
1391 TREE_TYPE (vfunc_type) = integer_type_node;
1392 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1393 layout_type (vfunc_type);
1395 vtable_entry_type = build_pointer_type (vfunc_type);
1398 vtabletmp =
1399 create_tmp_reg (build_pointer_type
1400 (build_pointer_type (vtable_entry_type)), "vptr");
1402 /* The vptr is always at offset zero in the object. */
1403 stmt = gimple_build_assign (vtabletmp,
1404 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1405 ptr));
1406 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1408 /* Form the vtable address. */
1409 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
1410 "vtableaddr");
1411 stmt = gimple_build_assign (vtabletmp2,
1412 build_simple_mem_ref (vtabletmp));
1413 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1415 /* Find the entry with the vcall offset. */
1416 stmt = gimple_build_assign (vtabletmp2,
1417 fold_build_pointer_plus_loc (input_location,
1418 vtabletmp2,
1419 virtual_offset));
1420 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1422 /* Get the offset itself. */
1423 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1424 "vcalloffset");
1425 stmt = gimple_build_assign (vtabletmp3,
1426 build_simple_mem_ref (vtabletmp2));
1427 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1429 /* Adjust the `this' pointer. */
1430 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1431 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1432 GSI_CONTINUE_LINKING);
1435 if (!this_adjusting
1436 && fixed_offset != 0)
1437 /* Adjust the pointer by the constant. */
1439 tree ptrtmp;
1441 if (TREE_CODE (ptr) == VAR_DECL)
1442 ptrtmp = ptr;
1443 else
1445 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
1446 stmt = gimple_build_assign (ptrtmp, ptr);
1447 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1449 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1450 ptrtmp, fixed_offset);
1453 /* Emit the statement and gimplify the adjustment expression. */
1454 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
1455 stmt = gimple_build_assign (ret, ptr);
1456 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1458 return ret;
1461 /* Expand thunk NODE to gimple if possible.
1462 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1463 no assembler is produced.
1464 When OUTPUT_ASM_THUNK is true, also produce assembler for
1465 thunks that are not lowered. */
1467 bool
1468 cgraph_node::expand_thunk (bool output_asm_thunks, bool force_gimple_thunk)
1470 bool this_adjusting = thunk.this_adjusting;
1471 HOST_WIDE_INT fixed_offset = thunk.fixed_offset;
1472 HOST_WIDE_INT virtual_value = thunk.virtual_value;
1473 tree virtual_offset = NULL;
1474 tree alias = callees->callee->decl;
1475 tree thunk_fndecl = decl;
1476 tree a;
1479 if (!force_gimple_thunk && this_adjusting
1480 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1481 virtual_value, alias))
1483 const char *fnname;
1484 tree fn_block;
1485 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1487 if (!output_asm_thunks)
1489 analyzed = true;
1490 return false;
1493 if (in_lto_p)
1494 get_untransformed_body ();
1495 a = DECL_ARGUMENTS (thunk_fndecl);
1497 current_function_decl = thunk_fndecl;
1499 /* Ensure thunks are emitted in their correct sections. */
1500 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1502 DECL_RESULT (thunk_fndecl)
1503 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1504 RESULT_DECL, 0, restype);
1505 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1506 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
1508 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1509 create one. */
1510 fn_block = make_node (BLOCK);
1511 BLOCK_VARS (fn_block) = a;
1512 DECL_INITIAL (thunk_fndecl) = fn_block;
1513 init_function_start (thunk_fndecl);
1514 cfun->is_thunk = 1;
1515 insn_locations_init ();
1516 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1517 prologue_location = curr_insn_location ();
1518 assemble_start_function (thunk_fndecl, fnname);
1520 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1521 fixed_offset, virtual_value, alias);
1523 assemble_end_function (thunk_fndecl, fnname);
1524 insn_locations_finalize ();
1525 init_insn_lengths ();
1526 free_after_compilation (cfun);
1527 set_cfun (NULL);
1528 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1529 thunk.thunk_p = false;
1530 analyzed = false;
1532 else
1534 tree restype;
1535 basic_block bb, then_bb, else_bb, return_bb;
1536 gimple_stmt_iterator bsi;
1537 int nargs = 0;
1538 tree arg;
1539 int i;
1540 tree resdecl;
1541 tree restmp = NULL;
1543 gcall *call;
1544 greturn *ret;
1546 if (in_lto_p)
1547 get_untransformed_body ();
1548 a = DECL_ARGUMENTS (thunk_fndecl);
1550 current_function_decl = thunk_fndecl;
1552 /* Ensure thunks are emitted in their correct sections. */
1553 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1555 DECL_IGNORED_P (thunk_fndecl) = 1;
1556 bitmap_obstack_initialize (NULL);
1558 if (thunk.virtual_offset_p)
1559 virtual_offset = size_int (virtual_value);
1561 /* Build the return declaration for the function. */
1562 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1563 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1565 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1566 DECL_ARTIFICIAL (resdecl) = 1;
1567 DECL_IGNORED_P (resdecl) = 1;
1568 DECL_RESULT (thunk_fndecl) = resdecl;
1569 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1571 else
1572 resdecl = DECL_RESULT (thunk_fndecl);
1574 bb = then_bb = else_bb = return_bb = init_lowered_empty_function (thunk_fndecl, true);
1576 bsi = gsi_start_bb (bb);
1578 /* Build call to the function being thunked. */
1579 if (!VOID_TYPE_P (restype))
1581 if (DECL_BY_REFERENCE (resdecl))
1583 restmp = gimple_fold_indirect_ref (resdecl);
1584 if (!restmp)
1585 restmp = build2 (MEM_REF,
1586 TREE_TYPE (TREE_TYPE (DECL_RESULT (alias))),
1587 resdecl,
1588 build_int_cst (TREE_TYPE
1589 (DECL_RESULT (alias)), 0));
1591 else if (!is_gimple_reg_type (restype))
1593 restmp = resdecl;
1595 if (TREE_CODE (restmp) == VAR_DECL)
1596 add_local_decl (cfun, restmp);
1597 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1599 else
1600 restmp = create_tmp_reg (restype, "retval");
1603 for (arg = a; arg; arg = DECL_CHAIN (arg))
1604 nargs++;
1605 auto_vec<tree> vargs (nargs);
1606 if (this_adjusting)
1607 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
1608 virtual_offset));
1609 else if (nargs)
1610 vargs.quick_push (a);
1612 if (nargs)
1613 for (i = 1, arg = DECL_CHAIN (a); i < nargs; i++, arg = DECL_CHAIN (arg))
1615 tree tmp = arg;
1616 if (!is_gimple_val (arg))
1618 tmp = create_tmp_reg (TYPE_MAIN_VARIANT
1619 (TREE_TYPE (arg)), "arg");
1620 gimple stmt = gimple_build_assign (tmp, arg);
1621 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1623 vargs.quick_push (tmp);
1625 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1626 callees->call_stmt = call;
1627 gimple_call_set_from_thunk (call, true);
1628 gimple_call_set_with_bounds (call, instrumentation_clone);
1629 if (restmp)
1631 gimple_call_set_lhs (call, restmp);
1632 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp),
1633 TREE_TYPE (TREE_TYPE (alias))));
1635 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
1636 if (!(gimple_call_flags (call) & ECF_NORETURN))
1638 if (restmp && !this_adjusting
1639 && (fixed_offset || virtual_offset))
1641 tree true_label = NULL_TREE;
1643 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1645 gimple stmt;
1646 /* If the return type is a pointer, we need to
1647 protect against NULL. We know there will be an
1648 adjustment, because that's why we're emitting a
1649 thunk. */
1650 then_bb = create_basic_block (NULL, (void *) 0, bb);
1651 return_bb = create_basic_block (NULL, (void *) 0, then_bb);
1652 else_bb = create_basic_block (NULL, (void *) 0, else_bb);
1653 add_bb_to_loop (then_bb, bb->loop_father);
1654 add_bb_to_loop (return_bb, bb->loop_father);
1655 add_bb_to_loop (else_bb, bb->loop_father);
1656 remove_edge (single_succ_edge (bb));
1657 true_label = gimple_block_label (then_bb);
1658 stmt = gimple_build_cond (NE_EXPR, restmp,
1659 build_zero_cst (TREE_TYPE (restmp)),
1660 NULL_TREE, NULL_TREE);
1661 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1662 make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1663 make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1664 make_edge (return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1665 make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1666 make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1667 bsi = gsi_last_bb (then_bb);
1670 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1671 fixed_offset, virtual_offset);
1672 if (true_label)
1674 gimple stmt;
1675 bsi = gsi_last_bb (else_bb);
1676 stmt = gimple_build_assign (restmp,
1677 build_zero_cst (TREE_TYPE (restmp)));
1678 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1679 bsi = gsi_last_bb (return_bb);
1682 else
1683 gimple_call_set_tail (call, true);
1685 /* Build return value. */
1686 if (!DECL_BY_REFERENCE (resdecl))
1687 ret = gimple_build_return (restmp);
1688 else
1689 ret = gimple_build_return (resdecl);
1691 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1693 else
1695 gimple_call_set_tail (call, true);
1696 remove_edge (single_succ_edge (bb));
1699 cfun->gimple_df->in_ssa_p = true;
1700 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
1701 TREE_ASM_WRITTEN (thunk_fndecl) = false;
1702 delete_unreachable_blocks ();
1703 update_ssa (TODO_update_ssa);
1704 #ifdef ENABLE_CHECKING
1705 verify_flow_info ();
1706 #endif
1707 free_dominance_info (CDI_DOMINATORS);
1709 /* Since we want to emit the thunk, we explicitly mark its name as
1710 referenced. */
1711 thunk.thunk_p = false;
1712 lowered = true;
1713 bitmap_obstack_release (NULL);
1715 current_function_decl = NULL;
1716 set_cfun (NULL);
1717 return true;
1720 /* Assemble thunks and aliases associated to node. */
1722 void
1723 cgraph_node::assemble_thunks_and_aliases (void)
1725 cgraph_edge *e;
1726 ipa_ref *ref;
1728 for (e = callers; e;)
1729 if (e->caller->thunk.thunk_p
1730 && !e->caller->thunk.add_pointer_bounds_args)
1732 cgraph_node *thunk = e->caller;
1734 e = e->next_caller;
1735 thunk->expand_thunk (true, false);
1736 thunk->assemble_thunks_and_aliases ();
1738 else
1739 e = e->next_caller;
1741 FOR_EACH_ALIAS (this, ref)
1743 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1744 bool saved_written = TREE_ASM_WRITTEN (decl);
1746 /* Force assemble_alias to really output the alias this time instead
1747 of buffering it in same alias pairs. */
1748 TREE_ASM_WRITTEN (decl) = 1;
1749 do_assemble_alias (alias->decl,
1750 DECL_ASSEMBLER_NAME (decl));
1751 alias->assemble_thunks_and_aliases ();
1752 TREE_ASM_WRITTEN (decl) = saved_written;
1756 /* Expand function specified by node. */
1758 void
1759 cgraph_node::expand (void)
1761 location_t saved_loc;
1763 /* We ought to not compile any inline clones. */
1764 gcc_assert (!global.inlined_to);
1766 announce_function (decl);
1767 process = 0;
1768 gcc_assert (lowered);
1769 get_untransformed_body ();
1771 /* Generate RTL for the body of DECL. */
1773 timevar_push (TV_REST_OF_COMPILATION);
1775 gcc_assert (symtab->global_info_ready);
1777 /* Initialize the default bitmap obstack. */
1778 bitmap_obstack_initialize (NULL);
1780 /* Initialize the RTL code for the function. */
1781 current_function_decl = decl;
1782 saved_loc = input_location;
1783 input_location = DECL_SOURCE_LOCATION (decl);
1784 init_function_start (decl);
1786 gimple_register_cfg_hooks ();
1788 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1790 execute_all_ipa_transforms ();
1792 /* Perform all tree transforms and optimizations. */
1794 /* Signal the start of passes. */
1795 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1797 execute_pass_list (cfun, g->get_passes ()->all_passes);
1799 /* Signal the end of passes. */
1800 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1802 bitmap_obstack_release (&reg_obstack);
1804 /* Release the default bitmap obstack. */
1805 bitmap_obstack_release (NULL);
1807 /* If requested, warn about function definitions where the function will
1808 return a value (usually of some struct or union type) which itself will
1809 take up a lot of stack space. */
1810 if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl))
1812 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
1814 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1815 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1816 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1817 larger_than_size))
1819 unsigned int size_as_int
1820 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1822 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1823 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
1824 decl, size_as_int);
1825 else
1826 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
1827 decl, larger_than_size);
1831 gimple_set_body (decl, NULL);
1832 if (DECL_STRUCT_FUNCTION (decl) == 0
1833 && !cgraph_node::get (decl)->origin)
1835 /* Stop pointing to the local nodes about to be freed.
1836 But DECL_INITIAL must remain nonzero so we know this
1837 was an actual function definition.
1838 For a nested function, this is done in c_pop_function_context.
1839 If rest_of_compilation set this to 0, leave it 0. */
1840 if (DECL_INITIAL (decl) != 0)
1841 DECL_INITIAL (decl) = error_mark_node;
1844 input_location = saved_loc;
1846 ggc_collect ();
1847 timevar_pop (TV_REST_OF_COMPILATION);
1849 /* Make sure that BE didn't give up on compiling. */
1850 gcc_assert (TREE_ASM_WRITTEN (decl));
1851 set_cfun (NULL);
1852 current_function_decl = NULL;
1854 /* It would make a lot more sense to output thunks before function body to get more
1855 forward and lest backwarding jumps. This however would need solving problem
1856 with comdats. See PR48668. Also aliases must come after function itself to
1857 make one pass assemblers, like one on AIX, happy. See PR 50689.
1858 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1859 groups. */
1860 assemble_thunks_and_aliases ();
1861 release_body ();
1862 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1863 points to the dead function body. */
1864 remove_callees ();
1865 remove_all_references ();
1868 /* Node comparer that is responsible for the order that corresponds
1869 to time when a function was launched for the first time. */
1871 static int
1872 node_cmp (const void *pa, const void *pb)
1874 const cgraph_node *a = *(const cgraph_node * const *) pa;
1875 const cgraph_node *b = *(const cgraph_node * const *) pb;
1877 /* Functions with time profile must be before these without profile. */
1878 if (!a->tp_first_run || !b->tp_first_run)
1879 return a->tp_first_run - b->tp_first_run;
1881 return a->tp_first_run != b->tp_first_run
1882 ? b->tp_first_run - a->tp_first_run
1883 : b->order - a->order;
1886 /* Expand all functions that must be output.
1888 Attempt to topologically sort the nodes so function is output when
1889 all called functions are already assembled to allow data to be
1890 propagated across the callgraph. Use a stack to get smaller distance
1891 between a function and its callees (later we may choose to use a more
1892 sophisticated algorithm for function reordering; we will likely want
1893 to use subsections to make the output functions appear in top-down
1894 order). */
1896 static void
1897 expand_all_functions (void)
1899 cgraph_node *node;
1900 cgraph_node **order = XCNEWVEC (cgraph_node *,
1901 symtab->cgraph_count);
1902 unsigned int expanded_func_count = 0, profiled_func_count = 0;
1903 int order_pos, new_order_pos = 0;
1904 int i;
1906 order_pos = ipa_reverse_postorder (order);
1907 gcc_assert (order_pos == symtab->cgraph_count);
1909 /* Garbage collector may remove inline clones we eliminate during
1910 optimization. So we must be sure to not reference them. */
1911 for (i = 0; i < order_pos; i++)
1912 if (order[i]->process)
1913 order[new_order_pos++] = order[i];
1915 if (flag_profile_reorder_functions)
1916 qsort (order, new_order_pos, sizeof (cgraph_node *), node_cmp);
1918 for (i = new_order_pos - 1; i >= 0; i--)
1920 node = order[i];
1922 if (node->process)
1924 expanded_func_count++;
1925 if(node->tp_first_run)
1926 profiled_func_count++;
1928 if (symtab->dump_file)
1929 fprintf (symtab->dump_file,
1930 "Time profile order in expand_all_functions:%s:%d\n",
1931 node->asm_name (), node->tp_first_run);
1932 node->process = 0;
1933 node->expand ();
1937 if (dump_file)
1938 fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
1939 main_input_filename, profiled_func_count, expanded_func_count);
1941 if (symtab->dump_file && flag_profile_reorder_functions)
1942 fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n",
1943 profiled_func_count, expanded_func_count);
1945 symtab->process_new_functions ();
1946 free_gimplify_stack ();
1948 free (order);
1951 /* This is used to sort the node types by the cgraph order number. */
1953 enum cgraph_order_sort_kind
1955 ORDER_UNDEFINED = 0,
1956 ORDER_FUNCTION,
1957 ORDER_VAR,
1958 ORDER_ASM
1961 struct cgraph_order_sort
1963 enum cgraph_order_sort_kind kind;
1964 union
1966 cgraph_node *f;
1967 varpool_node *v;
1968 asm_node *a;
1969 } u;
1972 /* Output all functions, variables, and asm statements in the order
1973 according to their order fields, which is the order in which they
1974 appeared in the file. This implements -fno-toplevel-reorder. In
1975 this mode we may output functions and variables which don't really
1976 need to be output.
1977 When NO_REORDER is true only do this for symbols marked no reorder. */
1979 static void
1980 output_in_order (bool no_reorder)
1982 int max;
1983 cgraph_order_sort *nodes;
1984 int i;
1985 cgraph_node *pf;
1986 varpool_node *pv;
1987 asm_node *pa;
1988 max = symtab->order;
1989 nodes = XCNEWVEC (cgraph_order_sort, max);
1991 FOR_EACH_DEFINED_FUNCTION (pf)
1993 if (pf->process && !pf->thunk.thunk_p && !pf->alias)
1995 if (no_reorder && !pf->no_reorder)
1996 continue;
1997 i = pf->order;
1998 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1999 nodes[i].kind = ORDER_FUNCTION;
2000 nodes[i].u.f = pf;
2004 FOR_EACH_DEFINED_VARIABLE (pv)
2005 if (!DECL_EXTERNAL (pv->decl))
2007 if (no_reorder && !pv->no_reorder)
2008 continue;
2009 i = pv->order;
2010 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2011 nodes[i].kind = ORDER_VAR;
2012 nodes[i].u.v = pv;
2015 for (pa = symtab->first_asm_symbol (); pa; pa = pa->next)
2017 i = pa->order;
2018 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2019 nodes[i].kind = ORDER_ASM;
2020 nodes[i].u.a = pa;
2023 /* In toplevel reorder mode we output all statics; mark them as needed. */
2025 for (i = 0; i < max; ++i)
2026 if (nodes[i].kind == ORDER_VAR)
2027 nodes[i].u.v->finalize_named_section_flags ();
2029 for (i = 0; i < max; ++i)
2031 switch (nodes[i].kind)
2033 case ORDER_FUNCTION:
2034 nodes[i].u.f->process = 0;
2035 nodes[i].u.f->expand ();
2036 break;
2038 case ORDER_VAR:
2039 nodes[i].u.v->assemble_decl ();
2040 break;
2042 case ORDER_ASM:
2043 assemble_asm (nodes[i].u.a->asm_str);
2044 break;
2046 case ORDER_UNDEFINED:
2047 break;
2049 default:
2050 gcc_unreachable ();
2054 symtab->clear_asm_symbols ();
2056 free (nodes);
2059 static void
2060 ipa_passes (void)
2062 gcc::pass_manager *passes = g->get_passes ();
2064 set_cfun (NULL);
2065 current_function_decl = NULL;
2066 gimple_register_cfg_hooks ();
2067 bitmap_obstack_initialize (NULL);
2069 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2071 if (!in_lto_p)
2073 execute_ipa_pass_list (passes->all_small_ipa_passes);
2074 if (seen_error ())
2075 return;
2078 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2079 devirtualization and other changes where removal iterate. */
2080 symtab->remove_unreachable_nodes (symtab->dump_file);
2082 /* If pass_all_early_optimizations was not scheduled, the state of
2083 the cgraph will not be properly updated. Update it now. */
2084 if (symtab->state < IPA_SSA)
2085 symtab->state = IPA_SSA;
2087 if (!in_lto_p)
2089 /* Generate coverage variables and constructors. */
2090 coverage_finish ();
2092 /* Process new functions added. */
2093 set_cfun (NULL);
2094 current_function_decl = NULL;
2095 symtab->process_new_functions ();
2097 execute_ipa_summary_passes
2098 ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2101 /* Some targets need to handle LTO assembler output specially. */
2102 if (flag_generate_lto || flag_generate_offload)
2103 targetm.asm_out.lto_start ();
2105 if (!in_lto_p)
2107 if (g->have_offload)
2109 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2110 ipa_write_summaries (true);
2112 if (flag_lto)
2114 section_name_prefix = LTO_SECTION_NAME_PREFIX;
2115 ipa_write_summaries (false);
2119 if (flag_generate_lto || flag_generate_offload)
2120 targetm.asm_out.lto_end ();
2122 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
2123 execute_ipa_pass_list (passes->all_regular_ipa_passes);
2124 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2126 bitmap_obstack_release (NULL);
2130 /* Return string alias is alias of. */
2132 static tree
2133 get_alias_symbol (tree decl)
2135 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
2136 return get_identifier (TREE_STRING_POINTER
2137 (TREE_VALUE (TREE_VALUE (alias))));
2141 /* Weakrefs may be associated to external decls and thus not output
2142 at expansion time. Emit all necessary aliases. */
2144 void
2145 symbol_table::output_weakrefs (void)
2147 symtab_node *node;
2148 cgraph_node *cnode;
2149 FOR_EACH_SYMBOL (node)
2150 if (node->alias
2151 && !TREE_ASM_WRITTEN (node->decl)
2152 && (!(cnode = dyn_cast <cgraph_node *> (node))
2153 || !cnode->instrumented_version
2154 || !TREE_ASM_WRITTEN (cnode->instrumented_version->decl))
2155 && node->weakref)
2157 tree target;
2159 /* Weakrefs are special by not requiring target definition in current
2160 compilation unit. It is thus bit hard to work out what we want to
2161 alias.
2162 When alias target is defined, we need to fetch it from symtab reference,
2163 otherwise it is pointed to by alias_target. */
2164 if (node->alias_target)
2165 target = (DECL_P (node->alias_target)
2166 ? DECL_ASSEMBLER_NAME (node->alias_target)
2167 : node->alias_target);
2168 else if (node->analyzed)
2169 target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
2170 else
2172 gcc_unreachable ();
2173 target = get_alias_symbol (node->decl);
2175 do_assemble_alias (node->decl, target);
2179 /* Perform simple optimizations based on callgraph. */
2181 void
2182 symbol_table::compile (void)
2184 if (seen_error ())
2185 return;
2187 #ifdef ENABLE_CHECKING
2188 symtab_node::verify_symtab_nodes ();
2189 #endif
2191 timevar_push (TV_CGRAPHOPT);
2192 if (pre_ipa_mem_report)
2194 fprintf (stderr, "Memory consumption before IPA\n");
2195 dump_memory_report (false);
2197 if (!quiet_flag)
2198 fprintf (stderr, "Performing interprocedural optimizations\n");
2199 state = IPA;
2201 /* Offloading requires LTO infrastructure. */
2202 if (!in_lto_p && g->have_offload)
2203 flag_generate_offload = 1;
2205 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2206 if (flag_generate_lto || flag_generate_offload)
2207 lto_streamer_hooks_init ();
2209 /* Don't run the IPA passes if there was any error or sorry messages. */
2210 if (!seen_error ())
2211 ipa_passes ();
2213 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2214 if (seen_error ()
2215 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
2217 timevar_pop (TV_CGRAPHOPT);
2218 return;
2221 global_info_ready = true;
2222 if (dump_file)
2224 fprintf (dump_file, "Optimized ");
2225 symtab_node:: dump_table (dump_file);
2227 if (post_ipa_mem_report)
2229 fprintf (stderr, "Memory consumption after IPA\n");
2230 dump_memory_report (false);
2232 timevar_pop (TV_CGRAPHOPT);
2234 /* Output everything. */
2235 (*debug_hooks->assembly_start) ();
2236 if (!quiet_flag)
2237 fprintf (stderr, "Assembling functions:\n");
2238 #ifdef ENABLE_CHECKING
2239 symtab_node::verify_symtab_nodes ();
2240 #endif
2242 materialize_all_clones ();
2243 bitmap_obstack_initialize (NULL);
2244 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2245 bitmap_obstack_release (NULL);
2246 mark_functions_to_output ();
2248 /* When weakref support is missing, we autmatically translate all
2249 references to NODE to references to its ultimate alias target.
2250 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2251 TREE_CHAIN.
2253 Set up this mapping before we output any assembler but once we are sure
2254 that all symbol renaming is done.
2256 FIXME: All this uglyness can go away if we just do renaming at gimple
2257 level by physically rewritting the IL. At the moment we can only redirect
2258 calls, so we need infrastructure for renaming references as well. */
2259 #ifndef ASM_OUTPUT_WEAKREF
2260 symtab_node *node;
2262 FOR_EACH_SYMBOL (node)
2263 if (node->alias
2264 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2266 IDENTIFIER_TRANSPARENT_ALIAS
2267 (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2268 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2269 = (node->alias_target ? node->alias_target
2270 : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
2272 #endif
2274 state = EXPANSION;
2276 if (!flag_toplevel_reorder)
2277 output_in_order (false);
2278 else
2280 /* Output first asm statements and anything ordered. The process
2281 flag is cleared for these nodes, so we skip them later. */
2282 output_in_order (true);
2283 expand_all_functions ();
2284 output_variables ();
2287 process_new_functions ();
2288 state = FINISHED;
2289 output_weakrefs ();
2291 if (dump_file)
2293 fprintf (dump_file, "\nFinal ");
2294 symtab_node::dump_table (dump_file);
2296 #ifdef ENABLE_CHECKING
2297 symtab_node::verify_symtab_nodes ();
2298 /* Double check that all inline clones are gone and that all
2299 function bodies have been released from memory. */
2300 if (!seen_error ())
2302 cgraph_node *node;
2303 bool error_found = false;
2305 FOR_EACH_DEFINED_FUNCTION (node)
2306 if (node->global.inlined_to
2307 || gimple_has_body_p (node->decl))
2309 error_found = true;
2310 node->debug ();
2312 if (error_found)
2313 internal_error ("nodes with unreleased memory found");
2315 #endif
2319 /* Analyze the whole compilation unit once it is parsed completely. */
2321 void
2322 symbol_table::finalize_compilation_unit (void)
2324 timevar_push (TV_CGRAPH);
2326 /* If we're here there's no current function anymore. Some frontends
2327 are lazy in clearing these. */
2328 current_function_decl = NULL;
2329 set_cfun (NULL);
2331 /* Do not skip analyzing the functions if there were errors, we
2332 miss diagnostics for following functions otherwise. */
2334 /* Emit size functions we didn't inline. */
2335 finalize_size_functions ();
2337 /* Mark alias targets necessary and emit diagnostics. */
2338 handle_alias_pairs ();
2340 if (!quiet_flag)
2342 fprintf (stderr, "\nAnalyzing compilation unit\n");
2343 fflush (stderr);
2346 if (flag_dump_passes)
2347 dump_passes ();
2349 /* Gimplify and lower all functions, compute reachability and
2350 remove unreachable nodes. */
2351 analyze_functions ();
2353 /* Mark alias targets necessary and emit diagnostics. */
2354 handle_alias_pairs ();
2356 /* Gimplify and lower thunks. */
2357 analyze_functions ();
2359 /* Finally drive the pass manager. */
2360 compile ();
2362 timevar_pop (TV_CGRAPH);
2365 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2366 within the same process. For use by toplev::finalize. */
2368 void
2369 cgraphunit_c_finalize (void)
2371 gcc_assert (cgraph_new_nodes.length () == 0);
2372 cgraph_new_nodes.truncate (0);
2374 vtable_entry_type = NULL;
2375 queued_nodes = &symtab_terminator;
2377 first_analyzed = NULL;
2378 first_analyzed_var = NULL;
2381 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2382 kind of wrapper method. */
2384 void
2385 cgraph_node::create_wrapper (cgraph_node *target)
2387 /* Preserve DECL_RESULT so we get right by reference flag. */
2388 tree decl_result = DECL_RESULT (decl);
2390 /* Remove the function's body but keep arguments to be reused
2391 for thunk. */
2392 release_body (true);
2393 reset ();
2395 DECL_RESULT (decl) = decl_result;
2396 DECL_INITIAL (decl) = NULL;
2397 allocate_struct_function (decl, false);
2398 set_cfun (NULL);
2400 /* Turn alias into thunk and expand it into GIMPLE representation. */
2401 definition = true;
2402 thunk.thunk_p = true;
2403 thunk.this_adjusting = false;
2405 cgraph_edge *e = create_edge (target, NULL, 0, CGRAPH_FREQ_BASE);
2407 tree arguments = DECL_ARGUMENTS (decl);
2409 while (arguments)
2411 TREE_ADDRESSABLE (arguments) = false;
2412 arguments = TREE_CHAIN (arguments);
2415 expand_thunk (false, true);
2416 e->call_stmt_cannot_inline_p = true;
2418 /* Inline summary set-up. */
2419 analyze ();
2420 inline_analyze_function (this);
2423 #include "gt-cgraphunit.h"