Skip to content

Commit 4ce651b

Browse files
Start on Optimizing Compiler
Register type Add Register type Add Register type Add DomTree and LoopFinding WIP SSA WIP SSA - add Phi instructions WIP SSA - Initial implementation of SSA var renaming WIP SSA - Fixes and first test cases
1 parent 7036bf3 commit 4ce651b

File tree

24 files changed

+3558
-12
lines changed

24 files changed

+3558
-12
lines changed

optvm/pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.compilerprogramming.ezlang</groupId>
8+
<artifactId>compilercraft</artifactId>
9+
<version>1.0</version>
10+
</parent>
11+
<artifactId>optvm</artifactId>
12+
<packaging>jar</packaging>
13+
<name>Optimizing VM</name>
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.compilerprogramming.ezlang</groupId>
17+
<artifactId>parser</artifactId>
18+
<version>1.0</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.compilerprogramming.ezlang</groupId>
22+
<artifactId>types</artifactId>
23+
<version>1.0</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.compilerprogramming.ezlang</groupId>
27+
<artifactId>semantic</artifactId>
28+
<version>1.0</version>
29+
</dependency>
30+
</dependencies>
31+
</project>
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package com.compilerprogramming.ezlang.bytecode;
2+
3+
import com.compilerprogramming.ezlang.types.Register;
4+
5+
import java.util.*;
6+
7+
public class BasicBlock {
8+
public final int bid;
9+
public final boolean loopHead;
10+
public final List<BasicBlock> successors = new ArrayList<>(); // successors
11+
public final List<BasicBlock> predecessors = new ArrayList<>();
12+
public final List<Instruction> instructions = new ArrayList<>();
13+
/**
14+
* The preorder traversal number, also acts as a flag indicating whether the
15+
* BB is yet to be visited (_pre==0 means not yet visited).
16+
*/
17+
int pre;
18+
/**
19+
* The depth of the BB in the dominator tree
20+
*/
21+
int domDepth;
22+
/**
23+
* Reverse post order traversal number;
24+
* Sort node list in ascending order by this to traverse graph in reverse post order.
25+
* In RPO order if an edge exists from A to B we visit A followed by B, but cycles have to
26+
* be dealt with in another way.
27+
*/
28+
int rpo;
29+
/**
30+
* Immediate dominator is the closest strict dominator.
31+
* @see DominatorTree
32+
*/
33+
public BasicBlock idom;
34+
/**
35+
* Nodes for whom this node is the immediate dominator,
36+
* thus the dominator tree.
37+
*/
38+
public List<BasicBlock> dominatedChildren = new ArrayList<>();
39+
/**
40+
* Dominance frontier
41+
*/
42+
public Set<BasicBlock> dominationFrontier = new HashSet<>();
43+
44+
/**
45+
* Nearest Loop to which this BB belongs
46+
*/
47+
public LoopNest loop;
48+
49+
public BasicBlock(int bid, boolean loopHead) {
50+
this.bid = bid;
51+
this.loopHead = loopHead;
52+
}
53+
public BasicBlock(int bid) {
54+
this(bid, false);
55+
}
56+
// For testing only
57+
public BasicBlock(int bid, BasicBlock... preds) {
58+
this.bid = bid;
59+
this.loopHead = false;
60+
for (BasicBlock bb : preds)
61+
bb.addSuccessor(this);
62+
}
63+
public void add(Instruction instruction) {
64+
instructions.add(instruction);
65+
}
66+
public void addSuccessor(BasicBlock successor) {
67+
successors.add(successor);
68+
successor.predecessors.add(this);
69+
}
70+
71+
/**
72+
* Initially the phi has the form
73+
* v = phi(v,v,...)
74+
*/
75+
public void insertPhiFor(Register var) {
76+
for (Instruction i: instructions) {
77+
if (i instanceof Instruction.Phi phi) {
78+
if (phi.dest.id == var.id)
79+
// already added
80+
return;
81+
}
82+
else break;
83+
}
84+
List<Register> inputs = new ArrayList<>();
85+
for (int i = 0; i < predecessors.size(); i++)
86+
inputs.add(var);
87+
Instruction.Phi phi = new Instruction.Phi(var, inputs);
88+
instructions.add(0, phi);
89+
}
90+
public List<Instruction.Phi> phis() {
91+
List<Instruction.Phi> list = new ArrayList<>();
92+
for (Instruction i: instructions) {
93+
if (i instanceof Instruction.Phi phi)
94+
list.add(phi);
95+
else break;
96+
}
97+
return list;
98+
}
99+
public static StringBuilder toStr(StringBuilder sb, BasicBlock bb, BitSet visited)
100+
{
101+
if (visited.get(bb.bid))
102+
return sb;
103+
visited.set(bb.bid);
104+
sb.append("L").append(bb.bid).append(":\n");
105+
for (Instruction n: bb.instructions) {
106+
sb.append(" ");
107+
n.toStr(sb).append("\n");
108+
}
109+
for (BasicBlock succ: bb.successors) {
110+
toStr(sb, succ, visited);
111+
}
112+
return sb;
113+
}
114+
@Override
115+
public boolean equals(Object o) {
116+
if (this == o) return true;
117+
if (o == null || getClass() != o.getClass()) return false;
118+
BasicBlock that = (BasicBlock) o;
119+
return bid == that.bid;
120+
}
121+
122+
@Override
123+
public int hashCode() {
124+
return Objects.hash(bid);
125+
}
126+
127+
public String label() {
128+
return "BB(" + bid + ")";
129+
}
130+
131+
public String uniqueName() {
132+
return "BB_" + bid;
133+
}
134+
135+
//////////////// dominator calculations /////////////////////
136+
137+
public void resetDomInfo() {
138+
domDepth = 0;
139+
idom = null;
140+
dominatedChildren.clear();
141+
dominationFrontier.clear();
142+
}
143+
144+
public void resetRPO() {
145+
pre = 0;
146+
rpo = 0;
147+
}
148+
149+
public boolean dominates(BasicBlock other) {
150+
if (this == other) return true;
151+
while (other.domDepth > domDepth) other = other.idom;
152+
return this == other;
153+
}
154+
155+
/////////////////// End of dominator calculations //////////////////////////////////
156+
}

0 commit comments

Comments
 (0)