blob: 23cc1d63589f100148419fb48faafda24d2acf7c [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"
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000025#include "scoped_thread_state_change-inl.h"
Roland Levillain0396ed72015-05-27 15:12:19 +010026
Nicolas Geoffray004c2302015-03-20 10:06:38 +000027namespace art {
28
David Srbecky6de88332018-06-03 12:00:11 +010029// Scan backward to determine dex register locations at given stack map.
30// All registers for a stack map are combined - inlined registers are just appended,
31// therefore 'first_dex_register' allows us to select a sub-range to decode.
32void CodeInfo::DecodeDexRegisterMap(uint32_t stack_map_index,
33 uint32_t first_dex_register,
34 /*out*/ DexRegisterMap* map) const {
35 // Count remaining work so we know when we have finished.
36 uint32_t remaining_registers = map->size();
37
38 // Keep scanning backwards and collect the most recent location of each register.
39 for (int32_t s = stack_map_index; s >= 0 && remaining_registers != 0; s--) {
40 StackMap stack_map = GetStackMapAt(s);
41 DCHECK_LE(stack_map_index - s, kMaxDexRegisterMapSearchDistance) << "Unbounded search";
42
43 // The mask specifies which registers where modified in this stack map.
44 // NB: the mask can be shorter than expected if trailing zero bits were removed.
45 uint32_t mask_index = stack_map.GetDexRegisterMaskIndex();
46 if (mask_index == StackMap::kNoValue) {
47 continue; // Nothing changed at this stack map.
48 }
49 BitMemoryRegion mask = dex_register_masks_.GetBitMemoryRegion(mask_index);
50 if (mask.size_in_bits() <= first_dex_register) {
51 continue; // Nothing changed after the first register we are interested in.
52 }
53
54 // The map stores one catalogue index per each modified register location.
55 uint32_t map_index = stack_map.GetDexRegisterMapIndex();
56 DCHECK_NE(map_index, StackMap::kNoValue);
57
58 // Skip initial registers which we are not interested in (to get to inlined registers).
59 map_index += mask.PopCount(0, first_dex_register);
60 mask = mask.Subregion(first_dex_register, mask.size_in_bits() - first_dex_register);
61
62 // Update registers that we see for first time (i.e. most recent value).
63 DexRegisterLocation* regs = map->data();
64 const uint32_t end = std::min<uint32_t>(map->size(), mask.size_in_bits());
65 const size_t kNumBits = BitSizeOf<uint32_t>();
66 for (uint32_t reg = 0; reg < end; reg += kNumBits) {
67 // Process the mask in chunks of kNumBits for performance.
68 uint32_t bits = mask.LoadBits(reg, std::min<uint32_t>(end - reg, kNumBits));
69 while (bits != 0) {
70 uint32_t bit = CTZ(bits);
71 if (regs[reg + bit].GetKind() == DexRegisterLocation::Kind::kInvalid) {
72 regs[reg + bit] = GetDexRegisterCatalogEntry(dex_register_maps_.Get(map_index));
73 remaining_registers--;
74 }
75 map_index++;
76 bits ^= 1u << bit; // Clear the bit.
77 }
78 }
79 }
80
81 // Set any remaining registers to None (which is the default state at first stack map).
82 if (remaining_registers != 0) {
83 DexRegisterLocation* regs = map->data();
84 for (uint32_t r = 0; r < map->size(); r++) {
85 if (regs[r].GetKind() == DexRegisterLocation::Kind::kInvalid) {
86 regs[r] = DexRegisterLocation::None();
87 }
88 }
89 }
90}
91
David Srbecky71ec1cc2018-05-18 15:57:25 +010092std::ostream& operator<<(std::ostream& stream, const DexRegisterLocation& reg) {
David Srbecky7dc11782016-02-25 13:23:56 +000093 using Kind = DexRegisterLocation::Kind;
David Srbecky71ec1cc2018-05-18 15:57:25 +010094 switch (reg.GetKind()) {
David Srbecky7dc11782016-02-25 13:23:56 +000095 case Kind::kNone:
David Srbecky71ec1cc2018-05-18 15:57:25 +010096 return stream << "None";
David Srbecky7dc11782016-02-25 13:23:56 +000097 case Kind::kInStack:
David Srbecky71ec1cc2018-05-18 15:57:25 +010098 return stream << "sp+" << reg.GetValue();
David Srbecky7dc11782016-02-25 13:23:56 +000099 case Kind::kInRegister:
David Srbecky71ec1cc2018-05-18 15:57:25 +0100100 return stream << "r" << reg.GetValue();
David Srbecky7dc11782016-02-25 13:23:56 +0000101 case Kind::kInRegisterHigh:
David Srbecky71ec1cc2018-05-18 15:57:25 +0100102 return stream << "r" << reg.GetValue() << "/hi";
David Srbecky7dc11782016-02-25 13:23:56 +0000103 case Kind::kInFpuRegister:
David Srbecky71ec1cc2018-05-18 15:57:25 +0100104 return stream << "f" << reg.GetValue();
David Srbecky7dc11782016-02-25 13:23:56 +0000105 case Kind::kInFpuRegisterHigh:
David Srbecky71ec1cc2018-05-18 15:57:25 +0100106 return stream << "f" << reg.GetValue() << "/hi";
David Srbecky7dc11782016-02-25 13:23:56 +0000107 case Kind::kConstant:
David Srbecky71ec1cc2018-05-18 15:57:25 +0100108 return stream << "#" << reg.GetValue();
David Srbecky6de88332018-06-03 12:00:11 +0100109 case Kind::kInvalid:
110 return stream << "Invalid";
David Srbecky71ec1cc2018-05-18 15:57:25 +0100111 default:
112 return stream << "DexRegisterLocation(" << static_cast<uint32_t>(reg.GetKind())
113 << "," << reg.GetValue() << ")";
David Srbecky7dc11782016-02-25 13:23:56 +0000114 }
David Srbecky7dc11782016-02-25 13:23:56 +0000115}
116
David Srbecky86decb62018-06-05 06:41:10 +0100117template<typename Accessor>
118static void AddTableSizeStats(const char* table_name,
119 const BitTable<Accessor::kCount>& table,
120 /*out*/ Stats* parent) {
121 Stats* table_stats = parent->Child(table_name);
122 table_stats->AddBits(table.BitSize());
123 table_stats->Child("Header")->AddBits(table.HeaderBitSize());
124 const char* const* column_names = GetBitTableColumnNames<Accessor>();
125 for (size_t c = 0; c < table.NumColumns(); c++) {
126 if (table.NumColumnBits(c) > 0) {
127 Stats* column_stats = table_stats->Child(column_names[c]);
128 column_stats->AddBits(table.NumRows() * table.NumColumnBits(c), table.NumRows());
129 }
130 }
131}
132
133void CodeInfo::AddSizeStats(/*out*/ Stats* parent) const {
134 Stats* stats = parent->Child("CodeInfo");
135 stats->AddBytes(size_);
136 stats->Child("Header")->AddBytes(UnsignedLeb128Size(size_));
137 AddTableSizeStats<StackMap>("StackMaps", stack_maps_, stats);
138 AddTableSizeStats<RegisterMask>("RegisterMasks", register_masks_, stats);
139 AddTableSizeStats<MaskInfo>("StackMasks", stack_masks_, stats);
140 AddTableSizeStats<InvokeInfo>("InvokeInfos", invoke_infos_, stats);
141 AddTableSizeStats<InlineInfo>("InlineInfos", inline_infos_, stats);
142 AddTableSizeStats<MaskInfo>("DexRegisterMasks", dex_register_masks_, stats);
143 AddTableSizeStats<DexRegisterMapInfo>("DexRegisterMaps", dex_register_maps_, stats);
144 AddTableSizeStats<DexRegisterInfo>("DexRegisterCatalog", dex_register_catalog_, stats);
145}
146
David Srbecky71ec1cc2018-05-18 15:57:25 +0100147static void DumpDexRegisterMap(VariableIndentationOutputStream* vios,
148 const DexRegisterMap& map) {
David Srbecky86decb62018-06-05 06:41:10 +0100149 if (map.HasAnyLiveDexRegisters()) {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100150 ScopedIndentation indent1(vios);
151 for (size_t i = 0; i < map.size(); ++i) {
152 if (map.IsDexRegisterLive(i)) {
153 vios->Stream() << "v" << i << ":" << map.Get(i) << " ";
154 }
155 }
156 vios->Stream() << "\n";
157 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000158}
159
David Srbecky86decb62018-06-05 06:41:10 +0100160template<typename Accessor>
David Srbecky71ec1cc2018-05-18 15:57:25 +0100161static void DumpTable(VariableIndentationOutputStream* vios,
162 const char* table_name,
David Srbecky86decb62018-06-05 06:41:10 +0100163 const BitTable<Accessor::kCount>& table,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100164 bool verbose,
165 bool is_mask = false) {
166 if (table.NumRows() != 0) {
David Srbecky86decb62018-06-05 06:41:10 +0100167 vios->Stream() << table_name << " BitSize=" << table.BitSize();
David Srbecky71ec1cc2018-05-18 15:57:25 +0100168 vios->Stream() << " Rows=" << table.NumRows() << " Bits={";
David Srbecky86decb62018-06-05 06:41:10 +0100169 const char* const* column_names = GetBitTableColumnNames<Accessor>();
David Srbecky71ec1cc2018-05-18 15:57:25 +0100170 for (size_t c = 0; c < table.NumColumns(); c++) {
171 vios->Stream() << (c != 0 ? " " : "");
David Srbecky86decb62018-06-05 06:41:10 +0100172 vios->Stream() << column_names[c] << "=" << table.NumColumnBits(c);
David Srbecky71ec1cc2018-05-18 15:57:25 +0100173 }
174 vios->Stream() << "}\n";
175 if (verbose) {
176 ScopedIndentation indent1(vios);
177 for (size_t r = 0; r < table.NumRows(); r++) {
178 vios->Stream() << "[" << std::right << std::setw(3) << r << "]={";
179 for (size_t c = 0; c < table.NumColumns(); c++) {
180 vios->Stream() << (c != 0 ? " " : "");
181 if (is_mask) {
182 BitMemoryRegion bits = table.GetBitMemoryRegion(r, c);
183 for (size_t b = 0, e = bits.size_in_bits(); b < e; b++) {
184 vios->Stream() << bits.LoadBit(e - b - 1);
185 }
186 } else {
187 vios->Stream() << std::right << std::setw(8) << static_cast<int32_t>(table.Get(r, c));
188 }
189 }
190 vios->Stream() << "}\n";
191 }
192 }
193 }
David Srbecky61b28a12016-02-25 21:55:03 +0000194}
195
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100196void CodeInfo::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100197 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100198 bool verbose,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700199 InstructionSet instruction_set,
200 const MethodInfo& method_info) const {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100201 vios->Stream()
David Srbecky71ec1cc2018-05-18 15:57:25 +0100202 << "CodeInfo"
203 << " BitSize=" << size_ * kBitsPerByte
204 << "\n";
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100205 ScopedIndentation indent1(vios);
David Srbecky86decb62018-06-05 06:41:10 +0100206 DumpTable<StackMap>(vios, "StackMaps", stack_maps_, verbose);
207 DumpTable<RegisterMask>(vios, "RegisterMasks", register_masks_, verbose);
208 DumpTable<MaskInfo>(vios, "StackMasks", stack_masks_, verbose, true /* is_mask */);
209 DumpTable<InvokeInfo>(vios, "InvokeInfos", invoke_infos_, verbose);
210 DumpTable<InlineInfo>(vios, "InlineInfos", inline_infos_, verbose);
211 DumpTable<MaskInfo>(vios, "DexRegisterMasks", dex_register_masks_, verbose, true /* is_mask */);
212 DumpTable<DexRegisterMapInfo>(vios, "DexRegisterMaps", dex_register_maps_, verbose);
213 DumpTable<DexRegisterInfo>(vios, "DexRegisterCatalog", dex_register_catalog_, verbose);
David Srbecky71ec1cc2018-05-18 15:57:25 +0100214
Roland Levillaina552e1c2015-03-26 15:01:03 +0000215 // Display stack maps along with (live) Dex register maps.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100216 if (verbose) {
217 for (size_t i = 0; i < GetNumberOfStackMaps(); ++i) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100218 StackMap stack_map = GetStackMapAt(i);
David Srbeckyfd89b072018-06-03 12:00:22 +0100219 stack_map.Dump(vios, *this, method_info, code_offset, instruction_set);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100220 }
221 }
222}
223
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100224void StackMap::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100225 const CodeInfo& code_info,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700226 const MethodInfo& method_info,
Roland Levillainf2650d12015-05-28 14:53:28 +0100227 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100228 InstructionSet instruction_set) const {
David Srbecky052f8ca2018-04-26 15:42:54 +0100229 const uint32_t pc_offset = GetNativePcOffset(instruction_set);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100230 vios->Stream()
David Srbecky71ec1cc2018-05-18 15:57:25 +0100231 << "StackMap[" << Row() << "]"
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100232 << std::hex
David Srbecky71ec1cc2018-05-18 15:57:25 +0100233 << " (native_pc=0x" << code_offset + pc_offset
234 << ", dex_pc=0x" << GetDexPc()
David Srbecky052f8ca2018-04-26 15:42:54 +0100235 << ", register_mask=0x" << code_info.GetRegisterMaskOf(*this)
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100236 << std::dec
237 << ", stack_mask=0b";
David Srbecky052f8ca2018-04-26 15:42:54 +0100238 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(*this);
David Srbecky4b59d102018-05-29 21:46:10 +0000239 for (size_t i = 0, e = stack_mask.size_in_bits(); i < e; ++i) {
David Srbecky45aa5982016-03-18 02:15:09 +0000240 vios->Stream() << stack_mask.LoadBit(e - i - 1);
Roland Levillainf2650d12015-05-28 14:53:28 +0100241 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100242 vios->Stream() << ")\n";
David Srbeckyfd89b072018-06-03 12:00:22 +0100243 DumpDexRegisterMap(vios, code_info.GetDexRegisterMapOf(*this));
David Srbecky6e69e522018-06-03 12:00:14 +0100244 uint32_t depth = code_info.GetInlineDepthOf(*this);
245 for (size_t d = 0; d < depth; d++) {
246 InlineInfo inline_info = code_info.GetInlineInfoAtDepth(*this, d);
David Srbeckyfd89b072018-06-03 12:00:22 +0100247 inline_info.Dump(vios, code_info, *this, method_info);
Nicolas Geoffray12bdb722015-06-17 09:44:43 +0100248 }
Roland Levillainf2650d12015-05-28 14:53:28 +0100249}
250
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100251void InlineInfo::Dump(VariableIndentationOutputStream* vios,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100252 const CodeInfo& code_info,
David Srbecky6e69e522018-06-03 12:00:14 +0100253 const StackMap& stack_map,
David Srbeckyfd89b072018-06-03 12:00:22 +0100254 const MethodInfo& method_info) const {
David Srbecky6e69e522018-06-03 12:00:14 +0100255 uint32_t depth = Row() - stack_map.GetInlineInfoIndex();
256 vios->Stream()
257 << "InlineInfo[" << Row() << "]"
258 << " (depth=" << depth
259 << std::hex
260 << ", dex_pc=0x" << GetDexPc();
261 if (EncodesArtMethod()) {
262 ScopedObjectAccess soa(Thread::Current());
263 vios->Stream() << ", method=" << GetArtMethod()->PrettyMethod();
264 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100265 vios->Stream()
David Srbecky6e69e522018-06-03 12:00:14 +0100266 << std::dec
267 << ", method_index=" << GetMethodIndex(method_info);
268 }
269 vios->Stream() << ")\n";
David Srbeckyfd89b072018-06-03 12:00:22 +0100270 DumpDexRegisterMap(vios, code_info.GetDexRegisterMapAtDepth(depth, stack_map));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000271}
272
273} // namespace art