forked from kostya/benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhavlak.d
457 lines (367 loc) · 10.2 KB
/
havlak.d
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
import std.conv;
import std.algorithm: canFind;
import std.array;
import std.stdio;
final:
class BasicBlock {
BasicBlock[] inEdges;
BasicBlock[] outEdges;
int name;
this(int _name) { name = _name; }
}
struct BasicBlockEdge {
BasicBlock from;
BasicBlock to;
this(CFG cfg, int fromName, int toName) {
from = cfg.createNode(fromName);
to = cfg.createNode(toName);
from.outEdges ~= to;
to.inEdges ~= from;
cfg.addEdge(this);
}
}
class CFG {
BasicBlock[int] basicBlockMap;
BasicBlockEdge[] edgeList;
BasicBlock startNode;
BasicBlock createNode(int name) {
BasicBlock node = basicBlockMap.get(name, null);
if (node is null) {
node = new BasicBlock(name);
basicBlockMap[name] = node;
}
if (startNode is null) startNode = node;
return node;
}
void addEdge(BasicBlockEdge edge) { edgeList ~= edge; }
int getNumNodes() { return to!int(basicBlockMap.length); }
}
class SimpleLoop {
bool[BasicBlock] basicBlocks;
bool[SimpleLoop] children;
bool isRoot;
bool isReducible;
int counter;
int nestingLevel;
int depthLevel;
SimpleLoop parent;
BasicBlock header;
this() {
parent = null;
header = null;
counter = 0;
depthLevel = 0;
nestingLevel = 0;
isRoot = false;
isReducible = true;
}
void addNode(BasicBlock bb) { basicBlocks[bb] = true; }
void addChildLoop(SimpleLoop loop) { children[loop] = true; }
void setParent(SimpleLoop p) {
parent = p;
parent.addChildLoop(this);
}
void setHeader(BasicBlock bb) {
basicBlocks[bb] = true;
header = bb;
}
void setNestingLevel(int level) {
nestingLevel = level;
if (level == 0) isRoot = true;
}
}
static int loopCounter = 0;
class LSG {
SimpleLoop[] loops;
SimpleLoop root;
this() {
root = createNewLoop();
root.setNestingLevel(0);
addLoop(root);
}
SimpleLoop createNewLoop() {
SimpleLoop s = new SimpleLoop;
loopCounter += 1;
s.counter = loopCounter;
return s;
}
void addLoop(SimpleLoop loop) { loops ~= loop; }
int getNumLoops() { return to!int(loops.length); }
}
class UnionFindNode {
UnionFindNode parent;
BasicBlock bb;
SimpleLoop loop;
int dfsNumber;
this() {
bb = null;
parent = null;
loop = null;
dfsNumber = 0;
}
void initNode(BasicBlock _bb, int dfs) {
parent = this;
bb = _bb;
dfsNumber = dfs;
}
UnionFindNode findSet() {
UnionFindNode[] nodeList;
UnionFindNode node = this;
while (node != node.parent) {
parent = node.parent;
if (parent != parent.parent) { nodeList ~= node; }
node = parent;
}
foreach(iter; nodeList) { iter.parent = node.parent; }
return node;
}
void union_parent(UnionFindNode ufn) {
parent = ufn;
}
}
class HavlakLoopFinder {
CFG cfg;
LSG lsg;
enum UNVISITED = -1;
enum BB_TOP = 0;
enum BB_NONHEADER = 1;
enum BB_REDUCIBLE = 2;
enum BB_SELF = 3;
enum BB_IRREDUCIBLE = 4;
enum BB_DEAD = 5;
enum BB_LAST = 6;
enum MAXNONBACKPREDS = (32 * 1024);
this(CFG _cfg, LSG _lsg) {
cfg = _cfg;
lsg = _lsg;
}
bool isAncestor(int w, int v, int[] last) {
return (w <= v) && (v <= last[w]);
}
int DSF(BasicBlock currentNode, UnionFindNode[] nodes, int[BasicBlock] number, int []last, int current) {
nodes[current].initNode(currentNode, current);
number[currentNode] = current;
int lastid = current;
foreach(target; currentNode.outEdges) {
if (number[target] == UNVISITED) {
lastid = DSF(target, nodes, number, last, lastid + 1);
}
}
last[number[currentNode]] = lastid;
return lastid;
}
int findLoops() {
BasicBlock startNode = cfg.startNode;
if (!startNode) return 0;
int size = cfg.getNumNodes();
// init
bool[int][] non_back_preds;
int[][] back_preds;
int[] header;
int[] types;
int[] last;
UnionFindNode[] nodes;
int[BasicBlock] number;
foreach (_; 0..size) {
bool[int] newset;
non_back_preds ~= newset;
int[] newarr;
back_preds ~= newarr;
header ~= 0;
types ~= 0;
last ~= 0;
nodes ~= new UnionFindNode();
}
// Step a:
// - initialize all nodes as unvisited.
// - depth-first traversal and numbering.
// - unreached BB's are marked as dead.
foreach( k, v; cfg.basicBlockMap) { number[v] = UNVISITED; }
DSF(startNode, nodes, number, last, 0);
// Step b:
// - iterate over all nodes.
// A backedge comes from a descendant in the DFS tree, and non-backedges
// from non-descendants (following Tarjan).
// - check incoming edges 'v' and add them to either
// - the list of backedges (backPreds) or
// - the list of non-backedges (nonBackPreds)
foreach (w; 0..size) {
header[w] = 0;
types[w] = BB_NONHEADER;
auto nodeW = nodes[w].bb;
if (nodeW) {
foreach (nodeV; nodeW.inEdges) {
auto v = number[nodeV];
if (v != UNVISITED) {
if (isAncestor(w, v, last)) {
back_preds[w] ~= v;
} else {
non_back_preds[w][v] = true;
}
}
}
} else {
types[w] = BB_DEAD;
}
}
// Start node is root of all other loops.
header[0] = 0;
// Step c:
// The outer loop, unchanged from Tarjan. It does nothing except
// for those nodes which are the destinations of backedges.
// For a header node w, we chase backward from the sources of the
// backedges adding nodes to the set P, representing the body of
// the loop headed by w.
// By running through the nodes in reverse of the DFST preorder,
// we ensure that inner loop headers will be processed before the
// headers for surrounding loops.
foreach_reverse (w; 0 .. size) {
// this is 'P' in Havlak's paper
UnionFindNode[] nodePool;
auto nodeW = nodes[w].bb;
if (nodeW) { // dead BB
// Step d:
foreach(v; back_preds[w]) {
if (v != w)
nodePool ~= nodes[v].findSet;
else
types[w] = BB_SELF;
}
auto workList = nodePool.dup;
if (nodePool.length != 0) types[w] = BB_REDUCIBLE;
while (!workList.empty) {
auto x = workList[0];
workList.popFront();
// Step e:
// Step e represents the main difference from Tarjan's method.
// Chasing upwards from the sources of a node w's backedges. If
// there is a node y' that is not a descendant of w, w is marked
// the header of an irreducible loop, there is another entry
// into this loop that avoids w.
// The algorithm has degenerated. Break and
// return in this case.
auto nonBackSize = non_back_preds[x.dfsNumber].length;
if (nonBackSize > MAXNONBACKPREDS) return 0;
foreach(iter; non_back_preds[x.dfsNumber]) {
auto y = nodes[iter];
auto ydash = y.findSet;
if (!isAncestor(w, ydash.dfsNumber, last)) {
types[w] = BB_IRREDUCIBLE;
non_back_preds[w][ydash.dfsNumber] = true;
} else {
if (ydash.dfsNumber != w && !nodePool.canFind(ydash)) {
workList ~= ydash;
nodePool ~= ydash;
}
}
}
}
// Collapse/Unionize nodes in a SCC to a single node
// For every SCC found, create a loop descriptor and link it in.
if ((nodePool.length > 0) || (types[w] == BB_SELF)) {
auto loop = lsg.createNewLoop;
loop.setHeader(nodeW);
loop.isReducible = (types[w] != BB_IRREDUCIBLE);
nodes[w].loop = loop;
foreach(node; nodePool) {
// Add nodes to loop descriptor.
header[node.dfsNumber] = w;
node.union_parent(nodes[w]);
if (node.loop) node.loop.setParent(loop); else loop.addNode(node.bb);
}
lsg.addLoop(loop);
}
}
}
return lsg.getNumLoops();
}
}
int findHavlakLoops(CFG cfg, LSG lsg)
{
scope h = new HavlakLoopFinder(cfg, lsg);
return h.findLoops();
}
int findHavlakLoops(CFG cfg)
{
scope lsg = new LSG();
return findHavlakLoops(cfg, lsg);
}
class LoopTesterApp {
CFG cfg;
LSG lsg;
this() {
cfg = new CFG();
lsg = new LSG();
}
int buildDiamond(int start) {
int bb0 = start;
BasicBlockEdge(cfg, bb0, bb0 + 1);
BasicBlockEdge(cfg, bb0, bb0 + 2);
BasicBlockEdge(cfg, bb0 + 1, bb0 + 3);
BasicBlockEdge(cfg, bb0 + 2, bb0 + 3);
return bb0 + 3;
}
void buildConnect(int _start, int _end) {
BasicBlockEdge(cfg, _start, _end);
}
int buildStraight(int start, int n) {
foreach(i; 0..n) {
buildConnect(start + i, start + i + 1);
}
return start + n;
}
int buildBaseLoop(int from) {
auto header = buildStraight(from, 1);
auto diamond1 = buildDiamond(header);
auto d11 = buildStraight(diamond1, 1);
auto diamond2 = buildDiamond(d11);
auto footer = buildStraight(diamond2, 1);
buildConnect(diamond2, d11);
buildConnect(diamond1, header);
buildConnect(footer, from);
return buildStraight(footer, 1);
}
void run() {
writeln("Welcome to LoopTesterApp, D edition");
writeln("Constructing Simple CFG...");
cfg.createNode(0);
buildBaseLoop(0);
cfg.createNode(1);
buildConnect(0, 2);
writeln("15000 dummy loops");
foreach(_; 0..15000) {
findHavlakLoops(cfg);
}
writeln("Constructing CFG...");
int n = 2;
foreach(parlooptrees; 0..10) {
cfg.createNode(n + 1);
buildConnect(2, n + 1);
n = n + 1;
foreach(i; 0..100) {
int top = n;
n = buildStraight(n, 1);
foreach(_; 0..25) { n = buildBaseLoop(n); }
int bottom = buildStraight(n, 1);
buildConnect(n, top);
n = bottom;
}
buildConnect(n, 1);
}
writeln("Performing Loop Recognition\n1 Iteration");
int loops = findHavlakLoops(cfg);
writeln("Another 50 iterations...");
int sum = 0;
foreach (_; 0..50) {
write(".");
stdout.flush();
sum += findHavlakLoops(cfg);
}
writefln("\nFound %d loops (including artificial root node) (%d)", loops, sum);
}
}
int main() {
auto l = new LoopTesterApp();
l.run();
return 0;
}