1 /* 2 * Copyright (c) 2019, 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. 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 24 25 package org.graalvm.compiler.hotspot.test; 26 27 import org.graalvm.compiler.core.test.GraalCompilerTest; 28 import org.graalvm.compiler.java.BytecodeParserOptions; 29 import org.graalvm.compiler.nodes.StructuredGraph; 30 import org.graalvm.compiler.nodes.extended.ForeignCallNode; 31 import org.graalvm.compiler.nodes.java.MethodCallTargetNode; 32 import org.graalvm.compiler.nodes.memory.ReadNode; 33 import org.graalvm.compiler.options.OptionValues; 34 import org.junit.Test; 35 36 import jdk.vm.ci.meta.JavaTypeProfile; 37 import jdk.vm.ci.meta.JavaTypeProfile.ProfiledType; 38 import jdk.vm.ci.meta.MetaAccessProvider; 39 import jdk.vm.ci.meta.ResolvedJavaMethod; 40 import jdk.vm.ci.meta.TriState; 41 42 public class ObjectHashCodeInliningTest extends GraalCompilerTest { 43 44 public static int getHash(Object obj) { 45 return obj.hashCode(); 46 } 47 48 @Test 49 public void testGetHash() { 50 MetaAccessProvider metaAccess = getMetaAccess(); 51 ProfiledType[] injectedProfile = { 52 new ProfiledType(metaAccess.lookupJavaType(String.class), 0.9D), 53 new ProfiledType(metaAccess.lookupJavaType(Object.class), 0.1D)}; 54 55 ResolvedJavaMethod method = getResolvedJavaMethod("getHash"); 56 StructuredGraph graph = parseForCompile(method, 57 new OptionValues(getInitialOptions(), BytecodeParserOptions.InlineDuringParsing, false, BytecodeParserOptions.InlineIntrinsicsDuringParsing, false)); 58 for (MethodCallTargetNode callTargetNode : graph.getNodes(MethodCallTargetNode.TYPE)) { 59 if ("Object.hashCode".equals(callTargetNode.targetName())) { 60 callTargetNode.setJavaTypeProfile(new JavaTypeProfile(TriState.FALSE, 0.0D, injectedProfile)); 61 } 62 } 63 64 compile(method, graph); 65 } 66 67 private static boolean containsForeignCallToIdentityHashCode(StructuredGraph graph) { 68 for (ForeignCallNode foreignCallNode : graph.getNodes().filter(ForeignCallNode.class)) { 69 if ("identity_hashcode".equals(foreignCallNode.getDescriptor().getName())) { 70 return true; 71 } 72 } 73 return false; 74 } 75 76 private static boolean containsReadStringHash(StructuredGraph graph) { 77 for (ReadNode readNode : graph.getNodes().filter(ReadNode.class)) { 78 if ("String.hash".equals(readNode.getLocationIdentity().toString())) { 79 return true; 80 } 81 } 82 return false; 83 } 84 85 @Override 86 protected void checkHighTierGraph(StructuredGraph graph) { 87 assert containsForeignCallToIdentityHashCode(graph) : "expected a foreign call to identity_hashcode"; 88 assert containsReadStringHash(graph) : "expected a read from String.hash"; 89 } 90 91 }