Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2 | // 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 Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 28 | #include "v8.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 29 | |
| 30 | #include "ast.h" |
| 31 | #include "compiler.h" |
| 32 | #include "ic.h" |
| 33 | #include "macro-assembler.h" |
| 34 | #include "stub-cache.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 35 | #include "type-info.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 36 | |
| 37 | #include "ic-inl.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 38 | #include "objects-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 39 | |
| 40 | namespace v8 { |
| 41 | namespace internal { |
| 42 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 43 | |
| 44 | TypeInfo 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 60 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 61 | STATIC_ASSERT(DEFAULT_STRING_STUB == Code::kNoExtraICState); |
| 62 | |
| 63 | |
| 64 | TypeFeedbackOracle::TypeFeedbackOracle(Handle<Code> code, |
| 65 | Handle<Context> global_context) { |
| 66 | global_context_ = global_context; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 67 | PopulateMap(code); |
| 68 | ASSERT(reinterpret_cast<Address>(*dictionary_.location()) != kHandleZapValue); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 72 | Handle<Object> TypeFeedbackOracle::GetInfo(int pos) { |
| 73 | int entry = dictionary_->FindEntry(pos); |
| 74 | return entry != NumberDictionary::kNotFound |
| 75 | ? Handle<Object>(dictionary_->ValueAt(entry)) |
| 76 | : Isolate::Current()->factory()->undefined_value(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | |
| 80 | bool TypeFeedbackOracle::LoadIsMonomorphic(Property* expr) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 81 | Handle<Object> map_or_code(GetInfo(expr->position())); |
| 82 | if (map_or_code->IsMap()) return true; |
| 83 | if (map_or_code->IsCode()) { |
| 84 | Handle<Code> code(Code::cast(*map_or_code)); |
| 85 | return code->kind() == Code::KEYED_EXTERNAL_ARRAY_LOAD_IC && |
| 86 | code->FindFirstMap() != NULL; |
| 87 | } |
| 88 | return false; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 92 | bool TypeFeedbackOracle::StoreIsMonomorphic(Expression* expr) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 93 | Handle<Object> map_or_code(GetInfo(expr->position())); |
| 94 | if (map_or_code->IsMap()) return true; |
| 95 | if (map_or_code->IsCode()) { |
| 96 | Handle<Code> code(Code::cast(*map_or_code)); |
| 97 | return code->kind() == Code::KEYED_EXTERNAL_ARRAY_STORE_IC && |
| 98 | code->FindFirstMap() != NULL; |
| 99 | } |
| 100 | return false; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | |
| 104 | bool TypeFeedbackOracle::CallIsMonomorphic(Call* expr) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 105 | Handle<Object> value = GetInfo(expr->position()); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 106 | return value->IsMap() || value->IsSmi(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | |
| 110 | Handle<Map> TypeFeedbackOracle::LoadMonomorphicReceiverType(Property* expr) { |
| 111 | ASSERT(LoadIsMonomorphic(expr)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 112 | Handle<Object> map_or_code( |
| 113 | Handle<HeapObject>::cast(GetInfo(expr->position()))); |
| 114 | if (map_or_code->IsCode()) { |
| 115 | Handle<Code> code(Code::cast(*map_or_code)); |
| 116 | return Handle<Map>(code->FindFirstMap()); |
| 117 | } |
| 118 | return Handle<Map>(Map::cast(*map_or_code)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 122 | Handle<Map> TypeFeedbackOracle::StoreMonomorphicReceiverType(Expression* expr) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 123 | ASSERT(StoreIsMonomorphic(expr)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 124 | Handle<HeapObject> map_or_code( |
| 125 | Handle<HeapObject>::cast(GetInfo(expr->position()))); |
| 126 | if (map_or_code->IsCode()) { |
| 127 | Handle<Code> code(Code::cast(*map_or_code)); |
| 128 | return Handle<Map>(code->FindFirstMap()); |
| 129 | } |
| 130 | return Handle<Map>(Map::cast(*map_or_code)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 134 | ZoneMapList* TypeFeedbackOracle::LoadReceiverTypes(Property* expr, |
| 135 | Handle<String> name) { |
| 136 | Code::Flags flags = Code::ComputeMonomorphicFlags(Code::LOAD_IC, NORMAL); |
| 137 | return CollectReceiverTypes(expr->position(), name, flags); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | ZoneMapList* TypeFeedbackOracle::StoreReceiverTypes(Assignment* expr, |
| 142 | Handle<String> name) { |
| 143 | Code::Flags flags = Code::ComputeMonomorphicFlags(Code::STORE_IC, NORMAL); |
| 144 | return CollectReceiverTypes(expr->position(), name, flags); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | ZoneMapList* TypeFeedbackOracle::CallReceiverTypes(Call* expr, |
| 149 | Handle<String> name) { |
| 150 | int arity = expr->arguments()->length(); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 151 | // Note: these flags won't let us get maps from stubs with |
| 152 | // non-default extra ic state in the megamorphic case. In the more |
| 153 | // important monomorphic case the map is obtained directly, so it's |
| 154 | // not a problem until we decide to emit more polymorphic code. |
| 155 | Code::Flags flags = Code::ComputeMonomorphicFlags(Code::CALL_IC, |
| 156 | NORMAL, |
| 157 | Code::kNoExtraICState, |
| 158 | OWN_MAP, |
| 159 | NOT_IN_LOOP, |
| 160 | arity); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 161 | return CollectReceiverTypes(expr->position(), name, flags); |
| 162 | } |
| 163 | |
| 164 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 165 | CheckType TypeFeedbackOracle::GetCallCheckType(Call* expr) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 166 | Handle<Object> value = GetInfo(expr->position()); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 167 | if (!value->IsSmi()) return RECEIVER_MAP_CHECK; |
| 168 | CheckType check = static_cast<CheckType>(Smi::cast(*value)->value()); |
| 169 | ASSERT(check != RECEIVER_MAP_CHECK); |
| 170 | return check; |
| 171 | } |
| 172 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 173 | ExternalArrayType TypeFeedbackOracle::GetKeyedLoadExternalArrayType( |
| 174 | Property* expr) { |
| 175 | Handle<Object> stub = GetInfo(expr->position()); |
| 176 | ASSERT(stub->IsCode()); |
| 177 | return Code::cast(*stub)->external_array_type(); |
| 178 | } |
| 179 | |
| 180 | ExternalArrayType TypeFeedbackOracle::GetKeyedStoreExternalArrayType( |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 181 | Expression* expr) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 182 | Handle<Object> stub = GetInfo(expr->position()); |
| 183 | ASSERT(stub->IsCode()); |
| 184 | return Code::cast(*stub)->external_array_type(); |
| 185 | } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 186 | |
| 187 | Handle<JSObject> TypeFeedbackOracle::GetPrototypeForPrimitiveCheck( |
| 188 | CheckType check) { |
| 189 | JSFunction* function = NULL; |
| 190 | switch (check) { |
| 191 | case RECEIVER_MAP_CHECK: |
| 192 | UNREACHABLE(); |
| 193 | break; |
| 194 | case STRING_CHECK: |
| 195 | function = global_context_->string_function(); |
| 196 | break; |
| 197 | case NUMBER_CHECK: |
| 198 | function = global_context_->number_function(); |
| 199 | break; |
| 200 | case BOOLEAN_CHECK: |
| 201 | function = global_context_->boolean_function(); |
| 202 | break; |
| 203 | } |
| 204 | ASSERT(function != NULL); |
| 205 | return Handle<JSObject>(JSObject::cast(function->instance_prototype())); |
| 206 | } |
| 207 | |
| 208 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 209 | bool TypeFeedbackOracle::LoadIsBuiltin(Property* expr, Builtins::Name id) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 210 | return *GetInfo(expr->position()) == |
| 211 | Isolate::Current()->builtins()->builtin(id); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 215 | TypeInfo TypeFeedbackOracle::CompareType(CompareOperation* expr) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 216 | Handle<Object> object = GetInfo(expr->position()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 217 | TypeInfo unknown = TypeInfo::Unknown(); |
| 218 | if (!object->IsCode()) return unknown; |
| 219 | Handle<Code> code = Handle<Code>::cast(object); |
| 220 | if (!code->is_compare_ic_stub()) return unknown; |
| 221 | |
| 222 | CompareIC::State state = static_cast<CompareIC::State>(code->compare_state()); |
| 223 | switch (state) { |
| 224 | case CompareIC::UNINITIALIZED: |
| 225 | // Uninitialized means never executed. |
| 226 | // TODO(fschneider): Introduce a separate value for never-executed ICs. |
| 227 | return unknown; |
| 228 | case CompareIC::SMIS: |
| 229 | return TypeInfo::Smi(); |
| 230 | case CompareIC::HEAP_NUMBERS: |
| 231 | return TypeInfo::Number(); |
| 232 | case CompareIC::OBJECTS: |
| 233 | // TODO(kasperl): We really need a type for JS objects here. |
| 234 | return TypeInfo::NonPrimitive(); |
| 235 | case CompareIC::GENERIC: |
| 236 | default: |
| 237 | return unknown; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 242 | TypeInfo TypeFeedbackOracle::BinaryType(BinaryOperation* expr) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 243 | Handle<Object> object = GetInfo(expr->position()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 244 | TypeInfo unknown = TypeInfo::Unknown(); |
| 245 | if (!object->IsCode()) return unknown; |
| 246 | Handle<Code> code = Handle<Code>::cast(object); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 247 | if (code->is_type_recording_binary_op_stub()) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 248 | TRBinaryOpIC::TypeInfo type = static_cast<TRBinaryOpIC::TypeInfo>( |
| 249 | code->type_recording_binary_op_type()); |
| 250 | TRBinaryOpIC::TypeInfo result_type = static_cast<TRBinaryOpIC::TypeInfo>( |
| 251 | code->type_recording_binary_op_result_type()); |
| 252 | |
| 253 | switch (type) { |
| 254 | case TRBinaryOpIC::UNINITIALIZED: |
| 255 | // Uninitialized means never executed. |
| 256 | // TODO(fschneider): Introduce a separate value for never-executed ICs |
| 257 | return unknown; |
| 258 | case TRBinaryOpIC::SMI: |
| 259 | switch (result_type) { |
| 260 | case TRBinaryOpIC::UNINITIALIZED: |
| 261 | case TRBinaryOpIC::SMI: |
| 262 | return TypeInfo::Smi(); |
| 263 | case TRBinaryOpIC::INT32: |
| 264 | return TypeInfo::Integer32(); |
| 265 | case TRBinaryOpIC::HEAP_NUMBER: |
| 266 | return TypeInfo::Double(); |
| 267 | default: |
| 268 | return unknown; |
| 269 | } |
| 270 | case TRBinaryOpIC::INT32: |
| 271 | if (expr->op() == Token::DIV || |
| 272 | result_type == TRBinaryOpIC::HEAP_NUMBER) { |
| 273 | return TypeInfo::Double(); |
| 274 | } |
| 275 | return TypeInfo::Integer32(); |
| 276 | case TRBinaryOpIC::HEAP_NUMBER: |
| 277 | return TypeInfo::Double(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 278 | case TRBinaryOpIC::BOTH_STRING: |
| 279 | return TypeInfo::String(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 280 | case TRBinaryOpIC::STRING: |
| 281 | case TRBinaryOpIC::GENERIC: |
| 282 | return unknown; |
| 283 | default: |
| 284 | return unknown; |
| 285 | } |
| 286 | } |
| 287 | return unknown; |
| 288 | } |
| 289 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 290 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 291 | TypeInfo TypeFeedbackOracle::SwitchType(CaseClause* clause) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 292 | Handle<Object> object = GetInfo(clause->position()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 293 | TypeInfo unknown = TypeInfo::Unknown(); |
| 294 | if (!object->IsCode()) return unknown; |
| 295 | Handle<Code> code = Handle<Code>::cast(object); |
| 296 | if (!code->is_compare_ic_stub()) return unknown; |
| 297 | |
| 298 | CompareIC::State state = static_cast<CompareIC::State>(code->compare_state()); |
| 299 | switch (state) { |
| 300 | case CompareIC::UNINITIALIZED: |
| 301 | // Uninitialized means never executed. |
| 302 | // TODO(fschneider): Introduce a separate value for never-executed ICs. |
| 303 | return unknown; |
| 304 | case CompareIC::SMIS: |
| 305 | return TypeInfo::Smi(); |
| 306 | case CompareIC::HEAP_NUMBERS: |
| 307 | return TypeInfo::Number(); |
| 308 | case CompareIC::OBJECTS: |
| 309 | // TODO(kasperl): We really need a type for JS objects here. |
| 310 | return TypeInfo::NonPrimitive(); |
| 311 | case CompareIC::GENERIC: |
| 312 | default: |
| 313 | return unknown; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 318 | ZoneMapList* TypeFeedbackOracle::CollectReceiverTypes(int position, |
| 319 | Handle<String> name, |
| 320 | Code::Flags flags) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 321 | Isolate* isolate = Isolate::Current(); |
| 322 | Handle<Object> object = GetInfo(position); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 323 | if (object->IsUndefined() || object->IsSmi()) return NULL; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 324 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 325 | if (*object == isolate->builtins()->builtin(Builtins::kStoreIC_GlobalProxy)) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 326 | // TODO(fschneider): We could collect the maps and signal that |
| 327 | // we need a generic store (or load) here. |
| 328 | ASSERT(Handle<Code>::cast(object)->ic_state() == MEGAMORPHIC); |
| 329 | return NULL; |
| 330 | } else if (object->IsMap()) { |
| 331 | ZoneMapList* types = new ZoneMapList(1); |
| 332 | types->Add(Handle<Map>::cast(object)); |
| 333 | return types; |
| 334 | } else if (Handle<Code>::cast(object)->ic_state() == MEGAMORPHIC) { |
| 335 | ZoneMapList* types = new ZoneMapList(4); |
| 336 | ASSERT(object->IsCode()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 337 | isolate->stub_cache()->CollectMatchingMaps(types, *name, flags); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 338 | return types->length() > 0 ? types : NULL; |
| 339 | } else { |
| 340 | return NULL; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 345 | void TypeFeedbackOracle::SetInfo(int position, Object* target) { |
| 346 | MaybeObject* maybe_result = dictionary_->AtNumberPut(position, target); |
| 347 | USE(maybe_result); |
| 348 | #ifdef DEBUG |
| 349 | Object* result; |
| 350 | // Dictionary has been allocated with sufficient size for all elements. |
| 351 | ASSERT(maybe_result->ToObject(&result)); |
| 352 | ASSERT(*dictionary_ == result); |
| 353 | #endif |
| 354 | } |
| 355 | |
| 356 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 357 | void TypeFeedbackOracle::PopulateMap(Handle<Code> code) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 358 | Isolate* isolate = Isolate::Current(); |
| 359 | HandleScope scope(isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 360 | |
| 361 | const int kInitialCapacity = 16; |
| 362 | List<int> code_positions(kInitialCapacity); |
| 363 | List<int> source_positions(kInitialCapacity); |
| 364 | CollectPositions(*code, &code_positions, &source_positions); |
| 365 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 366 | ASSERT(dictionary_.is_null()); // Only initialize once. |
| 367 | dictionary_ = isolate->factory()->NewNumberDictionary( |
| 368 | code_positions.length()); |
| 369 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 370 | int length = code_positions.length(); |
| 371 | ASSERT(source_positions.length() == length); |
| 372 | for (int i = 0; i < length; i++) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 373 | AssertNoAllocation no_allocation; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 374 | RelocInfo info(code->instruction_start() + code_positions[i], |
| 375 | RelocInfo::CODE_TARGET, 0); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 376 | Code* target = Code::GetCodeFromTargetAddress(info.target_address()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 377 | int position = source_positions[i]; |
| 378 | InlineCacheState state = target->ic_state(); |
| 379 | Code::Kind kind = target->kind(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 380 | |
| 381 | if (kind == Code::TYPE_RECORDING_BINARY_OP_IC || |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 382 | kind == Code::COMPARE_IC) { |
| 383 | // TODO(kasperl): Avoid having multiple ICs with the same |
| 384 | // position by making sure that we have position information |
| 385 | // recorded for all binary ICs. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 386 | int entry = dictionary_->FindEntry(position); |
| 387 | if (entry == NumberDictionary::kNotFound) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 388 | SetInfo(position, target); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 389 | } |
| 390 | } else if (state == MONOMORPHIC) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 391 | if (kind == Code::KEYED_EXTERNAL_ARRAY_LOAD_IC || |
| 392 | kind == Code::KEYED_EXTERNAL_ARRAY_STORE_IC) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 393 | SetInfo(position, target); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 394 | } else if (target->kind() != Code::CALL_IC || |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 395 | target->check_type() == RECEIVER_MAP_CHECK) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 396 | Map* map = target->FindFirstMap(); |
| 397 | if (map == NULL) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 398 | SetInfo(position, target); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 399 | } else { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 400 | SetInfo(position, map); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 401 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 402 | } else { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 403 | ASSERT(target->kind() == Code::CALL_IC); |
| 404 | CheckType check = target->check_type(); |
| 405 | ASSERT(check != RECEIVER_MAP_CHECK); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 406 | SetInfo(position, Smi::FromInt(check)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 407 | } |
| 408 | } else if (state == MEGAMORPHIC) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 409 | SetInfo(position, target); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 410 | } |
| 411 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 412 | // Allocate handle in the parent scope. |
| 413 | dictionary_ = scope.CloseAndEscape(dictionary_); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | |
| 417 | void TypeFeedbackOracle::CollectPositions(Code* code, |
| 418 | List<int>* code_positions, |
| 419 | List<int>* source_positions) { |
| 420 | AssertNoAllocation no_allocation; |
| 421 | int position = 0; |
| 422 | // Because the ICs we use for global variables access in the full |
| 423 | // code generator do not have any meaningful positions, we avoid |
| 424 | // collecting those by filtering out contextual code targets. |
| 425 | int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) | |
| 426 | RelocInfo::kPositionMask; |
| 427 | for (RelocIterator it(code, mask); !it.done(); it.next()) { |
| 428 | RelocInfo* info = it.rinfo(); |
| 429 | RelocInfo::Mode mode = info->rmode(); |
| 430 | if (RelocInfo::IsCodeTarget(mode)) { |
| 431 | Code* target = Code::GetCodeFromTargetAddress(info->target_address()); |
| 432 | if (target->is_inline_cache_stub()) { |
| 433 | InlineCacheState state = target->ic_state(); |
| 434 | Code::Kind kind = target->kind(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 435 | if (kind == Code::TYPE_RECORDING_BINARY_OP_IC) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 436 | if (target->type_recording_binary_op_type() == |
| 437 | TRBinaryOpIC::GENERIC) { |
| 438 | continue; |
| 439 | } |
| 440 | } else if (kind == Code::COMPARE_IC) { |
| 441 | if (target->compare_state() == CompareIC::GENERIC) continue; |
| 442 | } else { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 443 | if (state != MONOMORPHIC && state != MEGAMORPHIC) continue; |
| 444 | } |
| 445 | code_positions->Add( |
| 446 | static_cast<int>(info->pc() - code->instruction_start())); |
| 447 | source_positions->Add(position); |
| 448 | } |
| 449 | } else { |
| 450 | ASSERT(RelocInfo::IsPosition(mode)); |
| 451 | position = static_cast<int>(info->data()); |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 456 | } } // namespace v8::internal |