Loads do not remove any nodes from the exec after sets. Also fix a 'node leak'.
[libfirm] / ir / adt / bitset.h
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   A bitset implementation.
23  * @author  Sebastian Hack
24  * @date    15.10.2004
25  * @version $Id$
26  */
27 #ifndef FIRM_ADT_BITSET_H
28 #define FIRM_ADT_BITSET_H
29
30 #include "firm_config.h"
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <assert.h>
35 #include <string.h>
36
37 #include "xmalloc.h"
38 #include "bitfiddle.h"
39
40 typedef unsigned int bitset_pos_t;
41
42 #include "bitset_std.h"
43
44 /*
45 #if defined(__GNUC__) && defined(__i386__)
46 #include "bitset_ia32.h"
47 #endif
48 */
49
50 typedef struct _bitset_t {
51         bitset_pos_t units;
52         bitset_pos_t size;
53         bitset_unit_t *data;
54 } bitset_t;
55
56 #define BS_UNIT_SIZE sizeof(bitset_unit_t)
57 #define BS_UNIT_SIZE_BITS (BS_UNIT_SIZE * 8)
58 #define BS_UNIT_MASK (BS_UNIT_SIZE_BITS - 1)
59
60 /**
61  * Initialize a bitset.
62  * This functions should not be called.
63  *
64  * Note that this function needs three macros which must be provided by the
65  * bitfield implementor:
66  * - _bitset_overall_size(size) The overall size that must be
67  *   allocated for the bitfield in bytes.
68  * - _bitset_units(size) The number of units that will be
69  *   present in the bitfield for a given highest bit.
70  * - _bitset_data_ptr(data, size) This produces as pointer to the
71  *   first unit in the allocated memory area. The main reason for this
72  *   macro is, that some bitset implementors want control over memory
73  *   alignment.
74  *
75  * @param area A pointer to memory reserved for the bitset.
76  * @param size The size of the bitset in bits.
77  * @return A pointer to the initialized bitset.
78  */
79 static INLINE bitset_t *_bitset_prepare(void *area, bitset_pos_t size)
80 {
81         bitset_t *ptr = area;
82         memset(area, 0, _bitset_overall_size(sizeof(bitset_t), size));
83         ptr->units = _bitset_units(size);
84         ptr->size = size;
85         ptr->data = _bitset_data_ptr(area, sizeof(bitset_t), size);
86         return ptr;
87 }
88
89 /**
90  * Mask out all bits, which are only there, because the number
91  * of bits in the set didn't match a unit size boundary.
92  * @param bs The bitset.
93  * @return The masked bitset.
94  */
95 static INLINE bitset_t *_bitset_mask_highest(bitset_t *bs)
96 {
97         bitset_pos_t rest = bs->size & BS_UNIT_MASK;
98         if (rest)
99                 bs->data[bs->units - 1] &= (1 << rest) - 1;
100         return bs;
101 }
102
103 /**
104  * Get the capacity of the bitset in bits.
105  * @param bs The bitset.
106  * @return The capacity in bits of the bitset.
107  */
108 #define bitset_capacity(bs) ((bs)->units * BS_UNIT_SIZE_BITS)
109
110 /**
111  * Get the size of the bitset in bits.
112  * @note Note the difference between capacity and size.
113  * @param bs The bitset.
114  * @return The highest bit which can be set or cleared plus 1.
115  */
116 #define bitset_size(bs)  ((bs)->size)
117
118 /**
119  * Allocate a bitset on an obstack.
120  * @param obst The obstack.
121  * @param size The greatest bit that shall be stored in the set.
122  * @return A pointer to an empty initialized bitset.
123  */
124 #define bitset_obstack_alloc(obst,size) \
125         _bitset_prepare(obstack_alloc(obst, _bitset_overall_size(sizeof(bitset_t), size)), size)
126
127 /**
128  * Allocate a bitset via malloc.
129  * @param size The greatest bit that shall be stored in the set.
130  * @return A pointer to an empty initialized bitset.
131  */
132 #define bitset_malloc(size) \
133         _bitset_prepare(xmalloc(_bitset_overall_size(sizeof(bitset_t), size)), size)
134
135 /**
136  * Free a bitset allocated with bitset_malloc().
137  * @param bs The bitset.
138  */
139 #define bitset_free(bs) free(bs)
140
141 /**
142  * Allocate a bitset on the stack via alloca.
143  * @param size The greatest bit that shall be stored in the set.
144  * @return A pointer to an empty initialized bitset.
145  */
146 #define bitset_alloca(size) \
147         _bitset_prepare(alloca(_bitset_overall_size(sizeof(bitset_t), size)), size)
148
149
150 /**
151  * Get the unit which contains a specific bit.
152  * This function is internal.
153  * @param bs The bitset.
154  * @param bit The bit.
155  * @return A pointer to the unit containing the bit.
156  */
157 static INLINE bitset_unit_t *_bitset_get_unit(const bitset_t *bs, bitset_pos_t bit)
158 {
159         /* assert(bit < bs->units * BS_UNIT_SIZE_BITS && "Bit too large"); */
160         assert(bit <= bs->size && "Bit to large");
161         return bs->data + bit / BS_UNIT_SIZE_BITS;
162 }
163
164 /**
165  * Set a bit in the bitset.
166  * @param bs The bitset.
167  * @param bit The bit to set.
168  */
169 static INLINE void bitset_set(bitset_t *bs, bitset_pos_t bit)
170 {
171         bitset_unit_t *unit = _bitset_get_unit(bs, bit);
172         _bitset_inside_set(unit, bit & BS_UNIT_MASK);
173 }
174
175 /**
176  * Clear a bit in the bitset.
177  * @param bs The bitset.
178  * @param bit The bit to clear.
179  */
180 static INLINE void bitset_clear(bitset_t *bs, bitset_pos_t bit)
181 {
182         bitset_unit_t *unit = _bitset_get_unit(bs, bit);
183         _bitset_inside_clear(unit, bit & BS_UNIT_MASK);
184 }
185
186 /**
187  * Check, if a bit is set.
188  * @param bs The bitset.
189  * @param bit The bit to check for.
190  * @return 1, if the bit was set, 0 if not.
191  */
192 static INLINE int bitset_is_set(const bitset_t *bs, bitset_pos_t bit)
193 {
194         bitset_unit_t *unit = _bitset_get_unit(bs, bit);
195         return _bitset_inside_is_set(unit, bit & BS_UNIT_MASK);
196 }
197
198 /**
199  * Flip a bit in a bitset.
200  * @param bs The bitset.
201  * @param bit The bit to flip.
202  */
203 static INLINE void bitset_flip(bitset_t *bs, bitset_pos_t bit)
204 {
205         bitset_unit_t *unit = _bitset_get_unit(bs, bit);
206         _bitset_inside_flip(unit, bit & BS_UNIT_MASK);
207 }
208
209 /**
210  * Flip the whole bitset.
211  * @param bs The bitset.
212  */
213 static INLINE void bitset_flip_all(bitset_t *bs)
214 {
215         bitset_pos_t i;
216         for(i = 0; i < bs->units; i++)
217                 _bitset_inside_flip_unit(&bs->data[i]);
218         _bitset_mask_highest(bs);
219 }
220
221 /**
222  * Copy a bitset to another.
223  * @param tgt The target bitset.
224  * @param src The source bitset.
225  * @return The target bitset.
226  */
227 static INLINE bitset_t *bitset_copy(bitset_t *tgt, const bitset_t *src)
228 {
229         bitset_pos_t tu = tgt->units;
230         bitset_pos_t su = src->units;
231         bitset_pos_t min_units = tu < su ? tu : su;
232         memcpy(tgt->data, src->data, min_units * BS_UNIT_SIZE);
233         if(tu > min_units)
234                 memset(tgt->data + min_units, 0, BS_UNIT_SIZE * (tu - min_units));
235         return _bitset_mask_highest(tgt);
236 }
237
238 /**
239  * Find the smallest bit set in the bitset.
240  * @param bs The bitset.
241  * @return The smallest bit set in the bitset.
242  */
243 static INLINE bitset_pos_t bitset_min(const bitset_t *bs)
244 {
245         bitset_pos_t i, ofs = 0;
246
247         for(i = 0; i < bs->units; ++i) {
248                 bitset_unit_t *unit = &bs->data[i];
249                 bitset_pos_t pos = _bitset_inside_ntz(unit);
250                 if(pos > 0)
251                         return ofs + pos;
252                 ofs += BS_UNIT_SIZE_BITS;
253         }
254
255         return 0;
256 }
257
258 /**
259  * Find the greatest bit set in the bitset.
260  * @param bs The bitset.
261  * @return The greatest bit set in the bitset.
262  */
263 static INLINE bitset_pos_t bitset_max(const bitset_t *bs)
264 {
265         bitset_pos_t i, max = 0, ofs = 0;
266
267         for(i = 0; i < bs->units; ++i) {
268                 bitset_unit_t *unit = &bs->data[i];
269                 bitset_pos_t pos = _bitset_inside_nlz(unit);
270                 if(pos > 0)
271                         max = ofs + pos;
272                 ofs += BS_UNIT_SIZE_BITS;
273         }
274
275         return max;
276 }
277
278 /**
279  * Find the next set bit from a given bit.
280  * @note Note that if pos is set, pos is returned.
281  * @param bs The bitset.
282  * @param pos The bit from which to search for the next set bit.
283  * @return The next set bit from pos on, or -1, if no set bit was found
284  * after pos.
285  */
286 static INLINE bitset_pos_t _bitset_next(const bitset_t *bs,
287                 bitset_pos_t pos, int set)
288 {
289         bitset_pos_t unit_number = pos / BS_UNIT_SIZE_BITS;
290         bitset_pos_t res;
291
292         if(pos >= bs->size)
293                 return -1;
294
295         {
296                 bitset_pos_t bit_in_unit = pos & BS_UNIT_MASK;
297                 bitset_pos_t in_unit_mask = (1 << bit_in_unit) - 1;
298
299                 /*
300                  * Mask out the bits smaller than pos in the current unit.
301                  * We are only interested in bits set higher than pos.
302                  */
303                 bitset_unit_t curr_unit = bs->data[unit_number];
304
305                 /*
306                  * Find the next bit set in the unit.
307                  * Mind that this function returns 0, if the unit is -1 and
308                  * counts the bits from 1 on.
309                  */
310                 bitset_pos_t next_in_this_unit =
311                         _bitset_inside_ntz_value((set ? curr_unit : ~curr_unit) & ~in_unit_mask);
312
313                 /* If there is a bit set in the current unit, exit. */
314                 if (next_in_this_unit < BS_UNIT_SIZE_BITS) {
315                         res = next_in_this_unit + unit_number * BS_UNIT_SIZE_BITS;
316                         return res < bs->size ? res : -1;
317                 }
318
319                 /* Else search for set bits in the next units. */
320                 else {
321                         bitset_pos_t i;
322                         for(i = unit_number + 1; i < bs->units; ++i) {
323                                 bitset_unit_t data = bs->data[i];
324                                 bitset_pos_t first_set =
325                                         _bitset_inside_ntz_value(set ? data : ~data);
326
327                                 if (first_set < BS_UNIT_SIZE_BITS) {
328                                         res = first_set + i * BS_UNIT_SIZE_BITS;
329                                         return res < bs->size ? res : -1;
330                                 }
331                         }
332                 }
333         }
334
335         return -1;
336 }
337
338 #define bitset_next_clear(bs,pos) _bitset_next((bs), (pos), 0)
339 #define bitset_next_set(bs,pos) _bitset_next((bs), (pos), 1)
340
341 /**
342  * Convenience macro for bitset iteration.
343  * @param bitset The bitset.
344  * @param elm A unsigned long variable.
345  */
346 #define bitset_foreach(bitset,elm) \
347         for(elm = bitset_next_set(bitset,0); elm != -1; elm = bitset_next_set(bitset,elm+1))
348
349
350 #define bitset_foreach_clear(bitset,elm) \
351         for(elm = bitset_next_clear(bitset,0); elm != -1; elm = bitset_next_clear(bitset,elm+1))
352
353 /**
354  * Count the bits set.
355  * This can also be seen as the cardinality of the set.
356  * @param bs The bitset.
357  * @return The number of bits set in the bitset.
358  */
359 static INLINE unsigned bitset_popcnt(const bitset_t *bs)
360 {
361         bitset_pos_t  i;
362         bitset_unit_t *unit;
363         unsigned      pop = 0;
364
365         for (i = 0, unit = bs->data; i < bs->units; ++i, ++unit)
366                 pop += _bitset_inside_pop(unit);
367
368         return pop;
369 }
370
371 /**
372  * Clear the bitset.
373  * This sets all bits to zero.
374  * @param bs The bitset.
375  */
376 static INLINE bitset_t *bitset_clear_all(bitset_t *bs)
377 {
378         memset(bs->data, 0, BS_UNIT_SIZE * bs->units);
379         return bs;
380 }
381
382 /**
383  * Set the bitset.
384  * This sets all bits to one.
385  * @param bs The bitset.
386  */
387 static INLINE bitset_t *bitset_set_all(bitset_t *bs)
388 {
389         memset(bs->data, -1, bs->units * BS_UNIT_SIZE);
390         return _bitset_mask_highest(bs);
391 }
392
393 /**
394  * Check, if one bitset is contained by another.
395  * That is, each bit set in lhs is also set in rhs.
396  * @param lhs A bitset.
397  * @param rhs Another bitset.
398  * @return 1, if all bits in lhs are also set in rhs, 0 otherwise.
399  */
400 static INLINE int bitset_contains(const bitset_t *lhs, const bitset_t *rhs)
401 {
402         bitset_pos_t n = lhs->units < rhs->units ? lhs->units : rhs->units;
403         bitset_pos_t i;
404
405         for(i = 0; i < n; ++i) {
406                 bitset_unit_t lu = lhs->data[i];
407                 bitset_unit_t ru = rhs->data[i];
408
409                 if((lu | ru) & ~ru)
410                         return 0;
411         }
412
413         /*
414          * If the left hand sinde is a larger bitset than rhs,
415          * we have to check, that all extra bits in lhs are 0
416          */
417         if(lhs->units > n) {
418                 for(i = n; i < lhs->units; ++i) {
419                         if(lhs->data[i] != 0)
420                                 return 0;
421                 }
422         }
423
424         return 1;
425 }
426
427 /**
428  * Treat the bitset as a number and subtract 1.
429  * @param bs The bitset.
430  * @return The same bitset.
431  */
432 static INLINE void bitset_minus1(bitset_t *bs)
433 {
434 #define _SH (sizeof(bitset_unit_t) * 8 - 1)
435
436         bitset_pos_t i;
437
438         for(i = 0; i < bs->units; ++i) {
439                 bitset_unit_t unit = bs->data[i];
440                 bitset_unit_t um1  = unit - 1;
441
442                 bs->data[i] = um1;
443
444                 if(((unit >> _SH) ^ (um1 >> _SH)) == 0)
445                         break;
446         }
447 #undef _SH
448 }
449
450 /**
451  * Check if two bitsets intersect.
452  * @param a The first bitset.
453  * @param b The second bitset.
454  * @return 1 if they have a bit in common, 0 if not.
455  */
456 static INLINE int bitset_intersect(const bitset_t *a, const bitset_t *b)
457 {
458         bitset_pos_t n = a->units < b->units ? a->units : b->units;
459         bitset_pos_t i;
460
461         for (i = 0; i < n; ++i)
462                 if (a->data[i] & b->data[i])
463                         return 1;
464
465         return 0;
466 }
467
468 /**
469  * Check, if a bitset is empty.
470  * @param a The bitset.
471  * @return 1, if the bitset is empty, 0 if not.
472  */
473 static INLINE int bitset_is_empty(const bitset_t *a)
474 {
475         bitset_pos_t i;
476         for (i = 0; i < a->units; ++i)
477                 if (a->data[i] != 0)
478                         return 0;
479         return 1;
480 }
481
482 /**
483  * Print a bitset to a stream.
484  * The bitset is printed as a comma separated list of bits set.
485  * @param file The stream.
486  * @param bs The bitset.
487  */
488 static INLINE void bitset_fprint(FILE *file, const bitset_t *bs)
489 {
490         const char *prefix = "";
491         int i;
492
493         putc('{', file);
494         for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
495                 fprintf(file, "%s%u", prefix, i);
496                 prefix = ",";
497         }
498         putc('}', file);
499 }
500
501 static INLINE void bitset_debug_fprint(FILE *file, const bitset_t *bs)
502 {
503         bitset_pos_t i;
504
505         fprintf(file, "%u:", bs->units);
506         for(i = 0; i < bs->units; ++i)
507                 fprintf(file, " " BITSET_UNIT_FMT, bs->data[i]);
508 }
509
510 /**
511  * Perform tgt = tgt \ src operation.
512  * @param tgt  The target bitset.
513  * @param src  The source bitset.
514  * @return the tgt set.
515  */
516 static INLINE bitset_t *bitset_andnot(bitset_t *tgt, const bitset_t *src);
517
518 /**
519  * Perform Union, tgt = tgt u src operation.
520  * @param tgt  The target bitset.
521  * @param src  The source bitset.
522  * @return the tgt set.
523  */
524 static INLINE bitset_t *bitset_or(bitset_t *tgt, const bitset_t *src);
525
526 /**
527  * Perform tgt = tgt ^ ~src operation.
528  * @param tgt  The target bitset.
529  * @param src  The source bitset.
530  * @return the tgt set.
531  */
532 static INLINE bitset_t *bitset_xor(bitset_t *tgt, const bitset_t *src);
533
534 /*
535  * Here, the binary operations follow.
536  * And, Or, And Not, Xor are available.
537  */
538 #define BINARY_OP(op) \
539 static INLINE bitset_t *bitset_ ## op(bitset_t *tgt, const bitset_t *src) \
540 { \
541         bitset_pos_t i; \
542         bitset_pos_t n = tgt->units > src->units ? src->units : tgt->units; \
543         for(i = 0; i < n; i += _BITSET_BINOP_UNITS_INC) \
544                 _bitset_inside_binop_ ## op(&tgt->data[i], &src->data[i]); \
545         if(n < tgt->units) \
546                 _bitset_clear_rest(&tgt->data[i], tgt->units - i); \
547         return _bitset_mask_highest(tgt); \
548 }
549
550 /*
551  * Define the clear rest macro for the and, since it is the only case,
552  * were non existed (treated as 0) units in the src must be handled.
553  * For all other operations holds: x Op 0 = x for Op in { Andnot, Or, Xor }
554  *
555  * For and, each bitset implementer has to provide the macro
556  * _bitset_clear_units(data, n), which clears n units from the pointer
557  * data on.
558  */
559 #define _bitset_clear_rest(data,n) _bitset_inside_clear_units(data, n)
560 BINARY_OP(and)
561 #undef _bitset_clear_rest
562 #define _bitset_clear_rest(data,n)
563
564 BINARY_OP(andnot)
565 BINARY_OP(or)
566 BINARY_OP(xor)
567
568 #endif