]>
Commit | Line | Data |
---|---|---|
da5e1e7c | 1 | /* Driver of optimization process |
d353bf18 | 2 | Copyright (C) 2003-2015 Free Software Foundation, Inc. |
ae01b312 | 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 | |
8c4c00c1 | 9 | Software Foundation; either version 3, or (at your option) any later |
ae01b312 | 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 | |
8c4c00c1 | 18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
ae01b312 | 20 | |
da5e1e7c | 21 | /* This module implements main driver of compilation process. |
b0cdf642 | 22 | |
23 | The main scope of this file is to act as an interface in between | |
da5e1e7c | 24 | tree based frontends and the backend. |
b0cdf642 | 25 | |
26 | The front-end is supposed to use following functionality: | |
27 | ||
35ee1c66 | 28 | - finalize_function |
b0cdf642 | 29 | |
30 | This function is called once front-end has parsed whole body of function | |
31 | and it is certain that the function body nor the declaration will change. | |
32 | ||
b326746d | 33 | (There is one exception needed for implementing GCC extern inline |
34 | function.) | |
b0cdf642 | 35 | |
a2d67028 | 36 | - varpool_finalize_decl |
b0cdf642 | 37 | |
7bd28bba | 38 | This function has same behavior as the above but is used for static |
b0cdf642 | 39 | variables. |
40 | ||
cf951b1a | 41 | - add_asm_node |
42 | ||
43 | Insert new toplevel ASM statement | |
44 | ||
45 | - finalize_compilation_unit | |
b0cdf642 | 46 | |
b326746d | 47 | This function is called once (source level) compilation unit is finalized |
48 | and it will no longer change. | |
b0cdf642 | 49 | |
da5e1e7c | 50 | The symbol table is constructed starting from the trivially needed |
51 | symbols finalized by the frontend. Functions are lowered into | |
52 | GIMPLE representation and callgraph/reference lists are constructed. | |
9d75589a | 53 | Those are used to discover other necessary functions and variables. |
da5e1e7c | 54 | |
55 | At the end the bodies of unreachable functions are removed. | |
b0cdf642 | 56 | |
b326746d | 57 | The function can be called multiple times when multiple source level |
da5e1e7c | 58 | compilation units are combined. |
b0cdf642 | 59 | |
cf951b1a | 60 | - compile |
b0cdf642 | 61 | |
da5e1e7c | 62 | This passes control to the back-end. Optimizations are performed and |
63 | final assembler is generated. This is done in the following way. Note | |
64 | that with link time optimization the process is split into three | |
65 | stages (compile time, linktime analysis and parallel linktime as | |
66 | indicated bellow). | |
67 | ||
68 | Compile time: | |
69 | ||
70 | 1) Inter-procedural optimization. | |
71 | (ipa_passes) | |
72 | ||
73 | This part is further split into: | |
74 | ||
75 | a) early optimizations. These are local passes executed in | |
76 | the topological order on the callgraph. | |
77 | ||
78 | The purpose of early optimiations is to optimize away simple | |
79 | things that may otherwise confuse IP analysis. Very simple | |
80 | propagation across the callgraph is done i.e. to discover | |
81 | functions without side effects and simple inlining is performed. | |
82 | ||
83 | b) early small interprocedural passes. | |
84 | ||
85 | Those are interprocedural passes executed only at compilation | |
a04e8d62 | 86 | time. These include, for example, transational memory lowering, |
da5e1e7c | 87 | unreachable code removal and other simple transformations. |
88 | ||
89 | c) IP analysis stage. All interprocedural passes do their | |
90 | analysis. | |
91 | ||
92 | Interprocedural passes differ from small interprocedural | |
93 | passes by their ability to operate across whole program | |
94 | at linktime. Their analysis stage is performed early to | |
95 | both reduce linking times and linktime memory usage by | |
96 | not having to represent whole program in memory. | |
97 | ||
98 | d) LTO sreaming. When doing LTO, everything important gets | |
99 | streamed into the object file. | |
100 | ||
101 | Compile time and or linktime analysis stage (WPA): | |
102 | ||
103 | At linktime units gets streamed back and symbol table is | |
104 | merged. Function bodies are not streamed in and not | |
105 | available. | |
106 | e) IP propagation stage. All IP passes execute their | |
107 | IP propagation. This is done based on the earlier analysis | |
108 | without having function bodies at hand. | |
109 | f) Ltrans streaming. When doing WHOPR LTO, the program | |
110 | is partitioned and streamed into multple object files. | |
b0cdf642 | 111 | |
da5e1e7c | 112 | Compile time and/or parallel linktime stage (ltrans) |
b0cdf642 | 113 | |
da5e1e7c | 114 | Each of the object files is streamed back and compiled |
115 | separately. Now the function bodies becomes available | |
116 | again. | |
b0cdf642 | 117 | |
da5e1e7c | 118 | 2) Virtual clone materialization |
119 | (cgraph_materialize_clone) | |
b0cdf642 | 120 | |
da5e1e7c | 121 | IP passes can produce copies of existing functoins (such |
122 | as versioned clones or inline clones) without actually | |
123 | manipulating their bodies by creating virtual clones in | |
124 | the callgraph. At this time the virtual clones are | |
125 | turned into real functions | |
126 | 3) IP transformation | |
b0cdf642 | 127 | |
da5e1e7c | 128 | All IP passes transform function bodies based on earlier |
129 | decision of the IP propagation. | |
b0cdf642 | 130 | |
da5e1e7c | 131 | 4) late small IP passes |
b0cdf642 | 132 | |
da5e1e7c | 133 | Simple IP passes working within single program partition. |
b0cdf642 | 134 | |
da5e1e7c | 135 | 5) Expansion |
cf951b1a | 136 | (expand_all_functions) |
b0cdf642 | 137 | |
da5e1e7c | 138 | At this stage functions that needs to be output into |
139 | assembler are identified and compiled in topological order | |
140 | 6) Output of variables and aliases | |
141 | Now it is known what variable references was not optimized | |
142 | out and thus all variables are output to the file. | |
b0cdf642 | 143 | |
da5e1e7c | 144 | Note that with -fno-toplevel-reorder passes 5 and 6 |
145 | are combined together in cgraph_output_in_order. | |
b0cdf642 | 146 | |
da5e1e7c | 147 | Finally there are functions to manipulate the callgraph from |
148 | backend. | |
149 | - cgraph_add_new_function is used to add backend produced | |
150 | functions introduced after the unit is finalized. | |
151 | The functions are enqueue for later processing and inserted | |
152 | into callgraph with cgraph_process_new_functions. | |
121f3051 | 153 | |
da5e1e7c | 154 | - cgraph_function_versioning |
155 | ||
156 | produces a copy of function into new one (a version) | |
157 | and apply simple transformations | |
158 | */ | |
acc70efa | 159 | |
ae01b312 | 160 | #include "config.h" |
161 | #include "system.h" | |
162 | #include "coretypes.h" | |
9ef16211 | 163 | #include "backend.h" |
7c29e30e | 164 | #include "target.h" |
165 | #include "rtl.h" | |
ae01b312 | 166 | #include "tree.h" |
9ef16211 | 167 | #include "gimple.h" |
7c29e30e | 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" | |
b20a8bb4 | 177 | #include "fold-const.h" |
9ed99284 | 178 | #include "varasm.h" |
179 | #include "stor-layout.h" | |
941366fd | 180 | #include "output.h" |
a5cb93e3 | 181 | #include "cfgcleanup.h" |
bc61cadb | 182 | #include "gimple-fold.h" |
a8783bee | 183 | #include "gimplify.h" |
dcf1a1ec | 184 | #include "gimple-iterator.h" |
e795d6e1 | 185 | #include "gimplify-me.h" |
073c1fd5 | 186 | #include "tree-cfg.h" |
187 | #include "tree-into-ssa.h" | |
69ee5dbb | 188 | #include "tree-ssa.h" |
ae01b312 | 189 | #include "langhooks.h" |
ae01b312 | 190 | #include "toplev.h" |
ae01b312 | 191 | #include "debug.h" |
2cc80ac3 | 192 | #include "symbol-summary.h" |
b5d36404 | 193 | #include "ipa-prop.h" |
da5e1e7c | 194 | #include "gimple-pretty-print.h" |
c9036234 | 195 | #include "plugin.h" |
a41f2a28 | 196 | #include "ipa-inline.h" |
7771d558 | 197 | #include "ipa-utils.h" |
3db65b62 | 198 | #include "except.h" |
7eacb0dd | 199 | #include "cfgloop.h" |
3ea50c01 | 200 | #include "context.h" |
201 | #include "pass_manager.h" | |
e797f49f | 202 | #include "tree-nested.h" |
ceb49bba | 203 | #include "dbgcnt.h" |
058a1b7a | 204 | #include "tree-chkp.h" |
b0c5e347 | 205 | #include "lto-section-names.h" |
d7c6d889 | 206 | |
ff2a5ada | 207 | /* Queue of cgraph nodes scheduled to be added into cgraph. This is a |
208 | secondary queue used during optimization to accommodate passes that | |
209 | may generate new functions that need to be optimized and expanded. */ | |
347a47cb | 210 | vec<cgraph_node *> cgraph_new_nodes; |
ff2a5ada | 211 | |
cf951b1a | 212 | static void expand_all_functions (void); |
213 | static void mark_functions_to_output (void); | |
18a71d50 | 214 | static void handle_alias_pairs (void); |
25bb88de | 215 | |
28454517 | 216 | /* Used for vtable lookup in thunk adjusting. */ |
217 | static GTY (()) tree vtable_entry_type; | |
218 | ||
35ee1c66 | 219 | /* Determine if symbol declaration is needed. That is, visible to something |
6a1c0403 | 220 | either outside this translation unit, something magic in the system |
221 | configury */ | |
222 | bool | |
35ee1c66 | 223 | symtab_node::needed_p (void) |
2c0b522d | 224 | { |
8efa224a | 225 | /* Double check that no one output the function into assembly file |
226 | early. */ | |
227 | gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl) | |
48669653 | 228 | || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))); |
3f82b628 | 229 | |
35ee1c66 | 230 | if (!definition) |
6a1c0403 | 231 | return false; |
55680bef | 232 | |
6a1c0403 | 233 | if (DECL_EXTERNAL (decl)) |
234 | return false; | |
8baa9d15 | 235 | |
6a1c0403 | 236 | /* If the user told us it is used, then it must be so. */ |
35ee1c66 | 237 | if (force_output) |
6a1c0403 | 238 | return true; |
239 | ||
240 | /* ABI forced symbols are needed when they are external. */ | |
35ee1c66 | 241 | if (forced_by_abi && TREE_PUBLIC (decl)) |
6a1c0403 | 242 | return true; |
243 | ||
af822fd2 | 244 | /* Keep constructors, destructors and virtual functions. */ |
6a1c0403 | 245 | if (TREE_CODE (decl) == FUNCTION_DECL |
246 | && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl))) | |
247 | return true; | |
248 | ||
249 | /* Externally visible variables must be output. The exception is | |
250 | COMDAT variables that must be output only when they are needed. */ | |
251 | if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl)) | |
2c0b522d | 252 | return true; |
253 | ||
2c0b522d | 254 | return false; |
255 | } | |
256 | ||
3cb3fce5 | 257 | /* Head and terminator of the queue of nodes to be processed while building |
258 | callgraph. */ | |
ff2a5ada | 259 | |
3cb3fce5 | 260 | static symtab_node symtab_terminator; |
261 | static symtab_node *queued_nodes = &symtab_terminator; | |
ff2a5ada | 262 | |
3cb3fce5 | 263 | /* Add NODE to queue starting at QUEUED_NODES. |
ff2a5ada | 264 | The queue is linked via AUX pointers and terminated by pointer to 1. */ |
265 | ||
266 | static void | |
452659af | 267 | enqueue_node (symtab_node *node) |
ff2a5ada | 268 | { |
02774f2d | 269 | if (node->aux) |
ff2a5ada | 270 | return; |
3cb3fce5 | 271 | gcc_checking_assert (queued_nodes); |
272 | node->aux = queued_nodes; | |
273 | queued_nodes = node; | |
ff2a5ada | 274 | } |
275 | ||
bdc40eb8 | 276 | /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these |
523c1122 | 277 | functions into callgraph in a way so they look like ordinary reachable |
278 | functions inserted into callgraph already at construction time. */ | |
279 | ||
3cb3fce5 | 280 | void |
35ee1c66 | 281 | symbol_table::process_new_functions (void) |
523c1122 | 282 | { |
523c1122 | 283 | tree fndecl; |
523c1122 | 284 | |
347a47cb | 285 | if (!cgraph_new_nodes.exists ()) |
3cb3fce5 | 286 | return; |
347a47cb | 287 | |
18a71d50 | 288 | handle_alias_pairs (); |
523c1122 | 289 | /* Note that this queue may grow as its being processed, as the new |
290 | functions may generate new ones. */ | |
347a47cb | 291 | for (unsigned i = 0; i < cgraph_new_nodes.length (); i++) |
523c1122 | 292 | { |
347a47cb | 293 | cgraph_node *node = cgraph_new_nodes[i]; |
02774f2d | 294 | fndecl = node->decl; |
35ee1c66 | 295 | switch (state) |
523c1122 | 296 | { |
35ee1c66 | 297 | case CONSTRUCTION: |
523c1122 | 298 | /* At construction time we just need to finalize function and move |
299 | it into reachable functions list. */ | |
300 | ||
35ee1c66 | 301 | cgraph_node::finalize_function (fndecl, false); |
302 | call_cgraph_insertion_hooks (node); | |
02774f2d | 303 | enqueue_node (node); |
523c1122 | 304 | break; |
305 | ||
35ee1c66 | 306 | case IPA: |
307 | case IPA_SSA: | |
366970c6 | 308 | case IPA_SSA_AFTER_INLINING: |
523c1122 | 309 | /* When IPA optimization already started, do all essential |
310 | transformations that has been already performed on the whole | |
311 | cgraph but not on this function. */ | |
312 | ||
75a70cf9 | 313 | gimple_register_cfg_hooks (); |
02774f2d | 314 | if (!node->analyzed) |
415d1b9a | 315 | node->analyze (); |
523c1122 | 316 | push_cfun (DECL_STRUCT_FUNCTION (fndecl)); |
366970c6 | 317 | if ((state == IPA_SSA || state == IPA_SSA_AFTER_INLINING) |
f517b36e | 318 | && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl))) |
bcfddb5b | 319 | g->get_passes ()->execute_early_local_passes (); |
b4bae7a0 | 320 | else if (inline_summaries != NULL) |
a41f2a28 | 321 | compute_inline_parameters (node, true); |
523c1122 | 322 | free_dominance_info (CDI_POST_DOMINATORS); |
323 | free_dominance_info (CDI_DOMINATORS); | |
324 | pop_cfun (); | |
51c5d4d3 | 325 | call_cgraph_insertion_hooks (node); |
523c1122 | 326 | break; |
327 | ||
35ee1c66 | 328 | case EXPANSION: |
523c1122 | 329 | /* Functions created during expansion shall be compiled |
330 | directly. */ | |
09fc9532 | 331 | node->process = 0; |
35ee1c66 | 332 | call_cgraph_insertion_hooks (node); |
333 | node->expand (); | |
523c1122 | 334 | break; |
335 | ||
336 | default: | |
337 | gcc_unreachable (); | |
338 | break; | |
339 | } | |
340 | } | |
347a47cb | 341 | |
342 | cgraph_new_nodes.release (); | |
523c1122 | 343 | } |
344 | ||
9b8fb23a | 345 | /* As an GCC extension we allow redefinition of the function. The |
346 | semantics when both copies of bodies differ is not well defined. | |
347 | We replace the old body with new body so in unit at a time mode | |
348 | we always use new body, while in normal mode we may end up with | |
349 | old body inlined into some functions and new body expanded and | |
350 | inlined in others. | |
351 | ||
352 | ??? It may make more sense to use one body for inlining and other | |
353 | body for expanding the function but this is difficult to do. */ | |
354 | ||
15ca8f90 | 355 | void |
415d1b9a | 356 | cgraph_node::reset (void) |
9b8fb23a | 357 | { |
415d1b9a | 358 | /* If process is set, then we have already begun whole-unit analysis. |
6329636b | 359 | This is *not* testing for whether we've already emitted the function. |
360 | That case can be sort-of legitimately seen with real function redefinition | |
361 | errors. I would argue that the front end should never present us with | |
362 | such a case, but don't enforce that for now. */ | |
415d1b9a | 363 | gcc_assert (!process); |
9b8fb23a | 364 | |
365 | /* Reset our data structures so we can analyze the function again. */ | |
415d1b9a | 366 | memset (&local, 0, sizeof (local)); |
367 | memset (&global, 0, sizeof (global)); | |
368 | memset (&rtl, 0, sizeof (rtl)); | |
369 | analyzed = false; | |
370 | definition = false; | |
371 | alias = false; | |
372 | weakref = false; | |
373 | cpp_implicit_alias = false; | |
374 | ||
375 | remove_callees (); | |
376 | remove_all_references (); | |
9b8fb23a | 377 | } |
c08871a9 | 378 | |
3a1c9df2 | 379 | /* Return true when there are references to the node. INCLUDE_SELF is |
380 | true if a self reference counts as a reference. */ | |
9a2639fc | 381 | |
35ee1c66 | 382 | bool |
3a1c9df2 | 383 | symtab_node::referred_to_p (bool include_self) |
9a2639fc | 384 | { |
35ee1c66 | 385 | ipa_ref *ref = NULL; |
9a2639fc | 386 | |
9d75589a | 387 | /* See if there are any references at all. */ |
35ee1c66 | 388 | if (iterate_referring (0, ref)) |
9a2639fc | 389 | return true; |
cf951b1a | 390 | /* For functions check also calls. */ |
35ee1c66 | 391 | cgraph_node *cn = dyn_cast <cgraph_node *> (this); |
2dc9831f | 392 | if (cn && cn->callers) |
3a1c9df2 | 393 | { |
394 | if (include_self) | |
395 | return true; | |
396 | for (cgraph_edge *e = cn->callers; e; e = e->next_caller) | |
397 | if (e->caller != this) | |
398 | return true; | |
399 | } | |
9a2639fc | 400 | return false; |
401 | } | |
402 | ||
28df663b | 403 | /* DECL has been parsed. Take it, queue it, compile it at the whim of the |
1f7747bd | 404 | logic in effect. If NO_COLLECT is true, then our caller cannot stand to have |
28df663b | 405 | the garbage collector run at the moment. We would need to either create |
406 | a new GC context, or just not compile right now. */ | |
ae01b312 | 407 | |
408 | void | |
35ee1c66 | 409 | cgraph_node::finalize_function (tree decl, bool no_collect) |
ae01b312 | 410 | { |
35ee1c66 | 411 | cgraph_node *node = cgraph_node::get_create (decl); |
ae01b312 | 412 | |
02774f2d | 413 | if (node->definition) |
443089c1 | 414 | { |
1f7747bd | 415 | /* Nested functions should only be defined once. */ |
416 | gcc_assert (!DECL_CONTEXT (decl) | |
417 | || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL); | |
415d1b9a | 418 | node->reset (); |
443089c1 | 419 | node->local.redefined_extern_inline = true; |
420 | } | |
28df663b | 421 | |
78db4b4d | 422 | /* Set definition first before calling notice_global_symbol so that |
423 | it is available to notice_global_symbol. */ | |
02774f2d | 424 | node->definition = true; |
78db4b4d | 425 | notice_global_symbol (decl); |
e27482aa | 426 | node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL; |
ae01b312 | 427 | |
8efa224a | 428 | /* With -fkeep-inline-functions we are keeping all inline functions except |
429 | for extern inline ones. */ | |
430 | if (flag_keep_inline_functions | |
431 | && DECL_DECLARED_INLINE_P (decl) | |
432 | && !DECL_EXTERNAL (decl) | |
433 | && !DECL_DISREGARD_INLINE_LIMITS (decl)) | |
02774f2d | 434 | node->force_output = 1; |
2c0b522d | 435 | |
8efa224a | 436 | /* When not optimizing, also output the static functions. (see |
437 | PR24561), but don't do so for always_inline functions, functions | |
438 | declared inline and nested functions. These were optimized out | |
439 | in the original implementation and it is unclear whether we want | |
440 | to change the behavior here. */ | |
c071eedc | 441 | if (((!opt_for_fn (decl, optimize) || flag_keep_static_functions) |
02774f2d | 442 | && !node->cpp_implicit_alias |
8efa224a | 443 | && !DECL_DISREGARD_INLINE_LIMITS (decl) |
444 | && !DECL_DECLARED_INLINE_P (decl) | |
445 | && !(DECL_CONTEXT (decl) | |
446 | && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)) | |
447 | && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl)) | |
02774f2d | 448 | node->force_output = 1; |
8efa224a | 449 | |
2c0b522d | 450 | /* If we've not yet emitted decl, tell the debug info about it. */ |
28df663b | 451 | if (!TREE_ASM_WRITTEN (decl)) |
2c0b522d | 452 | (*debug_hooks->deferred_inline_function) (decl); |
4e8871a0 | 453 | |
1f7747bd | 454 | if (!no_collect) |
6329636b | 455 | ggc_collect (); |
9a2639fc | 456 | |
35ee1c66 | 457 | if (symtab->state == CONSTRUCTION |
458 | && (node->needed_p () || node->referred_to_p ())) | |
02774f2d | 459 | enqueue_node (node); |
ae01b312 | 460 | } |
461 | ||
3db65b62 | 462 | /* Add the function FNDECL to the call graph. |
35ee1c66 | 463 | Unlike finalize_function, this function is intended to be used |
3db65b62 | 464 | by middle end and allows insertion of new function at arbitrary point |
465 | of compilation. The function can be either in high, low or SSA form | |
466 | GIMPLE. | |
467 | ||
468 | The function is assumed to be reachable and have address taken (so no | |
469 | API breaking optimizations are performed on it). | |
470 | ||
471 | Main work done by this function is to enqueue the function for later | |
472 | processing to avoid need the passes to be re-entrant. */ | |
473 | ||
474 | void | |
415d1b9a | 475 | cgraph_node::add_new_function (tree fndecl, bool lowered) |
3db65b62 | 476 | { |
3ea50c01 | 477 | gcc::pass_manager *passes = g->get_passes (); |
35ee1c66 | 478 | cgraph_node *node; |
f48e5cbc | 479 | |
480 | if (dump_file) | |
481 | { | |
482 | struct function *fn = DECL_STRUCT_FUNCTION (fndecl); | |
483 | const char *function_type = ((gimple_has_body_p (fndecl)) | |
484 | ? (lowered | |
485 | ? (gimple_in_ssa_p (fn) | |
486 | ? "ssa gimple" | |
487 | : "low gimple") | |
488 | : "high gimple") | |
489 | : "to-be-gimplified"); | |
490 | fprintf (dump_file, | |
491 | "Added new %s function %s to callgraph\n", | |
492 | function_type, | |
493 | fndecl_name (fndecl)); | |
494 | } | |
495 | ||
35ee1c66 | 496 | switch (symtab->state) |
3db65b62 | 497 | { |
35ee1c66 | 498 | case PARSING: |
499 | cgraph_node::finalize_function (fndecl, false); | |
ff2a5ada | 500 | break; |
35ee1c66 | 501 | case CONSTRUCTION: |
3db65b62 | 502 | /* Just enqueue function to be processed at nearest occurrence. */ |
415d1b9a | 503 | node = cgraph_node::get_create (fndecl); |
3db65b62 | 504 | if (lowered) |
505 | node->lowered = true; | |
347a47cb | 506 | cgraph_new_nodes.safe_push (node); |
3db65b62 | 507 | break; |
508 | ||
35ee1c66 | 509 | case IPA: |
510 | case IPA_SSA: | |
366970c6 | 511 | case IPA_SSA_AFTER_INLINING: |
35ee1c66 | 512 | case EXPANSION: |
3db65b62 | 513 | /* Bring the function into finalized state and enqueue for later |
514 | analyzing and compilation. */ | |
415d1b9a | 515 | node = cgraph_node::get_create (fndecl); |
3db65b62 | 516 | node->local.local = false; |
02774f2d | 517 | node->definition = true; |
518 | node->force_output = true; | |
35ee1c66 | 519 | if (!lowered && symtab->state == EXPANSION) |
3db65b62 | 520 | { |
521 | push_cfun (DECL_STRUCT_FUNCTION (fndecl)); | |
3db65b62 | 522 | gimple_register_cfg_hooks (); |
523 | bitmap_obstack_initialize (NULL); | |
3538ae0d | 524 | execute_pass_list (cfun, passes->all_lowering_passes); |
bcfddb5b | 525 | passes->execute_early_local_passes (); |
3db65b62 | 526 | bitmap_obstack_release (NULL); |
527 | pop_cfun (); | |
3db65b62 | 528 | |
529 | lowered = true; | |
530 | } | |
531 | if (lowered) | |
532 | node->lowered = true; | |
347a47cb | 533 | cgraph_new_nodes.safe_push (node); |
3db65b62 | 534 | break; |
535 | ||
35ee1c66 | 536 | case FINISHED: |
3db65b62 | 537 | /* At the very end of compilation we have to do all the work up |
538 | to expansion. */ | |
415d1b9a | 539 | node = cgraph_node::create (fndecl); |
3db65b62 | 540 | if (lowered) |
541 | node->lowered = true; | |
02774f2d | 542 | node->definition = true; |
415d1b9a | 543 | node->analyze (); |
3db65b62 | 544 | push_cfun (DECL_STRUCT_FUNCTION (fndecl)); |
3db65b62 | 545 | gimple_register_cfg_hooks (); |
546 | bitmap_obstack_initialize (NULL); | |
547 | if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl))) | |
bcfddb5b | 548 | g->get_passes ()->execute_early_local_passes (); |
3db65b62 | 549 | bitmap_obstack_release (NULL); |
3db65b62 | 550 | pop_cfun (); |
35ee1c66 | 551 | node->expand (); |
3db65b62 | 552 | break; |
553 | ||
554 | default: | |
555 | gcc_unreachable (); | |
556 | } | |
557 | ||
558 | /* Set a personality if required and we already passed EH lowering. */ | |
559 | if (lowered | |
560 | && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl)) | |
561 | == eh_personality_lang)) | |
562 | DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality (); | |
563 | } | |
564 | ||
0785e435 | 565 | /* Analyze the function scheduled to be output. */ |
415d1b9a | 566 | void |
567 | cgraph_node::analyze (void) | |
0785e435 | 568 | { |
415d1b9a | 569 | tree decl = this->decl; |
da5e1e7c | 570 | location_t saved_loc = input_location; |
571 | input_location = DECL_SOURCE_LOCATION (decl); | |
0785e435 | 572 | |
415d1b9a | 573 | if (thunk.thunk_p) |
91bf9d9a | 574 | { |
07f32730 | 575 | cgraph_node *t = cgraph_node::get (thunk.alias); |
576 | ||
577 | create_edge (t, NULL, 0, CGRAPH_FREQ_BASE); | |
578 | /* Target code in expand_thunk may need the thunk's target | |
579 | to be analyzed, so recurse here. */ | |
580 | if (!t->analyzed) | |
581 | t->analyze (); | |
582 | if (t->alias) | |
583 | { | |
584 | t = t->get_alias_target (); | |
585 | if (!t->analyzed) | |
586 | t->analyze (); | |
587 | } | |
415d1b9a | 588 | if (!expand_thunk (false, false)) |
6e1aa353 | 589 | { |
415d1b9a | 590 | thunk.alias = NULL; |
6e1aa353 | 591 | return; |
592 | } | |
415d1b9a | 593 | thunk.alias = NULL; |
91bf9d9a | 594 | } |
415d1b9a | 595 | if (alias) |
596 | resolve_alias (cgraph_node::get (alias_target)); | |
597 | else if (dispatcher_function) | |
cc8ef84f | 598 | { |
599 | /* Generate the dispatcher body of multi-versioned functions. */ | |
35ee1c66 | 600 | cgraph_function_version_info *dispatcher_version_info |
415d1b9a | 601 | = function_version (); |
cc8ef84f | 602 | if (dispatcher_version_info != NULL |
603 | && (dispatcher_version_info->dispatcher_resolver | |
604 | == NULL_TREE)) | |
605 | { | |
606 | tree resolver = NULL_TREE; | |
607 | gcc_assert (targetm.generate_version_dispatcher_body); | |
415d1b9a | 608 | resolver = targetm.generate_version_dispatcher_body (this); |
cc8ef84f | 609 | gcc_assert (resolver != NULL_TREE); |
610 | } | |
611 | } | |
91bf9d9a | 612 | else |
613 | { | |
91bf9d9a | 614 | push_cfun (DECL_STRUCT_FUNCTION (decl)); |
bfec3452 | 615 | |
415d1b9a | 616 | assign_assembler_name_if_neeeded (decl); |
6816d0c4 | 617 | |
91bf9d9a | 618 | /* Make sure to gimplify bodies only once. During analyzing a |
619 | function we lower it, which will require gimplified nested | |
620 | functions, so we can end up here with an already gimplified | |
621 | body. */ | |
e3a19533 | 622 | if (!gimple_has_body_p (decl)) |
91bf9d9a | 623 | gimplify_function_tree (decl); |
bfec3452 | 624 | |
47199071 | 625 | /* Lower the function. */ |
415d1b9a | 626 | if (!lowered) |
47199071 | 627 | { |
415d1b9a | 628 | if (nested) |
629 | lower_nested_functions (decl); | |
630 | gcc_assert (!nested); | |
47199071 | 631 | |
632 | gimple_register_cfg_hooks (); | |
633 | bitmap_obstack_initialize (NULL); | |
3538ae0d | 634 | execute_pass_list (cfun, g->get_passes ()->all_lowering_passes); |
47199071 | 635 | free_dominance_info (CDI_POST_DOMINATORS); |
636 | free_dominance_info (CDI_DOMINATORS); | |
637 | compact_blocks (); | |
638 | bitmap_obstack_release (NULL); | |
415d1b9a | 639 | lowered = true; |
47199071 | 640 | } |
641 | ||
91bf9d9a | 642 | pop_cfun (); |
643 | } | |
415d1b9a | 644 | analyzed = true; |
0785e435 | 645 | |
da5e1e7c | 646 | input_location = saved_loc; |
0785e435 | 647 | } |
648 | ||
c70f46b0 | 649 | /* C++ frontend produce same body aliases all over the place, even before PCH |
650 | gets streamed out. It relies on us linking the aliases with their function | |
651 | in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we | |
652 | first produce aliases without links, but once C++ FE is sure he won't sream | |
653 | PCH we build the links via this function. */ | |
654 | ||
655 | void | |
35ee1c66 | 656 | symbol_table::process_same_body_aliases (void) |
c70f46b0 | 657 | { |
452659af | 658 | symtab_node *node; |
48669653 | 659 | FOR_EACH_SYMBOL (node) |
02774f2d | 660 | if (node->cpp_implicit_alias && !node->analyzed) |
415d1b9a | 661 | node->resolve_alias |
662 | (TREE_CODE (node->alias_target) == VAR_DECL | |
97221fd7 | 663 | ? (symtab_node *)varpool_node::get_create (node->alias_target) |
415d1b9a | 664 | : (symtab_node *)cgraph_node::get_create (node->alias_target)); |
48669653 | 665 | cpp_implicit_aliases_done = true; |
c70f46b0 | 666 | } |
667 | ||
d05db70d | 668 | /* Process attributes common for vars and functions. */ |
669 | ||
670 | static void | |
6b722052 | 671 | process_common_attributes (symtab_node *node, tree decl) |
d05db70d | 672 | { |
673 | tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)); | |
674 | ||
675 | if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl))) | |
676 | { | |
677 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes, | |
678 | "%<weakref%> attribute should be accompanied with" | |
679 | " an %<alias%> attribute"); | |
680 | DECL_WEAK (decl) = 0; | |
40b32d93 | 681 | DECL_ATTRIBUTES (decl) = remove_attribute ("weakref", |
682 | DECL_ATTRIBUTES (decl)); | |
d05db70d | 683 | } |
6b722052 | 684 | |
685 | if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl))) | |
686 | node->no_reorder = 1; | |
d05db70d | 687 | } |
688 | ||
05806473 | 689 | /* Look for externally_visible and used attributes and mark cgraph nodes |
690 | accordingly. | |
691 | ||
692 | We cannot mark the nodes at the point the attributes are processed (in | |
693 | handle_*_attribute) because the copy of the declarations available at that | |
694 | point may not be canonical. For example, in: | |
695 | ||
696 | void f(); | |
697 | void f() __attribute__((used)); | |
698 | ||
699 | the declaration we see in handle_used_attribute will be the second | |
700 | declaration -- but the front end will subsequently merge that declaration | |
701 | with the original declaration and discard the second declaration. | |
702 | ||
35ee1c66 | 703 | Furthermore, we can't mark these nodes in finalize_function because: |
05806473 | 704 | |
705 | void f() {} | |
706 | void f() __attribute__((externally_visible)); | |
707 | ||
708 | is valid. | |
709 | ||
710 | So, we walk the nodes at the end of the translation unit, applying the | |
711 | attributes at that point. */ | |
712 | ||
713 | static void | |
35ee1c66 | 714 | process_function_and_variable_attributes (cgraph_node *first, |
098f44bc | 715 | varpool_node *first_var) |
05806473 | 716 | { |
35ee1c66 | 717 | cgraph_node *node; |
098f44bc | 718 | varpool_node *vnode; |
05806473 | 719 | |
35ee1c66 | 720 | for (node = symtab->first_function (); node != first; |
721 | node = symtab->next_function (node)) | |
05806473 | 722 | { |
02774f2d | 723 | tree decl = node->decl; |
83a23b05 | 724 | if (DECL_PRESERVE_P (decl)) |
415d1b9a | 725 | node->mark_force_output (); |
62433d51 | 726 | else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl))) |
05806473 | 727 | { |
02774f2d | 728 | if (! TREE_PUBLIC (node->decl)) |
729 | warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes, | |
712d2297 | 730 | "%<externally_visible%>" |
731 | " attribute have effect only on public objects"); | |
05806473 | 732 | } |
40b32d93 | 733 | if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)) |
02774f2d | 734 | && (node->definition && !node->alias)) |
40b32d93 | 735 | { |
02774f2d | 736 | warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes, |
40b32d93 | 737 | "%<weakref%> attribute ignored" |
738 | " because function is defined"); | |
739 | DECL_WEAK (decl) = 0; | |
740 | DECL_ATTRIBUTES (decl) = remove_attribute ("weakref", | |
741 | DECL_ATTRIBUTES (decl)); | |
742 | } | |
a522e9eb | 743 | |
744 | if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl)) | |
745 | && !DECL_DECLARED_INLINE_P (decl) | |
746 | /* redefining extern inline function makes it DECL_UNINLINABLE. */ | |
747 | && !DECL_UNINLINABLE (decl)) | |
748 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes, | |
749 | "always_inline function might not be inlinable"); | |
750 | ||
6b722052 | 751 | process_common_attributes (node, decl); |
05806473 | 752 | } |
35ee1c66 | 753 | for (vnode = symtab->first_variable (); vnode != first_var; |
754 | vnode = symtab->next_variable (vnode)) | |
05806473 | 755 | { |
02774f2d | 756 | tree decl = vnode->decl; |
aa419a52 | 757 | if (DECL_EXTERNAL (decl) |
df8d3e89 | 758 | && DECL_INITIAL (decl)) |
97221fd7 | 759 | varpool_node::finalize_decl (decl); |
83a23b05 | 760 | if (DECL_PRESERVE_P (decl)) |
02774f2d | 761 | vnode->force_output = true; |
62433d51 | 762 | else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl))) |
05806473 | 763 | { |
02774f2d | 764 | if (! TREE_PUBLIC (vnode->decl)) |
765 | warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes, | |
712d2297 | 766 | "%<externally_visible%>" |
767 | " attribute have effect only on public objects"); | |
05806473 | 768 | } |
40b32d93 | 769 | if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)) |
02774f2d | 770 | && vnode->definition |
40b32d93 | 771 | && DECL_INITIAL (decl)) |
772 | { | |
02774f2d | 773 | warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes, |
40b32d93 | 774 | "%<weakref%> attribute ignored" |
775 | " because variable is initialized"); | |
776 | DECL_WEAK (decl) = 0; | |
777 | DECL_ATTRIBUTES (decl) = remove_attribute ("weakref", | |
778 | DECL_ATTRIBUTES (decl)); | |
779 | } | |
6b722052 | 780 | process_common_attributes (vnode, decl); |
05806473 | 781 | } |
782 | } | |
783 | ||
ff2a5ada | 784 | /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the |
785 | middle end to output the variable to asm file, if needed or externally | |
786 | visible. */ | |
787 | ||
788 | void | |
97221fd7 | 789 | varpool_node::finalize_decl (tree decl) |
ff2a5ada | 790 | { |
97221fd7 | 791 | varpool_node *node = varpool_node::get_create (decl); |
ff2a5ada | 792 | |
2d4bf241 | 793 | gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl)); |
ff2a5ada | 794 | |
02774f2d | 795 | if (node->definition) |
ff2a5ada | 796 | return; |
78db4b4d | 797 | /* Set definition first before calling notice_global_symbol so that |
798 | it is available to notice_global_symbol. */ | |
02774f2d | 799 | node->definition = true; |
78db4b4d | 800 | notice_global_symbol (decl); |
ff2a5ada | 801 | if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl) |
802 | /* Traditionally we do not eliminate static variables when not | |
803 | optimizing and when not doing toplevel reoder. */ | |
6b722052 | 804 | || node->no_reorder |
805 | || ((!flag_toplevel_reorder | |
806 | && !DECL_COMDAT (node->decl) | |
807 | && !DECL_ARTIFICIAL (node->decl)))) | |
02774f2d | 808 | node->force_output = true; |
ff2a5ada | 809 | |
35ee1c66 | 810 | if (symtab->state == CONSTRUCTION |
811 | && (node->needed_p () || node->referred_to_p ())) | |
02774f2d | 812 | enqueue_node (node); |
35ee1c66 | 813 | if (symtab->state >= IPA_SSA) |
97221fd7 | 814 | node->analyze (); |
3d1c0354 | 815 | /* Some frontends produce various interface variables after compilation |
816 | finished. */ | |
35ee1c66 | 817 | if (symtab->state == FINISHED |
818 | || (!flag_toplevel_reorder | |
819 | && symtab->state == EXPANSION)) | |
97221fd7 | 820 | node->assemble_decl (); |
058a1b7a | 821 | |
822 | if (DECL_INITIAL (decl)) | |
823 | chkp_register_var_initializer (decl); | |
ff2a5ada | 824 | } |
825 | ||
98e7a67f | 826 | /* EDGE is an polymorphic call. Mark all possible targets as reachable |
827 | and if there is only one target, perform trivial devirtualization. | |
828 | REACHABLE_CALL_TARGETS collects target lists we already walked to | |
829 | avoid udplicate work. */ | |
830 | ||
831 | static void | |
431205b7 | 832 | walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets, |
35ee1c66 | 833 | cgraph_edge *edge) |
98e7a67f | 834 | { |
835 | unsigned int i; | |
836 | void *cache_token; | |
837 | bool final; | |
838 | vec <cgraph_node *>targets | |
839 | = possible_polymorphic_call_targets | |
840 | (edge, &final, &cache_token); | |
841 | ||
431205b7 | 842 | if (!reachable_call_targets->add (cache_token)) |
98e7a67f | 843 | { |
35ee1c66 | 844 | if (symtab->dump_file) |
98e7a67f | 845 | dump_possible_polymorphic_call_targets |
35ee1c66 | 846 | (symtab->dump_file, edge); |
98e7a67f | 847 | |
9af5ce0c | 848 | for (i = 0; i < targets.length (); i++) |
98e7a67f | 849 | { |
850 | /* Do not bother to mark virtual methods in anonymous namespace; | |
851 | either we will find use of virtual table defining it, or it is | |
852 | unused. */ | |
02774f2d | 853 | if (targets[i]->definition |
98e7a67f | 854 | && TREE_CODE |
02774f2d | 855 | (TREE_TYPE (targets[i]->decl)) |
98e7a67f | 856 | == METHOD_TYPE |
857 | && !type_in_anonymous_namespace_p | |
1fda15e2 | 858 | (TYPE_METHOD_BASETYPE (TREE_TYPE (targets[i]->decl)))) |
859 | enqueue_node (targets[i]); | |
98e7a67f | 860 | } |
861 | } | |
862 | ||
863 | /* Very trivial devirtualization; when the type is | |
864 | final or anonymous (so we know all its derivation) | |
865 | and there is only one possible virtual call target, | |
866 | make the edge direct. */ | |
867 | if (final) | |
868 | { | |
ceb49bba | 869 | if (targets.length () <= 1 && dbg_cnt (devirt)) |
98e7a67f | 870 | { |
e2fa5d74 | 871 | cgraph_node *target; |
872 | if (targets.length () == 1) | |
873 | target = targets[0]; | |
874 | else | |
415d1b9a | 875 | target = cgraph_node::create |
876 | (builtin_decl_implicit (BUILT_IN_UNREACHABLE)); | |
e2fa5d74 | 877 | |
35ee1c66 | 878 | if (symtab->dump_file) |
98e7a67f | 879 | { |
35ee1c66 | 880 | fprintf (symtab->dump_file, |
98e7a67f | 881 | "Devirtualizing call: "); |
35ee1c66 | 882 | print_gimple_stmt (symtab->dump_file, |
98e7a67f | 883 | edge->call_stmt, 0, |
884 | TDF_SLIM); | |
885 | } | |
ceb49bba | 886 | if (dump_enabled_p ()) |
887 | { | |
4c8041d7 | 888 | location_t locus = gimple_location_safe (edge->call_stmt); |
ceb49bba | 889 | dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus, |
890 | "devirtualizing call in %s to %s\n", | |
891 | edge->caller->name (), target->name ()); | |
892 | } | |
893 | ||
35ee1c66 | 894 | edge->make_direct (target); |
895 | edge->redirect_call_stmt_to_callee (); | |
058a1b7a | 896 | |
897 | /* Call to __builtin_unreachable shouldn't be instrumented. */ | |
898 | if (!targets.length ()) | |
899 | gimple_call_set_with_bounds (edge->call_stmt, false); | |
900 | ||
35ee1c66 | 901 | if (symtab->dump_file) |
98e7a67f | 902 | { |
35ee1c66 | 903 | fprintf (symtab->dump_file, |
98e7a67f | 904 | "Devirtualized as: "); |
35ee1c66 | 905 | print_gimple_stmt (symtab->dump_file, |
98e7a67f | 906 | edge->call_stmt, 0, |
907 | TDF_SLIM); | |
908 | } | |
909 | } | |
910 | } | |
911 | } | |
912 | ||
22c5bcc6 | 913 | /* Issue appropriate warnings for the global declaration DECL. */ |
914 | ||
915 | static void | |
916 | check_global_declaration (symtab_node *snode) | |
917 | { | |
918 | tree decl = snode->decl; | |
919 | ||
920 | /* Warn about any function declared static but not defined. We don't | |
921 | warn about variables, because many programs have static variables | |
922 | that exist only to get some text into the object file. */ | |
923 | if (TREE_CODE (decl) == FUNCTION_DECL | |
924 | && DECL_INITIAL (decl) == 0 | |
925 | && DECL_EXTERNAL (decl) | |
926 | && ! DECL_ARTIFICIAL (decl) | |
927 | && ! TREE_NO_WARNING (decl) | |
928 | && ! TREE_PUBLIC (decl) | |
929 | && (warn_unused_function | |
930 | || snode->referred_to_p (/*include_self=*/false))) | |
931 | { | |
932 | if (snode->referred_to_p (/*include_self=*/false)) | |
933 | pedwarn (input_location, 0, "%q+F used but never defined", decl); | |
934 | else | |
935 | warning (OPT_Wunused_function, "%q+F declared %<static%> but never defined", decl); | |
936 | /* This symbol is effectively an "extern" declaration now. */ | |
937 | TREE_PUBLIC (decl) = 1; | |
938 | } | |
939 | ||
940 | /* Warn about static fns or vars defined but not used. */ | |
941 | if (((warn_unused_function && TREE_CODE (decl) == FUNCTION_DECL) | |
942 | || (((warn_unused_variable && ! TREE_READONLY (decl)) | |
943 | || (warn_unused_const_variable && TREE_READONLY (decl))) | |
944 | && TREE_CODE (decl) == VAR_DECL)) | |
945 | && ! DECL_IN_SYSTEM_HEADER (decl) | |
946 | && ! snode->referred_to_p (/*include_self=*/false) | |
947 | /* This TREE_USED check is needed in addition to referred_to_p | |
948 | above, because the `__unused__' attribute is not being | |
949 | considered for referred_to_p. */ | |
950 | && ! TREE_USED (decl) | |
951 | /* The TREE_USED bit for file-scope decls is kept in the identifier, | |
952 | to handle multiple external decls in different scopes. */ | |
953 | && ! (DECL_NAME (decl) && TREE_USED (DECL_NAME (decl))) | |
954 | && ! DECL_EXTERNAL (decl) | |
955 | && ! DECL_ARTIFICIAL (decl) | |
956 | && ! DECL_ABSTRACT_ORIGIN (decl) | |
957 | && ! TREE_PUBLIC (decl) | |
958 | /* A volatile variable might be used in some non-obvious way. */ | |
959 | && ! TREE_THIS_VOLATILE (decl) | |
960 | /* Global register variables must be declared to reserve them. */ | |
961 | && ! (TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)) | |
962 | /* Global ctors and dtors are called by the runtime. */ | |
963 | && (TREE_CODE (decl) != FUNCTION_DECL | |
964 | || (!DECL_STATIC_CONSTRUCTOR (decl) | |
965 | && !DECL_STATIC_DESTRUCTOR (decl))) | |
966 | /* Otherwise, ask the language. */ | |
967 | && lang_hooks.decls.warn_unused_global (decl)) | |
968 | warning_at (DECL_SOURCE_LOCATION (decl), | |
969 | (TREE_CODE (decl) == FUNCTION_DECL) | |
970 | ? OPT_Wunused_function | |
971 | : (TREE_READONLY (decl) | |
972 | ? OPT_Wunused_const_variable | |
973 | : OPT_Wunused_variable), | |
974 | "%qD defined but not used", decl); | |
975 | } | |
2dc9831f | 976 | |
ff2a5ada | 977 | /* Discover all functions and variables that are trivially needed, analyze |
978 | them as well as all functions and variables referred by them */ | |
415309e2 | 979 | static cgraph_node *first_analyzed; |
980 | static varpool_node *first_analyzed_var; | |
ae01b312 | 981 | |
3a1c9df2 | 982 | /* FIRST_TIME is set to TRUE for the first time we are called for a |
983 | translation unit from finalize_compilation_unit() or false | |
984 | otherwise. */ | |
985 | ||
aeeb194b | 986 | static void |
3a1c9df2 | 987 | analyze_functions (bool first_time) |
ae01b312 | 988 | { |
c1dcd13c | 989 | /* Keep track of already processed nodes when called multiple times for |
06b27565 | 990 | intermodule optimization. */ |
35ee1c66 | 991 | cgraph_node *first_handled = first_analyzed; |
098f44bc | 992 | varpool_node *first_handled_var = first_analyzed_var; |
431205b7 | 993 | hash_set<void *> reachable_call_targets; |
ff2a5ada | 994 | |
452659af | 995 | symtab_node *node; |
996 | symtab_node *next; | |
ff2a5ada | 997 | int i; |
35ee1c66 | 998 | ipa_ref *ref; |
ff2a5ada | 999 | bool changed = true; |
fd9fde78 | 1000 | location_t saved_loc = input_location; |
ae01b312 | 1001 | |
f1c35659 | 1002 | bitmap_obstack_initialize (NULL); |
35ee1c66 | 1003 | symtab->state = CONSTRUCTION; |
fd9fde78 | 1004 | input_location = UNKNOWN_LOCATION; |
ae01b312 | 1005 | |
48669653 | 1006 | /* Ugly, but the fixup can not happen at a time same body alias is created; |
1007 | C++ FE is confused about the COMDAT groups being right. */ | |
35ee1c66 | 1008 | if (symtab->cpp_implicit_aliases_done) |
48669653 | 1009 | FOR_EACH_SYMBOL (node) |
02774f2d | 1010 | if (node->cpp_implicit_alias) |
415d1b9a | 1011 | node->fixup_same_cpp_alias_visibility (node->get_alias_target ()); |
d1f68cd8 | 1012 | build_type_inheritance_graph (); |
48669653 | 1013 | |
ff2a5ada | 1014 | /* Analysis adds static variables that in turn adds references to new functions. |
1015 | So we need to iterate the process until it stabilize. */ | |
1016 | while (changed) | |
ae01b312 | 1017 | { |
ff2a5ada | 1018 | changed = false; |
1019 | process_function_and_variable_attributes (first_analyzed, | |
1020 | first_analyzed_var); | |
1021 | ||
1022 | /* First identify the trivially needed symbols. */ | |
35ee1c66 | 1023 | for (node = symtab->first_symbol (); |
02774f2d | 1024 | node != first_analyzed |
1025 | && node != first_analyzed_var; node = node->next) | |
9b8fb23a | 1026 | { |
1d9ca4f0 | 1027 | /* Convert COMDAT group designators to IDENTIFIER_NODEs. */ |
1028 | node->get_comdat_group_id (); | |
35ee1c66 | 1029 | if (node->needed_p ()) |
ff2a5ada | 1030 | { |
1031 | enqueue_node (node); | |
35ee1c66 | 1032 | if (!changed && symtab->dump_file) |
1033 | fprintf (symtab->dump_file, "Trivially needed symbols:"); | |
ff2a5ada | 1034 | changed = true; |
35ee1c66 | 1035 | if (symtab->dump_file) |
1036 | fprintf (symtab->dump_file, " %s", node->asm_name ()); | |
1037 | if (!changed && symtab->dump_file) | |
1038 | fprintf (symtab->dump_file, "\n"); | |
ff2a5ada | 1039 | } |
02774f2d | 1040 | if (node == first_analyzed |
1041 | || node == first_analyzed_var) | |
ff2a5ada | 1042 | break; |
9b8fb23a | 1043 | } |
35ee1c66 | 1044 | symtab->process_new_functions (); |
1045 | first_analyzed_var = symtab->first_variable (); | |
1046 | first_analyzed = symtab->first_function (); | |
638531ad | 1047 | |
35ee1c66 | 1048 | if (changed && symtab->dump_file) |
1049 | fprintf (symtab->dump_file, "\n"); | |
2c0b522d | 1050 | |
ff2a5ada | 1051 | /* Lower representation, build callgraph edges and references for all trivially |
1052 | needed symbols and all symbols referred by them. */ | |
3cb3fce5 | 1053 | while (queued_nodes != &symtab_terminator) |
61c2c7b1 | 1054 | { |
ff2a5ada | 1055 | changed = true; |
3cb3fce5 | 1056 | node = queued_nodes; |
1057 | queued_nodes = (symtab_node *)queued_nodes->aux; | |
13cbeaac | 1058 | cgraph_node *cnode = dyn_cast <cgraph_node *> (node); |
02774f2d | 1059 | if (cnode && cnode->definition) |
ff2a5ada | 1060 | { |
35ee1c66 | 1061 | cgraph_edge *edge; |
02774f2d | 1062 | tree decl = cnode->decl; |
ff2a5ada | 1063 | |
2dc9831f | 1064 | /* ??? It is possible to create extern inline function |
1065 | and later using weak alias attribute to kill its body. | |
1066 | See gcc.c-torture/compile/20011119-1.c */ | |
ff2a5ada | 1067 | if (!DECL_STRUCT_FUNCTION (decl) |
02774f2d | 1068 | && !cnode->alias |
cc8ef84f | 1069 | && !cnode->thunk.thunk_p |
1070 | && !cnode->dispatcher_function) | |
ff2a5ada | 1071 | { |
415d1b9a | 1072 | cnode->reset (); |
ff2a5ada | 1073 | cnode->local.redefined_extern_inline = true; |
1074 | continue; | |
1075 | } | |
61c2c7b1 | 1076 | |
02774f2d | 1077 | if (!cnode->analyzed) |
415d1b9a | 1078 | cnode->analyze (); |
d544ceff | 1079 | |
ff2a5ada | 1080 | for (edge = cnode->callees; edge; edge = edge->next_callee) |
af822fd2 | 1081 | if (edge->callee->definition |
1082 | && (!DECL_EXTERNAL (edge->callee->decl) | |
1083 | /* When not optimizing, do not try to analyze extern | |
1084 | inline functions. Doing so is pointless. */ | |
1085 | || opt_for_fn (edge->callee->decl, optimize) | |
1086 | /* Weakrefs needs to be preserved. */ | |
1087 | || edge->callee->alias | |
1088 | /* always_inline functions are inlined aven at -O0. */ | |
1089 | || lookup_attribute | |
1090 | ("always_inline", | |
1091 | DECL_ATTRIBUTES (edge->callee->decl)) | |
1092 | /* Multiversioned functions needs the dispatcher to | |
1093 | be produced locally even for extern functions. */ | |
1094 | || edge->callee->function_version ())) | |
02774f2d | 1095 | enqueue_node (edge->callee); |
d1f68cd8 | 1096 | if (opt_for_fn (cnode->decl, optimize) |
1097 | && opt_for_fn (cnode->decl, flag_devirtualize)) | |
5514adf9 | 1098 | { |
35ee1c66 | 1099 | cgraph_edge *next; |
98e7a67f | 1100 | |
1101 | for (edge = cnode->indirect_calls; edge; edge = next) | |
1e4f4118 | 1102 | { |
1103 | next = edge->next_callee; | |
1104 | if (edge->indirect_info->polymorphic) | |
431205b7 | 1105 | walk_polymorphic_call_targets (&reachable_call_targets, |
98e7a67f | 1106 | edge); |
1e4f4118 | 1107 | } |
5514adf9 | 1108 | } |
ff2a5ada | 1109 | |
2dc9831f | 1110 | /* If decl is a clone of an abstract function, |
1111 | mark that abstract function so that we don't release its body. | |
1112 | The DECL_INITIAL() of that abstract function declaration | |
1113 | will be later needed to output debug info. */ | |
ff2a5ada | 1114 | if (DECL_ABSTRACT_ORIGIN (decl)) |
1115 | { | |
35ee1c66 | 1116 | cgraph_node *origin_node |
b3a78a8c | 1117 | = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl)); |
abb1a237 | 1118 | origin_node->used_as_abstract_origin = true; |
ff2a5ada | 1119 | } |
ff2a5ada | 1120 | } |
2dc9831f | 1121 | else |
1122 | { | |
13cbeaac | 1123 | varpool_node *vnode = dyn_cast <varpool_node *> (node); |
02774f2d | 1124 | if (vnode && vnode->definition && !vnode->analyzed) |
97221fd7 | 1125 | vnode->analyze (); |
2dc9831f | 1126 | } |
ff2a5ada | 1127 | |
02774f2d | 1128 | if (node->same_comdat_group) |
ff2a5ada | 1129 | { |
452659af | 1130 | symtab_node *next; |
02774f2d | 1131 | for (next = node->same_comdat_group; |
ff2a5ada | 1132 | next != node; |
02774f2d | 1133 | next = next->same_comdat_group) |
af822fd2 | 1134 | if (!next->comdat_local_p ()) |
1135 | enqueue_node (next); | |
ff2a5ada | 1136 | } |
51ce5652 | 1137 | for (i = 0; node->iterate_reference (i, ref); i++) |
af822fd2 | 1138 | if (ref->referred->definition |
1139 | && (!DECL_EXTERNAL (ref->referred->decl) | |
1140 | || ((TREE_CODE (ref->referred->decl) != FUNCTION_DECL | |
1141 | && optimize) | |
1142 | || (TREE_CODE (ref->referred->decl) == FUNCTION_DECL | |
1143 | && opt_for_fn (ref->referred->decl, optimize)) | |
8a407369 | 1144 | || node->alias |
af822fd2 | 1145 | || ref->referred->alias))) |
ff2a5ada | 1146 | enqueue_node (ref->referred); |
35ee1c66 | 1147 | symtab->process_new_functions (); |
ff2a5ada | 1148 | } |
ae01b312 | 1149 | } |
d1f68cd8 | 1150 | update_type_inheritance_graph (); |
2c0b522d | 1151 | |
aa5e06c7 | 1152 | /* Collect entry points to the unit. */ |
35ee1c66 | 1153 | if (symtab->dump_file) |
3d7bfc56 | 1154 | { |
35ee1c66 | 1155 | fprintf (symtab->dump_file, "\n\nInitial "); |
1156 | symtab_node::dump_table (symtab->dump_file); | |
3d7bfc56 | 1157 | } |
e6d2b2d8 | 1158 | |
3a1c9df2 | 1159 | if (first_time) |
1160 | { | |
1161 | symtab_node *snode; | |
1162 | FOR_EACH_SYMBOL (snode) | |
22c5bcc6 | 1163 | check_global_declaration (snode); |
3a1c9df2 | 1164 | } |
1165 | ||
35ee1c66 | 1166 | if (symtab->dump_file) |
1167 | fprintf (symtab->dump_file, "\nRemoving unused symbols:"); | |
ae01b312 | 1168 | |
35ee1c66 | 1169 | for (node = symtab->first_symbol (); |
02774f2d | 1170 | node != first_handled |
1171 | && node != first_handled_var; node = next) | |
ae01b312 | 1172 | { |
02774f2d | 1173 | next = node->next; |
35ee1c66 | 1174 | if (!node->aux && !node->referred_to_p ()) |
ae01b312 | 1175 | { |
35ee1c66 | 1176 | if (symtab->dump_file) |
1177 | fprintf (symtab->dump_file, " %s", node->name ()); | |
3a1c9df2 | 1178 | |
1179 | /* See if the debugger can use anything before the DECL | |
1180 | passes away. Perhaps it can notice a DECL that is now a | |
1181 | constant and can tag the early DIE with an appropriate | |
1182 | attribute. | |
1183 | ||
1184 | Otherwise, this is the last chance the debug_hooks have | |
1185 | at looking at optimized away DECLs, since | |
1186 | late_global_decl will subsequently be called from the | |
1187 | contents of the now pruned symbol table. */ | |
1188 | if (!decl_function_context (node->decl)) | |
1189 | (*debug_hooks->late_global_decl) (node->decl); | |
1190 | ||
415d1b9a | 1191 | node->remove (); |
9b8fb23a | 1192 | continue; |
ae01b312 | 1193 | } |
13cbeaac | 1194 | if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node)) |
ff2a5ada | 1195 | { |
02774f2d | 1196 | tree decl = node->decl; |
ff2a5ada | 1197 | |
02774f2d | 1198 | if (cnode->definition && !gimple_has_body_p (decl) |
1199 | && !cnode->alias | |
ff2a5ada | 1200 | && !cnode->thunk.thunk_p) |
415d1b9a | 1201 | cnode->reset (); |
ff2a5ada | 1202 | |
02774f2d | 1203 | gcc_assert (!cnode->definition || cnode->thunk.thunk_p |
1204 | || cnode->alias | |
ff2a5ada | 1205 | || gimple_has_body_p (decl)); |
02774f2d | 1206 | gcc_assert (cnode->analyzed == cnode->definition); |
ff2a5ada | 1207 | } |
02774f2d | 1208 | node->aux = NULL; |
ae01b312 | 1209 | } |
02774f2d | 1210 | for (;node; node = node->next) |
1211 | node->aux = NULL; | |
35ee1c66 | 1212 | first_analyzed = symtab->first_function (); |
1213 | first_analyzed_var = symtab->first_variable (); | |
1214 | if (symtab->dump_file) | |
e4200070 | 1215 | { |
35ee1c66 | 1216 | fprintf (symtab->dump_file, "\n\nReclaimed "); |
1217 | symtab_node::dump_table (symtab->dump_file); | |
e4200070 | 1218 | } |
f1c35659 | 1219 | bitmap_obstack_release (NULL); |
ae01b312 | 1220 | ggc_collect (); |
d5b66e83 | 1221 | /* Initialize assembler name hash, in particular we want to trigger C++ |
1222 | mangling and same body alias creation before we free DECL_ARGUMENTS | |
1223 | used by it. */ | |
1224 | if (!seen_error ()) | |
35ee1c66 | 1225 | symtab->symtab_initialize_asm_name_hash (); |
fd9fde78 | 1226 | |
1227 | input_location = saved_loc; | |
aeeb194b | 1228 | } |
1229 | ||
3a849bc1 | 1230 | /* Translate the ugly representation of aliases as alias pairs into nice |
1231 | representation in callgraph. We don't handle all cases yet, | |
a3ddd82f | 1232 | unfortunately. */ |
3a849bc1 | 1233 | |
1234 | static void | |
1235 | handle_alias_pairs (void) | |
1236 | { | |
1237 | alias_pair *p; | |
1238 | unsigned i; | |
3a849bc1 | 1239 | |
f1f41a6c | 1240 | for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);) |
3a849bc1 | 1241 | { |
35ee1c66 | 1242 | symtab_node *target_node = symtab_node::get_for_asmname (p->target); |
48c84ee3 | 1243 | |
a3ddd82f | 1244 | /* Weakrefs with target not defined in current unit are easy to handle: |
1245 | they behave just as external variables except we need to note the | |
1246 | alias flag to later output the weakref pseudo op into asm file. */ | |
1247 | if (!target_node | |
1248 | && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL) | |
badeded8 | 1249 | { |
415d1b9a | 1250 | symtab_node *node = symtab_node::get (p->decl); |
48669653 | 1251 | if (node) |
fc8456b4 | 1252 | { |
02774f2d | 1253 | node->alias_target = p->target; |
1254 | node->weakref = true; | |
1255 | node->alias = true; | |
fc8456b4 | 1256 | } |
f1f41a6c | 1257 | alias_pairs->unordered_remove (i); |
48c84ee3 | 1258 | continue; |
badeded8 | 1259 | } |
48c84ee3 | 1260 | else if (!target_node) |
3a849bc1 | 1261 | { |
48c84ee3 | 1262 | error ("%q+D aliased to undefined symbol %qE", p->decl, p->target); |
415d1b9a | 1263 | symtab_node *node = symtab_node::get (p->decl); |
a3ddd82f | 1264 | if (node) |
02774f2d | 1265 | node->alias = false; |
f1f41a6c | 1266 | alias_pairs->unordered_remove (i); |
48c84ee3 | 1267 | continue; |
1268 | } | |
1269 | ||
02774f2d | 1270 | if (DECL_EXTERNAL (target_node->decl) |
afea39ad | 1271 | /* We use local aliases for C++ thunks to force the tailcall |
1272 | to bind locally. This is a hack - to keep it working do | |
1273 | the following (which is not strictly correct). */ | |
e4b77b71 | 1274 | && (TREE_CODE (target_node->decl) != FUNCTION_DECL |
02774f2d | 1275 | || ! DECL_VIRTUAL_P (target_node->decl)) |
afea39ad | 1276 | && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl))) |
1277 | { | |
1278 | error ("%q+D aliased to external symbol %qE", | |
1279 | p->decl, p->target); | |
1280 | } | |
1281 | ||
48c84ee3 | 1282 | if (TREE_CODE (p->decl) == FUNCTION_DECL |
13cbeaac | 1283 | && target_node && is_a <cgraph_node *> (target_node)) |
48c84ee3 | 1284 | { |
35ee1c66 | 1285 | cgraph_node *src_node = cgraph_node::get (p->decl); |
02774f2d | 1286 | if (src_node && src_node->definition) |
415d1b9a | 1287 | src_node->reset (); |
1288 | cgraph_node::create_alias (p->decl, target_node->decl); | |
f1f41a6c | 1289 | alias_pairs->unordered_remove (i); |
48c84ee3 | 1290 | } |
1291 | else if (TREE_CODE (p->decl) == VAR_DECL | |
13cbeaac | 1292 | && target_node && is_a <varpool_node *> (target_node)) |
48c84ee3 | 1293 | { |
97221fd7 | 1294 | varpool_node::create_alias (p->decl, target_node->decl); |
f1f41a6c | 1295 | alias_pairs->unordered_remove (i); |
48c84ee3 | 1296 | } |
1297 | else | |
1298 | { | |
1299 | error ("%q+D alias in between function and variable is not supported", | |
1300 | p->decl); | |
1301 | warning (0, "%q+D aliased declaration", | |
02774f2d | 1302 | target_node->decl); |
f1f41a6c | 1303 | alias_pairs->unordered_remove (i); |
3a849bc1 | 1304 | } |
1305 | } | |
f1f41a6c | 1306 | vec_free (alias_pairs); |
3a849bc1 | 1307 | } |
1308 | ||
8f69fd82 | 1309 | |
ae01b312 | 1310 | /* Figure out what functions we want to assemble. */ |
1311 | ||
1312 | static void | |
cf951b1a | 1313 | mark_functions_to_output (void) |
ae01b312 | 1314 | { |
61c2c7b1 | 1315 | bool check_same_comdat_groups = false; |
382ecba7 | 1316 | cgraph_node *node; |
61c2c7b1 | 1317 | |
382ecba7 | 1318 | if (flag_checking) |
1319 | FOR_EACH_FUNCTION (node) | |
1320 | gcc_assert (!node->process); | |
ae01b312 | 1321 | |
7c455d87 | 1322 | FOR_EACH_FUNCTION (node) |
ae01b312 | 1323 | { |
02774f2d | 1324 | tree decl = node->decl; |
a0c938f0 | 1325 | |
02774f2d | 1326 | gcc_assert (!node->process || node->same_comdat_group); |
61c2c7b1 | 1327 | if (node->process) |
1328 | continue; | |
d7c6d889 | 1329 | |
e6d2b2d8 | 1330 | /* We need to output all local functions that are used and not |
1331 | always inlined, as well as those that are reachable from | |
1332 | outside the current compilation unit. */ | |
02774f2d | 1333 | if (node->analyzed |
91bf9d9a | 1334 | && !node->thunk.thunk_p |
02774f2d | 1335 | && !node->alias |
b0cdf642 | 1336 | && !node->global.inlined_to |
4ee9c684 | 1337 | && !TREE_ASM_WRITTEN (decl) |
ae01b312 | 1338 | && !DECL_EXTERNAL (decl)) |
61c2c7b1 | 1339 | { |
1340 | node->process = 1; | |
02774f2d | 1341 | if (node->same_comdat_group) |
61c2c7b1 | 1342 | { |
35ee1c66 | 1343 | cgraph_node *next; |
415d1b9a | 1344 | for (next = dyn_cast<cgraph_node *> (node->same_comdat_group); |
61c2c7b1 | 1345 | next != node; |
415d1b9a | 1346 | next = dyn_cast<cgraph_node *> (next->same_comdat_group)) |
468088ac | 1347 | if (!next->thunk.thunk_p && !next->alias |
415d1b9a | 1348 | && !next->comdat_local_p ()) |
91bf9d9a | 1349 | next->process = 1; |
61c2c7b1 | 1350 | } |
1351 | } | |
02774f2d | 1352 | else if (node->same_comdat_group) |
61c2c7b1 | 1353 | { |
382ecba7 | 1354 | if (flag_checking) |
1355 | check_same_comdat_groups = true; | |
61c2c7b1 | 1356 | } |
cc636d56 | 1357 | else |
9cee7c3f | 1358 | { |
1359 | /* We should've reclaimed all functions that are not needed. */ | |
382ecba7 | 1360 | if (flag_checking |
1361 | && !node->global.inlined_to | |
1a1a827a | 1362 | && gimple_has_body_p (decl) |
08843223 | 1363 | /* FIXME: in ltrans unit when offline copy is outside partition but inline copies |
1364 | are inside partition, we can end up not removing the body since we no longer | |
1365 | have analyzed node pointing to it. */ | |
02774f2d | 1366 | && !node->in_other_partition |
1367 | && !node->alias | |
2d4bf241 | 1368 | && !node->clones |
9cee7c3f | 1369 | && !DECL_EXTERNAL (decl)) |
1370 | { | |
415d1b9a | 1371 | node->debug (); |
9cee7c3f | 1372 | internal_error ("failed to reclaim unneeded function"); |
1373 | } | |
75a70cf9 | 1374 | gcc_assert (node->global.inlined_to |
1a1a827a | 1375 | || !gimple_has_body_p (decl) |
02774f2d | 1376 | || node->in_other_partition |
aa419a52 | 1377 | || node->clones |
1378 | || DECL_ARTIFICIAL (decl) | |
9cee7c3f | 1379 | || DECL_EXTERNAL (decl)); |
1380 | ||
1381 | } | |
a0c938f0 | 1382 | |
961e3b13 | 1383 | } |
382ecba7 | 1384 | if (flag_checking && check_same_comdat_groups) |
7c455d87 | 1385 | FOR_EACH_FUNCTION (node) |
02774f2d | 1386 | if (node->same_comdat_group && !node->process) |
61c2c7b1 | 1387 | { |
02774f2d | 1388 | tree decl = node->decl; |
61c2c7b1 | 1389 | if (!node->global.inlined_to |
1390 | && gimple_has_body_p (decl) | |
6d36105a | 1391 | /* FIXME: in an ltrans unit when the offline copy is outside a |
1392 | partition but inline copies are inside a partition, we can | |
1393 | end up not removing the body since we no longer have an | |
1394 | analyzed node pointing to it. */ | |
02774f2d | 1395 | && !node->in_other_partition |
afea39ad | 1396 | && !node->clones |
61c2c7b1 | 1397 | && !DECL_EXTERNAL (decl)) |
1398 | { | |
415d1b9a | 1399 | node->debug (); |
6d36105a | 1400 | internal_error ("failed to reclaim unneeded function in same " |
1401 | "comdat group"); | |
61c2c7b1 | 1402 | } |
1403 | } | |
961e3b13 | 1404 | } |
1405 | ||
28454517 | 1406 | /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function |
cc8ef84f | 1407 | in lowered gimple form. IN_SSA is true if the gimple is in SSA. |
28454517 | 1408 | |
1409 | Set current_function_decl and cfun to newly constructed empty function body. | |
1410 | return basic block in the function body. */ | |
1411 | ||
cc8ef84f | 1412 | basic_block |
3a9f48e7 | 1413 | init_lowered_empty_function (tree decl, bool in_ssa, gcov_type count) |
28454517 | 1414 | { |
1415 | basic_block bb; | |
3a9f48e7 | 1416 | edge e; |
28454517 | 1417 | |
1418 | current_function_decl = decl; | |
1419 | allocate_struct_function (decl, false); | |
1420 | gimple_register_cfg_hooks (); | |
1421 | init_empty_tree_cfg (); | |
cc8ef84f | 1422 | |
1423 | if (in_ssa) | |
1424 | { | |
1425 | init_tree_ssa (cfun); | |
1426 | init_ssa_operands (cfun); | |
1427 | cfun->gimple_df->in_ssa_p = true; | |
7eacb0dd | 1428 | cfun->curr_properties |= PROP_ssa; |
cc8ef84f | 1429 | } |
1430 | ||
28454517 | 1431 | DECL_INITIAL (decl) = make_node (BLOCK); |
1432 | ||
1433 | DECL_SAVED_TREE (decl) = error_mark_node; | |
7eacb0dd | 1434 | cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any |
1435 | | PROP_cfg | PROP_loops); | |
1436 | ||
25a27413 | 1437 | set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ()); |
7eacb0dd | 1438 | init_loops_structure (cfun, loops_for_fn (cfun), 1); |
1439 | loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES; | |
28454517 | 1440 | |
1441 | /* Create BB for body of the function and connect it properly. */ | |
3a9f48e7 | 1442 | ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = count; |
1443 | ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency = REG_BR_PROB_BASE; | |
1444 | EXIT_BLOCK_PTR_FOR_FN (cfun)->count = count; | |
1445 | EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency = REG_BR_PROB_BASE; | |
4302d619 | 1446 | bb = create_basic_block (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun)); |
3a9f48e7 | 1447 | bb->count = count; |
1448 | bb->frequency = BB_FREQ_MAX; | |
1449 | e = make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU); | |
1450 | e->count = count; | |
1451 | e->probability = REG_BR_PROB_BASE; | |
1452 | e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0); | |
1453 | e->count = count; | |
1454 | e->probability = REG_BR_PROB_BASE; | |
34154e27 | 1455 | add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father); |
28454517 | 1456 | |
1457 | return bb; | |
1458 | } | |
1459 | ||
1460 | /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable | |
1461 | offset indicated by VIRTUAL_OFFSET, if that is | |
1462 | non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and | |
1463 | zero for a result adjusting thunk. */ | |
1464 | ||
1465 | static tree | |
1466 | thunk_adjust (gimple_stmt_iterator * bsi, | |
1467 | tree ptr, bool this_adjusting, | |
1468 | HOST_WIDE_INT fixed_offset, tree virtual_offset) | |
1469 | { | |
1a91d914 | 1470 | gassign *stmt; |
28454517 | 1471 | tree ret; |
1472 | ||
55d6cb23 | 1473 | if (this_adjusting |
1474 | && fixed_offset != 0) | |
28454517 | 1475 | { |
2cc66f2a | 1476 | stmt = gimple_build_assign |
1477 | (ptr, fold_build_pointer_plus_hwi_loc (input_location, | |
1478 | ptr, | |
1479 | fixed_offset)); | |
28454517 | 1480 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); |
1481 | } | |
1482 | ||
1483 | /* If there's a virtual offset, look up that value in the vtable and | |
1484 | adjust the pointer again. */ | |
1485 | if (virtual_offset) | |
1486 | { | |
1487 | tree vtabletmp; | |
1488 | tree vtabletmp2; | |
1489 | tree vtabletmp3; | |
28454517 | 1490 | |
1491 | if (!vtable_entry_type) | |
1492 | { | |
1493 | tree vfunc_type = make_node (FUNCTION_TYPE); | |
1494 | TREE_TYPE (vfunc_type) = integer_type_node; | |
1495 | TYPE_ARG_TYPES (vfunc_type) = NULL_TREE; | |
1496 | layout_type (vfunc_type); | |
1497 | ||
1498 | vtable_entry_type = build_pointer_type (vfunc_type); | |
1499 | } | |
1500 | ||
1501 | vtabletmp = | |
072f7ab1 | 1502 | create_tmp_reg (build_pointer_type |
37ffa8fe | 1503 | (build_pointer_type (vtable_entry_type)), "vptr"); |
28454517 | 1504 | |
1505 | /* The vptr is always at offset zero in the object. */ | |
1506 | stmt = gimple_build_assign (vtabletmp, | |
1507 | build1 (NOP_EXPR, TREE_TYPE (vtabletmp), | |
1508 | ptr)); | |
1509 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); | |
28454517 | 1510 | |
1511 | /* Form the vtable address. */ | |
072f7ab1 | 1512 | vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)), |
37ffa8fe | 1513 | "vtableaddr"); |
28454517 | 1514 | stmt = gimple_build_assign (vtabletmp2, |
182cf5a9 | 1515 | build_simple_mem_ref (vtabletmp)); |
28454517 | 1516 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); |
28454517 | 1517 | |
1518 | /* Find the entry with the vcall offset. */ | |
1519 | stmt = gimple_build_assign (vtabletmp2, | |
2cc66f2a | 1520 | fold_build_pointer_plus_loc (input_location, |
1521 | vtabletmp2, | |
1522 | virtual_offset)); | |
28454517 | 1523 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); |
1524 | ||
1525 | /* Get the offset itself. */ | |
072f7ab1 | 1526 | vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)), |
37ffa8fe | 1527 | "vcalloffset"); |
28454517 | 1528 | stmt = gimple_build_assign (vtabletmp3, |
182cf5a9 | 1529 | build_simple_mem_ref (vtabletmp2)); |
28454517 | 1530 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); |
28454517 | 1531 | |
28454517 | 1532 | /* Adjust the `this' pointer. */ |
a0553bff | 1533 | ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3); |
1534 | ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false, | |
1535 | GSI_CONTINUE_LINKING); | |
28454517 | 1536 | } |
1537 | ||
55d6cb23 | 1538 | if (!this_adjusting |
1539 | && fixed_offset != 0) | |
28454517 | 1540 | /* Adjust the pointer by the constant. */ |
1541 | { | |
1542 | tree ptrtmp; | |
1543 | ||
1544 | if (TREE_CODE (ptr) == VAR_DECL) | |
1545 | ptrtmp = ptr; | |
1546 | else | |
1547 | { | |
072f7ab1 | 1548 | ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr"); |
28454517 | 1549 | stmt = gimple_build_assign (ptrtmp, ptr); |
1550 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); | |
28454517 | 1551 | } |
2cc66f2a | 1552 | ptr = fold_build_pointer_plus_hwi_loc (input_location, |
1553 | ptrtmp, fixed_offset); | |
28454517 | 1554 | } |
1555 | ||
1556 | /* Emit the statement and gimplify the adjustment expression. */ | |
072f7ab1 | 1557 | ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this"); |
28454517 | 1558 | stmt = gimple_build_assign (ret, ptr); |
28454517 | 1559 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); |
1560 | ||
1561 | return ret; | |
1562 | } | |
1563 | ||
6e1aa353 | 1564 | /* Expand thunk NODE to gimple if possible. |
46e5ad0c | 1565 | When FORCE_GIMPLE_THUNK is true, gimple thunk is created and |
1566 | no assembler is produced. | |
6e1aa353 | 1567 | When OUTPUT_ASM_THUNK is true, also produce assembler for |
1568 | thunks that are not lowered. */ | |
28454517 | 1569 | |
6e1aa353 | 1570 | bool |
415d1b9a | 1571 | cgraph_node::expand_thunk (bool output_asm_thunks, bool force_gimple_thunk) |
28454517 | 1572 | { |
415d1b9a | 1573 | bool this_adjusting = thunk.this_adjusting; |
1574 | HOST_WIDE_INT fixed_offset = thunk.fixed_offset; | |
1575 | HOST_WIDE_INT virtual_value = thunk.virtual_value; | |
28454517 | 1576 | tree virtual_offset = NULL; |
415d1b9a | 1577 | tree alias = callees->callee->decl; |
1578 | tree thunk_fndecl = decl; | |
bb251465 | 1579 | tree a; |
1580 | ||
e4436fff | 1581 | /* Instrumentation thunk is the same function with |
1582 | a different signature. Never need to expand it. */ | |
1583 | if (thunk.add_pointer_bounds_args) | |
1584 | return false; | |
aed6e608 | 1585 | |
46e5ad0c | 1586 | if (!force_gimple_thunk && this_adjusting |
28454517 | 1587 | && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset, |
1588 | virtual_value, alias)) | |
1589 | { | |
1590 | const char *fnname; | |
1591 | tree fn_block; | |
28b2c6a7 | 1592 | tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl)); |
6e1aa353 | 1593 | |
1594 | if (!output_asm_thunks) | |
ae949f2f | 1595 | { |
1596 | analyzed = true; | |
1597 | return false; | |
1598 | } | |
6e1aa353 | 1599 | |
1600 | if (in_lto_p) | |
4560777c | 1601 | get_untransformed_body (); |
6e1aa353 | 1602 | a = DECL_ARGUMENTS (thunk_fndecl); |
28454517 | 1603 | |
6e1aa353 | 1604 | current_function_decl = thunk_fndecl; |
1605 | ||
1606 | /* Ensure thunks are emitted in their correct sections. */ | |
07f32730 | 1607 | resolve_unique_section (thunk_fndecl, 0, |
1608 | flag_function_sections); | |
6e1aa353 | 1609 | |
28454517 | 1610 | DECL_RESULT (thunk_fndecl) |
1611 | = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl), | |
28b2c6a7 | 1612 | RESULT_DECL, 0, restype); |
6e1aa353 | 1613 | DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl; |
22ea3b47 | 1614 | fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl)); |
28454517 | 1615 | |
1616 | /* The back end expects DECL_INITIAL to contain a BLOCK, so we | |
1617 | create one. */ | |
1618 | fn_block = make_node (BLOCK); | |
1619 | BLOCK_VARS (fn_block) = a; | |
1620 | DECL_INITIAL (thunk_fndecl) = fn_block; | |
bf3a27b8 | 1621 | allocate_struct_function (thunk_fndecl, false); |
28454517 | 1622 | init_function_start (thunk_fndecl); |
1623 | cfun->is_thunk = 1; | |
32c7c682 | 1624 | insn_locations_init (); |
1625 | set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl)); | |
1626 | prologue_location = curr_insn_location (); | |
28454517 | 1627 | assemble_start_function (thunk_fndecl, fnname); |
1628 | ||
1629 | targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl, | |
1630 | fixed_offset, virtual_value, alias); | |
1631 | ||
1632 | assemble_end_function (thunk_fndecl, fnname); | |
32c7c682 | 1633 | insn_locations_finalize (); |
28454517 | 1634 | init_insn_lengths (); |
1635 | free_after_compilation (cfun); | |
28454517 | 1636 | TREE_ASM_WRITTEN (thunk_fndecl) = 1; |
415d1b9a | 1637 | thunk.thunk_p = false; |
1638 | analyzed = false; | |
28454517 | 1639 | } |
17bbda62 | 1640 | else if (stdarg_p (TREE_TYPE (thunk_fndecl))) |
1641 | { | |
1642 | error ("generic thunk code fails for method %qD which uses %<...%>", | |
1643 | thunk_fndecl); | |
1644 | TREE_ASM_WRITTEN (thunk_fndecl) = 1; | |
1645 | analyzed = true; | |
1646 | return false; | |
1647 | } | |
28454517 | 1648 | else |
1649 | { | |
1650 | tree restype; | |
1651 | basic_block bb, then_bb, else_bb, return_bb; | |
1652 | gimple_stmt_iterator bsi; | |
1653 | int nargs = 0; | |
1654 | tree arg; | |
1655 | int i; | |
1656 | tree resdecl; | |
1657 | tree restmp = NULL; | |
17c596cc | 1658 | tree resbnd = NULL; |
28454517 | 1659 | |
1a91d914 | 1660 | gcall *call; |
1661 | greturn *ret; | |
e9a9dc09 | 1662 | bool alias_is_noreturn = TREE_THIS_VOLATILE (alias); |
28454517 | 1663 | |
6e1aa353 | 1664 | if (in_lto_p) |
4560777c | 1665 | get_untransformed_body (); |
6e1aa353 | 1666 | a = DECL_ARGUMENTS (thunk_fndecl); |
52200d03 | 1667 | |
6e1aa353 | 1668 | current_function_decl = thunk_fndecl; |
1669 | ||
1670 | /* Ensure thunks are emitted in their correct sections. */ | |
07f32730 | 1671 | resolve_unique_section (thunk_fndecl, 0, |
1672 | flag_function_sections); | |
6e1aa353 | 1673 | |
28454517 | 1674 | DECL_IGNORED_P (thunk_fndecl) = 1; |
1675 | bitmap_obstack_initialize (NULL); | |
1676 | ||
415d1b9a | 1677 | if (thunk.virtual_offset_p) |
28454517 | 1678 | virtual_offset = size_int (virtual_value); |
1679 | ||
1680 | /* Build the return declaration for the function. */ | |
1681 | restype = TREE_TYPE (TREE_TYPE (thunk_fndecl)); | |
1682 | if (DECL_RESULT (thunk_fndecl) == NULL_TREE) | |
1683 | { | |
1684 | resdecl = build_decl (input_location, RESULT_DECL, 0, restype); | |
1685 | DECL_ARTIFICIAL (resdecl) = 1; | |
1686 | DECL_IGNORED_P (resdecl) = 1; | |
1687 | DECL_RESULT (thunk_fndecl) = resdecl; | |
6e1aa353 | 1688 | DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl; |
28454517 | 1689 | } |
1690 | else | |
1691 | resdecl = DECL_RESULT (thunk_fndecl); | |
1692 | ||
3a9f48e7 | 1693 | bb = then_bb = else_bb = return_bb |
1694 | = init_lowered_empty_function (thunk_fndecl, true, count); | |
28454517 | 1695 | |
1696 | bsi = gsi_start_bb (bb); | |
1697 | ||
1698 | /* Build call to the function being thunked. */ | |
e9a9dc09 | 1699 | if (!VOID_TYPE_P (restype) && !alias_is_noreturn) |
28454517 | 1700 | { |
f2c6b33f | 1701 | if (DECL_BY_REFERENCE (resdecl)) |
fd43cc59 | 1702 | { |
1703 | restmp = gimple_fold_indirect_ref (resdecl); | |
1704 | if (!restmp) | |
1705 | restmp = build2 (MEM_REF, | |
1706 | TREE_TYPE (TREE_TYPE (DECL_RESULT (alias))), | |
1707 | resdecl, | |
1708 | build_int_cst (TREE_TYPE | |
1709 | (DECL_RESULT (alias)), 0)); | |
1710 | } | |
f2c6b33f | 1711 | else if (!is_gimple_reg_type (restype)) |
28454517 | 1712 | { |
91df30af | 1713 | if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl))) |
1714 | { | |
1715 | restmp = resdecl; | |
bdb8985a | 1716 | |
91df30af | 1717 | if (TREE_CODE (restmp) == VAR_DECL) |
1718 | add_local_decl (cfun, restmp); | |
1719 | BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp; | |
1720 | } | |
1721 | else | |
1722 | restmp = create_tmp_var (restype, "retval"); | |
28454517 | 1723 | } |
1724 | else | |
072f7ab1 | 1725 | restmp = create_tmp_reg (restype, "retval"); |
28454517 | 1726 | } |
1727 | ||
1767a056 | 1728 | for (arg = a; arg; arg = DECL_CHAIN (arg)) |
28454517 | 1729 | nargs++; |
c2078b80 | 1730 | auto_vec<tree> vargs (nargs); |
8c2b231e | 1731 | i = 0; |
1732 | arg = a; | |
28454517 | 1733 | if (this_adjusting) |
8c2b231e | 1734 | { |
1735 | vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset, | |
1736 | virtual_offset)); | |
1737 | arg = DECL_CHAIN (a); | |
1738 | i = 1; | |
1739 | } | |
f2c6b33f | 1740 | |
1741 | if (nargs) | |
8c2b231e | 1742 | for (; i < nargs; i++, arg = DECL_CHAIN (arg)) |
08a057fa | 1743 | { |
1744 | tree tmp = arg; | |
1745 | if (!is_gimple_val (arg)) | |
1746 | { | |
1747 | tmp = create_tmp_reg (TYPE_MAIN_VARIANT | |
1748 | (TREE_TYPE (arg)), "arg"); | |
42acab1c | 1749 | gimple *stmt = gimple_build_assign (tmp, arg); |
08a057fa | 1750 | gsi_insert_after (&bsi, stmt, GSI_NEW_STMT); |
1751 | } | |
1752 | vargs.quick_push (tmp); | |
1753 | } | |
28454517 | 1754 | call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs); |
415d1b9a | 1755 | callees->call_stmt = call; |
28454517 | 1756 | gimple_call_set_from_thunk (call, true); |
058a1b7a | 1757 | gimple_call_set_with_bounds (call, instrumentation_clone); |
86bbd552 | 1758 | |
1759 | /* Return slot optimization is always possible and in fact requred to | |
1760 | return values with DECL_BY_REFERENCE. */ | |
1761 | if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl)) | |
1762 | && (!is_gimple_reg_type (TREE_TYPE (resdecl)) | |
1763 | || DECL_BY_REFERENCE (resdecl))) | |
1764 | gimple_call_set_return_slot_opt (call, true); | |
1765 | ||
e9a9dc09 | 1766 | if (restmp && !alias_is_noreturn) |
f2c6b33f | 1767 | { |
1768 | gimple_call_set_lhs (call, restmp); | |
1769 | gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp), | |
1770 | TREE_TYPE (TREE_TYPE (alias)))); | |
1771 | } | |
28454517 | 1772 | gsi_insert_after (&bsi, call, GSI_NEW_STMT); |
e9a9dc09 | 1773 | if (!alias_is_noreturn) |
f2c6b33f | 1774 | { |
17c596cc | 1775 | if (instrumentation_clone |
1776 | && !DECL_BY_REFERENCE (resdecl) | |
1777 | && restmp | |
1778 | && BOUNDED_P (restmp)) | |
1779 | { | |
1780 | resbnd = chkp_insert_retbnd_call (NULL, restmp, &bsi); | |
1781 | create_edge (get_create (gimple_call_fndecl (gsi_stmt (bsi))), | |
1782 | as_a <gcall *> (gsi_stmt (bsi)), | |
1783 | callees->count, callees->frequency); | |
1784 | } | |
1785 | ||
f2c6b33f | 1786 | if (restmp && !this_adjusting |
1787 | && (fixed_offset || virtual_offset)) | |
1788 | { | |
1789 | tree true_label = NULL_TREE; | |
28454517 | 1790 | |
f2c6b33f | 1791 | if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE) |
1792 | { | |
42acab1c | 1793 | gimple *stmt; |
3a9f48e7 | 1794 | edge e; |
f2c6b33f | 1795 | /* If the return type is a pointer, we need to |
1796 | protect against NULL. We know there will be an | |
1797 | adjustment, because that's why we're emitting a | |
1798 | thunk. */ | |
4302d619 | 1799 | then_bb = create_basic_block (NULL, bb); |
3a9f48e7 | 1800 | then_bb->count = count - count / 16; |
1801 | then_bb->frequency = BB_FREQ_MAX - BB_FREQ_MAX / 16; | |
4302d619 | 1802 | return_bb = create_basic_block (NULL, then_bb); |
3a9f48e7 | 1803 | return_bb->count = count; |
1804 | return_bb->frequency = BB_FREQ_MAX; | |
4302d619 | 1805 | else_bb = create_basic_block (NULL, else_bb); |
3a9f48e7 | 1806 | then_bb->count = count / 16; |
1807 | then_bb->frequency = BB_FREQ_MAX / 16; | |
f2c6b33f | 1808 | add_bb_to_loop (then_bb, bb->loop_father); |
1809 | add_bb_to_loop (return_bb, bb->loop_father); | |
1810 | add_bb_to_loop (else_bb, bb->loop_father); | |
1811 | remove_edge (single_succ_edge (bb)); | |
1812 | true_label = gimple_block_label (then_bb); | |
1813 | stmt = gimple_build_cond (NE_EXPR, restmp, | |
1814 | build_zero_cst (TREE_TYPE (restmp)), | |
1815 | NULL_TREE, NULL_TREE); | |
1816 | gsi_insert_after (&bsi, stmt, GSI_NEW_STMT); | |
3a9f48e7 | 1817 | e = make_edge (bb, then_bb, EDGE_TRUE_VALUE); |
1818 | e->probability = REG_BR_PROB_BASE - REG_BR_PROB_BASE / 16; | |
1819 | e->count = count - count / 16; | |
1820 | e = make_edge (bb, else_bb, EDGE_FALSE_VALUE); | |
1821 | e->probability = REG_BR_PROB_BASE / 16; | |
1822 | e->count = count / 16; | |
1823 | e = make_edge (return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0); | |
1824 | e->probability = REG_BR_PROB_BASE; | |
1825 | e->count = count; | |
1826 | e = make_edge (then_bb, return_bb, EDGE_FALLTHRU); | |
1827 | e->probability = REG_BR_PROB_BASE; | |
1828 | e->count = count - count / 16; | |
1829 | e = make_edge (else_bb, return_bb, EDGE_FALLTHRU); | |
1830 | e->probability = REG_BR_PROB_BASE; | |
1831 | e->count = count / 16; | |
f2c6b33f | 1832 | bsi = gsi_last_bb (then_bb); |
1833 | } | |
28454517 | 1834 | |
f2c6b33f | 1835 | restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0, |
1836 | fixed_offset, virtual_offset); | |
1837 | if (true_label) | |
1838 | { | |
42acab1c | 1839 | gimple *stmt; |
f2c6b33f | 1840 | bsi = gsi_last_bb (else_bb); |
1841 | stmt = gimple_build_assign (restmp, | |
1842 | build_zero_cst (TREE_TYPE (restmp))); | |
1843 | gsi_insert_after (&bsi, stmt, GSI_NEW_STMT); | |
1844 | bsi = gsi_last_bb (return_bb); | |
1845 | } | |
28454517 | 1846 | } |
f2c6b33f | 1847 | else |
1848 | gimple_call_set_tail (call, true); | |
28454517 | 1849 | |
f2c6b33f | 1850 | /* Build return value. */ |
fd43cc59 | 1851 | if (!DECL_BY_REFERENCE (resdecl)) |
1852 | ret = gimple_build_return (restmp); | |
1853 | else | |
1854 | ret = gimple_build_return (resdecl); | |
17c596cc | 1855 | gimple_return_set_retbnd (ret, resbnd); |
fd43cc59 | 1856 | |
f2c6b33f | 1857 | gsi_insert_after (&bsi, ret, GSI_NEW_STMT); |
28454517 | 1858 | } |
1859 | else | |
f2c6b33f | 1860 | { |
1861 | gimple_call_set_tail (call, true); | |
1862 | remove_edge (single_succ_edge (bb)); | |
1863 | } | |
28454517 | 1864 | |
6e1aa353 | 1865 | cfun->gimple_df->in_ssa_p = true; |
3a9f48e7 | 1866 | profile_status_for_fn (cfun) |
1867 | = count ? PROFILE_READ : PROFILE_GUESSED; | |
6e1aa353 | 1868 | /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */ |
1869 | TREE_ASM_WRITTEN (thunk_fndecl) = false; | |
28454517 | 1870 | delete_unreachable_blocks (); |
1871 | update_ssa (TODO_update_ssa); | |
382ecba7 | 1872 | checking_verify_flow_info (); |
ba4dc75f | 1873 | free_dominance_info (CDI_DOMINATORS); |
28454517 | 1874 | |
28454517 | 1875 | /* Since we want to emit the thunk, we explicitly mark its name as |
1876 | referenced. */ | |
415d1b9a | 1877 | thunk.thunk_p = false; |
1878 | lowered = true; | |
28454517 | 1879 | bitmap_obstack_release (NULL); |
1880 | } | |
1881 | current_function_decl = NULL; | |
9078126c | 1882 | set_cfun (NULL); |
6e1aa353 | 1883 | return true; |
28454517 | 1884 | } |
1885 | ||
35ee1c66 | 1886 | /* Assemble thunks and aliases associated to node. */ |
91bf9d9a | 1887 | |
35ee1c66 | 1888 | void |
1889 | cgraph_node::assemble_thunks_and_aliases (void) | |
91bf9d9a | 1890 | { |
35ee1c66 | 1891 | cgraph_edge *e; |
1892 | ipa_ref *ref; | |
c70f46b0 | 1893 | |
35ee1c66 | 1894 | for (e = callers; e;) |
058a1b7a | 1895 | if (e->caller->thunk.thunk_p |
1896 | && !e->caller->thunk.add_pointer_bounds_args) | |
91bf9d9a | 1897 | { |
35ee1c66 | 1898 | cgraph_node *thunk = e->caller; |
91bf9d9a | 1899 | |
1900 | e = e->next_caller; | |
415d1b9a | 1901 | thunk->expand_thunk (true, false); |
35ee1c66 | 1902 | thunk->assemble_thunks_and_aliases (); |
91bf9d9a | 1903 | } |
1904 | else | |
1905 | e = e->next_caller; | |
e4a2b488 | 1906 | |
35ee1c66 | 1907 | FOR_EACH_ALIAS (this, ref) |
e4a2b488 | 1908 | { |
35ee1c66 | 1909 | cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring); |
1910 | bool saved_written = TREE_ASM_WRITTEN (decl); | |
e4a2b488 | 1911 | |
1912 | /* Force assemble_alias to really output the alias this time instead | |
1913 | of buffering it in same alias pairs. */ | |
35ee1c66 | 1914 | TREE_ASM_WRITTEN (decl) = 1; |
e4a2b488 | 1915 | do_assemble_alias (alias->decl, |
35ee1c66 | 1916 | DECL_ASSEMBLER_NAME (decl)); |
1917 | alias->assemble_thunks_and_aliases (); | |
1918 | TREE_ASM_WRITTEN (decl) = saved_written; | |
e4a2b488 | 1919 | } |
91bf9d9a | 1920 | } |
1921 | ||
35ee1c66 | 1922 | /* Expand function specified by node. */ |
941366fd | 1923 | |
35ee1c66 | 1924 | void |
1925 | cgraph_node::expand (void) | |
941366fd | 1926 | { |
941366fd | 1927 | location_t saved_loc; |
1928 | ||
da5e1e7c | 1929 | /* We ought to not compile any inline clones. */ |
35ee1c66 | 1930 | gcc_assert (!global.inlined_to); |
da5e1e7c | 1931 | |
1932 | announce_function (decl); | |
35ee1c66 | 1933 | process = 0; |
1934 | gcc_assert (lowered); | |
4560777c | 1935 | get_untransformed_body (); |
da5e1e7c | 1936 | |
1937 | /* Generate RTL for the body of DECL. */ | |
1938 | ||
941366fd | 1939 | timevar_push (TV_REST_OF_COMPILATION); |
1940 | ||
35ee1c66 | 1941 | gcc_assert (symtab->global_info_ready); |
941366fd | 1942 | |
1943 | /* Initialize the default bitmap obstack. */ | |
1944 | bitmap_obstack_initialize (NULL); | |
1945 | ||
1946 | /* Initialize the RTL code for the function. */ | |
941366fd | 1947 | saved_loc = input_location; |
da5e1e7c | 1948 | input_location = DECL_SOURCE_LOCATION (decl); |
bf3a27b8 | 1949 | |
1950 | gcc_assert (DECL_STRUCT_FUNCTION (decl)); | |
1951 | push_cfun (DECL_STRUCT_FUNCTION (decl)); | |
da5e1e7c | 1952 | init_function_start (decl); |
941366fd | 1953 | |
1954 | gimple_register_cfg_hooks (); | |
1955 | ||
1956 | bitmap_obstack_initialize (®_obstack); /* FIXME, only at RTL generation*/ | |
1957 | ||
1958 | execute_all_ipa_transforms (); | |
1959 | ||
1960 | /* Perform all tree transforms and optimizations. */ | |
1961 | ||
1962 | /* Signal the start of passes. */ | |
1963 | invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL); | |
1964 | ||
3538ae0d | 1965 | execute_pass_list (cfun, g->get_passes ()->all_passes); |
941366fd | 1966 | |
1967 | /* Signal the end of passes. */ | |
1968 | invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL); | |
1969 | ||
1970 | bitmap_obstack_release (®_obstack); | |
1971 | ||
1972 | /* Release the default bitmap obstack. */ | |
1973 | bitmap_obstack_release (NULL); | |
1974 | ||
941366fd | 1975 | /* If requested, warn about function definitions where the function will |
1976 | return a value (usually of some struct or union type) which itself will | |
1977 | take up a lot of stack space. */ | |
da5e1e7c | 1978 | if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl)) |
941366fd | 1979 | { |
da5e1e7c | 1980 | tree ret_type = TREE_TYPE (TREE_TYPE (decl)); |
941366fd | 1981 | |
1982 | if (ret_type && TYPE_SIZE_UNIT (ret_type) | |
1983 | && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST | |
1984 | && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type), | |
1985 | larger_than_size)) | |
1986 | { | |
1987 | unsigned int size_as_int | |
1988 | = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type)); | |
1989 | ||
1990 | if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0) | |
1991 | warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes", | |
da5e1e7c | 1992 | decl, size_as_int); |
941366fd | 1993 | else |
1994 | warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes", | |
da5e1e7c | 1995 | decl, larger_than_size); |
941366fd | 1996 | } |
1997 | } | |
1998 | ||
da5e1e7c | 1999 | gimple_set_body (decl, NULL); |
2000 | if (DECL_STRUCT_FUNCTION (decl) == 0 | |
415d1b9a | 2001 | && !cgraph_node::get (decl)->origin) |
941366fd | 2002 | { |
2003 | /* Stop pointing to the local nodes about to be freed. | |
2004 | But DECL_INITIAL must remain nonzero so we know this | |
2005 | was an actual function definition. | |
2006 | For a nested function, this is done in c_pop_function_context. | |
2007 | If rest_of_compilation set this to 0, leave it 0. */ | |
da5e1e7c | 2008 | if (DECL_INITIAL (decl) != 0) |
2009 | DECL_INITIAL (decl) = error_mark_node; | |
941366fd | 2010 | } |
2011 | ||
2012 | input_location = saved_loc; | |
2013 | ||
2014 | ggc_collect (); | |
2015 | timevar_pop (TV_REST_OF_COMPILATION); | |
f7777314 | 2016 | |
2017 | /* Make sure that BE didn't give up on compiling. */ | |
2018 | gcc_assert (TREE_ASM_WRITTEN (decl)); | |
bf3a27b8 | 2019 | if (cfun) |
2020 | pop_cfun (); | |
f76f7453 | 2021 | |
2022 | /* It would make a lot more sense to output thunks before function body to get more | |
cf951b1a | 2023 | forward and lest backwarding jumps. This however would need solving problem |
f76f7453 | 2024 | with comdats. See PR48668. Also aliases must come after function itself to |
cf951b1a | 2025 | make one pass assemblers, like one on AIX, happy. See PR 50689. |
f76f7453 | 2026 | FIXME: Perhaps thunks should be move before function IFF they are not in comdat |
2027 | groups. */ | |
35ee1c66 | 2028 | assemble_thunks_and_aliases (); |
2029 | release_body (); | |
1a1a827a | 2030 | /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer |
2031 | points to the dead function body. */ | |
35ee1c66 | 2032 | remove_callees (); |
2033 | remove_all_references (); | |
ae01b312 | 2034 | } |
2035 | ||
af48f0b1 | 2036 | /* Node comparer that is responsible for the order that corresponds |
2037 | to time when a function was launched for the first time. */ | |
2038 | ||
2039 | static int | |
2040 | node_cmp (const void *pa, const void *pb) | |
2041 | { | |
35ee1c66 | 2042 | const cgraph_node *a = *(const cgraph_node * const *) pa; |
2043 | const cgraph_node *b = *(const cgraph_node * const *) pb; | |
af48f0b1 | 2044 | |
2045 | /* Functions with time profile must be before these without profile. */ | |
2046 | if (!a->tp_first_run || !b->tp_first_run) | |
2047 | return a->tp_first_run - b->tp_first_run; | |
2048 | ||
2049 | return a->tp_first_run != b->tp_first_run | |
2050 | ? b->tp_first_run - a->tp_first_run | |
2051 | : b->order - a->order; | |
2052 | } | |
acc70efa | 2053 | |
d9d9733a | 2054 | /* Expand all functions that must be output. |
2055 | ||
d7c6d889 | 2056 | Attempt to topologically sort the nodes so function is output when |
2057 | all called functions are already assembled to allow data to be | |
91c82c20 | 2058 | propagated across the callgraph. Use a stack to get smaller distance |
3927afe0 | 2059 | between a function and its callees (later we may choose to use a more |
d7c6d889 | 2060 | sophisticated algorithm for function reordering; we will likely want |
2061 | to use subsections to make the output functions appear in top-down | |
2062 | order). */ | |
2063 | ||
2064 | static void | |
cf951b1a | 2065 | expand_all_functions (void) |
d7c6d889 | 2066 | { |
35ee1c66 | 2067 | cgraph_node *node; |
2068 | cgraph_node **order = XCNEWVEC (cgraph_node *, | |
2069 | symtab->cgraph_count); | |
af48f0b1 | 2070 | unsigned int expanded_func_count = 0, profiled_func_count = 0; |
c04e3894 | 2071 | int order_pos, new_order_pos = 0; |
d7c6d889 | 2072 | int i; |
2073 | ||
7771d558 | 2074 | order_pos = ipa_reverse_postorder (order); |
35ee1c66 | 2075 | gcc_assert (order_pos == symtab->cgraph_count); |
d7c6d889 | 2076 | |
7bd28bba | 2077 | /* Garbage collector may remove inline clones we eliminate during |
b0cdf642 | 2078 | optimization. So we must be sure to not reference them. */ |
2079 | for (i = 0; i < order_pos; i++) | |
09fc9532 | 2080 | if (order[i]->process) |
b0cdf642 | 2081 | order[new_order_pos++] = order[i]; |
2082 | ||
af48f0b1 | 2083 | if (flag_profile_reorder_functions) |
35ee1c66 | 2084 | qsort (order, new_order_pos, sizeof (cgraph_node *), node_cmp); |
af48f0b1 | 2085 | |
b0cdf642 | 2086 | for (i = new_order_pos - 1; i >= 0; i--) |
d7c6d889 | 2087 | { |
2088 | node = order[i]; | |
af48f0b1 | 2089 | |
09fc9532 | 2090 | if (node->process) |
d7c6d889 | 2091 | { |
5c6f6a61 | 2092 | expanded_func_count++; |
2093 | if(node->tp_first_run) | |
2094 | profiled_func_count++; | |
2095 | ||
2096 | if (symtab->dump_file) | |
2097 | fprintf (symtab->dump_file, | |
2098 | "Time profile order in expand_all_functions:%s:%d\n", | |
2099 | node->asm_name (), node->tp_first_run); | |
09fc9532 | 2100 | node->process = 0; |
35ee1c66 | 2101 | node->expand (); |
d7c6d889 | 2102 | } |
2103 | } | |
af48f0b1 | 2104 | |
2105 | if (dump_file) | |
2106 | fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n", | |
2107 | main_input_filename, profiled_func_count, expanded_func_count); | |
2108 | ||
35ee1c66 | 2109 | if (symtab->dump_file && flag_profile_reorder_functions) |
2110 | fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n", | |
af48f0b1 | 2111 | profiled_func_count, expanded_func_count); |
2112 | ||
35ee1c66 | 2113 | symtab->process_new_functions (); |
8a4a28a8 | 2114 | free_gimplify_stack (); |
773c5ba7 | 2115 | |
d7c6d889 | 2116 | free (order); |
2117 | } | |
2118 | ||
56af936e | 2119 | /* This is used to sort the node types by the cgraph order number. */ |
2120 | ||
0b09525f | 2121 | enum cgraph_order_sort_kind |
2122 | { | |
2123 | ORDER_UNDEFINED = 0, | |
2124 | ORDER_FUNCTION, | |
2125 | ORDER_VAR, | |
2126 | ORDER_ASM | |
2127 | }; | |
2128 | ||
56af936e | 2129 | struct cgraph_order_sort |
2130 | { | |
0b09525f | 2131 | enum cgraph_order_sort_kind kind; |
56af936e | 2132 | union |
2133 | { | |
35ee1c66 | 2134 | cgraph_node *f; |
098f44bc | 2135 | varpool_node *v; |
35ee1c66 | 2136 | asm_node *a; |
56af936e | 2137 | } u; |
2138 | }; | |
2139 | ||
2140 | /* Output all functions, variables, and asm statements in the order | |
2141 | according to their order fields, which is the order in which they | |
2142 | appeared in the file. This implements -fno-toplevel-reorder. In | |
2143 | this mode we may output functions and variables which don't really | |
6b722052 | 2144 | need to be output. |
2145 | When NO_REORDER is true only do this for symbols marked no reorder. */ | |
56af936e | 2146 | |
2147 | static void | |
6b722052 | 2148 | output_in_order (bool no_reorder) |
56af936e | 2149 | { |
2150 | int max; | |
35ee1c66 | 2151 | cgraph_order_sort *nodes; |
56af936e | 2152 | int i; |
35ee1c66 | 2153 | cgraph_node *pf; |
098f44bc | 2154 | varpool_node *pv; |
35ee1c66 | 2155 | asm_node *pa; |
2156 | max = symtab->order; | |
2157 | nodes = XCNEWVEC (cgraph_order_sort, max); | |
56af936e | 2158 | |
7c455d87 | 2159 | FOR_EACH_DEFINED_FUNCTION (pf) |
56af936e | 2160 | { |
02774f2d | 2161 | if (pf->process && !pf->thunk.thunk_p && !pf->alias) |
56af936e | 2162 | { |
6b722052 | 2163 | if (no_reorder && !pf->no_reorder) |
2164 | continue; | |
02774f2d | 2165 | i = pf->order; |
56af936e | 2166 | gcc_assert (nodes[i].kind == ORDER_UNDEFINED); |
2167 | nodes[i].kind = ORDER_FUNCTION; | |
2168 | nodes[i].u.f = pf; | |
2169 | } | |
2170 | } | |
2171 | ||
7c455d87 | 2172 | FOR_EACH_DEFINED_VARIABLE (pv) |
02774f2d | 2173 | if (!DECL_EXTERNAL (pv->decl)) |
aa419a52 | 2174 | { |
6b722052 | 2175 | if (no_reorder && !pv->no_reorder) |
2176 | continue; | |
02774f2d | 2177 | i = pv->order; |
aa419a52 | 2178 | gcc_assert (nodes[i].kind == ORDER_UNDEFINED); |
2179 | nodes[i].kind = ORDER_VAR; | |
2180 | nodes[i].u.v = pv; | |
2181 | } | |
56af936e | 2182 | |
35ee1c66 | 2183 | for (pa = symtab->first_asm_symbol (); pa; pa = pa->next) |
56af936e | 2184 | { |
2185 | i = pa->order; | |
2186 | gcc_assert (nodes[i].kind == ORDER_UNDEFINED); | |
2187 | nodes[i].kind = ORDER_ASM; | |
2188 | nodes[i].u.a = pa; | |
2189 | } | |
56af936e | 2190 | |
304e5318 | 2191 | /* In toplevel reorder mode we output all statics; mark them as needed. */ |
304e5318 | 2192 | |
91da0f1c | 2193 | for (i = 0; i < max; ++i) |
2194 | if (nodes[i].kind == ORDER_VAR) | |
97221fd7 | 2195 | nodes[i].u.v->finalize_named_section_flags (); |
91da0f1c | 2196 | |
56af936e | 2197 | for (i = 0; i < max; ++i) |
2198 | { | |
2199 | switch (nodes[i].kind) | |
2200 | { | |
2201 | case ORDER_FUNCTION: | |
09fc9532 | 2202 | nodes[i].u.f->process = 0; |
35ee1c66 | 2203 | nodes[i].u.f->expand (); |
56af936e | 2204 | break; |
2205 | ||
2206 | case ORDER_VAR: | |
97221fd7 | 2207 | nodes[i].u.v->assemble_decl (); |
56af936e | 2208 | break; |
2209 | ||
2210 | case ORDER_ASM: | |
2211 | assemble_asm (nodes[i].u.a->asm_str); | |
2212 | break; | |
2213 | ||
2214 | case ORDER_UNDEFINED: | |
2215 | break; | |
2216 | ||
2217 | default: | |
2218 | gcc_unreachable (); | |
2219 | } | |
2220 | } | |
4b4ea2db | 2221 | |
35ee1c66 | 2222 | symtab->clear_asm_symbols (); |
2223 | ||
3e1cde87 | 2224 | free (nodes); |
56af936e | 2225 | } |
2226 | ||
77fce4cd | 2227 | static void |
2228 | ipa_passes (void) | |
2229 | { | |
3ea50c01 | 2230 | gcc::pass_manager *passes = g->get_passes (); |
2231 | ||
87d4aa85 | 2232 | set_cfun (NULL); |
4b14adf9 | 2233 | current_function_decl = NULL; |
75a70cf9 | 2234 | gimple_register_cfg_hooks (); |
77fce4cd | 2235 | bitmap_obstack_initialize (NULL); |
59dd4830 | 2236 | |
c9036234 | 2237 | invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL); |
2238 | ||
59dd4830 | 2239 | if (!in_lto_p) |
7b2e8956 | 2240 | { |
3ea50c01 | 2241 | execute_ipa_pass_list (passes->all_small_ipa_passes); |
7b2e8956 | 2242 | if (seen_error ()) |
2243 | return; | |
2244 | } | |
9ed5b1f5 | 2245 | |
289c4db4 | 2246 | /* This extra symtab_remove_unreachable_nodes pass tends to catch some |
2247 | devirtualization and other changes where removal iterate. */ | |
366970c6 | 2248 | symtab->remove_unreachable_nodes (symtab->dump_file); |
941125aa | 2249 | |
7bfefa9d | 2250 | /* If pass_all_early_optimizations was not scheduled, the state of |
2251 | the cgraph will not be properly updated. Update it now. */ | |
35ee1c66 | 2252 | if (symtab->state < IPA_SSA) |
2253 | symtab->state = IPA_SSA; | |
9ed5b1f5 | 2254 | |
7bfefa9d | 2255 | if (!in_lto_p) |
2256 | { | |
2257 | /* Generate coverage variables and constructors. */ | |
2258 | coverage_finish (); | |
2259 | ||
2260 | /* Process new functions added. */ | |
2261 | set_cfun (NULL); | |
2262 | current_function_decl = NULL; | |
35ee1c66 | 2263 | symtab->process_new_functions (); |
7bfefa9d | 2264 | |
c9036234 | 2265 | execute_ipa_summary_passes |
b23e5d49 | 2266 | ((ipa_opt_pass_d *) passes->all_regular_ipa_passes); |
8867b500 | 2267 | } |
23433d72 | 2268 | |
2269 | /* Some targets need to handle LTO assembler output specially. */ | |
9f28dc4c | 2270 | if (flag_generate_lto || flag_generate_offload) |
23433d72 | 2271 | targetm.asm_out.lto_start (); |
2272 | ||
7bfefa9d | 2273 | if (!in_lto_p) |
b0c5e347 | 2274 | { |
2275 | if (g->have_offload) | |
2276 | { | |
2277 | section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX; | |
ba000093 | 2278 | lto_stream_offload_p = true; |
9d65fe51 | 2279 | ipa_write_summaries (); |
ba000093 | 2280 | lto_stream_offload_p = false; |
b0c5e347 | 2281 | } |
2282 | if (flag_lto) | |
2283 | { | |
2284 | section_name_prefix = LTO_SECTION_NAME_PREFIX; | |
ba000093 | 2285 | lto_stream_offload_p = false; |
9d65fe51 | 2286 | ipa_write_summaries (); |
b0c5e347 | 2287 | } |
2288 | } | |
7bfefa9d | 2289 | |
9f28dc4c | 2290 | if (flag_generate_lto || flag_generate_offload) |
23433d72 | 2291 | targetm.asm_out.lto_end (); |
2292 | ||
b33542ab | 2293 | if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects)) |
3ea50c01 | 2294 | execute_ipa_pass_list (passes->all_regular_ipa_passes); |
c9036234 | 2295 | invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL); |
9ed5b1f5 | 2296 | |
77fce4cd | 2297 | bitmap_obstack_release (NULL); |
2298 | } | |
2299 | ||
badeded8 | 2300 | |
2301 | /* Return string alias is alias of. */ | |
2302 | ||
2303 | static tree | |
2304 | get_alias_symbol (tree decl) | |
2305 | { | |
2306 | tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)); | |
2307 | return get_identifier (TREE_STRING_POINTER | |
2308 | (TREE_VALUE (TREE_VALUE (alias)))); | |
2309 | } | |
2310 | ||
2311 | ||
5e712541 | 2312 | /* Weakrefs may be associated to external decls and thus not output |
9d75589a | 2313 | at expansion time. Emit all necessary aliases. */ |
5e712541 | 2314 | |
35ee1c66 | 2315 | void |
2316 | symbol_table::output_weakrefs (void) | |
5e712541 | 2317 | { |
452659af | 2318 | symtab_node *node; |
058a1b7a | 2319 | cgraph_node *cnode; |
48669653 | 2320 | FOR_EACH_SYMBOL (node) |
02774f2d | 2321 | if (node->alias |
2322 | && !TREE_ASM_WRITTEN (node->decl) | |
058a1b7a | 2323 | && (!(cnode = dyn_cast <cgraph_node *> (node)) |
2324 | || !cnode->instrumented_version | |
2325 | || !TREE_ASM_WRITTEN (cnode->instrumented_version->decl)) | |
02774f2d | 2326 | && node->weakref) |
48669653 | 2327 | { |
2328 | tree target; | |
2329 | ||
2330 | /* Weakrefs are special by not requiring target definition in current | |
2331 | compilation unit. It is thus bit hard to work out what we want to | |
2332 | alias. | |
2333 | When alias target is defined, we need to fetch it from symtab reference, | |
2334 | otherwise it is pointed to by alias_target. */ | |
02774f2d | 2335 | if (node->alias_target) |
2336 | target = (DECL_P (node->alias_target) | |
2337 | ? DECL_ASSEMBLER_NAME (node->alias_target) | |
2338 | : node->alias_target); | |
2339 | else if (node->analyzed) | |
415d1b9a | 2340 | target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl); |
48669653 | 2341 | else |
2342 | { | |
2343 | gcc_unreachable (); | |
02774f2d | 2344 | target = get_alias_symbol (node->decl); |
48669653 | 2345 | } |
02774f2d | 2346 | do_assemble_alias (node->decl, target); |
48669653 | 2347 | } |
5e712541 | 2348 | } |
2349 | ||
d2bb3f9d | 2350 | /* Perform simple optimizations based on callgraph. */ |
2351 | ||
2352 | void | |
35ee1c66 | 2353 | symbol_table::compile (void) |
d2bb3f9d | 2354 | { |
2355 | if (seen_error ()) | |
2356 | return; | |
2357 | ||
382ecba7 | 2358 | symtab_node::checking_verify_symtab_nodes (); |
d2bb3f9d | 2359 | |
d2bb3f9d | 2360 | timevar_push (TV_CGRAPHOPT); |
2361 | if (pre_ipa_mem_report) | |
2362 | { | |
2363 | fprintf (stderr, "Memory consumption before IPA\n"); | |
2364 | dump_memory_report (false); | |
2365 | } | |
2366 | if (!quiet_flag) | |
2367 | fprintf (stderr, "Performing interprocedural optimizations\n"); | |
35ee1c66 | 2368 | state = IPA; |
d2bb3f9d | 2369 | |
b0c5e347 | 2370 | /* Offloading requires LTO infrastructure. */ |
2371 | if (!in_lto_p && g->have_offload) | |
9f28dc4c | 2372 | flag_generate_offload = 1; |
b0c5e347 | 2373 | |
cf951b1a | 2374 | /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */ |
9f28dc4c | 2375 | if (flag_generate_lto || flag_generate_offload) |
cf951b1a | 2376 | lto_streamer_hooks_init (); |
2377 | ||
d2bb3f9d | 2378 | /* Don't run the IPA passes if there was any error or sorry messages. */ |
2379 | if (!seen_error ()) | |
2380 | ipa_passes (); | |
2381 | ||
2382 | /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */ | |
2383 | if (seen_error () | |
2384 | || (!in_lto_p && flag_lto && !flag_fat_lto_objects)) | |
2385 | { | |
2386 | timevar_pop (TV_CGRAPHOPT); | |
2387 | return; | |
2388 | } | |
2389 | ||
35ee1c66 | 2390 | global_info_ready = true; |
2391 | if (dump_file) | |
d2bb3f9d | 2392 | { |
35ee1c66 | 2393 | fprintf (dump_file, "Optimized "); |
2394 | symtab_node:: dump_table (dump_file); | |
d2bb3f9d | 2395 | } |
2396 | if (post_ipa_mem_report) | |
2397 | { | |
2398 | fprintf (stderr, "Memory consumption after IPA\n"); | |
2399 | dump_memory_report (false); | |
2400 | } | |
2401 | timevar_pop (TV_CGRAPHOPT); | |
2402 | ||
2403 | /* Output everything. */ | |
2404 | (*debug_hooks->assembly_start) (); | |
2405 | if (!quiet_flag) | |
2406 | fprintf (stderr, "Assembling functions:\n"); | |
382ecba7 | 2407 | symtab_node::checking_verify_symtab_nodes (); |
d2bb3f9d | 2408 | |
35ee1c66 | 2409 | materialize_all_clones (); |
d2bb3f9d | 2410 | bitmap_obstack_initialize (NULL); |
3ea50c01 | 2411 | execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes); |
d2bb3f9d | 2412 | bitmap_obstack_release (NULL); |
cf951b1a | 2413 | mark_functions_to_output (); |
d2bb3f9d | 2414 | |
4cd8ae4b | 2415 | /* When weakref support is missing, we autmatically translate all |
2416 | references to NODE to references to its ultimate alias target. | |
2417 | The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and | |
2418 | TREE_CHAIN. | |
2419 | ||
2420 | Set up this mapping before we output any assembler but once we are sure | |
2421 | that all symbol renaming is done. | |
2422 | ||
2423 | FIXME: All this uglyness can go away if we just do renaming at gimple | |
2424 | level by physically rewritting the IL. At the moment we can only redirect | |
2425 | calls, so we need infrastructure for renaming references as well. */ | |
2426 | #ifndef ASM_OUTPUT_WEAKREF | |
452659af | 2427 | symtab_node *node; |
4cd8ae4b | 2428 | |
2429 | FOR_EACH_SYMBOL (node) | |
02774f2d | 2430 | if (node->alias |
2431 | && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl))) | |
4cd8ae4b | 2432 | { |
2433 | IDENTIFIER_TRANSPARENT_ALIAS | |
02774f2d | 2434 | (DECL_ASSEMBLER_NAME (node->decl)) = 1; |
2435 | TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl)) | |
2436 | = (node->alias_target ? node->alias_target | |
448d76c7 | 2437 | : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl)); |
4cd8ae4b | 2438 | } |
2439 | #endif | |
2440 | ||
35ee1c66 | 2441 | state = EXPANSION; |
af48f0b1 | 2442 | |
d2bb3f9d | 2443 | if (!flag_toplevel_reorder) |
6b722052 | 2444 | output_in_order (false); |
d2bb3f9d | 2445 | else |
2446 | { | |
6b722052 | 2447 | /* Output first asm statements and anything ordered. The process |
2448 | flag is cleared for these nodes, so we skip them later. */ | |
2449 | output_in_order (true); | |
cf951b1a | 2450 | expand_all_functions (); |
35ee1c66 | 2451 | output_variables (); |
d2bb3f9d | 2452 | } |
2453 | ||
35ee1c66 | 2454 | process_new_functions (); |
2455 | state = FINISHED; | |
afea39ad | 2456 | output_weakrefs (); |
d2bb3f9d | 2457 | |
35ee1c66 | 2458 | if (dump_file) |
d2bb3f9d | 2459 | { |
35ee1c66 | 2460 | fprintf (dump_file, "\nFinal "); |
2461 | symtab_node::dump_table (dump_file); | |
d2bb3f9d | 2462 | } |
382ecba7 | 2463 | if (!flag_checking) |
2464 | return; | |
415d1b9a | 2465 | symtab_node::verify_symtab_nodes (); |
d2bb3f9d | 2466 | /* Double check that all inline clones are gone and that all |
2467 | function bodies have been released from memory. */ | |
2468 | if (!seen_error ()) | |
2469 | { | |
35ee1c66 | 2470 | cgraph_node *node; |
d2bb3f9d | 2471 | bool error_found = false; |
2472 | ||
7c455d87 | 2473 | FOR_EACH_DEFINED_FUNCTION (node) |
2474 | if (node->global.inlined_to | |
02774f2d | 2475 | || gimple_has_body_p (node->decl)) |
d2bb3f9d | 2476 | { |
2477 | error_found = true; | |
415d1b9a | 2478 | node->debug (); |
d2bb3f9d | 2479 | } |
2480 | if (error_found) | |
2481 | internal_error ("nodes with unreleased memory found"); | |
2482 | } | |
d2bb3f9d | 2483 | } |
2484 | ||
2485 | ||
2486 | /* Analyze the whole compilation unit once it is parsed completely. */ | |
2487 | ||
2488 | void | |
35ee1c66 | 2489 | symbol_table::finalize_compilation_unit (void) |
d2bb3f9d | 2490 | { |
2491 | timevar_push (TV_CGRAPH); | |
2492 | ||
d2bb3f9d | 2493 | /* If we're here there's no current function anymore. Some frontends |
2494 | are lazy in clearing these. */ | |
2495 | current_function_decl = NULL; | |
2496 | set_cfun (NULL); | |
2497 | ||
2498 | /* Do not skip analyzing the functions if there were errors, we | |
2499 | miss diagnostics for following functions otherwise. */ | |
2500 | ||
2501 | /* Emit size functions we didn't inline. */ | |
2502 | finalize_size_functions (); | |
2503 | ||
2504 | /* Mark alias targets necessary and emit diagnostics. */ | |
d2bb3f9d | 2505 | handle_alias_pairs (); |
2506 | ||
2507 | if (!quiet_flag) | |
2508 | { | |
2509 | fprintf (stderr, "\nAnalyzing compilation unit\n"); | |
2510 | fflush (stderr); | |
2511 | } | |
2512 | ||
2513 | if (flag_dump_passes) | |
2514 | dump_passes (); | |
2515 | ||
2516 | /* Gimplify and lower all functions, compute reachability and | |
2517 | remove unreachable nodes. */ | |
3a1c9df2 | 2518 | analyze_functions (/*first_time=*/true); |
d2bb3f9d | 2519 | |
2520 | /* Mark alias targets necessary and emit diagnostics. */ | |
d2bb3f9d | 2521 | handle_alias_pairs (); |
2522 | ||
2523 | /* Gimplify and lower thunks. */ | |
3a1c9df2 | 2524 | analyze_functions (/*first_time=*/false); |
2525 | ||
68f04316 | 2526 | if (!seen_error ()) |
2527 | { | |
2528 | /* Emit early debug for reachable functions, and by consequence, | |
2529 | locally scoped symbols. */ | |
2530 | struct cgraph_node *cnode; | |
2531 | FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (cnode) | |
2532 | (*debug_hooks->early_global_decl) (cnode->decl); | |
2533 | ||
2534 | /* Clean up anything that needs cleaning up after initial debug | |
2535 | generation. */ | |
2536 | (*debug_hooks->early_finish) (); | |
2537 | } | |
2538 | ||
d2bb3f9d | 2539 | /* Finally drive the pass manager. */ |
cf951b1a | 2540 | compile (); |
d2bb3f9d | 2541 | |
2542 | timevar_pop (TV_CGRAPH); | |
2543 | } | |
2544 | ||
415309e2 | 2545 | /* Reset all state within cgraphunit.c so that we can rerun the compiler |
2546 | within the same process. For use by toplev::finalize. */ | |
2547 | ||
2548 | void | |
2549 | cgraphunit_c_finalize (void) | |
2550 | { | |
2551 | gcc_assert (cgraph_new_nodes.length () == 0); | |
2552 | cgraph_new_nodes.truncate (0); | |
2553 | ||
2554 | vtable_entry_type = NULL; | |
2555 | queued_nodes = &symtab_terminator; | |
2556 | ||
2557 | first_analyzed = NULL; | |
2558 | first_analyzed_var = NULL; | |
2559 | } | |
2560 | ||
415d1b9a | 2561 | /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this |
cb8994e9 | 2562 | kind of wrapper method. */ |
2563 | ||
2564 | void | |
35ee1c66 | 2565 | cgraph_node::create_wrapper (cgraph_node *target) |
cb8994e9 | 2566 | { |
275e2756 | 2567 | /* Preserve DECL_RESULT so we get right by reference flag. */ |
2568 | tree decl_result = DECL_RESULT (decl); | |
cb8994e9 | 2569 | |
275e2756 | 2570 | /* Remove the function's body but keep arguments to be reused |
2571 | for thunk. */ | |
2572 | release_body (true); | |
2573 | reset (); | |
cb8994e9 | 2574 | |
ce7711df | 2575 | DECL_UNINLINABLE (decl) = false; |
275e2756 | 2576 | DECL_RESULT (decl) = decl_result; |
2577 | DECL_INITIAL (decl) = NULL; | |
2578 | allocate_struct_function (decl, false); | |
2579 | set_cfun (NULL); | |
cb8994e9 | 2580 | |
275e2756 | 2581 | /* Turn alias into thunk and expand it into GIMPLE representation. */ |
2582 | definition = true; | |
50671005 | 2583 | |
2584 | memset (&thunk, 0, sizeof (cgraph_thunk_info)); | |
275e2756 | 2585 | thunk.thunk_p = true; |
3a9f48e7 | 2586 | create_edge (target, NULL, count, CGRAPH_FREQ_BASE); |
519627d3 | 2587 | callees->can_throw_external = !TREE_NOTHROW (target->decl); |
cb8994e9 | 2588 | |
275e2756 | 2589 | tree arguments = DECL_ARGUMENTS (decl); |
f9516d28 | 2590 | |
275e2756 | 2591 | while (arguments) |
2592 | { | |
2593 | TREE_ADDRESSABLE (arguments) = false; | |
2594 | arguments = TREE_CHAIN (arguments); | |
2595 | } | |
f9516d28 | 2596 | |
275e2756 | 2597 | expand_thunk (false, true); |
cb8994e9 | 2598 | |
275e2756 | 2599 | /* Inline summary set-up. */ |
2600 | analyze (); | |
2601 | inline_analyze_function (this); | |
cb8994e9 | 2602 | } |
d2bb3f9d | 2603 | |
a861fe52 | 2604 | #include "gt-cgraphunit.h" |