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;
  24 
  25 import java.nio.charset.Charset;
  26 
  27 // JaCoCo Exclude
  28 
  29 /**
  30  * Directives that influence the compilation of methods by Graal. They don't influence the semantics
  31  * of the code, but they are useful for unit testing and benchmarking.
  32  */
  33 public final class GraalDirectives {
  34 
  35     public static final double LIKELY_PROBABILITY = 0.75;
  36     public static final double UNLIKELY_PROBABILITY = 1.0 - LIKELY_PROBABILITY;
  37 
  38     public static final double SLOWPATH_PROBABILITY = 0.0001;
  39     public static final double FASTPATH_PROBABILITY = 1.0 - SLOWPATH_PROBABILITY;
  40 
  41     /**
  42      * Directive for the compiler to fall back to the bytecode interpreter at this point.
  43      */
  44     public static void deoptimize() {
  45     }
  46 
  47     /**
  48      * Directive for the compiler to fall back to the bytecode interpreter at this point, invalidate
  49      * the compiled code and reprofile the method.
  50      */
  51     public static void deoptimizeAndInvalidate() {
  52     }
  53 
  54     /**
  55      * Returns a boolean value indicating whether the method is executed in Graal-compiled code.
  56      */
  57     public static boolean inCompiledCode() {
  58         return false;
  59     }
  60 
  61     /**
  62      * A call to this method will never be duplicated by control flow optimizations in the compiler.
  63      */
  64     public static void controlFlowAnchor() {
  65     }
  66 
  67     /**
  68      * Injects a probability for the given condition into the profiling information of a branch
  69      * instruction. The probability must be a value between 0.0 and 1.0 (inclusive).
  70      *
  71      * Example usage (it specifies that the likelihood for a to be greater than b is 90%):
  72      *
  73      * <code>
  74      * if (injectBranchProbability(0.9, a &gt; b)) {
  75      *    // ...
  76      * }
  77      * </code>
  78      *
  79      * There are predefined constants for commonly used probabilities (see
  80      * {@link #LIKELY_PROBABILITY} , {@link #UNLIKELY_PROBABILITY}, {@link #SLOWPATH_PROBABILITY},
  81      * {@link #FASTPATH_PROBABILITY} ).
  82      *
  83      * @param probability the probability value between 0.0 and 1.0 that should be injected
  84      */
  85     public static boolean injectBranchProbability(double probability, boolean condition) {
  86         assert probability >= 0.0 && probability <= 1.0;
  87         return condition;
  88     }
  89 
  90     /**
  91      * Injects an average iteration count of a loop into the probability information of a loop exit
  92      * condition. The iteration count specifies how often the condition is checked, i.e. in for and
  93      * while loops it is one more than the body iteration count, and in do-while loops it is equal
  94      * to the body iteration count. The iteration count must be >= 1.0.
  95      *
  96      * Example usage (it specifies that the expected iteration count of the loop condition is 500,
  97      * so the iteration count of the loop body is 499):
  98      *
  99      * <code>
 100      * for (int i = 0; injectIterationCount(500, i < array.length); i++) {
 101      *     // ...
 102      * }
 103      * </code>
 104      *
 105      * @param iterations the expected number of iterations that should be injected
 106      */
 107     public static boolean injectIterationCount(double iterations, boolean condition) {
 108         return injectBranchProbability(1. - 1. / iterations, condition);
 109     }
 110 
 111     /**
 112      * Consume a value, making sure the compiler doesn't optimize away the computation of this
 113      * value, even if it is otherwise unused.
 114      */
 115     @SuppressWarnings("unused")
 116     public static void blackhole(boolean value) {
 117     }
 118 
 119     /**
 120      * Consume a value, making sure the compiler doesn't optimize away the computation of this
 121      * value, even if it is otherwise unused.
 122      */
 123     @SuppressWarnings("unused")
 124     public static void blackhole(byte value) {
 125     }
 126 
 127     /**
 128      * Consume a value, making sure the compiler doesn't optimize away the computation of this
 129      * value, even if it is otherwise unused.
 130      */
 131     @SuppressWarnings("unused")
 132     public static void blackhole(short value) {
 133     }
 134 
 135     /**
 136      * Consume a value, making sure the compiler doesn't optimize away the computation of this
 137      * value, even if it is otherwise unused.
 138      */
 139     @SuppressWarnings("unused")
 140     public static void blackhole(char value) {
 141     }
 142 
 143     /**
 144      * Consume a value, making sure the compiler doesn't optimize away the computation of this
 145      * value, even if it is otherwise unused.
 146      */
 147     @SuppressWarnings("unused")
 148     public static void blackhole(int value) {
 149     }
 150 
 151     /**
 152      * Consume a value, making sure the compiler doesn't optimize away the computation of this
 153      * value, even if it is otherwise unused.
 154      */
 155     @SuppressWarnings("unused")
 156     public static void blackhole(long value) {
 157     }
 158 
 159     /**
 160      * Consume a value, making sure the compiler doesn't optimize away the computation of this
 161      * value, even if it is otherwise unused.
 162      */
 163     @SuppressWarnings("unused")
 164     public static void blackhole(float value) {
 165     }
 166 
 167     /**
 168      * Consume a value, making sure the compiler doesn't optimize away the computation of this
 169      * value, even if it is otherwise unused.
 170      */
 171     @SuppressWarnings("unused")
 172     public static void blackhole(double value) {
 173     }
 174 
 175     /**
 176      * Consume a value, making sure the compiler doesn't optimize away the computation of this
 177      * value, even if it is otherwise unused.
 178      */
 179     @SuppressWarnings("unused")
 180     public static void blackhole(Object value) {
 181     }
 182 
 183     /**
 184      * Forces a value to be kept in a register.
 185      */
 186     @SuppressWarnings("unused")
 187     public static void bindToRegister(boolean value) {
 188     }
 189 
 190     /**
 191      * Forces a value to be kept in a register.
 192      */
 193     @SuppressWarnings("unused")
 194     public static void bindToRegister(byte value) {
 195     }
 196 
 197     /**
 198      * Forces a value to be kept in a register.
 199      */
 200     @SuppressWarnings("unused")
 201     public static void bindToRegister(short value) {
 202     }
 203 
 204     /**
 205      * Forces a value to be kept in a register.
 206      */
 207     @SuppressWarnings("unused")
 208     public static void bindToRegister(char value) {
 209     }
 210 
 211     /**
 212      * Forces a value to be kept in a register.
 213      */
 214     @SuppressWarnings("unused")
 215     public static void bindToRegister(int value) {
 216     }
 217 
 218     /**
 219      * Forces a value to be kept in a register.
 220      */
 221     @SuppressWarnings("unused")
 222     public static void bindToRegister(long value) {
 223     }
 224 
 225     /**
 226      * Forces a value to be kept in a register.
 227      */
 228     @SuppressWarnings("unused")
 229     public static void bindToRegister(float value) {
 230     }
 231 
 232     /**
 233      * Forces a value to be kept in a register.
 234      */
 235     @SuppressWarnings("unused")
 236     public static void bindToRegister(double value) {
 237     }
 238 
 239     /**
 240      * Forces a value to be kept in a register.
 241      */
 242     @SuppressWarnings("unused")
 243     public static void bindToRegister(Object value) {
 244     }
 245 
 246     /**
 247      * Spills all caller saved registers.
 248      */
 249     public static void spillRegisters() {
 250     }
 251 
 252     /**
 253      * Do nothing, but also make sure the compiler doesn't do any optimizations across this call.
 254      *
 255      * For example, the compiler will constant fold the expression 5 * 3, but the expression 5 *
 256      * opaque(3) will result in a real multiplication, because the compiler will not see that
 257      * opaque(3) is a constant.
 258      */
 259     public static boolean opaque(boolean value) {
 260         return value;
 261     }
 262 
 263     /**
 264      * Do nothing, but also make sure the compiler doesn't do any optimizations across this call.
 265      *
 266      * For example, the compiler will constant fold the expression 5 * 3, but the expression 5 *
 267      * opaque(3) will result in a real multiplication, because the compiler will not see that
 268      * opaque(3) is a constant.
 269      */
 270     public static byte opaque(byte value) {
 271         return value;
 272     }
 273 
 274     /**
 275      * Do nothing, but also make sure the compiler doesn't do any optimizations across this call.
 276      *
 277      * For example, the compiler will constant fold the expression 5 * 3, but the expression 5 *
 278      * opaque(3) will result in a real multiplication, because the compiler will not see that
 279      * opaque(3) is a constant.
 280      */
 281     public static short opaque(short value) {
 282         return value;
 283     }
 284 
 285     /**
 286      * Do nothing, but also make sure the compiler doesn't do any optimizations across this call.
 287      *
 288      * For example, the compiler will constant fold the expression 5 * 3, but the expression 5 *
 289      * opaque(3) will result in a real multiplication, because the compiler will not see that
 290      * opaque(3) is a constant.
 291      */
 292     public static char opaque(char value) {
 293         return value;
 294     }
 295 
 296     /**
 297      * Do nothing, but also make sure the compiler doesn't do any optimizations across this call.
 298      *
 299      * For example, the compiler will constant fold the expression 5 * 3, but the expression 5 *
 300      * opaque(3) will result in a real multiplication, because the compiler will not see that
 301      * opaque(3) is a constant.
 302      */
 303     public static int opaque(int value) {
 304         return value;
 305     }
 306 
 307     /**
 308      * Do nothing, but also make sure the compiler doesn't do any optimizations across this call.
 309      *
 310      * For example, the compiler will constant fold the expression 5 * 3, but the expression 5 *
 311      * opaque(3) will result in a real multiplication, because the compiler will not see that
 312      * opaque(3) is a constant.
 313      */
 314     public static long opaque(long value) {
 315         return value;
 316     }
 317 
 318     /**
 319      * Do nothing, but also make sure the compiler doesn't do any optimizations across this call.
 320      *
 321      * For example, the compiler will constant fold the expression 5 * 3, but the expression 5 *
 322      * opaque(3) will result in a real multiplication, because the compiler will not see that
 323      * opaque(3) is a constant.
 324      */
 325     public static float opaque(float value) {
 326         return value;
 327     }
 328 
 329     /**
 330      * Do nothing, but also make sure the compiler doesn't do any optimizations across this call.
 331      *
 332      * For example, the compiler will constant fold the expression 5 * 3, but the expression 5 *
 333      * opaque(3) will result in a real multiplication, because the compiler will not see that
 334      * opaque(3) is a constant.
 335      */
 336     public static double opaque(double value) {
 337         return value;
 338     }
 339 
 340     /**
 341      * Do nothing, but also make sure the compiler doesn't do any optimizations across this call.
 342      *
 343      * For example, the compiler will constant fold the expression 5 * 3, but the expression 5 *
 344      * opaque(3) will result in a real multiplication, because the compiler will not see that
 345      * opaque(3) is a constant.
 346      */
 347     public static <T> T opaque(T value) {
 348         return value;
 349     }
 350 
 351     public static <T> T guardingNonNull(T value) {
 352         if (value == null) {
 353             deoptimize();
 354         }
 355         return value;
 356     }
 357 
 358     /**
 359      * Ensures that the given object will be virtual (escape analyzed) at all points that are
 360      * dominated by the current position.
 361      */
 362     public static void ensureVirtualized(@SuppressWarnings("unused") Object object) {
 363     }
 364 
 365     /**
 366      * Ensures that the given object will be virtual at the current position.
 367      */
 368     public static void ensureVirtualizedHere(@SuppressWarnings("unused") Object object) {
 369     }
 370 
 371     /**
 372      * Marks the beginning of an instrumentation boundary. The instrumentation code will be folded
 373      * during compilation and will not affect inlining heuristics regarding graph size except one on
 374      * compiled low-level graph size (e.g., {@code GraalOptions.SmallCompiledLowLevelGraphSize}).
 375      */
 376     public static void instrumentationBegin() {
 377     }
 378 
 379     /**
 380      * Marks the beginning of an instrumentation boundary and associates the instrumentation with
 381      * the preceding bytecode. If the instrumented instruction is {@code new}, then instrumentation
 382      * will adapt to optimizations concerning allocation, and only be executed if allocation really
 383      * happens.
 384      *
 385      * Example (the instrumentation is associated with {@code new}):
 386      *
 387      * <blockquote>
 388      *
 389      * <pre>
 390      *  0  new java.lang.Object
 391      *  3  invokestatic org.graalvm.compiler.api.directives.GraalDirectives.instrumentationBeginForPredecessor() : void
 392      *  6  invokestatic AllocationProfiler.countActualAllocation() : void
 393      *  9  invokestatic org.graalvm.compiler.api.directives.GraalDirectives.instrumentationEnd() : void
 394      * 12  invokespecial java.lang.Object()
 395      * </pre>
 396      *
 397      * </blockquote>
 398      *
 399      * @see #instrumentationBegin()
 400      */
 401     public static void instrumentationBeginForPredecessor() {
 402     }
 403 
 404     /**
 405      * Marks the end of the instrumentation boundary.
 406      *
 407      * @see #instrumentationBegin()
 408      */
 409     public static void instrumentationEnd() {
 410     }
 411 
 412     /**
 413      * @return true if the enclosing method is inlined.
 414      */
 415     public static boolean isMethodInlined() {
 416         return false;
 417     }
 418 
 419     private static final Charset UTF8 = Charset.forName("UTF-8");
 420 
 421     /**
 422      * @return the name of the root method for the current compilation task. If the enclosing method
 423      *         is inlined, it returns the name of the method into which it is inlined.
 424      */
 425     public static String rootName() {
 426         return new String(rawRootName(), UTF8);
 427     }
 428 
 429     public static byte[] rawRootName() {
 430         return new byte[0];
 431     }
 432 
 433 }