Skip to content

Commit 150d966

Browse files
WIP implement incremental SSA construction using Braun's method
WIP implement incremental SSA construction using Braun's method WIP implement incremental SSA construction using Braun's method WIP implement incremental SSA construction using Braun's method WIP implement incremental SSA construction using Braun's method
1 parent edc687b commit 150d966

File tree

11 files changed

+480
-63
lines changed

11 files changed

+480
-63
lines changed

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/BasicBlock.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ public void deleteInstruction(Instruction instruction) {
111111
instructions.remove(instruction);
112112
}
113113
public void addSuccessor(BasicBlock successor) {
114-
assert successors.contains(successor) == false;
115-
successors.add(successor);
116-
assert successor.predecessors.contains(this) == false;
117-
successor.predecessors.add(this);
114+
if (!successors.contains(successor)) {
115+
successors.add(successor);
116+
successor.predecessors.add(this);
117+
}
118118
}
119119
public void removeSuccessor(BasicBlock successor) {
120120
successors.remove(successor);

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/CompiledFunction.java

Lines changed: 365 additions & 48 deletions
Large diffs are not rendered by default.

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Instruction.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,13 @@ public StringBuilder toStr(StringBuilder sb) {
426426
sb.append(")");
427427
return sb;
428428
}
429+
430+
public void addInput(Register register) {
431+
var newUses = new Operand[uses.length + 1];
432+
System.arraycopy(uses, 0, newUses, 0, uses.length);
433+
newUses[newUses.length-1] = new Operand.RegisterOperand(register);
434+
this.uses = newUses;
435+
}
429436
}
430437

431438
public static class ArgInstruction extends Instruction {

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Operand.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.compilerprogramming.ezlang.compiler;
22

3+
import com.compilerprogramming.ezlang.types.Symbol;
34
import com.compilerprogramming.ezlang.types.Type;
45

56
public class Operand {
@@ -48,12 +49,14 @@ public String toString() {
4849
}
4950

5051
public static class LocalRegisterOperand extends RegisterOperand {
51-
public LocalRegisterOperand(Register reg) {
52+
Symbol.VarSymbol variable;
53+
public LocalRegisterOperand(Register reg, Symbol.VarSymbol variable) {
5254
super(reg);
55+
this.variable = variable;
5356
}
5457
@Override
5558
public RegisterOperand copy(Register register) {
56-
return new LocalRegisterOperand(register);
59+
return new LocalRegisterOperand(register, variable);
5760
}
5861
}
5962

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Register.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,6 @@ public int nonSSAId() {
106106
return originalRegNumber;
107107
}
108108
}
109+
110+
109111
}

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/RegisterPool.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ public Register newReg(String baseName, Type type) {
2424
registers.add(reg);
2525
return reg;
2626
}
27+
public Register newISSAReg(String baseName, Type type) {
28+
var id = registers.size();
29+
var reg = new Register(id, baseName + "_" + id, type);
30+
registers.add(reg);
31+
return reg;
32+
}
2733
public Register newTempReg(Type type) {
2834
var id = registers.size();
2935
var name = "%t"+id;

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/SSAEdges.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,16 @@ private static void recordUses(CompiledFunction function, Map<Register, SSADef>
8383

8484
private static void recordUses(Map<Register, SSADef> defUseChains, Register[] inputs, BasicBlock block, Instruction instruction) {
8585
for (Register register : inputs) {
86-
SSADef def = defUseChains.get(register);
87-
def.useList.add(instruction);
86+
recordUse(defUseChains, instruction, register);
8887
}
8988
}
9089

91-
private static void recordDef(Map<Register, SSADef> defUseChains, Register value, Instruction instruction) {
90+
public static void recordUse(Map<Register, SSADef> defUseChains, Instruction instruction, Register register) {
91+
SSADef def = defUseChains.get(register);
92+
def.useList.add(instruction);
93+
}
94+
95+
public static void recordDef(Map<Register, SSADef> defUseChains, Register value, Instruction instruction) {
9296
if (defUseChains.containsKey(value))
9397
throw new CompilerException("Register already defined, invalid multiple definition in SSA");
9498
defUseChains.put(value, new SSADef(instruction));
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.compilerprogramming.ezlang.compiler;
2+
3+
import org.junit.Test;
4+
5+
public class TestIncrementalSSA {
6+
String compileSrc(String src) {
7+
var compiler = new Compiler();
8+
var typeDict = compiler.compileSrc(src);
9+
return compiler.dumpIR(typeDict);
10+
}
11+
12+
@Test
13+
public void test1() {
14+
String src = """
15+
func foo(d: Int) {
16+
var a = 42;
17+
var b = a;
18+
var c = a + b;
19+
a = c + 23;
20+
c = a + d;
21+
}
22+
""";
23+
String result = compileSrc(src);
24+
System.out.println(result);
25+
}
26+
27+
@Test
28+
public void test2() {
29+
String src = """
30+
func foo(d: Int)->Int {
31+
var a = 42
32+
if (d)
33+
{
34+
a = a + 1
35+
}
36+
else
37+
{
38+
a = a - 1
39+
}
40+
return a
41+
}
42+
""";
43+
String result = compileSrc(src);
44+
System.out.println(result);
45+
}
46+
47+
@Test
48+
public void test3() {
49+
String src = """
50+
func factorial(num: Int)->Int {
51+
var result = 1
52+
while (num > 1)
53+
{
54+
result = result * num
55+
num = num - 1
56+
}
57+
return result
58+
}
59+
""";
60+
String result = compileSrc(src);
61+
System.out.println(result);
62+
}
63+
}

optvm/src/test/java/com/compilerprogramming/ezlang/compiler/TestInterferenceGraph.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ public class TestInterferenceGraph {
1111
private CompiledFunction buildTest1() {
1212
TypeDictionary typeDictionary = new TypeDictionary();
1313
Type.TypeFunction functionType = new Type.TypeFunction("foo");
14-
functionType.addArg(new Symbol.ParameterSymbol("a", typeDictionary.INT));
14+
var argSymbol = new Symbol.ParameterSymbol("a", typeDictionary.INT);
15+
functionType.addArg(argSymbol);
1516
functionType.setReturnType(typeDictionary.INT);
1617
CompiledFunction function = new CompiledFunction(functionType, typeDictionary);
1718
RegisterPool regPool = function.registerPool;
1819
Register a = regPool.newReg("a", typeDictionary.INT);
1920
Register b = regPool.newReg("b", typeDictionary.INT);
2021
Register c = regPool.newReg("c", typeDictionary.INT);
2122
Register d = regPool.newReg("d", typeDictionary.INT);
22-
function.code(new Instruction.ArgInstruction(new Operand.LocalRegisterOperand(a)));
23+
function.code(new Instruction.ArgInstruction(new Operand.LocalRegisterOperand(a, argSymbol)));
2324
function.code(new Instruction.Binary(
2425
"+",
2526
new Operand.RegisterOperand(a),

optvm/src/test/java/com/compilerprogramming/ezlang/compiler/TestSSATransform.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -638,13 +638,14 @@ func bar(arg: Int)->Int {
638638
static CompiledFunction buildLostCopyTest() {
639639
TypeDictionary typeDictionary = new TypeDictionary();
640640
Type.TypeFunction functionType = new Type.TypeFunction("foo");
641-
functionType.addArg(new Symbol.ParameterSymbol("p", typeDictionary.INT));
641+
var argSymbol = new Symbol.ParameterSymbol("p", typeDictionary.INT);
642+
functionType.addArg(argSymbol);
642643
functionType.setReturnType(typeDictionary.INT);
643644
CompiledFunction function = new CompiledFunction(functionType, typeDictionary);
644645
RegisterPool regPool = function.registerPool;
645646
Register p = regPool.newReg("p", typeDictionary.INT);
646647
Register x1 = regPool.newReg("x1", typeDictionary.INT);
647-
function.code(new Instruction.ArgInstruction(new Operand.LocalRegisterOperand(p)));
648+
function.code(new Instruction.ArgInstruction(new Operand.LocalRegisterOperand(p, argSymbol)));
648649
function.code(new Instruction.Move(
649650
new Operand.ConstantOperand(1, typeDictionary.INT),
650651
new Operand.RegisterOperand(x1)));
@@ -706,7 +707,8 @@ public void testLostCopyProblem() {
706707
static CompiledFunction buildSwapTest() {
707708
TypeDictionary typeDictionary = new TypeDictionary();
708709
Type.TypeFunction functionType = new Type.TypeFunction("foo");
709-
functionType.addArg(new Symbol.ParameterSymbol("p", typeDictionary.INT));
710+
var argSymbol = new Symbol.ParameterSymbol("p", typeDictionary.INT);
711+
functionType.addArg(argSymbol);
710712
functionType.setReturnType(typeDictionary.VOID);
711713
CompiledFunction function = new CompiledFunction(functionType, typeDictionary);
712714
RegisterPool regPool = function.registerPool;
@@ -715,7 +717,7 @@ static CompiledFunction buildSwapTest() {
715717
Register a2 = regPool.newReg("a2", typeDictionary.INT);
716718
Register b1 = regPool.newReg("b1", typeDictionary.INT);
717719
Register b2 = regPool.newReg("b2", typeDictionary.INT);
718-
function.code(new Instruction.ArgInstruction(new Operand.LocalRegisterOperand(p)));
720+
function.code(new Instruction.ArgInstruction(new Operand.LocalRegisterOperand(p, argSymbol)));
719721
function.code(new Instruction.Move(
720722
new Operand.ConstantOperand(42, typeDictionary.INT),
721723
new Operand.RegisterOperand(a1)));

0 commit comments

Comments
 (0)