blob: 3f41e3594e0a3b8d27d6fb96177d02fc9eb9489c [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";
Calin Juravleb5c46932015-10-06 17:09:49 +010027 DCHECK_NE(dex_pc, static_cast<uint32_t>(-1)) << "invalid dex_pc";
Calin Juravle4f46ac52015-04-23 18:47:21 +010028 current_entry_.dex_pc = dex_pc;
29 current_entry_.native_pc_offset = native_pc_offset;
30 current_entry_.register_mask = register_mask;
31 current_entry_.sp_mask = sp_mask;
32 current_entry_.num_dex_registers = num_dex_registers;
33 current_entry_.inlining_depth = inlining_depth;
Vladimir Marko225b6462015-09-28 12:17:40 +010034 current_entry_.dex_register_locations_start_index = dex_register_locations_.size();
35 current_entry_.inline_infos_start_index = inline_infos_.size();
Calin Juravle4f46ac52015-04-23 18:47:21 +010036 current_entry_.dex_register_map_hash = 0;
37 current_entry_.same_dex_register_map_as_ = kNoSameDexMapFound;
Calin Juravlec416d332015-04-23 16:01:43 +010038 if (num_dex_registers != 0) {
Calin Juravle4f46ac52015-04-23 18:47:21 +010039 current_entry_.live_dex_registers_mask =
Vladimir Markof6a35de2016-03-21 12:01:50 +000040 ArenaBitVector::Create(allocator_, num_dex_registers, true, kArenaAllocStackMapStream);
Calin Juravlec416d332015-04-23 16:01:43 +010041 } else {
Calin Juravle4f46ac52015-04-23 18:47:21 +010042 current_entry_.live_dex_registers_mask = nullptr;
Calin Juravlec416d332015-04-23 16:01:43 +010043 }
Calin Juravlec416d332015-04-23 16:01:43 +010044
45 if (sp_mask != nullptr) {
46 stack_mask_max_ = std::max(stack_mask_max_, sp_mask->GetHighestBitSet());
47 }
48 if (inlining_depth > 0) {
49 number_of_stack_maps_with_inline_info_++;
50 }
51
52 dex_pc_max_ = std::max(dex_pc_max_, dex_pc);
Calin Juravlec416d332015-04-23 16:01:43 +010053 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();
Vladimir Marko225b6462015-09-28 12:17:40 +010059 stack_maps_.push_back(current_entry_);
Calin Juravle4f46ac52015-04-23 18:47:21 +010060 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.
David Srbecky7dc11782016-02-25 13:23:56 +000066 DCHECK(DexRegisterLocation::IsShortLocationKind(kind)) << kind;
Calin Juravlec416d332015-04-23 16:01:43 +010067 DexRegisterLocation location(kind, value);
68
69 // Look for Dex register `location` in the location catalog (using the
70 // companion hash map of locations to indices). Use its index if it
71 // is already in the location catalog. If not, insert it (in the
72 // location catalog and the hash map) and use the newly created index.
73 auto it = location_catalog_entries_indices_.Find(location);
74 if (it != location_catalog_entries_indices_.end()) {
75 // Retrieve the index from the hash map.
Vladimir Marko225b6462015-09-28 12:17:40 +010076 dex_register_locations_.push_back(it->second);
Calin Juravlec416d332015-04-23 16:01:43 +010077 } else {
78 // Create a new entry in the location catalog and the hash map.
Vladimir Marko225b6462015-09-28 12:17:40 +010079 size_t index = location_catalog_entries_.size();
80 location_catalog_entries_.push_back(location);
81 dex_register_locations_.push_back(index);
Calin Juravlec416d332015-04-23 16:01:43 +010082 location_catalog_entries_indices_.Insert(std::make_pair(location, index));
83 }
84
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010085 if (in_inline_frame_) {
86 // TODO: Support sharing DexRegisterMap across InlineInfo.
87 DCHECK_LT(current_dex_register_, current_inline_info_.num_dex_registers);
88 current_inline_info_.live_dex_registers_mask->SetBit(current_dex_register_);
89 } else {
90 DCHECK_LT(current_dex_register_, current_entry_.num_dex_registers);
91 current_entry_.live_dex_registers_mask->SetBit(current_dex_register_);
92 current_entry_.dex_register_map_hash += (1 <<
93 (current_dex_register_ % (sizeof(current_entry_.dex_register_map_hash) * kBitsPerByte)));
94 current_entry_.dex_register_map_hash += static_cast<uint32_t>(value);
95 current_entry_.dex_register_map_hash += static_cast<uint32_t>(kind);
96 }
Calin Juravlec416d332015-04-23 16:01:43 +010097 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010098 current_dex_register_++;
Calin Juravlec416d332015-04-23 16:01:43 +010099}
100
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100101void StackMapStream::BeginInlineInfoEntry(uint32_t method_index,
102 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100103 InvokeType invoke_type,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100104 uint32_t num_dex_registers) {
105 DCHECK(!in_inline_frame_);
106 in_inline_frame_ = true;
107 current_inline_info_.method_index = method_index;
108 current_inline_info_.dex_pc = dex_pc;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100109 current_inline_info_.invoke_type = invoke_type;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100110 current_inline_info_.num_dex_registers = num_dex_registers;
Vladimir Marko225b6462015-09-28 12:17:40 +0100111 current_inline_info_.dex_register_locations_start_index = dex_register_locations_.size();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100112 if (num_dex_registers != 0) {
113 current_inline_info_.live_dex_registers_mask =
Vladimir Markof6a35de2016-03-21 12:01:50 +0000114 ArenaBitVector::Create(allocator_, num_dex_registers, true, kArenaAllocStackMapStream);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100115 } else {
116 current_inline_info_.live_dex_registers_mask = nullptr;
117 }
118 current_dex_register_ = 0;
119}
120
121void StackMapStream::EndInlineInfoEntry() {
122 DCHECK(in_inline_frame_);
123 DCHECK_EQ(current_dex_register_, current_inline_info_.num_dex_registers)
124 << "Inline information contains less registers than expected";
125 in_inline_frame_ = false;
Vladimir Marko225b6462015-09-28 12:17:40 +0100126 inline_infos_.push_back(current_inline_info_);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100127 current_inline_info_ = InlineInfoEntry();
Calin Juravlec416d332015-04-23 16:01:43 +0100128}
129
Vladimir Markobd8c7252015-06-12 10:06:32 +0100130uint32_t StackMapStream::ComputeMaxNativePcOffset() const {
131 uint32_t max_native_pc_offset = 0u;
Vladimir Marko225b6462015-09-28 12:17:40 +0100132 for (const StackMapEntry& entry : stack_maps_) {
133 max_native_pc_offset = std::max(max_native_pc_offset, entry.native_pc_offset);
Vladimir Markobd8c7252015-06-12 10:06:32 +0100134 }
135 return max_native_pc_offset;
136}
137
Calin Juravle4f46ac52015-04-23 18:47:21 +0100138size_t StackMapStream::PrepareForFillIn() {
139 int stack_mask_number_of_bits = stack_mask_max_ + 1; // Need room for max element too.
140 stack_mask_size_ = RoundUp(stack_mask_number_of_bits, kBitsPerByte) / kBitsPerByte;
141 inline_info_size_ = ComputeInlineInfoSize();
142 dex_register_maps_size_ = ComputeDexRegisterMapsSize();
Vladimir Markobd8c7252015-06-12 10:06:32 +0100143 uint32_t max_native_pc_offset = ComputeMaxNativePcOffset();
David Brazdilf677ebf2015-05-29 16:29:43 +0100144 stack_map_encoding_ = StackMapEncoding::CreateFromSizes(stack_mask_size_,
145 inline_info_size_,
146 dex_register_maps_size_,
147 dex_pc_max_,
Vladimir Markobd8c7252015-06-12 10:06:32 +0100148 max_native_pc_offset,
David Brazdilf677ebf2015-05-29 16:29:43 +0100149 register_mask_max_);
Vladimir Marko225b6462015-09-28 12:17:40 +0100150 stack_maps_size_ = stack_maps_.size() * stack_map_encoding_.ComputeStackMapSize();
Calin Juravle4f46ac52015-04-23 18:47:21 +0100151 dex_register_location_catalog_size_ = ComputeDexRegisterLocationCatalogSize();
152
Calin Juravlec416d332015-04-23 16:01:43 +0100153 // Note: use RoundUp to word-size here if you want CodeInfo objects to be word aligned.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100154 needed_size_ = CodeInfo::kFixedSize
Calin Juravle4f46ac52015-04-23 18:47:21 +0100155 + stack_maps_size_
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100156 + dex_register_location_catalog_size_
Calin Juravle4f46ac52015-04-23 18:47:21 +0100157 + dex_register_maps_size_
158 + inline_info_size_;
Calin Juravlec416d332015-04-23 16:01:43 +0100159
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100160 stack_maps_start_ = CodeInfo::kFixedSize;
161 // TODO: Move the catalog at the end. It is currently too expensive at runtime
162 // to compute its size (note that we do not encode that size in the CodeInfo).
163 dex_register_location_catalog_start_ = stack_maps_start_ + stack_maps_size_;
164 dex_register_maps_start_ =
165 dex_register_location_catalog_start_ + dex_register_location_catalog_size_;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100166 inline_infos_start_ = dex_register_maps_start_ + dex_register_maps_size_;
Calin Juravlec416d332015-04-23 16:01:43 +0100167
Calin Juravle4f46ac52015-04-23 18:47:21 +0100168 return needed_size_;
Calin Juravlec416d332015-04-23 16:01:43 +0100169}
170
171size_t StackMapStream::ComputeDexRegisterLocationCatalogSize() const {
172 size_t size = DexRegisterLocationCatalog::kFixedSize;
Vladimir Marko225b6462015-09-28 12:17:40 +0100173 for (const DexRegisterLocation& dex_register_location : location_catalog_entries_) {
Calin Juravlec416d332015-04-23 16:01:43 +0100174 size += DexRegisterLocationCatalog::EntrySize(dex_register_location);
175 }
176 return size;
177}
178
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100179size_t StackMapStream::ComputeDexRegisterMapSize(uint32_t num_dex_registers,
Vladimir Marko225b6462015-09-28 12:17:40 +0100180 const BitVector* live_dex_registers_mask) const {
181 // For num_dex_registers == 0u live_dex_registers_mask may be null.
182 if (num_dex_registers == 0u) {
183 return 0u; // No register map will be emitted.
184 }
185 DCHECK(live_dex_registers_mask != nullptr);
186
Calin Juravlec416d332015-04-23 16:01:43 +0100187 // Size of the map in bytes.
188 size_t size = DexRegisterMap::kFixedSize;
189 // Add the live bit mask for the Dex register liveness.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100190 size += DexRegisterMap::GetLiveBitMaskSize(num_dex_registers);
Calin Juravlec416d332015-04-23 16:01:43 +0100191 // Compute the size of the set of live Dex register entries.
Vladimir Marko225b6462015-09-28 12:17:40 +0100192 size_t number_of_live_dex_registers = live_dex_registers_mask->NumSetBits();
Calin Juravlec416d332015-04-23 16:01:43 +0100193 size_t map_entries_size_in_bits =
Vladimir Marko225b6462015-09-28 12:17:40 +0100194 DexRegisterMap::SingleEntrySizeInBits(location_catalog_entries_.size())
Calin Juravlec416d332015-04-23 16:01:43 +0100195 * number_of_live_dex_registers;
196 size_t map_entries_size_in_bytes =
197 RoundUp(map_entries_size_in_bits, kBitsPerByte) / kBitsPerByte;
198 size += map_entries_size_in_bytes;
199 return size;
200}
201
Calin Juravle4f46ac52015-04-23 18:47:21 +0100202size_t StackMapStream::ComputeDexRegisterMapsSize() const {
Calin Juravlec416d332015-04-23 16:01:43 +0100203 size_t size = 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100204 size_t inline_info_index = 0;
Vladimir Marko225b6462015-09-28 12:17:40 +0100205 for (const StackMapEntry& entry : stack_maps_) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100206 if (entry.same_dex_register_map_as_ == kNoSameDexMapFound) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100207 size += ComputeDexRegisterMapSize(entry.num_dex_registers, entry.live_dex_registers_mask);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100208 } else {
Calin Juravlec416d332015-04-23 16:01:43 +0100209 // Entries with the same dex map will have the same offset.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100210 }
211 for (size_t j = 0; j < entry.inlining_depth; ++j) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100212 InlineInfoEntry inline_entry = inline_infos_[inline_info_index++];
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100213 size += ComputeDexRegisterMapSize(inline_entry.num_dex_registers,
Vladimir Marko225b6462015-09-28 12:17:40 +0100214 inline_entry.live_dex_registers_mask);
Calin Juravlec416d332015-04-23 16:01:43 +0100215 }
216 }
217 return size;
218}
219
220size_t StackMapStream::ComputeInlineInfoSize() const {
Vladimir Marko225b6462015-09-28 12:17:40 +0100221 return inline_infos_.size() * InlineInfo::SingleEntrySize()
Calin Juravlec416d332015-04-23 16:01:43 +0100222 // For encoding the depth.
223 + (number_of_stack_maps_with_inline_info_ * InlineInfo::kFixedSize);
224}
225
Calin Juravlec416d332015-04-23 16:01:43 +0100226void StackMapStream::FillIn(MemoryRegion region) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100227 DCHECK_EQ(0u, current_entry_.dex_pc) << "EndStackMapEntry not called after BeginStackMapEntry";
228 DCHECK_NE(0u, needed_size_) << "PrepareForFillIn not called before FillIn";
229
Calin Juravlec416d332015-04-23 16:01:43 +0100230 CodeInfo code_info(region);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100231 DCHECK_EQ(region.size(), needed_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100232 code_info.SetOverallSize(region.size());
233
Calin Juravlec416d332015-04-23 16:01:43 +0100234 MemoryRegion dex_register_locations_region = region.Subregion(
Calin Juravle4f46ac52015-04-23 18:47:21 +0100235 dex_register_maps_start_, dex_register_maps_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100236
237 MemoryRegion inline_infos_region = region.Subregion(
Calin Juravle4f46ac52015-04-23 18:47:21 +0100238 inline_infos_start_, inline_info_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100239
David Brazdilf677ebf2015-05-29 16:29:43 +0100240 code_info.SetEncoding(stack_map_encoding_);
Vladimir Marko225b6462015-09-28 12:17:40 +0100241 code_info.SetNumberOfStackMaps(stack_maps_.size());
David Brazdilf677ebf2015-05-29 16:29:43 +0100242 DCHECK_EQ(code_info.GetStackMapsSize(code_info.ExtractEncoding()), stack_maps_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100243
244 // Set the Dex register location catalog.
Vladimir Marko225b6462015-09-28 12:17:40 +0100245 code_info.SetNumberOfLocationCatalogEntries(location_catalog_entries_.size());
Calin Juravlec416d332015-04-23 16:01:43 +0100246 MemoryRegion dex_register_location_catalog_region = region.Subregion(
Calin Juravle4f46ac52015-04-23 18:47:21 +0100247 dex_register_location_catalog_start_, dex_register_location_catalog_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100248 DexRegisterLocationCatalog dex_register_location_catalog(dex_register_location_catalog_region);
249 // Offset in `dex_register_location_catalog` where to store the next
250 // register location.
251 size_t location_catalog_offset = DexRegisterLocationCatalog::kFixedSize;
Vladimir Marko225b6462015-09-28 12:17:40 +0100252 for (DexRegisterLocation dex_register_location : location_catalog_entries_) {
Calin Juravlec416d332015-04-23 16:01:43 +0100253 dex_register_location_catalog.SetRegisterInfo(location_catalog_offset, dex_register_location);
254 location_catalog_offset += DexRegisterLocationCatalog::EntrySize(dex_register_location);
255 }
256 // Ensure we reached the end of the Dex registers location_catalog.
257 DCHECK_EQ(location_catalog_offset, dex_register_location_catalog_region.size());
258
Vladimir Markof6a35de2016-03-21 12:01:50 +0000259 ArenaBitVector empty_bitmask(allocator_, 0, /* expandable */ false, kArenaAllocStackMapStream);
Calin Juravlec416d332015-04-23 16:01:43 +0100260 uintptr_t next_dex_register_map_offset = 0;
261 uintptr_t next_inline_info_offset = 0;
Vladimir Marko225b6462015-09-28 12:17:40 +0100262 for (size_t i = 0, e = stack_maps_.size(); i < e; ++i) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100263 StackMap stack_map = code_info.GetStackMapAt(i, stack_map_encoding_);
Vladimir Marko225b6462015-09-28 12:17:40 +0100264 StackMapEntry entry = stack_maps_[i];
Calin Juravlec416d332015-04-23 16:01:43 +0100265
David Brazdilf677ebf2015-05-29 16:29:43 +0100266 stack_map.SetDexPc(stack_map_encoding_, entry.dex_pc);
267 stack_map.SetNativePcOffset(stack_map_encoding_, entry.native_pc_offset);
268 stack_map.SetRegisterMask(stack_map_encoding_, entry.register_mask);
Calin Juravlec416d332015-04-23 16:01:43 +0100269 if (entry.sp_mask != nullptr) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100270 stack_map.SetStackMask(stack_map_encoding_, *entry.sp_mask);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000271 } else {
272 // The MemoryRegion does not have to be zeroed, so make sure we clear the bits.
273 stack_map.SetStackMask(stack_map_encoding_, empty_bitmask);
Calin Juravlec416d332015-04-23 16:01:43 +0100274 }
275
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000276 if (entry.num_dex_registers == 0 || (entry.live_dex_registers_mask->NumSetBits() == 0)) {
Calin Juravlec416d332015-04-23 16:01:43 +0100277 // No dex map available.
David Brazdilf677ebf2015-05-29 16:29:43 +0100278 stack_map.SetDexRegisterMapOffset(stack_map_encoding_, StackMap::kNoDexRegisterMap);
Calin Juravlec416d332015-04-23 16:01:43 +0100279 } 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.
David Brazdilf677ebf2015-05-29 16:29:43 +0100283 stack_map.SetDexRegisterMapOffset(
284 stack_map_encoding_,
285 code_info.GetStackMapAt(entry.same_dex_register_map_as_, stack_map_encoding_)
David Brazdil77a48ae2015-09-15 12:34:04 +0000286 .GetDexRegisterMapOffset(stack_map_encoding_));
Calin Juravlec416d332015-04-23 16:01:43 +0100287 } else {
288 // New dex registers maps should be added to the stack map.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100289 MemoryRegion register_region = dex_register_locations_region.Subregion(
290 next_dex_register_map_offset,
Vladimir Marko225b6462015-09-28 12:17:40 +0100291 ComputeDexRegisterMapSize(entry.num_dex_registers, entry.live_dex_registers_mask));
Calin Juravlec416d332015-04-23 16:01:43 +0100292 next_dex_register_map_offset += register_region.size();
293 DexRegisterMap dex_register_map(register_region);
294 stack_map.SetDexRegisterMapOffset(
David Brazdilf677ebf2015-05-29 16:29:43 +0100295 stack_map_encoding_, register_region.start() - dex_register_locations_region.start());
Calin Juravlec416d332015-04-23 16:01:43 +0100296
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100297 // Set the dex register location.
298 FillInDexRegisterMap(dex_register_map,
299 entry.num_dex_registers,
300 *entry.live_dex_registers_mask,
301 entry.dex_register_locations_start_index);
Calin Juravlec416d332015-04-23 16:01:43 +0100302 }
303 }
304
305 // Set the inlining info.
306 if (entry.inlining_depth != 0) {
307 MemoryRegion inline_region = inline_infos_region.Subregion(
308 next_inline_info_offset,
309 InlineInfo::kFixedSize + entry.inlining_depth * InlineInfo::SingleEntrySize());
310 next_inline_info_offset += inline_region.size();
311 InlineInfo inline_info(inline_region);
312
313 // Currently relative to the dex register map.
314 stack_map.SetInlineDescriptorOffset(
David Brazdilf677ebf2015-05-29 16:29:43 +0100315 stack_map_encoding_, inline_region.start() - dex_register_locations_region.start());
Calin Juravlec416d332015-04-23 16:01:43 +0100316
317 inline_info.SetDepth(entry.inlining_depth);
Vladimir Marko225b6462015-09-28 12:17:40 +0100318 DCHECK_LE(entry.inline_infos_start_index + entry.inlining_depth, inline_infos_.size());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100319 for (size_t depth = 0; depth < entry.inlining_depth; ++depth) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100320 InlineInfoEntry inline_entry = inline_infos_[depth + entry.inline_infos_start_index];
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100321 inline_info.SetMethodIndexAtDepth(depth, inline_entry.method_index);
322 inline_info.SetDexPcAtDepth(depth, inline_entry.dex_pc);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100323 inline_info.SetInvokeTypeAtDepth(depth, inline_entry.invoke_type);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100324 if (inline_entry.num_dex_registers == 0) {
325 // No dex map available.
326 inline_info.SetDexRegisterMapOffsetAtDepth(depth, StackMap::kNoDexRegisterMap);
327 DCHECK(inline_entry.live_dex_registers_mask == nullptr);
328 } else {
329 MemoryRegion register_region = dex_register_locations_region.Subregion(
330 next_dex_register_map_offset,
331 ComputeDexRegisterMapSize(inline_entry.num_dex_registers,
Vladimir Marko225b6462015-09-28 12:17:40 +0100332 inline_entry.live_dex_registers_mask));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100333 next_dex_register_map_offset += register_region.size();
334 DexRegisterMap dex_register_map(register_region);
335 inline_info.SetDexRegisterMapOffsetAtDepth(
336 depth, register_region.start() - dex_register_locations_region.start());
337
338 FillInDexRegisterMap(dex_register_map,
339 inline_entry.num_dex_registers,
340 *inline_entry.live_dex_registers_mask,
341 inline_entry.dex_register_locations_start_index);
342 }
Calin Juravlec416d332015-04-23 16:01:43 +0100343 }
344 } else {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100345 if (inline_info_size_ != 0) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100346 stack_map.SetInlineDescriptorOffset(stack_map_encoding_, StackMap::kNoInlineInfo);
Calin Juravlec416d332015-04-23 16:01:43 +0100347 }
348 }
349 }
David Srbecky1bbdfd72016-02-24 16:39:26 +0000350
351 // Verify all written data in debug build.
352 if (kIsDebugBuild) {
353 CheckCodeInfo(region);
354 }
Calin Juravlec416d332015-04-23 16:01:43 +0100355}
356
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100357void StackMapStream::FillInDexRegisterMap(DexRegisterMap dex_register_map,
358 uint32_t num_dex_registers,
359 const BitVector& live_dex_registers_mask,
360 uint32_t start_index_in_dex_register_locations) const {
361 dex_register_map.SetLiveBitMask(num_dex_registers, live_dex_registers_mask);
362 // Set the dex register location mapping data.
Vladimir Marko225b6462015-09-28 12:17:40 +0100363 size_t number_of_live_dex_registers = live_dex_registers_mask.NumSetBits();
364 DCHECK_LE(number_of_live_dex_registers, dex_register_locations_.size());
365 DCHECK_LE(start_index_in_dex_register_locations,
366 dex_register_locations_.size() - number_of_live_dex_registers);
367 for (size_t index_in_dex_register_locations = 0;
368 index_in_dex_register_locations != number_of_live_dex_registers;
369 ++index_in_dex_register_locations) {
370 size_t location_catalog_entry_index = dex_register_locations_[
371 start_index_in_dex_register_locations + index_in_dex_register_locations];
372 dex_register_map.SetLocationCatalogEntryIndex(
373 index_in_dex_register_locations,
374 location_catalog_entry_index,
375 num_dex_registers,
376 location_catalog_entries_.size());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100377 }
378}
379
Calin Juravle4f46ac52015-04-23 18:47:21 +0100380size_t StackMapStream::FindEntryWithTheSameDexMap() {
Vladimir Marko225b6462015-09-28 12:17:40 +0100381 size_t current_entry_index = stack_maps_.size();
Calin Juravle4f46ac52015-04-23 18:47:21 +0100382 auto entries_it = dex_map_hash_to_stack_map_indices_.find(current_entry_.dex_register_map_hash);
Calin Juravlec416d332015-04-23 16:01:43 +0100383 if (entries_it == dex_map_hash_to_stack_map_indices_.end()) {
384 // We don't have a perfect hash functions so we need a list to collect all stack maps
385 // which might have the same dex register map.
Vladimir Marko225b6462015-09-28 12:17:40 +0100386 ArenaVector<uint32_t> stack_map_indices(allocator_->Adapter(kArenaAllocStackMapStream));
387 stack_map_indices.push_back(current_entry_index);
388 dex_map_hash_to_stack_map_indices_.Put(current_entry_.dex_register_map_hash,
389 std::move(stack_map_indices));
Calin Juravlec416d332015-04-23 16:01:43 +0100390 return kNoSameDexMapFound;
391 }
392
Calin Juravle4f46ac52015-04-23 18:47:21 +0100393 // We might have collisions, so we need to check whether or not we really have a match.
Vladimir Marko225b6462015-09-28 12:17:40 +0100394 for (uint32_t test_entry_index : entries_it->second) {
395 if (HaveTheSameDexMaps(GetStackMap(test_entry_index), current_entry_)) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100396 return test_entry_index;
Calin Juravlec416d332015-04-23 16:01:43 +0100397 }
398 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100399 entries_it->second.push_back(current_entry_index);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100400 return kNoSameDexMapFound;
Calin Juravlec416d332015-04-23 16:01:43 +0100401}
402
403bool StackMapStream::HaveTheSameDexMaps(const StackMapEntry& a, const StackMapEntry& b) const {
404 if (a.live_dex_registers_mask == nullptr && b.live_dex_registers_mask == nullptr) {
405 return true;
406 }
407 if (a.live_dex_registers_mask == nullptr || b.live_dex_registers_mask == nullptr) {
408 return false;
409 }
410 if (a.num_dex_registers != b.num_dex_registers) {
411 return false;
412 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100413 if (a.num_dex_registers != 0u) {
414 DCHECK(a.live_dex_registers_mask != nullptr);
415 DCHECK(b.live_dex_registers_mask != nullptr);
416 if (!a.live_dex_registers_mask->Equal(b.live_dex_registers_mask)) {
Calin Juravlec416d332015-04-23 16:01:43 +0100417 return false;
418 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100419 size_t number_of_live_dex_registers = a.live_dex_registers_mask->NumSetBits();
420 DCHECK_LE(number_of_live_dex_registers, dex_register_locations_.size());
421 DCHECK_LE(a.dex_register_locations_start_index,
422 dex_register_locations_.size() - number_of_live_dex_registers);
423 DCHECK_LE(b.dex_register_locations_start_index,
424 dex_register_locations_.size() - number_of_live_dex_registers);
425 auto a_begin = dex_register_locations_.begin() + a.dex_register_locations_start_index;
426 auto b_begin = dex_register_locations_.begin() + b.dex_register_locations_start_index;
427 if (!std::equal(a_begin, a_begin + number_of_live_dex_registers, b_begin)) {
428 return false;
Calin Juravlec416d332015-04-23 16:01:43 +0100429 }
430 }
431 return true;
432}
433
David Srbecky1bbdfd72016-02-24 16:39:26 +0000434// Helper for CheckCodeInfo - check that register map has the expected content.
435void StackMapStream::CheckDexRegisterMap(const CodeInfo& code_info,
436 const DexRegisterMap& dex_register_map,
437 size_t num_dex_registers,
438 BitVector* live_dex_registers_mask,
439 size_t dex_register_locations_index) const {
440 StackMapEncoding encoding = code_info.ExtractEncoding();
441 for (size_t reg = 0; reg < num_dex_registers; reg++) {
442 // Find the location we tried to encode.
443 DexRegisterLocation expected = DexRegisterLocation::None();
444 if (live_dex_registers_mask->IsBitSet(reg)) {
445 size_t catalog_index = dex_register_locations_[dex_register_locations_index++];
446 expected = location_catalog_entries_[catalog_index];
447 }
448 // Compare to the seen location.
449 if (expected.GetKind() == DexRegisterLocation::Kind::kNone) {
450 DCHECK(!dex_register_map.IsValid() || !dex_register_map.IsDexRegisterLive(reg));
451 } else {
452 DCHECK(dex_register_map.IsDexRegisterLive(reg));
453 DexRegisterLocation seen = dex_register_map.GetDexRegisterLocation(
454 reg, num_dex_registers, code_info, encoding);
455 DCHECK_EQ(expected.GetKind(), seen.GetKind());
456 DCHECK_EQ(expected.GetValue(), seen.GetValue());
457 }
458 }
459 if (num_dex_registers == 0) {
460 DCHECK(!dex_register_map.IsValid());
461 }
462}
463
464// Check that all StackMapStream inputs are correctly encoded by trying to read them back.
465void StackMapStream::CheckCodeInfo(MemoryRegion region) const {
466 CodeInfo code_info(region);
467 StackMapEncoding encoding = code_info.ExtractEncoding();
468 DCHECK_EQ(code_info.GetNumberOfStackMaps(), stack_maps_.size());
469 for (size_t s = 0; s < stack_maps_.size(); ++s) {
470 const StackMap stack_map = code_info.GetStackMapAt(s, encoding);
471 StackMapEntry entry = stack_maps_[s];
472
473 // Check main stack map fields.
474 DCHECK_EQ(stack_map.GetNativePcOffset(encoding), entry.native_pc_offset);
475 DCHECK_EQ(stack_map.GetDexPc(encoding), entry.dex_pc);
476 DCHECK_EQ(stack_map.GetRegisterMask(encoding), entry.register_mask);
477 MemoryRegion stack_mask = stack_map.GetStackMask(encoding);
478 if (entry.sp_mask != nullptr) {
479 DCHECK_GE(stack_mask.size_in_bits(), entry.sp_mask->GetNumberOfBits());
480 for (size_t b = 0; b < stack_mask.size_in_bits(); b++) {
481 DCHECK_EQ(stack_mask.LoadBit(b), entry.sp_mask->IsBitSet(b));
482 }
483 } else {
484 for (size_t b = 0; b < stack_mask.size_in_bits(); b++) {
485 DCHECK_EQ(stack_mask.LoadBit(b), 0u);
486 }
487 }
488
489 CheckDexRegisterMap(code_info,
490 code_info.GetDexRegisterMapOf(
491 stack_map, encoding, entry.num_dex_registers),
492 entry.num_dex_registers,
493 entry.live_dex_registers_mask,
494 entry.dex_register_locations_start_index);
495
496 // Check inline info.
497 DCHECK_EQ(stack_map.HasInlineInfo(encoding), (entry.inlining_depth != 0));
498 if (entry.inlining_depth != 0) {
499 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
500 DCHECK_EQ(inline_info.GetDepth(), entry.inlining_depth);
501 for (size_t d = 0; d < entry.inlining_depth; ++d) {
502 size_t inline_info_index = entry.inline_infos_start_index + d;
503 DCHECK_LT(inline_info_index, inline_infos_.size());
504 InlineInfoEntry inline_entry = inline_infos_[inline_info_index];
505 DCHECK_EQ(inline_info.GetDexPcAtDepth(d), inline_entry.dex_pc);
506 DCHECK_EQ(inline_info.GetMethodIndexAtDepth(d), inline_entry.method_index);
507 DCHECK_EQ(inline_info.GetInvokeTypeAtDepth(d), inline_entry.invoke_type);
508
509 CheckDexRegisterMap(code_info,
510 code_info.GetDexRegisterMapAtDepth(
511 d, inline_info, encoding, inline_entry.num_dex_registers),
512 inline_entry.num_dex_registers,
513 inline_entry.live_dex_registers_mask,
514 inline_entry.dex_register_locations_start_index);
515 }
516 }
517 }
518}
519
Calin Juravlec416d332015-04-23 16:01:43 +0100520} // namespace art