Line data Source code
1 : // Copyright 2014 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 : #ifndef V8_LAYOUT_DESCRIPTOR_INL_H_
6 : #define V8_LAYOUT_DESCRIPTOR_INL_H_
7 :
8 : #include "src/layout-descriptor.h"
9 :
10 : namespace v8 {
11 : namespace internal {
12 :
13 : LayoutDescriptor* LayoutDescriptor::FromSmi(Smi* smi) {
14 : return LayoutDescriptor::cast(smi);
15 : }
16 :
17 :
18 : Handle<LayoutDescriptor> LayoutDescriptor::New(Isolate* isolate, int length) {
19 3525899 : if (length <= kSmiValueSize) {
20 : // The whole bit vector fits into a smi.
21 : return handle(LayoutDescriptor::FromSmi(Smi::kZero), isolate);
22 : }
23 : length = GetSlowModeBackingStoreLength(length);
24 : return Handle<LayoutDescriptor>::cast(isolate->factory()->NewFixedTypedArray(
25 578 : length, kExternalUint32Array, true));
26 : }
27 :
28 :
29 : bool LayoutDescriptor::InobjectUnboxedField(int inobject_properties,
30 : PropertyDetails details) {
31 79944645 : if (details.location() != kField || !details.representation().IsDouble()) {
32 : return false;
33 : }
34 : // We care only about in-object properties.
35 115240 : return details.field_index() < inobject_properties;
36 : }
37 :
38 :
39 : LayoutDescriptor* LayoutDescriptor::FastPointerLayout() {
40 : return LayoutDescriptor::FromSmi(Smi::kZero);
41 : }
42 :
43 :
44 : bool LayoutDescriptor::GetIndexes(int field_index, int* layout_word_index,
45 : int* layout_bit_index) {
46 4054829 : if (static_cast<unsigned>(field_index) >= static_cast<unsigned>(capacity())) {
47 : return false;
48 : }
49 :
50 1963188 : *layout_word_index = field_index / kNumberOfBits;
51 1963188 : CHECK((!IsSmi() && (*layout_word_index < length())) ||
52 : (IsSmi() && (*layout_word_index < 1)));
53 :
54 1963188 : *layout_bit_index = field_index % kNumberOfBits;
55 : return true;
56 : }
57 :
58 :
59 : LayoutDescriptor* LayoutDescriptor::SetRawData(int field_index) {
60 : return SetTagged(field_index, false);
61 : }
62 :
63 :
64 : LayoutDescriptor* LayoutDescriptor::SetTagged(int field_index, bool tagged) {
65 : int layout_word_index = 0;
66 : int layout_bit_index = 0;
67 :
68 236753 : if (!GetIndexes(field_index, &layout_word_index, &layout_bit_index)) {
69 0 : CHECK(false);
70 : return this;
71 : }
72 236753 : uint32_t layout_mask = static_cast<uint32_t>(1) << layout_bit_index;
73 :
74 236753 : if (IsSlowLayout()) {
75 209253 : uint32_t value = get_scalar(layout_word_index);
76 202020 : if (tagged) {
77 93702 : value &= ~layout_mask;
78 : } else {
79 115551 : value |= layout_mask;
80 : }
81 209253 : set(layout_word_index, value);
82 : return this;
83 : } else {
84 27500 : uint32_t value = static_cast<uint32_t>(Smi::cast(this)->value());
85 3808 : if (tagged) {
86 1925 : value &= ~layout_mask;
87 : } else {
88 25575 : value |= layout_mask;
89 : }
90 27500 : return LayoutDescriptor::FromSmi(Smi::FromInt(static_cast<int>(value)));
91 : }
92 : }
93 :
94 :
95 : bool LayoutDescriptor::IsTagged(int field_index) {
96 117573822 : if (IsFastPointerLayout()) return true;
97 :
98 : int layout_word_index;
99 : int layout_bit_index;
100 :
101 1069172 : if (!GetIndexes(field_index, &layout_word_index, &layout_bit_index)) {
102 : // All bits after Out of bounds queries
103 : return true;
104 : }
105 996813 : uint32_t layout_mask = static_cast<uint32_t>(1) << layout_bit_index;
106 :
107 996813 : if (IsSlowLayout()) {
108 235481 : uint32_t value = get_scalar(layout_word_index);
109 235481 : return (value & layout_mask) == 0;
110 : } else {
111 761332 : uint32_t value = static_cast<uint32_t>(Smi::cast(this)->value());
112 761332 : return (value & layout_mask) == 0;
113 : }
114 : }
115 :
116 :
117 : bool LayoutDescriptor::IsFastPointerLayout() {
118 : return this == FastPointerLayout();
119 : }
120 :
121 :
122 : bool LayoutDescriptor::IsFastPointerLayout(Object* layout_descriptor) {
123 : return layout_descriptor == FastPointerLayout();
124 : }
125 :
126 :
127 : bool LayoutDescriptor::IsSlowLayout() { return !IsSmi(); }
128 :
129 :
130 : int LayoutDescriptor::capacity() {
131 2170830 : return IsSlowLayout() ? (length() * kNumberOfBits) : kSmiValueSize;
132 : }
133 :
134 :
135 : LayoutDescriptor* LayoutDescriptor::cast_gc_safe(Object* object) {
136 239461 : if (object->IsSmi()) {
137 : // Fast mode layout descriptor.
138 : return reinterpret_cast<LayoutDescriptor*>(object);
139 : }
140 :
141 : // This is a mixed descriptor which is a fixed typed array.
142 179 : MapWord map_word = reinterpret_cast<HeapObject*>(object)->map_word();
143 179 : if (map_word.IsForwardingAddress()) {
144 : // Mark-compact has already moved layout descriptor.
145 88 : object = map_word.ToForwardingAddress();
146 : }
147 : return LayoutDescriptor::cast(object);
148 : }
149 :
150 :
151 : int LayoutDescriptor::GetSlowModeBackingStoreLength(int length) {
152 615 : length = (length + kNumberOfBits - 1) / kNumberOfBits;
153 : DCHECK_LT(0, length);
154 :
155 615 : if (SmiValuesAre32Bits() && (length & 1)) {
156 : // On 64-bit systems if the length is odd then the half-word space would be
157 : // lost anyway (due to alignment and the fact that we are allocating
158 : // uint32-typed array), so we increase the length of allocated array
159 : // to utilize that "lost" space which could also help to avoid layout
160 : // descriptor reallocations.
161 109 : ++length;
162 : }
163 : return length;
164 : }
165 :
166 :
167 : int LayoutDescriptor::CalculateCapacity(Map* map, DescriptorArray* descriptors,
168 : int num_descriptors) {
169 6795798 : int inobject_properties = map->GetInObjectProperties();
170 6795801 : if (inobject_properties == 0) return 0;
171 :
172 : DCHECK_LE(num_descriptors, descriptors->number_of_descriptors());
173 :
174 : int layout_descriptor_length;
175 : const int kMaxWordsPerField = kDoubleSize / kPointerSize;
176 :
177 3641004 : if (num_descriptors <= kSmiValueSize / kMaxWordsPerField) {
178 : // Even in the "worst" case (all fields are doubles) it would fit into
179 : // a Smi, so no need to calculate length.
180 : layout_descriptor_length = kSmiValueSize;
181 :
182 : } else {
183 : layout_descriptor_length = 0;
184 :
185 54004856 : for (int i = 0; i < num_descriptors; i++) {
186 54004856 : PropertyDetails details = descriptors->GetDetails(i);
187 107999520 : if (!InobjectUnboxedField(inobject_properties, details)) continue;
188 10192 : int field_index = details.field_index();
189 10192 : int field_width_in_words = details.field_width_in_words();
190 : layout_descriptor_length =
191 10192 : Max(layout_descriptor_length, field_index + field_width_in_words);
192 : }
193 : }
194 3641004 : layout_descriptor_length = Min(layout_descriptor_length, inobject_properties);
195 : return layout_descriptor_length;
196 : }
197 :
198 :
199 : LayoutDescriptor* LayoutDescriptor::Initialize(
200 : LayoutDescriptor* layout_descriptor, Map* map, DescriptorArray* descriptors,
201 : int num_descriptors) {
202 : DisallowHeapAllocation no_allocation;
203 3525520 : int inobject_properties = map->GetInObjectProperties();
204 :
205 16156187 : for (int i = 0; i < num_descriptors; i++) {
206 16156185 : PropertyDetails details = descriptors->GetDetails(i);
207 16156187 : if (!InobjectUnboxedField(inobject_properties, details)) {
208 : DCHECK(details.location() != kField ||
209 : layout_descriptor->IsTagged(details.field_index()));
210 16142087 : continue;
211 : }
212 14100 : int field_index = details.field_index();
213 : layout_descriptor = layout_descriptor->SetRawData(field_index);
214 14100 : if (details.field_width_in_words() > 1) {
215 0 : layout_descriptor = layout_descriptor->SetRawData(field_index + 1);
216 : }
217 : }
218 : return layout_descriptor;
219 : }
220 :
221 :
222 : // LayoutDescriptorHelper is a helper class for querying whether inobject
223 : // property at offset is Double or not.
224 239461 : LayoutDescriptorHelper::LayoutDescriptorHelper(Map* map)
225 : : all_fields_tagged_(true),
226 : header_size_(0),
227 239461 : layout_descriptor_(LayoutDescriptor::FastPointerLayout()) {
228 : if (!FLAG_unbox_double_fields) return;
229 :
230 239461 : layout_descriptor_ = map->layout_descriptor_gc_safe();
231 239461 : if (layout_descriptor_->IsFastPointerLayout()) {
232 : return;
233 : }
234 :
235 : int inobject_properties = map->GetInObjectProperties();
236 : DCHECK(inobject_properties > 0);
237 239461 : header_size_ = map->instance_size() - (inobject_properties * kPointerSize);
238 : DCHECK(header_size_ >= 0);
239 :
240 239461 : all_fields_tagged_ = false;
241 : }
242 :
243 :
244 0 : bool LayoutDescriptorHelper::IsTagged(int offset_in_bytes) {
245 : DCHECK(IsAligned(offset_in_bytes, kPointerSize));
246 0 : if (all_fields_tagged_) return true;
247 : // Object headers do not contain non-tagged fields.
248 0 : if (offset_in_bytes < header_size_) return true;
249 0 : int field_index = (offset_in_bytes - header_size_) / kPointerSize;
250 :
251 0 : return layout_descriptor_->IsTagged(field_index);
252 : }
253 : } // namespace internal
254 : } // namespace v8
255 :
256 : #endif // V8_LAYOUT_DESCRIPTOR_INL_H_
|