]>
Commit | Line | Data |
---|---|---|
9c8305f8 | 1 | /* Driver of optimization process |
818ab71a | 2 | Copyright (C) 2003-2016 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 | ||
3dafb85c | 28 | - finalize_function |
18c6ada9 JH |
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" | |
c7131fb2 | 163 | #include "backend.h" |
957060b5 AM |
164 | #include "target.h" |
165 | #include "rtl.h" | |
1c4a429a | 166 | #include "tree.h" |
c7131fb2 | 167 | #include "gimple.h" |
957060b5 AM |
168 | #include "cfghooks.h" |
169 | #include "regset.h" /* FIXME: For reg_obstack. */ | |
170 | #include "alloc-pool.h" | |
171 | #include "tree-pass.h" | |
172 | #include "stringpool.h" | |
173 | #include "gimple-ssa.h" | |
174 | #include "cgraph.h" | |
175 | #include "coverage.h" | |
176 | #include "lto-streamer.h" | |
40e23961 | 177 | #include "fold-const.h" |
d8a2d370 DN |
178 | #include "varasm.h" |
179 | #include "stor-layout.h" | |
0878843f | 180 | #include "output.h" |
e677a9d4 | 181 | #include "cfgcleanup.h" |
2fb9a547 | 182 | #include "gimple-fold.h" |
45b0be94 | 183 | #include "gimplify.h" |
5be5c238 | 184 | #include "gimple-iterator.h" |
18f429e2 | 185 | #include "gimplify-me.h" |
442b4905 AM |
186 | #include "tree-cfg.h" |
187 | #include "tree-into-ssa.h" | |
7a300452 | 188 | #include "tree-ssa.h" |
1c4a429a | 189 | #include "langhooks.h" |
1c4a429a | 190 | #include "toplev.h" |
1c4a429a | 191 | #include "debug.h" |
dd912cb8 | 192 | #include "symbol-summary.h" |
8bc5448f | 193 | #include "tree-vrp.h" |
57fb5341 | 194 | #include "ipa-prop.h" |
9c8305f8 | 195 | #include "gimple-pretty-print.h" |
090fa0ab | 196 | #include "plugin.h" |
632b4f8e | 197 | #include "ipa-inline.h" |
af8bca3c | 198 | #include "ipa-utils.h" |
452aa9c5 | 199 | #include "except.h" |
31ee20ba | 200 | #include "cfgloop.h" |
315f8c0e DM |
201 | #include "context.h" |
202 | #include "pass_manager.h" | |
1fe37220 | 203 | #include "tree-nested.h" |
2b5f0895 | 204 | #include "dbgcnt.h" |
d5e254e1 | 205 | #include "tree-chkp.h" |
1f6be682 | 206 | #include "lto-section-names.h" |
b58b1157 | 207 | |
66058468 JH |
208 | /* Queue of cgraph nodes scheduled to be added into cgraph. This is a |
209 | secondary queue used during optimization to accommodate passes that | |
210 | may generate new functions that need to be optimized and expanded. */ | |
31acf1bb | 211 | vec<cgraph_node *> cgraph_new_nodes; |
66058468 | 212 | |
65d630d4 JH |
213 | static void expand_all_functions (void); |
214 | static void mark_functions_to_output (void); | |
877ab5e9 | 215 | static void handle_alias_pairs (void); |
7dff32e6 | 216 | |
6744a6ab JH |
217 | /* Used for vtable lookup in thunk adjusting. */ |
218 | static GTY (()) tree vtable_entry_type; | |
219 | ||
3dafb85c | 220 | /* Determine if symbol declaration is needed. That is, visible to something |
edb983b2 JH |
221 | either outside this translation unit, something magic in the system |
222 | configury */ | |
223 | bool | |
3dafb85c | 224 | symtab_node::needed_p (void) |
8dafba3c | 225 | { |
ead84f73 JH |
226 | /* Double check that no one output the function into assembly file |
227 | early. */ | |
228 | gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl) | |
40a7fe1e | 229 | || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))); |
e7d6beb0 | 230 | |
3dafb85c | 231 | if (!definition) |
edb983b2 | 232 | return false; |
a1d31187 | 233 | |
edb983b2 JH |
234 | if (DECL_EXTERNAL (decl)) |
235 | return false; | |
04f77d0f | 236 | |
edb983b2 | 237 | /* If the user told us it is used, then it must be so. */ |
3dafb85c | 238 | if (force_output) |
edb983b2 JH |
239 | return true; |
240 | ||
241 | /* ABI forced symbols are needed when they are external. */ | |
3dafb85c | 242 | if (forced_by_abi && TREE_PUBLIC (decl)) |
edb983b2 JH |
243 | return true; |
244 | ||
1bf7c324 | 245 | /* Keep constructors, destructors and virtual functions. */ |
edb983b2 JH |
246 | if (TREE_CODE (decl) == FUNCTION_DECL |
247 | && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl))) | |
248 | return true; | |
249 | ||
250 | /* Externally visible variables must be output. The exception is | |
251 | COMDAT variables that must be output only when they are needed. */ | |
252 | if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl)) | |
8dafba3c RH |
253 | return true; |
254 | ||
8dafba3c RH |
255 | return false; |
256 | } | |
257 | ||
b4d05578 BS |
258 | /* Head and terminator of the queue of nodes to be processed while building |
259 | callgraph. */ | |
66058468 | 260 | |
b4d05578 BS |
261 | static symtab_node symtab_terminator; |
262 | static symtab_node *queued_nodes = &symtab_terminator; | |
66058468 | 263 | |
b4d05578 | 264 | /* Add NODE to queue starting at QUEUED_NODES. |
66058468 JH |
265 | The queue is linked via AUX pointers and terminated by pointer to 1. */ |
266 | ||
267 | static void | |
5e20cdc9 | 268 | enqueue_node (symtab_node *node) |
66058468 | 269 | { |
67348ccc | 270 | if (node->aux) |
66058468 | 271 | return; |
b4d05578 BS |
272 | gcc_checking_assert (queued_nodes); |
273 | node->aux = queued_nodes; | |
274 | queued_nodes = node; | |
66058468 JH |
275 | } |
276 | ||
d60ab196 | 277 | /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these |
f45e0ad1 JH |
278 | functions into callgraph in a way so they look like ordinary reachable |
279 | functions inserted into callgraph already at construction time. */ | |
280 | ||
b4d05578 | 281 | void |
3dafb85c | 282 | symbol_table::process_new_functions (void) |
f45e0ad1 | 283 | { |
f45e0ad1 | 284 | tree fndecl; |
f45e0ad1 | 285 | |
31acf1bb | 286 | if (!cgraph_new_nodes.exists ()) |
b4d05578 | 287 | return; |
31acf1bb | 288 | |
877ab5e9 | 289 | handle_alias_pairs (); |
f45e0ad1 JH |
290 | /* Note that this queue may grow as its being processed, as the new |
291 | functions may generate new ones. */ | |
31acf1bb | 292 | for (unsigned i = 0; i < cgraph_new_nodes.length (); i++) |
f45e0ad1 | 293 | { |
31acf1bb | 294 | cgraph_node *node = cgraph_new_nodes[i]; |
67348ccc | 295 | fndecl = node->decl; |
3dafb85c | 296 | switch (state) |
f45e0ad1 | 297 | { |
3dafb85c | 298 | case CONSTRUCTION: |
f45e0ad1 JH |
299 | /* At construction time we just need to finalize function and move |
300 | it into reachable functions list. */ | |
301 | ||
3dafb85c ML |
302 | cgraph_node::finalize_function (fndecl, false); |
303 | call_cgraph_insertion_hooks (node); | |
67348ccc | 304 | enqueue_node (node); |
f45e0ad1 JH |
305 | break; |
306 | ||
3dafb85c ML |
307 | case IPA: |
308 | case IPA_SSA: | |
17e0fc92 | 309 | case IPA_SSA_AFTER_INLINING: |
f45e0ad1 JH |
310 | /* When IPA optimization already started, do all essential |
311 | transformations that has been already performed on the whole | |
312 | cgraph but not on this function. */ | |
313 | ||
726a989a | 314 | gimple_register_cfg_hooks (); |
67348ccc | 315 | if (!node->analyzed) |
d52f5295 | 316 | node->analyze (); |
f45e0ad1 | 317 | push_cfun (DECL_STRUCT_FUNCTION (fndecl)); |
17e0fc92 | 318 | if ((state == IPA_SSA || state == IPA_SSA_AFTER_INLINING) |
7a388ee4 | 319 | && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl))) |
f7695dbf | 320 | g->get_passes ()->execute_early_local_passes (); |
9a1e784a | 321 | else if (inline_summaries != NULL) |
632b4f8e | 322 | compute_inline_parameters (node, true); |
f45e0ad1 JH |
323 | free_dominance_info (CDI_POST_DOMINATORS); |
324 | free_dominance_info (CDI_DOMINATORS); | |
325 | pop_cfun (); | |
82afdc6f | 326 | call_cgraph_insertion_hooks (node); |
f45e0ad1 JH |
327 | break; |
328 | ||
3dafb85c | 329 | case EXPANSION: |
f45e0ad1 JH |
330 | /* Functions created during expansion shall be compiled |
331 | directly. */ | |
257eb6e3 | 332 | node->process = 0; |
3dafb85c ML |
333 | call_cgraph_insertion_hooks (node); |
334 | node->expand (); | |
f45e0ad1 JH |
335 | break; |
336 | ||
337 | default: | |
338 | gcc_unreachable (); | |
339 | break; | |
340 | } | |
341 | } | |
31acf1bb ML |
342 | |
343 | cgraph_new_nodes.release (); | |
f45e0ad1 JH |
344 | } |
345 | ||
d71cc23f JH |
346 | /* As an GCC extension we allow redefinition of the function. The |
347 | semantics when both copies of bodies differ is not well defined. | |
348 | We replace the old body with new body so in unit at a time mode | |
349 | we always use new body, while in normal mode we may end up with | |
350 | old body inlined into some functions and new body expanded and | |
351 | inlined in others. | |
352 | ||
353 | ??? It may make more sense to use one body for inlining and other | |
354 | body for expanding the function but this is difficult to do. */ | |
355 | ||
e70670cf | 356 | void |
d52f5295 | 357 | cgraph_node::reset (void) |
d71cc23f | 358 | { |
d52f5295 | 359 | /* If process is set, then we have already begun whole-unit analysis. |
7e8b322a JH |
360 | This is *not* testing for whether we've already emitted the function. |
361 | That case can be sort-of legitimately seen with real function redefinition | |
362 | errors. I would argue that the front end should never present us with | |
363 | such a case, but don't enforce that for now. */ | |
d52f5295 | 364 | gcc_assert (!process); |
d71cc23f JH |
365 | |
366 | /* Reset our data structures so we can analyze the function again. */ | |
d52f5295 ML |
367 | memset (&local, 0, sizeof (local)); |
368 | memset (&global, 0, sizeof (global)); | |
369 | memset (&rtl, 0, sizeof (rtl)); | |
370 | analyzed = false; | |
371 | definition = false; | |
372 | alias = false; | |
71e54687 | 373 | transparent_alias = false; |
d52f5295 ML |
374 | weakref = false; |
375 | cpp_implicit_alias = false; | |
376 | ||
377 | remove_callees (); | |
378 | remove_all_references (); | |
d71cc23f | 379 | } |
d853a20e | 380 | |
d7438551 AH |
381 | /* Return true when there are references to the node. INCLUDE_SELF is |
382 | true if a self reference counts as a reference. */ | |
838ff415 | 383 | |
3dafb85c | 384 | bool |
d7438551 | 385 | symtab_node::referred_to_p (bool include_self) |
838ff415 | 386 | { |
3dafb85c | 387 | ipa_ref *ref = NULL; |
838ff415 | 388 | |
073a8998 | 389 | /* See if there are any references at all. */ |
3dafb85c | 390 | if (iterate_referring (0, ref)) |
838ff415 | 391 | return true; |
65d630d4 | 392 | /* For functions check also calls. */ |
3dafb85c | 393 | cgraph_node *cn = dyn_cast <cgraph_node *> (this); |
5d59b5e1 | 394 | if (cn && cn->callers) |
d7438551 AH |
395 | { |
396 | if (include_self) | |
397 | return true; | |
398 | for (cgraph_edge *e = cn->callers; e; e = e->next_caller) | |
399 | if (e->caller != this) | |
400 | return true; | |
401 | } | |
838ff415 JH |
402 | return false; |
403 | } | |
404 | ||
6b00c969 | 405 | /* DECL has been parsed. Take it, queue it, compile it at the whim of the |
15682f24 | 406 | logic in effect. If NO_COLLECT is true, then our caller cannot stand to have |
6b00c969 RH |
407 | the garbage collector run at the moment. We would need to either create |
408 | a new GC context, or just not compile right now. */ | |
1c4a429a JH |
409 | |
410 | void | |
3dafb85c | 411 | cgraph_node::finalize_function (tree decl, bool no_collect) |
1c4a429a | 412 | { |
3dafb85c | 413 | cgraph_node *node = cgraph_node::get_create (decl); |
1c4a429a | 414 | |
67348ccc | 415 | if (node->definition) |
b125ad45 | 416 | { |
15682f24 MJ |
417 | /* Nested functions should only be defined once. */ |
418 | gcc_assert (!DECL_CONTEXT (decl) | |
419 | || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL); | |
d52f5295 | 420 | node->reset (); |
b125ad45 JH |
421 | node->local.redefined_extern_inline = true; |
422 | } | |
6b00c969 | 423 | |
6a1e352e L |
424 | /* Set definition first before calling notice_global_symbol so that |
425 | it is available to notice_global_symbol. */ | |
67348ccc | 426 | node->definition = true; |
6a1e352e | 427 | notice_global_symbol (decl); |
e21aff8a | 428 | node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL; |
1c4a429a | 429 | |
ead84f73 JH |
430 | /* With -fkeep-inline-functions we are keeping all inline functions except |
431 | for extern inline ones. */ | |
432 | if (flag_keep_inline_functions | |
433 | && DECL_DECLARED_INLINE_P (decl) | |
434 | && !DECL_EXTERNAL (decl) | |
435 | && !DECL_DISREGARD_INLINE_LIMITS (decl)) | |
67348ccc | 436 | node->force_output = 1; |
8dafba3c | 437 | |
ead84f73 JH |
438 | /* When not optimizing, also output the static functions. (see |
439 | PR24561), but don't do so for always_inline functions, functions | |
440 | declared inline and nested functions. These were optimized out | |
441 | in the original implementation and it is unclear whether we want | |
442 | to change the behavior here. */ | |
787da5fd | 443 | if (((!opt_for_fn (decl, optimize) || flag_keep_static_functions) |
67348ccc | 444 | && !node->cpp_implicit_alias |
ead84f73 JH |
445 | && !DECL_DISREGARD_INLINE_LIMITS (decl) |
446 | && !DECL_DECLARED_INLINE_P (decl) | |
447 | && !(DECL_CONTEXT (decl) | |
448 | && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)) | |
449 | && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl)) | |
67348ccc | 450 | node->force_output = 1; |
ead84f73 | 451 | |
8dafba3c | 452 | /* If we've not yet emitted decl, tell the debug info about it. */ |
6b00c969 | 453 | if (!TREE_ASM_WRITTEN (decl)) |
8dafba3c | 454 | (*debug_hooks->deferred_inline_function) (decl); |
d173e685 | 455 | |
15682f24 | 456 | if (!no_collect) |
7e8b322a | 457 | ggc_collect (); |
838ff415 | 458 | |
3dafb85c ML |
459 | if (symtab->state == CONSTRUCTION |
460 | && (node->needed_p () || node->referred_to_p ())) | |
67348ccc | 461 | enqueue_node (node); |
1c4a429a JH |
462 | } |
463 | ||
452aa9c5 | 464 | /* Add the function FNDECL to the call graph. |
3dafb85c | 465 | Unlike finalize_function, this function is intended to be used |
452aa9c5 RG |
466 | by middle end and allows insertion of new function at arbitrary point |
467 | of compilation. The function can be either in high, low or SSA form | |
468 | GIMPLE. | |
469 | ||
470 | The function is assumed to be reachable and have address taken (so no | |
471 | API breaking optimizations are performed on it). | |
472 | ||
473 | Main work done by this function is to enqueue the function for later | |
474 | processing to avoid need the passes to be re-entrant. */ | |
475 | ||
476 | void | |
d52f5295 | 477 | cgraph_node::add_new_function (tree fndecl, bool lowered) |
452aa9c5 | 478 | { |
315f8c0e | 479 | gcc::pass_manager *passes = g->get_passes (); |
3dafb85c | 480 | cgraph_node *node; |
9452ef06 TV |
481 | |
482 | if (dump_file) | |
483 | { | |
484 | struct function *fn = DECL_STRUCT_FUNCTION (fndecl); | |
485 | const char *function_type = ((gimple_has_body_p (fndecl)) | |
486 | ? (lowered | |
487 | ? (gimple_in_ssa_p (fn) | |
488 | ? "ssa gimple" | |
489 | : "low gimple") | |
490 | : "high gimple") | |
491 | : "to-be-gimplified"); | |
492 | fprintf (dump_file, | |
493 | "Added new %s function %s to callgraph\n", | |
494 | function_type, | |
495 | fndecl_name (fndecl)); | |
496 | } | |
497 | ||
3dafb85c | 498 | switch (symtab->state) |
452aa9c5 | 499 | { |
3dafb85c ML |
500 | case PARSING: |
501 | cgraph_node::finalize_function (fndecl, false); | |
66058468 | 502 | break; |
3dafb85c | 503 | case CONSTRUCTION: |
452aa9c5 | 504 | /* Just enqueue function to be processed at nearest occurrence. */ |
d52f5295 | 505 | node = cgraph_node::get_create (fndecl); |
452aa9c5 RG |
506 | if (lowered) |
507 | node->lowered = true; | |
31acf1bb | 508 | cgraph_new_nodes.safe_push (node); |
452aa9c5 RG |
509 | break; |
510 | ||
3dafb85c ML |
511 | case IPA: |
512 | case IPA_SSA: | |
17e0fc92 | 513 | case IPA_SSA_AFTER_INLINING: |
3dafb85c | 514 | case EXPANSION: |
452aa9c5 RG |
515 | /* Bring the function into finalized state and enqueue for later |
516 | analyzing and compilation. */ | |
d52f5295 | 517 | node = cgraph_node::get_create (fndecl); |
452aa9c5 | 518 | node->local.local = false; |
67348ccc DM |
519 | node->definition = true; |
520 | node->force_output = true; | |
3dafb85c | 521 | if (!lowered && symtab->state == EXPANSION) |
452aa9c5 RG |
522 | { |
523 | push_cfun (DECL_STRUCT_FUNCTION (fndecl)); | |
452aa9c5 RG |
524 | gimple_register_cfg_hooks (); |
525 | bitmap_obstack_initialize (NULL); | |
2cbf2d95 | 526 | execute_pass_list (cfun, passes->all_lowering_passes); |
f7695dbf | 527 | passes->execute_early_local_passes (); |
452aa9c5 RG |
528 | bitmap_obstack_release (NULL); |
529 | pop_cfun (); | |
452aa9c5 RG |
530 | |
531 | lowered = true; | |
532 | } | |
533 | if (lowered) | |
534 | node->lowered = true; | |
31acf1bb | 535 | cgraph_new_nodes.safe_push (node); |
452aa9c5 RG |
536 | break; |
537 | ||
3dafb85c | 538 | case FINISHED: |
452aa9c5 RG |
539 | /* At the very end of compilation we have to do all the work up |
540 | to expansion. */ | |
d52f5295 | 541 | node = cgraph_node::create (fndecl); |
452aa9c5 RG |
542 | if (lowered) |
543 | node->lowered = true; | |
67348ccc | 544 | node->definition = true; |
d52f5295 | 545 | node->analyze (); |
452aa9c5 | 546 | push_cfun (DECL_STRUCT_FUNCTION (fndecl)); |
452aa9c5 RG |
547 | gimple_register_cfg_hooks (); |
548 | bitmap_obstack_initialize (NULL); | |
549 | if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl))) | |
f7695dbf | 550 | g->get_passes ()->execute_early_local_passes (); |
452aa9c5 | 551 | bitmap_obstack_release (NULL); |
452aa9c5 | 552 | pop_cfun (); |
3dafb85c | 553 | node->expand (); |
452aa9c5 RG |
554 | break; |
555 | ||
556 | default: | |
557 | gcc_unreachable (); | |
558 | } | |
559 | ||
560 | /* Set a personality if required and we already passed EH lowering. */ | |
561 | if (lowered | |
562 | && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl)) | |
563 | == eh_personality_lang)) | |
564 | DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality (); | |
565 | } | |
566 | ||
e767b5be | 567 | /* Analyze the function scheduled to be output. */ |
d52f5295 ML |
568 | void |
569 | cgraph_node::analyze (void) | |
e767b5be | 570 | { |
d52f5295 | 571 | tree decl = this->decl; |
9c8305f8 JH |
572 | location_t saved_loc = input_location; |
573 | input_location = DECL_SOURCE_LOCATION (decl); | |
e767b5be | 574 | |
d52f5295 | 575 | if (thunk.thunk_p) |
c47d0034 | 576 | { |
9641fab3 JH |
577 | cgraph_node *t = cgraph_node::get (thunk.alias); |
578 | ||
579 | create_edge (t, NULL, 0, CGRAPH_FREQ_BASE); | |
89bbe9ba | 580 | callees->can_throw_external = !TREE_NOTHROW (t->decl); |
9641fab3 JH |
581 | /* Target code in expand_thunk may need the thunk's target |
582 | to be analyzed, so recurse here. */ | |
583 | if (!t->analyzed) | |
584 | t->analyze (); | |
585 | if (t->alias) | |
586 | { | |
587 | t = t->get_alias_target (); | |
588 | if (!t->analyzed) | |
589 | t->analyze (); | |
590 | } | |
d52f5295 | 591 | if (!expand_thunk (false, false)) |
afdec9bd | 592 | { |
d52f5295 | 593 | thunk.alias = NULL; |
afdec9bd JH |
594 | return; |
595 | } | |
d52f5295 | 596 | thunk.alias = NULL; |
c47d0034 | 597 | } |
d52f5295 | 598 | if (alias) |
71e54687 | 599 | resolve_alias (cgraph_node::get (alias_target), transparent_alias); |
d52f5295 | 600 | else if (dispatcher_function) |
3649b9b7 ST |
601 | { |
602 | /* Generate the dispatcher body of multi-versioned functions. */ | |
3dafb85c | 603 | cgraph_function_version_info *dispatcher_version_info |
d52f5295 | 604 | = function_version (); |
3649b9b7 ST |
605 | if (dispatcher_version_info != NULL |
606 | && (dispatcher_version_info->dispatcher_resolver | |
607 | == NULL_TREE)) | |
608 | { | |
609 | tree resolver = NULL_TREE; | |
610 | gcc_assert (targetm.generate_version_dispatcher_body); | |
d52f5295 | 611 | resolver = targetm.generate_version_dispatcher_body (this); |
3649b9b7 ST |
612 | gcc_assert (resolver != NULL_TREE); |
613 | } | |
614 | } | |
c47d0034 JH |
615 | else |
616 | { | |
c47d0034 | 617 | push_cfun (DECL_STRUCT_FUNCTION (decl)); |
a406865a | 618 | |
d52f5295 | 619 | assign_assembler_name_if_neeeded (decl); |
0e0a1359 | 620 | |
c47d0034 JH |
621 | /* Make sure to gimplify bodies only once. During analyzing a |
622 | function we lower it, which will require gimplified nested | |
623 | functions, so we can end up here with an already gimplified | |
624 | body. */ | |
355a7673 | 625 | if (!gimple_has_body_p (decl)) |
c47d0034 | 626 | gimplify_function_tree (decl); |
a406865a | 627 | |
26eb69c6 | 628 | /* Lower the function. */ |
d52f5295 | 629 | if (!lowered) |
26eb69c6 | 630 | { |
d52f5295 ML |
631 | if (nested) |
632 | lower_nested_functions (decl); | |
633 | gcc_assert (!nested); | |
26eb69c6 RG |
634 | |
635 | gimple_register_cfg_hooks (); | |
636 | bitmap_obstack_initialize (NULL); | |
2cbf2d95 | 637 | execute_pass_list (cfun, g->get_passes ()->all_lowering_passes); |
26eb69c6 RG |
638 | free_dominance_info (CDI_POST_DOMINATORS); |
639 | free_dominance_info (CDI_DOMINATORS); | |
640 | compact_blocks (); | |
641 | bitmap_obstack_release (NULL); | |
d52f5295 | 642 | lowered = true; |
26eb69c6 RG |
643 | } |
644 | ||
c47d0034 JH |
645 | pop_cfun (); |
646 | } | |
d52f5295 | 647 | analyzed = true; |
e767b5be | 648 | |
9c8305f8 | 649 | input_location = saved_loc; |
e767b5be JH |
650 | } |
651 | ||
39e2db00 JH |
652 | /* C++ frontend produce same body aliases all over the place, even before PCH |
653 | gets streamed out. It relies on us linking the aliases with their function | |
654 | in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we | |
655 | first produce aliases without links, but once C++ FE is sure he won't sream | |
656 | PCH we build the links via this function. */ | |
657 | ||
658 | void | |
3dafb85c | 659 | symbol_table::process_same_body_aliases (void) |
39e2db00 | 660 | { |
5e20cdc9 | 661 | symtab_node *node; |
40a7fe1e | 662 | FOR_EACH_SYMBOL (node) |
67348ccc | 663 | if (node->cpp_implicit_alias && !node->analyzed) |
d52f5295 ML |
664 | node->resolve_alias |
665 | (TREE_CODE (node->alias_target) == VAR_DECL | |
9041d2e6 | 666 | ? (symtab_node *)varpool_node::get_create (node->alias_target) |
d52f5295 | 667 | : (symtab_node *)cgraph_node::get_create (node->alias_target)); |
40a7fe1e | 668 | cpp_implicit_aliases_done = true; |
39e2db00 JH |
669 | } |
670 | ||
768e3c60 RG |
671 | /* Process attributes common for vars and functions. */ |
672 | ||
673 | static void | |
7861b648 | 674 | process_common_attributes (symtab_node *node, tree decl) |
768e3c60 RG |
675 | { |
676 | tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)); | |
677 | ||
678 | if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl))) | |
679 | { | |
680 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes, | |
681 | "%<weakref%> attribute should be accompanied with" | |
682 | " an %<alias%> attribute"); | |
683 | DECL_WEAK (decl) = 0; | |
779d4b91 JH |
684 | DECL_ATTRIBUTES (decl) = remove_attribute ("weakref", |
685 | DECL_ATTRIBUTES (decl)); | |
768e3c60 | 686 | } |
7861b648 AK |
687 | |
688 | if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl))) | |
689 | node->no_reorder = 1; | |
768e3c60 RG |
690 | } |
691 | ||
386b46cf JH |
692 | /* Look for externally_visible and used attributes and mark cgraph nodes |
693 | accordingly. | |
694 | ||
695 | We cannot mark the nodes at the point the attributes are processed (in | |
696 | handle_*_attribute) because the copy of the declarations available at that | |
697 | point may not be canonical. For example, in: | |
698 | ||
699 | void f(); | |
700 | void f() __attribute__((used)); | |
701 | ||
702 | the declaration we see in handle_used_attribute will be the second | |
703 | declaration -- but the front end will subsequently merge that declaration | |
704 | with the original declaration and discard the second declaration. | |
705 | ||
3dafb85c | 706 | Furthermore, we can't mark these nodes in finalize_function because: |
386b46cf JH |
707 | |
708 | void f() {} | |
709 | void f() __attribute__((externally_visible)); | |
710 | ||
711 | is valid. | |
712 | ||
713 | So, we walk the nodes at the end of the translation unit, applying the | |
714 | attributes at that point. */ | |
715 | ||
716 | static void | |
3dafb85c | 717 | process_function_and_variable_attributes (cgraph_node *first, |
2c8326a5 | 718 | varpool_node *first_var) |
386b46cf | 719 | { |
3dafb85c | 720 | cgraph_node *node; |
2c8326a5 | 721 | varpool_node *vnode; |
386b46cf | 722 | |
3dafb85c ML |
723 | for (node = symtab->first_function (); node != first; |
724 | node = symtab->next_function (node)) | |
386b46cf | 725 | { |
67348ccc | 726 | tree decl = node->decl; |
b42186f1 | 727 | if (DECL_PRESERVE_P (decl)) |
d52f5295 | 728 | node->mark_force_output (); |
9d602c59 | 729 | else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl))) |
386b46cf | 730 | { |
67348ccc DM |
731 | if (! TREE_PUBLIC (node->decl)) |
732 | warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes, | |
c5d75364 MLI |
733 | "%<externally_visible%>" |
734 | " attribute have effect only on public objects"); | |
386b46cf | 735 | } |
779d4b91 | 736 | if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)) |
67348ccc | 737 | && (node->definition && !node->alias)) |
779d4b91 | 738 | { |
67348ccc | 739 | warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes, |
779d4b91 JH |
740 | "%<weakref%> attribute ignored" |
741 | " because function is defined"); | |
742 | DECL_WEAK (decl) = 0; | |
743 | DECL_ATTRIBUTES (decl) = remove_attribute ("weakref", | |
744 | DECL_ATTRIBUTES (decl)); | |
745 | } | |
c9fc06dc CB |
746 | |
747 | if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl)) | |
748 | && !DECL_DECLARED_INLINE_P (decl) | |
749 | /* redefining extern inline function makes it DECL_UNINLINABLE. */ | |
750 | && !DECL_UNINLINABLE (decl)) | |
751 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes, | |
752 | "always_inline function might not be inlinable"); | |
753 | ||
7861b648 | 754 | process_common_attributes (node, decl); |
386b46cf | 755 | } |
3dafb85c ML |
756 | for (vnode = symtab->first_variable (); vnode != first_var; |
757 | vnode = symtab->next_variable (vnode)) | |
386b46cf | 758 | { |
67348ccc | 759 | tree decl = vnode->decl; |
6649df51 | 760 | if (DECL_EXTERNAL (decl) |
6a6dac52 | 761 | && DECL_INITIAL (decl)) |
9041d2e6 | 762 | varpool_node::finalize_decl (decl); |
b42186f1 | 763 | if (DECL_PRESERVE_P (decl)) |
67348ccc | 764 | vnode->force_output = true; |
9d602c59 | 765 | else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl))) |
386b46cf | 766 | { |
67348ccc DM |
767 | if (! TREE_PUBLIC (vnode->decl)) |
768 | warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes, | |
c5d75364 MLI |
769 | "%<externally_visible%>" |
770 | " attribute have effect only on public objects"); | |
386b46cf | 771 | } |
779d4b91 | 772 | if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)) |
67348ccc | 773 | && vnode->definition |
779d4b91 JH |
774 | && DECL_INITIAL (decl)) |
775 | { | |
67348ccc | 776 | warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes, |
779d4b91 JH |
777 | "%<weakref%> attribute ignored" |
778 | " because variable is initialized"); | |
779 | DECL_WEAK (decl) = 0; | |
780 | DECL_ATTRIBUTES (decl) = remove_attribute ("weakref", | |
781 | DECL_ATTRIBUTES (decl)); | |
782 | } | |
7861b648 | 783 | process_common_attributes (vnode, decl); |
386b46cf JH |
784 | } |
785 | } | |
786 | ||
66058468 JH |
787 | /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the |
788 | middle end to output the variable to asm file, if needed or externally | |
789 | visible. */ | |
790 | ||
791 | void | |
9041d2e6 | 792 | varpool_node::finalize_decl (tree decl) |
66058468 | 793 | { |
9041d2e6 | 794 | varpool_node *node = varpool_node::get_create (decl); |
66058468 | 795 | |
387df871 | 796 | gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl)); |
66058468 | 797 | |
67348ccc | 798 | if (node->definition) |
66058468 | 799 | return; |
6a1e352e L |
800 | /* Set definition first before calling notice_global_symbol so that |
801 | it is available to notice_global_symbol. */ | |
67348ccc | 802 | node->definition = true; |
6a1e352e | 803 | notice_global_symbol (decl); |
66058468 JH |
804 | if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl) |
805 | /* Traditionally we do not eliminate static variables when not | |
806 | optimizing and when not doing toplevel reoder. */ | |
7861b648 AK |
807 | || node->no_reorder |
808 | || ((!flag_toplevel_reorder | |
809 | && !DECL_COMDAT (node->decl) | |
810 | && !DECL_ARTIFICIAL (node->decl)))) | |
67348ccc | 811 | node->force_output = true; |
66058468 | 812 | |
3dafb85c ML |
813 | if (symtab->state == CONSTRUCTION |
814 | && (node->needed_p () || node->referred_to_p ())) | |
67348ccc | 815 | enqueue_node (node); |
3dafb85c | 816 | if (symtab->state >= IPA_SSA) |
9041d2e6 | 817 | node->analyze (); |
0d6bf48c JH |
818 | /* Some frontends produce various interface variables after compilation |
819 | finished. */ | |
3dafb85c ML |
820 | if (symtab->state == FINISHED |
821 | || (!flag_toplevel_reorder | |
822 | && symtab->state == EXPANSION)) | |
9041d2e6 | 823 | node->assemble_decl (); |
d5e254e1 IE |
824 | |
825 | if (DECL_INITIAL (decl)) | |
826 | chkp_register_var_initializer (decl); | |
66058468 JH |
827 | } |
828 | ||
e18412fc JH |
829 | /* EDGE is an polymorphic call. Mark all possible targets as reachable |
830 | and if there is only one target, perform trivial devirtualization. | |
831 | REACHABLE_CALL_TARGETS collects target lists we already walked to | |
832 | avoid udplicate work. */ | |
833 | ||
834 | static void | |
6e2830c3 | 835 | walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets, |
3dafb85c | 836 | cgraph_edge *edge) |
e18412fc JH |
837 | { |
838 | unsigned int i; | |
839 | void *cache_token; | |
840 | bool final; | |
841 | vec <cgraph_node *>targets | |
842 | = possible_polymorphic_call_targets | |
843 | (edge, &final, &cache_token); | |
844 | ||
6e2830c3 | 845 | if (!reachable_call_targets->add (cache_token)) |
e18412fc | 846 | { |
3dafb85c | 847 | if (symtab->dump_file) |
e18412fc | 848 | dump_possible_polymorphic_call_targets |
3dafb85c | 849 | (symtab->dump_file, edge); |
e18412fc | 850 | |
c3284718 | 851 | for (i = 0; i < targets.length (); i++) |
e18412fc JH |
852 | { |
853 | /* Do not bother to mark virtual methods in anonymous namespace; | |
854 | either we will find use of virtual table defining it, or it is | |
855 | unused. */ | |
67348ccc | 856 | if (targets[i]->definition |
e18412fc | 857 | && TREE_CODE |
67348ccc | 858 | (TREE_TYPE (targets[i]->decl)) |
e18412fc JH |
859 | == METHOD_TYPE |
860 | && !type_in_anonymous_namespace_p | |
70e7f2a2 JH |
861 | (TYPE_METHOD_BASETYPE (TREE_TYPE (targets[i]->decl)))) |
862 | enqueue_node (targets[i]); | |
e18412fc JH |
863 | } |
864 | } | |
865 | ||
866 | /* Very trivial devirtualization; when the type is | |
867 | final or anonymous (so we know all its derivation) | |
868 | and there is only one possible virtual call target, | |
869 | make the edge direct. */ | |
870 | if (final) | |
871 | { | |
2b5f0895 | 872 | if (targets.length () <= 1 && dbg_cnt (devirt)) |
e18412fc | 873 | { |
3462aa02 JH |
874 | cgraph_node *target; |
875 | if (targets.length () == 1) | |
876 | target = targets[0]; | |
877 | else | |
d52f5295 ML |
878 | target = cgraph_node::create |
879 | (builtin_decl_implicit (BUILT_IN_UNREACHABLE)); | |
3462aa02 | 880 | |
3dafb85c | 881 | if (symtab->dump_file) |
e18412fc | 882 | { |
3dafb85c | 883 | fprintf (symtab->dump_file, |
e18412fc | 884 | "Devirtualizing call: "); |
3dafb85c | 885 | print_gimple_stmt (symtab->dump_file, |
e18412fc JH |
886 | edge->call_stmt, 0, |
887 | TDF_SLIM); | |
888 | } | |
2b5f0895 XDL |
889 | if (dump_enabled_p ()) |
890 | { | |
807b7d62 | 891 | location_t locus = gimple_location_safe (edge->call_stmt); |
2b5f0895 XDL |
892 | dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus, |
893 | "devirtualizing call in %s to %s\n", | |
894 | edge->caller->name (), target->name ()); | |
895 | } | |
896 | ||
3dafb85c ML |
897 | edge->make_direct (target); |
898 | edge->redirect_call_stmt_to_callee (); | |
d5e254e1 IE |
899 | |
900 | /* Call to __builtin_unreachable shouldn't be instrumented. */ | |
901 | if (!targets.length ()) | |
902 | gimple_call_set_with_bounds (edge->call_stmt, false); | |
903 | ||
3dafb85c | 904 | if (symtab->dump_file) |
e18412fc | 905 | { |
3dafb85c | 906 | fprintf (symtab->dump_file, |
e18412fc | 907 | "Devirtualized as: "); |
3dafb85c | 908 | print_gimple_stmt (symtab->dump_file, |
e18412fc JH |
909 | edge->call_stmt, 0, |
910 | TDF_SLIM); | |
911 | } | |
912 | } | |
913 | } | |
914 | } | |
915 | ||
4ec39494 MLI |
916 | /* Issue appropriate warnings for the global declaration DECL. */ |
917 | ||
918 | static void | |
919 | check_global_declaration (symtab_node *snode) | |
920 | { | |
261e741f | 921 | const char *decl_file; |
4ec39494 MLI |
922 | tree decl = snode->decl; |
923 | ||
924 | /* Warn about any function declared static but not defined. We don't | |
925 | warn about variables, because many programs have static variables | |
926 | that exist only to get some text into the object file. */ | |
927 | if (TREE_CODE (decl) == FUNCTION_DECL | |
928 | && DECL_INITIAL (decl) == 0 | |
929 | && DECL_EXTERNAL (decl) | |
930 | && ! DECL_ARTIFICIAL (decl) | |
931 | && ! TREE_NO_WARNING (decl) | |
932 | && ! TREE_PUBLIC (decl) | |
933 | && (warn_unused_function | |
934 | || snode->referred_to_p (/*include_self=*/false))) | |
935 | { | |
936 | if (snode->referred_to_p (/*include_self=*/false)) | |
937 | pedwarn (input_location, 0, "%q+F used but never defined", decl); | |
938 | else | |
939 | warning (OPT_Wunused_function, "%q+F declared %<static%> but never defined", decl); | |
940 | /* This symbol is effectively an "extern" declaration now. */ | |
941 | TREE_PUBLIC (decl) = 1; | |
942 | } | |
943 | ||
944 | /* Warn about static fns or vars defined but not used. */ | |
945 | if (((warn_unused_function && TREE_CODE (decl) == FUNCTION_DECL) | |
946 | || (((warn_unused_variable && ! TREE_READONLY (decl)) | |
4246c8da MW |
947 | || (warn_unused_const_variable > 0 && TREE_READONLY (decl) |
948 | && (warn_unused_const_variable == 2 | |
261e741f MW |
949 | || (main_input_filename != NULL |
950 | && (decl_file = DECL_SOURCE_FILE (decl)) != NULL | |
951 | && filename_cmp (main_input_filename, | |
952 | decl_file) == 0)))) | |
4ec39494 MLI |
953 | && TREE_CODE (decl) == VAR_DECL)) |
954 | && ! DECL_IN_SYSTEM_HEADER (decl) | |
955 | && ! snode->referred_to_p (/*include_self=*/false) | |
956 | /* This TREE_USED check is needed in addition to referred_to_p | |
957 | above, because the `__unused__' attribute is not being | |
958 | considered for referred_to_p. */ | |
959 | && ! TREE_USED (decl) | |
960 | /* The TREE_USED bit for file-scope decls is kept in the identifier, | |
961 | to handle multiple external decls in different scopes. */ | |
962 | && ! (DECL_NAME (decl) && TREE_USED (DECL_NAME (decl))) | |
963 | && ! DECL_EXTERNAL (decl) | |
964 | && ! DECL_ARTIFICIAL (decl) | |
965 | && ! DECL_ABSTRACT_ORIGIN (decl) | |
966 | && ! TREE_PUBLIC (decl) | |
967 | /* A volatile variable might be used in some non-obvious way. */ | |
8b6ab677 | 968 | && (! VAR_P (decl) || ! TREE_THIS_VOLATILE (decl)) |
4ec39494 MLI |
969 | /* Global register variables must be declared to reserve them. */ |
970 | && ! (TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)) | |
971 | /* Global ctors and dtors are called by the runtime. */ | |
972 | && (TREE_CODE (decl) != FUNCTION_DECL | |
973 | || (!DECL_STATIC_CONSTRUCTOR (decl) | |
974 | && !DECL_STATIC_DESTRUCTOR (decl))) | |
975 | /* Otherwise, ask the language. */ | |
976 | && lang_hooks.decls.warn_unused_global (decl)) | |
977 | warning_at (DECL_SOURCE_LOCATION (decl), | |
978 | (TREE_CODE (decl) == FUNCTION_DECL) | |
979 | ? OPT_Wunused_function | |
980 | : (TREE_READONLY (decl) | |
4246c8da | 981 | ? OPT_Wunused_const_variable_ |
4ec39494 MLI |
982 | : OPT_Wunused_variable), |
983 | "%qD defined but not used", decl); | |
984 | } | |
5d59b5e1 | 985 | |
66058468 JH |
986 | /* Discover all functions and variables that are trivially needed, analyze |
987 | them as well as all functions and variables referred by them */ | |
3edf64aa DM |
988 | static cgraph_node *first_analyzed; |
989 | static varpool_node *first_analyzed_var; | |
1c4a429a | 990 | |
d7438551 AH |
991 | /* FIRST_TIME is set to TRUE for the first time we are called for a |
992 | translation unit from finalize_compilation_unit() or false | |
993 | otherwise. */ | |
994 | ||
151e6f24 | 995 | static void |
d7438551 | 996 | analyze_functions (bool first_time) |
1c4a429a | 997 | { |
cd9c7bd2 | 998 | /* Keep track of already processed nodes when called multiple times for |
aabcd309 | 999 | intermodule optimization. */ |
3dafb85c | 1000 | cgraph_node *first_handled = first_analyzed; |
2c8326a5 | 1001 | varpool_node *first_handled_var = first_analyzed_var; |
6e2830c3 | 1002 | hash_set<void *> reachable_call_targets; |
66058468 | 1003 | |
5e20cdc9 DM |
1004 | symtab_node *node; |
1005 | symtab_node *next; | |
66058468 | 1006 | int i; |
3dafb85c | 1007 | ipa_ref *ref; |
66058468 | 1008 | bool changed = true; |
4f90d3e0 | 1009 | location_t saved_loc = input_location; |
1c4a429a | 1010 | |
1389294c | 1011 | bitmap_obstack_initialize (NULL); |
3dafb85c | 1012 | symtab->state = CONSTRUCTION; |
4f90d3e0 | 1013 | input_location = UNKNOWN_LOCATION; |
1c4a429a | 1014 | |
40a7fe1e JH |
1015 | /* Ugly, but the fixup can not happen at a time same body alias is created; |
1016 | C++ FE is confused about the COMDAT groups being right. */ | |
3dafb85c | 1017 | if (symtab->cpp_implicit_aliases_done) |
40a7fe1e | 1018 | FOR_EACH_SYMBOL (node) |
67348ccc | 1019 | if (node->cpp_implicit_alias) |
d52f5295 | 1020 | node->fixup_same_cpp_alias_visibility (node->get_alias_target ()); |
2bf86c84 | 1021 | build_type_inheritance_graph (); |
40a7fe1e | 1022 | |
66058468 JH |
1023 | /* Analysis adds static variables that in turn adds references to new functions. |
1024 | So we need to iterate the process until it stabilize. */ | |
1025 | while (changed) | |
1c4a429a | 1026 | { |
66058468 JH |
1027 | changed = false; |
1028 | process_function_and_variable_attributes (first_analyzed, | |
1029 | first_analyzed_var); | |
1030 | ||
1031 | /* First identify the trivially needed symbols. */ | |
3dafb85c | 1032 | for (node = symtab->first_symbol (); |
67348ccc DM |
1033 | node != first_analyzed |
1034 | && node != first_analyzed_var; node = node->next) | |
d71cc23f | 1035 | { |
d67ff7b7 JM |
1036 | /* Convert COMDAT group designators to IDENTIFIER_NODEs. */ |
1037 | node->get_comdat_group_id (); | |
3dafb85c | 1038 | if (node->needed_p ()) |
66058468 JH |
1039 | { |
1040 | enqueue_node (node); | |
3dafb85c ML |
1041 | if (!changed && symtab->dump_file) |
1042 | fprintf (symtab->dump_file, "Trivially needed symbols:"); | |
66058468 | 1043 | changed = true; |
3dafb85c ML |
1044 | if (symtab->dump_file) |
1045 | fprintf (symtab->dump_file, " %s", node->asm_name ()); | |
1046 | if (!changed && symtab->dump_file) | |
1047 | fprintf (symtab->dump_file, "\n"); | |
66058468 | 1048 | } |
67348ccc DM |
1049 | if (node == first_analyzed |
1050 | || node == first_analyzed_var) | |
66058468 | 1051 | break; |
d71cc23f | 1052 | } |
3dafb85c ML |
1053 | symtab->process_new_functions (); |
1054 | first_analyzed_var = symtab->first_variable (); | |
1055 | first_analyzed = symtab->first_function (); | |
cd4dea62 | 1056 | |
3dafb85c ML |
1057 | if (changed && symtab->dump_file) |
1058 | fprintf (symtab->dump_file, "\n"); | |
8dafba3c | 1059 | |
66058468 JH |
1060 | /* Lower representation, build callgraph edges and references for all trivially |
1061 | needed symbols and all symbols referred by them. */ | |
b4d05578 | 1062 | while (queued_nodes != &symtab_terminator) |
b66887e4 | 1063 | { |
66058468 | 1064 | changed = true; |
b4d05578 BS |
1065 | node = queued_nodes; |
1066 | queued_nodes = (symtab_node *)queued_nodes->aux; | |
7de90a6c | 1067 | cgraph_node *cnode = dyn_cast <cgraph_node *> (node); |
67348ccc | 1068 | if (cnode && cnode->definition) |
66058468 | 1069 | { |
3dafb85c | 1070 | cgraph_edge *edge; |
67348ccc | 1071 | tree decl = cnode->decl; |
66058468 | 1072 | |
5d59b5e1 LC |
1073 | /* ??? It is possible to create extern inline function |
1074 | and later using weak alias attribute to kill its body. | |
1075 | See gcc.c-torture/compile/20011119-1.c */ | |
66058468 | 1076 | if (!DECL_STRUCT_FUNCTION (decl) |
67348ccc | 1077 | && !cnode->alias |
3649b9b7 ST |
1078 | && !cnode->thunk.thunk_p |
1079 | && !cnode->dispatcher_function) | |
66058468 | 1080 | { |
d52f5295 | 1081 | cnode->reset (); |
66058468 JH |
1082 | cnode->local.redefined_extern_inline = true; |
1083 | continue; | |
1084 | } | |
b66887e4 | 1085 | |
67348ccc | 1086 | if (!cnode->analyzed) |
d52f5295 | 1087 | cnode->analyze (); |
6b20f353 | 1088 | |
66058468 | 1089 | for (edge = cnode->callees; edge; edge = edge->next_callee) |
1bf7c324 JH |
1090 | if (edge->callee->definition |
1091 | && (!DECL_EXTERNAL (edge->callee->decl) | |
1092 | /* When not optimizing, do not try to analyze extern | |
1093 | inline functions. Doing so is pointless. */ | |
1094 | || opt_for_fn (edge->callee->decl, optimize) | |
1095 | /* Weakrefs needs to be preserved. */ | |
1096 | || edge->callee->alias | |
1097 | /* always_inline functions are inlined aven at -O0. */ | |
1098 | || lookup_attribute | |
1099 | ("always_inline", | |
1100 | DECL_ATTRIBUTES (edge->callee->decl)) | |
1101 | /* Multiversioned functions needs the dispatcher to | |
1102 | be produced locally even for extern functions. */ | |
1103 | || edge->callee->function_version ())) | |
67348ccc | 1104 | enqueue_node (edge->callee); |
2bf86c84 JH |
1105 | if (opt_for_fn (cnode->decl, optimize) |
1106 | && opt_for_fn (cnode->decl, flag_devirtualize)) | |
eefe9a99 | 1107 | { |
3dafb85c | 1108 | cgraph_edge *next; |
e18412fc JH |
1109 | |
1110 | for (edge = cnode->indirect_calls; edge; edge = next) | |
c4be6568 JH |
1111 | { |
1112 | next = edge->next_callee; | |
1113 | if (edge->indirect_info->polymorphic) | |
6e2830c3 | 1114 | walk_polymorphic_call_targets (&reachable_call_targets, |
e18412fc | 1115 | edge); |
c4be6568 | 1116 | } |
eefe9a99 | 1117 | } |
66058468 | 1118 | |
5d59b5e1 LC |
1119 | /* If decl is a clone of an abstract function, |
1120 | mark that abstract function so that we don't release its body. | |
1121 | The DECL_INITIAL() of that abstract function declaration | |
1122 | will be later needed to output debug info. */ | |
66058468 JH |
1123 | if (DECL_ABSTRACT_ORIGIN (decl)) |
1124 | { | |
3dafb85c | 1125 | cgraph_node *origin_node |
c84495c0 | 1126 | = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl)); |
c0c123ef | 1127 | origin_node->used_as_abstract_origin = true; |
66058468 | 1128 | } |
66058468 | 1129 | } |
5d59b5e1 LC |
1130 | else |
1131 | { | |
7de90a6c | 1132 | varpool_node *vnode = dyn_cast <varpool_node *> (node); |
67348ccc | 1133 | if (vnode && vnode->definition && !vnode->analyzed) |
9041d2e6 | 1134 | vnode->analyze (); |
5d59b5e1 | 1135 | } |
66058468 | 1136 | |
67348ccc | 1137 | if (node->same_comdat_group) |
66058468 | 1138 | { |
5e20cdc9 | 1139 | symtab_node *next; |
67348ccc | 1140 | for (next = node->same_comdat_group; |
66058468 | 1141 | next != node; |
67348ccc | 1142 | next = next->same_comdat_group) |
1bf7c324 JH |
1143 | if (!next->comdat_local_p ()) |
1144 | enqueue_node (next); | |
66058468 | 1145 | } |
d122681a | 1146 | for (i = 0; node->iterate_reference (i, ref); i++) |
1bf7c324 JH |
1147 | if (ref->referred->definition |
1148 | && (!DECL_EXTERNAL (ref->referred->decl) | |
1149 | || ((TREE_CODE (ref->referred->decl) != FUNCTION_DECL | |
1150 | && optimize) | |
1151 | || (TREE_CODE (ref->referred->decl) == FUNCTION_DECL | |
1152 | && opt_for_fn (ref->referred->decl, optimize)) | |
38c1b72f | 1153 | || node->alias |
1bf7c324 | 1154 | || ref->referred->alias))) |
66058468 | 1155 | enqueue_node (ref->referred); |
3dafb85c | 1156 | symtab->process_new_functions (); |
66058468 | 1157 | } |
1c4a429a | 1158 | } |
2bf86c84 | 1159 | update_type_inheritance_graph (); |
8dafba3c | 1160 | |
564738df | 1161 | /* Collect entry points to the unit. */ |
3dafb85c | 1162 | if (symtab->dump_file) |
1668aabc | 1163 | { |
3dafb85c ML |
1164 | fprintf (symtab->dump_file, "\n\nInitial "); |
1165 | symtab_node::dump_table (symtab->dump_file); | |
1668aabc | 1166 | } |
7660e67e | 1167 | |
d7438551 AH |
1168 | if (first_time) |
1169 | { | |
1170 | symtab_node *snode; | |
1171 | FOR_EACH_SYMBOL (snode) | |
4ec39494 | 1172 | check_global_declaration (snode); |
d7438551 AH |
1173 | } |
1174 | ||
3dafb85c ML |
1175 | if (symtab->dump_file) |
1176 | fprintf (symtab->dump_file, "\nRemoving unused symbols:"); | |
1c4a429a | 1177 | |
3dafb85c | 1178 | for (node = symtab->first_symbol (); |
67348ccc DM |
1179 | node != first_handled |
1180 | && node != first_handled_var; node = next) | |
1c4a429a | 1181 | { |
67348ccc | 1182 | next = node->next; |
3dafb85c | 1183 | if (!node->aux && !node->referred_to_p ()) |
1c4a429a | 1184 | { |
3dafb85c ML |
1185 | if (symtab->dump_file) |
1186 | fprintf (symtab->dump_file, " %s", node->name ()); | |
d7438551 AH |
1187 | |
1188 | /* See if the debugger can use anything before the DECL | |
1189 | passes away. Perhaps it can notice a DECL that is now a | |
1190 | constant and can tag the early DIE with an appropriate | |
1191 | attribute. | |
1192 | ||
1193 | Otherwise, this is the last chance the debug_hooks have | |
1194 | at looking at optimized away DECLs, since | |
1195 | late_global_decl will subsequently be called from the | |
1196 | contents of the now pruned symbol table. */ | |
e6358ebd RB |
1197 | if (TREE_CODE (node->decl) == VAR_DECL |
1198 | && !decl_function_context (node->decl)) | |
1199 | { | |
1200 | /* We are reclaiming totally unreachable code and variables | |
1201 | so they effectively appear as readonly. Show that to | |
1202 | the debug machinery. */ | |
1203 | TREE_READONLY (node->decl) = 1; | |
1204 | (*debug_hooks->late_global_decl) (node->decl); | |
1205 | } | |
d7438551 | 1206 | |
d52f5295 | 1207 | node->remove (); |
d71cc23f | 1208 | continue; |
1c4a429a | 1209 | } |
7de90a6c | 1210 | if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node)) |
66058468 | 1211 | { |
67348ccc | 1212 | tree decl = node->decl; |
66058468 | 1213 | |
67348ccc DM |
1214 | if (cnode->definition && !gimple_has_body_p (decl) |
1215 | && !cnode->alias | |
66058468 | 1216 | && !cnode->thunk.thunk_p) |
d52f5295 | 1217 | cnode->reset (); |
66058468 | 1218 | |
67348ccc DM |
1219 | gcc_assert (!cnode->definition || cnode->thunk.thunk_p |
1220 | || cnode->alias | |
66058468 | 1221 | || gimple_has_body_p (decl)); |
67348ccc | 1222 | gcc_assert (cnode->analyzed == cnode->definition); |
66058468 | 1223 | } |
67348ccc | 1224 | node->aux = NULL; |
1c4a429a | 1225 | } |
67348ccc DM |
1226 | for (;node; node = node->next) |
1227 | node->aux = NULL; | |
3dafb85c ML |
1228 | first_analyzed = symtab->first_function (); |
1229 | first_analyzed_var = symtab->first_variable (); | |
1230 | if (symtab->dump_file) | |
7d82fe7c | 1231 | { |
3dafb85c ML |
1232 | fprintf (symtab->dump_file, "\n\nReclaimed "); |
1233 | symtab_node::dump_table (symtab->dump_file); | |
7d82fe7c | 1234 | } |
1389294c | 1235 | bitmap_obstack_release (NULL); |
1c4a429a | 1236 | ggc_collect (); |
d352b245 JH |
1237 | /* Initialize assembler name hash, in particular we want to trigger C++ |
1238 | mangling and same body alias creation before we free DECL_ARGUMENTS | |
1239 | used by it. */ | |
1240 | if (!seen_error ()) | |
3dafb85c | 1241 | symtab->symtab_initialize_asm_name_hash (); |
4f90d3e0 JH |
1242 | |
1243 | input_location = saved_loc; | |
151e6f24 JH |
1244 | } |
1245 | ||
85ce9375 JH |
1246 | /* Translate the ugly representation of aliases as alias pairs into nice |
1247 | representation in callgraph. We don't handle all cases yet, | |
4f219961 | 1248 | unfortunately. */ |
85ce9375 JH |
1249 | |
1250 | static void | |
1251 | handle_alias_pairs (void) | |
1252 | { | |
1253 | alias_pair *p; | |
1254 | unsigned i; | |
85ce9375 | 1255 | |
9771b263 | 1256 | for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);) |
85ce9375 | 1257 | { |
3dafb85c | 1258 | symtab_node *target_node = symtab_node::get_for_asmname (p->target); |
38e55ac9 | 1259 | |
4f219961 EB |
1260 | /* Weakrefs with target not defined in current unit are easy to handle: |
1261 | they behave just as external variables except we need to note the | |
1262 | alias flag to later output the weakref pseudo op into asm file. */ | |
1263 | if (!target_node | |
1264 | && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL) | |
25e2c40d | 1265 | { |
d52f5295 | 1266 | symtab_node *node = symtab_node::get (p->decl); |
40a7fe1e | 1267 | if (node) |
e01c7cca | 1268 | { |
67348ccc DM |
1269 | node->alias_target = p->target; |
1270 | node->weakref = true; | |
1271 | node->alias = true; | |
71e54687 | 1272 | node->transparent_alias = true; |
e01c7cca | 1273 | } |
9771b263 | 1274 | alias_pairs->unordered_remove (i); |
38e55ac9 | 1275 | continue; |
25e2c40d | 1276 | } |
38e55ac9 | 1277 | else if (!target_node) |
85ce9375 | 1278 | { |
38e55ac9 | 1279 | error ("%q+D aliased to undefined symbol %qE", p->decl, p->target); |
d52f5295 | 1280 | symtab_node *node = symtab_node::get (p->decl); |
4f219961 | 1281 | if (node) |
67348ccc | 1282 | node->alias = false; |
9771b263 | 1283 | alias_pairs->unordered_remove (i); |
38e55ac9 JH |
1284 | continue; |
1285 | } | |
1286 | ||
67348ccc | 1287 | if (DECL_EXTERNAL (target_node->decl) |
07250f0e JH |
1288 | /* We use local aliases for C++ thunks to force the tailcall |
1289 | to bind locally. This is a hack - to keep it working do | |
1290 | the following (which is not strictly correct). */ | |
abbb94e6 | 1291 | && (TREE_CODE (target_node->decl) != FUNCTION_DECL |
67348ccc | 1292 | || ! DECL_VIRTUAL_P (target_node->decl)) |
07250f0e JH |
1293 | && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl))) |
1294 | { | |
1295 | error ("%q+D aliased to external symbol %qE", | |
1296 | p->decl, p->target); | |
1297 | } | |
1298 | ||
38e55ac9 | 1299 | if (TREE_CODE (p->decl) == FUNCTION_DECL |
7de90a6c | 1300 | && target_node && is_a <cgraph_node *> (target_node)) |
38e55ac9 | 1301 | { |
3dafb85c | 1302 | cgraph_node *src_node = cgraph_node::get (p->decl); |
67348ccc | 1303 | if (src_node && src_node->definition) |
d52f5295 ML |
1304 | src_node->reset (); |
1305 | cgraph_node::create_alias (p->decl, target_node->decl); | |
9771b263 | 1306 | alias_pairs->unordered_remove (i); |
38e55ac9 JH |
1307 | } |
1308 | else if (TREE_CODE (p->decl) == VAR_DECL | |
7de90a6c | 1309 | && target_node && is_a <varpool_node *> (target_node)) |
38e55ac9 | 1310 | { |
9041d2e6 | 1311 | varpool_node::create_alias (p->decl, target_node->decl); |
9771b263 | 1312 | alias_pairs->unordered_remove (i); |
38e55ac9 JH |
1313 | } |
1314 | else | |
1315 | { | |
1316 | error ("%q+D alias in between function and variable is not supported", | |
1317 | p->decl); | |
1318 | warning (0, "%q+D aliased declaration", | |
67348ccc | 1319 | target_node->decl); |
9771b263 | 1320 | alias_pairs->unordered_remove (i); |
85ce9375 JH |
1321 | } |
1322 | } | |
9771b263 | 1323 | vec_free (alias_pairs); |
85ce9375 JH |
1324 | } |
1325 | ||
5f1a9ebb | 1326 | |
1c4a429a JH |
1327 | /* Figure out what functions we want to assemble. */ |
1328 | ||
1329 | static void | |
65d630d4 | 1330 | mark_functions_to_output (void) |
1c4a429a | 1331 | { |
b66887e4 | 1332 | bool check_same_comdat_groups = false; |
b2b29377 | 1333 | cgraph_node *node; |
b66887e4 | 1334 | |
b2b29377 MM |
1335 | if (flag_checking) |
1336 | FOR_EACH_FUNCTION (node) | |
1337 | gcc_assert (!node->process); | |
1c4a429a | 1338 | |
65c70e6b | 1339 | FOR_EACH_FUNCTION (node) |
1c4a429a | 1340 | { |
67348ccc | 1341 | tree decl = node->decl; |
c22cacf3 | 1342 | |
67348ccc | 1343 | gcc_assert (!node->process || node->same_comdat_group); |
b66887e4 JJ |
1344 | if (node->process) |
1345 | continue; | |
b58b1157 | 1346 | |
7660e67e SB |
1347 | /* We need to output all local functions that are used and not |
1348 | always inlined, as well as those that are reachable from | |
1349 | outside the current compilation unit. */ | |
67348ccc | 1350 | if (node->analyzed |
c47d0034 | 1351 | && !node->thunk.thunk_p |
67348ccc | 1352 | && !node->alias |
18c6ada9 | 1353 | && !node->global.inlined_to |
6de9cd9a | 1354 | && !TREE_ASM_WRITTEN (decl) |
1c4a429a | 1355 | && !DECL_EXTERNAL (decl)) |
b66887e4 JJ |
1356 | { |
1357 | node->process = 1; | |
67348ccc | 1358 | if (node->same_comdat_group) |
b66887e4 | 1359 | { |
3dafb85c | 1360 | cgraph_node *next; |
d52f5295 | 1361 | for (next = dyn_cast<cgraph_node *> (node->same_comdat_group); |
b66887e4 | 1362 | next != node; |
d52f5295 | 1363 | next = dyn_cast<cgraph_node *> (next->same_comdat_group)) |
1f26ac87 | 1364 | if (!next->thunk.thunk_p && !next->alias |
d52f5295 | 1365 | && !next->comdat_local_p ()) |
c47d0034 | 1366 | next->process = 1; |
b66887e4 JJ |
1367 | } |
1368 | } | |
67348ccc | 1369 | else if (node->same_comdat_group) |
b66887e4 | 1370 | { |
b2b29377 MM |
1371 | if (flag_checking) |
1372 | check_same_comdat_groups = true; | |
b66887e4 | 1373 | } |
341c100f | 1374 | else |
1a2caa7a NS |
1375 | { |
1376 | /* We should've reclaimed all functions that are not needed. */ | |
b2b29377 MM |
1377 | if (flag_checking |
1378 | && !node->global.inlined_to | |
39ecc018 | 1379 | && gimple_has_body_p (decl) |
a837268b JH |
1380 | /* FIXME: in ltrans unit when offline copy is outside partition but inline copies |
1381 | are inside partition, we can end up not removing the body since we no longer | |
1382 | have analyzed node pointing to it. */ | |
67348ccc DM |
1383 | && !node->in_other_partition |
1384 | && !node->alias | |
387df871 | 1385 | && !node->clones |
1a2caa7a NS |
1386 | && !DECL_EXTERNAL (decl)) |
1387 | { | |
d52f5295 | 1388 | node->debug (); |
1a2caa7a NS |
1389 | internal_error ("failed to reclaim unneeded function"); |
1390 | } | |
726a989a | 1391 | gcc_assert (node->global.inlined_to |
39ecc018 | 1392 | || !gimple_has_body_p (decl) |
67348ccc | 1393 | || node->in_other_partition |
6649df51 JH |
1394 | || node->clones |
1395 | || DECL_ARTIFICIAL (decl) | |
1a2caa7a NS |
1396 | || DECL_EXTERNAL (decl)); |
1397 | ||
1398 | } | |
c22cacf3 | 1399 | |
18d13f34 | 1400 | } |
b2b29377 | 1401 | if (flag_checking && check_same_comdat_groups) |
65c70e6b | 1402 | FOR_EACH_FUNCTION (node) |
67348ccc | 1403 | if (node->same_comdat_group && !node->process) |
b66887e4 | 1404 | { |
67348ccc | 1405 | tree decl = node->decl; |
b66887e4 JJ |
1406 | if (!node->global.inlined_to |
1407 | && gimple_has_body_p (decl) | |
7cbf224d RG |
1408 | /* FIXME: in an ltrans unit when the offline copy is outside a |
1409 | partition but inline copies are inside a partition, we can | |
1410 | end up not removing the body since we no longer have an | |
1411 | analyzed node pointing to it. */ | |
67348ccc | 1412 | && !node->in_other_partition |
07250f0e | 1413 | && !node->clones |
b66887e4 JJ |
1414 | && !DECL_EXTERNAL (decl)) |
1415 | { | |
d52f5295 | 1416 | node->debug (); |
7cbf224d RG |
1417 | internal_error ("failed to reclaim unneeded function in same " |
1418 | "comdat group"); | |
b66887e4 JJ |
1419 | } |
1420 | } | |
18d13f34 JH |
1421 | } |
1422 | ||
6744a6ab | 1423 | /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function |
3649b9b7 | 1424 | in lowered gimple form. IN_SSA is true if the gimple is in SSA. |
6744a6ab JH |
1425 | |
1426 | Set current_function_decl and cfun to newly constructed empty function body. | |
1427 | return basic block in the function body. */ | |
1428 | ||
3649b9b7 | 1429 | basic_block |
10881cff | 1430 | init_lowered_empty_function (tree decl, bool in_ssa, gcov_type count) |
6744a6ab JH |
1431 | { |
1432 | basic_block bb; | |
10881cff | 1433 | edge e; |
6744a6ab JH |
1434 | |
1435 | current_function_decl = decl; | |
1436 | allocate_struct_function (decl, false); | |
1437 | gimple_register_cfg_hooks (); | |
1438 | init_empty_tree_cfg (); | |
381cdae4 | 1439 | init_tree_ssa (cfun); |
3649b9b7 ST |
1440 | |
1441 | if (in_ssa) | |
1442 | { | |
3649b9b7 ST |
1443 | init_ssa_operands (cfun); |
1444 | cfun->gimple_df->in_ssa_p = true; | |
31ee20ba | 1445 | cfun->curr_properties |= PROP_ssa; |
3649b9b7 ST |
1446 | } |
1447 | ||
6744a6ab | 1448 | DECL_INITIAL (decl) = make_node (BLOCK); |
01771d43 | 1449 | BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl; |
6744a6ab JH |
1450 | |
1451 | DECL_SAVED_TREE (decl) = error_mark_node; | |
31ee20ba RB |
1452 | cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any |
1453 | | PROP_cfg | PROP_loops); | |
1454 | ||
766090c2 | 1455 | set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ()); |
31ee20ba RB |
1456 | init_loops_structure (cfun, loops_for_fn (cfun), 1); |
1457 | loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES; | |
6744a6ab JH |
1458 | |
1459 | /* Create BB for body of the function and connect it properly. */ | |
10881cff JH |
1460 | ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = count; |
1461 | ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency = REG_BR_PROB_BASE; | |
1462 | EXIT_BLOCK_PTR_FOR_FN (cfun)->count = count; | |
1463 | EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency = REG_BR_PROB_BASE; | |
c4d281b2 | 1464 | bb = create_basic_block (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun)); |
10881cff JH |
1465 | bb->count = count; |
1466 | bb->frequency = BB_FREQ_MAX; | |
1467 | e = make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU); | |
1468 | e->count = count; | |
1469 | e->probability = REG_BR_PROB_BASE; | |
1470 | e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0); | |
1471 | e->count = count; | |
1472 | e->probability = REG_BR_PROB_BASE; | |
fefa31b5 | 1473 | add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father); |
6744a6ab JH |
1474 | |
1475 | return bb; | |
1476 | } | |
1477 | ||
1478 | /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable | |
1479 | offset indicated by VIRTUAL_OFFSET, if that is | |
1480 | non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and | |
1481 | zero for a result adjusting thunk. */ | |
1482 | ||
6bbf39b7 | 1483 | tree |
6744a6ab JH |
1484 | thunk_adjust (gimple_stmt_iterator * bsi, |
1485 | tree ptr, bool this_adjusting, | |
1486 | HOST_WIDE_INT fixed_offset, tree virtual_offset) | |
1487 | { | |
538dd0b7 | 1488 | gassign *stmt; |
6744a6ab JH |
1489 | tree ret; |
1490 | ||
313333a6 RG |
1491 | if (this_adjusting |
1492 | && fixed_offset != 0) | |
6744a6ab | 1493 | { |
5d49b6a7 RG |
1494 | stmt = gimple_build_assign |
1495 | (ptr, fold_build_pointer_plus_hwi_loc (input_location, | |
1496 | ptr, | |
1497 | fixed_offset)); | |
6744a6ab JH |
1498 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); |
1499 | } | |
1500 | ||
1501 | /* If there's a virtual offset, look up that value in the vtable and | |
1502 | adjust the pointer again. */ | |
1503 | if (virtual_offset) | |
1504 | { | |
1505 | tree vtabletmp; | |
1506 | tree vtabletmp2; | |
1507 | tree vtabletmp3; | |
6744a6ab JH |
1508 | |
1509 | if (!vtable_entry_type) | |
1510 | { | |
1511 | tree vfunc_type = make_node (FUNCTION_TYPE); | |
1512 | TREE_TYPE (vfunc_type) = integer_type_node; | |
1513 | TYPE_ARG_TYPES (vfunc_type) = NULL_TREE; | |
1514 | layout_type (vfunc_type); | |
1515 | ||
1516 | vtable_entry_type = build_pointer_type (vfunc_type); | |
1517 | } | |
1518 | ||
1519 | vtabletmp = | |
7cc434a3 | 1520 | create_tmp_reg (build_pointer_type |
7d80ca1f | 1521 | (build_pointer_type (vtable_entry_type)), "vptr"); |
6744a6ab JH |
1522 | |
1523 | /* The vptr is always at offset zero in the object. */ | |
1524 | stmt = gimple_build_assign (vtabletmp, | |
1525 | build1 (NOP_EXPR, TREE_TYPE (vtabletmp), | |
1526 | ptr)); | |
1527 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); | |
6744a6ab JH |
1528 | |
1529 | /* Form the vtable address. */ | |
7cc434a3 | 1530 | vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)), |
7d80ca1f | 1531 | "vtableaddr"); |
6744a6ab | 1532 | stmt = gimple_build_assign (vtabletmp2, |
70f34814 | 1533 | build_simple_mem_ref (vtabletmp)); |
6744a6ab | 1534 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); |
6744a6ab JH |
1535 | |
1536 | /* Find the entry with the vcall offset. */ | |
1537 | stmt = gimple_build_assign (vtabletmp2, | |
5d49b6a7 RG |
1538 | fold_build_pointer_plus_loc (input_location, |
1539 | vtabletmp2, | |
1540 | virtual_offset)); | |
6744a6ab JH |
1541 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); |
1542 | ||
1543 | /* Get the offset itself. */ | |
7cc434a3 | 1544 | vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)), |
7d80ca1f | 1545 | "vcalloffset"); |
6744a6ab | 1546 | stmt = gimple_build_assign (vtabletmp3, |
70f34814 | 1547 | build_simple_mem_ref (vtabletmp2)); |
6744a6ab | 1548 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); |
6744a6ab | 1549 | |
6744a6ab | 1550 | /* Adjust the `this' pointer. */ |
0d82a1c8 RG |
1551 | ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3); |
1552 | ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false, | |
1553 | GSI_CONTINUE_LINKING); | |
6744a6ab JH |
1554 | } |
1555 | ||
313333a6 RG |
1556 | if (!this_adjusting |
1557 | && fixed_offset != 0) | |
6744a6ab JH |
1558 | /* Adjust the pointer by the constant. */ |
1559 | { | |
1560 | tree ptrtmp; | |
1561 | ||
1562 | if (TREE_CODE (ptr) == VAR_DECL) | |
1563 | ptrtmp = ptr; | |
1564 | else | |
1565 | { | |
7cc434a3 | 1566 | ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr"); |
6744a6ab JH |
1567 | stmt = gimple_build_assign (ptrtmp, ptr); |
1568 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); | |
6744a6ab | 1569 | } |
5d49b6a7 RG |
1570 | ptr = fold_build_pointer_plus_hwi_loc (input_location, |
1571 | ptrtmp, fixed_offset); | |
6744a6ab JH |
1572 | } |
1573 | ||
1574 | /* Emit the statement and gimplify the adjustment expression. */ | |
7cc434a3 | 1575 | ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this"); |
6744a6ab | 1576 | stmt = gimple_build_assign (ret, ptr); |
6744a6ab JH |
1577 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); |
1578 | ||
1579 | return ret; | |
1580 | } | |
1581 | ||
afdec9bd | 1582 | /* Expand thunk NODE to gimple if possible. |
d211e471 ML |
1583 | When FORCE_GIMPLE_THUNK is true, gimple thunk is created and |
1584 | no assembler is produced. | |
afdec9bd JH |
1585 | When OUTPUT_ASM_THUNK is true, also produce assembler for |
1586 | thunks that are not lowered. */ | |
6744a6ab | 1587 | |
afdec9bd | 1588 | bool |
d52f5295 | 1589 | cgraph_node::expand_thunk (bool output_asm_thunks, bool force_gimple_thunk) |
6744a6ab | 1590 | { |
d52f5295 ML |
1591 | bool this_adjusting = thunk.this_adjusting; |
1592 | HOST_WIDE_INT fixed_offset = thunk.fixed_offset; | |
1593 | HOST_WIDE_INT virtual_value = thunk.virtual_value; | |
6744a6ab | 1594 | tree virtual_offset = NULL; |
d52f5295 ML |
1595 | tree alias = callees->callee->decl; |
1596 | tree thunk_fndecl = decl; | |
bcb650cb JH |
1597 | tree a; |
1598 | ||
a115251c IE |
1599 | /* Instrumentation thunk is the same function with |
1600 | a different signature. Never need to expand it. */ | |
1601 | if (thunk.add_pointer_bounds_args) | |
1602 | return false; | |
d06865bf | 1603 | |
d211e471 | 1604 | if (!force_gimple_thunk && this_adjusting |
6744a6ab JH |
1605 | && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset, |
1606 | virtual_value, alias)) | |
1607 | { | |
1608 | const char *fnname; | |
1609 | tree fn_block; | |
4399cf59 | 1610 | tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl)); |
afdec9bd JH |
1611 | |
1612 | if (!output_asm_thunks) | |
77b7d74b MJ |
1613 | { |
1614 | analyzed = true; | |
1615 | return false; | |
1616 | } | |
afdec9bd JH |
1617 | |
1618 | if (in_lto_p) | |
70486010 | 1619 | get_untransformed_body (); |
afdec9bd | 1620 | a = DECL_ARGUMENTS (thunk_fndecl); |
6744a6ab | 1621 | |
afdec9bd JH |
1622 | current_function_decl = thunk_fndecl; |
1623 | ||
1624 | /* Ensure thunks are emitted in their correct sections. */ | |
9641fab3 JH |
1625 | resolve_unique_section (thunk_fndecl, 0, |
1626 | flag_function_sections); | |
afdec9bd | 1627 | |
6744a6ab JH |
1628 | DECL_RESULT (thunk_fndecl) |
1629 | = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl), | |
4399cf59 | 1630 | RESULT_DECL, 0, restype); |
afdec9bd | 1631 | DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl; |
15488554 | 1632 | fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl)); |
6744a6ab JH |
1633 | |
1634 | /* The back end expects DECL_INITIAL to contain a BLOCK, so we | |
1635 | create one. */ | |
1636 | fn_block = make_node (BLOCK); | |
1637 | BLOCK_VARS (fn_block) = a; | |
1638 | DECL_INITIAL (thunk_fndecl) = fn_block; | |
01771d43 | 1639 | BLOCK_SUPERCONTEXT (fn_block) = thunk_fndecl; |
be373510 | 1640 | allocate_struct_function (thunk_fndecl, false); |
6744a6ab JH |
1641 | init_function_start (thunk_fndecl); |
1642 | cfun->is_thunk = 1; | |
68a55980 JJ |
1643 | insn_locations_init (); |
1644 | set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl)); | |
1645 | prologue_location = curr_insn_location (); | |
6744a6ab JH |
1646 | assemble_start_function (thunk_fndecl, fnname); |
1647 | ||
1648 | targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl, | |
1649 | fixed_offset, virtual_value, alias); | |
1650 | ||
1651 | assemble_end_function (thunk_fndecl, fnname); | |
68a55980 | 1652 | insn_locations_finalize (); |
6744a6ab JH |
1653 | init_insn_lengths (); |
1654 | free_after_compilation (cfun); | |
6744a6ab | 1655 | TREE_ASM_WRITTEN (thunk_fndecl) = 1; |
d52f5295 ML |
1656 | thunk.thunk_p = false; |
1657 | analyzed = false; | |
6744a6ab | 1658 | } |
d90cae08 JH |
1659 | else if (stdarg_p (TREE_TYPE (thunk_fndecl))) |
1660 | { | |
1661 | error ("generic thunk code fails for method %qD which uses %<...%>", | |
1662 | thunk_fndecl); | |
1663 | TREE_ASM_WRITTEN (thunk_fndecl) = 1; | |
1664 | analyzed = true; | |
1665 | return false; | |
1666 | } | |
6744a6ab JH |
1667 | else |
1668 | { | |
1669 | tree restype; | |
1670 | basic_block bb, then_bb, else_bb, return_bb; | |
1671 | gimple_stmt_iterator bsi; | |
1672 | int nargs = 0; | |
1673 | tree arg; | |
1674 | int i; | |
1675 | tree resdecl; | |
1676 | tree restmp = NULL; | |
4f5a53cc | 1677 | tree resbnd = NULL; |
6744a6ab | 1678 | |
538dd0b7 DM |
1679 | gcall *call; |
1680 | greturn *ret; | |
c43ade80 | 1681 | bool alias_is_noreturn = TREE_THIS_VOLATILE (alias); |
6744a6ab | 1682 | |
e9191ad3 JH |
1683 | /* We may be called from expand_thunk that releses body except for |
1684 | DECL_ARGUMENTS. In this case force_gimple_thunk is true. */ | |
1685 | if (in_lto_p && !force_gimple_thunk) | |
70486010 | 1686 | get_untransformed_body (); |
afdec9bd | 1687 | a = DECL_ARGUMENTS (thunk_fndecl); |
b84d4347 | 1688 | |
afdec9bd JH |
1689 | current_function_decl = thunk_fndecl; |
1690 | ||
1691 | /* Ensure thunks are emitted in their correct sections. */ | |
9641fab3 JH |
1692 | resolve_unique_section (thunk_fndecl, 0, |
1693 | flag_function_sections); | |
afdec9bd | 1694 | |
6744a6ab JH |
1695 | DECL_IGNORED_P (thunk_fndecl) = 1; |
1696 | bitmap_obstack_initialize (NULL); | |
1697 | ||
d52f5295 | 1698 | if (thunk.virtual_offset_p) |
6744a6ab JH |
1699 | virtual_offset = size_int (virtual_value); |
1700 | ||
1701 | /* Build the return declaration for the function. */ | |
1702 | restype = TREE_TYPE (TREE_TYPE (thunk_fndecl)); | |
1703 | if (DECL_RESULT (thunk_fndecl) == NULL_TREE) | |
1704 | { | |
1705 | resdecl = build_decl (input_location, RESULT_DECL, 0, restype); | |
1706 | DECL_ARTIFICIAL (resdecl) = 1; | |
1707 | DECL_IGNORED_P (resdecl) = 1; | |
1708 | DECL_RESULT (thunk_fndecl) = resdecl; | |
afdec9bd | 1709 | DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl; |
6744a6ab JH |
1710 | } |
1711 | else | |
1712 | resdecl = DECL_RESULT (thunk_fndecl); | |
1713 | ||
10881cff JH |
1714 | bb = then_bb = else_bb = return_bb |
1715 | = init_lowered_empty_function (thunk_fndecl, true, count); | |
6744a6ab JH |
1716 | |
1717 | bsi = gsi_start_bb (bb); | |
1718 | ||
1719 | /* Build call to the function being thunked. */ | |
e199dd0a | 1720 | if (!VOID_TYPE_P (restype) |
ab4c578f MP |
1721 | && (!alias_is_noreturn |
1722 | || TREE_ADDRESSABLE (restype) | |
1723 | || TREE_CODE (TYPE_SIZE_UNIT (restype)) != INTEGER_CST)) | |
6744a6ab | 1724 | { |
bc0ec027 | 1725 | if (DECL_BY_REFERENCE (resdecl)) |
8c14c817 ML |
1726 | { |
1727 | restmp = gimple_fold_indirect_ref (resdecl); | |
1728 | if (!restmp) | |
1729 | restmp = build2 (MEM_REF, | |
1730 | TREE_TYPE (TREE_TYPE (DECL_RESULT (alias))), | |
1731 | resdecl, | |
1732 | build_int_cst (TREE_TYPE | |
1733 | (DECL_RESULT (alias)), 0)); | |
1734 | } | |
bc0ec027 | 1735 | else if (!is_gimple_reg_type (restype)) |
6744a6ab | 1736 | { |
bbd1bae2 JJ |
1737 | if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl))) |
1738 | { | |
1739 | restmp = resdecl; | |
5cf18d25 | 1740 | |
bbd1bae2 JJ |
1741 | if (TREE_CODE (restmp) == VAR_DECL) |
1742 | add_local_decl (cfun, restmp); | |
1743 | BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp; | |
1744 | } | |
1745 | else | |
1746 | restmp = create_tmp_var (restype, "retval"); | |
6744a6ab JH |
1747 | } |
1748 | else | |
7cc434a3 | 1749 | restmp = create_tmp_reg (restype, "retval"); |
6744a6ab JH |
1750 | } |
1751 | ||
910ad8de | 1752 | for (arg = a; arg; arg = DECL_CHAIN (arg)) |
6744a6ab | 1753 | nargs++; |
ef062b13 | 1754 | auto_vec<tree> vargs (nargs); |
4eaf52aa JJ |
1755 | i = 0; |
1756 | arg = a; | |
6744a6ab | 1757 | if (this_adjusting) |
4eaf52aa JJ |
1758 | { |
1759 | vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset, | |
1760 | virtual_offset)); | |
1761 | arg = DECL_CHAIN (a); | |
1762 | i = 1; | |
1763 | } | |
bc0ec027 JH |
1764 | |
1765 | if (nargs) | |
4eaf52aa | 1766 | for (; i < nargs; i++, arg = DECL_CHAIN (arg)) |
fc044323 JM |
1767 | { |
1768 | tree tmp = arg; | |
1769 | if (!is_gimple_val (arg)) | |
1770 | { | |
1771 | tmp = create_tmp_reg (TYPE_MAIN_VARIANT | |
1772 | (TREE_TYPE (arg)), "arg"); | |
355fe088 | 1773 | gimple *stmt = gimple_build_assign (tmp, arg); |
fc044323 JM |
1774 | gsi_insert_after (&bsi, stmt, GSI_NEW_STMT); |
1775 | } | |
1776 | vargs.quick_push (tmp); | |
1777 | } | |
6744a6ab | 1778 | call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs); |
d52f5295 | 1779 | callees->call_stmt = call; |
6744a6ab | 1780 | gimple_call_set_from_thunk (call, true); |
d5e254e1 | 1781 | gimple_call_set_with_bounds (call, instrumentation_clone); |
b7aa4a3a JH |
1782 | |
1783 | /* Return slot optimization is always possible and in fact requred to | |
1784 | return values with DECL_BY_REFERENCE. */ | |
1785 | if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl)) | |
1786 | && (!is_gimple_reg_type (TREE_TYPE (resdecl)) | |
1787 | || DECL_BY_REFERENCE (resdecl))) | |
1788 | gimple_call_set_return_slot_opt (call, true); | |
1789 | ||
e199dd0a | 1790 | if (restmp) |
bc0ec027 JH |
1791 | { |
1792 | gimple_call_set_lhs (call, restmp); | |
1793 | gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp), | |
1794 | TREE_TYPE (TREE_TYPE (alias)))); | |
1795 | } | |
6744a6ab | 1796 | gsi_insert_after (&bsi, call, GSI_NEW_STMT); |
c43ade80 | 1797 | if (!alias_is_noreturn) |
bc0ec027 | 1798 | { |
4f5a53cc IE |
1799 | if (instrumentation_clone |
1800 | && !DECL_BY_REFERENCE (resdecl) | |
1801 | && restmp | |
1802 | && BOUNDED_P (restmp)) | |
1803 | { | |
1804 | resbnd = chkp_insert_retbnd_call (NULL, restmp, &bsi); | |
1805 | create_edge (get_create (gimple_call_fndecl (gsi_stmt (bsi))), | |
1806 | as_a <gcall *> (gsi_stmt (bsi)), | |
1807 | callees->count, callees->frequency); | |
1808 | } | |
1809 | ||
bc0ec027 JH |
1810 | if (restmp && !this_adjusting |
1811 | && (fixed_offset || virtual_offset)) | |
1812 | { | |
1813 | tree true_label = NULL_TREE; | |
6744a6ab | 1814 | |
bc0ec027 JH |
1815 | if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE) |
1816 | { | |
355fe088 | 1817 | gimple *stmt; |
10881cff | 1818 | edge e; |
bc0ec027 JH |
1819 | /* If the return type is a pointer, we need to |
1820 | protect against NULL. We know there will be an | |
1821 | adjustment, because that's why we're emitting a | |
1822 | thunk. */ | |
c4d281b2 | 1823 | then_bb = create_basic_block (NULL, bb); |
10881cff JH |
1824 | then_bb->count = count - count / 16; |
1825 | then_bb->frequency = BB_FREQ_MAX - BB_FREQ_MAX / 16; | |
c4d281b2 | 1826 | return_bb = create_basic_block (NULL, then_bb); |
10881cff JH |
1827 | return_bb->count = count; |
1828 | return_bb->frequency = BB_FREQ_MAX; | |
c4d281b2 | 1829 | else_bb = create_basic_block (NULL, else_bb); |
10881cff JH |
1830 | then_bb->count = count / 16; |
1831 | then_bb->frequency = BB_FREQ_MAX / 16; | |
bc0ec027 JH |
1832 | add_bb_to_loop (then_bb, bb->loop_father); |
1833 | add_bb_to_loop (return_bb, bb->loop_father); | |
1834 | add_bb_to_loop (else_bb, bb->loop_father); | |
1835 | remove_edge (single_succ_edge (bb)); | |
1836 | true_label = gimple_block_label (then_bb); | |
1837 | stmt = gimple_build_cond (NE_EXPR, restmp, | |
1838 | build_zero_cst (TREE_TYPE (restmp)), | |
1839 | NULL_TREE, NULL_TREE); | |
1840 | gsi_insert_after (&bsi, stmt, GSI_NEW_STMT); | |
10881cff JH |
1841 | e = make_edge (bb, then_bb, EDGE_TRUE_VALUE); |
1842 | e->probability = REG_BR_PROB_BASE - REG_BR_PROB_BASE / 16; | |
1843 | e->count = count - count / 16; | |
1844 | e = make_edge (bb, else_bb, EDGE_FALSE_VALUE); | |
1845 | e->probability = REG_BR_PROB_BASE / 16; | |
1846 | e->count = count / 16; | |
1847 | e = make_edge (return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0); | |
1848 | e->probability = REG_BR_PROB_BASE; | |
1849 | e->count = count; | |
1850 | e = make_edge (then_bb, return_bb, EDGE_FALLTHRU); | |
1851 | e->probability = REG_BR_PROB_BASE; | |
1852 | e->count = count - count / 16; | |
1853 | e = make_edge (else_bb, return_bb, EDGE_FALLTHRU); | |
1854 | e->probability = REG_BR_PROB_BASE; | |
1855 | e->count = count / 16; | |
bc0ec027 JH |
1856 | bsi = gsi_last_bb (then_bb); |
1857 | } | |
6744a6ab | 1858 | |
bc0ec027 JH |
1859 | restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0, |
1860 | fixed_offset, virtual_offset); | |
1861 | if (true_label) | |
1862 | { | |
355fe088 | 1863 | gimple *stmt; |
bc0ec027 JH |
1864 | bsi = gsi_last_bb (else_bb); |
1865 | stmt = gimple_build_assign (restmp, | |
1866 | build_zero_cst (TREE_TYPE (restmp))); | |
1867 | gsi_insert_after (&bsi, stmt, GSI_NEW_STMT); | |
1868 | bsi = gsi_last_bb (return_bb); | |
1869 | } | |
6744a6ab | 1870 | } |
bc0ec027 JH |
1871 | else |
1872 | gimple_call_set_tail (call, true); | |
6744a6ab | 1873 | |
bc0ec027 | 1874 | /* Build return value. */ |
8c14c817 ML |
1875 | if (!DECL_BY_REFERENCE (resdecl)) |
1876 | ret = gimple_build_return (restmp); | |
1877 | else | |
1878 | ret = gimple_build_return (resdecl); | |
4f5a53cc | 1879 | gimple_return_set_retbnd (ret, resbnd); |
8c14c817 | 1880 | |
bc0ec027 | 1881 | gsi_insert_after (&bsi, ret, GSI_NEW_STMT); |
6744a6ab JH |
1882 | } |
1883 | else | |
bc0ec027 JH |
1884 | { |
1885 | gimple_call_set_tail (call, true); | |
1886 | remove_edge (single_succ_edge (bb)); | |
1887 | } | |
6744a6ab | 1888 | |
afdec9bd | 1889 | cfun->gimple_df->in_ssa_p = true; |
10881cff JH |
1890 | profile_status_for_fn (cfun) |
1891 | = count ? PROFILE_READ : PROFILE_GUESSED; | |
afdec9bd JH |
1892 | /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */ |
1893 | TREE_ASM_WRITTEN (thunk_fndecl) = false; | |
6744a6ab JH |
1894 | delete_unreachable_blocks (); |
1895 | update_ssa (TODO_update_ssa); | |
b2b29377 | 1896 | checking_verify_flow_info (); |
253eab4f | 1897 | free_dominance_info (CDI_DOMINATORS); |
6744a6ab | 1898 | |
6744a6ab JH |
1899 | /* Since we want to emit the thunk, we explicitly mark its name as |
1900 | referenced. */ | |
d52f5295 ML |
1901 | thunk.thunk_p = false; |
1902 | lowered = true; | |
6744a6ab JH |
1903 | bitmap_obstack_release (NULL); |
1904 | } | |
1905 | current_function_decl = NULL; | |
af16bc76 | 1906 | set_cfun (NULL); |
afdec9bd | 1907 | return true; |
6744a6ab JH |
1908 | } |
1909 | ||
3dafb85c | 1910 | /* Assemble thunks and aliases associated to node. */ |
c47d0034 | 1911 | |
3dafb85c ML |
1912 | void |
1913 | cgraph_node::assemble_thunks_and_aliases (void) | |
c47d0034 | 1914 | { |
3dafb85c ML |
1915 | cgraph_edge *e; |
1916 | ipa_ref *ref; | |
39e2db00 | 1917 | |
3dafb85c | 1918 | for (e = callers; e;) |
d5e254e1 | 1919 | if (e->caller->thunk.thunk_p |
6bbf39b7 | 1920 | && !e->caller->global.inlined_to |
d5e254e1 | 1921 | && !e->caller->thunk.add_pointer_bounds_args) |
c47d0034 | 1922 | { |
3dafb85c | 1923 | cgraph_node *thunk = e->caller; |
c47d0034 JH |
1924 | |
1925 | e = e->next_caller; | |
d52f5295 | 1926 | thunk->expand_thunk (true, false); |
3dafb85c | 1927 | thunk->assemble_thunks_and_aliases (); |
c47d0034 JH |
1928 | } |
1929 | else | |
1930 | e = e->next_caller; | |
e55637b7 | 1931 | |
3dafb85c | 1932 | FOR_EACH_ALIAS (this, ref) |
e55637b7 | 1933 | { |
3dafb85c | 1934 | cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring); |
71e54687 JH |
1935 | if (!alias->transparent_alias) |
1936 | { | |
1937 | bool saved_written = TREE_ASM_WRITTEN (decl); | |
1938 | ||
1939 | /* Force assemble_alias to really output the alias this time instead | |
1940 | of buffering it in same alias pairs. */ | |
1941 | TREE_ASM_WRITTEN (decl) = 1; | |
1942 | do_assemble_alias (alias->decl, | |
1943 | DECL_ASSEMBLER_NAME (decl)); | |
1944 | alias->assemble_thunks_and_aliases (); | |
1945 | TREE_ASM_WRITTEN (decl) = saved_written; | |
1946 | } | |
e55637b7 | 1947 | } |
c47d0034 JH |
1948 | } |
1949 | ||
3dafb85c | 1950 | /* Expand function specified by node. */ |
0878843f | 1951 | |
3dafb85c ML |
1952 | void |
1953 | cgraph_node::expand (void) | |
0878843f | 1954 | { |
0878843f RG |
1955 | location_t saved_loc; |
1956 | ||
9c8305f8 | 1957 | /* We ought to not compile any inline clones. */ |
3dafb85c | 1958 | gcc_assert (!global.inlined_to); |
9c8305f8 JH |
1959 | |
1960 | announce_function (decl); | |
3dafb85c ML |
1961 | process = 0; |
1962 | gcc_assert (lowered); | |
70486010 | 1963 | get_untransformed_body (); |
9c8305f8 JH |
1964 | |
1965 | /* Generate RTL for the body of DECL. */ | |
1966 | ||
0878843f RG |
1967 | timevar_push (TV_REST_OF_COMPILATION); |
1968 | ||
3dafb85c | 1969 | gcc_assert (symtab->global_info_ready); |
0878843f RG |
1970 | |
1971 | /* Initialize the default bitmap obstack. */ | |
1972 | bitmap_obstack_initialize (NULL); | |
1973 | ||
1974 | /* Initialize the RTL code for the function. */ | |
0878843f | 1975 | saved_loc = input_location; |
9c8305f8 | 1976 | input_location = DECL_SOURCE_LOCATION (decl); |
be373510 ML |
1977 | |
1978 | gcc_assert (DECL_STRUCT_FUNCTION (decl)); | |
1979 | push_cfun (DECL_STRUCT_FUNCTION (decl)); | |
9c8305f8 | 1980 | init_function_start (decl); |
0878843f RG |
1981 | |
1982 | gimple_register_cfg_hooks (); | |
1983 | ||
1984 | bitmap_obstack_initialize (®_obstack); /* FIXME, only at RTL generation*/ | |
1985 | ||
1986 | execute_all_ipa_transforms (); | |
1987 | ||
1988 | /* Perform all tree transforms and optimizations. */ | |
1989 | ||
1990 | /* Signal the start of passes. */ | |
1991 | invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL); | |
1992 | ||
2cbf2d95 | 1993 | execute_pass_list (cfun, g->get_passes ()->all_passes); |
0878843f RG |
1994 | |
1995 | /* Signal the end of passes. */ | |
1996 | invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL); | |
1997 | ||
1998 | bitmap_obstack_release (®_obstack); | |
1999 | ||
2000 | /* Release the default bitmap obstack. */ | |
2001 | bitmap_obstack_release (NULL); | |
2002 | ||
0878843f RG |
2003 | /* If requested, warn about function definitions where the function will |
2004 | return a value (usually of some struct or union type) which itself will | |
2005 | take up a lot of stack space. */ | |
9c8305f8 | 2006 | if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl)) |
0878843f | 2007 | { |
9c8305f8 | 2008 | tree ret_type = TREE_TYPE (TREE_TYPE (decl)); |
0878843f RG |
2009 | |
2010 | if (ret_type && TYPE_SIZE_UNIT (ret_type) | |
2011 | && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST | |
2012 | && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type), | |
2013 | larger_than_size)) | |
2014 | { | |
2015 | unsigned int size_as_int | |
2016 | = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type)); | |
2017 | ||
2018 | if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0) | |
2019 | warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes", | |
9c8305f8 | 2020 | decl, size_as_int); |
0878843f RG |
2021 | else |
2022 | warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes", | |
9c8305f8 | 2023 | decl, larger_than_size); |
0878843f RG |
2024 | } |
2025 | } | |
2026 | ||
9c8305f8 JH |
2027 | gimple_set_body (decl, NULL); |
2028 | if (DECL_STRUCT_FUNCTION (decl) == 0 | |
d52f5295 | 2029 | && !cgraph_node::get (decl)->origin) |
0878843f RG |
2030 | { |
2031 | /* Stop pointing to the local nodes about to be freed. | |
2032 | But DECL_INITIAL must remain nonzero so we know this | |
2033 | was an actual function definition. | |
2034 | For a nested function, this is done in c_pop_function_context. | |
2035 | If rest_of_compilation set this to 0, leave it 0. */ | |
9c8305f8 JH |
2036 | if (DECL_INITIAL (decl) != 0) |
2037 | DECL_INITIAL (decl) = error_mark_node; | |
0878843f RG |
2038 | } |
2039 | ||
2040 | input_location = saved_loc; | |
2041 | ||
2042 | ggc_collect (); | |
2043 | timevar_pop (TV_REST_OF_COMPILATION); | |
5806d9ac JH |
2044 | |
2045 | /* Make sure that BE didn't give up on compiling. */ | |
2046 | gcc_assert (TREE_ASM_WRITTEN (decl)); | |
be373510 ML |
2047 | if (cfun) |
2048 | pop_cfun (); | |
1bb7e8f8 JH |
2049 | |
2050 | /* It would make a lot more sense to output thunks before function body to get more | |
65d630d4 | 2051 | forward and lest backwarding jumps. This however would need solving problem |
1bb7e8f8 | 2052 | with comdats. See PR48668. Also aliases must come after function itself to |
65d630d4 | 2053 | make one pass assemblers, like one on AIX, happy. See PR 50689. |
1bb7e8f8 JH |
2054 | FIXME: Perhaps thunks should be move before function IFF they are not in comdat |
2055 | groups. */ | |
3dafb85c ML |
2056 | assemble_thunks_and_aliases (); |
2057 | release_body (); | |
39ecc018 JH |
2058 | /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer |
2059 | points to the dead function body. */ | |
3dafb85c ML |
2060 | remove_callees (); |
2061 | remove_all_references (); | |
1c4a429a JH |
2062 | } |
2063 | ||
9cec31f4 ML |
2064 | /* Node comparer that is responsible for the order that corresponds |
2065 | to time when a function was launched for the first time. */ | |
2066 | ||
2067 | static int | |
2068 | node_cmp (const void *pa, const void *pb) | |
2069 | { | |
3dafb85c ML |
2070 | const cgraph_node *a = *(const cgraph_node * const *) pa; |
2071 | const cgraph_node *b = *(const cgraph_node * const *) pb; | |
9cec31f4 ML |
2072 | |
2073 | /* Functions with time profile must be before these without profile. */ | |
2074 | if (!a->tp_first_run || !b->tp_first_run) | |
2075 | return a->tp_first_run - b->tp_first_run; | |
2076 | ||
2077 | return a->tp_first_run != b->tp_first_run | |
2078 | ? b->tp_first_run - a->tp_first_run | |
2079 | : b->order - a->order; | |
2080 | } | |
6674a6ce | 2081 | |
db0e878d AJ |
2082 | /* Expand all functions that must be output. |
2083 | ||
b58b1157 JH |
2084 | Attempt to topologically sort the nodes so function is output when |
2085 | all called functions are already assembled to allow data to be | |
a98ebe2e | 2086 | propagated across the callgraph. Use a stack to get smaller distance |
d1a6adeb | 2087 | between a function and its callees (later we may choose to use a more |
b58b1157 JH |
2088 | sophisticated algorithm for function reordering; we will likely want |
2089 | to use subsections to make the output functions appear in top-down | |
2090 | order). */ | |
2091 | ||
2092 | static void | |
65d630d4 | 2093 | expand_all_functions (void) |
b58b1157 | 2094 | { |
3dafb85c ML |
2095 | cgraph_node *node; |
2096 | cgraph_node **order = XCNEWVEC (cgraph_node *, | |
2097 | symtab->cgraph_count); | |
9cec31f4 | 2098 | unsigned int expanded_func_count = 0, profiled_func_count = 0; |
f30cfcb1 | 2099 | int order_pos, new_order_pos = 0; |
b58b1157 JH |
2100 | int i; |
2101 | ||
af8bca3c | 2102 | order_pos = ipa_reverse_postorder (order); |
3dafb85c | 2103 | gcc_assert (order_pos == symtab->cgraph_count); |
b58b1157 | 2104 | |
1ae58c30 | 2105 | /* Garbage collector may remove inline clones we eliminate during |
18c6ada9 JH |
2106 | optimization. So we must be sure to not reference them. */ |
2107 | for (i = 0; i < order_pos; i++) | |
257eb6e3 | 2108 | if (order[i]->process) |
18c6ada9 JH |
2109 | order[new_order_pos++] = order[i]; |
2110 | ||
9cec31f4 | 2111 | if (flag_profile_reorder_functions) |
3dafb85c | 2112 | qsort (order, new_order_pos, sizeof (cgraph_node *), node_cmp); |
9cec31f4 | 2113 | |
18c6ada9 | 2114 | for (i = new_order_pos - 1; i >= 0; i--) |
b58b1157 JH |
2115 | { |
2116 | node = order[i]; | |
9cec31f4 | 2117 | |
257eb6e3 | 2118 | if (node->process) |
b58b1157 | 2119 | { |
21c0a521 DM |
2120 | expanded_func_count++; |
2121 | if(node->tp_first_run) | |
2122 | profiled_func_count++; | |
2123 | ||
2124 | if (symtab->dump_file) | |
2125 | fprintf (symtab->dump_file, | |
2126 | "Time profile order in expand_all_functions:%s:%d\n", | |
2127 | node->asm_name (), node->tp_first_run); | |
257eb6e3 | 2128 | node->process = 0; |
3dafb85c | 2129 | node->expand (); |
b58b1157 JH |
2130 | } |
2131 | } | |
9cec31f4 ML |
2132 | |
2133 | if (dump_file) | |
2134 | fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n", | |
2135 | main_input_filename, profiled_func_count, expanded_func_count); | |
2136 | ||
3dafb85c ML |
2137 | if (symtab->dump_file && flag_profile_reorder_functions) |
2138 | fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n", | |
9cec31f4 ML |
2139 | profiled_func_count, expanded_func_count); |
2140 | ||
3dafb85c | 2141 | symtab->process_new_functions (); |
45852dcc | 2142 | free_gimplify_stack (); |
50674e96 | 2143 | |
b58b1157 JH |
2144 | free (order); |
2145 | } | |
2146 | ||
474eccc6 ILT |
2147 | /* This is used to sort the node types by the cgraph order number. */ |
2148 | ||
24b97832 ILT |
2149 | enum cgraph_order_sort_kind |
2150 | { | |
2151 | ORDER_UNDEFINED = 0, | |
2152 | ORDER_FUNCTION, | |
2153 | ORDER_VAR, | |
3ef46782 | 2154 | ORDER_VAR_UNDEF, |
24b97832 ILT |
2155 | ORDER_ASM |
2156 | }; | |
2157 | ||
474eccc6 ILT |
2158 | struct cgraph_order_sort |
2159 | { | |
24b97832 | 2160 | enum cgraph_order_sort_kind kind; |
474eccc6 ILT |
2161 | union |
2162 | { | |
3dafb85c | 2163 | cgraph_node *f; |
2c8326a5 | 2164 | varpool_node *v; |
3dafb85c | 2165 | asm_node *a; |
474eccc6 ILT |
2166 | } u; |
2167 | }; | |
2168 | ||
2169 | /* Output all functions, variables, and asm statements in the order | |
2170 | according to their order fields, which is the order in which they | |
2171 | appeared in the file. This implements -fno-toplevel-reorder. In | |
2172 | this mode we may output functions and variables which don't really | |
7861b648 AK |
2173 | need to be output. |
2174 | When NO_REORDER is true only do this for symbols marked no reorder. */ | |
474eccc6 ILT |
2175 | |
2176 | static void | |
7861b648 | 2177 | output_in_order (bool no_reorder) |
474eccc6 ILT |
2178 | { |
2179 | int max; | |
3dafb85c | 2180 | cgraph_order_sort *nodes; |
474eccc6 | 2181 | int i; |
3dafb85c | 2182 | cgraph_node *pf; |
2c8326a5 | 2183 | varpool_node *pv; |
3dafb85c ML |
2184 | asm_node *pa; |
2185 | max = symtab->order; | |
2186 | nodes = XCNEWVEC (cgraph_order_sort, max); | |
474eccc6 | 2187 | |
65c70e6b | 2188 | FOR_EACH_DEFINED_FUNCTION (pf) |
474eccc6 | 2189 | { |
67348ccc | 2190 | if (pf->process && !pf->thunk.thunk_p && !pf->alias) |
474eccc6 | 2191 | { |
7861b648 AK |
2192 | if (no_reorder && !pf->no_reorder) |
2193 | continue; | |
67348ccc | 2194 | i = pf->order; |
474eccc6 ILT |
2195 | gcc_assert (nodes[i].kind == ORDER_UNDEFINED); |
2196 | nodes[i].kind = ORDER_FUNCTION; | |
2197 | nodes[i].u.f = pf; | |
2198 | } | |
2199 | } | |
2200 | ||
3ef46782 AM |
2201 | /* There is a similar loop in symbol_table::output_variables. |
2202 | Please keep them in sync. */ | |
2203 | FOR_EACH_VARIABLE (pv) | |
2204 | { | |
2205 | if (no_reorder && !pv->no_reorder) | |
2206 | continue; | |
2207 | if (DECL_HARD_REGISTER (pv->decl) | |
2208 | || DECL_HAS_VALUE_EXPR_P (pv->decl)) | |
2209 | continue; | |
2210 | i = pv->order; | |
2211 | gcc_assert (nodes[i].kind == ORDER_UNDEFINED); | |
2212 | nodes[i].kind = pv->definition ? ORDER_VAR : ORDER_VAR_UNDEF; | |
2213 | nodes[i].u.v = pv; | |
2214 | } | |
474eccc6 | 2215 | |
3dafb85c | 2216 | for (pa = symtab->first_asm_symbol (); pa; pa = pa->next) |
474eccc6 ILT |
2217 | { |
2218 | i = pa->order; | |
2219 | gcc_assert (nodes[i].kind == ORDER_UNDEFINED); | |
2220 | nodes[i].kind = ORDER_ASM; | |
2221 | nodes[i].u.a = pa; | |
2222 | } | |
474eccc6 | 2223 | |
7386e3ee | 2224 | /* In toplevel reorder mode we output all statics; mark them as needed. */ |
7386e3ee | 2225 | |
7fece979 JJ |
2226 | for (i = 0; i < max; ++i) |
2227 | if (nodes[i].kind == ORDER_VAR) | |
9041d2e6 | 2228 | nodes[i].u.v->finalize_named_section_flags (); |
7fece979 | 2229 | |
474eccc6 ILT |
2230 | for (i = 0; i < max; ++i) |
2231 | { | |
2232 | switch (nodes[i].kind) | |
2233 | { | |
2234 | case ORDER_FUNCTION: | |
257eb6e3 | 2235 | nodes[i].u.f->process = 0; |
3dafb85c | 2236 | nodes[i].u.f->expand (); |
474eccc6 ILT |
2237 | break; |
2238 | ||
2239 | case ORDER_VAR: | |
9041d2e6 | 2240 | nodes[i].u.v->assemble_decl (); |
474eccc6 ILT |
2241 | break; |
2242 | ||
3ef46782 AM |
2243 | case ORDER_VAR_UNDEF: |
2244 | assemble_undefined_decl (nodes[i].u.v->decl); | |
2245 | break; | |
2246 | ||
474eccc6 ILT |
2247 | case ORDER_ASM: |
2248 | assemble_asm (nodes[i].u.a->asm_str); | |
2249 | break; | |
2250 | ||
2251 | case ORDER_UNDEFINED: | |
2252 | break; | |
2253 | ||
2254 | default: | |
2255 | gcc_unreachable (); | |
2256 | } | |
2257 | } | |
e7b9eb2c | 2258 | |
3dafb85c ML |
2259 | symtab->clear_asm_symbols (); |
2260 | ||
33283dad | 2261 | free (nodes); |
474eccc6 ILT |
2262 | } |
2263 | ||
ef330312 PB |
2264 | static void |
2265 | ipa_passes (void) | |
2266 | { | |
315f8c0e DM |
2267 | gcc::pass_manager *passes = g->get_passes (); |
2268 | ||
db2960f4 | 2269 | set_cfun (NULL); |
04b201a2 | 2270 | current_function_decl = NULL; |
726a989a | 2271 | gimple_register_cfg_hooks (); |
ef330312 | 2272 | bitmap_obstack_initialize (NULL); |
b20996ff | 2273 | |
090fa0ab GF |
2274 | invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL); |
2275 | ||
b20996ff | 2276 | if (!in_lto_p) |
0430f80c | 2277 | { |
315f8c0e | 2278 | execute_ipa_pass_list (passes->all_small_ipa_passes); |
0430f80c RG |
2279 | if (seen_error ()) |
2280 | return; | |
2281 | } | |
3baf459d | 2282 | |
8605403e JH |
2283 | /* This extra symtab_remove_unreachable_nodes pass tends to catch some |
2284 | devirtualization and other changes where removal iterate. */ | |
17e0fc92 | 2285 | symtab->remove_unreachable_nodes (symtab->dump_file); |
467a8db0 | 2286 | |
d7f09764 DN |
2287 | /* If pass_all_early_optimizations was not scheduled, the state of |
2288 | the cgraph will not be properly updated. Update it now. */ | |
3dafb85c ML |
2289 | if (symtab->state < IPA_SSA) |
2290 | symtab->state = IPA_SSA; | |
3baf459d | 2291 | |
d7f09764 DN |
2292 | if (!in_lto_p) |
2293 | { | |
2294 | /* Generate coverage variables and constructors. */ | |
2295 | coverage_finish (); | |
2296 | ||
2297 | /* Process new functions added. */ | |
2298 | set_cfun (NULL); | |
2299 | current_function_decl = NULL; | |
3dafb85c | 2300 | symtab->process_new_functions (); |
d7f09764 | 2301 | |
090fa0ab | 2302 | execute_ipa_summary_passes |
6a5ac314 | 2303 | ((ipa_opt_pass_d *) passes->all_regular_ipa_passes); |
fb3f88cc | 2304 | } |
c082f9f3 SB |
2305 | |
2306 | /* Some targets need to handle LTO assembler output specially. */ | |
f0d78df9 | 2307 | if (flag_generate_lto || flag_generate_offload) |
c082f9f3 SB |
2308 | targetm.asm_out.lto_start (); |
2309 | ||
d7f09764 | 2310 | if (!in_lto_p) |
1f6be682 IV |
2311 | { |
2312 | if (g->have_offload) | |
2313 | { | |
2314 | section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX; | |
1b34e6e2 | 2315 | lto_stream_offload_p = true; |
837bac8c | 2316 | ipa_write_summaries (); |
1b34e6e2 | 2317 | lto_stream_offload_p = false; |
1f6be682 IV |
2318 | } |
2319 | if (flag_lto) | |
2320 | { | |
2321 | section_name_prefix = LTO_SECTION_NAME_PREFIX; | |
1b34e6e2 | 2322 | lto_stream_offload_p = false; |
837bac8c | 2323 | ipa_write_summaries (); |
1f6be682 IV |
2324 | } |
2325 | } | |
d7f09764 | 2326 | |
f0d78df9 | 2327 | if (flag_generate_lto || flag_generate_offload) |
c082f9f3 SB |
2328 | targetm.asm_out.lto_end (); |
2329 | ||
cc8547a7 | 2330 | if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects)) |
315f8c0e | 2331 | execute_ipa_pass_list (passes->all_regular_ipa_passes); |
090fa0ab | 2332 | invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL); |
3baf459d | 2333 | |
ef330312 PB |
2334 | bitmap_obstack_release (NULL); |
2335 | } | |
2336 | ||
25e2c40d JH |
2337 | |
2338 | /* Return string alias is alias of. */ | |
2339 | ||
2340 | static tree | |
2341 | get_alias_symbol (tree decl) | |
2342 | { | |
2343 | tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)); | |
2344 | return get_identifier (TREE_STRING_POINTER | |
2345 | (TREE_VALUE (TREE_VALUE (alias)))); | |
2346 | } | |
2347 | ||
2348 | ||
c9552bff | 2349 | /* Weakrefs may be associated to external decls and thus not output |
073a8998 | 2350 | at expansion time. Emit all necessary aliases. */ |
c9552bff | 2351 | |
3dafb85c ML |
2352 | void |
2353 | symbol_table::output_weakrefs (void) | |
c9552bff | 2354 | { |
5e20cdc9 | 2355 | symtab_node *node; |
d5e254e1 | 2356 | cgraph_node *cnode; |
40a7fe1e | 2357 | FOR_EACH_SYMBOL (node) |
67348ccc DM |
2358 | if (node->alias |
2359 | && !TREE_ASM_WRITTEN (node->decl) | |
d5e254e1 IE |
2360 | && (!(cnode = dyn_cast <cgraph_node *> (node)) |
2361 | || !cnode->instrumented_version | |
2362 | || !TREE_ASM_WRITTEN (cnode->instrumented_version->decl)) | |
67348ccc | 2363 | && node->weakref) |
40a7fe1e JH |
2364 | { |
2365 | tree target; | |
2366 | ||
2367 | /* Weakrefs are special by not requiring target definition in current | |
2368 | compilation unit. It is thus bit hard to work out what we want to | |
2369 | alias. | |
2370 | When alias target is defined, we need to fetch it from symtab reference, | |
2371 | otherwise it is pointed to by alias_target. */ | |
67348ccc DM |
2372 | if (node->alias_target) |
2373 | target = (DECL_P (node->alias_target) | |
2374 | ? DECL_ASSEMBLER_NAME (node->alias_target) | |
2375 | : node->alias_target); | |
2376 | else if (node->analyzed) | |
d52f5295 | 2377 | target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl); |
40a7fe1e JH |
2378 | else |
2379 | { | |
2380 | gcc_unreachable (); | |
67348ccc | 2381 | target = get_alias_symbol (node->decl); |
40a7fe1e | 2382 | } |
67348ccc | 2383 | do_assemble_alias (node->decl, target); |
40a7fe1e | 2384 | } |
c9552bff JH |
2385 | } |
2386 | ||
711417cd RG |
2387 | /* Perform simple optimizations based on callgraph. */ |
2388 | ||
2389 | void | |
3dafb85c | 2390 | symbol_table::compile (void) |
711417cd RG |
2391 | { |
2392 | if (seen_error ()) | |
2393 | return; | |
2394 | ||
b2b29377 | 2395 | symtab_node::checking_verify_symtab_nodes (); |
711417cd | 2396 | |
711417cd RG |
2397 | timevar_push (TV_CGRAPHOPT); |
2398 | if (pre_ipa_mem_report) | |
2399 | { | |
2400 | fprintf (stderr, "Memory consumption before IPA\n"); | |
2401 | dump_memory_report (false); | |
2402 | } | |
2403 | if (!quiet_flag) | |
2404 | fprintf (stderr, "Performing interprocedural optimizations\n"); | |
3dafb85c | 2405 | state = IPA; |
711417cd | 2406 | |
1f6be682 IV |
2407 | /* Offloading requires LTO infrastructure. */ |
2408 | if (!in_lto_p && g->have_offload) | |
f0d78df9 | 2409 | flag_generate_offload = 1; |
1f6be682 | 2410 | |
65d630d4 | 2411 | /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */ |
f0d78df9 | 2412 | if (flag_generate_lto || flag_generate_offload) |
65d630d4 JH |
2413 | lto_streamer_hooks_init (); |
2414 | ||
711417cd RG |
2415 | /* Don't run the IPA passes if there was any error or sorry messages. */ |
2416 | if (!seen_error ()) | |
2417 | ipa_passes (); | |
2418 | ||
2419 | /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */ | |
2420 | if (seen_error () | |
2421 | || (!in_lto_p && flag_lto && !flag_fat_lto_objects)) | |
2422 | { | |
2423 | timevar_pop (TV_CGRAPHOPT); | |
2424 | return; | |
2425 | } | |
2426 | ||
3dafb85c ML |
2427 | global_info_ready = true; |
2428 | if (dump_file) | |
711417cd | 2429 | { |
3dafb85c ML |
2430 | fprintf (dump_file, "Optimized "); |
2431 | symtab_node:: dump_table (dump_file); | |
711417cd RG |
2432 | } |
2433 | if (post_ipa_mem_report) | |
2434 | { | |
2435 | fprintf (stderr, "Memory consumption after IPA\n"); | |
2436 | dump_memory_report (false); | |
2437 | } | |
2438 | timevar_pop (TV_CGRAPHOPT); | |
2439 | ||
2440 | /* Output everything. */ | |
2441 | (*debug_hooks->assembly_start) (); | |
2442 | if (!quiet_flag) | |
2443 | fprintf (stderr, "Assembling functions:\n"); | |
b2b29377 | 2444 | symtab_node::checking_verify_symtab_nodes (); |
711417cd | 2445 | |
711417cd | 2446 | bitmap_obstack_initialize (NULL); |
315f8c0e | 2447 | execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes); |
711417cd | 2448 | bitmap_obstack_release (NULL); |
65d630d4 | 2449 | mark_functions_to_output (); |
711417cd | 2450 | |
38e55e5c JH |
2451 | /* When weakref support is missing, we autmatically translate all |
2452 | references to NODE to references to its ultimate alias target. | |
2453 | The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and | |
2454 | TREE_CHAIN. | |
2455 | ||
2456 | Set up this mapping before we output any assembler but once we are sure | |
2457 | that all symbol renaming is done. | |
2458 | ||
2459 | FIXME: All this uglyness can go away if we just do renaming at gimple | |
2460 | level by physically rewritting the IL. At the moment we can only redirect | |
2461 | calls, so we need infrastructure for renaming references as well. */ | |
2462 | #ifndef ASM_OUTPUT_WEAKREF | |
5e20cdc9 | 2463 | symtab_node *node; |
38e55e5c JH |
2464 | |
2465 | FOR_EACH_SYMBOL (node) | |
67348ccc DM |
2466 | if (node->alias |
2467 | && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl))) | |
38e55e5c JH |
2468 | { |
2469 | IDENTIFIER_TRANSPARENT_ALIAS | |
67348ccc DM |
2470 | (DECL_ASSEMBLER_NAME (node->decl)) = 1; |
2471 | TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl)) | |
2472 | = (node->alias_target ? node->alias_target | |
dacd445e | 2473 | : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl)); |
38e55e5c JH |
2474 | } |
2475 | #endif | |
2476 | ||
3dafb85c | 2477 | state = EXPANSION; |
9cec31f4 | 2478 | |
711417cd | 2479 | if (!flag_toplevel_reorder) |
7861b648 | 2480 | output_in_order (false); |
711417cd RG |
2481 | else |
2482 | { | |
7861b648 AK |
2483 | /* Output first asm statements and anything ordered. The process |
2484 | flag is cleared for these nodes, so we skip them later. */ | |
2485 | output_in_order (true); | |
65d630d4 | 2486 | expand_all_functions (); |
3dafb85c | 2487 | output_variables (); |
711417cd RG |
2488 | } |
2489 | ||
3dafb85c ML |
2490 | process_new_functions (); |
2491 | state = FINISHED; | |
07250f0e | 2492 | output_weakrefs (); |
711417cd | 2493 | |
3dafb85c | 2494 | if (dump_file) |
711417cd | 2495 | { |
3dafb85c ML |
2496 | fprintf (dump_file, "\nFinal "); |
2497 | symtab_node::dump_table (dump_file); | |
711417cd | 2498 | } |
b2b29377 MM |
2499 | if (!flag_checking) |
2500 | return; | |
d52f5295 | 2501 | symtab_node::verify_symtab_nodes (); |
711417cd RG |
2502 | /* Double check that all inline clones are gone and that all |
2503 | function bodies have been released from memory. */ | |
2504 | if (!seen_error ()) | |
2505 | { | |
3dafb85c | 2506 | cgraph_node *node; |
711417cd RG |
2507 | bool error_found = false; |
2508 | ||
65c70e6b JH |
2509 | FOR_EACH_DEFINED_FUNCTION (node) |
2510 | if (node->global.inlined_to | |
67348ccc | 2511 | || gimple_has_body_p (node->decl)) |
711417cd RG |
2512 | { |
2513 | error_found = true; | |
d52f5295 | 2514 | node->debug (); |
711417cd RG |
2515 | } |
2516 | if (error_found) | |
2517 | internal_error ("nodes with unreleased memory found"); | |
2518 | } | |
711417cd RG |
2519 | } |
2520 | ||
2521 | ||
2522 | /* Analyze the whole compilation unit once it is parsed completely. */ | |
2523 | ||
2524 | void | |
3dafb85c | 2525 | symbol_table::finalize_compilation_unit (void) |
711417cd RG |
2526 | { |
2527 | timevar_push (TV_CGRAPH); | |
2528 | ||
711417cd RG |
2529 | /* If we're here there's no current function anymore. Some frontends |
2530 | are lazy in clearing these. */ | |
2531 | current_function_decl = NULL; | |
2532 | set_cfun (NULL); | |
2533 | ||
2534 | /* Do not skip analyzing the functions if there were errors, we | |
2535 | miss diagnostics for following functions otherwise. */ | |
2536 | ||
2537 | /* Emit size functions we didn't inline. */ | |
2538 | finalize_size_functions (); | |
2539 | ||
2540 | /* Mark alias targets necessary and emit diagnostics. */ | |
711417cd RG |
2541 | handle_alias_pairs (); |
2542 | ||
2543 | if (!quiet_flag) | |
2544 | { | |
2545 | fprintf (stderr, "\nAnalyzing compilation unit\n"); | |
2546 | fflush (stderr); | |
2547 | } | |
2548 | ||
2549 | if (flag_dump_passes) | |
2550 | dump_passes (); | |
2551 | ||
2552 | /* Gimplify and lower all functions, compute reachability and | |
2553 | remove unreachable nodes. */ | |
d7438551 | 2554 | analyze_functions (/*first_time=*/true); |
711417cd RG |
2555 | |
2556 | /* Mark alias targets necessary and emit diagnostics. */ | |
711417cd RG |
2557 | handle_alias_pairs (); |
2558 | ||
2559 | /* Gimplify and lower thunks. */ | |
d7438551 AH |
2560 | analyze_functions (/*first_time=*/false); |
2561 | ||
9ba01f82 RB |
2562 | if (!seen_error ()) |
2563 | { | |
2564 | /* Emit early debug for reachable functions, and by consequence, | |
2565 | locally scoped symbols. */ | |
2566 | struct cgraph_node *cnode; | |
2567 | FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (cnode) | |
2568 | (*debug_hooks->early_global_decl) (cnode->decl); | |
2569 | ||
2570 | /* Clean up anything that needs cleaning up after initial debug | |
2571 | generation. */ | |
68317985 | 2572 | (*debug_hooks->early_finish) (main_input_filename); |
9ba01f82 RB |
2573 | } |
2574 | ||
711417cd | 2575 | /* Finally drive the pass manager. */ |
65d630d4 | 2576 | compile (); |
711417cd RG |
2577 | |
2578 | timevar_pop (TV_CGRAPH); | |
2579 | } | |
2580 | ||
3edf64aa DM |
2581 | /* Reset all state within cgraphunit.c so that we can rerun the compiler |
2582 | within the same process. For use by toplev::finalize. */ | |
2583 | ||
2584 | void | |
2585 | cgraphunit_c_finalize (void) | |
2586 | { | |
2587 | gcc_assert (cgraph_new_nodes.length () == 0); | |
2588 | cgraph_new_nodes.truncate (0); | |
2589 | ||
2590 | vtable_entry_type = NULL; | |
2591 | queued_nodes = &symtab_terminator; | |
2592 | ||
2593 | first_analyzed = NULL; | |
2594 | first_analyzed_var = NULL; | |
2595 | } | |
2596 | ||
d52f5295 | 2597 | /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this |
8be2dc8c ML |
2598 | kind of wrapper method. */ |
2599 | ||
2600 | void | |
3dafb85c | 2601 | cgraph_node::create_wrapper (cgraph_node *target) |
8be2dc8c | 2602 | { |
3f9f4ae7 ML |
2603 | /* Preserve DECL_RESULT so we get right by reference flag. */ |
2604 | tree decl_result = DECL_RESULT (decl); | |
8be2dc8c | 2605 | |
3f9f4ae7 ML |
2606 | /* Remove the function's body but keep arguments to be reused |
2607 | for thunk. */ | |
2608 | release_body (true); | |
2609 | reset (); | |
8be2dc8c | 2610 | |
0a7246ee | 2611 | DECL_UNINLINABLE (decl) = false; |
3f9f4ae7 ML |
2612 | DECL_RESULT (decl) = decl_result; |
2613 | DECL_INITIAL (decl) = NULL; | |
2614 | allocate_struct_function (decl, false); | |
2615 | set_cfun (NULL); | |
8be2dc8c | 2616 | |
3f9f4ae7 ML |
2617 | /* Turn alias into thunk and expand it into GIMPLE representation. */ |
2618 | definition = true; | |
e68287df ML |
2619 | |
2620 | memset (&thunk, 0, sizeof (cgraph_thunk_info)); | |
3f9f4ae7 | 2621 | thunk.thunk_p = true; |
10881cff | 2622 | create_edge (target, NULL, count, CGRAPH_FREQ_BASE); |
ce8bdcef | 2623 | callees->can_throw_external = !TREE_NOTHROW (target->decl); |
8be2dc8c | 2624 | |
3f9f4ae7 | 2625 | tree arguments = DECL_ARGUMENTS (decl); |
d862b343 | 2626 | |
3f9f4ae7 ML |
2627 | while (arguments) |
2628 | { | |
2629 | TREE_ADDRESSABLE (arguments) = false; | |
2630 | arguments = TREE_CHAIN (arguments); | |
2631 | } | |
d862b343 | 2632 | |
3f9f4ae7 | 2633 | expand_thunk (false, true); |
8be2dc8c | 2634 | |
3f9f4ae7 ML |
2635 | /* Inline summary set-up. */ |
2636 | analyze (); | |
2637 | inline_analyze_function (this); | |
8be2dc8c | 2638 | } |
711417cd | 2639 | |
7be82279 | 2640 | #include "gt-cgraphunit.h" |