summaryrefslogtreecommitdiff
path: root/src/jit/hashbv.h
blob: b07b3d89c8de290bd745656af8dcd5fbbbd34fa0 (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#ifndef HASHBV_H
#define HASHBV_H

#if defined(_M_AMD64) || defined(_M_X86)
#include <xmmintrin.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <windows.h>

//#define TESTING 1

#define LOG2_BITS_PER_ELEMENT 5
#define LOG2_ELEMENTS_PER_NODE 2
#define LOG2_BITS_PER_NODE (LOG2_BITS_PER_ELEMENT + LOG2_ELEMENTS_PER_NODE)

#define BITS_PER_ELEMENT (1 << LOG2_BITS_PER_ELEMENT)
#define ELEMENTS_PER_NODE (1 << LOG2_ELEMENTS_PER_NODE)
#define BITS_PER_NODE (1 << LOG2_BITS_PER_NODE)

#ifdef _TARGET_AMD64_
typedef unsigned __int64 elemType;
typedef unsigned __int64 indexType;
#else
typedef unsigned int elemType;
typedef unsigned int indexType;
#endif

class hashBvNode;
class hashBv;
class hashBvIterator;
class hashBvGlobalData;

typedef void bitAction(indexType);
typedef void nodeAction(hashBvNode*);
typedef void dualNodeAction(hashBv* left, hashBv* right, hashBvNode* a, hashBvNode* b);

#define NOMOREBITS -1

#ifdef DEBUG
inline void pBit(indexType i)
{
    printf("%d ", i);
}
#endif // DEBUG

// ------------------------------------------------------------
//  this is essentially a hashtable of small fixed bitvectors.
//  for any index, bits select position as follows:
//   32                                                      0
// ------------------------------------------------------------
//  | ... ... ... | hash | element in node | index in element |
// ------------------------------------------------------------
//
//
// hashBv
// | // hashtable
// v
// []->node->node->node
// []->node
// []
// []->node->node
//
//

#if TESTING
inline int log2(int number)
{
    int result = 0;
    number >>= 1;
    while (number)
    {
        result++;
        number >>= 1;
    }
    return result;
}
#endif

// return greatest power of 2 that is less than or equal
inline int nearest_pow2(unsigned number)
{
    int result = 0;

    if (number > 0xffff)
    {
        number >>= 16;
        result += 16;
    }
    if (number > 0xff)
    {
        number >>= 8;
        result += 8;
    }
    if (number > 0xf)
    {
        number >>= 4;
        result += 4;
    }
    if (number > 0x3)
    {
        number >>= 2;
        result += 2;
    }
    if (number > 0x1)
    {
        number >>= 1;
        result += 1;
    }
    return 1 << result;
}

class hashBvNode
{
public:
    hashBvNode* next;
    indexType   baseIndex;
    elemType    elements[ELEMENTS_PER_NODE];

public:
    hashBvNode(indexType base);
    hashBvNode()
    {
    }
    static hashBvNode* Create(indexType base, Compiler* comp);
    void Reconstruct(indexType base);
    int numElements()
    {
        return ELEMENTS_PER_NODE;
    }
    void setBit(indexType base);
    void setLowest(indexType numToSet);
    bool getBit(indexType base);
    void clrBit(indexType base);
    bool anySet();
    bool belongsIn(indexType index);
    int  countBits();
    bool anyBits();
    void foreachBit(bitAction x);
    void freeNode(hashBvGlobalData* glob);
    bool sameAs(hashBvNode* other);
    void copyFrom(hashBvNode* other);

    void AndWith(hashBvNode* other);
    void OrWith(hashBvNode* other);
    void XorWith(hashBvNode* other);
    void Subtract(hashBvNode* other);

    elemType AndWithChange(hashBvNode* other);
    elemType OrWithChange(hashBvNode* other);
    elemType XorWithChange(hashBvNode* other);
    elemType SubtractWithChange(hashBvNode* other);

    bool Intersects(hashBvNode* other);

#ifdef DEBUG
    void dump();
#endif // DEBUG
};

class hashBv
{
public:
    // --------------------------------------
    // data
    // --------------------------------------
    hashBvNode** nodeArr;
    hashBvNode*  initialVector[1];

    union {
        Compiler* compiler;
        // for freelist
        hashBv* next;
    };

    unsigned short log2_hashSize;
    // used for heuristic resizing... could be overflowed in rare circumstances
    // but should not affect correctness
    unsigned short numNodes;

public:
    hashBv(Compiler* comp);
    static hashBv* Create(Compiler* comp);
    static void Init(Compiler* comp);
    static hashBv* CreateFrom(hashBv* other, Compiler* comp);
    void hbvFree();
#ifdef DEBUG
    void dump();
    void dumpFancy();
#endif // DEBUG
    __forceinline int hashtable_size()
    {
        return 1 << this->log2_hashSize;
    }

    hashBvGlobalData* globalData();

    static hashBvNode*& nodeFreeList(hashBvGlobalData* globalData);
    static hashBv*& hbvFreeList(hashBvGlobalData* data);

    hashBvNode** getInsertionPointForIndex(indexType index);

private:
    hashBvNode* getNodeForIndexHelper(indexType index, bool canAdd);
    int getHashForIndex(indexType index, int table_size);
    int getRehashForIndex(indexType thisIndex, int thisTableSize, int newTableSize);

    // maintain free lists for vectors
    hashBvNode** getNewVector(int vectorLength);
    int getNodeCount();

public:
    inline hashBvNode* getOrAddNodeForIndex(indexType index)
    {
        hashBvNode* temp = getNodeForIndexHelper(index, true);
        return temp;
    }
    hashBvNode* getNodeForIndex(indexType index);
    void removeNodeAtBase(indexType index);

public:
    void setBit(indexType index);
    void setAll(indexType numToSet);
    bool testBit(indexType index);
    void clearBit(indexType index);
    int  countBits();
    bool anySet();
    void copyFrom(hashBv* other, Compiler* comp);
    void ZeroAll();
    bool CompareWith(hashBv* other);

    void AndWith(hashBv* other);
    void OrWith(hashBv* other);
    void XorWith(hashBv* other);
    void Subtract(hashBv* other);
    void Subtract3(hashBv* other, hashBv* other2);

    void UnionMinus(hashBv* a, hashBv* b, hashBv* c);

    bool AndWithChange(hashBv* other);
    bool OrWithChange(hashBv* other);
    bool OrWithChangeRight(hashBv* other);
    bool OrWithChangeLeft(hashBv* other);
    bool XorWithChange(hashBv* other);
    bool SubtractWithChange(hashBv* other);

    bool Intersects(hashBv* other);

    template <class Action>
    bool MultiTraverseLHSBigger(hashBv* other);
    template <class Action>
    bool MultiTraverseRHSBigger(hashBv* other);
    template <class Action>
    bool MultiTraverseEqual(hashBv* other);
    template <class Action>
    bool MultiTraverse(hashBv* other);

    void InorderTraverse(nodeAction a);
    void InorderTraverseTwo(hashBv* other, dualNodeAction a);

    void Resize(int newSize);
    void Resize();
    void MergeLists(hashBvNode** a, hashBvNode** b);

    bool TooSmall();
    bool TooBig();
    bool IsValid();
};

// --------------------------------------------------------------------
// --------------------------------------------------------------------

class hashBvIterator
{
public:
    unsigned    hashtable_size;
    unsigned    hashtable_index;
    hashBv*     bv;
    hashBvNode* currNode;
    indexType   current_element;
    // base index of current node
    indexType current_base;
    // working data of current element
    elemType current_data;

    hashBvIterator(hashBv* bv);
    void initFrom(hashBv* bv);
    hashBvIterator();
    indexType nextBit();

private:
    void nextNode();
};

class hashBvGlobalData
{
    friend class hashBv;
    friend class hashBvNode;

    hashBvNode* hbvNodeFreeList;
    hashBv*     hbvFreeList;
};

// clang-format off
#define FOREACH_HBV_BIT_SET(index, bv) \
    { \
        for (int hashNum=0; hashNum<(bv)->hashtable_size(); hashNum++) {\
            hashBvNode *node = (bv)->nodeArr[hashNum];\
            while (node) { \
                indexType base = node->baseIndex; \
                for (int el=0; el<node->numElements(); el++) {\
                    elemType _i = 0; \
                    elemType _e = node->elements[el]; \
                    while (_e) { \
                    int _result = BitScanForwardPtr((DWORD *) &_i, _e); \
                        assert(_result); \
                        (index) = base + (el*BITS_PER_ELEMENT) + _i; \
                        _e ^= (elemType(1) << _i);

#define NEXT_HBV_BIT_SET \
                    }\
                }\
                node = node->next; \
            }\
        }\
    } \
//clang-format on

#ifdef DEBUG
void SimpleDumpNode(hashBvNode *n);
void DumpNode(hashBvNode *n);
void SimpleDumpDualNode(hashBv *a, hashBv *b, hashBvNode *n, hashBvNode *m);
#endif // DEBUG

#endif