Skip to content

Commit 111a8fb

Browse files
Fix bug - instruction.block not being set
1 parent 1232c49 commit 111a8fb

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
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/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)