Fix bootstrap/PR63632
[official-gcc.git] / gcc / ipa.c
blobc8d7ab918da4efc85265b6f681a128a3f0c809da
1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003-2014 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 "tm.h"
24 #include "tree.h"
25 #include "calls.h"
26 #include "stringpool.h"
27 #include "cgraph.h"
28 #include "tree-pass.h"
29 #include "hash-map.h"
30 #include "hash-set.h"
31 #include "gimple-expr.h"
32 #include "gimplify.h"
33 #include "flags.h"
34 #include "target.h"
35 #include "tree-iterator.h"
36 #include "ipa-utils.h"
37 #include "ipa-inline.h"
38 #include "tree-inline.h"
39 #include "profile.h"
40 #include "params.h"
41 #include "internal-fn.h"
42 #include "tree-ssa-alias.h"
43 #include "gimple.h"
44 #include "dbgcnt.h"
47 /* Return true when NODE has ADDR reference. */
49 static bool
50 has_addr_references_p (struct cgraph_node *node,
51 void *data ATTRIBUTE_UNUSED)
53 int i;
54 struct ipa_ref *ref = NULL;
56 for (i = 0; node->iterate_referring (i, ref); i++)
57 if (ref->use == IPA_REF_ADDR)
58 return true;
59 return false;
62 /* Look for all functions inlined to NODE and update their inlined_to pointers
63 to INLINED_TO. */
65 static void
66 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
68 struct cgraph_edge *e;
69 for (e = node->callees; e; e = e->next_callee)
70 if (e->callee->global.inlined_to)
72 e->callee->global.inlined_to = inlined_to;
73 update_inlined_to_pointer (e->callee, inlined_to);
77 /* Add symtab NODE to queue starting at FIRST.
79 The queue is linked via AUX pointers and terminated by pointer to 1.
80 We enqueue nodes at two occasions: when we find them reachable or when we find
81 their bodies needed for further clonning. In the second case we mark them
82 by pointer to 2 after processing so they are re-queue when they become
83 reachable. */
85 static void
86 enqueue_node (symtab_node *node, symtab_node **first,
87 hash_set<symtab_node *> *reachable)
89 /* Node is still in queue; do nothing. */
90 if (node->aux && node->aux != (void *) 2)
91 return;
92 /* Node was already processed as unreachable, re-enqueue
93 only if it became reachable now. */
94 if (node->aux == (void *)2 && !reachable->contains (node))
95 return;
96 node->aux = *first;
97 *first = node;
100 /* Process references. */
102 static void
103 process_references (symtab_node *snode,
104 symtab_node **first,
105 bool before_inlining_p,
106 hash_set<symtab_node *> *reachable)
108 int i;
109 struct ipa_ref *ref = NULL;
110 for (i = 0; snode->iterate_reference (i, ref); i++)
112 symtab_node *node = ref->referred;
114 if (node->definition && !node->in_other_partition
115 && ((!DECL_EXTERNAL (node->decl) || node->alias)
116 || (((before_inlining_p
117 && (symtab->state < IPA_SSA
118 || !lookup_attribute ("always_inline",
119 DECL_ATTRIBUTES (node->decl)))))
120 /* We use variable constructors during late complation for
121 constant folding. Keep references alive so partitioning
122 knows about potential references. */
123 || (TREE_CODE (node->decl) == VAR_DECL
124 && flag_wpa
125 && ctor_for_folding (node->decl)
126 != error_mark_node))))
127 reachable->add (node);
128 enqueue_node (node, first, reachable);
132 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
133 all its potential targets as reachable to permit later inlining if
134 devirtualization happens. After inlining still keep their declarations
135 around, so we can devirtualize to a direct call.
137 Also try to make trivial devirutalization when no or only one target is
138 possible. */
140 static void
141 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
142 struct cgraph_edge *edge,
143 symtab_node **first,
144 hash_set<symtab_node *> *reachable,
145 bool before_inlining_p)
147 unsigned int i;
148 void *cache_token;
149 bool final;
150 vec <cgraph_node *>targets
151 = possible_polymorphic_call_targets
152 (edge, &final, &cache_token);
154 if (!reachable_call_targets->add (cache_token))
156 for (i = 0; i < targets.length (); i++)
158 struct cgraph_node *n = targets[i];
160 /* Do not bother to mark virtual methods in anonymous namespace;
161 either we will find use of virtual table defining it, or it is
162 unused. */
163 if (TREE_CODE (TREE_TYPE (n->decl)) == METHOD_TYPE
164 && type_in_anonymous_namespace_p
165 (method_class_type (TREE_TYPE (n->decl))))
166 continue;
168 /* Prior inlining, keep alive bodies of possible targets for
169 devirtualization. */
170 if (n->definition
171 && (before_inlining_p
172 && (symtab->state < IPA_SSA
173 || !lookup_attribute ("always_inline",
174 DECL_ATTRIBUTES (n->decl)))))
175 reachable->add (n);
177 /* Even after inlining we want to keep the possible targets in the
178 boundary, so late passes can still produce direct call even if
179 the chance for inlining is lost. */
180 enqueue_node (n, first, reachable);
184 /* Very trivial devirtualization; when the type is
185 final or anonymous (so we know all its derivation)
186 and there is only one possible virtual call target,
187 make the edge direct. */
188 if (final)
190 if (targets.length () <= 1 && dbg_cnt (devirt))
192 cgraph_node *target, *node = edge->caller;
193 if (targets.length () == 1)
194 target = targets[0];
195 else
196 target = cgraph_node::get_create
197 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
199 if (dump_enabled_p ())
201 location_t locus;
202 if (edge->call_stmt)
203 locus = gimple_location (edge->call_stmt);
204 else
205 locus = UNKNOWN_LOCATION;
206 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
207 "devirtualizing call in %s/%i to %s/%i\n",
208 edge->caller->name (), edge->caller->order,
209 target->name (),
210 target->order);
212 edge = edge->make_direct (target);
213 if (inline_summary_vec)
214 inline_update_overall_summary (node);
215 else if (edge->call_stmt)
216 edge->redirect_call_stmt_to_callee ();
221 /* Perform reachability analysis and reclaim all unreachable nodes.
223 The algorithm is basically mark&sweep but with some extra refinements:
225 - reachable extern inline functions needs special handling; the bodies needs
226 to stay in memory until inlining in hope that they will be inlined.
227 After inlining we release their bodies and turn them into unanalyzed
228 nodes even when they are reachable.
230 BEFORE_INLINING_P specify whether we are before or after inlining.
232 - virtual functions are kept in callgraph even if they seem unreachable in
233 hope calls to them will be devirtualized.
235 Again we remove them after inlining. In late optimization some
236 devirtualization may happen, but it is not important since we won't inline
237 the call. In theory early opts and IPA should work out all important cases.
239 - virtual clones needs bodies of their origins for later materialization;
240 this means that we want to keep the body even if the origin is unreachable
241 otherwise. To avoid origin from sitting in the callgraph and being
242 walked by IPA passes, we turn them into unanalyzed nodes with body
243 defined.
245 We maintain set of function declaration where body needs to stay in
246 body_needed_for_clonning
248 Inline clones represent special case: their declaration match the
249 declaration of origin and cgraph_remove_node already knows how to
250 reshape callgraph and preserve body when offline copy of function or
251 inline clone is being removed.
253 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
254 variables with DECL_INITIAL set. We finalize these and keep reachable
255 ones around for constant folding purposes. After inlining we however
256 stop walking their references to let everything static referneced by them
257 to be removed when it is otherwise unreachable.
259 We maintain queue of both reachable symbols (i.e. defined symbols that needs
260 to stay) and symbols that are in boundary (i.e. external symbols referenced
261 by reachable symbols or origins of clones). The queue is represented
262 as linked list by AUX pointer terminated by 1.
264 At the end we keep all reachable symbols. For symbols in boundary we always
265 turn definition into a declaration, but we may keep function body around
266 based on body_needed_for_clonning
268 All symbols that enter the queue have AUX pointer non-zero and are in the
269 boundary. Pointer set REACHABLE is used to track reachable symbols.
271 Every symbol can be visited twice - once as part of boundary and once
272 as real reachable symbol. enqueue_node needs to decide whether the
273 node needs to be re-queued for second processing. For this purpose
274 we set AUX pointer of processed symbols in the boundary to constant 2. */
276 bool
277 symbol_table::remove_unreachable_nodes (bool before_inlining_p, FILE *file)
279 symtab_node *first = (symtab_node *) (void *) 1;
280 struct cgraph_node *node, *next;
281 varpool_node *vnode, *vnext;
282 bool changed = false;
283 hash_set<symtab_node *> reachable;
284 hash_set<tree> body_needed_for_clonning;
285 hash_set<void *> reachable_call_targets;
287 timevar_push (TV_IPA_UNREACHABLE);
288 if (optimize && flag_devirtualize)
289 build_type_inheritance_graph ();
290 if (file)
291 fprintf (file, "\nReclaiming functions:");
292 #ifdef ENABLE_CHECKING
293 FOR_EACH_FUNCTION (node)
294 gcc_assert (!node->aux);
295 FOR_EACH_VARIABLE (vnode)
296 gcc_assert (!vnode->aux);
297 #endif
298 /* Mark functions whose bodies are obviously needed.
299 This is mostly when they can be referenced externally. Inline clones
300 are special since their declarations are shared with master clone and thus
301 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
302 FOR_EACH_FUNCTION (node)
304 node->used_as_abstract_origin = false;
305 if (node->definition
306 && !node->global.inlined_to
307 && !node->in_other_partition
308 && !node->can_remove_if_no_direct_calls_and_refs_p ())
310 gcc_assert (!node->global.inlined_to);
311 reachable.add (node);
312 enqueue_node (node, &first, &reachable);
314 else
315 gcc_assert (!node->aux);
318 /* Mark variables that are obviously needed. */
319 FOR_EACH_DEFINED_VARIABLE (vnode)
320 if (!vnode->can_remove_if_no_refs_p()
321 && !vnode->in_other_partition)
323 reachable.add (vnode);
324 enqueue_node (vnode, &first, &reachable);
327 /* Perform reachability analysis. */
328 while (first != (symtab_node *) (void *) 1)
330 bool in_boundary_p = !reachable.contains (first);
331 symtab_node *node = first;
333 first = (symtab_node *)first->aux;
335 /* If we are processing symbol in boundary, mark its AUX pointer for
336 possible later re-processing in enqueue_node. */
337 if (in_boundary_p)
338 node->aux = (void *)2;
339 else
341 if (TREE_CODE (node->decl) == FUNCTION_DECL
342 && DECL_ABSTRACT_ORIGIN (node->decl))
344 struct cgraph_node *origin_node
345 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (node->decl));
346 origin_node->used_as_abstract_origin = true;
347 enqueue_node (origin_node, &first, &reachable);
349 /* If any symbol in a comdat group is reachable, force
350 all externally visible symbols in the same comdat
351 group to be reachable as well. Comdat-local symbols
352 can be discarded if all uses were inlined. */
353 if (node->same_comdat_group)
355 symtab_node *next;
356 for (next = node->same_comdat_group;
357 next != node;
358 next = next->same_comdat_group)
359 if (!next->comdat_local_p ()
360 && !reachable.add (next))
361 enqueue_node (next, &first, &reachable);
363 /* Mark references as reachable. */
364 process_references (node, &first, before_inlining_p, &reachable);
367 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
369 /* Mark the callees reachable unless they are direct calls to extern
370 inline functions we decided to not inline. */
371 if (!in_boundary_p)
373 struct cgraph_edge *e;
374 /* Keep alive possible targets for devirtualization. */
375 if (optimize && flag_devirtualize)
377 struct cgraph_edge *next;
378 for (e = cnode->indirect_calls; e; e = next)
380 next = e->next_callee;
381 if (e->indirect_info->polymorphic)
382 walk_polymorphic_call_targets (&reachable_call_targets,
383 e, &first, &reachable,
384 before_inlining_p);
387 for (e = cnode->callees; e; e = e->next_callee)
389 if (e->callee->definition
390 && !e->callee->in_other_partition
391 && (!e->inline_failed
392 || !DECL_EXTERNAL (e->callee->decl)
393 || e->callee->alias
394 || before_inlining_p))
396 /* Be sure that we will not optimize out alias target
397 body. */
398 if (DECL_EXTERNAL (e->callee->decl)
399 && e->callee->alias
400 && before_inlining_p)
401 reachable.add (e->callee->function_symbol ());
402 reachable.add (e->callee);
404 enqueue_node (e->callee, &first, &reachable);
407 /* When inline clone exists, mark body to be preserved so when removing
408 offline copy of the function we don't kill it. */
409 if (cnode->global.inlined_to)
410 body_needed_for_clonning.add (cnode->decl);
412 /* For non-inline clones, force their origins to the boundary and ensure
413 that body is not removed. */
414 while (cnode->clone_of)
416 bool noninline = cnode->clone_of->decl != cnode->decl;
417 cnode = cnode->clone_of;
418 if (noninline)
420 body_needed_for_clonning.add (cnode->decl);
421 enqueue_node (cnode, &first, &reachable);
426 /* If any reachable function has simd clones, mark them as
427 reachable as well. */
428 if (cnode->simd_clones)
430 cgraph_node *next;
431 for (next = cnode->simd_clones;
432 next;
433 next = next->simdclone->next_clone)
434 if (in_boundary_p
435 || !reachable.add (next))
436 enqueue_node (next, &first, &reachable);
439 /* When we see constructor of external variable, keep referred nodes in the
440 boundary. This will also hold initializers of the external vars NODE
441 refers to. */
442 varpool_node *vnode = dyn_cast <varpool_node *> (node);
443 if (vnode
444 && DECL_EXTERNAL (node->decl)
445 && !vnode->alias
446 && in_boundary_p)
448 struct ipa_ref *ref = NULL;
449 for (int i = 0; node->iterate_reference (i, ref); i++)
450 enqueue_node (ref->referred, &first, &reachable);
454 /* Remove unreachable functions. */
455 for (node = first_function (); node; node = next)
457 next = next_function (node);
459 /* If node is not needed at all, remove it. */
460 if (!node->aux)
462 if (file)
463 fprintf (file, " %s/%i", node->name (), node->order);
464 node->remove ();
465 changed = true;
467 /* If node is unreachable, remove its body. */
468 else if (!reachable.contains (node))
470 if (!body_needed_for_clonning.contains (node->decl))
471 node->release_body ();
472 else if (!node->clone_of)
473 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
474 if (node->definition)
476 if (file)
477 fprintf (file, " %s/%i", node->name (), node->order);
478 node->body_removed = true;
479 node->analyzed = false;
480 node->definition = false;
481 node->cpp_implicit_alias = false;
482 node->alias = false;
483 node->thunk.thunk_p = false;
484 node->weakref = false;
485 /* After early inlining we drop always_inline attributes on
486 bodies of functions that are still referenced (have their
487 address taken). */
488 DECL_ATTRIBUTES (node->decl)
489 = remove_attribute ("always_inline",
490 DECL_ATTRIBUTES (node->decl));
491 if (!node->in_other_partition)
492 node->local.local = false;
493 node->remove_callees ();
494 node->remove_from_same_comdat_group ();
495 node->remove_all_references ();
496 changed = true;
499 else
500 gcc_assert (node->clone_of || !node->has_gimple_body_p ()
501 || in_lto_p || DECL_RESULT (node->decl));
504 /* Inline clones might be kept around so their materializing allows further
505 cloning. If the function the clone is inlined into is removed, we need
506 to turn it into normal cone. */
507 FOR_EACH_FUNCTION (node)
509 if (node->global.inlined_to
510 && !node->callers)
512 gcc_assert (node->clones);
513 node->global.inlined_to = NULL;
514 update_inlined_to_pointer (node, node);
516 node->aux = NULL;
519 /* Remove unreachable variables. */
520 if (file)
521 fprintf (file, "\nReclaiming variables:");
522 for (vnode = first_variable (); vnode; vnode = vnext)
524 vnext = next_variable (vnode);
525 if (!vnode->aux
526 /* For can_refer_decl_in_current_unit_p we want to track for
527 all external variables if they are defined in other partition
528 or not. */
529 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
531 if (file)
532 fprintf (file, " %s/%i", vnode->name (), vnode->order);
533 vnode->remove ();
534 changed = true;
536 else if (!reachable.contains (vnode))
538 tree init;
539 if (vnode->definition)
541 if (file)
542 fprintf (file, " %s", vnode->name ());
543 changed = true;
545 /* Keep body if it may be useful for constant folding. */
546 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node)
547 vnode->remove_initializer ();
548 else
549 DECL_INITIAL (vnode->decl) = init;
550 vnode->body_removed = true;
551 vnode->definition = false;
552 vnode->analyzed = false;
553 vnode->aux = NULL;
555 vnode->remove_from_same_comdat_group ();
557 vnode->remove_all_references ();
559 else
560 vnode->aux = NULL;
563 /* Now update address_taken flags and try to promote functions to be local. */
564 if (file)
565 fprintf (file, "\nClearing address taken flags:");
566 FOR_EACH_DEFINED_FUNCTION (node)
567 if (node->address_taken
568 && !node->used_from_other_partition)
570 if (!node->call_for_symbol_thunks_and_aliases
571 (has_addr_references_p, NULL, true))
573 if (file)
574 fprintf (file, " %s", node->name ());
575 node->address_taken = false;
576 changed = true;
577 if (node->local_p ())
579 node->local.local = true;
580 if (file)
581 fprintf (file, " (local)");
585 if (file)
586 fprintf (file, "\n");
588 #ifdef ENABLE_CHECKING
589 symtab_node::verify_symtab_nodes ();
590 #endif
592 /* If we removed something, perhaps profile could be improved. */
593 if (changed && optimize && inline_edge_summary_vec.exists ())
594 FOR_EACH_DEFINED_FUNCTION (node)
595 ipa_propagate_frequency (node);
597 timevar_pop (TV_IPA_UNREACHABLE);
598 return changed;
601 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
602 as needed, also clear EXPLICIT_REFS if the references to given variable
603 do not need to be explicit. */
605 void
606 process_references (varpool_node *vnode,
607 bool *written, bool *address_taken,
608 bool *read, bool *explicit_refs)
610 int i;
611 struct ipa_ref *ref;
613 if (!vnode->all_refs_explicit_p ()
614 || TREE_THIS_VOLATILE (vnode->decl))
615 *explicit_refs = false;
617 for (i = 0; vnode->iterate_referring (i, ref)
618 && *explicit_refs && (!*written || !*address_taken || !*read); i++)
619 switch (ref->use)
621 case IPA_REF_ADDR:
622 *address_taken = true;
623 break;
624 case IPA_REF_LOAD:
625 *read = true;
626 break;
627 case IPA_REF_STORE:
628 *written = true;
629 break;
630 case IPA_REF_ALIAS:
631 process_references (dyn_cast<varpool_node *> (ref->referring), written,
632 address_taken, read, explicit_refs);
633 break;
637 /* Set TREE_READONLY bit. */
639 bool
640 set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
642 TREE_READONLY (vnode->decl) = true;
643 return false;
646 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
648 bool
649 set_writeonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
651 vnode->writeonly = true;
652 if (optimize)
654 DECL_INITIAL (vnode->decl) = NULL;
655 if (!vnode->alias)
656 vnode->remove_all_references ();
658 return false;
661 /* Clear addressale bit of VNODE. */
663 bool
664 clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
666 vnode->address_taken = false;
667 TREE_ADDRESSABLE (vnode->decl) = 0;
668 return false;
671 /* Discover variables that have no longer address taken or that are read only
672 and update their flags.
674 FIXME: This can not be done in between gimplify and omp_expand since
675 readonly flag plays role on what is shared and what is not. Currently we do
676 this transformation as part of whole program visibility and re-do at
677 ipa-reference pass (to take into account clonning), but it would
678 make sense to do it before early optimizations. */
680 void
681 ipa_discover_readonly_nonaddressable_vars (void)
683 varpool_node *vnode;
684 if (dump_file)
685 fprintf (dump_file, "Clearing variable flags:");
686 FOR_EACH_VARIABLE (vnode)
687 if (!vnode->alias
688 && (TREE_ADDRESSABLE (vnode->decl)
689 || !vnode->writeonly
690 || !TREE_READONLY (vnode->decl)))
692 bool written = false;
693 bool address_taken = false;
694 bool read = false;
695 bool explicit_refs = true;
697 process_references (vnode, &written, &address_taken, &read, &explicit_refs);
698 if (!explicit_refs)
699 continue;
700 if (!address_taken)
702 if (TREE_ADDRESSABLE (vnode->decl) && dump_file)
703 fprintf (dump_file, " %s (non-addressable)", vnode->name ());
704 vnode->call_for_node_and_aliases (clear_addressable_bit, NULL, true);
706 if (!address_taken && !written
707 /* Making variable in explicit section readonly can cause section
708 type conflict.
709 See e.g. gcc.c-torture/compile/pr23237.c */
710 && vnode->get_section () == NULL)
712 if (!TREE_READONLY (vnode->decl) && dump_file)
713 fprintf (dump_file, " %s (read-only)", vnode->name ());
714 vnode->call_for_node_and_aliases (set_readonly_bit, NULL, true);
716 if (!vnode->writeonly && !read && !address_taken && written)
718 if (dump_file)
719 fprintf (dump_file, " %s (write-only)", vnode->name ());
720 vnode->call_for_node_and_aliases (set_writeonly_bit, NULL, true);
723 if (dump_file)
724 fprintf (dump_file, "\n");
727 /* Free inline summary. */
729 namespace {
731 const pass_data pass_data_ipa_free_inline_summary =
733 SIMPLE_IPA_PASS, /* type */
734 "free-inline-summary", /* name */
735 OPTGROUP_NONE, /* optinfo_flags */
736 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
737 0, /* properties_required */
738 0, /* properties_provided */
739 0, /* properties_destroyed */
740 0, /* todo_flags_start */
741 /* Early optimizations may make function unreachable. We can not
742 remove unreachable functions as part of the ealry opts pass because
743 TODOs are run before subpasses. Do it here. */
744 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
747 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
749 public:
750 pass_ipa_free_inline_summary (gcc::context *ctxt)
751 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
754 /* opt_pass methods: */
755 virtual unsigned int execute (function *)
757 inline_free_summary ();
758 return 0;
761 }; // class pass_ipa_free_inline_summary
763 } // anon namespace
765 simple_ipa_opt_pass *
766 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
768 return new pass_ipa_free_inline_summary (ctxt);
771 /* Generate and emit a static constructor or destructor. WHICH must
772 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
773 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
774 initialization priority for this constructor or destructor.
776 FINAL specify whether the externally visible name for collect2 should
777 be produced. */
779 static void
780 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
782 static int counter = 0;
783 char which_buf[16];
784 tree decl, name, resdecl;
786 /* The priority is encoded in the constructor or destructor name.
787 collect2 will sort the names and arrange that they are called at
788 program startup. */
789 if (final)
790 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
791 else
792 /* Proudce sane name but one not recognizable by collect2, just for the
793 case we fail to inline the function. */
794 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
795 name = get_file_function_name (which_buf);
797 decl = build_decl (input_location, FUNCTION_DECL, name,
798 build_function_type_list (void_type_node, NULL_TREE));
799 current_function_decl = decl;
801 resdecl = build_decl (input_location,
802 RESULT_DECL, NULL_TREE, void_type_node);
803 DECL_ARTIFICIAL (resdecl) = 1;
804 DECL_RESULT (decl) = resdecl;
805 DECL_CONTEXT (resdecl) = decl;
807 allocate_struct_function (decl, false);
809 TREE_STATIC (decl) = 1;
810 TREE_USED (decl) = 1;
811 DECL_ARTIFICIAL (decl) = 1;
812 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
813 DECL_SAVED_TREE (decl) = body;
814 if (!targetm.have_ctors_dtors && final)
816 TREE_PUBLIC (decl) = 1;
817 DECL_PRESERVE_P (decl) = 1;
819 DECL_UNINLINABLE (decl) = 1;
821 DECL_INITIAL (decl) = make_node (BLOCK);
822 TREE_USED (DECL_INITIAL (decl)) = 1;
824 DECL_SOURCE_LOCATION (decl) = input_location;
825 cfun->function_end_locus = input_location;
827 switch (which)
829 case 'I':
830 DECL_STATIC_CONSTRUCTOR (decl) = 1;
831 decl_init_priority_insert (decl, priority);
832 break;
833 case 'D':
834 DECL_STATIC_DESTRUCTOR (decl) = 1;
835 decl_fini_priority_insert (decl, priority);
836 break;
837 default:
838 gcc_unreachable ();
841 gimplify_function_tree (decl);
843 cgraph_node::add_new_function (decl, false);
845 set_cfun (NULL);
846 current_function_decl = NULL;
849 /* Generate and emit a static constructor or destructor. WHICH must
850 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
851 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
852 initialization priority for this constructor or destructor. */
854 void
855 cgraph_build_static_cdtor (char which, tree body, int priority)
857 cgraph_build_static_cdtor_1 (which, body, priority, false);
860 /* A vector of FUNCTION_DECLs declared as static constructors. */
861 static vec<tree> static_ctors;
862 /* A vector of FUNCTION_DECLs declared as static destructors. */
863 static vec<tree> static_dtors;
865 /* When target does not have ctors and dtors, we call all constructor
866 and destructor by special initialization/destruction function
867 recognized by collect2.
869 When we are going to build this function, collect all constructors and
870 destructors and turn them into normal functions. */
872 static void
873 record_cdtor_fn (struct cgraph_node *node)
875 if (DECL_STATIC_CONSTRUCTOR (node->decl))
876 static_ctors.safe_push (node->decl);
877 if (DECL_STATIC_DESTRUCTOR (node->decl))
878 static_dtors.safe_push (node->decl);
879 node = cgraph_node::get (node->decl);
880 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
883 /* Define global constructors/destructor functions for the CDTORS, of
884 which they are LEN. The CDTORS are sorted by initialization
885 priority. If CTOR_P is true, these are constructors; otherwise,
886 they are destructors. */
888 static void
889 build_cdtor (bool ctor_p, vec<tree> cdtors)
891 size_t i,j;
892 size_t len = cdtors.length ();
894 i = 0;
895 while (i < len)
897 tree body;
898 tree fn;
899 priority_type priority;
901 priority = 0;
902 body = NULL_TREE;
903 j = i;
906 priority_type p;
907 fn = cdtors[j];
908 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
909 if (j == i)
910 priority = p;
911 else if (p != priority)
912 break;
913 j++;
915 while (j < len);
917 /* When there is only one cdtor and target supports them, do nothing. */
918 if (j == i + 1
919 && targetm.have_ctors_dtors)
921 i++;
922 continue;
924 /* Find the next batch of constructors/destructors with the same
925 initialization priority. */
926 for (;i < j; i++)
928 tree call;
929 fn = cdtors[i];
930 call = build_call_expr (fn, 0);
931 if (ctor_p)
932 DECL_STATIC_CONSTRUCTOR (fn) = 0;
933 else
934 DECL_STATIC_DESTRUCTOR (fn) = 0;
935 /* We do not want to optimize away pure/const calls here.
936 When optimizing, these should be already removed, when not
937 optimizing, we want user to be able to breakpoint in them. */
938 TREE_SIDE_EFFECTS (call) = 1;
939 append_to_statement_list (call, &body);
941 gcc_assert (body != NULL_TREE);
942 /* Generate a function to call all the function of like
943 priority. */
944 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
948 /* Comparison function for qsort. P1 and P2 are actually of type
949 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
950 used to determine the sort order. */
952 static int
953 compare_ctor (const void *p1, const void *p2)
955 tree f1;
956 tree f2;
957 int priority1;
958 int priority2;
960 f1 = *(const tree *)p1;
961 f2 = *(const tree *)p2;
962 priority1 = DECL_INIT_PRIORITY (f1);
963 priority2 = DECL_INIT_PRIORITY (f2);
965 if (priority1 < priority2)
966 return -1;
967 else if (priority1 > priority2)
968 return 1;
969 else
970 /* Ensure a stable sort. Constructors are executed in backwarding
971 order to make LTO initialize braries first. */
972 return DECL_UID (f2) - DECL_UID (f1);
975 /* Comparison function for qsort. P1 and P2 are actually of type
976 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
977 used to determine the sort order. */
979 static int
980 compare_dtor (const void *p1, const void *p2)
982 tree f1;
983 tree f2;
984 int priority1;
985 int priority2;
987 f1 = *(const tree *)p1;
988 f2 = *(const tree *)p2;
989 priority1 = DECL_FINI_PRIORITY (f1);
990 priority2 = DECL_FINI_PRIORITY (f2);
992 if (priority1 < priority2)
993 return -1;
994 else if (priority1 > priority2)
995 return 1;
996 else
997 /* Ensure a stable sort. */
998 return DECL_UID (f1) - DECL_UID (f2);
1001 /* Generate functions to call static constructors and destructors
1002 for targets that do not support .ctors/.dtors sections. These
1003 functions have magic names which are detected by collect2. */
1005 static void
1006 build_cdtor_fns (void)
1008 if (!static_ctors.is_empty ())
1010 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1011 static_ctors.qsort (compare_ctor);
1012 build_cdtor (/*ctor_p=*/true, static_ctors);
1015 if (!static_dtors.is_empty ())
1017 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1018 static_dtors.qsort (compare_dtor);
1019 build_cdtor (/*ctor_p=*/false, static_dtors);
1023 /* Look for constructors and destructors and produce function calling them.
1024 This is needed for targets not supporting ctors or dtors, but we perform the
1025 transformation also at linktime to merge possibly numerous
1026 constructors/destructors into single function to improve code locality and
1027 reduce size. */
1029 static unsigned int
1030 ipa_cdtor_merge (void)
1032 struct cgraph_node *node;
1033 FOR_EACH_DEFINED_FUNCTION (node)
1034 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1035 || DECL_STATIC_DESTRUCTOR (node->decl))
1036 record_cdtor_fn (node);
1037 build_cdtor_fns ();
1038 static_ctors.release ();
1039 static_dtors.release ();
1040 return 0;
1043 namespace {
1045 const pass_data pass_data_ipa_cdtor_merge =
1047 IPA_PASS, /* type */
1048 "cdtor", /* name */
1049 OPTGROUP_NONE, /* optinfo_flags */
1050 TV_CGRAPHOPT, /* tv_id */
1051 0, /* properties_required */
1052 0, /* properties_provided */
1053 0, /* properties_destroyed */
1054 0, /* todo_flags_start */
1055 0, /* todo_flags_finish */
1058 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1060 public:
1061 pass_ipa_cdtor_merge (gcc::context *ctxt)
1062 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1063 NULL, /* generate_summary */
1064 NULL, /* write_summary */
1065 NULL, /* read_summary */
1066 NULL, /* write_optimization_summary */
1067 NULL, /* read_optimization_summary */
1068 NULL, /* stmt_fixup */
1069 0, /* function_transform_todo_flags_start */
1070 NULL, /* function_transform */
1071 NULL) /* variable_transform */
1074 /* opt_pass methods: */
1075 virtual bool gate (function *);
1076 virtual unsigned int execute (function *) { return ipa_cdtor_merge (); }
1078 }; // class pass_ipa_cdtor_merge
1080 bool
1081 pass_ipa_cdtor_merge::gate (function *)
1083 /* Perform the pass when we have no ctors/dtors support
1084 or at LTO time to merge multiple constructors into single
1085 function. */
1086 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1089 } // anon namespace
1091 ipa_opt_pass_d *
1092 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1094 return new pass_ipa_cdtor_merge (ctxt);
1097 /* Invalid pointer representing BOTTOM for single user dataflow. */
1098 #define BOTTOM ((cgraph_node *)(size_t) 2)
1100 /* Meet operation for single user dataflow.
1101 Here we want to associate variables with sigle function that may access it.
1103 FUNCTION is current single user of a variable, VAR is variable that uses it.
1104 Latttice is stored in SINGLE_USER_MAP.
1106 We represent:
1107 - TOP by no entry in SIGNLE_USER_MAP
1108 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1109 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1111 cgraph_node *
1112 meet (cgraph_node *function, varpool_node *var,
1113 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1115 struct cgraph_node *user, **f;
1117 if (var->aux == BOTTOM)
1118 return BOTTOM;
1120 f = single_user_map.get (var);
1121 if (!f)
1122 return function;
1123 user = *f;
1124 if (!function)
1125 return user;
1126 else if (function != user)
1127 return BOTTOM;
1128 else
1129 return function;
1132 /* Propagation step of single-use dataflow.
1134 Check all uses of VNODE and see if they are used by single function FUNCTION.
1135 SINGLE_USER_MAP represents the dataflow lattice. */
1137 cgraph_node *
1138 propagate_single_user (varpool_node *vnode, cgraph_node *function,
1139 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1141 int i;
1142 struct ipa_ref *ref;
1144 gcc_assert (!vnode->externally_visible);
1146 /* If node is an alias, first meet with its target. */
1147 if (vnode->alias)
1148 function = meet (function, vnode->get_alias_target (), single_user_map);
1150 /* Check all users and see if they correspond to a single function. */
1151 for (i = 0; vnode->iterate_referring (i, ref) && function != BOTTOM; i++)
1153 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
1154 if (cnode)
1156 if (cnode->global.inlined_to)
1157 cnode = cnode->global.inlined_to;
1158 if (!function)
1159 function = cnode;
1160 else if (function != cnode)
1161 function = BOTTOM;
1163 else
1164 function = meet (function, dyn_cast <varpool_node *> (ref->referring), single_user_map);
1166 return function;
1169 /* Pass setting used_by_single_function flag.
1170 This flag is set on variable when there is only one function that may possibly
1171 referr to it. */
1173 static unsigned int
1174 ipa_single_use (void)
1176 varpool_node *first = (varpool_node *) (void *) 1;
1177 varpool_node *var;
1178 hash_map<varpool_node *, cgraph_node *> single_user_map;
1180 FOR_EACH_DEFINED_VARIABLE (var)
1181 if (!var->all_refs_explicit_p ())
1182 var->aux = BOTTOM;
1183 else
1185 /* Enqueue symbol for dataflow. */
1186 var->aux = first;
1187 first = var;
1190 /* The actual dataflow. */
1192 while (first != (void *) 1)
1194 cgraph_node *user, *orig_user, **f;
1196 var = first;
1197 first = (varpool_node *)first->aux;
1199 f = single_user_map.get (var);
1200 if (f)
1201 orig_user = *f;
1202 else
1203 orig_user = NULL;
1204 user = propagate_single_user (var, orig_user, single_user_map);
1206 gcc_checking_assert (var->aux != BOTTOM);
1208 /* If user differs, enqueue all references. */
1209 if (user != orig_user)
1211 unsigned int i;
1212 ipa_ref *ref;
1214 single_user_map.put (var, user);
1216 /* Enqueue all aliases for re-processing. */
1217 for (i = 0; var->iterate_referring (i, ref); i++)
1218 if (ref->use == IPA_REF_ALIAS
1219 && !ref->referring->aux)
1221 ref->referring->aux = first;
1222 first = dyn_cast <varpool_node *> (ref->referring);
1224 /* Enqueue all users for re-processing. */
1225 for (i = 0; var->iterate_reference (i, ref); i++)
1226 if (!ref->referred->aux
1227 && ref->referred->definition
1228 && is_a <varpool_node *> (ref->referred))
1230 ref->referred->aux = first;
1231 first = dyn_cast <varpool_node *> (ref->referred);
1234 /* If user is BOTTOM, just punt on this var. */
1235 if (user == BOTTOM)
1236 var->aux = BOTTOM;
1237 else
1238 var->aux = NULL;
1240 else
1241 var->aux = NULL;
1244 FOR_EACH_DEFINED_VARIABLE (var)
1246 if (var->aux != BOTTOM)
1248 #ifdef ENABLE_CHECKING
1249 if (!single_user_map.get (var))
1250 gcc_assert (single_user_map.get (var));
1251 #endif
1252 if (dump_file)
1254 fprintf (dump_file, "Variable %s/%i is used by single function\n",
1255 var->name (), var->order);
1257 var->used_by_single_function = true;
1259 var->aux = NULL;
1261 return 0;
1264 namespace {
1266 const pass_data pass_data_ipa_single_use =
1268 IPA_PASS, /* type */
1269 "single-use", /* name */
1270 OPTGROUP_NONE, /* optinfo_flags */
1271 TV_CGRAPHOPT, /* tv_id */
1272 0, /* properties_required */
1273 0, /* properties_provided */
1274 0, /* properties_destroyed */
1275 0, /* todo_flags_start */
1276 0, /* todo_flags_finish */
1279 class pass_ipa_single_use : public ipa_opt_pass_d
1281 public:
1282 pass_ipa_single_use (gcc::context *ctxt)
1283 : ipa_opt_pass_d (pass_data_ipa_single_use, ctxt,
1284 NULL, /* generate_summary */
1285 NULL, /* write_summary */
1286 NULL, /* read_summary */
1287 NULL, /* write_optimization_summary */
1288 NULL, /* read_optimization_summary */
1289 NULL, /* stmt_fixup */
1290 0, /* function_transform_todo_flags_start */
1291 NULL, /* function_transform */
1292 NULL) /* variable_transform */
1295 /* opt_pass methods: */
1296 virtual bool gate (function *);
1297 virtual unsigned int execute (function *) { return ipa_single_use (); }
1299 }; // class pass_ipa_single_use
1301 bool
1302 pass_ipa_single_use::gate (function *)
1304 return optimize;
1307 } // anon namespace
1309 ipa_opt_pass_d *
1310 make_pass_ipa_single_use (gcc::context *ctxt)
1312 return new pass_ipa_single_use (ctxt);