]>
Commit | Line | Data |
---|---|---|
1d416bd7 | 1 | /* Callgraph handling code. |
3aea1f79 | 2 | Copyright (C) 2003-2014 Free Software Foundation, Inc. |
1d416bd7 | 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 |
1d416bd7 | 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/>. */ | |
1d416bd7 | 20 | |
21 | #include "config.h" | |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
24 | #include "tm.h" | |
25 | #include "tree.h" | |
9ed99284 | 26 | #include "varasm.h" |
1d416bd7 | 27 | #include "cgraph.h" |
28 | #include "langhooks.h" | |
852f689e | 29 | #include "diagnostic-core.h" |
1d416bd7 | 30 | #include "hashtab.h" |
1d416bd7 | 31 | #include "timevar.h" |
48e1416a | 32 | #include "debug.h" |
1d416bd7 | 33 | #include "target.h" |
34 | #include "output.h" | |
bc61cadb | 35 | #include "gimple-expr.h" |
59dd4830 | 36 | #include "flags.h" |
5c3e0e39 | 37 | #include "pointer-set.h" |
1d416bd7 | 38 | |
332db80a | 39 | /* List of hooks triggered on varpool_node events. */ |
40 | struct varpool_node_hook_list { | |
41 | varpool_node_hook hook; | |
42 | void *data; | |
43 | struct varpool_node_hook_list *next; | |
44 | }; | |
45 | ||
46 | /* List of hooks triggered when a node is removed. */ | |
47 | struct varpool_node_hook_list *first_varpool_node_removal_hook; | |
48 | /* List of hooks triggered when an variable is inserted. */ | |
49 | struct varpool_node_hook_list *first_varpool_variable_insertion_hook; | |
50 | ||
51 | /* Register HOOK to be called with DATA on each removed node. */ | |
52 | struct varpool_node_hook_list * | |
53 | varpool_add_node_removal_hook (varpool_node_hook hook, void *data) | |
54 | { | |
55 | struct varpool_node_hook_list *entry; | |
56 | struct varpool_node_hook_list **ptr = &first_varpool_node_removal_hook; | |
57 | ||
58 | entry = (struct varpool_node_hook_list *) xmalloc (sizeof (*entry)); | |
59 | entry->hook = hook; | |
60 | entry->data = data; | |
61 | entry->next = NULL; | |
62 | while (*ptr) | |
63 | ptr = &(*ptr)->next; | |
64 | *ptr = entry; | |
65 | return entry; | |
66 | } | |
67 | ||
68 | /* Remove ENTRY from the list of hooks called on removing nodes. */ | |
69 | void | |
70 | varpool_remove_node_removal_hook (struct varpool_node_hook_list *entry) | |
71 | { | |
72 | struct varpool_node_hook_list **ptr = &first_varpool_node_removal_hook; | |
73 | ||
74 | while (*ptr != entry) | |
75 | ptr = &(*ptr)->next; | |
76 | *ptr = entry->next; | |
77 | free (entry); | |
78 | } | |
79 | ||
80 | /* Call all node removal hooks. */ | |
81 | static void | |
098f44bc | 82 | varpool_call_node_removal_hooks (varpool_node *node) |
332db80a | 83 | { |
84 | struct varpool_node_hook_list *entry = first_varpool_node_removal_hook; | |
85 | while (entry) | |
86 | { | |
87 | entry->hook (node, entry->data); | |
88 | entry = entry->next; | |
89 | } | |
90 | } | |
91 | ||
92 | /* Register HOOK to be called with DATA on each inserted node. */ | |
93 | struct varpool_node_hook_list * | |
94 | varpool_add_variable_insertion_hook (varpool_node_hook hook, void *data) | |
95 | { | |
96 | struct varpool_node_hook_list *entry; | |
97 | struct varpool_node_hook_list **ptr = &first_varpool_variable_insertion_hook; | |
98 | ||
99 | entry = (struct varpool_node_hook_list *) xmalloc (sizeof (*entry)); | |
100 | entry->hook = hook; | |
101 | entry->data = data; | |
102 | entry->next = NULL; | |
103 | while (*ptr) | |
104 | ptr = &(*ptr)->next; | |
105 | *ptr = entry; | |
106 | return entry; | |
107 | } | |
108 | ||
109 | /* Remove ENTRY from the list of hooks called on inserted nodes. */ | |
110 | void | |
111 | varpool_remove_variable_insertion_hook (struct varpool_node_hook_list *entry) | |
112 | { | |
113 | struct varpool_node_hook_list **ptr = &first_varpool_variable_insertion_hook; | |
114 | ||
115 | while (*ptr != entry) | |
116 | ptr = &(*ptr)->next; | |
117 | *ptr = entry->next; | |
118 | free (entry); | |
119 | } | |
120 | ||
121 | /* Call all node insertion hooks. */ | |
122 | void | |
098f44bc | 123 | varpool_call_variable_insertion_hooks (varpool_node *node) |
332db80a | 124 | { |
125 | struct varpool_node_hook_list *entry = first_varpool_variable_insertion_hook; | |
126 | while (entry) | |
127 | { | |
128 | entry->hook (node, entry->data); | |
129 | entry = entry->next; | |
130 | } | |
131 | } | |
132 | ||
9e9c3e92 | 133 | /* Allocate new callgraph node and insert it into basic data structures. */ |
134 | ||
098f44bc | 135 | varpool_node * |
9e9c3e92 | 136 | varpool_create_empty_node (void) |
137 | { | |
25a27413 | 138 | varpool_node *node = ggc_cleared_alloc<varpool_node> (); |
02774f2d | 139 | node->type = SYMTAB_VARIABLE; |
9e9c3e92 | 140 | return node; |
141 | } | |
142 | ||
1d416bd7 | 143 | /* Return varpool node assigned to DECL. Create new one when needed. */ |
098f44bc | 144 | varpool_node * |
2dc9831f | 145 | varpool_node_for_decl (tree decl) |
1d416bd7 | 146 | { |
098f44bc | 147 | varpool_node *node = varpool_get_node (decl); |
9e9c3e92 | 148 | gcc_checking_assert (TREE_CODE (decl) == VAR_DECL); |
cfbe30aa | 149 | if (node) |
150 | return node; | |
1d416bd7 | 151 | |
9e9c3e92 | 152 | node = varpool_create_empty_node (); |
02774f2d | 153 | node->decl = decl; |
154 | symtab_register_node (node); | |
1d416bd7 | 155 | return node; |
156 | } | |
157 | ||
0cddb138 | 158 | /* Remove node from the varpool. */ |
159 | void | |
098f44bc | 160 | varpool_remove_node (varpool_node *node) |
0cddb138 | 161 | { |
df8d3e89 | 162 | tree init; |
332db80a | 163 | varpool_call_node_removal_hooks (node); |
02774f2d | 164 | symtab_unregister_node (node); |
15ca8f90 | 165 | |
166 | /* Because we remove references from external functions before final compilation, | |
167 | we may end up removing useful constructors. | |
168 | FIXME: We probably want to trace boundaries better. */ | |
1a8f6e9c | 169 | if (cgraph_state == CGRAPH_LTO_STREAMING) |
170 | ; | |
171 | else if ((init = ctor_for_folding (node->decl)) == error_mark_node) | |
15ca8f90 | 172 | varpool_remove_initializer (node); |
df8d3e89 | 173 | else |
02774f2d | 174 | DECL_INITIAL (node->decl) = init; |
15ca8f90 | 175 | ggc_free (node); |
176 | } | |
177 | ||
178 | /* Renove node initializer when it is no longer needed. */ | |
179 | void | |
098f44bc | 180 | varpool_remove_initializer (varpool_node *node) |
15ca8f90 | 181 | { |
02774f2d | 182 | if (DECL_INITIAL (node->decl) |
183 | && !DECL_IN_CONSTANT_POOL (node->decl) | |
aa419a52 | 184 | /* Keep vtables for BINFO folding. */ |
02774f2d | 185 | && !DECL_VIRTUAL_P (node->decl) |
dc6f13e3 | 186 | /* FIXME: http://gcc.gnu.org/PR55395 */ |
8ceff600 | 187 | && debug_info_level == DINFO_LEVEL_NONE |
188 | /* When doing declaration merging we have duplicate | |
189 | entries for given decl. Do not attempt to remove | |
190 | the boides, or we will end up remiving | |
191 | wrong one. */ | |
192 | && cgraph_state != CGRAPH_LTO_STREAMING) | |
02774f2d | 193 | DECL_INITIAL (node->decl) = error_mark_node; |
0cddb138 | 194 | } |
195 | ||
1d416bd7 | 196 | /* Dump given cgraph node. */ |
197 | void | |
098f44bc | 198 | dump_varpool_node (FILE *f, varpool_node *node) |
1d416bd7 | 199 | { |
02774f2d | 200 | dump_symtab_base (f, node); |
18841b0c | 201 | fprintf (f, " Availability: %s\n", |
1d416bd7 | 202 | cgraph_function_flags_ready |
203 | ? cgraph_availability_names[cgraph_variable_initializer_availability (node)] | |
204 | : "not-ready"); | |
18841b0c | 205 | fprintf (f, " Varpool flags:"); |
02774f2d | 206 | if (DECL_INITIAL (node->decl)) |
1d416bd7 | 207 | fprintf (f, " initialized"); |
1d416bd7 | 208 | if (node->output) |
209 | fprintf (f, " output"); | |
02774f2d | 210 | if (TREE_READONLY (node->decl)) |
33864132 | 211 | fprintf (f, " read-only"); |
02774f2d | 212 | if (ctor_for_folding (node->decl) != error_mark_node) |
33864132 | 213 | fprintf (f, " const-value-known"); |
703ad42c | 214 | if (node->writeonly) |
215 | fprintf (f, " write-only"); | |
1d416bd7 | 216 | fprintf (f, "\n"); |
217 | } | |
218 | ||
219 | /* Dump the variable pool. */ | |
220 | void | |
221 | dump_varpool (FILE *f) | |
222 | { | |
098f44bc | 223 | varpool_node *node; |
1d416bd7 | 224 | |
225 | fprintf (f, "variable pool:\n\n"); | |
7c455d87 | 226 | FOR_EACH_VARIABLE (node) |
1d416bd7 | 227 | dump_varpool_node (f, node); |
228 | } | |
229 | ||
64f3398b | 230 | /* Dump the variable pool to stderr. */ |
231 | ||
4b987fac | 232 | DEBUG_FUNCTION void |
64f3398b | 233 | debug_varpool (void) |
234 | { | |
235 | dump_varpool (stderr); | |
236 | } | |
237 | ||
1d416bd7 | 238 | /* Given an assembler name, lookup node. */ |
098f44bc | 239 | varpool_node * |
1d416bd7 | 240 | varpool_node_for_asm (tree asmname) |
241 | { | |
452659af | 242 | if (symtab_node *node = symtab_node_for_asm (asmname)) |
13cbeaac | 243 | return dyn_cast <varpool_node *> (node); |
15ca8f90 | 244 | else |
245 | return NULL; | |
1d416bd7 | 246 | } |
247 | ||
7ae8b539 | 248 | /* Return if DECL is constant and its initial value is known (so we can do |
df8d3e89 | 249 | constant folding using DECL_INITIAL (decl)). |
250 | Return ERROR_MARK_NODE when value is unknown. */ | |
9d924a14 | 251 | |
df8d3e89 | 252 | tree |
253 | ctor_for_folding (tree decl) | |
9d924a14 | 254 | { |
098f44bc | 255 | varpool_node *node, *real_node; |
df8d3e89 | 256 | tree real_decl; |
257 | ||
93b79643 | 258 | if (TREE_CODE (decl) != VAR_DECL |
df8d3e89 | 259 | && TREE_CODE (decl) != CONST_DECL) |
260 | return error_mark_node; | |
7ae8b539 | 261 | |
262 | if (TREE_CODE (decl) == CONST_DECL | |
263 | || DECL_IN_CONSTANT_POOL (decl)) | |
df8d3e89 | 264 | return DECL_INITIAL (decl); |
7ae8b539 | 265 | |
df8d3e89 | 266 | if (TREE_THIS_VOLATILE (decl)) |
267 | return error_mark_node; | |
7ae8b539 | 268 | |
df8d3e89 | 269 | /* Do not care about automatic variables. Those are never initialized |
5c3e0e39 | 270 | anyway, because gimplifier exapnds the code. */ |
7ae8b539 | 271 | if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl)) |
df8d3e89 | 272 | { |
273 | gcc_assert (!TREE_PUBLIC (decl)); | |
274 | return error_mark_node; | |
275 | } | |
7ae8b539 | 276 | |
df8d3e89 | 277 | gcc_assert (TREE_CODE (decl) == VAR_DECL); |
278 | ||
279 | node = varpool_get_node (decl); | |
280 | if (node) | |
281 | { | |
282 | real_node = varpool_variable_node (node); | |
02774f2d | 283 | real_decl = real_node->decl; |
df8d3e89 | 284 | } |
285 | else | |
286 | real_decl = decl; | |
287 | ||
288 | /* See if we are dealing with alias. | |
289 | In most cases alias is just alternative symbol pointing to a given | |
290 | constructor. This allows us to use interposition rules of DECL | |
291 | constructor of REAL_NODE. However weakrefs are special by being just | |
292 | alternative name of their target (if defined). */ | |
293 | if (decl != real_decl) | |
294 | { | |
295 | gcc_assert (!DECL_INITIAL (decl) | |
296 | || DECL_INITIAL (decl) == error_mark_node); | |
297 | if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))) | |
298 | { | |
299 | node = varpool_alias_target (node); | |
02774f2d | 300 | decl = node->decl; |
df8d3e89 | 301 | } |
302 | } | |
303 | ||
304 | /* Vtables are defined by their types and must match no matter of interposition | |
305 | rules. */ | |
306 | if (DECL_VIRTUAL_P (real_decl)) | |
307 | { | |
308 | gcc_checking_assert (TREE_READONLY (real_decl)); | |
309 | return DECL_INITIAL (real_decl); | |
310 | } | |
311 | ||
ec513397 | 312 | /* If there is no constructor, we have nothing to do. */ |
df8d3e89 | 313 | if (DECL_INITIAL (real_decl) == error_mark_node) |
314 | return error_mark_node; | |
315 | ||
316 | /* Non-readonly alias of readonly variable is also de-facto readonly, | |
317 | because the variable itself is in readonly section. | |
318 | We also honnor READONLY flag on alias assuming that user knows | |
319 | what he is doing. */ | |
320 | if (!TREE_READONLY (decl) && !TREE_READONLY (real_decl)) | |
321 | return error_mark_node; | |
7ae8b539 | 322 | |
9d924a14 | 323 | /* Variables declared 'const' without an initializer |
324 | have zero as the initializer if they may not be | |
325 | overridden at link or run time. */ | |
df8d3e89 | 326 | if (!DECL_INITIAL (real_decl) |
327 | && (DECL_EXTERNAL (decl) || decl_replaceable_p (decl))) | |
328 | return error_mark_node; | |
9d924a14 | 329 | |
330 | /* Variables declared `const' with an initializer are considered | |
331 | to not be overwritable with different initializer by default. | |
332 | ||
333 | ??? Previously we behaved so for scalar variables but not for array | |
334 | accesses. */ | |
df8d3e89 | 335 | return DECL_INITIAL (real_decl); |
9d924a14 | 336 | } |
337 | ||
124f4875 | 338 | /* Add the variable DECL to the varpool. |
339 | Unlike varpool_finalize_decl function is intended to be used | |
340 | by middle end and allows insertion of new variable at arbitrary point | |
341 | of compilation. */ | |
342 | void | |
343 | varpool_add_new_variable (tree decl) | |
344 | { | |
098f44bc | 345 | varpool_node *node; |
124f4875 | 346 | varpool_finalize_decl (decl); |
2dc9831f | 347 | node = varpool_node_for_decl (decl); |
332db80a | 348 | varpool_call_variable_insertion_hooks (node); |
7dfe7ce5 | 349 | if (varpool_externally_visible_p (node)) |
02774f2d | 350 | node->externally_visible = true; |
124f4875 | 351 | } |
352 | ||
1d416bd7 | 353 | /* Return variable availability. See cgraph.h for description of individual |
354 | return values. */ | |
355 | enum availability | |
098f44bc | 356 | cgraph_variable_initializer_availability (varpool_node *node) |
1d416bd7 | 357 | { |
358 | gcc_assert (cgraph_function_flags_ready); | |
02774f2d | 359 | if (!node->definition) |
1d416bd7 | 360 | return AVAIL_NOT_AVAILABLE; |
02774f2d | 361 | if (!TREE_PUBLIC (node->decl)) |
1d416bd7 | 362 | return AVAIL_AVAILABLE; |
02774f2d | 363 | if (DECL_IN_CONSTANT_POOL (node->decl) |
364 | || DECL_VIRTUAL_P (node->decl)) | |
2fe870c5 | 365 | return AVAIL_AVAILABLE; |
02774f2d | 366 | if (node->alias && node->weakref) |
2fe870c5 | 367 | { |
368 | enum availability avail; | |
369 | ||
370 | cgraph_variable_initializer_availability | |
371 | (varpool_variable_node (node, &avail)); | |
372 | return avail; | |
373 | } | |
1d416bd7 | 374 | /* If the variable can be overwritten, return OVERWRITABLE. Takes |
15ca8f90 | 375 | care of at least one notable extension - the COMDAT variables |
1d416bd7 | 376 | used to share template instantiations in C++. */ |
02774f2d | 377 | if (decl_replaceable_p (node->decl) |
378 | || DECL_EXTERNAL (node->decl)) | |
1d416bd7 | 379 | return AVAIL_OVERWRITABLE; |
380 | return AVAIL_AVAILABLE; | |
381 | } | |
382 | ||
ff2a5ada | 383 | void |
098f44bc | 384 | varpool_analyze_node (varpool_node *node) |
1d416bd7 | 385 | { |
02774f2d | 386 | tree decl = node->decl; |
1d416bd7 | 387 | |
ff2a5ada | 388 | /* When reading back varpool at LTO time, we re-construct the queue in order |
389 | to have "needed" list right by inserting all needed nodes into varpool. | |
390 | We however don't want to re-analyze already analyzed nodes. */ | |
02774f2d | 391 | if (!node->analyzed) |
1d416bd7 | 392 | { |
ff2a5ada | 393 | gcc_assert (!in_lto_p || cgraph_function_flags_ready); |
394 | /* Compute the alignment early so function body expanders are | |
395 | already informed about increased alignment. */ | |
396 | align_variable (decl, 0); | |
397 | } | |
02774f2d | 398 | if (node->alias) |
48669653 | 399 | symtab_resolve_alias |
02774f2d | 400 | (node, varpool_get_node (node->alias_target)); |
ff2a5ada | 401 | else if (DECL_INITIAL (decl)) |
02774f2d | 402 | record_references_in_initializer (decl, node->analyzed); |
403 | node->analyzed = true; | |
1d416bd7 | 404 | } |
405 | ||
9d75589a | 406 | /* Assemble thunks and aliases associated to NODE. */ |
e0eaac80 | 407 | |
408 | static void | |
098f44bc | 409 | assemble_aliases (varpool_node *node) |
e0eaac80 | 410 | { |
411 | int i; | |
412 | struct ipa_ref *ref; | |
02774f2d | 413 | for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++) |
e0eaac80 | 414 | if (ref->use == IPA_REF_ALIAS) |
415 | { | |
098f44bc | 416 | varpool_node *alias = ipa_ref_referring_varpool_node (ref); |
02774f2d | 417 | do_assemble_alias (alias->decl, |
418 | DECL_ASSEMBLER_NAME (node->decl)); | |
e0eaac80 | 419 | assemble_aliases (alias); |
420 | } | |
421 | } | |
422 | ||
1d416bd7 | 423 | /* Output one variable, if necessary. Return whether we output it. */ |
3d1c0354 | 424 | |
1d416bd7 | 425 | bool |
098f44bc | 426 | varpool_assemble_decl (varpool_node *node) |
1d416bd7 | 427 | { |
02774f2d | 428 | tree decl = node->decl; |
1d416bd7 | 429 | |
3d1c0354 | 430 | /* Aliases are outout when their target is produced or by |
431 | output_weakrefs. */ | |
02774f2d | 432 | if (node->alias) |
3d1c0354 | 433 | return false; |
434 | ||
435 | /* Constant pool is output from RTL land when the reference | |
436 | survive till this level. */ | |
015a842e | 437 | if (DECL_IN_CONSTANT_POOL (decl) && TREE_ASM_WRITTEN (decl)) |
3d1c0354 | 438 | return false; |
439 | ||
440 | /* Decls with VALUE_EXPR should not be in the varpool at all. They | |
441 | are not real variables, but just info for debugging and codegen. | |
442 | Unfortunately at the moment emutls is not updating varpool correctly | |
443 | after turning real vars into value_expr vars. */ | |
444 | if (DECL_HAS_VALUE_EXPR_P (decl) | |
445 | && !targetm.have_tls) | |
446 | return false; | |
447 | ||
c9aa6453 | 448 | /* Hard register vars do not need to be output. */ |
449 | if (DECL_HARD_REGISTER (decl)) | |
450 | return false; | |
451 | ||
3d1c0354 | 452 | gcc_checking_assert (!TREE_ASM_WRITTEN (decl) |
453 | && TREE_CODE (decl) == VAR_DECL | |
454 | && !DECL_HAS_VALUE_EXPR_P (decl)); | |
455 | ||
02774f2d | 456 | if (!node->in_other_partition |
3d1c0354 | 457 | && !DECL_EXTERNAL (decl)) |
1d416bd7 | 458 | { |
459 | assemble_variable (decl, 0, 1, 0); | |
3d1c0354 | 460 | gcc_assert (TREE_ASM_WRITTEN (decl)); |
02774f2d | 461 | node->definition = true; |
3d1c0354 | 462 | assemble_aliases (node); |
463 | return true; | |
1d416bd7 | 464 | } |
465 | ||
466 | return false; | |
467 | } | |
468 | ||
ff2a5ada | 469 | /* Add NODE to queue starting at FIRST. |
470 | The queue is linked via AUX pointers and terminated by pointer to 1. */ | |
471 | ||
472 | static void | |
098f44bc | 473 | enqueue_node (varpool_node *node, varpool_node **first) |
ff2a5ada | 474 | { |
02774f2d | 475 | if (node->aux) |
ff2a5ada | 476 | return; |
477 | gcc_checking_assert (*first); | |
02774f2d | 478 | node->aux = *first; |
ff2a5ada | 479 | *first = node; |
480 | } | |
481 | ||
1d416bd7 | 482 | /* Optimization of function bodies might've rendered some variables as |
ff2a5ada | 483 | unnecessary so we want to avoid these from being compiled. Re-do |
484 | reachability starting from variables that are either externally visible | |
485 | or was referred from the asm output routines. */ | |
1d416bd7 | 486 | |
cf951b1a | 487 | static void |
1d416bd7 | 488 | varpool_remove_unreferenced_decls (void) |
489 | { | |
098f44bc | 490 | varpool_node *next, *node; |
491 | varpool_node *first = (varpool_node *)(void *)1; | |
ff2a5ada | 492 | int i; |
493 | struct ipa_ref *ref; | |
5c3e0e39 | 494 | struct pointer_set_t *referenced = pointer_set_create (); |
1d416bd7 | 495 | |
852f689e | 496 | if (seen_error ()) |
1d416bd7 | 497 | return; |
498 | ||
ff2a5ada | 499 | if (cgraph_dump_file) |
500 | fprintf (cgraph_dump_file, "Trivially needed variables:"); | |
ff2a5ada | 501 | FOR_EACH_DEFINED_VARIABLE (node) |
1d416bd7 | 502 | { |
02774f2d | 503 | if (node->analyzed |
7410370b | 504 | && (!varpool_can_remove_if_no_refs (node) |
505 | /* We just expanded all function bodies. See if any of | |
506 | them needed the variable. */ | |
02774f2d | 507 | || DECL_RTL_SET_P (node->decl))) |
ff2a5ada | 508 | { |
509 | enqueue_node (node, &first); | |
510 | if (cgraph_dump_file) | |
f1c8b4d7 | 511 | fprintf (cgraph_dump_file, " %s", node->asm_name ()); |
ff2a5ada | 512 | } |
513 | } | |
098f44bc | 514 | while (first != (varpool_node *)(void *)1) |
ff2a5ada | 515 | { |
516 | node = first; | |
098f44bc | 517 | first = (varpool_node *)first->aux; |
1d416bd7 | 518 | |
02774f2d | 519 | if (node->same_comdat_group) |
ff2a5ada | 520 | { |
452659af | 521 | symtab_node *next; |
02774f2d | 522 | for (next = node->same_comdat_group; |
523 | next != node; | |
524 | next = next->same_comdat_group) | |
2dc9831f | 525 | { |
13cbeaac | 526 | varpool_node *vnext = dyn_cast <varpool_node *> (next); |
5c3e0e39 | 527 | if (vnext && vnext->analyzed && !symtab_comdat_local_p (next)) |
2dc9831f | 528 | enqueue_node (vnext, &first); |
529 | } | |
ff2a5ada | 530 | } |
02774f2d | 531 | for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++) |
2dc9831f | 532 | { |
13cbeaac | 533 | varpool_node *vnode = dyn_cast <varpool_node *> (ref->referred); |
2dc9831f | 534 | if (vnode |
5c3e0e39 | 535 | && !vnode->in_other_partition |
02774f2d | 536 | && (!DECL_EXTERNAL (ref->referred->decl) |
537 | || vnode->alias) | |
538 | && vnode->analyzed) | |
2dc9831f | 539 | enqueue_node (vnode, &first); |
5c3e0e39 | 540 | else |
541 | pointer_set_insert (referenced, node); | |
2dc9831f | 542 | } |
1d416bd7 | 543 | } |
ff2a5ada | 544 | if (cgraph_dump_file) |
545 | fprintf (cgraph_dump_file, "\nRemoving variables:"); | |
546 | for (node = varpool_first_defined_variable (); node; node = next) | |
547 | { | |
548 | next = varpool_next_defined_variable (node); | |
02774f2d | 549 | if (!node->aux) |
ff2a5ada | 550 | { |
551 | if (cgraph_dump_file) | |
f1c8b4d7 | 552 | fprintf (cgraph_dump_file, " %s", node->asm_name ()); |
5c3e0e39 | 553 | if (pointer_set_contains (referenced, node)) |
554 | varpool_remove_initializer (node); | |
555 | else | |
556 | varpool_remove_node (node); | |
ff2a5ada | 557 | } |
558 | } | |
5c3e0e39 | 559 | pointer_set_destroy (referenced); |
ff2a5ada | 560 | if (cgraph_dump_file) |
561 | fprintf (cgraph_dump_file, "\n"); | |
1d416bd7 | 562 | } |
563 | ||
91da0f1c | 564 | /* For variables in named sections make sure get_variable_section |
565 | is called before we switch to those sections. Then section | |
566 | conflicts between read-only and read-only requiring relocations | |
567 | sections can be resolved. */ | |
568 | void | |
098f44bc | 569 | varpool_finalize_named_section_flags (varpool_node *node) |
91da0f1c | 570 | { |
02774f2d | 571 | if (!TREE_ASM_WRITTEN (node->decl) |
572 | && !node->alias | |
573 | && !node->in_other_partition | |
574 | && !DECL_EXTERNAL (node->decl) | |
575 | && TREE_CODE (node->decl) == VAR_DECL | |
576 | && !DECL_HAS_VALUE_EXPR_P (node->decl) | |
577 | && DECL_SECTION_NAME (node->decl)) | |
578 | get_variable_section (node->decl, false); | |
91da0f1c | 579 | } |
580 | ||
1d416bd7 | 581 | /* Output all variables enqueued to be assembled. */ |
582 | bool | |
cf951b1a | 583 | varpool_output_variables (void) |
1d416bd7 | 584 | { |
585 | bool changed = false; | |
098f44bc | 586 | varpool_node *node; |
1d416bd7 | 587 | |
852f689e | 588 | if (seen_error ()) |
1d416bd7 | 589 | return false; |
590 | ||
cf951b1a | 591 | varpool_remove_unreferenced_decls (); |
592 | ||
57305941 | 593 | timevar_push (TV_VAROUT); |
1d416bd7 | 594 | |
7c455d87 | 595 | FOR_EACH_DEFINED_VARIABLE (node) |
91da0f1c | 596 | varpool_finalize_named_section_flags (node); |
597 | ||
ff2a5ada | 598 | FOR_EACH_DEFINED_VARIABLE (node) |
599 | if (varpool_assemble_decl (node)) | |
600 | changed = true; | |
57305941 | 601 | timevar_pop (TV_VAROUT); |
1d416bd7 | 602 | return changed; |
603 | } | |
604 | ||
604cde73 | 605 | /* Create a new global variable of type TYPE. */ |
606 | tree | |
607 | add_new_static_var (tree type) | |
608 | { | |
609 | tree new_decl; | |
098f44bc | 610 | varpool_node *new_node; |
604cde73 | 611 | |
134f0957 | 612 | new_decl = create_tmp_var_raw (type, NULL); |
604cde73 | 613 | DECL_NAME (new_decl) = create_tmp_var_name (NULL); |
614 | TREE_READONLY (new_decl) = 0; | |
615 | TREE_STATIC (new_decl) = 1; | |
616 | TREE_USED (new_decl) = 1; | |
617 | DECL_CONTEXT (new_decl) = NULL_TREE; | |
618 | DECL_ABSTRACT (new_decl) = 0; | |
619 | lang_hooks.dup_lang_specific_decl (new_decl); | |
2dc9831f | 620 | new_node = varpool_node_for_decl (new_decl); |
604cde73 | 621 | varpool_finalize_decl (new_decl); |
622 | ||
02774f2d | 623 | return new_node->decl; |
604cde73 | 624 | } |
625 | ||
f7496db6 | 626 | /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful. |
627 | Extra name aliases are output whenever DECL is output. */ | |
628 | ||
098f44bc | 629 | varpool_node * |
e0eaac80 | 630 | varpool_create_variable_alias (tree alias, tree decl) |
f7496db6 | 631 | { |
098f44bc | 632 | varpool_node *alias_node; |
f7496db6 | 633 | |
634 | gcc_assert (TREE_CODE (decl) == VAR_DECL); | |
635 | gcc_assert (TREE_CODE (alias) == VAR_DECL); | |
2dc9831f | 636 | alias_node = varpool_node_for_decl (alias); |
02774f2d | 637 | alias_node->alias = true; |
638 | alias_node->definition = true; | |
639 | alias_node->alias_target = decl; | |
f2526cce | 640 | if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL) |
02774f2d | 641 | alias_node->weakref = true; |
e0eaac80 | 642 | return alias_node; |
643 | } | |
f7496db6 | 644 | |
e0eaac80 | 645 | /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful. |
646 | Extra name aliases are output whenever DECL is output. */ | |
f7496db6 | 647 | |
098f44bc | 648 | varpool_node * |
e0eaac80 | 649 | varpool_extra_name_alias (tree alias, tree decl) |
650 | { | |
098f44bc | 651 | varpool_node *alias_node; |
f7496db6 | 652 | |
e0eaac80 | 653 | #ifndef ASM_OUTPUT_DEF |
654 | /* If aliases aren't supported by the assembler, fail. */ | |
655 | return NULL; | |
656 | #endif | |
657 | alias_node = varpool_create_variable_alias (alias, decl); | |
02774f2d | 658 | alias_node->cpp_implicit_alias = true; |
48669653 | 659 | |
660 | /* Extra name alias mechanizm creates aliases really late | |
661 | via DECL_ASSEMBLER_NAME mechanizm. | |
662 | This is unfortunate because they are not going through the | |
663 | standard channels. Ensure they get output. */ | |
664 | if (cpp_implicit_aliases_done) | |
02774f2d | 665 | symtab_resolve_alias (alias_node, |
666 | varpool_node_for_decl (decl)); | |
9ced88d0 | 667 | return alias_node; |
668 | } | |
669 | ||
9d75589a | 670 | /* Call calback on NODE and aliases associated to NODE. |
e0eaac80 | 671 | When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are |
672 | skipped. */ | |
673 | ||
674 | bool | |
098f44bc | 675 | varpool_for_node_and_aliases (varpool_node *node, |
676 | bool (*callback) (varpool_node *, void *), | |
e0eaac80 | 677 | void *data, |
678 | bool include_overwritable) | |
679 | { | |
680 | int i; | |
681 | struct ipa_ref *ref; | |
682 | ||
683 | if (callback (node, data)) | |
684 | return true; | |
02774f2d | 685 | for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++) |
e0eaac80 | 686 | if (ref->use == IPA_REF_ALIAS) |
687 | { | |
098f44bc | 688 | varpool_node *alias = ipa_ref_referring_varpool_node (ref); |
e0eaac80 | 689 | if (include_overwritable |
690 | || cgraph_variable_initializer_availability (alias) > AVAIL_OVERWRITABLE) | |
691 | if (varpool_for_node_and_aliases (alias, callback, data, | |
692 | include_overwritable)) | |
693 | return true; | |
694 | } | |
695 | return false; | |
696 | } |