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