blob: 5d71c1fe6b2a5d422402d929c93746c38d98151d [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));
37 STATIC_ASSERT((Code::ICStateField::kMask & 1) == 1);
38
Ben Murdochc5610432016-08-08 18:44:38 +010039 // Make sure that the cache holder are not included in the hash.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040 DCHECK(Code::ExtractCacheHolderFromFlags(flags) == 0);
41
42 return flags;
43}
44
45
46Code* StubCache::Set(Name* name, Map* map, Code* code) {
47 Code::Flags flags = CommonStubCacheChecks(name, map, code->flags());
48
49 // Compute the primary entry.
50 int primary_offset = PrimaryOffset(name, flags, map);
51 Entry* primary = entry(primary_, primary_offset);
52 Code* old_code = primary->value;
53
54 // If the primary entry has useful data in it, we retire it to the
55 // secondary cache before overwriting it.
56 if (old_code != isolate_->builtins()->builtin(Builtins::kIllegal)) {
57 Map* old_map = primary->map;
Ben Murdochc5610432016-08-08 18:44:38 +010058 Code::Flags old_flags = Code::RemoveHolderFromFlags(old_code->flags());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000059 int seed = PrimaryOffset(primary->key, old_flags, old_map);
60 int secondary_offset = SecondaryOffset(primary->key, old_flags, seed);
61 Entry* secondary = entry(secondary_, secondary_offset);
62 *secondary = *primary;
63 }
64
65 // Update primary cache.
66 primary->key = name;
67 primary->value = code;
68 primary->map = map;
69 isolate()->counters()->megamorphic_stub_cache_updates()->Increment();
70 return code;
71}
72
73
74Code* StubCache::Get(Name* name, Map* map, Code::Flags flags) {
75 flags = CommonStubCacheChecks(name, map, flags);
76 int primary_offset = PrimaryOffset(name, flags, map);
77 Entry* primary = entry(primary_, primary_offset);
78 if (primary->key == name && primary->map == map) {
79 return primary->value;
80 }
81 int secondary_offset = SecondaryOffset(name, flags, primary_offset);
82 Entry* secondary = entry(secondary_, secondary_offset);
83 if (secondary->key == name && secondary->map == map) {
84 return secondary->value;
85 }
86 return NULL;
87}
88
89
90void StubCache::Clear() {
91 Code* empty = isolate_->builtins()->builtin(Builtins::kIllegal);
92 for (int i = 0; i < kPrimaryTableSize; i++) {
93 primary_[i].key = isolate()->heap()->empty_string();
94 primary_[i].map = NULL;
95 primary_[i].value = empty;
96 }
97 for (int j = 0; j < kSecondaryTableSize; j++) {
98 secondary_[j].key = isolate()->heap()->empty_string();
99 secondary_[j].map = NULL;
100 secondary_[j].value = empty;
101 }
102}
103
104
105void StubCache::CollectMatchingMaps(SmallMapList* types, Handle<Name> name,
106 Code::Flags flags,
107 Handle<Context> native_context,
108 Zone* zone) {
109 for (int i = 0; i < kPrimaryTableSize; i++) {
110 if (primary_[i].key == *name) {
111 Map* map = primary_[i].map;
112 // Map can be NULL, if the stub is constant function call
113 // with a primitive receiver.
114 if (map == NULL) continue;
115
116 int offset = PrimaryOffset(*name, flags, map);
117 if (entry(primary_, offset) == &primary_[i] &&
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000118 TypeFeedbackOracle::IsRelevantFeedback(map, *native_context)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000119 types->AddMapIfMissing(Handle<Map>(map), zone);
120 }
121 }
122 }
123
124 for (int i = 0; i < kSecondaryTableSize; i++) {
125 if (secondary_[i].key == *name) {
126 Map* map = secondary_[i].map;
127 // Map can be NULL, if the stub is constant function call
128 // with a primitive receiver.
129 if (map == NULL) continue;
130
131 // Lookup in primary table and skip duplicates.
132 int primary_offset = PrimaryOffset(*name, flags, map);
133
134 // Lookup in secondary table and add matches.
135 int offset = SecondaryOffset(*name, flags, primary_offset);
136 if (entry(secondary_, offset) == &secondary_[i] &&
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000137 TypeFeedbackOracle::IsRelevantFeedback(map, *native_context)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000138 types->AddMapIfMissing(Handle<Map>(map), zone);
139 }
140 }
141 }
142}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000143} // namespace internal
144} // namespace v8