Daily bump.
[official-gcc.git] / gcc / ipa-utils.c
blob328f8e5cc0858e2e85ce034561f4f6b2feec83ee
1 /* Utilities for ipa analysis.
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
3 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "basic-block.h"
27 #include "tree-ssa-alias.h"
28 #include "internal-fn.h"
29 #include "gimple-expr.h"
30 #include "is-a.h"
31 #include "gimple.h"
32 #include "tree-inline.h"
33 #include "dumpfile.h"
34 #include "langhooks.h"
35 #include "splay-tree.h"
36 #include "ipa-utils.h"
37 #include "ipa-reference.h"
38 #include "flags.h"
39 #include "diagnostic.h"
40 #include "langhooks.h"
41 #include "lto-streamer.h"
42 #include "ipa-inline.h"
44 /* Debugging function for postorder and inorder code. NOTE is a string
45 that is printed before the nodes are printed. ORDER is an array of
46 cgraph_nodes that has COUNT useful nodes in it. */
48 void
49 ipa_print_order (FILE* out,
50 const char * note,
51 struct cgraph_node** order,
52 int count)
54 int i;
55 fprintf (out, "\n\n ordered call graph: %s\n", note);
57 for (i = count - 1; i >= 0; i--)
58 dump_cgraph_node (dump_file, order[i]);
59 fprintf (out, "\n");
60 fflush (out);
64 struct searchc_env {
65 struct cgraph_node **stack;
66 int stack_size;
67 struct cgraph_node **result;
68 int order_pos;
69 splay_tree nodes_marked_new;
70 bool reduce;
71 bool allow_overwritable;
72 int count;
75 /* This is an implementation of Tarjan's strongly connected region
76 finder as reprinted in Aho Hopcraft and Ullman's The Design and
77 Analysis of Computer Programs (1975) pages 192-193. This version
78 has been customized for cgraph_nodes. The env parameter is because
79 it is recursive and there are no nested functions here. This
80 function should only be called from itself or
81 ipa_reduced_postorder. ENV is a stack env and would be
82 unnecessary if C had nested functions. V is the node to start
83 searching from. */
85 static void
86 searchc (struct searchc_env* env, struct cgraph_node *v,
87 bool (*ignore_edge) (struct cgraph_edge *))
89 struct cgraph_edge *edge;
90 struct ipa_dfs_info *v_info = (struct ipa_dfs_info *) v->aux;
92 /* mark node as old */
93 v_info->new_node = false;
94 splay_tree_remove (env->nodes_marked_new, v->uid);
96 v_info->dfn_number = env->count;
97 v_info->low_link = env->count;
98 env->count++;
99 env->stack[(env->stack_size)++] = v;
100 v_info->on_stack = true;
102 for (edge = v->callees; edge; edge = edge->next_callee)
104 struct ipa_dfs_info * w_info;
105 enum availability avail;
106 struct cgraph_node *w = cgraph_function_or_thunk_node (edge->callee, &avail);
108 if (!w || (ignore_edge && ignore_edge (edge)))
109 continue;
111 if (w->aux
112 && (avail > AVAIL_OVERWRITABLE
113 || (env->allow_overwritable && avail == AVAIL_OVERWRITABLE)))
115 w_info = (struct ipa_dfs_info *) w->aux;
116 if (w_info->new_node)
118 searchc (env, w, ignore_edge);
119 v_info->low_link =
120 (v_info->low_link < w_info->low_link) ?
121 v_info->low_link : w_info->low_link;
123 else
124 if ((w_info->dfn_number < v_info->dfn_number)
125 && (w_info->on_stack))
126 v_info->low_link =
127 (w_info->dfn_number < v_info->low_link) ?
128 w_info->dfn_number : v_info->low_link;
133 if (v_info->low_link == v_info->dfn_number)
135 struct cgraph_node *last = NULL;
136 struct cgraph_node *x;
137 struct ipa_dfs_info *x_info;
138 do {
139 x = env->stack[--(env->stack_size)];
140 x_info = (struct ipa_dfs_info *) x->aux;
141 x_info->on_stack = false;
142 x_info->scc_no = v_info->dfn_number;
144 if (env->reduce)
146 x_info->next_cycle = last;
147 last = x;
149 else
150 env->result[env->order_pos++] = x;
152 while (v != x);
153 if (env->reduce)
154 env->result[env->order_pos++] = v;
158 /* Topsort the call graph by caller relation. Put the result in ORDER.
160 The REDUCE flag is true if you want the cycles reduced to single nodes.
161 You can use ipa_get_nodes_in_cycle to obtain a vector containing all real
162 call graph nodes in a reduced node.
164 Set ALLOW_OVERWRITABLE if nodes with such availability should be included.
165 IGNORE_EDGE, if non-NULL is a hook that may make some edges insignificant
166 for the topological sort. */
169 ipa_reduced_postorder (struct cgraph_node **order,
170 bool reduce, bool allow_overwritable,
171 bool (*ignore_edge) (struct cgraph_edge *))
173 struct cgraph_node *node;
174 struct searchc_env env;
175 splay_tree_node result;
176 env.stack = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
177 env.stack_size = 0;
178 env.result = order;
179 env.order_pos = 0;
180 env.nodes_marked_new = splay_tree_new (splay_tree_compare_ints, 0, 0);
181 env.count = 1;
182 env.reduce = reduce;
183 env.allow_overwritable = allow_overwritable;
185 FOR_EACH_DEFINED_FUNCTION (node)
187 enum availability avail = cgraph_function_body_availability (node);
189 if (avail > AVAIL_OVERWRITABLE
190 || (allow_overwritable
191 && (avail == AVAIL_OVERWRITABLE)))
193 /* Reuse the info if it is already there. */
194 struct ipa_dfs_info *info = (struct ipa_dfs_info *) node->aux;
195 if (!info)
196 info = XCNEW (struct ipa_dfs_info);
197 info->new_node = true;
198 info->on_stack = false;
199 info->next_cycle = NULL;
200 node->aux = info;
202 splay_tree_insert (env.nodes_marked_new,
203 (splay_tree_key)node->uid,
204 (splay_tree_value)node);
206 else
207 node->aux = NULL;
209 result = splay_tree_min (env.nodes_marked_new);
210 while (result)
212 node = (struct cgraph_node *)result->value;
213 searchc (&env, node, ignore_edge);
214 result = splay_tree_min (env.nodes_marked_new);
216 splay_tree_delete (env.nodes_marked_new);
217 free (env.stack);
219 return env.order_pos;
222 /* Deallocate all ipa_dfs_info structures pointed to by the aux pointer of call
223 graph nodes. */
225 void
226 ipa_free_postorder_info (void)
228 struct cgraph_node *node;
229 FOR_EACH_DEFINED_FUNCTION (node)
231 /* Get rid of the aux information. */
232 if (node->aux)
234 free (node->aux);
235 node->aux = NULL;
240 /* Get the set of nodes for the cycle in the reduced call graph starting
241 from NODE. */
243 vec<cgraph_node_ptr>
244 ipa_get_nodes_in_cycle (struct cgraph_node *node)
246 vec<cgraph_node_ptr> v = vNULL;
247 struct ipa_dfs_info *node_dfs_info;
248 while (node)
250 v.safe_push (node);
251 node_dfs_info = (struct ipa_dfs_info *) node->aux;
252 node = node_dfs_info->next_cycle;
254 return v;
257 /* Return true iff the CS is an edge within a strongly connected component as
258 computed by ipa_reduced_postorder. */
260 bool
261 ipa_edge_within_scc (struct cgraph_edge *cs)
263 struct ipa_dfs_info *caller_dfs = (struct ipa_dfs_info *) cs->caller->aux;
264 struct ipa_dfs_info *callee_dfs;
265 struct cgraph_node *callee = cgraph_function_node (cs->callee, NULL);
267 callee_dfs = (struct ipa_dfs_info *) callee->aux;
268 return (caller_dfs
269 && callee_dfs
270 && caller_dfs->scc_no == callee_dfs->scc_no);
273 struct postorder_stack
275 struct cgraph_node *node;
276 struct cgraph_edge *edge;
277 int ref;
280 /* Fill array order with all nodes with output flag set in the reverse
281 topological order. Return the number of elements in the array.
282 FIXME: While walking, consider aliases, too. */
285 ipa_reverse_postorder (struct cgraph_node **order)
287 struct cgraph_node *node, *node2;
288 int stack_size = 0;
289 int order_pos = 0;
290 struct cgraph_edge *edge;
291 int pass;
292 struct ipa_ref *ref;
294 struct postorder_stack *stack =
295 XCNEWVEC (struct postorder_stack, cgraph_n_nodes);
297 /* We have to deal with cycles nicely, so use a depth first traversal
298 output algorithm. Ignore the fact that some functions won't need
299 to be output and put them into order as well, so we get dependencies
300 right through inline functions. */
301 FOR_EACH_FUNCTION (node)
302 node->aux = NULL;
303 for (pass = 0; pass < 2; pass++)
304 FOR_EACH_FUNCTION (node)
305 if (!node->aux
306 && (pass
307 || (!node->address_taken
308 && !node->global.inlined_to
309 && !node->alias && !node->thunk.thunk_p
310 && !cgraph_only_called_directly_p (node))))
312 stack_size = 0;
313 stack[stack_size].node = node;
314 stack[stack_size].edge = node->callers;
315 stack[stack_size].ref = 0;
316 node->aux = (void *)(size_t)1;
317 while (stack_size >= 0)
319 while (true)
321 node2 = NULL;
322 while (stack[stack_size].edge && !node2)
324 edge = stack[stack_size].edge;
325 node2 = edge->caller;
326 stack[stack_size].edge = edge->next_caller;
327 /* Break possible cycles involving always-inline
328 functions by ignoring edges from always-inline
329 functions to non-always-inline functions. */
330 if (DECL_DISREGARD_INLINE_LIMITS (edge->caller->decl)
331 && !DECL_DISREGARD_INLINE_LIMITS
332 (cgraph_function_node (edge->callee, NULL)->decl))
333 node2 = NULL;
335 for (;ipa_ref_list_referring_iterate (&stack[stack_size].node->ref_list,
336 stack[stack_size].ref,
337 ref) && !node2;
338 stack[stack_size].ref++)
340 if (ref->use == IPA_REF_ALIAS)
341 node2 = ipa_ref_referring_node (ref);
343 if (!node2)
344 break;
345 if (!node2->aux)
347 stack[++stack_size].node = node2;
348 stack[stack_size].edge = node2->callers;
349 stack[stack_size].ref = 0;
350 node2->aux = (void *)(size_t)1;
353 order[order_pos++] = stack[stack_size--].node;
356 free (stack);
357 FOR_EACH_FUNCTION (node)
358 node->aux = NULL;
359 return order_pos;
364 /* Given a memory reference T, will return the variable at the bottom
365 of the access. Unlike get_base_address, this will recurse through
366 INDIRECT_REFS. */
368 tree
369 get_base_var (tree t)
371 while (!SSA_VAR_P (t)
372 && (!CONSTANT_CLASS_P (t))
373 && TREE_CODE (t) != LABEL_DECL
374 && TREE_CODE (t) != FUNCTION_DECL
375 && TREE_CODE (t) != CONST_DECL
376 && TREE_CODE (t) != CONSTRUCTOR)
378 t = TREE_OPERAND (t, 0);
380 return t;
384 /* Create a new cgraph node set. */
386 cgraph_node_set
387 cgraph_node_set_new (void)
389 cgraph_node_set new_node_set;
391 new_node_set = XCNEW (struct cgraph_node_set_def);
392 new_node_set->map = pointer_map_create ();
393 new_node_set->nodes.create (0);
394 return new_node_set;
398 /* Add cgraph_node NODE to cgraph_node_set SET. */
400 void
401 cgraph_node_set_add (cgraph_node_set set, struct cgraph_node *node)
403 void **slot;
405 slot = pointer_map_insert (set->map, node);
407 if (*slot)
409 int index = (size_t) *slot - 1;
410 gcc_checking_assert ((set->nodes[index]
411 == node));
412 return;
415 *slot = (void *)(size_t) (set->nodes.length () + 1);
417 /* Insert into node vector. */
418 set->nodes.safe_push (node);
422 /* Remove cgraph_node NODE from cgraph_node_set SET. */
424 void
425 cgraph_node_set_remove (cgraph_node_set set, struct cgraph_node *node)
427 void **slot, **last_slot;
428 int index;
429 struct cgraph_node *last_node;
431 slot = pointer_map_contains (set->map, node);
432 if (slot == NULL || !*slot)
433 return;
435 index = (size_t) *slot - 1;
436 gcc_checking_assert (set->nodes[index]
437 == node);
439 /* Remove from vector. We do this by swapping node with the last element
440 of the vector. */
441 last_node = set->nodes.pop ();
442 if (last_node != node)
444 last_slot = pointer_map_contains (set->map, last_node);
445 gcc_checking_assert (last_slot && *last_slot);
446 *last_slot = (void *)(size_t) (index + 1);
448 /* Move the last element to the original spot of NODE. */
449 set->nodes[index] = last_node;
452 /* Remove element from hash table. */
453 *slot = NULL;
457 /* Find NODE in SET and return an iterator to it if found. A null iterator
458 is returned if NODE is not in SET. */
460 cgraph_node_set_iterator
461 cgraph_node_set_find (cgraph_node_set set, struct cgraph_node *node)
463 void **slot;
464 cgraph_node_set_iterator csi;
466 slot = pointer_map_contains (set->map, node);
467 if (slot == NULL || !*slot)
468 csi.index = (unsigned) ~0;
469 else
470 csi.index = (size_t)*slot - 1;
471 csi.set = set;
473 return csi;
477 /* Dump content of SET to file F. */
479 void
480 dump_cgraph_node_set (FILE *f, cgraph_node_set set)
482 cgraph_node_set_iterator iter;
484 for (iter = csi_start (set); !csi_end_p (iter); csi_next (&iter))
486 struct cgraph_node *node = csi_node (iter);
487 fprintf (f, " %s/%i", node->name (), node->order);
489 fprintf (f, "\n");
493 /* Dump content of SET to stderr. */
495 DEBUG_FUNCTION void
496 debug_cgraph_node_set (cgraph_node_set set)
498 dump_cgraph_node_set (stderr, set);
502 /* Free varpool node set. */
504 void
505 free_cgraph_node_set (cgraph_node_set set)
507 set->nodes.release ();
508 pointer_map_destroy (set->map);
509 free (set);
513 /* Create a new varpool node set. */
515 varpool_node_set
516 varpool_node_set_new (void)
518 varpool_node_set new_node_set;
520 new_node_set = XCNEW (struct varpool_node_set_def);
521 new_node_set->map = pointer_map_create ();
522 new_node_set->nodes.create (0);
523 return new_node_set;
527 /* Add varpool_node NODE to varpool_node_set SET. */
529 void
530 varpool_node_set_add (varpool_node_set set, varpool_node *node)
532 void **slot;
534 slot = pointer_map_insert (set->map, node);
536 if (*slot)
538 int index = (size_t) *slot - 1;
539 gcc_checking_assert ((set->nodes[index]
540 == node));
541 return;
544 *slot = (void *)(size_t) (set->nodes.length () + 1);
546 /* Insert into node vector. */
547 set->nodes.safe_push (node);
551 /* Remove varpool_node NODE from varpool_node_set SET. */
553 void
554 varpool_node_set_remove (varpool_node_set set, varpool_node *node)
556 void **slot, **last_slot;
557 int index;
558 varpool_node *last_node;
560 slot = pointer_map_contains (set->map, node);
561 if (slot == NULL || !*slot)
562 return;
564 index = (size_t) *slot - 1;
565 gcc_checking_assert (set->nodes[index]
566 == node);
568 /* Remove from vector. We do this by swapping node with the last element
569 of the vector. */
570 last_node = set->nodes.pop ();
571 if (last_node != node)
573 last_slot = pointer_map_contains (set->map, last_node);
574 gcc_checking_assert (last_slot && *last_slot);
575 *last_slot = (void *)(size_t) (index + 1);
577 /* Move the last element to the original spot of NODE. */
578 set->nodes[index] = last_node;
581 /* Remove element from hash table. */
582 *slot = NULL;
586 /* Find NODE in SET and return an iterator to it if found. A null iterator
587 is returned if NODE is not in SET. */
589 varpool_node_set_iterator
590 varpool_node_set_find (varpool_node_set set, varpool_node *node)
592 void **slot;
593 varpool_node_set_iterator vsi;
595 slot = pointer_map_contains (set->map, node);
596 if (slot == NULL || !*slot)
597 vsi.index = (unsigned) ~0;
598 else
599 vsi.index = (size_t)*slot - 1;
600 vsi.set = set;
602 return vsi;
606 /* Dump content of SET to file F. */
608 void
609 dump_varpool_node_set (FILE *f, varpool_node_set set)
611 varpool_node_set_iterator iter;
613 for (iter = vsi_start (set); !vsi_end_p (iter); vsi_next (&iter))
615 varpool_node *node = vsi_node (iter);
616 fprintf (f, " %s", node->name ());
618 fprintf (f, "\n");
622 /* Free varpool node set. */
624 void
625 free_varpool_node_set (varpool_node_set set)
627 set->nodes.release ();
628 pointer_map_destroy (set->map);
629 free (set);
633 /* Dump content of SET to stderr. */
635 DEBUG_FUNCTION void
636 debug_varpool_node_set (varpool_node_set set)
638 dump_varpool_node_set (stderr, set);
642 /* SRC and DST are going to be merged. Take SRC's profile and merge it into
643 DST so it is not going to be lost. Destroy SRC's body on the way. */
645 void
646 ipa_merge_profiles (struct cgraph_node *dst,
647 struct cgraph_node *src)
649 tree oldsrcdecl = src->decl;
650 struct function *srccfun, *dstcfun;
651 bool match = true;
653 if (!src->definition
654 || !dst->definition)
655 return;
656 if (src->frequency < dst->frequency)
657 src->frequency = dst->frequency;
659 /* Time profiles are merged. */
660 if (dst->tp_first_run > src->tp_first_run && src->tp_first_run)
661 dst->tp_first_run = src->tp_first_run;
663 if (!dst->count)
664 return;
665 if (cgraph_dump_file)
667 fprintf (cgraph_dump_file, "Merging profiles of %s/%i to %s/%i\n",
668 xstrdup (src->name ()), src->order,
669 xstrdup (dst->name ()), dst->order);
671 dst->count += src->count;
673 /* This is ugly. We need to get both function bodies into memory.
674 If declaration is merged, we need to duplicate it to be able
675 to load body that is being replaced. This makes symbol table
676 temporarily inconsistent. */
677 if (src->decl == dst->decl)
679 void **slot;
680 struct lto_in_decl_state temp;
681 struct lto_in_decl_state *state;
683 /* We are going to move the decl, we want to remove its file decl data.
684 and link these with the new decl. */
685 temp.fn_decl = src->decl;
686 slot = htab_find_slot (src->lto_file_data->function_decl_states,
687 &temp, NO_INSERT);
688 state = (lto_in_decl_state *)*slot;
689 htab_clear_slot (src->lto_file_data->function_decl_states, slot);
690 gcc_assert (state);
692 /* Duplicate the decl and be sure it does not link into body of DST. */
693 src->decl = copy_node (src->decl);
694 DECL_STRUCT_FUNCTION (src->decl) = NULL;
695 DECL_ARGUMENTS (src->decl) = NULL;
696 DECL_INITIAL (src->decl) = NULL;
697 DECL_RESULT (src->decl) = NULL;
699 /* Associate the decl state with new declaration, so LTO streamer
700 can look it up. */
701 state->fn_decl = src->decl;
702 slot = htab_find_slot (src->lto_file_data->function_decl_states,
703 state, INSERT);
704 gcc_assert (!*slot);
705 *slot = state;
707 cgraph_get_body (src);
708 cgraph_get_body (dst);
709 srccfun = DECL_STRUCT_FUNCTION (src->decl);
710 dstcfun = DECL_STRUCT_FUNCTION (dst->decl);
711 if (n_basic_blocks_for_fn (srccfun)
712 != n_basic_blocks_for_fn (dstcfun))
714 if (cgraph_dump_file)
715 fprintf (cgraph_dump_file,
716 "Giving up; number of basic block mismatch.\n");
717 match = false;
719 else if (last_basic_block_for_fn (srccfun)
720 != last_basic_block_for_fn (dstcfun))
722 if (cgraph_dump_file)
723 fprintf (cgraph_dump_file,
724 "Giving up; last block mismatch.\n");
725 match = false;
727 else
729 basic_block srcbb, dstbb;
731 FOR_ALL_BB_FN (srcbb, srccfun)
733 unsigned int i;
735 dstbb = BASIC_BLOCK_FOR_FN (dstcfun, srcbb->index);
736 if (dstbb == NULL)
738 if (cgraph_dump_file)
739 fprintf (cgraph_dump_file,
740 "No matching block for bb %i.\n",
741 srcbb->index);
742 match = false;
743 break;
745 if (EDGE_COUNT (srcbb->succs) != EDGE_COUNT (dstbb->succs))
747 if (cgraph_dump_file)
748 fprintf (cgraph_dump_file,
749 "Edge count mistmatch for bb %i.\n",
750 srcbb->index);
751 match = false;
752 break;
754 for (i = 0; i < EDGE_COUNT (srcbb->succs); i++)
756 edge srce = EDGE_SUCC (srcbb, i);
757 edge dste = EDGE_SUCC (dstbb, i);
758 if (srce->dest->index != dste->dest->index)
760 if (cgraph_dump_file)
761 fprintf (cgraph_dump_file,
762 "Succ edge mistmatch for bb %i.\n",
763 srce->dest->index);
764 match = false;
765 break;
770 if (match)
772 struct cgraph_edge *e;
773 basic_block srcbb, dstbb;
775 /* TODO: merge also statement histograms. */
776 FOR_ALL_BB_FN (srcbb, srccfun)
778 unsigned int i;
780 dstbb = BASIC_BLOCK_FOR_FN (dstcfun, srcbb->index);
781 dstbb->count += srcbb->count;
782 for (i = 0; i < EDGE_COUNT (srcbb->succs); i++)
784 edge srce = EDGE_SUCC (srcbb, i);
785 edge dste = EDGE_SUCC (dstbb, i);
786 dste->count += srce->count;
789 push_cfun (dstcfun);
790 counts_to_freqs ();
791 compute_function_frequency ();
792 pop_cfun ();
793 for (e = dst->callees; e; e = e->next_callee)
795 gcc_assert (!e->speculative);
796 e->count = gimple_bb (e->call_stmt)->count;
797 e->frequency = compute_call_stmt_bb_frequency
798 (dst->decl,
799 gimple_bb (e->call_stmt));
801 for (e = dst->indirect_calls; e; e = e->next_callee)
803 gcc_assert (!e->speculative);
804 e->count = gimple_bb (e->call_stmt)->count;
805 e->frequency = compute_call_stmt_bb_frequency
806 (dst->decl,
807 gimple_bb (e->call_stmt));
809 cgraph_release_function_body (src);
810 inline_update_overall_summary (dst);
812 /* TODO: if there is no match, we can scale up. */
813 src->decl = oldsrcdecl;
816 /* Return true if call to DEST is known to be self-recusive call withing FUNC. */
818 bool
819 recursive_call_p (tree func, tree dest)
821 struct cgraph_node *dest_node = cgraph_get_create_node (dest);
822 struct cgraph_node *cnode = cgraph_get_create_node (func);
824 return symtab_semantically_equivalent_p (dest_node,
825 cnode);