1 /*
   2  * Copyright (c) 2017, 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 java.util.HashSet;
  28 
  29 import org.graalvm.compiler.api.directives.GraalDirectives;
  30 import org.graalvm.compiler.core.test.GraalCompilerTest;
  31 import org.graalvm.compiler.debug.DebugContext;
  32 import org.graalvm.compiler.nodes.debug.BlackholeNode;
  33 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  34 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
  35 import org.junit.Assert;
  36 import org.junit.Test;
  37 
  38 import jdk.vm.ci.code.InstalledCode;
  39 import jdk.vm.ci.meta.ResolvedJavaMethod;
  40 
  41 /*
  42  * Test whether complex tree structures properly maintain identity.
  43  */
  44 public class PartialEscapeAnalysisTreesTest extends EATestBase {
  45 
  46     static class TreeNode {
  47         TreeNode left;
  48         TreeNode right;
  49 
  50         TreeNode() {
  51 
  52         }
  53 
  54         TreeNode(TreeNode left, TreeNode right) {
  55             this.left = left;
  56             this.right = right;
  57         }
  58 
  59         public void visit(HashSet<TreeNode> instances) {
  60             instances.add(this);
  61             if (left != null) {
  62                 left.visit(instances);
  63             }
  64             if (right != null) {
  65                 right.visit(instances);
  66             }
  67         }
  68 
  69         int countInstances() {
  70             HashSet<TreeNode> instances = new HashSet<>();
  71             visit(instances);
  72             return instances.size();
  73         }
  74     }
  75 
  76     public static TreeNode buildTree(boolean a) {
  77         TreeNode leftChild;
  78         TreeNode rightChild;
  79         TreeNode taskToFork;
  80         TreeNode task;
  81         if (a) {
  82             GraalDirectives.blackhole(new TreeNode());
  83             leftChild = new TreeNode();
  84             rightChild = new TreeNode();
  85             task = new TreeNode(leftChild, rightChild);
  86             taskToFork = rightChild;
  87             GraalDirectives.blackhole(task);
  88         } else {
  89             leftChild = new TreeNode();
  90             rightChild = new TreeNode();
  91             task = new TreeNode(leftChild, rightChild);
  92             taskToFork = leftChild;
  93             GraalDirectives.blackhole(task);
  94         }
  95         if (taskToFork.left == null) {
  96             taskToFork.left = new TreeNode();
  97         }
  98 
  99         return new TreeNode(task, null);
 100     }
 101 
 102     @Test
 103     public void testBuildTree() {
 104         testGraph("buildTree");
 105     }
 106 
 107     /**
 108      * Prepare a graph that includes some blackholes and then remove the blackholes and compile
 109      * normally to create an unusual situation for PEA.
 110      */
 111     @SuppressWarnings("try")
 112     public void testGraph(String name) {
 113         ResolvedJavaMethod method = getResolvedJavaMethod(name);
 114 
 115         prepareGraph(name, true);
 116         try (DebugContext.Scope s = graph.getDebug().scope(getClass(), method, getCodeCache(), graph)) {
 117             for (BlackholeNode node : graph.getNodes().filter(BlackholeNode.class)) {
 118                 graph.removeFixed(node);
 119             }
 120             new DeadCodeEliminationPhase().apply(graph);
 121             new CanonicalizerPhase().apply(graph, context);
 122 
 123             InstalledCode code = getCode(method, graph, true);
 124 
 125             GraalCompilerTest.Result r = executeExpected(method, null, true);
 126             int expectedInstances = ((TreeNode) r.returnValue).countInstances();
 127             TreeNode r2 = (TreeNode) code.executeVarargs(true);
 128             Assert.assertEquals("Wrong number of nodes in tree", expectedInstances, r2.countInstances());
 129 
 130             r = executeExpected(method, null, false);
 131             expectedInstances = ((TreeNode) r.returnValue).countInstances();
 132             r2 = (TreeNode) code.executeVarargs(false);
 133             Assert.assertEquals("Wrong number of nodes in tree", expectedInstances, r2.countInstances());
 134         } catch (Throwable e) {
 135             throw graph.getDebug().handle(e);
 136         }
 137     }
 138 }