blob: cd82284a9aa5494b64a24684a62dde2a875e3913 [file] [log] [blame]
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001/*
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 */
16
17#include "stack_map.h"
18
David Srbecky71ec1cc2018-05-18 15:57:25 +010019#include <iomanip>
Nicolas Geoffray896f8f72015-03-30 15:44:25 +010020#include <stdint.h>
21
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000022#include "art_method.h"
David Sehr9c4a0152018-04-05 12:23:54 -070023#include "base/indenter.h"
David Srbecky86decb62018-06-05 06:41:10 +010024#include "base/stats.h"
David Srbeckyf6ba5b32018-06-23 22:05:49 +010025#include "oat_quick_method_header.h"
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000026#include "scoped_thread_state_change-inl.h"
Roland Levillain0396ed72015-05-27 15:12:19 +010027
Nicolas Geoffray004c2302015-03-20 10:06:38 +000028namespace art {
29
David Srbecky6ee06e92018-07-25 21:45:54 +010030CodeInfo::CodeInfo(const OatQuickMethodHeader* header, DecodeFlags flags)
31 : CodeInfo(header->GetOptimizedCodeInfoPtr(), flags) {
David Srbeckyf6ba5b32018-06-23 22:05:49 +010032}
33
David Srbeckyb73323c2018-07-15 23:58:44 +010034template<typename Accessor>
35ALWAYS_INLINE static void DecodeTable(BitTable<Accessor>& table,
36 BitMemoryReader& reader,
37 const uint8_t* data) {
38 bool is_deduped = reader.ReadBit();
39 if (is_deduped) {
40 // 'data' points to the start of the reader's data.
41 uint32_t current_bit_offset = reader.GetBitOffset();
42 uint32_t bit_offset_backwards = DecodeVarintBits(reader) - current_bit_offset;
43 uint32_t byte_offset_backwards = BitsToBytesRoundUp(bit_offset_backwards);
44 BitMemoryReader reader2(data - byte_offset_backwards,
45 byte_offset_backwards * kBitsPerByte - bit_offset_backwards);
46 table.Decode(reader2);
47 } else {
48 table.Decode(reader);
49 }
50}
51
David Srbecky6ee06e92018-07-25 21:45:54 +010052void CodeInfo::Decode(const uint8_t* data, DecodeFlags flags) {
David Srbeckya38e6cf2018-06-26 18:13:49 +010053 const uint8_t* begin = data;
David Srbeckyf6ba5b32018-06-23 22:05:49 +010054 frame_size_in_bytes_ = DecodeUnsignedLeb128(&data);
55 core_spill_mask_ = DecodeUnsignedLeb128(&data);
56 fp_spill_mask_ = DecodeUnsignedLeb128(&data);
57 number_of_dex_registers_ = DecodeUnsignedLeb128(&data);
David Srbeckya38e6cf2018-06-26 18:13:49 +010058 BitMemoryReader reader(data, /* bit_offset */ 0);
David Srbeckyb73323c2018-07-15 23:58:44 +010059 DecodeTable(stack_maps_, reader, data);
60 DecodeTable(inline_infos_, reader, data);
David Srbecky8cd54542018-07-15 23:58:44 +010061 DecodeTable(method_infos_, reader, data);
David Srbecky6ee06e92018-07-25 21:45:54 +010062 if (flags & DecodeFlags::InlineInfoOnly) {
63 return;
64 }
David Srbeckyb73323c2018-07-15 23:58:44 +010065 DecodeTable(register_masks_, reader, data);
66 DecodeTable(stack_masks_, reader, data);
67 DecodeTable(dex_register_masks_, reader, data);
68 DecodeTable(dex_register_maps_, reader, data);
69 DecodeTable(dex_register_catalog_, reader, data);
David Srbeckya38e6cf2018-06-26 18:13:49 +010070 size_in_bits_ = (data - begin) * kBitsPerByte + reader.GetBitOffset();
David Srbecky078d7ba2018-06-21 15:36:48 +010071}
72
David Srbeckyb73323c2018-07-15 23:58:44 +010073template<typename Accessor>
74ALWAYS_INLINE static void DedupeTable(BitMemoryWriter<std::vector<uint8_t>>& writer,
75 BitMemoryReader& reader,
76 CodeInfo::DedupeMap* dedupe_map) {
77 bool is_deduped = reader.ReadBit();
78 DCHECK(!is_deduped);
79 BitTable<Accessor> bit_table(reader);
80 BitMemoryRegion region = reader.Tail(bit_table.BitSize());
81 auto it = dedupe_map->insert(std::make_pair(region, writer.GetBitOffset() + 1 /* dedupe bit */));
82 if (it.second /* new bit table */ || region.size_in_bits() < 32) {
83 writer.WriteBit(false); // Is not deduped.
84 writer.WriteRegion(region);
85 } else {
86 writer.WriteBit(true); // Is deduped.
87 EncodeVarintBits(writer, writer.GetBitOffset() - it.first->second);
88 }
89}
90
91size_t CodeInfo::Dedupe(std::vector<uint8_t>* out, const uint8_t* in, DedupeMap* dedupe_map) {
92 // Remember the current offset in the output buffer so that we can return it later.
93 const size_t result = out->size();
94 // Copy the header which encodes QuickMethodFrameInfo.
95 EncodeUnsignedLeb128(out, DecodeUnsignedLeb128(&in));
96 EncodeUnsignedLeb128(out, DecodeUnsignedLeb128(&in));
97 EncodeUnsignedLeb128(out, DecodeUnsignedLeb128(&in));
98 EncodeUnsignedLeb128(out, DecodeUnsignedLeb128(&in));
99 BitMemoryReader reader(in, /* bit_offset */ 0);
100 BitMemoryWriter<std::vector<uint8_t>> writer(out, /* bit_offset */ out->size() * kBitsPerByte);
101 DedupeTable<StackMap>(writer, reader, dedupe_map);
102 DedupeTable<InlineInfo>(writer, reader, dedupe_map);
David Srbecky8cd54542018-07-15 23:58:44 +0100103 DedupeTable<MethodInfo>(writer, reader, dedupe_map);
David Srbeckyb73323c2018-07-15 23:58:44 +0100104 DedupeTable<RegisterMask>(writer, reader, dedupe_map);
105 DedupeTable<MaskInfo>(writer, reader, dedupe_map);
106 DedupeTable<MaskInfo>(writer, reader, dedupe_map);
107 DedupeTable<DexRegisterMapInfo>(writer, reader, dedupe_map);
108 DedupeTable<DexRegisterInfo>(writer, reader, dedupe_map);
109 return result;
110}
111
David Srbecky0b4e5a32018-06-11 16:25:29 +0100112BitTable<StackMap>::const_iterator CodeInfo::BinarySearchNativePc(uint32_t packed_pc) const {
113 return std::partition_point(
114 stack_maps_.begin(),
115 stack_maps_.end(),
116 [packed_pc](const StackMap& sm) {
117 return sm.GetPackedNativePc() < packed_pc && sm.GetKind() != StackMap::Kind::Catch;
118 });
119}
120
121StackMap CodeInfo::GetStackMapForNativePcOffset(uint32_t pc, InstructionSet isa) const {
122 auto it = BinarySearchNativePc(StackMap::PackNativePc(pc, isa));
123 // Start at the lower bound and iterate over all stack maps with the given native pc.
124 for (; it != stack_maps_.end() && (*it).GetNativePcOffset(isa) == pc; ++it) {
125 StackMap::Kind kind = static_cast<StackMap::Kind>((*it).GetKind());
126 if (kind == StackMap::Kind::Default || kind == StackMap::Kind::OSR) {
127 return *it;
128 }
129 }
David Srbeckya45a85c2018-06-21 16:03:12 +0100130 return stack_maps_.GetInvalidRow();
David Srbecky0b4e5a32018-06-11 16:25:29 +0100131}
132
David Srbecky6de88332018-06-03 12:00:11 +0100133// Scan backward to determine dex register locations at given stack map.
134// All registers for a stack map are combined - inlined registers are just appended,
135// therefore 'first_dex_register' allows us to select a sub-range to decode.
136void CodeInfo::DecodeDexRegisterMap(uint32_t stack_map_index,
137 uint32_t first_dex_register,
138 /*out*/ DexRegisterMap* map) const {
139 // Count remaining work so we know when we have finished.
140 uint32_t remaining_registers = map->size();
141
142 // Keep scanning backwards and collect the most recent location of each register.
143 for (int32_t s = stack_map_index; s >= 0 && remaining_registers != 0; s--) {
144 StackMap stack_map = GetStackMapAt(s);
145 DCHECK_LE(stack_map_index - s, kMaxDexRegisterMapSearchDistance) << "Unbounded search";
146
147 // The mask specifies which registers where modified in this stack map.
148 // NB: the mask can be shorter than expected if trailing zero bits were removed.
149 uint32_t mask_index = stack_map.GetDexRegisterMaskIndex();
150 if (mask_index == StackMap::kNoValue) {
151 continue; // Nothing changed at this stack map.
152 }
153 BitMemoryRegion mask = dex_register_masks_.GetBitMemoryRegion(mask_index);
154 if (mask.size_in_bits() <= first_dex_register) {
155 continue; // Nothing changed after the first register we are interested in.
156 }
157
158 // The map stores one catalogue index per each modified register location.
159 uint32_t map_index = stack_map.GetDexRegisterMapIndex();
160 DCHECK_NE(map_index, StackMap::kNoValue);
161
162 // Skip initial registers which we are not interested in (to get to inlined registers).
163 map_index += mask.PopCount(0, first_dex_register);
164 mask = mask.Subregion(first_dex_register, mask.size_in_bits() - first_dex_register);
165
166 // Update registers that we see for first time (i.e. most recent value).
167 DexRegisterLocation* regs = map->data();
168 const uint32_t end = std::min<uint32_t>(map->size(), mask.size_in_bits());
169 const size_t kNumBits = BitSizeOf<uint32_t>();
170 for (uint32_t reg = 0; reg < end; reg += kNumBits) {
171 // Process the mask in chunks of kNumBits for performance.
172 uint32_t bits = mask.LoadBits(reg, std::min<uint32_t>(end - reg, kNumBits));
173 while (bits != 0) {
174 uint32_t bit = CTZ(bits);
175 if (regs[reg + bit].GetKind() == DexRegisterLocation::Kind::kInvalid) {
176 regs[reg + bit] = GetDexRegisterCatalogEntry(dex_register_maps_.Get(map_index));
177 remaining_registers--;
178 }
179 map_index++;
180 bits ^= 1u << bit; // Clear the bit.
181 }
182 }
183 }
184
185 // Set any remaining registers to None (which is the default state at first stack map).
186 if (remaining_registers != 0) {
187 DexRegisterLocation* regs = map->data();
188 for (uint32_t r = 0; r < map->size(); r++) {
189 if (regs[r].GetKind() == DexRegisterLocation::Kind::kInvalid) {
190 regs[r] = DexRegisterLocation::None();
191 }
192 }
193 }
194}
195
David Srbecky86decb62018-06-05 06:41:10 +0100196template<typename Accessor>
197static void AddTableSizeStats(const char* table_name,
David Srbeckycf7833e2018-06-14 16:45:22 +0100198 const BitTable<Accessor>& table,
David Srbecky86decb62018-06-05 06:41:10 +0100199 /*out*/ Stats* parent) {
200 Stats* table_stats = parent->Child(table_name);
201 table_stats->AddBits(table.BitSize());
202 table_stats->Child("Header")->AddBits(table.HeaderBitSize());
203 const char* const* column_names = GetBitTableColumnNames<Accessor>();
204 for (size_t c = 0; c < table.NumColumns(); c++) {
205 if (table.NumColumnBits(c) > 0) {
206 Stats* column_stats = table_stats->Child(column_names[c]);
207 column_stats->AddBits(table.NumRows() * table.NumColumnBits(c), table.NumRows());
208 }
209 }
210}
211
212void CodeInfo::AddSizeStats(/*out*/ Stats* parent) const {
213 Stats* stats = parent->Child("CodeInfo");
David Srbeckya38e6cf2018-06-26 18:13:49 +0100214 stats->AddBytes(Size());
David Srbecky86decb62018-06-05 06:41:10 +0100215 AddTableSizeStats<StackMap>("StackMaps", stack_maps_, stats);
David Srbecky8cd54542018-07-15 23:58:44 +0100216 AddTableSizeStats<InlineInfo>("InlineInfos", inline_infos_, stats);
217 AddTableSizeStats<MethodInfo>("MethodInfo", method_infos_, stats);
David Srbecky86decb62018-06-05 06:41:10 +0100218 AddTableSizeStats<RegisterMask>("RegisterMasks", register_masks_, stats);
219 AddTableSizeStats<MaskInfo>("StackMasks", stack_masks_, stats);
David Srbecky86decb62018-06-05 06:41:10 +0100220 AddTableSizeStats<MaskInfo>("DexRegisterMasks", dex_register_masks_, stats);
221 AddTableSizeStats<DexRegisterMapInfo>("DexRegisterMaps", dex_register_maps_, stats);
222 AddTableSizeStats<DexRegisterInfo>("DexRegisterCatalog", dex_register_catalog_, stats);
223}
224
David Srbeckye1402122018-06-13 18:20:45 +0100225void DexRegisterMap::Dump(VariableIndentationOutputStream* vios) const {
226 if (HasAnyLiveDexRegisters()) {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100227 ScopedIndentation indent1(vios);
David Srbeckye1402122018-06-13 18:20:45 +0100228 for (size_t i = 0; i < size(); ++i) {
229 DexRegisterLocation reg = (*this)[i];
230 if (reg.IsLive()) {
231 vios->Stream() << "v" << i << ":" << reg << " ";
David Srbecky71ec1cc2018-05-18 15:57:25 +0100232 }
233 }
234 vios->Stream() << "\n";
235 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000236}
237
David Srbecky86decb62018-06-05 06:41:10 +0100238template<typename Accessor>
David Srbecky71ec1cc2018-05-18 15:57:25 +0100239static void DumpTable(VariableIndentationOutputStream* vios,
240 const char* table_name,
David Srbeckycf7833e2018-06-14 16:45:22 +0100241 const BitTable<Accessor>& table,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100242 bool verbose,
243 bool is_mask = false) {
244 if (table.NumRows() != 0) {
David Srbecky86decb62018-06-05 06:41:10 +0100245 vios->Stream() << table_name << " BitSize=" << table.BitSize();
David Srbecky71ec1cc2018-05-18 15:57:25 +0100246 vios->Stream() << " Rows=" << table.NumRows() << " Bits={";
David Srbecky86decb62018-06-05 06:41:10 +0100247 const char* const* column_names = GetBitTableColumnNames<Accessor>();
David Srbecky71ec1cc2018-05-18 15:57:25 +0100248 for (size_t c = 0; c < table.NumColumns(); c++) {
249 vios->Stream() << (c != 0 ? " " : "");
David Srbecky86decb62018-06-05 06:41:10 +0100250 vios->Stream() << column_names[c] << "=" << table.NumColumnBits(c);
David Srbecky71ec1cc2018-05-18 15:57:25 +0100251 }
252 vios->Stream() << "}\n";
253 if (verbose) {
254 ScopedIndentation indent1(vios);
255 for (size_t r = 0; r < table.NumRows(); r++) {
256 vios->Stream() << "[" << std::right << std::setw(3) << r << "]={";
257 for (size_t c = 0; c < table.NumColumns(); c++) {
258 vios->Stream() << (c != 0 ? " " : "");
259 if (is_mask) {
260 BitMemoryRegion bits = table.GetBitMemoryRegion(r, c);
261 for (size_t b = 0, e = bits.size_in_bits(); b < e; b++) {
262 vios->Stream() << bits.LoadBit(e - b - 1);
263 }
264 } else {
265 vios->Stream() << std::right << std::setw(8) << static_cast<int32_t>(table.Get(r, c));
266 }
267 }
268 vios->Stream() << "}\n";
269 }
270 }
271 }
David Srbecky61b28a12016-02-25 21:55:03 +0000272}
273
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100274void CodeInfo::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100275 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100276 bool verbose,
David Srbecky8cd54542018-07-15 23:58:44 +0100277 InstructionSet instruction_set) const {
David Srbeckyb73323c2018-07-15 23:58:44 +0100278 vios->Stream() << "CodeInfo\n";
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100279 ScopedIndentation indent1(vios);
David Srbecky86decb62018-06-05 06:41:10 +0100280 DumpTable<StackMap>(vios, "StackMaps", stack_maps_, verbose);
David Srbecky8cd54542018-07-15 23:58:44 +0100281 DumpTable<InlineInfo>(vios, "InlineInfos", inline_infos_, verbose);
282 DumpTable<MethodInfo>(vios, "MethodInfo", method_infos_, verbose);
David Srbecky86decb62018-06-05 06:41:10 +0100283 DumpTable<RegisterMask>(vios, "RegisterMasks", register_masks_, verbose);
284 DumpTable<MaskInfo>(vios, "StackMasks", stack_masks_, verbose, true /* is_mask */);
David Srbecky86decb62018-06-05 06:41:10 +0100285 DumpTable<MaskInfo>(vios, "DexRegisterMasks", dex_register_masks_, verbose, true /* is_mask */);
286 DumpTable<DexRegisterMapInfo>(vios, "DexRegisterMaps", dex_register_maps_, verbose);
287 DumpTable<DexRegisterInfo>(vios, "DexRegisterCatalog", dex_register_catalog_, verbose);
David Srbecky71ec1cc2018-05-18 15:57:25 +0100288
Roland Levillaina552e1c2015-03-26 15:01:03 +0000289 // Display stack maps along with (live) Dex register maps.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100290 if (verbose) {
David Srbecky93bd3612018-07-02 19:30:18 +0100291 for (StackMap stack_map : stack_maps_) {
David Srbecky8cd54542018-07-15 23:58:44 +0100292 stack_map.Dump(vios, *this, code_offset, instruction_set);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100293 }
294 }
295}
296
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100297void StackMap::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100298 const CodeInfo& code_info,
299 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100300 InstructionSet instruction_set) const {
David Srbecky052f8ca2018-04-26 15:42:54 +0100301 const uint32_t pc_offset = GetNativePcOffset(instruction_set);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100302 vios->Stream()
David Srbecky71ec1cc2018-05-18 15:57:25 +0100303 << "StackMap[" << Row() << "]"
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100304 << std::hex
David Srbecky71ec1cc2018-05-18 15:57:25 +0100305 << " (native_pc=0x" << code_offset + pc_offset
306 << ", dex_pc=0x" << GetDexPc()
David Srbecky052f8ca2018-04-26 15:42:54 +0100307 << ", register_mask=0x" << code_info.GetRegisterMaskOf(*this)
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100308 << std::dec
309 << ", stack_mask=0b";
David Srbecky052f8ca2018-04-26 15:42:54 +0100310 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(*this);
David Srbecky4b59d102018-05-29 21:46:10 +0000311 for (size_t i = 0, e = stack_mask.size_in_bits(); i < e; ++i) {
David Srbecky45aa5982016-03-18 02:15:09 +0000312 vios->Stream() << stack_mask.LoadBit(e - i - 1);
Roland Levillainf2650d12015-05-28 14:53:28 +0100313 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100314 vios->Stream() << ")\n";
David Srbeckye1402122018-06-13 18:20:45 +0100315 code_info.GetDexRegisterMapOf(*this).Dump(vios);
David Srbecky93bd3612018-07-02 19:30:18 +0100316 for (InlineInfo inline_info : code_info.GetInlineInfosOf(*this)) {
David Srbecky8cd54542018-07-15 23:58:44 +0100317 inline_info.Dump(vios, code_info, *this);
Nicolas Geoffray12bdb722015-06-17 09:44:43 +0100318 }
Roland Levillainf2650d12015-05-28 14:53:28 +0100319}
320
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100321void InlineInfo::Dump(VariableIndentationOutputStream* vios,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100322 const CodeInfo& code_info,
David Srbecky8cd54542018-07-15 23:58:44 +0100323 const StackMap& stack_map) const {
David Srbecky6e69e522018-06-03 12:00:14 +0100324 uint32_t depth = Row() - stack_map.GetInlineInfoIndex();
325 vios->Stream()
326 << "InlineInfo[" << Row() << "]"
327 << " (depth=" << depth
328 << std::hex
329 << ", dex_pc=0x" << GetDexPc();
330 if (EncodesArtMethod()) {
331 ScopedObjectAccess soa(Thread::Current());
332 vios->Stream() << ", method=" << GetArtMethod()->PrettyMethod();
333 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100334 vios->Stream()
David Srbecky6e69e522018-06-03 12:00:14 +0100335 << std::dec
David Srbecky8cd54542018-07-15 23:58:44 +0100336 << ", method_index=" << code_info.GetMethodIndexOf(*this);
David Srbecky6e69e522018-06-03 12:00:14 +0100337 }
338 vios->Stream() << ")\n";
David Srbecky93bd3612018-07-02 19:30:18 +0100339 code_info.GetInlineDexRegisterMapOf(stack_map, *this).Dump(vios);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000340}
341
342} // namespace art