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"
28 #include "fold-const.h"
30 #include "stringpool.h"
32 #include "basic-block.h"
33 #include "plugin-api.h"
34 #include "hard-reg-set.h"
38 #include "tree-pass.h"
39 #include "gimple-expr.h"
43 #include "tree-iterator.h"
44 #include "ipa-utils.h"
45 #include "alloc-pool.h"
46 #include "symbol-summary.h"
48 #include "ipa-inline.h"
49 #include "tree-inline.h"
52 #include "internal-fn.h"
53 #include "tree-ssa-alias.h"
58 /* Return true when NODE has ADDR reference. */
61 has_addr_references_p (struct cgraph_node
*node
,
62 void *data ATTRIBUTE_UNUSED
)
65 struct ipa_ref
*ref
= NULL
;
67 for (i
= 0; node
->iterate_referring (i
, ref
); i
++)
68 if (ref
->use
== IPA_REF_ADDR
)
73 /* Look for all functions inlined to NODE and update their inlined_to pointers
77 update_inlined_to_pointer (struct cgraph_node
*node
, struct cgraph_node
*inlined_to
)
79 struct cgraph_edge
*e
;
80 for (e
= node
->callees
; e
; e
= e
->next_callee
)
81 if (e
->callee
->global
.inlined_to
)
83 e
->callee
->global
.inlined_to
= inlined_to
;
84 update_inlined_to_pointer (e
->callee
, inlined_to
);
88 /* Add symtab NODE to queue starting at FIRST.
90 The queue is linked via AUX pointers and terminated by pointer to 1.
91 We enqueue nodes at two occasions: when we find them reachable or when we find
92 their bodies needed for further clonning. In the second case we mark them
93 by pointer to 2 after processing so they are re-queue when they become
97 enqueue_node (symtab_node
*node
, symtab_node
**first
,
98 hash_set
<symtab_node
*> *reachable
)
100 /* Node is still in queue; do nothing. */
101 if (node
->aux
&& node
->aux
!= (void *) 2)
103 /* Node was already processed as unreachable, re-enqueue
104 only if it became reachable now. */
105 if (node
->aux
== (void *)2 && !reachable
->contains (node
))
111 /* Process references. */
114 process_references (symtab_node
*snode
,
116 bool before_inlining_p
,
117 hash_set
<symtab_node
*> *reachable
)
120 struct ipa_ref
*ref
= NULL
;
121 for (i
= 0; snode
->iterate_reference (i
, ref
); i
++)
123 symtab_node
*node
= ref
->referred
;
124 symtab_node
*body
= node
->ultimate_alias_target ();
126 if (node
->definition
&& !node
->in_other_partition
127 && ((!DECL_EXTERNAL (node
->decl
) || node
->alias
)
128 || (((before_inlining_p
129 && ((TREE_CODE (node
->decl
) != FUNCTION_DECL
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 (TYPE_METHOD_BASETYPE (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_summaries
)
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. */
377 node
->aux
= (void *)2;
378 if (node
->alias
&& node
->analyzed
)
379 enqueue_node (node
->get_alias_target (), &first
, &reachable
);
383 if (TREE_CODE (node
->decl
) == FUNCTION_DECL
384 && DECL_ABSTRACT_ORIGIN (node
->decl
))
386 struct cgraph_node
*origin_node
387 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node
->decl
));
388 if (origin_node
&& !origin_node
->used_as_abstract_origin
)
390 origin_node
->used_as_abstract_origin
= true;
391 gcc_assert (!origin_node
->prev_sibling_clone
);
392 gcc_assert (!origin_node
->next_sibling_clone
);
393 for (cgraph_node
*n
= origin_node
->clones
; n
;
394 n
= n
->next_sibling_clone
)
395 if (n
->decl
== DECL_ABSTRACT_ORIGIN (node
->decl
))
396 n
->used_as_abstract_origin
= true;
399 /* If any symbol in a comdat group is reachable, force
400 all externally visible symbols in the same comdat
401 group to be reachable as well. Comdat-local symbols
402 can be discarded if all uses were inlined. */
403 if (node
->same_comdat_group
)
406 for (next
= node
->same_comdat_group
;
408 next
= next
->same_comdat_group
)
409 if (!next
->comdat_local_p ()
410 && !reachable
.add (next
))
411 enqueue_node (next
, &first
, &reachable
);
413 /* Mark references as reachable. */
414 process_references (node
, &first
, before_inlining_p
, &reachable
);
417 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
419 /* Mark the callees reachable unless they are direct calls to extern
420 inline functions we decided to not inline. */
423 struct cgraph_edge
*e
;
424 /* Keep alive possible targets for devirtualization. */
425 if (opt_for_fn (cnode
->decl
, optimize
)
426 && opt_for_fn (cnode
->decl
, flag_devirtualize
))
428 struct cgraph_edge
*next
;
429 for (e
= cnode
->indirect_calls
; e
; e
= next
)
431 next
= e
->next_callee
;
432 if (e
->indirect_info
->polymorphic
)
433 walk_polymorphic_call_targets (&reachable_call_targets
,
434 e
, &first
, &reachable
,
438 for (e
= cnode
->callees
; e
; e
= e
->next_callee
)
440 symtab_node
*body
= e
->callee
->function_symbol ();
441 if (e
->callee
->definition
442 && !e
->callee
->in_other_partition
443 && (!e
->inline_failed
444 || !DECL_EXTERNAL (e
->callee
->decl
)
446 || (before_inlining_p
447 && (opt_for_fn (body
->decl
, optimize
)
448 || (symtab
->state
< IPA_SSA
451 DECL_ATTRIBUTES (body
->decl
)))))))
453 /* Be sure that we will not optimize out alias target
455 if (DECL_EXTERNAL (e
->callee
->decl
)
457 && before_inlining_p
)
458 reachable
.add (body
);
459 reachable
.add (e
->callee
);
461 enqueue_node (e
->callee
, &first
, &reachable
);
464 /* When inline clone exists, mark body to be preserved so when removing
465 offline copy of the function we don't kill it. */
466 if (cnode
->global
.inlined_to
)
467 body_needed_for_clonning
.add (cnode
->decl
);
469 /* For instrumentation clones we always need original
470 function node for proper LTO privatization. */
471 if (cnode
->instrumentation_clone
472 && cnode
->definition
)
474 gcc_assert (cnode
->instrumented_version
|| in_lto_p
);
475 if (cnode
->instrumented_version
)
477 enqueue_node (cnode
->instrumented_version
, &first
,
479 reachable
.add (cnode
->instrumented_version
);
483 /* For non-inline clones, force their origins to the boundary and ensure
484 that body is not removed. */
485 while (cnode
->clone_of
)
487 bool noninline
= cnode
->clone_of
->decl
!= cnode
->decl
;
488 cnode
= cnode
->clone_of
;
491 body_needed_for_clonning
.add (cnode
->decl
);
492 enqueue_node (cnode
, &first
, &reachable
);
497 else if (cnode
->thunk
.thunk_p
)
498 enqueue_node (cnode
->callees
->callee
, &first
, &reachable
);
500 /* If any reachable function has simd clones, mark them as
501 reachable as well. */
502 if (cnode
->simd_clones
)
505 for (next
= cnode
->simd_clones
;
507 next
= next
->simdclone
->next_clone
)
509 || !reachable
.add (next
))
510 enqueue_node (next
, &first
, &reachable
);
513 /* When we see constructor of external variable, keep referred nodes in the
514 boundary. This will also hold initializers of the external vars NODE
516 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
);
518 && DECL_EXTERNAL (node
->decl
)
522 struct ipa_ref
*ref
= NULL
;
523 for (int i
= 0; node
->iterate_reference (i
, ref
); i
++)
524 enqueue_node (ref
->referred
, &first
, &reachable
);
528 /* Remove unreachable functions. */
529 for (node
= first_function (); node
; node
= next
)
531 next
= next_function (node
);
533 /* If node is not needed at all, remove it. */
537 fprintf (file
, " %s/%i", node
->name (), node
->order
);
541 /* If node is unreachable, remove its body. */
542 else if (!reachable
.contains (node
))
544 /* We keep definitions of thunks and aliases in the boundary so
545 we can walk to the ultimate alias targets and function symbols
547 if (node
->alias
|| node
->thunk
.thunk_p
)
549 else if (!body_needed_for_clonning
.contains (node
->decl
)
550 && !node
->alias
&& !node
->thunk
.thunk_p
)
551 node
->release_body ();
552 else if (!node
->clone_of
)
553 gcc_assert (in_lto_p
|| DECL_RESULT (node
->decl
));
554 if (node
->definition
&& !node
->alias
&& !node
->thunk
.thunk_p
)
557 fprintf (file
, " %s/%i", node
->name (), node
->order
);
558 node
->body_removed
= true;
559 node
->analyzed
= false;
560 node
->definition
= false;
561 node
->cpp_implicit_alias
= false;
563 node
->thunk
.thunk_p
= false;
564 node
->weakref
= false;
565 /* After early inlining we drop always_inline attributes on
566 bodies of functions that are still referenced (have their
568 DECL_ATTRIBUTES (node
->decl
)
569 = remove_attribute ("always_inline",
570 DECL_ATTRIBUTES (node
->decl
));
571 if (!node
->in_other_partition
)
572 node
->local
.local
= false;
573 node
->remove_callees ();
574 node
->remove_all_references ();
576 if (node
->thunk
.thunk_p
577 && node
->thunk
.add_pointer_bounds_args
)
579 node
->thunk
.thunk_p
= false;
580 node
->thunk
.add_pointer_bounds_args
= false;
585 gcc_assert (node
->clone_of
|| !node
->has_gimple_body_p ()
586 || in_lto_p
|| DECL_RESULT (node
->decl
));
589 /* Inline clones might be kept around so their materializing allows further
590 cloning. If the function the clone is inlined into is removed, we need
591 to turn it into normal cone. */
592 FOR_EACH_FUNCTION (node
)
594 if (node
->global
.inlined_to
597 gcc_assert (node
->clones
);
598 node
->global
.inlined_to
= NULL
;
599 update_inlined_to_pointer (node
, node
);
604 /* Remove unreachable variables. */
606 fprintf (file
, "\nReclaiming variables:");
607 for (vnode
= first_variable (); vnode
; vnode
= vnext
)
609 vnext
= next_variable (vnode
);
611 /* For can_refer_decl_in_current_unit_p we want to track for
612 all external variables if they are defined in other partition
614 && (!flag_ltrans
|| !DECL_EXTERNAL (vnode
->decl
)))
616 struct ipa_ref
*ref
= NULL
;
618 /* First remove the aliases, so varpool::remove can possibly lookup
619 the constructor and save it for future use. */
620 while (vnode
->iterate_direct_aliases (0, ref
))
623 fprintf (file
, " %s/%i", ref
->referred
->name (),
624 ref
->referred
->order
);
625 ref
->referring
->remove ();
628 fprintf (file
, " %s/%i", vnode
->name (), vnode
->order
);
629 vnext
= next_variable (vnode
);
633 else if (!reachable
.contains (vnode
) && !vnode
->alias
)
636 if (vnode
->definition
)
639 fprintf (file
, " %s", vnode
->name ());
642 /* Keep body if it may be useful for constant folding. */
643 if ((init
= ctor_for_folding (vnode
->decl
)) == error_mark_node
644 && !POINTER_BOUNDS_P (vnode
->decl
))
645 vnode
->remove_initializer ();
647 DECL_INITIAL (vnode
->decl
) = init
;
648 vnode
->body_removed
= true;
649 vnode
->definition
= false;
650 vnode
->analyzed
= false;
653 vnode
->remove_from_same_comdat_group ();
655 vnode
->remove_all_references ();
661 /* Now update address_taken flags and try to promote functions to be local. */
663 fprintf (file
, "\nClearing address taken flags:");
664 FOR_EACH_DEFINED_FUNCTION (node
)
665 if (node
->address_taken
666 && !node
->used_from_other_partition
)
668 if (!node
->call_for_symbol_and_aliases
669 (has_addr_references_p
, NULL
, true)
670 && (!node
->instrumentation_clone
671 || !node
->instrumented_version
672 || !node
->instrumented_version
->address_taken
))
675 fprintf (file
, " %s", node
->name ());
676 node
->address_taken
= false;
678 if (node
->local_p ())
680 node
->local
.local
= true;
682 fprintf (file
, " (local)");
687 fprintf (file
, "\n");
689 #ifdef ENABLE_CHECKING
690 symtab_node::verify_symtab_nodes ();
693 /* If we removed something, perhaps profile could be improved. */
694 if (changed
&& optimize
&& inline_edge_summary_vec
.exists ())
695 FOR_EACH_DEFINED_FUNCTION (node
)
696 ipa_propagate_frequency (node
);
698 timevar_pop (TV_IPA_UNREACHABLE
);
702 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
703 as needed, also clear EXPLICIT_REFS if the references to given variable
704 do not need to be explicit. */
707 process_references (varpool_node
*vnode
,
708 bool *written
, bool *address_taken
,
709 bool *read
, bool *explicit_refs
)
714 if (!vnode
->all_refs_explicit_p ()
715 || TREE_THIS_VOLATILE (vnode
->decl
))
716 *explicit_refs
= false;
718 for (i
= 0; vnode
->iterate_referring (i
, ref
)
719 && *explicit_refs
&& (!*written
|| !*address_taken
|| !*read
); i
++)
723 *address_taken
= true;
732 process_references (dyn_cast
<varpool_node
*> (ref
->referring
), written
,
733 address_taken
, read
, explicit_refs
);
740 /* Set TREE_READONLY bit. */
743 set_readonly_bit (varpool_node
*vnode
, void *data ATTRIBUTE_UNUSED
)
745 TREE_READONLY (vnode
->decl
) = true;
749 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
752 set_writeonly_bit (varpool_node
*vnode
, void *data
)
754 vnode
->writeonly
= true;
757 DECL_INITIAL (vnode
->decl
) = NULL
;
760 if (vnode
->num_references ())
761 *(bool *)data
= true;
762 vnode
->remove_all_references ();
768 /* Clear addressale bit of VNODE. */
771 clear_addressable_bit (varpool_node
*vnode
, void *data ATTRIBUTE_UNUSED
)
773 vnode
->address_taken
= false;
774 TREE_ADDRESSABLE (vnode
->decl
) = 0;
778 /* Discover variables that have no longer address taken or that are read only
779 and update their flags.
781 Return true when unreachable symbol removan should be done.
783 FIXME: This can not be done in between gimplify and omp_expand since
784 readonly flag plays role on what is shared and what is not. Currently we do
785 this transformation as part of whole program visibility and re-do at
786 ipa-reference pass (to take into account clonning), but it would
787 make sense to do it before early optimizations. */
790 ipa_discover_readonly_nonaddressable_vars (void)
792 bool remove_p
= false;
795 fprintf (dump_file
, "Clearing variable flags:");
796 FOR_EACH_VARIABLE (vnode
)
798 && (TREE_ADDRESSABLE (vnode
->decl
)
800 || !TREE_READONLY (vnode
->decl
)))
802 bool written
= false;
803 bool address_taken
= false;
805 bool explicit_refs
= true;
807 process_references (vnode
, &written
, &address_taken
, &read
,
813 if (TREE_ADDRESSABLE (vnode
->decl
) && dump_file
)
814 fprintf (dump_file
, " %s (non-addressable)", vnode
->name ());
815 vnode
->call_for_symbol_and_aliases (clear_addressable_bit
, NULL
,
818 if (!address_taken
&& !written
819 /* Making variable in explicit section readonly can cause section
821 See e.g. gcc.c-torture/compile/pr23237.c */
822 && vnode
->get_section () == NULL
)
824 if (!TREE_READONLY (vnode
->decl
) && dump_file
)
825 fprintf (dump_file
, " %s (read-only)", vnode
->name ());
826 vnode
->call_for_symbol_and_aliases (set_readonly_bit
, NULL
, true);
828 if (!vnode
->writeonly
&& !read
&& !address_taken
&& written
)
831 fprintf (dump_file
, " %s (write-only)", vnode
->name ());
832 vnode
->call_for_symbol_and_aliases (set_writeonly_bit
, &remove_p
,
837 fprintf (dump_file
, "\n");
841 /* Free inline summary. */
845 const pass_data pass_data_ipa_free_inline_summary
=
847 SIMPLE_IPA_PASS
, /* type */
848 "free-inline-summary", /* name */
849 OPTGROUP_NONE
, /* optinfo_flags */
850 TV_IPA_FREE_INLINE_SUMMARY
, /* tv_id */
851 0, /* properties_required */
852 0, /* properties_provided */
853 0, /* properties_destroyed */
854 0, /* todo_flags_start */
855 /* Early optimizations may make function unreachable. We can not
856 remove unreachable functions as part of the ealry opts pass because
857 TODOs are run before subpasses. Do it here. */
858 ( TODO_remove_functions
| TODO_dump_symtab
), /* todo_flags_finish */
861 class pass_ipa_free_inline_summary
: public simple_ipa_opt_pass
864 pass_ipa_free_inline_summary (gcc::context
*ctxt
)
865 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary
, ctxt
)
868 /* opt_pass methods: */
869 virtual unsigned int execute (function
*)
871 inline_free_summary ();
875 }; // class pass_ipa_free_inline_summary
879 simple_ipa_opt_pass
*
880 make_pass_ipa_free_inline_summary (gcc::context
*ctxt
)
882 return new pass_ipa_free_inline_summary (ctxt
);
885 /* Generate and emit a static constructor or destructor. WHICH must
886 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
887 (for chp static vars constructor) or 'B' (for chkp static bounds
888 constructor). BODY is a STATEMENT_LIST containing GENERIC
889 statements. PRIORITY is the initialization priority for this
890 constructor or destructor.
892 FINAL specify whether the externally visible name for collect2 should
896 cgraph_build_static_cdtor_1 (char which
, tree body
, int priority
, bool final
)
898 static int counter
= 0;
900 tree decl
, name
, resdecl
;
902 /* The priority is encoded in the constructor or destructor name.
903 collect2 will sort the names and arrange that they are called at
906 sprintf (which_buf
, "%c_%.5d_%d", which
, priority
, counter
++);
908 /* Proudce sane name but one not recognizable by collect2, just for the
909 case we fail to inline the function. */
910 sprintf (which_buf
, "sub_%c_%.5d_%d", which
, priority
, counter
++);
911 name
= get_file_function_name (which_buf
);
913 decl
= build_decl (input_location
, FUNCTION_DECL
, name
,
914 build_function_type_list (void_type_node
, NULL_TREE
));
915 current_function_decl
= decl
;
917 resdecl
= build_decl (input_location
,
918 RESULT_DECL
, NULL_TREE
, void_type_node
);
919 DECL_ARTIFICIAL (resdecl
) = 1;
920 DECL_RESULT (decl
) = resdecl
;
921 DECL_CONTEXT (resdecl
) = decl
;
923 allocate_struct_function (decl
, false);
925 TREE_STATIC (decl
) = 1;
926 TREE_USED (decl
) = 1;
927 DECL_ARTIFICIAL (decl
) = 1;
928 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
929 DECL_SAVED_TREE (decl
) = body
;
930 if (!targetm
.have_ctors_dtors
&& final
)
932 TREE_PUBLIC (decl
) = 1;
933 DECL_PRESERVE_P (decl
) = 1;
935 DECL_UNINLINABLE (decl
) = 1;
937 DECL_INITIAL (decl
) = make_node (BLOCK
);
938 TREE_USED (DECL_INITIAL (decl
)) = 1;
940 DECL_SOURCE_LOCATION (decl
) = input_location
;
941 cfun
->function_end_locus
= input_location
;
946 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
947 decl_init_priority_insert (decl
, priority
);
950 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
951 DECL_ATTRIBUTES (decl
) = tree_cons (get_identifier ("chkp ctor"),
954 decl_init_priority_insert (decl
, priority
);
957 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
958 DECL_ATTRIBUTES (decl
) = tree_cons (get_identifier ("bnd_legacy"),
961 decl_init_priority_insert (decl
, priority
);
964 DECL_STATIC_DESTRUCTOR (decl
) = 1;
965 decl_fini_priority_insert (decl
, priority
);
971 gimplify_function_tree (decl
);
973 cgraph_node::add_new_function (decl
, false);
976 current_function_decl
= NULL
;
979 /* Generate and emit a static constructor or destructor. WHICH must
980 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
981 (for chkp static vars constructor) or 'B' (for chkp static bounds
982 constructor). BODY is a STATEMENT_LIST containing GENERIC
983 statements. PRIORITY is the initialization priority for this
984 constructor or destructor. */
987 cgraph_build_static_cdtor (char which
, tree body
, int priority
)
989 cgraph_build_static_cdtor_1 (which
, body
, priority
, false);
992 /* A vector of FUNCTION_DECLs declared as static constructors. */
993 static vec
<tree
> static_ctors
;
994 /* A vector of FUNCTION_DECLs declared as static destructors. */
995 static vec
<tree
> static_dtors
;
997 /* When target does not have ctors and dtors, we call all constructor
998 and destructor by special initialization/destruction function
999 recognized by collect2.
1001 When we are going to build this function, collect all constructors and
1002 destructors and turn them into normal functions. */
1005 record_cdtor_fn (struct cgraph_node
*node
)
1007 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
1008 static_ctors
.safe_push (node
->decl
);
1009 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
1010 static_dtors
.safe_push (node
->decl
);
1011 node
= cgraph_node::get (node
->decl
);
1012 DECL_DISREGARD_INLINE_LIMITS (node
->decl
) = 1;
1015 /* Define global constructors/destructor functions for the CDTORS, of
1016 which they are LEN. The CDTORS are sorted by initialization
1017 priority. If CTOR_P is true, these are constructors; otherwise,
1018 they are destructors. */
1021 build_cdtor (bool ctor_p
, vec
<tree
> cdtors
)
1024 size_t len
= cdtors
.length ();
1031 priority_type priority
;
1040 p
= ctor_p
? DECL_INIT_PRIORITY (fn
) : DECL_FINI_PRIORITY (fn
);
1043 else if (p
!= priority
)
1049 /* When there is only one cdtor and target supports them, do nothing. */
1051 && targetm
.have_ctors_dtors
)
1056 /* Find the next batch of constructors/destructors with the same
1057 initialization priority. */
1062 call
= build_call_expr (fn
, 0);
1064 DECL_STATIC_CONSTRUCTOR (fn
) = 0;
1066 DECL_STATIC_DESTRUCTOR (fn
) = 0;
1067 /* We do not want to optimize away pure/const calls here.
1068 When optimizing, these should be already removed, when not
1069 optimizing, we want user to be able to breakpoint in them. */
1070 TREE_SIDE_EFFECTS (call
) = 1;
1071 append_to_statement_list (call
, &body
);
1073 gcc_assert (body
!= NULL_TREE
);
1074 /* Generate a function to call all the function of like
1076 cgraph_build_static_cdtor_1 (ctor_p
? 'I' : 'D', body
, priority
, true);
1080 /* Comparison function for qsort. P1 and P2 are actually of type
1081 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1082 used to determine the sort order. */
1085 compare_ctor (const void *p1
, const void *p2
)
1092 f1
= *(const tree
*)p1
;
1093 f2
= *(const tree
*)p2
;
1094 priority1
= DECL_INIT_PRIORITY (f1
);
1095 priority2
= DECL_INIT_PRIORITY (f2
);
1097 if (priority1
< priority2
)
1099 else if (priority1
> priority2
)
1102 /* Ensure a stable sort. Constructors are executed in backwarding
1103 order to make LTO initialize braries first. */
1104 return DECL_UID (f2
) - DECL_UID (f1
);
1107 /* Comparison function for qsort. P1 and P2 are actually of type
1108 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1109 used to determine the sort order. */
1112 compare_dtor (const void *p1
, const void *p2
)
1119 f1
= *(const tree
*)p1
;
1120 f2
= *(const tree
*)p2
;
1121 priority1
= DECL_FINI_PRIORITY (f1
);
1122 priority2
= DECL_FINI_PRIORITY (f2
);
1124 if (priority1
< priority2
)
1126 else if (priority1
> priority2
)
1129 /* Ensure a stable sort. */
1130 return DECL_UID (f1
) - DECL_UID (f2
);
1133 /* Generate functions to call static constructors and destructors
1134 for targets that do not support .ctors/.dtors sections. These
1135 functions have magic names which are detected by collect2. */
1138 build_cdtor_fns (void)
1140 if (!static_ctors
.is_empty ())
1142 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1143 static_ctors
.qsort (compare_ctor
);
1144 build_cdtor (/*ctor_p=*/true, static_ctors
);
1147 if (!static_dtors
.is_empty ())
1149 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1150 static_dtors
.qsort (compare_dtor
);
1151 build_cdtor (/*ctor_p=*/false, static_dtors
);
1155 /* Look for constructors and destructors and produce function calling them.
1156 This is needed for targets not supporting ctors or dtors, but we perform the
1157 transformation also at linktime to merge possibly numerous
1158 constructors/destructors into single function to improve code locality and
1162 ipa_cdtor_merge (void)
1164 struct cgraph_node
*node
;
1165 FOR_EACH_DEFINED_FUNCTION (node
)
1166 if (DECL_STATIC_CONSTRUCTOR (node
->decl
)
1167 || DECL_STATIC_DESTRUCTOR (node
->decl
))
1168 record_cdtor_fn (node
);
1170 static_ctors
.release ();
1171 static_dtors
.release ();
1177 const pass_data pass_data_ipa_cdtor_merge
=
1179 IPA_PASS
, /* type */
1181 OPTGROUP_NONE
, /* optinfo_flags */
1182 TV_CGRAPHOPT
, /* tv_id */
1183 0, /* properties_required */
1184 0, /* properties_provided */
1185 0, /* properties_destroyed */
1186 0, /* todo_flags_start */
1187 0, /* todo_flags_finish */
1190 class pass_ipa_cdtor_merge
: public ipa_opt_pass_d
1193 pass_ipa_cdtor_merge (gcc::context
*ctxt
)
1194 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge
, ctxt
,
1195 NULL
, /* generate_summary */
1196 NULL
, /* write_summary */
1197 NULL
, /* read_summary */
1198 NULL
, /* write_optimization_summary */
1199 NULL
, /* read_optimization_summary */
1200 NULL
, /* stmt_fixup */
1201 0, /* function_transform_todo_flags_start */
1202 NULL
, /* function_transform */
1203 NULL
) /* variable_transform */
1206 /* opt_pass methods: */
1207 virtual bool gate (function
*);
1208 virtual unsigned int execute (function
*) { return ipa_cdtor_merge (); }
1210 }; // class pass_ipa_cdtor_merge
1213 pass_ipa_cdtor_merge::gate (function
*)
1215 /* Perform the pass when we have no ctors/dtors support
1216 or at LTO time to merge multiple constructors into single
1218 return !targetm
.have_ctors_dtors
|| (optimize
&& in_lto_p
);
1224 make_pass_ipa_cdtor_merge (gcc::context
*ctxt
)
1226 return new pass_ipa_cdtor_merge (ctxt
);
1229 /* Invalid pointer representing BOTTOM for single user dataflow. */
1230 #define BOTTOM ((cgraph_node *)(size_t) 2)
1232 /* Meet operation for single user dataflow.
1233 Here we want to associate variables with sigle function that may access it.
1235 FUNCTION is current single user of a variable, VAR is variable that uses it.
1236 Latttice is stored in SINGLE_USER_MAP.
1239 - TOP by no entry in SIGNLE_USER_MAP
1240 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1241 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1244 meet (cgraph_node
*function
, varpool_node
*var
,
1245 hash_map
<varpool_node
*, cgraph_node
*> &single_user_map
)
1247 struct cgraph_node
*user
, **f
;
1249 if (var
->aux
== BOTTOM
)
1252 f
= single_user_map
.get (var
);
1258 else if (function
!= user
)
1264 /* Propagation step of single-use dataflow.
1266 Check all uses of VNODE and see if they are used by single function FUNCTION.
1267 SINGLE_USER_MAP represents the dataflow lattice. */
1270 propagate_single_user (varpool_node
*vnode
, cgraph_node
*function
,
1271 hash_map
<varpool_node
*, cgraph_node
*> &single_user_map
)
1274 struct ipa_ref
*ref
;
1276 gcc_assert (!vnode
->externally_visible
);
1278 /* If node is an alias, first meet with its target. */
1280 function
= meet (function
, vnode
->get_alias_target (), single_user_map
);
1282 /* Check all users and see if they correspond to a single function. */
1283 for (i
= 0; vnode
->iterate_referring (i
, ref
) && function
!= BOTTOM
; i
++)
1285 struct cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (ref
->referring
);
1288 if (cnode
->global
.inlined_to
)
1289 cnode
= cnode
->global
.inlined_to
;
1292 else if (function
!= cnode
)
1296 function
= meet (function
, dyn_cast
<varpool_node
*> (ref
->referring
),
1302 /* Pass setting used_by_single_function flag.
1303 This flag is set on variable when there is only one function that may
1304 possibly referr to it. */
1307 ipa_single_use (void)
1309 varpool_node
*first
= (varpool_node
*) (void *) 1;
1311 hash_map
<varpool_node
*, cgraph_node
*> single_user_map
;
1313 FOR_EACH_DEFINED_VARIABLE (var
)
1314 if (!var
->all_refs_explicit_p ())
1318 /* Enqueue symbol for dataflow. */
1323 /* The actual dataflow. */
1325 while (first
!= (void *) 1)
1327 cgraph_node
*user
, *orig_user
, **f
;
1330 first
= (varpool_node
*)first
->aux
;
1332 f
= single_user_map
.get (var
);
1337 user
= propagate_single_user (var
, orig_user
, single_user_map
);
1339 gcc_checking_assert (var
->aux
!= BOTTOM
);
1341 /* If user differs, enqueue all references. */
1342 if (user
!= orig_user
)
1347 single_user_map
.put (var
, user
);
1349 /* Enqueue all aliases for re-processing. */
1350 for (i
= 0; var
->iterate_direct_aliases (i
, ref
); i
++)
1351 if (!ref
->referring
->aux
)
1353 ref
->referring
->aux
= first
;
1354 first
= dyn_cast
<varpool_node
*> (ref
->referring
);
1356 /* Enqueue all users for re-processing. */
1357 for (i
= 0; var
->iterate_reference (i
, ref
); i
++)
1358 if (!ref
->referred
->aux
1359 && ref
->referred
->definition
1360 && is_a
<varpool_node
*> (ref
->referred
))
1362 ref
->referred
->aux
= first
;
1363 first
= dyn_cast
<varpool_node
*> (ref
->referred
);
1366 /* If user is BOTTOM, just punt on this var. */
1376 FOR_EACH_DEFINED_VARIABLE (var
)
1378 if (var
->aux
!= BOTTOM
)
1380 #ifdef ENABLE_CHECKING
1381 /* Not having the single user known means that the VAR is
1382 unreachable. Either someone forgot to remove unreachable
1383 variables or the reachability here is wrong. */
1385 gcc_assert (single_user_map
.get (var
));
1389 fprintf (dump_file
, "Variable %s/%i is used by single function\n",
1390 var
->name (), var
->order
);
1392 var
->used_by_single_function
= true;
1401 const pass_data pass_data_ipa_single_use
=
1403 IPA_PASS
, /* type */
1404 "single-use", /* name */
1405 OPTGROUP_NONE
, /* optinfo_flags */
1406 TV_CGRAPHOPT
, /* tv_id */
1407 0, /* properties_required */
1408 0, /* properties_provided */
1409 0, /* properties_destroyed */
1410 0, /* todo_flags_start */
1411 0, /* todo_flags_finish */
1414 class pass_ipa_single_use
: public ipa_opt_pass_d
1417 pass_ipa_single_use (gcc::context
*ctxt
)
1418 : ipa_opt_pass_d (pass_data_ipa_single_use
, ctxt
,
1419 NULL
, /* generate_summary */
1420 NULL
, /* write_summary */
1421 NULL
, /* read_summary */
1422 NULL
, /* write_optimization_summary */
1423 NULL
, /* read_optimization_summary */
1424 NULL
, /* stmt_fixup */
1425 0, /* function_transform_todo_flags_start */
1426 NULL
, /* function_transform */
1427 NULL
) /* variable_transform */
1430 /* opt_pass methods: */
1431 virtual bool gate (function
*);
1432 virtual unsigned int execute (function
*) { return ipa_single_use (); }
1434 }; // class pass_ipa_single_use
1437 pass_ipa_single_use::gate (function
*)
1445 make_pass_ipa_single_use (gcc::context
*ctxt
)
1447 return new pass_ipa_single_use (ctxt
);