* config/rl78/rl78-expand.md (umulqihi3): Disable for G10.
[official-gcc.git] / gcc / ipa.c
blob1081e89d94550f426a98ab4f1db2efee7d9cacd9
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 && (cgraph_state < CGRAPH_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 && (cgraph_state < CGRAPH_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 = gimple_location (edge->call_stmt);
202 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
203 "devirtualizing call in %s/%i to %s/%i\n",
204 edge->caller->name (), edge->caller->order,
205 target->name (),
206 target->order);
208 edge = cgraph_make_edge_direct (edge, target);
209 if (inline_summary_vec)
210 inline_update_overall_summary (node);
211 else if (edge->call_stmt)
212 cgraph_redirect_edge_call_stmt_to_callee (edge);
217 /* Perform reachability analysis and reclaim all unreachable nodes.
219 The algorithm is basically mark&sweep but with some extra refinements:
221 - reachable extern inline functions needs special handling; the bodies needs
222 to stay in memory until inlining in hope that they will be inlined.
223 After inlining we release their bodies and turn them into unanalyzed
224 nodes even when they are reachable.
226 BEFORE_INLINING_P specify whether we are before or after inlining.
228 - virtual functions are kept in callgraph even if they seem unreachable in
229 hope calls to them will be devirtualized.
231 Again we remove them after inlining. In late optimization some
232 devirtualization may happen, but it is not important since we won't inline
233 the call. In theory early opts and IPA should work out all important cases.
235 - virtual clones needs bodies of their origins for later materialization;
236 this means that we want to keep the body even if the origin is unreachable
237 otherwise. To avoid origin from sitting in the callgraph and being
238 walked by IPA passes, we turn them into unanalyzed nodes with body
239 defined.
241 We maintain set of function declaration where body needs to stay in
242 body_needed_for_clonning
244 Inline clones represent special case: their declaration match the
245 declaration of origin and cgraph_remove_node already knows how to
246 reshape callgraph and preserve body when offline copy of function or
247 inline clone is being removed.
249 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
250 variables with DECL_INITIAL set. We finalize these and keep reachable
251 ones around for constant folding purposes. After inlining we however
252 stop walking their references to let everything static referneced by them
253 to be removed when it is otherwise unreachable.
255 We maintain queue of both reachable symbols (i.e. defined symbols that needs
256 to stay) and symbols that are in boundary (i.e. external symbols referenced
257 by reachable symbols or origins of clones). The queue is represented
258 as linked list by AUX pointer terminated by 1.
260 At the end we keep all reachable symbols. For symbols in boundary we always
261 turn definition into a declaration, but we may keep function body around
262 based on body_needed_for_clonning
264 All symbols that enter the queue have AUX pointer non-zero and are in the
265 boundary. Pointer set REACHABLE is used to track reachable symbols.
267 Every symbol can be visited twice - once as part of boundary and once
268 as real reachable symbol. enqueue_node needs to decide whether the
269 node needs to be re-queued for second processing. For this purpose
270 we set AUX pointer of processed symbols in the boundary to constant 2. */
272 bool
273 symtab_remove_unreachable_nodes (bool before_inlining_p, FILE *file)
275 symtab_node *first = (symtab_node *) (void *) 1;
276 struct cgraph_node *node, *next;
277 varpool_node *vnode, *vnext;
278 bool changed = false;
279 hash_set<symtab_node *> reachable;
280 hash_set<tree> body_needed_for_clonning;
281 hash_set<void *> reachable_call_targets;
283 timevar_push (TV_IPA_UNREACHABLE);
284 if (optimize && flag_devirtualize)
285 build_type_inheritance_graph ();
286 if (file)
287 fprintf (file, "\nReclaiming functions:");
288 #ifdef ENABLE_CHECKING
289 FOR_EACH_FUNCTION (node)
290 gcc_assert (!node->aux);
291 FOR_EACH_VARIABLE (vnode)
292 gcc_assert (!vnode->aux);
293 #endif
294 /* Mark functions whose bodies are obviously needed.
295 This is mostly when they can be referenced externally. Inline clones
296 are special since their declarations are shared with master clone and thus
297 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
298 FOR_EACH_FUNCTION (node)
300 node->used_as_abstract_origin = false;
301 if (node->definition
302 && !node->global.inlined_to
303 && !node->in_other_partition
304 && !node->can_remove_if_no_direct_calls_and_refs_p ())
306 gcc_assert (!node->global.inlined_to);
307 reachable.add (node);
308 enqueue_node (node, &first, &reachable);
310 else
311 gcc_assert (!node->aux);
314 /* Mark variables that are obviously needed. */
315 FOR_EACH_DEFINED_VARIABLE (vnode)
316 if (!vnode->can_remove_if_no_refs_p()
317 && !vnode->in_other_partition)
319 reachable.add (vnode);
320 enqueue_node (vnode, &first, &reachable);
323 /* Perform reachability analysis. */
324 while (first != (symtab_node *) (void *) 1)
326 bool in_boundary_p = !reachable.contains (first);
327 symtab_node *node = first;
329 first = (symtab_node *)first->aux;
331 /* If we are processing symbol in boundary, mark its AUX pointer for
332 possible later re-processing in enqueue_node. */
333 if (in_boundary_p)
334 node->aux = (void *)2;
335 else
337 if (TREE_CODE (node->decl) == FUNCTION_DECL
338 && DECL_ABSTRACT_ORIGIN (node->decl))
340 struct cgraph_node *origin_node
341 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (node->decl));
342 origin_node->used_as_abstract_origin = true;
343 enqueue_node (origin_node, &first, &reachable);
345 /* If any symbol in a comdat group is reachable, force
346 all externally visible symbols in the same comdat
347 group to be reachable as well. Comdat-local symbols
348 can be discarded if all uses were inlined. */
349 if (node->same_comdat_group)
351 symtab_node *next;
352 for (next = node->same_comdat_group;
353 next != node;
354 next = next->same_comdat_group)
355 if (!next->comdat_local_p ()
356 && !reachable.add (next))
357 enqueue_node (next, &first, &reachable);
359 /* Mark references as reachable. */
360 process_references (node, &first, before_inlining_p, &reachable);
363 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
365 /* Mark the callees reachable unless they are direct calls to extern
366 inline functions we decided to not inline. */
367 if (!in_boundary_p)
369 struct cgraph_edge *e;
370 /* Keep alive possible targets for devirtualization. */
371 if (optimize && flag_devirtualize)
373 struct cgraph_edge *next;
374 for (e = cnode->indirect_calls; e; e = next)
376 next = e->next_callee;
377 if (e->indirect_info->polymorphic)
378 walk_polymorphic_call_targets (&reachable_call_targets,
379 e, &first, &reachable,
380 before_inlining_p);
383 for (e = cnode->callees; e; e = e->next_callee)
385 if (e->callee->definition
386 && !e->callee->in_other_partition
387 && (!e->inline_failed
388 || !DECL_EXTERNAL (e->callee->decl)
389 || e->callee->alias
390 || before_inlining_p))
392 /* Be sure that we will not optimize out alias target
393 body. */
394 if (DECL_EXTERNAL (e->callee->decl)
395 && e->callee->alias
396 && before_inlining_p)
397 reachable.add (e->callee->function_symbol ());
398 reachable.add (e->callee);
400 enqueue_node (e->callee, &first, &reachable);
403 /* When inline clone exists, mark body to be preserved so when removing
404 offline copy of the function we don't kill it. */
405 if (cnode->global.inlined_to)
406 body_needed_for_clonning.add (cnode->decl);
408 /* For non-inline clones, force their origins to the boundary and ensure
409 that body is not removed. */
410 while (cnode->clone_of)
412 bool noninline = cnode->clone_of->decl != cnode->decl;
413 cnode = cnode->clone_of;
414 if (noninline)
416 body_needed_for_clonning.add (cnode->decl);
417 enqueue_node (cnode, &first, &reachable);
422 /* If any reachable function has simd clones, mark them as
423 reachable as well. */
424 if (cnode->simd_clones)
426 cgraph_node *next;
427 for (next = cnode->simd_clones;
428 next;
429 next = next->simdclone->next_clone)
430 if (in_boundary_p
431 || !reachable.add (next))
432 enqueue_node (next, &first, &reachable);
435 /* When we see constructor of external variable, keep referred nodes in the
436 boundary. This will also hold initializers of the external vars NODE
437 refers to. */
438 varpool_node *vnode = dyn_cast <varpool_node *> (node);
439 if (vnode
440 && DECL_EXTERNAL (node->decl)
441 && !vnode->alias
442 && in_boundary_p)
444 struct ipa_ref *ref = NULL;
445 for (int i = 0; node->iterate_reference (i, ref); i++)
446 enqueue_node (ref->referred, &first, &reachable);
450 /* Remove unreachable functions. */
451 for (node = cgraph_first_function (); node; node = next)
453 next = cgraph_next_function (node);
455 /* If node is not needed at all, remove it. */
456 if (!node->aux)
458 if (file)
459 fprintf (file, " %s/%i", node->name (), node->order);
460 node->remove ();
461 changed = true;
463 /* If node is unreachable, remove its body. */
464 else if (!reachable.contains (node))
466 if (!body_needed_for_clonning.contains (node->decl))
467 node->release_body ();
468 else if (!node->clone_of)
469 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
470 if (node->definition)
472 if (file)
473 fprintf (file, " %s/%i", node->name (), node->order);
474 node->body_removed = true;
475 node->analyzed = false;
476 node->definition = false;
477 node->cpp_implicit_alias = false;
478 node->alias = false;
479 node->thunk.thunk_p = false;
480 node->weakref = false;
481 /* After early inlining we drop always_inline attributes on
482 bodies of functions that are still referenced (have their
483 address taken). */
484 DECL_ATTRIBUTES (node->decl)
485 = remove_attribute ("always_inline",
486 DECL_ATTRIBUTES (node->decl));
487 if (!node->in_other_partition)
488 node->local.local = false;
489 node->remove_callees ();
490 node->remove_from_same_comdat_group ();
491 node->remove_all_references ();
492 changed = true;
495 else
496 gcc_assert (node->clone_of || !node->has_gimple_body_p ()
497 || in_lto_p || DECL_RESULT (node->decl));
500 /* Inline clones might be kept around so their materializing allows further
501 cloning. If the function the clone is inlined into is removed, we need
502 to turn it into normal cone. */
503 FOR_EACH_FUNCTION (node)
505 if (node->global.inlined_to
506 && !node->callers)
508 gcc_assert (node->clones);
509 node->global.inlined_to = NULL;
510 update_inlined_to_pointer (node, node);
512 node->aux = NULL;
515 /* Remove unreachable variables. */
516 if (file)
517 fprintf (file, "\nReclaiming variables:");
518 for (vnode = varpool_first_variable (); vnode; vnode = vnext)
520 vnext = varpool_next_variable (vnode);
521 if (!vnode->aux
522 /* For can_refer_decl_in_current_unit_p we want to track for
523 all external variables if they are defined in other partition
524 or not. */
525 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
527 if (file)
528 fprintf (file, " %s/%i", vnode->name (), vnode->order);
529 vnode->remove ();
530 changed = true;
532 else if (!reachable.contains (vnode))
534 tree init;
535 if (vnode->definition)
537 if (file)
538 fprintf (file, " %s", vnode->name ());
539 changed = true;
541 vnode->body_removed = true;
542 vnode->definition = false;
543 vnode->analyzed = false;
544 vnode->aux = NULL;
546 vnode->remove_from_same_comdat_group ();
548 /* Keep body if it may be useful for constant folding. */
549 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node)
550 vnode->remove_initializer ();
551 else
552 DECL_INITIAL (vnode->decl) = init;
553 vnode->remove_all_references ();
555 else
556 vnode->aux = NULL;
559 /* Now update address_taken flags and try to promote functions to be local. */
560 if (file)
561 fprintf (file, "\nClearing address taken flags:");
562 FOR_EACH_DEFINED_FUNCTION (node)
563 if (node->address_taken
564 && !node->used_from_other_partition)
566 if (!node->call_for_symbol_thunks_and_aliases
567 (has_addr_references_p, NULL, true))
569 if (file)
570 fprintf (file, " %s", node->name ());
571 node->address_taken = false;
572 changed = true;
573 if (node->local_p ())
575 node->local.local = true;
576 if (file)
577 fprintf (file, " (local)");
581 if (file)
582 fprintf (file, "\n");
584 #ifdef ENABLE_CHECKING
585 symtab_node::verify_symtab_nodes ();
586 #endif
588 /* If we removed something, perhaps profile could be improved. */
589 if (changed && optimize && inline_edge_summary_vec.exists ())
590 FOR_EACH_DEFINED_FUNCTION (node)
591 ipa_propagate_frequency (node);
593 timevar_pop (TV_IPA_UNREACHABLE);
594 return changed;
597 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
598 as needed, also clear EXPLICIT_REFS if the references to given variable
599 do not need to be explicit. */
601 void
602 process_references (varpool_node *vnode,
603 bool *written, bool *address_taken,
604 bool *read, bool *explicit_refs)
606 int i;
607 struct ipa_ref *ref;
609 if (!vnode->all_refs_explicit_p ()
610 || TREE_THIS_VOLATILE (vnode->decl))
611 *explicit_refs = false;
613 for (i = 0; vnode->iterate_referring (i, ref)
614 && *explicit_refs && (!*written || !*address_taken || !*read); i++)
615 switch (ref->use)
617 case IPA_REF_ADDR:
618 *address_taken = true;
619 break;
620 case IPA_REF_LOAD:
621 *read = true;
622 break;
623 case IPA_REF_STORE:
624 *written = true;
625 break;
626 case IPA_REF_ALIAS:
627 process_references (dyn_cast<varpool_node *> (ref->referring), written,
628 address_taken, read, explicit_refs);
629 break;
633 /* Set TREE_READONLY bit. */
635 bool
636 set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
638 TREE_READONLY (vnode->decl) = true;
639 return false;
642 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
644 bool
645 set_writeonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
647 vnode->writeonly = true;
648 if (optimize)
650 DECL_INITIAL (vnode->decl) = NULL;
651 if (!vnode->alias)
652 vnode->remove_all_references ();
654 return false;
657 /* Clear addressale bit of VNODE. */
659 bool
660 clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
662 vnode->address_taken = false;
663 TREE_ADDRESSABLE (vnode->decl) = 0;
664 return false;
667 /* Discover variables that have no longer address taken or that are read only
668 and update their flags.
670 FIXME: This can not be done in between gimplify and omp_expand since
671 readonly flag plays role on what is shared and what is not. Currently we do
672 this transformation as part of whole program visibility and re-do at
673 ipa-reference pass (to take into account clonning), but it would
674 make sense to do it before early optimizations. */
676 void
677 ipa_discover_readonly_nonaddressable_vars (void)
679 varpool_node *vnode;
680 if (dump_file)
681 fprintf (dump_file, "Clearing variable flags:");
682 FOR_EACH_VARIABLE (vnode)
683 if (!vnode->alias
684 && (TREE_ADDRESSABLE (vnode->decl)
685 || !vnode->writeonly
686 || !TREE_READONLY (vnode->decl)))
688 bool written = false;
689 bool address_taken = false;
690 bool read = false;
691 bool explicit_refs = true;
693 process_references (vnode, &written, &address_taken, &read, &explicit_refs);
694 if (!explicit_refs)
695 continue;
696 if (!address_taken)
698 if (TREE_ADDRESSABLE (vnode->decl) && dump_file)
699 fprintf (dump_file, " %s (non-addressable)", vnode->name ());
700 vnode->call_for_node_and_aliases (clear_addressable_bit, NULL, true);
702 if (!address_taken && !written
703 /* Making variable in explicit section readonly can cause section
704 type conflict.
705 See e.g. gcc.c-torture/compile/pr23237.c */
706 && vnode->get_section () == NULL)
708 if (!TREE_READONLY (vnode->decl) && dump_file)
709 fprintf (dump_file, " %s (read-only)", vnode->name ());
710 vnode->call_for_node_and_aliases (set_readonly_bit, NULL, true);
712 if (!vnode->writeonly && !read && !address_taken && written)
714 if (dump_file)
715 fprintf (dump_file, " %s (write-only)", vnode->name ());
716 vnode->call_for_node_and_aliases (set_writeonly_bit, NULL, true);
719 if (dump_file)
720 fprintf (dump_file, "\n");
723 /* Free inline summary. */
725 namespace {
727 const pass_data pass_data_ipa_free_inline_summary =
729 SIMPLE_IPA_PASS, /* type */
730 "*free_inline_summary", /* name */
731 OPTGROUP_NONE, /* optinfo_flags */
732 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
733 0, /* properties_required */
734 0, /* properties_provided */
735 0, /* properties_destroyed */
736 0, /* todo_flags_start */
737 0, /* todo_flags_finish */
740 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
742 public:
743 pass_ipa_free_inline_summary (gcc::context *ctxt)
744 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
747 /* opt_pass methods: */
748 virtual unsigned int execute (function *)
750 inline_free_summary ();
751 return 0;
754 }; // class pass_ipa_free_inline_summary
756 } // anon namespace
758 simple_ipa_opt_pass *
759 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
761 return new pass_ipa_free_inline_summary (ctxt);
764 /* Generate and emit a static constructor or destructor. WHICH must
765 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
766 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
767 initialization priority for this constructor or destructor.
769 FINAL specify whether the externally visible name for collect2 should
770 be produced. */
772 static void
773 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
775 static int counter = 0;
776 char which_buf[16];
777 tree decl, name, resdecl;
779 /* The priority is encoded in the constructor or destructor name.
780 collect2 will sort the names and arrange that they are called at
781 program startup. */
782 if (final)
783 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
784 else
785 /* Proudce sane name but one not recognizable by collect2, just for the
786 case we fail to inline the function. */
787 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
788 name = get_file_function_name (which_buf);
790 decl = build_decl (input_location, FUNCTION_DECL, name,
791 build_function_type_list (void_type_node, NULL_TREE));
792 current_function_decl = decl;
794 resdecl = build_decl (input_location,
795 RESULT_DECL, NULL_TREE, void_type_node);
796 DECL_ARTIFICIAL (resdecl) = 1;
797 DECL_RESULT (decl) = resdecl;
798 DECL_CONTEXT (resdecl) = decl;
800 allocate_struct_function (decl, false);
802 TREE_STATIC (decl) = 1;
803 TREE_USED (decl) = 1;
804 DECL_ARTIFICIAL (decl) = 1;
805 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
806 DECL_SAVED_TREE (decl) = body;
807 if (!targetm.have_ctors_dtors && final)
809 TREE_PUBLIC (decl) = 1;
810 DECL_PRESERVE_P (decl) = 1;
812 DECL_UNINLINABLE (decl) = 1;
814 DECL_INITIAL (decl) = make_node (BLOCK);
815 TREE_USED (DECL_INITIAL (decl)) = 1;
817 DECL_SOURCE_LOCATION (decl) = input_location;
818 cfun->function_end_locus = input_location;
820 switch (which)
822 case 'I':
823 DECL_STATIC_CONSTRUCTOR (decl) = 1;
824 decl_init_priority_insert (decl, priority);
825 break;
826 case 'D':
827 DECL_STATIC_DESTRUCTOR (decl) = 1;
828 decl_fini_priority_insert (decl, priority);
829 break;
830 default:
831 gcc_unreachable ();
834 gimplify_function_tree (decl);
836 cgraph_node::add_new_function (decl, false);
838 set_cfun (NULL);
839 current_function_decl = NULL;
842 /* Generate and emit a static constructor or destructor. WHICH must
843 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
844 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
845 initialization priority for this constructor or destructor. */
847 void
848 cgraph_build_static_cdtor (char which, tree body, int priority)
850 cgraph_build_static_cdtor_1 (which, body, priority, false);
853 /* A vector of FUNCTION_DECLs declared as static constructors. */
854 static vec<tree> static_ctors;
855 /* A vector of FUNCTION_DECLs declared as static destructors. */
856 static vec<tree> static_dtors;
858 /* When target does not have ctors and dtors, we call all constructor
859 and destructor by special initialization/destruction function
860 recognized by collect2.
862 When we are going to build this function, collect all constructors and
863 destructors and turn them into normal functions. */
865 static void
866 record_cdtor_fn (struct cgraph_node *node)
868 if (DECL_STATIC_CONSTRUCTOR (node->decl))
869 static_ctors.safe_push (node->decl);
870 if (DECL_STATIC_DESTRUCTOR (node->decl))
871 static_dtors.safe_push (node->decl);
872 node = cgraph_node::get (node->decl);
873 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
876 /* Define global constructors/destructor functions for the CDTORS, of
877 which they are LEN. The CDTORS are sorted by initialization
878 priority. If CTOR_P is true, these are constructors; otherwise,
879 they are destructors. */
881 static void
882 build_cdtor (bool ctor_p, vec<tree> cdtors)
884 size_t i,j;
885 size_t len = cdtors.length ();
887 i = 0;
888 while (i < len)
890 tree body;
891 tree fn;
892 priority_type priority;
894 priority = 0;
895 body = NULL_TREE;
896 j = i;
899 priority_type p;
900 fn = cdtors[j];
901 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
902 if (j == i)
903 priority = p;
904 else if (p != priority)
905 break;
906 j++;
908 while (j < len);
910 /* When there is only one cdtor and target supports them, do nothing. */
911 if (j == i + 1
912 && targetm.have_ctors_dtors)
914 i++;
915 continue;
917 /* Find the next batch of constructors/destructors with the same
918 initialization priority. */
919 for (;i < j; i++)
921 tree call;
922 fn = cdtors[i];
923 call = build_call_expr (fn, 0);
924 if (ctor_p)
925 DECL_STATIC_CONSTRUCTOR (fn) = 0;
926 else
927 DECL_STATIC_DESTRUCTOR (fn) = 0;
928 /* We do not want to optimize away pure/const calls here.
929 When optimizing, these should be already removed, when not
930 optimizing, we want user to be able to breakpoint in them. */
931 TREE_SIDE_EFFECTS (call) = 1;
932 append_to_statement_list (call, &body);
934 gcc_assert (body != NULL_TREE);
935 /* Generate a function to call all the function of like
936 priority. */
937 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
941 /* Comparison function for qsort. P1 and P2 are actually of type
942 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
943 used to determine the sort order. */
945 static int
946 compare_ctor (const void *p1, const void *p2)
948 tree f1;
949 tree f2;
950 int priority1;
951 int priority2;
953 f1 = *(const tree *)p1;
954 f2 = *(const tree *)p2;
955 priority1 = DECL_INIT_PRIORITY (f1);
956 priority2 = DECL_INIT_PRIORITY (f2);
958 if (priority1 < priority2)
959 return -1;
960 else if (priority1 > priority2)
961 return 1;
962 else
963 /* Ensure a stable sort. Constructors are executed in backwarding
964 order to make LTO initialize braries first. */
965 return DECL_UID (f2) - DECL_UID (f1);
968 /* Comparison function for qsort. P1 and P2 are actually of type
969 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
970 used to determine the sort order. */
972 static int
973 compare_dtor (const void *p1, const void *p2)
975 tree f1;
976 tree f2;
977 int priority1;
978 int priority2;
980 f1 = *(const tree *)p1;
981 f2 = *(const tree *)p2;
982 priority1 = DECL_FINI_PRIORITY (f1);
983 priority2 = DECL_FINI_PRIORITY (f2);
985 if (priority1 < priority2)
986 return -1;
987 else if (priority1 > priority2)
988 return 1;
989 else
990 /* Ensure a stable sort. */
991 return DECL_UID (f1) - DECL_UID (f2);
994 /* Generate functions to call static constructors and destructors
995 for targets that do not support .ctors/.dtors sections. These
996 functions have magic names which are detected by collect2. */
998 static void
999 build_cdtor_fns (void)
1001 if (!static_ctors.is_empty ())
1003 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1004 static_ctors.qsort (compare_ctor);
1005 build_cdtor (/*ctor_p=*/true, static_ctors);
1008 if (!static_dtors.is_empty ())
1010 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1011 static_dtors.qsort (compare_dtor);
1012 build_cdtor (/*ctor_p=*/false, static_dtors);
1016 /* Look for constructors and destructors and produce function calling them.
1017 This is needed for targets not supporting ctors or dtors, but we perform the
1018 transformation also at linktime to merge possibly numerous
1019 constructors/destructors into single function to improve code locality and
1020 reduce size. */
1022 static unsigned int
1023 ipa_cdtor_merge (void)
1025 struct cgraph_node *node;
1026 FOR_EACH_DEFINED_FUNCTION (node)
1027 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1028 || DECL_STATIC_DESTRUCTOR (node->decl))
1029 record_cdtor_fn (node);
1030 build_cdtor_fns ();
1031 static_ctors.release ();
1032 static_dtors.release ();
1033 return 0;
1036 namespace {
1038 const pass_data pass_data_ipa_cdtor_merge =
1040 IPA_PASS, /* type */
1041 "cdtor", /* name */
1042 OPTGROUP_NONE, /* optinfo_flags */
1043 TV_CGRAPHOPT, /* tv_id */
1044 0, /* properties_required */
1045 0, /* properties_provided */
1046 0, /* properties_destroyed */
1047 0, /* todo_flags_start */
1048 0, /* todo_flags_finish */
1051 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1053 public:
1054 pass_ipa_cdtor_merge (gcc::context *ctxt)
1055 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1056 NULL, /* generate_summary */
1057 NULL, /* write_summary */
1058 NULL, /* read_summary */
1059 NULL, /* write_optimization_summary */
1060 NULL, /* read_optimization_summary */
1061 NULL, /* stmt_fixup */
1062 0, /* function_transform_todo_flags_start */
1063 NULL, /* function_transform */
1064 NULL) /* variable_transform */
1067 /* opt_pass methods: */
1068 virtual bool gate (function *);
1069 virtual unsigned int execute (function *) { return ipa_cdtor_merge (); }
1071 }; // class pass_ipa_cdtor_merge
1073 bool
1074 pass_ipa_cdtor_merge::gate (function *)
1076 /* Perform the pass when we have no ctors/dtors support
1077 or at LTO time to merge multiple constructors into single
1078 function. */
1079 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1082 } // anon namespace
1084 ipa_opt_pass_d *
1085 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1087 return new pass_ipa_cdtor_merge (ctxt);
1090 /* Invalid pointer representing BOTTOM for single user dataflow. */
1091 #define BOTTOM ((cgraph_node *)(size_t) 2)
1093 /* Meet operation for single user dataflow.
1094 Here we want to associate variables with sigle function that may access it.
1096 FUNCTION is current single user of a variable, VAR is variable that uses it.
1097 Latttice is stored in SINGLE_USER_MAP.
1099 We represent:
1100 - TOP by no entry in SIGNLE_USER_MAP
1101 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1102 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1104 cgraph_node *
1105 meet (cgraph_node *function, varpool_node *var,
1106 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1108 struct cgraph_node *user, **f;
1110 if (var->aux == BOTTOM)
1111 return BOTTOM;
1113 f = single_user_map.get (var);
1114 if (!f)
1115 return function;
1116 user = *f;
1117 if (!function)
1118 return user;
1119 else if (function != user)
1120 return BOTTOM;
1121 else
1122 return function;
1125 /* Propagation step of single-use dataflow.
1127 Check all uses of VNODE and see if they are used by single function FUNCTION.
1128 SINGLE_USER_MAP represents the dataflow lattice. */
1130 cgraph_node *
1131 propagate_single_user (varpool_node *vnode, cgraph_node *function,
1132 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1134 int i;
1135 struct ipa_ref *ref;
1137 gcc_assert (!vnode->externally_visible);
1139 /* If node is an alias, first meet with its target. */
1140 if (vnode->alias)
1141 function = meet (function, vnode->get_alias_target (), single_user_map);
1143 /* Check all users and see if they correspond to a single function. */
1144 for (i = 0; vnode->iterate_referring (i, ref) && function != BOTTOM; i++)
1146 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
1147 if (cnode)
1149 if (cnode->global.inlined_to)
1150 cnode = cnode->global.inlined_to;
1151 if (!function)
1152 function = cnode;
1153 else if (function != cnode)
1154 function = BOTTOM;
1156 else
1157 function = meet (function, dyn_cast <varpool_node *> (ref->referring), single_user_map);
1159 return function;
1162 /* Pass setting used_by_single_function flag.
1163 This flag is set on variable when there is only one function that may possibly
1164 referr to it. */
1166 static unsigned int
1167 ipa_single_use (void)
1169 varpool_node *first = (varpool_node *) (void *) 1;
1170 varpool_node *var;
1171 hash_map<varpool_node *, cgraph_node *> single_user_map;
1173 FOR_EACH_DEFINED_VARIABLE (var)
1174 if (!var->all_refs_explicit_p ())
1175 var->aux = BOTTOM;
1176 else
1178 /* Enqueue symbol for dataflow. */
1179 var->aux = first;
1180 first = var;
1183 /* The actual dataflow. */
1185 while (first != (void *) 1)
1187 cgraph_node *user, *orig_user, **f;
1189 var = first;
1190 first = (varpool_node *)first->aux;
1192 f = single_user_map.get (var);
1193 if (f)
1194 orig_user = *f;
1195 else
1196 orig_user = NULL;
1197 user = propagate_single_user (var, orig_user, single_user_map);
1199 gcc_checking_assert (var->aux != BOTTOM);
1201 /* If user differs, enqueue all references. */
1202 if (user != orig_user)
1204 unsigned int i;
1205 ipa_ref *ref;
1207 single_user_map.put (var, user);
1209 /* Enqueue all aliases for re-processing. */
1210 for (i = 0; var->iterate_referring (i, ref); i++)
1211 if (ref->use == IPA_REF_ALIAS
1212 && !ref->referring->aux)
1214 ref->referring->aux = first;
1215 first = dyn_cast <varpool_node *> (ref->referring);
1217 /* Enqueue all users for re-processing. */
1218 for (i = 0; var->iterate_reference (i, ref); i++)
1219 if (!ref->referred->aux
1220 && ref->referred->definition
1221 && is_a <varpool_node *> (ref->referred))
1223 ref->referred->aux = first;
1224 first = dyn_cast <varpool_node *> (ref->referred);
1227 /* If user is BOTTOM, just punt on this var. */
1228 if (user == BOTTOM)
1229 var->aux = BOTTOM;
1230 else
1231 var->aux = NULL;
1233 else
1234 var->aux = NULL;
1237 FOR_EACH_DEFINED_VARIABLE (var)
1239 if (var->aux != BOTTOM)
1241 #ifdef ENABLE_CHECKING
1242 if (!single_user_map.get (var))
1243 gcc_assert (single_user_map.get (var));
1244 #endif
1245 if (dump_file)
1247 fprintf (dump_file, "Variable %s/%i is used by single function\n",
1248 var->name (), var->order);
1250 var->used_by_single_function = true;
1252 var->aux = NULL;
1254 return 0;
1257 namespace {
1259 const pass_data pass_data_ipa_single_use =
1261 IPA_PASS, /* type */
1262 "single-use", /* name */
1263 OPTGROUP_NONE, /* optinfo_flags */
1264 TV_CGRAPHOPT, /* tv_id */
1265 0, /* properties_required */
1266 0, /* properties_provided */
1267 0, /* properties_destroyed */
1268 0, /* todo_flags_start */
1269 0, /* todo_flags_finish */
1272 class pass_ipa_single_use : public ipa_opt_pass_d
1274 public:
1275 pass_ipa_single_use (gcc::context *ctxt)
1276 : ipa_opt_pass_d (pass_data_ipa_single_use, ctxt,
1277 NULL, /* generate_summary */
1278 NULL, /* write_summary */
1279 NULL, /* read_summary */
1280 NULL, /* write_optimization_summary */
1281 NULL, /* read_optimization_summary */
1282 NULL, /* stmt_fixup */
1283 0, /* function_transform_todo_flags_start */
1284 NULL, /* function_transform */
1285 NULL) /* variable_transform */
1288 /* opt_pass methods: */
1289 virtual bool gate (function *);
1290 virtual unsigned int execute (function *) { return ipa_single_use (); }
1292 }; // class pass_ipa_single_use
1294 bool
1295 pass_ipa_single_use::gate (function *)
1297 return optimize;
1300 } // anon namespace
1302 ipa_opt_pass_d *
1303 make_pass_ipa_single_use (gcc::context *ctxt)
1305 return new pass_ipa_single_use (ctxt);