1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003-2015 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 "fold-const.h"
36 #include "tree-iterator.h"
37 #include "ipa-utils.h"
38 #include "symbol-summary.h"
40 #include "ipa-inline.h"
41 #include "tree-inline.h"
44 #include "internal-fn.h"
48 /* Return true when NODE has ADDR reference. */
51 has_addr_references_p (struct cgraph_node
*node
,
52 void *data ATTRIBUTE_UNUSED
)
55 struct ipa_ref
*ref
= NULL
;
57 for (i
= 0; node
->iterate_referring (i
, ref
); i
++)
58 if (ref
->use
== IPA_REF_ADDR
)
63 /* Look for all functions inlined to NODE and update their inlined_to pointers
67 update_inlined_to_pointer (struct cgraph_node
*node
, struct cgraph_node
*inlined_to
)
69 struct cgraph_edge
*e
;
70 for (e
= node
->callees
; e
; e
= e
->next_callee
)
71 if (e
->callee
->global
.inlined_to
)
73 e
->callee
->global
.inlined_to
= inlined_to
;
74 update_inlined_to_pointer (e
->callee
, inlined_to
);
78 /* Add symtab NODE to queue starting at FIRST.
80 The queue is linked via AUX pointers and terminated by pointer to 1.
81 We enqueue nodes at two occasions: when we find them reachable or when we find
82 their bodies needed for further clonning. In the second case we mark them
83 by pointer to 2 after processing so they are re-queue when they become
87 enqueue_node (symtab_node
*node
, symtab_node
**first
,
88 hash_set
<symtab_node
*> *reachable
)
90 /* Node is still in queue; do nothing. */
91 if (node
->aux
&& node
->aux
!= (void *) 2)
93 /* Node was already processed as unreachable, re-enqueue
94 only if it became reachable now. */
95 if (node
->aux
== (void *)2 && !reachable
->contains (node
))
101 /* Process references. */
104 process_references (symtab_node
*snode
,
106 bool before_inlining_p
,
107 hash_set
<symtab_node
*> *reachable
)
110 struct ipa_ref
*ref
= NULL
;
111 for (i
= 0; snode
->iterate_reference (i
, ref
); i
++)
113 symtab_node
*node
= ref
->referred
;
114 symtab_node
*body
= node
->ultimate_alias_target ();
116 if (node
->definition
&& !node
->in_other_partition
117 && ((!DECL_EXTERNAL (node
->decl
) || node
->alias
)
118 || (((before_inlining_p
119 && ((TREE_CODE (node
->decl
) != FUNCTION_DECL
121 || (TREE_CODE (node
->decl
) == FUNCTION_DECL
122 && opt_for_fn (body
->decl
, optimize
))
123 || (symtab
->state
< IPA_SSA
126 DECL_ATTRIBUTES (body
->decl
))))))
127 /* We use variable constructors during late compilation for
128 constant folding. Keep references alive so partitioning
129 knows about potential references. */
130 || (TREE_CODE (node
->decl
) == VAR_DECL
132 && ctor_for_folding (node
->decl
)
133 != error_mark_node
))))
135 /* Be sure that we will not optimize out alias target
137 if (DECL_EXTERNAL (node
->decl
)
139 && before_inlining_p
)
140 reachable
->add (body
);
141 reachable
->add (node
);
143 enqueue_node (node
, first
, reachable
);
147 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
148 all its potential targets as reachable to permit later inlining if
149 devirtualization happens. After inlining still keep their declarations
150 around, so we can devirtualize to a direct call.
152 Also try to make trivial devirutalization when no or only one target is
156 walk_polymorphic_call_targets (hash_set
<void *> *reachable_call_targets
,
157 struct cgraph_edge
*edge
,
159 hash_set
<symtab_node
*> *reachable
,
160 bool before_inlining_p
)
165 vec
<cgraph_node
*>targets
166 = possible_polymorphic_call_targets
167 (edge
, &final
, &cache_token
);
169 if (!reachable_call_targets
->add (cache_token
))
171 for (i
= 0; i
< targets
.length (); i
++)
173 struct cgraph_node
*n
= targets
[i
];
175 /* Do not bother to mark virtual methods in anonymous namespace;
176 either we will find use of virtual table defining it, or it is
178 if (TREE_CODE (TREE_TYPE (n
->decl
)) == METHOD_TYPE
179 && type_in_anonymous_namespace_p
180 (TYPE_METHOD_BASETYPE (TREE_TYPE (n
->decl
))))
183 symtab_node
*body
= n
->function_symbol ();
185 /* Prior inlining, keep alive bodies of possible targets for
188 && (before_inlining_p
189 && opt_for_fn (body
->decl
, optimize
)
190 && opt_for_fn (body
->decl
, flag_devirtualize
)))
192 /* Be sure that we will not optimize out alias target
194 if (DECL_EXTERNAL (n
->decl
)
196 && before_inlining_p
)
197 reachable
->add (body
);
200 /* Even after inlining we want to keep the possible targets in the
201 boundary, so late passes can still produce direct call even if
202 the chance for inlining is lost. */
203 enqueue_node (n
, first
, reachable
);
207 /* Very trivial devirtualization; when the type is
208 final or anonymous (so we know all its derivation)
209 and there is only one possible virtual call target,
210 make the edge direct. */
213 if (targets
.length () <= 1 && dbg_cnt (devirt
))
215 cgraph_node
*target
, *node
= edge
->caller
;
216 if (targets
.length () == 1)
219 target
= cgraph_node::get_create
220 (builtin_decl_implicit (BUILT_IN_UNREACHABLE
));
222 if (dump_enabled_p ())
226 locus
= gimple_location (edge
->call_stmt
);
228 locus
= UNKNOWN_LOCATION
;
229 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, locus
,
230 "devirtualizing call in %s/%i to %s/%i\n",
231 edge
->caller
->name (), edge
->caller
->order
,
235 edge
= edge
->make_direct (target
);
236 if (inline_summaries
)
237 inline_update_overall_summary (node
);
238 else if (edge
->call_stmt
)
240 edge
->redirect_call_stmt_to_callee ();
242 /* Call to __builtin_unreachable shouldn't be instrumented. */
243 if (!targets
.length ())
244 gimple_call_set_with_bounds (edge
->call_stmt
, false);
250 /* Perform reachability analysis and reclaim all unreachable nodes.
252 The algorithm is basically mark&sweep but with some extra refinements:
254 - reachable extern inline functions needs special handling; the bodies needs
255 to stay in memory until inlining in hope that they will be inlined.
256 After inlining we release their bodies and turn them into unanalyzed
257 nodes even when they are reachable.
259 - virtual functions are kept in callgraph even if they seem unreachable in
260 hope calls to them will be devirtualized.
262 Again we remove them after inlining. In late optimization some
263 devirtualization may happen, but it is not important since we won't inline
264 the call. In theory early opts and IPA should work out all important cases.
266 - virtual clones needs bodies of their origins for later materialization;
267 this means that we want to keep the body even if the origin is unreachable
268 otherwise. To avoid origin from sitting in the callgraph and being
269 walked by IPA passes, we turn them into unanalyzed nodes with body
272 We maintain set of function declaration where body needs to stay in
273 body_needed_for_clonning
275 Inline clones represent special case: their declaration match the
276 declaration of origin and cgraph_remove_node already knows how to
277 reshape callgraph and preserve body when offline copy of function or
278 inline clone is being removed.
280 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
281 variables with DECL_INITIAL set. We finalize these and keep reachable
282 ones around for constant folding purposes. After inlining we however
283 stop walking their references to let everything static referneced by them
284 to be removed when it is otherwise unreachable.
286 We maintain queue of both reachable symbols (i.e. defined symbols that needs
287 to stay) and symbols that are in boundary (i.e. external symbols referenced
288 by reachable symbols or origins of clones). The queue is represented
289 as linked list by AUX pointer terminated by 1.
291 At the end we keep all reachable symbols. For symbols in boundary we always
292 turn definition into a declaration, but we may keep function body around
293 based on body_needed_for_clonning
295 All symbols that enter the queue have AUX pointer non-zero and are in the
296 boundary. Pointer set REACHABLE is used to track reachable symbols.
298 Every symbol can be visited twice - once as part of boundary and once
299 as real reachable symbol. enqueue_node needs to decide whether the
300 node needs to be re-queued for second processing. For this purpose
301 we set AUX pointer of processed symbols in the boundary to constant 2. */
304 symbol_table::remove_unreachable_nodes (FILE *file
)
306 symtab_node
*first
= (symtab_node
*) (void *) 1;
307 struct cgraph_node
*node
, *next
;
308 varpool_node
*vnode
, *vnext
;
309 bool changed
= false;
310 hash_set
<symtab_node
*> reachable
;
311 hash_set
<tree
> body_needed_for_clonning
;
312 hash_set
<void *> reachable_call_targets
;
313 bool before_inlining_p
= symtab
->state
< (!optimize
? IPA_SSA
314 : IPA_SSA_AFTER_INLINING
);
316 timevar_push (TV_IPA_UNREACHABLE
);
317 build_type_inheritance_graph ();
319 fprintf (file
, "\nReclaiming functions:");
322 FOR_EACH_FUNCTION (node
)
323 gcc_assert (!node
->aux
);
324 FOR_EACH_VARIABLE (vnode
)
325 gcc_assert (!vnode
->aux
);
327 /* Mark functions whose bodies are obviously needed.
328 This is mostly when they can be referenced externally. Inline clones
329 are special since their declarations are shared with master clone and thus
330 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
331 FOR_EACH_FUNCTION (node
)
333 node
->used_as_abstract_origin
= false;
335 && !node
->global
.inlined_to
336 && !node
->in_other_partition
337 && !node
->can_remove_if_no_direct_calls_and_refs_p ())
339 gcc_assert (!node
->global
.inlined_to
);
340 reachable
.add (node
);
341 enqueue_node (node
, &first
, &reachable
);
344 gcc_assert (!node
->aux
);
347 /* Mark variables that are obviously needed. */
348 FOR_EACH_DEFINED_VARIABLE (vnode
)
349 if (!vnode
->can_remove_if_no_refs_p()
350 && !vnode
->in_other_partition
)
352 reachable
.add (vnode
);
353 enqueue_node (vnode
, &first
, &reachable
);
356 /* Perform reachability analysis. */
357 while (first
!= (symtab_node
*) (void *) 1)
359 bool in_boundary_p
= !reachable
.contains (first
);
360 symtab_node
*node
= first
;
362 first
= (symtab_node
*)first
->aux
;
364 /* If we are processing symbol in boundary, mark its AUX pointer for
365 possible later re-processing in enqueue_node. */
368 node
->aux
= (void *)2;
369 if (node
->alias
&& node
->analyzed
)
370 enqueue_node (node
->get_alias_target (), &first
, &reachable
);
374 if (TREE_CODE (node
->decl
) == FUNCTION_DECL
375 && DECL_ABSTRACT_ORIGIN (node
->decl
))
377 struct cgraph_node
*origin_node
378 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node
->decl
));
379 if (origin_node
&& !origin_node
->used_as_abstract_origin
)
381 origin_node
->used_as_abstract_origin
= true;
382 gcc_assert (!origin_node
->prev_sibling_clone
);
383 gcc_assert (!origin_node
->next_sibling_clone
);
384 for (cgraph_node
*n
= origin_node
->clones
; n
;
385 n
= n
->next_sibling_clone
)
386 if (n
->decl
== DECL_ABSTRACT_ORIGIN (node
->decl
))
387 n
->used_as_abstract_origin
= true;
390 /* If any symbol in a comdat group is reachable, force
391 all externally visible symbols in the same comdat
392 group to be reachable as well. Comdat-local symbols
393 can be discarded if all uses were inlined. */
394 if (node
->same_comdat_group
)
397 for (next
= node
->same_comdat_group
;
399 next
= next
->same_comdat_group
)
400 if (!next
->comdat_local_p ()
401 && !reachable
.add (next
))
402 enqueue_node (next
, &first
, &reachable
);
404 /* Mark references as reachable. */
405 process_references (node
, &first
, before_inlining_p
, &reachable
);
408 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
410 /* Mark the callees reachable unless they are direct calls to extern
411 inline functions we decided to not inline. */
414 struct cgraph_edge
*e
;
415 /* Keep alive possible targets for devirtualization. */
416 if (opt_for_fn (cnode
->decl
, optimize
)
417 && opt_for_fn (cnode
->decl
, flag_devirtualize
))
419 struct cgraph_edge
*next
;
420 for (e
= cnode
->indirect_calls
; e
; e
= next
)
422 next
= e
->next_callee
;
423 if (e
->indirect_info
->polymorphic
)
424 walk_polymorphic_call_targets (&reachable_call_targets
,
425 e
, &first
, &reachable
,
429 for (e
= cnode
->callees
; e
; e
= e
->next_callee
)
431 symtab_node
*body
= e
->callee
->function_symbol ();
432 if (e
->callee
->definition
433 && !e
->callee
->in_other_partition
434 && (!e
->inline_failed
435 || !DECL_EXTERNAL (e
->callee
->decl
)
437 || (before_inlining_p
438 && (opt_for_fn (body
->decl
, optimize
)
439 || (symtab
->state
< IPA_SSA
442 DECL_ATTRIBUTES (body
->decl
)))))))
444 /* Be sure that we will not optimize out alias target
446 if (DECL_EXTERNAL (e
->callee
->decl
)
448 && before_inlining_p
)
449 reachable
.add (body
);
450 reachable
.add (e
->callee
);
452 enqueue_node (e
->callee
, &first
, &reachable
);
455 /* When inline clone exists, mark body to be preserved so when removing
456 offline copy of the function we don't kill it. */
457 if (cnode
->global
.inlined_to
)
458 body_needed_for_clonning
.add (cnode
->decl
);
460 /* For instrumentation clones we always need original
461 function node for proper LTO privatization. */
462 if (cnode
->instrumentation_clone
463 && cnode
->definition
)
465 gcc_assert (cnode
->instrumented_version
|| in_lto_p
);
466 if (cnode
->instrumented_version
)
468 enqueue_node (cnode
->instrumented_version
, &first
,
470 reachable
.add (cnode
->instrumented_version
);
474 /* For non-inline clones, force their origins to the boundary and ensure
475 that body is not removed. */
476 while (cnode
->clone_of
)
478 bool noninline
= cnode
->clone_of
->decl
!= cnode
->decl
;
479 cnode
= cnode
->clone_of
;
482 body_needed_for_clonning
.add (cnode
->decl
);
483 enqueue_node (cnode
, &first
, &reachable
);
488 else if (cnode
->thunk
.thunk_p
)
489 enqueue_node (cnode
->callees
->callee
, &first
, &reachable
);
491 /* If any reachable function has simd clones, mark them as
492 reachable as well. */
493 if (cnode
->simd_clones
)
496 for (next
= cnode
->simd_clones
;
498 next
= next
->simdclone
->next_clone
)
500 || !reachable
.add (next
))
501 enqueue_node (next
, &first
, &reachable
);
504 /* When we see constructor of external variable, keep referred nodes in the
505 boundary. This will also hold initializers of the external vars NODE
507 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
);
509 && DECL_EXTERNAL (node
->decl
)
513 struct ipa_ref
*ref
= NULL
;
514 for (int i
= 0; node
->iterate_reference (i
, ref
); i
++)
515 enqueue_node (ref
->referred
, &first
, &reachable
);
519 /* Remove unreachable functions. */
520 for (node
= first_function (); node
; node
= next
)
522 next
= next_function (node
);
524 /* If node is not needed at all, remove it. */
528 fprintf (file
, " %s/%i", node
->name (), node
->order
);
532 /* If node is unreachable, remove its body. */
533 else if (!reachable
.contains (node
))
535 /* We keep definitions of thunks and aliases in the boundary so
536 we can walk to the ultimate alias targets and function symbols
538 if (node
->alias
|| node
->thunk
.thunk_p
)
540 else if (!body_needed_for_clonning
.contains (node
->decl
)
541 && !node
->alias
&& !node
->thunk
.thunk_p
)
542 node
->release_body ();
543 else if (!node
->clone_of
)
544 gcc_assert (in_lto_p
|| DECL_RESULT (node
->decl
));
545 if (node
->definition
&& !node
->alias
&& !node
->thunk
.thunk_p
)
548 fprintf (file
, " %s/%i", node
->name (), node
->order
);
549 node
->body_removed
= true;
550 node
->analyzed
= false;
551 node
->definition
= false;
552 node
->cpp_implicit_alias
= false;
554 node
->thunk
.thunk_p
= false;
555 node
->weakref
= false;
556 /* After early inlining we drop always_inline attributes on
557 bodies of functions that are still referenced (have their
559 DECL_ATTRIBUTES (node
->decl
)
560 = remove_attribute ("always_inline",
561 DECL_ATTRIBUTES (node
->decl
));
562 if (!node
->in_other_partition
)
563 node
->local
.local
= false;
564 node
->remove_callees ();
565 node
->remove_all_references ();
567 if (node
->thunk
.thunk_p
568 && node
->thunk
.add_pointer_bounds_args
)
570 node
->thunk
.thunk_p
= false;
571 node
->thunk
.add_pointer_bounds_args
= false;
576 gcc_assert (node
->clone_of
|| !node
->has_gimple_body_p ()
577 || in_lto_p
|| DECL_RESULT (node
->decl
));
580 /* Inline clones might be kept around so their materializing allows further
581 cloning. If the function the clone is inlined into is removed, we need
582 to turn it into normal cone. */
583 FOR_EACH_FUNCTION (node
)
585 if (node
->global
.inlined_to
588 gcc_assert (node
->clones
);
589 node
->global
.inlined_to
= NULL
;
590 update_inlined_to_pointer (node
, node
);
595 /* Remove unreachable variables. */
597 fprintf (file
, "\nReclaiming variables:");
598 for (vnode
= first_variable (); vnode
; vnode
= vnext
)
600 vnext
= next_variable (vnode
);
602 /* For can_refer_decl_in_current_unit_p we want to track for
603 all external variables if they are defined in other partition
605 && (!flag_ltrans
|| !DECL_EXTERNAL (vnode
->decl
)))
607 struct ipa_ref
*ref
= NULL
;
609 /* First remove the aliases, so varpool::remove can possibly lookup
610 the constructor and save it for future use. */
611 while (vnode
->iterate_direct_aliases (0, ref
))
614 fprintf (file
, " %s/%i", ref
->referred
->name (),
615 ref
->referred
->order
);
616 ref
->referring
->remove ();
619 fprintf (file
, " %s/%i", vnode
->name (), vnode
->order
);
620 vnext
= next_variable (vnode
);
624 else if (!reachable
.contains (vnode
) && !vnode
->alias
)
627 if (vnode
->definition
)
630 fprintf (file
, " %s", vnode
->name ());
633 /* Keep body if it may be useful for constant folding. */
634 if ((init
= ctor_for_folding (vnode
->decl
)) == error_mark_node
635 && !POINTER_BOUNDS_P (vnode
->decl
))
636 vnode
->remove_initializer ();
638 DECL_INITIAL (vnode
->decl
) = init
;
639 vnode
->body_removed
= true;
640 vnode
->definition
= false;
641 vnode
->analyzed
= false;
644 vnode
->remove_from_same_comdat_group ();
646 vnode
->remove_all_references ();
652 /* Now update address_taken flags and try to promote functions to be local. */
654 fprintf (file
, "\nClearing address taken flags:");
655 FOR_EACH_DEFINED_FUNCTION (node
)
656 if (node
->address_taken
657 && !node
->used_from_other_partition
)
659 if (!node
->call_for_symbol_and_aliases
660 (has_addr_references_p
, NULL
, true)
661 && (!node
->instrumentation_clone
662 || !node
->instrumented_version
663 || !node
->instrumented_version
->address_taken
))
666 fprintf (file
, " %s", node
->name ());
667 node
->address_taken
= false;
669 if (node
->local_p ())
671 node
->local
.local
= true;
673 fprintf (file
, " (local)");
678 fprintf (file
, "\n");
680 symtab_node::checking_verify_symtab_nodes ();
682 /* If we removed something, perhaps profile could be improved. */
683 if (changed
&& optimize
&& inline_edge_summary_vec
.exists ())
684 FOR_EACH_DEFINED_FUNCTION (node
)
685 ipa_propagate_frequency (node
);
687 timevar_pop (TV_IPA_UNREACHABLE
);
691 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
692 as needed, also clear EXPLICIT_REFS if the references to given variable
693 do not need to be explicit. */
696 process_references (varpool_node
*vnode
,
697 bool *written
, bool *address_taken
,
698 bool *read
, bool *explicit_refs
)
703 if (!vnode
->all_refs_explicit_p ()
704 || TREE_THIS_VOLATILE (vnode
->decl
))
705 *explicit_refs
= false;
707 for (i
= 0; vnode
->iterate_referring (i
, ref
)
708 && *explicit_refs
&& (!*written
|| !*address_taken
|| !*read
); i
++)
712 *address_taken
= true;
721 process_references (dyn_cast
<varpool_node
*> (ref
->referring
), written
,
722 address_taken
, read
, explicit_refs
);
729 /* Set TREE_READONLY bit. */
732 set_readonly_bit (varpool_node
*vnode
, void *data ATTRIBUTE_UNUSED
)
734 TREE_READONLY (vnode
->decl
) = true;
738 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
741 set_writeonly_bit (varpool_node
*vnode
, void *data
)
743 vnode
->writeonly
= true;
746 DECL_INITIAL (vnode
->decl
) = NULL
;
749 if (vnode
->num_references ())
750 *(bool *)data
= true;
751 vnode
->remove_all_references ();
757 /* Clear addressale bit of VNODE. */
760 clear_addressable_bit (varpool_node
*vnode
, void *data ATTRIBUTE_UNUSED
)
762 vnode
->address_taken
= false;
763 TREE_ADDRESSABLE (vnode
->decl
) = 0;
767 /* Discover variables that have no longer address taken or that are read only
768 and update their flags.
770 Return true when unreachable symbol removan should be done.
772 FIXME: This can not be done in between gimplify and omp_expand since
773 readonly flag plays role on what is shared and what is not. Currently we do
774 this transformation as part of whole program visibility and re-do at
775 ipa-reference pass (to take into account clonning), but it would
776 make sense to do it before early optimizations. */
779 ipa_discover_readonly_nonaddressable_vars (void)
781 bool remove_p
= false;
784 fprintf (dump_file
, "Clearing variable flags:");
785 FOR_EACH_VARIABLE (vnode
)
787 && (TREE_ADDRESSABLE (vnode
->decl
)
789 || !TREE_READONLY (vnode
->decl
)))
791 bool written
= false;
792 bool address_taken
= false;
794 bool explicit_refs
= true;
796 process_references (vnode
, &written
, &address_taken
, &read
,
802 if (TREE_ADDRESSABLE (vnode
->decl
) && dump_file
)
803 fprintf (dump_file
, " %s (non-addressable)", vnode
->name ());
804 vnode
->call_for_symbol_and_aliases (clear_addressable_bit
, NULL
,
807 if (!address_taken
&& !written
808 /* Making variable in explicit section readonly can cause section
810 See e.g. gcc.c-torture/compile/pr23237.c */
811 && vnode
->get_section () == NULL
)
813 if (!TREE_READONLY (vnode
->decl
) && dump_file
)
814 fprintf (dump_file
, " %s (read-only)", vnode
->name ());
815 vnode
->call_for_symbol_and_aliases (set_readonly_bit
, NULL
, true);
817 if (!vnode
->writeonly
&& !read
&& !address_taken
&& written
)
820 fprintf (dump_file
, " %s (write-only)", vnode
->name ());
821 vnode
->call_for_symbol_and_aliases (set_writeonly_bit
, &remove_p
,
826 fprintf (dump_file
, "\n");
830 /* Free inline summary. */
834 const pass_data pass_data_ipa_free_inline_summary
=
836 SIMPLE_IPA_PASS
, /* type */
837 "free-inline-summary", /* name */
838 OPTGROUP_NONE
, /* optinfo_flags */
839 TV_IPA_FREE_INLINE_SUMMARY
, /* tv_id */
840 0, /* properties_required */
841 0, /* properties_provided */
842 0, /* properties_destroyed */
843 0, /* todo_flags_start */
844 /* Early optimizations may make function unreachable. We can not
845 remove unreachable functions as part of the ealry opts pass because
846 TODOs are run before subpasses. Do it here. */
847 ( TODO_remove_functions
| TODO_dump_symtab
), /* todo_flags_finish */
850 class pass_ipa_free_inline_summary
: public simple_ipa_opt_pass
853 pass_ipa_free_inline_summary (gcc::context
*ctxt
)
854 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary
, ctxt
)
857 /* opt_pass methods: */
858 virtual unsigned int execute (function
*)
860 inline_free_summary ();
864 }; // class pass_ipa_free_inline_summary
868 simple_ipa_opt_pass
*
869 make_pass_ipa_free_inline_summary (gcc::context
*ctxt
)
871 return new pass_ipa_free_inline_summary (ctxt
);
874 /* Generate and emit a static constructor or destructor. WHICH must
875 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
876 (for chp static vars constructor) or 'B' (for chkp static bounds
877 constructor). BODY is a STATEMENT_LIST containing GENERIC
878 statements. PRIORITY is the initialization priority for this
879 constructor or destructor.
881 FINAL specify whether the externally visible name for collect2 should
885 cgraph_build_static_cdtor_1 (char which
, tree body
, int priority
, bool final
)
887 static int counter
= 0;
889 tree decl
, name
, resdecl
;
891 /* The priority is encoded in the constructor or destructor name.
892 collect2 will sort the names and arrange that they are called at
895 sprintf (which_buf
, "%c_%.5d_%d", which
, priority
, counter
++);
897 /* Proudce sane name but one not recognizable by collect2, just for the
898 case we fail to inline the function. */
899 sprintf (which_buf
, "sub_%c_%.5d_%d", which
, priority
, counter
++);
900 name
= get_file_function_name (which_buf
);
902 decl
= build_decl (input_location
, FUNCTION_DECL
, name
,
903 build_function_type_list (void_type_node
, NULL_TREE
));
904 current_function_decl
= decl
;
906 resdecl
= build_decl (input_location
,
907 RESULT_DECL
, NULL_TREE
, void_type_node
);
908 DECL_ARTIFICIAL (resdecl
) = 1;
909 DECL_RESULT (decl
) = resdecl
;
910 DECL_CONTEXT (resdecl
) = decl
;
912 allocate_struct_function (decl
, false);
914 TREE_STATIC (decl
) = 1;
915 TREE_USED (decl
) = 1;
916 DECL_ARTIFICIAL (decl
) = 1;
917 DECL_IGNORED_P (decl
) = 1;
918 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
919 DECL_SAVED_TREE (decl
) = body
;
920 if (!targetm
.have_ctors_dtors
&& final
)
922 TREE_PUBLIC (decl
) = 1;
923 DECL_PRESERVE_P (decl
) = 1;
925 DECL_UNINLINABLE (decl
) = 1;
927 DECL_INITIAL (decl
) = make_node (BLOCK
);
928 TREE_USED (DECL_INITIAL (decl
)) = 1;
930 DECL_SOURCE_LOCATION (decl
) = input_location
;
931 cfun
->function_end_locus
= input_location
;
936 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
937 decl_init_priority_insert (decl
, priority
);
940 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
941 DECL_ATTRIBUTES (decl
) = tree_cons (get_identifier ("chkp ctor"),
944 decl_init_priority_insert (decl
, priority
);
947 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
948 DECL_ATTRIBUTES (decl
) = tree_cons (get_identifier ("bnd_legacy"),
951 decl_init_priority_insert (decl
, priority
);
954 DECL_STATIC_DESTRUCTOR (decl
) = 1;
955 decl_fini_priority_insert (decl
, priority
);
961 gimplify_function_tree (decl
);
963 cgraph_node::add_new_function (decl
, false);
966 current_function_decl
= NULL
;
969 /* Generate and emit a static constructor or destructor. WHICH must
970 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
971 (for chkp static vars constructor) or 'B' (for chkp static bounds
972 constructor). BODY is a STATEMENT_LIST containing GENERIC
973 statements. PRIORITY is the initialization priority for this
974 constructor or destructor. */
977 cgraph_build_static_cdtor (char which
, tree body
, int priority
)
979 cgraph_build_static_cdtor_1 (which
, body
, priority
, false);
982 /* A vector of FUNCTION_DECLs declared as static constructors. */
983 static vec
<tree
> static_ctors
;
984 /* A vector of FUNCTION_DECLs declared as static destructors. */
985 static vec
<tree
> static_dtors
;
987 /* When target does not have ctors and dtors, we call all constructor
988 and destructor by special initialization/destruction function
989 recognized by collect2.
991 When we are going to build this function, collect all constructors and
992 destructors and turn them into normal functions. */
995 record_cdtor_fn (struct cgraph_node
*node
)
997 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
998 static_ctors
.safe_push (node
->decl
);
999 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
1000 static_dtors
.safe_push (node
->decl
);
1001 node
= cgraph_node::get (node
->decl
);
1002 DECL_DISREGARD_INLINE_LIMITS (node
->decl
) = 1;
1005 /* Define global constructors/destructor functions for the CDTORS, of
1006 which they are LEN. The CDTORS are sorted by initialization
1007 priority. If CTOR_P is true, these are constructors; otherwise,
1008 they are destructors. */
1011 build_cdtor (bool ctor_p
, vec
<tree
> cdtors
)
1014 size_t len
= cdtors
.length ();
1021 priority_type priority
;
1030 p
= ctor_p
? DECL_INIT_PRIORITY (fn
) : DECL_FINI_PRIORITY (fn
);
1033 else if (p
!= priority
)
1039 /* When there is only one cdtor and target supports them, do nothing. */
1041 && targetm
.have_ctors_dtors
)
1046 /* Find the next batch of constructors/destructors with the same
1047 initialization priority. */
1052 call
= build_call_expr (fn
, 0);
1054 DECL_STATIC_CONSTRUCTOR (fn
) = 0;
1056 DECL_STATIC_DESTRUCTOR (fn
) = 0;
1057 /* We do not want to optimize away pure/const calls here.
1058 When optimizing, these should be already removed, when not
1059 optimizing, we want user to be able to breakpoint in them. */
1060 TREE_SIDE_EFFECTS (call
) = 1;
1061 append_to_statement_list (call
, &body
);
1063 gcc_assert (body
!= NULL_TREE
);
1064 /* Generate a function to call all the function of like
1066 cgraph_build_static_cdtor_1 (ctor_p
? 'I' : 'D', body
, priority
, true);
1070 /* Comparison function for qsort. P1 and P2 are actually of type
1071 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1072 used to determine the sort order. */
1075 compare_ctor (const void *p1
, const void *p2
)
1082 f1
= *(const tree
*)p1
;
1083 f2
= *(const tree
*)p2
;
1084 priority1
= DECL_INIT_PRIORITY (f1
);
1085 priority2
= DECL_INIT_PRIORITY (f2
);
1087 if (priority1
< priority2
)
1089 else if (priority1
> priority2
)
1092 /* Ensure a stable sort. Constructors are executed in backwarding
1093 order to make LTO initialize braries first. */
1094 return DECL_UID (f2
) - DECL_UID (f1
);
1097 /* Comparison function for qsort. P1 and P2 are actually of type
1098 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1099 used to determine the sort order. */
1102 compare_dtor (const void *p1
, const void *p2
)
1109 f1
= *(const tree
*)p1
;
1110 f2
= *(const tree
*)p2
;
1111 priority1
= DECL_FINI_PRIORITY (f1
);
1112 priority2
= DECL_FINI_PRIORITY (f2
);
1114 if (priority1
< priority2
)
1116 else if (priority1
> priority2
)
1119 /* Ensure a stable sort. */
1120 return DECL_UID (f1
) - DECL_UID (f2
);
1123 /* Generate functions to call static constructors and destructors
1124 for targets that do not support .ctors/.dtors sections. These
1125 functions have magic names which are detected by collect2. */
1128 build_cdtor_fns (void)
1130 if (!static_ctors
.is_empty ())
1132 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1133 static_ctors
.qsort (compare_ctor
);
1134 build_cdtor (/*ctor_p=*/true, static_ctors
);
1137 if (!static_dtors
.is_empty ())
1139 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1140 static_dtors
.qsort (compare_dtor
);
1141 build_cdtor (/*ctor_p=*/false, static_dtors
);
1145 /* Look for constructors and destructors and produce function calling them.
1146 This is needed for targets not supporting ctors or dtors, but we perform the
1147 transformation also at linktime to merge possibly numerous
1148 constructors/destructors into single function to improve code locality and
1152 ipa_cdtor_merge (void)
1154 struct cgraph_node
*node
;
1155 FOR_EACH_DEFINED_FUNCTION (node
)
1156 if (DECL_STATIC_CONSTRUCTOR (node
->decl
)
1157 || DECL_STATIC_DESTRUCTOR (node
->decl
))
1158 record_cdtor_fn (node
);
1160 static_ctors
.release ();
1161 static_dtors
.release ();
1167 const pass_data pass_data_ipa_cdtor_merge
=
1169 IPA_PASS
, /* type */
1171 OPTGROUP_NONE
, /* optinfo_flags */
1172 TV_CGRAPHOPT
, /* tv_id */
1173 0, /* properties_required */
1174 0, /* properties_provided */
1175 0, /* properties_destroyed */
1176 0, /* todo_flags_start */
1177 0, /* todo_flags_finish */
1180 class pass_ipa_cdtor_merge
: public ipa_opt_pass_d
1183 pass_ipa_cdtor_merge (gcc::context
*ctxt
)
1184 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge
, ctxt
,
1185 NULL
, /* generate_summary */
1186 NULL
, /* write_summary */
1187 NULL
, /* read_summary */
1188 NULL
, /* write_optimization_summary */
1189 NULL
, /* read_optimization_summary */
1190 NULL
, /* stmt_fixup */
1191 0, /* function_transform_todo_flags_start */
1192 NULL
, /* function_transform */
1193 NULL
) /* variable_transform */
1196 /* opt_pass methods: */
1197 virtual bool gate (function
*);
1198 virtual unsigned int execute (function
*) { return ipa_cdtor_merge (); }
1200 }; // class pass_ipa_cdtor_merge
1203 pass_ipa_cdtor_merge::gate (function
*)
1205 /* Perform the pass when we have no ctors/dtors support
1206 or at LTO time to merge multiple constructors into single
1208 return !targetm
.have_ctors_dtors
|| (optimize
&& in_lto_p
);
1214 make_pass_ipa_cdtor_merge (gcc::context
*ctxt
)
1216 return new pass_ipa_cdtor_merge (ctxt
);
1219 /* Invalid pointer representing BOTTOM for single user dataflow. */
1220 #define BOTTOM ((cgraph_node *)(size_t) 2)
1222 /* Meet operation for single user dataflow.
1223 Here we want to associate variables with sigle function that may access it.
1225 FUNCTION is current single user of a variable, VAR is variable that uses it.
1226 Latttice is stored in SINGLE_USER_MAP.
1229 - TOP by no entry in SIGNLE_USER_MAP
1230 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1231 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1234 meet (cgraph_node
*function
, varpool_node
*var
,
1235 hash_map
<varpool_node
*, cgraph_node
*> &single_user_map
)
1237 struct cgraph_node
*user
, **f
;
1239 if (var
->aux
== BOTTOM
)
1242 f
= single_user_map
.get (var
);
1248 else if (function
!= user
)
1254 /* Propagation step of single-use dataflow.
1256 Check all uses of VNODE and see if they are used by single function FUNCTION.
1257 SINGLE_USER_MAP represents the dataflow lattice. */
1260 propagate_single_user (varpool_node
*vnode
, cgraph_node
*function
,
1261 hash_map
<varpool_node
*, cgraph_node
*> &single_user_map
)
1264 struct ipa_ref
*ref
;
1266 gcc_assert (!vnode
->externally_visible
);
1268 /* If node is an alias, first meet with its target. */
1270 function
= meet (function
, vnode
->get_alias_target (), single_user_map
);
1272 /* Check all users and see if they correspond to a single function. */
1273 for (i
= 0; vnode
->iterate_referring (i
, ref
) && function
!= BOTTOM
; i
++)
1275 struct cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (ref
->referring
);
1278 if (cnode
->global
.inlined_to
)
1279 cnode
= cnode
->global
.inlined_to
;
1282 else if (function
!= cnode
)
1286 function
= meet (function
, dyn_cast
<varpool_node
*> (ref
->referring
),
1292 /* Pass setting used_by_single_function flag.
1293 This flag is set on variable when there is only one function that may
1294 possibly referr to it. */
1297 ipa_single_use (void)
1299 varpool_node
*first
= (varpool_node
*) (void *) 1;
1301 hash_map
<varpool_node
*, cgraph_node
*> single_user_map
;
1303 FOR_EACH_DEFINED_VARIABLE (var
)
1304 if (!var
->all_refs_explicit_p ())
1308 /* Enqueue symbol for dataflow. */
1313 /* The actual dataflow. */
1315 while (first
!= (void *) 1)
1317 cgraph_node
*user
, *orig_user
, **f
;
1320 first
= (varpool_node
*)first
->aux
;
1322 f
= single_user_map
.get (var
);
1327 user
= propagate_single_user (var
, orig_user
, single_user_map
);
1329 gcc_checking_assert (var
->aux
!= BOTTOM
);
1331 /* If user differs, enqueue all references. */
1332 if (user
!= orig_user
)
1337 single_user_map
.put (var
, user
);
1339 /* Enqueue all aliases for re-processing. */
1340 for (i
= 0; var
->iterate_direct_aliases (i
, ref
); i
++)
1341 if (!ref
->referring
->aux
)
1343 ref
->referring
->aux
= first
;
1344 first
= dyn_cast
<varpool_node
*> (ref
->referring
);
1346 /* Enqueue all users for re-processing. */
1347 for (i
= 0; var
->iterate_reference (i
, ref
); i
++)
1348 if (!ref
->referred
->aux
1349 && ref
->referred
->definition
1350 && is_a
<varpool_node
*> (ref
->referred
))
1352 ref
->referred
->aux
= first
;
1353 first
= dyn_cast
<varpool_node
*> (ref
->referred
);
1356 /* If user is BOTTOM, just punt on this var. */
1366 FOR_EACH_DEFINED_VARIABLE (var
)
1368 if (var
->aux
!= BOTTOM
)
1370 /* Not having the single user known means that the VAR is
1371 unreachable. Either someone forgot to remove unreachable
1372 variables or the reachability here is wrong. */
1374 gcc_checking_assert (single_user_map
.get (var
));
1378 fprintf (dump_file
, "Variable %s/%i is used by single function\n",
1379 var
->name (), var
->order
);
1381 var
->used_by_single_function
= true;
1390 const pass_data pass_data_ipa_single_use
=
1392 IPA_PASS
, /* type */
1393 "single-use", /* name */
1394 OPTGROUP_NONE
, /* optinfo_flags */
1395 TV_CGRAPHOPT
, /* tv_id */
1396 0, /* properties_required */
1397 0, /* properties_provided */
1398 0, /* properties_destroyed */
1399 0, /* todo_flags_start */
1400 0, /* todo_flags_finish */
1403 class pass_ipa_single_use
: public ipa_opt_pass_d
1406 pass_ipa_single_use (gcc::context
*ctxt
)
1407 : ipa_opt_pass_d (pass_data_ipa_single_use
, ctxt
,
1408 NULL
, /* generate_summary */
1409 NULL
, /* write_summary */
1410 NULL
, /* read_summary */
1411 NULL
, /* write_optimization_summary */
1412 NULL
, /* read_optimization_summary */
1413 NULL
, /* stmt_fixup */
1414 0, /* function_transform_todo_flags_start */
1415 NULL
, /* function_transform */
1416 NULL
) /* variable_transform */
1419 /* opt_pass methods: */
1420 virtual bool gate (function
*);
1421 virtual unsigned int execute (function
*) { return ipa_single_use (); }
1423 }; // class pass_ipa_single_use
1426 pass_ipa_single_use::gate (function
*)
1434 make_pass_ipa_single_use (gcc::context
*ctxt
)
1436 return new pass_ipa_single_use (ctxt
);