blob: 2300d1fc0d584df3a7481f8969ec4c00f75f2cde [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 Srbecky697c47a2019-06-16 21:53:07 +010036 uint32_t header[kNumHeaders];
David Srbecky67ba8722019-05-23 15:32:18 +010037 reader.ReadVarints(header);
David Srbecky697c47a2019-06-16 21:53:07 +010038 ForEachHeaderField([this, &header](size_t i, auto member_pointer) {
39 this->*member_pointer = header[i];
40 });
41 ForEachBitTableField([this, &reader](size_t i, auto member_pointer) {
42 auto& table = this->*member_pointer;
43 if (HasBitTable(i)) {
44 if (UNLIKELY(IsBitTableDeduped(i))) {
45 ssize_t bit_offset = reader.NumberOfReadBits() - reader.ReadVarint();
46 BitMemoryReader reader2(reader.data(), bit_offset); // The offset is negative.
47 table.Decode(reader2);
48 } else {
49 table.Decode(reader);
50 }
51 }
David Srbecky42deda82018-08-10 11:23:27 +010052 }, flags);
David Srbeckyd1606412018-07-31 15:05:14 +010053 size_in_bits_ = reader.NumberOfReadBits();
David Srbeckye42a4b92019-05-26 00:10:25 +010054 if (flags == AllTables) {
55 DCHECK_EQ(HasInlineInfo(data), HasInlineInfo());
56 }
David Srbecky078d7ba2018-06-21 15:36:48 +010057}
58
David Srbecky42deda82018-08-10 11:23:27 +010059size_t CodeInfo::Deduper::Dedupe(const uint8_t* code_info_data) {
David Srbeckyd1606412018-07-31 15:05:14 +010060 writer_.ByteAlign();
61 size_t deduped_offset = writer_.NumberOfWrittenBits() / kBitsPerByte;
David Srbecky697c47a2019-06-16 21:53:07 +010062
63 // Read the existing code info and find (and keep) dedup-map iterator for each table.
64 // The iterator stores BitMemoryRegion and bit_offset of previous identical BitTable.
David Srbecky42deda82018-08-10 11:23:27 +010065 BitMemoryReader reader(code_info_data);
66 CodeInfo code_info; // Temporary storage for decoded data.
David Srbecky697c47a2019-06-16 21:53:07 +010067 ForEachHeaderField([&reader, &code_info](size_t, auto member_pointer) {
David Srbecky42deda82018-08-10 11:23:27 +010068 code_info.*member_pointer = reader.ReadVarint();
David Srbecky697c47a2019-06-16 21:53:07 +010069 });
70 std::map<BitMemoryRegion, uint32_t, BitMemoryRegion::Less>::iterator it[kNumBitTables];
71 ForEachBitTableField([this, &reader, &code_info, &it](size_t i, auto member_pointer) {
72 DCHECK(!code_info.IsBitTableDeduped(i));
73 if (code_info.HasBitTable(i)) {
74 size_t bit_table_start = reader.NumberOfReadBits();
75 (code_info.*member_pointer).Decode(reader);
76 BitMemoryRegion region = reader.GetReadRegion().Subregion(bit_table_start);
77 it[i] = dedupe_map_.emplace(region, /* default bit_offset */ 0).first;
78 if (it[i]->second != 0 && region.size_in_bits() > 32) { // Seen before and large?
79 code_info.SetBitTableDeduped(i); // Mark as deduped before we write header.
80 }
81 }
82 });
83
84 // Write the code info back, but replace deduped tables with relative offsets.
85 ForEachHeaderField([this, &code_info](size_t, auto member_pointer) {
David Srbecky42deda82018-08-10 11:23:27 +010086 writer_.WriteVarint(code_info.*member_pointer);
87 });
David Srbecky697c47a2019-06-16 21:53:07 +010088 ForEachBitTableField([this, &code_info, &it](size_t i, auto) {
89 if (code_info.HasBitTable(i)) {
90 uint32_t& bit_offset = it[i]->second;
91 if (code_info.IsBitTableDeduped(i)) {
92 DCHECK_NE(bit_offset, 0u);
93 writer_.WriteVarint(writer_.NumberOfWrittenBits() - bit_offset);
94 } else {
95 bit_offset = writer_.NumberOfWrittenBits(); // Store offset in dedup map.
96 writer_.WriteRegion(it[i]->first);
97 }
David Srbecky42deda82018-08-10 11:23:27 +010098 }
99 });
David Srbeckyd1606412018-07-31 15:05:14 +0100100
101 if (kIsDebugBuild) {
David Srbecky42deda82018-08-10 11:23:27 +0100102 CodeInfo old_code_info(code_info_data);
David Srbeckyd1606412018-07-31 15:05:14 +0100103 CodeInfo new_code_info(writer_.data() + deduped_offset);
David Srbecky697c47a2019-06-16 21:53:07 +0100104 ForEachHeaderField([&old_code_info, &new_code_info](size_t, auto member_pointer) {
105 if (member_pointer != &CodeInfo::bit_table_flags_) { // Expected to differ.
106 DCHECK_EQ(old_code_info.*member_pointer, new_code_info.*member_pointer);
107 }
David Srbecky42deda82018-08-10 11:23:27 +0100108 });
David Srbecky697c47a2019-06-16 21:53:07 +0100109 ForEachBitTableField([&old_code_info, &new_code_info](size_t, auto member_pointer) {
David Srbecky42deda82018-08-10 11:23:27 +0100110 DCHECK((old_code_info.*member_pointer).Equals(new_code_info.*member_pointer));
111 });
David Srbeckyd1606412018-07-31 15:05:14 +0100112 }
113
114 return deduped_offset;
David Srbeckyb73323c2018-07-15 23:58:44 +0100115}
116
David Srbecky0b4e5a32018-06-11 16:25:29 +0100117BitTable<StackMap>::const_iterator CodeInfo::BinarySearchNativePc(uint32_t packed_pc) const {
118 return std::partition_point(
119 stack_maps_.begin(),
120 stack_maps_.end(),
121 [packed_pc](const StackMap& sm) {
122 return sm.GetPackedNativePc() < packed_pc && sm.GetKind() != StackMap::Kind::Catch;
123 });
124}
125
126StackMap CodeInfo::GetStackMapForNativePcOffset(uint32_t pc, InstructionSet isa) const {
127 auto it = BinarySearchNativePc(StackMap::PackNativePc(pc, isa));
128 // Start at the lower bound and iterate over all stack maps with the given native pc.
129 for (; it != stack_maps_.end() && (*it).GetNativePcOffset(isa) == pc; ++it) {
130 StackMap::Kind kind = static_cast<StackMap::Kind>((*it).GetKind());
131 if (kind == StackMap::Kind::Default || kind == StackMap::Kind::OSR) {
132 return *it;
133 }
134 }
David Srbeckya45a85c2018-06-21 16:03:12 +0100135 return stack_maps_.GetInvalidRow();
David Srbecky0b4e5a32018-06-11 16:25:29 +0100136}
137
David Srbecky6de88332018-06-03 12:00:11 +0100138// Scan backward to determine dex register locations at given stack map.
139// All registers for a stack map are combined - inlined registers are just appended,
140// therefore 'first_dex_register' allows us to select a sub-range to decode.
141void CodeInfo::DecodeDexRegisterMap(uint32_t stack_map_index,
142 uint32_t first_dex_register,
143 /*out*/ DexRegisterMap* map) const {
144 // Count remaining work so we know when we have finished.
145 uint32_t remaining_registers = map->size();
146
147 // Keep scanning backwards and collect the most recent location of each register.
148 for (int32_t s = stack_map_index; s >= 0 && remaining_registers != 0; s--) {
149 StackMap stack_map = GetStackMapAt(s);
150 DCHECK_LE(stack_map_index - s, kMaxDexRegisterMapSearchDistance) << "Unbounded search";
151
152 // The mask specifies which registers where modified in this stack map.
153 // NB: the mask can be shorter than expected if trailing zero bits were removed.
154 uint32_t mask_index = stack_map.GetDexRegisterMaskIndex();
155 if (mask_index == StackMap::kNoValue) {
156 continue; // Nothing changed at this stack map.
157 }
158 BitMemoryRegion mask = dex_register_masks_.GetBitMemoryRegion(mask_index);
159 if (mask.size_in_bits() <= first_dex_register) {
160 continue; // Nothing changed after the first register we are interested in.
161 }
162
163 // The map stores one catalogue index per each modified register location.
164 uint32_t map_index = stack_map.GetDexRegisterMapIndex();
165 DCHECK_NE(map_index, StackMap::kNoValue);
166
167 // Skip initial registers which we are not interested in (to get to inlined registers).
168 map_index += mask.PopCount(0, first_dex_register);
169 mask = mask.Subregion(first_dex_register, mask.size_in_bits() - first_dex_register);
170
171 // Update registers that we see for first time (i.e. most recent value).
172 DexRegisterLocation* regs = map->data();
173 const uint32_t end = std::min<uint32_t>(map->size(), mask.size_in_bits());
174 const size_t kNumBits = BitSizeOf<uint32_t>();
175 for (uint32_t reg = 0; reg < end; reg += kNumBits) {
176 // Process the mask in chunks of kNumBits for performance.
177 uint32_t bits = mask.LoadBits(reg, std::min<uint32_t>(end - reg, kNumBits));
178 while (bits != 0) {
179 uint32_t bit = CTZ(bits);
180 if (regs[reg + bit].GetKind() == DexRegisterLocation::Kind::kInvalid) {
181 regs[reg + bit] = GetDexRegisterCatalogEntry(dex_register_maps_.Get(map_index));
182 remaining_registers--;
183 }
184 map_index++;
185 bits ^= 1u << bit; // Clear the bit.
186 }
187 }
188 }
189
190 // Set any remaining registers to None (which is the default state at first stack map).
191 if (remaining_registers != 0) {
192 DexRegisterLocation* regs = map->data();
193 for (uint32_t r = 0; r < map->size(); r++) {
194 if (regs[r].GetKind() == DexRegisterLocation::Kind::kInvalid) {
195 regs[r] = DexRegisterLocation::None();
196 }
197 }
198 }
199}
200
David Srbecky42deda82018-08-10 11:23:27 +0100201// Decode the CodeInfo while collecting size statistics.
202void CodeInfo::CollectSizeStats(const uint8_t* code_info_data, /*out*/ Stats* parent) {
203 Stats* codeinfo_stats = parent->Child("CodeInfo");
204 BitMemoryReader reader(code_info_data);
David Srbecky42deda82018-08-10 11:23:27 +0100205 CodeInfo code_info; // Temporary storage for decoded tables.
David Srbecky697c47a2019-06-16 21:53:07 +0100206 ForEachHeaderField([&reader, &code_info](size_t, auto member_pointer) {
207 code_info.*member_pointer = reader.ReadVarint();
208 });
209 codeinfo_stats->Child("Header")->AddBits(reader.NumberOfReadBits());
210 ForEachBitTableField([codeinfo_stats, &reader, &code_info](size_t i, auto member_pointer) {
David Srbecky42deda82018-08-10 11:23:27 +0100211 auto& table = code_info.*member_pointer;
212 size_t bit_offset = reader.NumberOfReadBits();
David Srbecky697c47a2019-06-16 21:53:07 +0100213 if (code_info.HasBitTable(i)) {
214 if (code_info.IsBitTableDeduped(i)) {
215 reader.ReadVarint();
216 codeinfo_stats->Child("DedupeOffset")->AddBits(reader.NumberOfReadBits() - bit_offset);
217 } else {
218 table.Decode(reader);
219 Stats* table_stats = codeinfo_stats->Child(table.GetName());
220 table_stats->AddBits(reader.NumberOfReadBits() - bit_offset);
221 const char* const* column_names = table.GetColumnNames();
222 for (size_t c = 0; c < table.NumColumns(); c++) {
223 if (table.NumColumnBits(c) > 0) {
224 Stats* column_stats = table_stats->Child(column_names[c]);
225 column_stats->AddBits(table.NumRows() * table.NumColumnBits(c), table.NumRows());
226 }
David Srbecky42deda82018-08-10 11:23:27 +0100227 }
228 }
David Srbecky86decb62018-06-05 06:41:10 +0100229 }
David Srbecky42deda82018-08-10 11:23:27 +0100230 });
231 codeinfo_stats->AddBytes(BitsToBytesRoundUp(reader.NumberOfReadBits()));
David Srbecky86decb62018-06-05 06:41:10 +0100232}
233
David Srbeckye1402122018-06-13 18:20:45 +0100234void DexRegisterMap::Dump(VariableIndentationOutputStream* vios) const {
235 if (HasAnyLiveDexRegisters()) {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100236 ScopedIndentation indent1(vios);
David Srbeckye1402122018-06-13 18:20:45 +0100237 for (size_t i = 0; i < size(); ++i) {
238 DexRegisterLocation reg = (*this)[i];
239 if (reg.IsLive()) {
240 vios->Stream() << "v" << i << ":" << reg << " ";
David Srbecky71ec1cc2018-05-18 15:57:25 +0100241 }
242 }
243 vios->Stream() << "\n";
244 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000245}
246
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100247void CodeInfo::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100248 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100249 bool verbose,
David Srbecky8cd54542018-07-15 23:58:44 +0100250 InstructionSet instruction_set) const {
David Srbecky42deda82018-08-10 11:23:27 +0100251 vios->Stream() << "CodeInfo BitSize=" << size_in_bits_
252 << " FrameSize:" << packed_frame_size_ * kStackAlignment
253 << " CoreSpillMask:" << std::hex << core_spill_mask_
254 << " FpSpillMask:" << std::hex << fp_spill_mask_
255 << " NumberOfDexRegisters:" << std::dec << number_of_dex_registers_
256 << "\n";
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100257 ScopedIndentation indent1(vios);
David Srbecky697c47a2019-06-16 21:53:07 +0100258 ForEachBitTableField([this, &vios, verbose](size_t, auto member_pointer) {
David Srbecky42deda82018-08-10 11:23:27 +0100259 const auto& table = this->*member_pointer;
260 if (table.NumRows() != 0) {
261 vios->Stream() << table.GetName() << " BitSize=" << table.DataBitSize();
262 vios->Stream() << " Rows=" << table.NumRows() << " Bits={";
263 const char* const* column_names = table.GetColumnNames();
264 for (size_t c = 0; c < table.NumColumns(); c++) {
265 vios->Stream() << (c != 0 ? " " : "");
266 vios->Stream() << column_names[c] << "=" << table.NumColumnBits(c);
267 }
268 vios->Stream() << "}\n";
269 if (verbose) {
270 ScopedIndentation indent1(vios);
271 for (size_t r = 0; r < table.NumRows(); r++) {
272 vios->Stream() << "[" << std::right << std::setw(3) << r << "]={";
273 for (size_t c = 0; c < table.NumColumns(); c++) {
274 vios->Stream() << (c != 0 ? " " : "");
275 if (&table == static_cast<const void*>(&stack_masks_) ||
276 &table == static_cast<const void*>(&dex_register_masks_)) {
277 BitMemoryRegion bits = table.GetBitMemoryRegion(r, c);
278 for (size_t b = 0, e = bits.size_in_bits(); b < e; b++) {
279 vios->Stream() << bits.LoadBit(e - b - 1);
280 }
281 } else {
282 vios->Stream() << std::right << std::setw(8) << static_cast<int32_t>(table.Get(r, c));
283 }
284 }
285 vios->Stream() << "}\n";
286 }
287 }
288 }
289 });
David Srbecky71ec1cc2018-05-18 15:57:25 +0100290
Roland Levillaina552e1c2015-03-26 15:01:03 +0000291 // Display stack maps along with (live) Dex register maps.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100292 if (verbose) {
David Srbecky93bd3612018-07-02 19:30:18 +0100293 for (StackMap stack_map : stack_maps_) {
David Srbecky8cd54542018-07-15 23:58:44 +0100294 stack_map.Dump(vios, *this, code_offset, instruction_set);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100295 }
296 }
297}
298
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100299void StackMap::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100300 const CodeInfo& code_info,
301 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100302 InstructionSet instruction_set) const {
David Srbecky052f8ca2018-04-26 15:42:54 +0100303 const uint32_t pc_offset = GetNativePcOffset(instruction_set);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100304 vios->Stream()
David Srbecky71ec1cc2018-05-18 15:57:25 +0100305 << "StackMap[" << Row() << "]"
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100306 << std::hex
David Srbecky71ec1cc2018-05-18 15:57:25 +0100307 << " (native_pc=0x" << code_offset + pc_offset
308 << ", dex_pc=0x" << GetDexPc()
David Srbecky052f8ca2018-04-26 15:42:54 +0100309 << ", register_mask=0x" << code_info.GetRegisterMaskOf(*this)
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100310 << std::dec
311 << ", stack_mask=0b";
David Srbecky052f8ca2018-04-26 15:42:54 +0100312 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(*this);
David Srbecky4b59d102018-05-29 21:46:10 +0000313 for (size_t i = 0, e = stack_mask.size_in_bits(); i < e; ++i) {
David Srbecky45aa5982016-03-18 02:15:09 +0000314 vios->Stream() << stack_mask.LoadBit(e - i - 1);
Roland Levillainf2650d12015-05-28 14:53:28 +0100315 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100316 vios->Stream() << ")\n";
David Srbeckye1402122018-06-13 18:20:45 +0100317 code_info.GetDexRegisterMapOf(*this).Dump(vios);
David Srbecky93bd3612018-07-02 19:30:18 +0100318 for (InlineInfo inline_info : code_info.GetInlineInfosOf(*this)) {
David Srbecky8cd54542018-07-15 23:58:44 +0100319 inline_info.Dump(vios, code_info, *this);
Nicolas Geoffray12bdb722015-06-17 09:44:43 +0100320 }
Roland Levillainf2650d12015-05-28 14:53:28 +0100321}
322
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100323void InlineInfo::Dump(VariableIndentationOutputStream* vios,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100324 const CodeInfo& code_info,
David Srbecky8cd54542018-07-15 23:58:44 +0100325 const StackMap& stack_map) const {
David Srbecky6e69e522018-06-03 12:00:14 +0100326 uint32_t depth = Row() - stack_map.GetInlineInfoIndex();
327 vios->Stream()
328 << "InlineInfo[" << Row() << "]"
329 << " (depth=" << depth
330 << std::hex
331 << ", dex_pc=0x" << GetDexPc();
332 if (EncodesArtMethod()) {
333 ScopedObjectAccess soa(Thread::Current());
334 vios->Stream() << ", method=" << GetArtMethod()->PrettyMethod();
335 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100336 vios->Stream()
David Srbecky6e69e522018-06-03 12:00:14 +0100337 << std::dec
David Srbecky8cd54542018-07-15 23:58:44 +0100338 << ", method_index=" << code_info.GetMethodIndexOf(*this);
David Srbecky6e69e522018-06-03 12:00:14 +0100339 }
340 vios->Stream() << ")\n";
David Srbecky93bd3612018-07-02 19:30:18 +0100341 code_info.GetInlineDexRegisterMapOf(stack_map, *this).Dump(vios);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000342}
343
344} // namespace art