Skip to content

Commit bb8d97c

Browse files
Merge pull request #26 from CompilerProgramming/sccp
Bug fixes
2 parents bad22b5 + 111a8fb commit bb8d97c

File tree

5 files changed

+28
-13
lines changed

5 files changed

+28
-13
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ public void add(Instruction instruction) {
9898
instructions.add(instruction);
9999
instruction.block = this;
100100
}
101+
public void add(int pos, Instruction instruction) {
102+
instructions.add(pos, instruction);
103+
instruction.block = this;
104+
}
105+
public void update(int pos, Instruction instruction) {
106+
instructions.set(pos, instruction);
107+
instruction.block = this;
108+
}
101109
public void deleteInstruction(Instruction instruction) {
102110
instructions.remove(instruction);
103111
}
@@ -129,8 +137,7 @@ public void insertPhiFor(Register var) {
129137
for (int i = 0; i < predecessors.size(); i++)
130138
inputs.add(var);
131139
Instruction.Phi phi = new Instruction.Phi(var, inputs);
132-
instructions.add(0, phi);
133-
phi.block = this;
140+
add(0, phi);
134141
}
135142
public List<Instruction.Phi> phis() {
136143
List<Instruction.Phi> list = new ArrayList<>();

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public Map<Integer, Integer> assignRegisters(CompiledFunction function, int numR
2121
if (function.isSSA) throw new IllegalStateException("Register allocation should be done after exiting SSA");
2222
// Remove useless copy operations
2323
InterferenceGraph g = coalesce(function, options);
24-
// Get used registers
24+
// Get used registers, indexed by reg.id
2525
Set<Integer> registers = registersInIR(function);
2626
// Create color set
2727
List<Integer> colors = new ArrayList<>(IntStream.range(0, numRegisters).boxed().toList());
@@ -59,9 +59,9 @@ private Map<Integer, Integer> preAssignArgsToColors(CompiledFunction function, S
5959
for (Instruction instruction : function.entry.instructions) {
6060
if (instruction instanceof Instruction.ArgInstruction argInstruction) {
6161
Integer color = colors.get(count);
62-
Register reg = argInstruction.arg().reg;
63-
registers.remove(reg.nonSSAId()); // Remove register from set before changing slot
64-
assignments.put(reg.nonSSAId(), color);
62+
int reg = argInstruction.arg().reg.id;
63+
registers.remove(reg); // Remove register from set before changing slot
64+
assignments.put(reg, color);
6565
count++;
6666
}
6767
else break;
@@ -142,6 +142,7 @@ private void rewriteInstructions(CompiledFunction function, Instruction deadInst
142142

143143
/**
144144
* Get the list of registers in use in the Intermediate Code
145+
* Indexed by reg.id
145146
* Chaitin: registers_in_il()
146147
*/
147148
private Set<Integer> registersInIR(CompiledFunction function) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ private void insertAtEnd(BasicBlock bb, Instruction i) {
200200
// Last instruction is a branch - so new instruction will
201201
// go before that
202202
int pos = bb.instructions.size()-1;
203-
bb.instructions.add(pos, i);
203+
bb.add(pos, i);
204204
}
205205

206206
private void insertAfterPhi(BasicBlock bb, Register phiDef, Instruction newInst) {
@@ -218,7 +218,7 @@ private void insertAfterPhi(BasicBlock bb, Register phiDef, Instruction newInst)
218218
if (insertionPos < 0) {
219219
throw new IllegalStateException();
220220
}
221-
bb.instructions.add(insertionPos, newInst);
221+
bb.add(insertionPos, newInst);
222222
}
223223

224224
/* Insert a copy from dest to new temp at end of BB, and return temp */

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ public abstract class Instruction {
3131
protected Operand[] uses;
3232
public BasicBlock block;
3333

34-
public Instruction(int opcode, Operand... uses) {
34+
protected Instruction(int opcode, Operand... uses) {
3535
this.opcode = opcode;
3636
this.def = null;
3737
this.uses = new Operand[uses.length];
3838
System.arraycopy(uses, 0, this.uses, 0, uses.length);
3939
}
40-
public Instruction(int opcode, Operand.RegisterOperand def, Operand... uses) {
40+
protected Instruction(int opcode, Operand.RegisterOperand def, Operand... uses) {
4141
this.opcode = opcode;
4242
this.def = def;
4343
this.uses = new Operand[uses.length];
@@ -373,6 +373,7 @@ public Register inputAsRegister(int i) {
373373
public Operand input(int i) {
374374
return uses[i];
375375
}
376+
public int numInputs() { return uses.length; }
376377
public boolean isRegisterInput(int i) {
377378
return uses[i] instanceof Operand.RegisterOperand;
378379
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ private void removeEdge(BasicBlock source, BasicBlock target) {
183183
Instruction instruction = source.instructions.get(idx);
184184
if (instruction instanceof Instruction.ConditionalBranch cbr) {
185185
BasicBlock remainingExecutableBlock = (cbr.falseBlock == target) ? cbr.trueBlock : cbr.falseBlock;
186-
source.instructions.set(idx, new Instruction.Jump(remainingExecutableBlock));
186+
source.update(idx, new Instruction.Jump(remainingExecutableBlock));
187187
}
188188
// Remove phis in target corresponding to the input
189189
for (var phi: target.phis()) {
@@ -318,6 +318,7 @@ public String toString() {
318318
*/
319319
private boolean evalInstruction(Instruction instruction) {
320320
BasicBlock block = instruction.block;
321+
assert block != null;
321322
boolean changed = false;
322323
switch (instruction) {
323324
case Instruction.Ret retInst -> {
@@ -448,8 +449,13 @@ private boolean visitPhi(BasicBlock block, Instruction.Phi phiInst) {
448449
BasicBlock pred = block.predecessors.get(j);
449450
// We ignore non-executable edges
450451
if (isEdgeExecutable(pred, block)) {
451-
LatticeElement varValue = valueLattice.get(phiInst.inputAsRegister(j));
452-
newValue.meet(varValue);
452+
if (phiInst.isRegisterInput(j)) {
453+
LatticeElement varValue = valueLattice.get(phiInst.inputAsRegister(j));
454+
newValue.meet(varValue);
455+
}
456+
else if (phiInst.input(j) instanceof Operand.ConstantOperand constantOperand) {
457+
newValue.meet(constantOperand.value);
458+
}
453459
}
454460
}
455461
return oldValue.meet(newValue);

0 commit comments

Comments
 (0)