001/* 002 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package com.oracle.graal.replacements.test; 024 025import java.lang.reflect.*; 026 027import jdk.internal.jvmci.code.*; 028import com.oracle.graal.debug.*; 029import com.oracle.graal.debug.Debug.*; 030import jdk.internal.jvmci.meta.*; 031 032import com.oracle.graal.api.replacements.*; 033import com.oracle.graal.compiler.test.*; 034import com.oracle.graal.graph.*; 035import com.oracle.graal.nodes.*; 036import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions; 037import com.oracle.graal.phases.common.*; 038import com.oracle.graal.phases.common.inlining.*; 039import com.oracle.graal.phases.tiers.*; 040 041/** 042 * Tests if {@link MethodSubstitution}s are inlined correctly. Most test cases only assert that 043 * there are no remaining invocations in the graph. This is sufficient if the method that is being 044 * substituted is a native method. For Java methods, additional checks are necessary. 045 */ 046public abstract class MethodSubstitutionTest extends GraalCompilerTest { 047 048 protected StructuredGraph testGraph(final String snippet) { 049 try (Scope s = Debug.scope("MethodSubstitutionTest", getResolvedJavaMethod(snippet))) { 050 StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES); 051 HighTierContext context = getDefaultHighTierContext(); 052 Debug.dump(graph, "Graph"); 053 new InliningPhase(new CanonicalizerPhase()).apply(graph, context); 054 Debug.dump(graph, "Graph"); 055 new CanonicalizerPhase().apply(graph, context); 056 new DeadCodeEliminationPhase().apply(graph); 057 058 assertNotInGraph(graph, Invoke.class); 059 return graph; 060 } catch (Throwable e) { 061 throw Debug.handle(e); 062 } 063 } 064 065 protected static StructuredGraph assertNotInGraph(StructuredGraph graph, Class<?> clazz) { 066 for (Node node : graph.getNodes()) { 067 if (clazz.isInstance(node)) { 068 fail(node.toString()); 069 } 070 } 071 return graph; 072 } 073 074 protected void testSubstitution(String testMethodName, Class<?> intrinsicClass, Class<?> holder, String methodName, Class<?>[] parameterTypes, boolean optional, Object[] args1, Object[] args2) { 075 ResolvedJavaMethod realMethod = getResolvedJavaMethod(holder, methodName, parameterTypes); 076 ResolvedJavaMethod testMethod = getResolvedJavaMethod(testMethodName); 077 StructuredGraph graph = testGraph(testMethodName); 078 079 // Check to see if the resulting graph contains the expected node 080 StructuredGraph replacement = getReplacements().getSubstitution(realMethod, -1); 081 if (replacement == null && !optional) { 082 assertInGraph(graph, intrinsicClass); 083 } 084 085 // Force compilation 086 InstalledCode code = getCode(testMethod); 087 assert optional || code != null; 088 089 for (int i = 0; i < args1.length; i++) { 090 Object arg1 = args1[i]; 091 Object arg2 = args2[i]; 092 Object expected = invokeSafe(realMethod, null, arg1, arg2); 093 // Verify that the original method and the substitution produce the same value 094 assertDeepEquals(expected, invokeSafe(testMethod, null, arg1, arg2)); 095 // Verify that the generated code and the original produce the same value 096 assertDeepEquals(expected, executeVarargsSafe(code, arg1, arg2)); 097 } 098 } 099 100 protected static StructuredGraph assertInGraph(StructuredGraph graph, Class<?> clazz) { 101 for (Node node : graph.getNodes()) { 102 if (clazz.isInstance(node)) { 103 return graph; 104 } 105 } 106 fail("Graph does not contain a node of class " + clazz.getName()); 107 return graph; 108 } 109 110 protected static Object executeVarargsSafe(InstalledCode code, Object... args) { 111 try { 112 return code.executeVarargs(args); 113 } catch (InvalidInstalledCodeException e) { 114 throw new RuntimeException(e); 115 } 116 } 117 118 protected Object invokeSafe(ResolvedJavaMethod method, Object receiver, Object... args) { 119 try { 120 return invoke(method, receiver, args); 121 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { 122 throw new RuntimeException(e); 123 } 124 } 125 126}