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