| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/ic/stub-cache.h" | 5 #include "src/ic/stub-cache.h" |
| 6 | 6 |
| 7 #include "src/base/bits.h" | 7 #include "src/base/bits.h" |
| 8 #include "src/type-info.h" | 8 #include "src/type-info.h" |
| 9 | 9 |
| 10 namespace v8 { | 10 namespace v8 { |
| 11 namespace internal { | 11 namespace internal { |
| 12 | 12 |
| 13 StubCache::StubCache(Isolate* isolate, Code::Kind ic_kind) | 13 StubCache::StubCache(Isolate* isolate, Code::Kind ic_kind) |
| 14 : isolate_(isolate), ic_kind_(ic_kind) {} | 14 : isolate_(isolate), ic_kind_(ic_kind) {} |
| 15 | 15 |
| 16 void StubCache::Initialize() { | 16 void StubCache::Initialize() { |
| 17 DCHECK(base::bits::IsPowerOfTwo32(kPrimaryTableSize)); | 17 DCHECK(base::bits::IsPowerOfTwo32(kPrimaryTableSize)); |
| 18 DCHECK(base::bits::IsPowerOfTwo32(kSecondaryTableSize)); | 18 DCHECK(base::bits::IsPowerOfTwo32(kSecondaryTableSize)); |
| 19 Clear(); | 19 Clear(); |
| 20 } | 20 } |
| 21 | 21 |
| 22 | 22 static void CommonStubCacheChecks(Name* name, Map* map, Code* code) { |
| 23 static Code::Flags CommonStubCacheChecks(Name* name, Map* map, | |
| 24 Code::Flags flags) { | |
| 25 flags = Code::RemoveHolderFromFlags(flags); | |
| 26 | |
| 27 // Validate that the name does not move on scavenge, and that we | 23 // Validate that the name does not move on scavenge, and that we |
| 28 // can use identity checks instead of structural equality checks. | 24 // can use identity checks instead of structural equality checks. |
| 29 DCHECK(!name->GetHeap()->InNewSpace(name)); | 25 DCHECK(!name->GetHeap()->InNewSpace(name)); |
| 30 DCHECK(name->IsUniqueName()); | 26 DCHECK(name->IsUniqueName()); |
| 31 | 27 DCHECK(name->HasHashCode()); |
| 32 // The state bits are not important to the hash function because the stub | 28 if (code) { |
| 33 // cache only contains handlers. Make sure that the bits are the least | 29 DCHECK_EQ(Code::HANDLER, Code::ExtractKindFromFlags(code->flags())); |
| 34 // significant so they will be the ones masked out. | 30 } |
| 35 DCHECK_EQ(Code::HANDLER, Code::ExtractKindFromFlags(flags)); | |
| 36 | |
| 37 // Make sure that the cache holder are not included in the hash. | |
| 38 DCHECK(Code::ExtractCacheHolderFromFlags(flags) == 0); | |
| 39 | |
| 40 return flags; | |
| 41 } | 31 } |
| 42 | 32 |
| 43 | 33 |
| 44 Code* StubCache::Set(Name* name, Map* map, Code* code) { | 34 Code* StubCache::Set(Name* name, Map* map, Code* code) { |
| 45 Code::Flags flags = CommonStubCacheChecks(name, map, code->flags()); | 35 CommonStubCacheChecks(name, map, code); |
| 46 | 36 |
| 47 // Compute the primary entry. | 37 // Compute the primary entry. |
| 48 int primary_offset = PrimaryOffset(name, flags, map); | 38 int primary_offset = PrimaryOffset(name, map); |
| 49 Entry* primary = entry(primary_, primary_offset); | 39 Entry* primary = entry(primary_, primary_offset); |
| 50 Code* old_code = primary->value; | 40 Code* old_code = primary->value; |
| 51 | 41 |
| 52 // If the primary entry has useful data in it, we retire it to the | 42 // If the primary entry has useful data in it, we retire it to the |
| 53 // secondary cache before overwriting it. | 43 // secondary cache before overwriting it. |
| 54 if (old_code != isolate_->builtins()->builtin(Builtins::kIllegal)) { | 44 if (old_code != isolate_->builtins()->builtin(Builtins::kIllegal)) { |
| 55 Map* old_map = primary->map; | 45 Map* old_map = primary->map; |
| 56 Code::Flags old_flags = Code::RemoveHolderFromFlags(old_code->flags()); | 46 int seed = PrimaryOffset(primary->key, old_map); |
| 57 int seed = PrimaryOffset(primary->key, old_flags, old_map); | 47 int secondary_offset = SecondaryOffset(primary->key, seed); |
| 58 int secondary_offset = SecondaryOffset(primary->key, old_flags, seed); | |
| 59 Entry* secondary = entry(secondary_, secondary_offset); | 48 Entry* secondary = entry(secondary_, secondary_offset); |
| 60 *secondary = *primary; | 49 *secondary = *primary; |
| 61 } | 50 } |
| 62 | 51 |
| 63 // Update primary cache. | 52 // Update primary cache. |
| 64 primary->key = name; | 53 primary->key = name; |
| 65 primary->value = code; | 54 primary->value = code; |
| 66 primary->map = map; | 55 primary->map = map; |
| 67 isolate()->counters()->megamorphic_stub_cache_updates()->Increment(); | 56 isolate()->counters()->megamorphic_stub_cache_updates()->Increment(); |
| 68 return code; | 57 return code; |
| 69 } | 58 } |
| 70 | 59 |
| 71 | 60 Code* StubCache::Get(Name* name, Map* map) { |
| 72 Code* StubCache::Get(Name* name, Map* map, Code::Flags flags) { | 61 CommonStubCacheChecks(name, map, nullptr); |
| 73 flags = CommonStubCacheChecks(name, map, flags); | 62 int primary_offset = PrimaryOffset(name, map); |
| 74 int primary_offset = PrimaryOffset(name, flags, map); | |
| 75 Entry* primary = entry(primary_, primary_offset); | 63 Entry* primary = entry(primary_, primary_offset); |
| 76 if (primary->key == name && primary->map == map && | 64 if (primary->key == name && primary->map == map) { |
| 77 flags == Code::RemoveHolderFromFlags(primary->value->flags())) { | |
| 78 return primary->value; | 65 return primary->value; |
| 79 } | 66 } |
| 80 int secondary_offset = SecondaryOffset(name, flags, primary_offset); | 67 int secondary_offset = SecondaryOffset(name, primary_offset); |
| 81 Entry* secondary = entry(secondary_, secondary_offset); | 68 Entry* secondary = entry(secondary_, secondary_offset); |
| 82 if (secondary->key == name && secondary->map == map && | 69 if (secondary->key == name && secondary->map == map) { |
| 83 flags == Code::RemoveHolderFromFlags(secondary->value->flags())) { | |
| 84 return secondary->value; | 70 return secondary->value; |
| 85 } | 71 } |
| 86 return NULL; | 72 return NULL; |
| 87 } | 73 } |
| 88 | 74 |
| 89 | 75 |
| 90 void StubCache::Clear() { | 76 void StubCache::Clear() { |
| 91 Code* empty = isolate_->builtins()->builtin(Builtins::kIllegal); | 77 Code* empty = isolate_->builtins()->builtin(Builtins::kIllegal); |
| 92 for (int i = 0; i < kPrimaryTableSize; i++) { | 78 for (int i = 0; i < kPrimaryTableSize; i++) { |
| 93 primary_[i].key = isolate()->heap()->empty_string(); | 79 primary_[i].key = isolate()->heap()->empty_string(); |
| 94 primary_[i].map = NULL; | 80 primary_[i].map = NULL; |
| 95 primary_[i].value = empty; | 81 primary_[i].value = empty; |
| 96 } | 82 } |
| 97 for (int j = 0; j < kSecondaryTableSize; j++) { | 83 for (int j = 0; j < kSecondaryTableSize; j++) { |
| 98 secondary_[j].key = isolate()->heap()->empty_string(); | 84 secondary_[j].key = isolate()->heap()->empty_string(); |
| 99 secondary_[j].map = NULL; | 85 secondary_[j].map = NULL; |
| 100 secondary_[j].value = empty; | 86 secondary_[j].value = empty; |
| 101 } | 87 } |
| 102 } | 88 } |
| 103 | 89 |
| 104 | 90 |
| 105 void StubCache::CollectMatchingMaps(SmallMapList* types, Handle<Name> name, | 91 void StubCache::CollectMatchingMaps(SmallMapList* types, Handle<Name> name, |
| 106 Code::Flags flags, | |
| 107 Handle<Context> native_context, | 92 Handle<Context> native_context, |
| 108 Zone* zone) { | 93 Zone* zone) { |
| 109 for (int i = 0; i < kPrimaryTableSize; i++) { | 94 for (int i = 0; i < kPrimaryTableSize; i++) { |
| 110 if (primary_[i].key == *name) { | 95 if (primary_[i].key == *name) { |
| 111 Map* map = primary_[i].map; | 96 Map* map = primary_[i].map; |
| 112 // Map can be NULL, if the stub is constant function call | 97 // Map can be NULL, if the stub is constant function call |
| 113 // with a primitive receiver. | 98 // with a primitive receiver. |
| 114 if (map == NULL) continue; | 99 if (map == NULL) continue; |
| 115 | 100 |
| 116 int offset = PrimaryOffset(*name, flags, map); | 101 int offset = PrimaryOffset(*name, map); |
| 117 if (entry(primary_, offset) == &primary_[i] && | 102 if (entry(primary_, offset) == &primary_[i] && |
| 118 TypeFeedbackOracle::IsRelevantFeedback(map, *native_context)) { | 103 TypeFeedbackOracle::IsRelevantFeedback(map, *native_context)) { |
| 119 types->AddMapIfMissing(Handle<Map>(map), zone); | 104 types->AddMapIfMissing(Handle<Map>(map), zone); |
| 120 } | 105 } |
| 121 } | 106 } |
| 122 } | 107 } |
| 123 | 108 |
| 124 for (int i = 0; i < kSecondaryTableSize; i++) { | 109 for (int i = 0; i < kSecondaryTableSize; i++) { |
| 125 if (secondary_[i].key == *name) { | 110 if (secondary_[i].key == *name) { |
| 126 Map* map = secondary_[i].map; | 111 Map* map = secondary_[i].map; |
| 127 // Map can be NULL, if the stub is constant function call | 112 // Map can be NULL, if the stub is constant function call |
| 128 // with a primitive receiver. | 113 // with a primitive receiver. |
| 129 if (map == NULL) continue; | 114 if (map == NULL) continue; |
| 130 | 115 |
| 131 // Lookup in primary table and skip duplicates. | 116 // Lookup in primary table and skip duplicates. |
| 132 int primary_offset = PrimaryOffset(*name, flags, map); | 117 int primary_offset = PrimaryOffset(*name, map); |
| 133 | 118 |
| 134 // Lookup in secondary table and add matches. | 119 // Lookup in secondary table and add matches. |
| 135 int offset = SecondaryOffset(*name, flags, primary_offset); | 120 int offset = SecondaryOffset(*name, primary_offset); |
| 136 if (entry(secondary_, offset) == &secondary_[i] && | 121 if (entry(secondary_, offset) == &secondary_[i] && |
| 137 TypeFeedbackOracle::IsRelevantFeedback(map, *native_context)) { | 122 TypeFeedbackOracle::IsRelevantFeedback(map, *native_context)) { |
| 138 types->AddMapIfMissing(Handle<Map>(map), zone); | 123 types->AddMapIfMissing(Handle<Map>(map), zone); |
| 139 } | 124 } |
| 140 } | 125 } |
| 141 } | 126 } |
| 142 } | 127 } |
| 143 } // namespace internal | 128 } // namespace internal |
| 144 } // namespace v8 | 129 } // namespace v8 |
| OLD | NEW |