blob: 48037b37c794af7d62e6ef192d94ca8e0e4ab6e8 [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"
Nicolas Geoffrayf290c012017-01-27 23:09:22 +000020#include "optimizing/optimizing_compiler.h"
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000021#include "runtime.h"
22#include "scoped_thread_state_change-inl.h"
23
Calin Juravlec416d332015-04-23 16:01:43 +010024namespace art {
25
Calin Juravle4f46ac52015-04-23 18:47:21 +010026void StackMapStream::BeginStackMapEntry(uint32_t dex_pc,
27 uint32_t native_pc_offset,
28 uint32_t register_mask,
29 BitVector* sp_mask,
30 uint32_t num_dex_registers,
31 uint8_t inlining_depth) {
32 DCHECK_EQ(0u, current_entry_.dex_pc) << "EndStackMapEntry not called after BeginStackMapEntry";
Calin Juravleb5c46932015-10-06 17:09:49 +010033 DCHECK_NE(dex_pc, static_cast<uint32_t>(-1)) << "invalid dex_pc";
Calin Juravle4f46ac52015-04-23 18:47:21 +010034 current_entry_.dex_pc = dex_pc;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -080035 current_entry_.native_pc_code_offset = CodeOffset::FromOffset(native_pc_offset, instruction_set_);
Calin Juravle4f46ac52015-04-23 18:47:21 +010036 current_entry_.register_mask = register_mask;
37 current_entry_.sp_mask = sp_mask;
38 current_entry_.num_dex_registers = num_dex_registers;
39 current_entry_.inlining_depth = inlining_depth;
Vladimir Marko225b6462015-09-28 12:17:40 +010040 current_entry_.dex_register_locations_start_index = dex_register_locations_.size();
41 current_entry_.inline_infos_start_index = inline_infos_.size();
Calin Juravle4f46ac52015-04-23 18:47:21 +010042 current_entry_.dex_register_map_hash = 0;
43 current_entry_.same_dex_register_map_as_ = kNoSameDexMapFound;
Calin Juravlec416d332015-04-23 16:01:43 +010044 if (num_dex_registers != 0) {
Calin Juravle4f46ac52015-04-23 18:47:21 +010045 current_entry_.live_dex_registers_mask =
Vladimir Markof6a35de2016-03-21 12:01:50 +000046 ArenaBitVector::Create(allocator_, num_dex_registers, true, kArenaAllocStackMapStream);
Calin Juravlec416d332015-04-23 16:01:43 +010047 } else {
Calin Juravle4f46ac52015-04-23 18:47:21 +010048 current_entry_.live_dex_registers_mask = nullptr;
Calin Juravlec416d332015-04-23 16:01:43 +010049 }
Calin Juravlec416d332015-04-23 16:01:43 +010050
51 if (sp_mask != nullptr) {
52 stack_mask_max_ = std::max(stack_mask_max_, sp_mask->GetHighestBitSet());
53 }
54 if (inlining_depth > 0) {
55 number_of_stack_maps_with_inline_info_++;
56 }
57
58 dex_pc_max_ = std::max(dex_pc_max_, dex_pc);
Calin Juravlec416d332015-04-23 16:01:43 +010059 register_mask_max_ = std::max(register_mask_max_, register_mask);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010060 current_dex_register_ = 0;
Calin Juravlec416d332015-04-23 16:01:43 +010061}
62
Calin Juravle4f46ac52015-04-23 18:47:21 +010063void StackMapStream::EndStackMapEntry() {
64 current_entry_.same_dex_register_map_as_ = FindEntryWithTheSameDexMap();
Vladimir Marko225b6462015-09-28 12:17:40 +010065 stack_maps_.push_back(current_entry_);
Calin Juravle4f46ac52015-04-23 18:47:21 +010066 current_entry_ = StackMapEntry();
67}
68
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010069void StackMapStream::AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value) {
Calin Juravlec416d332015-04-23 16:01:43 +010070 if (kind != DexRegisterLocation::Kind::kNone) {
71 // Ensure we only use non-compressed location kind at this stage.
David Srbecky7dc11782016-02-25 13:23:56 +000072 DCHECK(DexRegisterLocation::IsShortLocationKind(kind)) << kind;
Calin Juravlec416d332015-04-23 16:01:43 +010073 DexRegisterLocation location(kind, value);
74
75 // Look for Dex register `location` in the location catalog (using the
76 // companion hash map of locations to indices). Use its index if it
77 // is already in the location catalog. If not, insert it (in the
78 // location catalog and the hash map) and use the newly created index.
79 auto it = location_catalog_entries_indices_.Find(location);
80 if (it != location_catalog_entries_indices_.end()) {
81 // Retrieve the index from the hash map.
Vladimir Marko225b6462015-09-28 12:17:40 +010082 dex_register_locations_.push_back(it->second);
Calin Juravlec416d332015-04-23 16:01:43 +010083 } else {
84 // Create a new entry in the location catalog and the hash map.
Vladimir Marko225b6462015-09-28 12:17:40 +010085 size_t index = location_catalog_entries_.size();
86 location_catalog_entries_.push_back(location);
87 dex_register_locations_.push_back(index);
Calin Juravlec416d332015-04-23 16:01:43 +010088 location_catalog_entries_indices_.Insert(std::make_pair(location, index));
89 }
90
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010091 if (in_inline_frame_) {
92 // TODO: Support sharing DexRegisterMap across InlineInfo.
93 DCHECK_LT(current_dex_register_, current_inline_info_.num_dex_registers);
94 current_inline_info_.live_dex_registers_mask->SetBit(current_dex_register_);
95 } else {
96 DCHECK_LT(current_dex_register_, current_entry_.num_dex_registers);
97 current_entry_.live_dex_registers_mask->SetBit(current_dex_register_);
98 current_entry_.dex_register_map_hash += (1 <<
99 (current_dex_register_ % (sizeof(current_entry_.dex_register_map_hash) * kBitsPerByte)));
100 current_entry_.dex_register_map_hash += static_cast<uint32_t>(value);
101 current_entry_.dex_register_map_hash += static_cast<uint32_t>(kind);
102 }
Calin Juravlec416d332015-04-23 16:01:43 +0100103 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100104 current_dex_register_++;
Calin Juravlec416d332015-04-23 16:01:43 +0100105}
106
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000107void StackMapStream::BeginInlineInfoEntry(ArtMethod* method,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100108 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000109 uint32_t num_dex_registers,
110 const DexFile* outer_dex_file) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100111 DCHECK(!in_inline_frame_);
112 in_inline_frame_ = true;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000113 if (EncodeArtMethodInInlineInfo(method)) {
114 current_inline_info_.method = method;
115 } else {
116 if (dex_pc != static_cast<uint32_t>(-1) && kIsDebugBuild) {
117 ScopedObjectAccess soa(Thread::Current());
118 DCHECK(IsSameDexFile(*outer_dex_file, *method->GetDexFile()));
119 }
120 current_inline_info_.method_index = method->GetDexMethodIndexUnchecked();
121 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100122 current_inline_info_.dex_pc = dex_pc;
123 current_inline_info_.num_dex_registers = num_dex_registers;
Vladimir Marko225b6462015-09-28 12:17:40 +0100124 current_inline_info_.dex_register_locations_start_index = dex_register_locations_.size();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100125 if (num_dex_registers != 0) {
126 current_inline_info_.live_dex_registers_mask =
Vladimir Markof6a35de2016-03-21 12:01:50 +0000127 ArenaBitVector::Create(allocator_, num_dex_registers, true, kArenaAllocStackMapStream);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100128 } else {
129 current_inline_info_.live_dex_registers_mask = nullptr;
130 }
131 current_dex_register_ = 0;
132}
133
134void StackMapStream::EndInlineInfoEntry() {
135 DCHECK(in_inline_frame_);
136 DCHECK_EQ(current_dex_register_, current_inline_info_.num_dex_registers)
137 << "Inline information contains less registers than expected";
138 in_inline_frame_ = false;
Vladimir Marko225b6462015-09-28 12:17:40 +0100139 inline_infos_.push_back(current_inline_info_);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100140 current_inline_info_ = InlineInfoEntry();
Calin Juravlec416d332015-04-23 16:01:43 +0100141}
142
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800143CodeOffset StackMapStream::ComputeMaxNativePcCodeOffset() const {
144 CodeOffset max_native_pc_offset;
Vladimir Marko225b6462015-09-28 12:17:40 +0100145 for (const StackMapEntry& entry : stack_maps_) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800146 max_native_pc_offset = std::max(max_native_pc_offset, entry.native_pc_code_offset);
Vladimir Markobd8c7252015-06-12 10:06:32 +0100147 }
148 return max_native_pc_offset;
149}
150
Calin Juravle4f46ac52015-04-23 18:47:21 +0100151size_t StackMapStream::PrepareForFillIn() {
152 int stack_mask_number_of_bits = stack_mask_max_ + 1; // Need room for max element too.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100153 dex_register_maps_size_ = ComputeDexRegisterMapsSize();
David Srbecky61b28a12016-02-25 21:55:03 +0000154 ComputeInlineInfoEncoding(); // needs dex_register_maps_size_.
155 inline_info_size_ = inline_infos_.size() * inline_info_encoding_.GetEntrySize();
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800156 CodeOffset max_native_pc_offset = ComputeMaxNativePcCodeOffset();
157 // The stack map contains compressed native offsets.
158 size_t stack_map_size = stack_map_encoding_.SetFromSizes(max_native_pc_offset.CompressedValue(),
David Srbecky09ed0982016-02-12 21:58:43 +0000159 dex_pc_max_,
160 dex_register_maps_size_,
161 inline_info_size_,
162 register_mask_max_,
163 stack_mask_number_of_bits);
164 stack_maps_size_ = stack_maps_.size() * stack_map_size;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100165 dex_register_location_catalog_size_ = ComputeDexRegisterLocationCatalogSize();
166
David Srbecky09ed0982016-02-12 21:58:43 +0000167 size_t non_header_size =
168 stack_maps_size_ +
169 dex_register_location_catalog_size_ +
170 dex_register_maps_size_ +
171 inline_info_size_;
Calin Juravlec416d332015-04-23 16:01:43 +0100172
David Srbecky09ed0982016-02-12 21:58:43 +0000173 // Prepare the CodeInfo variable-sized encoding.
174 CodeInfoEncoding code_info_encoding;
175 code_info_encoding.non_header_size = non_header_size;
David Srbecky09ed0982016-02-12 21:58:43 +0000176 code_info_encoding.number_of_stack_maps = stack_maps_.size();
177 code_info_encoding.stack_map_size_in_bytes = stack_map_size;
David Srbecky61b28a12016-02-25 21:55:03 +0000178 code_info_encoding.stack_map_encoding = stack_map_encoding_;
179 code_info_encoding.inline_info_encoding = inline_info_encoding_;
David Srbecky09ed0982016-02-12 21:58:43 +0000180 code_info_encoding.number_of_location_catalog_entries = location_catalog_entries_.size();
181 code_info_encoding.Compress(&code_info_encoding_);
182
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100183 // TODO: Move the catalog at the end. It is currently too expensive at runtime
184 // to compute its size (note that we do not encode that size in the CodeInfo).
David Srbecky09ed0982016-02-12 21:58:43 +0000185 dex_register_location_catalog_start_ = code_info_encoding_.size() + stack_maps_size_;
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100186 dex_register_maps_start_ =
187 dex_register_location_catalog_start_ + dex_register_location_catalog_size_;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100188 inline_infos_start_ = dex_register_maps_start_ + dex_register_maps_size_;
Calin Juravlec416d332015-04-23 16:01:43 +0100189
David Srbecky09ed0982016-02-12 21:58:43 +0000190 needed_size_ = code_info_encoding_.size() + non_header_size;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100191 return needed_size_;
Calin Juravlec416d332015-04-23 16:01:43 +0100192}
193
194size_t StackMapStream::ComputeDexRegisterLocationCatalogSize() const {
195 size_t size = DexRegisterLocationCatalog::kFixedSize;
Vladimir Marko225b6462015-09-28 12:17:40 +0100196 for (const DexRegisterLocation& dex_register_location : location_catalog_entries_) {
Calin Juravlec416d332015-04-23 16:01:43 +0100197 size += DexRegisterLocationCatalog::EntrySize(dex_register_location);
198 }
199 return size;
200}
201
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100202size_t StackMapStream::ComputeDexRegisterMapSize(uint32_t num_dex_registers,
Vladimir Marko225b6462015-09-28 12:17:40 +0100203 const BitVector* live_dex_registers_mask) const {
204 // For num_dex_registers == 0u live_dex_registers_mask may be null.
205 if (num_dex_registers == 0u) {
206 return 0u; // No register map will be emitted.
207 }
208 DCHECK(live_dex_registers_mask != nullptr);
209
Calin Juravlec416d332015-04-23 16:01:43 +0100210 // Size of the map in bytes.
211 size_t size = DexRegisterMap::kFixedSize;
212 // Add the live bit mask for the Dex register liveness.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100213 size += DexRegisterMap::GetLiveBitMaskSize(num_dex_registers);
Calin Juravlec416d332015-04-23 16:01:43 +0100214 // Compute the size of the set of live Dex register entries.
Vladimir Marko225b6462015-09-28 12:17:40 +0100215 size_t number_of_live_dex_registers = live_dex_registers_mask->NumSetBits();
Calin Juravlec416d332015-04-23 16:01:43 +0100216 size_t map_entries_size_in_bits =
Vladimir Marko225b6462015-09-28 12:17:40 +0100217 DexRegisterMap::SingleEntrySizeInBits(location_catalog_entries_.size())
Calin Juravlec416d332015-04-23 16:01:43 +0100218 * number_of_live_dex_registers;
219 size_t map_entries_size_in_bytes =
220 RoundUp(map_entries_size_in_bits, kBitsPerByte) / kBitsPerByte;
221 size += map_entries_size_in_bytes;
222 return size;
223}
224
Calin Juravle4f46ac52015-04-23 18:47:21 +0100225size_t StackMapStream::ComputeDexRegisterMapsSize() const {
Calin Juravlec416d332015-04-23 16:01:43 +0100226 size_t size = 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100227 size_t inline_info_index = 0;
Vladimir Marko225b6462015-09-28 12:17:40 +0100228 for (const StackMapEntry& entry : stack_maps_) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100229 if (entry.same_dex_register_map_as_ == kNoSameDexMapFound) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100230 size += ComputeDexRegisterMapSize(entry.num_dex_registers, entry.live_dex_registers_mask);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100231 } else {
Calin Juravlec416d332015-04-23 16:01:43 +0100232 // Entries with the same dex map will have the same offset.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100233 }
234 for (size_t j = 0; j < entry.inlining_depth; ++j) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100235 InlineInfoEntry inline_entry = inline_infos_[inline_info_index++];
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100236 size += ComputeDexRegisterMapSize(inline_entry.num_dex_registers,
Vladimir Marko225b6462015-09-28 12:17:40 +0100237 inline_entry.live_dex_registers_mask);
Calin Juravlec416d332015-04-23 16:01:43 +0100238 }
239 }
240 return size;
241}
242
David Srbecky61b28a12016-02-25 21:55:03 +0000243void StackMapStream::ComputeInlineInfoEncoding() {
244 uint32_t method_index_max = 0;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100245 uint32_t dex_pc_max = DexFile::kDexNoIndex;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000246 uint32_t extra_data_max = 0;
David Srbecky61b28a12016-02-25 21:55:03 +0000247
248 uint32_t inline_info_index = 0;
249 for (const StackMapEntry& entry : stack_maps_) {
250 for (size_t j = 0; j < entry.inlining_depth; ++j) {
251 InlineInfoEntry inline_entry = inline_infos_[inline_info_index++];
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000252 if (inline_entry.method == nullptr) {
253 method_index_max = std::max(method_index_max, inline_entry.method_index);
254 extra_data_max = std::max(extra_data_max, 1u);
255 } else {
256 method_index_max = std::max(
257 method_index_max, High32Bits(reinterpret_cast<uintptr_t>(inline_entry.method)));
258 extra_data_max = std::max(
259 extra_data_max, Low32Bits(reinterpret_cast<uintptr_t>(inline_entry.method)));
260 }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100261 if (inline_entry.dex_pc != DexFile::kDexNoIndex &&
262 (dex_pc_max == DexFile::kDexNoIndex || dex_pc_max < inline_entry.dex_pc)) {
263 dex_pc_max = inline_entry.dex_pc;
264 }
David Srbecky61b28a12016-02-25 21:55:03 +0000265 }
266 }
267 DCHECK_EQ(inline_info_index, inline_infos_.size());
268
269 inline_info_encoding_.SetFromSizes(method_index_max,
270 dex_pc_max,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000271 extra_data_max,
David Srbecky61b28a12016-02-25 21:55:03 +0000272 dex_register_maps_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100273}
274
Calin Juravlec416d332015-04-23 16:01:43 +0100275void StackMapStream::FillIn(MemoryRegion region) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100276 DCHECK_EQ(0u, current_entry_.dex_pc) << "EndStackMapEntry not called after BeginStackMapEntry";
277 DCHECK_NE(0u, needed_size_) << "PrepareForFillIn not called before FillIn";
278
Calin Juravle4f46ac52015-04-23 18:47:21 +0100279 DCHECK_EQ(region.size(), needed_size_);
David Srbecky09ed0982016-02-12 21:58:43 +0000280
281 // Note that the memory region does not have to be zeroed when we JIT code
282 // because we do not use the arena allocator there.
283
284 // Write the CodeInfo header.
285 region.CopyFrom(0, MemoryRegion(code_info_encoding_.data(), code_info_encoding_.size()));
Calin Juravlec416d332015-04-23 16:01:43 +0100286
Calin Juravlec416d332015-04-23 16:01:43 +0100287 MemoryRegion dex_register_locations_region = region.Subregion(
Calin Juravle4f46ac52015-04-23 18:47:21 +0100288 dex_register_maps_start_, dex_register_maps_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100289
290 MemoryRegion inline_infos_region = region.Subregion(
Calin Juravle4f46ac52015-04-23 18:47:21 +0100291 inline_infos_start_, inline_info_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100292
David Srbecky09ed0982016-02-12 21:58:43 +0000293 CodeInfo code_info(region);
294 CodeInfoEncoding encoding = code_info.ExtractEncoding();
295 DCHECK_EQ(code_info.GetStackMapsSize(encoding), stack_maps_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100296
297 // Set the Dex register location catalog.
Calin Juravlec416d332015-04-23 16:01:43 +0100298 MemoryRegion dex_register_location_catalog_region = region.Subregion(
Calin Juravle4f46ac52015-04-23 18:47:21 +0100299 dex_register_location_catalog_start_, dex_register_location_catalog_size_);
Calin Juravlec416d332015-04-23 16:01:43 +0100300 DexRegisterLocationCatalog dex_register_location_catalog(dex_register_location_catalog_region);
301 // Offset in `dex_register_location_catalog` where to store the next
302 // register location.
303 size_t location_catalog_offset = DexRegisterLocationCatalog::kFixedSize;
Vladimir Marko225b6462015-09-28 12:17:40 +0100304 for (DexRegisterLocation dex_register_location : location_catalog_entries_) {
Calin Juravlec416d332015-04-23 16:01:43 +0100305 dex_register_location_catalog.SetRegisterInfo(location_catalog_offset, dex_register_location);
306 location_catalog_offset += DexRegisterLocationCatalog::EntrySize(dex_register_location);
307 }
308 // Ensure we reached the end of the Dex registers location_catalog.
309 DCHECK_EQ(location_catalog_offset, dex_register_location_catalog_region.size());
310
Vladimir Markof6a35de2016-03-21 12:01:50 +0000311 ArenaBitVector empty_bitmask(allocator_, 0, /* expandable */ false, kArenaAllocStackMapStream);
Calin Juravlec416d332015-04-23 16:01:43 +0100312 uintptr_t next_dex_register_map_offset = 0;
313 uintptr_t next_inline_info_offset = 0;
Vladimir Marko225b6462015-09-28 12:17:40 +0100314 for (size_t i = 0, e = stack_maps_.size(); i < e; ++i) {
David Srbecky09ed0982016-02-12 21:58:43 +0000315 StackMap stack_map = code_info.GetStackMapAt(i, encoding);
Vladimir Marko225b6462015-09-28 12:17:40 +0100316 StackMapEntry entry = stack_maps_[i];
Calin Juravlec416d332015-04-23 16:01:43 +0100317
David Brazdilf677ebf2015-05-29 16:29:43 +0100318 stack_map.SetDexPc(stack_map_encoding_, entry.dex_pc);
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800319 stack_map.SetNativePcCodeOffset(stack_map_encoding_, entry.native_pc_code_offset);
David Brazdilf677ebf2015-05-29 16:29:43 +0100320 stack_map.SetRegisterMask(stack_map_encoding_, entry.register_mask);
David Srbecky09ed0982016-02-12 21:58:43 +0000321 size_t number_of_stack_mask_bits = stack_map.GetNumberOfStackMaskBits(stack_map_encoding_);
Calin Juravlec416d332015-04-23 16:01:43 +0100322 if (entry.sp_mask != nullptr) {
David Srbecky09ed0982016-02-12 21:58:43 +0000323 for (size_t bit = 0; bit < number_of_stack_mask_bits; bit++) {
324 stack_map.SetStackMaskBit(stack_map_encoding_, bit, entry.sp_mask->IsBitSet(bit));
325 }
David Srbecky1bbdfd72016-02-24 16:39:26 +0000326 } else {
327 // The MemoryRegion does not have to be zeroed, so make sure we clear the bits.
David Srbecky09ed0982016-02-12 21:58:43 +0000328 for (size_t bit = 0; bit < number_of_stack_mask_bits; bit++) {
329 stack_map.SetStackMaskBit(stack_map_encoding_, bit, false);
330 }
Calin Juravlec416d332015-04-23 16:01:43 +0100331 }
332
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000333 if (entry.num_dex_registers == 0 || (entry.live_dex_registers_mask->NumSetBits() == 0)) {
Calin Juravlec416d332015-04-23 16:01:43 +0100334 // No dex map available.
David Brazdilf677ebf2015-05-29 16:29:43 +0100335 stack_map.SetDexRegisterMapOffset(stack_map_encoding_, StackMap::kNoDexRegisterMap);
Calin Juravlec416d332015-04-23 16:01:43 +0100336 } else {
337 // Search for an entry with the same dex map.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100338 if (entry.same_dex_register_map_as_ != kNoSameDexMapFound) {
Calin Juravlec416d332015-04-23 16:01:43 +0100339 // If we have a hit reuse the offset.
David Brazdilf677ebf2015-05-29 16:29:43 +0100340 stack_map.SetDexRegisterMapOffset(
341 stack_map_encoding_,
David Srbecky09ed0982016-02-12 21:58:43 +0000342 code_info.GetStackMapAt(entry.same_dex_register_map_as_, encoding)
David Brazdil77a48ae2015-09-15 12:34:04 +0000343 .GetDexRegisterMapOffset(stack_map_encoding_));
Calin Juravlec416d332015-04-23 16:01:43 +0100344 } else {
345 // New dex registers maps should be added to the stack map.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100346 MemoryRegion register_region = dex_register_locations_region.Subregion(
347 next_dex_register_map_offset,
Vladimir Marko225b6462015-09-28 12:17:40 +0100348 ComputeDexRegisterMapSize(entry.num_dex_registers, entry.live_dex_registers_mask));
Calin Juravlec416d332015-04-23 16:01:43 +0100349 next_dex_register_map_offset += register_region.size();
350 DexRegisterMap dex_register_map(register_region);
351 stack_map.SetDexRegisterMapOffset(
David Brazdilf677ebf2015-05-29 16:29:43 +0100352 stack_map_encoding_, register_region.start() - dex_register_locations_region.start());
Calin Juravlec416d332015-04-23 16:01:43 +0100353
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100354 // Set the dex register location.
355 FillInDexRegisterMap(dex_register_map,
356 entry.num_dex_registers,
357 *entry.live_dex_registers_mask,
358 entry.dex_register_locations_start_index);
Calin Juravlec416d332015-04-23 16:01:43 +0100359 }
360 }
361
362 // Set the inlining info.
363 if (entry.inlining_depth != 0) {
364 MemoryRegion inline_region = inline_infos_region.Subregion(
365 next_inline_info_offset,
David Srbecky61b28a12016-02-25 21:55:03 +0000366 entry.inlining_depth * inline_info_encoding_.GetEntrySize());
Calin Juravlec416d332015-04-23 16:01:43 +0100367 next_inline_info_offset += inline_region.size();
368 InlineInfo inline_info(inline_region);
369
370 // Currently relative to the dex register map.
371 stack_map.SetInlineDescriptorOffset(
David Brazdilf677ebf2015-05-29 16:29:43 +0100372 stack_map_encoding_, inline_region.start() - dex_register_locations_region.start());
Calin Juravlec416d332015-04-23 16:01:43 +0100373
David Srbecky61b28a12016-02-25 21:55:03 +0000374 inline_info.SetDepth(inline_info_encoding_, entry.inlining_depth);
Vladimir Marko225b6462015-09-28 12:17:40 +0100375 DCHECK_LE(entry.inline_infos_start_index + entry.inlining_depth, inline_infos_.size());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100376 for (size_t depth = 0; depth < entry.inlining_depth; ++depth) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100377 InlineInfoEntry inline_entry = inline_infos_[depth + entry.inline_infos_start_index];
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000378 if (inline_entry.method != nullptr) {
379 inline_info.SetMethodIndexAtDepth(
380 inline_info_encoding_,
381 depth,
382 High32Bits(reinterpret_cast<uintptr_t>(inline_entry.method)));
383 inline_info.SetExtraDataAtDepth(
384 inline_info_encoding_,
385 depth,
386 Low32Bits(reinterpret_cast<uintptr_t>(inline_entry.method)));
387 } else {
388 inline_info.SetMethodIndexAtDepth(inline_info_encoding_, depth, inline_entry.method_index);
389 inline_info.SetExtraDataAtDepth(inline_info_encoding_, depth, 1);
390 }
David Srbecky61b28a12016-02-25 21:55:03 +0000391 inline_info.SetDexPcAtDepth(inline_info_encoding_, depth, inline_entry.dex_pc);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100392 if (inline_entry.num_dex_registers == 0) {
393 // No dex map available.
David Srbecky61b28a12016-02-25 21:55:03 +0000394 inline_info.SetDexRegisterMapOffsetAtDepth(inline_info_encoding_,
395 depth,
396 StackMap::kNoDexRegisterMap);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100397 DCHECK(inline_entry.live_dex_registers_mask == nullptr);
398 } else {
399 MemoryRegion register_region = dex_register_locations_region.Subregion(
400 next_dex_register_map_offset,
401 ComputeDexRegisterMapSize(inline_entry.num_dex_registers,
Vladimir Marko225b6462015-09-28 12:17:40 +0100402 inline_entry.live_dex_registers_mask));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100403 next_dex_register_map_offset += register_region.size();
404 DexRegisterMap dex_register_map(register_region);
405 inline_info.SetDexRegisterMapOffsetAtDepth(
David Srbecky61b28a12016-02-25 21:55:03 +0000406 inline_info_encoding_,
407 depth, register_region.start() - dex_register_locations_region.start());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100408
409 FillInDexRegisterMap(dex_register_map,
410 inline_entry.num_dex_registers,
411 *inline_entry.live_dex_registers_mask,
412 inline_entry.dex_register_locations_start_index);
413 }
Calin Juravlec416d332015-04-23 16:01:43 +0100414 }
415 } else {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100416 if (inline_info_size_ != 0) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100417 stack_map.SetInlineDescriptorOffset(stack_map_encoding_, StackMap::kNoInlineInfo);
Calin Juravlec416d332015-04-23 16:01:43 +0100418 }
419 }
420 }
David Srbecky1bbdfd72016-02-24 16:39:26 +0000421
422 // 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) {
521 DCHECK(!dex_register_map.IsValid() || !dex_register_map.IsDexRegisterLive(reg));
522 } else {
523 DCHECK(dex_register_map.IsDexRegisterLive(reg));
524 DexRegisterLocation seen = dex_register_map.GetDexRegisterLocation(
525 reg, num_dex_registers, code_info, encoding);
526 DCHECK_EQ(expected.GetKind(), seen.GetKind());
527 DCHECK_EQ(expected.GetValue(), seen.GetValue());
528 }
529 }
530 if (num_dex_registers == 0) {
531 DCHECK(!dex_register_map.IsValid());
532 }
533}
534
535// Check that all StackMapStream inputs are correctly encoded by trying to read them back.
536void StackMapStream::CheckCodeInfo(MemoryRegion region) const {
537 CodeInfo code_info(region);
David Srbecky09ed0982016-02-12 21:58:43 +0000538 CodeInfoEncoding encoding = code_info.ExtractEncoding();
539 DCHECK_EQ(code_info.GetNumberOfStackMaps(encoding), stack_maps_.size());
David Srbecky1bbdfd72016-02-24 16:39:26 +0000540 for (size_t s = 0; s < stack_maps_.size(); ++s) {
541 const StackMap stack_map = code_info.GetStackMapAt(s, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +0000542 const StackMapEncoding& stack_map_encoding = encoding.stack_map_encoding;
David Srbecky1bbdfd72016-02-24 16:39:26 +0000543 StackMapEntry entry = stack_maps_[s];
544
545 // Check main stack map fields.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800546 DCHECK_EQ(stack_map.GetNativePcOffset(stack_map_encoding, instruction_set_),
547 entry.native_pc_code_offset.Uint32Value(instruction_set_));
David Srbecky09ed0982016-02-12 21:58:43 +0000548 DCHECK_EQ(stack_map.GetDexPc(stack_map_encoding), entry.dex_pc);
549 DCHECK_EQ(stack_map.GetRegisterMask(stack_map_encoding), entry.register_mask);
550 size_t num_stack_mask_bits = stack_map.GetNumberOfStackMaskBits(stack_map_encoding);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000551 if (entry.sp_mask != nullptr) {
David Srbecky09ed0982016-02-12 21:58:43 +0000552 DCHECK_GE(num_stack_mask_bits, entry.sp_mask->GetNumberOfBits());
553 for (size_t b = 0; b < num_stack_mask_bits; b++) {
554 DCHECK_EQ(stack_map.GetStackMaskBit(stack_map_encoding, b), entry.sp_mask->IsBitSet(b));
David Srbecky1bbdfd72016-02-24 16:39:26 +0000555 }
556 } else {
David Srbecky09ed0982016-02-12 21:58:43 +0000557 for (size_t b = 0; b < num_stack_mask_bits; b++) {
558 DCHECK_EQ(stack_map.GetStackMaskBit(stack_map_encoding, b), 0u);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000559 }
560 }
561
562 CheckDexRegisterMap(code_info,
563 code_info.GetDexRegisterMapOf(
564 stack_map, encoding, entry.num_dex_registers),
565 entry.num_dex_registers,
566 entry.live_dex_registers_mask,
567 entry.dex_register_locations_start_index);
568
569 // Check inline info.
David Srbecky09ed0982016-02-12 21:58:43 +0000570 DCHECK_EQ(stack_map.HasInlineInfo(stack_map_encoding), (entry.inlining_depth != 0));
David Srbecky1bbdfd72016-02-24 16:39:26 +0000571 if (entry.inlining_depth != 0) {
572 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
David Srbecky61b28a12016-02-25 21:55:03 +0000573 DCHECK_EQ(inline_info.GetDepth(encoding.inline_info_encoding), entry.inlining_depth);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000574 for (size_t d = 0; d < entry.inlining_depth; ++d) {
575 size_t inline_info_index = entry.inline_infos_start_index + d;
576 DCHECK_LT(inline_info_index, inline_infos_.size());
577 InlineInfoEntry inline_entry = inline_infos_[inline_info_index];
David Srbecky61b28a12016-02-25 21:55:03 +0000578 DCHECK_EQ(inline_info.GetDexPcAtDepth(encoding.inline_info_encoding, d),
579 inline_entry.dex_pc);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000580 if (inline_info.EncodesArtMethodAtDepth(encoding.inline_info_encoding, d)) {
581 DCHECK_EQ(inline_info.GetArtMethodAtDepth(encoding.inline_info_encoding, d),
582 inline_entry.method);
583 } else {
584 DCHECK_EQ(inline_info.GetMethodIndexAtDepth(encoding.inline_info_encoding, d),
585 inline_entry.method_index);
586 }
David Srbecky1bbdfd72016-02-24 16:39:26 +0000587
588 CheckDexRegisterMap(code_info,
589 code_info.GetDexRegisterMapAtDepth(
590 d, inline_info, encoding, inline_entry.num_dex_registers),
591 inline_entry.num_dex_registers,
592 inline_entry.live_dex_registers_mask,
593 inline_entry.dex_register_locations_start_index);
594 }
595 }
596 }
597}
598
Calin Juravlec416d332015-04-23 16:01:43 +0100599} // namespace art