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