blob: 42b9182d55f40f78059e29cac8c85549780b9fd9 [file] [log] [blame]
Calin Juravlec416d332015-04-23 16:01:43 +01001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Calin Juravlec416d332015-04-23 16:01:43 +010016#include "stack_map_stream.h"
17
18namespace art {
19
Calin Juravle4f46ac52015-04-23 18:47:21 +010020void StackMapStream::BeginStackMapEntry(uint32_t dex_pc,
21 uint32_t native_pc_offset,
22 uint32_t register_mask,
23 BitVector* sp_mask,
24 uint32_t num_dex_registers,
25 uint8_t inlining_depth) {
26 DCHECK_EQ(0u, current_entry_.dex_pc) << "EndStackMapEntry not called after BeginStackMapEntry";
27 current_entry_.dex_pc = dex_pc;
28 current_entry_.native_pc_offset = native_pc_offset;
29 current_entry_.register_mask = register_mask;
30 current_entry_.sp_mask = sp_mask;
31 current_entry_.num_dex_registers = num_dex_registers;
32 current_entry_.inlining_depth = inlining_depth;
33 current_entry_.dex_register_locations_start_index = dex_register_locations_.Size();
34 current_entry_.inline_infos_start_index = inline_infos_.Size();
35 current_entry_.dex_register_map_hash = 0;
36 current_entry_.same_dex_register_map_as_ = kNoSameDexMapFound;
Calin Juravlec416d332015-04-23 16:01:43 +010037 if (num_dex_registers != 0) {
Calin Juravle4f46ac52015-04-23 18:47:21 +010038 current_entry_.live_dex_registers_mask =
Calin Juravlec416d332015-04-23 16:01:43 +010039 new (allocator_) ArenaBitVector(allocator_, num_dex_registers, true);
40 } else {
Calin Juravle4f46ac52015-04-23 18:47:21 +010041 current_entry_.live_dex_registers_mask = nullptr;
Calin Juravlec416d332015-04-23 16:01:43 +010042 }
Calin Juravlec416d332015-04-23 16:01:43 +010043
44 if (sp_mask != nullptr) {
45 stack_mask_max_ = std::max(stack_mask_max_, sp_mask->GetHighestBitSet());
46 }
47 if (inlining_depth > 0) {
48 number_of_stack_maps_with_inline_info_++;
49 }
50
51 dex_pc_max_ = std::max(dex_pc_max_, dex_pc);
52 native_pc_offset_max_ = std::max(native_pc_offset_max_, native_pc_offset);
53 register_mask_max_ = std::max(register_mask_max_, register_mask);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010054 current_dex_register_ = 0;
Calin Juravlec416d332015-04-23 16:01:43 +010055}
56
Calin Juravle4f46ac52015-04-23 18:47:21 +010057void StackMapStream::EndStackMapEntry() {
58 current_entry_.same_dex_register_map_as_ = FindEntryWithTheSameDexMap();
59 stack_maps_.Add(current_entry_);
60 current_entry_ = StackMapEntry();
61}
62
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010063void StackMapStream::AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value) {
Calin Juravlec416d332015-04-23 16:01:43 +010064 if (kind != DexRegisterLocation::Kind::kNone) {
65 // Ensure we only use non-compressed location kind at this stage.
66 DCHECK(DexRegisterLocation::IsShortLocationKind(kind))
67 << DexRegisterLocation::PrettyDescriptor(kind);
68 DexRegisterLocation location(kind, value);
69
70 // Look for Dex register `location` in the location catalog (using the
71 // companion hash map of locations to indices). Use its index if it
72 // is already in the location catalog. If not, insert it (in the
73 // location catalog and the hash map) and use the newly created index.
74 auto it = location_catalog_entries_indices_.Find(location);
75 if (it != location_catalog_entries_indices_.end()) {
76 // Retrieve the index from the hash map.
77 dex_register_locations_.Add(it->second);
78 } else {
79 // Create a new entry in the location catalog and the hash map.
80 size_t index = location_catalog_entries_.Size();
81 location_catalog_entries_.Add(location);
82 dex_register_locations_.Add(index);
83 location_catalog_entries_indices_.Insert(std::make_pair(location, index));
84 }
85
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010086 if (in_inline_frame_) {
87 // TODO: Support sharing DexRegisterMap across InlineInfo.
88 DCHECK_LT(current_dex_register_, current_inline_info_.num_dex_registers);
89 current_inline_info_.live_dex_registers_mask->SetBit(current_dex_register_);
90 } else {
91 DCHECK_LT(current_dex_register_, current_entry_.num_dex_registers);
92 current_entry_.live_dex_registers_mask->SetBit(current_dex_register_);
93 current_entry_.dex_register_map_hash += (1 <<
94 (current_dex_register_ % (sizeof(current_entry_.dex_register_map_hash) * kBitsPerByte)));
95 current_entry_.dex_register_map_hash += static_cast<uint32_t>(value);
96 current_entry_.dex_register_map_hash += static_cast<uint32_t>(kind);
97 }
Calin Juravlec416d332015-04-23 16:01:43 +010098 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010099 current_dex_register_++;
Calin Juravlec416d332015-04-23 16:01:43 +0100100}
101
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100102void StackMapStream::BeginInlineInfoEntry(uint32_t method_index,
103 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100104 InvokeType invoke_type,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100105 uint32_t num_dex_registers) {
106 DCHECK(!in_inline_frame_);
107 in_inline_frame_ = true;
108 current_inline_info_.method_index = method_index;
109 current_inline_info_.dex_pc = dex_pc;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100110 current_inline_info_.invoke_type = invoke_type;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100111 current_inline_info_.num_dex_registers = num_dex_registers;
112 current_inline_info_.dex_register_locations_start_index = dex_register_locations_.Size();
113 if (num_dex_registers != 0) {
114 current_inline_info_.live_dex_registers_mask =
115 new (allocator_) ArenaBitVector(allocator_, num_dex_registers, true);
116 } else {
117 current_inline_info_.live_dex_registers_mask = nullptr;
118 }
119 current_dex_register_ = 0;
120}
121
122void StackMapStream::EndInlineInfoEntry() {
123 DCHECK(in_inline_frame_);
124 DCHECK_EQ(current_dex_register_, current_inline_info_.num_dex_registers)
125 << "Inline information contains less registers than expected";
126 in_inline_frame_ = false;
127 inline_infos_.Add(current_inline_info_);
128 current_inline_info_ = InlineInfoEntry();
Calin Juravlec416d332015-04-23 16:01:43 +0100129}
130
Calin Juravle4f46ac52015-04-23 18:47:21 +0100131size_t StackMapStream::PrepareForFillIn() {
132 int stack_mask_number_of_bits = stack_mask_max_ + 1; // Need room for max element too.
133 stack_mask_size_ = RoundUp(stack_mask_number_of_bits, kBitsPerByte) / kBitsPerByte;
134 inline_info_size_ = ComputeInlineInfoSize();
135 dex_register_maps_size_ = ComputeDexRegisterMapsSize();
David Brazdilf677ebf2015-05-29 16:29:43 +0100136 stack_map_encoding_ = StackMapEncoding::CreateFromSizes(stack_mask_size_,
137 inline_info_size_,
138 dex_register_maps_size_,
139 dex_pc_max_,
140 native_pc_offset_max_,
141 register_mask_max_);
142 stack_maps_size_ = stack_maps_.Size() * stack_map_encoding_.ComputeStackMapSize();
Calin Juravle4f46ac52015-04-23 18:47:21 +0100143 dex_register_location_catalog_size_ = ComputeDexRegisterLocationCatalogSize();
144
Calin Juravlec416d332015-04-23 16:01:43 +0100145 // Note: use RoundUp to word-size here if you want CodeInfo objects to be word aligned.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100146 needed_size_ = CodeInfo::kFixedSize
Calin Juravle4f46ac52015-04-23 18:47:21 +0100147 + stack_maps_size_
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100148 + dex_register_location_catalog_size_
Calin Juravle4f46ac52015-04-23 18:47:21 +0100149 + dex_register_maps_size_
150 + inline_info_size_;
Calin Juravlec416d332015-04-23 16:01:43 +0100151
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100152 stack_maps_start_ = CodeInfo::kFixedSize;
153 // TODO: Move the catalog at the end. It is currently too expensive at runtime
154 // to compute its size (note that we do not encode that size in the CodeInfo).
155 dex_register_location_catalog_start_ = stack_maps_start_ + stack_maps_size_;
156 dex_register_maps_start_ =
157 dex_register_location_catalog_start_ + dex_register_location_catalog_size_;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100158 inline_infos_start_ = dex_register_maps_start_ + dex_register_maps_size_;
Calin Juravlec416d332015-04-23 16:01:43 +0100159
Calin Juravle4f46ac52015-04-23 18:47:21 +0100160 return needed_size_;
Calin Juravlec416d332015-04-23 16:01:43 +0100161}
162
163size_t StackMapStream::ComputeDexRegisterLocationCatalogSize() const {
164 size_t size = DexRegisterLocationCatalog::kFixedSize;
165 for (size_t location_catalog_entry_index = 0;
166 location_catalog_entry_index < location_catalog_entries_.Size();
167 ++location_catalog_entry_index) {
168 DexRegisterLocation dex_register_location =
169 location_catalog_entries_.Get(location_catalog_entry_index);
170 size += DexRegisterLocationCatalog::EntrySize(dex_register_location);
171 }
172 return size;
173}
174
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100175size_t StackMapStream::ComputeDexRegisterMapSize(uint32_t num_dex_registers,
176 const BitVector& live_dex_registers_mask) const {
Calin Juravlec416d332015-04-23 16:01:43 +0100177 // Size of the map in bytes.
178 size_t size = DexRegisterMap::kFixedSize;
179 // Add the live bit mask for the Dex register liveness.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100180 size += DexRegisterMap::GetLiveBitMaskSize(num_dex_registers);
Calin Juravlec416d332015-04-23 16:01:43 +0100181 // Compute the size of the set of live Dex register entries.
182 size_t number_of_live_dex_registers = 0;
183 for (size_t dex_register_number = 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100184 dex_register_number < num_dex_registers;
Calin Juravlec416d332015-04-23 16:01:43 +0100185 ++dex_register_number) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100186 if (live_dex_registers_mask.IsBitSet(dex_register_number)) {
Calin Juravlec416d332015-04-23 16:01:43 +0100187 ++number_of_live_dex_registers;
188 }
189 }
190 size_t map_entries_size_in_bits =
191 DexRegisterMap::SingleEntrySizeInBits(location_catalog_entries_.Size())
192 * number_of_live_dex_registers;
193 size_t map_entries_size_in_bytes =
194 RoundUp(map_entries_size_in_bits, kBitsPerByte) / kBitsPerByte;
195 size += map_entries_size_in_bytes;
196 return size;
197}
198
Calin Juravle4f46ac52015-04-23 18:47:21 +0100199size_t StackMapStream::ComputeDexRegisterMapsSize() const {
Calin Juravlec416d332015-04-23 16:01:43 +0100200 size_t size = 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100201 size_t inline_info_index = 0;
Calin Juravlec416d332015-04-23 16:01:43 +0100202 for (size_t i = 0; i < stack_maps_.Size(); ++i) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100203 StackMapEntry entry = stack_maps_.Get(i);
204 if (entry.same_dex_register_map_as_ == kNoSameDexMapFound) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100205 size += ComputeDexRegisterMapSize(entry.num_dex_registers, *entry.live_dex_registers_mask);
206 } else {
Calin Juravlec416d332015-04-23 16:01:43 +0100207 // Entries with the same dex map will have the same offset.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100208 }
209 for (size_t j = 0; j < entry.inlining_depth; ++j) {
210 InlineInfoEntry inline_entry = inline_infos_.Get(inline_info_index++);
211 size += ComputeDexRegisterMapSize(inline_entry.num_dex_registers,
212 *inline_entry.live_dex_registers_mask);
Calin Juravlec416d332015-04-23 16:01:43 +0100213 }
214 }
215 return size;
216}
217
218size_t StackMapStream::ComputeInlineInfoSize() const {
219 return inline_infos_.Size() * InlineInfo::SingleEntrySize()
220 // For encoding the depth.
221 + (number_of_stack_maps_with_inline_info_ * InlineInfo::kFixedSize);
222}
223
Calin Juravlec416d332015-04-23 16:01:43 +0100224void StackMapStream::FillIn(MemoryRegion region) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100225 DCHECK_EQ(0u, current_entry_.dex_pc) << "EndStackMapEntry not called after BeginStackMapEntry";
226 DCHECK_NE(0u, needed_size_) << "PrepareForFillIn not called before FillIn";
227
Calin Juravlec416d332015-04-23 16:01:43 +0100228 CodeInfo code_info(region);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100229 DCHECK_EQ(region.size(), needed_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100230 code_info.SetOverallSize(region.size());
231
Calin Juravlec416d332015-04-23 16:01:43 +0100232 MemoryRegion dex_register_locations_region = region.Subregion(
Calin Juravle4f46ac52015-04-23 18:47:21 +0100233 dex_register_maps_start_, dex_register_maps_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100234
235 MemoryRegion inline_infos_region = region.Subregion(
Calin Juravle4f46ac52015-04-23 18:47:21 +0100236 inline_infos_start_, inline_info_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100237
David Brazdilf677ebf2015-05-29 16:29:43 +0100238 code_info.SetEncoding(stack_map_encoding_);
Calin Juravlec416d332015-04-23 16:01:43 +0100239 code_info.SetNumberOfStackMaps(stack_maps_.Size());
David Brazdilf677ebf2015-05-29 16:29:43 +0100240 DCHECK_EQ(code_info.GetStackMapsSize(code_info.ExtractEncoding()), stack_maps_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100241
242 // Set the Dex register location catalog.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100243 code_info.SetNumberOfDexRegisterLocationCatalogEntries(location_catalog_entries_.Size());
Calin Juravlec416d332015-04-23 16:01:43 +0100244 MemoryRegion dex_register_location_catalog_region = region.Subregion(
Calin Juravle4f46ac52015-04-23 18:47:21 +0100245 dex_register_location_catalog_start_, dex_register_location_catalog_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100246 DexRegisterLocationCatalog dex_register_location_catalog(dex_register_location_catalog_region);
247 // Offset in `dex_register_location_catalog` where to store the next
248 // register location.
249 size_t location_catalog_offset = DexRegisterLocationCatalog::kFixedSize;
250 for (size_t i = 0, e = location_catalog_entries_.Size(); i < e; ++i) {
251 DexRegisterLocation dex_register_location = location_catalog_entries_.Get(i);
252 dex_register_location_catalog.SetRegisterInfo(location_catalog_offset, dex_register_location);
253 location_catalog_offset += DexRegisterLocationCatalog::EntrySize(dex_register_location);
254 }
255 // Ensure we reached the end of the Dex registers location_catalog.
256 DCHECK_EQ(location_catalog_offset, dex_register_location_catalog_region.size());
257
258 uintptr_t next_dex_register_map_offset = 0;
259 uintptr_t next_inline_info_offset = 0;
260 for (size_t i = 0, e = stack_maps_.Size(); i < e; ++i) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100261 StackMap stack_map = code_info.GetStackMapAt(i, stack_map_encoding_);
Calin Juravlec416d332015-04-23 16:01:43 +0100262 StackMapEntry entry = stack_maps_.Get(i);
263
David Brazdilf677ebf2015-05-29 16:29:43 +0100264 stack_map.SetDexPc(stack_map_encoding_, entry.dex_pc);
265 stack_map.SetNativePcOffset(stack_map_encoding_, entry.native_pc_offset);
266 stack_map.SetRegisterMask(stack_map_encoding_, entry.register_mask);
Calin Juravlec416d332015-04-23 16:01:43 +0100267 if (entry.sp_mask != nullptr) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100268 stack_map.SetStackMask(stack_map_encoding_, *entry.sp_mask);
Calin Juravlec416d332015-04-23 16:01:43 +0100269 }
270
271 if (entry.num_dex_registers == 0) {
272 // No dex map available.
David Brazdilf677ebf2015-05-29 16:29:43 +0100273 stack_map.SetDexRegisterMapOffset(stack_map_encoding_, StackMap::kNoDexRegisterMap);
Calin Juravlec416d332015-04-23 16:01:43 +0100274 } else {
275 // Search for an entry with the same dex map.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100276 if (entry.same_dex_register_map_as_ != kNoSameDexMapFound) {
Calin Juravlec416d332015-04-23 16:01:43 +0100277 // If we have a hit reuse the offset.
David Brazdilf677ebf2015-05-29 16:29:43 +0100278 stack_map.SetDexRegisterMapOffset(
279 stack_map_encoding_,
280 code_info.GetStackMapAt(entry.same_dex_register_map_as_, stack_map_encoding_)
281 .GetDexRegisterMapOffset(stack_map_encoding_));
Calin Juravlec416d332015-04-23 16:01:43 +0100282 } else {
283 // New dex registers maps should be added to the stack map.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100284 MemoryRegion register_region = dex_register_locations_region.Subregion(
285 next_dex_register_map_offset,
286 ComputeDexRegisterMapSize(entry.num_dex_registers, *entry.live_dex_registers_mask));
Calin Juravlec416d332015-04-23 16:01:43 +0100287 next_dex_register_map_offset += register_region.size();
288 DexRegisterMap dex_register_map(register_region);
289 stack_map.SetDexRegisterMapOffset(
David Brazdilf677ebf2015-05-29 16:29:43 +0100290 stack_map_encoding_, register_region.start() - dex_register_locations_region.start());
Calin Juravlec416d332015-04-23 16:01:43 +0100291
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100292 // Set the dex register location.
293 FillInDexRegisterMap(dex_register_map,
294 entry.num_dex_registers,
295 *entry.live_dex_registers_mask,
296 entry.dex_register_locations_start_index);
Calin Juravlec416d332015-04-23 16:01:43 +0100297 }
298 }
299
300 // Set the inlining info.
301 if (entry.inlining_depth != 0) {
302 MemoryRegion inline_region = inline_infos_region.Subregion(
303 next_inline_info_offset,
304 InlineInfo::kFixedSize + entry.inlining_depth * InlineInfo::SingleEntrySize());
305 next_inline_info_offset += inline_region.size();
306 InlineInfo inline_info(inline_region);
307
308 // Currently relative to the dex register map.
309 stack_map.SetInlineDescriptorOffset(
David Brazdilf677ebf2015-05-29 16:29:43 +0100310 stack_map_encoding_, inline_region.start() - dex_register_locations_region.start());
Calin Juravlec416d332015-04-23 16:01:43 +0100311
312 inline_info.SetDepth(entry.inlining_depth);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100313 for (size_t depth = 0; depth < entry.inlining_depth; ++depth) {
314 InlineInfoEntry inline_entry = inline_infos_.Get(depth + entry.inline_infos_start_index);
315 inline_info.SetMethodIndexAtDepth(depth, inline_entry.method_index);
316 inline_info.SetDexPcAtDepth(depth, inline_entry.dex_pc);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100317 inline_info.SetInvokeTypeAtDepth(depth, inline_entry.invoke_type);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100318 if (inline_entry.num_dex_registers == 0) {
319 // No dex map available.
320 inline_info.SetDexRegisterMapOffsetAtDepth(depth, StackMap::kNoDexRegisterMap);
321 DCHECK(inline_entry.live_dex_registers_mask == nullptr);
322 } else {
323 MemoryRegion register_region = dex_register_locations_region.Subregion(
324 next_dex_register_map_offset,
325 ComputeDexRegisterMapSize(inline_entry.num_dex_registers,
326 *inline_entry.live_dex_registers_mask));
327 next_dex_register_map_offset += register_region.size();
328 DexRegisterMap dex_register_map(register_region);
329 inline_info.SetDexRegisterMapOffsetAtDepth(
330 depth, register_region.start() - dex_register_locations_region.start());
331
332 FillInDexRegisterMap(dex_register_map,
333 inline_entry.num_dex_registers,
334 *inline_entry.live_dex_registers_mask,
335 inline_entry.dex_register_locations_start_index);
336 }
Calin Juravlec416d332015-04-23 16:01:43 +0100337 }
338 } else {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100339 if (inline_info_size_ != 0) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100340 stack_map.SetInlineDescriptorOffset(stack_map_encoding_, StackMap::kNoInlineInfo);
Calin Juravlec416d332015-04-23 16:01:43 +0100341 }
342 }
343 }
344}
345
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100346void StackMapStream::FillInDexRegisterMap(DexRegisterMap dex_register_map,
347 uint32_t num_dex_registers,
348 const BitVector& live_dex_registers_mask,
349 uint32_t start_index_in_dex_register_locations) const {
350 dex_register_map.SetLiveBitMask(num_dex_registers, live_dex_registers_mask);
351 // Set the dex register location mapping data.
352 for (size_t dex_register_number = 0, index_in_dex_register_locations = 0;
353 dex_register_number < num_dex_registers;
354 ++dex_register_number) {
355 if (live_dex_registers_mask.IsBitSet(dex_register_number)) {
356 size_t location_catalog_entry_index = dex_register_locations_.Get(
357 start_index_in_dex_register_locations + index_in_dex_register_locations);
358 dex_register_map.SetLocationCatalogEntryIndex(
359 index_in_dex_register_locations,
360 location_catalog_entry_index,
361 num_dex_registers,
362 location_catalog_entries_.Size());
363 ++index_in_dex_register_locations;
364 }
365 }
366}
367
Calin Juravle4f46ac52015-04-23 18:47:21 +0100368size_t StackMapStream::FindEntryWithTheSameDexMap() {
369 size_t current_entry_index = stack_maps_.Size();
370 auto entries_it = dex_map_hash_to_stack_map_indices_.find(current_entry_.dex_register_map_hash);
Calin Juravlec416d332015-04-23 16:01:43 +0100371 if (entries_it == dex_map_hash_to_stack_map_indices_.end()) {
372 // We don't have a perfect hash functions so we need a list to collect all stack maps
373 // which might have the same dex register map.
374 GrowableArray<uint32_t> stack_map_indices(allocator_, 1);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100375 stack_map_indices.Add(current_entry_index);
376 dex_map_hash_to_stack_map_indices_.Put(current_entry_.dex_register_map_hash, stack_map_indices);
Calin Juravlec416d332015-04-23 16:01:43 +0100377 return kNoSameDexMapFound;
378 }
379
Calin Juravle4f46ac52015-04-23 18:47:21 +0100380 // We might have collisions, so we need to check whether or not we really have a match.
Calin Juravlec416d332015-04-23 16:01:43 +0100381 for (size_t i = 0; i < entries_it->second.Size(); i++) {
382 size_t test_entry_index = entries_it->second.Get(i);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100383 if (HaveTheSameDexMaps(stack_maps_.Get(test_entry_index), current_entry_)) {
384 return test_entry_index;
Calin Juravlec416d332015-04-23 16:01:43 +0100385 }
386 }
Calin Juravle4f46ac52015-04-23 18:47:21 +0100387 entries_it->second.Add(current_entry_index);
388 return kNoSameDexMapFound;
Calin Juravlec416d332015-04-23 16:01:43 +0100389}
390
391bool StackMapStream::HaveTheSameDexMaps(const StackMapEntry& a, const StackMapEntry& b) const {
392 if (a.live_dex_registers_mask == nullptr && b.live_dex_registers_mask == nullptr) {
393 return true;
394 }
395 if (a.live_dex_registers_mask == nullptr || b.live_dex_registers_mask == nullptr) {
396 return false;
397 }
398 if (a.num_dex_registers != b.num_dex_registers) {
399 return false;
400 }
401
402 int index_in_dex_register_locations = 0;
403 for (uint32_t i = 0; i < a.num_dex_registers; i++) {
404 if (a.live_dex_registers_mask->IsBitSet(i) != b.live_dex_registers_mask->IsBitSet(i)) {
405 return false;
406 }
407 if (a.live_dex_registers_mask->IsBitSet(i)) {
408 size_t a_loc = dex_register_locations_.Get(
409 a.dex_register_locations_start_index + index_in_dex_register_locations);
410 size_t b_loc = dex_register_locations_.Get(
411 b.dex_register_locations_start_index + index_in_dex_register_locations);
412 if (a_loc != b_loc) {
413 return false;
414 }
415 ++index_in_dex_register_locations;
416 }
417 }
418 return true;
419}
420
421} // namespace art