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