blob: 6087e36507f173e7ab16e636a48944aed4e1a079 [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
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000019#include "art_method.h"
20#include "runtime.h"
21#include "scoped_thread_state_change-inl.h"
22
Calin Juravlec416d332015-04-23 16:01:43 +010023namespace art {
24
Calin Juravle4f46ac52015-04-23 18:47:21 +010025void StackMapStream::BeginStackMapEntry(uint32_t dex_pc,
26 uint32_t native_pc_offset,
27 uint32_t register_mask,
28 BitVector* sp_mask,
29 uint32_t num_dex_registers,
30 uint8_t inlining_depth) {
31 DCHECK_EQ(0u, current_entry_.dex_pc) << "EndStackMapEntry not called after BeginStackMapEntry";
Calin Juravleb5c46932015-10-06 17:09:49 +010032 DCHECK_NE(dex_pc, static_cast<uint32_t>(-1)) << "invalid dex_pc";
Calin Juravle4f46ac52015-04-23 18:47:21 +010033 current_entry_.dex_pc = dex_pc;
34 current_entry_.native_pc_offset = native_pc_offset;
35 current_entry_.register_mask = register_mask;
36 current_entry_.sp_mask = sp_mask;
37 current_entry_.num_dex_registers = num_dex_registers;
38 current_entry_.inlining_depth = inlining_depth;
Vladimir Marko225b6462015-09-28 12:17:40 +010039 current_entry_.dex_register_locations_start_index = dex_register_locations_.size();
40 current_entry_.inline_infos_start_index = inline_infos_.size();
Calin Juravle4f46ac52015-04-23 18:47:21 +010041 current_entry_.dex_register_map_hash = 0;
42 current_entry_.same_dex_register_map_as_ = kNoSameDexMapFound;
Calin Juravlec416d332015-04-23 16:01:43 +010043 if (num_dex_registers != 0) {
Calin Juravle4f46ac52015-04-23 18:47:21 +010044 current_entry_.live_dex_registers_mask =
Vladimir Markof6a35de2016-03-21 12:01:50 +000045 ArenaBitVector::Create(allocator_, num_dex_registers, true, kArenaAllocStackMapStream);
Calin Juravlec416d332015-04-23 16:01:43 +010046 } else {
Calin Juravle4f46ac52015-04-23 18:47:21 +010047 current_entry_.live_dex_registers_mask = nullptr;
Calin Juravlec416d332015-04-23 16:01:43 +010048 }
Calin Juravlec416d332015-04-23 16:01:43 +010049
50 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() {
63 current_entry_.same_dex_register_map_as_ = FindEntryWithTheSameDexMap();
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 }
89
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010090 if (in_inline_frame_) {
91 // TODO: Support sharing DexRegisterMap across InlineInfo.
92 DCHECK_LT(current_dex_register_, current_inline_info_.num_dex_registers);
93 current_inline_info_.live_dex_registers_mask->SetBit(current_dex_register_);
94 } else {
95 DCHECK_LT(current_dex_register_, current_entry_.num_dex_registers);
96 current_entry_.live_dex_registers_mask->SetBit(current_dex_register_);
97 current_entry_.dex_register_map_hash += (1 <<
98 (current_dex_register_ % (sizeof(current_entry_.dex_register_map_hash) * kBitsPerByte)));
99 current_entry_.dex_register_map_hash += static_cast<uint32_t>(value);
100 current_entry_.dex_register_map_hash += static_cast<uint32_t>(kind);
101 }
Calin Juravlec416d332015-04-23 16:01:43 +0100102 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100103 current_dex_register_++;
Calin Juravlec416d332015-04-23 16:01:43 +0100104}
105
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000106static bool EncodeArtMethodInInlineInfo(ArtMethod* method ATTRIBUTE_UNUSED) {
107 // Note: the runtime is null only for unit testing.
108 return Runtime::Current() == nullptr || !Runtime::Current()->IsAotCompiler();
109}
110
111void 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
Vladimir Markobd8c7252015-06-12 10:06:32 +0100147uint32_t StackMapStream::ComputeMaxNativePcOffset() const {
148 uint32_t max_native_pc_offset = 0u;
Vladimir Marko225b6462015-09-28 12:17:40 +0100149 for (const StackMapEntry& entry : stack_maps_) {
150 max_native_pc_offset = std::max(max_native_pc_offset, entry.native_pc_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() {
156 int stack_mask_number_of_bits = stack_mask_max_ + 1; // Need room for max element too.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100157 dex_register_maps_size_ = ComputeDexRegisterMapsSize();
David Srbecky61b28a12016-02-25 21:55:03 +0000158 ComputeInlineInfoEncoding(); // needs dex_register_maps_size_.
159 inline_info_size_ = inline_infos_.size() * inline_info_encoding_.GetEntrySize();
Vladimir Markobd8c7252015-06-12 10:06:32 +0100160 uint32_t max_native_pc_offset = ComputeMaxNativePcOffset();
David Srbecky09ed0982016-02-12 21:58:43 +0000161 size_t stack_map_size = stack_map_encoding_.SetFromSizes(max_native_pc_offset,
162 dex_pc_max_,
163 dex_register_maps_size_,
164 inline_info_size_,
165 register_mask_max_,
166 stack_mask_number_of_bits);
167 stack_maps_size_ = stack_maps_.size() * stack_map_size;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100168 dex_register_location_catalog_size_ = ComputeDexRegisterLocationCatalogSize();
169
David Srbecky09ed0982016-02-12 21:58:43 +0000170 size_t non_header_size =
171 stack_maps_size_ +
172 dex_register_location_catalog_size_ +
173 dex_register_maps_size_ +
174 inline_info_size_;
Calin Juravlec416d332015-04-23 16:01:43 +0100175
David Srbecky09ed0982016-02-12 21:58:43 +0000176 // Prepare the CodeInfo variable-sized encoding.
177 CodeInfoEncoding code_info_encoding;
178 code_info_encoding.non_header_size = non_header_size;
David Srbecky09ed0982016-02-12 21:58:43 +0000179 code_info_encoding.number_of_stack_maps = stack_maps_.size();
180 code_info_encoding.stack_map_size_in_bytes = stack_map_size;
David Srbecky61b28a12016-02-25 21:55:03 +0000181 code_info_encoding.stack_map_encoding = stack_map_encoding_;
182 code_info_encoding.inline_info_encoding = inline_info_encoding_;
David Srbecky09ed0982016-02-12 21:58:43 +0000183 code_info_encoding.number_of_location_catalog_entries = location_catalog_entries_.size();
184 code_info_encoding.Compress(&code_info_encoding_);
185
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100186 // TODO: Move the catalog at the end. It is currently too expensive at runtime
187 // to compute its size (note that we do not encode that size in the CodeInfo).
David Srbecky09ed0982016-02-12 21:58:43 +0000188 dex_register_location_catalog_start_ = code_info_encoding_.size() + stack_maps_size_;
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100189 dex_register_maps_start_ =
190 dex_register_location_catalog_start_ + dex_register_location_catalog_size_;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100191 inline_infos_start_ = dex_register_maps_start_ + dex_register_maps_size_;
Calin Juravlec416d332015-04-23 16:01:43 +0100192
David Srbecky09ed0982016-02-12 21:58:43 +0000193 needed_size_ = code_info_encoding_.size() + non_header_size;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100194 return needed_size_;
Calin Juravlec416d332015-04-23 16:01:43 +0100195}
196
197size_t StackMapStream::ComputeDexRegisterLocationCatalogSize() const {
198 size_t size = DexRegisterLocationCatalog::kFixedSize;
Vladimir Marko225b6462015-09-28 12:17:40 +0100199 for (const DexRegisterLocation& dex_register_location : location_catalog_entries_) {
Calin Juravlec416d332015-04-23 16:01:43 +0100200 size += DexRegisterLocationCatalog::EntrySize(dex_register_location);
201 }
202 return size;
203}
204
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100205size_t StackMapStream::ComputeDexRegisterMapSize(uint32_t num_dex_registers,
Vladimir Marko225b6462015-09-28 12:17:40 +0100206 const BitVector* live_dex_registers_mask) const {
207 // For num_dex_registers == 0u live_dex_registers_mask may be null.
208 if (num_dex_registers == 0u) {
209 return 0u; // No register map will be emitted.
210 }
211 DCHECK(live_dex_registers_mask != nullptr);
212
Calin Juravlec416d332015-04-23 16:01:43 +0100213 // Size of the map in bytes.
214 size_t size = DexRegisterMap::kFixedSize;
215 // Add the live bit mask for the Dex register liveness.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100216 size += DexRegisterMap::GetLiveBitMaskSize(num_dex_registers);
Calin Juravlec416d332015-04-23 16:01:43 +0100217 // Compute the size of the set of live Dex register entries.
Vladimir Marko225b6462015-09-28 12:17:40 +0100218 size_t number_of_live_dex_registers = live_dex_registers_mask->NumSetBits();
Calin Juravlec416d332015-04-23 16:01:43 +0100219 size_t map_entries_size_in_bits =
Vladimir Marko225b6462015-09-28 12:17:40 +0100220 DexRegisterMap::SingleEntrySizeInBits(location_catalog_entries_.size())
Calin Juravlec416d332015-04-23 16:01:43 +0100221 * number_of_live_dex_registers;
222 size_t map_entries_size_in_bytes =
223 RoundUp(map_entries_size_in_bits, kBitsPerByte) / kBitsPerByte;
224 size += map_entries_size_in_bytes;
225 return size;
226}
227
Calin Juravle4f46ac52015-04-23 18:47:21 +0100228size_t StackMapStream::ComputeDexRegisterMapsSize() const {
Calin Juravlec416d332015-04-23 16:01:43 +0100229 size_t size = 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100230 size_t inline_info_index = 0;
Vladimir Marko225b6462015-09-28 12:17:40 +0100231 for (const StackMapEntry& entry : stack_maps_) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100232 if (entry.same_dex_register_map_as_ == kNoSameDexMapFound) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100233 size += ComputeDexRegisterMapSize(entry.num_dex_registers, entry.live_dex_registers_mask);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100234 } else {
Calin Juravlec416d332015-04-23 16:01:43 +0100235 // Entries with the same dex map will have the same offset.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100236 }
237 for (size_t j = 0; j < entry.inlining_depth; ++j) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100238 InlineInfoEntry inline_entry = inline_infos_[inline_info_index++];
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100239 size += ComputeDexRegisterMapSize(inline_entry.num_dex_registers,
Vladimir Marko225b6462015-09-28 12:17:40 +0100240 inline_entry.live_dex_registers_mask);
Calin Juravlec416d332015-04-23 16:01:43 +0100241 }
242 }
243 return size;
244}
245
David Srbecky61b28a12016-02-25 21:55:03 +0000246void StackMapStream::ComputeInlineInfoEncoding() {
247 uint32_t method_index_max = 0;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100248 uint32_t dex_pc_max = DexFile::kDexNoIndex;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000249 uint32_t extra_data_max = 0;
David Srbecky61b28a12016-02-25 21:55:03 +0000250
251 uint32_t inline_info_index = 0;
252 for (const StackMapEntry& entry : stack_maps_) {
253 for (size_t j = 0; j < entry.inlining_depth; ++j) {
254 InlineInfoEntry inline_entry = inline_infos_[inline_info_index++];
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000255 if (inline_entry.method == nullptr) {
256 method_index_max = std::max(method_index_max, inline_entry.method_index);
257 extra_data_max = std::max(extra_data_max, 1u);
258 } else {
259 method_index_max = std::max(
260 method_index_max, High32Bits(reinterpret_cast<uintptr_t>(inline_entry.method)));
261 extra_data_max = std::max(
262 extra_data_max, Low32Bits(reinterpret_cast<uintptr_t>(inline_entry.method)));
263 }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100264 if (inline_entry.dex_pc != DexFile::kDexNoIndex &&
265 (dex_pc_max == DexFile::kDexNoIndex || dex_pc_max < inline_entry.dex_pc)) {
266 dex_pc_max = inline_entry.dex_pc;
267 }
David Srbecky61b28a12016-02-25 21:55:03 +0000268 }
269 }
270 DCHECK_EQ(inline_info_index, inline_infos_.size());
271
272 inline_info_encoding_.SetFromSizes(method_index_max,
273 dex_pc_max,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000274 extra_data_max,
David Srbecky61b28a12016-02-25 21:55:03 +0000275 dex_register_maps_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100276}
277
Calin Juravlec416d332015-04-23 16:01:43 +0100278void StackMapStream::FillIn(MemoryRegion region) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100279 DCHECK_EQ(0u, current_entry_.dex_pc) << "EndStackMapEntry not called after BeginStackMapEntry";
280 DCHECK_NE(0u, needed_size_) << "PrepareForFillIn not called before FillIn";
281
Calin Juravle4f46ac52015-04-23 18:47:21 +0100282 DCHECK_EQ(region.size(), needed_size_);
David Srbecky09ed0982016-02-12 21:58:43 +0000283
284 // Note that the memory region does not have to be zeroed when we JIT code
285 // because we do not use the arena allocator there.
286
287 // Write the CodeInfo header.
288 region.CopyFrom(0, MemoryRegion(code_info_encoding_.data(), code_info_encoding_.size()));
Calin Juravlec416d332015-04-23 16:01:43 +0100289
Calin Juravlec416d332015-04-23 16:01:43 +0100290 MemoryRegion dex_register_locations_region = region.Subregion(
Calin Juravle4f46ac52015-04-23 18:47:21 +0100291 dex_register_maps_start_, dex_register_maps_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100292
293 MemoryRegion inline_infos_region = region.Subregion(
Calin Juravle4f46ac52015-04-23 18:47:21 +0100294 inline_infos_start_, inline_info_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100295
David Srbecky09ed0982016-02-12 21:58:43 +0000296 CodeInfo code_info(region);
297 CodeInfoEncoding encoding = code_info.ExtractEncoding();
298 DCHECK_EQ(code_info.GetStackMapsSize(encoding), stack_maps_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100299
300 // Set the Dex register location catalog.
Calin Juravlec416d332015-04-23 16:01:43 +0100301 MemoryRegion dex_register_location_catalog_region = region.Subregion(
Calin Juravle4f46ac52015-04-23 18:47:21 +0100302 dex_register_location_catalog_start_, dex_register_location_catalog_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100303 DexRegisterLocationCatalog dex_register_location_catalog(dex_register_location_catalog_region);
304 // Offset in `dex_register_location_catalog` where to store the next
305 // register location.
306 size_t location_catalog_offset = DexRegisterLocationCatalog::kFixedSize;
Vladimir Marko225b6462015-09-28 12:17:40 +0100307 for (DexRegisterLocation dex_register_location : location_catalog_entries_) {
Calin Juravlec416d332015-04-23 16:01:43 +0100308 dex_register_location_catalog.SetRegisterInfo(location_catalog_offset, dex_register_location);
309 location_catalog_offset += DexRegisterLocationCatalog::EntrySize(dex_register_location);
310 }
311 // Ensure we reached the end of the Dex registers location_catalog.
312 DCHECK_EQ(location_catalog_offset, dex_register_location_catalog_region.size());
313
Vladimir Markof6a35de2016-03-21 12:01:50 +0000314 ArenaBitVector empty_bitmask(allocator_, 0, /* expandable */ false, kArenaAllocStackMapStream);
Calin Juravlec416d332015-04-23 16:01:43 +0100315 uintptr_t next_dex_register_map_offset = 0;
316 uintptr_t next_inline_info_offset = 0;
Vladimir Marko225b6462015-09-28 12:17:40 +0100317 for (size_t i = 0, e = stack_maps_.size(); i < e; ++i) {
David Srbecky09ed0982016-02-12 21:58:43 +0000318 StackMap stack_map = code_info.GetStackMapAt(i, encoding);
Vladimir Marko225b6462015-09-28 12:17:40 +0100319 StackMapEntry entry = stack_maps_[i];
Calin Juravlec416d332015-04-23 16:01:43 +0100320
David Brazdilf677ebf2015-05-29 16:29:43 +0100321 stack_map.SetDexPc(stack_map_encoding_, entry.dex_pc);
322 stack_map.SetNativePcOffset(stack_map_encoding_, entry.native_pc_offset);
323 stack_map.SetRegisterMask(stack_map_encoding_, entry.register_mask);
David Srbecky09ed0982016-02-12 21:58:43 +0000324 size_t number_of_stack_mask_bits = stack_map.GetNumberOfStackMaskBits(stack_map_encoding_);
Calin Juravlec416d332015-04-23 16:01:43 +0100325 if (entry.sp_mask != nullptr) {
David Srbecky09ed0982016-02-12 21:58:43 +0000326 for (size_t bit = 0; bit < number_of_stack_mask_bits; bit++) {
327 stack_map.SetStackMaskBit(stack_map_encoding_, bit, entry.sp_mask->IsBitSet(bit));
328 }
David Srbecky1bbdfd72016-02-24 16:39:26 +0000329 } else {
330 // The MemoryRegion does not have to be zeroed, so make sure we clear the bits.
David Srbecky09ed0982016-02-12 21:58:43 +0000331 for (size_t bit = 0; bit < number_of_stack_mask_bits; bit++) {
332 stack_map.SetStackMaskBit(stack_map_encoding_, bit, false);
333 }
Calin Juravlec416d332015-04-23 16:01:43 +0100334 }
335
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000336 if (entry.num_dex_registers == 0 || (entry.live_dex_registers_mask->NumSetBits() == 0)) {
Calin Juravlec416d332015-04-23 16:01:43 +0100337 // No dex map available.
David Brazdilf677ebf2015-05-29 16:29:43 +0100338 stack_map.SetDexRegisterMapOffset(stack_map_encoding_, StackMap::kNoDexRegisterMap);
Calin Juravlec416d332015-04-23 16:01:43 +0100339 } else {
340 // Search for an entry with the same dex map.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100341 if (entry.same_dex_register_map_as_ != kNoSameDexMapFound) {
Calin Juravlec416d332015-04-23 16:01:43 +0100342 // If we have a hit reuse the offset.
David Brazdilf677ebf2015-05-29 16:29:43 +0100343 stack_map.SetDexRegisterMapOffset(
344 stack_map_encoding_,
David Srbecky09ed0982016-02-12 21:58:43 +0000345 code_info.GetStackMapAt(entry.same_dex_register_map_as_, encoding)
David Brazdil77a48ae2015-09-15 12:34:04 +0000346 .GetDexRegisterMapOffset(stack_map_encoding_));
Calin Juravlec416d332015-04-23 16:01:43 +0100347 } else {
348 // New dex registers maps should be added to the stack map.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100349 MemoryRegion register_region = dex_register_locations_region.Subregion(
350 next_dex_register_map_offset,
Vladimir Marko225b6462015-09-28 12:17:40 +0100351 ComputeDexRegisterMapSize(entry.num_dex_registers, entry.live_dex_registers_mask));
Calin Juravlec416d332015-04-23 16:01:43 +0100352 next_dex_register_map_offset += register_region.size();
353 DexRegisterMap dex_register_map(register_region);
354 stack_map.SetDexRegisterMapOffset(
David Brazdilf677ebf2015-05-29 16:29:43 +0100355 stack_map_encoding_, register_region.start() - dex_register_locations_region.start());
Calin Juravlec416d332015-04-23 16:01:43 +0100356
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100357 // Set the dex register location.
358 FillInDexRegisterMap(dex_register_map,
359 entry.num_dex_registers,
360 *entry.live_dex_registers_mask,
361 entry.dex_register_locations_start_index);
Calin Juravlec416d332015-04-23 16:01:43 +0100362 }
363 }
364
365 // Set the inlining info.
366 if (entry.inlining_depth != 0) {
367 MemoryRegion inline_region = inline_infos_region.Subregion(
368 next_inline_info_offset,
David Srbecky61b28a12016-02-25 21:55:03 +0000369 entry.inlining_depth * inline_info_encoding_.GetEntrySize());
Calin Juravlec416d332015-04-23 16:01:43 +0100370 next_inline_info_offset += inline_region.size();
371 InlineInfo inline_info(inline_region);
372
373 // Currently relative to the dex register map.
374 stack_map.SetInlineDescriptorOffset(
David Brazdilf677ebf2015-05-29 16:29:43 +0100375 stack_map_encoding_, inline_region.start() - dex_register_locations_region.start());
Calin Juravlec416d332015-04-23 16:01:43 +0100376
David Srbecky61b28a12016-02-25 21:55:03 +0000377 inline_info.SetDepth(inline_info_encoding_, entry.inlining_depth);
Vladimir Marko225b6462015-09-28 12:17:40 +0100378 DCHECK_LE(entry.inline_infos_start_index + entry.inlining_depth, inline_infos_.size());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100379 for (size_t depth = 0; depth < entry.inlining_depth; ++depth) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100380 InlineInfoEntry inline_entry = inline_infos_[depth + entry.inline_infos_start_index];
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000381 if (inline_entry.method != nullptr) {
382 inline_info.SetMethodIndexAtDepth(
383 inline_info_encoding_,
384 depth,
385 High32Bits(reinterpret_cast<uintptr_t>(inline_entry.method)));
386 inline_info.SetExtraDataAtDepth(
387 inline_info_encoding_,
388 depth,
389 Low32Bits(reinterpret_cast<uintptr_t>(inline_entry.method)));
390 } else {
391 inline_info.SetMethodIndexAtDepth(inline_info_encoding_, depth, inline_entry.method_index);
392 inline_info.SetExtraDataAtDepth(inline_info_encoding_, depth, 1);
393 }
David Srbecky61b28a12016-02-25 21:55:03 +0000394 inline_info.SetDexPcAtDepth(inline_info_encoding_, depth, inline_entry.dex_pc);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100395 if (inline_entry.num_dex_registers == 0) {
396 // No dex map available.
David Srbecky61b28a12016-02-25 21:55:03 +0000397 inline_info.SetDexRegisterMapOffsetAtDepth(inline_info_encoding_,
398 depth,
399 StackMap::kNoDexRegisterMap);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100400 DCHECK(inline_entry.live_dex_registers_mask == nullptr);
401 } else {
402 MemoryRegion register_region = dex_register_locations_region.Subregion(
403 next_dex_register_map_offset,
404 ComputeDexRegisterMapSize(inline_entry.num_dex_registers,
Vladimir Marko225b6462015-09-28 12:17:40 +0100405 inline_entry.live_dex_registers_mask));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100406 next_dex_register_map_offset += register_region.size();
407 DexRegisterMap dex_register_map(register_region);
408 inline_info.SetDexRegisterMapOffsetAtDepth(
David Srbecky61b28a12016-02-25 21:55:03 +0000409 inline_info_encoding_,
410 depth, register_region.start() - dex_register_locations_region.start());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100411
412 FillInDexRegisterMap(dex_register_map,
413 inline_entry.num_dex_registers,
414 *inline_entry.live_dex_registers_mask,
415 inline_entry.dex_register_locations_start_index);
416 }
Calin Juravlec416d332015-04-23 16:01:43 +0100417 }
418 } else {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100419 if (inline_info_size_ != 0) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100420 stack_map.SetInlineDescriptorOffset(stack_map_encoding_, StackMap::kNoInlineInfo);
Calin Juravlec416d332015-04-23 16:01:43 +0100421 }
422 }
423 }
David Srbecky1bbdfd72016-02-24 16:39:26 +0000424
425 // Verify all written data in debug build.
426 if (kIsDebugBuild) {
427 CheckCodeInfo(region);
428 }
Calin Juravlec416d332015-04-23 16:01:43 +0100429}
430
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100431void StackMapStream::FillInDexRegisterMap(DexRegisterMap dex_register_map,
432 uint32_t num_dex_registers,
433 const BitVector& live_dex_registers_mask,
434 uint32_t start_index_in_dex_register_locations) const {
435 dex_register_map.SetLiveBitMask(num_dex_registers, live_dex_registers_mask);
436 // Set the dex register location mapping data.
Vladimir Marko225b6462015-09-28 12:17:40 +0100437 size_t number_of_live_dex_registers = live_dex_registers_mask.NumSetBits();
438 DCHECK_LE(number_of_live_dex_registers, dex_register_locations_.size());
439 DCHECK_LE(start_index_in_dex_register_locations,
440 dex_register_locations_.size() - number_of_live_dex_registers);
441 for (size_t index_in_dex_register_locations = 0;
442 index_in_dex_register_locations != number_of_live_dex_registers;
443 ++index_in_dex_register_locations) {
444 size_t location_catalog_entry_index = dex_register_locations_[
445 start_index_in_dex_register_locations + index_in_dex_register_locations];
446 dex_register_map.SetLocationCatalogEntryIndex(
447 index_in_dex_register_locations,
448 location_catalog_entry_index,
449 num_dex_registers,
450 location_catalog_entries_.size());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100451 }
452}
453
Calin Juravle4f46ac52015-04-23 18:47:21 +0100454size_t StackMapStream::FindEntryWithTheSameDexMap() {
Vladimir Marko225b6462015-09-28 12:17:40 +0100455 size_t current_entry_index = stack_maps_.size();
Calin Juravle4f46ac52015-04-23 18:47:21 +0100456 auto entries_it = dex_map_hash_to_stack_map_indices_.find(current_entry_.dex_register_map_hash);
Calin Juravlec416d332015-04-23 16:01:43 +0100457 if (entries_it == dex_map_hash_to_stack_map_indices_.end()) {
458 // We don't have a perfect hash functions so we need a list to collect all stack maps
459 // which might have the same dex register map.
Vladimir Marko225b6462015-09-28 12:17:40 +0100460 ArenaVector<uint32_t> stack_map_indices(allocator_->Adapter(kArenaAllocStackMapStream));
461 stack_map_indices.push_back(current_entry_index);
462 dex_map_hash_to_stack_map_indices_.Put(current_entry_.dex_register_map_hash,
463 std::move(stack_map_indices));
Calin Juravlec416d332015-04-23 16:01:43 +0100464 return kNoSameDexMapFound;
465 }
466
Calin Juravle4f46ac52015-04-23 18:47:21 +0100467 // We might have collisions, so we need to check whether or not we really have a match.
Vladimir Marko225b6462015-09-28 12:17:40 +0100468 for (uint32_t test_entry_index : entries_it->second) {
469 if (HaveTheSameDexMaps(GetStackMap(test_entry_index), current_entry_)) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100470 return test_entry_index;
Calin Juravlec416d332015-04-23 16:01:43 +0100471 }
472 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100473 entries_it->second.push_back(current_entry_index);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100474 return kNoSameDexMapFound;
Calin Juravlec416d332015-04-23 16:01:43 +0100475}
476
477bool StackMapStream::HaveTheSameDexMaps(const StackMapEntry& a, const StackMapEntry& b) const {
478 if (a.live_dex_registers_mask == nullptr && b.live_dex_registers_mask == nullptr) {
479 return true;
480 }
481 if (a.live_dex_registers_mask == nullptr || b.live_dex_registers_mask == nullptr) {
482 return false;
483 }
484 if (a.num_dex_registers != b.num_dex_registers) {
485 return false;
486 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100487 if (a.num_dex_registers != 0u) {
488 DCHECK(a.live_dex_registers_mask != nullptr);
489 DCHECK(b.live_dex_registers_mask != nullptr);
490 if (!a.live_dex_registers_mask->Equal(b.live_dex_registers_mask)) {
Calin Juravlec416d332015-04-23 16:01:43 +0100491 return false;
492 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100493 size_t number_of_live_dex_registers = a.live_dex_registers_mask->NumSetBits();
494 DCHECK_LE(number_of_live_dex_registers, dex_register_locations_.size());
495 DCHECK_LE(a.dex_register_locations_start_index,
496 dex_register_locations_.size() - number_of_live_dex_registers);
497 DCHECK_LE(b.dex_register_locations_start_index,
498 dex_register_locations_.size() - number_of_live_dex_registers);
499 auto a_begin = dex_register_locations_.begin() + a.dex_register_locations_start_index;
500 auto b_begin = dex_register_locations_.begin() + b.dex_register_locations_start_index;
501 if (!std::equal(a_begin, a_begin + number_of_live_dex_registers, b_begin)) {
502 return false;
Calin Juravlec416d332015-04-23 16:01:43 +0100503 }
504 }
505 return true;
506}
507
David Srbecky1bbdfd72016-02-24 16:39:26 +0000508// Helper for CheckCodeInfo - check that register map has the expected content.
509void StackMapStream::CheckDexRegisterMap(const CodeInfo& code_info,
510 const DexRegisterMap& dex_register_map,
511 size_t num_dex_registers,
512 BitVector* live_dex_registers_mask,
513 size_t dex_register_locations_index) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000514 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Srbecky1bbdfd72016-02-24 16:39:26 +0000515 for (size_t reg = 0; reg < num_dex_registers; reg++) {
516 // Find the location we tried to encode.
517 DexRegisterLocation expected = DexRegisterLocation::None();
518 if (live_dex_registers_mask->IsBitSet(reg)) {
519 size_t catalog_index = dex_register_locations_[dex_register_locations_index++];
520 expected = location_catalog_entries_[catalog_index];
521 }
522 // Compare to the seen location.
523 if (expected.GetKind() == DexRegisterLocation::Kind::kNone) {
524 DCHECK(!dex_register_map.IsValid() || !dex_register_map.IsDexRegisterLive(reg));
525 } else {
526 DCHECK(dex_register_map.IsDexRegisterLive(reg));
527 DexRegisterLocation seen = dex_register_map.GetDexRegisterLocation(
528 reg, num_dex_registers, code_info, encoding);
529 DCHECK_EQ(expected.GetKind(), seen.GetKind());
530 DCHECK_EQ(expected.GetValue(), seen.GetValue());
531 }
532 }
533 if (num_dex_registers == 0) {
534 DCHECK(!dex_register_map.IsValid());
535 }
536}
537
538// Check that all StackMapStream inputs are correctly encoded by trying to read them back.
539void StackMapStream::CheckCodeInfo(MemoryRegion region) const {
540 CodeInfo code_info(region);
David Srbecky09ed0982016-02-12 21:58:43 +0000541 CodeInfoEncoding encoding = code_info.ExtractEncoding();
542 DCHECK_EQ(code_info.GetNumberOfStackMaps(encoding), stack_maps_.size());
David Srbecky1bbdfd72016-02-24 16:39:26 +0000543 for (size_t s = 0; s < stack_maps_.size(); ++s) {
544 const StackMap stack_map = code_info.GetStackMapAt(s, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +0000545 const StackMapEncoding& stack_map_encoding = encoding.stack_map_encoding;
David Srbecky1bbdfd72016-02-24 16:39:26 +0000546 StackMapEntry entry = stack_maps_[s];
547
548 // Check main stack map fields.
David Srbecky09ed0982016-02-12 21:58:43 +0000549 DCHECK_EQ(stack_map.GetNativePcOffset(stack_map_encoding), entry.native_pc_offset);
550 DCHECK_EQ(stack_map.GetDexPc(stack_map_encoding), entry.dex_pc);
551 DCHECK_EQ(stack_map.GetRegisterMask(stack_map_encoding), entry.register_mask);
552 size_t num_stack_mask_bits = stack_map.GetNumberOfStackMaskBits(stack_map_encoding);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000553 if (entry.sp_mask != nullptr) {
David Srbecky09ed0982016-02-12 21:58:43 +0000554 DCHECK_GE(num_stack_mask_bits, entry.sp_mask->GetNumberOfBits());
555 for (size_t b = 0; b < num_stack_mask_bits; b++) {
556 DCHECK_EQ(stack_map.GetStackMaskBit(stack_map_encoding, b), entry.sp_mask->IsBitSet(b));
David Srbecky1bbdfd72016-02-24 16:39:26 +0000557 }
558 } else {
David Srbecky09ed0982016-02-12 21:58:43 +0000559 for (size_t b = 0; b < num_stack_mask_bits; b++) {
560 DCHECK_EQ(stack_map.GetStackMaskBit(stack_map_encoding, b), 0u);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000561 }
562 }
563
564 CheckDexRegisterMap(code_info,
565 code_info.GetDexRegisterMapOf(
566 stack_map, encoding, entry.num_dex_registers),
567 entry.num_dex_registers,
568 entry.live_dex_registers_mask,
569 entry.dex_register_locations_start_index);
570
571 // Check inline info.
David Srbecky09ed0982016-02-12 21:58:43 +0000572 DCHECK_EQ(stack_map.HasInlineInfo(stack_map_encoding), (entry.inlining_depth != 0));
David Srbecky1bbdfd72016-02-24 16:39:26 +0000573 if (entry.inlining_depth != 0) {
574 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
David Srbecky61b28a12016-02-25 21:55:03 +0000575 DCHECK_EQ(inline_info.GetDepth(encoding.inline_info_encoding), entry.inlining_depth);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000576 for (size_t d = 0; d < entry.inlining_depth; ++d) {
577 size_t inline_info_index = entry.inline_infos_start_index + d;
578 DCHECK_LT(inline_info_index, inline_infos_.size());
579 InlineInfoEntry inline_entry = inline_infos_[inline_info_index];
David Srbecky61b28a12016-02-25 21:55:03 +0000580 DCHECK_EQ(inline_info.GetDexPcAtDepth(encoding.inline_info_encoding, d),
581 inline_entry.dex_pc);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000582 if (inline_info.EncodesArtMethodAtDepth(encoding.inline_info_encoding, d)) {
583 DCHECK_EQ(inline_info.GetArtMethodAtDepth(encoding.inline_info_encoding, d),
584 inline_entry.method);
585 } else {
586 DCHECK_EQ(inline_info.GetMethodIndexAtDepth(encoding.inline_info_encoding, d),
587 inline_entry.method_index);
588 }
David Srbecky1bbdfd72016-02-24 16:39:26 +0000589
590 CheckDexRegisterMap(code_info,
591 code_info.GetDexRegisterMapAtDepth(
592 d, inline_info, encoding, inline_entry.num_dex_registers),
593 inline_entry.num_dex_registers,
594 inline_entry.live_dex_registers_mask,
595 inline_entry.dex_register_locations_start_index);
596 }
597 }
598 }
599}
600
Calin Juravlec416d332015-04-23 16:01:43 +0100601} // namespace art