1 /* 2 * Copyright (c) 2016, 2018, 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.replacements.test; 26 27 import org.graalvm.compiler.core.test.GraalCompilerTest; 28 import org.graalvm.compiler.nodes.ConstantNode; 29 import org.graalvm.compiler.nodes.FixedNode; 30 import org.graalvm.compiler.nodes.ReturnNode; 31 import org.graalvm.compiler.nodes.StructuredGraph; 32 import org.graalvm.compiler.nodes.ValueNode; 33 import jdk.vm.ci.meta.JavaConstant; 34 import org.junit.Test; 35 36 import jdk.vm.ci.meta.ResolvedJavaMethod; 37 import static org.hamcrest.CoreMatchers.instanceOf; 38 import org.junit.Assert; 39 40 /** 41 * Tests constant folding of {@link String#hashCode()}. 42 */ 43 public class StringHashConstantTest extends GraalCompilerTest { 44 45 private static final String A_CONSTANT_STRING = "a constant string"; 46 asConstant(StructuredGraph graph, String str)47 private ValueNode asConstant(StructuredGraph graph, String str) { 48 return ConstantNode.forConstant(getSnippetReflection().forObject(str), getMetaAccess(), graph); 49 } 50 51 @Test test1()52 public void test1() { 53 ResolvedJavaMethod method = getResolvedJavaMethod("parameterizedHashCode"); 54 StructuredGraph graph = parseForCompile(method); 55 56 String s = "some string"; 57 int expected = s.hashCode(); 58 graph.getParameter(0).replaceAndDelete(asConstant(graph, s)); 59 60 compile(method, graph); 61 62 FixedNode firstFixed = graph.start().next(); 63 Assert.assertThat(firstFixed, instanceOf(ReturnNode.class)); 64 65 ReturnNode ret = (ReturnNode) firstFixed; 66 JavaConstant result = ret.result().asJavaConstant(); 67 if (result == null) { 68 Assert.fail("result not constant: " + ret.result()); 69 } else { 70 Assert.assertEquals("result", expected, result.asInt()); 71 } 72 } 73 parameterizedHashCode(String value)74 public static int parameterizedHashCode(String value) { 75 return value.hashCode(); 76 } 77 78 @Test test2()79 public void test2() { 80 ResolvedJavaMethod method = getResolvedJavaMethod("constantHashCode"); 81 StructuredGraph graph = parseForCompile(method); 82 83 FixedNode firstFixed = graph.start().next(); 84 Assert.assertThat(firstFixed, instanceOf(ReturnNode.class)); 85 86 ReturnNode ret = (ReturnNode) firstFixed; 87 JavaConstant result = ret.result().asJavaConstant(); 88 if (result == null) { 89 Assert.fail("result not constant: " + ret.result()); 90 } else { 91 int expected = A_CONSTANT_STRING.hashCode(); 92 Assert.assertEquals("result", expected, result.asInt()); 93 } 94 } 95 constantHashCode()96 public static int constantHashCode() { 97 return A_CONSTANT_STRING.hashCode(); 98 } 99 } 100