1 /*
   2  * Copyright (c) 2011, 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 
  24 
  25 package org.graalvm.compiler.core.test.ea;
  26 
  27 import java.util.List;
  28 
  29 import org.graalvm.compiler.core.test.GraalCompilerTest;
  30 import org.graalvm.compiler.debug.DebugContext;
  31 import org.graalvm.compiler.nodes.ReturnNode;
  32 import org.graalvm.compiler.nodes.StructuredGraph;
  33 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  34 import org.graalvm.compiler.nodes.java.NewArrayNode;
  35 import org.graalvm.compiler.nodes.java.NewInstanceNode;
  36 import org.graalvm.compiler.nodes.virtual.CommitAllocationNode;
  37 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  38 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
  39 import org.graalvm.compiler.phases.common.inlining.InliningPhase;
  40 import org.graalvm.compiler.phases.tiers.HighTierContext;
  41 import org.graalvm.compiler.virtual.phases.ea.PartialEscapePhase;
  42 import org.junit.Assert;
  43 
  44 import jdk.vm.ci.meta.JavaConstant;
  45 import jdk.vm.ci.meta.ResolvedJavaMethod;
  46 
  47 //JaCoCo Exclude
  48 
  49 /**
  50  * This base class for all Escape Analysis tests does not contain tests itself, therefore it is not
  51  * automatically excluded from JaCoCo. Since it includes code that is used in the test snippets, it
  52  * needs to be excluded manually.
  53  */
  54 public class EATestBase extends GraalCompilerTest {
  55 
  56     public static class TestClassInt {
  57         public int x;
  58         public int y;
  59         public int z;
  60 
  61         public TestClassInt() {
  62             this(0, 0);
  63         }
  64 
  65         public TestClassInt(int x) {
  66             this(x, 0);
  67         }
  68 
  69         public TestClassInt(int x, int y) {
  70             this.x = x;
  71             this.y = y;
  72         }
  73 
  74         @Override
  75         public boolean equals(Object obj) {
  76             TestClassInt other = (TestClassInt) obj;
  77             return x == other.x && y == other.y && z == other.z;
  78         }
  79 
  80         @Override
  81         public String toString() {
  82             return "{" + x + "," + y + "," + z + "}";
  83         }
  84 
  85         @Override
  86         public int hashCode() {
  87             return x + 13 * y;
  88         }
  89     }
  90 
  91     public static class TestClassObject {
  92         public Object x;
  93         public Object y;
  94 
  95         public TestClassObject() {
  96             this(null, null);
  97         }
  98 
  99         public TestClassObject(Object x) {
 100             this(x, null);
 101         }
 102 
 103         public TestClassObject(Object x, Object y) {
 104             this.x = x;
 105             this.y = y;
 106         }
 107 
 108         @Override
 109         public boolean equals(Object obj) {
 110             TestClassObject other = (TestClassObject) obj;
 111             return x == other.x && y == other.y;
 112         }
 113 
 114         @Override
 115         public String toString() {
 116             return "{" + x + "," + y + "}";
 117         }
 118 
 119         @Override
 120         public int hashCode() {
 121             return (x == null ? 0 : x.hashCode()) + 13 * (y == null ? 0 : y.hashCode());
 122         }
 123     }
 124 
 125     protected static native void notInlineable();
 126 
 127     protected StructuredGraph graph;
 128     protected HighTierContext context;
 129     protected List<ReturnNode> returnNodes;
 130 
 131     /**
 132      * Runs Escape Analysis on the given snippet and makes sure that no allocations remain in the
 133      * graph.
 134      *
 135      * @param snippet the name of the method whose graph should be processed
 136      * @param expectedConstantResult if this is non-null, the resulting graph needs to have the
 137      *            given constant return value
 138      * @param iterativeEscapeAnalysis true if escape analysis should be run for more than one
 139      *            iteration
 140      */
 141     protected void testEscapeAnalysis(String snippet, JavaConstant expectedConstantResult, boolean iterativeEscapeAnalysis) {
 142         prepareGraph(snippet, iterativeEscapeAnalysis);
 143         if (expectedConstantResult != null) {
 144             for (ReturnNode returnNode : returnNodes) {
 145                 Assert.assertTrue(returnNode.result().toString(), returnNode.result().isConstant());
 146                 Assert.assertEquals(expectedConstantResult, returnNode.result().asConstant());
 147             }
 148         }
 149         int newInstanceCount = graph.getNodes().filter(NewInstanceNode.class).count() + graph.getNodes().filter(NewArrayNode.class).count() +
 150                         graph.getNodes().filter(CommitAllocationNode.class).count();
 151         Assert.assertEquals(0, newInstanceCount);
 152     }
 153 
 154     @SuppressWarnings("try")
 155     protected void prepareGraph(String snippet, boolean iterativeEscapeAnalysis) {
 156         ResolvedJavaMethod method = getResolvedJavaMethod(snippet);
 157         DebugContext debug = getDebugContext();
 158         try (DebugContext.Scope s = debug.scope(getClass(), method, getCodeCache())) {
 159             graph = parseEager(method, AllowAssumptions.YES, debug);
 160             context = getDefaultHighTierContext();
 161             new InliningPhase(new CanonicalizerPhase()).apply(graph, context);
 162             new DeadCodeEliminationPhase().apply(graph);
 163             canonicalizeGraph();
 164             new PartialEscapePhase(iterativeEscapeAnalysis, false, new CanonicalizerPhase(), null, graph.getOptions()).apply(graph, context);
 165             postEACanonicalizeGraph();
 166             returnNodes = graph.getNodes(ReturnNode.TYPE).snapshot();
 167         } catch (Throwable e) {
 168             throw debug.handle(e);
 169         }
 170     }
 171 
 172     protected void postEACanonicalizeGraph() {
 173     }
 174 
 175     protected void canonicalizeGraph() {
 176         new CanonicalizerPhase().apply(graph, context);
 177     }
 178 }