Improve GVN

This commit is contained in:
Alexey Andreev 2019-06-23 01:05:31 +03:00
parent 2c6068b36a
commit c430578426
2 changed files with 89 additions and 73 deletions

View File

@ -15,6 +15,8 @@
*/ */
package org.teavm.model.optimization; package org.teavm.model.optimization;
import com.carrotsearch.hppc.ObjectIntHashMap;
import com.carrotsearch.hppc.ObjectIntMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -29,6 +31,7 @@ import org.teavm.model.Instruction;
import org.teavm.model.InvokeDynamicInstruction; import org.teavm.model.InvokeDynamicInstruction;
import org.teavm.model.Program; import org.teavm.model.Program;
import org.teavm.model.Variable; import org.teavm.model.Variable;
import org.teavm.model.instructions.AbstractInstructionVisitor;
import org.teavm.model.instructions.ArrayLengthInstruction; import org.teavm.model.instructions.ArrayLengthInstruction;
import org.teavm.model.instructions.AssignInstruction; import org.teavm.model.instructions.AssignInstruction;
import org.teavm.model.instructions.BinaryBranchingInstruction; import org.teavm.model.instructions.BinaryBranchingInstruction;
@ -38,34 +41,27 @@ import org.teavm.model.instructions.BranchingInstruction;
import org.teavm.model.instructions.CastInstruction; import org.teavm.model.instructions.CastInstruction;
import org.teavm.model.instructions.CastIntegerInstruction; import org.teavm.model.instructions.CastIntegerInstruction;
import org.teavm.model.instructions.CastNumberInstruction; import org.teavm.model.instructions.CastNumberInstruction;
import org.teavm.model.instructions.ClassConstantInstruction;
import org.teavm.model.instructions.CloneArrayInstruction; import org.teavm.model.instructions.CloneArrayInstruction;
import org.teavm.model.instructions.ConstructArrayInstruction; import org.teavm.model.instructions.ConstructArrayInstruction;
import org.teavm.model.instructions.ConstructInstruction;
import org.teavm.model.instructions.ConstructMultiArrayInstruction; import org.teavm.model.instructions.ConstructMultiArrayInstruction;
import org.teavm.model.instructions.DoubleConstantInstruction; import org.teavm.model.instructions.DoubleConstantInstruction;
import org.teavm.model.instructions.EmptyInstruction;
import org.teavm.model.instructions.ExitInstruction; import org.teavm.model.instructions.ExitInstruction;
import org.teavm.model.instructions.FloatConstantInstruction; import org.teavm.model.instructions.FloatConstantInstruction;
import org.teavm.model.instructions.GetElementInstruction; import org.teavm.model.instructions.GetElementInstruction;
import org.teavm.model.instructions.GetFieldInstruction; import org.teavm.model.instructions.GetFieldInstruction;
import org.teavm.model.instructions.InitClassInstruction;
import org.teavm.model.instructions.InstructionVisitor; import org.teavm.model.instructions.InstructionVisitor;
import org.teavm.model.instructions.IntegerConstantInstruction; import org.teavm.model.instructions.IntegerConstantInstruction;
import org.teavm.model.instructions.InvokeInstruction; import org.teavm.model.instructions.InvokeInstruction;
import org.teavm.model.instructions.IsInstanceInstruction; import org.teavm.model.instructions.IsInstanceInstruction;
import org.teavm.model.instructions.JumpInstruction;
import org.teavm.model.instructions.LongConstantInstruction; import org.teavm.model.instructions.LongConstantInstruction;
import org.teavm.model.instructions.MonitorEnterInstruction; import org.teavm.model.instructions.MonitorEnterInstruction;
import org.teavm.model.instructions.MonitorExitInstruction; import org.teavm.model.instructions.MonitorExitInstruction;
import org.teavm.model.instructions.NegateInstruction; import org.teavm.model.instructions.NegateInstruction;
import org.teavm.model.instructions.NullCheckInstruction; import org.teavm.model.instructions.NullCheckInstruction;
import org.teavm.model.instructions.NullConstantInstruction;
import org.teavm.model.instructions.NumericOperandType; import org.teavm.model.instructions.NumericOperandType;
import org.teavm.model.instructions.PutElementInstruction; import org.teavm.model.instructions.PutElementInstruction;
import org.teavm.model.instructions.PutFieldInstruction; import org.teavm.model.instructions.PutFieldInstruction;
import org.teavm.model.instructions.RaiseInstruction; import org.teavm.model.instructions.RaiseInstruction;
import org.teavm.model.instructions.StringConstantInstruction;
import org.teavm.model.instructions.SwitchInstruction; import org.teavm.model.instructions.SwitchInstruction;
import org.teavm.model.instructions.UnwrapArrayInstruction; import org.teavm.model.instructions.UnwrapArrayInstruction;
import org.teavm.model.util.ProgramUtils; import org.teavm.model.util.ProgramUtils;
@ -74,6 +70,9 @@ 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;
private int[] map; private int[] map;
private int[] replaceMap;
private boolean[] deletedVars;
private ObjectIntMap<Number> constantIndexes = new ObjectIntHashMap<>();
private Number[] numericConstants; private Number[] numericConstants;
private Number evaluatedConstant; private Number evaluatedConstant;
private int receiver; private int receiver;
@ -104,10 +103,14 @@ public class GlobalValueNumbering implements MethodOptimization {
domTree = GraphUtils.buildDominatorTree(cfg); domTree = GraphUtils.buildDominatorTree(cfg);
Graph dom = GraphUtils.buildDominatorGraph(domTree, cfg.size()); Graph dom = GraphUtils.buildDominatorGraph(domTree, cfg.size());
map = new int[program.variableCount()]; map = new int[program.variableCount()];
replaceMap = new int[program.variableCount()];
numericConstants = new Number[program.variableCount()]; numericConstants = new Number[program.variableCount()];
for (int i = 0; i < map.length; ++i) { for (int i = 0; i < map.length; ++i) {
map[i] = i; map[i] = i;
replaceMap[i] = i;
} }
deletedVars = new boolean[program.variableCount()];
preprocessConstants();
List<List<Incoming>> outgoings = ProgramUtils.getPhiOutputs(program); List<List<Incoming>> outgoings = ProgramUtils.getPhiOutputs(program);
int[] stack = new int[cfg.size() * 2]; int[] stack = new int[cfg.size() * 2];
@ -163,7 +166,7 @@ public class GlobalValueNumbering implements MethodOptimization {
} }
} }
for (Incoming incoming : outgoings.get(v)) { for (Incoming incoming : outgoings.get(v)) {
int value = map[incoming.getValue().getIndex()]; int value = replaceMap[incoming.getValue().getIndex()];
incoming.setValue(program.variableAt(value)); incoming.setValue(program.variableAt(value));
} }
@ -172,7 +175,7 @@ public class GlobalValueNumbering implements MethodOptimization {
} }
} }
for (int i = 0; i < map.length; ++i) { for (int i = 0; i < map.length; ++i) {
if (map[i] != i) { if (deletedVars[i]) {
program.deleteVariable(i); program.deleteVariable(i);
} }
} }
@ -182,7 +185,19 @@ public class GlobalValueNumbering implements MethodOptimization {
return affected; return affected;
} }
private void preprocessConstants() {
for (BasicBlock block : program.getBasicBlocks()) {
for (Instruction instruction : block) {
instruction.acceptVisitor(constantPreprocessor);
}
}
}
private void bind(int var, String value) { private void bind(int var, String value) {
bind(var, value, false);
}
private void bind(int var, String value, boolean noReplace) {
String name = program.variableAt(map[var]).getDebugName(); String name = program.variableAt(map[var]).getDebugName();
if (name == null || namesPreserved) { if (name == null || namesPreserved) {
name = ""; name = "";
@ -202,8 +217,12 @@ public class GlobalValueNumbering implements MethodOptimization {
} }
if (known != null && domTree.dominates(known.location, currentBlockIndex) && known.value != var if (known != null && domTree.dominates(known.location, currentBlockIndex) && known.value != var
&& namesCompatible) { && namesCompatible) {
eliminate = true;
map[var] = known.value; map[var] = known.value;
if (!noReplace) {
replaceMap[var] = known.value;
deletedVars[var] = true;
eliminate = true;
}
if (!name.isEmpty()) { if (!name.isEmpty()) {
program.variableAt(known.value).setDebugName(name); program.variableAt(known.value).setDebugName(name);
knownValues.put(name + ":" + value, known); knownValues.put(name + ":" + value, known);
@ -219,50 +238,49 @@ public class GlobalValueNumbering implements MethodOptimization {
} }
} }
private InstructionVisitor optimizer = new InstructionVisitor() { private InstructionVisitor constantPreprocessor = new AbstractInstructionVisitor() {
@Override private void setConstant(int index, Number value) {
public void visit(EmptyInstruction insn) { numericConstants[index] = value;
int knownIndex = constantIndexes.getOrDefault(value, -1);
if (knownIndex < 0) {
constantIndexes.put(value, index);
} else {
map[index] = knownIndex;
} }
@Override
public void visit(ClassConstantInstruction insn) {
}
@Override
public void visit(NullConstantInstruction insn) {
} }
@Override @Override
public void visit(IntegerConstantInstruction insn) { public void visit(IntegerConstantInstruction insn) {
numericConstants[insn.getReceiver().getIndex()] = insn.getConstant(); setConstant(insn.getReceiver().getIndex(), insn.getConstant());
} }
@Override @Override
public void visit(LongConstantInstruction insn) { public void visit(LongConstantInstruction insn) {
numericConstants[insn.getReceiver().getIndex()] = insn.getConstant(); setConstant(insn.getReceiver().getIndex(), insn.getConstant());
} }
@Override @Override
public void visit(FloatConstantInstruction insn) { public void visit(FloatConstantInstruction insn) {
numericConstants[insn.getReceiver().getIndex()] = insn.getConstant(); setConstant(insn.getReceiver().getIndex(), insn.getConstant());
} }
@Override @Override
public void visit(DoubleConstantInstruction insn) { public void visit(DoubleConstantInstruction insn) {
numericConstants[insn.getReceiver().getIndex()] = insn.getConstant(); setConstant(insn.getReceiver().getIndex(), insn.getConstant());
}
@Override
public void visit(StringConstantInstruction insn) {
} }
};
private InstructionVisitor optimizer = new AbstractInstructionVisitor() {
@Override @Override
public void visit(BinaryInstruction insn) { public void visit(BinaryInstruction insn) {
int a = map[insn.getFirstOperand().getIndex()]; int a = map[insn.getFirstOperand().getIndex()];
int p = replaceMap[insn.getFirstOperand().getIndex()];
int b = map[insn.getSecondOperand().getIndex()]; int b = map[insn.getSecondOperand().getIndex()];
insn.setFirstOperand(program.variableAt(a)); int q = replaceMap[insn.getSecondOperand().getIndex()];
insn.setSecondOperand(program.variableAt(b)); insn.setFirstOperand(program.variableAt(p));
insn.setSecondOperand(program.variableAt(q));
boolean commutative = false; boolean commutative = false;
boolean noReplace = false;
String value; String value;
switch (insn.getOperation()) { switch (insn.getOperation()) {
case ADD: case ADD:
@ -284,6 +302,7 @@ public class GlobalValueNumbering implements MethodOptimization {
break; break;
case COMPARE: case COMPARE:
value = "$"; value = "$";
noReplace = true;
break; break;
case AND: case AND:
value = "&"; value = "&";
@ -501,7 +520,8 @@ public class GlobalValueNumbering implements MethodOptimization {
@Override @Override
public void visit(NegateInstruction insn) { public void visit(NegateInstruction insn) {
int a = map[insn.getOperand().getIndex()]; int a = map[insn.getOperand().getIndex()];
insn.setOperand(program.variableAt(a)); int p = replaceMap[insn.getOperand().getIndex()];
insn.setOperand(program.variableAt(p));
bind(insn.getReceiver().getIndex(), "-@" + a); bind(insn.getReceiver().getIndex(), "-@" + a);
Number value = numericConstants[a]; Number value = numericConstants[a];
@ -541,20 +561,23 @@ public class GlobalValueNumbering implements MethodOptimization {
insn.getAssignee().setLabel(insn.getReceiver().getLabel()); insn.getAssignee().setLabel(insn.getReceiver().getLabel());
} }
map[insn.getReceiver().getIndex()] = map[insn.getAssignee().getIndex()]; map[insn.getReceiver().getIndex()] = map[insn.getAssignee().getIndex()];
replaceMap[insn.getReceiver().getIndex()] = replaceMap[insn.getAssignee().getIndex()];
eliminate = true; eliminate = true;
} }
@Override @Override
public void visit(CastInstruction insn) { public void visit(CastInstruction insn) {
int a = map[insn.getValue().getIndex()]; int a = map[insn.getValue().getIndex()];
insn.setValue(program.variableAt(a)); int p = replaceMap[insn.getValue().getIndex()];
insn.setValue(program.variableAt(p));
bind(insn.getReceiver().getIndex(), "@" + a + "::" + insn.getTargetType()); bind(insn.getReceiver().getIndex(), "@" + a + "::" + insn.getTargetType());
} }
@Override @Override
public void visit(CastNumberInstruction insn) { public void visit(CastNumberInstruction insn) {
int a = map[insn.getValue().getIndex()]; int a = map[insn.getValue().getIndex()];
insn.setValue(program.variableAt(a)); int p = replaceMap[insn.getValue().getIndex()];
insn.setValue(program.variableAt(p));
bind(insn.getReceiver().getIndex(), "@" + a + "::" + insn.getTargetType()); bind(insn.getReceiver().getIndex(), "@" + a + "::" + insn.getTargetType());
Number value = numericConstants[a]; Number value = numericConstants[a];
@ -581,7 +604,8 @@ public class GlobalValueNumbering implements MethodOptimization {
@Override @Override
public void visit(CastIntegerInstruction insn) { public void visit(CastIntegerInstruction insn) {
int a = map[insn.getValue().getIndex()]; int a = map[insn.getValue().getIndex()];
insn.setValue(program.variableAt(a)); int p = replaceMap[insn.getValue().getIndex()];
insn.setValue(program.variableAt(p));
bind(insn.getReceiver().getIndex(), "@" + a + "::" + insn.getTargetType() + " " + insn.getDirection()); bind(insn.getReceiver().getIndex(), "@" + a + "::" + insn.getTargetType() + " " + insn.getDirection());
Number value = numericConstants[a]; Number value = numericConstants[a];
@ -611,56 +635,48 @@ public class GlobalValueNumbering implements MethodOptimization {
@Override @Override
public void visit(BranchingInstruction insn) { public void visit(BranchingInstruction insn) {
int a = map[insn.getOperand().getIndex()]; int a = replaceMap[insn.getOperand().getIndex()];
insn.setOperand(program.variableAt(a)); insn.setOperand(program.variableAt(a));
} }
@Override @Override
public void visit(BinaryBranchingInstruction insn) { public void visit(BinaryBranchingInstruction insn) {
int a = map[insn.getFirstOperand().getIndex()]; int a = replaceMap[insn.getFirstOperand().getIndex()];
int b = map[insn.getSecondOperand().getIndex()]; int b = replaceMap[insn.getSecondOperand().getIndex()];
insn.setFirstOperand(program.variableAt(a)); insn.setFirstOperand(program.variableAt(a));
insn.setSecondOperand(program.variableAt(b)); insn.setSecondOperand(program.variableAt(b));
} }
@Override
public void visit(JumpInstruction insn) {
}
@Override @Override
public void visit(SwitchInstruction insn) { public void visit(SwitchInstruction insn) {
int a = map[insn.getCondition().getIndex()]; int a = replaceMap[insn.getCondition().getIndex()];
insn.setCondition(program.variableAt(a)); insn.setCondition(program.variableAt(a));
} }
@Override @Override
public void visit(ExitInstruction insn) { public void visit(ExitInstruction insn) {
if (insn.getValueToReturn() != null) { if (insn.getValueToReturn() != null) {
int a = map[insn.getValueToReturn().getIndex()]; int a = replaceMap[insn.getValueToReturn().getIndex()];
insn.setValueToReturn(program.variableAt(a)); insn.setValueToReturn(program.variableAt(a));
} }
} }
@Override @Override
public void visit(RaiseInstruction insn) { public void visit(RaiseInstruction insn) {
int a = map[insn.getException().getIndex()]; int a = replaceMap[insn.getException().getIndex()];
insn.setException(program.variableAt(a)); insn.setException(program.variableAt(a));
} }
@Override @Override
public void visit(ConstructArrayInstruction insn) { public void visit(ConstructArrayInstruction insn) {
int a = map[insn.getSize().getIndex()]; int a = replaceMap[insn.getSize().getIndex()];
insn.setSize(program.variableAt(a)); insn.setSize(program.variableAt(a));
} }
@Override
public void visit(ConstructInstruction insn) {
}
@Override @Override
public void visit(ConstructMultiArrayInstruction insn) { public void visit(ConstructMultiArrayInstruction insn) {
for (int i = 0; i < insn.getDimensions().size(); ++i) { for (int i = 0; i < insn.getDimensions().size(); ++i) {
int a = map[insn.getDimensions().get(i).getIndex()]; int a = replaceMap[insn.getDimensions().get(i).getIndex()];
insn.getDimensions().set(i, program.variableAt(a)); insn.getDimensions().set(i, program.variableAt(a));
} }
} }
@ -668,7 +684,7 @@ public class GlobalValueNumbering implements MethodOptimization {
@Override @Override
public void visit(GetFieldInstruction insn) { public void visit(GetFieldInstruction insn) {
if (insn.getInstance() != null) { if (insn.getInstance() != null) {
int instance = map[insn.getInstance().getIndex()]; int instance = replaceMap[insn.getInstance().getIndex()];
insn.setInstance(program.variableAt(instance)); insn.setInstance(program.variableAt(instance));
} }
} }
@ -676,55 +692,57 @@ public class GlobalValueNumbering implements MethodOptimization {
@Override @Override
public void visit(PutFieldInstruction insn) { public void visit(PutFieldInstruction insn) {
if (insn.getInstance() != null) { if (insn.getInstance() != null) {
int instance = map[insn.getInstance().getIndex()]; int instance = replaceMap[insn.getInstance().getIndex()];
insn.setInstance(program.variableAt(instance)); insn.setInstance(program.variableAt(instance));
} }
int val = map[insn.getValue().getIndex()]; int val = replaceMap[insn.getValue().getIndex()];
insn.setValue(program.variableAt(val)); insn.setValue(program.variableAt(val));
} }
@Override @Override
public void visit(ArrayLengthInstruction insn) { public void visit(ArrayLengthInstruction insn) {
int a = map[insn.getArray().getIndex()]; int a = map[insn.getArray().getIndex()];
insn.setArray(program.variableAt(a)); int p = replaceMap[insn.getArray().getIndex()];
insn.setArray(program.variableAt(p));
bind(insn.getReceiver().getIndex(), "@" + a + ".length"); bind(insn.getReceiver().getIndex(), "@" + a + ".length");
} }
@Override @Override
public void visit(CloneArrayInstruction insn) { public void visit(CloneArrayInstruction insn) {
int a = map[insn.getArray().getIndex()]; int a = replaceMap[insn.getArray().getIndex()];
insn.setArray(program.variableAt(a)); insn.setArray(program.variableAt(a));
} }
@Override @Override
public void visit(UnwrapArrayInstruction insn) { public void visit(UnwrapArrayInstruction insn) {
int a = map[insn.getArray().getIndex()]; int a = map[insn.getArray().getIndex()];
insn.setArray(program.variableAt(a)); int p = replaceMap[insn.getArray().getIndex()];
insn.setArray(program.variableAt(p));
bind(insn.getReceiver().getIndex(), "@" + a + ".data"); bind(insn.getReceiver().getIndex(), "@" + a + ".data");
} }
@Override @Override
public void visit(GetElementInstruction insn) { public void visit(GetElementInstruction insn) {
int a = map[insn.getArray().getIndex()]; int a = replaceMap[insn.getArray().getIndex()];
insn.setArray(program.variableAt(a)); insn.setArray(program.variableAt(a));
int index = map[insn.getIndex().getIndex()]; int index = replaceMap[insn.getIndex().getIndex()];
insn.setIndex(program.variableAt(index)); insn.setIndex(program.variableAt(index));
} }
@Override @Override
public void visit(PutElementInstruction insn) { public void visit(PutElementInstruction insn) {
int a = map[insn.getArray().getIndex()]; int a = replaceMap[insn.getArray().getIndex()];
insn.setArray(program.variableAt(a)); insn.setArray(program.variableAt(a));
int index = map[insn.getIndex().getIndex()]; int index = replaceMap[insn.getIndex().getIndex()];
insn.setIndex(program.variableAt(index)); insn.setIndex(program.variableAt(index));
int val = map[insn.getValue().getIndex()]; int val = replaceMap[insn.getValue().getIndex()];
insn.setValue(program.variableAt(val)); insn.setValue(program.variableAt(val));
} }
@Override @Override
public void visit(InvokeInstruction insn) { public void visit(InvokeInstruction insn) {
if (insn.getInstance() != null) { if (insn.getInstance() != null) {
int instance = map[insn.getInstance().getIndex()]; int instance = replaceMap[insn.getInstance().getIndex()];
insn.setInstance(program.variableAt(instance)); insn.setInstance(program.variableAt(instance));
} }
insn.replaceArguments(mapper); insn.replaceArguments(mapper);
@ -736,35 +754,33 @@ public class GlobalValueNumbering implements MethodOptimization {
insn.getArguments().replaceAll(mapper); insn.getArguments().replaceAll(mapper);
} }
private UnaryOperator<Variable> mapper = var -> program.variableAt(map[var.getIndex()]); private UnaryOperator<Variable> mapper = var -> program.variableAt(replaceMap[var.getIndex()]);
@Override @Override
public void visit(IsInstanceInstruction insn) { public void visit(IsInstanceInstruction insn) {
int val = map[insn.getValue().getIndex()]; int val = map[insn.getValue().getIndex()];
insn.setValue(program.variableAt(val)); int p = replaceMap[insn.getValue().getIndex()];
insn.setValue(program.variableAt(p));
bind(insn.getReceiver().getIndex(), "@" + val + " :? " + insn.getType()); bind(insn.getReceiver().getIndex(), "@" + val + " :? " + insn.getType());
} }
@Override
public void visit(InitClassInstruction insn) {
}
@Override @Override
public void visit(NullCheckInstruction insn) { public void visit(NullCheckInstruction insn) {
int val = map[insn.getValue().getIndex()]; int val = map[insn.getValue().getIndex()];
insn.setValue(program.variableAt(val)); int p = replaceMap[insn.getValue().getIndex()];
insn.setValue(program.variableAt(p));
bind(insn.getReceiver().getIndex(), "nullCheck @" + val); bind(insn.getReceiver().getIndex(), "nullCheck @" + val);
} }
@Override @Override
public void visit(MonitorEnterInstruction insn) { public void visit(MonitorEnterInstruction insn) {
int val = map[insn.getObjectRef().getIndex()]; int val = replaceMap[insn.getObjectRef().getIndex()];
insn.setObjectRef(program.variableAt(val)); insn.setObjectRef(program.variableAt(val));
} }
@Override @Override
public void visit(MonitorExitInstruction insn) { public void visit(MonitorExitInstruction insn) {
int val = map[insn.getObjectRef().getIndex()]; int val = replaceMap[insn.getObjectRef().getIndex()];
insn.setObjectRef(program.variableAt(val)); insn.setObjectRef(program.variableAt(val));
} }
}; };

View File

@ -24,7 +24,7 @@ function $rt_nextId() {
return x; return x;
} }
function $rt_compare(a, b) { function $rt_compare(a, b) {
return a > b ? 1 : a < b ? -1 : 0; return a > b ? 1 : a < b ? -1 : a === b ? 0 : 1;
} }
function $rt_isInstance(obj, cls) { function $rt_isInstance(obj, cls) {
return obj !== null && !!obj.constructor.$meta && $rt_isAssignable(obj.constructor, cls); return obj !== null && !!obj.constructor.$meta && $rt_isAssignable(obj.constructor, cls);