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