annotate truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/PrimitiveValueProfile.java @ 22157:dc83cc1f94f2

Using fully qualified imports
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 16 Sep 2015 11:33:22 +0200
parents 329fe954f6f2
children a63bda98cfdb
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
22157
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22123
diff changeset
27 import com.oracle.truffle.api.CompilerDirectives;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
28 import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
22157
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22123
diff changeset
29 import java.util.Objects;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
30
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 * 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
33 * values.
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
34 * <p>
18769
144fba40c979 Truffle: typo in documentation of PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents: 18164
diff changeset
35 * 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
36 * {@link Float#floatToRawIntBits} and {@link Double#doubleToRawLongBits}, so that for example
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
37 * {@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
38 * normally say that it was.
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
39 */
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
40 public class PrimitiveValueProfile extends ValueProfile {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
41
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
42 private static final Object UNINITIALIZED = new Object();
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
43 private static final Object GENERIC = new Object();
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
44
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
45 @CompilationFinal private Object cachedValue = UNINITIALIZED;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
46
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
47 PrimitiveValueProfile() {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
48 }
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 @SuppressWarnings("unchecked")
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
51 @Override
22123
329fe954f6f2 Can compile Truffle API with following javac lints: -Xlint:all,-auxiliaryclass,-try,-processing
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
52 public <T> T profile(T v) {
329fe954f6f2 Can compile Truffle API with following javac lints: -Xlint:all,-auxiliaryclass,-try,-processing
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
53 Object value = v;
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) {
20076
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
56 if (snapshot instanceof Byte) {
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
57 if (value instanceof Byte && (byte) snapshot == (byte) value) {
22123
329fe954f6f2 Can compile Truffle API with following javac lints: -Xlint:all,-auxiliaryclass,-try,-processing
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
58 return (T) snapshot;
20076
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
59 }
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
60 } else if (snapshot instanceof Short) {
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
61 if (value instanceof Short && (short) snapshot == (short) value) {
22123
329fe954f6f2 Can compile Truffle API with following javac lints: -Xlint:all,-auxiliaryclass,-try,-processing
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
62 return (T) snapshot;
20076
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
63 }
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
64 } else if (snapshot instanceof Integer) {
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
65 if (value instanceof Integer && (int) snapshot == (int) value) {
22123
329fe954f6f2 Can compile Truffle API with following javac lints: -Xlint:all,-auxiliaryclass,-try,-processing
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
66 return (T) snapshot;
20076
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
67 }
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
68 } else if (snapshot instanceof Long) {
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
69 if (value instanceof Long && (long) snapshot == (long) value) {
22123
329fe954f6f2 Can compile Truffle API with following javac lints: -Xlint:all,-auxiliaryclass,-try,-processing
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
70 return (T) snapshot;
20076
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
71 }
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
72 } else if (snapshot instanceof Float) {
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
73 if (value instanceof Float && exactCompare((float) snapshot, (float) value)) {
22123
329fe954f6f2 Can compile Truffle API with following javac lints: -Xlint:all,-auxiliaryclass,-try,-processing
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
74 return (T) snapshot;
20076
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
75 }
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
76 } else if (snapshot instanceof Double) {
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
77 if (value instanceof Double && exactCompare((double) snapshot, (double) value)) {
22123
329fe954f6f2 Can compile Truffle API with following javac lints: -Xlint:all,-auxiliaryclass,-try,-processing
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
78 return (T) snapshot;
20076
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
79 }
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
80 } else if (snapshot instanceof Boolean) {
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
81 if (value instanceof Boolean && (boolean) snapshot == (boolean) value) {
22123
329fe954f6f2 Can compile Truffle API with following javac lints: -Xlint:all,-auxiliaryclass,-try,-processing
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
82 return (T) snapshot;
20076
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
83 }
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
84 } else if (snapshot instanceof Character) {
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
85 if (value instanceof Character && (char) snapshot == (char) value) {
22123
329fe954f6f2 Can compile Truffle API with following javac lints: -Xlint:all,-auxiliaryclass,-try,-processing
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
86 return (T) snapshot;
20076
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
87 }
19592
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
88 } else if (snapshot == value) {
22123
329fe954f6f2 Can compile Truffle API with following javac lints: -Xlint:all,-auxiliaryclass,-try,-processing
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
89 return (T) snapshot;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
90 }
20076
126ab00f859c refactor PrimitiveValueProfile to omit object equality comparison in the profiled case
Lukas Stadler <lukas.stadler@oracle.com>
parents: 19701
diff changeset
91 cacheMiss(value);
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
92 }
22123
329fe954f6f2 Can compile Truffle API with following javac lints: -Xlint:all,-auxiliaryclass,-try,-processing
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
93 return (T) value;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
94 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
95
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
96 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
97 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
98 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
99 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
100 return (byte) snapshot;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
101 } else {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
102 cacheMiss(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 return value;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
106 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
107
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
108 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
109 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
110 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
111 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
112 return (short) snapshot;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
113 } else {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
114 cacheMiss(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 return value;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
118 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
119
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
120 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
121 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
122 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
123 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
124 return (int) snapshot;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
125 } else {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
126 cacheMiss(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 return value;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
130 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
131
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
132 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
133 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
134 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
135 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
136 return (long) snapshot;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
137 } else {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
138 cacheMiss(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 return value;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
142 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
143
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
144 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
145 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
146 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
147 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
148 return (float) snapshot;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
149 } else {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
150 cacheMiss(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 return value;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
154 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
155
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
156 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
157 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
158 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
159 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
160 return (double) snapshot;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
161 } else {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
162 cacheMiss(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 return value;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
166 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
167
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
168 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
169 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
170 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
171 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
172 return (boolean) snapshot;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
173 } else {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
174 cacheMiss(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 return value;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
178 }
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 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
181 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
182 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
183 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
184 return (char) snapshot;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
185 } else {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
186 cacheMiss(value);
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 return value;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
190 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
191
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
192 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
193 return cachedValue == GENERIC;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
194 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
195
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
196 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
197 return cachedValue == UNINITIALIZED;
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
198 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
199
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
200 public Object getCachedValue() {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
201 return cachedValue;
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 @Override
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
205 public String toString() {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
206 return String.format("%s(%s)@%x", getClass().getSimpleName(), formatValue(), hashCode());
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
207 }
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 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
210 // TODO should we try to handle this more atomically?
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
211 CompilerDirectives.transferToInterpreterAndInvalidate();
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
212 if (cachedValue == UNINITIALIZED) {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
213 cachedValue = value;
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
214 } else {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
215 cachedValue = GENERIC;
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 }
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 public static boolean exactCompare(float a, float b) {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
220 /*
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
221 * -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
222 * differentiate.
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
223 */
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
224 return Float.floatToRawIntBits(a) == Float.floatToRawIntBits(b);
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
225 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
226
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
227 public static boolean exactCompare(double a, double b) {
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
228 /*
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
229 * -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
230 * differentiate.
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
231 */
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
232 return Double.doubleToRawLongBits(a) == Double.doubleToRawLongBits(b);
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 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
236 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
237 if (snapshot == null) {
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
238 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
239 } else if (snapshot == UNINITIALIZED) {
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
240 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
241 } else if (snapshot == GENERIC) {
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
242 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
243 } 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
244 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
245 return String.format("%s=%s", snapshot.getClass().getSimpleName(), snapshot);
18164
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
246 } else {
19592
bc3cd000d9c8 PrimitiveValueProfile: read from a snapshot to avoid races with multiple reads.
Benoit Daloze <benoit.daloze@jku.at>
parents: 18769
diff changeset
247 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
248 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
249 }
53afdc71b311 Truffle: added PrimitiveValueProfile.
Chris Seaton <chris.seaton@oracle.com>
parents:
diff changeset
250 }