1 /* Calculate (post)dominators in slightly super-linear time.
2 Copyright (C) 2000-2015 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This file implements the well known algorithm from Lengauer and Tarjan
22 to compute the dominators in a control flow graph. A basic block D is said
23 to dominate another block X, when all paths from the entry node of the CFG
24 to X go also over D. The dominance relation is a transitive reflexive
25 relation and its minimal transitive reduction is a tree, called the
26 dominator tree. So for each block X besides the entry block exists a
27 block I(X), called the immediate dominator of X, which is the parent of X
28 in the dominator tree.
30 The algorithm computes this dominator tree implicitly by computing for
31 each block its immediate dominator. We use tree balancing and path
32 compression, so it's the O(e*a(e,v)) variant, where a(e,v) is the very
33 slowly growing functional inverse of the Ackerman function. */
37 #include "coretypes.h"
41 #include "diagnostic-core.h"
42 #include "alloc-pool.h"
43 #include "et-forest.h"
47 /* We name our nodes with integers, beginning with 1. Zero is reserved for
48 'undefined' or 'end of list'. The name of each node is given by the dfs
49 number of the corresponding basic block. Please note, that we include the
50 artificial ENTRY_BLOCK (or EXIT_BLOCK in the post-dom case) in our lists to
51 support multiple entry points. Its dfs number is of course 1. */
53 /* Type of Basic Block aka. TBB */
54 typedef unsigned int TBB
;
56 /* We work in a poor-mans object oriented fashion, and carry an instance of
57 this structure through all our 'methods'. It holds various arrays
58 reflecting the (sub)structure of the flowgraph. Most of them are of type
59 TBB and are also indexed by TBB. */
63 /* The parent of a node in the DFS tree. */
65 /* For a node x key[x] is roughly the node nearest to the root from which
66 exists a way to x only over nodes behind x. Such a node is also called
69 /* The value in path_min[x] is the node y on the path from x to the root of
70 the tree x is in with the smallest key[y]. */
72 /* bucket[x] points to the first node of the set of nodes having x as key. */
74 /* And next_bucket[x] points to the next node. */
76 /* After the algorithm is done, dom[x] contains the immediate dominator
80 /* The following few fields implement the structures needed for disjoint
82 /* set_chain[x] is the next node on the path from x to the representative
83 of the set containing x. If set_chain[x]==0 then x is a root. */
85 /* set_size[x] is the number of elements in the set named by x. */
86 unsigned int *set_size
;
87 /* set_child[x] is used for balancing the tree representing a set. It can
88 be understood as the next sibling of x. */
91 /* If b is the number of a basic block (BB->index), dfs_order[b] is the
92 number of that node in DFS order counted from 1. This is an index
93 into most of the other arrays in this structure. */
95 /* If x is the DFS-index of a node which corresponds with a basic block,
96 dfs_to_bb[x] is that basic block. Note, that in our structure there are
97 more nodes that basic blocks, so only dfs_to_bb[dfs_order[bb->index]]==bb
98 is true for every basic block bb, but not the opposite. */
99 basic_block
*dfs_to_bb
;
101 /* This is the next free DFS number when creating the DFS tree. */
103 /* The number of nodes in the DFS tree (==dfsnum-1). */
106 /* Blocks with bits set here have a fake edge to EXIT. These are used
107 to turn a DFS forest into a proper tree. */
108 bitmap fake_exit_edge
;
111 static void init_dom_info (struct dom_info
*, enum cdi_direction
);
112 static void free_dom_info (struct dom_info
*);
113 static void calc_dfs_tree_nonrec (struct dom_info
*, basic_block
, bool);
114 static void calc_dfs_tree (struct dom_info
*, bool);
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
*, bool);
119 void debug_dominance_info (enum cdi_direction
);
120 void debug_dominance_tree (enum cdi_direction
, basic_block
);
122 /* Helper macro for allocating and initializing an array,
123 for aesthetic reasons. */
124 #define init_ar(var, type, num, content) \
127 unsigned int i = 1; /* Catch content == i. */ \
129 (var) = XCNEWVEC (type, num); \
132 (var) = XNEWVEC (type, (num)); \
133 for (i = 0; i < num; i++) \
134 (var)[i] = (content); \
139 /* Allocate all needed memory in a pessimistic fashion (so we round up).
140 This initializes the contents of DI, which already must be allocated. */
143 init_dom_info (struct dom_info
*di
, enum cdi_direction dir
)
145 /* We need memory for n_basic_blocks nodes. */
146 unsigned int num
= n_basic_blocks_for_fn (cfun
);
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
,
160 (unsigned int) last_basic_block_for_fn (cfun
) + 1, 0);
161 init_ar (di
->dfs_to_bb
, basic_block
, num
, 0);
169 di
->fake_exit_edge
= NULL
;
171 case CDI_POST_DOMINATORS
:
172 di
->fake_exit_edge
= BITMAP_ALLOC (NULL
);
182 /* Map dominance calculation type to array index used for various
183 dominance information arrays. This version is simple -- it will need
184 to be modified, obviously, if additional values are added to
188 dom_convert_dir_to_idx (enum cdi_direction dir
)
190 gcc_checking_assert (dir
== CDI_DOMINATORS
|| dir
== CDI_POST_DOMINATORS
);
194 /* Free all allocated memory in DI, but not DI itself. */
197 free_dom_info (struct dom_info
*di
)
199 free (di
->dfs_parent
);
204 free (di
->next_bucket
);
205 free (di
->set_chain
);
207 free (di
->set_child
);
208 free (di
->dfs_order
);
209 free (di
->dfs_to_bb
);
210 BITMAP_FREE (di
->fake_exit_edge
);
213 /* The nonrecursive variant of creating a DFS tree. DI is our working
214 structure, BB the starting basic block for this tree and REVERSE
215 is true, if predecessors should be visited instead of successors of a
216 node. After this is done all nodes reachable from BB were visited, have
217 assigned their dfs number and are linked together to form a tree. */
220 calc_dfs_tree_nonrec (struct dom_info
*di
, basic_block bb
, bool reverse
)
222 /* We call this _only_ if bb is not already visited. */
224 TBB child_i
, my_i
= 0;
225 edge_iterator
*stack
;
226 edge_iterator ei
, einext
;
228 /* Start block (the entry block for forward problem, exit block for backward
230 basic_block en_block
;
232 basic_block ex_block
;
234 stack
= XNEWVEC (edge_iterator
, n_basic_blocks_for_fn (cfun
) + 1);
237 /* Initialize our border blocks, and the first edge. */
240 ei
= ei_start (bb
->preds
);
241 en_block
= EXIT_BLOCK_PTR_FOR_FN (cfun
);
242 ex_block
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
246 ei
= ei_start (bb
->succs
);
247 en_block
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
248 ex_block
= EXIT_BLOCK_PTR_FOR_FN (cfun
);
251 /* When the stack is empty we break out of this loop. */
256 /* This loop traverses edges e in depth first manner, and fills the
258 while (!ei_end_p (ei
))
262 /* Deduce from E the current and the next block (BB and BN), and the
268 /* If the next node BN is either already visited or a border
269 block the current edge is useless, and simply overwritten
270 with the next edge out of the current node. */
271 if (bn
== ex_block
|| di
->dfs_order
[bn
->index
])
277 einext
= ei_start (bn
->preds
);
282 if (bn
== ex_block
|| di
->dfs_order
[bn
->index
])
288 einext
= ei_start (bn
->succs
);
291 gcc_assert (bn
!= en_block
);
293 /* Fill the DFS tree info calculatable _before_ recursing. */
295 my_i
= di
->dfs_order
[bb
->index
];
297 my_i
= di
->dfs_order
[last_basic_block_for_fn (cfun
)];
298 child_i
= di
->dfs_order
[bn
->index
] = di
->dfsnum
++;
299 di
->dfs_to_bb
[child_i
] = bn
;
300 di
->dfs_parent
[child_i
] = my_i
;
302 /* Save the current point in the CFG on the stack, and recurse. */
311 /* OK. The edge-list was exhausted, meaning normally we would
312 end the recursion. After returning from the recursive call,
313 there were (may be) other statements which were run after a
314 child node was completely considered by DFS. Here is the
315 point to do it in the non-recursive variant.
316 E.g. The block just completed is in e->dest for forward DFS,
317 the block not yet completed (the parent of the one above)
318 in e->src. This could be used e.g. for computing the number of
319 descendants or the tree depth. */
325 /* The main entry for calculating the DFS tree or forest. DI is our working
326 structure and REVERSE is true, if we are interested in the reverse flow
327 graph. In that case the result is not necessarily a tree but a forest,
328 because there may be nodes from which the EXIT_BLOCK is unreachable. */
331 calc_dfs_tree (struct dom_info
*di
, bool reverse
)
333 /* The first block is the ENTRY_BLOCK (or EXIT_BLOCK if REVERSE). */
334 basic_block begin
= (reverse
335 ? EXIT_BLOCK_PTR_FOR_FN (cfun
) : ENTRY_BLOCK_PTR_FOR_FN (cfun
));
336 di
->dfs_order
[last_basic_block_for_fn (cfun
)] = di
->dfsnum
;
337 di
->dfs_to_bb
[di
->dfsnum
] = begin
;
340 calc_dfs_tree_nonrec (di
, begin
, reverse
);
344 /* In the post-dom case we may have nodes without a path to EXIT_BLOCK.
345 They are reverse-unreachable. In the dom-case we disallow such
346 nodes, but in post-dom we have to deal with them.
348 There are two situations in which this occurs. First, noreturn
349 functions. Second, infinite loops. In the first case we need to
350 pretend that there is an edge to the exit block. In the second
351 case, we wind up with a forest. We need to process all noreturn
352 blocks before we know if we've got any infinite loops. */
355 bool saw_unconnected
= false;
357 FOR_EACH_BB_REVERSE_FN (b
, cfun
)
359 if (EDGE_COUNT (b
->succs
) > 0)
361 if (di
->dfs_order
[b
->index
] == 0)
362 saw_unconnected
= true;
365 bitmap_set_bit (di
->fake_exit_edge
, b
->index
);
366 di
->dfs_order
[b
->index
] = di
->dfsnum
;
367 di
->dfs_to_bb
[di
->dfsnum
] = b
;
368 di
->dfs_parent
[di
->dfsnum
] =
369 di
->dfs_order
[last_basic_block_for_fn (cfun
)];
371 calc_dfs_tree_nonrec (di
, b
, reverse
);
376 FOR_EACH_BB_REVERSE_FN (b
, cfun
)
379 if (di
->dfs_order
[b
->index
])
381 b2
= dfs_find_deadend (b
);
382 gcc_checking_assert (di
->dfs_order
[b2
->index
] == 0);
383 bitmap_set_bit (di
->fake_exit_edge
, b2
->index
);
384 di
->dfs_order
[b2
->index
] = di
->dfsnum
;
385 di
->dfs_to_bb
[di
->dfsnum
] = b2
;
386 di
->dfs_parent
[di
->dfsnum
] =
387 di
->dfs_order
[last_basic_block_for_fn (cfun
)];
389 calc_dfs_tree_nonrec (di
, b2
, reverse
);
390 gcc_checking_assert (di
->dfs_order
[b
->index
]);
395 di
->nodes
= di
->dfsnum
- 1;
397 /* This aborts e.g. when there is _no_ path from ENTRY to EXIT at all. */
398 gcc_assert (di
->nodes
== (unsigned int) n_basic_blocks_for_fn (cfun
) - 1);
401 /* Compress the path from V to the root of its set and update path_min at the
402 same time. After compress(di, V) set_chain[V] is the root of the set V is
403 in and path_min[V] is the node with the smallest key[] value on the path
404 from V to that root. */
407 compress (struct dom_info
*di
, TBB v
)
409 /* Btw. It's not worth to unrecurse compress() as the depth is usually not
410 greater than 5 even for huge graphs (I've not seen call depth > 4).
411 Also performance wise compress() ranges _far_ behind eval(). */
412 TBB parent
= di
->set_chain
[v
];
413 if (di
->set_chain
[parent
])
415 compress (di
, parent
);
416 if (di
->key
[di
->path_min
[parent
]] < di
->key
[di
->path_min
[v
]])
417 di
->path_min
[v
] = di
->path_min
[parent
];
418 di
->set_chain
[v
] = di
->set_chain
[parent
];
422 /* Compress the path from V to the set root of V if needed (when the root has
423 changed since the last call). Returns the node with the smallest key[]
424 value on the path from V to the root. */
427 eval (struct dom_info
*di
, TBB v
)
429 /* The representative of the set V is in, also called root (as the set
430 representation is a tree). */
431 TBB rep
= di
->set_chain
[v
];
433 /* V itself is the root. */
435 return di
->path_min
[v
];
437 /* Compress only if necessary. */
438 if (di
->set_chain
[rep
])
441 rep
= di
->set_chain
[v
];
444 if (di
->key
[di
->path_min
[rep
]] >= di
->key
[di
->path_min
[v
]])
445 return di
->path_min
[v
];
447 return di
->path_min
[rep
];
450 /* This essentially merges the two sets of V and W, giving a single set with
451 the new root V. The internal representation of these disjoint sets is a
452 balanced tree. Currently link(V,W) is only used with V being the parent
456 link_roots (struct dom_info
*di
, TBB v
, TBB w
)
460 /* Rebalance the tree. */
461 while (di
->key
[di
->path_min
[w
]] < di
->key
[di
->path_min
[di
->set_child
[s
]]])
463 if (di
->set_size
[s
] + di
->set_size
[di
->set_child
[di
->set_child
[s
]]]
464 >= 2 * di
->set_size
[di
->set_child
[s
]])
466 di
->set_chain
[di
->set_child
[s
]] = s
;
467 di
->set_child
[s
] = di
->set_child
[di
->set_child
[s
]];
471 di
->set_size
[di
->set_child
[s
]] = di
->set_size
[s
];
472 s
= di
->set_chain
[s
] = di
->set_child
[s
];
476 di
->path_min
[s
] = di
->path_min
[w
];
477 di
->set_size
[v
] += di
->set_size
[w
];
478 if (di
->set_size
[v
] < 2 * di
->set_size
[w
])
479 std::swap (di
->set_child
[v
], s
);
481 /* Merge all subtrees. */
484 di
->set_chain
[s
] = v
;
485 s
= di
->set_child
[s
];
489 /* This calculates the immediate dominators (or post-dominators if REVERSE is
490 true). DI is our working structure and should hold the DFS forest.
491 On return the immediate dominator to node V is in di->dom[V]. */
494 calc_idoms (struct dom_info
*di
, bool reverse
)
497 basic_block en_block
;
498 edge_iterator ei
, einext
;
501 en_block
= EXIT_BLOCK_PTR_FOR_FN (cfun
);
503 en_block
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
505 /* Go backwards in DFS order, to first look at the leafs. */
509 basic_block bb
= di
->dfs_to_bb
[v
];
512 par
= di
->dfs_parent
[v
];
515 ei
= (reverse
) ? ei_start (bb
->succs
) : ei_start (bb
->preds
);
519 /* If this block has a fake edge to exit, process that first. */
520 if (bitmap_bit_p (di
->fake_exit_edge
, bb
->index
))
524 goto do_fake_exit_edge
;
528 /* Search all direct predecessors for the smallest node with a path
529 to them. That way we have the smallest node with also a path to
530 us only over nodes behind us. In effect we search for our
532 while (!ei_end_p (ei
))
538 b
= (reverse
) ? e
->dest
: e
->src
;
545 k1
= di
->dfs_order
[last_basic_block_for_fn (cfun
)];
548 k1
= di
->dfs_order
[b
->index
];
550 /* Call eval() only if really needed. If k1 is above V in DFS tree,
551 then we know, that eval(k1) == k1 and key[k1] == k1. */
553 k1
= di
->key
[eval (di
, k1
)];
561 link_roots (di
, par
, v
);
562 di
->next_bucket
[v
] = di
->bucket
[k
];
565 /* Transform semidominators into dominators. */
566 for (w
= di
->bucket
[par
]; w
; w
= di
->next_bucket
[w
])
569 if (di
->key
[k
] < di
->key
[w
])
574 /* We don't need to cleanup next_bucket[]. */
579 /* Explicitly define the dominators. */
581 for (v
= 2; v
<= di
->nodes
; v
++)
582 if (di
->dom
[v
] != di
->key
[v
])
583 di
->dom
[v
] = di
->dom
[di
->dom
[v
]];
586 /* Assign dfs numbers starting from NUM to NODE and its sons. */
589 assign_dfs_numbers (struct et_node
*node
, int *num
)
593 node
->dfs_num_in
= (*num
)++;
597 assign_dfs_numbers (node
->son
, num
);
598 for (son
= node
->son
->right
; son
!= node
->son
; son
= son
->right
)
599 assign_dfs_numbers (son
, num
);
602 node
->dfs_num_out
= (*num
)++;
605 /* Compute the data necessary for fast resolving of dominator queries in a
606 static dominator tree. */
609 compute_dom_fast_query (enum cdi_direction dir
)
613 unsigned int dir_index
= dom_convert_dir_to_idx (dir
);
615 gcc_checking_assert (dom_info_available_p (dir
));
617 if (dom_computed
[dir_index
] == DOM_OK
)
620 FOR_ALL_BB_FN (bb
, cfun
)
622 if (!bb
->dom
[dir_index
]->father
)
623 assign_dfs_numbers (bb
->dom
[dir_index
], &num
);
626 dom_computed
[dir_index
] = DOM_OK
;
629 /* The main entry point into this module. DIR is set depending on whether
630 we want to compute dominators or postdominators. */
633 calculate_dominance_info (enum cdi_direction dir
)
637 unsigned int dir_index
= dom_convert_dir_to_idx (dir
);
638 bool reverse
= (dir
== CDI_POST_DOMINATORS
) ? true : false;
640 if (dom_computed
[dir_index
] == DOM_OK
)
643 verify_dominators (dir
);
648 timevar_push (TV_DOMINANCE
);
649 if (!dom_info_available_p (dir
))
651 gcc_assert (!n_bbs_in_dom_tree
[dir_index
]);
653 FOR_ALL_BB_FN (b
, cfun
)
655 b
->dom
[dir_index
] = et_new_tree (b
);
657 n_bbs_in_dom_tree
[dir_index
] = n_basic_blocks_for_fn (cfun
);
659 init_dom_info (&di
, dir
);
660 calc_dfs_tree (&di
, reverse
);
661 calc_idoms (&di
, reverse
);
663 FOR_EACH_BB_FN (b
, cfun
)
665 TBB d
= di
.dom
[di
.dfs_order
[b
->index
]];
668 et_set_father (b
->dom
[dir_index
], di
.dfs_to_bb
[d
]->dom
[dir_index
]);
672 dom_computed
[dir_index
] = DOM_NO_FAST_QUERY
;
677 verify_dominators (dir
);
681 compute_dom_fast_query (dir
);
683 timevar_pop (TV_DOMINANCE
);
686 /* Free dominance information for direction DIR. */
688 free_dominance_info (function
*fn
, enum cdi_direction dir
)
691 unsigned int dir_index
= dom_convert_dir_to_idx (dir
);
693 if (!dom_info_available_p (fn
, dir
))
696 FOR_ALL_BB_FN (bb
, fn
)
698 et_free_tree_force (bb
->dom
[dir_index
]);
699 bb
->dom
[dir_index
] = NULL
;
703 fn
->cfg
->x_n_bbs_in_dom_tree
[dir_index
] = 0;
705 fn
->cfg
->x_dom_computed
[dir_index
] = DOM_NONE
;
709 free_dominance_info (enum cdi_direction dir
)
711 free_dominance_info (cfun
, dir
);
714 /* Return the immediate dominator of basic block BB. */
716 get_immediate_dominator (enum cdi_direction dir
, basic_block bb
)
718 unsigned int dir_index
= dom_convert_dir_to_idx (dir
);
719 struct et_node
*node
= bb
->dom
[dir_index
];
721 gcc_checking_assert (dom_computed
[dir_index
]);
726 return (basic_block
) node
->father
->data
;
729 /* Set the immediate dominator of the block possibly removing
730 existing edge. NULL can be used to remove any edge. */
732 set_immediate_dominator (enum cdi_direction dir
, basic_block bb
,
733 basic_block dominated_by
)
735 unsigned int dir_index
= dom_convert_dir_to_idx (dir
);
736 struct et_node
*node
= bb
->dom
[dir_index
];
738 gcc_checking_assert (dom_computed
[dir_index
]);
742 if (node
->father
->data
== dominated_by
)
748 et_set_father (node
, dominated_by
->dom
[dir_index
]);
750 if (dom_computed
[dir_index
] == DOM_OK
)
751 dom_computed
[dir_index
] = DOM_NO_FAST_QUERY
;
754 /* Returns the list of basic blocks immediately dominated by BB, in the
757 get_dominated_by (enum cdi_direction dir
, basic_block bb
)
759 unsigned int dir_index
= dom_convert_dir_to_idx (dir
);
760 struct et_node
*node
= bb
->dom
[dir_index
], *son
= node
->son
, *ason
;
761 vec
<basic_block
> bbs
= vNULL
;
763 gcc_checking_assert (dom_computed
[dir_index
]);
768 bbs
.safe_push ((basic_block
) son
->data
);
769 for (ason
= son
->right
; ason
!= son
; ason
= ason
->right
)
770 bbs
.safe_push ((basic_block
) ason
->data
);
775 /* Returns the list of basic blocks that are immediately dominated (in
776 direction DIR) by some block between N_REGION ones stored in REGION,
777 except for blocks in the REGION itself. */
780 get_dominated_by_region (enum cdi_direction dir
, basic_block
*region
,
785 vec
<basic_block
> doms
= vNULL
;
787 for (i
= 0; i
< n_region
; i
++)
788 region
[i
]->flags
|= BB_DUPLICATED
;
789 for (i
= 0; i
< n_region
; i
++)
790 for (dom
= first_dom_son (dir
, region
[i
]);
792 dom
= next_dom_son (dir
, dom
))
793 if (!(dom
->flags
& BB_DUPLICATED
))
794 doms
.safe_push (dom
);
795 for (i
= 0; i
< n_region
; i
++)
796 region
[i
]->flags
&= ~BB_DUPLICATED
;
801 /* Returns the list of basic blocks including BB dominated by BB, in the
802 direction DIR up to DEPTH in the dominator tree. The DEPTH of zero will
803 produce a vector containing all dominated blocks. The vector will be sorted
807 get_dominated_to_depth (enum cdi_direction dir
, basic_block bb
, int depth
)
809 vec
<basic_block
> bbs
= vNULL
;
811 unsigned next_level_start
;
815 next_level_start
= 1; /* = bbs.length (); */
822 for (son
= first_dom_son (dir
, bb
);
824 son
= next_dom_son (dir
, son
))
827 if (i
== next_level_start
&& --depth
)
828 next_level_start
= bbs
.length ();
830 while (i
< next_level_start
);
835 /* Returns the list of basic blocks including BB dominated by BB, in the
836 direction DIR. The vector will be sorted in preorder. */
839 get_all_dominated_blocks (enum cdi_direction dir
, basic_block bb
)
841 return get_dominated_to_depth (dir
, bb
, 0);
844 /* Redirect all edges pointing to BB to TO. */
846 redirect_immediate_dominators (enum cdi_direction dir
, basic_block bb
,
849 unsigned int dir_index
= dom_convert_dir_to_idx (dir
);
850 struct et_node
*bb_node
, *to_node
, *son
;
852 bb_node
= bb
->dom
[dir_index
];
853 to_node
= to
->dom
[dir_index
];
855 gcc_checking_assert (dom_computed
[dir_index
]);
865 et_set_father (son
, to_node
);
868 if (dom_computed
[dir_index
] == DOM_OK
)
869 dom_computed
[dir_index
] = DOM_NO_FAST_QUERY
;
872 /* Find first basic block in the tree dominating both BB1 and BB2. */
874 nearest_common_dominator (enum cdi_direction dir
, basic_block bb1
, basic_block bb2
)
876 unsigned int dir_index
= dom_convert_dir_to_idx (dir
);
878 gcc_checking_assert (dom_computed
[dir_index
]);
885 return (basic_block
) et_nca (bb1
->dom
[dir_index
], bb2
->dom
[dir_index
])->data
;
889 /* Find the nearest common dominator for the basic blocks in BLOCKS,
890 using dominance direction DIR. */
893 nearest_common_dominator_for_set (enum cdi_direction dir
, bitmap blocks
)
899 first
= bitmap_first_set_bit (blocks
);
900 dom
= BASIC_BLOCK_FOR_FN (cfun
, first
);
901 EXECUTE_IF_SET_IN_BITMAP (blocks
, 0, i
, bi
)
902 if (dom
!= BASIC_BLOCK_FOR_FN (cfun
, i
))
903 dom
= nearest_common_dominator (dir
, dom
, BASIC_BLOCK_FOR_FN (cfun
, i
));
908 /* Given a dominator tree, we can determine whether one thing
909 dominates another in constant time by using two DFS numbers:
911 1. The number for when we visit a node on the way down the tree
912 2. The number for when we visit a node on the way back up the tree
914 You can view these as bounds for the range of dfs numbers the
915 nodes in the subtree of the dominator tree rooted at that node
918 The dominator tree is always a simple acyclic tree, so there are
919 only three possible relations two nodes in the dominator tree have
922 1. Node A is above Node B (and thus, Node A dominates node B)
931 In the above case, DFS_Number_In of A will be <= DFS_Number_In of
932 B, and DFS_Number_Out of A will be >= DFS_Number_Out of B. This is
933 because we must hit A in the dominator tree *before* B on the walk
934 down, and we will hit A *after* B on the walk back up
936 2. Node A is below node B (and thus, node B dominates node A)
945 In the above case, DFS_Number_In of A will be >= DFS_Number_In of
946 B, and DFS_Number_Out of A will be <= DFS_Number_Out of B.
948 This is because we must hit A in the dominator tree *after* B on
949 the walk down, and we will hit A *before* B on the walk back up
951 3. Node A and B are siblings (and thus, neither dominates the other)
959 In the above case, DFS_Number_In of A will *always* be <=
960 DFS_Number_In of B, and DFS_Number_Out of A will *always* be <=
961 DFS_Number_Out of B. This is because we will always finish the dfs
962 walk of one of the subtrees before the other, and thus, the dfs
963 numbers for one subtree can't intersect with the range of dfs
964 numbers for the other subtree. If you swap A and B's position in
965 the dominator tree, the comparison changes direction, but the point
966 is that both comparisons will always go the same way if there is no
967 dominance relationship.
969 Thus, it is sufficient to write
971 A_Dominates_B (node A, node B)
973 return DFS_Number_In(A) <= DFS_Number_In(B)
974 && DFS_Number_Out (A) >= DFS_Number_Out(B);
977 A_Dominated_by_B (node A, node B)
979 return DFS_Number_In(A) >= DFS_Number_In(B)
980 && DFS_Number_Out (A) <= DFS_Number_Out(B);
983 /* Return TRUE in case BB1 is dominated by BB2. */
985 dominated_by_p (enum cdi_direction dir
, const_basic_block bb1
, const_basic_block bb2
)
987 unsigned int dir_index
= dom_convert_dir_to_idx (dir
);
988 struct et_node
*n1
= bb1
->dom
[dir_index
], *n2
= bb2
->dom
[dir_index
];
990 gcc_checking_assert (dom_computed
[dir_index
]);
992 if (dom_computed
[dir_index
] == DOM_OK
)
993 return (n1
->dfs_num_in
>= n2
->dfs_num_in
994 && n1
->dfs_num_out
<= n2
->dfs_num_out
);
996 return et_below (n1
, n2
);
999 /* Returns the entry dfs number for basic block BB, in the direction DIR. */
1002 bb_dom_dfs_in (enum cdi_direction dir
, basic_block bb
)
1004 unsigned int dir_index
= dom_convert_dir_to_idx (dir
);
1005 struct et_node
*n
= bb
->dom
[dir_index
];
1007 gcc_checking_assert (dom_computed
[dir_index
] == DOM_OK
);
1008 return n
->dfs_num_in
;
1011 /* Returns the exit dfs number for basic block BB, in the direction DIR. */
1014 bb_dom_dfs_out (enum cdi_direction dir
, basic_block bb
)
1016 unsigned int dir_index
= dom_convert_dir_to_idx (dir
);
1017 struct et_node
*n
= bb
->dom
[dir_index
];
1019 gcc_checking_assert (dom_computed
[dir_index
] == DOM_OK
);
1020 return n
->dfs_num_out
;
1023 /* Verify invariants of dominator structure. */
1025 verify_dominators (enum cdi_direction dir
)
1028 basic_block bb
, imm_bb
, imm_bb_correct
;
1030 bool reverse
= (dir
== CDI_POST_DOMINATORS
) ? true : false;
1032 gcc_assert (dom_info_available_p (dir
));
1034 init_dom_info (&di
, dir
);
1035 calc_dfs_tree (&di
, reverse
);
1036 calc_idoms (&di
, reverse
);
1038 FOR_EACH_BB_FN (bb
, cfun
)
1040 imm_bb
= get_immediate_dominator (dir
, bb
);
1043 error ("dominator of %d status unknown", bb
->index
);
1047 imm_bb_correct
= di
.dfs_to_bb
[di
.dom
[di
.dfs_order
[bb
->index
]]];
1048 if (imm_bb
!= imm_bb_correct
)
1050 error ("dominator of %d should be %d, not %d",
1051 bb
->index
, imm_bb_correct
->index
, imm_bb
->index
);
1056 free_dom_info (&di
);
1060 /* Determine immediate dominator (or postdominator, according to DIR) of BB,
1061 assuming that dominators of other blocks are correct. We also use it to
1062 recompute the dominators in a restricted area, by iterating it until it
1063 reaches a fixed point. */
1066 recompute_dominator (enum cdi_direction dir
, basic_block bb
)
1068 unsigned int dir_index
= dom_convert_dir_to_idx (dir
);
1069 basic_block dom_bb
= NULL
;
1073 gcc_checking_assert (dom_computed
[dir_index
]);
1075 if (dir
== CDI_DOMINATORS
)
1077 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1079 if (!dominated_by_p (dir
, e
->src
, bb
))
1080 dom_bb
= nearest_common_dominator (dir
, dom_bb
, e
->src
);
1085 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1087 if (!dominated_by_p (dir
, e
->dest
, bb
))
1088 dom_bb
= nearest_common_dominator (dir
, dom_bb
, e
->dest
);
1095 /* Use simple heuristics (see iterate_fix_dominators) to determine dominators
1096 of BBS. We assume that all the immediate dominators except for those of the
1097 blocks in BBS are correct. If CONSERVATIVE is true, we also assume that the
1098 currently recorded immediate dominators of blocks in BBS really dominate the
1099 blocks. The basic blocks for that we determine the dominator are removed
1103 prune_bbs_to_update_dominators (vec
<basic_block
> bbs
,
1108 basic_block bb
, dom
= NULL
;
1112 for (i
= 0; bbs
.iterate (i
, &bb
);)
1114 if (bb
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1117 if (single_pred_p (bb
))
1119 set_immediate_dominator (CDI_DOMINATORS
, bb
, single_pred (bb
));
1128 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1130 if (dominated_by_p (CDI_DOMINATORS
, e
->src
, bb
))
1138 dom
= nearest_common_dominator (CDI_DOMINATORS
, dom
, e
->src
);
1142 gcc_assert (dom
!= NULL
);
1144 || find_edge (dom
, bb
))
1146 set_immediate_dominator (CDI_DOMINATORS
, bb
, dom
);
1155 bbs
.unordered_remove (i
);
1159 /* Returns root of the dominance tree in the direction DIR that contains
1163 root_of_dom_tree (enum cdi_direction dir
, basic_block bb
)
1165 return (basic_block
) et_root (bb
->dom
[dom_convert_dir_to_idx (dir
)])->data
;
1168 /* See the comment in iterate_fix_dominators. Finds the immediate dominators
1169 for the sons of Y, found using the SON and BROTHER arrays representing
1170 the dominance tree of graph G. BBS maps the vertices of G to the basic
1174 determine_dominators_for_sons (struct graph
*g
, vec
<basic_block
> bbs
,
1175 int y
, int *son
, int *brother
)
1180 basic_block bb
, dom
, ybb
;
1187 if (y
== (int) bbs
.length ())
1188 ybb
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
1192 if (brother
[son
[y
]] == -1)
1194 /* Handle the common case Y has just one son specially. */
1196 set_immediate_dominator (CDI_DOMINATORS
, bb
,
1197 recompute_dominator (CDI_DOMINATORS
, bb
));
1198 identify_vertices (g
, y
, son
[y
]);
1202 gprime
= BITMAP_ALLOC (NULL
);
1203 for (a
= son
[y
]; a
!= -1; a
= brother
[a
])
1204 bitmap_set_bit (gprime
, a
);
1206 nc
= graphds_scc (g
, gprime
);
1207 BITMAP_FREE (gprime
);
1209 /* ??? Needed to work around the pre-processor confusion with
1210 using a multi-argument template type as macro argument. */
1211 typedef vec
<int> vec_int_heap
;
1212 sccs
= XCNEWVEC (vec_int_heap
, nc
);
1213 for (a
= son
[y
]; a
!= -1; a
= brother
[a
])
1214 sccs
[g
->vertices
[a
].component
].safe_push (a
);
1216 for (i
= nc
- 1; i
>= 0; i
--)
1219 FOR_EACH_VEC_ELT (sccs
[i
], si
, a
)
1222 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1224 if (root_of_dom_tree (CDI_DOMINATORS
, e
->src
) != ybb
)
1227 dom
= nearest_common_dominator (CDI_DOMINATORS
, dom
, e
->src
);
1231 gcc_assert (dom
!= NULL
);
1232 FOR_EACH_VEC_ELT (sccs
[i
], si
, a
)
1235 set_immediate_dominator (CDI_DOMINATORS
, bb
, dom
);
1239 for (i
= 0; i
< nc
; i
++)
1243 for (a
= son
[y
]; a
!= -1; a
= brother
[a
])
1244 identify_vertices (g
, y
, a
);
1247 /* Recompute dominance information for basic blocks in the set BBS. The
1248 function assumes that the immediate dominators of all the other blocks
1249 in CFG are correct, and that there are no unreachable blocks.
1251 If CONSERVATIVE is true, we additionally assume that all the ancestors of
1252 a block of BBS in the current dominance tree dominate it. */
1255 iterate_fix_dominators (enum cdi_direction dir
, vec
<basic_block
> bbs
,
1259 basic_block bb
, dom
;
1265 int *parent
, *son
, *brother
;
1266 unsigned int dir_index
= dom_convert_dir_to_idx (dir
);
1268 /* We only support updating dominators. There are some problems with
1269 updating postdominators (need to add fake edges from infinite loops
1270 and noreturn functions), and since we do not currently use
1271 iterate_fix_dominators for postdominators, any attempt to handle these
1272 problems would be unused, untested, and almost surely buggy. We keep
1273 the DIR argument for consistency with the rest of the dominator analysis
1275 gcc_checking_assert (dir
== CDI_DOMINATORS
&& dom_computed
[dir_index
]);
1277 /* The algorithm we use takes inspiration from the following papers, although
1278 the details are quite different from any of them:
1280 [1] G. Ramalingam, T. Reps, An Incremental Algorithm for Maintaining the
1281 Dominator Tree of a Reducible Flowgraph
1282 [2] V. C. Sreedhar, G. R. Gao, Y.-F. Lee: Incremental computation of
1284 [3] K. D. Cooper, T. J. Harvey and K. Kennedy: A Simple, Fast Dominance
1287 First, we use the following heuristics to decrease the size of the BBS
1289 a) if BB has a single predecessor, then its immediate dominator is this
1291 additionally, if CONSERVATIVE is true:
1292 b) if all the predecessors of BB except for one (X) are dominated by BB,
1293 then X is the immediate dominator of BB
1294 c) if the nearest common ancestor of the predecessors of BB is X and
1295 X -> BB is an edge in CFG, then X is the immediate dominator of BB
1297 Then, we need to establish the dominance relation among the basic blocks
1298 in BBS. We split the dominance tree by removing the immediate dominator
1299 edges from BBS, creating a forest F. We form a graph G whose vertices
1300 are BBS and ENTRY and X -> Y is an edge of G if there exists an edge
1301 X' -> Y in CFG such that X' belongs to the tree of the dominance forest
1302 whose root is X. We then determine dominance tree of G. Note that
1303 for X, Y in BBS, X dominates Y in CFG if and only if X dominates Y in G.
1304 In this step, we can use arbitrary algorithm to determine dominators.
1305 We decided to prefer the algorithm [3] to the algorithm of
1306 Lengauer and Tarjan, since the set BBS is usually small (rarely exceeding
1307 10 during gcc bootstrap), and [3] should perform better in this case.
1309 Finally, we need to determine the immediate dominators for the basic
1310 blocks of BBS. If the immediate dominator of X in G is Y, then
1311 the immediate dominator of X in CFG belongs to the tree of F rooted in
1312 Y. We process the dominator tree T of G recursively, starting from leaves.
1313 Suppose that X_1, X_2, ..., X_k are the sons of Y in T, and that the
1314 subtrees of the dominance tree of CFG rooted in X_i are already correct.
1315 Let G' be the subgraph of G induced by {X_1, X_2, ..., X_k}. We make
1316 the following observations:
1317 (i) the immediate dominator of all blocks in a strongly connected
1318 component of G' is the same
1319 (ii) if X has no predecessors in G', then the immediate dominator of X
1320 is the nearest common ancestor of the predecessors of X in the
1321 subtree of F rooted in Y
1322 Therefore, it suffices to find the topological ordering of G', and
1323 process the nodes X_i in this order using the rules (i) and (ii).
1324 Then, we contract all the nodes X_i with Y in G, so that the further
1325 steps work correctly. */
1329 /* Split the tree now. If the idoms of blocks in BBS are not
1330 conservatively correct, setting the dominators using the
1331 heuristics in prune_bbs_to_update_dominators could
1332 create cycles in the dominance "tree", and cause ICE. */
1333 FOR_EACH_VEC_ELT (bbs
, i
, bb
)
1334 set_immediate_dominator (CDI_DOMINATORS
, bb
, NULL
);
1337 prune_bbs_to_update_dominators (bbs
, conservative
);
1346 set_immediate_dominator (CDI_DOMINATORS
, bb
,
1347 recompute_dominator (CDI_DOMINATORS
, bb
));
1351 /* Construct the graph G. */
1352 hash_map
<basic_block
, int> map (251);
1353 FOR_EACH_VEC_ELT (bbs
, i
, bb
)
1355 /* If the dominance tree is conservatively correct, split it now. */
1357 set_immediate_dominator (CDI_DOMINATORS
, bb
, NULL
);
1360 map
.put (ENTRY_BLOCK_PTR_FOR_FN (cfun
), n
);
1362 g
= new_graph (n
+ 1);
1363 for (y
= 0; y
< g
->n_vertices
; y
++)
1364 g
->vertices
[y
].data
= BITMAP_ALLOC (NULL
);
1365 FOR_EACH_VEC_ELT (bbs
, i
, bb
)
1367 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1369 dom
= root_of_dom_tree (CDI_DOMINATORS
, e
->src
);
1373 dom_i
= *map
.get (dom
);
1375 /* Do not include parallel edges to G. */
1376 if (!bitmap_set_bit ((bitmap
) g
->vertices
[dom_i
].data
, i
))
1379 add_edge (g
, dom_i
, i
);
1382 for (y
= 0; y
< g
->n_vertices
; y
++)
1383 BITMAP_FREE (g
->vertices
[y
].data
);
1385 /* Find the dominator tree of G. */
1386 son
= XNEWVEC (int, n
+ 1);
1387 brother
= XNEWVEC (int, n
+ 1);
1388 parent
= XNEWVEC (int, n
+ 1);
1389 graphds_domtree (g
, n
, parent
, son
, brother
);
1391 /* Finally, traverse the tree and find the immediate dominators. */
1392 for (y
= n
; son
[y
] != -1; y
= son
[y
])
1396 determine_dominators_for_sons (g
, bbs
, y
, son
, brother
);
1398 if (brother
[y
] != -1)
1401 while (son
[y
] != -1)
1416 add_to_dominance_info (enum cdi_direction dir
, basic_block bb
)
1418 unsigned int dir_index
= dom_convert_dir_to_idx (dir
);
1420 gcc_checking_assert (dom_computed
[dir_index
] && !bb
->dom
[dir_index
]);
1422 n_bbs_in_dom_tree
[dir_index
]++;
1424 bb
->dom
[dir_index
] = et_new_tree (bb
);
1426 if (dom_computed
[dir_index
] == DOM_OK
)
1427 dom_computed
[dir_index
] = DOM_NO_FAST_QUERY
;
1431 delete_from_dominance_info (enum cdi_direction dir
, basic_block bb
)
1433 unsigned int dir_index
= dom_convert_dir_to_idx (dir
);
1435 gcc_checking_assert (dom_computed
[dir_index
]);
1437 et_free_tree (bb
->dom
[dir_index
]);
1438 bb
->dom
[dir_index
] = NULL
;
1439 n_bbs_in_dom_tree
[dir_index
]--;
1441 if (dom_computed
[dir_index
] == DOM_OK
)
1442 dom_computed
[dir_index
] = DOM_NO_FAST_QUERY
;
1445 /* Returns the first son of BB in the dominator or postdominator tree
1446 as determined by DIR. */
1449 first_dom_son (enum cdi_direction dir
, basic_block bb
)
1451 unsigned int dir_index
= dom_convert_dir_to_idx (dir
);
1452 struct et_node
*son
= bb
->dom
[dir_index
]->son
;
1454 return (basic_block
) (son
? son
->data
: NULL
);
1457 /* Returns the next dominance son after BB in the dominator or postdominator
1458 tree as determined by DIR, or NULL if it was the last one. */
1461 next_dom_son (enum cdi_direction dir
, basic_block bb
)
1463 unsigned int dir_index
= dom_convert_dir_to_idx (dir
);
1464 struct et_node
*next
= bb
->dom
[dir_index
]->right
;
1466 return (basic_block
) (next
->father
->son
== next
? NULL
: next
->data
);
1469 /* Return dominance availability for dominance info DIR. */
1472 dom_info_state (function
*fn
, enum cdi_direction dir
)
1477 unsigned int dir_index
= dom_convert_dir_to_idx (dir
);
1478 return fn
->cfg
->x_dom_computed
[dir_index
];
1482 dom_info_state (enum cdi_direction dir
)
1484 return dom_info_state (cfun
, dir
);
1487 /* Set the dominance availability for dominance info DIR to NEW_STATE. */
1490 set_dom_info_availability (enum cdi_direction dir
, enum dom_state new_state
)
1492 unsigned int dir_index
= dom_convert_dir_to_idx (dir
);
1494 dom_computed
[dir_index
] = new_state
;
1497 /* Returns true if dominance information for direction DIR is available. */
1500 dom_info_available_p (function
*fn
, enum cdi_direction dir
)
1502 return dom_info_state (fn
, dir
) != DOM_NONE
;
1506 dom_info_available_p (enum cdi_direction dir
)
1508 return dom_info_available_p (cfun
, dir
);
1512 debug_dominance_info (enum cdi_direction dir
)
1514 basic_block bb
, bb2
;
1515 FOR_EACH_BB_FN (bb
, cfun
)
1516 if ((bb2
= get_immediate_dominator (dir
, bb
)))
1517 fprintf (stderr
, "%i %i\n", bb
->index
, bb2
->index
);
1520 /* Prints to stderr representation of the dominance tree (for direction DIR)
1521 rooted in ROOT, indented by INDENT tabulators. If INDENT_FIRST is false,
1522 the first line of the output is not indented. */
1525 debug_dominance_tree_1 (enum cdi_direction dir
, basic_block root
,
1526 unsigned indent
, bool indent_first
)
1533 for (i
= 0; i
< indent
; i
++)
1534 fprintf (stderr
, "\t");
1535 fprintf (stderr
, "%d\t", root
->index
);
1537 for (son
= first_dom_son (dir
, root
);
1539 son
= next_dom_son (dir
, son
))
1541 debug_dominance_tree_1 (dir
, son
, indent
+ 1, !first
);
1546 fprintf (stderr
, "\n");
1549 /* Prints to stderr representation of the dominance tree (for direction DIR)
1553 debug_dominance_tree (enum cdi_direction dir
, basic_block root
)
1555 debug_dominance_tree_1 (dir
, root
, 0, false);