1 /*
   2  * Copyright (c) 2011, 2019, 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.core.test.deopt;
  26 
  27 import org.graalvm.compiler.api.directives.GraalDirectives;
  28 import org.graalvm.compiler.core.test.GraalCompilerTest;
  29 import org.graalvm.compiler.nodes.ConstantNode;
  30 import org.graalvm.compiler.nodes.StructuredGraph;
  31 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  32 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  33 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
  34 import org.junit.Assert;
  35 import org.junit.Test;
  36 
  37 import jdk.vm.ci.code.InstalledCode;
  38 import jdk.vm.ci.code.InvalidInstalledCodeException;
  39 import jdk.vm.ci.meta.JavaKind;
  40 import jdk.vm.ci.meta.ResolvedJavaMethod;
  41 
  42 public class CompiledMethodTest extends GraalCompilerTest {
  43 
  44     public static Object testMethod(Object arg1, Object arg2, Object arg3) {
  45         String res = arg1 + " " + arg2 + " " + arg3;
  46         return GraalDirectives.inCompiledCode() ? res : "interpreter";
  47     }
  48 
  49     Object f1;
  50 
  51     public Object testMethodVirtual(Object arg1, Object arg2, Object arg3) {
  52         return f1 + " " + arg1 + " " + arg2 + " " + arg3;
  53     }
  54 
  55     /**
  56      * Usages of the constant {@code " "} are replaced with the constant {@code "-"} and it is
  57      * verified that executing the compiled code produces a result that the preserves the node
  58      * replacement unless deoptimization occurs (e.g., due to -Xcomp causing profiles to be
  59      * missing).
  60      */
  61     @Test
  62     public void test1() {
  63         final ResolvedJavaMethod javaMethod = getResolvedJavaMethod("testMethod");
  64         final StructuredGraph graph = parseEager(javaMethod, AllowAssumptions.NO);
  65         new CanonicalizerPhase().apply(graph, getProviders());
  66         new DeadCodeEliminationPhase().apply(graph);
  67 
  68         for (ConstantNode node : ConstantNode.getConstantNodes(graph)) {
  69             if (node.getStackKind() == JavaKind.Object && " ".equals(getSnippetReflection().asObject(String.class, node.asJavaConstant()))) {
  70                 node.replace(graph, ConstantNode.forConstant(getSnippetReflection().forObject("-"), getMetaAccess(), graph));
  71             }
  72         }
  73 
  74         InstalledCode compiledMethod = getCode(javaMethod, graph, true);
  75         try {
  76             Object result = compiledMethod.executeVarargs("1", "2", "3");
  77             if (!"1-2-3".equals(result)) {
  78                 // Deoptimization probably occurred
  79                 Assert.assertEquals("interpreter", result);
  80             }
  81         } catch (InvalidInstalledCodeException t) {
  82             Assert.fail("method invalidated");
  83         }
  84     }
  85 
  86     @Test
  87     public void test3() {
  88         final ResolvedJavaMethod javaMethod = getResolvedJavaMethod("testMethod");
  89         InstalledCode compiledMethod = getCode(javaMethod);
  90         try {
  91             Object result = compiledMethod.executeVarargs("1", "2", "3");
  92             Assert.assertEquals("1 2 3", result);
  93         } catch (InvalidInstalledCodeException t) {
  94             Assert.fail("method invalidated");
  95         }
  96     }
  97 
  98     @Test
  99     public void test4() {
 100         final ResolvedJavaMethod javaMethod = getResolvedJavaMethod("testMethodVirtual");
 101         InstalledCode compiledMethod = getCode(javaMethod);
 102         try {
 103             f1 = "0";
 104             Object result = compiledMethod.executeVarargs(this, "1", "2", "3");
 105             Assert.assertEquals("0 1 2 3", result);
 106         } catch (InvalidInstalledCodeException t) {
 107             Assert.fail("method invalidated");
 108         }
 109     }
 110 }