]> gcc.gnu.org Git - gcc.git/blame - gcc/ipa-utils.c
LANGUAGES: Fix typos.
[gcc.git] / gcc / ipa-utils.c
CommitLineData
ea900239 1/* Utilities for ipa analysis.
66647d44 2 Copyright (C) 2005, 2007, 2008 Free Software Foundation, Inc.
ea900239
DB
3 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9dcd6f09 9Software Foundation; either version 3, or (at your option) any later
ea900239
DB
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
ea900239
DB
20
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"
ea264ca5 31#include "splay-tree.h"
ea900239
DB
32#include "ggc.h"
33#include "ipa-utils.h"
34#include "ipa-reference.h"
726a989a 35#include "gimple.h"
ea900239
DB
36#include "cgraph.h"
37#include "output.h"
38#include "flags.h"
39#include "timevar.h"
40#include "diagnostic.h"
41#include "langhooks.h"
42
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. */
46
b8698a0f 47void
af8bca3c
MJ
48ipa_print_order (FILE* out,
49 const char * note,
50 struct cgraph_node** order,
51 int count)
ea900239
DB
52{
53 int i;
54 fprintf (out, "\n\n ordered call graph: %s\n", note);
b8698a0f 55
ea900239
DB
56 for (i = count - 1; i >= 0; i--)
57 dump_cgraph_node(dump_file, order[i]);
58 fprintf (out, "\n");
59 fflush(out);
60}
61
62\f
63struct searchc_env {
64 struct cgraph_node **stack;
65 int stack_size;
66 struct cgraph_node **result;
67 int order_pos;
68 splay_tree nodes_marked_new;
69 bool reduce;
b6156cf2 70 bool allow_overwritable;
ea900239
DB
71 int count;
72};
73
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
af8bca3c 80 ipa_reduced_postorder. ENV is a stack env and would be
ea900239
DB
81 unnecessary if C had nested functions. V is the node to start
82 searching from. */
83
84static void
2505c5ed
JH
85searchc (struct searchc_env* env, struct cgraph_node *v,
86 bool (*ignore_edge) (struct cgraph_edge *))
ea900239
DB
87{
88 struct cgraph_edge *edge;
960bfb69 89 struct ipa_dfs_info *v_info = (struct ipa_dfs_info *) v->symbol.aux;
b8698a0f 90
ea900239 91 /* mark node as old */
c5274326 92 v_info->new_node = false;
ea900239 93 splay_tree_remove (env->nodes_marked_new, v->uid);
b8698a0f 94
ea900239
DB
95 v_info->dfn_number = env->count;
96 v_info->low_link = env->count;
97 env->count++;
98 env->stack[(env->stack_size)++] = v;
99 v_info->on_stack = true;
b8698a0f 100
ea900239
DB
101 for (edge = v->callees; edge; edge = edge->next_callee)
102 {
103 struct ipa_dfs_info * w_info;
fede8efa
JH
104 enum availability avail;
105 struct cgraph_node *w = cgraph_function_or_thunk_node (edge->callee, &avail);
e2c9111c 106
fede8efa 107 if (!w || (ignore_edge && ignore_edge (edge)))
2505c5ed
JH
108 continue;
109
960bfb69 110 if (w->symbol.aux
b6156cf2
MJ
111 && (avail > AVAIL_OVERWRITABLE
112 || (env->allow_overwritable && avail == AVAIL_OVERWRITABLE)))
ea900239 113 {
960bfb69 114 w_info = (struct ipa_dfs_info *) w->symbol.aux;
b8698a0f 115 if (w_info->new_node)
ea900239 116 {
2505c5ed 117 searchc (env, w, ignore_edge);
ea900239
DB
118 v_info->low_link =
119 (v_info->low_link < w_info->low_link) ?
120 v_info->low_link : w_info->low_link;
b8698a0f
L
121 }
122 else
123 if ((w_info->dfn_number < v_info->dfn_number)
124 && (w_info->on_stack))
ea900239
DB
125 v_info->low_link =
126 (w_info->dfn_number < v_info->low_link) ?
127 w_info->dfn_number : v_info->low_link;
128 }
129 }
130
131
b8698a0f 132 if (v_info->low_link == v_info->dfn_number)
ea900239
DB
133 {
134 struct cgraph_node *last = NULL;
135 struct cgraph_node *x;
136 struct ipa_dfs_info *x_info;
137 do {
138 x = env->stack[--(env->stack_size)];
960bfb69 139 x_info = (struct ipa_dfs_info *) x->symbol.aux;
ea900239 140 x_info->on_stack = false;
11026b51 141 x_info->scc_no = v_info->dfn_number;
b8698a0f
L
142
143 if (env->reduce)
ea900239
DB
144 {
145 x_info->next_cycle = last;
146 last = x;
b8698a0f
L
147 }
148 else
ea900239 149 env->result[env->order_pos++] = x;
b8698a0f 150 }
ea900239 151 while (v != x);
b8698a0f 152 if (env->reduce)
ea900239
DB
153 env->result[env->order_pos++] = v;
154 }
155}
156
157/* Topsort the call graph by caller relation. Put the result in ORDER.
158
af8bca3c
MJ
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. */
ea900239
DB
163
164int
af8bca3c
MJ
165ipa_reduced_postorder (struct cgraph_node **order,
166 bool reduce, bool allow_overwritable,
167 bool (*ignore_edge) (struct cgraph_edge *))
ea900239
DB
168{
169 struct cgraph_node *node;
170 struct searchc_env env;
171 splay_tree_node result;
5ed6ace5 172 env.stack = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
ea900239
DB
173 env.stack_size = 0;
174 env.result = order;
175 env.order_pos = 0;
176 env.nodes_marked_new = splay_tree_new (splay_tree_compare_ints, 0, 0);
177 env.count = 1;
178 env.reduce = reduce;
b6156cf2 179 env.allow_overwritable = allow_overwritable;
b8698a0f 180
65c70e6b 181 FOR_EACH_DEFINED_FUNCTION (node)
e2c9111c
JH
182 {
183 enum availability avail = cgraph_function_body_availability (node);
184
185 if (avail > AVAIL_OVERWRITABLE
b8698a0f 186 || (allow_overwritable
e2c9111c
JH
187 && (avail == AVAIL_OVERWRITABLE)))
188 {
189 /* Reuse the info if it is already there. */
960bfb69 190 struct ipa_dfs_info *info = (struct ipa_dfs_info *) node->symbol.aux;
e2c9111c
JH
191 if (!info)
192 info = XCNEW (struct ipa_dfs_info);
193 info->new_node = true;
194 info->on_stack = false;
195 info->next_cycle = NULL;
960bfb69 196 node->symbol.aux = info;
b8698a0f 197
e2c9111c 198 splay_tree_insert (env.nodes_marked_new,
b8698a0f 199 (splay_tree_key)node->uid,
e2c9111c 200 (splay_tree_value)node);
b8698a0f
L
201 }
202 else
960bfb69 203 node->symbol.aux = NULL;
e2c9111c 204 }
ea900239
DB
205 result = splay_tree_min (env.nodes_marked_new);
206 while (result)
207 {
208 node = (struct cgraph_node *)result->value;
2505c5ed 209 searchc (&env, node, ignore_edge);
ea900239
DB
210 result = splay_tree_min (env.nodes_marked_new);
211 }
212 splay_tree_delete (env.nodes_marked_new);
213 free (env.stack);
214
215 return env.order_pos;
216}
217
af8bca3c
MJ
218/* Deallocate all ipa_dfs_info structures pointed to by the aux pointer of call
219 graph nodes. */
220
221void
222ipa_free_postorder_info (void)
223{
224 struct cgraph_node *node;
65c70e6b 225 FOR_EACH_DEFINED_FUNCTION (node)
af8bca3c
MJ
226 {
227 /* Get rid of the aux information. */
960bfb69 228 if (node->symbol.aux)
af8bca3c 229 {
960bfb69
JH
230 free (node->symbol.aux);
231 node->symbol.aux = NULL;
af8bca3c
MJ
232 }
233 }
234}
235
8775a18b
JH
236struct postorder_stack
237{
238 struct cgraph_node *node;
239 struct cgraph_edge *edge;
240 int ref;
241};
242
af8bca3c 243/* Fill array order with all nodes with output flag set in the reverse
39e2db00
JH
244 topological order. Return the number of elements in the array.
245 FIXME: While walking, consider aliases, too. */
af8bca3c
MJ
246
247int
248ipa_reverse_postorder (struct cgraph_node **order)
249{
250 struct cgraph_node *node, *node2;
251 int stack_size = 0;
252 int order_pos = 0;
8775a18b 253 struct cgraph_edge *edge;
af8bca3c 254 int pass;
8775a18b 255 struct ipa_ref *ref;
af8bca3c 256
8775a18b
JH
257 struct postorder_stack *stack =
258 XCNEWVEC (struct postorder_stack, cgraph_n_nodes);
af8bca3c
MJ
259
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. */
65c70e6b 264 FOR_EACH_FUNCTION (node)
960bfb69 265 node->symbol.aux = NULL;
af8bca3c 266 for (pass = 0; pass < 2; pass++)
65c70e6b 267 FOR_EACH_FUNCTION (node)
960bfb69 268 if (!node->symbol.aux
af8bca3c 269 && (pass
960bfb69 270 || (!node->symbol.address_taken
af8bca3c 271 && !node->global.inlined_to
8775a18b
JH
272 && !node->alias && !node->thunk.thunk_p
273 && !cgraph_only_called_directly_p (node))))
af8bca3c 274 {
8775a18b
JH
275 stack_size = 0;
276 stack[stack_size].node = node;
277 stack[stack_size].edge = node->callers;
278 stack[stack_size].ref = 0;
960bfb69 279 node->symbol.aux = (void *)(size_t)1;
8775a18b 280 while (stack_size >= 0)
af8bca3c 281 {
8775a18b 282 while (true)
af8bca3c 283 {
8775a18b
JH
284 node2 = NULL;
285 while (stack[stack_size].edge && !node2)
af8bca3c 286 {
8775a18b 287 edge = stack[stack_size].edge;
af8bca3c 288 node2 = edge->caller;
8775a18b
JH
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. */
960bfb69 293 if (DECL_DISREGARD_INLINE_LIMITS (edge->caller->symbol.decl)
8775a18b 294 && !DECL_DISREGARD_INLINE_LIMITS
960bfb69 295 (cgraph_function_node (edge->callee, NULL)->symbol.decl))
8775a18b
JH
296 node2 = NULL;
297 }
5932a4d4 298 for (;ipa_ref_list_referring_iterate (&stack[stack_size].node->symbol.ref_list,
8775a18b
JH
299 stack[stack_size].ref,
300 ref) && !node2;
301 stack[stack_size].ref++)
302 {
303 if (ref->use == IPA_REF_ALIAS)
5932a4d4 304 node2 = ipa_ref_referring_node (ref);
8775a18b
JH
305 }
306 if (!node2)
307 break;
960bfb69 308 if (!node2->symbol.aux)
8775a18b
JH
309 {
310 stack[++stack_size].node = node2;
311 stack[stack_size].edge = node2->callers;
312 stack[stack_size].ref = 0;
960bfb69 313 node2->symbol.aux = (void *)(size_t)1;
af8bca3c
MJ
314 }
315 }
8775a18b 316 order[order_pos++] = stack[stack_size--].node;
af8bca3c
MJ
317 }
318 }
319 free (stack);
65c70e6b 320 FOR_EACH_FUNCTION (node)
960bfb69 321 node->symbol.aux = NULL;
af8bca3c
MJ
322 return order_pos;
323}
324
325
ea900239
DB
326
327/* Given a memory reference T, will return the variable at the bottom
073a8998 328 of the access. Unlike get_base_address, this will recurse through
ea900239
DB
329 INDIRECT_REFS. */
330
331tree
332get_base_var (tree t)
333{
b8698a0f 334 while (!SSA_VAR_P (t)
ea900239
DB
335 && (!CONSTANT_CLASS_P (t))
336 && TREE_CODE (t) != LABEL_DECL
337 && TREE_CODE (t) != FUNCTION_DECL
3baf459d
DN
338 && TREE_CODE (t) != CONST_DECL
339 && TREE_CODE (t) != CONSTRUCTOR)
ea900239
DB
340 {
341 t = TREE_OPERAND (t, 0);
342 }
343 return t;
b8698a0f 344}
ea900239 345
1cb1a99f
JH
346
347/* Create a new cgraph node set. */
348
349cgraph_node_set
350cgraph_node_set_new (void)
351{
352 cgraph_node_set new_node_set;
353
354 new_node_set = XCNEW (struct cgraph_node_set_def);
355 new_node_set->map = pointer_map_create ();
356 new_node_set->nodes = NULL;
357 return new_node_set;
358}
359
360
361/* Add cgraph_node NODE to cgraph_node_set SET. */
362
363void
364cgraph_node_set_add (cgraph_node_set set, struct cgraph_node *node)
365{
366 void **slot;
367
368 slot = pointer_map_insert (set->map, node);
369
370 if (*slot)
371 {
372 int index = (size_t) *slot - 1;
373 gcc_checking_assert ((VEC_index (cgraph_node_ptr, set->nodes, index)
374 == node));
375 return;
376 }
377
378 *slot = (void *)(size_t) (VEC_length (cgraph_node_ptr, set->nodes) + 1);
379
380 /* Insert into node vector. */
381 VEC_safe_push (cgraph_node_ptr, heap, set->nodes, node);
382}
383
384
385/* Remove cgraph_node NODE from cgraph_node_set SET. */
386
387void
388cgraph_node_set_remove (cgraph_node_set set, struct cgraph_node *node)
389{
390 void **slot, **last_slot;
391 int index;
392 struct cgraph_node *last_node;
393
394 slot = pointer_map_contains (set->map, node);
395 if (slot == NULL || !*slot)
396 return;
397
398 index = (size_t) *slot - 1;
399 gcc_checking_assert (VEC_index (cgraph_node_ptr, set->nodes, index)
400 == node);
401
402 /* Remove from vector. We do this by swapping node with the last element
403 of the vector. */
404 last_node = VEC_pop (cgraph_node_ptr, set->nodes);
405 if (last_node != node)
406 {
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);
410
411 /* Move the last element to the original spot of NODE. */
412 VEC_replace (cgraph_node_ptr, set->nodes, index, last_node);
413 }
414
415 /* Remove element from hash table. */
416 *slot = NULL;
417}
418
419
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. */
422
423cgraph_node_set_iterator
424cgraph_node_set_find (cgraph_node_set set, struct cgraph_node *node)
425{
426 void **slot;
427 cgraph_node_set_iterator csi;
428
429 slot = pointer_map_contains (set->map, node);
430 if (slot == NULL || !*slot)
431 csi.index = (unsigned) ~0;
432 else
433 csi.index = (size_t)*slot - 1;
434 csi.set = set;
435
436 return csi;
437}
438
439
440/* Dump content of SET to file F. */
441
442void
443dump_cgraph_node_set (FILE *f, cgraph_node_set set)
444{
445 cgraph_node_set_iterator iter;
446
447 for (iter = csi_start (set); !csi_end_p (iter); csi_next (&iter))
448 {
449 struct cgraph_node *node = csi_node (iter);
450 fprintf (f, " %s/%i", cgraph_node_name (node), node->uid);
451 }
452 fprintf (f, "\n");
453}
454
455
456/* Dump content of SET to stderr. */
457
458DEBUG_FUNCTION void
459debug_cgraph_node_set (cgraph_node_set set)
460{
461 dump_cgraph_node_set (stderr, set);
462}
463
464
465/* Free varpool node set. */
466
467void
468free_cgraph_node_set (cgraph_node_set set)
469{
470 VEC_free (cgraph_node_ptr, heap, set->nodes);
471 pointer_map_destroy (set->map);
472 free (set);
473}
474
475
476/* Create a new varpool node set. */
477
478varpool_node_set
479varpool_node_set_new (void)
480{
481 varpool_node_set new_node_set;
482
483 new_node_set = XCNEW (struct varpool_node_set_def);
484 new_node_set->map = pointer_map_create ();
485 new_node_set->nodes = NULL;
486 return new_node_set;
487}
488
489
490/* Add varpool_node NODE to varpool_node_set SET. */
491
492void
493varpool_node_set_add (varpool_node_set set, struct varpool_node *node)
494{
495 void **slot;
496
497 slot = pointer_map_insert (set->map, node);
498
499 if (*slot)
500 {
501 int index = (size_t) *slot - 1;
502 gcc_checking_assert ((VEC_index (varpool_node_ptr, set->nodes, index)
503 == node));
504 return;
505 }
506
507 *slot = (void *)(size_t) (VEC_length (varpool_node_ptr, set->nodes) + 1);
508
509 /* Insert into node vector. */
510 VEC_safe_push (varpool_node_ptr, heap, set->nodes, node);
511}
512
513
514/* Remove varpool_node NODE from varpool_node_set SET. */
515
516void
517varpool_node_set_remove (varpool_node_set set, struct varpool_node *node)
518{
519 void **slot, **last_slot;
520 int index;
521 struct varpool_node *last_node;
522
523 slot = pointer_map_contains (set->map, node);
524 if (slot == NULL || !*slot)
525 return;
526
527 index = (size_t) *slot - 1;
528 gcc_checking_assert (VEC_index (varpool_node_ptr, set->nodes, index)
529 == node);
530
531 /* Remove from vector. We do this by swapping node with the last element
532 of the vector. */
533 last_node = VEC_pop (varpool_node_ptr, set->nodes);
534 if (last_node != node)
535 {
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);
539
540 /* Move the last element to the original spot of NODE. */
541 VEC_replace (varpool_node_ptr, set->nodes, index, last_node);
542 }
543
544 /* Remove element from hash table. */
545 *slot = NULL;
546}
547
548
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. */
551
552varpool_node_set_iterator
553varpool_node_set_find (varpool_node_set set, struct varpool_node *node)
554{
555 void **slot;
556 varpool_node_set_iterator vsi;
557
558 slot = pointer_map_contains (set->map, node);
559 if (slot == NULL || !*slot)
560 vsi.index = (unsigned) ~0;
561 else
562 vsi.index = (size_t)*slot - 1;
563 vsi.set = set;
564
565 return vsi;
566}
567
568
569/* Dump content of SET to file F. */
570
571void
572dump_varpool_node_set (FILE *f, varpool_node_set set)
573{
574 varpool_node_set_iterator iter;
575
576 for (iter = vsi_start (set); !vsi_end_p (iter); vsi_next (&iter))
577 {
578 struct varpool_node *node = vsi_node (iter);
579 fprintf (f, " %s", varpool_node_name (node));
580 }
581 fprintf (f, "\n");
582}
583
584
585/* Free varpool node set. */
586
587void
588free_varpool_node_set (varpool_node_set set)
589{
590 VEC_free (varpool_node_ptr, heap, set->nodes);
591 pointer_map_destroy (set->map);
592 free (set);
593}
594
595
596/* Dump content of SET to stderr. */
597
598DEBUG_FUNCTION void
599debug_varpool_node_set (varpool_node_set set)
600{
601 dump_varpool_node_set (stderr, set);
602}
This page took 2.432131 seconds and 5 git commands to generate.