Skip to content

Commit

Permalink
Fix possible bug with exceptions in GVN
Browse files Browse the repository at this point in the history
  • Loading branch information
konsoletyper committed Sep 26, 2024
1 parent 054d4c2 commit 1d81b70
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public boolean optimize(Program program) {
boolean affected = false;
this.program = program;
knownValues.clear();
Graph cfg = ProgramUtils.buildControlFlowGraph(program);
Graph cfg = ProgramUtils.buildControlFlowGraph2(program);
domTree = GraphUtils.buildDominatorTree(cfg);
Graph dom = GraphUtils.buildDominatorGraph(domTree, cfg.size());
map = new int[program.variableCount()];
Expand All @@ -123,52 +123,55 @@ public boolean optimize(Program program) {
}
while (top > 0) {
int v = stack[--top];
currentBlockIndex = v;
BasicBlock block = program.basicBlockAt(v);

if (block.getExceptionVariable() != null) {
int var = map[block.getExceptionVariable().getIndex()];
block.setExceptionVariable(program.variableAt(var));
}
if (v % 2 == 0) {
var blockIndex = v / 2;
currentBlockIndex = blockIndex;
BasicBlock block = program.basicBlockAt(blockIndex);

if (block.getExceptionVariable() != null) {
int var = map[block.getExceptionVariable().getIndex()];
block.setExceptionVariable(program.variableAt(var));
}

for (Instruction currentInsn : block) {
evaluatedConstant = null;
currentInsn.acceptVisitor(optimizer);
if (eliminate) {
affected = true;
currentInsn.delete();
eliminate = false;
} else if (evaluatedConstant != null) {
if (evaluatedConstant instanceof Integer) {
IntegerConstantInstruction newInsn = new IntegerConstantInstruction();
newInsn.setConstant((Integer) evaluatedConstant);
newInsn.setReceiver(program.variableAt(receiver));
newInsn.setLocation(currentInsn.getLocation());
currentInsn.replace(newInsn);
} else if (evaluatedConstant instanceof Long) {
LongConstantInstruction newInsn = new LongConstantInstruction();
newInsn.setConstant((Long) evaluatedConstant);
newInsn.setReceiver(program.variableAt(receiver));
newInsn.setLocation(currentInsn.getLocation());
currentInsn.replace(newInsn);
} else if (evaluatedConstant instanceof Float) {
FloatConstantInstruction newInsn = new FloatConstantInstruction();
newInsn.setConstant((Float) evaluatedConstant);
newInsn.setReceiver(program.variableAt(receiver));
newInsn.setLocation(currentInsn.getLocation());
currentInsn.replace(newInsn);
} else if (evaluatedConstant instanceof Double) {
DoubleConstantInstruction newInsn = new DoubleConstantInstruction();
newInsn.setConstant((Double) evaluatedConstant);
newInsn.setReceiver(program.variableAt(receiver));
newInsn.setLocation(currentInsn.getLocation());
currentInsn.replace(newInsn);
for (Instruction currentInsn : block) {
evaluatedConstant = null;
currentInsn.acceptVisitor(optimizer);
if (eliminate) {
affected = true;
currentInsn.delete();
eliminate = false;
} else if (evaluatedConstant != null) {
if (evaluatedConstant instanceof Integer) {
IntegerConstantInstruction newInsn = new IntegerConstantInstruction();
newInsn.setConstant((Integer) evaluatedConstant);
newInsn.setReceiver(program.variableAt(receiver));
newInsn.setLocation(currentInsn.getLocation());
currentInsn.replace(newInsn);
} else if (evaluatedConstant instanceof Long) {
LongConstantInstruction newInsn = new LongConstantInstruction();
newInsn.setConstant((Long) evaluatedConstant);
newInsn.setReceiver(program.variableAt(receiver));
newInsn.setLocation(currentInsn.getLocation());
currentInsn.replace(newInsn);
} else if (evaluatedConstant instanceof Float) {
FloatConstantInstruction newInsn = new FloatConstantInstruction();
newInsn.setConstant((Float) evaluatedConstant);
newInsn.setReceiver(program.variableAt(receiver));
newInsn.setLocation(currentInsn.getLocation());
currentInsn.replace(newInsn);
} else if (evaluatedConstant instanceof Double) {
DoubleConstantInstruction newInsn = new DoubleConstantInstruction();
newInsn.setConstant((Double) evaluatedConstant);
newInsn.setReceiver(program.variableAt(receiver));
newInsn.setLocation(currentInsn.getLocation());
currentInsn.replace(newInsn);
}
}
}
}
for (Incoming incoming : outgoings.get(v)) {
int value = replaceMap[incoming.getValue().getIndex()];
incoming.setValue(program.variableAt(value));
for (Incoming incoming : outgoings.get(blockIndex)) {
int value = replaceMap[incoming.getValue().getIndex()];
incoming.setValue(program.variableAt(value));
}
}

for (int succ : dom.outgoingEdges(v)) {
Expand Down Expand Up @@ -216,7 +219,7 @@ private void bind(int var, String value, boolean noReplace) {
}
namesCompatible = knownName.isEmpty() || name.isEmpty() || knownName.equals(name);
}
if (known != null && domTree.dominates(known.location, currentBlockIndex) && known.value != var
if (known != null && domTree.dominates(known.location * 2 + 1, currentBlockIndex * 2) && known.value != var
&& namesCompatible) {
map[var] = known.value;
if (!noReplace) {
Expand Down
23 changes: 23 additions & 0 deletions core/src/main/java/org/teavm/model/util/ProgramUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,29 @@ public static Graph buildControlFlowGraph(Program program) {
return graphBuilder.build();
}


public static Graph buildControlFlowGraph2(Program program) {
var graphBuilder = new GraphBuilder(program.basicBlockCount());
TransitionExtractor transitionExtractor = new TransitionExtractor();
for (int i = 0; i < program.basicBlockCount(); ++i) {
graphBuilder.addEdge(i * 2, i * 2 + 1);
BasicBlock block = program.basicBlockAt(i);
Instruction insn = block.getLastInstruction();
if (insn != null) {
insn.acceptVisitor(transitionExtractor);
if (transitionExtractor.getTargets() != null) {
for (BasicBlock successor : transitionExtractor.getTargets()) {
graphBuilder.addEdge(i * 2 + 1, successor.getIndex() * 2);
}
}
}
for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
graphBuilder.addEdge(i * 2, tryCatch.getHandler().getIndex() * 2);
}
}
return graphBuilder.build();
}

public static ControlFlowEntry[] getLocationCFG(Program program) {
return new LocationGraphBuilder().build(program);
}
Expand Down
1 change: 1 addition & 0 deletions tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,5 @@ tasks.test {
.joinToString(File.pathSeparator))

maxParallelForks = (Runtime.getRuntime().availableProcessors() / 2).coerceAtLeast(1)
maxHeapSize = "800m"
}

0 comments on commit 1d81b70

Please sign in to comment.