-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLoopNest.java
More file actions
62 lines (55 loc) · 1.69 KB
/
LoopNest.java
File metadata and controls
62 lines (55 loc) · 1.69 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
package com.compilerprogramming.ezlang.compiler;
import java.util.*;
public class LoopNest {
/**
* Parent loop
*/
public LoopNest _parent;
/**
* The block that is the head of the loop
*/
public final BasicBlock _loopHead;
/**
* Blocks that are part of this loop
*/
public final Set<BasicBlock> _blocks;
/**
* Children as per Loop Tree
*/
public final List<LoopNest> _kids;
/**
* Loop Tree depth - top has depth 1
*/
public int _depth;
public LoopNest(BasicBlock loopHead) {
_loopHead = loopHead;
_blocks = new HashSet<>();
_blocks.add(loopHead);
_kids = new ArrayList<>();
}
public void insert(BasicBlock m, Stack<BasicBlock> stack) {
if (!_blocks.contains(m)) {
_blocks.add(m);
stack.push(m);
}
}
public boolean contains(LoopNest other) {
return this != other && _blocks.containsAll(other._blocks);
}
public String uniqueName() { return "Loop_" + _loopHead.uniqueName(); }
public String label() { return "Loop(" + _loopHead.label() + ":" + _depth + ")"; }
public static String generateDotOutput(List<LoopNest> loopNests) {
StringBuilder sb = new StringBuilder();
sb.append("digraph LoopTree {\n");
for (LoopNest n : loopNests) {
sb.append(n.uniqueName()).append(" [label=\"").append(n.label()).append("\"];\n");
}
for (LoopNest n : loopNests) {
for (LoopNest c: n._kids) {
sb.append(n.uniqueName()).append("->").append(c.uniqueName()).append(";\n");
}
}
sb.append("}\n");
return sb.toString();
}
}