]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/dominance.c
Revert the attempted fix for c++/69855, it breaks bootstrap.
[thirdparty/gcc.git] / gcc / dominance.c
CommitLineData
f8032688 1/* Calculate (post)dominators in slightly super-linear time.
818ab71a 2 Copyright (C) 2000-2016 Free Software Foundation, Inc.
f8032688 3 Contributed by Michael Matz (matz@ifh.de).
3a538a66 4
1322177d 5 This file is part of GCC.
3a538a66 6
1322177d
LB
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
9dcd6f09 9 the Free Software Foundation; either version 3, or (at your option)
f8032688
MM
10 any later version.
11
1322177d
LB
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.
f8032688
MM
16
17 You should have received a copy of the GNU General Public License
9dcd6f09
NC
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
f8032688
MM
20
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.
29
a1f300c0 30 The algorithm computes this dominator tree implicitly by computing for
f8032688 31 each block its immediate dominator. We use tree balancing and path
f3b569ca 32 compression, so it's the O(e*a(e,v)) variant, where a(e,v) is the very
f8032688
MM
33 slowly growing functional inverse of the Ackerman function. */
34
35#include "config.h"
36#include "system.h"
4977bab6 37#include "coretypes.h"
c7131fb2 38#include "backend.h"
74c96e0c 39#include "timevar.h"
957060b5
AM
40#include "diagnostic-core.h"
41#include "cfganal.h"
42#include "et-forest.h"
66f97d31 43#include "graphds.h"
f8032688 44
f8032688
MM
45/* We name our nodes with integers, beginning with 1. Zero is reserved for
46 'undefined' or 'end of list'. The name of each node is given by the dfs
47 number of the corresponding basic block. Please note, that we include the
48 artificial ENTRY_BLOCK (or EXIT_BLOCK in the post-dom case) in our lists to
24bd1a0b 49 support multiple entry points. Its dfs number is of course 1. */
f8032688
MM
50
51/* Type of Basic Block aka. TBB */
52typedef unsigned int TBB;
53
2321dd91 54namespace {
f8032688 55
2321dd91
MM
56/* This class holds various arrays reflecting the (sub)structure of the
57 flowgraph. Most of them are of type TBB and are also indexed by TBB. */
58
59class dom_info
f8032688 60{
2321dd91
MM
61public:
62 dom_info (function *, cdi_direction);
63 ~dom_info ();
64 void calc_dfs_tree ();
65 void calc_idoms ();
66
67 inline basic_block get_idom (basic_block);
68private:
69 void calc_dfs_tree_nonrec (basic_block);
70 void compress (TBB);
71 TBB eval (TBB);
72 void link_roots (TBB, TBB);
73
f8032688 74 /* The parent of a node in the DFS tree. */
2321dd91
MM
75 TBB *m_dfs_parent;
76 /* For a node x m_key[x] is roughly the node nearest to the root from which
f8032688
MM
77 exists a way to x only over nodes behind x. Such a node is also called
78 semidominator. */
2321dd91
MM
79 TBB *m_key;
80 /* The value in m_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 m_key[y]. */
82 TBB *m_path_min;
83 /* m_bucket[x] points to the first node of the set of nodes having x as
84 key. */
85 TBB *m_bucket;
86 /* And m_next_bucket[x] points to the next node. */
87 TBB *m_next_bucket;
88 /* After the algorithm is done, m_dom[x] contains the immediate dominator
f8032688 89 of x. */
2321dd91 90 TBB *m_dom;
f8032688
MM
91
92 /* The following few fields implement the structures needed for disjoint
93 sets. */
2321dd91
MM
94 /* m_set_chain[x] is the next node on the path from x to the representative
95 of the set containing x. If m_set_chain[x]==0 then x is a root. */
96 TBB *m_set_chain;
97 /* m_set_size[x] is the number of elements in the set named by x. */
98 unsigned int *m_set_size;
99 /* m_set_child[x] is used for balancing the tree representing a set. It can
f8032688 100 be understood as the next sibling of x. */
2321dd91 101 TBB *m_set_child;
f8032688 102
2321dd91 103 /* If b is the number of a basic block (BB->index), m_dfs_order[b] is the
f8032688
MM
104 number of that node in DFS order counted from 1. This is an index
105 into most of the other arrays in this structure. */
2321dd91
MM
106 TBB *m_dfs_order;
107 /* Points to last element in m_dfs_order array. */
108 TBB *m_dfs_last;
09da1532 109 /* If x is the DFS-index of a node which corresponds with a basic block,
2321dd91
MM
110 m_dfs_to_bb[x] is that basic block. Note, that in our structure there are
111 more nodes that basic blocks, so only
112 m_dfs_to_bb[m_dfs_order[bb->index]]==bb is true for every basic block bb,
113 but not the opposite. */
114 basic_block *m_dfs_to_bb;
f8032688 115
26e0e410 116 /* This is the next free DFS number when creating the DFS tree. */
2321dd91
MM
117 unsigned int m_dfsnum;
118 /* The number of nodes in the DFS tree (==m_dfsnum-1). */
119 unsigned int m_nodes;
26e0e410
RH
120
121 /* Blocks with bits set here have a fake edge to EXIT. These are used
122 to turn a DFS forest into a proper tree. */
2321dd91
MM
123 bitmap m_fake_exit_edge;
124
125 /* Number of basic blocks in the function being compiled. */
126 size_t m_n_basic_blocks;
127
128 /* True, if we are computing postdominators (rather than dominators). */
129 bool m_reverse;
130
131 /* Start block (the entry block for forward problem, exit block for backward
132 problem). */
133 basic_block m_start_block;
134 /* Ending block. */
135 basic_block m_end_block;
f8032688
MM
136};
137
2321dd91 138} // anonymous namespace
f8032688 139
2321dd91
MM
140void debug_dominance_info (cdi_direction);
141void debug_dominance_tree (cdi_direction, basic_block);
142
143/* Allocate and zero-initialize NUM elements of type T (T must be a
144 POD-type). Note: after transition to C++11 or later,
145 `x = new_zero_array <T> (num);' can be replaced with
146 `x = new T[num] {};'. */
147
148template<typename T>
149inline T *new_zero_array (size_t num)
150{
151 T *result = new T[num];
152 memset (result, 0, sizeof (T) * num);
153 return result;
154}
155
156/* Allocate all needed memory in a pessimistic fashion (so we round up). */
157
158dom_info::dom_info (function *fn, cdi_direction dir)
f8032688 159{
6fb5fa3c 160 /* We need memory for n_basic_blocks nodes. */
2321dd91
MM
161 size_t num = m_n_basic_blocks = n_basic_blocks_for_fn (fn);
162 m_dfs_parent = new_zero_array <TBB> (num);
163 m_dom = new_zero_array <TBB> (num);
164
165 m_path_min = new TBB[num];
166 m_key = new TBB[num];
167 m_set_size = new unsigned int[num];
168 for (size_t i = 0; i < num; i++)
169 {
170 m_path_min[i] = m_key[i] = i;
171 m_set_size[i] = 1;
172 }
f8032688 173
2321dd91
MM
174 m_bucket = new_zero_array <TBB> (num);
175 m_next_bucket = new_zero_array <TBB> (num);
f8032688 176
2321dd91
MM
177 m_set_chain = new_zero_array <TBB> (num);
178 m_set_child = new_zero_array <TBB> (num);
f8032688 179
2321dd91
MM
180 unsigned last_bb_index = last_basic_block_for_fn (fn);
181 m_dfs_order = new_zero_array <TBB> (last_bb_index + 1);
182 m_dfs_last = &m_dfs_order[last_bb_index];
183 m_dfs_to_bb = new_zero_array <basic_block> (num);
f8032688 184
2321dd91
MM
185 m_dfsnum = 1;
186 m_nodes = 0;
26e0e410 187
2b28c07a
JC
188 switch (dir)
189 {
190 case CDI_DOMINATORS:
2321dd91
MM
191 m_reverse = false;
192 m_fake_exit_edge = NULL;
193 m_start_block = ENTRY_BLOCK_PTR_FOR_FN (fn);
194 m_end_block = EXIT_BLOCK_PTR_FOR_FN (fn);
2b28c07a
JC
195 break;
196 case CDI_POST_DOMINATORS:
2321dd91
MM
197 m_reverse = true;
198 m_fake_exit_edge = BITMAP_ALLOC (NULL);
199 m_start_block = EXIT_BLOCK_PTR_FOR_FN (fn);
200 m_end_block = ENTRY_BLOCK_PTR_FOR_FN (fn);
2b28c07a
JC
201 break;
202 default:
203 gcc_unreachable ();
2b28c07a 204 }
f8032688
MM
205}
206
2321dd91
MM
207inline basic_block
208dom_info::get_idom (basic_block bb)
209{
210 TBB d = m_dom[m_dfs_order[bb->index]];
211 return m_dfs_to_bb[d];
212}
f8032688 213
2b28c07a
JC
214/* Map dominance calculation type to array index used for various
215 dominance information arrays. This version is simple -- it will need
216 to be modified, obviously, if additional values are added to
217 cdi_direction. */
218
2321dd91
MM
219static inline unsigned int
220dom_convert_dir_to_idx (cdi_direction dir)
2b28c07a 221{
2ba31c05 222 gcc_checking_assert (dir == CDI_DOMINATORS || dir == CDI_POST_DOMINATORS);
2b28c07a
JC
223 return dir - 1;
224}
225
2321dd91 226/* Free all allocated memory in dom_info. */
f8032688 227
2321dd91 228dom_info::~dom_info ()
f8032688 229{
2321dd91
MM
230 delete[] m_dfs_parent;
231 delete[] m_path_min;
232 delete[] m_key;
233 delete[] m_dom;
234 delete[] m_bucket;
235 delete[] m_next_bucket;
236 delete[] m_set_chain;
237 delete[] m_set_size;
238 delete[] m_set_child;
239 delete[] m_dfs_order;
240 delete[] m_dfs_to_bb;
241 BITMAP_FREE (m_fake_exit_edge);
f8032688
MM
242}
243
2321dd91
MM
244/* The nonrecursive variant of creating a DFS tree. BB is the starting basic
245 block for this tree and m_reverse is true, if predecessors should be visited
246 instead of successors of a node. After this is done all nodes reachable
247 from BB were visited, have assigned their dfs number and are linked together
248 to form a tree. */
f8032688 249
2321dd91
MM
250void
251dom_info::calc_dfs_tree_nonrec (basic_block bb)
f8032688 252{
2321dd91
MM
253 edge_iterator *stack = new edge_iterator[m_n_basic_blocks + 1];
254 int sp = 0;
f8032688 255
2321dd91
MM
256 /* Initialize the first edge. */
257 edge_iterator ei = m_reverse ? ei_start (bb->preds)
258 : ei_start (bb->succs);
f8032688
MM
259
260 /* When the stack is empty we break out of this loop. */
261 while (1)
262 {
263 basic_block bn;
2321dd91 264 edge_iterator einext;
f8032688
MM
265
266 /* This loop traverses edges e in depth first manner, and fills the
267 stack. */
628f6a4e 268 while (!ei_end_p (ei))
f8032688 269 {
2321dd91 270 edge e = ei_edge (ei);
f8032688
MM
271
272 /* Deduce from E the current and the next block (BB and BN), and the
273 next edge. */
2321dd91 274 if (m_reverse)
f8032688
MM
275 {
276 bn = e->src;
277
278 /* If the next node BN is either already visited or a border
279 block the current edge is useless, and simply overwritten
280 with the next edge out of the current node. */
2321dd91 281 if (bn == m_end_block || m_dfs_order[bn->index])
f8032688 282 {
628f6a4e 283 ei_next (&ei);
f8032688
MM
284 continue;
285 }
286 bb = e->dest;
628f6a4e 287 einext = ei_start (bn->preds);
f8032688
MM
288 }
289 else
290 {
291 bn = e->dest;
2321dd91 292 if (bn == m_end_block || m_dfs_order[bn->index])
f8032688 293 {
628f6a4e 294 ei_next (&ei);
f8032688
MM
295 continue;
296 }
297 bb = e->src;
628f6a4e 298 einext = ei_start (bn->succs);
f8032688
MM
299 }
300
2321dd91 301 gcc_assert (bn != m_start_block);
f8032688
MM
302
303 /* Fill the DFS tree info calculatable _before_ recursing. */
2321dd91
MM
304 TBB my_i;
305 if (bb != m_start_block)
306 my_i = m_dfs_order[bb->index];
f8032688 307 else
2321dd91
MM
308 my_i = *m_dfs_last;
309 TBB child_i = m_dfs_order[bn->index] = m_dfsnum++;
310 m_dfs_to_bb[child_i] = bn;
311 m_dfs_parent[child_i] = my_i;
f8032688
MM
312
313 /* Save the current point in the CFG on the stack, and recurse. */
628f6a4e
BE
314 stack[sp++] = ei;
315 ei = einext;
f8032688
MM
316 }
317
318 if (!sp)
319 break;
628f6a4e 320 ei = stack[--sp];
f8032688
MM
321
322 /* OK. The edge-list was exhausted, meaning normally we would
323 end the recursion. After returning from the recursive call,
324 there were (may be) other statements which were run after a
325 child node was completely considered by DFS. Here is the
326 point to do it in the non-recursive variant.
327 E.g. The block just completed is in e->dest for forward DFS,
328 the block not yet completed (the parent of the one above)
329 in e->src. This could be used e.g. for computing the number of
330 descendants or the tree depth. */
628f6a4e 331 ei_next (&ei);
f8032688 332 }
2321dd91 333 delete[] stack;
f8032688
MM
334}
335
2321dd91
MM
336/* The main entry for calculating the DFS tree or forest. m_reverse is true,
337 if we are interested in the reverse flow graph. In that case the result is
338 not necessarily a tree but a forest, because there may be nodes from which
339 the EXIT_BLOCK is unreachable. */
f8032688 340
2321dd91
MM
341void
342dom_info::calc_dfs_tree ()
f8032688 343{
2321dd91
MM
344 *m_dfs_last = m_dfsnum;
345 m_dfs_to_bb[m_dfsnum] = m_start_block;
346 m_dfsnum++;
f8032688 347
2321dd91 348 calc_dfs_tree_nonrec (m_start_block);
f8032688 349
2321dd91 350 if (m_reverse)
f8032688
MM
351 {
352 /* In the post-dom case we may have nodes without a path to EXIT_BLOCK.
353 They are reverse-unreachable. In the dom-case we disallow such
26e0e410
RH
354 nodes, but in post-dom we have to deal with them.
355
356 There are two situations in which this occurs. First, noreturn
357 functions. Second, infinite loops. In the first case we need to
358 pretend that there is an edge to the exit block. In the second
359 case, we wind up with a forest. We need to process all noreturn
360 blocks before we know if we've got any infinite loops. */
361
e0082a72 362 basic_block b;
26e0e410
RH
363 bool saw_unconnected = false;
364
2321dd91 365 FOR_BB_BETWEEN (b, m_start_block->prev_bb, m_end_block, prev_bb)
f8032688 366 {
628f6a4e 367 if (EDGE_COUNT (b->succs) > 0)
26e0e410 368 {
2321dd91 369 if (m_dfs_order[b->index] == 0)
26e0e410
RH
370 saw_unconnected = true;
371 continue;
372 }
2321dd91
MM
373 bitmap_set_bit (m_fake_exit_edge, b->index);
374 m_dfs_order[b->index] = m_dfsnum;
375 m_dfs_to_bb[m_dfsnum] = b;
376 m_dfs_parent[m_dfsnum] = *m_dfs_last;
377 m_dfsnum++;
378 calc_dfs_tree_nonrec (b);
f8032688 379 }
26e0e410
RH
380
381 if (saw_unconnected)
382 {
2321dd91 383 FOR_BB_BETWEEN (b, m_start_block->prev_bb, m_end_block, prev_bb)
26e0e410 384 {
2321dd91 385 if (m_dfs_order[b->index])
26e0e410 386 continue;
2321dd91
MM
387 basic_block b2 = dfs_find_deadend (b);
388 gcc_checking_assert (m_dfs_order[b2->index] == 0);
389 bitmap_set_bit (m_fake_exit_edge, b2->index);
390 m_dfs_order[b2->index] = m_dfsnum;
391 m_dfs_to_bb[m_dfsnum] = b2;
392 m_dfs_parent[m_dfsnum] = *m_dfs_last;
393 m_dfsnum++;
394 calc_dfs_tree_nonrec (b2);
395 gcc_checking_assert (m_dfs_order[b->index]);
26e0e410
RH
396 }
397 }
f8032688
MM
398 }
399
2321dd91 400 m_nodes = m_dfsnum - 1;
f8032688 401
24bd1a0b 402 /* This aborts e.g. when there is _no_ path from ENTRY to EXIT at all. */
2321dd91 403 gcc_assert (m_nodes == (unsigned int) m_n_basic_blocks - 1);
f8032688
MM
404}
405
406/* Compress the path from V to the root of its set and update path_min at the
407 same time. After compress(di, V) set_chain[V] is the root of the set V is
408 in and path_min[V] is the node with the smallest key[] value on the path
409 from V to that root. */
410
2321dd91
MM
411void
412dom_info::compress (TBB v)
f8032688
MM
413{
414 /* Btw. It's not worth to unrecurse compress() as the depth is usually not
415 greater than 5 even for huge graphs (I've not seen call depth > 4).
416 Also performance wise compress() ranges _far_ behind eval(). */
2321dd91
MM
417 TBB parent = m_set_chain[v];
418 if (m_set_chain[parent])
f8032688 419 {
2321dd91
MM
420 compress (parent);
421 if (m_key[m_path_min[parent]] < m_key[m_path_min[v]])
422 m_path_min[v] = m_path_min[parent];
423 m_set_chain[v] = m_set_chain[parent];
f8032688
MM
424 }
425}
426
427/* Compress the path from V to the set root of V if needed (when the root has
428 changed since the last call). Returns the node with the smallest key[]
429 value on the path from V to the root. */
430
2321dd91
MM
431inline TBB
432dom_info::eval (TBB v)
f8032688 433{
fa10beec 434 /* The representative of the set V is in, also called root (as the set
f8032688 435 representation is a tree). */
2321dd91 436 TBB rep = m_set_chain[v];
f8032688
MM
437
438 /* V itself is the root. */
439 if (!rep)
2321dd91 440 return m_path_min[v];
f8032688
MM
441
442 /* Compress only if necessary. */
2321dd91 443 if (m_set_chain[rep])
f8032688 444 {
2321dd91
MM
445 compress (v);
446 rep = m_set_chain[v];
f8032688
MM
447 }
448
2321dd91
MM
449 if (m_key[m_path_min[rep]] >= m_key[m_path_min[v]])
450 return m_path_min[v];
f8032688 451 else
2321dd91 452 return m_path_min[rep];
f8032688
MM
453}
454
455/* This essentially merges the two sets of V and W, giving a single set with
456 the new root V. The internal representation of these disjoint sets is a
457 balanced tree. Currently link(V,W) is only used with V being the parent
458 of W. */
459
2321dd91
MM
460void
461dom_info::link_roots (TBB v, TBB w)
f8032688
MM
462{
463 TBB s = w;
464
465 /* Rebalance the tree. */
2321dd91 466 while (m_key[m_path_min[w]] < m_key[m_path_min[m_set_child[s]]])
f8032688 467 {
2321dd91
MM
468 if (m_set_size[s] + m_set_size[m_set_child[m_set_child[s]]]
469 >= 2 * m_set_size[m_set_child[s]])
f8032688 470 {
2321dd91
MM
471 m_set_chain[m_set_child[s]] = s;
472 m_set_child[s] = m_set_child[m_set_child[s]];
f8032688
MM
473 }
474 else
475 {
2321dd91
MM
476 m_set_size[m_set_child[s]] = m_set_size[s];
477 s = m_set_chain[s] = m_set_child[s];
f8032688
MM
478 }
479 }
480
2321dd91
MM
481 m_path_min[s] = m_path_min[w];
482 m_set_size[v] += m_set_size[w];
483 if (m_set_size[v] < 2 * m_set_size[w])
484 std::swap (m_set_child[v], s);
f8032688
MM
485
486 /* Merge all subtrees. */
487 while (s)
488 {
2321dd91
MM
489 m_set_chain[s] = v;
490 s = m_set_child[s];
f8032688
MM
491 }
492}
493
2321dd91
MM
494/* This calculates the immediate dominators (or post-dominators). THIS is our
495 working structure and should hold the DFS forest.
496 On return the immediate dominator to node V is in m_dom[V]. */
f8032688 497
2321dd91
MM
498void
499dom_info::calc_idoms ()
f8032688 500{
f8032688 501 /* Go backwards in DFS order, to first look at the leafs. */
2321dd91 502 for (TBB v = m_nodes; v > 1; v--)
f8032688 503 {
2321dd91 504 basic_block bb = m_dfs_to_bb[v];
628f6a4e 505 edge e;
f8032688 506
2321dd91
MM
507 TBB par = m_dfs_parent[v];
508 TBB k = v;
628f6a4e 509
2321dd91
MM
510 edge_iterator ei = m_reverse ? ei_start (bb->succs)
511 : ei_start (bb->preds);
512 edge_iterator einext;
628f6a4e 513
2321dd91 514 if (m_reverse)
26e0e410 515 {
26e0e410 516 /* If this block has a fake edge to exit, process that first. */
2321dd91 517 if (bitmap_bit_p (m_fake_exit_edge, bb->index))
26e0e410 518 {
628f6a4e
BE
519 einext = ei;
520 einext.index = 0;
26e0e410
RH
521 goto do_fake_exit_edge;
522 }
523 }
f8032688
MM
524
525 /* Search all direct predecessors for the smallest node with a path
526 to them. That way we have the smallest node with also a path to
527 us only over nodes behind us. In effect we search for our
528 semidominator. */
628f6a4e 529 while (!ei_end_p (ei))
f8032688 530 {
f8032688 531 basic_block b;
2321dd91 532 TBB k1;
f8032688 533
628f6a4e 534 e = ei_edge (ei);
2321dd91 535 b = m_reverse ? e->dest : e->src;
628f6a4e
BE
536 einext = ei;
537 ei_next (&einext);
538
2321dd91 539 if (b == m_start_block)
26e0e410
RH
540 {
541 do_fake_exit_edge:
2321dd91 542 k1 = *m_dfs_last;
26e0e410 543 }
f8032688 544 else
2321dd91 545 k1 = m_dfs_order[b->index];
f8032688
MM
546
547 /* Call eval() only if really needed. If k1 is above V in DFS tree,
548 then we know, that eval(k1) == k1 and key[k1] == k1. */
549 if (k1 > v)
2321dd91 550 k1 = m_key[eval (k1)];
f8032688
MM
551 if (k1 < k)
552 k = k1;
628f6a4e
BE
553
554 ei = einext;
f8032688
MM
555 }
556
2321dd91
MM
557 m_key[v] = k;
558 link_roots (par, v);
559 m_next_bucket[v] = m_bucket[k];
560 m_bucket[k] = v;
f8032688
MM
561
562 /* Transform semidominators into dominators. */
2321dd91 563 for (TBB w = m_bucket[par]; w; w = m_next_bucket[w])
f8032688 564 {
2321dd91
MM
565 k = eval (w);
566 if (m_key[k] < m_key[w])
567 m_dom[w] = k;
f8032688 568 else
2321dd91 569 m_dom[w] = par;
f8032688
MM
570 }
571 /* We don't need to cleanup next_bucket[]. */
2321dd91 572 m_bucket[par] = 0;
f8032688
MM
573 }
574
a1f300c0 575 /* Explicitly define the dominators. */
2321dd91
MM
576 m_dom[1] = 0;
577 for (TBB v = 2; v <= m_nodes; v++)
578 if (m_dom[v] != m_key[v])
579 m_dom[v] = m_dom[m_dom[v]];
f8032688
MM
580}
581
d47cc544
SB
582/* Assign dfs numbers starting from NUM to NODE and its sons. */
583
584static void
585assign_dfs_numbers (struct et_node *node, int *num)
586{
587 struct et_node *son;
588
589 node->dfs_num_in = (*num)++;
590
591 if (node->son)
592 {
593 assign_dfs_numbers (node->son, num);
594 for (son = node->son->right; son != node->son; son = son->right)
6de9cd9a 595 assign_dfs_numbers (son, num);
d47cc544 596 }
f8032688 597
d47cc544
SB
598 node->dfs_num_out = (*num)++;
599}
f8032688 600
5d3cc252 601/* Compute the data necessary for fast resolving of dominator queries in a
d47cc544 602 static dominator tree. */
f8032688 603
d47cc544
SB
604static void
605compute_dom_fast_query (enum cdi_direction dir)
606{
607 int num = 0;
608 basic_block bb;
2b28c07a 609 unsigned int dir_index = dom_convert_dir_to_idx (dir);
d47cc544 610
2ba31c05 611 gcc_checking_assert (dom_info_available_p (dir));
d47cc544 612
2b28c07a 613 if (dom_computed[dir_index] == DOM_OK)
d47cc544
SB
614 return;
615
04a90bec 616 FOR_ALL_BB_FN (bb, cfun)
d47cc544 617 {
2b28c07a
JC
618 if (!bb->dom[dir_index]->father)
619 assign_dfs_numbers (bb->dom[dir_index], &num);
d47cc544
SB
620 }
621
2b28c07a 622 dom_computed[dir_index] = DOM_OK;
d47cc544
SB
623}
624
625/* The main entry point into this module. DIR is set depending on whether
626 we want to compute dominators or postdominators. */
627
628void
2321dd91 629calculate_dominance_info (cdi_direction dir)
f8032688 630{
2b28c07a 631 unsigned int dir_index = dom_convert_dir_to_idx (dir);
355be0dc 632
2b28c07a 633 if (dom_computed[dir_index] == DOM_OK)
f3c676e1 634 {
b2b29377 635 checking_verify_dominators (dir);
f3c676e1
TV
636 return;
637 }
355be0dc 638
74c96e0c 639 timevar_push (TV_DOMINANCE);
fce22de5 640 if (!dom_info_available_p (dir))
d47cc544 641 {
2b28c07a 642 gcc_assert (!n_bbs_in_dom_tree[dir_index]);
f8032688 643
2321dd91 644 basic_block b;
04a90bec 645 FOR_ALL_BB_FN (b, cfun)
d47cc544 646 {
2b28c07a 647 b->dom[dir_index] = et_new_tree (b);
d47cc544 648 }
0cae8d31 649 n_bbs_in_dom_tree[dir_index] = n_basic_blocks_for_fn (cfun);
f8032688 650
2321dd91
MM
651 dom_info di (cfun, dir);
652 di.calc_dfs_tree ();
653 di.calc_idoms ();
355be0dc 654
11cd3bed 655 FOR_EACH_BB_FN (b, cfun)
d47cc544 656 {
2321dd91
MM
657 if (basic_block d = di.get_idom (b))
658 et_set_father (b->dom[dir_index], d->dom[dir_index]);
d47cc544 659 }
e0082a72 660
2b28c07a 661 dom_computed[dir_index] = DOM_NO_FAST_QUERY;
355be0dc 662 }
4081bdd2 663 else
b2b29377 664 checking_verify_dominators (dir);
355be0dc 665
d47cc544 666 compute_dom_fast_query (dir);
74c96e0c
ZD
667
668 timevar_pop (TV_DOMINANCE);
355be0dc
JH
669}
670
d47cc544 671/* Free dominance information for direction DIR. */
355be0dc 672void
e3f613cb 673free_dominance_info (function *fn, enum cdi_direction dir)
355be0dc
JH
674{
675 basic_block bb;
2b28c07a 676 unsigned int dir_index = dom_convert_dir_to_idx (dir);
355be0dc 677
e3f613cb 678 if (!dom_info_available_p (fn, dir))
d47cc544
SB
679 return;
680
e3f613cb 681 FOR_ALL_BB_FN (bb, fn)
d47cc544 682 {
2b28c07a
JC
683 et_free_tree_force (bb->dom[dir_index]);
684 bb->dom[dir_index] = NULL;
d47cc544 685 }
5a6ccafd 686 et_free_pools ();
d47cc544 687
e3f613cb
RB
688 fn->cfg->x_n_bbs_in_dom_tree[dir_index] = 0;
689
690 fn->cfg->x_dom_computed[dir_index] = DOM_NONE;
691}
6de9cd9a 692
e3f613cb
RB
693void
694free_dominance_info (enum cdi_direction dir)
695{
696 free_dominance_info (cfun, dir);
355be0dc
JH
697}
698
699/* Return the immediate dominator of basic block BB. */
700basic_block
d47cc544 701get_immediate_dominator (enum cdi_direction dir, basic_block bb)
355be0dc 702{
2b28c07a
JC
703 unsigned int dir_index = dom_convert_dir_to_idx (dir);
704 struct et_node *node = bb->dom[dir_index];
d47cc544 705
2ba31c05 706 gcc_checking_assert (dom_computed[dir_index]);
d47cc544
SB
707
708 if (!node->father)
709 return NULL;
710
f883e0a7 711 return (basic_block) node->father->data;
355be0dc
JH
712}
713
714/* Set the immediate dominator of the block possibly removing
715 existing edge. NULL can be used to remove any edge. */
7031a8b9 716void
d47cc544
SB
717set_immediate_dominator (enum cdi_direction dir, basic_block bb,
718 basic_block dominated_by)
355be0dc 719{
2b28c07a
JC
720 unsigned int dir_index = dom_convert_dir_to_idx (dir);
721 struct et_node *node = bb->dom[dir_index];
b8698a0f 722
2ba31c05 723 gcc_checking_assert (dom_computed[dir_index]);
355be0dc 724
d47cc544 725 if (node->father)
355be0dc 726 {
d47cc544 727 if (node->father->data == dominated_by)
6de9cd9a 728 return;
d47cc544 729 et_split (node);
355be0dc 730 }
d47cc544
SB
731
732 if (dominated_by)
2b28c07a 733 et_set_father (node, dominated_by->dom[dir_index]);
d47cc544 734
2b28c07a
JC
735 if (dom_computed[dir_index] == DOM_OK)
736 dom_computed[dir_index] = DOM_NO_FAST_QUERY;
355be0dc
JH
737}
738
66f97d31
ZD
739/* Returns the list of basic blocks immediately dominated by BB, in the
740 direction DIR. */
9771b263 741vec<basic_block>
66f97d31 742get_dominated_by (enum cdi_direction dir, basic_block bb)
355be0dc 743{
66f97d31 744 unsigned int dir_index = dom_convert_dir_to_idx (dir);
2b28c07a 745 struct et_node *node = bb->dom[dir_index], *son = node->son, *ason;
6e1aa848 746 vec<basic_block> bbs = vNULL;
66f97d31 747
2ba31c05 748 gcc_checking_assert (dom_computed[dir_index]);
d47cc544
SB
749
750 if (!son)
6e1aa848 751 return vNULL;
d47cc544 752
9771b263 753 bbs.safe_push ((basic_block) son->data);
2d888286 754 for (ason = son->right; ason != son; ason = ason->right)
9771b263 755 bbs.safe_push ((basic_block) ason->data);
355be0dc 756
66f97d31 757 return bbs;
355be0dc
JH
758}
759
66f97d31
ZD
760/* Returns the list of basic blocks that are immediately dominated (in
761 direction DIR) by some block between N_REGION ones stored in REGION,
762 except for blocks in the REGION itself. */
b8698a0f 763
9771b263 764vec<basic_block>
42759f1e 765get_dominated_by_region (enum cdi_direction dir, basic_block *region,
66f97d31 766 unsigned n_region)
42759f1e 767{
66f97d31 768 unsigned i;
42759f1e 769 basic_block dom;
6e1aa848 770 vec<basic_block> doms = vNULL;
42759f1e
ZD
771
772 for (i = 0; i < n_region; i++)
6580ee77 773 region[i]->flags |= BB_DUPLICATED;
42759f1e
ZD
774 for (i = 0; i < n_region; i++)
775 for (dom = first_dom_son (dir, region[i]);
776 dom;
777 dom = next_dom_son (dir, dom))
6580ee77 778 if (!(dom->flags & BB_DUPLICATED))
9771b263 779 doms.safe_push (dom);
42759f1e 780 for (i = 0; i < n_region; i++)
6580ee77 781 region[i]->flags &= ~BB_DUPLICATED;
42759f1e 782
66f97d31 783 return doms;
42759f1e
ZD
784}
785
438c239d 786/* Returns the list of basic blocks including BB dominated by BB, in the
cad9aa15
MK
787 direction DIR up to DEPTH in the dominator tree. The DEPTH of zero will
788 produce a vector containing all dominated blocks. The vector will be sorted
789 in preorder. */
438c239d 790
9771b263 791vec<basic_block>
cad9aa15 792get_dominated_to_depth (enum cdi_direction dir, basic_block bb, int depth)
438c239d 793{
6e1aa848 794 vec<basic_block> bbs = vNULL;
438c239d 795 unsigned i;
cad9aa15 796 unsigned next_level_start;
438c239d
RG
797
798 i = 0;
9771b263
DN
799 bbs.safe_push (bb);
800 next_level_start = 1; /* = bbs.length (); */
438c239d
RG
801
802 do
803 {
804 basic_block son;
805
9771b263 806 bb = bbs[i++];
438c239d
RG
807 for (son = first_dom_son (dir, bb);
808 son;
809 son = next_dom_son (dir, son))
9771b263 810 bbs.safe_push (son);
cad9aa15
MK
811
812 if (i == next_level_start && --depth)
9771b263 813 next_level_start = bbs.length ();
438c239d 814 }
cad9aa15 815 while (i < next_level_start);
438c239d
RG
816
817 return bbs;
818}
819
cad9aa15
MK
820/* Returns the list of basic blocks including BB dominated by BB, in the
821 direction DIR. The vector will be sorted in preorder. */
822
9771b263 823vec<basic_block>
cad9aa15
MK
824get_all_dominated_blocks (enum cdi_direction dir, basic_block bb)
825{
826 return get_dominated_to_depth (dir, bb, 0);
827}
828
355be0dc
JH
829/* Redirect all edges pointing to BB to TO. */
830void
d47cc544
SB
831redirect_immediate_dominators (enum cdi_direction dir, basic_block bb,
832 basic_block to)
355be0dc 833{
2b28c07a
JC
834 unsigned int dir_index = dom_convert_dir_to_idx (dir);
835 struct et_node *bb_node, *to_node, *son;
b8698a0f 836
2b28c07a
JC
837 bb_node = bb->dom[dir_index];
838 to_node = to->dom[dir_index];
d47cc544 839
2ba31c05 840 gcc_checking_assert (dom_computed[dir_index]);
355be0dc 841
d47cc544
SB
842 if (!bb_node->son)
843 return;
844
845 while (bb_node->son)
355be0dc 846 {
d47cc544
SB
847 son = bb_node->son;
848
849 et_split (son);
850 et_set_father (son, to_node);
355be0dc 851 }
d47cc544 852
2b28c07a
JC
853 if (dom_computed[dir_index] == DOM_OK)
854 dom_computed[dir_index] = DOM_NO_FAST_QUERY;
355be0dc
JH
855}
856
857/* Find first basic block in the tree dominating both BB1 and BB2. */
858basic_block
d47cc544 859nearest_common_dominator (enum cdi_direction dir, basic_block bb1, basic_block bb2)
355be0dc 860{
2b28c07a
JC
861 unsigned int dir_index = dom_convert_dir_to_idx (dir);
862
2ba31c05 863 gcc_checking_assert (dom_computed[dir_index]);
d47cc544 864
355be0dc
JH
865 if (!bb1)
866 return bb2;
867 if (!bb2)
868 return bb1;
d47cc544 869
f883e0a7 870 return (basic_block) et_nca (bb1->dom[dir_index], bb2->dom[dir_index])->data;
355be0dc
JH
871}
872
0bca51f0
DN
873
874/* Find the nearest common dominator for the basic blocks in BLOCKS,
875 using dominance direction DIR. */
876
877basic_block
878nearest_common_dominator_for_set (enum cdi_direction dir, bitmap blocks)
879{
880 unsigned i, first;
881 bitmap_iterator bi;
882 basic_block dom;
b8698a0f 883
0bca51f0 884 first = bitmap_first_set_bit (blocks);
06e28de2 885 dom = BASIC_BLOCK_FOR_FN (cfun, first);
0bca51f0 886 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
06e28de2
DM
887 if (dom != BASIC_BLOCK_FOR_FN (cfun, i))
888 dom = nearest_common_dominator (dir, dom, BASIC_BLOCK_FOR_FN (cfun, i));
0bca51f0
DN
889
890 return dom;
891}
892
b629276a
DB
893/* Given a dominator tree, we can determine whether one thing
894 dominates another in constant time by using two DFS numbers:
895
896 1. The number for when we visit a node on the way down the tree
897 2. The number for when we visit a node on the way back up the tree
898
899 You can view these as bounds for the range of dfs numbers the
900 nodes in the subtree of the dominator tree rooted at that node
901 will contain.
b8698a0f 902
b629276a
DB
903 The dominator tree is always a simple acyclic tree, so there are
904 only three possible relations two nodes in the dominator tree have
905 to each other:
b8698a0f 906
b629276a
DB
907 1. Node A is above Node B (and thus, Node A dominates node B)
908
909 A
910 |
911 C
912 / \
913 B D
914
915
916 In the above case, DFS_Number_In of A will be <= DFS_Number_In of
917 B, and DFS_Number_Out of A will be >= DFS_Number_Out of B. This is
918 because we must hit A in the dominator tree *before* B on the walk
919 down, and we will hit A *after* B on the walk back up
b8698a0f 920
d8701f02 921 2. Node A is below node B (and thus, node B dominates node A)
b8698a0f
L
922
923
b629276a
DB
924 B
925 |
926 A
927 / \
928 C D
929
930 In the above case, DFS_Number_In of A will be >= DFS_Number_In of
931 B, and DFS_Number_Out of A will be <= DFS_Number_Out of B.
b8698a0f 932
b629276a
DB
933 This is because we must hit A in the dominator tree *after* B on
934 the walk down, and we will hit A *before* B on the walk back up
b8698a0f 935
b629276a
DB
936 3. Node A and B are siblings (and thus, neither dominates the other)
937
938 C
939 |
940 D
941 / \
942 A B
943
944 In the above case, DFS_Number_In of A will *always* be <=
945 DFS_Number_In of B, and DFS_Number_Out of A will *always* be <=
946 DFS_Number_Out of B. This is because we will always finish the dfs
947 walk of one of the subtrees before the other, and thus, the dfs
948 numbers for one subtree can't intersect with the range of dfs
949 numbers for the other subtree. If you swap A and B's position in
950 the dominator tree, the comparison changes direction, but the point
951 is that both comparisons will always go the same way if there is no
952 dominance relationship.
953
954 Thus, it is sufficient to write
955
956 A_Dominates_B (node A, node B)
957 {
b8698a0f 958 return DFS_Number_In(A) <= DFS_Number_In(B)
b629276a
DB
959 && DFS_Number_Out (A) >= DFS_Number_Out(B);
960 }
961
962 A_Dominated_by_B (node A, node B)
963 {
048f1a9c 964 return DFS_Number_In(A) >= DFS_Number_In(B)
b629276a
DB
965 && DFS_Number_Out (A) <= DFS_Number_Out(B);
966 } */
0bca51f0 967
355be0dc
JH
968/* Return TRUE in case BB1 is dominated by BB2. */
969bool
ed7a4b4b 970dominated_by_p (enum cdi_direction dir, const_basic_block bb1, const_basic_block bb2)
b8698a0f 971{
2b28c07a
JC
972 unsigned int dir_index = dom_convert_dir_to_idx (dir);
973 struct et_node *n1 = bb1->dom[dir_index], *n2 = bb2->dom[dir_index];
b8698a0f 974
2ba31c05 975 gcc_checking_assert (dom_computed[dir_index]);
d47cc544 976
2b28c07a 977 if (dom_computed[dir_index] == DOM_OK)
d47cc544 978 return (n1->dfs_num_in >= n2->dfs_num_in
6de9cd9a 979 && n1->dfs_num_out <= n2->dfs_num_out);
d47cc544
SB
980
981 return et_below (n1, n2);
355be0dc
JH
982}
983
f074ff6c
ZD
984/* Returns the entry dfs number for basic block BB, in the direction DIR. */
985
986unsigned
987bb_dom_dfs_in (enum cdi_direction dir, basic_block bb)
988{
2b28c07a
JC
989 unsigned int dir_index = dom_convert_dir_to_idx (dir);
990 struct et_node *n = bb->dom[dir_index];
f074ff6c 991
2ba31c05 992 gcc_checking_assert (dom_computed[dir_index] == DOM_OK);
f074ff6c
ZD
993 return n->dfs_num_in;
994}
995
996/* Returns the exit dfs number for basic block BB, in the direction DIR. */
997
998unsigned
999bb_dom_dfs_out (enum cdi_direction dir, basic_block bb)
1000{
2b28c07a
JC
1001 unsigned int dir_index = dom_convert_dir_to_idx (dir);
1002 struct et_node *n = bb->dom[dir_index];
f074ff6c 1003
2ba31c05 1004 gcc_checking_assert (dom_computed[dir_index] == DOM_OK);
f074ff6c
ZD
1005 return n->dfs_num_out;
1006}
1007
355be0dc 1008/* Verify invariants of dominator structure. */
24e47c76 1009DEBUG_FUNCTION void
2321dd91 1010verify_dominators (cdi_direction dir)
355be0dc 1011{
fce22de5 1012 gcc_assert (dom_info_available_p (dir));
d47cc544 1013
2321dd91
MM
1014 dom_info di (cfun, dir);
1015 di.calc_dfs_tree ();
1016 di.calc_idoms ();
1fc3998d 1017
2321dd91
MM
1018 bool err = false;
1019 basic_block bb;
11cd3bed 1020 FOR_EACH_BB_FN (bb, cfun)
355be0dc 1021 {
2321dd91 1022 basic_block imm_bb = get_immediate_dominator (dir, bb);
1fc3998d 1023 if (!imm_bb)
f8032688 1024 {
66f97d31 1025 error ("dominator of %d status unknown", bb->index);
2321dd91 1026 err = true;
355be0dc 1027 }
66f97d31 1028
2321dd91 1029 basic_block imm_bb_correct = di.get_idom (bb);
1fc3998d 1030 if (imm_bb != imm_bb_correct)
e7bd94cc 1031 {
66f97d31 1032 error ("dominator of %d should be %d, not %d",
1fc3998d 1033 bb->index, imm_bb_correct->index, imm_bb->index);
2321dd91 1034 err = true;
e7bd94cc
ZD
1035 }
1036 }
1037
ced3f397 1038 gcc_assert (!err);
355be0dc
JH
1039}
1040
738ed977
ZD
1041/* Determine immediate dominator (or postdominator, according to DIR) of BB,
1042 assuming that dominators of other blocks are correct. We also use it to
1043 recompute the dominators in a restricted area, by iterating it until it
71cc389b 1044 reaches a fixed point. */
738ed977 1045
355be0dc 1046basic_block
66f97d31 1047recompute_dominator (enum cdi_direction dir, basic_block bb)
355be0dc 1048{
2b28c07a 1049 unsigned int dir_index = dom_convert_dir_to_idx (dir);
738ed977
ZD
1050 basic_block dom_bb = NULL;
1051 edge e;
628f6a4e 1052 edge_iterator ei;
355be0dc 1053
2ba31c05 1054 gcc_checking_assert (dom_computed[dir_index]);
d47cc544 1055
738ed977
ZD
1056 if (dir == CDI_DOMINATORS)
1057 {
628f6a4e 1058 FOR_EACH_EDGE (e, ei, bb->preds)
738ed977
ZD
1059 {
1060 if (!dominated_by_p (dir, e->src, bb))
1061 dom_bb = nearest_common_dominator (dir, dom_bb, e->src);
1062 }
1063 }
1064 else
1065 {
628f6a4e 1066 FOR_EACH_EDGE (e, ei, bb->succs)
738ed977
ZD
1067 {
1068 if (!dominated_by_p (dir, e->dest, bb))
1069 dom_bb = nearest_common_dominator (dir, dom_bb, e->dest);
1070 }
1071 }
f8032688 1072
738ed977 1073 return dom_bb;
355be0dc
JH
1074}
1075
66f97d31
ZD
1076/* Use simple heuristics (see iterate_fix_dominators) to determine dominators
1077 of BBS. We assume that all the immediate dominators except for those of the
1078 blocks in BBS are correct. If CONSERVATIVE is true, we also assume that the
1079 currently recorded immediate dominators of blocks in BBS really dominate the
1080 blocks. The basic blocks for that we determine the dominator are removed
1081 from BBS. */
1082
1083static void
9771b263 1084prune_bbs_to_update_dominators (vec<basic_block> bbs,
66f97d31
ZD
1085 bool conservative)
1086{
1087 unsigned i;
1088 bool single;
1089 basic_block bb, dom = NULL;
1090 edge_iterator ei;
1091 edge e;
1092
9771b263 1093 for (i = 0; bbs.iterate (i, &bb);)
66f97d31 1094 {
fefa31b5 1095 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
66f97d31
ZD
1096 goto succeed;
1097
1098 if (single_pred_p (bb))
1099 {
1100 set_immediate_dominator (CDI_DOMINATORS, bb, single_pred (bb));
1101 goto succeed;
1102 }
1103
1104 if (!conservative)
1105 goto fail;
1106
1107 single = true;
1108 dom = NULL;
1109 FOR_EACH_EDGE (e, ei, bb->preds)
1110 {
1111 if (dominated_by_p (CDI_DOMINATORS, e->src, bb))
1112 continue;
1113
1114 if (!dom)
1115 dom = e->src;
1116 else
1117 {
1118 single = false;
1119 dom = nearest_common_dominator (CDI_DOMINATORS, dom, e->src);
1120 }
1121 }
1122
1123 gcc_assert (dom != NULL);
1124 if (single
1125 || find_edge (dom, bb))
1126 {
1127 set_immediate_dominator (CDI_DOMINATORS, bb, dom);
1128 goto succeed;
1129 }
1130
1131fail:
1132 i++;
1133 continue;
1134
1135succeed:
9771b263 1136 bbs.unordered_remove (i);
66f97d31
ZD
1137 }
1138}
1139
1140/* Returns root of the dominance tree in the direction DIR that contains
1141 BB. */
1142
1143static basic_block
1144root_of_dom_tree (enum cdi_direction dir, basic_block bb)
1145{
f883e0a7 1146 return (basic_block) et_root (bb->dom[dom_convert_dir_to_idx (dir)])->data;
66f97d31
ZD
1147}
1148
1149/* See the comment in iterate_fix_dominators. Finds the immediate dominators
1150 for the sons of Y, found using the SON and BROTHER arrays representing
1151 the dominance tree of graph G. BBS maps the vertices of G to the basic
1152 blocks. */
1153
1154static void
9771b263 1155determine_dominators_for_sons (struct graph *g, vec<basic_block> bbs,
66f97d31
ZD
1156 int y, int *son, int *brother)
1157{
1158 bitmap gprime;
1159 int i, a, nc;
9771b263 1160 vec<int> *sccs;
66f97d31
ZD
1161 basic_block bb, dom, ybb;
1162 unsigned si;
1163 edge e;
1164 edge_iterator ei;
1165
1166 if (son[y] == -1)
1167 return;
9771b263 1168 if (y == (int) bbs.length ())
fefa31b5 1169 ybb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
66f97d31 1170 else
9771b263 1171 ybb = bbs[y];
66f97d31
ZD
1172
1173 if (brother[son[y]] == -1)
1174 {
1175 /* Handle the common case Y has just one son specially. */
9771b263 1176 bb = bbs[son[y]];
66f97d31
ZD
1177 set_immediate_dominator (CDI_DOMINATORS, bb,
1178 recompute_dominator (CDI_DOMINATORS, bb));
1179 identify_vertices (g, y, son[y]);
1180 return;
1181 }
1182
1183 gprime = BITMAP_ALLOC (NULL);
1184 for (a = son[y]; a != -1; a = brother[a])
1185 bitmap_set_bit (gprime, a);
1186
1187 nc = graphds_scc (g, gprime);
1188 BITMAP_FREE (gprime);
1189
9771b263
DN
1190 /* ??? Needed to work around the pre-processor confusion with
1191 using a multi-argument template type as macro argument. */
1192 typedef vec<int> vec_int_heap;
1193 sccs = XCNEWVEC (vec_int_heap, nc);
66f97d31 1194 for (a = son[y]; a != -1; a = brother[a])
9771b263 1195 sccs[g->vertices[a].component].safe_push (a);
66f97d31
ZD
1196
1197 for (i = nc - 1; i >= 0; i--)
1198 {
1199 dom = NULL;
9771b263 1200 FOR_EACH_VEC_ELT (sccs[i], si, a)
66f97d31 1201 {
9771b263 1202 bb = bbs[a];
66f97d31
ZD
1203 FOR_EACH_EDGE (e, ei, bb->preds)
1204 {
1205 if (root_of_dom_tree (CDI_DOMINATORS, e->src) != ybb)
1206 continue;
1207
1208 dom = nearest_common_dominator (CDI_DOMINATORS, dom, e->src);
1209 }
1210 }
1211
1212 gcc_assert (dom != NULL);
9771b263 1213 FOR_EACH_VEC_ELT (sccs[i], si, a)
66f97d31 1214 {
9771b263 1215 bb = bbs[a];
66f97d31
ZD
1216 set_immediate_dominator (CDI_DOMINATORS, bb, dom);
1217 }
1218 }
1219
1220 for (i = 0; i < nc; i++)
9771b263 1221 sccs[i].release ();
66f97d31
ZD
1222 free (sccs);
1223
1224 for (a = son[y]; a != -1; a = brother[a])
1225 identify_vertices (g, y, a);
1226}
1227
1228/* Recompute dominance information for basic blocks in the set BBS. The
1229 function assumes that the immediate dominators of all the other blocks
1230 in CFG are correct, and that there are no unreachable blocks.
1231
1232 If CONSERVATIVE is true, we additionally assume that all the ancestors of
1233 a block of BBS in the current dominance tree dominate it. */
1234
355be0dc 1235void
9771b263 1236iterate_fix_dominators (enum cdi_direction dir, vec<basic_block> bbs,
66f97d31 1237 bool conservative)
355be0dc 1238{
66f97d31
ZD
1239 unsigned i;
1240 basic_block bb, dom;
1241 struct graph *g;
1242 int n, y;
1243 size_t dom_i;
1244 edge e;
1245 edge_iterator ei;
66f97d31 1246 int *parent, *son, *brother;
2b28c07a 1247 unsigned int dir_index = dom_convert_dir_to_idx (dir);
355be0dc 1248
66f97d31
ZD
1249 /* We only support updating dominators. There are some problems with
1250 updating postdominators (need to add fake edges from infinite loops
1251 and noreturn functions), and since we do not currently use
1252 iterate_fix_dominators for postdominators, any attempt to handle these
1253 problems would be unused, untested, and almost surely buggy. We keep
1254 the DIR argument for consistency with the rest of the dominator analysis
1255 interface. */
2ba31c05 1256 gcc_checking_assert (dir == CDI_DOMINATORS && dom_computed[dir_index]);
d47cc544 1257
66f97d31
ZD
1258 /* The algorithm we use takes inspiration from the following papers, although
1259 the details are quite different from any of them:
1260
1261 [1] G. Ramalingam, T. Reps, An Incremental Algorithm for Maintaining the
1262 Dominator Tree of a Reducible Flowgraph
1263 [2] V. C. Sreedhar, G. R. Gao, Y.-F. Lee: Incremental computation of
1264 dominator trees
1265 [3] K. D. Cooper, T. J. Harvey and K. Kennedy: A Simple, Fast Dominance
1266 Algorithm
1267
1268 First, we use the following heuristics to decrease the size of the BBS
1269 set:
1270 a) if BB has a single predecessor, then its immediate dominator is this
1271 predecessor
1272 additionally, if CONSERVATIVE is true:
1273 b) if all the predecessors of BB except for one (X) are dominated by BB,
1274 then X is the immediate dominator of BB
1275 c) if the nearest common ancestor of the predecessors of BB is X and
1276 X -> BB is an edge in CFG, then X is the immediate dominator of BB
1277
1278 Then, we need to establish the dominance relation among the basic blocks
1279 in BBS. We split the dominance tree by removing the immediate dominator
0d52bcc1 1280 edges from BBS, creating a forest F. We form a graph G whose vertices
66f97d31 1281 are BBS and ENTRY and X -> Y is an edge of G if there exists an edge
0d52bcc1 1282 X' -> Y in CFG such that X' belongs to the tree of the dominance forest
66f97d31
ZD
1283 whose root is X. We then determine dominance tree of G. Note that
1284 for X, Y in BBS, X dominates Y in CFG if and only if X dominates Y in G.
1285 In this step, we can use arbitrary algorithm to determine dominators.
1286 We decided to prefer the algorithm [3] to the algorithm of
1287 Lengauer and Tarjan, since the set BBS is usually small (rarely exceeding
1288 10 during gcc bootstrap), and [3] should perform better in this case.
1289
1290 Finally, we need to determine the immediate dominators for the basic
1291 blocks of BBS. If the immediate dominator of X in G is Y, then
1292 the immediate dominator of X in CFG belongs to the tree of F rooted in
1293 Y. We process the dominator tree T of G recursively, starting from leaves.
1294 Suppose that X_1, X_2, ..., X_k are the sons of Y in T, and that the
1295 subtrees of the dominance tree of CFG rooted in X_i are already correct.
1296 Let G' be the subgraph of G induced by {X_1, X_2, ..., X_k}. We make
1297 the following observations:
1298 (i) the immediate dominator of all blocks in a strongly connected
1299 component of G' is the same
1300 (ii) if X has no predecessors in G', then the immediate dominator of X
1301 is the nearest common ancestor of the predecessors of X in the
1302 subtree of F rooted in Y
1303 Therefore, it suffices to find the topological ordering of G', and
1304 process the nodes X_i in this order using the rules (i) and (ii).
1305 Then, we contract all the nodes X_i with Y in G, so that the further
1306 steps work correctly. */
1307
1308 if (!conservative)
1309 {
1310 /* Split the tree now. If the idoms of blocks in BBS are not
1311 conservatively correct, setting the dominators using the
1312 heuristics in prune_bbs_to_update_dominators could
1313 create cycles in the dominance "tree", and cause ICE. */
9771b263 1314 FOR_EACH_VEC_ELT (bbs, i, bb)
66f97d31
ZD
1315 set_immediate_dominator (CDI_DOMINATORS, bb, NULL);
1316 }
1317
1318 prune_bbs_to_update_dominators (bbs, conservative);
9771b263 1319 n = bbs.length ();
66f97d31
ZD
1320
1321 if (n == 0)
1322 return;
e7bd94cc 1323
66f97d31 1324 if (n == 1)
355be0dc 1325 {
9771b263 1326 bb = bbs[0];
66f97d31
ZD
1327 set_immediate_dominator (CDI_DOMINATORS, bb,
1328 recompute_dominator (CDI_DOMINATORS, bb));
1329 return;
1330 }
1331
1332 /* Construct the graph G. */
1eb68d2d 1333 hash_map<basic_block, int> map (251);
9771b263 1334 FOR_EACH_VEC_ELT (bbs, i, bb)
66f97d31
ZD
1335 {
1336 /* If the dominance tree is conservatively correct, split it now. */
1337 if (conservative)
1338 set_immediate_dominator (CDI_DOMINATORS, bb, NULL);
1eb68d2d 1339 map.put (bb, i);
66f97d31 1340 }
1eb68d2d 1341 map.put (ENTRY_BLOCK_PTR_FOR_FN (cfun), n);
66f97d31
ZD
1342
1343 g = new_graph (n + 1);
1344 for (y = 0; y < g->n_vertices; y++)
1345 g->vertices[y].data = BITMAP_ALLOC (NULL);
9771b263 1346 FOR_EACH_VEC_ELT (bbs, i, bb)
66f97d31
ZD
1347 {
1348 FOR_EACH_EDGE (e, ei, bb->preds)
355be0dc 1349 {
66f97d31
ZD
1350 dom = root_of_dom_tree (CDI_DOMINATORS, e->src);
1351 if (dom == bb)
1352 continue;
1353
1eb68d2d 1354 dom_i = *map.get (dom);
66f97d31
ZD
1355
1356 /* Do not include parallel edges to G. */
fcaa4ca4 1357 if (!bitmap_set_bit ((bitmap) g->vertices[dom_i].data, i))
66f97d31
ZD
1358 continue;
1359
66f97d31 1360 add_edge (g, dom_i, i);
f8032688
MM
1361 }
1362 }
66f97d31
ZD
1363 for (y = 0; y < g->n_vertices; y++)
1364 BITMAP_FREE (g->vertices[y].data);
66f97d31
ZD
1365
1366 /* Find the dominator tree of G. */
1367 son = XNEWVEC (int, n + 1);
1368 brother = XNEWVEC (int, n + 1);
1369 parent = XNEWVEC (int, n + 1);
1370 graphds_domtree (g, n, parent, son, brother);
1371
1372 /* Finally, traverse the tree and find the immediate dominators. */
1373 for (y = n; son[y] != -1; y = son[y])
1374 continue;
1375 while (y != -1)
1376 {
1377 determine_dominators_for_sons (g, bbs, y, son, brother);
1378
1379 if (brother[y] != -1)
1380 {
1381 y = brother[y];
1382 while (son[y] != -1)
1383 y = son[y];
1384 }
1385 else
1386 y = parent[y];
1387 }
1388
1389 free (son);
1390 free (brother);
1391 free (parent);
e7bd94cc 1392
66f97d31 1393 free_graph (g);
355be0dc 1394}
f8032688 1395
355be0dc 1396void
d47cc544 1397add_to_dominance_info (enum cdi_direction dir, basic_block bb)
355be0dc 1398{
2b28c07a
JC
1399 unsigned int dir_index = dom_convert_dir_to_idx (dir);
1400
2ba31c05 1401 gcc_checking_assert (dom_computed[dir_index] && !bb->dom[dir_index]);
d47cc544 1402
2b28c07a 1403 n_bbs_in_dom_tree[dir_index]++;
b8698a0f 1404
2b28c07a 1405 bb->dom[dir_index] = et_new_tree (bb);
d47cc544 1406
2b28c07a
JC
1407 if (dom_computed[dir_index] == DOM_OK)
1408 dom_computed[dir_index] = DOM_NO_FAST_QUERY;
355be0dc
JH
1409}
1410
1411void
d47cc544
SB
1412delete_from_dominance_info (enum cdi_direction dir, basic_block bb)
1413{
2b28c07a 1414 unsigned int dir_index = dom_convert_dir_to_idx (dir);
d47cc544 1415
2ba31c05 1416 gcc_checking_assert (dom_computed[dir_index]);
d47cc544 1417
2b28c07a
JC
1418 et_free_tree (bb->dom[dir_index]);
1419 bb->dom[dir_index] = NULL;
1420 n_bbs_in_dom_tree[dir_index]--;
1421
1422 if (dom_computed[dir_index] == DOM_OK)
1423 dom_computed[dir_index] = DOM_NO_FAST_QUERY;
d47cc544
SB
1424}
1425
1426/* Returns the first son of BB in the dominator or postdominator tree
1427 as determined by DIR. */
1428
1429basic_block
1430first_dom_son (enum cdi_direction dir, basic_block bb)
355be0dc 1431{
2b28c07a
JC
1432 unsigned int dir_index = dom_convert_dir_to_idx (dir);
1433 struct et_node *son = bb->dom[dir_index]->son;
d47cc544 1434
f883e0a7 1435 return (basic_block) (son ? son->data : NULL);
d47cc544
SB
1436}
1437
1438/* Returns the next dominance son after BB in the dominator or postdominator
1439 tree as determined by DIR, or NULL if it was the last one. */
1440
1441basic_block
1442next_dom_son (enum cdi_direction dir, basic_block bb)
1443{
2b28c07a
JC
1444 unsigned int dir_index = dom_convert_dir_to_idx (dir);
1445 struct et_node *next = bb->dom[dir_index]->right;
d47cc544 1446
f883e0a7 1447 return (basic_block) (next->father->son == next ? NULL : next->data);
355be0dc
JH
1448}
1449
2b28c07a
JC
1450/* Return dominance availability for dominance info DIR. */
1451
1452enum dom_state
e3f613cb 1453dom_info_state (function *fn, enum cdi_direction dir)
2b28c07a 1454{
e3f613cb
RB
1455 if (!fn->cfg)
1456 return DOM_NONE;
1457
2b28c07a 1458 unsigned int dir_index = dom_convert_dir_to_idx (dir);
e3f613cb
RB
1459 return fn->cfg->x_dom_computed[dir_index];
1460}
2b28c07a 1461
e3f613cb
RB
1462enum dom_state
1463dom_info_state (enum cdi_direction dir)
1464{
1465 return dom_info_state (cfun, dir);
2b28c07a
JC
1466}
1467
1468/* Set the dominance availability for dominance info DIR to NEW_STATE. */
1469
1470void
1471set_dom_info_availability (enum cdi_direction dir, enum dom_state new_state)
1472{
1473 unsigned int dir_index = dom_convert_dir_to_idx (dir);
1474
1475 dom_computed[dir_index] = new_state;
1476}
1477
fce22de5
ZD
1478/* Returns true if dominance information for direction DIR is available. */
1479
1480bool
e3f613cb 1481dom_info_available_p (function *fn, enum cdi_direction dir)
fce22de5 1482{
e3f613cb
RB
1483 return dom_info_state (fn, dir) != DOM_NONE;
1484}
2b28c07a 1485
e3f613cb
RB
1486bool
1487dom_info_available_p (enum cdi_direction dir)
1488{
1489 return dom_info_available_p (cfun, dir);
fce22de5
ZD
1490}
1491
24e47c76 1492DEBUG_FUNCTION void
d47cc544 1493debug_dominance_info (enum cdi_direction dir)
355be0dc
JH
1494{
1495 basic_block bb, bb2;
11cd3bed 1496 FOR_EACH_BB_FN (bb, cfun)
d47cc544 1497 if ((bb2 = get_immediate_dominator (dir, bb)))
355be0dc 1498 fprintf (stderr, "%i %i\n", bb->index, bb2->index);
f8032688 1499}
1fc3998d
ZD
1500
1501/* Prints to stderr representation of the dominance tree (for direction DIR)
cea618ac 1502 rooted in ROOT, indented by INDENT tabulators. If INDENT_FIRST is false,
1fc3998d
ZD
1503 the first line of the output is not indented. */
1504
1505static void
1506debug_dominance_tree_1 (enum cdi_direction dir, basic_block root,
1507 unsigned indent, bool indent_first)
1508{
1509 basic_block son;
1510 unsigned i;
1511 bool first = true;
1512
1513 if (indent_first)
1514 for (i = 0; i < indent; i++)
1515 fprintf (stderr, "\t");
1516 fprintf (stderr, "%d\t", root->index);
1517
1518 for (son = first_dom_son (dir, root);
1519 son;
1520 son = next_dom_son (dir, son))
1521 {
1522 debug_dominance_tree_1 (dir, son, indent + 1, !first);
1523 first = false;
1524 }
1525
1526 if (first)
1527 fprintf (stderr, "\n");
1528}
1529
1530/* Prints to stderr representation of the dominance tree (for direction DIR)
1531 rooted in ROOT. */
1532
24e47c76 1533DEBUG_FUNCTION void
1fc3998d
ZD
1534debug_dominance_tree (enum cdi_direction dir, basic_block root)
1535{
1536 debug_dominance_tree_1 (dir, root, 0, false);
1537}