Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<Instruction.Phi> phis() {
List<Instruction.Phi> list = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Map<Integer, Integer> 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<Integer> registers = registersInIR(function);
// Create color set
List<Integer> colors = new ArrayList<>(IntStream.range(0, numRegisters).boxed().toList());
Expand Down Expand Up @@ -59,9 +59,9 @@ private Map<Integer, Integer> 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;
Expand Down Expand Up @@ -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<Integer> registersInIR(CompiledFunction function) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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 -> {
Expand Down Expand Up @@ -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);
Expand Down
Loading