blob: f51366f86033c52129e65b14a7dcc473cbaa4e8b [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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 Murdoch4a90d5f2016-03-22 12:00:34 +00005#include "src/ic/stub-cache.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006
7#include "src/base/bits.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/type-info.h"
9
10namespace v8 {
11namespace internal {
12
13
14StubCache::StubCache(Isolate* isolate) : isolate_(isolate) {}
15
16
17void StubCache::Initialize() {
18 DCHECK(base::bits::IsPowerOfTwo32(kPrimaryTableSize));
19 DCHECK(base::bits::IsPowerOfTwo32(kSecondaryTableSize));
20 Clear();
21}
22
23
24static Code::Flags CommonStubCacheChecks(Name* name, Map* map,
25 Code::Flags flags) {
Ben Murdochc5610432016-08-08 18:44:38 +010026 flags = Code::RemoveHolderFromFlags(flags);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000027
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 Murdochb8a8cc12014-11-26 15:28:44 +000037
Ben Murdochc5610432016-08-08 18:44:38 +010038 // Make sure that the cache holder are not included in the hash.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039 DCHECK(Code::ExtractCacheHolderFromFlags(flags) == 0);
40
41 return flags;
42}
43
44
45Code* 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 Murdochc5610432016-08-08 18:44:38 +010057 Code::Flags old_flags = Code::RemoveHolderFromFlags(old_code->flags());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058 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
73Code* 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 Murdoch61f157c2016-09-16 13:49:30 +010077 if (primary->key == name && primary->map == map &&
78 flags == Code::RemoveHolderFromFlags(primary->value->flags())) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 return primary->value;
80 }
81 int secondary_offset = SecondaryOffset(name, flags, primary_offset);
82 Entry* secondary = entry(secondary_, secondary_offset);
Ben Murdoch61f157c2016-09-16 13:49:30 +010083 if (secondary->key == name && secondary->map == map &&
84 flags == Code::RemoveHolderFromFlags(secondary->value->flags())) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000085 return secondary->value;
86 }
87 return NULL;
88}
89
90
91void 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
106void 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 Murdoch4a90d5f2016-03-22 12:00:34 +0000119 TypeFeedbackOracle::IsRelevantFeedback(map, *native_context)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000120 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 Murdoch4a90d5f2016-03-22 12:00:34 +0000138 TypeFeedbackOracle::IsRelevantFeedback(map, *native_context)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000139 types->AddMapIfMissing(Handle<Map>(map), zone);
140 }
141 }
142 }
143}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000144} // namespace internal
145} // namespace v8