varpool.c 23 KB

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