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