Further work on incremental phi updater

This commit is contained in:
Alexey Andreev 2016-06-05 17:36:43 +03:00
parent 859f428036
commit 66931401a8
29 changed files with 361 additions and 230 deletions

View File

@ -20,10 +20,6 @@ import java.util.Objects;
import org.teavm.model.*; import org.teavm.model.*;
import org.teavm.model.instructions.*; import org.teavm.model.instructions.*;
/**
*
* @author Alexey Andreev
*/
public class ProgramIO { public class ProgramIO {
private SymbolTable symbolTable; private SymbolTable symbolTable;
private SymbolTable fileTable; private SymbolTable fileTable;
@ -70,13 +66,14 @@ public class ProgramIO {
data.writeShort(tryCatch.getExceptionVariable() != null data.writeShort(tryCatch.getExceptionVariable() != null
? tryCatch.getExceptionVariable().getIndex() : -1); ? tryCatch.getExceptionVariable().getIndex() : -1);
data.writeShort(tryCatch.getHandler().getIndex()); data.writeShort(tryCatch.getHandler().getIndex());
data.writeShort(tryCatch.getJoints().size()); }
for (TryCatchJoint joint : tryCatch.getJoints()) { data.writeShort(basicBlock.getTryCatchJoints().size());
data.writeShort(joint.getTargetVariable().getIndex()); for (TryCatchJoint joint : basicBlock.getTryCatchJoints()) {
data.writeShort(joint.getSourceVariables().size()); data.writeShort(joint.getSource().getIndex());
for (Variable sourceVar : joint.getSourceVariables()) { data.writeShort(joint.getReceiver().getIndex());
data.writeShort(sourceVar.getIndex()); data.writeShort(joint.getSourceVariables().size());
} for (Variable sourceVar : joint.getSourceVariables()) {
data.writeShort(sourceVar.getIndex());
} }
} }
InstructionLocation location = null; InstructionLocation location = null;
@ -146,18 +143,20 @@ public class ProgramIO {
} }
tryCatch.setHandler(program.basicBlockAt(data.readShort())); tryCatch.setHandler(program.basicBlockAt(data.readShort()));
block.getTryCatchBlocks().add(tryCatch); block.getTryCatchBlocks().add(tryCatch);
int jointCount = data.readShort();
for (int k = 0; k < jointCount; ++k) {
TryCatchJoint joint = new TryCatchJoint();
joint.setTargetVariable(program.variableAt(data.readShort()));
int jointSourceCount = data.readShort();
for (int m = 0; m < jointSourceCount; ++m) {
joint.getSourceVariables().add(program.variableAt(data.readShort()));
}
tryCatch.getJoints().add(joint);
}
} }
int jointCount = data.readShort();
for (int j = 0; j < jointCount; ++j) {
TryCatchJoint joint = new TryCatchJoint();
joint.setSource(program.basicBlockAt(data.readShort()));
joint.setReceiver(program.variableAt(data.readShort()));
int jointSourceCount = data.readShort();
for (int k = 0; k < jointSourceCount; ++k) {
joint.getSourceVariables().add(program.variableAt(data.readShort()));
}
block.getTryCatchJoints().add(joint);
}
InstructionLocation location = null; InstructionLocation location = null;
insnLoop: while (true) { insnLoop: while (true) {
byte insnType = data.readByte(); byte insnType = data.readByte();

View File

@ -26,10 +26,6 @@ import org.teavm.common.*;
import org.teavm.model.*; import org.teavm.model.*;
import org.teavm.model.instructions.*; import org.teavm.model.instructions.*;
/**
*
* @author Alexey Andreev
*/
public class DataFlowGraphBuilder implements InstructionReader { public class DataFlowGraphBuilder implements InstructionReader {
private int lastIndex; private int lastIndex;
private GraphBuilder builder = new GraphBuilder(); private GraphBuilder builder = new GraphBuilder();
@ -68,6 +64,11 @@ public class DataFlowGraphBuilder implements InstructionReader {
builder.addEdge(from, to); builder.addEdge(from, to);
} }
} }
for (TryCatchJointReader joint : block.readTryCatchJoints()) {
for (VariableReader sourceVar : joint.readSourceVariables()) {
builder.addEdge(sourceVar.getIndex(), joint.getReceiver().getIndex());
}
}
block.readAllInstructions(this); block.readAllInstructions(this);
} }
Graph graph = builder.build(); Graph graph = builder.build();

View File

@ -44,6 +44,7 @@ import org.teavm.model.PhiReader;
import org.teavm.model.Program; import org.teavm.model.Program;
import org.teavm.model.RuntimeConstant; import org.teavm.model.RuntimeConstant;
import org.teavm.model.TryCatchBlockReader; import org.teavm.model.TryCatchBlockReader;
import org.teavm.model.TryCatchJointReader;
import org.teavm.model.ValueType; import org.teavm.model.ValueType;
import org.teavm.model.VariableReader; import org.teavm.model.VariableReader;
import org.teavm.model.emit.ProgramEmitter; import org.teavm.model.emit.ProgramEmitter;
@ -136,10 +137,17 @@ class DependencyGraphBuilder {
BasicBlockReader block = program.basicBlockAt(i); BasicBlockReader block = program.basicBlockAt(i);
currentExceptionConsumer = createExceptionConsumer(dep, block); currentExceptionConsumer = createExceptionConsumer(dep, block);
block.readAllInstructions(reader); block.readAllInstructions(reader);
for (TryCatchJointReader joint : block.readTryCatchJoints()) {
DependencyNode receiverNode = nodes[joint.getReceiver().getIndex()];
for (VariableReader source : joint.readSourceVariables()) {
DependencyNode sourceNode = nodes[source.getIndex()];
sourceNode.connect(receiverNode);
}
}
for (PhiReader phi : block.readPhis()) { for (PhiReader phi : block.readPhis()) {
DependencyNode receiverNode = nodes[phi.getReceiver().getIndex()];
for (IncomingReader incoming : phi.readIncomings()) { for (IncomingReader incoming : phi.readIncomings()) {
DependencyNode incomingNode = nodes[incoming.getValue().getIndex()]; DependencyNode incomingNode = nodes[incoming.getValue().getIndex()];
DependencyNode receiverNode = nodes[phi.getReceiver().getIndex()];
if (incomingNode != null && receiverNode != null) { if (incomingNode != null && receiverNode != null) {
incomingNode.connect(receiverNode); incomingNode.connect(receiverNode);
} }

View File

@ -24,10 +24,6 @@ import org.teavm.model.util.DefinitionExtractor;
import org.teavm.model.util.ProgramUtils; import org.teavm.model.util.ProgramUtils;
import org.teavm.model.util.UsageExtractor; import org.teavm.model.util.UsageExtractor;
/**
*
* @author Alexey Andreev
*/
class ReadWriteStatsBuilder { class ReadWriteStatsBuilder {
public int[] reads; public int[] reads;
public int[] writes; public int[] writes;
@ -67,6 +63,13 @@ class ReadWriteStatsBuilder {
reads[var.getIndex()]++; reads[var.getIndex()]++;
} }
} }
for (TryCatchJoint joint : block.getTryCatchJoints()) {
writes[joint.getReceiver().getIndex()] += joint.getSourceVariables().size();
for (Variable var : joint.getSourceVariables()) {
reads[var.getIndex()]++;
}
}
for (Phi phi : block.getPhis()) { for (Phi phi : block.getPhis()) {
writes[phi.getReceiver().getIndex()] += phi.getIncomings().size(); writes[phi.getReceiver().getIndex()] += phi.getIncomings().size();
for (Incoming incoming : phi.getIncomings()) { for (Incoming incoming : phi.getIncomings()) {

View File

@ -18,16 +18,14 @@ package org.teavm.model;
import java.util.*; import java.util.*;
import org.teavm.model.instructions.InstructionReader; import org.teavm.model.instructions.InstructionReader;
/**
*
* @author Alexey Andreev
*/
public class BasicBlock implements BasicBlockReader { public class BasicBlock implements BasicBlockReader {
private Program program; private Program program;
private int index; private int index;
private List<Phi> phis = new ArrayList<>(); private List<Phi> phis = new ArrayList<>();
private List<Instruction> instructions = new ArrayList<>(); private List<Instruction> instructions = new ArrayList<>();
List<TryCatchBlock> tryCatchBlocks = new ArrayList<>(); private List<TryCatchBlock> tryCatchBlocks = new ArrayList<>();
private List<TryCatchJoint> joints = new ArrayList<>();
private List<TryCatchJointReader> immutableJoints = new ArrayList<>();
BasicBlock(Program program, int index) { BasicBlock(Program program, int index) {
this.program = program; this.program = program;
@ -199,6 +197,12 @@ public class BasicBlock implements BasicBlockReader {
} }
} }
} }
for (int i = 0; i < joints.size(); ++i) {
TryCatchJoint joint = joints.get(i);
if (joint.getSource() == predecessor) {
joints.remove(i--);
}
}
} }
private List<TryCatchBlock> immutableTryCatchBlocks = Collections.unmodifiableList(tryCatchBlocks); private List<TryCatchBlock> immutableTryCatchBlocks = Collections.unmodifiableList(tryCatchBlocks);
@ -251,4 +255,16 @@ public class BasicBlock implements BasicBlockReader {
public List<TryCatchBlock> getTryCatchBlocks() { public List<TryCatchBlock> getTryCatchBlocks() {
return safeTryCatchBlocks; return safeTryCatchBlocks;
} }
public List<TryCatchJoint> getTryCatchJoints() {
return joints;
}
@Override
public List<TryCatchJointReader> readTryCatchJoints() {
if (immutableJoints == null) {
immutableJoints = Collections.unmodifiableList(joints);
}
return immutableJoints;
}
} }

View File

@ -18,10 +18,6 @@ package org.teavm.model;
import java.util.List; import java.util.List;
import org.teavm.model.instructions.InstructionReader; import org.teavm.model.instructions.InstructionReader;
/**
*
* @author Alexey Andreev
*/
public interface BasicBlockReader { public interface BasicBlockReader {
ProgramReader getProgram(); ProgramReader getProgram();
@ -36,4 +32,6 @@ public interface BasicBlockReader {
void readAllInstructions(InstructionReader reader); void readAllInstructions(InstructionReader reader);
List<? extends TryCatchBlockReader> readTryCatchBlocks(); List<? extends TryCatchBlockReader> readTryCatchBlocks();
List<TryCatchJointReader> readTryCatchJoints();
} }

View File

@ -20,10 +20,6 @@ import java.util.Map;
import org.teavm.optimization.GlobalValueNumbering; import org.teavm.optimization.GlobalValueNumbering;
import org.teavm.optimization.UnusedVariableElimination; import org.teavm.optimization.UnusedVariableElimination;
/**
*
* @author Alexey Andreev
*/
public class PreOptimizingClassHolderSource implements ClassHolderSource { public class PreOptimizingClassHolderSource implements ClassHolderSource {
private ClassHolderSource innerClassSource; private ClassHolderSource innerClassSource;
private Map<String, ClassHolder> cache = new HashMap<>(); private Map<String, ClassHolder> cache = new HashMap<>();

View File

@ -15,17 +15,11 @@
*/ */
package org.teavm.model; package org.teavm.model;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TryCatchBlock implements TryCatchBlockReader { public class TryCatchBlock implements TryCatchBlockReader {
BasicBlock protectedBlock; BasicBlock protectedBlock;
private BasicBlock handler; private BasicBlock handler;
private String exceptionType; private String exceptionType;
private Variable exceptionVariable; private Variable exceptionVariable;
private List<TryCatchJoint> joints = new ArrayList<>();
private List<TryCatchJointReader> immutableJoints = new ArrayList<>();
@Override @Override
public BasicBlock getHandler() { public BasicBlock getHandler() {
@ -58,16 +52,4 @@ public class TryCatchBlock implements TryCatchBlockReader {
public BasicBlock getProtectedBlock() { public BasicBlock getProtectedBlock() {
return protectedBlock; return protectedBlock;
} }
public List<TryCatchJoint> getJoints() {
return joints;
}
@Override
public List<TryCatchJointReader> readJoints() {
if (immutableJoints == null) {
immutableJoints = Collections.unmodifiableList(joints);
}
return immutableJoints;
}
} }

View File

@ -15,8 +15,6 @@
*/ */
package org.teavm.model; package org.teavm.model;
import java.util.List;
public interface TryCatchBlockReader { public interface TryCatchBlockReader {
BasicBlockReader getProtectedBlock(); BasicBlockReader getProtectedBlock();
@ -25,6 +23,4 @@ public interface TryCatchBlockReader {
String getExceptionType(); String getExceptionType();
VariableReader getExceptionVariable(); VariableReader getExceptionVariable();
List<TryCatchJointReader> readJoints();
} }

View File

@ -20,9 +20,10 @@ import java.util.Collections;
import java.util.List; import java.util.List;
public class TryCatchJoint implements TryCatchJointReader { public class TryCatchJoint implements TryCatchJointReader {
private BasicBlock source;
private List<Variable> sourceVariables = new ArrayList<>(); private List<Variable> sourceVariables = new ArrayList<>();
private List<VariableReader> readonlySourceVariables; private List<VariableReader> readonlySourceVariables;
private Variable targetVariable; private Variable receiver;
@Override @Override
public List<VariableReader> readSourceVariables() { public List<VariableReader> readSourceVariables() {
@ -37,11 +38,20 @@ public class TryCatchJoint implements TryCatchJointReader {
} }
@Override @Override
public Variable getTargetVariable() { public Variable getReceiver() {
return targetVariable; return receiver;
} }
public void setTargetVariable(Variable targetVariable) { public void setReceiver(Variable receiver) {
this.targetVariable = targetVariable; this.receiver = receiver;
}
@Override
public BasicBlock getSource() {
return source;
}
public void setSource(BasicBlock source) {
this.source = source;
} }
} }

View File

@ -20,5 +20,7 @@ import java.util.List;
public interface TryCatchJointReader { public interface TryCatchJointReader {
List<VariableReader> readSourceVariables(); List<VariableReader> readSourceVariables();
VariableReader getTargetVariable(); VariableReader getReceiver();
BasicBlockReader getSource();
} }

View File

@ -19,10 +19,6 @@ import java.util.function.IntUnaryOperator;
import org.teavm.model.*; import org.teavm.model.*;
import org.teavm.model.instructions.*; import org.teavm.model.instructions.*;
/**
*
* @author Alexey Andreev
*/
public class BasicBlockMapper implements InstructionVisitor { public class BasicBlockMapper implements InstructionVisitor {
private IntUnaryOperator mapFunction; private IntUnaryOperator mapFunction;
@ -47,6 +43,9 @@ public class BasicBlockMapper implements InstructionVisitor {
return; return;
} }
lastInsn.acceptVisitor(this); lastInsn.acceptVisitor(this);
for (TryCatchJoint joint : block.getTryCatchJoints()) {
joint.setSource(map(joint.getSource()));
}
for (Phi phi : block.getPhis()) { for (Phi phi : block.getPhis()) {
for (Incoming incoming : phi.getIncomings()) { for (Incoming incoming : phi.getIncomings()) {
incoming.setSource(map(incoming.getSource())); incoming.setSource(map(incoming.getSource()));

View File

@ -37,6 +37,7 @@ public class InstructionVariableMapper implements InstructionVisitor {
applyToInstructions(block); applyToInstructions(block);
applyToPhis(block); applyToPhis(block);
applyToTryCatchBlocks(block); applyToTryCatchBlocks(block);
applyToTryCatchJoints(block);
} }
public void applyToInstructions(BasicBlock block) { public void applyToInstructions(BasicBlock block) {
@ -59,12 +60,15 @@ public class InstructionVariableMapper implements InstructionVisitor {
if (tryCatch.getExceptionVariable() != null) { if (tryCatch.getExceptionVariable() != null) {
tryCatch.setExceptionVariable(map(tryCatch.getExceptionVariable())); tryCatch.setExceptionVariable(map(tryCatch.getExceptionVariable()));
} }
for (TryCatchJoint joint : tryCatch.getJoints()) { }
joint.setTargetVariable(map(joint.getTargetVariable())); }
for (int i = 0; i < joint.getSourceVariables().size(); ++i) {
Variable var = joint.getSourceVariables().get(i); public void applyToTryCatchJoints(BasicBlock block) {
joint.getSourceVariables().set(i, map(var)); for (TryCatchJoint joint : block.getTryCatchJoints()) {
} joint.setReceiver(map(joint.getReceiver()));
for (int i = 0; i < joint.getSourceVariables().size(); ++i) {
Variable var = joint.getSourceVariables().get(i);
joint.getSourceVariables().set(i, map(var));
} }
} }
} }

View File

@ -29,6 +29,7 @@ class InterferenceGraphBuilder {
DefinitionExtractor defExtractor = new DefinitionExtractor(); DefinitionExtractor defExtractor = new DefinitionExtractor();
InstructionTransitionExtractor succExtractor = new InstructionTransitionExtractor(); InstructionTransitionExtractor succExtractor = new InstructionTransitionExtractor();
List<List<Incoming>> outgoings = ProgramUtils.getPhiOutputs(program); List<List<Incoming>> outgoings = ProgramUtils.getPhiOutputs(program);
List<List<TryCatchJoint>> outputJoints = ProgramUtils.getOutputJoints(program);
Set<MutableGraphNode> live = new HashSet<>(128); Set<MutableGraphNode> live = new HashSet<>(128);
for (int i = 0; i < program.basicBlockCount(); ++i) { for (int i = 0; i < program.basicBlockCount(); ++i) {
BasicBlock block = program.basicBlockAt(i); BasicBlock block = program.basicBlockAt(i);
@ -46,6 +47,11 @@ class InterferenceGraphBuilder {
live.add(nodes.get(j)); live.add(nodes.get(j));
} }
} }
for (TryCatchJoint joint : outputJoints.get(i)) {
for (Variable outputVar : joint.getSourceVariables()) {
live.add(nodes.get(outputVar.getIndex()));
}
}
for (Incoming outgoing : outgoings.get(i)) { for (Incoming outgoing : outgoings.get(i)) {
live.add(nodes.get(outgoing.getValue().getIndex())); live.add(nodes.get(outgoing.getValue().getIndex()));
} }
@ -53,24 +59,11 @@ class InterferenceGraphBuilder {
if (tryCatch.getExceptionVariable() != null) { if (tryCatch.getExceptionVariable() != null) {
nodes.get(tryCatch.getExceptionVariable().getIndex()).connectAll(live); nodes.get(tryCatch.getExceptionVariable().getIndex()).connectAll(live);
} }
for (TryCatchJoint joint : tryCatch.getJoints()) {
nodes.get(joint.getTargetVariable().getIndex()).connectAll(live);
}
} }
for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) { for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
if (tryCatch.getExceptionVariable() != null) { if (tryCatch.getExceptionVariable() != null) {
live.remove(nodes.get(tryCatch.getExceptionVariable().getIndex())); live.remove(nodes.get(tryCatch.getExceptionVariable().getIndex()));
} }
for (TryCatchJoint joint : tryCatch.getJoints()) {
nodes.get(joint.getTargetVariable().getIndex()).connectAll(live);
}
}
for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
for (TryCatchJoint joint : tryCatch.getJoints()) {
for (Variable var : joint.getSourceVariables()) {
live.add(nodes.get(var.getIndex()));
}
}
} }
for (int j = block.getInstructions().size() - 1; j >= 0; --j) { for (int j = block.getInstructions().size() - 1; j >= 0; --j) {
Instruction insn = block.getInstructions().get(j); Instruction insn = block.getInstructions().get(j);
@ -98,9 +91,17 @@ class InterferenceGraphBuilder {
live.add(nodes.get(j)); live.add(nodes.get(j));
} }
} }
for (TryCatchJoint joint : block.getTryCatchJoints()) {
live.add(nodes.get(joint.getReceiver().getIndex()));
}
for (Phi phi : block.getPhis()) { for (Phi phi : block.getPhis()) {
live.add(nodes.get(phi.getReceiver().getIndex())); live.add(nodes.get(phi.getReceiver().getIndex()));
} }
for (TryCatchJoint joint : block.getTryCatchJoints()) {
nodes.get(joint.getReceiver().getIndex()).connectAll(live);
}
for (Phi phi : block.getPhis()) { for (Phi phi : block.getPhis()) {
nodes.get(phi.getReceiver().getIndex()).connectAll(live); nodes.get(phi.getReceiver().getIndex()).connectAll(live);
} }

View File

@ -47,6 +47,13 @@ public class ListingBuilder {
if (block == null) { if (block == null) {
continue; continue;
} }
for (TryCatchJointReader joint : block.readTryCatchJoints()) {
sb.append("@").append(joint.getReceiver().getIndex()).append(" := e-phi(");
sb.append(joint.readSourceVariables().stream().map(sourceVar -> "@" + sourceVar.getIndex())
.collect(Collectors.joining(", ")));
sb.append(") from $").append(joint.getSource().getIndex()).append("\n");
}
for (PhiReader phi : block.readPhis()) { for (PhiReader phi : block.readPhis()) {
sb.append(prefix).append(" "); sb.append(prefix).append(" ");
sb.append("@").append(phi.getReceiver().getIndex()).append(" := "); sb.append("@").append(phi.getReceiver().getIndex()).append(" := ");
@ -61,6 +68,7 @@ public class ListingBuilder {
} }
sb.append("\n"); sb.append("\n");
} }
InstructionLocation location = null; InstructionLocation location = null;
for (int j = 0; j < block.instructionCount(); ++j) { for (int j = 0; j < block.instructionCount(); ++j) {
insnSb.setLength(0); insnSb.setLength(0);
@ -76,14 +84,6 @@ public class ListingBuilder {
sb.append(prefix).append(" catch ").append(tryCatch.getExceptionType()).append(" @") sb.append(prefix).append(" catch ").append(tryCatch.getExceptionType()).append(" @")
.append(tryCatch.getExceptionVariable().getIndex()) .append(tryCatch.getExceptionVariable().getIndex())
.append(" -> $").append(tryCatch.getHandler().getIndex()); .append(" -> $").append(tryCatch.getHandler().getIndex());
if (!tryCatch.readJoints().isEmpty()) {
sb.append(" with ");
for (TryCatchJointReader joint : tryCatch.readJoints()) {
sb.append(" @").append(joint.getTargetVariable().getIndex()).append(" := ");
sb.append(joint.readSourceVariables().stream().map(sourceVar -> "@" + sourceVar.getIndex())
.collect(Collectors.joining("|")));
}
}
sb.append("\n"); sb.append("\n");
} }
} }

View File

@ -65,19 +65,20 @@ public class LivenessAnalyzer {
} }
} }
for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) { for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
for (TryCatchJoint joint : tryCatch.getJoints()) {
definitions[joint.getTargetVariable().getIndex()] = i;
for (Variable sourceVar : joint.getSourceVariables()) {
Task task = new Task();
task.block = i;
task.var = sourceVar.getIndex();
stack.push(task);
}
}
if (tryCatch.getExceptionVariable() != null) { if (tryCatch.getExceptionVariable() != null) {
definitions[tryCatch.getExceptionVariable().getIndex()] = i; definitions[tryCatch.getExceptionVariable().getIndex()] = i;
} }
} }
for (TryCatchJoint joint : block.getTryCatchJoints()) {
definitions[joint.getReceiver().getIndex()] = i;
for (Variable sourceVar : joint.getSourceVariables()) {
Task task = new Task();
task.block = joint.getSource().getIndex();
task.var = sourceVar.getIndex();
stack.push(task);
}
}
for (Phi phi : block.getPhis()) { for (Phi phi : block.getPhis()) {
definitions[phi.getReceiver().getIndex()] = i; definitions[phi.getReceiver().getIndex()] = i;
for (Incoming incoming : phi.getIncomings()) { for (Incoming incoming : phi.getIncomings()) {

View File

@ -22,7 +22,9 @@ import java.nio.CharBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import org.teavm.common.DominatorTree; import org.teavm.common.DominatorTree;
import org.teavm.common.Graph; import org.teavm.common.Graph;
import org.teavm.common.GraphUtils; import org.teavm.common.GraphUtils;
@ -86,8 +88,9 @@ public class PhiUpdater {
private BasicBlock currentBlock; private BasicBlock currentBlock;
private Phi[][] phiMap; private Phi[][] phiMap;
private int[][] phiIndexMap; private int[][] phiIndexMap;
private List<Map<BasicBlock, Map<Variable, TryCatchJoint>>> jointMap = new ArrayList<>();
private List<List<Phi>> synthesizedPhis = new ArrayList<>(); private List<List<Phi>> synthesizedPhis = new ArrayList<>();
private List<IntObjectMap<TryCatchJoint>> outputJoints = new ArrayList<>(); private List<List<TryCatchJoint>> synthesizedJoints = new ArrayList<>();
private boolean[] usedDefinitions; private boolean[] usedDefinitions;
public void updatePhis(Program program, Variable[] arguments) { public void updatePhis(Program program, Variable[] arguments) {
@ -106,15 +109,19 @@ public class PhiUpdater {
} }
phiMap = new Phi[program.basicBlockCount()][]; phiMap = new Phi[program.basicBlockCount()][];
phiIndexMap = new int[program.basicBlockCount()][]; phiIndexMap = new int[program.basicBlockCount()][];
jointMap = new ArrayList<>();
for (int i = 0; i < phiMap.length; ++i) { for (int i = 0; i < phiMap.length; ++i) {
phiMap[i] = new Phi[program.variableCount()]; phiMap[i] = new Phi[program.variableCount()];
phiIndexMap[i] = new int[program.variableCount()]; phiIndexMap[i] = new int[program.variableCount()];
jointMap.add(new HashMap<>());
} }
domFrontiers = GraphUtils.findDominanceFrontiers(cfg, domTree); domFrontiers = GraphUtils.findDominanceFrontiers(cfg, domTree);
synthesizedPhis.clear(); synthesizedPhis.clear();
synthesizedJoints.clear();
for (int i = 0; i < program.basicBlockCount(); ++i) { for (int i = 0; i < program.basicBlockCount(); ++i) {
synthesizedPhis.add(new ArrayList<>()); synthesizedPhis.add(new ArrayList<>());
synthesizedJoints.add(new ArrayList<>());
} }
estimatePhis(); estimatePhis();
renameVariables(); renameVariables();
@ -127,6 +134,9 @@ public class PhiUpdater {
for (Phi phi : currentBlock.getPhis()) { for (Phi phi : currentBlock.getPhis()) {
markAssignment(phi.getReceiver()); markAssignment(phi.getReceiver());
} }
for (TryCatchJoint joint : currentBlock.getTryCatchJoints()) {
markAssignment(joint.getReceiver());
}
for (Instruction insn : currentBlock.getInstructions()) { for (Instruction insn : currentBlock.getInstructions()) {
insn.acceptVisitor(definitionExtractor); insn.acceptVisitor(definitionExtractor);
for (Variable var : definitionExtractor.getDefinedVariables()) { for (Variable var : definitionExtractor.getDefinedVariables()) {
@ -159,6 +169,7 @@ public class PhiUpdater {
} }
List<List<Incoming>> phiOutputs = ProgramUtils.getPhiOutputs(program); List<List<Incoming>> phiOutputs = ProgramUtils.getPhiOutputs(program);
List<List<TryCatchJoint>> existingOutputJoints = ProgramUtils.getOutputJoints(program);
boolean[] processed = new boolean[program.basicBlockCount()]; boolean[] processed = new boolean[program.basicBlockCount()];
while (head > 0) { while (head > 0) {
@ -170,10 +181,6 @@ public class PhiUpdater {
} }
processed[index] = true; processed[index] = true;
variableMap = Arrays.copyOf(task.variables, task.variables.length); variableMap = Arrays.copyOf(task.variables, task.variables.length);
outputJoints.clear();
for (int i = 0; i < currentBlock.getTryCatchBlocks().size(); ++i) {
outputJoints.add(new IntObjectOpenHashMap<>());
}
for (Phi phi : synthesizedPhis.get(index)) { for (Phi phi : synthesizedPhis.get(index)) {
Variable var = program.createVariable(); Variable var = program.createVariable();
@ -181,18 +188,22 @@ public class PhiUpdater {
variableMap[phi.getReceiver().getIndex()] = var; variableMap[phi.getReceiver().getIndex()] = var;
phi.setReceiver(var); phi.setReceiver(var);
} }
for (TryCatchJoint joint : synthesizedJoints.get(index)) {
Variable var = program.createVariable();
var.getDebugNames().addAll(joint.getReceiver().getDebugNames());
variableMap[joint.getReceiver().getIndex()] = var;
joint.setReceiver(var);
}
for (Phi phi : currentBlock.getPhis()) { for (Phi phi : currentBlock.getPhis()) {
phi.setReceiver(define(phi.getReceiver())); phi.setReceiver(define(phi.getReceiver()));
} }
for (TryCatchJoint joint : currentBlock.getTryCatchJoints()) {
joint.setReceiver(define(joint.getReceiver()));
}
for (Instruction insn : currentBlock.getInstructions()) { for (Instruction insn : currentBlock.getInstructions()) {
insn.acceptVisitor(consumer); insn.acceptVisitor(consumer);
} }
for (IntObjectMap<TryCatchJoint> joints : outputJoints) {
for (int jointVar : joints.keys().toArray()) {
joints.get(jointVar).setTargetVariable(introduce(program.variableAt(jointVar)));
}
}
IntObjectMap<IntObjectMap<Variable>> exceptionVariableMap = new IntObjectOpenHashMap<>(); IntObjectMap<IntObjectMap<Variable>> exceptionVariableMap = new IntObjectOpenHashMap<>();
for (TryCatchBlock tryCatch : currentBlock.getTryCatchBlocks()) { for (TryCatchBlock tryCatch : currentBlock.getTryCatchBlocks()) {
@ -205,9 +216,16 @@ public class PhiUpdater {
tryCatchVariableMap.put(var.getIndex(), newVar); tryCatchVariableMap.put(var.getIndex(), newVar);
} }
} }
for (Incoming output : phiOutputs.get(index)) { for (Incoming output : phiOutputs.get(index)) {
output.setValue(use(output.getValue())); output.setValue(use(output.getValue()));
} }
for (TryCatchJoint joint : existingOutputJoints.get(index)) {
for (int i = 0; i < joint.getSourceVariables().size(); ++i) {
Variable var = joint.getSourceVariables().get(i);
joint.getSourceVariables().set(i, use(var));
}
}
int[] successors = domGraph.outgoingEdges(index); int[] successors = domGraph.outgoingEdges(index);
for (int successor : successors) { for (int successor : successors) {
@ -243,6 +261,7 @@ public class PhiUpdater {
for (int i = 0; i < program.basicBlockCount(); ++i) { for (int i = 0; i < program.basicBlockCount(); ++i) {
program.basicBlockAt(i).getPhis().addAll(synthesizedPhis.get(i)); program.basicBlockAt(i).getPhis().addAll(synthesizedPhis.get(i));
program.basicBlockAt(i).getTryCatchJoints().addAll(synthesizedJoints.get(i));
} }
} }
@ -258,25 +277,51 @@ public class PhiUpdater {
} }
for (int frontier : frontiers) { for (int frontier : frontiers) {
BasicBlock frontierBlock = program.basicBlockAt(frontier); BasicBlock frontierBlock = program.basicBlockAt(frontier);
boolean exists = frontierBlock.getPhis().stream()
.flatMap(phi -> phi.getIncomings().stream()) if (isExceptionHandler(block, frontierBlock)) {
.anyMatch(incoming -> incoming.getSource() == block && incoming.getValue() == var); boolean jointExists = frontierBlock.getTryCatchJoints().stream()
if (exists) { .anyMatch(joint -> joint.getSourceVariables().contains(var) && joint.getSource() == block);
continue; if (!jointExists) {
} Map<Variable, TryCatchJoint> jointSubmap = jointMap.get(frontier).get(block);
Phi phi = phiMap[frontier][var.getIndex()]; if (jointSubmap == null) {
if (phi == null) { jointSubmap = new HashMap<>();
phi = new Phi(); jointMap.get(frontier).put(block, jointSubmap);
phi.setReceiver(var); }
phiIndexMap[frontier][synthesizedPhis.get(frontier).size()] = var.getIndex(); TryCatchJoint joint = jointSubmap.get(var);
synthesizedPhis.get(frontier).add(phi); if (joint == null) {
phiMap[frontier][var.getIndex()] = phi; joint = new TryCatchJoint();
worklist[head++] = frontierBlock; joint.setSource(block);
joint.setReceiver(var);
synthesizedJoints.get(frontier).add(joint);
jointSubmap.put(var, joint);
worklist[head++] = frontierBlock;
}
}
} else {
boolean exists = frontierBlock.getPhis().stream()
.flatMap(phi -> phi.getIncomings().stream())
.anyMatch(incoming -> incoming.getSource() == block && incoming.getValue() == var);
if (exists) {
continue;
}
Phi phi = phiMap[frontier][var.getIndex()];
if (phi == null) {
phi = new Phi();
phi.setReceiver(var);
phiIndexMap[frontier][synthesizedPhis.get(frontier).size()] = var.getIndex();
synthesizedPhis.get(frontier).add(phi);
phiMap[frontier][var.getIndex()] = phi;
worklist[head++] = frontierBlock;
}
} }
} }
} }
} }
private boolean isExceptionHandler(BasicBlock source, BasicBlock target) {
return source.getTryCatchBlocks().stream().anyMatch(tryCatch -> tryCatch.getHandler() == target);
}
private Variable define(Variable var) { private Variable define(Variable var) {
Variable original = var; Variable original = var;
Variable old = variableMap[var.getIndex()]; Variable old = variableMap[var.getIndex()];
@ -285,24 +330,22 @@ public class PhiUpdater {
} }
var = introduce(var); var = introduce(var);
if (original != var) { for (TryCatchBlock tryCatch : currentBlock.getTryCatchBlocks()) {
List<TryCatchBlock> tryCatches = currentBlock.getTryCatchBlocks(); Map<Variable, TryCatchJoint> joints = jointMap.get(tryCatch.getHandler().getIndex()).get(currentBlock);
for (int i = 0; i < tryCatches.size(); ++i) { if (joints == null) {
TryCatchBlock tryCatch = tryCatches.get(i); continue;
IntObjectMap<TryCatchJoint> jointMap = outputJoints.get(i);
TryCatchJoint joint = jointMap.get(original.getIndex());
if (joint == null) {
joint = new TryCatchJoint();
joint.getSourceVariables().add(old);
tryCatch.getJoints().add(joint);
jointMap.put(original.getIndex(), joint);
}
joint.getSourceVariables().add(var);
} }
TryCatchJoint joint = joints.get(original);
if (joint == null) {
continue;
}
if (joint.getSourceVariables().isEmpty()) {
joint.getSourceVariables().add(original);
}
joint.getSourceVariables().add(var);
} }
variableMap[original.getIndex()] = var; variableMap[original.getIndex()] = var;
return var; return var;
} }

View File

@ -81,6 +81,7 @@ public final class ProgramUtils {
blockCopy.getInstructions().addAll(copyInstructions(block, 0, block.instructionCount(), copy)); blockCopy.getInstructions().addAll(copyInstructions(block, 0, block.instructionCount(), copy));
blockCopy.getPhis().addAll(copyPhis(block, copy)); blockCopy.getPhis().addAll(copyPhis(block, copy));
blockCopy.getTryCatchBlocks().addAll(copyTryCatches(block, copy)); blockCopy.getTryCatchBlocks().addAll(copyTryCatches(block, copy));
blockCopy.getTryCatchJoints().addAll(copyTryCatchJoints(block, copy));
} }
return copy; return copy;
} }
@ -119,14 +120,20 @@ public final class ProgramUtils {
tryCatchCopy.setExceptionVariable(target.variableAt(tryCatch.getExceptionVariable().getIndex())); tryCatchCopy.setExceptionVariable(target.variableAt(tryCatch.getExceptionVariable().getIndex()));
tryCatchCopy.setHandler(target.basicBlockAt(tryCatch.getHandler().getIndex())); tryCatchCopy.setHandler(target.basicBlockAt(tryCatch.getHandler().getIndex()));
result.add(tryCatchCopy); result.add(tryCatchCopy);
for (TryCatchJointReader joint : tryCatch.readJoints()) { }
TryCatchJoint jointCopy = new TryCatchJoint(); return result;
jointCopy.setTargetVariable(target.variableAt(joint.getTargetVariable().getIndex())); }
for (VariableReader jointSourceVar : joint.readSourceVariables()) {
jointCopy.getSourceVariables().add(target.variableAt(jointSourceVar.getIndex())); public static List<TryCatchJoint> copyTryCatchJoints(BasicBlockReader block, Program target) {
} List<TryCatchJoint> result = new ArrayList<>();
tryCatchCopy.getJoints().add(jointCopy); for (TryCatchJointReader joint : block.readTryCatchJoints()) {
TryCatchJoint jointCopy = new TryCatchJoint();
jointCopy.setSource(target.basicBlockAt(joint.getSource().getIndex()));
jointCopy.setReceiver(target.variableAt(joint.getReceiver().getIndex()));
for (VariableReader sourceVar : jointCopy.getSourceVariables()) {
jointCopy.getSourceVariables().add(target.variableAt(sourceVar.getIndex()));
} }
result.add(jointCopy);
} }
return result; return result;
} }
@ -148,4 +155,20 @@ public final class ProgramUtils {
return outputs; return outputs;
} }
public static List<List<TryCatchJoint>> getOutputJoints(Program program) {
List<List<TryCatchJoint>> outputs = new ArrayList<>(program.basicBlockCount());
for (int i = 0; i < program.basicBlockCount(); ++i) {
outputs.add(new ArrayList<>());
}
for (int i = 0; i < program.basicBlockCount(); ++i) {
BasicBlock block = program.basicBlockAt(i);
for (TryCatchJoint joint : block.getTryCatchJoints()) {
outputs.get(joint.getSource().getIndex()).add(joint);
}
}
return outputs;
}
} }

View File

@ -15,19 +15,33 @@
*/ */
package org.teavm.model.util; package org.teavm.model.util;
import java.util.*; import java.util.ArrayList;
import org.teavm.common.*; import java.util.Arrays;
import org.teavm.model.*; import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.teavm.common.DisjointSet;
import org.teavm.common.MutableGraphEdge;
import org.teavm.common.MutableGraphNode;
import org.teavm.model.BasicBlock;
import org.teavm.model.Incoming;
import org.teavm.model.Instruction;
import org.teavm.model.MethodReader;
import org.teavm.model.MethodReference;
import org.teavm.model.Phi;
import org.teavm.model.Program;
import org.teavm.model.ProgramReader;
import org.teavm.model.TryCatchJoint;
import org.teavm.model.Variable;
import org.teavm.model.instructions.AssignInstruction; import org.teavm.model.instructions.AssignInstruction;
import org.teavm.model.instructions.EmptyInstruction; import org.teavm.model.instructions.EmptyInstruction;
import org.teavm.model.instructions.JumpInstruction; import org.teavm.model.instructions.JumpInstruction;
/**
*
* @author Alexey Andreev
*/
public class RegisterAllocator { public class RegisterAllocator {
public void allocateRegisters(MethodReader method, Program program) { public void allocateRegisters(MethodReader method, Program program) {
insertJointArgumentsCopies(program);
insertPhiArgumentsCopies(program); insertPhiArgumentsCopies(program);
InterferenceGraphBuilder interferenceBuilder = new InterferenceGraphBuilder(); InterferenceGraphBuilder interferenceBuilder = new InterferenceGraphBuilder();
LivenessAnalyzer liveness = new LivenessAnalyzer(); LivenessAnalyzer liveness = new LivenessAnalyzer();
@ -63,6 +77,7 @@ public class RegisterAllocator {
for (int i = 0; i < program.basicBlockCount(); ++i) { for (int i = 0; i < program.basicBlockCount(); ++i) {
program.basicBlockAt(i).getPhis().clear(); program.basicBlockAt(i).getPhis().clear();
program.basicBlockAt(i).getTryCatchJoints().clear();
} }
} }
@ -98,6 +113,39 @@ public class RegisterAllocator {
} }
} }
private void insertJointArgumentsCopies(Program program) {
for (int i = 0; i < program.basicBlockCount(); ++i) {
BasicBlock block = program.basicBlockAt(i);
block.getTryCatchJoints().forEach(this::insertCopy);
}
}
private void insertCopy(TryCatchJoint joint) {
Set<Variable> variableSet = new HashSet<>(joint.getSourceVariables());
BasicBlock block = joint.getSource();
DefinitionExtractor defExtractor = new DefinitionExtractor();
for (int i = block.getInstructions().size() - 1; i >= 0; --i) {
Instruction insn = block.getInstructions().get(i);
insn.acceptVisitor(defExtractor);
for (Variable definedVar : defExtractor.getDefinedVariables()) {
if (variableSet.remove(definedVar)) {
AssignInstruction copyInsn = new AssignInstruction();
copyInsn.setReceiver(joint.getReceiver());
copyInsn.setAssignee(definedVar);
block.getInstructions().add(i, copyInsn);
}
}
}
for (Variable enteringVar : variableSet) {
AssignInstruction copyInsn = new AssignInstruction();
copyInsn.setReceiver(joint.getReceiver());
copyInsn.setAssignee(enteringVar);
block.getInstructions().add(0, copyInsn);
}
}
private void insertPhiArgumentsCopies(Program program) { private void insertPhiArgumentsCopies(Program program) {
for (int i = 0; i < program.basicBlockCount(); ++i) { for (int i = 0; i < program.basicBlockCount(); ++i) {
Map<BasicBlock, BasicBlock> blockMap = new HashMap<>(); Map<BasicBlock, BasicBlock> blockMap = new HashMap<>();
@ -147,12 +195,8 @@ public class RegisterAllocator {
} }
private boolean isExceptionHandler(Incoming incoming) { private boolean isExceptionHandler(Incoming incoming) {
for (TryCatchBlock tryCatch : incoming.getSource().getTryCatchBlocks()) { return incoming.getSource().getTryCatchJoints().stream().anyMatch(
if (tryCatch.getExceptionVariable() == incoming.getValue()) { joint -> joint.getReceiver() == incoming.getValue());
return true;
}
}
return false;
} }
private void removeRedundantCopies(Program program, List<MutableGraphNode> interferenceGraph, private void removeRedundantCopies(Program program, List<MutableGraphNode> interferenceGraph,
@ -257,6 +301,11 @@ public class RegisterAllocator {
classes.union(phi.getReceiver().getIndex(), incoming.getValue().getIndex()); classes.union(phi.getReceiver().getIndex(), incoming.getValue().getIndex());
} }
} }
for (TryCatchJoint joint : block.getTryCatchJoints()) {
for (Variable sourceVar : joint.getSourceVariables()) {
classes.union(sourceVar.getIndex(), joint.getReceiver().getIndex());
}
}
} }
return classes; return classes;
} }

View File

@ -51,6 +51,11 @@ public class TypeInferer {
builder.addEdge(incoming.getValue().getIndex(), phi.getReceiver().getIndex()); builder.addEdge(incoming.getValue().getIndex(), phi.getReceiver().getIndex());
} }
} }
for (TryCatchJointReader joint : block.readTryCatchJoints()) {
for (VariableReader sourceVar : joint.readSourceVariables()) {
builder.addEdge(sourceVar.getIndex(), joint.getReceiver().getIndex());
}
}
for (TryCatchBlockReader tryCatch : block.readTryCatchBlocks()) { for (TryCatchBlockReader tryCatch : block.readTryCatchBlocks()) {
if (tryCatch.getExceptionVariable() != null) { if (tryCatch.getExceptionVariable() != null) {
types[tryCatch.getExceptionVariable().getIndex()] = VariableType.OBJECT; types[tryCatch.getExceptionVariable().getIndex()] = VariableType.OBJECT;

View File

@ -35,8 +35,8 @@ public class EmptyBlockElimination implements MethodOptimization {
int lastNonEmpty = program.basicBlockCount() - 1; int lastNonEmpty = program.basicBlockCount() - 1;
for (int i = program.basicBlockCount() - 2; i > 0; --i) { for (int i = program.basicBlockCount() - 2; i > 0; --i) {
BasicBlock block = program.basicBlockAt(i); BasicBlock block = program.basicBlockAt(i);
if (block.getPhis().isEmpty() && block.getInstructions().size() == 1 if (block.getPhis().isEmpty() && block.getTryCatchJoints().isEmpty()
&& block.getLastInstruction() instanceof JumpInstruction) { && block.getInstructions().size() == 1 && block.getLastInstruction() instanceof JumpInstruction) {
JumpInstruction insn = (JumpInstruction) block.getLastInstruction(); JumpInstruction insn = (JumpInstruction) block.getLastInstruction();
if (insn.getTarget().getIndex() == i + 1) { if (insn.getTarget().getIndex() == i + 1) {
blockMapping[i] = lastNonEmpty; blockMapping[i] = lastNonEmpty;

View File

@ -24,10 +24,6 @@ import org.teavm.model.*;
import org.teavm.model.instructions.*; import org.teavm.model.instructions.*;
import org.teavm.model.util.ProgramUtils; import org.teavm.model.util.ProgramUtils;
/**
*
* @author Alexey Andreev
*/
public class GlobalValueNumbering implements MethodOptimization { public class GlobalValueNumbering implements MethodOptimization {
private Map<String, KnownValue> knownValues = new HashMap<>(); private Map<String, KnownValue> knownValues = new HashMap<>();
private boolean eliminate; private boolean eliminate;
@ -52,7 +48,8 @@ public class GlobalValueNumbering implements MethodOptimization {
for (int i = 0; i < map.length; ++i) { for (int i = 0; i < map.length; ++i) {
map[i] = i; map[i] = i;
} }
List<List<Incoming>> outgoings = findOutgoings(program); List<List<Incoming>> outgoings = ProgramUtils.getPhiOutputs(program);
List<List<TryCatchJoint>> outputJoints = ProgramUtils.getOutputJoints(program);
int[] stack = new int[cfg.size() * 2]; int[] stack = new int[cfg.size() * 2];
int top = 0; int top = 0;
@ -95,19 +92,17 @@ public class GlobalValueNumbering implements MethodOptimization {
eliminate = false; eliminate = false;
} }
} }
for (TryCatchJoint joint : outputJoints.get(v)) {
for (int i = 0; i < joint.getSourceVariables().size(); ++i) {
int sourceVar = map[joint.getSourceVariables().get(i).getIndex()];
joint.getSourceVariables().set(i, program.variableAt(sourceVar));
}
}
for (Incoming incoming : outgoings.get(v)) { for (Incoming incoming : outgoings.get(v)) {
int value = map[incoming.getValue().getIndex()]; int value = map[incoming.getValue().getIndex()];
incoming.setValue(program.variableAt(value)); incoming.setValue(program.variableAt(value));
} }
for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) { for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
for (TryCatchJoint joint : tryCatch.getJoints()) {
joint.setTargetVariable(program.variableAt(map[joint.getTargetVariable().getIndex()]));
for (int i = 0; i < joint.getSourceVariables().size(); ++i) {
int var = joint.getSourceVariables().get(i).getIndex();
var = map[var];
joint.getSourceVariables().set(i, program.variableAt(var));
}
}
if (tryCatch.getExceptionVariable() != null) { if (tryCatch.getExceptionVariable() != null) {
int var = map[tryCatch.getExceptionVariable().getIndex()]; int var = map[tryCatch.getExceptionVariable().getIndex()];
tryCatch.setExceptionVariable(program.variableAt(var)); tryCatch.setExceptionVariable(program.variableAt(var));
@ -131,21 +126,6 @@ public class GlobalValueNumbering implements MethodOptimization {
this.program = null; this.program = null;
} }
private List<List<Incoming>> findOutgoings(Program program) {
List<List<Incoming>> outgoings = new ArrayList<>();
for (int i = 0; i < program.basicBlockCount(); ++i) {
outgoings.add(new ArrayList<>());
}
for (int i = 0; i < program.basicBlockCount(); ++i) {
for (Phi phi : program.basicBlockAt(i).getPhis()) {
for (Incoming incoming : phi.getIncomings()) {
outgoings.get(incoming.getSource().getIndex()).add(incoming);
}
}
}
return outgoings;
}
private void bind(int var, String value) { private void bind(int var, String value) {
KnownValue known = knownValues.get(value); KnownValue known = knownValues.get(value);
if (known != null && domTree.dominates(known.location, currentBlockIndex) && known.value != var) { if (known != null && domTree.dominates(known.location, currentBlockIndex) && known.value != var) {

View File

@ -165,18 +165,18 @@ public class LoopInvariantMotion implements MethodOptimization {
} }
private int insertPreheader(int headerIndex) { private int insertPreheader(int headerIndex) {
final BasicBlock preheader = program.createBasicBlock(); BasicBlock preheader = program.createBasicBlock();
JumpInstruction escapeInsn = new JumpInstruction(); JumpInstruction escapeInsn = new JumpInstruction();
final BasicBlock header = program.basicBlockAt(headerIndex); BasicBlock header = program.basicBlockAt(headerIndex);
escapeInsn.setTarget(header); escapeInsn.setTarget(header);
preheader.getInstructions().add(escapeInsn); preheader.getInstructions().add(escapeInsn);
for (int i = 0; i < header.getPhis().size(); ++i) {
Phi phi = header.getPhis().get(i); for (Phi phi : header.getPhis()) {
Phi preheaderPhi = null; Phi preheaderPhi = null;
for (int j = 0; j < phi.getIncomings().size(); ++j) { for (int i = 0; i < phi.getIncomings().size(); ++i) {
Incoming incoming = phi.getIncomings().get(j); Incoming incoming = phi.getIncomings().get(i);
if (!dom.dominates(headerIndex, incoming.getSource().getIndex())) { if (!dom.dominates(headerIndex, incoming.getSource().getIndex())) {
phi.getIncomings().remove(j--); phi.getIncomings().remove(i--);
if (preheaderPhi == null) { if (preheaderPhi == null) {
preheaderPhi = new Phi(); preheaderPhi = new Phi();
preheaderPhi.setReceiver(program.createVariable()); preheaderPhi.setReceiver(program.createVariable());
@ -192,6 +192,7 @@ public class LoopInvariantMotion implements MethodOptimization {
phi.getIncomings().add(incoming); phi.getIncomings().add(incoming);
} }
} }
for (int predIndex : graph.incomingEdges(headerIndex)) { for (int predIndex : graph.incomingEdges(headerIndex)) {
if (!dom.dominates(headerIndex, predIndex)) { if (!dom.dominates(headerIndex, predIndex)) {
BasicBlock pred = program.basicBlockAt(predIndex); BasicBlock pred = program.basicBlockAt(predIndex);
@ -199,6 +200,7 @@ public class LoopInvariantMotion implements MethodOptimization {
block -> block == header.getIndex() ? preheader.getIndex() : block)); block -> block == header.getIndex() ? preheader.getIndex() : block));
} }
} }
return preheader.getIndex(); return preheader.getIndex();
} }

View File

@ -277,12 +277,15 @@ class LoopInversionImpl {
tryCatchCopy.setExceptionVariable(tryCatch.getExceptionVariable()); tryCatchCopy.setExceptionVariable(tryCatch.getExceptionVariable());
tryCatchCopy.setHandler(program.basicBlockAt(copiedNodes.getOrDefault(handler, handler))); tryCatchCopy.setHandler(program.basicBlockAt(copiedNodes.getOrDefault(handler, handler)));
targetBlock.getTryCatchBlocks().add(tryCatchCopy); targetBlock.getTryCatchBlocks().add(tryCatchCopy);
for (TryCatchJoint joint : tryCatch.getJoints()) { }
TryCatchJoint jointCopy = new TryCatchJoint();
jointCopy.setTargetVariable(joint.getTargetVariable()); for (TryCatchJoint joint : sourceBlock.getTryCatchJoints()) {
jointCopy.getSourceVariables().addAll(joint.getSourceVariables()); TryCatchJoint jointCopy = new TryCatchJoint();
tryCatchCopy.getJoints().add(joint); jointCopy.setReceiver(joint.getReceiver());
} jointCopy.getSourceVariables().addAll(joint.getSourceVariables());
int source = joint.getSource().getIndex();
jointCopy.setSource(program.basicBlockAt(copiedNodes.getOrDefault(source, source)));
targetBlock.getTryCatchJoints().add(joint);
} }
} }

View File

@ -19,10 +19,6 @@ import org.teavm.common.Graph;
import org.teavm.model.*; import org.teavm.model.*;
import org.teavm.model.instructions.*; import org.teavm.model.instructions.*;
/**
*
* @author Alexey Andreev
*/
public class UnusedVariableElimination implements MethodOptimization { public class UnusedVariableElimination implements MethodOptimization {
@Override @Override
public void optimize(MethodReader method, Program program) { public void optimize(MethodReader method, Program program) {
@ -64,20 +60,18 @@ public class UnusedVariableElimination implements MethodOptimization {
block.getInstructions().remove(j--); block.getInstructions().remove(j--);
} }
} }
for (int j = 0; j < block.getTryCatchJoints().size(); ++j) {
TryCatchJoint joint = block.getTryCatchJoints().get(j);
if (!used[joint.getReceiver().getIndex()]) {
block.getTryCatchJoints().remove(j--);
}
}
for (int j = 0; j < block.getPhis().size(); ++j) { for (int j = 0; j < block.getPhis().size(); ++j) {
Phi phi = block.getPhis().get(j); Phi phi = block.getPhis().get(j);
if (!used[phi.getReceiver().getIndex()]) { if (!used[phi.getReceiver().getIndex()]) {
block.getPhis().remove(j--); block.getPhis().remove(j--);
} }
} }
for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
for (int j = 0; j < tryCatch.getJoints().size(); ++j) {
TryCatchJoint joint = tryCatch.getJoints().get(j);
if (!used[joint.getTargetVariable().getIndex()]) {
tryCatch.getJoints().remove(j--);
}
}
}
} }
} }

View File

@ -20,10 +20,6 @@ import org.teavm.common.GraphBuilder;
import org.teavm.model.*; import org.teavm.model.*;
import org.teavm.model.instructions.*; import org.teavm.model.instructions.*;
/**
*
* @author Alexey Andreev
*/
public final class VariableUsageGraphBuilder { public final class VariableUsageGraphBuilder {
private VariableUsageGraphBuilder() { private VariableUsageGraphBuilder() {
} }
@ -41,6 +37,11 @@ public final class VariableUsageGraphBuilder {
builder.addEdge(incoming.getValue().getIndex(), phi.getReceiver().getIndex()); builder.addEdge(incoming.getValue().getIndex(), phi.getReceiver().getIndex());
} }
} }
for (TryCatchJoint joint : block.getTryCatchJoints()) {
for (Variable sourceVar : joint.getSourceVariables()) {
builder.addEdge(sourceVar.getIndex(), joint.getReceiver().getIndex());
}
}
} }
return builder.build(); return builder.build();
} }

View File

@ -712,7 +712,7 @@ public class TeaVM implements TeaVMHost, ServiceRepository {
} }
private List<MethodOptimization> getOptimizations() { private List<MethodOptimization> getOptimizations() {
return Arrays.asList(new ArrayUnwrapMotion(), new LoopInversion(), new LoopInvariantMotion(), return Arrays.asList(new ArrayUnwrapMotion(), /*new LoopInversion(),*/ new LoopInvariantMotion(),
new GlobalValueNumbering(), new UnusedVariableElimination()); new GlobalValueNumbering(), new UnusedVariableElimination());
} }

View File

@ -29,6 +29,7 @@ import org.teavm.model.MethodReference;
import org.teavm.model.PhiReader; import org.teavm.model.PhiReader;
import org.teavm.model.ProgramReader; import org.teavm.model.ProgramReader;
import org.teavm.model.RuntimeConstant; import org.teavm.model.RuntimeConstant;
import org.teavm.model.TryCatchJointReader;
import org.teavm.model.ValueType; import org.teavm.model.ValueType;
import org.teavm.model.VariableReader; import org.teavm.model.VariableReader;
import org.teavm.model.instructions.ArrayElementType; import org.teavm.model.instructions.ArrayElementType;
@ -64,6 +65,14 @@ class AliasFinder {
set.union(inputs.iterator().next(), phi.getReceiver().getIndex()); set.union(inputs.iterator().next(), phi.getReceiver().getIndex());
} }
} }
for (TryCatchJointReader joint : block.readTryCatchJoints()) {
Set<Integer> inputs = joint.readSourceVariables().stream()
.map(sourceVar -> sourceVar.getIndex())
.collect(Collectors.toSet());
if (inputs.size() == 1) {
set.union(inputs.iterator().next(), joint.getReceiver().getIndex());
}
}
} }
int[] map = new int[set.size()]; int[] map = new int[set.size()];

View File

@ -29,6 +29,7 @@ import org.teavm.model.MethodReference;
import org.teavm.model.Phi; import org.teavm.model.Phi;
import org.teavm.model.PrimitiveType; import org.teavm.model.PrimitiveType;
import org.teavm.model.Program; import org.teavm.model.Program;
import org.teavm.model.TryCatchJoint;
import org.teavm.model.ValueType; import org.teavm.model.ValueType;
import org.teavm.model.Variable; import org.teavm.model.Variable;
import org.teavm.model.instructions.AssignInstruction; import org.teavm.model.instructions.AssignInstruction;
@ -110,6 +111,11 @@ public class BoxingElimination {
union(phi.getReceiver().getIndex(), incoming.getValue().getIndex()); union(phi.getReceiver().getIndex(), incoming.getValue().getIndex());
} }
} }
for (TryCatchJoint joint : block.getTryCatchJoints()) {
for (Variable sourceVar : joint.getSourceVariables()) {
union(sourceVar.getIndex(), joint.getReceiver().getIndex());
}
}
} }
} }