cgraphunit.c (cgraph_node::expand_thunk): Don't expand instrumentation thunks.
[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 (method_class_type
870 (TREE_TYPE (targets[i]->decl))))
871 enqueue_node (targets[i]);
872 }
873 }
874
875 /* Very trivial devirtualization; when the type is
876 final or anonymous (so we know all its derivation)
877 and there is only one possible virtual call target,
878 make the edge direct. */
879 if (final)
880 {
881 if (targets.length () <= 1 && dbg_cnt (devirt))
882 {
883 cgraph_node *target;
884 if (targets.length () == 1)
885 target = targets[0];
886 else
887 target = cgraph_node::create
888 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
889
890 if (symtab->dump_file)
891 {
892 fprintf (symtab->dump_file,
893 "Devirtualizing call: ");
894 print_gimple_stmt (symtab->dump_file,
895 edge->call_stmt, 0,
896 TDF_SLIM);
897 }
898 if (dump_enabled_p ())
899 {
900 location_t locus = gimple_location_safe (edge->call_stmt);
901 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
902 "devirtualizing call in %s to %s\n",
903 edge->caller->name (), target->name ());
904 }
905
906 edge->make_direct (target);
907 edge->redirect_call_stmt_to_callee ();
908
909 /* Call to __builtin_unreachable shouldn't be instrumented. */
910 if (!targets.length ())
911 gimple_call_set_with_bounds (edge->call_stmt, false);
912
913 if (symtab->dump_file)
914 {
915 fprintf (symtab->dump_file,
916 "Devirtualized as: ");
917 print_gimple_stmt (symtab->dump_file,
918 edge->call_stmt, 0,
919 TDF_SLIM);
920 }
921 }
922 }
923 }
924
925
926 /* Discover all functions and variables that are trivially needed, analyze
927 them as well as all functions and variables referred by them */
928 static cgraph_node *first_analyzed;
929 static varpool_node *first_analyzed_var;
930
931 static void
932 analyze_functions (void)
933 {
934 /* Keep track of already processed nodes when called multiple times for
935 intermodule optimization. */
936 cgraph_node *first_handled = first_analyzed;
937 varpool_node *first_handled_var = first_analyzed_var;
938 hash_set<void *> reachable_call_targets;
939
940 symtab_node *node;
941 symtab_node *next;
942 int i;
943 ipa_ref *ref;
944 bool changed = true;
945 location_t saved_loc = input_location;
946
947 bitmap_obstack_initialize (NULL);
948 symtab->state = CONSTRUCTION;
949 input_location = UNKNOWN_LOCATION;
950
951 /* Ugly, but the fixup can not happen at a time same body alias is created;
952 C++ FE is confused about the COMDAT groups being right. */
953 if (symtab->cpp_implicit_aliases_done)
954 FOR_EACH_SYMBOL (node)
955 if (node->cpp_implicit_alias)
956 node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
957 build_type_inheritance_graph ();
958
959 /* Analysis adds static variables that in turn adds references to new functions.
960 So we need to iterate the process until it stabilize. */
961 while (changed)
962 {
963 changed = false;
964 process_function_and_variable_attributes (first_analyzed,
965 first_analyzed_var);
966
967 /* First identify the trivially needed symbols. */
968 for (node = symtab->first_symbol ();
969 node != first_analyzed
970 && node != first_analyzed_var; node = node->next)
971 {
972 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
973 node->get_comdat_group_id ();
974 if (node->needed_p ())
975 {
976 enqueue_node (node);
977 if (!changed && symtab->dump_file)
978 fprintf (symtab->dump_file, "Trivially needed symbols:");
979 changed = true;
980 if (symtab->dump_file)
981 fprintf (symtab->dump_file, " %s", node->asm_name ());
982 if (!changed && symtab->dump_file)
983 fprintf (symtab->dump_file, "\n");
984 }
985 if (node == first_analyzed
986 || node == first_analyzed_var)
987 break;
988 }
989 symtab->process_new_functions ();
990 first_analyzed_var = symtab->first_variable ();
991 first_analyzed = symtab->first_function ();
992
993 if (changed && symtab->dump_file)
994 fprintf (symtab->dump_file, "\n");
995
996 /* Lower representation, build callgraph edges and references for all trivially
997 needed symbols and all symbols referred by them. */
998 while (queued_nodes != &symtab_terminator)
999 {
1000 changed = true;
1001 node = queued_nodes;
1002 queued_nodes = (symtab_node *)queued_nodes->aux;
1003 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1004 if (cnode && cnode->definition)
1005 {
1006 cgraph_edge *edge;
1007 tree decl = cnode->decl;
1008
1009 /* ??? It is possible to create extern inline function
1010 and later using weak alias attribute to kill its body.
1011 See gcc.c-torture/compile/20011119-1.c */
1012 if (!DECL_STRUCT_FUNCTION (decl)
1013 && !cnode->alias
1014 && !cnode->thunk.thunk_p
1015 && !cnode->dispatcher_function)
1016 {
1017 cnode->reset ();
1018 cnode->local.redefined_extern_inline = true;
1019 continue;
1020 }
1021
1022 if (!cnode->analyzed)
1023 cnode->analyze ();
1024
1025 for (edge = cnode->callees; edge; edge = edge->next_callee)
1026 if (edge->callee->definition
1027 && (!DECL_EXTERNAL (edge->callee->decl)
1028 /* When not optimizing, do not try to analyze extern
1029 inline functions. Doing so is pointless. */
1030 || opt_for_fn (edge->callee->decl, optimize)
1031 /* Weakrefs needs to be preserved. */
1032 || edge->callee->alias
1033 /* always_inline functions are inlined aven at -O0. */
1034 || lookup_attribute
1035 ("always_inline",
1036 DECL_ATTRIBUTES (edge->callee->decl))
1037 /* Multiversioned functions needs the dispatcher to
1038 be produced locally even for extern functions. */
1039 || edge->callee->function_version ()))
1040 enqueue_node (edge->callee);
1041 if (opt_for_fn (cnode->decl, optimize)
1042 && opt_for_fn (cnode->decl, flag_devirtualize))
1043 {
1044 cgraph_edge *next;
1045
1046 for (edge = cnode->indirect_calls; edge; edge = next)
1047 {
1048 next = edge->next_callee;
1049 if (edge->indirect_info->polymorphic)
1050 walk_polymorphic_call_targets (&reachable_call_targets,
1051 edge);
1052 }
1053 }
1054
1055 /* If decl is a clone of an abstract function,
1056 mark that abstract function so that we don't release its body.
1057 The DECL_INITIAL() of that abstract function declaration
1058 will be later needed to output debug info. */
1059 if (DECL_ABSTRACT_ORIGIN (decl))
1060 {
1061 cgraph_node *origin_node
1062 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl));
1063 origin_node->used_as_abstract_origin = true;
1064 }
1065 }
1066 else
1067 {
1068 varpool_node *vnode = dyn_cast <varpool_node *> (node);
1069 if (vnode && vnode->definition && !vnode->analyzed)
1070 vnode->analyze ();
1071 }
1072
1073 if (node->same_comdat_group)
1074 {
1075 symtab_node *next;
1076 for (next = node->same_comdat_group;
1077 next != node;
1078 next = next->same_comdat_group)
1079 if (!next->comdat_local_p ())
1080 enqueue_node (next);
1081 }
1082 for (i = 0; node->iterate_reference (i, ref); i++)
1083 if (ref->referred->definition
1084 && (!DECL_EXTERNAL (ref->referred->decl)
1085 || ((TREE_CODE (ref->referred->decl) != FUNCTION_DECL
1086 && optimize)
1087 || (TREE_CODE (ref->referred->decl) == FUNCTION_DECL
1088 && opt_for_fn (ref->referred->decl, optimize))
1089 || node->alias
1090 || ref->referred->alias)))
1091 enqueue_node (ref->referred);
1092 symtab->process_new_functions ();
1093 }
1094 }
1095 update_type_inheritance_graph ();
1096
1097 /* Collect entry points to the unit. */
1098 if (symtab->dump_file)
1099 {
1100 fprintf (symtab->dump_file, "\n\nInitial ");
1101 symtab_node::dump_table (symtab->dump_file);
1102 }
1103
1104 if (symtab->dump_file)
1105 fprintf (symtab->dump_file, "\nRemoving unused symbols:");
1106
1107 for (node = symtab->first_symbol ();
1108 node != first_handled
1109 && node != first_handled_var; node = next)
1110 {
1111 next = node->next;
1112 if (!node->aux && !node->referred_to_p ())
1113 {
1114 if (symtab->dump_file)
1115 fprintf (symtab->dump_file, " %s", node->name ());
1116 node->remove ();
1117 continue;
1118 }
1119 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1120 {
1121 tree decl = node->decl;
1122
1123 if (cnode->definition && !gimple_has_body_p (decl)
1124 && !cnode->alias
1125 && !cnode->thunk.thunk_p)
1126 cnode->reset ();
1127
1128 gcc_assert (!cnode->definition || cnode->thunk.thunk_p
1129 || cnode->alias
1130 || gimple_has_body_p (decl));
1131 gcc_assert (cnode->analyzed == cnode->definition);
1132 }
1133 node->aux = NULL;
1134 }
1135 for (;node; node = node->next)
1136 node->aux = NULL;
1137 first_analyzed = symtab->first_function ();
1138 first_analyzed_var = symtab->first_variable ();
1139 if (symtab->dump_file)
1140 {
1141 fprintf (symtab->dump_file, "\n\nReclaimed ");
1142 symtab_node::dump_table (symtab->dump_file);
1143 }
1144 bitmap_obstack_release (NULL);
1145 ggc_collect ();
1146 /* Initialize assembler name hash, in particular we want to trigger C++
1147 mangling and same body alias creation before we free DECL_ARGUMENTS
1148 used by it. */
1149 if (!seen_error ())
1150 symtab->symtab_initialize_asm_name_hash ();
1151
1152 input_location = saved_loc;
1153 }
1154
1155 /* Translate the ugly representation of aliases as alias pairs into nice
1156 representation in callgraph. We don't handle all cases yet,
1157 unfortunately. */
1158
1159 static void
1160 handle_alias_pairs (void)
1161 {
1162 alias_pair *p;
1163 unsigned i;
1164
1165 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1166 {
1167 symtab_node *target_node = symtab_node::get_for_asmname (p->target);
1168
1169 /* Weakrefs with target not defined in current unit are easy to handle:
1170 they behave just as external variables except we need to note the
1171 alias flag to later output the weakref pseudo op into asm file. */
1172 if (!target_node
1173 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1174 {
1175 symtab_node *node = symtab_node::get (p->decl);
1176 if (node)
1177 {
1178 node->alias_target = p->target;
1179 node->weakref = true;
1180 node->alias = true;
1181 }
1182 alias_pairs->unordered_remove (i);
1183 continue;
1184 }
1185 else if (!target_node)
1186 {
1187 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1188 symtab_node *node = symtab_node::get (p->decl);
1189 if (node)
1190 node->alias = false;
1191 alias_pairs->unordered_remove (i);
1192 continue;
1193 }
1194
1195 if (DECL_EXTERNAL (target_node->decl)
1196 /* We use local aliases for C++ thunks to force the tailcall
1197 to bind locally. This is a hack - to keep it working do
1198 the following (which is not strictly correct). */
1199 && (TREE_CODE (target_node->decl) != FUNCTION_DECL
1200 || ! DECL_VIRTUAL_P (target_node->decl))
1201 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1202 {
1203 error ("%q+D aliased to external symbol %qE",
1204 p->decl, p->target);
1205 }
1206
1207 if (TREE_CODE (p->decl) == FUNCTION_DECL
1208 && target_node && is_a <cgraph_node *> (target_node))
1209 {
1210 cgraph_node *src_node = cgraph_node::get (p->decl);
1211 if (src_node && src_node->definition)
1212 src_node->reset ();
1213 cgraph_node::create_alias (p->decl, target_node->decl);
1214 alias_pairs->unordered_remove (i);
1215 }
1216 else if (TREE_CODE (p->decl) == VAR_DECL
1217 && target_node && is_a <varpool_node *> (target_node))
1218 {
1219 varpool_node::create_alias (p->decl, target_node->decl);
1220 alias_pairs->unordered_remove (i);
1221 }
1222 else
1223 {
1224 error ("%q+D alias in between function and variable is not supported",
1225 p->decl);
1226 warning (0, "%q+D aliased declaration",
1227 target_node->decl);
1228 alias_pairs->unordered_remove (i);
1229 }
1230 }
1231 vec_free (alias_pairs);
1232 }
1233
1234
1235 /* Figure out what functions we want to assemble. */
1236
1237 static void
1238 mark_functions_to_output (void)
1239 {
1240 cgraph_node *node;
1241 #ifdef ENABLE_CHECKING
1242 bool check_same_comdat_groups = false;
1243
1244 FOR_EACH_FUNCTION (node)
1245 gcc_assert (!node->process);
1246 #endif
1247
1248 FOR_EACH_FUNCTION (node)
1249 {
1250 tree decl = node->decl;
1251
1252 gcc_assert (!node->process || node->same_comdat_group);
1253 if (node->process)
1254 continue;
1255
1256 /* We need to output all local functions that are used and not
1257 always inlined, as well as those that are reachable from
1258 outside the current compilation unit. */
1259 if (node->analyzed
1260 && !node->thunk.thunk_p
1261 && !node->alias
1262 && !node->global.inlined_to
1263 && !TREE_ASM_WRITTEN (decl)
1264 && !DECL_EXTERNAL (decl))
1265 {
1266 node->process = 1;
1267 if (node->same_comdat_group)
1268 {
1269 cgraph_node *next;
1270 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
1271 next != node;
1272 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
1273 if (!next->thunk.thunk_p && !next->alias
1274 && !next->comdat_local_p ())
1275 next->process = 1;
1276 }
1277 }
1278 else if (node->same_comdat_group)
1279 {
1280 #ifdef ENABLE_CHECKING
1281 check_same_comdat_groups = true;
1282 #endif
1283 }
1284 else
1285 {
1286 /* We should've reclaimed all functions that are not needed. */
1287 #ifdef ENABLE_CHECKING
1288 if (!node->global.inlined_to
1289 && gimple_has_body_p (decl)
1290 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1291 are inside partition, we can end up not removing the body since we no longer
1292 have analyzed node pointing to it. */
1293 && !node->in_other_partition
1294 && !node->alias
1295 && !node->clones
1296 && !DECL_EXTERNAL (decl))
1297 {
1298 node->debug ();
1299 internal_error ("failed to reclaim unneeded function");
1300 }
1301 #endif
1302 gcc_assert (node->global.inlined_to
1303 || !gimple_has_body_p (decl)
1304 || node->in_other_partition
1305 || node->clones
1306 || DECL_ARTIFICIAL (decl)
1307 || DECL_EXTERNAL (decl));
1308
1309 }
1310
1311 }
1312 #ifdef ENABLE_CHECKING
1313 if (check_same_comdat_groups)
1314 FOR_EACH_FUNCTION (node)
1315 if (node->same_comdat_group && !node->process)
1316 {
1317 tree decl = node->decl;
1318 if (!node->global.inlined_to
1319 && gimple_has_body_p (decl)
1320 /* FIXME: in an ltrans unit when the offline copy is outside a
1321 partition but inline copies are inside a partition, we can
1322 end up not removing the body since we no longer have an
1323 analyzed node pointing to it. */
1324 && !node->in_other_partition
1325 && !node->clones
1326 && !DECL_EXTERNAL (decl))
1327 {
1328 node->debug ();
1329 internal_error ("failed to reclaim unneeded function in same "
1330 "comdat group");
1331 }
1332 }
1333 #endif
1334 }
1335
1336 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1337 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1338
1339 Set current_function_decl and cfun to newly constructed empty function body.
1340 return basic block in the function body. */
1341
1342 basic_block
1343 init_lowered_empty_function (tree decl, bool in_ssa, gcov_type count)
1344 {
1345 basic_block bb;
1346 edge e;
1347
1348 current_function_decl = decl;
1349 allocate_struct_function (decl, false);
1350 gimple_register_cfg_hooks ();
1351 init_empty_tree_cfg ();
1352
1353 if (in_ssa)
1354 {
1355 init_tree_ssa (cfun);
1356 init_ssa_operands (cfun);
1357 cfun->gimple_df->in_ssa_p = true;
1358 cfun->curr_properties |= PROP_ssa;
1359 }
1360
1361 DECL_INITIAL (decl) = make_node (BLOCK);
1362
1363 DECL_SAVED_TREE (decl) = error_mark_node;
1364 cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1365 | PROP_cfg | PROP_loops);
1366
1367 set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ());
1368 init_loops_structure (cfun, loops_for_fn (cfun), 1);
1369 loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1370
1371 /* Create BB for body of the function and connect it properly. */
1372 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = count;
1373 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency = REG_BR_PROB_BASE;
1374 EXIT_BLOCK_PTR_FOR_FN (cfun)->count = count;
1375 EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency = REG_BR_PROB_BASE;
1376 bb = create_basic_block (NULL, (void *) 0, ENTRY_BLOCK_PTR_FOR_FN (cfun));
1377 bb->count = count;
1378 bb->frequency = BB_FREQ_MAX;
1379 e = make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU);
1380 e->count = count;
1381 e->probability = REG_BR_PROB_BASE;
1382 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1383 e->count = count;
1384 e->probability = REG_BR_PROB_BASE;
1385 add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
1386
1387 return bb;
1388 }
1389
1390 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1391 offset indicated by VIRTUAL_OFFSET, if that is
1392 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1393 zero for a result adjusting thunk. */
1394
1395 static tree
1396 thunk_adjust (gimple_stmt_iterator * bsi,
1397 tree ptr, bool this_adjusting,
1398 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1399 {
1400 gassign *stmt;
1401 tree ret;
1402
1403 if (this_adjusting
1404 && fixed_offset != 0)
1405 {
1406 stmt = gimple_build_assign
1407 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1408 ptr,
1409 fixed_offset));
1410 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1411 }
1412
1413 /* If there's a virtual offset, look up that value in the vtable and
1414 adjust the pointer again. */
1415 if (virtual_offset)
1416 {
1417 tree vtabletmp;
1418 tree vtabletmp2;
1419 tree vtabletmp3;
1420
1421 if (!vtable_entry_type)
1422 {
1423 tree vfunc_type = make_node (FUNCTION_TYPE);
1424 TREE_TYPE (vfunc_type) = integer_type_node;
1425 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1426 layout_type (vfunc_type);
1427
1428 vtable_entry_type = build_pointer_type (vfunc_type);
1429 }
1430
1431 vtabletmp =
1432 create_tmp_reg (build_pointer_type
1433 (build_pointer_type (vtable_entry_type)), "vptr");
1434
1435 /* The vptr is always at offset zero in the object. */
1436 stmt = gimple_build_assign (vtabletmp,
1437 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1438 ptr));
1439 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1440
1441 /* Form the vtable address. */
1442 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
1443 "vtableaddr");
1444 stmt = gimple_build_assign (vtabletmp2,
1445 build_simple_mem_ref (vtabletmp));
1446 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1447
1448 /* Find the entry with the vcall offset. */
1449 stmt = gimple_build_assign (vtabletmp2,
1450 fold_build_pointer_plus_loc (input_location,
1451 vtabletmp2,
1452 virtual_offset));
1453 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1454
1455 /* Get the offset itself. */
1456 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1457 "vcalloffset");
1458 stmt = gimple_build_assign (vtabletmp3,
1459 build_simple_mem_ref (vtabletmp2));
1460 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1461
1462 /* Adjust the `this' pointer. */
1463 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1464 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1465 GSI_CONTINUE_LINKING);
1466 }
1467
1468 if (!this_adjusting
1469 && fixed_offset != 0)
1470 /* Adjust the pointer by the constant. */
1471 {
1472 tree ptrtmp;
1473
1474 if (TREE_CODE (ptr) == VAR_DECL)
1475 ptrtmp = ptr;
1476 else
1477 {
1478 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
1479 stmt = gimple_build_assign (ptrtmp, ptr);
1480 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1481 }
1482 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1483 ptrtmp, fixed_offset);
1484 }
1485
1486 /* Emit the statement and gimplify the adjustment expression. */
1487 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
1488 stmt = gimple_build_assign (ret, ptr);
1489 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1490
1491 return ret;
1492 }
1493
1494 /* Expand thunk NODE to gimple if possible.
1495 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1496 no assembler is produced.
1497 When OUTPUT_ASM_THUNK is true, also produce assembler for
1498 thunks that are not lowered. */
1499
1500 bool
1501 cgraph_node::expand_thunk (bool output_asm_thunks, bool force_gimple_thunk)
1502 {
1503 bool this_adjusting = thunk.this_adjusting;
1504 HOST_WIDE_INT fixed_offset = thunk.fixed_offset;
1505 HOST_WIDE_INT virtual_value = thunk.virtual_value;
1506 tree virtual_offset = NULL;
1507 tree alias = callees->callee->decl;
1508 tree thunk_fndecl = decl;
1509 tree a;
1510
1511 /* Instrumentation thunk is the same function with
1512 a different signature. Never need to expand it. */
1513 if (thunk.add_pointer_bounds_args)
1514 return false;
1515
1516 if (!force_gimple_thunk && this_adjusting
1517 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1518 virtual_value, alias))
1519 {
1520 const char *fnname;
1521 tree fn_block;
1522 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1523
1524 if (!output_asm_thunks)
1525 {
1526 analyzed = true;
1527 return false;
1528 }
1529
1530 if (in_lto_p)
1531 get_untransformed_body ();
1532 a = DECL_ARGUMENTS (thunk_fndecl);
1533
1534 current_function_decl = thunk_fndecl;
1535
1536 /* Ensure thunks are emitted in their correct sections. */
1537 resolve_unique_section (thunk_fndecl, 0,
1538 flag_function_sections);
1539
1540 DECL_RESULT (thunk_fndecl)
1541 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1542 RESULT_DECL, 0, restype);
1543 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1544 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
1545
1546 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1547 create one. */
1548 fn_block = make_node (BLOCK);
1549 BLOCK_VARS (fn_block) = a;
1550 DECL_INITIAL (thunk_fndecl) = fn_block;
1551 init_function_start (thunk_fndecl);
1552 cfun->is_thunk = 1;
1553 insn_locations_init ();
1554 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1555 prologue_location = curr_insn_location ();
1556 assemble_start_function (thunk_fndecl, fnname);
1557
1558 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1559 fixed_offset, virtual_value, alias);
1560
1561 assemble_end_function (thunk_fndecl, fnname);
1562 insn_locations_finalize ();
1563 init_insn_lengths ();
1564 free_after_compilation (cfun);
1565 set_cfun (NULL);
1566 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1567 thunk.thunk_p = false;
1568 analyzed = false;
1569 }
1570 else if (stdarg_p (TREE_TYPE (thunk_fndecl)))
1571 {
1572 error ("generic thunk code fails for method %qD which uses %<...%>",
1573 thunk_fndecl);
1574 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1575 analyzed = true;
1576 return false;
1577 }
1578 else
1579 {
1580 tree restype;
1581 basic_block bb, then_bb, else_bb, return_bb;
1582 gimple_stmt_iterator bsi;
1583 int nargs = 0;
1584 tree arg;
1585 int i;
1586 tree resdecl;
1587 tree restmp = 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 (restmp && !this_adjusting
1705 && (fixed_offset || virtual_offset))
1706 {
1707 tree true_label = NULL_TREE;
1708
1709 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1710 {
1711 gimple stmt;
1712 edge e;
1713 /* If the return type is a pointer, we need to
1714 protect against NULL. We know there will be an
1715 adjustment, because that's why we're emitting a
1716 thunk. */
1717 then_bb = create_basic_block (NULL, (void *) 0, bb);
1718 then_bb->count = count - count / 16;
1719 then_bb->frequency = BB_FREQ_MAX - BB_FREQ_MAX / 16;
1720 return_bb = create_basic_block (NULL, (void *) 0, then_bb);
1721 return_bb->count = count;
1722 return_bb->frequency = BB_FREQ_MAX;
1723 else_bb = create_basic_block (NULL, (void *) 0, else_bb);
1724 then_bb->count = count / 16;
1725 then_bb->frequency = BB_FREQ_MAX / 16;
1726 add_bb_to_loop (then_bb, bb->loop_father);
1727 add_bb_to_loop (return_bb, bb->loop_father);
1728 add_bb_to_loop (else_bb, bb->loop_father);
1729 remove_edge (single_succ_edge (bb));
1730 true_label = gimple_block_label (then_bb);
1731 stmt = gimple_build_cond (NE_EXPR, restmp,
1732 build_zero_cst (TREE_TYPE (restmp)),
1733 NULL_TREE, NULL_TREE);
1734 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1735 e = make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1736 e->probability = REG_BR_PROB_BASE - REG_BR_PROB_BASE / 16;
1737 e->count = count - count / 16;
1738 e = make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1739 e->probability = REG_BR_PROB_BASE / 16;
1740 e->count = count / 16;
1741 e = make_edge (return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1742 e->probability = REG_BR_PROB_BASE;
1743 e->count = count;
1744 e = make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1745 e->probability = REG_BR_PROB_BASE;
1746 e->count = count - count / 16;
1747 e = make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1748 e->probability = REG_BR_PROB_BASE;
1749 e->count = count / 16;
1750 bsi = gsi_last_bb (then_bb);
1751 }
1752
1753 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1754 fixed_offset, virtual_offset);
1755 if (true_label)
1756 {
1757 gimple stmt;
1758 bsi = gsi_last_bb (else_bb);
1759 stmt = gimple_build_assign (restmp,
1760 build_zero_cst (TREE_TYPE (restmp)));
1761 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1762 bsi = gsi_last_bb (return_bb);
1763 }
1764 }
1765 else
1766 gimple_call_set_tail (call, true);
1767
1768 /* Build return value. */
1769 if (!DECL_BY_REFERENCE (resdecl))
1770 ret = gimple_build_return (restmp);
1771 else
1772 ret = gimple_build_return (resdecl);
1773
1774 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1775 }
1776 else
1777 {
1778 gimple_call_set_tail (call, true);
1779 remove_edge (single_succ_edge (bb));
1780 }
1781
1782 cfun->gimple_df->in_ssa_p = true;
1783 profile_status_for_fn (cfun)
1784 = count ? PROFILE_READ : PROFILE_GUESSED;
1785 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
1786 TREE_ASM_WRITTEN (thunk_fndecl) = false;
1787 delete_unreachable_blocks ();
1788 update_ssa (TODO_update_ssa);
1789 #ifdef ENABLE_CHECKING
1790 verify_flow_info ();
1791 #endif
1792 free_dominance_info (CDI_DOMINATORS);
1793
1794 /* Since we want to emit the thunk, we explicitly mark its name as
1795 referenced. */
1796 thunk.thunk_p = false;
1797 lowered = true;
1798 bitmap_obstack_release (NULL);
1799 }
1800 current_function_decl = NULL;
1801 set_cfun (NULL);
1802 return true;
1803 }
1804
1805 /* Assemble thunks and aliases associated to node. */
1806
1807 void
1808 cgraph_node::assemble_thunks_and_aliases (void)
1809 {
1810 cgraph_edge *e;
1811 ipa_ref *ref;
1812
1813 for (e = callers; e;)
1814 if (e->caller->thunk.thunk_p
1815 && !e->caller->thunk.add_pointer_bounds_args)
1816 {
1817 cgraph_node *thunk = e->caller;
1818
1819 e = e->next_caller;
1820 thunk->expand_thunk (true, false);
1821 thunk->assemble_thunks_and_aliases ();
1822 }
1823 else
1824 e = e->next_caller;
1825
1826 FOR_EACH_ALIAS (this, ref)
1827 {
1828 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1829 bool saved_written = TREE_ASM_WRITTEN (decl);
1830
1831 /* Force assemble_alias to really output the alias this time instead
1832 of buffering it in same alias pairs. */
1833 TREE_ASM_WRITTEN (decl) = 1;
1834 do_assemble_alias (alias->decl,
1835 DECL_ASSEMBLER_NAME (decl));
1836 alias->assemble_thunks_and_aliases ();
1837 TREE_ASM_WRITTEN (decl) = saved_written;
1838 }
1839 }
1840
1841 /* Expand function specified by node. */
1842
1843 void
1844 cgraph_node::expand (void)
1845 {
1846 location_t saved_loc;
1847
1848 /* We ought to not compile any inline clones. */
1849 gcc_assert (!global.inlined_to);
1850
1851 announce_function (decl);
1852 process = 0;
1853 gcc_assert (lowered);
1854 get_untransformed_body ();
1855
1856 /* Generate RTL for the body of DECL. */
1857
1858 timevar_push (TV_REST_OF_COMPILATION);
1859
1860 gcc_assert (symtab->global_info_ready);
1861
1862 /* Initialize the default bitmap obstack. */
1863 bitmap_obstack_initialize (NULL);
1864
1865 /* Initialize the RTL code for the function. */
1866 current_function_decl = decl;
1867 saved_loc = input_location;
1868 input_location = DECL_SOURCE_LOCATION (decl);
1869 init_function_start (decl);
1870
1871 gimple_register_cfg_hooks ();
1872
1873 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1874
1875 execute_all_ipa_transforms ();
1876
1877 /* Perform all tree transforms and optimizations. */
1878
1879 /* Signal the start of passes. */
1880 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1881
1882 execute_pass_list (cfun, g->get_passes ()->all_passes);
1883
1884 /* Signal the end of passes. */
1885 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1886
1887 bitmap_obstack_release (&reg_obstack);
1888
1889 /* Release the default bitmap obstack. */
1890 bitmap_obstack_release (NULL);
1891
1892 /* If requested, warn about function definitions where the function will
1893 return a value (usually of some struct or union type) which itself will
1894 take up a lot of stack space. */
1895 if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl))
1896 {
1897 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
1898
1899 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1900 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1901 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1902 larger_than_size))
1903 {
1904 unsigned int size_as_int
1905 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1906
1907 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1908 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
1909 decl, size_as_int);
1910 else
1911 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
1912 decl, larger_than_size);
1913 }
1914 }
1915
1916 gimple_set_body (decl, NULL);
1917 if (DECL_STRUCT_FUNCTION (decl) == 0
1918 && !cgraph_node::get (decl)->origin)
1919 {
1920 /* Stop pointing to the local nodes about to be freed.
1921 But DECL_INITIAL must remain nonzero so we know this
1922 was an actual function definition.
1923 For a nested function, this is done in c_pop_function_context.
1924 If rest_of_compilation set this to 0, leave it 0. */
1925 if (DECL_INITIAL (decl) != 0)
1926 DECL_INITIAL (decl) = error_mark_node;
1927 }
1928
1929 input_location = saved_loc;
1930
1931 ggc_collect ();
1932 timevar_pop (TV_REST_OF_COMPILATION);
1933
1934 /* Make sure that BE didn't give up on compiling. */
1935 gcc_assert (TREE_ASM_WRITTEN (decl));
1936 set_cfun (NULL);
1937 current_function_decl = NULL;
1938
1939 /* It would make a lot more sense to output thunks before function body to get more
1940 forward and lest backwarding jumps. This however would need solving problem
1941 with comdats. See PR48668. Also aliases must come after function itself to
1942 make one pass assemblers, like one on AIX, happy. See PR 50689.
1943 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1944 groups. */
1945 assemble_thunks_and_aliases ();
1946 release_body ();
1947 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1948 points to the dead function body. */
1949 remove_callees ();
1950 remove_all_references ();
1951 }
1952
1953 /* Node comparer that is responsible for the order that corresponds
1954 to time when a function was launched for the first time. */
1955
1956 static int
1957 node_cmp (const void *pa, const void *pb)
1958 {
1959 const cgraph_node *a = *(const cgraph_node * const *) pa;
1960 const cgraph_node *b = *(const cgraph_node * const *) pb;
1961
1962 /* Functions with time profile must be before these without profile. */
1963 if (!a->tp_first_run || !b->tp_first_run)
1964 return a->tp_first_run - b->tp_first_run;
1965
1966 return a->tp_first_run != b->tp_first_run
1967 ? b->tp_first_run - a->tp_first_run
1968 : b->order - a->order;
1969 }
1970
1971 /* Expand all functions that must be output.
1972
1973 Attempt to topologically sort the nodes so function is output when
1974 all called functions are already assembled to allow data to be
1975 propagated across the callgraph. Use a stack to get smaller distance
1976 between a function and its callees (later we may choose to use a more
1977 sophisticated algorithm for function reordering; we will likely want
1978 to use subsections to make the output functions appear in top-down
1979 order). */
1980
1981 static void
1982 expand_all_functions (void)
1983 {
1984 cgraph_node *node;
1985 cgraph_node **order = XCNEWVEC (cgraph_node *,
1986 symtab->cgraph_count);
1987 unsigned int expanded_func_count = 0, profiled_func_count = 0;
1988 int order_pos, new_order_pos = 0;
1989 int i;
1990
1991 order_pos = ipa_reverse_postorder (order);
1992 gcc_assert (order_pos == symtab->cgraph_count);
1993
1994 /* Garbage collector may remove inline clones we eliminate during
1995 optimization. So we must be sure to not reference them. */
1996 for (i = 0; i < order_pos; i++)
1997 if (order[i]->process)
1998 order[new_order_pos++] = order[i];
1999
2000 if (flag_profile_reorder_functions)
2001 qsort (order, new_order_pos, sizeof (cgraph_node *), node_cmp);
2002
2003 for (i = new_order_pos - 1; i >= 0; i--)
2004 {
2005 node = order[i];
2006
2007 if (node->process)
2008 {
2009 expanded_func_count++;
2010 if(node->tp_first_run)
2011 profiled_func_count++;
2012
2013 if (symtab->dump_file)
2014 fprintf (symtab->dump_file,
2015 "Time profile order in expand_all_functions:%s:%d\n",
2016 node->asm_name (), node->tp_first_run);
2017 node->process = 0;
2018 node->expand ();
2019 }
2020 }
2021
2022 if (dump_file)
2023 fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
2024 main_input_filename, profiled_func_count, expanded_func_count);
2025
2026 if (symtab->dump_file && flag_profile_reorder_functions)
2027 fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n",
2028 profiled_func_count, expanded_func_count);
2029
2030 symtab->process_new_functions ();
2031 free_gimplify_stack ();
2032
2033 free (order);
2034 }
2035
2036 /* This is used to sort the node types by the cgraph order number. */
2037
2038 enum cgraph_order_sort_kind
2039 {
2040 ORDER_UNDEFINED = 0,
2041 ORDER_FUNCTION,
2042 ORDER_VAR,
2043 ORDER_ASM
2044 };
2045
2046 struct cgraph_order_sort
2047 {
2048 enum cgraph_order_sort_kind kind;
2049 union
2050 {
2051 cgraph_node *f;
2052 varpool_node *v;
2053 asm_node *a;
2054 } u;
2055 };
2056
2057 /* Output all functions, variables, and asm statements in the order
2058 according to their order fields, which is the order in which they
2059 appeared in the file. This implements -fno-toplevel-reorder. In
2060 this mode we may output functions and variables which don't really
2061 need to be output.
2062 When NO_REORDER is true only do this for symbols marked no reorder. */
2063
2064 static void
2065 output_in_order (bool no_reorder)
2066 {
2067 int max;
2068 cgraph_order_sort *nodes;
2069 int i;
2070 cgraph_node *pf;
2071 varpool_node *pv;
2072 asm_node *pa;
2073 max = symtab->order;
2074 nodes = XCNEWVEC (cgraph_order_sort, max);
2075
2076 FOR_EACH_DEFINED_FUNCTION (pf)
2077 {
2078 if (pf->process && !pf->thunk.thunk_p && !pf->alias)
2079 {
2080 if (no_reorder && !pf->no_reorder)
2081 continue;
2082 i = pf->order;
2083 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2084 nodes[i].kind = ORDER_FUNCTION;
2085 nodes[i].u.f = pf;
2086 }
2087 }
2088
2089 FOR_EACH_DEFINED_VARIABLE (pv)
2090 if (!DECL_EXTERNAL (pv->decl))
2091 {
2092 if (no_reorder && !pv->no_reorder)
2093 continue;
2094 i = pv->order;
2095 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2096 nodes[i].kind = ORDER_VAR;
2097 nodes[i].u.v = pv;
2098 }
2099
2100 for (pa = symtab->first_asm_symbol (); pa; pa = pa->next)
2101 {
2102 i = pa->order;
2103 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2104 nodes[i].kind = ORDER_ASM;
2105 nodes[i].u.a = pa;
2106 }
2107
2108 /* In toplevel reorder mode we output all statics; mark them as needed. */
2109
2110 for (i = 0; i < max; ++i)
2111 if (nodes[i].kind == ORDER_VAR)
2112 nodes[i].u.v->finalize_named_section_flags ();
2113
2114 for (i = 0; i < max; ++i)
2115 {
2116 switch (nodes[i].kind)
2117 {
2118 case ORDER_FUNCTION:
2119 nodes[i].u.f->process = 0;
2120 nodes[i].u.f->expand ();
2121 break;
2122
2123 case ORDER_VAR:
2124 nodes[i].u.v->assemble_decl ();
2125 break;
2126
2127 case ORDER_ASM:
2128 assemble_asm (nodes[i].u.a->asm_str);
2129 break;
2130
2131 case ORDER_UNDEFINED:
2132 break;
2133
2134 default:
2135 gcc_unreachable ();
2136 }
2137 }
2138
2139 symtab->clear_asm_symbols ();
2140
2141 free (nodes);
2142 }
2143
2144 static void
2145 ipa_passes (void)
2146 {
2147 gcc::pass_manager *passes = g->get_passes ();
2148
2149 set_cfun (NULL);
2150 current_function_decl = NULL;
2151 gimple_register_cfg_hooks ();
2152 bitmap_obstack_initialize (NULL);
2153
2154 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2155
2156 if (!in_lto_p)
2157 {
2158 execute_ipa_pass_list (passes->all_small_ipa_passes);
2159 if (seen_error ())
2160 return;
2161 }
2162
2163 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2164 devirtualization and other changes where removal iterate. */
2165 symtab->remove_unreachable_nodes (symtab->dump_file);
2166
2167 /* If pass_all_early_optimizations was not scheduled, the state of
2168 the cgraph will not be properly updated. Update it now. */
2169 if (symtab->state < IPA_SSA)
2170 symtab->state = IPA_SSA;
2171
2172 if (!in_lto_p)
2173 {
2174 /* Generate coverage variables and constructors. */
2175 coverage_finish ();
2176
2177 /* Process new functions added. */
2178 set_cfun (NULL);
2179 current_function_decl = NULL;
2180 symtab->process_new_functions ();
2181
2182 execute_ipa_summary_passes
2183 ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2184 }
2185
2186 /* Some targets need to handle LTO assembler output specially. */
2187 if (flag_generate_lto || flag_generate_offload)
2188 targetm.asm_out.lto_start ();
2189
2190 if (!in_lto_p)
2191 {
2192 if (g->have_offload)
2193 {
2194 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2195 lto_stream_offload_p = true;
2196 ipa_write_summaries ();
2197 lto_stream_offload_p = false;
2198 }
2199 if (flag_lto)
2200 {
2201 section_name_prefix = LTO_SECTION_NAME_PREFIX;
2202 lto_stream_offload_p = false;
2203 ipa_write_summaries ();
2204 }
2205 }
2206
2207 if (flag_generate_lto || flag_generate_offload)
2208 targetm.asm_out.lto_end ();
2209
2210 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
2211 execute_ipa_pass_list (passes->all_regular_ipa_passes);
2212 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2213
2214 bitmap_obstack_release (NULL);
2215 }
2216
2217
2218 /* Return string alias is alias of. */
2219
2220 static tree
2221 get_alias_symbol (tree decl)
2222 {
2223 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
2224 return get_identifier (TREE_STRING_POINTER
2225 (TREE_VALUE (TREE_VALUE (alias))));
2226 }
2227
2228
2229 /* Weakrefs may be associated to external decls and thus not output
2230 at expansion time. Emit all necessary aliases. */
2231
2232 void
2233 symbol_table::output_weakrefs (void)
2234 {
2235 symtab_node *node;
2236 cgraph_node *cnode;
2237 FOR_EACH_SYMBOL (node)
2238 if (node->alias
2239 && !TREE_ASM_WRITTEN (node->decl)
2240 && (!(cnode = dyn_cast <cgraph_node *> (node))
2241 || !cnode->instrumented_version
2242 || !TREE_ASM_WRITTEN (cnode->instrumented_version->decl))
2243 && node->weakref)
2244 {
2245 tree target;
2246
2247 /* Weakrefs are special by not requiring target definition in current
2248 compilation unit. It is thus bit hard to work out what we want to
2249 alias.
2250 When alias target is defined, we need to fetch it from symtab reference,
2251 otherwise it is pointed to by alias_target. */
2252 if (node->alias_target)
2253 target = (DECL_P (node->alias_target)
2254 ? DECL_ASSEMBLER_NAME (node->alias_target)
2255 : node->alias_target);
2256 else if (node->analyzed)
2257 target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
2258 else
2259 {
2260 gcc_unreachable ();
2261 target = get_alias_symbol (node->decl);
2262 }
2263 do_assemble_alias (node->decl, target);
2264 }
2265 }
2266
2267 /* Perform simple optimizations based on callgraph. */
2268
2269 void
2270 symbol_table::compile (void)
2271 {
2272 if (seen_error ())
2273 return;
2274
2275 #ifdef ENABLE_CHECKING
2276 symtab_node::verify_symtab_nodes ();
2277 #endif
2278
2279 timevar_push (TV_CGRAPHOPT);
2280 if (pre_ipa_mem_report)
2281 {
2282 fprintf (stderr, "Memory consumption before IPA\n");
2283 dump_memory_report (false);
2284 }
2285 if (!quiet_flag)
2286 fprintf (stderr, "Performing interprocedural optimizations\n");
2287 state = IPA;
2288
2289 /* Offloading requires LTO infrastructure. */
2290 if (!in_lto_p && g->have_offload)
2291 flag_generate_offload = 1;
2292
2293 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2294 if (flag_generate_lto || flag_generate_offload)
2295 lto_streamer_hooks_init ();
2296
2297 /* Don't run the IPA passes if there was any error or sorry messages. */
2298 if (!seen_error ())
2299 ipa_passes ();
2300
2301 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2302 if (seen_error ()
2303 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
2304 {
2305 timevar_pop (TV_CGRAPHOPT);
2306 return;
2307 }
2308
2309 global_info_ready = true;
2310 if (dump_file)
2311 {
2312 fprintf (dump_file, "Optimized ");
2313 symtab_node:: dump_table (dump_file);
2314 }
2315 if (post_ipa_mem_report)
2316 {
2317 fprintf (stderr, "Memory consumption after IPA\n");
2318 dump_memory_report (false);
2319 }
2320 timevar_pop (TV_CGRAPHOPT);
2321
2322 /* Output everything. */
2323 (*debug_hooks->assembly_start) ();
2324 if (!quiet_flag)
2325 fprintf (stderr, "Assembling functions:\n");
2326 #ifdef ENABLE_CHECKING
2327 symtab_node::verify_symtab_nodes ();
2328 #endif
2329
2330 materialize_all_clones ();
2331 bitmap_obstack_initialize (NULL);
2332 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2333 bitmap_obstack_release (NULL);
2334 mark_functions_to_output ();
2335
2336 /* When weakref support is missing, we autmatically translate all
2337 references to NODE to references to its ultimate alias target.
2338 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2339 TREE_CHAIN.
2340
2341 Set up this mapping before we output any assembler but once we are sure
2342 that all symbol renaming is done.
2343
2344 FIXME: All this uglyness can go away if we just do renaming at gimple
2345 level by physically rewritting the IL. At the moment we can only redirect
2346 calls, so we need infrastructure for renaming references as well. */
2347 #ifndef ASM_OUTPUT_WEAKREF
2348 symtab_node *node;
2349
2350 FOR_EACH_SYMBOL (node)
2351 if (node->alias
2352 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2353 {
2354 IDENTIFIER_TRANSPARENT_ALIAS
2355 (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2356 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2357 = (node->alias_target ? node->alias_target
2358 : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
2359 }
2360 #endif
2361
2362 state = EXPANSION;
2363
2364 if (!flag_toplevel_reorder)
2365 output_in_order (false);
2366 else
2367 {
2368 /* Output first asm statements and anything ordered. The process
2369 flag is cleared for these nodes, so we skip them later. */
2370 output_in_order (true);
2371 expand_all_functions ();
2372 output_variables ();
2373 }
2374
2375 process_new_functions ();
2376 state = FINISHED;
2377 output_weakrefs ();
2378
2379 if (dump_file)
2380 {
2381 fprintf (dump_file, "\nFinal ");
2382 symtab_node::dump_table (dump_file);
2383 }
2384 #ifdef ENABLE_CHECKING
2385 symtab_node::verify_symtab_nodes ();
2386 /* Double check that all inline clones are gone and that all
2387 function bodies have been released from memory. */
2388 if (!seen_error ())
2389 {
2390 cgraph_node *node;
2391 bool error_found = false;
2392
2393 FOR_EACH_DEFINED_FUNCTION (node)
2394 if (node->global.inlined_to
2395 || gimple_has_body_p (node->decl))
2396 {
2397 error_found = true;
2398 node->debug ();
2399 }
2400 if (error_found)
2401 internal_error ("nodes with unreleased memory found");
2402 }
2403 #endif
2404 }
2405
2406
2407 /* Analyze the whole compilation unit once it is parsed completely. */
2408
2409 void
2410 symbol_table::finalize_compilation_unit (void)
2411 {
2412 timevar_push (TV_CGRAPH);
2413
2414 /* If we're here there's no current function anymore. Some frontends
2415 are lazy in clearing these. */
2416 current_function_decl = NULL;
2417 set_cfun (NULL);
2418
2419 /* Do not skip analyzing the functions if there were errors, we
2420 miss diagnostics for following functions otherwise. */
2421
2422 /* Emit size functions we didn't inline. */
2423 finalize_size_functions ();
2424
2425 /* Mark alias targets necessary and emit diagnostics. */
2426 handle_alias_pairs ();
2427
2428 if (!quiet_flag)
2429 {
2430 fprintf (stderr, "\nAnalyzing compilation unit\n");
2431 fflush (stderr);
2432 }
2433
2434 if (flag_dump_passes)
2435 dump_passes ();
2436
2437 /* Gimplify and lower all functions, compute reachability and
2438 remove unreachable nodes. */
2439 analyze_functions ();
2440
2441 /* Mark alias targets necessary and emit diagnostics. */
2442 handle_alias_pairs ();
2443
2444 /* Gimplify and lower thunks. */
2445 analyze_functions ();
2446
2447 /* Finally drive the pass manager. */
2448 compile ();
2449
2450 timevar_pop (TV_CGRAPH);
2451 }
2452
2453 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2454 within the same process. For use by toplev::finalize. */
2455
2456 void
2457 cgraphunit_c_finalize (void)
2458 {
2459 gcc_assert (cgraph_new_nodes.length () == 0);
2460 cgraph_new_nodes.truncate (0);
2461
2462 vtable_entry_type = NULL;
2463 queued_nodes = &symtab_terminator;
2464
2465 first_analyzed = NULL;
2466 first_analyzed_var = NULL;
2467 }
2468
2469 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2470 kind of wrapper method. */
2471
2472 void
2473 cgraph_node::create_wrapper (cgraph_node *target)
2474 {
2475 /* Preserve DECL_RESULT so we get right by reference flag. */
2476 tree decl_result = DECL_RESULT (decl);
2477
2478 /* Remove the function's body but keep arguments to be reused
2479 for thunk. */
2480 release_body (true);
2481 reset ();
2482
2483 DECL_UNINLINABLE (decl) = false;
2484 DECL_RESULT (decl) = decl_result;
2485 DECL_INITIAL (decl) = NULL;
2486 allocate_struct_function (decl, false);
2487 set_cfun (NULL);
2488
2489 /* Turn alias into thunk and expand it into GIMPLE representation. */
2490 definition = true;
2491
2492 memset (&thunk, 0, sizeof (cgraph_thunk_info));
2493 thunk.thunk_p = true;
2494 create_edge (target, NULL, count, CGRAPH_FREQ_BASE);
2495
2496 tree arguments = DECL_ARGUMENTS (decl);
2497
2498 while (arguments)
2499 {
2500 TREE_ADDRESSABLE (arguments) = false;
2501 arguments = TREE_CHAIN (arguments);
2502 }
2503
2504 expand_thunk (false, true);
2505
2506 /* Inline summary set-up. */
2507 analyze ();
2508 inline_analyze_function (this);
2509 }
2510
2511 #include "gt-cgraphunit.h"