Remove unused parts of call graph API

This commit is contained in:
Alexey Andreev 2017-11-12 23:06:58 +03:00
parent edfbe01a7f
commit 1b0bacd90c
7 changed files with 4 additions and 182 deletions

View File

@ -36,6 +36,4 @@ public interface CallGraph {
CallGraphNode getNode(MethodReference method); CallGraphNode getNode(MethodReference method);
Collection<? extends FieldAccessSite> getFieldAccess(FieldReference reference); Collection<? extends FieldAccessSite> getFieldAccess(FieldReference reference);
Collection<? extends ClassAccessSite> getClassAccess(String className);
} }

View File

@ -52,6 +52,4 @@ public interface CallGraphNode {
Collection<? extends CallSite> getCallerCallSites(); Collection<? extends CallSite> getCallerCallSites();
Collection<? extends FieldAccessSite> getFieldAccessSites(); Collection<? extends FieldAccessSite> getFieldAccessSites();
Collection<? extends ClassAccessSite> getClassAccessSites();
} }

View File

@ -1,26 +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.callgraph;
import org.teavm.model.TextLocation;
public interface ClassAccessSite {
TextLocation getLocation();
CallGraphNode getCallee();
String getClassName();
}

View File

@ -35,7 +35,6 @@ import org.teavm.model.MethodReference;
public class DefaultCallGraph implements CallGraph, Serializable { public class DefaultCallGraph implements CallGraph, Serializable {
Map<MethodReference, DefaultCallGraphNode> nodes = new LinkedHashMap<>(); Map<MethodReference, DefaultCallGraphNode> nodes = new LinkedHashMap<>();
Map<FieldReference, Set<DefaultFieldAccessSite>> fieldAccessSites = new LinkedHashMap<>(); Map<FieldReference, Set<DefaultFieldAccessSite>> fieldAccessSites = new LinkedHashMap<>();
Map<String, Set<DefaultClassAccessSite>> classAccessSites = new LinkedHashMap<>();
@Override @Override
public DefaultCallGraphNode getNode(MethodReference method) { public DefaultCallGraphNode getNode(MethodReference method) {
@ -52,16 +51,6 @@ public class DefaultCallGraph implements CallGraph, Serializable {
fieldAccessSites.computeIfAbsent(accessSite.getField(), k -> new HashSet<>()).add(accessSite); fieldAccessSites.computeIfAbsent(accessSite.getField(), k -> new HashSet<>()).add(accessSite);
} }
@Override
public Collection<DefaultClassAccessSite> getClassAccess(String className) {
Set<DefaultClassAccessSite> resultSet = classAccessSites.get(className);
return resultSet != null ? Collections.unmodifiableSet(resultSet) : Collections.emptySet();
}
void addClassAccess(DefaultClassAccessSite accessSite) {
classAccessSites.computeIfAbsent(accessSite.getClassName(), k -> new HashSet<>()).add(accessSite);
}
private void writeObject(ObjectOutputStream out) throws IOException { private void writeObject(ObjectOutputStream out) throws IOException {
SerializableCallGraphBuilder builder = new SerializableCallGraphBuilder(); SerializableCallGraphBuilder builder = new SerializableCallGraphBuilder();
out.writeObject(builder.build(this)); out.writeObject(builder.build(this));
@ -71,7 +60,6 @@ public class DefaultCallGraph implements CallGraph, Serializable {
SerializableCallGraph scg = (SerializableCallGraph) in.readObject(); SerializableCallGraph scg = (SerializableCallGraph) in.readObject();
nodes = new LinkedHashMap<>(); nodes = new LinkedHashMap<>();
fieldAccessSites = new LinkedHashMap<>(); fieldAccessSites = new LinkedHashMap<>();
classAccessSites = new LinkedHashMap<>();
new CallGraphBuilder().build(scg, this); new CallGraphBuilder().build(scg, this);
} }
@ -83,12 +71,9 @@ public class DefaultCallGraph implements CallGraph, Serializable {
ObjectIntMap<DefaultCallSite> callSiteToIndex = new ObjectIntOpenHashMap<>(); ObjectIntMap<DefaultCallSite> callSiteToIndex = new ObjectIntOpenHashMap<>();
List<SerializableCallGraph.FieldAccess> fieldAccessList = new ArrayList<>(); List<SerializableCallGraph.FieldAccess> fieldAccessList = new ArrayList<>();
ObjectIntMap<DefaultFieldAccessSite> fieldAccessToIndex = new ObjectIntOpenHashMap<>(); ObjectIntMap<DefaultFieldAccessSite> fieldAccessToIndex = new ObjectIntOpenHashMap<>();
List<SerializableCallGraph.ClassAccess> classAccessList = new ArrayList<>();
ObjectIntMap<DefaultClassAccessSite> classAccessToIndex = new ObjectIntOpenHashMap<>();
List<DefaultCallGraphNode> nodesToProcess = new ArrayList<>(); List<DefaultCallGraphNode> nodesToProcess = new ArrayList<>();
List<DefaultCallSite> callSitesToProcess = new ArrayList<>(); List<DefaultCallSite> callSitesToProcess = new ArrayList<>();
List<DefaultFieldAccessSite> fieldAccessToProcess = new ArrayList<>(); List<DefaultFieldAccessSite> fieldAccessToProcess = new ArrayList<>();
List<DefaultClassAccessSite> classAccessToProcess = new ArrayList<>();
SerializableCallGraph build(DefaultCallGraph cg) { SerializableCallGraph build(DefaultCallGraph cg) {
SerializableCallGraph scg = new SerializableCallGraph(); SerializableCallGraph scg = new SerializableCallGraph();
@ -99,9 +84,6 @@ public class DefaultCallGraph implements CallGraph, Serializable {
scg.fieldAccessIndexes = cg.fieldAccessSites.values().stream() scg.fieldAccessIndexes = cg.fieldAccessSites.values().stream()
.flatMapToInt(accessSites -> accessSites.stream().mapToInt(this::getFieldAccess)) .flatMapToInt(accessSites -> accessSites.stream().mapToInt(this::getFieldAccess))
.toArray(); .toArray();
scg.classAccessIndexes = cg.classAccessSites.values().stream()
.flatMapToInt(accessSites -> accessSites.stream().mapToInt(this::getClassAccess))
.toArray();
while (step()) { while (step()) {
// just repeat // just repeat
@ -110,13 +92,12 @@ public class DefaultCallGraph implements CallGraph, Serializable {
scg.nodes = nodes.toArray(new SerializableCallGraph.Node[0]); scg.nodes = nodes.toArray(new SerializableCallGraph.Node[0]);
scg.callSites = callSites.toArray(new SerializableCallGraph.CallSite[0]); scg.callSites = callSites.toArray(new SerializableCallGraph.CallSite[0]);
scg.fieldAccessList = fieldAccessList.toArray(new SerializableCallGraph.FieldAccess[0]); scg.fieldAccessList = fieldAccessList.toArray(new SerializableCallGraph.FieldAccess[0]);
scg.classAccessList = classAccessList.toArray(new SerializableCallGraph.ClassAccess[0]);
return scg; return scg;
} }
boolean step() { boolean step() {
return processNodes() | processCallSites() | processFieldAccess() | processClassAccess(); return processNodes() | processCallSites() | processFieldAccess();
} }
boolean processNodes() { boolean processNodes() {
@ -134,9 +115,6 @@ public class DefaultCallGraph implements CallGraph, Serializable {
serializableNode.fieldAccessSites = node.getFieldAccessSites().stream() serializableNode.fieldAccessSites = node.getFieldAccessSites().stream()
.mapToInt(this::getFieldAccess) .mapToInt(this::getFieldAccess)
.toArray(); .toArray();
serializableNode.classAccessSites = node.getClassAccessSites().stream()
.mapToInt(this::getClassAccess)
.toArray();
hasAny = true; hasAny = true;
} }
nodesToProcess.clear(); nodesToProcess.clear();
@ -171,20 +149,6 @@ public class DefaultCallGraph implements CallGraph, Serializable {
return hasAny; return hasAny;
} }
boolean processClassAccess() {
boolean hasAny = false;
for (DefaultClassAccessSite accessSite : classAccessToProcess.toArray(new DefaultClassAccessSite[0])) {
int index = classAccessToIndex.get(accessSite);
SerializableCallGraph.ClassAccess sca = classAccessList.get(index);
sca.location = accessSite.getLocation();
sca.className = accessSite.getClassName();
sca.callee = getNode(accessSite.getCallee());
hasAny = true;
}
classAccessToProcess.clear();
return hasAny;
}
private int getNode(DefaultCallGraphNode node) { private int getNode(DefaultCallGraphNode node) {
int index = nodeToIndex.getOrDefault(node, -1); int index = nodeToIndex.getOrDefault(node, -1);
if (index < 0) { if (index < 0) {
@ -217,24 +181,12 @@ public class DefaultCallGraph implements CallGraph, Serializable {
} }
return index; return index;
} }
private int getClassAccess(DefaultClassAccessSite classAccessSite) {
int index = classAccessToIndex.getOrDefault(classAccessSite, -1);
if (index < 0) {
index = classAccessToIndex.size();
classAccessToIndex.put(classAccessSite, index);
classAccessList.add(new SerializableCallGraph.ClassAccess());
classAccessToProcess.add(classAccessSite);
}
return index;
}
} }
static class CallGraphBuilder { static class CallGraphBuilder {
List<DefaultCallGraphNode> nodes = new ArrayList<>(); List<DefaultCallGraphNode> nodes = new ArrayList<>();
List<DefaultCallSite> callSites = new ArrayList<>(); List<DefaultCallSite> callSites = new ArrayList<>();
List<DefaultFieldAccessSite> fieldAccessList = new ArrayList<>(); List<DefaultFieldAccessSite> fieldAccessList = new ArrayList<>();
List<DefaultClassAccessSite> classAccessList = new ArrayList<>();
void build(SerializableCallGraph scg, DefaultCallGraph cg) { void build(SerializableCallGraph scg, DefaultCallGraph cg) {
for (SerializableCallGraph.Node serializableNode : scg.nodes) { for (SerializableCallGraph.Node serializableNode : scg.nodes) {
@ -246,9 +198,6 @@ public class DefaultCallGraph implements CallGraph, Serializable {
for (SerializableCallGraph.FieldAccess sfa : scg.fieldAccessList) { for (SerializableCallGraph.FieldAccess sfa : scg.fieldAccessList) {
fieldAccessList.add(new DefaultFieldAccessSite(sfa.location, nodes.get(sfa.callee), sfa.field)); fieldAccessList.add(new DefaultFieldAccessSite(sfa.location, nodes.get(sfa.callee), sfa.field));
} }
for (SerializableCallGraph.ClassAccess sca : scg.classAccessList) {
classAccessList.add(new DefaultClassAccessSite(sca.location, nodes.get(sca.callee), sca.className));
}
for (int index : scg.nodeIndexes) { for (int index : scg.nodeIndexes) {
DefaultCallGraphNode node = nodes.get(index); DefaultCallGraphNode node = nodes.get(index);
@ -257,9 +206,6 @@ public class DefaultCallGraph implements CallGraph, Serializable {
for (int index : scg.fieldAccessIndexes) { for (int index : scg.fieldAccessIndexes) {
cg.addFieldAccess(fieldAccessList.get(index)); cg.addFieldAccess(fieldAccessList.get(index));
} }
for (int index : scg.classAccessIndexes) {
cg.addClassAccess(classAccessList.get(index));
}
} }
} }
} }

View File

@ -28,14 +28,12 @@ import org.teavm.model.TextLocation;
public class DefaultCallGraphNode implements CallGraphNode { public class DefaultCallGraphNode implements CallGraphNode {
private DefaultCallGraph graph; private DefaultCallGraph graph;
private MethodReference method; private MethodReference method;
Set<DefaultCallSite> callSites = new HashSet<>(); private Set<DefaultCallSite> callSites = new HashSet<>();
private Set<DefaultCallSite> safeCallSites; private Set<DefaultCallSite> safeCallSites;
List<DefaultCallSite> callerCallSites = new ArrayList<>(); private List<DefaultCallSite> callerCallSites = new ArrayList<>();
private List<DefaultCallSite> safeCallersCallSites; private List<DefaultCallSite> safeCallersCallSites;
Set<DefaultFieldAccessSite> fieldAccessSites = new HashSet<>(); private Set<DefaultFieldAccessSite> fieldAccessSites = new HashSet<>();
private Set<DefaultFieldAccessSite> safeFieldAccessSites; private Set<DefaultFieldAccessSite> safeFieldAccessSites;
Set<DefaultClassAccessSite> classAccessSites = new HashSet<>();
private Set<DefaultClassAccessSite> safeClassAccessSites;
DefaultCallGraphNode(DefaultCallGraph graph, MethodReference method) { DefaultCallGraphNode(DefaultCallGraph graph, MethodReference method) {
this.graph = graph; this.graph = graph;
@ -100,22 +98,4 @@ public class DefaultCallGraphNode implements CallGraphNode {
return false; return false;
} }
} }
@Override
public Collection<DefaultClassAccessSite> getClassAccessSites() {
if (safeClassAccessSites == null) {
safeClassAccessSites = Collections.unmodifiableSet(classAccessSites);
}
return safeClassAccessSites;
}
public boolean addClassAccess(String className, TextLocation location) {
DefaultClassAccessSite site = new DefaultClassAccessSite(location, this, className);
if (classAccessSites.add(site)) {
graph.addClassAccess(site);
return true;
} else {
return false;
}
}
} }

View File

@ -1,66 +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.callgraph;
import java.io.Serializable;
import java.util.Objects;
import org.teavm.model.TextLocation;
public class DefaultClassAccessSite implements ClassAccessSite, Serializable {
private TextLocation location;
private DefaultCallGraphNode callee;
private String className;
DefaultClassAccessSite(TextLocation location, DefaultCallGraphNode callee, String className) {
this.location = location;
this.callee = callee;
this.className = className;
}
@Override
public TextLocation getLocation() {
return location;
}
@Override
public DefaultCallGraphNode getCallee() {
return callee;
}
@Override
public String getClassName() {
return className;
}
@Override
public int hashCode() {
return Objects.hash(location, callee, className);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof DefaultClassAccessSite)) {
return false;
}
DefaultClassAccessSite other = (DefaultClassAccessSite) obj;
return Objects.equals(location, other.location)
&& Objects.equals(callee, other.callee)
&& Objects.equals(className, other.className);
}
}

View File

@ -27,14 +27,12 @@ class SerializableCallGraph implements Serializable {
Node[] nodes; Node[] nodes;
CallSite[] callSites; CallSite[] callSites;
FieldAccess[] fieldAccessList; FieldAccess[] fieldAccessList;
ClassAccess[] classAccessList;
static class Node implements Serializable { static class Node implements Serializable {
MethodReference method; MethodReference method;
int[] callSites; int[] callSites;
int[] callerCallSites; int[] callerCallSites;
int[] fieldAccessSites; int[] fieldAccessSites;
int[] classAccessSites;
} }
static class CallSite implements Serializable { static class CallSite implements Serializable {
@ -48,10 +46,4 @@ class SerializableCallGraph implements Serializable {
int callee; int callee;
FieldReference field; FieldReference field;
} }
static class ClassAccess implements Serializable {
TextLocation location;
int callee;
String className;
}
} }