New vectorizer messages; message format change.
[official-gcc.git] / gcc / ipa.c
blob778a88fcbdfb1ac3697999f0225b2f64053be89d
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, C++
578 constructors/destructors and vtables, where this is not possible by
579 language standard. */
580 if (!DECL_VIRTUAL_P (node->symbol.decl)
581 && (TREE_CODE (node->symbol.decl) != FUNCTION_DECL
582 || (!DECL_CXX_CONSTRUCTOR_P (node->symbol.decl)
583 && !DECL_CXX_DESTRUCTOR_P (node->symbol.decl)))
584 && address_taken_from_non_vtable_p (node))
585 return false;
587 /* If the symbol is used in some weird way, better to not touch it. */
588 if (node->symbol.force_output)
589 return false;
591 /* Explicit instantiations needs to be output when possibly
592 used externally. */
593 if (node->symbol.forced_by_abi
594 && TREE_PUBLIC (node->symbol.decl)
595 && (node->symbol.resolution != LDPR_PREVAILING_DEF_IRONLY
596 && !flag_whole_program))
597 return false;
599 /* Non-readonly and volatile variables can not be duplicated. */
600 if (is_a <varpool_node> (node)
601 && (!TREE_READONLY (node->symbol.decl)
602 || TREE_THIS_VOLATILE (node->symbol.decl)))
603 return false;
604 return true;
607 /* COMDAT functions must be shared only if they have address taken,
608 otherwise we can produce our own private implementation with
609 -fwhole-program.
610 Return true when turning COMDAT functoin static can not lead to wrong
611 code when the resulting object links with a library defining same COMDAT.
613 Virtual functions do have their addresses taken from the vtables,
614 but in C++ there is no way to compare their addresses for equality. */
616 static bool
617 comdat_can_be_unshared_p (symtab_node node)
619 if (!comdat_can_be_unshared_p_1 (node))
620 return false;
621 if (node->symbol.same_comdat_group)
623 symtab_node next;
625 /* If more than one function is in the same COMDAT group, it must
626 be shared even if just one function in the comdat group has
627 address taken. */
628 for (next = node->symbol.same_comdat_group;
629 next != node; next = next->symbol.same_comdat_group)
630 if (!comdat_can_be_unshared_p_1 (next))
631 return false;
633 return true;
636 /* Return true when function NODE should be considered externally visible. */
638 static bool
639 cgraph_externally_visible_p (struct cgraph_node *node,
640 bool whole_program)
642 if (!node->symbol.definition)
643 return false;
644 if (!TREE_PUBLIC (node->symbol.decl)
645 || DECL_EXTERNAL (node->symbol.decl))
646 return false;
648 /* Do not try to localize built-in functions yet. One of problems is that we
649 end up mangling their asm for WHOPR that makes it impossible to call them
650 using the implicit built-in declarations anymore. Similarly this enables
651 us to remove them as unreachable before actual calls may appear during
652 expansion or folding. */
653 if (DECL_BUILT_IN (node->symbol.decl))
654 return true;
656 /* If linker counts on us, we must preserve the function. */
657 if (symtab_used_from_object_file_p ((symtab_node) node))
658 return true;
659 if (DECL_PRESERVE_P (node->symbol.decl))
660 return true;
661 if (lookup_attribute ("externally_visible",
662 DECL_ATTRIBUTES (node->symbol.decl)))
663 return true;
664 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
665 && lookup_attribute ("dllexport",
666 DECL_ATTRIBUTES (node->symbol.decl)))
667 return true;
668 if (node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY)
669 return false;
670 /* When doing LTO or whole program, we can bring COMDAT functoins static.
671 This improves code quality and we know we will duplicate them at most twice
672 (in the case that we are not using plugin and link with object file
673 implementing same COMDAT) */
674 if ((in_lto_p || whole_program)
675 && DECL_COMDAT (node->symbol.decl)
676 && comdat_can_be_unshared_p ((symtab_node) node))
677 return false;
679 /* When doing link time optimizations, hidden symbols become local. */
680 if (in_lto_p
681 && (DECL_VISIBILITY (node->symbol.decl) == VISIBILITY_HIDDEN
682 || DECL_VISIBILITY (node->symbol.decl) == VISIBILITY_INTERNAL)
683 /* Be sure that node is defined in IR file, not in other object
684 file. In that case we don't set used_from_other_object_file. */
685 && node->symbol.definition)
687 else if (!whole_program)
688 return true;
690 if (MAIN_NAME_P (DECL_NAME (node->symbol.decl)))
691 return true;
693 return false;
696 /* Return true when variable VNODE should be considered externally visible. */
698 bool
699 varpool_externally_visible_p (struct varpool_node *vnode)
701 if (DECL_EXTERNAL (vnode->symbol.decl))
702 return true;
704 if (!TREE_PUBLIC (vnode->symbol.decl))
705 return false;
707 /* If linker counts on us, we must preserve the function. */
708 if (symtab_used_from_object_file_p ((symtab_node) vnode))
709 return true;
711 if (DECL_HARD_REGISTER (vnode->symbol.decl))
712 return true;
713 if (DECL_PRESERVE_P (vnode->symbol.decl))
714 return true;
715 if (lookup_attribute ("externally_visible",
716 DECL_ATTRIBUTES (vnode->symbol.decl)))
717 return true;
718 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
719 && lookup_attribute ("dllexport",
720 DECL_ATTRIBUTES (vnode->symbol.decl)))
721 return true;
723 /* See if we have linker information about symbol not being used or
724 if we need to make guess based on the declaration.
726 Even if the linker clams the symbol is unused, never bring internal
727 symbols that are declared by user as used or externally visible.
728 This is needed for i.e. references from asm statements. */
729 if (symtab_used_from_object_file_p ((symtab_node) vnode))
730 return true;
731 if (vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY)
732 return false;
734 /* As a special case, the COMDAT virtual tables can be unshared.
735 In LTO mode turn vtables into static variables. The variable is readonly,
736 so this does not enable more optimization, but referring static var
737 is faster for dynamic linking. Also this match logic hidding vtables
738 from LTO symbol tables. */
739 if ((in_lto_p || flag_whole_program)
740 && DECL_COMDAT (vnode->symbol.decl)
741 && comdat_can_be_unshared_p ((symtab_node) vnode))
742 return false;
744 /* When doing link time optimizations, hidden symbols become local. */
745 if (in_lto_p
746 && (DECL_VISIBILITY (vnode->symbol.decl) == VISIBILITY_HIDDEN
747 || DECL_VISIBILITY (vnode->symbol.decl) == VISIBILITY_INTERNAL)
748 /* Be sure that node is defined in IR file, not in other object
749 file. In that case we don't set used_from_other_object_file. */
750 && vnode->symbol.definition)
752 else if (!flag_whole_program)
753 return true;
755 /* Do not attempt to privatize COMDATS by default.
756 This would break linking with C++ libraries sharing
757 inline definitions.
759 FIXME: We can do so for readonly vars with no address taken and
760 possibly also for vtables since no direct pointer comparsion is done.
761 It might be interesting to do so to reduce linking overhead. */
762 if (DECL_COMDAT (vnode->symbol.decl) || DECL_WEAK (vnode->symbol.decl))
763 return true;
764 return false;
767 /* Return true if reference to NODE can be replaced by a local alias.
768 Local aliases save dynamic linking overhead and enable more optimizations.
771 bool
772 can_replace_by_local_alias (symtab_node node)
774 return (symtab_node_availability (node) > AVAIL_OVERWRITABLE
775 && !symtab_can_be_discarded (node));
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 vnode->symbol.unique_name = ((vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
976 || vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
977 && TREE_PUBLIC (vnode->symbol.decl));
978 symtab_make_decl_local (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;
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;
1410 struct cgraph_node *n,*n2;
1411 int nindirect = 0, ncommon = 0, nunknown = 0, nuseless = 0, nconverted = 0;
1412 bool node_map_initialized = false;
1414 if (dump_file)
1415 dump_histogram (dump_file, histogram);
1416 for (i = 0; i < (int)histogram.length (); i++)
1418 overall_time += histogram[i]->count * histogram[i]->time;
1419 overall_size += histogram[i]->size;
1421 if (overall_time)
1423 gcov_type threshold;
1425 gcc_assert (overall_size);
1426 if (dump_file)
1428 gcov_type min, cumulated_time = 0, cumulated_size = 0;
1430 fprintf (dump_file, "Overall time: "HOST_WIDEST_INT_PRINT_DEC"\n",
1431 (HOST_WIDEST_INT)overall_time);
1432 min = get_hot_bb_threshold ();
1433 for (i = 0; i < (int)histogram.length () && histogram[i]->count >= min;
1434 i++)
1436 cumulated_time += histogram[i]->count * histogram[i]->time;
1437 cumulated_size += histogram[i]->size;
1439 fprintf (dump_file, "GCOV min count: "HOST_WIDEST_INT_PRINT_DEC
1440 " Time:%3.2f%% Size:%3.2f%%\n",
1441 (HOST_WIDEST_INT)min,
1442 cumulated_time * 100.0 / overall_time,
1443 cumulated_size * 100.0 / overall_size);
1445 cutoff = (overall_time * PARAM_VALUE (HOT_BB_COUNT_WS_PERMILLE) + 500) / 1000;
1446 threshold = 0;
1447 for (i = 0; cumulated < cutoff; i++)
1449 cumulated += histogram[i]->count * histogram[i]->time;
1450 threshold = histogram[i]->count;
1452 if (!threshold)
1453 threshold = 1;
1454 if (dump_file)
1456 gcov_type cumulated_time = 0, cumulated_size = 0;
1458 for (i = 0;
1459 i < (int)histogram.length () && histogram[i]->count >= threshold;
1460 i++)
1462 cumulated_time += histogram[i]->count * histogram[i]->time;
1463 cumulated_size += histogram[i]->size;
1465 fprintf (dump_file, "Determined min count: "HOST_WIDEST_INT_PRINT_DEC
1466 " Time:%3.2f%% Size:%3.2f%%\n",
1467 (HOST_WIDEST_INT)threshold,
1468 cumulated_time * 100.0 / overall_time,
1469 cumulated_size * 100.0 / overall_size);
1471 if (threshold > get_hot_bb_threshold ()
1472 || in_lto_p)
1474 if (dump_file)
1475 fprintf (dump_file, "Threshold updated.\n");
1476 set_hot_bb_threshold (threshold);
1479 histogram.release();
1480 free_alloc_pool (histogram_pool);
1482 /* Produce speculative calls: we saved common traget from porfiling into
1483 e->common_target_id. Now, at link time, we can look up corresponding
1484 function node and produce speculative call. */
1486 FOR_EACH_DEFINED_FUNCTION (n)
1488 bool update = false;
1490 for (e = n->indirect_calls; e; e = e->next_callee)
1492 if (n->count)
1493 nindirect++;
1494 if (e->indirect_info->common_target_id)
1496 if (!node_map_initialized)
1497 init_node_map (false);
1498 node_map_initialized = true;
1499 ncommon++;
1500 n2 = find_func_by_profile_id (e->indirect_info->common_target_id);
1501 if (n2)
1503 if (dump_file)
1505 fprintf (dump_file, "Indirect call -> direct call from"
1506 " other module %s/%i => %s/%i, prob %3.2f\n",
1507 xstrdup (cgraph_node_name (n)), n->symbol.order,
1508 xstrdup (cgraph_node_name (n2)), n2->symbol.order,
1509 e->indirect_info->common_target_probability
1510 / (float)REG_BR_PROB_BASE);
1512 if (e->indirect_info->common_target_probability
1513 < REG_BR_PROB_BASE / 2)
1515 nuseless++;
1516 if (dump_file)
1517 fprintf (dump_file,
1518 "Not speculating: probability is too low.\n");
1520 else if (!cgraph_maybe_hot_edge_p (e))
1522 nuseless++;
1523 if (dump_file)
1524 fprintf (dump_file,
1525 "Not speculating: call is cold.\n");
1527 else if (cgraph_function_body_availability (n2)
1528 <= AVAIL_OVERWRITABLE
1529 && symtab_can_be_discarded ((symtab_node) n2))
1531 nuseless++;
1532 if (dump_file)
1533 fprintf (dump_file,
1534 "Not speculating: target is overwritable "
1535 "and can be discarded.\n");
1537 else
1539 /* Target may be overwritable, but profile says that
1540 control flow goes to this particular implementation
1541 of N2. Speculate on the local alias to allow inlining.
1543 if (!symtab_can_be_discarded ((symtab_node) n2))
1544 n2 = cgraph (symtab_nonoverwritable_alias ((symtab_node)n2));
1545 nconverted++;
1546 cgraph_turn_edge_to_speculative
1547 (e, n2,
1548 apply_scale (e->count,
1549 e->indirect_info->common_target_probability),
1550 apply_scale (e->frequency,
1551 e->indirect_info->common_target_probability));
1552 update = true;
1555 else
1557 if (dump_file)
1558 fprintf (dump_file, "Function with profile-id %i not found.\n",
1559 e->indirect_info->common_target_id);
1560 nunknown++;
1564 if (update)
1565 inline_update_overall_summary (n);
1567 if (node_map_initialized)
1568 del_node_map ();
1569 if (dump_file && nindirect)
1570 fprintf (dump_file,
1571 "%i indirect calls trained.\n"
1572 "%i (%3.2f%%) have common target.\n"
1573 "%i (%3.2f%%) targets was not found.\n"
1574 "%i (%3.2f%%) speculations seems useless.\n"
1575 "%i (%3.2f%%) speculations produced.\n",
1576 nindirect,
1577 ncommon, ncommon * 100.0 / nindirect,
1578 nunknown, nunknown * 100.0 / nindirect,
1579 nuseless, nuseless * 100.0 / nindirect,
1580 nconverted, nconverted * 100.0 / nindirect);
1582 order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1583 order_pos = ipa_reverse_postorder (order);
1584 for (i = order_pos - 1; i >= 0; i--)
1586 if (order[i]->local.local && cgraph_propagate_frequency (order[i]))
1588 for (e = order[i]->callees; e; e = e->next_callee)
1589 if (e->callee->local.local && !e->callee->symbol.aux)
1591 something_changed = true;
1592 e->callee->symbol.aux = (void *)1;
1595 order[i]->symbol.aux = NULL;
1598 while (something_changed)
1600 something_changed = false;
1601 for (i = order_pos - 1; i >= 0; i--)
1603 if (order[i]->symbol.aux && cgraph_propagate_frequency (order[i]))
1605 for (e = order[i]->callees; e; e = e->next_callee)
1606 if (e->callee->local.local && !e->callee->symbol.aux)
1608 something_changed = true;
1609 e->callee->symbol.aux = (void *)1;
1612 order[i]->symbol.aux = NULL;
1615 free (order);
1616 return 0;
1619 static bool
1620 gate_ipa_profile (void)
1622 return flag_ipa_profile;
1625 namespace {
1627 const pass_data pass_data_ipa_profile =
1629 IPA_PASS, /* type */
1630 "profile_estimate", /* name */
1631 OPTGROUP_NONE, /* optinfo_flags */
1632 true, /* has_gate */
1633 true, /* has_execute */
1634 TV_IPA_PROFILE, /* tv_id */
1635 0, /* properties_required */
1636 0, /* properties_provided */
1637 0, /* properties_destroyed */
1638 0, /* todo_flags_start */
1639 0, /* todo_flags_finish */
1642 class pass_ipa_profile : public ipa_opt_pass_d
1644 public:
1645 pass_ipa_profile(gcc::context *ctxt)
1646 : ipa_opt_pass_d(pass_data_ipa_profile, ctxt,
1647 ipa_profile_generate_summary, /* generate_summary */
1648 ipa_profile_write_summary, /* write_summary */
1649 ipa_profile_read_summary, /* read_summary */
1650 NULL, /* write_optimization_summary */
1651 NULL, /* read_optimization_summary */
1652 NULL, /* stmt_fixup */
1653 0, /* function_transform_todo_flags_start */
1654 NULL, /* function_transform */
1655 NULL) /* variable_transform */
1658 /* opt_pass methods: */
1659 bool gate () { return gate_ipa_profile (); }
1660 unsigned int execute () { return ipa_profile (); }
1662 }; // class pass_ipa_profile
1664 } // anon namespace
1666 ipa_opt_pass_d *
1667 make_pass_ipa_profile (gcc::context *ctxt)
1669 return new pass_ipa_profile (ctxt);
1672 /* Generate and emit a static constructor or destructor. WHICH must
1673 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1674 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1675 initialization priority for this constructor or destructor.
1677 FINAL specify whether the externally visible name for collect2 should
1678 be produced. */
1680 static void
1681 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
1683 static int counter = 0;
1684 char which_buf[16];
1685 tree decl, name, resdecl;
1687 /* The priority is encoded in the constructor or destructor name.
1688 collect2 will sort the names and arrange that they are called at
1689 program startup. */
1690 if (final)
1691 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
1692 else
1693 /* Proudce sane name but one not recognizable by collect2, just for the
1694 case we fail to inline the function. */
1695 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
1696 name = get_file_function_name (which_buf);
1698 decl = build_decl (input_location, FUNCTION_DECL, name,
1699 build_function_type_list (void_type_node, NULL_TREE));
1700 current_function_decl = decl;
1702 resdecl = build_decl (input_location,
1703 RESULT_DECL, NULL_TREE, void_type_node);
1704 DECL_ARTIFICIAL (resdecl) = 1;
1705 DECL_RESULT (decl) = resdecl;
1706 DECL_CONTEXT (resdecl) = decl;
1708 allocate_struct_function (decl, false);
1710 TREE_STATIC (decl) = 1;
1711 TREE_USED (decl) = 1;
1712 DECL_ARTIFICIAL (decl) = 1;
1713 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1714 DECL_SAVED_TREE (decl) = body;
1715 if (!targetm.have_ctors_dtors && final)
1717 TREE_PUBLIC (decl) = 1;
1718 DECL_PRESERVE_P (decl) = 1;
1720 DECL_UNINLINABLE (decl) = 1;
1722 DECL_INITIAL (decl) = make_node (BLOCK);
1723 TREE_USED (DECL_INITIAL (decl)) = 1;
1725 DECL_SOURCE_LOCATION (decl) = input_location;
1726 cfun->function_end_locus = input_location;
1728 switch (which)
1730 case 'I':
1731 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1732 decl_init_priority_insert (decl, priority);
1733 break;
1734 case 'D':
1735 DECL_STATIC_DESTRUCTOR (decl) = 1;
1736 decl_fini_priority_insert (decl, priority);
1737 break;
1738 default:
1739 gcc_unreachable ();
1742 gimplify_function_tree (decl);
1744 cgraph_add_new_function (decl, false);
1746 set_cfun (NULL);
1747 current_function_decl = NULL;
1750 /* Generate and emit a static constructor or destructor. WHICH must
1751 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1752 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1753 initialization priority for this constructor or destructor. */
1755 void
1756 cgraph_build_static_cdtor (char which, tree body, int priority)
1758 cgraph_build_static_cdtor_1 (which, body, priority, false);
1761 /* A vector of FUNCTION_DECLs declared as static constructors. */
1762 static vec<tree> static_ctors;
1763 /* A vector of FUNCTION_DECLs declared as static destructors. */
1764 static vec<tree> static_dtors;
1766 /* When target does not have ctors and dtors, we call all constructor
1767 and destructor by special initialization/destruction function
1768 recognized by collect2.
1770 When we are going to build this function, collect all constructors and
1771 destructors and turn them into normal functions. */
1773 static void
1774 record_cdtor_fn (struct cgraph_node *node)
1776 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
1777 static_ctors.safe_push (node->symbol.decl);
1778 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1779 static_dtors.safe_push (node->symbol.decl);
1780 node = cgraph_get_node (node->symbol.decl);
1781 DECL_DISREGARD_INLINE_LIMITS (node->symbol.decl) = 1;
1784 /* Define global constructors/destructor functions for the CDTORS, of
1785 which they are LEN. The CDTORS are sorted by initialization
1786 priority. If CTOR_P is true, these are constructors; otherwise,
1787 they are destructors. */
1789 static void
1790 build_cdtor (bool ctor_p, vec<tree> cdtors)
1792 size_t i,j;
1793 size_t len = cdtors.length ();
1795 i = 0;
1796 while (i < len)
1798 tree body;
1799 tree fn;
1800 priority_type priority;
1802 priority = 0;
1803 body = NULL_TREE;
1804 j = i;
1807 priority_type p;
1808 fn = cdtors[j];
1809 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1810 if (j == i)
1811 priority = p;
1812 else if (p != priority)
1813 break;
1814 j++;
1816 while (j < len);
1818 /* When there is only one cdtor and target supports them, do nothing. */
1819 if (j == i + 1
1820 && targetm.have_ctors_dtors)
1822 i++;
1823 continue;
1825 /* Find the next batch of constructors/destructors with the same
1826 initialization priority. */
1827 for (;i < j; i++)
1829 tree call;
1830 fn = cdtors[i];
1831 call = build_call_expr (fn, 0);
1832 if (ctor_p)
1833 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1834 else
1835 DECL_STATIC_DESTRUCTOR (fn) = 0;
1836 /* We do not want to optimize away pure/const calls here.
1837 When optimizing, these should be already removed, when not
1838 optimizing, we want user to be able to breakpoint in them. */
1839 TREE_SIDE_EFFECTS (call) = 1;
1840 append_to_statement_list (call, &body);
1842 gcc_assert (body != NULL_TREE);
1843 /* Generate a function to call all the function of like
1844 priority. */
1845 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1849 /* Comparison function for qsort. P1 and P2 are actually of type
1850 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1851 used to determine the sort order. */
1853 static int
1854 compare_ctor (const void *p1, const void *p2)
1856 tree f1;
1857 tree f2;
1858 int priority1;
1859 int priority2;
1861 f1 = *(const tree *)p1;
1862 f2 = *(const tree *)p2;
1863 priority1 = DECL_INIT_PRIORITY (f1);
1864 priority2 = DECL_INIT_PRIORITY (f2);
1866 if (priority1 < priority2)
1867 return -1;
1868 else if (priority1 > priority2)
1869 return 1;
1870 else
1871 /* Ensure a stable sort. Constructors are executed in backwarding
1872 order to make LTO initialize braries first. */
1873 return DECL_UID (f2) - DECL_UID (f1);
1876 /* Comparison function for qsort. P1 and P2 are actually of type
1877 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1878 used to determine the sort order. */
1880 static int
1881 compare_dtor (const void *p1, const void *p2)
1883 tree f1;
1884 tree f2;
1885 int priority1;
1886 int priority2;
1888 f1 = *(const tree *)p1;
1889 f2 = *(const tree *)p2;
1890 priority1 = DECL_FINI_PRIORITY (f1);
1891 priority2 = DECL_FINI_PRIORITY (f2);
1893 if (priority1 < priority2)
1894 return -1;
1895 else if (priority1 > priority2)
1896 return 1;
1897 else
1898 /* Ensure a stable sort. */
1899 return DECL_UID (f1) - DECL_UID (f2);
1902 /* Generate functions to call static constructors and destructors
1903 for targets that do not support .ctors/.dtors sections. These
1904 functions have magic names which are detected by collect2. */
1906 static void
1907 build_cdtor_fns (void)
1909 if (!static_ctors.is_empty ())
1911 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1912 static_ctors.qsort (compare_ctor);
1913 build_cdtor (/*ctor_p=*/true, static_ctors);
1916 if (!static_dtors.is_empty ())
1918 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1919 static_dtors.qsort (compare_dtor);
1920 build_cdtor (/*ctor_p=*/false, static_dtors);
1924 /* Look for constructors and destructors and produce function calling them.
1925 This is needed for targets not supporting ctors or dtors, but we perform the
1926 transformation also at linktime to merge possibly numerous
1927 constructors/destructors into single function to improve code locality and
1928 reduce size. */
1930 static unsigned int
1931 ipa_cdtor_merge (void)
1933 struct cgraph_node *node;
1934 FOR_EACH_DEFINED_FUNCTION (node)
1935 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl)
1936 || DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1937 record_cdtor_fn (node);
1938 build_cdtor_fns ();
1939 static_ctors.release ();
1940 static_dtors.release ();
1941 return 0;
1944 /* Perform the pass when we have no ctors/dtors support
1945 or at LTO time to merge multiple constructors into single
1946 function. */
1948 static bool
1949 gate_ipa_cdtor_merge (void)
1951 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1954 namespace {
1956 const pass_data pass_data_ipa_cdtor_merge =
1958 IPA_PASS, /* type */
1959 "cdtor", /* name */
1960 OPTGROUP_NONE, /* optinfo_flags */
1961 true, /* has_gate */
1962 true, /* has_execute */
1963 TV_CGRAPHOPT, /* tv_id */
1964 0, /* properties_required */
1965 0, /* properties_provided */
1966 0, /* properties_destroyed */
1967 0, /* todo_flags_start */
1968 0, /* todo_flags_finish */
1971 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1973 public:
1974 pass_ipa_cdtor_merge(gcc::context *ctxt)
1975 : ipa_opt_pass_d(pass_data_ipa_cdtor_merge, ctxt,
1976 NULL, /* generate_summary */
1977 NULL, /* write_summary */
1978 NULL, /* read_summary */
1979 NULL, /* write_optimization_summary */
1980 NULL, /* read_optimization_summary */
1981 NULL, /* stmt_fixup */
1982 0, /* function_transform_todo_flags_start */
1983 NULL, /* function_transform */
1984 NULL) /* variable_transform */
1987 /* opt_pass methods: */
1988 bool gate () { return gate_ipa_cdtor_merge (); }
1989 unsigned int execute () { return ipa_cdtor_merge (); }
1991 }; // class pass_ipa_cdtor_merge
1993 } // anon namespace
1995 ipa_opt_pass_d *
1996 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1998 return new pass_ipa_cdtor_merge (ctxt);