blob: f4f65e99b4836c8a4d5b0924ac093b11289734f3 [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 Murdochb8e0da22011-05-16 14:20:40 +010061STATIC_ASSERT(DEFAULT_STRING_STUB == Code::kNoExtraICState);
62
63
64TypeFeedbackOracle::TypeFeedbackOracle(Handle<Code> code,
65 Handle<Context> global_context) {
66 global_context_ = global_context;
Ben Murdochb0fe1622011-05-05 13:52:32 +010067 Initialize(code);
68}
69
70
71void TypeFeedbackOracle::Initialize(Handle<Code> code) {
72 ASSERT(map_.is_null()); // Only initialize once.
73 map_ = Factory::NewJSObject(Top::object_function());
74 PopulateMap(code);
75}
76
77
78bool TypeFeedbackOracle::LoadIsMonomorphic(Property* expr) {
Ben Murdochb8e0da22011-05-16 14:20:40 +010079 return GetElement(map_, expr->position())->IsMap();
Ben Murdochb0fe1622011-05-05 13:52:32 +010080}
81
82
83bool TypeFeedbackOracle:: StoreIsMonomorphic(Assignment* expr) {
Ben Murdochb8e0da22011-05-16 14:20:40 +010084 return GetElement(map_, expr->position())->IsMap();
Ben Murdochb0fe1622011-05-05 13:52:32 +010085}
86
87
88bool TypeFeedbackOracle::CallIsMonomorphic(Call* expr) {
Ben Murdochb8e0da22011-05-16 14:20:40 +010089 Handle<Object> value = GetElement(map_, expr->position());
90 return value->IsMap() || value->IsSmi();
Ben Murdochb0fe1622011-05-05 13:52:32 +010091}
92
93
94Handle<Map> TypeFeedbackOracle::LoadMonomorphicReceiverType(Property* expr) {
95 ASSERT(LoadIsMonomorphic(expr));
96 return Handle<Map>::cast(GetElement(map_, expr->position()));
97}
98
99
100Handle<Map> TypeFeedbackOracle::StoreMonomorphicReceiverType(Assignment* expr) {
101 ASSERT(StoreIsMonomorphic(expr));
102 return Handle<Map>::cast(GetElement(map_, expr->position()));
103}
104
105
Ben Murdochb0fe1622011-05-05 13:52:32 +0100106ZoneMapList* 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();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100123 // Note: these flags won't let us get maps from stubs with
124 // non-default extra ic state in the megamorphic case. In the more
125 // important monomorphic case the map is obtained directly, so it's
126 // not a problem until we decide to emit more polymorphic code.
127 Code::Flags flags = Code::ComputeMonomorphicFlags(Code::CALL_IC,
128 NORMAL,
129 Code::kNoExtraICState,
130 OWN_MAP,
131 NOT_IN_LOOP,
132 arity);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100133 return CollectReceiverTypes(expr->position(), name, flags);
134}
135
136
Ben Murdochb8e0da22011-05-16 14:20:40 +0100137CheckType TypeFeedbackOracle::GetCallCheckType(Call* expr) {
138 Handle<Object> value = GetElement(map_, expr->position());
139 if (!value->IsSmi()) return RECEIVER_MAP_CHECK;
140 CheckType check = static_cast<CheckType>(Smi::cast(*value)->value());
141 ASSERT(check != RECEIVER_MAP_CHECK);
142 return check;
143}
144
145
146Handle<JSObject> TypeFeedbackOracle::GetPrototypeForPrimitiveCheck(
147 CheckType check) {
148 JSFunction* function = NULL;
149 switch (check) {
150 case RECEIVER_MAP_CHECK:
151 UNREACHABLE();
152 break;
153 case STRING_CHECK:
154 function = global_context_->string_function();
155 break;
156 case NUMBER_CHECK:
157 function = global_context_->number_function();
158 break;
159 case BOOLEAN_CHECK:
160 function = global_context_->boolean_function();
161 break;
162 }
163 ASSERT(function != NULL);
164 return Handle<JSObject>(JSObject::cast(function->instance_prototype()));
165}
166
167
Ben Murdochb0fe1622011-05-05 13:52:32 +0100168bool TypeFeedbackOracle::LoadIsBuiltin(Property* expr, Builtins::Name id) {
169 Handle<Object> object = GetElement(map_, expr->position());
170 return *object == Builtins::builtin(id);
171}
172
173
174TypeInfo TypeFeedbackOracle::CompareType(CompareOperation* expr, Side side) {
175 Handle<Object> object = GetElement(map_, expr->position());
176 TypeInfo unknown = TypeInfo::Unknown();
177 if (!object->IsCode()) return unknown;
178 Handle<Code> code = Handle<Code>::cast(object);
179 if (!code->is_compare_ic_stub()) return unknown;
180
181 CompareIC::State state = static_cast<CompareIC::State>(code->compare_state());
182 switch (state) {
183 case CompareIC::UNINITIALIZED:
184 // Uninitialized means never executed.
185 // TODO(fschneider): Introduce a separate value for never-executed ICs.
186 return unknown;
187 case CompareIC::SMIS:
188 return TypeInfo::Smi();
189 case CompareIC::HEAP_NUMBERS:
190 return TypeInfo::Number();
191 case CompareIC::OBJECTS:
192 // TODO(kasperl): We really need a type for JS objects here.
193 return TypeInfo::NonPrimitive();
194 case CompareIC::GENERIC:
195 default:
196 return unknown;
197 }
198}
199
200
201TypeInfo TypeFeedbackOracle::BinaryType(BinaryOperation* expr, Side side) {
202 Handle<Object> object = GetElement(map_, expr->position());
203 TypeInfo unknown = TypeInfo::Unknown();
204 if (!object->IsCode()) return unknown;
205 Handle<Code> code = Handle<Code>::cast(object);
206 if (code->is_binary_op_stub()) {
207 BinaryOpIC::TypeInfo type = static_cast<BinaryOpIC::TypeInfo>(
208 code->binary_op_type());
209 switch (type) {
210 case BinaryOpIC::UNINIT_OR_SMI:
211 return TypeInfo::Smi();
212 case BinaryOpIC::DEFAULT:
213 return (expr->op() == Token::DIV || expr->op() == Token::MUL)
214 ? TypeInfo::Double()
215 : TypeInfo::Integer32();
216 case BinaryOpIC::HEAP_NUMBERS:
217 return TypeInfo::Double();
218 default:
219 return unknown;
220 }
221 } else if (code->is_type_recording_binary_op_stub()) {
222 TRBinaryOpIC::TypeInfo type = static_cast<TRBinaryOpIC::TypeInfo>(
223 code->type_recording_binary_op_type());
224 TRBinaryOpIC::TypeInfo result_type = static_cast<TRBinaryOpIC::TypeInfo>(
225 code->type_recording_binary_op_result_type());
226
227 switch (type) {
228 case TRBinaryOpIC::UNINITIALIZED:
229 // Uninitialized means never executed.
230 // TODO(fschneider): Introduce a separate value for never-executed ICs
231 return unknown;
232 case TRBinaryOpIC::SMI:
233 switch (result_type) {
234 case TRBinaryOpIC::UNINITIALIZED:
235 case TRBinaryOpIC::SMI:
236 return TypeInfo::Smi();
237 case TRBinaryOpIC::INT32:
238 return TypeInfo::Integer32();
239 case TRBinaryOpIC::HEAP_NUMBER:
240 return TypeInfo::Double();
241 default:
242 return unknown;
243 }
244 case TRBinaryOpIC::INT32:
245 if (expr->op() == Token::DIV ||
246 result_type == TRBinaryOpIC::HEAP_NUMBER) {
247 return TypeInfo::Double();
248 }
249 return TypeInfo::Integer32();
250 case TRBinaryOpIC::HEAP_NUMBER:
251 return TypeInfo::Double();
252 case TRBinaryOpIC::STRING:
253 case TRBinaryOpIC::GENERIC:
254 return unknown;
255 default:
256 return unknown;
257 }
258 }
259 return unknown;
260}
261
Ben Murdochb8e0da22011-05-16 14:20:40 +0100262
Ben Murdochb0fe1622011-05-05 13:52:32 +0100263TypeInfo TypeFeedbackOracle::SwitchType(CaseClause* clause) {
264 Handle<Object> object = GetElement(map_, clause->position());
265 TypeInfo unknown = TypeInfo::Unknown();
266 if (!object->IsCode()) return unknown;
267 Handle<Code> code = Handle<Code>::cast(object);
268 if (!code->is_compare_ic_stub()) return unknown;
269
270 CompareIC::State state = static_cast<CompareIC::State>(code->compare_state());
271 switch (state) {
272 case CompareIC::UNINITIALIZED:
273 // Uninitialized means never executed.
274 // TODO(fschneider): Introduce a separate value for never-executed ICs.
275 return unknown;
276 case CompareIC::SMIS:
277 return TypeInfo::Smi();
278 case CompareIC::HEAP_NUMBERS:
279 return TypeInfo::Number();
280 case CompareIC::OBJECTS:
281 // TODO(kasperl): We really need a type for JS objects here.
282 return TypeInfo::NonPrimitive();
283 case CompareIC::GENERIC:
284 default:
285 return unknown;
286 }
287}
288
289
Ben Murdochb0fe1622011-05-05 13:52:32 +0100290ZoneMapList* TypeFeedbackOracle::CollectReceiverTypes(int position,
291 Handle<String> name,
292 Code::Flags flags) {
293 Handle<Object> object = GetElement(map_, position);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100294 if (object->IsUndefined() || object->IsSmi()) return NULL;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100295
296 if (*object == Builtins::builtin(Builtins::StoreIC_GlobalProxy)) {
297 // TODO(fschneider): We could collect the maps and signal that
298 // we need a generic store (or load) here.
299 ASSERT(Handle<Code>::cast(object)->ic_state() == MEGAMORPHIC);
300 return NULL;
301 } else if (object->IsMap()) {
302 ZoneMapList* types = new ZoneMapList(1);
303 types->Add(Handle<Map>::cast(object));
304 return types;
305 } else if (Handle<Code>::cast(object)->ic_state() == MEGAMORPHIC) {
306 ZoneMapList* types = new ZoneMapList(4);
307 ASSERT(object->IsCode());
308 StubCache::CollectMatchingMaps(types, *name, flags);
309 return types->length() > 0 ? types : NULL;
310 } else {
311 return NULL;
312 }
313}
314
315
316void TypeFeedbackOracle::PopulateMap(Handle<Code> code) {
317 HandleScope scope;
318
319 const int kInitialCapacity = 16;
320 List<int> code_positions(kInitialCapacity);
321 List<int> source_positions(kInitialCapacity);
322 CollectPositions(*code, &code_positions, &source_positions);
323
324 int length = code_positions.length();
325 ASSERT(source_positions.length() == length);
326 for (int i = 0; i < length; i++) {
327 RelocInfo info(code->instruction_start() + code_positions[i],
328 RelocInfo::CODE_TARGET, 0);
329 Handle<Code> target(Code::GetCodeFromTargetAddress(info.target_address()));
330 int position = source_positions[i];
331 InlineCacheState state = target->ic_state();
332 Code::Kind kind = target->kind();
333 if (kind == Code::BINARY_OP_IC ||
334 kind == Code::TYPE_RECORDING_BINARY_OP_IC ||
335 kind == Code::COMPARE_IC) {
336 // TODO(kasperl): Avoid having multiple ICs with the same
337 // position by making sure that we have position information
338 // recorded for all binary ICs.
339 if (GetElement(map_, position)->IsUndefined()) {
340 SetElement(map_, position, target);
341 }
342 } else if (state == MONOMORPHIC) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100343 if (target->kind() != Code::CALL_IC ||
344 target->check_type() == RECEIVER_MAP_CHECK) {
345 Handle<Map> map = Handle<Map>(target->FindFirstMap());
346 if (*map == NULL) {
347 SetElement(map_, position, target);
348 } else {
349 SetElement(map_, position, map);
350 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100351 } else {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100352 ASSERT(target->kind() == Code::CALL_IC);
353 CheckType check = target->check_type();
354 ASSERT(check != RECEIVER_MAP_CHECK);
355 SetElement(map_, position, Handle<Object>(Smi::FromInt(check)));
356 ASSERT(Smi::cast(*GetElement(map_, position))->value() == check);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100357 }
358 } else if (state == MEGAMORPHIC) {
359 SetElement(map_, position, target);
360 }
361 }
362}
363
364
365void TypeFeedbackOracle::CollectPositions(Code* code,
366 List<int>* code_positions,
367 List<int>* source_positions) {
368 AssertNoAllocation no_allocation;
369 int position = 0;
370 // Because the ICs we use for global variables access in the full
371 // code generator do not have any meaningful positions, we avoid
372 // collecting those by filtering out contextual code targets.
373 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
374 RelocInfo::kPositionMask;
375 for (RelocIterator it(code, mask); !it.done(); it.next()) {
376 RelocInfo* info = it.rinfo();
377 RelocInfo::Mode mode = info->rmode();
378 if (RelocInfo::IsCodeTarget(mode)) {
379 Code* target = Code::GetCodeFromTargetAddress(info->target_address());
380 if (target->is_inline_cache_stub()) {
381 InlineCacheState state = target->ic_state();
382 Code::Kind kind = target->kind();
383 if (kind == Code::BINARY_OP_IC) {
384 if (target->binary_op_type() == BinaryOpIC::GENERIC) continue;
385 } else if (kind == Code::TYPE_RECORDING_BINARY_OP_IC) {
386 if (target->type_recording_binary_op_type() ==
387 TRBinaryOpIC::GENERIC) {
388 continue;
389 }
390 } else if (kind == Code::COMPARE_IC) {
391 if (target->compare_state() == CompareIC::GENERIC) continue;
392 } else {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100393 if (state != MONOMORPHIC && state != MEGAMORPHIC) continue;
394 }
395 code_positions->Add(
396 static_cast<int>(info->pc() - code->instruction_start()));
397 source_positions->Add(position);
398 }
399 } else {
400 ASSERT(RelocInfo::IsPosition(mode));
401 position = static_cast<int>(info->data());
402 }
403 }
404}
405
Steve Blocka7e24c12009-10-30 11:49:00 +0000406} } // namespace v8::internal