1 /* Calculate (post)dominators in slightly super-linear time.
2 Copyright (C) 2000, 2003 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"
52 #define BB_NODE(info, bb) \
53 ((et_forest_node_t)VARRAY_GENERIC_PTR ((info)->varray, (bb)->index + 2))
54 #define SET_BB_NODE(info, bb, node) \
55 (VARRAY_GENERIC_PTR ((info)->varray, (bb)->index + 2) = (node))
57 /* We name our nodes with integers, beginning with 1. Zero is reserved for
58 'undefined' or 'end of list'. The name of each node is given by the dfs
59 number of the corresponding basic block. Please note, that we include the
60 artificial ENTRY_BLOCK (or EXIT_BLOCK in the post-dom case) in our lists to
61 support multiple entry points. As it has no real basic block index we use
62 'last_basic_block' for that. Its dfs number is of course 1. */
64 /* Type of Basic Block aka. TBB */
65 typedef unsigned int TBB
;
67 /* We work in a poor-mans object oriented fashion, and carry an instance of
68 this structure through all our 'methods'. It holds various arrays
69 reflecting the (sub)structure of the flowgraph. Most of them are of type
70 TBB and are also indexed by TBB. */
74 /* The parent of a node in the DFS tree. */
76 /* For a node x key[x] is roughly the node nearest to the root from which
77 exists a way to x only over nodes behind x. Such a node is also called
80 /* The value in path_min[x] is the node y on the path from x to the root of
81 the tree x is in with the smallest key[y]. */
83 /* bucket[x] points to the first node of the set of nodes having x as key. */
85 /* And next_bucket[x] points to the next node. */
87 /* After the algorithm is done, dom[x] contains the immediate dominator
91 /* The following few fields implement the structures needed for disjoint
93 /* set_chain[x] is the next node on the path from x to the representant
94 of the set containing x. If set_chain[x]==0 then x is a root. */
96 /* set_size[x] is the number of elements in the set named by x. */
97 unsigned int *set_size
;
98 /* set_child[x] is used for balancing the tree representing a set. It can
99 be understood as the next sibling of x. */
102 /* If b is the number of a basic block (BB->index), dfs_order[b] is the
103 number of that node in DFS order counted from 1. This is an index
104 into most of the other arrays in this structure. */
106 /* If x is the DFS-index of a node which corresponds with a basic block,
107 dfs_to_bb[x] is that basic block. Note, that in our structure there are
108 more nodes that basic blocks, so only dfs_to_bb[dfs_order[bb->index]]==bb
109 is true for every basic block bb, but not the opposite. */
110 basic_block
*dfs_to_bb
;
112 /* This is the next free DFS number when creating the DFS tree or forest. */
114 /* The number of nodes in the DFS tree (==dfsnum-1). */
118 static void init_dom_info (struct dom_info
*);
119 static void free_dom_info (struct dom_info
*);
120 static void calc_dfs_tree_nonrec (struct dom_info
*, basic_block
,
122 static void calc_dfs_tree (struct dom_info
*, enum cdi_direction
);
123 static void compress (struct dom_info
*, TBB
);
124 static TBB
eval (struct dom_info
*, TBB
);
125 static void link_roots (struct dom_info
*, TBB
, TBB
);
126 static void calc_idoms (struct dom_info
*, enum cdi_direction
);
127 void debug_dominance_info (dominance_info
);
129 /* Helper macro for allocating and initializing an array,
130 for aesthetic reasons. */
131 #define init_ar(var, type, num, content) \
134 unsigned int i = 1; /* Catch content == i. */ \
136 (var) = xcalloc ((num), sizeof (type)); \
139 (var) = xmalloc ((num) * sizeof (type)); \
140 for (i = 0; i < num; i++) \
141 (var)[i] = (content); \
146 /* Allocate all needed memory in a pessimistic fashion (so we round up).
147 This initializes the contents of DI, which already must be allocated. */
150 init_dom_info (struct dom_info
*di
)
152 /* We need memory for n_basic_blocks nodes and the ENTRY_BLOCK or
154 unsigned int num
= n_basic_blocks
+ 1 + 1;
155 init_ar (di
->dfs_parent
, TBB
, num
, 0);
156 init_ar (di
->path_min
, TBB
, num
, i
);
157 init_ar (di
->key
, TBB
, num
, i
);
158 init_ar (di
->dom
, TBB
, num
, 0);
160 init_ar (di
->bucket
, TBB
, num
, 0);
161 init_ar (di
->next_bucket
, TBB
, num
, 0);
163 init_ar (di
->set_chain
, TBB
, num
, 0);
164 init_ar (di
->set_size
, unsigned int, num
, 1);
165 init_ar (di
->set_child
, TBB
, num
, 0);
167 init_ar (di
->dfs_order
, TBB
, (unsigned int) last_basic_block
+ 1, 0);
168 init_ar (di
->dfs_to_bb
, basic_block
, num
, 0);
176 /* Free all allocated memory in DI, but not DI itself. */
179 free_dom_info (struct dom_info
*di
)
181 free (di
->dfs_parent
);
186 free (di
->next_bucket
);
187 free (di
->set_chain
);
189 free (di
->set_child
);
190 free (di
->dfs_order
);
191 free (di
->dfs_to_bb
);
194 /* The nonrecursive variant of creating a DFS tree. DI is our working
195 structure, BB the starting basic block for this tree and REVERSE
196 is true, if predecessors should be visited instead of successors of a
197 node. After this is done all nodes reachable from BB were visited, have
198 assigned their dfs number and are linked together to form a tree. */
201 calc_dfs_tree_nonrec (struct dom_info
*di
, basic_block bb
, enum cdi_direction reverse
)
203 /* We never call this with bb==EXIT_BLOCK_PTR (ENTRY_BLOCK_PTR if REVERSE). */
204 /* We call this _only_ if bb is not already visited. */
206 TBB child_i
, my_i
= 0;
209 /* Start block (ENTRY_BLOCK_PTR for forward problem, EXIT_BLOCK for backward
211 basic_block en_block
;
213 basic_block ex_block
;
215 stack
= xmalloc ((n_basic_blocks
+ 3) * sizeof (edge
));
218 /* Initialize our border blocks, and the first edge. */
222 en_block
= EXIT_BLOCK_PTR
;
223 ex_block
= ENTRY_BLOCK_PTR
;
228 en_block
= ENTRY_BLOCK_PTR
;
229 ex_block
= EXIT_BLOCK_PTR
;
232 /* When the stack is empty we break out of this loop. */
237 /* This loop traverses edges e in depth first manner, and fills the
243 /* Deduce from E the current and the next block (BB and BN), and the
249 /* If the next node BN is either already visited or a border
250 block the current edge is useless, and simply overwritten
251 with the next edge out of the current node. */
252 if (bn
== ex_block
|| di
->dfs_order
[bn
->index
])
263 if (bn
== ex_block
|| di
->dfs_order
[bn
->index
])
275 /* Fill the DFS tree info calculatable _before_ recursing. */
277 my_i
= di
->dfs_order
[bb
->index
];
279 my_i
= di
->dfs_order
[last_basic_block
];
280 child_i
= di
->dfs_order
[bn
->index
] = di
->dfsnum
++;
281 di
->dfs_to_bb
[child_i
] = bn
;
282 di
->dfs_parent
[child_i
] = my_i
;
284 /* Save the current point in the CFG on the stack, and recurse. */
293 /* OK. The edge-list was exhausted, meaning normally we would
294 end the recursion. After returning from the recursive call,
295 there were (may be) other statements which were run after a
296 child node was completely considered by DFS. Here is the
297 point to do it in the non-recursive variant.
298 E.g. The block just completed is in e->dest for forward DFS,
299 the block not yet completed (the parent of the one above)
300 in e->src. This could be used e.g. for computing the number of
301 descendants or the tree depth. */
310 /* The main entry for calculating the DFS tree or forest. DI is our working
311 structure and REVERSE is true, if we are interested in the reverse flow
312 graph. In that case the result is not necessarily a tree but a forest,
313 because there may be nodes from which the EXIT_BLOCK is unreachable. */
316 calc_dfs_tree (struct dom_info
*di
, enum cdi_direction reverse
)
318 /* The first block is the ENTRY_BLOCK (or EXIT_BLOCK if REVERSE). */
319 basic_block begin
= reverse
? EXIT_BLOCK_PTR
: ENTRY_BLOCK_PTR
;
320 di
->dfs_order
[last_basic_block
] = di
->dfsnum
;
321 di
->dfs_to_bb
[di
->dfsnum
] = begin
;
324 calc_dfs_tree_nonrec (di
, begin
, reverse
);
328 /* In the post-dom case we may have nodes without a path to EXIT_BLOCK.
329 They are reverse-unreachable. In the dom-case we disallow such
330 nodes, but in post-dom we have to deal with them, so we simply
331 include them in the DFS tree which actually becomes a forest. */
333 FOR_EACH_BB_REVERSE (b
)
335 if (di
->dfs_order
[b
->index
])
337 di
->dfs_order
[b
->index
] = di
->dfsnum
;
338 di
->dfs_to_bb
[di
->dfsnum
] = b
;
340 calc_dfs_tree_nonrec (di
, b
, reverse
);
344 di
->nodes
= di
->dfsnum
- 1;
346 /* This aborts e.g. when there is _no_ path from ENTRY to EXIT at all. */
347 if (di
->nodes
!= (unsigned int) n_basic_blocks
+ 1)
351 /* Compress the path from V to the root of its set and update path_min at the
352 same time. After compress(di, V) set_chain[V] is the root of the set V is
353 in and path_min[V] is the node with the smallest key[] value on the path
354 from V to that root. */
357 compress (struct dom_info
*di
, TBB v
)
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
])
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
];
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. */
377 eval (struct dom_info
*di
, TBB v
)
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 (struct dom_info
*di
, TBB v
, TBB w
)
410 /* Rebalance the tree. */
411 while (di
->key
[di
->path_min
[w
]] < di
->key
[di
->path_min
[di
->set_child
[s
]]])
413 if (di
->set_size
[s
] + di
->set_size
[di
->set_child
[di
->set_child
[s
]]]
414 >= 2 * di
->set_size
[di
->set_child
[s
]])
416 di
->set_chain
[di
->set_child
[s
]] = s
;
417 di
->set_child
[s
] = di
->set_child
[di
->set_child
[s
]];
421 di
->set_size
[di
->set_child
[s
]] = di
->set_size
[s
];
422 s
= di
->set_chain
[s
] = di
->set_child
[s
];
426 di
->path_min
[s
] = di
->path_min
[w
];
427 di
->set_size
[v
] += di
->set_size
[w
];
428 if (di
->set_size
[v
] < 2 * di
->set_size
[w
])
431 s
= di
->set_child
[v
];
432 di
->set_child
[v
] = tmp
;
435 /* Merge all subtrees. */
438 di
->set_chain
[s
] = v
;
439 s
= di
->set_child
[s
];
443 /* This calculates the immediate dominators (or post-dominators if REVERSE is
444 true). DI is our working structure and should hold the DFS forest.
445 On return the immediate dominator to node V is in di->dom[V]. */
448 calc_idoms (struct dom_info
*di
, enum cdi_direction reverse
)
451 basic_block en_block
;
453 en_block
= EXIT_BLOCK_PTR
;
455 en_block
= ENTRY_BLOCK_PTR
;
457 /* Go backwards in DFS order, to first look at the leafs. */
461 basic_block bb
= di
->dfs_to_bb
[v
];
464 par
= di
->dfs_parent
[v
];
471 /* Search all direct predecessors for the smallest node with a path
472 to them. That way we have the smallest node with also a path to
473 us only over nodes behind us. In effect we search for our
475 for (; e
; e
= e_next
)
483 e_next
= e
->succ_next
;
488 e_next
= e
->pred_next
;
491 k1
= di
->dfs_order
[last_basic_block
];
493 k1
= di
->dfs_order
[b
->index
];
495 /* Call eval() only if really needed. If k1 is above V in DFS tree,
496 then we know, that eval(k1) == k1 and key[k1] == k1. */
498 k1
= di
->key
[eval (di
, k1
)];
504 link_roots (di
, par
, v
);
505 di
->next_bucket
[v
] = di
->bucket
[k
];
508 /* Transform semidominators into dominators. */
509 for (w
= di
->bucket
[par
]; w
; w
= di
->next_bucket
[w
])
512 if (di
->key
[k
] < di
->key
[w
])
517 /* We don't need to cleanup next_bucket[]. */
522 /* Explicitly define the dominators. */
524 for (v
= 2; v
<= di
->nodes
; v
++)
525 if (di
->dom
[v
] != di
->key
[v
])
526 di
->dom
[v
] = di
->dom
[di
->dom
[v
]];
529 /* The main entry point into this module. IDOM is an integer array with room
530 for last_basic_block integers, DOMS is a preallocated sbitmap array having
531 room for last_basic_block^2 bits, and POST is true if the caller wants to
532 know post-dominators.
534 On return IDOM[i] will be the BB->index of the immediate (post) dominator
535 of basic block i, and DOMS[i] will have set bit j if basic block j is a
536 (post)dominator for block i.
538 Either IDOM or DOMS may be NULL (meaning the caller is not interested in
539 immediate resp. all dominators). */
542 calculate_dominance_info (enum cdi_direction reverse
)
548 /* Allocate structure for dominance information. */
549 info
= xmalloc (sizeof (struct dominance_info
));
550 info
->forest
= et_forest_create ();
551 VARRAY_GENERIC_PTR_INIT (info
->varray
, last_basic_block
+ 3, "dominance info");
553 /* Add the two well-known basic blocks. */
554 SET_BB_NODE (info
, ENTRY_BLOCK_PTR
, et_forest_add_node (info
->forest
,
556 SET_BB_NODE (info
, EXIT_BLOCK_PTR
, et_forest_add_node (info
->forest
,
559 SET_BB_NODE (info
, b
, et_forest_add_node (info
->forest
, b
));
562 calc_dfs_tree (&di
, reverse
);
563 calc_idoms (&di
, reverse
);
568 TBB d
= di
.dom
[di
.dfs_order
[b
->index
]];
571 et_forest_add_edge (info
->forest
, BB_NODE (info
, di
.dfs_to_bb
[d
]), BB_NODE (info
, b
));
578 /* Free dominance information. */
580 free_dominance_info (dominance_info info
)
584 /* Allow users to create new basic block without setting up the dominance
585 information for them. */
587 if (bb
->index
< (int)(info
->varray
->num_elements
- 2)
588 && BB_NODE (info
, bb
))
589 delete_from_dominance_info (info
, bb
);
590 delete_from_dominance_info (info
, ENTRY_BLOCK_PTR
);
591 delete_from_dominance_info (info
, EXIT_BLOCK_PTR
);
592 et_forest_delete (info
->forest
);
593 VARRAY_GROW (info
->varray
, 0);
597 /* Return the immediate dominator of basic block BB. */
599 get_immediate_dominator (dominance_info dom
, basic_block bb
)
601 return et_forest_node_value (dom
->forest
,
602 et_forest_parent (dom
->forest
,
606 /* Set the immediate dominator of the block possibly removing
607 existing edge. NULL can be used to remove any edge. */
609 set_immediate_dominator (dominance_info dom
, basic_block bb
, basic_block dominated_by
)
612 et_forest_node_t bb_node
= BB_NODE (dom
, bb
);
614 aux_bb_node
= et_forest_parent (dom
->forest
, bb_node
);
616 et_forest_remove_edge (dom
->forest
, aux_bb_node
, bb_node
);
617 if (dominated_by
!= NULL
)
619 if (bb
== dominated_by
)
621 if (!et_forest_add_edge (dom
->forest
, BB_NODE (dom
, dominated_by
), bb_node
))
626 /* Store all basic blocks dominated by BB into BBS and return their number. */
628 get_dominated_by (dominance_info dom
, basic_block bb
, basic_block
**bbs
)
632 *bbs
= xmalloc (n_basic_blocks
* sizeof (basic_block
));
633 n
= et_forest_enumerate_sons (dom
->forest
, BB_NODE (dom
, bb
), (et_forest_node_t
*)*bbs
);
634 for (i
= 0; i
< n
; i
++)
635 (*bbs
)[i
] = et_forest_node_value (dom
->forest
, (et_forest_node_t
)(*bbs
)[i
]);
639 /* Redirect all edges pointing to BB to TO. */
641 redirect_immediate_dominators (dominance_info dom
, basic_block bb
, basic_block to
)
643 et_forest_node_t
*bbs
= xmalloc (n_basic_blocks
* sizeof (basic_block
));
644 et_forest_node_t node
= BB_NODE (dom
, bb
);
645 et_forest_node_t node2
= BB_NODE (dom
, to
);
646 int n
= et_forest_enumerate_sons (dom
->forest
, node
, bbs
);
649 for (i
= 0; i
< n
; i
++)
651 et_forest_remove_edge (dom
->forest
, node
, bbs
[i
]);
652 et_forest_add_edge (dom
->forest
, node2
, bbs
[i
]);
657 /* Find first basic block in the tree dominating both BB1 and BB2. */
659 nearest_common_dominator (dominance_info dom
, basic_block bb1
, basic_block bb2
)
665 return et_forest_node_value (dom
->forest
,
666 et_forest_common_ancestor (dom
->forest
,
672 /* Return TRUE in case BB1 is dominated by BB2. */
674 dominated_by_p (dominance_info dom
, basic_block bb1
, basic_block bb2
)
676 return nearest_common_dominator (dom
, bb1
, bb2
) == bb2
;
679 /* Verify invariants of dominator structure. */
681 verify_dominators (dominance_info dom
)
690 dom_bb
= recount_dominator (dom
, bb
);
691 if (dom_bb
!= get_immediate_dominator (dom
, bb
))
693 error ("dominator of %d should be %d, not %d",
694 bb
->index
, dom_bb
->index
, get_immediate_dominator(dom
, bb
)->index
);
702 /* Recount dominator of BB. */
704 recount_dominator (dominance_info dom
, basic_block bb
)
706 basic_block dom_bb
= NULL
;
709 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
711 if (!dominated_by_p (dom
, e
->src
, bb
))
712 dom_bb
= nearest_common_dominator (dom
, dom_bb
, e
->src
);
718 /* Iteratively recount dominators of BBS. The change is supposed to be local
719 and not to grow further. */
721 iterate_fix_dominators (dominance_info dom
, basic_block
*bbs
, int n
)
724 basic_block old_dom
, new_dom
;
729 for (i
= 0; i
< n
; i
++)
731 old_dom
= get_immediate_dominator (dom
, bbs
[i
]);
732 new_dom
= recount_dominator (dom
, bbs
[i
]);
733 if (old_dom
!= new_dom
)
736 set_immediate_dominator (dom
, bbs
[i
], new_dom
);
743 add_to_dominance_info (dominance_info dom
, basic_block bb
)
745 VARRAY_GROW (dom
->varray
, last_basic_block
+ 3);
746 #ifdef ENABLE_CHECKING
747 if (BB_NODE (dom
, bb
))
750 SET_BB_NODE (dom
, bb
, et_forest_add_node (dom
->forest
, bb
));
754 delete_from_dominance_info (dominance_info dom
, basic_block bb
)
756 et_forest_remove_node (dom
->forest
, BB_NODE (dom
, bb
));
757 SET_BB_NODE (dom
, bb
, NULL
);
761 debug_dominance_info (dominance_info dom
)
765 if ((bb2
= get_immediate_dominator (dom
, bb
)))
766 fprintf (stderr
, "%i %i\n", bb
->index
, bb2
->index
);