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