-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathBitVector.hpp
1525 lines (1351 loc) · 43.8 KB
/
BitVector.hpp
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
/*******************************************************************************
* Copyright IBM Corp. and others 2000
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#ifndef BITVECTOR_INCL
#define BITVECTOR_INCL
#include <stdint.h>
#include <string.h>
#include "env/FilePointerDecl.hpp"
#include "env/TRMemory.hpp"
#include "env/defines.h"
#include "infra/Assert.hpp"
class TR_BitVector;
class TR_BitVectorCursor;
namespace TR { class Compilation; }
#if defined(BITVECTOR_64BIT)
typedef uint64_t chunk_t;
#define BITS_IN_CHUNK 64
#define SHIFT 6
#else
typedef uint32_t chunk_t;
#define BITS_IN_CHUNK 32
#define SHIFT 5
#endif
#define BV_SANITY_CHECK 0
enum TR_BitContainerType
{
singleton,
bitvector
};
class TR_BitContainer
{
public:
TR_ALLOC(TR_Memory::BitVector)
TR_BitContainer() : _bitVector(NULL), _type(bitvector) {}
TR_BitContainer(int32_t index) : _singleBit(index), _type(singleton) {}
TR_BitContainer(TR_BitVector *bv) : _bitVector(bv), _type(bitvector) {}
operator TR_BitVector *()
{
TR_ASSERT(_type == bitvector, "BitContainer cannot be converted to BitVector\n");
return _bitVector;
}
TR_BitVector * getBitVector() { return (_type == bitvector ? _bitVector : NULL); }
// TR_BitVector methods (have extra run time check)
int32_t get(int32_t n);
bool intersects(TR_BitVector& v2);
bool intersects(TR_BitContainer& v2);
bool operator== (TR_BitVector& v2);
bool hasMoreThanOneElement();
bool isEmpty();
bool isSingleValue() { return (_type != bitvector); }
int32_t getSingleValue() { return _singleBit; }
private:
union
{
int32_t _singleBit;
TR_BitVector * _bitVector;
};
TR_BitContainerType _type;
friend class TR_BitVector;
friend class TR_BitContainerIterator;
};
/**
* A simple data structure for use in single-bit dataflow analyses.
*
* The normal BitVector and BitContainer classes used with the dataflow engine
* can have a significant overhead when only a single bit is being propagated
* owing to their backing storage. This class encapsulates a simple bool value
* and implements a BitVector-style interface making it compatible with the
* data flow engine.
*/
class TR_SingleBitContainer
{
public:
TR_ALLOC(TR_Memory::BitVector)
typedef int32_t containerCharacteristic; // used by data flow
static const containerCharacteristic nullContainerCharacteristic = -1;
TR_SingleBitContainer() : _value(false) {}
TR_SingleBitContainer(int64_t initBits, TR_Memory * m, TR_AllocationKind allocKind = heapAlloc) : _value(false) { }
TR_SingleBitContainer(int64_t initBits, TR::Region ®ion) : _value(false) { }
int32_t get(int32_t n) { TR_ASSERT(n == 0, "SingleBitContainers only contain one bit\n"); return _value; }
int32_t get() { return _value; }
void set() { _value = true; }
void set(int32_t n) { TR_ASSERT(n == 0, "SingleBitContainers only contain one bit\n"); _value = true; }
bool isEmpty() { return !_value; }
bool intersects(TR_SingleBitContainer &other) { return _value && other._value; }
bool operator==(TR_SingleBitContainer &other) { return _value == other._value; }
bool operator!=(TR_SingleBitContainer &other) { return !operator==(other); }
void operator|=(TR_SingleBitContainer &other) { _value = _value || other._value; }
void operator&=(TR_SingleBitContainer &other) { _value = _value && other._value; }
void operator-=(TR_SingleBitContainer &other) { if (other._value) { _value = false; } }
void operator=(TR_SingleBitContainer &other) { _value = other._value; }
void setAll(int64_t n) { TR_ASSERT(n < 2, "SingleBitContainers only contain one bit\n"); if (n > 0) { _value = true; } }
void setAll(int64_t m, int64_t n) { if (m == 0 && n == 1) { _value = true; } }
void resetAll(int64_t n) { TR_ASSERT(n < 2, "SingleBitContainers only contain one bit\n"); if (n > 0) { _value = false; } }
void resetAll(int64_t m, int64_t n) { if (m == 0 && n == 1) { _value = false; } }
void empty() { _value = false; }
bool hasMoreThanOneElement() { return false; }
int32_t elementCount() { return 1; }
int32_t numUsedChunks() { return 1; }
int32_t numNonZeroChunks() { return _value ? 1 : 0; }
void print(TR::Compilation *comp, TR::FILE *file = NULL);
private:
bool _value;
};
enum TR_BitVectorGrowable
{
notGrowable,
growable
};
// An optionally growable bit-vector
class TR_BitVector
{
public:
TR_ALLOC(TR_Memory::BitVector)
typedef TR_BitVectorCursor Cursor;
typedef int32_t containerCharacteristic; // used by data flow
static const containerCharacteristic nullContainerCharacteristic = -1;
// Construct an empty bit vector. All bits are initially off.
//
TR_BitVector() : _chunks(NULL), _region(0), _numChunks(0), _firstChunkWithNonZero(0), _lastChunkWithNonZero(-1), _growable(growable) { }
TR_BitVector(TR::Region ®ion) : _chunks(NULL), _region(®ion), _numChunks(0), _firstChunkWithNonZero(0), _lastChunkWithNonZero(-1), _growable(growable) { }
/**
* @brief Constructor to create a new BitVector by reading serialized data from the memory buffer
* @param [in] buffer Memory buffer containing serialized BitVector
*
* @note This method does not check agains buffer over-reads. The caller should ensure that the
* the memory buffer has been populated by calling the serialized() method to avoid buffer over-reads.
* On return the buffer gets updated to point to the location past the serialized data.
* Also see getSizeForSerialization(), serialize().
*/
TR_BitVector(uint8_t * &buffer)
{
TR_SerializedBitVector *sbv = reinterpret_cast<TR_SerializedBitVector *>(buffer);
_firstChunkWithNonZero = sbv->_firstChunkWithNonZero;
_lastChunkWithNonZero = sbv->_lastChunkWithNonZero;
_numChunks = sbv->_numChunks;
buffer += sizeof(TR_SerializedBitVector);
if (_numChunks > 0)
{
size_t chunksSize = _numChunks * sizeof(*_chunks);
_chunks = (chunk_t*) TR_Memory::jitPersistentAlloc(chunksSize, TR_Memory::BitVector);
memcpy(_chunks, buffer, chunksSize);
buffer += chunksSize;
}
else
{
_chunks = NULL;
}
_region = NULL;
}
// Construct a bit vector with a certain number of bits pre-allocated.
// All bits are initially off.
//
TR_BitVector(int64_t initBits, TR_Memory * m, TR_AllocationKind allocKind = heapAlloc, TR_BitVectorGrowable growableOrNot = growable, TR_MemoryBase::ObjectType ot= TR_MemoryBase::BitVector)
{
_chunks = NULL;
_numChunks = getChunkIndex(initBits-1)+1;
_firstChunkWithNonZero = _numChunks;
_lastChunkWithNonZero = -1;
_region = NULL;
switch (allocKind)
{
case heapAlloc:
_region = &(m->heapMemoryRegion());
break;
case stackAlloc:
_region = &(m->currentStackRegion());
break;
case persistentAlloc:
_region = NULL;
break;
default:
TR_ASSERT(false, "Unhandled allocation type!");
}
#ifdef TRACK_TRBITVECTOR_MEMORY
_memoryUsed=sizeof(TR_BitVector);
#endif
if (_numChunks)
{
if (_region)
{
_chunks = (chunk_t*)_region->allocate(_numChunks*sizeof(chunk_t));
}
else
{
TR_ASSERT(allocKind == persistentAlloc, "Should not allocate through trMemory except for persistent memory");
_chunks = (chunk_t*)TR_Memory::jitPersistentAlloc(_numChunks*sizeof(chunk_t), TR_Memory::BitVector);
}
memset(_chunks, 0, _numChunks*sizeof(chunk_t));
#ifdef TRACK_TRBITVECTOR_MEMORY
_memoryUsed += _numChunks*sizeof(chunk_t);
#endif
}
_growable = growableOrNot;
}
TR_BitVector(int64_t initBits, TR::Region ®ion, TR_BitVectorGrowable growableOrNot = growable, TR_MemoryBase::ObjectType ot= TR_MemoryBase::BitVector)
{
_chunks = NULL;
_numChunks = getChunkIndex(initBits-1)+1;
_firstChunkWithNonZero = _numChunks;
_lastChunkWithNonZero = -1;
_region = ®ion;
#ifdef TRACK_TRBITVECTOR_MEMORY
_memoryUsed=sizeof(TR_BitVector);
#endif
if (_numChunks)
{
_chunks = (chunk_t*)_region->allocate(_numChunks*sizeof(chunk_t));
memset(_chunks, 0, _numChunks*sizeof(chunk_t));
#ifdef TRACK_TRBITVECTOR_MEMORY
_memoryUsed += _numChunks*sizeof(chunk_t);
#endif
}
_growable = growableOrNot;
}
void setSize(int64_t n)
{
int32_t chunkIndex = getChunkIndex(n-1);
setChunkSize(chunkIndex+1);
}
// Construct a bit vector from a second bit vector
//
TR_BitVector(const TR_BitVector &v2)
: _chunks(NULL), _numChunks(0), _firstChunkWithNonZero(0), _lastChunkWithNonZero(-1), _growable(growable)
{
_region = v2._region;
*this = v2;
_growable = v2._growable;
}
// Perform a bitwise assignment from TR_BitContainer to TR_BitVector
//
inline TR_BitVector & operator= (TR_BitContainer &bc)
{
if (bc._type == singleton)
{
empty();
set(bc._singleBit);
}
else
{
TR_BitVector &v2 = *(bc._bitVector);
_region = v2._region;
*this = v2;
_growable = v2._growable;
}
return *this;
}
// Initialize or re-initialize the bit vector to have the given size and
// to be empty.
//
void init(int64_t initBits, TR_Memory * m, TR_AllocationKind allocKind = heapAlloc, TR_BitVectorGrowable growableOrNot = notGrowable)
{
if (_chunks && _region == NULL)
jitPersistentFree(_chunks);
_region = NULL;
switch (allocKind)
{
case heapAlloc:
_region = &(m->heapMemoryRegion());
break;
case stackAlloc:
_region = &(m->currentStackRegion());
break;
case persistentAlloc:
_region = NULL;
break;
default:
TR_ASSERT(false, "Unhandled allocation type!");
}
_growable = growable;
_chunks = NULL;
_numChunks = 0;
_firstChunkWithNonZero = 0;
_lastChunkWithNonZero = -1;
setChunkSize(getChunkIndex(initBits-1)+1);
_growable = growableOrNot;
}
void init(int64_t initBits, TR::Region ®ion, TR_BitVectorGrowable growableOrNot = notGrowable)
{
if (_chunks && _region == NULL)
jitPersistentFree(_chunks);
_region = ®ion;
_growable = growable;
_chunks = NULL;
_numChunks = 0;
_firstChunkWithNonZero = 0;
_lastChunkWithNonZero = -1;
setChunkSize(getChunkIndex(initBits-1)+1);
_growable = growableOrNot;
}
// Make sure the vector is large enough to hold the given number of bits.
//
void ensureBits(int64_t n)
{
int32_t chunkIndex = getChunkIndex(n);
if (chunkIndex >= _numChunks)
setChunkSize(chunkIndex+1);
#if BV_SANITY_CHECK
sanityCheck("ensureBits");
#endif
}
// Get the value of the nth bit. The word returned is non-zero if the bit
// is set and is zero if the bit is not set.
//
int32_t get(int64_t n)
{
int32_t chunkIndex = getChunkIndex(n);
if (chunkIndex > _lastChunkWithNonZero)
return 0;
return (_chunks[chunkIndex] & getBitMask(static_cast<int32_t>(n))) != 0;
}
bool isSet(int64_t n)
{
return get(n) != 0;
}
// Set the value of the nth bit.
//
void set(int64_t n)
{
TR_ASSERT(n >= 0, "assertion failure");
int32_t chunkIndex = getChunkIndex(n);
if (chunkIndex >= _numChunks)
setChunkSize(chunkIndex+1);
if (chunkIndex < _firstChunkWithNonZero)
_firstChunkWithNonZero = chunkIndex;
if (chunkIndex > _lastChunkWithNonZero)
_lastChunkWithNonZero = chunkIndex;
_chunks[chunkIndex] |= getBitMask(static_cast<int32_t>(n));
#if BV_SANITY_CHECK
sanityCheck("set");
#endif
}
void setTo(int64_t n, bool value)
{
if (value)
set(n);
else
reset(n);
}
int64_t getHighestBitPosition()
{
if (_lastChunkWithNonZero < 0)
return 0;
int chunkIndex = _lastChunkWithNonZero;
for (int bitIndex = BITS_IN_CHUNK-1; bitIndex >= 0; bitIndex--)
if (_chunks[chunkIndex] & getBitMask(bitIndex))
return getIndexInChunk(bitIndex) + getBitIndex(chunkIndex);
return 0;
}
// Reset the value of the nth bit.
//
void reset(int64_t n, bool updateLowHigh = true)
{
int32_t chunkIndex = getChunkIndex(n);
if (chunkIndex > _lastChunkWithNonZero || chunkIndex < _firstChunkWithNonZero)
return;
if (_chunks[chunkIndex]) {
_chunks[chunkIndex] &= ~getBitMask(static_cast<int32_t>(n));
if (updateLowHigh && _chunks[chunkIndex] == 0)
resetLowAndHighChunks(_firstChunkWithNonZero, _lastChunkWithNonZero);
}
#if BV_SANITY_CHECK
sanityCheck("reset");
#endif
}
// like reset except that it tells you if the
// value had been set
bool clear(int64_t n)
{
bool rc = get(n) != 0;
if (rc)
reset(n);
#if BV_SANITY_CHECK
sanityCheck("clear");
#endif
return rc;
}
// Copy a second vector into this one
//
void operator= (const TR_BitVector& v2)
{
int32_t i;
int32_t v2Used = v2._numChunks;
// Grow the this vector if smaller than the 2nd vector
if (_numChunks < v2Used)
{
setChunkSize(v2Used);
}
int32_t high = v2._lastChunkWithNonZero;
if (high < 0)
{
// Other vector is empty ... empty this one
empty();
}
else
{
// Copy all of the used words from the 2nd vector
int32_t low = v2._firstChunkWithNonZero;
for (i = _firstChunkWithNonZero; i < low; i++)
_chunks[i] = 0;
for (i = low ; i <= high; i++)
_chunks[i] = v2._chunks[i];
for (i = high+1; i <= _lastChunkWithNonZero; i++)
_chunks[i] = 0;
_firstChunkWithNonZero = low;
_lastChunkWithNonZero = high;
}
#if BV_SANITY_CHECK
sanityCheck("assign");
#endif
}
// Perform a bitwise OR between this vector and a second vector
// Results are in the this vector
//
void operator|= (TR_BitVector& v2)
{
if (v2._lastChunkWithNonZero < 0)
return; // other is empty
// Grow the this vector if smaller than the 2nd vector
int32_t v2Used = v2._numChunks;
if (_numChunks < v2Used)
setChunkSize(v2Used);
// OR in all of the words from the 2nd vector
for (int32_t i = v2._firstChunkWithNonZero; i <= v2._lastChunkWithNonZero; i++)
_chunks[i] |= v2._chunks[i];
if (_firstChunkWithNonZero > v2._firstChunkWithNonZero)
_firstChunkWithNonZero = v2._firstChunkWithNonZero;
if (_lastChunkWithNonZero < v2._lastChunkWithNonZero)
_lastChunkWithNonZero = v2._lastChunkWithNonZero;
#if BV_SANITY_CHECK
sanityCheck("operator|=");
#endif
}
void operator|= (TR_BitContainer& v2)
{
if (v2._type == singleton)
set(v2._singleBit);
else if (v2._bitVector)
*this |= *v2._bitVector;
}
// Perform a bitwise AND between this vector and a second vector
// Results are in the this vector
//
void operator&= (TR_BitVector& v2)
{
if (_lastChunkWithNonZero < 0)
return; // Already empty
int32_t low = v2._firstChunkWithNonZero;
int32_t high = v2._lastChunkWithNonZero;
if (high < _firstChunkWithNonZero || low > _lastChunkWithNonZero)
{
// No intersection
this->empty();
#if BV_SANITY_CHECK
sanityCheck("operator&=");
#endif
return;
}
// Clear all the chunks before and after those set in the other vector
int32_t i;
if (low < _firstChunkWithNonZero)
low = _firstChunkWithNonZero;
else
{
for (i =_firstChunkWithNonZero; i < low; i++)
_chunks[i] = 0;
}
if (high > _lastChunkWithNonZero)
high = _lastChunkWithNonZero;
else
{
for (i = _lastChunkWithNonZero; i > high; i--)
_chunks[i] = 0;
}
// AND in all of the words from the 2nd vector
for (i = low; i <= high; i++)
_chunks[i] &= v2._chunks[i];
// Reset first and last chunks with non-zero
resetLowAndHighChunks(low, high);
#if BV_SANITY_CHECK
sanityCheck("operator&=");
#endif
}
// Determine if any bit is set in both this vector and a second vector.
//
bool intersects(TR_BitVector& v2)
{
if (_lastChunkWithNonZero < 0)
return false; // empty - no intersection
int32_t low = v2._firstChunkWithNonZero;
int32_t high = v2._lastChunkWithNonZero;
if (high < _firstChunkWithNonZero || low > _lastChunkWithNonZero)
return false; // No intersection
// AND in all of the words from the 2nd vector
if (low < _firstChunkWithNonZero)
low = _firstChunkWithNonZero;
if (high > _lastChunkWithNonZero)
high = _lastChunkWithNonZero;
for (int32_t i = low; i<= high; i++)
if (_chunks[i] & v2._chunks[i])
return true;
return false;
}
// Perform a bitwise negation (AND-NOT) between this vector and a second vector
// Results are in the this vector
//
void operator-= (TR_BitVector& v2)
{
if (_lastChunkWithNonZero < 0)
return;
int32_t low = v2._firstChunkWithNonZero;
int32_t high = v2._lastChunkWithNonZero;
if (high < _firstChunkWithNonZero || low > _lastChunkWithNonZero)
return; // No intersection
// AND in the logical NOT of all of the words from the 2nd vector
if (low < _firstChunkWithNonZero)
low = _firstChunkWithNonZero;
if (high > _lastChunkWithNonZero)
high = _lastChunkWithNonZero;
for (int32_t i = low; i<= high; i++)
_chunks[i] &= ~v2._chunks[i];
// Reset first and last chunks with non-zero
resetLowAndHighChunks(_firstChunkWithNonZero, _lastChunkWithNonZero);
#if BV_SANITY_CHECK
sanityCheck("operator-=");
#endif
}
void operator-= (TR_BitContainer& v2)
{
if (v2._type == singleton)
reset(v2._singleBit);
else if (v2._bitVector)
*this -= *v2._bitVector;
}
// mixed type operations and conversions
template <class BitVector>
TR_BitVector & operator= (const BitVector &sparse);
template <class BitVector>
TR_BitVector & operator-= (const BitVector &sparse);
template <class BitVector>
TR_BitVector & operator&= (const BitVector &sparse);
template <class BitVector>
TR_BitVector & operator|= (const BitVector &sparse);
// Compare this vector with another
//
bool operator== (TR_BitVector& v2)
{
if (_lastChunkWithNonZero != v2._lastChunkWithNonZero)
return false;
if (_lastChunkWithNonZero < 0)
return true; // Both empty
// Now both are non-empty
if (_firstChunkWithNonZero != v2._firstChunkWithNonZero)
return false;
if (_lastChunkWithNonZero != v2._lastChunkWithNonZero)
return false;
for (int32_t i = _firstChunkWithNonZero; i <= _lastChunkWithNonZero; i++)
if (_chunks[i] != v2._chunks[i])
return false;
return true;
}
bool operator!= (TR_BitVector& v2){ return !operator==(v2); }
// Set the first n elements of the set
//
void setAll(int64_t n)
{
if (n <= 0)
return;
int32_t i;
int32_t chunkIndex = getChunkIndex(n-1);
if (chunkIndex >= _numChunks)
setChunkSize(chunkIndex+1);
for (i = chunkIndex-1; i >= 0; i--)
_chunks[i] = (chunk_t)-1;
for (i = static_cast<int32_t>(getBitIndex(chunkIndex)); i < n; i++)
_chunks[chunkIndex] |= getBitMask(i);
_firstChunkWithNonZero = 0;
if (_lastChunkWithNonZero < chunkIndex)
_lastChunkWithNonZero = chunkIndex;
#if BV_SANITY_CHECK
sanityCheck("setAll(n)");
#endif
}
// Set elements m to n of the set
//
void setAll(int64_t m, int64_t n)
{
int32_t firstChunk = getChunkIndex(m);
int32_t lastChunk = getChunkIndex(n);
if (lastChunk >= _numChunks)
{
setChunkSize(lastChunk+1);
}
if (firstChunk < _firstChunkWithNonZero)
_firstChunkWithNonZero = firstChunk;
if (_lastChunkWithNonZero < lastChunk)
_lastChunkWithNonZero = lastChunk;
// Special case - we're only modifying part of one chunk
//
if (firstChunk == lastChunk)
{
for (int32_t i = getIndexInChunk(m); i <= getIndexInChunk(n); i++)
_chunks[firstChunk] |= getBitMask(i);
#if BV_SANITY_CHECK
sanityCheck("setAll(m,n)");
#endif
return;
}
if (firstChunk == lastChunk)
{
TR_ASSERT(false, "this code is not used");
_chunks[firstChunk] |= getBitMask(static_cast<int32_t>(m), static_cast<int32_t>(n));
}
else
{
// Set first part-chunk bits
//
int32_t i = getIndexInChunk(m);
if (i > 0)
{
for ( ; i < BITS_IN_CHUNK; i++)
_chunks[firstChunk] |= getBitMask(i);
}
else
{
_chunks[firstChunk] |= (chunk_t)-1;
}
// Set last part-chunk bits
//
i = getIndexInChunk(n);
if (i < (BITS_IN_CHUNK-1))
{
for ( ; i >= 0; i--)
_chunks[lastChunk] |= getBitMask(i);
}
else
{
_chunks[lastChunk] |= (chunk_t)-1;
}
// Set the complete chunks
//
for (i = firstChunk+1; i < lastChunk; i++)
_chunks[i] = (chunk_t)-1;
}
#if BV_SANITY_CHECK
sanityCheck("setAll(m,n)");
#endif
}
// Reset all elements of the set (i.e. empty the set)
//
void empty()
{
for (int32_t i = _firstChunkWithNonZero; i <= _lastChunkWithNonZero; i++)
_chunks[i] = 0;
_firstChunkWithNonZero = _numChunks;
_lastChunkWithNonZero = -1;
#if BV_SANITY_CHECK
sanityCheck("empty");
#endif
}
// Reset all elements m to n of the set
//
void resetAll(int64_t m, int64_t n)
{
int32_t i;
int32_t firstChunk = getChunkIndex(m);
int32_t lastChunk = getChunkIndex(n);
if (_lastChunkWithNonZero < firstChunk || _firstChunkWithNonZero > lastChunk)
return; // All relevant bits are already reset
if (lastChunk >= _numChunks)
{
setChunkSize(getChunkIndex(n));
}
if (firstChunk == lastChunk)
{
_chunks[firstChunk] &= ~getBitMask(static_cast<int32_t>(m), static_cast<int32_t>(n));
}
else
{
// Reset first part-chunk bits
//
i = getIndexInChunk(m);
if (i > 0)
{
for ( ; i < BITS_IN_CHUNK; i++)
_chunks[firstChunk] &= ~getBitMask(i);
}
else
{
_chunks[firstChunk] = 0;
}
// Reset last part-chunk bits
//
i = getIndexInChunk(n);
if (i < (BITS_IN_CHUNK-1))
{
for ( ; i >= 0; i--)
_chunks[lastChunk] &= ~getBitMask(i);
}
else
{
_chunks[lastChunk] = 0;
}
// Set the complete chunks
//
for (i = firstChunk+1; i < lastChunk; i++)
_chunks[i] = 0;
}
// Reset first and last chunks with non-zero
resetLowAndHighChunks(_firstChunkWithNonZero, _lastChunkWithNonZero);
#if BV_SANITY_CHECK
sanityCheck("resetAll");
#endif
}
// Check if the set is empty
//
bool isEmpty()
{
return _lastChunkWithNonZero < 0;
}
bool hasMoreThanOneElement();
// Find the number of elements in the set
//
int32_t elementCount();
// Find the number of common elements in the sets
//
int32_t commonElementCount(TR_BitVector&);
// Pack the bit vector into the smallest possible representation
//
void pack()
{
setChunkSize(numUsedChunks());
}
// Print the bit vector to the log file
//
void print(TR::Compilation *comp, TR::FILE *file = NULL);
// Find the number of used chunks
//
int32_t numUsedChunks()
{
return _lastChunkWithNonZero+1;
}
int32_t numNonZeroChunks()
{
if (_lastChunkWithNonZero < 0)
return 0;
return _lastChunkWithNonZero-_firstChunkWithNonZero+1;
}
int32_t numChunks()
{
return _numChunks;
}
int32_t chunkSize()
{
return sizeof(chunk_t);
}
#ifdef TRACK_TRBITVECTOR_MEMORY
uint32_t MemoryUsage()
{
return _memoryUsed;
}
#endif
/**
* @brief Computes number of bytes required for serializing this object
*
* @return Number of bytes required for serializing this object
*/
uint32_t getSizeForSerialization() const
{
uint32_t size = sizeof(TR_SerializedBitVector);
if (_numChunks > 0)
{
size += (_numChunks * sizeof(*_chunks));
}
return size;
}
/**
* @brief Serialize this object
* @param [in] serializer used for serializing this object
*
* @note The caller should ensure buffer is large enough to accommodate the serialized data.
* On return the buffer gets updated to point to the location past the serialized data.
* Also see getSizeForSerialization(), TR_BitVector(uint8_t * &).
*/
void serialize(uint8_t * &buffer) const
{
TR_SerializedBitVector *sbv = reinterpret_cast<TR_SerializedBitVector *>(buffer);
sbv->_firstChunkWithNonZero = _firstChunkWithNonZero;
sbv->_lastChunkWithNonZero = _lastChunkWithNonZero;
sbv->_numChunks = _numChunks;
buffer += sizeof(TR_SerializedBitVector);
if (_numChunks > 0)
{
size_t chunksSize = _numChunks * sizeof(*_chunks);
memcpy(buffer, _chunks, chunksSize);
buffer += chunksSize;
}
}
private:
/**
* This data structure contains all the fields required for serializing TR_BitVector.
*/
struct TR_SerializedBitVector
{
int32_t _firstChunkWithNonZero;
int32_t _lastChunkWithNonZero;
int32_t _numChunks;
};
// Each chunk of bits is an unsigned word, which is 32/64 bits.
// The low order 5/6 bits of a bit index define the bit position within the
// chunk and the rest define the chunk index.
chunk_t *_chunks;
TR::Region *_region;
#ifdef TRACK_TRBITVECTOR_MEMORY
uint32_t _memoryUsed;
#endif
int32_t _numChunks;
// _firstChunkWithNonZero and _lastChunkWithNonero must be maintained precisely
int32_t _firstChunkWithNonZero; // == _numChunks if empty
int32_t _lastChunkWithNonZero; // == -1 if empty
TR_BitVectorGrowable _growable;
friend class TR_BitVectorIterator;
friend class CS2_TR_BitVector;
// Re-calculate the first and last chunks with non-zero
void resetLowAndHighChunks(int32_t low, int32_t high)
{
for ( ; low <= high &&_chunks[low] == 0; low++)
;
if (low > high)
{
// vector is now empty
_firstChunkWithNonZero = _numChunks;
_lastChunkWithNonZero = -1;
return;
}
_firstChunkWithNonZero = low;
// Recalculate last chunk with non-zero
// Should stop at least at _firstChunkWithNonZero
for ( ; _chunks[high] == 0; high--)
;
_lastChunkWithNonZero = high;
}
#if BV_SANITY_CHECK
void sanityCheck(const char *source)
{
// Calculate the correct values for _firstChunkWithNonZero and _lastChunkWithNonZero
// and check them against the stored values
int32_t low, high;
int32_t i;
if (!_chunks)
{
low = 0;
high = -1;
}
else
{
for (i = 0; i < _numChunks && _chunks[i] == 0; i++)
;
low = i;
if (i >= _numChunks)
high = -1;
else
{
for (i = _numChunks-1; i >= low && _chunks[i] == 0; i--)
;
high = i;
}
}
if (low != _firstChunkWithNonZero || high != _lastChunkWithNonZero)
{
printf("TR_BitVector sanity check failed at %s\n", source);
printf("TR_BitVector Low is %d, should be %d\n", _firstChunkWithNonZero, low);
printf("TR_BitVector High is %d, should be %d, num chunks = %d\n", _lastChunkWithNonZero, high, _numChunks);
TR_ASSERT(0, "TR_BitVector sanity check failed");
}
}
#endif
// Given a bit index, calculate the chunk index
//
static int32_t getChunkIndex(int64_t bitIndex)
{
#ifdef DEBUG
TR_ASSERT((((bitIndex >> SHIFT) & ~((int64_t)0x7fffffff)) == 0) || (bitIndex < 0), "Chunk index out of int32_t range. bitIndex=%lx\n",bitIndex);
#endif
return static_cast<int32_t>(bitIndex >> SHIFT);
}