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