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 'last_basic_block' 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) \
124 unsigned int i = 1; /* Catch content == i. */ \
126 (var) = (type *) xcalloc ((num), sizeof (type)); \
129 (var) = (type *) xmalloc ((num) * sizeof (type)); \
130 for (i = 0; i < num; i++) \
131 (var)[i] = (content); \
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. */
143 /* We need memory for n_basic_blocks nodes and the ENTRY_BLOCK or
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);
151 init_ar (di
->bucket
, TBB
, num
, 0);
152 init_ar (di
->next_bucket
, TBB
, num
, 0);
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);
158 init_ar (di
->dfs_order
, TBB
, (unsigned int) last_basic_block
+ 1, 0);
159 init_ar (di
->dfs_to_bb
, basic_block
, num
, 0);
167 /* Free all allocated memory in DI, but not DI itself. */
173 free (di
->dfs_parent
);
178 free (di
->next_bucket
);
179 free (di
->set_chain
);
181 free (di
->set_child
);
182 free (di
->dfs_order
);
183 free (di
->dfs_to_bb
);
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. */
193 calc_dfs_tree_nonrec (di
, bb
, reverse
)
196 enum cdi_direction reverse
;
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. */
201 TBB child_i
, my_i
= 0;
204 /* Start block (ENTRY_BLOCK_PTR for forward problem, EXIT_BLOCK for backward
206 basic_block en_block
;
208 basic_block ex_block
;
210 stack
= (edge
*) xmalloc ((n_basic_blocks
+ 3) * sizeof (edge
));
213 /* Initialize our border blocks, and the first edge. */
217 en_block
= EXIT_BLOCK_PTR
;
218 ex_block
= ENTRY_BLOCK_PTR
;
223 en_block
= ENTRY_BLOCK_PTR
;
224 ex_block
= EXIT_BLOCK_PTR
;
227 /* When the stack is empty we break out of this loop. */
232 /* This loop traverses edges e in depth first manner, and fills the
238 /* Deduce from E the current and the next block (BB and BN), and the
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
])
258 if (bn
== ex_block
|| di
->dfs_order
[bn
->index
])
270 /* Fill the DFS tree info calculatable _before_ recursing. */
272 my_i
= di
->dfs_order
[bb
->index
];
274 my_i
= di
->dfs_order
[last_basic_block
];
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
;
279 /* Save the current point in the CFG on the stack, and recurse. */
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. */
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. */
311 calc_dfs_tree (di
, reverse
)
313 enum cdi_direction reverse
;
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
[last_basic_block
] = di
->dfsnum
;
318 di
->dfs_to_bb
[di
->dfsnum
] = begin
;
321 calc_dfs_tree_nonrec (di
, begin
, reverse
);
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. */
330 FOR_EACH_BB_REVERSE (b
)
332 if (di
->dfs_order
[b
->index
])
334 di
->dfs_order
[b
->index
] = di
->dfsnum
;
335 di
->dfs_to_bb
[di
->dfsnum
] = b
;
337 calc_dfs_tree_nonrec (di
, b
, reverse
);
341 di
->nodes
= di
->dfsnum
- 1;
343 /* This aborts e.g. when there is _no_ path from ENTRY to EXIT at all. */
344 if (di
->nodes
!= (unsigned int) n_basic_blocks
+ 1)
348 /* Compress the path from V to the root of its set and update path_min at the
349 same time. After compress(di, V) set_chain[V] is the root of the set V is
350 in and path_min[V] is the node with the smallest key[] value on the path
351 from V to that root. */
358 /* Btw. It's not worth to unrecurse compress() as the depth is usually not
359 greater than 5 even for huge graphs (I've not seen call depth > 4).
360 Also performance wise compress() ranges _far_ behind eval(). */
361 TBB parent
= di
->set_chain
[v
];
362 if (di
->set_chain
[parent
])
364 compress (di
, parent
);
365 if (di
->key
[di
->path_min
[parent
]] < di
->key
[di
->path_min
[v
]])
366 di
->path_min
[v
] = di
->path_min
[parent
];
367 di
->set_chain
[v
] = di
->set_chain
[parent
];
371 /* Compress the path from V to the set root of V if needed (when the root has
372 changed since the last call). Returns the node with the smallest key[]
373 value on the path from V to the root. */
380 /* The representant of the set V is in, also called root (as the set
381 representation is a tree). */
382 TBB rep
= di
->set_chain
[v
];
384 /* V itself is the root. */
386 return di
->path_min
[v
];
388 /* Compress only if necessary. */
389 if (di
->set_chain
[rep
])
392 rep
= di
->set_chain
[v
];
395 if (di
->key
[di
->path_min
[rep
]] >= di
->key
[di
->path_min
[v
]])
396 return di
->path_min
[v
];
398 return di
->path_min
[rep
];
401 /* This essentially merges the two sets of V and W, giving a single set with
402 the new root V. The internal representation of these disjoint sets is a
403 balanced tree. Currently link(V,W) is only used with V being the parent
407 link_roots (di
, v
, w
)
413 /* Rebalance the tree. */
414 while (di
->key
[di
->path_min
[w
]] < di
->key
[di
->path_min
[di
->set_child
[s
]]])
416 if (di
->set_size
[s
] + di
->set_size
[di
->set_child
[di
->set_child
[s
]]]
417 >= 2 * di
->set_size
[di
->set_child
[s
]])
419 di
->set_chain
[di
->set_child
[s
]] = s
;
420 di
->set_child
[s
] = di
->set_child
[di
->set_child
[s
]];
424 di
->set_size
[di
->set_child
[s
]] = di
->set_size
[s
];
425 s
= di
->set_chain
[s
] = di
->set_child
[s
];
429 di
->path_min
[s
] = di
->path_min
[w
];
430 di
->set_size
[v
] += di
->set_size
[w
];
431 if (di
->set_size
[v
] < 2 * di
->set_size
[w
])
434 s
= di
->set_child
[v
];
435 di
->set_child
[v
] = tmp
;
438 /* Merge all subtrees. */
441 di
->set_chain
[s
] = v
;
442 s
= di
->set_child
[s
];
446 /* This calculates the immediate dominators (or post-dominators if REVERSE is
447 true). DI is our working structure and should hold the DFS forest.
448 On return the immediate dominator to node V is in di->dom[V]. */
451 calc_idoms (di
, reverse
)
453 enum cdi_direction reverse
;
456 basic_block en_block
;
458 en_block
= EXIT_BLOCK_PTR
;
460 en_block
= ENTRY_BLOCK_PTR
;
462 /* Go backwards in DFS order, to first look at the leafs. */
466 basic_block bb
= di
->dfs_to_bb
[v
];
469 par
= di
->dfs_parent
[v
];
476 /* Search all direct predecessors for the smallest node with a path
477 to them. That way we have the smallest node with also a path to
478 us only over nodes behind us. In effect we search for our
480 for (; e
; e
= e_next
)
488 e_next
= e
->succ_next
;
493 e_next
= e
->pred_next
;
496 k1
= di
->dfs_order
[last_basic_block
];
498 k1
= di
->dfs_order
[b
->index
];
500 /* Call eval() only if really needed. If k1 is above V in DFS tree,
501 then we know, that eval(k1) == k1 and key[k1] == k1. */
503 k1
= di
->key
[eval (di
, k1
)];
509 link_roots (di
, par
, v
);
510 di
->next_bucket
[v
] = di
->bucket
[k
];
513 /* Transform semidominators into dominators. */
514 for (w
= di
->bucket
[par
]; w
; w
= di
->next_bucket
[w
])
517 if (di
->key
[k
] < di
->key
[w
])
522 /* We don't need to cleanup next_bucket[]. */
527 /* Explicitly define the dominators. */
529 for (v
= 2; v
<= di
->nodes
; v
++)
530 if (di
->dom
[v
] != di
->key
[v
])
531 di
->dom
[v
] = di
->dom
[di
->dom
[v
]];
534 /* Convert the information about immediate dominators (in DI) to sets of all
535 dominators (in DOMINATORS). */
538 idoms_to_doms (di
, dominators
)
544 sbitmap_vector_zero (dominators
, last_basic_block
);
545 /* We have to be careful, to not include the ENTRY_BLOCK or EXIT_BLOCK
546 in the list of (post)-doms, so remember that in e_index. */
547 e_index
= di
->dfs_order
[last_basic_block
];
549 for (i
= 1; i
<= di
->nodes
; i
++)
553 bb
= di
->dfs_to_bb
[i
]->index
;
555 if (di
->dom
[i
] && (di
->dom
[i
] != e_index
))
557 bb_idom
= di
->dfs_to_bb
[di
->dom
[i
]]->index
;
558 sbitmap_copy (dominators
[bb
], dominators
[bb_idom
]);
562 /* It has no immediate dom or only ENTRY_BLOCK or EXIT_BLOCK.
563 If it is a child of ENTRY_BLOCK that's OK, and it's only
564 dominated by itself; if it's _not_ a child of ENTRY_BLOCK, it
565 means, it is unreachable. That case has been disallowed in the
566 building of the DFS tree, so we are save here. For the reverse
567 flow graph it means, it has no children, so, to be compatible
568 with the old code, we set the post_dominators to all one. */
571 sbitmap_ones (dominators
[bb
]);
574 SET_BIT (dominators
[bb
], bb
);
578 /* The main entry point into this module. IDOM is an integer array with room
579 for last_basic_block integers, DOMS is a preallocated sbitmap array having
580 room for last_basic_block^2 bits, and POST is true if the caller wants to
581 know post-dominators.
583 On return IDOM[i] will be the BB->index of the immediate (post) dominator
584 of basic block i, and DOMS[i] will have set bit j if basic block j is a
585 (post)dominator for block i.
587 Either IDOM or DOMS may be NULL (meaning the caller is not interested in
588 immediate resp. all dominators). */
591 calculate_dominance_info (idom
, doms
, reverse
)
594 enum cdi_direction reverse
;
601 calc_dfs_tree (&di
, reverse
);
602 calc_idoms (&di
, reverse
);
610 TBB d
= di
.dom
[di
.dfs_order
[b
->index
]];
612 /* The old code didn't modify array elements of nodes having only
613 itself as dominator (d==0) or only ENTRY_BLOCK (resp. EXIT_BLOCK)
616 idom
[b
->index
] = di
.dfs_to_bb
[d
]->index
;
620 idoms_to_doms (&di
, doms
);