Repository navigation
Expand file tree
/
Copy pathbitmap.c
More file actions
2870 lines (2426 loc) · 66.6 KB
/
Copy pathbitmap.c
File metadata and controls
2870 lines (2426 loc) · 66.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Functions to support general ended bitmaps.
Copyright (C) 1997-2020 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "bitmap.h"
#include "selftest.h"
/* Memory allocation statistics purpose instance. */
mem_alloc_description<bitmap_usage> bitmap_mem_desc;
/* Static zero-initialized bitmap obstack used for default initialization
of bitmap_head. */
bitmap_obstack bitmap_head::crashme;
static bitmap_element *bitmap_tree_listify_from (bitmap, bitmap_element *);
/* Register new bitmap. */
void
bitmap_register (bitmap b MEM_STAT_DECL)
{
static unsigned alloc_descriptor_max_uid = 1;
gcc_assert (b->alloc_descriptor == 0);
b->alloc_descriptor = alloc_descriptor_max_uid++;
bitmap_mem_desc.register_descriptor (b->get_descriptor (), BITMAP_ORIGIN,
false FINAL_PASS_MEM_STAT);
}
/* Account the overhead. */
static void
register_overhead (bitmap b, size_t amount)
{
unsigned *d = b->get_descriptor ();
if (bitmap_mem_desc.contains_descriptor_for_instance (d))
bitmap_mem_desc.register_instance_overhead (amount, d);
}
/* Release the overhead. */
static void
release_overhead (bitmap b, size_t amount, bool remove_from_map)
{
unsigned *d = b->get_descriptor ();
if (bitmap_mem_desc.contains_descriptor_for_instance (d))
bitmap_mem_desc.release_instance_overhead (d, amount, remove_from_map);
}
/* Global data */
bitmap_element bitmap_zero_bits; /* An element of all zero bits. */
bitmap_obstack bitmap_default_obstack; /* The default bitmap obstack. */
static int bitmap_default_obstack_depth;
static GTY((deletable)) bitmap_element *bitmap_ggc_free; /* Freelist of
GC'd elements. */
/* Bitmap memory management. */
/* Add ELT to the appropriate freelist. */
static inline void
bitmap_elem_to_freelist (bitmap head, bitmap_element *elt)
{
bitmap_obstack *bit_obstack = head->obstack;
if (GATHER_STATISTICS)
release_overhead (head, sizeof (bitmap_element), false);
elt->next = NULL;
elt->indx = -1;
if (bit_obstack)
{
elt->prev = bit_obstack->elements;
bit_obstack->elements = elt;
}
else
{
elt->prev = bitmap_ggc_free;
bitmap_ggc_free = elt;
}
}
/* Allocate a bitmap element. The bits are cleared, but nothing else is. */
static inline bitmap_element *
bitmap_element_allocate (bitmap head)
{
bitmap_element *element;
bitmap_obstack *bit_obstack = head->obstack;
if (bit_obstack)
{
element = bit_obstack->elements;
if (element)
/* Use up the inner list first before looking at the next
element of the outer list. */
if (element->next)
{
bit_obstack->elements = element->next;
bit_obstack->elements->prev = element->prev;
}
else
/* Inner list was just a singleton. */
bit_obstack->elements = element->prev;
else
element = XOBNEW (&bit_obstack->obstack, bitmap_element);
}
else
{
element = bitmap_ggc_free;
if (element)
/* Use up the inner list first before looking at the next
element of the outer list. */
if (element->next)
{
bitmap_ggc_free = element->next;
bitmap_ggc_free->prev = element->prev;
}
else
/* Inner list was just a singleton. */
bitmap_ggc_free = element->prev;
else
element = ggc_alloc<bitmap_element> ();
}
if (GATHER_STATISTICS)
register_overhead (head, sizeof (bitmap_element));
memset (element->bits, 0, sizeof (element->bits));
return element;
}
/* Remove ELT and all following elements from bitmap HEAD.
Put the released elements in the freelist for HEAD. */
void
bitmap_elt_clear_from (bitmap head, bitmap_element *elt)
{
bitmap_element *prev;
bitmap_obstack *bit_obstack = head->obstack;
if (!elt)
return;
if (head->tree_form)
elt = bitmap_tree_listify_from (head, elt);
if (GATHER_STATISTICS)
{
int n = 0;
for (prev = elt; prev; prev = prev->next)
n++;
release_overhead (head, sizeof (bitmap_element) * n, false);
}
prev = elt->prev;
if (prev)
{
prev->next = NULL;
if (head->current->indx > prev->indx)
{
head->current = prev;
head->indx = prev->indx;
}
}
else
{
head->first = NULL;
head->current = NULL;
head->indx = 0;
}
/* Put the entire list onto the freelist in one operation. */
if (bit_obstack)
{
elt->prev = bit_obstack->elements;
bit_obstack->elements = elt;
}
else
{
elt->prev = bitmap_ggc_free;
bitmap_ggc_free = elt;
}
}
/* Linked-list view of bitmaps.
In this representation, the bitmap elements form a double-linked list
with elements sorted by increasing index. */
/* Link the bitmap element into the current bitmap linked list. */
static inline void
bitmap_list_link_element (bitmap head, bitmap_element *element)
{
unsigned int indx = element->indx;
bitmap_element *ptr;
gcc_checking_assert (!head->tree_form);
/* If this is the first and only element, set it in. */
if (head->first == 0)
{
element->next = element->prev = 0;
head->first = element;
}
/* If this index is less than that of the current element, it goes someplace
before the current element. */
else if (indx < head->indx)
{
for (ptr = head->current;
ptr->prev != 0 && ptr->prev->indx > indx;
ptr = ptr->prev)
;
if (ptr->prev)
ptr->prev->next = element;
else
head->first = element;
element->prev = ptr->prev;
element->next = ptr;
ptr->prev = element;
}
/* Otherwise, it must go someplace after the current element. */
else
{
for (ptr = head->current;
ptr->next != 0 && ptr->next->indx < indx;
ptr = ptr->next)
;
if (ptr->next)
ptr->next->prev = element;
element->next = ptr->next;
element->prev = ptr;
ptr->next = element;
}
/* Set up so this is the first element searched. */
head->current = element;
head->indx = indx;
}
/* Unlink the bitmap element from the current bitmap linked list,
and return it to the freelist. */
static inline void
bitmap_list_unlink_element (bitmap head, bitmap_element *element,
bool to_freelist = true)
{
bitmap_element *next = element->next;
bitmap_element *prev = element->prev;
gcc_checking_assert (!head->tree_form);
if (prev)
prev->next = next;
if (next)
next->prev = prev;
if (head->first == element)
head->first = next;
/* Since the first thing we try is to insert before current,
make current the next entry in preference to the previous. */
if (head->current == element)
{
head->current = next != 0 ? next : prev;
if (head->current)
head->indx = head->current->indx;
else
head->indx = 0;
}
if (to_freelist)
bitmap_elem_to_freelist (head, element);
}
/* Insert a new uninitialized element (or NODE if not NULL) into bitmap
HEAD after element ELT. If ELT is NULL, insert the element at the start.
Return the new element. */
static bitmap_element *
bitmap_list_insert_element_after (bitmap head,
bitmap_element *elt, unsigned int indx,
bitmap_element *node = NULL)
{
if (!node)
node = bitmap_element_allocate (head);
node->indx = indx;
gcc_checking_assert (!head->tree_form);
if (!elt)
{
if (!head->current)
{
head->current = node;
head->indx = indx;
}
node->next = head->first;
if (node->next)
node->next->prev = node;
head->first = node;
node->prev = NULL;
}
else
{
gcc_checking_assert (head->current);
node->next = elt->next;
if (node->next)
node->next->prev = node;
elt->next = node;
node->prev = elt;
}
return node;
}
/* Return the element for INDX, or NULL if the element doesn't exist.
Update the `current' field even if we can't find an element that
would hold the bitmap's bit to make eventual allocation
faster. */
static inline bitmap_element *
bitmap_list_find_element (bitmap head, unsigned int indx)
{
bitmap_element *element;
if (head->current == NULL
|| head->indx == indx)
return head->current;
if (head->current == head->first
&& head->first->next == NULL)
return NULL;
/* Usage can be NULL due to allocated bitmaps for which we do not
call initialize function. */
bitmap_usage *usage = NULL;
if (GATHER_STATISTICS)
usage = bitmap_mem_desc.get_descriptor_for_instance (head);
/* This bitmap has more than one element, and we're going to look
through the elements list. Count that as a search. */
if (GATHER_STATISTICS && usage)
usage->m_nsearches++;
if (head->indx < indx)
/* INDX is beyond head->indx. Search from head->current
forward. */
for (element = head->current;
element->next != 0 && element->indx < indx;
element = element->next)
{
if (GATHER_STATISTICS && usage)
usage->m_search_iter++;
}
else if (head->indx / 2 < indx)
/* INDX is less than head->indx and closer to head->indx than to
0. Search from head->current backward. */
for (element = head->current;
element->prev != 0 && element->indx > indx;
element = element->prev)
{
if (GATHER_STATISTICS && usage)
usage->m_search_iter++;
}
else
/* INDX is less than head->indx and closer to 0 than to
head->indx. Search from head->first forward. */
for (element = head->first;
element->next != 0 && element->indx < indx;
element = element->next)
{
if (GATHER_STATISTICS && usage)
usage->m_search_iter++;
}
/* `element' is the nearest to the one we want. If it's not the one we
want, the one we want doesn't exist. */
gcc_checking_assert (element != NULL);
head->current = element;
head->indx = element->indx;
if (element->indx != indx)
element = 0;
return element;
}
/* Splay-tree view of bitmaps.
This is an almost one-to-one the implementatin of the simple top-down
splay tree in Sleator and Tarjan's "Self-adjusting Binary Search Trees".
It is probably not the most efficient form of splay trees, but it should
be good enough to experiment with this idea of bitmaps-as-trees.
For all functions below, the variable or function argument "t" is a node
in the tree, and "e" is a temporary or new node in the tree. The rest
is sufficiently straigh-forward (and very well explained in the paper)
that comment would only clutter things. */
static inline void
bitmap_tree_link_left (bitmap_element * &t, bitmap_element * &l)
{
l->next = t;
l = t;
t = t->next;
}
static inline void
bitmap_tree_link_right (bitmap_element * &t, bitmap_element * &r)
{
r->prev = t;
r = t;
t = t->prev;
}
static inline void
bitmap_tree_rotate_left (bitmap_element * &t)
{
bitmap_element *e = t->next;
t->next = t->next->prev;
e->prev = t;
t = e;
}
static inline void
bitmap_tree_rotate_right (bitmap_element * &t)
{
bitmap_element *e = t->prev;
t->prev = t->prev->next;
e->next = t;
t = e;
}
static bitmap_element *
bitmap_tree_splay (bitmap head, bitmap_element *t, unsigned int indx)
{
bitmap_element N, *l, *r;
if (t == NULL)
return NULL;
bitmap_usage *usage = NULL;
if (GATHER_STATISTICS)
usage = bitmap_mem_desc.get_descriptor_for_instance (head);
N.prev = N.next = NULL;
l = r = &N;
while (indx != t->indx)
{
if (GATHER_STATISTICS && usage)
usage->m_search_iter++;
if (indx < t->indx)
{
if (t->prev != NULL && indx < t->prev->indx)
bitmap_tree_rotate_right (t);
if (t->prev == NULL)
break;
bitmap_tree_link_right (t, r);
}
else if (indx > t->indx)
{
if (t->next != NULL && indx > t->next->indx)
bitmap_tree_rotate_left (t);
if (t->next == NULL)
break;
bitmap_tree_link_left (t, l);
}
}
l->next = t->prev;
r->prev = t->next;
t->prev = N.next;
t->next = N.prev;
return t;
}
/* Link bitmap element E into the current bitmap splay tree. */
static inline void
bitmap_tree_link_element (bitmap head, bitmap_element *e)
{
if (head->first == NULL)
e->prev = e->next = NULL;
else
{
bitmap_element *t = bitmap_tree_splay (head, head->first, e->indx);
if (e->indx < t->indx)
{
e->prev = t->prev;
e->next = t;
t->prev = NULL;
}
else if (e->indx > t->indx)
{
e->next = t->next;
e->prev = t;
t->next = NULL;
}
else
gcc_unreachable ();
}
head->first = e;
head->current = e;
head->indx = e->indx;
}
/* Unlink bitmap element E from the current bitmap splay tree,
and return it to the freelist. */
static void
bitmap_tree_unlink_element (bitmap head, bitmap_element *e)
{
bitmap_element *t = bitmap_tree_splay (head, head->first, e->indx);
gcc_checking_assert (t == e);
if (e->prev == NULL)
t = e->next;
else
{
t = bitmap_tree_splay (head, e->prev, e->indx);
t->next = e->next;
}
head->first = t;
head->current = t;
head->indx = (t != NULL) ? t->indx : 0;
bitmap_elem_to_freelist (head, e);
}
/* Return the element for INDX, or NULL if the element doesn't exist. */
static inline bitmap_element *
bitmap_tree_find_element (bitmap head, unsigned int indx)
{
if (head->current == NULL
|| head->indx == indx)
return head->current;
/* Usage can be NULL due to allocated bitmaps for which we do not
call initialize function. */
bitmap_usage *usage = NULL;
if (GATHER_STATISTICS)
usage = bitmap_mem_desc.get_descriptor_for_instance (head);
/* This bitmap has more than one element, and we're going to look
through the elements list. Count that as a search. */
if (GATHER_STATISTICS && usage)
usage->m_nsearches++;
bitmap_element *element = bitmap_tree_splay (head, head->first, indx);
gcc_checking_assert (element != NULL);
head->first = element;
head->current = element;
head->indx = element->indx;
if (element->indx != indx)
element = 0;
return element;
}
/* Converting bitmap views from linked-list to tree and vice versa. */
/* Splice element E and all elements with a larger index from
bitmap HEAD, convert the spliced elements to the linked-list
view, and return the head of the list (which should be E again), */
static bitmap_element *
bitmap_tree_listify_from (bitmap head, bitmap_element *e)
{
bitmap_element *t, *erb;
/* Detach the right branch from E (all elements with indx > E->indx),
and splay E to the root. */
erb = e->next;
e->next = NULL;
t = bitmap_tree_splay (head, head->first, e->indx);
gcc_checking_assert (t == e);
/* Because E has no right branch, and we rotated it to the root,
the left branch is the new root. */
t = e->prev;
head->first = t;
head->current = t;
head->indx = (t != NULL) ? t->indx : 0;
/* Detach the tree from E, and re-attach the right branch of E. */
e->prev = NULL;
e->next = erb;
/* The tree is now valid again. Now we need to "un-tree" E.
It is imperative that a non-recursive implementation is used
for this, because splay trees have a worst case depth of O(N)
for a tree with N nodes. A recursive implementation could
result in a stack overflow for a sufficiently large, unbalanced
bitmap tree. */
auto_vec<bitmap_element *, 32> stack;
auto_vec<bitmap_element *, 32> sorted_elements;
bitmap_element *n = e;
while (true)
{
while (n != NULL)
{
stack.safe_push (n);
n = n->prev;
}
if (stack.is_empty ())
break;
n = stack.pop ();
sorted_elements.safe_push (n);
n = n->next;
}
gcc_assert (sorted_elements[0] == e);
bitmap_element *prev = NULL;
unsigned ix;
FOR_EACH_VEC_ELT (sorted_elements, ix, n)
{
if (prev != NULL)
prev->next = n;
n->prev = prev;
n->next = NULL;
prev = n;
}
return e;
}
/* Convert bitmap HEAD from splay-tree view to linked-list view. */
void
bitmap_list_view (bitmap head)
{
bitmap_element *ptr;
gcc_assert (head->tree_form);
ptr = head->first;
if (ptr)
{
while (ptr->prev)
bitmap_tree_rotate_right (ptr);
head->first = ptr;
head->first = bitmap_tree_listify_from (head, ptr);
}
head->tree_form = false;
}
/* Convert bitmap HEAD from linked-list view to splay-tree view.
This is simply a matter of dropping the prev or next pointers
and setting the tree_form flag. The tree will balance itself
if and when it is used. */
void
bitmap_tree_view (bitmap head)
{
bitmap_element *ptr;
gcc_assert (! head->tree_form);
ptr = head->first;
while (ptr)
{
ptr->prev = NULL;
ptr = ptr->next;
}
head->tree_form = true;
}
/* Clear a bitmap by freeing all its elements. */
void
bitmap_clear (bitmap head)
{
if (head->first == NULL)
return;
if (head->tree_form)
{
bitmap_element *e, *t;
for (e = head->first; e->prev; e = e->prev)
/* Loop to find the element with the smallest index. */ ;
t = bitmap_tree_splay (head, head->first, e->indx);
gcc_checking_assert (t == e);
head->first = t;
}
bitmap_elt_clear_from (head, head->first);
}
/* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
the default bitmap obstack. */
void
bitmap_obstack_initialize (bitmap_obstack *bit_obstack)
{
if (!bit_obstack)
{
if (bitmap_default_obstack_depth++)
return;
bit_obstack = &bitmap_default_obstack;
}
#if !defined(__GNUC__) || (__GNUC__ < 2)
#define __alignof__(type) 0
#endif
bit_obstack->elements = NULL;
bit_obstack->heads = NULL;
obstack_specify_allocation (&bit_obstack->obstack, OBSTACK_CHUNK_SIZE,
__alignof__ (bitmap_element),
obstack_chunk_alloc,
obstack_chunk_free);
}
/* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
release the default bitmap obstack. */
void
bitmap_obstack_release (bitmap_obstack *bit_obstack)
{
if (!bit_obstack)
{
if (--bitmap_default_obstack_depth)
{
gcc_assert (bitmap_default_obstack_depth > 0);
return;
}
bit_obstack = &bitmap_default_obstack;
}
bit_obstack->elements = NULL;
bit_obstack->heads = NULL;
obstack_free (&bit_obstack->obstack, NULL);
}
/* Create a new bitmap on an obstack. If BIT_OBSTACK is NULL, create
it on the
10A94
default bitmap obstack. */
bitmap
bitmap_alloc (bitmap_obstack *bit_obstack MEM_STAT_DECL)
{
bitmap map;
if (!bit_obstack)
bit_obstack = &bitmap_default_obstack;
map = bit_obstack->heads;
if (map)
bit_obstack->heads = (class bitmap_head *) map->first;
else
map = XOBNEW (&bit_obstack->obstack, bitmap_head);
bitmap_initialize (map, bit_obstack PASS_MEM_STAT);
if (GATHER_STATISTICS)
register_overhead (map, sizeof (bitmap_head));
return map;
}
/* Create a new GCd bitmap. */
bitmap
bitmap_gc_alloc (ALONE_MEM_STAT_DECL)
{
bitmap map;
map = ggc_alloc<bitmap_head> ();
bitmap_initialize (map, NULL PASS_MEM_STAT);
if (GATHER_STATISTICS)
register_overhead (map, sizeof (bitmap_head));
return map;
}
/* Release an obstack allocated bitmap. */
void
bitmap_obstack_free (bitmap map)
{
if (map)
{
bitmap_clear (map);
map->first = (bitmap_element *) map->obstack->heads;
if (GATHER_STATISTICS)
release_overhead (map, sizeof (bitmap_head), true);
map->obstack->heads = map;
}
}
/* Return nonzero if all bits in an element are zero. */
static inline int
bitmap_element_zerop (const bitmap_element *element)
{
#if BITMAP_ELEMENT_WORDS == 2
return (element->bits[0] | element->bits[1]) == 0;
#else
unsigned i;
for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
if (element->bits[i] != 0)
return 0;
return 1;
#endif
}
/* Copy a bitmap to another bitmap. */
void
bitmap_copy (bitmap to, const_bitmap from)
{
const bitmap_element *from_ptr;
bitmap_element *to_ptr = 0;
gcc_checking_assert (!to->tree_form && !from->tree_form);
bitmap_clear (to);
/* Copy elements in forward direction one at a time. */
for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
{
bitmap_element *to_elt = bitmap_element_allocate (to);
to_elt->indx = from_ptr->indx;
memcpy (to_elt->bits, from_ptr->bits, sizeof (to_elt->bits));
/* Here we have a special case of bitmap_list_link_element,
for the case where we know the links are being entered
in sequence. */
if (to_ptr == 0)
{
to->first = to->current = to_elt;
to->indx = from_ptr->indx;
to_elt->next = to_elt->prev = 0;
}
else
{
to_elt->prev = to_ptr;
to_elt->next = 0;
to_ptr->next = to_elt;
}
to_ptr = to_elt;
}
}
/* Move a bitmap to another bitmap. */
void
bitmap_move (bitmap to, bitmap from)
{
gcc_assert (to->obstack == from->obstack);
bitmap_clear (to);
size_t sz = 0;
if (GATHER_STATISTICS)
{
for (bitmap_element *e = to->first; e; e = e->next)
sz += sizeof (bitmap_element);
register_overhead (to, sz);
}
*to = *from;
if (GATHER_STATISTICS)
release_overhead (from, sz, false);
}
/* Clear a single bit in a bitmap. Return true if the bit changed. */
bool
bitmap_clear_bit (bitmap head, int bit)
{
unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
bitmap_element *ptr;
if (!head->tree_form)
ptr = bitmap_list_find_element (head, indx);
else
ptr = bitmap_tree_find_element (head, indx);
if (ptr != 0)
{
unsigned bit_num = bit % BITMAP_WORD_BITS;
unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
bool res = (ptr->bits[word_num] & bit_val) != 0;
if (res)
{
ptr->bits[word_num] &= ~bit_val;
/* If we cleared the entire word, free up the element. */
if (!ptr->bits[word_num]
&& bitmap_element_zerop (ptr))
{
if (!head->tree_form)
bitmap_list_unlink_element (head, ptr);
else
bitmap_tree_unlink_element (head, ptr);
}
}
return res;
}
return false;
}
/* Set a single bit in a bitmap. Return true if the bit changed. */
bool
bitmap_set_bit (bitmap head, int bit)
{
unsigned indx = bit / BITMAP_ELEMENT_ALL_BITS;
bitmap_element *ptr;
if (!head->tree_form)
ptr = bitmap_list_find_element (head, indx);
else
ptr = bitmap_tree_find_element (head, indx);
unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
unsigned bit_num = bit % BITMAP_WORD_BITS;
BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
if (ptr != 0)
{
bool res = (ptr->bits[word_num] & bit_val) == 0;
if (res)
ptr->bits[word_num] |= bit_val;
return res;
}
ptr = bitmap_element_allocate (head);
ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS;
ptr->bits[word_num] = bit_val;
if (!head->tree_form)
bitmap_list_link_element (head, ptr);
else
bitmap_tree_link_element (head, ptr);
return true;
}
/* Return whether a bit is set within a bitmap. */
int
bitmap_bit_p (const_bitmap head, int bit)
{
unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
const bitmap_element *ptr;
unsigned bit_num;
unsigned word_num;
if (!head->tree_form)
ptr = bitmap_list_find_element (const_cast<bitmap> (head), indx);
else
ptr = bitmap_tree_find_element (const_cast<bitmap> (head), indx);
if (ptr == 0)
return 0;
bit_num = bit % BITMAP_WORD_BITS;
word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
return (ptr->bits[word_num] >> bit_num) & 1;
}