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