blob: f8e01b753747852a0cbf19fb5f9c5d608306cdc1 [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;
41 current_entry_.num_dex_registers = num_dex_registers;
42 current_entry_.inlining_depth = inlining_depth;
Vladimir Marko225b6462015-09-28 12:17:40 +010043 current_entry_.dex_register_locations_start_index = dex_register_locations_.size();
44 current_entry_.inline_infos_start_index = inline_infos_.size();
Calin Juravle4f46ac52015-04-23 18:47:21 +010045 current_entry_.dex_register_map_hash = 0;
46 current_entry_.same_dex_register_map_as_ = kNoSameDexMapFound;
David Srbecky45aa5982016-03-18 02:15:09 +000047 current_entry_.stack_mask_index = 0;
Calin Juravlec416d332015-04-23 16:01:43 +010048 if (num_dex_registers != 0) {
Calin Juravle4f46ac52015-04-23 18:47:21 +010049 current_entry_.live_dex_registers_mask =
Vladimir Markof6a35de2016-03-21 12:01:50 +000050 ArenaBitVector::Create(allocator_, num_dex_registers, true, kArenaAllocStackMapStream);
Calin Juravlec416d332015-04-23 16:01:43 +010051 } else {
Calin Juravle4f46ac52015-04-23 18:47:21 +010052 current_entry_.live_dex_registers_mask = nullptr;
Calin Juravlec416d332015-04-23 16:01:43 +010053 }
Calin Juravlec416d332015-04-23 16:01:43 +010054
55 if (sp_mask != nullptr) {
56 stack_mask_max_ = std::max(stack_mask_max_, sp_mask->GetHighestBitSet());
57 }
58 if (inlining_depth > 0) {
59 number_of_stack_maps_with_inline_info_++;
60 }
61
62 dex_pc_max_ = std::max(dex_pc_max_, dex_pc);
Calin Juravlec416d332015-04-23 16:01:43 +010063 register_mask_max_ = std::max(register_mask_max_, register_mask);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010064 current_dex_register_ = 0;
Calin Juravlec416d332015-04-23 16:01:43 +010065}
66
Calin Juravle4f46ac52015-04-23 18:47:21 +010067void StackMapStream::EndStackMapEntry() {
68 current_entry_.same_dex_register_map_as_ = FindEntryWithTheSameDexMap();
Vladimir Marko225b6462015-09-28 12:17:40 +010069 stack_maps_.push_back(current_entry_);
Calin Juravle4f46ac52015-04-23 18:47:21 +010070 current_entry_ = StackMapEntry();
71}
72
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010073void StackMapStream::AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value) {
Calin Juravlec416d332015-04-23 16:01:43 +010074 if (kind != DexRegisterLocation::Kind::kNone) {
75 // Ensure we only use non-compressed location kind at this stage.
David Srbecky7dc11782016-02-25 13:23:56 +000076 DCHECK(DexRegisterLocation::IsShortLocationKind(kind)) << kind;
Calin Juravlec416d332015-04-23 16:01:43 +010077 DexRegisterLocation location(kind, value);
78
79 // Look for Dex register `location` in the location catalog (using the
80 // companion hash map of locations to indices). Use its index if it
81 // is already in the location catalog. If not, insert it (in the
82 // location catalog and the hash map) and use the newly created index.
83 auto it = location_catalog_entries_indices_.Find(location);
84 if (it != location_catalog_entries_indices_.end()) {
85 // Retrieve the index from the hash map.
Vladimir Marko225b6462015-09-28 12:17:40 +010086 dex_register_locations_.push_back(it->second);
Calin Juravlec416d332015-04-23 16:01:43 +010087 } else {
88 // Create a new entry in the location catalog and the hash map.
Vladimir Marko225b6462015-09-28 12:17:40 +010089 size_t index = location_catalog_entries_.size();
90 location_catalog_entries_.push_back(location);
91 dex_register_locations_.push_back(index);
Calin Juravlec416d332015-04-23 16:01:43 +010092 location_catalog_entries_indices_.Insert(std::make_pair(location, index));
93 }
94
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010095 if (in_inline_frame_) {
96 // TODO: Support sharing DexRegisterMap across InlineInfo.
97 DCHECK_LT(current_dex_register_, current_inline_info_.num_dex_registers);
98 current_inline_info_.live_dex_registers_mask->SetBit(current_dex_register_);
99 } else {
100 DCHECK_LT(current_dex_register_, current_entry_.num_dex_registers);
101 current_entry_.live_dex_registers_mask->SetBit(current_dex_register_);
102 current_entry_.dex_register_map_hash += (1 <<
103 (current_dex_register_ % (sizeof(current_entry_.dex_register_map_hash) * kBitsPerByte)));
104 current_entry_.dex_register_map_hash += static_cast<uint32_t>(value);
105 current_entry_.dex_register_map_hash += static_cast<uint32_t>(kind);
106 }
Calin Juravlec416d332015-04-23 16:01:43 +0100107 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100108 current_dex_register_++;
Calin Juravlec416d332015-04-23 16:01:43 +0100109}
110
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000111void StackMapStream::BeginInlineInfoEntry(ArtMethod* method,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100112 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000113 uint32_t num_dex_registers,
114 const DexFile* outer_dex_file) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100115 DCHECK(!in_inline_frame_);
116 in_inline_frame_ = true;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000117 if (EncodeArtMethodInInlineInfo(method)) {
118 current_inline_info_.method = method;
119 } else {
120 if (dex_pc != static_cast<uint32_t>(-1) && kIsDebugBuild) {
121 ScopedObjectAccess soa(Thread::Current());
122 DCHECK(IsSameDexFile(*outer_dex_file, *method->GetDexFile()));
123 }
124 current_inline_info_.method_index = method->GetDexMethodIndexUnchecked();
125 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100126 current_inline_info_.dex_pc = dex_pc;
127 current_inline_info_.num_dex_registers = num_dex_registers;
Vladimir Marko225b6462015-09-28 12:17:40 +0100128 current_inline_info_.dex_register_locations_start_index = dex_register_locations_.size();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100129 if (num_dex_registers != 0) {
130 current_inline_info_.live_dex_registers_mask =
Vladimir Markof6a35de2016-03-21 12:01:50 +0000131 ArenaBitVector::Create(allocator_, num_dex_registers, true, kArenaAllocStackMapStream);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100132 } else {
133 current_inline_info_.live_dex_registers_mask = nullptr;
134 }
135 current_dex_register_ = 0;
136}
137
138void StackMapStream::EndInlineInfoEntry() {
139 DCHECK(in_inline_frame_);
140 DCHECK_EQ(current_dex_register_, current_inline_info_.num_dex_registers)
141 << "Inline information contains less registers than expected";
142 in_inline_frame_ = false;
Vladimir Marko225b6462015-09-28 12:17:40 +0100143 inline_infos_.push_back(current_inline_info_);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100144 current_inline_info_ = InlineInfoEntry();
Calin Juravlec416d332015-04-23 16:01:43 +0100145}
146
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800147CodeOffset StackMapStream::ComputeMaxNativePcCodeOffset() const {
148 CodeOffset max_native_pc_offset;
Vladimir Marko225b6462015-09-28 12:17:40 +0100149 for (const StackMapEntry& entry : stack_maps_) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800150 max_native_pc_offset = std::max(max_native_pc_offset, entry.native_pc_code_offset);
Vladimir Markobd8c7252015-06-12 10:06:32 +0100151 }
152 return max_native_pc_offset;
153}
154
Calin Juravle4f46ac52015-04-23 18:47:21 +0100155size_t StackMapStream::PrepareForFillIn() {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800156 CodeInfoEncoding encoding;
157 encoding.dex_register_map.num_entries = 0; // TODO: Remove this field.
158 encoding.dex_register_map.num_bytes = ComputeDexRegisterMapsSize();
159 encoding.location_catalog.num_entries = location_catalog_entries_.size();
160 encoding.location_catalog.num_bytes = ComputeDexRegisterLocationCatalogSize();
161 encoding.inline_info.num_entries = inline_infos_.size();
162 ComputeInlineInfoEncoding(&encoding.inline_info.encoding,
163 encoding.dex_register_map.num_bytes);
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800164 CodeOffset max_native_pc_offset = ComputeMaxNativePcCodeOffset();
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800165 // Prepare the CodeInfo variable-sized encoding.
166 encoding.stack_mask.encoding.num_bits = stack_mask_max_ + 1; // Need room for max element too.
167 encoding.stack_mask.num_entries = PrepareStackMasks(encoding.stack_mask.encoding.num_bits);
168 encoding.register_mask.encoding.num_bits = MinimumBitsToStore(register_mask_max_);
169 encoding.register_mask.num_entries = PrepareRegisterMasks();
170 encoding.stack_map.num_entries = stack_maps_.size();
171 encoding.stack_map.encoding.SetFromSizes(
172 // The stack map contains compressed native PC offsets.
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800173 max_native_pc_offset.CompressedValue(),
174 dex_pc_max_,
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800175 encoding.dex_register_map.num_bytes,
176 encoding.inline_info.num_entries,
177 encoding.register_mask.num_entries,
178 encoding.stack_mask.num_entries);
179 DCHECK_EQ(code_info_encoding_.size(), 0u);
180 encoding.Compress(&code_info_encoding_);
181 encoding.ComputeTableOffsets();
182 // Compute table offsets so we can get the non header size.
183 DCHECK_EQ(encoding.HeaderSize(), code_info_encoding_.size());
184 needed_size_ = code_info_encoding_.size() + encoding.NonHeaderSize();
Calin Juravle4f46ac52015-04-23 18:47:21 +0100185 return needed_size_;
Calin Juravlec416d332015-04-23 16:01:43 +0100186}
187
188size_t StackMapStream::ComputeDexRegisterLocationCatalogSize() const {
189 size_t size = DexRegisterLocationCatalog::kFixedSize;
Vladimir Marko225b6462015-09-28 12:17:40 +0100190 for (const DexRegisterLocation& dex_register_location : location_catalog_entries_) {
Calin Juravlec416d332015-04-23 16:01:43 +0100191 size += DexRegisterLocationCatalog::EntrySize(dex_register_location);
192 }
193 return size;
194}
195
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100196size_t StackMapStream::ComputeDexRegisterMapSize(uint32_t num_dex_registers,
Vladimir Marko225b6462015-09-28 12:17:40 +0100197 const BitVector* live_dex_registers_mask) const {
198 // For num_dex_registers == 0u live_dex_registers_mask may be null.
199 if (num_dex_registers == 0u) {
200 return 0u; // No register map will be emitted.
201 }
202 DCHECK(live_dex_registers_mask != nullptr);
203
Calin Juravlec416d332015-04-23 16:01:43 +0100204 // Size of the map in bytes.
205 size_t size = DexRegisterMap::kFixedSize;
206 // Add the live bit mask for the Dex register liveness.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100207 size += DexRegisterMap::GetLiveBitMaskSize(num_dex_registers);
Calin Juravlec416d332015-04-23 16:01:43 +0100208 // Compute the size of the set of live Dex register entries.
Vladimir Marko225b6462015-09-28 12:17:40 +0100209 size_t number_of_live_dex_registers = live_dex_registers_mask->NumSetBits();
Calin Juravlec416d332015-04-23 16:01:43 +0100210 size_t map_entries_size_in_bits =
Vladimir Marko225b6462015-09-28 12:17:40 +0100211 DexRegisterMap::SingleEntrySizeInBits(location_catalog_entries_.size())
Calin Juravlec416d332015-04-23 16:01:43 +0100212 * number_of_live_dex_registers;
213 size_t map_entries_size_in_bytes =
214 RoundUp(map_entries_size_in_bits, kBitsPerByte) / kBitsPerByte;
215 size += map_entries_size_in_bytes;
216 return size;
217}
218
Calin Juravle4f46ac52015-04-23 18:47:21 +0100219size_t StackMapStream::ComputeDexRegisterMapsSize() const {
Calin Juravlec416d332015-04-23 16:01:43 +0100220 size_t size = 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100221 size_t inline_info_index = 0;
Vladimir Marko225b6462015-09-28 12:17:40 +0100222 for (const StackMapEntry& entry : stack_maps_) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100223 if (entry.same_dex_register_map_as_ == kNoSameDexMapFound) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100224 size += ComputeDexRegisterMapSize(entry.num_dex_registers, entry.live_dex_registers_mask);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100225 } else {
Calin Juravlec416d332015-04-23 16:01:43 +0100226 // Entries with the same dex map will have the same offset.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100227 }
228 for (size_t j = 0; j < entry.inlining_depth; ++j) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100229 InlineInfoEntry inline_entry = inline_infos_[inline_info_index++];
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100230 size += ComputeDexRegisterMapSize(inline_entry.num_dex_registers,
Vladimir Marko225b6462015-09-28 12:17:40 +0100231 inline_entry.live_dex_registers_mask);
Calin Juravlec416d332015-04-23 16:01:43 +0100232 }
233 }
234 return size;
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
Calin Juravlec416d332015-04-23 16:01:43 +0100267void StackMapStream::FillIn(MemoryRegion region) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100268 DCHECK_EQ(0u, current_entry_.dex_pc) << "EndStackMapEntry not called after BeginStackMapEntry";
269 DCHECK_NE(0u, needed_size_) << "PrepareForFillIn not called before FillIn";
270
Calin Juravle4f46ac52015-04-23 18:47:21 +0100271 DCHECK_EQ(region.size(), needed_size_);
David Srbecky09ed0982016-02-12 21:58:43 +0000272
273 // Note that the memory region does not have to be zeroed when we JIT code
274 // because we do not use the arena allocator there.
275
276 // Write the CodeInfo header.
277 region.CopyFrom(0, MemoryRegion(code_info_encoding_.data(), code_info_encoding_.size()));
Calin Juravlec416d332015-04-23 16:01:43 +0100278
David Srbecky09ed0982016-02-12 21:58:43 +0000279 CodeInfo code_info(region);
280 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800281 DCHECK_EQ(encoding.stack_map.num_entries, stack_maps_.size());
282
283 MemoryRegion dex_register_locations_region = region.Subregion(
284 encoding.dex_register_map.byte_offset,
285 encoding.dex_register_map.num_bytes);
Calin Juravlec416d332015-04-23 16:01:43 +0100286
287 // Set the Dex register location catalog.
Calin Juravlec416d332015-04-23 16:01:43 +0100288 MemoryRegion dex_register_location_catalog_region = region.Subregion(
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800289 encoding.location_catalog.byte_offset,
290 encoding.location_catalog.num_bytes);
Calin Juravlec416d332015-04-23 16:01:43 +0100291 DexRegisterLocationCatalog dex_register_location_catalog(dex_register_location_catalog_region);
292 // Offset in `dex_register_location_catalog` where to store the next
293 // register location.
294 size_t location_catalog_offset = DexRegisterLocationCatalog::kFixedSize;
Vladimir Marko225b6462015-09-28 12:17:40 +0100295 for (DexRegisterLocation dex_register_location : location_catalog_entries_) {
Calin Juravlec416d332015-04-23 16:01:43 +0100296 dex_register_location_catalog.SetRegisterInfo(location_catalog_offset, dex_register_location);
297 location_catalog_offset += DexRegisterLocationCatalog::EntrySize(dex_register_location);
298 }
299 // Ensure we reached the end of the Dex registers location_catalog.
300 DCHECK_EQ(location_catalog_offset, dex_register_location_catalog_region.size());
301
Vladimir Markof6a35de2016-03-21 12:01:50 +0000302 ArenaBitVector empty_bitmask(allocator_, 0, /* expandable */ false, kArenaAllocStackMapStream);
Calin Juravlec416d332015-04-23 16:01:43 +0100303 uintptr_t next_dex_register_map_offset = 0;
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800304 uintptr_t next_inline_info_index = 0;
Vladimir Marko225b6462015-09-28 12:17:40 +0100305 for (size_t i = 0, e = stack_maps_.size(); i < e; ++i) {
David Srbecky09ed0982016-02-12 21:58:43 +0000306 StackMap stack_map = code_info.GetStackMapAt(i, encoding);
Vladimir Marko225b6462015-09-28 12:17:40 +0100307 StackMapEntry entry = stack_maps_[i];
Calin Juravlec416d332015-04-23 16:01:43 +0100308
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800309 stack_map.SetDexPc(encoding.stack_map.encoding, entry.dex_pc);
310 stack_map.SetNativePcCodeOffset(encoding.stack_map.encoding, entry.native_pc_code_offset);
311 stack_map.SetRegisterMaskIndex(encoding.stack_map.encoding, entry.register_mask_index);
312 stack_map.SetStackMaskIndex(encoding.stack_map.encoding, entry.stack_mask_index);
Calin Juravlec416d332015-04-23 16:01:43 +0100313
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000314 if (entry.num_dex_registers == 0 || (entry.live_dex_registers_mask->NumSetBits() == 0)) {
Calin Juravlec416d332015-04-23 16:01:43 +0100315 // No dex map available.
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800316 stack_map.SetDexRegisterMapOffset(encoding.stack_map.encoding, StackMap::kNoDexRegisterMap);
Calin Juravlec416d332015-04-23 16:01:43 +0100317 } else {
318 // Search for an entry with the same dex map.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100319 if (entry.same_dex_register_map_as_ != kNoSameDexMapFound) {
Calin Juravlec416d332015-04-23 16:01:43 +0100320 // If we have a hit reuse the offset.
David Brazdilf677ebf2015-05-29 16:29:43 +0100321 stack_map.SetDexRegisterMapOffset(
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800322 encoding.stack_map.encoding,
David Srbecky09ed0982016-02-12 21:58:43 +0000323 code_info.GetStackMapAt(entry.same_dex_register_map_as_, encoding)
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800324 .GetDexRegisterMapOffset(encoding.stack_map.encoding));
Calin Juravlec416d332015-04-23 16:01:43 +0100325 } else {
326 // New dex registers maps should be added to the stack map.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100327 MemoryRegion register_region = dex_register_locations_region.Subregion(
328 next_dex_register_map_offset,
Vladimir Marko225b6462015-09-28 12:17:40 +0100329 ComputeDexRegisterMapSize(entry.num_dex_registers, entry.live_dex_registers_mask));
Calin Juravlec416d332015-04-23 16:01:43 +0100330 next_dex_register_map_offset += register_region.size();
331 DexRegisterMap dex_register_map(register_region);
332 stack_map.SetDexRegisterMapOffset(
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800333 encoding.stack_map.encoding,
334 register_region.begin() - dex_register_locations_region.begin());
Calin Juravlec416d332015-04-23 16:01:43 +0100335
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100336 // Set the dex register location.
337 FillInDexRegisterMap(dex_register_map,
338 entry.num_dex_registers,
339 *entry.live_dex_registers_mask,
340 entry.dex_register_locations_start_index);
Calin Juravlec416d332015-04-23 16:01:43 +0100341 }
342 }
343
344 // Set the inlining info.
345 if (entry.inlining_depth != 0) {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800346 InlineInfo inline_info = code_info.GetInlineInfo(next_inline_info_index, encoding);
Calin Juravlec416d332015-04-23 16:01:43 +0100347
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800348 // Fill in the index.
349 stack_map.SetInlineInfoIndex(encoding.stack_map.encoding, next_inline_info_index);
350 DCHECK_EQ(next_inline_info_index, entry.inline_infos_start_index);
351 next_inline_info_index += entry.inlining_depth;
Calin Juravlec416d332015-04-23 16:01:43 +0100352
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800353 inline_info.SetDepth(encoding.inline_info.encoding, entry.inlining_depth);
Vladimir Marko225b6462015-09-28 12:17:40 +0100354 DCHECK_LE(entry.inline_infos_start_index + entry.inlining_depth, inline_infos_.size());
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800355
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100356 for (size_t depth = 0; depth < entry.inlining_depth; ++depth) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100357 InlineInfoEntry inline_entry = inline_infos_[depth + entry.inline_infos_start_index];
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000358 if (inline_entry.method != nullptr) {
359 inline_info.SetMethodIndexAtDepth(
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800360 encoding.inline_info.encoding,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000361 depth,
362 High32Bits(reinterpret_cast<uintptr_t>(inline_entry.method)));
363 inline_info.SetExtraDataAtDepth(
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800364 encoding.inline_info.encoding,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000365 depth,
366 Low32Bits(reinterpret_cast<uintptr_t>(inline_entry.method)));
367 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800368 inline_info.SetMethodIndexAtDepth(encoding.inline_info.encoding,
369 depth,
370 inline_entry.method_index);
371 inline_info.SetExtraDataAtDepth(encoding.inline_info.encoding, depth, 1);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000372 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800373 inline_info.SetDexPcAtDepth(encoding.inline_info.encoding, depth, inline_entry.dex_pc);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100374 if (inline_entry.num_dex_registers == 0) {
375 // No dex map available.
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800376 inline_info.SetDexRegisterMapOffsetAtDepth(encoding.inline_info.encoding,
David Srbecky61b28a12016-02-25 21:55:03 +0000377 depth,
378 StackMap::kNoDexRegisterMap);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100379 DCHECK(inline_entry.live_dex_registers_mask == nullptr);
380 } else {
381 MemoryRegion register_region = dex_register_locations_region.Subregion(
382 next_dex_register_map_offset,
383 ComputeDexRegisterMapSize(inline_entry.num_dex_registers,
Vladimir Marko225b6462015-09-28 12:17:40 +0100384 inline_entry.live_dex_registers_mask));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100385 next_dex_register_map_offset += register_region.size();
386 DexRegisterMap dex_register_map(register_region);
387 inline_info.SetDexRegisterMapOffsetAtDepth(
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800388 encoding.inline_info.encoding,
389 depth,
390 register_region.begin() - dex_register_locations_region.begin());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100391
392 FillInDexRegisterMap(dex_register_map,
393 inline_entry.num_dex_registers,
394 *inline_entry.live_dex_registers_mask,
395 inline_entry.dex_register_locations_start_index);
396 }
Calin Juravlec416d332015-04-23 16:01:43 +0100397 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800398 } else if (encoding.stack_map.encoding.GetInlineInfoEncoding().BitSize() > 0) {
399 stack_map.SetInlineInfoIndex(encoding.stack_map.encoding, StackMap::kNoInlineInfo);
Calin Juravlec416d332015-04-23 16:01:43 +0100400 }
401 }
David Srbecky1bbdfd72016-02-24 16:39:26 +0000402
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800403 // Write stack masks table.
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800404 const size_t stack_mask_bits = encoding.stack_mask.encoding.BitSize();
David Srbecky45aa5982016-03-18 02:15:09 +0000405 if (stack_mask_bits > 0) {
406 size_t stack_mask_bytes = RoundUp(stack_mask_bits, kBitsPerByte) / kBitsPerByte;
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800407 for (size_t i = 0; i < encoding.stack_mask.num_entries; ++i) {
David Srbecky45aa5982016-03-18 02:15:09 +0000408 MemoryRegion source(&stack_masks_[i * stack_mask_bytes], stack_mask_bytes);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800409 BitMemoryRegion stack_mask = code_info.GetStackMask(i, encoding);
410 for (size_t bit_index = 0; bit_index < stack_mask_bits; ++bit_index) {
David Srbecky45aa5982016-03-18 02:15:09 +0000411 stack_mask.StoreBit(bit_index, source.LoadBit(bit_index));
412 }
413 }
414 }
415
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800416 // Write register masks table.
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800417 for (size_t i = 0; i < encoding.register_mask.num_entries; ++i) {
418 BitMemoryRegion register_mask = code_info.GetRegisterMask(i, encoding);
419 register_mask.StoreBits(0, register_masks_[i], encoding.register_mask.encoding.BitSize());
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800420 }
421
David Srbecky1bbdfd72016-02-24 16:39:26 +0000422 // Verify all written data in debug build.
423 if (kIsDebugBuild) {
424 CheckCodeInfo(region);
425 }
Calin Juravlec416d332015-04-23 16:01:43 +0100426}
427
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100428void StackMapStream::FillInDexRegisterMap(DexRegisterMap dex_register_map,
429 uint32_t num_dex_registers,
430 const BitVector& live_dex_registers_mask,
431 uint32_t start_index_in_dex_register_locations) const {
432 dex_register_map.SetLiveBitMask(num_dex_registers, live_dex_registers_mask);
433 // Set the dex register location mapping data.
Vladimir Marko225b6462015-09-28 12:17:40 +0100434 size_t number_of_live_dex_registers = live_dex_registers_mask.NumSetBits();
435 DCHECK_LE(number_of_live_dex_registers, dex_register_locations_.size());
436 DCHECK_LE(start_index_in_dex_register_locations,
437 dex_register_locations_.size() - number_of_live_dex_registers);
438 for (size_t index_in_dex_register_locations = 0;
439 index_in_dex_register_locations != number_of_live_dex_registers;
440 ++index_in_dex_register_locations) {
441 size_t location_catalog_entry_index = dex_register_locations_[
442 start_index_in_dex_register_locations + index_in_dex_register_locations];
443 dex_register_map.SetLocationCatalogEntryIndex(
444 index_in_dex_register_locations,
445 location_catalog_entry_index,
446 num_dex_registers,
447 location_catalog_entries_.size());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100448 }
449}
450
Calin Juravle4f46ac52015-04-23 18:47:21 +0100451size_t StackMapStream::FindEntryWithTheSameDexMap() {
Vladimir Marko225b6462015-09-28 12:17:40 +0100452 size_t current_entry_index = stack_maps_.size();
Calin Juravle4f46ac52015-04-23 18:47:21 +0100453 auto entries_it = dex_map_hash_to_stack_map_indices_.find(current_entry_.dex_register_map_hash);
Calin Juravlec416d332015-04-23 16:01:43 +0100454 if (entries_it == dex_map_hash_to_stack_map_indices_.end()) {
455 // We don't have a perfect hash functions so we need a list to collect all stack maps
456 // which might have the same dex register map.
Vladimir Marko225b6462015-09-28 12:17:40 +0100457 ArenaVector<uint32_t> stack_map_indices(allocator_->Adapter(kArenaAllocStackMapStream));
458 stack_map_indices.push_back(current_entry_index);
459 dex_map_hash_to_stack_map_indices_.Put(current_entry_.dex_register_map_hash,
460 std::move(stack_map_indices));
Calin Juravlec416d332015-04-23 16:01:43 +0100461 return kNoSameDexMapFound;
462 }
463
Calin Juravle4f46ac52015-04-23 18:47:21 +0100464 // We might have collisions, so we need to check whether or not we really have a match.
Vladimir Marko225b6462015-09-28 12:17:40 +0100465 for (uint32_t test_entry_index : entries_it->second) {
466 if (HaveTheSameDexMaps(GetStackMap(test_entry_index), current_entry_)) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100467 return test_entry_index;
Calin Juravlec416d332015-04-23 16:01:43 +0100468 }
469 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100470 entries_it->second.push_back(current_entry_index);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100471 return kNoSameDexMapFound;
Calin Juravlec416d332015-04-23 16:01:43 +0100472}
473
474bool StackMapStream::HaveTheSameDexMaps(const StackMapEntry& a, const StackMapEntry& b) const {
475 if (a.live_dex_registers_mask == nullptr && b.live_dex_registers_mask == nullptr) {
476 return true;
477 }
478 if (a.live_dex_registers_mask == nullptr || b.live_dex_registers_mask == nullptr) {
479 return false;
480 }
481 if (a.num_dex_registers != b.num_dex_registers) {
482 return false;
483 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100484 if (a.num_dex_registers != 0u) {
485 DCHECK(a.live_dex_registers_mask != nullptr);
486 DCHECK(b.live_dex_registers_mask != nullptr);
487 if (!a.live_dex_registers_mask->Equal(b.live_dex_registers_mask)) {
Calin Juravlec416d332015-04-23 16:01:43 +0100488 return false;
489 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100490 size_t number_of_live_dex_registers = a.live_dex_registers_mask->NumSetBits();
491 DCHECK_LE(number_of_live_dex_registers, dex_register_locations_.size());
492 DCHECK_LE(a.dex_register_locations_start_index,
493 dex_register_locations_.size() - number_of_live_dex_registers);
494 DCHECK_LE(b.dex_register_locations_start_index,
495 dex_register_locations_.size() - number_of_live_dex_registers);
496 auto a_begin = dex_register_locations_.begin() + a.dex_register_locations_start_index;
497 auto b_begin = dex_register_locations_.begin() + b.dex_register_locations_start_index;
498 if (!std::equal(a_begin, a_begin + number_of_live_dex_registers, b_begin)) {
499 return false;
Calin Juravlec416d332015-04-23 16:01:43 +0100500 }
501 }
502 return true;
503}
504
David Srbecky1bbdfd72016-02-24 16:39:26 +0000505// Helper for CheckCodeInfo - check that register map has the expected content.
506void StackMapStream::CheckDexRegisterMap(const CodeInfo& code_info,
507 const DexRegisterMap& dex_register_map,
508 size_t num_dex_registers,
509 BitVector* live_dex_registers_mask,
510 size_t dex_register_locations_index) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000511 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Srbecky1bbdfd72016-02-24 16:39:26 +0000512 for (size_t reg = 0; reg < num_dex_registers; reg++) {
513 // Find the location we tried to encode.
514 DexRegisterLocation expected = DexRegisterLocation::None();
515 if (live_dex_registers_mask->IsBitSet(reg)) {
516 size_t catalog_index = dex_register_locations_[dex_register_locations_index++];
517 expected = location_catalog_entries_[catalog_index];
518 }
519 // Compare to the seen location.
520 if (expected.GetKind() == DexRegisterLocation::Kind::kNone) {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800521 DCHECK(!dex_register_map.IsValid() || !dex_register_map.IsDexRegisterLive(reg))
522 << dex_register_map.IsValid() << " " << dex_register_map.IsDexRegisterLive(reg);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000523 } else {
524 DCHECK(dex_register_map.IsDexRegisterLive(reg));
525 DexRegisterLocation seen = dex_register_map.GetDexRegisterLocation(
526 reg, num_dex_registers, code_info, encoding);
527 DCHECK_EQ(expected.GetKind(), seen.GetKind());
528 DCHECK_EQ(expected.GetValue(), seen.GetValue());
529 }
530 }
531 if (num_dex_registers == 0) {
532 DCHECK(!dex_register_map.IsValid());
533 }
534}
535
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800536size_t StackMapStream::PrepareRegisterMasks() {
537 register_masks_.resize(stack_maps_.size(), 0u);
538 std::unordered_map<uint32_t, size_t> dedupe;
539 for (StackMapEntry& stack_map : stack_maps_) {
540 const size_t index = dedupe.size();
541 stack_map.register_mask_index = dedupe.emplace(stack_map.register_mask, index).first->second;
542 register_masks_[index] = stack_map.register_mask;
543 }
544 return dedupe.size();
545}
546
David Srbecky45aa5982016-03-18 02:15:09 +0000547size_t StackMapStream::PrepareStackMasks(size_t entry_size_in_bits) {
548 // Preallocate memory since we do not want it to move (the dedup map will point into it).
549 const size_t byte_entry_size = RoundUp(entry_size_in_bits, kBitsPerByte) / kBitsPerByte;
550 stack_masks_.resize(byte_entry_size * stack_maps_.size(), 0u);
551 // For deduplicating we store the stack masks as byte packed for simplicity. We can bit pack later
552 // when copying out from stack_masks_.
553 std::unordered_map<MemoryRegion,
554 size_t,
555 FNVHash<MemoryRegion>,
556 MemoryRegion::ContentEquals> dedup(stack_maps_.size());
557 for (StackMapEntry& stack_map : stack_maps_) {
558 size_t index = dedup.size();
559 MemoryRegion stack_mask(stack_masks_.data() + index * byte_entry_size, byte_entry_size);
560 for (size_t i = 0; i < entry_size_in_bits; i++) {
561 stack_mask.StoreBit(i, stack_map.sp_mask != nullptr && stack_map.sp_mask->IsBitSet(i));
562 }
563 stack_map.stack_mask_index = dedup.emplace(stack_mask, index).first->second;
564 }
565 return dedup.size();
566}
567
David Srbecky1bbdfd72016-02-24 16:39:26 +0000568// Check that all StackMapStream inputs are correctly encoded by trying to read them back.
569void StackMapStream::CheckCodeInfo(MemoryRegion region) const {
570 CodeInfo code_info(region);
David Srbecky09ed0982016-02-12 21:58:43 +0000571 CodeInfoEncoding encoding = code_info.ExtractEncoding();
572 DCHECK_EQ(code_info.GetNumberOfStackMaps(encoding), stack_maps_.size());
David Srbecky1bbdfd72016-02-24 16:39:26 +0000573 for (size_t s = 0; s < stack_maps_.size(); ++s) {
574 const StackMap stack_map = code_info.GetStackMapAt(s, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800575 const StackMapEncoding& stack_map_encoding = encoding.stack_map.encoding;
David Srbecky1bbdfd72016-02-24 16:39:26 +0000576 StackMapEntry entry = stack_maps_[s];
577
578 // Check main stack map fields.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800579 DCHECK_EQ(stack_map.GetNativePcOffset(stack_map_encoding, instruction_set_),
580 entry.native_pc_code_offset.Uint32Value(instruction_set_));
David Srbecky09ed0982016-02-12 21:58:43 +0000581 DCHECK_EQ(stack_map.GetDexPc(stack_map_encoding), entry.dex_pc);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800582 DCHECK_EQ(stack_map.GetRegisterMaskIndex(stack_map_encoding), entry.register_mask_index);
583 DCHECK_EQ(code_info.GetRegisterMaskOf(encoding, stack_map), entry.register_mask);
David Srbecky45aa5982016-03-18 02:15:09 +0000584 const size_t num_stack_mask_bits = code_info.GetNumberOfStackMaskBits(encoding);
585 DCHECK_EQ(stack_map.GetStackMaskIndex(stack_map_encoding), entry.stack_mask_index);
586 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(encoding, stack_map);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000587 if (entry.sp_mask != nullptr) {
David Srbecky45aa5982016-03-18 02:15:09 +0000588 DCHECK_GE(stack_mask.size_in_bits(), entry.sp_mask->GetNumberOfBits());
David Srbecky09ed0982016-02-12 21:58:43 +0000589 for (size_t b = 0; b < num_stack_mask_bits; b++) {
David Srbecky45aa5982016-03-18 02:15:09 +0000590 DCHECK_EQ(stack_mask.LoadBit(b), entry.sp_mask->IsBitSet(b));
David Srbecky1bbdfd72016-02-24 16:39:26 +0000591 }
592 } else {
David Srbecky09ed0982016-02-12 21:58:43 +0000593 for (size_t b = 0; b < num_stack_mask_bits; b++) {
David Srbecky45aa5982016-03-18 02:15:09 +0000594 DCHECK_EQ(stack_mask.LoadBit(b), 0u);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000595 }
596 }
597
598 CheckDexRegisterMap(code_info,
599 code_info.GetDexRegisterMapOf(
600 stack_map, encoding, entry.num_dex_registers),
601 entry.num_dex_registers,
602 entry.live_dex_registers_mask,
603 entry.dex_register_locations_start_index);
604
605 // Check inline info.
David Srbecky09ed0982016-02-12 21:58:43 +0000606 DCHECK_EQ(stack_map.HasInlineInfo(stack_map_encoding), (entry.inlining_depth != 0));
David Srbecky1bbdfd72016-02-24 16:39:26 +0000607 if (entry.inlining_depth != 0) {
608 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800609 DCHECK_EQ(inline_info.GetDepth(encoding.inline_info.encoding), entry.inlining_depth);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000610 for (size_t d = 0; d < entry.inlining_depth; ++d) {
611 size_t inline_info_index = entry.inline_infos_start_index + d;
612 DCHECK_LT(inline_info_index, inline_infos_.size());
613 InlineInfoEntry inline_entry = inline_infos_[inline_info_index];
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800614 DCHECK_EQ(inline_info.GetDexPcAtDepth(encoding.inline_info.encoding, d),
David Srbecky61b28a12016-02-25 21:55:03 +0000615 inline_entry.dex_pc);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800616 if (inline_info.EncodesArtMethodAtDepth(encoding.inline_info.encoding, d)) {
617 DCHECK_EQ(inline_info.GetArtMethodAtDepth(encoding.inline_info.encoding, d),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000618 inline_entry.method);
619 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800620 DCHECK_EQ(inline_info.GetMethodIndexAtDepth(encoding.inline_info.encoding, d),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000621 inline_entry.method_index);
622 }
David Srbecky1bbdfd72016-02-24 16:39:26 +0000623
624 CheckDexRegisterMap(code_info,
625 code_info.GetDexRegisterMapAtDepth(
626 d, inline_info, encoding, inline_entry.num_dex_registers),
627 inline_entry.num_dex_registers,
628 inline_entry.live_dex_registers_mask,
629 inline_entry.dex_register_locations_start_index);
630 }
631 }
632 }
633}
634
Calin Juravlec416d332015-04-23 16:01:43 +0100635} // namespace art