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