OSDN Git Service

PR c++/28710
[pf3gnuchains/gcc-fork.git] / gcc / sbitmap.c
index 508eca3..5ef7f08 100644 (file)
@@ -1,5 +1,5 @@
 /* Simple bitmaps.
-   Copyright (C) 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -15,8 +15,8 @@ for more details.
 
 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.  */
 
 #include "config.h"
 #include "system.h"
@@ -25,6 +25,7 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include "rtl.h"
 #include "flags.h"
 #include "hard-reg-set.h"
+#include "obstack.h"
 #include "basic-block.h"
 
 /* Bitmap manipulation routines.  */
@@ -41,7 +42,7 @@ sbitmap_alloc (unsigned int n_elms)
   bytes = size * sizeof (SBITMAP_ELT_TYPE);
   amt = (sizeof (struct simple_bitmap_def)
         + bytes - sizeof (SBITMAP_ELT_TYPE));
-  bmap = (sbitmap) xmalloc (amt);
+  bmap = xmalloc (amt);
   bmap->n_bits = n_elms;
   bmap->size = size;
   bmap->bytes = bytes;
@@ -64,7 +65,7 @@ sbitmap_resize (sbitmap bmap, unsigned int n_elms, int def)
     {
       amt = (sizeof (struct simple_bitmap_def)
            + bytes - sizeof (SBITMAP_ELT_TYPE));
-      bmap = (sbitmap) xrealloc (bmap, amt);
+      bmap = xrealloc (bmap, amt);
     }
 
   if (n_elms > bmap->n_bits)
@@ -103,6 +104,32 @@ sbitmap_resize (sbitmap bmap, unsigned int n_elms, int def)
   return bmap;
 }
 
+/* Re-allocate a simple bitmap of N_ELMS bits. New storage is uninitialized.  */
+
+sbitmap
+sbitmap_realloc (sbitmap src, unsigned int n_elms)
+{
+  unsigned int bytes, size, amt;
+  sbitmap bmap;
+
+  size = SBITMAP_SET_SIZE (n_elms);
+  bytes = size * sizeof (SBITMAP_ELT_TYPE);
+  amt = (sizeof (struct simple_bitmap_def)
+        + bytes - sizeof (SBITMAP_ELT_TYPE));
+
+  if (src->bytes  >= bytes)
+    {
+      src->n_bits = n_elms;
+      return src;
+    }
+
+  bmap = (sbitmap) xrealloc (src, amt);
+  bmap->n_bits = n_elms;
+  bmap->size = size;
+  bmap->bytes = bytes;
+  return bmap;
+}
+
 /* Allocate a vector of N_VECS bitmaps of N_ELMS bits.  */
 
 sbitmap *
@@ -130,7 +157,7 @@ sbitmap_vector_alloc (unsigned int n_vecs, unsigned int n_elms)
   }
 
   amt = vector_bytes + (n_vecs * elm_bytes);
-  bitmap_vector = (sbitmap *) xmalloc (amt);
+  bitmap_vector = xmalloc (amt);
 
   for (i = 0, offset = vector_bytes; i < n_vecs; i++, offset += elm_bytes)
     {
@@ -250,9 +277,16 @@ sbitmap_not (sbitmap dst, sbitmap src)
   unsigned int i, n = dst->size;
   sbitmap_ptr dstp = dst->elms;
   sbitmap_ptr srcp = src->elms;
+  unsigned int last_bit;
 
   for (i = 0; i < n; i++)
     *dstp++ = ~*srcp++;
+
+  /* Zero all bits past n_bits, by ANDing dst with sbitmap_ones.  */
+  last_bit = src->n_bits % SBITMAP_ELT_BITS;
+  if (last_bit)
+    dst->elms[n-1] = dst->elms[n-1]
+      & ((SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit));
 }
 
 /* Set the bits in DST to be the difference between the bits
@@ -268,8 +302,7 @@ sbitmap_difference (sbitmap dst, sbitmap a, sbitmap b)
   sbitmap_ptr bp = b->elms;
 
   /* A should be at least as large as DEST, to have a defined source.  */
-  if (a->size < dst_size)
-    abort ();
+  gcc_assert (a->size >= dst_size);
   /* If minuend is smaller, we simply pretend it to be zero bits, i.e.
      only copy the subtrahend into dest.  */
   if (b->size < min_size)
@@ -283,6 +316,24 @@ sbitmap_difference (sbitmap dst, sbitmap a, sbitmap b)
       *dstp++ = *ap++;
 }
 
+/* Return true if there are any bits set in A are also set in B.
+   Return false otherwise.  */
+
+bool
+sbitmap_any_common_bits (sbitmap a, sbitmap b)
+{
+  sbitmap_ptr ap = a->elms;
+  sbitmap_ptr bp = b->elms;
+  unsigned int i, n;
+
+  n = MIN (a->size, b->size);
+  for (i = 0; i < n; i++)
+    if ((*ap++ & *bp++) != 0)
+      return true;
+
+  return false;
+}
+
 /* Set DST to be (A and B).
    Return nonzero if any change is made.  */
 
@@ -298,7 +349,7 @@ sbitmap_a_and_b_cg (sbitmap dst, sbitmap a, sbitmap b)
   for (i = 0; i < n; i++)
     {
       SBITMAP_ELT_TYPE tmp = *ap++ & *bp++;
-      changed = *dstp ^ tmp;
+      changed |= *dstp ^ tmp;
       *dstp++ = tmp;
     }
 
@@ -332,7 +383,7 @@ sbitmap_a_xor_b_cg (sbitmap dst, sbitmap a, sbitmap b)
   for (i = 0; i < n; i++)
     {
       SBITMAP_ELT_TYPE tmp = *ap++ ^ *bp++;
-      changed = *dstp ^ tmp;
+      changed |= *dstp ^ tmp;
       *dstp++ = tmp;
     }
 
@@ -366,7 +417,7 @@ sbitmap_a_or_b_cg (sbitmap dst, sbitmap a, sbitmap b)
   for (i = 0; i < n; i++)
     {
       SBITMAP_ELT_TYPE tmp = *ap++ | *bp++;
-      changed = *dstp ^ tmp;
+      changed |= *dstp ^ tmp;
       *dstp++ = tmp;
     }
 
@@ -482,12 +533,14 @@ sbitmap_intersection_of_succs (sbitmap dst, sbitmap *src, int bb)
   basic_block b = BASIC_BLOCK (bb);
   unsigned int set_size = dst->size;
   edge e;
+  unsigned ix;
 
-  for (e = b->succ; e != 0; e = e->succ_next)
+  for (e = NULL, ix = 0; ix < EDGE_COUNT (b->succs); ix++)
     {
+      e = EDGE_SUCC (b, ix);
       if (e->dest == EXIT_BLOCK_PTR)
        continue;
-
+      
       sbitmap_copy (dst, src[e->dest->index]);
       break;
     }
@@ -495,11 +548,12 @@ sbitmap_intersection_of_succs (sbitmap dst, sbitmap *src, int bb)
   if (e == 0)
     sbitmap_ones (dst);
   else
-    for (e = e->succ_next; e != 0; e = e->succ_next)
+    for (++ix; ix < EDGE_COUNT (b->succs); ix++)
       {
        unsigned int i;
        sbitmap_ptr p, r;
 
+       e = EDGE_SUCC (b, ix);
        if (e->dest == EXIT_BLOCK_PTR)
          continue;
 
@@ -519,9 +573,11 @@ sbitmap_intersection_of_preds (sbitmap dst, sbitmap *src, int bb)
   basic_block b = BASIC_BLOCK (bb);
   unsigned int set_size = dst->size;
   edge e;
+  unsigned ix;
 
-  for (e = b->pred; e != 0; e = e->pred_next)
+  for (e = NULL, ix = 0; ix < EDGE_COUNT (b->preds); ix++)
     {
+      e = EDGE_PRED (b, ix);
       if (e->src == ENTRY_BLOCK_PTR)
        continue;
 
@@ -532,11 +588,12 @@ sbitmap_intersection_of_preds (sbitmap dst, sbitmap *src, int bb)
   if (e == 0)
     sbitmap_ones (dst);
   else
-    for (e = e->pred_next; e != 0; e = e->pred_next)
+    for (++ix; ix < EDGE_COUNT (b->preds); ix++)
       {
        unsigned int i;
        sbitmap_ptr p, r;
 
+       e = EDGE_PRED (b, ix);
        if (e->src == ENTRY_BLOCK_PTR)
          continue;
 
@@ -556,9 +613,11 @@ sbitmap_union_of_succs (sbitmap dst, sbitmap *src, int bb)
   basic_block b = BASIC_BLOCK (bb);
   unsigned int set_size = dst->size;
   edge e;
+  unsigned ix;
 
-  for (e = b->succ; e != 0; e = e->succ_next)
+  for (ix = 0; ix < EDGE_COUNT (b->succs); ix++)
     {
+      e = EDGE_SUCC (b, ix);
       if (e->dest == EXIT_BLOCK_PTR)
        continue;
 
@@ -566,14 +625,15 @@ sbitmap_union_of_succs (sbitmap dst, sbitmap *src, int bb)
       break;
     }
 
-  if (e == 0)
+  if (ix == EDGE_COUNT (b->succs))
     sbitmap_zero (dst);
   else
-    for (e = e->succ_next; e != 0; e = e->succ_next)
+    for (ix++; ix < EDGE_COUNT (b->succs); ix++)
       {
        unsigned int i;
        sbitmap_ptr p, r;
 
+       e = EDGE_SUCC (b, ix);
        if (e->dest == EXIT_BLOCK_PTR)
          continue;
 
@@ -593,9 +653,11 @@ sbitmap_union_of_preds (sbitmap dst, sbitmap *src, int bb)
   basic_block b = BASIC_BLOCK (bb);
   unsigned int set_size = dst->size;
   edge e;
+  unsigned ix;
 
-  for (e = b->pred; e != 0; e = e->pred_next)
+  for (ix = 0; ix < EDGE_COUNT (b->preds); ix++)
     {
+      e = EDGE_PRED (b, ix);
       if (e->src== ENTRY_BLOCK_PTR)
        continue;
 
@@ -603,14 +665,15 @@ sbitmap_union_of_preds (sbitmap dst, sbitmap *src, int bb)
       break;
     }
 
-  if (e == 0)
+  if (ix == EDGE_COUNT (b->preds))
     sbitmap_zero (dst);
   else
-    for (e = e->pred_next; e != 0; e = e->pred_next)
+    for (ix++; ix < EDGE_COUNT (b->preds); ix++)
       {
        unsigned int i;
        sbitmap_ptr p, r;
 
+       e = EDGE_PRED (b, ix);
        if (e->src == ENTRY_BLOCK_PTR)
          continue;
 
@@ -627,9 +690,11 @@ sbitmap_union_of_preds (sbitmap dst, sbitmap *src, int bb)
 int
 sbitmap_first_set_bit (sbitmap bmap)
 {
-  unsigned int n;
+  unsigned int n = 0;
+  sbitmap_iterator sbi;
 
-  EXECUTE_IF_SET_IN_SBITMAP (bmap, 0, n, { return n; });
+  EXECUTE_IF_SET_IN_SBITMAP (bmap, 0, n, sbi)
+    return n;
   return -1;
 }