]>
Commit | Line | Data |
---|---|---|
9c8305f8 | 1 | /* Driver of optimization process |
d1e082c2 | 2 | Copyright (C) 2003-2013 Free Software Foundation, Inc. |
1c4a429a JH |
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 | |
9dcd6f09 | 9 | Software Foundation; either version 3, or (at your option) any later |
1c4a429a JH |
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 | |
9dcd6f09 NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
1c4a429a | 20 | |
9c8305f8 | 21 | /* This module implements main driver of compilation process. |
18c6ada9 JH |
22 | |
23 | The main scope of this file is to act as an interface in between | |
9c8305f8 | 24 | tree based frontends and the backend. |
18c6ada9 JH |
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 | ||
efe75b6f JH |
33 | (There is one exception needed for implementing GCC extern inline |
34 | function.) | |
18c6ada9 | 35 | |
047ae098 | 36 | - varpool_finalize_decl |
18c6ada9 | 37 | |
1ae58c30 | 38 | This function has same behavior as the above but is used for static |
18c6ada9 JH |
39 | variables. |
40 | ||
65d630d4 JH |
41 | - add_asm_node |
42 | ||
43 | Insert new toplevel ASM statement | |
44 | ||
45 | - finalize_compilation_unit | |
18c6ada9 | 46 | |
efe75b6f JH |
47 | This function is called once (source level) compilation unit is finalized |
48 | and it will no longer change. | |
18c6ada9 | 49 | |
9c8305f8 JH |
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. | |
073a8998 | 53 | Those are used to discover other necessary functions and variables. |
9c8305f8 JH |
54 | |
55 | At the end the bodies of unreachable functions are removed. | |
18c6ada9 | 56 | |
efe75b6f | 57 | The function can be called multiple times when multiple source level |
9c8305f8 | 58 | compilation units are combined. |
18c6ada9 | 59 | |
65d630d4 | 60 | - compile |
18c6ada9 | 61 | |
9c8305f8 JH |
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 | |
688010ba | 86 | time. These include, for example, transational memory lowering, |
9c8305f8 JH |
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. | |
18c6ada9 | 111 | |
9c8305f8 | 112 | Compile time and/or parallel linktime stage (ltrans) |
18c6ada9 | 113 | |
9c8305f8 JH |
114 | Each of the object files is streamed back and compiled |
115 | separately. Now the function bodies becomes available | |
116 | again. | |
18c6ada9 | 117 | |
9c8305f8 JH |
118 | 2) Virtual clone materialization |
119 | (cgraph_materialize_clone) | |
18c6ada9 | 120 | |
9c8305f8 JH |
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 | |
18c6ada9 | 127 | |
9c8305f8 JH |
128 | All IP passes transform function bodies based on earlier |
129 | decision of the IP propagation. | |
18c6ada9 | 130 | |
9c8305f8 | 131 | 4) late small IP passes |
18c6ada9 | 132 | |
9c8305f8 | 133 | Simple IP passes working within single program partition. |
18c6ada9 | 134 | |
9c8305f8 | 135 | 5) Expansion |
65d630d4 | 136 | (expand_all_functions) |
18c6ada9 | 137 | |
9c8305f8 JH |
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. | |
18c6ada9 | 143 | |
9c8305f8 JH |
144 | Note that with -fno-toplevel-reorder passes 5 and 6 |
145 | are combined together in cgraph_output_in_order. | |
18c6ada9 | 146 | |
9c8305f8 JH |
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. | |
9b3e897d | 153 | |
9c8305f8 JH |
154 | - cgraph_function_versioning |
155 | ||
156 | produces a copy of function into new one (a version) | |
157 | and apply simple transformations | |
158 | */ | |
6674a6ce | 159 | |
1c4a429a JH |
160 | #include "config.h" |
161 | #include "system.h" | |
162 | #include "coretypes.h" | |
163 | #include "tm.h" | |
164 | #include "tree.h" | |
0878843f | 165 | #include "output.h" |
c9b9aa64 | 166 | #include "rtl.h" |
442b4905 AM |
167 | #include "gimple.h" |
168 | #include "gimple-ssa.h" | |
169 | #include "tree-cfg.h" | |
170 | #include "tree-into-ssa.h" | |
7a300452 | 171 | #include "tree-ssa.h" |
1c4a429a JH |
172 | #include "tree-inline.h" |
173 | #include "langhooks.h" | |
0c58f841 | 174 | #include "pointer-set.h" |
1c4a429a JH |
175 | #include "toplev.h" |
176 | #include "flags.h" | |
177 | #include "ggc.h" | |
178 | #include "debug.h" | |
179 | #include "target.h" | |
dafc5b82 | 180 | #include "diagnostic.h" |
b58b1157 JH |
181 | #include "params.h" |
182 | #include "fibheap.h" | |
dc0bfe6a | 183 | #include "intl.h" |
902edd36 | 184 | #include "function.h" |
57fb5341 | 185 | #include "ipa-prop.h" |
726a989a | 186 | #include "tree-iterator.h" |
b4861090 | 187 | #include "tree-pass.h" |
a406865a | 188 | #include "tree-dump.h" |
9c8305f8 | 189 | #include "gimple-pretty-print.h" |
cd9c7bd2 | 190 | #include "output.h" |
3baf459d | 191 | #include "coverage.h" |
090fa0ab | 192 | #include "plugin.h" |
632b4f8e | 193 | #include "ipa-inline.h" |
af8bca3c | 194 | #include "ipa-utils.h" |
47c79d56 | 195 | #include "lto-streamer.h" |
452aa9c5 | 196 | #include "except.h" |
31ee20ba | 197 | #include "cfgloop.h" |
0878843f | 198 | #include "regset.h" /* FIXME: For reg_obstack. */ |
315f8c0e DM |
199 | #include "context.h" |
200 | #include "pass_manager.h" | |
b58b1157 | 201 | |
66058468 JH |
202 | /* Queue of cgraph nodes scheduled to be added into cgraph. This is a |
203 | secondary queue used during optimization to accommodate passes that | |
204 | may generate new functions that need to be optimized and expanded. */ | |
205 | cgraph_node_set cgraph_new_nodes; | |
206 | ||
65d630d4 JH |
207 | static void expand_all_functions (void); |
208 | static void mark_functions_to_output (void); | |
209 | static void expand_function (struct cgraph_node *); | |
e70670cf | 210 | static void analyze_function (struct cgraph_node *); |
877ab5e9 | 211 | static void handle_alias_pairs (void); |
7dff32e6 | 212 | |
0a5fa5a1 | 213 | FILE *cgraph_dump_file; |
9b3e897d | 214 | |
65d630d4 JH |
215 | /* Linked list of cgraph asm nodes. */ |
216 | struct asm_node *asm_nodes; | |
217 | ||
218 | /* Last node in cgraph_asm_nodes. */ | |
219 | static GTY(()) struct asm_node *asm_last_node; | |
220 | ||
6744a6ab JH |
221 | /* Used for vtable lookup in thunk adjusting. */ |
222 | static GTY (()) tree vtable_entry_type; | |
223 | ||
edb983b2 JH |
224 | /* Determine if symbol DECL is needed. That is, visible to something |
225 | either outside this translation unit, something magic in the system | |
226 | configury */ | |
227 | bool | |
5e20cdc9 | 228 | decide_is_symbol_needed (symtab_node *node) |
8dafba3c | 229 | { |
67348ccc | 230 | tree decl = node->decl; |
386b46cf | 231 | |
ead84f73 JH |
232 | /* Double check that no one output the function into assembly file |
233 | early. */ | |
234 | gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl) | |
40a7fe1e | 235 | || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))); |
e7d6beb0 | 236 | |
67348ccc | 237 | if (!node->definition) |
edb983b2 | 238 | return false; |
a1d31187 | 239 | |
edb983b2 JH |
240 | if (DECL_EXTERNAL (decl)) |
241 | return false; | |
04f77d0f | 242 | |
edb983b2 | 243 | /* If the user told us it is used, then it must be so. */ |
67348ccc | 244 | if (node->force_output) |
edb983b2 JH |
245 | return true; |
246 | ||
247 | /* ABI forced symbols are needed when they are external. */ | |
67348ccc | 248 | if (node->forced_by_abi && TREE_PUBLIC (decl)) |
edb983b2 JH |
249 | return true; |
250 | ||
251 | /* Keep constructors, destructors and virtual functions. */ | |
252 | if (TREE_CODE (decl) == FUNCTION_DECL | |
253 | && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl))) | |
254 | return true; | |
255 | ||
256 | /* Externally visible variables must be output. The exception is | |
257 | COMDAT variables that must be output only when they are needed. */ | |
258 | if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl)) | |
8dafba3c RH |
259 | return true; |
260 | ||
8dafba3c RH |
261 | return false; |
262 | } | |
263 | ||
66058468 JH |
264 | /* Head of the queue of nodes to be processed while building callgraph */ |
265 | ||
5e20cdc9 | 266 | static symtab_node *first = (symtab_node *)(void *)1; |
66058468 JH |
267 | |
268 | /* Add NODE to queue starting at FIRST. | |
269 | The queue is linked via AUX pointers and terminated by pointer to 1. */ | |
270 | ||
271 | static void | |
5e20cdc9 | 272 | enqueue_node (symtab_node *node) |
66058468 | 273 | { |
67348ccc | 274 | if (node->aux) |
66058468 JH |
275 | return; |
276 | gcc_checking_assert (first); | |
67348ccc | 277 | node->aux = first; |
66058468 JH |
278 | first = node; |
279 | } | |
280 | ||
d60ab196 | 281 | /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these |
f45e0ad1 JH |
282 | functions into callgraph in a way so they look like ordinary reachable |
283 | functions inserted into callgraph already at construction time. */ | |
284 | ||
285 | bool | |
286 | cgraph_process_new_functions (void) | |
287 | { | |
288 | bool output = false; | |
289 | tree fndecl; | |
290 | struct cgraph_node *node; | |
66058468 | 291 | cgraph_node_set_iterator csi; |
f45e0ad1 | 292 | |
66058468 JH |
293 | if (!cgraph_new_nodes) |
294 | return false; | |
877ab5e9 | 295 | handle_alias_pairs (); |
f45e0ad1 JH |
296 | /* Note that this queue may grow as its being processed, as the new |
297 | functions may generate new ones. */ | |
66058468 | 298 | for (csi = csi_start (cgraph_new_nodes); !csi_end_p (csi); csi_next (&csi)) |
f45e0ad1 | 299 | { |
66058468 | 300 | node = csi_node (csi); |
67348ccc | 301 | fndecl = node->decl; |
f45e0ad1 JH |
302 | switch (cgraph_state) |
303 | { | |
304 | case CGRAPH_STATE_CONSTRUCTION: | |
305 | /* At construction time we just need to finalize function and move | |
306 | it into reachable functions list. */ | |
307 | ||
f45e0ad1 | 308 | cgraph_finalize_function (fndecl, false); |
f45e0ad1 | 309 | output = true; |
4d5dcfb2 | 310 | cgraph_call_function_insertion_hooks (node); |
67348ccc | 311 | enqueue_node (node); |
f45e0ad1 JH |
312 | break; |
313 | ||
314 | case CGRAPH_STATE_IPA: | |
7a388ee4 | 315 | case CGRAPH_STATE_IPA_SSA: |
f45e0ad1 JH |
316 | /* When IPA optimization already started, do all essential |
317 | transformations that has been already performed on the whole | |
318 | cgraph but not on this function. */ | |
319 | ||
726a989a | 320 | gimple_register_cfg_hooks (); |
67348ccc | 321 | if (!node->analyzed) |
e70670cf | 322 | analyze_function (node); |
f45e0ad1 | 323 | push_cfun (DECL_STRUCT_FUNCTION (fndecl)); |
bc0ec027 | 324 | if (cgraph_state == CGRAPH_STATE_IPA_SSA |
7a388ee4 | 325 | && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl))) |
f7695dbf | 326 | g->get_passes ()->execute_early_local_passes (); |
bc0ec027 | 327 | else if (inline_summary_vec != NULL) |
632b4f8e | 328 | compute_inline_parameters (node, true); |
f45e0ad1 JH |
329 | free_dominance_info (CDI_POST_DOMINATORS); |
330 | free_dominance_info (CDI_DOMINATORS); | |
331 | pop_cfun (); | |
4d5dcfb2 | 332 | cgraph_call_function_insertion_hooks (node); |
f45e0ad1 JH |
333 | break; |
334 | ||
335 | case CGRAPH_STATE_EXPANSION: | |
336 | /* Functions created during expansion shall be compiled | |
337 | directly. */ | |
257eb6e3 | 338 | node->process = 0; |
4d5dcfb2 | 339 | cgraph_call_function_insertion_hooks (node); |
65d630d4 | 340 | expand_function (node); |
f45e0ad1 JH |
341 | break; |
342 | ||
343 | default: | |
344 | gcc_unreachable (); | |
345 | break; | |
346 | } | |
347 | } | |
66058468 JH |
348 | free_cgraph_node_set (cgraph_new_nodes); |
349 | cgraph_new_nodes = NULL; | |
f45e0ad1 JH |
350 | return output; |
351 | } | |
352 | ||
d71cc23f JH |
353 | /* As an GCC extension we allow redefinition of the function. The |
354 | semantics when both copies of bodies differ is not well defined. | |
355 | We replace the old body with new body so in unit at a time mode | |
356 | we always use new body, while in normal mode we may end up with | |
357 | old body inlined into some functions and new body expanded and | |
358 | inlined in others. | |
359 | ||
360 | ??? It may make more sense to use one body for inlining and other | |
361 | body for expanding the function but this is difficult to do. */ | |
362 | ||
e70670cf | 363 | void |
d71cc23f JH |
364 | cgraph_reset_node (struct cgraph_node *node) |
365 | { | |
257eb6e3 | 366 | /* If node->process is set, then we have already begun whole-unit analysis. |
7e8b322a JH |
367 | This is *not* testing for whether we've already emitted the function. |
368 | That case can be sort-of legitimately seen with real function redefinition | |
369 | errors. I would argue that the front end should never present us with | |
370 | such a case, but don't enforce that for now. */ | |
257eb6e3 | 371 | gcc_assert (!node->process); |
d71cc23f JH |
372 | |
373 | /* Reset our data structures so we can analyze the function again. */ | |
374 | memset (&node->local, 0, sizeof (node->local)); | |
375 | memset (&node->global, 0, sizeof (node->global)); | |
376 | memset (&node->rtl, 0, sizeof (node->rtl)); | |
67348ccc DM |
377 | node->analyzed = false; |
378 | node->definition = false; | |
379 | node->alias = false; | |
380 | node->weakref = false; | |
381 | node->cpp_implicit_alias = false; | |
d71cc23f | 382 | |
d71cc23f | 383 | cgraph_node_remove_callees (node); |
67348ccc | 384 | ipa_remove_all_references (&node->ref_list); |
d71cc23f | 385 | } |
d853a20e | 386 | |
838ff415 JH |
387 | /* Return true when there are references to NODE. */ |
388 | ||
389 | static bool | |
5e20cdc9 | 390 | referred_to_p (symtab_node *node) |
838ff415 | 391 | { |
838ff415 JH |
392 | struct ipa_ref *ref; |
393 | ||
073a8998 | 394 | /* See if there are any references at all. */ |
67348ccc | 395 | if (ipa_ref_list_referring_iterate (&node->ref_list, 0, ref)) |
838ff415 | 396 | return true; |
65d630d4 | 397 | /* For functions check also calls. */ |
5d59b5e1 LC |
398 | cgraph_node *cn = dyn_cast <cgraph_node> (node); |
399 | if (cn && cn->callers) | |
838ff415 JH |
400 | return true; |
401 | return false; | |
402 | } | |
403 | ||
6b00c969 | 404 | /* DECL has been parsed. Take it, queue it, compile it at the whim of the |
15682f24 | 405 | logic in effect. If NO_COLLECT is true, then our caller cannot stand to have |
6b00c969 RH |
406 | the garbage collector run at the moment. We would need to either create |
407 | a new GC context, or just not compile right now. */ | |
1c4a429a JH |
408 | |
409 | void | |
15682f24 | 410 | cgraph_finalize_function (tree decl, bool no_collect) |
1c4a429a | 411 | { |
a358e188 | 412 | struct cgraph_node *node = cgraph_get_create_node (decl); |
1c4a429a | 413 | |
67348ccc | 414 | if (node->definition) |
b125ad45 | 415 | { |
15682f24 MJ |
416 | /* Nested functions should only be defined once. */ |
417 | gcc_assert (!DECL_CONTEXT (decl) | |
418 | || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL); | |
b125ad45 JH |
419 | cgraph_reset_node (node); |
420 | node->local.redefined_extern_inline = true; | |
421 | } | |
6b00c969 | 422 | |
d853a20e | 423 | notice_global_symbol (decl); |
67348ccc | 424 | node->definition = true; |
e21aff8a | 425 | node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL; |
1c4a429a | 426 | |
ead84f73 JH |
427 | /* With -fkeep-inline-functions we are keeping all inline functions except |
428 | for extern inline ones. */ | |
429 | if (flag_keep_inline_functions | |
430 | && DECL_DECLARED_INLINE_P (decl) | |
431 | && !DECL_EXTERNAL (decl) | |
432 | && !DECL_DISREGARD_INLINE_LIMITS (decl)) | |
67348ccc | 433 | node->force_output = 1; |
8dafba3c | 434 | |
ead84f73 JH |
435 | /* When not optimizing, also output the static functions. (see |
436 | PR24561), but don't do so for always_inline functions, functions | |
437 | declared inline and nested functions. These were optimized out | |
438 | in the original implementation and it is unclear whether we want | |
439 | to change the behavior here. */ | |
440 | if ((!optimize | |
67348ccc | 441 | && !node->cpp_implicit_alias |
ead84f73 JH |
442 | && !DECL_DISREGARD_INLINE_LIMITS (decl) |
443 | && !DECL_DECLARED_INLINE_P (decl) | |
444 | && !(DECL_CONTEXT (decl) | |
445 | && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)) | |
446 | && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl)) | |
67348ccc | 447 | node->force_output = 1; |
ead84f73 | 448 | |
8dafba3c | 449 | /* If we've not yet emitted decl, tell the debug info about it. */ |
6b00c969 | 450 | if (!TREE_ASM_WRITTEN (decl)) |
8dafba3c | 451 | (*debug_hooks->deferred_inline_function) (decl); |
d173e685 | 452 | |
902edd36 JH |
453 | /* Possibly warn about unused parameters. */ |
454 | if (warn_unused_parameter) | |
455 | do_warn_unused_parameter (decl); | |
7e8b322a | 456 | |
15682f24 | 457 | if (!no_collect) |
7e8b322a | 458 | ggc_collect (); |
838ff415 JH |
459 | |
460 | if (cgraph_state == CGRAPH_STATE_CONSTRUCTION | |
67348ccc DM |
461 | && (decide_is_symbol_needed (node) |
462 | || referred_to_p (node))) | |
463 | enqueue_node (node); | |
1c4a429a JH |
464 | } |
465 | ||
452aa9c5 RG |
466 | /* Add the function FNDECL to the call graph. |
467 | Unlike cgraph_finalize_function, this function is intended to be used | |
468 | by middle end and allows insertion of new function at arbitrary point | |
469 | of compilation. The function can be either in high, low or SSA form | |
470 | GIMPLE. | |
471 | ||
472 | The function is assumed to be reachable and have address taken (so no | |
473 | API breaking optimizations are performed on it). | |
474 | ||
475 | Main work done by this function is to enqueue the function for later | |
476 | processing to avoid need the passes to be re-entrant. */ | |
477 | ||
478 | void | |
479 | cgraph_add_new_function (tree fndecl, bool lowered) | |
480 | { | |
315f8c0e | 481 | gcc::pass_manager *passes = g->get_passes (); |
452aa9c5 RG |
482 | struct cgraph_node *node; |
483 | switch (cgraph_state) | |
484 | { | |
66058468 JH |
485 | case CGRAPH_STATE_PARSING: |
486 | cgraph_finalize_function (fndecl, false); | |
487 | break; | |
452aa9c5 RG |
488 | case CGRAPH_STATE_CONSTRUCTION: |
489 | /* Just enqueue function to be processed at nearest occurrence. */ | |
490 | node = cgraph_create_node (fndecl); | |
452aa9c5 RG |
491 | if (lowered) |
492 | node->lowered = true; | |
66058468 JH |
493 | if (!cgraph_new_nodes) |
494 | cgraph_new_nodes = cgraph_node_set_new (); | |
495 | cgraph_node_set_add (cgraph_new_nodes, node); | |
452aa9c5 RG |
496 | break; |
497 | ||
498 | case CGRAPH_STATE_IPA: | |
499 | case CGRAPH_STATE_IPA_SSA: | |
500 | case CGRAPH_STATE_EXPANSION: | |
501 | /* Bring the function into finalized state and enqueue for later | |
502 | analyzing and compilation. */ | |
503 | node = cgraph_get_create_node (fndecl); | |
504 | node->local.local = false; | |
67348ccc DM |
505 | node->definition = true; |
506 | node->force_output = true; | |
452aa9c5 RG |
507 | if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION) |
508 | { | |
509 | push_cfun (DECL_STRUCT_FUNCTION (fndecl)); | |
452aa9c5 RG |
510 | gimple_register_cfg_hooks (); |
511 | bitmap_obstack_initialize (NULL); | |
315f8c0e | 512 | execute_pass_list (passes->all_lowering_passes); |
f7695dbf | 513 | passes->execute_early_local_passes (); |
452aa9c5 RG |
514 | bitmap_obstack_release (NULL); |
515 | pop_cfun (); | |
452aa9c5 RG |
516 | |
517 | lowered = true; | |
518 | } | |
519 | if (lowered) | |
520 | node->lowered = true; | |
66058468 JH |
521 | if (!cgraph_new_nodes) |
522 | cgraph_new_nodes = cgraph_node_set_new (); | |
523 | cgraph_node_set_add (cgraph_new_nodes, node); | |
452aa9c5 RG |
524 | break; |
525 | ||
526 | case CGRAPH_STATE_FINISHED: | |
527 | /* At the very end of compilation we have to do all the work up | |
528 | to expansion. */ | |
529 | node = cgraph_create_node (fndecl); | |
530 | if (lowered) | |
531 | node->lowered = true; | |
67348ccc | 532 | node->definition = true; |
e70670cf | 533 | analyze_function (node); |
452aa9c5 | 534 | push_cfun (DECL_STRUCT_FUNCTION (fndecl)); |
452aa9c5 RG |
535 | gimple_register_cfg_hooks (); |
536 | bitmap_obstack_initialize (NULL); | |
537 | if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl))) | |
f7695dbf | 538 | g->get_passes ()->execute_early_local_passes (); |
452aa9c5 | 539 | bitmap_obstack_release (NULL); |
452aa9c5 | 540 | pop_cfun (); |
65d630d4 | 541 | expand_function (node); |
452aa9c5 RG |
542 | break; |
543 | ||
544 | default: | |
545 | gcc_unreachable (); | |
546 | } | |
547 | ||
548 | /* Set a personality if required and we already passed EH lowering. */ | |
549 | if (lowered | |
550 | && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl)) | |
551 | == eh_personality_lang)) | |
552 | DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality (); | |
553 | } | |
554 | ||
65d630d4 JH |
555 | /* Add a top-level asm statement to the list. */ |
556 | ||
557 | struct asm_node * | |
558 | add_asm_node (tree asm_str) | |
559 | { | |
560 | struct asm_node *node; | |
561 | ||
562 | node = ggc_alloc_cleared_asm_node (); | |
563 | node->asm_str = asm_str; | |
564 | node->order = symtab_order++; | |
565 | node->next = NULL; | |
566 | if (asm_nodes == NULL) | |
567 | asm_nodes = node; | |
568 | else | |
569 | asm_last_node->next = node; | |
570 | asm_last_node = node; | |
571 | return node; | |
572 | } | |
573 | ||
474eccc6 ILT |
574 | /* Output all asm statements we have stored up to be output. */ |
575 | ||
576 | static void | |
65d630d4 | 577 | output_asm_statements (void) |
474eccc6 | 578 | { |
65d630d4 | 579 | struct asm_node *can; |
474eccc6 | 580 | |
1da2ed5f | 581 | if (seen_error ()) |
474eccc6 ILT |
582 | return; |
583 | ||
65d630d4 | 584 | for (can = asm_nodes; can; can = can->next) |
474eccc6 | 585 | assemble_asm (can->asm_str); |
65d630d4 JH |
586 | asm_nodes = NULL; |
587 | } | |
588 | ||
e767b5be | 589 | /* Analyze the function scheduled to be output. */ |
9c8305f8 | 590 | static void |
e70670cf | 591 | analyze_function (struct cgraph_node *node) |
e767b5be | 592 | { |
67348ccc | 593 | tree decl = node->decl; |
9c8305f8 JH |
594 | location_t saved_loc = input_location; |
595 | input_location = DECL_SOURCE_LOCATION (decl); | |
e767b5be | 596 | |
afdec9bd | 597 | if (node->thunk.thunk_p) |
c47d0034 JH |
598 | { |
599 | cgraph_create_edge (node, cgraph_get_node (node->thunk.alias), | |
afdec9bd JH |
600 | NULL, 0, CGRAPH_FREQ_BASE); |
601 | if (!expand_thunk (node, false)) | |
602 | { | |
603 | node->thunk.alias = NULL; | |
67348ccc | 604 | node->analyzed = true; |
afdec9bd JH |
605 | return; |
606 | } | |
40a7fe1e | 607 | node->thunk.alias = NULL; |
c47d0034 | 608 | } |
67348ccc | 609 | if (node->alias) |
afdec9bd | 610 | symtab_resolve_alias |
67348ccc | 611 | (node, cgraph_get_node (node->alias_target)); |
3649b9b7 ST |
612 | else if (node->dispatcher_function) |
613 | { | |
614 | /* Generate the dispatcher body of multi-versioned functions. */ | |
615 | struct cgraph_function_version_info *dispatcher_version_info | |
616 | = get_cgraph_node_version (node); | |
617 | if (dispatcher_version_info != NULL | |
618 | && (dispatcher_version_info->dispatcher_resolver | |
619 | == NULL_TREE)) | |
620 | { | |
621 | tree resolver = NULL_TREE; | |
622 | gcc_assert (targetm.generate_version_dispatcher_body); | |
623 | resolver = targetm.generate_version_dispatcher_body (node); | |
624 | gcc_assert (resolver != NULL_TREE); | |
625 | } | |
626 | } | |
c47d0034 JH |
627 | else |
628 | { | |
c47d0034 | 629 | push_cfun (DECL_STRUCT_FUNCTION (decl)); |
a406865a | 630 | |
67348ccc | 631 | assign_assembler_name_if_neeeded (node->decl); |
0e0a1359 | 632 | |
c47d0034 JH |
633 | /* Make sure to gimplify bodies only once. During analyzing a |
634 | function we lower it, which will require gimplified nested | |
635 | functions, so we can end up here with an already gimplified | |
636 | body. */ | |
355a7673 | 637 | if (!gimple_has_body_p (decl)) |
c47d0034 JH |
638 | gimplify_function_tree (decl); |
639 | dump_function (TDI_generic, decl); | |
a406865a | 640 | |
26eb69c6 RG |
641 | /* Lower the function. */ |
642 | if (!node->lowered) | |
643 | { | |
644 | if (node->nested) | |
67348ccc | 645 | lower_nested_functions (node->decl); |
26eb69c6 RG |
646 | gcc_assert (!node->nested); |
647 | ||
648 | gimple_register_cfg_hooks (); | |
649 | bitmap_obstack_initialize (NULL); | |
315f8c0e | 650 | execute_pass_list (g->get_passes ()->all_lowering_passes); |
26eb69c6 RG |
651 | free_dominance_info (CDI_POST_DOMINATORS); |
652 | free_dominance_info (CDI_DOMINATORS); | |
653 | compact_blocks (); | |
654 | bitmap_obstack_release (NULL); | |
655 | node->lowered = true; | |
656 | } | |
657 | ||
c47d0034 JH |
658 | pop_cfun (); |
659 | } | |
67348ccc | 660 | node->analyzed = true; |
e767b5be | 661 | |
9c8305f8 | 662 | input_location = saved_loc; |
e767b5be JH |
663 | } |
664 | ||
39e2db00 JH |
665 | /* C++ frontend produce same body aliases all over the place, even before PCH |
666 | gets streamed out. It relies on us linking the aliases with their function | |
667 | in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we | |
668 | first produce aliases without links, but once C++ FE is sure he won't sream | |
669 | PCH we build the links via this function. */ | |
670 | ||
671 | void | |
672 | cgraph_process_same_body_aliases (void) | |
673 | { | |
5e20cdc9 | 674 | symtab_node *node; |
40a7fe1e | 675 | FOR_EACH_SYMBOL (node) |
67348ccc | 676 | if (node->cpp_implicit_alias && !node->analyzed) |
cb0f665d JH |
677 | symtab_resolve_alias |
678 | (node, | |
67348ccc | 679 | TREE_CODE (node->alias_target) == VAR_DECL |
5e20cdc9 DM |
680 | ? (symtab_node *)varpool_node_for_decl (node->alias_target) |
681 | : (symtab_node *)cgraph_get_create_node (node->alias_target)); | |
40a7fe1e | 682 | cpp_implicit_aliases_done = true; |
39e2db00 JH |
683 | } |
684 | ||
768e3c60 RG |
685 | /* Process attributes common for vars and functions. */ |
686 | ||
687 | static void | |
688 | process_common_attributes (tree decl) | |
689 | { | |
690 | tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)); | |
691 | ||
692 | if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl))) | |
693 | { | |
694 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes, | |
695 | "%<weakref%> attribute should be accompanied with" | |
696 | " an %<alias%> attribute"); | |
697 | DECL_WEAK (decl) = 0; | |
779d4b91 JH |
698 | DECL_ATTRIBUTES (decl) = remove_attribute ("weakref", |
699 | DECL_ATTRIBUTES (decl)); | |
768e3c60 RG |
700 | } |
701 | } | |
702 | ||
386b46cf JH |
703 | /* Look for externally_visible and used attributes and mark cgraph nodes |
704 | accordingly. | |
705 | ||
706 | We cannot mark the nodes at the point the attributes are processed (in | |
707 | handle_*_attribute) because the copy of the declarations available at that | |
708 | point may not be canonical. For example, in: | |
709 | ||
710 | void f(); | |
711 | void f() __attribute__((used)); | |
712 | ||
713 | the declaration we see in handle_used_attribute will be the second | |
714 | declaration -- but the front end will subsequently merge that declaration | |
715 | with the original declaration and discard the second declaration. | |
716 | ||
717 | Furthermore, we can't mark these nodes in cgraph_finalize_function because: | |
718 | ||
719 | void f() {} | |
720 | void f() __attribute__((externally_visible)); | |
721 | ||
722 | is valid. | |
723 | ||
724 | So, we walk the nodes at the end of the translation unit, applying the | |
725 | attributes at that point. */ | |
726 | ||
727 | static void | |
728 | process_function_and_variable_attributes (struct cgraph_node *first, | |
8a4a83ed | 729 | struct varpool_node *first_var) |
386b46cf JH |
730 | { |
731 | struct cgraph_node *node; | |
8a4a83ed | 732 | struct varpool_node *vnode; |
386b46cf | 733 | |
2aae7680 JH |
734 | for (node = cgraph_first_function (); node != first; |
735 | node = cgraph_next_function (node)) | |
386b46cf | 736 | { |
67348ccc | 737 | tree decl = node->decl; |
b42186f1 | 738 | if (DECL_PRESERVE_P (decl)) |
ead84f73 | 739 | cgraph_mark_force_output_node (node); |
9d602c59 | 740 | else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl))) |
386b46cf | 741 | { |
67348ccc DM |
742 | if (! TREE_PUBLIC (node->decl)) |
743 | warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes, | |
c5d75364 MLI |
744 | "%<externally_visible%>" |
745 | " attribute have effect only on public objects"); | |
386b46cf | 746 | } |
779d4b91 | 747 | if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)) |
67348ccc | 748 | && (node->definition && !node->alias)) |
779d4b91 | 749 | { |
67348ccc | 750 | warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes, |
779d4b91 JH |
751 | "%<weakref%> attribute ignored" |
752 | " because function is defined"); | |
753 | DECL_WEAK (decl) = 0; | |
754 | DECL_ATTRIBUTES (decl) = remove_attribute ("weakref", | |
755 | DECL_ATTRIBUTES (decl)); | |
756 | } | |
c9fc06dc CB |
757 | |
758 | if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl)) | |
759 | && !DECL_DECLARED_INLINE_P (decl) | |
760 | /* redefining extern inline function makes it DECL_UNINLINABLE. */ | |
761 | && !DECL_UNINLINABLE (decl)) | |
762 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes, | |
763 | "always_inline function might not be inlinable"); | |
764 | ||
768e3c60 | 765 | process_common_attributes (decl); |
386b46cf | 766 | } |
2aae7680 JH |
767 | for (vnode = varpool_first_variable (); vnode != first_var; |
768 | vnode = varpool_next_variable (vnode)) | |
386b46cf | 769 | { |
67348ccc | 770 | tree decl = vnode->decl; |
6649df51 | 771 | if (DECL_EXTERNAL (decl) |
6a6dac52 | 772 | && DECL_INITIAL (decl)) |
6649df51 | 773 | varpool_finalize_decl (decl); |
b42186f1 | 774 | if (DECL_PRESERVE_P (decl)) |
67348ccc | 775 | vnode->force_output = true; |
9d602c59 | 776 | else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl))) |
386b46cf | 777 | { |
67348ccc DM |
778 | if (! TREE_PUBLIC (vnode->decl)) |
779 | warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes, | |
c5d75364 MLI |
780 | "%<externally_visible%>" |
781 | " attribute have effect only on public objects"); | |
386b46cf | 782 | } |
779d4b91 | 783 | if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)) |
67348ccc | 784 | && vnode->definition |
779d4b91 JH |
785 | && DECL_INITIAL (decl)) |
786 | { | |
67348ccc | 787 | warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes, |
779d4b91 JH |
788 | "%<weakref%> attribute ignored" |
789 | " because variable is initialized"); | |
790 | DECL_WEAK (decl) = 0; | |
791 | DECL_ATTRIBUTES (decl) = remove_attribute ("weakref", | |
792 | DECL_ATTRIBUTES (decl)); | |
793 | } | |
768e3c60 | 794 | process_common_attributes (decl); |
386b46cf JH |
795 | } |
796 | } | |
797 | ||
66058468 JH |
798 | /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the |
799 | middle end to output the variable to asm file, if needed or externally | |
800 | visible. */ | |
801 | ||
802 | void | |
803 | varpool_finalize_decl (tree decl) | |
804 | { | |
5d59b5e1 | 805 | struct varpool_node *node = varpool_node_for_decl (decl); |
66058468 | 806 | |
387df871 | 807 | gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl)); |
66058468 | 808 | |
67348ccc | 809 | if (node->definition) |
66058468 JH |
810 | return; |
811 | notice_global_symbol (decl); | |
67348ccc | 812 | node->definition = true; |
66058468 JH |
813 | if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl) |
814 | /* Traditionally we do not eliminate static variables when not | |
815 | optimizing and when not doing toplevel reoder. */ | |
67348ccc DM |
816 | || (!flag_toplevel_reorder && !DECL_COMDAT (node->decl) |
817 | && !DECL_ARTIFICIAL (node->decl))) | |
818 | node->force_output = true; | |
66058468 JH |
819 | |
820 | if (cgraph_state == CGRAPH_STATE_CONSTRUCTION | |
67348ccc DM |
821 | && (decide_is_symbol_needed (node) |
822 | || referred_to_p (node))) | |
823 | enqueue_node (node); | |
66058468 JH |
824 | if (cgraph_state >= CGRAPH_STATE_IPA_SSA) |
825 | varpool_analyze_node (node); | |
0d6bf48c JH |
826 | /* Some frontends produce various interface variables after compilation |
827 | finished. */ | |
828 | if (cgraph_state == CGRAPH_STATE_FINISHED) | |
829 | varpool_assemble_decl (node); | |
66058468 JH |
830 | } |
831 | ||
e18412fc JH |
832 | /* EDGE is an polymorphic call. Mark all possible targets as reachable |
833 | and if there is only one target, perform trivial devirtualization. | |
834 | REACHABLE_CALL_TARGETS collects target lists we already walked to | |
835 | avoid udplicate work. */ | |
836 | ||
837 | static void | |
838 | walk_polymorphic_call_targets (pointer_set_t *reachable_call_targets, | |
839 | struct cgraph_edge *edge) | |
840 | { | |
841 | unsigned int i; | |
842 | void *cache_token; | |
843 | bool final; | |
844 | vec <cgraph_node *>targets | |
845 | = possible_polymorphic_call_targets | |
846 | (edge, &final, &cache_token); | |
847 | ||
848 | if (!pointer_set_insert (reachable_call_targets, | |
849 | cache_token)) | |
850 | { | |
851 | if (cgraph_dump_file) | |
852 | dump_possible_polymorphic_call_targets | |
853 | (cgraph_dump_file, edge); | |
854 | ||
c3284718 | 855 | for (i = 0; i < targets.length (); i++) |
e18412fc JH |
856 | { |
857 | /* Do not bother to mark virtual methods in anonymous namespace; | |
858 | either we will find use of virtual table defining it, or it is | |
859 | unused. */ | |
67348ccc | 860 | if (targets[i]->definition |
e18412fc | 861 | && TREE_CODE |
67348ccc | 862 | (TREE_TYPE (targets[i]->decl)) |
e18412fc JH |
863 | == METHOD_TYPE |
864 | && !type_in_anonymous_namespace_p | |
865 | (method_class_type | |
67348ccc DM |
866 | (TREE_TYPE (targets[i]->decl)))) |
867 | enqueue_node (targets[i]); | |
e18412fc JH |
868 | } |
869 | } | |
870 | ||
871 | /* Very trivial devirtualization; when the type is | |
872 | final or anonymous (so we know all its derivation) | |
873 | and there is only one possible virtual call target, | |
874 | make the edge direct. */ | |
875 | if (final) | |
876 | { | |
c3284718 | 877 | if (targets.length () <= 1) |
e18412fc | 878 | { |
3462aa02 JH |
879 | cgraph_node *target; |
880 | if (targets.length () == 1) | |
881 | target = targets[0]; | |
882 | else | |
883 | target = cgraph_get_create_node | |
884 | (builtin_decl_implicit (BUILT_IN_UNREACHABLE)); | |
885 | ||
e18412fc JH |
886 | if (cgraph_dump_file) |
887 | { | |
888 | fprintf (cgraph_dump_file, | |
889 | "Devirtualizing call: "); | |
890 | print_gimple_stmt (cgraph_dump_file, | |
891 | edge->call_stmt, 0, | |
892 | TDF_SLIM); | |
893 | } | |
3462aa02 | 894 | cgraph_make_edge_direct (edge, target); |
e18412fc JH |
895 | cgraph_redirect_edge_call_stmt_to_callee (edge); |
896 | if (cgraph_dump_file) | |
897 | { | |
898 | fprintf (cgraph_dump_file, | |
899 | "Devirtualized as: "); | |
900 | print_gimple_stmt (cgraph_dump_file, | |
901 | edge->call_stmt, 0, | |
902 | TDF_SLIM); | |
903 | } | |
904 | } | |
905 | } | |
906 | } | |
907 | ||
5d59b5e1 | 908 | |
66058468 JH |
909 | /* Discover all functions and variables that are trivially needed, analyze |
910 | them as well as all functions and variables referred by them */ | |
1c4a429a | 911 | |
151e6f24 | 912 | static void |
e70670cf | 913 | analyze_functions (void) |
1c4a429a | 914 | { |
cd9c7bd2 | 915 | /* Keep track of already processed nodes when called multiple times for |
aabcd309 | 916 | intermodule optimization. */ |
cd9c7bd2 | 917 | static struct cgraph_node *first_analyzed; |
66058468 | 918 | struct cgraph_node *first_handled = first_analyzed; |
8a4a83ed | 919 | static struct varpool_node *first_analyzed_var; |
66058468 | 920 | struct varpool_node *first_handled_var = first_analyzed_var; |
eefe9a99 | 921 | struct pointer_set_t *reachable_call_targets = pointer_set_create (); |
66058468 | 922 | |
5e20cdc9 DM |
923 | symtab_node *node; |
924 | symtab_node *next; | |
66058468 JH |
925 | int i; |
926 | struct ipa_ref *ref; | |
927 | bool changed = true; | |
4f90d3e0 | 928 | location_t saved_loc = input_location; |
1c4a429a | 929 | |
1389294c | 930 | bitmap_obstack_initialize (NULL); |
66058468 | 931 | cgraph_state = CGRAPH_STATE_CONSTRUCTION; |
4f90d3e0 | 932 | input_location = UNKNOWN_LOCATION; |
1c4a429a | 933 | |
40a7fe1e JH |
934 | /* Ugly, but the fixup can not happen at a time same body alias is created; |
935 | C++ FE is confused about the COMDAT groups being right. */ | |
936 | if (cpp_implicit_aliases_done) | |
937 | FOR_EACH_SYMBOL (node) | |
67348ccc | 938 | if (node->cpp_implicit_alias) |
40a7fe1e | 939 | fixup_same_cpp_alias_visibility (node, symtab_alias_target (node)); |
eefe9a99 JH |
940 | if (optimize && flag_devirtualize) |
941 | build_type_inheritance_graph (); | |
40a7fe1e | 942 | |
66058468 JH |
943 | /* Analysis adds static variables that in turn adds references to new functions. |
944 | So we need to iterate the process until it stabilize. */ | |
945 | while (changed) | |
1c4a429a | 946 | { |
66058468 JH |
947 | changed = false; |
948 | process_function_and_variable_attributes (first_analyzed, | |
949 | first_analyzed_var); | |
950 | ||
951 | /* First identify the trivially needed symbols. */ | |
952 | for (node = symtab_nodes; | |
67348ccc DM |
953 | node != first_analyzed |
954 | && node != first_analyzed_var; node = node->next) | |
d71cc23f | 955 | { |
edb983b2 | 956 | if (decide_is_symbol_needed (node)) |
66058468 JH |
957 | { |
958 | enqueue_node (node); | |
959 | if (!changed && cgraph_dump_file) | |
960 | fprintf (cgraph_dump_file, "Trivially needed symbols:"); | |
961 | changed = true; | |
962 | if (cgraph_dump_file) | |
963 | fprintf (cgraph_dump_file, " %s", symtab_node_asm_name (node)); | |
eefe9a99 JH |
964 | if (!changed && cgraph_dump_file) |
965 | fprintf (cgraph_dump_file, "\n"); | |
66058468 | 966 | } |
67348ccc DM |
967 | if (node == first_analyzed |
968 | || node == first_analyzed_var) | |
66058468 | 969 | break; |
d71cc23f | 970 | } |
66058468 JH |
971 | cgraph_process_new_functions (); |
972 | first_analyzed_var = varpool_first_variable (); | |
973 | first_analyzed = cgraph_first_function (); | |
cd4dea62 | 974 | |
66058468 JH |
975 | if (changed && dump_file) |
976 | fprintf (cgraph_dump_file, "\n"); | |
8dafba3c | 977 | |
66058468 JH |
978 | /* Lower representation, build callgraph edges and references for all trivially |
979 | needed symbols and all symbols referred by them. */ | |
5e20cdc9 | 980 | while (first != (symtab_node *)(void *)1) |
b66887e4 | 981 | { |
66058468 JH |
982 | changed = true; |
983 | node = first; | |
5e20cdc9 | 984 | first = (symtab_node *)first->aux; |
5d59b5e1 | 985 | cgraph_node *cnode = dyn_cast <cgraph_node> (node); |
67348ccc | 986 | if (cnode && cnode->definition) |
66058468 JH |
987 | { |
988 | struct cgraph_edge *edge; | |
67348ccc | 989 | tree decl = cnode->decl; |
66058468 | 990 | |
5d59b5e1 LC |
991 | /* ??? It is possible to create extern inline function |
992 | and later using weak alias attribute to kill its body. | |
993 | See gcc.c-torture/compile/20011119-1.c */ | |
66058468 | 994 | if (!DECL_STRUCT_FUNCTION (decl) |
67348ccc | 995 | && !cnode->alias |
3649b9b7 ST |
996 | && !cnode->thunk.thunk_p |
997 | && !cnode->dispatcher_function) | |
66058468 JH |
998 | { |
999 | cgraph_reset_node (cnode); | |
1000 | cnode->local.redefined_extern_inline = true; | |
1001 | continue; | |
1002 | } | |
b66887e4 | 1003 | |
67348ccc | 1004 | if (!cnode->analyzed) |
e70670cf | 1005 | analyze_function (cnode); |
6b20f353 | 1006 | |
66058468 | 1007 | for (edge = cnode->callees; edge; edge = edge->next_callee) |
67348ccc DM |
1008 | if (edge->callee->definition) |
1009 | enqueue_node (edge->callee); | |
eefe9a99 JH |
1010 | if (optimize && flag_devirtualize) |
1011 | { | |
c4be6568 | 1012 | struct cgraph_edge *next; |
e18412fc JH |
1013 | |
1014 | for (edge = cnode->indirect_calls; edge; edge = next) | |
c4be6568 JH |
1015 | { |
1016 | next = edge->next_callee; | |
1017 | if (edge->indirect_info->polymorphic) | |
e18412fc JH |
1018 | walk_polymorphic_call_targets (reachable_call_targets, |
1019 | edge); | |
c4be6568 | 1020 | } |
eefe9a99 | 1021 | } |
66058468 | 1022 | |
5d59b5e1 LC |
1023 | /* If decl is a clone of an abstract function, |
1024 | mark that abstract function so that we don't release its body. | |
1025 | The DECL_INITIAL() of that abstract function declaration | |
1026 | will be later needed to output debug info. */ | |
66058468 JH |
1027 | if (DECL_ABSTRACT_ORIGIN (decl)) |
1028 | { | |
5d59b5e1 LC |
1029 | struct cgraph_node *origin_node |
1030 | = cgraph_get_node (DECL_ABSTRACT_ORIGIN (decl)); | |
c0c123ef | 1031 | origin_node->used_as_abstract_origin = true; |
66058468 | 1032 | } |
66058468 | 1033 | } |
5d59b5e1 LC |
1034 | else |
1035 | { | |
1036 | varpool_node *vnode = dyn_cast <varpool_node> (node); | |
67348ccc | 1037 | if (vnode && vnode->definition && !vnode->analyzed) |
5d59b5e1 LC |
1038 | varpool_analyze_node (vnode); |
1039 | } | |
66058468 | 1040 | |
67348ccc | 1041 | if (node->same_comdat_group) |
66058468 | 1042 | { |
5e20cdc9 | 1043 | symtab_node *next; |
67348ccc | 1044 | for (next = node->same_comdat_group; |
66058468 | 1045 | next != node; |
67348ccc | 1046 | next = next->same_comdat_group) |
66058468 JH |
1047 | enqueue_node (next); |
1048 | } | |
67348ccc DM |
1049 | for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++) |
1050 | if (ref->referred->definition) | |
66058468 JH |
1051 | enqueue_node (ref->referred); |
1052 | cgraph_process_new_functions (); | |
1053 | } | |
1c4a429a | 1054 | } |
0e1474e5 JH |
1055 | if (optimize && flag_devirtualize) |
1056 | update_type_inheritance_graph (); | |
8dafba3c | 1057 | |
564738df | 1058 | /* Collect entry points to the unit. */ |
a194aa56 | 1059 | if (cgraph_dump_file) |
1668aabc | 1060 | { |
7d82fe7c | 1061 | fprintf (cgraph_dump_file, "\n\nInitial "); |
8f940ee6 | 1062 | dump_symtab (cgraph_dump_file); |
1668aabc | 1063 | } |
7660e67e | 1064 | |
a194aa56 | 1065 | if (cgraph_dump_file) |
66058468 | 1066 | fprintf (cgraph_dump_file, "\nRemoving unused symbols:"); |
1c4a429a | 1067 | |
66058468 | 1068 | for (node = symtab_nodes; |
67348ccc DM |
1069 | node != first_handled |
1070 | && node != first_handled_var; node = next) | |
1c4a429a | 1071 | { |
67348ccc DM |
1072 | next = node->next; |
1073 | if (!node->aux && !referred_to_p (node)) | |
1c4a429a | 1074 | { |
a194aa56 | 1075 | if (cgraph_dump_file) |
66058468 JH |
1076 | fprintf (cgraph_dump_file, " %s", symtab_node_name (node)); |
1077 | symtab_remove_node (node); | |
d71cc23f | 1078 | continue; |
1c4a429a | 1079 | } |
5d59b5e1 | 1080 | if (cgraph_node *cnode = dyn_cast <cgraph_node> (node)) |
66058468 | 1081 | { |
67348ccc | 1082 | tree decl = node->decl; |
66058468 | 1083 | |
67348ccc DM |
1084 | if (cnode->definition && !gimple_has_body_p (decl) |
1085 | && !cnode->alias | |
66058468 JH |
1086 | && !cnode->thunk.thunk_p) |
1087 | cgraph_reset_node (cnode); | |
1088 | ||
67348ccc DM |
1089 | gcc_assert (!cnode->definition || cnode->thunk.thunk_p |
1090 | || cnode->alias | |
66058468 | 1091 | || gimple_has_body_p (decl)); |
67348ccc | 1092 | gcc_assert (cnode->analyzed == cnode->definition); |
66058468 | 1093 | } |
67348ccc | 1094 | node->aux = NULL; |
1c4a429a | 1095 | } |
67348ccc DM |
1096 | for (;node; node = node->next) |
1097 | node->aux = NULL; | |
66058468 JH |
1098 | first_analyzed = cgraph_first_function (); |
1099 | first_analyzed_var = varpool_first_variable (); | |
a194aa56 | 1100 | if (cgraph_dump_file) |
7d82fe7c KC |
1101 | { |
1102 | fprintf (cgraph_dump_file, "\n\nReclaimed "); | |
8f940ee6 | 1103 | dump_symtab (cgraph_dump_file); |
7d82fe7c | 1104 | } |
1389294c | 1105 | bitmap_obstack_release (NULL); |
eefe9a99 | 1106 | pointer_set_destroy (reachable_call_targets); |
1c4a429a | 1107 | ggc_collect (); |
d352b245 JH |
1108 | /* Initialize assembler name hash, in particular we want to trigger C++ |
1109 | mangling and same body alias creation before we free DECL_ARGUMENTS | |
1110 | used by it. */ | |
1111 | if (!seen_error ()) | |
3462aa02 | 1112 | symtab_initialize_asm_name_hash (); |
4f90d3e0 JH |
1113 | |
1114 | input_location = saved_loc; | |
151e6f24 JH |
1115 | } |
1116 | ||
85ce9375 JH |
1117 | /* Translate the ugly representation of aliases as alias pairs into nice |
1118 | representation in callgraph. We don't handle all cases yet, | |
4f219961 | 1119 | unfortunately. */ |
85ce9375 JH |
1120 | |
1121 | static void | |
1122 | handle_alias_pairs (void) | |
1123 | { | |
1124 | alias_pair *p; | |
1125 | unsigned i; | |
85ce9375 | 1126 | |
9771b263 | 1127 | for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);) |
85ce9375 | 1128 | { |
5e20cdc9 | 1129 | symtab_node *target_node = symtab_node_for_asm (p->target); |
38e55ac9 | 1130 | |
4f219961 EB |
1131 | /* Weakrefs with target not defined in current unit are easy to handle: |
1132 | they behave just as external variables except we need to note the | |
1133 | alias flag to later output the weakref pseudo op into asm file. */ | |
1134 | if (!target_node | |
1135 | && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL) | |
25e2c40d | 1136 | { |
5e20cdc9 | 1137 | symtab_node *node = symtab_get_node (p->decl); |
40a7fe1e | 1138 | if (node) |
e01c7cca | 1139 | { |
67348ccc DM |
1140 | node->alias_target = p->target; |
1141 | node->weakref = true; | |
1142 | node->alias = true; | |
e01c7cca | 1143 | } |
9771b263 | 1144 | alias_pairs->unordered_remove (i); |
38e55ac9 | 1145 | continue; |
25e2c40d | 1146 | } |
38e55ac9 | 1147 | else if (!target_node) |
85ce9375 | 1148 | { |
38e55ac9 | 1149 | error ("%q+D aliased to undefined symbol %qE", p->decl, p->target); |
5e20cdc9 | 1150 | symtab_node *node = symtab_get_node (p->decl); |
4f219961 | 1151 | if (node) |
67348ccc | 1152 | node->alias = false; |
9771b263 | 1153 | alias_pairs->unordered_remove (i); |
38e55ac9 JH |
1154 | continue; |
1155 | } | |
1156 | ||
67348ccc | 1157 | if (DECL_EXTERNAL (target_node->decl) |
07250f0e JH |
1158 | /* We use local aliases for C++ thunks to force the tailcall |
1159 | to bind locally. This is a hack - to keep it working do | |
1160 | the following (which is not strictly correct). */ | |
67348ccc DM |
1161 | && (! TREE_CODE (target_node->decl) == FUNCTION_DECL |
1162 | || ! DECL_VIRTUAL_P (target_node->decl)) | |
07250f0e JH |
1163 | && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl))) |
1164 | { | |
1165 | error ("%q+D aliased to external symbol %qE", | |
1166 | p->decl, p->target); | |
1167 | } | |
1168 | ||
38e55ac9 | 1169 | if (TREE_CODE (p->decl) == FUNCTION_DECL |
5d59b5e1 | 1170 | && target_node && is_a <cgraph_node> (target_node)) |
38e55ac9 JH |
1171 | { |
1172 | struct cgraph_node *src_node = cgraph_get_node (p->decl); | |
67348ccc | 1173 | if (src_node && src_node->definition) |
38e55ac9 | 1174 | cgraph_reset_node (src_node); |
67348ccc | 1175 | cgraph_create_function_alias (p->decl, target_node->decl); |
9771b263 | 1176 | alias_pairs->unordered_remove (i); |
38e55ac9 JH |
1177 | } |
1178 | else if (TREE_CODE (p->decl) == VAR_DECL | |
5d59b5e1 | 1179 | && target_node && is_a <varpool_node> (target_node)) |
38e55ac9 | 1180 | { |
67348ccc | 1181 | varpool_create_variable_alias (p->decl, target_node->decl); |
9771b263 | 1182 | alias_pairs->unordered_remove (i); |
38e55ac9 JH |
1183 | } |
1184 | else | |
1185 | { | |
1186 | error ("%q+D alias in between function and variable is not supported", | |
1187 | p->decl); | |
1188 | warning (0, "%q+D aliased declaration", | |
67348ccc | 1189 | target_node->decl); |
9771b263 | 1190 | alias_pairs->unordered_remove (i); |
85ce9375 JH |
1191 | } |
1192 | } | |
9771b263 | 1193 | vec_free (alias_pairs); |
85ce9375 JH |
1194 | } |
1195 | ||
5f1a9ebb | 1196 | |
1c4a429a JH |
1197 | /* Figure out what functions we want to assemble. */ |
1198 | ||
1199 | static void | |
65d630d4 | 1200 | mark_functions_to_output (void) |
1c4a429a JH |
1201 | { |
1202 | struct cgraph_node *node; | |
b66887e4 JJ |
1203 | #ifdef ENABLE_CHECKING |
1204 | bool check_same_comdat_groups = false; | |
1205 | ||
65c70e6b | 1206 | FOR_EACH_FUNCTION (node) |
b66887e4 JJ |
1207 | gcc_assert (!node->process); |
1208 | #endif | |
1c4a429a | 1209 | |
65c70e6b | 1210 | FOR_EACH_FUNCTION (node) |
1c4a429a | 1211 | { |
67348ccc | 1212 | tree decl = node->decl; |
c22cacf3 | 1213 | |
67348ccc | 1214 | gcc_assert (!node->process || node->same_comdat_group); |
b66887e4 JJ |
1215 | if (node->process) |
1216 | continue; | |
b58b1157 | 1217 | |
7660e67e SB |
1218 | /* We need to output all local functions that are used and not |
1219 | always inlined, as well as those that are reachable from | |
1220 | outside the current compilation unit. */ | |
67348ccc | 1221 | if (node->analyzed |
c47d0034 | 1222 | && !node->thunk.thunk_p |
67348ccc | 1223 | && !node->alias |
18c6ada9 | 1224 | && !node->global.inlined_to |
6de9cd9a | 1225 | && !TREE_ASM_WRITTEN (decl) |
1c4a429a | 1226 | && !DECL_EXTERNAL (decl)) |
b66887e4 JJ |
1227 | { |
1228 | node->process = 1; | |
67348ccc | 1229 | if (node->same_comdat_group) |
b66887e4 JJ |
1230 | { |
1231 | struct cgraph_node *next; | |
67348ccc | 1232 | for (next = cgraph (node->same_comdat_group); |
b66887e4 | 1233 | next != node; |
67348ccc DM |
1234 | next = cgraph (next->same_comdat_group)) |
1235 | if (!next->thunk.thunk_p && !next->alias) | |
c47d0034 | 1236 | next->process = 1; |
b66887e4 JJ |
1237 | } |
1238 | } | |
67348ccc | 1239 | else if (node->same_comdat_group) |
b66887e4 JJ |
1240 | { |
1241 | #ifdef ENABLE_CHECKING | |
1242 | check_same_comdat_groups = true; | |
1243 | #endif | |
1244 | } | |
341c100f | 1245 | else |
1a2caa7a NS |
1246 | { |
1247 | /* We should've reclaimed all functions that are not needed. */ | |
1248 | #ifdef ENABLE_CHECKING | |
726a989a | 1249 | if (!node->global.inlined_to |
39ecc018 | 1250 | && gimple_has_body_p (decl) |
a837268b JH |
1251 | /* FIXME: in ltrans unit when offline copy is outside partition but inline copies |
1252 | are inside partition, we can end up not removing the body since we no longer | |
1253 | have analyzed node pointing to it. */ | |
67348ccc DM |
1254 | && !node->in_other_partition |
1255 | && !node->alias | |
387df871 | 1256 | && !node->clones |
1a2caa7a NS |
1257 | && !DECL_EXTERNAL (decl)) |
1258 | { | |
1259 | dump_cgraph_node (stderr, node); | |
1260 | internal_error ("failed to reclaim unneeded function"); | |
1261 | } | |
1262 | #endif | |
726a989a | 1263 | gcc_assert (node->global.inlined_to |
39ecc018 | 1264 | || !gimple_has_body_p (decl) |
67348ccc | 1265 | || node->in_other_partition |
6649df51 JH |
1266 | || node->clones |
1267 | || DECL_ARTIFICIAL (decl) | |
1a2caa7a NS |
1268 | || DECL_EXTERNAL (decl)); |
1269 | ||
1270 | } | |
c22cacf3 | 1271 | |
18d13f34 | 1272 | } |
b66887e4 JJ |
1273 | #ifdef ENABLE_CHECKING |
1274 | if (check_same_comdat_groups) | |
65c70e6b | 1275 | FOR_EACH_FUNCTION (node) |
67348ccc | 1276 | if (node->same_comdat_group && !node->process) |
b66887e4 | 1277 | { |
67348ccc | 1278 | tree decl = node->decl; |
b66887e4 JJ |
1279 | if (!node->global.inlined_to |
1280 | && gimple_has_body_p (decl) | |
7cbf224d RG |
1281 | /* FIXME: in an ltrans unit when the offline copy is outside a |
1282 | partition but inline copies are inside a partition, we can | |
1283 | end up not removing the body since we no longer have an | |
1284 | analyzed node pointing to it. */ | |
67348ccc | 1285 | && !node->in_other_partition |
07250f0e | 1286 | && !node->clones |
b66887e4 JJ |
1287 | && !DECL_EXTERNAL (decl)) |
1288 | { | |
1289 | dump_cgraph_node (stderr, node); | |
7cbf224d RG |
1290 | internal_error ("failed to reclaim unneeded function in same " |
1291 | "comdat group"); | |
b66887e4 JJ |
1292 | } |
1293 | } | |
1294 | #endif | |
18d13f34 JH |
1295 | } |
1296 | ||
6744a6ab | 1297 | /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function |
3649b9b7 | 1298 | in lowered gimple form. IN_SSA is true if the gimple is in SSA. |
6744a6ab JH |
1299 | |
1300 | Set current_function_decl and cfun to newly constructed empty function body. | |
1301 | return basic block in the function body. */ | |
1302 | ||
3649b9b7 ST |
1303 | basic_block |
1304 | init_lowered_empty_function (tree decl, bool in_ssa) | |
6744a6ab JH |
1305 | { |
1306 | basic_block bb; | |
1307 | ||
1308 | current_function_decl = decl; | |
1309 | allocate_struct_function (decl, false); | |
1310 | gimple_register_cfg_hooks (); | |
1311 | init_empty_tree_cfg (); | |
3649b9b7 ST |
1312 | |
1313 | if (in_ssa) | |
1314 | { | |
1315 | init_tree_ssa (cfun); | |
1316 | init_ssa_operands (cfun); | |
1317 | cfun->gimple_df->in_ssa_p = true; | |
31ee20ba | 1318 | cfun->curr_properties |= PROP_ssa; |
3649b9b7 ST |
1319 | } |
1320 | ||
6744a6ab JH |
1321 | DECL_INITIAL (decl) = make_node (BLOCK); |
1322 | ||
1323 | DECL_SAVED_TREE (decl) = error_mark_node; | |
31ee20ba RB |
1324 | cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any |
1325 | | PROP_cfg | PROP_loops); | |
1326 | ||
1327 | set_loops_for_fn (cfun, ggc_alloc_cleared_loops ()); | |
1328 | init_loops_structure (cfun, loops_for_fn (cfun), 1); | |
1329 | loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES; | |
6744a6ab JH |
1330 | |
1331 | /* Create BB for body of the function and connect it properly. */ | |
1332 | bb = create_basic_block (NULL, (void *) 0, ENTRY_BLOCK_PTR); | |
31ee20ba | 1333 | make_edge (ENTRY_BLOCK_PTR, bb, EDGE_FALLTHRU); |
4f9c574a | 1334 | make_edge (bb, EXIT_BLOCK_PTR, 0); |
31ee20ba | 1335 | add_bb_to_loop (bb, ENTRY_BLOCK_PTR->loop_father); |
6744a6ab JH |
1336 | |
1337 | return bb; | |
1338 | } | |
1339 | ||
1340 | /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable | |
1341 | offset indicated by VIRTUAL_OFFSET, if that is | |
1342 | non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and | |
1343 | zero for a result adjusting thunk. */ | |
1344 | ||
1345 | static tree | |
1346 | thunk_adjust (gimple_stmt_iterator * bsi, | |
1347 | tree ptr, bool this_adjusting, | |
1348 | HOST_WIDE_INT fixed_offset, tree virtual_offset) | |
1349 | { | |
1350 | gimple stmt; | |
1351 | tree ret; | |
1352 | ||
313333a6 RG |
1353 | if (this_adjusting |
1354 | && fixed_offset != 0) | |
6744a6ab | 1355 | { |
5d49b6a7 RG |
1356 | stmt = gimple_build_assign |
1357 | (ptr, fold_build_pointer_plus_hwi_loc (input_location, | |
1358 | ptr, | |
1359 | fixed_offset)); | |
6744a6ab JH |
1360 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); |
1361 | } | |
1362 | ||
1363 | /* If there's a virtual offset, look up that value in the vtable and | |
1364 | adjust the pointer again. */ | |
1365 | if (virtual_offset) | |
1366 | { | |
1367 | tree vtabletmp; | |
1368 | tree vtabletmp2; | |
1369 | tree vtabletmp3; | |
6744a6ab JH |
1370 | |
1371 | if (!vtable_entry_type) | |
1372 | { | |
1373 | tree vfunc_type = make_node (FUNCTION_TYPE); | |
1374 | TREE_TYPE (vfunc_type) = integer_type_node; | |
1375 | TYPE_ARG_TYPES (vfunc_type) = NULL_TREE; | |
1376 | layout_type (vfunc_type); | |
1377 | ||
1378 | vtable_entry_type = build_pointer_type (vfunc_type); | |
1379 | } | |
1380 | ||
1381 | vtabletmp = | |
7cc434a3 | 1382 | create_tmp_reg (build_pointer_type |
7d80ca1f | 1383 | (build_pointer_type (vtable_entry_type)), "vptr"); |
6744a6ab JH |
1384 | |
1385 | /* The vptr is always at offset zero in the object. */ | |
1386 | stmt = gimple_build_assign (vtabletmp, | |
1387 | build1 (NOP_EXPR, TREE_TYPE (vtabletmp), | |
1388 | ptr)); | |
1389 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); | |
6744a6ab JH |
1390 | |
1391 | /* Form the vtable address. */ | |
7cc434a3 | 1392 | vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)), |
7d80ca1f | 1393 | "vtableaddr"); |
6744a6ab | 1394 | stmt = gimple_build_assign (vtabletmp2, |
70f34814 | 1395 | build_simple_mem_ref (vtabletmp)); |
6744a6ab | 1396 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); |
6744a6ab JH |
1397 | |
1398 | /* Find the entry with the vcall offset. */ | |
1399 | stmt = gimple_build_assign (vtabletmp2, | |
5d49b6a7 RG |
1400 | fold_build_pointer_plus_loc (input_location, |
1401 | vtabletmp2, | |
1402 | virtual_offset)); | |
6744a6ab JH |
1403 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); |
1404 | ||
1405 | /* Get the offset itself. */ | |
7cc434a3 | 1406 | vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)), |
7d80ca1f | 1407 | "vcalloffset"); |
6744a6ab | 1408 | stmt = gimple_build_assign (vtabletmp3, |
70f34814 | 1409 | build_simple_mem_ref (vtabletmp2)); |
6744a6ab | 1410 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); |
6744a6ab | 1411 | |
6744a6ab | 1412 | /* Adjust the `this' pointer. */ |
0d82a1c8 RG |
1413 | ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3); |
1414 | ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false, | |
1415 | GSI_CONTINUE_LINKING); | |
6744a6ab JH |
1416 | } |
1417 | ||
313333a6 RG |
1418 | if (!this_adjusting |
1419 | && fixed_offset != 0) | |
6744a6ab JH |
1420 | /* Adjust the pointer by the constant. */ |
1421 | { | |
1422 | tree ptrtmp; | |
1423 | ||
1424 | if (TREE_CODE (ptr) == VAR_DECL) | |
1425 | ptrtmp = ptr; | |
1426 | else | |
1427 | { | |
7cc434a3 | 1428 | ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr"); |
6744a6ab JH |
1429 | stmt = gimple_build_assign (ptrtmp, ptr); |
1430 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); | |
6744a6ab | 1431 | } |
5d49b6a7 RG |
1432 | ptr = fold_build_pointer_plus_hwi_loc (input_location, |
1433 | ptrtmp, fixed_offset); | |
6744a6ab JH |
1434 | } |
1435 | ||
1436 | /* Emit the statement and gimplify the adjustment expression. */ | |
7cc434a3 | 1437 | ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this"); |
6744a6ab | 1438 | stmt = gimple_build_assign (ret, ptr); |
6744a6ab JH |
1439 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); |
1440 | ||
1441 | return ret; | |
1442 | } | |
1443 | ||
afdec9bd JH |
1444 | /* Expand thunk NODE to gimple if possible. |
1445 | When OUTPUT_ASM_THUNK is true, also produce assembler for | |
1446 | thunks that are not lowered. */ | |
6744a6ab | 1447 | |
afdec9bd JH |
1448 | bool |
1449 | expand_thunk (struct cgraph_node *node, bool output_asm_thunks) | |
6744a6ab JH |
1450 | { |
1451 | bool this_adjusting = node->thunk.this_adjusting; | |
1452 | HOST_WIDE_INT fixed_offset = node->thunk.fixed_offset; | |
1453 | HOST_WIDE_INT virtual_value = node->thunk.virtual_value; | |
1454 | tree virtual_offset = NULL; | |
67348ccc DM |
1455 | tree alias = node->callees->callee->decl; |
1456 | tree thunk_fndecl = node->decl; | |
bcb650cb JH |
1457 | tree a; |
1458 | ||
d06865bf | 1459 | |
6744a6ab JH |
1460 | if (this_adjusting |
1461 | && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset, | |
1462 | virtual_value, alias)) | |
1463 | { | |
1464 | const char *fnname; | |
1465 | tree fn_block; | |
4399cf59 | 1466 | tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl)); |
afdec9bd JH |
1467 | |
1468 | if (!output_asm_thunks) | |
1469 | return false; | |
1470 | ||
1471 | if (in_lto_p) | |
1472 | cgraph_get_body (node); | |
1473 | a = DECL_ARGUMENTS (thunk_fndecl); | |
6744a6ab | 1474 | |
afdec9bd JH |
1475 | current_function_decl = thunk_fndecl; |
1476 | ||
1477 | /* Ensure thunks are emitted in their correct sections. */ | |
1478 | resolve_unique_section (thunk_fndecl, 0, flag_function_sections); | |
1479 | ||
6744a6ab JH |
1480 | DECL_RESULT (thunk_fndecl) |
1481 | = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl), | |
4399cf59 | 1482 | RESULT_DECL, 0, restype); |
afdec9bd | 1483 | DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl; |
15488554 | 1484 | fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl)); |
6744a6ab JH |
1485 | |
1486 | /* The back end expects DECL_INITIAL to contain a BLOCK, so we | |
1487 | create one. */ | |
1488 | fn_block = make_node (BLOCK); | |
1489 | BLOCK_VARS (fn_block) = a; | |
1490 | DECL_INITIAL (thunk_fndecl) = fn_block; | |
1491 | init_function_start (thunk_fndecl); | |
1492 | cfun->is_thunk = 1; | |
68a55980 JJ |
1493 | insn_locations_init (); |
1494 | set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl)); | |
1495 | prologue_location = curr_insn_location (); | |
6744a6ab JH |
1496 | assemble_start_function (thunk_fndecl, fnname); |
1497 | ||
1498 | targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl, | |
1499 | fixed_offset, virtual_value, alias); | |
1500 | ||
1501 | assemble_end_function (thunk_fndecl, fnname); | |
68a55980 | 1502 | insn_locations_finalize (); |
6744a6ab JH |
1503 | init_insn_lengths (); |
1504 | free_after_compilation (cfun); | |
1505 | set_cfun (NULL); | |
1506 | TREE_ASM_WRITTEN (thunk_fndecl) = 1; | |
c47d0034 | 1507 | node->thunk.thunk_p = false; |
67348ccc | 1508 | node->analyzed = false; |
6744a6ab JH |
1509 | } |
1510 | else | |
1511 | { | |
1512 | tree restype; | |
1513 | basic_block bb, then_bb, else_bb, return_bb; | |
1514 | gimple_stmt_iterator bsi; | |
1515 | int nargs = 0; | |
1516 | tree arg; | |
1517 | int i; | |
1518 | tree resdecl; | |
1519 | tree restmp = NULL; | |
9771b263 | 1520 | vec<tree> vargs; |
6744a6ab JH |
1521 | |
1522 | gimple call; | |
1523 | gimple ret; | |
1524 | ||
afdec9bd JH |
1525 | if (in_lto_p) |
1526 | cgraph_get_body (node); | |
1527 | a = DECL_ARGUMENTS (thunk_fndecl); | |
1528 | ||
1529 | current_function_decl = thunk_fndecl; | |
1530 | ||
1531 | /* Ensure thunks are emitted in their correct sections. */ | |
1532 | resolve_unique_section (thunk_fndecl, 0, flag_function_sections); | |
1533 | ||
6744a6ab JH |
1534 | DECL_IGNORED_P (thunk_fndecl) = 1; |
1535 | bitmap_obstack_initialize (NULL); | |
1536 | ||
1537 | if (node->thunk.virtual_offset_p) | |
1538 | virtual_offset = size_int (virtual_value); | |
1539 | ||
1540 | /* Build the return declaration for the function. */ | |
1541 | restype = TREE_TYPE (TREE_TYPE (thunk_fndecl)); | |
1542 | if (DECL_RESULT (thunk_fndecl) == NULL_TREE) | |
1543 | { | |
1544 | resdecl = build_decl (input_location, RESULT_DECL, 0, restype); | |
1545 | DECL_ARTIFICIAL (resdecl) = 1; | |
1546 | DECL_IGNORED_P (resdecl) = 1; | |
1547 | DECL_RESULT (thunk_fndecl) = resdecl; | |
afdec9bd | 1548 | DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl; |
6744a6ab JH |
1549 | } |
1550 | else | |
1551 | resdecl = DECL_RESULT (thunk_fndecl); | |
1552 | ||
3649b9b7 | 1553 | bb = then_bb = else_bb = return_bb = init_lowered_empty_function (thunk_fndecl, true); |
6744a6ab JH |
1554 | |
1555 | bsi = gsi_start_bb (bb); | |
1556 | ||
1557 | /* Build call to the function being thunked. */ | |
1558 | if (!VOID_TYPE_P (restype)) | |
1559 | { | |
bc0ec027 JH |
1560 | if (DECL_BY_REFERENCE (resdecl)) |
1561 | restmp = gimple_fold_indirect_ref (resdecl); | |
1562 | else if (!is_gimple_reg_type (restype)) | |
6744a6ab JH |
1563 | { |
1564 | restmp = resdecl; | |
c021f10b | 1565 | add_local_decl (cfun, restmp); |
6744a6ab JH |
1566 | BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp; |
1567 | } | |
1568 | else | |
7cc434a3 | 1569 | restmp = create_tmp_reg (restype, "retval"); |
6744a6ab JH |
1570 | } |
1571 | ||
910ad8de | 1572 | for (arg = a; arg; arg = DECL_CHAIN (arg)) |
6744a6ab | 1573 | nargs++; |
9771b263 | 1574 | vargs.create (nargs); |
6744a6ab | 1575 | if (this_adjusting) |
9771b263 DN |
1576 | vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset, |
1577 | virtual_offset)); | |
bc0ec027 | 1578 | else if (nargs) |
9771b263 | 1579 | vargs.quick_push (a); |
bc0ec027 JH |
1580 | |
1581 | if (nargs) | |
1582 | for (i = 1, arg = DECL_CHAIN (a); i < nargs; i++, arg = DECL_CHAIN (arg)) | |
1583 | vargs.quick_push (arg); | |
6744a6ab | 1584 | call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs); |
afdec9bd | 1585 | node->callees->call_stmt = call; |
9771b263 | 1586 | vargs.release (); |
6744a6ab JH |
1587 | gimple_call_set_from_thunk (call, true); |
1588 | if (restmp) | |
bc0ec027 JH |
1589 | { |
1590 | gimple_call_set_lhs (call, restmp); | |
1591 | gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp), | |
1592 | TREE_TYPE (TREE_TYPE (alias)))); | |
1593 | } | |
6744a6ab | 1594 | gsi_insert_after (&bsi, call, GSI_NEW_STMT); |
bc0ec027 JH |
1595 | if (!(gimple_call_flags (call) & ECF_NORETURN)) |
1596 | { | |
1597 | if (restmp && !this_adjusting | |
1598 | && (fixed_offset || virtual_offset)) | |
1599 | { | |
1600 | tree true_label = NULL_TREE; | |
6744a6ab | 1601 | |
bc0ec027 JH |
1602 | if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE) |
1603 | { | |
1604 | gimple stmt; | |
1605 | /* If the return type is a pointer, we need to | |
1606 | protect against NULL. We know there will be an | |
1607 | adjustment, because that's why we're emitting a | |
1608 | thunk. */ | |
1609 | then_bb = create_basic_block (NULL, (void *) 0, bb); | |
1610 | return_bb = create_basic_block (NULL, (void *) 0, then_bb); | |
1611 | else_bb = create_basic_block (NULL, (void *) 0, else_bb); | |
1612 | add_bb_to_loop (then_bb, bb->loop_father); | |
1613 | add_bb_to_loop (return_bb, bb->loop_father); | |
1614 | add_bb_to_loop (else_bb, bb->loop_father); | |
1615 | remove_edge (single_succ_edge (bb)); | |
1616 | true_label = gimple_block_label (then_bb); | |
1617 | stmt = gimple_build_cond (NE_EXPR, restmp, | |
1618 | build_zero_cst (TREE_TYPE (restmp)), | |
1619 | NULL_TREE, NULL_TREE); | |
1620 | gsi_insert_after (&bsi, stmt, GSI_NEW_STMT); | |
1621 | make_edge (bb, then_bb, EDGE_TRUE_VALUE); | |
1622 | make_edge (bb, else_bb, EDGE_FALSE_VALUE); | |
1623 | make_edge (return_bb, EXIT_BLOCK_PTR, 0); | |
1624 | make_edge (then_bb, return_bb, EDGE_FALLTHRU); | |
1625 | make_edge (else_bb, return_bb, EDGE_FALLTHRU); | |
1626 | bsi = gsi_last_bb (then_bb); | |
1627 | } | |
6744a6ab | 1628 | |
bc0ec027 JH |
1629 | restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0, |
1630 | fixed_offset, virtual_offset); | |
1631 | if (true_label) | |
1632 | { | |
1633 | gimple stmt; | |
1634 | bsi = gsi_last_bb (else_bb); | |
1635 | stmt = gimple_build_assign (restmp, | |
1636 | build_zero_cst (TREE_TYPE (restmp))); | |
1637 | gsi_insert_after (&bsi, stmt, GSI_NEW_STMT); | |
1638 | bsi = gsi_last_bb (return_bb); | |
1639 | } | |
6744a6ab | 1640 | } |
bc0ec027 JH |
1641 | else |
1642 | gimple_call_set_tail (call, true); | |
6744a6ab | 1643 | |
bc0ec027 JH |
1644 | /* Build return value. */ |
1645 | ret = gimple_build_return (restmp); | |
1646 | gsi_insert_after (&bsi, ret, GSI_NEW_STMT); | |
6744a6ab JH |
1647 | } |
1648 | else | |
bc0ec027 JH |
1649 | { |
1650 | gimple_call_set_tail (call, true); | |
1651 | remove_edge (single_succ_edge (bb)); | |
1652 | } | |
6744a6ab | 1653 | |
afdec9bd JH |
1654 | cfun->gimple_df->in_ssa_p = true; |
1655 | /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */ | |
1656 | TREE_ASM_WRITTEN (thunk_fndecl) = false; | |
6744a6ab JH |
1657 | delete_unreachable_blocks (); |
1658 | update_ssa (TODO_update_ssa); | |
bc0ec027 JH |
1659 | #ifdef ENABLE_CHECKING |
1660 | verify_flow_info (); | |
1661 | #endif | |
6744a6ab | 1662 | |
6744a6ab JH |
1663 | /* Since we want to emit the thunk, we explicitly mark its name as |
1664 | referenced. */ | |
c47d0034 | 1665 | node->thunk.thunk_p = false; |
afdec9bd | 1666 | node->lowered = true; |
6744a6ab JH |
1667 | bitmap_obstack_release (NULL); |
1668 | } | |
1669 | current_function_decl = NULL; | |
af16bc76 | 1670 | set_cfun (NULL); |
afdec9bd | 1671 | return true; |
6744a6ab JH |
1672 | } |
1673 | ||
073a8998 | 1674 | /* Assemble thunks and aliases associated to NODE. */ |
c47d0034 JH |
1675 | |
1676 | static void | |
39e2db00 | 1677 | assemble_thunks_and_aliases (struct cgraph_node *node) |
c47d0034 JH |
1678 | { |
1679 | struct cgraph_edge *e; | |
39e2db00 JH |
1680 | int i; |
1681 | struct ipa_ref *ref; | |
1682 | ||
c47d0034 JH |
1683 | for (e = node->callers; e;) |
1684 | if (e->caller->thunk.thunk_p) | |
1685 | { | |
1686 | struct cgraph_node *thunk = e->caller; | |
1687 | ||
1688 | e = e->next_caller; | |
39e2db00 | 1689 | assemble_thunks_and_aliases (thunk); |
afdec9bd | 1690 | expand_thunk (thunk, true); |
c47d0034 JH |
1691 | } |
1692 | else | |
1693 | e = e->next_caller; | |
67348ccc | 1694 | for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, |
960bfb69 | 1695 | i, ref); i++) |
39e2db00 JH |
1696 | if (ref->use == IPA_REF_ALIAS) |
1697 | { | |
5932a4d4 | 1698 | struct cgraph_node *alias = ipa_ref_referring_node (ref); |
67348ccc | 1699 | bool saved_written = TREE_ASM_WRITTEN (node->decl); |
42f833bc JH |
1700 | |
1701 | /* Force assemble_alias to really output the alias this time instead | |
1702 | of buffering it in same alias pairs. */ | |
67348ccc DM |
1703 | TREE_ASM_WRITTEN (node->decl) = 1; |
1704 | do_assemble_alias (alias->decl, | |
1705 | DECL_ASSEMBLER_NAME (node->decl)); | |
39e2db00 | 1706 | assemble_thunks_and_aliases (alias); |
67348ccc | 1707 | TREE_ASM_WRITTEN (node->decl) = saved_written; |
39e2db00 | 1708 | } |
c47d0034 JH |
1709 | } |
1710 | ||
9c8305f8 | 1711 | /* Expand function specified by NODE. */ |
0878843f | 1712 | |
452aa9c5 | 1713 | static void |
65d630d4 | 1714 | expand_function (struct cgraph_node *node) |
0878843f | 1715 | { |
67348ccc | 1716 | tree decl = node->decl; |
0878843f RG |
1717 | location_t saved_loc; |
1718 | ||
9c8305f8 JH |
1719 | /* We ought to not compile any inline clones. */ |
1720 | gcc_assert (!node->global.inlined_to); | |
1721 | ||
1722 | announce_function (decl); | |
1723 | node->process = 0; | |
1724 | gcc_assert (node->lowered); | |
a2e2a668 | 1725 | cgraph_get_body (node); |
9c8305f8 JH |
1726 | |
1727 | /* Generate RTL for the body of DECL. */ | |
1728 | ||
0878843f RG |
1729 | timevar_push (TV_REST_OF_COMPILATION); |
1730 | ||
1731 | gcc_assert (cgraph_global_info_ready); | |
1732 | ||
1733 | /* Initialize the default bitmap obstack. */ | |
1734 | bitmap_obstack_initialize (NULL); | |
1735 | ||
1736 | /* Initialize the RTL code for the function. */ | |
9c8305f8 | 1737 | current_function_decl = decl; |
0878843f | 1738 | saved_loc = input_location; |
9c8305f8 JH |
1739 | input_location = DECL_SOURCE_LOCATION (decl); |
1740 | init_function_start (decl); | |
0878843f RG |
1741 | |
1742 | gimple_register_cfg_hooks (); | |
1743 | ||
1744 | bitmap_obstack_initialize (®_obstack); /* FIXME, only at RTL generation*/ | |
1745 | ||
1746 | execute_all_ipa_transforms (); | |
1747 | ||
1748 | /* Perform all tree transforms and optimizations. */ | |
1749 | ||
1750 | /* Signal the start of passes. */ | |
1751 | invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL); | |
1752 | ||
315f8c0e | 1753 | execute_pass_list (g->get_passes ()->all_passes); |
0878843f RG |
1754 | |
1755 | /* Signal the end of passes. */ | |
1756 | invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL); | |
1757 | ||
1758 | bitmap_obstack_release (®_obstack); | |
1759 | ||
1760 | /* Release the default bitmap obstack. */ | |
1761 | bitmap_obstack_release (NULL); | |
1762 | ||
0878843f RG |
1763 | /* If requested, warn about function definitions where the function will |
1764 | return a value (usually of some struct or union type) which itself will | |
1765 | take up a lot of stack space. */ | |
9c8305f8 | 1766 | if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl)) |
0878843f | 1767 | { |
9c8305f8 | 1768 | tree ret_type = TREE_TYPE (TREE_TYPE (decl)); |
0878843f RG |
1769 | |
1770 | if (ret_type && TYPE_SIZE_UNIT (ret_type) | |
1771 | && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST | |
1772 | && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type), | |
1773 | larger_than_size)) | |
1774 | { | |
1775 | unsigned int size_as_int | |
1776 | = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type)); | |
1777 | ||
1778 | if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0) | |
1779 | warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes", | |
9c8305f8 | 1780 | decl, size_as_int); |
0878843f RG |
1781 | else |
1782 | warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes", | |
9c8305f8 | 1783 | decl, larger_than_size); |
0878843f RG |
1784 | } |
1785 | } | |
1786 | ||
9c8305f8 JH |
1787 | gimple_set_body (decl, NULL); |
1788 | if (DECL_STRUCT_FUNCTION (decl) == 0 | |
1789 | && !cgraph_get_node (decl)->origin) | |
0878843f RG |
1790 | { |
1791 | /* Stop pointing to the local nodes about to be freed. | |
1792 | But DECL_INITIAL must remain nonzero so we know this | |
1793 | was an actual function definition. | |
1794 | For a nested function, this is done in c_pop_function_context. | |
1795 | If rest_of_compilation set this to 0, leave it 0. */ | |
9c8305f8 JH |
1796 | if (DECL_INITIAL (decl) != 0) |
1797 | DECL_INITIAL (decl) = error_mark_node; | |
0878843f RG |
1798 | } |
1799 | ||
1800 | input_location = saved_loc; | |
1801 | ||
1802 | ggc_collect (); | |
1803 | timevar_pop (TV_REST_OF_COMPILATION); | |
5806d9ac JH |
1804 | |
1805 | /* Make sure that BE didn't give up on compiling. */ | |
1806 | gcc_assert (TREE_ASM_WRITTEN (decl)); | |
af16bc76 | 1807 | set_cfun (NULL); |
5806d9ac | 1808 | current_function_decl = NULL; |
1bb7e8f8 JH |
1809 | |
1810 | /* It would make a lot more sense to output thunks before function body to get more | |
65d630d4 | 1811 | forward and lest backwarding jumps. This however would need solving problem |
1bb7e8f8 | 1812 | with comdats. See PR48668. Also aliases must come after function itself to |
65d630d4 | 1813 | make one pass assemblers, like one on AIX, happy. See PR 50689. |
1bb7e8f8 JH |
1814 | FIXME: Perhaps thunks should be move before function IFF they are not in comdat |
1815 | groups. */ | |
1816 | assemble_thunks_and_aliases (node); | |
39ecc018 JH |
1817 | cgraph_release_function_body (node); |
1818 | /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer | |
1819 | points to the dead function body. */ | |
1820 | cgraph_node_remove_callees (node); | |
67348ccc | 1821 | ipa_remove_all_references (&node->ref_list); |
1c4a429a JH |
1822 | } |
1823 | ||
6674a6ce | 1824 | |
db0e878d AJ |
1825 | /* Expand all functions that must be output. |
1826 | ||
b58b1157 JH |
1827 | Attempt to topologically sort the nodes so function is output when |
1828 | all called functions are already assembled to allow data to be | |
a98ebe2e | 1829 | propagated across the callgraph. Use a stack to get smaller distance |
d1a6adeb | 1830 | between a function and its callees (later we may choose to use a more |
b58b1157 JH |
1831 | sophisticated algorithm for function reordering; we will likely want |
1832 | to use subsections to make the output functions appear in top-down | |
1833 | order). */ | |
1834 | ||
1835 | static void | |
65d630d4 | 1836 | expand_all_functions (void) |
b58b1157 JH |
1837 | { |
1838 | struct cgraph_node *node; | |
5ed6ace5 | 1839 | struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes); |
f30cfcb1 | 1840 | int order_pos, new_order_pos = 0; |
b58b1157 JH |
1841 | int i; |
1842 | ||
af8bca3c | 1843 | order_pos = ipa_reverse_postorder (order); |
341c100f | 1844 | gcc_assert (order_pos == cgraph_n_nodes); |
b58b1157 | 1845 | |
1ae58c30 | 1846 | /* Garbage collector may remove inline clones we eliminate during |
18c6ada9 JH |
1847 | optimization. So we must be sure to not reference them. */ |
1848 | for (i = 0; i < order_pos; i++) | |
257eb6e3 | 1849 | if (order[i]->process) |
18c6ada9 JH |
1850 | order[new_order_pos++] = order[i]; |
1851 | ||
1852 | for (i = new_order_pos - 1; i >= 0; i--) | |
b58b1157 JH |
1853 | { |
1854 | node = order[i]; | |
257eb6e3 | 1855 | if (node->process) |
b58b1157 | 1856 | { |
257eb6e3 | 1857 | node->process = 0; |
65d630d4 | 1858 | expand_function (node); |
b58b1157 JH |
1859 | } |
1860 | } | |
f45e0ad1 | 1861 | cgraph_process_new_functions (); |
50674e96 | 1862 | |
b58b1157 | 1863 | free (order); |
50674e96 | 1864 | |
b58b1157 JH |
1865 | } |
1866 | ||
474eccc6 ILT |
1867 | /* This is used to sort the node types by the cgraph order number. */ |
1868 | ||
24b97832 ILT |
1869 | enum cgraph_order_sort_kind |
1870 | { | |
1871 | ORDER_UNDEFINED = 0, | |
1872 | ORDER_FUNCTION, | |
1873 | ORDER_VAR, | |
1874 | ORDER_ASM | |
1875 | }; | |
1876 | ||
474eccc6 ILT |
1877 | struct cgraph_order_sort |
1878 | { | |
24b97832 | 1879 | enum cgraph_order_sort_kind kind; |
474eccc6 ILT |
1880 | union |
1881 | { | |
1882 | struct cgraph_node *f; | |
8a4a83ed | 1883 | struct varpool_node *v; |
65d630d4 | 1884 | struct asm_node *a; |
474eccc6 ILT |
1885 | } u; |
1886 | }; | |
1887 | ||
1888 | /* Output all functions, variables, and asm statements in the order | |
1889 | according to their order fields, which is the order in which they | |
1890 | appeared in the file. This implements -fno-toplevel-reorder. In | |
1891 | this mode we may output functions and variables which don't really | |
1892 | need to be output. */ | |
1893 | ||
1894 | static void | |
65d630d4 | 1895 | output_in_order (void) |
474eccc6 ILT |
1896 | { |
1897 | int max; | |
474eccc6 ILT |
1898 | struct cgraph_order_sort *nodes; |
1899 | int i; | |
1900 | struct cgraph_node *pf; | |
8a4a83ed | 1901 | struct varpool_node *pv; |
65d630d4 | 1902 | struct asm_node *pa; |
474eccc6 | 1903 | |
2aae7680 | 1904 | max = symtab_order; |
33283dad | 1905 | nodes = XCNEWVEC (struct cgraph_order_sort, max); |
474eccc6 | 1906 | |
65c70e6b | 1907 | FOR_EACH_DEFINED_FUNCTION (pf) |
474eccc6 | 1908 | { |
67348ccc | 1909 | if (pf->process && !pf->thunk.thunk_p && !pf->alias) |
474eccc6 | 1910 | { |
67348ccc | 1911 | i = pf->order; |
474eccc6 ILT |
1912 | gcc_assert (nodes[i].kind == ORDER_UNDEFINED); |
1913 | nodes[i].kind = ORDER_FUNCTION; | |
1914 | nodes[i].u.f = pf; | |
1915 | } | |
1916 | } | |
1917 | ||
65c70e6b | 1918 | FOR_EACH_DEFINED_VARIABLE (pv) |
67348ccc | 1919 | if (!DECL_EXTERNAL (pv->decl)) |
6649df51 | 1920 | { |
67348ccc | 1921 | i = pv->order; |
6649df51 JH |
1922 | gcc_assert (nodes[i].kind == ORDER_UNDEFINED); |
1923 | nodes[i].kind = ORDER_VAR; | |
1924 | nodes[i].u.v = pv; | |
1925 | } | |
474eccc6 | 1926 | |
65d630d4 | 1927 | for (pa = asm_nodes; pa; pa = pa->next) |
474eccc6 ILT |
1928 | { |
1929 | i = pa->order; | |
1930 | gcc_assert (nodes[i].kind == ORDER_UNDEFINED); | |
1931 | nodes[i].kind = ORDER_ASM; | |
1932 | nodes[i].u.a = pa; | |
1933 | } | |
474eccc6 | 1934 | |
7386e3ee | 1935 | /* In toplevel reorder mode we output all statics; mark them as needed. */ |
7386e3ee | 1936 | |
7fece979 JJ |
1937 | for (i = 0; i < max; ++i) |
1938 | if (nodes[i].kind == ORDER_VAR) | |
1939 | varpool_finalize_named_section_flags (nodes[i].u.v); | |
1940 | ||
474eccc6 ILT |
1941 | for (i = 0; i < max; ++i) |
1942 | { | |
1943 | switch (nodes[i].kind) | |
1944 | { | |
1945 | case ORDER_FUNCTION: | |
257eb6e3 | 1946 | nodes[i].u.f->process = 0; |
65d630d4 | 1947 | expand_function (nodes[i].u.f); |
474eccc6 ILT |
1948 | break; |
1949 | ||
1950 | case ORDER_VAR: | |
8a4a83ed | 1951 | varpool_assemble_decl (nodes[i].u.v); |
474eccc6 ILT |
1952 | break; |
1953 | ||
1954 | case ORDER_ASM: | |
1955 | assemble_asm (nodes[i].u.a->asm_str); | |
1956 | break; | |
1957 | ||
1958 | case ORDER_UNDEFINED: | |
1959 | break; | |
1960 | ||
1961 | default: | |
1962 | gcc_unreachable (); | |
1963 | } | |
1964 | } | |
e7b9eb2c | 1965 | |
65d630d4 | 1966 | asm_nodes = NULL; |
33283dad | 1967 | free (nodes); |
474eccc6 ILT |
1968 | } |
1969 | ||
ef330312 PB |
1970 | static void |
1971 | ipa_passes (void) | |
1972 | { | |
315f8c0e DM |
1973 | gcc::pass_manager *passes = g->get_passes (); |
1974 | ||
db2960f4 | 1975 | set_cfun (NULL); |
04b201a2 | 1976 | current_function_decl = NULL; |
726a989a | 1977 | gimple_register_cfg_hooks (); |
ef330312 | 1978 | bitmap_obstack_initialize (NULL); |
b20996ff | 1979 | |
090fa0ab GF |
1980 | invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL); |
1981 | ||
b20996ff | 1982 | if (!in_lto_p) |
0430f80c | 1983 | { |
315f8c0e | 1984 | execute_ipa_pass_list (passes->all_small_ipa_passes); |
0430f80c RG |
1985 | if (seen_error ()) |
1986 | return; | |
1987 | } | |
3baf459d | 1988 | |
467a8db0 JH |
1989 | /* We never run removal of unreachable nodes after early passes. This is |
1990 | because TODO is run before the subpasses. It is important to remove | |
1991 | the unreachable functions to save works at IPA level and to get LTO | |
1992 | symbol tables right. */ | |
04142cc3 | 1993 | symtab_remove_unreachable_nodes (true, cgraph_dump_file); |
467a8db0 | 1994 | |
d7f09764 DN |
1995 | /* If pass_all_early_optimizations was not scheduled, the state of |
1996 | the cgraph will not be properly updated. Update it now. */ | |
1997 | if (cgraph_state < CGRAPH_STATE_IPA_SSA) | |
1998 | cgraph_state = CGRAPH_STATE_IPA_SSA; | |
3baf459d | 1999 | |
d7f09764 DN |
2000 | if (!in_lto_p) |
2001 | { | |
2002 | /* Generate coverage variables and constructors. */ | |
2003 | coverage_finish (); | |
2004 | ||
2005 | /* Process new functions added. */ | |
2006 | set_cfun (NULL); | |
2007 | current_function_decl = NULL; | |
2008 | cgraph_process_new_functions (); | |
d7f09764 | 2009 | |
090fa0ab | 2010 | execute_ipa_summary_passes |
315f8c0e | 2011 | ((struct ipa_opt_pass_d *) passes->all_regular_ipa_passes); |
fb3f88cc | 2012 | } |
c082f9f3 SB |
2013 | |
2014 | /* Some targets need to handle LTO assembler output specially. */ | |
2015 | if (flag_generate_lto) | |
2016 | targetm.asm_out.lto_start (); | |
2017 | ||
315f8c0e DM |
2018 | execute_ipa_summary_passes ((struct ipa_opt_pass_d *) |
2019 | passes->all_lto_gen_passes); | |
d7f09764 DN |
2020 | |
2021 | if (!in_lto_p) | |
2022 | ipa_write_summaries (); | |
2023 | ||
c082f9f3 SB |
2024 | if (flag_generate_lto) |
2025 | targetm.asm_out.lto_end (); | |
2026 | ||
cc8547a7 | 2027 | if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects)) |
315f8c0e | 2028 | execute_ipa_pass_list (passes->all_regular_ipa_passes); |
090fa0ab | 2029 | invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL); |
3baf459d | 2030 | |
ef330312 PB |
2031 | bitmap_obstack_release (NULL); |
2032 | } | |
2033 | ||
25e2c40d JH |
2034 | |
2035 | /* Return string alias is alias of. */ | |
2036 | ||
2037 | static tree | |
2038 | get_alias_symbol (tree decl) | |
2039 | { | |
2040 | tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)); | |
2041 | return get_identifier (TREE_STRING_POINTER | |
2042 | (TREE_VALUE (TREE_VALUE (alias)))); | |
2043 | } | |
2044 | ||
2045 | ||
c9552bff | 2046 | /* Weakrefs may be associated to external decls and thus not output |
073a8998 | 2047 | at expansion time. Emit all necessary aliases. */ |
c9552bff | 2048 | |
a66f86bb | 2049 | static void |
c9552bff JH |
2050 | output_weakrefs (void) |
2051 | { | |
5e20cdc9 | 2052 | symtab_node *node; |
40a7fe1e | 2053 | FOR_EACH_SYMBOL (node) |
67348ccc DM |
2054 | if (node->alias |
2055 | && !TREE_ASM_WRITTEN (node->decl) | |
2056 | && node->weakref) | |
40a7fe1e JH |
2057 | { |
2058 | tree target; | |
2059 | ||
2060 | /* Weakrefs are special by not requiring target definition in current | |
2061 | compilation unit. It is thus bit hard to work out what we want to | |
2062 | alias. | |
2063 | When alias target is defined, we need to fetch it from symtab reference, | |
2064 | otherwise it is pointed to by alias_target. */ | |
67348ccc DM |
2065 | if (node->alias_target) |
2066 | target = (DECL_P (node->alias_target) | |
2067 | ? DECL_ASSEMBLER_NAME (node->alias_target) | |
2068 | : node->alias_target); | |
2069 | else if (node->analyzed) | |
2070 | target = DECL_ASSEMBLER_NAME (symtab_alias_target (node)->decl); | |
40a7fe1e JH |
2071 | else |
2072 | { | |
2073 | gcc_unreachable (); | |
67348ccc | 2074 | target = get_alias_symbol (node->decl); |
40a7fe1e | 2075 | } |
67348ccc | 2076 | do_assemble_alias (node->decl, target); |
40a7fe1e | 2077 | } |
c9552bff JH |
2078 | } |
2079 | ||
9c8305f8 | 2080 | /* Initialize callgraph dump file. */ |
4537ec0c | 2081 | |
9b3e897d PB |
2082 | void |
2083 | init_cgraph (void) | |
2084 | { | |
a05541a9 JH |
2085 | if (!cgraph_dump_file) |
2086 | cgraph_dump_file = dump_begin (TDI_cgraph, NULL); | |
9b3e897d | 2087 | } |
57fb5341 | 2088 | |
711417cd RG |
2089 | |
2090 | /* Perform simple optimizations based on callgraph. */ | |
2091 | ||
2092 | void | |
65d630d4 | 2093 | compile (void) |
711417cd RG |
2094 | { |
2095 | if (seen_error ()) | |
2096 | return; | |
2097 | ||
2098 | #ifdef ENABLE_CHECKING | |
474ffc72 | 2099 | verify_symtab (); |
711417cd RG |
2100 | #endif |
2101 | ||
711417cd RG |
2102 | timevar_push (TV_CGRAPHOPT); |
2103 | if (pre_ipa_mem_report) | |
2104 | { | |
2105 | fprintf (stderr, "Memory consumption before IPA\n"); | |
2106 | dump_memory_report (false); | |
2107 | } | |
2108 | if (!quiet_flag) | |
2109 | fprintf (stderr, "Performing interprocedural optimizations\n"); | |
2110 | cgraph_state = CGRAPH_STATE_IPA; | |
2111 | ||
65d630d4 JH |
2112 | /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */ |
2113 | if (flag_lto) | |
2114 | lto_streamer_hooks_init (); | |
2115 | ||
711417cd RG |
2116 | /* Don't run the IPA passes if there was any error or sorry messages. */ |
2117 | if (!seen_error ()) | |
2118 | ipa_passes (); | |
2119 | ||
2120 | /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */ | |
2121 | if (seen_error () | |
2122 | || (!in_lto_p && flag_lto && !flag_fat_lto_objects)) | |
2123 | { | |
2124 | timevar_pop (TV_CGRAPHOPT); | |
2125 | return; | |
2126 | } | |
2127 | ||
2128 | /* This pass remove bodies of extern inline functions we never inlined. | |
2129 | Do this later so other IPA passes see what is really going on. */ | |
04142cc3 | 2130 | symtab_remove_unreachable_nodes (false, dump_file); |
711417cd RG |
2131 | cgraph_global_info_ready = true; |
2132 | if (cgraph_dump_file) | |
2133 | { | |
2134 | fprintf (cgraph_dump_file, "Optimized "); | |
8f940ee6 | 2135 | dump_symtab (cgraph_dump_file); |
711417cd RG |
2136 | } |
2137 | if (post_ipa_mem_report) | |
2138 | { | |
2139 | fprintf (stderr, "Memory consumption after IPA\n"); | |
2140 | dump_memory_report (false); | |
2141 | } | |
2142 | timevar_pop (TV_CGRAPHOPT); | |
2143 | ||
2144 | /* Output everything. */ | |
2145 | (*debug_hooks->assembly_start) (); | |
2146 | if (!quiet_flag) | |
2147 | fprintf (stderr, "Assembling functions:\n"); | |
2148 | #ifdef ENABLE_CHECKING | |
474ffc72 | 2149 | verify_symtab (); |
711417cd RG |
2150 | #endif |
2151 | ||
2152 | cgraph_materialize_all_clones (); | |
2153 | bitmap_obstack_initialize (NULL); | |
315f8c0e | 2154 | execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes); |
04142cc3 | 2155 | symtab_remove_unreachable_nodes (true, dump_file); |
711417cd | 2156 | #ifdef ENABLE_CHECKING |
474ffc72 | 2157 | verify_symtab (); |
711417cd RG |
2158 | #endif |
2159 | bitmap_obstack_release (NULL); | |
65d630d4 | 2160 | mark_functions_to_output (); |
711417cd | 2161 | |
38e55e5c JH |
2162 | /* When weakref support is missing, we autmatically translate all |
2163 | references to NODE to references to its ultimate alias target. | |
2164 | The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and | |
2165 | TREE_CHAIN. | |
2166 | ||
2167 | Set up this mapping before we output any assembler but once we are sure | |
2168 | that all symbol renaming is done. | |
2169 | ||
2170 | FIXME: All this uglyness can go away if we just do renaming at gimple | |
2171 | level by physically rewritting the IL. At the moment we can only redirect | |
2172 | calls, so we need infrastructure for renaming references as well. */ | |
2173 | #ifndef ASM_OUTPUT_WEAKREF | |
5e20cdc9 | 2174 | symtab_node *node; |
38e55e5c JH |
2175 | |
2176 | FOR_EACH_SYMBOL (node) | |
67348ccc DM |
2177 | if (node->alias |
2178 | && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl))) | |
38e55e5c JH |
2179 | { |
2180 | IDENTIFIER_TRANSPARENT_ALIAS | |
67348ccc DM |
2181 | (DECL_ASSEMBLER_NAME (node->decl)) = 1; |
2182 | TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl)) | |
2183 | = (node->alias_target ? node->alias_target | |
2184 | : DECL_ASSEMBLER_NAME (symtab_alias_target (node)->decl)); | |
38e55e5c JH |
2185 | } |
2186 | #endif | |
2187 | ||
711417cd RG |
2188 | cgraph_state = CGRAPH_STATE_EXPANSION; |
2189 | if (!flag_toplevel_reorder) | |
65d630d4 | 2190 | output_in_order (); |
711417cd RG |
2191 | else |
2192 | { | |
65d630d4 | 2193 | output_asm_statements (); |
711417cd | 2194 | |
65d630d4 JH |
2195 | expand_all_functions (); |
2196 | varpool_output_variables (); | |
711417cd RG |
2197 | } |
2198 | ||
2199 | cgraph_process_new_functions (); | |
2200 | cgraph_state = CGRAPH_STATE_FINISHED; | |
07250f0e | 2201 | output_weakrefs (); |
711417cd RG |
2202 | |
2203 | if (cgraph_dump_file) | |
2204 | { | |
2205 | fprintf (cgraph_dump_file, "\nFinal "); | |
8f940ee6 | 2206 | dump_symtab (cgraph_dump_file); |
711417cd RG |
2207 | } |
2208 | #ifdef ENABLE_CHECKING | |
474ffc72 | 2209 | verify_symtab (); |
711417cd RG |
2210 | /* Double check that all inline clones are gone and that all |
2211 | function bodies have been released from memory. */ | |
2212 | if (!seen_error ()) | |
2213 | { | |
2214 | struct cgraph_node *node; | |
2215 | bool error_found = false; | |
2216 | ||
65c70e6b JH |
2217 | FOR_EACH_DEFINED_FUNCTION (node) |
2218 | if (node->global.inlined_to | |
67348ccc | 2219 | || gimple_has_body_p (node->decl)) |
711417cd RG |
2220 | { |
2221 | error_found = true; | |
2222 | dump_cgraph_node (stderr, node); | |
2223 | } | |
2224 | if (error_found) | |
2225 | internal_error ("nodes with unreleased memory found"); | |
2226 | } | |
2227 | #endif | |
2228 | } | |
2229 | ||
2230 | ||
2231 | /* Analyze the whole compilation unit once it is parsed completely. */ | |
2232 | ||
2233 | void | |
65d630d4 | 2234 | finalize_compilation_unit (void) |
711417cd RG |
2235 | { |
2236 | timevar_push (TV_CGRAPH); | |
2237 | ||
711417cd RG |
2238 | /* If we're here there's no current function anymore. Some frontends |
2239 | are lazy in clearing these. */ | |
2240 | current_function_decl = NULL; | |
2241 | set_cfun (NULL); | |
2242 | ||
2243 | /* Do not skip analyzing the functions if there were errors, we | |
2244 | miss diagnostics for following functions otherwise. */ | |
2245 | ||
2246 | /* Emit size functions we didn't inline. */ | |
2247 | finalize_size_functions (); | |
2248 | ||
2249 | /* Mark alias targets necessary and emit diagnostics. */ | |
711417cd RG |
2250 | handle_alias_pairs (); |
2251 | ||
2252 | if (!quiet_flag) | |
2253 | { | |
2254 | fprintf (stderr, "\nAnalyzing compilation unit\n"); | |
2255 | fflush (stderr); | |
2256 | } | |
2257 | ||
2258 | if (flag_dump_passes) | |
2259 | dump_passes (); | |
2260 | ||
2261 | /* Gimplify and lower all functions, compute reachability and | |
2262 | remove unreachable nodes. */ | |
e70670cf | 2263 | analyze_functions (); |
711417cd RG |
2264 | |
2265 | /* Mark alias targets necessary and emit diagnostics. */ | |
711417cd RG |
2266 | handle_alias_pairs (); |
2267 | ||
2268 | /* Gimplify and lower thunks. */ | |
e70670cf | 2269 | analyze_functions (); |
711417cd RG |
2270 | |
2271 | /* Finally drive the pass manager. */ | |
65d630d4 | 2272 | compile (); |
711417cd RG |
2273 | |
2274 | timevar_pop (TV_CGRAPH); | |
2275 | } | |
2276 | ||
2277 | ||
7be82279 | 2278 | #include "gt-cgraphunit.h" |