alias.c: Remove unused headers.
[platform/upstream/gcc.git] / gcc / bitmap.c
1 /* Functions to support general ended bitmaps.
2    Copyright (C) 1997-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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 3, or (at your option) any later
9 version.
10
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.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "bitmap.h"
24
25 /* Memory allocation statistics purpose instance.  */
26 mem_alloc_description<bitmap_usage> bitmap_mem_desc;
27
28 /* Register new bitmap.  */
29 void
30 bitmap_register (bitmap b MEM_STAT_DECL)
31 {
32   bitmap_mem_desc.register_descriptor (b, BITMAP_ORIGIN, false
33                                        FINAL_PASS_MEM_STAT);
34 }
35
36 /* Account the overhead.  */
37 static void
38 register_overhead (bitmap b, int amount)
39 {
40   if (bitmap_mem_desc.contains_descriptor_for_instance (b))
41     bitmap_mem_desc.register_instance_overhead (amount, b);
42 }
43
44 /* Global data */
45 bitmap_element bitmap_zero_bits;  /* An element of all zero bits.  */
46 bitmap_obstack bitmap_default_obstack;    /* The default bitmap obstack.  */
47 static int bitmap_default_obstack_depth;
48 static GTY((deletable)) bitmap_element *bitmap_ggc_free; /* Freelist of
49                                                             GC'd elements.  */
50
51 static void bitmap_elem_to_freelist (bitmap, bitmap_element *);
52 static void bitmap_element_free (bitmap, bitmap_element *);
53 static bitmap_element *bitmap_element_allocate (bitmap);
54 static int bitmap_element_zerop (const bitmap_element *);
55 static void bitmap_element_link (bitmap, bitmap_element *);
56 static bitmap_element *bitmap_elt_insert_after (bitmap, bitmap_element *, unsigned int);
57 static void bitmap_elt_clear_from (bitmap, bitmap_element *);
58 static bitmap_element *bitmap_find_bit (bitmap, unsigned int);
59 \f
60
61 /* Add ELEM to the appropriate freelist.  */
62 static inline void
63 bitmap_elem_to_freelist (bitmap head, bitmap_element *elt)
64 {
65   bitmap_obstack *bit_obstack = head->obstack;
66
67   elt->next = NULL;
68   if (bit_obstack)
69     {
70       elt->prev = bit_obstack->elements;
71       bit_obstack->elements = elt;
72     }
73   else
74     {
75       elt->prev = bitmap_ggc_free;
76       bitmap_ggc_free = elt;
77     }
78 }
79
80 /* Free a bitmap element.  Since these are allocated off the
81    bitmap_obstack, "free" actually means "put onto the freelist".  */
82
83 static inline void
84 bitmap_element_free (bitmap head, bitmap_element *elt)
85 {
86   bitmap_element *next = elt->next;
87   bitmap_element *prev = elt->prev;
88
89   if (prev)
90     prev->next = next;
91
92   if (next)
93     next->prev = prev;
94
95   if (head->first == elt)
96     head->first = next;
97
98   /* Since the first thing we try is to insert before current,
99      make current the next entry in preference to the previous.  */
100   if (head->current == elt)
101     {
102       head->current = next != 0 ? next : prev;
103       if (head->current)
104         head->indx = head->current->indx;
105       else
106         head->indx = 0;
107     }
108
109   if (GATHER_STATISTICS)
110     register_overhead (head, -((int)sizeof (bitmap_element)));
111
112   bitmap_elem_to_freelist (head, elt);
113 }
114 \f
115 /* Allocate a bitmap element.  The bits are cleared, but nothing else is.  */
116
117 static inline bitmap_element *
118 bitmap_element_allocate (bitmap head)
119 {
120   bitmap_element *element;
121   bitmap_obstack *bit_obstack = head->obstack;
122
123   if (bit_obstack)
124     {
125       element = bit_obstack->elements;
126
127       if (element)
128         /* Use up the inner list first before looking at the next
129            element of the outer list.  */
130         if (element->next)
131           {
132             bit_obstack->elements = element->next;
133             bit_obstack->elements->prev = element->prev;
134           }
135         else
136           /*  Inner list was just a singleton.  */
137           bit_obstack->elements = element->prev;
138       else
139         element = XOBNEW (&bit_obstack->obstack, bitmap_element);
140     }
141   else
142     {
143       element = bitmap_ggc_free;
144       if (element)
145         /* Use up the inner list first before looking at the next
146            element of the outer list.  */
147         if (element->next)
148           {
149             bitmap_ggc_free = element->next;
150             bitmap_ggc_free->prev = element->prev;
151           }
152         else
153           /*  Inner list was just a singleton.  */
154           bitmap_ggc_free = element->prev;
155       else
156         element = ggc_alloc<bitmap_element> ();
157     }
158
159   if (GATHER_STATISTICS)
160     register_overhead (head, sizeof (bitmap_element));
161
162   memset (element->bits, 0, sizeof (element->bits));
163
164   return element;
165 }
166
167 /* Remove ELT and all following elements from bitmap HEAD.  */
168
169 void
170 bitmap_elt_clear_from (bitmap head, bitmap_element *elt)
171 {
172   bitmap_element *prev;
173   bitmap_obstack *bit_obstack = head->obstack;
174
175   if (!elt) return;
176
177   if (GATHER_STATISTICS)
178     {
179       int n = 0;
180       for (prev = elt; prev; prev = prev->next)
181         n++;
182       register_overhead (head, -sizeof (bitmap_element) * n);
183     }
184
185   prev = elt->prev;
186   if (prev)
187     {
188       prev->next = NULL;
189       if (head->current->indx > prev->indx)
190         {
191           head->current = prev;
192           head->indx = prev->indx;
193         }
194     }
195   else
196     {
197       head->first = NULL;
198       head->current = NULL;
199       head->indx = 0;
200     }
201
202   /* Put the entire list onto the free list in one operation. */
203   if (bit_obstack)
204     {
205       elt->prev = bit_obstack->elements;
206       bit_obstack->elements = elt;
207     }
208   else
209     {
210       elt->prev = bitmap_ggc_free;
211       bitmap_ggc_free = elt;
212     }
213 }
214
215 /* Clear a bitmap by freeing the linked list.  */
216
217 void
218 bitmap_clear (bitmap head)
219 {
220   if (head->first)
221     bitmap_elt_clear_from (head, head->first);
222 }
223 \f
224 /* Initialize a bitmap obstack.  If BIT_OBSTACK is NULL, initialize
225    the default bitmap obstack.  */
226
227 void
228 bitmap_obstack_initialize (bitmap_obstack *bit_obstack)
229 {
230   if (!bit_obstack)
231     {
232       if (bitmap_default_obstack_depth++)
233         return;
234       bit_obstack = &bitmap_default_obstack;
235     }
236
237 #if !defined(__GNUC__) || (__GNUC__ < 2)
238 #define __alignof__(type) 0
239 #endif
240
241   bit_obstack->elements = NULL;
242   bit_obstack->heads = NULL;
243   obstack_specify_allocation (&bit_obstack->obstack, OBSTACK_CHUNK_SIZE,
244                               __alignof__ (bitmap_element),
245                               obstack_chunk_alloc,
246                               obstack_chunk_free);
247 }
248
249 /* Release the memory from a bitmap obstack.  If BIT_OBSTACK is NULL,
250    release the default bitmap obstack.  */
251
252 void
253 bitmap_obstack_release (bitmap_obstack *bit_obstack)
254 {
255   if (!bit_obstack)
256     {
257       if (--bitmap_default_obstack_depth)
258         {
259           gcc_assert (bitmap_default_obstack_depth > 0);
260           return;
261         }
262       bit_obstack = &bitmap_default_obstack;
263     }
264
265   bit_obstack->elements = NULL;
266   bit_obstack->heads = NULL;
267   obstack_free (&bit_obstack->obstack, NULL);
268 }
269
270 /* Create a new bitmap on an obstack.  If BIT_OBSTACK is NULL, create
271    it on the default bitmap obstack.  */
272
273 bitmap
274 bitmap_obstack_alloc_stat (bitmap_obstack *bit_obstack MEM_STAT_DECL)
275 {
276   bitmap map;
277
278   if (!bit_obstack)
279     bit_obstack = &bitmap_default_obstack;
280   map = bit_obstack->heads;
281   if (map)
282     bit_obstack->heads = (struct bitmap_head *) map->first;
283   else
284     map = XOBNEW (&bit_obstack->obstack, bitmap_head);
285   bitmap_initialize_stat (map, bit_obstack PASS_MEM_STAT);
286
287   if (GATHER_STATISTICS)
288     register_overhead (map, sizeof (bitmap_head));
289
290   return map;
291 }
292
293 /* Create a new GCd bitmap.  */
294
295 bitmap
296 bitmap_gc_alloc_stat (ALONE_MEM_STAT_DECL)
297 {
298   bitmap map;
299
300   map = ggc_alloc<bitmap_head> ();
301   bitmap_initialize_stat (map, NULL PASS_MEM_STAT);
302
303   if (GATHER_STATISTICS)
304     register_overhead (map, sizeof (bitmap_head));
305
306   return map;
307 }
308
309 /* Release an obstack allocated bitmap.  */
310
311 void
312 bitmap_obstack_free (bitmap map)
313 {
314   if (map)
315     {
316       bitmap_clear (map);
317       map->first = (bitmap_element *) map->obstack->heads;
318
319       if (GATHER_STATISTICS)
320         register_overhead (map, -((int)sizeof (bitmap_head)));
321
322       map->obstack->heads = map;
323     }
324 }
325
326 \f
327 /* Return nonzero if all bits in an element are zero.  */
328
329 static inline int
330 bitmap_element_zerop (const bitmap_element *element)
331 {
332 #if BITMAP_ELEMENT_WORDS == 2
333   return (element->bits[0] | element->bits[1]) == 0;
334 #else
335   unsigned i;
336
337   for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
338     if (element->bits[i] != 0)
339       return 0;
340
341   return 1;
342 #endif
343 }
344 \f
345 /* Link the bitmap element into the current bitmap linked list.  */
346
347 static inline void
348 bitmap_element_link (bitmap head, bitmap_element *element)
349 {
350   unsigned int indx = element->indx;
351   bitmap_element *ptr;
352
353   /* If this is the first and only element, set it in.  */
354   if (head->first == 0)
355     {
356       element->next = element->prev = 0;
357       head->first = element;
358     }
359
360   /* If this index is less than that of the current element, it goes someplace
361      before the current element.  */
362   else if (indx < head->indx)
363     {
364       for (ptr = head->current;
365            ptr->prev != 0 && ptr->prev->indx > indx;
366            ptr = ptr->prev)
367         ;
368
369       if (ptr->prev)
370         ptr->prev->next = element;
371       else
372         head->first = element;
373
374       element->prev = ptr->prev;
375       element->next = ptr;
376       ptr->prev = element;
377     }
378
379   /* Otherwise, it must go someplace after the current element.  */
380   else
381     {
382       for (ptr = head->current;
383            ptr->next != 0 && ptr->next->indx < indx;
384            ptr = ptr->next)
385         ;
386
387       if (ptr->next)
388         ptr->next->prev = element;
389
390       element->next = ptr->next;
391       element->prev = ptr;
392       ptr->next = element;
393     }
394
395   /* Set up so this is the first element searched.  */
396   head->current = element;
397   head->indx = indx;
398 }
399
400 /* Insert a new uninitialized element into bitmap HEAD after element
401    ELT.  If ELT is NULL, insert the element at the start.  Return the
402    new element.  */
403
404 static bitmap_element *
405 bitmap_elt_insert_after (bitmap head, bitmap_element *elt, unsigned int indx)
406 {
407   bitmap_element *node = bitmap_element_allocate (head);
408   node->indx = indx;
409
410   if (!elt)
411     {
412       if (!head->current)
413         {
414           head->current = node;
415           head->indx = indx;
416         }
417       node->next = head->first;
418       if (node->next)
419         node->next->prev = node;
420       head->first = node;
421       node->prev = NULL;
422     }
423   else
424     {
425       gcc_checking_assert (head->current);
426       node->next = elt->next;
427       if (node->next)
428         node->next->prev = node;
429       elt->next = node;
430       node->prev = elt;
431     }
432   return node;
433 }
434 \f
435 /* Copy a bitmap to another bitmap.  */
436
437 void
438 bitmap_copy (bitmap to, const_bitmap from)
439 {
440   const bitmap_element *from_ptr;
441   bitmap_element *to_ptr = 0;
442
443   bitmap_clear (to);
444
445   /* Copy elements in forward direction one at a time.  */
446   for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
447     {
448       bitmap_element *to_elt = bitmap_element_allocate (to);
449
450       to_elt->indx = from_ptr->indx;
451       memcpy (to_elt->bits, from_ptr->bits, sizeof (to_elt->bits));
452
453       /* Here we have a special case of bitmap_element_link, for the case
454          where we know the links are being entered in sequence.  */
455       if (to_ptr == 0)
456         {
457           to->first = to->current = to_elt;
458           to->indx = from_ptr->indx;
459           to_elt->next = to_elt->prev = 0;
460         }
461       else
462         {
463           to_elt->prev = to_ptr;
464           to_elt->next = 0;
465           to_ptr->next = to_elt;
466         }
467
468       to_ptr = to_elt;
469     }
470 }
471 \f
472 /* Find a bitmap element that would hold a bitmap's bit.
473    Update the `current' field even if we can't find an element that
474    would hold the bitmap's bit to make eventual allocation
475    faster.  */
476
477 static inline bitmap_element *
478 bitmap_find_bit (bitmap head, unsigned int bit)
479 {
480   bitmap_element *element;
481   unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
482
483   if (head->current == NULL
484       || head->indx == indx)
485     return head->current;
486   if (head->current == head->first
487       && head->first->next == NULL)
488     return NULL;
489
490    /* Usage can be NULL due to allocated bitmaps for which we do not
491       call initialize function.  */
492    bitmap_usage *usage = bitmap_mem_desc.get_descriptor_for_instance (head);
493
494   /* This bitmap has more than one element, and we're going to look
495      through the elements list.  Count that as a search.  */
496   if (GATHER_STATISTICS && usage)
497     usage->m_nsearches++;
498
499   if (head->indx < indx)
500     /* INDX is beyond head->indx.  Search from head->current
501        forward.  */
502     for (element = head->current;
503          element->next != 0 && element->indx < indx;
504          element = element->next)
505       {
506         if (GATHER_STATISTICS && usage)
507           usage->m_search_iter++;
508       }
509
510   else if (head->indx / 2 < indx)
511     /* INDX is less than head->indx and closer to head->indx than to
512        0.  Search from head->current backward.  */
513     for (element = head->current;
514          element->prev != 0 && element->indx > indx;
515          element = element->prev)
516       {
517         if (GATHER_STATISTICS && usage)
518           usage->m_search_iter++;
519       }
520
521   else
522     /* INDX is less than head->indx and closer to 0 than to
523        head->indx.  Search from head->first forward.  */
524     for (element = head->first;
525          element->next != 0 && element->indx < indx;
526          element = element->next)
527       if (GATHER_STATISTICS && usage)
528         {
529           usage->m_search_iter++;
530         }
531
532   /* `element' is the nearest to the one we want.  If it's not the one we
533      want, the one we want doesn't exist.  */
534   head->current = element;
535   head->indx = element->indx;
536   if (element != 0 && element->indx != indx)
537     element = 0;
538
539   return element;
540 }
541 \f
542 /* Clear a single bit in a bitmap.  Return true if the bit changed.  */
543
544 bool
545 bitmap_clear_bit (bitmap head, int bit)
546 {
547   bitmap_element *const ptr = bitmap_find_bit (head, bit);
548
549   if (ptr != 0)
550     {
551       unsigned bit_num  = bit % BITMAP_WORD_BITS;
552       unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
553       BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
554       bool res = (ptr->bits[word_num] & bit_val) != 0;
555       if (res)
556         {
557           ptr->bits[word_num] &= ~bit_val;
558           /* If we cleared the entire word, free up the element.  */
559           if (!ptr->bits[word_num]
560               && bitmap_element_zerop (ptr))
561             bitmap_element_free (head, ptr);
562         }
563
564       return res;
565     }
566
567   return false;
568 }
569
570 /* Set a single bit in a bitmap.  Return true if the bit changed.  */
571
572 bool
573 bitmap_set_bit (bitmap head, int bit)
574 {
575   bitmap_element *ptr = bitmap_find_bit (head, bit);
576   unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
577   unsigned bit_num  = bit % BITMAP_WORD_BITS;
578   BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
579
580   if (ptr == 0)
581     {
582       ptr = bitmap_element_allocate (head);
583       ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS;
584       ptr->bits[word_num] = bit_val;
585       bitmap_element_link (head, ptr);
586       return true;
587     }
588   else
589     {
590       bool res = (ptr->bits[word_num] & bit_val) == 0;
591       if (res)
592         ptr->bits[word_num] |= bit_val;
593       return res;
594     }
595 }
596
597 /* Return whether a bit is set within a bitmap.  */
598
599 int
600 bitmap_bit_p (bitmap head, int bit)
601 {
602   bitmap_element *ptr;
603   unsigned bit_num;
604   unsigned word_num;
605
606   ptr = bitmap_find_bit (head, bit);
607   if (ptr == 0)
608     return 0;
609
610   bit_num = bit % BITMAP_WORD_BITS;
611   word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
612
613   return (ptr->bits[word_num] >> bit_num) & 1;
614 }
615 \f
616 #if GCC_VERSION < 3400
617 /* Table of number of set bits in a character, indexed by value of char.  */
618 static const unsigned char popcount_table[] =
619 {
620     0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
621     1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
622     1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
623     2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
624     1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
625     2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
626     2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
627     3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
628 };
629
630 static unsigned long
631 bitmap_popcount (BITMAP_WORD a)
632 {
633   unsigned long ret = 0;
634   unsigned i;
635
636   /* Just do this the table way for now  */
637   for (i = 0; i < BITMAP_WORD_BITS; i+= 8)
638     ret += popcount_table[(a >> i) & 0xff];
639   return ret;
640 }
641 #endif
642 /* Count the number of bits set in the bitmap, and return it.  */
643
644 unsigned long
645 bitmap_count_bits (const_bitmap a)
646 {
647   unsigned long count = 0;
648   const bitmap_element *elt;
649   unsigned ix;
650
651   for (elt = a->first; elt; elt = elt->next)
652     {
653       for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
654         {
655 #if GCC_VERSION >= 3400
656           /* Note that popcountl matches BITMAP_WORD in type, so the actual size
657          of BITMAP_WORD is not material.  */
658           count += __builtin_popcountl (elt->bits[ix]);
659 #else
660           count += bitmap_popcount (elt->bits[ix]);
661 #endif
662         }
663     }
664   return count;
665 }
666
667 /* Return true if the bitmap has a single bit set.  Otherwise return
668    false.  */
669
670 bool
671 bitmap_single_bit_set_p (const_bitmap a)
672 {
673   unsigned long count = 0;
674   const bitmap_element *elt;
675   unsigned ix;
676
677   if (bitmap_empty_p (a))
678     return false;
679
680   elt = a->first;
681   /* As there are no completely empty bitmap elements, a second one
682      means we have more than one bit set.  */
683   if (elt->next != NULL)
684     return false;
685
686   for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
687     {
688 #if GCC_VERSION >= 3400
689       /* Note that popcountl matches BITMAP_WORD in type, so the actual size
690          of BITMAP_WORD is not material.  */
691       count += __builtin_popcountl (elt->bits[ix]);
692 #else
693       count += bitmap_popcount (elt->bits[ix]);
694 #endif
695       if (count > 1)
696         return false;
697     }
698
699   return count == 1;
700 }
701
702
703 /* Return the bit number of the first set bit in the bitmap.  The
704    bitmap must be non-empty.  */
705
706 unsigned
707 bitmap_first_set_bit (const_bitmap a)
708 {
709   const bitmap_element *elt = a->first;
710   unsigned bit_no;
711   BITMAP_WORD word;
712   unsigned ix;
713
714   gcc_checking_assert (elt);
715   bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
716   for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
717     {
718       word = elt->bits[ix];
719       if (word)
720         goto found_bit;
721     }
722   gcc_unreachable ();
723  found_bit:
724   bit_no += ix * BITMAP_WORD_BITS;
725
726 #if GCC_VERSION >= 3004
727   gcc_assert (sizeof (long) == sizeof (word));
728   bit_no += __builtin_ctzl (word);
729 #else
730   /* Binary search for the first set bit.  */
731 #if BITMAP_WORD_BITS > 64
732 #error "Fill out the table."
733 #endif
734 #if BITMAP_WORD_BITS > 32
735   if (!(word & 0xffffffff))
736     word >>= 32, bit_no += 32;
737 #endif
738   if (!(word & 0xffff))
739     word >>= 16, bit_no += 16;
740   if (!(word & 0xff))
741     word >>= 8, bit_no += 8;
742   if (!(word & 0xf))
743     word >>= 4, bit_no += 4;
744   if (!(word & 0x3))
745     word >>= 2, bit_no += 2;
746   if (!(word & 0x1))
747     word >>= 1, bit_no += 1;
748
749  gcc_checking_assert (word & 1);
750 #endif
751  return bit_no;
752 }
753
754 /* Return the bit number of the first set bit in the bitmap.  The
755    bitmap must be non-empty.  */
756
757 unsigned
758 bitmap_last_set_bit (const_bitmap a)
759 {
760   const bitmap_element *elt = a->current ? a->current : a->first;
761   unsigned bit_no;
762   BITMAP_WORD word;
763   int ix;
764
765   gcc_checking_assert (elt);
766   while (elt->next)
767     elt = elt->next;
768   bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
769   for (ix = BITMAP_ELEMENT_WORDS - 1; ix >= 0; ix--)
770     {
771       word = elt->bits[ix];
772       if (word)
773         goto found_bit;
774     }
775   gcc_unreachable ();
776  found_bit:
777   bit_no += ix * BITMAP_WORD_BITS;
778 #if GCC_VERSION >= 3004
779   gcc_assert (sizeof (long) == sizeof (word));
780   bit_no += BITMAP_WORD_BITS - __builtin_clzl (word) - 1;
781 #else
782   /* Hopefully this is a twos-complement host...  */
783   BITMAP_WORD x = word;
784   x |= (x >> 1);
785   x |= (x >> 2);
786   x |= (x >> 4);
787   x |= (x >> 8);
788   x |= (x >> 16);
789 #if BITMAP_WORD_BITS > 32
790   x |= (x >> 32);
791 #endif
792   bit_no += bitmap_popcount (x) - 1;
793 #endif
794
795   return bit_no;
796 }
797 \f
798
799 /* DST = A & B.  */
800
801 void
802 bitmap_and (bitmap dst, const_bitmap a, const_bitmap b)
803 {
804   bitmap_element *dst_elt = dst->first;
805   const bitmap_element *a_elt = a->first;
806   const bitmap_element *b_elt = b->first;
807   bitmap_element *dst_prev = NULL;
808
809   gcc_assert (dst != a && dst != b);
810
811   if (a == b)
812     {
813       bitmap_copy (dst, a);
814       return;
815     }
816
817   while (a_elt && b_elt)
818     {
819       if (a_elt->indx < b_elt->indx)
820         a_elt = a_elt->next;
821       else if (b_elt->indx < a_elt->indx)
822         b_elt = b_elt->next;
823       else
824         {
825           /* Matching elts, generate A & B.  */
826           unsigned ix;
827           BITMAP_WORD ior = 0;
828
829           if (!dst_elt)
830             dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
831           else
832             dst_elt->indx = a_elt->indx;
833           for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
834             {
835               BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
836
837               dst_elt->bits[ix] = r;
838               ior |= r;
839             }
840           if (ior)
841             {
842               dst_prev = dst_elt;
843               dst_elt = dst_elt->next;
844             }
845           a_elt = a_elt->next;
846           b_elt = b_elt->next;
847         }
848     }
849   /* Ensure that dst->current is valid.  */
850   dst->current = dst->first;
851   bitmap_elt_clear_from (dst, dst_elt);
852   gcc_checking_assert (!dst->current == !dst->first);
853   if (dst->current)
854     dst->indx = dst->current->indx;
855 }
856
857 /* A &= B.  Return true if A changed.  */
858
859 bool
860 bitmap_and_into (bitmap a, const_bitmap b)
861 {
862   bitmap_element *a_elt = a->first;
863   const bitmap_element *b_elt = b->first;
864   bitmap_element *next;
865   bool changed = false;
866
867   if (a == b)
868     return false;
869
870   while (a_elt && b_elt)
871     {
872       if (a_elt->indx < b_elt->indx)
873         {
874           next = a_elt->next;
875           bitmap_element_free (a, a_elt);
876           a_elt = next;
877           changed = true;
878         }
879       else if (b_elt->indx < a_elt->indx)
880         b_elt = b_elt->next;
881       else
882         {
883           /* Matching elts, generate A &= B.  */
884           unsigned ix;
885           BITMAP_WORD ior = 0;
886
887           for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
888             {
889               BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
890               if (a_elt->bits[ix] != r)
891                 changed = true;
892               a_elt->bits[ix] = r;
893               ior |= r;
894             }
895           next = a_elt->next;
896           if (!ior)
897             bitmap_element_free (a, a_elt);
898           a_elt = next;
899           b_elt = b_elt->next;
900         }
901     }
902
903   if (a_elt)
904     {
905       changed = true;
906       bitmap_elt_clear_from (a, a_elt);
907     }
908
909   gcc_checking_assert (!a->current == !a->first
910                        && (!a->current || a->indx == a->current->indx));
911
912   return changed;
913 }
914
915
916 /* Insert an element equal to SRC_ELT after DST_PREV, overwriting DST_ELT
917    if non-NULL.  CHANGED is true if the destination bitmap had already been
918    changed; the new value of CHANGED is returned.  */
919
920 static inline bool
921 bitmap_elt_copy (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
922                  const bitmap_element *src_elt, bool changed)
923 {
924   if (!changed && dst_elt && dst_elt->indx == src_elt->indx)
925     {
926       unsigned ix;
927
928       for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
929         if (src_elt->bits[ix] != dst_elt->bits[ix])
930           {
931             dst_elt->bits[ix] = src_elt->bits[ix];
932             changed = true;
933           }
934     }
935   else
936     {
937       changed = true;
938       if (!dst_elt)
939         dst_elt = bitmap_elt_insert_after (dst, dst_prev, src_elt->indx);
940       else
941         dst_elt->indx = src_elt->indx;
942       memcpy (dst_elt->bits, src_elt->bits, sizeof (dst_elt->bits));
943     }
944   return changed;
945 }
946
947
948
949 /* DST = A & ~B  */
950
951 bool
952 bitmap_and_compl (bitmap dst, const_bitmap a, const_bitmap b)
953 {
954   bitmap_element *dst_elt = dst->first;
955   const bitmap_element *a_elt = a->first;
956   const bitmap_element *b_elt = b->first;
957   bitmap_element *dst_prev = NULL;
958   bitmap_element **dst_prev_pnext = &dst->first;
959   bool changed = false;
960
961   gcc_assert (dst != a && dst != b);
962
963   if (a == b)
964     {
965       changed = !bitmap_empty_p (dst);
966       bitmap_clear (dst);
967       return changed;
968     }
969
970   while (a_elt)
971     {
972       while (b_elt && b_elt->indx < a_elt->indx)
973         b_elt = b_elt->next;
974
975       if (!b_elt || b_elt->indx > a_elt->indx)
976         {
977           changed = bitmap_elt_copy (dst, dst_elt, dst_prev, a_elt, changed);
978           dst_prev = *dst_prev_pnext;
979           dst_prev_pnext = &dst_prev->next;
980           dst_elt = *dst_prev_pnext;
981           a_elt = a_elt->next;
982         }
983
984       else
985         {
986           /* Matching elts, generate A & ~B.  */
987           unsigned ix;
988           BITMAP_WORD ior = 0;
989
990           if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
991             {
992               for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
993                 {
994                   BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
995
996                   if (dst_elt->bits[ix] != r)
997                     {
998                       changed = true;
999                       dst_elt->bits[ix] = r;
1000                     }
1001                   ior |= r;
1002                 }
1003             }
1004           else
1005             {
1006               bool new_element;
1007               if (!dst_elt || dst_elt->indx > a_elt->indx)
1008                 {
1009                   dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
1010                   new_element = true;
1011                 }
1012               else
1013                 {
1014                   dst_elt->indx = a_elt->indx;
1015                   new_element = false;
1016                 }
1017
1018               for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1019                 {
1020                   BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1021
1022                   dst_elt->bits[ix] = r;
1023                   ior |= r;
1024                 }
1025
1026               if (ior)
1027                 changed = true;
1028               else
1029                 {
1030                   changed |= !new_element;
1031                   bitmap_element_free (dst, dst_elt);
1032                   dst_elt = *dst_prev_pnext;
1033                 }
1034             }
1035
1036           if (ior)
1037             {
1038               dst_prev = *dst_prev_pnext;
1039               dst_prev_pnext = &dst_prev->next;
1040               dst_elt = *dst_prev_pnext;
1041             }
1042           a_elt = a_elt->next;
1043           b_elt = b_elt->next;
1044         }
1045     }
1046
1047   /* Ensure that dst->current is valid.  */
1048   dst->current = dst->first;
1049
1050   if (dst_elt)
1051     {
1052       changed = true;
1053       bitmap_elt_clear_from (dst, dst_elt);
1054     }
1055   gcc_checking_assert (!dst->current == !dst->first);
1056   if (dst->current)
1057     dst->indx = dst->current->indx;
1058
1059   return changed;
1060 }
1061
1062 /* A &= ~B. Returns true if A changes */
1063
1064 bool
1065 bitmap_and_compl_into (bitmap a, const_bitmap b)
1066 {
1067   bitmap_element *a_elt = a->first;
1068   const bitmap_element *b_elt = b->first;
1069   bitmap_element *next;
1070   BITMAP_WORD changed = 0;
1071
1072   if (a == b)
1073     {
1074       if (bitmap_empty_p (a))
1075         return false;
1076       else
1077         {
1078           bitmap_clear (a);
1079           return true;
1080         }
1081     }
1082
1083   while (a_elt && b_elt)
1084     {
1085       if (a_elt->indx < b_elt->indx)
1086         a_elt = a_elt->next;
1087       else if (b_elt->indx < a_elt->indx)
1088         b_elt = b_elt->next;
1089       else
1090         {
1091           /* Matching elts, generate A &= ~B.  */
1092           unsigned ix;
1093           BITMAP_WORD ior = 0;
1094
1095           for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1096             {
1097               BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1098               BITMAP_WORD r = a_elt->bits[ix] ^ cleared;
1099
1100               a_elt->bits[ix] = r;
1101               changed |= cleared;
1102               ior |= r;
1103             }
1104           next = a_elt->next;
1105           if (!ior)
1106             bitmap_element_free (a, a_elt);
1107           a_elt = next;
1108           b_elt = b_elt->next;
1109         }
1110     }
1111   gcc_checking_assert (!a->current == !a->first
1112                        && (!a->current || a->indx == a->current->indx));
1113   return changed != 0;
1114 }
1115
1116 /* Set COUNT bits from START in HEAD.  */
1117 void
1118 bitmap_set_range (bitmap head, unsigned int start, unsigned int count)
1119 {
1120   unsigned int first_index, end_bit_plus1, last_index;
1121   bitmap_element *elt, *elt_prev;
1122   unsigned int i;
1123
1124   if (!count)
1125     return;
1126
1127   if (count == 1)
1128     {
1129       bitmap_set_bit (head, start);
1130       return;
1131     }
1132
1133   first_index = start / BITMAP_ELEMENT_ALL_BITS;
1134   end_bit_plus1 = start + count;
1135   last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1136   elt = bitmap_find_bit (head, start);
1137
1138   /* If bitmap_find_bit returns zero, the current is the closest block
1139      to the result.  Otherwise, just use bitmap_element_allocate to
1140      ensure ELT is set; in the loop below, ELT == NULL means "insert
1141      at the end of the bitmap".  */
1142   if (!elt)
1143     {
1144       elt = bitmap_element_allocate (head);
1145       elt->indx = first_index;
1146       bitmap_element_link (head, elt);
1147     }
1148
1149   gcc_checking_assert (elt->indx == first_index);
1150   elt_prev = elt->prev;
1151   for (i = first_index; i <= last_index; i++)
1152     {
1153       unsigned elt_start_bit = i * BITMAP_ELEMENT_ALL_BITS;
1154       unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1155
1156       unsigned int first_word_to_mod;
1157       BITMAP_WORD first_mask;
1158       unsigned int last_word_to_mod;
1159       BITMAP_WORD last_mask;
1160       unsigned int ix;
1161
1162       if (!elt || elt->indx != i)
1163         elt = bitmap_elt_insert_after (head, elt_prev, i);
1164
1165       if (elt_start_bit <= start)
1166         {
1167           /* The first bit to turn on is somewhere inside this
1168              elt.  */
1169           first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1170
1171           /* This mask should have 1s in all bits >= start position. */
1172           first_mask =
1173             (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1174           first_mask = ~first_mask;
1175         }
1176       else
1177         {
1178           /* The first bit to turn on is below this start of this elt.  */
1179           first_word_to_mod = 0;
1180           first_mask = ~(BITMAP_WORD) 0;
1181         }
1182
1183       if (elt_end_bit_plus1 <= end_bit_plus1)
1184         {
1185           /* The last bit to turn on is beyond this elt.  */
1186           last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1187           last_mask = ~(BITMAP_WORD) 0;
1188         }
1189       else
1190         {
1191           /* The last bit to turn on is inside to this elt.  */
1192           last_word_to_mod =
1193             (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1194
1195           /* The last mask should have 1s below the end bit.  */
1196           last_mask =
1197             (((BITMAP_WORD) 1) << ((end_bit_plus1 % BITMAP_WORD_BITS))) - 1;
1198         }
1199
1200       if (first_word_to_mod == last_word_to_mod)
1201         {
1202           BITMAP_WORD mask = first_mask & last_mask;
1203           elt->bits[first_word_to_mod] |= mask;
1204         }
1205       else
1206         {
1207           elt->bits[first_word_to_mod] |= first_mask;
1208           if (BITMAP_ELEMENT_WORDS > 2)
1209             for (ix = first_word_to_mod + 1; ix < last_word_to_mod; ix++)
1210               elt->bits[ix] = ~(BITMAP_WORD) 0;
1211           elt->bits[last_word_to_mod] |= last_mask;
1212         }
1213
1214       elt_prev = elt;
1215       elt = elt->next;
1216     }
1217
1218   head->current = elt ? elt : elt_prev;
1219   head->indx = head->current->indx;
1220 }
1221
1222 /* Clear COUNT bits from START in HEAD.  */
1223 void
1224 bitmap_clear_range (bitmap head, unsigned int start, unsigned int count)
1225 {
1226   unsigned int first_index, end_bit_plus1, last_index;
1227   bitmap_element *elt;
1228
1229   if (!count)
1230     return;
1231
1232   if (count == 1)
1233     {
1234       bitmap_clear_bit (head, start);
1235       return;
1236     }
1237
1238   first_index = start / BITMAP_ELEMENT_ALL_BITS;
1239   end_bit_plus1 = start + count;
1240   last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1241   elt = bitmap_find_bit (head, start);
1242
1243   /* If bitmap_find_bit returns zero, the current is the closest block
1244      to the result.  If the current is less than first index, find the
1245      next one.  Otherwise, just set elt to be current.  */
1246   if (!elt)
1247     {
1248       if (head->current)
1249         {
1250           if (head->indx < first_index)
1251             {
1252               elt = head->current->next;
1253               if (!elt)
1254                 return;
1255             }
1256           else
1257             elt = head->current;
1258         }
1259       else
1260         return;
1261     }
1262
1263   while (elt && (elt->indx <= last_index))
1264     {
1265       bitmap_element * next_elt = elt->next;
1266       unsigned elt_start_bit = (elt->indx) * BITMAP_ELEMENT_ALL_BITS;
1267       unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1268
1269
1270       if (elt_start_bit >= start && elt_end_bit_plus1 <= end_bit_plus1)
1271         /* Get rid of the entire elt and go to the next one.  */
1272         bitmap_element_free (head, elt);
1273       else
1274         {
1275           /* Going to have to knock out some bits in this elt.  */
1276           unsigned int first_word_to_mod;
1277           BITMAP_WORD first_mask;
1278           unsigned int last_word_to_mod;
1279           BITMAP_WORD last_mask;
1280           unsigned int i;
1281           bool clear = true;
1282
1283           if (elt_start_bit <= start)
1284             {
1285               /* The first bit to turn off is somewhere inside this
1286                  elt.  */
1287               first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1288
1289               /* This mask should have 1s in all bits >= start position. */
1290               first_mask =
1291                 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1292               first_mask = ~first_mask;
1293             }
1294           else
1295             {
1296               /* The first bit to turn off is below this start of this elt.  */
1297               first_word_to_mod = 0;
1298               first_mask = 0;
1299               first_mask = ~first_mask;
1300             }
1301
1302           if (elt_end_bit_plus1 <= end_bit_plus1)
1303             {
1304               /* The last bit to turn off is beyond this elt.  */
1305               last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1306               last_mask = 0;
1307               last_mask = ~last_mask;
1308             }
1309           else
1310             {
1311               /* The last bit to turn off is inside to this elt.  */
1312               last_word_to_mod =
1313                 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1314
1315               /* The last mask should have 1s below the end bit.  */
1316               last_mask =
1317                 (((BITMAP_WORD) 1) << (((end_bit_plus1) % BITMAP_WORD_BITS))) - 1;
1318             }
1319
1320
1321           if (first_word_to_mod == last_word_to_mod)
1322             {
1323               BITMAP_WORD mask = first_mask & last_mask;
1324               elt->bits[first_word_to_mod] &= ~mask;
1325             }
1326           else
1327             {
1328               elt->bits[first_word_to_mod] &= ~first_mask;
1329               if (BITMAP_ELEMENT_WORDS > 2)
1330                 for (i = first_word_to_mod + 1; i < last_word_to_mod; i++)
1331                   elt->bits[i] = 0;
1332               elt->bits[last_word_to_mod] &= ~last_mask;
1333             }
1334           for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
1335             if (elt->bits[i])
1336               {
1337                 clear = false;
1338                 break;
1339               }
1340           /* Check to see if there are any bits left.  */
1341           if (clear)
1342             bitmap_element_free (head, elt);
1343         }
1344       elt = next_elt;
1345     }
1346
1347   if (elt)
1348     {
1349       head->current = elt;
1350       head->indx = head->current->indx;
1351     }
1352 }
1353
1354 /* A = ~A & B. */
1355
1356 void
1357 bitmap_compl_and_into (bitmap a, const_bitmap b)
1358 {
1359   bitmap_element *a_elt = a->first;
1360   const bitmap_element *b_elt = b->first;
1361   bitmap_element *a_prev = NULL;
1362   bitmap_element *next;
1363
1364   gcc_assert (a != b);
1365
1366   if (bitmap_empty_p (a))
1367     {
1368       bitmap_copy (a, b);
1369       return;
1370     }
1371   if (bitmap_empty_p (b))
1372     {
1373       bitmap_clear (a);
1374       return;
1375     }
1376
1377   while (a_elt || b_elt)
1378     {
1379       if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1380         {
1381           /* A is before B.  Remove A */
1382           next = a_elt->next;
1383           a_prev = a_elt->prev;
1384           bitmap_element_free (a, a_elt);
1385           a_elt = next;
1386         }
1387       else if (!a_elt || b_elt->indx < a_elt->indx)
1388         {
1389           /* B is before A.  Copy B. */
1390           next = bitmap_elt_insert_after (a, a_prev, b_elt->indx);
1391           memcpy (next->bits, b_elt->bits, sizeof (next->bits));
1392           a_prev = next;
1393           b_elt = b_elt->next;
1394         }
1395       else
1396         {
1397           /* Matching elts, generate A = ~A & B.  */
1398           unsigned ix;
1399           BITMAP_WORD ior = 0;
1400
1401           for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1402             {
1403               BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1404               BITMAP_WORD r = b_elt->bits[ix] ^ cleared;
1405
1406               a_elt->bits[ix] = r;
1407               ior |= r;
1408             }
1409           next = a_elt->next;
1410           if (!ior)
1411             bitmap_element_free (a, a_elt);
1412           else
1413             a_prev = a_elt;
1414           a_elt = next;
1415           b_elt = b_elt->next;
1416         }
1417     }
1418   gcc_checking_assert (!a->current == !a->first
1419                        && (!a->current || a->indx == a->current->indx));
1420   return;
1421 }
1422
1423
1424 /* Insert an element corresponding to A_ELT | B_ELT after DST_PREV,
1425    overwriting DST_ELT if non-NULL.  CHANGED is true if the destination bitmap
1426    had already been changed; the new value of CHANGED is returned.  */
1427
1428 static inline bool
1429 bitmap_elt_ior (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
1430                 const bitmap_element *a_elt, const bitmap_element *b_elt,
1431                 bool changed)
1432 {
1433   gcc_assert (a_elt || b_elt);
1434
1435   if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1436     {
1437       /* Matching elts, generate A | B.  */
1438       unsigned ix;
1439
1440       if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1441         {
1442           for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1443             {
1444               BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1445               if (r != dst_elt->bits[ix])
1446                 {
1447                   dst_elt->bits[ix] = r;
1448                   changed = true;
1449                 }
1450             }
1451         }
1452       else
1453         {
1454           changed = true;
1455           if (!dst_elt)
1456             dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
1457           else
1458             dst_elt->indx = a_elt->indx;
1459           for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1460             {
1461               BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1462               dst_elt->bits[ix] = r;
1463             }
1464         }
1465     }
1466   else
1467     {
1468       /* Copy a single element.  */
1469       const bitmap_element *src;
1470
1471       if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1472         src = a_elt;
1473       else
1474         src = b_elt;
1475
1476       gcc_checking_assert (src);
1477       changed = bitmap_elt_copy (dst, dst_elt, dst_prev, src, changed);
1478     }
1479   return changed;
1480 }
1481
1482
1483 /* DST = A | B.  Return true if DST changes.  */
1484
1485 bool
1486 bitmap_ior (bitmap dst, const_bitmap a, const_bitmap b)
1487 {
1488   bitmap_element *dst_elt = dst->first;
1489   const bitmap_element *a_elt = a->first;
1490   const bitmap_element *b_elt = b->first;
1491   bitmap_element *dst_prev = NULL;
1492   bitmap_element **dst_prev_pnext = &dst->first;
1493   bool changed = false;
1494
1495   gcc_assert (dst != a && dst != b);
1496
1497   while (a_elt || b_elt)
1498     {
1499       changed = bitmap_elt_ior (dst, dst_elt, dst_prev, a_elt, b_elt, changed);
1500
1501       if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1502         {
1503           a_elt = a_elt->next;
1504           b_elt = b_elt->next;
1505         }
1506       else
1507         {
1508           if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
1509             a_elt = a_elt->next;
1510           else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
1511             b_elt = b_elt->next;
1512         }
1513
1514       dst_prev = *dst_prev_pnext;
1515       dst_prev_pnext = &dst_prev->next;
1516       dst_elt = *dst_prev_pnext;
1517     }
1518
1519   if (dst_elt)
1520     {
1521       changed = true;
1522       /* Ensure that dst->current is valid.  */
1523       dst->current = dst->first;
1524       bitmap_elt_clear_from (dst, dst_elt);
1525     }
1526   gcc_checking_assert (!dst->current == !dst->first);
1527   if (dst->current)
1528     dst->indx = dst->current->indx;
1529   return changed;
1530 }
1531
1532 /* A |= B.  Return true if A changes.  */
1533
1534 bool
1535 bitmap_ior_into (bitmap a, const_bitmap b)
1536 {
1537   bitmap_element *a_elt = a->first;
1538   const bitmap_element *b_elt = b->first;
1539   bitmap_element *a_prev = NULL;
1540   bitmap_element **a_prev_pnext = &a->first;
1541   bool changed = false;
1542
1543   if (a == b)
1544     return false;
1545
1546   while (b_elt)
1547     {
1548       /* If A lags behind B, just advance it.  */
1549       if (!a_elt || a_elt->indx == b_elt->indx)
1550         {
1551           changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, b_elt, changed);
1552           b_elt = b_elt->next;
1553         }
1554       else if (a_elt->indx > b_elt->indx)
1555         {
1556           changed = bitmap_elt_copy (a, NULL, a_prev, b_elt, changed);
1557           b_elt = b_elt->next;
1558         }
1559
1560       a_prev = *a_prev_pnext;
1561       a_prev_pnext = &a_prev->next;
1562       a_elt = *a_prev_pnext;
1563     }
1564
1565   gcc_checking_assert (!a->current == !a->first);
1566   if (a->current)
1567     a->indx = a->current->indx;
1568   return changed;
1569 }
1570
1571 /* DST = A ^ B  */
1572
1573 void
1574 bitmap_xor (bitmap dst, const_bitmap a, const_bitmap b)
1575 {
1576   bitmap_element *dst_elt = dst->first;
1577   const bitmap_element *a_elt = a->first;
1578   const bitmap_element *b_elt = b->first;
1579   bitmap_element *dst_prev = NULL;
1580
1581   gcc_assert (dst != a && dst != b);
1582   if (a == b)
1583     {
1584       bitmap_clear (dst);
1585       return;
1586     }
1587
1588   while (a_elt || b_elt)
1589     {
1590       if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1591         {
1592           /* Matching elts, generate A ^ B.  */
1593           unsigned ix;
1594           BITMAP_WORD ior = 0;
1595
1596           if (!dst_elt)
1597             dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
1598           else
1599             dst_elt->indx = a_elt->indx;
1600           for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1601             {
1602               BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
1603
1604               ior |= r;
1605               dst_elt->bits[ix] = r;
1606             }
1607           a_elt = a_elt->next;
1608           b_elt = b_elt->next;
1609           if (ior)
1610             {
1611               dst_prev = dst_elt;
1612               dst_elt = dst_elt->next;
1613             }
1614         }
1615       else
1616         {
1617           /* Copy a single element.  */
1618           const bitmap_element *src;
1619
1620           if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1621             {
1622               src = a_elt;
1623               a_elt = a_elt->next;
1624             }
1625           else
1626             {
1627               src = b_elt;
1628               b_elt = b_elt->next;
1629             }
1630
1631           if (!dst_elt)
1632             dst_elt = bitmap_elt_insert_after (dst, dst_prev, src->indx);
1633           else
1634             dst_elt->indx = src->indx;
1635           memcpy (dst_elt->bits, src->bits, sizeof (dst_elt->bits));
1636           dst_prev = dst_elt;
1637           dst_elt = dst_elt->next;
1638         }
1639     }
1640   /* Ensure that dst->current is valid.  */
1641   dst->current = dst->first;
1642   bitmap_elt_clear_from (dst, dst_elt);
1643   gcc_checking_assert (!dst->current == !dst->first);
1644   if (dst->current)
1645     dst->indx = dst->current->indx;
1646 }
1647
1648 /* A ^= B */
1649
1650 void
1651 bitmap_xor_into (bitmap a, const_bitmap b)
1652 {
1653   bitmap_element *a_elt = a->first;
1654   const bitmap_element *b_elt = b->first;
1655   bitmap_element *a_prev = NULL;
1656
1657   if (a == b)
1658     {
1659       bitmap_clear (a);
1660       return;
1661     }
1662
1663   while (b_elt)
1664     {
1665       if (!a_elt || b_elt->indx < a_elt->indx)
1666         {
1667           /* Copy b_elt.  */
1668           bitmap_element *dst = bitmap_elt_insert_after (a, a_prev, b_elt->indx);
1669           memcpy (dst->bits, b_elt->bits, sizeof (dst->bits));
1670           a_prev = dst;
1671           b_elt = b_elt->next;
1672         }
1673       else if (a_elt->indx < b_elt->indx)
1674         {
1675           a_prev = a_elt;
1676           a_elt = a_elt->next;
1677         }
1678       else
1679         {
1680           /* Matching elts, generate A ^= B.  */
1681           unsigned ix;
1682           BITMAP_WORD ior = 0;
1683           bitmap_element *next = a_elt->next;
1684
1685           for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1686             {
1687               BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
1688
1689               ior |= r;
1690               a_elt->bits[ix] = r;
1691             }
1692           b_elt = b_elt->next;
1693           if (ior)
1694             a_prev = a_elt;
1695           else
1696             bitmap_element_free (a, a_elt);
1697           a_elt = next;
1698         }
1699     }
1700   gcc_checking_assert (!a->current == !a->first);
1701   if (a->current)
1702     a->indx = a->current->indx;
1703 }
1704
1705 /* Return true if two bitmaps are identical.
1706    We do not bother with a check for pointer equality, as that never
1707    occurs in practice.  */
1708
1709 bool
1710 bitmap_equal_p (const_bitmap a, const_bitmap b)
1711 {
1712   const bitmap_element *a_elt;
1713   const bitmap_element *b_elt;
1714   unsigned ix;
1715
1716   for (a_elt = a->first, b_elt = b->first;
1717        a_elt && b_elt;
1718        a_elt = a_elt->next, b_elt = b_elt->next)
1719     {
1720       if (a_elt->indx != b_elt->indx)
1721         return false;
1722       for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1723         if (a_elt->bits[ix] != b_elt->bits[ix])
1724           return false;
1725     }
1726   return !a_elt && !b_elt;
1727 }
1728
1729 /* Return true if A AND B is not empty.  */
1730
1731 bool
1732 bitmap_intersect_p (const_bitmap a, const_bitmap b)
1733 {
1734   const bitmap_element *a_elt;
1735   const bitmap_element *b_elt;
1736   unsigned ix;
1737
1738   for (a_elt = a->first, b_elt = b->first;
1739        a_elt && b_elt;)
1740     {
1741       if (a_elt->indx < b_elt->indx)
1742         a_elt = a_elt->next;
1743       else if (b_elt->indx < a_elt->indx)
1744         b_elt = b_elt->next;
1745       else
1746         {
1747           for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1748             if (a_elt->bits[ix] & b_elt->bits[ix])
1749               return true;
1750           a_elt = a_elt->next;
1751           b_elt = b_elt->next;
1752         }
1753     }
1754   return false;
1755 }
1756
1757 /* Return true if A AND NOT B is not empty.  */
1758
1759 bool
1760 bitmap_intersect_compl_p (const_bitmap a, const_bitmap b)
1761 {
1762   const bitmap_element *a_elt;
1763   const bitmap_element *b_elt;
1764   unsigned ix;
1765   for (a_elt = a->first, b_elt = b->first;
1766        a_elt && b_elt;)
1767     {
1768       if (a_elt->indx < b_elt->indx)
1769         return true;
1770       else if (b_elt->indx < a_elt->indx)
1771         b_elt = b_elt->next;
1772       else
1773         {
1774           for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1775             if (a_elt->bits[ix] & ~b_elt->bits[ix])
1776               return true;
1777           a_elt = a_elt->next;
1778           b_elt = b_elt->next;
1779         }
1780     }
1781   return a_elt != NULL;
1782 }
1783
1784 \f
1785 /* DST = A | (FROM1 & ~FROM2).  Return true if DST changes.  */
1786
1787 bool
1788 bitmap_ior_and_compl (bitmap dst, const_bitmap a, const_bitmap b, const_bitmap kill)
1789 {
1790   bool changed = false;
1791
1792   bitmap_element *dst_elt = dst->first;
1793   const bitmap_element *a_elt = a->first;
1794   const bitmap_element *b_elt = b->first;
1795   const bitmap_element *kill_elt = kill->first;
1796   bitmap_element *dst_prev = NULL;
1797   bitmap_element **dst_prev_pnext = &dst->first;
1798
1799   gcc_assert (dst != a && dst != b && dst != kill);
1800
1801   /* Special cases.  We don't bother checking for bitmap_equal_p (b, kill).  */
1802   if (b == kill || bitmap_empty_p (b))
1803     {
1804       changed = !bitmap_equal_p (dst, a);
1805       if (changed)
1806         bitmap_copy (dst, a);
1807       return changed;
1808     }
1809   if (bitmap_empty_p (kill))
1810     return bitmap_ior (dst, a, b);
1811   if (bitmap_empty_p (a))
1812     return bitmap_and_compl (dst, b, kill);
1813
1814   while (a_elt || b_elt)
1815     {
1816       bool new_element = false;
1817
1818       if (b_elt)
1819         while (kill_elt && kill_elt->indx < b_elt->indx)
1820           kill_elt = kill_elt->next;
1821
1822       if (b_elt && kill_elt && kill_elt->indx == b_elt->indx
1823           && (!a_elt || a_elt->indx >= b_elt->indx))
1824         {
1825           bitmap_element tmp_elt;
1826           unsigned ix;
1827
1828           BITMAP_WORD ior = 0;
1829           tmp_elt.indx = b_elt->indx;
1830           for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1831             {
1832               BITMAP_WORD r = b_elt->bits[ix] & ~kill_elt->bits[ix];
1833               ior |= r;
1834               tmp_elt.bits[ix] = r;
1835             }
1836
1837           if (ior)
1838             {
1839               changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
1840                                         a_elt, &tmp_elt, changed);
1841               new_element = true;
1842               if (a_elt && a_elt->indx == b_elt->indx)
1843                 a_elt = a_elt->next;
1844             }
1845
1846           b_elt = b_elt->next;
1847           kill_elt = kill_elt->next;
1848         }
1849       else
1850         {
1851           changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
1852                                     a_elt, b_elt, changed);
1853           new_element = true;
1854
1855           if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1856             {
1857               a_elt = a_elt->next;
1858               b_elt = b_elt->next;
1859             }
1860           else
1861             {
1862               if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
1863                 a_elt = a_elt->next;
1864               else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
1865                 b_elt = b_elt->next;
1866             }
1867         }
1868
1869       if (new_element)
1870         {
1871           dst_prev = *dst_prev_pnext;
1872           dst_prev_pnext = &dst_prev->next;
1873           dst_elt = *dst_prev_pnext;
1874         }
1875     }
1876
1877   if (dst_elt)
1878     {
1879       changed = true;
1880       /* Ensure that dst->current is valid.  */
1881       dst->current = dst->first;
1882       bitmap_elt_clear_from (dst, dst_elt);
1883     }
1884   gcc_checking_assert (!dst->current == !dst->first);
1885   if (dst->current)
1886     dst->indx = dst->current->indx;
1887
1888   return changed;
1889 }
1890
1891 /* A |= (FROM1 & ~FROM2).  Return true if A changes.  */
1892
1893 bool
1894 bitmap_ior_and_compl_into (bitmap a, const_bitmap from1, const_bitmap from2)
1895 {
1896   bitmap_head tmp;
1897   bool changed;
1898
1899   bitmap_initialize (&tmp, &bitmap_default_obstack);
1900   bitmap_and_compl (&tmp, from1, from2);
1901   changed = bitmap_ior_into (a, &tmp);
1902   bitmap_clear (&tmp);
1903
1904   return changed;
1905 }
1906
1907 /* A |= (B & C).  Return true if A changes.  */
1908
1909 bool
1910 bitmap_ior_and_into (bitmap a, const_bitmap b, const_bitmap c)
1911 {
1912   bitmap_element *a_elt = a->first;
1913   const bitmap_element *b_elt = b->first;
1914   const bitmap_element *c_elt = c->first;
1915   bitmap_element and_elt;
1916   bitmap_element *a_prev = NULL;
1917   bitmap_element **a_prev_pnext = &a->first;
1918   bool changed = false;
1919   unsigned ix;
1920
1921   if (b == c)
1922     return bitmap_ior_into (a, b);
1923   if (bitmap_empty_p (b) || bitmap_empty_p (c))
1924     return false;
1925
1926   and_elt.indx = -1;
1927   while (b_elt && c_elt)
1928     {
1929       BITMAP_WORD overall;
1930
1931       /* Find a common item of B and C.  */
1932       while (b_elt->indx != c_elt->indx)
1933         {
1934           if (b_elt->indx < c_elt->indx)
1935             {
1936               b_elt = b_elt->next;
1937               if (!b_elt)
1938                 goto done;
1939             }
1940           else
1941             {
1942               c_elt = c_elt->next;
1943               if (!c_elt)
1944                 goto done;
1945             }
1946         }
1947
1948       overall = 0;
1949       and_elt.indx = b_elt->indx;
1950       for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1951         {
1952           and_elt.bits[ix] = b_elt->bits[ix] & c_elt->bits[ix];
1953           overall |= and_elt.bits[ix];
1954         }
1955
1956       b_elt = b_elt->next;
1957       c_elt = c_elt->next;
1958       if (!overall)
1959         continue;
1960
1961       /* Now find a place to insert AND_ELT.  */
1962       do
1963         {
1964           ix = a_elt ? a_elt->indx : and_elt.indx;
1965           if (ix == and_elt.indx)
1966             changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, &and_elt, changed);
1967           else if (ix > and_elt.indx)
1968             changed = bitmap_elt_copy (a, NULL, a_prev, &and_elt, changed);
1969
1970           a_prev = *a_prev_pnext;
1971           a_prev_pnext = &a_prev->next;
1972           a_elt = *a_prev_pnext;
1973
1974           /* If A lagged behind B/C, we advanced it so loop once more.  */
1975         }
1976       while (ix < and_elt.indx);
1977     }
1978
1979  done:
1980   gcc_checking_assert (!a->current == !a->first);
1981   if (a->current)
1982     a->indx = a->current->indx;
1983   return changed;
1984 }
1985
1986 /* Compute hash of bitmap (for purposes of hashing).  */
1987 hashval_t
1988 bitmap_hash (const_bitmap head)
1989 {
1990   const bitmap_element *ptr;
1991   BITMAP_WORD hash = 0;
1992   int ix;
1993
1994   for (ptr = head->first; ptr; ptr = ptr->next)
1995     {
1996       hash ^= ptr->indx;
1997       for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1998         hash ^= ptr->bits[ix];
1999     }
2000   return (hashval_t)hash;
2001 }
2002
2003 \f
2004 /* Debugging function to print out the contents of a bitmap.  */
2005
2006 DEBUG_FUNCTION void
2007 debug_bitmap_file (FILE *file, const_bitmap head)
2008 {
2009   const bitmap_element *ptr;
2010
2011   fprintf (file, "\nfirst = " HOST_PTR_PRINTF
2012            " current = " HOST_PTR_PRINTF " indx = %u\n",
2013            (void *) head->first, (void *) head->current, head->indx);
2014
2015   for (ptr = head->first; ptr; ptr = ptr->next)
2016     {
2017       unsigned int i, j, col = 26;
2018
2019       fprintf (file, "\t" HOST_PTR_PRINTF " next = " HOST_PTR_PRINTF
2020                " prev = " HOST_PTR_PRINTF " indx = %u\n\t\tbits = {",
2021                (const void*) ptr, (const void*) ptr->next,
2022                (const void*) ptr->prev, ptr->indx);
2023
2024       for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
2025         for (j = 0; j < BITMAP_WORD_BITS; j++)
2026           if ((ptr->bits[i] >> j) & 1)
2027             {
2028               if (col > 70)
2029                 {
2030                   fprintf (file, "\n\t\t\t");
2031                   col = 24;
2032                 }
2033
2034               fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
2035                                      + i * BITMAP_WORD_BITS + j));
2036               col += 4;
2037             }
2038
2039       fprintf (file, " }\n");
2040     }
2041 }
2042
2043 /* Function to be called from the debugger to print the contents
2044    of a bitmap.  */
2045
2046 DEBUG_FUNCTION void
2047 debug_bitmap (const_bitmap head)
2048 {
2049   debug_bitmap_file (stderr, head);
2050 }
2051
2052 /* Function to print out the contents of a bitmap.  Unlike debug_bitmap_file,
2053    it does not print anything but the bits.  */
2054
2055 DEBUG_FUNCTION void
2056 bitmap_print (FILE *file, const_bitmap head, const char *prefix,
2057               const char *suffix)
2058 {
2059   const char *comma = "";
2060   unsigned i;
2061   bitmap_iterator bi;
2062
2063   fputs (prefix, file);
2064   EXECUTE_IF_SET_IN_BITMAP (head, 0, i, bi)
2065     {
2066       fprintf (file, "%s%d", comma, i);
2067       comma = ", ";
2068     }
2069   fputs (suffix, file);
2070 }
2071
2072 /* Output per-bitmap memory usage statistics.  */
2073 void
2074 dump_bitmap_statistics (void)
2075 {
2076   if (!GATHER_STATISTICS)
2077     return;
2078
2079   bitmap_mem_desc.dump (BITMAP_ORIGIN);
2080 }
2081
2082 DEBUG_FUNCTION void
2083 debug (const bitmap_head &ref)
2084 {
2085   dump_bitmap (stderr, &ref);
2086 }
2087
2088 DEBUG_FUNCTION void
2089 debug (const bitmap_head *ptr)
2090 {
2091   if (ptr)
2092     debug (*ptr);
2093   else
2094     fprintf (stderr, "<nil>\n");
2095 }
2096
2097
2098 #include "gt-bitmap.h"