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