This repository was archived by the owner on Jul 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathhavlak6.cc
More file actions
401 lines (340 loc) · 8.83 KB
/
Copy pathhavlak6.cc
File metadata and controls
401 lines (340 loc) · 8.83 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
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
#include <stdio.h>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
class Block {
public:
Block(int n) : name(n) {}
int name;
vector<Block*> in;
vector<Block*> out;
string String();
void Dump(FILE*);
};
string Block::String() {
char buf[20];
snprintf(buf, sizeof buf, "b%d", this->name);
return buf;
}
void Block::Dump(FILE *f) {
fprintf(f, "%s: [", this->String().c_str());
for (int i = 0; i < this->in.size(); i++)
fprintf(f, "%s%s", i > 0 ? " " : "", this->in[i]->String().c_str());
fprintf(f, "] [");
for (int i = 0; i < this->out.size(); i++)
fprintf(f, "%s%s", i > 0 ? " " : "", this->out[i]->String().c_str());
fprintf(f, "]\n");
}
struct Edge {
Edge(int s, int d) : src(s), dst(d) {}
int src, dst;
};
class CFG {
public:
vector<Block*> block;
vector<Edge> edge;
Block *NewBlock();
void Connect(Block *src, Block *dst);
Block *Path(Block *from);
Block *Diamond(Block *from);
Block *BaseLoop(Block *from);
void Dump(FILE*);
};
Block *CFG::NewBlock() {
Block *b = new Block(this->block.size());
this->block.push_back(b);
return b;
}
void CFG::Dump(FILE *f) {
for (int i = 0; i < this->block.size(); i++)
this->block[i]->Dump(f);
}
void CFG::Connect(Block *src, Block *dst) {
src->out.push_back(dst);
dst->in.push_back(src);
this->edge.push_back(Edge(src->name, dst->name));
}
Block *CFG::Path(Block *from) {
Block *n = this->NewBlock();
this->Connect(from, n);
return n;
}
Block *CFG::Diamond(Block *from) {
Block *x = this->Path(from);
Block *y = this->Path(from);
Block *z = this->Path(x);
this->Connect(y, z);
this->Connect(z, from);
return z;
}
Block *CFG::BaseLoop(Block *from) {
Block *z = this->Path(this->Diamond(this->Path(this->Diamond(this->Path(from)))));
this->Connect(z, from);
return this->Path(z);
}
CFG *BuildGraph() {
CFG *g = new CFG;
Block *n0 = g->NewBlock();
Block *n1 = g->NewBlock();
Block *n2 = g->NewBlock();
g->Connect(n0, n2);
for (int i = 0; i < 10; i++) {
Block *n = g->NewBlock();
g->Connect(n2, n);
for (int j = 0; j < 100; j++) {
Block *top = n;
n = g->Path(n);
for (int k = 0; k < 25; k++) {
n = g->BaseLoop(n);
}
Block *bottom = g->Path(n);
g->Connect(n, top);
n = bottom;
}
g->Connect(n, n1);
}
return g;
}
// Basic representation of loop graph.
class Loop {
public:
vector<Block*> block;
vector<Loop*> child;
Loop *parent;
Block *head;
bool isRoot;
bool isReducible;
int counter;
int nesting;
int depth;
};
class LoopGraph {
public:
Loop root;
vector<Loop*> loop;
~LoopGraph();
Loop *NewLoop(int cap);
void CalculateNesting();
void calculateNesting(Loop* l, int depth);
};
LoopGraph::~LoopGraph() {
for (int i = 0; i < this->loop.size(); i++)
delete this->loop[i];
}
static int loopCounter = 0;
Loop *LoopGraph::NewLoop(int cap) {
loopCounter++;
Loop *l = new Loop;
l->counter = loopCounter;
l->block.reserve(cap);
this->loop.push_back(l);
return l;
}
void LoopGraph::CalculateNesting() {
for (int i = 0; i < this->loop.size(); i++) {
Loop *l = this->loop[i];
if (l->isRoot)
continue;
if (l->parent == NULL) {
l->parent = &this->root;
this->root.child.push_back(l);
}
}
this->calculateNesting(&this->root, 0);
}
void LoopGraph::calculateNesting(Loop *l, int depth) {
l->depth = depth;
for (int i = 0; i < l->child.size(); i++) {
Loop *child = l->child[i];
this->calculateNesting(child, depth+1);
int n = child->nesting + 1;
if (l->nesting < n)
l->nesting = n;
}
}
// TODO: Dump, String
// Loop finding state, generated or reused on each iteration.
class LoopBlock {
public:
enum Type {
NonHeader,
Reducible,
Self,
Irreducible,
Dead,
};
Block *block;
Loop *loop;
int first;
int last;
LoopBlock *header; // TODO: head
Type type;
vector<LoopBlock*> backPred;
vector<LoopBlock*> nonBackPred;
LoopBlock *unionf;
void Init(Block*);
LoopBlock *Find();
bool IsAncestor(LoopBlock*);
};
class LoopFinder {
public:
vector<LoopBlock> loopBlock;
vector<LoopBlock*> depthFirst;
vector<LoopBlock*> pool;
void Search(Block*);
void FindLoops(CFG*, LoopGraph*);
};
const int Unvisited = -1;
void LoopBlock::Init(Block *b) {
this->block = b;
this->loop = NULL;
this->first = Unvisited;
this->last = Unvisited;
this->header = NULL;
this->type = LoopBlock::NonHeader;
this->backPred.clear();
this->nonBackPred.clear();
this->unionf = this;
}
LoopBlock *LoopBlock::Find() {
if (this->unionf != this) {
this->unionf = this->unionf->Find();
}
return this->unionf;
}
// Depth first search to number blocks.
void LoopFinder::Search(Block *b) {
LoopBlock *lb = &this->loopBlock[b->name];
this->depthFirst.push_back(lb);
lb->first = this->depthFirst.size();
for (int i = 0; i < b->out.size(); i++) {
Block *out = b->out[i];
if (this->loopBlock[out->name].first == Unvisited)
this->Search(out);
}
lb->last = this->depthFirst.size();
}
bool LoopBlock::IsAncestor(LoopBlock *p) {
return this->first <= p->first && p->first <= this->last;
}
void LoopFinder::FindLoops(CFG *g, LoopGraph *lsg) {
int size = g->block.size();
if (size == 0)
return;
// Step A: Initialize nodes, depth first numbering, mark dead nodes.
this->loopBlock.resize(size);
this->depthFirst.reserve(size);
this->depthFirst.clear();
for (int i = 0; i < size; i++)
this->loopBlock[i].Init(g->block[i]);
this->Search(g->block[0]);
for (int i = 0; i < size; i++ ){
LoopBlock *lb = &this->loopBlock[i]; // TODO
if (lb->first == Unvisited)
lb->type = LoopBlock::Dead;
}
// Step B: Classify back edges as coming from descendents or not.
for (int i = 0; i < this->depthFirst.size(); i++) {
LoopBlock *lb = this->depthFirst[i];
for (int j = 0; j < lb->block->in.size(); j++) {
Block *b = lb->block->in[j];
LoopBlock *lbb = &this->loopBlock[b->name]; // TODO
if (lb->IsAncestor(lbb))
lb->backPred.push_back(lbb);
else
lb->nonBackPred.push_back(lbb);
}
}
// Start node is root of all other loops.
this->loopBlock[0].header = &this->loopBlock[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.
for (int i = this->depthFirst.size() - 1; i >= 0; i--) {
LoopBlock *w = this->depthFirst[i];
this->pool.clear();
// Step D.
for (int i = 0; i < w->backPred.size(); i++) {
LoopBlock* pred = w->backPred[i];
if (w == pred) {
w->type = LoopBlock::Self;
continue;
}
this->pool.push_back(pred->Find());
}
// Process node pool in order as work list.
for (int i = 0; i < this->pool.size(); i++) {
LoopBlock *x = this->pool[i];
// 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->
for (int j = 0; j < x->nonBackPred.size(); j++) {
LoopBlock *y = x->nonBackPred[j];
LoopBlock *ydash = y->Find();
if (!w->IsAncestor(ydash)) {
w->type = LoopBlock::Irreducible;
if (find(w->nonBackPred.begin(), w->nonBackPred.end(), y) == w->nonBackPred.end())
w->nonBackPred.push_back(y);
} else if (ydash != w) {
if (find(this->pool.begin(), this->pool.end(), ydash) == this->pool.end())
this->pool.push_back(ydash);
}
}
}
// Collapse/Unionize nodes in a SCC to a single node
// For every SCC found, create a loop descriptor and link it in.
if (this->pool.size() > 0 || w->type == LoopBlock::Self) {
Loop *l = lsg->NewLoop(1 + pool.size());
l->head = w->block;
l->block.push_back(w->block);
l->isReducible = w->type != LoopBlock::Irreducible;
w->loop = l;
// At this point, one can set attributes to the loop, such as:
//
// the bottom node:
// iter = backPreds[w].begin();
// loop bottom is: nodes[iter].node);
//
// the number of backedges:
// backPreds[w].size()
for (int i = 0; i < pool.size(); i++) {
LoopBlock *node = pool[i];
// Add nodes to loop descriptor.
node->header = w;
node->unionf = w;
// Nested loops are not added, but linked together.
if (node->loop != NULL) {
node->loop->parent = l;
} else {
l->block.push_back(node->block);
}
}
}
}
}
// Main program.
int main() {
LoopFinder f;
CFG *g = BuildGraph();
LoopGraph lsg;
f.FindLoops(g, &lsg);
for (int i = 0; i < 50; i++) {
LoopGraph lsg;
f.FindLoops(g, &lsg);
}
printf("# of loops: %d (including 1 artificial root node)\n", (int)lsg.loop.size());
lsg.CalculateNesting();
}