OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_STUB_CACHE_H_ | 5 #ifndef V8_STUB_CACHE_H_ |
6 #define V8_STUB_CACHE_H_ | 6 #define V8_STUB_CACHE_H_ |
7 | 7 |
8 #include "src/macro-assembler.h" | 8 #include "src/macro-assembler.h" |
9 | 9 |
10 namespace v8 { | 10 namespace v8 { |
(...skipping 23 matching lines...) Expand all Loading... |
34 public: | 34 public: |
35 struct Entry { | 35 struct Entry { |
36 Name* key; | 36 Name* key; |
37 Code* value; | 37 Code* value; |
38 Map* map; | 38 Map* map; |
39 }; | 39 }; |
40 | 40 |
41 void Initialize(); | 41 void Initialize(); |
42 // Access cache for entry hash(name, map). | 42 // Access cache for entry hash(name, map). |
43 Code* Set(Name* name, Map* map, Code* code); | 43 Code* Set(Name* name, Map* map, Code* code); |
44 Code* Get(Name* name, Map* map, Code::Flags flags); | 44 Code* Get(Name* name, Map* map); |
45 // Clear the lookup table (@ mark compact collection). | 45 // Clear the lookup table (@ mark compact collection). |
46 void Clear(); | 46 void Clear(); |
47 // Collect all maps that match the name and flags. | 47 // Collect all maps that match the name. |
48 void CollectMatchingMaps(SmallMapList* types, Handle<Name> name, | 48 void CollectMatchingMaps(SmallMapList* types, Handle<Name> name, |
49 Code::Flags flags, Handle<Context> native_context, | 49 Handle<Context> native_context, Zone* zone); |
50 Zone* zone); | |
51 // Generate code for probing the stub cache table. | 50 // Generate code for probing the stub cache table. |
52 // Arguments extra, extra2 and extra3 may be used to pass additional scratch | 51 // Arguments extra, extra2 and extra3 may be used to pass additional scratch |
53 // registers. Set to no_reg if not needed. | 52 // registers. Set to no_reg if not needed. |
54 // If leave_frame is true, then exit a frame before the tail call. | 53 // If leave_frame is true, then exit a frame before the tail call. |
55 void GenerateProbe(MacroAssembler* masm, Register receiver, Register name, | 54 void GenerateProbe(MacroAssembler* masm, Register receiver, Register name, |
56 Register scratch, Register extra, Register extra2 = no_reg, | 55 Register scratch, Register extra, Register extra2 = no_reg, |
57 Register extra3 = no_reg); | 56 Register extra3 = no_reg); |
58 | 57 |
59 enum Table { kPrimary, kSecondary }; | 58 enum Table { kPrimary, kSecondary }; |
60 | 59 |
(...skipping 29 matching lines...) Expand all Loading... |
90 // Setting the entry size such that the index is shifted by Name::kHashShift | 89 // Setting the entry size such that the index is shifted by Name::kHashShift |
91 // is convenient; shifting down the length field (to extract the hash code) | 90 // is convenient; shifting down the length field (to extract the hash code) |
92 // automatically discards the hash bit field. | 91 // automatically discards the hash bit field. |
93 static const int kCacheIndexShift = Name::kHashShift; | 92 static const int kCacheIndexShift = Name::kHashShift; |
94 | 93 |
95 static const int kPrimaryTableBits = 11; | 94 static const int kPrimaryTableBits = 11; |
96 static const int kPrimaryTableSize = (1 << kPrimaryTableBits); | 95 static const int kPrimaryTableSize = (1 << kPrimaryTableBits); |
97 static const int kSecondaryTableBits = 9; | 96 static const int kSecondaryTableBits = 9; |
98 static const int kSecondaryTableSize = (1 << kSecondaryTableBits); | 97 static const int kSecondaryTableSize = (1 << kSecondaryTableBits); |
99 | 98 |
100 static int PrimaryOffsetForTesting(Name* name, Code::Flags flags, Map* map) { | 99 static int PrimaryOffsetForTesting(Name* name, Map* map) { |
101 return PrimaryOffset(name, flags, map); | 100 return PrimaryOffset(name, map); |
102 } | 101 } |
103 | 102 |
104 static int SecondaryOffsetForTesting(Name* name, Code::Flags flags, | 103 static int SecondaryOffsetForTesting(Name* name, int seed) { |
105 int seed) { | 104 return SecondaryOffset(name, seed); |
106 return SecondaryOffset(name, flags, seed); | |
107 } | 105 } |
108 | 106 |
109 // The constructor is made public only for the purposes of testing. | 107 // The constructor is made public only for the purposes of testing. |
110 StubCache(Isolate* isolate, Code::Kind ic_kind); | 108 StubCache(Isolate* isolate, Code::Kind ic_kind); |
111 | 109 |
112 private: | 110 private: |
113 // The stub cache has a primary and secondary level. The two levels have | 111 // The stub cache has a primary and secondary level. The two levels have |
114 // different hashing algorithms in order to avoid simultaneous collisions | 112 // different hashing algorithms in order to avoid simultaneous collisions |
115 // in both caches. Unlike a probing strategy (quadratic or otherwise) the | 113 // in both caches. Unlike a probing strategy (quadratic or otherwise) the |
116 // update strategy on updates is fairly clear and simple: Any existing entry | 114 // update strategy on updates is fairly clear and simple: Any existing entry |
117 // in the primary cache is moved to the secondary cache, and secondary cache | 115 // in the primary cache is moved to the secondary cache, and secondary cache |
118 // entries are overwritten. | 116 // entries are overwritten. |
119 | 117 |
120 // Hash algorithm for the primary table. This algorithm is replicated in | 118 // Hash algorithm for the primary table. This algorithm is replicated in |
121 // assembler for every architecture. Returns an index into the table that | 119 // assembler for every architecture. Returns an index into the table that |
122 // is scaled by 1 << kCacheIndexShift. | 120 // is scaled by 1 << kCacheIndexShift. |
123 static int PrimaryOffset(Name* name, Code::Flags flags, Map* map) { | 121 static int PrimaryOffset(Name* name, Map* map) { |
124 STATIC_ASSERT(kCacheIndexShift == Name::kHashShift); | 122 STATIC_ASSERT(kCacheIndexShift == Name::kHashShift); |
125 // Compute the hash of the name (use entire hash field). | 123 // Compute the hash of the name (use entire hash field). |
126 DCHECK(name->HasHashCode()); | 124 DCHECK(name->HasHashCode()); |
127 uint32_t field = name->hash_field(); | 125 uint32_t field = name->hash_field(); |
128 // Using only the low bits in 64-bit mode is unlikely to increase the | 126 // Using only the low bits in 64-bit mode is unlikely to increase the |
129 // risk of collision even if the heap is spread over an area larger than | 127 // risk of collision even if the heap is spread over an area larger than |
130 // 4Gb (and not at all if it isn't). | 128 // 4Gb (and not at all if it isn't). |
131 uint32_t map_low32bits = | 129 uint32_t map_low32bits = |
132 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(map)); | 130 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(map)); |
133 // We always set the in_loop bit to zero when generating the lookup code | 131 // Base the offset on a simple combination of name and map. |
134 // so do it here too so the hash codes match. | 132 uint32_t key = map_low32bits + field; |
135 uint32_t iflags = | |
136 (static_cast<uint32_t>(flags) & ~Code::kFlagsNotUsedInLookup); | |
137 // Base the offset on a simple combination of name, flags, and map. | |
138 uint32_t key = (map_low32bits + field) ^ iflags; | |
139 return key & ((kPrimaryTableSize - 1) << kCacheIndexShift); | 133 return key & ((kPrimaryTableSize - 1) << kCacheIndexShift); |
140 } | 134 } |
141 | 135 |
142 // Hash algorithm for the secondary table. This algorithm is replicated in | 136 // Hash algorithm for the secondary table. This algorithm is replicated in |
143 // assembler for every architecture. Returns an index into the table that | 137 // assembler for every architecture. Returns an index into the table that |
144 // is scaled by 1 << kCacheIndexShift. | 138 // is scaled by 1 << kCacheIndexShift. |
145 static int SecondaryOffset(Name* name, Code::Flags flags, int seed) { | 139 static int SecondaryOffset(Name* name, int seed) { |
146 // Use the seed from the primary cache in the secondary cache. | 140 // Use the seed from the primary cache in the secondary cache. |
147 uint32_t name_low32bits = | 141 uint32_t name_low32bits = |
148 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name)); | 142 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name)); |
149 // We always set the in_loop bit to zero when generating the lookup code | 143 uint32_t key = (seed - name_low32bits); |
150 // so do it here too so the hash codes match. | |
151 uint32_t iflags = | |
152 (static_cast<uint32_t>(flags) & ~Code::kFlagsNotUsedInLookup); | |
153 uint32_t key = (seed - name_low32bits) + iflags; | |
154 return key & ((kSecondaryTableSize - 1) << kCacheIndexShift); | 144 return key & ((kSecondaryTableSize - 1) << kCacheIndexShift); |
155 } | 145 } |
156 | 146 |
157 // Compute the entry for a given offset in exactly the same way as | 147 // Compute the entry for a given offset in exactly the same way as |
158 // we do in generated code. We generate an hash code that already | 148 // we do in generated code. We generate an hash code that already |
159 // ends in Name::kHashShift 0s. Then we multiply it so it is a multiple | 149 // ends in Name::kHashShift 0s. Then we multiply it so it is a multiple |
160 // of sizeof(Entry). This makes it easier to avoid making mistakes | 150 // of sizeof(Entry). This makes it easier to avoid making mistakes |
161 // in the hashed offset computations. | 151 // in the hashed offset computations. |
162 static Entry* entry(Entry* table, int offset) { | 152 static Entry* entry(Entry* table, int offset) { |
163 const int multiplier = sizeof(*table) >> Name::kHashShift; | 153 const int multiplier = sizeof(*table) >> Name::kHashShift; |
164 return reinterpret_cast<Entry*>(reinterpret_cast<Address>(table) + | 154 return reinterpret_cast<Entry*>(reinterpret_cast<Address>(table) + |
165 offset * multiplier); | 155 offset * multiplier); |
166 } | 156 } |
167 | 157 |
168 private: | 158 private: |
169 Entry primary_[kPrimaryTableSize]; | 159 Entry primary_[kPrimaryTableSize]; |
170 Entry secondary_[kSecondaryTableSize]; | 160 Entry secondary_[kSecondaryTableSize]; |
171 Isolate* isolate_; | 161 Isolate* isolate_; |
172 Code::Kind ic_kind_; | 162 Code::Kind ic_kind_; |
173 | 163 |
174 friend class Isolate; | 164 friend class Isolate; |
175 friend class SCTableReference; | 165 friend class SCTableReference; |
176 | 166 |
177 DISALLOW_COPY_AND_ASSIGN(StubCache); | 167 DISALLOW_COPY_AND_ASSIGN(StubCache); |
178 }; | 168 }; |
179 } // namespace internal | 169 } // namespace internal |
180 } // namespace v8 | 170 } // namespace v8 |
181 | 171 |
182 #endif // V8_STUB_CACHE_H_ | 172 #endif // V8_STUB_CACHE_H_ |
OLD | NEW |