Skip to content

Commit 1a21e95

Browse files
Chaitin Graph Coloring Register allocator - WIP coalesce step
Chaitin Graph Coloring Register allocator - WIP coalesce step Bugfix Bugfix Phi and liveness issue Refactored Instruction so that we avoid calculating uses all over the place Some unit tests
1 parent 083941a commit 1a21e95

File tree

13 files changed

+459
-548
lines changed

13 files changed

+459
-548
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.compilerprogramming.ezlang.compiler;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class ChaitinGraphColoringRegisterAllocator {
7+
8+
public ChaitinGraphColoringRegisterAllocator(CompiledFunction function) {
9+
coalesce(function);
10+
}
11+
12+
private void coalesce(CompiledFunction function) {
13+
boolean changed = true;
14+
while (changed) {
15+
var igraph = new InterferenceGraphBuilder().build(function);
16+
changed = coalesceRegisters(function, igraph);
17+
}
18+
}
19+
20+
private boolean coalesceRegisters(CompiledFunction function, InterferenceGraph igraph) {
21+
boolean changed = false;
22+
for (var block: function.getBlocks()) {
23+
List<Integer> instructionsToRemove = new ArrayList<>();
24+
for (int j = 0; j < block.instructions.size(); j++) {
25+
Instruction i = block.instructions.get(j);
26+
if (i instanceof Instruction.Move move
27+
&& move.from() instanceof Operand.RegisterOperand targetOperand) {
28+
Register source = move.def();
29+
Register target = targetOperand.reg;
30+
if (source.id != target.id &&
31+
!igraph.interfere(target.id, source.id)) {
32+
igraph.rename(source.id, target.id);
33+
rewriteInstructions(function, i, source, target);
34+
instructionsToRemove.add(j);
35+
changed = true;
36+
}
37+
}
38+
}
39+
for (var j: instructionsToRemove) {
40+
block.instructions.set(j, new Instruction.NoOp());
41+
}
42+
}
43+
return changed;
44+
}
45+
46+
private void rewriteInstructions(CompiledFunction function, Instruction notNeeded, Register source, Register target) {
47+
for (var block: function.getBlocks()) {
48+
for (Instruction i: block.instructions) {
49+
if (i == notNeeded)
50+
continue;
51+
if (i.definesVar() && source.id == i.def().id)
52+
i.replaceDef(target);
53+
i.replaceUse(source, target);
54+
}
55+
}
56+
}
57+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,4 +565,8 @@ public void livenessAnalysis() {
565565
new Liveness(this);
566566
this.hasLiveness = true;
567567
}
568+
569+
public List<BasicBlock> getBlocks() {
570+
return BBHelper.findAllBlocks(entry);
571+
}
568572
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public ExitSSA(CompiledFunction function) {
1818
this.function = function;
1919
if (!function.isSSA) throw new IllegalStateException();
2020
function.livenessAnalysis();
21+
System.out.println(function.toStr(new StringBuilder(), true));
2122
tree = new DominatorTree(function.entry);
2223
initStack();
2324
insertCopies(function.entry);
@@ -35,9 +36,7 @@ private void insertCopies(BasicBlock block) {
3536
List<Integer> pushed = new ArrayList<>();
3637
for (Instruction i: block.instructions) {
3738
// replace all uses u with stacks[i]
38-
if (i.usesVars()) {
39-
replaceUses(i);
40-
}
39+
replaceUses(i);
4140
}
4241
scheduleCopies(block, pushed);
4342
for (BasicBlock c: block.dominatedChildren) {
@@ -86,7 +85,7 @@ private void scheduleCopies(BasicBlock block, List<Integer> pushed) {
8685
int j = SSATransform.whichPred(s, block);
8786
for (Instruction.Phi phi: s.phis()) {
8887
Register dst = phi.def();
89-
Register src = phi.inputs.get(j).reg; // jth operand of phi node
88+
Register src = phi.input(j); // jth operand of phi node
9089
copySet.add(new CopyItem(src, dst));
9190
map.put(src.id, src);
9291
map.put(dst.id, dst);

0 commit comments

Comments
 (0)