1 /* Calculate (post)dominators in slightly super-linear time.
2 Copyright (C) 2000, 2003, 2004 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. */
38 #include "coretypes.h"
41 #include "hard-reg-set.h"
42 #include "basic-block.h"
44 #include "et-forest.h"
46 /* Whether the dominators and the postdominators are available. */
47 enum dom_state dom_computed
[2];
49 /* We name our nodes with integers, beginning with 1. Zero is reserved for
50 'undefined' or 'end of list'. The name of each node is given by the dfs
51 number of the corresponding basic block. Please note, that we include the
52 artificial ENTRY_BLOCK (or EXIT_BLOCK in the post-dom case) in our lists to
53 support multiple entry points. As it has no real basic block index we use
54 'last_basic_block' for that. Its dfs number is of course 1. */
56 /* Type of Basic Block aka. TBB */
57 typedef unsigned int TBB
;
59 /* We work in a poor-mans object oriented fashion, and carry an instance of
60 this structure through all our 'methods'. It holds various arrays
61 reflecting the (sub)structure of the flowgraph. Most of them are of type
62 TBB and are also indexed by TBB. */
66 /* The parent of a node in the DFS tree. */
68 /* For a node x key[x] is roughly the node nearest to the root from which
69 exists a way to x only over nodes behind x. Such a node is also called
72 /* The value in path_min[x] is the node y on the path from x to the root of
73 the tree x is in with the smallest key[y]. */
75 /* bucket[x] points to the first node of the set of nodes having x as key. */
77 /* And next_bucket[x] points to the next node. */
79 /* After the algorithm is done, dom[x] contains the immediate dominator
83 /* The following few fields implement the structures needed for disjoint
85 /* set_chain[x] is the next node on the path from x to the representant
86 of the set containing x. If set_chain[x]==0 then x is a root. */
88 /* set_size[x] is the number of elements in the set named by x. */
89 unsigned int *set_size
;
90 /* set_child[x] is used for balancing the tree representing a set. It can
91 be understood as the next sibling of x. */
94 /* If b is the number of a basic block (BB->index), dfs_order[b] is the
95 number of that node in DFS order counted from 1. This is an index
96 into most of the other arrays in this structure. */
98 /* If x is the DFS-index of a node which corresponds with a basic block,
99 dfs_to_bb[x] is that basic block. Note, that in our structure there are
100 more nodes that basic blocks, so only dfs_to_bb[dfs_order[bb->index]]==bb
101 is true for every basic block bb, but not the opposite. */
102 basic_block
*dfs_to_bb
;
104 /* This is the next free DFS number when creating the DFS tree or forest. */
106 /* The number of nodes in the DFS tree (==dfsnum-1). */
110 static void init_dom_info (struct dom_info
*);
111 static void free_dom_info (struct dom_info
*);
112 static void calc_dfs_tree_nonrec (struct dom_info
*, basic_block
,
114 static void calc_dfs_tree (struct dom_info
*, enum cdi_direction
);
115 static void compress (struct dom_info
*, TBB
);
116 static TBB
eval (struct dom_info
*, TBB
);
117 static void link_roots (struct dom_info
*, TBB
, TBB
);
118 static void calc_idoms (struct dom_info
*, enum cdi_direction
);
119 void debug_dominance_info (enum cdi_direction
);
121 /* Helper macro for allocating and initializing an array,
122 for aesthetic reasons. */
123 #define init_ar(var, type, num, content) \
126 unsigned int i = 1; /* Catch content == i. */ \
128 (var) = xcalloc ((num), sizeof (type)); \
131 (var) = xmalloc ((num) * sizeof (type)); \
132 for (i = 0; i < num; i++) \
133 (var)[i] = (content); \
138 /* Allocate all needed memory in a pessimistic fashion (so we round up).
139 This initializes the contents of DI, which already must be allocated. */
142 init_dom_info (struct dom_info
*di
)
144 /* We need memory for n_basic_blocks nodes and the ENTRY_BLOCK or
146 unsigned int num
= n_basic_blocks
+ 1 + 1;
147 init_ar (di
->dfs_parent
, TBB
, num
, 0);
148 init_ar (di
->path_min
, TBB
, num
, i
);
149 init_ar (di
->key
, TBB
, num
, i
);
150 init_ar (di
->dom
, TBB
, num
, 0);
152 init_ar (di
->bucket
, TBB
, num
, 0);
153 init_ar (di
->next_bucket
, TBB
, num
, 0);
155 init_ar (di
->set_chain
, TBB
, num
, 0);
156 init_ar (di
->set_size
, unsigned int, num
, 1);
157 init_ar (di
->set_child
, TBB
, num
, 0);
159 init_ar (di
->dfs_order
, TBB
, (unsigned int) last_basic_block
+ 1, 0);
160 init_ar (di
->dfs_to_bb
, basic_block
, num
, 0);
168 /* Free all allocated memory in DI, but not DI itself. */
171 free_dom_info (struct dom_info
*di
)
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 (struct dom_info
*di
, basic_block bb
, enum cdi_direction reverse
)
195 /* We never call this with bb==EXIT_BLOCK_PTR (ENTRY_BLOCK_PTR if REVERSE). */
196 /* We call this _only_ if bb is not already visited. */
198 TBB child_i
, my_i
= 0;
201 /* Start block (ENTRY_BLOCK_PTR for forward problem, EXIT_BLOCK for backward
203 basic_block en_block
;
205 basic_block ex_block
;
207 stack
= xmalloc ((n_basic_blocks
+ 3) * sizeof (edge
));
210 /* Initialize our border blocks, and the first edge. */
214 en_block
= EXIT_BLOCK_PTR
;
215 ex_block
= ENTRY_BLOCK_PTR
;
220 en_block
= ENTRY_BLOCK_PTR
;
221 ex_block
= EXIT_BLOCK_PTR
;
224 /* When the stack is empty we break out of this loop. */
229 /* This loop traverses edges e in depth first manner, and fills the
235 /* Deduce from E the current and the next block (BB and BN), and the
241 /* If the next node BN is either already visited or a border
242 block the current edge is useless, and simply overwritten
243 with the next edge out of the current node. */
244 if (bn
== ex_block
|| di
->dfs_order
[bn
->index
])
255 if (bn
== ex_block
|| di
->dfs_order
[bn
->index
])
267 /* Fill the DFS tree info calculatable _before_ recursing. */
269 my_i
= di
->dfs_order
[bb
->index
];
271 my_i
= di
->dfs_order
[last_basic_block
];
272 child_i
= di
->dfs_order
[bn
->index
] = di
->dfsnum
++;
273 di
->dfs_to_bb
[child_i
] = bn
;
274 di
->dfs_parent
[child_i
] = my_i
;
276 /* Save the current point in the CFG on the stack, and recurse. */
285 /* OK. The edge-list was exhausted, meaning normally we would
286 end the recursion. After returning from the recursive call,
287 there were (may be) other statements which were run after a
288 child node was completely considered by DFS. Here is the
289 point to do it in the non-recursive variant.
290 E.g. The block just completed is in e->dest for forward DFS,
291 the block not yet completed (the parent of the one above)
292 in e->src. This could be used e.g. for computing the number of
293 descendants or the tree depth. */
302 /* The main entry for calculating the DFS tree or forest. DI is our working
303 structure and REVERSE is true, if we are interested in the reverse flow
304 graph. In that case the result is not necessarily a tree but a forest,
305 because there may be nodes from which the EXIT_BLOCK is unreachable. */
308 calc_dfs_tree (struct dom_info
*di
, enum cdi_direction reverse
)
310 /* The first block is the ENTRY_BLOCK (or EXIT_BLOCK if REVERSE). */
311 basic_block begin
= reverse
? EXIT_BLOCK_PTR
: ENTRY_BLOCK_PTR
;
312 di
->dfs_order
[last_basic_block
] = di
->dfsnum
;
313 di
->dfs_to_bb
[di
->dfsnum
] = begin
;
316 calc_dfs_tree_nonrec (di
, begin
, reverse
);
320 /* In the post-dom case we may have nodes without a path to EXIT_BLOCK.
321 They are reverse-unreachable. In the dom-case we disallow such
322 nodes, but in post-dom we have to deal with them, so we simply
323 include them in the DFS tree which actually becomes a forest. */
325 FOR_EACH_BB_REVERSE (b
)
327 if (di
->dfs_order
[b
->index
])
329 di
->dfs_order
[b
->index
] = di
->dfsnum
;
330 di
->dfs_to_bb
[di
->dfsnum
] = b
;
332 calc_dfs_tree_nonrec (di
, b
, reverse
);
336 di
->nodes
= di
->dfsnum
- 1;
338 /* This aborts e.g. when there is _no_ path from ENTRY to EXIT at all. */
339 if (di
->nodes
!= (unsigned int) n_basic_blocks
+ 1)
343 /* Compress the path from V to the root of its set and update path_min at the
344 same time. After compress(di, V) set_chain[V] is the root of the set V is
345 in and path_min[V] is the node with the smallest key[] value on the path
346 from V to that root. */
349 compress (struct dom_info
*di
, TBB v
)
351 /* Btw. It's not worth to unrecurse compress() as the depth is usually not
352 greater than 5 even for huge graphs (I've not seen call depth > 4).
353 Also performance wise compress() ranges _far_ behind eval(). */
354 TBB parent
= di
->set_chain
[v
];
355 if (di
->set_chain
[parent
])
357 compress (di
, parent
);
358 if (di
->key
[di
->path_min
[parent
]] < di
->key
[di
->path_min
[v
]])
359 di
->path_min
[v
] = di
->path_min
[parent
];
360 di
->set_chain
[v
] = di
->set_chain
[parent
];
364 /* Compress the path from V to the set root of V if needed (when the root has
365 changed since the last call). Returns the node with the smallest key[]
366 value on the path from V to the root. */
369 eval (struct dom_info
*di
, TBB v
)
371 /* The representant of the set V is in, also called root (as the set
372 representation is a tree). */
373 TBB rep
= di
->set_chain
[v
];
375 /* V itself is the root. */
377 return di
->path_min
[v
];
379 /* Compress only if necessary. */
380 if (di
->set_chain
[rep
])
383 rep
= di
->set_chain
[v
];
386 if (di
->key
[di
->path_min
[rep
]] >= di
->key
[di
->path_min
[v
]])
387 return di
->path_min
[v
];
389 return di
->path_min
[rep
];
392 /* This essentially merges the two sets of V and W, giving a single set with
393 the new root V. The internal representation of these disjoint sets is a
394 balanced tree. Currently link(V,W) is only used with V being the parent
398 link_roots (struct dom_info
*di
, TBB v
, TBB w
)
402 /* Rebalance the tree. */
403 while (di
->key
[di
->path_min
[w
]] < di
->key
[di
->path_min
[di
->set_child
[s
]]])
405 if (di
->set_size
[s
] + di
->set_size
[di
->set_child
[di
->set_child
[s
]]]
406 >= 2 * di
->set_size
[di
->set_child
[s
]])
408 di
->set_chain
[di
->set_child
[s
]] = s
;
409 di
->set_child
[s
] = di
->set_child
[di
->set_child
[s
]];
413 di
->set_size
[di
->set_child
[s
]] = di
->set_size
[s
];
414 s
= di
->set_chain
[s
] = di
->set_child
[s
];
418 di
->path_min
[s
] = di
->path_min
[w
];
419 di
->set_size
[v
] += di
->set_size
[w
];
420 if (di
->set_size
[v
] < 2 * di
->set_size
[w
])
423 s
= di
->set_child
[v
];
424 di
->set_child
[v
] = tmp
;
427 /* Merge all subtrees. */
430 di
->set_chain
[s
] = v
;
431 s
= di
->set_child
[s
];
435 /* This calculates the immediate dominators (or post-dominators if REVERSE is
436 true). DI is our working structure and should hold the DFS forest.
437 On return the immediate dominator to node V is in di->dom[V]. */
440 calc_idoms (struct dom_info
*di
, enum cdi_direction reverse
)
443 basic_block en_block
;
445 en_block
= EXIT_BLOCK_PTR
;
447 en_block
= ENTRY_BLOCK_PTR
;
449 /* Go backwards in DFS order, to first look at the leafs. */
453 basic_block bb
= di
->dfs_to_bb
[v
];
456 par
= di
->dfs_parent
[v
];
463 /* Search all direct predecessors for the smallest node with a path
464 to them. That way we have the smallest node with also a path to
465 us only over nodes behind us. In effect we search for our
467 for (; e
; e
= e_next
)
475 e_next
= e
->succ_next
;
480 e_next
= e
->pred_next
;
483 k1
= di
->dfs_order
[last_basic_block
];
485 k1
= di
->dfs_order
[b
->index
];
487 /* Call eval() only if really needed. If k1 is above V in DFS tree,
488 then we know, that eval(k1) == k1 and key[k1] == k1. */
490 k1
= di
->key
[eval (di
, k1
)];
496 link_roots (di
, par
, v
);
497 di
->next_bucket
[v
] = di
->bucket
[k
];
500 /* Transform semidominators into dominators. */
501 for (w
= di
->bucket
[par
]; w
; w
= di
->next_bucket
[w
])
504 if (di
->key
[k
] < di
->key
[w
])
509 /* We don't need to cleanup next_bucket[]. */
514 /* Explicitly define the dominators. */
516 for (v
= 2; v
<= di
->nodes
; v
++)
517 if (di
->dom
[v
] != di
->key
[v
])
518 di
->dom
[v
] = di
->dom
[di
->dom
[v
]];
521 /* Assign dfs numbers starting from NUM to NODE and its sons. */
524 assign_dfs_numbers (struct et_node
*node
, int *num
)
528 node
->dfs_num_in
= (*num
)++;
532 assign_dfs_numbers (node
->son
, num
);
533 for (son
= node
->son
->right
; son
!= node
->son
; son
= son
->right
)
534 assign_dfs_numbers (son
, num
);
537 node
->dfs_num_out
= (*num
)++;
540 /* Compute the data necessary for fast resolving of dominator queries in a
541 static dominator tree. */
544 compute_dom_fast_query (enum cdi_direction dir
)
549 if (dom_computed
[dir
] < DOM_NO_FAST_QUERY
)
552 if (dom_computed
[dir
] == DOM_OK
)
557 if (!bb
->dom
[dir
]->father
)
558 assign_dfs_numbers (bb
->dom
[dir
], &num
);
561 dom_computed
[dir
] = DOM_OK
;
564 /* The main entry point into this module. DIR is set depending on whether
565 we want to compute dominators or postdominators. */
568 calculate_dominance_info (enum cdi_direction dir
)
573 if (dom_computed
[dir
] == DOM_OK
)
576 if (dom_computed
[dir
] != DOM_NO_FAST_QUERY
)
578 if (dom_computed
[dir
] != DOM_NONE
)
579 free_dominance_info (dir
);
583 b
->dom
[dir
] = et_new_tree (b
);
587 calc_dfs_tree (&di
, dir
);
588 calc_idoms (&di
, dir
);
592 TBB d
= di
.dom
[di
.dfs_order
[b
->index
]];
595 et_set_father (b
->dom
[dir
], di
.dfs_to_bb
[d
]->dom
[dir
]);
599 dom_computed
[dir
] = DOM_NO_FAST_QUERY
;
602 compute_dom_fast_query (dir
);
605 /* Free dominance information for direction DIR. */
607 free_dominance_info (enum cdi_direction dir
)
611 if (!dom_computed
[dir
])
616 delete_from_dominance_info (dir
, bb
);
619 dom_computed
[dir
] = DOM_NONE
;
622 /* Return the immediate dominator of basic block BB. */
624 get_immediate_dominator (enum cdi_direction dir
, basic_block bb
)
626 struct et_node
*node
= bb
->dom
[dir
];
628 if (!dom_computed
[dir
])
634 return node
->father
->data
;
637 /* Set the immediate dominator of the block possibly removing
638 existing edge. NULL can be used to remove any edge. */
640 set_immediate_dominator (enum cdi_direction dir
, basic_block bb
,
641 basic_block dominated_by
)
643 struct et_node
*node
= bb
->dom
[dir
];
645 if (!dom_computed
[dir
])
650 if (node
->father
->data
== dominated_by
)
656 et_set_father (node
, dominated_by
->dom
[dir
]);
658 if (dom_computed
[dir
] == DOM_OK
)
659 dom_computed
[dir
] = DOM_NO_FAST_QUERY
;
662 /* Store all basic blocks immediately dominated by BB into BBS and return
665 get_dominated_by (enum cdi_direction dir
, basic_block bb
, basic_block
**bbs
)
668 struct et_node
*node
= bb
->dom
[dir
], *son
= node
->son
, *ason
;
670 if (!dom_computed
[dir
])
679 for (ason
= son
->right
, n
= 1; ason
!= son
; ason
= ason
->right
)
682 *bbs
= xmalloc (n
* sizeof (basic_block
));
683 (*bbs
)[0] = son
->data
;
684 for (ason
= son
->right
, n
= 1; ason
!= son
; ason
= ason
->right
)
685 (*bbs
)[n
++] = ason
->data
;
690 /* Redirect all edges pointing to BB to TO. */
692 redirect_immediate_dominators (enum cdi_direction dir
, basic_block bb
,
695 struct et_node
*bb_node
= bb
->dom
[dir
], *to_node
= to
->dom
[dir
], *son
;
697 if (!dom_computed
[dir
])
708 et_set_father (son
, to_node
);
711 if (dom_computed
[dir
] == DOM_OK
)
712 dom_computed
[dir
] = DOM_NO_FAST_QUERY
;
715 /* Find first basic block in the tree dominating both BB1 and BB2. */
717 nearest_common_dominator (enum cdi_direction dir
, basic_block bb1
, basic_block bb2
)
719 if (!dom_computed
[dir
])
727 return et_nca (bb1
->dom
[dir
], bb2
->dom
[dir
])->data
;
730 /* Return TRUE in case BB1 is dominated by BB2. */
732 dominated_by_p (enum cdi_direction dir
, basic_block bb1
, basic_block bb2
)
734 struct et_node
*n1
= bb1
->dom
[dir
], *n2
= bb2
->dom
[dir
];
736 if (!dom_computed
[dir
])
739 if (dom_computed
[dir
] == DOM_OK
)
740 return (n1
->dfs_num_in
>= n2
->dfs_num_in
741 && n1
->dfs_num_out
<= n2
->dfs_num_out
);
743 return et_below (n1
, n2
);
746 /* Verify invariants of dominator structure. */
748 verify_dominators (enum cdi_direction dir
)
753 if (!dom_computed
[dir
])
760 dom_bb
= recount_dominator (dir
, bb
);
761 if (dom_bb
!= get_immediate_dominator (dir
, bb
))
763 error ("dominator of %d should be %d, not %d",
764 bb
->index
, dom_bb
->index
, get_immediate_dominator(dir
, bb
)->index
);
772 /* Determine immediate dominator (or postdominator, according to DIR) of BB,
773 assuming that dominators of other blocks are correct. We also use it to
774 recompute the dominators in a restricted area, by iterating it until it
775 reaches a fixed point. */
778 recount_dominator (enum cdi_direction dir
, basic_block bb
)
780 basic_block dom_bb
= NULL
;
783 if (!dom_computed
[dir
])
786 if (dir
== CDI_DOMINATORS
)
788 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
790 if (!dominated_by_p (dir
, e
->src
, bb
))
791 dom_bb
= nearest_common_dominator (dir
, dom_bb
, e
->src
);
796 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
798 if (!dominated_by_p (dir
, e
->dest
, bb
))
799 dom_bb
= nearest_common_dominator (dir
, dom_bb
, e
->dest
);
806 /* Iteratively recount dominators of BBS. The change is supposed to be local
807 and not to grow further. */
809 iterate_fix_dominators (enum cdi_direction dir
, basic_block
*bbs
, int n
)
812 basic_block old_dom
, new_dom
;
814 if (!dom_computed
[dir
])
820 for (i
= 0; i
< n
; i
++)
822 old_dom
= get_immediate_dominator (dir
, bbs
[i
]);
823 new_dom
= recount_dominator (dir
, bbs
[i
]);
824 if (old_dom
!= new_dom
)
827 set_immediate_dominator (dir
, bbs
[i
], new_dom
);
834 add_to_dominance_info (enum cdi_direction dir
, basic_block bb
)
836 if (!dom_computed
[dir
])
842 bb
->dom
[dir
] = et_new_tree (bb
);
844 if (dom_computed
[dir
] == DOM_OK
)
845 dom_computed
[dir
] = DOM_NO_FAST_QUERY
;
849 delete_from_dominance_info (enum cdi_direction dir
, basic_block bb
)
851 if (!dom_computed
[dir
])
854 et_free_tree (bb
->dom
[dir
]);
857 if (dom_computed
[dir
] == DOM_OK
)
858 dom_computed
[dir
] = DOM_NO_FAST_QUERY
;
861 /* Returns the first son of BB in the dominator or postdominator tree
862 as determined by DIR. */
865 first_dom_son (enum cdi_direction dir
, basic_block bb
)
867 struct et_node
*son
= bb
->dom
[dir
]->son
;
869 return son
? son
->data
: NULL
;
872 /* Returns the next dominance son after BB in the dominator or postdominator
873 tree as determined by DIR, or NULL if it was the last one. */
876 next_dom_son (enum cdi_direction dir
, basic_block bb
)
878 struct et_node
*next
= bb
->dom
[dir
]->right
;
880 return next
->father
->son
== next
? NULL
: next
->data
;
884 debug_dominance_info (enum cdi_direction dir
)
888 if ((bb2
= get_immediate_dominator (dir
, bb
)))
889 fprintf (stderr
, "%i %i\n", bb
->index
, bb2
->index
);