Daily bump.
[official-gcc.git] / gcc / cgraphunit.c
blob7e78bf789155c779f9620a4a5185e7f8465b85a7
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 "alias.h"
165 #include "symtab.h"
166 #include "tree.h"
167 #include "fold-const.h"
168 #include "varasm.h"
169 #include "stor-layout.h"
170 #include "stringpool.h"
171 #include "output.h"
172 #include "rtl.h"
173 #include "predict.h"
174 #include "hard-reg-set.h"
175 #include "function.h"
176 #include "basic-block.h"
177 #include "dominance.h"
178 #include "cfgcleanup.h"
179 #include "cfg.h"
180 #include "tree-ssa-alias.h"
181 #include "internal-fn.h"
182 #include "gimple-fold.h"
183 #include "gimple-expr.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 "cgraph.h"
202 #include "alloc-pool.h"
203 #include "symbol-summary.h"
204 #include "ipa-prop.h"
205 #include "tree-iterator.h"
206 #include "tree-pass.h"
207 #include "tree-dump.h"
208 #include "gimple-pretty-print.h"
209 #include "output.h"
210 #include "coverage.h"
211 #include "plugin.h"
212 #include "ipa-inline.h"
213 #include "ipa-utils.h"
214 #include "lto-streamer.h"
215 #include "except.h"
216 #include "cfgloop.h"
217 #include "regset.h" /* FIXME: For reg_obstack. */
218 #include "context.h"
219 #include "pass_manager.h"
220 #include "tree-nested.h"
221 #include "gimplify.h"
222 #include "dbgcnt.h"
223 #include "tree-chkp.h"
224 #include "lto-section-names.h"
225 #include "omp-low.h"
226 #include "print-tree.h"
228 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
229 secondary queue used during optimization to accommodate passes that
230 may generate new functions that need to be optimized and expanded. */
231 vec<cgraph_node *> cgraph_new_nodes;
233 static void expand_all_functions (void);
234 static void mark_functions_to_output (void);
235 static void handle_alias_pairs (void);
237 /* Used for vtable lookup in thunk adjusting. */
238 static GTY (()) tree vtable_entry_type;
240 /* Determine if symbol declaration is needed. That is, visible to something
241 either outside this translation unit, something magic in the system
242 configury */
243 bool
244 symtab_node::needed_p (void)
246 /* Double check that no one output the function into assembly file
247 early. */
248 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)
249 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
251 if (!definition)
252 return false;
254 if (DECL_EXTERNAL (decl))
255 return false;
257 /* If the user told us it is used, then it must be so. */
258 if (force_output)
259 return true;
261 /* ABI forced symbols are needed when they are external. */
262 if (forced_by_abi && TREE_PUBLIC (decl))
263 return true;
265 /* Keep constructors, destructors and virtual functions. */
266 if (TREE_CODE (decl) == FUNCTION_DECL
267 && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)))
268 return true;
270 /* Externally visible variables must be output. The exception is
271 COMDAT variables that must be output only when they are needed. */
272 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
273 return true;
275 return false;
278 /* Head and terminator of the queue of nodes to be processed while building
279 callgraph. */
281 static symtab_node symtab_terminator;
282 static symtab_node *queued_nodes = &symtab_terminator;
284 /* Add NODE to queue starting at QUEUED_NODES.
285 The queue is linked via AUX pointers and terminated by pointer to 1. */
287 static void
288 enqueue_node (symtab_node *node)
290 if (node->aux)
291 return;
292 gcc_checking_assert (queued_nodes);
293 node->aux = queued_nodes;
294 queued_nodes = node;
297 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
298 functions into callgraph in a way so they look like ordinary reachable
299 functions inserted into callgraph already at construction time. */
301 void
302 symbol_table::process_new_functions (void)
304 tree fndecl;
306 if (!cgraph_new_nodes.exists ())
307 return;
309 handle_alias_pairs ();
310 /* Note that this queue may grow as its being processed, as the new
311 functions may generate new ones. */
312 for (unsigned i = 0; i < cgraph_new_nodes.length (); i++)
314 cgraph_node *node = cgraph_new_nodes[i];
315 fndecl = node->decl;
316 switch (state)
318 case CONSTRUCTION:
319 /* At construction time we just need to finalize function and move
320 it into reachable functions list. */
322 cgraph_node::finalize_function (fndecl, false);
323 call_cgraph_insertion_hooks (node);
324 enqueue_node (node);
325 break;
327 case IPA:
328 case IPA_SSA:
329 case IPA_SSA_AFTER_INLINING:
330 /* When IPA optimization already started, do all essential
331 transformations that has been already performed on the whole
332 cgraph but not on this function. */
334 gimple_register_cfg_hooks ();
335 if (!node->analyzed)
336 node->analyze ();
337 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
338 if ((state == IPA_SSA || state == IPA_SSA_AFTER_INLINING)
339 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
340 g->get_passes ()->execute_early_local_passes ();
341 else if (inline_summaries != NULL)
342 compute_inline_parameters (node, true);
343 free_dominance_info (CDI_POST_DOMINATORS);
344 free_dominance_info (CDI_DOMINATORS);
345 pop_cfun ();
346 call_cgraph_insertion_hooks (node);
347 break;
349 case EXPANSION:
350 /* Functions created during expansion shall be compiled
351 directly. */
352 node->process = 0;
353 call_cgraph_insertion_hooks (node);
354 node->expand ();
355 break;
357 default:
358 gcc_unreachable ();
359 break;
363 cgraph_new_nodes.release ();
366 /* As an GCC extension we allow redefinition of the function. The
367 semantics when both copies of bodies differ is not well defined.
368 We replace the old body with new body so in unit at a time mode
369 we always use new body, while in normal mode we may end up with
370 old body inlined into some functions and new body expanded and
371 inlined in others.
373 ??? It may make more sense to use one body for inlining and other
374 body for expanding the function but this is difficult to do. */
376 void
377 cgraph_node::reset (void)
379 /* If process is set, then we have already begun whole-unit analysis.
380 This is *not* testing for whether we've already emitted the function.
381 That case can be sort-of legitimately seen with real function redefinition
382 errors. I would argue that the front end should never present us with
383 such a case, but don't enforce that for now. */
384 gcc_assert (!process);
386 /* Reset our data structures so we can analyze the function again. */
387 memset (&local, 0, sizeof (local));
388 memset (&global, 0, sizeof (global));
389 memset (&rtl, 0, sizeof (rtl));
390 analyzed = false;
391 definition = false;
392 alias = false;
393 weakref = false;
394 cpp_implicit_alias = false;
396 remove_callees ();
397 remove_all_references ();
400 /* Return true when there are references to the node. INCLUDE_SELF is
401 true if a self reference counts as a reference. */
403 bool
404 symtab_node::referred_to_p (bool include_self)
406 ipa_ref *ref = NULL;
408 /* See if there are any references at all. */
409 if (iterate_referring (0, ref))
410 return true;
411 /* For functions check also calls. */
412 cgraph_node *cn = dyn_cast <cgraph_node *> (this);
413 if (cn && cn->callers)
415 if (include_self)
416 return true;
417 for (cgraph_edge *e = cn->callers; e; e = e->next_caller)
418 if (e->caller != this)
419 return true;
421 return false;
424 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
425 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
426 the garbage collector run at the moment. We would need to either create
427 a new GC context, or just not compile right now. */
429 void
430 cgraph_node::finalize_function (tree decl, bool no_collect)
432 cgraph_node *node = cgraph_node::get_create (decl);
434 if (node->definition)
436 /* Nested functions should only be defined once. */
437 gcc_assert (!DECL_CONTEXT (decl)
438 || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL);
439 node->reset ();
440 node->local.redefined_extern_inline = true;
443 /* Set definition first before calling notice_global_symbol so that
444 it is available to notice_global_symbol. */
445 node->definition = true;
446 notice_global_symbol (decl);
447 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
449 /* With -fkeep-inline-functions we are keeping all inline functions except
450 for extern inline ones. */
451 if (flag_keep_inline_functions
452 && DECL_DECLARED_INLINE_P (decl)
453 && !DECL_EXTERNAL (decl)
454 && !DECL_DISREGARD_INLINE_LIMITS (decl))
455 node->force_output = 1;
457 /* When not optimizing, also output the static functions. (see
458 PR24561), but don't do so for always_inline functions, functions
459 declared inline and nested functions. These were optimized out
460 in the original implementation and it is unclear whether we want
461 to change the behavior here. */
462 if ((!opt_for_fn (decl, optimize)
463 && !node->cpp_implicit_alias
464 && !DECL_DISREGARD_INLINE_LIMITS (decl)
465 && !DECL_DECLARED_INLINE_P (decl)
466 && !(DECL_CONTEXT (decl)
467 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
468 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
469 node->force_output = 1;
471 /* If we've not yet emitted decl, tell the debug info about it. */
472 if (!TREE_ASM_WRITTEN (decl))
473 (*debug_hooks->deferred_inline_function) (decl);
475 if (!no_collect)
476 ggc_collect ();
478 if (symtab->state == CONSTRUCTION
479 && (node->needed_p () || node->referred_to_p ()))
480 enqueue_node (node);
483 /* Add the function FNDECL to the call graph.
484 Unlike finalize_function, this function is intended to be used
485 by middle end and allows insertion of new function at arbitrary point
486 of compilation. The function can be either in high, low or SSA form
487 GIMPLE.
489 The function is assumed to be reachable and have address taken (so no
490 API breaking optimizations are performed on it).
492 Main work done by this function is to enqueue the function for later
493 processing to avoid need the passes to be re-entrant. */
495 void
496 cgraph_node::add_new_function (tree fndecl, bool lowered)
498 gcc::pass_manager *passes = g->get_passes ();
499 cgraph_node *node;
501 if (dump_file)
503 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
504 const char *function_type = ((gimple_has_body_p (fndecl))
505 ? (lowered
506 ? (gimple_in_ssa_p (fn)
507 ? "ssa gimple"
508 : "low gimple")
509 : "high gimple")
510 : "to-be-gimplified");
511 fprintf (dump_file,
512 "Added new %s function %s to callgraph\n",
513 function_type,
514 fndecl_name (fndecl));
517 switch (symtab->state)
519 case PARSING:
520 cgraph_node::finalize_function (fndecl, false);
521 break;
522 case CONSTRUCTION:
523 /* Just enqueue function to be processed at nearest occurrence. */
524 node = cgraph_node::get_create (fndecl);
525 if (lowered)
526 node->lowered = true;
527 cgraph_new_nodes.safe_push (node);
528 break;
530 case IPA:
531 case IPA_SSA:
532 case IPA_SSA_AFTER_INLINING:
533 case EXPANSION:
534 /* Bring the function into finalized state and enqueue for later
535 analyzing and compilation. */
536 node = cgraph_node::get_create (fndecl);
537 node->local.local = false;
538 node->definition = true;
539 node->force_output = true;
540 if (!lowered && symtab->state == EXPANSION)
542 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
543 gimple_register_cfg_hooks ();
544 bitmap_obstack_initialize (NULL);
545 execute_pass_list (cfun, passes->all_lowering_passes);
546 passes->execute_early_local_passes ();
547 bitmap_obstack_release (NULL);
548 pop_cfun ();
550 lowered = true;
552 if (lowered)
553 node->lowered = true;
554 cgraph_new_nodes.safe_push (node);
555 break;
557 case FINISHED:
558 /* At the very end of compilation we have to do all the work up
559 to expansion. */
560 node = cgraph_node::create (fndecl);
561 if (lowered)
562 node->lowered = true;
563 node->definition = true;
564 node->analyze ();
565 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
566 gimple_register_cfg_hooks ();
567 bitmap_obstack_initialize (NULL);
568 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
569 g->get_passes ()->execute_early_local_passes ();
570 bitmap_obstack_release (NULL);
571 pop_cfun ();
572 node->expand ();
573 break;
575 default:
576 gcc_unreachable ();
579 /* Set a personality if required and we already passed EH lowering. */
580 if (lowered
581 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
582 == eh_personality_lang))
583 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
586 /* Analyze the function scheduled to be output. */
587 void
588 cgraph_node::analyze (void)
590 tree decl = this->decl;
591 location_t saved_loc = input_location;
592 input_location = DECL_SOURCE_LOCATION (decl);
594 if (thunk.thunk_p)
596 cgraph_node *t = cgraph_node::get (thunk.alias);
598 create_edge (t, NULL, 0, CGRAPH_FREQ_BASE);
599 /* Target code in expand_thunk may need the thunk's target
600 to be analyzed, so recurse here. */
601 if (!t->analyzed)
602 t->analyze ();
603 if (t->alias)
605 t = t->get_alias_target ();
606 if (!t->analyzed)
607 t->analyze ();
609 if (!expand_thunk (false, false))
611 thunk.alias = NULL;
612 return;
614 thunk.alias = NULL;
616 if (alias)
617 resolve_alias (cgraph_node::get (alias_target));
618 else if (dispatcher_function)
620 /* Generate the dispatcher body of multi-versioned functions. */
621 cgraph_function_version_info *dispatcher_version_info
622 = function_version ();
623 if (dispatcher_version_info != NULL
624 && (dispatcher_version_info->dispatcher_resolver
625 == NULL_TREE))
627 tree resolver = NULL_TREE;
628 gcc_assert (targetm.generate_version_dispatcher_body);
629 resolver = targetm.generate_version_dispatcher_body (this);
630 gcc_assert (resolver != NULL_TREE);
633 else
635 push_cfun (DECL_STRUCT_FUNCTION (decl));
637 assign_assembler_name_if_neeeded (decl);
639 /* Make sure to gimplify bodies only once. During analyzing a
640 function we lower it, which will require gimplified nested
641 functions, so we can end up here with an already gimplified
642 body. */
643 if (!gimple_has_body_p (decl))
644 gimplify_function_tree (decl);
646 /* Lower the function. */
647 if (!lowered)
649 if (nested)
650 lower_nested_functions (decl);
651 gcc_assert (!nested);
653 gimple_register_cfg_hooks ();
654 bitmap_obstack_initialize (NULL);
655 execute_pass_list (cfun, g->get_passes ()->all_lowering_passes);
656 free_dominance_info (CDI_POST_DOMINATORS);
657 free_dominance_info (CDI_DOMINATORS);
658 compact_blocks ();
659 bitmap_obstack_release (NULL);
660 lowered = true;
663 pop_cfun ();
665 analyzed = true;
667 input_location = saved_loc;
670 /* C++ frontend produce same body aliases all over the place, even before PCH
671 gets streamed out. It relies on us linking the aliases with their function
672 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
673 first produce aliases without links, but once C++ FE is sure he won't sream
674 PCH we build the links via this function. */
676 void
677 symbol_table::process_same_body_aliases (void)
679 symtab_node *node;
680 FOR_EACH_SYMBOL (node)
681 if (node->cpp_implicit_alias && !node->analyzed)
682 node->resolve_alias
683 (TREE_CODE (node->alias_target) == VAR_DECL
684 ? (symtab_node *)varpool_node::get_create (node->alias_target)
685 : (symtab_node *)cgraph_node::get_create (node->alias_target));
686 cpp_implicit_aliases_done = true;
689 /* Process attributes common for vars and functions. */
691 static void
692 process_common_attributes (symtab_node *node, tree decl)
694 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
696 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
698 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
699 "%<weakref%> attribute should be accompanied with"
700 " an %<alias%> attribute");
701 DECL_WEAK (decl) = 0;
702 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
703 DECL_ATTRIBUTES (decl));
706 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
707 node->no_reorder = 1;
710 /* Look for externally_visible and used attributes and mark cgraph nodes
711 accordingly.
713 We cannot mark the nodes at the point the attributes are processed (in
714 handle_*_attribute) because the copy of the declarations available at that
715 point may not be canonical. For example, in:
717 void f();
718 void f() __attribute__((used));
720 the declaration we see in handle_used_attribute will be the second
721 declaration -- but the front end will subsequently merge that declaration
722 with the original declaration and discard the second declaration.
724 Furthermore, we can't mark these nodes in finalize_function because:
726 void f() {}
727 void f() __attribute__((externally_visible));
729 is valid.
731 So, we walk the nodes at the end of the translation unit, applying the
732 attributes at that point. */
734 static void
735 process_function_and_variable_attributes (cgraph_node *first,
736 varpool_node *first_var)
738 cgraph_node *node;
739 varpool_node *vnode;
741 for (node = symtab->first_function (); node != first;
742 node = symtab->next_function (node))
744 tree decl = node->decl;
745 if (DECL_PRESERVE_P (decl))
746 node->mark_force_output ();
747 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
749 if (! TREE_PUBLIC (node->decl))
750 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
751 "%<externally_visible%>"
752 " attribute have effect only on public objects");
754 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
755 && (node->definition && !node->alias))
757 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
758 "%<weakref%> attribute ignored"
759 " because function is defined");
760 DECL_WEAK (decl) = 0;
761 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
762 DECL_ATTRIBUTES (decl));
765 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
766 && !DECL_DECLARED_INLINE_P (decl)
767 /* redefining extern inline function makes it DECL_UNINLINABLE. */
768 && !DECL_UNINLINABLE (decl))
769 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
770 "always_inline function might not be inlinable");
772 process_common_attributes (node, decl);
774 for (vnode = symtab->first_variable (); vnode != first_var;
775 vnode = symtab->next_variable (vnode))
777 tree decl = vnode->decl;
778 if (DECL_EXTERNAL (decl)
779 && DECL_INITIAL (decl))
780 varpool_node::finalize_decl (decl);
781 if (DECL_PRESERVE_P (decl))
782 vnode->force_output = true;
783 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
785 if (! TREE_PUBLIC (vnode->decl))
786 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
787 "%<externally_visible%>"
788 " attribute have effect only on public objects");
790 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
791 && vnode->definition
792 && DECL_INITIAL (decl))
794 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
795 "%<weakref%> attribute ignored"
796 " because variable is initialized");
797 DECL_WEAK (decl) = 0;
798 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
799 DECL_ATTRIBUTES (decl));
801 process_common_attributes (vnode, decl);
805 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
806 middle end to output the variable to asm file, if needed or externally
807 visible. */
809 void
810 varpool_node::finalize_decl (tree decl)
812 varpool_node *node = varpool_node::get_create (decl);
814 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
816 if (node->definition)
817 return;
818 /* Set definition first before calling notice_global_symbol so that
819 it is available to notice_global_symbol. */
820 node->definition = true;
821 notice_global_symbol (decl);
822 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
823 /* Traditionally we do not eliminate static variables when not
824 optimizing and when not doing toplevel reoder. */
825 || node->no_reorder
826 || ((!flag_toplevel_reorder
827 && !DECL_COMDAT (node->decl)
828 && !DECL_ARTIFICIAL (node->decl))))
829 node->force_output = true;
831 if (symtab->state == CONSTRUCTION
832 && (node->needed_p () || node->referred_to_p ()))
833 enqueue_node (node);
834 if (symtab->state >= IPA_SSA)
835 node->analyze ();
836 /* Some frontends produce various interface variables after compilation
837 finished. */
838 if (symtab->state == FINISHED
839 || (!flag_toplevel_reorder
840 && symtab->state == EXPANSION))
841 node->assemble_decl ();
843 if (DECL_INITIAL (decl))
844 chkp_register_var_initializer (decl);
847 /* EDGE is an polymorphic call. Mark all possible targets as reachable
848 and if there is only one target, perform trivial devirtualization.
849 REACHABLE_CALL_TARGETS collects target lists we already walked to
850 avoid udplicate work. */
852 static void
853 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
854 cgraph_edge *edge)
856 unsigned int i;
857 void *cache_token;
858 bool final;
859 vec <cgraph_node *>targets
860 = possible_polymorphic_call_targets
861 (edge, &final, &cache_token);
863 if (!reachable_call_targets->add (cache_token))
865 if (symtab->dump_file)
866 dump_possible_polymorphic_call_targets
867 (symtab->dump_file, edge);
869 for (i = 0; i < targets.length (); i++)
871 /* Do not bother to mark virtual methods in anonymous namespace;
872 either we will find use of virtual table defining it, or it is
873 unused. */
874 if (targets[i]->definition
875 && TREE_CODE
876 (TREE_TYPE (targets[i]->decl))
877 == METHOD_TYPE
878 && !type_in_anonymous_namespace_p
879 (TYPE_METHOD_BASETYPE (TREE_TYPE (targets[i]->decl))))
880 enqueue_node (targets[i]);
884 /* Very trivial devirtualization; when the type is
885 final or anonymous (so we know all its derivation)
886 and there is only one possible virtual call target,
887 make the edge direct. */
888 if (final)
890 if (targets.length () <= 1 && dbg_cnt (devirt))
892 cgraph_node *target;
893 if (targets.length () == 1)
894 target = targets[0];
895 else
896 target = cgraph_node::create
897 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
899 if (symtab->dump_file)
901 fprintf (symtab->dump_file,
902 "Devirtualizing call: ");
903 print_gimple_stmt (symtab->dump_file,
904 edge->call_stmt, 0,
905 TDF_SLIM);
907 if (dump_enabled_p ())
909 location_t locus = gimple_location_safe (edge->call_stmt);
910 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
911 "devirtualizing call in %s to %s\n",
912 edge->caller->name (), target->name ());
915 edge->make_direct (target);
916 edge->redirect_call_stmt_to_callee ();
918 /* Call to __builtin_unreachable shouldn't be instrumented. */
919 if (!targets.length ())
920 gimple_call_set_with_bounds (edge->call_stmt, false);
922 if (symtab->dump_file)
924 fprintf (symtab->dump_file,
925 "Devirtualized as: ");
926 print_gimple_stmt (symtab->dump_file,
927 edge->call_stmt, 0,
928 TDF_SLIM);
935 /* Discover all functions and variables that are trivially needed, analyze
936 them as well as all functions and variables referred by them */
937 static cgraph_node *first_analyzed;
938 static varpool_node *first_analyzed_var;
940 /* FIRST_TIME is set to TRUE for the first time we are called for a
941 translation unit from finalize_compilation_unit() or false
942 otherwise. */
944 static void
945 analyze_functions (bool first_time)
947 /* Keep track of already processed nodes when called multiple times for
948 intermodule optimization. */
949 cgraph_node *first_handled = first_analyzed;
950 varpool_node *first_handled_var = first_analyzed_var;
951 hash_set<void *> reachable_call_targets;
953 symtab_node *node;
954 symtab_node *next;
955 int i;
956 ipa_ref *ref;
957 bool changed = true;
958 location_t saved_loc = input_location;
960 bitmap_obstack_initialize (NULL);
961 symtab->state = CONSTRUCTION;
962 input_location = UNKNOWN_LOCATION;
964 /* Ugly, but the fixup can not happen at a time same body alias is created;
965 C++ FE is confused about the COMDAT groups being right. */
966 if (symtab->cpp_implicit_aliases_done)
967 FOR_EACH_SYMBOL (node)
968 if (node->cpp_implicit_alias)
969 node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
970 build_type_inheritance_graph ();
972 /* Analysis adds static variables that in turn adds references to new functions.
973 So we need to iterate the process until it stabilize. */
974 while (changed)
976 changed = false;
977 process_function_and_variable_attributes (first_analyzed,
978 first_analyzed_var);
980 /* First identify the trivially needed symbols. */
981 for (node = symtab->first_symbol ();
982 node != first_analyzed
983 && node != first_analyzed_var; node = node->next)
985 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
986 node->get_comdat_group_id ();
987 if (node->needed_p ())
989 enqueue_node (node);
990 if (!changed && symtab->dump_file)
991 fprintf (symtab->dump_file, "Trivially needed symbols:");
992 changed = true;
993 if (symtab->dump_file)
994 fprintf (symtab->dump_file, " %s", node->asm_name ());
995 if (!changed && symtab->dump_file)
996 fprintf (symtab->dump_file, "\n");
998 if (node == first_analyzed
999 || node == first_analyzed_var)
1000 break;
1002 symtab->process_new_functions ();
1003 first_analyzed_var = symtab->first_variable ();
1004 first_analyzed = symtab->first_function ();
1006 if (changed && symtab->dump_file)
1007 fprintf (symtab->dump_file, "\n");
1009 /* Lower representation, build callgraph edges and references for all trivially
1010 needed symbols and all symbols referred by them. */
1011 while (queued_nodes != &symtab_terminator)
1013 changed = true;
1014 node = queued_nodes;
1015 queued_nodes = (symtab_node *)queued_nodes->aux;
1016 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1017 if (cnode && cnode->definition)
1019 cgraph_edge *edge;
1020 tree decl = cnode->decl;
1022 /* ??? It is possible to create extern inline function
1023 and later using weak alias attribute to kill its body.
1024 See gcc.c-torture/compile/20011119-1.c */
1025 if (!DECL_STRUCT_FUNCTION (decl)
1026 && !cnode->alias
1027 && !cnode->thunk.thunk_p
1028 && !cnode->dispatcher_function)
1030 cnode->reset ();
1031 cnode->local.redefined_extern_inline = true;
1032 continue;
1035 if (!cnode->analyzed)
1036 cnode->analyze ();
1038 for (edge = cnode->callees; edge; edge = edge->next_callee)
1039 if (edge->callee->definition
1040 && (!DECL_EXTERNAL (edge->callee->decl)
1041 /* When not optimizing, do not try to analyze extern
1042 inline functions. Doing so is pointless. */
1043 || opt_for_fn (edge->callee->decl, optimize)
1044 /* Weakrefs needs to be preserved. */
1045 || edge->callee->alias
1046 /* always_inline functions are inlined aven at -O0. */
1047 || lookup_attribute
1048 ("always_inline",
1049 DECL_ATTRIBUTES (edge->callee->decl))
1050 /* Multiversioned functions needs the dispatcher to
1051 be produced locally even for extern functions. */
1052 || edge->callee->function_version ()))
1053 enqueue_node (edge->callee);
1054 if (opt_for_fn (cnode->decl, optimize)
1055 && opt_for_fn (cnode->decl, flag_devirtualize))
1057 cgraph_edge *next;
1059 for (edge = cnode->indirect_calls; edge; edge = next)
1061 next = edge->next_callee;
1062 if (edge->indirect_info->polymorphic)
1063 walk_polymorphic_call_targets (&reachable_call_targets,
1064 edge);
1068 /* If decl is a clone of an abstract function,
1069 mark that abstract function so that we don't release its body.
1070 The DECL_INITIAL() of that abstract function declaration
1071 will be later needed to output debug info. */
1072 if (DECL_ABSTRACT_ORIGIN (decl))
1074 cgraph_node *origin_node
1075 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl));
1076 origin_node->used_as_abstract_origin = true;
1079 else
1081 varpool_node *vnode = dyn_cast <varpool_node *> (node);
1082 if (vnode && vnode->definition && !vnode->analyzed)
1083 vnode->analyze ();
1086 if (node->same_comdat_group)
1088 symtab_node *next;
1089 for (next = node->same_comdat_group;
1090 next != node;
1091 next = next->same_comdat_group)
1092 if (!next->comdat_local_p ())
1093 enqueue_node (next);
1095 for (i = 0; node->iterate_reference (i, ref); i++)
1096 if (ref->referred->definition
1097 && (!DECL_EXTERNAL (ref->referred->decl)
1098 || ((TREE_CODE (ref->referred->decl) != FUNCTION_DECL
1099 && optimize)
1100 || (TREE_CODE (ref->referred->decl) == FUNCTION_DECL
1101 && opt_for_fn (ref->referred->decl, optimize))
1102 || node->alias
1103 || ref->referred->alias)))
1104 enqueue_node (ref->referred);
1105 symtab->process_new_functions ();
1108 update_type_inheritance_graph ();
1110 /* Collect entry points to the unit. */
1111 if (symtab->dump_file)
1113 fprintf (symtab->dump_file, "\n\nInitial ");
1114 symtab_node::dump_table (symtab->dump_file);
1117 if (first_time)
1119 symtab_node *snode;
1120 FOR_EACH_SYMBOL (snode)
1121 check_global_declaration (snode->decl);
1124 if (symtab->dump_file)
1125 fprintf (symtab->dump_file, "\nRemoving unused symbols:");
1127 for (node = symtab->first_symbol ();
1128 node != first_handled
1129 && node != first_handled_var; node = next)
1131 next = node->next;
1132 if (!node->aux && !node->referred_to_p ())
1134 if (symtab->dump_file)
1135 fprintf (symtab->dump_file, " %s", node->name ());
1137 /* See if the debugger can use anything before the DECL
1138 passes away. Perhaps it can notice a DECL that is now a
1139 constant and can tag the early DIE with an appropriate
1140 attribute.
1142 Otherwise, this is the last chance the debug_hooks have
1143 at looking at optimized away DECLs, since
1144 late_global_decl will subsequently be called from the
1145 contents of the now pruned symbol table. */
1146 if (!decl_function_context (node->decl))
1147 (*debug_hooks->late_global_decl) (node->decl);
1149 node->remove ();
1150 continue;
1152 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1154 tree decl = node->decl;
1156 if (cnode->definition && !gimple_has_body_p (decl)
1157 && !cnode->alias
1158 && !cnode->thunk.thunk_p)
1159 cnode->reset ();
1161 gcc_assert (!cnode->definition || cnode->thunk.thunk_p
1162 || cnode->alias
1163 || gimple_has_body_p (decl));
1164 gcc_assert (cnode->analyzed == cnode->definition);
1166 node->aux = NULL;
1168 for (;node; node = node->next)
1169 node->aux = NULL;
1170 first_analyzed = symtab->first_function ();
1171 first_analyzed_var = symtab->first_variable ();
1172 if (symtab->dump_file)
1174 fprintf (symtab->dump_file, "\n\nReclaimed ");
1175 symtab_node::dump_table (symtab->dump_file);
1177 bitmap_obstack_release (NULL);
1178 ggc_collect ();
1179 /* Initialize assembler name hash, in particular we want to trigger C++
1180 mangling and same body alias creation before we free DECL_ARGUMENTS
1181 used by it. */
1182 if (!seen_error ())
1183 symtab->symtab_initialize_asm_name_hash ();
1185 input_location = saved_loc;
1188 /* Translate the ugly representation of aliases as alias pairs into nice
1189 representation in callgraph. We don't handle all cases yet,
1190 unfortunately. */
1192 static void
1193 handle_alias_pairs (void)
1195 alias_pair *p;
1196 unsigned i;
1198 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1200 symtab_node *target_node = symtab_node::get_for_asmname (p->target);
1202 /* Weakrefs with target not defined in current unit are easy to handle:
1203 they behave just as external variables except we need to note the
1204 alias flag to later output the weakref pseudo op into asm file. */
1205 if (!target_node
1206 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1208 symtab_node *node = symtab_node::get (p->decl);
1209 if (node)
1211 node->alias_target = p->target;
1212 node->weakref = true;
1213 node->alias = true;
1215 alias_pairs->unordered_remove (i);
1216 continue;
1218 else if (!target_node)
1220 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1221 symtab_node *node = symtab_node::get (p->decl);
1222 if (node)
1223 node->alias = false;
1224 alias_pairs->unordered_remove (i);
1225 continue;
1228 if (DECL_EXTERNAL (target_node->decl)
1229 /* We use local aliases for C++ thunks to force the tailcall
1230 to bind locally. This is a hack - to keep it working do
1231 the following (which is not strictly correct). */
1232 && (TREE_CODE (target_node->decl) != FUNCTION_DECL
1233 || ! DECL_VIRTUAL_P (target_node->decl))
1234 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1236 error ("%q+D aliased to external symbol %qE",
1237 p->decl, p->target);
1240 if (TREE_CODE (p->decl) == FUNCTION_DECL
1241 && target_node && is_a <cgraph_node *> (target_node))
1243 cgraph_node *src_node = cgraph_node::get (p->decl);
1244 if (src_node && src_node->definition)
1245 src_node->reset ();
1246 cgraph_node::create_alias (p->decl, target_node->decl);
1247 alias_pairs->unordered_remove (i);
1249 else if (TREE_CODE (p->decl) == VAR_DECL
1250 && target_node && is_a <varpool_node *> (target_node))
1252 varpool_node::create_alias (p->decl, target_node->decl);
1253 alias_pairs->unordered_remove (i);
1255 else
1257 error ("%q+D alias in between function and variable is not supported",
1258 p->decl);
1259 warning (0, "%q+D aliased declaration",
1260 target_node->decl);
1261 alias_pairs->unordered_remove (i);
1264 vec_free (alias_pairs);
1268 /* Figure out what functions we want to assemble. */
1270 static void
1271 mark_functions_to_output (void)
1273 cgraph_node *node;
1274 #ifdef ENABLE_CHECKING
1275 bool check_same_comdat_groups = false;
1277 FOR_EACH_FUNCTION (node)
1278 gcc_assert (!node->process);
1279 #endif
1281 FOR_EACH_FUNCTION (node)
1283 tree decl = node->decl;
1285 gcc_assert (!node->process || node->same_comdat_group);
1286 if (node->process)
1287 continue;
1289 /* We need to output all local functions that are used and not
1290 always inlined, as well as those that are reachable from
1291 outside the current compilation unit. */
1292 if (node->analyzed
1293 && !node->thunk.thunk_p
1294 && !node->alias
1295 && !node->global.inlined_to
1296 && !TREE_ASM_WRITTEN (decl)
1297 && !DECL_EXTERNAL (decl))
1299 node->process = 1;
1300 if (node->same_comdat_group)
1302 cgraph_node *next;
1303 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
1304 next != node;
1305 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
1306 if (!next->thunk.thunk_p && !next->alias
1307 && !next->comdat_local_p ())
1308 next->process = 1;
1311 else if (node->same_comdat_group)
1313 #ifdef ENABLE_CHECKING
1314 check_same_comdat_groups = true;
1315 #endif
1317 else
1319 /* We should've reclaimed all functions that are not needed. */
1320 #ifdef ENABLE_CHECKING
1321 if (!node->global.inlined_to
1322 && gimple_has_body_p (decl)
1323 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1324 are inside partition, we can end up not removing the body since we no longer
1325 have analyzed node pointing to it. */
1326 && !node->in_other_partition
1327 && !node->alias
1328 && !node->clones
1329 && !DECL_EXTERNAL (decl))
1331 node->debug ();
1332 internal_error ("failed to reclaim unneeded function");
1334 #endif
1335 gcc_assert (node->global.inlined_to
1336 || !gimple_has_body_p (decl)
1337 || node->in_other_partition
1338 || node->clones
1339 || DECL_ARTIFICIAL (decl)
1340 || DECL_EXTERNAL (decl));
1345 #ifdef ENABLE_CHECKING
1346 if (check_same_comdat_groups)
1347 FOR_EACH_FUNCTION (node)
1348 if (node->same_comdat_group && !node->process)
1350 tree decl = node->decl;
1351 if (!node->global.inlined_to
1352 && gimple_has_body_p (decl)
1353 /* FIXME: in an ltrans unit when the offline copy is outside a
1354 partition but inline copies are inside a partition, we can
1355 end up not removing the body since we no longer have an
1356 analyzed node pointing to it. */
1357 && !node->in_other_partition
1358 && !node->clones
1359 && !DECL_EXTERNAL (decl))
1361 node->debug ();
1362 internal_error ("failed to reclaim unneeded function in same "
1363 "comdat group");
1366 #endif
1369 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1370 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1372 Set current_function_decl and cfun to newly constructed empty function body.
1373 return basic block in the function body. */
1375 basic_block
1376 init_lowered_empty_function (tree decl, bool in_ssa, gcov_type count)
1378 basic_block bb;
1379 edge e;
1381 current_function_decl = decl;
1382 allocate_struct_function (decl, false);
1383 gimple_register_cfg_hooks ();
1384 init_empty_tree_cfg ();
1386 if (in_ssa)
1388 init_tree_ssa (cfun);
1389 init_ssa_operands (cfun);
1390 cfun->gimple_df->in_ssa_p = true;
1391 cfun->curr_properties |= PROP_ssa;
1394 DECL_INITIAL (decl) = make_node (BLOCK);
1396 DECL_SAVED_TREE (decl) = error_mark_node;
1397 cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1398 | PROP_cfg | PROP_loops);
1400 set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ());
1401 init_loops_structure (cfun, loops_for_fn (cfun), 1);
1402 loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1404 /* Create BB for body of the function and connect it properly. */
1405 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = count;
1406 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency = REG_BR_PROB_BASE;
1407 EXIT_BLOCK_PTR_FOR_FN (cfun)->count = count;
1408 EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency = REG_BR_PROB_BASE;
1409 bb = create_basic_block (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun));
1410 bb->count = count;
1411 bb->frequency = BB_FREQ_MAX;
1412 e = make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU);
1413 e->count = count;
1414 e->probability = REG_BR_PROB_BASE;
1415 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1416 e->count = count;
1417 e->probability = REG_BR_PROB_BASE;
1418 add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
1420 return bb;
1423 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1424 offset indicated by VIRTUAL_OFFSET, if that is
1425 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1426 zero for a result adjusting thunk. */
1428 static tree
1429 thunk_adjust (gimple_stmt_iterator * bsi,
1430 tree ptr, bool this_adjusting,
1431 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1433 gassign *stmt;
1434 tree ret;
1436 if (this_adjusting
1437 && fixed_offset != 0)
1439 stmt = gimple_build_assign
1440 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1441 ptr,
1442 fixed_offset));
1443 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1446 /* If there's a virtual offset, look up that value in the vtable and
1447 adjust the pointer again. */
1448 if (virtual_offset)
1450 tree vtabletmp;
1451 tree vtabletmp2;
1452 tree vtabletmp3;
1454 if (!vtable_entry_type)
1456 tree vfunc_type = make_node (FUNCTION_TYPE);
1457 TREE_TYPE (vfunc_type) = integer_type_node;
1458 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1459 layout_type (vfunc_type);
1461 vtable_entry_type = build_pointer_type (vfunc_type);
1464 vtabletmp =
1465 create_tmp_reg (build_pointer_type
1466 (build_pointer_type (vtable_entry_type)), "vptr");
1468 /* The vptr is always at offset zero in the object. */
1469 stmt = gimple_build_assign (vtabletmp,
1470 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1471 ptr));
1472 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1474 /* Form the vtable address. */
1475 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
1476 "vtableaddr");
1477 stmt = gimple_build_assign (vtabletmp2,
1478 build_simple_mem_ref (vtabletmp));
1479 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1481 /* Find the entry with the vcall offset. */
1482 stmt = gimple_build_assign (vtabletmp2,
1483 fold_build_pointer_plus_loc (input_location,
1484 vtabletmp2,
1485 virtual_offset));
1486 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1488 /* Get the offset itself. */
1489 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1490 "vcalloffset");
1491 stmt = gimple_build_assign (vtabletmp3,
1492 build_simple_mem_ref (vtabletmp2));
1493 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1495 /* Adjust the `this' pointer. */
1496 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1497 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1498 GSI_CONTINUE_LINKING);
1501 if (!this_adjusting
1502 && fixed_offset != 0)
1503 /* Adjust the pointer by the constant. */
1505 tree ptrtmp;
1507 if (TREE_CODE (ptr) == VAR_DECL)
1508 ptrtmp = ptr;
1509 else
1511 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
1512 stmt = gimple_build_assign (ptrtmp, ptr);
1513 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1515 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1516 ptrtmp, fixed_offset);
1519 /* Emit the statement and gimplify the adjustment expression. */
1520 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
1521 stmt = gimple_build_assign (ret, ptr);
1522 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1524 return ret;
1527 /* Expand thunk NODE to gimple if possible.
1528 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1529 no assembler is produced.
1530 When OUTPUT_ASM_THUNK is true, also produce assembler for
1531 thunks that are not lowered. */
1533 bool
1534 cgraph_node::expand_thunk (bool output_asm_thunks, bool force_gimple_thunk)
1536 bool this_adjusting = thunk.this_adjusting;
1537 HOST_WIDE_INT fixed_offset = thunk.fixed_offset;
1538 HOST_WIDE_INT virtual_value = thunk.virtual_value;
1539 tree virtual_offset = NULL;
1540 tree alias = callees->callee->decl;
1541 tree thunk_fndecl = decl;
1542 tree a;
1544 /* Instrumentation thunk is the same function with
1545 a different signature. Never need to expand it. */
1546 if (thunk.add_pointer_bounds_args)
1547 return false;
1549 if (!force_gimple_thunk && this_adjusting
1550 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1551 virtual_value, alias))
1553 const char *fnname;
1554 tree fn_block;
1555 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1557 if (!output_asm_thunks)
1559 analyzed = true;
1560 return false;
1563 if (in_lto_p)
1564 get_untransformed_body ();
1565 a = DECL_ARGUMENTS (thunk_fndecl);
1567 current_function_decl = thunk_fndecl;
1569 /* Ensure thunks are emitted in their correct sections. */
1570 resolve_unique_section (thunk_fndecl, 0,
1571 flag_function_sections);
1573 DECL_RESULT (thunk_fndecl)
1574 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1575 RESULT_DECL, 0, restype);
1576 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1577 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
1579 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1580 create one. */
1581 fn_block = make_node (BLOCK);
1582 BLOCK_VARS (fn_block) = a;
1583 DECL_INITIAL (thunk_fndecl) = fn_block;
1584 init_function_start (thunk_fndecl);
1585 cfun->is_thunk = 1;
1586 insn_locations_init ();
1587 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1588 prologue_location = curr_insn_location ();
1589 assemble_start_function (thunk_fndecl, fnname);
1591 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1592 fixed_offset, virtual_value, alias);
1594 assemble_end_function (thunk_fndecl, fnname);
1595 insn_locations_finalize ();
1596 init_insn_lengths ();
1597 free_after_compilation (cfun);
1598 set_cfun (NULL);
1599 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1600 thunk.thunk_p = false;
1601 analyzed = false;
1603 else if (stdarg_p (TREE_TYPE (thunk_fndecl)))
1605 error ("generic thunk code fails for method %qD which uses %<...%>",
1606 thunk_fndecl);
1607 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1608 analyzed = true;
1609 return false;
1611 else
1613 tree restype;
1614 basic_block bb, then_bb, else_bb, return_bb;
1615 gimple_stmt_iterator bsi;
1616 int nargs = 0;
1617 tree arg;
1618 int i;
1619 tree resdecl;
1620 tree restmp = NULL;
1621 tree resbnd = NULL;
1623 gcall *call;
1624 greturn *ret;
1625 bool alias_is_noreturn = TREE_THIS_VOLATILE (alias);
1627 if (in_lto_p)
1628 get_untransformed_body ();
1629 a = DECL_ARGUMENTS (thunk_fndecl);
1631 current_function_decl = thunk_fndecl;
1633 /* Ensure thunks are emitted in their correct sections. */
1634 resolve_unique_section (thunk_fndecl, 0,
1635 flag_function_sections);
1637 DECL_IGNORED_P (thunk_fndecl) = 1;
1638 bitmap_obstack_initialize (NULL);
1640 if (thunk.virtual_offset_p)
1641 virtual_offset = size_int (virtual_value);
1643 /* Build the return declaration for the function. */
1644 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1645 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1647 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1648 DECL_ARTIFICIAL (resdecl) = 1;
1649 DECL_IGNORED_P (resdecl) = 1;
1650 DECL_RESULT (thunk_fndecl) = resdecl;
1651 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1653 else
1654 resdecl = DECL_RESULT (thunk_fndecl);
1656 bb = then_bb = else_bb = return_bb
1657 = init_lowered_empty_function (thunk_fndecl, true, count);
1659 bsi = gsi_start_bb (bb);
1661 /* Build call to the function being thunked. */
1662 if (!VOID_TYPE_P (restype) && !alias_is_noreturn)
1664 if (DECL_BY_REFERENCE (resdecl))
1666 restmp = gimple_fold_indirect_ref (resdecl);
1667 if (!restmp)
1668 restmp = build2 (MEM_REF,
1669 TREE_TYPE (TREE_TYPE (DECL_RESULT (alias))),
1670 resdecl,
1671 build_int_cst (TREE_TYPE
1672 (DECL_RESULT (alias)), 0));
1674 else if (!is_gimple_reg_type (restype))
1676 if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl)))
1678 restmp = resdecl;
1680 if (TREE_CODE (restmp) == VAR_DECL)
1681 add_local_decl (cfun, restmp);
1682 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1684 else
1685 restmp = create_tmp_var (restype, "retval");
1687 else
1688 restmp = create_tmp_reg (restype, "retval");
1691 for (arg = a; arg; arg = DECL_CHAIN (arg))
1692 nargs++;
1693 auto_vec<tree> vargs (nargs);
1694 i = 0;
1695 arg = a;
1696 if (this_adjusting)
1698 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
1699 virtual_offset));
1700 arg = DECL_CHAIN (a);
1701 i = 1;
1704 if (nargs)
1705 for (; i < nargs; i++, arg = DECL_CHAIN (arg))
1707 tree tmp = arg;
1708 if (!is_gimple_val (arg))
1710 tmp = create_tmp_reg (TYPE_MAIN_VARIANT
1711 (TREE_TYPE (arg)), "arg");
1712 gimple stmt = gimple_build_assign (tmp, arg);
1713 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1715 vargs.quick_push (tmp);
1717 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1718 callees->call_stmt = call;
1719 gimple_call_set_from_thunk (call, true);
1720 gimple_call_set_with_bounds (call, instrumentation_clone);
1722 /* Return slot optimization is always possible and in fact requred to
1723 return values with DECL_BY_REFERENCE. */
1724 if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl))
1725 && (!is_gimple_reg_type (TREE_TYPE (resdecl))
1726 || DECL_BY_REFERENCE (resdecl)))
1727 gimple_call_set_return_slot_opt (call, true);
1729 if (restmp && !alias_is_noreturn)
1731 gimple_call_set_lhs (call, restmp);
1732 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp),
1733 TREE_TYPE (TREE_TYPE (alias))));
1735 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
1736 if (!alias_is_noreturn)
1738 if (instrumentation_clone
1739 && !DECL_BY_REFERENCE (resdecl)
1740 && restmp
1741 && BOUNDED_P (restmp))
1743 resbnd = chkp_insert_retbnd_call (NULL, restmp, &bsi);
1744 create_edge (get_create (gimple_call_fndecl (gsi_stmt (bsi))),
1745 as_a <gcall *> (gsi_stmt (bsi)),
1746 callees->count, callees->frequency);
1749 if (restmp && !this_adjusting
1750 && (fixed_offset || virtual_offset))
1752 tree true_label = NULL_TREE;
1754 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1756 gimple stmt;
1757 edge e;
1758 /* If the return type is a pointer, we need to
1759 protect against NULL. We know there will be an
1760 adjustment, because that's why we're emitting a
1761 thunk. */
1762 then_bb = create_basic_block (NULL, bb);
1763 then_bb->count = count - count / 16;
1764 then_bb->frequency = BB_FREQ_MAX - BB_FREQ_MAX / 16;
1765 return_bb = create_basic_block (NULL, then_bb);
1766 return_bb->count = count;
1767 return_bb->frequency = BB_FREQ_MAX;
1768 else_bb = create_basic_block (NULL, else_bb);
1769 then_bb->count = count / 16;
1770 then_bb->frequency = BB_FREQ_MAX / 16;
1771 add_bb_to_loop (then_bb, bb->loop_father);
1772 add_bb_to_loop (return_bb, bb->loop_father);
1773 add_bb_to_loop (else_bb, bb->loop_father);
1774 remove_edge (single_succ_edge (bb));
1775 true_label = gimple_block_label (then_bb);
1776 stmt = gimple_build_cond (NE_EXPR, restmp,
1777 build_zero_cst (TREE_TYPE (restmp)),
1778 NULL_TREE, NULL_TREE);
1779 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1780 e = make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1781 e->probability = REG_BR_PROB_BASE - REG_BR_PROB_BASE / 16;
1782 e->count = count - count / 16;
1783 e = make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1784 e->probability = REG_BR_PROB_BASE / 16;
1785 e->count = count / 16;
1786 e = make_edge (return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1787 e->probability = REG_BR_PROB_BASE;
1788 e->count = count;
1789 e = make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1790 e->probability = REG_BR_PROB_BASE;
1791 e->count = count - count / 16;
1792 e = make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1793 e->probability = REG_BR_PROB_BASE;
1794 e->count = count / 16;
1795 bsi = gsi_last_bb (then_bb);
1798 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1799 fixed_offset, virtual_offset);
1800 if (true_label)
1802 gimple stmt;
1803 bsi = gsi_last_bb (else_bb);
1804 stmt = gimple_build_assign (restmp,
1805 build_zero_cst (TREE_TYPE (restmp)));
1806 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1807 bsi = gsi_last_bb (return_bb);
1810 else
1811 gimple_call_set_tail (call, true);
1813 /* Build return value. */
1814 if (!DECL_BY_REFERENCE (resdecl))
1815 ret = gimple_build_return (restmp);
1816 else
1817 ret = gimple_build_return (resdecl);
1818 gimple_return_set_retbnd (ret, resbnd);
1820 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1822 else
1824 gimple_call_set_tail (call, true);
1825 remove_edge (single_succ_edge (bb));
1828 cfun->gimple_df->in_ssa_p = true;
1829 profile_status_for_fn (cfun)
1830 = count ? PROFILE_READ : PROFILE_GUESSED;
1831 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
1832 TREE_ASM_WRITTEN (thunk_fndecl) = false;
1833 delete_unreachable_blocks ();
1834 update_ssa (TODO_update_ssa);
1835 #ifdef ENABLE_CHECKING
1836 verify_flow_info ();
1837 #endif
1838 free_dominance_info (CDI_DOMINATORS);
1840 /* Since we want to emit the thunk, we explicitly mark its name as
1841 referenced. */
1842 thunk.thunk_p = false;
1843 lowered = true;
1844 bitmap_obstack_release (NULL);
1846 current_function_decl = NULL;
1847 set_cfun (NULL);
1848 return true;
1851 /* Assemble thunks and aliases associated to node. */
1853 void
1854 cgraph_node::assemble_thunks_and_aliases (void)
1856 cgraph_edge *e;
1857 ipa_ref *ref;
1859 for (e = callers; e;)
1860 if (e->caller->thunk.thunk_p
1861 && !e->caller->thunk.add_pointer_bounds_args)
1863 cgraph_node *thunk = e->caller;
1865 e = e->next_caller;
1866 thunk->expand_thunk (true, false);
1867 thunk->assemble_thunks_and_aliases ();
1869 else
1870 e = e->next_caller;
1872 FOR_EACH_ALIAS (this, ref)
1874 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1875 bool saved_written = TREE_ASM_WRITTEN (decl);
1877 /* Force assemble_alias to really output the alias this time instead
1878 of buffering it in same alias pairs. */
1879 TREE_ASM_WRITTEN (decl) = 1;
1880 do_assemble_alias (alias->decl,
1881 DECL_ASSEMBLER_NAME (decl));
1882 alias->assemble_thunks_and_aliases ();
1883 TREE_ASM_WRITTEN (decl) = saved_written;
1887 /* Expand function specified by node. */
1889 void
1890 cgraph_node::expand (void)
1892 location_t saved_loc;
1894 /* We ought to not compile any inline clones. */
1895 gcc_assert (!global.inlined_to);
1897 announce_function (decl);
1898 process = 0;
1899 gcc_assert (lowered);
1900 get_untransformed_body ();
1902 /* Generate RTL for the body of DECL. */
1904 timevar_push (TV_REST_OF_COMPILATION);
1906 gcc_assert (symtab->global_info_ready);
1908 /* Initialize the default bitmap obstack. */
1909 bitmap_obstack_initialize (NULL);
1911 /* Initialize the RTL code for the function. */
1912 current_function_decl = decl;
1913 saved_loc = input_location;
1914 input_location = DECL_SOURCE_LOCATION (decl);
1915 init_function_start (decl);
1917 gimple_register_cfg_hooks ();
1919 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1921 execute_all_ipa_transforms ();
1923 /* Perform all tree transforms and optimizations. */
1925 /* Signal the start of passes. */
1926 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1928 execute_pass_list (cfun, g->get_passes ()->all_passes);
1930 /* Signal the end of passes. */
1931 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1933 bitmap_obstack_release (&reg_obstack);
1935 /* Release the default bitmap obstack. */
1936 bitmap_obstack_release (NULL);
1938 /* If requested, warn about function definitions where the function will
1939 return a value (usually of some struct or union type) which itself will
1940 take up a lot of stack space. */
1941 if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl))
1943 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
1945 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1946 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1947 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1948 larger_than_size))
1950 unsigned int size_as_int
1951 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1953 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1954 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
1955 decl, size_as_int);
1956 else
1957 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
1958 decl, larger_than_size);
1962 gimple_set_body (decl, NULL);
1963 if (DECL_STRUCT_FUNCTION (decl) == 0
1964 && !cgraph_node::get (decl)->origin)
1966 /* Stop pointing to the local nodes about to be freed.
1967 But DECL_INITIAL must remain nonzero so we know this
1968 was an actual function definition.
1969 For a nested function, this is done in c_pop_function_context.
1970 If rest_of_compilation set this to 0, leave it 0. */
1971 if (DECL_INITIAL (decl) != 0)
1972 DECL_INITIAL (decl) = error_mark_node;
1975 input_location = saved_loc;
1977 ggc_collect ();
1978 timevar_pop (TV_REST_OF_COMPILATION);
1980 /* Make sure that BE didn't give up on compiling. */
1981 gcc_assert (TREE_ASM_WRITTEN (decl));
1982 set_cfun (NULL);
1983 current_function_decl = NULL;
1985 /* It would make a lot more sense to output thunks before function body to get more
1986 forward and lest backwarding jumps. This however would need solving problem
1987 with comdats. See PR48668. Also aliases must come after function itself to
1988 make one pass assemblers, like one on AIX, happy. See PR 50689.
1989 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1990 groups. */
1991 assemble_thunks_and_aliases ();
1992 release_body ();
1993 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1994 points to the dead function body. */
1995 remove_callees ();
1996 remove_all_references ();
1999 /* Node comparer that is responsible for the order that corresponds
2000 to time when a function was launched for the first time. */
2002 static int
2003 node_cmp (const void *pa, const void *pb)
2005 const cgraph_node *a = *(const cgraph_node * const *) pa;
2006 const cgraph_node *b = *(const cgraph_node * const *) pb;
2008 /* Functions with time profile must be before these without profile. */
2009 if (!a->tp_first_run || !b->tp_first_run)
2010 return a->tp_first_run - b->tp_first_run;
2012 return a->tp_first_run != b->tp_first_run
2013 ? b->tp_first_run - a->tp_first_run
2014 : b->order - a->order;
2017 /* Expand all functions that must be output.
2019 Attempt to topologically sort the nodes so function is output when
2020 all called functions are already assembled to allow data to be
2021 propagated across the callgraph. Use a stack to get smaller distance
2022 between a function and its callees (later we may choose to use a more
2023 sophisticated algorithm for function reordering; we will likely want
2024 to use subsections to make the output functions appear in top-down
2025 order). */
2027 static void
2028 expand_all_functions (void)
2030 cgraph_node *node;
2031 cgraph_node **order = XCNEWVEC (cgraph_node *,
2032 symtab->cgraph_count);
2033 unsigned int expanded_func_count = 0, profiled_func_count = 0;
2034 int order_pos, new_order_pos = 0;
2035 int i;
2037 order_pos = ipa_reverse_postorder (order);
2038 gcc_assert (order_pos == symtab->cgraph_count);
2040 /* Garbage collector may remove inline clones we eliminate during
2041 optimization. So we must be sure to not reference them. */
2042 for (i = 0; i < order_pos; i++)
2043 if (order[i]->process)
2044 order[new_order_pos++] = order[i];
2046 if (flag_profile_reorder_functions)
2047 qsort (order, new_order_pos, sizeof (cgraph_node *), node_cmp);
2049 for (i = new_order_pos - 1; i >= 0; i--)
2051 node = order[i];
2053 if (node->process)
2055 expanded_func_count++;
2056 if(node->tp_first_run)
2057 profiled_func_count++;
2059 if (symtab->dump_file)
2060 fprintf (symtab->dump_file,
2061 "Time profile order in expand_all_functions:%s:%d\n",
2062 node->asm_name (), node->tp_first_run);
2063 node->process = 0;
2064 node->expand ();
2068 if (dump_file)
2069 fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
2070 main_input_filename, profiled_func_count, expanded_func_count);
2072 if (symtab->dump_file && flag_profile_reorder_functions)
2073 fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n",
2074 profiled_func_count, expanded_func_count);
2076 symtab->process_new_functions ();
2077 free_gimplify_stack ();
2079 free (order);
2082 /* This is used to sort the node types by the cgraph order number. */
2084 enum cgraph_order_sort_kind
2086 ORDER_UNDEFINED = 0,
2087 ORDER_FUNCTION,
2088 ORDER_VAR,
2089 ORDER_ASM
2092 struct cgraph_order_sort
2094 enum cgraph_order_sort_kind kind;
2095 union
2097 cgraph_node *f;
2098 varpool_node *v;
2099 asm_node *a;
2100 } u;
2103 /* Output all functions, variables, and asm statements in the order
2104 according to their order fields, which is the order in which they
2105 appeared in the file. This implements -fno-toplevel-reorder. In
2106 this mode we may output functions and variables which don't really
2107 need to be output.
2108 When NO_REORDER is true only do this for symbols marked no reorder. */
2110 static void
2111 output_in_order (bool no_reorder)
2113 int max;
2114 cgraph_order_sort *nodes;
2115 int i;
2116 cgraph_node *pf;
2117 varpool_node *pv;
2118 asm_node *pa;
2119 max = symtab->order;
2120 nodes = XCNEWVEC (cgraph_order_sort, max);
2122 FOR_EACH_DEFINED_FUNCTION (pf)
2124 if (pf->process && !pf->thunk.thunk_p && !pf->alias)
2126 if (no_reorder && !pf->no_reorder)
2127 continue;
2128 i = pf->order;
2129 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2130 nodes[i].kind = ORDER_FUNCTION;
2131 nodes[i].u.f = pf;
2135 FOR_EACH_DEFINED_VARIABLE (pv)
2136 if (!DECL_EXTERNAL (pv->decl))
2138 if (no_reorder && !pv->no_reorder)
2139 continue;
2140 i = pv->order;
2141 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2142 nodes[i].kind = ORDER_VAR;
2143 nodes[i].u.v = pv;
2146 for (pa = symtab->first_asm_symbol (); pa; pa = pa->next)
2148 i = pa->order;
2149 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2150 nodes[i].kind = ORDER_ASM;
2151 nodes[i].u.a = pa;
2154 /* In toplevel reorder mode we output all statics; mark them as needed. */
2156 for (i = 0; i < max; ++i)
2157 if (nodes[i].kind == ORDER_VAR)
2158 nodes[i].u.v->finalize_named_section_flags ();
2160 for (i = 0; i < max; ++i)
2162 switch (nodes[i].kind)
2164 case ORDER_FUNCTION:
2165 nodes[i].u.f->process = 0;
2166 nodes[i].u.f->expand ();
2167 break;
2169 case ORDER_VAR:
2170 nodes[i].u.v->assemble_decl ();
2171 break;
2173 case ORDER_ASM:
2174 assemble_asm (nodes[i].u.a->asm_str);
2175 break;
2177 case ORDER_UNDEFINED:
2178 break;
2180 default:
2181 gcc_unreachable ();
2185 symtab->clear_asm_symbols ();
2187 free (nodes);
2190 static void
2191 ipa_passes (void)
2193 gcc::pass_manager *passes = g->get_passes ();
2195 set_cfun (NULL);
2196 current_function_decl = NULL;
2197 gimple_register_cfg_hooks ();
2198 bitmap_obstack_initialize (NULL);
2200 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2202 if (!in_lto_p)
2204 execute_ipa_pass_list (passes->all_small_ipa_passes);
2205 if (seen_error ())
2206 return;
2209 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2210 devirtualization and other changes where removal iterate. */
2211 symtab->remove_unreachable_nodes (symtab->dump_file);
2213 /* If pass_all_early_optimizations was not scheduled, the state of
2214 the cgraph will not be properly updated. Update it now. */
2215 if (symtab->state < IPA_SSA)
2216 symtab->state = IPA_SSA;
2218 if (!in_lto_p)
2220 /* Generate coverage variables and constructors. */
2221 coverage_finish ();
2223 /* Process new functions added. */
2224 set_cfun (NULL);
2225 current_function_decl = NULL;
2226 symtab->process_new_functions ();
2228 execute_ipa_summary_passes
2229 ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2232 /* Some targets need to handle LTO assembler output specially. */
2233 if (flag_generate_lto || flag_generate_offload)
2234 targetm.asm_out.lto_start ();
2236 if (!in_lto_p)
2238 if (g->have_offload)
2240 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2241 lto_stream_offload_p = true;
2242 ipa_write_summaries ();
2243 lto_stream_offload_p = false;
2245 if (flag_lto)
2247 section_name_prefix = LTO_SECTION_NAME_PREFIX;
2248 lto_stream_offload_p = false;
2249 ipa_write_summaries ();
2253 if (flag_generate_lto || flag_generate_offload)
2254 targetm.asm_out.lto_end ();
2256 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
2257 execute_ipa_pass_list (passes->all_regular_ipa_passes);
2258 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2260 bitmap_obstack_release (NULL);
2264 /* Return string alias is alias of. */
2266 static tree
2267 get_alias_symbol (tree decl)
2269 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
2270 return get_identifier (TREE_STRING_POINTER
2271 (TREE_VALUE (TREE_VALUE (alias))));
2275 /* Weakrefs may be associated to external decls and thus not output
2276 at expansion time. Emit all necessary aliases. */
2278 void
2279 symbol_table::output_weakrefs (void)
2281 symtab_node *node;
2282 cgraph_node *cnode;
2283 FOR_EACH_SYMBOL (node)
2284 if (node->alias
2285 && !TREE_ASM_WRITTEN (node->decl)
2286 && (!(cnode = dyn_cast <cgraph_node *> (node))
2287 || !cnode->instrumented_version
2288 || !TREE_ASM_WRITTEN (cnode->instrumented_version->decl))
2289 && node->weakref)
2291 tree target;
2293 /* Weakrefs are special by not requiring target definition in current
2294 compilation unit. It is thus bit hard to work out what we want to
2295 alias.
2296 When alias target is defined, we need to fetch it from symtab reference,
2297 otherwise it is pointed to by alias_target. */
2298 if (node->alias_target)
2299 target = (DECL_P (node->alias_target)
2300 ? DECL_ASSEMBLER_NAME (node->alias_target)
2301 : node->alias_target);
2302 else if (node->analyzed)
2303 target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
2304 else
2306 gcc_unreachable ();
2307 target = get_alias_symbol (node->decl);
2309 do_assemble_alias (node->decl, target);
2313 /* Perform simple optimizations based on callgraph. */
2315 void
2316 symbol_table::compile (void)
2318 if (seen_error ())
2319 return;
2321 #ifdef ENABLE_CHECKING
2322 symtab_node::verify_symtab_nodes ();
2323 #endif
2325 timevar_push (TV_CGRAPHOPT);
2326 if (pre_ipa_mem_report)
2328 fprintf (stderr, "Memory consumption before IPA\n");
2329 dump_memory_report (false);
2331 if (!quiet_flag)
2332 fprintf (stderr, "Performing interprocedural optimizations\n");
2333 state = IPA;
2335 /* Offloading requires LTO infrastructure. */
2336 if (!in_lto_p && g->have_offload)
2337 flag_generate_offload = 1;
2339 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2340 if (flag_generate_lto || flag_generate_offload)
2341 lto_streamer_hooks_init ();
2343 /* Don't run the IPA passes if there was any error or sorry messages. */
2344 if (!seen_error ())
2345 ipa_passes ();
2347 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2348 if (seen_error ()
2349 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
2351 timevar_pop (TV_CGRAPHOPT);
2352 return;
2355 global_info_ready = true;
2356 if (dump_file)
2358 fprintf (dump_file, "Optimized ");
2359 symtab_node:: dump_table (dump_file);
2361 if (post_ipa_mem_report)
2363 fprintf (stderr, "Memory consumption after IPA\n");
2364 dump_memory_report (false);
2366 timevar_pop (TV_CGRAPHOPT);
2368 /* Output everything. */
2369 (*debug_hooks->assembly_start) ();
2370 if (!quiet_flag)
2371 fprintf (stderr, "Assembling functions:\n");
2372 #ifdef ENABLE_CHECKING
2373 symtab_node::verify_symtab_nodes ();
2374 #endif
2376 materialize_all_clones ();
2377 bitmap_obstack_initialize (NULL);
2378 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2379 bitmap_obstack_release (NULL);
2380 mark_functions_to_output ();
2382 /* When weakref support is missing, we autmatically translate all
2383 references to NODE to references to its ultimate alias target.
2384 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2385 TREE_CHAIN.
2387 Set up this mapping before we output any assembler but once we are sure
2388 that all symbol renaming is done.
2390 FIXME: All this uglyness can go away if we just do renaming at gimple
2391 level by physically rewritting the IL. At the moment we can only redirect
2392 calls, so we need infrastructure for renaming references as well. */
2393 #ifndef ASM_OUTPUT_WEAKREF
2394 symtab_node *node;
2396 FOR_EACH_SYMBOL (node)
2397 if (node->alias
2398 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2400 IDENTIFIER_TRANSPARENT_ALIAS
2401 (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2402 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2403 = (node->alias_target ? node->alias_target
2404 : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
2406 #endif
2408 state = EXPANSION;
2410 if (!flag_toplevel_reorder)
2411 output_in_order (false);
2412 else
2414 /* Output first asm statements and anything ordered. The process
2415 flag is cleared for these nodes, so we skip them later. */
2416 output_in_order (true);
2417 expand_all_functions ();
2418 output_variables ();
2421 process_new_functions ();
2422 state = FINISHED;
2423 output_weakrefs ();
2425 if (dump_file)
2427 fprintf (dump_file, "\nFinal ");
2428 symtab_node::dump_table (dump_file);
2430 #ifdef ENABLE_CHECKING
2431 symtab_node::verify_symtab_nodes ();
2432 /* Double check that all inline clones are gone and that all
2433 function bodies have been released from memory. */
2434 if (!seen_error ())
2436 cgraph_node *node;
2437 bool error_found = false;
2439 FOR_EACH_DEFINED_FUNCTION (node)
2440 if (node->global.inlined_to
2441 || gimple_has_body_p (node->decl))
2443 error_found = true;
2444 node->debug ();
2446 if (error_found)
2447 internal_error ("nodes with unreleased memory found");
2449 #endif
2453 /* Analyze the whole compilation unit once it is parsed completely. */
2455 void
2456 symbol_table::finalize_compilation_unit (void)
2458 timevar_push (TV_CGRAPH);
2460 /* If we're here there's no current function anymore. Some frontends
2461 are lazy in clearing these. */
2462 current_function_decl = NULL;
2463 set_cfun (NULL);
2465 /* Do not skip analyzing the functions if there were errors, we
2466 miss diagnostics for following functions otherwise. */
2468 /* Emit size functions we didn't inline. */
2469 finalize_size_functions ();
2471 /* Mark alias targets necessary and emit diagnostics. */
2472 handle_alias_pairs ();
2474 if (!quiet_flag)
2476 fprintf (stderr, "\nAnalyzing compilation unit\n");
2477 fflush (stderr);
2480 if (flag_dump_passes)
2481 dump_passes ();
2483 /* Gimplify and lower all functions, compute reachability and
2484 remove unreachable nodes. */
2485 analyze_functions (/*first_time=*/true);
2487 /* Mark alias targets necessary and emit diagnostics. */
2488 handle_alias_pairs ();
2490 /* Gimplify and lower thunks. */
2491 analyze_functions (/*first_time=*/false);
2493 /* Emit early debug for reachable functions, and by consequence,
2494 locally scoped symbols. */
2495 struct cgraph_node *cnode;
2496 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (cnode)
2497 (*debug_hooks->early_global_decl) (cnode->decl);
2499 /* Clean up anything that needs cleaning up after initial debug
2500 generation. */
2501 (*debug_hooks->early_finish) ();
2503 /* Finally drive the pass manager. */
2504 compile ();
2506 timevar_pop (TV_CGRAPH);
2509 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2510 within the same process. For use by toplev::finalize. */
2512 void
2513 cgraphunit_c_finalize (void)
2515 gcc_assert (cgraph_new_nodes.length () == 0);
2516 cgraph_new_nodes.truncate (0);
2518 vtable_entry_type = NULL;
2519 queued_nodes = &symtab_terminator;
2521 first_analyzed = NULL;
2522 first_analyzed_var = NULL;
2525 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2526 kind of wrapper method. */
2528 void
2529 cgraph_node::create_wrapper (cgraph_node *target)
2531 /* Preserve DECL_RESULT so we get right by reference flag. */
2532 tree decl_result = DECL_RESULT (decl);
2534 /* Remove the function's body but keep arguments to be reused
2535 for thunk. */
2536 release_body (true);
2537 reset ();
2539 DECL_UNINLINABLE (decl) = false;
2540 DECL_RESULT (decl) = decl_result;
2541 DECL_INITIAL (decl) = NULL;
2542 allocate_struct_function (decl, false);
2543 set_cfun (NULL);
2545 /* Turn alias into thunk and expand it into GIMPLE representation. */
2546 definition = true;
2548 memset (&thunk, 0, sizeof (cgraph_thunk_info));
2549 thunk.thunk_p = true;
2550 create_edge (target, NULL, count, CGRAPH_FREQ_BASE);
2552 tree arguments = DECL_ARGUMENTS (decl);
2554 while (arguments)
2556 TREE_ADDRESSABLE (arguments) = false;
2557 arguments = TREE_CHAIN (arguments);
2560 expand_thunk (false, true);
2562 /* Inline summary set-up. */
2563 analyze ();
2564 inline_analyze_function (this);
2567 #include "gt-cgraphunit.h"