blob: 6585a3b4ee10c5ab536bf26da0f73b9fe6f11513 [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 Srbecky42deda82018-08-10 11:23:27 +010034// Returns true if the decoded table was deduped.
David Srbeckyb73323c2018-07-15 23:58:44 +010035template<typename Accessor>
David Srbecky42deda82018-08-10 11:23:27 +010036ALWAYS_INLINE static bool DecodeTable(BitTable<Accessor>& table, BitMemoryReader& reader) {
37 bool is_deduped = reader.ReadBit();
David Srbecky67ba8722019-05-23 15:32:18 +010038 if (UNLIKELY(is_deduped)) {
David Srbecky0c3aa312018-08-03 14:52:32 +010039 ssize_t bit_offset = reader.NumberOfReadBits() - reader.ReadVarint();
David Srbecky42deda82018-08-10 11:23:27 +010040 BitMemoryReader reader2(reader.data(), bit_offset); // The offset is negative.
David Srbeckyb73323c2018-07-15 23:58:44 +010041 table.Decode(reader2);
42 } else {
43 table.Decode(reader);
44 }
David Srbecky42deda82018-08-10 11:23:27 +010045 return is_deduped;
David Srbeckyb73323c2018-07-15 23:58:44 +010046}
47
David Srbecky6ee06e92018-07-25 21:45:54 +010048void CodeInfo::Decode(const uint8_t* data, DecodeFlags flags) {
David Srbecky3aaaa212018-07-30 16:46:53 +010049 BitMemoryReader reader(data);
David Srbecky67ba8722019-05-23 15:32:18 +010050 uint32_t header[4];
51 reader.ReadVarints(header);
52 packed_frame_size_ = header[0];
53 core_spill_mask_ = header[1];
54 fp_spill_mask_ = header[2];
55 number_of_dex_registers_ = header[3];
David Srbecky42deda82018-08-10 11:23:27 +010056 ForEachBitTableField([this, &reader](auto member_pointer) {
57 DecodeTable(this->*member_pointer, reader);
58 }, flags);
David Srbeckyd1606412018-07-31 15:05:14 +010059 size_in_bits_ = reader.NumberOfReadBits();
David Srbecky078d7ba2018-06-21 15:36:48 +010060}
61
David Srbecky42deda82018-08-10 11:23:27 +010062size_t CodeInfo::Deduper::Dedupe(const uint8_t* code_info_data) {
David Srbeckyd1606412018-07-31 15:05:14 +010063 writer_.ByteAlign();
64 size_t deduped_offset = writer_.NumberOfWrittenBits() / kBitsPerByte;
David Srbecky42deda82018-08-10 11:23:27 +010065 BitMemoryReader reader(code_info_data);
66 CodeInfo code_info; // Temporary storage for decoded data.
67 ForEachHeaderField([this, &reader, &code_info](auto member_pointer) {
68 code_info.*member_pointer = reader.ReadVarint();
69 writer_.WriteVarint(code_info.*member_pointer);
70 });
71 ForEachBitTableField([this, &reader, &code_info](auto member_pointer) {
72 bool is_deduped = reader.ReadBit();
73 DCHECK(!is_deduped);
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 auto it = dedupe_map_.insert(std::make_pair(region, /* placeholder */ 0));
78 if (it.second /* new bit table */ || region.size_in_bits() < 32) {
79 writer_.WriteBit(false); // Is not deduped.
80 it.first->second = writer_.NumberOfWrittenBits();
81 writer_.WriteRegion(region);
82 } else {
83 writer_.WriteBit(true); // Is deduped.
84 size_t bit_offset = writer_.NumberOfWrittenBits();
85 writer_.WriteVarint(bit_offset - it.first->second);
86 }
87 });
David Srbeckyd1606412018-07-31 15:05:14 +010088
89 if (kIsDebugBuild) {
David Srbecky42deda82018-08-10 11:23:27 +010090 CodeInfo old_code_info(code_info_data);
David Srbeckyd1606412018-07-31 15:05:14 +010091 CodeInfo new_code_info(writer_.data() + deduped_offset);
David Srbecky42deda82018-08-10 11:23:27 +010092 ForEachHeaderField([&old_code_info, &new_code_info](auto member_pointer) {
93 DCHECK_EQ(old_code_info.*member_pointer, new_code_info.*member_pointer);
94 });
95 ForEachBitTableField([&old_code_info, &new_code_info](auto member_pointer) {
96 DCHECK((old_code_info.*member_pointer).Equals(new_code_info.*member_pointer));
97 });
David Srbeckyd1606412018-07-31 15:05:14 +010098 }
99
100 return deduped_offset;
David Srbeckyb73323c2018-07-15 23:58:44 +0100101}
102
David Srbecky0b4e5a32018-06-11 16:25:29 +0100103BitTable<StackMap>::const_iterator CodeInfo::BinarySearchNativePc(uint32_t packed_pc) const {
104 return std::partition_point(
105 stack_maps_.begin(),
106 stack_maps_.end(),
107 [packed_pc](const StackMap& sm) {
108 return sm.GetPackedNativePc() < packed_pc && sm.GetKind() != StackMap::Kind::Catch;
109 });
110}
111
112StackMap CodeInfo::GetStackMapForNativePcOffset(uint32_t pc, InstructionSet isa) const {
113 auto it = BinarySearchNativePc(StackMap::PackNativePc(pc, isa));
114 // Start at the lower bound and iterate over all stack maps with the given native pc.
115 for (; it != stack_maps_.end() && (*it).GetNativePcOffset(isa) == pc; ++it) {
116 StackMap::Kind kind = static_cast<StackMap::Kind>((*it).GetKind());
117 if (kind == StackMap::Kind::Default || kind == StackMap::Kind::OSR) {
118 return *it;
119 }
120 }
David Srbeckya45a85c2018-06-21 16:03:12 +0100121 return stack_maps_.GetInvalidRow();
David Srbecky0b4e5a32018-06-11 16:25:29 +0100122}
123
David Srbecky6de88332018-06-03 12:00:11 +0100124// Scan backward to determine dex register locations at given stack map.
125// All registers for a stack map are combined - inlined registers are just appended,
126// therefore 'first_dex_register' allows us to select a sub-range to decode.
127void CodeInfo::DecodeDexRegisterMap(uint32_t stack_map_index,
128 uint32_t first_dex_register,
129 /*out*/ DexRegisterMap* map) const {
130 // Count remaining work so we know when we have finished.
131 uint32_t remaining_registers = map->size();
132
133 // Keep scanning backwards and collect the most recent location of each register.
134 for (int32_t s = stack_map_index; s >= 0 && remaining_registers != 0; s--) {
135 StackMap stack_map = GetStackMapAt(s);
136 DCHECK_LE(stack_map_index - s, kMaxDexRegisterMapSearchDistance) << "Unbounded search";
137
138 // The mask specifies which registers where modified in this stack map.
139 // NB: the mask can be shorter than expected if trailing zero bits were removed.
140 uint32_t mask_index = stack_map.GetDexRegisterMaskIndex();
141 if (mask_index == StackMap::kNoValue) {
142 continue; // Nothing changed at this stack map.
143 }
144 BitMemoryRegion mask = dex_register_masks_.GetBitMemoryRegion(mask_index);
145 if (mask.size_in_bits() <= first_dex_register) {
146 continue; // Nothing changed after the first register we are interested in.
147 }
148
149 // The map stores one catalogue index per each modified register location.
150 uint32_t map_index = stack_map.GetDexRegisterMapIndex();
151 DCHECK_NE(map_index, StackMap::kNoValue);
152
153 // Skip initial registers which we are not interested in (to get to inlined registers).
154 map_index += mask.PopCount(0, first_dex_register);
155 mask = mask.Subregion(first_dex_register, mask.size_in_bits() - first_dex_register);
156
157 // Update registers that we see for first time (i.e. most recent value).
158 DexRegisterLocation* regs = map->data();
159 const uint32_t end = std::min<uint32_t>(map->size(), mask.size_in_bits());
160 const size_t kNumBits = BitSizeOf<uint32_t>();
161 for (uint32_t reg = 0; reg < end; reg += kNumBits) {
162 // Process the mask in chunks of kNumBits for performance.
163 uint32_t bits = mask.LoadBits(reg, std::min<uint32_t>(end - reg, kNumBits));
164 while (bits != 0) {
165 uint32_t bit = CTZ(bits);
166 if (regs[reg + bit].GetKind() == DexRegisterLocation::Kind::kInvalid) {
167 regs[reg + bit] = GetDexRegisterCatalogEntry(dex_register_maps_.Get(map_index));
168 remaining_registers--;
169 }
170 map_index++;
171 bits ^= 1u << bit; // Clear the bit.
172 }
173 }
174 }
175
176 // Set any remaining registers to None (which is the default state at first stack map).
177 if (remaining_registers != 0) {
178 DexRegisterLocation* regs = map->data();
179 for (uint32_t r = 0; r < map->size(); r++) {
180 if (regs[r].GetKind() == DexRegisterLocation::Kind::kInvalid) {
181 regs[r] = DexRegisterLocation::None();
182 }
183 }
184 }
185}
186
David Srbecky42deda82018-08-10 11:23:27 +0100187// Decode the CodeInfo while collecting size statistics.
188void CodeInfo::CollectSizeStats(const uint8_t* code_info_data, /*out*/ Stats* parent) {
189 Stats* codeinfo_stats = parent->Child("CodeInfo");
190 BitMemoryReader reader(code_info_data);
191 ForEachHeaderField([&reader](auto) { reader.ReadVarint(); });
192 codeinfo_stats->Child("Header")->AddBits(reader.NumberOfReadBits());
193 CodeInfo code_info; // Temporary storage for decoded tables.
194 ForEachBitTableField([codeinfo_stats, &reader, &code_info](auto member_pointer) {
195 auto& table = code_info.*member_pointer;
196 size_t bit_offset = reader.NumberOfReadBits();
197 bool deduped = DecodeTable(table, reader);
198 if (deduped) {
199 codeinfo_stats->Child("DedupeOffset")->AddBits(reader.NumberOfReadBits() - bit_offset);
200 } else {
201 Stats* table_stats = codeinfo_stats->Child(table.GetName());
202 table_stats->AddBits(reader.NumberOfReadBits() - bit_offset);
203 const char* const* column_names = table.GetColumnNames();
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 }
David Srbecky86decb62018-06-05 06:41:10 +0100210 }
David Srbecky42deda82018-08-10 11:23:27 +0100211 });
212 codeinfo_stats->AddBytes(BitsToBytesRoundUp(reader.NumberOfReadBits()));
David Srbecky86decb62018-06-05 06:41:10 +0100213}
214
David Srbeckye1402122018-06-13 18:20:45 +0100215void DexRegisterMap::Dump(VariableIndentationOutputStream* vios) const {
216 if (HasAnyLiveDexRegisters()) {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100217 ScopedIndentation indent1(vios);
David Srbeckye1402122018-06-13 18:20:45 +0100218 for (size_t i = 0; i < size(); ++i) {
219 DexRegisterLocation reg = (*this)[i];
220 if (reg.IsLive()) {
221 vios->Stream() << "v" << i << ":" << reg << " ";
David Srbecky71ec1cc2018-05-18 15:57:25 +0100222 }
223 }
224 vios->Stream() << "\n";
225 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000226}
227
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100228void CodeInfo::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100229 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100230 bool verbose,
David Srbecky8cd54542018-07-15 23:58:44 +0100231 InstructionSet instruction_set) const {
David Srbecky42deda82018-08-10 11:23:27 +0100232 vios->Stream() << "CodeInfo BitSize=" << size_in_bits_
233 << " FrameSize:" << packed_frame_size_ * kStackAlignment
234 << " CoreSpillMask:" << std::hex << core_spill_mask_
235 << " FpSpillMask:" << std::hex << fp_spill_mask_
236 << " NumberOfDexRegisters:" << std::dec << number_of_dex_registers_
237 << "\n";
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100238 ScopedIndentation indent1(vios);
David Srbecky42deda82018-08-10 11:23:27 +0100239 ForEachBitTableField([this, &vios, verbose](auto member_pointer) {
240 const auto& table = this->*member_pointer;
241 if (table.NumRows() != 0) {
242 vios->Stream() << table.GetName() << " BitSize=" << table.DataBitSize();
243 vios->Stream() << " Rows=" << table.NumRows() << " Bits={";
244 const char* const* column_names = table.GetColumnNames();
245 for (size_t c = 0; c < table.NumColumns(); c++) {
246 vios->Stream() << (c != 0 ? " " : "");
247 vios->Stream() << column_names[c] << "=" << table.NumColumnBits(c);
248 }
249 vios->Stream() << "}\n";
250 if (verbose) {
251 ScopedIndentation indent1(vios);
252 for (size_t r = 0; r < table.NumRows(); r++) {
253 vios->Stream() << "[" << std::right << std::setw(3) << r << "]={";
254 for (size_t c = 0; c < table.NumColumns(); c++) {
255 vios->Stream() << (c != 0 ? " " : "");
256 if (&table == static_cast<const void*>(&stack_masks_) ||
257 &table == static_cast<const void*>(&dex_register_masks_)) {
258 BitMemoryRegion bits = table.GetBitMemoryRegion(r, c);
259 for (size_t b = 0, e = bits.size_in_bits(); b < e; b++) {
260 vios->Stream() << bits.LoadBit(e - b - 1);
261 }
262 } else {
263 vios->Stream() << std::right << std::setw(8) << static_cast<int32_t>(table.Get(r, c));
264 }
265 }
266 vios->Stream() << "}\n";
267 }
268 }
269 }
270 });
David Srbecky71ec1cc2018-05-18 15:57:25 +0100271
Roland Levillaina552e1c2015-03-26 15:01:03 +0000272 // Display stack maps along with (live) Dex register maps.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100273 if (verbose) {
David Srbecky93bd3612018-07-02 19:30:18 +0100274 for (StackMap stack_map : stack_maps_) {
David Srbecky8cd54542018-07-15 23:58:44 +0100275 stack_map.Dump(vios, *this, code_offset, instruction_set);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100276 }
277 }
278}
279
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100280void StackMap::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100281 const CodeInfo& code_info,
282 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100283 InstructionSet instruction_set) const {
David Srbecky052f8ca2018-04-26 15:42:54 +0100284 const uint32_t pc_offset = GetNativePcOffset(instruction_set);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100285 vios->Stream()
David Srbecky71ec1cc2018-05-18 15:57:25 +0100286 << "StackMap[" << Row() << "]"
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100287 << std::hex
David Srbecky71ec1cc2018-05-18 15:57:25 +0100288 << " (native_pc=0x" << code_offset + pc_offset
289 << ", dex_pc=0x" << GetDexPc()
David Srbecky052f8ca2018-04-26 15:42:54 +0100290 << ", register_mask=0x" << code_info.GetRegisterMaskOf(*this)
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100291 << std::dec
292 << ", stack_mask=0b";
David Srbecky052f8ca2018-04-26 15:42:54 +0100293 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(*this);
David Srbecky4b59d102018-05-29 21:46:10 +0000294 for (size_t i = 0, e = stack_mask.size_in_bits(); i < e; ++i) {
David Srbecky45aa5982016-03-18 02:15:09 +0000295 vios->Stream() << stack_mask.LoadBit(e - i - 1);
Roland Levillainf2650d12015-05-28 14:53:28 +0100296 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100297 vios->Stream() << ")\n";
David Srbeckye1402122018-06-13 18:20:45 +0100298 code_info.GetDexRegisterMapOf(*this).Dump(vios);
David Srbecky93bd3612018-07-02 19:30:18 +0100299 for (InlineInfo inline_info : code_info.GetInlineInfosOf(*this)) {
David Srbecky8cd54542018-07-15 23:58:44 +0100300 inline_info.Dump(vios, code_info, *this);
Nicolas Geoffray12bdb722015-06-17 09:44:43 +0100301 }
Roland Levillainf2650d12015-05-28 14:53:28 +0100302}
303
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100304void InlineInfo::Dump(VariableIndentationOutputStream* vios,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100305 const CodeInfo& code_info,
David Srbecky8cd54542018-07-15 23:58:44 +0100306 const StackMap& stack_map) const {
David Srbecky6e69e522018-06-03 12:00:14 +0100307 uint32_t depth = Row() - stack_map.GetInlineInfoIndex();
308 vios->Stream()
309 << "InlineInfo[" << Row() << "]"
310 << " (depth=" << depth
311 << std::hex
312 << ", dex_pc=0x" << GetDexPc();
313 if (EncodesArtMethod()) {
314 ScopedObjectAccess soa(Thread::Current());
315 vios->Stream() << ", method=" << GetArtMethod()->PrettyMethod();
316 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100317 vios->Stream()
David Srbecky6e69e522018-06-03 12:00:14 +0100318 << std::dec
David Srbecky8cd54542018-07-15 23:58:44 +0100319 << ", method_index=" << code_info.GetMethodIndexOf(*this);
David Srbecky6e69e522018-06-03 12:00:14 +0100320 }
321 vios->Stream() << ")\n";
David Srbecky93bd3612018-07-02 19:30:18 +0100322 code_info.GetInlineDexRegisterMapOf(stack_map, *this).Dump(vios);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000323}
324
325} // namespace art