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