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 jdk.vm.ci.meta.ResolvedJavaMethod;
  28 import org.graalvm.compiler.core.test.GraalCompilerTest;
  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.junit.Assume;
  34 import org.junit.Test;
  35 
  36 public class ObjectHashCodeInliningTest extends GraalCompilerTest {
  37 
  38     public static int getHash(Object obj) {
  39         return obj.hashCode();
  40     }
  41 
  42     @Test
  43     public void testInstallCodeInvalidation() {
  44         for (int i = 0; i < 100000; i++) {
  45             getHash(i % 10 == 0 ? new Object() : "");
  46         }
  47 
  48         ResolvedJavaMethod method = getResolvedJavaMethod("getHash");
  49         StructuredGraph graph = parseForCompile(method);
  50         for (MethodCallTargetNode callTargetNode : graph.getNodes(MethodCallTargetNode.TYPE)) {
  51             if ("Object.hashCode".equals(callTargetNode.targetName())) {
  52                 Assume.assumeTrue(callTargetNode.getProfile() != null);
  53             }
  54         }
  55         compile(method, graph);
  56     }
  57 
  58     private static boolean containsForeignCallToIdentityHashCode(StructuredGraph graph) {
  59         for (ForeignCallNode foreignCallNode : graph.getNodes().filter(ForeignCallNode.class)) {
  60             if ("identity_hashcode".equals(foreignCallNode.getDescriptor().getName())) {
  61                 return true;
  62             }
  63         }
  64         return false;
  65     }
  66 
  67     private static boolean containsReadStringHash(StructuredGraph graph) {
  68         for (ReadNode readNode : graph.getNodes().filter(ReadNode.class)) {
  69             if ("String.hash".equals(readNode.getLocationIdentity().toString())) {
  70                 return true;
  71             }
  72         }
  73         return false;
  74     }
  75 
  76     @Override
  77     protected void checkHighTierGraph(StructuredGraph graph) {
  78         assert containsForeignCallToIdentityHashCode(graph) : "expected a foreign call to identity_hashcode";
  79         assert containsReadStringHash(graph) : "expected a read from String.hash";
  80     }
  81 
  82 }