* rtl.h (insn_location): Declare.
[official-gcc.git] / gcc / ipa.c
blob5850d2818295aaea7043a80e8e946a01cdf65f0c
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 "pointer-set.h"
30 #include "gimple-expr.h"
31 #include "gimplify.h"
32 #include "flags.h"
33 #include "target.h"
34 #include "tree-iterator.h"
35 #include "ipa-utils.h"
36 #include "ipa-inline.h"
37 #include "tree-inline.h"
38 #include "profile.h"
39 #include "params.h"
40 #include "internal-fn.h"
41 #include "tree-ssa-alias.h"
42 #include "gimple.h"
43 #include "dbgcnt.h"
46 /* Return true when NODE has ADDR reference. */
48 static bool
49 has_addr_references_p (struct cgraph_node *node,
50 void *data ATTRIBUTE_UNUSED)
52 int i;
53 struct ipa_ref *ref;
55 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list,
56 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 struct pointer_set_t *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 && !pointer_set_contains (reachable, node))
95 return;
96 node->aux = *first;
97 *first = node;
100 /* Process references. */
102 static void
103 process_references (struct ipa_ref_list *list,
104 symtab_node **first,
105 bool before_inlining_p,
106 struct pointer_set_t *reachable)
108 int i;
109 struct ipa_ref *ref;
110 for (i = 0; ipa_ref_list_reference_iterate (list, 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 pointer_set_insert (reachable, 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 (pointer_set_t *reachable_call_targets,
142 struct cgraph_edge *edge,
143 symtab_node **first,
144 pointer_set_t *reachable, bool before_inlining_p)
146 unsigned int i;
147 void *cache_token;
148 bool final;
149 vec <cgraph_node *>targets
150 = possible_polymorphic_call_targets
151 (edge, &final, &cache_token);
153 if (!pointer_set_insert (reachable_call_targets,
154 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 pointer_set_insert (reachable, 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_get_create_node
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 struct pointer_set_t *reachable = pointer_set_create ();
280 struct pointer_set_t *body_needed_for_clonning = pointer_set_create ();
281 struct pointer_set_t *reachable_call_targets = pointer_set_create ();
283 timevar_push (TV_IPA_UNREACHABLE);
284 #ifdef ENABLE_CHECKING
285 verify_symtab ();
286 #endif
287 if (optimize && flag_devirtualize)
288 build_type_inheritance_graph ();
289 if (file)
290 fprintf (file, "\nReclaiming functions:");
291 #ifdef ENABLE_CHECKING
292 FOR_EACH_FUNCTION (node)
293 gcc_assert (!node->aux);
294 FOR_EACH_VARIABLE (vnode)
295 gcc_assert (!vnode->aux);
296 #endif
297 /* Mark functions whose bodies are obviously needed.
298 This is mostly when they can be referenced externally. Inline clones
299 are special since their declarations are shared with master clone and thus
300 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
301 FOR_EACH_FUNCTION (node)
303 node->used_as_abstract_origin = false;
304 if (node->definition
305 && !node->global.inlined_to
306 && !node->in_other_partition
307 && !cgraph_can_remove_if_no_direct_calls_and_refs_p (node))
309 gcc_assert (!node->global.inlined_to);
310 pointer_set_insert (reachable, node);
311 enqueue_node (node, &first, reachable);
313 else
314 gcc_assert (!node->aux);
317 /* Mark variables that are obviously needed. */
318 FOR_EACH_DEFINED_VARIABLE (vnode)
319 if (!varpool_can_remove_if_no_refs (vnode)
320 && !vnode->in_other_partition)
322 pointer_set_insert (reachable, vnode);
323 enqueue_node (vnode, &first, reachable);
326 /* Perform reachability analysis. */
327 while (first != (symtab_node *) (void *) 1)
329 bool in_boundary_p = !pointer_set_contains (reachable, first);
330 symtab_node *node = first;
332 first = (symtab_node *)first->aux;
334 /* If we are processing symbol in boundary, mark its AUX pointer for
335 possible later re-processing in enqueue_node. */
336 if (in_boundary_p)
337 node->aux = (void *)2;
338 else
340 if (TREE_CODE (node->decl) == FUNCTION_DECL
341 && DECL_ABSTRACT_ORIGIN (node->decl))
343 struct cgraph_node *origin_node
344 = cgraph_get_create_node (DECL_ABSTRACT_ORIGIN (node->decl));
345 origin_node->used_as_abstract_origin = true;
346 enqueue_node (origin_node, &first, reachable);
348 /* If any symbol in a comdat group is reachable, force
349 all externally visible symbols in the same comdat
350 group to be reachable as well. Comdat-local symbols
351 can be discarded if all uses were inlined. */
352 if (node->same_comdat_group)
354 symtab_node *next;
355 for (next = node->same_comdat_group;
356 next != node;
357 next = next->same_comdat_group)
358 if (!symtab_comdat_local_p (next)
359 && !pointer_set_insert (reachable, next))
360 enqueue_node (next, &first, reachable);
362 /* Mark references as reachable. */
363 process_references (&node->ref_list, &first,
364 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)
402 pointer_set_insert (reachable,
403 cgraph_function_node (e->callee));
405 pointer_set_insert (reachable, e->callee);
407 enqueue_node (e->callee, &first, reachable);
410 /* When inline clone exists, mark body to be preserved so when removing
411 offline copy of the function we don't kill it. */
412 if (cnode->global.inlined_to)
413 pointer_set_insert (body_needed_for_clonning, cnode->decl);
415 /* For non-inline clones, force their origins to the boundary and ensure
416 that body is not removed. */
417 while (cnode->clone_of)
419 bool noninline = cnode->clone_of->decl != cnode->decl;
420 cnode = cnode->clone_of;
421 if (noninline)
423 pointer_set_insert (body_needed_for_clonning, cnode->decl);
424 enqueue_node (cnode, &first, reachable);
429 /* If any reachable function has simd clones, mark them as
430 reachable as well. */
431 if (cnode->simd_clones)
433 cgraph_node *next;
434 for (next = cnode->simd_clones;
435 next;
436 next = next->simdclone->next_clone)
437 if (in_boundary_p
438 || !pointer_set_insert (reachable, next))
439 enqueue_node (next, &first, reachable);
442 /* When we see constructor of external variable, keep referred nodes in the
443 boundary. This will also hold initializers of the external vars NODE
444 refers to. */
445 varpool_node *vnode = dyn_cast <varpool_node *> (node);
446 if (vnode
447 && DECL_EXTERNAL (node->decl)
448 && !vnode->alias
449 && in_boundary_p)
451 struct ipa_ref *ref;
452 for (int i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
453 enqueue_node (ref->referred, &first, reachable);
457 /* Remove unreachable functions. */
458 for (node = cgraph_first_function (); node; node = next)
460 next = cgraph_next_function (node);
462 /* If node is not needed at all, remove it. */
463 if (!node->aux)
465 if (file)
466 fprintf (file, " %s/%i", node->name (), node->order);
467 cgraph_remove_node (node);
468 changed = true;
470 /* If node is unreachable, remove its body. */
471 else if (!pointer_set_contains (reachable, node))
473 if (!pointer_set_contains (body_needed_for_clonning, node->decl))
474 cgraph_release_function_body (node);
475 else if (!node->clone_of)
476 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
477 if (node->definition)
479 if (file)
480 fprintf (file, " %s/%i", node->name (), node->order);
481 node->body_removed = true;
482 node->analyzed = false;
483 node->definition = false;
484 node->cpp_implicit_alias = false;
485 node->alias = false;
486 node->thunk.thunk_p = false;
487 node->weakref = false;
488 /* After early inlining we drop always_inline attributes on
489 bodies of functions that are still referenced (have their
490 address taken). */
491 DECL_ATTRIBUTES (node->decl)
492 = remove_attribute ("always_inline",
493 DECL_ATTRIBUTES (node->decl));
494 if (!node->in_other_partition)
495 node->local.local = false;
496 cgraph_node_remove_callees (node);
497 symtab_remove_from_same_comdat_group (node);
498 ipa_remove_all_references (&node->ref_list);
499 changed = true;
502 else
503 gcc_assert (node->clone_of || !cgraph_function_with_gimple_body_p (node)
504 || in_lto_p || DECL_RESULT (node->decl));
507 /* Inline clones might be kept around so their materializing allows further
508 cloning. If the function the clone is inlined into is removed, we need
509 to turn it into normal cone. */
510 FOR_EACH_FUNCTION (node)
512 if (node->global.inlined_to
513 && !node->callers)
515 gcc_assert (node->clones);
516 node->global.inlined_to = NULL;
517 update_inlined_to_pointer (node, node);
519 node->aux = NULL;
522 /* Remove unreachable variables. */
523 if (file)
524 fprintf (file, "\nReclaiming variables:");
525 for (vnode = varpool_first_variable (); vnode; vnode = vnext)
527 vnext = varpool_next_variable (vnode);
528 if (!vnode->aux
529 /* For can_refer_decl_in_current_unit_p we want to track for
530 all external variables if they are defined in other partition
531 or not. */
532 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
534 if (file)
535 fprintf (file, " %s/%i", vnode->name (), vnode->order);
536 varpool_remove_node (vnode);
537 changed = true;
539 else if (!pointer_set_contains (reachable, vnode))
541 tree init;
542 if (vnode->definition)
544 if (file)
545 fprintf (file, " %s", vnode->name ());
546 changed = true;
548 vnode->body_removed = true;
549 vnode->definition = false;
550 vnode->analyzed = false;
551 vnode->aux = NULL;
553 symtab_remove_from_same_comdat_group (vnode);
555 /* Keep body if it may be useful for constant folding. */
556 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node)
557 varpool_remove_initializer (vnode);
558 else
559 DECL_INITIAL (vnode->decl) = init;
560 ipa_remove_all_references (&vnode->ref_list);
562 else
563 vnode->aux = NULL;
566 pointer_set_destroy (reachable);
567 pointer_set_destroy (body_needed_for_clonning);
568 pointer_set_destroy (reachable_call_targets);
570 /* Now update address_taken flags and try to promote functions to be local. */
571 if (file)
572 fprintf (file, "\nClearing address taken flags:");
573 FOR_EACH_DEFINED_FUNCTION (node)
574 if (node->address_taken
575 && !node->used_from_other_partition)
577 if (!cgraph_for_node_and_aliases (node, has_addr_references_p, NULL, true))
579 if (file)
580 fprintf (file, " %s", node->name ());
581 node->address_taken = false;
582 changed = true;
583 if (cgraph_local_node_p (node))
585 node->local.local = true;
586 if (file)
587 fprintf (file, " (local)");
591 if (file)
592 fprintf (file, "\n");
594 #ifdef ENABLE_CHECKING
595 verify_symtab ();
596 #endif
598 /* If we removed something, perhaps profile could be improved. */
599 if (changed && optimize && inline_edge_summary_vec.exists ())
600 FOR_EACH_DEFINED_FUNCTION (node)
601 ipa_propagate_frequency (node);
603 timevar_pop (TV_IPA_UNREACHABLE);
604 return changed;
607 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
608 as needed, also clear EXPLICIT_REFS if the references to given variable
609 do not need to be explicit. */
611 void
612 process_references (varpool_node *vnode,
613 bool *written, bool *address_taken,
614 bool *read, bool *explicit_refs)
616 int i;
617 struct ipa_ref *ref;
619 if (!varpool_all_refs_explicit_p (vnode)
620 || TREE_THIS_VOLATILE (vnode->decl))
621 *explicit_refs = false;
623 for (i = 0; ipa_ref_list_referring_iterate (&vnode->ref_list,
624 i, ref)
625 && *explicit_refs && (!*written || !*address_taken || !*read); i++)
626 switch (ref->use)
628 case IPA_REF_ADDR:
629 *address_taken = true;
630 break;
631 case IPA_REF_LOAD:
632 *read = true;
633 break;
634 case IPA_REF_STORE:
635 *written = true;
636 break;
637 case IPA_REF_ALIAS:
638 process_references (varpool (ref->referring), written, address_taken,
639 read, explicit_refs);
640 break;
644 /* Set TREE_READONLY bit. */
646 bool
647 set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
649 TREE_READONLY (vnode->decl) = true;
650 return false;
653 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
655 bool
656 set_writeonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
658 vnode->writeonly = true;
659 if (optimize)
661 DECL_INITIAL (vnode->decl) = NULL;
662 if (!vnode->alias)
663 ipa_remove_all_references (&vnode->ref_list);
665 return false;
668 /* Clear addressale bit of VNODE. */
670 bool
671 clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
673 vnode->address_taken = false;
674 TREE_ADDRESSABLE (vnode->decl) = 0;
675 return false;
678 /* Discover variables that have no longer address taken or that are read only
679 and update their flags.
681 FIXME: This can not be done in between gimplify and omp_expand since
682 readonly flag plays role on what is shared and what is not. Currently we do
683 this transformation as part of whole program visibility and re-do at
684 ipa-reference pass (to take into account clonning), but it would
685 make sense to do it before early optimizations. */
687 void
688 ipa_discover_readonly_nonaddressable_vars (void)
690 varpool_node *vnode;
691 if (dump_file)
692 fprintf (dump_file, "Clearing variable flags:");
693 FOR_EACH_VARIABLE (vnode)
694 if (!vnode->alias
695 && (TREE_ADDRESSABLE (vnode->decl)
696 || !vnode->writeonly
697 || !TREE_READONLY (vnode->decl)))
699 bool written = false;
700 bool address_taken = false;
701 bool read = false;
702 bool explicit_refs = true;
704 process_references (vnode, &written, &address_taken, &read, &explicit_refs);
705 if (!explicit_refs)
706 continue;
707 if (!address_taken)
709 if (TREE_ADDRESSABLE (vnode->decl) && dump_file)
710 fprintf (dump_file, " %s (non-addressable)", vnode->name ());
711 varpool_for_node_and_aliases (vnode, clear_addressable_bit, NULL, true);
713 if (!address_taken && !written
714 /* Making variable in explicit section readonly can cause section
715 type conflict.
716 See e.g. gcc.c-torture/compile/pr23237.c */
717 && DECL_SECTION_NAME (vnode->decl) == NULL)
719 if (!TREE_READONLY (vnode->decl) && dump_file)
720 fprintf (dump_file, " %s (read-only)", vnode->name ());
721 varpool_for_node_and_aliases (vnode, set_readonly_bit, NULL, true);
723 if (!vnode->writeonly && !read && !address_taken && written)
725 if (dump_file)
726 fprintf (dump_file, " %s (write-only)", vnode->name ());
727 varpool_for_node_and_aliases (vnode, set_writeonly_bit, NULL, true);
730 if (dump_file)
731 fprintf (dump_file, "\n");
734 /* Free inline summary. */
736 namespace {
738 const pass_data pass_data_ipa_free_inline_summary =
740 SIMPLE_IPA_PASS, /* type */
741 "*free_inline_summary", /* name */
742 OPTGROUP_NONE, /* optinfo_flags */
743 true, /* has_execute */
744 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
745 0, /* properties_required */
746 0, /* properties_provided */
747 0, /* properties_destroyed */
748 0, /* todo_flags_start */
749 0, /* todo_flags_finish */
752 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
754 public:
755 pass_ipa_free_inline_summary (gcc::context *ctxt)
756 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
759 /* opt_pass methods: */
760 virtual unsigned int execute (function *)
762 inline_free_summary ();
763 return 0;
766 }; // class pass_ipa_free_inline_summary
768 } // anon namespace
770 simple_ipa_opt_pass *
771 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
773 return new pass_ipa_free_inline_summary (ctxt);
776 /* Generate and emit a static constructor or destructor. WHICH must
777 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
778 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
779 initialization priority for this constructor or destructor.
781 FINAL specify whether the externally visible name for collect2 should
782 be produced. */
784 static void
785 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
787 static int counter = 0;
788 char which_buf[16];
789 tree decl, name, resdecl;
791 /* The priority is encoded in the constructor or destructor name.
792 collect2 will sort the names and arrange that they are called at
793 program startup. */
794 if (final)
795 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
796 else
797 /* Proudce sane name but one not recognizable by collect2, just for the
798 case we fail to inline the function. */
799 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
800 name = get_file_function_name (which_buf);
802 decl = build_decl (input_location, FUNCTION_DECL, name,
803 build_function_type_list (void_type_node, NULL_TREE));
804 current_function_decl = decl;
806 resdecl = build_decl (input_location,
807 RESULT_DECL, NULL_TREE, void_type_node);
808 DECL_ARTIFICIAL (resdecl) = 1;
809 DECL_RESULT (decl) = resdecl;
810 DECL_CONTEXT (resdecl) = decl;
812 allocate_struct_function (decl, false);
814 TREE_STATIC (decl) = 1;
815 TREE_USED (decl) = 1;
816 DECL_ARTIFICIAL (decl) = 1;
817 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
818 DECL_SAVED_TREE (decl) = body;
819 if (!targetm.have_ctors_dtors && final)
821 TREE_PUBLIC (decl) = 1;
822 DECL_PRESERVE_P (decl) = 1;
824 DECL_UNINLINABLE (decl) = 1;
826 DECL_INITIAL (decl) = make_node (BLOCK);
827 TREE_USED (DECL_INITIAL (decl)) = 1;
829 DECL_SOURCE_LOCATION (decl) = input_location;
830 cfun->function_end_locus = input_location;
832 switch (which)
834 case 'I':
835 DECL_STATIC_CONSTRUCTOR (decl) = 1;
836 decl_init_priority_insert (decl, priority);
837 break;
838 case 'D':
839 DECL_STATIC_DESTRUCTOR (decl) = 1;
840 decl_fini_priority_insert (decl, priority);
841 break;
842 default:
843 gcc_unreachable ();
846 gimplify_function_tree (decl);
848 cgraph_add_new_function (decl, false);
850 set_cfun (NULL);
851 current_function_decl = NULL;
854 /* Generate and emit a static constructor or destructor. WHICH must
855 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
856 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
857 initialization priority for this constructor or destructor. */
859 void
860 cgraph_build_static_cdtor (char which, tree body, int priority)
862 cgraph_build_static_cdtor_1 (which, body, priority, false);
865 /* A vector of FUNCTION_DECLs declared as static constructors. */
866 static vec<tree> static_ctors;
867 /* A vector of FUNCTION_DECLs declared as static destructors. */
868 static vec<tree> static_dtors;
870 /* When target does not have ctors and dtors, we call all constructor
871 and destructor by special initialization/destruction function
872 recognized by collect2.
874 When we are going to build this function, collect all constructors and
875 destructors and turn them into normal functions. */
877 static void
878 record_cdtor_fn (struct cgraph_node *node)
880 if (DECL_STATIC_CONSTRUCTOR (node->decl))
881 static_ctors.safe_push (node->decl);
882 if (DECL_STATIC_DESTRUCTOR (node->decl))
883 static_dtors.safe_push (node->decl);
884 node = cgraph_get_node (node->decl);
885 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
888 /* Define global constructors/destructor functions for the CDTORS, of
889 which they are LEN. The CDTORS are sorted by initialization
890 priority. If CTOR_P is true, these are constructors; otherwise,
891 they are destructors. */
893 static void
894 build_cdtor (bool ctor_p, vec<tree> cdtors)
896 size_t i,j;
897 size_t len = cdtors.length ();
899 i = 0;
900 while (i < len)
902 tree body;
903 tree fn;
904 priority_type priority;
906 priority = 0;
907 body = NULL_TREE;
908 j = i;
911 priority_type p;
912 fn = cdtors[j];
913 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
914 if (j == i)
915 priority = p;
916 else if (p != priority)
917 break;
918 j++;
920 while (j < len);
922 /* When there is only one cdtor and target supports them, do nothing. */
923 if (j == i + 1
924 && targetm.have_ctors_dtors)
926 i++;
927 continue;
929 /* Find the next batch of constructors/destructors with the same
930 initialization priority. */
931 for (;i < j; i++)
933 tree call;
934 fn = cdtors[i];
935 call = build_call_expr (fn, 0);
936 if (ctor_p)
937 DECL_STATIC_CONSTRUCTOR (fn) = 0;
938 else
939 DECL_STATIC_DESTRUCTOR (fn) = 0;
940 /* We do not want to optimize away pure/const calls here.
941 When optimizing, these should be already removed, when not
942 optimizing, we want user to be able to breakpoint in them. */
943 TREE_SIDE_EFFECTS (call) = 1;
944 append_to_statement_list (call, &body);
946 gcc_assert (body != NULL_TREE);
947 /* Generate a function to call all the function of like
948 priority. */
949 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
953 /* Comparison function for qsort. P1 and P2 are actually of type
954 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
955 used to determine the sort order. */
957 static int
958 compare_ctor (const void *p1, const void *p2)
960 tree f1;
961 tree f2;
962 int priority1;
963 int priority2;
965 f1 = *(const tree *)p1;
966 f2 = *(const tree *)p2;
967 priority1 = DECL_INIT_PRIORITY (f1);
968 priority2 = DECL_INIT_PRIORITY (f2);
970 if (priority1 < priority2)
971 return -1;
972 else if (priority1 > priority2)
973 return 1;
974 else
975 /* Ensure a stable sort. Constructors are executed in backwarding
976 order to make LTO initialize braries first. */
977 return DECL_UID (f2) - DECL_UID (f1);
980 /* Comparison function for qsort. P1 and P2 are actually of type
981 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
982 used to determine the sort order. */
984 static int
985 compare_dtor (const void *p1, const void *p2)
987 tree f1;
988 tree f2;
989 int priority1;
990 int priority2;
992 f1 = *(const tree *)p1;
993 f2 = *(const tree *)p2;
994 priority1 = DECL_FINI_PRIORITY (f1);
995 priority2 = DECL_FINI_PRIORITY (f2);
997 if (priority1 < priority2)
998 return -1;
999 else if (priority1 > priority2)
1000 return 1;
1001 else
1002 /* Ensure a stable sort. */
1003 return DECL_UID (f1) - DECL_UID (f2);
1006 /* Generate functions to call static constructors and destructors
1007 for targets that do not support .ctors/.dtors sections. These
1008 functions have magic names which are detected by collect2. */
1010 static void
1011 build_cdtor_fns (void)
1013 if (!static_ctors.is_empty ())
1015 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1016 static_ctors.qsort (compare_ctor);
1017 build_cdtor (/*ctor_p=*/true, static_ctors);
1020 if (!static_dtors.is_empty ())
1022 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1023 static_dtors.qsort (compare_dtor);
1024 build_cdtor (/*ctor_p=*/false, static_dtors);
1028 /* Look for constructors and destructors and produce function calling them.
1029 This is needed for targets not supporting ctors or dtors, but we perform the
1030 transformation also at linktime to merge possibly numerous
1031 constructors/destructors into single function to improve code locality and
1032 reduce size. */
1034 static unsigned int
1035 ipa_cdtor_merge (void)
1037 struct cgraph_node *node;
1038 FOR_EACH_DEFINED_FUNCTION (node)
1039 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1040 || DECL_STATIC_DESTRUCTOR (node->decl))
1041 record_cdtor_fn (node);
1042 build_cdtor_fns ();
1043 static_ctors.release ();
1044 static_dtors.release ();
1045 return 0;
1048 namespace {
1050 const pass_data pass_data_ipa_cdtor_merge =
1052 IPA_PASS, /* type */
1053 "cdtor", /* name */
1054 OPTGROUP_NONE, /* optinfo_flags */
1055 true, /* has_execute */
1056 TV_CGRAPHOPT, /* tv_id */
1057 0, /* properties_required */
1058 0, /* properties_provided */
1059 0, /* properties_destroyed */
1060 0, /* todo_flags_start */
1061 0, /* todo_flags_finish */
1064 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1066 public:
1067 pass_ipa_cdtor_merge (gcc::context *ctxt)
1068 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1069 NULL, /* generate_summary */
1070 NULL, /* write_summary */
1071 NULL, /* read_summary */
1072 NULL, /* write_optimization_summary */
1073 NULL, /* read_optimization_summary */
1074 NULL, /* stmt_fixup */
1075 0, /* function_transform_todo_flags_start */
1076 NULL, /* function_transform */
1077 NULL) /* variable_transform */
1080 /* opt_pass methods: */
1081 virtual bool gate (function *);
1082 virtual unsigned int execute (function *) { return ipa_cdtor_merge (); }
1084 }; // class pass_ipa_cdtor_merge
1086 bool
1087 pass_ipa_cdtor_merge::gate (function *)
1089 /* Perform the pass when we have no ctors/dtors support
1090 or at LTO time to merge multiple constructors into single
1091 function. */
1092 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1095 } // anon namespace
1097 ipa_opt_pass_d *
1098 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1100 return new pass_ipa_cdtor_merge (ctxt);