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