]> gcc.gnu.org Git - gcc.git/blame - gcc/varpool.c
tree-ssa-pre.c (get_representative_for): Make sure to return the value number of...
[gcc.git] / gcc / varpool.c
CommitLineData
8a4a83ed 1/* Callgraph handling code.
818ab71a 2 Copyright (C) 2003-2016 Free Software Foundation, Inc.
8a4a83ed
JH
3 Contributed by Jan Hubicka
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9dcd6f09 9Software Foundation; either version 3, or (at your option) any later
8a4a83ed
JH
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
8a4a83ed
JH
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
c7131fb2 24#include "backend.h"
957060b5 25#include "target.h"
8a4a83ed 26#include "tree.h"
c7131fb2 27#include "gimple.h"
957060b5
AM
28#include "timevar.h"
29#include "cgraph.h"
30#include "lto-streamer.h"
d8a2d370 31#include "varasm.h"
b8698a0f 32#include "debug.h"
8a4a83ed 33#include "output.h"
2631d4eb
CP
34#include "omp-low.h"
35#include "context.h"
8a4a83ed 36
b1474d30
JH
37const char * const tls_model_names[]={"none", "emulated",
38 "global-dynamic", "local-dynamic",
39 "initial-exec", "local-exec"};
714c800f 40
26e5b0fd
JH
41/* List of hooks triggered on varpool_node events. */
42struct varpool_node_hook_list {
43 varpool_node_hook hook;
44 void *data;
45 struct varpool_node_hook_list *next;
46};
47
26e5b0fd 48/* Register HOOK to be called with DATA on each removed node. */
3dafb85c
ML
49varpool_node_hook_list *
50symbol_table::add_varpool_removal_hook (varpool_node_hook hook, void *data)
26e5b0fd 51{
3dafb85c
ML
52 varpool_node_hook_list *entry;
53 varpool_node_hook_list **ptr = &m_first_varpool_removal_hook;
26e5b0fd 54
3dafb85c 55 entry = (varpool_node_hook_list *) xmalloc (sizeof (*entry));
26e5b0fd
JH
56 entry->hook = hook;
57 entry->data = data;
58 entry->next = NULL;
59 while (*ptr)
60 ptr = &(*ptr)->next;
61 *ptr = entry;
62 return entry;
63}
64
65/* Remove ENTRY from the list of hooks called on removing nodes. */
66void
3dafb85c 67symbol_table::remove_varpool_removal_hook (varpool_node_hook_list *entry)
26e5b0fd 68{
3dafb85c 69 varpool_node_hook_list **ptr = &m_first_varpool_removal_hook;
26e5b0fd
JH
70
71 while (*ptr != entry)
72 ptr = &(*ptr)->next;
73 *ptr = entry->next;
74 free (entry);
75}
76
77/* Call all node removal hooks. */
3dafb85c
ML
78void
79symbol_table::call_varpool_removal_hooks (varpool_node *node)
26e5b0fd 80{
3dafb85c 81 varpool_node_hook_list *entry = m_first_varpool_removal_hook;
26e5b0fd
JH
82 while (entry)
83 {
84 entry->hook (node, entry->data);
85 entry = entry->next;
86 }
87}
88
89/* Register HOOK to be called with DATA on each inserted node. */
3dafb85c
ML
90varpool_node_hook_list *
91symbol_table::add_varpool_insertion_hook (varpool_node_hook hook, void *data)
26e5b0fd 92{
3dafb85c
ML
93 varpool_node_hook_list *entry;
94 varpool_node_hook_list **ptr = &m_first_varpool_insertion_hook;
26e5b0fd 95
3dafb85c 96 entry = (varpool_node_hook_list *) xmalloc (sizeof (*entry));
26e5b0fd
JH
97 entry->hook = hook;
98 entry->data = data;
99 entry->next = NULL;
100 while (*ptr)
101 ptr = &(*ptr)->next;
102 *ptr = entry;
103 return entry;
104}
105
106/* Remove ENTRY from the list of hooks called on inserted nodes. */
107void
3dafb85c 108symbol_table::remove_varpool_insertion_hook (varpool_node_hook_list *entry)
26e5b0fd 109{
3dafb85c 110 varpool_node_hook_list **ptr = &m_first_varpool_insertion_hook;
26e5b0fd
JH
111
112 while (*ptr != entry)
113 ptr = &(*ptr)->next;
114 *ptr = entry->next;
115 free (entry);
116}
117
118/* Call all node insertion hooks. */
119void
3dafb85c 120symbol_table::call_varpool_insertion_hooks (varpool_node *node)
26e5b0fd 121{
3dafb85c 122 varpool_node_hook_list *entry = m_first_varpool_insertion_hook;
26e5b0fd
JH
123 while (entry)
124 {
125 entry->hook (node, entry->data);
126 entry = entry->next;
127 }
128}
129
bbf9ad07
JH
130/* Allocate new callgraph node and insert it into basic data structures. */
131
2c8326a5 132varpool_node *
9041d2e6 133varpool_node::create_empty (void)
bbf9ad07 134{
766090c2 135 varpool_node *node = ggc_cleared_alloc<varpool_node> ();
67348ccc 136 node->type = SYMTAB_VARIABLE;
bbf9ad07
JH
137 return node;
138}
139
8a4a83ed 140/* Return varpool node assigned to DECL. Create new one when needed. */
2c8326a5 141varpool_node *
9041d2e6 142varpool_node::get_create (tree decl)
8a4a83ed 143{
9041d2e6 144 varpool_node *node = varpool_node::get (decl);
bbf9ad07 145 gcc_checking_assert (TREE_CODE (decl) == VAR_DECL);
1ab24192
JH
146 if (node)
147 return node;
8a4a83ed 148
9041d2e6 149 node = varpool_node::create_empty ();
67348ccc 150 node->decl = decl;
1f6be682 151
db397e2e 152 if ((flag_openacc || flag_openmp) && !DECL_EXTERNAL (decl)
1f6be682
IV
153 && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)))
154 {
155 node->offloadable = 1;
1d899da2
TS
156 if (ENABLE_OFFLOADING)
157 {
158 g->have_offload = true;
159 if (!in_lto_p)
160 vec_safe_push (offload_vars, decl);
1d899da2 161 }
1f6be682
IV
162 }
163
d52f5295 164 node->register_symbol ();
8a4a83ed
JH
165 return node;
166}
167
d52f5295
ML
168/* Remove variable from symbol table. */
169
2942c502 170void
d52f5295 171varpool_node::remove (void)
2942c502 172{
3dafb85c 173 symtab->call_varpool_removal_hooks (this);
1c4db829
JH
174 if (lto_file_data)
175 {
176 lto_free_function_in_decl_state_for_node (this);
177 lto_file_data = NULL;
178 }
e70670cf 179
0b83e688 180 /* When streaming we can have multiple nodes associated with decl. */
3dafb85c 181 if (symtab->state == LTO_STREAMING)
006202e8 182 ;
0b83e688
JH
183 /* Keep constructor when it may be used for folding. We remove
184 references to external variables before final compilation. */
d52f5295 185 else if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node
9041d2e6
ML
186 && !ctor_useable_for_folding_p ())
187 remove_initializer ();
65a428db
JH
188
189 unregister ();
d52f5295 190 ggc_free (this);
e70670cf
JH
191}
192
9041d2e6 193/* Remove node initializer when it is no longer needed. */
e70670cf 194void
9041d2e6 195varpool_node::remove_initializer (void)
e70670cf 196{
9041d2e6
ML
197 if (DECL_INITIAL (decl)
198 && !DECL_IN_CONSTANT_POOL (decl)
6649df51 199 /* Keep vtables for BINFO folding. */
9041d2e6 200 && !DECL_VIRTUAL_P (decl)
0bd0d3bc 201 /* FIXME: http://gcc.gnu.org/PR55395 */
ee03e71d
RB
202 && debug_info_level == DINFO_LEVEL_NONE
203 /* When doing declaration merging we have duplicate
204 entries for given decl. Do not attempt to remove
205 the boides, or we will end up remiving
206 wrong one. */
3dafb85c 207 && symtab->state != LTO_STREAMING)
9041d2e6 208 DECL_INITIAL (decl) = error_mark_node;
2942c502
JH
209}
210
9041d2e6 211/* Dump given varpool node to F. */
8a4a83ed 212void
d52f5295 213varpool_node::dump (FILE *f)
8a4a83ed 214{
d52f5295 215 dump_base (f);
8f940ee6 216 fprintf (f, " Availability: %s\n",
3dafb85c 217 symtab->function_flags_ready
9041d2e6 218 ? cgraph_availability_names[get_availability ()]
8a4a83ed 219 : "not-ready");
8f940ee6 220 fprintf (f, " Varpool flags:");
d52f5295 221 if (DECL_INITIAL (decl))
8a4a83ed 222 fprintf (f, " initialized");
d52f5295 223 if (output)
8a4a83ed 224 fprintf (f, " output");
d52f5295 225 if (used_by_single_function)
eb6a09a7 226 fprintf (f, " used-by-single-function");
d5e254e1
IE
227 if (need_bounds_init)
228 fprintf (f, " need-bounds-init");
d52f5295 229 if (TREE_READONLY (decl))
2d6e4603 230 fprintf (f, " read-only");
9041d2e6 231 if (ctor_useable_for_folding_p ())
2d6e4603 232 fprintf (f, " const-value-known");
d52f5295 233 if (writeonly)
6de88c6a 234 fprintf (f, " write-only");
d52f5295 235 if (tls_model)
b1474d30 236 fprintf (f, " tls-%s", tls_model_names [tls_model]);
8a4a83ed
JH
237 fprintf (f, "\n");
238}
239
9041d2e6
ML
240
241/* Dump given varpool node to stderr. */
242void varpool_node::debug (void)
243{
244 varpool_node::dump (stderr);
245}
246
d52f5295 247/* Dump the variable pool to F. */
8a4a83ed 248void
9041d2e6 249varpool_node::dump_varpool (FILE *f)
8a4a83ed 250{
2c8326a5 251 varpool_node *node;
8a4a83ed
JH
252
253 fprintf (f, "variable pool:\n\n");
65c70e6b 254 FOR_EACH_VARIABLE (node)
d52f5295 255 node->dump (f);
8a4a83ed
JH
256}
257
d85478c2
RAE
258/* Dump the variable pool to stderr. */
259
24e47c76 260DEBUG_FUNCTION void
9041d2e6 261varpool_node::debug_varpool (void)
d85478c2
RAE
262{
263 dump_varpool (stderr);
264}
265
8a4a83ed 266/* Given an assembler name, lookup node. */
2c8326a5 267varpool_node *
9041d2e6 268varpool_node::get_for_asmname (tree asmname)
8a4a83ed 269{
3dafb85c 270 if (symtab_node *node = symtab_node::get_for_asmname (asmname))
7de90a6c 271 return dyn_cast <varpool_node *> (node);
e70670cf
JH
272 else
273 return NULL;
8a4a83ed
JH
274}
275
9041d2e6
ML
276/* When doing LTO, read variable's constructor from disk if
277 it is not already present. */
0b83e688
JH
278
279tree
9041d2e6 280varpool_node::get_constructor (void)
0b83e688 281{
3dafb85c 282 lto_file_decl_data *file_data;
0b83e688
JH
283 const char *data, *name;
284 size_t len;
0b83e688 285
9041d2e6 286 if (DECL_INITIAL (decl) != error_mark_node
176ca71c
JJ
287 || !in_lto_p
288 || !lto_file_data)
9041d2e6 289 return DECL_INITIAL (decl);
0b83e688 290
917dd9bf
JH
291 timevar_push (TV_IPA_LTO_CTORS_IN);
292
9041d2e6 293 file_data = lto_file_data;
0b83e688
JH
294 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
295
296 /* We may have renamed the declaration, e.g., a static function. */
297 name = lto_get_decl_name_mapping (file_data, name);
ca834876
JH
298 struct lto_in_decl_state *decl_state
299 = lto_get_function_in_decl_state (file_data, decl);
0b83e688
JH
300
301 data = lto_get_section_data (file_data, LTO_section_function_body,
ca834876 302 name, &len, decl_state->compressed);
0b83e688 303 if (!data)
40fecdd6 304 fatal_error (input_location, "%s: section %s is missing",
0b83e688
JH
305 file_data->file_name,
306 name);
307
9041d2e6 308 lto_input_variable_constructor (file_data, this, data);
1c4db829 309 gcc_assert (DECL_INITIAL (decl) != error_mark_node);
0b83e688
JH
310 lto_stats.num_function_bodies++;
311 lto_free_section_data (file_data, LTO_section_function_body, name,
ca834876 312 data, len, decl_state->compressed);
9041d2e6 313 lto_free_function_in_decl_state_for_node (this);
917dd9bf 314 timevar_pop (TV_IPA_LTO_CTORS_IN);
9041d2e6 315 return DECL_INITIAL (decl);
0b83e688
JH
316}
317
9041d2e6 318/* Return true if variable has constructor that can be used for folding. */
0b83e688
JH
319
320bool
9041d2e6 321varpool_node::ctor_useable_for_folding_p (void)
0b83e688 322{
9041d2e6 323 varpool_node *real_node = this;
0b83e688
JH
324
325 if (real_node->alias && real_node->definition)
9041d2e6 326 real_node = ultimate_alias_target ();
0b83e688 327
9041d2e6
ML
328 if (TREE_CODE (decl) == CONST_DECL
329 || DECL_IN_CONSTANT_POOL (decl))
0b83e688 330 return true;
9041d2e6 331 if (TREE_THIS_VOLATILE (decl))
0b83e688
JH
332 return false;
333
334 /* If we do not have a constructor, we can't use it. */
335 if (DECL_INITIAL (real_node->decl) == error_mark_node
336 && !real_node->lto_file_data)
337 return false;
338
7c46e07b
JH
339 /* Avoid attempts to load constructors that was not streamed. */
340 if (flag_ltrans && DECL_INITIAL (real_node->decl) == error_mark_node
341 && real_node->body_removed)
342 return false;
343
0b83e688
JH
344 /* Vtables are defined by their types and must match no matter of interposition
345 rules. */
9041d2e6 346 if (DECL_VIRTUAL_P (decl))
0b83e688
JH
347 {
348 /* The C++ front end creates VAR_DECLs for vtables of typeinfo
349 classes not defined in the current TU so that it can refer
350 to them from typeinfo objects. Avoid returning NULL_TREE. */
351 return DECL_INITIAL (real_node->decl) != NULL;
352 }
353
354 /* Alias of readonly variable is also readonly, since the variable is stored
355 in readonly memory. We also accept readonly aliases of non-readonly
356 locations assuming that user knows what he is asking for. */
9041d2e6 357 if (!TREE_READONLY (decl) && !TREE_READONLY (real_node->decl))
0b83e688
JH
358 return false;
359
360 /* Variables declared 'const' without an initializer
361 have zero as the initializer if they may not be
91bc34a9
JH
362 overridden at link or run time.
363
364 It is actually requirement for C++ compiler to optimize const variables
365 consistently. As a GNU extension, do not enfore this rule for user defined
366 weak variables, so we support interposition on:
367 static const int dummy = 0;
368 extern const int foo __attribute__((__weak__, __alias__("dummy")));
369 */
370 if ((!DECL_INITIAL (real_node->decl)
371 || (DECL_WEAK (decl) && !DECL_COMDAT (decl)))
9041d2e6 372 && (DECL_EXTERNAL (decl) || decl_replaceable_p (decl)))
0b83e688
JH
373 return false;
374
375 /* Variables declared `const' with an initializer are considered
376 to not be overwritable with different initializer by default.
377
378 ??? Previously we behaved so for scalar variables but not for array
379 accesses. */
380 return true;
381}
382
9041d2e6
ML
383/* If DECLARATION is constant variable and its initial value is known
384 (so we can do constant folding), return its constructor (DECL_INITIAL).
385 This may be an expression or NULL when DECL is initialized to 0.
0b83e688
JH
386 Return ERROR_MARK_NODE otherwise.
387
388 In LTO this may actually trigger reading the constructor from disk.
389 For this reason varpool_ctor_useable_for_folding_p should be used when
390 the actual constructor value is not needed. */
155c92a7 391
6a6dac52
JH
392tree
393ctor_for_folding (tree decl)
155c92a7 394{
2c8326a5 395 varpool_node *node, *real_node;
6a6dac52
JH
396 tree real_decl;
397
1d0804d4 398 if (TREE_CODE (decl) != VAR_DECL
6a6dac52
JH
399 && TREE_CODE (decl) != CONST_DECL)
400 return error_mark_node;
64e0f5ff 401
d5e254e1
IE
402 /* Static constant bounds are created to be
403 used instead of constants and therefore
404 do not let folding it. */
405 if (POINTER_BOUNDS_P (decl))
406 return error_mark_node;
407
64e0f5ff
JH
408 if (TREE_CODE (decl) == CONST_DECL
409 || DECL_IN_CONSTANT_POOL (decl))
6a6dac52 410 return DECL_INITIAL (decl);
64e0f5ff 411
6a6dac52
JH
412 if (TREE_THIS_VOLATILE (decl))
413 return error_mark_node;
64e0f5ff 414
6a6dac52 415 /* Do not care about automatic variables. Those are never initialized
a0a98fef 416 anyway, because gimplifier exapnds the code. */
64e0f5ff 417 if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6a6dac52
JH
418 {
419 gcc_assert (!TREE_PUBLIC (decl));
420 return error_mark_node;
421 }
64e0f5ff 422
6a6dac52
JH
423 gcc_assert (TREE_CODE (decl) == VAR_DECL);
424
9041d2e6 425 real_node = node = varpool_node::get (decl);
6a6dac52
JH
426 if (node)
427 {
9041d2e6 428 real_node = node->ultimate_alias_target ();
67348ccc 429 real_decl = real_node->decl;
6a6dac52
JH
430 }
431 else
432 real_decl = decl;
433
434 /* See if we are dealing with alias.
435 In most cases alias is just alternative symbol pointing to a given
436 constructor. This allows us to use interposition rules of DECL
437 constructor of REAL_NODE. However weakrefs are special by being just
438 alternative name of their target (if defined). */
439 if (decl != real_decl)
440 {
441 gcc_assert (!DECL_INITIAL (decl)
fd29c024 442 || (node->alias && node->get_alias_target () == real_node)
6a6dac52 443 || DECL_INITIAL (decl) == error_mark_node);
71e54687 444 while (node->transparent_alias && node->analyzed)
6a6dac52 445 {
9041d2e6 446 node = node->get_alias_target ();
67348ccc 447 decl = node->decl;
6a6dac52
JH
448 }
449 }
450
0b83e688
JH
451 if ((!DECL_VIRTUAL_P (real_decl)
452 || DECL_INITIAL (real_decl) == error_mark_node
453 || !DECL_INITIAL (real_decl))
9041d2e6 454 && (!node || !node->ctor_useable_for_folding_p ()))
6a6dac52 455 return error_mark_node;
155c92a7 456
0b83e688
JH
457 /* OK, we can return constructor. See if we need to fetch it from disk
458 in LTO mode. */
459 if (DECL_INITIAL (real_decl) != error_mark_node
460 || !in_lto_p)
461 return DECL_INITIAL (real_decl);
9041d2e6 462 return real_node->get_constructor ();
155c92a7
JH
463}
464
38877e98 465/* Add the variable DECL to the varpool.
3dafb85c 466 Unlike finalize_decl function is intended to be used
38877e98
ZM
467 by middle end and allows insertion of new variable at arbitrary point
468 of compilation. */
469void
3dafb85c 470varpool_node::add (tree decl)
38877e98 471{
2c8326a5 472 varpool_node *node;
9041d2e6
ML
473 varpool_node::finalize_decl (decl);
474 node = varpool_node::get_create (decl);
3dafb85c 475 symtab->call_varpool_insertion_hooks (node);
9041d2e6 476 if (node->externally_visible_p ())
67348ccc 477 node->externally_visible = true;
0f549a67 478 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
7861b648 479 node->no_reorder = 1;
38877e98
ZM
480}
481
8a4a83ed
JH
482/* Return variable availability. See cgraph.h for description of individual
483 return values. */
484enum availability
f13fe18b 485varpool_node::get_availability (symtab_node *ref)
8a4a83ed 486{
9041d2e6 487 if (!definition)
8a4a83ed 488 return AVAIL_NOT_AVAILABLE;
9041d2e6 489 if (!TREE_PUBLIC (decl))
8a4a83ed 490 return AVAIL_AVAILABLE;
9041d2e6
ML
491 if (DECL_IN_CONSTANT_POOL (decl)
492 || DECL_VIRTUAL_P (decl))
8a41354f 493 return AVAIL_AVAILABLE;
9b21e866 494 if (transparent_alias && definition)
8a41354f
JH
495 {
496 enum availability avail;
497
f13fe18b 498 ultimate_alias_target (&avail, ref);
8a41354f
JH
499 return avail;
500 }
f13fe18b 501 /* If this is a reference from symbol itself and there are no aliases, we
65c74eb2 502 may be sure that the symbol was not interposed by something else because
f13fe18b
JH
503 the symbol itself would be unreachable otherwise. */
504 if ((this == ref && !has_aliases_p ())
505 || (ref && get_comdat_group ()
506 && get_comdat_group () == ref->get_comdat_group ()))
507 return AVAIL_AVAILABLE;
8a4a83ed 508 /* If the variable can be overwritten, return OVERWRITABLE. Takes
e70670cf 509 care of at least one notable extension - the COMDAT variables
8a4a83ed 510 used to share template instantiations in C++. */
9041d2e6
ML
511 if (decl_replaceable_p (decl)
512 || DECL_EXTERNAL (decl))
d52f5295 513 return AVAIL_INTERPOSABLE;
8a4a83ed
JH
514 return AVAIL_AVAILABLE;
515}
516
66058468 517void
9041d2e6 518varpool_node::analyze (void)
8a4a83ed 519{
66058468
JH
520 /* When reading back varpool at LTO time, we re-construct the queue in order
521 to have "needed" list right by inserting all needed nodes into varpool.
522 We however don't want to re-analyze already analyzed nodes. */
9041d2e6 523 if (!analyzed)
8a4a83ed 524 {
3dafb85c 525 gcc_assert (!in_lto_p || symtab->function_flags_ready);
66058468
JH
526 /* Compute the alignment early so function body expanders are
527 already informed about increased alignment. */
528 align_variable (decl, 0);
529 }
9041d2e6
ML
530 if (alias)
531 resolve_alias (varpool_node::get (alias_target));
66058468 532 else if (DECL_INITIAL (decl))
9041d2e6
ML
533 record_references_in_initializer (decl, analyzed);
534 analyzed = true;
8a4a83ed
JH
535}
536
9041d2e6 537/* Assemble thunks and aliases associated to varpool node. */
cd35bcf7 538
9041d2e6
ML
539void
540varpool_node::assemble_aliases (void)
cd35bcf7 541{
3dafb85c 542 ipa_ref *ref;
d122681a 543
9041d2e6 544 FOR_EACH_ALIAS (this, ref)
e55637b7
ML
545 {
546 varpool_node *alias = dyn_cast <varpool_node *> (ref->referring);
71e54687
JH
547 if (!alias->transparent_alias)
548 do_assemble_alias (alias->decl,
549 DECL_ASSEMBLER_NAME (decl));
9041d2e6 550 alias->assemble_aliases ();
e55637b7 551 }
cd35bcf7
JH
552}
553
8a4a83ed 554/* Output one variable, if necessary. Return whether we output it. */
0d6bf48c 555
8a4a83ed 556bool
9041d2e6 557varpool_node::assemble_decl (void)
8a4a83ed 558{
0d6bf48c
JH
559 /* Aliases are outout when their target is produced or by
560 output_weakrefs. */
9041d2e6 561 if (alias)
0d6bf48c
JH
562 return false;
563
564 /* Constant pool is output from RTL land when the reference
565 survive till this level. */
8fc17ddc 566 if (DECL_IN_CONSTANT_POOL (decl) && TREE_ASM_WRITTEN (decl))
0d6bf48c
JH
567 return false;
568
569 /* Decls with VALUE_EXPR should not be in the varpool at all. They
570 are not real variables, but just info for debugging and codegen.
571 Unfortunately at the moment emutls is not updating varpool correctly
572 after turning real vars into value_expr vars. */
573 if (DECL_HAS_VALUE_EXPR_P (decl)
574 && !targetm.have_tls)
575 return false;
576
b5493fb2
JH
577 /* Hard register vars do not need to be output. */
578 if (DECL_HARD_REGISTER (decl))
579 return false;
580
0d6bf48c
JH
581 gcc_checking_assert (!TREE_ASM_WRITTEN (decl)
582 && TREE_CODE (decl) == VAR_DECL
583 && !DECL_HAS_VALUE_EXPR_P (decl));
584
9041d2e6 585 if (!in_other_partition
0d6bf48c 586 && !DECL_EXTERNAL (decl))
8a4a83ed 587 {
9041d2e6 588 get_constructor ();
8a4a83ed 589 assemble_variable (decl, 0, 1, 0);
0d6bf48c 590 gcc_assert (TREE_ASM_WRITTEN (decl));
9041d2e6
ML
591 gcc_assert (definition);
592 assemble_aliases ();
5fc6ae7d
RB
593 /* After the parser has generated debugging information, augment
594 this information with any new location/etc information that may
595 have become available after the compilation proper. */
5fc6ae7d 596 debug_hooks->late_global_decl (decl);
0d6bf48c 597 return true;
8a4a83ed
JH
598 }
599
600 return false;
601}
602
66058468
JH
603/* Add NODE to queue starting at FIRST.
604 The queue is linked via AUX pointers and terminated by pointer to 1. */
605
606static void
2c8326a5 607enqueue_node (varpool_node *node, varpool_node **first)
66058468 608{
67348ccc 609 if (node->aux)
66058468
JH
610 return;
611 gcc_checking_assert (*first);
67348ccc 612 node->aux = *first;
66058468
JH
613 *first = node;
614}
615
8a4a83ed 616/* Optimization of function bodies might've rendered some variables as
66058468
JH
617 unnecessary so we want to avoid these from being compiled. Re-do
618 reachability starting from variables that are either externally visible
619 or was referred from the asm output routines. */
8a4a83ed 620
3dafb85c
ML
621void
622symbol_table::remove_unreferenced_decls (void)
8a4a83ed 623{
2c8326a5
OE
624 varpool_node *next, *node;
625 varpool_node *first = (varpool_node *)(void *)1;
66058468 626 int i;
3dafb85c 627 ipa_ref *ref = NULL;
6e2830c3 628 hash_set<varpool_node *> referenced;
8a4a83ed 629
1da2ed5f 630 if (seen_error ())
8a4a83ed
JH
631 return;
632
3dafb85c
ML
633 if (dump_file)
634 fprintf (dump_file, "Trivially needed variables:");
66058468 635 FOR_EACH_DEFINED_VARIABLE (node)
8a4a83ed 636 {
67348ccc 637 if (node->analyzed
9041d2e6 638 && (!node->can_remove_if_no_refs_p ()
df7705b1
JH
639 /* We just expanded all function bodies. See if any of
640 them needed the variable. */
67348ccc 641 || DECL_RTL_SET_P (node->decl)))
66058468
JH
642 {
643 enqueue_node (node, &first);
3dafb85c
ML
644 if (dump_file)
645 fprintf (dump_file, " %s", node->asm_name ());
66058468
JH
646 }
647 }
2c8326a5 648 while (first != (varpool_node *)(void *)1)
66058468
JH
649 {
650 node = first;
2c8326a5 651 first = (varpool_node *)first->aux;
8a4a83ed 652
67348ccc 653 if (node->same_comdat_group)
66058468 654 {
5e20cdc9 655 symtab_node *next;
67348ccc
DM
656 for (next = node->same_comdat_group;
657 next != node;
658 next = next->same_comdat_group)
5d59b5e1 659 {
7de90a6c 660 varpool_node *vnext = dyn_cast <varpool_node *> (next);
d52f5295 661 if (vnext && vnext->analyzed && !next->comdat_local_p ())
5d59b5e1
LC
662 enqueue_node (vnext, &first);
663 }
66058468 664 }
d122681a 665 for (i = 0; node->iterate_reference (i, ref); i++)
5d59b5e1 666 {
7de90a6c 667 varpool_node *vnode = dyn_cast <varpool_node *> (ref->referred);
5d59b5e1 668 if (vnode
a0a98fef 669 && !vnode->in_other_partition
67348ccc
DM
670 && (!DECL_EXTERNAL (ref->referred->decl)
671 || vnode->alias)
672 && vnode->analyzed)
5d59b5e1 673 enqueue_node (vnode, &first);
a0a98fef 674 else
71e54687 675 {
9b21e866
JH
676 referenced.add (vnode);
677 while (vnode && vnode->alias && vnode->definition)
71e54687 678 {
9b21e866
JH
679 vnode = vnode->get_alias_target ();
680 referenced.add (vnode);
71e54687
JH
681 }
682 }
5d59b5e1 683 }
8a4a83ed 684 }
3dafb85c
ML
685 if (dump_file)
686 fprintf (dump_file, "\nRemoving variables:");
687 for (node = first_defined_variable (); node; node = next)
66058468 688 {
3dafb85c 689 next = next_defined_variable (node);
7861b648 690 if (!node->aux && !node->no_reorder)
66058468 691 {
3dafb85c
ML
692 if (dump_file)
693 fprintf (dump_file, " %s", node->asm_name ());
694 if (referenced.contains(node))
9041d2e6 695 node->remove_initializer ();
a0a98fef 696 else
d52f5295 697 node->remove ();
66058468
JH
698 }
699 }
6e2830c3 700
3dafb85c
ML
701 if (dump_file)
702 fprintf (dump_file, "\n");
8a4a83ed
JH
703}
704
7fece979
JJ
705/* For variables in named sections make sure get_variable_section
706 is called before we switch to those sections. Then section
707 conflicts between read-only and read-only requiring relocations
708 sections can be resolved. */
709void
9041d2e6 710varpool_node::finalize_named_section_flags (void)
7fece979 711{
9041d2e6
ML
712 if (!TREE_ASM_WRITTEN (decl)
713 && !alias
714 && !in_other_partition
715 && !DECL_EXTERNAL (decl)
716 && TREE_CODE (decl) == VAR_DECL
717 && !DECL_HAS_VALUE_EXPR_P (decl)
718 && get_section ())
719 get_variable_section (decl, false);
7fece979
JJ
720}
721
8a4a83ed
JH
722/* Output all variables enqueued to be assembled. */
723bool
3dafb85c 724symbol_table::output_variables (void)
8a4a83ed
JH
725{
726 bool changed = false;
2c8326a5 727 varpool_node *node;
8a4a83ed 728
1da2ed5f 729 if (seen_error ())
8a4a83ed
JH
730 return false;
731
3dafb85c 732 remove_unreferenced_decls ();
65d630d4 733
49ba8180 734 timevar_push (TV_VAROUT);
8a4a83ed 735
0d4b5b86 736 FOR_EACH_VARIABLE (node)
c97de764
JH
737 if (!node->definition
738 && !DECL_HAS_VALUE_EXPR_P (node->decl)
739 && !DECL_HARD_REGISTER (node->decl))
0d4b5b86 740 assemble_undefined_decl (node->decl);
65c70e6b 741 FOR_EACH_DEFINED_VARIABLE (node)
7861b648
AK
742 {
743 /* Handled in output_in_order. */
744 if (node->no_reorder)
745 continue;
746
747 node->finalize_named_section_flags ();
748 }
7fece979 749
66058468 750 FOR_EACH_DEFINED_VARIABLE (node)
7861b648
AK
751 {
752 /* Handled in output_in_order. */
753 if (node->no_reorder)
754 continue;
4a38b02b
IV
755#ifdef ACCEL_COMPILER
756 /* Do not assemble "omp declare target link" vars. */
757 if (DECL_HAS_VALUE_EXPR_P (node->decl)
758 && lookup_attribute ("omp declare target link",
759 DECL_ATTRIBUTES (node->decl)))
760 continue;
761#endif
7861b648
AK
762 if (node->assemble_decl ())
763 changed = true;
764 }
49ba8180 765 timevar_pop (TV_VAROUT);
8a4a83ed
JH
766 return changed;
767}
768
2c71ac78
JM
769/* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
770 Extra name aliases are output whenever DECL is output. */
771
2c8326a5 772varpool_node *
9041d2e6 773varpool_node::create_alias (tree alias, tree decl)
2c71ac78 774{
2c8326a5 775 varpool_node *alias_node;
2c71ac78
JM
776
777 gcc_assert (TREE_CODE (decl) == VAR_DECL);
778 gcc_assert (TREE_CODE (alias) == VAR_DECL);
9041d2e6 779 alias_node = varpool_node::get_create (alias);
67348ccc
DM
780 alias_node->alias = true;
781 alias_node->definition = true;
782 alias_node->alias_target = decl;
08346abd 783 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
71e54687 784 alias_node->weakref = alias_node->transparent_alias = true;
cd35bcf7
JH
785 return alias_node;
786}
2c71ac78 787
cd35bcf7
JH
788/* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
789 Extra name aliases are output whenever DECL is output. */
2c71ac78 790
2c8326a5 791varpool_node *
9041d2e6 792varpool_node::create_extra_name_alias (tree alias, tree decl)
cd35bcf7 793{
2c8326a5 794 varpool_node *alias_node;
2c71ac78 795
cd35bcf7
JH
796#ifndef ASM_OUTPUT_DEF
797 /* If aliases aren't supported by the assembler, fail. */
798 return NULL;
799#endif
9041d2e6 800 alias_node = varpool_node::create_alias (alias, decl);
67348ccc 801 alias_node->cpp_implicit_alias = true;
40a7fe1e
JH
802
803 /* Extra name alias mechanizm creates aliases really late
804 via DECL_ASSEMBLER_NAME mechanizm.
805 This is unfortunate because they are not going through the
806 standard channels. Ensure they get output. */
3dafb85c 807 if (symtab->cpp_implicit_aliases_done)
9041d2e6 808 alias_node->resolve_alias (varpool_node::get_create (decl));
051f8cc6
JH
809 return alias_node;
810}
811
31de7606 812/* Worker for call_for_symbol_and_aliases. */
cd35bcf7
JH
813
814bool
31de7606
JH
815varpool_node::call_for_symbol_and_aliases_1 (bool (*callback) (varpool_node *,
816 void *),
817 void *data,
818 bool include_overwritable)
cd35bcf7 819{
3dafb85c 820 ipa_ref *ref;
cd35bcf7 821
9041d2e6 822 FOR_EACH_ALIAS (this, ref)
e55637b7
ML
823 {
824 varpool_node *alias = dyn_cast <varpool_node *> (ref->referring);
825 if (include_overwritable
9041d2e6 826 || alias->get_availability () > AVAIL_INTERPOSABLE)
31de7606
JH
827 if (alias->call_for_symbol_and_aliases (callback, data,
828 include_overwritable))
e55637b7
ML
829 return true;
830 }
cd35bcf7
JH
831 return false;
832}
This page took 5.527505 seconds and 5 git commands to generate.