blob: eef737862da3dddb249f88e2e0f0a695f75f0851 [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 Srbeckye42a4b92019-05-26 00:10:25 +010050 uint32_t header[5];
David Srbecky67ba8722019-05-23 15:32:18 +010051 reader.ReadVarints(header);
David Srbeckye42a4b92019-05-26 00:10:25 +010052 flags_ = header[0];
53 packed_frame_size_ = header[1];
54 core_spill_mask_ = header[2];
55 fp_spill_mask_ = header[3];
56 number_of_dex_registers_ = header[4];
David Srbecky42deda82018-08-10 11:23:27 +010057 ForEachBitTableField([this, &reader](auto member_pointer) {
58 DecodeTable(this->*member_pointer, reader);
59 }, flags);
David Srbeckyd1606412018-07-31 15:05:14 +010060 size_in_bits_ = reader.NumberOfReadBits();
David Srbeckye42a4b92019-05-26 00:10:25 +010061 if (flags == AllTables) {
62 DCHECK_EQ(HasInlineInfo(data), HasInlineInfo());
63 }
David Srbecky078d7ba2018-06-21 15:36:48 +010064}
65
David Srbecky42deda82018-08-10 11:23:27 +010066size_t CodeInfo::Deduper::Dedupe(const uint8_t* code_info_data) {
David Srbeckyd1606412018-07-31 15:05:14 +010067 writer_.ByteAlign();
68 size_t deduped_offset = writer_.NumberOfWrittenBits() / kBitsPerByte;
David Srbecky42deda82018-08-10 11:23:27 +010069 BitMemoryReader reader(code_info_data);
70 CodeInfo code_info; // Temporary storage for decoded data.
71 ForEachHeaderField([this, &reader, &code_info](auto member_pointer) {
72 code_info.*member_pointer = reader.ReadVarint();
73 writer_.WriteVarint(code_info.*member_pointer);
74 });
75 ForEachBitTableField([this, &reader, &code_info](auto member_pointer) {
76 bool is_deduped = reader.ReadBit();
77 DCHECK(!is_deduped);
78 size_t bit_table_start = reader.NumberOfReadBits();
79 (code_info.*member_pointer).Decode(reader);
80 BitMemoryRegion region = reader.GetReadRegion().Subregion(bit_table_start);
81 auto it = dedupe_map_.insert(std::make_pair(region, /* placeholder */ 0));
82 if (it.second /* new bit table */ || region.size_in_bits() < 32) {
83 writer_.WriteBit(false); // Is not deduped.
84 it.first->second = writer_.NumberOfWrittenBits();
85 writer_.WriteRegion(region);
86 } else {
87 writer_.WriteBit(true); // Is deduped.
88 size_t bit_offset = writer_.NumberOfWrittenBits();
89 writer_.WriteVarint(bit_offset - it.first->second);
90 }
91 });
David Srbeckyd1606412018-07-31 15:05:14 +010092
93 if (kIsDebugBuild) {
David Srbecky42deda82018-08-10 11:23:27 +010094 CodeInfo old_code_info(code_info_data);
David Srbeckyd1606412018-07-31 15:05:14 +010095 CodeInfo new_code_info(writer_.data() + deduped_offset);
David Srbecky42deda82018-08-10 11:23:27 +010096 ForEachHeaderField([&old_code_info, &new_code_info](auto member_pointer) {
97 DCHECK_EQ(old_code_info.*member_pointer, new_code_info.*member_pointer);
98 });
99 ForEachBitTableField([&old_code_info, &new_code_info](auto member_pointer) {
100 DCHECK((old_code_info.*member_pointer).Equals(new_code_info.*member_pointer));
101 });
David Srbeckyd1606412018-07-31 15:05:14 +0100102 }
103
104 return deduped_offset;
David Srbeckyb73323c2018-07-15 23:58:44 +0100105}
106
David Srbecky0b4e5a32018-06-11 16:25:29 +0100107BitTable<StackMap>::const_iterator CodeInfo::BinarySearchNativePc(uint32_t packed_pc) const {
108 return std::partition_point(
109 stack_maps_.begin(),
110 stack_maps_.end(),
111 [packed_pc](const StackMap& sm) {
112 return sm.GetPackedNativePc() < packed_pc && sm.GetKind() != StackMap::Kind::Catch;
113 });
114}
115
116StackMap CodeInfo::GetStackMapForNativePcOffset(uint32_t pc, InstructionSet isa) const {
117 auto it = BinarySearchNativePc(StackMap::PackNativePc(pc, isa));
118 // Start at the lower bound and iterate over all stack maps with the given native pc.
119 for (; it != stack_maps_.end() && (*it).GetNativePcOffset(isa) == pc; ++it) {
120 StackMap::Kind kind = static_cast<StackMap::Kind>((*it).GetKind());
121 if (kind == StackMap::Kind::Default || kind == StackMap::Kind::OSR) {
122 return *it;
123 }
124 }
David Srbeckya45a85c2018-06-21 16:03:12 +0100125 return stack_maps_.GetInvalidRow();
David Srbecky0b4e5a32018-06-11 16:25:29 +0100126}
127
David Srbecky6de88332018-06-03 12:00:11 +0100128// Scan backward to determine dex register locations at given stack map.
129// All registers for a stack map are combined - inlined registers are just appended,
130// therefore 'first_dex_register' allows us to select a sub-range to decode.
131void CodeInfo::DecodeDexRegisterMap(uint32_t stack_map_index,
132 uint32_t first_dex_register,
133 /*out*/ DexRegisterMap* map) const {
134 // Count remaining work so we know when we have finished.
135 uint32_t remaining_registers = map->size();
136
137 // Keep scanning backwards and collect the most recent location of each register.
138 for (int32_t s = stack_map_index; s >= 0 && remaining_registers != 0; s--) {
139 StackMap stack_map = GetStackMapAt(s);
140 DCHECK_LE(stack_map_index - s, kMaxDexRegisterMapSearchDistance) << "Unbounded search";
141
142 // The mask specifies which registers where modified in this stack map.
143 // NB: the mask can be shorter than expected if trailing zero bits were removed.
144 uint32_t mask_index = stack_map.GetDexRegisterMaskIndex();
145 if (mask_index == StackMap::kNoValue) {
146 continue; // Nothing changed at this stack map.
147 }
148 BitMemoryRegion mask = dex_register_masks_.GetBitMemoryRegion(mask_index);
149 if (mask.size_in_bits() <= first_dex_register) {
150 continue; // Nothing changed after the first register we are interested in.
151 }
152
153 // The map stores one catalogue index per each modified register location.
154 uint32_t map_index = stack_map.GetDexRegisterMapIndex();
155 DCHECK_NE(map_index, StackMap::kNoValue);
156
157 // Skip initial registers which we are not interested in (to get to inlined registers).
158 map_index += mask.PopCount(0, first_dex_register);
159 mask = mask.Subregion(first_dex_register, mask.size_in_bits() - first_dex_register);
160
161 // Update registers that we see for first time (i.e. most recent value).
162 DexRegisterLocation* regs = map->data();
163 const uint32_t end = std::min<uint32_t>(map->size(), mask.size_in_bits());
164 const size_t kNumBits = BitSizeOf<uint32_t>();
165 for (uint32_t reg = 0; reg < end; reg += kNumBits) {
166 // Process the mask in chunks of kNumBits for performance.
167 uint32_t bits = mask.LoadBits(reg, std::min<uint32_t>(end - reg, kNumBits));
168 while (bits != 0) {
169 uint32_t bit = CTZ(bits);
170 if (regs[reg + bit].GetKind() == DexRegisterLocation::Kind::kInvalid) {
171 regs[reg + bit] = GetDexRegisterCatalogEntry(dex_register_maps_.Get(map_index));
172 remaining_registers--;
173 }
174 map_index++;
175 bits ^= 1u << bit; // Clear the bit.
176 }
177 }
178 }
179
180 // Set any remaining registers to None (which is the default state at first stack map).
181 if (remaining_registers != 0) {
182 DexRegisterLocation* regs = map->data();
183 for (uint32_t r = 0; r < map->size(); r++) {
184 if (regs[r].GetKind() == DexRegisterLocation::Kind::kInvalid) {
185 regs[r] = DexRegisterLocation::None();
186 }
187 }
188 }
189}
190
David Srbecky42deda82018-08-10 11:23:27 +0100191// Decode the CodeInfo while collecting size statistics.
192void CodeInfo::CollectSizeStats(const uint8_t* code_info_data, /*out*/ Stats* parent) {
193 Stats* codeinfo_stats = parent->Child("CodeInfo");
194 BitMemoryReader reader(code_info_data);
195 ForEachHeaderField([&reader](auto) { reader.ReadVarint(); });
196 codeinfo_stats->Child("Header")->AddBits(reader.NumberOfReadBits());
197 CodeInfo code_info; // Temporary storage for decoded tables.
198 ForEachBitTableField([codeinfo_stats, &reader, &code_info](auto member_pointer) {
199 auto& table = code_info.*member_pointer;
200 size_t bit_offset = reader.NumberOfReadBits();
201 bool deduped = DecodeTable(table, reader);
202 if (deduped) {
203 codeinfo_stats->Child("DedupeOffset")->AddBits(reader.NumberOfReadBits() - bit_offset);
204 } else {
205 Stats* table_stats = codeinfo_stats->Child(table.GetName());
206 table_stats->AddBits(reader.NumberOfReadBits() - bit_offset);
207 const char* const* column_names = table.GetColumnNames();
208 for (size_t c = 0; c < table.NumColumns(); c++) {
209 if (table.NumColumnBits(c) > 0) {
210 Stats* column_stats = table_stats->Child(column_names[c]);
211 column_stats->AddBits(table.NumRows() * table.NumColumnBits(c), table.NumRows());
212 }
213 }
David Srbecky86decb62018-06-05 06:41:10 +0100214 }
David Srbecky42deda82018-08-10 11:23:27 +0100215 });
216 codeinfo_stats->AddBytes(BitsToBytesRoundUp(reader.NumberOfReadBits()));
David Srbecky86decb62018-06-05 06:41:10 +0100217}
218
David Srbeckye1402122018-06-13 18:20:45 +0100219void DexRegisterMap::Dump(VariableIndentationOutputStream* vios) const {
220 if (HasAnyLiveDexRegisters()) {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100221 ScopedIndentation indent1(vios);
David Srbeckye1402122018-06-13 18:20:45 +0100222 for (size_t i = 0; i < size(); ++i) {
223 DexRegisterLocation reg = (*this)[i];
224 if (reg.IsLive()) {
225 vios->Stream() << "v" << i << ":" << reg << " ";
David Srbecky71ec1cc2018-05-18 15:57:25 +0100226 }
227 }
228 vios->Stream() << "\n";
229 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000230}
231
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100232void CodeInfo::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100233 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100234 bool verbose,
David Srbecky8cd54542018-07-15 23:58:44 +0100235 InstructionSet instruction_set) const {
David Srbecky42deda82018-08-10 11:23:27 +0100236 vios->Stream() << "CodeInfo BitSize=" << size_in_bits_
David Srbeckye42a4b92019-05-26 00:10:25 +0100237 << " Flags:" << flags_
David Srbecky42deda82018-08-10 11:23:27 +0100238 << " FrameSize:" << packed_frame_size_ * kStackAlignment
239 << " CoreSpillMask:" << std::hex << core_spill_mask_
240 << " FpSpillMask:" << std::hex << fp_spill_mask_
241 << " NumberOfDexRegisters:" << std::dec << number_of_dex_registers_
242 << "\n";
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100243 ScopedIndentation indent1(vios);
David Srbecky42deda82018-08-10 11:23:27 +0100244 ForEachBitTableField([this, &vios, verbose](auto member_pointer) {
245 const auto& table = this->*member_pointer;
246 if (table.NumRows() != 0) {
247 vios->Stream() << table.GetName() << " BitSize=" << table.DataBitSize();
248 vios->Stream() << " Rows=" << table.NumRows() << " Bits={";
249 const char* const* column_names = table.GetColumnNames();
250 for (size_t c = 0; c < table.NumColumns(); c++) {
251 vios->Stream() << (c != 0 ? " " : "");
252 vios->Stream() << column_names[c] << "=" << table.NumColumnBits(c);
253 }
254 vios->Stream() << "}\n";
255 if (verbose) {
256 ScopedIndentation indent1(vios);
257 for (size_t r = 0; r < table.NumRows(); r++) {
258 vios->Stream() << "[" << std::right << std::setw(3) << r << "]={";
259 for (size_t c = 0; c < table.NumColumns(); c++) {
260 vios->Stream() << (c != 0 ? " " : "");
261 if (&table == static_cast<const void*>(&stack_masks_) ||
262 &table == static_cast<const void*>(&dex_register_masks_)) {
263 BitMemoryRegion bits = table.GetBitMemoryRegion(r, c);
264 for (size_t b = 0, e = bits.size_in_bits(); b < e; b++) {
265 vios->Stream() << bits.LoadBit(e - b - 1);
266 }
267 } else {
268 vios->Stream() << std::right << std::setw(8) << static_cast<int32_t>(table.Get(r, c));
269 }
270 }
271 vios->Stream() << "}\n";
272 }
273 }
274 }
275 });
David Srbecky71ec1cc2018-05-18 15:57:25 +0100276
Roland Levillaina552e1c2015-03-26 15:01:03 +0000277 // Display stack maps along with (live) Dex register maps.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100278 if (verbose) {
David Srbecky93bd3612018-07-02 19:30:18 +0100279 for (StackMap stack_map : stack_maps_) {
David Srbecky8cd54542018-07-15 23:58:44 +0100280 stack_map.Dump(vios, *this, code_offset, instruction_set);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100281 }
282 }
283}
284
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100285void StackMap::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100286 const CodeInfo& code_info,
287 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100288 InstructionSet instruction_set) const {
David Srbecky052f8ca2018-04-26 15:42:54 +0100289 const uint32_t pc_offset = GetNativePcOffset(instruction_set);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100290 vios->Stream()
David Srbecky71ec1cc2018-05-18 15:57:25 +0100291 << "StackMap[" << Row() << "]"
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100292 << std::hex
David Srbecky71ec1cc2018-05-18 15:57:25 +0100293 << " (native_pc=0x" << code_offset + pc_offset
294 << ", dex_pc=0x" << GetDexPc()
David Srbecky052f8ca2018-04-26 15:42:54 +0100295 << ", register_mask=0x" << code_info.GetRegisterMaskOf(*this)
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100296 << std::dec
297 << ", stack_mask=0b";
David Srbecky052f8ca2018-04-26 15:42:54 +0100298 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(*this);
David Srbecky4b59d102018-05-29 21:46:10 +0000299 for (size_t i = 0, e = stack_mask.size_in_bits(); i < e; ++i) {
David Srbecky45aa5982016-03-18 02:15:09 +0000300 vios->Stream() << stack_mask.LoadBit(e - i - 1);
Roland Levillainf2650d12015-05-28 14:53:28 +0100301 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100302 vios->Stream() << ")\n";
David Srbeckye1402122018-06-13 18:20:45 +0100303 code_info.GetDexRegisterMapOf(*this).Dump(vios);
David Srbecky93bd3612018-07-02 19:30:18 +0100304 for (InlineInfo inline_info : code_info.GetInlineInfosOf(*this)) {
David Srbecky8cd54542018-07-15 23:58:44 +0100305 inline_info.Dump(vios, code_info, *this);
Nicolas Geoffray12bdb722015-06-17 09:44:43 +0100306 }
Roland Levillainf2650d12015-05-28 14:53:28 +0100307}
308
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100309void InlineInfo::Dump(VariableIndentationOutputStream* vios,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100310 const CodeInfo& code_info,
David Srbecky8cd54542018-07-15 23:58:44 +0100311 const StackMap& stack_map) const {
David Srbecky6e69e522018-06-03 12:00:14 +0100312 uint32_t depth = Row() - stack_map.GetInlineInfoIndex();
313 vios->Stream()
314 << "InlineInfo[" << Row() << "]"
315 << " (depth=" << depth
316 << std::hex
317 << ", dex_pc=0x" << GetDexPc();
318 if (EncodesArtMethod()) {
319 ScopedObjectAccess soa(Thread::Current());
320 vios->Stream() << ", method=" << GetArtMethod()->PrettyMethod();
321 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100322 vios->Stream()
David Srbecky6e69e522018-06-03 12:00:14 +0100323 << std::dec
David Srbecky8cd54542018-07-15 23:58:44 +0100324 << ", method_index=" << code_info.GetMethodIndexOf(*this);
David Srbecky6e69e522018-06-03 12:00:14 +0100325 }
326 vios->Stream() << ")\n";
David Srbecky93bd3612018-07-02 19:30:18 +0100327 code_info.GetInlineDexRegisterMapOf(stack_map, *this).Dump(vios);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000328}
329
330} // namespace art