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