PR rtl-optimization/53827
[official-gcc.git] / gcc / ipa-utils.c
blobb83c87b96ad9a4db9bbc97e52786cf84a2d4fcc4
1 /* Utilities for ipa analysis.
2 Copyright (C) 2005, 2007, 2008 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 "tree-flow.h"
27 #include "tree-inline.h"
28 #include "tree-pass.h"
29 #include "langhooks.h"
30 #include "pointer-set.h"
31 #include "splay-tree.h"
32 #include "ggc.h"
33 #include "ipa-utils.h"
34 #include "ipa-reference.h"
35 #include "gimple.h"
36 #include "cgraph.h"
37 #include "flags.h"
38 #include "timevar.h"
39 #include "diagnostic.h"
40 #include "langhooks.h"
42 /* Debugging function for postorder and inorder code. NOTE is a string
43 that is printed before the nodes are printed. ORDER is an array of
44 cgraph_nodes that has COUNT useful nodes in it. */
46 void
47 ipa_print_order (FILE* out,
48 const char * note,
49 struct cgraph_node** order,
50 int count)
52 int i;
53 fprintf (out, "\n\n ordered call graph: %s\n", note);
55 for (i = count - 1; i >= 0; i--)
56 dump_cgraph_node(dump_file, order[i]);
57 fprintf (out, "\n");
58 fflush(out);
62 struct searchc_env {
63 struct cgraph_node **stack;
64 int stack_size;
65 struct cgraph_node **result;
66 int order_pos;
67 splay_tree nodes_marked_new;
68 bool reduce;
69 bool allow_overwritable;
70 int count;
73 /* This is an implementation of Tarjan's strongly connected region
74 finder as reprinted in Aho Hopcraft and Ullman's The Design and
75 Analysis of Computer Programs (1975) pages 192-193. This version
76 has been customized for cgraph_nodes. The env parameter is because
77 it is recursive and there are no nested functions here. This
78 function should only be called from itself or
79 ipa_reduced_postorder. ENV is a stack env and would be
80 unnecessary if C had nested functions. V is the node to start
81 searching from. */
83 static void
84 searchc (struct searchc_env* env, struct cgraph_node *v,
85 bool (*ignore_edge) (struct cgraph_edge *))
87 struct cgraph_edge *edge;
88 struct ipa_dfs_info *v_info = (struct ipa_dfs_info *) v->symbol.aux;
90 /* mark node as old */
91 v_info->new_node = false;
92 splay_tree_remove (env->nodes_marked_new, v->uid);
94 v_info->dfn_number = env->count;
95 v_info->low_link = env->count;
96 env->count++;
97 env->stack[(env->stack_size)++] = v;
98 v_info->on_stack = true;
100 for (edge = v->callees; edge; edge = edge->next_callee)
102 struct ipa_dfs_info * w_info;
103 enum availability avail;
104 struct cgraph_node *w = cgraph_function_or_thunk_node (edge->callee, &avail);
106 if (!w || (ignore_edge && ignore_edge (edge)))
107 continue;
109 if (w->symbol.aux
110 && (avail > AVAIL_OVERWRITABLE
111 || (env->allow_overwritable && avail == AVAIL_OVERWRITABLE)))
113 w_info = (struct ipa_dfs_info *) w->symbol.aux;
114 if (w_info->new_node)
116 searchc (env, w, ignore_edge);
117 v_info->low_link =
118 (v_info->low_link < w_info->low_link) ?
119 v_info->low_link : w_info->low_link;
121 else
122 if ((w_info->dfn_number < v_info->dfn_number)
123 && (w_info->on_stack))
124 v_info->low_link =
125 (w_info->dfn_number < v_info->low_link) ?
126 w_info->dfn_number : v_info->low_link;
131 if (v_info->low_link == v_info->dfn_number)
133 struct cgraph_node *last = NULL;
134 struct cgraph_node *x;
135 struct ipa_dfs_info *x_info;
136 do {
137 x = env->stack[--(env->stack_size)];
138 x_info = (struct ipa_dfs_info *) x->symbol.aux;
139 x_info->on_stack = false;
140 x_info->scc_no = v_info->dfn_number;
142 if (env->reduce)
144 x_info->next_cycle = last;
145 last = x;
147 else
148 env->result[env->order_pos++] = x;
150 while (v != x);
151 if (env->reduce)
152 env->result[env->order_pos++] = v;
156 /* Topsort the call graph by caller relation. Put the result in ORDER.
158 The REDUCE flag is true if you want the cycles reduced to single nodes. Set
159 ALLOW_OVERWRITABLE if nodes with such availability should be included.
160 IGNORE_EDGE, if non-NULL is a hook that may make some edges insignificant
161 for the topological sort. */
164 ipa_reduced_postorder (struct cgraph_node **order,
165 bool reduce, bool allow_overwritable,
166 bool (*ignore_edge) (struct cgraph_edge *))
168 struct cgraph_node *node;
169 struct searchc_env env;
170 splay_tree_node result;
171 env.stack = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
172 env.stack_size = 0;
173 env.result = order;
174 env.order_pos = 0;
175 env.nodes_marked_new = splay_tree_new (splay_tree_compare_ints, 0, 0);
176 env.count = 1;
177 env.reduce = reduce;
178 env.allow_overwritable = allow_overwritable;
180 FOR_EACH_DEFINED_FUNCTION (node)
182 enum availability avail = cgraph_function_body_availability (node);
184 if (avail > AVAIL_OVERWRITABLE
185 || (allow_overwritable
186 && (avail == AVAIL_OVERWRITABLE)))
188 /* Reuse the info if it is already there. */
189 struct ipa_dfs_info *info = (struct ipa_dfs_info *) node->symbol.aux;
190 if (!info)
191 info = XCNEW (struct ipa_dfs_info);
192 info->new_node = true;
193 info->on_stack = false;
194 info->next_cycle = NULL;
195 node->symbol.aux = info;
197 splay_tree_insert (env.nodes_marked_new,
198 (splay_tree_key)node->uid,
199 (splay_tree_value)node);
201 else
202 node->symbol.aux = NULL;
204 result = splay_tree_min (env.nodes_marked_new);
205 while (result)
207 node = (struct cgraph_node *)result->value;
208 searchc (&env, node, ignore_edge);
209 result = splay_tree_min (env.nodes_marked_new);
211 splay_tree_delete (env.nodes_marked_new);
212 free (env.stack);
214 return env.order_pos;
217 /* Deallocate all ipa_dfs_info structures pointed to by the aux pointer of call
218 graph nodes. */
220 void
221 ipa_free_postorder_info (void)
223 struct cgraph_node *node;
224 FOR_EACH_DEFINED_FUNCTION (node)
226 /* Get rid of the aux information. */
227 if (node->symbol.aux)
229 free (node->symbol.aux);
230 node->symbol.aux = NULL;
235 struct postorder_stack
237 struct cgraph_node *node;
238 struct cgraph_edge *edge;
239 int ref;
242 /* Fill array order with all nodes with output flag set in the reverse
243 topological order. Return the number of elements in the array.
244 FIXME: While walking, consider aliases, too. */
247 ipa_reverse_postorder (struct cgraph_node **order)
249 struct cgraph_node *node, *node2;
250 int stack_size = 0;
251 int order_pos = 0;
252 struct cgraph_edge *edge;
253 int pass;
254 struct ipa_ref *ref;
256 struct postorder_stack *stack =
257 XCNEWVEC (struct postorder_stack, cgraph_n_nodes);
259 /* We have to deal with cycles nicely, so use a depth first traversal
260 output algorithm. Ignore the fact that some functions won't need
261 to be output and put them into order as well, so we get dependencies
262 right through inline functions. */
263 FOR_EACH_FUNCTION (node)
264 node->symbol.aux = NULL;
265 for (pass = 0; pass < 2; pass++)
266 FOR_EACH_FUNCTION (node)
267 if (!node->symbol.aux
268 && (pass
269 || (!node->symbol.address_taken
270 && !node->global.inlined_to
271 && !node->alias && !node->thunk.thunk_p
272 && !cgraph_only_called_directly_p (node))))
274 stack_size = 0;
275 stack[stack_size].node = node;
276 stack[stack_size].edge = node->callers;
277 stack[stack_size].ref = 0;
278 node->symbol.aux = (void *)(size_t)1;
279 while (stack_size >= 0)
281 while (true)
283 node2 = NULL;
284 while (stack[stack_size].edge && !node2)
286 edge = stack[stack_size].edge;
287 node2 = edge->caller;
288 stack[stack_size].edge = edge->next_caller;
289 /* Break possible cycles involving always-inline
290 functions by ignoring edges from always-inline
291 functions to non-always-inline functions. */
292 if (DECL_DISREGARD_INLINE_LIMITS (edge->caller->symbol.decl)
293 && !DECL_DISREGARD_INLINE_LIMITS
294 (cgraph_function_node (edge->callee, NULL)->symbol.decl))
295 node2 = NULL;
297 for (;ipa_ref_list_referring_iterate (&stack[stack_size].node->symbol.ref_list,
298 stack[stack_size].ref,
299 ref) && !node2;
300 stack[stack_size].ref++)
302 if (ref->use == IPA_REF_ALIAS)
303 node2 = ipa_ref_referring_node (ref);
305 if (!node2)
306 break;
307 if (!node2->symbol.aux)
309 stack[++stack_size].node = node2;
310 stack[stack_size].edge = node2->callers;
311 stack[stack_size].ref = 0;
312 node2->symbol.aux = (void *)(size_t)1;
315 order[order_pos++] = stack[stack_size--].node;
318 free (stack);
319 FOR_EACH_FUNCTION (node)
320 node->symbol.aux = NULL;
321 return order_pos;
326 /* Given a memory reference T, will return the variable at the bottom
327 of the access. Unlike get_base_address, this will recurse through
328 INDIRECT_REFS. */
330 tree
331 get_base_var (tree t)
333 while (!SSA_VAR_P (t)
334 && (!CONSTANT_CLASS_P (t))
335 && TREE_CODE (t) != LABEL_DECL
336 && TREE_CODE (t) != FUNCTION_DECL
337 && TREE_CODE (t) != CONST_DECL
338 && TREE_CODE (t) != CONSTRUCTOR)
340 t = TREE_OPERAND (t, 0);
342 return t;
346 /* Create a new cgraph node set. */
348 cgraph_node_set
349 cgraph_node_set_new (void)
351 cgraph_node_set new_node_set;
353 new_node_set = XCNEW (struct cgraph_node_set_def);
354 new_node_set->map = pointer_map_create ();
355 new_node_set->nodes = NULL;
356 return new_node_set;
360 /* Add cgraph_node NODE to cgraph_node_set SET. */
362 void
363 cgraph_node_set_add (cgraph_node_set set, struct cgraph_node *node)
365 void **slot;
367 slot = pointer_map_insert (set->map, node);
369 if (*slot)
371 int index = (size_t) *slot - 1;
372 gcc_checking_assert ((VEC_index (cgraph_node_ptr, set->nodes, index)
373 == node));
374 return;
377 *slot = (void *)(size_t) (VEC_length (cgraph_node_ptr, set->nodes) + 1);
379 /* Insert into node vector. */
380 VEC_safe_push (cgraph_node_ptr, heap, set->nodes, node);
384 /* Remove cgraph_node NODE from cgraph_node_set SET. */
386 void
387 cgraph_node_set_remove (cgraph_node_set set, struct cgraph_node *node)
389 void **slot, **last_slot;
390 int index;
391 struct cgraph_node *last_node;
393 slot = pointer_map_contains (set->map, node);
394 if (slot == NULL || !*slot)
395 return;
397 index = (size_t) *slot - 1;
398 gcc_checking_assert (VEC_index (cgraph_node_ptr, set->nodes, index)
399 == node);
401 /* Remove from vector. We do this by swapping node with the last element
402 of the vector. */
403 last_node = VEC_pop (cgraph_node_ptr, set->nodes);
404 if (last_node != node)
406 last_slot = pointer_map_contains (set->map, last_node);
407 gcc_checking_assert (last_slot && *last_slot);
408 *last_slot = (void *)(size_t) (index + 1);
410 /* Move the last element to the original spot of NODE. */
411 VEC_replace (cgraph_node_ptr, set->nodes, index, last_node);
414 /* Remove element from hash table. */
415 *slot = NULL;
419 /* Find NODE in SET and return an iterator to it if found. A null iterator
420 is returned if NODE is not in SET. */
422 cgraph_node_set_iterator
423 cgraph_node_set_find (cgraph_node_set set, struct cgraph_node *node)
425 void **slot;
426 cgraph_node_set_iterator csi;
428 slot = pointer_map_contains (set->map, node);
429 if (slot == NULL || !*slot)
430 csi.index = (unsigned) ~0;
431 else
432 csi.index = (size_t)*slot - 1;
433 csi.set = set;
435 return csi;
439 /* Dump content of SET to file F. */
441 void
442 dump_cgraph_node_set (FILE *f, cgraph_node_set set)
444 cgraph_node_set_iterator iter;
446 for (iter = csi_start (set); !csi_end_p (iter); csi_next (&iter))
448 struct cgraph_node *node = csi_node (iter);
449 fprintf (f, " %s/%i", cgraph_node_name (node), node->uid);
451 fprintf (f, "\n");
455 /* Dump content of SET to stderr. */
457 DEBUG_FUNCTION void
458 debug_cgraph_node_set (cgraph_node_set set)
460 dump_cgraph_node_set (stderr, set);
464 /* Free varpool node set. */
466 void
467 free_cgraph_node_set (cgraph_node_set set)
469 VEC_free (cgraph_node_ptr, heap, set->nodes);
470 pointer_map_destroy (set->map);
471 free (set);
475 /* Create a new varpool node set. */
477 varpool_node_set
478 varpool_node_set_new (void)
480 varpool_node_set new_node_set;
482 new_node_set = XCNEW (struct varpool_node_set_def);
483 new_node_set->map = pointer_map_create ();
484 new_node_set->nodes = NULL;
485 return new_node_set;
489 /* Add varpool_node NODE to varpool_node_set SET. */
491 void
492 varpool_node_set_add (varpool_node_set set, struct varpool_node *node)
494 void **slot;
496 slot = pointer_map_insert (set->map, node);
498 if (*slot)
500 int index = (size_t) *slot - 1;
501 gcc_checking_assert ((VEC_index (varpool_node_ptr, set->nodes, index)
502 == node));
503 return;
506 *slot = (void *)(size_t) (VEC_length (varpool_node_ptr, set->nodes) + 1);
508 /* Insert into node vector. */
509 VEC_safe_push (varpool_node_ptr, heap, set->nodes, node);
513 /* Remove varpool_node NODE from varpool_node_set SET. */
515 void
516 varpool_node_set_remove (varpool_node_set set, struct varpool_node *node)
518 void **slot, **last_slot;
519 int index;
520 struct varpool_node *last_node;
522 slot = pointer_map_contains (set->map, node);
523 if (slot == NULL || !*slot)
524 return;
526 index = (size_t) *slot - 1;
527 gcc_checking_assert (VEC_index (varpool_node_ptr, set->nodes, index)
528 == node);
530 /* Remove from vector. We do this by swapping node with the last element
531 of the vector. */
532 last_node = VEC_pop (varpool_node_ptr, set->nodes);
533 if (last_node != node)
535 last_slot = pointer_map_contains (set->map, last_node);
536 gcc_checking_assert (last_slot && *last_slot);
537 *last_slot = (void *)(size_t) (index + 1);
539 /* Move the last element to the original spot of NODE. */
540 VEC_replace (varpool_node_ptr, set->nodes, index, last_node);
543 /* Remove element from hash table. */
544 *slot = NULL;
548 /* Find NODE in SET and return an iterator to it if found. A null iterator
549 is returned if NODE is not in SET. */
551 varpool_node_set_iterator
552 varpool_node_set_find (varpool_node_set set, struct varpool_node *node)
554 void **slot;
555 varpool_node_set_iterator vsi;
557 slot = pointer_map_contains (set->map, node);
558 if (slot == NULL || !*slot)
559 vsi.index = (unsigned) ~0;
560 else
561 vsi.index = (size_t)*slot - 1;
562 vsi.set = set;
564 return vsi;
568 /* Dump content of SET to file F. */
570 void
571 dump_varpool_node_set (FILE *f, varpool_node_set set)
573 varpool_node_set_iterator iter;
575 for (iter = vsi_start (set); !vsi_end_p (iter); vsi_next (&iter))
577 struct varpool_node *node = vsi_node (iter);
578 fprintf (f, " %s", varpool_node_name (node));
580 fprintf (f, "\n");
584 /* Free varpool node set. */
586 void
587 free_varpool_node_set (varpool_node_set set)
589 VEC_free (varpool_node_ptr, heap, set->nodes);
590 pointer_map_destroy (set->map);
591 free (set);
595 /* Dump content of SET to stderr. */
597 DEBUG_FUNCTION void
598 debug_varpool_node_set (varpool_node_set set)
600 dump_varpool_node_set (stderr, set);