InstalledCodeExecuteHelperTest.java revision 13264:48566d838608
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 */
23package org.graalvm.compiler.hotspot.test;
24
25import static java.lang.reflect.Modifier.isStatic;
26
27import org.graalvm.compiler.core.test.GraalCompilerTest;
28import org.graalvm.compiler.nodes.ConstantNode;
29import org.graalvm.compiler.nodes.ParameterNode;
30import org.graalvm.compiler.nodes.StructuredGraph;
31import org.graalvm.compiler.nodes.StructuredGraph.Builder;
32import org.graalvm.compiler.phases.PhaseSuite;
33import org.graalvm.compiler.phases.tiers.HighTierContext;
34import org.junit.Assert;
35import org.junit.Test;
36
37import jdk.vm.ci.code.InvalidInstalledCodeException;
38import jdk.vm.ci.hotspot.HotSpotInstalledCode;
39import jdk.vm.ci.meta.JavaConstant;
40import jdk.vm.ci.meta.JavaType;
41import jdk.vm.ci.meta.ResolvedJavaMethod;
42
43public 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 parse(Builder builder, PhaseSuite<HighTierContext> graphBuilderSuite) {
80        StructuredGraph graph = super.parse(builder, graphBuilderSuite);
81        if (argsToBind != null) {
82            ResolvedJavaMethod m = graph.method();
83            Object receiver = isStatic(m.getModifiers()) ? null : this;
84            Object[] args = argsWithReceiver(receiver, argsToBind);
85            JavaType[] parameterTypes = m.toParameterTypes();
86            assert parameterTypes.length == args.length;
87            for (int i = 0; i < argsToBind.length; i++) {
88                ParameterNode param = graph.getParameter(i);
89                JavaConstant c = getSnippetReflection().forBoxed(parameterTypes[i].getJavaKind(), argsToBind[i]);
90                ConstantNode replacement = ConstantNode.forConstant(c, getMetaAccess(), graph);
91                param.replaceAtUsages(replacement);
92            }
93        }
94        return graph;
95    }
96}
97