S/390: Testsuite: Add asm scan patterns for -m31.
[official-gcc.git] / gcc / cgraphunit.c
blobf73d9a78e0303257afadc9aa10c5a3a7b5588216
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 "backend.h"
164 #include "target.h"
165 #include "rtl.h"
166 #include "tree.h"
167 #include "gimple.h"
168 #include "cfghooks.h"
169 #include "regset.h" /* FIXME: For reg_obstack. */
170 #include "alloc-pool.h"
171 #include "tree-pass.h"
172 #include "stringpool.h"
173 #include "gimple-ssa.h"
174 #include "cgraph.h"
175 #include "coverage.h"
176 #include "lto-streamer.h"
177 #include "fold-const.h"
178 #include "varasm.h"
179 #include "stor-layout.h"
180 #include "output.h"
181 #include "cfgcleanup.h"
182 #include "gimple-fold.h"
183 #include "gimplify.h"
184 #include "gimple-iterator.h"
185 #include "gimplify-me.h"
186 #include "tree-cfg.h"
187 #include "tree-into-ssa.h"
188 #include "tree-ssa.h"
189 #include "langhooks.h"
190 #include "toplev.h"
191 #include "debug.h"
192 #include "symbol-summary.h"
193 #include "ipa-prop.h"
194 #include "gimple-pretty-print.h"
195 #include "plugin.h"
196 #include "ipa-inline.h"
197 #include "ipa-utils.h"
198 #include "except.h"
199 #include "cfgloop.h"
200 #include "context.h"
201 #include "pass_manager.h"
202 #include "tree-nested.h"
203 #include "dbgcnt.h"
204 #include "tree-chkp.h"
205 #include "lto-section-names.h"
207 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
208 secondary queue used during optimization to accommodate passes that
209 may generate new functions that need to be optimized and expanded. */
210 vec<cgraph_node *> cgraph_new_nodes;
212 static void expand_all_functions (void);
213 static void mark_functions_to_output (void);
214 static void handle_alias_pairs (void);
216 /* Used for vtable lookup in thunk adjusting. */
217 static GTY (()) tree vtable_entry_type;
219 /* Determine if symbol declaration is needed. That is, visible to something
220 either outside this translation unit, something magic in the system
221 configury */
222 bool
223 symtab_node::needed_p (void)
225 /* Double check that no one output the function into assembly file
226 early. */
227 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)
228 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
230 if (!definition)
231 return false;
233 if (DECL_EXTERNAL (decl))
234 return false;
236 /* If the user told us it is used, then it must be so. */
237 if (force_output)
238 return true;
240 /* ABI forced symbols are needed when they are external. */
241 if (forced_by_abi && TREE_PUBLIC (decl))
242 return true;
244 /* Keep constructors, destructors and virtual functions. */
245 if (TREE_CODE (decl) == FUNCTION_DECL
246 && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)))
247 return true;
249 /* Externally visible variables must be output. The exception is
250 COMDAT variables that must be output only when they are needed. */
251 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
252 return true;
254 return false;
257 /* Head and terminator of the queue of nodes to be processed while building
258 callgraph. */
260 static symtab_node symtab_terminator;
261 static symtab_node *queued_nodes = &symtab_terminator;
263 /* Add NODE to queue starting at QUEUED_NODES.
264 The queue is linked via AUX pointers and terminated by pointer to 1. */
266 static void
267 enqueue_node (symtab_node *node)
269 if (node->aux)
270 return;
271 gcc_checking_assert (queued_nodes);
272 node->aux = queued_nodes;
273 queued_nodes = node;
276 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
277 functions into callgraph in a way so they look like ordinary reachable
278 functions inserted into callgraph already at construction time. */
280 void
281 symbol_table::process_new_functions (void)
283 tree fndecl;
285 if (!cgraph_new_nodes.exists ())
286 return;
288 handle_alias_pairs ();
289 /* Note that this queue may grow as its being processed, as the new
290 functions may generate new ones. */
291 for (unsigned i = 0; i < cgraph_new_nodes.length (); i++)
293 cgraph_node *node = cgraph_new_nodes[i];
294 fndecl = node->decl;
295 switch (state)
297 case CONSTRUCTION:
298 /* At construction time we just need to finalize function and move
299 it into reachable functions list. */
301 cgraph_node::finalize_function (fndecl, false);
302 call_cgraph_insertion_hooks (node);
303 enqueue_node (node);
304 break;
306 case IPA:
307 case IPA_SSA:
308 case IPA_SSA_AFTER_INLINING:
309 /* When IPA optimization already started, do all essential
310 transformations that has been already performed on the whole
311 cgraph but not on this function. */
313 gimple_register_cfg_hooks ();
314 if (!node->analyzed)
315 node->analyze ();
316 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
317 if ((state == IPA_SSA || state == IPA_SSA_AFTER_INLINING)
318 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
319 g->get_passes ()->execute_early_local_passes ();
320 else if (inline_summaries != NULL)
321 compute_inline_parameters (node, true);
322 free_dominance_info (CDI_POST_DOMINATORS);
323 free_dominance_info (CDI_DOMINATORS);
324 pop_cfun ();
325 call_cgraph_insertion_hooks (node);
326 break;
328 case EXPANSION:
329 /* Functions created during expansion shall be compiled
330 directly. */
331 node->process = 0;
332 call_cgraph_insertion_hooks (node);
333 node->expand ();
334 break;
336 default:
337 gcc_unreachable ();
338 break;
342 cgraph_new_nodes.release ();
345 /* As an GCC extension we allow redefinition of the function. The
346 semantics when both copies of bodies differ is not well defined.
347 We replace the old body with new body so in unit at a time mode
348 we always use new body, while in normal mode we may end up with
349 old body inlined into some functions and new body expanded and
350 inlined in others.
352 ??? It may make more sense to use one body for inlining and other
353 body for expanding the function but this is difficult to do. */
355 void
356 cgraph_node::reset (void)
358 /* If process is set, then we have already begun whole-unit analysis.
359 This is *not* testing for whether we've already emitted the function.
360 That case can be sort-of legitimately seen with real function redefinition
361 errors. I would argue that the front end should never present us with
362 such a case, but don't enforce that for now. */
363 gcc_assert (!process);
365 /* Reset our data structures so we can analyze the function again. */
366 memset (&local, 0, sizeof (local));
367 memset (&global, 0, sizeof (global));
368 memset (&rtl, 0, sizeof (rtl));
369 analyzed = false;
370 definition = false;
371 alias = false;
372 weakref = false;
373 cpp_implicit_alias = false;
375 remove_callees ();
376 remove_all_references ();
379 /* Return true when there are references to the node. INCLUDE_SELF is
380 true if a self reference counts as a reference. */
382 bool
383 symtab_node::referred_to_p (bool include_self)
385 ipa_ref *ref = NULL;
387 /* See if there are any references at all. */
388 if (iterate_referring (0, ref))
389 return true;
390 /* For functions check also calls. */
391 cgraph_node *cn = dyn_cast <cgraph_node *> (this);
392 if (cn && cn->callers)
394 if (include_self)
395 return true;
396 for (cgraph_edge *e = cn->callers; e; e = e->next_caller)
397 if (e->caller != this)
398 return true;
400 return false;
403 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
404 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
405 the garbage collector run at the moment. We would need to either create
406 a new GC context, or just not compile right now. */
408 void
409 cgraph_node::finalize_function (tree decl, bool no_collect)
411 cgraph_node *node = cgraph_node::get_create (decl);
413 if (node->definition)
415 /* Nested functions should only be defined once. */
416 gcc_assert (!DECL_CONTEXT (decl)
417 || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL);
418 node->reset ();
419 node->local.redefined_extern_inline = true;
422 /* Set definition first before calling notice_global_symbol so that
423 it is available to notice_global_symbol. */
424 node->definition = true;
425 notice_global_symbol (decl);
426 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
428 /* With -fkeep-inline-functions we are keeping all inline functions except
429 for extern inline ones. */
430 if (flag_keep_inline_functions
431 && DECL_DECLARED_INLINE_P (decl)
432 && !DECL_EXTERNAL (decl)
433 && !DECL_DISREGARD_INLINE_LIMITS (decl))
434 node->force_output = 1;
436 /* When not optimizing, also output the static functions. (see
437 PR24561), but don't do so for always_inline functions, functions
438 declared inline and nested functions. These were optimized out
439 in the original implementation and it is unclear whether we want
440 to change the behavior here. */
441 if (((!opt_for_fn (decl, optimize) || flag_keep_static_functions)
442 && !node->cpp_implicit_alias
443 && !DECL_DISREGARD_INLINE_LIMITS (decl)
444 && !DECL_DECLARED_INLINE_P (decl)
445 && !(DECL_CONTEXT (decl)
446 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
447 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
448 node->force_output = 1;
450 /* If we've not yet emitted decl, tell the debug info about it. */
451 if (!TREE_ASM_WRITTEN (decl))
452 (*debug_hooks->deferred_inline_function) (decl);
454 if (!no_collect)
455 ggc_collect ();
457 if (symtab->state == CONSTRUCTION
458 && (node->needed_p () || node->referred_to_p ()))
459 enqueue_node (node);
462 /* Add the function FNDECL to the call graph.
463 Unlike finalize_function, this function is intended to be used
464 by middle end and allows insertion of new function at arbitrary point
465 of compilation. The function can be either in high, low or SSA form
466 GIMPLE.
468 The function is assumed to be reachable and have address taken (so no
469 API breaking optimizations are performed on it).
471 Main work done by this function is to enqueue the function for later
472 processing to avoid need the passes to be re-entrant. */
474 void
475 cgraph_node::add_new_function (tree fndecl, bool lowered)
477 gcc::pass_manager *passes = g->get_passes ();
478 cgraph_node *node;
480 if (dump_file)
482 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
483 const char *function_type = ((gimple_has_body_p (fndecl))
484 ? (lowered
485 ? (gimple_in_ssa_p (fn)
486 ? "ssa gimple"
487 : "low gimple")
488 : "high gimple")
489 : "to-be-gimplified");
490 fprintf (dump_file,
491 "Added new %s function %s to callgraph\n",
492 function_type,
493 fndecl_name (fndecl));
496 switch (symtab->state)
498 case PARSING:
499 cgraph_node::finalize_function (fndecl, false);
500 break;
501 case CONSTRUCTION:
502 /* Just enqueue function to be processed at nearest occurrence. */
503 node = cgraph_node::get_create (fndecl);
504 if (lowered)
505 node->lowered = true;
506 cgraph_new_nodes.safe_push (node);
507 break;
509 case IPA:
510 case IPA_SSA:
511 case IPA_SSA_AFTER_INLINING:
512 case EXPANSION:
513 /* Bring the function into finalized state and enqueue for later
514 analyzing and compilation. */
515 node = cgraph_node::get_create (fndecl);
516 node->local.local = false;
517 node->definition = true;
518 node->force_output = true;
519 if (!lowered && symtab->state == EXPANSION)
521 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
522 gimple_register_cfg_hooks ();
523 bitmap_obstack_initialize (NULL);
524 execute_pass_list (cfun, passes->all_lowering_passes);
525 passes->execute_early_local_passes ();
526 bitmap_obstack_release (NULL);
527 pop_cfun ();
529 lowered = true;
531 if (lowered)
532 node->lowered = true;
533 cgraph_new_nodes.safe_push (node);
534 break;
536 case FINISHED:
537 /* At the very end of compilation we have to do all the work up
538 to expansion. */
539 node = cgraph_node::create (fndecl);
540 if (lowered)
541 node->lowered = true;
542 node->definition = true;
543 node->analyze ();
544 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
545 gimple_register_cfg_hooks ();
546 bitmap_obstack_initialize (NULL);
547 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
548 g->get_passes ()->execute_early_local_passes ();
549 bitmap_obstack_release (NULL);
550 pop_cfun ();
551 node->expand ();
552 break;
554 default:
555 gcc_unreachable ();
558 /* Set a personality if required and we already passed EH lowering. */
559 if (lowered
560 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
561 == eh_personality_lang))
562 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
565 /* Analyze the function scheduled to be output. */
566 void
567 cgraph_node::analyze (void)
569 tree decl = this->decl;
570 location_t saved_loc = input_location;
571 input_location = DECL_SOURCE_LOCATION (decl);
573 if (thunk.thunk_p)
575 cgraph_node *t = cgraph_node::get (thunk.alias);
577 create_edge (t, NULL, 0, CGRAPH_FREQ_BASE);
578 /* Target code in expand_thunk may need the thunk's target
579 to be analyzed, so recurse here. */
580 if (!t->analyzed)
581 t->analyze ();
582 if (t->alias)
584 t = t->get_alias_target ();
585 if (!t->analyzed)
586 t->analyze ();
588 if (!expand_thunk (false, false))
590 thunk.alias = NULL;
591 return;
593 thunk.alias = NULL;
595 if (alias)
596 resolve_alias (cgraph_node::get (alias_target));
597 else if (dispatcher_function)
599 /* Generate the dispatcher body of multi-versioned functions. */
600 cgraph_function_version_info *dispatcher_version_info
601 = function_version ();
602 if (dispatcher_version_info != NULL
603 && (dispatcher_version_info->dispatcher_resolver
604 == NULL_TREE))
606 tree resolver = NULL_TREE;
607 gcc_assert (targetm.generate_version_dispatcher_body);
608 resolver = targetm.generate_version_dispatcher_body (this);
609 gcc_assert (resolver != NULL_TREE);
612 else
614 push_cfun (DECL_STRUCT_FUNCTION (decl));
616 assign_assembler_name_if_neeeded (decl);
618 /* Make sure to gimplify bodies only once. During analyzing a
619 function we lower it, which will require gimplified nested
620 functions, so we can end up here with an already gimplified
621 body. */
622 if (!gimple_has_body_p (decl))
623 gimplify_function_tree (decl);
625 /* Lower the function. */
626 if (!lowered)
628 if (nested)
629 lower_nested_functions (decl);
630 gcc_assert (!nested);
632 gimple_register_cfg_hooks ();
633 bitmap_obstack_initialize (NULL);
634 execute_pass_list (cfun, g->get_passes ()->all_lowering_passes);
635 free_dominance_info (CDI_POST_DOMINATORS);
636 free_dominance_info (CDI_DOMINATORS);
637 compact_blocks ();
638 bitmap_obstack_release (NULL);
639 lowered = true;
642 pop_cfun ();
644 analyzed = true;
646 input_location = saved_loc;
649 /* C++ frontend produce same body aliases all over the place, even before PCH
650 gets streamed out. It relies on us linking the aliases with their function
651 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
652 first produce aliases without links, but once C++ FE is sure he won't sream
653 PCH we build the links via this function. */
655 void
656 symbol_table::process_same_body_aliases (void)
658 symtab_node *node;
659 FOR_EACH_SYMBOL (node)
660 if (node->cpp_implicit_alias && !node->analyzed)
661 node->resolve_alias
662 (TREE_CODE (node->alias_target) == VAR_DECL
663 ? (symtab_node *)varpool_node::get_create (node->alias_target)
664 : (symtab_node *)cgraph_node::get_create (node->alias_target));
665 cpp_implicit_aliases_done = true;
668 /* Process attributes common for vars and functions. */
670 static void
671 process_common_attributes (symtab_node *node, tree decl)
673 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
675 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
677 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
678 "%<weakref%> attribute should be accompanied with"
679 " an %<alias%> attribute");
680 DECL_WEAK (decl) = 0;
681 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
682 DECL_ATTRIBUTES (decl));
685 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
686 node->no_reorder = 1;
689 /* Look for externally_visible and used attributes and mark cgraph nodes
690 accordingly.
692 We cannot mark the nodes at the point the attributes are processed (in
693 handle_*_attribute) because the copy of the declarations available at that
694 point may not be canonical. For example, in:
696 void f();
697 void f() __attribute__((used));
699 the declaration we see in handle_used_attribute will be the second
700 declaration -- but the front end will subsequently merge that declaration
701 with the original declaration and discard the second declaration.
703 Furthermore, we can't mark these nodes in finalize_function because:
705 void f() {}
706 void f() __attribute__((externally_visible));
708 is valid.
710 So, we walk the nodes at the end of the translation unit, applying the
711 attributes at that point. */
713 static void
714 process_function_and_variable_attributes (cgraph_node *first,
715 varpool_node *first_var)
717 cgraph_node *node;
718 varpool_node *vnode;
720 for (node = symtab->first_function (); node != first;
721 node = symtab->next_function (node))
723 tree decl = node->decl;
724 if (DECL_PRESERVE_P (decl))
725 node->mark_force_output ();
726 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
728 if (! TREE_PUBLIC (node->decl))
729 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
730 "%<externally_visible%>"
731 " attribute have effect only on public objects");
733 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
734 && (node->definition && !node->alias))
736 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
737 "%<weakref%> attribute ignored"
738 " because function is defined");
739 DECL_WEAK (decl) = 0;
740 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
741 DECL_ATTRIBUTES (decl));
744 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
745 && !DECL_DECLARED_INLINE_P (decl)
746 /* redefining extern inline function makes it DECL_UNINLINABLE. */
747 && !DECL_UNINLINABLE (decl))
748 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
749 "always_inline function might not be inlinable");
751 process_common_attributes (node, decl);
753 for (vnode = symtab->first_variable (); vnode != first_var;
754 vnode = symtab->next_variable (vnode))
756 tree decl = vnode->decl;
757 if (DECL_EXTERNAL (decl)
758 && DECL_INITIAL (decl))
759 varpool_node::finalize_decl (decl);
760 if (DECL_PRESERVE_P (decl))
761 vnode->force_output = true;
762 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
764 if (! TREE_PUBLIC (vnode->decl))
765 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
766 "%<externally_visible%>"
767 " attribute have effect only on public objects");
769 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
770 && vnode->definition
771 && DECL_INITIAL (decl))
773 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
774 "%<weakref%> attribute ignored"
775 " because variable is initialized");
776 DECL_WEAK (decl) = 0;
777 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
778 DECL_ATTRIBUTES (decl));
780 process_common_attributes (vnode, decl);
784 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
785 middle end to output the variable to asm file, if needed or externally
786 visible. */
788 void
789 varpool_node::finalize_decl (tree decl)
791 varpool_node *node = varpool_node::get_create (decl);
793 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
795 if (node->definition)
796 return;
797 /* Set definition first before calling notice_global_symbol so that
798 it is available to notice_global_symbol. */
799 node->definition = true;
800 notice_global_symbol (decl);
801 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
802 /* Traditionally we do not eliminate static variables when not
803 optimizing and when not doing toplevel reoder. */
804 || node->no_reorder
805 || ((!flag_toplevel_reorder
806 && !DECL_COMDAT (node->decl)
807 && !DECL_ARTIFICIAL (node->decl))))
808 node->force_output = true;
810 if (symtab->state == CONSTRUCTION
811 && (node->needed_p () || node->referred_to_p ()))
812 enqueue_node (node);
813 if (symtab->state >= IPA_SSA)
814 node->analyze ();
815 /* Some frontends produce various interface variables after compilation
816 finished. */
817 if (symtab->state == FINISHED
818 || (!flag_toplevel_reorder
819 && symtab->state == EXPANSION))
820 node->assemble_decl ();
822 if (DECL_INITIAL (decl))
823 chkp_register_var_initializer (decl);
826 /* EDGE is an polymorphic call. Mark all possible targets as reachable
827 and if there is only one target, perform trivial devirtualization.
828 REACHABLE_CALL_TARGETS collects target lists we already walked to
829 avoid udplicate work. */
831 static void
832 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
833 cgraph_edge *edge)
835 unsigned int i;
836 void *cache_token;
837 bool final;
838 vec <cgraph_node *>targets
839 = possible_polymorphic_call_targets
840 (edge, &final, &cache_token);
842 if (!reachable_call_targets->add (cache_token))
844 if (symtab->dump_file)
845 dump_possible_polymorphic_call_targets
846 (symtab->dump_file, edge);
848 for (i = 0; i < targets.length (); i++)
850 /* Do not bother to mark virtual methods in anonymous namespace;
851 either we will find use of virtual table defining it, or it is
852 unused. */
853 if (targets[i]->definition
854 && TREE_CODE
855 (TREE_TYPE (targets[i]->decl))
856 == METHOD_TYPE
857 && !type_in_anonymous_namespace_p
858 (TYPE_METHOD_BASETYPE (TREE_TYPE (targets[i]->decl))))
859 enqueue_node (targets[i]);
863 /* Very trivial devirtualization; when the type is
864 final or anonymous (so we know all its derivation)
865 and there is only one possible virtual call target,
866 make the edge direct. */
867 if (final)
869 if (targets.length () <= 1 && dbg_cnt (devirt))
871 cgraph_node *target;
872 if (targets.length () == 1)
873 target = targets[0];
874 else
875 target = cgraph_node::create
876 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
878 if (symtab->dump_file)
880 fprintf (symtab->dump_file,
881 "Devirtualizing call: ");
882 print_gimple_stmt (symtab->dump_file,
883 edge->call_stmt, 0,
884 TDF_SLIM);
886 if (dump_enabled_p ())
888 location_t locus = gimple_location_safe (edge->call_stmt);
889 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
890 "devirtualizing call in %s to %s\n",
891 edge->caller->name (), target->name ());
894 edge->make_direct (target);
895 edge->redirect_call_stmt_to_callee ();
897 /* Call to __builtin_unreachable shouldn't be instrumented. */
898 if (!targets.length ())
899 gimple_call_set_with_bounds (edge->call_stmt, false);
901 if (symtab->dump_file)
903 fprintf (symtab->dump_file,
904 "Devirtualized as: ");
905 print_gimple_stmt (symtab->dump_file,
906 edge->call_stmt, 0,
907 TDF_SLIM);
913 /* Issue appropriate warnings for the global declaration DECL. */
915 static void
916 check_global_declaration (symtab_node *snode)
918 tree decl = snode->decl;
920 /* Warn about any function declared static but not defined. We don't
921 warn about variables, because many programs have static variables
922 that exist only to get some text into the object file. */
923 if (TREE_CODE (decl) == FUNCTION_DECL
924 && DECL_INITIAL (decl) == 0
925 && DECL_EXTERNAL (decl)
926 && ! DECL_ARTIFICIAL (decl)
927 && ! TREE_NO_WARNING (decl)
928 && ! TREE_PUBLIC (decl)
929 && (warn_unused_function
930 || snode->referred_to_p (/*include_self=*/false)))
932 if (snode->referred_to_p (/*include_self=*/false))
933 pedwarn (input_location, 0, "%q+F used but never defined", decl);
934 else
935 warning (OPT_Wunused_function, "%q+F declared %<static%> but never defined", decl);
936 /* This symbol is effectively an "extern" declaration now. */
937 TREE_PUBLIC (decl) = 1;
940 /* Warn about static fns or vars defined but not used. */
941 if (((warn_unused_function && TREE_CODE (decl) == FUNCTION_DECL)
942 || (((warn_unused_variable && ! TREE_READONLY (decl))
943 || (warn_unused_const_variable && TREE_READONLY (decl)))
944 && TREE_CODE (decl) == VAR_DECL))
945 && ! DECL_IN_SYSTEM_HEADER (decl)
946 && ! snode->referred_to_p (/*include_self=*/false)
947 /* This TREE_USED check is needed in addition to referred_to_p
948 above, because the `__unused__' attribute is not being
949 considered for referred_to_p. */
950 && ! TREE_USED (decl)
951 /* The TREE_USED bit for file-scope decls is kept in the identifier,
952 to handle multiple external decls in different scopes. */
953 && ! (DECL_NAME (decl) && TREE_USED (DECL_NAME (decl)))
954 && ! DECL_EXTERNAL (decl)
955 && ! DECL_ARTIFICIAL (decl)
956 && ! DECL_ABSTRACT_ORIGIN (decl)
957 && ! TREE_PUBLIC (decl)
958 /* A volatile variable might be used in some non-obvious way. */
959 && ! TREE_THIS_VOLATILE (decl)
960 /* Global register variables must be declared to reserve them. */
961 && ! (TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
962 /* Global ctors and dtors are called by the runtime. */
963 && (TREE_CODE (decl) != FUNCTION_DECL
964 || (!DECL_STATIC_CONSTRUCTOR (decl)
965 && !DECL_STATIC_DESTRUCTOR (decl)))
966 /* Otherwise, ask the language. */
967 && lang_hooks.decls.warn_unused_global (decl))
968 warning_at (DECL_SOURCE_LOCATION (decl),
969 (TREE_CODE (decl) == FUNCTION_DECL)
970 ? OPT_Wunused_function
971 : (TREE_READONLY (decl)
972 ? OPT_Wunused_const_variable
973 : OPT_Wunused_variable),
974 "%qD defined but not used", decl);
977 /* Discover all functions and variables that are trivially needed, analyze
978 them as well as all functions and variables referred by them */
979 static cgraph_node *first_analyzed;
980 static varpool_node *first_analyzed_var;
982 /* FIRST_TIME is set to TRUE for the first time we are called for a
983 translation unit from finalize_compilation_unit() or false
984 otherwise. */
986 static void
987 analyze_functions (bool first_time)
989 /* Keep track of already processed nodes when called multiple times for
990 intermodule optimization. */
991 cgraph_node *first_handled = first_analyzed;
992 varpool_node *first_handled_var = first_analyzed_var;
993 hash_set<void *> reachable_call_targets;
995 symtab_node *node;
996 symtab_node *next;
997 int i;
998 ipa_ref *ref;
999 bool changed = true;
1000 location_t saved_loc = input_location;
1002 bitmap_obstack_initialize (NULL);
1003 symtab->state = CONSTRUCTION;
1004 input_location = UNKNOWN_LOCATION;
1006 /* Ugly, but the fixup can not happen at a time same body alias is created;
1007 C++ FE is confused about the COMDAT groups being right. */
1008 if (symtab->cpp_implicit_aliases_done)
1009 FOR_EACH_SYMBOL (node)
1010 if (node->cpp_implicit_alias)
1011 node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
1012 build_type_inheritance_graph ();
1014 /* Analysis adds static variables that in turn adds references to new functions.
1015 So we need to iterate the process until it stabilize. */
1016 while (changed)
1018 changed = false;
1019 process_function_and_variable_attributes (first_analyzed,
1020 first_analyzed_var);
1022 /* First identify the trivially needed symbols. */
1023 for (node = symtab->first_symbol ();
1024 node != first_analyzed
1025 && node != first_analyzed_var; node = node->next)
1027 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
1028 node->get_comdat_group_id ();
1029 if (node->needed_p ())
1031 enqueue_node (node);
1032 if (!changed && symtab->dump_file)
1033 fprintf (symtab->dump_file, "Trivially needed symbols:");
1034 changed = true;
1035 if (symtab->dump_file)
1036 fprintf (symtab->dump_file, " %s", node->asm_name ());
1037 if (!changed && symtab->dump_file)
1038 fprintf (symtab->dump_file, "\n");
1040 if (node == first_analyzed
1041 || node == first_analyzed_var)
1042 break;
1044 symtab->process_new_functions ();
1045 first_analyzed_var = symtab->first_variable ();
1046 first_analyzed = symtab->first_function ();
1048 if (changed && symtab->dump_file)
1049 fprintf (symtab->dump_file, "\n");
1051 /* Lower representation, build callgraph edges and references for all trivially
1052 needed symbols and all symbols referred by them. */
1053 while (queued_nodes != &symtab_terminator)
1055 changed = true;
1056 node = queued_nodes;
1057 queued_nodes = (symtab_node *)queued_nodes->aux;
1058 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1059 if (cnode && cnode->definition)
1061 cgraph_edge *edge;
1062 tree decl = cnode->decl;
1064 /* ??? It is possible to create extern inline function
1065 and later using weak alias attribute to kill its body.
1066 See gcc.c-torture/compile/20011119-1.c */
1067 if (!DECL_STRUCT_FUNCTION (decl)
1068 && !cnode->alias
1069 && !cnode->thunk.thunk_p
1070 && !cnode->dispatcher_function)
1072 cnode->reset ();
1073 cnode->local.redefined_extern_inline = true;
1074 continue;
1077 if (!cnode->analyzed)
1078 cnode->analyze ();
1080 for (edge = cnode->callees; edge; edge = edge->next_callee)
1081 if (edge->callee->definition
1082 && (!DECL_EXTERNAL (edge->callee->decl)
1083 /* When not optimizing, do not try to analyze extern
1084 inline functions. Doing so is pointless. */
1085 || opt_for_fn (edge->callee->decl, optimize)
1086 /* Weakrefs needs to be preserved. */
1087 || edge->callee->alias
1088 /* always_inline functions are inlined aven at -O0. */
1089 || lookup_attribute
1090 ("always_inline",
1091 DECL_ATTRIBUTES (edge->callee->decl))
1092 /* Multiversioned functions needs the dispatcher to
1093 be produced locally even for extern functions. */
1094 || edge->callee->function_version ()))
1095 enqueue_node (edge->callee);
1096 if (opt_for_fn (cnode->decl, optimize)
1097 && opt_for_fn (cnode->decl, flag_devirtualize))
1099 cgraph_edge *next;
1101 for (edge = cnode->indirect_calls; edge; edge = next)
1103 next = edge->next_callee;
1104 if (edge->indirect_info->polymorphic)
1105 walk_polymorphic_call_targets (&reachable_call_targets,
1106 edge);
1110 /* If decl is a clone of an abstract function,
1111 mark that abstract function so that we don't release its body.
1112 The DECL_INITIAL() of that abstract function declaration
1113 will be later needed to output debug info. */
1114 if (DECL_ABSTRACT_ORIGIN (decl))
1116 cgraph_node *origin_node
1117 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl));
1118 origin_node->used_as_abstract_origin = true;
1121 else
1123 varpool_node *vnode = dyn_cast <varpool_node *> (node);
1124 if (vnode && vnode->definition && !vnode->analyzed)
1125 vnode->analyze ();
1128 if (node->same_comdat_group)
1130 symtab_node *next;
1131 for (next = node->same_comdat_group;
1132 next != node;
1133 next = next->same_comdat_group)
1134 if (!next->comdat_local_p ())
1135 enqueue_node (next);
1137 for (i = 0; node->iterate_reference (i, ref); i++)
1138 if (ref->referred->definition
1139 && (!DECL_EXTERNAL (ref->referred->decl)
1140 || ((TREE_CODE (ref->referred->decl) != FUNCTION_DECL
1141 && optimize)
1142 || (TREE_CODE (ref->referred->decl) == FUNCTION_DECL
1143 && opt_for_fn (ref->referred->decl, optimize))
1144 || node->alias
1145 || ref->referred->alias)))
1146 enqueue_node (ref->referred);
1147 symtab->process_new_functions ();
1150 update_type_inheritance_graph ();
1152 /* Collect entry points to the unit. */
1153 if (symtab->dump_file)
1155 fprintf (symtab->dump_file, "\n\nInitial ");
1156 symtab_node::dump_table (symtab->dump_file);
1159 if (first_time)
1161 symtab_node *snode;
1162 FOR_EACH_SYMBOL (snode)
1163 check_global_declaration (snode);
1166 if (symtab->dump_file)
1167 fprintf (symtab->dump_file, "\nRemoving unused symbols:");
1169 for (node = symtab->first_symbol ();
1170 node != first_handled
1171 && node != first_handled_var; node = next)
1173 next = node->next;
1174 if (!node->aux && !node->referred_to_p ())
1176 if (symtab->dump_file)
1177 fprintf (symtab->dump_file, " %s", node->name ());
1179 /* See if the debugger can use anything before the DECL
1180 passes away. Perhaps it can notice a DECL that is now a
1181 constant and can tag the early DIE with an appropriate
1182 attribute.
1184 Otherwise, this is the last chance the debug_hooks have
1185 at looking at optimized away DECLs, since
1186 late_global_decl will subsequently be called from the
1187 contents of the now pruned symbol table. */
1188 if (!decl_function_context (node->decl))
1189 (*debug_hooks->late_global_decl) (node->decl);
1191 node->remove ();
1192 continue;
1194 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1196 tree decl = node->decl;
1198 if (cnode->definition && !gimple_has_body_p (decl)
1199 && !cnode->alias
1200 && !cnode->thunk.thunk_p)
1201 cnode->reset ();
1203 gcc_assert (!cnode->definition || cnode->thunk.thunk_p
1204 || cnode->alias
1205 || gimple_has_body_p (decl));
1206 gcc_assert (cnode->analyzed == cnode->definition);
1208 node->aux = NULL;
1210 for (;node; node = node->next)
1211 node->aux = NULL;
1212 first_analyzed = symtab->first_function ();
1213 first_analyzed_var = symtab->first_variable ();
1214 if (symtab->dump_file)
1216 fprintf (symtab->dump_file, "\n\nReclaimed ");
1217 symtab_node::dump_table (symtab->dump_file);
1219 bitmap_obstack_release (NULL);
1220 ggc_collect ();
1221 /* Initialize assembler name hash, in particular we want to trigger C++
1222 mangling and same body alias creation before we free DECL_ARGUMENTS
1223 used by it. */
1224 if (!seen_error ())
1225 symtab->symtab_initialize_asm_name_hash ();
1227 input_location = saved_loc;
1230 /* Translate the ugly representation of aliases as alias pairs into nice
1231 representation in callgraph. We don't handle all cases yet,
1232 unfortunately. */
1234 static void
1235 handle_alias_pairs (void)
1237 alias_pair *p;
1238 unsigned i;
1240 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1242 symtab_node *target_node = symtab_node::get_for_asmname (p->target);
1244 /* Weakrefs with target not defined in current unit are easy to handle:
1245 they behave just as external variables except we need to note the
1246 alias flag to later output the weakref pseudo op into asm file. */
1247 if (!target_node
1248 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1250 symtab_node *node = symtab_node::get (p->decl);
1251 if (node)
1253 node->alias_target = p->target;
1254 node->weakref = true;
1255 node->alias = true;
1257 alias_pairs->unordered_remove (i);
1258 continue;
1260 else if (!target_node)
1262 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1263 symtab_node *node = symtab_node::get (p->decl);
1264 if (node)
1265 node->alias = false;
1266 alias_pairs->unordered_remove (i);
1267 continue;
1270 if (DECL_EXTERNAL (target_node->decl)
1271 /* We use local aliases for C++ thunks to force the tailcall
1272 to bind locally. This is a hack - to keep it working do
1273 the following (which is not strictly correct). */
1274 && (TREE_CODE (target_node->decl) != FUNCTION_DECL
1275 || ! DECL_VIRTUAL_P (target_node->decl))
1276 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1278 error ("%q+D aliased to external symbol %qE",
1279 p->decl, p->target);
1282 if (TREE_CODE (p->decl) == FUNCTION_DECL
1283 && target_node && is_a <cgraph_node *> (target_node))
1285 cgraph_node *src_node = cgraph_node::get (p->decl);
1286 if (src_node && src_node->definition)
1287 src_node->reset ();
1288 cgraph_node::create_alias (p->decl, target_node->decl);
1289 alias_pairs->unordered_remove (i);
1291 else if (TREE_CODE (p->decl) == VAR_DECL
1292 && target_node && is_a <varpool_node *> (target_node))
1294 varpool_node::create_alias (p->decl, target_node->decl);
1295 alias_pairs->unordered_remove (i);
1297 else
1299 error ("%q+D alias in between function and variable is not supported",
1300 p->decl);
1301 warning (0, "%q+D aliased declaration",
1302 target_node->decl);
1303 alias_pairs->unordered_remove (i);
1306 vec_free (alias_pairs);
1310 /* Figure out what functions we want to assemble. */
1312 static void
1313 mark_functions_to_output (void)
1315 bool check_same_comdat_groups = false;
1316 cgraph_node *node;
1318 if (flag_checking)
1319 FOR_EACH_FUNCTION (node)
1320 gcc_assert (!node->process);
1322 FOR_EACH_FUNCTION (node)
1324 tree decl = node->decl;
1326 gcc_assert (!node->process || node->same_comdat_group);
1327 if (node->process)
1328 continue;
1330 /* We need to output all local functions that are used and not
1331 always inlined, as well as those that are reachable from
1332 outside the current compilation unit. */
1333 if (node->analyzed
1334 && !node->thunk.thunk_p
1335 && !node->alias
1336 && !node->global.inlined_to
1337 && !TREE_ASM_WRITTEN (decl)
1338 && !DECL_EXTERNAL (decl))
1340 node->process = 1;
1341 if (node->same_comdat_group)
1343 cgraph_node *next;
1344 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
1345 next != node;
1346 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
1347 if (!next->thunk.thunk_p && !next->alias
1348 && !next->comdat_local_p ())
1349 next->process = 1;
1352 else if (node->same_comdat_group)
1354 if (flag_checking)
1355 check_same_comdat_groups = true;
1357 else
1359 /* We should've reclaimed all functions that are not needed. */
1360 if (flag_checking
1361 && !node->global.inlined_to
1362 && gimple_has_body_p (decl)
1363 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1364 are inside partition, we can end up not removing the body since we no longer
1365 have analyzed node pointing to it. */
1366 && !node->in_other_partition
1367 && !node->alias
1368 && !node->clones
1369 && !DECL_EXTERNAL (decl))
1371 node->debug ();
1372 internal_error ("failed to reclaim unneeded function");
1374 gcc_assert (node->global.inlined_to
1375 || !gimple_has_body_p (decl)
1376 || node->in_other_partition
1377 || node->clones
1378 || DECL_ARTIFICIAL (decl)
1379 || DECL_EXTERNAL (decl));
1384 if (flag_checking && check_same_comdat_groups)
1385 FOR_EACH_FUNCTION (node)
1386 if (node->same_comdat_group && !node->process)
1388 tree decl = node->decl;
1389 if (!node->global.inlined_to
1390 && gimple_has_body_p (decl)
1391 /* FIXME: in an ltrans unit when the offline copy is outside a
1392 partition but inline copies are inside a partition, we can
1393 end up not removing the body since we no longer have an
1394 analyzed node pointing to it. */
1395 && !node->in_other_partition
1396 && !node->clones
1397 && !DECL_EXTERNAL (decl))
1399 node->debug ();
1400 internal_error ("failed to reclaim unneeded function in same "
1401 "comdat group");
1406 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1407 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1409 Set current_function_decl and cfun to newly constructed empty function body.
1410 return basic block in the function body. */
1412 basic_block
1413 init_lowered_empty_function (tree decl, bool in_ssa, gcov_type count)
1415 basic_block bb;
1416 edge e;
1418 current_function_decl = decl;
1419 allocate_struct_function (decl, false);
1420 gimple_register_cfg_hooks ();
1421 init_empty_tree_cfg ();
1423 if (in_ssa)
1425 init_tree_ssa (cfun);
1426 init_ssa_operands (cfun);
1427 cfun->gimple_df->in_ssa_p = true;
1428 cfun->curr_properties |= PROP_ssa;
1431 DECL_INITIAL (decl) = make_node (BLOCK);
1433 DECL_SAVED_TREE (decl) = error_mark_node;
1434 cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1435 | PROP_cfg | PROP_loops);
1437 set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ());
1438 init_loops_structure (cfun, loops_for_fn (cfun), 1);
1439 loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1441 /* Create BB for body of the function and connect it properly. */
1442 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = count;
1443 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency = REG_BR_PROB_BASE;
1444 EXIT_BLOCK_PTR_FOR_FN (cfun)->count = count;
1445 EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency = REG_BR_PROB_BASE;
1446 bb = create_basic_block (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun));
1447 bb->count = count;
1448 bb->frequency = BB_FREQ_MAX;
1449 e = make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU);
1450 e->count = count;
1451 e->probability = REG_BR_PROB_BASE;
1452 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1453 e->count = count;
1454 e->probability = REG_BR_PROB_BASE;
1455 add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
1457 return bb;
1460 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1461 offset indicated by VIRTUAL_OFFSET, if that is
1462 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1463 zero for a result adjusting thunk. */
1465 static tree
1466 thunk_adjust (gimple_stmt_iterator * bsi,
1467 tree ptr, bool this_adjusting,
1468 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1470 gassign *stmt;
1471 tree ret;
1473 if (this_adjusting
1474 && fixed_offset != 0)
1476 stmt = gimple_build_assign
1477 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1478 ptr,
1479 fixed_offset));
1480 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1483 /* If there's a virtual offset, look up that value in the vtable and
1484 adjust the pointer again. */
1485 if (virtual_offset)
1487 tree vtabletmp;
1488 tree vtabletmp2;
1489 tree vtabletmp3;
1491 if (!vtable_entry_type)
1493 tree vfunc_type = make_node (FUNCTION_TYPE);
1494 TREE_TYPE (vfunc_type) = integer_type_node;
1495 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1496 layout_type (vfunc_type);
1498 vtable_entry_type = build_pointer_type (vfunc_type);
1501 vtabletmp =
1502 create_tmp_reg (build_pointer_type
1503 (build_pointer_type (vtable_entry_type)), "vptr");
1505 /* The vptr is always at offset zero in the object. */
1506 stmt = gimple_build_assign (vtabletmp,
1507 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1508 ptr));
1509 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1511 /* Form the vtable address. */
1512 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
1513 "vtableaddr");
1514 stmt = gimple_build_assign (vtabletmp2,
1515 build_simple_mem_ref (vtabletmp));
1516 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1518 /* Find the entry with the vcall offset. */
1519 stmt = gimple_build_assign (vtabletmp2,
1520 fold_build_pointer_plus_loc (input_location,
1521 vtabletmp2,
1522 virtual_offset));
1523 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1525 /* Get the offset itself. */
1526 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1527 "vcalloffset");
1528 stmt = gimple_build_assign (vtabletmp3,
1529 build_simple_mem_ref (vtabletmp2));
1530 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1532 /* Adjust the `this' pointer. */
1533 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1534 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1535 GSI_CONTINUE_LINKING);
1538 if (!this_adjusting
1539 && fixed_offset != 0)
1540 /* Adjust the pointer by the constant. */
1542 tree ptrtmp;
1544 if (TREE_CODE (ptr) == VAR_DECL)
1545 ptrtmp = ptr;
1546 else
1548 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
1549 stmt = gimple_build_assign (ptrtmp, ptr);
1550 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1552 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1553 ptrtmp, fixed_offset);
1556 /* Emit the statement and gimplify the adjustment expression. */
1557 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
1558 stmt = gimple_build_assign (ret, ptr);
1559 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1561 return ret;
1564 /* Expand thunk NODE to gimple if possible.
1565 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1566 no assembler is produced.
1567 When OUTPUT_ASM_THUNK is true, also produce assembler for
1568 thunks that are not lowered. */
1570 bool
1571 cgraph_node::expand_thunk (bool output_asm_thunks, bool force_gimple_thunk)
1573 bool this_adjusting = thunk.this_adjusting;
1574 HOST_WIDE_INT fixed_offset = thunk.fixed_offset;
1575 HOST_WIDE_INT virtual_value = thunk.virtual_value;
1576 tree virtual_offset = NULL;
1577 tree alias = callees->callee->decl;
1578 tree thunk_fndecl = decl;
1579 tree a;
1581 /* Instrumentation thunk is the same function with
1582 a different signature. Never need to expand it. */
1583 if (thunk.add_pointer_bounds_args)
1584 return false;
1586 if (!force_gimple_thunk && this_adjusting
1587 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1588 virtual_value, alias))
1590 const char *fnname;
1591 tree fn_block;
1592 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1594 if (!output_asm_thunks)
1596 analyzed = true;
1597 return false;
1600 if (in_lto_p)
1601 get_untransformed_body ();
1602 a = DECL_ARGUMENTS (thunk_fndecl);
1604 current_function_decl = thunk_fndecl;
1606 /* Ensure thunks are emitted in their correct sections. */
1607 resolve_unique_section (thunk_fndecl, 0,
1608 flag_function_sections);
1610 DECL_RESULT (thunk_fndecl)
1611 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1612 RESULT_DECL, 0, restype);
1613 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1614 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
1616 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1617 create one. */
1618 fn_block = make_node (BLOCK);
1619 BLOCK_VARS (fn_block) = a;
1620 DECL_INITIAL (thunk_fndecl) = fn_block;
1621 allocate_struct_function (thunk_fndecl, false);
1622 init_function_start (thunk_fndecl);
1623 cfun->is_thunk = 1;
1624 insn_locations_init ();
1625 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1626 prologue_location = curr_insn_location ();
1627 assemble_start_function (thunk_fndecl, fnname);
1629 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1630 fixed_offset, virtual_value, alias);
1632 assemble_end_function (thunk_fndecl, fnname);
1633 insn_locations_finalize ();
1634 init_insn_lengths ();
1635 free_after_compilation (cfun);
1636 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1637 thunk.thunk_p = false;
1638 analyzed = false;
1640 else if (stdarg_p (TREE_TYPE (thunk_fndecl)))
1642 error ("generic thunk code fails for method %qD which uses %<...%>",
1643 thunk_fndecl);
1644 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1645 analyzed = true;
1646 return false;
1648 else
1650 tree restype;
1651 basic_block bb, then_bb, else_bb, return_bb;
1652 gimple_stmt_iterator bsi;
1653 int nargs = 0;
1654 tree arg;
1655 int i;
1656 tree resdecl;
1657 tree restmp = NULL;
1658 tree resbnd = NULL;
1660 gcall *call;
1661 greturn *ret;
1662 bool alias_is_noreturn = TREE_THIS_VOLATILE (alias);
1664 if (in_lto_p)
1665 get_untransformed_body ();
1666 a = DECL_ARGUMENTS (thunk_fndecl);
1668 current_function_decl = thunk_fndecl;
1670 /* Ensure thunks are emitted in their correct sections. */
1671 resolve_unique_section (thunk_fndecl, 0,
1672 flag_function_sections);
1674 DECL_IGNORED_P (thunk_fndecl) = 1;
1675 bitmap_obstack_initialize (NULL);
1677 if (thunk.virtual_offset_p)
1678 virtual_offset = size_int (virtual_value);
1680 /* Build the return declaration for the function. */
1681 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1682 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1684 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1685 DECL_ARTIFICIAL (resdecl) = 1;
1686 DECL_IGNORED_P (resdecl) = 1;
1687 DECL_RESULT (thunk_fndecl) = resdecl;
1688 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1690 else
1691 resdecl = DECL_RESULT (thunk_fndecl);
1693 bb = then_bb = else_bb = return_bb
1694 = init_lowered_empty_function (thunk_fndecl, true, count);
1696 bsi = gsi_start_bb (bb);
1698 /* Build call to the function being thunked. */
1699 if (!VOID_TYPE_P (restype) && !alias_is_noreturn)
1701 if (DECL_BY_REFERENCE (resdecl))
1703 restmp = gimple_fold_indirect_ref (resdecl);
1704 if (!restmp)
1705 restmp = build2 (MEM_REF,
1706 TREE_TYPE (TREE_TYPE (DECL_RESULT (alias))),
1707 resdecl,
1708 build_int_cst (TREE_TYPE
1709 (DECL_RESULT (alias)), 0));
1711 else if (!is_gimple_reg_type (restype))
1713 if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl)))
1715 restmp = resdecl;
1717 if (TREE_CODE (restmp) == VAR_DECL)
1718 add_local_decl (cfun, restmp);
1719 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1721 else
1722 restmp = create_tmp_var (restype, "retval");
1724 else
1725 restmp = create_tmp_reg (restype, "retval");
1728 for (arg = a; arg; arg = DECL_CHAIN (arg))
1729 nargs++;
1730 auto_vec<tree> vargs (nargs);
1731 i = 0;
1732 arg = a;
1733 if (this_adjusting)
1735 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
1736 virtual_offset));
1737 arg = DECL_CHAIN (a);
1738 i = 1;
1741 if (nargs)
1742 for (; i < nargs; i++, arg = DECL_CHAIN (arg))
1744 tree tmp = arg;
1745 if (!is_gimple_val (arg))
1747 tmp = create_tmp_reg (TYPE_MAIN_VARIANT
1748 (TREE_TYPE (arg)), "arg");
1749 gimple *stmt = gimple_build_assign (tmp, arg);
1750 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1752 vargs.quick_push (tmp);
1754 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1755 callees->call_stmt = call;
1756 gimple_call_set_from_thunk (call, true);
1757 gimple_call_set_with_bounds (call, instrumentation_clone);
1759 /* Return slot optimization is always possible and in fact requred to
1760 return values with DECL_BY_REFERENCE. */
1761 if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl))
1762 && (!is_gimple_reg_type (TREE_TYPE (resdecl))
1763 || DECL_BY_REFERENCE (resdecl)))
1764 gimple_call_set_return_slot_opt (call, true);
1766 if (restmp && !alias_is_noreturn)
1768 gimple_call_set_lhs (call, restmp);
1769 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp),
1770 TREE_TYPE (TREE_TYPE (alias))));
1772 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
1773 if (!alias_is_noreturn)
1775 if (instrumentation_clone
1776 && !DECL_BY_REFERENCE (resdecl)
1777 && restmp
1778 && BOUNDED_P (restmp))
1780 resbnd = chkp_insert_retbnd_call (NULL, restmp, &bsi);
1781 create_edge (get_create (gimple_call_fndecl (gsi_stmt (bsi))),
1782 as_a <gcall *> (gsi_stmt (bsi)),
1783 callees->count, callees->frequency);
1786 if (restmp && !this_adjusting
1787 && (fixed_offset || virtual_offset))
1789 tree true_label = NULL_TREE;
1791 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1793 gimple *stmt;
1794 edge e;
1795 /* If the return type is a pointer, we need to
1796 protect against NULL. We know there will be an
1797 adjustment, because that's why we're emitting a
1798 thunk. */
1799 then_bb = create_basic_block (NULL, bb);
1800 then_bb->count = count - count / 16;
1801 then_bb->frequency = BB_FREQ_MAX - BB_FREQ_MAX / 16;
1802 return_bb = create_basic_block (NULL, then_bb);
1803 return_bb->count = count;
1804 return_bb->frequency = BB_FREQ_MAX;
1805 else_bb = create_basic_block (NULL, else_bb);
1806 then_bb->count = count / 16;
1807 then_bb->frequency = BB_FREQ_MAX / 16;
1808 add_bb_to_loop (then_bb, bb->loop_father);
1809 add_bb_to_loop (return_bb, bb->loop_father);
1810 add_bb_to_loop (else_bb, bb->loop_father);
1811 remove_edge (single_succ_edge (bb));
1812 true_label = gimple_block_label (then_bb);
1813 stmt = gimple_build_cond (NE_EXPR, restmp,
1814 build_zero_cst (TREE_TYPE (restmp)),
1815 NULL_TREE, NULL_TREE);
1816 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1817 e = make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1818 e->probability = REG_BR_PROB_BASE - REG_BR_PROB_BASE / 16;
1819 e->count = count - count / 16;
1820 e = make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1821 e->probability = REG_BR_PROB_BASE / 16;
1822 e->count = count / 16;
1823 e = make_edge (return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1824 e->probability = REG_BR_PROB_BASE;
1825 e->count = count;
1826 e = make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1827 e->probability = REG_BR_PROB_BASE;
1828 e->count = count - count / 16;
1829 e = make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1830 e->probability = REG_BR_PROB_BASE;
1831 e->count = count / 16;
1832 bsi = gsi_last_bb (then_bb);
1835 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1836 fixed_offset, virtual_offset);
1837 if (true_label)
1839 gimple *stmt;
1840 bsi = gsi_last_bb (else_bb);
1841 stmt = gimple_build_assign (restmp,
1842 build_zero_cst (TREE_TYPE (restmp)));
1843 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1844 bsi = gsi_last_bb (return_bb);
1847 else
1848 gimple_call_set_tail (call, true);
1850 /* Build return value. */
1851 if (!DECL_BY_REFERENCE (resdecl))
1852 ret = gimple_build_return (restmp);
1853 else
1854 ret = gimple_build_return (resdecl);
1855 gimple_return_set_retbnd (ret, resbnd);
1857 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1859 else
1861 gimple_call_set_tail (call, true);
1862 remove_edge (single_succ_edge (bb));
1865 cfun->gimple_df->in_ssa_p = true;
1866 profile_status_for_fn (cfun)
1867 = count ? PROFILE_READ : PROFILE_GUESSED;
1868 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
1869 TREE_ASM_WRITTEN (thunk_fndecl) = false;
1870 delete_unreachable_blocks ();
1871 update_ssa (TODO_update_ssa);
1872 checking_verify_flow_info ();
1873 free_dominance_info (CDI_DOMINATORS);
1875 /* Since we want to emit the thunk, we explicitly mark its name as
1876 referenced. */
1877 thunk.thunk_p = false;
1878 lowered = true;
1879 bitmap_obstack_release (NULL);
1881 current_function_decl = NULL;
1882 set_cfun (NULL);
1883 return true;
1886 /* Assemble thunks and aliases associated to node. */
1888 void
1889 cgraph_node::assemble_thunks_and_aliases (void)
1891 cgraph_edge *e;
1892 ipa_ref *ref;
1894 for (e = callers; e;)
1895 if (e->caller->thunk.thunk_p
1896 && !e->caller->thunk.add_pointer_bounds_args)
1898 cgraph_node *thunk = e->caller;
1900 e = e->next_caller;
1901 thunk->expand_thunk (true, false);
1902 thunk->assemble_thunks_and_aliases ();
1904 else
1905 e = e->next_caller;
1907 FOR_EACH_ALIAS (this, ref)
1909 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1910 bool saved_written = TREE_ASM_WRITTEN (decl);
1912 /* Force assemble_alias to really output the alias this time instead
1913 of buffering it in same alias pairs. */
1914 TREE_ASM_WRITTEN (decl) = 1;
1915 do_assemble_alias (alias->decl,
1916 DECL_ASSEMBLER_NAME (decl));
1917 alias->assemble_thunks_and_aliases ();
1918 TREE_ASM_WRITTEN (decl) = saved_written;
1922 /* Expand function specified by node. */
1924 void
1925 cgraph_node::expand (void)
1927 location_t saved_loc;
1929 /* We ought to not compile any inline clones. */
1930 gcc_assert (!global.inlined_to);
1932 announce_function (decl);
1933 process = 0;
1934 gcc_assert (lowered);
1935 get_untransformed_body ();
1937 /* Generate RTL for the body of DECL. */
1939 timevar_push (TV_REST_OF_COMPILATION);
1941 gcc_assert (symtab->global_info_ready);
1943 /* Initialize the default bitmap obstack. */
1944 bitmap_obstack_initialize (NULL);
1946 /* Initialize the RTL code for the function. */
1947 saved_loc = input_location;
1948 input_location = DECL_SOURCE_LOCATION (decl);
1950 gcc_assert (DECL_STRUCT_FUNCTION (decl));
1951 push_cfun (DECL_STRUCT_FUNCTION (decl));
1952 init_function_start (decl);
1954 gimple_register_cfg_hooks ();
1956 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1958 execute_all_ipa_transforms ();
1960 /* Perform all tree transforms and optimizations. */
1962 /* Signal the start of passes. */
1963 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1965 execute_pass_list (cfun, g->get_passes ()->all_passes);
1967 /* Signal the end of passes. */
1968 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1970 bitmap_obstack_release (&reg_obstack);
1972 /* Release the default bitmap obstack. */
1973 bitmap_obstack_release (NULL);
1975 /* If requested, warn about function definitions where the function will
1976 return a value (usually of some struct or union type) which itself will
1977 take up a lot of stack space. */
1978 if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl))
1980 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
1982 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1983 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1984 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1985 larger_than_size))
1987 unsigned int size_as_int
1988 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1990 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1991 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
1992 decl, size_as_int);
1993 else
1994 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
1995 decl, larger_than_size);
1999 gimple_set_body (decl, NULL);
2000 if (DECL_STRUCT_FUNCTION (decl) == 0
2001 && !cgraph_node::get (decl)->origin)
2003 /* Stop pointing to the local nodes about to be freed.
2004 But DECL_INITIAL must remain nonzero so we know this
2005 was an actual function definition.
2006 For a nested function, this is done in c_pop_function_context.
2007 If rest_of_compilation set this to 0, leave it 0. */
2008 if (DECL_INITIAL (decl) != 0)
2009 DECL_INITIAL (decl) = error_mark_node;
2012 input_location = saved_loc;
2014 ggc_collect ();
2015 timevar_pop (TV_REST_OF_COMPILATION);
2017 /* Make sure that BE didn't give up on compiling. */
2018 gcc_assert (TREE_ASM_WRITTEN (decl));
2019 if (cfun)
2020 pop_cfun ();
2022 /* It would make a lot more sense to output thunks before function body to get more
2023 forward and lest backwarding jumps. This however would need solving problem
2024 with comdats. See PR48668. Also aliases must come after function itself to
2025 make one pass assemblers, like one on AIX, happy. See PR 50689.
2026 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
2027 groups. */
2028 assemble_thunks_and_aliases ();
2029 release_body ();
2030 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
2031 points to the dead function body. */
2032 remove_callees ();
2033 remove_all_references ();
2036 /* Node comparer that is responsible for the order that corresponds
2037 to time when a function was launched for the first time. */
2039 static int
2040 node_cmp (const void *pa, const void *pb)
2042 const cgraph_node *a = *(const cgraph_node * const *) pa;
2043 const cgraph_node *b = *(const cgraph_node * const *) pb;
2045 /* Functions with time profile must be before these without profile. */
2046 if (!a->tp_first_run || !b->tp_first_run)
2047 return a->tp_first_run - b->tp_first_run;
2049 return a->tp_first_run != b->tp_first_run
2050 ? b->tp_first_run - a->tp_first_run
2051 : b->order - a->order;
2054 /* Expand all functions that must be output.
2056 Attempt to topologically sort the nodes so function is output when
2057 all called functions are already assembled to allow data to be
2058 propagated across the callgraph. Use a stack to get smaller distance
2059 between a function and its callees (later we may choose to use a more
2060 sophisticated algorithm for function reordering; we will likely want
2061 to use subsections to make the output functions appear in top-down
2062 order). */
2064 static void
2065 expand_all_functions (void)
2067 cgraph_node *node;
2068 cgraph_node **order = XCNEWVEC (cgraph_node *,
2069 symtab->cgraph_count);
2070 unsigned int expanded_func_count = 0, profiled_func_count = 0;
2071 int order_pos, new_order_pos = 0;
2072 int i;
2074 order_pos = ipa_reverse_postorder (order);
2075 gcc_assert (order_pos == symtab->cgraph_count);
2077 /* Garbage collector may remove inline clones we eliminate during
2078 optimization. So we must be sure to not reference them. */
2079 for (i = 0; i < order_pos; i++)
2080 if (order[i]->process)
2081 order[new_order_pos++] = order[i];
2083 if (flag_profile_reorder_functions)
2084 qsort (order, new_order_pos, sizeof (cgraph_node *), node_cmp);
2086 for (i = new_order_pos - 1; i >= 0; i--)
2088 node = order[i];
2090 if (node->process)
2092 expanded_func_count++;
2093 if(node->tp_first_run)
2094 profiled_func_count++;
2096 if (symtab->dump_file)
2097 fprintf (symtab->dump_file,
2098 "Time profile order in expand_all_functions:%s:%d\n",
2099 node->asm_name (), node->tp_first_run);
2100 node->process = 0;
2101 node->expand ();
2105 if (dump_file)
2106 fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
2107 main_input_filename, profiled_func_count, expanded_func_count);
2109 if (symtab->dump_file && flag_profile_reorder_functions)
2110 fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n",
2111 profiled_func_count, expanded_func_count);
2113 symtab->process_new_functions ();
2114 free_gimplify_stack ();
2116 free (order);
2119 /* This is used to sort the node types by the cgraph order number. */
2121 enum cgraph_order_sort_kind
2123 ORDER_UNDEFINED = 0,
2124 ORDER_FUNCTION,
2125 ORDER_VAR,
2126 ORDER_ASM
2129 struct cgraph_order_sort
2131 enum cgraph_order_sort_kind kind;
2132 union
2134 cgraph_node *f;
2135 varpool_node *v;
2136 asm_node *a;
2137 } u;
2140 /* Output all functions, variables, and asm statements in the order
2141 according to their order fields, which is the order in which they
2142 appeared in the file. This implements -fno-toplevel-reorder. In
2143 this mode we may output functions and variables which don't really
2144 need to be output.
2145 When NO_REORDER is true only do this for symbols marked no reorder. */
2147 static void
2148 output_in_order (bool no_reorder)
2150 int max;
2151 cgraph_order_sort *nodes;
2152 int i;
2153 cgraph_node *pf;
2154 varpool_node *pv;
2155 asm_node *pa;
2156 max = symtab->order;
2157 nodes = XCNEWVEC (cgraph_order_sort, max);
2159 FOR_EACH_DEFINED_FUNCTION (pf)
2161 if (pf->process && !pf->thunk.thunk_p && !pf->alias)
2163 if (no_reorder && !pf->no_reorder)
2164 continue;
2165 i = pf->order;
2166 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2167 nodes[i].kind = ORDER_FUNCTION;
2168 nodes[i].u.f = pf;
2172 FOR_EACH_DEFINED_VARIABLE (pv)
2173 if (!DECL_EXTERNAL (pv->decl))
2175 if (no_reorder && !pv->no_reorder)
2176 continue;
2177 i = pv->order;
2178 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2179 nodes[i].kind = ORDER_VAR;
2180 nodes[i].u.v = pv;
2183 for (pa = symtab->first_asm_symbol (); pa; pa = pa->next)
2185 i = pa->order;
2186 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2187 nodes[i].kind = ORDER_ASM;
2188 nodes[i].u.a = pa;
2191 /* In toplevel reorder mode we output all statics; mark them as needed. */
2193 for (i = 0; i < max; ++i)
2194 if (nodes[i].kind == ORDER_VAR)
2195 nodes[i].u.v->finalize_named_section_flags ();
2197 for (i = 0; i < max; ++i)
2199 switch (nodes[i].kind)
2201 case ORDER_FUNCTION:
2202 nodes[i].u.f->process = 0;
2203 nodes[i].u.f->expand ();
2204 break;
2206 case ORDER_VAR:
2207 nodes[i].u.v->assemble_decl ();
2208 break;
2210 case ORDER_ASM:
2211 assemble_asm (nodes[i].u.a->asm_str);
2212 break;
2214 case ORDER_UNDEFINED:
2215 break;
2217 default:
2218 gcc_unreachable ();
2222 symtab->clear_asm_symbols ();
2224 free (nodes);
2227 static void
2228 ipa_passes (void)
2230 gcc::pass_manager *passes = g->get_passes ();
2232 set_cfun (NULL);
2233 current_function_decl = NULL;
2234 gimple_register_cfg_hooks ();
2235 bitmap_obstack_initialize (NULL);
2237 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2239 if (!in_lto_p)
2241 execute_ipa_pass_list (passes->all_small_ipa_passes);
2242 if (seen_error ())
2243 return;
2246 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2247 devirtualization and other changes where removal iterate. */
2248 symtab->remove_unreachable_nodes (symtab->dump_file);
2250 /* If pass_all_early_optimizations was not scheduled, the state of
2251 the cgraph will not be properly updated. Update it now. */
2252 if (symtab->state < IPA_SSA)
2253 symtab->state = IPA_SSA;
2255 if (!in_lto_p)
2257 /* Generate coverage variables and constructors. */
2258 coverage_finish ();
2260 /* Process new functions added. */
2261 set_cfun (NULL);
2262 current_function_decl = NULL;
2263 symtab->process_new_functions ();
2265 execute_ipa_summary_passes
2266 ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2269 /* Some targets need to handle LTO assembler output specially. */
2270 if (flag_generate_lto || flag_generate_offload)
2271 targetm.asm_out.lto_start ();
2273 if (!in_lto_p)
2275 if (g->have_offload)
2277 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2278 lto_stream_offload_p = true;
2279 ipa_write_summaries ();
2280 lto_stream_offload_p = false;
2282 if (flag_lto)
2284 section_name_prefix = LTO_SECTION_NAME_PREFIX;
2285 lto_stream_offload_p = false;
2286 ipa_write_summaries ();
2290 if (flag_generate_lto || flag_generate_offload)
2291 targetm.asm_out.lto_end ();
2293 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
2294 execute_ipa_pass_list (passes->all_regular_ipa_passes);
2295 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2297 bitmap_obstack_release (NULL);
2301 /* Return string alias is alias of. */
2303 static tree
2304 get_alias_symbol (tree decl)
2306 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
2307 return get_identifier (TREE_STRING_POINTER
2308 (TREE_VALUE (TREE_VALUE (alias))));
2312 /* Weakrefs may be associated to external decls and thus not output
2313 at expansion time. Emit all necessary aliases. */
2315 void
2316 symbol_table::output_weakrefs (void)
2318 symtab_node *node;
2319 cgraph_node *cnode;
2320 FOR_EACH_SYMBOL (node)
2321 if (node->alias
2322 && !TREE_ASM_WRITTEN (node->decl)
2323 && (!(cnode = dyn_cast <cgraph_node *> (node))
2324 || !cnode->instrumented_version
2325 || !TREE_ASM_WRITTEN (cnode->instrumented_version->decl))
2326 && node->weakref)
2328 tree target;
2330 /* Weakrefs are special by not requiring target definition in current
2331 compilation unit. It is thus bit hard to work out what we want to
2332 alias.
2333 When alias target is defined, we need to fetch it from symtab reference,
2334 otherwise it is pointed to by alias_target. */
2335 if (node->alias_target)
2336 target = (DECL_P (node->alias_target)
2337 ? DECL_ASSEMBLER_NAME (node->alias_target)
2338 : node->alias_target);
2339 else if (node->analyzed)
2340 target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
2341 else
2343 gcc_unreachable ();
2344 target = get_alias_symbol (node->decl);
2346 do_assemble_alias (node->decl, target);
2350 /* Perform simple optimizations based on callgraph. */
2352 void
2353 symbol_table::compile (void)
2355 if (seen_error ())
2356 return;
2358 symtab_node::checking_verify_symtab_nodes ();
2360 timevar_push (TV_CGRAPHOPT);
2361 if (pre_ipa_mem_report)
2363 fprintf (stderr, "Memory consumption before IPA\n");
2364 dump_memory_report (false);
2366 if (!quiet_flag)
2367 fprintf (stderr, "Performing interprocedural optimizations\n");
2368 state = IPA;
2370 /* Offloading requires LTO infrastructure. */
2371 if (!in_lto_p && g->have_offload)
2372 flag_generate_offload = 1;
2374 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2375 if (flag_generate_lto || flag_generate_offload)
2376 lto_streamer_hooks_init ();
2378 /* Don't run the IPA passes if there was any error or sorry messages. */
2379 if (!seen_error ())
2380 ipa_passes ();
2382 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2383 if (seen_error ()
2384 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
2386 timevar_pop (TV_CGRAPHOPT);
2387 return;
2390 global_info_ready = true;
2391 if (dump_file)
2393 fprintf (dump_file, "Optimized ");
2394 symtab_node:: dump_table (dump_file);
2396 if (post_ipa_mem_report)
2398 fprintf (stderr, "Memory consumption after IPA\n");
2399 dump_memory_report (false);
2401 timevar_pop (TV_CGRAPHOPT);
2403 /* Output everything. */
2404 (*debug_hooks->assembly_start) ();
2405 if (!quiet_flag)
2406 fprintf (stderr, "Assembling functions:\n");
2407 symtab_node::checking_verify_symtab_nodes ();
2409 materialize_all_clones ();
2410 bitmap_obstack_initialize (NULL);
2411 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2412 bitmap_obstack_release (NULL);
2413 mark_functions_to_output ();
2415 /* When weakref support is missing, we autmatically translate all
2416 references to NODE to references to its ultimate alias target.
2417 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2418 TREE_CHAIN.
2420 Set up this mapping before we output any assembler but once we are sure
2421 that all symbol renaming is done.
2423 FIXME: All this uglyness can go away if we just do renaming at gimple
2424 level by physically rewritting the IL. At the moment we can only redirect
2425 calls, so we need infrastructure for renaming references as well. */
2426 #ifndef ASM_OUTPUT_WEAKREF
2427 symtab_node *node;
2429 FOR_EACH_SYMBOL (node)
2430 if (node->alias
2431 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2433 IDENTIFIER_TRANSPARENT_ALIAS
2434 (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2435 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2436 = (node->alias_target ? node->alias_target
2437 : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
2439 #endif
2441 state = EXPANSION;
2443 if (!flag_toplevel_reorder)
2444 output_in_order (false);
2445 else
2447 /* Output first asm statements and anything ordered. The process
2448 flag is cleared for these nodes, so we skip them later. */
2449 output_in_order (true);
2450 expand_all_functions ();
2451 output_variables ();
2454 process_new_functions ();
2455 state = FINISHED;
2456 output_weakrefs ();
2458 if (dump_file)
2460 fprintf (dump_file, "\nFinal ");
2461 symtab_node::dump_table (dump_file);
2463 if (!flag_checking)
2464 return;
2465 symtab_node::verify_symtab_nodes ();
2466 /* Double check that all inline clones are gone and that all
2467 function bodies have been released from memory. */
2468 if (!seen_error ())
2470 cgraph_node *node;
2471 bool error_found = false;
2473 FOR_EACH_DEFINED_FUNCTION (node)
2474 if (node->global.inlined_to
2475 || gimple_has_body_p (node->decl))
2477 error_found = true;
2478 node->debug ();
2480 if (error_found)
2481 internal_error ("nodes with unreleased memory found");
2486 /* Analyze the whole compilation unit once it is parsed completely. */
2488 void
2489 symbol_table::finalize_compilation_unit (void)
2491 timevar_push (TV_CGRAPH);
2493 /* If we're here there's no current function anymore. Some frontends
2494 are lazy in clearing these. */
2495 current_function_decl = NULL;
2496 set_cfun (NULL);
2498 /* Do not skip analyzing the functions if there were errors, we
2499 miss diagnostics for following functions otherwise. */
2501 /* Emit size functions we didn't inline. */
2502 finalize_size_functions ();
2504 /* Mark alias targets necessary and emit diagnostics. */
2505 handle_alias_pairs ();
2507 if (!quiet_flag)
2509 fprintf (stderr, "\nAnalyzing compilation unit\n");
2510 fflush (stderr);
2513 if (flag_dump_passes)
2514 dump_passes ();
2516 /* Gimplify and lower all functions, compute reachability and
2517 remove unreachable nodes. */
2518 analyze_functions (/*first_time=*/true);
2520 /* Mark alias targets necessary and emit diagnostics. */
2521 handle_alias_pairs ();
2523 /* Gimplify and lower thunks. */
2524 analyze_functions (/*first_time=*/false);
2526 if (!seen_error ())
2528 /* Emit early debug for reachable functions, and by consequence,
2529 locally scoped symbols. */
2530 struct cgraph_node *cnode;
2531 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (cnode)
2532 (*debug_hooks->early_global_decl) (cnode->decl);
2534 /* Clean up anything that needs cleaning up after initial debug
2535 generation. */
2536 (*debug_hooks->early_finish) ();
2539 /* Finally drive the pass manager. */
2540 compile ();
2542 timevar_pop (TV_CGRAPH);
2545 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2546 within the same process. For use by toplev::finalize. */
2548 void
2549 cgraphunit_c_finalize (void)
2551 gcc_assert (cgraph_new_nodes.length () == 0);
2552 cgraph_new_nodes.truncate (0);
2554 vtable_entry_type = NULL;
2555 queued_nodes = &symtab_terminator;
2557 first_analyzed = NULL;
2558 first_analyzed_var = NULL;
2561 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2562 kind of wrapper method. */
2564 void
2565 cgraph_node::create_wrapper (cgraph_node *target)
2567 /* Preserve DECL_RESULT so we get right by reference flag. */
2568 tree decl_result = DECL_RESULT (decl);
2570 /* Remove the function's body but keep arguments to be reused
2571 for thunk. */
2572 release_body (true);
2573 reset ();
2575 DECL_UNINLINABLE (decl) = false;
2576 DECL_RESULT (decl) = decl_result;
2577 DECL_INITIAL (decl) = NULL;
2578 allocate_struct_function (decl, false);
2579 set_cfun (NULL);
2581 /* Turn alias into thunk and expand it into GIMPLE representation. */
2582 definition = true;
2584 memset (&thunk, 0, sizeof (cgraph_thunk_info));
2585 thunk.thunk_p = true;
2586 create_edge (target, NULL, count, CGRAPH_FREQ_BASE);
2587 callees->can_throw_external = !TREE_NOTHROW (target->decl);
2589 tree arguments = DECL_ARGUMENTS (decl);
2591 while (arguments)
2593 TREE_ADDRESSABLE (arguments) = false;
2594 arguments = TREE_CHAIN (arguments);
2597 expand_thunk (false, true);
2599 /* Inline summary set-up. */
2600 analyze ();
2601 inline_analyze_function (this);
2604 #include "gt-cgraphunit.h"