Branch data Line data Source code
1 : : /* Basic IPA optimizations and utilities.
2 : : Copyright (C) 2003-2025 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 : 3264080 : has_addr_references_p (struct cgraph_node *node,
49 : : void *)
50 : : {
51 : 3264080 : int i;
52 : 3264080 : struct ipa_ref *ref = NULL;
53 : :
54 : 3360606 : for (i = 0; node->iterate_referring (i, ref); i++)
55 : 3273369 : 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 : 380 : is_indirect_call_target_p (struct cgraph_node *node, void *)
64 : : {
65 : 380 : 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 : 138826970 : enqueue_node (symtab_node *node, symtab_node **first,
93 : : hash_set<symtab_node *> *reachable)
94 : : {
95 : : /* Node is still in queue; do nothing. */
96 : 138826970 : 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 : 77134995 : if (node->aux == (void *)2 && !reachable->contains (node))
101 : : return;
102 : 48924676 : node->aux = *first;
103 : 48924676 : *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 : 1373766 : possible_inline_candidate_p (symtab_node *node)
112 : : {
113 : 1373766 : if (symtab->state >= IPA_SSA_AFTER_INLINING)
114 : : return false;
115 : 1310532 : cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
116 : 1280433 : if (!cnode)
117 : : return false;
118 : 1280433 : if (DECL_UNINLINABLE (cnode->decl))
119 : : return false;
120 : 1278450 : if (opt_for_fn (cnode->decl, optimize))
121 : : return true;
122 : 1450 : if (symtab->state >= IPA_SSA)
123 : : return false;
124 : 1389 : return lookup_attribute ("always_inline", DECL_ATTRIBUTES (node->decl));
125 : : }
126 : :
127 : : /* Process references. */
128 : :
129 : : static void
130 : 35512129 : process_references (symtab_node *snode,
131 : : symtab_node **first,
132 : : hash_set<symtab_node *> *reachable)
133 : : {
134 : 35512129 : int i;
135 : 35512129 : struct ipa_ref *ref = NULL;
136 : 102258522 : for (i = 0; snode->iterate_reference (i, ref); i++)
137 : : {
138 : 66746393 : symtab_node *node = ref->referred;
139 : 66746393 : symtab_node *body = node->ultimate_alias_target ();
140 : :
141 : 66746393 : if (node->definition && !node->in_other_partition
142 : 66746393 : && ((!DECL_EXTERNAL (node->decl) || node->alias)
143 : 90256 : || (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 : 33673 : || (VAR_P (node->decl)
148 : 30099 : && (flag_wpa
149 : 30099 : || 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 : 48198655 : if (DECL_EXTERNAL (node->decl)
157 : 57443 : && node->alias
158 : 48199515 : && symtab->state < IPA_SSA_AFTER_INLINING)
159 : 689 : reachable->add (body);
160 : 48198655 : reachable->add (node);
161 : : }
162 : 66746393 : enqueue_node (node, first, reachable);
163 : : }
164 : 35512129 : }
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 : 174076 : 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 : 174076 : unsigned int i;
181 : 174076 : void *cache_token;
182 : 174076 : bool final;
183 : 174076 : vec <cgraph_node *>targets
184 : : = possible_polymorphic_call_targets
185 : 174076 : (edge, &final, &cache_token);
186 : :
187 : 174076 : if (cache_token != NULL && !reachable_call_targets->add (cache_token))
188 : : {
189 : 210573 : for (i = 0; i < targets.length (); i++)
190 : : {
191 : 122783 : 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 : 122783 : if (TREE_CODE (TREE_TYPE (n->decl)) == METHOD_TYPE
197 : 239156 : && type_in_anonymous_namespace_p
198 : 116373 : (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl))))
199 : 5381 : continue;
200 : :
201 : 117402 : n->indirect_call_target = true;
202 : 117402 : symtab_node *body = n->function_symbol ();
203 : :
204 : : /* Prior inlining, keep alive bodies of possible targets for
205 : : devirtualization. */
206 : 117402 : if (n->definition
207 : 117402 : && (possible_inline_candidate_p (body)
208 : 86634 : && opt_for_fn (body->decl, flag_devirtualize)))
209 : : {
210 : : /* Be sure that we will not optimize out alias target
211 : : body. */
212 : 86634 : if (DECL_EXTERNAL (n->decl)
213 : 2952 : && n->alias
214 : 86634 : && symtab->state < IPA_SSA_AFTER_INLINING)
215 : 0 : reachable->add (body);
216 : 86634 : 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 : 117402 : 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 : 174076 : if (final)
230 : : {
231 : 243 : if (targets.length () <= 1 && dbg_cnt (devirt))
232 : : {
233 : 15 : cgraph_node *target, *node = edge->caller;
234 : 15 : if (targets.length () == 1)
235 : 6 : target = targets[0];
236 : : else
237 : 9 : target = cgraph_node::get_create (builtin_decl_unreachable ());
238 : :
239 : 15 : 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 : 15 : edge = cgraph_edge::make_direct (edge, target);
247 : 15 : if (ipa_fn_summaries)
248 : 3 : ipa_update_overall_fn_summary (node->inlined_to
249 : : ? node->inlined_to : node);
250 : 12 : else if (edge->call_stmt)
251 : 12 : cgraph_edge::redirect_call_stmt_to_callee (edge);
252 : : }
253 : : }
254 : 174076 : }
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 : 1439165 : symbol_table::remove_unreachable_nodes (FILE *file)
311 : : {
312 : 1439165 : symtab_node *first = (symtab_node *) (void *) 1;
313 : 1439165 : struct cgraph_node *node, *next;
314 : 1439165 : varpool_node *vnode, *vnext;
315 : 1439165 : bool changed = false;
316 : 1439165 : hash_set<symtab_node *> reachable;
317 : 1439165 : hash_set<tree> body_needed_for_clonning;
318 : 1439165 : hash_set<void *> reachable_call_targets;
319 : :
320 : 1439165 : timevar_push (TV_IPA_UNREACHABLE);
321 : 1439165 : build_type_inheritance_graph ();
322 : 1439165 : if (file)
323 : 673 : fprintf (file, "\nReclaiming functions:");
324 : 1439165 : if (flag_checking)
325 : : {
326 : 57171428 : FOR_EACH_FUNCTION (node)
327 : 27146650 : gcc_assert (!node->aux);
328 : 45629714 : FOR_EACH_VARIABLE (vnode)
329 : 22095325 : 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 : 57172276 : FOR_EACH_FUNCTION (node)
336 : : {
337 : 27146973 : node->used_as_abstract_origin = false;
338 : 27146973 : node->indirect_call_target = false;
339 : 27146973 : if (node->definition
340 : 15817687 : && !node->inlined_to
341 : 13781798 : && !node->in_other_partition
342 : 40928537 : && !node->can_remove_if_no_direct_calls_and_refs_p ())
343 : : {
344 : 7582235 : gcc_assert (!node->inlined_to);
345 : 7582235 : reachable.add (node);
346 : 7582235 : enqueue_node (node, &first, &reachable);
347 : : }
348 : : else
349 : 19564738 : gcc_assert (!node->aux);
350 : : }
351 : :
352 : : /* Mark variables that are obviously needed. */
353 : 21472160 : FOR_EACH_DEFINED_VARIABLE (vnode)
354 : 20032995 : if (!vnode->can_remove_if_no_refs_p()
355 : 20032995 : && !vnode->in_other_partition)
356 : : {
357 : 10070668 : reachable.add (vnode);
358 : 10070668 : enqueue_node (vnode, &first, &reachable);
359 : : }
360 : :
361 : : /* Perform reachability analysis. */
362 : 50363841 : while (first != (symtab_node *) (void *) 1)
363 : : {
364 : 48924676 : bool in_boundary_p = !reachable.contains (first);
365 : 48924676 : symtab_node *node = first;
366 : :
367 : 48924676 : 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 : 48924676 : if (in_boundary_p)
372 : : {
373 : 13412547 : node->aux = (void *)2;
374 : 13412547 : if (node->alias && node->analyzed)
375 : 3590 : enqueue_node (node->get_alias_target (), &first, &reachable);
376 : : }
377 : : else
378 : : {
379 : 35512129 : if (TREE_CODE (node->decl) == FUNCTION_DECL
380 : 35512129 : && DECL_ABSTRACT_ORIGIN (node->decl))
381 : : {
382 : 3049378 : struct cgraph_node *origin_node
383 : 3049378 : = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node->decl));
384 : 3049378 : if (origin_node && !origin_node->used_as_abstract_origin)
385 : : {
386 : 356040 : origin_node->used_as_abstract_origin = true;
387 : 356040 : gcc_assert (!origin_node->prev_sibling_clone);
388 : 356040 : gcc_assert (!origin_node->next_sibling_clone);
389 : 579664 : for (cgraph_node *n = origin_node->clones; n;
390 : 223624 : n = n->next_sibling_clone)
391 : 223624 : if (n->decl == DECL_ABSTRACT_ORIGIN (node->decl))
392 : 197595 : 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 : 35512129 : if (node->same_comdat_group
400 : 1561412 : && node->externally_visible
401 : 37048343 : && !DECL_EXTERNAL (node->decl))
402 : : {
403 : 1536214 : symtab_node *next;
404 : 1536214 : for (next = node->same_comdat_group;
405 : 4723300 : next != node;
406 : 3187086 : next = next->same_comdat_group)
407 : 6374172 : if (!next->comdat_local_p ()
408 : 3121333 : && !DECL_EXTERNAL (next->decl)
409 : 3121330 : && !reachable.add (next))
410 : 731122 : enqueue_node (next, &first, &reachable);
411 : : }
412 : : /* Mark references as reachable. */
413 : 35512129 : process_references (node, &first, &reachable);
414 : : }
415 : :
416 : 48924676 : 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 : 26869849 : if (!in_boundary_p)
421 : : {
422 : 15518744 : struct cgraph_edge *e;
423 : : /* Keep alive possible targets for devirtualization. */
424 : 15518744 : if (opt_for_fn (cnode->decl, optimize)
425 : 15518744 : && opt_for_fn (cnode->decl, flag_devirtualize))
426 : : {
427 : 12700220 : struct cgraph_edge *next;
428 : 13846920 : for (e = cnode->indirect_calls; e; e = next)
429 : : {
430 : 1146700 : next = e->next_callee;
431 : 1146700 : if (e->indirect_info->polymorphic)
432 : 174076 : walk_polymorphic_call_targets (&reachable_call_targets,
433 : : e, &first, &reachable);
434 : : }
435 : : }
436 : 68319910 : for (e = cnode->callees; e; e = e->next_callee)
437 : : {
438 : 52801166 : symtab_node *body = e->callee->function_symbol ();
439 : 52801166 : if (e->callee->definition
440 : 52801166 : && !e->callee->in_other_partition
441 : 52801166 : && (!e->inline_failed
442 : 18130299 : || !DECL_EXTERNAL (e->callee->decl)
443 : 1364764 : || e->callee->alias
444 : 1185843 : || possible_inline_candidate_p (e->callee)))
445 : : {
446 : : /* Be sure that we will not optimize out alias target
447 : : body. */
448 : 20094673 : if (DECL_EXTERNAL (e->callee->decl)
449 : 1575870 : && e->callee->alias
450 : 20273594 : && symtab->state < IPA_SSA_AFTER_INLINING)
451 : 177509 : reachable.add (body);
452 : 20094673 : reachable.add (e->callee);
453 : : }
454 : 52801166 : enqueue_node (e->callee, &first, &reachable);
455 : : }
456 : :
457 : : /* When inline clone exists, mark body to be preserved so when removing
458 : : offline copy of the function we don't kill it. */
459 : 15518744 : if (cnode->inlined_to)
460 : 2015073 : body_needed_for_clonning.add (cnode->decl);
461 : :
462 : : /* For non-inline clones, force their origins to the boundary and ensure
463 : : that body is not removed. */
464 : 18215528 : while (cnode->clone_of)
465 : : {
466 : 2696784 : bool noninline = cnode->clone_of->decl != cnode->decl;
467 : 2696784 : cnode = cnode->clone_of;
468 : 2696784 : if (noninline)
469 : : {
470 : 679189 : body_needed_for_clonning.add (cnode->decl);
471 : 679189 : enqueue_node (cnode, &first, &reachable);
472 : : }
473 : : }
474 : :
475 : : }
476 : 11351105 : else if (cnode->thunk)
477 : 42 : enqueue_node (cnode->callees->callee, &first, &reachable);
478 : :
479 : : /* If any reachable function has simd clones, mark them as
480 : : reachable as well. */
481 : 26869849 : if (cnode->simd_clones)
482 : : {
483 : : cgraph_node *next;
484 : 0 : for (next = cnode->simd_clones;
485 : 0 : next;
486 : 0 : next = next->simdclone->next_clone)
487 : 0 : if (in_boundary_p
488 : 0 : || !reachable.add (next))
489 : 0 : enqueue_node (next, &first, &reachable);
490 : : }
491 : : }
492 : : /* When we see constructor of external variable, keep referred nodes in the
493 : : boundary. This will also hold initializers of the external vars NODE
494 : : refers to. */
495 : 48924676 : varpool_node *vnode = dyn_cast <varpool_node *> (node);
496 : 48924676 : if (vnode
497 : 22054827 : && DECL_EXTERNAL (node->decl)
498 : 2061209 : && !vnode->alias
499 : : && in_boundary_p)
500 : : {
501 : 2156370 : struct ipa_ref *ref = NULL;
502 : 5656742 : for (int i = 0; node->iterate_reference (i, ref); i++)
503 : 95163 : enqueue_node (ref->referred, &first, &reachable);
504 : : }
505 : : }
506 : :
507 : : /* Remove unreachable functions. */
508 : 30030460 : for (node = first_function (); node; node = next)
509 : : {
510 : 27152130 : next = next_function (node);
511 : :
512 : : /* If node is not needed at all, remove it. */
513 : 27152130 : if (!node->aux)
514 : : {
515 : 288568 : if (file)
516 : 89 : fprintf (file, " %s", node->dump_name ());
517 : 288568 : node->remove ();
518 : 288568 : changed = true;
519 : : }
520 : : /* If node is unreachable, remove its body. */
521 : 26863562 : else if (!reachable.contains (node))
522 : : {
523 : : /* We keep definitions of thunks and aliases in the boundary so
524 : : we can walk to the ultimate alias targets and function symbols
525 : : reliably. */
526 : 11344818 : if (node->alias || node->thunk)
527 : : ;
528 : 11340672 : else if (!body_needed_for_clonning.contains (node->decl))
529 : : {
530 : : /* Make the node a non-clone so that we do not attempt to
531 : : materialize it later. */
532 : 11021322 : if (node->clone_of)
533 : 0 : node->remove_from_clone_tree ();
534 : 11021322 : node->release_body ();
535 : : }
536 : 319350 : else if (!node->clone_of)
537 : 310864 : gcc_assert (in_lto_p || DECL_RESULT (node->decl));
538 : 11344818 : if (node->definition && !node->alias && !node->thunk)
539 : : {
540 : 128100 : if (file)
541 : 189 : fprintf (file, " %s", node->dump_name ());
542 : 128100 : node->body_removed = true;
543 : 128100 : node->analyzed = false;
544 : 128100 : node->definition = false;
545 : 128100 : node->cpp_implicit_alias = false;
546 : 128100 : node->alias = false;
547 : 128100 : node->transparent_alias = false;
548 : 128100 : node->thunk = false;
549 : 128100 : node->weakref = false;
550 : : /* After early inlining we drop always_inline attributes on
551 : : bodies of functions that are still referenced (have their
552 : : address taken). */
553 : 256200 : DECL_ATTRIBUTES (node->decl)
554 : 128100 : = remove_attribute ("always_inline",
555 : 128100 : DECL_ATTRIBUTES (node->decl));
556 : 128100 : if (!node->in_other_partition)
557 : 127923 : node->local = false;
558 : 128100 : node->remove_callees ();
559 : 128100 : node->remove_all_references ();
560 : 128100 : changed = true;
561 : : }
562 : : }
563 : : else
564 : 15518744 : gcc_assert (node->clone_of || !node->has_gimple_body_p ()
565 : : || in_lto_p || DECL_RESULT (node->decl));
566 : : }
567 : :
568 : : /* Inline clones might be kept around so their materializing allows further
569 : : cloning. If the function the clone is inlined into is removed, we need
570 : : to turn it into normal cone. */
571 : 29741892 : FOR_EACH_FUNCTION (node)
572 : : {
573 : 26863562 : if (node->inlined_to
574 : 2015073 : && !node->callers)
575 : : {
576 : 0 : gcc_assert (node->clones);
577 : 0 : node->inlined_to = NULL;
578 : 0 : update_inlined_to_pointer (node, node);
579 : : }
580 : 26863562 : node->aux = NULL;
581 : : }
582 : :
583 : : /* Remove unreachable variables. */
584 : 1439165 : if (file)
585 : 673 : fprintf (file, "\nReclaiming variables:");
586 : 24974075 : for (vnode = first_variable (); vnode; vnode = vnext)
587 : : {
588 : 22095745 : vnext = next_variable (vnode);
589 : 22095745 : if (!vnode->aux
590 : : /* For can_refer_decl_in_current_unit_p we want to track for
591 : : all external variables if they are defined in other partition
592 : : or not. */
593 : 22095745 : && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
594 : : {
595 : 40879 : struct ipa_ref *ref = NULL;
596 : :
597 : : /* First remove the aliases, so varpool::remove can possibly lookup
598 : : the constructor and save it for future use. */
599 : 40879 : while (vnode->iterate_direct_aliases (0, ref))
600 : : {
601 : 0 : if (file)
602 : 0 : fprintf (file, " %s", ref->referred->dump_name ());
603 : 0 : ref->referring->remove ();
604 : : }
605 : 40879 : if (file)
606 : 1 : fprintf (file, " %s", vnode->dump_name ());
607 : 40879 : vnext = next_variable (vnode);
608 : : /* Signal removal to the debug machinery. */
609 : 40879 : if (! flag_wpa || flag_incremental_link == INCREMENTAL_LINK_LTO)
610 : : {
611 : 38600 : vnode->definition = false;
612 : 38600 : (*debug_hooks->late_global_decl) (vnode->decl);
613 : : }
614 : 40879 : vnode->remove ();
615 : 40879 : changed = true;
616 : : }
617 : 22054866 : else if (!reachable.contains (vnode) && !vnode->alias)
618 : : {
619 : 2061269 : tree init;
620 : 2061269 : if (vnode->definition)
621 : : {
622 : 15713 : if (file)
623 : 0 : fprintf (file, " %s", vnode->dump_name ());
624 : : changed = true;
625 : : }
626 : : /* Keep body if it may be useful for constant folding. */
627 : 2056688 : if ((flag_wpa || flag_incremental_link == INCREMENTAL_LINK_LTO)
628 : 4117897 : || ((init = ctor_for_folding (vnode->decl)) == error_mark_node))
629 : 1977958 : vnode->remove_initializer ();
630 : : else
631 : 83311 : DECL_INITIAL (vnode->decl) = init;
632 : 2061269 : vnode->body_removed = true;
633 : 2061269 : vnode->definition = false;
634 : 2061269 : vnode->analyzed = false;
635 : 2061269 : vnode->aux = NULL;
636 : :
637 : 2061269 : vnode->remove_from_same_comdat_group ();
638 : :
639 : 2061269 : vnode->remove_all_references ();
640 : : }
641 : : else
642 : 19993597 : vnode->aux = NULL;
643 : : }
644 : :
645 : : /* Now update address_taken flags and try to promote functions to be local. */
646 : 1439165 : if (file)
647 : 673 : fprintf (file, "\nClearing address taken flags:");
648 : 16961382 : FOR_EACH_DEFINED_FUNCTION (node)
649 : 15522217 : if (node->address_taken
650 : 15522217 : && !node->used_from_other_partition)
651 : : {
652 : 3198542 : if (!node->call_for_symbol_and_aliases
653 : 3198542 : (has_addr_references_p, NULL, true))
654 : : {
655 : 21699 : if (file)
656 : 2 : fprintf (file, " %s", node->dump_name ());
657 : 21699 : node->address_taken = false;
658 : 21699 : changed = true;
659 : 21699 : if (node->local_p ()
660 : : /* Virtual functions may be kept in cgraph just because
661 : : of possible later devirtualization. Do not mark them as
662 : : local too early so we won't optimize them out before
663 : : we are done with polymorphic call analysis. */
664 : 21699 : && (symtab->state >= IPA_SSA_AFTER_INLINING
665 : 380 : || !node->call_for_symbol_and_aliases
666 : 380 : (is_indirect_call_target_p, NULL, true)))
667 : : {
668 : 391 : node->local = true;
669 : 391 : if (file)
670 : 2 : fprintf (file, " (local)");
671 : : }
672 : : }
673 : : }
674 : 1439165 : if (file)
675 : 673 : fprintf (file, "\n");
676 : :
677 : 1439165 : symtab_node::checking_verify_symtab_nodes ();
678 : :
679 : : /* If we removed something, perhaps profile could be improved. */
680 : 1439165 : if (changed && (optimize || in_lto_p) && ipa_call_summaries)
681 : 3750364 : FOR_EACH_DEFINED_FUNCTION (node)
682 : 3683822 : ipa_propagate_frequency (node);
683 : :
684 : 1439165 : timevar_pop (TV_IPA_UNREACHABLE);
685 : 1439165 : return changed;
686 : 1439165 : }
687 : :
688 : : /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
689 : : as needed, also clear EXPLICIT_REFS if the references to given variable
690 : : do not need to be explicit. */
691 : :
692 : : void
693 : 5357550 : process_references (varpool_node *vnode,
694 : : bool *written, bool *address_taken,
695 : : bool *read, bool *explicit_refs)
696 : : {
697 : 5357550 : int i;
698 : 5357550 : struct ipa_ref *ref;
699 : :
700 : 5357550 : if (!vnode->all_refs_explicit_p ()
701 : 5357550 : || TREE_THIS_VOLATILE (vnode->decl))
702 : 2914969 : *explicit_refs = false;
703 : :
704 : 3960099 : for (i = 0; vnode->iterate_referring (i, ref)
705 : 9317649 : && *explicit_refs && (!*written || !*address_taken || !*read); i++)
706 : 3960099 : switch (ref->use)
707 : : {
708 : 2765470 : case IPA_REF_ADDR:
709 : 2765470 : *address_taken = true;
710 : 2765470 : break;
711 : 646394 : case IPA_REF_LOAD:
712 : 646394 : *read = true;
713 : 646394 : break;
714 : 536219 : case IPA_REF_STORE:
715 : 536219 : *written = true;
716 : 536219 : break;
717 : 12016 : case IPA_REF_ALIAS:
718 : 12016 : process_references (dyn_cast<varpool_node *> (ref->referring), written,
719 : : address_taken, read, explicit_refs);
720 : 12016 : break;
721 : : }
722 : 5357550 : }
723 : :
724 : : /* Set TREE_READONLY bit. */
725 : :
726 : : bool
727 : 75905 : set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
728 : : {
729 : 75905 : TREE_READONLY (vnode->decl) = true;
730 : 75905 : return false;
731 : : }
732 : :
733 : : /* Set writeonly bit and clear the initalizer, since it will not be needed. */
734 : :
735 : : bool
736 : 25458 : set_writeonly_bit (varpool_node *vnode, void *data)
737 : : {
738 : 25458 : vnode->writeonly = true;
739 : 25458 : if (optimize || in_lto_p)
740 : : {
741 : 25458 : DECL_INITIAL (vnode->decl) = NULL;
742 : 25458 : if (!vnode->alias)
743 : : {
744 : 25458 : if (vnode->num_references ())
745 : 204 : *(bool *)data = true;
746 : 25458 : vnode->remove_all_references ();
747 : : }
748 : : }
749 : 25458 : return false;
750 : : }
751 : :
752 : : /* Clear addressale bit of VNODE. */
753 : :
754 : : bool
755 : 169964 : clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
756 : : {
757 : 169964 : vnode->address_taken = false;
758 : 169964 : TREE_ADDRESSABLE (vnode->decl) = 0;
759 : 169964 : return false;
760 : : }
761 : :
762 : : /* Discover variables that have no longer address taken, are read-only or
763 : : write-only and update their flags.
764 : :
765 : : Return true when unreachable symbol removal should be done.
766 : :
767 : : FIXME: This cannot be done in between gimplify and omp_expand since
768 : : readonly flag plays role on what is shared and what is not. Currently we do
769 : : this transformation as part of whole program visibility and re-do at
770 : : ipa-reference pass (to take into account clonning), but it would
771 : : make sense to do it before early optimizations. */
772 : :
773 : : bool
774 : 297425 : ipa_discover_variable_flags (void)
775 : : {
776 : 297425 : if (!flag_ipa_reference_addressable)
777 : : return false;
778 : :
779 : 290903 : bool remove_p = false;
780 : 290903 : varpool_node *vnode;
781 : 290903 : if (dump_file)
782 : 54 : fprintf (dump_file, "Clearing variable flags:");
783 : 11297354 : FOR_EACH_VARIABLE (vnode)
784 : 5357774 : if (!vnode->alias
785 : 5357774 : && (TREE_ADDRESSABLE (vnode->decl)
786 : 2363577 : || !vnode->writeonly
787 : 25382 : || !TREE_READONLY (vnode->decl)))
788 : : {
789 : 5345534 : bool written = false;
790 : 5345534 : bool address_taken = false;
791 : 5345534 : bool read = false;
792 : 5345534 : bool explicit_refs = true;
793 : :
794 : 5345534 : process_references (vnode, &written, &address_taken, &read,
795 : : &explicit_refs);
796 : 5345534 : if (!explicit_refs)
797 : 2914969 : continue;
798 : 2430565 : if (!address_taken)
799 : : {
800 : 158799 : if (TREE_ADDRESSABLE (vnode->decl) && dump_file)
801 : 0 : fprintf (dump_file, " %s (non-addressable)",
802 : : vnode->dump_name ());
803 : 158799 : vnode->call_for_symbol_and_aliases (clear_addressable_bit, NULL,
804 : : true);
805 : : }
806 : 158799 : if (!address_taken && !written
807 : : /* Making variable in explicit section readonly can cause section
808 : : type conflict.
809 : : See e.g. gcc.c-torture/compile/pr23237.c */
810 : 2495339 : && vnode->get_section () == NULL)
811 : : {
812 : 64744 : if (!TREE_READONLY (vnode->decl) && dump_file)
813 : 3 : fprintf (dump_file, " %s (read-only)", vnode->dump_name ());
814 : 64744 : vnode->call_for_symbol_and_aliases (set_readonly_bit, NULL, true);
815 : : }
816 : 2430565 : if (!vnode->writeonly && !read && !address_taken && written)
817 : : {
818 : 25458 : if (dump_file)
819 : 0 : fprintf (dump_file, " %s (write-only)", vnode->dump_name ());
820 : 25458 : vnode->call_for_symbol_and_aliases (set_writeonly_bit, &remove_p,
821 : : true);
822 : : }
823 : : }
824 : 290903 : if (dump_file)
825 : 54 : fprintf (dump_file, "\n");
826 : 290903 : return remove_p;
827 : : }
828 : :
829 : : /* Generate and emit a static constructor or destructor. WHICH must
830 : : be one of 'I' (for a constructor), 'D' (for a destructor).
831 : : BODY is a STATEMENT_LIST containing GENERIC
832 : : statements. PRIORITY is the initialization priority for this
833 : : constructor or destructor.
834 : :
835 : : FINAL specify whether the externally visible name for collect2 should
836 : : be produced. */
837 : :
838 : : static tree
839 : 4734 : cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final,
840 : : tree optimization,
841 : : tree target)
842 : : {
843 : 4734 : static int counter = 0;
844 : 4734 : char which_buf[16];
845 : 4734 : tree decl, name, resdecl;
846 : :
847 : : /* The priority is encoded in the constructor or destructor name.
848 : : collect2 will sort the names and arrange that they are called at
849 : : program startup. */
850 : 4734 : if (!targetm.have_ctors_dtors && final)
851 : : {
852 : 0 : sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
853 : 0 : name = get_file_function_name (which_buf);
854 : : }
855 : : else
856 : : {
857 : : /* Proudce sane name but one not recognizable by collect2, just for the
858 : : case we fail to inline the function. */
859 : 4734 : sprintf (which_buf, "_sub_%c_%.5d_%d", which, priority, counter++);
860 : 4734 : name = get_identifier (which_buf);
861 : : }
862 : :
863 : 4734 : decl = build_decl (input_location, FUNCTION_DECL, name,
864 : : build_function_type_list (void_type_node, NULL_TREE));
865 : 4734 : current_function_decl = decl;
866 : :
867 : 4734 : resdecl = build_decl (input_location,
868 : : RESULT_DECL, NULL_TREE, void_type_node);
869 : 4734 : DECL_ARTIFICIAL (resdecl) = 1;
870 : 4734 : DECL_RESULT (decl) = resdecl;
871 : 4734 : DECL_CONTEXT (resdecl) = decl;
872 : :
873 : 4734 : allocate_struct_function (decl, false);
874 : :
875 : 4734 : TREE_STATIC (decl) = 1;
876 : 4734 : TREE_USED (decl) = 1;
877 : 4734 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl) = optimization;
878 : 4734 : DECL_FUNCTION_SPECIFIC_TARGET (decl) = target;
879 : 4734 : DECL_ARTIFICIAL (decl) = 1;
880 : 4734 : DECL_IGNORED_P (decl) = 1;
881 : 4734 : DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
882 : 4734 : DECL_SAVED_TREE (decl) = body;
883 : 4734 : if (!targetm.have_ctors_dtors && final)
884 : : {
885 : 0 : TREE_PUBLIC (decl) = 1;
886 : 0 : DECL_PRESERVE_P (decl) = 1;
887 : : }
888 : 4734 : DECL_UNINLINABLE (decl) = 1;
889 : :
890 : 4734 : DECL_INITIAL (decl) = make_node (BLOCK);
891 : 4734 : BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl;
892 : 4734 : TREE_USED (DECL_INITIAL (decl)) = 1;
893 : :
894 : 4734 : DECL_SOURCE_LOCATION (decl) = input_location;
895 : 4734 : cfun->function_end_locus = input_location;
896 : :
897 : 4734 : switch (which)
898 : : {
899 : 3313 : case 'I':
900 : 3313 : DECL_STATIC_CONSTRUCTOR (decl) = 1;
901 : 3313 : decl_init_priority_insert (decl, priority);
902 : 3313 : break;
903 : 1421 : case 'D':
904 : 1421 : DECL_STATIC_DESTRUCTOR (decl) = 1;
905 : 1421 : decl_fini_priority_insert (decl, priority);
906 : 1421 : break;
907 : 0 : default:
908 : 0 : gcc_unreachable ();
909 : : }
910 : :
911 : 4734 : gimplify_function_tree (decl);
912 : :
913 : 4734 : cgraph_node::add_new_function (decl, false);
914 : :
915 : 4734 : set_cfun (NULL);
916 : 4734 : current_function_decl = NULL;
917 : 4734 : return decl;
918 : : }
919 : :
920 : : /* Generate and emit a static constructor or destructor. WHICH must
921 : : be one of 'I' (for a constructor) or 'D' (for a destructor).
922 : : BODY is a STATEMENT_LIST containing GENERIC
923 : : statements. PRIORITY is the initialization priority for this
924 : : constructor or destructor. */
925 : :
926 : : void
927 : 4726 : cgraph_build_static_cdtor (char which, tree body, int priority)
928 : : {
929 : : /* FIXME: We should be able to
930 : : gcc_assert (!in_lto_p);
931 : : because at LTO time the global options are not safe to use.
932 : : Unfortunately ASAN finish_file will produce constructors late and they
933 : : may lead to surprises. */
934 : 4726 : cgraph_build_static_cdtor_1 (which, body, priority, false,
935 : : optimization_default_node,
936 : : target_option_default_node);
937 : 4726 : }
938 : :
939 : : /* When target does not have ctors and dtors, we call all constructor
940 : : and destructor by special initialization/destruction function
941 : : recognized by collect2.
942 : :
943 : : When we are going to build this function, collect all constructors and
944 : : destructors and turn them into normal functions. */
945 : :
946 : : static void
947 : 94 : record_cdtor_fn (struct cgraph_node *node, vec<tree> *ctors, vec<tree> *dtors)
948 : : {
949 : 94 : if (DECL_STATIC_CONSTRUCTOR (node->decl))
950 : 69 : ctors->safe_push (node->decl);
951 : 94 : if (DECL_STATIC_DESTRUCTOR (node->decl))
952 : 29 : dtors->safe_push (node->decl);
953 : 94 : node = cgraph_node::get (node->decl);
954 : 94 : DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
955 : 94 : }
956 : :
957 : : /* Define global constructors/destructor functions for the CDTORS, of
958 : : which they are LEN. The CDTORS are sorted by initialization
959 : : priority. If CTOR_P is true, these are constructors; otherwise,
960 : : they are destructors. */
961 : :
962 : : static void
963 : 66 : build_cdtor (bool ctor_p, const vec<tree> &cdtors)
964 : : {
965 : 66 : size_t i,j;
966 : 66 : size_t len = cdtors.length ();
967 : :
968 : 66 : i = 0;
969 : 156 : while (i < len)
970 : : {
971 : 90 : tree body;
972 : 90 : tree fn;
973 : 90 : priority_type priority;
974 : :
975 : 90 : priority = 0;
976 : 90 : body = NULL_TREE;
977 : 90 : j = i;
978 : 122 : do
979 : : {
980 : 122 : priority_type p;
981 : 122 : fn = cdtors[j];
982 : 122 : p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
983 : 122 : if (j == i)
984 : : priority = p;
985 : 32 : else if (p != priority)
986 : : break;
987 : 98 : j++;
988 : : }
989 : 98 : while (j < len);
990 : :
991 : : /* When there is only one cdtor and target supports them, do nothing. */
992 : 90 : if (j == i + 1
993 : 82 : && targetm.have_ctors_dtors)
994 : : {
995 : 82 : i++;
996 : 82 : continue;
997 : : }
998 : : /* Find the next batch of constructors/destructors with the same
999 : : initialization priority. */
1000 : 24 : for (;i < j; i++)
1001 : : {
1002 : 16 : tree call;
1003 : 16 : fn = cdtors[i];
1004 : 16 : call = build_call_expr (fn, 0);
1005 : 16 : if (ctor_p)
1006 : 8 : DECL_STATIC_CONSTRUCTOR (fn) = 0;
1007 : : else
1008 : 8 : DECL_STATIC_DESTRUCTOR (fn) = 0;
1009 : : /* We do not want to optimize away pure/const calls here.
1010 : : When optimizing, these should be already removed, when not
1011 : : optimizing, we want user to be able to breakpoint in them. */
1012 : 16 : TREE_SIDE_EFFECTS (call) = 1;
1013 : 16 : append_to_statement_list (call, &body);
1014 : : }
1015 : 8 : gcc_assert (body != NULL_TREE);
1016 : : /* Generate a function to call all the function of like
1017 : : priority. */
1018 : 16 : cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true,
1019 : 8 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (cdtors[0]),
1020 : 8 : DECL_FUNCTION_SPECIFIC_TARGET (cdtors[0]));
1021 : : }
1022 : 66 : }
1023 : :
1024 : : /* Helper functions for build_cxa_dtor_registrations ().
1025 : : Build a decl for __cxa_atexit (). */
1026 : :
1027 : : static tree
1028 : 0 : build_cxa_atexit_decl ()
1029 : : {
1030 : : /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
1031 : 0 : tree fn_type = build_function_type_list (void_type_node,
1032 : : ptr_type_node, NULL_TREE);
1033 : 0 : tree fn_ptr_type = build_pointer_type (fn_type);
1034 : : /* The declaration for `__cxa_atexit' is:
1035 : : int __cxa_atexit (void (*)(void *), void *, void *). */
1036 : 0 : const char *name = "__cxa_atexit";
1037 : 0 : tree cxa_name = get_identifier (name);
1038 : 0 : fn_type = build_function_type_list (integer_type_node, fn_ptr_type,
1039 : : ptr_type_node, ptr_type_node, NULL_TREE);
1040 : 0 : tree atexit_fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
1041 : : cxa_name, fn_type);
1042 : 0 : SET_DECL_ASSEMBLER_NAME (atexit_fndecl, cxa_name);
1043 : 0 : DECL_VISIBILITY (atexit_fndecl) = VISIBILITY_DEFAULT;
1044 : 0 : DECL_VISIBILITY_SPECIFIED (atexit_fndecl) = true;
1045 : 0 : set_call_expr_flags (atexit_fndecl, ECF_LEAF | ECF_NOTHROW);
1046 : 0 : TREE_PUBLIC (atexit_fndecl) = true;
1047 : 0 : DECL_EXTERNAL (atexit_fndecl) = true;
1048 : 0 : DECL_ARTIFICIAL (atexit_fndecl) = true;
1049 : 0 : return atexit_fndecl;
1050 : : }
1051 : :
1052 : : /* Build a decl for __dso_handle. */
1053 : :
1054 : : static tree
1055 : 0 : build_dso_handle_decl ()
1056 : : {
1057 : : /* Declare the __dso_handle variable. */
1058 : 0 : tree dso_handle_decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
1059 : : get_identifier ("__dso_handle"),
1060 : : ptr_type_node);
1061 : 0 : TREE_PUBLIC (dso_handle_decl) = true;
1062 : 0 : DECL_EXTERNAL (dso_handle_decl) = true;
1063 : 0 : DECL_ARTIFICIAL (dso_handle_decl) = true;
1064 : : #ifdef HAVE_GAS_HIDDEN
1065 : 0 : if (dso_handle_decl != error_mark_node)
1066 : : {
1067 : 0 : DECL_VISIBILITY (dso_handle_decl) = VISIBILITY_HIDDEN;
1068 : 0 : DECL_VISIBILITY_SPECIFIED (dso_handle_decl) = true;
1069 : : }
1070 : : #endif
1071 : 0 : return dso_handle_decl;
1072 : : }
1073 : :
1074 : : /* This builds one or more constructor functions that register DTORs with
1075 : : __cxa_atexit (). Within a priority level, DTORs are registered in TU
1076 : : order - which means that they will run in reverse TU order from cxa_atexit.
1077 : : This is the same behavior as using a .fini / .mod_term_funcs section.
1078 : : As the functions are built, they are appended to the CTORs vector. */
1079 : :
1080 : : static void
1081 : 0 : build_cxa_dtor_registrations (const vec<tree> &dtors, vec<tree> *ctors)
1082 : : {
1083 : 0 : size_t i,j;
1084 : 0 : size_t len = dtors.length ();
1085 : :
1086 : 0 : location_t sav_loc = input_location;
1087 : 0 : input_location = UNKNOWN_LOCATION;
1088 : :
1089 : 0 : tree atexit_fndecl = build_cxa_atexit_decl ();
1090 : 0 : tree dso_handle_decl = build_dso_handle_decl ();
1091 : :
1092 : : /* We want &__dso_handle. */
1093 : 0 : tree dso_ptr = build1_loc (UNKNOWN_LOCATION, ADDR_EXPR,
1094 : : ptr_type_node, dso_handle_decl);
1095 : :
1096 : 0 : i = 0;
1097 : 0 : while (i < len)
1098 : : {
1099 : 0 : priority_type priority = 0;
1100 : 0 : tree body = NULL_TREE;
1101 : 0 : j = i;
1102 : 0 : do
1103 : : {
1104 : 0 : priority_type p;
1105 : 0 : tree fn = dtors[j];
1106 : 0 : p = DECL_FINI_PRIORITY (fn);
1107 : 0 : if (j == i)
1108 : : priority = p;
1109 : 0 : else if (p != priority)
1110 : : break;
1111 : 0 : j++;
1112 : : }
1113 : 0 : while (j < len);
1114 : :
1115 : : /* Find the next batch of destructors with the same initialization
1116 : : priority. */
1117 : 0 : for (;i < j; i++)
1118 : : {
1119 : 0 : tree fn = dtors[i];
1120 : 0 : DECL_STATIC_DESTRUCTOR (fn) = 0;
1121 : 0 : tree dtor_ptr = build1_loc (UNKNOWN_LOCATION, ADDR_EXPR,
1122 : : ptr_type_node, fn);
1123 : 0 : tree call_cxa_atexit
1124 : 0 : = build_call_expr_loc (UNKNOWN_LOCATION, atexit_fndecl, 3,
1125 : : dtor_ptr, null_pointer_node, dso_ptr);
1126 : 0 : TREE_SIDE_EFFECTS (call_cxa_atexit) = 1;
1127 : 0 : append_to_statement_list (call_cxa_atexit, &body);
1128 : : }
1129 : :
1130 : 0 : gcc_assert (body != NULL_TREE);
1131 : : /* Generate a function to register the DTORs at this priority. */
1132 : 0 : tree new_ctor
1133 : 0 : = cgraph_build_static_cdtor_1 ('I', body, priority, true,
1134 : 0 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (dtors[0]),
1135 : 0 : DECL_FUNCTION_SPECIFIC_TARGET (dtors[0]));
1136 : : /* Add this to the list of ctors. */
1137 : 0 : ctors->safe_push (new_ctor);
1138 : : }
1139 : 0 : input_location = sav_loc;
1140 : 0 : }
1141 : :
1142 : : /* Comparison function for qsort. P1 and P2 are actually of type
1143 : : "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1144 : : used to determine the sort order. */
1145 : :
1146 : : static int
1147 : 96 : compare_ctor (const void *p1, const void *p2)
1148 : : {
1149 : 96 : tree f1;
1150 : 96 : tree f2;
1151 : 96 : int priority1;
1152 : 96 : int priority2;
1153 : :
1154 : 96 : f1 = *(const tree *)p1;
1155 : 96 : f2 = *(const tree *)p2;
1156 : 96 : priority1 = DECL_INIT_PRIORITY (f1);
1157 : 96 : priority2 = DECL_INIT_PRIORITY (f2);
1158 : :
1159 : 96 : if (priority1 < priority2)
1160 : : return -1;
1161 : 40 : else if (priority1 > priority2)
1162 : : return 1;
1163 : : else
1164 : : /* Ensure a stable sort. Constructors are executed in backwarding
1165 : : order to make LTO initialize braries first. */
1166 : 16 : return DECL_UID (f2) - DECL_UID (f1);
1167 : : }
1168 : :
1169 : : /* Comparison function for qsort. P1 and P2 are actually of type
1170 : : "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1171 : : used to determine the sort order. */
1172 : :
1173 : : static int
1174 : 96 : compare_dtor (const void *p1, const void *p2)
1175 : : {
1176 : 96 : tree f1;
1177 : 96 : tree f2;
1178 : 96 : int priority1;
1179 : 96 : int priority2;
1180 : :
1181 : 96 : f1 = *(const tree *)p1;
1182 : 96 : f2 = *(const tree *)p2;
1183 : 96 : priority1 = DECL_FINI_PRIORITY (f1);
1184 : 96 : priority2 = DECL_FINI_PRIORITY (f2);
1185 : :
1186 : 96 : if (priority1 < priority2)
1187 : : return -1;
1188 : 40 : else if (priority1 > priority2)
1189 : : return 1;
1190 : : else
1191 : : /* Ensure a stable sort - into TU order. */
1192 : 16 : return DECL_UID (f1) - DECL_UID (f2);
1193 : : }
1194 : :
1195 : : /* Comparison function for qsort. P1 and P2 are of type "tree *" and point to
1196 : : a pair of static constructors or destructors. We first sort on the basis of
1197 : : priority and then into TU order (on the strict assumption that DECL_UIDs are
1198 : : ordered in the same way as the original functions). ???: this seems quite
1199 : : fragile. */
1200 : :
1201 : : static int
1202 : 0 : compare_cdtor_tu_order (const void *p1, const void *p2)
1203 : : {
1204 : 0 : tree f1;
1205 : 0 : tree f2;
1206 : 0 : int priority1;
1207 : 0 : int priority2;
1208 : :
1209 : 0 : f1 = *(const tree *)p1;
1210 : 0 : f2 = *(const tree *)p2;
1211 : : /* We process the DTORs first, and then remove their flag, so this order
1212 : : allows for functions that are declared as both CTOR and DTOR. */
1213 : 0 : if (DECL_STATIC_DESTRUCTOR (f1))
1214 : : {
1215 : 0 : gcc_checking_assert (DECL_STATIC_DESTRUCTOR (f2));
1216 : 0 : priority1 = DECL_FINI_PRIORITY (f1);
1217 : 0 : priority2 = DECL_FINI_PRIORITY (f2);
1218 : : }
1219 : : else
1220 : : {
1221 : 0 : priority1 = DECL_INIT_PRIORITY (f1);
1222 : 0 : priority2 = DECL_INIT_PRIORITY (f2);
1223 : : }
1224 : :
1225 : 0 : if (priority1 < priority2)
1226 : : return -1;
1227 : 0 : else if (priority1 > priority2)
1228 : : return 1;
1229 : : else
1230 : : /* For equal priority, sort into the order of definition in the TU. */
1231 : 0 : return DECL_UID (f1) - DECL_UID (f2);
1232 : : }
1233 : :
1234 : : /* Generate functions to call static constructors and destructors
1235 : : for targets that do not support .ctors/.dtors sections. These
1236 : : functions have magic names which are detected by collect2. */
1237 : :
1238 : : static void
1239 : 12950 : build_cdtor_fns (vec<tree> *ctors, vec<tree> *dtors)
1240 : : {
1241 : 12950 : if (!ctors->is_empty ())
1242 : : {
1243 : 53 : gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1244 : 53 : ctors->qsort (compare_ctor);
1245 : 53 : build_cdtor (/*ctor_p=*/true, *ctors);
1246 : : }
1247 : :
1248 : 12950 : if (!dtors->is_empty ())
1249 : : {
1250 : 13 : gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1251 : 13 : dtors->qsort (compare_dtor);
1252 : 13 : build_cdtor (/*ctor_p=*/false, *dtors);
1253 : : }
1254 : 12950 : }
1255 : :
1256 : : /* Generate new CTORs to register static destructors with __cxa_atexit and add
1257 : : them to the existing list of CTORs; we then process the revised CTORs list.
1258 : :
1259 : : We sort the DTORs into priority and then TU order, this means that they are
1260 : : registered in that order with __cxa_atexit () and therefore will be run in
1261 : : the reverse order.
1262 : :
1263 : : Likewise, CTORs are sorted into priority and then TU order, which means that
1264 : : they will run in that order.
1265 : :
1266 : : This matches the behavior of using init/fini or mod_init_func/mod_term_func
1267 : : sections. */
1268 : :
1269 : : static void
1270 : 0 : build_cxa_atexit_fns (vec<tree> *ctors, vec<tree> *dtors)
1271 : : {
1272 : 0 : if (!dtors->is_empty ())
1273 : : {
1274 : 0 : gcc_assert (targetm.dtors_from_cxa_atexit);
1275 : 0 : dtors->qsort (compare_cdtor_tu_order);
1276 : 0 : build_cxa_dtor_registrations (*dtors, ctors);
1277 : : }
1278 : :
1279 : 0 : if (!ctors->is_empty ())
1280 : : {
1281 : 0 : gcc_assert (targetm.dtors_from_cxa_atexit);
1282 : 0 : ctors->qsort (compare_cdtor_tu_order);
1283 : 0 : build_cdtor (/*ctor_p=*/true, *ctors);
1284 : : }
1285 : 0 : }
1286 : :
1287 : : /* Look for constructors and destructors and produce function calling them.
1288 : : This is needed for targets not supporting ctors or dtors, but we perform the
1289 : : transformation also at linktime to merge possibly numerous
1290 : : constructors/destructors into single function to improve code locality and
1291 : : reduce size. */
1292 : :
1293 : : static unsigned int
1294 : 12950 : ipa_cdtor_merge (void)
1295 : : {
1296 : : /* A vector of FUNCTION_DECLs declared as static constructors. */
1297 : 12950 : auto_vec<tree, 20> ctors;
1298 : : /* A vector of FUNCTION_DECLs declared as static destructors. */
1299 : 12950 : auto_vec<tree, 20> dtors;
1300 : 12950 : struct cgraph_node *node;
1301 : 90998 : FOR_EACH_DEFINED_FUNCTION (node)
1302 : 78048 : if (DECL_STATIC_CONSTRUCTOR (node->decl)
1303 : 78048 : || DECL_STATIC_DESTRUCTOR (node->decl))
1304 : 94 : record_cdtor_fn (node, &ctors, &dtors);
1305 : 12950 : if (targetm.dtors_from_cxa_atexit)
1306 : 0 : build_cxa_atexit_fns (&ctors, &dtors);
1307 : : else
1308 : 12950 : build_cdtor_fns (&ctors, &dtors);
1309 : 12950 : return 0;
1310 : 12950 : }
1311 : :
1312 : : namespace {
1313 : :
1314 : : const pass_data pass_data_ipa_cdtor_merge =
1315 : : {
1316 : : IPA_PASS, /* type */
1317 : : "cdtor", /* name */
1318 : : OPTGROUP_NONE, /* optinfo_flags */
1319 : : TV_CGRAPHOPT, /* tv_id */
1320 : : 0, /* properties_required */
1321 : : 0, /* properties_provided */
1322 : : 0, /* properties_destroyed */
1323 : : 0, /* todo_flags_start */
1324 : : 0, /* todo_flags_finish */
1325 : : };
1326 : :
1327 : : class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1328 : : {
1329 : : public:
1330 : 282866 : pass_ipa_cdtor_merge (gcc::context *ctxt)
1331 : : : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1332 : : NULL, /* generate_summary */
1333 : : NULL, /* write_summary */
1334 : : NULL, /* read_summary */
1335 : : NULL, /* write_optimization_summary */
1336 : : NULL, /* read_optimization_summary */
1337 : : NULL, /* stmt_fixup */
1338 : : 0, /* function_transform_todo_flags_start */
1339 : : NULL, /* function_transform */
1340 : 282866 : NULL) /* variable_transform */
1341 : 282866 : {}
1342 : :
1343 : : /* opt_pass methods: */
1344 : : bool gate (function *) final override;
1345 : 12950 : unsigned int execute (function *) final override
1346 : : {
1347 : 12950 : return ipa_cdtor_merge ();
1348 : : }
1349 : :
1350 : : }; // class pass_ipa_cdtor_merge
1351 : :
1352 : : bool
1353 : 559002 : pass_ipa_cdtor_merge::gate (function *)
1354 : : {
1355 : : /* Perform the pass when we have no ctors/dtors support
1356 : : or at LTO time to merge multiple constructors into single
1357 : : function. */
1358 : 559002 : return !targetm.have_ctors_dtors || in_lto_p || targetm.dtors_from_cxa_atexit;
1359 : : }
1360 : :
1361 : : } // anon namespace
1362 : :
1363 : : ipa_opt_pass_d *
1364 : 282866 : make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1365 : : {
1366 : 282866 : return new pass_ipa_cdtor_merge (ctxt);
1367 : : }
1368 : :
1369 : : /* Invalid pointer representing BOTTOM for single user dataflow. */
1370 : : #define BOTTOM ((cgraph_node *)(size_t) 2)
1371 : :
1372 : : /* Meet operation for single user dataflow.
1373 : : Here we want to associate variables with sigle function that may access it.
1374 : :
1375 : : FUNCTION is current single user of a variable, VAR is variable that uses it.
1376 : : Latttice is stored in SINGLE_USER_MAP.
1377 : :
1378 : : We represent:
1379 : : - TOP by no entry in SIGNLE_USER_MAP
1380 : : - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1381 : : - known single user by cgraph pointer in SINGLE_USER_MAP. */
1382 : :
1383 : : cgraph_node *
1384 : 3436200 : meet (cgraph_node *function, varpool_node *var,
1385 : : hash_map<varpool_node *, cgraph_node *> &single_user_map)
1386 : : {
1387 : 3436200 : struct cgraph_node *user, **f;
1388 : :
1389 : 3436200 : if (var->aux == BOTTOM)
1390 : : return BOTTOM;
1391 : :
1392 : 2454855 : f = single_user_map.get (var);
1393 : 2454855 : if (!f)
1394 : : return function;
1395 : 1002489 : user = *f;
1396 : 1002489 : if (!function)
1397 : : return user;
1398 : 980729 : else if (function != user)
1399 : : return BOTTOM;
1400 : : else
1401 : : return function;
1402 : : }
1403 : :
1404 : : /* Propagation step of single-use dataflow.
1405 : :
1406 : : Check all uses of VNODE and see if they are used by single function FUNCTION.
1407 : : SINGLE_USER_MAP represents the dataflow lattice. */
1408 : :
1409 : : cgraph_node *
1410 : 1774808 : propagate_single_user (varpool_node *vnode, cgraph_node *function,
1411 : : hash_map<varpool_node *, cgraph_node *> &single_user_map)
1412 : : {
1413 : 1774808 : int i;
1414 : 1774808 : struct ipa_ref *ref;
1415 : :
1416 : 1774808 : gcc_assert (!vnode->externally_visible);
1417 : :
1418 : : /* If node is an alias, first meet with its target. */
1419 : 1774808 : if (vnode->alias)
1420 : 16087 : function = meet (function, vnode->get_alias_target (), single_user_map);
1421 : :
1422 : : /* Check all users and see if they correspond to a single function. */
1423 : 5768234 : for (i = 0; vnode->iterate_referring (i, ref) && function != BOTTOM; i++)
1424 : : {
1425 : 7986852 : struct cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
1426 : 3993426 : if (cnode)
1427 : : {
1428 : 573313 : if (cnode->inlined_to)
1429 : 82419 : cnode = cnode->inlined_to;
1430 : 573313 : if (!function)
1431 : : function = cnode;
1432 : 279608 : else if (function != cnode)
1433 : 23193 : function = BOTTOM;
1434 : : }
1435 : : else
1436 : 6840226 : function = meet (function, dyn_cast <varpool_node *> (ref->referring),
1437 : : single_user_map);
1438 : : }
1439 : 1774808 : return function;
1440 : : }
1441 : :
1442 : : /* Pass setting used_by_single_function flag.
1443 : : This flag is set on variable when there is only one function that may
1444 : : possibly referr to it. */
1445 : :
1446 : : static unsigned int
1447 : 226688 : ipa_single_use (void)
1448 : : {
1449 : 226688 : varpool_node *first = (varpool_node *) (void *) 1;
1450 : 226688 : varpool_node *var;
1451 : 226688 : hash_map<varpool_node *, cgraph_node *> single_user_map;
1452 : :
1453 : 3067797 : FOR_EACH_DEFINED_VARIABLE (var)
1454 : 2841109 : if (!var->all_refs_explicit_p ())
1455 : 1548632 : var->aux = BOTTOM;
1456 : : else
1457 : : {
1458 : : /* Enqueue symbol for dataflow. */
1459 : 1292477 : var->aux = first;
1460 : 1292477 : first = var;
1461 : : }
1462 : :
1463 : : /* The actual dataflow. */
1464 : :
1465 : 2001496 : while (first != (void *) 1)
1466 : : {
1467 : 1774808 : cgraph_node *user, *orig_user, **f;
1468 : :
1469 : 1774808 : var = first;
1470 : 1774808 : first = (varpool_node *)first->aux;
1471 : :
1472 : 1774808 : f = single_user_map.get (var);
1473 : 1774808 : if (f)
1474 : 29008 : orig_user = *f;
1475 : : else
1476 : : orig_user = NULL;
1477 : 1774808 : user = propagate_single_user (var, orig_user, single_user_map);
1478 : :
1479 : 1774808 : gcc_checking_assert (var->aux != BOTTOM);
1480 : :
1481 : : /* If user differs, enqueue all references. */
1482 : 1774808 : if (user != orig_user)
1483 : : {
1484 : 1298433 : unsigned int i;
1485 : 1298433 : ipa_ref *ref;
1486 : :
1487 : 1298433 : single_user_map.put (var, user);
1488 : :
1489 : : /* Enqueue all aliases for re-processing. */
1490 : 2610845 : for (i = 0; var->iterate_direct_aliases (i, ref); i++)
1491 : 13979 : if (!ref->referring->aux)
1492 : : {
1493 : 4069 : ref->referring->aux = first;
1494 : 13979 : first = dyn_cast <varpool_node *> (ref->referring);
1495 : : }
1496 : : /* Enqueue all users for re-processing. */
1497 : 5195622 : for (i = 0; var->iterate_reference (i, ref); i++)
1498 : 1299378 : if (!ref->referred->aux
1499 : 731321 : && ref->referred->definition
1500 : 2508961 : && is_a <varpool_node *> (ref->referred))
1501 : : {
1502 : 478262 : ref->referred->aux = first;
1503 : 478262 : first = dyn_cast <varpool_node *> (ref->referred);
1504 : : }
1505 : :
1506 : : /* If user is BOTTOM, just punt on this var. */
1507 : 1298433 : if (user == BOTTOM)
1508 : 1008141 : var->aux = BOTTOM;
1509 : : else
1510 : 290292 : var->aux = NULL;
1511 : : }
1512 : : else
1513 : 476375 : var->aux = NULL;
1514 : : }
1515 : :
1516 : 3067797 : FOR_EACH_DEFINED_VARIABLE (var)
1517 : : {
1518 : 2841109 : if (var->aux != BOTTOM)
1519 : : {
1520 : : /* Not having the single user known means that the VAR is
1521 : : unreachable. Either someone forgot to remove unreachable
1522 : : variables or the reachability here is wrong. */
1523 : :
1524 : 284336 : gcc_checking_assert (single_user_map.get (var));
1525 : :
1526 : 284336 : if (dump_file)
1527 : : {
1528 : 10 : fprintf (dump_file, "Variable %s is used by single function\n",
1529 : : var->dump_name ());
1530 : : }
1531 : 284336 : var->used_by_single_function = true;
1532 : : }
1533 : 2841109 : var->aux = NULL;
1534 : : }
1535 : 226688 : return 0;
1536 : 226688 : }
1537 : :
1538 : : namespace {
1539 : :
1540 : : const pass_data pass_data_ipa_single_use =
1541 : : {
1542 : : IPA_PASS, /* type */
1543 : : "single-use", /* name */
1544 : : OPTGROUP_NONE, /* optinfo_flags */
1545 : : TV_CGRAPHOPT, /* tv_id */
1546 : : 0, /* properties_required */
1547 : : 0, /* properties_provided */
1548 : : 0, /* properties_destroyed */
1549 : : 0, /* todo_flags_start */
1550 : : 0, /* todo_flags_finish */
1551 : : };
1552 : :
1553 : : class pass_ipa_single_use : public ipa_opt_pass_d
1554 : : {
1555 : : public:
1556 : 282866 : pass_ipa_single_use (gcc::context *ctxt)
1557 : : : ipa_opt_pass_d (pass_data_ipa_single_use, ctxt,
1558 : : NULL, /* generate_summary */
1559 : : NULL, /* write_summary */
1560 : : NULL, /* read_summary */
1561 : : NULL, /* write_optimization_summary */
1562 : : NULL, /* read_optimization_summary */
1563 : : NULL, /* stmt_fixup */
1564 : : 0, /* function_transform_todo_flags_start */
1565 : : NULL, /* function_transform */
1566 : 282866 : NULL) /* variable_transform */
1567 : 282866 : {}
1568 : :
1569 : : /* opt_pass methods: */
1570 : 226688 : unsigned int execute (function *) final override { return ipa_single_use (); }
1571 : :
1572 : : }; // class pass_ipa_single_use
1573 : :
1574 : : } // anon namespace
1575 : :
1576 : : ipa_opt_pass_d *
1577 : 282866 : make_pass_ipa_single_use (gcc::context *ctxt)
1578 : : {
1579 : 282866 : return new pass_ipa_single_use (ctxt);
1580 : : }
1581 : :
|