blob: 0d289b877bc3c8d03ab7c753855f1e7f332857c3 [file] [log] [blame]
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001/*
2 * Copyright (C) 2014 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#ifndef ART_RUNTIME_STACK_MAP_H_
18#define ART_RUNTIME_STACK_MAP_H_
19
Andreas Gampe69489fa2017-06-08 18:03:25 -070020#include <limits>
21
David Srbeckyf6ba5b32018-06-23 22:05:49 +010022#include "arch/instruction_set.h"
David Sehr1ce2b3b2018-04-05 11:02:03 -070023#include "base/bit_memory_region.h"
David Srbecky052f8ca2018-04-26 15:42:54 +010024#include "base/bit_table.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010025#include "base/bit_utils.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070026#include "base/bit_vector.h"
David Sehr67bf42e2018-02-26 16:43:04 -080027#include "base/leb128.h"
David Sehr1ce2b3b2018-04-05 11:02:03 -070028#include "base/memory_region.h"
David Sehr9e734c72018-01-04 17:56:19 -080029#include "dex/dex_file_types.h"
David Srbecky71ec1cc2018-05-18 15:57:25 +010030#include "dex_register_location.h"
David Srbeckyf6ba5b32018-06-23 22:05:49 +010031#include "quick/quick_method_frame_info.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010032
33namespace art {
34
David Srbeckyf6ba5b32018-06-23 22:05:49 +010035class OatQuickMethodHeader;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +010036class VariableIndentationOutputStream;
37
Roland Levillaina2d8ec62015-03-12 15:25:29 +000038// Size of a frame slot, in bytes. This constant is a signed value,
39// to please the compiler in arithmetic operations involving int32_t
40// (signed) values.
Roland Levillaina552e1c2015-03-26 15:01:03 +000041static constexpr ssize_t kFrameSlotSize = 4;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000042
David Srbecky6de88332018-06-03 12:00:11 +010043// The delta compression of dex register maps means we need to scan the stackmaps backwards.
44// We compress the data in such a way so that there is an upper bound on the search distance.
45// Max distance 0 means each stack map must be fully defined and no scanning back is allowed.
46// If this value is changed, the oat file version should be incremented (for DCHECK to pass).
47static constexpr size_t kMaxDexRegisterMapSearchDistance = 32;
48
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000049class ArtMethod;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000050class CodeInfo;
David Srbecky86decb62018-06-05 06:41:10 +010051class Stats;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000052
David Srbecky71ec1cc2018-05-18 15:57:25 +010053std::ostream& operator<<(std::ostream& stream, const DexRegisterLocation& reg);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010054
David Srbecky71ec1cc2018-05-18 15:57:25 +010055// Information on Dex register locations for a specific PC.
56// Effectively just a convenience wrapper for DexRegisterLocation vector.
57// If the size is small enough, it keeps the data on the stack.
David Srbeckye1402122018-06-13 18:20:45 +010058// TODO: Replace this with generic purpose "small-vector" implementation.
Roland Levillaina552e1c2015-03-26 15:01:03 +000059class DexRegisterMap {
60 public:
David Srbecky6de88332018-06-03 12:00:11 +010061 using iterator = DexRegisterLocation*;
David Srbeckye1402122018-06-13 18:20:45 +010062 using const_iterator = const DexRegisterLocation*;
David Srbecky6de88332018-06-03 12:00:11 +010063
64 // Create map for given number of registers and initialize them to the given value.
65 DexRegisterMap(size_t count, DexRegisterLocation value) : count_(count), regs_small_{} {
David Srbecky71ec1cc2018-05-18 15:57:25 +010066 if (count_ <= kSmallCount) {
David Srbecky6de88332018-06-03 12:00:11 +010067 std::fill_n(regs_small_.begin(), count, value);
David Srbecky71ec1cc2018-05-18 15:57:25 +010068 } else {
David Srbecky6de88332018-06-03 12:00:11 +010069 regs_large_.resize(count, value);
David Srbecky71ec1cc2018-05-18 15:57:25 +010070 }
Roland Levillaina552e1c2015-03-26 15:01:03 +000071 }
72
David Srbecky71ec1cc2018-05-18 15:57:25 +010073 DexRegisterLocation* data() {
74 return count_ <= kSmallCount ? regs_small_.data() : regs_large_.data();
75 }
David Srbeckye1402122018-06-13 18:20:45 +010076 const DexRegisterLocation* data() const {
77 return count_ <= kSmallCount ? regs_small_.data() : regs_large_.data();
78 }
Roland Levillaina552e1c2015-03-26 15:01:03 +000079
David Srbecky6de88332018-06-03 12:00:11 +010080 iterator begin() { return data(); }
81 iterator end() { return data() + count_; }
David Srbeckye1402122018-06-13 18:20:45 +010082 const_iterator begin() const { return data(); }
83 const_iterator end() const { return data() + count_; }
David Srbecky71ec1cc2018-05-18 15:57:25 +010084 size_t size() const { return count_; }
David Srbeckyfd89b072018-06-03 12:00:22 +010085 bool empty() const { return count_ == 0; }
David Srbecky71ec1cc2018-05-18 15:57:25 +010086
David Srbeckye1402122018-06-13 18:20:45 +010087 DexRegisterLocation& operator[](size_t index) {
David Srbecky71ec1cc2018-05-18 15:57:25 +010088 DCHECK_LT(index, count_);
David Srbeckye1402122018-06-13 18:20:45 +010089 return data()[index];
David Srbecky71ec1cc2018-05-18 15:57:25 +010090 }
David Srbeckye1402122018-06-13 18:20:45 +010091 const DexRegisterLocation& operator[](size_t index) const {
92 DCHECK_LT(index, count_);
93 return data()[index];
Roland Levillaina552e1c2015-03-26 15:01:03 +000094 }
95
David Srbecky71ec1cc2018-05-18 15:57:25 +010096 size_t GetNumberOfLiveDexRegisters() const {
David Srbeckye1402122018-06-13 18:20:45 +010097 return std::count_if(begin(), end(), [](auto& loc) { return loc.IsLive(); });
Roland Levillaina552e1c2015-03-26 15:01:03 +000098 }
99
David Srbecky71ec1cc2018-05-18 15:57:25 +0100100 bool HasAnyLiveDexRegisters() const {
David Srbeckye1402122018-06-13 18:20:45 +0100101 return std::any_of(begin(), end(), [](auto& loc) { return loc.IsLive(); });
David Srbecky21d45b42018-05-30 06:35:05 +0100102 }
103
David Srbeckye1402122018-06-13 18:20:45 +0100104 void Dump(VariableIndentationOutputStream* vios) const;
105
Roland Levillaina552e1c2015-03-26 15:01:03 +0000106 private:
David Srbecky71ec1cc2018-05-18 15:57:25 +0100107 // Store the data inline if the number of registers is small to avoid memory allocations.
108 // If count_ <= kSmallCount, we use the regs_small_ array, and regs_large_ otherwise.
109 static constexpr size_t kSmallCount = 16;
110 size_t count_;
111 std::array<DexRegisterLocation, kSmallCount> regs_small_;
112 dchecked_vector<DexRegisterLocation> regs_large_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000113};
114
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100115/**
116 * A Stack Map holds compilation information for a specific PC necessary for:
117 * - Mapping it to a dex PC,
118 * - Knowing which stack entries are objects,
119 * - Knowing which registers hold objects,
120 * - Knowing the inlining information,
121 * - Knowing the values of dex registers.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100122 */
David Srbeckycf7833e2018-06-14 16:45:22 +0100123class StackMap : public BitTableAccessor<8> {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100124 public:
David Srbecky50fac062018-06-13 18:55:35 +0100125 enum Kind {
126 Default = -1,
127 Catch = 0,
128 OSR = 1,
129 Debug = 2,
130 };
David Srbecky42deda82018-08-10 11:23:27 +0100131 BIT_TABLE_HEADER(StackMap)
David Srbecky50fac062018-06-13 18:55:35 +0100132 BIT_TABLE_COLUMN(0, Kind)
133 BIT_TABLE_COLUMN(1, PackedNativePc)
134 BIT_TABLE_COLUMN(2, DexPc)
135 BIT_TABLE_COLUMN(3, RegisterMaskIndex)
136 BIT_TABLE_COLUMN(4, StackMaskIndex)
137 BIT_TABLE_COLUMN(5, InlineInfoIndex)
138 BIT_TABLE_COLUMN(6, DexRegisterMaskIndex)
139 BIT_TABLE_COLUMN(7, DexRegisterMapIndex)
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100140
David Srbecky052f8ca2018-04-26 15:42:54 +0100141 ALWAYS_INLINE uint32_t GetNativePcOffset(InstructionSet instruction_set) const {
David Srbeckycf7833e2018-06-14 16:45:22 +0100142 return UnpackNativePc(GetPackedNativePc(), instruction_set);
David Brazdilf677ebf2015-05-29 16:29:43 +0100143 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100144
David Srbeckyd97e0822018-06-03 12:00:24 +0100145 ALWAYS_INLINE bool HasInlineInfo() const {
146 return HasInlineInfoIndex();
147 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100148
David Srbeckyd97e0822018-06-03 12:00:24 +0100149 ALWAYS_INLINE bool HasDexRegisterMap() const {
150 return HasDexRegisterMapIndex();
151 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100152
David Srbeckyd02b23f2018-05-29 23:27:22 +0100153 static uint32_t PackNativePc(uint32_t native_pc, InstructionSet isa) {
David Srbeckyd775f962018-05-30 18:12:52 +0100154 DCHECK_ALIGNED_PARAM(native_pc, GetInstructionSetInstructionAlignment(isa));
David Srbeckyd02b23f2018-05-29 23:27:22 +0100155 return native_pc / GetInstructionSetInstructionAlignment(isa);
156 }
157
158 static uint32_t UnpackNativePc(uint32_t packed_native_pc, InstructionSet isa) {
159 uint32_t native_pc = packed_native_pc * GetInstructionSetInstructionAlignment(isa);
160 DCHECK_EQ(native_pc / GetInstructionSetInstructionAlignment(isa), packed_native_pc);
161 return native_pc;
162 }
163
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100164 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100165 const CodeInfo& code_info,
166 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100167 InstructionSet instruction_set) const;
David Srbecky61b28a12016-02-25 21:55:03 +0000168};
169
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100170/**
David Srbecky052f8ca2018-04-26 15:42:54 +0100171 * Inline information for a specific PC.
172 * The row referenced from the StackMap holds information at depth 0.
173 * Following rows hold information for further depths.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100174 */
David Srbeckycf7833e2018-06-14 16:45:22 +0100175class InlineInfo : public BitTableAccessor<6> {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100176 public:
David Srbecky42deda82018-08-10 11:23:27 +0100177 BIT_TABLE_HEADER(InlineInfo)
David Srbeckyd97e0822018-06-03 12:00:24 +0100178 BIT_TABLE_COLUMN(0, IsLast) // Determines if there are further rows for further depths.
179 BIT_TABLE_COLUMN(1, DexPc)
180 BIT_TABLE_COLUMN(2, MethodInfoIndex)
181 BIT_TABLE_COLUMN(3, ArtMethodHi) // High bits of ArtMethod*.
182 BIT_TABLE_COLUMN(4, ArtMethodLo) // Low bits of ArtMethod*.
David Srbecky6de88332018-06-03 12:00:11 +0100183 BIT_TABLE_COLUMN(5, NumberOfDexRegisters) // Includes outer levels and the main method.
David Srbeckyd97e0822018-06-03 12:00:24 +0100184
David Srbecky052f8ca2018-04-26 15:42:54 +0100185 static constexpr uint32_t kLast = -1;
186 static constexpr uint32_t kMore = 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100187
David Srbecky6e69e522018-06-03 12:00:14 +0100188 bool EncodesArtMethod() const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100189 return HasArtMethodLo();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100190 }
191
David Srbecky6e69e522018-06-03 12:00:14 +0100192 ArtMethod* GetArtMethod() const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100193 uint64_t lo = GetArtMethodLo();
194 uint64_t hi = GetArtMethodHi();
David Srbecky71ec1cc2018-05-18 15:57:25 +0100195 return reinterpret_cast<ArtMethod*>((hi << 32) | lo);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100196 }
197
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100198 void Dump(VariableIndentationOutputStream* vios,
David Srbecky61b28a12016-02-25 21:55:03 +0000199 const CodeInfo& info,
David Srbecky8cd54542018-07-15 23:58:44 +0100200 const StackMap& stack_map) const;
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800201};
202
David Srbecky42deda82018-08-10 11:23:27 +0100203class StackMask : public BitTableAccessor<1> {
David Srbecky86decb62018-06-05 06:41:10 +0100204 public:
David Srbecky42deda82018-08-10 11:23:27 +0100205 BIT_TABLE_HEADER(StackMask)
206 BIT_TABLE_COLUMN(0, Mask)
207};
208
209class DexRegisterMask : public BitTableAccessor<1> {
210 public:
211 BIT_TABLE_HEADER(DexRegisterMask)
David Srbecky86decb62018-06-05 06:41:10 +0100212 BIT_TABLE_COLUMN(0, Mask)
213};
214
David Srbeckycf7833e2018-06-14 16:45:22 +0100215class DexRegisterMapInfo : public BitTableAccessor<1> {
David Srbecky86decb62018-06-05 06:41:10 +0100216 public:
David Srbecky42deda82018-08-10 11:23:27 +0100217 BIT_TABLE_HEADER(DexRegisterMapInfo)
David Srbecky86decb62018-06-05 06:41:10 +0100218 BIT_TABLE_COLUMN(0, CatalogueIndex)
219};
220
David Srbeckycf7833e2018-06-14 16:45:22 +0100221class DexRegisterInfo : public BitTableAccessor<2> {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100222 public:
David Srbecky42deda82018-08-10 11:23:27 +0100223 BIT_TABLE_HEADER(DexRegisterInfo)
David Srbeckyd97e0822018-06-03 12:00:24 +0100224 BIT_TABLE_COLUMN(0, Kind)
225 BIT_TABLE_COLUMN(1, PackedValue)
David Srbecky71ec1cc2018-05-18 15:57:25 +0100226
227 ALWAYS_INLINE DexRegisterLocation GetLocation() const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100228 DexRegisterLocation::Kind kind = static_cast<DexRegisterLocation::Kind>(GetKind());
229 return DexRegisterLocation(kind, UnpackValue(kind, GetPackedValue()));
David Srbecky71ec1cc2018-05-18 15:57:25 +0100230 }
231
232 static uint32_t PackValue(DexRegisterLocation::Kind kind, uint32_t value) {
233 uint32_t packed_value = value;
234 if (kind == DexRegisterLocation::Kind::kInStack) {
235 DCHECK(IsAligned<kFrameSlotSize>(packed_value));
236 packed_value /= kFrameSlotSize;
237 }
238 return packed_value;
239 }
240
241 static uint32_t UnpackValue(DexRegisterLocation::Kind kind, uint32_t packed_value) {
242 uint32_t value = packed_value;
243 if (kind == DexRegisterLocation::Kind::kInStack) {
244 value *= kFrameSlotSize;
245 }
246 return value;
247 }
248};
249
David Srbecky4b59d102018-05-29 21:46:10 +0000250// Register masks tend to have many trailing zero bits (caller-saves are usually not encoded),
251// therefore it is worth encoding the mask as value+shift.
David Srbeckycf7833e2018-06-14 16:45:22 +0100252class RegisterMask : public BitTableAccessor<2> {
David Srbecky4b59d102018-05-29 21:46:10 +0000253 public:
David Srbecky42deda82018-08-10 11:23:27 +0100254 BIT_TABLE_HEADER(RegisterMask)
David Srbeckyd97e0822018-06-03 12:00:24 +0100255 BIT_TABLE_COLUMN(0, Value)
256 BIT_TABLE_COLUMN(1, Shift)
David Srbecky4b59d102018-05-29 21:46:10 +0000257
258 ALWAYS_INLINE uint32_t GetMask() const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100259 return GetValue() << GetShift();
David Srbecky4b59d102018-05-29 21:46:10 +0000260 }
261};
262
David Srbecky8cd54542018-07-15 23:58:44 +0100263// Method indices are not very dedup friendly.
264// Separating them greatly improves dedup efficiency of the other tables.
265class MethodInfo : public BitTableAccessor<1> {
266 public:
David Srbecky42deda82018-08-10 11:23:27 +0100267 BIT_TABLE_HEADER(MethodInfo)
David Srbecky8cd54542018-07-15 23:58:44 +0100268 BIT_TABLE_COLUMN(0, MethodIndex)
269};
270
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100271/**
272 * Wrapper around all compiler information collected for a method.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100273 * See the Decode method at the end for the precise binary format.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100274 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100275class CodeInfo {
276 public:
David Srbeckyd1606412018-07-31 15:05:14 +0100277 class Deduper {
278 public:
279 explicit Deduper(std::vector<uint8_t>* output) : writer_(output) {
280 DCHECK_EQ(output->size(), 0u);
281 }
282
283 // Copy CodeInfo into output while de-duplicating the internal bit tables.
284 // It returns the byte offset of the copied CodeInfo within the output.
285 size_t Dedupe(const uint8_t* code_info);
286
287 private:
David Srbeckyd1606412018-07-31 15:05:14 +0100288 BitMemoryWriter<std::vector<uint8_t>> writer_;
289
290 // Deduplicate at BitTable level. The value is bit offset within the output.
291 std::map<BitMemoryRegion, uint32_t, BitMemoryRegion::Less> dedupe_map_;
292 };
293
David Srbecky0d4567f2019-05-30 22:45:40 +0100294 ALWAYS_INLINE CodeInfo() {}
295 ALWAYS_INLINE explicit CodeInfo(const uint8_t* data, size_t* num_read_bits = nullptr);
296 ALWAYS_INLINE explicit CodeInfo(const OatQuickMethodHeader* header);
David Srbecky6ee06e92018-07-25 21:45:54 +0100297
David Srbecky0d4567f2019-05-30 22:45:40 +0100298 // The following methods decode only part of the data.
299 static QuickMethodFrameInfo DecodeFrameInfo(const uint8_t* data);
300 static CodeInfo DecodeGcMasksOnly(const OatQuickMethodHeader* header);
301 static CodeInfo DecodeInlineInfoOnly(const OatQuickMethodHeader* header);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000302
David Srbecky93bd3612018-07-02 19:30:18 +0100303 ALWAYS_INLINE const BitTable<StackMap>& GetStackMaps() const {
304 return stack_maps_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100305 }
306
David Srbecky052f8ca2018-04-26 15:42:54 +0100307 ALWAYS_INLINE StackMap GetStackMapAt(size_t index) const {
David Srbeckycf7833e2018-06-14 16:45:22 +0100308 return stack_maps_.GetRow(index);
David Srbecky45aa5982016-03-18 02:15:09 +0000309 }
310
David Srbecky052f8ca2018-04-26 15:42:54 +0100311 BitMemoryRegion GetStackMask(size_t index) const {
David Srbecky4b59d102018-05-29 21:46:10 +0000312 return stack_masks_.GetBitMemoryRegion(index);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800313 }
314
David Srbecky052f8ca2018-04-26 15:42:54 +0100315 BitMemoryRegion GetStackMaskOf(const StackMap& stack_map) const {
David Srbecky4b59d102018-05-29 21:46:10 +0000316 uint32_t index = stack_map.GetStackMaskIndex();
317 return (index == StackMap::kNoValue) ? BitMemoryRegion() : GetStackMask(index);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800318 }
319
David Srbecky052f8ca2018-04-26 15:42:54 +0100320 uint32_t GetRegisterMaskOf(const StackMap& stack_map) const {
David Srbecky4b59d102018-05-29 21:46:10 +0000321 uint32_t index = stack_map.GetRegisterMaskIndex();
David Srbeckycf7833e2018-06-14 16:45:22 +0100322 return (index == StackMap::kNoValue) ? 0 : register_masks_.GetRow(index).GetMask();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100323 }
324
David Srbecky052f8ca2018-04-26 15:42:54 +0100325 uint32_t GetNumberOfLocationCatalogEntries() const {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100326 return dex_register_catalog_.NumRows();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000327 }
328
David Srbecky71ec1cc2018-05-18 15:57:25 +0100329 ALWAYS_INLINE DexRegisterLocation GetDexRegisterCatalogEntry(size_t index) const {
David Srbecky6de88332018-06-03 12:00:11 +0100330 return (index == StackMap::kNoValue)
331 ? DexRegisterLocation::None()
David Srbeckycf7833e2018-06-14 16:45:22 +0100332 : dex_register_catalog_.GetRow(index).GetLocation();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100333 }
334
David Srbecky93bd3612018-07-02 19:30:18 +0100335 bool HasInlineInfo() const {
336 return inline_infos_.NumRows() > 0;
337 }
338
David Srbecky052f8ca2018-04-26 15:42:54 +0100339 uint32_t GetNumberOfStackMaps() const {
340 return stack_maps_.NumRows();
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100341 }
342
David Srbecky8cd54542018-07-15 23:58:44 +0100343 uint32_t GetMethodIndexOf(InlineInfo inline_info) const {
344 return method_infos_.GetRow(inline_info.GetMethodInfoIndex()).GetMethodIndex();
345 }
346
David Srbeckyfd89b072018-06-03 12:00:22 +0100347 ALWAYS_INLINE DexRegisterMap GetDexRegisterMapOf(StackMap stack_map) const {
David Srbecky6de88332018-06-03 12:00:11 +0100348 if (stack_map.HasDexRegisterMap()) {
349 DexRegisterMap map(number_of_dex_registers_, DexRegisterLocation::Invalid());
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700350 DecodeDexRegisterMap(stack_map.Row(), /* first_dex_register= */ 0, &map);
David Srbecky6de88332018-06-03 12:00:11 +0100351 return map;
352 }
353 return DexRegisterMap(0, DexRegisterLocation::None());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100354 }
355
David Srbecky93bd3612018-07-02 19:30:18 +0100356 ALWAYS_INLINE DexRegisterMap GetInlineDexRegisterMapOf(StackMap stack_map,
357 InlineInfo inline_info) const {
David Srbecky6de88332018-06-03 12:00:11 +0100358 if (stack_map.HasDexRegisterMap()) {
David Srbecky93bd3612018-07-02 19:30:18 +0100359 DCHECK(stack_map.HasInlineInfoIndex());
360 uint32_t depth = inline_info.Row() - stack_map.GetInlineInfoIndex();
David Srbecky6de88332018-06-03 12:00:11 +0100361 // The register counts are commutative and include all outer levels.
362 // This allows us to determine the range [first, last) in just two lookups.
363 // If we are at depth 0 (the first inlinee), the count from the main method is used.
David Srbecky93bd3612018-07-02 19:30:18 +0100364 uint32_t first = (depth == 0)
365 ? number_of_dex_registers_
366 : inline_infos_.GetRow(inline_info.Row() - 1).GetNumberOfDexRegisters();
367 uint32_t last = inline_info.GetNumberOfDexRegisters();
David Srbecky6de88332018-06-03 12:00:11 +0100368 DexRegisterMap map(last - first, DexRegisterLocation::Invalid());
369 DecodeDexRegisterMap(stack_map.Row(), first, &map);
370 return map;
371 }
372 return DexRegisterMap(0, DexRegisterLocation::None());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100373 }
374
David Srbecky93bd3612018-07-02 19:30:18 +0100375 BitTableRange<InlineInfo> GetInlineInfosOf(StackMap stack_map) const {
David Srbecky052f8ca2018-04-26 15:42:54 +0100376 uint32_t index = stack_map.GetInlineInfoIndex();
David Srbecky6e69e522018-06-03 12:00:14 +0100377 if (index != StackMap::kNoValue) {
David Srbecky93bd3612018-07-02 19:30:18 +0100378 auto begin = inline_infos_.begin() + index;
379 auto end = begin;
380 while ((*end++).GetIsLast() == InlineInfo::kMore) { }
381 return BitTableRange<InlineInfo>(begin, end);
382 } else {
383 return BitTableRange<InlineInfo>();
David Srbecky6e69e522018-06-03 12:00:14 +0100384 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100385 }
386
David Srbecky052f8ca2018-04-26 15:42:54 +0100387 StackMap GetStackMapForDexPc(uint32_t dex_pc) const {
David Srbecky93bd3612018-07-02 19:30:18 +0100388 for (StackMap stack_map : stack_maps_) {
David Srbecky50fac062018-06-13 18:55:35 +0100389 if (stack_map.GetDexPc() == dex_pc && stack_map.GetKind() != StackMap::Kind::Debug) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100390 return stack_map;
391 }
392 }
David Srbeckya45a85c2018-06-21 16:03:12 +0100393 return stack_maps_.GetInvalidRow();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100394 }
395
David Srbecky50fac062018-06-13 18:55:35 +0100396 // Searches the stack map list backwards because catch stack maps are stored at the end.
David Srbecky052f8ca2018-04-26 15:42:54 +0100397 StackMap GetCatchStackMapForDexPc(uint32_t dex_pc) const {
398 for (size_t i = GetNumberOfStackMaps(); i > 0; --i) {
399 StackMap stack_map = GetStackMapAt(i - 1);
David Srbecky50fac062018-06-13 18:55:35 +0100400 if (stack_map.GetDexPc() == dex_pc && stack_map.GetKind() == StackMap::Kind::Catch) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000401 return stack_map;
402 }
403 }
David Srbeckya45a85c2018-06-21 16:03:12 +0100404 return stack_maps_.GetInvalidRow();
David Brazdil77a48ae2015-09-15 12:34:04 +0000405 }
406
David Srbecky052f8ca2018-04-26 15:42:54 +0100407 StackMap GetOsrStackMapForDexPc(uint32_t dex_pc) const {
David Srbecky93bd3612018-07-02 19:30:18 +0100408 for (StackMap stack_map : stack_maps_) {
David Srbecky50fac062018-06-13 18:55:35 +0100409 if (stack_map.GetDexPc() == dex_pc && stack_map.GetKind() == StackMap::Kind::OSR) {
410 return stack_map;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000411 }
412 }
David Srbeckya45a85c2018-06-21 16:03:12 +0100413 return stack_maps_.GetInvalidRow();
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000414 }
415
Eric Holkf1e1dd12020-08-21 15:38:12 -0700416 StackMap GetStackMapForNativePcOffset(uintptr_t pc, InstructionSet isa = kRuntimeISA) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100417
David Srbecky71ec1cc2018-05-18 15:57:25 +0100418 // Dump this CodeInfo object on `vios`.
419 // `code_offset` is the (absolute) native PC of the compiled method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100420 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100421 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100422 bool verbose,
David Srbecky8cd54542018-07-15 23:58:44 +0100423 InstructionSet instruction_set) const;
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000424
David Srbecky86decb62018-06-05 06:41:10 +0100425 // Accumulate code info size statistics into the given Stats tree.
David Srbecky42deda82018-08-10 11:23:27 +0100426 static void CollectSizeStats(const uint8_t* code_info, /*out*/ Stats* parent);
David Srbecky86decb62018-06-05 06:41:10 +0100427
David Srbeckye42a4b92019-05-26 00:10:25 +0100428 ALWAYS_INLINE static bool HasInlineInfo(const uint8_t* code_info_data) {
429 return (*code_info_data & kHasInlineInfo) != 0;
430 }
431
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000432 ALWAYS_INLINE static bool IsBaseline(const uint8_t* code_info_data) {
433 return (*code_info_data & kIsBaseline) != 0;
434 }
435
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100436 private:
David Srbecky6de88332018-06-03 12:00:11 +0100437 // Scan backward to determine dex register locations at given stack map.
438 void DecodeDexRegisterMap(uint32_t stack_map_index,
439 uint32_t first_dex_register,
440 /*out*/ DexRegisterMap* map) const;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000441
David Srbecky0d4567f2019-05-30 22:45:40 +0100442 template<typename DecodeCallback> // (size_t index, BitTable<...>*, BitMemoryRegion).
443 ALWAYS_INLINE CodeInfo(const uint8_t* data, size_t* num_read_bits, DecodeCallback callback);
David Srbecky052f8ca2018-04-26 15:42:54 +0100444
David Srbecky697c47a2019-06-16 21:53:07 +0100445 // Invokes the callback with index and member pointer of each header field.
David Srbecky42deda82018-08-10 11:23:27 +0100446 template<typename Callback>
447 ALWAYS_INLINE static void ForEachHeaderField(Callback callback) {
David Srbecky697c47a2019-06-16 21:53:07 +0100448 size_t index = 0;
449 callback(index++, &CodeInfo::flags_);
450 callback(index++, &CodeInfo::packed_frame_size_);
451 callback(index++, &CodeInfo::core_spill_mask_);
452 callback(index++, &CodeInfo::fp_spill_mask_);
453 callback(index++, &CodeInfo::number_of_dex_registers_);
454 callback(index++, &CodeInfo::bit_table_flags_);
455 DCHECK_EQ(index, kNumHeaders);
David Srbecky42deda82018-08-10 11:23:27 +0100456 }
457
David Srbecky697c47a2019-06-16 21:53:07 +0100458 // Invokes the callback with index and member pointer of each BitTable field.
David Srbecky42deda82018-08-10 11:23:27 +0100459 template<typename Callback>
David Srbecky0d4567f2019-05-30 22:45:40 +0100460 ALWAYS_INLINE static void ForEachBitTableField(Callback callback) {
David Srbecky697c47a2019-06-16 21:53:07 +0100461 size_t index = 0;
462 callback(index++, &CodeInfo::stack_maps_);
463 callback(index++, &CodeInfo::register_masks_);
464 callback(index++, &CodeInfo::stack_masks_);
David Srbecky697c47a2019-06-16 21:53:07 +0100465 callback(index++, &CodeInfo::inline_infos_);
466 callback(index++, &CodeInfo::method_infos_);
David Srbecky697c47a2019-06-16 21:53:07 +0100467 callback(index++, &CodeInfo::dex_register_masks_);
468 callback(index++, &CodeInfo::dex_register_maps_);
469 callback(index++, &CodeInfo::dex_register_catalog_);
470 DCHECK_EQ(index, kNumBitTables);
David Srbecky42deda82018-08-10 11:23:27 +0100471 }
472
David Srbecky697c47a2019-06-16 21:53:07 +0100473 bool HasBitTable(size_t i) { return ((bit_table_flags_ >> i) & 1) != 0; }
474 bool IsBitTableDeduped(size_t i) { return ((bit_table_flags_ >> (kNumBitTables + i)) & 1) != 0; }
475 void SetBitTableDeduped(size_t i) { bit_table_flags_ |= 1 << (kNumBitTables + i); }
476
David Srbeckye42a4b92019-05-26 00:10:25 +0100477 enum Flags {
478 kHasInlineInfo = 1 << 0,
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000479 kIsBaseline = 1 << 1,
David Srbeckye42a4b92019-05-26 00:10:25 +0100480 };
481
David Srbecky697c47a2019-06-16 21:53:07 +0100482 // The CodeInfo starts with sequence of variable-length bit-encoded integers.
483 static constexpr size_t kNumHeaders = 6;
David Srbeckye42a4b92019-05-26 00:10:25 +0100484 uint32_t flags_ = 0;
David Srbecky2259f1c2019-01-16 23:18:30 +0000485 uint32_t packed_frame_size_ = 0; // Frame size in kStackAlignment units.
486 uint32_t core_spill_mask_ = 0;
487 uint32_t fp_spill_mask_ = 0;
488 uint32_t number_of_dex_registers_ = 0;
David Srbecky697c47a2019-06-16 21:53:07 +0100489 uint32_t bit_table_flags_ = 0;
490
491 // The encoded bit-tables follow the header. Based on the above flags field,
492 // bit-tables might be omitted or replaced by relative bit-offset if deduped.
493 static constexpr size_t kNumBitTables = 8;
David Srbeckycf7833e2018-06-14 16:45:22 +0100494 BitTable<StackMap> stack_maps_;
495 BitTable<RegisterMask> register_masks_;
David Srbecky42deda82018-08-10 11:23:27 +0100496 BitTable<StackMask> stack_masks_;
David Srbeckya2d29a32018-08-03 11:06:38 +0100497 BitTable<InlineInfo> inline_infos_;
498 BitTable<MethodInfo> method_infos_;
David Srbecky42deda82018-08-10 11:23:27 +0100499 BitTable<DexRegisterMask> dex_register_masks_;
David Srbeckycf7833e2018-06-14 16:45:22 +0100500 BitTable<DexRegisterMapInfo> dex_register_maps_;
501 BitTable<DexRegisterInfo> dex_register_catalog_;
David Srbecky697c47a2019-06-16 21:53:07 +0100502
David Srbecky67ba8722019-05-23 15:32:18 +0100503 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100504};
505
Roland Levillain1c1da432015-07-16 11:54:44 +0100506#undef ELEMENT_BYTE_OFFSET_AFTER
507#undef ELEMENT_BIT_OFFSET_AFTER
508
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100509} // namespace art
510
511#endif // ART_RUNTIME_STACK_MAP_H_