001/*
002 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.replacements.test;
024
025import java.lang.reflect.*;
026
027import jdk.internal.jvmci.meta.*;
028
029import org.junit.*;
030
031import com.oracle.graal.compiler.test.*;
032import com.oracle.graal.graph.*;
033import com.oracle.graal.nodeinfo.*;
034import com.oracle.graal.nodes.*;
035import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
036import com.oracle.graal.nodes.calc.*;
037import com.oracle.graal.nodes.java.*;
038import com.oracle.graal.phases.common.*;
039import com.oracle.graal.phases.common.inlining.*;
040import com.oracle.graal.phases.common.inlining.policy.*;
041import com.oracle.graal.phases.tiers.*;
042
043public class EdgesTest extends GraalCompilerTest {
044
045    @NodeInfo
046    static final class TestNode extends Node {
047        public static final NodeClass<TestNode> TYPE = NodeClass.create(TestNode.class);
048        @Input NodeInputList<ValueNode> itail;
049        @Input ConstantNode i1;
050        @Input FloatingNode i2;
051
052        public TestNode() {
053            super(TYPE);
054        }
055
056    }
057
058    StructuredGraph graph = new StructuredGraph(AllowAssumptions.NO);
059    TestNode node;
060    ConstantNode i1;
061    ConstantNode i2;
062    ConstantNode i3;
063    ConstantNode i4;
064    Edges inputs;
065
066    public EdgesTest() {
067        node = new TestNode();
068        i1 = ConstantNode.forInt(1, graph);
069        i2 = ConstantNode.forDouble(1.0d, graph);
070        i3 = ConstantNode.forInt(4, graph);
071        i4 = ConstantNode.forInt(14, graph);
072        node.itail = new NodeInputList<>(node, new ValueNode[]{i3, i4});
073        node.i1 = i1;
074        node.i2 = i2;
075        graph.add(node);
076        inputs = node.getNodeClass().getInputEdges();
077    }
078
079    /**
080     * Checks that there are no checkcasts in the compiled version of
081     * {@link Edges#getNode(Node, long[], int)}.
082     */
083    @Test
084    public void test0() {
085        testMethod(getMethod("getNode", Node.class, long[].class, int.class), null, node, inputs.getOffsets(), 0);
086    }
087
088    /**
089     * Checks that there are no checkcasts in the compiled version of
090     * {@link Edges#getNodeList(Node, long[], int)}.
091     */
092    @Test
093    public void test1() {
094        testMethod(getMethod("getNodeList", Node.class, long[].class, int.class), null, node, inputs.getOffsets(), 2);
095    }
096
097    /**
098     * Checks that there are no checkcasts in the compiled version of
099     * {@link Edges#setNode(Node, int, Node)}.
100     */
101    @Test
102    public void test2() {
103        testMethod(getMethod("setNode", Node.class, int.class, Node.class), inputs, node, 1, i2);
104    }
105
106    private void testMethod(Method method, Object receiver, Object... args) {
107        try {
108            // Invoke the method to ensure it has a type profile
109            for (int i = 0; i < 5000; i++) {
110                method.invoke(receiver, args);
111            }
112        } catch (Exception e) {
113            throw new RuntimeException(e);
114        }
115
116        ResolvedJavaMethod javaMethod = getMetaAccess().lookupJavaMethod(method);
117        StructuredGraph g = parseProfiled(javaMethod, AllowAssumptions.NO);
118        HighTierContext context = getDefaultHighTierContext();
119        new InliningPhase(new InlineMethodSubstitutionsPolicy(), new CanonicalizerPhase()).apply(g, context);
120        new CanonicalizerPhase().apply(g, context);
121        Assert.assertTrue(g.getNodes().filter(CheckCastNode.class).isEmpty());
122    }
123
124    private static Method getMethod(final String name, Class<?>... parameters) {
125        try {
126            return Edges.class.getDeclaredMethod(name, parameters);
127        } catch (NoSuchMethodException | SecurityException e) {
128            throw new RuntimeException(e);
129        }
130    }
131}