ipa-comdats.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /* Localize comdats.
  2. Copyright (C) 2014-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 is very simple pass that looks for static symbols that are used
  16. exlusively by symbol within one comdat group. In this case it makes
  17. sense to bring the symbol itself into the group to avoid dead code
  18. that would arrise when the comdat group from current unit is replaced
  19. by a different copy. Consider for example:
  20. static int q(void)
  21. {
  22. ....
  23. }
  24. inline int t(void)
  25. {
  26. return q();
  27. }
  28. if Q is used only by T, it makes sense to put Q into T's comdat group.
  29. The pass solve simple dataflow across the callgraph trying to prove what
  30. symbols are used exclusively from a given comdat group.
  31. The implementation maintains a queue linked by AUX pointer terminated by
  32. pointer value 1. Lattice values are NULL for TOP, actual comdat group, or
  33. ERROR_MARK_NODE for bottom.
  34. TODO: When symbol is used only by comdat symbols, but from different groups,
  35. it would make sense to produce a new comdat group for it with anonymous name.
  36. TODO2: We can't mix variables and functions within one group. Currently
  37. we just give up on references of symbols of different types. We also should
  38. handle this by anonymous comdat group section. */
  39. #include "config.h"
  40. #include "system.h"
  41. #include "coretypes.h"
  42. #include "tm.h"
  43. #include "hash-set.h"
  44. #include "machmode.h"
  45. #include "vec.h"
  46. #include "double-int.h"
  47. #include "input.h"
  48. #include "alias.h"
  49. #include "symtab.h"
  50. #include "wide-int.h"
  51. #include "inchash.h"
  52. #include "tree.h"
  53. #include "hash-map.h"
  54. #include "is-a.h"
  55. #include "plugin-api.h"
  56. #include "vec.h"
  57. #include "hard-reg-set.h"
  58. #include "input.h"
  59. #include "function.h"
  60. #include "ipa-ref.h"
  61. #include "cgraph.h"
  62. #include "tree-pass.h"
  63. /* Main dataflow loop propagating comdat groups across
  64. the symbol table. All references to SYMBOL are examined
  65. and NEWGROUP is updated accordingly. MAP holds current lattice
  66. values for individual symbols. */
  67. tree
  68. propagate_comdat_group (struct symtab_node *symbol,
  69. tree newgroup, hash_map<symtab_node *, tree> &map)
  70. {
  71. int i;
  72. struct ipa_ref *ref;
  73. /* Walk all references to SYMBOL, recursively dive into aliases. */
  74. for (i = 0;
  75. symbol->iterate_referring (i, ref)
  76. && newgroup != error_mark_node; i++)
  77. {
  78. struct symtab_node *symbol2 = ref->referring;
  79. if (ref->use == IPA_REF_ALIAS)
  80. {
  81. newgroup = propagate_comdat_group (symbol2, newgroup, map);
  82. continue;
  83. }
  84. /* One COMDAT group can not hold both variables and functions at
  85. a same time. For now we just go to BOTTOM, in future we may
  86. invent special comdat groups for this case. */
  87. if (symbol->type != symbol2->type)
  88. {
  89. newgroup = error_mark_node;
  90. break;
  91. }
  92. /* If we see inline clone, its comdat group actually
  93. corresponds to the comdat group of the function it is inlined
  94. to. */
  95. if (cgraph_node * cn = dyn_cast <cgraph_node *> (symbol2))
  96. {
  97. if (cn->global.inlined_to)
  98. symbol2 = cn->global.inlined_to;
  99. }
  100. /* The actual merge operation. */
  101. tree *val2 = map.get (symbol2);
  102. if (val2 && *val2 != newgroup)
  103. {
  104. if (!newgroup)
  105. newgroup = *val2;
  106. else
  107. newgroup = error_mark_node;
  108. }
  109. }
  110. /* If we analyze function, walk also callers. */
  111. cgraph_node *cnode = dyn_cast <cgraph_node *> (symbol);
  112. if (cnode)
  113. for (struct cgraph_edge * edge = cnode->callers;
  114. edge && newgroup != error_mark_node; edge = edge->next_caller)
  115. {
  116. struct symtab_node *symbol2 = edge->caller;
  117. if (cgraph_node * cn = dyn_cast <cgraph_node *> (symbol2))
  118. {
  119. /* Thunks can not call across section boundary. */
  120. if (cn->thunk.thunk_p)
  121. newgroup = propagate_comdat_group (symbol2, newgroup, map);
  122. /* If we see inline clone, its comdat group actually
  123. corresponds to the comdat group of the function it
  124. is inlined to. */
  125. if (cn->global.inlined_to)
  126. symbol2 = cn->global.inlined_to;
  127. }
  128. /* The actual merge operation. */
  129. tree *val2 = map.get (symbol2);
  130. if (val2 && *val2 != newgroup)
  131. {
  132. if (!newgroup)
  133. newgroup = *val2;
  134. else
  135. newgroup = error_mark_node;
  136. }
  137. }
  138. return newgroup;
  139. }
  140. /* Add all references of SYMBOL that are defined into queue started by FIRST
  141. and linked by AUX pointer (unless they are already enqueued).
  142. Walk recursively inlined functions. */
  143. void
  144. enqueue_references (symtab_node **first,
  145. symtab_node *symbol)
  146. {
  147. int i;
  148. struct ipa_ref *ref = NULL;
  149. for (i = 0; symbol->iterate_reference (i, ref); i++)
  150. {
  151. symtab_node *node = ref->referred->ultimate_alias_target ();
  152. /* Always keep thunks in same sections as target function. */
  153. if (is_a <cgraph_node *>(node))
  154. node = dyn_cast <cgraph_node *> (node)->function_symbol ();
  155. if (!node->aux && node->definition)
  156. {
  157. node->aux = *first;
  158. *first = node;
  159. }
  160. }
  161. if (cgraph_node *cnode = dyn_cast <cgraph_node *> (symbol))
  162. {
  163. struct cgraph_edge *edge;
  164. for (edge = cnode->callees; edge; edge = edge->next_callee)
  165. if (!edge->inline_failed)
  166. enqueue_references (first, edge->callee);
  167. else
  168. {
  169. symtab_node *node = edge->callee->ultimate_alias_target ();
  170. /* Always keep thunks in same sections as target function. */
  171. if (is_a <cgraph_node *>(node))
  172. node = dyn_cast <cgraph_node *> (node)->function_symbol ();
  173. if (!node->aux && node->definition)
  174. {
  175. node->aux = *first;
  176. *first = node;
  177. }
  178. }
  179. }
  180. }
  181. /* Set comdat group of SYMBOL to GROUP.
  182. Callback for for_node_and_aliases. */
  183. bool
  184. set_comdat_group (symtab_node *symbol,
  185. void *head_p)
  186. {
  187. symtab_node *head = (symtab_node *)head_p;
  188. gcc_assert (!symbol->get_comdat_group ());
  189. symbol->set_comdat_group (head->get_comdat_group ());
  190. symbol->add_to_same_comdat_group (head);
  191. return false;
  192. }
  193. /* Set comdat group of SYMBOL to GROUP.
  194. Callback for for_node_thunks_and_aliases. */
  195. bool
  196. set_comdat_group_1 (cgraph_node *symbol,
  197. void *head_p)
  198. {
  199. return set_comdat_group (symbol, head_p);
  200. }
  201. /* The actual pass with the main dataflow loop. */
  202. static unsigned int
  203. ipa_comdats (void)
  204. {
  205. hash_map<symtab_node *, tree> map (251);
  206. hash_map<tree, symtab_node *> comdat_head_map (251);
  207. symtab_node *symbol;
  208. bool comdat_group_seen = false;
  209. symtab_node *first = (symtab_node *) (void *) 1;
  210. tree group;
  211. /* Start the dataflow by assigning comdat group to symbols that are in comdat
  212. groups already. All other externally visible symbols must stay, we use
  213. ERROR_MARK_NODE as bottom for the propagation. */
  214. FOR_EACH_DEFINED_SYMBOL (symbol)
  215. if (!symbol->real_symbol_p ())
  216. ;
  217. else if ((group = symbol->get_comdat_group ()) != NULL)
  218. {
  219. map.put (symbol, group);
  220. comdat_head_map.put (group, symbol);
  221. comdat_group_seen = true;
  222. /* Mark the symbol so we won't waste time visiting it for dataflow. */
  223. symbol->aux = (symtab_node *) (void *) 1;
  224. }
  225. /* See symbols that can not be privatized to comdats; that is externally
  226. visible symbols or otherwise used ones. We also do not want to mangle
  227. user section names. */
  228. else if (symbol->externally_visible
  229. || symbol->force_output
  230. || symbol->used_from_other_partition
  231. || TREE_THIS_VOLATILE (symbol->decl)
  232. || symbol->get_section ()
  233. || (TREE_CODE (symbol->decl) == FUNCTION_DECL
  234. && (DECL_STATIC_CONSTRUCTOR (symbol->decl)
  235. || DECL_STATIC_DESTRUCTOR (symbol->decl))))
  236. {
  237. symtab_node *target = symbol->ultimate_alias_target ();
  238. /* Always keep thunks in same sections as target function. */
  239. if (is_a <cgraph_node *>(target))
  240. target = dyn_cast <cgraph_node *> (target)->function_symbol ();
  241. map.put (target, error_mark_node);
  242. /* Mark the symbol so we won't waste time visiting it for dataflow. */
  243. symbol->aux = (symtab_node *) (void *) 1;
  244. }
  245. else
  246. {
  247. /* Enqueue symbol for dataflow. */
  248. symbol->aux = first;
  249. first = symbol;
  250. }
  251. if (!comdat_group_seen)
  252. {
  253. FOR_EACH_DEFINED_SYMBOL (symbol)
  254. symbol->aux = NULL;
  255. return 0;
  256. }
  257. /* The actual dataflow. */
  258. while (first != (void *) 1)
  259. {
  260. tree group = NULL;
  261. tree newgroup, *val;
  262. symbol = first;
  263. first = (symtab_node *)first->aux;
  264. /* Get current lattice value of SYMBOL. */
  265. val = map.get (symbol);
  266. if (val)
  267. group = *val;
  268. /* If it is bottom, there is nothing to do; do not clear AUX
  269. so we won't re-queue the symbol. */
  270. if (group == error_mark_node)
  271. continue;
  272. newgroup = propagate_comdat_group (symbol, group, map);
  273. /* If nothing changed, proceed to next symbol. */
  274. if (newgroup == group)
  275. {
  276. symbol->aux = NULL;
  277. continue;
  278. }
  279. /* Update lattice value and enqueue all references for re-visiting. */
  280. gcc_assert (newgroup);
  281. if (val)
  282. *val = newgroup;
  283. else
  284. map.put (symbol, newgroup);
  285. enqueue_references (&first, symbol);
  286. /* We may need to revisit the symbol unless it is BOTTOM. */
  287. if (newgroup != error_mark_node)
  288. symbol->aux = NULL;
  289. }
  290. /* Finally assign symbols to the sections. */
  291. FOR_EACH_DEFINED_SYMBOL (symbol)
  292. {
  293. struct cgraph_node *fun;
  294. symbol->aux = NULL;
  295. if (!symbol->get_comdat_group ()
  296. && !symbol->alias
  297. && (!(fun = dyn_cast <cgraph_node *> (symbol))
  298. || !fun->thunk.thunk_p)
  299. && symbol->real_symbol_p ())
  300. {
  301. tree *val = map.get (symbol);
  302. /* A NULL here means that SYMBOL is unreachable in the definition
  303. of ipa-comdats. Either ipa-comdats is wrong about this or someone
  304. forgot to cleanup and remove unreachable functions earlier. */
  305. gcc_assert (val);
  306. tree group = *val;
  307. if (group == error_mark_node)
  308. continue;
  309. if (dump_file)
  310. {
  311. fprintf (dump_file, "Localizing symbol\n");
  312. symbol->dump (dump_file);
  313. fprintf (dump_file, "To group: %s\n", IDENTIFIER_POINTER (group));
  314. }
  315. if (is_a <cgraph_node *> (symbol))
  316. dyn_cast <cgraph_node *>(symbol)->call_for_symbol_thunks_and_aliases
  317. (set_comdat_group_1,
  318. *comdat_head_map.get (group),
  319. true);
  320. else
  321. symbol->call_for_symbol_and_aliases
  322. (set_comdat_group,
  323. *comdat_head_map.get (group),
  324. true);
  325. }
  326. }
  327. return 0;
  328. }
  329. namespace {
  330. const pass_data pass_data_ipa_comdats =
  331. {
  332. IPA_PASS, /* type */
  333. "comdats", /* name */
  334. OPTGROUP_NONE, /* optinfo_flags */
  335. TV_IPA_COMDATS, /* tv_id */
  336. 0, /* properties_required */
  337. 0, /* properties_provided */
  338. 0, /* properties_destroyed */
  339. 0, /* todo_flags_start */
  340. 0, /* todo_flags_finish */
  341. };
  342. class pass_ipa_comdats : public ipa_opt_pass_d
  343. {
  344. public:
  345. pass_ipa_comdats (gcc::context *ctxt)
  346. : ipa_opt_pass_d (pass_data_ipa_comdats, ctxt,
  347. NULL, /* generate_summary */
  348. NULL, /* write_summary */
  349. NULL, /* read_summary */
  350. NULL, /* write_optimization_summary */
  351. NULL, /* read_optimization_summary */
  352. NULL, /* stmt_fixup */
  353. 0, /* function_transform_todo_flags_start */
  354. NULL, /* function_transform */
  355. NULL) /* variable_transform */
  356. {}
  357. /* opt_pass methods: */
  358. virtual bool gate (function *);
  359. virtual unsigned int execute (function *) { return ipa_comdats (); }
  360. }; // class pass_ipa_comdats
  361. bool
  362. pass_ipa_comdats::gate (function *)
  363. {
  364. return optimize;
  365. }
  366. } // anon namespace
  367. ipa_opt_pass_d *
  368. make_pass_ipa_comdats (gcc::context *ctxt)
  369. {
  370. return new pass_ipa_comdats (ctxt);
  371. }