comparison truffle/com.oracle.truffle.api.profiles/src/com/oracle/truffle/api/profiles/DoubleValueProfile.java @ 22503:828c67903db2

Moving profiles into their own project to ensure the core API doesn't reference these utility classes.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Thu, 17 Dec 2015 10:01:38 +0100
parents truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/profiles/DoubleValueProfile.java@a63bda98cfdb
children d80a5ff56f51
comparison
equal deleted inserted replaced
22502:d2b4fe945c23 22503:828c67903db2
1 /*
2 * Copyright (c) 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package com.oracle.truffle.api.profiles;
26
27 import com.oracle.truffle.api.CompilerDirectives;
28 import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
29 import com.oracle.truffle.api.Truffle;
30
31 /**
32 * <p>
33 * Specialized value profile to capture certain properties of <code>double</code> runtime values.
34 * Value profiles require a runtime check in their initialized state to verify their profiled
35 * assumption. Value profiles are limited to capture monomorphic profiles only. This means that if
36 * two or more values are profiled within a single profile then the profile has no effect. If the
37 * value assumption is invalidated in compiled code then it is invalidated.
38 * </p>
39 *
40 * <p>
41 * <b> Usage example: </b>
42 *
43 * <pre>
44 * class SampleNode extends Node {
45 *
46 * final DoubleValueProfile profile = DoubleValueProfile.createRawIdentityProfile();
47 *
48 * double execute(double input) {
49 * double profiledValue = profile.profile(input);
50 * // compiler may know now more about profiledValue
51 * return profiledValue;
52 * }
53 * }
54 * </pre>
55 * <p>
56 *
57 *
58 * {@inheritDoc}
59 *
60 * @see #createRawIdentityProfile()
61 * @see ValueProfile
62 */
63 public abstract class DoubleValueProfile extends Profile {
64
65 DoubleValueProfile() {
66 }
67
68 public abstract double profile(double value);
69
70 /**
71 * Returns a value profile that profiles the exact value of a <code>double</code> using
72 * {@link Double#doubleToRawLongBits(double)}.
73 *
74 * @see IntValueProfile
75 */
76 public static DoubleValueProfile createRawIdentityProfile() {
77 if (Truffle.getRuntime().isProfilingEnabled()) {
78 return Enabled.create();
79 } else {
80 return Disabled.INSTANCE;
81 }
82 }
83
84 static final class Enabled extends DoubleValueProfile {
85
86 private static final byte UNINITIALIZED = 0;
87 private static final byte SPECIALIZED = 1;
88 private static final byte GENERIC = 2;
89
90 @CompilationFinal private double cachedValue;
91 @CompilationFinal private long cachedRawValue;
92 @CompilationFinal private byte state = 0;
93
94 @Override
95 public double profile(double value) {
96 byte localState = this.state;
97 if (localState != GENERIC) {
98 if (localState == SPECIALIZED) {
99 if (cachedRawValue == Double.doubleToRawLongBits(value)) {
100 return cachedValue;
101 }
102 }
103 CompilerDirectives.transferToInterpreterAndInvalidate();
104 if (localState == UNINITIALIZED) {
105 this.cachedValue = value;
106 this.cachedRawValue = Double.doubleToRawLongBits(value);
107 this.state = SPECIALIZED;
108 } else {
109 this.state = GENERIC;
110 }
111 }
112 return value;
113 }
114
115 boolean isGeneric() {
116 return state == GENERIC;
117 }
118
119 boolean isUninitialized() {
120 return state == UNINITIALIZED;
121 }
122
123 double getCachedValue() {
124 return cachedValue;
125 }
126
127 @Override
128 public String toString() {
129 return toString(DoubleValueProfile.class, state == UNINITIALIZED, state == GENERIC, //
130 String.format("value == (double)%s (raw %h)", cachedValue, cachedRawValue));
131 }
132
133 /* Needed for lazy class loading. */
134 static DoubleValueProfile create() {
135 return new Enabled();
136 }
137 }
138
139 static final class Disabled extends DoubleValueProfile {
140
141 static final DoubleValueProfile INSTANCE = new Disabled();
142
143 @Override
144 protected Object clone() {
145 return INSTANCE;
146 }
147
148 @Override
149 public double profile(double value) {
150 return value;
151 }
152
153 @Override
154 public String toString() {
155 return toStringDisabled(DoubleValueProfile.class);
156 }
157
158 }
159
160 }