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