1 /*
   2  * Copyright (c) 2012, 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 java.lang.reflect.InvocationTargetException;
  28 
  29 import org.graalvm.compiler.api.replacements.MethodSubstitution;
  30 import org.graalvm.compiler.core.test.GraalCompilerTest;
  31 import org.graalvm.compiler.debug.DebugContext;
  32 import org.graalvm.compiler.graph.Node;
  33 import org.graalvm.compiler.nodes.Invoke;
  34 import org.graalvm.compiler.nodes.StructuredGraph;
  35 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  36 import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
  37 import org.graalvm.compiler.nodes.spi.LoweringTool;
  38 import org.graalvm.compiler.phases.BasePhase;
  39 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  40 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
  41 import org.graalvm.compiler.phases.common.LoweringPhase;
  42 import org.graalvm.compiler.phases.common.inlining.InliningPhase;
  43 import org.graalvm.compiler.phases.tiers.HighTierContext;
  44 import org.graalvm.compiler.replacements.nodes.MacroNode;
  45 
  46 import jdk.vm.ci.code.InstalledCode;
  47 import jdk.vm.ci.code.InvalidInstalledCodeException;
  48 import jdk.vm.ci.meta.ResolvedJavaMethod;
  49 
  50 /**
  51  * Tests if {@link MethodSubstitution}s are inlined correctly. Most test cases only assert that
  52  * there are no remaining invocations in the graph. This is sufficient if the method that is being
  53  * substituted is a native method. For Java methods, additional checks are necessary.
  54  */
  55 public abstract class MethodSubstitutionTest extends GraalCompilerTest {
  56 
  57     protected StructuredGraph testGraph(final String snippet) {
  58         return testGraph(snippet, null);
  59     }
  60 
  61     @SuppressWarnings("unused")
  62     public BasePhase<HighTierContext> createInliningPhase(StructuredGraph graph) {
  63         return new InliningPhase(new CanonicalizerPhase());
  64     }
  65 
  66     @SuppressWarnings("try")
  67     protected StructuredGraph testGraph(final String snippet, String name) {
  68         DebugContext debug = getDebugContext();
  69         try (DebugContext.Scope s = debug.scope("MethodSubstitutionTest", getResolvedJavaMethod(snippet))) {
  70             StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES, debug);
  71             HighTierContext context = getDefaultHighTierContext();
  72             debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph");
  73             createInliningPhase(graph).apply(graph, context);
  74             debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph");
  75             new CanonicalizerPhase().apply(graph, context);
  76             new DeadCodeEliminationPhase().apply(graph);
  77             // Try to ensure any macro nodes are lowered to expose any resulting invokes
  78             if (graph.getNodes().filter(MacroNode.class).isNotEmpty()) {
  79                 new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
  80             }
  81             if (graph.getNodes().filter(MacroNode.class).isNotEmpty()) {
  82                 new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.MID_TIER).apply(graph, context);
  83             }
  84             assertNotInGraph(graph, MacroNode.class);
  85             if (name != null) {
  86                 for (Node node : graph.getNodes()) {
  87                     if (node instanceof Invoke) {
  88                         Invoke invoke = (Invoke) node;
  89                         if (invoke.callTarget() instanceof MethodCallTargetNode) {
  90                             MethodCallTargetNode call = (MethodCallTargetNode) invoke.callTarget();
  91                             assertTrue(!call.targetMethod().getName().equals(name), "Unexpected invoke of intrinsic %s", call.targetMethod());
  92                         }
  93                     }
  94 
  95                 }
  96             } else {
  97                 assertNotInGraph(graph, Invoke.class);
  98             }
  99             return graph;
 100         } catch (Throwable e) {
 101             throw debug.handle(e);
 102         }
 103     }
 104 
 105     protected static StructuredGraph assertNotInGraph(StructuredGraph graph, Class<?> clazz) {
 106         for (Node node : graph.getNodes()) {
 107             if (clazz.isInstance(node)) {
 108                 fail(node.toString());
 109             }
 110         }
 111         return graph;
 112     }
 113 
 114     protected void testSubstitution(String testMethodName, Class<?> intrinsicClass, Class<?> holder, String methodName, Class<?>[] parameterTypes, boolean optional, Object[] args1, Object[] args2) {
 115         ResolvedJavaMethod realMethod = getResolvedJavaMethod(holder, methodName, parameterTypes);
 116         ResolvedJavaMethod testMethod = getResolvedJavaMethod(testMethodName);
 117         StructuredGraph graph = testGraph(testMethodName);
 118 
 119         // Check to see if the resulting graph contains the expected node
 120         StructuredGraph replacement = getReplacements().getSubstitution(realMethod, -1, false, null);
 121         if (replacement == null && !optional) {
 122             assertInGraph(graph, intrinsicClass);
 123         }
 124 
 125         // Force compilation
 126         InstalledCode code = getCode(testMethod);
 127         assert optional || code != null;
 128 
 129         for (int i = 0; i < args1.length; i++) {
 130             Object arg1 = args1[i];
 131             Object arg2 = args2[i];
 132             Object expected = invokeSafe(realMethod, null, arg1, arg2);
 133             // Verify that the original method and the substitution produce the same value
 134             assertDeepEquals(expected, invokeSafe(testMethod, null, arg1, arg2));
 135             // Verify that the generated code and the original produce the same value
 136             assertDeepEquals(expected, executeVarargsSafe(code, arg1, arg2));
 137         }
 138     }
 139 
 140     protected static StructuredGraph assertInGraph(StructuredGraph graph, Class<?> clazz) {
 141         for (Node node : graph.getNodes()) {
 142             if (clazz.isInstance(node)) {
 143                 return graph;
 144             }
 145         }
 146         fail("Graph does not contain a node of class " + clazz.getName());
 147         return graph;
 148     }
 149 
 150     protected static Object executeVarargsSafe(InstalledCode code, Object... args) {
 151         try {
 152             return code.executeVarargs(args);
 153         } catch (InvalidInstalledCodeException e) {
 154             throw new RuntimeException(e);
 155         }
 156     }
 157 
 158     protected Object invokeSafe(ResolvedJavaMethod method, Object receiver, Object... args) {
 159         try {
 160             return invoke(method, receiver, args);
 161         } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) {
 162             throw new RuntimeException(e);
 163         }
 164     }
 165 
 166 }