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