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"
29 #include "fold-const.h"
31 #include "stringpool.h"
33 #include "basic-block.h"
35 #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"
49 #include "symbol-summary.h"
51 #include "ipa-inline.h"
52 #include "tree-inline.h"
55 #include "internal-fn.h"
56 #include "tree-ssa-alias.h"
61 /* Return true when NODE has ADDR reference. */
64 has_addr_references_p (struct cgraph_node
*node
,
65 void *data ATTRIBUTE_UNUSED
)
68 struct ipa_ref
*ref
= NULL
;
70 for (i
= 0; node
->iterate_referring (i
, ref
); i
++)
71 if (ref
->use
== IPA_REF_ADDR
)
76 /* Look for all functions inlined to NODE and update their inlined_to pointers
80 update_inlined_to_pointer (struct cgraph_node
*node
, struct cgraph_node
*inlined_to
)
82 struct cgraph_edge
*e
;
83 for (e
= node
->callees
; e
; e
= e
->next_callee
)
84 if (e
->callee
->global
.inlined_to
)
86 e
->callee
->global
.inlined_to
= inlined_to
;
87 update_inlined_to_pointer (e
->callee
, inlined_to
);
91 /* Add symtab NODE to queue starting at FIRST.
93 The queue is linked via AUX pointers and terminated by pointer to 1.
94 We enqueue nodes at two occasions: when we find them reachable or when we find
95 their bodies needed for further clonning. In the second case we mark them
96 by pointer to 2 after processing so they are re-queue when they become
100 enqueue_node (symtab_node
*node
, symtab_node
**first
,
101 hash_set
<symtab_node
*> *reachable
)
103 /* Node is still in queue; do nothing. */
104 if (node
->aux
&& node
->aux
!= (void *) 2)
106 /* Node was already processed as unreachable, re-enqueue
107 only if it became reachable now. */
108 if (node
->aux
== (void *)2 && !reachable
->contains (node
))
114 /* Process references. */
117 process_references (symtab_node
*snode
,
119 bool before_inlining_p
,
120 hash_set
<symtab_node
*> *reachable
)
123 struct ipa_ref
*ref
= NULL
;
124 for (i
= 0; snode
->iterate_reference (i
, ref
); i
++)
126 symtab_node
*node
= ref
->referred
;
127 symtab_node
*body
= node
->ultimate_alias_target ();
129 if (node
->definition
&& !node
->in_other_partition
130 && ((!DECL_EXTERNAL (node
->decl
) || node
->alias
)
131 || (((before_inlining_p
132 && ((TREE_CODE (node
->decl
) != FUNCTION_DECL
134 || (TREE_CODE (node
->decl
) == FUNCTION_DECL
135 && opt_for_fn (body
->decl
, optimize
))
136 || (symtab
->state
< IPA_SSA
139 DECL_ATTRIBUTES (body
->decl
))))))
140 /* We use variable constructors during late compilation for
141 constant folding. Keep references alive so partitioning
142 knows about potential references. */
143 || (TREE_CODE (node
->decl
) == VAR_DECL
145 && ctor_for_folding (node
->decl
)
146 != error_mark_node
))))
148 /* Be sure that we will not optimize out alias target
150 if (DECL_EXTERNAL (node
->decl
)
152 && before_inlining_p
)
153 reachable
->add (body
);
154 reachable
->add (node
);
156 enqueue_node (node
, first
, reachable
);
160 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
161 all its potential targets as reachable to permit later inlining if
162 devirtualization happens. After inlining still keep their declarations
163 around, so we can devirtualize to a direct call.
165 Also try to make trivial devirutalization when no or only one target is
169 walk_polymorphic_call_targets (hash_set
<void *> *reachable_call_targets
,
170 struct cgraph_edge
*edge
,
172 hash_set
<symtab_node
*> *reachable
,
173 bool before_inlining_p
)
178 vec
<cgraph_node
*>targets
179 = possible_polymorphic_call_targets
180 (edge
, &final
, &cache_token
);
182 if (!reachable_call_targets
->add (cache_token
))
184 for (i
= 0; i
< targets
.length (); i
++)
186 struct cgraph_node
*n
= targets
[i
];
188 /* Do not bother to mark virtual methods in anonymous namespace;
189 either we will find use of virtual table defining it, or it is
191 if (TREE_CODE (TREE_TYPE (n
->decl
)) == METHOD_TYPE
192 && type_in_anonymous_namespace_p
193 (TYPE_METHOD_BASETYPE (TREE_TYPE (n
->decl
))))
196 symtab_node
*body
= n
->function_symbol ();
198 /* Prior inlining, keep alive bodies of possible targets for
201 && (before_inlining_p
202 && opt_for_fn (body
->decl
, optimize
)
203 && opt_for_fn (body
->decl
, flag_devirtualize
)))
205 /* Be sure that we will not optimize out alias target
207 if (DECL_EXTERNAL (n
->decl
)
209 && before_inlining_p
)
210 reachable
->add (body
);
213 /* Even after inlining we want to keep the possible targets in the
214 boundary, so late passes can still produce direct call even if
215 the chance for inlining is lost. */
216 enqueue_node (n
, first
, reachable
);
220 /* Very trivial devirtualization; when the type is
221 final or anonymous (so we know all its derivation)
222 and there is only one possible virtual call target,
223 make the edge direct. */
226 if (targets
.length () <= 1 && dbg_cnt (devirt
))
228 cgraph_node
*target
, *node
= edge
->caller
;
229 if (targets
.length () == 1)
232 target
= cgraph_node::get_create
233 (builtin_decl_implicit (BUILT_IN_UNREACHABLE
));
235 if (dump_enabled_p ())
239 locus
= gimple_location (edge
->call_stmt
);
241 locus
= UNKNOWN_LOCATION
;
242 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, locus
,
243 "devirtualizing call in %s/%i to %s/%i\n",
244 edge
->caller
->name (), edge
->caller
->order
,
248 edge
= edge
->make_direct (target
);
249 if (inline_summaries
)
250 inline_update_overall_summary (node
);
251 else if (edge
->call_stmt
)
253 edge
->redirect_call_stmt_to_callee ();
255 /* Call to __builtin_unreachable shouldn't be instrumented. */
256 if (!targets
.length ())
257 gimple_call_set_with_bounds (edge
->call_stmt
, false);
263 /* Perform reachability analysis and reclaim all unreachable nodes.
265 The algorithm is basically mark&sweep but with some extra refinements:
267 - reachable extern inline functions needs special handling; the bodies needs
268 to stay in memory until inlining in hope that they will be inlined.
269 After inlining we release their bodies and turn them into unanalyzed
270 nodes even when they are reachable.
272 - virtual functions are kept in callgraph even if they seem unreachable in
273 hope calls to them will be devirtualized.
275 Again we remove them after inlining. In late optimization some
276 devirtualization may happen, but it is not important since we won't inline
277 the call. In theory early opts and IPA should work out all important cases.
279 - virtual clones needs bodies of their origins for later materialization;
280 this means that we want to keep the body even if the origin is unreachable
281 otherwise. To avoid origin from sitting in the callgraph and being
282 walked by IPA passes, we turn them into unanalyzed nodes with body
285 We maintain set of function declaration where body needs to stay in
286 body_needed_for_clonning
288 Inline clones represent special case: their declaration match the
289 declaration of origin and cgraph_remove_node already knows how to
290 reshape callgraph and preserve body when offline copy of function or
291 inline clone is being removed.
293 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
294 variables with DECL_INITIAL set. We finalize these and keep reachable
295 ones around for constant folding purposes. After inlining we however
296 stop walking their references to let everything static referneced by them
297 to be removed when it is otherwise unreachable.
299 We maintain queue of both reachable symbols (i.e. defined symbols that needs
300 to stay) and symbols that are in boundary (i.e. external symbols referenced
301 by reachable symbols or origins of clones). The queue is represented
302 as linked list by AUX pointer terminated by 1.
304 At the end we keep all reachable symbols. For symbols in boundary we always
305 turn definition into a declaration, but we may keep function body around
306 based on body_needed_for_clonning
308 All symbols that enter the queue have AUX pointer non-zero and are in the
309 boundary. Pointer set REACHABLE is used to track reachable symbols.
311 Every symbol can be visited twice - once as part of boundary and once
312 as real reachable symbol. enqueue_node needs to decide whether the
313 node needs to be re-queued for second processing. For this purpose
314 we set AUX pointer of processed symbols in the boundary to constant 2. */
317 symbol_table::remove_unreachable_nodes (FILE *file
)
319 symtab_node
*first
= (symtab_node
*) (void *) 1;
320 struct cgraph_node
*node
, *next
;
321 varpool_node
*vnode
, *vnext
;
322 bool changed
= false;
323 hash_set
<symtab_node
*> reachable
;
324 hash_set
<tree
> body_needed_for_clonning
;
325 hash_set
<void *> reachable_call_targets
;
326 bool before_inlining_p
= symtab
->state
< (!optimize
? IPA_SSA
327 : IPA_SSA_AFTER_INLINING
);
329 timevar_push (TV_IPA_UNREACHABLE
);
330 build_type_inheritance_graph ();
332 fprintf (file
, "\nReclaiming functions:");
333 #ifdef ENABLE_CHECKING
334 FOR_EACH_FUNCTION (node
)
335 gcc_assert (!node
->aux
);
336 FOR_EACH_VARIABLE (vnode
)
337 gcc_assert (!vnode
->aux
);
339 /* Mark functions whose bodies are obviously needed.
340 This is mostly when they can be referenced externally. Inline clones
341 are special since their declarations are shared with master clone and thus
342 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
343 FOR_EACH_FUNCTION (node
)
345 node
->used_as_abstract_origin
= false;
347 && !node
->global
.inlined_to
348 && !node
->in_other_partition
349 && !node
->can_remove_if_no_direct_calls_and_refs_p ())
351 gcc_assert (!node
->global
.inlined_to
);
352 reachable
.add (node
);
353 enqueue_node (node
, &first
, &reachable
);
356 gcc_assert (!node
->aux
);
359 /* Mark variables that are obviously needed. */
360 FOR_EACH_DEFINED_VARIABLE (vnode
)
361 if (!vnode
->can_remove_if_no_refs_p()
362 && !vnode
->in_other_partition
)
364 reachable
.add (vnode
);
365 enqueue_node (vnode
, &first
, &reachable
);
368 /* Perform reachability analysis. */
369 while (first
!= (symtab_node
*) (void *) 1)
371 bool in_boundary_p
= !reachable
.contains (first
);
372 symtab_node
*node
= first
;
374 first
= (symtab_node
*)first
->aux
;
376 /* If we are processing symbol in boundary, mark its AUX pointer for
377 possible later re-processing in enqueue_node. */
380 node
->aux
= (void *)2;
381 if (node
->alias
&& node
->analyzed
)
382 enqueue_node (node
->get_alias_target (), &first
, &reachable
);
386 if (TREE_CODE (node
->decl
) == FUNCTION_DECL
387 && DECL_ABSTRACT_ORIGIN (node
->decl
))
389 struct cgraph_node
*origin_node
390 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node
->decl
));
391 if (origin_node
&& !origin_node
->used_as_abstract_origin
)
393 origin_node
->used_as_abstract_origin
= true;
394 gcc_assert (!origin_node
->prev_sibling_clone
);
395 gcc_assert (!origin_node
->next_sibling_clone
);
396 for (cgraph_node
*n
= origin_node
->clones
; n
;
397 n
= n
->next_sibling_clone
)
398 if (n
->decl
== DECL_ABSTRACT_ORIGIN (node
->decl
))
399 n
->used_as_abstract_origin
= true;
402 /* If any symbol in a comdat group is reachable, force
403 all externally visible symbols in the same comdat
404 group to be reachable as well. Comdat-local symbols
405 can be discarded if all uses were inlined. */
406 if (node
->same_comdat_group
)
409 for (next
= node
->same_comdat_group
;
411 next
= next
->same_comdat_group
)
412 if (!next
->comdat_local_p ()
413 && !reachable
.add (next
))
414 enqueue_node (next
, &first
, &reachable
);
416 /* Mark references as reachable. */
417 process_references (node
, &first
, before_inlining_p
, &reachable
);
420 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
422 /* Mark the callees reachable unless they are direct calls to extern
423 inline functions we decided to not inline. */
426 struct cgraph_edge
*e
;
427 /* Keep alive possible targets for devirtualization. */
428 if (opt_for_fn (cnode
->decl
, optimize
)
429 && opt_for_fn (cnode
->decl
, flag_devirtualize
))
431 struct cgraph_edge
*next
;
432 for (e
= cnode
->indirect_calls
; e
; e
= next
)
434 next
= e
->next_callee
;
435 if (e
->indirect_info
->polymorphic
)
436 walk_polymorphic_call_targets (&reachable_call_targets
,
437 e
, &first
, &reachable
,
441 for (e
= cnode
->callees
; e
; e
= e
->next_callee
)
443 symtab_node
*body
= e
->callee
->function_symbol ();
444 if (e
->callee
->definition
445 && !e
->callee
->in_other_partition
446 && (!e
->inline_failed
447 || !DECL_EXTERNAL (e
->callee
->decl
)
449 || (before_inlining_p
450 && (opt_for_fn (body
->decl
, optimize
)
451 || (symtab
->state
< IPA_SSA
454 DECL_ATTRIBUTES (body
->decl
)))))))
456 /* Be sure that we will not optimize out alias target
458 if (DECL_EXTERNAL (e
->callee
->decl
)
460 && before_inlining_p
)
461 reachable
.add (body
);
462 reachable
.add (e
->callee
);
464 enqueue_node (e
->callee
, &first
, &reachable
);
467 /* When inline clone exists, mark body to be preserved so when removing
468 offline copy of the function we don't kill it. */
469 if (cnode
->global
.inlined_to
)
470 body_needed_for_clonning
.add (cnode
->decl
);
472 /* For instrumentation clones we always need original
473 function node for proper LTO privatization. */
474 if (cnode
->instrumentation_clone
475 && cnode
->definition
)
477 gcc_assert (cnode
->instrumented_version
|| in_lto_p
);
478 if (cnode
->instrumented_version
)
480 enqueue_node (cnode
->instrumented_version
, &first
,
482 reachable
.add (cnode
->instrumented_version
);
486 /* For non-inline clones, force their origins to the boundary and ensure
487 that body is not removed. */
488 while (cnode
->clone_of
)
490 bool noninline
= cnode
->clone_of
->decl
!= cnode
->decl
;
491 cnode
= cnode
->clone_of
;
494 body_needed_for_clonning
.add (cnode
->decl
);
495 enqueue_node (cnode
, &first
, &reachable
);
500 else if (cnode
->thunk
.thunk_p
)
501 enqueue_node (cnode
->callees
->callee
, &first
, &reachable
);
503 /* If any reachable function has simd clones, mark them as
504 reachable as well. */
505 if (cnode
->simd_clones
)
508 for (next
= cnode
->simd_clones
;
510 next
= next
->simdclone
->next_clone
)
512 || !reachable
.add (next
))
513 enqueue_node (next
, &first
, &reachable
);
516 /* When we see constructor of external variable, keep referred nodes in the
517 boundary. This will also hold initializers of the external vars NODE
519 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
);
521 && DECL_EXTERNAL (node
->decl
)
525 struct ipa_ref
*ref
= NULL
;
526 for (int i
= 0; node
->iterate_reference (i
, ref
); i
++)
527 enqueue_node (ref
->referred
, &first
, &reachable
);
531 /* Remove unreachable functions. */
532 for (node
= first_function (); node
; node
= next
)
534 next
= next_function (node
);
536 /* If node is not needed at all, remove it. */
540 fprintf (file
, " %s/%i", node
->name (), node
->order
);
544 /* If node is unreachable, remove its body. */
545 else if (!reachable
.contains (node
))
547 /* We keep definitions of thunks and aliases in the boundary so
548 we can walk to the ultimate alias targets and function symbols
550 if (node
->alias
|| node
->thunk
.thunk_p
)
552 else if (!body_needed_for_clonning
.contains (node
->decl
)
553 && !node
->alias
&& !node
->thunk
.thunk_p
)
554 node
->release_body ();
555 else if (!node
->clone_of
)
556 gcc_assert (in_lto_p
|| DECL_RESULT (node
->decl
));
557 if (node
->definition
&& !node
->alias
&& !node
->thunk
.thunk_p
)
560 fprintf (file
, " %s/%i", node
->name (), node
->order
);
561 node
->body_removed
= true;
562 node
->analyzed
= false;
563 node
->definition
= false;
564 node
->cpp_implicit_alias
= false;
566 node
->thunk
.thunk_p
= false;
567 node
->weakref
= false;
568 /* After early inlining we drop always_inline attributes on
569 bodies of functions that are still referenced (have their
571 DECL_ATTRIBUTES (node
->decl
)
572 = remove_attribute ("always_inline",
573 DECL_ATTRIBUTES (node
->decl
));
574 if (!node
->in_other_partition
)
575 node
->local
.local
= false;
576 node
->remove_callees ();
577 node
->remove_all_references ();
579 if (node
->thunk
.thunk_p
580 && node
->thunk
.add_pointer_bounds_args
)
582 node
->thunk
.thunk_p
= false;
583 node
->thunk
.add_pointer_bounds_args
= false;
588 gcc_assert (node
->clone_of
|| !node
->has_gimple_body_p ()
589 || in_lto_p
|| DECL_RESULT (node
->decl
));
592 /* Inline clones might be kept around so their materializing allows further
593 cloning. If the function the clone is inlined into is removed, we need
594 to turn it into normal cone. */
595 FOR_EACH_FUNCTION (node
)
597 if (node
->global
.inlined_to
600 gcc_assert (node
->clones
);
601 node
->global
.inlined_to
= NULL
;
602 update_inlined_to_pointer (node
, node
);
607 /* Remove unreachable variables. */
609 fprintf (file
, "\nReclaiming variables:");
610 for (vnode
= first_variable (); vnode
; vnode
= vnext
)
612 vnext
= next_variable (vnode
);
614 /* For can_refer_decl_in_current_unit_p we want to track for
615 all external variables if they are defined in other partition
617 && (!flag_ltrans
|| !DECL_EXTERNAL (vnode
->decl
)))
619 struct ipa_ref
*ref
= NULL
;
621 /* First remove the aliases, so varpool::remove can possibly lookup
622 the constructor and save it for future use. */
623 while (vnode
->iterate_direct_aliases (0, ref
))
626 fprintf (file
, " %s/%i", ref
->referred
->name (),
627 ref
->referred
->order
);
628 ref
->referring
->remove ();
631 fprintf (file
, " %s/%i", vnode
->name (), vnode
->order
);
632 vnext
= next_variable (vnode
);
636 else if (!reachable
.contains (vnode
) && !vnode
->alias
)
639 if (vnode
->definition
)
642 fprintf (file
, " %s", vnode
->name ());
645 /* Keep body if it may be useful for constant folding. */
646 if ((init
= ctor_for_folding (vnode
->decl
)) == error_mark_node
647 && !POINTER_BOUNDS_P (vnode
->decl
))
648 vnode
->remove_initializer ();
650 DECL_INITIAL (vnode
->decl
) = init
;
651 vnode
->body_removed
= true;
652 vnode
->definition
= false;
653 vnode
->analyzed
= false;
656 vnode
->remove_from_same_comdat_group ();
658 vnode
->remove_all_references ();
664 /* Now update address_taken flags and try to promote functions to be local. */
666 fprintf (file
, "\nClearing address taken flags:");
667 FOR_EACH_DEFINED_FUNCTION (node
)
668 if (node
->address_taken
669 && !node
->used_from_other_partition
)
671 if (!node
->call_for_symbol_and_aliases
672 (has_addr_references_p
, NULL
, true)
673 && (!node
->instrumentation_clone
674 || !node
->instrumented_version
675 || !node
->instrumented_version
->address_taken
))
678 fprintf (file
, " %s", node
->name ());
679 node
->address_taken
= false;
681 if (node
->local_p ())
683 node
->local
.local
= true;
685 fprintf (file
, " (local)");
690 fprintf (file
, "\n");
692 #ifdef ENABLE_CHECKING
693 symtab_node::verify_symtab_nodes ();
696 /* If we removed something, perhaps profile could be improved. */
697 if (changed
&& optimize
&& inline_edge_summary_vec
.exists ())
698 FOR_EACH_DEFINED_FUNCTION (node
)
699 ipa_propagate_frequency (node
);
701 timevar_pop (TV_IPA_UNREACHABLE
);
705 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
706 as needed, also clear EXPLICIT_REFS if the references to given variable
707 do not need to be explicit. */
710 process_references (varpool_node
*vnode
,
711 bool *written
, bool *address_taken
,
712 bool *read
, bool *explicit_refs
)
717 if (!vnode
->all_refs_explicit_p ()
718 || TREE_THIS_VOLATILE (vnode
->decl
))
719 *explicit_refs
= false;
721 for (i
= 0; vnode
->iterate_referring (i
, ref
)
722 && *explicit_refs
&& (!*written
|| !*address_taken
|| !*read
); i
++)
726 *address_taken
= true;
735 process_references (dyn_cast
<varpool_node
*> (ref
->referring
), written
,
736 address_taken
, read
, explicit_refs
);
743 /* Set TREE_READONLY bit. */
746 set_readonly_bit (varpool_node
*vnode
, void *data ATTRIBUTE_UNUSED
)
748 TREE_READONLY (vnode
->decl
) = true;
752 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
755 set_writeonly_bit (varpool_node
*vnode
, void *data
)
757 vnode
->writeonly
= true;
760 DECL_INITIAL (vnode
->decl
) = NULL
;
763 if (vnode
->num_references ())
764 *(bool *)data
= true;
765 vnode
->remove_all_references ();
771 /* Clear addressale bit of VNODE. */
774 clear_addressable_bit (varpool_node
*vnode
, void *data ATTRIBUTE_UNUSED
)
776 vnode
->address_taken
= false;
777 TREE_ADDRESSABLE (vnode
->decl
) = 0;
781 /* Discover variables that have no longer address taken or that are read only
782 and update their flags.
784 Return true when unreachable symbol removan should be done.
786 FIXME: This can not be done in between gimplify and omp_expand since
787 readonly flag plays role on what is shared and what is not. Currently we do
788 this transformation as part of whole program visibility and re-do at
789 ipa-reference pass (to take into account clonning), but it would
790 make sense to do it before early optimizations. */
793 ipa_discover_readonly_nonaddressable_vars (void)
795 bool remove_p
= false;
798 fprintf (dump_file
, "Clearing variable flags:");
799 FOR_EACH_VARIABLE (vnode
)
801 && (TREE_ADDRESSABLE (vnode
->decl
)
803 || !TREE_READONLY (vnode
->decl
)))
805 bool written
= false;
806 bool address_taken
= false;
808 bool explicit_refs
= true;
810 process_references (vnode
, &written
, &address_taken
, &read
,
816 if (TREE_ADDRESSABLE (vnode
->decl
) && dump_file
)
817 fprintf (dump_file
, " %s (non-addressable)", vnode
->name ());
818 vnode
->call_for_symbol_and_aliases (clear_addressable_bit
, NULL
,
821 if (!address_taken
&& !written
822 /* Making variable in explicit section readonly can cause section
824 See e.g. gcc.c-torture/compile/pr23237.c */
825 && vnode
->get_section () == NULL
)
827 if (!TREE_READONLY (vnode
->decl
) && dump_file
)
828 fprintf (dump_file
, " %s (read-only)", vnode
->name ());
829 vnode
->call_for_symbol_and_aliases (set_readonly_bit
, NULL
, true);
831 if (!vnode
->writeonly
&& !read
&& !address_taken
&& written
)
834 fprintf (dump_file
, " %s (write-only)", vnode
->name ());
835 vnode
->call_for_symbol_and_aliases (set_writeonly_bit
, &remove_p
,
840 fprintf (dump_file
, "\n");
844 /* Free inline summary. */
848 const pass_data pass_data_ipa_free_inline_summary
=
850 SIMPLE_IPA_PASS
, /* type */
851 "free-inline-summary", /* name */
852 OPTGROUP_NONE
, /* optinfo_flags */
853 TV_IPA_FREE_INLINE_SUMMARY
, /* tv_id */
854 0, /* properties_required */
855 0, /* properties_provided */
856 0, /* properties_destroyed */
857 0, /* todo_flags_start */
858 /* Early optimizations may make function unreachable. We can not
859 remove unreachable functions as part of the ealry opts pass because
860 TODOs are run before subpasses. Do it here. */
861 ( TODO_remove_functions
| TODO_dump_symtab
), /* todo_flags_finish */
864 class pass_ipa_free_inline_summary
: public simple_ipa_opt_pass
867 pass_ipa_free_inline_summary (gcc::context
*ctxt
)
868 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary
, ctxt
)
871 /* opt_pass methods: */
872 virtual unsigned int execute (function
*)
874 inline_free_summary ();
878 }; // class pass_ipa_free_inline_summary
882 simple_ipa_opt_pass
*
883 make_pass_ipa_free_inline_summary (gcc::context
*ctxt
)
885 return new pass_ipa_free_inline_summary (ctxt
);
888 /* Generate and emit a static constructor or destructor. WHICH must
889 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
890 (for chp static vars constructor) or 'B' (for chkp static bounds
891 constructor). BODY is a STATEMENT_LIST containing GENERIC
892 statements. PRIORITY is the initialization priority for this
893 constructor or destructor.
895 FINAL specify whether the externally visible name for collect2 should
899 cgraph_build_static_cdtor_1 (char which
, tree body
, int priority
, bool final
)
901 static int counter
= 0;
903 tree decl
, name
, resdecl
;
905 /* The priority is encoded in the constructor or destructor name.
906 collect2 will sort the names and arrange that they are called at
909 sprintf (which_buf
, "%c_%.5d_%d", which
, priority
, counter
++);
911 /* Proudce sane name but one not recognizable by collect2, just for the
912 case we fail to inline the function. */
913 sprintf (which_buf
, "sub_%c_%.5d_%d", which
, priority
, counter
++);
914 name
= get_file_function_name (which_buf
);
916 decl
= build_decl (input_location
, FUNCTION_DECL
, name
,
917 build_function_type_list (void_type_node
, NULL_TREE
));
918 current_function_decl
= decl
;
920 resdecl
= build_decl (input_location
,
921 RESULT_DECL
, NULL_TREE
, void_type_node
);
922 DECL_ARTIFICIAL (resdecl
) = 1;
923 DECL_RESULT (decl
) = resdecl
;
924 DECL_CONTEXT (resdecl
) = decl
;
926 allocate_struct_function (decl
, false);
928 TREE_STATIC (decl
) = 1;
929 TREE_USED (decl
) = 1;
930 DECL_ARTIFICIAL (decl
) = 1;
931 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
932 DECL_SAVED_TREE (decl
) = body
;
933 if (!targetm
.have_ctors_dtors
&& final
)
935 TREE_PUBLIC (decl
) = 1;
936 DECL_PRESERVE_P (decl
) = 1;
938 DECL_UNINLINABLE (decl
) = 1;
940 DECL_INITIAL (decl
) = make_node (BLOCK
);
941 TREE_USED (DECL_INITIAL (decl
)) = 1;
943 DECL_SOURCE_LOCATION (decl
) = input_location
;
944 cfun
->function_end_locus
= input_location
;
949 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
950 decl_init_priority_insert (decl
, priority
);
953 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
954 DECL_ATTRIBUTES (decl
) = tree_cons (get_identifier ("chkp ctor"),
957 decl_init_priority_insert (decl
, priority
);
960 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
961 DECL_ATTRIBUTES (decl
) = tree_cons (get_identifier ("bnd_legacy"),
964 decl_init_priority_insert (decl
, priority
);
967 DECL_STATIC_DESTRUCTOR (decl
) = 1;
968 decl_fini_priority_insert (decl
, priority
);
974 gimplify_function_tree (decl
);
976 cgraph_node::add_new_function (decl
, false);
979 current_function_decl
= NULL
;
982 /* Generate and emit a static constructor or destructor. WHICH must
983 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
984 (for chkp static vars constructor) or 'B' (for chkp static bounds
985 constructor). BODY is a STATEMENT_LIST containing GENERIC
986 statements. PRIORITY is the initialization priority for this
987 constructor or destructor. */
990 cgraph_build_static_cdtor (char which
, tree body
, int priority
)
992 cgraph_build_static_cdtor_1 (which
, body
, priority
, false);
995 /* A vector of FUNCTION_DECLs declared as static constructors. */
996 static vec
<tree
> static_ctors
;
997 /* A vector of FUNCTION_DECLs declared as static destructors. */
998 static vec
<tree
> static_dtors
;
1000 /* When target does not have ctors and dtors, we call all constructor
1001 and destructor by special initialization/destruction function
1002 recognized by collect2.
1004 When we are going to build this function, collect all constructors and
1005 destructors and turn them into normal functions. */
1008 record_cdtor_fn (struct cgraph_node
*node
)
1010 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
1011 static_ctors
.safe_push (node
->decl
);
1012 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
1013 static_dtors
.safe_push (node
->decl
);
1014 node
= cgraph_node::get (node
->decl
);
1015 DECL_DISREGARD_INLINE_LIMITS (node
->decl
) = 1;
1018 /* Define global constructors/destructor functions for the CDTORS, of
1019 which they are LEN. The CDTORS are sorted by initialization
1020 priority. If CTOR_P is true, these are constructors; otherwise,
1021 they are destructors. */
1024 build_cdtor (bool ctor_p
, vec
<tree
> cdtors
)
1027 size_t len
= cdtors
.length ();
1034 priority_type priority
;
1043 p
= ctor_p
? DECL_INIT_PRIORITY (fn
) : DECL_FINI_PRIORITY (fn
);
1046 else if (p
!= priority
)
1052 /* When there is only one cdtor and target supports them, do nothing. */
1054 && targetm
.have_ctors_dtors
)
1059 /* Find the next batch of constructors/destructors with the same
1060 initialization priority. */
1065 call
= build_call_expr (fn
, 0);
1067 DECL_STATIC_CONSTRUCTOR (fn
) = 0;
1069 DECL_STATIC_DESTRUCTOR (fn
) = 0;
1070 /* We do not want to optimize away pure/const calls here.
1071 When optimizing, these should be already removed, when not
1072 optimizing, we want user to be able to breakpoint in them. */
1073 TREE_SIDE_EFFECTS (call
) = 1;
1074 append_to_statement_list (call
, &body
);
1076 gcc_assert (body
!= NULL_TREE
);
1077 /* Generate a function to call all the function of like
1079 cgraph_build_static_cdtor_1 (ctor_p
? 'I' : 'D', body
, priority
, true);
1083 /* Comparison function for qsort. P1 and P2 are actually of type
1084 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1085 used to determine the sort order. */
1088 compare_ctor (const void *p1
, const void *p2
)
1095 f1
= *(const tree
*)p1
;
1096 f2
= *(const tree
*)p2
;
1097 priority1
= DECL_INIT_PRIORITY (f1
);
1098 priority2
= DECL_INIT_PRIORITY (f2
);
1100 if (priority1
< priority2
)
1102 else if (priority1
> priority2
)
1105 /* Ensure a stable sort. Constructors are executed in backwarding
1106 order to make LTO initialize braries first. */
1107 return DECL_UID (f2
) - DECL_UID (f1
);
1110 /* Comparison function for qsort. P1 and P2 are actually of type
1111 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1112 used to determine the sort order. */
1115 compare_dtor (const void *p1
, const void *p2
)
1122 f1
= *(const tree
*)p1
;
1123 f2
= *(const tree
*)p2
;
1124 priority1
= DECL_FINI_PRIORITY (f1
);
1125 priority2
= DECL_FINI_PRIORITY (f2
);
1127 if (priority1
< priority2
)
1129 else if (priority1
> priority2
)
1132 /* Ensure a stable sort. */
1133 return DECL_UID (f1
) - DECL_UID (f2
);
1136 /* Generate functions to call static constructors and destructors
1137 for targets that do not support .ctors/.dtors sections. These
1138 functions have magic names which are detected by collect2. */
1141 build_cdtor_fns (void)
1143 if (!static_ctors
.is_empty ())
1145 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1146 static_ctors
.qsort (compare_ctor
);
1147 build_cdtor (/*ctor_p=*/true, static_ctors
);
1150 if (!static_dtors
.is_empty ())
1152 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1153 static_dtors
.qsort (compare_dtor
);
1154 build_cdtor (/*ctor_p=*/false, static_dtors
);
1158 /* Look for constructors and destructors and produce function calling them.
1159 This is needed for targets not supporting ctors or dtors, but we perform the
1160 transformation also at linktime to merge possibly numerous
1161 constructors/destructors into single function to improve code locality and
1165 ipa_cdtor_merge (void)
1167 struct cgraph_node
*node
;
1168 FOR_EACH_DEFINED_FUNCTION (node
)
1169 if (DECL_STATIC_CONSTRUCTOR (node
->decl
)
1170 || DECL_STATIC_DESTRUCTOR (node
->decl
))
1171 record_cdtor_fn (node
);
1173 static_ctors
.release ();
1174 static_dtors
.release ();
1180 const pass_data pass_data_ipa_cdtor_merge
=
1182 IPA_PASS
, /* type */
1184 OPTGROUP_NONE
, /* optinfo_flags */
1185 TV_CGRAPHOPT
, /* tv_id */
1186 0, /* properties_required */
1187 0, /* properties_provided */
1188 0, /* properties_destroyed */
1189 0, /* todo_flags_start */
1190 0, /* todo_flags_finish */
1193 class pass_ipa_cdtor_merge
: public ipa_opt_pass_d
1196 pass_ipa_cdtor_merge (gcc::context
*ctxt
)
1197 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge
, ctxt
,
1198 NULL
, /* generate_summary */
1199 NULL
, /* write_summary */
1200 NULL
, /* read_summary */
1201 NULL
, /* write_optimization_summary */
1202 NULL
, /* read_optimization_summary */
1203 NULL
, /* stmt_fixup */
1204 0, /* function_transform_todo_flags_start */
1205 NULL
, /* function_transform */
1206 NULL
) /* variable_transform */
1209 /* opt_pass methods: */
1210 virtual bool gate (function
*);
1211 virtual unsigned int execute (function
*) { return ipa_cdtor_merge (); }
1213 }; // class pass_ipa_cdtor_merge
1216 pass_ipa_cdtor_merge::gate (function
*)
1218 /* Perform the pass when we have no ctors/dtors support
1219 or at LTO time to merge multiple constructors into single
1221 return !targetm
.have_ctors_dtors
|| (optimize
&& in_lto_p
);
1227 make_pass_ipa_cdtor_merge (gcc::context
*ctxt
)
1229 return new pass_ipa_cdtor_merge (ctxt
);
1232 /* Invalid pointer representing BOTTOM for single user dataflow. */
1233 #define BOTTOM ((cgraph_node *)(size_t) 2)
1235 /* Meet operation for single user dataflow.
1236 Here we want to associate variables with sigle function that may access it.
1238 FUNCTION is current single user of a variable, VAR is variable that uses it.
1239 Latttice is stored in SINGLE_USER_MAP.
1242 - TOP by no entry in SIGNLE_USER_MAP
1243 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1244 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1247 meet (cgraph_node
*function
, varpool_node
*var
,
1248 hash_map
<varpool_node
*, cgraph_node
*> &single_user_map
)
1250 struct cgraph_node
*user
, **f
;
1252 if (var
->aux
== BOTTOM
)
1255 f
= single_user_map
.get (var
);
1261 else if (function
!= user
)
1267 /* Propagation step of single-use dataflow.
1269 Check all uses of VNODE and see if they are used by single function FUNCTION.
1270 SINGLE_USER_MAP represents the dataflow lattice. */
1273 propagate_single_user (varpool_node
*vnode
, cgraph_node
*function
,
1274 hash_map
<varpool_node
*, cgraph_node
*> &single_user_map
)
1277 struct ipa_ref
*ref
;
1279 gcc_assert (!vnode
->externally_visible
);
1281 /* If node is an alias, first meet with its target. */
1283 function
= meet (function
, vnode
->get_alias_target (), single_user_map
);
1285 /* Check all users and see if they correspond to a single function. */
1286 for (i
= 0; vnode
->iterate_referring (i
, ref
) && function
!= BOTTOM
; i
++)
1288 struct cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (ref
->referring
);
1291 if (cnode
->global
.inlined_to
)
1292 cnode
= cnode
->global
.inlined_to
;
1295 else if (function
!= cnode
)
1299 function
= meet (function
, dyn_cast
<varpool_node
*> (ref
->referring
),
1305 /* Pass setting used_by_single_function flag.
1306 This flag is set on variable when there is only one function that may
1307 possibly referr to it. */
1310 ipa_single_use (void)
1312 varpool_node
*first
= (varpool_node
*) (void *) 1;
1314 hash_map
<varpool_node
*, cgraph_node
*> single_user_map
;
1316 FOR_EACH_DEFINED_VARIABLE (var
)
1317 if (!var
->all_refs_explicit_p ())
1321 /* Enqueue symbol for dataflow. */
1326 /* The actual dataflow. */
1328 while (first
!= (void *) 1)
1330 cgraph_node
*user
, *orig_user
, **f
;
1333 first
= (varpool_node
*)first
->aux
;
1335 f
= single_user_map
.get (var
);
1340 user
= propagate_single_user (var
, orig_user
, single_user_map
);
1342 gcc_checking_assert (var
->aux
!= BOTTOM
);
1344 /* If user differs, enqueue all references. */
1345 if (user
!= orig_user
)
1350 single_user_map
.put (var
, user
);
1352 /* Enqueue all aliases for re-processing. */
1353 for (i
= 0; var
->iterate_direct_aliases (i
, ref
); i
++)
1354 if (!ref
->referring
->aux
)
1356 ref
->referring
->aux
= first
;
1357 first
= dyn_cast
<varpool_node
*> (ref
->referring
);
1359 /* Enqueue all users for re-processing. */
1360 for (i
= 0; var
->iterate_reference (i
, ref
); i
++)
1361 if (!ref
->referred
->aux
1362 && ref
->referred
->definition
1363 && is_a
<varpool_node
*> (ref
->referred
))
1365 ref
->referred
->aux
= first
;
1366 first
= dyn_cast
<varpool_node
*> (ref
->referred
);
1369 /* If user is BOTTOM, just punt on this var. */
1379 FOR_EACH_DEFINED_VARIABLE (var
)
1381 if (var
->aux
!= BOTTOM
)
1383 #ifdef ENABLE_CHECKING
1384 /* Not having the single user known means that the VAR is
1385 unreachable. Either someone forgot to remove unreachable
1386 variables or the reachability here is wrong. */
1388 gcc_assert (single_user_map
.get (var
));
1392 fprintf (dump_file
, "Variable %s/%i is used by single function\n",
1393 var
->name (), var
->order
);
1395 var
->used_by_single_function
= true;
1404 const pass_data pass_data_ipa_single_use
=
1406 IPA_PASS
, /* type */
1407 "single-use", /* name */
1408 OPTGROUP_NONE
, /* optinfo_flags */
1409 TV_CGRAPHOPT
, /* tv_id */
1410 0, /* properties_required */
1411 0, /* properties_provided */
1412 0, /* properties_destroyed */
1413 0, /* todo_flags_start */
1414 0, /* todo_flags_finish */
1417 class pass_ipa_single_use
: public ipa_opt_pass_d
1420 pass_ipa_single_use (gcc::context
*ctxt
)
1421 : ipa_opt_pass_d (pass_data_ipa_single_use
, ctxt
,
1422 NULL
, /* generate_summary */
1423 NULL
, /* write_summary */
1424 NULL
, /* read_summary */
1425 NULL
, /* write_optimization_summary */
1426 NULL
, /* read_optimization_summary */
1427 NULL
, /* stmt_fixup */
1428 0, /* function_transform_todo_flags_start */
1429 NULL
, /* function_transform */
1430 NULL
) /* variable_transform */
1433 /* opt_pass methods: */
1434 virtual bool gate (function
*);
1435 virtual unsigned int execute (function
*) { return ipa_single_use (); }
1437 }; // class pass_ipa_single_use
1440 pass_ipa_single_use::gate (function
*)
1448 make_pass_ipa_single_use (gcc::context
*ctxt
)
1450 return new pass_ipa_single_use (ctxt
);