Implement -mmemcpy-strategy= and -mmemset-strategy= options
[official-gcc.git] / gcc / ipa.c
blobc870a6f067849073b7819a232924c482e2b36e9f
1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003-2013 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
9 version.
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
14 for more details.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "cgraph.h"
25 #include "tree-pass.h"
26 #include "gimple.h"
27 #include "ggc.h"
28 #include "flags.h"
29 #include "pointer-set.h"
30 #include "target.h"
31 #include "tree-iterator.h"
32 #include "ipa-utils.h"
33 #include "pointer-set.h"
34 #include "ipa-inline.h"
35 #include "hash-table.h"
36 #include "tree-inline.h"
37 #include "profile.h"
38 #include "params.h"
39 #include "lto-streamer.h"
40 #include "data-streamer.h"
41 #include "value-prof.h"
43 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
45 static bool
46 cgraph_non_local_node_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
48 /* FIXME: Aliases can be local, but i386 gets thunks wrong then. */
49 return !(cgraph_only_called_directly_or_aliased_p (node)
50 && !ipa_ref_has_aliases_p (&node->symbol.ref_list)
51 && node->symbol.definition
52 && !DECL_EXTERNAL (node->symbol.decl)
53 && !node->symbol.externally_visible
54 && !node->symbol.used_from_other_partition
55 && !node->symbol.in_other_partition);
58 /* Return true when function can be marked local. */
60 static bool
61 cgraph_local_node_p (struct cgraph_node *node)
63 struct cgraph_node *n = cgraph_function_or_thunk_node (node, NULL);
65 /* FIXME: thunks can be considered local, but we need prevent i386
66 from attempting to change calling convention of them. */
67 if (n->thunk.thunk_p)
68 return false;
69 return !cgraph_for_node_and_aliases (n,
70 cgraph_non_local_node_p_1, NULL, true);
74 /* Return true when NODE has ADDR reference. */
76 static bool
77 has_addr_references_p (struct cgraph_node *node,
78 void *data ATTRIBUTE_UNUSED)
80 int i;
81 struct ipa_ref *ref;
83 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list,
84 i, ref); i++)
85 if (ref->use == IPA_REF_ADDR)
86 return true;
87 return false;
90 /* Look for all functions inlined to NODE and update their inlined_to pointers
91 to INLINED_TO. */
93 static void
94 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
96 struct cgraph_edge *e;
97 for (e = node->callees; e; e = e->next_callee)
98 if (e->callee->global.inlined_to)
100 e->callee->global.inlined_to = inlined_to;
101 update_inlined_to_pointer (e->callee, inlined_to);
105 /* Add symtab NODE to queue starting at FIRST.
107 The queue is linked via AUX pointers and terminated by pointer to 1.
108 We enqueue nodes at two occasions: when we find them reachable or when we find
109 their bodies needed for further clonning. In the second case we mark them
110 by pointer to 2 after processing so they are re-queue when they become
111 reachable. */
113 static void
114 enqueue_node (symtab_node node, symtab_node *first,
115 struct pointer_set_t *reachable)
117 /* Node is still in queue; do nothing. */
118 if (node->symbol.aux && node->symbol.aux != (void *) 2)
119 return;
120 /* Node was already processed as unreachable, re-enqueue
121 only if it became reachable now. */
122 if (node->symbol.aux == (void *)2 && !pointer_set_contains (reachable, node))
123 return;
124 node->symbol.aux = *first;
125 *first = node;
128 /* Process references. */
130 static void
131 process_references (struct ipa_ref_list *list,
132 symtab_node *first,
133 bool before_inlining_p,
134 struct pointer_set_t *reachable)
136 int i;
137 struct ipa_ref *ref;
138 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
140 symtab_node node = ref->referred;
142 if (node->symbol.definition && !node->symbol.in_other_partition
143 && ((!DECL_EXTERNAL (node->symbol.decl) || node->symbol.alias)
144 || (before_inlining_p
145 /* We use variable constructors during late complation for
146 constant folding. Keep references alive so partitioning
147 knows about potential references. */
148 || (TREE_CODE (node->symbol.decl) == VAR_DECL
149 && flag_wpa
150 && ctor_for_folding (node->symbol.decl)
151 != error_mark_node))))
152 pointer_set_insert (reachable, node);
153 enqueue_node ((symtab_node) node, first, reachable);
158 /* Perform reachability analysis and reclaim all unreachable nodes.
160 The algorithm is basically mark&sweep but with some extra refinements:
162 - reachable extern inline functions needs special handling; the bodies needs
163 to stay in memory until inlining in hope that they will be inlined.
164 After inlining we release their bodies and turn them into unanalyzed
165 nodes even when they are reachable.
167 BEFORE_INLINING_P specify whether we are before or after inlining.
169 - virtual functions are kept in callgraph even if they seem unreachable in
170 hope calls to them will be devirtualized.
172 Again we remove them after inlining. In late optimization some
173 devirtualization may happen, but it is not importnat since we won't inline
174 the call. In theory early opts and IPA should work out all important cases.
176 - virtual clones needs bodies of their origins for later materialization;
177 this means that we want to keep the body even if the origin is unreachable
178 otherwise. To avoid origin from sitting in the callgraph and being
179 walked by IPA passes, we turn them into unanalyzed nodes with body
180 defined.
182 We maintain set of function declaration where body needs to stay in
183 body_needed_for_clonning
185 Inline clones represent special case: their declaration match the
186 declaration of origin and cgraph_remove_node already knows how to
187 reshape callgraph and preserve body when offline copy of function or
188 inline clone is being removed.
190 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
191 variables with DECL_INITIAL set. We finalize these and keep reachable
192 ones around for constant folding purposes. After inlining we however
193 stop walking their references to let everything static referneced by them
194 to be removed when it is otherwise unreachable.
196 We maintain queue of both reachable symbols (i.e. defined symbols that needs
197 to stay) and symbols that are in boundary (i.e. external symbols referenced
198 by reachable symbols or origins of clones). The queue is represented
199 as linked list by AUX pointer terminated by 1.
201 A the end we keep all reachable symbols. For symbols in boundary we always
202 turn definition into a declaration, but we may keep function body around
203 based on body_needed_for_clonning
205 All symbols that enter the queue have AUX pointer non-zero and are in the
206 boundary. Pointer set REACHABLE is used to track reachable symbols.
208 Every symbol can be visited twice - once as part of boundary and once
209 as real reachable symbol. enqueue_node needs to decide whether the
210 node needs to be re-queued for second processing. For this purpose
211 we set AUX pointer of processed symbols in the boundary to constant 2. */
213 bool
214 symtab_remove_unreachable_nodes (bool before_inlining_p, FILE *file)
216 symtab_node first = (symtab_node) (void *) 1;
217 struct cgraph_node *node, *next;
218 struct varpool_node *vnode, *vnext;
219 bool changed = false;
220 struct pointer_set_t *reachable = pointer_set_create ();
221 struct pointer_set_t *body_needed_for_clonning = pointer_set_create ();
223 #ifdef ENABLE_CHECKING
224 verify_symtab ();
225 #endif
226 if (file)
227 fprintf (file, "\nReclaiming functions:");
228 #ifdef ENABLE_CHECKING
229 FOR_EACH_FUNCTION (node)
230 gcc_assert (!node->symbol.aux);
231 FOR_EACH_VARIABLE (vnode)
232 gcc_assert (!vnode->symbol.aux);
233 #endif
234 /* Mark functions whose bodies are obviously needed.
235 This is mostly when they can be referenced externally. Inline clones
236 are special since their declarations are shared with master clone and thus
237 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
238 FOR_EACH_FUNCTION (node)
240 node->used_as_abstract_origin = false;
241 if (node->symbol.definition
242 && !node->global.inlined_to
243 && !node->symbol.in_other_partition
244 && (!cgraph_can_remove_if_no_direct_calls_and_refs_p (node)
245 /* Keep around virtual functions for possible devirtualization. */
246 || (before_inlining_p
247 && DECL_VIRTUAL_P (node->symbol.decl))))
249 gcc_assert (!node->global.inlined_to);
250 pointer_set_insert (reachable, node);
251 enqueue_node ((symtab_node)node, &first, reachable);
253 else
254 gcc_assert (!node->symbol.aux);
257 /* Mark variables that are obviously needed. */
258 FOR_EACH_DEFINED_VARIABLE (vnode)
259 if (!varpool_can_remove_if_no_refs (vnode)
260 && !vnode->symbol.in_other_partition)
262 pointer_set_insert (reachable, vnode);
263 enqueue_node ((symtab_node)vnode, &first, reachable);
266 /* Perform reachability analysis. */
267 while (first != (symtab_node) (void *) 1)
269 bool in_boundary_p = !pointer_set_contains (reachable, first);
270 symtab_node node = first;
272 first = (symtab_node)first->symbol.aux;
274 /* If we are processing symbol in boundary, mark its AUX pointer for
275 possible later re-processing in enqueue_node. */
276 if (in_boundary_p)
277 node->symbol.aux = (void *)2;
278 else
280 if (DECL_ABSTRACT_ORIGIN (node->symbol.decl))
282 struct cgraph_node *origin_node
283 = cgraph_get_create_real_symbol_node (DECL_ABSTRACT_ORIGIN (node->symbol.decl));
284 origin_node->used_as_abstract_origin = true;
285 enqueue_node ((symtab_node) origin_node, &first, reachable);
287 /* If any symbol in a comdat group is reachable, force
288 all other in the same comdat group to be also reachable. */
289 if (node->symbol.same_comdat_group)
291 symtab_node next;
292 for (next = node->symbol.same_comdat_group;
293 next != node;
294 next = next->symbol.same_comdat_group)
295 if (!pointer_set_insert (reachable, next))
296 enqueue_node ((symtab_node) next, &first, reachable);
298 /* Mark references as reachable. */
299 process_references (&node->symbol.ref_list, &first,
300 before_inlining_p, reachable);
303 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
305 /* Mark the callees reachable unless they are direct calls to extern
306 inline functions we decided to not inline. */
307 if (!in_boundary_p)
309 struct cgraph_edge *e;
310 for (e = cnode->callees; e; e = e->next_callee)
312 if (e->callee->symbol.definition
313 && !e->callee->symbol.in_other_partition
314 && (!e->inline_failed
315 || !DECL_EXTERNAL (e->callee->symbol.decl)
316 || e->callee->symbol.alias
317 || before_inlining_p))
318 pointer_set_insert (reachable, e->callee);
319 enqueue_node ((symtab_node) e->callee, &first, reachable);
322 /* When inline clone exists, mark body to be preserved so when removing
323 offline copy of the function we don't kill it. */
324 if (cnode->global.inlined_to)
325 pointer_set_insert (body_needed_for_clonning, cnode->symbol.decl);
327 /* For non-inline clones, force their origins to the boundary and ensure
328 that body is not removed. */
329 while (cnode->clone_of)
331 bool noninline = cnode->clone_of->symbol.decl != cnode->symbol.decl;
332 cnode = cnode->clone_of;
333 if (noninline)
335 pointer_set_insert (body_needed_for_clonning, cnode->symbol.decl);
336 enqueue_node ((symtab_node)cnode, &first, reachable);
341 /* When we see constructor of external variable, keep referred nodes in the
342 boundary. This will also hold initializers of the external vars NODE
343 refers to. */
344 varpool_node *vnode = dyn_cast <varpool_node> (node);
345 if (vnode
346 && DECL_EXTERNAL (node->symbol.decl)
347 && !vnode->symbol.alias
348 && in_boundary_p)
350 struct ipa_ref *ref;
351 for (int i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
352 enqueue_node (ref->referred, &first, reachable);
356 /* Remove unreachable functions. */
357 for (node = cgraph_first_function (); node; node = next)
359 next = cgraph_next_function (node);
361 /* If node is not needed at all, remove it. */
362 if (!node->symbol.aux)
364 if (file)
365 fprintf (file, " %s", cgraph_node_name (node));
366 cgraph_remove_node (node);
367 changed = true;
369 /* If node is unreachable, remove its body. */
370 else if (!pointer_set_contains (reachable, node))
372 if (!pointer_set_contains (body_needed_for_clonning, node->symbol.decl))
373 cgraph_release_function_body (node);
374 else if (!node->clone_of)
375 gcc_assert (in_lto_p || DECL_RESULT (node->symbol.decl));
376 if (node->symbol.definition)
378 if (file)
379 fprintf (file, " %s", cgraph_node_name (node));
380 node->symbol.analyzed = false;
381 node->symbol.definition = false;
382 node->symbol.cpp_implicit_alias = false;
383 node->symbol.alias = false;
384 node->symbol.weakref = false;
385 if (!node->symbol.in_other_partition)
386 node->local.local = false;
387 cgraph_node_remove_callees (node);
388 ipa_remove_all_references (&node->symbol.ref_list);
389 changed = true;
392 else
393 gcc_assert (node->clone_of || !cgraph_function_with_gimple_body_p (node)
394 || in_lto_p || DECL_RESULT (node->symbol.decl));
397 /* Inline clones might be kept around so their materializing allows further
398 cloning. If the function the clone is inlined into is removed, we need
399 to turn it into normal cone. */
400 FOR_EACH_FUNCTION (node)
402 if (node->global.inlined_to
403 && !node->callers)
405 gcc_assert (node->clones);
406 node->global.inlined_to = NULL;
407 update_inlined_to_pointer (node, node);
409 node->symbol.aux = NULL;
412 /* Remove unreachable variables. */
413 if (file)
414 fprintf (file, "\nReclaiming variables:");
415 for (vnode = varpool_first_variable (); vnode; vnode = vnext)
417 vnext = varpool_next_variable (vnode);
418 if (!vnode->symbol.aux
419 /* For can_refer_decl_in_current_unit_p we want to track for
420 all external variables if they are defined in other partition
421 or not. */
422 && (!flag_ltrans || !DECL_EXTERNAL (vnode->symbol.decl)))
424 if (file)
425 fprintf (file, " %s", varpool_node_name (vnode));
426 varpool_remove_node (vnode);
427 changed = true;
429 else if (!pointer_set_contains (reachable, vnode))
431 tree init;
432 if (vnode->symbol.definition)
434 if (file)
435 fprintf (file, " %s", varpool_node_name (vnode));
436 changed = true;
438 vnode->symbol.definition = false;
439 vnode->symbol.analyzed = false;
440 vnode->symbol.aux = NULL;
442 /* Keep body if it may be useful for constant folding. */
443 if ((init = ctor_for_folding (vnode->symbol.decl)) == error_mark_node)
444 varpool_remove_initializer (vnode);
445 else
446 DECL_INITIAL (vnode->symbol.decl) = init;
447 ipa_remove_all_references (&vnode->symbol.ref_list);
449 else
450 vnode->symbol.aux = NULL;
453 pointer_set_destroy (reachable);
454 pointer_set_destroy (body_needed_for_clonning);
456 /* Now update address_taken flags and try to promote functions to be local. */
457 if (file)
458 fprintf (file, "\nClearing address taken flags:");
459 FOR_EACH_DEFINED_FUNCTION (node)
460 if (node->symbol.address_taken
461 && !node->symbol.used_from_other_partition)
463 if (!cgraph_for_node_and_aliases (node, has_addr_references_p, NULL, true))
465 if (file)
466 fprintf (file, " %s", cgraph_node_name (node));
467 node->symbol.address_taken = false;
468 changed = true;
469 if (cgraph_local_node_p (node))
471 node->local.local = true;
472 if (file)
473 fprintf (file, " (local)");
477 if (file)
478 fprintf (file, "\n");
480 #ifdef ENABLE_CHECKING
481 verify_symtab ();
482 #endif
484 /* If we removed something, perhaps profile could be improved. */
485 if (changed && optimize && inline_edge_summary_vec.exists ())
486 FOR_EACH_DEFINED_FUNCTION (node)
487 cgraph_propagate_frequency (node);
489 return changed;
492 /* Discover variables that have no longer address taken or that are read only
493 and update their flags.
495 FIXME: This can not be done in between gimplify and omp_expand since
496 readonly flag plays role on what is shared and what is not. Currently we do
497 this transformation as part of whole program visibility and re-do at
498 ipa-reference pass (to take into account clonning), but it would
499 make sense to do it before early optimizations. */
501 void
502 ipa_discover_readonly_nonaddressable_vars (void)
504 struct varpool_node *vnode;
505 if (dump_file)
506 fprintf (dump_file, "Clearing variable flags:");
507 FOR_EACH_VARIABLE (vnode)
508 if (vnode->symbol.definition && varpool_all_refs_explicit_p (vnode)
509 && (TREE_ADDRESSABLE (vnode->symbol.decl)
510 || !TREE_READONLY (vnode->symbol.decl)))
512 bool written = false;
513 bool address_taken = false;
514 int i;
515 struct ipa_ref *ref;
516 for (i = 0; ipa_ref_list_referring_iterate (&vnode->symbol.ref_list,
517 i, ref)
518 && (!written || !address_taken); i++)
519 switch (ref->use)
521 case IPA_REF_ADDR:
522 address_taken = true;
523 break;
524 case IPA_REF_LOAD:
525 break;
526 case IPA_REF_STORE:
527 written = true;
528 break;
530 if (TREE_ADDRESSABLE (vnode->symbol.decl) && !address_taken)
532 if (dump_file)
533 fprintf (dump_file, " %s (addressable)", varpool_node_name (vnode));
534 TREE_ADDRESSABLE (vnode->symbol.decl) = 0;
536 if (!TREE_READONLY (vnode->symbol.decl) && !address_taken && !written
537 /* Making variable in explicit section readonly can cause section
538 type conflict.
539 See e.g. gcc.c-torture/compile/pr23237.c */
540 && DECL_SECTION_NAME (vnode->symbol.decl) == NULL)
542 if (dump_file)
543 fprintf (dump_file, " %s (read-only)", varpool_node_name (vnode));
544 TREE_READONLY (vnode->symbol.decl) = 1;
547 if (dump_file)
548 fprintf (dump_file, "\n");
551 /* Return true when there is a reference to node and it is not vtable. */
552 static bool
553 address_taken_from_non_vtable_p (symtab_node node)
555 int i;
556 struct ipa_ref *ref;
557 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list,
558 i, ref); i++)
559 if (ref->use == IPA_REF_ADDR)
561 struct varpool_node *node;
562 if (is_a <cgraph_node> (ref->referring))
563 return true;
564 node = ipa_ref_referring_varpool_node (ref);
565 if (!DECL_VIRTUAL_P (node->symbol.decl))
566 return true;
568 return false;
571 /* A helper for comdat_can_be_unshared_p. */
573 static bool
574 comdat_can_be_unshared_p_1 (symtab_node node)
576 /* When address is taken, we don't know if equality comparison won't
577 break eventually. Exception are virutal functions and vtables,
578 where this is not possible by language standard. */
579 if (!DECL_VIRTUAL_P (node->symbol.decl)
580 && address_taken_from_non_vtable_p (node))
581 return false;
583 /* If the symbol is used in some weird way, better to not touch it. */
584 if (node->symbol.force_output)
585 return false;
587 /* Explicit instantiations needs to be output when possibly
588 used externally. */
589 if (node->symbol.forced_by_abi
590 && TREE_PUBLIC (node->symbol.decl)
591 && (node->symbol.resolution != LDPR_PREVAILING_DEF_IRONLY
592 && !flag_whole_program))
593 return false;
595 /* Non-readonly and volatile variables can not be duplicated. */
596 if (is_a <varpool_node> (node)
597 && (!TREE_READONLY (node->symbol.decl)
598 || TREE_THIS_VOLATILE (node->symbol.decl)))
599 return false;
600 return true;
603 /* COMDAT functions must be shared only if they have address taken,
604 otherwise we can produce our own private implementation with
605 -fwhole-program.
606 Return true when turning COMDAT functoin static can not lead to wrong
607 code when the resulting object links with a library defining same COMDAT.
609 Virtual functions do have their addresses taken from the vtables,
610 but in C++ there is no way to compare their addresses for equality. */
612 static bool
613 comdat_can_be_unshared_p (symtab_node node)
615 if (!comdat_can_be_unshared_p_1 (node))
616 return false;
617 if (node->symbol.same_comdat_group)
619 symtab_node next;
621 /* If more than one function is in the same COMDAT group, it must
622 be shared even if just one function in the comdat group has
623 address taken. */
624 for (next = node->symbol.same_comdat_group;
625 next != node; next = next->symbol.same_comdat_group)
626 if (!comdat_can_be_unshared_p_1 (next))
627 return false;
629 return true;
632 /* Return true when function NODE should be considered externally visible. */
634 static bool
635 cgraph_externally_visible_p (struct cgraph_node *node,
636 bool whole_program)
638 if (!node->symbol.definition)
639 return false;
640 if (!TREE_PUBLIC (node->symbol.decl)
641 || DECL_EXTERNAL (node->symbol.decl))
642 return false;
644 /* Do not try to localize built-in functions yet. One of problems is that we
645 end up mangling their asm for WHOPR that makes it impossible to call them
646 using the implicit built-in declarations anymore. Similarly this enables
647 us to remove them as unreachable before actual calls may appear during
648 expansion or folding. */
649 if (DECL_BUILT_IN (node->symbol.decl))
650 return true;
652 /* If linker counts on us, we must preserve the function. */
653 if (symtab_used_from_object_file_p ((symtab_node) node))
654 return true;
655 if (DECL_PRESERVE_P (node->symbol.decl))
656 return true;
657 if (lookup_attribute ("externally_visible",
658 DECL_ATTRIBUTES (node->symbol.decl)))
659 return true;
660 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
661 && lookup_attribute ("dllexport",
662 DECL_ATTRIBUTES (node->symbol.decl)))
663 return true;
664 if (node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY)
665 return false;
666 /* When doing LTO or whole program, we can bring COMDAT functoins static.
667 This improves code quality and we know we will duplicate them at most twice
668 (in the case that we are not using plugin and link with object file
669 implementing same COMDAT) */
670 if ((in_lto_p || whole_program)
671 && DECL_COMDAT (node->symbol.decl)
672 && comdat_can_be_unshared_p ((symtab_node) node))
673 return false;
675 /* When doing link time optimizations, hidden symbols become local. */
676 if (in_lto_p
677 && (DECL_VISIBILITY (node->symbol.decl) == VISIBILITY_HIDDEN
678 || DECL_VISIBILITY (node->symbol.decl) == VISIBILITY_INTERNAL)
679 /* Be sure that node is defined in IR file, not in other object
680 file. In that case we don't set used_from_other_object_file. */
681 && node->symbol.definition)
683 else if (!whole_program)
684 return true;
686 if (MAIN_NAME_P (DECL_NAME (node->symbol.decl)))
687 return true;
689 return false;
692 /* Return true when variable VNODE should be considered externally visible. */
694 bool
695 varpool_externally_visible_p (struct varpool_node *vnode)
697 if (DECL_EXTERNAL (vnode->symbol.decl))
698 return true;
700 if (!TREE_PUBLIC (vnode->symbol.decl))
701 return false;
703 /* If linker counts on us, we must preserve the function. */
704 if (symtab_used_from_object_file_p ((symtab_node) vnode))
705 return true;
707 if (DECL_HARD_REGISTER (vnode->symbol.decl))
708 return true;
709 if (DECL_PRESERVE_P (vnode->symbol.decl))
710 return true;
711 if (lookup_attribute ("externally_visible",
712 DECL_ATTRIBUTES (vnode->symbol.decl)))
713 return true;
714 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
715 && lookup_attribute ("dllexport",
716 DECL_ATTRIBUTES (vnode->symbol.decl)))
717 return true;
719 /* See if we have linker information about symbol not being used or
720 if we need to make guess based on the declaration.
722 Even if the linker clams the symbol is unused, never bring internal
723 symbols that are declared by user as used or externally visible.
724 This is needed for i.e. references from asm statements. */
725 if (symtab_used_from_object_file_p ((symtab_node) vnode))
726 return true;
727 if (vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY)
728 return false;
730 /* As a special case, the COMDAT virtual tables can be unshared.
731 In LTO mode turn vtables into static variables. The variable is readonly,
732 so this does not enable more optimization, but referring static var
733 is faster for dynamic linking. Also this match logic hidding vtables
734 from LTO symbol tables. */
735 if ((in_lto_p || flag_whole_program)
736 && DECL_COMDAT (vnode->symbol.decl)
737 && comdat_can_be_unshared_p ((symtab_node) vnode))
738 return false;
740 /* When doing link time optimizations, hidden symbols become local. */
741 if (in_lto_p
742 && (DECL_VISIBILITY (vnode->symbol.decl) == VISIBILITY_HIDDEN
743 || DECL_VISIBILITY (vnode->symbol.decl) == VISIBILITY_INTERNAL)
744 /* Be sure that node is defined in IR file, not in other object
745 file. In that case we don't set used_from_other_object_file. */
746 && vnode->symbol.definition)
748 else if (!flag_whole_program)
749 return true;
751 /* Do not attempt to privatize COMDATS by default.
752 This would break linking with C++ libraries sharing
753 inline definitions.
755 FIXME: We can do so for readonly vars with no address taken and
756 possibly also for vtables since no direct pointer comparsion is done.
757 It might be interesting to do so to reduce linking overhead. */
758 if (DECL_COMDAT (vnode->symbol.decl) || DECL_WEAK (vnode->symbol.decl))
759 return true;
760 return false;
763 /* Return true if reference to NODE can be replaced by a local alias.
764 Local aliases save dynamic linking overhead and enable more optimizations.
767 bool
768 can_replace_by_local_alias (symtab_node node)
770 return (symtab_node_availability (node) > AVAIL_OVERWRITABLE
771 && !DECL_EXTERNAL (node->symbol.decl)
772 && (!DECL_ONE_ONLY (node->symbol.decl)
773 || node->symbol.resolution == LDPR_PREVAILING_DEF
774 || node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
775 || node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP));
778 /* Mark visibility of all functions.
780 A local function is one whose calls can occur only in the current
781 compilation unit and all its calls are explicit, so we can change
782 its calling convention. We simply mark all static functions whose
783 address is not taken as local.
785 We also change the TREE_PUBLIC flag of all declarations that are public
786 in language point of view but we want to overwrite this default
787 via visibilities for the backend point of view. */
789 static unsigned int
790 function_and_variable_visibility (bool whole_program)
792 struct cgraph_node *node;
793 struct varpool_node *vnode;
795 /* All aliases should be procssed at this point. */
796 gcc_checking_assert (!alias_pairs || !alias_pairs->length());
798 FOR_EACH_FUNCTION (node)
800 int flags = flags_from_decl_or_type (node->symbol.decl);
802 /* Optimize away PURE and CONST constructors and destructors. */
803 if (optimize
804 && (flags & (ECF_CONST | ECF_PURE))
805 && !(flags & ECF_LOOPING_CONST_OR_PURE))
807 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
808 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
811 /* Frontends and alias code marks nodes as needed before parsing is finished.
812 We may end up marking as node external nodes where this flag is meaningless
813 strip it. */
814 if (DECL_EXTERNAL (node->symbol.decl) || !node->symbol.definition)
816 node->symbol.force_output = 0;
817 node->symbol.forced_by_abi = 0;
820 /* C++ FE on lack of COMDAT support create local COMDAT functions
821 (that ought to be shared but can not due to object format
822 limitations). It is necessary to keep the flag to make rest of C++ FE
823 happy. Clear the flag here to avoid confusion in middle-end. */
824 if (DECL_COMDAT (node->symbol.decl) && !TREE_PUBLIC (node->symbol.decl))
825 DECL_COMDAT (node->symbol.decl) = 0;
827 /* For external decls stop tracking same_comdat_group. It doesn't matter
828 what comdat group they are in when they won't be emitted in this TU. */
829 if (node->symbol.same_comdat_group && DECL_EXTERNAL (node->symbol.decl))
831 #ifdef ENABLE_CHECKING
832 symtab_node n;
834 for (n = node->symbol.same_comdat_group;
835 n != (symtab_node)node;
836 n = n->symbol.same_comdat_group)
837 /* If at least one of same comdat group functions is external,
838 all of them have to be, otherwise it is a front-end bug. */
839 gcc_assert (DECL_EXTERNAL (n->symbol.decl));
840 #endif
841 symtab_dissolve_same_comdat_group_list ((symtab_node) node);
843 gcc_assert ((!DECL_WEAK (node->symbol.decl)
844 && !DECL_COMDAT (node->symbol.decl))
845 || TREE_PUBLIC (node->symbol.decl)
846 || node->symbol.weakref
847 || DECL_EXTERNAL (node->symbol.decl));
848 if (cgraph_externally_visible_p (node, whole_program))
850 gcc_assert (!node->global.inlined_to);
851 node->symbol.externally_visible = true;
853 else
855 node->symbol.externally_visible = false;
856 node->symbol.forced_by_abi = false;
858 if (!node->symbol.externally_visible
859 && node->symbol.definition && !node->symbol.weakref
860 && !DECL_EXTERNAL (node->symbol.decl))
862 gcc_assert (whole_program || in_lto_p
863 || !TREE_PUBLIC (node->symbol.decl));
864 node->symbol.unique_name = ((node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
865 || node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
866 && TREE_PUBLIC (node->symbol.decl));
867 symtab_make_decl_local (node->symbol.decl);
868 node->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
869 if (node->symbol.same_comdat_group)
870 /* cgraph_externally_visible_p has already checked all other nodes
871 in the group and they will all be made local. We need to
872 dissolve the group at once so that the predicate does not
873 segfault though. */
874 symtab_dissolve_same_comdat_group_list ((symtab_node) node);
877 if (node->thunk.thunk_p
878 && TREE_PUBLIC (node->symbol.decl))
880 struct cgraph_node *decl_node = node;
882 decl_node = cgraph_function_node (decl_node->callees->callee, NULL);
884 /* Thunks have the same visibility as function they are attached to.
885 Make sure the C++ front end set this up properly. */
886 if (DECL_ONE_ONLY (decl_node->symbol.decl))
888 gcc_checking_assert (DECL_COMDAT (node->symbol.decl)
889 == DECL_COMDAT (decl_node->symbol.decl));
890 gcc_checking_assert (DECL_COMDAT_GROUP (node->symbol.decl)
891 == DECL_COMDAT_GROUP (decl_node->symbol.decl));
892 gcc_checking_assert (node->symbol.same_comdat_group);
894 if (DECL_EXTERNAL (decl_node->symbol.decl))
895 DECL_EXTERNAL (node->symbol.decl) = 1;
898 FOR_EACH_DEFINED_FUNCTION (node)
900 node->local.local |= cgraph_local_node_p (node);
902 /* If we know that function can not be overwritten by a different semantics
903 and moreover its section can not be discarded, replace all direct calls
904 by calls to an nonoverwritable alias. This make dynamic linking
905 cheaper and enable more optimization.
907 TODO: We can also update virtual tables. */
908 if (node->callers && can_replace_by_local_alias ((symtab_node)node))
910 struct cgraph_node *alias = cgraph (symtab_nonoverwritable_alias ((symtab_node) node));
912 if (alias != node)
914 while (node->callers)
916 struct cgraph_edge *e = node->callers;
918 cgraph_redirect_edge_callee (e, alias);
919 if (gimple_has_body_p (e->caller->symbol.decl))
921 push_cfun (DECL_STRUCT_FUNCTION (e->caller->symbol.decl));
922 cgraph_redirect_edge_call_stmt_to_callee (e);
923 pop_cfun ();
929 FOR_EACH_VARIABLE (vnode)
931 /* weak flag makes no sense on local variables. */
932 gcc_assert (!DECL_WEAK (vnode->symbol.decl)
933 || vnode->symbol.weakref
934 || TREE_PUBLIC (vnode->symbol.decl)
935 || DECL_EXTERNAL (vnode->symbol.decl));
936 /* In several cases declarations can not be common:
938 - when declaration has initializer
939 - when it is in weak
940 - when it has specific section
941 - when it resides in non-generic address space.
942 - if declaration is local, it will get into .local common section
943 so common flag is not needed. Frontends still produce these in
944 certain cases, such as for:
946 static int a __attribute__ ((common))
948 Canonicalize things here and clear the redundant flag. */
949 if (DECL_COMMON (vnode->symbol.decl)
950 && (!(TREE_PUBLIC (vnode->symbol.decl)
951 || DECL_EXTERNAL (vnode->symbol.decl))
952 || (DECL_INITIAL (vnode->symbol.decl)
953 && DECL_INITIAL (vnode->symbol.decl) != error_mark_node)
954 || DECL_WEAK (vnode->symbol.decl)
955 || DECL_SECTION_NAME (vnode->symbol.decl) != NULL
956 || ! (ADDR_SPACE_GENERIC_P
957 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->symbol.decl))))))
958 DECL_COMMON (vnode->symbol.decl) = 0;
960 FOR_EACH_DEFINED_VARIABLE (vnode)
962 if (!vnode->symbol.definition)
963 continue;
964 if (varpool_externally_visible_p (vnode))
965 vnode->symbol.externally_visible = true;
966 else
968 vnode->symbol.externally_visible = false;
969 vnode->symbol.forced_by_abi = false;
971 if (!vnode->symbol.externally_visible
972 && !vnode->symbol.weakref)
974 gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->symbol.decl));
975 symtab_make_decl_local (vnode->symbol.decl);
976 vnode->symbol.unique_name = ((vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
977 || vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
978 && TREE_PUBLIC (vnode->symbol.decl));
979 if (vnode->symbol.same_comdat_group)
980 symtab_dissolve_same_comdat_group_list ((symtab_node) vnode);
981 vnode->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
985 if (dump_file)
987 fprintf (dump_file, "\nMarking local functions:");
988 FOR_EACH_DEFINED_FUNCTION (node)
989 if (node->local.local)
990 fprintf (dump_file, " %s", cgraph_node_name (node));
991 fprintf (dump_file, "\n\n");
992 fprintf (dump_file, "\nMarking externally visible functions:");
993 FOR_EACH_DEFINED_FUNCTION (node)
994 if (node->symbol.externally_visible)
995 fprintf (dump_file, " %s", cgraph_node_name (node));
996 fprintf (dump_file, "\n\n");
997 fprintf (dump_file, "\nMarking externally visible variables:");
998 FOR_EACH_DEFINED_VARIABLE (vnode)
999 if (vnode->symbol.externally_visible)
1000 fprintf (dump_file, " %s", varpool_node_name (vnode));
1001 fprintf (dump_file, "\n\n");
1003 cgraph_function_flags_ready = true;
1004 return 0;
1007 /* Local function pass handling visibilities. This happens before LTO streaming
1008 so in particular -fwhole-program should be ignored at this level. */
1010 static unsigned int
1011 local_function_and_variable_visibility (void)
1013 return function_and_variable_visibility (flag_whole_program && !flag_lto);
1016 namespace {
1018 const pass_data pass_data_ipa_function_and_variable_visibility =
1020 SIMPLE_IPA_PASS, /* type */
1021 "visibility", /* name */
1022 OPTGROUP_NONE, /* optinfo_flags */
1023 false, /* has_gate */
1024 true, /* has_execute */
1025 TV_CGRAPHOPT, /* tv_id */
1026 0, /* properties_required */
1027 0, /* properties_provided */
1028 0, /* properties_destroyed */
1029 0, /* todo_flags_start */
1030 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1033 class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
1035 public:
1036 pass_ipa_function_and_variable_visibility(gcc::context *ctxt)
1037 : simple_ipa_opt_pass(pass_data_ipa_function_and_variable_visibility, ctxt)
1040 /* opt_pass methods: */
1041 unsigned int execute () {
1042 return local_function_and_variable_visibility ();
1045 }; // class pass_ipa_function_and_variable_visibility
1047 } // anon namespace
1049 simple_ipa_opt_pass *
1050 make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1052 return new pass_ipa_function_and_variable_visibility (ctxt);
1055 /* Free inline summary. */
1057 static unsigned
1058 free_inline_summary (void)
1060 inline_free_summary ();
1061 return 0;
1064 namespace {
1066 const pass_data pass_data_ipa_free_inline_summary =
1068 SIMPLE_IPA_PASS, /* type */
1069 "*free_inline_summary", /* name */
1070 OPTGROUP_NONE, /* optinfo_flags */
1071 false, /* has_gate */
1072 true, /* has_execute */
1073 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
1074 0, /* properties_required */
1075 0, /* properties_provided */
1076 0, /* properties_destroyed */
1077 0, /* todo_flags_start */
1078 0, /* todo_flags_finish */
1081 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
1083 public:
1084 pass_ipa_free_inline_summary(gcc::context *ctxt)
1085 : simple_ipa_opt_pass(pass_data_ipa_free_inline_summary, ctxt)
1088 /* opt_pass methods: */
1089 unsigned int execute () { return free_inline_summary (); }
1091 }; // class pass_ipa_free_inline_summary
1093 } // anon namespace
1095 simple_ipa_opt_pass *
1096 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
1098 return new pass_ipa_free_inline_summary (ctxt);
1101 /* Do not re-run on ltrans stage. */
1103 static bool
1104 gate_whole_program_function_and_variable_visibility (void)
1106 return !flag_ltrans;
1109 /* Bring functionss local at LTO time with -fwhole-program. */
1111 static unsigned int
1112 whole_program_function_and_variable_visibility (void)
1114 function_and_variable_visibility (flag_whole_program);
1115 if (optimize)
1116 ipa_discover_readonly_nonaddressable_vars ();
1117 return 0;
1120 namespace {
1122 const pass_data pass_data_ipa_whole_program_visibility =
1124 IPA_PASS, /* type */
1125 "whole-program", /* name */
1126 OPTGROUP_NONE, /* optinfo_flags */
1127 true, /* has_gate */
1128 true, /* has_execute */
1129 TV_CGRAPHOPT, /* tv_id */
1130 0, /* properties_required */
1131 0, /* properties_provided */
1132 0, /* properties_destroyed */
1133 0, /* todo_flags_start */
1134 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1137 class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
1139 public:
1140 pass_ipa_whole_program_visibility(gcc::context *ctxt)
1141 : ipa_opt_pass_d(pass_data_ipa_whole_program_visibility, ctxt,
1142 NULL, /* generate_summary */
1143 NULL, /* write_summary */
1144 NULL, /* read_summary */
1145 NULL, /* write_optimization_summary */
1146 NULL, /* read_optimization_summary */
1147 NULL, /* stmt_fixup */
1148 0, /* function_transform_todo_flags_start */
1149 NULL, /* function_transform */
1150 NULL) /* variable_transform */
1153 /* opt_pass methods: */
1154 bool gate () {
1155 return gate_whole_program_function_and_variable_visibility ();
1157 unsigned int execute () {
1158 return whole_program_function_and_variable_visibility ();
1161 }; // class pass_ipa_whole_program_visibility
1163 } // anon namespace
1165 ipa_opt_pass_d *
1166 make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
1168 return new pass_ipa_whole_program_visibility (ctxt);
1171 /* Entry in the histogram. */
1173 struct histogram_entry
1175 gcov_type count;
1176 int time;
1177 int size;
1180 /* Histogram of profile values.
1181 The histogram is represented as an ordered vector of entries allocated via
1182 histogram_pool. During construction a separate hashtable is kept to lookup
1183 duplicate entries. */
1185 vec<histogram_entry *> histogram;
1186 static alloc_pool histogram_pool;
1188 /* Hashtable support for storing SSA names hashed by their SSA_NAME_VAR. */
1190 struct histogram_hash : typed_noop_remove <histogram_entry>
1192 typedef histogram_entry value_type;
1193 typedef histogram_entry compare_type;
1194 static inline hashval_t hash (const value_type *);
1195 static inline int equal (const value_type *, const compare_type *);
1198 inline hashval_t
1199 histogram_hash::hash (const histogram_entry *val)
1201 return val->count;
1204 inline int
1205 histogram_hash::equal (const histogram_entry *val, const histogram_entry *val2)
1207 return val->count == val2->count;
1210 /* Account TIME and SIZE executed COUNT times into HISTOGRAM.
1211 HASHTABLE is the on-side hash kept to avoid duplicates. */
1213 static void
1214 account_time_size (hash_table <histogram_hash> hashtable,
1215 vec<histogram_entry *> &histogram,
1216 gcov_type count, int time, int size)
1218 histogram_entry key = {count, 0, 0};
1219 histogram_entry **val = hashtable.find_slot (&key, INSERT);
1221 if (!*val)
1223 *val = (histogram_entry *) pool_alloc (histogram_pool);
1224 **val = key;
1225 histogram.safe_push (*val);
1227 (*val)->time += time;
1228 (*val)->size += size;
1232 cmp_counts (const void *v1, const void *v2)
1234 const histogram_entry *h1 = *(const histogram_entry * const *)v1;
1235 const histogram_entry *h2 = *(const histogram_entry * const *)v2;
1236 if (h1->count < h2->count)
1237 return 1;
1238 if (h1->count > h2->count)
1239 return -1;
1240 return 0;
1243 /* Dump HISTOGRAM to FILE. */
1245 static void
1246 dump_histogram (FILE *file, vec<histogram_entry *> histogram)
1248 unsigned int i;
1249 gcov_type overall_time = 0, cumulated_time = 0, cumulated_size = 0, overall_size = 0;
1251 fprintf (dump_file, "Histogram:\n");
1252 for (i = 0; i < histogram.length (); i++)
1254 overall_time += histogram[i]->count * histogram[i]->time;
1255 overall_size += histogram[i]->size;
1257 if (!overall_time)
1258 overall_time = 1;
1259 if (!overall_size)
1260 overall_size = 1;
1261 for (i = 0; i < histogram.length (); i++)
1263 cumulated_time += histogram[i]->count * histogram[i]->time;
1264 cumulated_size += histogram[i]->size;
1265 fprintf (file, " "HOST_WIDEST_INT_PRINT_DEC": time:%i (%2.2f) size:%i (%2.2f)\n",
1266 (HOST_WIDEST_INT) histogram[i]->count,
1267 histogram[i]->time,
1268 cumulated_time * 100.0 / overall_time,
1269 histogram[i]->size,
1270 cumulated_size * 100.0 / overall_size);
1274 /* Collect histogram from CFG profiles. */
1276 static void
1277 ipa_profile_generate_summary (void)
1279 struct cgraph_node *node;
1280 gimple_stmt_iterator gsi;
1281 hash_table <histogram_hash> hashtable;
1282 basic_block bb;
1284 hashtable.create (10);
1285 histogram_pool = create_alloc_pool ("IPA histogram", sizeof (struct histogram_entry),
1286 10);
1288 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
1289 FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (node->symbol.decl))
1291 int time = 0;
1292 int size = 0;
1293 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1295 gimple stmt = gsi_stmt (gsi);
1296 if (gimple_code (stmt) == GIMPLE_CALL
1297 && !gimple_call_fndecl (stmt))
1299 histogram_value h;
1300 h = gimple_histogram_value_of_type
1301 (DECL_STRUCT_FUNCTION (node->symbol.decl),
1302 stmt, HIST_TYPE_INDIR_CALL);
1303 /* No need to do sanity check: gimple_ic_transform already
1304 takes away bad histograms. */
1305 if (h)
1307 /* counter 0 is target, counter 1 is number of execution we called target,
1308 counter 2 is total number of executions. */
1309 if (h->hvalue.counters[2])
1311 struct cgraph_edge * e = cgraph_edge (node, stmt);
1312 e->indirect_info->common_target_id
1313 = h->hvalue.counters [0];
1314 e->indirect_info->common_target_probability
1315 = GCOV_COMPUTE_SCALE (h->hvalue.counters [1], h->hvalue.counters [2]);
1316 if (e->indirect_info->common_target_probability > REG_BR_PROB_BASE)
1318 if (dump_file)
1319 fprintf (dump_file, "Probability capped to 1\n");
1320 e->indirect_info->common_target_probability = REG_BR_PROB_BASE;
1323 gimple_remove_histogram_value (DECL_STRUCT_FUNCTION (node->symbol.decl),
1324 stmt, h);
1327 time += estimate_num_insns (stmt, &eni_time_weights);
1328 size += estimate_num_insns (stmt, &eni_size_weights);
1330 account_time_size (hashtable, histogram, bb->count, time, size);
1332 hashtable.dispose ();
1333 histogram.qsort (cmp_counts);
1336 /* Serialize the ipa info for lto. */
1338 static void
1339 ipa_profile_write_summary (void)
1341 struct lto_simple_output_block *ob
1342 = lto_create_simple_output_block (LTO_section_ipa_profile);
1343 unsigned int i;
1345 streamer_write_uhwi_stream (ob->main_stream, histogram.length());
1346 for (i = 0; i < histogram.length (); i++)
1348 streamer_write_gcov_count_stream (ob->main_stream, histogram[i]->count);
1349 streamer_write_uhwi_stream (ob->main_stream, histogram[i]->time);
1350 streamer_write_uhwi_stream (ob->main_stream, histogram[i]->size);
1352 lto_destroy_simple_output_block (ob);
1355 /* Deserialize the ipa info for lto. */
1357 static void
1358 ipa_profile_read_summary (void)
1360 struct lto_file_decl_data ** file_data_vec
1361 = lto_get_file_decl_data ();
1362 struct lto_file_decl_data * file_data;
1363 hash_table <histogram_hash> hashtable;
1364 int j = 0;
1366 hashtable.create (10);
1367 histogram_pool = create_alloc_pool ("IPA histogram", sizeof (struct histogram_entry),
1368 10);
1370 while ((file_data = file_data_vec[j++]))
1372 const char *data;
1373 size_t len;
1374 struct lto_input_block *ib
1375 = lto_create_simple_input_block (file_data,
1376 LTO_section_ipa_profile,
1377 &data, &len);
1378 if (ib)
1380 unsigned int num = streamer_read_uhwi (ib);
1381 unsigned int n;
1382 for (n = 0; n < num; n++)
1384 gcov_type count = streamer_read_gcov_count (ib);
1385 int time = streamer_read_uhwi (ib);
1386 int size = streamer_read_uhwi (ib);
1387 account_time_size (hashtable, histogram,
1388 count, time, size);
1390 lto_destroy_simple_input_block (file_data,
1391 LTO_section_ipa_profile,
1392 ib, data, len);
1395 hashtable.dispose ();
1396 histogram.qsort (cmp_counts);
1399 /* Simple ipa profile pass propagating frequencies across the callgraph. */
1401 static unsigned int
1402 ipa_profile (void)
1404 struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1405 struct cgraph_edge *e;
1406 int order_pos;
1407 bool something_changed = false;
1408 int i;
1409 gcov_type overall_time = 0, cutoff = 0, cumulated = 0, overall_size = 0;
1411 /* Produce speculative calls: we saved common traget from porfiling into
1412 e->common_target_id. Now, at link time, we can look up corresponding
1413 function node and produce speculative call. */
1414 if (in_lto_p)
1416 struct cgraph_edge *e;
1417 struct cgraph_node *n,*n2;
1419 init_node_map (false);
1420 FOR_EACH_DEFINED_FUNCTION (n)
1422 bool update = false;
1424 for (e = n->indirect_calls; e; e = e->next_callee)
1425 if (e->indirect_info->common_target_id)
1427 n2 = find_func_by_profile_id (e->indirect_info->common_target_id);
1428 if (n2)
1430 if (dump_file)
1432 fprintf (dump_file, "Indirect call -> direct call from"
1433 " other module %s/%i => %s/%i, prob %3.2f\n",
1434 xstrdup (cgraph_node_name (n)), n->symbol.order,
1435 xstrdup (cgraph_node_name (n2)), n2->symbol.order,
1436 e->indirect_info->common_target_probability
1437 / (float)REG_BR_PROB_BASE);
1439 cgraph_turn_edge_to_speculative
1440 (e, n2,
1441 apply_scale (e->count,
1442 e->indirect_info->common_target_probability),
1443 apply_scale (e->frequency,
1444 e->indirect_info->common_target_probability));
1445 update = true;
1447 else
1448 if (dump_file)
1449 fprintf (dump_file, "Function with profile-id %i not found.\n",
1450 e->indirect_info->common_target_id);
1452 if (update)
1453 inline_update_overall_summary (n);
1455 del_node_map ();
1458 if (dump_file)
1459 dump_histogram (dump_file, histogram);
1460 for (i = 0; i < (int)histogram.length (); i++)
1462 overall_time += histogram[i]->count * histogram[i]->time;
1463 overall_size += histogram[i]->size;
1465 if (overall_time)
1467 gcov_type threshold;
1469 gcc_assert (overall_size);
1470 if (dump_file)
1472 gcov_type min, cumulated_time = 0, cumulated_size = 0;
1474 fprintf (dump_file, "Overall time: "HOST_WIDEST_INT_PRINT_DEC"\n",
1475 (HOST_WIDEST_INT)overall_time);
1476 min = get_hot_bb_threshold ();
1477 for (i = 0; i < (int)histogram.length () && histogram[i]->count >= min;
1478 i++)
1480 cumulated_time += histogram[i]->count * histogram[i]->time;
1481 cumulated_size += histogram[i]->size;
1483 fprintf (dump_file, "GCOV min count: "HOST_WIDEST_INT_PRINT_DEC
1484 " Time:%3.2f%% Size:%3.2f%%\n",
1485 (HOST_WIDEST_INT)min,
1486 cumulated_time * 100.0 / overall_time,
1487 cumulated_size * 100.0 / overall_size);
1489 cutoff = (overall_time * PARAM_VALUE (HOT_BB_COUNT_WS_PERMILLE) + 500) / 1000;
1490 threshold = 0;
1491 for (i = 0; cumulated < cutoff; i++)
1493 cumulated += histogram[i]->count * histogram[i]->time;
1494 threshold = histogram[i]->count;
1496 if (!threshold)
1497 threshold = 1;
1498 if (dump_file)
1500 gcov_type cumulated_time = 0, cumulated_size = 0;
1502 for (i = 0;
1503 i < (int)histogram.length () && histogram[i]->count >= threshold;
1504 i++)
1506 cumulated_time += histogram[i]->count * histogram[i]->time;
1507 cumulated_size += histogram[i]->size;
1509 fprintf (dump_file, "Determined min count: "HOST_WIDEST_INT_PRINT_DEC
1510 " Time:%3.2f%% Size:%3.2f%%\n",
1511 (HOST_WIDEST_INT)threshold,
1512 cumulated_time * 100.0 / overall_time,
1513 cumulated_size * 100.0 / overall_size);
1515 if (threshold > get_hot_bb_threshold ()
1516 || in_lto_p)
1518 if (dump_file)
1519 fprintf (dump_file, "Threshold updated.\n");
1520 set_hot_bb_threshold (threshold);
1523 histogram.release();
1524 free_alloc_pool (histogram_pool);
1526 order_pos = ipa_reverse_postorder (order);
1527 for (i = order_pos - 1; i >= 0; i--)
1529 if (order[i]->local.local && cgraph_propagate_frequency (order[i]))
1531 for (e = order[i]->callees; e; e = e->next_callee)
1532 if (e->callee->local.local && !e->callee->symbol.aux)
1534 something_changed = true;
1535 e->callee->symbol.aux = (void *)1;
1538 order[i]->symbol.aux = NULL;
1541 while (something_changed)
1543 something_changed = false;
1544 for (i = order_pos - 1; i >= 0; i--)
1546 if (order[i]->symbol.aux && cgraph_propagate_frequency (order[i]))
1548 for (e = order[i]->callees; e; e = e->next_callee)
1549 if (e->callee->local.local && !e->callee->symbol.aux)
1551 something_changed = true;
1552 e->callee->symbol.aux = (void *)1;
1555 order[i]->symbol.aux = NULL;
1558 free (order);
1559 return 0;
1562 static bool
1563 gate_ipa_profile (void)
1565 return flag_ipa_profile;
1568 namespace {
1570 const pass_data pass_data_ipa_profile =
1572 IPA_PASS, /* type */
1573 "profile_estimate", /* name */
1574 OPTGROUP_NONE, /* optinfo_flags */
1575 true, /* has_gate */
1576 true, /* has_execute */
1577 TV_IPA_PROFILE, /* tv_id */
1578 0, /* properties_required */
1579 0, /* properties_provided */
1580 0, /* properties_destroyed */
1581 0, /* todo_flags_start */
1582 0, /* todo_flags_finish */
1585 class pass_ipa_profile : public ipa_opt_pass_d
1587 public:
1588 pass_ipa_profile(gcc::context *ctxt)
1589 : ipa_opt_pass_d(pass_data_ipa_profile, ctxt,
1590 ipa_profile_generate_summary, /* generate_summary */
1591 ipa_profile_write_summary, /* write_summary */
1592 ipa_profile_read_summary, /* read_summary */
1593 NULL, /* write_optimization_summary */
1594 NULL, /* read_optimization_summary */
1595 NULL, /* stmt_fixup */
1596 0, /* function_transform_todo_flags_start */
1597 NULL, /* function_transform */
1598 NULL) /* variable_transform */
1601 /* opt_pass methods: */
1602 bool gate () { return gate_ipa_profile (); }
1603 unsigned int execute () { return ipa_profile (); }
1605 }; // class pass_ipa_profile
1607 } // anon namespace
1609 ipa_opt_pass_d *
1610 make_pass_ipa_profile (gcc::context *ctxt)
1612 return new pass_ipa_profile (ctxt);
1615 /* Generate and emit a static constructor or destructor. WHICH must
1616 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1617 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1618 initialization priority for this constructor or destructor.
1620 FINAL specify whether the externally visible name for collect2 should
1621 be produced. */
1623 static void
1624 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
1626 static int counter = 0;
1627 char which_buf[16];
1628 tree decl, name, resdecl;
1630 /* The priority is encoded in the constructor or destructor name.
1631 collect2 will sort the names and arrange that they are called at
1632 program startup. */
1633 if (final)
1634 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
1635 else
1636 /* Proudce sane name but one not recognizable by collect2, just for the
1637 case we fail to inline the function. */
1638 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
1639 name = get_file_function_name (which_buf);
1641 decl = build_decl (input_location, FUNCTION_DECL, name,
1642 build_function_type_list (void_type_node, NULL_TREE));
1643 current_function_decl = decl;
1645 resdecl = build_decl (input_location,
1646 RESULT_DECL, NULL_TREE, void_type_node);
1647 DECL_ARTIFICIAL (resdecl) = 1;
1648 DECL_RESULT (decl) = resdecl;
1649 DECL_CONTEXT (resdecl) = decl;
1651 allocate_struct_function (decl, false);
1653 TREE_STATIC (decl) = 1;
1654 TREE_USED (decl) = 1;
1655 DECL_ARTIFICIAL (decl) = 1;
1656 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1657 DECL_SAVED_TREE (decl) = body;
1658 if (!targetm.have_ctors_dtors && final)
1660 TREE_PUBLIC (decl) = 1;
1661 DECL_PRESERVE_P (decl) = 1;
1663 DECL_UNINLINABLE (decl) = 1;
1665 DECL_INITIAL (decl) = make_node (BLOCK);
1666 TREE_USED (DECL_INITIAL (decl)) = 1;
1668 DECL_SOURCE_LOCATION (decl) = input_location;
1669 cfun->function_end_locus = input_location;
1671 switch (which)
1673 case 'I':
1674 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1675 decl_init_priority_insert (decl, priority);
1676 break;
1677 case 'D':
1678 DECL_STATIC_DESTRUCTOR (decl) = 1;
1679 decl_fini_priority_insert (decl, priority);
1680 break;
1681 default:
1682 gcc_unreachable ();
1685 gimplify_function_tree (decl);
1687 cgraph_add_new_function (decl, false);
1689 set_cfun (NULL);
1690 current_function_decl = NULL;
1693 /* Generate and emit a static constructor or destructor. WHICH must
1694 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1695 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1696 initialization priority for this constructor or destructor. */
1698 void
1699 cgraph_build_static_cdtor (char which, tree body, int priority)
1701 cgraph_build_static_cdtor_1 (which, body, priority, false);
1704 /* A vector of FUNCTION_DECLs declared as static constructors. */
1705 static vec<tree> static_ctors;
1706 /* A vector of FUNCTION_DECLs declared as static destructors. */
1707 static vec<tree> static_dtors;
1709 /* When target does not have ctors and dtors, we call all constructor
1710 and destructor by special initialization/destruction function
1711 recognized by collect2.
1713 When we are going to build this function, collect all constructors and
1714 destructors and turn them into normal functions. */
1716 static void
1717 record_cdtor_fn (struct cgraph_node *node)
1719 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
1720 static_ctors.safe_push (node->symbol.decl);
1721 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1722 static_dtors.safe_push (node->symbol.decl);
1723 node = cgraph_get_node (node->symbol.decl);
1724 DECL_DISREGARD_INLINE_LIMITS (node->symbol.decl) = 1;
1727 /* Define global constructors/destructor functions for the CDTORS, of
1728 which they are LEN. The CDTORS are sorted by initialization
1729 priority. If CTOR_P is true, these are constructors; otherwise,
1730 they are destructors. */
1732 static void
1733 build_cdtor (bool ctor_p, vec<tree> cdtors)
1735 size_t i,j;
1736 size_t len = cdtors.length ();
1738 i = 0;
1739 while (i < len)
1741 tree body;
1742 tree fn;
1743 priority_type priority;
1745 priority = 0;
1746 body = NULL_TREE;
1747 j = i;
1750 priority_type p;
1751 fn = cdtors[j];
1752 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1753 if (j == i)
1754 priority = p;
1755 else if (p != priority)
1756 break;
1757 j++;
1759 while (j < len);
1761 /* When there is only one cdtor and target supports them, do nothing. */
1762 if (j == i + 1
1763 && targetm.have_ctors_dtors)
1765 i++;
1766 continue;
1768 /* Find the next batch of constructors/destructors with the same
1769 initialization priority. */
1770 for (;i < j; i++)
1772 tree call;
1773 fn = cdtors[i];
1774 call = build_call_expr (fn, 0);
1775 if (ctor_p)
1776 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1777 else
1778 DECL_STATIC_DESTRUCTOR (fn) = 0;
1779 /* We do not want to optimize away pure/const calls here.
1780 When optimizing, these should be already removed, when not
1781 optimizing, we want user to be able to breakpoint in them. */
1782 TREE_SIDE_EFFECTS (call) = 1;
1783 append_to_statement_list (call, &body);
1785 gcc_assert (body != NULL_TREE);
1786 /* Generate a function to call all the function of like
1787 priority. */
1788 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1792 /* Comparison function for qsort. P1 and P2 are actually of type
1793 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1794 used to determine the sort order. */
1796 static int
1797 compare_ctor (const void *p1, const void *p2)
1799 tree f1;
1800 tree f2;
1801 int priority1;
1802 int priority2;
1804 f1 = *(const tree *)p1;
1805 f2 = *(const tree *)p2;
1806 priority1 = DECL_INIT_PRIORITY (f1);
1807 priority2 = DECL_INIT_PRIORITY (f2);
1809 if (priority1 < priority2)
1810 return -1;
1811 else if (priority1 > priority2)
1812 return 1;
1813 else
1814 /* Ensure a stable sort. Constructors are executed in backwarding
1815 order to make LTO initialize braries first. */
1816 return DECL_UID (f2) - DECL_UID (f1);
1819 /* Comparison function for qsort. P1 and P2 are actually of type
1820 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1821 used to determine the sort order. */
1823 static int
1824 compare_dtor (const void *p1, const void *p2)
1826 tree f1;
1827 tree f2;
1828 int priority1;
1829 int priority2;
1831 f1 = *(const tree *)p1;
1832 f2 = *(const tree *)p2;
1833 priority1 = DECL_FINI_PRIORITY (f1);
1834 priority2 = DECL_FINI_PRIORITY (f2);
1836 if (priority1 < priority2)
1837 return -1;
1838 else if (priority1 > priority2)
1839 return 1;
1840 else
1841 /* Ensure a stable sort. */
1842 return DECL_UID (f1) - DECL_UID (f2);
1845 /* Generate functions to call static constructors and destructors
1846 for targets that do not support .ctors/.dtors sections. These
1847 functions have magic names which are detected by collect2. */
1849 static void
1850 build_cdtor_fns (void)
1852 if (!static_ctors.is_empty ())
1854 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1855 static_ctors.qsort (compare_ctor);
1856 build_cdtor (/*ctor_p=*/true, static_ctors);
1859 if (!static_dtors.is_empty ())
1861 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1862 static_dtors.qsort (compare_dtor);
1863 build_cdtor (/*ctor_p=*/false, static_dtors);
1867 /* Look for constructors and destructors and produce function calling them.
1868 This is needed for targets not supporting ctors or dtors, but we perform the
1869 transformation also at linktime to merge possibly numerous
1870 constructors/destructors into single function to improve code locality and
1871 reduce size. */
1873 static unsigned int
1874 ipa_cdtor_merge (void)
1876 struct cgraph_node *node;
1877 FOR_EACH_DEFINED_FUNCTION (node)
1878 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl)
1879 || DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1880 record_cdtor_fn (node);
1881 build_cdtor_fns ();
1882 static_ctors.release ();
1883 static_dtors.release ();
1884 return 0;
1887 /* Perform the pass when we have no ctors/dtors support
1888 or at LTO time to merge multiple constructors into single
1889 function. */
1891 static bool
1892 gate_ipa_cdtor_merge (void)
1894 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1897 namespace {
1899 const pass_data pass_data_ipa_cdtor_merge =
1901 IPA_PASS, /* type */
1902 "cdtor", /* name */
1903 OPTGROUP_NONE, /* optinfo_flags */
1904 true, /* has_gate */
1905 true, /* has_execute */
1906 TV_CGRAPHOPT, /* tv_id */
1907 0, /* properties_required */
1908 0, /* properties_provided */
1909 0, /* properties_destroyed */
1910 0, /* todo_flags_start */
1911 0, /* todo_flags_finish */
1914 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1916 public:
1917 pass_ipa_cdtor_merge(gcc::context *ctxt)
1918 : ipa_opt_pass_d(pass_data_ipa_cdtor_merge, ctxt,
1919 NULL, /* generate_summary */
1920 NULL, /* write_summary */
1921 NULL, /* read_summary */
1922 NULL, /* write_optimization_summary */
1923 NULL, /* read_optimization_summary */
1924 NULL, /* stmt_fixup */
1925 0, /* function_transform_todo_flags_start */
1926 NULL, /* function_transform */
1927 NULL) /* variable_transform */
1930 /* opt_pass methods: */
1931 bool gate () { return gate_ipa_cdtor_merge (); }
1932 unsigned int execute () { return ipa_cdtor_merge (); }
1934 }; // class pass_ipa_cdtor_merge
1936 } // anon namespace
1938 ipa_opt_pass_d *
1939 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1941 return new pass_ipa_cdtor_merge (ctxt);