Get rid of irreducible CFGs in input bytecode

This commit is contained in:
Alexey Andreev 2020-05-14 16:18:33 +03:00
parent 4284bb2a6c
commit 6a4da94f2e

View File

@ -57,6 +57,7 @@ import org.teavm.model.Variable;
import org.teavm.model.optimization.UnreachableBasicBlockEliminator; import org.teavm.model.optimization.UnreachableBasicBlockEliminator;
import org.teavm.model.util.DefinitionExtractor; import org.teavm.model.util.DefinitionExtractor;
import org.teavm.model.util.PhiUpdater; import org.teavm.model.util.PhiUpdater;
import org.teavm.model.util.ProgramNodeSplittingBackend;
import org.teavm.model.util.ProgramUtils; import org.teavm.model.util.ProgramUtils;
public class Parser { public class Parser {
@ -86,6 +87,23 @@ public class Parser {
programParser.setFileName(fileName); programParser.setFileName(fileName);
Program program = programParser.parse(node); Program program = programParser.parse(node);
new UnreachableBasicBlockEliminator().optimize(program); new UnreachableBasicBlockEliminator().optimize(program);
Graph cfg = ProgramUtils.buildControlFlowGraph(program);
if (GraphUtils.isIrreducible(cfg)) {
ProgramNodeSplittingBackend be = new ProgramNodeSplittingBackend(program);
int[] weights = new int[program.basicBlockCount()];
for (int i = 0; i < weights.length; ++i) {
int count = 0;
Instruction insn = program.basicBlockAt(i).getFirstInstruction();
while (insn != null) {
count++;
insn = insn.getNext();
}
weights[i] = count;
}
GraphUtils.splitIrreducibleGraph(cfg, weights, be);
}
PhiUpdater phiUpdater = new PhiUpdater(); PhiUpdater phiUpdater = new PhiUpdater();
Variable[] argumentMapping = applySignature(program, method.getParameterTypes()); Variable[] argumentMapping = applySignature(program, method.getParameterTypes());
phiUpdater.updatePhis(program, argumentMapping); phiUpdater.updatePhis(program, argumentMapping);