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