blob: eeae96e6c2d5dc180abb5487223f74d560ef7cb4 [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 */
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000016
Calin Juravlec416d332015-04-23 16:01:43 +010017#include "stack_map_stream.h"
18
David Srbecky45aa5982016-03-18 02:15:09 +000019#include <unordered_map>
20
Andreas Gampe90b936d2017-01-31 08:58:55 -080021#include "art_method-inl.h"
David Srbecky45aa5982016-03-18 02:15:09 +000022#include "base/stl_util.h"
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +000023#include "optimizing/optimizing_compiler.h"
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000024#include "runtime.h"
25#include "scoped_thread_state_change-inl.h"
26
Calin Juravlec416d332015-04-23 16:01:43 +010027namespace art {
28
Calin Juravle4f46ac52015-04-23 18:47:21 +010029void StackMapStream::BeginStackMapEntry(uint32_t dex_pc,
30 uint32_t native_pc_offset,
31 uint32_t register_mask,
32 BitVector* sp_mask,
33 uint32_t num_dex_registers,
34 uint8_t inlining_depth) {
35 DCHECK_EQ(0u, current_entry_.dex_pc) << "EndStackMapEntry not called after BeginStackMapEntry";
Calin Juravleb5c46932015-10-06 17:09:49 +010036 DCHECK_NE(dex_pc, static_cast<uint32_t>(-1)) << "invalid dex_pc";
Calin Juravle4f46ac52015-04-23 18:47:21 +010037 current_entry_.dex_pc = dex_pc;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -080038 current_entry_.native_pc_code_offset = CodeOffset::FromOffset(native_pc_offset, instruction_set_);
Calin Juravle4f46ac52015-04-23 18:47:21 +010039 current_entry_.register_mask = register_mask;
40 current_entry_.sp_mask = sp_mask;
Calin Juravle4f46ac52015-04-23 18:47:21 +010041 current_entry_.inlining_depth = inlining_depth;
Vladimir Marko225b6462015-09-28 12:17:40 +010042 current_entry_.inline_infos_start_index = inline_infos_.size();
David Srbecky45aa5982016-03-18 02:15:09 +000043 current_entry_.stack_mask_index = 0;
Mathieu Chartierd776ff02017-01-17 09:32:18 -080044 current_entry_.dex_method_index = DexFile::kDexNoIndex;
Mathieu Chartier32289082017-02-09 15:57:37 -080045 current_entry_.dex_register_entry.num_dex_registers = num_dex_registers;
46 current_entry_.dex_register_entry.locations_start_index = dex_register_locations_.size();
47 current_entry_.dex_register_entry.live_dex_registers_mask = (num_dex_registers != 0)
48 ? ArenaBitVector::Create(allocator_, num_dex_registers, true, kArenaAllocStackMapStream)
49 : nullptr;
Calin Juravlec416d332015-04-23 16:01:43 +010050 if (sp_mask != nullptr) {
51 stack_mask_max_ = std::max(stack_mask_max_, sp_mask->GetHighestBitSet());
52 }
53 if (inlining_depth > 0) {
54 number_of_stack_maps_with_inline_info_++;
55 }
56
57 dex_pc_max_ = std::max(dex_pc_max_, dex_pc);
Calin Juravlec416d332015-04-23 16:01:43 +010058 register_mask_max_ = std::max(register_mask_max_, register_mask);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010059 current_dex_register_ = 0;
Calin Juravlec416d332015-04-23 16:01:43 +010060}
61
Calin Juravle4f46ac52015-04-23 18:47:21 +010062void StackMapStream::EndStackMapEntry() {
Mathieu Chartier32289082017-02-09 15:57:37 -080063 current_entry_.dex_register_map_index = AddDexRegisterMapEntry(current_entry_.dex_register_entry);
Vladimir Marko225b6462015-09-28 12:17:40 +010064 stack_maps_.push_back(current_entry_);
Calin Juravle4f46ac52015-04-23 18:47:21 +010065 current_entry_ = StackMapEntry();
66}
67
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010068void StackMapStream::AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value) {
Calin Juravlec416d332015-04-23 16:01:43 +010069 if (kind != DexRegisterLocation::Kind::kNone) {
70 // Ensure we only use non-compressed location kind at this stage.
David Srbecky7dc11782016-02-25 13:23:56 +000071 DCHECK(DexRegisterLocation::IsShortLocationKind(kind)) << kind;
Calin Juravlec416d332015-04-23 16:01:43 +010072 DexRegisterLocation location(kind, value);
73
74 // Look for Dex register `location` in the location catalog (using the
75 // companion hash map of locations to indices). Use its index if it
76 // is already in the location catalog. If not, insert it (in the
77 // location catalog and the hash map) and use the newly created index.
78 auto it = location_catalog_entries_indices_.Find(location);
79 if (it != location_catalog_entries_indices_.end()) {
80 // Retrieve the index from the hash map.
Vladimir Marko225b6462015-09-28 12:17:40 +010081 dex_register_locations_.push_back(it->second);
Calin Juravlec416d332015-04-23 16:01:43 +010082 } else {
83 // Create a new entry in the location catalog and the hash map.
Vladimir Marko225b6462015-09-28 12:17:40 +010084 size_t index = location_catalog_entries_.size();
85 location_catalog_entries_.push_back(location);
86 dex_register_locations_.push_back(index);
Calin Juravlec416d332015-04-23 16:01:43 +010087 location_catalog_entries_indices_.Insert(std::make_pair(location, index));
88 }
Mathieu Chartier32289082017-02-09 15:57:37 -080089 DexRegisterMapEntry* const entry = in_inline_frame_
90 ? &current_inline_info_.dex_register_entry
91 : &current_entry_.dex_register_entry;
92 DCHECK_LT(current_dex_register_, entry->num_dex_registers);
93 entry->live_dex_registers_mask->SetBit(current_dex_register_);
94 entry->hash += (1 <<
95 (current_dex_register_ % (sizeof(DexRegisterMapEntry::hash) * kBitsPerByte)));
96 entry->hash += static_cast<uint32_t>(value);
97 entry->hash += static_cast<uint32_t>(kind);
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
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800102void StackMapStream::AddInvoke(InvokeType invoke_type, uint32_t dex_method_index) {
103 current_entry_.invoke_type = invoke_type;
104 current_entry_.dex_method_index = dex_method_index;
105}
106
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000107void StackMapStream::BeginInlineInfoEntry(ArtMethod* method,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100108 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000109 uint32_t num_dex_registers,
110 const DexFile* outer_dex_file) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100111 DCHECK(!in_inline_frame_);
112 in_inline_frame_ = true;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000113 if (EncodeArtMethodInInlineInfo(method)) {
114 current_inline_info_.method = method;
115 } else {
116 if (dex_pc != static_cast<uint32_t>(-1) && kIsDebugBuild) {
117 ScopedObjectAccess soa(Thread::Current());
118 DCHECK(IsSameDexFile(*outer_dex_file, *method->GetDexFile()));
119 }
120 current_inline_info_.method_index = method->GetDexMethodIndexUnchecked();
121 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100122 current_inline_info_.dex_pc = dex_pc;
Mathieu Chartier32289082017-02-09 15:57:37 -0800123 current_inline_info_.dex_register_entry.num_dex_registers = num_dex_registers;
124 current_inline_info_.dex_register_entry.locations_start_index = dex_register_locations_.size();
125 current_inline_info_.dex_register_entry.live_dex_registers_mask = (num_dex_registers != 0)
126 ? ArenaBitVector::Create(allocator_, num_dex_registers, true, kArenaAllocStackMapStream)
127 : nullptr;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100128 current_dex_register_ = 0;
129}
130
131void StackMapStream::EndInlineInfoEntry() {
Mathieu Chartier32289082017-02-09 15:57:37 -0800132 current_inline_info_.dex_register_map_index =
133 AddDexRegisterMapEntry(current_inline_info_.dex_register_entry);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100134 DCHECK(in_inline_frame_);
Mathieu Chartier32289082017-02-09 15:57:37 -0800135 DCHECK_EQ(current_dex_register_, current_inline_info_.dex_register_entry.num_dex_registers)
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100136 << "Inline information contains less registers than expected";
137 in_inline_frame_ = false;
Vladimir Marko225b6462015-09-28 12:17:40 +0100138 inline_infos_.push_back(current_inline_info_);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100139 current_inline_info_ = InlineInfoEntry();
Calin Juravlec416d332015-04-23 16:01:43 +0100140}
141
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800142CodeOffset StackMapStream::ComputeMaxNativePcCodeOffset() const {
143 CodeOffset max_native_pc_offset;
Vladimir Marko225b6462015-09-28 12:17:40 +0100144 for (const StackMapEntry& entry : stack_maps_) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800145 max_native_pc_offset = std::max(max_native_pc_offset, entry.native_pc_code_offset);
Vladimir Markobd8c7252015-06-12 10:06:32 +0100146 }
147 return max_native_pc_offset;
148}
149
Calin Juravle4f46ac52015-04-23 18:47:21 +0100150size_t StackMapStream::PrepareForFillIn() {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800151 CodeInfoEncoding encoding;
152 encoding.dex_register_map.num_entries = 0; // TODO: Remove this field.
153 encoding.dex_register_map.num_bytes = ComputeDexRegisterMapsSize();
154 encoding.location_catalog.num_entries = location_catalog_entries_.size();
155 encoding.location_catalog.num_bytes = ComputeDexRegisterLocationCatalogSize();
156 encoding.inline_info.num_entries = inline_infos_.size();
157 ComputeInlineInfoEncoding(&encoding.inline_info.encoding,
158 encoding.dex_register_map.num_bytes);
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800159 CodeOffset max_native_pc_offset = ComputeMaxNativePcCodeOffset();
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800160 // Prepare the CodeInfo variable-sized encoding.
161 encoding.stack_mask.encoding.num_bits = stack_mask_max_ + 1; // Need room for max element too.
162 encoding.stack_mask.num_entries = PrepareStackMasks(encoding.stack_mask.encoding.num_bits);
163 encoding.register_mask.encoding.num_bits = MinimumBitsToStore(register_mask_max_);
164 encoding.register_mask.num_entries = PrepareRegisterMasks();
165 encoding.stack_map.num_entries = stack_maps_.size();
166 encoding.stack_map.encoding.SetFromSizes(
167 // The stack map contains compressed native PC offsets.
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800168 max_native_pc_offset.CompressedValue(),
169 dex_pc_max_,
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800170 encoding.dex_register_map.num_bytes,
171 encoding.inline_info.num_entries,
172 encoding.register_mask.num_entries,
173 encoding.stack_mask.num_entries);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800174 ComputeInvokeInfoEncoding(&encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800175 DCHECK_EQ(code_info_encoding_.size(), 0u);
176 encoding.Compress(&code_info_encoding_);
177 encoding.ComputeTableOffsets();
178 // Compute table offsets so we can get the non header size.
179 DCHECK_EQ(encoding.HeaderSize(), code_info_encoding_.size());
180 needed_size_ = code_info_encoding_.size() + encoding.NonHeaderSize();
Calin Juravle4f46ac52015-04-23 18:47:21 +0100181 return needed_size_;
Calin Juravlec416d332015-04-23 16:01:43 +0100182}
183
184size_t StackMapStream::ComputeDexRegisterLocationCatalogSize() const {
185 size_t size = DexRegisterLocationCatalog::kFixedSize;
Vladimir Marko225b6462015-09-28 12:17:40 +0100186 for (const DexRegisterLocation& dex_register_location : location_catalog_entries_) {
Calin Juravlec416d332015-04-23 16:01:43 +0100187 size += DexRegisterLocationCatalog::EntrySize(dex_register_location);
188 }
189 return size;
190}
191
Mathieu Chartier32289082017-02-09 15:57:37 -0800192size_t StackMapStream::DexRegisterMapEntry::ComputeSize(size_t catalog_size) const {
Vladimir Marko225b6462015-09-28 12:17:40 +0100193 // For num_dex_registers == 0u live_dex_registers_mask may be null.
194 if (num_dex_registers == 0u) {
195 return 0u; // No register map will be emitted.
196 }
197 DCHECK(live_dex_registers_mask != nullptr);
198
Calin Juravlec416d332015-04-23 16:01:43 +0100199 // Size of the map in bytes.
200 size_t size = DexRegisterMap::kFixedSize;
201 // Add the live bit mask for the Dex register liveness.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100202 size += DexRegisterMap::GetLiveBitMaskSize(num_dex_registers);
Calin Juravlec416d332015-04-23 16:01:43 +0100203 // Compute the size of the set of live Dex register entries.
Vladimir Marko225b6462015-09-28 12:17:40 +0100204 size_t number_of_live_dex_registers = live_dex_registers_mask->NumSetBits();
Calin Juravlec416d332015-04-23 16:01:43 +0100205 size_t map_entries_size_in_bits =
Mathieu Chartier32289082017-02-09 15:57:37 -0800206 DexRegisterMap::SingleEntrySizeInBits(catalog_size) * number_of_live_dex_registers;
Calin Juravlec416d332015-04-23 16:01:43 +0100207 size_t map_entries_size_in_bytes =
208 RoundUp(map_entries_size_in_bits, kBitsPerByte) / kBitsPerByte;
209 size += map_entries_size_in_bytes;
210 return size;
211}
212
Calin Juravle4f46ac52015-04-23 18:47:21 +0100213size_t StackMapStream::ComputeDexRegisterMapsSize() const {
Calin Juravlec416d332015-04-23 16:01:43 +0100214 size_t size = 0;
Mathieu Chartier32289082017-02-09 15:57:37 -0800215 for (const DexRegisterMapEntry& entry : dex_register_entries_) {
216 size += entry.ComputeSize(location_catalog_entries_.size());
Calin Juravlec416d332015-04-23 16:01:43 +0100217 }
218 return size;
219}
220
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800221void StackMapStream::ComputeInvokeInfoEncoding(CodeInfoEncoding* encoding) {
222 DCHECK(encoding != nullptr);
223 uint32_t native_pc_max = 0;
224 uint16_t method_index_max = 0;
225 size_t invoke_infos_count = 0;
226 size_t invoke_type_max = 0;
227 for (const StackMapEntry& entry : stack_maps_) {
228 if (entry.dex_method_index != DexFile::kDexNoIndex) {
229 native_pc_max = std::max(native_pc_max, entry.native_pc_code_offset.CompressedValue());
230 method_index_max = std::max(method_index_max, static_cast<uint16_t>(entry.dex_method_index));
231 invoke_type_max = std::max(invoke_type_max, static_cast<size_t>(entry.invoke_type));
232 ++invoke_infos_count;
233 }
234 }
235 encoding->invoke_info.num_entries = invoke_infos_count;
236 encoding->invoke_info.encoding.SetFromSizes(native_pc_max, invoke_type_max, method_index_max);
237}
238
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800239void StackMapStream::ComputeInlineInfoEncoding(InlineInfoEncoding* encoding,
240 size_t dex_register_maps_bytes) {
David Srbecky61b28a12016-02-25 21:55:03 +0000241 uint32_t method_index_max = 0;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100242 uint32_t dex_pc_max = DexFile::kDexNoIndex;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000243 uint32_t extra_data_max = 0;
David Srbecky61b28a12016-02-25 21:55:03 +0000244
245 uint32_t inline_info_index = 0;
246 for (const StackMapEntry& entry : stack_maps_) {
247 for (size_t j = 0; j < entry.inlining_depth; ++j) {
248 InlineInfoEntry inline_entry = inline_infos_[inline_info_index++];
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000249 if (inline_entry.method == nullptr) {
250 method_index_max = std::max(method_index_max, inline_entry.method_index);
251 extra_data_max = std::max(extra_data_max, 1u);
252 } else {
253 method_index_max = std::max(
254 method_index_max, High32Bits(reinterpret_cast<uintptr_t>(inline_entry.method)));
255 extra_data_max = std::max(
256 extra_data_max, Low32Bits(reinterpret_cast<uintptr_t>(inline_entry.method)));
257 }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100258 if (inline_entry.dex_pc != DexFile::kDexNoIndex &&
259 (dex_pc_max == DexFile::kDexNoIndex || dex_pc_max < inline_entry.dex_pc)) {
260 dex_pc_max = inline_entry.dex_pc;
261 }
David Srbecky61b28a12016-02-25 21:55:03 +0000262 }
263 }
264 DCHECK_EQ(inline_info_index, inline_infos_.size());
265
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800266 encoding->SetFromSizes(method_index_max, dex_pc_max, extra_data_max, dex_register_maps_bytes);
Calin Juravlec416d332015-04-23 16:01:43 +0100267}
268
Mathieu Chartier32289082017-02-09 15:57:37 -0800269size_t StackMapStream::MaybeCopyDexRegisterMap(DexRegisterMapEntry& entry,
270 size_t* current_offset,
271 MemoryRegion dex_register_locations_region) {
272 DCHECK(current_offset != nullptr);
273 if ((entry.num_dex_registers == 0) || (entry.live_dex_registers_mask->NumSetBits() == 0)) {
274 // No dex register map needed.
275 return StackMap::kNoDexRegisterMap;
276 }
277 if (entry.offset == DexRegisterMapEntry::kOffsetUnassigned) {
278 // Not already copied, need to copy and and assign an offset.
279 entry.offset = *current_offset;
280 const size_t entry_size = entry.ComputeSize(location_catalog_entries_.size());
281 DexRegisterMap dex_register_map(
282 dex_register_locations_region.Subregion(entry.offset, entry_size));
283 *current_offset += entry_size;
284 // Fill in the map since it was just added.
285 FillInDexRegisterMap(dex_register_map,
286 entry.num_dex_registers,
287 *entry.live_dex_registers_mask,
288 entry.locations_start_index);
289 }
290 return entry.offset;
291}
292
Calin Juravlec416d332015-04-23 16:01:43 +0100293void StackMapStream::FillIn(MemoryRegion region) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100294 DCHECK_EQ(0u, current_entry_.dex_pc) << "EndStackMapEntry not called after BeginStackMapEntry";
295 DCHECK_NE(0u, needed_size_) << "PrepareForFillIn not called before FillIn";
296
Calin Juravle4f46ac52015-04-23 18:47:21 +0100297 DCHECK_EQ(region.size(), needed_size_);
David Srbecky09ed0982016-02-12 21:58:43 +0000298
299 // Note that the memory region does not have to be zeroed when we JIT code
300 // because we do not use the arena allocator there.
301
302 // Write the CodeInfo header.
303 region.CopyFrom(0, MemoryRegion(code_info_encoding_.data(), code_info_encoding_.size()));
Calin Juravlec416d332015-04-23 16:01:43 +0100304
David Srbecky09ed0982016-02-12 21:58:43 +0000305 CodeInfo code_info(region);
306 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800307 DCHECK_EQ(encoding.stack_map.num_entries, stack_maps_.size());
308
309 MemoryRegion dex_register_locations_region = region.Subregion(
310 encoding.dex_register_map.byte_offset,
311 encoding.dex_register_map.num_bytes);
Calin Juravlec416d332015-04-23 16:01:43 +0100312
313 // Set the Dex register location catalog.
Calin Juravlec416d332015-04-23 16:01:43 +0100314 MemoryRegion dex_register_location_catalog_region = region.Subregion(
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800315 encoding.location_catalog.byte_offset,
316 encoding.location_catalog.num_bytes);
Calin Juravlec416d332015-04-23 16:01:43 +0100317 DexRegisterLocationCatalog dex_register_location_catalog(dex_register_location_catalog_region);
318 // Offset in `dex_register_location_catalog` where to store the next
319 // register location.
320 size_t location_catalog_offset = DexRegisterLocationCatalog::kFixedSize;
Vladimir Marko225b6462015-09-28 12:17:40 +0100321 for (DexRegisterLocation dex_register_location : location_catalog_entries_) {
Calin Juravlec416d332015-04-23 16:01:43 +0100322 dex_register_location_catalog.SetRegisterInfo(location_catalog_offset, dex_register_location);
323 location_catalog_offset += DexRegisterLocationCatalog::EntrySize(dex_register_location);
324 }
325 // Ensure we reached the end of the Dex registers location_catalog.
326 DCHECK_EQ(location_catalog_offset, dex_register_location_catalog_region.size());
327
Vladimir Markof6a35de2016-03-21 12:01:50 +0000328 ArenaBitVector empty_bitmask(allocator_, 0, /* expandable */ false, kArenaAllocStackMapStream);
Calin Juravlec416d332015-04-23 16:01:43 +0100329 uintptr_t next_dex_register_map_offset = 0;
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800330 uintptr_t next_inline_info_index = 0;
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800331 size_t invoke_info_idx = 0;
Vladimir Marko225b6462015-09-28 12:17:40 +0100332 for (size_t i = 0, e = stack_maps_.size(); i < e; ++i) {
David Srbecky09ed0982016-02-12 21:58:43 +0000333 StackMap stack_map = code_info.GetStackMapAt(i, encoding);
Vladimir Marko225b6462015-09-28 12:17:40 +0100334 StackMapEntry entry = stack_maps_[i];
Calin Juravlec416d332015-04-23 16:01:43 +0100335
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800336 stack_map.SetDexPc(encoding.stack_map.encoding, entry.dex_pc);
337 stack_map.SetNativePcCodeOffset(encoding.stack_map.encoding, entry.native_pc_code_offset);
338 stack_map.SetRegisterMaskIndex(encoding.stack_map.encoding, entry.register_mask_index);
339 stack_map.SetStackMaskIndex(encoding.stack_map.encoding, entry.stack_mask_index);
Calin Juravlec416d332015-04-23 16:01:43 +0100340
Mathieu Chartier32289082017-02-09 15:57:37 -0800341 size_t offset = MaybeCopyDexRegisterMap(dex_register_entries_[entry.dex_register_map_index],
342 &next_dex_register_map_offset,
343 dex_register_locations_region);
344 stack_map.SetDexRegisterMapOffset(encoding.stack_map.encoding, offset);
Calin Juravlec416d332015-04-23 16:01:43 +0100345
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800346 if (entry.dex_method_index != DexFile::kDexNoIndex) {
347 InvokeInfo invoke_info(code_info.GetInvokeInfo(encoding, invoke_info_idx));
348 invoke_info.SetNativePcCodeOffset(encoding.invoke_info.encoding, entry.native_pc_code_offset);
349 invoke_info.SetInvokeType(encoding.invoke_info.encoding, entry.invoke_type);
350 invoke_info.SetMethodIndex(encoding.invoke_info.encoding, entry.dex_method_index);
351 ++invoke_info_idx;
352 }
353
Calin Juravlec416d332015-04-23 16:01:43 +0100354 // Set the inlining info.
355 if (entry.inlining_depth != 0) {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800356 InlineInfo inline_info = code_info.GetInlineInfo(next_inline_info_index, encoding);
Calin Juravlec416d332015-04-23 16:01:43 +0100357
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800358 // Fill in the index.
359 stack_map.SetInlineInfoIndex(encoding.stack_map.encoding, next_inline_info_index);
360 DCHECK_EQ(next_inline_info_index, entry.inline_infos_start_index);
361 next_inline_info_index += entry.inlining_depth;
Calin Juravlec416d332015-04-23 16:01:43 +0100362
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800363 inline_info.SetDepth(encoding.inline_info.encoding, entry.inlining_depth);
Vladimir Marko225b6462015-09-28 12:17:40 +0100364 DCHECK_LE(entry.inline_infos_start_index + entry.inlining_depth, inline_infos_.size());
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800365
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100366 for (size_t depth = 0; depth < entry.inlining_depth; ++depth) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100367 InlineInfoEntry inline_entry = inline_infos_[depth + entry.inline_infos_start_index];
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000368 if (inline_entry.method != nullptr) {
369 inline_info.SetMethodIndexAtDepth(
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800370 encoding.inline_info.encoding,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000371 depth,
372 High32Bits(reinterpret_cast<uintptr_t>(inline_entry.method)));
373 inline_info.SetExtraDataAtDepth(
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800374 encoding.inline_info.encoding,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000375 depth,
376 Low32Bits(reinterpret_cast<uintptr_t>(inline_entry.method)));
377 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800378 inline_info.SetMethodIndexAtDepth(encoding.inline_info.encoding,
379 depth,
380 inline_entry.method_index);
381 inline_info.SetExtraDataAtDepth(encoding.inline_info.encoding, depth, 1);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000382 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800383 inline_info.SetDexPcAtDepth(encoding.inline_info.encoding, depth, inline_entry.dex_pc);
Mathieu Chartier32289082017-02-09 15:57:37 -0800384 size_t dex_register_map_offset = MaybeCopyDexRegisterMap(
385 dex_register_entries_[inline_entry.dex_register_map_index],
386 &next_dex_register_map_offset,
387 dex_register_locations_region);
388 inline_info.SetDexRegisterMapOffsetAtDepth(encoding.inline_info.encoding,
389 depth,
390 dex_register_map_offset);
Calin Juravlec416d332015-04-23 16:01:43 +0100391 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800392 } else if (encoding.stack_map.encoding.GetInlineInfoEncoding().BitSize() > 0) {
393 stack_map.SetInlineInfoIndex(encoding.stack_map.encoding, StackMap::kNoInlineInfo);
Calin Juravlec416d332015-04-23 16:01:43 +0100394 }
395 }
David Srbecky1bbdfd72016-02-24 16:39:26 +0000396
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800397 // Write stack masks table.
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800398 const size_t stack_mask_bits = encoding.stack_mask.encoding.BitSize();
David Srbecky45aa5982016-03-18 02:15:09 +0000399 if (stack_mask_bits > 0) {
400 size_t stack_mask_bytes = RoundUp(stack_mask_bits, kBitsPerByte) / kBitsPerByte;
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800401 for (size_t i = 0; i < encoding.stack_mask.num_entries; ++i) {
David Srbecky45aa5982016-03-18 02:15:09 +0000402 MemoryRegion source(&stack_masks_[i * stack_mask_bytes], stack_mask_bytes);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800403 BitMemoryRegion stack_mask = code_info.GetStackMask(i, encoding);
404 for (size_t bit_index = 0; bit_index < stack_mask_bits; ++bit_index) {
David Srbecky45aa5982016-03-18 02:15:09 +0000405 stack_mask.StoreBit(bit_index, source.LoadBit(bit_index));
406 }
407 }
408 }
409
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800410 // Write register masks table.
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800411 for (size_t i = 0; i < encoding.register_mask.num_entries; ++i) {
412 BitMemoryRegion register_mask = code_info.GetRegisterMask(i, encoding);
413 register_mask.StoreBits(0, register_masks_[i], encoding.register_mask.encoding.BitSize());
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800414 }
415
David Srbecky1bbdfd72016-02-24 16:39:26 +0000416 // Verify all written data in debug build.
417 if (kIsDebugBuild) {
418 CheckCodeInfo(region);
419 }
Calin Juravlec416d332015-04-23 16:01:43 +0100420}
421
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100422void StackMapStream::FillInDexRegisterMap(DexRegisterMap dex_register_map,
423 uint32_t num_dex_registers,
424 const BitVector& live_dex_registers_mask,
425 uint32_t start_index_in_dex_register_locations) const {
426 dex_register_map.SetLiveBitMask(num_dex_registers, live_dex_registers_mask);
427 // Set the dex register location mapping data.
Vladimir Marko225b6462015-09-28 12:17:40 +0100428 size_t number_of_live_dex_registers = live_dex_registers_mask.NumSetBits();
429 DCHECK_LE(number_of_live_dex_registers, dex_register_locations_.size());
430 DCHECK_LE(start_index_in_dex_register_locations,
431 dex_register_locations_.size() - number_of_live_dex_registers);
432 for (size_t index_in_dex_register_locations = 0;
433 index_in_dex_register_locations != number_of_live_dex_registers;
434 ++index_in_dex_register_locations) {
435 size_t location_catalog_entry_index = dex_register_locations_[
436 start_index_in_dex_register_locations + index_in_dex_register_locations];
437 dex_register_map.SetLocationCatalogEntryIndex(
438 index_in_dex_register_locations,
439 location_catalog_entry_index,
440 num_dex_registers,
441 location_catalog_entries_.size());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100442 }
443}
444
Mathieu Chartier32289082017-02-09 15:57:37 -0800445size_t StackMapStream::AddDexRegisterMapEntry(const DexRegisterMapEntry& entry) {
446 const size_t current_entry_index = dex_register_entries_.size();
447 auto entries_it = dex_map_hash_to_stack_map_indices_.find(entry.hash);
Calin Juravlec416d332015-04-23 16:01:43 +0100448 if (entries_it == dex_map_hash_to_stack_map_indices_.end()) {
449 // We don't have a perfect hash functions so we need a list to collect all stack maps
450 // which might have the same dex register map.
Vladimir Marko225b6462015-09-28 12:17:40 +0100451 ArenaVector<uint32_t> stack_map_indices(allocator_->Adapter(kArenaAllocStackMapStream));
452 stack_map_indices.push_back(current_entry_index);
Mathieu Chartier32289082017-02-09 15:57:37 -0800453 dex_map_hash_to_stack_map_indices_.Put(entry.hash, std::move(stack_map_indices));
454 } else {
455 // We might have collisions, so we need to check whether or not we really have a match.
456 for (uint32_t test_entry_index : entries_it->second) {
457 if (DexRegisterMapEntryEquals(dex_register_entries_[test_entry_index], entry)) {
458 return test_entry_index;
459 }
Calin Juravlec416d332015-04-23 16:01:43 +0100460 }
Mathieu Chartier32289082017-02-09 15:57:37 -0800461 entries_it->second.push_back(current_entry_index);
Calin Juravlec416d332015-04-23 16:01:43 +0100462 }
Mathieu Chartier32289082017-02-09 15:57:37 -0800463 dex_register_entries_.push_back(entry);
464 return current_entry_index;
Calin Juravlec416d332015-04-23 16:01:43 +0100465}
466
Mathieu Chartier32289082017-02-09 15:57:37 -0800467bool StackMapStream::DexRegisterMapEntryEquals(const DexRegisterMapEntry& a,
468 const DexRegisterMapEntry& b) const {
469 if ((a.live_dex_registers_mask == nullptr) != (b.live_dex_registers_mask == nullptr)) {
Calin Juravlec416d332015-04-23 16:01:43 +0100470 return false;
471 }
472 if (a.num_dex_registers != b.num_dex_registers) {
473 return false;
474 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100475 if (a.num_dex_registers != 0u) {
476 DCHECK(a.live_dex_registers_mask != nullptr);
477 DCHECK(b.live_dex_registers_mask != nullptr);
478 if (!a.live_dex_registers_mask->Equal(b.live_dex_registers_mask)) {
Calin Juravlec416d332015-04-23 16:01:43 +0100479 return false;
480 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100481 size_t number_of_live_dex_registers = a.live_dex_registers_mask->NumSetBits();
482 DCHECK_LE(number_of_live_dex_registers, dex_register_locations_.size());
Mathieu Chartier32289082017-02-09 15:57:37 -0800483 DCHECK_LE(a.locations_start_index,
Vladimir Marko225b6462015-09-28 12:17:40 +0100484 dex_register_locations_.size() - number_of_live_dex_registers);
Mathieu Chartier32289082017-02-09 15:57:37 -0800485 DCHECK_LE(b.locations_start_index,
Vladimir Marko225b6462015-09-28 12:17:40 +0100486 dex_register_locations_.size() - number_of_live_dex_registers);
Mathieu Chartier32289082017-02-09 15:57:37 -0800487 auto a_begin = dex_register_locations_.begin() + a.locations_start_index;
488 auto b_begin = dex_register_locations_.begin() + b.locations_start_index;
Vladimir Marko225b6462015-09-28 12:17:40 +0100489 if (!std::equal(a_begin, a_begin + number_of_live_dex_registers, b_begin)) {
490 return false;
Calin Juravlec416d332015-04-23 16:01:43 +0100491 }
492 }
493 return true;
494}
495
David Srbecky1bbdfd72016-02-24 16:39:26 +0000496// Helper for CheckCodeInfo - check that register map has the expected content.
497void StackMapStream::CheckDexRegisterMap(const CodeInfo& code_info,
498 const DexRegisterMap& dex_register_map,
499 size_t num_dex_registers,
500 BitVector* live_dex_registers_mask,
501 size_t dex_register_locations_index) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000502 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Srbecky1bbdfd72016-02-24 16:39:26 +0000503 for (size_t reg = 0; reg < num_dex_registers; reg++) {
504 // Find the location we tried to encode.
505 DexRegisterLocation expected = DexRegisterLocation::None();
506 if (live_dex_registers_mask->IsBitSet(reg)) {
507 size_t catalog_index = dex_register_locations_[dex_register_locations_index++];
508 expected = location_catalog_entries_[catalog_index];
509 }
510 // Compare to the seen location.
511 if (expected.GetKind() == DexRegisterLocation::Kind::kNone) {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800512 DCHECK(!dex_register_map.IsValid() || !dex_register_map.IsDexRegisterLive(reg))
513 << dex_register_map.IsValid() << " " << dex_register_map.IsDexRegisterLive(reg);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000514 } else {
515 DCHECK(dex_register_map.IsDexRegisterLive(reg));
516 DexRegisterLocation seen = dex_register_map.GetDexRegisterLocation(
517 reg, num_dex_registers, code_info, encoding);
518 DCHECK_EQ(expected.GetKind(), seen.GetKind());
519 DCHECK_EQ(expected.GetValue(), seen.GetValue());
520 }
521 }
522 if (num_dex_registers == 0) {
523 DCHECK(!dex_register_map.IsValid());
524 }
525}
526
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800527size_t StackMapStream::PrepareRegisterMasks() {
528 register_masks_.resize(stack_maps_.size(), 0u);
529 std::unordered_map<uint32_t, size_t> dedupe;
530 for (StackMapEntry& stack_map : stack_maps_) {
531 const size_t index = dedupe.size();
532 stack_map.register_mask_index = dedupe.emplace(stack_map.register_mask, index).first->second;
533 register_masks_[index] = stack_map.register_mask;
534 }
535 return dedupe.size();
536}
537
David Srbecky45aa5982016-03-18 02:15:09 +0000538size_t StackMapStream::PrepareStackMasks(size_t entry_size_in_bits) {
539 // Preallocate memory since we do not want it to move (the dedup map will point into it).
540 const size_t byte_entry_size = RoundUp(entry_size_in_bits, kBitsPerByte) / kBitsPerByte;
541 stack_masks_.resize(byte_entry_size * stack_maps_.size(), 0u);
542 // For deduplicating we store the stack masks as byte packed for simplicity. We can bit pack later
543 // when copying out from stack_masks_.
544 std::unordered_map<MemoryRegion,
545 size_t,
546 FNVHash<MemoryRegion>,
547 MemoryRegion::ContentEquals> dedup(stack_maps_.size());
548 for (StackMapEntry& stack_map : stack_maps_) {
549 size_t index = dedup.size();
550 MemoryRegion stack_mask(stack_masks_.data() + index * byte_entry_size, byte_entry_size);
551 for (size_t i = 0; i < entry_size_in_bits; i++) {
552 stack_mask.StoreBit(i, stack_map.sp_mask != nullptr && stack_map.sp_mask->IsBitSet(i));
553 }
554 stack_map.stack_mask_index = dedup.emplace(stack_mask, index).first->second;
555 }
556 return dedup.size();
557}
558
David Srbecky1bbdfd72016-02-24 16:39:26 +0000559// Check that all StackMapStream inputs are correctly encoded by trying to read them back.
560void StackMapStream::CheckCodeInfo(MemoryRegion region) const {
561 CodeInfo code_info(region);
David Srbecky09ed0982016-02-12 21:58:43 +0000562 CodeInfoEncoding encoding = code_info.ExtractEncoding();
563 DCHECK_EQ(code_info.GetNumberOfStackMaps(encoding), stack_maps_.size());
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800564 size_t invoke_info_index = 0;
David Srbecky1bbdfd72016-02-24 16:39:26 +0000565 for (size_t s = 0; s < stack_maps_.size(); ++s) {
566 const StackMap stack_map = code_info.GetStackMapAt(s, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800567 const StackMapEncoding& stack_map_encoding = encoding.stack_map.encoding;
David Srbecky1bbdfd72016-02-24 16:39:26 +0000568 StackMapEntry entry = stack_maps_[s];
569
570 // Check main stack map fields.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800571 DCHECK_EQ(stack_map.GetNativePcOffset(stack_map_encoding, instruction_set_),
572 entry.native_pc_code_offset.Uint32Value(instruction_set_));
David Srbecky09ed0982016-02-12 21:58:43 +0000573 DCHECK_EQ(stack_map.GetDexPc(stack_map_encoding), entry.dex_pc);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800574 DCHECK_EQ(stack_map.GetRegisterMaskIndex(stack_map_encoding), entry.register_mask_index);
575 DCHECK_EQ(code_info.GetRegisterMaskOf(encoding, stack_map), entry.register_mask);
David Srbecky45aa5982016-03-18 02:15:09 +0000576 const size_t num_stack_mask_bits = code_info.GetNumberOfStackMaskBits(encoding);
577 DCHECK_EQ(stack_map.GetStackMaskIndex(stack_map_encoding), entry.stack_mask_index);
578 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(encoding, stack_map);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000579 if (entry.sp_mask != nullptr) {
David Srbecky45aa5982016-03-18 02:15:09 +0000580 DCHECK_GE(stack_mask.size_in_bits(), entry.sp_mask->GetNumberOfBits());
David Srbecky09ed0982016-02-12 21:58:43 +0000581 for (size_t b = 0; b < num_stack_mask_bits; b++) {
David Srbecky45aa5982016-03-18 02:15:09 +0000582 DCHECK_EQ(stack_mask.LoadBit(b), entry.sp_mask->IsBitSet(b));
David Srbecky1bbdfd72016-02-24 16:39:26 +0000583 }
584 } else {
David Srbecky09ed0982016-02-12 21:58:43 +0000585 for (size_t b = 0; b < num_stack_mask_bits; b++) {
David Srbecky45aa5982016-03-18 02:15:09 +0000586 DCHECK_EQ(stack_mask.LoadBit(b), 0u);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000587 }
588 }
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800589 if (entry.dex_method_index != DexFile::kDexNoIndex) {
590 InvokeInfo invoke_info = code_info.GetInvokeInfo(encoding, invoke_info_index);
591 DCHECK_EQ(invoke_info.GetNativePcOffset(encoding.invoke_info.encoding, instruction_set_),
592 entry.native_pc_code_offset.Uint32Value(instruction_set_));
593 DCHECK_EQ(invoke_info.GetInvokeType(encoding.invoke_info.encoding), entry.invoke_type);
594 DCHECK_EQ(invoke_info.GetMethodIndex(encoding.invoke_info.encoding), entry.dex_method_index);
595 invoke_info_index++;
596 }
David Srbecky1bbdfd72016-02-24 16:39:26 +0000597 CheckDexRegisterMap(code_info,
598 code_info.GetDexRegisterMapOf(
Mathieu Chartier32289082017-02-09 15:57:37 -0800599 stack_map, encoding, entry.dex_register_entry.num_dex_registers),
600 entry.dex_register_entry.num_dex_registers,
601 entry.dex_register_entry.live_dex_registers_mask,
602 entry.dex_register_entry.locations_start_index);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000603
604 // Check inline info.
David Srbecky09ed0982016-02-12 21:58:43 +0000605 DCHECK_EQ(stack_map.HasInlineInfo(stack_map_encoding), (entry.inlining_depth != 0));
David Srbecky1bbdfd72016-02-24 16:39:26 +0000606 if (entry.inlining_depth != 0) {
607 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800608 DCHECK_EQ(inline_info.GetDepth(encoding.inline_info.encoding), entry.inlining_depth);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000609 for (size_t d = 0; d < entry.inlining_depth; ++d) {
610 size_t inline_info_index = entry.inline_infos_start_index + d;
611 DCHECK_LT(inline_info_index, inline_infos_.size());
612 InlineInfoEntry inline_entry = inline_infos_[inline_info_index];
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800613 DCHECK_EQ(inline_info.GetDexPcAtDepth(encoding.inline_info.encoding, d),
David Srbecky61b28a12016-02-25 21:55:03 +0000614 inline_entry.dex_pc);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800615 if (inline_info.EncodesArtMethodAtDepth(encoding.inline_info.encoding, d)) {
616 DCHECK_EQ(inline_info.GetArtMethodAtDepth(encoding.inline_info.encoding, d),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000617 inline_entry.method);
618 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800619 DCHECK_EQ(inline_info.GetMethodIndexAtDepth(encoding.inline_info.encoding, d),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000620 inline_entry.method_index);
621 }
David Srbecky1bbdfd72016-02-24 16:39:26 +0000622
623 CheckDexRegisterMap(code_info,
624 code_info.GetDexRegisterMapAtDepth(
Mathieu Chartier32289082017-02-09 15:57:37 -0800625 d,
626 inline_info,
627 encoding,
628 inline_entry.dex_register_entry.num_dex_registers),
629 inline_entry.dex_register_entry.num_dex_registers,
630 inline_entry.dex_register_entry.live_dex_registers_mask,
631 inline_entry.dex_register_entry.locations_start_index);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000632 }
633 }
634 }
635}
636
Calin Juravlec416d332015-04-23 16:01:43 +0100637} // namespace art