1 /* 2 * Copyright (c) 2012, 2015, 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 package org.graalvm.compiler.jtt; 24 25 import static java.lang.reflect.Modifier.isStatic; 26 27 import java.util.Collections; 28 import java.util.Set; 29 30 import jdk.vm.ci.code.InstalledCode; 31 import jdk.vm.ci.meta.DeoptimizationReason; 32 import jdk.vm.ci.meta.JavaConstant; 33 import jdk.vm.ci.meta.JavaType; 34 import jdk.vm.ci.meta.ResolvedJavaMethod; 35 36 import org.junit.Assert; 37 38 import org.graalvm.compiler.core.common.CompilationIdentifier; 39 import org.graalvm.compiler.core.test.GraalCompilerTest; 40 import org.graalvm.compiler.nodes.ConstantNode; 41 import org.graalvm.compiler.nodes.ParameterNode; 42 import org.graalvm.compiler.nodes.StructuredGraph; 43 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 44 45 /** 46 * Base class for the JTT tests. 47 * <p> 48 * These tests are executed twice: once with arguments passed to the execution and once with the 49 * arguments bound to the test's parameters during compilation. The latter is a good test of 50 * canonicalization. 51 */ 52 public class JTTTest extends GraalCompilerTest { 53 54 public static final class DummyTestClass { 55 } 56 57 protected static final Set<DeoptimizationReason> EMPTY = Collections.<DeoptimizationReason> emptySet(); 58 /** 59 * The arguments which, if non-null, will replace the Locals in the test method's graph. 60 */ 61 Object[] argsToBind; 62 63 public JTTTest() { 64 Assert.assertNotNull(getCodeCache()); 65 } 66 67 @Override 68 protected StructuredGraph parseEager(ResolvedJavaMethod m, AllowAssumptions allowAssumptions, CompilationIdentifier compilationId) { 69 StructuredGraph graph = super.parseEager(m, allowAssumptions, compilationId); 70 if (argsToBind != null) { 71 Object receiver = isStatic(m.getModifiers()) ? null : this; 72 Object[] args = argsWithReceiver(receiver, argsToBind); 73 JavaType[] parameterTypes = m.toParameterTypes(); 74 assert parameterTypes.length == args.length; 75 for (ParameterNode param : graph.getNodes(ParameterNode.TYPE)) { 76 JavaConstant c = getSnippetReflection().forBoxed(parameterTypes[param.index()].getJavaKind(), args[param.index()]); 77 ConstantNode replacement = ConstantNode.forConstant(c, getMetaAccess(), graph); 78 param.replaceAtUsages(replacement); 79 } 80 } 81 return graph; 82 } 83 84 @Override 85 protected InstalledCode getCode(ResolvedJavaMethod method, StructuredGraph graph) { 86 return super.getCode(method, graph, argsToBind != null); 87 } 88 89 Double delta; 90 91 @Override 92 protected void assertDeepEquals(Object expected, Object actual) { 93 if (delta != null) { 94 Assert.assertEquals(((Number) expected).doubleValue(), ((Number) actual).doubleValue(), delta); 95 } else { 96 super.assertDeepEquals(expected, actual); 97 } 98 } 99 100 @SuppressWarnings("hiding") 101 protected void runTestWithDelta(double delta, String name, Object... args) { 102 this.delta = Double.valueOf(delta); 103 runTest(name, args); 104 } 105 106 protected void runTest(String name, Object... args) { 107 runTest(EMPTY, name, args); 108 } 109 110 protected void runTest(Set<DeoptimizationReason> shouldNotDeopt, String name, Object... args) { 111 runTest(shouldNotDeopt, true, false, name, args); 112 } 113 114 protected void runTest(Set<DeoptimizationReason> shouldNotDeopt, boolean bind, boolean noProfile, String name, Object... args) { 115 ResolvedJavaMethod method = getResolvedJavaMethod(name); 116 Object receiver = method.isStatic() ? null : this; 117 118 Result expect = executeExpected(method, receiver, args); 119 120 if (noProfile) { 121 method.reprofile(); 122 } 123 124 testAgainstExpected(method, expect, shouldNotDeopt, receiver, args); 125 if (args.length > 0 && bind) { 126 if (noProfile) { 127 method.reprofile(); 128 } 129 130 this.argsToBind = args; 131 testAgainstExpected(method, expect, shouldNotDeopt, receiver, args); 132 this.argsToBind = null; 133 } 134 } 135 }