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