1 /*
   2  * Copyright (c) 2015, 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.api.directives.test;
  26 
  27 import org.graalvm.compiler.api.directives.GraalDirectives;
  28 import org.graalvm.compiler.core.test.GraalCompilerTest;
  29 import org.graalvm.compiler.graph.iterators.NodeIterable;
  30 import org.graalvm.compiler.nodes.AbstractBeginNode;
  31 import org.graalvm.compiler.nodes.IfNode;
  32 import org.graalvm.compiler.nodes.ReturnNode;
  33 import org.graalvm.compiler.nodes.StructuredGraph;
  34 import org.graalvm.compiler.nodes.debug.ControlFlowAnchorNode;
  35 import org.junit.Assert;
  36 import org.junit.Test;
  37 
  38 public class ProbabilityDirectiveTest extends GraalCompilerTest {
  39 
  40     public static int branchProbabilitySnippet(int arg) {
  41         if (GraalDirectives.injectBranchProbability(0.125, arg > 0)) {
  42             GraalDirectives.controlFlowAnchor(); // prevent removal of the if
  43             return 1;
  44         } else {
  45             GraalDirectives.controlFlowAnchor(); // prevent removal of the if
  46             return 2;
  47         }
  48     }
  49 
  50     @Test
  51     public void testBranchProbability() {
  52         test("branchProbabilitySnippet", 5);
  53     }
  54 
  55     public static int branchProbabilitySnippet2(int arg) {
  56         if (!GraalDirectives.injectBranchProbability(0.125, arg <= 0)) {
  57             GraalDirectives.controlFlowAnchor(); // prevent removal of the if
  58             return 2;
  59         } else {
  60             GraalDirectives.controlFlowAnchor(); // prevent removal of the if
  61             return 1;
  62         }
  63     }
  64 
  65     @Test
  66     public void testBranchProbability2() {
  67         test("branchProbabilitySnippet2", 5);
  68     }
  69 
  70     @Override
  71     protected boolean checkLowTierGraph(StructuredGraph graph) {
  72         NodeIterable<IfNode> ifNodes = graph.getNodes(IfNode.TYPE);
  73         Assert.assertEquals("IfNode count", 1, ifNodes.count());
  74 
  75         IfNode ifNode = ifNodes.first();
  76         AbstractBeginNode oneSuccessor;
  77         if (returnValue(ifNode.trueSuccessor()) == 1) {
  78             oneSuccessor = ifNode.trueSuccessor();
  79         } else {
  80             assert returnValue(ifNode.falseSuccessor()) == 1;
  81             oneSuccessor = ifNode.falseSuccessor();
  82         }
  83         Assert.assertEquals("branch probability of " + ifNode, 0.125, ifNode.probability(oneSuccessor), 0);
  84 
  85         return true;
  86     }
  87 
  88     private static int returnValue(AbstractBeginNode b) {
  89         ControlFlowAnchorNode anchor = (ControlFlowAnchorNode) b.next();
  90         ReturnNode returnNode = (ReturnNode) anchor.next();
  91         return returnNode.result().asJavaConstant().asInt();
  92     }
  93 }