1 | /* |
2 | * Copyright (C) 2011-2013, 2016 Apple Inc. All rights reserved. |
3 | * |
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. |
13 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
14 | * 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 | |
29 | #pragma once |
30 | |
31 | #include "ConcurrentJSLock.h" |
32 | #include "SpeculatedType.h" |
33 | #include "Structure.h" |
34 | #include <wtf/PrintStream.h> |
35 | #include <wtf/StringPrintStream.h> |
36 | |
37 | namespace JSC { |
38 | |
39 | template<unsigned numberOfBucketsArgument> |
40 | struct ValueProfileBase { |
41 | static const unsigned numberOfBuckets = numberOfBucketsArgument; |
42 | static const unsigned numberOfSpecFailBuckets = 1; |
43 | static const unsigned bucketIndexMask = numberOfBuckets - 1; |
44 | static const unsigned totalNumberOfBuckets = numberOfBuckets + numberOfSpecFailBuckets; |
45 | |
46 | ValueProfileBase() |
47 | : m_bytecodeOffset(-1) |
48 | { |
49 | for (unsigned i = 0; i < totalNumberOfBuckets; ++i) |
50 | m_buckets[i] = JSValue::encode(JSValue()); |
51 | } |
52 | |
53 | ValueProfileBase(int bytecodeOffset) |
54 | : m_bytecodeOffset(bytecodeOffset) |
55 | { |
56 | for (unsigned i = 0; i < totalNumberOfBuckets; ++i) |
57 | m_buckets[i] = JSValue::encode(JSValue()); |
58 | } |
59 | |
60 | EncodedJSValue* specFailBucket(unsigned i) |
61 | { |
62 | ASSERT(numberOfBuckets + i < totalNumberOfBuckets); |
63 | return m_buckets + numberOfBuckets + i; |
64 | } |
65 | |
66 | const ClassInfo* classInfo(unsigned bucket) const |
67 | { |
68 | JSValue value = JSValue::decode(m_buckets[bucket]); |
69 | if (!!value) { |
70 | if (!value.isCell()) |
71 | return 0; |
72 | return value.asCell()->structure()->classInfo(); |
73 | } |
74 | return 0; |
75 | } |
76 | |
77 | unsigned numberOfSamples() const |
78 | { |
79 | unsigned result = 0; |
80 | for (unsigned i = 0; i < totalNumberOfBuckets; ++i) { |
81 | if (!!JSValue::decode(m_buckets[i])) |
82 | result++; |
83 | } |
84 | return result; |
85 | } |
86 | |
87 | unsigned totalNumberOfSamples() const |
88 | { |
89 | return numberOfSamples() + m_numberOfSamplesInPrediction; |
90 | } |
91 | |
92 | bool isLive() const |
93 | { |
94 | for (unsigned i = 0; i < totalNumberOfBuckets; ++i) { |
95 | if (!!JSValue::decode(m_buckets[i])) |
96 | return true; |
97 | } |
98 | return false; |
99 | } |
100 | |
101 | CString briefDescription(const ConcurrentJSLocker& locker) |
102 | { |
103 | computeUpdatedPrediction(locker); |
104 | |
105 | StringPrintStream out; |
106 | out.print("predicting " , SpeculationDump(m_prediction)); |
107 | return out.toCString(); |
108 | } |
109 | |
110 | void dump(PrintStream& out) |
111 | { |
112 | out.print("samples = " , totalNumberOfSamples(), " prediction = " , SpeculationDump(m_prediction)); |
113 | bool first = true; |
114 | for (unsigned i = 0; i < totalNumberOfBuckets; ++i) { |
115 | JSValue value = JSValue::decode(m_buckets[i]); |
116 | if (!!value) { |
117 | if (first) { |
118 | out.printf(": " ); |
119 | first = false; |
120 | } else |
121 | out.printf(", " ); |
122 | out.print(value); |
123 | } |
124 | } |
125 | } |
126 | |
127 | // Updates the prediction and returns the new one. Never call this from any thread |
128 | // that isn't executing the code. |
129 | SpeculatedType computeUpdatedPrediction(const ConcurrentJSLocker&) |
130 | { |
131 | for (unsigned i = 0; i < totalNumberOfBuckets; ++i) { |
132 | JSValue value = JSValue::decode(m_buckets[i]); |
133 | if (!value) |
134 | continue; |
135 | |
136 | m_numberOfSamplesInPrediction++; |
137 | mergeSpeculation(m_prediction, speculationFromValue(value)); |
138 | |
139 | m_buckets[i] = JSValue::encode(JSValue()); |
140 | } |
141 | |
142 | return m_prediction; |
143 | } |
144 | |
145 | int m_bytecodeOffset; // -1 for prologue |
146 | unsigned m_numberOfSamplesInPrediction { 0 }; |
147 | |
148 | SpeculatedType m_prediction { SpecNone }; |
149 | |
150 | EncodedJSValue m_buckets[totalNumberOfBuckets]; |
151 | }; |
152 | |
153 | struct MinimalValueProfile : public ValueProfileBase<0> { |
154 | MinimalValueProfile(): ValueProfileBase<0>() { } |
155 | MinimalValueProfile(int bytecodeOffset): ValueProfileBase<0>(bytecodeOffset) { } |
156 | }; |
157 | |
158 | template<unsigned logNumberOfBucketsArgument> |
159 | struct ValueProfileWithLogNumberOfBuckets : public ValueProfileBase<1 << logNumberOfBucketsArgument> { |
160 | static const unsigned logNumberOfBuckets = logNumberOfBucketsArgument; |
161 | |
162 | ValueProfileWithLogNumberOfBuckets() |
163 | : ValueProfileBase<1 << logNumberOfBucketsArgument>() |
164 | { |
165 | } |
166 | ValueProfileWithLogNumberOfBuckets(int bytecodeOffset) |
167 | : ValueProfileBase<1 << logNumberOfBucketsArgument>(bytecodeOffset) |
168 | { |
169 | } |
170 | }; |
171 | |
172 | struct ValueProfile : public ValueProfileWithLogNumberOfBuckets<0> { |
173 | ValueProfile() : ValueProfileWithLogNumberOfBuckets<0>() { } |
174 | ValueProfile(int bytecodeOffset) : ValueProfileWithLogNumberOfBuckets<0>(bytecodeOffset) { } |
175 | }; |
176 | |
177 | template<typename T> |
178 | inline int getValueProfileBytecodeOffset(T* valueProfile) |
179 | { |
180 | return valueProfile->m_bytecodeOffset; |
181 | } |
182 | |
183 | // This is a mini value profile to catch pathologies. It is a counter that gets |
184 | // incremented when we take the slow path on any instruction. |
185 | struct RareCaseProfile { |
186 | RareCaseProfile(int bytecodeOffset) |
187 | : m_bytecodeOffset(bytecodeOffset) |
188 | , m_counter(0) |
189 | { |
190 | } |
191 | |
192 | int m_bytecodeOffset; |
193 | uint32_t m_counter; |
194 | }; |
195 | |
196 | inline int getRareCaseProfileBytecodeOffset(RareCaseProfile* rareCaseProfile) |
197 | { |
198 | return rareCaseProfile->m_bytecodeOffset; |
199 | } |
200 | |
201 | struct ValueProfileAndOperand { |
202 | ValueProfile m_profile; |
203 | int m_operand; |
204 | }; |
205 | |
206 | struct ValueProfileAndOperandBuffer { |
207 | ValueProfileAndOperandBuffer(unsigned size) |
208 | : m_size(size) |
209 | { |
210 | // FIXME: ValueProfile has more stuff than we need. We could optimize these value profiles |
211 | // to be more space efficient. |
212 | // https://bugs.webkit.org/show_bug.cgi?id=175413 |
213 | m_buffer = MallocPtr<ValueProfileAndOperand>::malloc(m_size * sizeof(ValueProfileAndOperand)); |
214 | for (unsigned i = 0; i < m_size; ++i) |
215 | new (&m_buffer.get()[i]) ValueProfileAndOperand(); |
216 | } |
217 | |
218 | ~ValueProfileAndOperandBuffer() |
219 | { |
220 | for (unsigned i = 0; i < m_size; ++i) |
221 | m_buffer.get()[i].~ValueProfileAndOperand(); |
222 | } |
223 | |
224 | template <typename Function> |
225 | void forEach(Function function) |
226 | { |
227 | for (unsigned i = 0; i < m_size; ++i) |
228 | function(m_buffer.get()[i]); |
229 | } |
230 | |
231 | unsigned m_size; |
232 | MallocPtr<ValueProfileAndOperand> m_buffer; |
233 | }; |
234 | |
235 | } // namespace JSC |
236 | |