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