coretypes.h: Include input.h and as-a.h.
[gcc.git] / gcc / cgraphunit.c
1 /* Driver of optimization process
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21 /* This module implements main driver of compilation process.
22
23 The main scope of this file is to act as an interface in between
24 tree based frontends and the backend.
25
26 The front-end is supposed to use following functionality:
27
28 - finalize_function
29
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.
32
33 (There is one exception needed for implementing GCC extern inline
34 function.)
35
36 - varpool_finalize_decl
37
38 This function has same behavior as the above but is used for static
39 variables.
40
41 - add_asm_node
42
43 Insert new toplevel ASM statement
44
45 - finalize_compilation_unit
46
47 This function is called once (source level) compilation unit is finalized
48 and it will no longer change.
49
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.
54
55 At the end the bodies of unreachable functions are removed.
56
57 The function can be called multiple times when multiple source level
58 compilation units are combined.
59
60 - compile
61
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).
67
68 Compile time:
69
70 1) Inter-procedural optimization.
71 (ipa_passes)
72
73 This part is further split into:
74
75 a) early optimizations. These are local passes executed in
76 the topological order on the callgraph.
77
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.
82
83 b) early small interprocedural passes.
84
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.
88
89 c) IP analysis stage. All interprocedural passes do their
90 analysis.
91
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.
97
98 d) LTO sreaming. When doing LTO, everything important gets
99 streamed into the object file.
100
101 Compile time and or linktime analysis stage (WPA):
102
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.
111
112 Compile time and/or parallel linktime stage (ltrans)
113
114 Each of the object files is streamed back and compiled
115 separately. Now the function bodies becomes available
116 again.
117
118 2) Virtual clone materialization
119 (cgraph_materialize_clone)
120
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
127
128 All IP passes transform function bodies based on earlier
129 decision of the IP propagation.
130
131 4) late small IP passes
132
133 Simple IP passes working within single program partition.
134
135 5) Expansion
136 (expand_all_functions)
137
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.
143
144 Note that with -fno-toplevel-reorder passes 5 and 6
145 are combined together in cgraph_output_in_order.
146
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.
153
154 - cgraph_function_versioning
155
156 produces a copy of function into new one (a version)
157 and apply simple transformations
158 */
159
160 #include "config.h"
161 #include "system.h"
162 #include "coretypes.h"
163 #include "tm.h"
164 #include "alias.h"
165 #include "symtab.h"
166 #include "tree.h"
167 #include "fold-const.h"
168 #include "varasm.h"
169 #include "stor-layout.h"
170 #include "stringpool.h"
171 #include "output.h"
172 #include "rtl.h"
173 #include "predict.h"
174 #include "hard-reg-set.h"
175 #include "function.h"
176 #include "basic-block.h"
177 #include "dominance.h"
178 #include "cfgcleanup.h"
179 #include "cfg.h"
180 #include "tree-ssa-alias.h"
181 #include "internal-fn.h"
182 #include "gimple-fold.h"
183 #include "gimple-expr.h"
184 #include "gimple.h"
185 #include "gimplify.h"
186 #include "gimple-iterator.h"
187 #include "gimplify-me.h"
188 #include "gimple-ssa.h"
189 #include "tree-cfg.h"
190 #include "tree-into-ssa.h"
191 #include "tree-ssa.h"
192 #include "tree-inline.h"
193 #include "langhooks.h"
194 #include "toplev.h"
195 #include "flags.h"
196 #include "debug.h"
197 #include "target.h"
198 #include "diagnostic.h"
199 #include "params.h"
200 #include "intl.h"
201 #include "plugin-api.h"
202 #include "ipa-ref.h"
203 #include "cgraph.h"
204 #include "alloc-pool.h"
205 #include "symbol-summary.h"
206 #include "ipa-prop.h"
207 #include "tree-iterator.h"
208 #include "tree-pass.h"
209 #include "tree-dump.h"
210 #include "gimple-pretty-print.h"
211 #include "output.h"
212 #include "coverage.h"
213 #include "plugin.h"
214 #include "ipa-inline.h"
215 #include "ipa-utils.h"
216 #include "lto-streamer.h"
217 #include "except.h"
218 #include "cfgloop.h"
219 #include "regset.h" /* FIXME: For reg_obstack. */
220 #include "context.h"
221 #include "pass_manager.h"
222 #include "tree-nested.h"
223 #include "gimplify.h"
224 #include "dbgcnt.h"
225 #include "tree-chkp.h"
226 #include "lto-section-names.h"
227 #include "omp-low.h"
228 #include "print-tree.h"
229
230 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
231 secondary queue used during optimization to accommodate passes that
232 may generate new functions that need to be optimized and expanded. */
233 vec<cgraph_node *> cgraph_new_nodes;
234
235 static void expand_all_functions (void);
236 static void mark_functions_to_output (void);
237 static void handle_alias_pairs (void);
238
239 /* Used for vtable lookup in thunk adjusting. */
240 static GTY (()) tree vtable_entry_type;
241
242 /* Determine if symbol declaration is needed. That is, visible to something
243 either outside this translation unit, something magic in the system
244 configury */
245 bool
246 symtab_node::needed_p (void)
247 {
248 /* Double check that no one output the function into assembly file
249 early. */
250 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)
251 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
252
253 if (!definition)
254 return false;
255
256 if (DECL_EXTERNAL (decl))
257 return false;
258
259 /* If the user told us it is used, then it must be so. */
260 if (force_output)
261 return true;
262
263 /* ABI forced symbols are needed when they are external. */
264 if (forced_by_abi && TREE_PUBLIC (decl))
265 return true;
266
267 /* Keep constructors, destructors and virtual functions. */
268 if (TREE_CODE (decl) == FUNCTION_DECL
269 && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)))
270 return true;
271
272 /* Externally visible variables must be output. The exception is
273 COMDAT variables that must be output only when they are needed. */
274 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
275 return true;
276
277 return false;
278 }
279
280 /* Head and terminator of the queue of nodes to be processed while building
281 callgraph. */
282
283 static symtab_node symtab_terminator;
284 static symtab_node *queued_nodes = &symtab_terminator;
285
286 /* Add NODE to queue starting at QUEUED_NODES.
287 The queue is linked via AUX pointers and terminated by pointer to 1. */
288
289 static void
290 enqueue_node (symtab_node *node)
291 {
292 if (node->aux)
293 return;
294 gcc_checking_assert (queued_nodes);
295 node->aux = queued_nodes;
296 queued_nodes = node;
297 }
298
299 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
300 functions into callgraph in a way so they look like ordinary reachable
301 functions inserted into callgraph already at construction time. */
302
303 void
304 symbol_table::process_new_functions (void)
305 {
306 tree fndecl;
307
308 if (!cgraph_new_nodes.exists ())
309 return;
310
311 handle_alias_pairs ();
312 /* Note that this queue may grow as its being processed, as the new
313 functions may generate new ones. */
314 for (unsigned i = 0; i < cgraph_new_nodes.length (); i++)
315 {
316 cgraph_node *node = cgraph_new_nodes[i];
317 fndecl = node->decl;
318 switch (state)
319 {
320 case CONSTRUCTION:
321 /* At construction time we just need to finalize function and move
322 it into reachable functions list. */
323
324 cgraph_node::finalize_function (fndecl, false);
325 call_cgraph_insertion_hooks (node);
326 enqueue_node (node);
327 break;
328
329 case IPA:
330 case IPA_SSA:
331 case IPA_SSA_AFTER_INLINING:
332 /* When IPA optimization already started, do all essential
333 transformations that has been already performed on the whole
334 cgraph but not on this function. */
335
336 gimple_register_cfg_hooks ();
337 if (!node->analyzed)
338 node->analyze ();
339 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
340 if ((state == IPA_SSA || state == IPA_SSA_AFTER_INLINING)
341 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
342 g->get_passes ()->execute_early_local_passes ();
343 else if (inline_summaries != NULL)
344 compute_inline_parameters (node, true);
345 free_dominance_info (CDI_POST_DOMINATORS);
346 free_dominance_info (CDI_DOMINATORS);
347 pop_cfun ();
348 call_cgraph_insertion_hooks (node);
349 break;
350
351 case EXPANSION:
352 /* Functions created during expansion shall be compiled
353 directly. */
354 node->process = 0;
355 call_cgraph_insertion_hooks (node);
356 node->expand ();
357 break;
358
359 default:
360 gcc_unreachable ();
361 break;
362 }
363 }
364
365 cgraph_new_nodes.release ();
366 }
367
368 /* As an GCC extension we allow redefinition of the function. The
369 semantics when both copies of bodies differ is not well defined.
370 We replace the old body with new body so in unit at a time mode
371 we always use new body, while in normal mode we may end up with
372 old body inlined into some functions and new body expanded and
373 inlined in others.
374
375 ??? It may make more sense to use one body for inlining and other
376 body for expanding the function but this is difficult to do. */
377
378 void
379 cgraph_node::reset (void)
380 {
381 /* If process is set, then we have already begun whole-unit analysis.
382 This is *not* testing for whether we've already emitted the function.
383 That case can be sort-of legitimately seen with real function redefinition
384 errors. I would argue that the front end should never present us with
385 such a case, but don't enforce that for now. */
386 gcc_assert (!process);
387
388 /* Reset our data structures so we can analyze the function again. */
389 memset (&local, 0, sizeof (local));
390 memset (&global, 0, sizeof (global));
391 memset (&rtl, 0, sizeof (rtl));
392 analyzed = false;
393 definition = false;
394 alias = false;
395 weakref = false;
396 cpp_implicit_alias = false;
397
398 remove_callees ();
399 remove_all_references ();
400 }
401
402 /* Return true when there are references to the node. INCLUDE_SELF is
403 true if a self reference counts as a reference. */
404
405 bool
406 symtab_node::referred_to_p (bool include_self)
407 {
408 ipa_ref *ref = NULL;
409
410 /* See if there are any references at all. */
411 if (iterate_referring (0, ref))
412 return true;
413 /* For functions check also calls. */
414 cgraph_node *cn = dyn_cast <cgraph_node *> (this);
415 if (cn && cn->callers)
416 {
417 if (include_self)
418 return true;
419 for (cgraph_edge *e = cn->callers; e; e = e->next_caller)
420 if (e->caller != this)
421 return true;
422 }
423 return false;
424 }
425
426 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
427 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
428 the garbage collector run at the moment. We would need to either create
429 a new GC context, or just not compile right now. */
430
431 void
432 cgraph_node::finalize_function (tree decl, bool no_collect)
433 {
434 cgraph_node *node = cgraph_node::get_create (decl);
435
436 if (node->definition)
437 {
438 /* Nested functions should only be defined once. */
439 gcc_assert (!DECL_CONTEXT (decl)
440 || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL);
441 node->reset ();
442 node->local.redefined_extern_inline = true;
443 }
444
445 /* Set definition first before calling notice_global_symbol so that
446 it is available to notice_global_symbol. */
447 node->definition = true;
448 notice_global_symbol (decl);
449 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
450
451 /* With -fkeep-inline-functions we are keeping all inline functions except
452 for extern inline ones. */
453 if (flag_keep_inline_functions
454 && DECL_DECLARED_INLINE_P (decl)
455 && !DECL_EXTERNAL (decl)
456 && !DECL_DISREGARD_INLINE_LIMITS (decl))
457 node->force_output = 1;
458
459 /* When not optimizing, also output the static functions. (see
460 PR24561), but don't do so for always_inline functions, functions
461 declared inline and nested functions. These were optimized out
462 in the original implementation and it is unclear whether we want
463 to change the behavior here. */
464 if ((!opt_for_fn (decl, optimize)
465 && !node->cpp_implicit_alias
466 && !DECL_DISREGARD_INLINE_LIMITS (decl)
467 && !DECL_DECLARED_INLINE_P (decl)
468 && !(DECL_CONTEXT (decl)
469 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
470 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
471 node->force_output = 1;
472
473 /* If we've not yet emitted decl, tell the debug info about it. */
474 if (!TREE_ASM_WRITTEN (decl))
475 (*debug_hooks->deferred_inline_function) (decl);
476
477 /* Possibly warn about unused parameters. */
478 if (warn_unused_parameter)
479 do_warn_unused_parameter (decl);
480
481 if (!no_collect)
482 ggc_collect ();
483
484 if (symtab->state == CONSTRUCTION
485 && (node->needed_p () || node->referred_to_p ()))
486 enqueue_node (node);
487 }
488
489 /* Add the function FNDECL to the call graph.
490 Unlike finalize_function, this function is intended to be used
491 by middle end and allows insertion of new function at arbitrary point
492 of compilation. The function can be either in high, low or SSA form
493 GIMPLE.
494
495 The function is assumed to be reachable and have address taken (so no
496 API breaking optimizations are performed on it).
497
498 Main work done by this function is to enqueue the function for later
499 processing to avoid need the passes to be re-entrant. */
500
501 void
502 cgraph_node::add_new_function (tree fndecl, bool lowered)
503 {
504 gcc::pass_manager *passes = g->get_passes ();
505 cgraph_node *node;
506
507 if (dump_file)
508 {
509 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
510 const char *function_type = ((gimple_has_body_p (fndecl))
511 ? (lowered
512 ? (gimple_in_ssa_p (fn)
513 ? "ssa gimple"
514 : "low gimple")
515 : "high gimple")
516 : "to-be-gimplified");
517 fprintf (dump_file,
518 "Added new %s function %s to callgraph\n",
519 function_type,
520 fndecl_name (fndecl));
521 }
522
523 switch (symtab->state)
524 {
525 case PARSING:
526 cgraph_node::finalize_function (fndecl, false);
527 break;
528 case CONSTRUCTION:
529 /* Just enqueue function to be processed at nearest occurrence. */
530 node = cgraph_node::get_create (fndecl);
531 if (lowered)
532 node->lowered = true;
533 cgraph_new_nodes.safe_push (node);
534 break;
535
536 case IPA:
537 case IPA_SSA:
538 case IPA_SSA_AFTER_INLINING:
539 case EXPANSION:
540 /* Bring the function into finalized state and enqueue for later
541 analyzing and compilation. */
542 node = cgraph_node::get_create (fndecl);
543 node->local.local = false;
544 node->definition = true;
545 node->force_output = true;
546 if (!lowered && symtab->state == EXPANSION)
547 {
548 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
549 gimple_register_cfg_hooks ();
550 bitmap_obstack_initialize (NULL);
551 execute_pass_list (cfun, passes->all_lowering_passes);
552 passes->execute_early_local_passes ();
553 bitmap_obstack_release (NULL);
554 pop_cfun ();
555
556 lowered = true;
557 }
558 if (lowered)
559 node->lowered = true;
560 cgraph_new_nodes.safe_push (node);
561 break;
562
563 case FINISHED:
564 /* At the very end of compilation we have to do all the work up
565 to expansion. */
566 node = cgraph_node::create (fndecl);
567 if (lowered)
568 node->lowered = true;
569 node->definition = true;
570 node->analyze ();
571 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
572 gimple_register_cfg_hooks ();
573 bitmap_obstack_initialize (NULL);
574 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
575 g->get_passes ()->execute_early_local_passes ();
576 bitmap_obstack_release (NULL);
577 pop_cfun ();
578 node->expand ();
579 break;
580
581 default:
582 gcc_unreachable ();
583 }
584
585 /* Set a personality if required and we already passed EH lowering. */
586 if (lowered
587 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
588 == eh_personality_lang))
589 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
590 }
591
592 /* Analyze the function scheduled to be output. */
593 void
594 cgraph_node::analyze (void)
595 {
596 tree decl = this->decl;
597 location_t saved_loc = input_location;
598 input_location = DECL_SOURCE_LOCATION (decl);
599
600 if (thunk.thunk_p)
601 {
602 cgraph_node *t = cgraph_node::get (thunk.alias);
603
604 create_edge (t, NULL, 0, CGRAPH_FREQ_BASE);
605 /* Target code in expand_thunk may need the thunk's target
606 to be analyzed, so recurse here. */
607 if (!t->analyzed)
608 t->analyze ();
609 if (t->alias)
610 {
611 t = t->get_alias_target ();
612 if (!t->analyzed)
613 t->analyze ();
614 }
615 if (!expand_thunk (false, false))
616 {
617 thunk.alias = NULL;
618 return;
619 }
620 thunk.alias = NULL;
621 }
622 if (alias)
623 resolve_alias (cgraph_node::get (alias_target));
624 else if (dispatcher_function)
625 {
626 /* Generate the dispatcher body of multi-versioned functions. */
627 cgraph_function_version_info *dispatcher_version_info
628 = function_version ();
629 if (dispatcher_version_info != NULL
630 && (dispatcher_version_info->dispatcher_resolver
631 == NULL_TREE))
632 {
633 tree resolver = NULL_TREE;
634 gcc_assert (targetm.generate_version_dispatcher_body);
635 resolver = targetm.generate_version_dispatcher_body (this);
636 gcc_assert (resolver != NULL_TREE);
637 }
638 }
639 else
640 {
641 push_cfun (DECL_STRUCT_FUNCTION (decl));
642
643 assign_assembler_name_if_neeeded (decl);
644
645 /* Make sure to gimplify bodies only once. During analyzing a
646 function we lower it, which will require gimplified nested
647 functions, so we can end up here with an already gimplified
648 body. */
649 if (!gimple_has_body_p (decl))
650 gimplify_function_tree (decl);
651
652 /* Lower the function. */
653 if (!lowered)
654 {
655 if (nested)
656 lower_nested_functions (decl);
657 gcc_assert (!nested);
658
659 gimple_register_cfg_hooks ();
660 bitmap_obstack_initialize (NULL);
661 execute_pass_list (cfun, g->get_passes ()->all_lowering_passes);
662 free_dominance_info (CDI_POST_DOMINATORS);
663 free_dominance_info (CDI_DOMINATORS);
664 compact_blocks ();
665 bitmap_obstack_release (NULL);
666 lowered = true;
667 }
668
669 pop_cfun ();
670 }
671 analyzed = true;
672
673 input_location = saved_loc;
674 }
675
676 /* C++ frontend produce same body aliases all over the place, even before PCH
677 gets streamed out. It relies on us linking the aliases with their function
678 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
679 first produce aliases without links, but once C++ FE is sure he won't sream
680 PCH we build the links via this function. */
681
682 void
683 symbol_table::process_same_body_aliases (void)
684 {
685 symtab_node *node;
686 FOR_EACH_SYMBOL (node)
687 if (node->cpp_implicit_alias && !node->analyzed)
688 node->resolve_alias
689 (TREE_CODE (node->alias_target) == VAR_DECL
690 ? (symtab_node *)varpool_node::get_create (node->alias_target)
691 : (symtab_node *)cgraph_node::get_create (node->alias_target));
692 cpp_implicit_aliases_done = true;
693 }
694
695 /* Process attributes common for vars and functions. */
696
697 static void
698 process_common_attributes (symtab_node *node, tree decl)
699 {
700 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
701
702 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
703 {
704 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
705 "%<weakref%> attribute should be accompanied with"
706 " an %<alias%> attribute");
707 DECL_WEAK (decl) = 0;
708 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
709 DECL_ATTRIBUTES (decl));
710 }
711
712 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
713 node->no_reorder = 1;
714 }
715
716 /* Look for externally_visible and used attributes and mark cgraph nodes
717 accordingly.
718
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:
722
723 void f();
724 void f() __attribute__((used));
725
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.
729
730 Furthermore, we can't mark these nodes in finalize_function because:
731
732 void f() {}
733 void f() __attribute__((externally_visible));
734
735 is valid.
736
737 So, we walk the nodes at the end of the translation unit, applying the
738 attributes at that point. */
739
740 static void
741 process_function_and_variable_attributes (cgraph_node *first,
742 varpool_node *first_var)
743 {
744 cgraph_node *node;
745 varpool_node *vnode;
746
747 for (node = symtab->first_function (); node != first;
748 node = symtab->next_function (node))
749 {
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)))
754 {
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");
759 }
760 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
761 && (node->definition && !node->alias))
762 {
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));
769 }
770
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");
777
778 process_common_attributes (node, decl);
779 }
780 for (vnode = symtab->first_variable (); vnode != first_var;
781 vnode = symtab->next_variable (vnode))
782 {
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)))
790 {
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");
795 }
796 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
797 && vnode->definition
798 && DECL_INITIAL (decl))
799 {
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));
806 }
807 process_common_attributes (vnode, decl);
808 }
809 }
810
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. */
814
815 void
816 varpool_node::finalize_decl (tree decl)
817 {
818 varpool_node *node = varpool_node::get_create (decl);
819
820 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
821
822 if (node->definition)
823 return;
824 /* Set definition first before calling notice_global_symbol so that
825 it is available to notice_global_symbol. */
826 node->definition = true;
827 notice_global_symbol (decl);
828 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
829 /* Traditionally we do not eliminate static variables when not
830 optimizing and when not doing toplevel reoder. */
831 || node->no_reorder
832 || ((!flag_toplevel_reorder
833 && !DECL_COMDAT (node->decl)
834 && !DECL_ARTIFICIAL (node->decl))))
835 node->force_output = true;
836
837 if (symtab->state == CONSTRUCTION
838 && (node->needed_p () || node->referred_to_p ()))
839 enqueue_node (node);
840 if (symtab->state >= IPA_SSA)
841 node->analyze ();
842 /* Some frontends produce various interface variables after compilation
843 finished. */
844 if (symtab->state == FINISHED
845 || (!flag_toplevel_reorder
846 && symtab->state == EXPANSION))
847 node->assemble_decl ();
848
849 if (DECL_INITIAL (decl))
850 chkp_register_var_initializer (decl);
851 }
852
853 /* EDGE is an polymorphic call. Mark all possible targets as reachable
854 and if there is only one target, perform trivial devirtualization.
855 REACHABLE_CALL_TARGETS collects target lists we already walked to
856 avoid udplicate work. */
857
858 static void
859 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
860 cgraph_edge *edge)
861 {
862 unsigned int i;
863 void *cache_token;
864 bool final;
865 vec <cgraph_node *>targets
866 = possible_polymorphic_call_targets
867 (edge, &final, &cache_token);
868
869 if (!reachable_call_targets->add (cache_token))
870 {
871 if (symtab->dump_file)
872 dump_possible_polymorphic_call_targets
873 (symtab->dump_file, edge);
874
875 for (i = 0; i < targets.length (); i++)
876 {
877 /* Do not bother to mark virtual methods in anonymous namespace;
878 either we will find use of virtual table defining it, or it is
879 unused. */
880 if (targets[i]->definition
881 && TREE_CODE
882 (TREE_TYPE (targets[i]->decl))
883 == METHOD_TYPE
884 && !type_in_anonymous_namespace_p
885 (TYPE_METHOD_BASETYPE (TREE_TYPE (targets[i]->decl))))
886 enqueue_node (targets[i]);
887 }
888 }
889
890 /* Very trivial devirtualization; when the type is
891 final or anonymous (so we know all its derivation)
892 and there is only one possible virtual call target,
893 make the edge direct. */
894 if (final)
895 {
896 if (targets.length () <= 1 && dbg_cnt (devirt))
897 {
898 cgraph_node *target;
899 if (targets.length () == 1)
900 target = targets[0];
901 else
902 target = cgraph_node::create
903 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
904
905 if (symtab->dump_file)
906 {
907 fprintf (symtab->dump_file,
908 "Devirtualizing call: ");
909 print_gimple_stmt (symtab->dump_file,
910 edge->call_stmt, 0,
911 TDF_SLIM);
912 }
913 if (dump_enabled_p ())
914 {
915 location_t locus = gimple_location_safe (edge->call_stmt);
916 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
917 "devirtualizing call in %s to %s\n",
918 edge->caller->name (), target->name ());
919 }
920
921 edge->make_direct (target);
922 edge->redirect_call_stmt_to_callee ();
923
924 /* Call to __builtin_unreachable shouldn't be instrumented. */
925 if (!targets.length ())
926 gimple_call_set_with_bounds (edge->call_stmt, false);
927
928 if (symtab->dump_file)
929 {
930 fprintf (symtab->dump_file,
931 "Devirtualized as: ");
932 print_gimple_stmt (symtab->dump_file,
933 edge->call_stmt, 0,
934 TDF_SLIM);
935 }
936 }
937 }
938 }
939
940
941 /* Discover all functions and variables that are trivially needed, analyze
942 them as well as all functions and variables referred by them */
943 static cgraph_node *first_analyzed;
944 static varpool_node *first_analyzed_var;
945
946 /* FIRST_TIME is set to TRUE for the first time we are called for a
947 translation unit from finalize_compilation_unit() or false
948 otherwise. */
949
950 static void
951 analyze_functions (bool first_time)
952 {
953 /* Keep track of already processed nodes when called multiple times for
954 intermodule optimization. */
955 cgraph_node *first_handled = first_analyzed;
956 varpool_node *first_handled_var = first_analyzed_var;
957 hash_set<void *> reachable_call_targets;
958
959 symtab_node *node;
960 symtab_node *next;
961 int i;
962 ipa_ref *ref;
963 bool changed = true;
964 location_t saved_loc = input_location;
965
966 bitmap_obstack_initialize (NULL);
967 symtab->state = CONSTRUCTION;
968 input_location = UNKNOWN_LOCATION;
969
970 /* Ugly, but the fixup can not happen at a time same body alias is created;
971 C++ FE is confused about the COMDAT groups being right. */
972 if (symtab->cpp_implicit_aliases_done)
973 FOR_EACH_SYMBOL (node)
974 if (node->cpp_implicit_alias)
975 node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
976 build_type_inheritance_graph ();
977
978 /* Analysis adds static variables that in turn adds references to new functions.
979 So we need to iterate the process until it stabilize. */
980 while (changed)
981 {
982 changed = false;
983 process_function_and_variable_attributes (first_analyzed,
984 first_analyzed_var);
985
986 /* First identify the trivially needed symbols. */
987 for (node = symtab->first_symbol ();
988 node != first_analyzed
989 && node != first_analyzed_var; node = node->next)
990 {
991 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
992 node->get_comdat_group_id ();
993 if (node->needed_p ())
994 {
995 enqueue_node (node);
996 if (!changed && symtab->dump_file)
997 fprintf (symtab->dump_file, "Trivially needed symbols:");
998 changed = true;
999 if (symtab->dump_file)
1000 fprintf (symtab->dump_file, " %s", node->asm_name ());
1001 if (!changed && symtab->dump_file)
1002 fprintf (symtab->dump_file, "\n");
1003 }
1004 if (node == first_analyzed
1005 || node == first_analyzed_var)
1006 break;
1007 }
1008 symtab->process_new_functions ();
1009 first_analyzed_var = symtab->first_variable ();
1010 first_analyzed = symtab->first_function ();
1011
1012 if (changed && symtab->dump_file)
1013 fprintf (symtab->dump_file, "\n");
1014
1015 /* Lower representation, build callgraph edges and references for all trivially
1016 needed symbols and all symbols referred by them. */
1017 while (queued_nodes != &symtab_terminator)
1018 {
1019 changed = true;
1020 node = queued_nodes;
1021 queued_nodes = (symtab_node *)queued_nodes->aux;
1022 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1023 if (cnode && cnode->definition)
1024 {
1025 cgraph_edge *edge;
1026 tree decl = cnode->decl;
1027
1028 /* ??? It is possible to create extern inline function
1029 and later using weak alias attribute to kill its body.
1030 See gcc.c-torture/compile/20011119-1.c */
1031 if (!DECL_STRUCT_FUNCTION (decl)
1032 && !cnode->alias
1033 && !cnode->thunk.thunk_p
1034 && !cnode->dispatcher_function)
1035 {
1036 cnode->reset ();
1037 cnode->local.redefined_extern_inline = true;
1038 continue;
1039 }
1040
1041 if (!cnode->analyzed)
1042 cnode->analyze ();
1043
1044 for (edge = cnode->callees; edge; edge = edge->next_callee)
1045 if (edge->callee->definition
1046 && (!DECL_EXTERNAL (edge->callee->decl)
1047 /* When not optimizing, do not try to analyze extern
1048 inline functions. Doing so is pointless. */
1049 || opt_for_fn (edge->callee->decl, optimize)
1050 /* Weakrefs needs to be preserved. */
1051 || edge->callee->alias
1052 /* always_inline functions are inlined aven at -O0. */
1053 || lookup_attribute
1054 ("always_inline",
1055 DECL_ATTRIBUTES (edge->callee->decl))
1056 /* Multiversioned functions needs the dispatcher to
1057 be produced locally even for extern functions. */
1058 || edge->callee->function_version ()))
1059 enqueue_node (edge->callee);
1060 if (opt_for_fn (cnode->decl, optimize)
1061 && opt_for_fn (cnode->decl, flag_devirtualize))
1062 {
1063 cgraph_edge *next;
1064
1065 for (edge = cnode->indirect_calls; edge; edge = next)
1066 {
1067 next = edge->next_callee;
1068 if (edge->indirect_info->polymorphic)
1069 walk_polymorphic_call_targets (&reachable_call_targets,
1070 edge);
1071 }
1072 }
1073
1074 /* If decl is a clone of an abstract function,
1075 mark that abstract function so that we don't release its body.
1076 The DECL_INITIAL() of that abstract function declaration
1077 will be later needed to output debug info. */
1078 if (DECL_ABSTRACT_ORIGIN (decl))
1079 {
1080 cgraph_node *origin_node
1081 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl));
1082 origin_node->used_as_abstract_origin = true;
1083 }
1084 }
1085 else
1086 {
1087 varpool_node *vnode = dyn_cast <varpool_node *> (node);
1088 if (vnode && vnode->definition && !vnode->analyzed)
1089 vnode->analyze ();
1090 }
1091
1092 if (node->same_comdat_group)
1093 {
1094 symtab_node *next;
1095 for (next = node->same_comdat_group;
1096 next != node;
1097 next = next->same_comdat_group)
1098 if (!next->comdat_local_p ())
1099 enqueue_node (next);
1100 }
1101 for (i = 0; node->iterate_reference (i, ref); i++)
1102 if (ref->referred->definition
1103 && (!DECL_EXTERNAL (ref->referred->decl)
1104 || ((TREE_CODE (ref->referred->decl) != FUNCTION_DECL
1105 && optimize)
1106 || (TREE_CODE (ref->referred->decl) == FUNCTION_DECL
1107 && opt_for_fn (ref->referred->decl, optimize))
1108 || node->alias
1109 || ref->referred->alias)))
1110 enqueue_node (ref->referred);
1111 symtab->process_new_functions ();
1112 }
1113 }
1114 update_type_inheritance_graph ();
1115
1116 /* Collect entry points to the unit. */
1117 if (symtab->dump_file)
1118 {
1119 fprintf (symtab->dump_file, "\n\nInitial ");
1120 symtab_node::dump_table (symtab->dump_file);
1121 }
1122
1123 if (first_time)
1124 {
1125 symtab_node *snode;
1126 FOR_EACH_SYMBOL (snode)
1127 check_global_declaration (snode->decl);
1128 }
1129
1130 if (symtab->dump_file)
1131 fprintf (symtab->dump_file, "\nRemoving unused symbols:");
1132
1133 for (node = symtab->first_symbol ();
1134 node != first_handled
1135 && node != first_handled_var; node = next)
1136 {
1137 next = node->next;
1138 if (!node->aux && !node->referred_to_p ())
1139 {
1140 if (symtab->dump_file)
1141 fprintf (symtab->dump_file, " %s", node->name ());
1142
1143 /* See if the debugger can use anything before the DECL
1144 passes away. Perhaps it can notice a DECL that is now a
1145 constant and can tag the early DIE with an appropriate
1146 attribute.
1147
1148 Otherwise, this is the last chance the debug_hooks have
1149 at looking at optimized away DECLs, since
1150 late_global_decl will subsequently be called from the
1151 contents of the now pruned symbol table. */
1152 if (!decl_function_context (node->decl))
1153 (*debug_hooks->late_global_decl) (node->decl);
1154
1155 node->remove ();
1156 continue;
1157 }
1158 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1159 {
1160 tree decl = node->decl;
1161
1162 if (cnode->definition && !gimple_has_body_p (decl)
1163 && !cnode->alias
1164 && !cnode->thunk.thunk_p)
1165 cnode->reset ();
1166
1167 gcc_assert (!cnode->definition || cnode->thunk.thunk_p
1168 || cnode->alias
1169 || gimple_has_body_p (decl));
1170 gcc_assert (cnode->analyzed == cnode->definition);
1171 }
1172 node->aux = NULL;
1173 }
1174 for (;node; node = node->next)
1175 node->aux = NULL;
1176 first_analyzed = symtab->first_function ();
1177 first_analyzed_var = symtab->first_variable ();
1178 if (symtab->dump_file)
1179 {
1180 fprintf (symtab->dump_file, "\n\nReclaimed ");
1181 symtab_node::dump_table (symtab->dump_file);
1182 }
1183 bitmap_obstack_release (NULL);
1184 ggc_collect ();
1185 /* Initialize assembler name hash, in particular we want to trigger C++
1186 mangling and same body alias creation before we free DECL_ARGUMENTS
1187 used by it. */
1188 if (!seen_error ())
1189 symtab->symtab_initialize_asm_name_hash ();
1190
1191 input_location = saved_loc;
1192 }
1193
1194 /* Translate the ugly representation of aliases as alias pairs into nice
1195 representation in callgraph. We don't handle all cases yet,
1196 unfortunately. */
1197
1198 static void
1199 handle_alias_pairs (void)
1200 {
1201 alias_pair *p;
1202 unsigned i;
1203
1204 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1205 {
1206 symtab_node *target_node = symtab_node::get_for_asmname (p->target);
1207
1208 /* Weakrefs with target not defined in current unit are easy to handle:
1209 they behave just as external variables except we need to note the
1210 alias flag to later output the weakref pseudo op into asm file. */
1211 if (!target_node
1212 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1213 {
1214 symtab_node *node = symtab_node::get (p->decl);
1215 if (node)
1216 {
1217 node->alias_target = p->target;
1218 node->weakref = true;
1219 node->alias = true;
1220 }
1221 alias_pairs->unordered_remove (i);
1222 continue;
1223 }
1224 else if (!target_node)
1225 {
1226 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1227 symtab_node *node = symtab_node::get (p->decl);
1228 if (node)
1229 node->alias = false;
1230 alias_pairs->unordered_remove (i);
1231 continue;
1232 }
1233
1234 if (DECL_EXTERNAL (target_node->decl)
1235 /* We use local aliases for C++ thunks to force the tailcall
1236 to bind locally. This is a hack - to keep it working do
1237 the following (which is not strictly correct). */
1238 && (TREE_CODE (target_node->decl) != FUNCTION_DECL
1239 || ! DECL_VIRTUAL_P (target_node->decl))
1240 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1241 {
1242 error ("%q+D aliased to external symbol %qE",
1243 p->decl, p->target);
1244 }
1245
1246 if (TREE_CODE (p->decl) == FUNCTION_DECL
1247 && target_node && is_a <cgraph_node *> (target_node))
1248 {
1249 cgraph_node *src_node = cgraph_node::get (p->decl);
1250 if (src_node && src_node->definition)
1251 src_node->reset ();
1252 cgraph_node::create_alias (p->decl, target_node->decl);
1253 alias_pairs->unordered_remove (i);
1254 }
1255 else if (TREE_CODE (p->decl) == VAR_DECL
1256 && target_node && is_a <varpool_node *> (target_node))
1257 {
1258 varpool_node::create_alias (p->decl, target_node->decl);
1259 alias_pairs->unordered_remove (i);
1260 }
1261 else
1262 {
1263 error ("%q+D alias in between function and variable is not supported",
1264 p->decl);
1265 warning (0, "%q+D aliased declaration",
1266 target_node->decl);
1267 alias_pairs->unordered_remove (i);
1268 }
1269 }
1270 vec_free (alias_pairs);
1271 }
1272
1273
1274 /* Figure out what functions we want to assemble. */
1275
1276 static void
1277 mark_functions_to_output (void)
1278 {
1279 cgraph_node *node;
1280 #ifdef ENABLE_CHECKING
1281 bool check_same_comdat_groups = false;
1282
1283 FOR_EACH_FUNCTION (node)
1284 gcc_assert (!node->process);
1285 #endif
1286
1287 FOR_EACH_FUNCTION (node)
1288 {
1289 tree decl = node->decl;
1290
1291 gcc_assert (!node->process || node->same_comdat_group);
1292 if (node->process)
1293 continue;
1294
1295 /* We need to output all local functions that are used and not
1296 always inlined, as well as those that are reachable from
1297 outside the current compilation unit. */
1298 if (node->analyzed
1299 && !node->thunk.thunk_p
1300 && !node->alias
1301 && !node->global.inlined_to
1302 && !TREE_ASM_WRITTEN (decl)
1303 && !DECL_EXTERNAL (decl))
1304 {
1305 node->process = 1;
1306 if (node->same_comdat_group)
1307 {
1308 cgraph_node *next;
1309 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
1310 next != node;
1311 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
1312 if (!next->thunk.thunk_p && !next->alias
1313 && !next->comdat_local_p ())
1314 next->process = 1;
1315 }
1316 }
1317 else if (node->same_comdat_group)
1318 {
1319 #ifdef ENABLE_CHECKING
1320 check_same_comdat_groups = true;
1321 #endif
1322 }
1323 else
1324 {
1325 /* We should've reclaimed all functions that are not needed. */
1326 #ifdef ENABLE_CHECKING
1327 if (!node->global.inlined_to
1328 && gimple_has_body_p (decl)
1329 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1330 are inside partition, we can end up not removing the body since we no longer
1331 have analyzed node pointing to it. */
1332 && !node->in_other_partition
1333 && !node->alias
1334 && !node->clones
1335 && !DECL_EXTERNAL (decl))
1336 {
1337 node->debug ();
1338 internal_error ("failed to reclaim unneeded function");
1339 }
1340 #endif
1341 gcc_assert (node->global.inlined_to
1342 || !gimple_has_body_p (decl)
1343 || node->in_other_partition
1344 || node->clones
1345 || DECL_ARTIFICIAL (decl)
1346 || DECL_EXTERNAL (decl));
1347
1348 }
1349
1350 }
1351 #ifdef ENABLE_CHECKING
1352 if (check_same_comdat_groups)
1353 FOR_EACH_FUNCTION (node)
1354 if (node->same_comdat_group && !node->process)
1355 {
1356 tree decl = node->decl;
1357 if (!node->global.inlined_to
1358 && gimple_has_body_p (decl)
1359 /* FIXME: in an ltrans unit when the offline copy is outside a
1360 partition but inline copies are inside a partition, we can
1361 end up not removing the body since we no longer have an
1362 analyzed node pointing to it. */
1363 && !node->in_other_partition
1364 && !node->clones
1365 && !DECL_EXTERNAL (decl))
1366 {
1367 node->debug ();
1368 internal_error ("failed to reclaim unneeded function in same "
1369 "comdat group");
1370 }
1371 }
1372 #endif
1373 }
1374
1375 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1376 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1377
1378 Set current_function_decl and cfun to newly constructed empty function body.
1379 return basic block in the function body. */
1380
1381 basic_block
1382 init_lowered_empty_function (tree decl, bool in_ssa, gcov_type count)
1383 {
1384 basic_block bb;
1385 edge e;
1386
1387 current_function_decl = decl;
1388 allocate_struct_function (decl, false);
1389 gimple_register_cfg_hooks ();
1390 init_empty_tree_cfg ();
1391
1392 if (in_ssa)
1393 {
1394 init_tree_ssa (cfun);
1395 init_ssa_operands (cfun);
1396 cfun->gimple_df->in_ssa_p = true;
1397 cfun->curr_properties |= PROP_ssa;
1398 }
1399
1400 DECL_INITIAL (decl) = make_node (BLOCK);
1401
1402 DECL_SAVED_TREE (decl) = error_mark_node;
1403 cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1404 | PROP_cfg | PROP_loops);
1405
1406 set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ());
1407 init_loops_structure (cfun, loops_for_fn (cfun), 1);
1408 loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1409
1410 /* Create BB for body of the function and connect it properly. */
1411 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = count;
1412 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency = REG_BR_PROB_BASE;
1413 EXIT_BLOCK_PTR_FOR_FN (cfun)->count = count;
1414 EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency = REG_BR_PROB_BASE;
1415 bb = create_basic_block (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun));
1416 bb->count = count;
1417 bb->frequency = BB_FREQ_MAX;
1418 e = make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU);
1419 e->count = count;
1420 e->probability = REG_BR_PROB_BASE;
1421 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1422 e->count = count;
1423 e->probability = REG_BR_PROB_BASE;
1424 add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
1425
1426 return bb;
1427 }
1428
1429 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1430 offset indicated by VIRTUAL_OFFSET, if that is
1431 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1432 zero for a result adjusting thunk. */
1433
1434 static tree
1435 thunk_adjust (gimple_stmt_iterator * bsi,
1436 tree ptr, bool this_adjusting,
1437 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1438 {
1439 gassign *stmt;
1440 tree ret;
1441
1442 if (this_adjusting
1443 && fixed_offset != 0)
1444 {
1445 stmt = gimple_build_assign
1446 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1447 ptr,
1448 fixed_offset));
1449 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1450 }
1451
1452 /* If there's a virtual offset, look up that value in the vtable and
1453 adjust the pointer again. */
1454 if (virtual_offset)
1455 {
1456 tree vtabletmp;
1457 tree vtabletmp2;
1458 tree vtabletmp3;
1459
1460 if (!vtable_entry_type)
1461 {
1462 tree vfunc_type = make_node (FUNCTION_TYPE);
1463 TREE_TYPE (vfunc_type) = integer_type_node;
1464 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1465 layout_type (vfunc_type);
1466
1467 vtable_entry_type = build_pointer_type (vfunc_type);
1468 }
1469
1470 vtabletmp =
1471 create_tmp_reg (build_pointer_type
1472 (build_pointer_type (vtable_entry_type)), "vptr");
1473
1474 /* The vptr is always at offset zero in the object. */
1475 stmt = gimple_build_assign (vtabletmp,
1476 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1477 ptr));
1478 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1479
1480 /* Form the vtable address. */
1481 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
1482 "vtableaddr");
1483 stmt = gimple_build_assign (vtabletmp2,
1484 build_simple_mem_ref (vtabletmp));
1485 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1486
1487 /* Find the entry with the vcall offset. */
1488 stmt = gimple_build_assign (vtabletmp2,
1489 fold_build_pointer_plus_loc (input_location,
1490 vtabletmp2,
1491 virtual_offset));
1492 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1493
1494 /* Get the offset itself. */
1495 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1496 "vcalloffset");
1497 stmt = gimple_build_assign (vtabletmp3,
1498 build_simple_mem_ref (vtabletmp2));
1499 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1500
1501 /* Adjust the `this' pointer. */
1502 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1503 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1504 GSI_CONTINUE_LINKING);
1505 }
1506
1507 if (!this_adjusting
1508 && fixed_offset != 0)
1509 /* Adjust the pointer by the constant. */
1510 {
1511 tree ptrtmp;
1512
1513 if (TREE_CODE (ptr) == VAR_DECL)
1514 ptrtmp = ptr;
1515 else
1516 {
1517 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
1518 stmt = gimple_build_assign (ptrtmp, ptr);
1519 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1520 }
1521 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1522 ptrtmp, fixed_offset);
1523 }
1524
1525 /* Emit the statement and gimplify the adjustment expression. */
1526 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
1527 stmt = gimple_build_assign (ret, ptr);
1528 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1529
1530 return ret;
1531 }
1532
1533 /* Expand thunk NODE to gimple if possible.
1534 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1535 no assembler is produced.
1536 When OUTPUT_ASM_THUNK is true, also produce assembler for
1537 thunks that are not lowered. */
1538
1539 bool
1540 cgraph_node::expand_thunk (bool output_asm_thunks, bool force_gimple_thunk)
1541 {
1542 bool this_adjusting = thunk.this_adjusting;
1543 HOST_WIDE_INT fixed_offset = thunk.fixed_offset;
1544 HOST_WIDE_INT virtual_value = thunk.virtual_value;
1545 tree virtual_offset = NULL;
1546 tree alias = callees->callee->decl;
1547 tree thunk_fndecl = decl;
1548 tree a;
1549
1550 /* Instrumentation thunk is the same function with
1551 a different signature. Never need to expand it. */
1552 if (thunk.add_pointer_bounds_args)
1553 return false;
1554
1555 if (!force_gimple_thunk && this_adjusting
1556 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1557 virtual_value, alias))
1558 {
1559 const char *fnname;
1560 tree fn_block;
1561 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1562
1563 if (!output_asm_thunks)
1564 {
1565 analyzed = true;
1566 return false;
1567 }
1568
1569 if (in_lto_p)
1570 get_untransformed_body ();
1571 a = DECL_ARGUMENTS (thunk_fndecl);
1572
1573 current_function_decl = thunk_fndecl;
1574
1575 /* Ensure thunks are emitted in their correct sections. */
1576 resolve_unique_section (thunk_fndecl, 0,
1577 flag_function_sections);
1578
1579 DECL_RESULT (thunk_fndecl)
1580 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1581 RESULT_DECL, 0, restype);
1582 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1583 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
1584
1585 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1586 create one. */
1587 fn_block = make_node (BLOCK);
1588 BLOCK_VARS (fn_block) = a;
1589 DECL_INITIAL (thunk_fndecl) = fn_block;
1590 init_function_start (thunk_fndecl);
1591 cfun->is_thunk = 1;
1592 insn_locations_init ();
1593 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1594 prologue_location = curr_insn_location ();
1595 assemble_start_function (thunk_fndecl, fnname);
1596
1597 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1598 fixed_offset, virtual_value, alias);
1599
1600 assemble_end_function (thunk_fndecl, fnname);
1601 insn_locations_finalize ();
1602 init_insn_lengths ();
1603 free_after_compilation (cfun);
1604 set_cfun (NULL);
1605 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1606 thunk.thunk_p = false;
1607 analyzed = false;
1608 }
1609 else if (stdarg_p (TREE_TYPE (thunk_fndecl)))
1610 {
1611 error ("generic thunk code fails for method %qD which uses %<...%>",
1612 thunk_fndecl);
1613 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1614 analyzed = true;
1615 return false;
1616 }
1617 else
1618 {
1619 tree restype;
1620 basic_block bb, then_bb, else_bb, return_bb;
1621 gimple_stmt_iterator bsi;
1622 int nargs = 0;
1623 tree arg;
1624 int i;
1625 tree resdecl;
1626 tree restmp = NULL;
1627 tree resbnd = NULL;
1628
1629 gcall *call;
1630 greturn *ret;
1631 bool alias_is_noreturn = TREE_THIS_VOLATILE (alias);
1632
1633 if (in_lto_p)
1634 get_untransformed_body ();
1635 a = DECL_ARGUMENTS (thunk_fndecl);
1636
1637 current_function_decl = thunk_fndecl;
1638
1639 /* Ensure thunks are emitted in their correct sections. */
1640 resolve_unique_section (thunk_fndecl, 0,
1641 flag_function_sections);
1642
1643 DECL_IGNORED_P (thunk_fndecl) = 1;
1644 bitmap_obstack_initialize (NULL);
1645
1646 if (thunk.virtual_offset_p)
1647 virtual_offset = size_int (virtual_value);
1648
1649 /* Build the return declaration for the function. */
1650 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1651 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1652 {
1653 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1654 DECL_ARTIFICIAL (resdecl) = 1;
1655 DECL_IGNORED_P (resdecl) = 1;
1656 DECL_RESULT (thunk_fndecl) = resdecl;
1657 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1658 }
1659 else
1660 resdecl = DECL_RESULT (thunk_fndecl);
1661
1662 bb = then_bb = else_bb = return_bb
1663 = init_lowered_empty_function (thunk_fndecl, true, count);
1664
1665 bsi = gsi_start_bb (bb);
1666
1667 /* Build call to the function being thunked. */
1668 if (!VOID_TYPE_P (restype) && !alias_is_noreturn)
1669 {
1670 if (DECL_BY_REFERENCE (resdecl))
1671 {
1672 restmp = gimple_fold_indirect_ref (resdecl);
1673 if (!restmp)
1674 restmp = build2 (MEM_REF,
1675 TREE_TYPE (TREE_TYPE (DECL_RESULT (alias))),
1676 resdecl,
1677 build_int_cst (TREE_TYPE
1678 (DECL_RESULT (alias)), 0));
1679 }
1680 else if (!is_gimple_reg_type (restype))
1681 {
1682 if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl)))
1683 {
1684 restmp = resdecl;
1685
1686 if (TREE_CODE (restmp) == VAR_DECL)
1687 add_local_decl (cfun, restmp);
1688 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1689 }
1690 else
1691 restmp = create_tmp_var (restype, "retval");
1692 }
1693 else
1694 restmp = create_tmp_reg (restype, "retval");
1695 }
1696
1697 for (arg = a; arg; arg = DECL_CHAIN (arg))
1698 nargs++;
1699 auto_vec<tree> vargs (nargs);
1700 i = 0;
1701 arg = a;
1702 if (this_adjusting)
1703 {
1704 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
1705 virtual_offset));
1706 arg = DECL_CHAIN (a);
1707 i = 1;
1708 }
1709
1710 if (nargs)
1711 for (; i < nargs; i++, arg = DECL_CHAIN (arg))
1712 {
1713 tree tmp = arg;
1714 if (!is_gimple_val (arg))
1715 {
1716 tmp = create_tmp_reg (TYPE_MAIN_VARIANT
1717 (TREE_TYPE (arg)), "arg");
1718 gimple stmt = gimple_build_assign (tmp, arg);
1719 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1720 }
1721 vargs.quick_push (tmp);
1722 }
1723 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1724 callees->call_stmt = call;
1725 gimple_call_set_from_thunk (call, true);
1726 gimple_call_set_with_bounds (call, instrumentation_clone);
1727
1728 /* Return slot optimization is always possible and in fact requred to
1729 return values with DECL_BY_REFERENCE. */
1730 if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl))
1731 && (!is_gimple_reg_type (TREE_TYPE (resdecl))
1732 || DECL_BY_REFERENCE (resdecl)))
1733 gimple_call_set_return_slot_opt (call, true);
1734
1735 if (restmp && !alias_is_noreturn)
1736 {
1737 gimple_call_set_lhs (call, restmp);
1738 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp),
1739 TREE_TYPE (TREE_TYPE (alias))));
1740 }
1741 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
1742 if (!alias_is_noreturn)
1743 {
1744 if (instrumentation_clone
1745 && !DECL_BY_REFERENCE (resdecl)
1746 && restmp
1747 && BOUNDED_P (restmp))
1748 {
1749 resbnd = chkp_insert_retbnd_call (NULL, restmp, &bsi);
1750 create_edge (get_create (gimple_call_fndecl (gsi_stmt (bsi))),
1751 as_a <gcall *> (gsi_stmt (bsi)),
1752 callees->count, callees->frequency);
1753 }
1754
1755 if (restmp && !this_adjusting
1756 && (fixed_offset || virtual_offset))
1757 {
1758 tree true_label = NULL_TREE;
1759
1760 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1761 {
1762 gimple stmt;
1763 edge e;
1764 /* If the return type is a pointer, we need to
1765 protect against NULL. We know there will be an
1766 adjustment, because that's why we're emitting a
1767 thunk. */
1768 then_bb = create_basic_block (NULL, bb);
1769 then_bb->count = count - count / 16;
1770 then_bb->frequency = BB_FREQ_MAX - BB_FREQ_MAX / 16;
1771 return_bb = create_basic_block (NULL, then_bb);
1772 return_bb->count = count;
1773 return_bb->frequency = BB_FREQ_MAX;
1774 else_bb = create_basic_block (NULL, else_bb);
1775 then_bb->count = count / 16;
1776 then_bb->frequency = BB_FREQ_MAX / 16;
1777 add_bb_to_loop (then_bb, bb->loop_father);
1778 add_bb_to_loop (return_bb, bb->loop_father);
1779 add_bb_to_loop (else_bb, bb->loop_father);
1780 remove_edge (single_succ_edge (bb));
1781 true_label = gimple_block_label (then_bb);
1782 stmt = gimple_build_cond (NE_EXPR, restmp,
1783 build_zero_cst (TREE_TYPE (restmp)),
1784 NULL_TREE, NULL_TREE);
1785 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1786 e = make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1787 e->probability = REG_BR_PROB_BASE - REG_BR_PROB_BASE / 16;
1788 e->count = count - count / 16;
1789 e = make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1790 e->probability = REG_BR_PROB_BASE / 16;
1791 e->count = count / 16;
1792 e = make_edge (return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1793 e->probability = REG_BR_PROB_BASE;
1794 e->count = count;
1795 e = make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1796 e->probability = REG_BR_PROB_BASE;
1797 e->count = count - count / 16;
1798 e = make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1799 e->probability = REG_BR_PROB_BASE;
1800 e->count = count / 16;
1801 bsi = gsi_last_bb (then_bb);
1802 }
1803
1804 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1805 fixed_offset, virtual_offset);
1806 if (true_label)
1807 {
1808 gimple stmt;
1809 bsi = gsi_last_bb (else_bb);
1810 stmt = gimple_build_assign (restmp,
1811 build_zero_cst (TREE_TYPE (restmp)));
1812 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1813 bsi = gsi_last_bb (return_bb);
1814 }
1815 }
1816 else
1817 gimple_call_set_tail (call, true);
1818
1819 /* Build return value. */
1820 if (!DECL_BY_REFERENCE (resdecl))
1821 ret = gimple_build_return (restmp);
1822 else
1823 ret = gimple_build_return (resdecl);
1824 gimple_return_set_retbnd (ret, resbnd);
1825
1826 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1827 }
1828 else
1829 {
1830 gimple_call_set_tail (call, true);
1831 remove_edge (single_succ_edge (bb));
1832 }
1833
1834 cfun->gimple_df->in_ssa_p = true;
1835 profile_status_for_fn (cfun)
1836 = count ? PROFILE_READ : PROFILE_GUESSED;
1837 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
1838 TREE_ASM_WRITTEN (thunk_fndecl) = false;
1839 delete_unreachable_blocks ();
1840 update_ssa (TODO_update_ssa);
1841 #ifdef ENABLE_CHECKING
1842 verify_flow_info ();
1843 #endif
1844 free_dominance_info (CDI_DOMINATORS);
1845
1846 /* Since we want to emit the thunk, we explicitly mark its name as
1847 referenced. */
1848 thunk.thunk_p = false;
1849 lowered = true;
1850 bitmap_obstack_release (NULL);
1851 }
1852 current_function_decl = NULL;
1853 set_cfun (NULL);
1854 return true;
1855 }
1856
1857 /* Assemble thunks and aliases associated to node. */
1858
1859 void
1860 cgraph_node::assemble_thunks_and_aliases (void)
1861 {
1862 cgraph_edge *e;
1863 ipa_ref *ref;
1864
1865 for (e = callers; e;)
1866 if (e->caller->thunk.thunk_p
1867 && !e->caller->thunk.add_pointer_bounds_args)
1868 {
1869 cgraph_node *thunk = e->caller;
1870
1871 e = e->next_caller;
1872 thunk->expand_thunk (true, false);
1873 thunk->assemble_thunks_and_aliases ();
1874 }
1875 else
1876 e = e->next_caller;
1877
1878 FOR_EACH_ALIAS (this, ref)
1879 {
1880 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1881 bool saved_written = TREE_ASM_WRITTEN (decl);
1882
1883 /* Force assemble_alias to really output the alias this time instead
1884 of buffering it in same alias pairs. */
1885 TREE_ASM_WRITTEN (decl) = 1;
1886 do_assemble_alias (alias->decl,
1887 DECL_ASSEMBLER_NAME (decl));
1888 alias->assemble_thunks_and_aliases ();
1889 TREE_ASM_WRITTEN (decl) = saved_written;
1890 }
1891 }
1892
1893 /* Expand function specified by node. */
1894
1895 void
1896 cgraph_node::expand (void)
1897 {
1898 location_t saved_loc;
1899
1900 /* We ought to not compile any inline clones. */
1901 gcc_assert (!global.inlined_to);
1902
1903 announce_function (decl);
1904 process = 0;
1905 gcc_assert (lowered);
1906 get_untransformed_body ();
1907
1908 /* Generate RTL for the body of DECL. */
1909
1910 timevar_push (TV_REST_OF_COMPILATION);
1911
1912 gcc_assert (symtab->global_info_ready);
1913
1914 /* Initialize the default bitmap obstack. */
1915 bitmap_obstack_initialize (NULL);
1916
1917 /* Initialize the RTL code for the function. */
1918 current_function_decl = decl;
1919 saved_loc = input_location;
1920 input_location = DECL_SOURCE_LOCATION (decl);
1921 init_function_start (decl);
1922
1923 gimple_register_cfg_hooks ();
1924
1925 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1926
1927 execute_all_ipa_transforms ();
1928
1929 /* Perform all tree transforms and optimizations. */
1930
1931 /* Signal the start of passes. */
1932 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1933
1934 execute_pass_list (cfun, g->get_passes ()->all_passes);
1935
1936 /* Signal the end of passes. */
1937 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1938
1939 bitmap_obstack_release (&reg_obstack);
1940
1941 /* Release the default bitmap obstack. */
1942 bitmap_obstack_release (NULL);
1943
1944 /* If requested, warn about function definitions where the function will
1945 return a value (usually of some struct or union type) which itself will
1946 take up a lot of stack space. */
1947 if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl))
1948 {
1949 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
1950
1951 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1952 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1953 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1954 larger_than_size))
1955 {
1956 unsigned int size_as_int
1957 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1958
1959 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1960 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
1961 decl, size_as_int);
1962 else
1963 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
1964 decl, larger_than_size);
1965 }
1966 }
1967
1968 gimple_set_body (decl, NULL);
1969 if (DECL_STRUCT_FUNCTION (decl) == 0
1970 && !cgraph_node::get (decl)->origin)
1971 {
1972 /* Stop pointing to the local nodes about to be freed.
1973 But DECL_INITIAL must remain nonzero so we know this
1974 was an actual function definition.
1975 For a nested function, this is done in c_pop_function_context.
1976 If rest_of_compilation set this to 0, leave it 0. */
1977 if (DECL_INITIAL (decl) != 0)
1978 DECL_INITIAL (decl) = error_mark_node;
1979 }
1980
1981 input_location = saved_loc;
1982
1983 ggc_collect ();
1984 timevar_pop (TV_REST_OF_COMPILATION);
1985
1986 /* Make sure that BE didn't give up on compiling. */
1987 gcc_assert (TREE_ASM_WRITTEN (decl));
1988 set_cfun (NULL);
1989 current_function_decl = NULL;
1990
1991 /* It would make a lot more sense to output thunks before function body to get more
1992 forward and lest backwarding jumps. This however would need solving problem
1993 with comdats. See PR48668. Also aliases must come after function itself to
1994 make one pass assemblers, like one on AIX, happy. See PR 50689.
1995 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1996 groups. */
1997 assemble_thunks_and_aliases ();
1998 release_body ();
1999 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
2000 points to the dead function body. */
2001 remove_callees ();
2002 remove_all_references ();
2003 }
2004
2005 /* Node comparer that is responsible for the order that corresponds
2006 to time when a function was launched for the first time. */
2007
2008 static int
2009 node_cmp (const void *pa, const void *pb)
2010 {
2011 const cgraph_node *a = *(const cgraph_node * const *) pa;
2012 const cgraph_node *b = *(const cgraph_node * const *) pb;
2013
2014 /* Functions with time profile must be before these without profile. */
2015 if (!a->tp_first_run || !b->tp_first_run)
2016 return a->tp_first_run - b->tp_first_run;
2017
2018 return a->tp_first_run != b->tp_first_run
2019 ? b->tp_first_run - a->tp_first_run
2020 : b->order - a->order;
2021 }
2022
2023 /* Expand all functions that must be output.
2024
2025 Attempt to topologically sort the nodes so function is output when
2026 all called functions are already assembled to allow data to be
2027 propagated across the callgraph. Use a stack to get smaller distance
2028 between a function and its callees (later we may choose to use a more
2029 sophisticated algorithm for function reordering; we will likely want
2030 to use subsections to make the output functions appear in top-down
2031 order). */
2032
2033 static void
2034 expand_all_functions (void)
2035 {
2036 cgraph_node *node;
2037 cgraph_node **order = XCNEWVEC (cgraph_node *,
2038 symtab->cgraph_count);
2039 unsigned int expanded_func_count = 0, profiled_func_count = 0;
2040 int order_pos, new_order_pos = 0;
2041 int i;
2042
2043 order_pos = ipa_reverse_postorder (order);
2044 gcc_assert (order_pos == symtab->cgraph_count);
2045
2046 /* Garbage collector may remove inline clones we eliminate during
2047 optimization. So we must be sure to not reference them. */
2048 for (i = 0; i < order_pos; i++)
2049 if (order[i]->process)
2050 order[new_order_pos++] = order[i];
2051
2052 if (flag_profile_reorder_functions)
2053 qsort (order, new_order_pos, sizeof (cgraph_node *), node_cmp);
2054
2055 for (i = new_order_pos - 1; i >= 0; i--)
2056 {
2057 node = order[i];
2058
2059 if (node->process)
2060 {
2061 expanded_func_count++;
2062 if(node->tp_first_run)
2063 profiled_func_count++;
2064
2065 if (symtab->dump_file)
2066 fprintf (symtab->dump_file,
2067 "Time profile order in expand_all_functions:%s:%d\n",
2068 node->asm_name (), node->tp_first_run);
2069 node->process = 0;
2070 node->expand ();
2071 }
2072 }
2073
2074 if (dump_file)
2075 fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
2076 main_input_filename, profiled_func_count, expanded_func_count);
2077
2078 if (symtab->dump_file && flag_profile_reorder_functions)
2079 fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n",
2080 profiled_func_count, expanded_func_count);
2081
2082 symtab->process_new_functions ();
2083 free_gimplify_stack ();
2084
2085 free (order);
2086 }
2087
2088 /* This is used to sort the node types by the cgraph order number. */
2089
2090 enum cgraph_order_sort_kind
2091 {
2092 ORDER_UNDEFINED = 0,
2093 ORDER_FUNCTION,
2094 ORDER_VAR,
2095 ORDER_ASM
2096 };
2097
2098 struct cgraph_order_sort
2099 {
2100 enum cgraph_order_sort_kind kind;
2101 union
2102 {
2103 cgraph_node *f;
2104 varpool_node *v;
2105 asm_node *a;
2106 } u;
2107 };
2108
2109 /* Output all functions, variables, and asm statements in the order
2110 according to their order fields, which is the order in which they
2111 appeared in the file. This implements -fno-toplevel-reorder. In
2112 this mode we may output functions and variables which don't really
2113 need to be output.
2114 When NO_REORDER is true only do this for symbols marked no reorder. */
2115
2116 static void
2117 output_in_order (bool no_reorder)
2118 {
2119 int max;
2120 cgraph_order_sort *nodes;
2121 int i;
2122 cgraph_node *pf;
2123 varpool_node *pv;
2124 asm_node *pa;
2125 max = symtab->order;
2126 nodes = XCNEWVEC (cgraph_order_sort, max);
2127
2128 FOR_EACH_DEFINED_FUNCTION (pf)
2129 {
2130 if (pf->process && !pf->thunk.thunk_p && !pf->alias)
2131 {
2132 if (no_reorder && !pf->no_reorder)
2133 continue;
2134 i = pf->order;
2135 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2136 nodes[i].kind = ORDER_FUNCTION;
2137 nodes[i].u.f = pf;
2138 }
2139 }
2140
2141 FOR_EACH_DEFINED_VARIABLE (pv)
2142 if (!DECL_EXTERNAL (pv->decl))
2143 {
2144 if (no_reorder && !pv->no_reorder)
2145 continue;
2146 i = pv->order;
2147 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2148 nodes[i].kind = ORDER_VAR;
2149 nodes[i].u.v = pv;
2150 }
2151
2152 for (pa = symtab->first_asm_symbol (); pa; pa = pa->next)
2153 {
2154 i = pa->order;
2155 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2156 nodes[i].kind = ORDER_ASM;
2157 nodes[i].u.a = pa;
2158 }
2159
2160 /* In toplevel reorder mode we output all statics; mark them as needed. */
2161
2162 for (i = 0; i < max; ++i)
2163 if (nodes[i].kind == ORDER_VAR)
2164 nodes[i].u.v->finalize_named_section_flags ();
2165
2166 for (i = 0; i < max; ++i)
2167 {
2168 switch (nodes[i].kind)
2169 {
2170 case ORDER_FUNCTION:
2171 nodes[i].u.f->process = 0;
2172 nodes[i].u.f->expand ();
2173 break;
2174
2175 case ORDER_VAR:
2176 nodes[i].u.v->assemble_decl ();
2177 break;
2178
2179 case ORDER_ASM:
2180 assemble_asm (nodes[i].u.a->asm_str);
2181 break;
2182
2183 case ORDER_UNDEFINED:
2184 break;
2185
2186 default:
2187 gcc_unreachable ();
2188 }
2189 }
2190
2191 symtab->clear_asm_symbols ();
2192
2193 free (nodes);
2194 }
2195
2196 static void
2197 ipa_passes (void)
2198 {
2199 gcc::pass_manager *passes = g->get_passes ();
2200
2201 set_cfun (NULL);
2202 current_function_decl = NULL;
2203 gimple_register_cfg_hooks ();
2204 bitmap_obstack_initialize (NULL);
2205
2206 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2207
2208 if (!in_lto_p)
2209 {
2210 execute_ipa_pass_list (passes->all_small_ipa_passes);
2211 if (seen_error ())
2212 return;
2213 }
2214
2215 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2216 devirtualization and other changes where removal iterate. */
2217 symtab->remove_unreachable_nodes (symtab->dump_file);
2218
2219 /* If pass_all_early_optimizations was not scheduled, the state of
2220 the cgraph will not be properly updated. Update it now. */
2221 if (symtab->state < IPA_SSA)
2222 symtab->state = IPA_SSA;
2223
2224 if (!in_lto_p)
2225 {
2226 /* Generate coverage variables and constructors. */
2227 coverage_finish ();
2228
2229 /* Process new functions added. */
2230 set_cfun (NULL);
2231 current_function_decl = NULL;
2232 symtab->process_new_functions ();
2233
2234 execute_ipa_summary_passes
2235 ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2236 }
2237
2238 /* Some targets need to handle LTO assembler output specially. */
2239 if (flag_generate_lto || flag_generate_offload)
2240 targetm.asm_out.lto_start ();
2241
2242 if (!in_lto_p)
2243 {
2244 if (g->have_offload)
2245 {
2246 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2247 lto_stream_offload_p = true;
2248 ipa_write_summaries ();
2249 lto_stream_offload_p = false;
2250 }
2251 if (flag_lto)
2252 {
2253 section_name_prefix = LTO_SECTION_NAME_PREFIX;
2254 lto_stream_offload_p = false;
2255 ipa_write_summaries ();
2256 }
2257 }
2258
2259 if (flag_generate_lto || flag_generate_offload)
2260 targetm.asm_out.lto_end ();
2261
2262 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
2263 execute_ipa_pass_list (passes->all_regular_ipa_passes);
2264 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2265
2266 bitmap_obstack_release (NULL);
2267 }
2268
2269
2270 /* Return string alias is alias of. */
2271
2272 static tree
2273 get_alias_symbol (tree decl)
2274 {
2275 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
2276 return get_identifier (TREE_STRING_POINTER
2277 (TREE_VALUE (TREE_VALUE (alias))));
2278 }
2279
2280
2281 /* Weakrefs may be associated to external decls and thus not output
2282 at expansion time. Emit all necessary aliases. */
2283
2284 void
2285 symbol_table::output_weakrefs (void)
2286 {
2287 symtab_node *node;
2288 cgraph_node *cnode;
2289 FOR_EACH_SYMBOL (node)
2290 if (node->alias
2291 && !TREE_ASM_WRITTEN (node->decl)
2292 && (!(cnode = dyn_cast <cgraph_node *> (node))
2293 || !cnode->instrumented_version
2294 || !TREE_ASM_WRITTEN (cnode->instrumented_version->decl))
2295 && node->weakref)
2296 {
2297 tree target;
2298
2299 /* Weakrefs are special by not requiring target definition in current
2300 compilation unit. It is thus bit hard to work out what we want to
2301 alias.
2302 When alias target is defined, we need to fetch it from symtab reference,
2303 otherwise it is pointed to by alias_target. */
2304 if (node->alias_target)
2305 target = (DECL_P (node->alias_target)
2306 ? DECL_ASSEMBLER_NAME (node->alias_target)
2307 : node->alias_target);
2308 else if (node->analyzed)
2309 target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
2310 else
2311 {
2312 gcc_unreachable ();
2313 target = get_alias_symbol (node->decl);
2314 }
2315 do_assemble_alias (node->decl, target);
2316 }
2317 }
2318
2319 /* Perform simple optimizations based on callgraph. */
2320
2321 void
2322 symbol_table::compile (void)
2323 {
2324 if (seen_error ())
2325 return;
2326
2327 #ifdef ENABLE_CHECKING
2328 symtab_node::verify_symtab_nodes ();
2329 #endif
2330
2331 timevar_push (TV_CGRAPHOPT);
2332 if (pre_ipa_mem_report)
2333 {
2334 fprintf (stderr, "Memory consumption before IPA\n");
2335 dump_memory_report (false);
2336 }
2337 if (!quiet_flag)
2338 fprintf (stderr, "Performing interprocedural optimizations\n");
2339 state = IPA;
2340
2341 /* Offloading requires LTO infrastructure. */
2342 if (!in_lto_p && g->have_offload)
2343 flag_generate_offload = 1;
2344
2345 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2346 if (flag_generate_lto || flag_generate_offload)
2347 lto_streamer_hooks_init ();
2348
2349 /* Don't run the IPA passes if there was any error or sorry messages. */
2350 if (!seen_error ())
2351 ipa_passes ();
2352
2353 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2354 if (seen_error ()
2355 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
2356 {
2357 timevar_pop (TV_CGRAPHOPT);
2358 return;
2359 }
2360
2361 global_info_ready = true;
2362 if (dump_file)
2363 {
2364 fprintf (dump_file, "Optimized ");
2365 symtab_node:: dump_table (dump_file);
2366 }
2367 if (post_ipa_mem_report)
2368 {
2369 fprintf (stderr, "Memory consumption after IPA\n");
2370 dump_memory_report (false);
2371 }
2372 timevar_pop (TV_CGRAPHOPT);
2373
2374 /* Output everything. */
2375 (*debug_hooks->assembly_start) ();
2376 if (!quiet_flag)
2377 fprintf (stderr, "Assembling functions:\n");
2378 #ifdef ENABLE_CHECKING
2379 symtab_node::verify_symtab_nodes ();
2380 #endif
2381
2382 materialize_all_clones ();
2383 bitmap_obstack_initialize (NULL);
2384 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2385 bitmap_obstack_release (NULL);
2386 mark_functions_to_output ();
2387
2388 /* When weakref support is missing, we autmatically translate all
2389 references to NODE to references to its ultimate alias target.
2390 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2391 TREE_CHAIN.
2392
2393 Set up this mapping before we output any assembler but once we are sure
2394 that all symbol renaming is done.
2395
2396 FIXME: All this uglyness can go away if we just do renaming at gimple
2397 level by physically rewritting the IL. At the moment we can only redirect
2398 calls, so we need infrastructure for renaming references as well. */
2399 #ifndef ASM_OUTPUT_WEAKREF
2400 symtab_node *node;
2401
2402 FOR_EACH_SYMBOL (node)
2403 if (node->alias
2404 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2405 {
2406 IDENTIFIER_TRANSPARENT_ALIAS
2407 (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2408 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2409 = (node->alias_target ? node->alias_target
2410 : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
2411 }
2412 #endif
2413
2414 state = EXPANSION;
2415
2416 if (!flag_toplevel_reorder)
2417 output_in_order (false);
2418 else
2419 {
2420 /* Output first asm statements and anything ordered. The process
2421 flag is cleared for these nodes, so we skip them later. */
2422 output_in_order (true);
2423 expand_all_functions ();
2424 output_variables ();
2425 }
2426
2427 process_new_functions ();
2428 state = FINISHED;
2429 output_weakrefs ();
2430
2431 if (dump_file)
2432 {
2433 fprintf (dump_file, "\nFinal ");
2434 symtab_node::dump_table (dump_file);
2435 }
2436 #ifdef ENABLE_CHECKING
2437 symtab_node::verify_symtab_nodes ();
2438 /* Double check that all inline clones are gone and that all
2439 function bodies have been released from memory. */
2440 if (!seen_error ())
2441 {
2442 cgraph_node *node;
2443 bool error_found = false;
2444
2445 FOR_EACH_DEFINED_FUNCTION (node)
2446 if (node->global.inlined_to
2447 || gimple_has_body_p (node->decl))
2448 {
2449 error_found = true;
2450 node->debug ();
2451 }
2452 if (error_found)
2453 internal_error ("nodes with unreleased memory found");
2454 }
2455 #endif
2456 }
2457
2458
2459 /* Analyze the whole compilation unit once it is parsed completely. */
2460
2461 void
2462 symbol_table::finalize_compilation_unit (void)
2463 {
2464 timevar_push (TV_CGRAPH);
2465
2466 /* If we're here there's no current function anymore. Some frontends
2467 are lazy in clearing these. */
2468 current_function_decl = NULL;
2469 set_cfun (NULL);
2470
2471 /* Do not skip analyzing the functions if there were errors, we
2472 miss diagnostics for following functions otherwise. */
2473
2474 /* Emit size functions we didn't inline. */
2475 finalize_size_functions ();
2476
2477 /* Mark alias targets necessary and emit diagnostics. */
2478 handle_alias_pairs ();
2479
2480 if (!quiet_flag)
2481 {
2482 fprintf (stderr, "\nAnalyzing compilation unit\n");
2483 fflush (stderr);
2484 }
2485
2486 if (flag_dump_passes)
2487 dump_passes ();
2488
2489 /* Gimplify and lower all functions, compute reachability and
2490 remove unreachable nodes. */
2491 analyze_functions (/*first_time=*/true);
2492
2493 /* Mark alias targets necessary and emit diagnostics. */
2494 handle_alias_pairs ();
2495
2496 /* Gimplify and lower thunks. */
2497 analyze_functions (/*first_time=*/false);
2498
2499 /* Emit early debug for reachable functions, and by consequence,
2500 locally scoped symbols. */
2501 struct cgraph_node *cnode;
2502 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (cnode)
2503 (*debug_hooks->early_global_decl) (cnode->decl);
2504
2505 /* Clean up anything that needs cleaning up after initial debug
2506 generation. */
2507 (*debug_hooks->early_finish) ();
2508
2509 /* Finally drive the pass manager. */
2510 compile ();
2511
2512 timevar_pop (TV_CGRAPH);
2513 }
2514
2515 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2516 within the same process. For use by toplev::finalize. */
2517
2518 void
2519 cgraphunit_c_finalize (void)
2520 {
2521 gcc_assert (cgraph_new_nodes.length () == 0);
2522 cgraph_new_nodes.truncate (0);
2523
2524 vtable_entry_type = NULL;
2525 queued_nodes = &symtab_terminator;
2526
2527 first_analyzed = NULL;
2528 first_analyzed_var = NULL;
2529 }
2530
2531 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2532 kind of wrapper method. */
2533
2534 void
2535 cgraph_node::create_wrapper (cgraph_node *target)
2536 {
2537 /* Preserve DECL_RESULT so we get right by reference flag. */
2538 tree decl_result = DECL_RESULT (decl);
2539
2540 /* Remove the function's body but keep arguments to be reused
2541 for thunk. */
2542 release_body (true);
2543 reset ();
2544
2545 DECL_UNINLINABLE (decl) = false;
2546 DECL_RESULT (decl) = decl_result;
2547 DECL_INITIAL (decl) = NULL;
2548 allocate_struct_function (decl, false);
2549 set_cfun (NULL);
2550
2551 /* Turn alias into thunk and expand it into GIMPLE representation. */
2552 definition = true;
2553
2554 memset (&thunk, 0, sizeof (cgraph_thunk_info));
2555 thunk.thunk_p = true;
2556 create_edge (target, NULL, count, CGRAPH_FREQ_BASE);
2557
2558 tree arguments = DECL_ARGUMENTS (decl);
2559
2560 while (arguments)
2561 {
2562 TREE_ADDRESSABLE (arguments) = false;
2563 arguments = TREE_CHAIN (arguments);
2564 }
2565
2566 expand_thunk (false, true);
2567
2568 /* Inline summary set-up. */
2569 analyze ();
2570 inline_analyze_function (this);
2571 }
2572
2573 #include "gt-cgraphunit.h"