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.hotspot.test;
  24 
  25 import static java.lang.reflect.Modifier.isStatic;
  26 
  27 import org.junit.Assert;
  28 import org.junit.Test;
  29 
  30 import org.graalvm.compiler.core.common.CompilationIdentifier;
  31 import org.graalvm.compiler.core.test.GraalCompilerTest;
  32 import org.graalvm.compiler.nodes.ConstantNode;
  33 import org.graalvm.compiler.nodes.ParameterNode;
  34 import org.graalvm.compiler.nodes.StructuredGraph;
  35 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  36 
  37 import jdk.vm.ci.code.InvalidInstalledCodeException;
  38 import jdk.vm.ci.hotspot.HotSpotInstalledCode;
  39 import jdk.vm.ci.meta.JavaConstant;
  40 import jdk.vm.ci.meta.JavaType;
  41 import jdk.vm.ci.meta.ResolvedJavaMethod;
  42 
  43 public class InstalledCodeExecuteHelperTest extends GraalCompilerTest {
  44 
  45     private static final int ITERATIONS = 100000;
  46     Object[] argsToBind;
  47 
  48     @Test
  49     public void test1() throws InvalidInstalledCodeException {
  50         final ResolvedJavaMethod fooMethod = getResolvedJavaMethod("foo");
  51         final HotSpotInstalledCode fooCode = (HotSpotInstalledCode) getCode(fooMethod);
  52 
  53         argsToBind = new Object[]{fooCode};
  54 
  55         final ResolvedJavaMethod benchmarkMethod = getResolvedJavaMethod("benchmark");
  56         final HotSpotInstalledCode installedBenchmarkCode = (HotSpotInstalledCode) getCode(benchmarkMethod);
  57 
  58         Assert.assertEquals(Integer.valueOf(42), benchmark(fooCode));
  59 
  60         Assert.assertEquals(Integer.valueOf(42), installedBenchmarkCode.executeVarargs(argsToBind[0]));
  61 
  62     }
  63 
  64     public static Integer benchmark(HotSpotInstalledCode code) throws InvalidInstalledCodeException {
  65         int val = 0;
  66         for (int j = 0; j < 100; j++) {
  67             for (int i = 0; i < ITERATIONS; i++) {
  68                 val = (Integer) code.executeVarargs();
  69             }
  70         }
  71         return val;
  72     }
  73 
  74     public static Integer foo() {
  75         return 42;
  76     }
  77 
  78     @Override
  79     protected StructuredGraph parseEager(ResolvedJavaMethod m, AllowAssumptions allowAssumptions, CompilationIdentifier compilationId) {
  80         StructuredGraph graph = super.parseEager(m, allowAssumptions, compilationId);
  81         if (argsToBind != null) {
  82             Object receiver = isStatic(m.getModifiers()) ? null : this;
  83             Object[] args = argsWithReceiver(receiver, argsToBind);
  84             JavaType[] parameterTypes = m.toParameterTypes();
  85             assert parameterTypes.length == args.length;
  86             for (int i = 0; i < argsToBind.length; i++) {
  87                 ParameterNode param = graph.getParameter(i);
  88                 JavaConstant c = getSnippetReflection().forBoxed(parameterTypes[i].getJavaKind(), argsToBind[i]);
  89                 ConstantNode replacement = ConstantNode.forConstant(c, getMetaAccess(), graph);
  90                 param.replaceAtUsages(replacement);
  91             }
  92         }
  93         return graph;
  94     }
  95 }