]>
Commit | Line | Data |
---|---|---|
096ab9ea | 1 | /* Functions to support general ended bitmaps. |
ae0ed63a | 2 | Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc. |
096ab9ea | 3 | |
1322177d | 4 | This file is part of GCC. |
096ab9ea | 5 | |
1322177d LB |
6 | GCC is free software; you can redistribute it and/or modify it under |
7 | the terms of the GNU General Public License as published by the Free | |
8 | Software Foundation; either version 2, or (at your option) any later | |
9 | version. | |
096ab9ea | 10 | |
1322177d LB |
11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
096ab9ea RK |
15 | |
16 | You should have received a copy of the GNU General Public License | |
1322177d LB |
17 | along with GCC; see the file COPYING. If not, write to the Free |
18 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
19 | 02111-1307, USA. */ | |
096ab9ea | 20 | |
096ab9ea | 21 | #include "config.h" |
670ee920 | 22 | #include "system.h" |
096ab9ea RK |
23 | #include "rtl.h" |
24 | #include "flags.h" | |
25 | #include "obstack.h" | |
efc9bd41 | 26 | #include "bitmap.h" |
096ab9ea | 27 | |
22fa5b8a MM |
28 | /* Obstack to allocate bitmap elements from. */ |
29 | static struct obstack bitmap_obstack; | |
30 | static int bitmap_obstack_init = FALSE; | |
096ab9ea RK |
31 | \f |
32 | #ifndef INLINE | |
33 | #ifndef __GNUC__ | |
34 | #define INLINE | |
35 | #else | |
36 | #define INLINE __inline__ | |
37 | #endif | |
38 | #endif | |
39 | ||
40 | /* Global data */ | |
ec5c56db KH |
41 | bitmap_element bitmap_zero_bits; /* An element of all zero bits. */ |
42 | static bitmap_element *bitmap_free; /* Freelist of bitmap elements. */ | |
096ab9ea | 43 | |
3d994c6b KG |
44 | static void bitmap_element_free PARAMS ((bitmap, bitmap_element *)); |
45 | static bitmap_element *bitmap_element_allocate PARAMS ((void)); | |
46 | static int bitmap_element_zerop PARAMS ((bitmap_element *)); | |
47 | static void bitmap_element_link PARAMS ((bitmap, bitmap_element *)); | |
48 | static bitmap_element *bitmap_find_bit PARAMS ((bitmap, unsigned int)); | |
096ab9ea | 49 | \f |
a615c28a RH |
50 | /* Free a bitmap element. Since these are allocated off the |
51 | bitmap_obstack, "free" actually means "put onto the freelist". */ | |
096ab9ea RK |
52 | |
53 | static INLINE void | |
54 | bitmap_element_free (head, elt) | |
55 | bitmap head; | |
56 | bitmap_element *elt; | |
57 | { | |
58 | bitmap_element *next = elt->next; | |
59 | bitmap_element *prev = elt->prev; | |
60 | ||
61 | if (prev) | |
62 | prev->next = next; | |
63 | ||
64 | if (next) | |
65 | next->prev = prev; | |
66 | ||
67 | if (head->first == elt) | |
68 | head->first = next; | |
69 | ||
70 | /* Since the first thing we try is to insert before current, | |
71 | make current the next entry in preference to the previous. */ | |
72 | if (head->current == elt) | |
73 | head->current = next != 0 ? next : prev; | |
74 | ||
75 | elt->next = bitmap_free; | |
76 | bitmap_free = elt; | |
77 | } | |
78 | \f | |
79 | /* Allocate a bitmap element. The bits are cleared, but nothing else is. */ | |
80 | ||
81 | static INLINE bitmap_element * | |
56c5d8bf | 82 | bitmap_element_allocate () |
096ab9ea RK |
83 | { |
84 | bitmap_element *element; | |
096ab9ea RK |
85 | |
86 | if (bitmap_free != 0) | |
87 | { | |
88 | element = bitmap_free; | |
89 | bitmap_free = element->next; | |
90 | } | |
91 | else | |
22fa5b8a MM |
92 | { |
93 | /* We can't use gcc_obstack_init to initialize the obstack since | |
94 | print-rtl.c now calls bitmap functions, and bitmap is linked | |
95 | into the gen* functions. */ | |
96 | if (!bitmap_obstack_init) | |
97 | { | |
98 | bitmap_obstack_init = TRUE; | |
99 | ||
100 | /* Let particular systems override the size of a chunk. */ | |
101 | #ifndef OBSTACK_CHUNK_SIZE | |
102 | #define OBSTACK_CHUNK_SIZE 0 | |
103 | #endif | |
104 | /* Let them override the alloc and free routines too. */ | |
105 | #ifndef OBSTACK_CHUNK_ALLOC | |
106 | #define OBSTACK_CHUNK_ALLOC xmalloc | |
107 | #endif | |
108 | #ifndef OBSTACK_CHUNK_FREE | |
109 | #define OBSTACK_CHUNK_FREE free | |
110 | #endif | |
111 | ||
112 | #if !defined(__GNUC__) || (__GNUC__ < 2) | |
113 | #define __alignof__(type) 0 | |
114 | #endif | |
115 | ||
116 | obstack_specify_allocation (&bitmap_obstack, OBSTACK_CHUNK_SIZE, | |
117 | __alignof__ (bitmap_element), | |
93868d11 KG |
118 | (void *(*) PARAMS ((long))) OBSTACK_CHUNK_ALLOC, |
119 | (void (*) PARAMS ((void *))) OBSTACK_CHUNK_FREE); | |
22fa5b8a MM |
120 | } |
121 | ||
122 | element = (bitmap_element *) obstack_alloc (&bitmap_obstack, | |
123 | sizeof (bitmap_element)); | |
124 | } | |
096ab9ea | 125 | |
a615c28a | 126 | memset (element->bits, 0, sizeof (element->bits)); |
096ab9ea RK |
127 | |
128 | return element; | |
129 | } | |
130 | ||
87e08c69 RH |
131 | /* Release any memory allocated by bitmaps. */ |
132 | ||
133 | void | |
134 | bitmap_release_memory () | |
135 | { | |
136 | bitmap_free = 0; | |
137 | if (bitmap_obstack_init) | |
138 | { | |
139 | bitmap_obstack_init = FALSE; | |
140 | obstack_free (&bitmap_obstack, NULL); | |
141 | } | |
142 | } | |
143 | ||
096ab9ea RK |
144 | /* Return nonzero if all bits in an element are zero. */ |
145 | ||
146 | static INLINE int | |
147 | bitmap_element_zerop (element) | |
148 | bitmap_element *element; | |
149 | { | |
150 | #if BITMAP_ELEMENT_WORDS == 2 | |
151 | return (element->bits[0] | element->bits[1]) == 0; | |
152 | #else | |
153 | int i; | |
154 | ||
155 | for (i = 0; i < BITMAP_ELEMENT_WORDS; i++) | |
156 | if (element->bits[i] != 0) | |
157 | return 0; | |
158 | ||
159 | return 1; | |
160 | #endif | |
161 | } | |
162 | \f | |
163 | /* Link the bitmap element into the current bitmap linked list. */ | |
164 | ||
165 | static INLINE void | |
166 | bitmap_element_link (head, element) | |
167 | bitmap head; | |
168 | bitmap_element *element; | |
169 | { | |
170 | unsigned int indx = element->indx; | |
171 | bitmap_element *ptr; | |
172 | ||
173 | /* If this is the first and only element, set it in. */ | |
174 | if (head->first == 0) | |
175 | { | |
176 | element->next = element->prev = 0; | |
177 | head->first = element; | |
178 | } | |
179 | ||
180 | /* If this index is less than that of the current element, it goes someplace | |
181 | before the current element. */ | |
182 | else if (indx < head->indx) | |
183 | { | |
184 | for (ptr = head->current; | |
185 | ptr->prev != 0 && ptr->prev->indx > indx; | |
186 | ptr = ptr->prev) | |
187 | ; | |
188 | ||
189 | if (ptr->prev) | |
190 | ptr->prev->next = element; | |
191 | else | |
192 | head->first = element; | |
193 | ||
194 | element->prev = ptr->prev; | |
195 | element->next = ptr; | |
196 | ptr->prev = element; | |
197 | } | |
198 | ||
199 | /* Otherwise, it must go someplace after the current element. */ | |
200 | else | |
201 | { | |
202 | for (ptr = head->current; | |
203 | ptr->next != 0 && ptr->next->indx < indx; | |
204 | ptr = ptr->next) | |
205 | ; | |
206 | ||
207 | if (ptr->next) | |
208 | ptr->next->prev = element; | |
209 | ||
210 | element->next = ptr->next; | |
211 | element->prev = ptr; | |
212 | ptr->next = element; | |
213 | } | |
214 | ||
215 | /* Set up so this is the first element searched. */ | |
216 | head->current = element; | |
217 | head->indx = indx; | |
218 | } | |
219 | \f | |
220 | /* Clear a bitmap by freeing the linked list. */ | |
221 | ||
1d300e19 | 222 | INLINE void |
096ab9ea RK |
223 | bitmap_clear (head) |
224 | bitmap head; | |
225 | { | |
226 | bitmap_element *element, *next; | |
227 | ||
228 | for (element = head->first; element != 0; element = next) | |
229 | { | |
230 | next = element->next; | |
231 | element->next = bitmap_free; | |
232 | bitmap_free = element; | |
233 | } | |
234 | ||
235 | head->first = head->current = 0; | |
236 | } | |
237 | \f | |
a615c28a | 238 | /* Copy a bitmap to another bitmap. */ |
096ab9ea RK |
239 | |
240 | void | |
241 | bitmap_copy (to, from) | |
242 | bitmap to; | |
243 | bitmap from; | |
244 | { | |
245 | bitmap_element *from_ptr, *to_ptr = 0; | |
29a82058 | 246 | #if BITMAP_ELEMENT_WORDS != 2 |
096ab9ea | 247 | int i; |
29a82058 | 248 | #endif |
096ab9ea RK |
249 | |
250 | bitmap_clear (to); | |
251 | ||
252 | /* Copy elements in forward direction one at a time */ | |
253 | for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next) | |
254 | { | |
56c5d8bf | 255 | bitmap_element *to_elt = bitmap_element_allocate (); |
096ab9ea RK |
256 | |
257 | to_elt->indx = from_ptr->indx; | |
258 | ||
259 | #if BITMAP_ELEMENT_WORDS == 2 | |
260 | to_elt->bits[0] = from_ptr->bits[0]; | |
261 | to_elt->bits[1] = from_ptr->bits[1]; | |
262 | #else | |
263 | for (i = 0; i < BITMAP_ELEMENT_WORDS; i++) | |
264 | to_elt->bits[i] = from_ptr->bits[i]; | |
265 | #endif | |
266 | ||
267 | /* Here we have a special case of bitmap_element_link, for the case | |
268 | where we know the links are being entered in sequence. */ | |
269 | if (to_ptr == 0) | |
270 | { | |
271 | to->first = to->current = to_elt; | |
272 | to->indx = from_ptr->indx; | |
273 | to_elt->next = to_elt->prev = 0; | |
274 | } | |
275 | else | |
276 | { | |
277 | to_elt->prev = to_ptr; | |
278 | to_elt->next = 0; | |
279 | to_ptr->next = to_elt; | |
280 | } | |
281 | ||
282 | to_ptr = to_elt; | |
283 | } | |
284 | } | |
285 | \f | |
286 | /* Find a bitmap element that would hold a bitmap's bit. | |
287 | Update the `current' field even if we can't find an element that | |
288 | would hold the bitmap's bit to make eventual allocation | |
289 | faster. */ | |
290 | ||
291 | static INLINE bitmap_element * | |
292 | bitmap_find_bit (head, bit) | |
293 | bitmap head; | |
294 | unsigned int bit; | |
295 | { | |
296 | bitmap_element *element; | |
297 | unsigned HOST_WIDE_INT indx = bit / BITMAP_ELEMENT_ALL_BITS; | |
298 | ||
299 | if (head->current == 0) | |
300 | return 0; | |
301 | ||
302 | if (head->indx > indx) | |
303 | for (element = head->current; | |
304 | element->prev != 0 && element->indx > indx; | |
305 | element = element->prev) | |
306 | ; | |
307 | ||
308 | else | |
309 | for (element = head->current; | |
310 | element->next != 0 && element->indx < indx; | |
311 | element = element->next) | |
312 | ; | |
313 | ||
314 | /* `element' is the nearest to the one we want. If it's not the one we | |
315 | want, the one we want doesn't exist. */ | |
316 | head->current = element; | |
317 | head->indx = element->indx; | |
318 | if (element != 0 && element->indx != indx) | |
319 | element = 0; | |
320 | ||
321 | return element; | |
322 | } | |
323 | \f | |
324 | /* Clear a single bit in a bitmap. */ | |
325 | ||
326 | void | |
327 | bitmap_clear_bit (head, bit) | |
328 | bitmap head; | |
329 | int bit; | |
330 | { | |
331 | bitmap_element *ptr = bitmap_find_bit (head, bit); | |
332 | ||
333 | if (ptr != 0) | |
334 | { | |
335 | unsigned bit_num = bit % (unsigned) HOST_BITS_PER_WIDE_INT; | |
336 | unsigned word_num = ((bit / (unsigned) HOST_BITS_PER_WIDE_INT) | |
337 | % BITMAP_ELEMENT_WORDS); | |
338 | ptr->bits[word_num] &= ~ (((unsigned HOST_WIDE_INT) 1) << bit_num); | |
339 | ||
340 | /* If we cleared the entire word, free up the element */ | |
341 | if (bitmap_element_zerop (ptr)) | |
342 | bitmap_element_free (head, ptr); | |
343 | } | |
344 | } | |
345 | ||
096ab9ea RK |
346 | /* Set a single bit in a bitmap. */ |
347 | ||
348 | void | |
349 | bitmap_set_bit (head, bit) | |
350 | bitmap head; | |
351 | int bit; | |
352 | { | |
353 | bitmap_element *ptr = bitmap_find_bit (head, bit); | |
354 | unsigned word_num | |
355 | = ((bit / (unsigned) HOST_BITS_PER_WIDE_INT) % BITMAP_ELEMENT_WORDS); | |
356 | unsigned bit_num = bit % (unsigned) HOST_BITS_PER_WIDE_INT; | |
357 | unsigned HOST_WIDE_INT bit_val = ((unsigned HOST_WIDE_INT) 1) << bit_num; | |
358 | ||
359 | if (ptr == 0) | |
360 | { | |
56c5d8bf | 361 | ptr = bitmap_element_allocate (); |
096ab9ea RK |
362 | ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS; |
363 | ptr->bits[word_num] = bit_val; | |
364 | bitmap_element_link (head, ptr); | |
365 | } | |
366 | else | |
367 | ptr->bits[word_num] |= bit_val; | |
368 | } | |
a615c28a | 369 | |
096ab9ea RK |
370 | /* Return whether a bit is set within a bitmap. */ |
371 | ||
372 | int | |
373 | bitmap_bit_p (head, bit) | |
374 | bitmap head; | |
375 | int bit; | |
376 | { | |
377 | bitmap_element *ptr; | |
378 | unsigned bit_num; | |
379 | unsigned word_num; | |
380 | ||
381 | ptr = bitmap_find_bit (head, bit); | |
382 | if (ptr == 0) | |
383 | return 0; | |
384 | ||
385 | bit_num = bit % (unsigned) HOST_BITS_PER_WIDE_INT; | |
386 | word_num | |
387 | = ((bit / (unsigned) HOST_BITS_PER_WIDE_INT) % BITMAP_ELEMENT_WORDS); | |
388 | ||
8229306b | 389 | return (ptr->bits[word_num] >> bit_num) & 1; |
096ab9ea RK |
390 | } |
391 | \f | |
a615c28a RH |
392 | /* Return the bit number of the first set bit in the bitmap, or -1 |
393 | if the bitmap is empty. */ | |
87e08c69 RH |
394 | |
395 | int | |
396 | bitmap_first_set_bit (a) | |
397 | bitmap a; | |
398 | { | |
399 | bitmap_element *ptr = a->first; | |
400 | unsigned HOST_WIDE_INT word; | |
401 | unsigned word_num, bit_num; | |
402 | ||
403 | if (ptr == NULL) | |
404 | return -1; | |
405 | ||
406 | #if BITMAP_ELEMENT_WORDS == 2 | |
407 | word_num = 0, word = ptr->bits[0]; | |
408 | if (word == 0) | |
409 | word_num = 1, word = ptr->bits[1]; | |
410 | #else | |
411 | for (word_num = 0; word_num < BITMAP_ELEMENT_WORDS; ++word_num) | |
412 | if ((word = ptr->bits[word_num]) != 0) | |
413 | break; | |
414 | #endif | |
415 | ||
416 | /* Binary search for the first set bit. */ | |
417 | /* ??? It'd be nice to know if ffs or ffsl was available. */ | |
418 | ||
419 | bit_num = 0; | |
420 | word = word & -word; | |
421 | ||
422 | #if HOST_BITS_PER_WIDE_INT > 64 | |
423 | #error "Fill out the table." | |
424 | #endif | |
425 | #if HOST_BITS_PER_WIDE_INT > 32 | |
426 | if ((word & 0xffffffff) == 0) | |
427 | word >>= 32, bit_num += 32; | |
428 | #endif | |
429 | if ((word & 0xffff) == 0) | |
430 | word >>= 16, bit_num += 16; | |
431 | if ((word & 0xff) == 0) | |
432 | word >>= 8, bit_num += 8; | |
433 | if (word & 0xf0) | |
434 | bit_num += 4; | |
435 | if (word & 0xcc) | |
436 | bit_num += 2; | |
437 | if (word & 0xaa) | |
438 | bit_num += 1; | |
439 | ||
440 | return (ptr->indx * BITMAP_ELEMENT_ALL_BITS | |
441 | + word_num * HOST_BITS_PER_WIDE_INT | |
442 | + bit_num); | |
443 | } | |
444 | ||
a615c28a RH |
445 | /* Return the bit number of the last set bit in the bitmap, or -1 |
446 | if the bitmap is empty. */ | |
447 | ||
87e08c69 RH |
448 | int |
449 | bitmap_last_set_bit (a) | |
450 | bitmap a; | |
451 | { | |
452 | bitmap_element *ptr = a->first; | |
453 | unsigned HOST_WIDE_INT word; | |
454 | unsigned word_num, bit_num; | |
455 | ||
456 | if (ptr == NULL) | |
457 | return -1; | |
458 | ||
459 | while (ptr->next != NULL) | |
460 | ptr = ptr->next; | |
461 | ||
462 | #if BITMAP_ELEMENT_WORDS == 2 | |
463 | word_num = 1, word = ptr->bits[1]; | |
464 | if (word == 0) | |
465 | word_num = 0, word = ptr->bits[0]; | |
466 | #else | |
467 | for (word_num = BITMAP_ELEMENT_WORDS; word_num-- > 0; ) | |
468 | if ((word = ptr->bits[word_num]) != 0) | |
469 | break; | |
470 | #endif | |
471 | ||
472 | /* Binary search for the last set bit. */ | |
473 | ||
474 | bit_num = 0; | |
475 | #if HOST_BITS_PER_WIDE_INT > 64 | |
476 | #error "Fill out the table." | |
477 | #endif | |
478 | #if HOST_BITS_PER_WIDE_INT > 32 | |
479 | if (word & ~ (unsigned HOST_WIDE_INT) 0xffffffff) | |
480 | word >>= 32, bit_num += 32; | |
481 | #endif | |
482 | if (word & 0xffff0000) | |
483 | word >>= 16, bit_num += 16; | |
484 | if (word & 0xff00) | |
485 | word >>= 8, bit_num += 8; | |
486 | if (word & 0xf0) | |
487 | word >>= 4, bit_num += 4; | |
488 | if (word & 0xc) | |
489 | word >>= 2, bit_num += 2; | |
490 | if (word & 0x2) | |
a615c28a | 491 | bit_num += 1; |
87e08c69 RH |
492 | |
493 | return (ptr->indx * BITMAP_ELEMENT_ALL_BITS | |
494 | + word_num * HOST_BITS_PER_WIDE_INT | |
495 | + bit_num); | |
496 | } | |
497 | \f | |
8229306b RH |
498 | /* Store in bitmap TO the result of combining bitmap FROM1 and FROM2 using |
499 | a specific bit manipulation. Return true if TO changes. */ | |
096ab9ea | 500 | |
8229306b | 501 | int |
096ab9ea RK |
502 | bitmap_operation (to, from1, from2, operation) |
503 | bitmap to; | |
504 | bitmap from1; | |
505 | bitmap from2; | |
506 | enum bitmap_bits operation; | |
507 | { | |
c8d8ed65 RK |
508 | #define HIGHEST_INDEX (unsigned int) ~0 |
509 | ||
096ab9ea RK |
510 | bitmap_element *from1_ptr = from1->first; |
511 | bitmap_element *from2_ptr = from2->first; | |
c8d8ed65 RK |
512 | unsigned int indx1 = (from1_ptr) ? from1_ptr->indx : HIGHEST_INDEX; |
513 | unsigned int indx2 = (from2_ptr) ? from2_ptr->indx : HIGHEST_INDEX; | |
8229306b | 514 | bitmap_element *to_ptr = to->first; |
096ab9ea RK |
515 | bitmap_element *from1_tmp; |
516 | bitmap_element *from2_tmp; | |
8229306b | 517 | bitmap_element *to_tmp; |
096ab9ea | 518 | unsigned int indx; |
8229306b RH |
519 | int changed = 0; |
520 | ||
521 | #if BITMAP_ELEMENT_WORDS == 2 | |
522 | #define DOIT(OP) \ | |
523 | do { \ | |
524 | unsigned HOST_WIDE_INT t0, t1, f10, f11, f20, f21; \ | |
525 | f10 = from1_tmp->bits[0]; \ | |
526 | f20 = from2_tmp->bits[0]; \ | |
527 | t0 = f10 OP f20; \ | |
528 | changed |= (t0 != to_tmp->bits[0]); \ | |
529 | f11 = from1_tmp->bits[1]; \ | |
530 | f21 = from2_tmp->bits[1]; \ | |
531 | t1 = f11 OP f21; \ | |
532 | changed |= (t1 != to_tmp->bits[1]); \ | |
533 | to_tmp->bits[0] = t0; \ | |
534 | to_tmp->bits[1] = t1; \ | |
535 | } while (0) | |
536 | #else | |
537 | #define DOIT(OP) \ | |
538 | do { \ | |
539 | unsigned HOST_WIDE_INT t, f1, f2; \ | |
540 | int i; \ | |
541 | for (i = 0; i < BITMAP_ELEMENT_WORDS; ++i) \ | |
542 | { \ | |
543 | f1 = from1_tmp->bits[i]; \ | |
544 | f2 = from2_tmp->bits[i]; \ | |
545 | t = f1 OP f2; \ | |
546 | changed |= (t != to_tmp->bits[i]); \ | |
547 | to_tmp->bits[i] = t; \ | |
548 | } \ | |
549 | } while (0) | |
29a82058 | 550 | #endif |
096ab9ea | 551 | |
8229306b | 552 | to->first = to->current = 0; |
096ab9ea RK |
553 | |
554 | while (from1_ptr != 0 || from2_ptr != 0) | |
555 | { | |
556 | /* Figure out whether we need to substitute zero elements for | |
557 | missing links. */ | |
558 | if (indx1 == indx2) | |
559 | { | |
560 | indx = indx1; | |
561 | from1_tmp = from1_ptr; | |
562 | from2_tmp = from2_ptr; | |
563 | from1_ptr = from1_ptr->next; | |
c8d8ed65 | 564 | indx1 = (from1_ptr) ? from1_ptr->indx : HIGHEST_INDEX; |
096ab9ea | 565 | from2_ptr = from2_ptr->next; |
c8d8ed65 | 566 | indx2 = (from2_ptr) ? from2_ptr->indx : HIGHEST_INDEX; |
096ab9ea RK |
567 | } |
568 | else if (indx1 < indx2) | |
569 | { | |
570 | indx = indx1; | |
571 | from1_tmp = from1_ptr; | |
ae0ed63a | 572 | from2_tmp = &bitmap_zero_bits; |
096ab9ea | 573 | from1_ptr = from1_ptr->next; |
c8d8ed65 | 574 | indx1 = (from1_ptr) ? from1_ptr->indx : HIGHEST_INDEX; |
096ab9ea RK |
575 | } |
576 | else | |
577 | { | |
578 | indx = indx2; | |
ae0ed63a | 579 | from1_tmp = &bitmap_zero_bits; |
096ab9ea RK |
580 | from2_tmp = from2_ptr; |
581 | from2_ptr = from2_ptr->next; | |
c8d8ed65 | 582 | indx2 = (from2_ptr) ? from2_ptr->indx : HIGHEST_INDEX; |
096ab9ea RK |
583 | } |
584 | ||
8229306b | 585 | /* Find the appropriate element from TO. Begin by discarding |
ec5c56db | 586 | elements that we've skipped. */ |
8229306b RH |
587 | while (to_ptr && to_ptr->indx < indx) |
588 | { | |
589 | changed = 1; | |
590 | to_tmp = to_ptr; | |
591 | to_ptr = to_ptr->next; | |
b9a73e32 | 592 | to_tmp->next = bitmap_free; |
8229306b RH |
593 | bitmap_free = to_tmp; |
594 | } | |
595 | if (to_ptr && to_ptr->indx == indx) | |
596 | { | |
597 | to_tmp = to_ptr; | |
598 | to_ptr = to_ptr->next; | |
599 | } | |
600 | else | |
601 | to_tmp = bitmap_element_allocate (); | |
096ab9ea RK |
602 | |
603 | /* Do the operation, and if any bits are set, link it into the | |
604 | linked list. */ | |
605 | switch (operation) | |
606 | { | |
607 | default: | |
608 | abort (); | |
609 | ||
610 | case BITMAP_AND: | |
8229306b | 611 | DOIT (&); |
096ab9ea RK |
612 | break; |
613 | ||
614 | case BITMAP_AND_COMPL: | |
8229306b | 615 | DOIT (&~); |
096ab9ea RK |
616 | break; |
617 | ||
618 | case BITMAP_IOR: | |
8229306b RH |
619 | DOIT (|); |
620 | break; | |
ea193996 DB |
621 | case BITMAP_IOR_COMPL: |
622 | DOIT (|~); | |
623 | break; | |
8229306b RH |
624 | case BITMAP_XOR: |
625 | DOIT (^); | |
096ab9ea RK |
626 | break; |
627 | } | |
628 | ||
8229306b | 629 | if (! bitmap_element_zerop (to_tmp)) |
096ab9ea | 630 | { |
8229306b RH |
631 | to_tmp->indx = indx; |
632 | bitmap_element_link (to, to_tmp); | |
b9a73e32 JM |
633 | } |
634 | else | |
635 | { | |
636 | to_tmp->next = bitmap_free; | |
637 | bitmap_free = to_tmp; | |
096ab9ea RK |
638 | } |
639 | } | |
640 | ||
8229306b RH |
641 | /* If we have elements of TO left over, free the lot. */ |
642 | if (to_ptr) | |
096ab9ea | 643 | { |
8229306b RH |
644 | changed = 1; |
645 | for (to_tmp = to_ptr; to_tmp->next ; to_tmp = to_tmp->next) | |
646 | continue; | |
647 | to_tmp->next = bitmap_free; | |
096ab9ea RK |
648 | bitmap_free = to_ptr; |
649 | } | |
650 | ||
8229306b RH |
651 | #undef DOIT |
652 | ||
653 | return changed; | |
654 | } | |
655 | ||
656 | /* Return true if two bitmaps are identical. */ | |
657 | ||
658 | int | |
659 | bitmap_equal_p (a, b) | |
660 | bitmap a; | |
661 | bitmap b; | |
662 | { | |
663 | bitmap_head c; | |
664 | int ret; | |
665 | ||
666 | c.first = c.current = 0; | |
667 | ret = ! bitmap_operation (&c, a, b, BITMAP_XOR); | |
668 | bitmap_clear (&c); | |
669 | ||
670 | return ret; | |
096ab9ea RK |
671 | } |
672 | \f | |
673 | /* Or into bitmap TO bitmap FROM1 and'ed with the complement of | |
ec5c56db | 674 | bitmap FROM2. */ |
096ab9ea RK |
675 | |
676 | void | |
677 | bitmap_ior_and_compl (to, from1, from2) | |
678 | bitmap to; | |
679 | bitmap from1; | |
680 | bitmap from2; | |
681 | { | |
682 | bitmap_head tmp; | |
683 | ||
684 | tmp.first = tmp.current = 0; | |
685 | ||
686 | bitmap_operation (&tmp, from1, from2, BITMAP_AND_COMPL); | |
687 | bitmap_operation (to, to, &tmp, BITMAP_IOR); | |
688 | bitmap_clear (&tmp); | |
689 | } | |
87e08c69 RH |
690 | |
691 | int | |
692 | bitmap_union_of_diff (dst, a, b, c) | |
693 | bitmap dst; | |
694 | bitmap a; | |
695 | bitmap b; | |
696 | bitmap c; | |
697 | { | |
698 | bitmap_head tmp; | |
699 | int changed; | |
700 | ||
701 | tmp.first = tmp.current = 0; | |
702 | ||
703 | bitmap_operation (&tmp, b, c, BITMAP_AND_COMPL); | |
704 | changed = bitmap_operation (dst, &tmp, a, BITMAP_IOR); | |
705 | bitmap_clear (&tmp); | |
706 | ||
707 | return changed; | |
708 | } | |
096ab9ea RK |
709 | \f |
710 | /* Initialize a bitmap header. */ | |
711 | ||
712 | bitmap | |
713 | bitmap_initialize (head) | |
714 | bitmap head; | |
715 | { | |
716 | head->first = head->current = 0; | |
717 | ||
718 | return head; | |
719 | } | |
720 | \f | |
721 | /* Debugging function to print out the contents of a bitmap. */ | |
722 | ||
723 | void | |
bfd38496 | 724 | debug_bitmap_file (file, head) |
096ab9ea RK |
725 | FILE *file; |
726 | bitmap head; | |
727 | { | |
728 | bitmap_element *ptr; | |
729 | ||
730 | fprintf (file, "\nfirst = "); | |
683eb0e9 | 731 | fprintf (file, HOST_PTR_PRINTF, (PTR) head->first); |
096ab9ea | 732 | fprintf (file, " current = "); |
683eb0e9 | 733 | fprintf (file, HOST_PTR_PRINTF, (PTR) head->current); |
096ab9ea RK |
734 | fprintf (file, " indx = %u\n", head->indx); |
735 | ||
736 | for (ptr = head->first; ptr; ptr = ptr->next) | |
737 | { | |
738 | int i, j, col = 26; | |
739 | ||
740 | fprintf (file, "\t"); | |
683eb0e9 | 741 | fprintf (file, HOST_PTR_PRINTF, (PTR) ptr); |
096ab9ea | 742 | fprintf (file, " next = "); |
683eb0e9 | 743 | fprintf (file, HOST_PTR_PRINTF, (PTR) ptr->next); |
096ab9ea | 744 | fprintf (file, " prev = "); |
683eb0e9 | 745 | fprintf (file, HOST_PTR_PRINTF, (PTR) ptr->prev); |
096ab9ea RK |
746 | fprintf (file, " indx = %u\n\t\tbits = {", ptr->indx); |
747 | ||
748 | for (i = 0; i < BITMAP_ELEMENT_WORDS; i++) | |
749 | for (j = 0; j < HOST_BITS_PER_WIDE_INT; j++) | |
8229306b | 750 | if ((ptr->bits[i] >> j) & 1) |
096ab9ea RK |
751 | { |
752 | if (col > 70) | |
753 | { | |
754 | fprintf (file, "\n\t\t\t"); | |
755 | col = 24; | |
756 | } | |
757 | ||
758 | fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS | |
759 | + i * HOST_BITS_PER_WIDE_INT + j)); | |
760 | col += 4; | |
761 | } | |
762 | ||
763 | fprintf (file, " }\n"); | |
764 | } | |
765 | } | |
a615c28a | 766 | |
096ab9ea RK |
767 | /* Function to be called from the debugger to print the contents |
768 | of a bitmap. */ | |
769 | ||
770 | void | |
771 | debug_bitmap (head) | |
772 | bitmap head; | |
773 | { | |
bfd38496 | 774 | debug_bitmap_file (stdout, head); |
096ab9ea | 775 | } |
a615c28a | 776 | |
bfd38496 | 777 | /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file, |
22fa5b8a MM |
778 | it does not print anything but the bits. */ |
779 | ||
780 | void | |
781 | bitmap_print (file, head, prefix, suffix) | |
782 | FILE *file; | |
783 | bitmap head; | |
5d5993dd KG |
784 | const char *prefix; |
785 | const char *suffix; | |
22fa5b8a | 786 | { |
5d5993dd | 787 | const char *comma = ""; |
22fa5b8a MM |
788 | int i; |
789 | ||
790 | fputs (prefix, file); | |
791 | EXECUTE_IF_SET_IN_BITMAP (head, 0, i, | |
792 | { | |
793 | fprintf (file, "%s%d", comma, i); | |
794 | comma = ", "; | |
795 | }); | |
796 | fputs (suffix, file); | |
797 | } |