blob: eebca853c6f2e3840ffe515146292f2136606dcc [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 Srbecky6ee06e92018-07-25 21:45:54 +010034void CodeInfo::Decode(const uint8_t* data, DecodeFlags flags) {
David Srbecky3aaaa212018-07-30 16:46:53 +010035 BitMemoryReader reader(data);
David Srbecky6c4ec5c2019-06-20 07:23:19 +000036 std::array<uint32_t, kNumHeaders> header = reader.ReadInterleavedVarints<kNumHeaders>();
David Srbecky697c47a2019-06-16 21:53:07 +010037 ForEachHeaderField([this, &header](size_t i, auto member_pointer) {
38 this->*member_pointer = header[i];
39 });
40 ForEachBitTableField([this, &reader](size_t i, auto member_pointer) {
41 auto& table = this->*member_pointer;
David Srbecky6c4ec5c2019-06-20 07:23:19 +000042 if (LIKELY(HasBitTable(i))) {
David Srbecky697c47a2019-06-16 21:53:07 +010043 if (UNLIKELY(IsBitTableDeduped(i))) {
44 ssize_t bit_offset = reader.NumberOfReadBits() - reader.ReadVarint();
45 BitMemoryReader reader2(reader.data(), bit_offset); // The offset is negative.
46 table.Decode(reader2);
47 } else {
48 table.Decode(reader);
49 }
50 }
David Srbecky42deda82018-08-10 11:23:27 +010051 }, flags);
David Srbeckyd1606412018-07-31 15:05:14 +010052 size_in_bits_ = reader.NumberOfReadBits();
David Srbeckye42a4b92019-05-26 00:10:25 +010053 if (flags == AllTables) {
54 DCHECK_EQ(HasInlineInfo(data), HasInlineInfo());
55 }
David Srbecky078d7ba2018-06-21 15:36:48 +010056}
57
David Srbecky42deda82018-08-10 11:23:27 +010058size_t CodeInfo::Deduper::Dedupe(const uint8_t* code_info_data) {
David Srbeckyd1606412018-07-31 15:05:14 +010059 writer_.ByteAlign();
60 size_t deduped_offset = writer_.NumberOfWrittenBits() / kBitsPerByte;
David Srbecky697c47a2019-06-16 21:53:07 +010061
David Srbecky6c4ec5c2019-06-20 07:23:19 +000062 // The back-reference offset takes space so dedupe is not worth it for tiny tables.
63 constexpr size_t kMinDedupSize = 32; // Assume 32-bit offset on average.
64
David Srbecky697c47a2019-06-16 21:53:07 +010065 // Read the existing code info and find (and keep) dedup-map iterator for each table.
66 // The iterator stores BitMemoryRegion and bit_offset of previous identical BitTable.
David Srbecky42deda82018-08-10 11:23:27 +010067 BitMemoryReader reader(code_info_data);
68 CodeInfo code_info; // Temporary storage for decoded data.
David Srbecky6c4ec5c2019-06-20 07:23:19 +000069 std::array<uint32_t, kNumHeaders> header = reader.ReadInterleavedVarints<kNumHeaders>();
70 ForEachHeaderField([&code_info, &header](size_t i, auto member_pointer) {
71 code_info.*member_pointer = header[i];
David Srbecky697c47a2019-06-16 21:53:07 +010072 });
73 std::map<BitMemoryRegion, uint32_t, BitMemoryRegion::Less>::iterator it[kNumBitTables];
74 ForEachBitTableField([this, &reader, &code_info, &it](size_t i, auto member_pointer) {
75 DCHECK(!code_info.IsBitTableDeduped(i));
76 if (code_info.HasBitTable(i)) {
77 size_t bit_table_start = reader.NumberOfReadBits();
78 (code_info.*member_pointer).Decode(reader);
79 BitMemoryRegion region = reader.GetReadRegion().Subregion(bit_table_start);
80 it[i] = dedupe_map_.emplace(region, /* default bit_offset */ 0).first;
David Srbecky6c4ec5c2019-06-20 07:23:19 +000081 if (it[i]->second != 0 && region.size_in_bits() > kMinDedupSize) { // Seen before and large?
David Srbecky697c47a2019-06-16 21:53:07 +010082 code_info.SetBitTableDeduped(i); // Mark as deduped before we write header.
83 }
84 }
85 });
86
87 // Write the code info back, but replace deduped tables with relative offsets.
David Srbecky6c4ec5c2019-06-20 07:23:19 +000088 ForEachHeaderField([&code_info, &header](size_t i, auto member_pointer) {
89 header[i] = code_info.*member_pointer;
David Srbecky42deda82018-08-10 11:23:27 +010090 });
David Srbecky6c4ec5c2019-06-20 07:23:19 +000091 writer_.WriteInterleavedVarints(header);
David Srbecky697c47a2019-06-16 21:53:07 +010092 ForEachBitTableField([this, &code_info, &it](size_t i, auto) {
93 if (code_info.HasBitTable(i)) {
94 uint32_t& bit_offset = it[i]->second;
95 if (code_info.IsBitTableDeduped(i)) {
96 DCHECK_NE(bit_offset, 0u);
97 writer_.WriteVarint(writer_.NumberOfWrittenBits() - bit_offset);
98 } else {
99 bit_offset = writer_.NumberOfWrittenBits(); // Store offset in dedup map.
100 writer_.WriteRegion(it[i]->first);
101 }
David Srbecky42deda82018-08-10 11:23:27 +0100102 }
103 });
David Srbeckyd1606412018-07-31 15:05:14 +0100104
105 if (kIsDebugBuild) {
David Srbecky42deda82018-08-10 11:23:27 +0100106 CodeInfo old_code_info(code_info_data);
David Srbeckyd1606412018-07-31 15:05:14 +0100107 CodeInfo new_code_info(writer_.data() + deduped_offset);
David Srbecky697c47a2019-06-16 21:53:07 +0100108 ForEachHeaderField([&old_code_info, &new_code_info](size_t, auto member_pointer) {
109 if (member_pointer != &CodeInfo::bit_table_flags_) { // Expected to differ.
110 DCHECK_EQ(old_code_info.*member_pointer, new_code_info.*member_pointer);
111 }
David Srbecky42deda82018-08-10 11:23:27 +0100112 });
David Srbecky6c4ec5c2019-06-20 07:23:19 +0000113 ForEachBitTableField([&old_code_info, &new_code_info](size_t i, auto member_pointer) {
114 DCHECK_EQ(old_code_info.HasBitTable(i), new_code_info.HasBitTable(i));
David Srbecky42deda82018-08-10 11:23:27 +0100115 DCHECK((old_code_info.*member_pointer).Equals(new_code_info.*member_pointer));
116 });
David Srbeckyd1606412018-07-31 15:05:14 +0100117 }
118
119 return deduped_offset;
David Srbeckyb73323c2018-07-15 23:58:44 +0100120}
121
David Srbecky0b4e5a32018-06-11 16:25:29 +0100122BitTable<StackMap>::const_iterator CodeInfo::BinarySearchNativePc(uint32_t packed_pc) const {
123 return std::partition_point(
124 stack_maps_.begin(),
125 stack_maps_.end(),
126 [packed_pc](const StackMap& sm) {
127 return sm.GetPackedNativePc() < packed_pc && sm.GetKind() != StackMap::Kind::Catch;
128 });
129}
130
131StackMap CodeInfo::GetStackMapForNativePcOffset(uint32_t pc, InstructionSet isa) const {
132 auto it = BinarySearchNativePc(StackMap::PackNativePc(pc, isa));
133 // Start at the lower bound and iterate over all stack maps with the given native pc.
134 for (; it != stack_maps_.end() && (*it).GetNativePcOffset(isa) == pc; ++it) {
135 StackMap::Kind kind = static_cast<StackMap::Kind>((*it).GetKind());
136 if (kind == StackMap::Kind::Default || kind == StackMap::Kind::OSR) {
137 return *it;
138 }
139 }
David Srbeckya45a85c2018-06-21 16:03:12 +0100140 return stack_maps_.GetInvalidRow();
David Srbecky0b4e5a32018-06-11 16:25:29 +0100141}
142
David Srbecky6de88332018-06-03 12:00:11 +0100143// Scan backward to determine dex register locations at given stack map.
144// All registers for a stack map are combined - inlined registers are just appended,
145// therefore 'first_dex_register' allows us to select a sub-range to decode.
146void CodeInfo::DecodeDexRegisterMap(uint32_t stack_map_index,
147 uint32_t first_dex_register,
148 /*out*/ DexRegisterMap* map) const {
149 // Count remaining work so we know when we have finished.
150 uint32_t remaining_registers = map->size();
151
152 // Keep scanning backwards and collect the most recent location of each register.
153 for (int32_t s = stack_map_index; s >= 0 && remaining_registers != 0; s--) {
154 StackMap stack_map = GetStackMapAt(s);
155 DCHECK_LE(stack_map_index - s, kMaxDexRegisterMapSearchDistance) << "Unbounded search";
156
157 // The mask specifies which registers where modified in this stack map.
158 // NB: the mask can be shorter than expected if trailing zero bits were removed.
159 uint32_t mask_index = stack_map.GetDexRegisterMaskIndex();
160 if (mask_index == StackMap::kNoValue) {
161 continue; // Nothing changed at this stack map.
162 }
163 BitMemoryRegion mask = dex_register_masks_.GetBitMemoryRegion(mask_index);
164 if (mask.size_in_bits() <= first_dex_register) {
165 continue; // Nothing changed after the first register we are interested in.
166 }
167
168 // The map stores one catalogue index per each modified register location.
169 uint32_t map_index = stack_map.GetDexRegisterMapIndex();
170 DCHECK_NE(map_index, StackMap::kNoValue);
171
172 // Skip initial registers which we are not interested in (to get to inlined registers).
173 map_index += mask.PopCount(0, first_dex_register);
174 mask = mask.Subregion(first_dex_register, mask.size_in_bits() - first_dex_register);
175
176 // Update registers that we see for first time (i.e. most recent value).
177 DexRegisterLocation* regs = map->data();
178 const uint32_t end = std::min<uint32_t>(map->size(), mask.size_in_bits());
179 const size_t kNumBits = BitSizeOf<uint32_t>();
180 for (uint32_t reg = 0; reg < end; reg += kNumBits) {
181 // Process the mask in chunks of kNumBits for performance.
182 uint32_t bits = mask.LoadBits(reg, std::min<uint32_t>(end - reg, kNumBits));
183 while (bits != 0) {
184 uint32_t bit = CTZ(bits);
185 if (regs[reg + bit].GetKind() == DexRegisterLocation::Kind::kInvalid) {
186 regs[reg + bit] = GetDexRegisterCatalogEntry(dex_register_maps_.Get(map_index));
187 remaining_registers--;
188 }
189 map_index++;
190 bits ^= 1u << bit; // Clear the bit.
191 }
192 }
193 }
194
195 // Set any remaining registers to None (which is the default state at first stack map).
196 if (remaining_registers != 0) {
197 DexRegisterLocation* regs = map->data();
198 for (uint32_t r = 0; r < map->size(); r++) {
199 if (regs[r].GetKind() == DexRegisterLocation::Kind::kInvalid) {
200 regs[r] = DexRegisterLocation::None();
201 }
202 }
203 }
204}
205
David Srbecky42deda82018-08-10 11:23:27 +0100206// Decode the CodeInfo while collecting size statistics.
207void CodeInfo::CollectSizeStats(const uint8_t* code_info_data, /*out*/ Stats* parent) {
208 Stats* codeinfo_stats = parent->Child("CodeInfo");
209 BitMemoryReader reader(code_info_data);
David Srbecky42deda82018-08-10 11:23:27 +0100210 CodeInfo code_info; // Temporary storage for decoded tables.
David Srbecky6c4ec5c2019-06-20 07:23:19 +0000211 std::array<uint32_t, kNumHeaders> header = reader.ReadInterleavedVarints<kNumHeaders>();
212 ForEachHeaderField([&code_info, &header](size_t i, auto member_pointer) {
213 code_info.*member_pointer = header[i];
David Srbecky697c47a2019-06-16 21:53:07 +0100214 });
215 codeinfo_stats->Child("Header")->AddBits(reader.NumberOfReadBits());
216 ForEachBitTableField([codeinfo_stats, &reader, &code_info](size_t i, auto member_pointer) {
David Srbecky42deda82018-08-10 11:23:27 +0100217 auto& table = code_info.*member_pointer;
218 size_t bit_offset = reader.NumberOfReadBits();
David Srbecky697c47a2019-06-16 21:53:07 +0100219 if (code_info.HasBitTable(i)) {
220 if (code_info.IsBitTableDeduped(i)) {
221 reader.ReadVarint();
222 codeinfo_stats->Child("DedupeOffset")->AddBits(reader.NumberOfReadBits() - bit_offset);
223 } else {
224 table.Decode(reader);
225 Stats* table_stats = codeinfo_stats->Child(table.GetName());
226 table_stats->AddBits(reader.NumberOfReadBits() - bit_offset);
227 const char* const* column_names = table.GetColumnNames();
228 for (size_t c = 0; c < table.NumColumns(); c++) {
229 if (table.NumColumnBits(c) > 0) {
230 Stats* column_stats = table_stats->Child(column_names[c]);
231 column_stats->AddBits(table.NumRows() * table.NumColumnBits(c), table.NumRows());
232 }
David Srbecky42deda82018-08-10 11:23:27 +0100233 }
234 }
David Srbecky86decb62018-06-05 06:41:10 +0100235 }
David Srbecky42deda82018-08-10 11:23:27 +0100236 });
237 codeinfo_stats->AddBytes(BitsToBytesRoundUp(reader.NumberOfReadBits()));
David Srbecky86decb62018-06-05 06:41:10 +0100238}
239
David Srbeckye1402122018-06-13 18:20:45 +0100240void DexRegisterMap::Dump(VariableIndentationOutputStream* vios) const {
241 if (HasAnyLiveDexRegisters()) {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100242 ScopedIndentation indent1(vios);
David Srbeckye1402122018-06-13 18:20:45 +0100243 for (size_t i = 0; i < size(); ++i) {
244 DexRegisterLocation reg = (*this)[i];
245 if (reg.IsLive()) {
246 vios->Stream() << "v" << i << ":" << reg << " ";
David Srbecky71ec1cc2018-05-18 15:57:25 +0100247 }
248 }
249 vios->Stream() << "\n";
250 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000251}
252
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100253void CodeInfo::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100254 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100255 bool verbose,
David Srbecky8cd54542018-07-15 23:58:44 +0100256 InstructionSet instruction_set) const {
David Srbecky42deda82018-08-10 11:23:27 +0100257 vios->Stream() << "CodeInfo BitSize=" << size_in_bits_
258 << " FrameSize:" << packed_frame_size_ * kStackAlignment
259 << " CoreSpillMask:" << std::hex << core_spill_mask_
260 << " FpSpillMask:" << std::hex << fp_spill_mask_
261 << " NumberOfDexRegisters:" << std::dec << number_of_dex_registers_
262 << "\n";
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100263 ScopedIndentation indent1(vios);
David Srbecky697c47a2019-06-16 21:53:07 +0100264 ForEachBitTableField([this, &vios, verbose](size_t, auto member_pointer) {
David Srbecky42deda82018-08-10 11:23:27 +0100265 const auto& table = this->*member_pointer;
266 if (table.NumRows() != 0) {
267 vios->Stream() << table.GetName() << " BitSize=" << table.DataBitSize();
268 vios->Stream() << " Rows=" << table.NumRows() << " Bits={";
269 const char* const* column_names = table.GetColumnNames();
270 for (size_t c = 0; c < table.NumColumns(); c++) {
271 vios->Stream() << (c != 0 ? " " : "");
272 vios->Stream() << column_names[c] << "=" << table.NumColumnBits(c);
273 }
274 vios->Stream() << "}\n";
275 if (verbose) {
276 ScopedIndentation indent1(vios);
277 for (size_t r = 0; r < table.NumRows(); r++) {
278 vios->Stream() << "[" << std::right << std::setw(3) << r << "]={";
279 for (size_t c = 0; c < table.NumColumns(); c++) {
280 vios->Stream() << (c != 0 ? " " : "");
281 if (&table == static_cast<const void*>(&stack_masks_) ||
282 &table == static_cast<const void*>(&dex_register_masks_)) {
283 BitMemoryRegion bits = table.GetBitMemoryRegion(r, c);
284 for (size_t b = 0, e = bits.size_in_bits(); b < e; b++) {
285 vios->Stream() << bits.LoadBit(e - b - 1);
286 }
287 } else {
288 vios->Stream() << std::right << std::setw(8) << static_cast<int32_t>(table.Get(r, c));
289 }
290 }
291 vios->Stream() << "}\n";
292 }
293 }
294 }
295 });
David Srbecky71ec1cc2018-05-18 15:57:25 +0100296
Roland Levillaina552e1c2015-03-26 15:01:03 +0000297 // Display stack maps along with (live) Dex register maps.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100298 if (verbose) {
David Srbecky93bd3612018-07-02 19:30:18 +0100299 for (StackMap stack_map : stack_maps_) {
David Srbecky8cd54542018-07-15 23:58:44 +0100300 stack_map.Dump(vios, *this, code_offset, instruction_set);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100301 }
302 }
303}
304
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100305void StackMap::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100306 const CodeInfo& code_info,
307 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100308 InstructionSet instruction_set) const {
David Srbecky052f8ca2018-04-26 15:42:54 +0100309 const uint32_t pc_offset = GetNativePcOffset(instruction_set);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100310 vios->Stream()
David Srbecky71ec1cc2018-05-18 15:57:25 +0100311 << "StackMap[" << Row() << "]"
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100312 << std::hex
David Srbecky71ec1cc2018-05-18 15:57:25 +0100313 << " (native_pc=0x" << code_offset + pc_offset
314 << ", dex_pc=0x" << GetDexPc()
David Srbecky052f8ca2018-04-26 15:42:54 +0100315 << ", register_mask=0x" << code_info.GetRegisterMaskOf(*this)
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100316 << std::dec
317 << ", stack_mask=0b";
David Srbecky052f8ca2018-04-26 15:42:54 +0100318 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(*this);
David Srbecky4b59d102018-05-29 21:46:10 +0000319 for (size_t i = 0, e = stack_mask.size_in_bits(); i < e; ++i) {
David Srbecky45aa5982016-03-18 02:15:09 +0000320 vios->Stream() << stack_mask.LoadBit(e - i - 1);
Roland Levillainf2650d12015-05-28 14:53:28 +0100321 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100322 vios->Stream() << ")\n";
David Srbeckye1402122018-06-13 18:20:45 +0100323 code_info.GetDexRegisterMapOf(*this).Dump(vios);
David Srbecky93bd3612018-07-02 19:30:18 +0100324 for (InlineInfo inline_info : code_info.GetInlineInfosOf(*this)) {
David Srbecky8cd54542018-07-15 23:58:44 +0100325 inline_info.Dump(vios, code_info, *this);
Nicolas Geoffray12bdb722015-06-17 09:44:43 +0100326 }
Roland Levillainf2650d12015-05-28 14:53:28 +0100327}
328
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100329void InlineInfo::Dump(VariableIndentationOutputStream* vios,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100330 const CodeInfo& code_info,
David Srbecky8cd54542018-07-15 23:58:44 +0100331 const StackMap& stack_map) const {
David Srbecky6e69e522018-06-03 12:00:14 +0100332 uint32_t depth = Row() - stack_map.GetInlineInfoIndex();
333 vios->Stream()
334 << "InlineInfo[" << Row() << "]"
335 << " (depth=" << depth
336 << std::hex
337 << ", dex_pc=0x" << GetDexPc();
338 if (EncodesArtMethod()) {
339 ScopedObjectAccess soa(Thread::Current());
340 vios->Stream() << ", method=" << GetArtMethod()->PrettyMethod();
341 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100342 vios->Stream()
David Srbecky6e69e522018-06-03 12:00:14 +0100343 << std::dec
David Srbecky8cd54542018-07-15 23:58:44 +0100344 << ", method_index=" << code_info.GetMethodIndexOf(*this);
David Srbecky6e69e522018-06-03 12:00:14 +0100345 }
346 vios->Stream() << ")\n";
David Srbecky93bd3612018-07-02 19:30:18 +0100347 code_info.GetInlineDexRegisterMapOf(stack_map, *this).Dump(vios);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000348}
349
350} // namespace art