Refactor phi placement

This commit is contained in:
Alexey Andreev 2016-05-14 23:58:48 +03:00
parent 3d69167280
commit 5ac195df5a
4 changed files with 45 additions and 85 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2013 Alexey Andreev. * Copyright 2016 Alexey Andreev.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.teavm.parsing; package org.teavm.model.util;
import java.util.*; import java.util.*;
import org.teavm.common.DominatorTree; import org.teavm.common.DominatorTree;
@ -21,72 +21,39 @@ import org.teavm.common.Graph;
import org.teavm.common.GraphUtils; import org.teavm.common.GraphUtils;
import org.teavm.model.*; import org.teavm.model.*;
import org.teavm.model.instructions.*; import org.teavm.model.instructions.*;
import org.teavm.model.util.DefinitionExtractor;
import org.teavm.model.util.ProgramUtils;
/** public class PhiUpdater {
*
* @author Alexey Andreev
*/
public class SSATransformer {
private Program program; private Program program;
private Graph cfg; private Graph cfg;
private DominatorTree domTree;
private int[][] domFrontiers; private int[][] domFrontiers;
private Variable[] variableMap; private Variable[] variableMap;
private BasicBlock currentBlock; private BasicBlock currentBlock;
private Phi[][] phiMap; private Phi[][] phiMap;
private int[][] phiIndexMap; private int[][] phiIndexMap;
private ValueType[] arguments;
private VariableDebugInformation variableDebugInfo;
private Map<Integer, String> variableDebugMap = new HashMap<>();
public void transformToSSA(Program program, VariableDebugInformation variableDebugInfo, ValueType[] arguments) { public void updatePhis(Program program, Variable[] arguments) {
if (program.basicBlockCount() == 0) { if (program.basicBlockCount() == 0) {
return; return;
} }
this.program = program; this.program = program;
this.variableDebugInfo = variableDebugInfo;
this.arguments = arguments;
variableDebugMap.clear();
cfg = ProgramUtils.buildControlFlowGraphWithTryCatch(program); cfg = ProgramUtils.buildControlFlowGraphWithTryCatch(program);
domTree = GraphUtils.buildDominatorTree(cfg); DominatorTree domTree = GraphUtils.buildDominatorTree(cfg);
domFrontiers = new int[cfg.size()][]; domFrontiers = new int[cfg.size()][];
variableMap = new Variable[program.variableCount()]; variableMap = new Variable[program.variableCount()];
for (int i = 0; i < arguments.length; ++i) {
variableMap[i] = arguments[i];
}
phiMap = new Phi[program.basicBlockCount()][]; phiMap = new Phi[program.basicBlockCount()][];
phiIndexMap = new int[program.basicBlockCount()][]; phiIndexMap = new int[program.basicBlockCount()][];
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()];
} }
applySignature();
domFrontiers = GraphUtils.findDominanceFrontiers(cfg, domTree); domFrontiers = GraphUtils.findDominanceFrontiers(cfg, domTree);
estimatePhis(); estimatePhis();
renameVariables(); renameVariables();
} }
private void applySignature() {
if (program.variableCount() == 0) {
return;
}
int index = 0;
variableMap[index] = program.variableAt(index);
++index;
for (int i = 0; i < arguments.length; ++i) {
variableMap[index] = program.variableAt(i + 1);
++index;
ValueType arg = arguments[i];
if (arg instanceof ValueType.Primitive) {
PrimitiveType kind = ((ValueType.Primitive) arg).getKind();
if (kind == PrimitiveType.LONG || kind == PrimitiveType.DOUBLE) {
variableMap[index] = variableMap[index - 1];
++index;
}
}
}
arguments = null;
}
private void estimatePhis() { private void estimatePhis() {
DefinitionExtractor definitionExtractor = new DefinitionExtractor(); DefinitionExtractor definitionExtractor = new DefinitionExtractor();
for (int i = 0; i < program.basicBlockCount(); ++i) { for (int i = 0; i < program.basicBlockCount(); ++i) {
@ -97,10 +64,13 @@ public class SSATransformer {
markAssignment(var); markAssignment(var);
} }
} }
for (Phi phi : currentBlock.getPhis()) {
markAssignment(phi.getReceiver());
}
} }
} }
static class Task { private static class Task {
Variable[] variables; Variable[] variables;
BasicBlock block; BasicBlock block;
} }
@ -161,19 +131,17 @@ public class SSATransformer {
specialPhis.get(currentBlock.getIndex()).add(phi); specialPhis.get(currentBlock.getIndex()).add(phi);
} }
for (Instruction insn : currentBlock.getInstructions()) { for (Instruction insn : currentBlock.getInstructions()) {
variableDebugMap.putAll(variableDebugInfo.getDebugNames(insn));
insn.acceptVisitor(consumer); insn.acceptVisitor(consumer);
} }
int[] successors = domGraph.outgoingEdges(currentBlock.getIndex()); int[] successors = domGraph.outgoingEdges(currentBlock.getIndex());
for (int i = 0; i < successors.length; ++i) { for (int successor : successors) {
Task next = new Task(); Task next = new Task();
next.variables = Arrays.copyOf(variableMap, variableMap.length); next.variables = Arrays.copyOf(variableMap, variableMap.length);
next.block = program.basicBlockAt(successors[i]); next.block = program.basicBlockAt(successor);
stack[head++] = next; stack[head++] = next;
} }
successors = cfg.outgoingEdges(currentBlock.getIndex()); successors = cfg.outgoingEdges(currentBlock.getIndex());
for (int i = 0; i < successors.length; ++i) { for (int successor : successors) {
int successor = successors[i];
int[] phiIndexes = phiIndexMap[successor]; int[] phiIndexes = phiIndexMap[successor];
List<Phi> phis = program.basicBlockAt(successor).getPhis(); List<Phi> phis = program.basicBlockAt(successor).getPhis();
for (int j = 0; j < phis.size(); ++j) { for (int j = 0; j < phis.size(); ++j) {
@ -231,10 +199,6 @@ public class SSATransformer {
if (mappedVar == null) { if (mappedVar == null) {
throw new AssertionError(); throw new AssertionError();
} }
String debugName = variableDebugMap.get(var.getIndex());
if (debugName != null) {
mappedVar.getDebugNames().add(debugName);
}
return mappedVar; return mappedVar;
} }
@ -440,9 +404,6 @@ public class SSATransformer {
@Override @Override
public void visit(UnwrapArrayInstruction insn) { public void visit(UnwrapArrayInstruction insn) {
insn.setArray(use(insn.getArray())); insn.setArray(use(insn.getArray()));
for (String debugName : insn.getArray().getDebugNames()) {
variableDebugMap.put(insn.getReceiver().getIndex(), debugName + ".data");
}
insn.setReceiver(define(insn.getReceiver())); insn.setReceiver(define(insn.getReceiver()));
} }

View File

@ -17,12 +17,14 @@ package org.teavm.parsing;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type; import org.objectweb.asm.Type;
import org.objectweb.asm.commons.JSRInlinerAdapter; import org.objectweb.asm.commons.JSRInlinerAdapter;
import org.objectweb.asm.tree.*; import org.objectweb.asm.tree.*;
import org.teavm.model.*; import org.teavm.model.*;
import org.teavm.model.util.PhiUpdater;
import org.teavm.optimization.UnreachableBasicBlockEliminator; import org.teavm.optimization.UnreachableBasicBlockEliminator;
/** /**
@ -47,8 +49,8 @@ public final class Parser {
programParser.setFileName(fileName); programParser.setFileName(fileName);
Program program = programParser.parse(node, className); Program program = programParser.parse(node, className);
new UnreachableBasicBlockEliminator().optimize(program); new UnreachableBasicBlockEliminator().optimize(program);
SSATransformer ssaProducer = new SSATransformer(); PhiUpdater phiUpdater = new PhiUpdater();
ssaProducer.transformToSSA(program, programParser, method.getParameterTypes()); phiUpdater.updatePhis(program, applySignature(program, method.getParameterTypes()));
method.setProgram(program); method.setProgram(program);
parseAnnotations(method.getAnnotations(), node.visibleAnnotations, node.invisibleAnnotations); parseAnnotations(method.getAnnotations(), node.visibleAnnotations, node.invisibleAnnotations);
while (program.variableCount() <= method.parameterCount()) { while (program.variableCount() <= method.parameterCount()) {
@ -65,6 +67,31 @@ public final class Parser {
return method; return method;
} }
private static Variable[] applySignature(Program program, ValueType[] arguments) {
if (program.variableCount() == 0) {
return new Variable[0];
}
Variable[] variableMap = new Variable[program.variableCount()];
int index = 0;
variableMap[index] = program.variableAt(index);
++index;
for (int i = 0; i < arguments.length; ++i) {
variableMap[index] = program.variableAt(i + 1);
++index;
ValueType arg = arguments[i];
if (arg instanceof ValueType.Primitive) {
PrimitiveType kind = ((ValueType.Primitive) arg).getKind();
if (kind == PrimitiveType.LONG || kind == PrimitiveType.DOUBLE) {
variableMap[index] = variableMap[index - 1];
++index;
}
}
}
return Arrays.copyOf(variableMap, index);
}
public static ClassHolder parseClass(ClassNode node) { public static ClassHolder parseClass(ClassNode node) {
ClassHolder cls = new ClassHolder(node.name.replace('/', '.')); ClassHolder cls = new ClassHolder(node.name.replace('/', '.'));
parseModifiers(node.access, cls); parseModifiers(node.access, cls);

View File

@ -27,7 +27,7 @@ import org.teavm.model.util.ProgramUtils;
* *
* @author Alexey Andreev * @author Alexey Andreev
*/ */
public class ProgramParser implements VariableDebugInformation { public class ProgramParser {
private static final byte ROOT = 0; private static final byte ROOT = 0;
private static final byte SINGLE = 1; private static final byte SINGLE = 1;
private static final byte DOUBLE_FIRST_HALF = 2; private static final byte DOUBLE_FIRST_HALF = 2;
@ -159,7 +159,6 @@ public class ProgramParser implements VariableDebugInformation {
return depth; return depth;
} }
@Override
public Map<Integer, String> getDebugNames(Instruction insn) { public Map<Integer, String> getDebugNames(Instruction insn) {
Map<Integer, String> map = variableDebugNames.get(insn); Map<Integer, String> map = variableDebugNames.get(insn);
return map != null ? Collections.unmodifiableMap(map) : Collections.emptyMap(); return map != null ? Collections.unmodifiableMap(map) : Collections.emptyMap();

View File

@ -1,27 +0,0 @@
/*
* Copyright 2014 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.parsing;
import java.util.Map;
import org.teavm.model.Instruction;
/**
*
* @author Alexey Andreev
*/
public interface VariableDebugInformation {
Map<Integer, String> getDebugNames(Instruction insn);
}