PR target/81369
[official-gcc.git] / gcc / ipa.c
blob00cd3084f660c9ad764e9544fba369f3d371d26a
1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
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"
42 /* Return true when NODE has ADDR reference. */
44 static bool
45 has_addr_references_p (struct cgraph_node *node,
46 void *)
48 int i;
49 struct ipa_ref *ref = NULL;
51 for (i = 0; node->iterate_referring (i, ref); i++)
52 if (ref->use == IPA_REF_ADDR)
53 return true;
54 return false;
57 /* Return true when NODE can be target of an indirect call. */
59 static bool
60 is_indirect_call_target_p (struct cgraph_node *node, void *)
62 return node->indirect_call_target;
65 /* Look for all functions inlined to NODE and update their inlined_to pointers
66 to INLINED_TO. */
68 static void
69 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
71 struct cgraph_edge *e;
72 for (e = node->callees; e; e = e->next_callee)
73 if (e->callee->global.inlined_to)
75 e->callee->global.inlined_to = inlined_to;
76 update_inlined_to_pointer (e->callee, inlined_to);
80 /* Add symtab NODE to queue starting at FIRST.
82 The queue is linked via AUX pointers and terminated by pointer to 1.
83 We enqueue nodes at two occasions: when we find them reachable or when we find
84 their bodies needed for further clonning. In the second case we mark them
85 by pointer to 2 after processing so they are re-queue when they become
86 reachable. */
88 static void
89 enqueue_node (symtab_node *node, symtab_node **first,
90 hash_set<symtab_node *> *reachable)
92 /* Node is still in queue; do nothing. */
93 if (node->aux && node->aux != (void *) 2)
94 return;
95 /* Node was already processed as unreachable, re-enqueue
96 only if it became reachable now. */
97 if (node->aux == (void *)2 && !reachable->contains (node))
98 return;
99 node->aux = *first;
100 *first = node;
103 /* Process references. */
105 static void
106 process_references (symtab_node *snode,
107 symtab_node **first,
108 bool before_inlining_p,
109 hash_set<symtab_node *> *reachable)
111 int i;
112 struct ipa_ref *ref = NULL;
113 for (i = 0; snode->iterate_reference (i, ref); i++)
115 symtab_node *node = ref->referred;
116 symtab_node *body = node->ultimate_alias_target ();
118 if (node->definition && !node->in_other_partition
119 && ((!DECL_EXTERNAL (node->decl) || node->alias)
120 || (((before_inlining_p
121 && (TREE_CODE (node->decl) != FUNCTION_DECL
122 || (TREE_CODE (node->decl) == FUNCTION_DECL
123 && opt_for_fn (body->decl, optimize))
124 || (symtab->state < IPA_SSA
125 && lookup_attribute
126 ("always_inline",
127 DECL_ATTRIBUTES (body->decl))))))
128 /* We use variable constructors during late compilation for
129 constant folding. Keep references alive so partitioning
130 knows about potential references. */
131 || (VAR_P (node->decl)
132 && flag_wpa
133 && ctor_for_folding (node->decl)
134 != error_mark_node))))
136 /* Be sure that we will not optimize out alias target
137 body. */
138 if (DECL_EXTERNAL (node->decl)
139 && node->alias
140 && before_inlining_p)
141 reachable->add (body);
142 reachable->add (node);
144 enqueue_node (node, first, reachable);
148 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
149 all its potential targets as reachable to permit later inlining if
150 devirtualization happens. After inlining still keep their declarations
151 around, so we can devirtualize to a direct call.
153 Also try to make trivial devirutalization when no or only one target is
154 possible. */
156 static void
157 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
158 struct cgraph_edge *edge,
159 symtab_node **first,
160 hash_set<symtab_node *> *reachable,
161 bool before_inlining_p)
163 unsigned int i;
164 void *cache_token;
165 bool final;
166 vec <cgraph_node *>targets
167 = possible_polymorphic_call_targets
168 (edge, &final, &cache_token);
170 if (!reachable_call_targets->add (cache_token))
172 for (i = 0; i < targets.length (); i++)
174 struct cgraph_node *n = targets[i];
176 /* Do not bother to mark virtual methods in anonymous namespace;
177 either we will find use of virtual table defining it, or it is
178 unused. */
179 if (TREE_CODE (TREE_TYPE (n->decl)) == METHOD_TYPE
180 && type_in_anonymous_namespace_p
181 (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl))))
182 continue;
184 n->indirect_call_target = true;
185 symtab_node *body = n->function_symbol ();
187 /* Prior inlining, keep alive bodies of possible targets for
188 devirtualization. */
189 if (n->definition
190 && (before_inlining_p
191 && opt_for_fn (body->decl, optimize)
192 && opt_for_fn (body->decl, flag_devirtualize)))
194 /* Be sure that we will not optimize out alias target
195 body. */
196 if (DECL_EXTERNAL (n->decl)
197 && n->alias
198 && before_inlining_p)
199 reachable->add (body);
200 reachable->add (n);
202 /* Even after inlining we want to keep the possible targets in the
203 boundary, so late passes can still produce direct call even if
204 the chance for inlining is lost. */
205 enqueue_node (n, first, reachable);
209 /* Very trivial devirtualization; when the type is
210 final or anonymous (so we know all its derivation)
211 and there is only one possible virtual call target,
212 make the edge direct. */
213 if (final)
215 if (targets.length () <= 1 && dbg_cnt (devirt))
217 cgraph_node *target, *node = edge->caller;
218 if (targets.length () == 1)
219 target = targets[0];
220 else
221 target = cgraph_node::get_create
222 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
224 if (dump_enabled_p ())
226 location_t locus;
227 if (edge->call_stmt)
228 locus = gimple_location (edge->call_stmt);
229 else
230 locus = UNKNOWN_LOCATION;
231 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
232 "devirtualizing call in %s to %s\n",
233 edge->caller->dump_name (),
234 target->dump_name ());
236 edge = edge->make_direct (target);
237 if (ipa_fn_summaries)
238 ipa_update_overall_fn_summary (node);
239 else if (edge->call_stmt)
241 edge->redirect_call_stmt_to_callee ();
243 /* Call to __builtin_unreachable shouldn't be instrumented. */
244 if (!targets.length ())
245 gimple_call_set_with_bounds (edge->call_stmt, false);
251 /* Perform reachability analysis and reclaim all unreachable nodes.
253 The algorithm is basically mark&sweep but with some extra refinements:
255 - reachable extern inline functions needs special handling; the bodies needs
256 to stay in memory until inlining in hope that they will be inlined.
257 After inlining we release their bodies and turn them into unanalyzed
258 nodes even when they are reachable.
260 - virtual functions are kept in callgraph even if they seem unreachable in
261 hope calls to them will be devirtualized.
263 Again we remove them after inlining. In late optimization some
264 devirtualization may happen, but it is not important since we won't inline
265 the call. In theory early opts and IPA should work out all important cases.
267 - virtual clones needs bodies of their origins for later materialization;
268 this means that we want to keep the body even if the origin is unreachable
269 otherwise. To avoid origin from sitting in the callgraph and being
270 walked by IPA passes, we turn them into unanalyzed nodes with body
271 defined.
273 We maintain set of function declaration where body needs to stay in
274 body_needed_for_clonning
276 Inline clones represent special case: their declaration match the
277 declaration of origin and cgraph_remove_node already knows how to
278 reshape callgraph and preserve body when offline copy of function or
279 inline clone is being removed.
281 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
282 variables with DECL_INITIAL set. We finalize these and keep reachable
283 ones around for constant folding purposes. After inlining we however
284 stop walking their references to let everything static referneced by them
285 to be removed when it is otherwise unreachable.
287 We maintain queue of both reachable symbols (i.e. defined symbols that needs
288 to stay) and symbols that are in boundary (i.e. external symbols referenced
289 by reachable symbols or origins of clones). The queue is represented
290 as linked list by AUX pointer terminated by 1.
292 At the end we keep all reachable symbols. For symbols in boundary we always
293 turn definition into a declaration, but we may keep function body around
294 based on body_needed_for_clonning
296 All symbols that enter the queue have AUX pointer non-zero and are in the
297 boundary. Pointer set REACHABLE is used to track reachable symbols.
299 Every symbol can be visited twice - once as part of boundary and once
300 as real reachable symbol. enqueue_node needs to decide whether the
301 node needs to be re-queued for second processing. For this purpose
302 we set AUX pointer of processed symbols in the boundary to constant 2. */
304 bool
305 symbol_table::remove_unreachable_nodes (FILE *file)
307 symtab_node *first = (symtab_node *) (void *) 1;
308 struct cgraph_node *node, *next;
309 varpool_node *vnode, *vnext;
310 bool changed = false;
311 hash_set<symtab_node *> reachable;
312 hash_set<tree> body_needed_for_clonning;
313 hash_set<void *> reachable_call_targets;
314 bool before_inlining_p = symtab->state < (!optimize && !in_lto_p ? IPA_SSA
315 : IPA_SSA_AFTER_INLINING);
317 timevar_push (TV_IPA_UNREACHABLE);
318 build_type_inheritance_graph ();
319 if (file)
320 fprintf (file, "\nReclaiming functions:");
321 if (flag_checking)
323 FOR_EACH_FUNCTION (node)
324 gcc_assert (!node->aux);
325 FOR_EACH_VARIABLE (vnode)
326 gcc_assert (!vnode->aux);
328 /* Mark functions whose bodies are obviously needed.
329 This is mostly when they can be referenced externally. Inline clones
330 are special since their declarations are shared with master clone and thus
331 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
332 FOR_EACH_FUNCTION (node)
334 node->used_as_abstract_origin = false;
335 node->indirect_call_target = false;
336 if (node->definition
337 && !node->global.inlined_to
338 && !node->in_other_partition
339 && !node->can_remove_if_no_direct_calls_and_refs_p ())
341 gcc_assert (!node->global.inlined_to);
342 reachable.add (node);
343 enqueue_node (node, &first, &reachable);
345 else
346 gcc_assert (!node->aux);
349 /* Mark variables that are obviously needed. */
350 FOR_EACH_DEFINED_VARIABLE (vnode)
351 if (!vnode->can_remove_if_no_refs_p()
352 && !vnode->in_other_partition)
354 reachable.add (vnode);
355 enqueue_node (vnode, &first, &reachable);
358 /* Perform reachability analysis. */
359 while (first != (symtab_node *) (void *) 1)
361 bool in_boundary_p = !reachable.contains (first);
362 symtab_node *node = first;
364 first = (symtab_node *)first->aux;
366 /* If we are processing symbol in boundary, mark its AUX pointer for
367 possible later re-processing in enqueue_node. */
368 if (in_boundary_p)
370 node->aux = (void *)2;
371 if (node->alias && node->analyzed)
372 enqueue_node (node->get_alias_target (), &first, &reachable);
374 else
376 if (TREE_CODE (node->decl) == FUNCTION_DECL
377 && DECL_ABSTRACT_ORIGIN (node->decl))
379 struct cgraph_node *origin_node
380 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node->decl));
381 if (origin_node && !origin_node->used_as_abstract_origin)
383 origin_node->used_as_abstract_origin = true;
384 gcc_assert (!origin_node->prev_sibling_clone);
385 gcc_assert (!origin_node->next_sibling_clone);
386 for (cgraph_node *n = origin_node->clones; n;
387 n = n->next_sibling_clone)
388 if (n->decl == DECL_ABSTRACT_ORIGIN (node->decl))
389 n->used_as_abstract_origin = true;
392 /* If any symbol in a comdat group is reachable, force
393 all externally visible symbols in the same comdat
394 group to be reachable as well. Comdat-local symbols
395 can be discarded if all uses were inlined. */
396 if (node->same_comdat_group)
398 symtab_node *next;
399 for (next = node->same_comdat_group;
400 next != node;
401 next = next->same_comdat_group)
402 if (!next->comdat_local_p ()
403 && !reachable.add (next))
404 enqueue_node (next, &first, &reachable);
406 /* Mark references as reachable. */
407 process_references (node, &first, before_inlining_p, &reachable);
410 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
412 /* Mark the callees reachable unless they are direct calls to extern
413 inline functions we decided to not inline. */
414 if (!in_boundary_p)
416 struct cgraph_edge *e;
417 /* Keep alive possible targets for devirtualization. */
418 if (opt_for_fn (cnode->decl, optimize)
419 && opt_for_fn (cnode->decl, flag_devirtualize))
421 struct cgraph_edge *next;
422 for (e = cnode->indirect_calls; e; e = next)
424 next = e->next_callee;
425 if (e->indirect_info->polymorphic)
426 walk_polymorphic_call_targets (&reachable_call_targets,
427 e, &first, &reachable,
428 before_inlining_p);
431 for (e = cnode->callees; e; e = e->next_callee)
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 || (before_inlining_p
440 && (opt_for_fn (body->decl, optimize)
441 || (symtab->state < IPA_SSA
442 && lookup_attribute
443 ("always_inline",
444 DECL_ATTRIBUTES (body->decl)))))))
446 /* Be sure that we will not optimize out alias target
447 body. */
448 if (DECL_EXTERNAL (e->callee->decl)
449 && e->callee->alias
450 && before_inlining_p)
451 reachable.add (body);
452 reachable.add (e->callee);
454 enqueue_node (e->callee, &first, &reachable);
457 /* When inline clone exists, mark body to be preserved so when removing
458 offline copy of the function we don't kill it. */
459 if (cnode->global.inlined_to)
460 body_needed_for_clonning.add (cnode->decl);
462 /* For instrumentation clones we always need original
463 function node for proper LTO privatization. */
464 if (cnode->instrumentation_clone
465 && cnode->definition)
467 gcc_assert (cnode->instrumented_version || in_lto_p);
468 if (cnode->instrumented_version)
470 enqueue_node (cnode->instrumented_version, &first,
471 &reachable);
472 reachable.add (cnode->instrumented_version);
476 /* For non-inline clones, force their origins to the boundary and ensure
477 that body is not removed. */
478 while (cnode->clone_of)
480 bool noninline = cnode->clone_of->decl != cnode->decl;
481 cnode = cnode->clone_of;
482 if (noninline)
484 body_needed_for_clonning.add (cnode->decl);
485 enqueue_node (cnode, &first, &reachable);
490 else if (cnode->thunk.thunk_p)
491 enqueue_node (cnode->callees->callee, &first, &reachable);
493 /* If any reachable function has simd clones, mark them as
494 reachable as well. */
495 if (cnode->simd_clones)
497 cgraph_node *next;
498 for (next = cnode->simd_clones;
499 next;
500 next = next->simdclone->next_clone)
501 if (in_boundary_p
502 || !reachable.add (next))
503 enqueue_node (next, &first, &reachable);
506 /* When we see constructor of external variable, keep referred nodes in the
507 boundary. This will also hold initializers of the external vars NODE
508 refers to. */
509 varpool_node *vnode = dyn_cast <varpool_node *> (node);
510 if (vnode
511 && DECL_EXTERNAL (node->decl)
512 && !vnode->alias
513 && in_boundary_p)
515 struct ipa_ref *ref = NULL;
516 for (int i = 0; node->iterate_reference (i, ref); i++)
517 enqueue_node (ref->referred, &first, &reachable);
521 /* Remove unreachable functions. */
522 for (node = first_function (); node; node = next)
524 next = next_function (node);
526 /* If node is not needed at all, remove it. */
527 if (!node->aux)
529 if (file)
530 fprintf (file, " %s", node->dump_name ());
531 node->remove ();
532 changed = true;
534 /* If node is unreachable, remove its body. */
535 else if (!reachable.contains (node))
537 /* We keep definitions of thunks and aliases in the boundary so
538 we can walk to the ultimate alias targets and function symbols
539 reliably. */
540 if (node->alias || node->thunk.thunk_p)
542 else if (!body_needed_for_clonning.contains (node->decl)
543 && !node->alias && !node->thunk.thunk_p)
544 node->release_body ();
545 else if (!node->clone_of)
546 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
547 if (node->definition && !node->alias && !node->thunk.thunk_p)
549 if (file)
550 fprintf (file, " %s", node->dump_name ());
551 node->body_removed = true;
552 node->analyzed = false;
553 node->definition = false;
554 node->cpp_implicit_alias = false;
555 node->alias = false;
556 node->transparent_alias = false;
557 node->thunk.thunk_p = false;
558 node->weakref = false;
559 /* After early inlining we drop always_inline attributes on
560 bodies of functions that are still referenced (have their
561 address taken). */
562 DECL_ATTRIBUTES (node->decl)
563 = remove_attribute ("always_inline",
564 DECL_ATTRIBUTES (node->decl));
565 if (!node->in_other_partition)
566 node->local.local = false;
567 node->remove_callees ();
568 node->remove_all_references ();
569 changed = true;
570 if (node->thunk.thunk_p
571 && node->thunk.add_pointer_bounds_args)
573 node->thunk.thunk_p = false;
574 node->thunk.add_pointer_bounds_args = false;
578 else
579 gcc_assert (node->clone_of || !node->has_gimple_body_p ()
580 || in_lto_p || DECL_RESULT (node->decl));
583 /* Inline clones might be kept around so their materializing allows further
584 cloning. If the function the clone is inlined into is removed, we need
585 to turn it into normal cone. */
586 FOR_EACH_FUNCTION (node)
588 if (node->global.inlined_to
589 && !node->callers)
591 gcc_assert (node->clones);
592 node->global.inlined_to = NULL;
593 update_inlined_to_pointer (node, node);
595 node->aux = NULL;
598 /* Remove unreachable variables. */
599 if (file)
600 fprintf (file, "\nReclaiming variables:");
601 for (vnode = first_variable (); vnode; vnode = vnext)
603 vnext = next_variable (vnode);
604 if (!vnode->aux
605 /* For can_refer_decl_in_current_unit_p we want to track for
606 all external variables if they are defined in other partition
607 or not. */
608 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
610 struct ipa_ref *ref = NULL;
612 /* First remove the aliases, so varpool::remove can possibly lookup
613 the constructor and save it for future use. */
614 while (vnode->iterate_direct_aliases (0, ref))
616 if (file)
617 fprintf (file, " %s", ref->referred->dump_name ());
618 ref->referring->remove ();
620 if (file)
621 fprintf (file, " %s", vnode->dump_name ());
622 vnext = next_variable (vnode);
623 /* Signal removal to the debug machinery. */
624 if (! flag_wpa)
626 vnode->definition = false;
627 (*debug_hooks->late_global_decl) (vnode->decl);
629 vnode->remove ();
630 changed = true;
632 else if (!reachable.contains (vnode) && !vnode->alias)
634 tree init;
635 if (vnode->definition)
637 if (file)
638 fprintf (file, " %s", vnode->name ());
639 changed = true;
641 /* Keep body if it may be useful for constant folding. */
642 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node
643 && !POINTER_BOUNDS_P (vnode->decl))
644 vnode->remove_initializer ();
645 else
646 DECL_INITIAL (vnode->decl) = init;
647 vnode->body_removed = true;
648 vnode->definition = false;
649 vnode->analyzed = false;
650 vnode->aux = NULL;
652 vnode->remove_from_same_comdat_group ();
654 vnode->remove_all_references ();
656 else
657 vnode->aux = NULL;
660 /* Now update address_taken flags and try to promote functions to be local. */
661 if (file)
662 fprintf (file, "\nClearing address taken flags:");
663 FOR_EACH_DEFINED_FUNCTION (node)
664 if (node->address_taken
665 && !node->used_from_other_partition)
667 if (!node->call_for_symbol_and_aliases
668 (has_addr_references_p, NULL, true)
669 && (!node->instrumentation_clone
670 || !node->instrumented_version
671 || !node->instrumented_version->address_taken))
673 if (file)
674 fprintf (file, " %s", node->name ());
675 node->address_taken = false;
676 changed = true;
677 if (node->local_p ()
678 /* Virtual functions may be kept in cgraph just because
679 of possible later devirtualization. Do not mark them as
680 local too early so we won't optimize them out before
681 we are done with polymorphic call analysis. */
682 && (!before_inlining_p
683 || !node->call_for_symbol_and_aliases
684 (is_indirect_call_target_p, NULL, true)))
686 node->local.local = true;
687 if (file)
688 fprintf (file, " (local)");
692 if (file)
693 fprintf (file, "\n");
695 symtab_node::checking_verify_symtab_nodes ();
697 /* If we removed something, perhaps profile could be improved. */
698 if (changed && (optimize || in_lto_p) && ipa_call_summaries)
699 FOR_EACH_DEFINED_FUNCTION (node)
700 ipa_propagate_frequency (node);
702 timevar_pop (TV_IPA_UNREACHABLE);
703 return changed;
706 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
707 as needed, also clear EXPLICIT_REFS if the references to given variable
708 do not need to be explicit. */
710 void
711 process_references (varpool_node *vnode,
712 bool *written, bool *address_taken,
713 bool *read, bool *explicit_refs)
715 int i;
716 struct ipa_ref *ref;
718 if (!vnode->all_refs_explicit_p ()
719 || TREE_THIS_VOLATILE (vnode->decl))
720 *explicit_refs = false;
722 for (i = 0; vnode->iterate_referring (i, ref)
723 && *explicit_refs && (!*written || !*address_taken || !*read); i++)
724 switch (ref->use)
726 case IPA_REF_ADDR:
727 *address_taken = true;
728 break;
729 case IPA_REF_LOAD:
730 *read = true;
731 break;
732 case IPA_REF_STORE:
733 *written = true;
734 break;
735 case IPA_REF_ALIAS:
736 process_references (dyn_cast<varpool_node *> (ref->referring), written,
737 address_taken, read, explicit_refs);
738 break;
739 case IPA_REF_CHKP:
740 gcc_unreachable ();
744 /* Set TREE_READONLY bit. */
746 bool
747 set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
749 TREE_READONLY (vnode->decl) = true;
750 return false;
753 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
755 bool
756 set_writeonly_bit (varpool_node *vnode, void *data)
758 vnode->writeonly = true;
759 if (optimize || in_lto_p)
761 DECL_INITIAL (vnode->decl) = NULL;
762 if (!vnode->alias)
764 if (vnode->num_references ())
765 *(bool *)data = true;
766 vnode->remove_all_references ();
769 return false;
772 /* Clear addressale bit of VNODE. */
774 bool
775 clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
777 vnode->address_taken = false;
778 TREE_ADDRESSABLE (vnode->decl) = 0;
779 return false;
782 /* Discover variables that have no longer address taken or that are read only
783 and update their flags.
785 Return true when unreachable symbol removan should be done.
787 FIXME: This can not be done in between gimplify and omp_expand since
788 readonly flag plays role on what is shared and what is not. Currently we do
789 this transformation as part of whole program visibility and re-do at
790 ipa-reference pass (to take into account clonning), but it would
791 make sense to do it before early optimizations. */
793 bool
794 ipa_discover_readonly_nonaddressable_vars (void)
796 bool remove_p = false;
797 varpool_node *vnode;
798 if (dump_file)
799 fprintf (dump_file, "Clearing variable flags:");
800 FOR_EACH_VARIABLE (vnode)
801 if (!vnode->alias
802 && (TREE_ADDRESSABLE (vnode->decl)
803 || !vnode->writeonly
804 || !TREE_READONLY (vnode->decl)))
806 bool written = false;
807 bool address_taken = false;
808 bool read = false;
809 bool explicit_refs = true;
811 process_references (vnode, &written, &address_taken, &read,
812 &explicit_refs);
813 if (!explicit_refs)
814 continue;
815 if (!address_taken)
817 if (TREE_ADDRESSABLE (vnode->decl) && dump_file)
818 fprintf (dump_file, " %s (non-addressable)", vnode->name ());
819 vnode->call_for_symbol_and_aliases (clear_addressable_bit, NULL,
820 true);
822 if (!address_taken && !written
823 /* Making variable in explicit section readonly can cause section
824 type conflict.
825 See e.g. gcc.c-torture/compile/pr23237.c */
826 && vnode->get_section () == NULL)
828 if (!TREE_READONLY (vnode->decl) && dump_file)
829 fprintf (dump_file, " %s (read-only)", vnode->name ());
830 vnode->call_for_symbol_and_aliases (set_readonly_bit, NULL, true);
832 if (!vnode->writeonly && !read && !address_taken && written)
834 if (dump_file)
835 fprintf (dump_file, " %s (write-only)", vnode->name ());
836 vnode->call_for_symbol_and_aliases (set_writeonly_bit, &remove_p,
837 true);
840 if (dump_file)
841 fprintf (dump_file, "\n");
842 return remove_p;
845 /* Generate and emit a static constructor or destructor. WHICH must
846 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
847 (for chp static vars constructor) or 'B' (for chkp static bounds
848 constructor). BODY is a STATEMENT_LIST containing GENERIC
849 statements. PRIORITY is the initialization priority for this
850 constructor or destructor.
852 FINAL specify whether the externally visible name for collect2 should
853 be produced. */
855 static void
856 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
858 static int counter = 0;
859 char which_buf[16];
860 tree decl, name, resdecl;
862 /* The priority is encoded in the constructor or destructor name.
863 collect2 will sort the names and arrange that they are called at
864 program startup. */
865 if (final)
866 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
867 else
868 /* Proudce sane name but one not recognizable by collect2, just for the
869 case we fail to inline the function. */
870 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
871 name = get_file_function_name (which_buf);
873 decl = build_decl (input_location, FUNCTION_DECL, name,
874 build_function_type_list (void_type_node, NULL_TREE));
875 current_function_decl = decl;
877 resdecl = build_decl (input_location,
878 RESULT_DECL, NULL_TREE, void_type_node);
879 DECL_ARTIFICIAL (resdecl) = 1;
880 DECL_RESULT (decl) = resdecl;
881 DECL_CONTEXT (resdecl) = decl;
883 allocate_struct_function (decl, false);
885 TREE_STATIC (decl) = 1;
886 TREE_USED (decl) = 1;
887 DECL_ARTIFICIAL (decl) = 1;
888 DECL_IGNORED_P (decl) = 1;
889 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
890 DECL_SAVED_TREE (decl) = body;
891 if (!targetm.have_ctors_dtors && final)
893 TREE_PUBLIC (decl) = 1;
894 DECL_PRESERVE_P (decl) = 1;
896 DECL_UNINLINABLE (decl) = 1;
898 DECL_INITIAL (decl) = make_node (BLOCK);
899 BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl;
900 TREE_USED (DECL_INITIAL (decl)) = 1;
902 DECL_SOURCE_LOCATION (decl) = input_location;
903 cfun->function_end_locus = input_location;
905 switch (which)
907 case 'I':
908 DECL_STATIC_CONSTRUCTOR (decl) = 1;
909 decl_init_priority_insert (decl, priority);
910 break;
911 case 'P':
912 DECL_STATIC_CONSTRUCTOR (decl) = 1;
913 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("chkp ctor"),
914 NULL,
915 NULL_TREE);
916 decl_init_priority_insert (decl, priority);
917 break;
918 case 'B':
919 DECL_STATIC_CONSTRUCTOR (decl) = 1;
920 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("bnd_legacy"),
921 NULL,
922 NULL_TREE);
923 decl_init_priority_insert (decl, priority);
924 break;
925 case 'D':
926 DECL_STATIC_DESTRUCTOR (decl) = 1;
927 decl_fini_priority_insert (decl, priority);
928 break;
929 default:
930 gcc_unreachable ();
933 gimplify_function_tree (decl);
935 cgraph_node::add_new_function (decl, false);
937 set_cfun (NULL);
938 current_function_decl = NULL;
941 /* Generate and emit a static constructor or destructor. WHICH must
942 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
943 (for chkp static vars constructor) or 'B' (for chkp static bounds
944 constructor). BODY is a STATEMENT_LIST containing GENERIC
945 statements. PRIORITY is the initialization priority for this
946 constructor or destructor. */
948 void
949 cgraph_build_static_cdtor (char which, tree body, int priority)
951 cgraph_build_static_cdtor_1 (which, body, priority, false);
954 /* When target does not have ctors and dtors, we call all constructor
955 and destructor by special initialization/destruction function
956 recognized by collect2.
958 When we are going to build this function, collect all constructors and
959 destructors and turn them into normal functions. */
961 static void
962 record_cdtor_fn (struct cgraph_node *node, vec<tree> *ctors, vec<tree> *dtors)
964 if (DECL_STATIC_CONSTRUCTOR (node->decl))
965 ctors->safe_push (node->decl);
966 if (DECL_STATIC_DESTRUCTOR (node->decl))
967 dtors->safe_push (node->decl);
968 node = cgraph_node::get (node->decl);
969 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
972 /* Define global constructors/destructor functions for the CDTORS, of
973 which they are LEN. The CDTORS are sorted by initialization
974 priority. If CTOR_P is true, these are constructors; otherwise,
975 they are destructors. */
977 static void
978 build_cdtor (bool ctor_p, const vec<tree> &cdtors)
980 size_t i,j;
981 size_t len = cdtors.length ();
983 i = 0;
984 while (i < len)
986 tree body;
987 tree fn;
988 priority_type priority;
990 priority = 0;
991 body = NULL_TREE;
992 j = i;
995 priority_type p;
996 fn = cdtors[j];
997 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
998 if (j == i)
999 priority = p;
1000 else if (p != priority)
1001 break;
1002 j++;
1004 while (j < len);
1006 /* When there is only one cdtor and target supports them, do nothing. */
1007 if (j == i + 1
1008 && targetm.have_ctors_dtors)
1010 i++;
1011 continue;
1013 /* Find the next batch of constructors/destructors with the same
1014 initialization priority. */
1015 for (;i < j; i++)
1017 tree call;
1018 fn = cdtors[i];
1019 call = build_call_expr (fn, 0);
1020 if (ctor_p)
1021 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1022 else
1023 DECL_STATIC_DESTRUCTOR (fn) = 0;
1024 /* We do not want to optimize away pure/const calls here.
1025 When optimizing, these should be already removed, when not
1026 optimizing, we want user to be able to breakpoint in them. */
1027 TREE_SIDE_EFFECTS (call) = 1;
1028 append_to_statement_list (call, &body);
1030 gcc_assert (body != NULL_TREE);
1031 /* Generate a function to call all the function of like
1032 priority. */
1033 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1037 /* Comparison function for qsort. P1 and P2 are actually of type
1038 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1039 used to determine the sort order. */
1041 static int
1042 compare_ctor (const void *p1, const void *p2)
1044 tree f1;
1045 tree f2;
1046 int priority1;
1047 int priority2;
1049 f1 = *(const tree *)p1;
1050 f2 = *(const tree *)p2;
1051 priority1 = DECL_INIT_PRIORITY (f1);
1052 priority2 = DECL_INIT_PRIORITY (f2);
1054 if (priority1 < priority2)
1055 return -1;
1056 else if (priority1 > priority2)
1057 return 1;
1058 else
1059 /* Ensure a stable sort. Constructors are executed in backwarding
1060 order to make LTO initialize braries first. */
1061 return DECL_UID (f2) - DECL_UID (f1);
1064 /* Comparison function for qsort. P1 and P2 are actually of type
1065 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1066 used to determine the sort order. */
1068 static int
1069 compare_dtor (const void *p1, const void *p2)
1071 tree f1;
1072 tree f2;
1073 int priority1;
1074 int priority2;
1076 f1 = *(const tree *)p1;
1077 f2 = *(const tree *)p2;
1078 priority1 = DECL_FINI_PRIORITY (f1);
1079 priority2 = DECL_FINI_PRIORITY (f2);
1081 if (priority1 < priority2)
1082 return -1;
1083 else if (priority1 > priority2)
1084 return 1;
1085 else
1086 /* Ensure a stable sort. */
1087 return DECL_UID (f1) - DECL_UID (f2);
1090 /* Generate functions to call static constructors and destructors
1091 for targets that do not support .ctors/.dtors sections. These
1092 functions have magic names which are detected by collect2. */
1094 static void
1095 build_cdtor_fns (vec<tree> *ctors, vec<tree> *dtors)
1097 if (!ctors->is_empty ())
1099 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1100 ctors->qsort (compare_ctor);
1101 build_cdtor (/*ctor_p=*/true, *ctors);
1104 if (!dtors->is_empty ())
1106 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1107 dtors->qsort (compare_dtor);
1108 build_cdtor (/*ctor_p=*/false, *dtors);
1112 /* Look for constructors and destructors and produce function calling them.
1113 This is needed for targets not supporting ctors or dtors, but we perform the
1114 transformation also at linktime to merge possibly numerous
1115 constructors/destructors into single function to improve code locality and
1116 reduce size. */
1118 static unsigned int
1119 ipa_cdtor_merge (void)
1121 /* A vector of FUNCTION_DECLs declared as static constructors. */
1122 auto_vec<tree, 20> ctors;
1123 /* A vector of FUNCTION_DECLs declared as static destructors. */
1124 auto_vec<tree, 20> dtors;
1125 struct cgraph_node *node;
1126 FOR_EACH_DEFINED_FUNCTION (node)
1127 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1128 || DECL_STATIC_DESTRUCTOR (node->decl))
1129 record_cdtor_fn (node, &ctors, &dtors);
1130 build_cdtor_fns (&ctors, &dtors);
1131 return 0;
1134 namespace {
1136 const pass_data pass_data_ipa_cdtor_merge =
1138 IPA_PASS, /* type */
1139 "cdtor", /* name */
1140 OPTGROUP_NONE, /* optinfo_flags */
1141 TV_CGRAPHOPT, /* tv_id */
1142 0, /* properties_required */
1143 0, /* properties_provided */
1144 0, /* properties_destroyed */
1145 0, /* todo_flags_start */
1146 0, /* todo_flags_finish */
1149 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1151 public:
1152 pass_ipa_cdtor_merge (gcc::context *ctxt)
1153 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1154 NULL, /* generate_summary */
1155 NULL, /* write_summary */
1156 NULL, /* read_summary */
1157 NULL, /* write_optimization_summary */
1158 NULL, /* read_optimization_summary */
1159 NULL, /* stmt_fixup */
1160 0, /* function_transform_todo_flags_start */
1161 NULL, /* function_transform */
1162 NULL) /* variable_transform */
1165 /* opt_pass methods: */
1166 virtual bool gate (function *);
1167 virtual unsigned int execute (function *) { return ipa_cdtor_merge (); }
1169 }; // class pass_ipa_cdtor_merge
1171 bool
1172 pass_ipa_cdtor_merge::gate (function *)
1174 /* Perform the pass when we have no ctors/dtors support
1175 or at LTO time to merge multiple constructors into single
1176 function. */
1177 return !targetm.have_ctors_dtors || in_lto_p;
1180 } // anon namespace
1182 ipa_opt_pass_d *
1183 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1185 return new pass_ipa_cdtor_merge (ctxt);
1188 /* Invalid pointer representing BOTTOM for single user dataflow. */
1189 #define BOTTOM ((cgraph_node *)(size_t) 2)
1191 /* Meet operation for single user dataflow.
1192 Here we want to associate variables with sigle function that may access it.
1194 FUNCTION is current single user of a variable, VAR is variable that uses it.
1195 Latttice is stored in SINGLE_USER_MAP.
1197 We represent:
1198 - TOP by no entry in SIGNLE_USER_MAP
1199 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1200 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1202 cgraph_node *
1203 meet (cgraph_node *function, varpool_node *var,
1204 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1206 struct cgraph_node *user, **f;
1208 if (var->aux == BOTTOM)
1209 return BOTTOM;
1211 f = single_user_map.get (var);
1212 if (!f)
1213 return function;
1214 user = *f;
1215 if (!function)
1216 return user;
1217 else if (function != user)
1218 return BOTTOM;
1219 else
1220 return function;
1223 /* Propagation step of single-use dataflow.
1225 Check all uses of VNODE and see if they are used by single function FUNCTION.
1226 SINGLE_USER_MAP represents the dataflow lattice. */
1228 cgraph_node *
1229 propagate_single_user (varpool_node *vnode, cgraph_node *function,
1230 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1232 int i;
1233 struct ipa_ref *ref;
1235 gcc_assert (!vnode->externally_visible);
1237 /* If node is an alias, first meet with its target. */
1238 if (vnode->alias)
1239 function = meet (function, vnode->get_alias_target (), single_user_map);
1241 /* Check all users and see if they correspond to a single function. */
1242 for (i = 0; vnode->iterate_referring (i, ref) && function != BOTTOM; i++)
1244 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
1245 if (cnode)
1247 if (cnode->global.inlined_to)
1248 cnode = cnode->global.inlined_to;
1249 if (!function)
1250 function = cnode;
1251 else if (function != cnode)
1252 function = BOTTOM;
1254 else
1255 function = meet (function, dyn_cast <varpool_node *> (ref->referring),
1256 single_user_map);
1258 return function;
1261 /* Pass setting used_by_single_function flag.
1262 This flag is set on variable when there is only one function that may
1263 possibly referr to it. */
1265 static unsigned int
1266 ipa_single_use (void)
1268 varpool_node *first = (varpool_node *) (void *) 1;
1269 varpool_node *var;
1270 hash_map<varpool_node *, cgraph_node *> single_user_map;
1272 FOR_EACH_DEFINED_VARIABLE (var)
1273 if (!var->all_refs_explicit_p ())
1274 var->aux = BOTTOM;
1275 else
1277 /* Enqueue symbol for dataflow. */
1278 var->aux = first;
1279 first = var;
1282 /* The actual dataflow. */
1284 while (first != (void *) 1)
1286 cgraph_node *user, *orig_user, **f;
1288 var = first;
1289 first = (varpool_node *)first->aux;
1291 f = single_user_map.get (var);
1292 if (f)
1293 orig_user = *f;
1294 else
1295 orig_user = NULL;
1296 user = propagate_single_user (var, orig_user, single_user_map);
1298 gcc_checking_assert (var->aux != BOTTOM);
1300 /* If user differs, enqueue all references. */
1301 if (user != orig_user)
1303 unsigned int i;
1304 ipa_ref *ref;
1306 single_user_map.put (var, user);
1308 /* Enqueue all aliases for re-processing. */
1309 for (i = 0; var->iterate_direct_aliases (i, ref); i++)
1310 if (!ref->referring->aux)
1312 ref->referring->aux = first;
1313 first = dyn_cast <varpool_node *> (ref->referring);
1315 /* Enqueue all users for re-processing. */
1316 for (i = 0; var->iterate_reference (i, ref); i++)
1317 if (!ref->referred->aux
1318 && ref->referred->definition
1319 && is_a <varpool_node *> (ref->referred))
1321 ref->referred->aux = first;
1322 first = dyn_cast <varpool_node *> (ref->referred);
1325 /* If user is BOTTOM, just punt on this var. */
1326 if (user == BOTTOM)
1327 var->aux = BOTTOM;
1328 else
1329 var->aux = NULL;
1331 else
1332 var->aux = NULL;
1335 FOR_EACH_DEFINED_VARIABLE (var)
1337 if (var->aux != BOTTOM)
1339 /* Not having the single user known means that the VAR is
1340 unreachable. Either someone forgot to remove unreachable
1341 variables or the reachability here is wrong. */
1343 gcc_checking_assert (single_user_map.get (var));
1345 if (dump_file)
1347 fprintf (dump_file, "Variable %s is used by single function\n",
1348 var->dump_name ());
1350 var->used_by_single_function = true;
1352 var->aux = NULL;
1354 return 0;
1357 namespace {
1359 const pass_data pass_data_ipa_single_use =
1361 IPA_PASS, /* type */
1362 "single-use", /* name */
1363 OPTGROUP_NONE, /* optinfo_flags */
1364 TV_CGRAPHOPT, /* tv_id */
1365 0, /* properties_required */
1366 0, /* properties_provided */
1367 0, /* properties_destroyed */
1368 0, /* todo_flags_start */
1369 0, /* todo_flags_finish */
1372 class pass_ipa_single_use : public ipa_opt_pass_d
1374 public:
1375 pass_ipa_single_use (gcc::context *ctxt)
1376 : ipa_opt_pass_d (pass_data_ipa_single_use, ctxt,
1377 NULL, /* generate_summary */
1378 NULL, /* write_summary */
1379 NULL, /* read_summary */
1380 NULL, /* write_optimization_summary */
1381 NULL, /* read_optimization_summary */
1382 NULL, /* stmt_fixup */
1383 0, /* function_transform_todo_flags_start */
1384 NULL, /* function_transform */
1385 NULL) /* variable_transform */
1388 /* opt_pass methods: */
1389 virtual unsigned int execute (function *) { return ipa_single_use (); }
1391 }; // class pass_ipa_single_use
1393 } // anon namespace
1395 ipa_opt_pass_d *
1396 make_pass_ipa_single_use (gcc::context *ctxt)
1398 return new pass_ipa_single_use (ctxt);
1401 /* Materialize all clones. */
1403 namespace {
1405 const pass_data pass_data_materialize_all_clones =
1407 SIMPLE_IPA_PASS, /* type */
1408 "materialize-all-clones", /* name */
1409 OPTGROUP_NONE, /* optinfo_flags */
1410 TV_IPA_OPT, /* tv_id */
1411 0, /* properties_required */
1412 0, /* properties_provided */
1413 0, /* properties_destroyed */
1414 0, /* todo_flags_start */
1415 0, /* todo_flags_finish */
1418 class pass_materialize_all_clones : public simple_ipa_opt_pass
1420 public:
1421 pass_materialize_all_clones (gcc::context *ctxt)
1422 : simple_ipa_opt_pass (pass_data_materialize_all_clones, ctxt)
1425 /* opt_pass methods: */
1426 virtual unsigned int execute (function *)
1428 symtab->materialize_all_clones ();
1429 return 0;
1432 }; // class pass_materialize_all_clones
1434 } // anon namespace
1436 simple_ipa_opt_pass *
1437 make_pass_materialize_all_clones (gcc::context *ctxt)
1439 return new pass_materialize_all_clones (ctxt);