comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/IdentityValueProfile.java @ 17039:1e542561783e

Truffle: new value profiles prototype.
author Christian Humer <christian.humer@gmail.com>
date Wed, 03 Sep 2014 14:58:53 +0200
parents
children
comparison
equal deleted inserted replaced
17038:3b3e768a2b92 17039:1e542561783e
1 /*
2 * Copyright (c) 2014, 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.utilities;
26
27 import java.util.*;
28
29 import com.oracle.truffle.api.*;
30 import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
31
32 /**
33 * Represents a {@link ValueProfile} that speculates on the object identity of a value.
34 */
35 public final class IdentityValueProfile extends ValueProfile {
36
37 private static final Object UNINITIALIZED = new Object();
38 private static final Object GENERIC = new Object();
39
40 @CompilationFinal protected Object cachedValue = UNINITIALIZED;
41
42 IdentityValueProfile() {
43 }
44
45 @Override
46 @SuppressWarnings("unchecked")
47 public <T> T profile(T value) {
48 if (cachedValue != GENERIC) {
49 if (cachedValue == value) {
50 return (T) cachedValue;
51 } else {
52 CompilerDirectives.transferToInterpreterAndInvalidate();
53 if (cachedValue == UNINITIALIZED) {
54 cachedValue = value;
55 } else {
56 cachedValue = GENERIC;
57 }
58 }
59 }
60 return value;
61 }
62
63 public boolean isGeneric() {
64 return getCachedValue() == GENERIC;
65 }
66
67 public boolean isUninitialized() {
68 return getCachedValue() == UNINITIALIZED;
69 }
70
71 public Object getCachedValue() {
72 return cachedValue;
73 }
74
75 @Override
76 public String toString() {
77 return String.format("%s(%s)@%x", getClass().getSimpleName(), isUninitialized() ? "uninitialized" : (isGeneric() ? "generic" : String.format("@%x", Objects.hash(cachedValue))), hashCode());
78 }
79
80 }