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).
5 This file is part of GCC.
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)
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.
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
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.
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. */
39 #include "hard-reg-set.h"
40 #include "basic-block.h"
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. */
50 /* Type of Basic Block aka. TBB */
51 typedef unsigned int TBB
;
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. */
60 /* The parent of a node in the DFS tree. */
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
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]. */
69 /* bucket[x] points to the first node of the set of nodes having x as key. */
71 /* And next_bucket[x] points to the next node. */
73 /* After the algorithm is done, dom[x] contains the immediate dominator
77 /* The following few fields implement the structures needed for disjoint
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. */
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. */
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. */
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
;
98 /* This is the next free DFS number when creating the DFS tree or forest. */
100 /* The number of nodes in the DFS tree (==dfsnum-1). */
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
*,
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
*,
119 /* Helper macro for allocating and initializing an array,
120 for aesthetic reasons. */
121 #define init_ar(var, type, num, content) \
123 unsigned int i = 1; /* Catch content == i. */ \
125 (var) = (type *) xcalloc ((num), sizeof (type)); \
128 (var) = (type *) xmalloc ((num) * sizeof (type)); \
129 for (i = 0; i < num; i++) \
130 (var)[i] = (content); \
134 /* Allocate all needed memory in a pessimistic fashion (so we round up).
135 This initialises the contents of DI, which already must be allocated. */
141 /* We need memory for n_basic_blocks nodes and the ENTRY_BLOCK or
143 unsigned int num
= n_basic_blocks
+ 1 + 1;
144 init_ar (di
->dfs_parent
, TBB
, num
, 0);
145 init_ar (di
->path_min
, TBB
, num
, i
);
146 init_ar (di
->key
, TBB
, num
, i
);
147 init_ar (di
->dom
, TBB
, num
, 0);
149 init_ar (di
->bucket
, TBB
, num
, 0);
150 init_ar (di
->next_bucket
, TBB
, num
, 0);
152 init_ar (di
->set_chain
, TBB
, num
, 0);
153 init_ar (di
->set_size
, unsigned int, num
, 1);
154 init_ar (di
->set_child
, TBB
, num
, 0);
156 init_ar (di
->dfs_order
, TBB
, (unsigned int) n_basic_blocks
+ 1, 0);
157 init_ar (di
->dfs_to_bb
, basic_block
, num
, 0);
165 /* Free all allocated memory in DI, but not DI itself. */
171 free (di
->dfs_parent
);
176 free (di
->next_bucket
);
177 free (di
->set_chain
);
179 free (di
->set_child
);
180 free (di
->dfs_order
);
181 free (di
->dfs_to_bb
);
184 /* The nonrecursive variant of creating a DFS tree. DI is our working
185 structure, BB the starting basic block for this tree and REVERSE
186 is true, if predecessors should be visited instead of successors of a
187 node. After this is done all nodes reachable from BB were visited, have
188 assigned their dfs number and are linked together to form a tree. */
191 calc_dfs_tree_nonrec (di
, bb
, reverse
)
194 enum cdi_direction reverse
;
196 /* We never call this with bb==EXIT_BLOCK_PTR (ENTRY_BLOCK_PTR if REVERSE). */
197 /* We call this _only_ if bb is not already visited. */
199 TBB child_i
, my_i
= 0;
202 /* Start block (ENTRY_BLOCK_PTR for forward problem, EXIT_BLOCK for backward
204 basic_block en_block
;
206 basic_block ex_block
;
208 stack
= (edge
*) xmalloc ((n_basic_blocks
+ 3) * sizeof (edge
));
211 /* Initialize our border blocks, and the first edge. */
215 en_block
= EXIT_BLOCK_PTR
;
216 ex_block
= ENTRY_BLOCK_PTR
;
221 en_block
= ENTRY_BLOCK_PTR
;
222 ex_block
= EXIT_BLOCK_PTR
;
225 /* When the stack is empty we break out of this loop. */
230 /* This loop traverses edges e in depth first manner, and fills the
236 /* Deduce from E the current and the next block (BB and BN), and the
242 /* If the next node BN is either already visited or a border
243 block the current edge is useless, and simply overwritten
244 with the next edge out of the current node. */
245 if (bn
== ex_block
|| di
->dfs_order
[bn
->index
])
256 if (bn
== ex_block
|| di
->dfs_order
[bn
->index
])
268 /* Fill the DFS tree info calculatable _before_ recursing. */
270 my_i
= di
->dfs_order
[bb
->index
];
272 my_i
= di
->dfs_order
[n_basic_blocks
];
273 child_i
= di
->dfs_order
[bn
->index
] = di
->dfsnum
++;
274 di
->dfs_to_bb
[child_i
] = bn
;
275 di
->dfs_parent
[child_i
] = my_i
;
277 /* Save the current point in the CFG on the stack, and recurse. */
286 /* OK. The edge-list was exhausted, meaning normally we would
287 end the recursion. After returning from the recursive call,
288 there were (may be) other statements which were run after a
289 child node was completely considered by DFS. Here is the
290 point to do it in the non-recursive variant.
291 E.g. The block just completed is in e->dest for forward DFS,
292 the block not yet completed (the parent of the one above)
293 in e->src. This could be used e.g. for computing the number of
294 descendants or the tree depth. */
303 /* The main entry for calculating the DFS tree or forest. DI is our working
304 structure and REVERSE is true, if we are interested in the reverse flow
305 graph. In that case the result is not necessarily a tree but a forest,
306 because there may be nodes from which the EXIT_BLOCK is unreachable. */
309 calc_dfs_tree (di
, reverse
)
311 enum cdi_direction reverse
;
313 /* The first block is the ENTRY_BLOCK (or EXIT_BLOCK if REVERSE). */
314 basic_block begin
= reverse
? EXIT_BLOCK_PTR
: ENTRY_BLOCK_PTR
;
315 di
->dfs_order
[n_basic_blocks
] = di
->dfsnum
;
316 di
->dfs_to_bb
[di
->dfsnum
] = begin
;
319 calc_dfs_tree_nonrec (di
, begin
, reverse
);
323 /* In the post-dom case we may have nodes without a path to EXIT_BLOCK.
324 They are reverse-unreachable. In the dom-case we disallow such
325 nodes, but in post-dom we have to deal with them, so we simply
326 include them in the DFS tree which actually becomes a forest. */
328 for (i
= n_basic_blocks
- 1; i
>= 0; i
--)
330 basic_block b
= BASIC_BLOCK (i
);
331 if (di
->dfs_order
[b
->index
])
333 di
->dfs_order
[b
->index
] = di
->dfsnum
;
334 di
->dfs_to_bb
[di
->dfsnum
] = b
;
336 calc_dfs_tree_nonrec (di
, b
, reverse
);
340 di
->nodes
= di
->dfsnum
- 1;
342 /* This aborts e.g. when there is _no_ path from ENTRY to EXIT at all. */
343 if (di
->nodes
!= (unsigned int) n_basic_blocks
+ 1)
347 /* Compress the path from V to the root of its set and update path_min at the
348 same time. After compress(di, V) set_chain[V] is the root of the set V is
349 in and path_min[V] is the node with the smallest key[] value on the path
350 from V to that root. */
357 /* Btw. It's not worth to unrecurse compress() as the depth is usually not
358 greater than 5 even for huge graphs (I've not seen call depth > 4).
359 Also performance wise compress() ranges _far_ behind eval(). */
360 TBB parent
= di
->set_chain
[v
];
361 if (di
->set_chain
[parent
])
363 compress (di
, parent
);
364 if (di
->key
[di
->path_min
[parent
]] < di
->key
[di
->path_min
[v
]])
365 di
->path_min
[v
] = di
->path_min
[parent
];
366 di
->set_chain
[v
] = di
->set_chain
[parent
];
370 /* Compress the path from V to the set root of V if needed (when the root has
371 changed since the last call). Returns the node with the smallest key[]
372 value on the path from V to the root. */
379 /* The representant of the set V is in, also called root (as the set
380 representation is a tree). */
381 TBB rep
= di
->set_chain
[v
];
383 /* V itself is the root. */
385 return di
->path_min
[v
];
387 /* Compress only if necessary. */
388 if (di
->set_chain
[rep
])
391 rep
= di
->set_chain
[v
];
394 if (di
->key
[di
->path_min
[rep
]] >= di
->key
[di
->path_min
[v
]])
395 return di
->path_min
[v
];
397 return di
->path_min
[rep
];
400 /* This essentially merges the two sets of V and W, giving a single set with
401 the new root V. The internal representation of these disjoint sets is a
402 balanced tree. Currently link(V,W) is only used with V being the parent
406 link_roots (di
, v
, w
)
412 /* Rebalance the tree. */
413 while (di
->key
[di
->path_min
[w
]] < di
->key
[di
->path_min
[di
->set_child
[s
]]])
415 if (di
->set_size
[s
] + di
->set_size
[di
->set_child
[di
->set_child
[s
]]]
416 >= 2 * di
->set_size
[di
->set_child
[s
]])
418 di
->set_chain
[di
->set_child
[s
]] = s
;
419 di
->set_child
[s
] = di
->set_child
[di
->set_child
[s
]];
423 di
->set_size
[di
->set_child
[s
]] = di
->set_size
[s
];
424 s
= di
->set_chain
[s
] = di
->set_child
[s
];
428 di
->path_min
[s
] = di
->path_min
[w
];
429 di
->set_size
[v
] += di
->set_size
[w
];
430 if (di
->set_size
[v
] < 2 * di
->set_size
[w
])
433 s
= di
->set_child
[v
];
434 di
->set_child
[v
] = tmp
;
437 /* Merge all subtrees. */
440 di
->set_chain
[s
] = v
;
441 s
= di
->set_child
[s
];
445 /* This calculates the immediate dominators (or post-dominators if REVERSE is
446 true). DI is our working structure and should hold the DFS forest.
447 On return the immediate dominator to node V is in di->dom[V]. */
450 calc_idoms (di
, reverse
)
452 enum cdi_direction reverse
;
455 basic_block en_block
;
457 en_block
= EXIT_BLOCK_PTR
;
459 en_block
= ENTRY_BLOCK_PTR
;
461 /* Go backwards in DFS order, to first look at the leafs. */
465 basic_block bb
= di
->dfs_to_bb
[v
];
468 par
= di
->dfs_parent
[v
];
475 /* Search all direct predecessors for the smallest node with a path
476 to them. That way we have the smallest node with also a path to
477 us only over nodes behind us. In effect we search for our
479 for (; e
; e
= e_next
)
487 e_next
= e
->succ_next
;
492 e_next
= e
->pred_next
;
495 k1
= di
->dfs_order
[n_basic_blocks
];
497 k1
= di
->dfs_order
[b
->index
];
499 /* Call eval() only if really needed. If k1 is above V in DFS tree,
500 then we know, that eval(k1) == k1 and key[k1] == k1. */
502 k1
= di
->key
[eval (di
, k1
)];
508 link_roots (di
, par
, v
);
509 di
->next_bucket
[v
] = di
->bucket
[k
];
512 /* Transform semidominators into dominators. */
513 for (w
= di
->bucket
[par
]; w
; w
= di
->next_bucket
[w
])
516 if (di
->key
[k
] < di
->key
[w
])
521 /* We don't need to cleanup next_bucket[]. */
526 /* Explicitly define the dominators. */
528 for (v
= 2; v
<= di
->nodes
; v
++)
529 if (di
->dom
[v
] != di
->key
[v
])
530 di
->dom
[v
] = di
->dom
[di
->dom
[v
]];
533 /* Convert the information about immediate dominators (in DI) to sets of all
534 dominators (in DOMINATORS). */
537 idoms_to_doms (di
, dominators
)
543 sbitmap_vector_zero (dominators
, n_basic_blocks
);
544 /* We have to be careful, to not include the ENTRY_BLOCK or EXIT_BLOCK
545 in the list of (post)-doms, so remember that in e_index. */
546 e_index
= di
->dfs_order
[n_basic_blocks
];
548 for (i
= 1; i
<= di
->nodes
; i
++)
552 bb
= di
->dfs_to_bb
[i
]->index
;
554 if (di
->dom
[i
] && (di
->dom
[i
] != e_index
))
556 bb_idom
= di
->dfs_to_bb
[di
->dom
[i
]]->index
;
557 sbitmap_copy (dominators
[bb
], dominators
[bb_idom
]);
561 /* It has no immediate dom or only ENTRY_BLOCK or EXIT_BLOCK.
562 If it is a child of ENTRY_BLOCK that's OK, and it's only
563 dominated by itself; if it's _not_ a child of ENTRY_BLOCK, it
564 means, it is unreachable. That case has been disallowed in the
565 building of the DFS tree, so we are save here. For the reverse
566 flow graph it means, it has no children, so, to be compatible
567 with the old code, we set the post_dominators to all one. */
570 sbitmap_ones (dominators
[bb
]);
573 SET_BIT (dominators
[bb
], bb
);
577 /* The main entry point into this module. IDOM is an integer array with room
578 for n_basic_blocks integers, DOMS is a preallocated sbitmap array having
579 room for n_basic_blocks^2 bits, and POST is true if the caller wants to
580 know post-dominators.
582 On return IDOM[i] will be the BB->index of the immediate (post) dominator
583 of basic block i, and DOMS[i] will have set bit j if basic block j is a
584 (post)dominator for block i.
586 Either IDOM or DOMS may be NULL (meaning the caller is not interested in
587 immediate resp. all dominators). */
590 calculate_dominance_info (idom
, doms
, reverse
)
593 enum cdi_direction reverse
;
600 calc_dfs_tree (&di
, reverse
);
601 calc_idoms (&di
, reverse
);
606 for (i
= 0; i
< n_basic_blocks
; i
++)
608 basic_block b
= BASIC_BLOCK (i
);
609 TBB d
= di
.dom
[di
.dfs_order
[b
->index
]];
611 /* The old code didn't modify array elements of nodes having only
612 itself as dominator (d==0) or only ENTRY_BLOCK (resp. EXIT_BLOCK)
615 idom
[i
] = di
.dfs_to_bb
[d
]->index
;
619 idoms_to_doms (&di
, doms
);