1 /*
   2  * Copyright (c) 2016, 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.nodeinfo;
  24 
  25 /**
  26  * Constants representing an estimation of the number of CPU cycles needed to execute a certain
  27  * compiler node.
  28  */
  29 public enum NodeCycles {
  30 
  31     /**
  32      * The default value of the {@link NodeInfo#cycles()} property.
  33      * <p>
  34      * For further information about the use of {@code CYCLES_UNSET} see {@link NodeInfo#cycles()}.
  35      */
  36     CYCLES_UNSET(0),
  37     /**
  38      * Nodes for which, due to arbitrary reasons, no estimation can be made either (1) statically
  39      * without inspecting the properties of a node or (2) at all (like e.g. for an invocation).
  40      * <p>
  41      * Nodes annotated with {@code CYCLES_UNKNOWN} should specify the
  42      * {@link NodeInfo#cyclesRationale()} property to clarify why an estimation cannot be done.
  43      */
  44     CYCLES_UNKNOWN(0),
  45     /**
  46      * Nodes for which runtime information is irrelevant and can be ignored, e.g. for test nodes.
  47      */
  48     CYCLES_IGNORED(0),
  49     /**
  50      * Nodes that do not consume any CPU time during the "execution", e.g. Constants.
  51      */
  52     CYCLES_0(0),
  53     CYCLES_1(1),
  54     CYCLES_2(2),
  55     CYCLES_4(4),
  56     CYCLES_8(8),
  57     CYCLES_16(16),
  58     CYCLES_32(32),
  59     CYCLES_64(64),
  60     CYCLES_128(128),
  61     CYCLES_256(256),
  62     CYCLES_512(512),
  63     CYCLES_1024(1024);
  64 
  65     public final int value;
  66 
  67     NodeCycles(int value) {
  68         this.value = value;
  69     }
  70 
  71     public static final int IGNORE_CYCLES_CONTRACT_FACTOR = 0xFFFF;
  72 
  73     public static NodeCycles compute(NodeCycles base, int opCount) {
  74         assert opCount >= 0;
  75         if (opCount == 0) {
  76             return CYCLES_0;
  77         }
  78         assert base.ordinal() > CYCLES_0.ordinal();
  79         int log2 = log2(base.value * opCount);
  80         NodeCycles[] values = values();
  81         for (int i = base.ordinal(); i < values.length; i++) {
  82             if (log2(values[i].value) == log2) {
  83                 return values[i];
  84             }
  85         }
  86         return CYCLES_1024;
  87     }
  88 
  89     public static NodeCycles compute(int rawValue) {
  90         assert rawValue >= 0;
  91         if (rawValue == 0) {
  92             return CYCLES_0;
  93         }
  94         NodeCycles[] values = values();
  95         for (int i = CYCLES_0.ordinal(); i < values.length - 1; i++) {
  96             if (values[i].value >= rawValue && rawValue <= values[i + 1].value) {
  97                 int r1 = values[i].value;
  98                 int r2 = values[i + 1].value;
  99                 int diff = r2 - r1;
 100                 return rawValue - r1 > diff / 2 ? values[i + 1] : values[i];
 101             }
 102         }
 103         return CYCLES_1024;
 104     }
 105 
 106     private static int log2(int val) {
 107         return (Integer.SIZE - 1) - Integer.numberOfLeadingZeros(val);
 108     }
 109 }