1 /*
   2  * Copyright (c) 2015, 2015, 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 package org.graalvm.compiler.api.directives.test;
  24 
  25 import java.lang.annotation.ElementType;
  26 import java.lang.annotation.Repeatable;
  27 import java.lang.annotation.Retention;
  28 import java.lang.annotation.RetentionPolicy;
  29 import java.lang.annotation.Target;
  30 import java.util.Arrays;
  31 import java.util.Collections;
  32 import java.util.List;
  33 
  34 import org.junit.Assert;
  35 import org.junit.Test;
  36 
  37 import org.graalvm.compiler.api.directives.GraalDirectives;
  38 import org.graalvm.compiler.core.test.GraalCompilerTest;
  39 import org.graalvm.compiler.graph.Node;
  40 import org.graalvm.compiler.graph.iterators.NodeIterable;
  41 import org.graalvm.compiler.nodes.IfNode;
  42 import org.graalvm.compiler.nodes.LoopBeginNode;
  43 import org.graalvm.compiler.nodes.ReturnNode;
  44 import org.graalvm.compiler.nodes.StructuredGraph;
  45 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  46 import org.graalvm.compiler.nodes.debug.ControlFlowAnchorNode;
  47 
  48 import jdk.vm.ci.meta.ResolvedJavaMethod;
  49 
  50 public class ControlFlowAnchorDirectiveTest extends GraalCompilerTest {
  51 
  52     @Retention(RetentionPolicy.RUNTIME)
  53     @Target(ElementType.METHOD)
  54     @Repeatable(AnchorSnippet.class)
  55     private @interface NodeCount {
  56 
  57         Class<? extends Node> nodeClass();
  58 
  59         int expectedCount();
  60     }
  61 
  62     @Retention(RetentionPolicy.RUNTIME)
  63     @Target(ElementType.METHOD)
  64     private @interface AnchorSnippet {
  65         NodeCount[] value();
  66     }
  67 
  68     @NodeCount(nodeClass = ReturnNode.class, expectedCount = 1)
  69     public static int verifyMergeSnippet(int arg) {
  70         if (arg > 5) {
  71             return 1;
  72         } else {
  73             return 2;
  74         }
  75     }
  76 
  77     @NodeCount(nodeClass = ControlFlowAnchorNode.class, expectedCount = 2)
  78     @NodeCount(nodeClass = ReturnNode.class, expectedCount = 2)
  79     public static int preventMergeSnippet(int arg) {
  80         if (arg > 5) {
  81             GraalDirectives.controlFlowAnchor();
  82             return 1;
  83         } else {
  84             GraalDirectives.controlFlowAnchor();
  85             return 2;
  86         }
  87     }
  88 
  89     @Test
  90     public void testMerge() {
  91         test("verifyMergeSnippet", 42);
  92         test("preventMergeSnippet", 42);
  93     }
  94 
  95     @NodeCount(nodeClass = ReturnNode.class, expectedCount = 2)
  96     public static int verifyDuplicateSnippet(int arg) {
  97         int ret;
  98         if (arg > 5) {
  99             ret = 17;
 100         } else {
 101             ret = arg;
 102         }
 103         return 42 / ret;
 104     }
 105 
 106     @NodeCount(nodeClass = ControlFlowAnchorNode.class, expectedCount = 1)
 107     @NodeCount(nodeClass = ReturnNode.class, expectedCount = 1)
 108     public static int preventDuplicateSnippet(int arg) {
 109         int ret;
 110         if (arg > 5) {
 111             ret = 17;
 112         } else {
 113             ret = arg;
 114         }
 115         GraalDirectives.controlFlowAnchor();
 116         return 42 / ret;
 117     }
 118 
 119     @Test
 120     public void testDuplicate() {
 121         // test("verifyDuplicateSnippet", 42);
 122         test("preventDuplicateSnippet", 42);
 123     }
 124 
 125     @NodeCount(nodeClass = LoopBeginNode.class, expectedCount = 0)
 126     public static int verifyFullUnrollSnippet(int arg) {
 127         int ret = arg;
 128         for (int i = 0; i < 5; i++) {
 129             ret = ret * 3 + 1;
 130         }
 131         return ret;
 132     }
 133 
 134     @NodeCount(nodeClass = LoopBeginNode.class, expectedCount = 1)
 135     @NodeCount(nodeClass = ControlFlowAnchorNode.class, expectedCount = 1)
 136     public static int preventFullUnrollSnippet(int arg) {
 137         int ret = arg;
 138         for (int i = 0; i < 5; i++) {
 139             GraalDirectives.controlFlowAnchor();
 140             ret = ret * 3 + 1;
 141         }
 142         return ret;
 143     }
 144 
 145     @Test
 146     public void testFullUnroll() {
 147         test("verifyFullUnrollSnippet", 42);
 148         test("preventFullUnrollSnippet", 42);
 149     }
 150 
 151     @NodeCount(nodeClass = LoopBeginNode.class, expectedCount = 1)
 152     @NodeCount(nodeClass = IfNode.class, expectedCount = 4)
 153     public static void verifyPeelSnippet(int arg) {
 154         int ret = arg;
 155         while (ret > 1) {
 156             if (ret % 2 == 0) {
 157                 ret /= 2;
 158             } else {
 159                 ret = 3 * ret + 1;
 160             }
 161         }
 162     }
 163 
 164     @NodeCount(nodeClass = LoopBeginNode.class, expectedCount = 1)
 165     @NodeCount(nodeClass = IfNode.class, expectedCount = 2)
 166     public static void preventPeelSnippet(int arg) {
 167         int ret = arg;
 168         while (ret > 1) {
 169             GraalDirectives.controlFlowAnchor();
 170             if (ret % 2 == 0) {
 171                 GraalDirectives.controlFlowAnchor();
 172                 ret /= 2;
 173             } else {
 174                 ret = 3 * ret + 1;
 175             }
 176         }
 177     }
 178 
 179     @Test
 180     public void testPeel() {
 181         test("preventPeelSnippet", 42);
 182     }
 183 
 184     @NodeCount(nodeClass = LoopBeginNode.class, expectedCount = 2)
 185     public static void verifyUnswitchSnippet(int arg, boolean flag) {
 186         int ret = arg;
 187         while (GraalDirectives.injectBranchProbability(0.9999, ret < 1000)) {
 188             if (flag) {
 189                 ret = ret * 2 + 1;
 190             } else {
 191                 ret = ret * 3 + 1;
 192             }
 193         }
 194     }
 195 
 196     @NodeCount(nodeClass = LoopBeginNode.class, expectedCount = 1)
 197     @NodeCount(nodeClass = IfNode.class, expectedCount = 2)
 198     public static void preventUnswitchSnippet(int arg, boolean flag) {
 199         int ret = arg;
 200         while (GraalDirectives.injectBranchProbability(0.9999, ret < 1000)) {
 201             if (flag) {
 202                 GraalDirectives.controlFlowAnchor();
 203                 ret++;
 204             } else {
 205                 ret += 2;
 206             }
 207         }
 208     }
 209 
 210     @Test
 211     public void testUnswitch() {
 212         test("verifyUnswitchSnippet", 0, false);
 213         test("preventUnswitchSnippet", 0, false);
 214     }
 215 
 216     /**
 217      * Cloning a ControlFlowAnchorNode is not allowed but cloning a whole graph containing one is
 218      * ok.
 219      */
 220     @Test
 221     public void testClone() {
 222         StructuredGraph g = parseEager("preventPeelSnippet", AllowAssumptions.NO);
 223         g.copy();
 224     }
 225 
 226     private static List<NodeCount> getNodeCountAnnotations(StructuredGraph graph) {
 227         ResolvedJavaMethod method = graph.method();
 228         AnchorSnippet snippet = method.getAnnotation(AnchorSnippet.class);
 229         if (snippet != null) {
 230             return Arrays.asList(snippet.value());
 231         }
 232 
 233         NodeCount single = method.getAnnotation(NodeCount.class);
 234         if (single != null) {
 235             return Collections.singletonList(single);
 236         }
 237 
 238         return Collections.emptyList();
 239     }
 240 
 241     @Override
 242     protected boolean checkLowTierGraph(StructuredGraph graph) {
 243         List<ControlFlowAnchorNode> anchors = graph.getNodes().filter(ControlFlowAnchorNode.class).snapshot();
 244         for (int i = 0; i < anchors.size(); i++) {
 245             ControlFlowAnchorNode a = anchors.get(i);
 246             for (int j = i + 1; j < anchors.size(); j++) {
 247                 ControlFlowAnchorNode b = anchors.get(j);
 248                 if (a.valueEquals(b)) {
 249                     Assert.fail("found duplicated control flow anchors (" + a + " and " + b + ")");
 250                 }
 251             }
 252         }
 253 
 254         for (NodeCount nodeCount : getNodeCountAnnotations(graph)) {
 255             NodeIterable<? extends Node> nodes = graph.getNodes().filter(nodeCount.nodeClass());
 256             Assert.assertEquals(nodeCount.nodeClass().getSimpleName(), nodeCount.expectedCount(), nodes.count());
 257         }
 258         return true;
 259     }
 260 }