blob: a3c6e050455d36f1f7a79b12a869e151834b227c [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 Srbeckyf6ba5b32018-06-23 22:05:49 +010030CodeInfo::CodeInfo(const OatQuickMethodHeader* header)
31 : CodeInfo(header->GetOptimizedCodeInfoPtr()) {
32}
33
David Srbecky078d7ba2018-06-21 15:36:48 +010034void CodeInfo::Decode(const uint8_t* data) {
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);
42 register_masks_.Decode(reader);
43 stack_masks_.Decode(reader);
44 invoke_infos_.Decode(reader);
45 inline_infos_.Decode(reader);
46 dex_register_masks_.Decode(reader);
47 dex_register_maps_.Decode(reader);
48 dex_register_catalog_.Decode(reader);
David Srbeckya38e6cf2018-06-26 18:13:49 +010049 size_in_bits_ = (data - begin) * kBitsPerByte + reader.GetBitOffset();
David Srbecky078d7ba2018-06-21 15:36:48 +010050}
51
David Srbecky0b4e5a32018-06-11 16:25:29 +010052BitTable<StackMap>::const_iterator CodeInfo::BinarySearchNativePc(uint32_t packed_pc) const {
53 return std::partition_point(
54 stack_maps_.begin(),
55 stack_maps_.end(),
56 [packed_pc](const StackMap& sm) {
57 return sm.GetPackedNativePc() < packed_pc && sm.GetKind() != StackMap::Kind::Catch;
58 });
59}
60
61StackMap CodeInfo::GetStackMapForNativePcOffset(uint32_t pc, InstructionSet isa) const {
62 auto it = BinarySearchNativePc(StackMap::PackNativePc(pc, isa));
63 // Start at the lower bound and iterate over all stack maps with the given native pc.
64 for (; it != stack_maps_.end() && (*it).GetNativePcOffset(isa) == pc; ++it) {
65 StackMap::Kind kind = static_cast<StackMap::Kind>((*it).GetKind());
66 if (kind == StackMap::Kind::Default || kind == StackMap::Kind::OSR) {
67 return *it;
68 }
69 }
David Srbeckya45a85c2018-06-21 16:03:12 +010070 return stack_maps_.GetInvalidRow();
David Srbecky0b4e5a32018-06-11 16:25:29 +010071}
72
David Srbecky6de88332018-06-03 12:00:11 +010073// Scan backward to determine dex register locations at given stack map.
74// All registers for a stack map are combined - inlined registers are just appended,
75// therefore 'first_dex_register' allows us to select a sub-range to decode.
76void CodeInfo::DecodeDexRegisterMap(uint32_t stack_map_index,
77 uint32_t first_dex_register,
78 /*out*/ DexRegisterMap* map) const {
79 // Count remaining work so we know when we have finished.
80 uint32_t remaining_registers = map->size();
81
82 // Keep scanning backwards and collect the most recent location of each register.
83 for (int32_t s = stack_map_index; s >= 0 && remaining_registers != 0; s--) {
84 StackMap stack_map = GetStackMapAt(s);
85 DCHECK_LE(stack_map_index - s, kMaxDexRegisterMapSearchDistance) << "Unbounded search";
86
87 // The mask specifies which registers where modified in this stack map.
88 // NB: the mask can be shorter than expected if trailing zero bits were removed.
89 uint32_t mask_index = stack_map.GetDexRegisterMaskIndex();
90 if (mask_index == StackMap::kNoValue) {
91 continue; // Nothing changed at this stack map.
92 }
93 BitMemoryRegion mask = dex_register_masks_.GetBitMemoryRegion(mask_index);
94 if (mask.size_in_bits() <= first_dex_register) {
95 continue; // Nothing changed after the first register we are interested in.
96 }
97
98 // The map stores one catalogue index per each modified register location.
99 uint32_t map_index = stack_map.GetDexRegisterMapIndex();
100 DCHECK_NE(map_index, StackMap::kNoValue);
101
102 // Skip initial registers which we are not interested in (to get to inlined registers).
103 map_index += mask.PopCount(0, first_dex_register);
104 mask = mask.Subregion(first_dex_register, mask.size_in_bits() - first_dex_register);
105
106 // Update registers that we see for first time (i.e. most recent value).
107 DexRegisterLocation* regs = map->data();
108 const uint32_t end = std::min<uint32_t>(map->size(), mask.size_in_bits());
109 const size_t kNumBits = BitSizeOf<uint32_t>();
110 for (uint32_t reg = 0; reg < end; reg += kNumBits) {
111 // Process the mask in chunks of kNumBits for performance.
112 uint32_t bits = mask.LoadBits(reg, std::min<uint32_t>(end - reg, kNumBits));
113 while (bits != 0) {
114 uint32_t bit = CTZ(bits);
115 if (regs[reg + bit].GetKind() == DexRegisterLocation::Kind::kInvalid) {
116 regs[reg + bit] = GetDexRegisterCatalogEntry(dex_register_maps_.Get(map_index));
117 remaining_registers--;
118 }
119 map_index++;
120 bits ^= 1u << bit; // Clear the bit.
121 }
122 }
123 }
124
125 // Set any remaining registers to None (which is the default state at first stack map).
126 if (remaining_registers != 0) {
127 DexRegisterLocation* regs = map->data();
128 for (uint32_t r = 0; r < map->size(); r++) {
129 if (regs[r].GetKind() == DexRegisterLocation::Kind::kInvalid) {
130 regs[r] = DexRegisterLocation::None();
131 }
132 }
133 }
134}
135
David Srbecky86decb62018-06-05 06:41:10 +0100136template<typename Accessor>
137static void AddTableSizeStats(const char* table_name,
David Srbeckycf7833e2018-06-14 16:45:22 +0100138 const BitTable<Accessor>& table,
David Srbecky86decb62018-06-05 06:41:10 +0100139 /*out*/ Stats* parent) {
140 Stats* table_stats = parent->Child(table_name);
141 table_stats->AddBits(table.BitSize());
142 table_stats->Child("Header")->AddBits(table.HeaderBitSize());
143 const char* const* column_names = GetBitTableColumnNames<Accessor>();
144 for (size_t c = 0; c < table.NumColumns(); c++) {
145 if (table.NumColumnBits(c) > 0) {
146 Stats* column_stats = table_stats->Child(column_names[c]);
147 column_stats->AddBits(table.NumRows() * table.NumColumnBits(c), table.NumRows());
148 }
149 }
150}
151
152void CodeInfo::AddSizeStats(/*out*/ Stats* parent) const {
153 Stats* stats = parent->Child("CodeInfo");
David Srbeckya38e6cf2018-06-26 18:13:49 +0100154 stats->AddBytes(Size());
David Srbecky86decb62018-06-05 06:41:10 +0100155 AddTableSizeStats<StackMap>("StackMaps", stack_maps_, stats);
156 AddTableSizeStats<RegisterMask>("RegisterMasks", register_masks_, stats);
157 AddTableSizeStats<MaskInfo>("StackMasks", stack_masks_, stats);
158 AddTableSizeStats<InvokeInfo>("InvokeInfos", invoke_infos_, stats);
159 AddTableSizeStats<InlineInfo>("InlineInfos", inline_infos_, stats);
160 AddTableSizeStats<MaskInfo>("DexRegisterMasks", dex_register_masks_, stats);
161 AddTableSizeStats<DexRegisterMapInfo>("DexRegisterMaps", dex_register_maps_, stats);
162 AddTableSizeStats<DexRegisterInfo>("DexRegisterCatalog", dex_register_catalog_, stats);
163}
164
David Srbeckye1402122018-06-13 18:20:45 +0100165void DexRegisterMap::Dump(VariableIndentationOutputStream* vios) const {
166 if (HasAnyLiveDexRegisters()) {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100167 ScopedIndentation indent1(vios);
David Srbeckye1402122018-06-13 18:20:45 +0100168 for (size_t i = 0; i < size(); ++i) {
169 DexRegisterLocation reg = (*this)[i];
170 if (reg.IsLive()) {
171 vios->Stream() << "v" << i << ":" << reg << " ";
David Srbecky71ec1cc2018-05-18 15:57:25 +0100172 }
173 }
174 vios->Stream() << "\n";
175 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000176}
177
David Srbecky86decb62018-06-05 06:41:10 +0100178template<typename Accessor>
David Srbecky71ec1cc2018-05-18 15:57:25 +0100179static void DumpTable(VariableIndentationOutputStream* vios,
180 const char* table_name,
David Srbeckycf7833e2018-06-14 16:45:22 +0100181 const BitTable<Accessor>& table,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100182 bool verbose,
183 bool is_mask = false) {
184 if (table.NumRows() != 0) {
David Srbecky86decb62018-06-05 06:41:10 +0100185 vios->Stream() << table_name << " BitSize=" << table.BitSize();
David Srbecky71ec1cc2018-05-18 15:57:25 +0100186 vios->Stream() << " Rows=" << table.NumRows() << " Bits={";
David Srbecky86decb62018-06-05 06:41:10 +0100187 const char* const* column_names = GetBitTableColumnNames<Accessor>();
David Srbecky71ec1cc2018-05-18 15:57:25 +0100188 for (size_t c = 0; c < table.NumColumns(); c++) {
189 vios->Stream() << (c != 0 ? " " : "");
David Srbecky86decb62018-06-05 06:41:10 +0100190 vios->Stream() << column_names[c] << "=" << table.NumColumnBits(c);
David Srbecky71ec1cc2018-05-18 15:57:25 +0100191 }
192 vios->Stream() << "}\n";
193 if (verbose) {
194 ScopedIndentation indent1(vios);
195 for (size_t r = 0; r < table.NumRows(); r++) {
196 vios->Stream() << "[" << std::right << std::setw(3) << r << "]={";
197 for (size_t c = 0; c < table.NumColumns(); c++) {
198 vios->Stream() << (c != 0 ? " " : "");
199 if (is_mask) {
200 BitMemoryRegion bits = table.GetBitMemoryRegion(r, c);
201 for (size_t b = 0, e = bits.size_in_bits(); b < e; b++) {
202 vios->Stream() << bits.LoadBit(e - b - 1);
203 }
204 } else {
205 vios->Stream() << std::right << std::setw(8) << static_cast<int32_t>(table.Get(r, c));
206 }
207 }
208 vios->Stream() << "}\n";
209 }
210 }
211 }
David Srbecky61b28a12016-02-25 21:55:03 +0000212}
213
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100214void CodeInfo::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100215 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100216 bool verbose,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700217 InstructionSet instruction_set,
218 const MethodInfo& method_info) const {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100219 vios->Stream()
David Srbecky71ec1cc2018-05-18 15:57:25 +0100220 << "CodeInfo"
David Srbeckya38e6cf2018-06-26 18:13:49 +0100221 << " BitSize=" << size_in_bits_
David Srbecky71ec1cc2018-05-18 15:57:25 +0100222 << "\n";
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100223 ScopedIndentation indent1(vios);
David Srbecky86decb62018-06-05 06:41:10 +0100224 DumpTable<StackMap>(vios, "StackMaps", stack_maps_, verbose);
225 DumpTable<RegisterMask>(vios, "RegisterMasks", register_masks_, verbose);
226 DumpTable<MaskInfo>(vios, "StackMasks", stack_masks_, verbose, true /* is_mask */);
227 DumpTable<InvokeInfo>(vios, "InvokeInfos", invoke_infos_, verbose);
228 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) {
235 for (size_t i = 0; i < GetNumberOfStackMaps(); ++i) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100236 StackMap stack_map = GetStackMapAt(i);
David Srbeckyfd89b072018-06-03 12:00:22 +0100237 stack_map.Dump(vios, *this, method_info, code_offset, instruction_set);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100238 }
239 }
240}
241
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100242void StackMap::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100243 const CodeInfo& code_info,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700244 const MethodInfo& method_info,
Roland Levillainf2650d12015-05-28 14:53:28 +0100245 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100246 InstructionSet instruction_set) const {
David Srbecky052f8ca2018-04-26 15:42:54 +0100247 const uint32_t pc_offset = GetNativePcOffset(instruction_set);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100248 vios->Stream()
David Srbecky71ec1cc2018-05-18 15:57:25 +0100249 << "StackMap[" << Row() << "]"
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100250 << std::hex
David Srbecky71ec1cc2018-05-18 15:57:25 +0100251 << " (native_pc=0x" << code_offset + pc_offset
252 << ", dex_pc=0x" << GetDexPc()
David Srbecky052f8ca2018-04-26 15:42:54 +0100253 << ", register_mask=0x" << code_info.GetRegisterMaskOf(*this)
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100254 << std::dec
255 << ", stack_mask=0b";
David Srbecky052f8ca2018-04-26 15:42:54 +0100256 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(*this);
David Srbecky4b59d102018-05-29 21:46:10 +0000257 for (size_t i = 0, e = stack_mask.size_in_bits(); i < e; ++i) {
David Srbecky45aa5982016-03-18 02:15:09 +0000258 vios->Stream() << stack_mask.LoadBit(e - i - 1);
Roland Levillainf2650d12015-05-28 14:53:28 +0100259 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100260 vios->Stream() << ")\n";
David Srbeckye1402122018-06-13 18:20:45 +0100261 code_info.GetDexRegisterMapOf(*this).Dump(vios);
David Srbecky6e69e522018-06-03 12:00:14 +0100262 uint32_t depth = code_info.GetInlineDepthOf(*this);
263 for (size_t d = 0; d < depth; d++) {
264 InlineInfo inline_info = code_info.GetInlineInfoAtDepth(*this, d);
David Srbeckyfd89b072018-06-03 12:00:22 +0100265 inline_info.Dump(vios, code_info, *this, method_info);
Nicolas Geoffray12bdb722015-06-17 09:44:43 +0100266 }
Roland Levillainf2650d12015-05-28 14:53:28 +0100267}
268
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100269void InlineInfo::Dump(VariableIndentationOutputStream* vios,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100270 const CodeInfo& code_info,
David Srbecky6e69e522018-06-03 12:00:14 +0100271 const StackMap& stack_map,
David Srbeckyfd89b072018-06-03 12:00:22 +0100272 const MethodInfo& method_info) const {
David Srbecky6e69e522018-06-03 12:00:14 +0100273 uint32_t depth = Row() - stack_map.GetInlineInfoIndex();
274 vios->Stream()
275 << "InlineInfo[" << Row() << "]"
276 << " (depth=" << depth
277 << std::hex
278 << ", dex_pc=0x" << GetDexPc();
279 if (EncodesArtMethod()) {
280 ScopedObjectAccess soa(Thread::Current());
281 vios->Stream() << ", method=" << GetArtMethod()->PrettyMethod();
282 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100283 vios->Stream()
David Srbecky6e69e522018-06-03 12:00:14 +0100284 << std::dec
285 << ", method_index=" << GetMethodIndex(method_info);
286 }
287 vios->Stream() << ")\n";
David Srbeckye1402122018-06-13 18:20:45 +0100288 code_info.GetDexRegisterMapAtDepth(depth, stack_map).Dump(vios);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000289}
290
291} // namespace art