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.
7 #include "src/base/bits.h"
8 #include "src/ic/stub-cache.h"
9 #include "src/type-info.h"
15 StubCache::StubCache(Isolate* isolate) : isolate_(isolate) {}
18 void StubCache::Initialize() {
19 DCHECK(base::bits::IsPowerOfTwo32(kPrimaryTableSize));
20 DCHECK(base::bits::IsPowerOfTwo32(kSecondaryTableSize));
25 static Code::Flags CommonStubCacheChecks(Name* name, Map* map,
27 flags = Code::RemoveTypeAndHolderFromFlags(flags);
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());
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);
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);
48 Code* StubCache::Set(Name* name, Map* map, Code* code) {
49 Code::Flags flags = CommonStubCacheChecks(name, map, code->flags());
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;
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;
68 // Update primary cache.
70 primary->value = code;
72 isolate()->counters()->megamorphic_stub_cache_updates()->Increment();
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;
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;
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;
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;
108 void StubCache::CollectMatchingMaps(SmallMapList* types, Handle<Name> name,
110 Handle<Context> native_context,
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;
119 int offset = PrimaryOffset(*name, flags, map);
120 if (entry(primary_, offset) == &primary_[i] &&
121 TypeFeedbackOracle::IsRelevantFeedback(map, *native_context)) {
122 types->AddMapIfMissing(Handle<Map>(map), zone);
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;
134 // Lookup in primary table and skip duplicates.
135 int primary_offset = PrimaryOffset(*name, flags, map);
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::IsRelevantFeedback(map, *native_context)) {
141 types->AddMapIfMissing(Handle<Map>(map), zone);
147 } // namespace v8::internal