forked from facebook/hhvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalias-id-set.cpp
More file actions
250 lines (206 loc) · 6.54 KB
/
alias-id-set.cpp
File metadata and controls
250 lines (206 loc) · 6.54 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
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-2016 Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/vm/jit/alias-id-set.h"
#include "hphp/util/bitops.h"
#include <folly/Bits.h>
#include <folly/Format.h>
#include <utility>
namespace HPHP { namespace jit {
///////////////////////////////////////////////////////////////////////////
/* implicit */ AliasIdSet::AliasIdSet(uint32_t id) {
if (id <= BitsetMax) {
m_bits = Empty | (1ull << id);
} else {
m_bits = id;
}
assertx(checkInvariants());
}
/* implicit */ AliasIdSet::AliasIdSet(IdRange r) {
// More than one element, must use bitset mode.
m_bits = Empty;
set(r);
assertx(checkInvariants());
}
bool AliasIdSet::checkInvariants() const {
if (isBigInteger()) {
// Must use bitset mode for single integers below BitsetMax.
return m_bits > BitsetMax && m_bits < Max;
}
return true;
}
bool AliasIdSet::hasSingleValue() const {
if (isBigInteger()) return true;
if (hasUpperRange()) return false;
auto const x = m_bits & (m_bits - 1);
// popcount(x) = popcount(m_bits) - 1.
return x == Empty;
}
uint32_t AliasIdSet::size() const {
if (isBigInteger()) return 1;
if (hasUpperRange()) return Max;
// MSB is always set in bitset mode; it doesn't count.
return folly::popcount(m_bits) - 1;
}
uint32_t AliasIdSet::singleValue() const {
assertx(hasSingleValue());
if (isBitset()) {
assertx(m_bits != Empty);
uint64_t ret;
DEBUG_ONLY auto const success = ffs64(m_bits, ret);
assertx(success && ret != 63);
return ret; // `n' is represented by the nth bit set, for n < 63
}
assertx(isBigInteger());
return m_bits;
}
void AliasIdSet::set(uint32_t id) {
if (id <= BitsetMax) {
// The only way to represent a small integer is to use the bitset mode.
toBitset().m_bits |= 1ull << id;
return;
}
// Convert an empty bitset to big-integer mode.
if (empty()) {
m_bits = id;
return;
}
if (m_bits == id) return; // Already representing the same integer.
// Convert to bitset mode if needed, and use upper range.
setUpperRange();
}
void AliasIdSet::set(IdRange range) {
if (range.empty()) return;
if (range.hasSingleValue()) return set(range.l);
// Multiple values, must use a bitset.
if (range.h > BitsetMax + 1) setUpperRange();
else toBitset();
// Set lower bits as needed.
if (range.l <= BitsetMax) {
auto const nBits = std::min(BitsetMax + 1u, range.h) - range.l;
m_bits |= ((1ull << nBits) - 1) << range.l;
}
}
void AliasIdSet::unset(uint32_t id) {
if (isBigInteger()) {
// Empty is represented in bitset mode.
if (m_bits == id) m_bits = Empty;
return;
}
// cannot safely unset upper range.
if (id > BitsetMax) return;
m_bits &= ~(1ull << id);
}
bool AliasIdSet::test(uint32_t id) const {
if (isBitset()) {
if (id <= BitsetMax) {
return m_bits & (1ull << id);
}
return hasUpperRange();
}
// Big integer.
return m_bits == id;
}
bool AliasIdSet::maybe(const AliasIdSet other) const {
if (isBigInteger()) {
if (m_bits == other.m_bits) return true;
// If `other' is a big integer, the following will return false.
return other.hasUpperRange();
}
if (other.isBigInteger()) {
return hasUpperRange();
}
// Both are bitsets, including cases when one is empty.
auto r = m_bits & other.m_bits;
// Does r have a bit set other than MSB?
return r & (r - 1);
}
AliasIdSet AliasIdSet::operator|=(const AliasIdSet rhs) {
if (*this == rhs || rhs.empty()) return *this;
if (empty()) {
m_bits = rhs.m_bits;
return *this;
}
if (isBigInteger() || rhs.isBigInteger()) {
// Result contains a big integer, as well as one other integer, so we
// must use bitset mode.
setUpperRange();
}
if (rhs.isBitset()) {
// Both are bitsets.
m_bits |= rhs.m_bits;
}
assertx(checkInvariants());
return *this;
}
bool AliasIdSet::isSubsetOf(const AliasIdSet rhs) const {
if (*this == rhs || empty()) return true;
if (isBigInteger()) {
// If `rhs' is a big integer, the following will return false. We know
// they cannot be the same integer.
return rhs.hasUpperRange();
}
// nonempty bitset.
if (rhs.isBigInteger()) return false;
// Both are bitsets.
return !(m_bits & (~rhs.m_bits));
}
std::string AliasIdSet::toString() const {
assertx(checkInvariants());
if (isBigInteger()) {
return folly::to<std::string>(m_bits);
}
if (empty()) return "None";
if (isAny()) return "Any";
std::string result;
// Try to print the slots by grouping them, expect output like
// '0~4,9,10,50~...'
auto first = true; // whether to avoid priting the separator
int32_t begin = -1; // starting bit of the consecutive range.
// Append slots [begin, end) to result string.
auto const appendRange = [&] (uint32_t end) {
assertx(end > begin);
result += folly::to<std::string>(begin);
if (end == begin + 1) return;
if (end == begin + 2) {
result += folly::to<std::string>(",", begin + 1);
} else {
result += folly::to<std::string>("~", end - 1);
}
};
for (uint32_t i = 0; i <= BitsetMax; ++i) {
if (test(i)) {
if (begin < 0) begin = static_cast<int32_t>(i);
else continue;
} else {
if (begin < 0) continue;
if (!first) result += ",";
appendRange(i);
begin = -1;
first = false;
}
}
if (hasUpperRange()) {
if (!first) result += ",";
if (begin < 0) begin = BitsetMax + 1;
result += folly::to<std::string>(begin, "~...");
} else if (begin >= 0) {
// Append [begin, BitsetMax].
appendRange(BitsetMax + 1);
}
return result;
}
///////////////////////////////////////////////////////////////////////////
}}