Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5 | #include "src/v8.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | #include "src/ast.h" |
| 8 | #include "src/code-stubs.h" |
| 9 | #include "src/compiler.h" |
| 10 | #include "src/ic/ic.h" |
| 11 | #include "src/ic/stub-cache.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12 | #include "src/type-info.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14 | namespace v8 { |
| 15 | namespace internal { |
| 16 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 17 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18 | TypeFeedbackOracle::TypeFeedbackOracle( |
| 19 | Handle<Code> code, Handle<TypeFeedbackVector> feedback_vector, |
| 20 | Handle<Context> native_context, Zone* zone) |
| 21 | : native_context_(native_context), zone_(zone) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 22 | BuildDictionary(code); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 23 | DCHECK(dictionary_->IsDictionary()); |
| 24 | // We make a copy of the feedback vector because a GC could clear |
| 25 | // the type feedback info contained therein. |
| 26 | // TODO(mvstanton): revisit the decision to copy when we weakly |
| 27 | // traverse the feedback vector at GC time. |
| 28 | feedback_vector_ = TypeFeedbackVector::Copy(isolate(), feedback_vector); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 32 | static uint32_t IdToKey(TypeFeedbackId ast_id) { |
| 33 | return static_cast<uint32_t>(ast_id.ToInt()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 37 | Handle<Object> TypeFeedbackOracle::GetInfo(TypeFeedbackId ast_id) { |
| 38 | int entry = dictionary_->FindEntry(IdToKey(ast_id)); |
| 39 | if (entry != UnseededNumberDictionary::kNotFound) { |
| 40 | Object* value = dictionary_->ValueAt(entry); |
| 41 | if (value->IsCell()) { |
| 42 | Cell* cell = Cell::cast(value); |
| 43 | return Handle<Object>(cell->value(), isolate()); |
| 44 | } else { |
| 45 | return Handle<Object>(value, isolate()); |
| 46 | } |
| 47 | } |
| 48 | return Handle<Object>::cast(isolate()->factory()->undefined_value()); |
| 49 | } |
| 50 | |
| 51 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 52 | Handle<Object> TypeFeedbackOracle::GetInfo(FeedbackVectorSlot slot) { |
| 53 | DCHECK(slot.ToInt() >= 0 && slot.ToInt() < feedback_vector_->length()); |
| 54 | Object* obj = feedback_vector_->Get(slot); |
| 55 | if (!obj->IsJSFunction() || |
| 56 | !CanRetainOtherContext(JSFunction::cast(obj), *native_context_)) { |
| 57 | return Handle<Object>(obj, isolate()); |
| 58 | } |
| 59 | return Handle<Object>::cast(isolate()->factory()->undefined_value()); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | Handle<Object> TypeFeedbackOracle::GetInfo(FeedbackVectorICSlot slot) { |
| 64 | DCHECK(slot.ToInt() >= 0 && slot.ToInt() < feedback_vector_->length()); |
| 65 | Object* obj = feedback_vector_->Get(slot); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 66 | if (!obj->IsJSFunction() || |
| 67 | !CanRetainOtherContext(JSFunction::cast(obj), *native_context_)) { |
| 68 | return Handle<Object>(obj, isolate()); |
| 69 | } |
| 70 | return Handle<Object>::cast(isolate()->factory()->undefined_value()); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | bool TypeFeedbackOracle::LoadIsUninitialized(TypeFeedbackId id) { |
| 75 | Handle<Object> maybe_code = GetInfo(id); |
| 76 | if (maybe_code->IsCode()) { |
| 77 | Handle<Code> code = Handle<Code>::cast(maybe_code); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 78 | return code->is_inline_cache_stub() && code->ic_state() == UNINITIALIZED; |
| 79 | } |
| 80 | return false; |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 84 | bool TypeFeedbackOracle::LoadIsUninitialized(FeedbackVectorICSlot slot) { |
| 85 | Code::Kind kind = feedback_vector_->GetKind(slot); |
| 86 | if (kind == Code::LOAD_IC) { |
| 87 | LoadICNexus nexus(feedback_vector_, slot); |
| 88 | return nexus.StateFromFeedback() == UNINITIALIZED; |
| 89 | } else if (kind == Code::KEYED_LOAD_IC) { |
| 90 | KeyedLoadICNexus nexus(feedback_vector_, slot); |
| 91 | return nexus.StateFromFeedback() == UNINITIALIZED; |
| 92 | } else if (kind == Code::NUMBER_OF_KINDS) { |
| 93 | // Code::NUMBER_OF_KINDS indicates a slot that was never even compiled |
| 94 | // in full code. |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 102 | bool TypeFeedbackOracle::StoreIsUninitialized(TypeFeedbackId ast_id) { |
| 103 | Handle<Object> maybe_code = GetInfo(ast_id); |
| 104 | if (!maybe_code->IsCode()) return false; |
| 105 | Handle<Code> code = Handle<Code>::cast(maybe_code); |
| 106 | return code->ic_state() == UNINITIALIZED; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 110 | bool TypeFeedbackOracle::CallIsUninitialized(FeedbackVectorICSlot slot) { |
| 111 | Handle<Object> value = GetInfo(slot); |
| 112 | return value->IsUndefined() || |
| 113 | value.is_identical_to( |
| 114 | TypeFeedbackVector::UninitializedSentinel(isolate())); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 118 | bool TypeFeedbackOracle::CallIsMonomorphic(FeedbackVectorICSlot slot) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 119 | Handle<Object> value = GetInfo(slot); |
| 120 | return value->IsAllocationSite() || value->IsJSFunction(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 124 | bool TypeFeedbackOracle::CallNewIsMonomorphic(FeedbackVectorSlot slot) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 125 | Handle<Object> info = GetInfo(slot); |
| 126 | return FLAG_pretenuring_call_new |
| 127 | ? info->IsJSFunction() |
| 128 | : info->IsAllocationSite() || info->IsJSFunction(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 132 | byte TypeFeedbackOracle::ForInType(FeedbackVectorSlot feedback_vector_slot) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 133 | Handle<Object> value = GetInfo(feedback_vector_slot); |
| 134 | return value.is_identical_to( |
| 135 | TypeFeedbackVector::UninitializedSentinel(isolate())) |
| 136 | ? ForInStatement::FAST_FOR_IN |
| 137 | : ForInStatement::SLOW_FOR_IN; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 141 | void TypeFeedbackOracle::GetStoreModeAndKeyType( |
| 142 | TypeFeedbackId ast_id, KeyedAccessStoreMode* store_mode, |
| 143 | IcCheckType* key_type) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 144 | Handle<Object> maybe_code = GetInfo(ast_id); |
| 145 | if (maybe_code->IsCode()) { |
| 146 | Handle<Code> code = Handle<Code>::cast(maybe_code); |
| 147 | if (code->kind() == Code::KEYED_STORE_IC) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 148 | ExtraICState extra_ic_state = code->extra_ic_state(); |
| 149 | *store_mode = KeyedStoreIC::GetKeyedAccessStoreMode(extra_ic_state); |
| 150 | *key_type = KeyedStoreIC::GetKeyType(extra_ic_state); |
| 151 | return; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 152 | } |
| 153 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 154 | *store_mode = STANDARD_STORE; |
| 155 | *key_type = ELEMENT; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 158 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 159 | void TypeFeedbackOracle::GetLoadKeyType( |
| 160 | TypeFeedbackId ast_id, IcCheckType* key_type) { |
| 161 | Handle<Object> maybe_code = GetInfo(ast_id); |
| 162 | if (maybe_code->IsCode()) { |
| 163 | Handle<Code> code = Handle<Code>::cast(maybe_code); |
| 164 | if (code->kind() == Code::KEYED_LOAD_IC) { |
| 165 | ExtraICState extra_ic_state = code->extra_ic_state(); |
| 166 | *key_type = KeyedLoadIC::GetKeyType(extra_ic_state); |
| 167 | return; |
| 168 | } |
| 169 | } |
| 170 | *key_type = ELEMENT; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | Handle<JSFunction> TypeFeedbackOracle::GetCallTarget( |
| 175 | FeedbackVectorICSlot slot) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 176 | Handle<Object> info = GetInfo(slot); |
| 177 | if (info->IsAllocationSite()) { |
| 178 | return Handle<JSFunction>(isolate()->native_context()->array_function()); |
| 179 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 180 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 181 | return Handle<JSFunction>::cast(info); |
| 182 | } |
| 183 | |
| 184 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 185 | Handle<JSFunction> TypeFeedbackOracle::GetCallNewTarget( |
| 186 | FeedbackVectorSlot slot) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 187 | Handle<Object> info = GetInfo(slot); |
| 188 | if (FLAG_pretenuring_call_new || info->IsJSFunction()) { |
| 189 | return Handle<JSFunction>::cast(info); |
| 190 | } |
| 191 | |
| 192 | DCHECK(info->IsAllocationSite()); |
| 193 | return Handle<JSFunction>(isolate()->native_context()->array_function()); |
| 194 | } |
| 195 | |
| 196 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 197 | Handle<AllocationSite> TypeFeedbackOracle::GetCallAllocationSite( |
| 198 | FeedbackVectorICSlot slot) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 199 | Handle<Object> info = GetInfo(slot); |
| 200 | if (info->IsAllocationSite()) { |
| 201 | return Handle<AllocationSite>::cast(info); |
| 202 | } |
| 203 | return Handle<AllocationSite>::null(); |
| 204 | } |
| 205 | |
| 206 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 207 | Handle<AllocationSite> TypeFeedbackOracle::GetCallNewAllocationSite( |
| 208 | FeedbackVectorSlot slot) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 209 | Handle<Object> info = GetInfo(slot); |
| 210 | if (FLAG_pretenuring_call_new || info->IsAllocationSite()) { |
| 211 | return Handle<AllocationSite>::cast(info); |
| 212 | } |
| 213 | return Handle<AllocationSite>::null(); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | bool TypeFeedbackOracle::LoadIsBuiltin( |
| 218 | TypeFeedbackId id, Builtins::Name builtin) { |
| 219 | return *GetInfo(id) == isolate()->builtins()->builtin(builtin); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | void TypeFeedbackOracle::CompareType(TypeFeedbackId id, |
| 224 | Type** left_type, |
| 225 | Type** right_type, |
| 226 | Type** combined_type) { |
| 227 | Handle<Object> info = GetInfo(id); |
| 228 | if (!info->IsCode()) { |
| 229 | // For some comparisons we don't have ICs, e.g. LiteralCompareTypeof. |
| 230 | *left_type = *right_type = *combined_type = Type::None(zone()); |
| 231 | return; |
| 232 | } |
| 233 | Handle<Code> code = Handle<Code>::cast(info); |
| 234 | |
| 235 | Handle<Map> map; |
| 236 | Map* raw_map = code->FindFirstMap(); |
| 237 | if (raw_map != NULL) { |
| 238 | if (Map::TryUpdate(handle(raw_map)).ToHandle(&map) && |
| 239 | CanRetainOtherContext(*map, *native_context_)) { |
| 240 | map = Handle<Map>::null(); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if (code->is_compare_ic_stub()) { |
| 245 | CompareICStub stub(code->stub_key(), isolate()); |
| 246 | *left_type = CompareICState::StateToType(zone(), stub.left()); |
| 247 | *right_type = CompareICState::StateToType(zone(), stub.right()); |
| 248 | *combined_type = CompareICState::StateToType(zone(), stub.state(), map); |
| 249 | } else if (code->is_compare_nil_ic_stub()) { |
| 250 | CompareNilICStub stub(isolate(), code->extra_ic_state()); |
| 251 | *combined_type = stub.GetType(zone(), map); |
| 252 | *left_type = *right_type = stub.GetInputType(zone(), map); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
| 256 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 257 | void TypeFeedbackOracle::BinaryType(TypeFeedbackId id, |
| 258 | Type** left, |
| 259 | Type** right, |
| 260 | Type** result, |
| 261 | Maybe<int>* fixed_right_arg, |
| 262 | Handle<AllocationSite>* allocation_site, |
| 263 | Token::Value op) { |
| 264 | Handle<Object> object = GetInfo(id); |
| 265 | if (!object->IsCode()) { |
| 266 | // For some binary ops we don't have ICs, e.g. Token::COMMA, but for the |
| 267 | // operations covered by the BinaryOpIC we should always have them. |
| 268 | DCHECK(op < BinaryOpICState::FIRST_TOKEN || |
| 269 | op > BinaryOpICState::LAST_TOKEN); |
| 270 | *left = *right = *result = Type::None(zone()); |
| 271 | *fixed_right_arg = Maybe<int>(); |
| 272 | *allocation_site = Handle<AllocationSite>::null(); |
| 273 | return; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 274 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 275 | Handle<Code> code = Handle<Code>::cast(object); |
| 276 | DCHECK_EQ(Code::BINARY_OP_IC, code->kind()); |
| 277 | BinaryOpICState state(isolate(), code->extra_ic_state()); |
| 278 | DCHECK_EQ(op, state.op()); |
| 279 | |
| 280 | *left = state.GetLeftType(zone()); |
| 281 | *right = state.GetRightType(zone()); |
| 282 | *result = state.GetResultType(zone()); |
| 283 | *fixed_right_arg = state.fixed_right_arg(); |
| 284 | |
| 285 | AllocationSite* first_allocation_site = code->FindFirstAllocationSite(); |
| 286 | if (first_allocation_site != NULL) { |
| 287 | *allocation_site = handle(first_allocation_site); |
| 288 | } else { |
| 289 | *allocation_site = Handle<AllocationSite>::null(); |
| 290 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 294 | Type* TypeFeedbackOracle::CountType(TypeFeedbackId id) { |
| 295 | Handle<Object> object = GetInfo(id); |
| 296 | if (!object->IsCode()) return Type::None(zone()); |
| 297 | Handle<Code> code = Handle<Code>::cast(object); |
| 298 | DCHECK_EQ(Code::BINARY_OP_IC, code->kind()); |
| 299 | BinaryOpICState state(isolate(), code->extra_ic_state()); |
| 300 | return state.GetLeftType(zone()); |
| 301 | } |
| 302 | |
| 303 | |
| 304 | void TypeFeedbackOracle::PropertyReceiverTypes(TypeFeedbackId id, |
| 305 | Handle<String> name, |
| 306 | SmallMapList* receiver_types) { |
| 307 | receiver_types->Clear(); |
| 308 | Code::Flags flags = Code::ComputeHandlerFlags(Code::LOAD_IC); |
| 309 | CollectReceiverTypes(id, name, flags, receiver_types); |
| 310 | } |
| 311 | |
| 312 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 313 | bool TypeFeedbackOracle::HasOnlyStringMaps(SmallMapList* receiver_types) { |
| 314 | bool all_strings = receiver_types->length() > 0; |
| 315 | for (int i = 0; i < receiver_types->length(); i++) { |
| 316 | all_strings &= receiver_types->at(i)->IsStringMap(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 317 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 318 | return all_strings; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | void TypeFeedbackOracle::KeyedPropertyReceiverTypes( |
| 323 | TypeFeedbackId id, |
| 324 | SmallMapList* receiver_types, |
| 325 | bool* is_string, |
| 326 | IcCheckType* key_type) { |
| 327 | receiver_types->Clear(); |
| 328 | CollectReceiverTypes(id, receiver_types); |
| 329 | *is_string = HasOnlyStringMaps(receiver_types); |
| 330 | GetLoadKeyType(id, key_type); |
| 331 | } |
| 332 | |
| 333 | |
| 334 | void TypeFeedbackOracle::PropertyReceiverTypes(FeedbackVectorICSlot slot, |
| 335 | Handle<String> name, |
| 336 | SmallMapList* receiver_types) { |
| 337 | receiver_types->Clear(); |
| 338 | LoadICNexus nexus(feedback_vector_, slot); |
| 339 | Code::Flags flags = Code::ComputeHandlerFlags(Code::LOAD_IC); |
| 340 | CollectReceiverTypes(&nexus, name, flags, receiver_types); |
| 341 | } |
| 342 | |
| 343 | |
| 344 | void TypeFeedbackOracle::KeyedPropertyReceiverTypes( |
| 345 | FeedbackVectorICSlot slot, SmallMapList* receiver_types, bool* is_string, |
| 346 | IcCheckType* key_type) { |
| 347 | receiver_types->Clear(); |
| 348 | KeyedLoadICNexus nexus(feedback_vector_, slot); |
| 349 | CollectReceiverTypes<FeedbackNexus>(&nexus, receiver_types); |
| 350 | *is_string = HasOnlyStringMaps(receiver_types); |
| 351 | *key_type = nexus.FindFirstName() != NULL ? PROPERTY : ELEMENT; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | |
| 355 | void TypeFeedbackOracle::AssignmentReceiverTypes( |
| 356 | TypeFeedbackId id, Handle<String> name, SmallMapList* receiver_types) { |
| 357 | receiver_types->Clear(); |
| 358 | Code::Flags flags = Code::ComputeHandlerFlags(Code::STORE_IC); |
| 359 | CollectReceiverTypes(id, name, flags, receiver_types); |
| 360 | } |
| 361 | |
| 362 | |
| 363 | void TypeFeedbackOracle::KeyedAssignmentReceiverTypes( |
| 364 | TypeFeedbackId id, SmallMapList* receiver_types, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 365 | KeyedAccessStoreMode* store_mode, IcCheckType* key_type) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 366 | receiver_types->Clear(); |
| 367 | CollectReceiverTypes(id, receiver_types); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 368 | GetStoreModeAndKeyType(id, store_mode, key_type); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | |
| 372 | void TypeFeedbackOracle::CountReceiverTypes(TypeFeedbackId id, |
| 373 | SmallMapList* receiver_types) { |
| 374 | receiver_types->Clear(); |
| 375 | CollectReceiverTypes(id, receiver_types); |
| 376 | } |
| 377 | |
| 378 | |
| 379 | void TypeFeedbackOracle::CollectReceiverTypes(TypeFeedbackId ast_id, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 380 | Handle<String> name, |
| 381 | Code::Flags flags, |
| 382 | SmallMapList* types) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 383 | Handle<Object> object = GetInfo(ast_id); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 384 | if (object->IsUndefined() || object->IsSmi()) return; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 385 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 386 | DCHECK(object->IsCode()); |
| 387 | Handle<Code> code(Handle<Code>::cast(object)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 388 | CollectReceiverTypes<Code>(*code, name, flags, types); |
| 389 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 390 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 391 | |
| 392 | template <class T> |
| 393 | void TypeFeedbackOracle::CollectReceiverTypes(T* obj, Handle<String> name, |
| 394 | Code::Flags flags, |
| 395 | SmallMapList* types) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 396 | if (FLAG_collect_megamorphic_maps_from_stub_cache && |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 397 | obj->ic_state() == MEGAMORPHIC) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 398 | types->Reserve(4, zone()); |
| 399 | isolate()->stub_cache()->CollectMatchingMaps( |
| 400 | types, name, flags, native_context_, zone()); |
| 401 | } else { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 402 | CollectReceiverTypes<T>(obj, types); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
| 406 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 407 | // Check if a map originates from a given native context. We use this |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 408 | // information to filter out maps from different context to avoid |
| 409 | // retaining objects from different tabs in Chrome via optimized code. |
| 410 | bool TypeFeedbackOracle::CanRetainOtherContext(Map* map, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 411 | Context* native_context) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 412 | Object* constructor = NULL; |
| 413 | while (!map->prototype()->IsNull()) { |
| 414 | constructor = map->constructor(); |
| 415 | if (!constructor->IsNull()) { |
| 416 | // If the constructor is not null or a JSFunction, we have to |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 417 | // conservatively assume that it may retain a native context. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 418 | if (!constructor->IsJSFunction()) return true; |
| 419 | // Check if the constructor directly references a foreign context. |
| 420 | if (CanRetainOtherContext(JSFunction::cast(constructor), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 421 | native_context)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 422 | return true; |
| 423 | } |
| 424 | } |
| 425 | map = HeapObject::cast(map->prototype())->map(); |
| 426 | } |
| 427 | constructor = map->constructor(); |
| 428 | if (constructor->IsNull()) return false; |
| 429 | JSFunction* function = JSFunction::cast(constructor); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 430 | return CanRetainOtherContext(function, native_context); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | |
| 434 | bool TypeFeedbackOracle::CanRetainOtherContext(JSFunction* function, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 435 | Context* native_context) { |
| 436 | return function->context()->global_object() != native_context->global_object() |
| 437 | && function->context()->global_object() != native_context->builtins(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 441 | void TypeFeedbackOracle::CollectReceiverTypes(TypeFeedbackId ast_id, |
| 442 | SmallMapList* types) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 443 | Handle<Object> object = GetInfo(ast_id); |
| 444 | if (!object->IsCode()) return; |
| 445 | Handle<Code> code = Handle<Code>::cast(object); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 446 | CollectReceiverTypes<Code>(*code, types); |
| 447 | } |
| 448 | |
| 449 | |
| 450 | template <class T> |
| 451 | void TypeFeedbackOracle::CollectReceiverTypes(T* obj, SmallMapList* types) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 452 | MapHandleList maps; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 453 | if (obj->ic_state() == MONOMORPHIC) { |
| 454 | Map* map = obj->FindFirstMap(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 455 | if (map != NULL) maps.Add(handle(map)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 456 | } else if (obj->ic_state() == POLYMORPHIC) { |
| 457 | obj->FindAllMaps(&maps); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 458 | } else { |
| 459 | return; |
| 460 | } |
| 461 | types->Reserve(maps.length(), zone()); |
| 462 | for (int i = 0; i < maps.length(); i++) { |
| 463 | Handle<Map> map(maps.at(i)); |
| 464 | if (!CanRetainOtherContext(*map, *native_context_)) { |
| 465 | types->AddMapIfMissing(map, zone()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 466 | } |
| 467 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 471 | byte TypeFeedbackOracle::ToBooleanTypes(TypeFeedbackId id) { |
| 472 | Handle<Object> object = GetInfo(id); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 473 | return object->IsCode() ? Handle<Code>::cast(object)->to_boolean_state() : 0; |
| 474 | } |
| 475 | |
| 476 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 477 | // Things are a bit tricky here: The iterator for the RelocInfos and the infos |
| 478 | // themselves are not GC-safe, so we first get all infos, then we create the |
| 479 | // dictionary (possibly triggering GC), and finally we relocate the collected |
| 480 | // infos before we process them. |
| 481 | void TypeFeedbackOracle::BuildDictionary(Handle<Code> code) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 482 | DisallowHeapAllocation no_allocation; |
| 483 | ZoneList<RelocInfo> infos(16, zone()); |
| 484 | HandleScope scope(isolate()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 485 | GetRelocInfos(code, &infos); |
| 486 | CreateDictionary(code, &infos); |
| 487 | ProcessRelocInfos(&infos); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 488 | // Allocate handle in the parent scope. |
| 489 | dictionary_ = scope.CloseAndEscape(dictionary_); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 493 | void TypeFeedbackOracle::GetRelocInfos(Handle<Code> code, |
| 494 | ZoneList<RelocInfo>* infos) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 495 | int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET_WITH_ID); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 496 | for (RelocIterator it(*code, mask); !it.done(); it.next()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 497 | infos->Add(*it.rinfo(), zone()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 501 | |
| 502 | void TypeFeedbackOracle::CreateDictionary(Handle<Code> code, |
| 503 | ZoneList<RelocInfo>* infos) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 504 | AllowHeapAllocation allocation_allowed; |
| 505 | Code* old_code = *code; |
| 506 | dictionary_ = UnseededNumberDictionary::New(isolate(), infos->length()); |
| 507 | RelocateRelocInfos(infos, old_code, *code); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | |
| 511 | void TypeFeedbackOracle::RelocateRelocInfos(ZoneList<RelocInfo>* infos, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 512 | Code* old_code, |
| 513 | Code* new_code) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 514 | for (int i = 0; i < infos->length(); i++) { |
| 515 | RelocInfo* info = &(*infos)[i]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 516 | info->set_host(new_code); |
| 517 | info->set_pc(new_code->instruction_start() + |
| 518 | (info->pc() - old_code->instruction_start())); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
| 522 | |
| 523 | void TypeFeedbackOracle::ProcessRelocInfos(ZoneList<RelocInfo>* infos) { |
| 524 | for (int i = 0; i < infos->length(); i++) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 525 | RelocInfo reloc_entry = (*infos)[i]; |
| 526 | Address target_address = reloc_entry.target_address(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 527 | TypeFeedbackId ast_id = |
| 528 | TypeFeedbackId(static_cast<unsigned>((*infos)[i].data())); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 529 | Code* target = Code::GetCodeFromTargetAddress(target_address); |
| 530 | switch (target->kind()) { |
| 531 | case Code::LOAD_IC: |
| 532 | case Code::STORE_IC: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 533 | case Code::KEYED_LOAD_IC: |
| 534 | case Code::KEYED_STORE_IC: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 535 | case Code::BINARY_OP_IC: |
| 536 | case Code::COMPARE_IC: |
| 537 | case Code::TO_BOOLEAN_IC: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 538 | case Code::COMPARE_NIL_IC: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 539 | SetInfo(ast_id, target); |
| 540 | break; |
| 541 | |
| 542 | default: |
| 543 | break; |
| 544 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
| 548 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 549 | void TypeFeedbackOracle::SetInfo(TypeFeedbackId ast_id, Object* target) { |
| 550 | DCHECK(dictionary_->FindEntry(IdToKey(ast_id)) == |
| 551 | UnseededNumberDictionary::kNotFound); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 552 | // Dictionary has been allocated with sufficient size for all elements. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 553 | DisallowHeapAllocation no_need_to_resize_dictionary; |
| 554 | HandleScope scope(isolate()); |
| 555 | USE(UnseededNumberDictionary::AtNumberPut( |
| 556 | dictionary_, IdToKey(ast_id), handle(target, isolate()))); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 559 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 560 | } } // namespace v8::internal |