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