comparison truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/profiles/FloatValueProfile.java @ 22501:a63bda98cfdb

Extract profiles into separate package. Add isProfilingEnabled in TruffleRuntime to disable profiling in the default runtime; Add low overhead profiles for primitives; Add LoopConditionProfile; Profile footprint/threadsafety improvements; Make toString implementations more consistent; Greatly enhanced javadoc documentation for profiles; Deprecate old profiles
author Christian Humer <christian.humer@oracle.com>
date Wed, 16 Dec 2015 16:38:13 +0100
parents
children
comparison
equal deleted inserted replaced
22500:fbe1eb7b4172 22501:a63bda98cfdb
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>float</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 FloatValueProfile profile = FloatValueProfile.createRawIdentityProfile();
47 *
48 * float execute(float input) {
49 * float 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 FloatValueProfile extends Profile {
64
65 FloatValueProfile() {
66 }
67
68 public abstract float profile(float value);
69
70 /**
71 * Returns a value profile that profiles the exact value of a <code>float</code> using
72 * {@link Float#floatToRawIntBits(float)}.
73 *
74 * @see IntValueProfile
75 */
76 public static FloatValueProfile 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 FloatValueProfile {
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 float cachedValue;
91 @CompilationFinal private int cachedRawValue;
92 @CompilationFinal private byte state = 0;
93
94 @Override
95 public float profile(float value) {
96 byte localState = this.state;
97 if (localState != GENERIC) {
98 if (localState == SPECIALIZED) {
99 if (cachedRawValue == Float.floatToRawIntBits(value)) {
100 return cachedValue;
101 }
102 }
103 CompilerDirectives.transferToInterpreterAndInvalidate();
104 if (localState == UNINITIALIZED) {
105 this.cachedValue = value;
106 this.cachedRawValue = Float.floatToRawIntBits(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 float getCachedValue() {
124 return cachedValue;
125 }
126
127 @Override
128 public String toString() {
129 return toString(FloatValueProfile.class, state == UNINITIALIZED, state == GENERIC, //
130 String.format("value == (float)%s (raw %h)", cachedValue, cachedRawValue));
131 }
132
133 /* Needed for lazy class loading. */
134 static FloatValueProfile create() {
135 return new Enabled();
136 }
137 }
138
139 static final class Disabled extends FloatValueProfile {
140
141 static final FloatValueProfile INSTANCE = new Disabled();
142
143 @Override
144 protected Object clone() {
145 return INSTANCE;
146 }
147
148 @Override
149 public float profile(float value) {
150 return value;
151 }
152
153 @Override
154 public String toString() {
155 return toStringDisabled(FloatValueProfile.class);
156 }
157
158 }
159 }