Skip to content

Commit bfba000

Browse files
SCCP: don't visit instruction that's not in a visited block because block might not be executable
Test case added
1 parent a956288 commit bfba000

File tree

2 files changed

+108
-17
lines changed

2 files changed

+108
-17
lines changed

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,6 @@ public SparseConditionalConstantPropagation constantPropagation(CompiledFunction
6565
return this;
6666
}
6767

68-
@Override
69-
public String toString() {
70-
StringBuilder sb = new StringBuilder();
71-
sb.append("Flow edges:\n");
72-
for (var edge : flowEdges.keySet()) {
73-
if (flowEdges.get(edge)) {
74-
sb.append(edge).append("=Executable").append("\n");
75-
}
76-
}
77-
sb.append("Lattices:\n");
78-
for (var register: valueLattice.getRegisters()) {
79-
sb.append(register.name()).append("=").append(valueLattice.get(register)).append("\n");
80-
}
81-
return sb.toString();
82-
}
83-
8468
private void visitBlock(BasicBlock b) {
8569
for (var phi : b.phis()) {
8670
visitInstruction(phi);
@@ -109,7 +93,10 @@ private void visitInstruction(Instruction instruction) {
10993
SSAEdges.SSADef ssaDef = ssaEdges.get(def);
11094
if (ssaDef != null) {
11195
for (Instruction use : ssaDef.useList) {
112-
instructionWorkList.push(use);
96+
if (visited.get(use.block.bid))
97+
// Don't visit the instruction if block hasn't been
98+
// visited
99+
instructionWorkList.push(use);
113100
}
114101
}
115102
}
@@ -457,6 +444,25 @@ private static boolean evalArith(LatticeElement cell, LatticeElement left, Latti
457444
return changed;
458445
}
459446

447+
@Override
448+
public String toString() {
449+
StringBuilder sb = new StringBuilder();
450+
sb.append("Flow edges:\n");
451+
for (var edge : flowEdges.keySet()) {
452+
if (flowEdges.get(edge)) {
453+
sb.append(edge).append("=Executable").append("\n");
454+
}
455+
else {
456+
sb.append(edge).append("=NOT Executable").append("\n");
457+
}
458+
}
459+
sb.append("Lattices:\n");
460+
for (var register: valueLattice.getRegisters()) {
461+
sb.append(register.name()).append("=").append(valueLattice.get(register)).append("\n");
462+
}
463+
return sb.toString();
464+
}
465+
460466
/**
461467
* Maintains a Lattice for each SSA variable - i.e register
462468
* Initial value of lattice is TOP/Undefined

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ String compileSrc(String src) {
1717
var functionBuilder = (CompiledFunction) f.code();
1818
new EnterSSA(functionBuilder);
1919
BasicBlock.toStr(sb, functionBuilder.entry, new BitSet(), false);
20+
//functionBuilder.toDot(sb, false);
2021
sb.append(new SparseConditionalConstantPropagation().constantPropagation(functionBuilder).toString());
2122
}
2223
}
@@ -53,14 +54,98 @@ func foo()->Int {
5354
i_1 = 3
5455
goto L4
5556
Flow edges:
57+
L0->L2=NOT Executable
5658
L0->L3=Executable
5759
L4->L1=Executable
60+
L2->L4=NOT Executable
5861
L3->L4=Executable
5962
Lattices:
6063
i_0=1
6164
%t1_0=0
6265
i_1=3
6366
i_3=3
67+
""";
68+
Assert.assertEquals(expected, actual);
69+
}
70+
71+
// 19.4 in MCIC Appel
72+
// Expected results are based on fig 19.13 page 456
73+
@Test
74+
public void test2() {
75+
String src = """
76+
func foo()->Int {
77+
var i = 1
78+
var j = 1
79+
var k = 0
80+
while (k < 100) {
81+
if (j < 20) {
82+
j = i
83+
k = k + 1
84+
}
85+
else {
86+
j = k
87+
k = k + 2
88+
}
89+
}
90+
return j
91+
}
92+
""";
93+
String actual = compileSrc(src);
94+
String expected = """
95+
L0:
96+
i_0 = 1
97+
j_0 = 1
98+
k_0 = 0
99+
goto L2
100+
L2:
101+
k_1 = phi(k_0, k_4)
102+
j_1 = phi(j_0, j_4)
103+
%t3_0 = k_1<100
104+
if %t3_0 goto L3 else goto L4
105+
L3:
106+
%t4_0 = j_1<20
107+
if %t4_0 goto L5 else goto L6
108+
L5:
109+
j_3 = i_0
110+
%t5_0 = k_1+1
111+
k_3 = %t5_0
112+
goto L7
113+
L7:
114+
k_4 = phi(k_3, k_2)
115+
j_4 = phi(j_3, j_2)
116+
goto L2
117+
L6:
118+
j_2 = k_1
119+
%t6_0 = k_1+2
120+
k_2 = %t6_0
121+
goto L7
122+
L4:
123+
ret j_1
124+
goto L1
125+
L1:
126+
Flow edges:
127+
L0->L2=Executable
128+
L4->L1=Executable
129+
L2->L3=Executable
130+
L2->L4=Executable
131+
L3->L5=Executable
132+
L7->L2=Executable
133+
L3->L6=NOT Executable
134+
L5->L7=Executable
135+
L6->L7=NOT Executable
136+
Lattices:
137+
j_3=1
138+
%t5_0=varying
139+
k_3=varying
140+
k_4=varying
141+
j_4=1
142+
i_0=1
143+
j_0=1
144+
k_0=0
145+
k_1=varying
146+
j_1=1
147+
%t3_0=varying
148+
%t4_0=1
64149
""";
65150
Assert.assertEquals(expected, actual);
66151
}

0 commit comments

Comments
 (0)