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