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