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
5 #include "src/ic/stub-cache.h"
6
7 #include "src/ast/ast.h"
8 #include "src/base/bits.h"
9 #include "src/heap/heap-inl.h" // For InYoungGeneration().
10 #include "src/ic/ic-inl.h"
11 #include "src/logging/counters.h"
12 #include "src/objects/tagged-value-inl.h"
13
14 namespace v8 {
15 namespace internal {
16
StubCache(Isolate * isolate)17 StubCache::StubCache(Isolate* isolate) : isolate_(isolate) {
18 // Ensure the nullptr (aka Smi::zero()) which StubCache::Get() returns
19 // when the entry is not found is not considered as a handler.
20 DCHECK(!IC::IsHandler(MaybeObject()));
21 }
22
Initialize()23 void StubCache::Initialize() {
24 DCHECK(base::bits::IsPowerOfTwo(kPrimaryTableSize));
25 DCHECK(base::bits::IsPowerOfTwo(kSecondaryTableSize));
26 Clear();
27 }
28
29 // Hash algorithm for the primary table. This algorithm is replicated in
30 // the AccessorAssembler. Returns an index into the table that
31 // is scaled by 1 << kCacheIndexShift.
PrimaryOffset(Name name,Map map)32 int StubCache::PrimaryOffset(Name name, Map map) {
33 // Compute the hash of the name (use entire hash field).
34 DCHECK(name.HasHashCode());
35 uint32_t field = name.hash_field();
36 // Using only the low bits in 64-bit mode is unlikely to increase the
37 // risk of collision even if the heap is spread over an area larger than
38 // 4Gb (and not at all if it isn't).
39 uint32_t map_low32bits =
40 static_cast<uint32_t>(map.ptr() ^ (map.ptr() >> kMapKeyShift));
41 // Base the offset on a simple combination of name and map.
42 uint32_t key = map_low32bits + field;
43 return key & ((kPrimaryTableSize - 1) << kCacheIndexShift);
44 }
45
46 // Hash algorithm for the secondary table. This algorithm is replicated in
47 // assembler for every architecture. Returns an index into the table that
48 // is scaled by 1 << kCacheIndexShift.
SecondaryOffset(Name name,int seed)49 int StubCache::SecondaryOffset(Name name, int seed) {
50 // Use the seed from the primary cache in the secondary cache.
51 uint32_t name_low32bits = static_cast<uint32_t>(name.ptr());
52 uint32_t key = (seed - name_low32bits) + kSecondaryMagic;
53 return key & ((kSecondaryTableSize - 1) << kCacheIndexShift);
54 }
55
PrimaryOffsetForTesting(Name name,Map map)56 int StubCache::PrimaryOffsetForTesting(Name name, Map map) {
57 return PrimaryOffset(name, map);
58 }
59
SecondaryOffsetForTesting(Name name,int seed)60 int StubCache::SecondaryOffsetForTesting(Name name, int seed) {
61 return SecondaryOffset(name, seed);
62 }
63
64 #ifdef DEBUG
65 namespace {
66
CommonStubCacheChecks(StubCache * stub_cache,Name name,Map map,MaybeObject handler)67 bool CommonStubCacheChecks(StubCache* stub_cache, Name name, Map map,
68 MaybeObject handler) {
69 // Validate that the name and handler do not move on scavenge, and that we
70 // can use identity checks instead of structural equality checks.
71 DCHECK(!Heap::InYoungGeneration(name));
72 DCHECK(!Heap::InYoungGeneration(handler));
73 DCHECK(name.IsUniqueName());
74 DCHECK(name.HasHashCode());
75 if (handler->ptr() != kNullAddress) DCHECK(IC::IsHandler(handler));
76 return true;
77 }
78
79 } // namespace
80 #endif
81
Set(Name name,Map map,MaybeObject handler)82 void StubCache::Set(Name name, Map map, MaybeObject handler) {
83 DCHECK(CommonStubCacheChecks(this, name, map, handler));
84
85 // Compute the primary entry.
86 int primary_offset = PrimaryOffset(name, map);
87 Entry* primary = entry(primary_, primary_offset);
88 MaybeObject old_handler(
89 TaggedValue::ToMaybeObject(isolate(), primary->value));
90 // If the primary entry has useful data in it, we retire it to the
91 // secondary cache before overwriting it.
92 if (old_handler != MaybeObject::FromObject(
93 isolate()->builtins()->builtin(Builtins::kIllegal)) &&
94 !primary->map.IsSmi()) {
95 Map old_map =
96 Map::cast(StrongTaggedValue::ToObject(isolate(), primary->map));
97 int seed = PrimaryOffset(
98 Name::cast(StrongTaggedValue::ToObject(isolate(), primary->key)),
99 old_map);
100 int secondary_offset = SecondaryOffset(
101 Name::cast(StrongTaggedValue::ToObject(isolate(), primary->key)), seed);
102 Entry* secondary = entry(secondary_, secondary_offset);
103 *secondary = *primary;
104 }
105
106 // Update primary cache.
107 primary->key = StrongTaggedValue(name);
108 primary->value = TaggedValue(handler);
109 primary->map = StrongTaggedValue(map);
110 isolate()->counters()->megamorphic_stub_cache_updates()->Increment();
111 }
112
Get(Name name,Map map)113 MaybeObject StubCache::Get(Name name, Map map) {
114 DCHECK(CommonStubCacheChecks(this, name, map, MaybeObject()));
115 int primary_offset = PrimaryOffset(name, map);
116 Entry* primary = entry(primary_, primary_offset);
117 if (primary->key == name && primary->map == map) {
118 return TaggedValue::ToMaybeObject(isolate(), primary->value);
119 }
120 int secondary_offset = SecondaryOffset(name, primary_offset);
121 Entry* secondary = entry(secondary_, secondary_offset);
122 if (secondary->key == name && secondary->map == map) {
123 return TaggedValue::ToMaybeObject(isolate(), secondary->value);
124 }
125 return MaybeObject();
126 }
127
Clear()128 void StubCache::Clear() {
129 MaybeObject empty = MaybeObject::FromObject(
130 isolate_->builtins()->builtin(Builtins::kIllegal));
131 Name empty_string = ReadOnlyRoots(isolate()).empty_string();
132 for (int i = 0; i < kPrimaryTableSize; i++) {
133 primary_[i].key = StrongTaggedValue(empty_string);
134 primary_[i].map = StrongTaggedValue(Smi::zero());
135 primary_[i].value = TaggedValue(empty);
136 }
137 for (int j = 0; j < kSecondaryTableSize; j++) {
138 secondary_[j].key = StrongTaggedValue(empty_string);
139 secondary_[j].map = StrongTaggedValue(Smi::zero());
140 secondary_[j].value = TaggedValue(empty);
141 }
142 }
143
144 } // namespace internal
145 } // namespace v8
146