Fix possible bug with exceptions in GVN

This commit is contained in:
Alexey Andreev 2024-09-26 20:39:29 +02:00
parent 054d4c2a24
commit 1d81b7004f
3 changed files with 71 additions and 44 deletions

View File

@ -100,7 +100,7 @@ public class GlobalValueNumbering implements MethodOptimization {
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()];
@ -123,8 +123,10 @@ public class GlobalValueNumbering implements MethodOptimization {
}
while (top > 0) {
int v = stack[--top];
currentBlockIndex = v;
BasicBlock block = program.basicBlockAt(v);
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()];
@ -166,10 +168,11 @@ public class GlobalValueNumbering implements MethodOptimization {
}
}
}
for (Incoming incoming : outgoings.get(v)) {
for (Incoming incoming : outgoings.get(blockIndex)) {
int value = replaceMap[incoming.getValue().getIndex()];
incoming.setValue(program.variableAt(value));
}
}
for (int succ : dom.outgoingEdges(v)) {
stack[top++] = succ;
@ -216,7 +219,7 @@ public class GlobalValueNumbering implements MethodOptimization {
}
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) {

View File

@ -73,6 +73,29 @@ public final class ProgramUtils {
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);
}

View File

@ -93,4 +93,5 @@ tasks.test {
.joinToString(File.pathSeparator))
maxParallelForks = (Runtime.getRuntime().availableProcessors() / 2).coerceAtLeast(1)
maxHeapSize = "800m"
}