Branch data Line data Source code
1 : : /* Basic IPA optimizations and utilities.
2 : : Copyright (C) 2003-2024 Free Software Foundation, Inc.
3 : :
4 : : This file is part of GCC.
5 : :
6 : : GCC is free software; you can redistribute it and/or modify it under
7 : : the terms of the GNU General Public License as published by the Free
8 : : Software Foundation; either version 3, or (at your option) any later
9 : : version.
10 : :
11 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : : for more details.
15 : :
16 : : You should have received a copy of the GNU General Public License
17 : : along with GCC; see the file COPYING3. If not see
18 : : <http://www.gnu.org/licenses/>. */
19 : :
20 : : #include "config.h"
21 : : #include "system.h"
22 : : #include "coretypes.h"
23 : : #include "backend.h"
24 : : #include "target.h"
25 : : #include "tree.h"
26 : : #include "gimple.h"
27 : : #include "alloc-pool.h"
28 : : #include "tree-pass.h"
29 : : #include "stringpool.h"
30 : : #include "cgraph.h"
31 : : #include "gimplify.h"
32 : : #include "tree-iterator.h"
33 : : #include "ipa-utils.h"
34 : : #include "symbol-summary.h"
35 : : #include "tree-vrp.h"
36 : : #include "sreal.h"
37 : : #include "ipa-cp.h"
38 : : #include "ipa-prop.h"
39 : : #include "ipa-fnsummary.h"
40 : : #include "dbgcnt.h"
41 : : #include "debug.h"
42 : : #include "stringpool.h"
43 : : #include "attribs.h"
44 : :
45 : : /* Return true when NODE has ADDR reference. */
46 : :
47 : : static bool
48 : 3302293 : has_addr_references_p (struct cgraph_node *node,
49 : : void *)
50 : : {
51 : 3302293 : int i;
52 : 3302293 : struct ipa_ref *ref = NULL;
53 : :
54 : 3406418 : for (i = 0; node->iterate_referring (i, ref); i++)
55 : 3312716 : if (ref->use == IPA_REF_ADDR)
56 : : return true;
57 : : return false;
58 : : }
59 : :
60 : : /* Return true when NODE can be target of an indirect call. */
61 : :
62 : : static bool
63 : 382 : is_indirect_call_target_p (struct cgraph_node *node, void *)
64 : : {
65 : 382 : return node->indirect_call_target;
66 : : }
67 : :
68 : : /* Look for all functions inlined to NODE and update their inlined_to pointers
69 : : to INLINED_TO. */
70 : :
71 : : static void
72 : 0 : update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
73 : : {
74 : 0 : struct cgraph_edge *e;
75 : 0 : for (e = node->callees; e; e = e->next_callee)
76 : 0 : if (e->callee->inlined_to)
77 : : {
78 : 0 : e->callee->inlined_to = inlined_to;
79 : 0 : update_inlined_to_pointer (e->callee, inlined_to);
80 : : }
81 : 0 : }
82 : :
83 : : /* Add symtab NODE to queue starting at FIRST.
84 : :
85 : : The queue is linked via AUX pointers and terminated by pointer to 1.
86 : : We enqueue nodes at two occasions: when we find them reachable or when we find
87 : : their bodies needed for further clonning. In the second case we mark them
88 : : by pointer to 2 after processing so they are re-queue when they become
89 : : reachable. */
90 : :
91 : : static void
92 : 136838126 : enqueue_node (symtab_node *node, symtab_node **first,
93 : : hash_set<symtab_node *> *reachable)
94 : : {
95 : : /* Node is still in queue; do nothing. */
96 : 136838126 : if (node->aux && node->aux != (void *) 2)
97 : : return;
98 : : /* Node was already processed as unreachable, re-enqueue
99 : : only if it became reachable now. */
100 : 75779803 : if (node->aux == (void *)2 && !reachable->contains (node))
101 : : return;
102 : 48622685 : node->aux = *first;
103 : 48622685 : *first = node;
104 : : }
105 : :
106 : : /* Return true if NODE may get inlined later.
107 : : This is used to keep DECL_EXTERNAL function bodies around long enough
108 : : so inliner can proces them. */
109 : :
110 : : static bool
111 : 1504353 : possible_inline_candidate_p (symtab_node *node)
112 : : {
113 : 1504353 : if (symtab->state >= IPA_SSA_AFTER_INLINING)
114 : : return false;
115 : 1429837 : cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
116 : 1399810 : if (!cnode)
117 : : return false;
118 : 1399810 : if (DECL_UNINLINABLE (cnode->decl))
119 : : return false;
120 : 1396678 : if (opt_for_fn (cnode->decl, optimize))
121 : : return true;
122 : 3269 : if (symtab->state >= IPA_SSA)
123 : : return false;
124 : 1980 : return lookup_attribute ("always_inline", DECL_ATTRIBUTES (node->decl));
125 : : }
126 : :
127 : : /* Process references. */
128 : :
129 : : static void
130 : 35230734 : process_references (symtab_node *snode,
131 : : symtab_node **first,
132 : : hash_set<symtab_node *> *reachable)
133 : : {
134 : 35230734 : int i;
135 : 35230734 : struct ipa_ref *ref = NULL;
136 : 101775193 : for (i = 0; snode->iterate_reference (i, ref); i++)
137 : : {
138 : 66544459 : symtab_node *node = ref->referred;
139 : 66544459 : symtab_node *body = node->ultimate_alias_target ();
140 : :
141 : 66544459 : if (node->definition && !node->in_other_partition
142 : 66544459 : && ((!DECL_EXTERNAL (node->decl) || node->alias)
143 : 98774 : || (possible_inline_candidate_p (node)
144 : : /* We use variable constructors during late compilation for
145 : : constant folding. Keep references alive so partitioning
146 : : knows about potential references. */
147 : 35986 : || (VAR_P (node->decl)
148 : 30027 : && (flag_wpa
149 : 30027 : || flag_incremental_link
150 : : == INCREMENTAL_LINK_LTO)
151 : 0 : && dyn_cast <varpool_node *> (node)
152 : 0 : ->ctor_useable_for_folding_p ()))))
153 : : {
154 : : /* Be sure that we will not optimize out alias target
155 : : body. */
156 : 48071730 : if (DECL_EXTERNAL (node->decl)
157 : 63730 : && node->alias
158 : 48072672 : && symtab->state < IPA_SSA_AFTER_INLINING)
159 : 755 : reachable->add (body);
160 : 48071730 : reachable->add (node);
161 : : }
162 : 66544459 : enqueue_node (node, first, reachable);
163 : : }
164 : 35230734 : }
165 : :
166 : : /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
167 : : all its potential targets as reachable to permit later inlining if
168 : : devirtualization happens. After inlining still keep their declarations
169 : : around, so we can devirtualize to a direct call.
170 : :
171 : : Also try to make trivial devirutalization when no or only one target is
172 : : possible. */
173 : :
174 : : static void
175 : 174367 : walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
176 : : struct cgraph_edge *edge,
177 : : symtab_node **first,
178 : : hash_set<symtab_node *> *reachable)
179 : : {
180 : 174367 : unsigned int i;
181 : 174367 : void *cache_token;
182 : 174367 : bool final;
183 : 174367 : vec <cgraph_node *>targets
184 : : = possible_polymorphic_call_targets
185 : 174367 : (edge, &final, &cache_token);
186 : :
187 : 174367 : if (cache_token != NULL && !reachable_call_targets->add (cache_token))
188 : : {
189 : 415990 : for (i = 0; i < targets.length (); i++)
190 : : {
191 : 125529 : struct cgraph_node *n = targets[i];
192 : :
193 : : /* Do not bother to mark virtual methods in anonymous namespace;
194 : : either we will find use of virtual table defining it, or it is
195 : : unused. */
196 : 125529 : if (TREE_CODE (TREE_TYPE (n->decl)) == METHOD_TYPE
197 : 244613 : && type_in_anonymous_namespace_p
198 : 119084 : (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl))))
199 : 5361 : continue;
200 : :
201 : 120168 : n->indirect_call_target = true;
202 : 120168 : symtab_node *body = n->function_symbol ();
203 : :
204 : : /* Prior inlining, keep alive bodies of possible targets for
205 : : devirtualization. */
206 : 120168 : if (n->definition
207 : 120168 : && (possible_inline_candidate_p (body)
208 : 89209 : && opt_for_fn (body->decl, flag_devirtualize)))
209 : : {
210 : : /* Be sure that we will not optimize out alias target
211 : : body. */
212 : 89209 : if (DECL_EXTERNAL (n->decl)
213 : 2940 : && n->alias
214 : 89209 : && symtab->state < IPA_SSA_AFTER_INLINING)
215 : 0 : reachable->add (body);
216 : 89209 : reachable->add (n);
217 : : }
218 : : /* Even after inlining we want to keep the possible targets in the
219 : : boundary, so late passes can still produce direct call even if
220 : : the chance for inlining is lost. */
221 : 120168 : enqueue_node (n, first, reachable);
222 : : }
223 : : }
224 : :
225 : : /* Very trivial devirtualization; when the type is
226 : : final or anonymous (so we know all its derivation)
227 : : and there is only one possible virtual call target,
228 : : make the edge direct. */
229 : 174367 : if (final)
230 : : {
231 : 256 : if (targets.length () <= 1 && dbg_cnt (devirt))
232 : : {
233 : 20 : cgraph_node *target, *node = edge->caller;
234 : 20 : if (targets.length () == 1)
235 : 8 : target = targets[0];
236 : : else
237 : 12 : target = cgraph_node::get_create (builtin_decl_unreachable ());
238 : :
239 : 20 : if (dump_enabled_p ())
240 : : {
241 : 0 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, edge->call_stmt,
242 : : "devirtualizing call in %s to %s\n",
243 : 0 : edge->caller->dump_name (),
244 : : target->dump_name ());
245 : : }
246 : 20 : edge = cgraph_edge::make_direct (edge, target);
247 : 20 : if (ipa_fn_summaries)
248 : 4 : ipa_update_overall_fn_summary (node->inlined_to
249 : : ? node->inlined_to : node);
250 : 16 : else if (edge->call_stmt)
251 : 16 : cgraph_edge::redirect_call_stmt_to_callee (edge);
252 : : }
253 : : }
254 : 174367 : }
255 : :
256 : : /* Perform reachability analysis and reclaim all unreachable nodes.
257 : :
258 : : The algorithm is basically mark&sweep but with some extra refinements:
259 : :
260 : : - reachable extern inline functions needs special handling; the bodies needs
261 : : to stay in memory until inlining in hope that they will be inlined.
262 : : After inlining we release their bodies and turn them into unanalyzed
263 : : nodes even when they are reachable.
264 : :
265 : : - virtual functions are kept in callgraph even if they seem unreachable in
266 : : hope calls to them will be devirtualized.
267 : :
268 : : Again we remove them after inlining. In late optimization some
269 : : devirtualization may happen, but it is not important since we won't inline
270 : : the call. In theory early opts and IPA should work out all important cases.
271 : :
272 : : - virtual clones needs bodies of their origins for later materialization;
273 : : this means that we want to keep the body even if the origin is unreachable
274 : : otherwise. To avoid origin from sitting in the callgraph and being
275 : : walked by IPA passes, we turn them into unanalyzed nodes with body
276 : : defined.
277 : :
278 : : We maintain set of function declaration where body needs to stay in
279 : : body_needed_for_clonning
280 : :
281 : : Inline clones represent special case: their declaration match the
282 : : declaration of origin and cgraph_remove_node already knows how to
283 : : reshape callgraph and preserve body when offline copy of function or
284 : : inline clone is being removed.
285 : :
286 : : - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
287 : : variables with DECL_INITIAL set. We finalize these and keep reachable
288 : : ones around for constant folding purposes. After inlining we however
289 : : stop walking their references to let everything static referenced by them
290 : : to be removed when it is otherwise unreachable.
291 : :
292 : : We maintain queue of both reachable symbols (i.e. defined symbols that needs
293 : : to stay) and symbols that are in boundary (i.e. external symbols referenced
294 : : by reachable symbols or origins of clones). The queue is represented
295 : : as linked list by AUX pointer terminated by 1.
296 : :
297 : : At the end we keep all reachable symbols. For symbols in boundary we always
298 : : turn definition into a declaration, but we may keep function body around
299 : : based on body_needed_for_clonning
300 : :
301 : : All symbols that enter the queue have AUX pointer non-zero and are in the
302 : : boundary. Pointer set REACHABLE is used to track reachable symbols.
303 : :
304 : : Every symbol can be visited twice - once as part of boundary and once
305 : : as real reachable symbol. enqueue_node needs to decide whether the
306 : : node needs to be re-queued for second processing. For this purpose
307 : : we set AUX pointer of processed symbols in the boundary to constant 2. */
308 : :
309 : : bool
310 : 1435228 : symbol_table::remove_unreachable_nodes (FILE *file)
311 : : {
312 : 1435228 : symtab_node *first = (symtab_node *) (void *) 1;
313 : 1435228 : struct cgraph_node *node, *next;
314 : 1435228 : varpool_node *vnode, *vnext;
315 : 1435228 : bool changed = false;
316 : 1435228 : hash_set<symtab_node *> reachable;
317 : 1435228 : hash_set<tree> body_needed_for_clonning;
318 : 1435228 : hash_set<void *> reachable_call_targets;
319 : :
320 : 1435228 : timevar_push (TV_IPA_UNREACHABLE);
321 : 1435228 : build_type_inheritance_graph ();
322 : 1435228 : if (file)
323 : 747 : fprintf (file, "\nReclaiming functions:");
324 : 1435228 : if (flag_checking)
325 : : {
326 : 56531950 : FOR_EACH_FUNCTION (node)
327 : 26830863 : gcc_assert (!node->aux);
328 : 45678168 : FOR_EACH_VARIABLE (vnode)
329 : 22121528 : gcc_assert (!vnode->aux);
330 : : }
331 : : /* Mark functions whose bodies are obviously needed.
332 : : This is mostly when they can be referenced externally. Inline clones
333 : : are special since their declarations are shared with master clone and thus
334 : : cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
335 : 56533008 : FOR_EACH_FUNCTION (node)
336 : : {
337 : 26831276 : node->used_as_abstract_origin = false;
338 : 26831276 : node->indirect_call_target = false;
339 : 26831276 : if (node->definition
340 : 15528898 : && !node->inlined_to
341 : 13662129 : && !node->in_other_partition
342 : 40493171 : && !node->can_remove_if_no_direct_calls_and_refs_p ())
343 : : {
344 : 7525251 : gcc_assert (!node->inlined_to);
345 : 7525251 : reachable.add (node);
346 : 7525251 : enqueue_node (node, &first, &reachable);
347 : : }
348 : : else
349 : 19306025 : gcc_assert (!node->aux);
350 : : }
351 : :
352 : : /* Mark variables that are obviously needed. */
353 : 21483584 : FOR_EACH_DEFINED_VARIABLE (vnode)
354 : 20048356 : if (!vnode->can_remove_if_no_refs_p()
355 : 20048356 : && !vnode->in_other_partition)
356 : : {
357 : 10128807 : reachable.add (vnode);
358 : 10128807 : enqueue_node (vnode, &first, &reachable);
359 : : }
360 : :
361 : : /* Perform reachability analysis. */
362 : 50057913 : while (first != (symtab_node *) (void *) 1)
363 : : {
364 : 48622685 : bool in_boundary_p = !reachable.contains (first);
365 : 48622685 : symtab_node *node = first;
366 : :
367 : 48622685 : first = (symtab_node *)first->aux;
368 : :
369 : : /* If we are processing symbol in boundary, mark its AUX pointer for
370 : : possible later re-processing in enqueue_node. */
371 : 48622685 : if (in_boundary_p)
372 : : {
373 : 13391951 : node->aux = (void *)2;
374 : 13391951 : if (node->alias && node->analyzed)
375 : 3812 : enqueue_node (node->get_alias_target (), &first, &reachable);
376 : : }
377 : : else
378 : : {
379 : 35230734 : if (TREE_CODE (node->decl) == FUNCTION_DECL
380 : 35230734 : && DECL_ABSTRACT_ORIGIN (node->decl))
381 : : {
382 : 2982705 : struct cgraph_node *origin_node
383 : 2982705 : = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node->decl));
384 : 2982705 : if (origin_node && !origin_node->used_as_abstract_origin)
385 : : {
386 : 343304 : origin_node->used_as_abstract_origin = true;
387 : 343304 : gcc_assert (!origin_node->prev_sibling_clone);
388 : 343304 : gcc_assert (!origin_node->next_sibling_clone);
389 : 557839 : for (cgraph_node *n = origin_node->clones; n;
390 : 214535 : n = n->next_sibling_clone)
391 : 214535 : if (n->decl == DECL_ABSTRACT_ORIGIN (node->decl))
392 : 191789 : n->used_as_abstract_origin = true;
393 : : }
394 : : }
395 : : /* If any non-external and non-local symbol in a comdat group is
396 : : reachable, force all externally visible symbols in the same comdat
397 : : group to be reachable as well. Comdat-local symbols
398 : : can be discarded if all uses were inlined. */
399 : 35230734 : if (node->same_comdat_group
400 : 1541293 : && node->externally_visible
401 : 36745081 : && !DECL_EXTERNAL (node->decl))
402 : : {
403 : 1514347 : symtab_node *next;
404 : 1514347 : for (next = node->same_comdat_group;
405 : 4799610 : next != node;
406 : 3285263 : next = next->same_comdat_group)
407 : 6570526 : if (!next->comdat_local_p ()
408 : 3212281 : && !DECL_EXTERNAL (next->decl)
409 : 3212278 : && !reachable.add (next))
410 : 722663 : enqueue_node (next, &first, &reachable);
411 : : }
412 : : /* Mark references as reachable. */
413 : 35230734 : process_references (node, &first, &reachable);
414 : : }
415 : :
416 : 48622685 : if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
417 : : {
418 : : /* Mark the callees reachable unless they are direct calls to extern
419 : : inline functions we decided to not inline. */
420 : 26541949 : if (!in_boundary_p)
421 : : {
422 : 15220484 : struct cgraph_edge *e;
423 : : /* Keep alive possible targets for devirtualization. */
424 : 15220484 : if (opt_for_fn (cnode->decl, optimize)
425 : 15220484 : && opt_for_fn (cnode->decl, flag_devirtualize))
426 : : {
427 : 12363639 : struct cgraph_edge *next;
428 : 13502159 : for (e = cnode->indirect_calls; e; e = next)
429 : : {
430 : 1138520 : next = e->next_callee;
431 : 1138520 : if (e->indirect_info->polymorphic)
432 : 174367 : walk_polymorphic_call_targets (&reachable_call_targets,
433 : : e, &first, &reachable);
434 : : }
435 : : }
436 : 66275758 : for (e = cnode->callees; e; e = e->next_callee)
437 : : {
438 : 51055274 : symtab_node *body = e->callee->function_symbol ();
439 : 51055274 : if (e->callee->definition
440 : 51055274 : && !e->callee->in_other_partition
441 : 51055274 : && (!e->inline_failed
442 : 17786939 : || !DECL_EXTERNAL (e->callee->decl)
443 : 1527195 : || e->callee->alias
444 : 1305232 : || possible_inline_candidate_p (e->callee)))
445 : : {
446 : : /* Be sure that we will not optimize out alias target
447 : : body. */
448 : 19570949 : if (DECL_EXTERNAL (e->callee->decl)
449 : 1719783 : && e->callee->alias
450 : 19792912 : && symtab->state < IPA_SSA_AFTER_INLINING)
451 : 219430 : reachable.add (body);
452 : 19570949 : reachable.add (e->callee);
453 : : }
454 : 31484325 : else if (e->callee->declare_variant_alt
455 : 137 : && !e->callee->in_other_partition)
456 : 137 : reachable.add (e->callee);
457 : 51055274 : enqueue_node (e->callee, &first, &reachable);
458 : : }
459 : :
460 : : /* When inline clone exists, mark body to be preserved so when removing
461 : : offline copy of the function we don't kill it. */
462 : 15220484 : if (cnode->inlined_to)
463 : 1846320 : body_needed_for_clonning.add (cnode->decl);
464 : :
465 : : /* For non-inline clones, force their origins to the boundary and ensure
466 : : that body is not removed. */
467 : 17674600 : while (cnode->clone_of)
468 : : {
469 : 2454116 : bool noninline = cnode->clone_of->decl != cnode->decl;
470 : 2454116 : cnode = cnode->clone_of;
471 : 2454116 : if (noninline)
472 : : {
473 : 634538 : body_needed_for_clonning.add (cnode->decl);
474 : 634538 : enqueue_node (cnode, &first, &reachable);
475 : : }
476 : : }
477 : :
478 : : }
479 : 11321465 : else if (cnode->thunk)
480 : 43 : enqueue_node (cnode->callees->callee, &first, &reachable);
481 : :
482 : : /* If any reachable function has simd clones, mark them as
483 : : reachable as well. */
484 : 26541949 : if (cnode->simd_clones)
485 : : {
486 : : cgraph_node *next;
487 : 0 : for (next = cnode->simd_clones;
488 : 0 : next;
489 : 0 : next = next->simdclone->next_clone)
490 : 0 : if (in_boundary_p
491 : 0 : || !reachable.add (next))
492 : 0 : enqueue_node (next, &first, &reachable);
493 : : }
494 : : }
495 : : /* When we see constructor of external variable, keep referred nodes in the
496 : : boundary. This will also hold initializers of the external vars NODE
497 : : refers to. */
498 : 48622685 : varpool_node *vnode = dyn_cast <varpool_node *> (node);
499 : 48622685 : if (vnode
500 : 22080736 : && DECL_EXTERNAL (node->decl)
501 : 2070243 : && !vnode->alias
502 : : && in_boundary_p)
503 : : {
504 : 2173352 : struct ipa_ref *ref = NULL;
505 : 5678821 : for (int i = 0; node->iterate_reference (i, ref); i++)
506 : 103111 : enqueue_node (ref->referred, &first, &reachable);
507 : : }
508 : : }
509 : :
510 : : /* Remove unreachable functions. */
511 : 29706936 : for (node = first_function (); node; node = next)
512 : : {
513 : 26836480 : next = next_function (node);
514 : :
515 : : /* If node is not needed at all, remove it. */
516 : 26836480 : if (!node->aux)
517 : : {
518 : 301162 : if (file)
519 : 112 : fprintf (file, " %s", node->dump_name ());
520 : 301162 : node->remove ();
521 : 301162 : changed = true;
522 : : }
523 : : /* If node is unreachable, remove its body. */
524 : 26535318 : else if (!reachable.contains (node))
525 : : {
526 : : /* We keep definitions of thunks and aliases in the boundary so
527 : : we can walk to the ultimate alias targets and function symbols
528 : : reliably. */
529 : 11314834 : if (node->alias || node->thunk)
530 : : ;
531 : 11308441 : else if (!body_needed_for_clonning.contains (node->decl))
532 : : {
533 : : /* Make the node a non-clone so that we do not attempt to
534 : : materialize it later. */
535 : 10993677 : if (node->clone_of)
536 : 0 : node->remove_from_clone_tree ();
537 : 10993677 : node->release_body ();
538 : : }
539 : 314764 : else if (!node->clone_of)
540 : 305904 : gcc_assert (in_lto_p || DECL_RESULT (node->decl));
541 : 11314834 : if (node->definition && !node->alias && !node->thunk)
542 : : {
543 : 130048 : if (file)
544 : 212 : fprintf (file, " %s", node->dump_name ());
545 : 130048 : node->body_removed = true;
546 : 130048 : node->analyzed = false;
547 : 130048 : node->definition = false;
548 : 130048 : node->cpp_implicit_alias = false;
549 : 130048 : node->alias = false;
550 : 130048 : node->transparent_alias = false;
551 : 130048 : node->thunk = false;
552 : 130048 : node->weakref = false;
553 : : /* After early inlining we drop always_inline attributes on
554 : : bodies of functions that are still referenced (have their
555 : : address taken). */
556 : 130048 : DECL_ATTRIBUTES (node->decl)
557 : 130048 : = remove_attribute ("always_inline",
558 : 130048 : DECL_ATTRIBUTES (node->decl));
559 : 130048 : if (!node->in_other_partition)
560 : 129871 : node->local = false;
561 : 130048 : node->remove_callees ();
562 : 130048 : node->remove_all_references ();
563 : 130048 : changed = true;
564 : : }
565 : : }
566 : : else
567 : 15220484 : gcc_assert (node->clone_of || !node->has_gimple_body_p ()
568 : : || in_lto_p || DECL_RESULT (node->decl));
569 : : }
570 : :
571 : : /* Inline clones might be kept around so their materializing allows further
572 : : cloning. If the function the clone is inlined into is removed, we need
573 : : to turn it into normal cone. */
574 : 29405774 : FOR_EACH_FUNCTION (node)
575 : : {
576 : 26535318 : if (node->inlined_to
577 : 1846320 : && !node->callers)
578 : : {
579 : 0 : gcc_assert (node->clones);
580 : 0 : node->inlined_to = NULL;
581 : 0 : update_inlined_to_pointer (node, node);
582 : : }
583 : 26535318 : node->aux = NULL;
584 : : }
585 : :
586 : : /* Remove unreachable variables. */
587 : 1435228 : if (file)
588 : 747 : fprintf (file, "\nReclaiming variables:");
589 : 24992494 : for (vnode = first_variable (); vnode; vnode = vnext)
590 : : {
591 : 22122038 : vnext = next_variable (vnode);
592 : 22122038 : if (!vnode->aux
593 : : /* For can_refer_decl_in_current_unit_p we want to track for
594 : : all external variables if they are defined in other partition
595 : : or not. */
596 : 22122038 : && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
597 : : {
598 : 41263 : struct ipa_ref *ref = NULL;
599 : :
600 : : /* First remove the aliases, so varpool::remove can possibly lookup
601 : : the constructor and save it for future use. */
602 : 41263 : while (vnode->iterate_direct_aliases (0, ref))
603 : : {
604 : 0 : if (file)
605 : 0 : fprintf (file, " %s", ref->referred->dump_name ());
606 : 0 : ref->referring->remove ();
607 : : }
608 : 41263 : if (file)
609 : 1 : fprintf (file, " %s", vnode->dump_name ());
610 : 41263 : vnext = next_variable (vnode);
611 : : /* Signal removal to the debug machinery. */
612 : 41263 : if (! flag_wpa || flag_incremental_link == INCREMENTAL_LINK_LTO)
613 : : {
614 : 39006 : vnode->definition = false;
615 : 39006 : (*debug_hooks->late_global_decl) (vnode->decl);
616 : : }
617 : 41263 : vnode->remove ();
618 : 41263 : changed = true;
619 : : }
620 : 22080775 : else if (!reachable.contains (vnode) && !vnode->alias)
621 : : {
622 : 2070303 : tree init;
623 : 2070303 : if (vnode->definition)
624 : : {
625 : 15337 : if (file)
626 : 0 : fprintf (file, " %s", vnode->dump_name ());
627 : : changed = true;
628 : : }
629 : : /* Keep body if it may be useful for constant folding. */
630 : 2065762 : if ((flag_wpa || flag_incremental_link == INCREMENTAL_LINK_LTO)
631 : 4136011 : || ((init = ctor_for_folding (vnode->decl)) == error_mark_node))
632 : 1982942 : vnode->remove_initializer ();
633 : : else
634 : 87361 : DECL_INITIAL (vnode->decl) = init;
635 : 2070303 : vnode->body_removed = true;
636 : 2070303 : vnode->definition = false;
637 : 2070303 : vnode->analyzed = false;
638 : 2070303 : vnode->aux = NULL;
639 : :
640 : 2070303 : vnode->remove_from_same_comdat_group ();
641 : :
642 : 2070303 : vnode->remove_all_references ();
643 : : }
644 : : else
645 : 20010472 : vnode->aux = NULL;
646 : : }
647 : :
648 : : /* Now update address_taken flags and try to promote functions to be local. */
649 : 1435228 : if (file)
650 : 747 : fprintf (file, "\nClearing address taken flags:");
651 : 16659255 : FOR_EACH_DEFINED_FUNCTION (node)
652 : 15224027 : if (node->address_taken
653 : 15224027 : && !node->used_from_other_partition)
654 : : {
655 : 3232303 : if (!node->call_for_symbol_and_aliases
656 : 3232303 : (has_addr_references_p, NULL, true))
657 : : {
658 : 23712 : if (file)
659 : 2 : fprintf (file, " %s", node->dump_name ());
660 : 23712 : node->address_taken = false;
661 : 23712 : changed = true;
662 : 23712 : if (node->local_p ()
663 : : /* Virtual functions may be kept in cgraph just because
664 : : of possible later devirtualization. Do not mark them as
665 : : local too early so we won't optimize them out before
666 : : we are done with polymorphic call analysis. */
667 : 23712 : && (symtab->state >= IPA_SSA_AFTER_INLINING
668 : 382 : || !node->call_for_symbol_and_aliases
669 : 382 : (is_indirect_call_target_p, NULL, true)))
670 : : {
671 : 393 : node->local = true;
672 : 393 : if (file)
673 : 2 : fprintf (file, " (local)");
674 : : }
675 : : }
676 : : }
677 : 1435228 : if (file)
678 : 747 : fprintf (file, "\n");
679 : :
680 : 1435228 : symtab_node::checking_verify_symtab_nodes ();
681 : :
682 : : /* If we removed something, perhaps profile could be improved. */
683 : 1435228 : if (changed && (optimize || in_lto_p) && ipa_call_summaries)
684 : 3715014 : FOR_EACH_DEFINED_FUNCTION (node)
685 : 3647801 : ipa_propagate_frequency (node);
686 : :
687 : 1435228 : timevar_pop (TV_IPA_UNREACHABLE);
688 : 1435228 : return changed;
689 : 1435228 : }
690 : :
691 : : /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
692 : : as needed, also clear EXPLICIT_REFS if the references to given variable
693 : : do not need to be explicit. */
694 : :
695 : : void
696 : 5335843 : process_references (varpool_node *vnode,
697 : : bool *written, bool *address_taken,
698 : : bool *read, bool *explicit_refs)
699 : : {
700 : 5335843 : int i;
701 : 5335843 : struct ipa_ref *ref;
702 : :
703 : 5335843 : if (!vnode->all_refs_explicit_p ()
704 : 5335843 : || TREE_THIS_VOLATILE (vnode->decl))
705 : 2914646 : *explicit_refs = false;
706 : :
707 : 3915052 : for (i = 0; vnode->iterate_referring (i, ref)
708 : 9250895 : && *explicit_refs && (!*written || !*address_taken || !*read); i++)
709 : 3915052 : switch (ref->use)
710 : : {
711 : 2755102 : case IPA_REF_ADDR:
712 : 2755102 : *address_taken = true;
713 : 2755102 : break;
714 : 623341 : case IPA_REF_LOAD:
715 : 623341 : *read = true;
716 : 623341 : break;
717 : 526572 : case IPA_REF_STORE:
718 : 526572 : *written = true;
719 : 526572 : break;
720 : 10037 : case IPA_REF_ALIAS:
721 : 10037 : process_references (dyn_cast<varpool_node *> (ref->referring), written,
722 : : address_taken, read, explicit_refs);
723 : 10037 : break;
724 : : }
725 : 5335843 : }
726 : :
727 : : /* Set TREE_READONLY bit. */
728 : :
729 : : bool
730 : 67263 : set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
731 : : {
732 : 67263 : TREE_READONLY (vnode->decl) = true;
733 : 67263 : return false;
734 : : }
735 : :
736 : : /* Set writeonly bit and clear the initalizer, since it will not be needed. */
737 : :
738 : : bool
739 : 24914 : set_writeonly_bit (varpool_node *vnode, void *data)
740 : : {
741 : 24914 : vnode->writeonly = true;
742 : 24914 : if (optimize || in_lto_p)
743 : : {
744 : 24914 : DECL_INITIAL (vnode->decl) = NULL;
745 : 24914 : if (!vnode->alias)
746 : : {
747 : 24914 : if (vnode->num_references ())
748 : 185 : *(bool *)data = true;
749 : 24914 : vnode->remove_all_references ();
750 : : }
751 : : }
752 : 24914 : return false;
753 : : }
754 : :
755 : : /* Clear addressale bit of VNODE. */
756 : :
757 : : bool
758 : 156836 : clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
759 : : {
760 : 156836 : vnode->address_taken = false;
761 : 156836 : TREE_ADDRESSABLE (vnode->decl) = 0;
762 : 156836 : return false;
763 : : }
764 : :
765 : : /* Discover variables that have no longer address taken, are read-only or
766 : : write-only and update their flags.
767 : :
768 : : Return true when unreachable symbol removal should be done.
769 : :
770 : : FIXME: This cannot be done in between gimplify and omp_expand since
771 : : readonly flag plays role on what is shared and what is not. Currently we do
772 : : this transformation as part of whole program visibility and re-do at
773 : : ipa-reference pass (to take into account clonning), but it would
774 : : make sense to do it before early optimizations. */
775 : :
776 : : bool
777 : 290900 : ipa_discover_variable_flags (void)
778 : : {
779 : 290900 : if (!flag_ipa_reference_addressable)
780 : : return false;
781 : :
782 : 285552 : bool remove_p = false;
783 : 285552 : varpool_node *vnode;
784 : 285552 : if (dump_file)
785 : 59 : fprintf (dump_file, "Clearing variable flags:");
786 : 11243230 : FOR_EACH_VARIABLE (vnode)
787 : 5336063 : if (!vnode->alias
788 : 5336063 : && (TREE_ADDRESSABLE (vnode->decl)
789 : 2351787 : || !vnode->writeonly
790 : 24842 : || !TREE_READONLY (vnode->decl)))
791 : : {
792 : 5325806 : bool written = false;
793 : 5325806 : bool address_taken = false;
794 : 5325806 : bool read = false;
795 : 5325806 : bool explicit_refs = true;
796 : :
797 : 5325806 : process_references (vnode, &written, &address_taken, &read,
798 : : &explicit_refs);
799 : 5325806 : if (!explicit_refs)
800 : 2914646 : continue;
801 : 2411160 : if (!address_taken)
802 : : {
803 : 147319 : if (TREE_ADDRESSABLE (vnode->decl) && dump_file)
804 : 0 : fprintf (dump_file, " %s (non-addressable)",
805 : : vnode->dump_name ());
806 : 147319 : vnode->call_for_symbol_and_aliases (clear_addressable_bit, NULL,
807 : : true);
808 : : }
809 : 147319 : if (!address_taken && !written
810 : : /* Making variable in explicit section readonly can cause section
811 : : type conflict.
812 : : See e.g. gcc.c-torture/compile/pr23237.c */
813 : 2468940 : && vnode->get_section () == NULL)
814 : : {
815 : 57750 : if (!TREE_READONLY (vnode->decl) && dump_file)
816 : 4 : fprintf (dump_file, " %s (read-only)", vnode->dump_name ());
817 : 57750 : vnode->call_for_symbol_and_aliases (set_readonly_bit, NULL, true);
818 : : }
819 : 2411160 : if (!vnode->writeonly && !read && !address_taken && written)
820 : : {
821 : 24914 : if (dump_file)
822 : 0 : fprintf (dump_file, " %s (write-only)", vnode->dump_name ());
823 : 24914 : vnode->call_for_symbol_and_aliases (set_writeonly_bit, &remove_p,
824 : : true);
825 : : }
826 : : }
827 : 285552 : if (dump_file)
828 : 59 : fprintf (dump_file, "\n");
829 : 285552 : return remove_p;
830 : : }
831 : :
832 : : /* Generate and emit a static constructor or destructor. WHICH must
833 : : be one of 'I' (for a constructor), 'D' (for a destructor).
834 : : BODY is a STATEMENT_LIST containing GENERIC
835 : : statements. PRIORITY is the initialization priority for this
836 : : constructor or destructor.
837 : :
838 : : FINAL specify whether the externally visible name for collect2 should
839 : : be produced. */
840 : :
841 : : static tree
842 : 4573 : cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final,
843 : : tree optimization,
844 : : tree target)
845 : : {
846 : 4573 : static int counter = 0;
847 : 4573 : char which_buf[16];
848 : 4573 : tree decl, name, resdecl;
849 : :
850 : : /* The priority is encoded in the constructor or destructor name.
851 : : collect2 will sort the names and arrange that they are called at
852 : : program startup. */
853 : 4573 : if (!targetm.have_ctors_dtors && final)
854 : : {
855 : 0 : sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
856 : 0 : name = get_file_function_name (which_buf);
857 : : }
858 : : else
859 : : {
860 : : /* Proudce sane name but one not recognizable by collect2, just for the
861 : : case we fail to inline the function. */
862 : 4573 : sprintf (which_buf, "_sub_%c_%.5d_%d", which, priority, counter++);
863 : 4573 : name = get_identifier (which_buf);
864 : : }
865 : :
866 : 4573 : decl = build_decl (input_location, FUNCTION_DECL, name,
867 : : build_function_type_list (void_type_node, NULL_TREE));
868 : 4573 : current_function_decl = decl;
869 : :
870 : 4573 : resdecl = build_decl (input_location,
871 : : RESULT_DECL, NULL_TREE, void_type_node);
872 : 4573 : DECL_ARTIFICIAL (resdecl) = 1;
873 : 4573 : DECL_RESULT (decl) = resdecl;
874 : 4573 : DECL_CONTEXT (resdecl) = decl;
875 : :
876 : 4573 : allocate_struct_function (decl, false);
877 : :
878 : 4573 : TREE_STATIC (decl) = 1;
879 : 4573 : TREE_USED (decl) = 1;
880 : 4573 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl) = optimization;
881 : 4573 : DECL_FUNCTION_SPECIFIC_TARGET (decl) = target;
882 : 4573 : DECL_ARTIFICIAL (decl) = 1;
883 : 4573 : DECL_IGNORED_P (decl) = 1;
884 : 4573 : DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
885 : 4573 : DECL_SAVED_TREE (decl) = body;
886 : 4573 : if (!targetm.have_ctors_dtors && final)
887 : : {
888 : 0 : TREE_PUBLIC (decl) = 1;
889 : 0 : DECL_PRESERVE_P (decl) = 1;
890 : : }
891 : 4573 : DECL_UNINLINABLE (decl) = 1;
892 : :
893 : 4573 : DECL_INITIAL (decl) = make_node (BLOCK);
894 : 4573 : BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl;
895 : 4573 : TREE_USED (DECL_INITIAL (decl)) = 1;
896 : :
897 : 4573 : DECL_SOURCE_LOCATION (decl) = input_location;
898 : 4573 : cfun->function_end_locus = input_location;
899 : :
900 : 4573 : switch (which)
901 : : {
902 : 3160 : case 'I':
903 : 3160 : DECL_STATIC_CONSTRUCTOR (decl) = 1;
904 : 3160 : decl_init_priority_insert (decl, priority);
905 : 3160 : break;
906 : 1413 : case 'D':
907 : 1413 : DECL_STATIC_DESTRUCTOR (decl) = 1;
908 : 1413 : decl_fini_priority_insert (decl, priority);
909 : 1413 : break;
910 : 0 : default:
911 : 0 : gcc_unreachable ();
912 : : }
913 : :
914 : 4573 : gimplify_function_tree (decl);
915 : :
916 : 4573 : cgraph_node::add_new_function (decl, false);
917 : :
918 : 4573 : set_cfun (NULL);
919 : 4573 : current_function_decl = NULL;
920 : 4573 : return decl;
921 : : }
922 : :
923 : : /* Generate and emit a static constructor or destructor. WHICH must
924 : : be one of 'I' (for a constructor) or 'D' (for a destructor).
925 : : BODY is a STATEMENT_LIST containing GENERIC
926 : : statements. PRIORITY is the initialization priority for this
927 : : constructor or destructor. */
928 : :
929 : : void
930 : 4565 : cgraph_build_static_cdtor (char which, tree body, int priority)
931 : : {
932 : : /* FIXME: We should be able to
933 : : gcc_assert (!in_lto_p);
934 : : because at LTO time the global options are not safe to use.
935 : : Unfortunately ASAN finish_file will produce constructors late and they
936 : : may lead to surprises. */
937 : 4565 : cgraph_build_static_cdtor_1 (which, body, priority, false,
938 : : optimization_default_node,
939 : : target_option_default_node);
940 : 4565 : }
941 : :
942 : : /* When target does not have ctors and dtors, we call all constructor
943 : : and destructor by special initialization/destruction function
944 : : recognized by collect2.
945 : :
946 : : When we are going to build this function, collect all constructors and
947 : : destructors and turn them into normal functions. */
948 : :
949 : : static void
950 : 69 : record_cdtor_fn (struct cgraph_node *node, vec<tree> *ctors, vec<tree> *dtors)
951 : : {
952 : 69 : if (DECL_STATIC_CONSTRUCTOR (node->decl))
953 : 55 : ctors->safe_push (node->decl);
954 : 69 : if (DECL_STATIC_DESTRUCTOR (node->decl))
955 : 15 : dtors->safe_push (node->decl);
956 : 69 : node = cgraph_node::get (node->decl);
957 : 69 : DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
958 : 69 : }
959 : :
960 : : /* Define global constructors/destructor functions for the CDTORS, of
961 : : which they are LEN. The CDTORS are sorted by initialization
962 : : priority. If CTOR_P is true, these are constructors; otherwise,
963 : : they are destructors. */
964 : :
965 : : static void
966 : 56 : build_cdtor (bool ctor_p, const vec<tree> &cdtors)
967 : : {
968 : 56 : size_t i,j;
969 : 56 : size_t len = cdtors.length ();
970 : :
971 : 56 : i = 0;
972 : 118 : while (i < len)
973 : : {
974 : 62 : tree body;
975 : 62 : tree fn;
976 : 62 : priority_type priority;
977 : :
978 : 62 : priority = 0;
979 : 62 : body = NULL_TREE;
980 : 62 : j = i;
981 : 76 : do
982 : : {
983 : 76 : priority_type p;
984 : 76 : fn = cdtors[j];
985 : 76 : p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
986 : 76 : if (j == i)
987 : : priority = p;
988 : 14 : else if (p != priority)
989 : : break;
990 : 70 : j++;
991 : : }
992 : 70 : while (j < len);
993 : :
994 : : /* When there is only one cdtor and target supports them, do nothing. */
995 : 62 : if (j == i + 1
996 : 54 : && targetm.have_ctors_dtors)
997 : : {
998 : 54 : i++;
999 : 54 : continue;
1000 : : }
1001 : : /* Find the next batch of constructors/destructors with the same
1002 : : initialization priority. */
1003 : 24 : for (;i < j; i++)
1004 : : {
1005 : 16 : tree call;
1006 : 16 : fn = cdtors[i];
1007 : 16 : call = build_call_expr (fn, 0);
1008 : 16 : if (ctor_p)
1009 : 8 : DECL_STATIC_CONSTRUCTOR (fn) = 0;
1010 : : else
1011 : 8 : DECL_STATIC_DESTRUCTOR (fn) = 0;
1012 : : /* We do not want to optimize away pure/const calls here.
1013 : : When optimizing, these should be already removed, when not
1014 : : optimizing, we want user to be able to breakpoint in them. */
1015 : 16 : TREE_SIDE_EFFECTS (call) = 1;
1016 : 16 : append_to_statement_list (call, &body);
1017 : : }
1018 : 8 : gcc_assert (body != NULL_TREE);
1019 : : /* Generate a function to call all the function of like
1020 : : priority. */
1021 : 16 : cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true,
1022 : 8 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (cdtors[0]),
1023 : 8 : DECL_FUNCTION_SPECIFIC_TARGET (cdtors[0]));
1024 : : }
1025 : 56 : }
1026 : :
1027 : : /* Helper functions for build_cxa_dtor_registrations ().
1028 : : Build a decl for __cxa_atexit (). */
1029 : :
1030 : : static tree
1031 : 0 : build_cxa_atexit_decl ()
1032 : : {
1033 : : /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
1034 : 0 : tree fn_type = build_function_type_list (void_type_node,
1035 : : ptr_type_node, NULL_TREE);
1036 : 0 : tree fn_ptr_type = build_pointer_type (fn_type);
1037 : : /* The declaration for `__cxa_atexit' is:
1038 : : int __cxa_atexit (void (*)(void *), void *, void *). */
1039 : 0 : const char *name = "__cxa_atexit";
1040 : 0 : tree cxa_name = get_identifier (name);
1041 : 0 : fn_type = build_function_type_list (integer_type_node, fn_ptr_type,
1042 : : ptr_type_node, ptr_type_node, NULL_TREE);
1043 : 0 : tree atexit_fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
1044 : : cxa_name, fn_type);
1045 : 0 : SET_DECL_ASSEMBLER_NAME (atexit_fndecl, cxa_name);
1046 : 0 : DECL_VISIBILITY (atexit_fndecl) = VISIBILITY_DEFAULT;
1047 : 0 : DECL_VISIBILITY_SPECIFIED (atexit_fndecl) = true;
1048 : 0 : set_call_expr_flags (atexit_fndecl, ECF_LEAF | ECF_NOTHROW);
1049 : 0 : TREE_PUBLIC (atexit_fndecl) = true;
1050 : 0 : DECL_EXTERNAL (atexit_fndecl) = true;
1051 : 0 : DECL_ARTIFICIAL (atexit_fndecl) = true;
1052 : 0 : return atexit_fndecl;
1053 : : }
1054 : :
1055 : : /* Build a decl for __dso_handle. */
1056 : :
1057 : : static tree
1058 : 0 : build_dso_handle_decl ()
1059 : : {
1060 : : /* Declare the __dso_handle variable. */
1061 : 0 : tree dso_handle_decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
1062 : : get_identifier ("__dso_handle"),
1063 : : ptr_type_node);
1064 : 0 : TREE_PUBLIC (dso_handle_decl) = true;
1065 : 0 : DECL_EXTERNAL (dso_handle_decl) = true;
1066 : 0 : DECL_ARTIFICIAL (dso_handle_decl) = true;
1067 : : #ifdef HAVE_GAS_HIDDEN
1068 : 0 : if (dso_handle_decl != error_mark_node)
1069 : : {
1070 : 0 : DECL_VISIBILITY (dso_handle_decl) = VISIBILITY_HIDDEN;
1071 : 0 : DECL_VISIBILITY_SPECIFIED (dso_handle_decl) = true;
1072 : : }
1073 : : #endif
1074 : 0 : return dso_handle_decl;
1075 : : }
1076 : :
1077 : : /* This builds one or more constructor functions that register DTORs with
1078 : : __cxa_atexit (). Within a priority level, DTORs are registered in TU
1079 : : order - which means that they will run in reverse TU order from cxa_atexit.
1080 : : This is the same behavior as using a .fini / .mod_term_funcs section.
1081 : : As the functions are built, they are appended to the CTORs vector. */
1082 : :
1083 : : static void
1084 : 0 : build_cxa_dtor_registrations (const vec<tree> &dtors, vec<tree> *ctors)
1085 : : {
1086 : 0 : size_t i,j;
1087 : 0 : size_t len = dtors.length ();
1088 : :
1089 : 0 : location_t sav_loc = input_location;
1090 : 0 : input_location = UNKNOWN_LOCATION;
1091 : :
1092 : 0 : tree atexit_fndecl = build_cxa_atexit_decl ();
1093 : 0 : tree dso_handle_decl = build_dso_handle_decl ();
1094 : :
1095 : : /* We want &__dso_handle. */
1096 : 0 : tree dso_ptr = build1_loc (UNKNOWN_LOCATION, ADDR_EXPR,
1097 : : ptr_type_node, dso_handle_decl);
1098 : :
1099 : 0 : i = 0;
1100 : 0 : while (i < len)
1101 : : {
1102 : 0 : priority_type priority = 0;
1103 : 0 : tree body = NULL_TREE;
1104 : 0 : j = i;
1105 : 0 : do
1106 : : {
1107 : 0 : priority_type p;
1108 : 0 : tree fn = dtors[j];
1109 : 0 : p = DECL_FINI_PRIORITY (fn);
1110 : 0 : if (j == i)
1111 : : priority = p;
1112 : 0 : else if (p != priority)
1113 : : break;
1114 : 0 : j++;
1115 : : }
1116 : 0 : while (j < len);
1117 : :
1118 : : /* Find the next batch of destructors with the same initialization
1119 : : priority. */
1120 : 0 : for (;i < j; i++)
1121 : : {
1122 : 0 : tree fn = dtors[i];
1123 : 0 : DECL_STATIC_DESTRUCTOR (fn) = 0;
1124 : 0 : tree dtor_ptr = build1_loc (UNKNOWN_LOCATION, ADDR_EXPR,
1125 : : ptr_type_node, fn);
1126 : 0 : tree call_cxa_atexit
1127 : 0 : = build_call_expr_loc (UNKNOWN_LOCATION, atexit_fndecl, 3,
1128 : : dtor_ptr, null_pointer_node, dso_ptr);
1129 : 0 : TREE_SIDE_EFFECTS (call_cxa_atexit) = 1;
1130 : 0 : append_to_statement_list (call_cxa_atexit, &body);
1131 : : }
1132 : :
1133 : 0 : gcc_assert (body != NULL_TREE);
1134 : : /* Generate a function to register the DTORs at this priority. */
1135 : 0 : tree new_ctor
1136 : 0 : = cgraph_build_static_cdtor_1 ('I', body, priority, true,
1137 : 0 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (dtors[0]),
1138 : 0 : DECL_FUNCTION_SPECIFIC_TARGET (dtors[0]));
1139 : : /* Add this to the list of ctors. */
1140 : 0 : ctors->safe_push (new_ctor);
1141 : : }
1142 : 0 : input_location = sav_loc;
1143 : 0 : }
1144 : :
1145 : : /* Comparison function for qsort. P1 and P2 are actually of type
1146 : : "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1147 : : used to determine the sort order. */
1148 : :
1149 : : static int
1150 : 36 : compare_ctor (const void *p1, const void *p2)
1151 : : {
1152 : 36 : tree f1;
1153 : 36 : tree f2;
1154 : 36 : int priority1;
1155 : 36 : int priority2;
1156 : :
1157 : 36 : f1 = *(const tree *)p1;
1158 : 36 : f2 = *(const tree *)p2;
1159 : 36 : priority1 = DECL_INIT_PRIORITY (f1);
1160 : 36 : priority2 = DECL_INIT_PRIORITY (f2);
1161 : :
1162 : 36 : if (priority1 < priority2)
1163 : : return -1;
1164 : 22 : else if (priority1 > priority2)
1165 : : return 1;
1166 : : else
1167 : : /* Ensure a stable sort. Constructors are executed in backwarding
1168 : : order to make LTO initialize braries first. */
1169 : 16 : return DECL_UID (f2) - DECL_UID (f1);
1170 : : }
1171 : :
1172 : : /* Comparison function for qsort. P1 and P2 are actually of type
1173 : : "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1174 : : used to determine the sort order. */
1175 : :
1176 : : static int
1177 : 36 : compare_dtor (const void *p1, const void *p2)
1178 : : {
1179 : 36 : tree f1;
1180 : 36 : tree f2;
1181 : 36 : int priority1;
1182 : 36 : int priority2;
1183 : :
1184 : 36 : f1 = *(const tree *)p1;
1185 : 36 : f2 = *(const tree *)p2;
1186 : 36 : priority1 = DECL_FINI_PRIORITY (f1);
1187 : 36 : priority2 = DECL_FINI_PRIORITY (f2);
1188 : :
1189 : 36 : if (priority1 < priority2)
1190 : : return -1;
1191 : 22 : else if (priority1 > priority2)
1192 : : return 1;
1193 : : else
1194 : : /* Ensure a stable sort - into TU order. */
1195 : 16 : return DECL_UID (f1) - DECL_UID (f2);
1196 : : }
1197 : :
1198 : : /* Comparison function for qsort. P1 and P2 are of type "tree *" and point to
1199 : : a pair of static constructors or destructors. We first sort on the basis of
1200 : : priority and then into TU order (on the strict assumption that DECL_UIDs are
1201 : : ordered in the same way as the original functions). ???: this seems quite
1202 : : fragile. */
1203 : :
1204 : : static int
1205 : 0 : compare_cdtor_tu_order (const void *p1, const void *p2)
1206 : : {
1207 : 0 : tree f1;
1208 : 0 : tree f2;
1209 : 0 : int priority1;
1210 : 0 : int priority2;
1211 : :
1212 : 0 : f1 = *(const tree *)p1;
1213 : 0 : f2 = *(const tree *)p2;
1214 : : /* We process the DTORs first, and then remove their flag, so this order
1215 : : allows for functions that are declared as both CTOR and DTOR. */
1216 : 0 : if (DECL_STATIC_DESTRUCTOR (f1))
1217 : : {
1218 : 0 : gcc_checking_assert (DECL_STATIC_DESTRUCTOR (f2));
1219 : 0 : priority1 = DECL_FINI_PRIORITY (f1);
1220 : 0 : priority2 = DECL_FINI_PRIORITY (f2);
1221 : : }
1222 : : else
1223 : : {
1224 : 0 : priority1 = DECL_INIT_PRIORITY (f1);
1225 : 0 : priority2 = DECL_INIT_PRIORITY (f2);
1226 : : }
1227 : :
1228 : 0 : if (priority1 < priority2)
1229 : : return -1;
1230 : 0 : else if (priority1 > priority2)
1231 : : return 1;
1232 : : else
1233 : : /* For equal priority, sort into the order of definition in the TU. */
1234 : 0 : return DECL_UID (f1) - DECL_UID (f2);
1235 : : }
1236 : :
1237 : : /* Generate functions to call static constructors and destructors
1238 : : for targets that do not support .ctors/.dtors sections. These
1239 : : functions have magic names which are detected by collect2. */
1240 : :
1241 : : static void
1242 : 12018 : build_cdtor_fns (vec<tree> *ctors, vec<tree> *dtors)
1243 : : {
1244 : 12018 : if (!ctors->is_empty ())
1245 : : {
1246 : 48 : gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1247 : 48 : ctors->qsort (compare_ctor);
1248 : 48 : build_cdtor (/*ctor_p=*/true, *ctors);
1249 : : }
1250 : :
1251 : 12018 : if (!dtors->is_empty ())
1252 : : {
1253 : 8 : gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1254 : 8 : dtors->qsort (compare_dtor);
1255 : 8 : build_cdtor (/*ctor_p=*/false, *dtors);
1256 : : }
1257 : 12018 : }
1258 : :
1259 : : /* Generate new CTORs to register static destructors with __cxa_atexit and add
1260 : : them to the existing list of CTORs; we then process the revised CTORs list.
1261 : :
1262 : : We sort the DTORs into priority and then TU order, this means that they are
1263 : : registered in that order with __cxa_atexit () and therefore will be run in
1264 : : the reverse order.
1265 : :
1266 : : Likewise, CTORs are sorted into priority and then TU order, which means that
1267 : : they will run in that order.
1268 : :
1269 : : This matches the behavior of using init/fini or mod_init_func/mod_term_func
1270 : : sections. */
1271 : :
1272 : : static void
1273 : 0 : build_cxa_atexit_fns (vec<tree> *ctors, vec<tree> *dtors)
1274 : : {
1275 : 0 : if (!dtors->is_empty ())
1276 : : {
1277 : 0 : gcc_assert (targetm.dtors_from_cxa_atexit);
1278 : 0 : dtors->qsort (compare_cdtor_tu_order);
1279 : 0 : build_cxa_dtor_registrations (*dtors, ctors);
1280 : : }
1281 : :
1282 : 0 : if (!ctors->is_empty ())
1283 : : {
1284 : 0 : gcc_assert (targetm.dtors_from_cxa_atexit);
1285 : 0 : ctors->qsort (compare_cdtor_tu_order);
1286 : 0 : build_cdtor (/*ctor_p=*/true, *ctors);
1287 : : }
1288 : 0 : }
1289 : :
1290 : : /* Look for constructors and destructors and produce function calling them.
1291 : : This is needed for targets not supporting ctors or dtors, but we perform the
1292 : : transformation also at linktime to merge possibly numerous
1293 : : constructors/destructors into single function to improve code locality and
1294 : : reduce size. */
1295 : :
1296 : : static unsigned int
1297 : 12018 : ipa_cdtor_merge (void)
1298 : : {
1299 : : /* A vector of FUNCTION_DECLs declared as static constructors. */
1300 : 12018 : auto_vec<tree, 20> ctors;
1301 : : /* A vector of FUNCTION_DECLs declared as static destructors. */
1302 : 12018 : auto_vec<tree, 20> dtors;
1303 : 12018 : struct cgraph_node *node;
1304 : 88147 : FOR_EACH_DEFINED_FUNCTION (node)
1305 : 76129 : if (DECL_STATIC_CONSTRUCTOR (node->decl)
1306 : 76129 : || DECL_STATIC_DESTRUCTOR (node->decl))
1307 : 69 : record_cdtor_fn (node, &ctors, &dtors);
1308 : 12018 : if (targetm.dtors_from_cxa_atexit)
1309 : 0 : build_cxa_atexit_fns (&ctors, &dtors);
1310 : : else
1311 : 12018 : build_cdtor_fns (&ctors, &dtors);
1312 : 12018 : return 0;
1313 : 12018 : }
1314 : :
1315 : : namespace {
1316 : :
1317 : : const pass_data pass_data_ipa_cdtor_merge =
1318 : : {
1319 : : IPA_PASS, /* type */
1320 : : "cdtor", /* name */
1321 : : OPTGROUP_NONE, /* optinfo_flags */
1322 : : TV_CGRAPHOPT, /* tv_id */
1323 : : 0, /* properties_required */
1324 : : 0, /* properties_provided */
1325 : : 0, /* properties_destroyed */
1326 : : 0, /* todo_flags_start */
1327 : : 0, /* todo_flags_finish */
1328 : : };
1329 : :
1330 : : class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1331 : : {
1332 : : public:
1333 : 281914 : pass_ipa_cdtor_merge (gcc::context *ctxt)
1334 : : : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1335 : : NULL, /* generate_summary */
1336 : : NULL, /* write_summary */
1337 : : NULL, /* read_summary */
1338 : : NULL, /* write_optimization_summary */
1339 : : NULL, /* read_optimization_summary */
1340 : : NULL, /* stmt_fixup */
1341 : : 0, /* function_transform_todo_flags_start */
1342 : : NULL, /* function_transform */
1343 : 281914 : NULL) /* variable_transform */
1344 : 281914 : {}
1345 : :
1346 : : /* opt_pass methods: */
1347 : : bool gate (function *) final override;
1348 : 12018 : unsigned int execute (function *) final override
1349 : : {
1350 : 12018 : return ipa_cdtor_merge ();
1351 : : }
1352 : :
1353 : : }; // class pass_ipa_cdtor_merge
1354 : :
1355 : : bool
1356 : 557041 : pass_ipa_cdtor_merge::gate (function *)
1357 : : {
1358 : : /* Perform the pass when we have no ctors/dtors support
1359 : : or at LTO time to merge multiple constructors into single
1360 : : function. */
1361 : 557041 : return !targetm.have_ctors_dtors || in_lto_p || targetm.dtors_from_cxa_atexit;
1362 : : }
1363 : :
1364 : : } // anon namespace
1365 : :
1366 : : ipa_opt_pass_d *
1367 : 281914 : make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1368 : : {
1369 : 281914 : return new pass_ipa_cdtor_merge (ctxt);
1370 : : }
1371 : :
1372 : : /* Invalid pointer representing BOTTOM for single user dataflow. */
1373 : : #define BOTTOM ((cgraph_node *)(size_t) 2)
1374 : :
1375 : : /* Meet operation for single user dataflow.
1376 : : Here we want to associate variables with sigle function that may access it.
1377 : :
1378 : : FUNCTION is current single user of a variable, VAR is variable that uses it.
1379 : : Latttice is stored in SINGLE_USER_MAP.
1380 : :
1381 : : We represent:
1382 : : - TOP by no entry in SIGNLE_USER_MAP
1383 : : - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1384 : : - known single user by cgraph pointer in SINGLE_USER_MAP. */
1385 : :
1386 : : cgraph_node *
1387 : 3636001 : meet (cgraph_node *function, varpool_node *var,
1388 : : hash_map<varpool_node *, cgraph_node *> &single_user_map)
1389 : : {
1390 : 3636001 : struct cgraph_node *user, **f;
1391 : :
1392 : 3636001 : if (var->aux == BOTTOM)
1393 : : return BOTTOM;
1394 : :
1395 : 2656829 : f = single_user_map.get (var);
1396 : 2656829 : if (!f)
1397 : : return function;
1398 : 1104016 : user = *f;
1399 : 1104016 : if (!function)
1400 : : return user;
1401 : 1082855 : else if (function != user)
1402 : : return BOTTOM;
1403 : : else
1404 : : return function;
1405 : : }
1406 : :
1407 : : /* Propagation step of single-use dataflow.
1408 : :
1409 : : Check all uses of VNODE and see if they are used by single function FUNCTION.
1410 : : SINGLE_USER_MAP represents the dataflow lattice. */
1411 : :
1412 : : cgraph_node *
1413 : 1763389 : propagate_single_user (varpool_node *vnode, cgraph_node *function,
1414 : : hash_map<varpool_node *, cgraph_node *> &single_user_map)
1415 : : {
1416 : 1763389 : int i;
1417 : 1763389 : struct ipa_ref *ref;
1418 : :
1419 : 1763389 : gcc_assert (!vnode->externally_visible);
1420 : :
1421 : : /* If node is an alias, first meet with its target. */
1422 : 1763389 : if (vnode->alias)
1423 : 12583 : function = meet (function, vnode->get_alias_target (), single_user_map);
1424 : :
1425 : : /* Check all users and see if they correspond to a single function. */
1426 : 5940383 : for (i = 0; vnode->iterate_referring (i, ref) && function != BOTTOM; i++)
1427 : : {
1428 : 8353988 : struct cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
1429 : 4176994 : if (cnode)
1430 : : {
1431 : 553576 : if (cnode->inlined_to)
1432 : 73407 : cnode = cnode->inlined_to;
1433 : 553576 : if (!function)
1434 : : function = cnode;
1435 : 269725 : else if (function != cnode)
1436 : 22785 : function = BOTTOM;
1437 : : }
1438 : : else
1439 : 7246836 : function = meet (function, dyn_cast <varpool_node *> (ref->referring),
1440 : : single_user_map);
1441 : : }
1442 : 1763389 : return function;
1443 : : }
1444 : :
1445 : : /* Pass setting used_by_single_function flag.
1446 : : This flag is set on variable when there is only one function that may
1447 : : possibly referr to it. */
1448 : :
1449 : : static unsigned int
1450 : 227672 : ipa_single_use (void)
1451 : : {
1452 : 227672 : varpool_node *first = (varpool_node *) (void *) 1;
1453 : 227672 : varpool_node *var;
1454 : 227672 : hash_map<varpool_node *, cgraph_node *> single_user_map;
1455 : :
1456 : 3076396 : FOR_EACH_DEFINED_VARIABLE (var)
1457 : 2848724 : if (!var->all_refs_explicit_p ())
1458 : 1567333 : var->aux = BOTTOM;
1459 : : else
1460 : : {
1461 : : /* Enqueue symbol for dataflow. */
1462 : 1281391 : var->aux = first;
1463 : 1281391 : first = var;
1464 : : }
1465 : :
1466 : : /* The actual dataflow. */
1467 : :
1468 : 1991061 : while (first != (void *) 1)
1469 : : {
1470 : 1763389 : cgraph_node *user, *orig_user, **f;
1471 : :
1472 : 1763389 : var = first;
1473 : 1763389 : first = (varpool_node *)first->aux;
1474 : :
1475 : 1763389 : f = single_user_map.get (var);
1476 : 1763389 : if (f)
1477 : 28930 : orig_user = *f;
1478 : : else
1479 : : orig_user = NULL;
1480 : 1763389 : user = propagate_single_user (var, orig_user, single_user_map);
1481 : :
1482 : 1763389 : gcc_checking_assert (var->aux != BOTTOM);
1483 : :
1484 : : /* If user differs, enqueue all references. */
1485 : 1763389 : if (user != orig_user)
1486 : : {
1487 : 1285821 : unsigned int i;
1488 : 1285821 : ipa_ref *ref;
1489 : :
1490 : 1285821 : single_user_map.put (var, user);
1491 : :
1492 : : /* Enqueue all aliases for re-processing. */
1493 : 2583538 : for (i = 0; var->iterate_direct_aliases (i, ref); i++)
1494 : 11896 : if (!ref->referring->aux)
1495 : : {
1496 : 2548 : ref->referring->aux = first;
1497 : 11896 : first = dyn_cast <varpool_node *> (ref->referring);
1498 : : }
1499 : : /* Enqueue all users for re-processing. */
1500 : 5165404 : for (i = 0; var->iterate_reference (i, ref); i++)
1501 : 1296881 : if (!ref->referred->aux
1502 : 732364 : && ref->referred->definition
1503 : 2508695 : && is_a <varpool_node *> (ref->referred))
1504 : : {
1505 : 479450 : ref->referred->aux = first;
1506 : 479450 : first = dyn_cast <varpool_node *> (ref->referred);
1507 : : }
1508 : :
1509 : : /* If user is BOTTOM, just punt on this var. */
1510 : 1285821 : if (user == BOTTOM)
1511 : 1005274 : var->aux = BOTTOM;
1512 : : else
1513 : 280547 : var->aux = NULL;
1514 : : }
1515 : : else
1516 : 477568 : var->aux = NULL;
1517 : : }
1518 : :
1519 : 3076396 : FOR_EACH_DEFINED_VARIABLE (var)
1520 : : {
1521 : 2848724 : if (var->aux != BOTTOM)
1522 : : {
1523 : : /* Not having the single user known means that the VAR is
1524 : : unreachable. Either someone forgot to remove unreachable
1525 : : variables or the reachability here is wrong. */
1526 : :
1527 : 276117 : gcc_checking_assert (single_user_map.get (var));
1528 : :
1529 : 276117 : if (dump_file)
1530 : : {
1531 : 10 : fprintf (dump_file, "Variable %s is used by single function\n",
1532 : : var->dump_name ());
1533 : : }
1534 : 276117 : var->used_by_single_function = true;
1535 : : }
1536 : 2848724 : var->aux = NULL;
1537 : : }
1538 : 227672 : return 0;
1539 : 227672 : }
1540 : :
1541 : : namespace {
1542 : :
1543 : : const pass_data pass_data_ipa_single_use =
1544 : : {
1545 : : IPA_PASS, /* type */
1546 : : "single-use", /* name */
1547 : : OPTGROUP_NONE, /* optinfo_flags */
1548 : : TV_CGRAPHOPT, /* tv_id */
1549 : : 0, /* properties_required */
1550 : : 0, /* properties_provided */
1551 : : 0, /* properties_destroyed */
1552 : : 0, /* todo_flags_start */
1553 : : 0, /* todo_flags_finish */
1554 : : };
1555 : :
1556 : : class pass_ipa_single_use : public ipa_opt_pass_d
1557 : : {
1558 : : public:
1559 : 281914 : pass_ipa_single_use (gcc::context *ctxt)
1560 : : : ipa_opt_pass_d (pass_data_ipa_single_use, ctxt,
1561 : : NULL, /* generate_summary */
1562 : : NULL, /* write_summary */
1563 : : NULL, /* read_summary */
1564 : : NULL, /* write_optimization_summary */
1565 : : NULL, /* read_optimization_summary */
1566 : : NULL, /* stmt_fixup */
1567 : : 0, /* function_transform_todo_flags_start */
1568 : : NULL, /* function_transform */
1569 : 281914 : NULL) /* variable_transform */
1570 : 281914 : {}
1571 : :
1572 : : /* opt_pass methods: */
1573 : 227672 : unsigned int execute (function *) final override { return ipa_single_use (); }
1574 : :
1575 : : }; // class pass_ipa_single_use
1576 : :
1577 : : } // anon namespace
1578 : :
1579 : : ipa_opt_pass_d *
1580 : 281914 : make_pass_ipa_single_use (gcc::context *ctxt)
1581 : : {
1582 : 281914 : return new pass_ipa_single_use (ctxt);
1583 : : }
1584 : :
|