annotate graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/PrimitiveValueProfile.java @ 19701:6755624bf03d

PrimitiveValueProfile: directly use the field inside the class to highlight its usages.
author Benoit Daloze <benoit.daloze@jku.at>
date Wed, 25 Feb 2015 18:31:23 +0100
parents bc3cd000d9c8
children 126ab00f859c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
1 /*
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
4 *
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation. Oracle designates this
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
8 * particular file as subject to the "Classpath" exception as provided
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
9 * by Oracle in the LICENSE file that accompanied this code.
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
10 *
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
11 * This code is distributed in the hope that it will be useful, but WITHOUT
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
14 * version 2 for more details (a copy is included in the LICENSE file that
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
15 * accompanied this code).
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
16 *
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
17 * You should have received a copy of the GNU General Public License version
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
18 * 2 along with this work; if not, write to the Free Software Foundation,
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
20 *
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
22 * or visit www.oracle.com if you need additional information or have any
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
23 * questions.
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
24 */
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
25 package com.oracle.truffle.api.utilities;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
26
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
27 import java.util.*;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
28
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
29 import com.oracle.truffle.api.*;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
30 import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
31
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
32 /**
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
33 * Represents a {@link ValueProfile} that speculates on the primitive equality or object identity of
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
34 * values.
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
35 * <p>
18769
144fba40c979 Truffle: typo in documentation of PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents: 18164
diff changeset
36 * Note that for {@code float} and {@code double} values we compare primitive equality via
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
37 * {@link Float#floatToRawIntBits} and {@link Double#doubleToRawLongBits}, so that for example
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
38 * {@code -0.0} is not considered the same as {@code 0.0}, even though primitive equality would
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
39 * normally say that it was.
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
40 */
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
41 public class PrimitiveValueProfile extends ValueProfile {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
42
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
43 private static final Object UNINITIALIZED = new Object();
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
44 private static final Object GENERIC = new Object();
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
45
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
46 @CompilationFinal private Object cachedValue = UNINITIALIZED;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
47
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
48 PrimitiveValueProfile() {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
49 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
50
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
51 @SuppressWarnings("unchecked")
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
52 @Override
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
53 public Object profile(Object value) {
19592
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
54 Object snapshot = this.cachedValue;
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
55 if (snapshot != GENERIC) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
56 if (snapshot instanceof Byte && value instanceof Byte && (byte) snapshot == (byte) value) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
57 return snapshot;
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
58 } else if (snapshot instanceof Short && value instanceof Short && (short) snapshot == (short) value) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
59 return snapshot;
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
60 } else if (snapshot instanceof Integer && value instanceof Integer && (int) snapshot == (int) value) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
61 return snapshot;
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
62 } else if (snapshot instanceof Long && value instanceof Long && (long) snapshot == (long) value) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
63 return snapshot;
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
64 } else if (snapshot instanceof Float && value instanceof Float && exactCompare((float) snapshot, (float) value)) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
65 return snapshot;
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
66 } else if (snapshot instanceof Double && value instanceof Double && exactCompare((double) snapshot, (double) value)) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
67 return snapshot;
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
68 } else if (snapshot instanceof Boolean && value instanceof Boolean && (boolean) snapshot == (boolean) value) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
69 return snapshot;
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
70 } else if (snapshot instanceof Character && value instanceof Character && (char) snapshot == (char) value) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
71 return snapshot;
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
72 } else if (snapshot == value) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
73 return snapshot;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
74 } else {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
75 cacheMiss(value);
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
76 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
77 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
78 return value;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
79 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
80
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
81 public byte profile(byte value) {
19592
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
82 Object snapshot = this.cachedValue;
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
83 if (snapshot != GENERIC) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
84 if (snapshot instanceof Byte && (byte) snapshot == value) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
85 return (byte) snapshot;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
86 } else {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
87 cacheMiss(value);
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
88 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
89 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
90 return value;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
91 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
92
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
93 public short profile(short value) {
19592
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
94 Object snapshot = this.cachedValue;
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
95 if (snapshot != GENERIC) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
96 if (snapshot instanceof Short && (short) snapshot == value) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
97 return (short) snapshot;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
98 } else {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
99 cacheMiss(value);
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
100 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
101 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
102 return value;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
103 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
104
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
105 public int profile(int value) {
19592
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
106 Object snapshot = this.cachedValue;
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
107 if (snapshot != GENERIC) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
108 if (snapshot instanceof Integer && (int) snapshot == value) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
109 return (int) snapshot;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
110 } else {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
111 cacheMiss(value);
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
112 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
113 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
114 return value;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
115 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
116
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
117 public long profile(long value) {
19592
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
118 Object snapshot = this.cachedValue;
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
119 if (snapshot != GENERIC) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
120 if (snapshot instanceof Long && (long) snapshot == value) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
121 return (long) snapshot;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
122 } else {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
123 cacheMiss(value);
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
124 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
125 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
126 return value;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
127 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
128
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
129 public float profile(float value) {
19592
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
130 Object snapshot = this.cachedValue;
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
131 if (snapshot != GENERIC) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
132 if (snapshot instanceof Float && exactCompare((float) snapshot, value)) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
133 return (float) snapshot;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
134 } else {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
135 cacheMiss(value);
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
136 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
137 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
138 return value;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
139 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
140
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
141 public double profile(double value) {
19592
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
142 Object snapshot = this.cachedValue;
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
143 if (snapshot != GENERIC) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
144 if (snapshot instanceof Double && exactCompare((double) snapshot, value)) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
145 return (double) snapshot;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
146 } else {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
147 cacheMiss(value);
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
148 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
149 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
150 return value;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
151 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
152
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
153 public boolean profile(boolean value) {
19592
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
154 Object snapshot = this.cachedValue;
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
155 if (snapshot != GENERIC) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
156 if (snapshot instanceof Boolean && (boolean) snapshot == value) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
157 return (boolean) snapshot;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
158 } else {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
159 cacheMiss(value);
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
160 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
161 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
162 return value;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
163 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
165 public char profile(char value) {
19592
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
166 Object snapshot = this.cachedValue;
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
167 if (snapshot != GENERIC) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
168 if (snapshot instanceof Character && (char) snapshot == value) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
169 return (char) snapshot;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
170 } else {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
171 cacheMiss(value);
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
172 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
173 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
174 return value;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
175 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
176
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
177 public boolean isGeneric() {
19701
6755624bf03d PrimitiveValueProfile: directly use the field inside the class to highlight its usages.
Benoit Daloze <benoit.daloze@jku.at>
parents: 19592
diff changeset
178 return cachedValue == GENERIC;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
179 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
180
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
181 public boolean isUninitialized() {
19701
6755624bf03d PrimitiveValueProfile: directly use the field inside the class to highlight its usages.
Benoit Daloze <benoit.daloze@jku.at>
parents: 19592
diff changeset
182 return cachedValue == UNINITIALIZED;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
183 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
184
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
185 public Object getCachedValue() {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
186 return cachedValue;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
187 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
188
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
189 @Override
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
190 public String toString() {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
191 return String.format("%s(%s)@%x", getClass().getSimpleName(), formatValue(), hashCode());
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
192 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
193
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
194 private void cacheMiss(Object value) {
19592
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
195 // TODO should we try to handle this more atomically?
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
196 CompilerDirectives.transferToInterpreterAndInvalidate();
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
197 if (cachedValue == UNINITIALIZED) {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
198 cachedValue = value;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
199 } else {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
200 cachedValue = GENERIC;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
201 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
202 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
203
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
204 public static boolean exactCompare(float a, float b) {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
205 /*
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
206 * -0.0 == 0.0, but you can tell the difference through other means, so we need to
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
207 * differentiate.
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
208 */
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
209 return Float.floatToRawIntBits(a) == Float.floatToRawIntBits(b);
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
210 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
211
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
212 public static boolean exactCompare(double a, double b) {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
213 /*
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
214 * -0.0 == 0.0, but you can tell the difference through other means, so we need to
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
215 * differentiate.
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
216 */
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
217 return Double.doubleToRawLongBits(a) == Double.doubleToRawLongBits(b);
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
218 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
219
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
220 private String formatValue() {
19592
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
221 Object snapshot = this.cachedValue;
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
222 if (snapshot == null) {
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
223 return "null";
19592
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
224 } else if (snapshot == UNINITIALIZED) {
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
225 return "uninitialized";
19592
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
226 } else if (snapshot == GENERIC) {
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
227 return "generic";
19592
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
228 } else if (snapshot instanceof Byte || snapshot instanceof Short || snapshot instanceof Integer || snapshot instanceof Long || snapshot instanceof Float || snapshot instanceof Double ||
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
229 snapshot instanceof Boolean || snapshot instanceof Character) {
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
230 return String.format("%s=%s", snapshot.getClass().getSimpleName(), snapshot);
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
231 } else {
19592
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
232 return String.format("%s@%x", snapshot.getClass().getSimpleName(), Objects.hash(snapshot));
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
233 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
234 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
235 }