-
Notifications
You must be signed in to change notification settings - Fork 1
/
ir.h
537 lines (430 loc) · 12 KB
/
ir.h
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
#include <cstdint>
#include <cassert>
#include <cstring>
#include <string>
#include <stack>
#include <iostream>
#include <array>
#include <functional>
#pragma pack(push, 1) // exact fit - no padding
class Node {
public:
enum class Type : char {
Constant,
Add,
};
inline size_t realSize() const;
const Type type;
Node(const Type type) : type(type) {}
virtual void print(std::ostream& os) const = 0;
friend std::ostream& operator<<(std::ostream& os, const Node& n) {
n.print(os);
return os;
}
virtual void adjustOffset(Node * n) {
*(Node**)n = this;
}
};
class Constant : public Node {
public:
const int value;
Constant(int value) : Node(Type::Constant), value(value) {}
void print(std::ostream& os) const override {
os << value;
}
};
class Add : public Node {
intptr_t offset_l;
intptr_t offset_r;
public:
Add(Node* l, Node* r) : Node(Type::Add),
offset_l((intptr_t)l - (intptr_t)this),
offset_r((intptr_t)r - (intptr_t)this) {}
Node* l() const {
return (Node*)((intptr_t)this + offset_l);
}
Node* r() const {
return (Node*)((intptr_t)this + offset_r);
}
void print(std::ostream& os) const override {
os << *l() << " + " << *r();
}
void adjustOffset(Node* old_) override {
Add* old = static_cast<Add*>(old_);
Node * up_r = *(Node**)old->r();
Node * up_l = *(Node**)old->l();
offset_l = (intptr_t)up_l - (intptr_t)this;
offset_r = (intptr_t)up_r - (intptr_t)this;
Node::adjustOffset(old_);
}
};
size_t Node::realSize() const {
switch(type) {
case Type::Constant: return sizeof(Constant);
case Type::Add: return sizeof(Add);
}
assert(false);
return -1;
}
#pragma pack(pop) // exact fit - no padding
class NodeList {
/* The Node List consists of blocks and cells:
* - Each block is a NodeList, with ->next pointing to the next one
* - Each block contains a variable number of cells
* - Each cell contains a gap and Node. The gap is a pointer, the node is
* an instruction Node stored inline.
* - If the gap is not null, it points to a sublist (also a NodeList) which
* contains intermediate nodes
*/
static constexpr size_t defaultSize = 128*1024;
static constexpr size_t gapSize = sizeof(NodeList*);
uintptr_t buf;
uintptr_t pos;
bool full = false;
NodeList* nextFree = nullptr;
const uintptr_t size;
// iterate over cells in one block
class flatIterator {
public:
uintptr_t finger_;
flatIterator(uintptr_t start) : finger_(start) {}
inline static flatIterator begin(NodeList* list) {
return flatIterator(list->buf + gapSize);
}
inline static flatIterator end(NodeList* list) {
return flatIterator(list->pos);
}
inline static flatIterator invalid() {
return flatIterator(-1);
}
bool operator == (const flatIterator & other) const {
return finger_ == other.finger_;
}
inline bool operator != (const flatIterator & other) const {
return finger_ != other.finger_;
}
inline void operator ++ () {
auto node = get();
finger_ += gapSize + node->realSize();
}
void setGap(NodeList* gap) {
*reinterpret_cast<NodeList**>(finger_ - gapSize) = gap;
}
inline bool hasGap() {
return *reinterpret_cast<NodeList**>(finger_ - gapSize) != nullptr;
}
inline NodeList* gap() {
return *reinterpret_cast<NodeList**>(finger_ - gapSize);
}
inline Node* get() {
return reinterpret_cast<Node*>(finger_);
}
};
// Iterate all cells of the current and all ->next lists
class nextIterator {
public:
flatIterator it_;
flatIterator end_;
NodeList* cur_;
nextIterator(flatIterator start, flatIterator end, NodeList* list) :
it_(start), end_(end), cur_(list) {}
inline static nextIterator begin(NodeList* list) {
return nextIterator(
flatIterator::begin(list), flatIterator::end(list), list);
}
inline static nextIterator& end() {
static nextIterator theEnd(
flatIterator::invalid(), flatIterator::invalid(), nullptr);
return theEnd;
}
inline void operator ++ () {
++it_;
if (it_ != end_) return;
cur_ = cur_->next;
if (cur_) {
it_ = flatIterator::begin(cur_);
end_ = flatIterator::end(cur_);
}
}
inline bool operator != (const nextIterator& other) const {
// Other is the end marker -> we are equal to the end marker
// if we are at end.
if (!other.cur_)
return it_ != end_;
if (!cur_)
return other.it_ != other.end_;
return it_ != other.it_;
}
void setGap(NodeList* gap) {
it_.setGap(gap);
}
bool hasGap() {
return it_.hasGap();
}
NodeList* gap() {
return it_.gap();
}
Node* get() {
return it_.get();
}
};
// This class is used to access the gaps
class Gaps {
// Keeps a list of gaps in this NodeList for faster dtr and
// totalSize(). If the list overflows we go the slow way and
// find the sublists by inspecting all gaps.
class GapsCache {
static constexpr unsigned gapsCacheSize = 8;
unsigned numGaps = 0;
std::array<NodeList*, gapsCacheSize> gapsCache;
public:
void add(NodeList* l) {
if (numGaps < gapsCacheSize)
gapsCache[numGaps] = l;
numGaps++;
}
bool overflow() {
return numGaps > gapsCacheSize;
}
std::array<NodeList*, gapsCacheSize>::iterator begin() {
return gapsCache.begin();
}
std::array<NodeList*, gapsCacheSize>::iterator end() {
auto it = begin();
std::advance(it, numGaps);
return it;
}
};
GapsCache gapsCache;
// Iterate over all gaps in one block of the list
class gapIterator {
flatIterator it_;
flatIterator end_;
void findNextGap() {
while (it_ != end_ && !it_.hasGap())
++it_;
}
gapIterator(flatIterator begin, flatIterator end) : it_(begin),
end_(end) {
findNextGap();
}
public:
inline static gapIterator begin(NodeList* list) {
return gapIterator(flatIterator::begin(list), flatIterator::end(list));
}
inline static gapIterator end(NodeList* list) {
return gapIterator(flatIterator::end(list), flatIterator::end(list));
}
inline bool operator != (const gapIterator & other) const {
return it_ != other.it_;
}
inline void operator ++ () {
++it_;
findNextGap();
}
inline NodeList* operator * () {
return it_.gap();
}
};
public:
void add(NodeList* l) {
gapsCache.add(l);
}
void foreach(NodeList* parent, std::function<void(NodeList*)> f) {
if (gapsCache.overflow()) {
auto end = gapIterator::end(parent);
for (auto i = gapIterator::begin(parent); i != end; ++i) {
f(*i);
}
} else {
for (auto g : gapsCache) {
f(g);
}
}
}
};
Gaps gaps;
NodeList* next = nullptr;
size_t totalSize() {
size_t sum = size;
gaps.foreach(this, [&sum](NodeList* gap) {
sum += gap->totalSize();
});
if (next)
sum += next->totalSize();
return sum;
}
inline void* prepareInsert(size_t s) {
if (nextFree && !nextFree->full) {
return nextFree->prepareInsert(s);
}
uintptr_t next_pos = pos + s + gapSize;
if (next_pos < buf + size) {
void* res = (void*)pos;
pos = next_pos;
*(NodeList**)(pos - gapSize) = nullptr;
return res;
}
return prepareInsertSlow(s);
}
void* prepareInsertSlow(size_t s) {
full = true;
NodeList* cur = this;
while(cur->full && cur->next) {
cur = cur->next;
}
if (cur == this) {
next = nextFree = new NodeList();
return next->prepareInsert(s);
}
nextFree = cur;
return nextFree->prepareInsert(s);
}
public:
NodeList(size_t initSize = defaultSize) : size(initSize) {
buf = (uintptr_t)new char[initSize];
*(NodeList**)buf = nullptr;
pos = buf + gapSize;
}
~NodeList() {
gaps.foreach(this, [](NodeList* gap) {
delete gap;
});
if (next) delete next;
delete[] (char*)buf;
}
template<typename Node>
Node* insert() {
return new(prepareInsert(sizeof(Node))) Node();
}
template<typename Node, typename Arg1>
Node* insert(Arg1 arg1) {
return new(prepareInsert(sizeof(Node))) Node(arg1);
}
template<typename Node, typename Arg1, typename Arg2>
Node* insert(Arg1 arg1, Arg2 arg2) {
return new(prepareInsert(sizeof(Node))) Node(arg1, arg2);
}
NodeList* flatten() {
auto flat = new NodeList(totalSize());
// Bulk copy, to avoid doing insert(Node*) for every element
auto fixup = [](uintptr_t old_start,
uintptr_t old_end,
uintptr_t new_start) -> uintptr_t {
Node* last = (Node*) old_end;
size_t last_size = last->realSize();
size_t s = old_end - old_start + last_size;
memcpy((void*)new_start, (void*)old_start, s);
uintptr_t finger_new = new_start;
uintptr_t finger_old = old_start;
while (finger_old <= old_end) {
Node* old = (Node*)finger_old;
Node* copy = (Node*)finger_new;
size_t s = copy->realSize();
copy->adjustOffset(old);
*((NodeList**)(finger_new - gapSize)) = nullptr;
finger_old += s + gapSize;
finger_new += s + gapSize;
}
*((NodeList**)(finger_new - gapSize)) = nullptr;
return finger_new;
};
auto i = begin();
NodeList* cur = i.cur();
uintptr_t bulkFixupStart = i.curFinger();
uintptr_t bulkFixupEnd = i.curFinger();
for (; i != end(); ++i) {
if (cur != i.cur()) {
flat->pos = fixup(bulkFixupStart, bulkFixupEnd, flat->pos);
cur = i.cur();
bulkFixupStart = i.curFinger();
}
bulkFixupEnd = i.curFinger();
}
flat->pos = fixup(bulkFixupStart, bulkFixupEnd, flat->pos);
delete this;
return flat;
}
// recursively iterate over all nodes: visits all gaps and next blocks
class iterator {
nextIterator it_;
nextIterator end_;
std::stack<nextIterator> worklist_;
inline bool atListEnd() {
return !(it_ != end_);
}
inline void findStart() {
while (it_.hasGap()) {
nextIterator n = nextIterator::begin(it_.gap());
if (n != nextIterator::end()) {
worklist_.push(it_);
it_ = n;
} else {
break;
}
}
}
inline void popNext() {
if (!worklist_.empty()) {
it_ = worklist_.top();
worklist_.pop();
}
}
iterator(nextIterator start, nextIterator end) : it_(start), end_(end) {
if (start != nextIterator::end())
findStart();
}
inline NodeList* insertBefore(NodeList* p) {
if (!it_.hasGap()) {
it_.setGap(new NodeList);
p->gaps.add(it_.gap());
}
return it_.gap();
}
friend class NodeList;
public:
inline static iterator begin(NodeList* cur) {
return iterator(nextIterator::begin(cur), nextIterator::end());
}
inline static iterator& end() {
static iterator theEnd(nextIterator::end(), nextIterator::end());
return theEnd;
}
inline void operator ++ () {
++it_;
if (atListEnd()) {
popNext();
} else {
findStart();
}
}
inline bool operator != (const iterator& other) const {
return it_ != other.it_;
}
inline Node* operator * () {
return it_.get();
}
NodeList* cur() {
return it_.cur_;
}
uintptr_t curFinger() {
return it_.it_.finger_;
}
};
NodeList* insertBefore(iterator i) {
return i.insertBefore(this);
}
iterator begin() {
return iterator::begin(this);
}
inline const iterator& end() {
return iterator::end();
}
iterator at(size_t pos) {
iterator i = begin();
for (size_t p = 0; p < pos; ++p) {
++i;
}
return i;
}
};