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 boolean isValueKnown() {
  72         return this != NodeCycles.CYCLES_UNKNOWN && this != NodeCycles.CYCLES_UNSET;
  73     }
  74 
  75     public static final int IGNORE_CYCLES_CONTRACT_FACTOR = 0xFFFF;
  76 
  77     public static NodeCycles compute(NodeCycles base, int opCount) {
  78         assert opCount >= 0;
  79         if (opCount == 0) {
  80             return CYCLES_0;
  81         }
  82         assert base.ordinal() > CYCLES_0.ordinal();
  83         int log2 = log2(base.value * opCount);
  84         NodeCycles[] values = values();
  85         for (int i = base.ordinal(); i < values.length; i++) {
  86             if (log2(values[i].value) == log2) {
  87                 return values[i];
  88             }
  89         }
  90         return CYCLES_1024;
  91     }
  92 
  93     public static NodeCycles compute(int rawValue) {
  94         assert rawValue >= 0;
  95         if (rawValue == 0) {
  96             return CYCLES_0;
  97         }
  98         NodeCycles[] values = values();
  99         for (int i = CYCLES_0.ordinal(); i < values.length - 1; i++) {
 100             if (values[i].value >= rawValue && rawValue <= values[i + 1].value) {
 101                 int r1 = values[i].value;
 102                 int r2 = values[i + 1].value;
 103                 int diff = r2 - r1;
 104                 return rawValue - r1 > diff / 2 ? values[i + 1] : values[i];
 105             }
 106         }
 107         return CYCLES_1024;
 108     }
 109 
 110     private static int log2(int val) {
 111         return (Integer.SIZE - 1) - Integer.numberOfLeadingZeros(val);
 112     }
 113 }