< prev index next >

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

Print this page
rev 52889 : 8214023: Update Graal
   1 /*
   2  * Copyright (c) 2011, 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;
  26 
  27 import jdk.vm.ci.meta.ResolvedJavaMethod;
  28 
  29 import org.junit.Test;
  30 import org.graalvm.compiler.loop.DefaultLoopPolicies;
  31 import org.graalvm.compiler.loop.phases.LoopFullUnrollPhase;
  32 import org.graalvm.compiler.nodes.StructuredGraph;
  33 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  34 import org.graalvm.compiler.nodes.java.MonitorExitNode;
  35 import org.graalvm.compiler.nodes.java.RawMonitorEnterNode;
  36 import org.graalvm.compiler.nodes.spi.LoweringTool;
  37 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  38 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
  39 import org.graalvm.compiler.phases.common.LockEliminationPhase;
  40 import org.graalvm.compiler.phases.common.LoweringPhase;
  41 import org.graalvm.compiler.phases.common.inlining.InliningPhase;
  42 import org.graalvm.compiler.phases.tiers.HighTierContext;
  43 import org.graalvm.compiler.phases.tiers.PhaseContext;
  44 import org.graalvm.compiler.virtual.phases.ea.PartialEscapePhase;



  45 
  46 public class LockEliminationTest extends GraalCompilerTest {
  47 
  48     static class A {
  49 
  50         int value;
  51 
  52         public synchronized int getValue() {
  53             return value;
  54         }
  55     }
  56 
  57     static int field1;
  58     static int field2;
  59 
  60     public static void testSynchronizedSnippet(A x, A y) {
  61         synchronized (x) {
  62             field1 = x.value;
  63         }
  64         synchronized (x) {


 104     }
 105 
 106     @Test
 107     public void testUnrolledSync() {
 108         StructuredGraph graph = getGraph("testUnrolledSyncSnippet", false);
 109         CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
 110         canonicalizer.apply(graph, new PhaseContext(getProviders()));
 111         HighTierContext context = getDefaultHighTierContext();
 112         new LoopFullUnrollPhase(canonicalizer, new DefaultLoopPolicies()).apply(graph, context);
 113         new LockEliminationPhase().apply(graph);
 114         assertDeepEquals(1, graph.getNodes().filter(RawMonitorEnterNode.class).count());
 115         assertDeepEquals(1, graph.getNodes().filter(MonitorExitNode.class).count());
 116     }
 117 
 118     private StructuredGraph getGraph(String snippet, boolean doEscapeAnalysis) {
 119         ResolvedJavaMethod method = getResolvedJavaMethod(snippet);
 120         StructuredGraph graph = parseEager(method, AllowAssumptions.YES);
 121         HighTierContext context = getDefaultHighTierContext();
 122         CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
 123         canonicalizer.apply(graph, context);
 124         new InliningPhase(new CanonicalizerPhase()).apply(graph, context);
 125         new CanonicalizerPhase().apply(graph, context);
 126         new DeadCodeEliminationPhase().apply(graph);
 127         if (doEscapeAnalysis) {
 128             new PartialEscapePhase(true, canonicalizer, graph.getOptions()).apply(graph, context);
 129         }
 130         new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
 131         return graph;
 132     }
 133 
 134     public void testEscapeAnalysisSnippet(A a) {
 135         A newA = new A();
 136         synchronized (newA) {
 137             synchronized (a) {
 138                 field1 = a.value;
 139             }
 140         }
 141         /*
 142          * Escape analysis removes the synchronization on newA. But lock elimination still must not
 143          * combine the two synchronizations on the parameter a because they have a different lock
 144          * depth.


   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;
  26 



  27 import org.graalvm.compiler.loop.DefaultLoopPolicies;
  28 import org.graalvm.compiler.loop.phases.LoopFullUnrollPhase;
  29 import org.graalvm.compiler.nodes.StructuredGraph;
  30 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  31 import org.graalvm.compiler.nodes.java.MonitorExitNode;
  32 import org.graalvm.compiler.nodes.java.RawMonitorEnterNode;
  33 import org.graalvm.compiler.nodes.spi.LoweringTool;
  34 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  35 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
  36 import org.graalvm.compiler.phases.common.LockEliminationPhase;
  37 import org.graalvm.compiler.phases.common.LoweringPhase;

  38 import org.graalvm.compiler.phases.tiers.HighTierContext;
  39 import org.graalvm.compiler.phases.tiers.PhaseContext;
  40 import org.graalvm.compiler.virtual.phases.ea.PartialEscapePhase;
  41 import org.junit.Test;
  42 
  43 import jdk.vm.ci.meta.ResolvedJavaMethod;
  44 
  45 public class LockEliminationTest extends GraalCompilerTest {
  46 
  47     static class A {
  48 
  49         int value;
  50 
  51         public synchronized int getValue() {
  52             return value;
  53         }
  54     }
  55 
  56     static int field1;
  57     static int field2;
  58 
  59     public static void testSynchronizedSnippet(A x, A y) {
  60         synchronized (x) {
  61             field1 = x.value;
  62         }
  63         synchronized (x) {


 103     }
 104 
 105     @Test
 106     public void testUnrolledSync() {
 107         StructuredGraph graph = getGraph("testUnrolledSyncSnippet", false);
 108         CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
 109         canonicalizer.apply(graph, new PhaseContext(getProviders()));
 110         HighTierContext context = getDefaultHighTierContext();
 111         new LoopFullUnrollPhase(canonicalizer, new DefaultLoopPolicies()).apply(graph, context);
 112         new LockEliminationPhase().apply(graph);
 113         assertDeepEquals(1, graph.getNodes().filter(RawMonitorEnterNode.class).count());
 114         assertDeepEquals(1, graph.getNodes().filter(MonitorExitNode.class).count());
 115     }
 116 
 117     private StructuredGraph getGraph(String snippet, boolean doEscapeAnalysis) {
 118         ResolvedJavaMethod method = getResolvedJavaMethod(snippet);
 119         StructuredGraph graph = parseEager(method, AllowAssumptions.YES);
 120         HighTierContext context = getDefaultHighTierContext();
 121         CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
 122         canonicalizer.apply(graph, context);
 123         createInliningPhase().apply(graph, context);
 124         new CanonicalizerPhase().apply(graph, context);
 125         new DeadCodeEliminationPhase().apply(graph);
 126         if (doEscapeAnalysis) {
 127             new PartialEscapePhase(true, canonicalizer, graph.getOptions()).apply(graph, context);
 128         }
 129         new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
 130         return graph;
 131     }
 132 
 133     public void testEscapeAnalysisSnippet(A a) {
 134         A newA = new A();
 135         synchronized (newA) {
 136             synchronized (a) {
 137                 field1 = a.value;
 138             }
 139         }
 140         /*
 141          * Escape analysis removes the synchronization on newA. But lock elimination still must not
 142          * combine the two synchronizations on the parameter a because they have a different lock
 143          * depth.


< prev index next >