Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/profiles/ByteValueProfile.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>byte</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 ByteValueProfile profile = ByteValueProfile.createIdentityProfile(); | |
47 * | |
48 * byte execute(byte input) { | |
49 * byte 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 #createIdentityProfile() | |
61 * @see ValueProfile | |
62 */ | |
63 public abstract class ByteValueProfile extends Profile { | |
64 | |
65 ByteValueProfile() { | |
66 } | |
67 | |
68 public abstract byte profile(byte value); | |
69 | |
70 /** | |
71 * Returns a value profile that profiles the exact value of a <code>byte</code>. | |
72 * | |
73 * @see ByteValueProfile | |
74 */ | |
75 public static ByteValueProfile createIdentityProfile() { | |
76 if (Truffle.getRuntime().isProfilingEnabled()) { | |
77 return Enabled.create(); | |
78 } else { | |
79 return Disabled.INSTANCE; | |
80 } | |
81 } | |
82 | |
83 static final class Enabled extends ByteValueProfile { | |
84 | |
85 private static final byte UNINITIALIZED = 0; | |
86 private static final byte SPECIALIZED = 1; | |
87 private static final byte GENERIC = 2; | |
88 | |
89 @CompilationFinal private byte cachedValue; | |
90 @CompilationFinal private byte state = 0; | |
91 | |
92 @Override | |
93 public byte profile(byte value) { | |
94 byte localState = this.state; | |
95 if (localState != GENERIC) { | |
96 if (localState == SPECIALIZED) { | |
97 byte v = cachedValue; | |
98 if (v == value) { | |
99 return v; | |
100 } | |
101 } | |
102 CompilerDirectives.transferToInterpreterAndInvalidate(); | |
103 if (localState == UNINITIALIZED) { | |
104 this.cachedValue = value; | |
105 this.state = SPECIALIZED; | |
106 } else { | |
107 this.state = GENERIC; | |
108 } | |
109 } | |
110 return value; | |
111 } | |
112 | |
113 boolean isGeneric() { | |
114 return state == GENERIC; | |
115 } | |
116 | |
117 boolean isUninitialized() { | |
118 return state == UNINITIALIZED; | |
119 } | |
120 | |
121 byte getCachedValue() { | |
122 return cachedValue; | |
123 } | |
124 | |
125 @Override | |
126 public String toString() { | |
127 return toString(ByteValueProfile.class, state == UNINITIALIZED, state == GENERIC, // | |
128 String.format("value == (byte)%s", cachedValue)); | |
129 } | |
130 | |
131 /* Needed for lazy class loading. */ | |
132 static ByteValueProfile create() { | |
133 return new Enabled(); | |
134 } | |
135 } | |
136 | |
137 static final class Disabled extends ByteValueProfile { | |
138 | |
139 static final ByteValueProfile INSTANCE = new Disabled(); | |
140 | |
141 @Override | |
142 protected Object clone() { | |
143 return INSTANCE; | |
144 } | |
145 | |
146 @Override | |
147 public byte profile(byte value) { | |
148 return value; | |
149 } | |
150 | |
151 @Override | |
152 public String toString() { | |
153 return toStringDisabled(ByteValueProfile.class); | |
154 } | |
155 | |
156 } | |
157 | |
158 } |