Skip to content

Commit 8ae5773

Browse files
author
Christian Wimmer
committed
[GR-50426] Store signature of AnalysisMethod as resolved types.
PullRequest: graal/16296
2 parents 8d7d2c8 + ed9c3ae commit 8ae5773

File tree

11 files changed

+258
-136
lines changed

11 files changed

+258
-136
lines changed

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/PointsToAnalysis.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
import com.oracle.graal.pointsto.flow.OffsetLoadTypeFlow.AbstractUnsafeLoadTypeFlow;
5757
import com.oracle.graal.pointsto.flow.OffsetStoreTypeFlow.AbstractUnsafeStoreTypeFlow;
5858
import com.oracle.graal.pointsto.flow.TypeFlow;
59-
import com.oracle.graal.pointsto.infrastructure.WrappedSignature;
6059
import com.oracle.graal.pointsto.meta.AnalysisField;
6160
import com.oracle.graal.pointsto.meta.AnalysisMetaAccess;
6261
import com.oracle.graal.pointsto.meta.AnalysisMethod;
@@ -88,6 +87,7 @@
8887
import jdk.vm.ci.meta.JavaKind;
8988
import jdk.vm.ci.meta.JavaType;
9089
import jdk.vm.ci.meta.ResolvedJavaField;
90+
import jdk.vm.ci.meta.Signature;
9191

9292
public abstract class PointsToAnalysis extends AbstractAnalysisEngine {
9393
/** The type of {@link java.lang.Object}. */
@@ -323,7 +323,7 @@ public AnalysisMethod addRootMethod(AnalysisMethod aMethod, boolean invokeSpecia
323323
AnalysisError.guarantee(aMethod.isOriginalMethod());
324324
AnalysisType declaringClass = aMethod.getDeclaringClass();
325325
boolean isStatic = aMethod.isStatic();
326-
WrappedSignature signature = aMethod.getSignature();
326+
Signature signature = aMethod.getSignature();
327327
int paramCount = signature.getParameterCount(!isStatic);
328328
PointsToAnalysisMethod originalPTAMethod = assertPointsToAnalysisMethod(aMethod);
329329

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.graal.pointsto.infrastructure;
26+
27+
import java.lang.invoke.MethodType;
28+
import java.util.ArrayList;
29+
import java.util.Arrays;
30+
import java.util.Collections;
31+
import java.util.List;
32+
import java.util.Objects;
33+
import java.util.stream.Collectors;
34+
35+
import jdk.vm.ci.meta.JavaKind;
36+
import jdk.vm.ci.meta.MetaAccessProvider;
37+
import jdk.vm.ci.meta.ResolvedJavaType;
38+
import jdk.vm.ci.meta.Signature;
39+
40+
/**
41+
* A straightforward implementation of {@link Signature} where all parameter types and the return
42+
* type are {@link ResolvedJavaType}. The generic type allows to further improve type safety of
43+
* usages.
44+
*
45+
* In a regular {@link Signature}, looking up a
46+
* {@link Signature#getParameterType(int, ResolvedJavaType) parameter type} or the
47+
* {@link Signature#getReturnType(ResolvedJavaType) return type} requires to provide the "accessing
48+
* class" for type resolution. Since in this implementation all types are always pre-resolved, these
49+
* parameters are ignored. In addition, methods are offered that allow parameter type and return
50+
* type lookup without providing the accessing class.
51+
*/
52+
public final class ResolvedSignature<T extends ResolvedJavaType> implements Signature {
53+
54+
public static <T extends ResolvedJavaType> ResolvedSignature<T> fromArray(T[] parameterTypes, T returnType) {
55+
return new ResolvedSignature<>(List.of(parameterTypes), returnType);
56+
}
57+
58+
public static <T extends ResolvedJavaType> ResolvedSignature<T> fromList(List<T> parameterTypes, T returnType) {
59+
return new ResolvedSignature<>(List.copyOf(parameterTypes), returnType);
60+
}
61+
62+
public static ResolvedSignature<ResolvedJavaType> fromKinds(JavaKind[] parameterKinds, JavaKind returnKind, MetaAccessProvider metaAccess) {
63+
return new ResolvedSignature<>(
64+
Arrays.stream(parameterKinds).map(kind -> resolveType(kind, metaAccess)).collect(Collectors.toUnmodifiableList()),
65+
resolveType(returnKind, metaAccess));
66+
}
67+
68+
private static ResolvedJavaType resolveType(JavaKind kind, MetaAccessProvider metaAccess) {
69+
return metaAccess.lookupJavaType(kind.isObject() ? Object.class : kind.toJavaClass());
70+
}
71+
72+
public static ResolvedSignature<ResolvedJavaType> fromMethodType(MethodType mt, MetaAccessProvider metaAccess) {
73+
return new ResolvedSignature<>(
74+
Arrays.stream(mt.parameterArray()).map(metaAccess::lookupJavaType).collect(Collectors.toUnmodifiableList()),
75+
metaAccess.lookupJavaType(mt.returnType()));
76+
}
77+
78+
private final List<T> parameterTypes;
79+
private final T returnType;
80+
81+
private ResolvedSignature(List<T> parameterTypes, T returnType) {
82+
/*
83+
* All factory methods must pass in an immutable list for the parameter types, so that the
84+
* list can be safely passes out again as the parameter list. Unfortunately, there is no way
85+
* to assert that here.
86+
*/
87+
this.parameterTypes = parameterTypes;
88+
this.returnType = returnType;
89+
}
90+
91+
@Override
92+
public int getParameterCount(boolean withReceiver) {
93+
return parameterTypes.size() + (withReceiver ? 1 : 0);
94+
}
95+
96+
@Override
97+
public T getParameterType(int index, ResolvedJavaType accessingClass) {
98+
return getParameterType(index);
99+
}
100+
101+
public T getParameterType(int index) {
102+
return parameterTypes.get(index);
103+
}
104+
105+
@Override
106+
public T getReturnType(ResolvedJavaType accessingClass) {
107+
return getReturnType();
108+
}
109+
110+
public T getReturnType() {
111+
return returnType;
112+
}
113+
114+
/**
115+
* A type-safe version of {@link Signature#toParameterTypes}.
116+
*
117+
* @return An unmodifiable list with all parameter types, including the receiverType if it is
118+
* non-null.
119+
*/
120+
public List<T> toParameterList(T receiverType) {
121+
if (receiverType == null) {
122+
/* parameterTypes is always an unmodifiable list, so we can return it directly. */
123+
return parameterTypes;
124+
}
125+
List<T> withReceiver = new ArrayList<>(parameterTypes.size() + 1);
126+
withReceiver.add(receiverType);
127+
withReceiver.addAll(parameterTypes);
128+
return Collections.unmodifiableList(withReceiver);
129+
}
130+
131+
@Override
132+
public boolean equals(Object obj) {
133+
return this == obj || (obj instanceof ResolvedSignature<?> other && Objects.equals(parameterTypes, other.parameterTypes) && Objects.equals(returnType, other.returnType));
134+
}
135+
136+
@Override
137+
public int hashCode() {
138+
return Objects.hash(parameterTypes, returnType);
139+
}
140+
141+
@Override
142+
public String toString() {
143+
return toMethodDescriptor();
144+
}
145+
}

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/infrastructure/Universe.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@
2424
*/
2525
package com.oracle.graal.pointsto.infrastructure;
2626

27-
import jdk.graal.compiler.api.replacements.SnippetReflectionProvider;
28-
2927
import com.oracle.graal.pointsto.api.HostVM;
3028
import com.oracle.graal.pointsto.heap.ImageHeap;
3129
import com.oracle.graal.pointsto.heap.ImageHeapConstant;
3230

31+
import jdk.graal.compiler.api.replacements.SnippetReflectionProvider;
3332
import jdk.vm.ci.meta.ConstantPool;
3433
import jdk.vm.ci.meta.JavaConstant;
3534
import jdk.vm.ci.meta.JavaField;
@@ -58,7 +57,7 @@ public interface Universe {
5857

5958
JavaMethod lookupAllowUnresolved(JavaMethod method);
6059

61-
WrappedSignature lookup(Signature signature, ResolvedJavaType defaultAccessingClass);
60+
ResolvedSignature<?> lookup(Signature signature, ResolvedJavaType defaultAccessingClass);
6261

6362
WrappedConstantPool lookup(ConstantPool constantPool, ResolvedJavaType defaultAccessingClass);
6463

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/infrastructure/WrappedConstantPool.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@
3131
import java.util.List;
3232
import java.util.stream.Collectors;
3333

34-
import jdk.graal.compiler.core.common.BootstrapMethodIntrospection;
35-
import jdk.graal.compiler.debug.GraalError;
36-
import jdk.graal.compiler.serviceprovider.GraalServices;
37-
3834
import com.oracle.graal.pointsto.constraints.UnresolvedElementException;
3935
import com.oracle.svm.util.ReflectionUtil;
4036

37+
import jdk.graal.compiler.core.common.BootstrapMethodIntrospection;
38+
import jdk.graal.compiler.debug.GraalError;
39+
import jdk.graal.compiler.serviceprovider.GraalServices;
4140
import jdk.vm.ci.meta.ConstantPool;
4241
import jdk.vm.ci.meta.JavaConstant;
4342
import jdk.vm.ci.meta.JavaField;
@@ -153,7 +152,7 @@ public JavaType lookupType(int cpi, int opcode) {
153152
}
154153

155154
@Override
156-
public WrappedSignature lookupSignature(int cpi) {
155+
public ResolvedSignature<?> lookupSignature(int cpi) {
157156
return universe.lookup(wrapped.lookupSignature(cpi), defaultAccessingClass);
158157
}
159158

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/infrastructure/WrappedSignature.java

Lines changed: 0 additions & 91 deletions
This file was deleted.

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/meta/AnalysisMethod.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
import com.oracle.graal.pointsto.flow.AnalysisParsedGraph;
5454
import com.oracle.graal.pointsto.infrastructure.GraphProvider;
5555
import com.oracle.graal.pointsto.infrastructure.OriginalMethodProvider;
56+
import com.oracle.graal.pointsto.infrastructure.ResolvedSignature;
5657
import com.oracle.graal.pointsto.infrastructure.WrappedJavaMethod;
57-
import com.oracle.graal.pointsto.infrastructure.WrappedSignature;
5858
import com.oracle.graal.pointsto.reports.ReportUtils;
5959
import com.oracle.graal.pointsto.util.AnalysisError;
6060
import com.oracle.graal.pointsto.util.AtomicUtils;
@@ -119,6 +119,7 @@ public record Signature(String name, AnalysisType[] parameterTypes) {
119119
private final String qualifiedName;
120120

121121
protected final AnalysisType declaringClass;
122+
protected final ResolvedSignature<AnalysisType> signature;
122123
private final int parsingContextMaxDepth;
123124

124125
private final MultiMethodKey multiMethodKey;
@@ -181,6 +182,7 @@ protected AnalysisMethod(AnalysisUniverse universe, ResolvedJavaMethod wrapped,
181182
id = universe.nextMethodId.getAndIncrement();
182183

183184
declaringClass = universe.lookup(wrapped.getDeclaringClass());
185+
signature = getUniverse().lookup(wrapped.getSignature(), wrapped.getDeclaringClass());
184186
hasNeverInlineDirective = universe.hostVM().hasNeverInlineDirective(wrapped);
185187

186188
name = createName(wrapped, multiMethodKey);
@@ -228,6 +230,7 @@ protected AnalysisMethod(AnalysisMethod original, MultiMethodKey multiMethodKey)
228230
wrapped = original.wrapped;
229231
id = original.id;
230232
declaringClass = original.declaringClass;
233+
signature = original.signature;
231234
hasNeverInlineDirective = original.hasNeverInlineDirective;
232235
exceptionHandlers = original.exceptionHandlers;
233236
localVariableTable = original.localVariableTable;
@@ -627,8 +630,8 @@ public String getName() {
627630
}
628631

629632
@Override
630-
public WrappedSignature getSignature() {
631-
return getUniverse().lookup(wrapped.getSignature(), wrapped.getDeclaringClass());
633+
public jdk.vm.ci.meta.Signature getSignature() {
634+
return signature;
632635
}
633636

634637
@Override

0 commit comments

Comments
 (0)