blob: 8719439ad4616aee939cdf02446a9a38b794b63c [file] [log] [blame]
Steve Block6ded16b2010-05-10 14:33:55 +01001// Copyright 2010 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
Steve Block6ded16b2010-05-10 14:33:55 +010028#include "v8.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010029
30#include "ast.h"
31#include "compiler.h"
32#include "ic.h"
33#include "macro-assembler.h"
34#include "stub-cache.h"
Steve Block6ded16b2010-05-10 14:33:55 +010035#include "type-info.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010036
37#include "ic-inl.h"
Steve Block6ded16b2010-05-10 14:33:55 +010038#include "objects-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000039
40namespace v8 {
41namespace internal {
42
Steve Block6ded16b2010-05-10 14:33:55 +010043
44TypeInfo TypeInfo::TypeFromValue(Handle<Object> value) {
45 TypeInfo info;
46 if (value->IsSmi()) {
47 info = TypeInfo::Smi();
48 } else if (value->IsHeapNumber()) {
49 info = TypeInfo::IsInt32Double(HeapNumber::cast(*value)->value())
50 ? TypeInfo::Integer32()
51 : TypeInfo::Double();
52 } else if (value->IsString()) {
53 info = TypeInfo::String();
54 } else {
55 info = TypeInfo::Unknown();
56 }
57 return info;
58}
59
Steve Blocka7e24c12009-10-30 11:49:00 +000060
Ben Murdochb0fe1622011-05-05 13:52:32 +010061TypeFeedbackOracle::TypeFeedbackOracle(Handle<Code> code) {
62 Initialize(code);
63}
64
65
66void TypeFeedbackOracle::Initialize(Handle<Code> code) {
67 ASSERT(map_.is_null()); // Only initialize once.
68 map_ = Factory::NewJSObject(Top::object_function());
69 PopulateMap(code);
70}
71
72
73bool TypeFeedbackOracle::LoadIsMonomorphic(Property* expr) {
74 return IsMonomorphic(expr->position());
75}
76
77
78bool TypeFeedbackOracle:: StoreIsMonomorphic(Assignment* expr) {
79 return IsMonomorphic(expr->position());
80}
81
82
83bool TypeFeedbackOracle::CallIsMonomorphic(Call* expr) {
84 return IsMonomorphic(expr->position());
85}
86
87
88Handle<Map> TypeFeedbackOracle::LoadMonomorphicReceiverType(Property* expr) {
89 ASSERT(LoadIsMonomorphic(expr));
90 return Handle<Map>::cast(GetElement(map_, expr->position()));
91}
92
93
94Handle<Map> TypeFeedbackOracle::StoreMonomorphicReceiverType(Assignment* expr) {
95 ASSERT(StoreIsMonomorphic(expr));
96 return Handle<Map>::cast(GetElement(map_, expr->position()));
97}
98
99
100Handle<Map> TypeFeedbackOracle::CallMonomorphicReceiverType(Call* expr) {
101 ASSERT(CallIsMonomorphic(expr));
102 return Handle<Map>::cast(GetElement(map_, expr->position()));
103}
104
105
106ZoneMapList* TypeFeedbackOracle::LoadReceiverTypes(Property* expr,
107 Handle<String> name) {
108 Code::Flags flags = Code::ComputeMonomorphicFlags(Code::LOAD_IC, NORMAL);
109 return CollectReceiverTypes(expr->position(), name, flags);
110}
111
112
113ZoneMapList* TypeFeedbackOracle::StoreReceiverTypes(Assignment* expr,
114 Handle<String> name) {
115 Code::Flags flags = Code::ComputeMonomorphicFlags(Code::STORE_IC, NORMAL);
116 return CollectReceiverTypes(expr->position(), name, flags);
117}
118
119
120ZoneMapList* TypeFeedbackOracle::CallReceiverTypes(Call* expr,
121 Handle<String> name) {
122 int arity = expr->arguments()->length();
123 Code::Flags flags = Code::ComputeMonomorphicFlags(
124 Code::CALL_IC, NORMAL, OWN_MAP, NOT_IN_LOOP, arity);
125 return CollectReceiverTypes(expr->position(), name, flags);
126}
127
128
129bool TypeFeedbackOracle::LoadIsBuiltin(Property* expr, Builtins::Name id) {
130 Handle<Object> object = GetElement(map_, expr->position());
131 return *object == Builtins::builtin(id);
132}
133
134
135TypeInfo TypeFeedbackOracle::CompareType(CompareOperation* expr, Side side) {
136 Handle<Object> object = GetElement(map_, expr->position());
137 TypeInfo unknown = TypeInfo::Unknown();
138 if (!object->IsCode()) return unknown;
139 Handle<Code> code = Handle<Code>::cast(object);
140 if (!code->is_compare_ic_stub()) return unknown;
141
142 CompareIC::State state = static_cast<CompareIC::State>(code->compare_state());
143 switch (state) {
144 case CompareIC::UNINITIALIZED:
145 // Uninitialized means never executed.
146 // TODO(fschneider): Introduce a separate value for never-executed ICs.
147 return unknown;
148 case CompareIC::SMIS:
149 return TypeInfo::Smi();
150 case CompareIC::HEAP_NUMBERS:
151 return TypeInfo::Number();
152 case CompareIC::OBJECTS:
153 // TODO(kasperl): We really need a type for JS objects here.
154 return TypeInfo::NonPrimitive();
155 case CompareIC::GENERIC:
156 default:
157 return unknown;
158 }
159}
160
161
162TypeInfo TypeFeedbackOracle::BinaryType(BinaryOperation* expr, Side side) {
163 Handle<Object> object = GetElement(map_, expr->position());
164 TypeInfo unknown = TypeInfo::Unknown();
165 if (!object->IsCode()) return unknown;
166 Handle<Code> code = Handle<Code>::cast(object);
167 if (code->is_binary_op_stub()) {
168 BinaryOpIC::TypeInfo type = static_cast<BinaryOpIC::TypeInfo>(
169 code->binary_op_type());
170 switch (type) {
171 case BinaryOpIC::UNINIT_OR_SMI:
172 return TypeInfo::Smi();
173 case BinaryOpIC::DEFAULT:
174 return (expr->op() == Token::DIV || expr->op() == Token::MUL)
175 ? TypeInfo::Double()
176 : TypeInfo::Integer32();
177 case BinaryOpIC::HEAP_NUMBERS:
178 return TypeInfo::Double();
179 default:
180 return unknown;
181 }
182 } else if (code->is_type_recording_binary_op_stub()) {
183 TRBinaryOpIC::TypeInfo type = static_cast<TRBinaryOpIC::TypeInfo>(
184 code->type_recording_binary_op_type());
185 TRBinaryOpIC::TypeInfo result_type = static_cast<TRBinaryOpIC::TypeInfo>(
186 code->type_recording_binary_op_result_type());
187
188 switch (type) {
189 case TRBinaryOpIC::UNINITIALIZED:
190 // Uninitialized means never executed.
191 // TODO(fschneider): Introduce a separate value for never-executed ICs
192 return unknown;
193 case TRBinaryOpIC::SMI:
194 switch (result_type) {
195 case TRBinaryOpIC::UNINITIALIZED:
196 case TRBinaryOpIC::SMI:
197 return TypeInfo::Smi();
198 case TRBinaryOpIC::INT32:
199 return TypeInfo::Integer32();
200 case TRBinaryOpIC::HEAP_NUMBER:
201 return TypeInfo::Double();
202 default:
203 return unknown;
204 }
205 case TRBinaryOpIC::INT32:
206 if (expr->op() == Token::DIV ||
207 result_type == TRBinaryOpIC::HEAP_NUMBER) {
208 return TypeInfo::Double();
209 }
210 return TypeInfo::Integer32();
211 case TRBinaryOpIC::HEAP_NUMBER:
212 return TypeInfo::Double();
213 case TRBinaryOpIC::STRING:
214 case TRBinaryOpIC::GENERIC:
215 return unknown;
216 default:
217 return unknown;
218 }
219 }
220 return unknown;
221}
222
223TypeInfo TypeFeedbackOracle::SwitchType(CaseClause* clause) {
224 Handle<Object> object = GetElement(map_, clause->position());
225 TypeInfo unknown = TypeInfo::Unknown();
226 if (!object->IsCode()) return unknown;
227 Handle<Code> code = Handle<Code>::cast(object);
228 if (!code->is_compare_ic_stub()) return unknown;
229
230 CompareIC::State state = static_cast<CompareIC::State>(code->compare_state());
231 switch (state) {
232 case CompareIC::UNINITIALIZED:
233 // Uninitialized means never executed.
234 // TODO(fschneider): Introduce a separate value for never-executed ICs.
235 return unknown;
236 case CompareIC::SMIS:
237 return TypeInfo::Smi();
238 case CompareIC::HEAP_NUMBERS:
239 return TypeInfo::Number();
240 case CompareIC::OBJECTS:
241 // TODO(kasperl): We really need a type for JS objects here.
242 return TypeInfo::NonPrimitive();
243 case CompareIC::GENERIC:
244 default:
245 return unknown;
246 }
247}
248
249
250
251ZoneMapList* TypeFeedbackOracle::CollectReceiverTypes(int position,
252 Handle<String> name,
253 Code::Flags flags) {
254 Handle<Object> object = GetElement(map_, position);
255 if (object->IsUndefined()) return NULL;
256
257 if (*object == Builtins::builtin(Builtins::StoreIC_GlobalProxy)) {
258 // TODO(fschneider): We could collect the maps and signal that
259 // we need a generic store (or load) here.
260 ASSERT(Handle<Code>::cast(object)->ic_state() == MEGAMORPHIC);
261 return NULL;
262 } else if (object->IsMap()) {
263 ZoneMapList* types = new ZoneMapList(1);
264 types->Add(Handle<Map>::cast(object));
265 return types;
266 } else if (Handle<Code>::cast(object)->ic_state() == MEGAMORPHIC) {
267 ZoneMapList* types = new ZoneMapList(4);
268 ASSERT(object->IsCode());
269 StubCache::CollectMatchingMaps(types, *name, flags);
270 return types->length() > 0 ? types : NULL;
271 } else {
272 return NULL;
273 }
274}
275
276
277void TypeFeedbackOracle::PopulateMap(Handle<Code> code) {
278 HandleScope scope;
279
280 const int kInitialCapacity = 16;
281 List<int> code_positions(kInitialCapacity);
282 List<int> source_positions(kInitialCapacity);
283 CollectPositions(*code, &code_positions, &source_positions);
284
285 int length = code_positions.length();
286 ASSERT(source_positions.length() == length);
287 for (int i = 0; i < length; i++) {
288 RelocInfo info(code->instruction_start() + code_positions[i],
289 RelocInfo::CODE_TARGET, 0);
290 Handle<Code> target(Code::GetCodeFromTargetAddress(info.target_address()));
291 int position = source_positions[i];
292 InlineCacheState state = target->ic_state();
293 Code::Kind kind = target->kind();
294 if (kind == Code::BINARY_OP_IC ||
295 kind == Code::TYPE_RECORDING_BINARY_OP_IC ||
296 kind == Code::COMPARE_IC) {
297 // TODO(kasperl): Avoid having multiple ICs with the same
298 // position by making sure that we have position information
299 // recorded for all binary ICs.
300 if (GetElement(map_, position)->IsUndefined()) {
301 SetElement(map_, position, target);
302 }
303 } else if (state == MONOMORPHIC) {
304 Handle<Map> map = Handle<Map>(target->FindFirstMap());
305 if (*map == NULL) {
306 SetElement(map_, position, target);
307 } else {
308 SetElement(map_, position, map);
309 }
310 } else if (state == MEGAMORPHIC) {
311 SetElement(map_, position, target);
312 }
313 }
314}
315
316
317void TypeFeedbackOracle::CollectPositions(Code* code,
318 List<int>* code_positions,
319 List<int>* source_positions) {
320 AssertNoAllocation no_allocation;
321 int position = 0;
322 // Because the ICs we use for global variables access in the full
323 // code generator do not have any meaningful positions, we avoid
324 // collecting those by filtering out contextual code targets.
325 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
326 RelocInfo::kPositionMask;
327 for (RelocIterator it(code, mask); !it.done(); it.next()) {
328 RelocInfo* info = it.rinfo();
329 RelocInfo::Mode mode = info->rmode();
330 if (RelocInfo::IsCodeTarget(mode)) {
331 Code* target = Code::GetCodeFromTargetAddress(info->target_address());
332 if (target->is_inline_cache_stub()) {
333 InlineCacheState state = target->ic_state();
334 Code::Kind kind = target->kind();
335 if (kind == Code::BINARY_OP_IC) {
336 if (target->binary_op_type() == BinaryOpIC::GENERIC) continue;
337 } else if (kind == Code::TYPE_RECORDING_BINARY_OP_IC) {
338 if (target->type_recording_binary_op_type() ==
339 TRBinaryOpIC::GENERIC) {
340 continue;
341 }
342 } else if (kind == Code::COMPARE_IC) {
343 if (target->compare_state() == CompareIC::GENERIC) continue;
344 } else {
345 if (kind == Code::CALL_IC && state == MONOMORPHIC &&
346 target->check_type() != RECEIVER_MAP_CHECK) continue;
347 if (state != MONOMORPHIC && state != MEGAMORPHIC) continue;
348 }
349 code_positions->Add(
350 static_cast<int>(info->pc() - code->instruction_start()));
351 source_positions->Add(position);
352 }
353 } else {
354 ASSERT(RelocInfo::IsPosition(mode));
355 position = static_cast<int>(info->data());
356 }
357 }
358}
359
Steve Blocka7e24c12009-10-30 11:49:00 +0000360} } // namespace v8::internal