diff --git a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/BasicBlock.java b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/BasicBlock.java index bb82949..80c8193 100644 --- a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/BasicBlock.java +++ b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/BasicBlock.java @@ -98,6 +98,14 @@ public void add(Instruction instruction) { instructions.add(instruction); instruction.block = this; } + public void add(int pos, Instruction instruction) { + instructions.add(pos, instruction); + instruction.block = this; + } + public void update(int pos, Instruction instruction) { + instructions.set(pos, instruction); + instruction.block = this; + } public void deleteInstruction(Instruction instruction) { instructions.remove(instruction); } @@ -129,8 +137,7 @@ public void insertPhiFor(Register var) { for (int i = 0; i < predecessors.size(); i++) inputs.add(var); Instruction.Phi phi = new Instruction.Phi(var, inputs); - instructions.add(0, phi); - phi.block = this; + add(0, phi); } public List phis() { List list = new ArrayList<>(); diff --git a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/ChaitinGraphColoringRegisterAllocator.java b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/ChaitinGraphColoringRegisterAllocator.java index abdce9f..a2a4840 100644 --- a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/ChaitinGraphColoringRegisterAllocator.java +++ b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/ChaitinGraphColoringRegisterAllocator.java @@ -21,7 +21,7 @@ public Map assignRegisters(CompiledFunction function, int numR if (function.isSSA) throw new IllegalStateException("Register allocation should be done after exiting SSA"); // Remove useless copy operations InterferenceGraph g = coalesce(function, options); - // Get used registers + // Get used registers, indexed by reg.id Set registers = registersInIR(function); // Create color set List colors = new ArrayList<>(IntStream.range(0, numRegisters).boxed().toList()); @@ -59,9 +59,9 @@ private Map preAssignArgsToColors(CompiledFunction function, S for (Instruction instruction : function.entry.instructions) { if (instruction instanceof Instruction.ArgInstruction argInstruction) { Integer color = colors.get(count); - Register reg = argInstruction.arg().reg; - registers.remove(reg.nonSSAId()); // Remove register from set before changing slot - assignments.put(reg.nonSSAId(), color); + int reg = argInstruction.arg().reg.id; + registers.remove(reg); // Remove register from set before changing slot + assignments.put(reg, color); count++; } else break; @@ -142,6 +142,7 @@ private void rewriteInstructions(CompiledFunction function, Instruction deadInst /** * Get the list of registers in use in the Intermediate Code + * Indexed by reg.id * Chaitin: registers_in_il() */ private Set registersInIR(CompiledFunction function) { diff --git a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/ExitSSA.java b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/ExitSSA.java index 745e181..0828f98 100644 --- a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/ExitSSA.java +++ b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/ExitSSA.java @@ -200,7 +200,7 @@ private void insertAtEnd(BasicBlock bb, Instruction i) { // Last instruction is a branch - so new instruction will // go before that int pos = bb.instructions.size()-1; - bb.instructions.add(pos, i); + bb.add(pos, i); } private void insertAfterPhi(BasicBlock bb, Register phiDef, Instruction newInst) { @@ -218,7 +218,7 @@ private void insertAfterPhi(BasicBlock bb, Register phiDef, Instruction newInst) if (insertionPos < 0) { throw new IllegalStateException(); } - bb.instructions.add(insertionPos, newInst); + bb.add(insertionPos, newInst); } /* Insert a copy from dest to new temp at end of BB, and return temp */ diff --git a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Instruction.java b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Instruction.java index 20f70a2..c4005a7 100644 --- a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Instruction.java +++ b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Instruction.java @@ -31,13 +31,13 @@ public abstract class Instruction { protected Operand[] uses; public BasicBlock block; - public Instruction(int opcode, Operand... uses) { + protected Instruction(int opcode, Operand... uses) { this.opcode = opcode; this.def = null; this.uses = new Operand[uses.length]; System.arraycopy(uses, 0, this.uses, 0, uses.length); } - public Instruction(int opcode, Operand.RegisterOperand def, Operand... uses) { + protected Instruction(int opcode, Operand.RegisterOperand def, Operand... uses) { this.opcode = opcode; this.def = def; this.uses = new Operand[uses.length]; @@ -373,6 +373,7 @@ public Register inputAsRegister(int i) { public Operand input(int i) { return uses[i]; } + public int numInputs() { return uses.length; } public boolean isRegisterInput(int i) { return uses[i] instanceof Operand.RegisterOperand; } diff --git a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/SparseConditionalConstantPropagation.java b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/SparseConditionalConstantPropagation.java index 92f5fd6..73a04fc 100644 --- a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/SparseConditionalConstantPropagation.java +++ b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/SparseConditionalConstantPropagation.java @@ -183,7 +183,7 @@ private void removeEdge(BasicBlock source, BasicBlock target) { Instruction instruction = source.instructions.get(idx); if (instruction instanceof Instruction.ConditionalBranch cbr) { BasicBlock remainingExecutableBlock = (cbr.falseBlock == target) ? cbr.trueBlock : cbr.falseBlock; - source.instructions.set(idx, new Instruction.Jump(remainingExecutableBlock)); + source.update(idx, new Instruction.Jump(remainingExecutableBlock)); } // Remove phis in target corresponding to the input for (var phi: target.phis()) { @@ -318,6 +318,7 @@ public String toString() { */ private boolean evalInstruction(Instruction instruction) { BasicBlock block = instruction.block; + assert block != null; boolean changed = false; switch (instruction) { case Instruction.Ret retInst -> { @@ -448,8 +449,13 @@ private boolean visitPhi(BasicBlock block, Instruction.Phi phiInst) { BasicBlock pred = block.predecessors.get(j); // We ignore non-executable edges if (isEdgeExecutable(pred, block)) { - LatticeElement varValue = valueLattice.get(phiInst.inputAsRegister(j)); - newValue.meet(varValue); + if (phiInst.isRegisterInput(j)) { + LatticeElement varValue = valueLattice.get(phiInst.inputAsRegister(j)); + newValue.meet(varValue); + } + else if (phiInst.input(j) instanceof Operand.ConstantOperand constantOperand) { + newValue.meet(constantOperand.value); + } } } return oldValue.meet(newValue);