* fold-const.c (fold_binary_loc): Don't fold if the result
[official-gcc.git] / gcc / cgraphunit.c
blob25af2347f42844a27d7f48dc8009ce78d8ad33cc
1 /* Driver of optimization process
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This module implements main driver of compilation process.
23 The main scope of this file is to act as an interface in between
24 tree based frontends and the backend.
26 The front-end is supposed to use following functionality:
28 - finalize_function
30 This function is called once front-end has parsed whole body of function
31 and it is certain that the function body nor the declaration will change.
33 (There is one exception needed for implementing GCC extern inline
34 function.)
36 - varpool_finalize_decl
38 This function has same behavior as the above but is used for static
39 variables.
41 - add_asm_node
43 Insert new toplevel ASM statement
45 - finalize_compilation_unit
47 This function is called once (source level) compilation unit is finalized
48 and it will no longer change.
50 The symbol table is constructed starting from the trivially needed
51 symbols finalized by the frontend. Functions are lowered into
52 GIMPLE representation and callgraph/reference lists are constructed.
53 Those are used to discover other necessary functions and variables.
55 At the end the bodies of unreachable functions are removed.
57 The function can be called multiple times when multiple source level
58 compilation units are combined.
60 - compile
62 This passes control to the back-end. Optimizations are performed and
63 final assembler is generated. This is done in the following way. Note
64 that with link time optimization the process is split into three
65 stages (compile time, linktime analysis and parallel linktime as
66 indicated bellow).
68 Compile time:
70 1) Inter-procedural optimization.
71 (ipa_passes)
73 This part is further split into:
75 a) early optimizations. These are local passes executed in
76 the topological order on the callgraph.
78 The purpose of early optimiations is to optimize away simple
79 things that may otherwise confuse IP analysis. Very simple
80 propagation across the callgraph is done i.e. to discover
81 functions without side effects and simple inlining is performed.
83 b) early small interprocedural passes.
85 Those are interprocedural passes executed only at compilation
86 time. These include, for example, transational memory lowering,
87 unreachable code removal and other simple transformations.
89 c) IP analysis stage. All interprocedural passes do their
90 analysis.
92 Interprocedural passes differ from small interprocedural
93 passes by their ability to operate across whole program
94 at linktime. Their analysis stage is performed early to
95 both reduce linking times and linktime memory usage by
96 not having to represent whole program in memory.
98 d) LTO sreaming. When doing LTO, everything important gets
99 streamed into the object file.
101 Compile time and or linktime analysis stage (WPA):
103 At linktime units gets streamed back and symbol table is
104 merged. Function bodies are not streamed in and not
105 available.
106 e) IP propagation stage. All IP passes execute their
107 IP propagation. This is done based on the earlier analysis
108 without having function bodies at hand.
109 f) Ltrans streaming. When doing WHOPR LTO, the program
110 is partitioned and streamed into multple object files.
112 Compile time and/or parallel linktime stage (ltrans)
114 Each of the object files is streamed back and compiled
115 separately. Now the function bodies becomes available
116 again.
118 2) Virtual clone materialization
119 (cgraph_materialize_clone)
121 IP passes can produce copies of existing functoins (such
122 as versioned clones or inline clones) without actually
123 manipulating their bodies by creating virtual clones in
124 the callgraph. At this time the virtual clones are
125 turned into real functions
126 3) IP transformation
128 All IP passes transform function bodies based on earlier
129 decision of the IP propagation.
131 4) late small IP passes
133 Simple IP passes working within single program partition.
135 5) Expansion
136 (expand_all_functions)
138 At this stage functions that needs to be output into
139 assembler are identified and compiled in topological order
140 6) Output of variables and aliases
141 Now it is known what variable references was not optimized
142 out and thus all variables are output to the file.
144 Note that with -fno-toplevel-reorder passes 5 and 6
145 are combined together in cgraph_output_in_order.
147 Finally there are functions to manipulate the callgraph from
148 backend.
149 - cgraph_add_new_function is used to add backend produced
150 functions introduced after the unit is finalized.
151 The functions are enqueue for later processing and inserted
152 into callgraph with cgraph_process_new_functions.
154 - cgraph_function_versioning
156 produces a copy of function into new one (a version)
157 and apply simple transformations
160 #include "config.h"
161 #include "system.h"
162 #include "coretypes.h"
163 #include "tm.h"
164 #include "tree.h"
165 #include "varasm.h"
166 #include "stor-layout.h"
167 #include "stringpool.h"
168 #include "output.h"
169 #include "rtl.h"
170 #include "predict.h"
171 #include "vec.h"
172 #include "hashtab.h"
173 #include "hash-set.h"
174 #include "machmode.h"
175 #include "hard-reg-set.h"
176 #include "input.h"
177 #include "function.h"
178 #include "basic-block.h"
179 #include "tree-ssa-alias.h"
180 #include "internal-fn.h"
181 #include "gimple-fold.h"
182 #include "gimple-expr.h"
183 #include "is-a.h"
184 #include "gimple.h"
185 #include "gimplify.h"
186 #include "gimple-iterator.h"
187 #include "gimplify-me.h"
188 #include "gimple-ssa.h"
189 #include "tree-cfg.h"
190 #include "tree-into-ssa.h"
191 #include "tree-ssa.h"
192 #include "tree-inline.h"
193 #include "langhooks.h"
194 #include "toplev.h"
195 #include "flags.h"
196 #include "debug.h"
197 #include "target.h"
198 #include "diagnostic.h"
199 #include "params.h"
200 #include "fibheap.h"
201 #include "intl.h"
202 #include "hash-map.h"
203 #include "plugin-api.h"
204 #include "ipa-ref.h"
205 #include "cgraph.h"
206 #include "alloc-pool.h"
207 #include "ipa-prop.h"
208 #include "tree-iterator.h"
209 #include "tree-pass.h"
210 #include "tree-dump.h"
211 #include "gimple-pretty-print.h"
212 #include "output.h"
213 #include "coverage.h"
214 #include "plugin.h"
215 #include "ipa-inline.h"
216 #include "ipa-utils.h"
217 #include "lto-streamer.h"
218 #include "except.h"
219 #include "cfgloop.h"
220 #include "regset.h" /* FIXME: For reg_obstack. */
221 #include "context.h"
222 #include "pass_manager.h"
223 #include "tree-nested.h"
224 #include "gimplify.h"
225 #include "dbgcnt.h"
226 #include "tree-chkp.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 /* When IPA optimization already started, do all essential
330 transformations that has been already performed on the whole
331 cgraph but not on this function. */
333 gimple_register_cfg_hooks ();
334 if (!node->analyzed)
335 node->analyze ();
336 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
337 if (state == IPA_SSA
338 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
339 g->get_passes ()->execute_early_local_passes ();
340 else if (inline_summary_vec != NULL)
341 compute_inline_parameters (node, true);
342 free_dominance_info (CDI_POST_DOMINATORS);
343 free_dominance_info (CDI_DOMINATORS);
344 pop_cfun ();
345 call_cgraph_insertion_hooks (node);
346 break;
348 case EXPANSION:
349 /* Functions created during expansion shall be compiled
350 directly. */
351 node->process = 0;
352 call_cgraph_insertion_hooks (node);
353 node->expand ();
354 break;
356 default:
357 gcc_unreachable ();
358 break;
362 cgraph_new_nodes.release ();
365 /* As an GCC extension we allow redefinition of the function. The
366 semantics when both copies of bodies differ is not well defined.
367 We replace the old body with new body so in unit at a time mode
368 we always use new body, while in normal mode we may end up with
369 old body inlined into some functions and new body expanded and
370 inlined in others.
372 ??? It may make more sense to use one body for inlining and other
373 body for expanding the function but this is difficult to do. */
375 void
376 cgraph_node::reset (void)
378 /* If process is set, then we have already begun whole-unit analysis.
379 This is *not* testing for whether we've already emitted the function.
380 That case can be sort-of legitimately seen with real function redefinition
381 errors. I would argue that the front end should never present us with
382 such a case, but don't enforce that for now. */
383 gcc_assert (!process);
385 /* Reset our data structures so we can analyze the function again. */
386 memset (&local, 0, sizeof (local));
387 memset (&global, 0, sizeof (global));
388 memset (&rtl, 0, sizeof (rtl));
389 analyzed = false;
390 definition = false;
391 alias = false;
392 weakref = false;
393 cpp_implicit_alias = false;
395 remove_callees ();
396 remove_all_references ();
399 /* Return true when there are references to the node. */
401 bool
402 symtab_node::referred_to_p (void)
404 ipa_ref *ref = NULL;
406 /* See if there are any references at all. */
407 if (iterate_referring (0, ref))
408 return true;
409 /* For functions check also calls. */
410 cgraph_node *cn = dyn_cast <cgraph_node *> (this);
411 if (cn && cn->callers)
412 return true;
413 return false;
416 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
417 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
418 the garbage collector run at the moment. We would need to either create
419 a new GC context, or just not compile right now. */
421 void
422 cgraph_node::finalize_function (tree decl, bool no_collect)
424 cgraph_node *node = cgraph_node::get_create (decl);
426 if (node->definition)
428 /* Nested functions should only be defined once. */
429 gcc_assert (!DECL_CONTEXT (decl)
430 || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL);
431 node->reset ();
432 node->local.redefined_extern_inline = true;
435 notice_global_symbol (decl);
436 node->definition = true;
437 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
439 /* With -fkeep-inline-functions we are keeping all inline functions except
440 for extern inline ones. */
441 if (flag_keep_inline_functions
442 && DECL_DECLARED_INLINE_P (decl)
443 && !DECL_EXTERNAL (decl)
444 && !DECL_DISREGARD_INLINE_LIMITS (decl))
445 node->force_output = 1;
447 /* When not optimizing, also output the static functions. (see
448 PR24561), but don't do so for always_inline functions, functions
449 declared inline and nested functions. These were optimized out
450 in the original implementation and it is unclear whether we want
451 to change the behavior here. */
452 if ((!optimize
453 && !node->cpp_implicit_alias
454 && !DECL_DISREGARD_INLINE_LIMITS (decl)
455 && !DECL_DECLARED_INLINE_P (decl)
456 && !(DECL_CONTEXT (decl)
457 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
458 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
459 node->force_output = 1;
461 /* If we've not yet emitted decl, tell the debug info about it. */
462 if (!TREE_ASM_WRITTEN (decl))
463 (*debug_hooks->deferred_inline_function) (decl);
465 /* Possibly warn about unused parameters. */
466 if (warn_unused_parameter)
467 do_warn_unused_parameter (decl);
469 if (!no_collect)
470 ggc_collect ();
472 if (symtab->state == CONSTRUCTION
473 && (node->needed_p () || node->referred_to_p ()))
474 enqueue_node (node);
477 /* Add the function FNDECL to the call graph.
478 Unlike finalize_function, this function is intended to be used
479 by middle end and allows insertion of new function at arbitrary point
480 of compilation. The function can be either in high, low or SSA form
481 GIMPLE.
483 The function is assumed to be reachable and have address taken (so no
484 API breaking optimizations are performed on it).
486 Main work done by this function is to enqueue the function for later
487 processing to avoid need the passes to be re-entrant. */
489 void
490 cgraph_node::add_new_function (tree fndecl, bool lowered)
492 gcc::pass_manager *passes = g->get_passes ();
493 cgraph_node *node;
494 switch (symtab->state)
496 case PARSING:
497 cgraph_node::finalize_function (fndecl, false);
498 break;
499 case CONSTRUCTION:
500 /* Just enqueue function to be processed at nearest occurrence. */
501 node = cgraph_node::get_create (fndecl);
502 if (lowered)
503 node->lowered = true;
504 cgraph_new_nodes.safe_push (node);
505 break;
507 case IPA:
508 case IPA_SSA:
509 case EXPANSION:
510 /* Bring the function into finalized state and enqueue for later
511 analyzing and compilation. */
512 node = cgraph_node::get_create (fndecl);
513 node->local.local = false;
514 node->definition = true;
515 node->force_output = true;
516 if (!lowered && symtab->state == EXPANSION)
518 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
519 gimple_register_cfg_hooks ();
520 bitmap_obstack_initialize (NULL);
521 execute_pass_list (cfun, passes->all_lowering_passes);
522 passes->execute_early_local_passes ();
523 bitmap_obstack_release (NULL);
524 pop_cfun ();
526 lowered = true;
528 if (lowered)
529 node->lowered = true;
530 cgraph_new_nodes.safe_push (node);
531 break;
533 case FINISHED:
534 /* At the very end of compilation we have to do all the work up
535 to expansion. */
536 node = cgraph_node::create (fndecl);
537 if (lowered)
538 node->lowered = true;
539 node->definition = true;
540 node->analyze ();
541 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
542 gimple_register_cfg_hooks ();
543 bitmap_obstack_initialize (NULL);
544 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
545 g->get_passes ()->execute_early_local_passes ();
546 bitmap_obstack_release (NULL);
547 pop_cfun ();
548 node->expand ();
549 break;
551 default:
552 gcc_unreachable ();
555 /* Set a personality if required and we already passed EH lowering. */
556 if (lowered
557 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
558 == eh_personality_lang))
559 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
562 /* Analyze the function scheduled to be output. */
563 void
564 cgraph_node::analyze (void)
566 tree decl = this->decl;
567 location_t saved_loc = input_location;
568 input_location = DECL_SOURCE_LOCATION (decl);
570 if (thunk.thunk_p)
572 create_edge (cgraph_node::get (thunk.alias),
573 NULL, 0, CGRAPH_FREQ_BASE);
574 if (!expand_thunk (false, false))
576 thunk.alias = NULL;
577 return;
579 thunk.alias = NULL;
581 if (alias)
582 resolve_alias (cgraph_node::get (alias_target));
583 else if (dispatcher_function)
585 /* Generate the dispatcher body of multi-versioned functions. */
586 cgraph_function_version_info *dispatcher_version_info
587 = function_version ();
588 if (dispatcher_version_info != NULL
589 && (dispatcher_version_info->dispatcher_resolver
590 == NULL_TREE))
592 tree resolver = NULL_TREE;
593 gcc_assert (targetm.generate_version_dispatcher_body);
594 resolver = targetm.generate_version_dispatcher_body (this);
595 gcc_assert (resolver != NULL_TREE);
598 else
600 push_cfun (DECL_STRUCT_FUNCTION (decl));
602 assign_assembler_name_if_neeeded (decl);
604 /* Make sure to gimplify bodies only once. During analyzing a
605 function we lower it, which will require gimplified nested
606 functions, so we can end up here with an already gimplified
607 body. */
608 if (!gimple_has_body_p (decl))
609 gimplify_function_tree (decl);
610 dump_function (TDI_generic, decl);
612 /* Lower the function. */
613 if (!lowered)
615 if (nested)
616 lower_nested_functions (decl);
617 gcc_assert (!nested);
619 gimple_register_cfg_hooks ();
620 bitmap_obstack_initialize (NULL);
621 execute_pass_list (cfun, g->get_passes ()->all_lowering_passes);
622 free_dominance_info (CDI_POST_DOMINATORS);
623 free_dominance_info (CDI_DOMINATORS);
624 compact_blocks ();
625 bitmap_obstack_release (NULL);
626 lowered = true;
629 pop_cfun ();
631 analyzed = true;
633 input_location = saved_loc;
636 /* C++ frontend produce same body aliases all over the place, even before PCH
637 gets streamed out. It relies on us linking the aliases with their function
638 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
639 first produce aliases without links, but once C++ FE is sure he won't sream
640 PCH we build the links via this function. */
642 void
643 symbol_table::process_same_body_aliases (void)
645 symtab_node *node;
646 FOR_EACH_SYMBOL (node)
647 if (node->cpp_implicit_alias && !node->analyzed)
648 node->resolve_alias
649 (TREE_CODE (node->alias_target) == VAR_DECL
650 ? (symtab_node *)varpool_node::get_create (node->alias_target)
651 : (symtab_node *)cgraph_node::get_create (node->alias_target));
652 cpp_implicit_aliases_done = true;
655 /* Process attributes common for vars and functions. */
657 static void
658 process_common_attributes (symtab_node *node, tree decl)
660 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
662 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
664 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
665 "%<weakref%> attribute should be accompanied with"
666 " an %<alias%> attribute");
667 DECL_WEAK (decl) = 0;
668 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
669 DECL_ATTRIBUTES (decl));
672 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
673 node->no_reorder = 1;
676 /* Look for externally_visible and used attributes and mark cgraph nodes
677 accordingly.
679 We cannot mark the nodes at the point the attributes are processed (in
680 handle_*_attribute) because the copy of the declarations available at that
681 point may not be canonical. For example, in:
683 void f();
684 void f() __attribute__((used));
686 the declaration we see in handle_used_attribute will be the second
687 declaration -- but the front end will subsequently merge that declaration
688 with the original declaration and discard the second declaration.
690 Furthermore, we can't mark these nodes in finalize_function because:
692 void f() {}
693 void f() __attribute__((externally_visible));
695 is valid.
697 So, we walk the nodes at the end of the translation unit, applying the
698 attributes at that point. */
700 static void
701 process_function_and_variable_attributes (cgraph_node *first,
702 varpool_node *first_var)
704 cgraph_node *node;
705 varpool_node *vnode;
707 for (node = symtab->first_function (); node != first;
708 node = symtab->next_function (node))
710 tree decl = node->decl;
711 if (DECL_PRESERVE_P (decl))
712 node->mark_force_output ();
713 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
715 if (! TREE_PUBLIC (node->decl))
716 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
717 "%<externally_visible%>"
718 " attribute have effect only on public objects");
720 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
721 && (node->definition && !node->alias))
723 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
724 "%<weakref%> attribute ignored"
725 " because function is defined");
726 DECL_WEAK (decl) = 0;
727 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
728 DECL_ATTRIBUTES (decl));
731 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
732 && !DECL_DECLARED_INLINE_P (decl)
733 /* redefining extern inline function makes it DECL_UNINLINABLE. */
734 && !DECL_UNINLINABLE (decl))
735 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
736 "always_inline function might not be inlinable");
738 process_common_attributes (node, decl);
740 for (vnode = symtab->first_variable (); vnode != first_var;
741 vnode = symtab->next_variable (vnode))
743 tree decl = vnode->decl;
744 if (DECL_EXTERNAL (decl)
745 && DECL_INITIAL (decl))
746 varpool_node::finalize_decl (decl);
747 if (DECL_PRESERVE_P (decl))
748 vnode->force_output = true;
749 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
751 if (! TREE_PUBLIC (vnode->decl))
752 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
753 "%<externally_visible%>"
754 " attribute have effect only on public objects");
756 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
757 && vnode->definition
758 && DECL_INITIAL (decl))
760 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
761 "%<weakref%> attribute ignored"
762 " because variable is initialized");
763 DECL_WEAK (decl) = 0;
764 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
765 DECL_ATTRIBUTES (decl));
767 process_common_attributes (vnode, decl);
771 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
772 middle end to output the variable to asm file, if needed or externally
773 visible. */
775 void
776 varpool_node::finalize_decl (tree decl)
778 varpool_node *node = varpool_node::get_create (decl);
780 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
782 if (node->definition)
783 return;
784 notice_global_symbol (decl);
785 node->definition = true;
786 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
787 /* Traditionally we do not eliminate static variables when not
788 optimizing and when not doing toplevel reoder. */
789 || node->no_reorder
790 || ((!flag_toplevel_reorder
791 && !DECL_COMDAT (node->decl)
792 && !DECL_ARTIFICIAL (node->decl))))
793 node->force_output = true;
795 if (symtab->state == CONSTRUCTION
796 && (node->needed_p () || node->referred_to_p ()))
797 enqueue_node (node);
798 if (symtab->state >= IPA_SSA)
799 node->analyze ();
800 /* Some frontends produce various interface variables after compilation
801 finished. */
802 if (symtab->state == FINISHED
803 || (!flag_toplevel_reorder
804 && symtab->state == EXPANSION))
805 node->assemble_decl ();
807 if (DECL_INITIAL (decl))
808 chkp_register_var_initializer (decl);
811 /* EDGE is an polymorphic call. Mark all possible targets as reachable
812 and if there is only one target, perform trivial devirtualization.
813 REACHABLE_CALL_TARGETS collects target lists we already walked to
814 avoid udplicate work. */
816 static void
817 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
818 cgraph_edge *edge)
820 unsigned int i;
821 void *cache_token;
822 bool final;
823 vec <cgraph_node *>targets
824 = possible_polymorphic_call_targets
825 (edge, &final, &cache_token);
827 if (!reachable_call_targets->add (cache_token))
829 if (symtab->dump_file)
830 dump_possible_polymorphic_call_targets
831 (symtab->dump_file, edge);
833 for (i = 0; i < targets.length (); i++)
835 /* Do not bother to mark virtual methods in anonymous namespace;
836 either we will find use of virtual table defining it, or it is
837 unused. */
838 if (targets[i]->definition
839 && TREE_CODE
840 (TREE_TYPE (targets[i]->decl))
841 == METHOD_TYPE
842 && !type_in_anonymous_namespace_p
843 (method_class_type
844 (TREE_TYPE (targets[i]->decl))))
845 enqueue_node (targets[i]);
849 /* Very trivial devirtualization; when the type is
850 final or anonymous (so we know all its derivation)
851 and there is only one possible virtual call target,
852 make the edge direct. */
853 if (final)
855 if (targets.length () <= 1 && dbg_cnt (devirt))
857 cgraph_node *target;
858 if (targets.length () == 1)
859 target = targets[0];
860 else
861 target = cgraph_node::create
862 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
864 if (symtab->dump_file)
866 fprintf (symtab->dump_file,
867 "Devirtualizing call: ");
868 print_gimple_stmt (symtab->dump_file,
869 edge->call_stmt, 0,
870 TDF_SLIM);
872 if (dump_enabled_p ())
874 location_t locus = gimple_location_safe (edge->call_stmt);
875 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
876 "devirtualizing call in %s to %s\n",
877 edge->caller->name (), target->name ());
880 edge->make_direct (target);
881 edge->redirect_call_stmt_to_callee ();
883 /* Call to __builtin_unreachable shouldn't be instrumented. */
884 if (!targets.length ())
885 gimple_call_set_with_bounds (edge->call_stmt, false);
887 if (symtab->dump_file)
889 fprintf (symtab->dump_file,
890 "Devirtualized as: ");
891 print_gimple_stmt (symtab->dump_file,
892 edge->call_stmt, 0,
893 TDF_SLIM);
900 /* Discover all functions and variables that are trivially needed, analyze
901 them as well as all functions and variables referred by them */
902 static cgraph_node *first_analyzed;
903 static varpool_node *first_analyzed_var;
905 static void
906 analyze_functions (void)
908 /* Keep track of already processed nodes when called multiple times for
909 intermodule optimization. */
910 cgraph_node *first_handled = first_analyzed;
911 varpool_node *first_handled_var = first_analyzed_var;
912 hash_set<void *> reachable_call_targets;
914 symtab_node *node;
915 symtab_node *next;
916 int i;
917 ipa_ref *ref;
918 bool changed = true;
919 location_t saved_loc = input_location;
921 bitmap_obstack_initialize (NULL);
922 symtab->state = CONSTRUCTION;
923 input_location = UNKNOWN_LOCATION;
925 /* Ugly, but the fixup can not happen at a time same body alias is created;
926 C++ FE is confused about the COMDAT groups being right. */
927 if (symtab->cpp_implicit_aliases_done)
928 FOR_EACH_SYMBOL (node)
929 if (node->cpp_implicit_alias)
930 node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
931 if (optimize && flag_devirtualize)
932 build_type_inheritance_graph ();
934 /* Analysis adds static variables that in turn adds references to new functions.
935 So we need to iterate the process until it stabilize. */
936 while (changed)
938 changed = false;
939 process_function_and_variable_attributes (first_analyzed,
940 first_analyzed_var);
942 /* First identify the trivially needed symbols. */
943 for (node = symtab->first_symbol ();
944 node != first_analyzed
945 && node != first_analyzed_var; node = node->next)
947 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
948 node->get_comdat_group_id ();
949 if (node->needed_p ())
951 enqueue_node (node);
952 if (!changed && symtab->dump_file)
953 fprintf (symtab->dump_file, "Trivially needed symbols:");
954 changed = true;
955 if (symtab->dump_file)
956 fprintf (symtab->dump_file, " %s", node->asm_name ());
957 if (!changed && symtab->dump_file)
958 fprintf (symtab->dump_file, "\n");
960 if (node == first_analyzed
961 || node == first_analyzed_var)
962 break;
964 symtab->process_new_functions ();
965 first_analyzed_var = symtab->first_variable ();
966 first_analyzed = symtab->first_function ();
968 if (changed && symtab->dump_file)
969 fprintf (symtab->dump_file, "\n");
971 /* Lower representation, build callgraph edges and references for all trivially
972 needed symbols and all symbols referred by them. */
973 while (queued_nodes != &symtab_terminator)
975 changed = true;
976 node = queued_nodes;
977 queued_nodes = (symtab_node *)queued_nodes->aux;
978 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
979 if (cnode && cnode->definition)
981 cgraph_edge *edge;
982 tree decl = cnode->decl;
984 /* ??? It is possible to create extern inline function
985 and later using weak alias attribute to kill its body.
986 See gcc.c-torture/compile/20011119-1.c */
987 if (!DECL_STRUCT_FUNCTION (decl)
988 && !cnode->alias
989 && !cnode->thunk.thunk_p
990 && !cnode->dispatcher_function)
992 cnode->reset ();
993 cnode->local.redefined_extern_inline = true;
994 continue;
997 if (!cnode->analyzed)
998 cnode->analyze ();
1000 for (edge = cnode->callees; edge; edge = edge->next_callee)
1001 if (edge->callee->definition)
1002 enqueue_node (edge->callee);
1003 if (optimize && flag_devirtualize)
1005 cgraph_edge *next;
1007 for (edge = cnode->indirect_calls; edge; edge = next)
1009 next = edge->next_callee;
1010 if (edge->indirect_info->polymorphic)
1011 walk_polymorphic_call_targets (&reachable_call_targets,
1012 edge);
1016 /* If decl is a clone of an abstract function,
1017 mark that abstract function so that we don't release its body.
1018 The DECL_INITIAL() of that abstract function declaration
1019 will be later needed to output debug info. */
1020 if (DECL_ABSTRACT_ORIGIN (decl))
1022 cgraph_node *origin_node
1023 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl));
1024 origin_node->used_as_abstract_origin = true;
1027 else
1029 varpool_node *vnode = dyn_cast <varpool_node *> (node);
1030 if (vnode && vnode->definition && !vnode->analyzed)
1031 vnode->analyze ();
1034 if (node->same_comdat_group)
1036 symtab_node *next;
1037 for (next = node->same_comdat_group;
1038 next != node;
1039 next = next->same_comdat_group)
1040 enqueue_node (next);
1042 for (i = 0; node->iterate_reference (i, ref); i++)
1043 if (ref->referred->definition)
1044 enqueue_node (ref->referred);
1045 symtab->process_new_functions ();
1048 if (optimize && flag_devirtualize)
1049 update_type_inheritance_graph ();
1051 /* Collect entry points to the unit. */
1052 if (symtab->dump_file)
1054 fprintf (symtab->dump_file, "\n\nInitial ");
1055 symtab_node::dump_table (symtab->dump_file);
1058 if (symtab->dump_file)
1059 fprintf (symtab->dump_file, "\nRemoving unused symbols:");
1061 for (node = symtab->first_symbol ();
1062 node != first_handled
1063 && node != first_handled_var; node = next)
1065 next = node->next;
1066 if (!node->aux && !node->referred_to_p ())
1068 if (symtab->dump_file)
1069 fprintf (symtab->dump_file, " %s", node->name ());
1070 node->remove ();
1071 continue;
1073 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1075 tree decl = node->decl;
1077 if (cnode->definition && !gimple_has_body_p (decl)
1078 && !cnode->alias
1079 && !cnode->thunk.thunk_p)
1080 cnode->reset ();
1082 gcc_assert (!cnode->definition || cnode->thunk.thunk_p
1083 || cnode->alias
1084 || gimple_has_body_p (decl));
1085 gcc_assert (cnode->analyzed == cnode->definition);
1087 node->aux = NULL;
1089 for (;node; node = node->next)
1090 node->aux = NULL;
1091 first_analyzed = symtab->first_function ();
1092 first_analyzed_var = symtab->first_variable ();
1093 if (symtab->dump_file)
1095 fprintf (symtab->dump_file, "\n\nReclaimed ");
1096 symtab_node::dump_table (symtab->dump_file);
1098 bitmap_obstack_release (NULL);
1099 ggc_collect ();
1100 /* Initialize assembler name hash, in particular we want to trigger C++
1101 mangling and same body alias creation before we free DECL_ARGUMENTS
1102 used by it. */
1103 if (!seen_error ())
1104 symtab->symtab_initialize_asm_name_hash ();
1106 input_location = saved_loc;
1109 /* Translate the ugly representation of aliases as alias pairs into nice
1110 representation in callgraph. We don't handle all cases yet,
1111 unfortunately. */
1113 static void
1114 handle_alias_pairs (void)
1116 alias_pair *p;
1117 unsigned i;
1119 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1121 symtab_node *target_node = symtab_node::get_for_asmname (p->target);
1123 /* Weakrefs with target not defined in current unit are easy to handle:
1124 they behave just as external variables except we need to note the
1125 alias flag to later output the weakref pseudo op into asm file. */
1126 if (!target_node
1127 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1129 symtab_node *node = symtab_node::get (p->decl);
1130 if (node)
1132 node->alias_target = p->target;
1133 node->weakref = true;
1134 node->alias = true;
1136 alias_pairs->unordered_remove (i);
1137 continue;
1139 else if (!target_node)
1141 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1142 symtab_node *node = symtab_node::get (p->decl);
1143 if (node)
1144 node->alias = false;
1145 alias_pairs->unordered_remove (i);
1146 continue;
1149 if (DECL_EXTERNAL (target_node->decl)
1150 /* We use local aliases for C++ thunks to force the tailcall
1151 to bind locally. This is a hack - to keep it working do
1152 the following (which is not strictly correct). */
1153 && (TREE_CODE (target_node->decl) != FUNCTION_DECL
1154 || ! DECL_VIRTUAL_P (target_node->decl))
1155 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1157 error ("%q+D aliased to external symbol %qE",
1158 p->decl, p->target);
1161 if (TREE_CODE (p->decl) == FUNCTION_DECL
1162 && target_node && is_a <cgraph_node *> (target_node))
1164 cgraph_node *src_node = cgraph_node::get (p->decl);
1165 if (src_node && src_node->definition)
1166 src_node->reset ();
1167 cgraph_node::create_alias (p->decl, target_node->decl);
1168 alias_pairs->unordered_remove (i);
1170 else if (TREE_CODE (p->decl) == VAR_DECL
1171 && target_node && is_a <varpool_node *> (target_node))
1173 varpool_node::create_alias (p->decl, target_node->decl);
1174 alias_pairs->unordered_remove (i);
1176 else
1178 error ("%q+D alias in between function and variable is not supported",
1179 p->decl);
1180 warning (0, "%q+D aliased declaration",
1181 target_node->decl);
1182 alias_pairs->unordered_remove (i);
1185 vec_free (alias_pairs);
1189 /* Figure out what functions we want to assemble. */
1191 static void
1192 mark_functions_to_output (void)
1194 cgraph_node *node;
1195 #ifdef ENABLE_CHECKING
1196 bool check_same_comdat_groups = false;
1198 FOR_EACH_FUNCTION (node)
1199 gcc_assert (!node->process);
1200 #endif
1202 FOR_EACH_FUNCTION (node)
1204 tree decl = node->decl;
1206 gcc_assert (!node->process || node->same_comdat_group);
1207 if (node->process)
1208 continue;
1210 /* We need to output all local functions that are used and not
1211 always inlined, as well as those that are reachable from
1212 outside the current compilation unit. */
1213 if (node->analyzed
1214 && !node->thunk.thunk_p
1215 && !node->alias
1216 && !node->global.inlined_to
1217 && !TREE_ASM_WRITTEN (decl)
1218 && !DECL_EXTERNAL (decl))
1220 node->process = 1;
1221 if (node->same_comdat_group)
1223 cgraph_node *next;
1224 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
1225 next != node;
1226 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
1227 if (!next->thunk.thunk_p && !next->alias
1228 && !next->comdat_local_p ())
1229 next->process = 1;
1232 else if (node->same_comdat_group)
1234 #ifdef ENABLE_CHECKING
1235 check_same_comdat_groups = true;
1236 #endif
1238 else
1240 /* We should've reclaimed all functions that are not needed. */
1241 #ifdef ENABLE_CHECKING
1242 if (!node->global.inlined_to
1243 && gimple_has_body_p (decl)
1244 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1245 are inside partition, we can end up not removing the body since we no longer
1246 have analyzed node pointing to it. */
1247 && !node->in_other_partition
1248 && !node->alias
1249 && !node->clones
1250 && !DECL_EXTERNAL (decl))
1252 node->debug ();
1253 internal_error ("failed to reclaim unneeded function");
1255 #endif
1256 gcc_assert (node->global.inlined_to
1257 || !gimple_has_body_p (decl)
1258 || node->in_other_partition
1259 || node->clones
1260 || DECL_ARTIFICIAL (decl)
1261 || DECL_EXTERNAL (decl));
1266 #ifdef ENABLE_CHECKING
1267 if (check_same_comdat_groups)
1268 FOR_EACH_FUNCTION (node)
1269 if (node->same_comdat_group && !node->process)
1271 tree decl = node->decl;
1272 if (!node->global.inlined_to
1273 && gimple_has_body_p (decl)
1274 /* FIXME: in an ltrans unit when the offline copy is outside a
1275 partition but inline copies are inside a partition, we can
1276 end up not removing the body since we no longer have an
1277 analyzed node pointing to it. */
1278 && !node->in_other_partition
1279 && !node->clones
1280 && !DECL_EXTERNAL (decl))
1282 node->debug ();
1283 internal_error ("failed to reclaim unneeded function in same "
1284 "comdat group");
1287 #endif
1290 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1291 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1293 Set current_function_decl and cfun to newly constructed empty function body.
1294 return basic block in the function body. */
1296 basic_block
1297 init_lowered_empty_function (tree decl, bool in_ssa)
1299 basic_block bb;
1301 current_function_decl = decl;
1302 allocate_struct_function (decl, false);
1303 gimple_register_cfg_hooks ();
1304 init_empty_tree_cfg ();
1306 if (in_ssa)
1308 init_tree_ssa (cfun);
1309 init_ssa_operands (cfun);
1310 cfun->gimple_df->in_ssa_p = true;
1311 cfun->curr_properties |= PROP_ssa;
1314 DECL_INITIAL (decl) = make_node (BLOCK);
1316 DECL_SAVED_TREE (decl) = error_mark_node;
1317 cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1318 | PROP_cfg | PROP_loops);
1320 set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ());
1321 init_loops_structure (cfun, loops_for_fn (cfun), 1);
1322 loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1324 /* Create BB for body of the function and connect it properly. */
1325 bb = create_basic_block (NULL, (void *) 0, ENTRY_BLOCK_PTR_FOR_FN (cfun));
1326 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU);
1327 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1328 add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
1330 return bb;
1333 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1334 offset indicated by VIRTUAL_OFFSET, if that is
1335 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1336 zero for a result adjusting thunk. */
1338 static tree
1339 thunk_adjust (gimple_stmt_iterator * bsi,
1340 tree ptr, bool this_adjusting,
1341 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1343 gimple stmt;
1344 tree ret;
1346 if (this_adjusting
1347 && fixed_offset != 0)
1349 stmt = gimple_build_assign
1350 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1351 ptr,
1352 fixed_offset));
1353 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1356 /* If there's a virtual offset, look up that value in the vtable and
1357 adjust the pointer again. */
1358 if (virtual_offset)
1360 tree vtabletmp;
1361 tree vtabletmp2;
1362 tree vtabletmp3;
1364 if (!vtable_entry_type)
1366 tree vfunc_type = make_node (FUNCTION_TYPE);
1367 TREE_TYPE (vfunc_type) = integer_type_node;
1368 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1369 layout_type (vfunc_type);
1371 vtable_entry_type = build_pointer_type (vfunc_type);
1374 vtabletmp =
1375 create_tmp_reg (build_pointer_type
1376 (build_pointer_type (vtable_entry_type)), "vptr");
1378 /* The vptr is always at offset zero in the object. */
1379 stmt = gimple_build_assign (vtabletmp,
1380 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1381 ptr));
1382 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1384 /* Form the vtable address. */
1385 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
1386 "vtableaddr");
1387 stmt = gimple_build_assign (vtabletmp2,
1388 build_simple_mem_ref (vtabletmp));
1389 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1391 /* Find the entry with the vcall offset. */
1392 stmt = gimple_build_assign (vtabletmp2,
1393 fold_build_pointer_plus_loc (input_location,
1394 vtabletmp2,
1395 virtual_offset));
1396 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1398 /* Get the offset itself. */
1399 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1400 "vcalloffset");
1401 stmt = gimple_build_assign (vtabletmp3,
1402 build_simple_mem_ref (vtabletmp2));
1403 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1405 /* Adjust the `this' pointer. */
1406 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1407 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1408 GSI_CONTINUE_LINKING);
1411 if (!this_adjusting
1412 && fixed_offset != 0)
1413 /* Adjust the pointer by the constant. */
1415 tree ptrtmp;
1417 if (TREE_CODE (ptr) == VAR_DECL)
1418 ptrtmp = ptr;
1419 else
1421 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
1422 stmt = gimple_build_assign (ptrtmp, ptr);
1423 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1425 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1426 ptrtmp, fixed_offset);
1429 /* Emit the statement and gimplify the adjustment expression. */
1430 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
1431 stmt = gimple_build_assign (ret, ptr);
1432 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1434 return ret;
1437 /* Expand thunk NODE to gimple if possible.
1438 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1439 no assembler is produced.
1440 When OUTPUT_ASM_THUNK is true, also produce assembler for
1441 thunks that are not lowered. */
1443 bool
1444 cgraph_node::expand_thunk (bool output_asm_thunks, bool force_gimple_thunk)
1446 bool this_adjusting = thunk.this_adjusting;
1447 HOST_WIDE_INT fixed_offset = thunk.fixed_offset;
1448 HOST_WIDE_INT virtual_value = thunk.virtual_value;
1449 tree virtual_offset = NULL;
1450 tree alias = callees->callee->decl;
1451 tree thunk_fndecl = decl;
1452 tree a;
1455 if (!force_gimple_thunk && this_adjusting
1456 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1457 virtual_value, alias))
1459 const char *fnname;
1460 tree fn_block;
1461 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1463 if (!output_asm_thunks)
1465 analyzed = true;
1466 return false;
1469 if (in_lto_p)
1470 get_body ();
1471 a = DECL_ARGUMENTS (thunk_fndecl);
1473 current_function_decl = thunk_fndecl;
1475 /* Ensure thunks are emitted in their correct sections. */
1476 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1478 DECL_RESULT (thunk_fndecl)
1479 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1480 RESULT_DECL, 0, restype);
1481 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1482 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
1484 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1485 create one. */
1486 fn_block = make_node (BLOCK);
1487 BLOCK_VARS (fn_block) = a;
1488 DECL_INITIAL (thunk_fndecl) = fn_block;
1489 init_function_start (thunk_fndecl);
1490 cfun->is_thunk = 1;
1491 insn_locations_init ();
1492 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1493 prologue_location = curr_insn_location ();
1494 assemble_start_function (thunk_fndecl, fnname);
1496 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1497 fixed_offset, virtual_value, alias);
1499 assemble_end_function (thunk_fndecl, fnname);
1500 insn_locations_finalize ();
1501 init_insn_lengths ();
1502 free_after_compilation (cfun);
1503 set_cfun (NULL);
1504 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1505 thunk.thunk_p = false;
1506 analyzed = false;
1508 else
1510 tree restype;
1511 basic_block bb, then_bb, else_bb, return_bb;
1512 gimple_stmt_iterator bsi;
1513 int nargs = 0;
1514 tree arg;
1515 int i;
1516 tree resdecl;
1517 tree restmp = NULL;
1519 gimple call;
1520 gimple ret;
1522 if (in_lto_p)
1523 get_body ();
1524 a = DECL_ARGUMENTS (thunk_fndecl);
1526 current_function_decl = thunk_fndecl;
1528 /* Ensure thunks are emitted in their correct sections. */
1529 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1531 DECL_IGNORED_P (thunk_fndecl) = 1;
1532 bitmap_obstack_initialize (NULL);
1534 if (thunk.virtual_offset_p)
1535 virtual_offset = size_int (virtual_value);
1537 /* Build the return declaration for the function. */
1538 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1539 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1541 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1542 DECL_ARTIFICIAL (resdecl) = 1;
1543 DECL_IGNORED_P (resdecl) = 1;
1544 DECL_RESULT (thunk_fndecl) = resdecl;
1545 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1547 else
1548 resdecl = DECL_RESULT (thunk_fndecl);
1550 bb = then_bb = else_bb = return_bb = init_lowered_empty_function (thunk_fndecl, true);
1552 bsi = gsi_start_bb (bb);
1554 /* Build call to the function being thunked. */
1555 if (!VOID_TYPE_P (restype))
1557 if (DECL_BY_REFERENCE (resdecl))
1559 restmp = gimple_fold_indirect_ref (resdecl);
1560 if (!restmp)
1561 restmp = build2 (MEM_REF,
1562 TREE_TYPE (TREE_TYPE (DECL_RESULT (alias))),
1563 resdecl,
1564 build_int_cst (TREE_TYPE
1565 (DECL_RESULT (alias)), 0));
1567 else if (!is_gimple_reg_type (restype))
1569 restmp = resdecl;
1571 if (TREE_CODE (restmp) == VAR_DECL)
1572 add_local_decl (cfun, restmp);
1573 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1575 else
1576 restmp = create_tmp_reg (restype, "retval");
1579 for (arg = a; arg; arg = DECL_CHAIN (arg))
1580 nargs++;
1581 auto_vec<tree> vargs (nargs);
1582 if (this_adjusting)
1583 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
1584 virtual_offset));
1585 else if (nargs)
1586 vargs.quick_push (a);
1588 if (nargs)
1589 for (i = 1, arg = DECL_CHAIN (a); i < nargs; i++, arg = DECL_CHAIN (arg))
1591 tree tmp = arg;
1592 if (!is_gimple_val (arg))
1594 tmp = create_tmp_reg (TYPE_MAIN_VARIANT
1595 (TREE_TYPE (arg)), "arg");
1596 gimple stmt = gimple_build_assign (tmp, arg);
1597 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1599 vargs.quick_push (tmp);
1601 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1602 callees->call_stmt = call;
1603 gimple_call_set_from_thunk (call, true);
1604 gimple_call_set_with_bounds (call, instrumentation_clone);
1605 if (restmp)
1607 gimple_call_set_lhs (call, restmp);
1608 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp),
1609 TREE_TYPE (TREE_TYPE (alias))));
1611 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
1612 if (!(gimple_call_flags (call) & ECF_NORETURN))
1614 if (restmp && !this_adjusting
1615 && (fixed_offset || virtual_offset))
1617 tree true_label = NULL_TREE;
1619 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1621 gimple stmt;
1622 /* If the return type is a pointer, we need to
1623 protect against NULL. We know there will be an
1624 adjustment, because that's why we're emitting a
1625 thunk. */
1626 then_bb = create_basic_block (NULL, (void *) 0, bb);
1627 return_bb = create_basic_block (NULL, (void *) 0, then_bb);
1628 else_bb = create_basic_block (NULL, (void *) 0, else_bb);
1629 add_bb_to_loop (then_bb, bb->loop_father);
1630 add_bb_to_loop (return_bb, bb->loop_father);
1631 add_bb_to_loop (else_bb, bb->loop_father);
1632 remove_edge (single_succ_edge (bb));
1633 true_label = gimple_block_label (then_bb);
1634 stmt = gimple_build_cond (NE_EXPR, restmp,
1635 build_zero_cst (TREE_TYPE (restmp)),
1636 NULL_TREE, NULL_TREE);
1637 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1638 make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1639 make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1640 make_edge (return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1641 make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1642 make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1643 bsi = gsi_last_bb (then_bb);
1646 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1647 fixed_offset, virtual_offset);
1648 if (true_label)
1650 gimple stmt;
1651 bsi = gsi_last_bb (else_bb);
1652 stmt = gimple_build_assign (restmp,
1653 build_zero_cst (TREE_TYPE (restmp)));
1654 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1655 bsi = gsi_last_bb (return_bb);
1658 else
1659 gimple_call_set_tail (call, true);
1661 /* Build return value. */
1662 if (!DECL_BY_REFERENCE (resdecl))
1663 ret = gimple_build_return (restmp);
1664 else
1665 ret = gimple_build_return (resdecl);
1667 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1669 else
1671 gimple_call_set_tail (call, true);
1672 remove_edge (single_succ_edge (bb));
1675 cfun->gimple_df->in_ssa_p = true;
1676 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
1677 TREE_ASM_WRITTEN (thunk_fndecl) = false;
1678 delete_unreachable_blocks ();
1679 update_ssa (TODO_update_ssa);
1680 #ifdef ENABLE_CHECKING
1681 verify_flow_info ();
1682 #endif
1683 free_dominance_info (CDI_DOMINATORS);
1685 /* Since we want to emit the thunk, we explicitly mark its name as
1686 referenced. */
1687 thunk.thunk_p = false;
1688 lowered = true;
1689 bitmap_obstack_release (NULL);
1691 current_function_decl = NULL;
1692 set_cfun (NULL);
1693 return true;
1696 /* Assemble thunks and aliases associated to node. */
1698 void
1699 cgraph_node::assemble_thunks_and_aliases (void)
1701 cgraph_edge *e;
1702 ipa_ref *ref;
1704 for (e = callers; e;)
1705 if (e->caller->thunk.thunk_p
1706 && !e->caller->thunk.add_pointer_bounds_args)
1708 cgraph_node *thunk = e->caller;
1710 e = e->next_caller;
1711 thunk->expand_thunk (true, false);
1712 thunk->assemble_thunks_and_aliases ();
1714 else
1715 e = e->next_caller;
1717 FOR_EACH_ALIAS (this, ref)
1719 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1720 bool saved_written = TREE_ASM_WRITTEN (decl);
1722 /* Force assemble_alias to really output the alias this time instead
1723 of buffering it in same alias pairs. */
1724 TREE_ASM_WRITTEN (decl) = 1;
1725 do_assemble_alias (alias->decl,
1726 DECL_ASSEMBLER_NAME (decl));
1727 alias->assemble_thunks_and_aliases ();
1728 TREE_ASM_WRITTEN (decl) = saved_written;
1732 /* Expand function specified by node. */
1734 void
1735 cgraph_node::expand (void)
1737 location_t saved_loc;
1739 /* We ought to not compile any inline clones. */
1740 gcc_assert (!global.inlined_to);
1742 announce_function (decl);
1743 process = 0;
1744 gcc_assert (lowered);
1745 get_body ();
1747 /* Generate RTL for the body of DECL. */
1749 timevar_push (TV_REST_OF_COMPILATION);
1751 gcc_assert (symtab->global_info_ready);
1753 /* Initialize the default bitmap obstack. */
1754 bitmap_obstack_initialize (NULL);
1756 /* Initialize the RTL code for the function. */
1757 current_function_decl = decl;
1758 saved_loc = input_location;
1759 input_location = DECL_SOURCE_LOCATION (decl);
1760 init_function_start (decl);
1762 gimple_register_cfg_hooks ();
1764 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1766 execute_all_ipa_transforms ();
1768 /* Perform all tree transforms and optimizations. */
1770 /* Signal the start of passes. */
1771 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1773 execute_pass_list (cfun, g->get_passes ()->all_passes);
1775 /* Signal the end of passes. */
1776 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1778 bitmap_obstack_release (&reg_obstack);
1780 /* Release the default bitmap obstack. */
1781 bitmap_obstack_release (NULL);
1783 /* If requested, warn about function definitions where the function will
1784 return a value (usually of some struct or union type) which itself will
1785 take up a lot of stack space. */
1786 if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl))
1788 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
1790 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1791 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1792 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1793 larger_than_size))
1795 unsigned int size_as_int
1796 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1798 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1799 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
1800 decl, size_as_int);
1801 else
1802 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
1803 decl, larger_than_size);
1807 gimple_set_body (decl, NULL);
1808 if (DECL_STRUCT_FUNCTION (decl) == 0
1809 && !cgraph_node::get (decl)->origin)
1811 /* Stop pointing to the local nodes about to be freed.
1812 But DECL_INITIAL must remain nonzero so we know this
1813 was an actual function definition.
1814 For a nested function, this is done in c_pop_function_context.
1815 If rest_of_compilation set this to 0, leave it 0. */
1816 if (DECL_INITIAL (decl) != 0)
1817 DECL_INITIAL (decl) = error_mark_node;
1820 input_location = saved_loc;
1822 ggc_collect ();
1823 timevar_pop (TV_REST_OF_COMPILATION);
1825 /* Make sure that BE didn't give up on compiling. */
1826 gcc_assert (TREE_ASM_WRITTEN (decl));
1827 set_cfun (NULL);
1828 current_function_decl = NULL;
1830 /* It would make a lot more sense to output thunks before function body to get more
1831 forward and lest backwarding jumps. This however would need solving problem
1832 with comdats. See PR48668. Also aliases must come after function itself to
1833 make one pass assemblers, like one on AIX, happy. See PR 50689.
1834 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1835 groups. */
1836 assemble_thunks_and_aliases ();
1837 release_body ();
1838 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1839 points to the dead function body. */
1840 remove_callees ();
1841 remove_all_references ();
1844 /* Node comparer that is responsible for the order that corresponds
1845 to time when a function was launched for the first time. */
1847 static int
1848 node_cmp (const void *pa, const void *pb)
1850 const cgraph_node *a = *(const cgraph_node * const *) pa;
1851 const cgraph_node *b = *(const cgraph_node * const *) pb;
1853 /* Functions with time profile must be before these without profile. */
1854 if (!a->tp_first_run || !b->tp_first_run)
1855 return a->tp_first_run - b->tp_first_run;
1857 return a->tp_first_run != b->tp_first_run
1858 ? b->tp_first_run - a->tp_first_run
1859 : b->order - a->order;
1862 /* Expand all functions that must be output.
1864 Attempt to topologically sort the nodes so function is output when
1865 all called functions are already assembled to allow data to be
1866 propagated across the callgraph. Use a stack to get smaller distance
1867 between a function and its callees (later we may choose to use a more
1868 sophisticated algorithm for function reordering; we will likely want
1869 to use subsections to make the output functions appear in top-down
1870 order). */
1872 static void
1873 expand_all_functions (void)
1875 cgraph_node *node;
1876 cgraph_node **order = XCNEWVEC (cgraph_node *,
1877 symtab->cgraph_count);
1878 unsigned int expanded_func_count = 0, profiled_func_count = 0;
1879 int order_pos, new_order_pos = 0;
1880 int i;
1882 order_pos = ipa_reverse_postorder (order);
1883 gcc_assert (order_pos == symtab->cgraph_count);
1885 /* Garbage collector may remove inline clones we eliminate during
1886 optimization. So we must be sure to not reference them. */
1887 for (i = 0; i < order_pos; i++)
1888 if (order[i]->process)
1889 order[new_order_pos++] = order[i];
1891 if (flag_profile_reorder_functions)
1892 qsort (order, new_order_pos, sizeof (cgraph_node *), node_cmp);
1894 for (i = new_order_pos - 1; i >= 0; i--)
1896 node = order[i];
1898 if (node->process)
1900 expanded_func_count++;
1901 if(node->tp_first_run)
1902 profiled_func_count++;
1904 if (symtab->dump_file)
1905 fprintf (symtab->dump_file,
1906 "Time profile order in expand_all_functions:%s:%d\n",
1907 node->asm_name (), node->tp_first_run);
1908 node->process = 0;
1909 node->expand ();
1913 if (dump_file)
1914 fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
1915 main_input_filename, profiled_func_count, expanded_func_count);
1917 if (symtab->dump_file && flag_profile_reorder_functions)
1918 fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n",
1919 profiled_func_count, expanded_func_count);
1921 symtab->process_new_functions ();
1922 free_gimplify_stack ();
1924 free (order);
1927 /* This is used to sort the node types by the cgraph order number. */
1929 enum cgraph_order_sort_kind
1931 ORDER_UNDEFINED = 0,
1932 ORDER_FUNCTION,
1933 ORDER_VAR,
1934 ORDER_ASM
1937 struct cgraph_order_sort
1939 enum cgraph_order_sort_kind kind;
1940 union
1942 cgraph_node *f;
1943 varpool_node *v;
1944 asm_node *a;
1945 } u;
1948 /* Output all functions, variables, and asm statements in the order
1949 according to their order fields, which is the order in which they
1950 appeared in the file. This implements -fno-toplevel-reorder. In
1951 this mode we may output functions and variables which don't really
1952 need to be output.
1953 When NO_REORDER is true only do this for symbols marked no reorder. */
1955 static void
1956 output_in_order (bool no_reorder)
1958 int max;
1959 cgraph_order_sort *nodes;
1960 int i;
1961 cgraph_node *pf;
1962 varpool_node *pv;
1963 asm_node *pa;
1964 max = symtab->order;
1965 nodes = XCNEWVEC (cgraph_order_sort, max);
1967 FOR_EACH_DEFINED_FUNCTION (pf)
1969 if (pf->process && !pf->thunk.thunk_p && !pf->alias)
1971 if (no_reorder && !pf->no_reorder)
1972 continue;
1973 i = pf->order;
1974 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1975 nodes[i].kind = ORDER_FUNCTION;
1976 nodes[i].u.f = pf;
1980 FOR_EACH_DEFINED_VARIABLE (pv)
1981 if (!DECL_EXTERNAL (pv->decl))
1983 if (no_reorder && !pv->no_reorder)
1984 continue;
1985 i = pv->order;
1986 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1987 nodes[i].kind = ORDER_VAR;
1988 nodes[i].u.v = pv;
1991 for (pa = symtab->first_asm_symbol (); pa; pa = pa->next)
1993 i = pa->order;
1994 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1995 nodes[i].kind = ORDER_ASM;
1996 nodes[i].u.a = pa;
1999 /* In toplevel reorder mode we output all statics; mark them as needed. */
2001 for (i = 0; i < max; ++i)
2002 if (nodes[i].kind == ORDER_VAR)
2003 nodes[i].u.v->finalize_named_section_flags ();
2005 for (i = 0; i < max; ++i)
2007 switch (nodes[i].kind)
2009 case ORDER_FUNCTION:
2010 nodes[i].u.f->process = 0;
2011 nodes[i].u.f->expand ();
2012 break;
2014 case ORDER_VAR:
2015 nodes[i].u.v->assemble_decl ();
2016 break;
2018 case ORDER_ASM:
2019 assemble_asm (nodes[i].u.a->asm_str);
2020 break;
2022 case ORDER_UNDEFINED:
2023 break;
2025 default:
2026 gcc_unreachable ();
2030 symtab->clear_asm_symbols ();
2032 free (nodes);
2035 static void
2036 ipa_passes (void)
2038 gcc::pass_manager *passes = g->get_passes ();
2040 set_cfun (NULL);
2041 current_function_decl = NULL;
2042 gimple_register_cfg_hooks ();
2043 bitmap_obstack_initialize (NULL);
2045 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2047 if (!in_lto_p)
2049 execute_ipa_pass_list (passes->all_small_ipa_passes);
2050 if (seen_error ())
2051 return;
2054 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2055 devirtualization and other changes where removal iterate. */
2056 symtab->remove_unreachable_nodes (true, symtab->dump_file);
2058 /* If pass_all_early_optimizations was not scheduled, the state of
2059 the cgraph will not be properly updated. Update it now. */
2060 if (symtab->state < IPA_SSA)
2061 symtab->state = IPA_SSA;
2063 if (!in_lto_p)
2065 /* Generate coverage variables and constructors. */
2066 coverage_finish ();
2068 /* Process new functions added. */
2069 set_cfun (NULL);
2070 current_function_decl = NULL;
2071 symtab->process_new_functions ();
2073 execute_ipa_summary_passes
2074 ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2077 /* Some targets need to handle LTO assembler output specially. */
2078 if (flag_generate_lto)
2079 targetm.asm_out.lto_start ();
2081 if (!in_lto_p)
2082 ipa_write_summaries ();
2084 if (flag_generate_lto)
2085 targetm.asm_out.lto_end ();
2087 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
2088 execute_ipa_pass_list (passes->all_regular_ipa_passes);
2089 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2091 bitmap_obstack_release (NULL);
2095 /* Return string alias is alias of. */
2097 static tree
2098 get_alias_symbol (tree decl)
2100 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
2101 return get_identifier (TREE_STRING_POINTER
2102 (TREE_VALUE (TREE_VALUE (alias))));
2106 /* Weakrefs may be associated to external decls and thus not output
2107 at expansion time. Emit all necessary aliases. */
2109 void
2110 symbol_table::output_weakrefs (void)
2112 symtab_node *node;
2113 cgraph_node *cnode;
2114 FOR_EACH_SYMBOL (node)
2115 if (node->alias
2116 && !TREE_ASM_WRITTEN (node->decl)
2117 && (!(cnode = dyn_cast <cgraph_node *> (node))
2118 || !cnode->instrumented_version
2119 || !TREE_ASM_WRITTEN (cnode->instrumented_version->decl))
2120 && node->weakref)
2122 tree target;
2124 /* Weakrefs are special by not requiring target definition in current
2125 compilation unit. It is thus bit hard to work out what we want to
2126 alias.
2127 When alias target is defined, we need to fetch it from symtab reference,
2128 otherwise it is pointed to by alias_target. */
2129 if (node->alias_target)
2130 target = (DECL_P (node->alias_target)
2131 ? DECL_ASSEMBLER_NAME (node->alias_target)
2132 : node->alias_target);
2133 else if (node->analyzed)
2134 target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
2135 else
2137 gcc_unreachable ();
2138 target = get_alias_symbol (node->decl);
2140 do_assemble_alias (node->decl, target);
2144 /* Perform simple optimizations based on callgraph. */
2146 void
2147 symbol_table::compile (void)
2149 if (seen_error ())
2150 return;
2152 #ifdef ENABLE_CHECKING
2153 symtab_node::verify_symtab_nodes ();
2154 #endif
2156 timevar_push (TV_CGRAPHOPT);
2157 if (pre_ipa_mem_report)
2159 fprintf (stderr, "Memory consumption before IPA\n");
2160 dump_memory_report (false);
2162 if (!quiet_flag)
2163 fprintf (stderr, "Performing interprocedural optimizations\n");
2164 state = IPA;
2166 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2167 if (flag_lto)
2168 lto_streamer_hooks_init ();
2170 /* Don't run the IPA passes if there was any error or sorry messages. */
2171 if (!seen_error ())
2172 ipa_passes ();
2174 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2175 if (seen_error ()
2176 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
2178 timevar_pop (TV_CGRAPHOPT);
2179 return;
2182 /* This pass remove bodies of extern inline functions we never inlined.
2183 Do this later so other IPA passes see what is really going on.
2184 FIXME: This should be run just after inlining by pasmanager. */
2185 remove_unreachable_nodes (false, dump_file);
2186 global_info_ready = true;
2187 if (dump_file)
2189 fprintf (dump_file, "Optimized ");
2190 symtab_node:: dump_table (dump_file);
2192 if (post_ipa_mem_report)
2194 fprintf (stderr, "Memory consumption after IPA\n");
2195 dump_memory_report (false);
2197 timevar_pop (TV_CGRAPHOPT);
2199 /* Output everything. */
2200 (*debug_hooks->assembly_start) ();
2201 if (!quiet_flag)
2202 fprintf (stderr, "Assembling functions:\n");
2203 #ifdef ENABLE_CHECKING
2204 symtab_node::verify_symtab_nodes ();
2205 #endif
2207 materialize_all_clones ();
2208 bitmap_obstack_initialize (NULL);
2209 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2210 bitmap_obstack_release (NULL);
2211 mark_functions_to_output ();
2213 /* When weakref support is missing, we autmatically translate all
2214 references to NODE to references to its ultimate alias target.
2215 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2216 TREE_CHAIN.
2218 Set up this mapping before we output any assembler but once we are sure
2219 that all symbol renaming is done.
2221 FIXME: All this uglyness can go away if we just do renaming at gimple
2222 level by physically rewritting the IL. At the moment we can only redirect
2223 calls, so we need infrastructure for renaming references as well. */
2224 #ifndef ASM_OUTPUT_WEAKREF
2225 symtab_node *node;
2227 FOR_EACH_SYMBOL (node)
2228 if (node->alias
2229 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2231 IDENTIFIER_TRANSPARENT_ALIAS
2232 (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2233 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2234 = (node->alias_target ? node->alias_target
2235 : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
2237 #endif
2239 state = EXPANSION;
2241 if (!flag_toplevel_reorder)
2242 output_in_order (false);
2243 else
2245 /* Output first asm statements and anything ordered. The process
2246 flag is cleared for these nodes, so we skip them later. */
2247 output_in_order (true);
2248 expand_all_functions ();
2249 output_variables ();
2252 process_new_functions ();
2253 state = FINISHED;
2254 output_weakrefs ();
2256 if (dump_file)
2258 fprintf (dump_file, "\nFinal ");
2259 symtab_node::dump_table (dump_file);
2261 #ifdef ENABLE_CHECKING
2262 symtab_node::verify_symtab_nodes ();
2263 /* Double check that all inline clones are gone and that all
2264 function bodies have been released from memory. */
2265 if (!seen_error ())
2267 cgraph_node *node;
2268 bool error_found = false;
2270 FOR_EACH_DEFINED_FUNCTION (node)
2271 if (node->global.inlined_to
2272 || gimple_has_body_p (node->decl))
2274 error_found = true;
2275 node->debug ();
2277 if (error_found)
2278 internal_error ("nodes with unreleased memory found");
2280 #endif
2284 /* Analyze the whole compilation unit once it is parsed completely. */
2286 void
2287 symbol_table::finalize_compilation_unit (void)
2289 timevar_push (TV_CGRAPH);
2291 /* If we're here there's no current function anymore. Some frontends
2292 are lazy in clearing these. */
2293 current_function_decl = NULL;
2294 set_cfun (NULL);
2296 /* Do not skip analyzing the functions if there were errors, we
2297 miss diagnostics for following functions otherwise. */
2299 /* Emit size functions we didn't inline. */
2300 finalize_size_functions ();
2302 /* Mark alias targets necessary and emit diagnostics. */
2303 handle_alias_pairs ();
2305 if (!quiet_flag)
2307 fprintf (stderr, "\nAnalyzing compilation unit\n");
2308 fflush (stderr);
2311 if (flag_dump_passes)
2312 dump_passes ();
2314 /* Gimplify and lower all functions, compute reachability and
2315 remove unreachable nodes. */
2316 analyze_functions ();
2318 /* Mark alias targets necessary and emit diagnostics. */
2319 handle_alias_pairs ();
2321 /* Gimplify and lower thunks. */
2322 analyze_functions ();
2324 /* Finally drive the pass manager. */
2325 compile ();
2327 timevar_pop (TV_CGRAPH);
2330 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2331 within the same process. For use by toplev::finalize. */
2333 void
2334 cgraphunit_c_finalize (void)
2336 gcc_assert (cgraph_new_nodes.length () == 0);
2337 cgraph_new_nodes.truncate (0);
2339 vtable_entry_type = NULL;
2340 queued_nodes = &symtab_terminator;
2342 first_analyzed = NULL;
2343 first_analyzed_var = NULL;
2346 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2347 kind of wrapper method. */
2349 void
2350 cgraph_node::create_wrapper (cgraph_node *target)
2352 /* Preserve DECL_RESULT so we get right by reference flag. */
2353 tree decl_result = DECL_RESULT (decl);
2355 /* Remove the function's body but keep arguments to be reused
2356 for thunk. */
2357 release_body (true);
2358 reset ();
2360 DECL_RESULT (decl) = decl_result;
2361 DECL_INITIAL (decl) = NULL;
2362 allocate_struct_function (decl, false);
2363 set_cfun (NULL);
2365 /* Turn alias into thunk and expand it into GIMPLE representation. */
2366 definition = true;
2367 thunk.thunk_p = true;
2368 thunk.this_adjusting = false;
2370 cgraph_edge *e = create_edge (target, NULL, 0, CGRAPH_FREQ_BASE);
2372 tree arguments = DECL_ARGUMENTS (decl);
2374 while (arguments)
2376 TREE_ADDRESSABLE (arguments) = false;
2377 arguments = TREE_CHAIN (arguments);
2380 expand_thunk (false, true);
2381 e->call_stmt_cannot_inline_p = true;
2383 /* Inline summary set-up. */
2384 analyze ();
2385 inline_analyze_function (this);
2388 #include "gt-cgraphunit.h"