ipa-visibility.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /* IPA visibility pass
  2. Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. /* This file implements two related passes:
  16. - pass_data_ipa_function_and_variable_visibility run just after
  17. symbol table, references and callgraph are built
  18. - pass_data_ipa_function_and_variable_visibility run as first
  19. proper IPA pass (that is after early optimization, or, (with LTO)
  20. as a first pass done at link-time.
  21. Purpose of both passes is to set correctly visibility properties
  22. of all symbols. This includes:
  23. - Symbol privatization:
  24. Some symbols that are declared public by frontend may be
  25. turned local (either by -fwhole-program flag, by linker plugin feedback
  26. or by other reasons)
  27. - Discovery of local functions:
  28. A local function is one whose calls can occur only in the current
  29. compilation unit and all its calls are explicit, so we can change
  30. its calling convention. We simply mark all static functions whose
  31. address is not taken as local.
  32. externally_visible flag is set for symbols that can not be privatized.
  33. For privatized symbols we clear TREE_PUBLIC flag and dismantle comdat
  34. group.
  35. - Dismantling of comdat groups:
  36. Comdat group represent a section that may be replaced by linker by
  37. a different copy of the same section from other unit.
  38. If we have resolution information (from linker plugin) and we know that
  39. a given comdat gorup is prevailing, we can dismantle it and turn symbols
  40. into normal symbols. If the resolution information says that the
  41. section was previaled by copy from non-LTO code, we can also dismantle
  42. it and turn all symbols into external.
  43. - Local aliases:
  44. Some symbols can be interposed by dynamic linker. Refering to these
  45. symbols is expensive, since it needs to be overwritable by the dynamic
  46. linker. In some cases we know that the interposition does not change
  47. semantic and we can always refer to a local copy (as in the case of
  48. inline function). In this case we produce a local alias and redirect
  49. calls to it.
  50. TODO: This should be done for references, too.
  51. - Removal of static ocnstructors and destructors that have no side effects.
  52. - Regularization of several oddities introduced by frontends that may
  53. be impractical later in the optimization queue. */
  54. #include "config.h"
  55. #include "system.h"
  56. #include "coretypes.h"
  57. #include "tm.h"
  58. #include "hash-set.h"
  59. #include "machmode.h"
  60. #include "vec.h"
  61. #include "double-int.h"
  62. #include "input.h"
  63. #include "alias.h"
  64. #include "symtab.h"
  65. #include "wide-int.h"
  66. #include "inchash.h"
  67. #include "tree.h"
  68. #include "hash-map.h"
  69. #include "is-a.h"
  70. #include "plugin-api.h"
  71. #include "hard-reg-set.h"
  72. #include "input.h"
  73. #include "function.h"
  74. #include "ipa-ref.h"
  75. #include "cgraph.h"
  76. #include "tree-pass.h"
  77. #include "calls.h"
  78. #include "gimple-expr.h"
  79. #include "varasm.h"
  80. /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
  81. static bool
  82. non_local_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
  83. {
  84. return !(node->only_called_directly_or_aliased_p ()
  85. /* i386 would need update to output thunk with locak calling
  86. ocnvetions. */
  87. && !node->thunk.thunk_p
  88. && node->definition
  89. && !DECL_EXTERNAL (node->decl)
  90. && !node->externally_visible
  91. && !node->used_from_other_partition
  92. && !node->in_other_partition);
  93. }
  94. /* Return true when function can be marked local. */
  95. bool
  96. cgraph_node::local_p (void)
  97. {
  98. cgraph_node *n = ultimate_alias_target ();
  99. if (n->thunk.thunk_p)
  100. return n->callees->callee->local_p ();
  101. return !n->call_for_symbol_thunks_and_aliases (non_local_p,
  102. NULL, true);
  103. }
  104. /* A helper for comdat_can_be_unshared_p. */
  105. static bool
  106. comdat_can_be_unshared_p_1 (symtab_node *node)
  107. {
  108. if (!node->externally_visible)
  109. return true;
  110. if (node->address_can_be_compared_p ())
  111. {
  112. struct ipa_ref *ref;
  113. for (unsigned int i = 0; node->iterate_referring (i, ref); i++)
  114. if (ref->address_matters_p ())
  115. return false;
  116. }
  117. /* If the symbol is used in some weird way, better to not touch it. */
  118. if (node->force_output)
  119. return false;
  120. /* Explicit instantiations needs to be output when possibly
  121. used externally. */
  122. if (node->forced_by_abi
  123. && TREE_PUBLIC (node->decl)
  124. && (node->resolution != LDPR_PREVAILING_DEF_IRONLY
  125. && !flag_whole_program))
  126. return false;
  127. /* Non-readonly and volatile variables can not be duplicated. */
  128. if (is_a <varpool_node *> (node)
  129. && (!TREE_READONLY (node->decl)
  130. || TREE_THIS_VOLATILE (node->decl)))
  131. return false;
  132. return true;
  133. }
  134. /* COMDAT functions must be shared only if they have address taken,
  135. otherwise we can produce our own private implementation with
  136. -fwhole-program.
  137. Return true when turning COMDAT functoin static can not lead to wrong
  138. code when the resulting object links with a library defining same COMDAT.
  139. Virtual functions do have their addresses taken from the vtables,
  140. but in C++ there is no way to compare their addresses for equality. */
  141. static bool
  142. comdat_can_be_unshared_p (symtab_node *node)
  143. {
  144. if (!comdat_can_be_unshared_p_1 (node))
  145. return false;
  146. if (node->same_comdat_group)
  147. {
  148. symtab_node *next;
  149. /* If more than one function is in the same COMDAT group, it must
  150. be shared even if just one function in the comdat group has
  151. address taken. */
  152. for (next = node->same_comdat_group;
  153. next != node; next = next->same_comdat_group)
  154. if (!comdat_can_be_unshared_p_1 (next))
  155. return false;
  156. }
  157. return true;
  158. }
  159. /* Return true when function NODE should be considered externally visible. */
  160. static bool
  161. cgraph_externally_visible_p (struct cgraph_node *node,
  162. bool whole_program)
  163. {
  164. if (!node->definition)
  165. return false;
  166. if (!TREE_PUBLIC (node->decl)
  167. || DECL_EXTERNAL (node->decl))
  168. return false;
  169. /* Do not try to localize built-in functions yet. One of problems is that we
  170. end up mangling their asm for WHOPR that makes it impossible to call them
  171. using the implicit built-in declarations anymore. Similarly this enables
  172. us to remove them as unreachable before actual calls may appear during
  173. expansion or folding. */
  174. if (DECL_BUILT_IN (node->decl))
  175. return true;
  176. /* If linker counts on us, we must preserve the function. */
  177. if (node->used_from_object_file_p ())
  178. return true;
  179. if (DECL_PRESERVE_P (node->decl))
  180. return true;
  181. if (lookup_attribute ("externally_visible",
  182. DECL_ATTRIBUTES (node->decl)))
  183. return true;
  184. if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
  185. && lookup_attribute ("dllexport",
  186. DECL_ATTRIBUTES (node->decl)))
  187. return true;
  188. if (node->resolution == LDPR_PREVAILING_DEF_IRONLY)
  189. return false;
  190. /* When doing LTO or whole program, we can bring COMDAT functoins static.
  191. This improves code quality and we know we will duplicate them at most twice
  192. (in the case that we are not using plugin and link with object file
  193. implementing same COMDAT) */
  194. if ((in_lto_p || whole_program)
  195. && DECL_COMDAT (node->decl)
  196. && comdat_can_be_unshared_p (node))
  197. return false;
  198. /* When doing link time optimizations, hidden symbols become local. */
  199. if (in_lto_p
  200. && (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
  201. || DECL_VISIBILITY (node->decl) == VISIBILITY_INTERNAL)
  202. /* Be sure that node is defined in IR file, not in other object
  203. file. In that case we don't set used_from_other_object_file. */
  204. && node->definition)
  205. ;
  206. else if (!whole_program)
  207. return true;
  208. if (MAIN_NAME_P (DECL_NAME (node->decl)))
  209. return true;
  210. if (node->instrumentation_clone
  211. && MAIN_NAME_P (DECL_NAME (node->orig_decl)))
  212. return true;
  213. return false;
  214. }
  215. /* Return true when variable should be considered externally visible. */
  216. bool
  217. varpool_node::externally_visible_p (void)
  218. {
  219. if (DECL_EXTERNAL (decl))
  220. return true;
  221. if (!TREE_PUBLIC (decl))
  222. return false;
  223. /* If linker counts on us, we must preserve the function. */
  224. if (used_from_object_file_p ())
  225. return true;
  226. /* Bringing TLS variables local may cause dynamic linker failures
  227. on limits of static TLS vars. */
  228. if (DECL_THREAD_LOCAL_P (decl)
  229. && (DECL_TLS_MODEL (decl) != TLS_MODEL_EMULATED
  230. && DECL_TLS_MODEL (decl) != TLS_MODEL_INITIAL_EXEC))
  231. return true;
  232. if (DECL_HARD_REGISTER (decl))
  233. return true;
  234. if (DECL_PRESERVE_P (decl))
  235. return true;
  236. if (lookup_attribute ("externally_visible",
  237. DECL_ATTRIBUTES (decl)))
  238. return true;
  239. if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
  240. && lookup_attribute ("dllexport",
  241. DECL_ATTRIBUTES (decl)))
  242. return true;
  243. /* See if we have linker information about symbol not being used or
  244. if we need to make guess based on the declaration.
  245. Even if the linker clams the symbol is unused, never bring internal
  246. symbols that are declared by user as used or externally visible.
  247. This is needed for i.e. references from asm statements. */
  248. if (used_from_object_file_p ())
  249. return true;
  250. if (resolution == LDPR_PREVAILING_DEF_IRONLY)
  251. return false;
  252. /* As a special case, the COMDAT virtual tables can be unshared.
  253. In LTO mode turn vtables into static variables. The variable is readonly,
  254. so this does not enable more optimization, but referring static var
  255. is faster for dynamic linking. Also this match logic hidding vtables
  256. from LTO symbol tables. */
  257. if ((in_lto_p || flag_whole_program)
  258. && DECL_COMDAT (decl)
  259. && comdat_can_be_unshared_p (this))
  260. return false;
  261. /* When doing link time optimizations, hidden symbols become local. */
  262. if (in_lto_p
  263. && (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN
  264. || DECL_VISIBILITY (decl) == VISIBILITY_INTERNAL)
  265. /* Be sure that node is defined in IR file, not in other object
  266. file. In that case we don't set used_from_other_object_file. */
  267. && definition)
  268. ;
  269. else if (!flag_whole_program)
  270. return true;
  271. /* Do not attempt to privatize COMDATS by default.
  272. This would break linking with C++ libraries sharing
  273. inline definitions.
  274. FIXME: We can do so for readonly vars with no address taken and
  275. possibly also for vtables since no direct pointer comparsion is done.
  276. It might be interesting to do so to reduce linking overhead. */
  277. if (DECL_COMDAT (decl) || DECL_WEAK (decl))
  278. return true;
  279. return false;
  280. }
  281. /* Return true if reference to NODE can be replaced by a local alias.
  282. Local aliases save dynamic linking overhead and enable more optimizations.
  283. */
  284. bool
  285. can_replace_by_local_alias (symtab_node *node)
  286. {
  287. return (node->get_availability () > AVAIL_INTERPOSABLE
  288. && !decl_binds_to_current_def_p (node->decl)
  289. && !node->can_be_discarded_p ());
  290. }
  291. /* Return true if we can replace refernece to NODE by local alias
  292. within a virtual table. Generally we can replace function pointers
  293. and virtual table pointers. */
  294. bool
  295. can_replace_by_local_alias_in_vtable (symtab_node *node)
  296. {
  297. if (is_a <varpool_node *> (node)
  298. && !DECL_VIRTUAL_P (node->decl))
  299. return false;
  300. return can_replace_by_local_alias (node);
  301. }
  302. /* walk_tree callback that rewrites initializer references. */
  303. static tree
  304. update_vtable_references (tree *tp, int *walk_subtrees,
  305. void *data ATTRIBUTE_UNUSED)
  306. {
  307. if (TREE_CODE (*tp) == VAR_DECL
  308. || TREE_CODE (*tp) == FUNCTION_DECL)
  309. {
  310. if (can_replace_by_local_alias_in_vtable (symtab_node::get (*tp)))
  311. *tp = symtab_node::get (*tp)->noninterposable_alias ()->decl;
  312. *walk_subtrees = 0;
  313. }
  314. else if (IS_TYPE_OR_DECL_P (*tp))
  315. *walk_subtrees = 0;
  316. return NULL;
  317. }
  318. /* In LTO we can remove COMDAT groups and weak symbols.
  319. Either turn them into normal symbols or external symbol depending on
  320. resolution info. */
  321. static void
  322. update_visibility_by_resolution_info (symtab_node * node)
  323. {
  324. bool define;
  325. if (!node->externally_visible
  326. || (!DECL_WEAK (node->decl) && !DECL_ONE_ONLY (node->decl))
  327. || node->resolution == LDPR_UNKNOWN)
  328. return;
  329. define = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
  330. || node->resolution == LDPR_PREVAILING_DEF
  331. || node->resolution == LDPR_UNDEF
  332. || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
  333. /* The linker decisions ought to agree in the whole group. */
  334. if (node->same_comdat_group)
  335. for (symtab_node *next = node->same_comdat_group;
  336. next != node; next = next->same_comdat_group)
  337. {
  338. if (!next->externally_visible)
  339. continue;
  340. bool same_def
  341. = define == (next->resolution == LDPR_PREVAILING_DEF_IRONLY
  342. || next->resolution == LDPR_PREVAILING_DEF
  343. || next->resolution == LDPR_UNDEF
  344. || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
  345. gcc_assert (in_lto_p || same_def);
  346. if (!same_def)
  347. return;
  348. }
  349. if (node->same_comdat_group)
  350. for (symtab_node *next = node->same_comdat_group;
  351. next != node; next = next->same_comdat_group)
  352. {
  353. next->set_comdat_group (NULL);
  354. DECL_WEAK (next->decl) = false;
  355. if (next->externally_visible
  356. && !define)
  357. DECL_EXTERNAL (next->decl) = true;
  358. }
  359. node->set_comdat_group (NULL);
  360. DECL_WEAK (node->decl) = false;
  361. if (!define)
  362. DECL_EXTERNAL (node->decl) = true;
  363. node->dissolve_same_comdat_group_list ();
  364. }
  365. /* Decide on visibility of all symbols. */
  366. static unsigned int
  367. function_and_variable_visibility (bool whole_program)
  368. {
  369. struct cgraph_node *node;
  370. varpool_node *vnode;
  371. /* All aliases should be procssed at this point. */
  372. gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
  373. FOR_EACH_FUNCTION (node)
  374. {
  375. int flags = flags_from_decl_or_type (node->decl);
  376. /* Optimize away PURE and CONST constructors and destructors. */
  377. if (optimize
  378. && (flags & (ECF_CONST | ECF_PURE))
  379. && !(flags & ECF_LOOPING_CONST_OR_PURE))
  380. {
  381. DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
  382. DECL_STATIC_DESTRUCTOR (node->decl) = 0;
  383. }
  384. /* Frontends and alias code marks nodes as needed before parsing is finished.
  385. We may end up marking as node external nodes where this flag is meaningless
  386. strip it. */
  387. if (DECL_EXTERNAL (node->decl) || !node->definition)
  388. {
  389. node->force_output = 0;
  390. node->forced_by_abi = 0;
  391. }
  392. /* C++ FE on lack of COMDAT support create local COMDAT functions
  393. (that ought to be shared but can not due to object format
  394. limitations). It is necessary to keep the flag to make rest of C++ FE
  395. happy. Clear the flag here to avoid confusion in middle-end. */
  396. if (DECL_COMDAT (node->decl) && !TREE_PUBLIC (node->decl))
  397. DECL_COMDAT (node->decl) = 0;
  398. /* For external decls stop tracking same_comdat_group. It doesn't matter
  399. what comdat group they are in when they won't be emitted in this TU. */
  400. if (node->same_comdat_group && DECL_EXTERNAL (node->decl))
  401. {
  402. #ifdef ENABLE_CHECKING
  403. symtab_node *n;
  404. for (n = node->same_comdat_group;
  405. n != node;
  406. n = n->same_comdat_group)
  407. /* If at least one of same comdat group functions is external,
  408. all of them have to be, otherwise it is a front-end bug. */
  409. gcc_assert (DECL_EXTERNAL (n->decl));
  410. #endif
  411. node->dissolve_same_comdat_group_list ();
  412. }
  413. gcc_assert ((!DECL_WEAK (node->decl)
  414. && !DECL_COMDAT (node->decl))
  415. || TREE_PUBLIC (node->decl)
  416. || node->weakref
  417. || DECL_EXTERNAL (node->decl));
  418. if (cgraph_externally_visible_p (node, whole_program))
  419. {
  420. gcc_assert (!node->global.inlined_to);
  421. node->externally_visible = true;
  422. }
  423. else
  424. {
  425. node->externally_visible = false;
  426. node->forced_by_abi = false;
  427. }
  428. if (!node->externally_visible
  429. && node->definition && !node->weakref
  430. && !DECL_EXTERNAL (node->decl))
  431. {
  432. gcc_assert (whole_program || in_lto_p
  433. || !TREE_PUBLIC (node->decl));
  434. node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
  435. || node->unique_name
  436. || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
  437. && TREE_PUBLIC (node->decl));
  438. node->resolution = LDPR_PREVAILING_DEF_IRONLY;
  439. if (node->same_comdat_group && TREE_PUBLIC (node->decl))
  440. {
  441. symtab_node *next = node;
  442. /* Set all members of comdat group local. */
  443. if (node->same_comdat_group)
  444. for (next = node->same_comdat_group;
  445. next != node;
  446. next = next->same_comdat_group)
  447. {
  448. next->set_comdat_group (NULL);
  449. if (!next->alias)
  450. next->set_section (NULL);
  451. next->make_decl_local ();
  452. next->unique_name = ((next->resolution == LDPR_PREVAILING_DEF_IRONLY
  453. || next->unique_name
  454. || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
  455. && TREE_PUBLIC (next->decl));
  456. }
  457. /* cgraph_externally_visible_p has already checked all other nodes
  458. in the group and they will all be made local. We need to
  459. dissolve the group at once so that the predicate does not
  460. segfault though. */
  461. node->dissolve_same_comdat_group_list ();
  462. }
  463. if (TREE_PUBLIC (node->decl))
  464. node->set_comdat_group (NULL);
  465. if (DECL_COMDAT (node->decl) && !node->alias)
  466. node->set_section (NULL);
  467. node->make_decl_local ();
  468. }
  469. if (node->thunk.thunk_p
  470. && !node->thunk.add_pointer_bounds_args
  471. && TREE_PUBLIC (node->decl))
  472. {
  473. struct cgraph_node *decl_node = node;
  474. decl_node = decl_node->callees->callee->function_symbol ();
  475. /* Thunks have the same visibility as function they are attached to.
  476. Make sure the C++ front end set this up properly. */
  477. if (DECL_ONE_ONLY (decl_node->decl))
  478. {
  479. gcc_checking_assert (DECL_COMDAT (node->decl)
  480. == DECL_COMDAT (decl_node->decl));
  481. gcc_checking_assert (node->in_same_comdat_group_p (decl_node));
  482. gcc_checking_assert (node->same_comdat_group);
  483. }
  484. node->forced_by_abi = decl_node->forced_by_abi;
  485. if (DECL_EXTERNAL (decl_node->decl))
  486. DECL_EXTERNAL (node->decl) = 1;
  487. }
  488. update_visibility_by_resolution_info (node);
  489. }
  490. FOR_EACH_DEFINED_FUNCTION (node)
  491. {
  492. if (!node->local.local)
  493. node->local.local |= node->local_p ();
  494. /* If we know that function can not be overwritten by a different semantics
  495. and moreover its section can not be discarded, replace all direct calls
  496. by calls to an noninterposable alias. This make dynamic linking
  497. cheaper and enable more optimization.
  498. TODO: We can also update virtual tables. */
  499. if (node->callers
  500. && can_replace_by_local_alias (node))
  501. {
  502. cgraph_node *alias = dyn_cast<cgraph_node *>
  503. (node->noninterposable_alias ());
  504. if (alias && alias != node)
  505. {
  506. while (node->callers)
  507. {
  508. struct cgraph_edge *e = node->callers;
  509. e->redirect_callee (alias);
  510. if (gimple_has_body_p (e->caller->decl))
  511. {
  512. push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
  513. e->redirect_call_stmt_to_callee ();
  514. pop_cfun ();
  515. }
  516. }
  517. }
  518. }
  519. }
  520. FOR_EACH_VARIABLE (vnode)
  521. {
  522. /* weak flag makes no sense on local variables. */
  523. gcc_assert (!DECL_WEAK (vnode->decl)
  524. || vnode->weakref
  525. || TREE_PUBLIC (vnode->decl)
  526. || DECL_EXTERNAL (vnode->decl));
  527. /* In several cases declarations can not be common:
  528. - when declaration has initializer
  529. - when it is in weak
  530. - when it has specific section
  531. - when it resides in non-generic address space.
  532. - if declaration is local, it will get into .local common section
  533. so common flag is not needed. Frontends still produce these in
  534. certain cases, such as for:
  535. static int a __attribute__ ((common))
  536. Canonicalize things here and clear the redundant flag. */
  537. if (DECL_COMMON (vnode->decl)
  538. && (!(TREE_PUBLIC (vnode->decl)
  539. || DECL_EXTERNAL (vnode->decl))
  540. || (DECL_INITIAL (vnode->decl)
  541. && DECL_INITIAL (vnode->decl) != error_mark_node)
  542. || DECL_WEAK (vnode->decl)
  543. || DECL_SECTION_NAME (vnode->decl) != NULL
  544. || ! (ADDR_SPACE_GENERIC_P
  545. (TYPE_ADDR_SPACE (TREE_TYPE (vnode->decl))))))
  546. DECL_COMMON (vnode->decl) = 0;
  547. }
  548. FOR_EACH_DEFINED_VARIABLE (vnode)
  549. {
  550. if (!vnode->definition)
  551. continue;
  552. if (vnode->externally_visible_p ())
  553. vnode->externally_visible = true;
  554. else
  555. {
  556. vnode->externally_visible = false;
  557. vnode->forced_by_abi = false;
  558. }
  559. if (lookup_attribute ("no_reorder",
  560. DECL_ATTRIBUTES (vnode->decl)))
  561. vnode->no_reorder = 1;
  562. if (!vnode->externally_visible
  563. && !vnode->weakref)
  564. {
  565. gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->decl));
  566. vnode->unique_name = ((vnode->resolution == LDPR_PREVAILING_DEF_IRONLY
  567. || vnode->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
  568. && TREE_PUBLIC (vnode->decl));
  569. if (vnode->same_comdat_group && TREE_PUBLIC (vnode->decl))
  570. {
  571. symtab_node *next = vnode;
  572. /* Set all members of comdat group local. */
  573. if (vnode->same_comdat_group)
  574. for (next = vnode->same_comdat_group;
  575. next != vnode;
  576. next = next->same_comdat_group)
  577. {
  578. next->set_comdat_group (NULL);
  579. if (!next->alias)
  580. next->set_section (NULL);
  581. next->make_decl_local ();
  582. next->unique_name = ((next->resolution == LDPR_PREVAILING_DEF_IRONLY
  583. || next->unique_name
  584. || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
  585. && TREE_PUBLIC (next->decl));
  586. }
  587. vnode->dissolve_same_comdat_group_list ();
  588. }
  589. if (TREE_PUBLIC (vnode->decl))
  590. vnode->set_comdat_group (NULL);
  591. if (DECL_COMDAT (vnode->decl) && !vnode->alias)
  592. vnode->set_section (NULL);
  593. vnode->make_decl_local ();
  594. vnode->resolution = LDPR_PREVAILING_DEF_IRONLY;
  595. }
  596. update_visibility_by_resolution_info (vnode);
  597. /* Update virtual tables to point to local aliases where possible. */
  598. if (DECL_VIRTUAL_P (vnode->decl)
  599. && !DECL_EXTERNAL (vnode->decl))
  600. {
  601. int i;
  602. struct ipa_ref *ref;
  603. bool found = false;
  604. /* See if there is something to update. */
  605. for (i = 0; vnode->iterate_referring (i, ref); i++)
  606. if (ref->use == IPA_REF_ADDR
  607. && can_replace_by_local_alias_in_vtable (ref->referred))
  608. {
  609. found = true;
  610. break;
  611. }
  612. if (found)
  613. {
  614. hash_set<tree> visited_nodes;
  615. vnode->get_constructor ();
  616. walk_tree (&DECL_INITIAL (vnode->decl),
  617. update_vtable_references, NULL, &visited_nodes);
  618. vnode->remove_all_references ();
  619. record_references_in_initializer (vnode->decl, false);
  620. }
  621. }
  622. }
  623. if (dump_file)
  624. {
  625. fprintf (dump_file, "\nMarking local functions:");
  626. FOR_EACH_DEFINED_FUNCTION (node)
  627. if (node->local.local)
  628. fprintf (dump_file, " %s", node->name ());
  629. fprintf (dump_file, "\n\n");
  630. fprintf (dump_file, "\nMarking externally visible functions:");
  631. FOR_EACH_DEFINED_FUNCTION (node)
  632. if (node->externally_visible)
  633. fprintf (dump_file, " %s", node->name ());
  634. fprintf (dump_file, "\n\n");
  635. fprintf (dump_file, "\nMarking externally visible variables:");
  636. FOR_EACH_DEFINED_VARIABLE (vnode)
  637. if (vnode->externally_visible)
  638. fprintf (dump_file, " %s", vnode->name ());
  639. fprintf (dump_file, "\n\n");
  640. }
  641. symtab->function_flags_ready = true;
  642. return 0;
  643. }
  644. /* Local function pass handling visibilities. This happens before LTO streaming
  645. so in particular -fwhole-program should be ignored at this level. */
  646. namespace {
  647. const pass_data pass_data_ipa_function_and_variable_visibility =
  648. {
  649. SIMPLE_IPA_PASS, /* type */
  650. "visibility", /* name */
  651. OPTGROUP_NONE, /* optinfo_flags */
  652. TV_CGRAPHOPT, /* tv_id */
  653. 0, /* properties_required */
  654. 0, /* properties_provided */
  655. 0, /* properties_destroyed */
  656. 0, /* todo_flags_start */
  657. ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
  658. };
  659. /* Bring functions local at LTO time with -fwhole-program. */
  660. static unsigned int
  661. whole_program_function_and_variable_visibility (void)
  662. {
  663. function_and_variable_visibility (flag_whole_program);
  664. if (optimize)
  665. ipa_discover_readonly_nonaddressable_vars ();
  666. return 0;
  667. }
  668. } // anon namespace
  669. namespace {
  670. const pass_data pass_data_ipa_whole_program_visibility =
  671. {
  672. IPA_PASS, /* type */
  673. "whole-program", /* name */
  674. OPTGROUP_NONE, /* optinfo_flags */
  675. TV_CGRAPHOPT, /* tv_id */
  676. 0, /* properties_required */
  677. 0, /* properties_provided */
  678. 0, /* properties_destroyed */
  679. 0, /* todo_flags_start */
  680. ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
  681. };
  682. class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
  683. {
  684. public:
  685. pass_ipa_whole_program_visibility (gcc::context *ctxt)
  686. : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility, ctxt,
  687. NULL, /* generate_summary */
  688. NULL, /* write_summary */
  689. NULL, /* read_summary */
  690. NULL, /* write_optimization_summary */
  691. NULL, /* read_optimization_summary */
  692. NULL, /* stmt_fixup */
  693. 0, /* function_transform_todo_flags_start */
  694. NULL, /* function_transform */
  695. NULL) /* variable_transform */
  696. {}
  697. /* opt_pass methods: */
  698. virtual bool gate (function *)
  699. {
  700. /* Do not re-run on ltrans stage. */
  701. return !flag_ltrans;
  702. }
  703. virtual unsigned int execute (function *)
  704. {
  705. return whole_program_function_and_variable_visibility ();
  706. }
  707. }; // class pass_ipa_whole_program_visibility
  708. } // anon namespace
  709. ipa_opt_pass_d *
  710. make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
  711. {
  712. return new pass_ipa_whole_program_visibility (ctxt);
  713. }
  714. class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
  715. {
  716. public:
  717. pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
  718. : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
  719. ctxt)
  720. {}
  721. /* opt_pass methods: */
  722. virtual unsigned int execute (function *)
  723. {
  724. return function_and_variable_visibility (flag_whole_program && !flag_lto);
  725. }
  726. }; // class pass_ipa_function_and_variable_visibility
  727. simple_ipa_opt_pass *
  728. make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
  729. {
  730. return new pass_ipa_function_and_variable_visibility (ctxt);
  731. }