2016-07-28 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / ipa.c
blob6f4693fd59ad647c198968ff12a4440951c10d14
1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003-2016 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 "ipa-prop.h"
36 #include "ipa-inline.h"
37 #include "dbgcnt.h"
40 /* Return true when NODE has ADDR reference. */
42 static bool
43 has_addr_references_p (struct cgraph_node *node,
44 void *)
46 int i;
47 struct ipa_ref *ref = NULL;
49 for (i = 0; node->iterate_referring (i, ref); i++)
50 if (ref->use == IPA_REF_ADDR)
51 return true;
52 return false;
55 /* Return true when NODE can be target of an indirect call. */
57 static bool
58 is_indirect_call_target_p (struct cgraph_node *node, void *)
60 return node->indirect_call_target;
63 /* Look for all functions inlined to NODE and update their inlined_to pointers
64 to INLINED_TO. */
66 static void
67 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
69 struct cgraph_edge *e;
70 for (e = node->callees; e; e = e->next_callee)
71 if (e->callee->global.inlined_to)
73 e->callee->global.inlined_to = inlined_to;
74 update_inlined_to_pointer (e->callee, inlined_to);
78 /* Add symtab NODE to queue starting at FIRST.
80 The queue is linked via AUX pointers and terminated by pointer to 1.
81 We enqueue nodes at two occasions: when we find them reachable or when we find
82 their bodies needed for further clonning. In the second case we mark them
83 by pointer to 2 after processing so they are re-queue when they become
84 reachable. */
86 static void
87 enqueue_node (symtab_node *node, symtab_node **first,
88 hash_set<symtab_node *> *reachable)
90 /* Node is still in queue; do nothing. */
91 if (node->aux && node->aux != (void *) 2)
92 return;
93 /* Node was already processed as unreachable, re-enqueue
94 only if it became reachable now. */
95 if (node->aux == (void *)2 && !reachable->contains (node))
96 return;
97 node->aux = *first;
98 *first = node;
101 /* Process references. */
103 static void
104 process_references (symtab_node *snode,
105 symtab_node **first,
106 bool before_inlining_p,
107 hash_set<symtab_node *> *reachable)
109 int i;
110 struct ipa_ref *ref = NULL;
111 for (i = 0; snode->iterate_reference (i, ref); i++)
113 symtab_node *node = ref->referred;
114 symtab_node *body = node->ultimate_alias_target ();
116 if (node->definition && !node->in_other_partition
117 && ((!DECL_EXTERNAL (node->decl) || node->alias)
118 || (((before_inlining_p
119 && ((TREE_CODE (node->decl) != FUNCTION_DECL
120 && optimize)
121 || (TREE_CODE (node->decl) == FUNCTION_DECL
122 && opt_for_fn (body->decl, optimize))
123 || (symtab->state < IPA_SSA
124 && lookup_attribute
125 ("always_inline",
126 DECL_ATTRIBUTES (body->decl))))))
127 /* We use variable constructors during late compilation for
128 constant folding. Keep references alive so partitioning
129 knows about potential references. */
130 || (TREE_CODE (node->decl) == VAR_DECL
131 && flag_wpa
132 && ctor_for_folding (node->decl)
133 != error_mark_node))))
135 /* Be sure that we will not optimize out alias target
136 body. */
137 if (DECL_EXTERNAL (node->decl)
138 && node->alias
139 && before_inlining_p)
140 reachable->add (body);
141 reachable->add (node);
143 enqueue_node (node, first, reachable);
147 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
148 all its potential targets as reachable to permit later inlining if
149 devirtualization happens. After inlining still keep their declarations
150 around, so we can devirtualize to a direct call.
152 Also try to make trivial devirutalization when no or only one target is
153 possible. */
155 static void
156 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
157 struct cgraph_edge *edge,
158 symtab_node **first,
159 hash_set<symtab_node *> *reachable,
160 bool before_inlining_p)
162 unsigned int i;
163 void *cache_token;
164 bool final;
165 vec <cgraph_node *>targets
166 = possible_polymorphic_call_targets
167 (edge, &final, &cache_token);
169 if (!reachable_call_targets->add (cache_token))
171 for (i = 0; i < targets.length (); i++)
173 struct cgraph_node *n = targets[i];
175 /* Do not bother to mark virtual methods in anonymous namespace;
176 either we will find use of virtual table defining it, or it is
177 unused. */
178 if (TREE_CODE (TREE_TYPE (n->decl)) == METHOD_TYPE
179 && type_in_anonymous_namespace_p
180 (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl))))
181 continue;
183 n->indirect_call_target = true;
184 symtab_node *body = n->function_symbol ();
186 /* Prior inlining, keep alive bodies of possible targets for
187 devirtualization. */
188 if (n->definition
189 && (before_inlining_p
190 && opt_for_fn (body->decl, optimize)
191 && opt_for_fn (body->decl, flag_devirtualize)))
193 /* Be sure that we will not optimize out alias target
194 body. */
195 if (DECL_EXTERNAL (n->decl)
196 && n->alias
197 && before_inlining_p)
198 reachable->add (body);
199 reachable->add (n);
201 /* Even after inlining we want to keep the possible targets in the
202 boundary, so late passes can still produce direct call even if
203 the chance for inlining is lost. */
204 enqueue_node (n, first, reachable);
208 /* Very trivial devirtualization; when the type is
209 final or anonymous (so we know all its derivation)
210 and there is only one possible virtual call target,
211 make the edge direct. */
212 if (final)
214 if (targets.length () <= 1 && dbg_cnt (devirt))
216 cgraph_node *target, *node = edge->caller;
217 if (targets.length () == 1)
218 target = targets[0];
219 else
220 target = cgraph_node::get_create
221 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
223 if (dump_enabled_p ())
225 location_t locus;
226 if (edge->call_stmt)
227 locus = gimple_location (edge->call_stmt);
228 else
229 locus = UNKNOWN_LOCATION;
230 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
231 "devirtualizing call in %s/%i to %s/%i\n",
232 edge->caller->name (), edge->caller->order,
233 target->name (),
234 target->order);
236 edge = edge->make_direct (target);
237 if (inline_summaries)
238 inline_update_overall_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 ? 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/%i", node->name (), node->order);
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/%i", node->name (), node->order);
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/%i", ref->referred->name (),
618 ref->referred->order);
619 ref->referring->remove ();
621 if (file)
622 fprintf (file, " %s/%i", vnode->name (), vnode->order);
623 vnext = next_variable (vnode);
624 vnode->remove ();
625 changed = true;
627 else if (!reachable.contains (vnode) && !vnode->alias)
629 tree init;
630 if (vnode->definition)
632 if (file)
633 fprintf (file, " %s", vnode->name ());
634 changed = true;
636 /* Keep body if it may be useful for constant folding. */
637 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node
638 && !POINTER_BOUNDS_P (vnode->decl))
639 vnode->remove_initializer ();
640 else
641 DECL_INITIAL (vnode->decl) = init;
642 vnode->body_removed = true;
643 vnode->definition = false;
644 vnode->analyzed = false;
645 vnode->aux = NULL;
647 vnode->remove_from_same_comdat_group ();
649 vnode->remove_all_references ();
651 else
652 vnode->aux = NULL;
655 /* Now update address_taken flags and try to promote functions to be local. */
656 if (file)
657 fprintf (file, "\nClearing address taken flags:");
658 FOR_EACH_DEFINED_FUNCTION (node)
659 if (node->address_taken
660 && !node->used_from_other_partition)
662 if (!node->call_for_symbol_and_aliases
663 (has_addr_references_p, NULL, true)
664 && (!node->instrumentation_clone
665 || !node->instrumented_version
666 || !node->instrumented_version->address_taken))
668 if (file)
669 fprintf (file, " %s", node->name ());
670 node->address_taken = false;
671 changed = true;
672 if (node->local_p ()
673 /* Virtual functions may be kept in cgraph just because
674 of possible later devirtualization. Do not mark them as
675 local too early so we won't optimize them out before
676 we are done with polymorphic call analysis. */
677 && (!before_inlining_p
678 || !node->call_for_symbol_and_aliases
679 (is_indirect_call_target_p, NULL, true)))
681 node->local.local = true;
682 if (file)
683 fprintf (file, " (local)");
687 if (file)
688 fprintf (file, "\n");
690 symtab_node::checking_verify_symtab_nodes ();
692 /* If we removed something, perhaps profile could be improved. */
693 if (changed && optimize && inline_edge_summary_vec.exists ())
694 FOR_EACH_DEFINED_FUNCTION (node)
695 ipa_propagate_frequency (node);
697 timevar_pop (TV_IPA_UNREACHABLE);
698 return changed;
701 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
702 as needed, also clear EXPLICIT_REFS if the references to given variable
703 do not need to be explicit. */
705 void
706 process_references (varpool_node *vnode,
707 bool *written, bool *address_taken,
708 bool *read, bool *explicit_refs)
710 int i;
711 struct ipa_ref *ref;
713 if (!vnode->all_refs_explicit_p ()
714 || TREE_THIS_VOLATILE (vnode->decl))
715 *explicit_refs = false;
717 for (i = 0; vnode->iterate_referring (i, ref)
718 && *explicit_refs && (!*written || !*address_taken || !*read); i++)
719 switch (ref->use)
721 case IPA_REF_ADDR:
722 *address_taken = true;
723 break;
724 case IPA_REF_LOAD:
725 *read = true;
726 break;
727 case IPA_REF_STORE:
728 *written = true;
729 break;
730 case IPA_REF_ALIAS:
731 process_references (dyn_cast<varpool_node *> (ref->referring), written,
732 address_taken, read, explicit_refs);
733 break;
734 case IPA_REF_CHKP:
735 gcc_unreachable ();
739 /* Set TREE_READONLY bit. */
741 bool
742 set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
744 TREE_READONLY (vnode->decl) = true;
745 return false;
748 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
750 bool
751 set_writeonly_bit (varpool_node *vnode, void *data)
753 vnode->writeonly = true;
754 if (optimize)
756 DECL_INITIAL (vnode->decl) = NULL;
757 if (!vnode->alias)
759 if (vnode->num_references ())
760 *(bool *)data = true;
761 vnode->remove_all_references ();
764 return false;
767 /* Clear addressale bit of VNODE. */
769 bool
770 clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
772 vnode->address_taken = false;
773 TREE_ADDRESSABLE (vnode->decl) = 0;
774 return false;
777 /* Discover variables that have no longer address taken or that are read only
778 and update their flags.
780 Return true when unreachable symbol removan should be done.
782 FIXME: This can not be done in between gimplify and omp_expand since
783 readonly flag plays role on what is shared and what is not. Currently we do
784 this transformation as part of whole program visibility and re-do at
785 ipa-reference pass (to take into account clonning), but it would
786 make sense to do it before early optimizations. */
788 bool
789 ipa_discover_readonly_nonaddressable_vars (void)
791 bool remove_p = false;
792 varpool_node *vnode;
793 if (dump_file)
794 fprintf (dump_file, "Clearing variable flags:");
795 FOR_EACH_VARIABLE (vnode)
796 if (!vnode->alias
797 && (TREE_ADDRESSABLE (vnode->decl)
798 || !vnode->writeonly
799 || !TREE_READONLY (vnode->decl)))
801 bool written = false;
802 bool address_taken = false;
803 bool read = false;
804 bool explicit_refs = true;
806 process_references (vnode, &written, &address_taken, &read,
807 &explicit_refs);
808 if (!explicit_refs)
809 continue;
810 if (!address_taken)
812 if (TREE_ADDRESSABLE (vnode->decl) && dump_file)
813 fprintf (dump_file, " %s (non-addressable)", vnode->name ());
814 vnode->call_for_symbol_and_aliases (clear_addressable_bit, NULL,
815 true);
817 if (!address_taken && !written
818 /* Making variable in explicit section readonly can cause section
819 type conflict.
820 See e.g. gcc.c-torture/compile/pr23237.c */
821 && vnode->get_section () == NULL)
823 if (!TREE_READONLY (vnode->decl) && dump_file)
824 fprintf (dump_file, " %s (read-only)", vnode->name ());
825 vnode->call_for_symbol_and_aliases (set_readonly_bit, NULL, true);
827 if (!vnode->writeonly && !read && !address_taken && written)
829 if (dump_file)
830 fprintf (dump_file, " %s (write-only)", vnode->name ());
831 vnode->call_for_symbol_and_aliases (set_writeonly_bit, &remove_p,
832 true);
835 if (dump_file)
836 fprintf (dump_file, "\n");
837 return remove_p;
840 /* Free inline summary. */
842 namespace {
844 const pass_data pass_data_ipa_free_inline_summary =
846 SIMPLE_IPA_PASS, /* type */
847 "free-inline-summary", /* name */
848 OPTGROUP_NONE, /* optinfo_flags */
849 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
850 0, /* properties_required */
851 0, /* properties_provided */
852 0, /* properties_destroyed */
853 0, /* todo_flags_start */
854 /* Early optimizations may make function unreachable. We can not
855 remove unreachable functions as part of the ealry opts pass because
856 TODOs are run before subpasses. Do it here. */
857 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
860 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
862 public:
863 pass_ipa_free_inline_summary (gcc::context *ctxt)
864 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
867 /* opt_pass methods: */
868 virtual unsigned int execute (function *)
870 inline_free_summary ();
871 return 0;
874 }; // class pass_ipa_free_inline_summary
876 } // anon namespace
878 simple_ipa_opt_pass *
879 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
881 return new pass_ipa_free_inline_summary (ctxt);
884 /* Generate and emit a static constructor or destructor. WHICH must
885 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
886 (for chp static vars constructor) or 'B' (for chkp static bounds
887 constructor). BODY is a STATEMENT_LIST containing GENERIC
888 statements. PRIORITY is the initialization priority for this
889 constructor or destructor.
891 FINAL specify whether the externally visible name for collect2 should
892 be produced. */
894 static void
895 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
897 static int counter = 0;
898 char which_buf[16];
899 tree decl, name, resdecl;
901 /* The priority is encoded in the constructor or destructor name.
902 collect2 will sort the names and arrange that they are called at
903 program startup. */
904 if (final)
905 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
906 else
907 /* Proudce sane name but one not recognizable by collect2, just for the
908 case we fail to inline the function. */
909 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
910 name = get_file_function_name (which_buf);
912 decl = build_decl (input_location, FUNCTION_DECL, name,
913 build_function_type_list (void_type_node, NULL_TREE));
914 current_function_decl = decl;
916 resdecl = build_decl (input_location,
917 RESULT_DECL, NULL_TREE, void_type_node);
918 DECL_ARTIFICIAL (resdecl) = 1;
919 DECL_RESULT (decl) = resdecl;
920 DECL_CONTEXT (resdecl) = decl;
922 allocate_struct_function (decl, false);
924 TREE_STATIC (decl) = 1;
925 TREE_USED (decl) = 1;
926 DECL_ARTIFICIAL (decl) = 1;
927 DECL_IGNORED_P (decl) = 1;
928 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
929 DECL_SAVED_TREE (decl) = body;
930 if (!targetm.have_ctors_dtors && final)
932 TREE_PUBLIC (decl) = 1;
933 DECL_PRESERVE_P (decl) = 1;
935 DECL_UNINLINABLE (decl) = 1;
937 DECL_INITIAL (decl) = make_node (BLOCK);
938 BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl;
939 TREE_USED (DECL_INITIAL (decl)) = 1;
941 DECL_SOURCE_LOCATION (decl) = input_location;
942 cfun->function_end_locus = input_location;
944 switch (which)
946 case 'I':
947 DECL_STATIC_CONSTRUCTOR (decl) = 1;
948 decl_init_priority_insert (decl, priority);
949 break;
950 case 'P':
951 DECL_STATIC_CONSTRUCTOR (decl) = 1;
952 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("chkp ctor"),
953 NULL,
954 NULL_TREE);
955 decl_init_priority_insert (decl, priority);
956 break;
957 case 'B':
958 DECL_STATIC_CONSTRUCTOR (decl) = 1;
959 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("bnd_legacy"),
960 NULL,
961 NULL_TREE);
962 decl_init_priority_insert (decl, priority);
963 break;
964 case 'D':
965 DECL_STATIC_DESTRUCTOR (decl) = 1;
966 decl_fini_priority_insert (decl, priority);
967 break;
968 default:
969 gcc_unreachable ();
972 gimplify_function_tree (decl);
974 cgraph_node::add_new_function (decl, false);
976 set_cfun (NULL);
977 current_function_decl = NULL;
980 /* Generate and emit a static constructor or destructor. WHICH must
981 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
982 (for chkp static vars constructor) or 'B' (for chkp static bounds
983 constructor). BODY is a STATEMENT_LIST containing GENERIC
984 statements. PRIORITY is the initialization priority for this
985 constructor or destructor. */
987 void
988 cgraph_build_static_cdtor (char which, tree body, int priority)
990 cgraph_build_static_cdtor_1 (which, body, priority, false);
993 /* When target does not have ctors and dtors, we call all constructor
994 and destructor by special initialization/destruction function
995 recognized by collect2.
997 When we are going to build this function, collect all constructors and
998 destructors and turn them into normal functions. */
1000 static void
1001 record_cdtor_fn (struct cgraph_node *node, vec<tree> *ctors, vec<tree> *dtors)
1003 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1004 ctors->safe_push (node->decl);
1005 if (DECL_STATIC_DESTRUCTOR (node->decl))
1006 dtors->safe_push (node->decl);
1007 node = cgraph_node::get (node->decl);
1008 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
1011 /* Define global constructors/destructor functions for the CDTORS, of
1012 which they are LEN. The CDTORS are sorted by initialization
1013 priority. If CTOR_P is true, these are constructors; otherwise,
1014 they are destructors. */
1016 static void
1017 build_cdtor (bool ctor_p, const vec<tree> &cdtors)
1019 size_t i,j;
1020 size_t len = cdtors.length ();
1022 i = 0;
1023 while (i < len)
1025 tree body;
1026 tree fn;
1027 priority_type priority;
1029 priority = 0;
1030 body = NULL_TREE;
1031 j = i;
1034 priority_type p;
1035 fn = cdtors[j];
1036 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1037 if (j == i)
1038 priority = p;
1039 else if (p != priority)
1040 break;
1041 j++;
1043 while (j < len);
1045 /* When there is only one cdtor and target supports them, do nothing. */
1046 if (j == i + 1
1047 && targetm.have_ctors_dtors)
1049 i++;
1050 continue;
1052 /* Find the next batch of constructors/destructors with the same
1053 initialization priority. */
1054 for (;i < j; i++)
1056 tree call;
1057 fn = cdtors[i];
1058 call = build_call_expr (fn, 0);
1059 if (ctor_p)
1060 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1061 else
1062 DECL_STATIC_DESTRUCTOR (fn) = 0;
1063 /* We do not want to optimize away pure/const calls here.
1064 When optimizing, these should be already removed, when not
1065 optimizing, we want user to be able to breakpoint in them. */
1066 TREE_SIDE_EFFECTS (call) = 1;
1067 append_to_statement_list (call, &body);
1069 gcc_assert (body != NULL_TREE);
1070 /* Generate a function to call all the function of like
1071 priority. */
1072 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1076 /* Comparison function for qsort. P1 and P2 are actually of type
1077 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1078 used to determine the sort order. */
1080 static int
1081 compare_ctor (const void *p1, const void *p2)
1083 tree f1;
1084 tree f2;
1085 int priority1;
1086 int priority2;
1088 f1 = *(const tree *)p1;
1089 f2 = *(const tree *)p2;
1090 priority1 = DECL_INIT_PRIORITY (f1);
1091 priority2 = DECL_INIT_PRIORITY (f2);
1093 if (priority1 < priority2)
1094 return -1;
1095 else if (priority1 > priority2)
1096 return 1;
1097 else
1098 /* Ensure a stable sort. Constructors are executed in backwarding
1099 order to make LTO initialize braries first. */
1100 return DECL_UID (f2) - DECL_UID (f1);
1103 /* Comparison function for qsort. P1 and P2 are actually of type
1104 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1105 used to determine the sort order. */
1107 static int
1108 compare_dtor (const void *p1, const void *p2)
1110 tree f1;
1111 tree f2;
1112 int priority1;
1113 int priority2;
1115 f1 = *(const tree *)p1;
1116 f2 = *(const tree *)p2;
1117 priority1 = DECL_FINI_PRIORITY (f1);
1118 priority2 = DECL_FINI_PRIORITY (f2);
1120 if (priority1 < priority2)
1121 return -1;
1122 else if (priority1 > priority2)
1123 return 1;
1124 else
1125 /* Ensure a stable sort. */
1126 return DECL_UID (f1) - DECL_UID (f2);
1129 /* Generate functions to call static constructors and destructors
1130 for targets that do not support .ctors/.dtors sections. These
1131 functions have magic names which are detected by collect2. */
1133 static void
1134 build_cdtor_fns (vec<tree> *ctors, vec<tree> *dtors)
1136 if (!ctors->is_empty ())
1138 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1139 ctors->qsort (compare_ctor);
1140 build_cdtor (/*ctor_p=*/true, *ctors);
1143 if (!dtors->is_empty ())
1145 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1146 dtors->qsort (compare_dtor);
1147 build_cdtor (/*ctor_p=*/false, *dtors);
1151 /* Look for constructors and destructors and produce function calling them.
1152 This is needed for targets not supporting ctors or dtors, but we perform the
1153 transformation also at linktime to merge possibly numerous
1154 constructors/destructors into single function to improve code locality and
1155 reduce size. */
1157 static unsigned int
1158 ipa_cdtor_merge (void)
1160 /* A vector of FUNCTION_DECLs declared as static constructors. */
1161 auto_vec<tree, 20> ctors;
1162 /* A vector of FUNCTION_DECLs declared as static destructors. */
1163 auto_vec<tree, 20> dtors;
1164 struct cgraph_node *node;
1165 FOR_EACH_DEFINED_FUNCTION (node)
1166 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1167 || DECL_STATIC_DESTRUCTOR (node->decl))
1168 record_cdtor_fn (node, &ctors, &dtors);
1169 build_cdtor_fns (&ctors, &dtors);
1170 return 0;
1173 namespace {
1175 const pass_data pass_data_ipa_cdtor_merge =
1177 IPA_PASS, /* type */
1178 "cdtor", /* name */
1179 OPTGROUP_NONE, /* optinfo_flags */
1180 TV_CGRAPHOPT, /* tv_id */
1181 0, /* properties_required */
1182 0, /* properties_provided */
1183 0, /* properties_destroyed */
1184 0, /* todo_flags_start */
1185 0, /* todo_flags_finish */
1188 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1190 public:
1191 pass_ipa_cdtor_merge (gcc::context *ctxt)
1192 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1193 NULL, /* generate_summary */
1194 NULL, /* write_summary */
1195 NULL, /* read_summary */
1196 NULL, /* write_optimization_summary */
1197 NULL, /* read_optimization_summary */
1198 NULL, /* stmt_fixup */
1199 0, /* function_transform_todo_flags_start */
1200 NULL, /* function_transform */
1201 NULL) /* variable_transform */
1204 /* opt_pass methods: */
1205 virtual bool gate (function *);
1206 virtual unsigned int execute (function *) { return ipa_cdtor_merge (); }
1208 }; // class pass_ipa_cdtor_merge
1210 bool
1211 pass_ipa_cdtor_merge::gate (function *)
1213 /* Perform the pass when we have no ctors/dtors support
1214 or at LTO time to merge multiple constructors into single
1215 function. */
1216 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1219 } // anon namespace
1221 ipa_opt_pass_d *
1222 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1224 return new pass_ipa_cdtor_merge (ctxt);
1227 /* Invalid pointer representing BOTTOM for single user dataflow. */
1228 #define BOTTOM ((cgraph_node *)(size_t) 2)
1230 /* Meet operation for single user dataflow.
1231 Here we want to associate variables with sigle function that may access it.
1233 FUNCTION is current single user of a variable, VAR is variable that uses it.
1234 Latttice is stored in SINGLE_USER_MAP.
1236 We represent:
1237 - TOP by no entry in SIGNLE_USER_MAP
1238 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1239 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1241 cgraph_node *
1242 meet (cgraph_node *function, varpool_node *var,
1243 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1245 struct cgraph_node *user, **f;
1247 if (var->aux == BOTTOM)
1248 return BOTTOM;
1250 f = single_user_map.get (var);
1251 if (!f)
1252 return function;
1253 user = *f;
1254 if (!function)
1255 return user;
1256 else if (function != user)
1257 return BOTTOM;
1258 else
1259 return function;
1262 /* Propagation step of single-use dataflow.
1264 Check all uses of VNODE and see if they are used by single function FUNCTION.
1265 SINGLE_USER_MAP represents the dataflow lattice. */
1267 cgraph_node *
1268 propagate_single_user (varpool_node *vnode, cgraph_node *function,
1269 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1271 int i;
1272 struct ipa_ref *ref;
1274 gcc_assert (!vnode->externally_visible);
1276 /* If node is an alias, first meet with its target. */
1277 if (vnode->alias)
1278 function = meet (function, vnode->get_alias_target (), single_user_map);
1280 /* Check all users and see if they correspond to a single function. */
1281 for (i = 0; vnode->iterate_referring (i, ref) && function != BOTTOM; i++)
1283 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
1284 if (cnode)
1286 if (cnode->global.inlined_to)
1287 cnode = cnode->global.inlined_to;
1288 if (!function)
1289 function = cnode;
1290 else if (function != cnode)
1291 function = BOTTOM;
1293 else
1294 function = meet (function, dyn_cast <varpool_node *> (ref->referring),
1295 single_user_map);
1297 return function;
1300 /* Pass setting used_by_single_function flag.
1301 This flag is set on variable when there is only one function that may
1302 possibly referr to it. */
1304 static unsigned int
1305 ipa_single_use (void)
1307 varpool_node *first = (varpool_node *) (void *) 1;
1308 varpool_node *var;
1309 hash_map<varpool_node *, cgraph_node *> single_user_map;
1311 FOR_EACH_DEFINED_VARIABLE (var)
1312 if (!var->all_refs_explicit_p ())
1313 var->aux = BOTTOM;
1314 else
1316 /* Enqueue symbol for dataflow. */
1317 var->aux = first;
1318 first = var;
1321 /* The actual dataflow. */
1323 while (first != (void *) 1)
1325 cgraph_node *user, *orig_user, **f;
1327 var = first;
1328 first = (varpool_node *)first->aux;
1330 f = single_user_map.get (var);
1331 if (f)
1332 orig_user = *f;
1333 else
1334 orig_user = NULL;
1335 user = propagate_single_user (var, orig_user, single_user_map);
1337 gcc_checking_assert (var->aux != BOTTOM);
1339 /* If user differs, enqueue all references. */
1340 if (user != orig_user)
1342 unsigned int i;
1343 ipa_ref *ref;
1345 single_user_map.put (var, user);
1347 /* Enqueue all aliases for re-processing. */
1348 for (i = 0; var->iterate_direct_aliases (i, ref); i++)
1349 if (!ref->referring->aux)
1351 ref->referring->aux = first;
1352 first = dyn_cast <varpool_node *> (ref->referring);
1354 /* Enqueue all users for re-processing. */
1355 for (i = 0; var->iterate_reference (i, ref); i++)
1356 if (!ref->referred->aux
1357 && ref->referred->definition
1358 && is_a <varpool_node *> (ref->referred))
1360 ref->referred->aux = first;
1361 first = dyn_cast <varpool_node *> (ref->referred);
1364 /* If user is BOTTOM, just punt on this var. */
1365 if (user == BOTTOM)
1366 var->aux = BOTTOM;
1367 else
1368 var->aux = NULL;
1370 else
1371 var->aux = NULL;
1374 FOR_EACH_DEFINED_VARIABLE (var)
1376 if (var->aux != BOTTOM)
1378 /* Not having the single user known means that the VAR is
1379 unreachable. Either someone forgot to remove unreachable
1380 variables or the reachability here is wrong. */
1382 gcc_checking_assert (single_user_map.get (var));
1384 if (dump_file)
1386 fprintf (dump_file, "Variable %s/%i is used by single function\n",
1387 var->name (), var->order);
1389 var->used_by_single_function = true;
1391 var->aux = NULL;
1393 return 0;
1396 namespace {
1398 const pass_data pass_data_ipa_single_use =
1400 IPA_PASS, /* type */
1401 "single-use", /* name */
1402 OPTGROUP_NONE, /* optinfo_flags */
1403 TV_CGRAPHOPT, /* tv_id */
1404 0, /* properties_required */
1405 0, /* properties_provided */
1406 0, /* properties_destroyed */
1407 0, /* todo_flags_start */
1408 0, /* todo_flags_finish */
1411 class pass_ipa_single_use : public ipa_opt_pass_d
1413 public:
1414 pass_ipa_single_use (gcc::context *ctxt)
1415 : ipa_opt_pass_d (pass_data_ipa_single_use, ctxt,
1416 NULL, /* generate_summary */
1417 NULL, /* write_summary */
1418 NULL, /* read_summary */
1419 NULL, /* write_optimization_summary */
1420 NULL, /* read_optimization_summary */
1421 NULL, /* stmt_fixup */
1422 0, /* function_transform_todo_flags_start */
1423 NULL, /* function_transform */
1424 NULL) /* variable_transform */
1427 /* opt_pass methods: */
1428 virtual bool gate (function *);
1429 virtual unsigned int execute (function *) { return ipa_single_use (); }
1431 }; // class pass_ipa_single_use
1433 bool
1434 pass_ipa_single_use::gate (function *)
1436 return optimize;
1439 } // anon namespace
1441 ipa_opt_pass_d *
1442 make_pass_ipa_single_use (gcc::context *ctxt)
1444 return new pass_ipa_single_use (ctxt);