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 java.util.HashMap;
  28 import java.util.Map;
  29 
  30 import org.graalvm.compiler.graph.Node;
  31 import org.graalvm.compiler.graph.iterators.NodeIterable;
  32 import org.graalvm.compiler.nodes.ConstantNode;
  33 import org.graalvm.compiler.nodes.FrameState;
  34 import org.graalvm.compiler.nodes.Invoke;
  35 import org.graalvm.compiler.nodes.ParameterNode;
  36 import org.graalvm.compiler.nodes.StructuredGraph;
  37 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  38 import org.graalvm.compiler.nodes.java.MonitorExitNode;
  39 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  40 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
  41 import org.graalvm.compiler.phases.tiers.HighTierContext;
  42 import org.junit.Assert;
  43 import org.junit.Test;
  44 
  45 /**
  46  * In the following tests, the usages of local variable "a" are replaced with the integer constant
  47  * 0. Then canonicalization is applied and it is verified that the resulting graph is equal to the
  48  * graph of the method that just has a "return 1" statement in it.
  49  */
  50 public class MonitorGraphTest extends GraalCompilerTest {
  51 
  52     private static final String REFERENCE_SNIPPET = "referenceSnippet";
  53 
  54     @SuppressWarnings("all")
  55     public static synchronized int referenceSnippet(int a) {
  56         return 1;
  57     }
  58 
  59     public static int const1() {
  60         return 1;
  61     }
  62 
  63     @Test
  64     public void test1() {
  65         test("test1Snippet");
  66     }
  67 
  68     @SuppressWarnings("all")
  69     public static synchronized int test1Snippet(int a) {
  70         return const1();
  71     }
  72 
  73     @Test
  74     public void test2() {
  75         StructuredGraph graph = parseAndProcess("test2Snippet");
  76         NodeIterable<MonitorExitNode> monitors = graph.getNodes(MonitorExitNode.TYPE);
  77         Assert.assertEquals(1, monitors.count());
  78         Assert.assertEquals(monitors.first().stateAfter().bci, 3);
  79     }
  80 
  81     @SuppressWarnings("all")
  82     public static int test2Snippet(int a) {
  83         return const2();
  84     }
  85 
  86     public static synchronized int const2() {
  87         return 1;
  88     }
  89 
  90     private StructuredGraph parseAndProcess(String snippet) {
  91         StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO);
  92         ParameterNode param = graph.getNodes(ParameterNode.TYPE).first();
  93         if (param != null) {
  94             ConstantNode constant = ConstantNode.forInt(0, graph);
  95             for (Node n : param.usages().snapshot()) {
  96                 if (!(n instanceof FrameState)) {
  97                     n.replaceFirstInput(param, constant);
  98                 }
  99             }
 100         }
 101         Map<Invoke, Double> hints = new HashMap<>();
 102         for (Invoke invoke : graph.getInvokes()) {
 103             hints.put(invoke, 1000d);
 104         }
 105         HighTierContext context = getDefaultHighTierContext();
 106         createInliningPhase(hints, new CanonicalizerPhase()).apply(graph, context);
 107         new CanonicalizerPhase().apply(graph, context);
 108         new DeadCodeEliminationPhase().apply(graph);
 109         return graph;
 110     }
 111 
 112     private void test(String snippet) {
 113         StructuredGraph graph = parseAndProcess(snippet);
 114         StructuredGraph referenceGraph = parseEager(REFERENCE_SNIPPET, AllowAssumptions.NO);
 115         assertEquals(referenceGraph, graph);
 116     }
 117 }