sbitmap.h revision 52284
152284Sobrien/* Simple bitmaps.
252284Sobrien   Copyright (C) 1999 Free Software Foundation, Inc.
352284Sobrien
452284SobrienThis file is part of GNU CC.
552284Sobrien
652284SobrienGNU CC is free software; you can redistribute it and/or modify
752284Sobrienit under the terms of the GNU General Public License as published by
852284Sobrienthe Free Software Foundation; either version 2, or (at your option)
952284Sobrienany later version.
1052284Sobrien
1152284SobrienGNU CC is distributed in the hope that it will be useful,
1252284Sobrienbut WITHOUT ANY WARRANTY; without even the implied warranty of
1352284SobrienMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1452284SobrienGNU General Public License for more details.
1552284Sobrien
1652284SobrienYou should have received a copy of the GNU General Public License
1752284Sobrienalong with GNU CC; see the file COPYING.  If not, write to
1852284Sobrienthe Free Software Foundation, 59 Temple Place - Suite 330,
1952284SobrienBoston, MA 02111-1307, USA.  */
2052284Sobrien
2152284Sobrien/* It's not clear yet whether using bitmap.[ch] will be a win.
2252284Sobrien   It should be straightforward to convert so for now we keep things simple
2352284Sobrien   while more important issues are dealt with.  */
2452284Sobrien
2552284Sobrien#define SBITMAP_ELT_BITS HOST_BITS_PER_WIDE_INT
2652284Sobrien#define SBITMAP_ELT_TYPE unsigned HOST_WIDE_INT
2752284Sobrien
2852284Sobrientypedef struct simple_bitmap_def {
2952284Sobrien  /* Number of bits.  */
3052284Sobrien  int n_bits;
3152284Sobrien  /* Size in elements.  */
3252284Sobrien  int size;
3352284Sobrien  /* Size in bytes.  */
3452284Sobrien  int bytes;
3552284Sobrien  /* The elements.  */
3652284Sobrien  SBITMAP_ELT_TYPE elms[1];
3752284Sobrien} *sbitmap;
3852284Sobrien
3952284Sobrientypedef SBITMAP_ELT_TYPE *sbitmap_ptr;
4052284Sobrien
4152284Sobrien/* Return the set size needed for N elements.  */
4252284Sobrien#define SBITMAP_SET_SIZE(n) (((n) + SBITMAP_ELT_BITS - 1) / SBITMAP_ELT_BITS)
4352284Sobrien
4452284Sobrien/* set bit number bitno in the bitmap */
4552284Sobrien#define SET_BIT(bitmap, bitno)					\
4652284Sobrien  ((bitmap)->elms [(bitno) / SBITMAP_ELT_BITS]			\
4752284Sobrien   |= (SBITMAP_ELT_TYPE) 1 << (bitno) % SBITMAP_ELT_BITS)
4852284Sobrien
4952284Sobrien/* test if bit number bitno in the bitmap is set */
5052284Sobrien#define TEST_BIT(bitmap, bitno) \
5152284Sobrien((bitmap)->elms [(bitno) / SBITMAP_ELT_BITS] >> (bitno) % SBITMAP_ELT_BITS & 1)
5252284Sobrien
5352284Sobrien/* reset bit number bitno in the bitmap  */
5452284Sobrien#define RESET_BIT(bitmap, bitno)				\
5552284Sobrien  ((bitmap)->elms [(bitno) / SBITMAP_ELT_BITS]			\
5652284Sobrien   &= ~((SBITMAP_ELT_TYPE) 1 << (bitno) % SBITMAP_ELT_BITS))
5752284Sobrien
5852284Sobrien/* Loop over all elements of SBITSET, starting with MIN.  */
5952284Sobrien#define EXECUTE_IF_SET_IN_SBITMAP(SBITMAP, MIN, N, CODE)		\
6052284Sobriendo {									\
6152284Sobrien  unsigned int bit_num_ = (MIN) % (unsigned) SBITMAP_ELT_BITS;		\
6252284Sobrien  unsigned int word_num_ = (MIN) / (unsigned) SBITMAP_ELT_BITS;		\
6352284Sobrien  unsigned int size_ = (SBITMAP)->size;					\
6452284Sobrien  SBITMAP_ELT_TYPE *ptr_ = (SBITMAP)->elms;				\
6552284Sobrien									\
6652284Sobrien  while (word_num_ < size_)						\
6752284Sobrien    {									\
6852284Sobrien      SBITMAP_ELT_TYPE word_ = ptr_[word_num_];				\
6952284Sobrien      if (word_ != 0)							\
7052284Sobrien	{								\
7152284Sobrien	  for (; bit_num_ < SBITMAP_ELT_BITS; ++bit_num_)		\
7252284Sobrien	    {								\
7352284Sobrien	      SBITMAP_ELT_TYPE mask_ = (SBITMAP_ELT_TYPE)1 << bit_num_;	\
7452284Sobrien	      if ((word_ & mask_) != 0)					\
7552284Sobrien		{							\
7652284Sobrien		  word_ &= ~mask_;					\
7752284Sobrien		  (N) = word_num_ * SBITMAP_ELT_BITS + bit_num_;	\
7852284Sobrien		  CODE;							\
7952284Sobrien		  if (word_ == 0)					\
8052284Sobrien		    break;						\
8152284Sobrien		}							\
8252284Sobrien	    }								\
8352284Sobrien	}								\
8452284Sobrien      bit_num_ = 0;							\
8552284Sobrien      word_num_++;							\
8652284Sobrien   }									\
8752284Sobrien} while (0)
8852284Sobrien
8952284Sobrien#define sbitmap_free(map)		free(map)
9052284Sobrien#define sbitmap_vector_free(vec)	free(vec)
9152284Sobrien
9252284Sobrienextern void dump_sbitmap PROTO ((FILE *, sbitmap));
9352284Sobrienextern void dump_sbitmap_vector PROTO ((FILE *, const char *, const char *,
9452284Sobrien					sbitmap *, int));
9552284Sobrien
9652284Sobrienextern sbitmap sbitmap_alloc PROTO ((int));
9752284Sobrienextern sbitmap *sbitmap_vector_alloc PROTO ((int, int));
9852284Sobrien
9952284Sobrienextern void sbitmap_copy PROTO ((sbitmap, sbitmap));
10052284Sobrienextern void sbitmap_zero PROTO ((sbitmap));
10152284Sobrienextern void sbitmap_ones PROTO ((sbitmap));
10252284Sobrienextern void sbitmap_vector_zero PROTO ((sbitmap *, int));
10352284Sobrienextern void sbitmap_vector_ones PROTO ((sbitmap *, int));
10452284Sobrien
10552284Sobrienextern int sbitmap_union_of_diff PROTO ((sbitmap, sbitmap, sbitmap, sbitmap));
10652284Sobrienextern void sbitmap_difference PROTO ((sbitmap, sbitmap, sbitmap));
10752284Sobrienextern void sbitmap_not PROTO ((sbitmap, sbitmap));
10852284Sobrienextern int sbitmap_a_or_b_and_c PROTO ((sbitmap, sbitmap, sbitmap, sbitmap));
10952284Sobrienextern int sbitmap_a_and_b_or_c PROTO ((sbitmap, sbitmap, sbitmap, sbitmap));
11052284Sobrienextern int sbitmap_a_and_b PROTO ((sbitmap, sbitmap, sbitmap));
11152284Sobrienextern int sbitmap_a_or_b PROTO ((sbitmap, sbitmap, sbitmap));
11252284Sobrien
11352284Sobrienstruct int_list;
11452284Sobrienextern void sbitmap_intersect_of_predsucc PROTO ((sbitmap, sbitmap *,
11552284Sobrien						  int, struct int_list **));
11652284Sobrien#define sbitmap_intersect_of_predecessors  sbitmap_intersect_of_predsucc
11752284Sobrien#define sbitmap_intersect_of_successors    sbitmap_intersect_of_predsucc
11852284Sobrien
11952284Sobrienextern void sbitmap_union_of_predsucc PROTO ((sbitmap, sbitmap *, int,
12052284Sobrien					      struct int_list **));
12152284Sobrien#define sbitmap_union_of_predecessors  sbitmap_union_of_predsucc
12252284Sobrien#define sbitmap_union_of_successors    sbitmap_union_of_predsucc
123