blob: 5663e3973d5dfe267e1833cd45bcabeb0b6e0aa8 [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();
136 stack_maps_size_ = stack_maps_.Size()
137 * StackMap::ComputeStackMapSize(stack_mask_size_,
138 inline_info_size_,
139 dex_register_maps_size_,
140 dex_pc_max_,
141 native_pc_offset_max_,
142 register_mask_max_);
143 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
Calin Juravle4f46ac52015-04-23 18:47:21 +0100238 code_info.SetEncoding(inline_info_size_,
239 dex_register_maps_size_,
Calin Juravlec416d332015-04-23 16:01:43 +0100240 dex_pc_max_,
241 native_pc_offset_max_,
242 register_mask_max_);
243 code_info.SetNumberOfStackMaps(stack_maps_.Size());
Calin Juravle4f46ac52015-04-23 18:47:21 +0100244 code_info.SetStackMaskSize(stack_mask_size_);
245 DCHECK_EQ(code_info.GetStackMapsSize(), stack_maps_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100246
247 // Set the Dex register location catalog.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100248 code_info.SetNumberOfDexRegisterLocationCatalogEntries(location_catalog_entries_.Size());
Calin Juravlec416d332015-04-23 16:01:43 +0100249 MemoryRegion dex_register_location_catalog_region = region.Subregion(
Calin Juravle4f46ac52015-04-23 18:47:21 +0100250 dex_register_location_catalog_start_, dex_register_location_catalog_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100251 DexRegisterLocationCatalog dex_register_location_catalog(dex_register_location_catalog_region);
252 // Offset in `dex_register_location_catalog` where to store the next
253 // register location.
254 size_t location_catalog_offset = DexRegisterLocationCatalog::kFixedSize;
255 for (size_t i = 0, e = location_catalog_entries_.Size(); i < e; ++i) {
256 DexRegisterLocation dex_register_location = location_catalog_entries_.Get(i);
257 dex_register_location_catalog.SetRegisterInfo(location_catalog_offset, dex_register_location);
258 location_catalog_offset += DexRegisterLocationCatalog::EntrySize(dex_register_location);
259 }
260 // Ensure we reached the end of the Dex registers location_catalog.
261 DCHECK_EQ(location_catalog_offset, dex_register_location_catalog_region.size());
262
263 uintptr_t next_dex_register_map_offset = 0;
264 uintptr_t next_inline_info_offset = 0;
265 for (size_t i = 0, e = stack_maps_.Size(); i < e; ++i) {
266 StackMap stack_map = code_info.GetStackMapAt(i);
267 StackMapEntry entry = stack_maps_.Get(i);
268
269 stack_map.SetDexPc(code_info, entry.dex_pc);
270 stack_map.SetNativePcOffset(code_info, entry.native_pc_offset);
271 stack_map.SetRegisterMask(code_info, entry.register_mask);
272 if (entry.sp_mask != nullptr) {
273 stack_map.SetStackMask(code_info, *entry.sp_mask);
274 }
275
276 if (entry.num_dex_registers == 0) {
277 // No dex map available.
278 stack_map.SetDexRegisterMapOffset(code_info, StackMap::kNoDexRegisterMap);
279 } else {
280 // Search for an entry with the same dex map.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100281 if (entry.same_dex_register_map_as_ != kNoSameDexMapFound) {
Calin Juravlec416d332015-04-23 16:01:43 +0100282 // If we have a hit reuse the offset.
283 stack_map.SetDexRegisterMapOffset(code_info,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100284 code_info.GetStackMapAt(entry.same_dex_register_map_as_)
285 .GetDexRegisterMapOffset(code_info));
Calin Juravlec416d332015-04-23 16:01:43 +0100286 } else {
287 // New dex registers maps should be added to the stack map.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100288 MemoryRegion register_region = dex_register_locations_region.Subregion(
289 next_dex_register_map_offset,
290 ComputeDexRegisterMapSize(entry.num_dex_registers, *entry.live_dex_registers_mask));
Calin Juravlec416d332015-04-23 16:01:43 +0100291 next_dex_register_map_offset += register_region.size();
292 DexRegisterMap dex_register_map(register_region);
293 stack_map.SetDexRegisterMapOffset(
294 code_info, register_region.start() - dex_register_locations_region.start());
295
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100296 // Set the dex register location.
297 FillInDexRegisterMap(dex_register_map,
298 entry.num_dex_registers,
299 *entry.live_dex_registers_mask,
300 entry.dex_register_locations_start_index);
Calin Juravlec416d332015-04-23 16:01:43 +0100301 }
302 }
303
304 // Set the inlining info.
305 if (entry.inlining_depth != 0) {
306 MemoryRegion inline_region = inline_infos_region.Subregion(
307 next_inline_info_offset,
308 InlineInfo::kFixedSize + entry.inlining_depth * InlineInfo::SingleEntrySize());
309 next_inline_info_offset += inline_region.size();
310 InlineInfo inline_info(inline_region);
311
312 // Currently relative to the dex register map.
313 stack_map.SetInlineDescriptorOffset(
314 code_info, inline_region.start() - dex_register_locations_region.start());
315
316 inline_info.SetDepth(entry.inlining_depth);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100317 for (size_t depth = 0; depth < entry.inlining_depth; ++depth) {
318 InlineInfoEntry inline_entry = inline_infos_.Get(depth + entry.inline_infos_start_index);
319 inline_info.SetMethodIndexAtDepth(depth, inline_entry.method_index);
320 inline_info.SetDexPcAtDepth(depth, inline_entry.dex_pc);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100321 inline_info.SetInvokeTypeAtDepth(depth, inline_entry.invoke_type);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100322 if (inline_entry.num_dex_registers == 0) {
323 // No dex map available.
324 inline_info.SetDexRegisterMapOffsetAtDepth(depth, StackMap::kNoDexRegisterMap);
325 DCHECK(inline_entry.live_dex_registers_mask == nullptr);
326 } else {
327 MemoryRegion register_region = dex_register_locations_region.Subregion(
328 next_dex_register_map_offset,
329 ComputeDexRegisterMapSize(inline_entry.num_dex_registers,
330 *inline_entry.live_dex_registers_mask));
331 next_dex_register_map_offset += register_region.size();
332 DexRegisterMap dex_register_map(register_region);
333 inline_info.SetDexRegisterMapOffsetAtDepth(
334 depth, register_region.start() - dex_register_locations_region.start());
335
336 FillInDexRegisterMap(dex_register_map,
337 inline_entry.num_dex_registers,
338 *inline_entry.live_dex_registers_mask,
339 inline_entry.dex_register_locations_start_index);
340 }
Calin Juravlec416d332015-04-23 16:01:43 +0100341 }
342 } else {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100343 if (inline_info_size_ != 0) {
Calin Juravlec416d332015-04-23 16:01:43 +0100344 stack_map.SetInlineDescriptorOffset(code_info, StackMap::kNoInlineInfo);
345 }
346 }
347 }
348}
349
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100350void StackMapStream::FillInDexRegisterMap(DexRegisterMap dex_register_map,
351 uint32_t num_dex_registers,
352 const BitVector& live_dex_registers_mask,
353 uint32_t start_index_in_dex_register_locations) const {
354 dex_register_map.SetLiveBitMask(num_dex_registers, live_dex_registers_mask);
355 // Set the dex register location mapping data.
356 for (size_t dex_register_number = 0, index_in_dex_register_locations = 0;
357 dex_register_number < num_dex_registers;
358 ++dex_register_number) {
359 if (live_dex_registers_mask.IsBitSet(dex_register_number)) {
360 size_t location_catalog_entry_index = dex_register_locations_.Get(
361 start_index_in_dex_register_locations + index_in_dex_register_locations);
362 dex_register_map.SetLocationCatalogEntryIndex(
363 index_in_dex_register_locations,
364 location_catalog_entry_index,
365 num_dex_registers,
366 location_catalog_entries_.Size());
367 ++index_in_dex_register_locations;
368 }
369 }
370}
371
Calin Juravle4f46ac52015-04-23 18:47:21 +0100372size_t StackMapStream::FindEntryWithTheSameDexMap() {
373 size_t current_entry_index = stack_maps_.Size();
374 auto entries_it = dex_map_hash_to_stack_map_indices_.find(current_entry_.dex_register_map_hash);
Calin Juravlec416d332015-04-23 16:01:43 +0100375 if (entries_it == dex_map_hash_to_stack_map_indices_.end()) {
376 // We don't have a perfect hash functions so we need a list to collect all stack maps
377 // which might have the same dex register map.
378 GrowableArray<uint32_t> stack_map_indices(allocator_, 1);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100379 stack_map_indices.Add(current_entry_index);
380 dex_map_hash_to_stack_map_indices_.Put(current_entry_.dex_register_map_hash, stack_map_indices);
Calin Juravlec416d332015-04-23 16:01:43 +0100381 return kNoSameDexMapFound;
382 }
383
Calin Juravle4f46ac52015-04-23 18:47:21 +0100384 // We might have collisions, so we need to check whether or not we really have a match.
Calin Juravlec416d332015-04-23 16:01:43 +0100385 for (size_t i = 0; i < entries_it->second.Size(); i++) {
386 size_t test_entry_index = entries_it->second.Get(i);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100387 if (HaveTheSameDexMaps(stack_maps_.Get(test_entry_index), current_entry_)) {
388 return test_entry_index;
Calin Juravlec416d332015-04-23 16:01:43 +0100389 }
390 }
Calin Juravle4f46ac52015-04-23 18:47:21 +0100391 entries_it->second.Add(current_entry_index);
392 return kNoSameDexMapFound;
Calin Juravlec416d332015-04-23 16:01:43 +0100393}
394
395bool StackMapStream::HaveTheSameDexMaps(const StackMapEntry& a, const StackMapEntry& b) const {
396 if (a.live_dex_registers_mask == nullptr && b.live_dex_registers_mask == nullptr) {
397 return true;
398 }
399 if (a.live_dex_registers_mask == nullptr || b.live_dex_registers_mask == nullptr) {
400 return false;
401 }
402 if (a.num_dex_registers != b.num_dex_registers) {
403 return false;
404 }
405
406 int index_in_dex_register_locations = 0;
407 for (uint32_t i = 0; i < a.num_dex_registers; i++) {
408 if (a.live_dex_registers_mask->IsBitSet(i) != b.live_dex_registers_mask->IsBitSet(i)) {
409 return false;
410 }
411 if (a.live_dex_registers_mask->IsBitSet(i)) {
412 size_t a_loc = dex_register_locations_.Get(
413 a.dex_register_locations_start_index + index_in_dex_register_locations);
414 size_t b_loc = dex_register_locations_.Get(
415 b.dex_register_locations_start_index + index_in_dex_register_locations);
416 if (a_loc != b_loc) {
417 return false;
418 }
419 ++index_in_dex_register_locations;
420 }
421 }
422 return true;
423}
424
425} // namespace art