]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ipa.c
Update copyright years.
[thirdparty/gcc.git] / gcc / ipa.c
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
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
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
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
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
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
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
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 = edge->make_direct (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 edge->redirect_call_stmt_to_callee ();
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
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 symbol in a comdat group is reachable, force
395 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 {
400 symtab_node *next;
401 for (next = node->same_comdat_group;
402 next != node;
403 next = next->same_comdat_group)
404 if (!next->comdat_local_p ()
405 && !reachable.add (next))
406 enqueue_node (next, &first, &reachable);
407 }
408 /* Mark references as reachable. */
409 process_references (node, &first, &reachable);
410 }
411
412 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
413 {
414 /* Mark the callees reachable unless they are direct calls to extern
415 inline functions we decided to not inline. */
416 if (!in_boundary_p)
417 {
418 struct cgraph_edge *e;
419 /* Keep alive possible targets for devirtualization. */
420 if (opt_for_fn (cnode->decl, optimize)
421 && opt_for_fn (cnode->decl, flag_devirtualize))
422 {
423 struct cgraph_edge *next;
424 for (e = cnode->indirect_calls; e; e = next)
425 {
426 next = e->next_callee;
427 if (e->indirect_info->polymorphic)
428 walk_polymorphic_call_targets (&reachable_call_targets,
429 e, &first, &reachable);
430 }
431 }
432 for (e = cnode->callees; e; e = e->next_callee)
433 {
434 symtab_node *body = e->callee->function_symbol ();
435 if (e->callee->definition
436 && !e->callee->in_other_partition
437 && (!e->inline_failed
438 || !DECL_EXTERNAL (e->callee->decl)
439 || e->callee->alias
440 || possible_inline_candidate_p (e->callee)))
441 {
442 /* Be sure that we will not optimize out alias target
443 body. */
444 if (DECL_EXTERNAL (e->callee->decl)
445 && e->callee->alias
446 && symtab->state < IPA_SSA_AFTER_INLINING)
447 reachable.add (body);
448 reachable.add (e->callee);
449 }
450 enqueue_node (e->callee, &first, &reachable);
451 }
452
453 /* When inline clone exists, mark body to be preserved so when removing
454 offline copy of the function we don't kill it. */
455 if (cnode->inlined_to)
456 body_needed_for_clonning.add (cnode->decl);
457
458 /* For non-inline clones, force their origins to the boundary and ensure
459 that body is not removed. */
460 while (cnode->clone_of)
461 {
462 bool noninline = cnode->clone_of->decl != cnode->decl;
463 cnode = cnode->clone_of;
464 if (noninline)
465 {
466 body_needed_for_clonning.add (cnode->decl);
467 enqueue_node (cnode, &first, &reachable);
468 }
469 }
470
471 }
472 else if (cnode->thunk.thunk_p)
473 enqueue_node (cnode->callees->callee, &first, &reachable);
474
475 /* If any reachable function has simd clones, mark them as
476 reachable as well. */
477 if (cnode->simd_clones)
478 {
479 cgraph_node *next;
480 for (next = cnode->simd_clones;
481 next;
482 next = next->simdclone->next_clone)
483 if (in_boundary_p
484 || !reachable.add (next))
485 enqueue_node (next, &first, &reachable);
486 }
487 }
488 /* When we see constructor of external variable, keep referred nodes in the
489 boundary. This will also hold initializers of the external vars NODE
490 refers to. */
491 varpool_node *vnode = dyn_cast <varpool_node *> (node);
492 if (vnode
493 && DECL_EXTERNAL (node->decl)
494 && !vnode->alias
495 && in_boundary_p)
496 {
497 struct ipa_ref *ref = NULL;
498 for (int i = 0; node->iterate_reference (i, ref); i++)
499 enqueue_node (ref->referred, &first, &reachable);
500 }
501 }
502
503 /* Remove unreachable functions. */
504 for (node = first_function (); node; node = next)
505 {
506 next = next_function (node);
507
508 /* If node is not needed at all, remove it. */
509 if (!node->aux)
510 {
511 if (file)
512 fprintf (file, " %s", node->dump_name ());
513 node->remove ();
514 changed = true;
515 }
516 /* If node is unreachable, remove its body. */
517 else if (!reachable.contains (node))
518 {
519 /* We keep definitions of thunks and aliases in the boundary so
520 we can walk to the ultimate alias targets and function symbols
521 reliably. */
522 if (node->alias || node->thunk.thunk_p)
523 ;
524 else if (!body_needed_for_clonning.contains (node->decl))
525 {
526 /* Make the node a non-clone so that we do not attempt to
527 materialize it later. */
528 if (node->clone_of)
529 node->remove_from_clone_tree ();
530 node->release_body ();
531 }
532 else if (!node->clone_of)
533 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
534 if (node->definition && !node->alias && !node->thunk.thunk_p)
535 {
536 if (file)
537 fprintf (file, " %s", node->dump_name ());
538 node->body_removed = true;
539 node->analyzed = false;
540 node->definition = false;
541 node->cpp_implicit_alias = false;
542 node->alias = false;
543 node->transparent_alias = false;
544 node->thunk.thunk_p = false;
545 node->weakref = false;
546 /* After early inlining we drop always_inline attributes on
547 bodies of functions that are still referenced (have their
548 address taken). */
549 DECL_ATTRIBUTES (node->decl)
550 = remove_attribute ("always_inline",
551 DECL_ATTRIBUTES (node->decl));
552 if (!node->in_other_partition)
553 node->local = false;
554 node->remove_callees ();
555 node->remove_all_references ();
556 changed = true;
557 }
558 }
559 else
560 gcc_assert (node->clone_of || !node->has_gimple_body_p ()
561 || in_lto_p || DECL_RESULT (node->decl));
562 }
563
564 /* Inline clones might be kept around so their materializing allows further
565 cloning. If the function the clone is inlined into is removed, we need
566 to turn it into normal cone. */
567 FOR_EACH_FUNCTION (node)
568 {
569 if (node->inlined_to
570 && !node->callers)
571 {
572 gcc_assert (node->clones);
573 node->inlined_to = NULL;
574 update_inlined_to_pointer (node, node);
575 }
576 node->aux = NULL;
577 }
578
579 /* Remove unreachable variables. */
580 if (file)
581 fprintf (file, "\nReclaiming variables:");
582 for (vnode = first_variable (); vnode; vnode = vnext)
583 {
584 vnext = next_variable (vnode);
585 if (!vnode->aux
586 /* For can_refer_decl_in_current_unit_p we want to track for
587 all external variables if they are defined in other partition
588 or not. */
589 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
590 {
591 struct ipa_ref *ref = NULL;
592
593 /* First remove the aliases, so varpool::remove can possibly lookup
594 the constructor and save it for future use. */
595 while (vnode->iterate_direct_aliases (0, ref))
596 {
597 if (file)
598 fprintf (file, " %s", ref->referred->dump_name ());
599 ref->referring->remove ();
600 }
601 if (file)
602 fprintf (file, " %s", vnode->dump_name ());
603 vnext = next_variable (vnode);
604 /* Signal removal to the debug machinery. */
605 if (! flag_wpa || flag_incremental_link == INCREMENTAL_LINK_LTO)
606 {
607 vnode->definition = false;
608 (*debug_hooks->late_global_decl) (vnode->decl);
609 }
610 vnode->remove ();
611 changed = true;
612 }
613 else if (!reachable.contains (vnode) && !vnode->alias)
614 {
615 tree init;
616 if (vnode->definition)
617 {
618 if (file)
619 fprintf (file, " %s", vnode->name ());
620 changed = true;
621 }
622 /* Keep body if it may be useful for constant folding. */
623 if ((flag_wpa || flag_incremental_link == INCREMENTAL_LINK_LTO)
624 || ((init = ctor_for_folding (vnode->decl)) == error_mark_node))
625 vnode->remove_initializer ();
626 else
627 DECL_INITIAL (vnode->decl) = init;
628 vnode->body_removed = true;
629 vnode->definition = false;
630 vnode->analyzed = false;
631 vnode->aux = NULL;
632
633 vnode->remove_from_same_comdat_group ();
634
635 vnode->remove_all_references ();
636 }
637 else
638 vnode->aux = NULL;
639 }
640
641 /* Now update address_taken flags and try to promote functions to be local. */
642 if (file)
643 fprintf (file, "\nClearing address taken flags:");
644 FOR_EACH_DEFINED_FUNCTION (node)
645 if (node->address_taken
646 && !node->used_from_other_partition)
647 {
648 if (!node->call_for_symbol_and_aliases
649 (has_addr_references_p, NULL, true))
650 {
651 if (file)
652 fprintf (file, " %s", node->name ());
653 node->address_taken = false;
654 changed = true;
655 if (node->local_p ()
656 /* Virtual functions may be kept in cgraph just because
657 of possible later devirtualization. Do not mark them as
658 local too early so we won't optimize them out before
659 we are done with polymorphic call analysis. */
660 && (symtab->state >= IPA_SSA_AFTER_INLINING
661 || !node->call_for_symbol_and_aliases
662 (is_indirect_call_target_p, NULL, true)))
663 {
664 node->local = true;
665 if (file)
666 fprintf (file, " (local)");
667 }
668 }
669 }
670 if (file)
671 fprintf (file, "\n");
672
673 symtab_node::checking_verify_symtab_nodes ();
674
675 /* If we removed something, perhaps profile could be improved. */
676 if (changed && (optimize || in_lto_p) && ipa_call_summaries)
677 FOR_EACH_DEFINED_FUNCTION (node)
678 ipa_propagate_frequency (node);
679
680 timevar_pop (TV_IPA_UNREACHABLE);
681 return changed;
682 }
683
684 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
685 as needed, also clear EXPLICIT_REFS if the references to given variable
686 do not need to be explicit. */
687
688 void
689 process_references (varpool_node *vnode,
690 bool *written, bool *address_taken,
691 bool *read, bool *explicit_refs)
692 {
693 int i;
694 struct ipa_ref *ref;
695
696 if (!vnode->all_refs_explicit_p ()
697 || TREE_THIS_VOLATILE (vnode->decl))
698 *explicit_refs = false;
699
700 for (i = 0; vnode->iterate_referring (i, ref)
701 && *explicit_refs && (!*written || !*address_taken || !*read); i++)
702 switch (ref->use)
703 {
704 case IPA_REF_ADDR:
705 *address_taken = true;
706 break;
707 case IPA_REF_LOAD:
708 *read = true;
709 break;
710 case IPA_REF_STORE:
711 *written = true;
712 break;
713 case IPA_REF_ALIAS:
714 process_references (dyn_cast<varpool_node *> (ref->referring), written,
715 address_taken, read, explicit_refs);
716 break;
717 }
718 }
719
720 /* Set TREE_READONLY bit. */
721
722 bool
723 set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
724 {
725 TREE_READONLY (vnode->decl) = true;
726 return false;
727 }
728
729 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
730
731 bool
732 set_writeonly_bit (varpool_node *vnode, void *data)
733 {
734 vnode->writeonly = true;
735 if (optimize || in_lto_p)
736 {
737 DECL_INITIAL (vnode->decl) = NULL;
738 if (!vnode->alias)
739 {
740 if (vnode->num_references ())
741 *(bool *)data = true;
742 vnode->remove_all_references ();
743 }
744 }
745 return false;
746 }
747
748 /* Clear addressale bit of VNODE. */
749
750 bool
751 clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
752 {
753 vnode->address_taken = false;
754 TREE_ADDRESSABLE (vnode->decl) = 0;
755 return false;
756 }
757
758 /* Discover variables that have no longer address taken, are read-only or
759 write-only and update their flags.
760
761 Return true when unreachable symbol removal should be done.
762
763 FIXME: This cannot be done in between gimplify and omp_expand since
764 readonly flag plays role on what is shared and what is not. Currently we do
765 this transformation as part of whole program visibility and re-do at
766 ipa-reference pass (to take into account clonning), but it would
767 make sense to do it before early optimizations. */
768
769 bool
770 ipa_discover_variable_flags (void)
771 {
772 if (!flag_ipa_reference_addressable)
773 return false;
774
775 bool remove_p = false;
776 varpool_node *vnode;
777 if (dump_file)
778 fprintf (dump_file, "Clearing variable flags:");
779 FOR_EACH_VARIABLE (vnode)
780 if (!vnode->alias
781 && (TREE_ADDRESSABLE (vnode->decl)
782 || !vnode->writeonly
783 || !TREE_READONLY (vnode->decl)))
784 {
785 bool written = false;
786 bool address_taken = false;
787 bool read = false;
788 bool explicit_refs = true;
789
790 process_references (vnode, &written, &address_taken, &read,
791 &explicit_refs);
792 if (!explicit_refs)
793 continue;
794 if (!address_taken)
795 {
796 if (TREE_ADDRESSABLE (vnode->decl) && dump_file)
797 fprintf (dump_file, " %s (non-addressable)", vnode->name ());
798 vnode->call_for_symbol_and_aliases (clear_addressable_bit, NULL,
799 true);
800 }
801 if (!address_taken && !written
802 /* Making variable in explicit section readonly can cause section
803 type conflict.
804 See e.g. gcc.c-torture/compile/pr23237.c */
805 && vnode->get_section () == NULL)
806 {
807 if (!TREE_READONLY (vnode->decl) && dump_file)
808 fprintf (dump_file, " %s (read-only)", vnode->name ());
809 vnode->call_for_symbol_and_aliases (set_readonly_bit, NULL, true);
810 }
811 if (!vnode->writeonly && !read && !address_taken && written)
812 {
813 if (dump_file)
814 fprintf (dump_file, " %s (write-only)", vnode->name ());
815 vnode->call_for_symbol_and_aliases (set_writeonly_bit, &remove_p,
816 true);
817 }
818 }
819 if (dump_file)
820 fprintf (dump_file, "\n");
821 return remove_p;
822 }
823
824 /* Generate and emit a static constructor or destructor. WHICH must
825 be one of 'I' (for a constructor), 'D' (for a destructor).
826 BODY is a STATEMENT_LIST containing GENERIC
827 statements. PRIORITY is the initialization priority for this
828 constructor or destructor.
829
830 FINAL specify whether the externally visible name for collect2 should
831 be produced. */
832
833 static void
834 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final,
835 tree optimization,
836 tree target)
837 {
838 static int counter = 0;
839 char which_buf[16];
840 tree decl, name, resdecl;
841
842 /* The priority is encoded in the constructor or destructor name.
843 collect2 will sort the names and arrange that they are called at
844 program startup. */
845 if (!targetm.have_ctors_dtors && final)
846 {
847 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
848 name = get_file_function_name (which_buf);
849 }
850 else
851 {
852 /* Proudce sane name but one not recognizable by collect2, just for the
853 case we fail to inline the function. */
854 sprintf (which_buf, "_sub_%c_%.5d_%d", which, priority, counter++);
855 name = get_identifier (which_buf);
856 }
857
858 decl = build_decl (input_location, FUNCTION_DECL, name,
859 build_function_type_list (void_type_node, NULL_TREE));
860 current_function_decl = decl;
861
862 resdecl = build_decl (input_location,
863 RESULT_DECL, NULL_TREE, void_type_node);
864 DECL_ARTIFICIAL (resdecl) = 1;
865 DECL_RESULT (decl) = resdecl;
866 DECL_CONTEXT (resdecl) = decl;
867
868 allocate_struct_function (decl, false);
869
870 TREE_STATIC (decl) = 1;
871 TREE_USED (decl) = 1;
872 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl) = optimization;
873 DECL_FUNCTION_SPECIFIC_TARGET (decl) = target;
874 DECL_ARTIFICIAL (decl) = 1;
875 DECL_IGNORED_P (decl) = 1;
876 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
877 DECL_SAVED_TREE (decl) = body;
878 if (!targetm.have_ctors_dtors && final)
879 {
880 TREE_PUBLIC (decl) = 1;
881 DECL_PRESERVE_P (decl) = 1;
882 }
883 DECL_UNINLINABLE (decl) = 1;
884
885 DECL_INITIAL (decl) = make_node (BLOCK);
886 BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl;
887 TREE_USED (DECL_INITIAL (decl)) = 1;
888
889 DECL_SOURCE_LOCATION (decl) = input_location;
890 cfun->function_end_locus = input_location;
891
892 switch (which)
893 {
894 case 'I':
895 DECL_STATIC_CONSTRUCTOR (decl) = 1;
896 decl_init_priority_insert (decl, priority);
897 break;
898 case 'D':
899 DECL_STATIC_DESTRUCTOR (decl) = 1;
900 decl_fini_priority_insert (decl, priority);
901 break;
902 default:
903 gcc_unreachable ();
904 }
905
906 gimplify_function_tree (decl);
907
908 cgraph_node::add_new_function (decl, false);
909
910 set_cfun (NULL);
911 current_function_decl = NULL;
912 }
913
914 /* Generate and emit a static constructor or destructor. WHICH must
915 be one of 'I' (for a constructor) or 'D' (for a destructor).
916 BODY is a STATEMENT_LIST containing GENERIC
917 statements. PRIORITY is the initialization priority for this
918 constructor or destructor. */
919
920 void
921 cgraph_build_static_cdtor (char which, tree body, int priority)
922 {
923 /* FIXME: We should be able to
924 gcc_assert (!in_lto_p);
925 because at LTO time the global options are not safe to use.
926 Unfortunately ASAN finish_file will produce constructors late and they
927 may lead to surprises. */
928 cgraph_build_static_cdtor_1 (which, body, priority, false,
929 optimization_default_node,
930 target_option_default_node);
931 }
932
933 /* When target does not have ctors and dtors, we call all constructor
934 and destructor by special initialization/destruction function
935 recognized by collect2.
936
937 When we are going to build this function, collect all constructors and
938 destructors and turn them into normal functions. */
939
940 static void
941 record_cdtor_fn (struct cgraph_node *node, vec<tree> *ctors, vec<tree> *dtors)
942 {
943 if (DECL_STATIC_CONSTRUCTOR (node->decl))
944 ctors->safe_push (node->decl);
945 if (DECL_STATIC_DESTRUCTOR (node->decl))
946 dtors->safe_push (node->decl);
947 node = cgraph_node::get (node->decl);
948 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
949 }
950
951 /* Define global constructors/destructor functions for the CDTORS, of
952 which they are LEN. The CDTORS are sorted by initialization
953 priority. If CTOR_P is true, these are constructors; otherwise,
954 they are destructors. */
955
956 static void
957 build_cdtor (bool ctor_p, const vec<tree> &cdtors)
958 {
959 size_t i,j;
960 size_t len = cdtors.length ();
961
962 i = 0;
963 while (i < len)
964 {
965 tree body;
966 tree fn;
967 priority_type priority;
968
969 priority = 0;
970 body = NULL_TREE;
971 j = i;
972 do
973 {
974 priority_type p;
975 fn = cdtors[j];
976 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
977 if (j == i)
978 priority = p;
979 else if (p != priority)
980 break;
981 j++;
982 }
983 while (j < len);
984
985 /* When there is only one cdtor and target supports them, do nothing. */
986 if (j == i + 1
987 && targetm.have_ctors_dtors)
988 {
989 i++;
990 continue;
991 }
992 /* Find the next batch of constructors/destructors with the same
993 initialization priority. */
994 for (;i < j; i++)
995 {
996 tree call;
997 fn = cdtors[i];
998 call = build_call_expr (fn, 0);
999 if (ctor_p)
1000 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1001 else
1002 DECL_STATIC_DESTRUCTOR (fn) = 0;
1003 /* We do not want to optimize away pure/const calls here.
1004 When optimizing, these should be already removed, when not
1005 optimizing, we want user to be able to breakpoint in them. */
1006 TREE_SIDE_EFFECTS (call) = 1;
1007 append_to_statement_list (call, &body);
1008 }
1009 gcc_assert (body != NULL_TREE);
1010 /* Generate a function to call all the function of like
1011 priority. */
1012 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true,
1013 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (cdtors[0]),
1014 DECL_FUNCTION_SPECIFIC_TARGET (cdtors[0]));
1015 }
1016 }
1017
1018 /* Comparison function for qsort. P1 and P2 are actually of type
1019 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1020 used to determine the sort order. */
1021
1022 static int
1023 compare_ctor (const void *p1, const void *p2)
1024 {
1025 tree f1;
1026 tree f2;
1027 int priority1;
1028 int priority2;
1029
1030 f1 = *(const tree *)p1;
1031 f2 = *(const tree *)p2;
1032 priority1 = DECL_INIT_PRIORITY (f1);
1033 priority2 = DECL_INIT_PRIORITY (f2);
1034
1035 if (priority1 < priority2)
1036 return -1;
1037 else if (priority1 > priority2)
1038 return 1;
1039 else
1040 /* Ensure a stable sort. Constructors are executed in backwarding
1041 order to make LTO initialize braries first. */
1042 return DECL_UID (f2) - DECL_UID (f1);
1043 }
1044
1045 /* Comparison function for qsort. P1 and P2 are actually of type
1046 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1047 used to determine the sort order. */
1048
1049 static int
1050 compare_dtor (const void *p1, const void *p2)
1051 {
1052 tree f1;
1053 tree f2;
1054 int priority1;
1055 int priority2;
1056
1057 f1 = *(const tree *)p1;
1058 f2 = *(const tree *)p2;
1059 priority1 = DECL_FINI_PRIORITY (f1);
1060 priority2 = DECL_FINI_PRIORITY (f2);
1061
1062 if (priority1 < priority2)
1063 return -1;
1064 else if (priority1 > priority2)
1065 return 1;
1066 else
1067 /* Ensure a stable sort. */
1068 return DECL_UID (f1) - DECL_UID (f2);
1069 }
1070
1071 /* Generate functions to call static constructors and destructors
1072 for targets that do not support .ctors/.dtors sections. These
1073 functions have magic names which are detected by collect2. */
1074
1075 static void
1076 build_cdtor_fns (vec<tree> *ctors, vec<tree> *dtors)
1077 {
1078 if (!ctors->is_empty ())
1079 {
1080 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1081 ctors->qsort (compare_ctor);
1082 build_cdtor (/*ctor_p=*/true, *ctors);
1083 }
1084
1085 if (!dtors->is_empty ())
1086 {
1087 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1088 dtors->qsort (compare_dtor);
1089 build_cdtor (/*ctor_p=*/false, *dtors);
1090 }
1091 }
1092
1093 /* Look for constructors and destructors and produce function calling them.
1094 This is needed for targets not supporting ctors or dtors, but we perform the
1095 transformation also at linktime to merge possibly numerous
1096 constructors/destructors into single function to improve code locality and
1097 reduce size. */
1098
1099 static unsigned int
1100 ipa_cdtor_merge (void)
1101 {
1102 /* A vector of FUNCTION_DECLs declared as static constructors. */
1103 auto_vec<tree, 20> ctors;
1104 /* A vector of FUNCTION_DECLs declared as static destructors. */
1105 auto_vec<tree, 20> dtors;
1106 struct cgraph_node *node;
1107 FOR_EACH_DEFINED_FUNCTION (node)
1108 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1109 || DECL_STATIC_DESTRUCTOR (node->decl))
1110 record_cdtor_fn (node, &ctors, &dtors);
1111 build_cdtor_fns (&ctors, &dtors);
1112 return 0;
1113 }
1114
1115 namespace {
1116
1117 const pass_data pass_data_ipa_cdtor_merge =
1118 {
1119 IPA_PASS, /* type */
1120 "cdtor", /* name */
1121 OPTGROUP_NONE, /* optinfo_flags */
1122 TV_CGRAPHOPT, /* tv_id */
1123 0, /* properties_required */
1124 0, /* properties_provided */
1125 0, /* properties_destroyed */
1126 0, /* todo_flags_start */
1127 0, /* todo_flags_finish */
1128 };
1129
1130 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1131 {
1132 public:
1133 pass_ipa_cdtor_merge (gcc::context *ctxt)
1134 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1135 NULL, /* generate_summary */
1136 NULL, /* write_summary */
1137 NULL, /* read_summary */
1138 NULL, /* write_optimization_summary */
1139 NULL, /* read_optimization_summary */
1140 NULL, /* stmt_fixup */
1141 0, /* function_transform_todo_flags_start */
1142 NULL, /* function_transform */
1143 NULL) /* variable_transform */
1144 {}
1145
1146 /* opt_pass methods: */
1147 virtual bool gate (function *);
1148 virtual unsigned int execute (function *) { return ipa_cdtor_merge (); }
1149
1150 }; // class pass_ipa_cdtor_merge
1151
1152 bool
1153 pass_ipa_cdtor_merge::gate (function *)
1154 {
1155 /* Perform the pass when we have no ctors/dtors support
1156 or at LTO time to merge multiple constructors into single
1157 function. */
1158 return !targetm.have_ctors_dtors || in_lto_p;
1159 }
1160
1161 } // anon namespace
1162
1163 ipa_opt_pass_d *
1164 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1165 {
1166 return new pass_ipa_cdtor_merge (ctxt);
1167 }
1168
1169 /* Invalid pointer representing BOTTOM for single user dataflow. */
1170 #define BOTTOM ((cgraph_node *)(size_t) 2)
1171
1172 /* Meet operation for single user dataflow.
1173 Here we want to associate variables with sigle function that may access it.
1174
1175 FUNCTION is current single user of a variable, VAR is variable that uses it.
1176 Latttice is stored in SINGLE_USER_MAP.
1177
1178 We represent:
1179 - TOP by no entry in SIGNLE_USER_MAP
1180 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1181 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1182
1183 cgraph_node *
1184 meet (cgraph_node *function, varpool_node *var,
1185 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1186 {
1187 struct cgraph_node *user, **f;
1188
1189 if (var->aux == BOTTOM)
1190 return BOTTOM;
1191
1192 f = single_user_map.get (var);
1193 if (!f)
1194 return function;
1195 user = *f;
1196 if (!function)
1197 return user;
1198 else if (function != user)
1199 return BOTTOM;
1200 else
1201 return function;
1202 }
1203
1204 /* Propagation step of single-use dataflow.
1205
1206 Check all uses of VNODE and see if they are used by single function FUNCTION.
1207 SINGLE_USER_MAP represents the dataflow lattice. */
1208
1209 cgraph_node *
1210 propagate_single_user (varpool_node *vnode, cgraph_node *function,
1211 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1212 {
1213 int i;
1214 struct ipa_ref *ref;
1215
1216 gcc_assert (!vnode->externally_visible);
1217
1218 /* If node is an alias, first meet with its target. */
1219 if (vnode->alias)
1220 function = meet (function, vnode->get_alias_target (), single_user_map);
1221
1222 /* Check all users and see if they correspond to a single function. */
1223 for (i = 0; vnode->iterate_referring (i, ref) && function != BOTTOM; i++)
1224 {
1225 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
1226 if (cnode)
1227 {
1228 if (cnode->inlined_to)
1229 cnode = cnode->inlined_to;
1230 if (!function)
1231 function = cnode;
1232 else if (function != cnode)
1233 function = BOTTOM;
1234 }
1235 else
1236 function = meet (function, dyn_cast <varpool_node *> (ref->referring),
1237 single_user_map);
1238 }
1239 return function;
1240 }
1241
1242 /* Pass setting used_by_single_function flag.
1243 This flag is set on variable when there is only one function that may
1244 possibly referr to it. */
1245
1246 static unsigned int
1247 ipa_single_use (void)
1248 {
1249 varpool_node *first = (varpool_node *) (void *) 1;
1250 varpool_node *var;
1251 hash_map<varpool_node *, cgraph_node *> single_user_map;
1252
1253 FOR_EACH_DEFINED_VARIABLE (var)
1254 if (!var->all_refs_explicit_p ())
1255 var->aux = BOTTOM;
1256 else
1257 {
1258 /* Enqueue symbol for dataflow. */
1259 var->aux = first;
1260 first = var;
1261 }
1262
1263 /* The actual dataflow. */
1264
1265 while (first != (void *) 1)
1266 {
1267 cgraph_node *user, *orig_user, **f;
1268
1269 var = first;
1270 first = (varpool_node *)first->aux;
1271
1272 f = single_user_map.get (var);
1273 if (f)
1274 orig_user = *f;
1275 else
1276 orig_user = NULL;
1277 user = propagate_single_user (var, orig_user, single_user_map);
1278
1279 gcc_checking_assert (var->aux != BOTTOM);
1280
1281 /* If user differs, enqueue all references. */
1282 if (user != orig_user)
1283 {
1284 unsigned int i;
1285 ipa_ref *ref;
1286
1287 single_user_map.put (var, user);
1288
1289 /* Enqueue all aliases for re-processing. */
1290 for (i = 0; var->iterate_direct_aliases (i, ref); i++)
1291 if (!ref->referring->aux)
1292 {
1293 ref->referring->aux = first;
1294 first = dyn_cast <varpool_node *> (ref->referring);
1295 }
1296 /* Enqueue all users for re-processing. */
1297 for (i = 0; var->iterate_reference (i, ref); i++)
1298 if (!ref->referred->aux
1299 && ref->referred->definition
1300 && is_a <varpool_node *> (ref->referred))
1301 {
1302 ref->referred->aux = first;
1303 first = dyn_cast <varpool_node *> (ref->referred);
1304 }
1305
1306 /* If user is BOTTOM, just punt on this var. */
1307 if (user == BOTTOM)
1308 var->aux = BOTTOM;
1309 else
1310 var->aux = NULL;
1311 }
1312 else
1313 var->aux = NULL;
1314 }
1315
1316 FOR_EACH_DEFINED_VARIABLE (var)
1317 {
1318 if (var->aux != BOTTOM)
1319 {
1320 /* Not having the single user known means that the VAR is
1321 unreachable. Either someone forgot to remove unreachable
1322 variables or the reachability here is wrong. */
1323
1324 gcc_checking_assert (single_user_map.get (var));
1325
1326 if (dump_file)
1327 {
1328 fprintf (dump_file, "Variable %s is used by single function\n",
1329 var->dump_name ());
1330 }
1331 var->used_by_single_function = true;
1332 }
1333 var->aux = NULL;
1334 }
1335 return 0;
1336 }
1337
1338 namespace {
1339
1340 const pass_data pass_data_ipa_single_use =
1341 {
1342 IPA_PASS, /* type */
1343 "single-use", /* name */
1344 OPTGROUP_NONE, /* optinfo_flags */
1345 TV_CGRAPHOPT, /* tv_id */
1346 0, /* properties_required */
1347 0, /* properties_provided */
1348 0, /* properties_destroyed */
1349 0, /* todo_flags_start */
1350 0, /* todo_flags_finish */
1351 };
1352
1353 class pass_ipa_single_use : public ipa_opt_pass_d
1354 {
1355 public:
1356 pass_ipa_single_use (gcc::context *ctxt)
1357 : ipa_opt_pass_d (pass_data_ipa_single_use, ctxt,
1358 NULL, /* generate_summary */
1359 NULL, /* write_summary */
1360 NULL, /* read_summary */
1361 NULL, /* write_optimization_summary */
1362 NULL, /* read_optimization_summary */
1363 NULL, /* stmt_fixup */
1364 0, /* function_transform_todo_flags_start */
1365 NULL, /* function_transform */
1366 NULL) /* variable_transform */
1367 {}
1368
1369 /* opt_pass methods: */
1370 virtual unsigned int execute (function *) { return ipa_single_use (); }
1371
1372 }; // class pass_ipa_single_use
1373
1374 } // anon namespace
1375
1376 ipa_opt_pass_d *
1377 make_pass_ipa_single_use (gcc::context *ctxt)
1378 {
1379 return new pass_ipa_single_use (ctxt);
1380 }
1381
1382 /* Materialize all clones. */
1383
1384 namespace {
1385
1386 const pass_data pass_data_materialize_all_clones =
1387 {
1388 SIMPLE_IPA_PASS, /* type */
1389 "materialize-all-clones", /* name */
1390 OPTGROUP_NONE, /* optinfo_flags */
1391 TV_IPA_OPT, /* tv_id */
1392 0, /* properties_required */
1393 0, /* properties_provided */
1394 0, /* properties_destroyed */
1395 0, /* todo_flags_start */
1396 0, /* todo_flags_finish */
1397 };
1398
1399 class pass_materialize_all_clones : public simple_ipa_opt_pass
1400 {
1401 public:
1402 pass_materialize_all_clones (gcc::context *ctxt)
1403 : simple_ipa_opt_pass (pass_data_materialize_all_clones, ctxt)
1404 {}
1405
1406 /* opt_pass methods: */
1407 virtual unsigned int execute (function *)
1408 {
1409 symtab->materialize_all_clones ();
1410 return 0;
1411 }
1412
1413 }; // class pass_materialize_all_clones
1414
1415 } // anon namespace
1416
1417 simple_ipa_opt_pass *
1418 make_pass_materialize_all_clones (gcc::context *ctxt)
1419 {
1420 return new pass_materialize_all_clones (ctxt);
1421 }