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"
28 #include "tree-pass.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"
40 #include "diagnostic.h"
41 #include "langhooks.h"
43 /* Debugging function for postorder and inorder code. NOTE is a string
44 that is printed before the nodes are printed. ORDER is an array of
45 cgraph_nodes that has COUNT useful nodes in it. */
48 ipa_print_order (FILE* out
,
50 struct cgraph_node
** order
,
54 fprintf (out
, "\n\n ordered call graph: %s\n", note
);
56 for (i
= count
- 1; i
>= 0; i
--)
57 dump_cgraph_node(dump_file
, order
[i
]);
64 struct cgraph_node
**stack
;
66 struct cgraph_node
**result
;
68 splay_tree nodes_marked_new
;
70 bool allow_overwritable
;
74 /* This is an implementation of Tarjan's strongly connected region
75 finder as reprinted in Aho Hopcraft and Ullman's The Design and
76 Analysis of Computer Programs (1975) pages 192-193. This version
77 has been customized for cgraph_nodes. The env parameter is because
78 it is recursive and there are no nested functions here. This
79 function should only be called from itself or
80 ipa_reduced_postorder. ENV is a stack env and would be
81 unnecessary if C had nested functions. V is the node to start
85 searchc (struct searchc_env
* env
, struct cgraph_node
*v
,
86 bool (*ignore_edge
) (struct cgraph_edge
*))
88 struct cgraph_edge
*edge
;
89 struct ipa_dfs_info
*v_info
= (struct ipa_dfs_info
*) v
->aux
;
91 /* mark node as old */
92 v_info
->new_node
= false;
93 splay_tree_remove (env
->nodes_marked_new
, v
->uid
);
95 v_info
->dfn_number
= env
->count
;
96 v_info
->low_link
= env
->count
;
98 env
->stack
[(env
->stack_size
)++] = v
;
99 v_info
->on_stack
= true;
101 for (edge
= v
->callees
; edge
; edge
= edge
->next_callee
)
103 struct ipa_dfs_info
* w_info
;
104 enum availability avail
;
105 struct cgraph_node
*w
= cgraph_function_or_thunk_node (edge
->callee
, &avail
);
107 if (!w
|| (ignore_edge
&& ignore_edge (edge
)))
111 && (avail
> AVAIL_OVERWRITABLE
112 || (env
->allow_overwritable
&& avail
== AVAIL_OVERWRITABLE
)))
114 w_info
= (struct ipa_dfs_info
*) w
->aux
;
115 if (w_info
->new_node
)
117 searchc (env
, w
, ignore_edge
);
119 (v_info
->low_link
< w_info
->low_link
) ?
120 v_info
->low_link
: w_info
->low_link
;
123 if ((w_info
->dfn_number
< v_info
->dfn_number
)
124 && (w_info
->on_stack
))
126 (w_info
->dfn_number
< v_info
->low_link
) ?
127 w_info
->dfn_number
: v_info
->low_link
;
132 if (v_info
->low_link
== v_info
->dfn_number
)
134 struct cgraph_node
*last
= NULL
;
135 struct cgraph_node
*x
;
136 struct ipa_dfs_info
*x_info
;
138 x
= env
->stack
[--(env
->stack_size
)];
139 x_info
= (struct ipa_dfs_info
*) x
->aux
;
140 x_info
->on_stack
= false;
141 x_info
->scc_no
= v_info
->dfn_number
;
145 x_info
->next_cycle
= last
;
149 env
->result
[env
->order_pos
++] = x
;
153 env
->result
[env
->order_pos
++] = v
;
157 /* Topsort the call graph by caller relation. Put the result in ORDER.
159 The REDUCE flag is true if you want the cycles reduced to single nodes. Set
160 ALLOW_OVERWRITABLE if nodes with such availability should be included.
161 IGNORE_EDGE, if non-NULL is a hook that may make some edges insignificant
162 for the topological sort. */
165 ipa_reduced_postorder (struct cgraph_node
**order
,
166 bool reduce
, bool allow_overwritable
,
167 bool (*ignore_edge
) (struct cgraph_edge
*))
169 struct cgraph_node
*node
;
170 struct searchc_env env
;
171 splay_tree_node result
;
172 env
.stack
= XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
176 env
.nodes_marked_new
= splay_tree_new (splay_tree_compare_ints
, 0, 0);
179 env
.allow_overwritable
= allow_overwritable
;
181 for (node
= cgraph_nodes
; node
; node
= node
->next
)
183 enum availability avail
= cgraph_function_body_availability (node
);
185 if (avail
> AVAIL_OVERWRITABLE
186 || (allow_overwritable
187 && (avail
== AVAIL_OVERWRITABLE
)))
189 /* Reuse the info if it is already there. */
190 struct ipa_dfs_info
*info
= (struct ipa_dfs_info
*) node
->aux
;
192 info
= XCNEW (struct ipa_dfs_info
);
193 info
->new_node
= true;
194 info
->on_stack
= false;
195 info
->next_cycle
= NULL
;
198 splay_tree_insert (env
.nodes_marked_new
,
199 (splay_tree_key
)node
->uid
,
200 (splay_tree_value
)node
);
205 result
= splay_tree_min (env
.nodes_marked_new
);
208 node
= (struct cgraph_node
*)result
->value
;
209 searchc (&env
, node
, ignore_edge
);
210 result
= splay_tree_min (env
.nodes_marked_new
);
212 splay_tree_delete (env
.nodes_marked_new
);
215 return env
.order_pos
;
218 /* Deallocate all ipa_dfs_info structures pointed to by the aux pointer of call
222 ipa_free_postorder_info (void)
224 struct cgraph_node
*node
;
225 for (node
= cgraph_nodes
; node
; node
= node
->next
)
227 /* Get rid of the aux information. */
236 struct postorder_stack
238 struct cgraph_node
*node
;
239 struct cgraph_edge
*edge
;
243 /* Fill array order with all nodes with output flag set in the reverse
244 topological order. Return the number of elements in the array.
245 FIXME: While walking, consider aliases, too. */
248 ipa_reverse_postorder (struct cgraph_node
**order
)
250 struct cgraph_node
*node
, *node2
;
253 struct cgraph_edge
*edge
;
257 struct postorder_stack
*stack
=
258 XCNEWVEC (struct postorder_stack
, cgraph_n_nodes
);
260 /* We have to deal with cycles nicely, so use a depth first traversal
261 output algorithm. Ignore the fact that some functions won't need
262 to be output and put them into order as well, so we get dependencies
263 right through inline functions. */
264 for (node
= cgraph_nodes
; node
; node
= node
->next
)
266 for (pass
= 0; pass
< 2; pass
++)
267 for (node
= cgraph_nodes
; node
; node
= node
->next
)
270 || (!node
->address_taken
271 && !node
->global
.inlined_to
272 && !node
->alias
&& !node
->thunk
.thunk_p
273 && !cgraph_only_called_directly_p (node
))))
276 stack
[stack_size
].node
= node
;
277 stack
[stack_size
].edge
= node
->callers
;
278 stack
[stack_size
].ref
= 0;
279 node
->aux
= (void *)(size_t)1;
280 while (stack_size
>= 0)
285 while (stack
[stack_size
].edge
&& !node2
)
287 edge
= stack
[stack_size
].edge
;
288 node2
= edge
->caller
;
289 stack
[stack_size
].edge
= edge
->next_caller
;
290 /* Break possible cycles involving always-inline
291 functions by ignoring edges from always-inline
292 functions to non-always-inline functions. */
293 if (DECL_DISREGARD_INLINE_LIMITS (edge
->caller
->decl
)
294 && !DECL_DISREGARD_INLINE_LIMITS
295 (cgraph_function_node (edge
->callee
, NULL
)->decl
))
298 for (;ipa_ref_list_refering_iterate (&stack
[stack_size
].node
->ref_list
,
299 stack
[stack_size
].ref
,
301 stack
[stack_size
].ref
++)
303 if (ref
->use
== IPA_REF_ALIAS
)
304 node2
= ipa_ref_refering_node (ref
);
310 stack
[++stack_size
].node
= node2
;
311 stack
[stack_size
].edge
= node2
->callers
;
312 stack
[stack_size
].ref
= 0;
313 node2
->aux
= (void *)(size_t)1;
316 order
[order_pos
++] = stack
[stack_size
--].node
;
320 for (node
= cgraph_nodes
; node
; node
= node
->next
)
327 /* Given a memory reference T, will return the variable at the bottom
328 of the access. Unlike get_base_address, this will recurse thru
332 get_base_var (tree t
)
334 while (!SSA_VAR_P (t
)
335 && (!CONSTANT_CLASS_P (t
))
336 && TREE_CODE (t
) != LABEL_DECL
337 && TREE_CODE (t
) != FUNCTION_DECL
338 && TREE_CODE (t
) != CONST_DECL
339 && TREE_CODE (t
) != CONSTRUCTOR
)
341 t
= TREE_OPERAND (t
, 0);
347 /* Create a new cgraph node set. */
350 cgraph_node_set_new (void)
352 cgraph_node_set new_node_set
;
354 new_node_set
= XCNEW (struct cgraph_node_set_def
);
355 new_node_set
->map
= pointer_map_create ();
356 new_node_set
->nodes
= NULL
;
361 /* Add cgraph_node NODE to cgraph_node_set SET. */
364 cgraph_node_set_add (cgraph_node_set set
, struct cgraph_node
*node
)
368 slot
= pointer_map_insert (set
->map
, node
);
372 int index
= (size_t) *slot
- 1;
373 gcc_checking_assert ((VEC_index (cgraph_node_ptr
, set
->nodes
, index
)
378 *slot
= (void *)(size_t) (VEC_length (cgraph_node_ptr
, set
->nodes
) + 1);
380 /* Insert into node vector. */
381 VEC_safe_push (cgraph_node_ptr
, heap
, set
->nodes
, node
);
385 /* Remove cgraph_node NODE from cgraph_node_set SET. */
388 cgraph_node_set_remove (cgraph_node_set set
, struct cgraph_node
*node
)
390 void **slot
, **last_slot
;
392 struct cgraph_node
*last_node
;
394 slot
= pointer_map_contains (set
->map
, node
);
395 if (slot
== NULL
|| !*slot
)
398 index
= (size_t) *slot
- 1;
399 gcc_checking_assert (VEC_index (cgraph_node_ptr
, set
->nodes
, index
)
402 /* Remove from vector. We do this by swapping node with the last element
404 last_node
= VEC_pop (cgraph_node_ptr
, set
->nodes
);
405 if (last_node
!= node
)
407 last_slot
= pointer_map_contains (set
->map
, last_node
);
408 gcc_checking_assert (last_slot
&& *last_slot
);
409 *last_slot
= (void *)(size_t) (index
+ 1);
411 /* Move the last element to the original spot of NODE. */
412 VEC_replace (cgraph_node_ptr
, set
->nodes
, index
, last_node
);
415 /* Remove element from hash table. */
420 /* Find NODE in SET and return an iterator to it if found. A null iterator
421 is returned if NODE is not in SET. */
423 cgraph_node_set_iterator
424 cgraph_node_set_find (cgraph_node_set set
, struct cgraph_node
*node
)
427 cgraph_node_set_iterator csi
;
429 slot
= pointer_map_contains (set
->map
, node
);
430 if (slot
== NULL
|| !*slot
)
431 csi
.index
= (unsigned) ~0;
433 csi
.index
= (size_t)*slot
- 1;
440 /* Dump content of SET to file F. */
443 dump_cgraph_node_set (FILE *f
, cgraph_node_set set
)
445 cgraph_node_set_iterator iter
;
447 for (iter
= csi_start (set
); !csi_end_p (iter
); csi_next (&iter
))
449 struct cgraph_node
*node
= csi_node (iter
);
450 fprintf (f
, " %s/%i", cgraph_node_name (node
), node
->uid
);
456 /* Dump content of SET to stderr. */
459 debug_cgraph_node_set (cgraph_node_set set
)
461 dump_cgraph_node_set (stderr
, set
);
465 /* Free varpool node set. */
468 free_cgraph_node_set (cgraph_node_set set
)
470 VEC_free (cgraph_node_ptr
, heap
, set
->nodes
);
471 pointer_map_destroy (set
->map
);
476 /* Create a new varpool node set. */
479 varpool_node_set_new (void)
481 varpool_node_set new_node_set
;
483 new_node_set
= XCNEW (struct varpool_node_set_def
);
484 new_node_set
->map
= pointer_map_create ();
485 new_node_set
->nodes
= NULL
;
490 /* Add varpool_node NODE to varpool_node_set SET. */
493 varpool_node_set_add (varpool_node_set set
, struct varpool_node
*node
)
497 slot
= pointer_map_insert (set
->map
, node
);
501 int index
= (size_t) *slot
- 1;
502 gcc_checking_assert ((VEC_index (varpool_node_ptr
, set
->nodes
, index
)
507 *slot
= (void *)(size_t) (VEC_length (varpool_node_ptr
, set
->nodes
) + 1);
509 /* Insert into node vector. */
510 VEC_safe_push (varpool_node_ptr
, heap
, set
->nodes
, node
);
514 /* Remove varpool_node NODE from varpool_node_set SET. */
517 varpool_node_set_remove (varpool_node_set set
, struct varpool_node
*node
)
519 void **slot
, **last_slot
;
521 struct varpool_node
*last_node
;
523 slot
= pointer_map_contains (set
->map
, node
);
524 if (slot
== NULL
|| !*slot
)
527 index
= (size_t) *slot
- 1;
528 gcc_checking_assert (VEC_index (varpool_node_ptr
, set
->nodes
, index
)
531 /* Remove from vector. We do this by swapping node with the last element
533 last_node
= VEC_pop (varpool_node_ptr
, set
->nodes
);
534 if (last_node
!= node
)
536 last_slot
= pointer_map_contains (set
->map
, last_node
);
537 gcc_checking_assert (last_slot
&& *last_slot
);
538 *last_slot
= (void *)(size_t) (index
+ 1);
540 /* Move the last element to the original spot of NODE. */
541 VEC_replace (varpool_node_ptr
, set
->nodes
, index
, last_node
);
544 /* Remove element from hash table. */
549 /* Find NODE in SET and return an iterator to it if found. A null iterator
550 is returned if NODE is not in SET. */
552 varpool_node_set_iterator
553 varpool_node_set_find (varpool_node_set set
, struct varpool_node
*node
)
556 varpool_node_set_iterator vsi
;
558 slot
= pointer_map_contains (set
->map
, node
);
559 if (slot
== NULL
|| !*slot
)
560 vsi
.index
= (unsigned) ~0;
562 vsi
.index
= (size_t)*slot
- 1;
569 /* Dump content of SET to file F. */
572 dump_varpool_node_set (FILE *f
, varpool_node_set set
)
574 varpool_node_set_iterator iter
;
576 for (iter
= vsi_start (set
); !vsi_end_p (iter
); vsi_next (&iter
))
578 struct varpool_node
*node
= vsi_node (iter
);
579 fprintf (f
, " %s", varpool_node_name (node
));
585 /* Free varpool node set. */
588 free_varpool_node_set (varpool_node_set set
)
590 VEC_free (varpool_node_ptr
, heap
, set
->nodes
);
591 pointer_map_destroy (set
->map
);
596 /* Dump content of SET to stderr. */
599 debug_varpool_node_set (varpool_node_set set
)
601 dump_varpool_node_set (stderr
, set
);