47667
|
1 |
/*
|
58877
|
2 |
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
|
47667
|
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.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
50858
|
23 |
|
|
24 |
|
47667
|
25 |
package org.graalvm.compiler.core.test.ea;
|
|
26 |
|
47798
|
27 |
import java.lang.reflect.Field;
|
|
28 |
|
47667
|
29 |
import org.graalvm.compiler.core.common.GraalOptions;
|
|
30 |
import org.graalvm.compiler.core.test.GraalCompilerTest;
|
|
31 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
32 |
import org.graalvm.compiler.nodes.extended.RawLoadNode;
|
|
33 |
import org.graalvm.compiler.nodes.extended.RawStoreNode;
|
|
34 |
import org.graalvm.compiler.nodes.virtual.CommitAllocationNode;
|
|
35 |
import org.graalvm.compiler.phases.tiers.HighTierContext;
|
|
36 |
import org.graalvm.compiler.virtual.phases.ea.PartialEscapePhase;
|
|
37 |
import org.junit.Test;
|
47798
|
38 |
|
47667
|
39 |
import sun.misc.Unsafe;
|
|
40 |
|
|
41 |
public class TrufflePEATest extends GraalCompilerTest {
|
|
42 |
|
|
43 |
/**
|
|
44 |
* This class mimics the behavior of {@code FrameWithoutBoxing}.
|
|
45 |
*/
|
|
46 |
static class Frame {
|
|
47 |
long[] primitiveLocals;
|
|
48 |
|
|
49 |
Frame(int size) {
|
|
50 |
primitiveLocals = new long[size];
|
|
51 |
}
|
|
52 |
}
|
|
53 |
|
|
54 |
/**
|
|
55 |
* This class mimics the behavior of {@code DynamicObjectL6I6}.
|
|
56 |
*/
|
|
57 |
static class DynamicObject {
|
|
58 |
int primitiveField0;
|
|
59 |
int primitiveField1;
|
47798
|
60 |
int primitiveField2;
|
47667
|
61 |
}
|
|
62 |
|
|
63 |
private static final long offsetLong1 = Unsafe.ARRAY_LONG_BASE_OFFSET + Unsafe.ARRAY_LONG_INDEX_SCALE * 1;
|
|
64 |
private static final long offsetLong2 = Unsafe.ARRAY_LONG_BASE_OFFSET + Unsafe.ARRAY_LONG_INDEX_SCALE * 2;
|
|
65 |
|
|
66 |
private static final long primitiveField0Offset;
|
|
67 |
|
|
68 |
static {
|
|
69 |
try {
|
|
70 |
Field primitiveField0 = DynamicObject.class.getDeclaredField("primitiveField0");
|
47798
|
71 |
long offset = UNSAFE.objectFieldOffset(primitiveField0);
|
|
72 |
if (offset % 8 == 0) {
|
|
73 |
primitiveField0Offset = offset;
|
|
74 |
} else {
|
|
75 |
Field primitiveField1 = DynamicObject.class.getDeclaredField("primitiveField1");
|
|
76 |
offset = UNSAFE.objectFieldOffset(primitiveField1);
|
|
77 |
assert offset % 8 == 0;
|
|
78 |
primitiveField0Offset = offset;
|
|
79 |
}
|
47667
|
80 |
} catch (NoSuchFieldException | SecurityException e) {
|
|
81 |
throw new AssertionError(e);
|
|
82 |
}
|
|
83 |
}
|
|
84 |
|
|
85 |
public static int unsafeAccessToLongArray(int v, Frame frame) {
|
|
86 |
long[] array = frame.primitiveLocals;
|
|
87 |
int s = UNSAFE.getInt(array, offsetLong1);
|
|
88 |
UNSAFE.putInt(array, offsetLong1, v);
|
|
89 |
UNSAFE.putInt(array, offsetLong2, v);
|
|
90 |
return s + UNSAFE.getInt(array, offsetLong1) + UNSAFE.getInt(array, offsetLong2);
|
|
91 |
}
|
|
92 |
|
|
93 |
@Test
|
|
94 |
public void testUnsafeAccessToLongArray() {
|
|
95 |
StructuredGraph graph = processMethod("unsafeAccessToLongArray");
|
|
96 |
assertDeepEquals(1, graph.getNodes().filter(RawLoadNode.class).count());
|
|
97 |
}
|
|
98 |
|
|
99 |
/**
|
|
100 |
* The following value should be less than the default value of
|
|
101 |
* {@link GraalOptions#MaximumEscapeAnalysisArrayLength}.
|
|
102 |
*/
|
|
103 |
private static final int FRAME_SIZE = 16;
|
|
104 |
|
|
105 |
public static long newFrame(long v) {
|
|
106 |
Frame frame = new Frame(FRAME_SIZE);
|
|
107 |
// Testing unsafe accesses with other kinds requires special handling of the initialized
|
|
108 |
// entry kind.
|
|
109 |
UNSAFE.putLong(frame.primitiveLocals, offsetLong1, v);
|
|
110 |
return UNSAFE.getLong(frame.primitiveLocals, offsetLong1);
|
|
111 |
}
|
|
112 |
|
|
113 |
@Test
|
|
114 |
public void testNewFrame() {
|
|
115 |
StructuredGraph graph = processMethod("newFrame");
|
|
116 |
assertDeepEquals(0, graph.getNodes().filter(CommitAllocationNode.class).count());
|
|
117 |
assertDeepEquals(0, graph.getNodes().filter(RawLoadNode.class).count());
|
|
118 |
assertDeepEquals(0, graph.getNodes().filter(RawStoreNode.class).count());
|
|
119 |
}
|
|
120 |
|
|
121 |
protected StructuredGraph processMethod(final String snippet) {
|
|
122 |
StructuredGraph graph = parseEager(snippet, StructuredGraph.AllowAssumptions.NO);
|
|
123 |
HighTierContext context = getDefaultHighTierContext();
|
52910
|
124 |
createInliningPhase().apply(graph, context);
|
58877
|
125 |
new PartialEscapePhase(true, true, createCanonicalizerPhase(), null, graph.getOptions()).apply(graph, context);
|
47667
|
126 |
return graph;
|
|
127 |
}
|
|
128 |
|
|
129 |
public static double accessDynamicObject(double v) {
|
|
130 |
DynamicObject obj = new DynamicObject();
|
|
131 |
UNSAFE.putDouble(obj, primitiveField0Offset, v);
|
|
132 |
return UNSAFE.getDouble(obj, primitiveField0Offset);
|
|
133 |
}
|
|
134 |
|
|
135 |
@Test
|
|
136 |
public void testAccessDynamicObject() {
|
|
137 |
StructuredGraph graph = processMethod("accessDynamicObject");
|
|
138 |
assertDeepEquals(0, graph.getNodes().filter(CommitAllocationNode.class).count());
|
|
139 |
assertDeepEquals(0, graph.getNodes().filter(RawLoadNode.class).count());
|
|
140 |
assertDeepEquals(0, graph.getNodes().filter(RawStoreNode.class).count());
|
|
141 |
}
|
|
142 |
|
|
143 |
}
|