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 */ 23 package org.graalvm.compiler.hotspot.test; 24 25 import static org.graalvm.compiler.core.GraalCompiler.compileGraph; 26 import static org.graalvm.compiler.core.common.GraalOptions.ImmutableCode; 27 import static org.graalvm.compiler.nodes.ConstantNode.getConstantNodes; 28 29 import org.junit.Assert; 30 import org.junit.Ignore; 31 import org.junit.Test; 32 33 import org.graalvm.compiler.api.test.Graal; 34 import org.graalvm.compiler.code.CompilationResult; 35 import org.graalvm.compiler.core.common.type.Stamp; 36 import org.graalvm.compiler.core.test.GraalCompilerTest; 37 import org.graalvm.compiler.graph.iterators.NodeIterable; 38 import org.graalvm.compiler.hotspot.nodes.type.KlassPointerStamp; 39 import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory; 40 import org.graalvm.compiler.lir.phases.LIRSuites; 41 import org.graalvm.compiler.nodes.ConstantNode; 42 import org.graalvm.compiler.nodes.PiNode; 43 import org.graalvm.compiler.nodes.StructuredGraph; 44 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 45 import org.graalvm.compiler.nodes.memory.FloatingReadNode; 46 import org.graalvm.compiler.nodes.memory.ReadNode; 47 import org.graalvm.compiler.options.OptionValue; 48 import org.graalvm.compiler.options.OptionValue.OverrideScope; 49 import org.graalvm.compiler.phases.OptimisticOptimizations; 50 import org.graalvm.compiler.phases.tiers.Suites; 51 import org.graalvm.compiler.phases.tiers.SuitesProvider; 52 import org.graalvm.compiler.runtime.RuntimeProvider; 53 54 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType; 55 import jdk.vm.ci.meta.JavaConstant; 56 import jdk.vm.ci.meta.JavaKind; 57 import jdk.vm.ci.meta.ResolvedJavaMethod; 58 59 /** 60 * use 61 * 62 * <pre> 63 * mx unittest AheadOfTimeCompilationTest @-XX:CompileCommand='print,*AheadOfTimeCompilationTest.*' 64 * </pre> 65 * 66 * to print disassembly. 67 */ 68 public class AheadOfTimeCompilationTest extends GraalCompilerTest { 69 70 public static final Object STATICFINALOBJECT = new Object(); 71 public static final String STATICFINALSTRING = "test string"; 72 73 public static Object getStaticFinalObject() { 74 return AheadOfTimeCompilationTest.STATICFINALOBJECT; 75 } 76 77 @Test 78 public void testStaticFinalObjectAOT() { 79 StructuredGraph result = compile("getStaticFinalObject", true); 80 assertDeepEquals(1, getConstantNodes(result).count()); 81 Stamp constantStamp = getConstantNodes(result).first().stamp(); 82 Assert.assertTrue(constantStamp.toString(), constantStamp instanceof KlassPointerStamp); 83 assertDeepEquals(2, result.getNodes().filter(FloatingReadNode.class).count()); 84 assertDeepEquals(0, result.getNodes().filter(ReadNode.class).count()); 85 } 86 87 @Test 88 public void testStaticFinalObject() { 89 StructuredGraph result = compile("getStaticFinalObject", false); 90 assertDeepEquals(1, getConstantNodes(result).count()); 91 assertDeepEquals(JavaKind.Object, getConstantNodes(result).first().getStackKind()); 92 assertDeepEquals(0, result.getNodes().filter(FloatingReadNode.class).count()); 93 assertDeepEquals(0, result.getNodes().filter(ReadNode.class).count()); 94 } 95 96 public static Class<AheadOfTimeCompilationTest> getClassObject() { 97 return AheadOfTimeCompilationTest.class; 98 } 99 100 @Test 101 public void testClassObjectAOT() { 102 StructuredGraph result = compile("getClassObject", true); 103 104 NodeIterable<ConstantNode> filter = getConstantNodes(result); 105 assertDeepEquals(1, filter.count()); 106 HotSpotResolvedObjectType type = (HotSpotResolvedObjectType) getMetaAccess().lookupJavaType(AheadOfTimeCompilationTest.class); 107 assertDeepEquals(type.klass(), filter.first().asConstant()); 108 109 assertDeepEquals(1, result.getNodes().filter(FloatingReadNode.class).count()); 110 assertDeepEquals(0, result.getNodes().filter(ReadNode.class).count()); 111 } 112 113 @Test 114 public void testClassObject() { 115 StructuredGraph result = compile("getClassObject", false); 116 117 NodeIterable<ConstantNode> filter = getConstantNodes(result); 118 assertDeepEquals(1, filter.count()); 119 JavaConstant c = filter.first().asJavaConstant(); 120 Assert.assertEquals(getSnippetReflection().asObject(Class.class, c), AheadOfTimeCompilationTest.class); 121 122 assertDeepEquals(0, result.getNodes().filter(FloatingReadNode.class).count()); 123 assertDeepEquals(0, result.getNodes().filter(ReadNode.class).count()); 124 } 125 126 public static Class<Integer> getPrimitiveClassObject() { 127 return int.class; 128 } 129 130 @Test 131 public void testPrimitiveClassObjectAOT() { 132 StructuredGraph result = compile("getPrimitiveClassObject", true); 133 NodeIterable<ConstantNode> filter = getConstantNodes(result); 134 assertDeepEquals(1, filter.count()); 135 Stamp constantStamp = filter.first().stamp(); 136 Assert.assertTrue(constantStamp instanceof KlassPointerStamp); 137 138 assertDeepEquals(2, result.getNodes().filter(FloatingReadNode.class).count()); 139 assertDeepEquals(0, result.getNodes().filter(ReadNode.class).count()); 140 } 141 142 @Test 143 public void testPrimitiveClassObject() { 144 StructuredGraph result = compile("getPrimitiveClassObject", false); 145 NodeIterable<ConstantNode> filter = getConstantNodes(result); 146 assertDeepEquals(1, filter.count()); 147 JavaConstant c = filter.first().asJavaConstant(); 148 Assert.assertEquals(getSnippetReflection().asObject(Class.class, c), Integer.TYPE); 149 150 assertDeepEquals(0, result.getNodes().filter(FloatingReadNode.class).count()); 151 assertDeepEquals(0, result.getNodes().filter(ReadNode.class).count()); 152 } 153 154 public static String getStringObject() { 155 return AheadOfTimeCompilationTest.STATICFINALSTRING; 156 } 157 158 @Test 159 public void testStringObjectAOT() { 160 // embedded strings are fine 161 testStringObjectCommon(true); 162 } 163 164 @Test 165 public void testStringObject() { 166 testStringObjectCommon(false); 167 } 168 169 private void testStringObjectCommon(boolean compileAOT) { 170 StructuredGraph result = compile("getStringObject", compileAOT); 171 172 NodeIterable<ConstantNode> filter = getConstantNodes(result); 173 assertDeepEquals(1, filter.count()); 174 JavaConstant c = filter.first().asJavaConstant(); 175 Assert.assertEquals(getSnippetReflection().asObject(String.class, c), "test string"); 176 177 assertDeepEquals(0, result.getNodes().filter(FloatingReadNode.class).count()); 178 assertDeepEquals(0, result.getNodes().filter(ReadNode.class).count()); 179 } 180 181 public static Boolean getBoxedBoolean() { 182 return Boolean.valueOf(true); 183 } 184 185 @Ignore("ImmutableCode override may not work reliably in non-hosted mode") 186 @Test 187 public void testBoxedBooleanAOT() { 188 StructuredGraph result = compile("getBoxedBoolean", true); 189 190 assertDeepEquals(2, result.getNodes().filter(FloatingReadNode.class).count()); 191 assertDeepEquals(1, result.getNodes(PiNode.TYPE).count()); 192 assertDeepEquals(1, getConstantNodes(result).count()); 193 ConstantNode constant = getConstantNodes(result).first(); 194 assertDeepEquals(JavaKind.Long, constant.getStackKind()); 195 assertDeepEquals(((HotSpotResolvedObjectType) getMetaAccess().lookupJavaType(Boolean.class)).klass(), constant.asConstant()); 196 } 197 198 @Test 199 public void testBoxedBoolean() { 200 StructuredGraph result = compile("getBoxedBoolean", false); 201 assertDeepEquals(0, result.getNodes().filter(FloatingReadNode.class).count()); 202 assertDeepEquals(0, result.getNodes(PiNode.TYPE).count()); 203 assertDeepEquals(1, getConstantNodes(result).count()); 204 ConstantNode constant = getConstantNodes(result).first(); 205 assertDeepEquals(JavaKind.Object, constant.getStackKind()); 206 207 JavaConstant c = constant.asJavaConstant(); 208 Assert.assertEquals(getSnippetReflection().asObject(Boolean.class, c), Boolean.TRUE); 209 } 210 211 @SuppressWarnings("try") 212 private StructuredGraph compile(String test, boolean compileAOT) { 213 try (OverrideScope s = OptionValue.override(ImmutableCode, compileAOT)) { 214 StructuredGraph graph = parseEager(test, AllowAssumptions.YES); 215 ResolvedJavaMethod method = graph.method(); 216 // create suites everytime, as we modify options for the compiler 217 SuitesProvider suitesProvider = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend().getSuites(); 218 final Suites suitesLocal = suitesProvider.getDefaultSuites(); 219 final LIRSuites lirSuitesLocal = suitesProvider.getDefaultLIRSuites(); 220 final CompilationResult compResult = compileGraph(graph, method, getProviders(), getBackend(), getDefaultGraphBuilderSuite(), OptimisticOptimizations.ALL, graph.getProfilingInfo(), 221 suitesLocal, lirSuitesLocal, new CompilationResult(), CompilationResultBuilderFactory.Default); 222 addMethod(method, compResult); 223 return graph; 224 } 225 } 226 } | 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 */ 23 package org.graalvm.compiler.hotspot.test; 24 25 import static org.graalvm.compiler.core.common.GraalOptions.ImmutableCode; 26 import static org.graalvm.compiler.nodes.ConstantNode.getConstantNodes; 27 28 import org.graalvm.compiler.core.common.type.Stamp; 29 import org.graalvm.compiler.core.test.GraalCompilerTest; 30 import org.graalvm.compiler.graph.iterators.NodeIterable; 31 import org.graalvm.compiler.hotspot.nodes.type.KlassPointerStamp; 32 import org.graalvm.compiler.nodes.ConstantNode; 33 import org.graalvm.compiler.nodes.PiNode; 34 import org.graalvm.compiler.nodes.StructuredGraph; 35 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 36 import org.graalvm.compiler.nodes.memory.FloatingReadNode; 37 import org.graalvm.compiler.nodes.memory.ReadNode; 38 import org.graalvm.compiler.options.OptionValues; 39 import org.junit.Assert; 40 import org.junit.Assume; 41 import org.junit.Before; 42 import org.junit.Test; 43 44 import jdk.vm.ci.aarch64.AArch64; 45 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType; 46 import jdk.vm.ci.meta.JavaConstant; 47 import jdk.vm.ci.meta.JavaKind; 48 49 /** 50 * use 51 * 52 * <pre> 53 * mx unittest AheadOfTimeCompilationTest @-XX:CompileCommand='print,*AheadOfTimeCompilationTest.*' 54 * </pre> 55 * 56 * to print disassembly. 57 */ 58 public class AheadOfTimeCompilationTest extends GraalCompilerTest { 59 60 public static final Object STATICFINALOBJECT = new Object(); 61 public static final String STATICFINALSTRING = "test string"; 62 63 public static Object getStaticFinalObject() { 64 return AheadOfTimeCompilationTest.STATICFINALOBJECT; 65 } 66 67 @Before 68 public void setUp() { 69 // Ignore on SPARC 70 Assume.assumeFalse("skipping on AArch64", getTarget().arch instanceof AArch64); 71 } 72 73 @Test 74 public void testStaticFinalObjectAOT() { 75 StructuredGraph result = compile("getStaticFinalObject", true); 76 assertDeepEquals(1, getConstantNodes(result).count()); 77 Stamp constantStamp = getConstantNodes(result).first().stamp(); 78 Assert.assertTrue(constantStamp.toString(), constantStamp instanceof KlassPointerStamp); 79 assertDeepEquals(2, result.getNodes().filter(ReadNode.class).count()); 80 } 81 82 @Test 83 public void testStaticFinalObject() { 84 StructuredGraph result = compile("getStaticFinalObject", false); 85 assertDeepEquals(1, getConstantNodes(result).count()); 86 assertDeepEquals(JavaKind.Object, getConstantNodes(result).first().getStackKind()); 87 assertDeepEquals(0, result.getNodes().filter(ReadNode.class).count()); 88 } 89 90 public static Class<AheadOfTimeCompilationTest> getClassObject() { 91 return AheadOfTimeCompilationTest.class; 92 } 93 94 @Test 95 public void testClassObjectAOT() { 96 StructuredGraph result = compile("getClassObject", true); 97 98 NodeIterable<ConstantNode> filter = getConstantNodes(result); 99 assertDeepEquals(1, filter.count()); 100 HotSpotResolvedObjectType type = (HotSpotResolvedObjectType) getMetaAccess().lookupJavaType(AheadOfTimeCompilationTest.class); 101 assertDeepEquals(type.klass(), filter.first().asConstant()); 102 103 assertDeepEquals(1, result.getNodes().filter(ReadNode.class).count()); 104 } 105 106 @Test 107 public void testClassObject() { 108 StructuredGraph result = compile("getClassObject", false); 109 110 NodeIterable<ConstantNode> filter = getConstantNodes(result); 111 assertDeepEquals(1, filter.count()); 112 JavaConstant c = filter.first().asJavaConstant(); 113 Assert.assertEquals(getSnippetReflection().asObject(Class.class, c), AheadOfTimeCompilationTest.class); 114 115 assertDeepEquals(0, result.getNodes().filter(ReadNode.class).count()); 116 } 117 118 public static Class<Integer> getPrimitiveClassObject() { 119 return int.class; 120 } 121 122 @Test 123 public void testPrimitiveClassObjectAOT() { 124 StructuredGraph result = compile("getPrimitiveClassObject", true); 125 NodeIterable<ConstantNode> filter = getConstantNodes(result); 126 assertDeepEquals(1, filter.count()); 127 Stamp constantStamp = filter.first().stamp(); 128 Assert.assertTrue(constantStamp instanceof KlassPointerStamp); 129 130 assertDeepEquals(2, result.getNodes().filter(ReadNode.class).count()); 131 } 132 133 @Test 134 public void testPrimitiveClassObject() { 135 StructuredGraph result = compile("getPrimitiveClassObject", false); 136 NodeIterable<ConstantNode> filter = getConstantNodes(result); 137 assertDeepEquals(1, filter.count()); 138 JavaConstant c = filter.first().asJavaConstant(); 139 Assert.assertEquals(getSnippetReflection().asObject(Class.class, c), Integer.TYPE); 140 141 assertDeepEquals(0, result.getNodes().filter(ReadNode.class).count()); 142 } 143 144 public static String getStringObject() { 145 return AheadOfTimeCompilationTest.STATICFINALSTRING; 146 } 147 148 @Test 149 public void testStringObjectAOT() { 150 // embedded strings are fine 151 testStringObjectCommon(true); 152 } 153 154 @Test 155 public void testStringObject() { 156 testStringObjectCommon(false); 157 } 158 159 private void testStringObjectCommon(boolean compileAOT) { 160 StructuredGraph result = compile("getStringObject", compileAOT); 161 162 NodeIterable<ConstantNode> filter = getConstantNodes(result); 163 assertDeepEquals(1, filter.count()); 164 JavaConstant c = filter.first().asJavaConstant(); 165 Assert.assertEquals(getSnippetReflection().asObject(String.class, c), "test string"); 166 167 assertDeepEquals(0, result.getNodes().filter(FloatingReadNode.class).count()); 168 assertDeepEquals(0, result.getNodes().filter(ReadNode.class).count()); 169 } 170 171 public static Boolean getBoxedBoolean() { 172 return Boolean.valueOf(true); 173 } 174 175 @Test 176 public void testBoxedBooleanAOT() { 177 StructuredGraph result = compile("getBoxedBoolean", true); 178 179 assertDeepEquals(0, result.getNodes().filter(FloatingReadNode.class).count()); 180 assertDeepEquals(0, result.getNodes(PiNode.TYPE).count()); 181 assertDeepEquals(1, getConstantNodes(result).count()); 182 ConstantNode constant = getConstantNodes(result).first(); 183 assertDeepEquals(JavaKind.Object, constant.getStackKind()); 184 185 JavaConstant c = constant.asJavaConstant(); 186 Assert.assertEquals(getSnippetReflection().asObject(Boolean.class, c), Boolean.TRUE); 187 } 188 189 @Test 190 public void testBoxedBoolean() { 191 StructuredGraph result = compile("getBoxedBoolean", false); 192 assertDeepEquals(0, result.getNodes().filter(FloatingReadNode.class).count()); 193 assertDeepEquals(0, result.getNodes(PiNode.TYPE).count()); 194 assertDeepEquals(1, getConstantNodes(result).count()); 195 ConstantNode constant = getConstantNodes(result).first(); 196 assertDeepEquals(JavaKind.Object, constant.getStackKind()); 197 198 JavaConstant c = constant.asJavaConstant(); 199 Assert.assertEquals(getSnippetReflection().asObject(Boolean.class, c), Boolean.TRUE); 200 } 201 202 @SuppressWarnings("try") 203 private StructuredGraph compile(String test, boolean compileAOT) { 204 OptionValues options = new OptionValues(getInitialOptions(), ImmutableCode, compileAOT); 205 StructuredGraph graph = parseEager(test, AllowAssumptions.YES, options); 206 compile(graph.method(), graph); 207 return graph; 208 } 209 } |