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