added a callback function to check whether a given entity is a allocation call
[libfirm] / ir / adt / bitset.h
index 4f249be..0b647d5 100644 (file)
@@ -1,19 +1,40 @@
+/*
+ * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
+ *
+ * This file is part of libFirm.
+ *
+ * This file may be distributed and/or modified under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation and appearing in the file LICENSE.GPL included in the
+ * packaging of this file.
+ *
+ * Licensees holding valid libFirm Professional Edition licenses may use
+ * this file in accordance with the libFirm Commercial License.
+ * Agreement provided with the Software.
+ *
+ * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+ * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE.
+ */
+
 /**
- * @file bitset.h
- * @date 15.10.2004
- * @author Sebastian Hack
- * @brief A bitset implementation.
+ * @file
+ * @brief   A bitset implementation.
+ * @author  Sebastian Hack
+ * @date    15.10.2004
+ * @version $Id$
  */
+#ifndef FIRM_ADT_BITSET_H
+#define FIRM_ADT_BITSET_H
 
-#ifndef __FIRM_BITSET_H
-#define __FIRM_BITSET_H
+#include "firm_config.h"
 
 #include <stdlib.h>
 #include <stdio.h>
 #include <assert.h>
 #include <string.h>
 
-#include "firm_config.h"
+#include "xmalloc.h"
 #include "bitfiddle.h"
 
 typedef unsigned int bitset_pos_t;
@@ -28,7 +49,7 @@ typedef unsigned int bitset_pos_t;
 
 typedef struct _bitset_t {
        bitset_pos_t units;
-  bitset_pos_t size;
+       bitset_pos_t size;
        bitset_unit_t *data;
 } bitset_t;
 
@@ -60,7 +81,7 @@ static INLINE bitset_t *_bitset_prepare(void *area, bitset_pos_t size)
        bitset_t *ptr = area;
        memset(area, 0, _bitset_overall_size(sizeof(bitset_t), size));
        ptr->units = _bitset_units(size);
-  ptr->size = size;
+       ptr->size = size;
        ptr->data = _bitset_data_ptr(area, sizeof(bitset_t), size);
        return ptr;
 }
@@ -73,8 +94,10 @@ static INLINE bitset_t *_bitset_prepare(void *area, bitset_pos_t size)
  */
 static INLINE bitset_t *_bitset_mask_highest(bitset_t *bs)
 {
-  bs->data[bs->units - 1] &= (1 << (bs->size & BS_UNIT_MASK)) - 1;
-  return bs;
+       bitset_pos_t rest = bs->size & BS_UNIT_MASK;
+       if (rest)
+               bs->data[bs->units - 1] &= (1 << rest) - 1;
+       return bs;
 }
 
 /**
@@ -90,7 +113,7 @@ static INLINE bitset_t *_bitset_mask_highest(bitset_t *bs)
  * @param bs The bitset.
  * @return The highest bit which can be set or cleared plus 1.
  */
-#define bistet_size(bs)  ((bs)->size)
+#define bitset_size(bs)  ((bs)->size)
 
 /**
  * Allocate a bitset on an obstack.
@@ -99,7 +122,7 @@ static INLINE bitset_t *_bitset_mask_highest(bitset_t *bs)
  * @return A pointer to an empty initialized bitset.
  */
 #define bitset_obstack_alloc(obst,size) \
-  _bitset_prepare(obstack_alloc(obst, _bitset_overall_size(sizeof(bitset_t), size)), size)
+       _bitset_prepare(obstack_alloc(obst, _bitset_overall_size(sizeof(bitset_t), size)), size)
 
 /**
  * Allocate a bitset via malloc.
@@ -107,7 +130,7 @@ static INLINE bitset_t *_bitset_mask_highest(bitset_t *bs)
  * @return A pointer to an empty initialized bitset.
  */
 #define bitset_malloc(size) \
-       _bitset_prepare(malloc(_bitset_overall_size(sizeof(bitset_t), size)), size)
+       _bitset_prepare(xmalloc(_bitset_overall_size(sizeof(bitset_t), size)), size)
 
 /**
  * Free a bitset allocated with bitset_malloc().
@@ -134,7 +157,7 @@ static INLINE bitset_t *_bitset_mask_highest(bitset_t *bs)
 static INLINE bitset_unit_t *_bitset_get_unit(const bitset_t *bs, bitset_pos_t bit)
 {
        /* assert(bit < bs->units * BS_UNIT_SIZE_BITS && "Bit too large"); */
-  assert(bit <= bs->size && "Bit to large");
+       assert(bit <= bs->size && "Bit to large");
        return bs->data + bit / BS_UNIT_SIZE_BITS;
 }
 
@@ -183,6 +206,18 @@ static INLINE void bitset_flip(bitset_t *bs, bitset_pos_t bit)
        _bitset_inside_flip(unit, bit & BS_UNIT_MASK);
 }
 
+/**
+ * Flip the whole bitset.
+ * @param bs The bitset.
+ */
+static INLINE void bitset_flip_all(bitset_t *bs)
+{
+       bitset_pos_t i;
+       for(i = 0; i < bs->units; i++)
+               _bitset_inside_flip_unit(&bs->data[i]);
+       _bitset_mask_highest(bs);
+}
+
 /**
  * Copy a bitset to another.
  * @param tgt The target bitset.
@@ -252,8 +287,9 @@ static INLINE bitset_pos_t _bitset_next(const bitset_t *bs,
                bitset_pos_t pos, int set)
 {
        bitset_pos_t unit_number = pos / BS_UNIT_SIZE_BITS;
+       bitset_pos_t res;
 
-       if(unit_number >= bs->units)
+       if(pos >= bs->size)
                return -1;
 
        {
@@ -275,8 +311,10 @@ static INLINE bitset_pos_t _bitset_next(const bitset_t *bs,
                        _bitset_inside_ntz_value((set ? curr_unit : ~curr_unit) & ~in_unit_mask);
 
                /* If there is a bit set in the current unit, exit. */
-               if(next_in_this_unit < BS_UNIT_SIZE_BITS)
-                       return next_in_this_unit + unit_number * BS_UNIT_SIZE_BITS;
+               if (next_in_this_unit < BS_UNIT_SIZE_BITS) {
+                       res = next_in_this_unit + unit_number * BS_UNIT_SIZE_BITS;
+                       return res < bs->size ? res : -1;
+               }
 
                /* Else search for set bits in the next units. */
                else {
@@ -286,8 +324,10 @@ static INLINE bitset_pos_t _bitset_next(const bitset_t *bs,
                                bitset_pos_t first_set =
                                        _bitset_inside_ntz_value(set ? data : ~data);
 
-                               if(first_set < BS_UNIT_SIZE_BITS)
-                                       return first_set + i * BS_UNIT_SIZE_BITS;
+                               if (first_set < BS_UNIT_SIZE_BITS) {
+                                       res = first_set + i * BS_UNIT_SIZE_BITS;
+                                       return res < bs->size ? res : -1;
+                               }
                        }
                }
        }
@@ -304,11 +344,11 @@ static INLINE bitset_pos_t _bitset_next(const bitset_t *bs,
  * @param elm A unsigned long variable.
  */
 #define bitset_foreach(bitset,elm) \
-  for(elm = bitset_next_set(bitset,0); elm != -1; elm = bitset_next_set(bitset,elm+1))
+       for(elm = bitset_next_set(bitset,0); elm != -1; elm = bitset_next_set(bitset,elm+1))
 
 
 #define bitset_foreach_clear(bitset,elm) \
-  for(elm = bitset_next_clear(bitset,0); elm != -1; elm = bitset_next_clear(bitset,elm+1))
+       for(elm = bitset_next_clear(bitset,0); elm != -1; elm = bitset_next_clear(bitset,elm+1))
 
 /**
  * Count the bits set.
@@ -316,12 +356,13 @@ static INLINE bitset_pos_t _bitset_next(const bitset_t *bs,
  * @param bs The bitset.
  * @return The number of bits set in the bitset.
  */
-static INLINE bitset_pos_t bitset_popcnt(const bitset_t *bs)
+static INLINE unsigned bitset_popcnt(const bitset_t *bs)
 {
-       bitset_pos_t i, pop = 0;
+       bitset_pos_t  i;
        bitset_unit_t *unit;
+       unsigned      pop = 0;
 
-       for(i = 0, unit = bs->data; i < bs->units; ++i, ++unit)
+       for (i = 0, unit = bs->data; i < bs->units; ++i, ++unit)
                pop += _bitset_inside_pop(unit);
 
        return pop;
@@ -335,7 +376,7 @@ static INLINE bitset_pos_t bitset_popcnt(const bitset_t *bs)
 static INLINE bitset_t *bitset_clear_all(bitset_t *bs)
 {
        memset(bs->data, 0, BS_UNIT_SIZE * bs->units);
-  return bs;
+       return bs;
 }
 
 /**
@@ -346,7 +387,7 @@ static INLINE bitset_t *bitset_clear_all(bitset_t *bs)
 static INLINE bitset_t *bitset_set_all(bitset_t *bs)
 {
        memset(bs->data, -1, bs->units * BS_UNIT_SIZE);
-  return _bitset_mask_highest(bs);
+       return _bitset_mask_highest(bs);
 }
 
 /**
@@ -392,23 +433,55 @@ static INLINE void bitset_minus1(bitset_t *bs)
 {
 #define _SH (sizeof(bitset_unit_t) * 8 - 1)
 
-  bitset_pos_t i;
+       bitset_pos_t i;
 
-  for(i = 0; i < bs->units; ++i) {
-    bitset_unit_t unit = bs->data[i];
-    bitset_unit_t um1  = unit - 1;
+       for(i = 0; i < bs->units; ++i) {
+               bitset_unit_t unit = bs->data[i];
+               bitset_unit_t um1  = unit - 1;
 
-    bs->data[i] = um1;
+               bs->data[i] = um1;
 
-    if(((unit >> _SH) ^ (um1 >> _SH)) == 0)
-      break;
-  }
+               if(((unit >> _SH) ^ (um1 >> _SH)) == 0)
+                       break;
+       }
 #undef _SH
 }
 
+/**
+ * Check if two bitsets intersect.
+ * @param a The first bitset.
+ * @param b The second bitset.
+ * @return 1 if they have a bit in common, 0 if not.
+ */
+static INLINE int bitset_intersect(const bitset_t *a, const bitset_t *b)
+{
+       bitset_pos_t n = a->units < b->units ? a->units : b->units;
+       bitset_pos_t i;
+
+       for (i = 0; i < n; ++i)
+               if (a->data[i] & b->data[i])
+                       return 1;
+
+       return 0;
+}
+
+/**
+ * Check, if a bitset is empty.
+ * @param a The bitset.
+ * @return 1, if the bitset is empty, 0 if not.
+ */
+static INLINE int bitset_is_empty(const bitset_t *a)
+{
+       bitset_pos_t i;
+       for (i = 0; i < a->units; ++i)
+               if (a->data[i] != 0)
+                       return 0;
+       return 1;
+}
+
 /**
  * Print a bitset to a stream.
- * The bitset is printed as a comma seperated list of bits set.
+ * The bitset is printed as a comma separated list of bits set.
  * @param file The stream.
  * @param bs The bitset.
  */
@@ -417,12 +490,12 @@ static INLINE void bitset_fprint(FILE *file, const bitset_t *bs)
        const char *prefix = "";
        int i;
 
-       putc('[', file);
+       putc('{', file);
        for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
                fprintf(file, "%s%u", prefix, i);
                prefix = ",";
        }
-       putc(']', file);
+       putc('}', file);
 }
 
 static INLINE void bitset_debug_fprint(FILE *file, const bitset_t *bs)
@@ -434,6 +507,30 @@ static INLINE void bitset_debug_fprint(FILE *file, const bitset_t *bs)
                fprintf(file, " " BITSET_UNIT_FMT, bs->data[i]);
 }
 
+/**
+ * Perform tgt = tgt \ src operation.
+ * @param tgt  The target bitset.
+ * @param src  The source bitset.
+ * @return the tgt set.
+ */
+static INLINE bitset_t *bitset_andnot(bitset_t *tgt, const bitset_t *src);
+
+/**
+ * Perform Union, tgt = tgt u src operation.
+ * @param tgt  The target bitset.
+ * @param src  The source bitset.
+ * @return the tgt set.
+ */
+static INLINE bitset_t *bitset_or(bitset_t *tgt, const bitset_t *src);
+
+/**
+ * Perform tgt = tgt ^ ~src operation.
+ * @param tgt  The target bitset.
+ * @param src  The source bitset.
+ * @return the tgt set.
+ */
+static INLINE bitset_t *bitset_xor(bitset_t *tgt, const bitset_t *src);
+
 /*
  * Here, the binary operations follow.
  * And, Or, And Not, Xor are available.