blob: 62b9f35341cee1dea46d41f16d0a6a14e93ecfbd [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 Srbeckya38e6cf2018-06-26 18:13:49 +010035 const uint8_t* begin = data;
David Srbeckyf6ba5b32018-06-23 22:05:49 +010036 frame_size_in_bytes_ = DecodeUnsignedLeb128(&data);
37 core_spill_mask_ = DecodeUnsignedLeb128(&data);
38 fp_spill_mask_ = DecodeUnsignedLeb128(&data);
39 number_of_dex_registers_ = DecodeUnsignedLeb128(&data);
David Srbeckya38e6cf2018-06-26 18:13:49 +010040 BitMemoryReader reader(data, /* bit_offset */ 0);
David Srbecky078d7ba2018-06-21 15:36:48 +010041 stack_maps_.Decode(reader);
David Srbecky6ee06e92018-07-25 21:45:54 +010042 inline_infos_.Decode(reader);
43 if (flags & DecodeFlags::InlineInfoOnly) {
44 return;
45 }
David Srbecky078d7ba2018-06-21 15:36:48 +010046 register_masks_.Decode(reader);
47 stack_masks_.Decode(reader);
David Srbecky078d7ba2018-06-21 15:36:48 +010048 dex_register_masks_.Decode(reader);
49 dex_register_maps_.Decode(reader);
50 dex_register_catalog_.Decode(reader);
David Srbeckya38e6cf2018-06-26 18:13:49 +010051 size_in_bits_ = (data - begin) * kBitsPerByte + reader.GetBitOffset();
David Srbecky078d7ba2018-06-21 15:36:48 +010052}
53
David Srbecky0b4e5a32018-06-11 16:25:29 +010054BitTable<StackMap>::const_iterator CodeInfo::BinarySearchNativePc(uint32_t packed_pc) const {
55 return std::partition_point(
56 stack_maps_.begin(),
57 stack_maps_.end(),
58 [packed_pc](const StackMap& sm) {
59 return sm.GetPackedNativePc() < packed_pc && sm.GetKind() != StackMap::Kind::Catch;
60 });
61}
62
63StackMap CodeInfo::GetStackMapForNativePcOffset(uint32_t pc, InstructionSet isa) const {
64 auto it = BinarySearchNativePc(StackMap::PackNativePc(pc, isa));
65 // Start at the lower bound and iterate over all stack maps with the given native pc.
66 for (; it != stack_maps_.end() && (*it).GetNativePcOffset(isa) == pc; ++it) {
67 StackMap::Kind kind = static_cast<StackMap::Kind>((*it).GetKind());
68 if (kind == StackMap::Kind::Default || kind == StackMap::Kind::OSR) {
69 return *it;
70 }
71 }
David Srbeckya45a85c2018-06-21 16:03:12 +010072 return stack_maps_.GetInvalidRow();
David Srbecky0b4e5a32018-06-11 16:25:29 +010073}
74
David Srbecky6de88332018-06-03 12:00:11 +010075// Scan backward to determine dex register locations at given stack map.
76// All registers for a stack map are combined - inlined registers are just appended,
77// therefore 'first_dex_register' allows us to select a sub-range to decode.
78void CodeInfo::DecodeDexRegisterMap(uint32_t stack_map_index,
79 uint32_t first_dex_register,
80 /*out*/ DexRegisterMap* map) const {
81 // Count remaining work so we know when we have finished.
82 uint32_t remaining_registers = map->size();
83
84 // Keep scanning backwards and collect the most recent location of each register.
85 for (int32_t s = stack_map_index; s >= 0 && remaining_registers != 0; s--) {
86 StackMap stack_map = GetStackMapAt(s);
87 DCHECK_LE(stack_map_index - s, kMaxDexRegisterMapSearchDistance) << "Unbounded search";
88
89 // The mask specifies which registers where modified in this stack map.
90 // NB: the mask can be shorter than expected if trailing zero bits were removed.
91 uint32_t mask_index = stack_map.GetDexRegisterMaskIndex();
92 if (mask_index == StackMap::kNoValue) {
93 continue; // Nothing changed at this stack map.
94 }
95 BitMemoryRegion mask = dex_register_masks_.GetBitMemoryRegion(mask_index);
96 if (mask.size_in_bits() <= first_dex_register) {
97 continue; // Nothing changed after the first register we are interested in.
98 }
99
100 // The map stores one catalogue index per each modified register location.
101 uint32_t map_index = stack_map.GetDexRegisterMapIndex();
102 DCHECK_NE(map_index, StackMap::kNoValue);
103
104 // Skip initial registers which we are not interested in (to get to inlined registers).
105 map_index += mask.PopCount(0, first_dex_register);
106 mask = mask.Subregion(first_dex_register, mask.size_in_bits() - first_dex_register);
107
108 // Update registers that we see for first time (i.e. most recent value).
109 DexRegisterLocation* regs = map->data();
110 const uint32_t end = std::min<uint32_t>(map->size(), mask.size_in_bits());
111 const size_t kNumBits = BitSizeOf<uint32_t>();
112 for (uint32_t reg = 0; reg < end; reg += kNumBits) {
113 // Process the mask in chunks of kNumBits for performance.
114 uint32_t bits = mask.LoadBits(reg, std::min<uint32_t>(end - reg, kNumBits));
115 while (bits != 0) {
116 uint32_t bit = CTZ(bits);
117 if (regs[reg + bit].GetKind() == DexRegisterLocation::Kind::kInvalid) {
118 regs[reg + bit] = GetDexRegisterCatalogEntry(dex_register_maps_.Get(map_index));
119 remaining_registers--;
120 }
121 map_index++;
122 bits ^= 1u << bit; // Clear the bit.
123 }
124 }
125 }
126
127 // Set any remaining registers to None (which is the default state at first stack map).
128 if (remaining_registers != 0) {
129 DexRegisterLocation* regs = map->data();
130 for (uint32_t r = 0; r < map->size(); r++) {
131 if (regs[r].GetKind() == DexRegisterLocation::Kind::kInvalid) {
132 regs[r] = DexRegisterLocation::None();
133 }
134 }
135 }
136}
137
David Srbecky86decb62018-06-05 06:41:10 +0100138template<typename Accessor>
139static void AddTableSizeStats(const char* table_name,
David Srbeckycf7833e2018-06-14 16:45:22 +0100140 const BitTable<Accessor>& table,
David Srbecky86decb62018-06-05 06:41:10 +0100141 /*out*/ Stats* parent) {
142 Stats* table_stats = parent->Child(table_name);
143 table_stats->AddBits(table.BitSize());
144 table_stats->Child("Header")->AddBits(table.HeaderBitSize());
145 const char* const* column_names = GetBitTableColumnNames<Accessor>();
146 for (size_t c = 0; c < table.NumColumns(); c++) {
147 if (table.NumColumnBits(c) > 0) {
148 Stats* column_stats = table_stats->Child(column_names[c]);
149 column_stats->AddBits(table.NumRows() * table.NumColumnBits(c), table.NumRows());
150 }
151 }
152}
153
154void CodeInfo::AddSizeStats(/*out*/ Stats* parent) const {
155 Stats* stats = parent->Child("CodeInfo");
David Srbeckya38e6cf2018-06-26 18:13:49 +0100156 stats->AddBytes(Size());
David Srbecky86decb62018-06-05 06:41:10 +0100157 AddTableSizeStats<StackMap>("StackMaps", stack_maps_, stats);
158 AddTableSizeStats<RegisterMask>("RegisterMasks", register_masks_, stats);
159 AddTableSizeStats<MaskInfo>("StackMasks", stack_masks_, stats);
David Srbecky86decb62018-06-05 06:41:10 +0100160 AddTableSizeStats<InlineInfo>("InlineInfos", inline_infos_, stats);
161 AddTableSizeStats<MaskInfo>("DexRegisterMasks", dex_register_masks_, stats);
162 AddTableSizeStats<DexRegisterMapInfo>("DexRegisterMaps", dex_register_maps_, stats);
163 AddTableSizeStats<DexRegisterInfo>("DexRegisterCatalog", dex_register_catalog_, stats);
164}
165
David Srbeckye1402122018-06-13 18:20:45 +0100166void DexRegisterMap::Dump(VariableIndentationOutputStream* vios) const {
167 if (HasAnyLiveDexRegisters()) {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100168 ScopedIndentation indent1(vios);
David Srbeckye1402122018-06-13 18:20:45 +0100169 for (size_t i = 0; i < size(); ++i) {
170 DexRegisterLocation reg = (*this)[i];
171 if (reg.IsLive()) {
172 vios->Stream() << "v" << i << ":" << reg << " ";
David Srbecky71ec1cc2018-05-18 15:57:25 +0100173 }
174 }
175 vios->Stream() << "\n";
176 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000177}
178
David Srbecky86decb62018-06-05 06:41:10 +0100179template<typename Accessor>
David Srbecky71ec1cc2018-05-18 15:57:25 +0100180static void DumpTable(VariableIndentationOutputStream* vios,
181 const char* table_name,
David Srbeckycf7833e2018-06-14 16:45:22 +0100182 const BitTable<Accessor>& table,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100183 bool verbose,
184 bool is_mask = false) {
185 if (table.NumRows() != 0) {
David Srbecky86decb62018-06-05 06:41:10 +0100186 vios->Stream() << table_name << " BitSize=" << table.BitSize();
David Srbecky71ec1cc2018-05-18 15:57:25 +0100187 vios->Stream() << " Rows=" << table.NumRows() << " Bits={";
David Srbecky86decb62018-06-05 06:41:10 +0100188 const char* const* column_names = GetBitTableColumnNames<Accessor>();
David Srbecky71ec1cc2018-05-18 15:57:25 +0100189 for (size_t c = 0; c < table.NumColumns(); c++) {
190 vios->Stream() << (c != 0 ? " " : "");
David Srbecky86decb62018-06-05 06:41:10 +0100191 vios->Stream() << column_names[c] << "=" << table.NumColumnBits(c);
David Srbecky71ec1cc2018-05-18 15:57:25 +0100192 }
193 vios->Stream() << "}\n";
194 if (verbose) {
195 ScopedIndentation indent1(vios);
196 for (size_t r = 0; r < table.NumRows(); r++) {
197 vios->Stream() << "[" << std::right << std::setw(3) << r << "]={";
198 for (size_t c = 0; c < table.NumColumns(); c++) {
199 vios->Stream() << (c != 0 ? " " : "");
200 if (is_mask) {
201 BitMemoryRegion bits = table.GetBitMemoryRegion(r, c);
202 for (size_t b = 0, e = bits.size_in_bits(); b < e; b++) {
203 vios->Stream() << bits.LoadBit(e - b - 1);
204 }
205 } else {
206 vios->Stream() << std::right << std::setw(8) << static_cast<int32_t>(table.Get(r, c));
207 }
208 }
209 vios->Stream() << "}\n";
210 }
211 }
212 }
David Srbecky61b28a12016-02-25 21:55:03 +0000213}
214
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100215void CodeInfo::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100216 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100217 bool verbose,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700218 InstructionSet instruction_set,
219 const MethodInfo& method_info) const {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100220 vios->Stream()
David Srbecky71ec1cc2018-05-18 15:57:25 +0100221 << "CodeInfo"
David Srbeckya38e6cf2018-06-26 18:13:49 +0100222 << " BitSize=" << size_in_bits_
David Srbecky71ec1cc2018-05-18 15:57:25 +0100223 << "\n";
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100224 ScopedIndentation indent1(vios);
David Srbecky86decb62018-06-05 06:41:10 +0100225 DumpTable<StackMap>(vios, "StackMaps", stack_maps_, verbose);
226 DumpTable<RegisterMask>(vios, "RegisterMasks", register_masks_, verbose);
227 DumpTable<MaskInfo>(vios, "StackMasks", stack_masks_, verbose, true /* is_mask */);
David Srbecky86decb62018-06-05 06:41:10 +0100228 DumpTable<InlineInfo>(vios, "InlineInfos", inline_infos_, verbose);
229 DumpTable<MaskInfo>(vios, "DexRegisterMasks", dex_register_masks_, verbose, true /* is_mask */);
230 DumpTable<DexRegisterMapInfo>(vios, "DexRegisterMaps", dex_register_maps_, verbose);
231 DumpTable<DexRegisterInfo>(vios, "DexRegisterCatalog", dex_register_catalog_, verbose);
David Srbecky71ec1cc2018-05-18 15:57:25 +0100232
Roland Levillaina552e1c2015-03-26 15:01:03 +0000233 // Display stack maps along with (live) Dex register maps.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100234 if (verbose) {
David Srbecky93bd3612018-07-02 19:30:18 +0100235 for (StackMap stack_map : stack_maps_) {
David Srbeckyfd89b072018-06-03 12:00:22 +0100236 stack_map.Dump(vios, *this, method_info, code_offset, instruction_set);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100237 }
238 }
239}
240
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100241void StackMap::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100242 const CodeInfo& code_info,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700243 const MethodInfo& method_info,
Roland Levillainf2650d12015-05-28 14:53:28 +0100244 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100245 InstructionSet instruction_set) const {
David Srbecky052f8ca2018-04-26 15:42:54 +0100246 const uint32_t pc_offset = GetNativePcOffset(instruction_set);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100247 vios->Stream()
David Srbecky71ec1cc2018-05-18 15:57:25 +0100248 << "StackMap[" << Row() << "]"
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100249 << std::hex
David Srbecky71ec1cc2018-05-18 15:57:25 +0100250 << " (native_pc=0x" << code_offset + pc_offset
251 << ", dex_pc=0x" << GetDexPc()
David Srbecky052f8ca2018-04-26 15:42:54 +0100252 << ", register_mask=0x" << code_info.GetRegisterMaskOf(*this)
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100253 << std::dec
254 << ", stack_mask=0b";
David Srbecky052f8ca2018-04-26 15:42:54 +0100255 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(*this);
David Srbecky4b59d102018-05-29 21:46:10 +0000256 for (size_t i = 0, e = stack_mask.size_in_bits(); i < e; ++i) {
David Srbecky45aa5982016-03-18 02:15:09 +0000257 vios->Stream() << stack_mask.LoadBit(e - i - 1);
Roland Levillainf2650d12015-05-28 14:53:28 +0100258 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100259 vios->Stream() << ")\n";
David Srbeckye1402122018-06-13 18:20:45 +0100260 code_info.GetDexRegisterMapOf(*this).Dump(vios);
David Srbecky93bd3612018-07-02 19:30:18 +0100261 for (InlineInfo inline_info : code_info.GetInlineInfosOf(*this)) {
David Srbeckyfd89b072018-06-03 12:00:22 +0100262 inline_info.Dump(vios, code_info, *this, method_info);
Nicolas Geoffray12bdb722015-06-17 09:44:43 +0100263 }
Roland Levillainf2650d12015-05-28 14:53:28 +0100264}
265
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100266void InlineInfo::Dump(VariableIndentationOutputStream* vios,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100267 const CodeInfo& code_info,
David Srbecky6e69e522018-06-03 12:00:14 +0100268 const StackMap& stack_map,
David Srbeckyfd89b072018-06-03 12:00:22 +0100269 const MethodInfo& method_info) const {
David Srbecky6e69e522018-06-03 12:00:14 +0100270 uint32_t depth = Row() - stack_map.GetInlineInfoIndex();
271 vios->Stream()
272 << "InlineInfo[" << Row() << "]"
273 << " (depth=" << depth
274 << std::hex
275 << ", dex_pc=0x" << GetDexPc();
276 if (EncodesArtMethod()) {
277 ScopedObjectAccess soa(Thread::Current());
278 vios->Stream() << ", method=" << GetArtMethod()->PrettyMethod();
279 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100280 vios->Stream()
David Srbecky6e69e522018-06-03 12:00:14 +0100281 << std::dec
282 << ", method_index=" << GetMethodIndex(method_info);
283 }
284 vios->Stream() << ")\n";
David Srbecky93bd3612018-07-02 19:30:18 +0100285 code_info.GetInlineDexRegisterMapOf(stack_map, *this).Dump(vios);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000286}
287
288} // namespace art