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