Skip to content

Commit ce25e48

Browse files
some cleanups
1 parent aeeb730 commit ce25e48

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

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

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.compilerprogramming.ezlang.compiler;
22

3+
import com.compilerprogramming.ezlang.exceptions.CompilerException;
34
import com.compilerprogramming.ezlang.types.Type;
45

56
import java.util.ArrayList;
@@ -30,11 +31,10 @@ public abstract class Instruction {
3031
protected Operand[] uses;
3132
public BasicBlock block;
3233

33-
protected Instruction(int opcode, Operand... uses) {
34+
protected Instruction(int opcode) {
3435
this.opcode = opcode;
3536
this.def = null;
36-
this.uses = new Operand[uses.length];
37-
System.arraycopy(uses, 0, this.uses, 0, uses.length);
37+
this.uses = new Operand[0];
3838
}
3939
protected Instruction(int opcode, Operand.RegisterOperand def, Operand... uses) {
4040
this.opcode = opcode;
@@ -49,9 +49,23 @@ public String toString() {
4949
return toStr(new StringBuilder()).toString();
5050
}
5151

52+
/**
53+
* Does this instruction define a var?
54+
*/
5255
public boolean definesVar() { return def != null; }
56+
/**
57+
* If the instruction defines a var then return the Register else null
58+
*/
5359
public Register def() { return def != null ? def.reg: null; }
60+
public void replaceDef(Register newDef) {
61+
if (def == null) throw new IllegalStateException();
62+
def = def.copy(newDef);
63+
}
5464

65+
/**
66+
* Get the registers used by this instruction. Non register operands are
67+
* not included.
68+
*/
5569
public List<Register> uses() {
5670
List<Register> useList = null;
5771
for (int i = 0; i < uses.length; i++) {
@@ -64,10 +78,14 @@ public List<Register> uses() {
6478
if (useList == null) useList = Collections.emptyList();
6579
return useList;
6680
}
67-
public void replaceDef(Register newReg) {
68-
if (def == null) throw new IllegalStateException();
69-
def = def.copy(newReg);
70-
}
81+
82+
/**
83+
* Replaces existing register uses with new ones. This api is not great
84+
* as it requires user to supply registers in the same order as returned by
85+
* the method {@link #uses()}.
86+
*
87+
* FIXME replace this with a better api
88+
*/
7189
public void replaceUses(Register[] newUses) {
7290
int j = 0;
7391
for (int i = 0; i < uses.length; i++) {
@@ -76,7 +94,14 @@ public void replaceUses(Register[] newUses) {
7694
uses[i] = registerOperand.copy(newUses[j++]);
7795
}
7896
}
97+
// Sanity check that we replaced the full set of registers
98+
if (j != newUses.length)
99+
throw new CompilerException("Error - supplied registers do not replace all uses");
79100
}
101+
102+
/**
103+
* Replaces all occurrences of source with target in the uses list of the instruction
104+
*/
80105
public boolean replaceUse(Register source, Register target) {
81106
boolean replaced = false;
82107
for (int i = 0; i < uses.length; i++) {
@@ -88,23 +113,18 @@ public boolean replaceUse(Register source, Register target) {
88113
}
89114
return replaced;
90115
}
91-
public void replaceWithConstant(Register register, Operand.ConstantOperand constantOperand) {
116+
117+
/**
118+
* Replaces all uses of given register with the constant operand
119+
*/
120+
public void replaceUseWithConstant(Register register, Operand.ConstantOperand constantOperand) {
92121
for (int i = 0; i < uses.length; i++) {
93122
Operand operand = uses[i];
94123
if (operand != null && operand instanceof Operand.RegisterOperand registerOperand && registerOperand.reg.id == register.id) {
95124
uses[i] = constantOperand;
96125
}
97126
}
98127
}
99-
public static class NoOp extends Instruction {
100-
public NoOp() {
101-
super(I_NOOP);
102-
}
103-
@Override
104-
public StringBuilder toStr(StringBuilder sb) {
105-
return sb.append("noop");
106-
}
107-
}
108128

109129
public static class Move extends Instruction {
110130
public Move(Operand from, Operand to) {
@@ -384,7 +404,7 @@ public Register def() {
384404
throw new UnsupportedOperationException();
385405
}
386406
@Override
387-
public void replaceDef(Register newReg) {
407+
public void replaceDef(Register newDef) {
388408
throw new UnsupportedOperationException();
389409
}
390410
@Override

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ private void visitInstruction(Instruction instruction) {
8888
flowWorklist.push(s);
8989
}
9090
}
91-
} else if (instruction.definesVar() || instruction instanceof Instruction.Phi) {
91+
}
92+
else if (instruction.definesVar() || instruction instanceof Instruction.Phi) {
9293
var def = instruction instanceof Instruction.Phi phi ? phi.value() : instruction.def();
9394
// Push all uses (instructions) of the def into the worklist
9495
SSAEdges.SSADef ssaDef = ssaEdges.get(def);
@@ -207,7 +208,7 @@ private void replaceVarsWithConstants() {
207208
// replace uses with constant
208209
for (var usingInstruction: defUseChain.useList) {
209210
if (executableBlocks.get(usingInstruction.block.bid))
210-
usingInstruction.replaceWithConstant(register, constant);
211+
usingInstruction.replaceUseWithConstant(register, constant);
211212
}
212213
defUseChain.useList.clear();
213214
var block = defUseChain.instruction.block;

0 commit comments

Comments
 (0)