Improve node splitting

This commit is contained in:
Alexey Andreev 2015-03-04 18:28:26 +04:00
parent 5ea55675f1
commit d4824ff9d2
7 changed files with 164 additions and 8 deletions

View File

@ -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.common.irreducible; package org.teavm.common;
import com.carrotsearch.hppc.IntOpenHashSet; import com.carrotsearch.hppc.IntOpenHashSet;
import com.carrotsearch.hppc.IntSet; import com.carrotsearch.hppc.IntSet;
@ -21,7 +21,6 @@ import com.carrotsearch.hppc.cursors.IntCursor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.teavm.common.*;
/** /**
* *

View File

@ -0,0 +1,97 @@
/*
* Copyright 2015 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.common;
import com.carrotsearch.hppc.IntIntMap;
import com.carrotsearch.hppc.IntIntOpenHashMap;
/**
*
* @author Alexey Andreev
*/
public class DefaultGraphSplittingBackend implements GraphSplittingBackend {
private MutableDirectedGraph graph;
private int index;
private IntegerArray prototypeNodes;
private IntegerArray copyIndexes;
private int[] copyCount;
public DefaultGraphSplittingBackend(Graph graph) {
this.graph = new MutableDirectedGraph(graph);
prototypeNodes = new IntegerArray(graph.size());
copyIndexes = new IntegerArray(graph.size());
copyCount = new int[graph.size()];
index = graph.size();
for (int i = 0; i < graph.size(); ++i) {
prototypeNodes.add(i);
copyIndexes.add(i);
}
}
public Graph getGraph() {
return graph.copyToImmutable();
}
public int prototype(int index) {
return prototypeNodes.get(index);
}
public int copyIndex(int index) {
return copyIndexes.get(index);
}
@Override
public int[] split(int[] domain, int[] nodes) {
int[] copies = new int[nodes.length];
IntIntMap map = new IntIntOpenHashMap();
for (int i = 0; i < nodes.length; ++i) {
copies[i] = index++;
map.put(nodes[i], copies[i]);
int proto = prototypeNodes.get(nodes[i]);
prototypeNodes.add(proto);
copyIndexes.add(++copyCount[proto]);
}
for (int i = 0; i < domain.length; ++i) {
int node = domain[i];
for (int succ : graph.outgoingEdges(node)) {
int succCopy = map.get(succ);
if (succCopy == 0) {
continue;
}
--succCopy;
graph.deleteEdge(node, succ);
graph.addEdge(node, succCopy);
}
}
for (int i = 0; i < nodes.length; ++i) {
int node = nodes[i];
int nodeCopy = copies[i];
for (int succ : graph.outgoingEdges(node)) {
int succCopy = map.get(succ);
if (succCopy != 0) {
graph.addEdge(nodeCopy, succCopy - 1);
} else {
graph.addEdge(nodeCopy, succ);
}
}
}
return copies;
}
}

View File

@ -65,6 +65,16 @@ public class GraphBuilder {
} }
} }
public void removeEdge(int from, int to) {
if (to < 0 || from < 0) {
throw new IllegalArgumentException();
}
if (from >= addedEdges.size() || to >= addedEdges.size()) {
return;
}
addedEdges.get(from).removeAllOccurrences(to);
}
public Graph build() { public Graph build() {
if (builtGraph == null) { if (builtGraph == null) {
IntSet[] incomingEdges = new IntSet[sz]; IntSet[] incomingEdges = new IntSet[sz];

View File

@ -13,12 +13,12 @@
* 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.common.irreducible; package org.teavm.common;
/** /**
* *
* @author Alexey Andreev <konsoletyper@gmail.com> * @author Alexey Andreev <konsoletyper@gmail.com>
*/ */
public interface GraphSplittingBackend { public interface GraphSplittingBackend {
int[][] split(int[][] domain, int[][] nodes); int[] split(int[] domain, int[] nodes);
} }

View File

@ -179,6 +179,10 @@ public final class GraphUtils {
return graph.build(); return graph.build();
} }
public static void splitIrreducibleGraph(Graph graph, int[] weights, GraphSplittingBackend backend) {
new IrreducibleGraphConverter().convertToReducible(graph, weights, backend);
}
public static int[][] findDominanceFrontiers(Graph cfg, DominatorTree domTree) { public static int[][] findDominanceFrontiers(Graph cfg, DominatorTree domTree) {
IntegerArray[] tmpFrontiers = new IntegerArray[cfg.size()]; IntegerArray[] tmpFrontiers = new IntegerArray[cfg.size()];
int[][] domFrontiers = new int[cfg.size()][]; int[][] domFrontiers = new int[cfg.size()][];

View File

@ -13,13 +13,12 @@
* 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.common.irreducible; package org.teavm.common;
import com.carrotsearch.hppc.IntOpenHashSet; import com.carrotsearch.hppc.IntOpenHashSet;
import com.carrotsearch.hppc.IntSet; import com.carrotsearch.hppc.IntSet;
import com.carrotsearch.hppc.cursors.IntCursor; import com.carrotsearch.hppc.cursors.IntCursor;
import java.util.Arrays; import java.util.Arrays;
import org.teavm.common.*;
/** /**
* <p>Converts irreducible graph to reducible one using node splitting algorithm described at * <p>Converts irreducible graph to reducible one using node splitting algorithm described at
@ -28,7 +27,7 @@ import org.teavm.common.*;
* *
* @author Alexey Andreev * @author Alexey Andreev
*/ */
public class IrreducibleGraphConverter { class IrreducibleGraphConverter {
private Graph cfg; private Graph cfg;
private int totalNodeCount; private int totalNodeCount;
private GraphSplittingBackend backend; private GraphSplittingBackend backend;
@ -174,7 +173,7 @@ public class IrreducibleGraphConverter {
} }
// Delegate splitting to domain // Delegate splitting to domain
int[][] newNodes = backend.split(mappedDomain, mappedNonDomain); int[][] newNodes = unflatten(backend.split(flatten(mappedDomain), flatten(mappedNonDomain)), mappedNonDomain);
for (int[] nodes : newNodes) { for (int[] nodes : newNodes) {
totalNodeCount += nodes.length; totalNodeCount += nodes.length;
} }
@ -242,6 +241,35 @@ public class IrreducibleGraphConverter {
handleLoops(new DJGraph(builder.build(), mappedWeight), newNodeMap); handleLoops(new DJGraph(builder.build(), mappedWeight), newNodeMap);
} }
private static int[] flatten(int[][] array) {
int count = 0;
for (int i = 0; i < array.length; ++i) {
count += array[i].length;
}
int[] flat = new int[count];
int index = 0;
for (int i = 0; i < array.length; ++i) {
int[] part = array[i];
for (int j = 0; j < part.length; ++j) {
flat[index++] = part[j];
}
}
return flat;
}
private static int[][] unflatten(int[] flat, int[][] pattern) {
int[][] rough = new int[pattern.length][];
int index = 0;
for (int i = 0; i < rough.length; ++i) {
int[] part = new int[pattern[i].length];
for (int j = 0; j < part.length; ++j) {
part[j] = flat[index++];
}
rough[i] = part;
}
return rough;
}
static class DJGraphNodeFilter implements GraphNodeFilter { static class DJGraphNodeFilter implements GraphNodeFilter {
private DJGraph graph; private DJGraph graph;
private int level; private int level;

View File

@ -42,6 +42,16 @@ public class MutableDirectedGraph implements Graph {
} }
} }
public Graph copyToImmutable() {
GraphBuilder builder = new GraphBuilder(successors.size());
for (int i = 0; i < successors.size(); ++i) {
for (IntCursor cursor : successors.get(i)) {
builder.addEdge(i, cursor.value);
}
}
return builder.build();
}
@Override @Override
public int size() { public int size() {
return successors.size(); return successors.size();
@ -57,6 +67,14 @@ public class MutableDirectedGraph implements Graph {
predecessors.get(to).add(from); predecessors.get(to).add(from);
} }
public void deleteEdge(int from, int to) {
if (from >= successors.size() || to >= successors.size()) {
return;
}
successors.get(from).removeAllOccurrences(to);
predecessors.get(to).removeAllOccurrences(from);
}
public void detachNode(int node) { public void detachNode(int node) {
for (IntCursor succ : successors.get(node)) { for (IntCursor succ : successors.get(node)) {
predecessors.get(succ.value).removeAllOccurrences(node); predecessors.get(succ.value).removeAllOccurrences(node);