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