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