OSDN Git Service

* Makefile.def (flags_to_pass): Add CFLAGS_FOR_BUILD.
[pf3gnuchains/gcc-fork.git] / gcc / dominance.c
index d46d8f5..8cfaba0 100644 (file)
@@ -1,5 +1,5 @@
 /* Calculate (post)dominators in slightly super-linear time.
-   Copyright (C) 2000, 2003, 2004 Free Software Foundation, Inc.
+   Copyright (C) 2000, 2003, 2004, 2005 Free Software Foundation, Inc.
    Contributed by Michael Matz (matz@ifh.de).
 
    This file is part of GCC.
@@ -16,8 +16,8 @@
 
    You should have received a copy of the GNU General Public License
    along with GCC; see the file COPYING.  If not, write to the Free
-   Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA.  */
+   Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.  */
 
 /* This file implements the well known algorithm from Lengauer and Tarjan
    to compute the dominators in a control flow graph.  A basic block D is said
@@ -30,7 +30,7 @@
 
    The algorithm computes this dominator tree implicitly by computing for
    each block its immediate dominator.  We use tree balancing and path
-   compression, so its the O(e*a(e,v)) variant, where a(e,v) is the very
+   compression, so it's the O(e*a(e,v)) variant, where a(e,v) is the very
    slowly growing functional inverse of the Ackerman function.  */
 
 #include "config.h"
@@ -39,8 +39,9 @@
 #include "tm.h"
 #include "rtl.h"
 #include "hard-reg-set.h"
+#include "obstack.h"
 #include "basic-block.h"
-#include "errors.h"
+#include "toplev.h"
 #include "et-forest.h"
 
 /* Whether the dominators and the postdominators are available.  */
@@ -169,7 +170,7 @@ init_dom_info (struct dom_info *di, enum cdi_direction dir)
   di->dfsnum = 1;
   di->nodes = 0;
 
-  di->fake_exit_edge = dir ? BITMAP_XMALLOC () : NULL;
+  di->fake_exit_edge = dir ? BITMAP_ALLOC (NULL) : NULL;
 }
 
 #undef init_ar
@@ -190,7 +191,7 @@ free_dom_info (struct dom_info *di)
   free (di->set_child);
   free (di->dfs_order);
   free (di->dfs_to_bb);
-  BITMAP_XFREE (di->fake_exit_edge);
+  BITMAP_FREE (di->fake_exit_edge);
 }
 
 /* The nonrecursive variant of creating a DFS tree.  DI is our working
@@ -206,7 +207,8 @@ calc_dfs_tree_nonrec (struct dom_info *di, basic_block bb,
   /* We call this _only_ if bb is not already visited.  */
   edge e;
   TBB child_i, my_i = 0;
-  edge *stack;
+  edge_iterator *stack;
+  edge_iterator ei, einext;
   int sp;
   /* Start block (ENTRY_BLOCK_PTR for forward problem, EXIT_BLOCK for backward
      problem).  */
@@ -214,19 +216,19 @@ calc_dfs_tree_nonrec (struct dom_info *di, basic_block bb,
   /* Ending block.  */
   basic_block ex_block;
 
-  stack = xmalloc ((n_basic_blocks + 3) * sizeof (edge));
+  stack = xmalloc ((n_basic_blocks + 3) * sizeof (edge_iterator));
   sp = 0;
 
   /* Initialize our border blocks, and the first edge.  */
   if (reverse)
     {
-      e = bb->pred;
+      ei = ei_start (bb->preds);
       en_block = EXIT_BLOCK_PTR;
       ex_block = ENTRY_BLOCK_PTR;
     }
   else
     {
-      e = bb->succ;
+      ei = ei_start (bb->succs);
       en_block = ENTRY_BLOCK_PTR;
       ex_block = EXIT_BLOCK_PTR;
     }
@@ -238,9 +240,9 @@ calc_dfs_tree_nonrec (struct dom_info *di, basic_block bb,
 
       /* This loop traverses edges e in depth first manner, and fills the
          stack.  */
-      while (e)
+      while (!ei_end_p (ei))
        {
-         edge e_next;
+         e = ei_edge (ei);
 
          /* Deduce from E the current and the next block (BB and BN), and the
             next edge.  */
@@ -253,26 +255,25 @@ calc_dfs_tree_nonrec (struct dom_info *di, basic_block bb,
                 with the next edge out of the current node.  */
              if (bn == ex_block || di->dfs_order[bn->index])
                {
-                 e = e->pred_next;
+                 ei_next (&ei);
                  continue;
                }
              bb = e->dest;
-             e_next = bn->pred;
+             einext = ei_start (bn->preds);
            }
          else
            {
              bn = e->dest;
              if (bn == ex_block || di->dfs_order[bn->index])
                {
-                 e = e->succ_next;
+                 ei_next (&ei);
                  continue;
                }
              bb = e->src;
-             e_next = bn->succ;
+             einext = ei_start (bn->succs);
            }
 
-         if (bn == en_block)
-           abort ();
+         gcc_assert (bn != en_block);
 
          /* Fill the DFS tree info calculatable _before_ recursing.  */
          if (bb != en_block)
@@ -284,13 +285,13 @@ calc_dfs_tree_nonrec (struct dom_info *di, basic_block bb,
          di->dfs_parent[child_i] = my_i;
 
          /* Save the current point in the CFG on the stack, and recurse.  */
-         stack[sp++] = e;
-         e = e_next;
+         stack[sp++] = ei;
+         ei = einext;
        }
 
       if (!sp)
        break;
-      e = stack[--sp];
+      ei = stack[--sp];
 
       /* OK.  The edge-list was exhausted, meaning normally we would
          end the recursion.  After returning from the recursive call,
@@ -301,10 +302,7 @@ calc_dfs_tree_nonrec (struct dom_info *di, basic_block bb,
          the block not yet completed (the parent of the one above)
          in e->src.  This could be used e.g. for computing the number of
          descendants or the tree depth.  */
-      if (reverse)
-       e = e->pred_next;
-      else
-       e = e->succ_next;
+      ei_next (&ei);
     }
   free (stack);
 }
@@ -342,7 +340,7 @@ calc_dfs_tree (struct dom_info *di, enum cdi_direction reverse)
 
       FOR_EACH_BB_REVERSE (b)
        {
-         if (b->succ)
+         if (EDGE_COUNT (b->succs) > 0)
            {
              if (di->dfs_order[b->index] == 0)
                saw_unconnected = true;
@@ -374,9 +372,8 @@ calc_dfs_tree (struct dom_info *di, enum cdi_direction reverse)
 
   di->nodes = di->dfsnum - 1;
 
-  /* This aborts e.g. when there is _no_ path from ENTRY to EXIT at all.  */
-  if (di->nodes != (unsigned int) n_basic_blocks + 1)
-    abort ();
+  /* Make sure there is a path from ENTRY to EXIT at all.  */
+  gcc_assert (di->nodes == (unsigned int) n_basic_blocks + 1);
 }
 
 /* Compress the path from V to the root of its set and update path_min at the
@@ -480,6 +477,8 @@ calc_idoms (struct dom_info *di, enum cdi_direction reverse)
 {
   TBB v, w, k, par;
   basic_block en_block;
+  edge_iterator ei, einext;
+
   if (reverse)
     en_block = EXIT_BLOCK_PTR;
   else
@@ -490,43 +489,38 @@ calc_idoms (struct dom_info *di, enum cdi_direction reverse)
   while (v > 1)
     {
       basic_block bb = di->dfs_to_bb[v];
-      edge e, e_next;
+      edge e;
 
       par = di->dfs_parent[v];
       k = v;
+
+      ei = (reverse) ? ei_start (bb->succs) : ei_start (bb->preds);
+
       if (reverse)
        {
-         e = bb->succ;
-
          /* If this block has a fake edge to exit, process that first.  */
          if (bitmap_bit_p (di->fake_exit_edge, bb->index))
            {
-             e_next = e;
+             einext = ei;
+             einext.index = 0;
              goto do_fake_exit_edge;
            }
        }
-      else
-       e = bb->pred;
 
       /* Search all direct predecessors for the smallest node with a path
          to them.  That way we have the smallest node with also a path to
          us only over nodes behind us.  In effect we search for our
          semidominator.  */
-      for (; e ; e = e_next)
+      while (!ei_end_p (ei))
        {
          TBB k1;
          basic_block b;
 
-         if (reverse)
-           {
-             b = e->dest;
-             e_next = e->succ_next;
-           }
-         else
-           {
-             b = e->src;
-             e_next = e->pred_next;
-           }
+         e = ei_edge (ei);
+         b = (reverse) ? e->dest : e->src;
+         einext = ei;
+         ei_next (&einext);
+
          if (b == en_block)
            {
            do_fake_exit_edge:
@@ -541,6 +535,8 @@ calc_idoms (struct dom_info *di, enum cdi_direction reverse)
            k1 = di->key[eval (di, k1)];
          if (k1 < k)
            k = k1;
+
+         ei = einext;
        }
 
       di->key[v] = k;
@@ -597,8 +593,7 @@ compute_dom_fast_query (enum cdi_direction dir)
   int num = 0;
   basic_block bb;
 
-  if (dom_computed[dir] < DOM_NO_FAST_QUERY)
-    abort ();
+  gcc_assert (dom_info_available_p (dir));
 
   if (dom_computed[dir] == DOM_OK)
     return;
@@ -624,13 +619,9 @@ calculate_dominance_info (enum cdi_direction dir)
   if (dom_computed[dir] == DOM_OK)
     return;
 
-  if (dom_computed[dir] != DOM_NO_FAST_QUERY)
+  if (!dom_info_available_p (dir))
     {
-      if (dom_computed[dir] != DOM_NONE)
-       free_dominance_info (dir);
-
-      if (n_bbs_in_dom_tree[dir])
-       abort ();
+      gcc_assert (!n_bbs_in_dom_tree[dir]);
 
       FOR_ALL_BB (b)
        {
@@ -663,17 +654,16 @@ free_dominance_info (enum cdi_direction dir)
 {
   basic_block bb;
 
-  if (!dom_computed[dir])
+  if (!dom_info_available_p (dir))
     return;
 
   FOR_ALL_BB (bb)
     {
-      delete_from_dominance_info (dir, bb);
+      et_free_tree_force (bb->dom[dir]);
+      bb->dom[dir] = NULL;
     }
 
-  /* If there are any nodes left, something is wrong.  */
-  if (n_bbs_in_dom_tree[dir])
-    abort ();
+  n_bbs_in_dom_tree[dir] = 0;
 
   dom_computed[dir] = DOM_NONE;
 }
@@ -684,8 +674,7 @@ get_immediate_dominator (enum cdi_direction dir, basic_block bb)
 {
   struct et_node *node = bb->dom[dir];
 
-  if (!dom_computed[dir])
-    abort ();
+  gcc_assert (dom_computed[dir]);
 
   if (!node->father)
     return NULL;
@@ -701,8 +690,7 @@ set_immediate_dominator (enum cdi_direction dir, basic_block bb,
 {
   struct et_node *node = bb->dom[dir];
 
-  if (!dom_computed[dir])
-    abort ();
+  gcc_assert (dom_computed[dir]);
 
   if (node->father)
     {
@@ -726,8 +714,7 @@ get_dominated_by (enum cdi_direction dir, basic_block bb, basic_block **bbs)
   int n;
   struct et_node *node = bb->dom[dir], *son = node->son, *ason;
 
-  if (!dom_computed[dir])
-    abort ();
+  gcc_assert (dom_computed[dir]);
 
   if (!son)
     {
@@ -746,6 +733,32 @@ get_dominated_by (enum cdi_direction dir, basic_block bb, basic_block **bbs)
   return n;
 }
 
+/* Find all basic blocks that are immediately dominated (in direction DIR)
+   by some block between N_REGION ones stored in REGION, except for blocks
+   in the REGION itself.  The found blocks are stored to DOMS and their number
+   is returned.  */
+
+unsigned
+get_dominated_by_region (enum cdi_direction dir, basic_block *region,
+                        unsigned n_region, basic_block *doms)
+{
+  unsigned n_doms = 0, i;
+  basic_block dom;
+
+  for (i = 0; i < n_region; i++)
+    region[i]->flags |= BB_DUPLICATED;
+  for (i = 0; i < n_region; i++)
+    for (dom = first_dom_son (dir, region[i]);
+        dom;
+        dom = next_dom_son (dir, dom))
+      if (!(dom->flags & BB_DUPLICATED))
+       doms[n_doms++] = dom;
+  for (i = 0; i < n_region; i++)
+    region[i]->flags &= ~BB_DUPLICATED;
+
+  return n_doms;
+}
+
 /* Redirect all edges pointing to BB to TO.  */
 void
 redirect_immediate_dominators (enum cdi_direction dir, basic_block bb,
@@ -753,8 +766,7 @@ redirect_immediate_dominators (enum cdi_direction dir, basic_block bb,
 {
   struct et_node *bb_node = bb->dom[dir], *to_node = to->dom[dir], *son;
 
-  if (!dom_computed[dir])
-    abort ();
+  gcc_assert (dom_computed[dir]);
 
   if (!bb_node->son)
     return;
@@ -775,8 +787,7 @@ redirect_immediate_dominators (enum cdi_direction dir, basic_block bb,
 basic_block
 nearest_common_dominator (enum cdi_direction dir, basic_block bb1, basic_block bb2)
 {
-  if (!dom_computed[dir])
-    abort ();
+  gcc_assert (dom_computed[dir]);
 
   if (!bb1)
     return bb2;
@@ -786,14 +797,34 @@ nearest_common_dominator (enum cdi_direction dir, basic_block bb1, basic_block b
   return et_nca (bb1->dom[dir], bb2->dom[dir])->data;
 }
 
+
+/* Find the nearest common dominator for the basic blocks in BLOCKS,
+   using dominance direction DIR.  */
+
+basic_block
+nearest_common_dominator_for_set (enum cdi_direction dir, bitmap blocks)
+{
+  unsigned i, first;
+  bitmap_iterator bi;
+  basic_block dom;
+  
+  first = bitmap_first_set_bit (blocks);
+  dom = BASIC_BLOCK (first);
+  EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
+    if (dom != BASIC_BLOCK (i))
+      dom = nearest_common_dominator (dir, dom, BASIC_BLOCK (i));
+
+  return dom;
+}
+
+
 /* Return TRUE in case BB1 is dominated by BB2.  */
 bool
 dominated_by_p (enum cdi_direction dir, basic_block bb1, basic_block bb2)
 { 
   struct et_node *n1 = bb1->dom[dir], *n2 = bb2->dom[dir];
 
-  if (!dom_computed[dir])
-    abort ();
+  gcc_assert (dom_computed[dir]);
 
   if (dom_computed[dir] == DOM_OK)
     return (n1->dfs_num_in >= n2->dfs_num_in
@@ -809,24 +840,27 @@ verify_dominators (enum cdi_direction dir)
   int err = 0;
   basic_block bb;
 
-  if (!dom_computed[dir])
-    abort ();
+  gcc_assert (dom_info_available_p (dir));
 
   FOR_EACH_BB (bb)
     {
       basic_block dom_bb;
+      basic_block imm_bb;
 
       dom_bb = recount_dominator (dir, bb);
-      if (dom_bb != get_immediate_dominator (dir, bb))
+      imm_bb = get_immediate_dominator (dir, bb);
+      if (dom_bb != imm_bb)
        {
-         error ("dominator of %d should be %d, not %d",
-          bb->index, dom_bb->index, get_immediate_dominator(dir, bb)->index);
+         if ((dom_bb == NULL) || (imm_bb == NULL))
+           error ("dominator of %d status unknown", bb->index);
+         else
+           error ("dominator of %d should be %d, not %d",
+                  bb->index, dom_bb->index, imm_bb->index);
          err = 1;
        }
     }
 
-  if (dir == CDI_DOMINATORS
-      && dom_computed[dir] >= DOM_NO_FAST_QUERY)
+  if (dir == CDI_DOMINATORS)
     {
       FOR_EACH_BB (bb)
        {
@@ -838,8 +872,7 @@ verify_dominators (enum cdi_direction dir)
        }
     }
 
-  if (err)
-    abort ();
+  gcc_assert (!err);
 }
 
 /* Determine immediate dominator (or postdominator, according to DIR) of BB,
@@ -852,13 +885,13 @@ recount_dominator (enum cdi_direction dir, basic_block bb)
 {
   basic_block dom_bb = NULL;
   edge e;
+  edge_iterator ei;
 
-  if (!dom_computed[dir])
-    abort ();
+  gcc_assert (dom_computed[dir]);
 
   if (dir == CDI_DOMINATORS)
     {
-      for (e = bb->pred; e; e = e->pred_next)
+      FOR_EACH_EDGE (e, ei, bb->preds)
        {
          /* Ignore the predecessors that either are not reachable from
             the entry block, or whose dominator was not determined yet.  */
@@ -871,7 +904,7 @@ recount_dominator (enum cdi_direction dir, basic_block bb)
     }
   else
     {
-      for (e = bb->succ; e; e = e->succ_next)
+      FOR_EACH_EDGE (e, ei, bb->succs)
        {
          if (!dominated_by_p (dir, e->dest, bb))
            dom_bb = nearest_common_dominator (dir, dom_bb, e->dest);
@@ -889,8 +922,7 @@ iterate_fix_dominators (enum cdi_direction dir, basic_block *bbs, int n)
   int i, changed = 1;
   basic_block old_dom, new_dom;
 
-  if (!dom_computed[dir])
-    abort ();
+  gcc_assert (dom_computed[dir]);
 
   for (i = 0; i < n; i++)
     set_immediate_dominator (dir, bbs[i], NULL);
@@ -911,18 +943,14 @@ iterate_fix_dominators (enum cdi_direction dir, basic_block *bbs, int n)
     }
 
   for (i = 0; i < n; i++)
-    if (!get_immediate_dominator (dir, bbs[i]))
-      abort ();
+    gcc_assert (get_immediate_dominator (dir, bbs[i]));
 }
 
 void
 add_to_dominance_info (enum cdi_direction dir, basic_block bb)
 {
-  if (!dom_computed[dir])
-    abort ();
-
-  if (bb->dom[dir])
-    abort ();
+  gcc_assert (dom_computed[dir]);
+  gcc_assert (!bb->dom[dir]);
 
   n_bbs_in_dom_tree[dir]++;
   
@@ -935,8 +963,7 @@ add_to_dominance_info (enum cdi_direction dir, basic_block bb)
 void
 delete_from_dominance_info (enum cdi_direction dir, basic_block bb)
 {
-  if (!dom_computed[dir])
-    abort ();
+  gcc_assert (dom_computed[dir]);
 
   et_free_tree (bb->dom[dir]);
   bb->dom[dir] = NULL;
@@ -968,6 +995,14 @@ next_dom_son (enum cdi_direction dir, basic_block bb)
   return next->father->son == next ? NULL : next->data;
 }
 
+/* Returns true if dominance information for direction DIR is available.  */
+
+bool
+dom_info_available_p (enum cdi_direction dir)
+{
+  return dom_computed[dir] != DOM_NONE;
+}
+
 void
 debug_dominance_info (enum cdi_direction dir)
 {