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