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