blob: 4d12ad6eb6ef5fd8477aeb23ad432e99b1ae6ef7 [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
Andreas Gampe90b936d2017-01-31 08:58:55 -080019#include "art_method-inl.h"
David Srbecky45aa5982016-03-18 02:15:09 +000020#include "base/stl_util.h"
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +000021#include "optimizing/optimizing_compiler.h"
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000022#include "runtime.h"
23#include "scoped_thread_state_change-inl.h"
24
Calin Juravlec416d332015-04-23 16:01:43 +010025namespace art {
26
Calin Juravle4f46ac52015-04-23 18:47:21 +010027void StackMapStream::BeginStackMapEntry(uint32_t dex_pc,
28 uint32_t native_pc_offset,
29 uint32_t register_mask,
30 BitVector* sp_mask,
31 uint32_t num_dex_registers,
32 uint8_t inlining_depth) {
33 DCHECK_EQ(0u, current_entry_.dex_pc) << "EndStackMapEntry not called after BeginStackMapEntry";
Calin Juravleb5c46932015-10-06 17:09:49 +010034 DCHECK_NE(dex_pc, static_cast<uint32_t>(-1)) << "invalid dex_pc";
Calin Juravle4f46ac52015-04-23 18:47:21 +010035 current_entry_.dex_pc = dex_pc;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -080036 current_entry_.native_pc_code_offset = CodeOffset::FromOffset(native_pc_offset, instruction_set_);
Calin Juravle4f46ac52015-04-23 18:47:21 +010037 current_entry_.register_mask = register_mask;
38 current_entry_.sp_mask = sp_mask;
Calin Juravle4f46ac52015-04-23 18:47:21 +010039 current_entry_.inlining_depth = inlining_depth;
Vladimir Marko225b6462015-09-28 12:17:40 +010040 current_entry_.inline_infos_start_index = inline_infos_.size();
David Srbecky45aa5982016-03-18 02:15:09 +000041 current_entry_.stack_mask_index = 0;
Mathieu Chartierd776ff02017-01-17 09:32:18 -080042 current_entry_.dex_method_index = DexFile::kDexNoIndex;
Mathieu Chartier32289082017-02-09 15:57:37 -080043 current_entry_.dex_register_entry.num_dex_registers = num_dex_registers;
44 current_entry_.dex_register_entry.locations_start_index = dex_register_locations_.size();
45 current_entry_.dex_register_entry.live_dex_registers_mask = (num_dex_registers != 0)
46 ? ArenaBitVector::Create(allocator_, num_dex_registers, true, kArenaAllocStackMapStream)
47 : nullptr;
Calin Juravlec416d332015-04-23 16:01:43 +010048 if (sp_mask != nullptr) {
49 stack_mask_max_ = std::max(stack_mask_max_, sp_mask->GetHighestBitSet());
50 }
51 if (inlining_depth > 0) {
52 number_of_stack_maps_with_inline_info_++;
53 }
54
55 dex_pc_max_ = std::max(dex_pc_max_, dex_pc);
Calin Juravlec416d332015-04-23 16:01:43 +010056 register_mask_max_ = std::max(register_mask_max_, register_mask);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010057 current_dex_register_ = 0;
Calin Juravlec416d332015-04-23 16:01:43 +010058}
59
Calin Juravle4f46ac52015-04-23 18:47:21 +010060void StackMapStream::EndStackMapEntry() {
Mathieu Chartier32289082017-02-09 15:57:37 -080061 current_entry_.dex_register_map_index = AddDexRegisterMapEntry(current_entry_.dex_register_entry);
Vladimir Marko225b6462015-09-28 12:17:40 +010062 stack_maps_.push_back(current_entry_);
Calin Juravle4f46ac52015-04-23 18:47:21 +010063 current_entry_ = StackMapEntry();
64}
65
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010066void StackMapStream::AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value) {
Calin Juravlec416d332015-04-23 16:01:43 +010067 if (kind != DexRegisterLocation::Kind::kNone) {
68 // Ensure we only use non-compressed location kind at this stage.
David Srbecky7dc11782016-02-25 13:23:56 +000069 DCHECK(DexRegisterLocation::IsShortLocationKind(kind)) << kind;
Calin Juravlec416d332015-04-23 16:01:43 +010070 DexRegisterLocation location(kind, value);
71
72 // Look for Dex register `location` in the location catalog (using the
73 // companion hash map of locations to indices). Use its index if it
74 // is already in the location catalog. If not, insert it (in the
75 // location catalog and the hash map) and use the newly created index.
76 auto it = location_catalog_entries_indices_.Find(location);
77 if (it != location_catalog_entries_indices_.end()) {
78 // Retrieve the index from the hash map.
Vladimir Marko225b6462015-09-28 12:17:40 +010079 dex_register_locations_.push_back(it->second);
Calin Juravlec416d332015-04-23 16:01:43 +010080 } else {
81 // Create a new entry in the location catalog and the hash map.
Vladimir Marko225b6462015-09-28 12:17:40 +010082 size_t index = location_catalog_entries_.size();
83 location_catalog_entries_.push_back(location);
84 dex_register_locations_.push_back(index);
Calin Juravlec416d332015-04-23 16:01:43 +010085 location_catalog_entries_indices_.Insert(std::make_pair(location, index));
86 }
Mathieu Chartier32289082017-02-09 15:57:37 -080087 DexRegisterMapEntry* const entry = in_inline_frame_
88 ? &current_inline_info_.dex_register_entry
89 : &current_entry_.dex_register_entry;
90 DCHECK_LT(current_dex_register_, entry->num_dex_registers);
91 entry->live_dex_registers_mask->SetBit(current_dex_register_);
92 entry->hash += (1 <<
93 (current_dex_register_ % (sizeof(DexRegisterMapEntry::hash) * kBitsPerByte)));
94 entry->hash += static_cast<uint32_t>(value);
95 entry->hash += static_cast<uint32_t>(kind);
Calin Juravlec416d332015-04-23 16:01:43 +010096 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010097 current_dex_register_++;
Calin Juravlec416d332015-04-23 16:01:43 +010098}
99
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800100void StackMapStream::AddInvoke(InvokeType invoke_type, uint32_t dex_method_index) {
101 current_entry_.invoke_type = invoke_type;
102 current_entry_.dex_method_index = dex_method_index;
103}
104
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000105void StackMapStream::BeginInlineInfoEntry(ArtMethod* method,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100106 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000107 uint32_t num_dex_registers,
108 const DexFile* outer_dex_file) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100109 DCHECK(!in_inline_frame_);
110 in_inline_frame_ = true;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000111 if (EncodeArtMethodInInlineInfo(method)) {
112 current_inline_info_.method = method;
113 } else {
114 if (dex_pc != static_cast<uint32_t>(-1) && kIsDebugBuild) {
115 ScopedObjectAccess soa(Thread::Current());
116 DCHECK(IsSameDexFile(*outer_dex_file, *method->GetDexFile()));
117 }
118 current_inline_info_.method_index = method->GetDexMethodIndexUnchecked();
119 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100120 current_inline_info_.dex_pc = dex_pc;
Mathieu Chartier32289082017-02-09 15:57:37 -0800121 current_inline_info_.dex_register_entry.num_dex_registers = num_dex_registers;
122 current_inline_info_.dex_register_entry.locations_start_index = dex_register_locations_.size();
123 current_inline_info_.dex_register_entry.live_dex_registers_mask = (num_dex_registers != 0)
124 ? ArenaBitVector::Create(allocator_, num_dex_registers, true, kArenaAllocStackMapStream)
125 : nullptr;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100126 current_dex_register_ = 0;
127}
128
129void StackMapStream::EndInlineInfoEntry() {
Mathieu Chartier32289082017-02-09 15:57:37 -0800130 current_inline_info_.dex_register_map_index =
131 AddDexRegisterMapEntry(current_inline_info_.dex_register_entry);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100132 DCHECK(in_inline_frame_);
Mathieu Chartier32289082017-02-09 15:57:37 -0800133 DCHECK_EQ(current_dex_register_, current_inline_info_.dex_register_entry.num_dex_registers)
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100134 << "Inline information contains less registers than expected";
135 in_inline_frame_ = false;
Vladimir Marko225b6462015-09-28 12:17:40 +0100136 inline_infos_.push_back(current_inline_info_);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100137 current_inline_info_ = InlineInfoEntry();
Calin Juravlec416d332015-04-23 16:01:43 +0100138}
139
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800140CodeOffset StackMapStream::ComputeMaxNativePcCodeOffset() const {
141 CodeOffset max_native_pc_offset;
Vladimir Marko225b6462015-09-28 12:17:40 +0100142 for (const StackMapEntry& entry : stack_maps_) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800143 max_native_pc_offset = std::max(max_native_pc_offset, entry.native_pc_code_offset);
Vladimir Markobd8c7252015-06-12 10:06:32 +0100144 }
145 return max_native_pc_offset;
146}
147
Calin Juravle4f46ac52015-04-23 18:47:21 +0100148size_t StackMapStream::PrepareForFillIn() {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800149 CodeInfoEncoding encoding;
150 encoding.dex_register_map.num_entries = 0; // TODO: Remove this field.
151 encoding.dex_register_map.num_bytes = ComputeDexRegisterMapsSize();
152 encoding.location_catalog.num_entries = location_catalog_entries_.size();
153 encoding.location_catalog.num_bytes = ComputeDexRegisterLocationCatalogSize();
154 encoding.inline_info.num_entries = inline_infos_.size();
155 ComputeInlineInfoEncoding(&encoding.inline_info.encoding,
156 encoding.dex_register_map.num_bytes);
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800157 CodeOffset max_native_pc_offset = ComputeMaxNativePcCodeOffset();
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800158 // Prepare the CodeInfo variable-sized encoding.
159 encoding.stack_mask.encoding.num_bits = stack_mask_max_ + 1; // Need room for max element too.
160 encoding.stack_mask.num_entries = PrepareStackMasks(encoding.stack_mask.encoding.num_bits);
161 encoding.register_mask.encoding.num_bits = MinimumBitsToStore(register_mask_max_);
162 encoding.register_mask.num_entries = PrepareRegisterMasks();
163 encoding.stack_map.num_entries = stack_maps_.size();
164 encoding.stack_map.encoding.SetFromSizes(
165 // The stack map contains compressed native PC offsets.
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800166 max_native_pc_offset.CompressedValue(),
167 dex_pc_max_,
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800168 encoding.dex_register_map.num_bytes,
169 encoding.inline_info.num_entries,
170 encoding.register_mask.num_entries,
171 encoding.stack_mask.num_entries);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800172 ComputeInvokeInfoEncoding(&encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800173 DCHECK_EQ(code_info_encoding_.size(), 0u);
174 encoding.Compress(&code_info_encoding_);
175 encoding.ComputeTableOffsets();
176 // Compute table offsets so we can get the non header size.
177 DCHECK_EQ(encoding.HeaderSize(), code_info_encoding_.size());
178 needed_size_ = code_info_encoding_.size() + encoding.NonHeaderSize();
Calin Juravle4f46ac52015-04-23 18:47:21 +0100179 return needed_size_;
Calin Juravlec416d332015-04-23 16:01:43 +0100180}
181
182size_t StackMapStream::ComputeDexRegisterLocationCatalogSize() const {
183 size_t size = DexRegisterLocationCatalog::kFixedSize;
Vladimir Marko225b6462015-09-28 12:17:40 +0100184 for (const DexRegisterLocation& dex_register_location : location_catalog_entries_) {
Calin Juravlec416d332015-04-23 16:01:43 +0100185 size += DexRegisterLocationCatalog::EntrySize(dex_register_location);
186 }
187 return size;
188}
189
Mathieu Chartier32289082017-02-09 15:57:37 -0800190size_t StackMapStream::DexRegisterMapEntry::ComputeSize(size_t catalog_size) const {
Vladimir Marko225b6462015-09-28 12:17:40 +0100191 // For num_dex_registers == 0u live_dex_registers_mask may be null.
192 if (num_dex_registers == 0u) {
193 return 0u; // No register map will be emitted.
194 }
195 DCHECK(live_dex_registers_mask != nullptr);
196
Calin Juravlec416d332015-04-23 16:01:43 +0100197 // Size of the map in bytes.
198 size_t size = DexRegisterMap::kFixedSize;
199 // Add the live bit mask for the Dex register liveness.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100200 size += DexRegisterMap::GetLiveBitMaskSize(num_dex_registers);
Calin Juravlec416d332015-04-23 16:01:43 +0100201 // Compute the size of the set of live Dex register entries.
Vladimir Marko225b6462015-09-28 12:17:40 +0100202 size_t number_of_live_dex_registers = live_dex_registers_mask->NumSetBits();
Calin Juravlec416d332015-04-23 16:01:43 +0100203 size_t map_entries_size_in_bits =
Mathieu Chartier32289082017-02-09 15:57:37 -0800204 DexRegisterMap::SingleEntrySizeInBits(catalog_size) * number_of_live_dex_registers;
Calin Juravlec416d332015-04-23 16:01:43 +0100205 size_t map_entries_size_in_bytes =
206 RoundUp(map_entries_size_in_bits, kBitsPerByte) / kBitsPerByte;
207 size += map_entries_size_in_bytes;
208 return size;
209}
210
Calin Juravle4f46ac52015-04-23 18:47:21 +0100211size_t StackMapStream::ComputeDexRegisterMapsSize() const {
Calin Juravlec416d332015-04-23 16:01:43 +0100212 size_t size = 0;
Mathieu Chartier32289082017-02-09 15:57:37 -0800213 for (const DexRegisterMapEntry& entry : dex_register_entries_) {
214 size += entry.ComputeSize(location_catalog_entries_.size());
Calin Juravlec416d332015-04-23 16:01:43 +0100215 }
216 return size;
217}
218
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800219void StackMapStream::ComputeInvokeInfoEncoding(CodeInfoEncoding* encoding) {
220 DCHECK(encoding != nullptr);
221 uint32_t native_pc_max = 0;
222 uint16_t method_index_max = 0;
223 size_t invoke_infos_count = 0;
224 size_t invoke_type_max = 0;
225 for (const StackMapEntry& entry : stack_maps_) {
226 if (entry.dex_method_index != DexFile::kDexNoIndex) {
227 native_pc_max = std::max(native_pc_max, entry.native_pc_code_offset.CompressedValue());
228 method_index_max = std::max(method_index_max, static_cast<uint16_t>(entry.dex_method_index));
229 invoke_type_max = std::max(invoke_type_max, static_cast<size_t>(entry.invoke_type));
230 ++invoke_infos_count;
231 }
232 }
233 encoding->invoke_info.num_entries = invoke_infos_count;
234 encoding->invoke_info.encoding.SetFromSizes(native_pc_max, invoke_type_max, method_index_max);
235}
236
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800237void StackMapStream::ComputeInlineInfoEncoding(InlineInfoEncoding* encoding,
238 size_t dex_register_maps_bytes) {
David Srbecky61b28a12016-02-25 21:55:03 +0000239 uint32_t method_index_max = 0;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100240 uint32_t dex_pc_max = DexFile::kDexNoIndex;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000241 uint32_t extra_data_max = 0;
David Srbecky61b28a12016-02-25 21:55:03 +0000242
243 uint32_t inline_info_index = 0;
244 for (const StackMapEntry& entry : stack_maps_) {
245 for (size_t j = 0; j < entry.inlining_depth; ++j) {
246 InlineInfoEntry inline_entry = inline_infos_[inline_info_index++];
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000247 if (inline_entry.method == nullptr) {
248 method_index_max = std::max(method_index_max, inline_entry.method_index);
249 extra_data_max = std::max(extra_data_max, 1u);
250 } else {
251 method_index_max = std::max(
252 method_index_max, High32Bits(reinterpret_cast<uintptr_t>(inline_entry.method)));
253 extra_data_max = std::max(
254 extra_data_max, Low32Bits(reinterpret_cast<uintptr_t>(inline_entry.method)));
255 }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100256 if (inline_entry.dex_pc != DexFile::kDexNoIndex &&
257 (dex_pc_max == DexFile::kDexNoIndex || dex_pc_max < inline_entry.dex_pc)) {
258 dex_pc_max = inline_entry.dex_pc;
259 }
David Srbecky61b28a12016-02-25 21:55:03 +0000260 }
261 }
262 DCHECK_EQ(inline_info_index, inline_infos_.size());
263
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800264 encoding->SetFromSizes(method_index_max, dex_pc_max, extra_data_max, dex_register_maps_bytes);
Calin Juravlec416d332015-04-23 16:01:43 +0100265}
266
Mathieu Chartier32289082017-02-09 15:57:37 -0800267size_t StackMapStream::MaybeCopyDexRegisterMap(DexRegisterMapEntry& entry,
268 size_t* current_offset,
269 MemoryRegion dex_register_locations_region) {
270 DCHECK(current_offset != nullptr);
271 if ((entry.num_dex_registers == 0) || (entry.live_dex_registers_mask->NumSetBits() == 0)) {
272 // No dex register map needed.
273 return StackMap::kNoDexRegisterMap;
274 }
275 if (entry.offset == DexRegisterMapEntry::kOffsetUnassigned) {
276 // Not already copied, need to copy and and assign an offset.
277 entry.offset = *current_offset;
278 const size_t entry_size = entry.ComputeSize(location_catalog_entries_.size());
279 DexRegisterMap dex_register_map(
280 dex_register_locations_region.Subregion(entry.offset, entry_size));
281 *current_offset += entry_size;
282 // Fill in the map since it was just added.
283 FillInDexRegisterMap(dex_register_map,
284 entry.num_dex_registers,
285 *entry.live_dex_registers_mask,
286 entry.locations_start_index);
287 }
288 return entry.offset;
289}
290
Calin Juravlec416d332015-04-23 16:01:43 +0100291void StackMapStream::FillIn(MemoryRegion region) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100292 DCHECK_EQ(0u, current_entry_.dex_pc) << "EndStackMapEntry not called after BeginStackMapEntry";
293 DCHECK_NE(0u, needed_size_) << "PrepareForFillIn not called before FillIn";
294
Calin Juravle4f46ac52015-04-23 18:47:21 +0100295 DCHECK_EQ(region.size(), needed_size_);
David Srbecky09ed0982016-02-12 21:58:43 +0000296
297 // Note that the memory region does not have to be zeroed when we JIT code
298 // because we do not use the arena allocator there.
299
300 // Write the CodeInfo header.
301 region.CopyFrom(0, MemoryRegion(code_info_encoding_.data(), code_info_encoding_.size()));
Calin Juravlec416d332015-04-23 16:01:43 +0100302
David Srbecky09ed0982016-02-12 21:58:43 +0000303 CodeInfo code_info(region);
304 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800305 DCHECK_EQ(encoding.stack_map.num_entries, stack_maps_.size());
306
307 MemoryRegion dex_register_locations_region = region.Subregion(
308 encoding.dex_register_map.byte_offset,
309 encoding.dex_register_map.num_bytes);
Calin Juravlec416d332015-04-23 16:01:43 +0100310
311 // Set the Dex register location catalog.
Calin Juravlec416d332015-04-23 16:01:43 +0100312 MemoryRegion dex_register_location_catalog_region = region.Subregion(
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800313 encoding.location_catalog.byte_offset,
314 encoding.location_catalog.num_bytes);
Calin Juravlec416d332015-04-23 16:01:43 +0100315 DexRegisterLocationCatalog dex_register_location_catalog(dex_register_location_catalog_region);
316 // Offset in `dex_register_location_catalog` where to store the next
317 // register location.
318 size_t location_catalog_offset = DexRegisterLocationCatalog::kFixedSize;
Vladimir Marko225b6462015-09-28 12:17:40 +0100319 for (DexRegisterLocation dex_register_location : location_catalog_entries_) {
Calin Juravlec416d332015-04-23 16:01:43 +0100320 dex_register_location_catalog.SetRegisterInfo(location_catalog_offset, dex_register_location);
321 location_catalog_offset += DexRegisterLocationCatalog::EntrySize(dex_register_location);
322 }
323 // Ensure we reached the end of the Dex registers location_catalog.
324 DCHECK_EQ(location_catalog_offset, dex_register_location_catalog_region.size());
325
Vladimir Markof6a35de2016-03-21 12:01:50 +0000326 ArenaBitVector empty_bitmask(allocator_, 0, /* expandable */ false, kArenaAllocStackMapStream);
Calin Juravlec416d332015-04-23 16:01:43 +0100327 uintptr_t next_dex_register_map_offset = 0;
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800328 uintptr_t next_inline_info_index = 0;
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800329 size_t invoke_info_idx = 0;
Vladimir Marko225b6462015-09-28 12:17:40 +0100330 for (size_t i = 0, e = stack_maps_.size(); i < e; ++i) {
David Srbecky09ed0982016-02-12 21:58:43 +0000331 StackMap stack_map = code_info.GetStackMapAt(i, encoding);
Vladimir Marko225b6462015-09-28 12:17:40 +0100332 StackMapEntry entry = stack_maps_[i];
Calin Juravlec416d332015-04-23 16:01:43 +0100333
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800334 stack_map.SetDexPc(encoding.stack_map.encoding, entry.dex_pc);
335 stack_map.SetNativePcCodeOffset(encoding.stack_map.encoding, entry.native_pc_code_offset);
336 stack_map.SetRegisterMaskIndex(encoding.stack_map.encoding, entry.register_mask_index);
337 stack_map.SetStackMaskIndex(encoding.stack_map.encoding, entry.stack_mask_index);
Calin Juravlec416d332015-04-23 16:01:43 +0100338
Mathieu Chartier32289082017-02-09 15:57:37 -0800339 size_t offset = MaybeCopyDexRegisterMap(dex_register_entries_[entry.dex_register_map_index],
340 &next_dex_register_map_offset,
341 dex_register_locations_region);
342 stack_map.SetDexRegisterMapOffset(encoding.stack_map.encoding, offset);
Calin Juravlec416d332015-04-23 16:01:43 +0100343
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800344 if (entry.dex_method_index != DexFile::kDexNoIndex) {
345 InvokeInfo invoke_info(code_info.GetInvokeInfo(encoding, invoke_info_idx));
346 invoke_info.SetNativePcCodeOffset(encoding.invoke_info.encoding, entry.native_pc_code_offset);
347 invoke_info.SetInvokeType(encoding.invoke_info.encoding, entry.invoke_type);
348 invoke_info.SetMethodIndex(encoding.invoke_info.encoding, entry.dex_method_index);
349 ++invoke_info_idx;
350 }
351
Calin Juravlec416d332015-04-23 16:01:43 +0100352 // Set the inlining info.
353 if (entry.inlining_depth != 0) {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800354 InlineInfo inline_info = code_info.GetInlineInfo(next_inline_info_index, encoding);
Calin Juravlec416d332015-04-23 16:01:43 +0100355
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800356 // Fill in the index.
357 stack_map.SetInlineInfoIndex(encoding.stack_map.encoding, next_inline_info_index);
358 DCHECK_EQ(next_inline_info_index, entry.inline_infos_start_index);
359 next_inline_info_index += entry.inlining_depth;
Calin Juravlec416d332015-04-23 16:01:43 +0100360
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800361 inline_info.SetDepth(encoding.inline_info.encoding, entry.inlining_depth);
Vladimir Marko225b6462015-09-28 12:17:40 +0100362 DCHECK_LE(entry.inline_infos_start_index + entry.inlining_depth, inline_infos_.size());
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800363
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100364 for (size_t depth = 0; depth < entry.inlining_depth; ++depth) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100365 InlineInfoEntry inline_entry = inline_infos_[depth + entry.inline_infos_start_index];
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000366 if (inline_entry.method != nullptr) {
367 inline_info.SetMethodIndexAtDepth(
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800368 encoding.inline_info.encoding,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000369 depth,
370 High32Bits(reinterpret_cast<uintptr_t>(inline_entry.method)));
371 inline_info.SetExtraDataAtDepth(
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800372 encoding.inline_info.encoding,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000373 depth,
374 Low32Bits(reinterpret_cast<uintptr_t>(inline_entry.method)));
375 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800376 inline_info.SetMethodIndexAtDepth(encoding.inline_info.encoding,
377 depth,
378 inline_entry.method_index);
379 inline_info.SetExtraDataAtDepth(encoding.inline_info.encoding, depth, 1);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000380 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800381 inline_info.SetDexPcAtDepth(encoding.inline_info.encoding, depth, inline_entry.dex_pc);
Mathieu Chartier32289082017-02-09 15:57:37 -0800382 size_t dex_register_map_offset = MaybeCopyDexRegisterMap(
383 dex_register_entries_[inline_entry.dex_register_map_index],
384 &next_dex_register_map_offset,
385 dex_register_locations_region);
386 inline_info.SetDexRegisterMapOffsetAtDepth(encoding.inline_info.encoding,
387 depth,
388 dex_register_map_offset);
Calin Juravlec416d332015-04-23 16:01:43 +0100389 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800390 } else if (encoding.stack_map.encoding.GetInlineInfoEncoding().BitSize() > 0) {
391 stack_map.SetInlineInfoIndex(encoding.stack_map.encoding, StackMap::kNoInlineInfo);
Calin Juravlec416d332015-04-23 16:01:43 +0100392 }
393 }
David Srbecky1bbdfd72016-02-24 16:39:26 +0000394
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800395 // Write stack masks table.
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800396 const size_t stack_mask_bits = encoding.stack_mask.encoding.BitSize();
David Srbecky45aa5982016-03-18 02:15:09 +0000397 if (stack_mask_bits > 0) {
398 size_t stack_mask_bytes = RoundUp(stack_mask_bits, kBitsPerByte) / kBitsPerByte;
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800399 for (size_t i = 0; i < encoding.stack_mask.num_entries; ++i) {
David Srbecky45aa5982016-03-18 02:15:09 +0000400 MemoryRegion source(&stack_masks_[i * stack_mask_bytes], stack_mask_bytes);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800401 BitMemoryRegion stack_mask = code_info.GetStackMask(i, encoding);
402 for (size_t bit_index = 0; bit_index < stack_mask_bits; ++bit_index) {
David Srbecky45aa5982016-03-18 02:15:09 +0000403 stack_mask.StoreBit(bit_index, source.LoadBit(bit_index));
404 }
405 }
406 }
407
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800408 // Write register masks table.
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800409 for (size_t i = 0; i < encoding.register_mask.num_entries; ++i) {
410 BitMemoryRegion register_mask = code_info.GetRegisterMask(i, encoding);
411 register_mask.StoreBits(0, register_masks_[i], encoding.register_mask.encoding.BitSize());
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800412 }
413
David Srbecky1bbdfd72016-02-24 16:39:26 +0000414 // Verify all written data in debug build.
415 if (kIsDebugBuild) {
416 CheckCodeInfo(region);
417 }
Calin Juravlec416d332015-04-23 16:01:43 +0100418}
419
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100420void StackMapStream::FillInDexRegisterMap(DexRegisterMap dex_register_map,
421 uint32_t num_dex_registers,
422 const BitVector& live_dex_registers_mask,
423 uint32_t start_index_in_dex_register_locations) const {
424 dex_register_map.SetLiveBitMask(num_dex_registers, live_dex_registers_mask);
425 // Set the dex register location mapping data.
Vladimir Marko225b6462015-09-28 12:17:40 +0100426 size_t number_of_live_dex_registers = live_dex_registers_mask.NumSetBits();
427 DCHECK_LE(number_of_live_dex_registers, dex_register_locations_.size());
428 DCHECK_LE(start_index_in_dex_register_locations,
429 dex_register_locations_.size() - number_of_live_dex_registers);
430 for (size_t index_in_dex_register_locations = 0;
431 index_in_dex_register_locations != number_of_live_dex_registers;
432 ++index_in_dex_register_locations) {
433 size_t location_catalog_entry_index = dex_register_locations_[
434 start_index_in_dex_register_locations + index_in_dex_register_locations];
435 dex_register_map.SetLocationCatalogEntryIndex(
436 index_in_dex_register_locations,
437 location_catalog_entry_index,
438 num_dex_registers,
439 location_catalog_entries_.size());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100440 }
441}
442
Mathieu Chartier32289082017-02-09 15:57:37 -0800443size_t StackMapStream::AddDexRegisterMapEntry(const DexRegisterMapEntry& entry) {
444 const size_t current_entry_index = dex_register_entries_.size();
445 auto entries_it = dex_map_hash_to_stack_map_indices_.find(entry.hash);
Calin Juravlec416d332015-04-23 16:01:43 +0100446 if (entries_it == dex_map_hash_to_stack_map_indices_.end()) {
447 // We don't have a perfect hash functions so we need a list to collect all stack maps
448 // which might have the same dex register map.
Vladimir Marko225b6462015-09-28 12:17:40 +0100449 ArenaVector<uint32_t> stack_map_indices(allocator_->Adapter(kArenaAllocStackMapStream));
450 stack_map_indices.push_back(current_entry_index);
Mathieu Chartier32289082017-02-09 15:57:37 -0800451 dex_map_hash_to_stack_map_indices_.Put(entry.hash, std::move(stack_map_indices));
452 } else {
453 // We might have collisions, so we need to check whether or not we really have a match.
454 for (uint32_t test_entry_index : entries_it->second) {
455 if (DexRegisterMapEntryEquals(dex_register_entries_[test_entry_index], entry)) {
456 return test_entry_index;
457 }
Calin Juravlec416d332015-04-23 16:01:43 +0100458 }
Mathieu Chartier32289082017-02-09 15:57:37 -0800459 entries_it->second.push_back(current_entry_index);
Calin Juravlec416d332015-04-23 16:01:43 +0100460 }
Mathieu Chartier32289082017-02-09 15:57:37 -0800461 dex_register_entries_.push_back(entry);
462 return current_entry_index;
Calin Juravlec416d332015-04-23 16:01:43 +0100463}
464
Mathieu Chartier32289082017-02-09 15:57:37 -0800465bool StackMapStream::DexRegisterMapEntryEquals(const DexRegisterMapEntry& a,
466 const DexRegisterMapEntry& b) const {
467 if ((a.live_dex_registers_mask == nullptr) != (b.live_dex_registers_mask == nullptr)) {
Calin Juravlec416d332015-04-23 16:01:43 +0100468 return false;
469 }
470 if (a.num_dex_registers != b.num_dex_registers) {
471 return false;
472 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100473 if (a.num_dex_registers != 0u) {
474 DCHECK(a.live_dex_registers_mask != nullptr);
475 DCHECK(b.live_dex_registers_mask != nullptr);
476 if (!a.live_dex_registers_mask->Equal(b.live_dex_registers_mask)) {
Calin Juravlec416d332015-04-23 16:01:43 +0100477 return false;
478 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100479 size_t number_of_live_dex_registers = a.live_dex_registers_mask->NumSetBits();
480 DCHECK_LE(number_of_live_dex_registers, dex_register_locations_.size());
Mathieu Chartier32289082017-02-09 15:57:37 -0800481 DCHECK_LE(a.locations_start_index,
Vladimir Marko225b6462015-09-28 12:17:40 +0100482 dex_register_locations_.size() - number_of_live_dex_registers);
Mathieu Chartier32289082017-02-09 15:57:37 -0800483 DCHECK_LE(b.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 auto a_begin = dex_register_locations_.begin() + a.locations_start_index;
486 auto b_begin = dex_register_locations_.begin() + b.locations_start_index;
Vladimir Marko225b6462015-09-28 12:17:40 +0100487 if (!std::equal(a_begin, a_begin + number_of_live_dex_registers, b_begin)) {
488 return false;
Calin Juravlec416d332015-04-23 16:01:43 +0100489 }
490 }
491 return true;
492}
493
David Srbecky1bbdfd72016-02-24 16:39:26 +0000494// Helper for CheckCodeInfo - check that register map has the expected content.
495void StackMapStream::CheckDexRegisterMap(const CodeInfo& code_info,
496 const DexRegisterMap& dex_register_map,
497 size_t num_dex_registers,
498 BitVector* live_dex_registers_mask,
499 size_t dex_register_locations_index) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000500 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Srbecky1bbdfd72016-02-24 16:39:26 +0000501 for (size_t reg = 0; reg < num_dex_registers; reg++) {
502 // Find the location we tried to encode.
503 DexRegisterLocation expected = DexRegisterLocation::None();
504 if (live_dex_registers_mask->IsBitSet(reg)) {
505 size_t catalog_index = dex_register_locations_[dex_register_locations_index++];
506 expected = location_catalog_entries_[catalog_index];
507 }
508 // Compare to the seen location.
509 if (expected.GetKind() == DexRegisterLocation::Kind::kNone) {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800510 DCHECK(!dex_register_map.IsValid() || !dex_register_map.IsDexRegisterLive(reg))
511 << dex_register_map.IsValid() << " " << dex_register_map.IsDexRegisterLive(reg);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000512 } else {
513 DCHECK(dex_register_map.IsDexRegisterLive(reg));
514 DexRegisterLocation seen = dex_register_map.GetDexRegisterLocation(
515 reg, num_dex_registers, code_info, encoding);
516 DCHECK_EQ(expected.GetKind(), seen.GetKind());
517 DCHECK_EQ(expected.GetValue(), seen.GetValue());
518 }
519 }
520 if (num_dex_registers == 0) {
521 DCHECK(!dex_register_map.IsValid());
522 }
523}
524
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800525size_t StackMapStream::PrepareRegisterMasks() {
526 register_masks_.resize(stack_maps_.size(), 0u);
Vladimir Marko9c571132017-02-22 11:59:57 +0000527 ArenaUnorderedMap<uint32_t, size_t> dedupe(allocator_->Adapter(kArenaAllocStackMapStream));
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800528 for (StackMapEntry& stack_map : stack_maps_) {
529 const size_t index = dedupe.size();
530 stack_map.register_mask_index = dedupe.emplace(stack_map.register_mask, index).first->second;
531 register_masks_[index] = stack_map.register_mask;
532 }
533 return dedupe.size();
534}
535
David Srbecky45aa5982016-03-18 02:15:09 +0000536size_t StackMapStream::PrepareStackMasks(size_t entry_size_in_bits) {
537 // Preallocate memory since we do not want it to move (the dedup map will point into it).
538 const size_t byte_entry_size = RoundUp(entry_size_in_bits, kBitsPerByte) / kBitsPerByte;
539 stack_masks_.resize(byte_entry_size * stack_maps_.size(), 0u);
540 // For deduplicating we store the stack masks as byte packed for simplicity. We can bit pack later
541 // when copying out from stack_masks_.
Vladimir Marko9c571132017-02-22 11:59:57 +0000542 ArenaUnorderedMap<MemoryRegion,
543 size_t,
544 FNVHash<MemoryRegion>,
545 MemoryRegion::ContentEquals> dedup(
546 stack_maps_.size(), allocator_->Adapter(kArenaAllocStackMapStream));
David Srbecky45aa5982016-03-18 02:15:09 +0000547 for (StackMapEntry& stack_map : stack_maps_) {
548 size_t index = dedup.size();
549 MemoryRegion stack_mask(stack_masks_.data() + index * byte_entry_size, byte_entry_size);
550 for (size_t i = 0; i < entry_size_in_bits; i++) {
551 stack_mask.StoreBit(i, stack_map.sp_mask != nullptr && stack_map.sp_mask->IsBitSet(i));
552 }
553 stack_map.stack_mask_index = dedup.emplace(stack_mask, index).first->second;
554 }
555 return dedup.size();
556}
557
David Srbecky1bbdfd72016-02-24 16:39:26 +0000558// Check that all StackMapStream inputs are correctly encoded by trying to read them back.
559void StackMapStream::CheckCodeInfo(MemoryRegion region) const {
560 CodeInfo code_info(region);
David Srbecky09ed0982016-02-12 21:58:43 +0000561 CodeInfoEncoding encoding = code_info.ExtractEncoding();
562 DCHECK_EQ(code_info.GetNumberOfStackMaps(encoding), stack_maps_.size());
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800563 size_t invoke_info_index = 0;
David Srbecky1bbdfd72016-02-24 16:39:26 +0000564 for (size_t s = 0; s < stack_maps_.size(); ++s) {
565 const StackMap stack_map = code_info.GetStackMapAt(s, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800566 const StackMapEncoding& stack_map_encoding = encoding.stack_map.encoding;
David Srbecky1bbdfd72016-02-24 16:39:26 +0000567 StackMapEntry entry = stack_maps_[s];
568
569 // Check main stack map fields.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800570 DCHECK_EQ(stack_map.GetNativePcOffset(stack_map_encoding, instruction_set_),
571 entry.native_pc_code_offset.Uint32Value(instruction_set_));
David Srbecky09ed0982016-02-12 21:58:43 +0000572 DCHECK_EQ(stack_map.GetDexPc(stack_map_encoding), entry.dex_pc);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800573 DCHECK_EQ(stack_map.GetRegisterMaskIndex(stack_map_encoding), entry.register_mask_index);
574 DCHECK_EQ(code_info.GetRegisterMaskOf(encoding, stack_map), entry.register_mask);
David Srbecky45aa5982016-03-18 02:15:09 +0000575 const size_t num_stack_mask_bits = code_info.GetNumberOfStackMaskBits(encoding);
576 DCHECK_EQ(stack_map.GetStackMaskIndex(stack_map_encoding), entry.stack_mask_index);
577 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(encoding, stack_map);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000578 if (entry.sp_mask != nullptr) {
David Srbecky45aa5982016-03-18 02:15:09 +0000579 DCHECK_GE(stack_mask.size_in_bits(), entry.sp_mask->GetNumberOfBits());
David Srbecky09ed0982016-02-12 21:58:43 +0000580 for (size_t b = 0; b < num_stack_mask_bits; b++) {
David Srbecky45aa5982016-03-18 02:15:09 +0000581 DCHECK_EQ(stack_mask.LoadBit(b), entry.sp_mask->IsBitSet(b));
David Srbecky1bbdfd72016-02-24 16:39:26 +0000582 }
583 } else {
David Srbecky09ed0982016-02-12 21:58:43 +0000584 for (size_t b = 0; b < num_stack_mask_bits; b++) {
David Srbecky45aa5982016-03-18 02:15:09 +0000585 DCHECK_EQ(stack_mask.LoadBit(b), 0u);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000586 }
587 }
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800588 if (entry.dex_method_index != DexFile::kDexNoIndex) {
589 InvokeInfo invoke_info = code_info.GetInvokeInfo(encoding, invoke_info_index);
590 DCHECK_EQ(invoke_info.GetNativePcOffset(encoding.invoke_info.encoding, instruction_set_),
591 entry.native_pc_code_offset.Uint32Value(instruction_set_));
592 DCHECK_EQ(invoke_info.GetInvokeType(encoding.invoke_info.encoding), entry.invoke_type);
593 DCHECK_EQ(invoke_info.GetMethodIndex(encoding.invoke_info.encoding), entry.dex_method_index);
594 invoke_info_index++;
595 }
David Srbecky1bbdfd72016-02-24 16:39:26 +0000596 CheckDexRegisterMap(code_info,
597 code_info.GetDexRegisterMapOf(
Mathieu Chartier32289082017-02-09 15:57:37 -0800598 stack_map, encoding, entry.dex_register_entry.num_dex_registers),
599 entry.dex_register_entry.num_dex_registers,
600 entry.dex_register_entry.live_dex_registers_mask,
601 entry.dex_register_entry.locations_start_index);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000602
603 // Check inline info.
David Srbecky09ed0982016-02-12 21:58:43 +0000604 DCHECK_EQ(stack_map.HasInlineInfo(stack_map_encoding), (entry.inlining_depth != 0));
David Srbecky1bbdfd72016-02-24 16:39:26 +0000605 if (entry.inlining_depth != 0) {
606 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800607 DCHECK_EQ(inline_info.GetDepth(encoding.inline_info.encoding), entry.inlining_depth);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000608 for (size_t d = 0; d < entry.inlining_depth; ++d) {
609 size_t inline_info_index = entry.inline_infos_start_index + d;
610 DCHECK_LT(inline_info_index, inline_infos_.size());
611 InlineInfoEntry inline_entry = inline_infos_[inline_info_index];
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800612 DCHECK_EQ(inline_info.GetDexPcAtDepth(encoding.inline_info.encoding, d),
David Srbecky61b28a12016-02-25 21:55:03 +0000613 inline_entry.dex_pc);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800614 if (inline_info.EncodesArtMethodAtDepth(encoding.inline_info.encoding, d)) {
615 DCHECK_EQ(inline_info.GetArtMethodAtDepth(encoding.inline_info.encoding, d),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000616 inline_entry.method);
617 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800618 DCHECK_EQ(inline_info.GetMethodIndexAtDepth(encoding.inline_info.encoding, d),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000619 inline_entry.method_index);
620 }
David Srbecky1bbdfd72016-02-24 16:39:26 +0000621
622 CheckDexRegisterMap(code_info,
623 code_info.GetDexRegisterMapAtDepth(
Mathieu Chartier32289082017-02-09 15:57:37 -0800624 d,
625 inline_info,
626 encoding,
627 inline_entry.dex_register_entry.num_dex_registers),
628 inline_entry.dex_register_entry.num_dex_registers,
629 inline_entry.dex_register_entry.live_dex_registers_mask,
630 inline_entry.dex_register_entry.locations_start_index);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000631 }
632 }
633 }
634}
635
Calin Juravlec416d332015-04-23 16:01:43 +0100636} // namespace art