blob: 8724eb47d2953807a2cea1ffa2a23787c3090e21 [file] [log] [blame]
fpizlo@apple.com95a9f0d2011-08-20 02:17:49 +00001/*
fpizlo@apple.comacdb63e2016-05-10 02:01:28 +00002 * Copyright (C) 2011-2013, 2016 Apple Inc. All rights reserved.
fpizlo@apple.com95a9f0d2011-08-20 02:17:49 +00003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
mjs@apple.com92047332014-03-15 04:08:27 +000013 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
fpizlo@apple.com95a9f0d2011-08-20 02:17:49 +000014 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
ryanhaddad@apple.com22104f52016-09-28 17:08:17 +000029#pragma once
fpizlo@apple.com95a9f0d2011-08-20 02:17:49 +000030
fpizlo@apple.com171d06f2016-11-15 23:21:50 +000031#include "ConcurrentJSLock.h"
fpizlo@apple.com016fd682012-05-25 20:19:55 +000032#include "Heap.h"
fpizlo@apple.comd13163d2011-09-03 05:14:04 +000033#include "JSArray.h"
fpizlo@apple.com62336162012-06-07 01:35:59 +000034#include "SpeculatedType.h"
fpizlo@apple.comd13163d2011-09-03 05:14:04 +000035#include "Structure.h"
fpizlo@apple.comacdb63e2016-05-10 02:01:28 +000036#include "TagRegistersMode.h"
fpizlo@apple.com95a9f0d2011-08-20 02:17:49 +000037#include "WriteBarrier.h"
fpizlo@apple.com8a8b45e2012-11-28 01:29:29 +000038#include <wtf/PrintStream.h>
fpizlo@apple.com77ef0e32012-12-12 00:21:43 +000039#include <wtf/StringPrintStream.h>
fpizlo@apple.com95a9f0d2011-08-20 02:17:49 +000040
41namespace JSC {
42
fpizlo@apple.com31659de2012-02-23 22:51:09 +000043template<unsigned numberOfBucketsArgument>
44struct ValueProfileBase {
45 static const unsigned numberOfBuckets = numberOfBucketsArgument;
fpizlo@apple.com49bfe572011-10-31 23:50:57 +000046 static const unsigned numberOfSpecFailBuckets = 1;
fpizlo@apple.com95a9f0d2011-08-20 02:17:49 +000047 static const unsigned bucketIndexMask = numberOfBuckets - 1;
fpizlo@apple.com49bfe572011-10-31 23:50:57 +000048 static const unsigned totalNumberOfBuckets = numberOfBuckets + numberOfSpecFailBuckets;
fpizlo@apple.com95a9f0d2011-08-20 02:17:49 +000049
fpizlo@apple.com31659de2012-02-23 22:51:09 +000050 ValueProfileBase()
fpizlo@apple.com086d2af2011-12-21 02:29:15 +000051 : m_bytecodeOffset(-1)
fpizlo@apple.com62336162012-06-07 01:35:59 +000052 , m_prediction(SpecNone)
fpizlo@apple.com086d2af2011-12-21 02:29:15 +000053 , m_numberOfSamplesInPrediction(0)
54 {
55 for (unsigned i = 0; i < totalNumberOfBuckets; ++i)
56 m_buckets[i] = JSValue::encode(JSValue());
57 }
58
fpizlo@apple.com31659de2012-02-23 22:51:09 +000059 ValueProfileBase(int bytecodeOffset)
fpizlo@apple.com7f2d2342011-09-14 23:00:26 +000060 : m_bytecodeOffset(bytecodeOffset)
fpizlo@apple.com62336162012-06-07 01:35:59 +000061 , m_prediction(SpecNone)
fpizlo@apple.com7f2d2342011-09-14 23:00:26 +000062 , m_numberOfSamplesInPrediction(0)
fpizlo@apple.com95a9f0d2011-08-20 02:17:49 +000063 {
fpizlo@apple.com49bfe572011-10-31 23:50:57 +000064 for (unsigned i = 0; i < totalNumberOfBuckets; ++i)
fpizlo@apple.com7f2d2342011-09-14 23:00:26 +000065 m_buckets[i] = JSValue::encode(JSValue());
fpizlo@apple.comd13163d2011-09-03 05:14:04 +000066 }
67
fpizlo@apple.com49bfe572011-10-31 23:50:57 +000068 EncodedJSValue* specFailBucket(unsigned i)
69 {
70 ASSERT(numberOfBuckets + i < totalNumberOfBuckets);
71 return m_buckets + numberOfBuckets + i;
72 }
73
fpizlo@apple.comd13163d2011-09-03 05:14:04 +000074 const ClassInfo* classInfo(unsigned bucket) const
75 {
commit-queue@webkit.org387d2ec2011-10-09 10:19:23 +000076 JSValue value = JSValue::decode(m_buckets[bucket]);
77 if (!!value) {
fpizlo@apple.comd13163d2011-09-03 05:14:04 +000078 if (!value.isCell())
79 return 0;
80 return value.asCell()->structure()->classInfo();
81 }
fpizlo@apple.comab9a92c2011-10-12 20:28:49 +000082 return 0;
fpizlo@apple.com95a9f0d2011-08-20 02:17:49 +000083 }
84
85 unsigned numberOfSamples() const
86 {
87 unsigned result = 0;
fpizlo@apple.com49bfe572011-10-31 23:50:57 +000088 for (unsigned i = 0; i < totalNumberOfBuckets; ++i) {
fpizlo@apple.comab9a92c2011-10-12 20:28:49 +000089 if (!!JSValue::decode(m_buckets[i]))
fpizlo@apple.com95a9f0d2011-08-20 02:17:49 +000090 result++;
91 }
92 return result;
93 }
94
fpizlo@apple.com7f2d2342011-09-14 23:00:26 +000095 unsigned totalNumberOfSamples() const
96 {
97 return numberOfSamples() + m_numberOfSamplesInPrediction;
98 }
99
100 bool isLive() const
101 {
fpizlo@apple.com49bfe572011-10-31 23:50:57 +0000102 for (unsigned i = 0; i < totalNumberOfBuckets; ++i) {
fpizlo@apple.comab9a92c2011-10-12 20:28:49 +0000103 if (!!JSValue::decode(m_buckets[i]))
fpizlo@apple.com7f2d2342011-09-14 23:00:26 +0000104 return true;
105 }
106 return false;
107 }
108
fpizlo@apple.com171d06f2016-11-15 23:21:50 +0000109 CString briefDescription(const ConcurrentJSLocker& locker)
fpizlo@apple.com77ef0e32012-12-12 00:21:43 +0000110 {
oliver@apple.comc14eb7d2013-07-25 03:58:47 +0000111 computeUpdatedPrediction(locker);
fpizlo@apple.com77ef0e32012-12-12 00:21:43 +0000112
113 StringPrintStream out;
mhahnenberg@apple.comc5684712013-09-26 17:16:47 +0000114 out.print("predicting ", SpeculationDump(m_prediction));
fpizlo@apple.com77ef0e32012-12-12 00:21:43 +0000115 return out.toCString();
116 }
117
fpizlo@apple.com8a8b45e2012-11-28 01:29:29 +0000118 void dump(PrintStream& out)
fpizlo@apple.com594887a2011-09-06 09:23:55 +0000119 {
fpizlo@apple.com02e35632012-11-29 06:01:40 +0000120 out.print("samples = ", totalNumberOfSamples(), " prediction = ", SpeculationDump(m_prediction));
fpizlo@apple.com594887a2011-09-06 09:23:55 +0000121 bool first = true;
fpizlo@apple.com49bfe572011-10-31 23:50:57 +0000122 for (unsigned i = 0; i < totalNumberOfBuckets; ++i) {
commit-queue@webkit.org387d2ec2011-10-09 10:19:23 +0000123 JSValue value = JSValue::decode(m_buckets[i]);
fpizlo@apple.comab9a92c2011-10-12 20:28:49 +0000124 if (!!value) {
fpizlo@apple.com594887a2011-09-06 09:23:55 +0000125 if (first) {
fpizlo@apple.com8a8b45e2012-11-28 01:29:29 +0000126 out.printf(": ");
fpizlo@apple.com594887a2011-09-06 09:23:55 +0000127 first = false;
128 } else
fpizlo@apple.com8a8b45e2012-11-28 01:29:29 +0000129 out.printf(", ");
fpizlo@apple.com9fe49132012-12-04 19:29:13 +0000130 out.print(value);
fpizlo@apple.comab9a92c2011-10-12 20:28:49 +0000131 }
fpizlo@apple.com594887a2011-09-06 09:23:55 +0000132 }
133 }
fpizlo@apple.com594887a2011-09-06 09:23:55 +0000134
oliver@apple.comc14eb7d2013-07-25 03:58:47 +0000135 // Updates the prediction and returns the new one. Never call this from any thread
136 // that isn't executing the code.
fpizlo@apple.com171d06f2016-11-15 23:21:50 +0000137 SpeculatedType computeUpdatedPrediction(const ConcurrentJSLocker&)
fpizlo@apple.com31659de2012-02-23 22:51:09 +0000138 {
139 for (unsigned i = 0; i < totalNumberOfBuckets; ++i) {
140 JSValue value = JSValue::decode(m_buckets[i]);
141 if (!value)
142 continue;
143
144 m_numberOfSamplesInPrediction++;
fpizlo@apple.com62336162012-06-07 01:35:59 +0000145 mergeSpeculation(m_prediction, speculationFromValue(value));
fpizlo@apple.com31659de2012-02-23 22:51:09 +0000146
147 m_buckets[i] = JSValue::encode(JSValue());
148 }
149
150 return m_prediction;
151 }
fpizlo@apple.com7f2d2342011-09-14 23:00:26 +0000152
153 int m_bytecodeOffset; // -1 for prologue
154
fpizlo@apple.com62336162012-06-07 01:35:59 +0000155 SpeculatedType m_prediction;
fpizlo@apple.com7f2d2342011-09-14 23:00:26 +0000156 unsigned m_numberOfSamplesInPrediction;
157
fpizlo@apple.com49bfe572011-10-31 23:50:57 +0000158 EncodedJSValue m_buckets[totalNumberOfBuckets];
fpizlo@apple.com95a9f0d2011-08-20 02:17:49 +0000159};
160
fpizlo@apple.com31659de2012-02-23 22:51:09 +0000161struct MinimalValueProfile : public ValueProfileBase<0> {
162 MinimalValueProfile(): ValueProfileBase<0>() { }
163 MinimalValueProfile(int bytecodeOffset): ValueProfileBase<0>(bytecodeOffset) { }
164};
165
166template<unsigned logNumberOfBucketsArgument>
167struct ValueProfileWithLogNumberOfBuckets : public ValueProfileBase<1 << logNumberOfBucketsArgument> {
168 static const unsigned logNumberOfBuckets = logNumberOfBucketsArgument;
169
170 ValueProfileWithLogNumberOfBuckets()
171 : ValueProfileBase<1 << logNumberOfBucketsArgument>()
172 {
173 }
174 ValueProfileWithLogNumberOfBuckets(int bytecodeOffset)
175 : ValueProfileBase<1 << logNumberOfBucketsArgument>(bytecodeOffset)
176 {
177 }
178};
179
180struct ValueProfile : public ValueProfileWithLogNumberOfBuckets<0> {
181 ValueProfile(): ValueProfileWithLogNumberOfBuckets<0>() { }
182 ValueProfile(int bytecodeOffset): ValueProfileWithLogNumberOfBuckets<0>(bytecodeOffset) { }
183};
184
185template<typename T>
186inline int getValueProfileBytecodeOffset(T* valueProfile)
fpizlo@apple.com95a9f0d2011-08-20 02:17:49 +0000187{
fpizlo@apple.com7f2d2342011-09-14 23:00:26 +0000188 return valueProfile->m_bytecodeOffset;
fpizlo@apple.com95a9f0d2011-08-20 02:17:49 +0000189}
fpizlo@apple.com9b0b31e2011-09-19 22:27:38 +0000190
191// This is a mini value profile to catch pathologies. It is a counter that gets
192// incremented when we take the slow path on any instruction.
193struct RareCaseProfile {
194 RareCaseProfile(int bytecodeOffset)
195 : m_bytecodeOffset(bytecodeOffset)
196 , m_counter(0)
197 {
198 }
199
200 int m_bytecodeOffset;
201 uint32_t m_counter;
202};
203
204inline int getRareCaseProfileBytecodeOffset(RareCaseProfile* rareCaseProfile)
205{
206 return rareCaseProfile->m_bytecodeOffset;
207}
fpizlo@apple.com95a9f0d2011-08-20 02:17:49 +0000208
fpizlo@apple.com31659de2012-02-23 22:51:09 +0000209} // namespace JSC