1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003-2019 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"
27 #include "alloc-pool.h"
28 #include "tree-pass.h"
29 #include "stringpool.h"
32 #include "tree-iterator.h"
33 #include "ipa-utils.h"
34 #include "symbol-summary.h"
37 #include "ipa-fnsummary.h"
40 #include "stringpool.h"
43 /* Return true when NODE has ADDR reference. */
46 has_addr_references_p (struct cgraph_node
*node
,
50 struct ipa_ref
*ref
= NULL
;
52 for (i
= 0; node
->iterate_referring (i
, ref
); i
++)
53 if (ref
->use
== IPA_REF_ADDR
)
58 /* Return true when NODE can be target of an indirect call. */
61 is_indirect_call_target_p (struct cgraph_node
*node
, void *)
63 return node
->indirect_call_target
;
66 /* Look for all functions inlined to NODE and update their inlined_to pointers
70 update_inlined_to_pointer (struct cgraph_node
*node
, struct cgraph_node
*inlined_to
)
72 struct cgraph_edge
*e
;
73 for (e
= node
->callees
; e
; e
= e
->next_callee
)
74 if (e
->callee
->inlined_to
)
76 e
->callee
->inlined_to
= inlined_to
;
77 update_inlined_to_pointer (e
->callee
, inlined_to
);
81 /* Add symtab NODE to queue starting at FIRST.
83 The queue is linked via AUX pointers and terminated by pointer to 1.
84 We enqueue nodes at two occasions: when we find them reachable or when we find
85 their bodies needed for further clonning. In the second case we mark them
86 by pointer to 2 after processing so they are re-queue when they become
90 enqueue_node (symtab_node
*node
, symtab_node
**first
,
91 hash_set
<symtab_node
*> *reachable
)
93 /* Node is still in queue; do nothing. */
94 if (node
->aux
&& node
->aux
!= (void *) 2)
96 /* Node was already processed as unreachable, re-enqueue
97 only if it became reachable now. */
98 if (node
->aux
== (void *)2 && !reachable
->contains (node
))
104 /* Return true if NODE may get inlined later.
105 This is used to keep DECL_EXTERNAL function bodies around long enough
106 so inliner can proces them. */
109 possible_inline_candidate_p (symtab_node
*node
)
111 if (symtab
->state
>= IPA_SSA_AFTER_INLINING
)
113 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
116 if (DECL_UNINLINABLE (cnode
->decl
))
118 if (opt_for_fn (cnode
->decl
, optimize
))
120 if (symtab
->state
>= IPA_SSA
)
122 return lookup_attribute ("always_inline", DECL_ATTRIBUTES (node
->decl
));
125 /* Process references. */
128 process_references (symtab_node
*snode
,
130 hash_set
<symtab_node
*> *reachable
)
133 struct ipa_ref
*ref
= NULL
;
134 for (i
= 0; snode
->iterate_reference (i
, ref
); i
++)
136 symtab_node
*node
= ref
->referred
;
137 symtab_node
*body
= node
->ultimate_alias_target ();
139 if (node
->definition
&& !node
->in_other_partition
140 && ((!DECL_EXTERNAL (node
->decl
) || node
->alias
)
141 || (possible_inline_candidate_p (node
)
142 /* We use variable constructors during late compilation for
143 constant folding. Keep references alive so partitioning
144 knows about potential references. */
145 || (VAR_P (node
->decl
)
147 || flag_incremental_link
148 == INCREMENTAL_LINK_LTO
)
149 && dyn_cast
<varpool_node
*> (node
)
150 ->ctor_useable_for_folding_p ()))))
152 /* Be sure that we will not optimize out alias target
154 if (DECL_EXTERNAL (node
->decl
)
156 && symtab
->state
< IPA_SSA_AFTER_INLINING
)
157 reachable
->add (body
);
158 reachable
->add (node
);
160 enqueue_node (node
, first
, reachable
);
164 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
165 all its potential targets as reachable to permit later inlining if
166 devirtualization happens. After inlining still keep their declarations
167 around, so we can devirtualize to a direct call.
169 Also try to make trivial devirutalization when no or only one target is
173 walk_polymorphic_call_targets (hash_set
<void *> *reachable_call_targets
,
174 struct cgraph_edge
*edge
,
176 hash_set
<symtab_node
*> *reachable
)
181 vec
<cgraph_node
*>targets
182 = possible_polymorphic_call_targets
183 (edge
, &final
, &cache_token
);
185 if (!reachable_call_targets
->add (cache_token
))
187 for (i
= 0; i
< targets
.length (); i
++)
189 struct cgraph_node
*n
= targets
[i
];
191 /* Do not bother to mark virtual methods in anonymous namespace;
192 either we will find use of virtual table defining it, or it is
194 if (TREE_CODE (TREE_TYPE (n
->decl
)) == METHOD_TYPE
195 && type_in_anonymous_namespace_p
196 (TYPE_METHOD_BASETYPE (TREE_TYPE (n
->decl
))))
199 n
->indirect_call_target
= true;
200 symtab_node
*body
= n
->function_symbol ();
202 /* Prior inlining, keep alive bodies of possible targets for
205 && (possible_inline_candidate_p (body
)
206 && opt_for_fn (body
->decl
, flag_devirtualize
)))
208 /* Be sure that we will not optimize out alias target
210 if (DECL_EXTERNAL (n
->decl
)
212 && symtab
->state
< IPA_SSA_AFTER_INLINING
)
213 reachable
->add (body
);
216 /* Even after inlining we want to keep the possible targets in the
217 boundary, so late passes can still produce direct call even if
218 the chance for inlining is lost. */
219 enqueue_node (n
, first
, reachable
);
223 /* Very trivial devirtualization; when the type is
224 final or anonymous (so we know all its derivation)
225 and there is only one possible virtual call target,
226 make the edge direct. */
229 if (targets
.length () <= 1 && dbg_cnt (devirt
))
231 cgraph_node
*target
, *node
= edge
->caller
;
232 if (targets
.length () == 1)
235 target
= cgraph_node::get_create
236 (builtin_decl_implicit (BUILT_IN_UNREACHABLE
));
238 if (dump_enabled_p ())
240 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, edge
->call_stmt
,
241 "devirtualizing call in %s to %s\n",
242 edge
->caller
->dump_name (),
243 target
->dump_name ());
245 edge
= edge
->make_direct (target
);
246 if (ipa_fn_summaries
)
247 ipa_update_overall_fn_summary (node
);
248 else if (edge
->call_stmt
)
249 edge
->redirect_call_stmt_to_callee ();
254 /* Perform reachability analysis and reclaim all unreachable nodes.
256 The algorithm is basically mark&sweep but with some extra refinements:
258 - reachable extern inline functions needs special handling; the bodies needs
259 to stay in memory until inlining in hope that they will be inlined.
260 After inlining we release their bodies and turn them into unanalyzed
261 nodes even when they are reachable.
263 - virtual functions are kept in callgraph even if they seem unreachable in
264 hope calls to them will be devirtualized.
266 Again we remove them after inlining. In late optimization some
267 devirtualization may happen, but it is not important since we won't inline
268 the call. In theory early opts and IPA should work out all important cases.
270 - virtual clones needs bodies of their origins for later materialization;
271 this means that we want to keep the body even if the origin is unreachable
272 otherwise. To avoid origin from sitting in the callgraph and being
273 walked by IPA passes, we turn them into unanalyzed nodes with body
276 We maintain set of function declaration where body needs to stay in
277 body_needed_for_clonning
279 Inline clones represent special case: their declaration match the
280 declaration of origin and cgraph_remove_node already knows how to
281 reshape callgraph and preserve body when offline copy of function or
282 inline clone is being removed.
284 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
285 variables with DECL_INITIAL set. We finalize these and keep reachable
286 ones around for constant folding purposes. After inlining we however
287 stop walking their references to let everything static referneced by them
288 to be removed when it is otherwise unreachable.
290 We maintain queue of both reachable symbols (i.e. defined symbols that needs
291 to stay) and symbols that are in boundary (i.e. external symbols referenced
292 by reachable symbols or origins of clones). The queue is represented
293 as linked list by AUX pointer terminated by 1.
295 At the end we keep all reachable symbols. For symbols in boundary we always
296 turn definition into a declaration, but we may keep function body around
297 based on body_needed_for_clonning
299 All symbols that enter the queue have AUX pointer non-zero and are in the
300 boundary. Pointer set REACHABLE is used to track reachable symbols.
302 Every symbol can be visited twice - once as part of boundary and once
303 as real reachable symbol. enqueue_node needs to decide whether the
304 node needs to be re-queued for second processing. For this purpose
305 we set AUX pointer of processed symbols in the boundary to constant 2. */
308 symbol_table::remove_unreachable_nodes (FILE *file
)
310 symtab_node
*first
= (symtab_node
*) (void *) 1;
311 struct cgraph_node
*node
, *next
;
312 varpool_node
*vnode
, *vnext
;
313 bool changed
= false;
314 hash_set
<symtab_node
*> reachable
;
315 hash_set
<tree
> body_needed_for_clonning
;
316 hash_set
<void *> reachable_call_targets
;
318 timevar_push (TV_IPA_UNREACHABLE
);
319 build_type_inheritance_graph ();
321 fprintf (file
, "\nReclaiming functions:");
324 FOR_EACH_FUNCTION (node
)
325 gcc_assert (!node
->aux
);
326 FOR_EACH_VARIABLE (vnode
)
327 gcc_assert (!vnode
->aux
);
329 /* Mark functions whose bodies are obviously needed.
330 This is mostly when they can be referenced externally. Inline clones
331 are special since their declarations are shared with master clone and thus
332 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
333 FOR_EACH_FUNCTION (node
)
335 node
->used_as_abstract_origin
= false;
336 node
->indirect_call_target
= false;
339 && !node
->in_other_partition
340 && !node
->can_remove_if_no_direct_calls_and_refs_p ())
342 gcc_assert (!node
->inlined_to
);
343 reachable
.add (node
);
344 enqueue_node (node
, &first
, &reachable
);
347 gcc_assert (!node
->aux
);
350 /* Mark variables that are obviously needed. */
351 FOR_EACH_DEFINED_VARIABLE (vnode
)
352 if (!vnode
->can_remove_if_no_refs_p()
353 && !vnode
->in_other_partition
)
355 reachable
.add (vnode
);
356 enqueue_node (vnode
, &first
, &reachable
);
359 /* Perform reachability analysis. */
360 while (first
!= (symtab_node
*) (void *) 1)
362 bool in_boundary_p
= !reachable
.contains (first
);
363 symtab_node
*node
= first
;
365 first
= (symtab_node
*)first
->aux
;
367 /* If we are processing symbol in boundary, mark its AUX pointer for
368 possible later re-processing in enqueue_node. */
371 node
->aux
= (void *)2;
372 if (node
->alias
&& node
->analyzed
)
373 enqueue_node (node
->get_alias_target (), &first
, &reachable
);
377 if (TREE_CODE (node
->decl
) == FUNCTION_DECL
378 && DECL_ABSTRACT_ORIGIN (node
->decl
))
380 struct cgraph_node
*origin_node
381 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node
->decl
));
382 if (origin_node
&& !origin_node
->used_as_abstract_origin
)
384 origin_node
->used_as_abstract_origin
= true;
385 gcc_assert (!origin_node
->prev_sibling_clone
);
386 gcc_assert (!origin_node
->next_sibling_clone
);
387 for (cgraph_node
*n
= origin_node
->clones
; n
;
388 n
= n
->next_sibling_clone
)
389 if (n
->decl
== DECL_ABSTRACT_ORIGIN (node
->decl
))
390 n
->used_as_abstract_origin
= true;
393 /* If any symbol in a comdat group is reachable, force
394 all externally visible symbols in the same comdat
395 group to be reachable as well. Comdat-local symbols
396 can be discarded if all uses were inlined. */
397 if (node
->same_comdat_group
)
400 for (next
= node
->same_comdat_group
;
402 next
= next
->same_comdat_group
)
403 if (!next
->comdat_local_p ()
404 && !reachable
.add (next
))
405 enqueue_node (next
, &first
, &reachable
);
407 /* Mark references as reachable. */
408 process_references (node
, &first
, &reachable
);
411 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
413 /* Mark the callees reachable unless they are direct calls to extern
414 inline functions we decided to not inline. */
417 struct cgraph_edge
*e
;
418 /* Keep alive possible targets for devirtualization. */
419 if (opt_for_fn (cnode
->decl
, optimize
)
420 && opt_for_fn (cnode
->decl
, flag_devirtualize
))
422 struct cgraph_edge
*next
;
423 for (e
= cnode
->indirect_calls
; e
; e
= next
)
425 next
= e
->next_callee
;
426 if (e
->indirect_info
->polymorphic
)
427 walk_polymorphic_call_targets (&reachable_call_targets
,
428 e
, &first
, &reachable
);
431 for (e
= cnode
->callees
; e
; e
= e
->next_callee
)
433 symtab_node
*body
= e
->callee
->function_symbol ();
434 if (e
->callee
->definition
435 && !e
->callee
->in_other_partition
436 && (!e
->inline_failed
437 || !DECL_EXTERNAL (e
->callee
->decl
)
439 || possible_inline_candidate_p (e
->callee
)))
441 /* Be sure that we will not optimize out alias target
443 if (DECL_EXTERNAL (e
->callee
->decl
)
445 && symtab
->state
< IPA_SSA_AFTER_INLINING
)
446 reachable
.add (body
);
447 reachable
.add (e
->callee
);
449 enqueue_node (e
->callee
, &first
, &reachable
);
452 /* When inline clone exists, mark body to be preserved so when removing
453 offline copy of the function we don't kill it. */
454 if (cnode
->inlined_to
)
455 body_needed_for_clonning
.add (cnode
->decl
);
457 /* For non-inline clones, force their origins to the boundary and ensure
458 that body is not removed. */
459 while (cnode
->clone_of
)
461 bool noninline
= cnode
->clone_of
->decl
!= cnode
->decl
;
462 cnode
= cnode
->clone_of
;
465 body_needed_for_clonning
.add (cnode
->decl
);
466 enqueue_node (cnode
, &first
, &reachable
);
471 else if (cnode
->thunk
.thunk_p
)
472 enqueue_node (cnode
->callees
->callee
, &first
, &reachable
);
474 /* If any reachable function has simd clones, mark them as
475 reachable as well. */
476 if (cnode
->simd_clones
)
479 for (next
= cnode
->simd_clones
;
481 next
= next
->simdclone
->next_clone
)
483 || !reachable
.add (next
))
484 enqueue_node (next
, &first
, &reachable
);
487 /* When we see constructor of external variable, keep referred nodes in the
488 boundary. This will also hold initializers of the external vars NODE
490 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
);
492 && DECL_EXTERNAL (node
->decl
)
496 struct ipa_ref
*ref
= NULL
;
497 for (int i
= 0; node
->iterate_reference (i
, ref
); i
++)
498 enqueue_node (ref
->referred
, &first
, &reachable
);
502 /* Remove unreachable functions. */
503 for (node
= first_function (); node
; node
= next
)
505 next
= next_function (node
);
507 /* If node is not needed at all, remove it. */
511 fprintf (file
, " %s", node
->dump_name ());
515 /* If node is unreachable, remove its body. */
516 else if (!reachable
.contains (node
))
518 /* We keep definitions of thunks and aliases in the boundary so
519 we can walk to the ultimate alias targets and function symbols
521 if (node
->alias
|| node
->thunk
.thunk_p
)
523 else if (!body_needed_for_clonning
.contains (node
->decl
))
525 /* Make the node a non-clone so that we do not attempt to
526 materialize it later. */
528 node
->remove_from_clone_tree ();
529 node
->release_body ();
531 else if (!node
->clone_of
)
532 gcc_assert (in_lto_p
|| DECL_RESULT (node
->decl
));
533 if (node
->definition
&& !node
->alias
&& !node
->thunk
.thunk_p
)
536 fprintf (file
, " %s", node
->dump_name ());
537 node
->body_removed
= true;
538 node
->analyzed
= false;
539 node
->definition
= false;
540 node
->cpp_implicit_alias
= false;
542 node
->transparent_alias
= false;
543 node
->thunk
.thunk_p
= false;
544 node
->weakref
= false;
545 /* After early inlining we drop always_inline attributes on
546 bodies of functions that are still referenced (have their
548 DECL_ATTRIBUTES (node
->decl
)
549 = remove_attribute ("always_inline",
550 DECL_ATTRIBUTES (node
->decl
));
551 if (!node
->in_other_partition
)
553 node
->remove_callees ();
554 node
->remove_all_references ();
559 gcc_assert (node
->clone_of
|| !node
->has_gimple_body_p ()
560 || in_lto_p
|| DECL_RESULT (node
->decl
));
563 /* Inline clones might be kept around so their materializing allows further
564 cloning. If the function the clone is inlined into is removed, we need
565 to turn it into normal cone. */
566 FOR_EACH_FUNCTION (node
)
571 gcc_assert (node
->clones
);
572 node
->inlined_to
= NULL
;
573 update_inlined_to_pointer (node
, node
);
578 /* Remove unreachable variables. */
580 fprintf (file
, "\nReclaiming variables:");
581 for (vnode
= first_variable (); vnode
; vnode
= vnext
)
583 vnext
= next_variable (vnode
);
585 /* For can_refer_decl_in_current_unit_p we want to track for
586 all external variables if they are defined in other partition
588 && (!flag_ltrans
|| !DECL_EXTERNAL (vnode
->decl
)))
590 struct ipa_ref
*ref
= NULL
;
592 /* First remove the aliases, so varpool::remove can possibly lookup
593 the constructor and save it for future use. */
594 while (vnode
->iterate_direct_aliases (0, ref
))
597 fprintf (file
, " %s", ref
->referred
->dump_name ());
598 ref
->referring
->remove ();
601 fprintf (file
, " %s", vnode
->dump_name ());
602 vnext
= next_variable (vnode
);
603 /* Signal removal to the debug machinery. */
604 if (! flag_wpa
|| flag_incremental_link
== INCREMENTAL_LINK_LTO
)
606 vnode
->definition
= false;
607 (*debug_hooks
->late_global_decl
) (vnode
->decl
);
612 else if (!reachable
.contains (vnode
) && !vnode
->alias
)
615 if (vnode
->definition
)
618 fprintf (file
, " %s", vnode
->name ());
621 /* Keep body if it may be useful for constant folding. */
622 if ((flag_wpa
|| flag_incremental_link
== INCREMENTAL_LINK_LTO
)
623 || ((init
= ctor_for_folding (vnode
->decl
)) == error_mark_node
))
624 vnode
->remove_initializer ();
626 DECL_INITIAL (vnode
->decl
) = init
;
627 vnode
->body_removed
= true;
628 vnode
->definition
= false;
629 vnode
->analyzed
= false;
632 vnode
->remove_from_same_comdat_group ();
634 vnode
->remove_all_references ();
640 /* Now update address_taken flags and try to promote functions to be local. */
642 fprintf (file
, "\nClearing address taken flags:");
643 FOR_EACH_DEFINED_FUNCTION (node
)
644 if (node
->address_taken
645 && !node
->used_from_other_partition
)
647 if (!node
->call_for_symbol_and_aliases
648 (has_addr_references_p
, NULL
, true))
651 fprintf (file
, " %s", node
->name ());
652 node
->address_taken
= false;
655 /* Virtual functions may be kept in cgraph just because
656 of possible later devirtualization. Do not mark them as
657 local too early so we won't optimize them out before
658 we are done with polymorphic call analysis. */
659 && (symtab
->state
>= IPA_SSA_AFTER_INLINING
660 || !node
->call_for_symbol_and_aliases
661 (is_indirect_call_target_p
, NULL
, true)))
665 fprintf (file
, " (local)");
670 fprintf (file
, "\n");
672 symtab_node::checking_verify_symtab_nodes ();
674 /* If we removed something, perhaps profile could be improved. */
675 if (changed
&& (optimize
|| in_lto_p
) && ipa_call_summaries
)
676 FOR_EACH_DEFINED_FUNCTION (node
)
677 ipa_propagate_frequency (node
);
679 timevar_pop (TV_IPA_UNREACHABLE
);
683 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
684 as needed, also clear EXPLICIT_REFS if the references to given variable
685 do not need to be explicit. */
688 process_references (varpool_node
*vnode
,
689 bool *written
, bool *address_taken
,
690 bool *read
, bool *explicit_refs
)
695 if (!vnode
->all_refs_explicit_p ()
696 || TREE_THIS_VOLATILE (vnode
->decl
))
697 *explicit_refs
= false;
699 for (i
= 0; vnode
->iterate_referring (i
, ref
)
700 && *explicit_refs
&& (!*written
|| !*address_taken
|| !*read
); i
++)
704 *address_taken
= true;
713 process_references (dyn_cast
<varpool_node
*> (ref
->referring
), written
,
714 address_taken
, read
, explicit_refs
);
719 /* Set TREE_READONLY bit. */
722 set_readonly_bit (varpool_node
*vnode
, void *data ATTRIBUTE_UNUSED
)
724 TREE_READONLY (vnode
->decl
) = true;
728 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
731 set_writeonly_bit (varpool_node
*vnode
, void *data
)
733 vnode
->writeonly
= true;
734 if (optimize
|| in_lto_p
)
736 DECL_INITIAL (vnode
->decl
) = NULL
;
739 if (vnode
->num_references ())
740 *(bool *)data
= true;
741 vnode
->remove_all_references ();
747 /* Clear addressale bit of VNODE. */
750 clear_addressable_bit (varpool_node
*vnode
, void *data ATTRIBUTE_UNUSED
)
752 vnode
->address_taken
= false;
753 TREE_ADDRESSABLE (vnode
->decl
) = 0;
757 /* Discover variables that have no longer address taken, are read-only or
758 write-only and update their flags.
760 Return true when unreachable symbol removal should be done.
762 FIXME: This cannot be done in between gimplify and omp_expand since
763 readonly flag plays role on what is shared and what is not. Currently we do
764 this transformation as part of whole program visibility and re-do at
765 ipa-reference pass (to take into account clonning), but it would
766 make sense to do it before early optimizations. */
769 ipa_discover_variable_flags (void)
771 if (!flag_ipa_reference_addressable
)
774 bool remove_p
= false;
777 fprintf (dump_file
, "Clearing variable flags:");
778 FOR_EACH_VARIABLE (vnode
)
780 && (TREE_ADDRESSABLE (vnode
->decl
)
782 || !TREE_READONLY (vnode
->decl
)))
784 bool written
= false;
785 bool address_taken
= false;
787 bool explicit_refs
= true;
789 process_references (vnode
, &written
, &address_taken
, &read
,
795 if (TREE_ADDRESSABLE (vnode
->decl
) && dump_file
)
796 fprintf (dump_file
, " %s (non-addressable)", vnode
->name ());
797 vnode
->call_for_symbol_and_aliases (clear_addressable_bit
, NULL
,
800 if (!address_taken
&& !written
801 /* Making variable in explicit section readonly can cause section
803 See e.g. gcc.c-torture/compile/pr23237.c */
804 && vnode
->get_section () == NULL
)
806 if (!TREE_READONLY (vnode
->decl
) && dump_file
)
807 fprintf (dump_file
, " %s (read-only)", vnode
->name ());
808 vnode
->call_for_symbol_and_aliases (set_readonly_bit
, NULL
, true);
810 if (!vnode
->writeonly
&& !read
&& !address_taken
&& written
)
813 fprintf (dump_file
, " %s (write-only)", vnode
->name ());
814 vnode
->call_for_symbol_and_aliases (set_writeonly_bit
, &remove_p
,
819 fprintf (dump_file
, "\n");
823 /* Generate and emit a static constructor or destructor. WHICH must
824 be one of 'I' (for a constructor), 'D' (for a destructor).
825 BODY is a STATEMENT_LIST containing GENERIC
826 statements. PRIORITY is the initialization priority for this
827 constructor or destructor.
829 FINAL specify whether the externally visible name for collect2 should
833 cgraph_build_static_cdtor_1 (char which
, tree body
, int priority
, bool final
,
837 static int counter
= 0;
839 tree decl
, name
, resdecl
;
841 /* The priority is encoded in the constructor or destructor name.
842 collect2 will sort the names and arrange that they are called at
844 if (!targetm
.have_ctors_dtors
&& final
)
846 sprintf (which_buf
, "%c_%.5d_%d", which
, priority
, counter
++);
847 name
= get_file_function_name (which_buf
);
851 /* Proudce sane name but one not recognizable by collect2, just for the
852 case we fail to inline the function. */
853 sprintf (which_buf
, "_sub_%c_%.5d_%d", which
, priority
, counter
++);
854 name
= get_identifier (which_buf
);
857 decl
= build_decl (input_location
, FUNCTION_DECL
, name
,
858 build_function_type_list (void_type_node
, NULL_TREE
));
859 current_function_decl
= decl
;
861 resdecl
= build_decl (input_location
,
862 RESULT_DECL
, NULL_TREE
, void_type_node
);
863 DECL_ARTIFICIAL (resdecl
) = 1;
864 DECL_RESULT (decl
) = resdecl
;
865 DECL_CONTEXT (resdecl
) = decl
;
867 allocate_struct_function (decl
, false);
869 TREE_STATIC (decl
) = 1;
870 TREE_USED (decl
) = 1;
871 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl
) = optimization
;
872 DECL_FUNCTION_SPECIFIC_TARGET (decl
) = target
;
873 DECL_ARTIFICIAL (decl
) = 1;
874 DECL_IGNORED_P (decl
) = 1;
875 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
876 DECL_SAVED_TREE (decl
) = body
;
877 if (!targetm
.have_ctors_dtors
&& final
)
879 TREE_PUBLIC (decl
) = 1;
880 DECL_PRESERVE_P (decl
) = 1;
882 DECL_UNINLINABLE (decl
) = 1;
884 DECL_INITIAL (decl
) = make_node (BLOCK
);
885 BLOCK_SUPERCONTEXT (DECL_INITIAL (decl
)) = decl
;
886 TREE_USED (DECL_INITIAL (decl
)) = 1;
888 DECL_SOURCE_LOCATION (decl
) = input_location
;
889 cfun
->function_end_locus
= input_location
;
894 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
895 decl_init_priority_insert (decl
, priority
);
898 DECL_STATIC_DESTRUCTOR (decl
) = 1;
899 decl_fini_priority_insert (decl
, priority
);
905 gimplify_function_tree (decl
);
907 cgraph_node::add_new_function (decl
, false);
910 current_function_decl
= NULL
;
913 /* Generate and emit a static constructor or destructor. WHICH must
914 be one of 'I' (for a constructor) or 'D' (for a destructor).
915 BODY is a STATEMENT_LIST containing GENERIC
916 statements. PRIORITY is the initialization priority for this
917 constructor or destructor. */
920 cgraph_build_static_cdtor (char which
, tree body
, int priority
)
922 /* FIXME: We should be able to
923 gcc_assert (!in_lto_p);
924 because at LTO time the global options are not safe to use.
925 Unfortunately ASAN finish_file will produce constructors late and they
926 may lead to surprises. */
927 cgraph_build_static_cdtor_1 (which
, body
, priority
, false,
928 optimization_default_node
,
929 target_option_default_node
);
932 /* When target does not have ctors and dtors, we call all constructor
933 and destructor by special initialization/destruction function
934 recognized by collect2.
936 When we are going to build this function, collect all constructors and
937 destructors and turn them into normal functions. */
940 record_cdtor_fn (struct cgraph_node
*node
, vec
<tree
> *ctors
, vec
<tree
> *dtors
)
942 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
943 ctors
->safe_push (node
->decl
);
944 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
945 dtors
->safe_push (node
->decl
);
946 node
= cgraph_node::get (node
->decl
);
947 DECL_DISREGARD_INLINE_LIMITS (node
->decl
) = 1;
950 /* Define global constructors/destructor functions for the CDTORS, of
951 which they are LEN. The CDTORS are sorted by initialization
952 priority. If CTOR_P is true, these are constructors; otherwise,
953 they are destructors. */
956 build_cdtor (bool ctor_p
, const vec
<tree
> &cdtors
)
959 size_t len
= cdtors
.length ();
966 priority_type priority
;
975 p
= ctor_p
? DECL_INIT_PRIORITY (fn
) : DECL_FINI_PRIORITY (fn
);
978 else if (p
!= priority
)
984 /* When there is only one cdtor and target supports them, do nothing. */
986 && targetm
.have_ctors_dtors
)
991 /* Find the next batch of constructors/destructors with the same
992 initialization priority. */
997 call
= build_call_expr (fn
, 0);
999 DECL_STATIC_CONSTRUCTOR (fn
) = 0;
1001 DECL_STATIC_DESTRUCTOR (fn
) = 0;
1002 /* We do not want to optimize away pure/const calls here.
1003 When optimizing, these should be already removed, when not
1004 optimizing, we want user to be able to breakpoint in them. */
1005 TREE_SIDE_EFFECTS (call
) = 1;
1006 append_to_statement_list (call
, &body
);
1008 gcc_assert (body
!= NULL_TREE
);
1009 /* Generate a function to call all the function of like
1011 cgraph_build_static_cdtor_1 (ctor_p
? 'I' : 'D', body
, priority
, true,
1012 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (cdtors
[0]),
1013 DECL_FUNCTION_SPECIFIC_TARGET (cdtors
[0]));
1017 /* Comparison function for qsort. P1 and P2 are actually of type
1018 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1019 used to determine the sort order. */
1022 compare_ctor (const void *p1
, const void *p2
)
1029 f1
= *(const tree
*)p1
;
1030 f2
= *(const tree
*)p2
;
1031 priority1
= DECL_INIT_PRIORITY (f1
);
1032 priority2
= DECL_INIT_PRIORITY (f2
);
1034 if (priority1
< priority2
)
1036 else if (priority1
> priority2
)
1039 /* Ensure a stable sort. Constructors are executed in backwarding
1040 order to make LTO initialize braries first. */
1041 return DECL_UID (f2
) - DECL_UID (f1
);
1044 /* Comparison function for qsort. P1 and P2 are actually of type
1045 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1046 used to determine the sort order. */
1049 compare_dtor (const void *p1
, const void *p2
)
1056 f1
= *(const tree
*)p1
;
1057 f2
= *(const tree
*)p2
;
1058 priority1
= DECL_FINI_PRIORITY (f1
);
1059 priority2
= DECL_FINI_PRIORITY (f2
);
1061 if (priority1
< priority2
)
1063 else if (priority1
> priority2
)
1066 /* Ensure a stable sort. */
1067 return DECL_UID (f1
) - DECL_UID (f2
);
1070 /* Generate functions to call static constructors and destructors
1071 for targets that do not support .ctors/.dtors sections. These
1072 functions have magic names which are detected by collect2. */
1075 build_cdtor_fns (vec
<tree
> *ctors
, vec
<tree
> *dtors
)
1077 if (!ctors
->is_empty ())
1079 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1080 ctors
->qsort (compare_ctor
);
1081 build_cdtor (/*ctor_p=*/true, *ctors
);
1084 if (!dtors
->is_empty ())
1086 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1087 dtors
->qsort (compare_dtor
);
1088 build_cdtor (/*ctor_p=*/false, *dtors
);
1092 /* Look for constructors and destructors and produce function calling them.
1093 This is needed for targets not supporting ctors or dtors, but we perform the
1094 transformation also at linktime to merge possibly numerous
1095 constructors/destructors into single function to improve code locality and
1099 ipa_cdtor_merge (void)
1101 /* A vector of FUNCTION_DECLs declared as static constructors. */
1102 auto_vec
<tree
, 20> ctors
;
1103 /* A vector of FUNCTION_DECLs declared as static destructors. */
1104 auto_vec
<tree
, 20> dtors
;
1105 struct cgraph_node
*node
;
1106 FOR_EACH_DEFINED_FUNCTION (node
)
1107 if (DECL_STATIC_CONSTRUCTOR (node
->decl
)
1108 || DECL_STATIC_DESTRUCTOR (node
->decl
))
1109 record_cdtor_fn (node
, &ctors
, &dtors
);
1110 build_cdtor_fns (&ctors
, &dtors
);
1116 const pass_data pass_data_ipa_cdtor_merge
=
1118 IPA_PASS
, /* type */
1120 OPTGROUP_NONE
, /* optinfo_flags */
1121 TV_CGRAPHOPT
, /* tv_id */
1122 0, /* properties_required */
1123 0, /* properties_provided */
1124 0, /* properties_destroyed */
1125 0, /* todo_flags_start */
1126 0, /* todo_flags_finish */
1129 class pass_ipa_cdtor_merge
: public ipa_opt_pass_d
1132 pass_ipa_cdtor_merge (gcc::context
*ctxt
)
1133 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge
, ctxt
,
1134 NULL
, /* generate_summary */
1135 NULL
, /* write_summary */
1136 NULL
, /* read_summary */
1137 NULL
, /* write_optimization_summary */
1138 NULL
, /* read_optimization_summary */
1139 NULL
, /* stmt_fixup */
1140 0, /* function_transform_todo_flags_start */
1141 NULL
, /* function_transform */
1142 NULL
) /* variable_transform */
1145 /* opt_pass methods: */
1146 virtual bool gate (function
*);
1147 virtual unsigned int execute (function
*) { return ipa_cdtor_merge (); }
1149 }; // class pass_ipa_cdtor_merge
1152 pass_ipa_cdtor_merge::gate (function
*)
1154 /* Perform the pass when we have no ctors/dtors support
1155 or at LTO time to merge multiple constructors into single
1157 return !targetm
.have_ctors_dtors
|| in_lto_p
;
1163 make_pass_ipa_cdtor_merge (gcc::context
*ctxt
)
1165 return new pass_ipa_cdtor_merge (ctxt
);
1168 /* Invalid pointer representing BOTTOM for single user dataflow. */
1169 #define BOTTOM ((cgraph_node *)(size_t) 2)
1171 /* Meet operation for single user dataflow.
1172 Here we want to associate variables with sigle function that may access it.
1174 FUNCTION is current single user of a variable, VAR is variable that uses it.
1175 Latttice is stored in SINGLE_USER_MAP.
1178 - TOP by no entry in SIGNLE_USER_MAP
1179 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1180 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1183 meet (cgraph_node
*function
, varpool_node
*var
,
1184 hash_map
<varpool_node
*, cgraph_node
*> &single_user_map
)
1186 struct cgraph_node
*user
, **f
;
1188 if (var
->aux
== BOTTOM
)
1191 f
= single_user_map
.get (var
);
1197 else if (function
!= user
)
1203 /* Propagation step of single-use dataflow.
1205 Check all uses of VNODE and see if they are used by single function FUNCTION.
1206 SINGLE_USER_MAP represents the dataflow lattice. */
1209 propagate_single_user (varpool_node
*vnode
, cgraph_node
*function
,
1210 hash_map
<varpool_node
*, cgraph_node
*> &single_user_map
)
1213 struct ipa_ref
*ref
;
1215 gcc_assert (!vnode
->externally_visible
);
1217 /* If node is an alias, first meet with its target. */
1219 function
= meet (function
, vnode
->get_alias_target (), single_user_map
);
1221 /* Check all users and see if they correspond to a single function. */
1222 for (i
= 0; vnode
->iterate_referring (i
, ref
) && function
!= BOTTOM
; i
++)
1224 struct cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (ref
->referring
);
1227 if (cnode
->inlined_to
)
1228 cnode
= cnode
->inlined_to
;
1231 else if (function
!= cnode
)
1235 function
= meet (function
, dyn_cast
<varpool_node
*> (ref
->referring
),
1241 /* Pass setting used_by_single_function flag.
1242 This flag is set on variable when there is only one function that may
1243 possibly referr to it. */
1246 ipa_single_use (void)
1248 varpool_node
*first
= (varpool_node
*) (void *) 1;
1250 hash_map
<varpool_node
*, cgraph_node
*> single_user_map
;
1252 FOR_EACH_DEFINED_VARIABLE (var
)
1253 if (!var
->all_refs_explicit_p ())
1257 /* Enqueue symbol for dataflow. */
1262 /* The actual dataflow. */
1264 while (first
!= (void *) 1)
1266 cgraph_node
*user
, *orig_user
, **f
;
1269 first
= (varpool_node
*)first
->aux
;
1271 f
= single_user_map
.get (var
);
1276 user
= propagate_single_user (var
, orig_user
, single_user_map
);
1278 gcc_checking_assert (var
->aux
!= BOTTOM
);
1280 /* If user differs, enqueue all references. */
1281 if (user
!= orig_user
)
1286 single_user_map
.put (var
, user
);
1288 /* Enqueue all aliases for re-processing. */
1289 for (i
= 0; var
->iterate_direct_aliases (i
, ref
); i
++)
1290 if (!ref
->referring
->aux
)
1292 ref
->referring
->aux
= first
;
1293 first
= dyn_cast
<varpool_node
*> (ref
->referring
);
1295 /* Enqueue all users for re-processing. */
1296 for (i
= 0; var
->iterate_reference (i
, ref
); i
++)
1297 if (!ref
->referred
->aux
1298 && ref
->referred
->definition
1299 && is_a
<varpool_node
*> (ref
->referred
))
1301 ref
->referred
->aux
= first
;
1302 first
= dyn_cast
<varpool_node
*> (ref
->referred
);
1305 /* If user is BOTTOM, just punt on this var. */
1315 FOR_EACH_DEFINED_VARIABLE (var
)
1317 if (var
->aux
!= BOTTOM
)
1319 /* Not having the single user known means that the VAR is
1320 unreachable. Either someone forgot to remove unreachable
1321 variables or the reachability here is wrong. */
1323 gcc_checking_assert (single_user_map
.get (var
));
1327 fprintf (dump_file
, "Variable %s is used by single function\n",
1330 var
->used_by_single_function
= true;
1339 const pass_data pass_data_ipa_single_use
=
1341 IPA_PASS
, /* type */
1342 "single-use", /* name */
1343 OPTGROUP_NONE
, /* optinfo_flags */
1344 TV_CGRAPHOPT
, /* tv_id */
1345 0, /* properties_required */
1346 0, /* properties_provided */
1347 0, /* properties_destroyed */
1348 0, /* todo_flags_start */
1349 0, /* todo_flags_finish */
1352 class pass_ipa_single_use
: public ipa_opt_pass_d
1355 pass_ipa_single_use (gcc::context
*ctxt
)
1356 : ipa_opt_pass_d (pass_data_ipa_single_use
, ctxt
,
1357 NULL
, /* generate_summary */
1358 NULL
, /* write_summary */
1359 NULL
, /* read_summary */
1360 NULL
, /* write_optimization_summary */
1361 NULL
, /* read_optimization_summary */
1362 NULL
, /* stmt_fixup */
1363 0, /* function_transform_todo_flags_start */
1364 NULL
, /* function_transform */
1365 NULL
) /* variable_transform */
1368 /* opt_pass methods: */
1369 virtual unsigned int execute (function
*) { return ipa_single_use (); }
1371 }; // class pass_ipa_single_use
1376 make_pass_ipa_single_use (gcc::context
*ctxt
)
1378 return new pass_ipa_single_use (ctxt
);
1381 /* Materialize all clones. */
1385 const pass_data pass_data_materialize_all_clones
=
1387 SIMPLE_IPA_PASS
, /* type */
1388 "materialize-all-clones", /* name */
1389 OPTGROUP_NONE
, /* optinfo_flags */
1390 TV_IPA_OPT
, /* tv_id */
1391 0, /* properties_required */
1392 0, /* properties_provided */
1393 0, /* properties_destroyed */
1394 0, /* todo_flags_start */
1395 0, /* todo_flags_finish */
1398 class pass_materialize_all_clones
: public simple_ipa_opt_pass
1401 pass_materialize_all_clones (gcc::context
*ctxt
)
1402 : simple_ipa_opt_pass (pass_data_materialize_all_clones
, ctxt
)
1405 /* opt_pass methods: */
1406 virtual unsigned int execute (function
*)
1408 symtab
->materialize_all_clones ();
1412 }; // class pass_materialize_all_clones
1416 simple_ipa_opt_pass
*
1417 make_pass_materialize_all_clones (gcc::context
*ctxt
)
1419 return new pass_materialize_all_clones (ctxt
);