Revert "Basic block renumbering removal", and two followup patches.
[gcc.git] / gcc / dominance.c
1 /* Calculate (post)dominators in slightly super-linear time.
2 Copyright (C) 2000 Free Software Foundation, Inc.
3 Contributed by Michael Matz (matz@ifh.de).
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 /* This file implements the well known algorithm from Lengauer and Tarjan
23 to compute the dominators in a control flow graph. A basic block D is said
24 to dominate another block X, when all paths from the entry node of the CFG
25 to X go also over D. The dominance relation is a transitive reflexive
26 relation and its minimal transitive reduction is a tree, called the
27 dominator tree. So for each block X besides the entry block exists a
28 block I(X), called the immediate dominator of X, which is the parent of X
29 in the dominator tree.
30
31 The algorithm computes this dominator tree implicitly by computing for
32 each block its immediate dominator. We use tree balancing and path
33 compression, so its the O(e*a(e,v)) variant, where a(e,v) is the very
34 slowly growing functional inverse of the Ackerman function. */
35
36 #include "config.h"
37 #include "system.h"
38 #include "rtl.h"
39 #include "hard-reg-set.h"
40 #include "basic-block.h"
41
42
43 /* We name our nodes with integers, beginning with 1. Zero is reserved for
44 'undefined' or 'end of list'. The name of each node is given by the dfs
45 number of the corresponding basic block. Please note, that we include the
46 artificial ENTRY_BLOCK (or EXIT_BLOCK in the post-dom case) in our lists to
47 support multiple entry points. As it has no real basic block index we use
48 'n_basic_blocks' for that. Its dfs number is of course 1. */
49
50 /* Type of Basic Block aka. TBB */
51 typedef unsigned int TBB;
52
53 /* We work in a poor-mans object oriented fashion, and carry an instance of
54 this structure through all our 'methods'. It holds various arrays
55 reflecting the (sub)structure of the flowgraph. Most of them are of type
56 TBB and are also indexed by TBB. */
57
58 struct dom_info
59 {
60 /* The parent of a node in the DFS tree. */
61 TBB *dfs_parent;
62 /* For a node x key[x] is roughly the node nearest to the root from which
63 exists a way to x only over nodes behind x. Such a node is also called
64 semidominator. */
65 TBB *key;
66 /* The value in path_min[x] is the node y on the path from x to the root of
67 the tree x is in with the smallest key[y]. */
68 TBB *path_min;
69 /* bucket[x] points to the first node of the set of nodes having x as key. */
70 TBB *bucket;
71 /* And next_bucket[x] points to the next node. */
72 TBB *next_bucket;
73 /* After the algorithm is done, dom[x] contains the immediate dominator
74 of x. */
75 TBB *dom;
76
77 /* The following few fields implement the structures needed for disjoint
78 sets. */
79 /* set_chain[x] is the next node on the path from x to the representant
80 of the set containing x. If set_chain[x]==0 then x is a root. */
81 TBB *set_chain;
82 /* set_size[x] is the number of elements in the set named by x. */
83 unsigned int *set_size;
84 /* set_child[x] is used for balancing the tree representing a set. It can
85 be understood as the next sibling of x. */
86 TBB *set_child;
87
88 /* If b is the number of a basic block (BB->index), dfs_order[b] is the
89 number of that node in DFS order counted from 1. This is an index
90 into most of the other arrays in this structure. */
91 TBB *dfs_order;
92 /* If x is the DFS-index of a node which corresponds with an basic block,
93 dfs_to_bb[x] is that basic block. Note, that in our structure there are
94 more nodes that basic blocks, so only dfs_to_bb[dfs_order[bb->index]]==bb
95 is true for every basic block bb, but not the opposite. */
96 basic_block *dfs_to_bb;
97
98 /* This is the next free DFS number when creating the DFS tree or forest. */
99 unsigned int dfsnum;
100 /* The number of nodes in the DFS tree (==dfsnum-1). */
101 unsigned int nodes;
102 };
103
104 static void init_dom_info PARAMS ((struct dom_info *));
105 static void free_dom_info PARAMS ((struct dom_info *));
106 static void calc_dfs_tree_nonrec PARAMS ((struct dom_info *,
107 basic_block,
108 enum cdi_direction));
109 static void calc_dfs_tree PARAMS ((struct dom_info *,
110 enum cdi_direction));
111 static void compress PARAMS ((struct dom_info *, TBB));
112 static TBB eval PARAMS ((struct dom_info *, TBB));
113 static void link_roots PARAMS ((struct dom_info *, TBB, TBB));
114 static void calc_idoms PARAMS ((struct dom_info *,
115 enum cdi_direction));
116 static void idoms_to_doms PARAMS ((struct dom_info *,
117 sbitmap *));
118
119 /* Helper macro for allocating and initializing an array,
120 for aesthetic reasons. */
121 #define init_ar(var, type, num, content) \
122 do \
123 { \
124 unsigned int i = 1; /* Catch content == i. */ \
125 if (! (content)) \
126 (var) = (type *) xcalloc ((num), sizeof (type)); \
127 else \
128 { \
129 (var) = (type *) xmalloc ((num) * sizeof (type)); \
130 for (i = 0; i < num; i++) \
131 (var)[i] = (content); \
132 } \
133 } \
134 while (0)
135
136 /* Allocate all needed memory in a pessimistic fashion (so we round up).
137 This initialises the contents of DI, which already must be allocated. */
138
139 static void
140 init_dom_info (di)
141 struct dom_info *di;
142 {
143 /* We need memory for n_basic_blocks nodes and the ENTRY_BLOCK or
144 EXIT_BLOCK. */
145 unsigned int num = n_basic_blocks + 1 + 1;
146 init_ar (di->dfs_parent, TBB, num, 0);
147 init_ar (di->path_min, TBB, num, i);
148 init_ar (di->key, TBB, num, i);
149 init_ar (di->dom, TBB, num, 0);
150
151 init_ar (di->bucket, TBB, num, 0);
152 init_ar (di->next_bucket, TBB, num, 0);
153
154 init_ar (di->set_chain, TBB, num, 0);
155 init_ar (di->set_size, unsigned int, num, 1);
156 init_ar (di->set_child, TBB, num, 0);
157
158 init_ar (di->dfs_order, TBB, (unsigned int) n_basic_blocks + 1, 0);
159 init_ar (di->dfs_to_bb, basic_block, num, 0);
160
161 di->dfsnum = 1;
162 di->nodes = 0;
163 }
164
165 #undef init_ar
166
167 /* Free all allocated memory in DI, but not DI itself. */
168
169 static void
170 free_dom_info (di)
171 struct dom_info *di;
172 {
173 free (di->dfs_parent);
174 free (di->path_min);
175 free (di->key);
176 free (di->dom);
177 free (di->bucket);
178 free (di->next_bucket);
179 free (di->set_chain);
180 free (di->set_size);
181 free (di->set_child);
182 free (di->dfs_order);
183 free (di->dfs_to_bb);
184 }
185
186 /* The nonrecursive variant of creating a DFS tree. DI is our working
187 structure, BB the starting basic block for this tree and REVERSE
188 is true, if predecessors should be visited instead of successors of a
189 node. After this is done all nodes reachable from BB were visited, have
190 assigned their dfs number and are linked together to form a tree. */
191
192 static void
193 calc_dfs_tree_nonrec (di, bb, reverse)
194 struct dom_info *di;
195 basic_block bb;
196 enum cdi_direction reverse;
197 {
198 /* We never call this with bb==EXIT_BLOCK_PTR (ENTRY_BLOCK_PTR if REVERSE). */
199 /* We call this _only_ if bb is not already visited. */
200 edge e;
201 TBB child_i, my_i = 0;
202 edge *stack;
203 int sp;
204 /* Start block (ENTRY_BLOCK_PTR for forward problem, EXIT_BLOCK for backward
205 problem). */
206 basic_block en_block;
207 /* Ending block. */
208 basic_block ex_block;
209
210 stack = (edge *) xmalloc ((n_basic_blocks + 3) * sizeof (edge));
211 sp = 0;
212
213 /* Initialize our border blocks, and the first edge. */
214 if (reverse)
215 {
216 e = bb->pred;
217 en_block = EXIT_BLOCK_PTR;
218 ex_block = ENTRY_BLOCK_PTR;
219 }
220 else
221 {
222 e = bb->succ;
223 en_block = ENTRY_BLOCK_PTR;
224 ex_block = EXIT_BLOCK_PTR;
225 }
226
227 /* When the stack is empty we break out of this loop. */
228 while (1)
229 {
230 basic_block bn;
231
232 /* This loop traverses edges e in depth first manner, and fills the
233 stack. */
234 while (e)
235 {
236 edge e_next;
237
238 /* Deduce from E the current and the next block (BB and BN), and the
239 next edge. */
240 if (reverse)
241 {
242 bn = e->src;
243
244 /* If the next node BN is either already visited or a border
245 block the current edge is useless, and simply overwritten
246 with the next edge out of the current node. */
247 if (bn == ex_block || di->dfs_order[bn->index])
248 {
249 e = e->pred_next;
250 continue;
251 }
252 bb = e->dest;
253 e_next = bn->pred;
254 }
255 else
256 {
257 bn = e->dest;
258 if (bn == ex_block || di->dfs_order[bn->index])
259 {
260 e = e->succ_next;
261 continue;
262 }
263 bb = e->src;
264 e_next = bn->succ;
265 }
266
267 if (bn == en_block)
268 abort ();
269
270 /* Fill the DFS tree info calculatable _before_ recursing. */
271 if (bb != en_block)
272 my_i = di->dfs_order[bb->index];
273 else
274 my_i = di->dfs_order[n_basic_blocks];
275 child_i = di->dfs_order[bn->index] = di->dfsnum++;
276 di->dfs_to_bb[child_i] = bn;
277 di->dfs_parent[child_i] = my_i;
278
279 /* Save the current point in the CFG on the stack, and recurse. */
280 stack[sp++] = e;
281 e = e_next;
282 }
283
284 if (!sp)
285 break;
286 e = stack[--sp];
287
288 /* OK. The edge-list was exhausted, meaning normally we would
289 end the recursion. After returning from the recursive call,
290 there were (may be) other statements which were run after a
291 child node was completely considered by DFS. Here is the
292 point to do it in the non-recursive variant.
293 E.g. The block just completed is in e->dest for forward DFS,
294 the block not yet completed (the parent of the one above)
295 in e->src. This could be used e.g. for computing the number of
296 descendants or the tree depth. */
297 if (reverse)
298 e = e->pred_next;
299 else
300 e = e->succ_next;
301 }
302 free (stack);
303 }
304
305 /* The main entry for calculating the DFS tree or forest. DI is our working
306 structure and REVERSE is true, if we are interested in the reverse flow
307 graph. In that case the result is not necessarily a tree but a forest,
308 because there may be nodes from which the EXIT_BLOCK is unreachable. */
309
310 static void
311 calc_dfs_tree (di, reverse)
312 struct dom_info *di;
313 enum cdi_direction reverse;
314 {
315 /* The first block is the ENTRY_BLOCK (or EXIT_BLOCK if REVERSE). */
316 basic_block begin = reverse ? EXIT_BLOCK_PTR : ENTRY_BLOCK_PTR;
317 di->dfs_order[n_basic_blocks] = di->dfsnum;
318 di->dfs_to_bb[di->dfsnum] = begin;
319 di->dfsnum++;
320
321 calc_dfs_tree_nonrec (di, begin, reverse);
322
323 if (reverse)
324 {
325 /* In the post-dom case we may have nodes without a path to EXIT_BLOCK.
326 They are reverse-unreachable. In the dom-case we disallow such
327 nodes, but in post-dom we have to deal with them, so we simply
328 include them in the DFS tree which actually becomes a forest. */
329 int i;
330 for (i = n_basic_blocks - 1; i >= 0; i--)
331 {
332 basic_block b = BASIC_BLOCK (i);
333 if (di->dfs_order[b->index])
334 continue;
335 di->dfs_order[b->index] = di->dfsnum;
336 di->dfs_to_bb[di->dfsnum] = b;
337 di->dfsnum++;
338 calc_dfs_tree_nonrec (di, b, reverse);
339 }
340 }
341
342 di->nodes = di->dfsnum - 1;
343
344 /* This aborts e.g. when there is _no_ path from ENTRY to EXIT at all. */
345 if (di->nodes != (unsigned int) n_basic_blocks + 1)
346 abort ();
347 }
348
349 /* Compress the path from V to the root of its set and update path_min at the
350 same time. After compress(di, V) set_chain[V] is the root of the set V is
351 in and path_min[V] is the node with the smallest key[] value on the path
352 from V to that root. */
353
354 static void
355 compress (di, v)
356 struct dom_info *di;
357 TBB v;
358 {
359 /* Btw. It's not worth to unrecurse compress() as the depth is usually not
360 greater than 5 even for huge graphs (I've not seen call depth > 4).
361 Also performance wise compress() ranges _far_ behind eval(). */
362 TBB parent = di->set_chain[v];
363 if (di->set_chain[parent])
364 {
365 compress (di, parent);
366 if (di->key[di->path_min[parent]] < di->key[di->path_min[v]])
367 di->path_min[v] = di->path_min[parent];
368 di->set_chain[v] = di->set_chain[parent];
369 }
370 }
371
372 /* Compress the path from V to the set root of V if needed (when the root has
373 changed since the last call). Returns the node with the smallest key[]
374 value on the path from V to the root. */
375
376 static inline TBB
377 eval (di, v)
378 struct dom_info *di;
379 TBB v;
380 {
381 /* The representant of the set V is in, also called root (as the set
382 representation is a tree). */
383 TBB rep = di->set_chain[v];
384
385 /* V itself is the root. */
386 if (!rep)
387 return di->path_min[v];
388
389 /* Compress only if necessary. */
390 if (di->set_chain[rep])
391 {
392 compress (di, v);
393 rep = di->set_chain[v];
394 }
395
396 if (di->key[di->path_min[rep]] >= di->key[di->path_min[v]])
397 return di->path_min[v];
398 else
399 return di->path_min[rep];
400 }
401
402 /* This essentially merges the two sets of V and W, giving a single set with
403 the new root V. The internal representation of these disjoint sets is a
404 balanced tree. Currently link(V,W) is only used with V being the parent
405 of W. */
406
407 static void
408 link_roots (di, v, w)
409 struct dom_info *di;
410 TBB v, w;
411 {
412 TBB s = w;
413
414 /* Rebalance the tree. */
415 while (di->key[di->path_min[w]] < di->key[di->path_min[di->set_child[s]]])
416 {
417 if (di->set_size[s] + di->set_size[di->set_child[di->set_child[s]]]
418 >= 2 * di->set_size[di->set_child[s]])
419 {
420 di->set_chain[di->set_child[s]] = s;
421 di->set_child[s] = di->set_child[di->set_child[s]];
422 }
423 else
424 {
425 di->set_size[di->set_child[s]] = di->set_size[s];
426 s = di->set_chain[s] = di->set_child[s];
427 }
428 }
429
430 di->path_min[s] = di->path_min[w];
431 di->set_size[v] += di->set_size[w];
432 if (di->set_size[v] < 2 * di->set_size[w])
433 {
434 TBB tmp = s;
435 s = di->set_child[v];
436 di->set_child[v] = tmp;
437 }
438
439 /* Merge all subtrees. */
440 while (s)
441 {
442 di->set_chain[s] = v;
443 s = di->set_child[s];
444 }
445 }
446
447 /* This calculates the immediate dominators (or post-dominators if REVERSE is
448 true). DI is our working structure and should hold the DFS forest.
449 On return the immediate dominator to node V is in di->dom[V]. */
450
451 static void
452 calc_idoms (di, reverse)
453 struct dom_info *di;
454 enum cdi_direction reverse;
455 {
456 TBB v, w, k, par;
457 basic_block en_block;
458 if (reverse)
459 en_block = EXIT_BLOCK_PTR;
460 else
461 en_block = ENTRY_BLOCK_PTR;
462
463 /* Go backwards in DFS order, to first look at the leafs. */
464 v = di->nodes;
465 while (v > 1)
466 {
467 basic_block bb = di->dfs_to_bb[v];
468 edge e, e_next;
469
470 par = di->dfs_parent[v];
471 k = v;
472 if (reverse)
473 e = bb->succ;
474 else
475 e = bb->pred;
476
477 /* Search all direct predecessors for the smallest node with a path
478 to them. That way we have the smallest node with also a path to
479 us only over nodes behind us. In effect we search for our
480 semidominator. */
481 for (; e; e = e_next)
482 {
483 TBB k1;
484 basic_block b;
485
486 if (reverse)
487 {
488 b = e->dest;
489 e_next = e->succ_next;
490 }
491 else
492 {
493 b = e->src;
494 e_next = e->pred_next;
495 }
496 if (b == en_block)
497 k1 = di->dfs_order[n_basic_blocks];
498 else
499 k1 = di->dfs_order[b->index];
500
501 /* Call eval() only if really needed. If k1 is above V in DFS tree,
502 then we know, that eval(k1) == k1 and key[k1] == k1. */
503 if (k1 > v)
504 k1 = di->key[eval (di, k1)];
505 if (k1 < k)
506 k = k1;
507 }
508
509 di->key[v] = k;
510 link_roots (di, par, v);
511 di->next_bucket[v] = di->bucket[k];
512 di->bucket[k] = v;
513
514 /* Transform semidominators into dominators. */
515 for (w = di->bucket[par]; w; w = di->next_bucket[w])
516 {
517 k = eval (di, w);
518 if (di->key[k] < di->key[w])
519 di->dom[w] = k;
520 else
521 di->dom[w] = par;
522 }
523 /* We don't need to cleanup next_bucket[]. */
524 di->bucket[par] = 0;
525 v--;
526 }
527
528 /* Explicitly define the dominators. */
529 di->dom[1] = 0;
530 for (v = 2; v <= di->nodes; v++)
531 if (di->dom[v] != di->key[v])
532 di->dom[v] = di->dom[di->dom[v]];
533 }
534
535 /* Convert the information about immediate dominators (in DI) to sets of all
536 dominators (in DOMINATORS). */
537
538 static void
539 idoms_to_doms (di, dominators)
540 struct dom_info *di;
541 sbitmap *dominators;
542 {
543 TBB i, e_index;
544 int bb, bb_idom;
545 sbitmap_vector_zero (dominators, n_basic_blocks);
546 /* We have to be careful, to not include the ENTRY_BLOCK or EXIT_BLOCK
547 in the list of (post)-doms, so remember that in e_index. */
548 e_index = di->dfs_order[n_basic_blocks];
549
550 for (i = 1; i <= di->nodes; i++)
551 {
552 if (i == e_index)
553 continue;
554 bb = di->dfs_to_bb[i]->index;
555
556 if (di->dom[i] && (di->dom[i] != e_index))
557 {
558 bb_idom = di->dfs_to_bb[di->dom[i]]->index;
559 sbitmap_copy (dominators[bb], dominators[bb_idom]);
560 }
561 else
562 {
563 /* It has no immediate dom or only ENTRY_BLOCK or EXIT_BLOCK.
564 If it is a child of ENTRY_BLOCK that's OK, and it's only
565 dominated by itself; if it's _not_ a child of ENTRY_BLOCK, it
566 means, it is unreachable. That case has been disallowed in the
567 building of the DFS tree, so we are save here. For the reverse
568 flow graph it means, it has no children, so, to be compatible
569 with the old code, we set the post_dominators to all one. */
570 if (!di->dom[i])
571 {
572 sbitmap_ones (dominators[bb]);
573 }
574 }
575 SET_BIT (dominators[bb], bb);
576 }
577 }
578
579 /* The main entry point into this module. IDOM is an integer array with room
580 for n_basic_blocks integers, DOMS is a preallocated sbitmap array having
581 room for n_basic_blocks^2 bits, and POST is true if the caller wants to
582 know post-dominators.
583
584 On return IDOM[i] will be the BB->index of the immediate (post) dominator
585 of basic block i, and DOMS[i] will have set bit j if basic block j is a
586 (post)dominator for block i.
587
588 Either IDOM or DOMS may be NULL (meaning the caller is not interested in
589 immediate resp. all dominators). */
590
591 void
592 calculate_dominance_info (idom, doms, reverse)
593 int *idom;
594 sbitmap *doms;
595 enum cdi_direction reverse;
596 {
597 struct dom_info di;
598
599 if (!doms && !idom)
600 return;
601 init_dom_info (&di);
602 calc_dfs_tree (&di, reverse);
603 calc_idoms (&di, reverse);
604
605 if (idom)
606 {
607 int i;
608 for (i = 0; i < n_basic_blocks; i++)
609 {
610 basic_block b = BASIC_BLOCK (i);
611 TBB d = di.dom[di.dfs_order[b->index]];
612
613 /* The old code didn't modify array elements of nodes having only
614 itself as dominator (d==0) or only ENTRY_BLOCK (resp. EXIT_BLOCK)
615 (d==1). */
616 if (d > 1)
617 idom[i] = di.dfs_to_bb[d]->index;
618 }
619 }
620 if (doms)
621 idoms_to_doms (&di, doms);
622
623 free_dom_info (&di);
624 }