1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003-2018 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
->global
.inlined_to
)
76 e
->callee
->global
.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 /* Process references. */
107 process_references (symtab_node
*snode
,
109 bool before_inlining_p
,
110 hash_set
<symtab_node
*> *reachable
)
113 struct ipa_ref
*ref
= NULL
;
114 for (i
= 0; snode
->iterate_reference (i
, ref
); i
++)
116 symtab_node
*node
= ref
->referred
;
117 symtab_node
*body
= node
->ultimate_alias_target ();
119 if (node
->definition
&& !node
->in_other_partition
120 && ((!DECL_EXTERNAL (node
->decl
) || node
->alias
)
121 || (((before_inlining_p
122 && (TREE_CODE (node
->decl
) != FUNCTION_DECL
123 || (TREE_CODE (node
->decl
) == FUNCTION_DECL
124 && opt_for_fn (body
->decl
, optimize
))
125 || (symtab
->state
< IPA_SSA
128 DECL_ATTRIBUTES (body
->decl
))))))
129 /* We use variable constructors during late compilation for
130 constant folding. Keep references alive so partitioning
131 knows about potential references. */
132 || (VAR_P (node
->decl
)
134 || flag_incremental_link
135 == INCREMENTAL_LINK_LTO
)
136 && dyn_cast
<varpool_node
*> (node
)
137 ->ctor_useable_for_folding_p ()))))
139 /* Be sure that we will not optimize out alias target
141 if (DECL_EXTERNAL (node
->decl
)
143 && before_inlining_p
)
144 reachable
->add (body
);
145 reachable
->add (node
);
147 enqueue_node (node
, first
, reachable
);
151 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
152 all its potential targets as reachable to permit later inlining if
153 devirtualization happens. After inlining still keep their declarations
154 around, so we can devirtualize to a direct call.
156 Also try to make trivial devirutalization when no or only one target is
160 walk_polymorphic_call_targets (hash_set
<void *> *reachable_call_targets
,
161 struct cgraph_edge
*edge
,
163 hash_set
<symtab_node
*> *reachable
,
164 bool before_inlining_p
)
169 vec
<cgraph_node
*>targets
170 = possible_polymorphic_call_targets
171 (edge
, &final
, &cache_token
);
173 if (!reachable_call_targets
->add (cache_token
))
175 for (i
= 0; i
< targets
.length (); i
++)
177 struct cgraph_node
*n
= targets
[i
];
179 /* Do not bother to mark virtual methods in anonymous namespace;
180 either we will find use of virtual table defining it, or it is
182 if (TREE_CODE (TREE_TYPE (n
->decl
)) == METHOD_TYPE
183 && type_in_anonymous_namespace_p
184 (TYPE_METHOD_BASETYPE (TREE_TYPE (n
->decl
))))
187 n
->indirect_call_target
= true;
188 symtab_node
*body
= n
->function_symbol ();
190 /* Prior inlining, keep alive bodies of possible targets for
193 && (before_inlining_p
194 && opt_for_fn (body
->decl
, optimize
)
195 && opt_for_fn (body
->decl
, flag_devirtualize
)))
197 /* Be sure that we will not optimize out alias target
199 if (DECL_EXTERNAL (n
->decl
)
201 && before_inlining_p
)
202 reachable
->add (body
);
205 /* Even after inlining we want to keep the possible targets in the
206 boundary, so late passes can still produce direct call even if
207 the chance for inlining is lost. */
208 enqueue_node (n
, first
, reachable
);
212 /* Very trivial devirtualization; when the type is
213 final or anonymous (so we know all its derivation)
214 and there is only one possible virtual call target,
215 make the edge direct. */
218 if (targets
.length () <= 1 && dbg_cnt (devirt
))
220 cgraph_node
*target
, *node
= edge
->caller
;
221 if (targets
.length () == 1)
224 target
= cgraph_node::get_create
225 (builtin_decl_implicit (BUILT_IN_UNREACHABLE
));
227 if (dump_enabled_p ())
231 locus
= gimple_location (edge
->call_stmt
);
233 locus
= UNKNOWN_LOCATION
;
234 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, locus
,
235 "devirtualizing call in %s to %s\n",
236 edge
->caller
->dump_name (),
237 target
->dump_name ());
239 edge
= edge
->make_direct (target
);
240 if (ipa_fn_summaries
)
241 ipa_update_overall_fn_summary (node
);
242 else if (edge
->call_stmt
)
243 edge
->redirect_call_stmt_to_callee ();
248 /* Perform reachability analysis and reclaim all unreachable nodes.
250 The algorithm is basically mark&sweep but with some extra refinements:
252 - reachable extern inline functions needs special handling; the bodies needs
253 to stay in memory until inlining in hope that they will be inlined.
254 After inlining we release their bodies and turn them into unanalyzed
255 nodes even when they are reachable.
257 - virtual functions are kept in callgraph even if they seem unreachable in
258 hope calls to them will be devirtualized.
260 Again we remove them after inlining. In late optimization some
261 devirtualization may happen, but it is not important since we won't inline
262 the call. In theory early opts and IPA should work out all important cases.
264 - virtual clones needs bodies of their origins for later materialization;
265 this means that we want to keep the body even if the origin is unreachable
266 otherwise. To avoid origin from sitting in the callgraph and being
267 walked by IPA passes, we turn them into unanalyzed nodes with body
270 We maintain set of function declaration where body needs to stay in
271 body_needed_for_clonning
273 Inline clones represent special case: their declaration match the
274 declaration of origin and cgraph_remove_node already knows how to
275 reshape callgraph and preserve body when offline copy of function or
276 inline clone is being removed.
278 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
279 variables with DECL_INITIAL set. We finalize these and keep reachable
280 ones around for constant folding purposes. After inlining we however
281 stop walking their references to let everything static referneced by them
282 to be removed when it is otherwise unreachable.
284 We maintain queue of both reachable symbols (i.e. defined symbols that needs
285 to stay) and symbols that are in boundary (i.e. external symbols referenced
286 by reachable symbols or origins of clones). The queue is represented
287 as linked list by AUX pointer terminated by 1.
289 At the end we keep all reachable symbols. For symbols in boundary we always
290 turn definition into a declaration, but we may keep function body around
291 based on body_needed_for_clonning
293 All symbols that enter the queue have AUX pointer non-zero and are in the
294 boundary. Pointer set REACHABLE is used to track reachable symbols.
296 Every symbol can be visited twice - once as part of boundary and once
297 as real reachable symbol. enqueue_node needs to decide whether the
298 node needs to be re-queued for second processing. For this purpose
299 we set AUX pointer of processed symbols in the boundary to constant 2. */
302 symbol_table::remove_unreachable_nodes (FILE *file
)
304 symtab_node
*first
= (symtab_node
*) (void *) 1;
305 struct cgraph_node
*node
, *next
;
306 varpool_node
*vnode
, *vnext
;
307 bool changed
= false;
308 hash_set
<symtab_node
*> reachable
;
309 hash_set
<tree
> body_needed_for_clonning
;
310 hash_set
<void *> reachable_call_targets
;
311 bool before_inlining_p
= symtab
->state
< (!optimize
&& !in_lto_p
? IPA_SSA
312 : IPA_SSA_AFTER_INLINING
);
314 timevar_push (TV_IPA_UNREACHABLE
);
315 build_type_inheritance_graph ();
317 fprintf (file
, "\nReclaiming functions:");
320 FOR_EACH_FUNCTION (node
)
321 gcc_assert (!node
->aux
);
322 FOR_EACH_VARIABLE (vnode
)
323 gcc_assert (!vnode
->aux
);
325 /* Mark functions whose bodies are obviously needed.
326 This is mostly when they can be referenced externally. Inline clones
327 are special since their declarations are shared with master clone and thus
328 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
329 FOR_EACH_FUNCTION (node
)
331 node
->used_as_abstract_origin
= false;
332 node
->indirect_call_target
= false;
334 && !node
->global
.inlined_to
335 && !node
->in_other_partition
336 && !node
->can_remove_if_no_direct_calls_and_refs_p ())
338 gcc_assert (!node
->global
.inlined_to
);
339 reachable
.add (node
);
340 enqueue_node (node
, &first
, &reachable
);
343 gcc_assert (!node
->aux
);
346 /* Mark variables that are obviously needed. */
347 FOR_EACH_DEFINED_VARIABLE (vnode
)
348 if (!vnode
->can_remove_if_no_refs_p()
349 && !vnode
->in_other_partition
)
351 reachable
.add (vnode
);
352 enqueue_node (vnode
, &first
, &reachable
);
355 /* Perform reachability analysis. */
356 while (first
!= (symtab_node
*) (void *) 1)
358 bool in_boundary_p
= !reachable
.contains (first
);
359 symtab_node
*node
= first
;
361 first
= (symtab_node
*)first
->aux
;
363 /* If we are processing symbol in boundary, mark its AUX pointer for
364 possible later re-processing in enqueue_node. */
367 node
->aux
= (void *)2;
368 if (node
->alias
&& node
->analyzed
)
369 enqueue_node (node
->get_alias_target (), &first
, &reachable
);
373 if (TREE_CODE (node
->decl
) == FUNCTION_DECL
374 && DECL_ABSTRACT_ORIGIN (node
->decl
))
376 struct cgraph_node
*origin_node
377 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node
->decl
));
378 if (origin_node
&& !origin_node
->used_as_abstract_origin
)
380 origin_node
->used_as_abstract_origin
= true;
381 gcc_assert (!origin_node
->prev_sibling_clone
);
382 gcc_assert (!origin_node
->next_sibling_clone
);
383 for (cgraph_node
*n
= origin_node
->clones
; n
;
384 n
= n
->next_sibling_clone
)
385 if (n
->decl
== DECL_ABSTRACT_ORIGIN (node
->decl
))
386 n
->used_as_abstract_origin
= true;
389 /* If any symbol in a comdat group is reachable, force
390 all externally visible symbols in the same comdat
391 group to be reachable as well. Comdat-local symbols
392 can be discarded if all uses were inlined. */
393 if (node
->same_comdat_group
)
396 for (next
= node
->same_comdat_group
;
398 next
= next
->same_comdat_group
)
399 if (!next
->comdat_local_p ()
400 && !reachable
.add (next
))
401 enqueue_node (next
, &first
, &reachable
);
403 /* Mark references as reachable. */
404 process_references (node
, &first
, before_inlining_p
, &reachable
);
407 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
409 /* Mark the callees reachable unless they are direct calls to extern
410 inline functions we decided to not inline. */
413 struct cgraph_edge
*e
;
414 /* Keep alive possible targets for devirtualization. */
415 if (opt_for_fn (cnode
->decl
, optimize
)
416 && opt_for_fn (cnode
->decl
, flag_devirtualize
))
418 struct cgraph_edge
*next
;
419 for (e
= cnode
->indirect_calls
; e
; e
= next
)
421 next
= e
->next_callee
;
422 if (e
->indirect_info
->polymorphic
)
423 walk_polymorphic_call_targets (&reachable_call_targets
,
424 e
, &first
, &reachable
,
428 for (e
= cnode
->callees
; e
; e
= e
->next_callee
)
430 symtab_node
*body
= e
->callee
->function_symbol ();
431 if (e
->callee
->definition
432 && !e
->callee
->in_other_partition
433 && (!e
->inline_failed
434 || !DECL_EXTERNAL (e
->callee
->decl
)
436 || (before_inlining_p
437 && (opt_for_fn (body
->decl
, optimize
)
438 || (symtab
->state
< IPA_SSA
441 DECL_ATTRIBUTES (body
->decl
)))))))
443 /* Be sure that we will not optimize out alias target
445 if (DECL_EXTERNAL (e
->callee
->decl
)
447 && before_inlining_p
)
448 reachable
.add (body
);
449 reachable
.add (e
->callee
);
451 enqueue_node (e
->callee
, &first
, &reachable
);
454 /* When inline clone exists, mark body to be preserved so when removing
455 offline copy of the function we don't kill it. */
456 if (cnode
->global
.inlined_to
)
457 body_needed_for_clonning
.add (cnode
->decl
);
459 /* For non-inline clones, force their origins to the boundary and ensure
460 that body is not removed. */
461 while (cnode
->clone_of
)
463 bool noninline
= cnode
->clone_of
->decl
!= cnode
->decl
;
464 cnode
= cnode
->clone_of
;
467 body_needed_for_clonning
.add (cnode
->decl
);
468 enqueue_node (cnode
, &first
, &reachable
);
473 else if (cnode
->thunk
.thunk_p
)
474 enqueue_node (cnode
->callees
->callee
, &first
, &reachable
);
476 /* If any reachable function has simd clones, mark them as
477 reachable as well. */
478 if (cnode
->simd_clones
)
481 for (next
= cnode
->simd_clones
;
483 next
= next
->simdclone
->next_clone
)
485 || !reachable
.add (next
))
486 enqueue_node (next
, &first
, &reachable
);
489 /* When we see constructor of external variable, keep referred nodes in the
490 boundary. This will also hold initializers of the external vars NODE
492 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
);
494 && DECL_EXTERNAL (node
->decl
)
498 struct ipa_ref
*ref
= NULL
;
499 for (int i
= 0; node
->iterate_reference (i
, ref
); i
++)
500 enqueue_node (ref
->referred
, &first
, &reachable
);
504 /* Remove unreachable functions. */
505 for (node
= first_function (); node
; node
= next
)
507 next
= next_function (node
);
509 /* If node is not needed at all, remove it. */
513 fprintf (file
, " %s", node
->dump_name ());
517 /* If node is unreachable, remove its body. */
518 else if (!reachable
.contains (node
))
520 /* We keep definitions of thunks and aliases in the boundary so
521 we can walk to the ultimate alias targets and function symbols
523 if (node
->alias
|| node
->thunk
.thunk_p
)
525 else if (!body_needed_for_clonning
.contains (node
->decl
)
526 && !node
->alias
&& !node
->thunk
.thunk_p
)
527 node
->release_body ();
528 else if (!node
->clone_of
)
529 gcc_assert (in_lto_p
|| DECL_RESULT (node
->decl
));
530 if (node
->definition
&& !node
->alias
&& !node
->thunk
.thunk_p
)
533 fprintf (file
, " %s", node
->dump_name ());
534 node
->body_removed
= true;
535 node
->analyzed
= false;
536 node
->definition
= false;
537 node
->cpp_implicit_alias
= false;
539 node
->transparent_alias
= false;
540 node
->thunk
.thunk_p
= false;
541 node
->weakref
= false;
542 /* After early inlining we drop always_inline attributes on
543 bodies of functions that are still referenced (have their
545 DECL_ATTRIBUTES (node
->decl
)
546 = remove_attribute ("always_inline",
547 DECL_ATTRIBUTES (node
->decl
));
548 if (!node
->in_other_partition
)
549 node
->local
.local
= false;
550 node
->remove_callees ();
551 node
->remove_all_references ();
553 if (node
->thunk
.thunk_p
554 && node
->thunk
.add_pointer_bounds_args
)
556 node
->thunk
.thunk_p
= false;
557 node
->thunk
.add_pointer_bounds_args
= false;
562 gcc_assert (node
->clone_of
|| !node
->has_gimple_body_p ()
563 || in_lto_p
|| DECL_RESULT (node
->decl
));
566 /* Inline clones might be kept around so their materializing allows further
567 cloning. If the function the clone is inlined into is removed, we need
568 to turn it into normal cone. */
569 FOR_EACH_FUNCTION (node
)
571 if (node
->global
.inlined_to
574 gcc_assert (node
->clones
);
575 node
->global
.inlined_to
= NULL
;
576 update_inlined_to_pointer (node
, node
);
581 /* Remove unreachable variables. */
583 fprintf (file
, "\nReclaiming variables:");
584 for (vnode
= first_variable (); vnode
; vnode
= vnext
)
586 vnext
= next_variable (vnode
);
588 /* For can_refer_decl_in_current_unit_p we want to track for
589 all external variables if they are defined in other partition
591 && (!flag_ltrans
|| !DECL_EXTERNAL (vnode
->decl
)))
593 struct ipa_ref
*ref
= NULL
;
595 /* First remove the aliases, so varpool::remove can possibly lookup
596 the constructor and save it for future use. */
597 while (vnode
->iterate_direct_aliases (0, ref
))
600 fprintf (file
, " %s", ref
->referred
->dump_name ());
601 ref
->referring
->remove ();
604 fprintf (file
, " %s", vnode
->dump_name ());
605 vnext
= next_variable (vnode
);
606 /* Signal removal to the debug machinery. */
607 if (! flag_wpa
|| flag_incremental_link
== INCREMENTAL_LINK_LTO
)
609 vnode
->definition
= false;
610 (*debug_hooks
->late_global_decl
) (vnode
->decl
);
615 else if (!reachable
.contains (vnode
) && !vnode
->alias
)
618 if (vnode
->definition
)
621 fprintf (file
, " %s", vnode
->name ());
624 /* Keep body if it may be useful for constant folding. */
625 if ((flag_wpa
|| flag_incremental_link
== INCREMENTAL_LINK_LTO
)
626 || ((init
= ctor_for_folding (vnode
->decl
)) == error_mark_node
))
627 vnode
->remove_initializer ();
629 DECL_INITIAL (vnode
->decl
) = init
;
630 vnode
->body_removed
= true;
631 vnode
->definition
= false;
632 vnode
->analyzed
= false;
635 vnode
->remove_from_same_comdat_group ();
637 vnode
->remove_all_references ();
643 /* Now update address_taken flags and try to promote functions to be local. */
645 fprintf (file
, "\nClearing address taken flags:");
646 FOR_EACH_DEFINED_FUNCTION (node
)
647 if (node
->address_taken
648 && !node
->used_from_other_partition
)
650 if (!node
->call_for_symbol_and_aliases
651 (has_addr_references_p
, NULL
, true))
654 fprintf (file
, " %s", node
->name ());
655 node
->address_taken
= false;
658 /* Virtual functions may be kept in cgraph just because
659 of possible later devirtualization. Do not mark them as
660 local too early so we won't optimize them out before
661 we are done with polymorphic call analysis. */
662 && (!before_inlining_p
663 || !node
->call_for_symbol_and_aliases
664 (is_indirect_call_target_p
, NULL
, true)))
666 node
->local
.local
= true;
668 fprintf (file
, " (local)");
673 fprintf (file
, "\n");
675 symtab_node::checking_verify_symtab_nodes ();
677 /* If we removed something, perhaps profile could be improved. */
678 if (changed
&& (optimize
|| in_lto_p
) && ipa_call_summaries
)
679 FOR_EACH_DEFINED_FUNCTION (node
)
680 ipa_propagate_frequency (node
);
682 timevar_pop (TV_IPA_UNREACHABLE
);
686 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
687 as needed, also clear EXPLICIT_REFS if the references to given variable
688 do not need to be explicit. */
691 process_references (varpool_node
*vnode
,
692 bool *written
, bool *address_taken
,
693 bool *read
, bool *explicit_refs
)
698 if (!vnode
->all_refs_explicit_p ()
699 || TREE_THIS_VOLATILE (vnode
->decl
))
700 *explicit_refs
= false;
702 for (i
= 0; vnode
->iterate_referring (i
, ref
)
703 && *explicit_refs
&& (!*written
|| !*address_taken
|| !*read
); i
++)
707 *address_taken
= true;
716 process_references (dyn_cast
<varpool_node
*> (ref
->referring
), written
,
717 address_taken
, read
, explicit_refs
);
722 /* Set TREE_READONLY bit. */
725 set_readonly_bit (varpool_node
*vnode
, void *data ATTRIBUTE_UNUSED
)
727 TREE_READONLY (vnode
->decl
) = true;
731 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
734 set_writeonly_bit (varpool_node
*vnode
, void *data
)
736 vnode
->writeonly
= true;
737 if (optimize
|| in_lto_p
)
739 DECL_INITIAL (vnode
->decl
) = NULL
;
742 if (vnode
->num_references ())
743 *(bool *)data
= true;
744 vnode
->remove_all_references ();
750 /* Clear addressale bit of VNODE. */
753 clear_addressable_bit (varpool_node
*vnode
, void *data ATTRIBUTE_UNUSED
)
755 vnode
->address_taken
= false;
756 TREE_ADDRESSABLE (vnode
->decl
) = 0;
760 /* Discover variables that have no longer address taken or that are read only
761 and update their flags.
763 Return true when unreachable symbol removan should be done.
765 FIXME: This can not be done in between gimplify and omp_expand since
766 readonly flag plays role on what is shared and what is not. Currently we do
767 this transformation as part of whole program visibility and re-do at
768 ipa-reference pass (to take into account clonning), but it would
769 make sense to do it before early optimizations. */
772 ipa_discover_readonly_nonaddressable_vars (void)
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
)
835 static int counter
= 0;
837 tree decl
, name
, resdecl
;
839 /* The priority is encoded in the constructor or destructor name.
840 collect2 will sort the names and arrange that they are called at
843 sprintf (which_buf
, "%c_%.5d_%d", which
, priority
, counter
++);
845 /* Proudce sane name but one not recognizable by collect2, just for the
846 case we fail to inline the function. */
847 sprintf (which_buf
, "sub_%c_%.5d_%d", which
, priority
, counter
++);
848 name
= get_file_function_name (which_buf
);
850 decl
= build_decl (input_location
, FUNCTION_DECL
, name
,
851 build_function_type_list (void_type_node
, NULL_TREE
));
852 current_function_decl
= decl
;
854 resdecl
= build_decl (input_location
,
855 RESULT_DECL
, NULL_TREE
, void_type_node
);
856 DECL_ARTIFICIAL (resdecl
) = 1;
857 DECL_RESULT (decl
) = resdecl
;
858 DECL_CONTEXT (resdecl
) = decl
;
860 allocate_struct_function (decl
, false);
862 TREE_STATIC (decl
) = 1;
863 TREE_USED (decl
) = 1;
864 DECL_ARTIFICIAL (decl
) = 1;
865 DECL_IGNORED_P (decl
) = 1;
866 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
867 DECL_SAVED_TREE (decl
) = body
;
868 if (!targetm
.have_ctors_dtors
&& final
)
870 TREE_PUBLIC (decl
) = 1;
871 DECL_PRESERVE_P (decl
) = 1;
873 DECL_UNINLINABLE (decl
) = 1;
875 DECL_INITIAL (decl
) = make_node (BLOCK
);
876 BLOCK_SUPERCONTEXT (DECL_INITIAL (decl
)) = decl
;
877 TREE_USED (DECL_INITIAL (decl
)) = 1;
879 DECL_SOURCE_LOCATION (decl
) = input_location
;
880 cfun
->function_end_locus
= input_location
;
885 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
886 decl_init_priority_insert (decl
, priority
);
889 DECL_STATIC_DESTRUCTOR (decl
) = 1;
890 decl_fini_priority_insert (decl
, priority
);
896 gimplify_function_tree (decl
);
898 cgraph_node::add_new_function (decl
, false);
901 current_function_decl
= NULL
;
904 /* Generate and emit a static constructor or destructor. WHICH must
905 be one of 'I' (for a constructor) or 'D' (for a destructor).
906 BODY is a STATEMENT_LIST containing GENERIC
907 statements. PRIORITY is the initialization priority for this
908 constructor or destructor. */
911 cgraph_build_static_cdtor (char which
, tree body
, int priority
)
913 cgraph_build_static_cdtor_1 (which
, body
, priority
, false);
916 /* When target does not have ctors and dtors, we call all constructor
917 and destructor by special initialization/destruction function
918 recognized by collect2.
920 When we are going to build this function, collect all constructors and
921 destructors and turn them into normal functions. */
924 record_cdtor_fn (struct cgraph_node
*node
, vec
<tree
> *ctors
, vec
<tree
> *dtors
)
926 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
927 ctors
->safe_push (node
->decl
);
928 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
929 dtors
->safe_push (node
->decl
);
930 node
= cgraph_node::get (node
->decl
);
931 DECL_DISREGARD_INLINE_LIMITS (node
->decl
) = 1;
934 /* Define global constructors/destructor functions for the CDTORS, of
935 which they are LEN. The CDTORS are sorted by initialization
936 priority. If CTOR_P is true, these are constructors; otherwise,
937 they are destructors. */
940 build_cdtor (bool ctor_p
, const vec
<tree
> &cdtors
)
943 size_t len
= cdtors
.length ();
950 priority_type priority
;
959 p
= ctor_p
? DECL_INIT_PRIORITY (fn
) : DECL_FINI_PRIORITY (fn
);
962 else if (p
!= priority
)
968 /* When there is only one cdtor and target supports them, do nothing. */
970 && targetm
.have_ctors_dtors
)
975 /* Find the next batch of constructors/destructors with the same
976 initialization priority. */
981 call
= build_call_expr (fn
, 0);
983 DECL_STATIC_CONSTRUCTOR (fn
) = 0;
985 DECL_STATIC_DESTRUCTOR (fn
) = 0;
986 /* We do not want to optimize away pure/const calls here.
987 When optimizing, these should be already removed, when not
988 optimizing, we want user to be able to breakpoint in them. */
989 TREE_SIDE_EFFECTS (call
) = 1;
990 append_to_statement_list (call
, &body
);
992 gcc_assert (body
!= NULL_TREE
);
993 /* Generate a function to call all the function of like
995 cgraph_build_static_cdtor_1 (ctor_p
? 'I' : 'D', body
, priority
, true);
999 /* Comparison function for qsort. P1 and P2 are actually of type
1000 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1001 used to determine the sort order. */
1004 compare_ctor (const void *p1
, const void *p2
)
1011 f1
= *(const tree
*)p1
;
1012 f2
= *(const tree
*)p2
;
1013 priority1
= DECL_INIT_PRIORITY (f1
);
1014 priority2
= DECL_INIT_PRIORITY (f2
);
1016 if (priority1
< priority2
)
1018 else if (priority1
> priority2
)
1021 /* Ensure a stable sort. Constructors are executed in backwarding
1022 order to make LTO initialize braries first. */
1023 return DECL_UID (f2
) - DECL_UID (f1
);
1026 /* Comparison function for qsort. P1 and P2 are actually of type
1027 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1028 used to determine the sort order. */
1031 compare_dtor (const void *p1
, const void *p2
)
1038 f1
= *(const tree
*)p1
;
1039 f2
= *(const tree
*)p2
;
1040 priority1
= DECL_FINI_PRIORITY (f1
);
1041 priority2
= DECL_FINI_PRIORITY (f2
);
1043 if (priority1
< priority2
)
1045 else if (priority1
> priority2
)
1048 /* Ensure a stable sort. */
1049 return DECL_UID (f1
) - DECL_UID (f2
);
1052 /* Generate functions to call static constructors and destructors
1053 for targets that do not support .ctors/.dtors sections. These
1054 functions have magic names which are detected by collect2. */
1057 build_cdtor_fns (vec
<tree
> *ctors
, vec
<tree
> *dtors
)
1059 if (!ctors
->is_empty ())
1061 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1062 ctors
->qsort (compare_ctor
);
1063 build_cdtor (/*ctor_p=*/true, *ctors
);
1066 if (!dtors
->is_empty ())
1068 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1069 dtors
->qsort (compare_dtor
);
1070 build_cdtor (/*ctor_p=*/false, *dtors
);
1074 /* Look for constructors and destructors and produce function calling them.
1075 This is needed for targets not supporting ctors or dtors, but we perform the
1076 transformation also at linktime to merge possibly numerous
1077 constructors/destructors into single function to improve code locality and
1081 ipa_cdtor_merge (void)
1083 /* A vector of FUNCTION_DECLs declared as static constructors. */
1084 auto_vec
<tree
, 20> ctors
;
1085 /* A vector of FUNCTION_DECLs declared as static destructors. */
1086 auto_vec
<tree
, 20> dtors
;
1087 struct cgraph_node
*node
;
1088 FOR_EACH_DEFINED_FUNCTION (node
)
1089 if (DECL_STATIC_CONSTRUCTOR (node
->decl
)
1090 || DECL_STATIC_DESTRUCTOR (node
->decl
))
1091 record_cdtor_fn (node
, &ctors
, &dtors
);
1092 build_cdtor_fns (&ctors
, &dtors
);
1098 const pass_data pass_data_ipa_cdtor_merge
=
1100 IPA_PASS
, /* type */
1102 OPTGROUP_NONE
, /* optinfo_flags */
1103 TV_CGRAPHOPT
, /* tv_id */
1104 0, /* properties_required */
1105 0, /* properties_provided */
1106 0, /* properties_destroyed */
1107 0, /* todo_flags_start */
1108 0, /* todo_flags_finish */
1111 class pass_ipa_cdtor_merge
: public ipa_opt_pass_d
1114 pass_ipa_cdtor_merge (gcc::context
*ctxt
)
1115 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge
, ctxt
,
1116 NULL
, /* generate_summary */
1117 NULL
, /* write_summary */
1118 NULL
, /* read_summary */
1119 NULL
, /* write_optimization_summary */
1120 NULL
, /* read_optimization_summary */
1121 NULL
, /* stmt_fixup */
1122 0, /* function_transform_todo_flags_start */
1123 NULL
, /* function_transform */
1124 NULL
) /* variable_transform */
1127 /* opt_pass methods: */
1128 virtual bool gate (function
*);
1129 virtual unsigned int execute (function
*) { return ipa_cdtor_merge (); }
1131 }; // class pass_ipa_cdtor_merge
1134 pass_ipa_cdtor_merge::gate (function
*)
1136 /* Perform the pass when we have no ctors/dtors support
1137 or at LTO time to merge multiple constructors into single
1139 return !targetm
.have_ctors_dtors
|| in_lto_p
;
1145 make_pass_ipa_cdtor_merge (gcc::context
*ctxt
)
1147 return new pass_ipa_cdtor_merge (ctxt
);
1150 /* Invalid pointer representing BOTTOM for single user dataflow. */
1151 #define BOTTOM ((cgraph_node *)(size_t) 2)
1153 /* Meet operation for single user dataflow.
1154 Here we want to associate variables with sigle function that may access it.
1156 FUNCTION is current single user of a variable, VAR is variable that uses it.
1157 Latttice is stored in SINGLE_USER_MAP.
1160 - TOP by no entry in SIGNLE_USER_MAP
1161 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1162 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1165 meet (cgraph_node
*function
, varpool_node
*var
,
1166 hash_map
<varpool_node
*, cgraph_node
*> &single_user_map
)
1168 struct cgraph_node
*user
, **f
;
1170 if (var
->aux
== BOTTOM
)
1173 f
= single_user_map
.get (var
);
1179 else if (function
!= user
)
1185 /* Propagation step of single-use dataflow.
1187 Check all uses of VNODE and see if they are used by single function FUNCTION.
1188 SINGLE_USER_MAP represents the dataflow lattice. */
1191 propagate_single_user (varpool_node
*vnode
, cgraph_node
*function
,
1192 hash_map
<varpool_node
*, cgraph_node
*> &single_user_map
)
1195 struct ipa_ref
*ref
;
1197 gcc_assert (!vnode
->externally_visible
);
1199 /* If node is an alias, first meet with its target. */
1201 function
= meet (function
, vnode
->get_alias_target (), single_user_map
);
1203 /* Check all users and see if they correspond to a single function. */
1204 for (i
= 0; vnode
->iterate_referring (i
, ref
) && function
!= BOTTOM
; i
++)
1206 struct cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (ref
->referring
);
1209 if (cnode
->global
.inlined_to
)
1210 cnode
= cnode
->global
.inlined_to
;
1213 else if (function
!= cnode
)
1217 function
= meet (function
, dyn_cast
<varpool_node
*> (ref
->referring
),
1223 /* Pass setting used_by_single_function flag.
1224 This flag is set on variable when there is only one function that may
1225 possibly referr to it. */
1228 ipa_single_use (void)
1230 varpool_node
*first
= (varpool_node
*) (void *) 1;
1232 hash_map
<varpool_node
*, cgraph_node
*> single_user_map
;
1234 FOR_EACH_DEFINED_VARIABLE (var
)
1235 if (!var
->all_refs_explicit_p ())
1239 /* Enqueue symbol for dataflow. */
1244 /* The actual dataflow. */
1246 while (first
!= (void *) 1)
1248 cgraph_node
*user
, *orig_user
, **f
;
1251 first
= (varpool_node
*)first
->aux
;
1253 f
= single_user_map
.get (var
);
1258 user
= propagate_single_user (var
, orig_user
, single_user_map
);
1260 gcc_checking_assert (var
->aux
!= BOTTOM
);
1262 /* If user differs, enqueue all references. */
1263 if (user
!= orig_user
)
1268 single_user_map
.put (var
, user
);
1270 /* Enqueue all aliases for re-processing. */
1271 for (i
= 0; var
->iterate_direct_aliases (i
, ref
); i
++)
1272 if (!ref
->referring
->aux
)
1274 ref
->referring
->aux
= first
;
1275 first
= dyn_cast
<varpool_node
*> (ref
->referring
);
1277 /* Enqueue all users for re-processing. */
1278 for (i
= 0; var
->iterate_reference (i
, ref
); i
++)
1279 if (!ref
->referred
->aux
1280 && ref
->referred
->definition
1281 && is_a
<varpool_node
*> (ref
->referred
))
1283 ref
->referred
->aux
= first
;
1284 first
= dyn_cast
<varpool_node
*> (ref
->referred
);
1287 /* If user is BOTTOM, just punt on this var. */
1297 FOR_EACH_DEFINED_VARIABLE (var
)
1299 if (var
->aux
!= BOTTOM
)
1301 /* Not having the single user known means that the VAR is
1302 unreachable. Either someone forgot to remove unreachable
1303 variables or the reachability here is wrong. */
1305 gcc_checking_assert (single_user_map
.get (var
));
1309 fprintf (dump_file
, "Variable %s is used by single function\n",
1312 var
->used_by_single_function
= true;
1321 const pass_data pass_data_ipa_single_use
=
1323 IPA_PASS
, /* type */
1324 "single-use", /* name */
1325 OPTGROUP_NONE
, /* optinfo_flags */
1326 TV_CGRAPHOPT
, /* tv_id */
1327 0, /* properties_required */
1328 0, /* properties_provided */
1329 0, /* properties_destroyed */
1330 0, /* todo_flags_start */
1331 0, /* todo_flags_finish */
1334 class pass_ipa_single_use
: public ipa_opt_pass_d
1337 pass_ipa_single_use (gcc::context
*ctxt
)
1338 : ipa_opt_pass_d (pass_data_ipa_single_use
, ctxt
,
1339 NULL
, /* generate_summary */
1340 NULL
, /* write_summary */
1341 NULL
, /* read_summary */
1342 NULL
, /* write_optimization_summary */
1343 NULL
, /* read_optimization_summary */
1344 NULL
, /* stmt_fixup */
1345 0, /* function_transform_todo_flags_start */
1346 NULL
, /* function_transform */
1347 NULL
) /* variable_transform */
1350 /* opt_pass methods: */
1351 virtual unsigned int execute (function
*) { return ipa_single_use (); }
1353 }; // class pass_ipa_single_use
1358 make_pass_ipa_single_use (gcc::context
*ctxt
)
1360 return new pass_ipa_single_use (ctxt
);
1363 /* Materialize all clones. */
1367 const pass_data pass_data_materialize_all_clones
=
1369 SIMPLE_IPA_PASS
, /* type */
1370 "materialize-all-clones", /* name */
1371 OPTGROUP_NONE
, /* optinfo_flags */
1372 TV_IPA_OPT
, /* tv_id */
1373 0, /* properties_required */
1374 0, /* properties_provided */
1375 0, /* properties_destroyed */
1376 0, /* todo_flags_start */
1377 0, /* todo_flags_finish */
1380 class pass_materialize_all_clones
: public simple_ipa_opt_pass
1383 pass_materialize_all_clones (gcc::context
*ctxt
)
1384 : simple_ipa_opt_pass (pass_data_materialize_all_clones
, ctxt
)
1387 /* opt_pass methods: */
1388 virtual unsigned int execute (function
*)
1390 symtab
->materialize_all_clones ();
1394 }; // class pass_materialize_all_clones
1398 simple_ipa_opt_pass
*
1399 make_pass_materialize_all_clones (gcc::context
*ctxt
)
1401 return new pass_materialize_all_clones (ctxt
);