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