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