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
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
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/>. */
23 #include "coretypes.h"
26 #include "tree-flow.h"
27 #include "tree-inline.h"
29 #include "langhooks.h"
30 #include "pointer-set.h"
31 #include "splay-tree.h"
33 #include "ipa-utils.h"
34 #include "ipa-reference.h"
38 #include "diagnostic.h"
39 #include "langhooks.h"
41 /* Debugging function for postorder and inorder code. NOTE is a string
42 that is printed before the nodes are printed. ORDER is an array of
43 cgraph_nodes that has COUNT useful nodes in it. */
46 ipa_print_order (FILE* out
,
48 struct cgraph_node
** order
,
52 fprintf (out
, "\n\n ordered call graph: %s\n", note
);
54 for (i
= count
- 1; i
>= 0; i
--)
55 dump_cgraph_node(dump_file
, order
[i
]);
62 struct cgraph_node
**stack
;
64 struct cgraph_node
**result
;
66 splay_tree nodes_marked_new
;
68 bool allow_overwritable
;
72 /* This is an implementation of Tarjan's strongly connected region
73 finder as reprinted in Aho Hopcraft and Ullman's The Design and
74 Analysis of Computer Programs (1975) pages 192-193. This version
75 has been customized for cgraph_nodes. The env parameter is because
76 it is recursive and there are no nested functions here. This
77 function should only be called from itself or
78 ipa_reduced_postorder. ENV is a stack env and would be
79 unnecessary if C had nested functions. V is the node to start
83 searchc (struct searchc_env
* env
, struct cgraph_node
*v
,
84 bool (*ignore_edge
) (struct cgraph_edge
*))
86 struct cgraph_edge
*edge
;
87 struct ipa_dfs_info
*v_info
= (struct ipa_dfs_info
*) v
->symbol
.aux
;
89 /* mark node as old */
90 v_info
->new_node
= false;
91 splay_tree_remove (env
->nodes_marked_new
, v
->uid
);
93 v_info
->dfn_number
= env
->count
;
94 v_info
->low_link
= env
->count
;
96 env
->stack
[(env
->stack_size
)++] = v
;
97 v_info
->on_stack
= true;
99 for (edge
= v
->callees
; edge
; edge
= edge
->next_callee
)
101 struct ipa_dfs_info
* w_info
;
102 enum availability avail
;
103 struct cgraph_node
*w
= cgraph_function_or_thunk_node (edge
->callee
, &avail
);
105 if (!w
|| (ignore_edge
&& ignore_edge (edge
)))
109 && (avail
> AVAIL_OVERWRITABLE
110 || (env
->allow_overwritable
&& avail
== AVAIL_OVERWRITABLE
)))
112 w_info
= (struct ipa_dfs_info
*) w
->symbol
.aux
;
113 if (w_info
->new_node
)
115 searchc (env
, w
, ignore_edge
);
117 (v_info
->low_link
< w_info
->low_link
) ?
118 v_info
->low_link
: w_info
->low_link
;
121 if ((w_info
->dfn_number
< v_info
->dfn_number
)
122 && (w_info
->on_stack
))
124 (w_info
->dfn_number
< v_info
->low_link
) ?
125 w_info
->dfn_number
: v_info
->low_link
;
130 if (v_info
->low_link
== v_info
->dfn_number
)
132 struct cgraph_node
*last
= NULL
;
133 struct cgraph_node
*x
;
134 struct ipa_dfs_info
*x_info
;
136 x
= env
->stack
[--(env
->stack_size
)];
137 x_info
= (struct ipa_dfs_info
*) x
->symbol
.aux
;
138 x_info
->on_stack
= false;
139 x_info
->scc_no
= v_info
->dfn_number
;
143 x_info
->next_cycle
= last
;
147 env
->result
[env
->order_pos
++] = x
;
151 env
->result
[env
->order_pos
++] = v
;
155 /* Topsort the call graph by caller relation. Put the result in ORDER.
157 The REDUCE flag is true if you want the cycles reduced to single nodes. Set
158 ALLOW_OVERWRITABLE if nodes with such availability should be included.
159 IGNORE_EDGE, if non-NULL is a hook that may make some edges insignificant
160 for the topological sort. */
163 ipa_reduced_postorder (struct cgraph_node
**order
,
164 bool reduce
, bool allow_overwritable
,
165 bool (*ignore_edge
) (struct cgraph_edge
*))
167 struct cgraph_node
*node
;
168 struct searchc_env env
;
169 splay_tree_node result
;
170 env
.stack
= XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
174 env
.nodes_marked_new
= splay_tree_new (splay_tree_compare_ints
, 0, 0);
177 env
.allow_overwritable
= allow_overwritable
;
179 FOR_EACH_DEFINED_FUNCTION (node
)
181 enum availability avail
= cgraph_function_body_availability (node
);
183 if (avail
> AVAIL_OVERWRITABLE
184 || (allow_overwritable
185 && (avail
== AVAIL_OVERWRITABLE
)))
187 /* Reuse the info if it is already there. */
188 struct ipa_dfs_info
*info
= (struct ipa_dfs_info
*) node
->symbol
.aux
;
190 info
= XCNEW (struct ipa_dfs_info
);
191 info
->new_node
= true;
192 info
->on_stack
= false;
193 info
->next_cycle
= NULL
;
194 node
->symbol
.aux
= info
;
196 splay_tree_insert (env
.nodes_marked_new
,
197 (splay_tree_key
)node
->uid
,
198 (splay_tree_value
)node
);
201 node
->symbol
.aux
= NULL
;
203 result
= splay_tree_min (env
.nodes_marked_new
);
206 node
= (struct cgraph_node
*)result
->value
;
207 searchc (&env
, node
, ignore_edge
);
208 result
= splay_tree_min (env
.nodes_marked_new
);
210 splay_tree_delete (env
.nodes_marked_new
);
213 return env
.order_pos
;
216 /* Deallocate all ipa_dfs_info structures pointed to by the aux pointer of call
220 ipa_free_postorder_info (void)
222 struct cgraph_node
*node
;
223 FOR_EACH_DEFINED_FUNCTION (node
)
225 /* Get rid of the aux information. */
226 if (node
->symbol
.aux
)
228 free (node
->symbol
.aux
);
229 node
->symbol
.aux
= NULL
;
234 struct postorder_stack
236 struct cgraph_node
*node
;
237 struct cgraph_edge
*edge
;
241 /* Fill array order with all nodes with output flag set in the reverse
242 topological order. Return the number of elements in the array.
243 FIXME: While walking, consider aliases, too. */
246 ipa_reverse_postorder (struct cgraph_node
**order
)
248 struct cgraph_node
*node
, *node2
;
251 struct cgraph_edge
*edge
;
255 struct postorder_stack
*stack
=
256 XCNEWVEC (struct postorder_stack
, cgraph_n_nodes
);
258 /* We have to deal with cycles nicely, so use a depth first traversal
259 output algorithm. Ignore the fact that some functions won't need
260 to be output and put them into order as well, so we get dependencies
261 right through inline functions. */
262 FOR_EACH_FUNCTION (node
)
263 node
->symbol
.aux
= NULL
;
264 for (pass
= 0; pass
< 2; pass
++)
265 FOR_EACH_FUNCTION (node
)
266 if (!node
->symbol
.aux
268 || (!node
->symbol
.address_taken
269 && !node
->global
.inlined_to
270 && !node
->alias
&& !node
->thunk
.thunk_p
271 && !cgraph_only_called_directly_p (node
))))
274 stack
[stack_size
].node
= node
;
275 stack
[stack_size
].edge
= node
->callers
;
276 stack
[stack_size
].ref
= 0;
277 node
->symbol
.aux
= (void *)(size_t)1;
278 while (stack_size
>= 0)
283 while (stack
[stack_size
].edge
&& !node2
)
285 edge
= stack
[stack_size
].edge
;
286 node2
= edge
->caller
;
287 stack
[stack_size
].edge
= edge
->next_caller
;
288 /* Break possible cycles involving always-inline
289 functions by ignoring edges from always-inline
290 functions to non-always-inline functions. */
291 if (DECL_DISREGARD_INLINE_LIMITS (edge
->caller
->symbol
.decl
)
292 && !DECL_DISREGARD_INLINE_LIMITS
293 (cgraph_function_node (edge
->callee
, NULL
)->symbol
.decl
))
296 for (;ipa_ref_list_referring_iterate (&stack
[stack_size
].node
->symbol
.ref_list
,
297 stack
[stack_size
].ref
,
299 stack
[stack_size
].ref
++)
301 if (ref
->use
== IPA_REF_ALIAS
)
302 node2
= ipa_ref_referring_node (ref
);
306 if (!node2
->symbol
.aux
)
308 stack
[++stack_size
].node
= node2
;
309 stack
[stack_size
].edge
= node2
->callers
;
310 stack
[stack_size
].ref
= 0;
311 node2
->symbol
.aux
= (void *)(size_t)1;
314 order
[order_pos
++] = stack
[stack_size
--].node
;
318 FOR_EACH_FUNCTION (node
)
319 node
->symbol
.aux
= NULL
;
325 /* Given a memory reference T, will return the variable at the bottom
326 of the access. Unlike get_base_address, this will recurse through
330 get_base_var (tree t
)
332 while (!SSA_VAR_P (t
)
333 && (!CONSTANT_CLASS_P (t
))
334 && TREE_CODE (t
) != LABEL_DECL
335 && TREE_CODE (t
) != FUNCTION_DECL
336 && TREE_CODE (t
) != CONST_DECL
337 && TREE_CODE (t
) != CONSTRUCTOR
)
339 t
= TREE_OPERAND (t
, 0);
345 /* Create a new cgraph node set. */
348 cgraph_node_set_new (void)
350 cgraph_node_set new_node_set
;
352 new_node_set
= XCNEW (struct cgraph_node_set_def
);
353 new_node_set
->map
= pointer_map_create ();
354 new_node_set
->nodes
= NULL
;
359 /* Add cgraph_node NODE to cgraph_node_set SET. */
362 cgraph_node_set_add (cgraph_node_set set
, struct cgraph_node
*node
)
366 slot
= pointer_map_insert (set
->map
, node
);
370 int index
= (size_t) *slot
- 1;
371 gcc_checking_assert ((VEC_index (cgraph_node_ptr
, set
->nodes
, index
)
376 *slot
= (void *)(size_t) (VEC_length (cgraph_node_ptr
, set
->nodes
) + 1);
378 /* Insert into node vector. */
379 VEC_safe_push (cgraph_node_ptr
, heap
, set
->nodes
, node
);
383 /* Remove cgraph_node NODE from cgraph_node_set SET. */
386 cgraph_node_set_remove (cgraph_node_set set
, struct cgraph_node
*node
)
388 void **slot
, **last_slot
;
390 struct cgraph_node
*last_node
;
392 slot
= pointer_map_contains (set
->map
, node
);
393 if (slot
== NULL
|| !*slot
)
396 index
= (size_t) *slot
- 1;
397 gcc_checking_assert (VEC_index (cgraph_node_ptr
, set
->nodes
, index
)
400 /* Remove from vector. We do this by swapping node with the last element
402 last_node
= VEC_pop (cgraph_node_ptr
, set
->nodes
);
403 if (last_node
!= node
)
405 last_slot
= pointer_map_contains (set
->map
, last_node
);
406 gcc_checking_assert (last_slot
&& *last_slot
);
407 *last_slot
= (void *)(size_t) (index
+ 1);
409 /* Move the last element to the original spot of NODE. */
410 VEC_replace (cgraph_node_ptr
, set
->nodes
, index
, last_node
);
413 /* Remove element from hash table. */
418 /* Find NODE in SET and return an iterator to it if found. A null iterator
419 is returned if NODE is not in SET. */
421 cgraph_node_set_iterator
422 cgraph_node_set_find (cgraph_node_set set
, struct cgraph_node
*node
)
425 cgraph_node_set_iterator csi
;
427 slot
= pointer_map_contains (set
->map
, node
);
428 if (slot
== NULL
|| !*slot
)
429 csi
.index
= (unsigned) ~0;
431 csi
.index
= (size_t)*slot
- 1;
438 /* Dump content of SET to file F. */
441 dump_cgraph_node_set (FILE *f
, cgraph_node_set set
)
443 cgraph_node_set_iterator iter
;
445 for (iter
= csi_start (set
); !csi_end_p (iter
); csi_next (&iter
))
447 struct cgraph_node
*node
= csi_node (iter
);
448 fprintf (f
, " %s/%i", cgraph_node_name (node
), node
->uid
);
454 /* Dump content of SET to stderr. */
457 debug_cgraph_node_set (cgraph_node_set set
)
459 dump_cgraph_node_set (stderr
, set
);
463 /* Free varpool node set. */
466 free_cgraph_node_set (cgraph_node_set set
)
468 VEC_free (cgraph_node_ptr
, heap
, set
->nodes
);
469 pointer_map_destroy (set
->map
);
474 /* Create a new varpool node set. */
477 varpool_node_set_new (void)
479 varpool_node_set new_node_set
;
481 new_node_set
= XCNEW (struct varpool_node_set_def
);
482 new_node_set
->map
= pointer_map_create ();
483 new_node_set
->nodes
= NULL
;
488 /* Add varpool_node NODE to varpool_node_set SET. */
491 varpool_node_set_add (varpool_node_set set
, struct varpool_node
*node
)
495 slot
= pointer_map_insert (set
->map
, node
);
499 int index
= (size_t) *slot
- 1;
500 gcc_checking_assert ((VEC_index (varpool_node_ptr
, set
->nodes
, index
)
505 *slot
= (void *)(size_t) (VEC_length (varpool_node_ptr
, set
->nodes
) + 1);
507 /* Insert into node vector. */
508 VEC_safe_push (varpool_node_ptr
, heap
, set
->nodes
, node
);
512 /* Remove varpool_node NODE from varpool_node_set SET. */
515 varpool_node_set_remove (varpool_node_set set
, struct varpool_node
*node
)
517 void **slot
, **last_slot
;
519 struct varpool_node
*last_node
;
521 slot
= pointer_map_contains (set
->map
, node
);
522 if (slot
== NULL
|| !*slot
)
525 index
= (size_t) *slot
- 1;
526 gcc_checking_assert (VEC_index (varpool_node_ptr
, set
->nodes
, index
)
529 /* Remove from vector. We do this by swapping node with the last element
531 last_node
= VEC_pop (varpool_node_ptr
, set
->nodes
);
532 if (last_node
!= node
)
534 last_slot
= pointer_map_contains (set
->map
, last_node
);
535 gcc_checking_assert (last_slot
&& *last_slot
);
536 *last_slot
= (void *)(size_t) (index
+ 1);
538 /* Move the last element to the original spot of NODE. */
539 VEC_replace (varpool_node_ptr
, set
->nodes
, index
, last_node
);
542 /* Remove element from hash table. */
547 /* Find NODE in SET and return an iterator to it if found. A null iterator
548 is returned if NODE is not in SET. */
550 varpool_node_set_iterator
551 varpool_node_set_find (varpool_node_set set
, struct varpool_node
*node
)
554 varpool_node_set_iterator vsi
;
556 slot
= pointer_map_contains (set
->map
, node
);
557 if (slot
== NULL
|| !*slot
)
558 vsi
.index
= (unsigned) ~0;
560 vsi
.index
= (size_t)*slot
- 1;
567 /* Dump content of SET to file F. */
570 dump_varpool_node_set (FILE *f
, varpool_node_set set
)
572 varpool_node_set_iterator iter
;
574 for (iter
= vsi_start (set
); !vsi_end_p (iter
); vsi_next (&iter
))
576 struct varpool_node
*node
= vsi_node (iter
);
577 fprintf (f
, " %s", varpool_node_name (node
));
583 /* Free varpool node set. */
586 free_varpool_node_set (varpool_node_set set
)
588 VEC_free (varpool_node_ptr
, heap
, set
->nodes
);
589 pointer_map_destroy (set
->map
);
594 /* Dump content of SET to stderr. */
597 debug_varpool_node_set (varpool_node_set set
)
599 dump_varpool_node_set (stderr
, set
);