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 "basic-block.h"
31 #include "plugin-api.h"
36 #include "hard-reg-set.h"
41 #include "tree-pass.h"
42 #include "gimple-expr.h"
46 #include "tree-iterator.h"
47 #include "ipa-utils.h"
48 #include "alloc-pool.h"
50 #include "ipa-inline.h"
51 #include "tree-inline.h"
54 #include "internal-fn.h"
55 #include "tree-ssa-alias.h"
60 /* Return true when NODE has ADDR reference. */
63 has_addr_references_p (struct cgraph_node
*node
,
64 void *data ATTRIBUTE_UNUSED
)
67 struct ipa_ref
*ref
= NULL
;
69 for (i
= 0; node
->iterate_referring (i
, ref
); i
++)
70 if (ref
->use
== IPA_REF_ADDR
)
75 /* Look for all functions inlined to NODE and update their inlined_to pointers
79 update_inlined_to_pointer (struct cgraph_node
*node
, struct cgraph_node
*inlined_to
)
81 struct cgraph_edge
*e
;
82 for (e
= node
->callees
; e
; e
= e
->next_callee
)
83 if (e
->callee
->global
.inlined_to
)
85 e
->callee
->global
.inlined_to
= inlined_to
;
86 update_inlined_to_pointer (e
->callee
, inlined_to
);
90 /* Add symtab NODE to queue starting at FIRST.
92 The queue is linked via AUX pointers and terminated by pointer to 1.
93 We enqueue nodes at two occasions: when we find them reachable or when we find
94 their bodies needed for further clonning. In the second case we mark them
95 by pointer to 2 after processing so they are re-queue when they become
99 enqueue_node (symtab_node
*node
, symtab_node
**first
,
100 hash_set
<symtab_node
*> *reachable
)
102 /* Node is still in queue; do nothing. */
103 if (node
->aux
&& node
->aux
!= (void *) 2)
105 /* Node was already processed as unreachable, re-enqueue
106 only if it became reachable now. */
107 if (node
->aux
== (void *)2 && !reachable
->contains (node
))
113 /* Process references. */
116 process_references (symtab_node
*snode
,
118 bool before_inlining_p
,
119 hash_set
<symtab_node
*> *reachable
)
122 struct ipa_ref
*ref
= NULL
;
123 for (i
= 0; snode
->iterate_reference (i
, ref
); i
++)
125 symtab_node
*node
= ref
->referred
;
126 symtab_node
*body
= node
->ultimate_alias_target ();
128 if (node
->definition
&& !node
->in_other_partition
129 && ((!DECL_EXTERNAL (node
->decl
) || node
->alias
)
130 || (((before_inlining_p
131 && (TREE_CODE (node
->decl
) != FUNCTION_DECL
132 || opt_for_fn (body
->decl
, optimize
)
133 || (symtab
->state
< IPA_SSA
136 DECL_ATTRIBUTES (body
->decl
))))))
137 /* We use variable constructors during late compilation for
138 constant folding. Keep references alive so partitioning
139 knows about potential references. */
140 || (TREE_CODE (node
->decl
) == VAR_DECL
142 && ctor_for_folding (node
->decl
)
143 != error_mark_node
))))
145 /* Be sure that we will not optimize out alias target
147 if (DECL_EXTERNAL (node
->decl
)
149 && before_inlining_p
)
150 reachable
->add (body
);
151 reachable
->add (node
);
153 enqueue_node (node
, first
, reachable
);
157 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
158 all its potential targets as reachable to permit later inlining if
159 devirtualization happens. After inlining still keep their declarations
160 around, so we can devirtualize to a direct call.
162 Also try to make trivial devirutalization when no or only one target is
166 walk_polymorphic_call_targets (hash_set
<void *> *reachable_call_targets
,
167 struct cgraph_edge
*edge
,
169 hash_set
<symtab_node
*> *reachable
,
170 bool before_inlining_p
)
175 vec
<cgraph_node
*>targets
176 = possible_polymorphic_call_targets
177 (edge
, &final
, &cache_token
);
179 if (!reachable_call_targets
->add (cache_token
))
181 for (i
= 0; i
< targets
.length (); i
++)
183 struct cgraph_node
*n
= targets
[i
];
185 /* Do not bother to mark virtual methods in anonymous namespace;
186 either we will find use of virtual table defining it, or it is
188 if (TREE_CODE (TREE_TYPE (n
->decl
)) == METHOD_TYPE
189 && type_in_anonymous_namespace_p
190 (method_class_type (TREE_TYPE (n
->decl
))))
193 symtab_node
*body
= n
->function_symbol ();
195 /* Prior inlining, keep alive bodies of possible targets for
198 && (before_inlining_p
199 && opt_for_fn (body
->decl
, optimize
)
200 && opt_for_fn (body
->decl
, flag_devirtualize
)))
202 /* Be sure that we will not optimize out alias target
204 if (DECL_EXTERNAL (n
->decl
)
206 && before_inlining_p
)
207 reachable
->add (body
);
210 /* Even after inlining we want to keep the possible targets in the
211 boundary, so late passes can still produce direct call even if
212 the chance for inlining is lost. */
213 enqueue_node (n
, first
, reachable
);
217 /* Very trivial devirtualization; when the type is
218 final or anonymous (so we know all its derivation)
219 and there is only one possible virtual call target,
220 make the edge direct. */
223 if (targets
.length () <= 1 && dbg_cnt (devirt
))
225 cgraph_node
*target
, *node
= edge
->caller
;
226 if (targets
.length () == 1)
229 target
= cgraph_node::get_create
230 (builtin_decl_implicit (BUILT_IN_UNREACHABLE
));
232 if (dump_enabled_p ())
236 locus
= gimple_location (edge
->call_stmt
);
238 locus
= UNKNOWN_LOCATION
;
239 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, locus
,
240 "devirtualizing call in %s/%i to %s/%i\n",
241 edge
->caller
->name (), edge
->caller
->order
,
245 edge
= edge
->make_direct (target
);
246 if (inline_summary_vec
)
247 inline_update_overall_summary (node
);
248 else if (edge
->call_stmt
)
250 edge
->redirect_call_stmt_to_callee ();
252 /* Call to __builtin_unreachable shouldn't be instrumented. */
253 if (!targets
.length ())
254 gimple_call_set_with_bounds (edge
->call_stmt
, false);
260 /* Perform reachability analysis and reclaim all unreachable nodes.
262 The algorithm is basically mark&sweep but with some extra refinements:
264 - reachable extern inline functions needs special handling; the bodies needs
265 to stay in memory until inlining in hope that they will be inlined.
266 After inlining we release their bodies and turn them into unanalyzed
267 nodes even when they are reachable.
269 - virtual functions are kept in callgraph even if they seem unreachable in
270 hope calls to them will be devirtualized.
272 Again we remove them after inlining. In late optimization some
273 devirtualization may happen, but it is not important since we won't inline
274 the call. In theory early opts and IPA should work out all important cases.
276 - virtual clones needs bodies of their origins for later materialization;
277 this means that we want to keep the body even if the origin is unreachable
278 otherwise. To avoid origin from sitting in the callgraph and being
279 walked by IPA passes, we turn them into unanalyzed nodes with body
282 We maintain set of function declaration where body needs to stay in
283 body_needed_for_clonning
285 Inline clones represent special case: their declaration match the
286 declaration of origin and cgraph_remove_node already knows how to
287 reshape callgraph and preserve body when offline copy of function or
288 inline clone is being removed.
290 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
291 variables with DECL_INITIAL set. We finalize these and keep reachable
292 ones around for constant folding purposes. After inlining we however
293 stop walking their references to let everything static referneced by them
294 to be removed when it is otherwise unreachable.
296 We maintain queue of both reachable symbols (i.e. defined symbols that needs
297 to stay) and symbols that are in boundary (i.e. external symbols referenced
298 by reachable symbols or origins of clones). The queue is represented
299 as linked list by AUX pointer terminated by 1.
301 At the end we keep all reachable symbols. For symbols in boundary we always
302 turn definition into a declaration, but we may keep function body around
303 based on body_needed_for_clonning
305 All symbols that enter the queue have AUX pointer non-zero and are in the
306 boundary. Pointer set REACHABLE is used to track reachable symbols.
308 Every symbol can be visited twice - once as part of boundary and once
309 as real reachable symbol. enqueue_node needs to decide whether the
310 node needs to be re-queued for second processing. For this purpose
311 we set AUX pointer of processed symbols in the boundary to constant 2. */
314 symbol_table::remove_unreachable_nodes (FILE *file
)
316 symtab_node
*first
= (symtab_node
*) (void *) 1;
317 struct cgraph_node
*node
, *next
;
318 varpool_node
*vnode
, *vnext
;
319 bool changed
= false;
320 hash_set
<symtab_node
*> reachable
;
321 hash_set
<tree
> body_needed_for_clonning
;
322 hash_set
<void *> reachable_call_targets
;
323 bool before_inlining_p
= symtab
->state
< (!optimize
? IPA_SSA
324 : IPA_SSA_AFTER_INLINING
);
326 timevar_push (TV_IPA_UNREACHABLE
);
327 build_type_inheritance_graph ();
329 fprintf (file
, "\nReclaiming functions:");
330 #ifdef ENABLE_CHECKING
331 FOR_EACH_FUNCTION (node
)
332 gcc_assert (!node
->aux
);
333 FOR_EACH_VARIABLE (vnode
)
334 gcc_assert (!vnode
->aux
);
336 /* Mark functions whose bodies are obviously needed.
337 This is mostly when they can be referenced externally. Inline clones
338 are special since their declarations are shared with master clone and thus
339 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
340 FOR_EACH_FUNCTION (node
)
342 node
->used_as_abstract_origin
= false;
344 && !node
->global
.inlined_to
345 && !node
->in_other_partition
346 && !node
->can_remove_if_no_direct_calls_and_refs_p ())
348 gcc_assert (!node
->global
.inlined_to
);
349 reachable
.add (node
);
350 enqueue_node (node
, &first
, &reachable
);
353 gcc_assert (!node
->aux
);
356 /* Mark variables that are obviously needed. */
357 FOR_EACH_DEFINED_VARIABLE (vnode
)
358 if (!vnode
->can_remove_if_no_refs_p()
359 && !vnode
->in_other_partition
)
361 reachable
.add (vnode
);
362 enqueue_node (vnode
, &first
, &reachable
);
365 /* Perform reachability analysis. */
366 while (first
!= (symtab_node
*) (void *) 1)
368 bool in_boundary_p
= !reachable
.contains (first
);
369 symtab_node
*node
= first
;
371 first
= (symtab_node
*)first
->aux
;
373 /* If we are processing symbol in boundary, mark its AUX pointer for
374 possible later re-processing in enqueue_node. */
376 node
->aux
= (void *)2;
379 if (TREE_CODE (node
->decl
) == FUNCTION_DECL
380 && DECL_ABSTRACT_ORIGIN (node
->decl
))
382 struct cgraph_node
*origin_node
383 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node
->decl
));
384 if (origin_node
&& !origin_node
->used_as_abstract_origin
)
386 origin_node
->used_as_abstract_origin
= true;
387 gcc_assert (!origin_node
->prev_sibling_clone
);
388 gcc_assert (!origin_node
->next_sibling_clone
);
389 for (cgraph_node
*n
= origin_node
->clones
; n
;
390 n
= n
->next_sibling_clone
)
391 if (n
->decl
== DECL_ABSTRACT_ORIGIN (node
->decl
))
392 n
->used_as_abstract_origin
= true;
393 enqueue_node (origin_node
, &first
, &reachable
);
396 /* If any symbol in a comdat group is reachable, force
397 all externally visible symbols in the same comdat
398 group to be reachable as well. Comdat-local symbols
399 can be discarded if all uses were inlined. */
400 if (node
->same_comdat_group
)
403 for (next
= node
->same_comdat_group
;
405 next
= next
->same_comdat_group
)
406 if (!next
->comdat_local_p ()
407 && !reachable
.add (next
))
408 enqueue_node (next
, &first
, &reachable
);
410 /* Mark references as reachable. */
411 process_references (node
, &first
, before_inlining_p
, &reachable
);
414 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
416 /* Mark the callees reachable unless they are direct calls to extern
417 inline functions we decided to not inline. */
420 struct cgraph_edge
*e
;
421 /* Keep alive possible targets for devirtualization. */
422 if (opt_for_fn (cnode
->decl
, optimize
)
423 && opt_for_fn (cnode
->decl
, flag_devirtualize
))
425 struct cgraph_edge
*next
;
426 for (e
= cnode
->indirect_calls
; e
; e
= next
)
428 next
= e
->next_callee
;
429 if (e
->indirect_info
->polymorphic
)
430 walk_polymorphic_call_targets (&reachable_call_targets
,
431 e
, &first
, &reachable
,
435 for (e
= cnode
->callees
; e
; e
= e
->next_callee
)
437 symtab_node
*body
= e
->callee
->function_symbol ();
438 if (e
->callee
->definition
439 && !e
->callee
->in_other_partition
440 && (!e
->inline_failed
441 || !DECL_EXTERNAL (e
->callee
->decl
)
443 || (before_inlining_p
444 && (opt_for_fn (body
->decl
, optimize
)
445 || (symtab
->state
< IPA_SSA
448 DECL_ATTRIBUTES (body
->decl
)))))))
450 /* Be sure that we will not optimize out alias target
452 if (DECL_EXTERNAL (e
->callee
->decl
)
454 && before_inlining_p
)
455 reachable
.add (body
);
456 reachable
.add (e
->callee
);
458 enqueue_node (e
->callee
, &first
, &reachable
);
461 /* When inline clone exists, mark body to be preserved so when removing
462 offline copy of the function we don't kill it. */
463 if (cnode
->global
.inlined_to
)
464 body_needed_for_clonning
.add (cnode
->decl
);
466 /* For non-inline clones, force their origins to the boundary and ensure
467 that body is not removed. */
468 while (cnode
->clone_of
)
470 bool noninline
= cnode
->clone_of
->decl
!= cnode
->decl
;
471 cnode
= cnode
->clone_of
;
474 body_needed_for_clonning
.add (cnode
->decl
);
475 enqueue_node (cnode
, &first
, &reachable
);
480 /* If any reachable function has simd clones, mark them as
481 reachable as well. */
482 if (cnode
->simd_clones
)
485 for (next
= cnode
->simd_clones
;
487 next
= next
->simdclone
->next_clone
)
489 || !reachable
.add (next
))
490 enqueue_node (next
, &first
, &reachable
);
493 /* When we see constructor of external variable, keep referred nodes in the
494 boundary. This will also hold initializers of the external vars NODE
496 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
);
498 && DECL_EXTERNAL (node
->decl
)
502 struct ipa_ref
*ref
= NULL
;
503 for (int i
= 0; node
->iterate_reference (i
, ref
); i
++)
504 enqueue_node (ref
->referred
, &first
, &reachable
);
508 /* Remove unreachable functions. */
509 for (node
= first_function (); node
; node
= next
)
511 next
= next_function (node
);
513 /* If node is not needed at all, remove it. */
517 fprintf (file
, " %s/%i", node
->name (), node
->order
);
521 /* If node is unreachable, remove its body. */
522 else if (!reachable
.contains (node
))
524 if (!body_needed_for_clonning
.contains (node
->decl
))
525 node
->release_body ();
526 else if (!node
->clone_of
)
527 gcc_assert (in_lto_p
|| DECL_RESULT (node
->decl
));
528 if (node
->definition
)
531 fprintf (file
, " %s/%i", node
->name (), node
->order
);
532 node
->body_removed
= true;
533 node
->analyzed
= false;
534 node
->definition
= false;
535 node
->cpp_implicit_alias
= false;
537 node
->thunk
.thunk_p
= false;
538 node
->weakref
= false;
539 /* After early inlining we drop always_inline attributes on
540 bodies of functions that are still referenced (have their
542 DECL_ATTRIBUTES (node
->decl
)
543 = remove_attribute ("always_inline",
544 DECL_ATTRIBUTES (node
->decl
));
545 if (!node
->in_other_partition
)
546 node
->local
.local
= false;
547 node
->remove_callees ();
548 node
->remove_from_same_comdat_group ();
549 node
->remove_all_references ();
551 if (node
->thunk
.thunk_p
552 && node
->thunk
.add_pointer_bounds_args
)
554 node
->thunk
.thunk_p
= false;
555 node
->thunk
.add_pointer_bounds_args
= false;
560 gcc_assert (node
->clone_of
|| !node
->has_gimple_body_p ()
561 || in_lto_p
|| DECL_RESULT (node
->decl
));
564 /* Inline clones might be kept around so their materializing allows further
565 cloning. If the function the clone is inlined into is removed, we need
566 to turn it into normal cone. */
567 FOR_EACH_FUNCTION (node
)
569 if (node
->global
.inlined_to
572 gcc_assert (node
->clones
);
573 node
->global
.inlined_to
= NULL
;
574 update_inlined_to_pointer (node
, node
);
579 /* Remove unreachable variables. */
581 fprintf (file
, "\nReclaiming variables:");
582 for (vnode
= first_variable (); vnode
; vnode
= vnext
)
584 vnext
= next_variable (vnode
);
586 /* For can_refer_decl_in_current_unit_p we want to track for
587 all external variables if they are defined in other partition
589 && (!flag_ltrans
|| !DECL_EXTERNAL (vnode
->decl
)))
592 fprintf (file
, " %s/%i", vnode
->name (), vnode
->order
);
596 else if (!reachable
.contains (vnode
))
599 if (vnode
->definition
)
602 fprintf (file
, " %s", vnode
->name ());
605 /* Keep body if it may be useful for constant folding. */
606 if ((init
= ctor_for_folding (vnode
->decl
)) == error_mark_node
607 && !POINTER_BOUNDS_P (vnode
->decl
))
608 vnode
->remove_initializer ();
610 DECL_INITIAL (vnode
->decl
) = init
;
611 vnode
->body_removed
= true;
612 vnode
->definition
= false;
613 vnode
->analyzed
= false;
616 vnode
->remove_from_same_comdat_group ();
618 vnode
->remove_all_references ();
624 /* Now update address_taken flags and try to promote functions to be local. */
626 fprintf (file
, "\nClearing address taken flags:");
627 FOR_EACH_DEFINED_FUNCTION (node
)
628 if (node
->address_taken
629 && !node
->used_from_other_partition
)
631 if (!node
->call_for_symbol_thunks_and_aliases
632 (has_addr_references_p
, NULL
, true)
633 && (!node
->instrumentation_clone
634 || !node
->instrumented_version
635 || !node
->instrumented_version
->address_taken
))
638 fprintf (file
, " %s", node
->name ());
639 node
->address_taken
= false;
641 if (node
->local_p ())
643 node
->local
.local
= true;
645 fprintf (file
, " (local)");
650 fprintf (file
, "\n");
652 #ifdef ENABLE_CHECKING
653 symtab_node::verify_symtab_nodes ();
656 /* If we removed something, perhaps profile could be improved. */
657 if (changed
&& optimize
&& inline_edge_summary_vec
.exists ())
658 FOR_EACH_DEFINED_FUNCTION (node
)
659 ipa_propagate_frequency (node
);
661 timevar_pop (TV_IPA_UNREACHABLE
);
665 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
666 as needed, also clear EXPLICIT_REFS if the references to given variable
667 do not need to be explicit. */
670 process_references (varpool_node
*vnode
,
671 bool *written
, bool *address_taken
,
672 bool *read
, bool *explicit_refs
)
677 if (!vnode
->all_refs_explicit_p ()
678 || TREE_THIS_VOLATILE (vnode
->decl
))
679 *explicit_refs
= false;
681 for (i
= 0; vnode
->iterate_referring (i
, ref
)
682 && *explicit_refs
&& (!*written
|| !*address_taken
|| !*read
); i
++)
686 *address_taken
= true;
695 process_references (dyn_cast
<varpool_node
*> (ref
->referring
), written
,
696 address_taken
, read
, explicit_refs
);
703 /* Set TREE_READONLY bit. */
706 set_readonly_bit (varpool_node
*vnode
, void *data ATTRIBUTE_UNUSED
)
708 TREE_READONLY (vnode
->decl
) = true;
712 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
715 set_writeonly_bit (varpool_node
*vnode
, void *data ATTRIBUTE_UNUSED
)
717 vnode
->writeonly
= true;
720 DECL_INITIAL (vnode
->decl
) = NULL
;
722 vnode
->remove_all_references ();
727 /* Clear addressale bit of VNODE. */
730 clear_addressable_bit (varpool_node
*vnode
, void *data ATTRIBUTE_UNUSED
)
732 vnode
->address_taken
= false;
733 TREE_ADDRESSABLE (vnode
->decl
) = 0;
737 /* Discover variables that have no longer address taken or that are read only
738 and update their flags.
740 FIXME: This can not be done in between gimplify and omp_expand since
741 readonly flag plays role on what is shared and what is not. Currently we do
742 this transformation as part of whole program visibility and re-do at
743 ipa-reference pass (to take into account clonning), but it would
744 make sense to do it before early optimizations. */
747 ipa_discover_readonly_nonaddressable_vars (void)
751 fprintf (dump_file
, "Clearing variable flags:");
752 FOR_EACH_VARIABLE (vnode
)
754 && (TREE_ADDRESSABLE (vnode
->decl
)
756 || !TREE_READONLY (vnode
->decl
)))
758 bool written
= false;
759 bool address_taken
= false;
761 bool explicit_refs
= true;
763 process_references (vnode
, &written
, &address_taken
, &read
, &explicit_refs
);
768 if (TREE_ADDRESSABLE (vnode
->decl
) && dump_file
)
769 fprintf (dump_file
, " %s (non-addressable)", vnode
->name ());
770 vnode
->call_for_node_and_aliases (clear_addressable_bit
, NULL
, true);
772 if (!address_taken
&& !written
773 /* Making variable in explicit section readonly can cause section
775 See e.g. gcc.c-torture/compile/pr23237.c */
776 && vnode
->get_section () == NULL
)
778 if (!TREE_READONLY (vnode
->decl
) && dump_file
)
779 fprintf (dump_file
, " %s (read-only)", vnode
->name ());
780 vnode
->call_for_node_and_aliases (set_readonly_bit
, NULL
, true);
782 if (!vnode
->writeonly
&& !read
&& !address_taken
&& written
)
785 fprintf (dump_file
, " %s (write-only)", vnode
->name ());
786 vnode
->call_for_node_and_aliases (set_writeonly_bit
, NULL
, true);
790 fprintf (dump_file
, "\n");
793 /* Free inline summary. */
797 const pass_data pass_data_ipa_free_inline_summary
=
799 SIMPLE_IPA_PASS
, /* type */
800 "free-inline-summary", /* name */
801 OPTGROUP_NONE
, /* optinfo_flags */
802 TV_IPA_FREE_INLINE_SUMMARY
, /* tv_id */
803 0, /* properties_required */
804 0, /* properties_provided */
805 0, /* properties_destroyed */
806 0, /* todo_flags_start */
807 /* Early optimizations may make function unreachable. We can not
808 remove unreachable functions as part of the ealry opts pass because
809 TODOs are run before subpasses. Do it here. */
810 ( TODO_remove_functions
| TODO_dump_symtab
), /* todo_flags_finish */
813 class pass_ipa_free_inline_summary
: public simple_ipa_opt_pass
816 pass_ipa_free_inline_summary (gcc::context
*ctxt
)
817 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary
, ctxt
)
820 /* opt_pass methods: */
821 virtual unsigned int execute (function
*)
823 inline_free_summary ();
827 }; // class pass_ipa_free_inline_summary
831 simple_ipa_opt_pass
*
832 make_pass_ipa_free_inline_summary (gcc::context
*ctxt
)
834 return new pass_ipa_free_inline_summary (ctxt
);
837 /* Generate and emit a static constructor or destructor. WHICH must
838 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
839 (for chp static vars constructor) or 'B' (for chkp static bounds
840 constructor). BODY is a STATEMENT_LIST containing GENERIC
841 statements. PRIORITY is the initialization priority for this
842 constructor or destructor.
844 FINAL specify whether the externally visible name for collect2 should
848 cgraph_build_static_cdtor_1 (char which
, tree body
, int priority
, bool final
)
850 static int counter
= 0;
852 tree decl
, name
, resdecl
;
854 /* The priority is encoded in the constructor or destructor name.
855 collect2 will sort the names and arrange that they are called at
858 sprintf (which_buf
, "%c_%.5d_%d", which
, priority
, counter
++);
860 /* Proudce sane name but one not recognizable by collect2, just for the
861 case we fail to inline the function. */
862 sprintf (which_buf
, "sub_%c_%.5d_%d", which
, priority
, counter
++);
863 name
= get_file_function_name (which_buf
);
865 decl
= build_decl (input_location
, FUNCTION_DECL
, name
,
866 build_function_type_list (void_type_node
, NULL_TREE
));
867 current_function_decl
= decl
;
869 resdecl
= build_decl (input_location
,
870 RESULT_DECL
, NULL_TREE
, void_type_node
);
871 DECL_ARTIFICIAL (resdecl
) = 1;
872 DECL_RESULT (decl
) = resdecl
;
873 DECL_CONTEXT (resdecl
) = decl
;
875 allocate_struct_function (decl
, false);
877 TREE_STATIC (decl
) = 1;
878 TREE_USED (decl
) = 1;
879 DECL_ARTIFICIAL (decl
) = 1;
880 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
881 DECL_SAVED_TREE (decl
) = body
;
882 if (!targetm
.have_ctors_dtors
&& final
)
884 TREE_PUBLIC (decl
) = 1;
885 DECL_PRESERVE_P (decl
) = 1;
887 DECL_UNINLINABLE (decl
) = 1;
889 DECL_INITIAL (decl
) = make_node (BLOCK
);
890 TREE_USED (DECL_INITIAL (decl
)) = 1;
892 DECL_SOURCE_LOCATION (decl
) = input_location
;
893 cfun
->function_end_locus
= input_location
;
898 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
899 decl_init_priority_insert (decl
, priority
);
902 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
903 DECL_ATTRIBUTES (decl
) = tree_cons (get_identifier ("chkp ctor"),
906 decl_init_priority_insert (decl
, priority
);
909 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
910 DECL_ATTRIBUTES (decl
) = tree_cons (get_identifier ("bnd_legacy"),
913 decl_init_priority_insert (decl
, priority
);
916 DECL_STATIC_DESTRUCTOR (decl
) = 1;
917 decl_fini_priority_insert (decl
, priority
);
923 gimplify_function_tree (decl
);
925 cgraph_node::add_new_function (decl
, false);
928 current_function_decl
= NULL
;
931 /* Generate and emit a static constructor or destructor. WHICH must
932 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
933 (for chkp static vars constructor) or 'B' (for chkp static bounds
934 constructor). BODY is a STATEMENT_LIST containing GENERIC
935 statements. PRIORITY is the initialization priority for this
936 constructor or destructor. */
939 cgraph_build_static_cdtor (char which
, tree body
, int priority
)
941 cgraph_build_static_cdtor_1 (which
, body
, priority
, false);
944 /* A vector of FUNCTION_DECLs declared as static constructors. */
945 static vec
<tree
> static_ctors
;
946 /* A vector of FUNCTION_DECLs declared as static destructors. */
947 static vec
<tree
> static_dtors
;
949 /* When target does not have ctors and dtors, we call all constructor
950 and destructor by special initialization/destruction function
951 recognized by collect2.
953 When we are going to build this function, collect all constructors and
954 destructors and turn them into normal functions. */
957 record_cdtor_fn (struct cgraph_node
*node
)
959 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
960 static_ctors
.safe_push (node
->decl
);
961 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
962 static_dtors
.safe_push (node
->decl
);
963 node
= cgraph_node::get (node
->decl
);
964 DECL_DISREGARD_INLINE_LIMITS (node
->decl
) = 1;
967 /* Define global constructors/destructor functions for the CDTORS, of
968 which they are LEN. The CDTORS are sorted by initialization
969 priority. If CTOR_P is true, these are constructors; otherwise,
970 they are destructors. */
973 build_cdtor (bool ctor_p
, vec
<tree
> cdtors
)
976 size_t len
= cdtors
.length ();
983 priority_type priority
;
992 p
= ctor_p
? DECL_INIT_PRIORITY (fn
) : DECL_FINI_PRIORITY (fn
);
995 else if (p
!= priority
)
1001 /* When there is only one cdtor and target supports them, do nothing. */
1003 && targetm
.have_ctors_dtors
)
1008 /* Find the next batch of constructors/destructors with the same
1009 initialization priority. */
1014 call
= build_call_expr (fn
, 0);
1016 DECL_STATIC_CONSTRUCTOR (fn
) = 0;
1018 DECL_STATIC_DESTRUCTOR (fn
) = 0;
1019 /* We do not want to optimize away pure/const calls here.
1020 When optimizing, these should be already removed, when not
1021 optimizing, we want user to be able to breakpoint in them. */
1022 TREE_SIDE_EFFECTS (call
) = 1;
1023 append_to_statement_list (call
, &body
);
1025 gcc_assert (body
!= NULL_TREE
);
1026 /* Generate a function to call all the function of like
1028 cgraph_build_static_cdtor_1 (ctor_p
? 'I' : 'D', body
, priority
, true);
1032 /* Comparison function for qsort. P1 and P2 are actually of type
1033 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1034 used to determine the sort order. */
1037 compare_ctor (const void *p1
, const void *p2
)
1044 f1
= *(const tree
*)p1
;
1045 f2
= *(const tree
*)p2
;
1046 priority1
= DECL_INIT_PRIORITY (f1
);
1047 priority2
= DECL_INIT_PRIORITY (f2
);
1049 if (priority1
< priority2
)
1051 else if (priority1
> priority2
)
1054 /* Ensure a stable sort. Constructors are executed in backwarding
1055 order to make LTO initialize braries first. */
1056 return DECL_UID (f2
) - DECL_UID (f1
);
1059 /* Comparison function for qsort. P1 and P2 are actually of type
1060 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1061 used to determine the sort order. */
1064 compare_dtor (const void *p1
, const void *p2
)
1071 f1
= *(const tree
*)p1
;
1072 f2
= *(const tree
*)p2
;
1073 priority1
= DECL_FINI_PRIORITY (f1
);
1074 priority2
= DECL_FINI_PRIORITY (f2
);
1076 if (priority1
< priority2
)
1078 else if (priority1
> priority2
)
1081 /* Ensure a stable sort. */
1082 return DECL_UID (f1
) - DECL_UID (f2
);
1085 /* Generate functions to call static constructors and destructors
1086 for targets that do not support .ctors/.dtors sections. These
1087 functions have magic names which are detected by collect2. */
1090 build_cdtor_fns (void)
1092 if (!static_ctors
.is_empty ())
1094 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1095 static_ctors
.qsort (compare_ctor
);
1096 build_cdtor (/*ctor_p=*/true, static_ctors
);
1099 if (!static_dtors
.is_empty ())
1101 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1102 static_dtors
.qsort (compare_dtor
);
1103 build_cdtor (/*ctor_p=*/false, static_dtors
);
1107 /* Look for constructors and destructors and produce function calling them.
1108 This is needed for targets not supporting ctors or dtors, but we perform the
1109 transformation also at linktime to merge possibly numerous
1110 constructors/destructors into single function to improve code locality and
1114 ipa_cdtor_merge (void)
1116 struct cgraph_node
*node
;
1117 FOR_EACH_DEFINED_FUNCTION (node
)
1118 if (DECL_STATIC_CONSTRUCTOR (node
->decl
)
1119 || DECL_STATIC_DESTRUCTOR (node
->decl
))
1120 record_cdtor_fn (node
);
1122 static_ctors
.release ();
1123 static_dtors
.release ();
1129 const pass_data pass_data_ipa_cdtor_merge
=
1131 IPA_PASS
, /* type */
1133 OPTGROUP_NONE
, /* optinfo_flags */
1134 TV_CGRAPHOPT
, /* tv_id */
1135 0, /* properties_required */
1136 0, /* properties_provided */
1137 0, /* properties_destroyed */
1138 0, /* todo_flags_start */
1139 0, /* todo_flags_finish */
1142 class pass_ipa_cdtor_merge
: public ipa_opt_pass_d
1145 pass_ipa_cdtor_merge (gcc::context
*ctxt
)
1146 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge
, ctxt
,
1147 NULL
, /* generate_summary */
1148 NULL
, /* write_summary */
1149 NULL
, /* read_summary */
1150 NULL
, /* write_optimization_summary */
1151 NULL
, /* read_optimization_summary */
1152 NULL
, /* stmt_fixup */
1153 0, /* function_transform_todo_flags_start */
1154 NULL
, /* function_transform */
1155 NULL
) /* variable_transform */
1158 /* opt_pass methods: */
1159 virtual bool gate (function
*);
1160 virtual unsigned int execute (function
*) { return ipa_cdtor_merge (); }
1162 }; // class pass_ipa_cdtor_merge
1165 pass_ipa_cdtor_merge::gate (function
*)
1167 /* Perform the pass when we have no ctors/dtors support
1168 or at LTO time to merge multiple constructors into single
1170 return !targetm
.have_ctors_dtors
|| (optimize
&& in_lto_p
);
1176 make_pass_ipa_cdtor_merge (gcc::context
*ctxt
)
1178 return new pass_ipa_cdtor_merge (ctxt
);
1181 /* Invalid pointer representing BOTTOM for single user dataflow. */
1182 #define BOTTOM ((cgraph_node *)(size_t) 2)
1184 /* Meet operation for single user dataflow.
1185 Here we want to associate variables with sigle function that may access it.
1187 FUNCTION is current single user of a variable, VAR is variable that uses it.
1188 Latttice is stored in SINGLE_USER_MAP.
1191 - TOP by no entry in SIGNLE_USER_MAP
1192 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1193 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1196 meet (cgraph_node
*function
, varpool_node
*var
,
1197 hash_map
<varpool_node
*, cgraph_node
*> &single_user_map
)
1199 struct cgraph_node
*user
, **f
;
1201 if (var
->aux
== BOTTOM
)
1204 f
= single_user_map
.get (var
);
1210 else if (function
!= user
)
1216 /* Propagation step of single-use dataflow.
1218 Check all uses of VNODE and see if they are used by single function FUNCTION.
1219 SINGLE_USER_MAP represents the dataflow lattice. */
1222 propagate_single_user (varpool_node
*vnode
, cgraph_node
*function
,
1223 hash_map
<varpool_node
*, cgraph_node
*> &single_user_map
)
1226 struct ipa_ref
*ref
;
1228 gcc_assert (!vnode
->externally_visible
);
1230 /* If node is an alias, first meet with its target. */
1232 function
= meet (function
, vnode
->get_alias_target (), single_user_map
);
1234 /* Check all users and see if they correspond to a single function. */
1235 for (i
= 0; vnode
->iterate_referring (i
, ref
) && function
!= BOTTOM
; i
++)
1237 struct cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (ref
->referring
);
1240 if (cnode
->global
.inlined_to
)
1241 cnode
= cnode
->global
.inlined_to
;
1244 else if (function
!= cnode
)
1248 function
= meet (function
, dyn_cast
<varpool_node
*> (ref
->referring
),
1254 /* Pass setting used_by_single_function flag.
1255 This flag is set on variable when there is only one function that may
1256 possibly referr to it. */
1259 ipa_single_use (void)
1261 varpool_node
*first
= (varpool_node
*) (void *) 1;
1263 hash_map
<varpool_node
*, cgraph_node
*> single_user_map
;
1265 FOR_EACH_DEFINED_VARIABLE (var
)
1266 if (!var
->all_refs_explicit_p ())
1270 /* Enqueue symbol for dataflow. */
1275 /* The actual dataflow. */
1277 while (first
!= (void *) 1)
1279 cgraph_node
*user
, *orig_user
, **f
;
1282 first
= (varpool_node
*)first
->aux
;
1284 f
= single_user_map
.get (var
);
1289 user
= propagate_single_user (var
, orig_user
, single_user_map
);
1291 gcc_checking_assert (var
->aux
!= BOTTOM
);
1293 /* If user differs, enqueue all references. */
1294 if (user
!= orig_user
)
1299 single_user_map
.put (var
, user
);
1301 /* Enqueue all aliases for re-processing. */
1302 for (i
= 0; var
->iterate_referring (i
, ref
); i
++)
1303 if (ref
->use
== IPA_REF_ALIAS
1304 && !ref
->referring
->aux
)
1306 ref
->referring
->aux
= first
;
1307 first
= dyn_cast
<varpool_node
*> (ref
->referring
);
1309 /* Enqueue all users for re-processing. */
1310 for (i
= 0; var
->iterate_reference (i
, ref
); i
++)
1311 if (!ref
->referred
->aux
1312 && ref
->referred
->definition
1313 && is_a
<varpool_node
*> (ref
->referred
))
1315 ref
->referred
->aux
= first
;
1316 first
= dyn_cast
<varpool_node
*> (ref
->referred
);
1319 /* If user is BOTTOM, just punt on this var. */
1329 FOR_EACH_DEFINED_VARIABLE (var
)
1331 if (var
->aux
!= BOTTOM
)
1333 #ifdef ENABLE_CHECKING
1334 /* Not having the single user known means that the VAR is
1335 unreachable. Either someone forgot to remove unreachable
1336 variables or the reachability here is wrong. */
1338 gcc_assert (single_user_map
.get (var
));
1342 fprintf (dump_file
, "Variable %s/%i is used by single function\n",
1343 var
->name (), var
->order
);
1345 var
->used_by_single_function
= true;
1354 const pass_data pass_data_ipa_single_use
=
1356 IPA_PASS
, /* type */
1357 "single-use", /* name */
1358 OPTGROUP_NONE
, /* optinfo_flags */
1359 TV_CGRAPHOPT
, /* tv_id */
1360 0, /* properties_required */
1361 0, /* properties_provided */
1362 0, /* properties_destroyed */
1363 0, /* todo_flags_start */
1364 0, /* todo_flags_finish */
1367 class pass_ipa_single_use
: public ipa_opt_pass_d
1370 pass_ipa_single_use (gcc::context
*ctxt
)
1371 : ipa_opt_pass_d (pass_data_ipa_single_use
, ctxt
,
1372 NULL
, /* generate_summary */
1373 NULL
, /* write_summary */
1374 NULL
, /* read_summary */
1375 NULL
, /* write_optimization_summary */
1376 NULL
, /* read_optimization_summary */
1377 NULL
, /* stmt_fixup */
1378 0, /* function_transform_todo_flags_start */
1379 NULL
, /* function_transform */
1380 NULL
) /* variable_transform */
1383 /* opt_pass methods: */
1384 virtual bool gate (function
*);
1385 virtual unsigned int execute (function
*) { return ipa_single_use (); }
1387 }; // class pass_ipa_single_use
1390 pass_ipa_single_use::gate (function
*)
1398 make_pass_ipa_single_use (gcc::context
*ctxt
)
1400 return new pass_ipa_single_use (ctxt
);