Merge from trunk:
[official-gcc.git] / main / gcc / cgraphunit.c
bloba75078e1378d1ca5845384ee5532456902d17691
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 - cgraph_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 "basic-block.h"
171 #include "tree-ssa-alias.h"
172 #include "internal-fn.h"
173 #include "gimple-fold.h"
174 #include "gimple-expr.h"
175 #include "is-a.h"
176 #include "gimple.h"
177 #include "gimplify.h"
178 #include "gimple-iterator.h"
179 #include "gimplify-me.h"
180 #include "gimple-ssa.h"
181 #include "tree-cfg.h"
182 #include "tree-into-ssa.h"
183 #include "tree-ssa.h"
184 #include "tree-inline.h"
185 #include "langhooks.h"
186 #include "toplev.h"
187 #include "flags.h"
188 #include "debug.h"
189 #include "target.h"
190 #include "diagnostic.h"
191 #include "params.h"
192 #include "fibheap.h"
193 #include "intl.h"
194 #include "function.h"
195 #include "ipa-prop.h"
196 #include "gcov-io.h"
197 #include "tree-iterator.h"
198 #include "tree-pass.h"
199 #include "tree-dump.h"
200 #include "gimple-pretty-print.h"
201 #include "output.h"
202 #include "coverage.h"
203 #include "plugin.h"
204 #include "ipa-inline.h"
205 #include "ipa-utils.h"
206 #include "lto-streamer.h"
207 #include "l-ipo.h"
208 #include "except.h"
209 #include "cfgloop.h"
210 #include "regset.h" /* FIXME: For reg_obstack. */
211 #include "context.h"
212 #include "pass_manager.h"
213 #include "tree-nested.h"
214 #include "gimplify.h"
215 #include "dbgcnt.h"
217 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
218 secondary queue used during optimization to accommodate passes that
219 may generate new functions that need to be optimized and expanded. */
220 vec<cgraph_node *> cgraph_new_nodes;
222 static void expand_all_functions (void);
223 static void mark_functions_to_output (void);
224 static void expand_function (struct cgraph_node *);
225 static void handle_alias_pairs (void);
227 FILE *cgraph_dump_file;
229 /* Linked list of cgraph asm nodes. */
230 struct asm_node *asm_nodes;
232 /* Last node in cgraph_asm_nodes. */
233 static GTY(()) struct asm_node *asm_last_node;
235 /* Used for vtable lookup in thunk adjusting. */
236 static GTY (()) tree vtable_entry_type;
238 /* Determine if symbol DECL is needed. That is, visible to something
239 either outside this translation unit, something magic in the system
240 configury */
241 bool
242 decide_is_symbol_needed (symtab_node *node)
244 tree decl = node->decl;
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 || (L_IPO_COMP_MODE
250 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))));
252 if (!node->definition)
253 return false;
255 if (DECL_EXTERNAL (decl))
256 return false;
258 /* If the user told us it is used, then it must be so. */
259 if (node->force_output)
260 return true;
262 /* ABI forced symbols are needed when they are external. */
263 if (node->forced_by_abi && TREE_PUBLIC (decl))
264 return true;
266 /* Keep constructors, destructors and virtual functions. */
267 if (TREE_CODE (decl) == FUNCTION_DECL
268 && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)))
269 return true;
271 /* Externally visible variables must be output. The exception is
272 COMDAT variables that must be output only when they are needed. */
273 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
274 return true;
276 return false;
279 /* Head and terminator of the queue of nodes to be processed while building
280 callgraph. */
282 static symtab_node symtab_terminator;
283 static symtab_node *queued_nodes = &symtab_terminator;
285 /* Add NODE to queue starting at QUEUED_NODES.
286 The queue is linked via AUX pointers and terminated by pointer to 1. */
288 static void
289 enqueue_node (symtab_node *node)
291 if (node->aux)
292 return;
293 gcc_checking_assert (queued_nodes);
294 node->aux = queued_nodes;
295 queued_nodes = node;
298 void
299 cgraph_enqueue_node (struct cgraph_node *node)
301 enqueue_node ((symtab_node *) node);
304 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
305 functions into callgraph in a way so they look like ordinary reachable
306 functions inserted into callgraph already at construction time. */
308 void
309 cgraph_process_new_functions (void)
311 tree fndecl;
313 if (!cgraph_new_nodes.exists ())
314 return;
316 handle_alias_pairs ();
317 /* Note that this queue may grow as its being processed, as the new
318 functions may generate new ones. */
319 for (unsigned i = 0; i < cgraph_new_nodes.length (); i++)
321 cgraph_node *node = cgraph_new_nodes[i];
322 fndecl = node->decl;
323 switch (cgraph_state)
325 case CGRAPH_STATE_CONSTRUCTION:
326 /* At construction time we just need to finalize function and move
327 it into reachable functions list. */
329 cgraph_finalize_function (fndecl, false);
330 node->call_function_insertion_hooks ();
331 enqueue_node (node);
332 break;
334 case CGRAPH_STATE_IPA:
335 case CGRAPH_STATE_IPA_SSA:
336 /* When IPA optimization already started, do all essential
337 transformations that has been already performed on the whole
338 cgraph but not on this function. */
340 gimple_register_cfg_hooks ();
341 if (!node->analyzed)
342 node->analyze ();
343 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
344 if (cgraph_state == CGRAPH_STATE_IPA_SSA
345 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
346 g->get_passes ()->execute_early_local_passes ();
347 else if (inline_summary_vec != NULL)
348 compute_inline_parameters (node, true);
349 free_dominance_info (CDI_POST_DOMINATORS);
350 free_dominance_info (CDI_DOMINATORS);
351 pop_cfun ();
352 node->call_function_insertion_hooks ();
353 break;
355 case CGRAPH_STATE_EXPANSION:
356 /* Functions created during expansion shall be compiled
357 directly. */
358 node->process = 0;
359 node->call_function_insertion_hooks ();
360 expand_function (node);
361 break;
363 default:
364 gcc_unreachable ();
365 break;
369 cgraph_new_nodes.release ();
372 /* As an GCC extension we allow redefinition of the function. The
373 semantics when both copies of bodies differ is not well defined.
374 We replace the old body with new body so in unit at a time mode
375 we always use new body, while in normal mode we may end up with
376 old body inlined into some functions and new body expanded and
377 inlined in others.
379 ??? It may make more sense to use one body for inlining and other
380 body for expanding the function but this is difficult to do. */
382 void
383 cgraph_node::reset (void)
385 /* If process is set, then we have already begun whole-unit analysis.
386 This is *not* testing for whether we've already emitted the function.
387 That case can be sort-of legitimately seen with real function redefinition
388 errors. I would argue that the front end should never present us with
389 such a case, but don't enforce that for now. */
390 gcc_assert (!process);
392 /* Reset our data structures so we can analyze the function again. */
393 memset (&local, 0, sizeof (local));
394 memset (&global, 0, sizeof (global));
395 memset (&rtl, 0, sizeof (rtl));
396 analyzed = false;
397 definition = false;
398 alias = false;
399 weakref = false;
400 cpp_implicit_alias = false;
402 remove_callees ();
403 remove_all_references ();
406 /* Return true when there are references to NODE. */
408 static bool
409 referred_to_p (symtab_node *node)
411 struct ipa_ref *ref = NULL;
413 /* See if there are any references at all. */
414 if (node->iterate_referring (0, ref))
415 return true;
416 /* For functions check also calls. */
417 cgraph_node *cn = dyn_cast <cgraph_node *> (node);
418 if (cn && cn->callers)
419 return true;
420 return false;
423 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
424 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
425 the garbage collector run at the moment. We would need to either create
426 a new GC context, or just not compile right now. */
428 void
429 cgraph_finalize_function (tree decl, bool no_collect)
431 struct cgraph_node *node = cgraph_node::get_create (decl);
433 if (node->definition)
435 /* Nested functions should only be defined once. */
436 gcc_assert (!DECL_CONTEXT (decl)
437 || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL);
438 node->reset ();
439 node->local.redefined_extern_inline = true;
442 notice_global_symbol (decl);
443 node->definition = true;
444 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
446 /* With -fkeep-inline-functions we are keeping all inline functions except
447 for extern inline ones. */
448 if (flag_keep_inline_functions
449 && DECL_DECLARED_INLINE_P (decl)
450 && !DECL_EXTERNAL (decl)
451 && !DECL_DISREGARD_INLINE_LIMITS (decl))
452 node->force_output = 1;
454 /* When not optimizing, also output the static functions. (see
455 PR24561), but don't do so for always_inline functions, functions
456 declared inline and nested functions. These were optimized out
457 in the original implementation and it is unclear whether we want
458 to change the behavior here. */
459 if ((!optimize
460 && !node->cpp_implicit_alias
461 && !DECL_DISREGARD_INLINE_LIMITS (decl)
462 && !DECL_DECLARED_INLINE_P (decl)
463 && !(DECL_CONTEXT (decl)
464 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
465 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
466 node->force_output = 1;
468 /* If we've not yet emitted decl, tell the debug info about it. */
469 if (!TREE_ASM_WRITTEN (decl))
470 (*debug_hooks->deferred_inline_function) (decl);
472 /* Possibly warn about unused parameters. */
473 if (warn_unused_parameter)
474 do_warn_unused_parameter (decl);
476 if (!no_collect)
477 ggc_collect ();
479 if (cgraph_state == CGRAPH_STATE_CONSTRUCTION
480 && (decide_is_symbol_needed (node)
481 || referred_to_p (node)))
482 enqueue_node (node);
485 /* Add the function FNDECL to the call graph.
486 Unlike cgraph_finalize_function, this function is intended to be used
487 by middle end and allows insertion of new function at arbitrary point
488 of compilation. The function can be either in high, low or SSA form
489 GIMPLE.
491 The function is assumed to be reachable and have address taken (so no
492 API breaking optimizations are performed on it).
494 Main work done by this function is to enqueue the function for later
495 processing to avoid need the passes to be re-entrant. */
497 void
498 cgraph_node::add_new_function (tree fndecl, bool lowered)
500 gcc::pass_manager *passes = g->get_passes ();
501 struct cgraph_node *node;
503 switch (cgraph_state)
505 case CGRAPH_STATE_PARSING:
506 cgraph_finalize_function (fndecl, false);
507 break;
508 case CGRAPH_STATE_CONSTRUCTION:
509 /* Just enqueue function to be processed at nearest occurrence. */
510 node = cgraph_node::get_create (fndecl);
511 if (lowered)
512 node->lowered = true;
513 cgraph_new_nodes.safe_push (node);
514 break;
516 case CGRAPH_STATE_IPA:
517 case CGRAPH_STATE_IPA_SSA:
518 case CGRAPH_STATE_EXPANSION:
519 /* Bring the function into finalized state and enqueue for later
520 analyzing and compilation. */
521 node = cgraph_node::get_create (fndecl);
522 node->local.local = false;
523 node->definition = true;
524 node->force_output = true;
525 if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
527 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
528 gimple_register_cfg_hooks ();
529 bitmap_obstack_initialize (NULL);
530 execute_pass_list (cfun, passes->all_lowering_passes);
531 passes->execute_early_local_passes ();
532 bitmap_obstack_release (NULL);
533 pop_cfun ();
535 lowered = true;
537 if (lowered)
538 node->lowered = true;
539 cgraph_new_nodes.safe_push (node);
540 break;
542 case CGRAPH_STATE_FINISHED:
543 /* At the very end of compilation we have to do all the work up
544 to expansion. */
545 node = cgraph_node::create (fndecl);
546 if (lowered)
547 node->lowered = true;
548 node->definition = true;
549 node->analyze ();
550 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
551 gimple_register_cfg_hooks ();
552 bitmap_obstack_initialize (NULL);
553 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
554 g->get_passes ()->execute_early_local_passes ();
555 bitmap_obstack_release (NULL);
556 pop_cfun ();
557 expand_function (node);
558 break;
559 default:
560 gcc_unreachable ();
563 /* Set a personality if required and we already passed EH lowering. */
564 if (lowered
565 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
566 == eh_personality_lang))
567 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
570 /* Add a top-level asm statement to the list. */
572 struct asm_node *
573 add_asm_node (tree asm_str)
575 struct asm_node *node;
577 node = ggc_cleared_alloc<asm_node> ();
578 node->asm_str = asm_str;
579 node->order = symtab_order++;
580 node->next = NULL;
581 if (asm_nodes == NULL)
582 asm_nodes = node;
583 else
584 asm_last_node->next = node;
585 asm_last_node = node;
586 return node;
589 /* Output all asm statements we have stored up to be output. */
591 static void
592 output_asm_statements (void)
594 struct asm_node *can;
596 if (seen_error ())
597 return;
599 for (can = asm_nodes; can; can = can->next)
600 assemble_asm (can->asm_str);
601 asm_nodes = NULL;
604 /* Analyze the function scheduled to be output. */
605 void
606 cgraph_node::analyze (void)
608 tree decl = this->decl;
609 location_t saved_loc = input_location;
610 input_location = DECL_SOURCE_LOCATION (decl);
612 if (thunk.thunk_p)
614 create_edge (cgraph_node::get (thunk.alias),
615 NULL, 0, CGRAPH_FREQ_BASE);
616 if (!expand_thunk (false, false))
618 thunk.alias = NULL;
619 analyzed = true;
620 return;
622 thunk.alias = NULL;
624 if (alias)
625 resolve_alias (cgraph_node::get (alias_target));
626 else if (dispatcher_function)
628 /* Generate the dispatcher body of multi-versioned functions. */
629 struct cgraph_function_version_info *dispatcher_version_info
630 = function_version ();
631 if (dispatcher_version_info != NULL
632 && (dispatcher_version_info->dispatcher_resolver
633 == NULL_TREE))
635 tree resolver = NULL_TREE;
636 gcc_assert (targetm.generate_version_dispatcher_body);
637 resolver = targetm.generate_version_dispatcher_body (this);
638 gcc_assert (resolver != NULL_TREE);
641 else
643 push_cfun (DECL_STRUCT_FUNCTION (decl));
645 assign_assembler_name_if_neeeded (decl);
647 /* Make sure to gimplify bodies only once. During analyzing a
648 function we lower it, which will require gimplified nested
649 functions, so we can end up here with an already gimplified
650 body. */
651 if (!gimple_has_body_p (decl))
652 gimplify_function_tree (decl);
653 dump_function (TDI_generic, decl);
655 /* Lower the function. */
656 if (!lowered)
658 if (nested)
659 lower_nested_functions (decl);
660 gcc_assert (!nested);
662 gimple_register_cfg_hooks ();
663 bitmap_obstack_initialize (NULL);
664 execute_pass_list (cfun, g->get_passes ()->all_lowering_passes);
665 free_dominance_info (CDI_POST_DOMINATORS);
666 free_dominance_info (CDI_DOMINATORS);
667 compact_blocks ();
668 bitmap_obstack_release (NULL);
669 lowered = true;
672 pop_cfun ();
674 analyzed = true;
676 input_location = saved_loc;
679 /* C++ frontend produce same body aliases all over the place, even before PCH
680 gets streamed out. It relies on us linking the aliases with their function
681 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
682 first produce aliases without links, but once C++ FE is sure he won't sream
683 PCH we build the links via this function. */
685 void
686 cgraph_process_same_body_aliases (void)
688 symtab_node *node;
689 FOR_EACH_SYMBOL (node)
690 if (node->cpp_implicit_alias && !node->analyzed)
691 node->resolve_alias
692 (TREE_CODE (node->alias_target) == VAR_DECL
693 ? (symtab_node *)varpool_node::get_create (node->alias_target)
694 : (symtab_node *)cgraph_node::get_create (node->alias_target));
695 cpp_implicit_aliases_done = true;
698 /* Process attributes common for vars and functions. */
700 static void
701 process_common_attributes (tree decl)
703 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
705 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
707 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
708 "%<weakref%> attribute should be accompanied with"
709 " an %<alias%> attribute");
710 DECL_WEAK (decl) = 0;
711 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
712 DECL_ATTRIBUTES (decl));
716 /* Look for externally_visible and used attributes and mark cgraph nodes
717 accordingly.
719 We cannot mark the nodes at the point the attributes are processed (in
720 handle_*_attribute) because the copy of the declarations available at that
721 point may not be canonical. For example, in:
723 void f();
724 void f() __attribute__((used));
726 the declaration we see in handle_used_attribute will be the second
727 declaration -- but the front end will subsequently merge that declaration
728 with the original declaration and discard the second declaration.
730 Furthermore, we can't mark these nodes in cgraph_finalize_function because:
732 void f() {}
733 void f() __attribute__((externally_visible));
735 is valid.
737 So, we walk the nodes at the end of the translation unit, applying the
738 attributes at that point. */
740 static void
741 process_function_and_variable_attributes (struct cgraph_node *first,
742 varpool_node *first_var)
744 struct cgraph_node *node;
745 varpool_node *vnode;
747 for (node = cgraph_first_function (); node != first;
748 node = cgraph_next_function (node))
750 tree decl = node->decl;
751 if (DECL_PRESERVE_P (decl))
752 node->mark_force_output ();
753 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
755 if (! TREE_PUBLIC (node->decl))
756 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
757 "%<externally_visible%>"
758 " attribute have effect only on public objects");
760 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
761 && (node->definition && !node->alias))
763 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
764 "%<weakref%> attribute ignored"
765 " because function is defined");
766 DECL_WEAK (decl) = 0;
767 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
768 DECL_ATTRIBUTES (decl));
771 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
772 && !DECL_DECLARED_INLINE_P (decl)
773 /* redefining extern inline function makes it DECL_UNINLINABLE. */
774 && !DECL_UNINLINABLE (decl))
775 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
776 "always_inline function might not be inlinable");
778 process_common_attributes (decl);
780 for (vnode = varpool_first_variable (); vnode != first_var;
781 vnode = varpool_next_variable (vnode))
783 tree decl = vnode->decl;
784 if (DECL_EXTERNAL (decl)
785 && DECL_INITIAL (decl))
786 varpool_node::finalize_decl (decl);
787 if (DECL_PRESERVE_P (decl))
788 vnode->force_output = true;
789 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
791 if (! TREE_PUBLIC (vnode->decl))
792 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
793 "%<externally_visible%>"
794 " attribute have effect only on public objects");
796 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
797 && vnode->definition
798 && DECL_INITIAL (decl))
800 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
801 "%<weakref%> attribute ignored"
802 " because variable is initialized");
803 DECL_WEAK (decl) = 0;
804 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
805 DECL_ATTRIBUTES (decl));
807 process_common_attributes (decl);
811 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
812 middle end to output the variable to asm file, if needed or externally
813 visible. */
815 void
816 varpool_node::finalize_decl (tree decl)
818 varpool_node *node = varpool_node::get_create (decl);
820 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
822 if (node->definition)
823 return;
824 notice_global_symbol (decl);
825 node->definition = true;
826 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
827 /* Traditionally we do not eliminate static variables when not
828 optimizing and when not doing toplevel reoder. */
829 || (!flag_toplevel_reorder && !DECL_COMDAT (node->decl)
830 && !DECL_ARTIFICIAL (node->decl)))
831 node->force_output = true;
833 if (cgraph_state == CGRAPH_STATE_CONSTRUCTION
834 && (decide_is_symbol_needed (node)
835 || referred_to_p (node)))
836 enqueue_node (node);
837 if (cgraph_state >= CGRAPH_STATE_IPA_SSA)
838 node->analyze ();
839 /* Some frontends produce various interface variables after compilation
840 finished. */
841 if (cgraph_state == CGRAPH_STATE_FINISHED
842 || (!flag_toplevel_reorder && cgraph_state == CGRAPH_STATE_EXPANSION))
843 node->assemble_decl ();
846 /* EDGE is an polymorphic call. Mark all possible targets as reachable
847 and if there is only one target, perform trivial devirtualization.
848 REACHABLE_CALL_TARGETS collects target lists we already walked to
849 avoid udplicate work. */
851 static void
852 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
853 struct cgraph_edge *edge)
855 unsigned int i;
856 void *cache_token;
857 bool final;
858 vec <cgraph_node *>targets
859 = possible_polymorphic_call_targets
860 (edge, &final, &cache_token);
862 if (!reachable_call_targets->add (cache_token))
864 if (cgraph_dump_file)
865 dump_possible_polymorphic_call_targets
866 (cgraph_dump_file, edge);
868 for (i = 0; i < targets.length (); i++)
870 /* Do not bother to mark virtual methods in anonymous namespace;
871 either we will find use of virtual table defining it, or it is
872 unused. */
873 if (targets[i]->definition
874 && TREE_CODE
875 (TREE_TYPE (targets[i]->decl))
876 == METHOD_TYPE
877 && !type_in_anonymous_namespace_p
878 (method_class_type
879 (TREE_TYPE (targets[i]->decl))))
880 enqueue_node (targets[i]);
884 /* Very trivial devirtualization; when the type is
885 final or anonymous (so we know all its derivation)
886 and there is only one possible virtual call target,
887 make the edge direct. */
888 if (final)
890 if (targets.length () <= 1 && dbg_cnt (devirt))
892 cgraph_node *target;
893 if (targets.length () == 1)
894 target = targets[0];
895 else
896 target = cgraph_node::create
897 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
899 if (cgraph_dump_file)
901 fprintf (cgraph_dump_file,
902 "Devirtualizing call: ");
903 print_gimple_stmt (cgraph_dump_file,
904 edge->call_stmt, 0,
905 TDF_SLIM);
907 if (dump_enabled_p ())
909 location_t locus = gimple_location_safe (edge->call_stmt);
910 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
911 "devirtualizing call in %s to %s\n",
912 edge->caller->name (), target->name ());
915 cgraph_make_edge_direct (edge, target);
916 cgraph_redirect_edge_call_stmt_to_callee (edge);
917 if (cgraph_dump_file)
919 fprintf (cgraph_dump_file,
920 "Devirtualized as: ");
921 print_gimple_stmt (cgraph_dump_file,
922 edge->call_stmt, 0,
923 TDF_SLIM);
930 /* Discover all functions and variables that are trivially needed, analyze
931 them as well as all functions and variables referred by them */
933 static void
934 analyze_functions (void)
936 /* Keep track of already processed nodes when called multiple times for
937 intermodule optimization. */
938 static struct cgraph_node *first_analyzed;
939 struct cgraph_node *first_handled = first_analyzed;
940 static varpool_node *first_analyzed_var;
941 varpool_node *first_handled_var = first_analyzed_var;
942 hash_set<void *> reachable_call_targets;
944 symtab_node *node;
945 symtab_node *next;
946 int i;
947 struct ipa_ref *ref;
948 bool changed = true;
949 location_t saved_loc = input_location;
951 bitmap_obstack_initialize (NULL);
952 cgraph_state = CGRAPH_STATE_CONSTRUCTION;
953 input_location = UNKNOWN_LOCATION;
955 /* Ugly, but the fixup can not happen at a time same body alias is created;
956 C++ FE is confused about the COMDAT groups being right. */
957 if (cpp_implicit_aliases_done)
958 FOR_EACH_SYMBOL (node)
959 if (node->cpp_implicit_alias)
960 node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
961 if (optimize && flag_devirtualize)
962 build_type_inheritance_graph ();
964 /* Analysis adds static variables that in turn adds references to new functions.
965 So we need to iterate the process until it stabilize. */
966 while (changed)
968 changed = false;
969 process_function_and_variable_attributes (first_analyzed,
970 first_analyzed_var);
972 /* First identify the trivially needed symbols. */
973 for (node = symtab_nodes;
974 node != first_analyzed
975 && node != first_analyzed_var; node = node->next)
977 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
978 node->get_comdat_group_id ();
979 if (decide_is_symbol_needed (node))
981 enqueue_node (node);
982 if (!changed && cgraph_dump_file)
983 fprintf (cgraph_dump_file, "Trivially needed symbols:");
984 changed = true;
985 if (cgraph_dump_file)
986 fprintf (cgraph_dump_file, " %s/%d", node->asm_name (),
987 node->order);
988 if (!changed && cgraph_dump_file)
989 fprintf (cgraph_dump_file, "\n");
991 if (node == first_analyzed
992 || node == first_analyzed_var)
993 break;
995 cgraph_process_new_functions ();
996 first_analyzed_var = varpool_first_variable ();
997 first_analyzed = cgraph_first_function ();
999 if (changed && cgraph_dump_file)
1000 fprintf (cgraph_dump_file, "\n");
1002 /* Lower representation, build callgraph edges and references for all trivially
1003 needed symbols and all symbols referred by them. */
1004 while (queued_nodes != &symtab_terminator)
1006 changed = true;
1007 node = queued_nodes;
1008 queued_nodes = (symtab_node *)queued_nodes->aux;
1009 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1010 if (cnode && cnode->definition)
1012 struct cgraph_edge *edge;
1013 tree decl = cnode->decl;
1015 /* ??? It is possible to create extern inline function
1016 and later using weak alias attribute to kill its body.
1017 See gcc.c-torture/compile/20011119-1.c */
1018 if (!DECL_STRUCT_FUNCTION (decl)
1019 && !cnode->alias
1020 && !cnode->thunk.thunk_p
1021 && !cnode->dispatcher_function)
1023 cnode->reset ();
1024 cnode->local.redefined_extern_inline = true;
1025 continue;
1028 if (!cnode->analyzed)
1029 cnode->analyze ();
1031 for (edge = cnode->callees; edge; edge = edge->next_callee)
1032 if (edge->callee->definition)
1033 enqueue_node (edge->callee);
1034 if (optimize && flag_devirtualize)
1036 struct cgraph_edge *next;
1038 for (edge = cnode->indirect_calls; edge; edge = next)
1040 next = edge->next_callee;
1041 if (edge->indirect_info->polymorphic)
1042 walk_polymorphic_call_targets (&reachable_call_targets,
1043 edge);
1047 /* If decl is a clone of an abstract function,
1048 mark that abstract function so that we don't release its body.
1049 The DECL_INITIAL() of that abstract function declaration
1050 will be later needed to output debug info. */
1051 if (DECL_ABSTRACT_ORIGIN (decl))
1053 struct cgraph_node *origin_node
1054 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl));
1055 origin_node->used_as_abstract_origin = true;
1058 else
1060 varpool_node *vnode = dyn_cast <varpool_node *> (node);
1061 if (vnode && vnode->definition && !vnode->analyzed)
1062 vnode->analyze ();
1065 if (node->same_comdat_group)
1067 symtab_node *next;
1068 for (next = node->same_comdat_group;
1069 next != node;
1070 next = next->same_comdat_group)
1071 enqueue_node (next);
1073 for (i = 0; node->iterate_reference (i, ref); i++)
1074 if (ref->referred->definition)
1075 enqueue_node (ref->referred);
1076 cgraph_process_new_functions ();
1079 if (optimize && flag_devirtualize)
1080 update_type_inheritance_graph ();
1082 /* Collect entry points to the unit. */
1083 if (cgraph_dump_file)
1085 fprintf (cgraph_dump_file, "\n\nInitial ");
1086 symtab_node::dump_table (cgraph_dump_file);
1089 if (cgraph_dump_file)
1090 fprintf (cgraph_dump_file, "\nRemoving unused symbols:");
1092 for (node = symtab_nodes;
1093 node != first_handled
1094 && node != first_handled_var; node = next)
1096 next = node->next;
1097 if (!node->aux && !referred_to_p (node))
1099 if (cgraph_dump_file)
1100 fprintf (cgraph_dump_file, " %s/%d", node->name (), node->order);
1101 node->remove ();
1102 continue;
1104 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1106 tree decl = node->decl;
1108 if (cnode->definition && !gimple_has_body_p (decl)
1109 && !cnode->alias
1110 && !cnode->thunk.thunk_p)
1111 cnode->reset ();
1113 gcc_assert (!cnode->definition || cnode->thunk.thunk_p
1114 || cnode->alias
1115 || gimple_has_body_p (decl));
1116 gcc_assert (cnode->analyzed == cnode->definition);
1118 node->aux = NULL;
1120 for (;node; node = node->next)
1121 node->aux = NULL;
1122 first_analyzed = cgraph_first_function ();
1123 first_analyzed_var = varpool_first_variable ();
1124 if (cgraph_dump_file)
1126 fprintf (cgraph_dump_file, "\n\nReclaimed ");
1127 symtab_node::dump_table (cgraph_dump_file);
1129 bitmap_obstack_release (NULL);
1130 ggc_collect ();
1131 /* Initialize assembler name hash, in particular we want to trigger C++
1132 mangling and same body alias creation before we free DECL_ARGUMENTS
1133 used by it. */
1134 if (!seen_error ())
1135 symtab_initialize_asm_name_hash ();
1137 input_location = saved_loc;
1140 /* Translate the ugly representation of aliases as alias pairs into nice
1141 representation in callgraph. We don't handle all cases yet,
1142 unfortunately. */
1144 static void
1145 handle_alias_pairs (void)
1147 alias_pair *p;
1148 unsigned i;
1150 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1152 symtab_node *target_node = symtab_node_for_asm (p->target);
1154 /* Weakrefs with target not defined in current unit are easy to handle:
1155 they behave just as external variables except we need to note the
1156 alias flag to later output the weakref pseudo op into asm file. */
1157 if (!target_node
1158 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1160 symtab_node *node = symtab_node::get (p->decl);
1161 if (node)
1163 node->alias_target = p->target;
1164 node->weakref = true;
1165 node->alias = true;
1167 alias_pairs->unordered_remove (i);
1168 continue;
1170 else if (!target_node)
1172 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1173 symtab_node *node = symtab_node::get (p->decl);
1174 if (node)
1175 node->alias = false;
1176 alias_pairs->unordered_remove (i);
1177 continue;
1180 if (DECL_EXTERNAL (target_node->decl)
1181 /* We use local aliases for C++ thunks to force the tailcall
1182 to bind locally. This is a hack - to keep it working do
1183 the following (which is not strictly correct). */
1184 && (! TREE_CODE (target_node->decl) == FUNCTION_DECL
1185 || ! DECL_VIRTUAL_P (target_node->decl))
1186 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1188 error ("%q+D aliased to external symbol %qE",
1189 p->decl, p->target);
1192 if (TREE_CODE (p->decl) == FUNCTION_DECL
1193 && target_node && is_a <cgraph_node *> (target_node))
1195 struct cgraph_node *src_node = cgraph_node::get (p->decl);
1196 if (src_node && src_node->definition)
1197 src_node->reset ();
1198 cgraph_node::create_alias (p->decl, target_node->decl);
1199 alias_pairs->unordered_remove (i);
1201 else if (TREE_CODE (p->decl) == VAR_DECL
1202 && target_node && is_a <varpool_node *> (target_node))
1204 varpool_node::create_alias (p->decl, target_node->decl);
1205 alias_pairs->unordered_remove (i);
1207 else
1209 error ("%q+D alias in between function and variable is not supported",
1210 p->decl);
1211 warning (0, "%q+D aliased declaration",
1212 target_node->decl);
1213 alias_pairs->unordered_remove (i);
1216 vec_free (alias_pairs);
1219 /* Hash function for symbol (function) resolution. */
1221 static hashval_t
1222 hash_node_by_assembler_name (const void *p)
1224 const struct cgraph_node *n = (const struct cgraph_node *) p;
1225 return (hashval_t) decl_assembler_name_hash (
1226 DECL_ASSEMBLER_NAME (n->decl));
1229 /* Equality function for cgraph_node table. */
1231 static int
1232 eq_node_assembler_name (const void *p1, const void *p2)
1234 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
1235 const_tree name = (const_tree)p2;
1236 return (decl_assembler_name_equal (n1->decl, name));
1239 /* In l-ipo mode compilation (light weight IPO), multiple bodies may
1240 be available for the same inline declared function. cgraph linking
1241 does not really merge them in order to keep the context (module info)
1242 of each body. After inlining, the linkage of the function may require
1243 them to be output (even if it is defined in an auxiliary module). This
1244 in term may result in duplicate emission. */
1246 static GTY((param_is (symtab_node))) htab_t output_node_hash = NULL;
1248 /* Add NODE that is expanded into the hashtable. */
1250 static struct cgraph_node *
1251 cgraph_add_output_node (struct cgraph_node *node)
1253 void **aslot;
1254 tree name;
1256 if (!L_IPO_COMP_MODE)
1257 return node;
1259 /* Never common non public names except for compiler
1260 generated static functions. (they are not promoted
1261 to globals either. */
1262 if (!TREE_PUBLIC (node->decl)
1263 && !(DECL_ARTIFICIAL (node->decl)
1264 && DECL_ASSEMBLER_NAME_SET_P (node->decl)))
1265 return node;
1267 if (!output_node_hash)
1268 output_node_hash =
1269 htab_create_ggc (10, hash_node_by_assembler_name,
1270 eq_node_assembler_name, NULL);
1272 name = DECL_ASSEMBLER_NAME (node->decl);
1274 aslot = htab_find_slot_with_hash (output_node_hash, name,
1275 decl_assembler_name_hash (name),
1276 INSERT);
1277 if (*aslot == NULL)
1279 *aslot = node;
1280 return node;
1282 else
1283 return (struct cgraph_node *)(*aslot);
1286 #if ENABLE_CHECKING
1287 /* Return the cgraph_node if the function symbol for NODE is
1288 expanded in the output. Returns NULL otherwise. */
1290 static struct cgraph_node *
1291 cgraph_find_output_node (struct cgraph_node *node)
1293 void **aslot;
1294 tree name;
1296 if (!L_IPO_COMP_MODE)
1297 return node;
1299 /* We do not track non-public functions. */
1300 if (!TREE_PUBLIC (node->decl))
1301 return NULL;
1303 /* Never addedd. */
1304 if (!output_node_hash)
1305 return NULL;
1307 name = DECL_ASSEMBLER_NAME (node->decl);
1309 aslot = htab_find_slot_with_hash (output_node_hash, name,
1310 decl_assembler_name_hash (name),
1311 NO_INSERT);
1312 if (!aslot)
1313 return NULL;
1315 return (struct cgraph_node *)(*aslot);
1317 #endif
1320 #if ENABLE_CHECKING
1321 /* A function used in validation. Return true if NODE was
1322 not expanded and its body was not reclaimed. */
1324 static bool
1325 cgraph_node_expansion_skipped (struct cgraph_node *node)
1327 struct cgraph_node *output_node;
1329 if (!L_IPO_COMP_MODE)
1330 return false;
1332 output_node = cgraph_find_output_node (node);
1334 if (output_node == node)
1335 return false;
1337 if (output_node)
1338 return true;
1340 /* No output, no duplicate being output, and the node is not
1341 inlined (and reclaimed) either -- check if the caller node
1342 is output/expanded or not. */
1343 if (node->global.inlined_to)
1344 return cgraph_node_expansion_skipped (node->global.inlined_to);
1346 /* External functions not marked for output. */
1347 return true;
1349 #endif
1351 /* Figure out what functions we want to assemble. */
1353 static void
1354 mark_functions_to_output (void)
1356 struct cgraph_node *node;
1357 #ifdef ENABLE_CHECKING
1358 bool check_same_comdat_groups = false;
1360 FOR_EACH_FUNCTION (node)
1361 gcc_assert (!node->process);
1362 #endif
1364 FOR_EACH_FUNCTION (node)
1366 tree decl = node->decl;
1368 gcc_assert (!node->process || node->same_comdat_group);
1369 if (node->process)
1370 continue;
1372 /* We need to output all local functions that are used and not
1373 always inlined, as well as those that are reachable from
1374 outside the current compilation unit. */
1375 if (node->analyzed
1376 && !node->thunk.thunk_p
1377 && !node->alias
1378 && !node->global.inlined_to
1379 && !TREE_ASM_WRITTEN (decl)
1380 && !(DECL_EXTERNAL (decl) || cgraph_is_aux_decl_external (node)))
1382 if (cgraph_add_output_node (node) == node) {
1383 /* Do not fix indentation. */
1384 node->process = 1;
1385 if (node->same_comdat_group)
1387 struct cgraph_node *next;
1388 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
1389 next != node;
1390 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
1391 if (!next->thunk.thunk_p && !next->alias
1392 && cgraph_add_output_node (next) == next
1393 && !next->comdat_local_p ())
1394 next->process = 1;
1398 else if (node->same_comdat_group)
1400 #ifdef ENABLE_CHECKING
1401 check_same_comdat_groups = true;
1402 #endif
1404 else
1406 /* We should've reclaimed all functions that are not needed. */
1407 #ifdef ENABLE_CHECKING
1408 if (!node->global.inlined_to
1409 && gimple_has_body_p (decl)
1410 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1411 are inside partition, we can end up not removing the body since we no longer
1412 have analyzed node pointing to it. */
1413 && !node->in_other_partition
1414 && !node->alias
1415 && !cgraph_is_auxiliary (node->decl)
1416 && !node->clones
1417 && !DECL_EXTERNAL (decl))
1419 node->debug ();
1420 internal_error ("failed to reclaim unneeded function");
1422 #endif
1423 gcc_assert (node->global.inlined_to
1424 || !gimple_has_body_p (decl)
1425 || node->in_other_partition
1426 || node->clones
1427 || DECL_ARTIFICIAL (decl)
1428 || DECL_EXTERNAL (decl)
1429 || cgraph_is_auxiliary (node->decl));
1434 #ifdef ENABLE_CHECKING
1435 if (check_same_comdat_groups && !L_IPO_COMP_MODE)
1436 FOR_EACH_FUNCTION (node)
1437 if (node->same_comdat_group && !node->process)
1439 tree decl = node->decl;
1440 if (!node->global.inlined_to
1441 && gimple_has_body_p (decl)
1442 /* FIXME: in an ltrans unit when the offline copy is outside a
1443 partition but inline copies are inside a partition, we can
1444 end up not removing the body since we no longer have an
1445 analyzed node pointing to it. */
1446 && !node->in_other_partition
1447 && !node->clones
1448 && !(DECL_EXTERNAL (decl) || cgraph_is_aux_decl_external (node))
1449 && !L_IPO_COMP_MODE)
1451 node->debug ();
1452 internal_error ("failed to reclaim unneeded function in same "
1453 "comdat group");
1456 #endif
1459 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1460 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1462 Set current_function_decl and cfun to newly constructed empty function body.
1463 return basic block in the function body. */
1465 basic_block
1466 init_lowered_empty_function (tree decl, bool in_ssa)
1468 basic_block bb;
1470 current_function_decl = decl;
1471 allocate_struct_function (decl, false);
1472 gimple_register_cfg_hooks ();
1473 init_empty_tree_cfg ();
1475 if (in_ssa)
1477 init_tree_ssa (cfun);
1478 init_ssa_operands (cfun);
1479 cfun->gimple_df->in_ssa_p = true;
1480 cfun->curr_properties |= PROP_ssa;
1483 DECL_INITIAL (decl) = make_node (BLOCK);
1485 DECL_SAVED_TREE (decl) = error_mark_node;
1486 cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1487 | PROP_cfg | PROP_loops);
1489 set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ());
1490 init_loops_structure (cfun, loops_for_fn (cfun), 1);
1491 loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1493 /* Create BB for body of the function and connect it properly. */
1494 bb = create_basic_block (NULL, (void *) 0, ENTRY_BLOCK_PTR_FOR_FN (cfun));
1495 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU);
1496 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1497 add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
1499 return bb;
1502 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1503 offset indicated by VIRTUAL_OFFSET, if that is
1504 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1505 zero for a result adjusting thunk. */
1507 static tree
1508 thunk_adjust (gimple_stmt_iterator * bsi,
1509 tree ptr, bool this_adjusting,
1510 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1512 gimple stmt;
1513 tree ret;
1515 if (this_adjusting
1516 && fixed_offset != 0)
1518 stmt = gimple_build_assign
1519 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1520 ptr,
1521 fixed_offset));
1522 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1525 /* If there's a virtual offset, look up that value in the vtable and
1526 adjust the pointer again. */
1527 if (virtual_offset)
1529 tree vtabletmp;
1530 tree vtabletmp2;
1531 tree vtabletmp3;
1533 if (!vtable_entry_type)
1535 tree vfunc_type = make_node (FUNCTION_TYPE);
1536 TREE_TYPE (vfunc_type) = integer_type_node;
1537 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1538 layout_type (vfunc_type);
1540 vtable_entry_type = build_pointer_type (vfunc_type);
1543 vtabletmp =
1544 create_tmp_reg (build_pointer_type
1545 (build_pointer_type (vtable_entry_type)), "vptr");
1547 /* The vptr is always at offset zero in the object. */
1548 stmt = gimple_build_assign (vtabletmp,
1549 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1550 ptr));
1551 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1553 /* Form the vtable address. */
1554 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
1555 "vtableaddr");
1556 stmt = gimple_build_assign (vtabletmp2,
1557 build_simple_mem_ref (vtabletmp));
1558 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1560 /* Find the entry with the vcall offset. */
1561 stmt = gimple_build_assign (vtabletmp2,
1562 fold_build_pointer_plus_loc (input_location,
1563 vtabletmp2,
1564 virtual_offset));
1565 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1567 /* Get the offset itself. */
1568 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1569 "vcalloffset");
1570 stmt = gimple_build_assign (vtabletmp3,
1571 build_simple_mem_ref (vtabletmp2));
1572 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1574 /* Adjust the `this' pointer. */
1575 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1576 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1577 GSI_CONTINUE_LINKING);
1580 if (!this_adjusting
1581 && fixed_offset != 0)
1582 /* Adjust the pointer by the constant. */
1584 tree ptrtmp;
1586 if (TREE_CODE (ptr) == VAR_DECL)
1587 ptrtmp = ptr;
1588 else
1590 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
1591 stmt = gimple_build_assign (ptrtmp, ptr);
1592 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1594 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1595 ptrtmp, fixed_offset);
1598 /* Emit the statement and gimplify the adjustment expression. */
1599 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
1600 stmt = gimple_build_assign (ret, ptr);
1601 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1603 return ret;
1606 /* Expand thunk NODE to gimple if possible.
1607 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1608 no assembler is produced.
1609 When OUTPUT_ASM_THUNK is true, also produce assembler for
1610 thunks that are not lowered. */
1612 bool
1613 cgraph_node::expand_thunk (bool output_asm_thunks, bool force_gimple_thunk)
1615 bool this_adjusting = thunk.this_adjusting;
1616 HOST_WIDE_INT fixed_offset = thunk.fixed_offset;
1617 HOST_WIDE_INT virtual_value = thunk.virtual_value;
1618 tree virtual_offset = NULL;
1619 tree alias = callees->callee->decl;
1620 tree thunk_fndecl = decl;
1621 tree a;
1624 if (!force_gimple_thunk && this_adjusting
1625 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1626 virtual_value, alias))
1628 const char *fnname;
1629 tree fn_block;
1630 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1632 if (!output_asm_thunks)
1633 return false;
1635 if (in_lto_p)
1636 get_body ();
1637 a = DECL_ARGUMENTS (thunk_fndecl);
1639 current_function_decl = thunk_fndecl;
1641 /* Ensure thunks are emitted in their correct sections. */
1642 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1644 DECL_RESULT (thunk_fndecl)
1645 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1646 RESULT_DECL, 0, restype);
1647 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1648 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
1650 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1651 create one. */
1652 fn_block = make_node (BLOCK);
1653 BLOCK_VARS (fn_block) = a;
1654 DECL_INITIAL (thunk_fndecl) = fn_block;
1655 init_function_start (thunk_fndecl);
1656 cfun->is_thunk = 1;
1657 insn_locations_init ();
1658 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1659 prologue_location = curr_insn_location ();
1660 assemble_start_function (thunk_fndecl, fnname);
1662 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1663 fixed_offset, virtual_value, alias);
1665 assemble_end_function (thunk_fndecl, fnname);
1666 insn_locations_finalize ();
1667 init_insn_lengths ();
1668 free_after_compilation (cfun);
1669 set_cfun (NULL);
1670 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1671 thunk.thunk_p = false;
1672 analyzed = false;
1674 else
1676 tree restype;
1677 basic_block bb, then_bb, else_bb, return_bb;
1678 gimple_stmt_iterator bsi;
1679 int nargs = 0;
1680 tree arg;
1681 int i;
1682 tree resdecl;
1683 tree restmp = NULL;
1685 gimple call;
1686 gimple ret;
1688 if (in_lto_p)
1689 get_body ();
1690 a = DECL_ARGUMENTS (thunk_fndecl);
1692 current_function_decl = thunk_fndecl;
1694 /* Ensure thunks are emitted in their correct sections. */
1695 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1697 DECL_IGNORED_P (thunk_fndecl) = 1;
1698 bitmap_obstack_initialize (NULL);
1700 if (thunk.virtual_offset_p)
1701 virtual_offset = size_int (virtual_value);
1703 /* Build the return declaration for the function. */
1704 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1705 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1707 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1708 DECL_ARTIFICIAL (resdecl) = 1;
1709 DECL_IGNORED_P (resdecl) = 1;
1710 DECL_RESULT (thunk_fndecl) = resdecl;
1711 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1713 else
1714 resdecl = DECL_RESULT (thunk_fndecl);
1716 bb = then_bb = else_bb = return_bb = init_lowered_empty_function (thunk_fndecl, true);
1718 bsi = gsi_start_bb (bb);
1720 /* Build call to the function being thunked. */
1721 if (!VOID_TYPE_P (restype))
1723 if (DECL_BY_REFERENCE (resdecl))
1724 restmp = gimple_fold_indirect_ref (resdecl);
1725 else if (!is_gimple_reg_type (restype))
1727 restmp = resdecl;
1728 add_local_decl (cfun, restmp);
1729 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1731 else
1732 restmp = create_tmp_reg (restype, "retval");
1735 for (arg = a; arg; arg = DECL_CHAIN (arg))
1736 nargs++;
1737 auto_vec<tree> vargs (nargs);
1738 if (this_adjusting)
1739 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
1740 virtual_offset));
1741 else if (nargs)
1742 vargs.quick_push (a);
1744 if (nargs)
1745 for (i = 1, arg = DECL_CHAIN (a); i < nargs; i++, arg = DECL_CHAIN (arg))
1747 tree tmp = arg;
1748 if (!is_gimple_val (arg))
1750 tmp = create_tmp_reg (TYPE_MAIN_VARIANT
1751 (TREE_TYPE (arg)), "arg");
1752 gimple stmt = gimple_build_assign (tmp, arg);
1753 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1755 vargs.quick_push (tmp);
1757 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1758 callees->call_stmt = call;
1759 gimple_call_set_from_thunk (call, true);
1760 if (restmp)
1762 gimple_call_set_lhs (call, restmp);
1763 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp),
1764 TREE_TYPE (TREE_TYPE (alias))));
1766 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
1767 if (!(gimple_call_flags (call) & ECF_NORETURN))
1769 if (restmp && !this_adjusting
1770 && (fixed_offset || virtual_offset))
1772 tree true_label = NULL_TREE;
1774 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1776 gimple stmt;
1777 /* If the return type is a pointer, we need to
1778 protect against NULL. We know there will be an
1779 adjustment, because that's why we're emitting a
1780 thunk. */
1781 then_bb = create_basic_block (NULL, (void *) 0, bb);
1782 return_bb = create_basic_block (NULL, (void *) 0, then_bb);
1783 else_bb = create_basic_block (NULL, (void *) 0, else_bb);
1784 add_bb_to_loop (then_bb, bb->loop_father);
1785 add_bb_to_loop (return_bb, bb->loop_father);
1786 add_bb_to_loop (else_bb, bb->loop_father);
1787 remove_edge (single_succ_edge (bb));
1788 true_label = gimple_block_label (then_bb);
1789 stmt = gimple_build_cond (NE_EXPR, restmp,
1790 build_zero_cst (TREE_TYPE (restmp)),
1791 NULL_TREE, NULL_TREE);
1792 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1793 make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1794 make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1795 make_edge (return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1796 make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1797 make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1798 bsi = gsi_last_bb (then_bb);
1801 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1802 fixed_offset, virtual_offset);
1803 if (true_label)
1805 gimple stmt;
1806 bsi = gsi_last_bb (else_bb);
1807 stmt = gimple_build_assign (restmp,
1808 build_zero_cst (TREE_TYPE (restmp)));
1809 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1810 bsi = gsi_last_bb (return_bb);
1813 else
1814 gimple_call_set_tail (call, true);
1816 /* Build return value. */
1817 ret = gimple_build_return (restmp);
1818 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1820 else
1822 gimple_call_set_tail (call, true);
1823 remove_edge (single_succ_edge (bb));
1826 cfun->gimple_df->in_ssa_p = true;
1827 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
1828 TREE_ASM_WRITTEN (thunk_fndecl) = false;
1829 delete_unreachable_blocks ();
1830 update_ssa (TODO_update_ssa);
1831 #ifdef ENABLE_CHECKING
1832 verify_flow_info ();
1833 #endif
1834 free_dominance_info (CDI_DOMINATORS);
1836 /* Since we want to emit the thunk, we explicitly mark its name as
1837 referenced. */
1838 thunk.thunk_p = false;
1839 lowered = true;
1840 bitmap_obstack_release (NULL);
1842 current_function_decl = NULL;
1843 set_cfun (NULL);
1844 return true;
1847 /* Assemble thunks and aliases associated to NODE. */
1849 static void
1850 assemble_thunks_and_aliases (struct cgraph_node *node)
1852 struct cgraph_edge *e;
1853 struct ipa_ref *ref;
1855 for (e = node->callers; e;)
1856 if (e->caller->thunk.thunk_p)
1858 struct cgraph_node *thunk = e->caller;
1860 e = e->next_caller;
1861 thunk->expand_thunk (true, false);
1862 assemble_thunks_and_aliases (thunk);
1864 else
1865 e = e->next_caller;
1867 FOR_EACH_ALIAS (node, ref)
1869 struct cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1870 bool saved_written = TREE_ASM_WRITTEN (node->decl);
1872 /* Force assemble_alias to really output the alias this time instead
1873 of buffering it in same alias pairs. */
1874 TREE_ASM_WRITTEN (node->decl) = 1;
1875 do_assemble_alias (alias->decl,
1876 DECL_ASSEMBLER_NAME (node->decl));
1877 assemble_thunks_and_aliases (alias);
1878 TREE_ASM_WRITTEN (node->decl) = saved_written;
1882 /* Expand function specified by NODE. */
1884 static void
1885 expand_function (struct cgraph_node *node)
1887 tree decl = node->decl;
1888 location_t saved_loc;
1890 /* We ought to not compile any inline clones. */
1891 gcc_assert (!node->global.inlined_to);
1893 announce_function (decl);
1894 node->process = 0;
1895 gcc_assert (node->lowered);
1896 node->get_body ();
1898 /* Generate RTL for the body of DECL. */
1900 timevar_push (TV_REST_OF_COMPILATION);
1902 gcc_assert (cgraph_global_info_ready);
1904 /* Initialize the default bitmap obstack. */
1905 bitmap_obstack_initialize (NULL);
1907 /* Initialize the RTL code for the function. */
1908 current_function_decl = decl;
1909 saved_loc = input_location;
1910 input_location = DECL_SOURCE_LOCATION (decl);
1911 init_function_start (decl);
1913 gimple_register_cfg_hooks ();
1915 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1917 execute_all_ipa_transforms ();
1919 /* Perform all tree transforms and optimizations. */
1921 /* Signal the start of passes. */
1922 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1924 execute_pass_list (cfun, g->get_passes ()->all_passes);
1926 /* Signal the end of passes. */
1927 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1929 bitmap_obstack_release (&reg_obstack);
1931 /* Release the default bitmap obstack. */
1932 bitmap_obstack_release (NULL);
1934 /* If requested, warn about function definitions where the function will
1935 return a value (usually of some struct or union type) which itself will
1936 take up a lot of stack space. */
1937 if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl))
1939 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
1941 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1942 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1943 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1944 larger_than_size))
1946 unsigned int size_as_int
1947 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1949 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1950 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
1951 decl, size_as_int);
1952 else
1953 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
1954 decl, larger_than_size);
1958 gimple_set_body (decl, NULL);
1959 if (DECL_STRUCT_FUNCTION (decl) == 0
1960 && !cgraph_node::get (decl)->origin)
1962 /* Stop pointing to the local nodes about to be freed.
1963 But DECL_INITIAL must remain nonzero so we know this
1964 was an actual function definition.
1965 For a nested function, this is done in c_pop_function_context.
1966 If rest_of_compilation set this to 0, leave it 0. */
1967 if (DECL_INITIAL (decl) != 0)
1968 DECL_INITIAL (decl) = error_mark_node;
1971 input_location = saved_loc;
1973 ggc_collect ();
1974 timevar_pop (TV_REST_OF_COMPILATION);
1976 /* Make sure that BE didn't give up on compiling. */
1977 gcc_assert (TREE_ASM_WRITTEN (decl));
1978 set_cfun (NULL);
1979 current_function_decl = NULL;
1981 /* It would make a lot more sense to output thunks before function body to get more
1982 forward and lest backwarding jumps. This however would need solving problem
1983 with comdats. See PR48668. Also aliases must come after function itself to
1984 make one pass assemblers, like one on AIX, happy. See PR 50689.
1985 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1986 groups. */
1987 assemble_thunks_and_aliases (node);
1988 node->release_body ();
1989 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1990 points to the dead function body. */
1991 node->remove_callees ();
1992 node->remove_all_references ();
1995 /* Node comparer that is responsible for the order that corresponds
1996 to time when a function was launched for the first time. */
1998 static int
1999 node_cmp (const void *pa, const void *pb)
2001 const struct cgraph_node *a = *(const struct cgraph_node * const *) pa;
2002 const struct cgraph_node *b = *(const struct cgraph_node * const *) pb;
2004 /* Functions with time profile must be before these without profile. */
2005 if (!a->tp_first_run || !b->tp_first_run)
2006 return a->tp_first_run - b->tp_first_run;
2008 return a->tp_first_run != b->tp_first_run
2009 ? b->tp_first_run - a->tp_first_run
2010 : b->order - a->order;
2013 /* Expand all functions that must be output.
2015 Attempt to topologically sort the nodes so function is output when
2016 all called functions are already assembled to allow data to be
2017 propagated across the callgraph. Use a stack to get smaller distance
2018 between a function and its callees (later we may choose to use a more
2019 sophisticated algorithm for function reordering; we will likely want
2020 to use subsections to make the output functions appear in top-down
2021 order). */
2023 static void
2024 expand_all_functions (void)
2026 struct cgraph_node *node;
2027 struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
2028 unsigned int expanded_func_count = 0, profiled_func_count = 0;
2029 int order_pos, new_order_pos = 0;
2030 int i;
2032 order_pos = ipa_reverse_postorder (order);
2033 gcc_assert (order_pos == cgraph_n_nodes);
2035 /* Garbage collector may remove inline clones we eliminate during
2036 optimization. So we must be sure to not reference them. */
2037 for (i = 0; i < order_pos; i++)
2038 if (order[i]->process)
2039 order[new_order_pos++] = order[i];
2041 if (flag_profile_reorder_functions)
2042 qsort (order, new_order_pos, sizeof (struct cgraph_node *), node_cmp);
2044 for (i = new_order_pos - 1; i >= 0; i--)
2046 node = order[i];
2048 if (node->process)
2050 expanded_func_count++;
2051 if(node->tp_first_run)
2052 profiled_func_count++;
2054 if (cgraph_dump_file)
2055 fprintf (cgraph_dump_file, "Time profile order in expand_all_functions:%s:%d\n", node->asm_name (), node->tp_first_run);
2057 node->process = 0;
2058 expand_function (node);
2062 if (dump_file)
2063 fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
2064 main_input_filename, profiled_func_count, expanded_func_count);
2066 if (cgraph_dump_file && flag_profile_reorder_functions)
2067 fprintf (cgraph_dump_file, "Expanded functions with time profile:%u/%u\n",
2068 profiled_func_count, expanded_func_count);
2070 cgraph_process_new_functions ();
2071 free_gimplify_stack ();
2073 free (order);
2076 /* This is used to sort the node types by the cgraph order number. */
2078 enum cgraph_order_sort_kind
2080 ORDER_UNDEFINED = 0,
2081 ORDER_FUNCTION,
2082 ORDER_VAR,
2083 ORDER_ASM
2086 struct cgraph_order_sort
2088 enum cgraph_order_sort_kind kind;
2089 union
2091 struct cgraph_node *f;
2092 varpool_node *v;
2093 struct asm_node *a;
2094 } u;
2097 /* Output all functions, variables, and asm statements in the order
2098 according to their order fields, which is the order in which they
2099 appeared in the file. This implements -fno-toplevel-reorder. In
2100 this mode we may output functions and variables which don't really
2101 need to be output. */
2103 static void
2104 output_in_order (void)
2106 int max;
2107 struct cgraph_order_sort *nodes;
2108 int i;
2109 struct cgraph_node *pf;
2110 varpool_node *pv;
2111 struct asm_node *pa;
2113 max = symtab_order;
2114 nodes = XCNEWVEC (struct cgraph_order_sort, max);
2116 varpool_remove_duplicate_weak_decls ();
2118 FOR_EACH_DEFINED_FUNCTION (pf)
2120 if (pf->process && !pf->thunk.thunk_p && !pf->alias)
2122 i = pf->order;
2123 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2124 nodes[i].kind = ORDER_FUNCTION;
2125 nodes[i].u.f = pf;
2129 FOR_EACH_DEFINED_VARIABLE (pv)
2130 if (!DECL_EXTERNAL (pv->decl))
2132 i = pv->order;
2133 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2134 nodes[i].kind = ORDER_VAR;
2135 nodes[i].u.v = pv;
2138 for (pa = asm_nodes; pa; pa = pa->next)
2140 i = pa->order;
2141 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2142 nodes[i].kind = ORDER_ASM;
2143 nodes[i].u.a = pa;
2146 /* In toplevel reorder mode we output all statics; mark them as needed. */
2148 for (i = 0; i < max; ++i)
2149 if (nodes[i].kind == ORDER_VAR)
2150 nodes[i].u.v->finalize_named_section_flags ();
2152 for (i = 0; i < max; ++i)
2154 switch (nodes[i].kind)
2156 case ORDER_FUNCTION:
2157 nodes[i].u.f->process = 0;
2158 expand_function (nodes[i].u.f);
2159 break;
2161 case ORDER_VAR:
2162 nodes[i].u.v->assemble_decl ();
2163 break;
2165 case ORDER_ASM:
2166 assemble_asm (nodes[i].u.a->asm_str);
2167 break;
2169 case ORDER_UNDEFINED:
2170 break;
2172 default:
2173 gcc_unreachable ();
2177 asm_nodes = NULL;
2178 free (nodes);
2181 static void
2182 ipa_passes (void)
2184 gcc::pass_manager *passes = g->get_passes ();
2186 set_cfun (NULL);
2187 current_function_decl = NULL;
2188 gimple_register_cfg_hooks ();
2189 bitmap_obstack_initialize (NULL);
2191 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2193 if (!in_lto_p)
2195 execute_ipa_pass_list (passes->all_small_ipa_passes);
2196 if (seen_error ())
2197 return;
2200 /* We never run removal of unreachable nodes after early passes. This is
2201 because TODO is run before the subpasses. It is important to remove
2202 the unreachable functions to save works at IPA level and to get LTO
2203 symbol tables right. */
2204 symtab_remove_unreachable_nodes (true, cgraph_dump_file);
2206 /* If pass_all_early_optimizations was not scheduled, the state of
2207 the cgraph will not be properly updated. Update it now. */
2208 if (cgraph_state < CGRAPH_STATE_IPA_SSA)
2209 cgraph_state = CGRAPH_STATE_IPA_SSA;
2211 if (!in_lto_p)
2213 /* Generate coverage variables and constructors.
2214 In LIPO mode, delay this until direct call profiling
2215 is done. */
2216 if (!flag_dyn_ipa)
2217 coverage_finish ();
2219 /* Process new functions added. */
2220 set_cfun (NULL);
2221 current_function_decl = NULL;
2222 cgraph_process_new_functions ();
2224 execute_ipa_summary_passes
2225 ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2228 /* Some targets need to handle LTO assembler output specially. */
2229 if (flag_generate_lto)
2230 targetm.asm_out.lto_start ();
2232 if (!in_lto_p)
2233 ipa_write_summaries ();
2235 if (flag_generate_lto)
2236 targetm.asm_out.lto_end ();
2238 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
2239 execute_ipa_pass_list (passes->all_regular_ipa_passes);
2240 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2242 bitmap_obstack_release (NULL);
2246 /* Return string alias is alias of. */
2248 static tree
2249 get_alias_symbol (tree decl)
2251 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
2252 return get_identifier (TREE_STRING_POINTER
2253 (TREE_VALUE (TREE_VALUE (alias))));
2257 /* Weakrefs may be associated to external decls and thus not output
2258 at expansion time. Emit all necessary aliases. */
2260 static void
2261 output_weakrefs (void)
2263 symtab_node *node;
2264 FOR_EACH_SYMBOL (node)
2265 if (node->alias
2266 && !TREE_ASM_WRITTEN (node->decl)
2267 && node->weakref)
2269 tree target;
2271 /* Weakrefs are special by not requiring target definition in current
2272 compilation unit. It is thus bit hard to work out what we want to
2273 alias.
2274 When alias target is defined, we need to fetch it from symtab reference,
2275 otherwise it is pointed to by alias_target. */
2276 if (node->alias_target)
2277 target = (DECL_P (node->alias_target)
2278 ? DECL_ASSEMBLER_NAME (node->alias_target)
2279 : node->alias_target);
2280 else if (node->analyzed)
2281 target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
2282 else
2284 gcc_unreachable ();
2285 target = get_alias_symbol (node->decl);
2287 do_assemble_alias (node->decl, target);
2291 /* Initialize callgraph dump file. */
2293 void
2294 init_cgraph (void)
2296 if (!cgraph_dump_file)
2297 cgraph_dump_file = dump_begin (TDI_cgraph, NULL);
2301 /* Perform simple optimizations based on callgraph. */
2303 void
2304 compile (void)
2306 if (seen_error ())
2307 return;
2309 #ifdef ENABLE_CHECKING
2310 symtab_node::verify_symtab_nodes ();
2311 #endif
2313 timevar_push (TV_CGRAPHOPT);
2314 if (pre_ipa_mem_report)
2316 fprintf (stderr, "Memory consumption before IPA\n");
2317 dump_memory_report (false);
2319 if (!quiet_flag)
2320 fprintf (stderr, "Performing interprocedural optimizations\n");
2321 cgraph_state = CGRAPH_STATE_IPA;
2323 if (L_IPO_COMP_MODE)
2325 cgraph_init_gid_map ();
2326 cgraph_add_fake_indirect_call_edges ();
2329 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2330 if (flag_lto)
2331 lto_streamer_hooks_init ();
2333 /* Don't run the IPA passes if there was any error or sorry messages. */
2334 if (!seen_error ())
2335 ipa_passes ();
2337 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2338 if (seen_error ()
2339 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
2341 timevar_pop (TV_CGRAPHOPT);
2342 return;
2345 /* This pass remove bodies of extern inline functions we never inlined.
2346 Do this later so other IPA passes see what is really going on. */
2347 symtab_remove_unreachable_nodes (false, dump_file);
2348 cgraph_global_info_ready = true;
2349 if (cgraph_dump_file)
2351 fprintf (cgraph_dump_file, "Optimized ");
2352 symtab_node:: dump_table (cgraph_dump_file);
2354 if (post_ipa_mem_report)
2356 fprintf (stderr, "Memory consumption after IPA\n");
2357 dump_memory_report (false);
2359 timevar_pop (TV_CGRAPHOPT);
2361 /* Output everything. */
2362 (*debug_hooks->assembly_start) ();
2363 if (!quiet_flag)
2364 fprintf (stderr, "Assembling functions:\n");
2365 #ifdef ENABLE_CHECKING
2366 symtab_node::verify_symtab_nodes ();
2367 #endif
2369 cgraph_materialize_all_clones ();
2370 bitmap_obstack_initialize (NULL);
2371 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2372 symtab_remove_unreachable_nodes (true, dump_file);
2373 #ifdef ENABLE_CHECKING
2374 symtab_node::verify_symtab_nodes ();
2375 #endif
2376 bitmap_obstack_release (NULL);
2377 mark_functions_to_output ();
2379 /* When weakref support is missing, we autmatically translate all
2380 references to NODE to references to its ultimate alias target.
2381 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2382 TREE_CHAIN.
2384 Set up this mapping before we output any assembler but once we are sure
2385 that all symbol renaming is done.
2387 FIXME: All this uglyness can go away if we just do renaming at gimple
2388 level by physically rewritting the IL. At the moment we can only redirect
2389 calls, so we need infrastructure for renaming references as well. */
2390 #ifndef ASM_OUTPUT_WEAKREF
2391 symtab_node *node;
2393 FOR_EACH_SYMBOL (node)
2394 if (node->alias
2395 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2397 IDENTIFIER_TRANSPARENT_ALIAS
2398 (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2399 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2400 = (node->alias_target ? node->alias_target
2401 : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
2403 #endif
2405 cgraph_state = CGRAPH_STATE_EXPANSION;
2407 if (!flag_toplevel_reorder)
2408 output_in_order ();
2409 else
2411 output_asm_statements ();
2413 expand_all_functions ();
2414 varpool_remove_duplicate_weak_decls ();
2415 varpool_node::output_variables ();
2418 cgraph_process_new_functions ();
2419 cgraph_state = CGRAPH_STATE_FINISHED;
2420 output_weakrefs ();
2422 if (cgraph_dump_file)
2424 fprintf (cgraph_dump_file, "\nFinal ");
2425 symtab_node::dump_table (cgraph_dump_file);
2427 #ifdef ENABLE_CHECKING
2428 symtab_node::verify_symtab_nodes ();
2429 /* Double check that all inline clones are gone and that all
2430 function bodies have been released from memory.
2431 As an exception, allow inline clones in the callgraph if
2432 they are auxiliary functions. This is because we don't
2433 expand any of the auxiliary functions, which may result
2434 in inline clones of some auxiliary functions to be left
2435 in the callgraph. */
2436 if (!seen_error ())
2438 struct cgraph_node *node;
2439 bool error_found = false;
2441 FOR_EACH_DEFINED_FUNCTION (node)
2442 if (((node->global.inlined_to && !cgraph_is_auxiliary (node->decl))
2443 || gimple_has_body_p (node->decl))
2444 && !cgraph_node_expansion_skipped (node))
2446 error_found = true;
2447 node->debug ();
2449 if (error_found)
2450 internal_error ("nodes with unreleased memory found");
2452 #endif
2456 /* Analyze the whole compilation unit once it is parsed completely. */
2458 void
2459 finalize_compilation_unit (void)
2461 timevar_push (TV_CGRAPH);
2463 /* If we're here there's no current function anymore. Some frontends
2464 are lazy in clearing these. */
2465 current_function_decl = NULL;
2466 set_cfun (NULL);
2468 /* Do not skip analyzing the functions if there were errors, we
2469 miss diagnostics for following functions otherwise. */
2471 /* Emit size functions we didn't inline. */
2472 finalize_size_functions ();
2474 /* Mark alias targets necessary and emit diagnostics. */
2475 handle_alias_pairs ();
2477 if (!quiet_flag)
2479 fprintf (stderr, "\nAnalyzing compilation unit\n");
2480 fflush (stderr);
2483 if (flag_dump_passes)
2484 dump_passes ();
2486 /* Gimplify and lower all functions, compute reachability and
2487 remove unreachable nodes. */
2488 analyze_functions ();
2490 /* Mark alias targets necessary and emit diagnostics. */
2491 handle_alias_pairs ();
2493 /* Gimplify and lower thunks. */
2494 analyze_functions ();
2496 /* Finally drive the pass manager. */
2497 compile ();
2499 timevar_pop (TV_CGRAPH);
2502 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2503 kind of wrapper method. */
2505 void
2506 cgraph_node::create_wrapper (struct cgraph_node *target)
2508 /* Preserve DECL_RESULT so we get right by reference flag. */
2509 tree decl_result = DECL_RESULT (decl);
2511 /* Remove the function's body. */
2512 release_body ();
2513 reset ();
2515 DECL_RESULT (decl) = decl_result;
2516 DECL_INITIAL (decl) = NULL;
2517 allocate_struct_function (decl, false);
2518 set_cfun (NULL);
2520 /* Turn alias into thunk and expand it into GIMPLE representation. */
2521 definition = true;
2522 thunk.thunk_p = true;
2523 thunk.this_adjusting = false;
2525 struct cgraph_edge *e = create_edge (target, NULL, 0, CGRAPH_FREQ_BASE);
2527 if (!expand_thunk (false, true))
2528 analyzed = true;
2530 e->call_stmt_cannot_inline_p = true;
2532 /* Inline summary set-up. */
2533 analyze ();
2534 inline_analyze_function (this);
2537 #include "gt-cgraphunit.h"