ipa.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  1. /* Basic IPA optimizations and utilities.
  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. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "hash-set.h"
  20. #include "machmode.h"
  21. #include "vec.h"
  22. #include "double-int.h"
  23. #include "input.h"
  24. #include "alias.h"
  25. #include "symtab.h"
  26. #include "options.h"
  27. #include "wide-int.h"
  28. #include "inchash.h"
  29. #include "tree.h"
  30. #include "fold-const.h"
  31. #include "calls.h"
  32. #include "stringpool.h"
  33. #include "predict.h"
  34. #include "basic-block.h"
  35. #include "hash-map.h"
  36. #include "is-a.h"
  37. #include "plugin-api.h"
  38. #include "hard-reg-set.h"
  39. #include "input.h"
  40. #include "function.h"
  41. #include "ipa-ref.h"
  42. #include "cgraph.h"
  43. #include "tree-pass.h"
  44. #include "gimple-expr.h"
  45. #include "gimplify.h"
  46. #include "flags.h"
  47. #include "target.h"
  48. #include "tree-iterator.h"
  49. #include "ipa-utils.h"
  50. #include "alloc-pool.h"
  51. #include "symbol-summary.h"
  52. #include "ipa-prop.h"
  53. #include "ipa-inline.h"
  54. #include "tree-inline.h"
  55. #include "profile.h"
  56. #include "params.h"
  57. #include "internal-fn.h"
  58. #include "tree-ssa-alias.h"
  59. #include "gimple.h"
  60. #include "dbgcnt.h"
  61. /* Return true when NODE has ADDR reference. */
  62. static bool
  63. has_addr_references_p (struct cgraph_node *node,
  64. void *data ATTRIBUTE_UNUSED)
  65. {
  66. int i;
  67. struct ipa_ref *ref = NULL;
  68. for (i = 0; node->iterate_referring (i, ref); i++)
  69. if (ref->use == IPA_REF_ADDR)
  70. return true;
  71. return false;
  72. }
  73. /* Look for all functions inlined to NODE and update their inlined_to pointers
  74. to INLINED_TO. */
  75. static void
  76. update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
  77. {
  78. struct cgraph_edge *e;
  79. for (e = node->callees; e; e = e->next_callee)
  80. if (e->callee->global.inlined_to)
  81. {
  82. e->callee->global.inlined_to = inlined_to;
  83. update_inlined_to_pointer (e->callee, inlined_to);
  84. }
  85. }
  86. /* Add symtab NODE to queue starting at FIRST.
  87. The queue is linked via AUX pointers and terminated by pointer to 1.
  88. We enqueue nodes at two occasions: when we find them reachable or when we find
  89. their bodies needed for further clonning. In the second case we mark them
  90. by pointer to 2 after processing so they are re-queue when they become
  91. reachable. */
  92. static void
  93. enqueue_node (symtab_node *node, symtab_node **first,
  94. hash_set<symtab_node *> *reachable)
  95. {
  96. /* Node is still in queue; do nothing. */
  97. if (node->aux && node->aux != (void *) 2)
  98. return;
  99. /* Node was already processed as unreachable, re-enqueue
  100. only if it became reachable now. */
  101. if (node->aux == (void *)2 && !reachable->contains (node))
  102. return;
  103. node->aux = *first;
  104. *first = node;
  105. }
  106. /* Process references. */
  107. static void
  108. process_references (symtab_node *snode,
  109. symtab_node **first,
  110. bool before_inlining_p,
  111. hash_set<symtab_node *> *reachable)
  112. {
  113. int i;
  114. struct ipa_ref *ref = NULL;
  115. for (i = 0; snode->iterate_reference (i, ref); i++)
  116. {
  117. symtab_node *node = ref->referred;
  118. symtab_node *body = node->ultimate_alias_target ();
  119. if (node->definition && !node->in_other_partition
  120. && ((!DECL_EXTERNAL (node->decl) || node->alias)
  121. || (((before_inlining_p
  122. && ((TREE_CODE (node->decl) != FUNCTION_DECL
  123. && optimize)
  124. || (TREE_CODE (node->decl) == FUNCTION_DECL
  125. && opt_for_fn (body->decl, optimize))
  126. || (symtab->state < IPA_SSA
  127. && lookup_attribute
  128. ("always_inline",
  129. DECL_ATTRIBUTES (body->decl))))))
  130. /* We use variable constructors during late compilation for
  131. constant folding. Keep references alive so partitioning
  132. knows about potential references. */
  133. || (TREE_CODE (node->decl) == VAR_DECL
  134. && flag_wpa
  135. && ctor_for_folding (node->decl)
  136. != error_mark_node))))
  137. {
  138. /* Be sure that we will not optimize out alias target
  139. body. */
  140. if (DECL_EXTERNAL (node->decl)
  141. && node->alias
  142. && before_inlining_p)
  143. reachable->add (body);
  144. reachable->add (node);
  145. }
  146. enqueue_node (node, first, reachable);
  147. }
  148. }
  149. /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
  150. all its potential targets as reachable to permit later inlining if
  151. devirtualization happens. After inlining still keep their declarations
  152. around, so we can devirtualize to a direct call.
  153. Also try to make trivial devirutalization when no or only one target is
  154. possible. */
  155. static void
  156. walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
  157. struct cgraph_edge *edge,
  158. symtab_node **first,
  159. hash_set<symtab_node *> *reachable,
  160. bool before_inlining_p)
  161. {
  162. unsigned int i;
  163. void *cache_token;
  164. bool final;
  165. vec <cgraph_node *>targets
  166. = possible_polymorphic_call_targets
  167. (edge, &final, &cache_token);
  168. if (!reachable_call_targets->add (cache_token))
  169. {
  170. for (i = 0; i < targets.length (); i++)
  171. {
  172. struct cgraph_node *n = targets[i];
  173. /* Do not bother to mark virtual methods in anonymous namespace;
  174. either we will find use of virtual table defining it, or it is
  175. unused. */
  176. if (TREE_CODE (TREE_TYPE (n->decl)) == METHOD_TYPE
  177. && type_in_anonymous_namespace_p
  178. (method_class_type (TREE_TYPE (n->decl))))
  179. continue;
  180. symtab_node *body = n->function_symbol ();
  181. /* Prior inlining, keep alive bodies of possible targets for
  182. devirtualization. */
  183. if (n->definition
  184. && (before_inlining_p
  185. && opt_for_fn (body->decl, optimize)
  186. && opt_for_fn (body->decl, flag_devirtualize)))
  187. {
  188. /* Be sure that we will not optimize out alias target
  189. body. */
  190. if (DECL_EXTERNAL (n->decl)
  191. && n->alias
  192. && before_inlining_p)
  193. reachable->add (body);
  194. reachable->add (n);
  195. }
  196. /* Even after inlining we want to keep the possible targets in the
  197. boundary, so late passes can still produce direct call even if
  198. the chance for inlining is lost. */
  199. enqueue_node (n, first, reachable);
  200. }
  201. }
  202. /* Very trivial devirtualization; when the type is
  203. final or anonymous (so we know all its derivation)
  204. and there is only one possible virtual call target,
  205. make the edge direct. */
  206. if (final)
  207. {
  208. if (targets.length () <= 1 && dbg_cnt (devirt))
  209. {
  210. cgraph_node *target, *node = edge->caller;
  211. if (targets.length () == 1)
  212. target = targets[0];
  213. else
  214. target = cgraph_node::get_create
  215. (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
  216. if (dump_enabled_p ())
  217. {
  218. location_t locus;
  219. if (edge->call_stmt)
  220. locus = gimple_location (edge->call_stmt);
  221. else
  222. locus = UNKNOWN_LOCATION;
  223. dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
  224. "devirtualizing call in %s/%i to %s/%i\n",
  225. edge->caller->name (), edge->caller->order,
  226. target->name (),
  227. target->order);
  228. }
  229. edge = edge->make_direct (target);
  230. if (inline_summaries)
  231. inline_update_overall_summary (node);
  232. else if (edge->call_stmt)
  233. {
  234. edge->redirect_call_stmt_to_callee ();
  235. /* Call to __builtin_unreachable shouldn't be instrumented. */
  236. if (!targets.length ())
  237. gimple_call_set_with_bounds (edge->call_stmt, false);
  238. }
  239. }
  240. }
  241. }
  242. /* Perform reachability analysis and reclaim all unreachable nodes.
  243. The algorithm is basically mark&sweep but with some extra refinements:
  244. - reachable extern inline functions needs special handling; the bodies needs
  245. to stay in memory until inlining in hope that they will be inlined.
  246. After inlining we release their bodies and turn them into unanalyzed
  247. nodes even when they are reachable.
  248. - virtual functions are kept in callgraph even if they seem unreachable in
  249. hope calls to them will be devirtualized.
  250. Again we remove them after inlining. In late optimization some
  251. devirtualization may happen, but it is not important since we won't inline
  252. the call. In theory early opts and IPA should work out all important cases.
  253. - virtual clones needs bodies of their origins for later materialization;
  254. this means that we want to keep the body even if the origin is unreachable
  255. otherwise. To avoid origin from sitting in the callgraph and being
  256. walked by IPA passes, we turn them into unanalyzed nodes with body
  257. defined.
  258. We maintain set of function declaration where body needs to stay in
  259. body_needed_for_clonning
  260. Inline clones represent special case: their declaration match the
  261. declaration of origin and cgraph_remove_node already knows how to
  262. reshape callgraph and preserve body when offline copy of function or
  263. inline clone is being removed.
  264. - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
  265. variables with DECL_INITIAL set. We finalize these and keep reachable
  266. ones around for constant folding purposes. After inlining we however
  267. stop walking their references to let everything static referneced by them
  268. to be removed when it is otherwise unreachable.
  269. We maintain queue of both reachable symbols (i.e. defined symbols that needs
  270. to stay) and symbols that are in boundary (i.e. external symbols referenced
  271. by reachable symbols or origins of clones). The queue is represented
  272. as linked list by AUX pointer terminated by 1.
  273. At the end we keep all reachable symbols. For symbols in boundary we always
  274. turn definition into a declaration, but we may keep function body around
  275. based on body_needed_for_clonning
  276. All symbols that enter the queue have AUX pointer non-zero and are in the
  277. boundary. Pointer set REACHABLE is used to track reachable symbols.
  278. Every symbol can be visited twice - once as part of boundary and once
  279. as real reachable symbol. enqueue_node needs to decide whether the
  280. node needs to be re-queued for second processing. For this purpose
  281. we set AUX pointer of processed symbols in the boundary to constant 2. */
  282. bool
  283. symbol_table::remove_unreachable_nodes (FILE *file)
  284. {
  285. symtab_node *first = (symtab_node *) (void *) 1;
  286. struct cgraph_node *node, *next;
  287. varpool_node *vnode, *vnext;
  288. bool changed = false;
  289. hash_set<symtab_node *> reachable;
  290. hash_set<tree> body_needed_for_clonning;
  291. hash_set<void *> reachable_call_targets;
  292. bool before_inlining_p = symtab->state < (!optimize ? IPA_SSA
  293. : IPA_SSA_AFTER_INLINING);
  294. timevar_push (TV_IPA_UNREACHABLE);
  295. build_type_inheritance_graph ();
  296. if (file)
  297. fprintf (file, "\nReclaiming functions:");
  298. #ifdef ENABLE_CHECKING
  299. FOR_EACH_FUNCTION (node)
  300. gcc_assert (!node->aux);
  301. FOR_EACH_VARIABLE (vnode)
  302. gcc_assert (!vnode->aux);
  303. #endif
  304. /* Mark functions whose bodies are obviously needed.
  305. This is mostly when they can be referenced externally. Inline clones
  306. are special since their declarations are shared with master clone and thus
  307. cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
  308. FOR_EACH_FUNCTION (node)
  309. {
  310. node->used_as_abstract_origin = false;
  311. if (node->definition
  312. && !node->global.inlined_to
  313. && !node->in_other_partition
  314. && !node->can_remove_if_no_direct_calls_and_refs_p ())
  315. {
  316. gcc_assert (!node->global.inlined_to);
  317. reachable.add (node);
  318. enqueue_node (node, &first, &reachable);
  319. }
  320. else
  321. gcc_assert (!node->aux);
  322. }
  323. /* Mark variables that are obviously needed. */
  324. FOR_EACH_DEFINED_VARIABLE (vnode)
  325. if (!vnode->can_remove_if_no_refs_p()
  326. && !vnode->in_other_partition)
  327. {
  328. reachable.add (vnode);
  329. enqueue_node (vnode, &first, &reachable);
  330. }
  331. /* Perform reachability analysis. */
  332. while (first != (symtab_node *) (void *) 1)
  333. {
  334. bool in_boundary_p = !reachable.contains (first);
  335. symtab_node *node = first;
  336. first = (symtab_node *)first->aux;
  337. /* If we are processing symbol in boundary, mark its AUX pointer for
  338. possible later re-processing in enqueue_node. */
  339. if (in_boundary_p)
  340. {
  341. node->aux = (void *)2;
  342. if (node->alias && node->analyzed)
  343. enqueue_node (node->get_alias_target (), &first, &reachable);
  344. }
  345. else
  346. {
  347. if (TREE_CODE (node->decl) == FUNCTION_DECL
  348. && DECL_ABSTRACT_ORIGIN (node->decl))
  349. {
  350. struct cgraph_node *origin_node
  351. = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node->decl));
  352. if (origin_node && !origin_node->used_as_abstract_origin)
  353. {
  354. origin_node->used_as_abstract_origin = true;
  355. gcc_assert (!origin_node->prev_sibling_clone);
  356. gcc_assert (!origin_node->next_sibling_clone);
  357. for (cgraph_node *n = origin_node->clones; n;
  358. n = n->next_sibling_clone)
  359. if (n->decl == DECL_ABSTRACT_ORIGIN (node->decl))
  360. n->used_as_abstract_origin = true;
  361. }
  362. }
  363. /* If any symbol in a comdat group is reachable, force
  364. all externally visible symbols in the same comdat
  365. group to be reachable as well. Comdat-local symbols
  366. can be discarded if all uses were inlined. */
  367. if (node->same_comdat_group)
  368. {
  369. symtab_node *next;
  370. for (next = node->same_comdat_group;
  371. next != node;
  372. next = next->same_comdat_group)
  373. if (!next->comdat_local_p ()
  374. && !reachable.add (next))
  375. enqueue_node (next, &first, &reachable);
  376. }
  377. /* Mark references as reachable. */
  378. process_references (node, &first, before_inlining_p, &reachable);
  379. }
  380. if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
  381. {
  382. /* Mark the callees reachable unless they are direct calls to extern
  383. inline functions we decided to not inline. */
  384. if (!in_boundary_p)
  385. {
  386. struct cgraph_edge *e;
  387. /* Keep alive possible targets for devirtualization. */
  388. if (opt_for_fn (cnode->decl, optimize)
  389. && opt_for_fn (cnode->decl, flag_devirtualize))
  390. {
  391. struct cgraph_edge *next;
  392. for (e = cnode->indirect_calls; e; e = next)
  393. {
  394. next = e->next_callee;
  395. if (e->indirect_info->polymorphic)
  396. walk_polymorphic_call_targets (&reachable_call_targets,
  397. e, &first, &reachable,
  398. before_inlining_p);
  399. }
  400. }
  401. for (e = cnode->callees; e; e = e->next_callee)
  402. {
  403. symtab_node *body = e->callee->function_symbol ();
  404. if (e->callee->definition
  405. && !e->callee->in_other_partition
  406. && (!e->inline_failed
  407. || !DECL_EXTERNAL (e->callee->decl)
  408. || e->callee->alias
  409. || (before_inlining_p
  410. && (opt_for_fn (body->decl, optimize)
  411. || (symtab->state < IPA_SSA
  412. && lookup_attribute
  413. ("always_inline",
  414. DECL_ATTRIBUTES (body->decl)))))))
  415. {
  416. /* Be sure that we will not optimize out alias target
  417. body. */
  418. if (DECL_EXTERNAL (e->callee->decl)
  419. && e->callee->alias
  420. && before_inlining_p)
  421. reachable.add (body);
  422. reachable.add (e->callee);
  423. }
  424. enqueue_node (e->callee, &first, &reachable);
  425. }
  426. /* When inline clone exists, mark body to be preserved so when removing
  427. offline copy of the function we don't kill it. */
  428. if (cnode->global.inlined_to)
  429. body_needed_for_clonning.add (cnode->decl);
  430. /* For instrumentation clones we always need original
  431. function node for proper LTO privatization. */
  432. if (cnode->instrumentation_clone
  433. && cnode->definition)
  434. {
  435. gcc_assert (cnode->instrumented_version || in_lto_p);
  436. if (cnode->instrumented_version)
  437. {
  438. enqueue_node (cnode->instrumented_version, &first,
  439. &reachable);
  440. reachable.add (cnode->instrumented_version);
  441. }
  442. }
  443. /* For non-inline clones, force their origins to the boundary and ensure
  444. that body is not removed. */
  445. while (cnode->clone_of)
  446. {
  447. bool noninline = cnode->clone_of->decl != cnode->decl;
  448. cnode = cnode->clone_of;
  449. if (noninline)
  450. {
  451. body_needed_for_clonning.add (cnode->decl);
  452. enqueue_node (cnode, &first, &reachable);
  453. }
  454. }
  455. }
  456. else if (cnode->thunk.thunk_p)
  457. enqueue_node (cnode->callees->callee, &first, &reachable);
  458. /* If any reachable function has simd clones, mark them as
  459. reachable as well. */
  460. if (cnode->simd_clones)
  461. {
  462. cgraph_node *next;
  463. for (next = cnode->simd_clones;
  464. next;
  465. next = next->simdclone->next_clone)
  466. if (in_boundary_p
  467. || !reachable.add (next))
  468. enqueue_node (next, &first, &reachable);
  469. }
  470. }
  471. /* When we see constructor of external variable, keep referred nodes in the
  472. boundary. This will also hold initializers of the external vars NODE
  473. refers to. */
  474. varpool_node *vnode = dyn_cast <varpool_node *> (node);
  475. if (vnode
  476. && DECL_EXTERNAL (node->decl)
  477. && !vnode->alias
  478. && in_boundary_p)
  479. {
  480. struct ipa_ref *ref = NULL;
  481. for (int i = 0; node->iterate_reference (i, ref); i++)
  482. enqueue_node (ref->referred, &first, &reachable);
  483. }
  484. }
  485. /* Remove unreachable functions. */
  486. for (node = first_function (); node; node = next)
  487. {
  488. next = next_function (node);
  489. /* If node is not needed at all, remove it. */
  490. if (!node->aux)
  491. {
  492. if (file)
  493. fprintf (file, " %s/%i", node->name (), node->order);
  494. node->remove ();
  495. changed = true;
  496. }
  497. /* If node is unreachable, remove its body. */
  498. else if (!reachable.contains (node))
  499. {
  500. /* We keep definitions of thunks and aliases in the boundary so
  501. we can walk to the ultimate alias targets and function symbols
  502. reliably. */
  503. if (node->alias || node->thunk.thunk_p)
  504. ;
  505. else if (!body_needed_for_clonning.contains (node->decl)
  506. && !node->alias && !node->thunk.thunk_p)
  507. node->release_body ();
  508. else if (!node->clone_of)
  509. gcc_assert (in_lto_p || DECL_RESULT (node->decl));
  510. if (node->definition && !node->alias && !node->thunk.thunk_p)
  511. {
  512. if (file)
  513. fprintf (file, " %s/%i", node->name (), node->order);
  514. node->body_removed = true;
  515. node->analyzed = false;
  516. node->definition = false;
  517. node->cpp_implicit_alias = false;
  518. node->alias = false;
  519. node->thunk.thunk_p = false;
  520. node->weakref = false;
  521. /* After early inlining we drop always_inline attributes on
  522. bodies of functions that are still referenced (have their
  523. address taken). */
  524. DECL_ATTRIBUTES (node->decl)
  525. = remove_attribute ("always_inline",
  526. DECL_ATTRIBUTES (node->decl));
  527. if (!node->in_other_partition)
  528. node->local.local = false;
  529. node->remove_callees ();
  530. node->remove_all_references ();
  531. changed = true;
  532. if (node->thunk.thunk_p
  533. && node->thunk.add_pointer_bounds_args)
  534. {
  535. node->thunk.thunk_p = false;
  536. node->thunk.add_pointer_bounds_args = false;
  537. }
  538. }
  539. }
  540. else
  541. gcc_assert (node->clone_of || !node->has_gimple_body_p ()
  542. || in_lto_p || DECL_RESULT (node->decl));
  543. }
  544. /* Inline clones might be kept around so their materializing allows further
  545. cloning. If the function the clone is inlined into is removed, we need
  546. to turn it into normal cone. */
  547. FOR_EACH_FUNCTION (node)
  548. {
  549. if (node->global.inlined_to
  550. && !node->callers)
  551. {
  552. gcc_assert (node->clones);
  553. node->global.inlined_to = NULL;
  554. update_inlined_to_pointer (node, node);
  555. }
  556. node->aux = NULL;
  557. }
  558. /* Remove unreachable variables. */
  559. if (file)
  560. fprintf (file, "\nReclaiming variables:");
  561. for (vnode = first_variable (); vnode; vnode = vnext)
  562. {
  563. vnext = next_variable (vnode);
  564. if (!vnode->aux
  565. /* For can_refer_decl_in_current_unit_p we want to track for
  566. all external variables if they are defined in other partition
  567. or not. */
  568. && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
  569. {
  570. struct ipa_ref *ref = NULL;
  571. /* First remove the aliases, so varpool::remove can possibly lookup
  572. the constructor and save it for future use. */
  573. while (vnode->iterate_direct_aliases (0, ref))
  574. {
  575. if (file)
  576. fprintf (file, " %s/%i", ref->referred->name (),
  577. ref->referred->order);
  578. ref->referring->remove ();
  579. }
  580. if (file)
  581. fprintf (file, " %s/%i", vnode->name (), vnode->order);
  582. vnext = next_variable (vnode);
  583. vnode->remove ();
  584. changed = true;
  585. }
  586. else if (!reachable.contains (vnode) && !vnode->alias)
  587. {
  588. tree init;
  589. if (vnode->definition)
  590. {
  591. if (file)
  592. fprintf (file, " %s", vnode->name ());
  593. changed = true;
  594. }
  595. /* Keep body if it may be useful for constant folding. */
  596. if ((init = ctor_for_folding (vnode->decl)) == error_mark_node
  597. && !POINTER_BOUNDS_P (vnode->decl))
  598. vnode->remove_initializer ();
  599. else
  600. DECL_INITIAL (vnode->decl) = init;
  601. vnode->body_removed = true;
  602. vnode->definition = false;
  603. vnode->analyzed = false;
  604. vnode->aux = NULL;
  605. vnode->remove_from_same_comdat_group ();
  606. vnode->remove_all_references ();
  607. }
  608. else
  609. vnode->aux = NULL;
  610. }
  611. /* Now update address_taken flags and try to promote functions to be local. */
  612. if (file)
  613. fprintf (file, "\nClearing address taken flags:");
  614. FOR_EACH_DEFINED_FUNCTION (node)
  615. if (node->address_taken
  616. && !node->used_from_other_partition)
  617. {
  618. if (!node->call_for_symbol_and_aliases
  619. (has_addr_references_p, NULL, true)
  620. && (!node->instrumentation_clone
  621. || !node->instrumented_version
  622. || !node->instrumented_version->address_taken))
  623. {
  624. if (file)
  625. fprintf (file, " %s", node->name ());
  626. node->address_taken = false;
  627. changed = true;
  628. if (node->local_p ())
  629. {
  630. node->local.local = true;
  631. if (file)
  632. fprintf (file, " (local)");
  633. }
  634. }
  635. }
  636. if (file)
  637. fprintf (file, "\n");
  638. #ifdef ENABLE_CHECKING
  639. symtab_node::verify_symtab_nodes ();
  640. #endif
  641. /* If we removed something, perhaps profile could be improved. */
  642. if (changed && optimize && inline_edge_summary_vec.exists ())
  643. FOR_EACH_DEFINED_FUNCTION (node)
  644. ipa_propagate_frequency (node);
  645. timevar_pop (TV_IPA_UNREACHABLE);
  646. return changed;
  647. }
  648. /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
  649. as needed, also clear EXPLICIT_REFS if the references to given variable
  650. do not need to be explicit. */
  651. void
  652. process_references (varpool_node *vnode,
  653. bool *written, bool *address_taken,
  654. bool *read, bool *explicit_refs)
  655. {
  656. int i;
  657. struct ipa_ref *ref;
  658. if (!vnode->all_refs_explicit_p ()
  659. || TREE_THIS_VOLATILE (vnode->decl))
  660. *explicit_refs = false;
  661. for (i = 0; vnode->iterate_referring (i, ref)
  662. && *explicit_refs && (!*written || !*address_taken || !*read); i++)
  663. switch (ref->use)
  664. {
  665. case IPA_REF_ADDR:
  666. *address_taken = true;
  667. break;
  668. case IPA_REF_LOAD:
  669. *read = true;
  670. break;
  671. case IPA_REF_STORE:
  672. *written = true;
  673. break;
  674. case IPA_REF_ALIAS:
  675. process_references (dyn_cast<varpool_node *> (ref->referring), written,
  676. address_taken, read, explicit_refs);
  677. break;
  678. case IPA_REF_CHKP:
  679. gcc_unreachable ();
  680. }
  681. }
  682. /* Set TREE_READONLY bit. */
  683. bool
  684. set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
  685. {
  686. TREE_READONLY (vnode->decl) = true;
  687. return false;
  688. }
  689. /* Set writeonly bit and clear the initalizer, since it will not be needed. */
  690. bool
  691. set_writeonly_bit (varpool_node *vnode, void *data)
  692. {
  693. vnode->writeonly = true;
  694. if (optimize)
  695. {
  696. DECL_INITIAL (vnode->decl) = NULL;
  697. if (!vnode->alias)
  698. {
  699. if (vnode->num_references ())
  700. *(bool *)data = true;
  701. vnode->remove_all_references ();
  702. }
  703. }
  704. return false;
  705. }
  706. /* Clear addressale bit of VNODE. */
  707. bool
  708. clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
  709. {
  710. vnode->address_taken = false;
  711. TREE_ADDRESSABLE (vnode->decl) = 0;
  712. return false;
  713. }
  714. /* Discover variables that have no longer address taken or that are read only
  715. and update their flags.
  716. Return true when unreachable symbol removan should be done.
  717. FIXME: This can not be done in between gimplify and omp_expand since
  718. readonly flag plays role on what is shared and what is not. Currently we do
  719. this transformation as part of whole program visibility and re-do at
  720. ipa-reference pass (to take into account clonning), but it would
  721. make sense to do it before early optimizations. */
  722. bool
  723. ipa_discover_readonly_nonaddressable_vars (void)
  724. {
  725. bool remove_p = false;
  726. varpool_node *vnode;
  727. if (dump_file)
  728. fprintf (dump_file, "Clearing variable flags:");
  729. FOR_EACH_VARIABLE (vnode)
  730. if (!vnode->alias
  731. && (TREE_ADDRESSABLE (vnode->decl)
  732. || !vnode->writeonly
  733. || !TREE_READONLY (vnode->decl)))
  734. {
  735. bool written = false;
  736. bool address_taken = false;
  737. bool read = false;
  738. bool explicit_refs = true;
  739. process_references (vnode, &written, &address_taken, &read,
  740. &explicit_refs);
  741. if (!explicit_refs)
  742. continue;
  743. if (!address_taken)
  744. {
  745. if (TREE_ADDRESSABLE (vnode->decl) && dump_file)
  746. fprintf (dump_file, " %s (non-addressable)", vnode->name ());
  747. vnode->call_for_symbol_and_aliases (clear_addressable_bit, NULL,
  748. true);
  749. }
  750. if (!address_taken && !written
  751. /* Making variable in explicit section readonly can cause section
  752. type conflict.
  753. See e.g. gcc.c-torture/compile/pr23237.c */
  754. && vnode->get_section () == NULL)
  755. {
  756. if (!TREE_READONLY (vnode->decl) && dump_file)
  757. fprintf (dump_file, " %s (read-only)", vnode->name ());
  758. vnode->call_for_symbol_and_aliases (set_readonly_bit, NULL, true);
  759. }
  760. if (!vnode->writeonly && !read && !address_taken && written)
  761. {
  762. if (dump_file)
  763. fprintf (dump_file, " %s (write-only)", vnode->name ());
  764. vnode->call_for_symbol_and_aliases (set_writeonly_bit, &remove_p,
  765. true);
  766. }
  767. }
  768. if (dump_file)
  769. fprintf (dump_file, "\n");
  770. return remove_p;
  771. }
  772. /* Free inline summary. */
  773. namespace {
  774. const pass_data pass_data_ipa_free_inline_summary =
  775. {
  776. SIMPLE_IPA_PASS, /* type */
  777. "free-inline-summary", /* name */
  778. OPTGROUP_NONE, /* optinfo_flags */
  779. TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
  780. 0, /* properties_required */
  781. 0, /* properties_provided */
  782. 0, /* properties_destroyed */
  783. 0, /* todo_flags_start */
  784. /* Early optimizations may make function unreachable. We can not
  785. remove unreachable functions as part of the ealry opts pass because
  786. TODOs are run before subpasses. Do it here. */
  787. ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
  788. };
  789. class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
  790. {
  791. public:
  792. pass_ipa_free_inline_summary (gcc::context *ctxt)
  793. : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
  794. {}
  795. /* opt_pass methods: */
  796. virtual unsigned int execute (function *)
  797. {
  798. inline_free_summary ();
  799. return 0;
  800. }
  801. }; // class pass_ipa_free_inline_summary
  802. } // anon namespace
  803. simple_ipa_opt_pass *
  804. make_pass_ipa_free_inline_summary (gcc::context *ctxt)
  805. {
  806. return new pass_ipa_free_inline_summary (ctxt);
  807. }
  808. /* Generate and emit a static constructor or destructor. WHICH must
  809. be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
  810. (for chp static vars constructor) or 'B' (for chkp static bounds
  811. constructor). BODY is a STATEMENT_LIST containing GENERIC
  812. statements. PRIORITY is the initialization priority for this
  813. constructor or destructor.
  814. FINAL specify whether the externally visible name for collect2 should
  815. be produced. */
  816. static void
  817. cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
  818. {
  819. static int counter = 0;
  820. char which_buf[16];
  821. tree decl, name, resdecl;
  822. /* The priority is encoded in the constructor or destructor name.
  823. collect2 will sort the names and arrange that they are called at
  824. program startup. */
  825. if (final)
  826. sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
  827. else
  828. /* Proudce sane name but one not recognizable by collect2, just for the
  829. case we fail to inline the function. */
  830. sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
  831. name = get_file_function_name (which_buf);
  832. decl = build_decl (input_location, FUNCTION_DECL, name,
  833. build_function_type_list (void_type_node, NULL_TREE));
  834. current_function_decl = decl;
  835. resdecl = build_decl (input_location,
  836. RESULT_DECL, NULL_TREE, void_type_node);
  837. DECL_ARTIFICIAL (resdecl) = 1;
  838. DECL_RESULT (decl) = resdecl;
  839. DECL_CONTEXT (resdecl) = decl;
  840. allocate_struct_function (decl, false);
  841. TREE_STATIC (decl) = 1;
  842. TREE_USED (decl) = 1;
  843. DECL_ARTIFICIAL (decl) = 1;
  844. DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
  845. DECL_SAVED_TREE (decl) = body;
  846. if (!targetm.have_ctors_dtors && final)
  847. {
  848. TREE_PUBLIC (decl) = 1;
  849. DECL_PRESERVE_P (decl) = 1;
  850. }
  851. DECL_UNINLINABLE (decl) = 1;
  852. DECL_INITIAL (decl) = make_node (BLOCK);
  853. TREE_USED (DECL_INITIAL (decl)) = 1;
  854. DECL_SOURCE_LOCATION (decl) = input_location;
  855. cfun->function_end_locus = input_location;
  856. switch (which)
  857. {
  858. case 'I':
  859. DECL_STATIC_CONSTRUCTOR (decl) = 1;
  860. decl_init_priority_insert (decl, priority);
  861. break;
  862. case 'P':
  863. DECL_STATIC_CONSTRUCTOR (decl) = 1;
  864. DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("chkp ctor"),
  865. NULL,
  866. NULL_TREE);
  867. decl_init_priority_insert (decl, priority);
  868. break;
  869. case 'B':
  870. DECL_STATIC_CONSTRUCTOR (decl) = 1;
  871. DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("bnd_legacy"),
  872. NULL,
  873. NULL_TREE);
  874. decl_init_priority_insert (decl, priority);
  875. break;
  876. case 'D':
  877. DECL_STATIC_DESTRUCTOR (decl) = 1;
  878. decl_fini_priority_insert (decl, priority);
  879. break;
  880. default:
  881. gcc_unreachable ();
  882. }
  883. gimplify_function_tree (decl);
  884. cgraph_node::add_new_function (decl, false);
  885. set_cfun (NULL);
  886. current_function_decl = NULL;
  887. }
  888. /* Generate and emit a static constructor or destructor. WHICH must
  889. be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
  890. (for chkp static vars constructor) or 'B' (for chkp static bounds
  891. constructor). BODY is a STATEMENT_LIST containing GENERIC
  892. statements. PRIORITY is the initialization priority for this
  893. constructor or destructor. */
  894. void
  895. cgraph_build_static_cdtor (char which, tree body, int priority)
  896. {
  897. cgraph_build_static_cdtor_1 (which, body, priority, false);
  898. }
  899. /* A vector of FUNCTION_DECLs declared as static constructors. */
  900. static vec<tree> static_ctors;
  901. /* A vector of FUNCTION_DECLs declared as static destructors. */
  902. static vec<tree> static_dtors;
  903. /* When target does not have ctors and dtors, we call all constructor
  904. and destructor by special initialization/destruction function
  905. recognized by collect2.
  906. When we are going to build this function, collect all constructors and
  907. destructors and turn them into normal functions. */
  908. static void
  909. record_cdtor_fn (struct cgraph_node *node)
  910. {
  911. if (DECL_STATIC_CONSTRUCTOR (node->decl))
  912. static_ctors.safe_push (node->decl);
  913. if (DECL_STATIC_DESTRUCTOR (node->decl))
  914. static_dtors.safe_push (node->decl);
  915. node = cgraph_node::get (node->decl);
  916. DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
  917. }
  918. /* Define global constructors/destructor functions for the CDTORS, of
  919. which they are LEN. The CDTORS are sorted by initialization
  920. priority. If CTOR_P is true, these are constructors; otherwise,
  921. they are destructors. */
  922. static void
  923. build_cdtor (bool ctor_p, vec<tree> cdtors)
  924. {
  925. size_t i,j;
  926. size_t len = cdtors.length ();
  927. i = 0;
  928. while (i < len)
  929. {
  930. tree body;
  931. tree fn;
  932. priority_type priority;
  933. priority = 0;
  934. body = NULL_TREE;
  935. j = i;
  936. do
  937. {
  938. priority_type p;
  939. fn = cdtors[j];
  940. p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
  941. if (j == i)
  942. priority = p;
  943. else if (p != priority)
  944. break;
  945. j++;
  946. }
  947. while (j < len);
  948. /* When there is only one cdtor and target supports them, do nothing. */
  949. if (j == i + 1
  950. && targetm.have_ctors_dtors)
  951. {
  952. i++;
  953. continue;
  954. }
  955. /* Find the next batch of constructors/destructors with the same
  956. initialization priority. */
  957. for (;i < j; i++)
  958. {
  959. tree call;
  960. fn = cdtors[i];
  961. call = build_call_expr (fn, 0);
  962. if (ctor_p)
  963. DECL_STATIC_CONSTRUCTOR (fn) = 0;
  964. else
  965. DECL_STATIC_DESTRUCTOR (fn) = 0;
  966. /* We do not want to optimize away pure/const calls here.
  967. When optimizing, these should be already removed, when not
  968. optimizing, we want user to be able to breakpoint in them. */
  969. TREE_SIDE_EFFECTS (call) = 1;
  970. append_to_statement_list (call, &body);
  971. }
  972. gcc_assert (body != NULL_TREE);
  973. /* Generate a function to call all the function of like
  974. priority. */
  975. cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
  976. }
  977. }
  978. /* Comparison function for qsort. P1 and P2 are actually of type
  979. "tree *" and point to static constructors. DECL_INIT_PRIORITY is
  980. used to determine the sort order. */
  981. static int
  982. compare_ctor (const void *p1, const void *p2)
  983. {
  984. tree f1;
  985. tree f2;
  986. int priority1;
  987. int priority2;
  988. f1 = *(const tree *)p1;
  989. f2 = *(const tree *)p2;
  990. priority1 = DECL_INIT_PRIORITY (f1);
  991. priority2 = DECL_INIT_PRIORITY (f2);
  992. if (priority1 < priority2)
  993. return -1;
  994. else if (priority1 > priority2)
  995. return 1;
  996. else
  997. /* Ensure a stable sort. Constructors are executed in backwarding
  998. order to make LTO initialize braries first. */
  999. return DECL_UID (f2) - DECL_UID (f1);
  1000. }
  1001. /* Comparison function for qsort. P1 and P2 are actually of type
  1002. "tree *" and point to static destructors. DECL_FINI_PRIORITY is
  1003. used to determine the sort order. */
  1004. static int
  1005. compare_dtor (const void *p1, const void *p2)
  1006. {
  1007. tree f1;
  1008. tree f2;
  1009. int priority1;
  1010. int priority2;
  1011. f1 = *(const tree *)p1;
  1012. f2 = *(const tree *)p2;
  1013. priority1 = DECL_FINI_PRIORITY (f1);
  1014. priority2 = DECL_FINI_PRIORITY (f2);
  1015. if (priority1 < priority2)
  1016. return -1;
  1017. else if (priority1 > priority2)
  1018. return 1;
  1019. else
  1020. /* Ensure a stable sort. */
  1021. return DECL_UID (f1) - DECL_UID (f2);
  1022. }
  1023. /* Generate functions to call static constructors and destructors
  1024. for targets that do not support .ctors/.dtors sections. These
  1025. functions have magic names which are detected by collect2. */
  1026. static void
  1027. build_cdtor_fns (void)
  1028. {
  1029. if (!static_ctors.is_empty ())
  1030. {
  1031. gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
  1032. static_ctors.qsort (compare_ctor);
  1033. build_cdtor (/*ctor_p=*/true, static_ctors);
  1034. }
  1035. if (!static_dtors.is_empty ())
  1036. {
  1037. gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
  1038. static_dtors.qsort (compare_dtor);
  1039. build_cdtor (/*ctor_p=*/false, static_dtors);
  1040. }
  1041. }
  1042. /* Look for constructors and destructors and produce function calling them.
  1043. This is needed for targets not supporting ctors or dtors, but we perform the
  1044. transformation also at linktime to merge possibly numerous
  1045. constructors/destructors into single function to improve code locality and
  1046. reduce size. */
  1047. static unsigned int
  1048. ipa_cdtor_merge (void)
  1049. {
  1050. struct cgraph_node *node;
  1051. FOR_EACH_DEFINED_FUNCTION (node)
  1052. if (DECL_STATIC_CONSTRUCTOR (node->decl)
  1053. || DECL_STATIC_DESTRUCTOR (node->decl))
  1054. record_cdtor_fn (node);
  1055. build_cdtor_fns ();
  1056. static_ctors.release ();
  1057. static_dtors.release ();
  1058. return 0;
  1059. }
  1060. namespace {
  1061. const pass_data pass_data_ipa_cdtor_merge =
  1062. {
  1063. IPA_PASS, /* type */
  1064. "cdtor", /* name */
  1065. OPTGROUP_NONE, /* optinfo_flags */
  1066. TV_CGRAPHOPT, /* tv_id */
  1067. 0, /* properties_required */
  1068. 0, /* properties_provided */
  1069. 0, /* properties_destroyed */
  1070. 0, /* todo_flags_start */
  1071. 0, /* todo_flags_finish */
  1072. };
  1073. class pass_ipa_cdtor_merge : public ipa_opt_pass_d
  1074. {
  1075. public:
  1076. pass_ipa_cdtor_merge (gcc::context *ctxt)
  1077. : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
  1078. NULL, /* generate_summary */
  1079. NULL, /* write_summary */
  1080. NULL, /* read_summary */
  1081. NULL, /* write_optimization_summary */
  1082. NULL, /* read_optimization_summary */
  1083. NULL, /* stmt_fixup */
  1084. 0, /* function_transform_todo_flags_start */
  1085. NULL, /* function_transform */
  1086. NULL) /* variable_transform */
  1087. {}
  1088. /* opt_pass methods: */
  1089. virtual bool gate (function *);
  1090. virtual unsigned int execute (function *) { return ipa_cdtor_merge (); }
  1091. }; // class pass_ipa_cdtor_merge
  1092. bool
  1093. pass_ipa_cdtor_merge::gate (function *)
  1094. {
  1095. /* Perform the pass when we have no ctors/dtors support
  1096. or at LTO time to merge multiple constructors into single
  1097. function. */
  1098. return !targetm.have_ctors_dtors || (optimize && in_lto_p);
  1099. }
  1100. } // anon namespace
  1101. ipa_opt_pass_d *
  1102. make_pass_ipa_cdtor_merge (gcc::context *ctxt)
  1103. {
  1104. return new pass_ipa_cdtor_merge (ctxt);
  1105. }
  1106. /* Invalid pointer representing BOTTOM for single user dataflow. */
  1107. #define BOTTOM ((cgraph_node *)(size_t) 2)
  1108. /* Meet operation for single user dataflow.
  1109. Here we want to associate variables with sigle function that may access it.
  1110. FUNCTION is current single user of a variable, VAR is variable that uses it.
  1111. Latttice is stored in SINGLE_USER_MAP.
  1112. We represent:
  1113. - TOP by no entry in SIGNLE_USER_MAP
  1114. - BOTTOM by BOTTOM in AUX pointer (to save lookups)
  1115. - known single user by cgraph pointer in SINGLE_USER_MAP. */
  1116. cgraph_node *
  1117. meet (cgraph_node *function, varpool_node *var,
  1118. hash_map<varpool_node *, cgraph_node *> &single_user_map)
  1119. {
  1120. struct cgraph_node *user, **f;
  1121. if (var->aux == BOTTOM)
  1122. return BOTTOM;
  1123. f = single_user_map.get (var);
  1124. if (!f)
  1125. return function;
  1126. user = *f;
  1127. if (!function)
  1128. return user;
  1129. else if (function != user)
  1130. return BOTTOM;
  1131. else
  1132. return function;
  1133. }
  1134. /* Propagation step of single-use dataflow.
  1135. Check all uses of VNODE and see if they are used by single function FUNCTION.
  1136. SINGLE_USER_MAP represents the dataflow lattice. */
  1137. cgraph_node *
  1138. propagate_single_user (varpool_node *vnode, cgraph_node *function,
  1139. hash_map<varpool_node *, cgraph_node *> &single_user_map)
  1140. {
  1141. int i;
  1142. struct ipa_ref *ref;
  1143. gcc_assert (!vnode->externally_visible);
  1144. /* If node is an alias, first meet with its target. */
  1145. if (vnode->alias)
  1146. function = meet (function, vnode->get_alias_target (), single_user_map);
  1147. /* Check all users and see if they correspond to a single function. */
  1148. for (i = 0; vnode->iterate_referring (i, ref) && function != BOTTOM; i++)
  1149. {
  1150. struct cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
  1151. if (cnode)
  1152. {
  1153. if (cnode->global.inlined_to)
  1154. cnode = cnode->global.inlined_to;
  1155. if (!function)
  1156. function = cnode;
  1157. else if (function != cnode)
  1158. function = BOTTOM;
  1159. }
  1160. else
  1161. function = meet (function, dyn_cast <varpool_node *> (ref->referring),
  1162. single_user_map);
  1163. }
  1164. return function;
  1165. }
  1166. /* Pass setting used_by_single_function flag.
  1167. This flag is set on variable when there is only one function that may
  1168. possibly referr to it. */
  1169. static unsigned int
  1170. ipa_single_use (void)
  1171. {
  1172. varpool_node *first = (varpool_node *) (void *) 1;
  1173. varpool_node *var;
  1174. hash_map<varpool_node *, cgraph_node *> single_user_map;
  1175. FOR_EACH_DEFINED_VARIABLE (var)
  1176. if (!var->all_refs_explicit_p ())
  1177. var->aux = BOTTOM;
  1178. else
  1179. {
  1180. /* Enqueue symbol for dataflow. */
  1181. var->aux = first;
  1182. first = var;
  1183. }
  1184. /* The actual dataflow. */
  1185. while (first != (void *) 1)
  1186. {
  1187. cgraph_node *user, *orig_user, **f;
  1188. var = first;
  1189. first = (varpool_node *)first->aux;
  1190. f = single_user_map.get (var);
  1191. if (f)
  1192. orig_user = *f;
  1193. else
  1194. orig_user = NULL;
  1195. user = propagate_single_user (var, orig_user, single_user_map);
  1196. gcc_checking_assert (var->aux != BOTTOM);
  1197. /* If user differs, enqueue all references. */
  1198. if (user != orig_user)
  1199. {
  1200. unsigned int i;
  1201. ipa_ref *ref;
  1202. single_user_map.put (var, user);
  1203. /* Enqueue all aliases for re-processing. */
  1204. for (i = 0; var->iterate_direct_aliases (i, ref); i++)
  1205. if (!ref->referring->aux)
  1206. {
  1207. ref->referring->aux = first;
  1208. first = dyn_cast <varpool_node *> (ref->referring);
  1209. }
  1210. /* Enqueue all users for re-processing. */
  1211. for (i = 0; var->iterate_reference (i, ref); i++)
  1212. if (!ref->referred->aux
  1213. && ref->referred->definition
  1214. && is_a <varpool_node *> (ref->referred))
  1215. {
  1216. ref->referred->aux = first;
  1217. first = dyn_cast <varpool_node *> (ref->referred);
  1218. }
  1219. /* If user is BOTTOM, just punt on this var. */
  1220. if (user == BOTTOM)
  1221. var->aux = BOTTOM;
  1222. else
  1223. var->aux = NULL;
  1224. }
  1225. else
  1226. var->aux = NULL;
  1227. }
  1228. FOR_EACH_DEFINED_VARIABLE (var)
  1229. {
  1230. if (var->aux != BOTTOM)
  1231. {
  1232. #ifdef ENABLE_CHECKING
  1233. /* Not having the single user known means that the VAR is
  1234. unreachable. Either someone forgot to remove unreachable
  1235. variables or the reachability here is wrong. */
  1236. gcc_assert (single_user_map.get (var));
  1237. #endif
  1238. if (dump_file)
  1239. {
  1240. fprintf (dump_file, "Variable %s/%i is used by single function\n",
  1241. var->name (), var->order);
  1242. }
  1243. var->used_by_single_function = true;
  1244. }
  1245. var->aux = NULL;
  1246. }
  1247. return 0;
  1248. }
  1249. namespace {
  1250. const pass_data pass_data_ipa_single_use =
  1251. {
  1252. IPA_PASS, /* type */
  1253. "single-use", /* name */
  1254. OPTGROUP_NONE, /* optinfo_flags */
  1255. TV_CGRAPHOPT, /* tv_id */
  1256. 0, /* properties_required */
  1257. 0, /* properties_provided */
  1258. 0, /* properties_destroyed */
  1259. 0, /* todo_flags_start */
  1260. 0, /* todo_flags_finish */
  1261. };
  1262. class pass_ipa_single_use : public ipa_opt_pass_d
  1263. {
  1264. public:
  1265. pass_ipa_single_use (gcc::context *ctxt)
  1266. : ipa_opt_pass_d (pass_data_ipa_single_use, ctxt,
  1267. NULL, /* generate_summary */
  1268. NULL, /* write_summary */
  1269. NULL, /* read_summary */
  1270. NULL, /* write_optimization_summary */
  1271. NULL, /* read_optimization_summary */
  1272. NULL, /* stmt_fixup */
  1273. 0, /* function_transform_todo_flags_start */
  1274. NULL, /* function_transform */
  1275. NULL) /* variable_transform */
  1276. {}
  1277. /* opt_pass methods: */
  1278. virtual bool gate (function *);
  1279. virtual unsigned int execute (function *) { return ipa_single_use (); }
  1280. }; // class pass_ipa_single_use
  1281. bool
  1282. pass_ipa_single_use::gate (function *)
  1283. {
  1284. return optimize;
  1285. }
  1286. } // anon namespace
  1287. ipa_opt_pass_d *
  1288. make_pass_ipa_single_use (gcc::context *ctxt)
  1289. {
  1290. return new pass_ipa_single_use (ctxt);
  1291. }