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