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
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
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/>. */
22 #include "coretypes.h"
26 #include "stringpool.h"
28 #include "tree-pass.h"
30 #include "pointer-set.h"
31 #include "gimple-expr.h"
35 #include "tree-iterator.h"
36 #include "ipa-utils.h"
37 #include "ipa-inline.h"
38 #include "tree-inline.h"
41 #include "internal-fn.h"
42 #include "tree-ssa-alias.h"
47 /* Return true when NODE has ADDR reference. */
50 has_addr_references_p (struct cgraph_node
*node
,
51 void *data ATTRIBUTE_UNUSED
)
54 struct ipa_ref
*ref
= NULL
;
56 for (i
= 0; node
->iterate_referring (i
, ref
); i
++)
57 if (ref
->use
== IPA_REF_ADDR
)
62 /* Look for all functions inlined to NODE and update their inlined_to pointers
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
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)
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
))
100 /* Process references. */
103 process_references (symtab_node
*snode
,
105 bool before_inlining_p
,
106 struct pointer_set_t
*reachable
)
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
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
141 walk_polymorphic_call_targets (pointer_set_t
*reachable_call_targets
,
142 struct cgraph_edge
*edge
,
144 pointer_set_t
*reachable
, bool before_inlining_p
)
149 vec
<cgraph_node
*>targets
150 = possible_polymorphic_call_targets
151 (edge
, &final
, &cache_token
);
153 if (!pointer_set_insert (reachable_call_targets
,
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
163 if (TREE_CODE (TREE_TYPE (n
->decl
)) == METHOD_TYPE
164 && type_in_anonymous_namespace_p
165 (method_class_type (TREE_TYPE (n
->decl
))))
168 /* Prior inlining, keep alive bodies of possible targets for
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. */
190 if (targets
.length () <= 1 && dbg_cnt (devirt
))
192 cgraph_node
*target
, *node
= edge
->caller
;
193 if (targets
.length () == 1)
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
,
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
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. */
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 if (optimize
&& flag_devirtualize
)
285 build_type_inheritance_graph ();
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
);
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;
302 && !node
->global
.inlined_to
303 && !node
->in_other_partition
304 && !cgraph_can_remove_if_no_direct_calls_and_refs_p (node
))
306 gcc_assert (!node
->global
.inlined_to
);
307 pointer_set_insert (reachable
, node
);
308 enqueue_node (node
, &first
, reachable
);
311 gcc_assert (!node
->aux
);
314 /* Mark variables that are obviously needed. */
315 FOR_EACH_DEFINED_VARIABLE (vnode
)
316 if (!varpool_can_remove_if_no_refs (vnode
)
317 && !vnode
->in_other_partition
)
319 pointer_set_insert (reachable
, vnode
);
320 enqueue_node (vnode
, &first
, reachable
);
323 /* Perform reachability analysis. */
324 while (first
!= (symtab_node
*) (void *) 1)
326 bool in_boundary_p
= !pointer_set_contains (reachable
, 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. */
334 node
->aux
= (void *)2;
337 if (TREE_CODE (node
->decl
) == FUNCTION_DECL
338 && DECL_ABSTRACT_ORIGIN (node
->decl
))
340 struct cgraph_node
*origin_node
341 = cgraph_get_create_node (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
)
352 for (next
= node
->same_comdat_group
;
354 next
= next
->same_comdat_group
)
355 if (!symtab_comdat_local_p (next
)
356 && !pointer_set_insert (reachable
, 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. */
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
,
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
)
390 || before_inlining_p
))
392 /* Be sure that we will not optimize out alias target
394 if (DECL_EXTERNAL (e
->callee
->decl
)
396 && before_inlining_p
)
398 pointer_set_insert (reachable
,
399 cgraph_function_node (e
->callee
));
401 pointer_set_insert (reachable
, e
->callee
);
403 enqueue_node (e
->callee
, &first
, reachable
);
406 /* When inline clone exists, mark body to be preserved so when removing
407 offline copy of the function we don't kill it. */
408 if (cnode
->global
.inlined_to
)
409 pointer_set_insert (body_needed_for_clonning
, cnode
->decl
);
411 /* For non-inline clones, force their origins to the boundary and ensure
412 that body is not removed. */
413 while (cnode
->clone_of
)
415 bool noninline
= cnode
->clone_of
->decl
!= cnode
->decl
;
416 cnode
= cnode
->clone_of
;
419 pointer_set_insert (body_needed_for_clonning
, cnode
->decl
);
420 enqueue_node (cnode
, &first
, reachable
);
425 /* If any reachable function has simd clones, mark them as
426 reachable as well. */
427 if (cnode
->simd_clones
)
430 for (next
= cnode
->simd_clones
;
432 next
= next
->simdclone
->next_clone
)
434 || !pointer_set_insert (reachable
, next
))
435 enqueue_node (next
, &first
, reachable
);
438 /* When we see constructor of external variable, keep referred nodes in the
439 boundary. This will also hold initializers of the external vars NODE
441 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
);
443 && DECL_EXTERNAL (node
->decl
)
447 struct ipa_ref
*ref
= NULL
;
448 for (int i
= 0; node
->iterate_reference (i
, ref
); i
++)
449 enqueue_node (ref
->referred
, &first
, reachable
);
453 /* Remove unreachable functions. */
454 for (node
= cgraph_first_function (); node
; node
= next
)
456 next
= cgraph_next_function (node
);
458 /* If node is not needed at all, remove it. */
462 fprintf (file
, " %s/%i", node
->name (), node
->order
);
463 cgraph_remove_node (node
);
466 /* If node is unreachable, remove its body. */
467 else if (!pointer_set_contains (reachable
, node
))
469 if (!pointer_set_contains (body_needed_for_clonning
, node
->decl
))
470 cgraph_release_function_body (node
);
471 else if (!node
->clone_of
)
472 gcc_assert (in_lto_p
|| DECL_RESULT (node
->decl
));
473 if (node
->definition
)
476 fprintf (file
, " %s/%i", node
->name (), node
->order
);
477 node
->body_removed
= true;
478 node
->analyzed
= false;
479 node
->definition
= false;
480 node
->cpp_implicit_alias
= false;
482 node
->thunk
.thunk_p
= false;
483 node
->weakref
= false;
484 /* After early inlining we drop always_inline attributes on
485 bodies of functions that are still referenced (have their
487 DECL_ATTRIBUTES (node
->decl
)
488 = remove_attribute ("always_inline",
489 DECL_ATTRIBUTES (node
->decl
));
490 if (!node
->in_other_partition
)
491 node
->local
.local
= false;
492 cgraph_node_remove_callees (node
);
493 symtab_remove_from_same_comdat_group (node
);
494 node
->remove_all_references ();
499 gcc_assert (node
->clone_of
|| !cgraph_function_with_gimple_body_p (node
)
500 || in_lto_p
|| DECL_RESULT (node
->decl
));
503 /* Inline clones might be kept around so their materializing allows further
504 cloning. If the function the clone is inlined into is removed, we need
505 to turn it into normal cone. */
506 FOR_EACH_FUNCTION (node
)
508 if (node
->global
.inlined_to
511 gcc_assert (node
->clones
);
512 node
->global
.inlined_to
= NULL
;
513 update_inlined_to_pointer (node
, node
);
518 /* Remove unreachable variables. */
520 fprintf (file
, "\nReclaiming variables:");
521 for (vnode
= varpool_first_variable (); vnode
; vnode
= vnext
)
523 vnext
= varpool_next_variable (vnode
);
525 /* For can_refer_decl_in_current_unit_p we want to track for
526 all external variables if they are defined in other partition
528 && (!flag_ltrans
|| !DECL_EXTERNAL (vnode
->decl
)))
531 fprintf (file
, " %s/%i", vnode
->name (), vnode
->order
);
532 varpool_remove_node (vnode
);
535 else if (!pointer_set_contains (reachable
, vnode
))
538 if (vnode
->definition
)
541 fprintf (file
, " %s", vnode
->name ());
544 vnode
->body_removed
= true;
545 vnode
->definition
= false;
546 vnode
->analyzed
= false;
549 symtab_remove_from_same_comdat_group (vnode
);
551 /* Keep body if it may be useful for constant folding. */
552 if ((init
= ctor_for_folding (vnode
->decl
)) == error_mark_node
)
553 varpool_remove_initializer (vnode
);
555 DECL_INITIAL (vnode
->decl
) = init
;
556 vnode
->remove_all_references ();
562 pointer_set_destroy (reachable
);
563 pointer_set_destroy (body_needed_for_clonning
);
564 pointer_set_destroy (reachable_call_targets
);
566 /* Now update address_taken flags and try to promote functions to be local. */
568 fprintf (file
, "\nClearing address taken flags:");
569 FOR_EACH_DEFINED_FUNCTION (node
)
570 if (node
->address_taken
571 && !node
->used_from_other_partition
)
573 if (!cgraph_for_node_and_aliases (node
, has_addr_references_p
, NULL
, true))
576 fprintf (file
, " %s", node
->name ());
577 node
->address_taken
= false;
579 if (cgraph_local_node_p (node
))
581 node
->local
.local
= true;
583 fprintf (file
, " (local)");
588 fprintf (file
, "\n");
590 #ifdef ENABLE_CHECKING
594 /* If we removed something, perhaps profile could be improved. */
595 if (changed
&& optimize
&& inline_edge_summary_vec
.exists ())
596 FOR_EACH_DEFINED_FUNCTION (node
)
597 ipa_propagate_frequency (node
);
599 timevar_pop (TV_IPA_UNREACHABLE
);
603 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
604 as needed, also clear EXPLICIT_REFS if the references to given variable
605 do not need to be explicit. */
608 process_references (varpool_node
*vnode
,
609 bool *written
, bool *address_taken
,
610 bool *read
, bool *explicit_refs
)
615 if (!varpool_all_refs_explicit_p (vnode
)
616 || TREE_THIS_VOLATILE (vnode
->decl
))
617 *explicit_refs
= false;
619 for (i
= 0; vnode
->iterate_referring (i
, ref
)
620 && *explicit_refs
&& (!*written
|| !*address_taken
|| !*read
); i
++)
624 *address_taken
= true;
633 process_references (varpool (ref
->referring
), written
, address_taken
,
634 read
, explicit_refs
);
639 /* Set TREE_READONLY bit. */
642 set_readonly_bit (varpool_node
*vnode
, void *data ATTRIBUTE_UNUSED
)
644 TREE_READONLY (vnode
->decl
) = true;
648 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
651 set_writeonly_bit (varpool_node
*vnode
, void *data ATTRIBUTE_UNUSED
)
653 vnode
->writeonly
= true;
656 DECL_INITIAL (vnode
->decl
) = NULL
;
658 vnode
->remove_all_references ();
663 /* Clear addressale bit of VNODE. */
666 clear_addressable_bit (varpool_node
*vnode
, void *data ATTRIBUTE_UNUSED
)
668 vnode
->address_taken
= false;
669 TREE_ADDRESSABLE (vnode
->decl
) = 0;
673 /* Discover variables that have no longer address taken or that are read only
674 and update their flags.
676 FIXME: This can not be done in between gimplify and omp_expand since
677 readonly flag plays role on what is shared and what is not. Currently we do
678 this transformation as part of whole program visibility and re-do at
679 ipa-reference pass (to take into account clonning), but it would
680 make sense to do it before early optimizations. */
683 ipa_discover_readonly_nonaddressable_vars (void)
687 fprintf (dump_file
, "Clearing variable flags:");
688 FOR_EACH_VARIABLE (vnode
)
690 && (TREE_ADDRESSABLE (vnode
->decl
)
692 || !TREE_READONLY (vnode
->decl
)))
694 bool written
= false;
695 bool address_taken
= false;
697 bool explicit_refs
= true;
699 process_references (vnode
, &written
, &address_taken
, &read
, &explicit_refs
);
704 if (TREE_ADDRESSABLE (vnode
->decl
) && dump_file
)
705 fprintf (dump_file
, " %s (non-addressable)", vnode
->name ());
706 varpool_for_node_and_aliases (vnode
, clear_addressable_bit
, NULL
, true);
708 if (!address_taken
&& !written
709 /* Making variable in explicit section readonly can cause section
711 See e.g. gcc.c-torture/compile/pr23237.c */
712 && vnode
->get_section () == NULL
)
714 if (!TREE_READONLY (vnode
->decl
) && dump_file
)
715 fprintf (dump_file
, " %s (read-only)", vnode
->name ());
716 varpool_for_node_and_aliases (vnode
, set_readonly_bit
, NULL
, true);
718 if (!vnode
->writeonly
&& !read
&& !address_taken
&& written
)
721 fprintf (dump_file
, " %s (write-only)", vnode
->name ());
722 varpool_for_node_and_aliases (vnode
, set_writeonly_bit
, NULL
, true);
726 fprintf (dump_file
, "\n");
729 /* Free inline summary. */
733 const pass_data pass_data_ipa_free_inline_summary
=
735 SIMPLE_IPA_PASS
, /* type */
736 "*free_inline_summary", /* name */
737 OPTGROUP_NONE
, /* optinfo_flags */
738 true, /* has_execute */
739 TV_IPA_FREE_INLINE_SUMMARY
, /* tv_id */
740 0, /* properties_required */
741 0, /* properties_provided */
742 0, /* properties_destroyed */
743 0, /* todo_flags_start */
744 0, /* todo_flags_finish */
747 class pass_ipa_free_inline_summary
: public simple_ipa_opt_pass
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 ();
761 }; // class pass_ipa_free_inline_summary
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
780 cgraph_build_static_cdtor_1 (char which
, tree body
, int priority
, bool final
)
782 static int counter
= 0;
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
790 sprintf (which_buf
, "%c_%.5d_%d", which
, priority
, counter
++);
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
;
830 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
831 decl_init_priority_insert (decl
, priority
);
834 DECL_STATIC_DESTRUCTOR (decl
) = 1;
835 decl_fini_priority_insert (decl
, priority
);
841 gimplify_function_tree (decl
);
843 cgraph_add_new_function (decl
, false);
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. */
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. */
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_get_node (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. */
889 build_cdtor (bool ctor_p
, vec
<tree
> cdtors
)
892 size_t len
= cdtors
.length ();
899 priority_type priority
;
908 p
= ctor_p
? DECL_INIT_PRIORITY (fn
) : DECL_FINI_PRIORITY (fn
);
911 else if (p
!= priority
)
917 /* When there is only one cdtor and target supports them, do nothing. */
919 && targetm
.have_ctors_dtors
)
924 /* Find the next batch of constructors/destructors with the same
925 initialization priority. */
930 call
= build_call_expr (fn
, 0);
932 DECL_STATIC_CONSTRUCTOR (fn
) = 0;
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
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. */
953 compare_ctor (const void *p1
, const void *p2
)
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
)
967 else if (priority1
> priority2
)
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. */
980 compare_dtor (const void *p1
, const void *p2
)
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
)
994 else if (priority1
> priority2
)
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. */
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
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
);
1038 static_ctors
.release ();
1039 static_dtors
.release ();
1045 const pass_data pass_data_ipa_cdtor_merge
=
1047 IPA_PASS
, /* type */
1049 OPTGROUP_NONE
, /* optinfo_flags */
1050 true, /* has_execute */
1051 TV_CGRAPHOPT
, /* tv_id */
1052 0, /* properties_required */
1053 0, /* properties_provided */
1054 0, /* properties_destroyed */
1055 0, /* todo_flags_start */
1056 0, /* todo_flags_finish */
1059 class pass_ipa_cdtor_merge
: public ipa_opt_pass_d
1062 pass_ipa_cdtor_merge (gcc::context
*ctxt
)
1063 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge
, ctxt
,
1064 NULL
, /* generate_summary */
1065 NULL
, /* write_summary */
1066 NULL
, /* read_summary */
1067 NULL
, /* write_optimization_summary */
1068 NULL
, /* read_optimization_summary */
1069 NULL
, /* stmt_fixup */
1070 0, /* function_transform_todo_flags_start */
1071 NULL
, /* function_transform */
1072 NULL
) /* variable_transform */
1075 /* opt_pass methods: */
1076 virtual bool gate (function
*);
1077 virtual unsigned int execute (function
*) { return ipa_cdtor_merge (); }
1079 }; // class pass_ipa_cdtor_merge
1082 pass_ipa_cdtor_merge::gate (function
*)
1084 /* Perform the pass when we have no ctors/dtors support
1085 or at LTO time to merge multiple constructors into single
1087 return !targetm
.have_ctors_dtors
|| (optimize
&& in_lto_p
);
1093 make_pass_ipa_cdtor_merge (gcc::context
*ctxt
)
1095 return new pass_ipa_cdtor_merge (ctxt
);
1098 /* Invalid pointer representing BOTTOM for single user dataflow. */
1099 #define BOTTOM ((cgraph_node *)(size_t) 2)
1101 /* Meet operation for single user dataflow.
1102 Here we want to associate variables with sigle function that may access it.
1104 FUNCTION is current single user of a variable, VAR is variable that uses it.
1105 Latttice is stored in SINGLE_USER_MAP.
1108 - TOP by no entry in SIGNLE_USER_MAP
1109 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1110 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1113 meet (cgraph_node
*function
, varpool_node
*var
,
1114 hash_map
<varpool_node
*, cgraph_node
*> &single_user_map
)
1116 struct cgraph_node
*user
, **f
;
1118 if (var
->aux
== BOTTOM
)
1121 f
= single_user_map
.get (var
);
1127 else if (function
!= user
)
1133 /* Propagation step of single-use dataflow.
1135 Check all uses of VNODE and see if they are used by single function FUNCTION.
1136 SINGLE_USER_MAP represents the dataflow lattice. */
1139 propagate_single_user (varpool_node
*vnode
, cgraph_node
*function
,
1140 hash_map
<varpool_node
*, cgraph_node
*> &single_user_map
)
1143 struct ipa_ref
*ref
;
1145 gcc_assert (!vnode
->externally_visible
);
1147 /* If node is an alias, first meet with its target. */
1149 function
= meet (function
, varpool_alias_target (vnode
), single_user_map
);
1151 /* Check all users and see if they correspond to a single function. */
1153 vnode
->iterate_referring (i
, ref
)
1154 && function
!= BOTTOM
; i
++)
1156 struct cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (ref
->referring
);
1159 if (cnode
->global
.inlined_to
)
1160 cnode
= cnode
->global
.inlined_to
;
1163 else if (function
!= cnode
)
1167 function
= meet (function
, dyn_cast
<varpool_node
*> (ref
->referring
), single_user_map
);
1172 /* Pass setting used_by_single_function flag.
1173 This flag is set on variable when there is only one function that may possibly
1177 ipa_single_use (void)
1179 varpool_node
*first
= (varpool_node
*) (void *) 1;
1181 hash_map
<varpool_node
*, cgraph_node
*> single_user_map
;
1183 FOR_EACH_DEFINED_VARIABLE (var
)
1184 if (!varpool_all_refs_explicit_p (var
))
1188 /* Enqueue symbol for dataflow. */
1193 /* The actual dataflow. */
1195 while (first
!= (void *) 1)
1197 cgraph_node
*user
, *orig_user
, **f
;
1200 first
= (varpool_node
*)first
->aux
;
1202 f
= single_user_map
.get (var
);
1207 user
= propagate_single_user (var
, orig_user
, single_user_map
);
1209 gcc_checking_assert (var
->aux
!= BOTTOM
);
1211 /* If user differs, enqueue all references. */
1212 if (user
!= orig_user
)
1217 single_user_map
.put (var
, user
);
1219 /* Enqueue all aliases for re-processing. */
1221 var
->iterate_referring (i
, ref
); i
++)
1222 if (ref
->use
== IPA_REF_ALIAS
1223 && !ref
->referring
->aux
)
1225 ref
->referring
->aux
= first
;
1226 first
= dyn_cast
<varpool_node
*> (ref
->referring
);
1228 /* Enqueue all users for re-processing. */
1230 var
->iterate_reference (i
, ref
); i
++)
1231 if (!ref
->referred
->aux
1232 && ref
->referred
->definition
1233 && is_a
<varpool_node
*> (ref
->referred
))
1235 ref
->referred
->aux
= first
;
1236 first
= dyn_cast
<varpool_node
*> (ref
->referred
);
1239 /* If user is BOTTOM, just punt on this var. */
1249 FOR_EACH_DEFINED_VARIABLE (var
)
1251 if (var
->aux
!= BOTTOM
)
1253 #ifdef ENABLE_CHECKING
1254 if (!single_user_map
.get (var
))
1255 gcc_assert (single_user_map
.get (var
));
1259 fprintf (dump_file
, "Variable %s/%i is used by single function\n",
1260 var
->name (), var
->order
);
1262 var
->used_by_single_function
= true;
1271 const pass_data pass_data_ipa_single_use
=
1273 IPA_PASS
, /* type */
1274 "single-use", /* name */
1275 OPTGROUP_NONE
, /* optinfo_flags */
1276 true, /* has_execute */
1277 TV_CGRAPHOPT
, /* tv_id */
1278 0, /* properties_required */
1279 0, /* properties_provided */
1280 0, /* properties_destroyed */
1281 0, /* todo_flags_start */
1282 0, /* todo_flags_finish */
1285 class pass_ipa_single_use
: public ipa_opt_pass_d
1288 pass_ipa_single_use (gcc::context
*ctxt
)
1289 : ipa_opt_pass_d (pass_data_ipa_single_use
, ctxt
,
1290 NULL
, /* generate_summary */
1291 NULL
, /* write_summary */
1292 NULL
, /* read_summary */
1293 NULL
, /* write_optimization_summary */
1294 NULL
, /* read_optimization_summary */
1295 NULL
, /* stmt_fixup */
1296 0, /* function_transform_todo_flags_start */
1297 NULL
, /* function_transform */
1298 NULL
) /* variable_transform */
1301 /* opt_pass methods: */
1302 virtual bool gate (function
*);
1303 virtual unsigned int execute (function
*) { return ipa_single_use (); }
1305 }; // class pass_ipa_single_use
1308 pass_ipa_single_use::gate (function
*)
1316 make_pass_ipa_single_use (gcc::context
*ctxt
)
1318 return new pass_ipa_single_use (ctxt
);