< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/ea/EATestBase.java

Print this page
rev 52509 : [mq]: graal


   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 


 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 


   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 


 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 
< prev index next >