OSDN Git Service

* config/xtensa/xtensa.c (xtensa_init_machine_status): Fix
[pf3gnuchains/gcc-fork.git] / gcc / dominance.c
1 /* Calculate (post)dominators in slightly super-linear time.
2    Copyright (C) 2000 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 2, 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 COPYING.  If not, write to the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 /* This file implements the well known algorithm from Lengauer and Tarjan
23    to compute the dominators in a control flow graph.  A basic block D is said
24    to dominate another block X, when all paths from the entry node of the CFG
25    to X go also over D.  The dominance relation is a transitive reflexive
26    relation and its minimal transitive reduction is a tree, called the
27    dominator tree.  So for each block X besides the entry block exists a
28    block I(X), called the immediate dominator of X, which is the parent of X
29    in the dominator tree.
30
31    The algorithm computes this dominator tree implicitly by computing for
32    each block its immediate dominator.  We use tree balancing and path
33    compression, so its the O(e*a(e,v)) variant, where a(e,v) is the very
34    slowly growing functional inverse of the Ackerman function.  */
35
36 #include "config.h"
37 #include "system.h"
38 #include "rtl.h"
39 #include "hard-reg-set.h"
40 #include "basic-block.h"
41
42
43 /* We name our nodes with integers, beginning with 1.  Zero is reserved for
44    'undefined' or 'end of list'.  The name of each node is given by the dfs
45    number of the corresponding basic block.  Please note, that we include the
46    artificial ENTRY_BLOCK (or EXIT_BLOCK in the post-dom case) in our lists to
47    support multiple entry points.  As it has no real basic block index we use
48    'last_basic_block' for that.  Its dfs number is of course 1.  */
49
50 /* Type of Basic Block aka. TBB */
51 typedef unsigned int TBB;
52
53 /* We work in a poor-mans object oriented fashion, and carry an instance of
54    this structure through all our 'methods'.  It holds various arrays
55    reflecting the (sub)structure of the flowgraph.  Most of them are of type
56    TBB and are also indexed by TBB.  */
57
58 struct dom_info
59 {
60   /* The parent of a node in the DFS tree.  */
61   TBB *dfs_parent;
62   /* For a node x key[x] is roughly the node nearest to the root from which
63      exists a way to x only over nodes behind x.  Such a node is also called
64      semidominator.  */
65   TBB *key;
66   /* The value in path_min[x] is the node y on the path from x to the root of
67      the tree x is in with the smallest key[y].  */
68   TBB *path_min;
69   /* bucket[x] points to the first node of the set of nodes having x as key.  */
70   TBB *bucket;
71   /* And next_bucket[x] points to the next node.  */
72   TBB *next_bucket;
73   /* After the algorithm is done, dom[x] contains the immediate dominator
74      of x.  */
75   TBB *dom;
76
77   /* The following few fields implement the structures needed for disjoint
78      sets.  */
79   /* set_chain[x] is the next node on the path from x to the representant
80      of the set containing x.  If set_chain[x]==0 then x is a root.  */
81   TBB *set_chain;
82   /* set_size[x] is the number of elements in the set named by x.  */
83   unsigned int *set_size;
84   /* set_child[x] is used for balancing the tree representing a set.  It can
85      be understood as the next sibling of x.  */
86   TBB *set_child;
87
88   /* If b is the number of a basic block (BB->index), dfs_order[b] is the
89      number of that node in DFS order counted from 1.  This is an index
90      into most of the other arrays in this structure.  */
91   TBB *dfs_order;
92   /* If x is the DFS-index of a node which corresponds with an basic block,
93      dfs_to_bb[x] is that basic block.  Note, that in our structure there are
94      more nodes that basic blocks, so only dfs_to_bb[dfs_order[bb->index]]==bb
95      is true for every basic block bb, but not the opposite.  */
96   basic_block *dfs_to_bb;
97
98   /* This is the next free DFS number when creating the DFS tree or forest.  */
99   unsigned int dfsnum;
100   /* The number of nodes in the DFS tree (==dfsnum-1).  */
101   unsigned int nodes;
102 };
103
104 static void init_dom_info               PARAMS ((struct dom_info *));
105 static void free_dom_info               PARAMS ((struct dom_info *));
106 static void calc_dfs_tree_nonrec        PARAMS ((struct dom_info *,
107                                                  basic_block,
108                                                  enum cdi_direction));
109 static void calc_dfs_tree               PARAMS ((struct dom_info *,
110                                                  enum cdi_direction));
111 static void compress                    PARAMS ((struct dom_info *, TBB));
112 static TBB eval                         PARAMS ((struct dom_info *, TBB));
113 static void link_roots                  PARAMS ((struct dom_info *, TBB, TBB));
114 static void calc_idoms                  PARAMS ((struct dom_info *,
115                                                  enum cdi_direction));
116 static void idoms_to_doms               PARAMS ((struct dom_info *,
117                                                  sbitmap *));
118
119 /* Helper macro for allocating and initializing an array,
120    for aesthetic reasons.  */
121 #define init_ar(var, type, num, content)                        \
122   do                                                            \
123     {                                                           \
124       unsigned int i = 1;    /* Catch content == i.  */         \
125       if (! (content))                                          \
126         (var) = (type *) xcalloc ((num), sizeof (type));        \
127       else                                                      \
128         {                                                       \
129           (var) = (type *) xmalloc ((num) * sizeof (type));     \
130           for (i = 0; i < num; i++)                             \
131             (var)[i] = (content);                               \
132         }                                                       \
133     }                                                           \
134   while (0)
135
136 /* Allocate all needed memory in a pessimistic fashion (so we round up).
137    This initialises the contents of DI, which already must be allocated.  */
138
139 static void
140 init_dom_info (di)
141      struct dom_info *di;
142 {
143   /* We need memory for n_basic_blocks nodes and the ENTRY_BLOCK or
144      EXIT_BLOCK.  */
145   unsigned int num = n_basic_blocks + 1 + 1;
146   init_ar (di->dfs_parent, TBB, num, 0);
147   init_ar (di->path_min, TBB, num, i);
148   init_ar (di->key, TBB, num, i);
149   init_ar (di->dom, TBB, num, 0);
150
151   init_ar (di->bucket, TBB, num, 0);
152   init_ar (di->next_bucket, TBB, num, 0);
153
154   init_ar (di->set_chain, TBB, num, 0);
155   init_ar (di->set_size, unsigned int, num, 1);
156   init_ar (di->set_child, TBB, num, 0);
157
158   init_ar (di->dfs_order, TBB, (unsigned int) last_basic_block + 1, 0);
159   init_ar (di->dfs_to_bb, basic_block, num, 0);
160
161   di->dfsnum = 1;
162   di->nodes = 0;
163 }
164
165 #undef init_ar
166
167 /* Free all allocated memory in DI, but not DI itself.  */
168
169 static void
170 free_dom_info (di)
171      struct dom_info *di;
172 {
173   free (di->dfs_parent);
174   free (di->path_min);
175   free (di->key);
176   free (di->dom);
177   free (di->bucket);
178   free (di->next_bucket);
179   free (di->set_chain);
180   free (di->set_size);
181   free (di->set_child);
182   free (di->dfs_order);
183   free (di->dfs_to_bb);
184 }
185
186 /* The nonrecursive variant of creating a DFS tree.  DI is our working
187    structure, BB the starting basic block for this tree and REVERSE
188    is true, if predecessors should be visited instead of successors of a
189    node.  After this is done all nodes reachable from BB were visited, have
190    assigned their dfs number and are linked together to form a tree.  */
191
192 static void
193 calc_dfs_tree_nonrec (di, bb, reverse)
194      struct dom_info *di;
195      basic_block bb;
196      enum cdi_direction reverse;
197 {
198   /* We never call this with bb==EXIT_BLOCK_PTR (ENTRY_BLOCK_PTR if REVERSE).  */
199   /* We call this _only_ if bb is not already visited.  */
200   edge e;
201   TBB child_i, my_i = 0;
202   edge *stack;
203   int sp;
204   /* Start block (ENTRY_BLOCK_PTR for forward problem, EXIT_BLOCK for backward
205      problem).  */
206   basic_block en_block;
207   /* Ending block.  */
208   basic_block ex_block;
209
210   stack = (edge *) xmalloc ((n_basic_blocks + 3) * sizeof (edge));
211   sp = 0;
212
213   /* Initialize our border blocks, and the first edge.  */
214   if (reverse)
215     {
216       e = bb->pred;
217       en_block = EXIT_BLOCK_PTR;
218       ex_block = ENTRY_BLOCK_PTR;
219     }
220   else
221     {
222       e = bb->succ;
223       en_block = ENTRY_BLOCK_PTR;
224       ex_block = EXIT_BLOCK_PTR;
225     }
226
227   /* When the stack is empty we break out of this loop.  */
228   while (1)
229     {
230       basic_block bn;
231
232       /* This loop traverses edges e in depth first manner, and fills the
233          stack.  */
234       while (e)
235         {
236           edge e_next;
237
238           /* Deduce from E the current and the next block (BB and BN), and the
239              next edge.  */
240           if (reverse)
241             {
242               bn = e->src;
243
244               /* If the next node BN is either already visited or a border
245                  block the current edge is useless, and simply overwritten
246                  with the next edge out of the current node.  */
247               if (bn == ex_block || di->dfs_order[bn->index])
248                 {
249                   e = e->pred_next;
250                   continue;
251                 }
252               bb = e->dest;
253               e_next = bn->pred;
254             }
255           else
256             {
257               bn = e->dest;
258               if (bn == ex_block || di->dfs_order[bn->index])
259                 {
260                   e = e->succ_next;
261                   continue;
262                 }
263               bb = e->src;
264               e_next = bn->succ;
265             }
266
267           if (bn == en_block)
268             abort ();
269
270           /* Fill the DFS tree info calculatable _before_ recursing.  */
271           if (bb != en_block)
272             my_i = di->dfs_order[bb->index];
273           else
274             my_i = di->dfs_order[last_basic_block];
275           child_i = di->dfs_order[bn->index] = di->dfsnum++;
276           di->dfs_to_bb[child_i] = bn;
277           di->dfs_parent[child_i] = my_i;
278
279           /* Save the current point in the CFG on the stack, and recurse.  */
280           stack[sp++] = e;
281           e = e_next;
282         }
283
284       if (!sp)
285         break;
286       e = stack[--sp];
287
288       /* OK.  The edge-list was exhausted, meaning normally we would
289          end the recursion.  After returning from the recursive call,
290          there were (may be) other statements which were run after a
291          child node was completely considered by DFS.  Here is the
292          point to do it in the non-recursive variant.
293          E.g. The block just completed is in e->dest for forward DFS,
294          the block not yet completed (the parent of the one above)
295          in e->src.  This could be used e.g. for computing the number of
296          descendants or the tree depth.  */
297       if (reverse)
298         e = e->pred_next;
299       else
300         e = e->succ_next;
301     }
302   free (stack);
303 }
304
305 /* The main entry for calculating the DFS tree or forest.  DI is our working
306    structure and REVERSE is true, if we are interested in the reverse flow
307    graph.  In that case the result is not necessarily a tree but a forest,
308    because there may be nodes from which the EXIT_BLOCK is unreachable.  */
309
310 static void
311 calc_dfs_tree (di, reverse)
312      struct dom_info *di;
313      enum cdi_direction reverse;
314 {
315   /* The first block is the ENTRY_BLOCK (or EXIT_BLOCK if REVERSE).  */
316   basic_block begin = reverse ? EXIT_BLOCK_PTR : ENTRY_BLOCK_PTR;
317   di->dfs_order[last_basic_block] = di->dfsnum;
318   di->dfs_to_bb[di->dfsnum] = begin;
319   di->dfsnum++;
320
321   calc_dfs_tree_nonrec (di, begin, reverse);
322
323   if (reverse)
324     {
325       /* In the post-dom case we may have nodes without a path to EXIT_BLOCK.
326          They are reverse-unreachable.  In the dom-case we disallow such
327          nodes, but in post-dom we have to deal with them, so we simply
328          include them in the DFS tree which actually becomes a forest.  */
329       basic_block b;
330       FOR_EACH_BB_REVERSE (b)
331         {
332           if (di->dfs_order[b->index])
333             continue;
334           di->dfs_order[b->index] = di->dfsnum;
335           di->dfs_to_bb[di->dfsnum] = b;
336           di->dfsnum++;
337           calc_dfs_tree_nonrec (di, b, reverse);
338         }
339     }
340
341   di->nodes = di->dfsnum - 1;
342
343   /* This aborts e.g. when there is _no_ path from ENTRY to EXIT at all.  */
344   if (di->nodes != (unsigned int) n_basic_blocks + 1)
345     abort ();
346 }
347
348 /* Compress the path from V to the root of its set and update path_min at the
349    same time.  After compress(di, V) set_chain[V] is the root of the set V is
350    in and path_min[V] is the node with the smallest key[] value on the path
351    from V to that root.  */
352
353 static void
354 compress (di, v)
355      struct dom_info *di;
356      TBB v;
357 {
358   /* Btw. It's not worth to unrecurse compress() as the depth is usually not
359      greater than 5 even for huge graphs (I've not seen call depth > 4).
360      Also performance wise compress() ranges _far_ behind eval().  */
361   TBB parent = di->set_chain[v];
362   if (di->set_chain[parent])
363     {
364       compress (di, parent);
365       if (di->key[di->path_min[parent]] < di->key[di->path_min[v]])
366         di->path_min[v] = di->path_min[parent];
367       di->set_chain[v] = di->set_chain[parent];
368     }
369 }
370
371 /* Compress the path from V to the set root of V if needed (when the root has
372    changed since the last call).  Returns the node with the smallest key[]
373    value on the path from V to the root.  */
374
375 static inline TBB
376 eval (di, v)
377      struct dom_info *di;
378      TBB v;
379 {
380   /* The representant of the set V is in, also called root (as the set
381      representation is a tree).  */
382   TBB rep = di->set_chain[v];
383
384   /* V itself is the root.  */
385   if (!rep)
386     return di->path_min[v];
387
388   /* Compress only if necessary.  */
389   if (di->set_chain[rep])
390     {
391       compress (di, v);
392       rep = di->set_chain[v];
393     }
394
395   if (di->key[di->path_min[rep]] >= di->key[di->path_min[v]])
396     return di->path_min[v];
397   else
398     return di->path_min[rep];
399 }
400
401 /* This essentially merges the two sets of V and W, giving a single set with
402    the new root V.  The internal representation of these disjoint sets is a
403    balanced tree.  Currently link(V,W) is only used with V being the parent
404    of W.  */
405
406 static void
407 link_roots (di, v, w)
408      struct dom_info *di;
409      TBB v, w;
410 {
411   TBB s = w;
412
413   /* Rebalance the tree.  */
414   while (di->key[di->path_min[w]] < di->key[di->path_min[di->set_child[s]]])
415     {
416       if (di->set_size[s] + di->set_size[di->set_child[di->set_child[s]]]
417           >= 2 * di->set_size[di->set_child[s]])
418         {
419           di->set_chain[di->set_child[s]] = s;
420           di->set_child[s] = di->set_child[di->set_child[s]];
421         }
422       else
423         {
424           di->set_size[di->set_child[s]] = di->set_size[s];
425           s = di->set_chain[s] = di->set_child[s];
426         }
427     }
428
429   di->path_min[s] = di->path_min[w];
430   di->set_size[v] += di->set_size[w];
431   if (di->set_size[v] < 2 * di->set_size[w])
432     {
433       TBB tmp = s;
434       s = di->set_child[v];
435       di->set_child[v] = tmp;
436     }
437
438   /* Merge all subtrees.  */
439   while (s)
440     {
441       di->set_chain[s] = v;
442       s = di->set_child[s];
443     }
444 }
445
446 /* This calculates the immediate dominators (or post-dominators if REVERSE is
447    true).  DI is our working structure and should hold the DFS forest.
448    On return the immediate dominator to node V is in di->dom[V].  */
449
450 static void
451 calc_idoms (di, reverse)
452      struct dom_info *di;
453      enum cdi_direction reverse;
454 {
455   TBB v, w, k, par;
456   basic_block en_block;
457   if (reverse)
458     en_block = EXIT_BLOCK_PTR;
459   else
460     en_block = ENTRY_BLOCK_PTR;
461
462   /* Go backwards in DFS order, to first look at the leafs.  */
463   v = di->nodes;
464   while (v > 1)
465     {
466       basic_block bb = di->dfs_to_bb[v];
467       edge e, e_next;
468
469       par = di->dfs_parent[v];
470       k = v;
471       if (reverse)
472         e = bb->succ;
473       else
474         e = bb->pred;
475
476       /* Search all direct predecessors for the smallest node with a path
477          to them.  That way we have the smallest node with also a path to
478          us only over nodes behind us.  In effect we search for our
479          semidominator.  */
480       for (; e; e = e_next)
481         {
482           TBB k1;
483           basic_block b;
484
485           if (reverse)
486             {
487               b = e->dest;
488               e_next = e->succ_next;
489             }
490           else
491             {
492               b = e->src;
493               e_next = e->pred_next;
494             }
495           if (b == en_block)
496             k1 = di->dfs_order[last_basic_block];
497           else
498             k1 = di->dfs_order[b->index];
499
500           /* Call eval() only if really needed.  If k1 is above V in DFS tree,
501              then we know, that eval(k1) == k1 and key[k1] == k1.  */
502           if (k1 > v)
503             k1 = di->key[eval (di, k1)];
504           if (k1 < k)
505             k = k1;
506         }
507
508       di->key[v] = k;
509       link_roots (di, par, v);
510       di->next_bucket[v] = di->bucket[k];
511       di->bucket[k] = v;
512
513       /* Transform semidominators into dominators.  */
514       for (w = di->bucket[par]; w; w = di->next_bucket[w])
515         {
516           k = eval (di, w);
517           if (di->key[k] < di->key[w])
518             di->dom[w] = k;
519           else
520             di->dom[w] = par;
521         }
522       /* We don't need to cleanup next_bucket[].  */
523       di->bucket[par] = 0;
524       v--;
525     }
526
527   /* Explicitly define the dominators.  */
528   di->dom[1] = 0;
529   for (v = 2; v <= di->nodes; v++)
530     if (di->dom[v] != di->key[v])
531       di->dom[v] = di->dom[di->dom[v]];
532 }
533
534 /* Convert the information about immediate dominators (in DI) to sets of all
535    dominators (in DOMINATORS).  */
536
537 static void
538 idoms_to_doms (di, dominators)
539      struct dom_info *di;
540      sbitmap *dominators;
541 {
542   TBB i, e_index;
543   int bb, bb_idom;
544   sbitmap_vector_zero (dominators, last_basic_block);
545   /* We have to be careful, to not include the ENTRY_BLOCK or EXIT_BLOCK
546      in the list of (post)-doms, so remember that in e_index.  */
547   e_index = di->dfs_order[last_basic_block];
548
549   for (i = 1; i <= di->nodes; i++)
550     {
551       if (i == e_index)
552         continue;
553       bb = di->dfs_to_bb[i]->index;
554
555       if (di->dom[i] && (di->dom[i] != e_index))
556         {
557           bb_idom = di->dfs_to_bb[di->dom[i]]->index;
558           sbitmap_copy (dominators[bb], dominators[bb_idom]);
559         }
560       else
561         {
562           /* It has no immediate dom or only ENTRY_BLOCK or EXIT_BLOCK.
563              If it is a child of ENTRY_BLOCK that's OK, and it's only
564              dominated by itself; if it's _not_ a child of ENTRY_BLOCK, it
565              means, it is unreachable.  That case has been disallowed in the
566              building of the DFS tree, so we are save here.  For the reverse
567              flow graph it means, it has no children, so, to be compatible
568              with the old code, we set the post_dominators to all one.  */
569           if (!di->dom[i])
570             {
571               sbitmap_ones (dominators[bb]);
572             }
573         }
574       SET_BIT (dominators[bb], bb);
575     }
576 }
577
578 /* The main entry point into this module.  IDOM is an integer array with room
579    for last_basic_block integers, DOMS is a preallocated sbitmap array having
580    room for last_basic_block^2 bits, and POST is true if the caller wants to
581    know post-dominators.
582
583    On return IDOM[i] will be the BB->index of the immediate (post) dominator
584    of basic block i, and DOMS[i] will have set bit j if basic block j is a
585    (post)dominator for block i.
586
587    Either IDOM or DOMS may be NULL (meaning the caller is not interested in
588    immediate resp. all dominators).  */
589
590 void
591 calculate_dominance_info (idom, doms, reverse)
592      int *idom;
593      sbitmap *doms;
594      enum cdi_direction reverse;
595 {
596   struct dom_info di;
597
598   if (!doms && !idom)
599     return;
600   init_dom_info (&di);
601   calc_dfs_tree (&di, reverse);
602   calc_idoms (&di, reverse);
603
604   if (idom)
605     {
606       basic_block b;
607
608       FOR_EACH_BB (b)
609         {
610           TBB d = di.dom[di.dfs_order[b->index]];
611
612           /* The old code didn't modify array elements of nodes having only
613              itself as dominator (d==0) or only ENTRY_BLOCK (resp. EXIT_BLOCK)
614              (d==1).  */
615           if (d > 1)
616             idom[b->index] = di.dfs_to_bb[d]->index;
617         }
618     }
619   if (doms)
620     idoms_to_doms (&di, doms);
621
622   free_dom_info (&di);
623 }