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