blob: 964b48c6ac3e71fad976c7c952f40d84fd505285 [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 Srbecky049d6812018-05-18 14:46:49 +010019#include <memory>
Santiago Aboy Solanese43aa3f2021-11-01 09:02:09 +000020#include <vector>
David Srbecky049d6812018-05-18 14:46:49 +010021
Andreas Gampe90b936d2017-01-31 08:58:55 -080022#include "art_method-inl.h"
David Srbecky45aa5982016-03-18 02:15:09 +000023#include "base/stl_util.h"
Santiago Aboy Solanese43aa3f2021-11-01 09:02:09 +000024#include "class_linker.h"
25#include "dex/dex_file.h"
David Sehr9e734c72018-01-04 17:56:19 -080026#include "dex/dex_file_types.h"
Santiago Aboy Solanese43aa3f2021-11-01 09:02:09 +000027#include "optimizing/nodes.h"
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +000028#include "optimizing/optimizing_compiler.h"
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000029#include "runtime.h"
30#include "scoped_thread_state_change-inl.h"
David Srbecky71ec1cc2018-05-18 15:57:25 +010031#include "stack_map.h"
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000032
Vladimir Marko0a516052019-10-14 13:00:44 +000033namespace art {
Calin Juravlec416d332015-04-23 16:01:43 +010034
David Srbecky049d6812018-05-18 14:46:49 +010035constexpr static bool kVerifyStackMaps = kIsDebugBuild;
36
David Srbeckyd02b23f2018-05-29 23:27:22 +010037uint32_t StackMapStream::GetStackMapNativePcOffset(size_t i) {
David Srbeckyf325e282018-06-13 15:02:32 +010038 return StackMap::UnpackNativePc(stack_maps_[i][StackMap::kPackedNativePc], instruction_set_);
David Srbeckyd02b23f2018-05-29 23:27:22 +010039}
40
41void StackMapStream::SetStackMapNativePcOffset(size_t i, uint32_t native_pc_offset) {
David Srbeckyf325e282018-06-13 15:02:32 +010042 stack_maps_[i][StackMap::kPackedNativePc] =
43 StackMap::PackNativePc(native_pc_offset, instruction_set_);
David Srbeckyd02b23f2018-05-29 23:27:22 +010044}
45
David Srbeckyf6ba5b32018-06-23 22:05:49 +010046void StackMapStream::BeginMethod(size_t frame_size_in_bytes,
47 size_t core_spill_mask,
48 size_t fp_spill_mask,
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +000049 uint32_t num_dex_registers,
50 bool baseline) {
David Srbeckyf6ba5b32018-06-23 22:05:49 +010051 DCHECK(!in_method_) << "Mismatched Begin/End calls";
52 in_method_ = true;
David Srbecky3aaaa212018-07-30 16:46:53 +010053 DCHECK_EQ(packed_frame_size_, 0u) << "BeginMethod was already called";
David Srbeckyf6ba5b32018-06-23 22:05:49 +010054
David Srbecky3aaaa212018-07-30 16:46:53 +010055 DCHECK_ALIGNED(frame_size_in_bytes, kStackAlignment);
56 packed_frame_size_ = frame_size_in_bytes / kStackAlignment;
David Srbeckyf6ba5b32018-06-23 22:05:49 +010057 core_spill_mask_ = core_spill_mask;
58 fp_spill_mask_ = fp_spill_mask;
59 num_dex_registers_ = num_dex_registers;
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +000060 baseline_ = baseline;
David Srbecky67ba8722019-05-23 15:32:18 +010061
62 if (kVerifyStackMaps) {
63 dchecks_.emplace_back([=](const CodeInfo& code_info) {
64 DCHECK_EQ(code_info.packed_frame_size_, frame_size_in_bytes / kStackAlignment);
65 DCHECK_EQ(code_info.core_spill_mask_, core_spill_mask);
66 DCHECK_EQ(code_info.fp_spill_mask_, fp_spill_mask);
67 DCHECK_EQ(code_info.number_of_dex_registers_, num_dex_registers);
68 });
69 }
David Srbeckyf6ba5b32018-06-23 22:05:49 +010070}
71
David Srbecky17b4d2b2021-03-02 18:14:31 +000072void StackMapStream::EndMethod(size_t code_size) {
David Srbeckyf6ba5b32018-06-23 22:05:49 +010073 DCHECK(in_method_) << "Mismatched Begin/End calls";
74 in_method_ = false;
David Srbecky17b4d2b2021-03-02 18:14:31 +000075 code_size_ = code_size;
David Srbeckye7a91942018-08-01 17:23:53 +010076
77 // Read the stack masks now. The compiler might have updated them.
78 for (size_t i = 0; i < lazy_stack_masks_.size(); i++) {
79 BitVector* stack_mask = lazy_stack_masks_[i];
80 if (stack_mask != nullptr && stack_mask->GetNumberOfBits() != 0) {
81 stack_maps_[i][StackMap::kStackMaskIndex] =
82 stack_masks_.Dedup(stack_mask->GetRawStorage(), stack_mask->GetNumberOfBits());
83 }
84 }
David Srbecky17b4d2b2021-03-02 18:14:31 +000085
86 if (kIsDebugBuild) {
87 uint32_t packed_code_size = StackMap::PackNativePc(code_size, instruction_set_);
88 for (size_t i = 0; i < stack_maps_.size(); i++) {
89 DCHECK_LE(stack_maps_[i][StackMap::kPackedNativePc], packed_code_size);
90 }
91 }
92
93 if (kVerifyStackMaps) {
94 dchecks_.emplace_back([=](const CodeInfo& code_info) {
95 CHECK_EQ(code_info.code_size_, code_size);
96 });
97 }
David Srbeckyf6ba5b32018-06-23 22:05:49 +010098}
99
Calin Juravle4f46ac52015-04-23 18:47:21 +0100100void StackMapStream::BeginStackMapEntry(uint32_t dex_pc,
101 uint32_t native_pc_offset,
102 uint32_t register_mask,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100103 BitVector* stack_mask,
Artem Serov2808be82018-12-20 19:15:11 +0000104 StackMap::Kind kind,
105 bool needs_vreg_info) {
David Srbeckyf6ba5b32018-06-23 22:05:49 +0100106 DCHECK(in_method_) << "Call BeginMethod first";
David Srbecky71ec1cc2018-05-18 15:57:25 +0100107 DCHECK(!in_stack_map_) << "Mismatched Begin/End calls";
108 in_stack_map_ = true;
109
David Srbeckycf7833e2018-06-14 16:45:22 +0100110 current_stack_map_ = BitTableBuilder<StackMap>::Entry();
David Srbeckyf325e282018-06-13 15:02:32 +0100111 current_stack_map_[StackMap::kKind] = static_cast<uint32_t>(kind);
112 current_stack_map_[StackMap::kPackedNativePc] =
113 StackMap::PackNativePc(native_pc_offset, instruction_set_);
114 current_stack_map_[StackMap::kDexPc] = dex_pc;
David Srbecky0b4e5a32018-06-11 16:25:29 +0100115 if (stack_maps_.size() > 0) {
116 // Check that non-catch stack maps are sorted by pc.
117 // Catch stack maps are at the end and may be unordered.
118 if (stack_maps_.back()[StackMap::kKind] == StackMap::Kind::Catch) {
119 DCHECK(current_stack_map_[StackMap::kKind] == StackMap::Kind::Catch);
120 } else if (current_stack_map_[StackMap::kKind] != StackMap::Kind::Catch) {
121 DCHECK_LE(stack_maps_.back()[StackMap::kPackedNativePc],
122 current_stack_map_[StackMap::kPackedNativePc]);
123 }
124 }
David Srbecky71ec1cc2018-05-18 15:57:25 +0100125 if (register_mask != 0) {
126 uint32_t shift = LeastSignificantBit(register_mask);
David Srbeckycf7833e2018-06-14 16:45:22 +0100127 BitTableBuilder<RegisterMask>::Entry entry;
David Srbeckyf325e282018-06-13 15:02:32 +0100128 entry[RegisterMask::kValue] = register_mask >> shift;
129 entry[RegisterMask::kShift] = shift;
130 current_stack_map_[StackMap::kRegisterMaskIndex] = register_masks_.Dedup(&entry);
Vladimir Marko174b2e22017-10-12 13:34:49 +0100131 }
David Srbecky71ec1cc2018-05-18 15:57:25 +0100132 // The compiler assumes the bit vector will be read during PrepareForFillIn(),
133 // and it might modify the data before that. Therefore, just store the pointer.
134 // See ClearSpillSlotsFromLoopPhisInStackMap in code_generator.h.
135 lazy_stack_masks_.push_back(stack_mask);
David Srbecky6eb4d5e2018-06-03 12:00:20 +0100136 current_inline_infos_.clear();
David Srbecky71ec1cc2018-05-18 15:57:25 +0100137 current_dex_registers_.clear();
Artem Serov2808be82018-12-20 19:15:11 +0000138 expected_num_dex_registers_ = needs_vreg_info ? num_dex_registers_ : 0u;
David Srbecky71ec1cc2018-05-18 15:57:25 +0100139
David Srbecky049d6812018-05-18 14:46:49 +0100140 if (kVerifyStackMaps) {
141 size_t stack_map_index = stack_maps_.size();
142 // Create lambda method, which will be executed at the very end to verify data.
143 // Parameters and local variables will be captured(stored) by the lambda "[=]".
144 dchecks_.emplace_back([=](const CodeInfo& code_info) {
David Srbecky50fac062018-06-13 18:55:35 +0100145 if (kind == StackMap::Kind::Default || kind == StackMap::Kind::OSR) {
146 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset,
147 instruction_set_);
148 CHECK_EQ(stack_map.Row(), stack_map_index);
149 } else if (kind == StackMap::Kind::Catch) {
150 StackMap stack_map = code_info.GetCatchStackMapForDexPc(dex_pc);
151 CHECK_EQ(stack_map.Row(), stack_map_index);
152 }
David Srbecky049d6812018-05-18 14:46:49 +0100153 StackMap stack_map = code_info.GetStackMapAt(stack_map_index);
154 CHECK_EQ(stack_map.GetNativePcOffset(instruction_set_), native_pc_offset);
David Srbecky50fac062018-06-13 18:55:35 +0100155 CHECK_EQ(stack_map.GetKind(), static_cast<uint32_t>(kind));
David Srbecky049d6812018-05-18 14:46:49 +0100156 CHECK_EQ(stack_map.GetDexPc(), dex_pc);
157 CHECK_EQ(code_info.GetRegisterMaskOf(stack_map), register_mask);
158 BitMemoryRegion seen_stack_mask = code_info.GetStackMaskOf(stack_map);
159 CHECK_GE(seen_stack_mask.size_in_bits(), stack_mask ? stack_mask->GetNumberOfBits() : 0);
160 for (size_t b = 0; b < seen_stack_mask.size_in_bits(); b++) {
161 CHECK_EQ(seen_stack_mask.LoadBit(b), stack_mask != nullptr && stack_mask->IsBitSet(b));
162 }
David Srbecky049d6812018-05-18 14:46:49 +0100163 });
David Srbecky71ec1cc2018-05-18 15:57:25 +0100164 }
Calin Juravlec416d332015-04-23 16:01:43 +0100165}
166
Calin Juravle4f46ac52015-04-23 18:47:21 +0100167void StackMapStream::EndStackMapEntry() {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100168 DCHECK(in_stack_map_) << "Mismatched Begin/End calls";
169 in_stack_map_ = false;
David Srbecky71ec1cc2018-05-18 15:57:25 +0100170
David Srbecky6eb4d5e2018-06-03 12:00:20 +0100171 // Generate index into the InlineInfo table.
David Srbeckyf6ba5b32018-06-23 22:05:49 +0100172 size_t inlining_depth = current_inline_infos_.size();
David Srbecky6eb4d5e2018-06-03 12:00:20 +0100173 if (!current_inline_infos_.empty()) {
David Srbeckyf325e282018-06-13 15:02:32 +0100174 current_inline_infos_.back()[InlineInfo::kIsLast] = InlineInfo::kLast;
175 current_stack_map_[StackMap::kInlineInfoIndex] =
David Srbecky6eb4d5e2018-06-03 12:00:20 +0100176 inline_infos_.Dedup(current_inline_infos_.data(), current_inline_infos_.size());
David Srbecky71ec1cc2018-05-18 15:57:25 +0100177 }
178
David Srbecky6de88332018-06-03 12:00:11 +0100179 // Generate delta-compressed dex register map.
David Srbeckyf6ba5b32018-06-23 22:05:49 +0100180 size_t num_dex_registers = current_dex_registers_.size();
181 if (!current_dex_registers_.empty()) {
182 DCHECK_EQ(expected_num_dex_registers_, current_dex_registers_.size());
183 CreateDexRegisterMap();
184 }
David Srbecky6de88332018-06-03 12:00:11 +0100185
David Srbecky71ec1cc2018-05-18 15:57:25 +0100186 stack_maps_.Add(current_stack_map_);
David Srbeckyf6ba5b32018-06-23 22:05:49 +0100187
188 if (kVerifyStackMaps) {
189 size_t stack_map_index = stack_maps_.size() - 1;
190 dchecks_.emplace_back([=](const CodeInfo& code_info) {
191 StackMap stack_map = code_info.GetStackMapAt(stack_map_index);
192 CHECK_EQ(stack_map.HasDexRegisterMap(), (num_dex_registers != 0));
193 CHECK_EQ(stack_map.HasInlineInfo(), (inlining_depth != 0));
David Srbecky93bd3612018-07-02 19:30:18 +0100194 CHECK_EQ(code_info.GetInlineInfosOf(stack_map).size(), inlining_depth);
David Srbeckyf6ba5b32018-06-23 22:05:49 +0100195 });
196 }
Calin Juravle4f46ac52015-04-23 18:47:21 +0100197}
198
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000199void StackMapStream::BeginInlineInfoEntry(ArtMethod* method,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100200 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000201 uint32_t num_dex_registers,
202 const DexFile* outer_dex_file) {
David Srbeckyf6ba5b32018-06-23 22:05:49 +0100203 DCHECK(in_stack_map_) << "Call BeginStackMapEntry first";
David Srbecky71ec1cc2018-05-18 15:57:25 +0100204 DCHECK(!in_inline_info_) << "Mismatched Begin/End calls";
205 in_inline_info_ = true;
206 DCHECK_EQ(expected_num_dex_registers_, current_dex_registers_.size());
207
David Srbecky6de88332018-06-03 12:00:11 +0100208 expected_num_dex_registers_ += num_dex_registers;
209
David Srbeckycf7833e2018-06-14 16:45:22 +0100210 BitTableBuilder<InlineInfo>::Entry entry;
David Srbeckyf325e282018-06-13 15:02:32 +0100211 entry[InlineInfo::kIsLast] = InlineInfo::kMore;
212 entry[InlineInfo::kDexPc] = dex_pc;
213 entry[InlineInfo::kNumberOfDexRegisters] = static_cast<uint32_t>(expected_num_dex_registers_);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000214 if (EncodeArtMethodInInlineInfo(method)) {
David Srbeckyf325e282018-06-13 15:02:32 +0100215 entry[InlineInfo::kArtMethodHi] = High32Bits(reinterpret_cast<uintptr_t>(method));
216 entry[InlineInfo::kArtMethodLo] = Low32Bits(reinterpret_cast<uintptr_t>(method));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000217 } else {
Santiago Aboy Solanese43aa3f2021-11-01 09:02:09 +0000218 uint32_t bootclasspath_index = MethodInfo::kSameDexFile;
219 if (dex_pc != static_cast<uint32_t>(-1)) {
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000220 ScopedObjectAccess soa(Thread::Current());
Santiago Aboy Solanese43aa3f2021-11-01 09:02:09 +0000221 const DexFile* dex_file = method->GetDexFile();
222 if (method->GetDeclaringClass()->GetClassLoader() == nullptr) {
223 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
224 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
225 auto it = std::find_if(
226 boot_class_path.begin(), boot_class_path.end(), [dex_file](const DexFile* df) {
227 return IsSameDexFile(*df, *dex_file);
228 });
229 DCHECK(it != boot_class_path.end());
230 bootclasspath_index = std::distance(boot_class_path.begin(), it);
231 } else {
232 DCHECK(IsSameDexFile(*outer_dex_file, *dex_file));
233 }
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000234 }
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100235 uint32_t dex_method_index = method->GetDexMethodIndex();
Santiago Aboy Solanese43aa3f2021-11-01 09:02:09 +0000236 entry[InlineInfo::kMethodInfoIndex] =
237 method_infos_.Dedup({dex_method_index, bootclasspath_index});
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000238 }
David Srbecky6eb4d5e2018-06-03 12:00:20 +0100239 current_inline_infos_.push_back(entry);
David Srbecky71ec1cc2018-05-18 15:57:25 +0100240
David Srbecky049d6812018-05-18 14:46:49 +0100241 if (kVerifyStackMaps) {
242 size_t stack_map_index = stack_maps_.size();
David Srbecky6eb4d5e2018-06-03 12:00:20 +0100243 size_t depth = current_inline_infos_.size() - 1;
David Srbecky049d6812018-05-18 14:46:49 +0100244 dchecks_.emplace_back([=](const CodeInfo& code_info) {
245 StackMap stack_map = code_info.GetStackMapAt(stack_map_index);
David Srbecky93bd3612018-07-02 19:30:18 +0100246 InlineInfo inline_info = code_info.GetInlineInfosOf(stack_map)[depth];
David Srbecky6e69e522018-06-03 12:00:14 +0100247 CHECK_EQ(inline_info.GetDexPc(), dex_pc);
David Srbecky049d6812018-05-18 14:46:49 +0100248 bool encode_art_method = EncodeArtMethodInInlineInfo(method);
David Srbecky6e69e522018-06-03 12:00:14 +0100249 CHECK_EQ(inline_info.EncodesArtMethod(), encode_art_method);
David Srbecky049d6812018-05-18 14:46:49 +0100250 if (encode_art_method) {
David Srbecky6e69e522018-06-03 12:00:14 +0100251 CHECK_EQ(inline_info.GetArtMethod(), method);
David Srbecky049d6812018-05-18 14:46:49 +0100252 } else {
Santiago Aboy Solanese43aa3f2021-11-01 09:02:09 +0000253 MethodInfo method_info = code_info.GetMethodInfoOf(inline_info);
254 CHECK_EQ(method_info.GetMethodIndex(), method->GetDexMethodIndex());
255 if (inline_info.GetDexPc() != static_cast<uint32_t>(-1)) {
256 ScopedObjectAccess soa(Thread::Current());
257 if (method->GetDeclaringClass()->GetClassLoader() == nullptr) {
258 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
259 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
260 DCHECK_LT(method_info.GetDexFileIndex(), boot_class_path.size());
261 CHECK(IsSameDexFile(*boot_class_path[method_info.GetDexFileIndex()],
262 *method->GetDexFile()));
263 }
264 }
David Srbecky049d6812018-05-18 14:46:49 +0100265 }
David Srbecky049d6812018-05-18 14:46:49 +0100266 });
David Srbecky71ec1cc2018-05-18 15:57:25 +0100267 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100268}
269
270void StackMapStream::EndInlineInfoEntry() {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100271 DCHECK(in_inline_info_) << "Mismatched Begin/End calls";
272 in_inline_info_ = false;
273 DCHECK_EQ(expected_num_dex_registers_, current_dex_registers_.size());
Calin Juravlec416d332015-04-23 16:01:43 +0100274}
275
David Srbecky6de88332018-06-03 12:00:11 +0100276// Create delta-compressed dex register map based on the current list of DexRegisterLocations.
277// All dex registers for a stack map are concatenated - inlined registers are just appended.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100278void StackMapStream::CreateDexRegisterMap() {
David Srbecky6de88332018-06-03 12:00:11 +0100279 // These are fields rather than local variables so that we can reuse the reserved memory.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100280 temp_dex_register_mask_.ClearAllBits();
281 temp_dex_register_map_.clear();
David Srbecky6de88332018-06-03 12:00:11 +0100282
283 // Ensure that the arrays that hold previous state are big enough to be safely indexed below.
284 if (previous_dex_registers_.size() < current_dex_registers_.size()) {
285 previous_dex_registers_.resize(current_dex_registers_.size(), DexRegisterLocation::None());
286 dex_register_timestamp_.resize(current_dex_registers_.size(), 0u);
287 }
288
289 // Set bit in the mask for each register that has been changed since the previous stack map.
290 // Modified registers are stored in the catalogue and the catalogue index added to the list.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100291 for (size_t i = 0; i < current_dex_registers_.size(); i++) {
292 DexRegisterLocation reg = current_dex_registers_[i];
David Srbecky6de88332018-06-03 12:00:11 +0100293 // Distance is difference between this index and the index of last modification.
294 uint32_t distance = stack_maps_.size() - dex_register_timestamp_[i];
295 if (previous_dex_registers_[i] != reg || distance > kMaxDexRegisterMapSearchDistance) {
David Srbeckycf7833e2018-06-14 16:45:22 +0100296 BitTableBuilder<DexRegisterInfo>::Entry entry;
David Srbeckyf325e282018-06-13 15:02:32 +0100297 entry[DexRegisterInfo::kKind] = static_cast<uint32_t>(reg.GetKind());
298 entry[DexRegisterInfo::kPackedValue] =
299 DexRegisterInfo::PackValue(reg.GetKind(), reg.GetValue());
David Srbecky6de88332018-06-03 12:00:11 +0100300 uint32_t index = reg.IsLive() ? dex_register_catalog_.Dedup(&entry) : kNoValue;
David Srbecky71ec1cc2018-05-18 15:57:25 +0100301 temp_dex_register_mask_.SetBit(i);
David Srbeckyf325e282018-06-13 15:02:32 +0100302 temp_dex_register_map_.push_back({index});
David Srbecky6de88332018-06-03 12:00:11 +0100303 previous_dex_registers_[i] = reg;
304 dex_register_timestamp_[i] = stack_maps_.size();
David Srbecky71ec1cc2018-05-18 15:57:25 +0100305 }
Calin Juravlec416d332015-04-23 16:01:43 +0100306 }
Calin Juravlec416d332015-04-23 16:01:43 +0100307
David Srbecky6de88332018-06-03 12:00:11 +0100308 // Set the mask and map for the current StackMap (which includes inlined registers).
David Srbecky71ec1cc2018-05-18 15:57:25 +0100309 if (temp_dex_register_mask_.GetNumberOfBits() != 0) {
David Srbeckyf325e282018-06-13 15:02:32 +0100310 current_stack_map_[StackMap::kDexRegisterMaskIndex] =
David Srbecky6de88332018-06-03 12:00:11 +0100311 dex_register_masks_.Dedup(temp_dex_register_mask_.GetRawStorage(),
312 temp_dex_register_mask_.GetNumberOfBits());
Vladimir Marko225b6462015-09-28 12:17:40 +0100313 }
David Srbecky6de88332018-06-03 12:00:11 +0100314 if (!current_dex_registers_.empty()) {
David Srbeckyf325e282018-06-13 15:02:32 +0100315 current_stack_map_[StackMap::kDexRegisterMapIndex] =
David Srbecky6de88332018-06-03 12:00:11 +0100316 dex_register_maps_.Dedup(temp_dex_register_map_.data(),
317 temp_dex_register_map_.size());
David Srbecky052f8ca2018-04-26 15:42:54 +0100318 }
David Srbecky049d6812018-05-18 14:46:49 +0100319
320 if (kVerifyStackMaps) {
321 size_t stack_map_index = stack_maps_.size();
David Srbecky049d6812018-05-18 14:46:49 +0100322 // We need to make copy of the current registers for later (when the check is run).
David Srbecky6de88332018-06-03 12:00:11 +0100323 auto expected_dex_registers = std::make_shared<dchecked_vector<DexRegisterLocation>>(
David Srbecky049d6812018-05-18 14:46:49 +0100324 current_dex_registers_.begin(), current_dex_registers_.end());
325 dchecks_.emplace_back([=](const CodeInfo& code_info) {
326 StackMap stack_map = code_info.GetStackMapAt(stack_map_index);
David Srbecky6de88332018-06-03 12:00:11 +0100327 uint32_t expected_reg = 0;
328 for (DexRegisterLocation reg : code_info.GetDexRegisterMapOf(stack_map)) {
329 CHECK_EQ((*expected_dex_registers)[expected_reg++], reg);
David Srbecky049d6812018-05-18 14:46:49 +0100330 }
David Srbecky93bd3612018-07-02 19:30:18 +0100331 for (InlineInfo inline_info : code_info.GetInlineInfosOf(stack_map)) {
332 DexRegisterMap map = code_info.GetInlineDexRegisterMapOf(stack_map, inline_info);
333 for (DexRegisterLocation reg : map) {
David Srbecky6de88332018-06-03 12:00:11 +0100334 CHECK_EQ((*expected_dex_registers)[expected_reg++], reg);
335 }
336 }
337 CHECK_EQ(expected_reg, expected_dex_registers->size());
David Srbecky049d6812018-05-18 14:46:49 +0100338 });
339 }
Calin Juravlec416d332015-04-23 16:01:43 +0100340}
341
David Srbeckye7a91942018-08-01 17:23:53 +0100342ScopedArenaVector<uint8_t> StackMapStream::Encode() {
343 DCHECK(in_stack_map_ == false) << "Mismatched Begin/End calls";
344 DCHECK(in_inline_info_ == false) << "Mismatched Begin/End calls";
David Srbecky71ec1cc2018-05-18 15:57:25 +0100345
David Srbecky697c47a2019-06-16 21:53:07 +0100346 uint32_t flags = (inline_infos_.size() > 0) ? CodeInfo::kHasInlineInfo : 0;
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000347 flags |= baseline_ ? CodeInfo::kIsBaseline : 0;
David Srbecky17b4d2b2021-03-02 18:14:31 +0000348 DCHECK_LE(flags, kVarintMax); // Ensure flags can be read directly as byte.
David Srbecky697c47a2019-06-16 21:53:07 +0100349 uint32_t bit_table_flags = 0;
350 ForEachBitTable([&bit_table_flags](size_t i, auto bit_table) {
351 if (bit_table->size() != 0) { // Record which bit-tables are stored.
352 bit_table_flags |= 1 << i;
353 }
354 });
355
David Srbeckye7a91942018-08-01 17:23:53 +0100356 ScopedArenaVector<uint8_t> buffer(allocator_->Adapter(kArenaAllocStackMapStream));
357 BitMemoryWriter<ScopedArenaVector<uint8_t>> out(&buffer);
David Srbecky6c4ec5c2019-06-20 07:23:19 +0000358 out.WriteInterleavedVarints(std::array<uint32_t, CodeInfo::kNumHeaders>{
359 flags,
David Srbecky17b4d2b2021-03-02 18:14:31 +0000360 code_size_,
David Srbecky6c4ec5c2019-06-20 07:23:19 +0000361 packed_frame_size_,
362 core_spill_mask_,
363 fp_spill_mask_,
364 num_dex_registers_,
365 bit_table_flags,
366 });
David Srbecky697c47a2019-06-16 21:53:07 +0100367 ForEachBitTable([&out](size_t, auto bit_table) {
368 if (bit_table->size() != 0) { // Skip empty bit-tables.
369 bit_table->Encode(out);
370 }
371 });
David Srbecky45aa5982016-03-18 02:15:09 +0000372
David Srbeckya38e6cf2018-06-26 18:13:49 +0100373 // Verify that we can load the CodeInfo and check some essentials.
David Srbecky0d4567f2019-05-30 22:45:40 +0100374 size_t number_of_read_bits;
375 CodeInfo code_info(buffer.data(), &number_of_read_bits);
376 CHECK_EQ(number_of_read_bits, out.NumberOfWrittenBits());
David Srbeckya38e6cf2018-06-26 18:13:49 +0100377 CHECK_EQ(code_info.GetNumberOfStackMaps(), stack_maps_.size());
David Srbecky17b4d2b2021-03-02 18:14:31 +0000378 CHECK_EQ(CodeInfo::HasInlineInfo(buffer.data()), inline_infos_.size() > 0);
379 CHECK_EQ(CodeInfo::IsBaseline(buffer.data()), baseline_);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800380
David Srbecky049d6812018-05-18 14:46:49 +0100381 // Verify all written data (usually only in debug builds).
382 if (kVerifyStackMaps) {
David Srbecky049d6812018-05-18 14:46:49 +0100383 for (const auto& dcheck : dchecks_) {
384 dcheck(code_info);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000385 }
David Srbecky71ec1cc2018-05-18 15:57:25 +0100386 }
David Srbeckye7a91942018-08-01 17:23:53 +0100387
388 return buffer;
David Srbecky1bbdfd72016-02-24 16:39:26 +0000389}
390
Calin Juravlec416d332015-04-23 16:01:43 +0100391} // namespace art