EdgesTest.java revision 13264:48566d838608
1/*
2 * Copyright (c) 2013, 2014, 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.replacements.test;
24
25import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0;
26import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0;
27
28import java.lang.reflect.Method;
29
30import org.junit.Assert;
31import org.junit.Test;
32
33import org.graalvm.compiler.core.test.GraalCompilerTest;
34import org.graalvm.compiler.graph.Edges;
35import org.graalvm.compiler.graph.Node;
36import org.graalvm.compiler.graph.NodeClass;
37import org.graalvm.compiler.graph.NodeInputList;
38import org.graalvm.compiler.nodeinfo.NodeInfo;
39import org.graalvm.compiler.nodes.ConstantNode;
40import org.graalvm.compiler.nodes.StructuredGraph;
41import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
42import org.graalvm.compiler.nodes.ValueNode;
43import org.graalvm.compiler.nodes.calc.FloatingNode;
44import org.graalvm.compiler.nodes.java.InstanceOfNode;
45import org.graalvm.compiler.options.OptionValues;
46import org.graalvm.compiler.phases.common.CanonicalizerPhase;
47import org.graalvm.compiler.phases.common.inlining.InliningPhase;
48import org.graalvm.compiler.phases.common.inlining.policy.InlineMethodSubstitutionsPolicy;
49import org.graalvm.compiler.phases.tiers.HighTierContext;
50
51import jdk.vm.ci.meta.ResolvedJavaMethod;
52
53public class EdgesTest extends GraalCompilerTest {
54
55    @NodeInfo(cycles = CYCLES_0, size = SIZE_0)
56    static final class TestNode extends Node {
57        public static final NodeClass<TestNode> TYPE = NodeClass.create(TestNode.class);
58        @Input NodeInputList<ValueNode> itail;
59        @Input ConstantNode i1;
60        @Input FloatingNode i2;
61
62        protected TestNode() {
63            super(TYPE);
64        }
65
66    }
67
68    protected StructuredGraph createGraph() {
69        OptionValues options = getInitialOptions();
70        return new StructuredGraph.Builder(options, getDebugContext(options)).build();
71    }
72
73    StructuredGraph graph = createGraph();
74    TestNode node;
75    ConstantNode i1;
76    ConstantNode i2;
77    ConstantNode i3;
78    ConstantNode i4;
79    Edges inputs;
80
81    public EdgesTest() {
82        node = new TestNode();
83        i1 = ConstantNode.forInt(1, graph);
84        i2 = ConstantNode.forDouble(1.0d, graph);
85        i3 = ConstantNode.forInt(4, graph);
86        i4 = ConstantNode.forInt(14, graph);
87        node.itail = new NodeInputList<>(node, new ValueNode[]{i3, i4});
88        node.i1 = i1;
89        node.i2 = i2;
90        graph.add(node);
91        inputs = node.getNodeClass().getInputEdges();
92    }
93
94    /**
95     * Checks that there are no checkcasts in the compiled version of
96     * {@link Edges#getNode(Node, long[], int)}.
97     */
98    @Test
99    public void test0() {
100        testMethod(getMethod("getNode", Node.class, long[].class, int.class), null, node, inputs.getOffsets(), 0);
101    }
102
103    /**
104     * Checks that there are no checkcasts in the compiled version of
105     * {@link Edges#getNodeList(Node, long[], int)}.
106     */
107    @Test
108    public void test1() {
109        testMethod(getMethod("getNodeList", Node.class, long[].class, int.class), null, node, inputs.getOffsets(), 2);
110    }
111
112    /**
113     * Checks that there are no checkcasts in the compiled version of
114     * {@link Edges#setNode(Node, int, Node)}.
115     */
116    @Test
117    public void test2() {
118        testMethod(getMethod("setNode", Node.class, int.class, Node.class), inputs, node, 1, i2);
119    }
120
121    private void testMethod(Method method, Object receiver, Object... args) {
122        try {
123            // Invoke the method to ensure it has a type profile
124            for (int i = 0; i < 5000; i++) {
125                method.invoke(receiver, args);
126            }
127        } catch (Exception e) {
128            throw new RuntimeException(e);
129        }
130
131        ResolvedJavaMethod javaMethod = getMetaAccess().lookupJavaMethod(method);
132        StructuredGraph g = parseProfiled(javaMethod, AllowAssumptions.NO);
133        HighTierContext context = getDefaultHighTierContext();
134        new InliningPhase(new InlineMethodSubstitutionsPolicy(), new CanonicalizerPhase()).apply(g, context);
135        new CanonicalizerPhase().apply(g, context);
136        Assert.assertTrue(g.getNodes().filter(InstanceOfNode.class).isEmpty());
137    }
138
139    private static Method getMethod(final String name, Class<?>... parameters) {
140        try {
141            return Edges.class.getDeclaredMethod(name, parameters);
142        } catch (NoSuchMethodException | SecurityException e) {
143            throw new RuntimeException(e);
144        }
145    }
146}
147