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