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