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