Line data Source code
1 : /* Driver of optimization process
2 : Copyright (C) 2003-2026 Free Software Foundation, Inc.
3 : Contributed by Jan Hubicka
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : /* This module implements main driver of compilation process.
22 :
23 : The main scope of this file is to act as an interface in between
24 : tree based frontends and the backend.
25 :
26 : The front-end is supposed to use following functionality:
27 :
28 : - 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 :
33 : (There is one exception needed for implementing GCC extern inline
34 : function.)
35 :
36 : - varpool_finalize_decl
37 :
38 : This function has same behavior as the above but is used for static
39 : variables.
40 :
41 : - add_asm_node
42 :
43 : Insert new toplevel ASM statement
44 :
45 : - finalize_compilation_unit
46 :
47 : This function is called once (source level) compilation unit is finalized
48 : and it will no longer change.
49 :
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.
53 : Those are used to discover other necessary functions and variables.
54 :
55 : At the end the bodies of unreachable functions are removed.
56 :
57 : The function can be called multiple times when multiple source level
58 : compilation units are combined.
59 :
60 : - compile
61 :
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 below).
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 optimizations 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 example, transactional 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 streaming. 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 multiple object files.
111 :
112 : Compile time and/or parallel linktime stage (ltrans)
113 :
114 : Each of the object files is streamed back and compiled
115 : separately. Now the function bodies becomes available
116 : again.
117 :
118 : 2) Virtual clone materialization
119 : (cgraph_materialize_clone)
120 :
121 : IP passes can produce copies of existing functions (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
127 :
128 : All IP passes transform function bodies based on earlier
129 : decision of the IP propagation.
130 :
131 : 4) late small IP passes
132 :
133 : Simple IP passes working within single program partition.
134 :
135 : 5) Expansion
136 : (expand_all_functions)
137 :
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.
143 :
144 : Note that with -fno-toplevel-reorder passes 5 and 6
145 : are combined together in cgraph_output_in_order.
146 :
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.
153 :
154 : - cgraph_function_versioning
155 :
156 : produces a copy of function into new one (a version)
157 : and apply simple transformations
158 : */
159 :
160 : #include "config.h"
161 : #include "system.h"
162 : #include "coretypes.h"
163 : #include "backend.h"
164 : #include "target.h"
165 : #include "rtl.h"
166 : #include "tree.h"
167 : #include "gimple.h"
168 : #include "cfghooks.h"
169 : #include "regset.h" /* FIXME: For reg_obstack. */
170 : #include "alloc-pool.h"
171 : #include "tree-pass.h"
172 : #include "stringpool.h"
173 : #include "gimple-ssa.h"
174 : #include "cgraph.h"
175 : #include "coverage.h"
176 : #include "lto-streamer.h"
177 : #include "fold-const.h"
178 : #include "varasm.h"
179 : #include "stor-layout.h"
180 : #include "output.h"
181 : #include "cfgcleanup.h"
182 : #include "gimple-iterator.h"
183 : #include "gimple-fold.h"
184 : #include "gimplify.h"
185 : #include "gimplify-me.h"
186 : #include "tree-cfg.h"
187 : #include "tree-into-ssa.h"
188 : #include "tree-ssa.h"
189 : #include "langhooks.h"
190 : #include "toplev.h"
191 : #include "debug.h"
192 : #include "symbol-summary.h"
193 : #include "tree-vrp.h"
194 : #include "sreal.h"
195 : #include "ipa-cp.h"
196 : #include "ipa-prop.h"
197 : #include "gimple-pretty-print.h"
198 : #include "plugin.h"
199 : #include "ipa-fnsummary.h"
200 : #include "ipa-utils.h"
201 : #include "except.h"
202 : #include "cfgloop.h"
203 : #include "context.h"
204 : #include "pass_manager.h"
205 : #include "tree-nested.h"
206 : #include "dbgcnt.h"
207 : #include "lto-section-names.h"
208 : #include "attribs.h"
209 : #include "ipa-inline.h"
210 : #include "omp-offload.h"
211 : #include "symtab-thunks.h"
212 :
213 : /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
214 : secondary queue used during optimization to accommodate passes that
215 : may generate new functions that need to be optimized and expanded. */
216 : vec<cgraph_node *> cgraph_new_nodes;
217 :
218 : static void expand_all_functions (void);
219 : static void mark_functions_to_output (void);
220 : static void handle_alias_pairs (void);
221 :
222 : /* Return true if this symbol is a function from the C frontend specified
223 : directly in RTL form (with "__RTL"). */
224 :
225 : bool
226 244481520 : symtab_node::native_rtl_p () const
227 : {
228 244481520 : if (TREE_CODE (decl) != FUNCTION_DECL)
229 : return false;
230 204947056 : if (!DECL_STRUCT_FUNCTION (decl))
231 : return false;
232 202973981 : return DECL_STRUCT_FUNCTION (decl)->curr_properties & PROP_rtl;
233 : }
234 :
235 : /* Determine if symbol declaration is needed. That is, visible to something
236 : either outside this translation unit, something magic in the system
237 : configury */
238 : bool
239 148770073 : symtab_node::needed_p (void)
240 : {
241 : /* Double check that no one output the function into assembly file
242 : early. */
243 148770073 : if (!native_rtl_p ())
244 148770050 : gcc_checking_assert
245 : (!DECL_ASSEMBLER_NAME_SET_P (decl)
246 : || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
247 :
248 148770073 : if (!definition)
249 : return false;
250 :
251 135312901 : if (DECL_EXTERNAL (decl))
252 : return false;
253 :
254 : /* If the user told us it is used, then it must be so. */
255 75911155 : if (force_output)
256 : return true;
257 74953258 : if (ref_by_asm)
258 : return true;
259 :
260 : /* ABI forced symbols are needed when they are external. */
261 74953256 : if (forced_by_abi && TREE_PUBLIC (decl))
262 : return true;
263 :
264 : /* Keep constructors, destructors and virtual functions. */
265 74917329 : if (TREE_CODE (decl) == FUNCTION_DECL
266 74917329 : && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)))
267 : return true;
268 :
269 : /* Externally visible variables must be output. The exception is
270 : COMDAT variables that must be output only when they are needed. */
271 74906783 : if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
272 1922803 : return true;
273 :
274 : return false;
275 : }
276 :
277 : /* Head and terminator of the queue of nodes to be processed while building
278 : callgraph. */
279 :
280 : static symtab_node symtab_terminator (SYMTAB_SYMBOL);
281 : static symtab_node *queued_nodes = &symtab_terminator;
282 :
283 : /* Add NODE to queue starting at QUEUED_NODES.
284 : The queue is linked via AUX pointers and terminated by pointer to 1. */
285 :
286 : static void
287 17131027 : enqueue_node (symtab_node *node)
288 : {
289 17131027 : if (node->aux)
290 : return;
291 6484027 : gcc_checking_assert (queued_nodes);
292 6484027 : node->aux = queued_nodes;
293 6484027 : queued_nodes = node;
294 : }
295 :
296 : /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
297 : functions into callgraph in a way so they look like ordinary reachable
298 : functions inserted into callgraph already at construction time. */
299 :
300 : void
301 22197173 : symbol_table::process_new_functions (void)
302 : {
303 22197173 : tree fndecl;
304 :
305 22197173 : if (!cgraph_new_nodes.exists ())
306 : return;
307 :
308 20067 : handle_alias_pairs ();
309 : /* Note that this queue may grow as its being processed, as the new
310 : functions may generate new ones. */
311 86097 : for (unsigned i = 0; i < cgraph_new_nodes.length (); i++)
312 : {
313 45963 : cgraph_node *node = cgraph_new_nodes[i];
314 45963 : fndecl = node->decl;
315 45963 : bitmap_obstack_initialize (NULL);
316 45963 : switch (state)
317 : {
318 44766 : case CONSTRUCTION:
319 : /* At construction time we just need to finalize function and move
320 : it into reachable functions list. */
321 :
322 44766 : cgraph_node::finalize_function (fndecl, false);
323 44766 : call_cgraph_insertion_hooks (node);
324 44766 : enqueue_node (node);
325 44766 : break;
326 :
327 1004 : case IPA:
328 1004 : case IPA_SSA:
329 1004 : case IPA_SSA_AFTER_INLINING:
330 : /* When IPA optimization already started, do all essential
331 : transformations that has been already performed on the whole
332 : cgraph but not on this function. */
333 :
334 1004 : gimple_register_cfg_hooks ();
335 1004 : if (!node->analyzed)
336 1004 : node->analyze ();
337 1004 : push_cfun (DECL_STRUCT_FUNCTION (fndecl));
338 1004 : if ((state == IPA_SSA || state == IPA_SSA_AFTER_INLINING)
339 2008 : && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
340 : {
341 1004 : bool summaried_computed = ipa_fn_summaries != NULL;
342 1004 : g->get_passes ()->execute_early_local_passes ();
343 : /* Early passes compute inline parameters to do inlining
344 : and splitting. This is redundant for functions added late.
345 : Just throw away whatever it did. */
346 1004 : if (!summaried_computed)
347 : {
348 906 : ipa_free_fn_summary ();
349 906 : ipa_free_size_summary ();
350 : }
351 : }
352 0 : else if (ipa_fn_summaries != NULL)
353 0 : compute_fn_summary (node, true);
354 1004 : free_dominance_info (CDI_POST_DOMINATORS);
355 1004 : free_dominance_info (CDI_DOMINATORS);
356 1004 : pop_cfun ();
357 1004 : call_cgraph_insertion_hooks (node);
358 1004 : break;
359 :
360 193 : case EXPANSION:
361 : /* Functions created during expansion shall be compiled
362 : directly. */
363 193 : node->process = 0;
364 193 : call_cgraph_insertion_hooks (node);
365 193 : node->expand ();
366 193 : break;
367 :
368 0 : default:
369 0 : gcc_unreachable ();
370 45963 : break;
371 : }
372 45963 : bitmap_obstack_release (NULL);
373 : }
374 :
375 20067 : cgraph_new_nodes.release ();
376 : }
377 :
378 : /* As an GCC extension we allow redefinition of the function. The
379 : semantics when both copies of bodies differ is not well defined.
380 : We replace the old body with new body so in unit at a time mode
381 : we always use new body, while in normal mode we may end up with
382 : old body inlined into some functions and new body expanded and
383 : inlined in others.
384 :
385 : ??? It may make more sense to use one body for inlining and other
386 : body for expanding the function but this is difficult to do.
387 :
388 : This is also used to cancel C++ mangling aliases, which can be for
389 : functions or variables. */
390 :
391 : void
392 38363 : symtab_node::reset (bool preserve_comdat_group)
393 : {
394 : /* Reset our data structures so we can analyze the function again. */
395 38363 : analyzed = false;
396 38363 : definition = false;
397 38363 : alias = false;
398 38363 : transparent_alias = false;
399 38363 : weakref = false;
400 38363 : cpp_implicit_alias = false;
401 :
402 38363 : remove_all_references ();
403 38363 : if (!preserve_comdat_group)
404 37886 : remove_from_same_comdat_group ();
405 :
406 38363 : if (cgraph_node *cn = dyn_cast <cgraph_node *> (this))
407 : {
408 : /* If process is set, then we have already begun whole-unit analysis.
409 : This is *not* testing for whether we've already emitted the function.
410 : That case can be sort-of legitimately seen with real function
411 : redefinition errors. I would argue that the front end should never
412 : present us with such a case, but don't enforce that for now. */
413 38363 : gcc_assert (!cn->process);
414 :
415 38363 : memset (&cn->rtl, 0, sizeof (cn->rtl));
416 38363 : cn->inlined_to = NULL;
417 38363 : cn->remove_callees ();
418 : }
419 38363 : }
420 :
421 : /* Return true when there are references to the node. INCLUDE_SELF is
422 : true if a self reference counts as a reference. */
423 :
424 : bool
425 146029572 : symtab_node::referred_to_p (bool include_self)
426 : {
427 146029572 : ipa_ref *ref = NULL;
428 :
429 : /* See if there are any references at all. */
430 146029572 : if (iterate_referring (0, ref))
431 : return true;
432 : /* For functions check also calls. */
433 143700394 : cgraph_node *cn = dyn_cast <cgraph_node *> (this);
434 107766284 : if (cn && cn->callers)
435 : {
436 3049096 : if (include_self)
437 : return true;
438 2035715 : for (cgraph_edge *e = cn->callers; e; e = e->next_caller)
439 2035379 : if (e->caller != this)
440 : return true;
441 : }
442 : return false;
443 : }
444 :
445 : /* DECL has been parsed. Take it, queue it, compile it at the whim of the
446 : logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
447 : the garbage collector run at the moment. We would need to either create
448 : a new GC context, or just not compile right now. Returns the created
449 : node. */
450 :
451 : cgraph_node *
452 91110007 : cgraph_node::finalize_function (tree decl, bool no_collect)
453 : {
454 91110007 : cgraph_node *node = cgraph_node::get_create (decl);
455 :
456 91110007 : if (node->definition)
457 : {
458 : /* Nested functions should only be defined once. */
459 150 : gcc_assert (!DECL_CONTEXT (decl)
460 : || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL);
461 150 : node->reset ();
462 150 : node->redefined_extern_inline = true;
463 : }
464 :
465 : /* Set definition first before calling notice_global_symbol so that
466 : it is available to notice_global_symbol. */
467 91110007 : node->definition = true;
468 91110007 : notice_global_symbol (decl);
469 91110007 : node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
470 91110007 : node->semantic_interposition = opt_for_fn (decl, flag_semantic_interposition);
471 91110007 : if (!flag_toplevel_reorder)
472 4543943 : node->no_reorder = true;
473 :
474 : /* With -fkeep-inline-functions we are keeping all inline functions except
475 : for extern inline ones. */
476 91110007 : if (flag_keep_inline_functions
477 25382 : && DECL_DECLARED_INLINE_P (decl)
478 25264 : && !DECL_EXTERNAL (decl)
479 91112084 : && !DECL_DISREGARD_INLINE_LIMITS (decl))
480 1880 : node->force_output = 1;
481 :
482 : /* __RTL functions were already output as soon as they were parsed (due
483 : to the large amount of global state in the backend).
484 : Mark such functions as "force_output" to reflect the fact that they
485 : will be in the asm file when considering the symbols they reference.
486 : The attempt to output them later on will bail out immediately. */
487 91110007 : if (node->native_rtl_p ())
488 127 : node->force_output = 1;
489 :
490 : /* When not optimizing, also output the static functions. (see
491 : PR24561), but don't do so for always_inline functions, functions
492 : declared inline and nested functions. These were optimized out
493 : in the original implementation and it is unclear whether we want
494 : to change the behavior here. */
495 177779742 : if (((!opt_for_fn (decl, optimize) || flag_keep_static_functions
496 86669734 : || node->no_reorder)
497 4544098 : && !node->cpp_implicit_alias
498 4544098 : && !DECL_DISREGARD_INLINE_LIMITS (decl)
499 2863281 : && !DECL_DECLARED_INLINE_P (decl)
500 517067 : && !(DECL_CONTEXT (decl)
501 256559 : && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
502 91611692 : && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
503 445450 : node->force_output = 1;
504 :
505 : /* If we've not yet emitted decl, tell the debug info about it. */
506 91110007 : if (!TREE_ASM_WRITTEN (decl))
507 91110007 : (*debug_hooks->deferred_inline_function) (decl);
508 :
509 91110007 : if (!no_collect)
510 80780419 : ggc_collect ();
511 :
512 91110007 : if (symtab->state == CONSTRUCTION
513 91110007 : && (node->needed_p () || node->referred_to_p ()))
514 45174 : enqueue_node (node);
515 91110007 : return node;
516 : }
517 :
518 : /* Add the function FNDECL to the call graph.
519 : Unlike finalize_function, this function is intended to be used
520 : by middle end and allows insertion of new function at arbitrary point
521 : of compilation. The function can be either in high, low or SSA form
522 : GIMPLE.
523 :
524 : The function is assumed to be reachable and have address taken (so no
525 : API breaking optimizations are performed on it).
526 :
527 : Main work done by this function is to enqueue the function for later
528 : processing to avoid need the passes to be re-entrant.
529 :
530 : Returns the created node. */
531 :
532 : cgraph_node *
533 49943 : cgraph_node::add_new_function (tree fndecl, bool lowered)
534 : {
535 49943 : gcc::pass_manager *passes = g->get_passes ();
536 49943 : cgraph_node *node;
537 :
538 49943 : if (dump_file)
539 : {
540 130 : struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
541 130 : const char *function_type = ((gimple_has_body_p (fndecl))
542 130 : ? (lowered
543 130 : ? (gimple_in_ssa_p (fn)
544 : ? "ssa gimple"
545 : : "low gimple")
546 : : "high gimple")
547 125 : : "to-be-gimplified");
548 130 : fprintf (dump_file,
549 : "Added new %s function %s to callgraph\n",
550 : function_type,
551 : fndecl_name (fndecl));
552 : }
553 :
554 49943 : switch (symtab->state)
555 : {
556 0 : case PARSING:
557 0 : node = cgraph_node::finalize_function (fndecl, false);
558 0 : break;
559 44769 : case CONSTRUCTION:
560 : /* Just enqueue function to be processed at nearest occurrence. */
561 44769 : node = cgraph_node::get_create (fndecl);
562 44769 : if (lowered)
563 44085 : node->lowered = true;
564 44769 : cgraph_new_nodes.safe_push (node);
565 44769 : break;
566 :
567 1197 : case IPA:
568 1197 : case IPA_SSA:
569 1197 : case IPA_SSA_AFTER_INLINING:
570 1197 : case EXPANSION:
571 : /* Bring the function into finalized state and enqueue for later
572 : analyzing and compilation. */
573 1197 : node = cgraph_node::get_create (fndecl);
574 1197 : node->local = false;
575 1197 : node->definition = true;
576 1197 : node->semantic_interposition = opt_for_fn (fndecl,
577 : flag_semantic_interposition);
578 1197 : node->force_output = true;
579 1197 : if (TREE_PUBLIC (fndecl))
580 68 : node->externally_visible = true;
581 1197 : if (!lowered && symtab->state == EXPANSION)
582 : {
583 0 : push_cfun (DECL_STRUCT_FUNCTION (fndecl));
584 0 : gimple_register_cfg_hooks ();
585 0 : bitmap_obstack_initialize (NULL);
586 0 : execute_pass_list (cfun, passes->all_lowering_passes);
587 0 : passes->execute_early_local_passes ();
588 0 : bitmap_obstack_release (NULL);
589 0 : pop_cfun ();
590 :
591 0 : lowered = true;
592 : }
593 0 : if (lowered)
594 283 : node->lowered = true;
595 1197 : cgraph_new_nodes.safe_push (node);
596 1197 : break;
597 :
598 3977 : case FINISHED:
599 : /* At the very end of compilation we have to do all the work up
600 : to expansion. */
601 3977 : node = cgraph_node::create (fndecl);
602 3977 : if (lowered)
603 0 : node->lowered = true;
604 3977 : node->definition = true;
605 3977 : node->semantic_interposition = opt_for_fn (fndecl,
606 : flag_semantic_interposition);
607 3977 : node->analyze ();
608 3977 : push_cfun (DECL_STRUCT_FUNCTION (fndecl));
609 3977 : gimple_register_cfg_hooks ();
610 3977 : bitmap_obstack_initialize (NULL);
611 3977 : if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
612 3977 : g->get_passes ()->execute_early_local_passes ();
613 3977 : bitmap_obstack_release (NULL);
614 3977 : pop_cfun ();
615 3977 : node->expand ();
616 3977 : break;
617 :
618 0 : default:
619 0 : gcc_unreachable ();
620 : }
621 :
622 : /* Set a personality if required and we already passed EH lowering. */
623 49943 : if (lowered
624 94311 : && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
625 : == eh_personality_lang))
626 2054 : DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
627 49943 : return node;
628 : }
629 :
630 : /* Analyze the function scheduled to be output. */
631 : void
632 3083322 : cgraph_node::analyze (void)
633 : {
634 3083322 : if (native_rtl_p ())
635 : {
636 23 : analyzed = true;
637 23 : return;
638 : }
639 :
640 3083299 : tree decl = this->decl;
641 3083299 : location_t saved_loc = input_location;
642 3083299 : input_location = DECL_SOURCE_LOCATION (decl);
643 3083299 : semantic_interposition = opt_for_fn (decl, flag_semantic_interposition);
644 :
645 3083299 : if (thunk)
646 : {
647 3461 : thunk_info *info = thunk_info::get (this);
648 3461 : cgraph_node *t = cgraph_node::get (info->alias);
649 :
650 3461 : create_edge (t, NULL, t->count);
651 3461 : callees->can_throw_external = !TREE_NOTHROW (t->decl);
652 : /* Target code in expand_thunk may need the thunk's target
653 : to be analyzed, so recurse here. */
654 3461 : if (!t->analyzed && t->definition)
655 0 : t->analyze ();
656 3461 : if (t->alias)
657 : {
658 3461 : t = t->get_alias_target ();
659 3461 : if (!t->analyzed && t->definition)
660 1337 : t->analyze ();
661 : }
662 3461 : bool ret = expand_thunk (this, false, false);
663 3461 : thunk_info::get (this)->alias = NULL;
664 3461 : if (!ret)
665 : return;
666 : }
667 3079999 : if (alias)
668 5131 : resolve_alias (cgraph_node::get (alias_target), transparent_alias);
669 3074868 : else if (dispatcher_function)
670 : {
671 : /* Generate the dispatcher body of multi-versioned functions. */
672 114 : cgraph_function_version_info *dispatcher_version_info
673 114 : = function_version ();
674 114 : if (dispatcher_version_info != NULL
675 114 : && (dispatcher_version_info->dispatcher_resolver
676 : == NULL_TREE))
677 : {
678 114 : tree resolver = NULL_TREE;
679 114 : gcc_assert (targetm.generate_version_dispatcher_body);
680 114 : resolver = targetm.generate_version_dispatcher_body (this);
681 114 : gcc_assert (resolver != NULL_TREE);
682 : }
683 : }
684 : else
685 : {
686 3074754 : push_cfun (DECL_STRUCT_FUNCTION (decl));
687 :
688 3074754 : assign_assembler_name_if_needed (decl);
689 :
690 : /* Make sure to gimplify bodies only once. During analyzing a
691 : function we lower it, which will require gimplified nested
692 : functions, so we can end up here with an already gimplified
693 : body. */
694 3074754 : if (!gimple_has_body_p (decl))
695 2974176 : gimplify_function_tree (decl);
696 :
697 : /* Lower the function. */
698 3074754 : if (!lowered)
699 : {
700 3020225 : if (first_nested_function (this))
701 10075 : lower_nested_functions (decl);
702 :
703 3010150 : gimple_register_cfg_hooks ();
704 3010150 : bitmap_obstack_initialize (NULL);
705 3010150 : execute_pass_list (cfun, g->get_passes ()->all_lowering_passes);
706 3010137 : compact_blocks ();
707 3010137 : bitmap_obstack_release (NULL);
708 3010137 : lowered = true;
709 : }
710 :
711 3074741 : pop_cfun ();
712 : }
713 3079986 : analyzed = true;
714 :
715 3079986 : input_location = saved_loc;
716 : }
717 :
718 : /* C++ frontend produce same body aliases all over the place, even before PCH
719 : gets streamed out. It relies on us linking the aliases with their function
720 : in order to do the fixups, but ipa-ref is not PCH safe. Consequently we
721 : first produce aliases without links, but once C++ FE is sure he won't stream
722 : PCH we build the links via this function. */
723 :
724 : void
725 207454 : symbol_table::process_same_body_aliases (void)
726 : {
727 207454 : symtab_node *node;
728 101753062 : FOR_EACH_SYMBOL (node)
729 101545608 : if (node->cpp_implicit_alias && !node->analyzed)
730 4392256 : node->resolve_alias (symtab_node::get_create (node->alias_target));
731 207454 : cpp_implicit_aliases_done = true;
732 207454 : }
733 :
734 : /* Process a symver attribute. */
735 :
736 : static void
737 146382189 : process_symver_attribute (symtab_node *n)
738 : {
739 146382189 : tree value = lookup_attribute ("symver", DECL_ATTRIBUTES (n->decl));
740 :
741 292764380 : for (; value != NULL; value = TREE_CHAIN (value))
742 : {
743 : /* Starting from binutils 2.35 gas supports:
744 : # Assign foo to bar@V1 and baz@V2.
745 : .symver foo, bar@V1
746 : .symver foo, baz@V2
747 : */
748 2 : const char *purpose = IDENTIFIER_POINTER (TREE_PURPOSE (value));
749 2 : if (strcmp (purpose, "symver") != 0)
750 0 : continue;
751 :
752 2 : tree symver = get_identifier_with_length
753 4 : (TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (value))),
754 2 : TREE_STRING_LENGTH (TREE_VALUE (TREE_VALUE (value))));
755 2 : symtab_node *def = symtab_node::get_for_asmname (symver);
756 :
757 2 : if (def)
758 : {
759 0 : error_at (DECL_SOURCE_LOCATION (n->decl),
760 : "duplicate definition of a symbol version");
761 0 : inform (DECL_SOURCE_LOCATION (def->decl),
762 : "same version was previously defined here");
763 0 : return;
764 : }
765 2 : if (!n->definition)
766 : {
767 0 : error_at (DECL_SOURCE_LOCATION (n->decl),
768 : "symbol needs to be defined to have a version");
769 0 : return;
770 : }
771 2 : if (DECL_COMMON (n->decl))
772 : {
773 0 : error_at (DECL_SOURCE_LOCATION (n->decl),
774 : "common symbol cannot be versioned");
775 0 : return;
776 : }
777 2 : if (DECL_COMDAT (n->decl))
778 : {
779 0 : error_at (DECL_SOURCE_LOCATION (n->decl),
780 : "comdat symbol cannot be versioned");
781 0 : return;
782 : }
783 2 : if (n->weakref)
784 : {
785 0 : error_at (DECL_SOURCE_LOCATION (n->decl),
786 : "%<weakref%> cannot be versioned");
787 0 : return;
788 : }
789 2 : if (!TREE_PUBLIC (n->decl))
790 : {
791 0 : error_at (DECL_SOURCE_LOCATION (n->decl),
792 : "versioned symbol must be public");
793 0 : return;
794 : }
795 2 : if (DECL_VISIBILITY (n->decl) != VISIBILITY_DEFAULT)
796 : {
797 0 : error_at (DECL_SOURCE_LOCATION (n->decl),
798 : "versioned symbol must have default visibility");
799 0 : return;
800 : }
801 :
802 : /* Create new symbol table entry representing the version. */
803 2 : tree new_decl = copy_node (n->decl);
804 :
805 2 : DECL_INITIAL (new_decl) = NULL_TREE;
806 2 : if (TREE_CODE (new_decl) == FUNCTION_DECL)
807 2 : DECL_STRUCT_FUNCTION (new_decl) = NULL;
808 2 : SET_DECL_ASSEMBLER_NAME (new_decl, symver);
809 2 : TREE_PUBLIC (new_decl) = 1;
810 2 : DECL_ATTRIBUTES (new_decl) = NULL;
811 :
812 2 : symtab_node *symver_node = symtab_node::get_create (new_decl);
813 2 : symver_node->alias = true;
814 2 : symver_node->definition = true;
815 2 : symver_node->symver = true;
816 2 : symver_node->create_reference (n, IPA_REF_ALIAS, NULL);
817 2 : symver_node->analyzed = true;
818 : }
819 : }
820 :
821 : /* Process attributes common for vars and functions. */
822 :
823 : static void
824 146382189 : process_common_attributes (symtab_node *node, tree decl)
825 : {
826 146382189 : tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
827 :
828 146382189 : if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
829 : {
830 1 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
831 : "%<weakref%> attribute should be accompanied with"
832 : " an %<alias%> attribute");
833 1 : DECL_WEAK (decl) = 0;
834 1 : DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
835 1 : DECL_ATTRIBUTES (decl));
836 : }
837 :
838 146382189 : if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
839 17 : node->no_reorder = 1;
840 146382189 : process_symver_attribute (node);
841 146382189 : }
842 :
843 : /* Look for externally_visible and used attributes and mark cgraph nodes
844 : accordingly.
845 :
846 : We cannot mark the nodes at the point the attributes are processed (in
847 : handle_*_attribute) because the copy of the declarations available at that
848 : point may not be canonical. For example, in:
849 :
850 : void f();
851 : void f() __attribute__((used));
852 :
853 : the declaration we see in handle_used_attribute will be the second
854 : declaration -- but the front end will subsequently merge that declaration
855 : with the original declaration and discard the second declaration.
856 :
857 : Furthermore, we can't mark these nodes in finalize_function because:
858 :
859 : void f() {}
860 : void f() __attribute__((externally_visible));
861 :
862 : is valid.
863 :
864 : So, we walk the nodes at the end of the translation unit, applying the
865 : attributes at that point. */
866 :
867 : static void
868 781540 : process_function_and_variable_attributes (cgraph_node *first,
869 : varpool_node *first_var)
870 : {
871 781540 : cgraph_node *node;
872 781540 : varpool_node *vnode;
873 :
874 109945565 : for (node = symtab->first_function (); node != first;
875 109164025 : node = symtab->next_function (node))
876 : {
877 109164025 : tree decl = node->decl;
878 :
879 109164025 : if (node->alias
880 109164025 : && lookup_attribute ("flatten", DECL_ATTRIBUTES (decl)))
881 : {
882 8 : tree tdecl = node->get_alias_target_tree ();
883 8 : if (!tdecl || !DECL_P (tdecl)
884 16 : || !lookup_attribute ("flatten", DECL_ATTRIBUTES (tdecl)))
885 1 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
886 : "%<flatten%> attribute is ignored on aliases");
887 : }
888 109164025 : if (DECL_PRESERVE_P (decl))
889 5145 : node->mark_force_output ();
890 109158880 : else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
891 : {
892 41496 : if (! TREE_PUBLIC (node->decl))
893 4 : warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
894 : "%<externally_visible%>"
895 : " attribute have effect only on public objects");
896 : }
897 109164025 : if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
898 394 : && node->definition
899 109164075 : && (!node->alias || DECL_INITIAL (decl) != error_mark_node))
900 : {
901 : /* NODE->DEFINITION && NODE->ALIAS is nonzero for valid weakref
902 : function declarations; DECL_INITIAL is non-null for invalid
903 : weakref functions that are also defined. */
904 3 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
905 : "%<weakref%> attribute ignored"
906 : " because function is defined");
907 3 : DECL_WEAK (decl) = 0;
908 3 : DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
909 3 : DECL_ATTRIBUTES (decl));
910 3 : DECL_ATTRIBUTES (decl) = remove_attribute ("alias",
911 3 : DECL_ATTRIBUTES (decl));
912 3 : node->alias = false;
913 3 : node->weakref = false;
914 3 : node->transparent_alias = false;
915 : }
916 109164022 : else if (lookup_attribute ("alias", DECL_ATTRIBUTES (decl))
917 5506 : && node->definition
918 109169175 : && !node->alias)
919 1 : warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
920 : "%<alias%> attribute ignored"
921 : " because function is defined");
922 :
923 109164025 : if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
924 45568307 : && !DECL_DECLARED_INLINE_P (decl)
925 : /* redefining extern inline function makes it DECL_UNINLINABLE. */
926 109233197 : && !DECL_UNINLINABLE (decl))
927 69163 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
928 : "%<always_inline%> function might not be inlinable"
929 : " unless also declared %<inline%>");
930 :
931 109164025 : process_common_attributes (node, decl);
932 : }
933 37999704 : for (vnode = symtab->first_variable (); vnode != first_var;
934 37218164 : vnode = symtab->next_variable (vnode))
935 : {
936 37218164 : tree decl = vnode->decl;
937 37218164 : if (DECL_EXTERNAL (decl)
938 37218164 : && DECL_INITIAL (decl))
939 2063329 : varpool_node::finalize_decl (decl);
940 37218164 : if (DECL_PRESERVE_P (decl))
941 3234 : vnode->force_output = true;
942 37214930 : else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
943 : {
944 42 : if (! TREE_PUBLIC (vnode->decl))
945 4 : warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
946 : "%<externally_visible%>"
947 : " attribute have effect only on public objects");
948 : }
949 37218164 : if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
950 69 : && vnode->definition
951 37218177 : && DECL_INITIAL (decl))
952 : {
953 1 : warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
954 : "%<weakref%> attribute ignored"
955 : " because variable is initialized");
956 1 : DECL_WEAK (decl) = 0;
957 1 : DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
958 1 : DECL_ATTRIBUTES (decl));
959 : }
960 37218164 : process_common_attributes (vnode, decl);
961 : }
962 781540 : }
963 :
964 : /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
965 : middle end to output the variable to asm file, if needed or externally
966 : visible. */
967 :
968 : void
969 34859286 : varpool_node::finalize_decl (tree decl)
970 : {
971 34859286 : varpool_node *node = varpool_node::get_create (decl);
972 :
973 34859286 : gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
974 :
975 34859286 : if (node->definition)
976 : return;
977 : /* Set definition first before calling notice_global_symbol so that
978 : it is available to notice_global_symbol. */
979 34833319 : node->definition = true;
980 34833319 : node->semantic_interposition = flag_semantic_interposition;
981 34833319 : notice_global_symbol (decl);
982 34833319 : if (!flag_toplevel_reorder)
983 3468995 : node->no_reorder = true;
984 34742598 : if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
985 : /* Traditionally we do not eliminate static variables when not
986 : optimizing and when not doing toplevel reorder. */
987 69572583 : || (node->no_reorder && !DECL_COMDAT (node->decl)
988 1092683 : && !DECL_ARTIFICIAL (node->decl)))
989 477011 : node->force_output = true;
990 :
991 34833319 : if (flag_openmp)
992 : {
993 157418 : tree attr = lookup_attribute ("omp allocate", DECL_ATTRIBUTES (decl));
994 157418 : if (attr)
995 : {
996 176 : tree align = TREE_VALUE (TREE_VALUE (attr));
997 174 : if (align)
998 19 : SET_DECL_ALIGN (decl, MAX (tree_to_uhwi (align) * BITS_PER_UNIT,
999 : DECL_ALIGN (decl)));
1000 : }
1001 : }
1002 :
1003 34833317 : if (symtab->state == CONSTRUCTION
1004 34833317 : && (node->needed_p () || node->referred_to_p ()))
1005 250001 : enqueue_node (node);
1006 34833317 : if (symtab->state >= IPA_SSA)
1007 33420 : node->analyze ();
1008 : /* Some frontends produce various interface variables after compilation
1009 : finished. */
1010 34833317 : if (symtab->state == FINISHED
1011 34828560 : || (node->no_reorder
1012 3468318 : && symtab->state == EXPANSION))
1013 12837 : node->assemble_decl ();
1014 : }
1015 :
1016 : /* EDGE is an polymorphic call. Mark all possible targets as reachable
1017 : and if there is only one target, perform trivial devirtualization.
1018 : REACHABLE_CALL_TARGETS collects target lists we already walked to
1019 : avoid duplicate work. */
1020 :
1021 : static void
1022 19320 : walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
1023 : cgraph_edge *edge)
1024 : {
1025 19320 : unsigned int i;
1026 19320 : void *cache_token;
1027 19320 : bool final;
1028 19320 : vec <cgraph_node *>targets
1029 : = possible_polymorphic_call_targets
1030 19320 : (edge, &final, &cache_token);
1031 :
1032 19320 : if (cache_token != NULL && !reachable_call_targets->add (cache_token))
1033 : {
1034 15011 : if (symtab->dump_file)
1035 12 : dump_possible_polymorphic_call_targets
1036 12 : (symtab->dump_file, edge);
1037 :
1038 42251 : for (i = 0; i < targets.length (); i++)
1039 : {
1040 : /* Do not bother to mark virtual methods in anonymous namespace;
1041 : either we will find use of virtual table defining it, or it is
1042 : unused. */
1043 27240 : if (targets[i]->definition
1044 18520 : && TREE_CODE
1045 : (TREE_TYPE (targets[i]->decl))
1046 : == METHOD_TYPE
1047 45760 : && !type_in_anonymous_namespace_p
1048 18520 : (TYPE_METHOD_BASETYPE (TREE_TYPE (targets[i]->decl))))
1049 17844 : enqueue_node (targets[i]);
1050 : }
1051 : }
1052 :
1053 : /* Very trivial devirtualization; when the type is
1054 : final or anonymous (so we know all its derivation)
1055 : and there is only one possible virtual call target,
1056 : make the edge direct. */
1057 19320 : if (final)
1058 : {
1059 84 : if (targets.length () <= 1 && dbg_cnt (devirt))
1060 : {
1061 0 : cgraph_node *target;
1062 0 : if (targets.length () == 1)
1063 0 : target = targets[0];
1064 : else
1065 0 : target = cgraph_node::create (builtin_decl_unreachable ());
1066 :
1067 0 : if (symtab->dump_file)
1068 : {
1069 0 : fprintf (symtab->dump_file,
1070 : "Devirtualizing call: ");
1071 0 : print_gimple_stmt (symtab->dump_file,
1072 0 : edge->call_stmt, 0,
1073 : TDF_SLIM);
1074 : }
1075 0 : if (dump_enabled_p ())
1076 : {
1077 0 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, edge->call_stmt,
1078 : "devirtualizing call in %s to %s\n",
1079 0 : edge->caller->dump_name (),
1080 : target->dump_name ());
1081 : }
1082 :
1083 0 : edge = cgraph_edge::make_direct (edge, target);
1084 0 : gimple *new_call = cgraph_edge::redirect_call_stmt_to_callee (edge);
1085 :
1086 0 : if (symtab->dump_file)
1087 : {
1088 0 : fprintf (symtab->dump_file, "Devirtualized as: ");
1089 0 : print_gimple_stmt (symtab->dump_file, new_call, 0, TDF_SLIM);
1090 : }
1091 : }
1092 : }
1093 19320 : }
1094 :
1095 : /* Issue appropriate warnings for the global declaration DECL. */
1096 :
1097 : static void
1098 146406507 : check_global_declaration (symtab_node *snode)
1099 : {
1100 146406507 : const char *decl_file;
1101 146406507 : tree decl = snode->decl;
1102 :
1103 : /* Warn about any function declared static but not defined. We don't
1104 : warn about variables, because many programs have static variables
1105 : that exist only to get some text into the object file. */
1106 146406507 : if (TREE_CODE (decl) == FUNCTION_DECL
1107 109188261 : && DECL_INITIAL (decl) == 0
1108 1866649 : && DECL_EXTERNAL (decl)
1109 1861390 : && ! DECL_ARTIFICIAL (decl)
1110 148118766 : && ! TREE_PUBLIC (decl))
1111 : {
1112 170 : if (warning_suppressed_p (decl, OPT_Wunused))
1113 : ;
1114 109 : else if (snode->referred_to_p (/*include_self=*/false))
1115 : {
1116 106 : if (pedwarn (input_location, 0, "%q+F used but never defined", decl))
1117 61 : suppress_warning (decl, OPT_Wunused);
1118 : }
1119 3 : else if (warning (OPT_Wunused_function,
1120 : "%q+F declared %<static%> but never defined", decl))
1121 0 : suppress_warning (decl, OPT_Wunused);
1122 : }
1123 :
1124 : /* Warn about static fns or vars defined but not used. */
1125 5094828 : if (((warn_unused_function && TREE_CODE (decl) == FUNCTION_DECL)
1126 141794326 : || (((warn_unused_variable && ! TREE_READONLY (decl))
1127 141730479 : || (warn_unused_const_variable > 0 && TREE_READONLY (decl)
1128 6731 : && (warn_unused_const_variable == 2
1129 6725 : || (main_input_filename != NULL
1130 146412608 : && (decl_file = DECL_SOURCE_FILE (decl)) != NULL
1131 6718 : && filename_cmp (main_input_filename,
1132 : decl_file) == 0))))
1133 69714 : && VAR_P (decl)))
1134 4659454 : && ! DECL_IN_SYSTEM_HEADER (decl)
1135 925686 : && ! snode->referred_to_p (/*include_self=*/false)
1136 : /* This TREE_USED check is needed in addition to referred_to_p
1137 : above, because the `__unused__' attribute is not being
1138 : considered for referred_to_p. */
1139 607638 : && ! TREE_USED (decl)
1140 : /* The TREE_USED bit for file-scope decls is kept in the identifier,
1141 : to handle multiple external decls in different scopes. */
1142 338123 : && ! (DECL_NAME (decl) && TREE_USED (DECL_NAME (decl)))
1143 338123 : && ! DECL_EXTERNAL (decl)
1144 140498 : && ! DECL_ARTIFICIAL (decl)
1145 139732 : && ! DECL_ABSTRACT_ORIGIN (decl)
1146 112232 : && ! TREE_PUBLIC (decl)
1147 : /* A volatile variable might be used in some non-obvious way. */
1148 19956 : && (! VAR_P (decl) || ! TREE_THIS_VOLATILE (decl))
1149 : /* Global register variables must be declared to reserve them. */
1150 19955 : && ! (VAR_P (decl) && DECL_REGISTER (decl))
1151 : /* Global ctors and dtors are called by the runtime. */
1152 19955 : && (TREE_CODE (decl) != FUNCTION_DECL
1153 19875 : || (!DECL_STATIC_CONSTRUCTOR (decl)
1154 19875 : && !DECL_STATIC_DESTRUCTOR (decl)))
1155 19955 : && (! VAR_P (decl) || !warning_suppressed_p (decl, OPT_Wunused_variable))
1156 : /* Otherwise, ask the language. */
1157 146426449 : && lang_hooks.decls.warn_unused_global (decl))
1158 617 : warning_at (DECL_SOURCE_LOCATION (decl),
1159 617 : (TREE_CODE (decl) == FUNCTION_DECL)
1160 684 : ? OPT_Wunused_function
1161 67 : : (TREE_READONLY (decl)
1162 : ? OPT_Wunused_const_variable_
1163 : : OPT_Wunused_variable),
1164 : "%qD defined but not used", decl);
1165 146406507 : }
1166 :
1167 : /* Discover all functions and variables that are trivially needed, analyze
1168 : them as well as all functions and variables referred by them */
1169 : static cgraph_node *first_analyzed;
1170 : static varpool_node *first_analyzed_var;
1171 :
1172 : /* FIRST_TIME is set to TRUE for the first time we are called for a
1173 : translation unit from finalize_compilation_unit() or false
1174 : otherwise. */
1175 :
1176 : static void
1177 531447 : analyze_functions (bool first_time)
1178 : {
1179 : /* Keep track of already processed nodes when called multiple times for
1180 : intermodule optimization. */
1181 531447 : cgraph_node *first_handled = first_analyzed;
1182 531447 : varpool_node *first_handled_var = first_analyzed_var;
1183 531447 : hash_set<void *> reachable_call_targets;
1184 :
1185 531447 : symtab_node *node;
1186 531447 : symtab_node *next;
1187 531447 : int i;
1188 531447 : ipa_ref *ref;
1189 531447 : bool changed = true;
1190 531447 : location_t saved_loc = input_location;
1191 :
1192 531447 : bitmap_obstack_initialize (NULL);
1193 531447 : symtab->state = CONSTRUCTION;
1194 531447 : input_location = UNKNOWN_LOCATION;
1195 :
1196 531447 : thunk_info::process_early_thunks ();
1197 :
1198 : /* Ugly, but the fixup cannot happen at a time same body alias is created;
1199 : C++ FE is confused about the COMDAT groups being right. */
1200 531447 : if (symtab->cpp_implicit_aliases_done)
1201 147842800 : FOR_EACH_SYMBOL (node)
1202 147428423 : if (node->cpp_implicit_alias)
1203 8457468 : node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
1204 531447 : build_type_inheritance_graph ();
1205 :
1206 531447 : if (flag_openmp && first_time)
1207 9879 : omp_discover_implicit_declare_target ();
1208 :
1209 : /* Analysis adds static variables that in turn adds references to new functions.
1210 : So we need to iterate the process until it stabilize. */
1211 1312974 : while (changed)
1212 : {
1213 781540 : changed = false;
1214 781540 : process_function_and_variable_attributes (first_analyzed,
1215 : first_analyzed_var);
1216 :
1217 : /* First identify the trivially needed symbols. */
1218 781540 : for (node = symtab->first_symbol ();
1219 147163731 : node != first_analyzed && node != first_analyzed_var;
1220 146382191 : node = safe_as_a<symtab_node *>(node->next))
1221 : {
1222 : /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
1223 146382191 : node->get_comdat_group_id ();
1224 146382191 : if (node->needed_p ())
1225 : {
1226 2844724 : enqueue_node (node);
1227 2844724 : if (!changed && symtab->dump_file)
1228 75 : fprintf (symtab->dump_file, "Trivially needed symbols:");
1229 2844724 : changed = true;
1230 2844724 : if (symtab->dump_file)
1231 128 : fprintf (symtab->dump_file, " %s", node->dump_asm_name ());
1232 : }
1233 146382191 : if (node == first_analyzed
1234 146382191 : || node == first_analyzed_var)
1235 : break;
1236 : }
1237 781540 : symtab->process_new_functions ();
1238 781540 : first_analyzed_var = symtab->first_variable ();
1239 781540 : first_analyzed = symtab->first_function ();
1240 :
1241 781540 : if (changed && symtab->dump_file)
1242 75 : fprintf (symtab->dump_file, "\n");
1243 :
1244 : /* Lower representation, build callgraph edges and references for all trivially
1245 : needed symbols and all symbols referred by them. */
1246 7265540 : while (queued_nodes != &symtab_terminator)
1247 : {
1248 6484013 : changed = true;
1249 6484013 : node = queued_nodes;
1250 6484013 : queued_nodes = (symtab_node *)queued_nodes->aux;
1251 6484013 : cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1252 3416550 : if (cnode && cnode->definition)
1253 : {
1254 3413309 : cgraph_edge *edge;
1255 3413309 : tree decl = cnode->decl;
1256 :
1257 : /* ??? It is possible to create extern inline function
1258 : and later using weak alias attribute to kill its body.
1259 : See gcc.c-torture/compile/20011119-1.c */
1260 3413309 : if (!DECL_STRUCT_FUNCTION (decl)
1261 15365 : && !cnode->alias
1262 3525 : && !cnode->thunk
1263 3413423 : && !cnode->dispatcher_function)
1264 : {
1265 0 : cnode->reset ();
1266 0 : cnode->redefined_extern_inline = true;
1267 0 : continue;
1268 : }
1269 :
1270 3413309 : if (!cnode->analyzed)
1271 3057154 : cnode->analyze ();
1272 :
1273 : /* A reference to a default node in a function set implies a
1274 : reference to all versions in the set. */
1275 3413296 : cgraph_function_version_info *node_v = cnode->function_version ();
1276 3413296 : if (node_v && is_function_default_version (node->decl))
1277 129 : for (cgraph_function_version_info *fvi = node_v->next;
1278 750 : fvi;
1279 621 : fvi = fvi->next)
1280 621 : enqueue_node (fvi->this_node);
1281 :
1282 13586904 : for (edge = cnode->callees; edge; edge = edge->next_callee)
1283 10173608 : if (edge->callee->definition
1284 10173608 : && (!DECL_EXTERNAL (edge->callee->decl)
1285 : /* When not optimizing, do not try to analyze extern
1286 : inline functions. Doing so is pointless. */
1287 636287 : || opt_for_fn (edge->callee->decl, optimize)
1288 : /* Weakrefs needs to be preserved. */
1289 4985 : || edge->callee->alias
1290 : /* always_inline functions are inlined even at -O0. */
1291 3355 : || lookup_attribute
1292 3355 : ("always_inline",
1293 3355 : DECL_ATTRIBUTES (edge->callee->decl))
1294 : /* Multiversioned functions needs the dispatcher to
1295 : be produced locally even for extern functions. */
1296 2002 : || edge->callee->function_version ()))
1297 5286655 : enqueue_node (edge->callee);
1298 3413296 : if (opt_for_fn (cnode->decl, optimize)
1299 3413296 : && opt_for_fn (cnode->decl, flag_devirtualize))
1300 : {
1301 2767534 : cgraph_edge *next;
1302 :
1303 2900794 : for (edge = cnode->indirect_calls; edge; edge = next)
1304 : {
1305 133260 : next = edge->next_callee;
1306 133260 : if (is_a <cgraph_polymorphic_indirect_info *>
1307 266520 : (edge->indirect_info))
1308 19320 : walk_polymorphic_call_targets (&reachable_call_targets,
1309 : edge);
1310 : }
1311 : }
1312 :
1313 : /* If decl is a clone of an abstract function,
1314 : mark that abstract function so that we don't release its body.
1315 : The DECL_INITIAL() of that abstract function declaration
1316 : will be later needed to output debug info. */
1317 3413296 : if (DECL_ABSTRACT_ORIGIN (decl))
1318 : {
1319 734192 : cgraph_node *origin_node
1320 734192 : = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl));
1321 734192 : origin_node->used_as_abstract_origin = true;
1322 : }
1323 : /* Preserve a functions function context node. It will
1324 : later be needed to output debug info. */
1325 3413296 : if (tree fn = decl_function_context (decl))
1326 : {
1327 129295 : cgraph_node *origin_node = cgraph_node::get_create (fn);
1328 129295 : enqueue_node (origin_node);
1329 : }
1330 : }
1331 : else
1332 : {
1333 3070704 : varpool_node *vnode = dyn_cast <varpool_node *> (node);
1334 3067463 : if (vnode && vnode->definition && !vnode->analyzed)
1335 3067435 : vnode->analyze ();
1336 : }
1337 :
1338 6484000 : if (node->same_comdat_group)
1339 : {
1340 : symtab_node *next;
1341 938840 : for (next = node->same_comdat_group;
1342 1612131 : next != node;
1343 938840 : next = next->same_comdat_group)
1344 1877680 : if (!next->comdat_local_p ())
1345 926595 : enqueue_node (next);
1346 : }
1347 18026486 : for (i = 0; node->iterate_reference (i, ref); i++)
1348 11542486 : if (ref->referred->definition
1349 11542486 : && (!DECL_EXTERNAL (ref->referred->decl)
1350 91162 : || ((TREE_CODE (ref->referred->decl) != FUNCTION_DECL
1351 26324 : && optimize)
1352 65453 : || (TREE_CODE (ref->referred->decl) == FUNCTION_DECL
1353 64838 : && opt_for_fn (ref->referred->decl, optimize))
1354 1499 : || node->alias
1355 1007 : || ref->referred->alias)))
1356 7585352 : enqueue_node (ref->referred);
1357 6484000 : symtab->process_new_functions ();
1358 : }
1359 : }
1360 531434 : update_type_inheritance_graph ();
1361 :
1362 : /* Collect entry points to the unit. */
1363 531434 : if (symtab->dump_file)
1364 : {
1365 150 : fprintf (symtab->dump_file, "\n\nInitial ");
1366 150 : symtab->dump (symtab->dump_file);
1367 : }
1368 :
1369 531434 : if (first_time)
1370 : {
1371 265717 : symtab_node *snode;
1372 146672224 : FOR_EACH_SYMBOL (snode)
1373 146406507 : check_global_declaration (snode);
1374 : }
1375 :
1376 531434 : if (symtab->dump_file)
1377 150 : fprintf (symtab->dump_file, "\nRemoving unused symbols:");
1378 :
1379 531434 : for (node = symtab->first_symbol ();
1380 146937959 : node != first_handled
1381 146937959 : && node != first_handled_var; node = next)
1382 : {
1383 146406525 : next = safe_as_a<symtab_node *>(node->next);
1384 : /* For symbols declared locally we clear TREE_READONLY when emitting
1385 : the constructor (if one is needed). For external declarations we can
1386 : not safely assume that the type is readonly because we may be called
1387 : during its construction. */
1388 146406525 : if (TREE_CODE (node->decl) == VAR_DECL
1389 37218247 : && TYPE_P (TREE_TYPE (node->decl))
1390 37217176 : && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (node->decl))
1391 147132381 : && DECL_EXTERNAL (node->decl))
1392 338045 : TREE_READONLY (node->decl) = 0;
1393 146406525 : if (!node->aux && !node->referred_to_p () && !node->ref_by_asm)
1394 : {
1395 137762487 : if (symtab->dump_file)
1396 21 : fprintf (symtab->dump_file, " %s", node->dump_name ());
1397 :
1398 : /* See if the debugger can use anything before the DECL
1399 : passes away. Perhaps it can notice a DECL that is now a
1400 : constant and can tag the early DIE with an appropriate
1401 : attribute.
1402 :
1403 : Otherwise, this is the last chance the debug_hooks have
1404 : at looking at optimized away DECLs, since
1405 : late_global_decl will subsequently be called from the
1406 : contents of the now pruned symbol table. */
1407 137762487 : if (VAR_P (node->decl)
1408 137762487 : && !decl_function_context (node->decl))
1409 : {
1410 : /* We are reclaiming totally unreachable code and variables
1411 : so they effectively appear as readonly. Show that to
1412 : the debug machinery. */
1413 33716422 : TREE_READONLY (node->decl) = 1;
1414 33716422 : node->definition = false;
1415 33716422 : (*debug_hooks->late_global_decl) (node->decl);
1416 : }
1417 :
1418 137762487 : node->remove ();
1419 137762487 : continue;
1420 : }
1421 8644038 : if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1422 : {
1423 5278662 : tree decl = node->decl;
1424 :
1425 3414290 : if (cnode->definition && !gimple_has_body_p (decl)
1426 364388 : && !cnode->alias
1427 5282989 : && !cnode->thunk)
1428 1027 : cnode->reset ();
1429 :
1430 5278662 : gcc_assert (!cnode->definition || cnode->thunk
1431 : || cnode->alias
1432 : || gimple_has_body_p (decl)
1433 : || cnode->native_rtl_p ());
1434 5278662 : gcc_assert (cnode->analyzed == cnode->definition);
1435 : }
1436 8644038 : node->aux = NULL;
1437 : }
1438 9175471 : for (;node; node = safe_as_a<symtab_node *>(node->next))
1439 8644037 : node->aux = NULL;
1440 531434 : first_analyzed = symtab->first_function ();
1441 531434 : first_analyzed_var = symtab->first_variable ();
1442 531434 : if (symtab->dump_file)
1443 : {
1444 150 : fprintf (symtab->dump_file, "\n\nReclaimed ");
1445 150 : symtab->dump (symtab->dump_file);
1446 : }
1447 531434 : bitmap_obstack_release (NULL);
1448 531434 : ggc_collect ();
1449 : /* Initialize assembler name hash, in particular we want to trigger C++
1450 : mangling and same body alias creation before we free DECL_ARGUMENTS
1451 : used by it. */
1452 531434 : if (!seen_error ())
1453 478145 : symtab->symtab_initialize_asm_name_hash ();
1454 :
1455 531434 : input_location = saved_loc;
1456 531434 : }
1457 :
1458 : /* Check declaration of the type of ALIAS for compatibility with its TARGET
1459 : (which may be an ifunc resolver) and issue a diagnostic when they are
1460 : not compatible according to language rules (plus a C++ extension for
1461 : non-static member functions). */
1462 :
1463 : static void
1464 5160 : maybe_diag_incompatible_alias (tree alias, tree target)
1465 : {
1466 5160 : tree altype = TREE_TYPE (alias);
1467 5160 : tree targtype = TREE_TYPE (target);
1468 :
1469 5160 : bool ifunc = cgraph_node::get (alias)->ifunc_resolver;
1470 5160 : tree funcptr = altype;
1471 :
1472 5160 : if (ifunc)
1473 : {
1474 : /* Handle attribute ifunc first. */
1475 113 : if (TREE_CODE (altype) == METHOD_TYPE)
1476 : {
1477 : /* Set FUNCPTR to the type of the alias target. If the type
1478 : is a non-static member function of class C, construct a type
1479 : of an ordinary function taking C* as the first argument,
1480 : followed by the member function argument list, and use it
1481 : instead to check for incompatibility. This conversion is
1482 : not defined by the language but an extension provided by
1483 : G++. */
1484 :
1485 21 : tree rettype = TREE_TYPE (altype);
1486 21 : tree args = TYPE_ARG_TYPES (altype);
1487 21 : altype = build_function_type (rettype, args);
1488 21 : funcptr = altype;
1489 : }
1490 :
1491 113 : targtype = TREE_TYPE (targtype);
1492 :
1493 113 : if (POINTER_TYPE_P (targtype))
1494 : {
1495 109 : targtype = TREE_TYPE (targtype);
1496 :
1497 : /* Only issue Wattribute-alias for conversions to void* with
1498 : -Wextra. */
1499 109 : if (VOID_TYPE_P (targtype) && !extra_warnings)
1500 : return;
1501 :
1502 : /* Proceed to handle incompatible ifunc resolvers below. */
1503 : }
1504 : else
1505 : {
1506 4 : funcptr = build_pointer_type (funcptr);
1507 :
1508 4 : error_at (DECL_SOURCE_LOCATION (target),
1509 : "%<ifunc%> resolver for %qD must return %qT",
1510 : alias, funcptr);
1511 4 : inform (DECL_SOURCE_LOCATION (alias),
1512 : "resolver indirect function declared here");
1513 4 : return;
1514 : }
1515 : }
1516 :
1517 5156 : if ((!FUNC_OR_METHOD_TYPE_P (targtype)
1518 5156 : || (prototype_p (altype)
1519 5030 : && prototype_p (targtype)
1520 5028 : && !types_compatible_p (altype, targtype))))
1521 : {
1522 : /* Warn for incompatibilities. Avoid warning for functions
1523 : without a prototype to make it possible to declare aliases
1524 : without knowing the exact type, as libstdc++ does. */
1525 16 : if (ifunc)
1526 : {
1527 5 : funcptr = build_pointer_type (funcptr);
1528 :
1529 5 : auto_diagnostic_group d;
1530 5 : if (warning_at (DECL_SOURCE_LOCATION (target),
1531 5 : OPT_Wattribute_alias_,
1532 : "%<ifunc%> resolver for %qD should return %qT",
1533 : alias, funcptr))
1534 5 : inform (DECL_SOURCE_LOCATION (alias),
1535 : "resolver indirect function declared here");
1536 5 : }
1537 : else
1538 : {
1539 11 : auto_diagnostic_group d;
1540 11 : if (warning_at (DECL_SOURCE_LOCATION (alias),
1541 11 : OPT_Wattribute_alias_,
1542 : "%qD alias between functions of incompatible "
1543 : "types %qT and %qT", alias, altype, targtype))
1544 3 : inform (DECL_SOURCE_LOCATION (target),
1545 : "aliased declaration here");
1546 11 : }
1547 : }
1548 : }
1549 :
1550 : /* Translate the ugly representation of aliases as alias pairs into nice
1551 : representation in callgraph. We don't handle all cases yet,
1552 : unfortunately. */
1553 :
1554 : static void
1555 551514 : handle_alias_pairs (void)
1556 : {
1557 551514 : alias_pair *p;
1558 551514 : unsigned i;
1559 :
1560 557227 : for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1561 : {
1562 5713 : symtab_node *target_node = symtab_node::get_for_asmname (p->target);
1563 :
1564 : /* Weakrefs with target not defined in current unit are easy to handle:
1565 : they behave just as external variables except we need to note the
1566 : alias flag to later output the weakref pseudo op into asm file. */
1567 5713 : if (!target_node
1568 5713 : && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1569 : {
1570 402 : symtab_node *node = symtab_node::get (p->decl);
1571 402 : if (node)
1572 : {
1573 402 : node->alias_target = p->target;
1574 402 : node->weakref = true;
1575 402 : node->alias = true;
1576 402 : node->transparent_alias = true;
1577 : }
1578 402 : alias_pairs->unordered_remove (i);
1579 402 : continue;
1580 402 : }
1581 5311 : else if (!target_node)
1582 : {
1583 8 : error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1584 8 : symtab_node *node = symtab_node::get (p->decl);
1585 8 : if (node)
1586 8 : node->alias = false;
1587 8 : alias_pairs->unordered_remove (i);
1588 8 : continue;
1589 8 : }
1590 :
1591 5303 : if (DECL_EXTERNAL (target_node->decl)
1592 : /* We use local aliases for C++ thunks to force the tailcall
1593 : to bind locally. This is a hack - to keep it working do
1594 : the following (which is not strictly correct). */
1595 24 : && (TREE_CODE (target_node->decl) != FUNCTION_DECL
1596 24 : || ! DECL_VIRTUAL_P (target_node->decl))
1597 5327 : && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1598 : {
1599 0 : error ("%q+D aliased to external symbol %qE",
1600 : p->decl, p->target);
1601 : }
1602 :
1603 5303 : if (TREE_CODE (p->decl) == FUNCTION_DECL
1604 5303 : && target_node && is_a <cgraph_node *> (target_node))
1605 : {
1606 5160 : maybe_diag_incompatible_alias (p->decl, target_node->decl);
1607 :
1608 5160 : maybe_diag_alias_attributes (p->decl, target_node->decl);
1609 :
1610 5160 : cgraph_node *src_node = cgraph_node::get (p->decl);
1611 5160 : if (src_node && src_node->definition)
1612 18 : src_node->reset ();
1613 5160 : cgraph_node::create_alias (p->decl, target_node->decl);
1614 5160 : alias_pairs->unordered_remove (i);
1615 : }
1616 143 : else if (VAR_P (p->decl)
1617 143 : && target_node && is_a <varpool_node *> (target_node))
1618 : {
1619 142 : varpool_node::create_alias (p->decl, target_node->decl);
1620 142 : alias_pairs->unordered_remove (i);
1621 : }
1622 : else
1623 : {
1624 1 : error ("%q+D alias between function and variable is not supported",
1625 : p->decl);
1626 1 : inform (DECL_SOURCE_LOCATION (target_node->decl),
1627 : "aliased declaration here");
1628 :
1629 1 : alias_pairs->unordered_remove (i);
1630 : }
1631 : }
1632 551514 : vec_free (alias_pairs);
1633 551514 : }
1634 :
1635 :
1636 : /* Figure out what functions we want to assemble. */
1637 :
1638 : static void
1639 239742 : mark_functions_to_output (void)
1640 : {
1641 239742 : bool check_same_comdat_groups = false;
1642 239742 : cgraph_node *node;
1643 :
1644 239742 : if (flag_checking)
1645 5067778 : FOR_EACH_FUNCTION (node)
1646 4828050 : gcc_assert (!node->process);
1647 :
1648 5067839 : FOR_EACH_FUNCTION (node)
1649 : {
1650 4828097 : tree decl = node->decl;
1651 :
1652 4828097 : gcc_assert (!node->process || node->same_comdat_group);
1653 4828097 : if (node->process)
1654 11476 : continue;
1655 :
1656 : /* We need to output all local functions that are used and not
1657 : always inlined, as well as those that are reachable from
1658 : outside the current compilation unit. */
1659 4816621 : if (node->analyzed
1660 3003207 : && !node->thunk
1661 3001137 : && !node->alias
1662 2937157 : && !node->inlined_to
1663 1502562 : && !TREE_ASM_WRITTEN (decl)
1664 6319183 : && !DECL_EXTERNAL (decl))
1665 : {
1666 1502562 : node->process = 1;
1667 1502562 : if (node->same_comdat_group)
1668 : {
1669 47011 : cgraph_node *next;
1670 47011 : for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
1671 125771 : next != node;
1672 78760 : next = dyn_cast<cgraph_node *> (next->same_comdat_group))
1673 77245 : if (!next->thunk && !next->alias
1674 193037 : && !next->comdat_local_p ())
1675 14786 : next->process = 1;
1676 : }
1677 : }
1678 3314059 : else if (node->same_comdat_group)
1679 : {
1680 41540 : if (flag_checking)
1681 4828097 : check_same_comdat_groups = true;
1682 : }
1683 : else
1684 : {
1685 : /* We should've reclaimed all functions that are not needed. */
1686 3272519 : if (flag_checking
1687 3272492 : && !node->inlined_to
1688 1837750 : && gimple_has_body_p (decl)
1689 : /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1690 : are inside partition, we can end up not removing the body since we no longer
1691 : have analyzed node pointing to it. */
1692 135585 : && !node->in_other_partition
1693 135585 : && !node->alias
1694 135585 : && !node->clones
1695 3272519 : && !DECL_EXTERNAL (decl))
1696 : {
1697 0 : node->debug ();
1698 0 : internal_error ("failed to reclaim unneeded function");
1699 : }
1700 3272519 : gcc_assert (node->inlined_to
1701 : || !gimple_has_body_p (decl)
1702 : || node->in_other_partition
1703 : || node->clones
1704 : || DECL_ARTIFICIAL (decl)
1705 : || DECL_EXTERNAL (decl));
1706 :
1707 : }
1708 :
1709 : }
1710 239742 : if (flag_checking && check_same_comdat_groups)
1711 1442514 : FOR_EACH_FUNCTION (node)
1712 1431578 : if (node->same_comdat_group && !node->process)
1713 : {
1714 41540 : tree decl = node->decl;
1715 41540 : if (!node->inlined_to
1716 41540 : && gimple_has_body_p (decl)
1717 : /* FIXME: in an ltrans unit when the offline copy is outside a
1718 : partition but inline copies are inside a partition, we can
1719 : end up not removing the body since we no longer have an
1720 : analyzed node pointing to it. */
1721 1 : && !node->in_other_partition
1722 1 : && !node->clones
1723 41540 : && !DECL_EXTERNAL (decl))
1724 : {
1725 0 : node->debug ();
1726 0 : internal_error ("failed to reclaim unneeded function in same "
1727 : "comdat group");
1728 : }
1729 : }
1730 239742 : }
1731 :
1732 : /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1733 : in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1734 :
1735 : Set current_function_decl and cfun to newly constructed empty function body.
1736 : return basic block in the function body. */
1737 :
1738 : basic_block
1739 20933 : init_lowered_empty_function (tree decl, bool in_ssa, profile_count count)
1740 : {
1741 20933 : basic_block bb;
1742 20933 : edge e;
1743 :
1744 20933 : current_function_decl = decl;
1745 20933 : allocate_struct_function (decl, false);
1746 20933 : gimple_register_cfg_hooks ();
1747 20933 : init_empty_tree_cfg ();
1748 20933 : init_tree_ssa (cfun);
1749 :
1750 20933 : if (in_ssa)
1751 : {
1752 20729 : init_ssa_operands (cfun);
1753 20729 : cfun->gimple_df->in_ssa_p = true;
1754 20729 : cfun->curr_properties |= PROP_ssa;
1755 : }
1756 :
1757 20933 : DECL_INITIAL (decl) = make_node (BLOCK);
1758 20933 : BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl;
1759 :
1760 20933 : DECL_SAVED_TREE (decl) = error_mark_node;
1761 20933 : cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1762 : | PROP_cfg | PROP_loops);
1763 :
1764 20933 : set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ());
1765 20933 : init_loops_structure (cfun, loops_for_fn (cfun), 1);
1766 20933 : loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1767 :
1768 : /* Create BB for body of the function and connect it properly. */
1769 20933 : ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = count;
1770 20933 : EXIT_BLOCK_PTR_FOR_FN (cfun)->count = count;
1771 20933 : bb = create_basic_block (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun));
1772 20933 : bb->count = count;
1773 20933 : e = make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU);
1774 20933 : e->probability = profile_probability::always ();
1775 20933 : e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1776 20933 : e->probability = profile_probability::always ();
1777 20933 : add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
1778 :
1779 20933 : return bb;
1780 : }
1781 :
1782 : /* Assemble thunks and aliases associated to node. */
1783 :
1784 : void
1785 1582669 : cgraph_node::assemble_thunks_and_aliases (void)
1786 : {
1787 1582669 : cgraph_edge *e;
1788 1582669 : ipa_ref *ref;
1789 :
1790 3175406 : for (e = callers; e;)
1791 1592737 : if (e->caller->thunk
1792 1940 : && !e->caller->inlined_to)
1793 : {
1794 1916 : cgraph_node *thunk = e->caller;
1795 :
1796 1916 : e = e->next_caller;
1797 1916 : expand_thunk (thunk, !rtl_dump_and_exit, false);
1798 1916 : thunk->assemble_thunks_and_aliases ();
1799 1916 : }
1800 : else
1801 1590821 : e = e->next_caller;
1802 :
1803 1645464 : FOR_EACH_ALIAS (this, ref)
1804 : {
1805 62795 : cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1806 62795 : if (!alias->transparent_alias)
1807 : {
1808 62789 : bool saved_written = TREE_ASM_WRITTEN (decl);
1809 :
1810 : /* Force assemble_alias to really output the alias this time instead
1811 : of buffering it in same alias pairs. */
1812 62789 : TREE_ASM_WRITTEN (decl) = 1;
1813 62789 : if (alias->symver)
1814 2 : do_assemble_symver (alias->decl,
1815 : DECL_ASSEMBLER_NAME (decl));
1816 : else
1817 62787 : do_assemble_alias (alias->decl,
1818 : DECL_ASSEMBLER_NAME (decl));
1819 62789 : alias->assemble_thunks_and_aliases ();
1820 62789 : TREE_ASM_WRITTEN (decl) = saved_written;
1821 : }
1822 : }
1823 1582669 : }
1824 :
1825 : /* Expand function specified by node. */
1826 :
1827 : void
1828 1518118 : cgraph_node::expand (void)
1829 : {
1830 1518118 : location_t saved_loc;
1831 :
1832 : /* We ought to not compile any inline clones. */
1833 1518118 : gcc_assert (!inlined_to);
1834 :
1835 : /* __RTL functions are compiled as soon as they are parsed, so don't
1836 : do it again. */
1837 1518118 : if (native_rtl_p ())
1838 : return;
1839 :
1840 1518118 : announce_function (decl);
1841 1518118 : process = 0;
1842 1518118 : gcc_assert (lowered);
1843 :
1844 : /* Initialize the default bitmap obstack. */
1845 1518118 : bitmap_obstack_initialize (NULL);
1846 1518118 : get_untransformed_body ();
1847 :
1848 : /* Generate RTL for the body of DECL. */
1849 :
1850 1518118 : timevar_push (TV_REST_OF_COMPILATION);
1851 :
1852 1518118 : gcc_assert (symtab->global_info_ready);
1853 :
1854 : /* Initialize the RTL code for the function. */
1855 1518118 : saved_loc = input_location;
1856 1518118 : input_location = DECL_SOURCE_LOCATION (decl);
1857 :
1858 1518118 : gcc_assert (DECL_STRUCT_FUNCTION (decl));
1859 1518118 : push_cfun (DECL_STRUCT_FUNCTION (decl));
1860 1518118 : init_function_start (decl);
1861 :
1862 1518118 : gimple_register_cfg_hooks ();
1863 :
1864 1518118 : bitmap_obstack_initialize (®_obstack); /* FIXME, only at RTL generation*/
1865 :
1866 1518118 : update_ssa (TODO_update_ssa_only_virtuals);
1867 1518118 : if (ipa_transforms_to_apply.exists ())
1868 1497034 : execute_all_ipa_transforms (false);
1869 :
1870 : /* Perform all tree transforms and optimizations. */
1871 :
1872 : /* Signal the start of passes. */
1873 1518118 : invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1874 :
1875 1518118 : execute_pass_list (cfun, g->get_passes ()->all_passes);
1876 :
1877 : /* Signal the end of passes. */
1878 1518108 : invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1879 :
1880 1518108 : bitmap_obstack_release (®_obstack);
1881 :
1882 : /* Release the default bitmap obstack. */
1883 1518108 : bitmap_obstack_release (NULL);
1884 :
1885 : /* If requested, warn about function definitions where the function will
1886 : return a value (usually of some struct or union type) which itself will
1887 : take up a lot of stack space. */
1888 1518108 : if (!DECL_EXTERNAL (decl) && TREE_TYPE (decl))
1889 : {
1890 1518108 : tree ret_type = TREE_TYPE (TREE_TYPE (decl));
1891 :
1892 1518108 : if (ret_type && TYPE_SIZE_UNIT (ret_type)
1893 809291 : && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1894 2327360 : && compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1895 809252 : warn_larger_than_size) > 0)
1896 : {
1897 0 : unsigned int size_as_int
1898 0 : = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1899 :
1900 0 : if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1901 0 : warning (OPT_Wlarger_than_,
1902 : "size of return value of %q+D is %u bytes",
1903 : decl, size_as_int);
1904 : else
1905 0 : warning (OPT_Wlarger_than_,
1906 : "size of return value of %q+D is larger than %wu bytes",
1907 : decl, warn_larger_than_size);
1908 : }
1909 : }
1910 :
1911 1518108 : gimple_set_body (decl, NULL);
1912 1518108 : if (DECL_STRUCT_FUNCTION (decl) == 0)
1913 : {
1914 : /* Stop pointing to the local nodes about to be freed.
1915 : But DECL_INITIAL must remain nonzero so we know this
1916 : was an actual function definition. */
1917 62 : if (DECL_INITIAL (decl) != 0)
1918 62 : DECL_INITIAL (decl) = error_mark_node;
1919 : }
1920 :
1921 1518108 : input_location = saved_loc;
1922 :
1923 1518108 : ggc_collect ();
1924 1518108 : timevar_pop (TV_REST_OF_COMPILATION);
1925 :
1926 1518108 : if (DECL_STRUCT_FUNCTION (decl)
1927 1518108 : && DECL_STRUCT_FUNCTION (decl)->assume_function)
1928 : {
1929 : /* Assume functions aren't expanded into RTL, on the other side
1930 : we don't want to release their body. */
1931 144 : if (cfun)
1932 0 : pop_cfun ();
1933 : return;
1934 : }
1935 :
1936 : /* Make sure that BE didn't give up on compiling. */
1937 1517964 : gcc_assert (TREE_ASM_WRITTEN (decl));
1938 1517964 : if (cfun)
1939 1517902 : pop_cfun ();
1940 :
1941 : /* It would make a lot more sense to output thunks before function body to
1942 : get more forward and fewer backward jumps. This however would need
1943 : solving problem with comdats. See PR48668. Also aliases must come after
1944 : function itself to make one pass assemblers, like one on AIX, happy.
1945 : See PR 50689.
1946 : FIXME: Perhaps thunks should be move before function IFF they are not in
1947 : comdat groups. */
1948 1517964 : assemble_thunks_and_aliases ();
1949 1517964 : release_body ();
1950 : }
1951 :
1952 : /* Node comparator that is responsible for the order that corresponds
1953 : to time when a function was launched for the first time. */
1954 :
1955 : int
1956 871859 : tp_first_run_node_cmp (const void *pa, const void *pb)
1957 : {
1958 871859 : const cgraph_node *a = *(const cgraph_node * const *) pa;
1959 871859 : const cgraph_node *b = *(const cgraph_node * const *) pb;
1960 871859 : unsigned int tp_first_run_a = a->tp_first_run;
1961 871859 : unsigned int tp_first_run_b = b->tp_first_run;
1962 :
1963 871859 : if (!opt_for_fn (a->decl, flag_profile_reorder_functions)
1964 871859 : || a->no_reorder)
1965 : tp_first_run_a = 0;
1966 871859 : if (!opt_for_fn (b->decl, flag_profile_reorder_functions)
1967 871859 : || b->no_reorder)
1968 : tp_first_run_b = 0;
1969 :
1970 871859 : if (tp_first_run_a == tp_first_run_b)
1971 869315 : return a->order - b->order;
1972 :
1973 : /* Functions with time profile must be before these without profile. */
1974 2544 : tp_first_run_a = (tp_first_run_a - 1) & INT_MAX;
1975 2544 : tp_first_run_b = (tp_first_run_b - 1) & INT_MAX;
1976 :
1977 2544 : return tp_first_run_a - tp_first_run_b;
1978 : }
1979 :
1980 : /* Expand all functions that must be output.
1981 :
1982 : Attempt to topologically sort the nodes so function is output when
1983 : all called functions are already assembled to allow data to be
1984 : propagated across the callgraph. Use a stack to get smaller distance
1985 : between a function and its callees (later we may choose to use a more
1986 : sophisticated algorithm for function reordering; we will likely want
1987 : to use subsections to make the output functions appear in top-down
1988 : order). */
1989 :
1990 : static void
1991 239732 : expand_all_functions (void)
1992 : {
1993 239732 : cgraph_node *node;
1994 239732 : cgraph_node **order = XCNEWVEC (cgraph_node *,
1995 : symtab->cgraph_count);
1996 239732 : cgraph_node **tp_first_run_order = XCNEWVEC (cgraph_node *,
1997 : symtab->cgraph_count);
1998 239732 : unsigned int expanded_func_count = 0, profiled_func_count = 0;
1999 239732 : int order_pos, tp_first_run_order_pos = 0, new_order_pos = 0;
2000 239732 : int i;
2001 :
2002 239732 : order_pos = ipa_reverse_postorder (order);
2003 239732 : gcc_assert (order_pos == symtab->cgraph_count);
2004 :
2005 : /* Garbage collector may remove inline clones we eliminate during
2006 : optimization. So we must be sure to not reference them. */
2007 5033462 : for (i = 0; i < order_pos; i++)
2008 4793730 : if (order[i]->process)
2009 : {
2010 964224 : if (order[i]->tp_first_run
2011 964224 : && opt_for_fn (order[i]->decl, flag_profile_reorder_functions))
2012 283 : tp_first_run_order[tp_first_run_order_pos++] = order[i];
2013 : else
2014 963941 : order[new_order_pos++] = order[i];
2015 : }
2016 :
2017 : /* First output functions with time profile in specified order. */
2018 239732 : qsort (tp_first_run_order, tp_first_run_order_pos,
2019 : sizeof (cgraph_node *), tp_first_run_node_cmp);
2020 479747 : for (i = 0; i < tp_first_run_order_pos; i++)
2021 : {
2022 283 : node = tp_first_run_order[i];
2023 :
2024 283 : if (node->process)
2025 : {
2026 283 : expanded_func_count++;
2027 283 : profiled_func_count++;
2028 :
2029 283 : if (symtab->dump_file)
2030 0 : fprintf (symtab->dump_file,
2031 : "Time profile order in expand_all_functions:%s:%d\n",
2032 : node->dump_asm_name (), node->tp_first_run);
2033 283 : node->process = 0;
2034 283 : node->expand ();
2035 : }
2036 : }
2037 :
2038 : /* Output functions in RPO so callees get optimized before callers. This
2039 : makes ipa-ra and other propagators to work.
2040 : FIXME: This is far from optimal code layout.
2041 : Make multiple passes over the list to defer processing of gc
2042 : candidates until all potential uses are seen. */
2043 : int gc_candidates = 0;
2044 : int prev_gc_candidates = 0;
2045 :
2046 239780 : while (1)
2047 : {
2048 1204312 : for (i = new_order_pos - 1; i >= 0; i--)
2049 : {
2050 964532 : node = order[i];
2051 :
2052 964532 : if (node->gc_candidate)
2053 159 : gc_candidates++;
2054 964373 : else if (node->process)
2055 : {
2056 963881 : expanded_func_count++;
2057 963881 : node->process = 0;
2058 963881 : node->expand ();
2059 : }
2060 : }
2061 239780 : if (!gc_candidates || gc_candidates == prev_gc_candidates)
2062 : break;
2063 : prev_gc_candidates = gc_candidates;
2064 : gc_candidates = 0;
2065 : }
2066 :
2067 : /* Free any unused gc_candidate functions. */
2068 239732 : if (gc_candidates)
2069 374 : for (i = new_order_pos - 1; i >= 0; i--)
2070 : {
2071 342 : node = order[i];
2072 342 : if (node->gc_candidate)
2073 : {
2074 60 : struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
2075 60 : if (symtab->dump_file)
2076 8 : fprintf (symtab->dump_file,
2077 : "Deleting unused function %s\n",
2078 4 : IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
2079 60 : node->process = false;
2080 60 : free_dominance_info (fn, CDI_DOMINATORS);
2081 60 : free_dominance_info (fn, CDI_POST_DOMINATORS);
2082 60 : node->release_body (false);
2083 : }
2084 : }
2085 :
2086 239732 : if (dump_file)
2087 0 : fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
2088 : main_input_filename, profiled_func_count, expanded_func_count);
2089 :
2090 239732 : if (symtab->dump_file && tp_first_run_order_pos)
2091 0 : fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n",
2092 : profiled_func_count, expanded_func_count);
2093 :
2094 239732 : symtab->process_new_functions ();
2095 239732 : free_gimplify_stack ();
2096 239732 : delete ipa_saved_clone_sources;
2097 239732 : ipa_saved_clone_sources = NULL;
2098 239732 : free (order);
2099 239732 : free (tp_first_run_order);
2100 239732 : }
2101 :
2102 : /* This is used to sort the node types by the cgraph order number. */
2103 :
2104 : enum cgraph_order_sort_kind
2105 : {
2106 : ORDER_FUNCTION,
2107 : ORDER_VAR,
2108 : ORDER_VAR_UNDEF,
2109 : ORDER_ASM
2110 : };
2111 :
2112 : struct cgraph_order_sort
2113 : {
2114 : /* Construct from a cgraph_node. */
2115 549814 : cgraph_order_sort (cgraph_node *node)
2116 549814 : : kind (ORDER_FUNCTION), order (node->order)
2117 : {
2118 549814 : u.f = node;
2119 : }
2120 :
2121 : /* Construct from a varpool_node. */
2122 1002119 : cgraph_order_sort (varpool_node *node)
2123 2004238 : : kind (node->definition ? ORDER_VAR : ORDER_VAR_UNDEF), order (node->order)
2124 : {
2125 1002119 : u.v = node;
2126 : }
2127 :
2128 : /* Construct from a asm_node. */
2129 12485 : cgraph_order_sort (asm_node *node)
2130 12485 : : kind (ORDER_ASM), order (node->order)
2131 : {
2132 12485 : u.a = node;
2133 : }
2134 :
2135 : /* Assembly cgraph_order_sort based on its type. */
2136 : void process ();
2137 :
2138 : enum cgraph_order_sort_kind kind;
2139 : union
2140 : {
2141 : cgraph_node *f;
2142 : varpool_node *v;
2143 : asm_node *a;
2144 : } u;
2145 : int order;
2146 : };
2147 :
2148 : /* Assembly cgraph_order_sort based on its type. */
2149 :
2150 : void
2151 1564388 : cgraph_order_sort::process ()
2152 : {
2153 1564388 : switch (kind)
2154 : {
2155 549784 : case ORDER_FUNCTION:
2156 549784 : u.f->process = 0;
2157 549784 : u.f->expand ();
2158 549784 : break;
2159 1001433 : case ORDER_VAR:
2160 1001433 : u.v->assemble_decl ();
2161 1001433 : break;
2162 686 : case ORDER_VAR_UNDEF:
2163 686 : assemble_undefined_decl (u.v->decl);
2164 686 : break;
2165 12485 : case ORDER_ASM:
2166 12485 : assemble_asm (u.a->asm_str);
2167 12485 : break;
2168 0 : default:
2169 0 : gcc_unreachable ();
2170 : }
2171 1564378 : }
2172 :
2173 : /* Compare cgraph_order_sort by order. */
2174 :
2175 : static int
2176 63845351 : cgraph_order_cmp (const void *a_p, const void *b_p)
2177 : {
2178 63845351 : const cgraph_order_sort *nodea = (const cgraph_order_sort *)a_p;
2179 63845351 : const cgraph_order_sort *nodeb = (const cgraph_order_sort *)b_p;
2180 :
2181 63845351 : return nodea->order - nodeb->order;
2182 : }
2183 :
2184 : /* Output all functions, variables, and asm statements in the order
2185 : according to their order fields, which is the order in which they
2186 : appeared in the file. This implements -fno-toplevel-reorder. In
2187 : this mode we may output functions and variables which don't really
2188 : need to be output. */
2189 :
2190 : static void
2191 239742 : output_in_order (void)
2192 : {
2193 239742 : int i;
2194 239742 : cgraph_node *cnode;
2195 239742 : varpool_node *vnode;
2196 239742 : asm_node *anode;
2197 239742 : auto_vec<cgraph_order_sort> nodes;
2198 239742 : cgraph_order_sort *node;
2199 :
2200 3254426 : FOR_EACH_DEFINED_FUNCTION (cnode)
2201 3014684 : if (cnode->process && !cnode->thunk
2202 1514038 : && !cnode->alias && cnode->no_reorder)
2203 549814 : nodes.safe_push (cgraph_order_sort (cnode));
2204 :
2205 : /* There is a similar loop in symbol_table::output_variables.
2206 : Please keep them in sync. */
2207 3434768 : FOR_EACH_VARIABLE (vnode)
2208 3195026 : if (vnode->no_reorder
2209 1002141 : && !DECL_HARD_REGISTER (vnode->decl)
2210 4197145 : && !DECL_HAS_VALUE_EXPR_P (vnode->decl))
2211 1002805 : nodes.safe_push (cgraph_order_sort (vnode));
2212 :
2213 252227 : for (anode = symtab->first_asm_symbol (); anode;
2214 12485 : anode = safe_as_a<asm_node*>(anode->next))
2215 12485 : nodes.safe_push (cgraph_order_sort (anode));
2216 :
2217 : /* Sort nodes by order. */
2218 239742 : nodes.qsort (cgraph_order_cmp);
2219 :
2220 : /* In toplevel reorder mode we output all statics; mark them as needed. */
2221 1804160 : FOR_EACH_VEC_ELT (nodes, i, node)
2222 1564418 : if (node->kind == ORDER_VAR)
2223 1001433 : node->u.v->finalize_named_section_flags ();
2224 :
2225 1804120 : FOR_EACH_VEC_ELT (nodes, i, node)
2226 1564388 : node->process ();
2227 :
2228 239732 : symtab->clear_asm_symbols ();
2229 239732 : }
2230 :
2231 : static void
2232 252241 : ipa_passes (void)
2233 : {
2234 252241 : gcc::pass_manager *passes = g->get_passes ();
2235 :
2236 252241 : set_cfun (NULL);
2237 252241 : current_function_decl = NULL;
2238 252241 : gimple_register_cfg_hooks ();
2239 252241 : bitmap_obstack_initialize (NULL);
2240 :
2241 252241 : invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2242 :
2243 252241 : if (!in_lto_p)
2244 : {
2245 239068 : execute_ipa_pass_list (passes->all_small_ipa_passes);
2246 239068 : if (seen_error ())
2247 : return;
2248 : }
2249 :
2250 : /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2251 : devirtualization and other changes where removal iterate. */
2252 252091 : symtab->remove_unreachable_nodes (symtab->dump_file);
2253 :
2254 : /* If pass_all_early_optimizations was not scheduled, the state of
2255 : the cgraph will not be properly updated. Update it now. */
2256 252091 : if (symtab->state < IPA_SSA)
2257 13173 : symtab->state = IPA_SSA;
2258 :
2259 252091 : if (!in_lto_p)
2260 : {
2261 : /* Generate coverage variables and constructors. */
2262 238918 : coverage_finish ();
2263 :
2264 : /* Process new functions added. */
2265 238918 : set_cfun (NULL);
2266 238918 : current_function_decl = NULL;
2267 238918 : symtab->process_new_functions ();
2268 :
2269 238918 : execute_ipa_summary_passes
2270 238918 : ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2271 : }
2272 :
2273 : /* Some targets need to handle LTO assembler output specially. */
2274 252091 : if (flag_generate_lto || flag_generate_offload)
2275 23943 : targetm.asm_out.lto_start ();
2276 :
2277 252091 : if (!in_lto_p
2278 13173 : || flag_incremental_link == INCREMENTAL_LINK_LTO)
2279 : {
2280 238954 : if (!quiet_flag)
2281 0 : fprintf (stderr, "Streaming LTO\n");
2282 238954 : if (g->have_offload)
2283 : {
2284 0 : section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2285 0 : lto_stream_offload_p = true;
2286 0 : ipa_write_summaries ();
2287 0 : lto_stream_offload_p = false;
2288 : }
2289 238954 : if (flag_lto)
2290 : {
2291 23943 : section_name_prefix = LTO_SECTION_NAME_PREFIX;
2292 23943 : lto_stream_offload_p = false;
2293 23943 : ipa_write_summaries ();
2294 : }
2295 : }
2296 :
2297 252091 : if (flag_generate_lto || flag_generate_offload)
2298 23943 : targetm.asm_out.lto_end ();
2299 :
2300 252091 : if (!flag_ltrans
2301 243553 : && ((in_lto_p && flag_incremental_link != INCREMENTAL_LINK_LTO)
2302 238954 : || !flag_lto || flag_fat_lto_objects))
2303 231211 : execute_ipa_pass_list (passes->all_regular_ipa_passes);
2304 252091 : invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2305 :
2306 252091 : bitmap_obstack_release (NULL);
2307 : }
2308 :
2309 :
2310 : /* Weakrefs may be associated to external decls and thus not output
2311 : at expansion time. Emit all necessary aliases. */
2312 :
2313 : void
2314 239732 : symbol_table::output_weakrefs (void)
2315 : {
2316 239732 : symtab_node *node;
2317 6808541 : FOR_EACH_SYMBOL (node)
2318 6568809 : if (node->alias
2319 69846 : && !TREE_ASM_WRITTEN (node->decl)
2320 1318 : && node->weakref)
2321 : {
2322 127 : tree target;
2323 :
2324 : /* Weakrefs are special by not requiring target definition in current
2325 : compilation unit. It is thus bit hard to work out what we want to
2326 : alias.
2327 : When alias target is defined, we need to fetch it from symtab reference,
2328 : otherwise it is pointed to by alias_target. */
2329 127 : if (node->alias_target)
2330 122 : target = (DECL_P (node->alias_target)
2331 122 : ? DECL_ASSEMBLER_NAME (node->alias_target)
2332 : : node->alias_target);
2333 5 : else if (node->analyzed)
2334 5 : target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
2335 : else
2336 0 : gcc_unreachable ();
2337 127 : do_assemble_alias (node->decl, target);
2338 : }
2339 239732 : }
2340 :
2341 : /* Perform simple optimizations based on callgraph. */
2342 :
2343 : void
2344 278890 : symbol_table::compile (void)
2345 : {
2346 278890 : if (seen_error ())
2347 : return;
2348 :
2349 252241 : symtab_node::checking_verify_symtab_nodes ();
2350 :
2351 252241 : symtab_node::check_ifunc_callee_symtab_nodes ();
2352 :
2353 252241 : timevar_push (TV_CGRAPHOPT);
2354 252241 : if (pre_ipa_mem_report)
2355 0 : dump_memory_report ("Memory consumption before IPA");
2356 252241 : if (!quiet_flag)
2357 0 : fprintf (stderr, "Performing interprocedural optimizations\n");
2358 252241 : state = IPA;
2359 :
2360 : /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2361 252241 : if (flag_generate_lto || flag_generate_offload)
2362 23955 : lto_streamer_hooks_init ();
2363 :
2364 : /* Don't run the IPA passes if there was any error or sorry messages. */
2365 252241 : if (!seen_error ())
2366 : {
2367 252241 : timevar_start (TV_CGRAPH_IPA_PASSES);
2368 252241 : ipa_passes ();
2369 252241 : timevar_stop (TV_CGRAPH_IPA_PASSES);
2370 : }
2371 : /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2372 252241 : if (seen_error ()
2373 252241 : || ((!in_lto_p || flag_incremental_link == INCREMENTAL_LINK_LTO)
2374 238947 : && flag_lto && !flag_fat_lto_objects))
2375 : {
2376 12499 : timevar_pop (TV_CGRAPHOPT);
2377 12499 : return;
2378 : }
2379 :
2380 239742 : global_info_ready = true;
2381 239742 : if (dump_file)
2382 : {
2383 72 : fprintf (dump_file, "Optimized ");
2384 72 : symtab->dump (dump_file);
2385 : }
2386 239742 : if (post_ipa_mem_report)
2387 0 : dump_memory_report ("Memory consumption after IPA");
2388 239742 : timevar_pop (TV_CGRAPHOPT);
2389 :
2390 : /* Output everything. */
2391 239742 : switch_to_section (text_section);
2392 239742 : (*debug_hooks->assembly_start) ();
2393 239742 : if (!quiet_flag)
2394 0 : fprintf (stderr, "Assembling functions:\n");
2395 239742 : symtab_node::checking_verify_symtab_nodes ();
2396 :
2397 239742 : bitmap_obstack_initialize (NULL);
2398 239742 : execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2399 239742 : bitmap_obstack_release (NULL);
2400 239742 : mark_functions_to_output ();
2401 :
2402 : /* When weakref support is missing, we automatically translate all
2403 : references to NODE to references to its ultimate alias target.
2404 : The renaming mechanism uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2405 : TREE_CHAIN.
2406 :
2407 : Set up this mapping before we output any assembler but once we are sure
2408 : that all symbol renaming is done.
2409 :
2410 : FIXME: All this ugliness can go away if we just do renaming at gimple
2411 : level by physically rewriting the IL. At the moment we can only redirect
2412 : calls, so we need infrastructure for renaming references as well. */
2413 : #ifndef ASM_OUTPUT_WEAKREF
2414 : symtab_node *node;
2415 :
2416 : FOR_EACH_SYMBOL (node)
2417 : if (node->alias
2418 : && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2419 : {
2420 : tree id = DECL_ASSEMBLER_NAME (node->decl);
2421 : gcc_assert (!IDENTIFIER_INTERNAL_P (id));
2422 : IDENTIFIER_TRANSPARENT_ALIAS (id) = 1;
2423 : TREE_CHAIN (id)
2424 : = (node->alias_target ? node->alias_target
2425 : : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
2426 : }
2427 : #endif
2428 :
2429 239742 : state = EXPANSION;
2430 :
2431 : /* Output first asm statements and anything ordered. The process
2432 : flag is cleared for these nodes, so we skip them later. */
2433 239742 : output_in_order ();
2434 :
2435 239732 : timevar_start (TV_CGRAPH_FUNC_EXPANSION);
2436 239732 : expand_all_functions ();
2437 239732 : timevar_stop (TV_CGRAPH_FUNC_EXPANSION);
2438 :
2439 239732 : output_variables ();
2440 :
2441 239732 : process_new_functions ();
2442 239732 : state = FINISHED;
2443 239732 : output_weakrefs ();
2444 :
2445 239732 : if (dump_file)
2446 : {
2447 72 : fprintf (dump_file, "\nFinal ");
2448 72 : symtab->dump (dump_file);
2449 : }
2450 239732 : if (!flag_checking)
2451 : return;
2452 239718 : symtab_node::verify_symtab_nodes ();
2453 : /* Double check that all inline clones are gone and that all
2454 : function bodies have been released from memory. */
2455 239718 : if (!seen_error ())
2456 : {
2457 239431 : cgraph_node *node;
2458 239431 : bool error_found = false;
2459 :
2460 1818597 : FOR_EACH_DEFINED_FUNCTION (node)
2461 1579166 : if (node->inlined_to
2462 1579166 : || gimple_has_body_p (node->decl))
2463 : {
2464 144 : if (DECL_STRUCT_FUNCTION (node->decl)
2465 144 : && (DECL_STRUCT_FUNCTION (node->decl)->curr_properties
2466 144 : & PROP_assumptions_done) != 0)
2467 144 : continue;
2468 0 : error_found = true;
2469 0 : node->debug ();
2470 : }
2471 239431 : if (error_found)
2472 0 : internal_error ("nodes with unreleased memory found");
2473 : }
2474 : }
2475 :
2476 : /* Earlydebug dump file, flags, and number. */
2477 :
2478 : static int debuginfo_early_dump_nr;
2479 : static FILE *debuginfo_early_dump_file;
2480 : static dump_flags_t debuginfo_early_dump_flags;
2481 :
2482 : /* Debug dump file, flags, and number. */
2483 :
2484 : static int debuginfo_dump_nr;
2485 : static FILE *debuginfo_dump_file;
2486 : static dump_flags_t debuginfo_dump_flags;
2487 :
2488 : /* Register the debug and earlydebug dump files. */
2489 :
2490 : void
2491 297180 : debuginfo_early_init (void)
2492 : {
2493 297180 : gcc::dump_manager *dumps = g->get_dumps ();
2494 297180 : debuginfo_early_dump_nr = dumps->dump_register (".earlydebug", "earlydebug",
2495 : "earlydebug", DK_tree,
2496 : OPTGROUP_NONE,
2497 : false);
2498 297180 : debuginfo_dump_nr = dumps->dump_register (".debug", "debug",
2499 : "debug", DK_tree,
2500 : OPTGROUP_NONE,
2501 : false);
2502 297180 : }
2503 :
2504 : /* Initialize the debug and earlydebug dump files. */
2505 :
2506 : void
2507 289638 : debuginfo_init (void)
2508 : {
2509 289638 : gcc::dump_manager *dumps = g->get_dumps ();
2510 289638 : debuginfo_dump_file = dump_begin (debuginfo_dump_nr, NULL);
2511 289638 : debuginfo_dump_flags = dumps->get_dump_file_info (debuginfo_dump_nr)->pflags;
2512 289638 : debuginfo_early_dump_file = dump_begin (debuginfo_early_dump_nr, NULL);
2513 289638 : debuginfo_early_dump_flags
2514 289638 : = dumps->get_dump_file_info (debuginfo_early_dump_nr)->pflags;
2515 289638 : }
2516 :
2517 : /* Finalize the debug and earlydebug dump files. */
2518 :
2519 : void
2520 287724 : debuginfo_fini (void)
2521 : {
2522 287724 : if (debuginfo_dump_file)
2523 49 : dump_end (debuginfo_dump_nr, debuginfo_dump_file);
2524 287724 : if (debuginfo_early_dump_file)
2525 49 : dump_end (debuginfo_early_dump_nr, debuginfo_early_dump_file);
2526 287724 : }
2527 :
2528 : /* Set dump_file to the debug dump file. */
2529 :
2530 : void
2531 239445 : debuginfo_start (void)
2532 : {
2533 239445 : set_dump_file (debuginfo_dump_file);
2534 239445 : }
2535 :
2536 : /* Undo setting dump_file to the debug dump file. */
2537 :
2538 : void
2539 239445 : debuginfo_stop (void)
2540 : {
2541 239445 : set_dump_file (NULL);
2542 239445 : }
2543 :
2544 : /* Set dump_file to the earlydebug dump file. */
2545 :
2546 : void
2547 252241 : debuginfo_early_start (void)
2548 : {
2549 252241 : set_dump_file (debuginfo_early_dump_file);
2550 252241 : }
2551 :
2552 : /* Undo setting dump_file to the earlydebug dump file. */
2553 :
2554 : void
2555 252241 : debuginfo_early_stop (void)
2556 : {
2557 252241 : set_dump_file (NULL);
2558 252241 : }
2559 :
2560 : /* Analyze the whole compilation unit once it is parsed completely. */
2561 :
2562 : void
2563 265730 : symbol_table::finalize_compilation_unit (void)
2564 : {
2565 265730 : timevar_push (TV_CGRAPH);
2566 :
2567 : /* If we're here there's no current function anymore. Some frontends
2568 : are lazy in clearing these. */
2569 265730 : current_function_decl = NULL;
2570 265730 : set_cfun (NULL);
2571 :
2572 : /* Do not skip analyzing the functions if there were errors, we
2573 : miss diagnostics for following functions otherwise. */
2574 :
2575 : /* Emit size functions we didn't inline. */
2576 265730 : finalize_size_functions ();
2577 :
2578 : /* Mark alias targets necessary and emit diagnostics. */
2579 265730 : handle_alias_pairs ();
2580 :
2581 265730 : if (!quiet_flag)
2582 : {
2583 0 : fprintf (stderr, "\nAnalyzing compilation unit\n");
2584 0 : fflush (stderr);
2585 : }
2586 :
2587 265730 : if (flag_dump_passes)
2588 5 : dump_passes ();
2589 :
2590 265730 : analyze_toplevel_extended_asm ();
2591 :
2592 : /* Gimplify and lower all functions, compute reachability and
2593 : remove unreachable nodes. */
2594 265730 : analyze_functions (/*first_time=*/true);
2595 :
2596 : /* Mark alias targets necessary and emit diagnostics. */
2597 265717 : handle_alias_pairs ();
2598 :
2599 : /* Gimplify and lower thunks. */
2600 265717 : analyze_functions (/*first_time=*/false);
2601 :
2602 : /* All nested functions should be lowered now. */
2603 265717 : nested_function_info::release ();
2604 :
2605 : /* Offloading requires LTO infrastructure. */
2606 265717 : if (!in_lto_p && g->have_offload)
2607 0 : flag_generate_offload = 1;
2608 :
2609 265717 : if (!seen_error ())
2610 : {
2611 239068 : timevar_push (TV_SYMOUT);
2612 :
2613 : /* Give the frontends the chance to emit early debug based on
2614 : what is still reachable in the TU. */
2615 239068 : (*lang_hooks.finalize_early_debug) ();
2616 :
2617 : /* Clean up anything that needs cleaning up after initial debug
2618 : generation. */
2619 239068 : debuginfo_early_start ();
2620 239068 : (*debug_hooks->early_finish) (main_input_filename);
2621 239068 : debuginfo_early_stop ();
2622 :
2623 239068 : timevar_pop (TV_SYMOUT);
2624 : }
2625 :
2626 : /* Finally drive the pass manager. */
2627 265717 : compile ();
2628 :
2629 265707 : timevar_pop (TV_CGRAPH);
2630 265707 : }
2631 :
2632 : /* Reset all state within cgraphunit.cc so that we can rerun the compiler
2633 : within the same process. For use by toplev::finalize. */
2634 :
2635 : void
2636 266934 : cgraphunit_cc_finalize (void)
2637 : {
2638 266934 : gcc_assert (cgraph_new_nodes.length () == 0);
2639 266934 : cgraph_new_nodes.truncate (0);
2640 :
2641 266934 : queued_nodes = &symtab_terminator;
2642 :
2643 266934 : first_analyzed = NULL;
2644 266934 : first_analyzed_var = NULL;
2645 266934 : }
2646 :
2647 : /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2648 : kind of wrapper method. */
2649 :
2650 : void
2651 18896 : cgraph_node::create_wrapper (cgraph_node *target)
2652 : {
2653 : /* Preserve DECL_RESULT so we get right by reference flag. */
2654 18896 : tree decl_result = DECL_RESULT (decl);
2655 :
2656 : /* Remove the function's body but keep arguments to be reused
2657 : for thunk. */
2658 18896 : release_body (true);
2659 18896 : reset ();
2660 :
2661 18896 : DECL_UNINLINABLE (decl) = false;
2662 18896 : DECL_RESULT (decl) = decl_result;
2663 18896 : DECL_INITIAL (decl) = NULL;
2664 18896 : allocate_struct_function (decl, false);
2665 18896 : set_cfun (NULL);
2666 :
2667 : /* Turn alias into thunk and expand it into GIMPLE representation. */
2668 18896 : definition = true;
2669 18896 : semantic_interposition = opt_for_fn (decl, flag_semantic_interposition);
2670 :
2671 : /* Create empty thunk, but be sure we did not keep former thunk around.
2672 : In that case we would need to preserve the info. */
2673 18896 : gcc_checking_assert (!thunk_info::get (this));
2674 18896 : thunk_info::get_create (this);
2675 18896 : thunk = true;
2676 18896 : create_edge (target, NULL, count);
2677 18896 : callees->can_throw_external = !TREE_NOTHROW (target->decl);
2678 :
2679 18896 : tree arguments = DECL_ARGUMENTS (decl);
2680 :
2681 52182 : while (arguments)
2682 : {
2683 33286 : TREE_ADDRESSABLE (arguments) = false;
2684 33286 : arguments = TREE_CHAIN (arguments);
2685 : }
2686 :
2687 : /* Forced GIMPLE thunks are normally ignored because they are created
2688 : after early debug. ICF wrappers retain the original function decl and
2689 : its early DIE, so preserve its original debug state. */
2690 18896 : bool ignored_p = DECL_IGNORED_P (decl);
2691 18896 : expand_thunk (this, false, true);
2692 18896 : DECL_IGNORED_P (decl) = ignored_p;
2693 18896 : thunk_info::remove (this);
2694 :
2695 : /* Inline summary set-up. */
2696 18896 : analyze ();
2697 18896 : inline_analyze_function (this);
2698 18896 : }
|