blob: 7a13dbd3ac7141bf5bba8b7d6fdf9a7a49573117 [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"
David Sehr1ce2b3b2018-04-05 11:02:03 -070026#include "base/memory_region.h"
David Sehr9e734c72018-01-04 17:56:19 -080027#include "dex/dex_file_types.h"
David Srbecky71ec1cc2018-05-18 15:57:25 +010028#include "dex_register_location.h"
David Srbeckyf6ba5b32018-06-23 22:05:49 +010029#include "quick/quick_method_frame_info.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010030
31namespace art {
32
Vladimir Markoe4ccbb52022-03-15 08:51:33 +000033namespace linker {
34class CodeInfoTableDeduper;
35} // namespace linker
36
David Srbeckyf6ba5b32018-06-23 22:05:49 +010037class OatQuickMethodHeader;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +010038class VariableIndentationOutputStream;
39
Roland Levillaina2d8ec62015-03-12 15:25:29 +000040// Size of a frame slot, in bytes. This constant is a signed value,
41// to please the compiler in arithmetic operations involving int32_t
42// (signed) values.
Roland Levillaina552e1c2015-03-26 15:01:03 +000043static constexpr ssize_t kFrameSlotSize = 4;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000044
David Srbecky6de88332018-06-03 12:00:11 +010045// The delta compression of dex register maps means we need to scan the stackmaps backwards.
46// We compress the data in such a way so that there is an upper bound on the search distance.
47// Max distance 0 means each stack map must be fully defined and no scanning back is allowed.
48// If this value is changed, the oat file version should be incremented (for DCHECK to pass).
49static constexpr size_t kMaxDexRegisterMapSearchDistance = 32;
50
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000051class ArtMethod;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000052class CodeInfo;
David Srbecky86decb62018-06-05 06:41:10 +010053class Stats;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000054
David Srbecky71ec1cc2018-05-18 15:57:25 +010055std::ostream& operator<<(std::ostream& stream, const DexRegisterLocation& reg);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010056
David Srbecky71ec1cc2018-05-18 15:57:25 +010057// Information on Dex register locations for a specific PC.
58// Effectively just a convenience wrapper for DexRegisterLocation vector.
59// If the size is small enough, it keeps the data on the stack.
David Srbeckye1402122018-06-13 18:20:45 +010060// TODO: Replace this with generic purpose "small-vector" implementation.
Roland Levillaina552e1c2015-03-26 15:01:03 +000061class DexRegisterMap {
62 public:
David Srbecky6de88332018-06-03 12:00:11 +010063 using iterator = DexRegisterLocation*;
David Srbeckye1402122018-06-13 18:20:45 +010064 using const_iterator = const DexRegisterLocation*;
David Srbecky6de88332018-06-03 12:00:11 +010065
66 // Create map for given number of registers and initialize them to the given value.
67 DexRegisterMap(size_t count, DexRegisterLocation value) : count_(count), regs_small_{} {
David Srbecky71ec1cc2018-05-18 15:57:25 +010068 if (count_ <= kSmallCount) {
David Srbecky6de88332018-06-03 12:00:11 +010069 std::fill_n(regs_small_.begin(), count, value);
David Srbecky71ec1cc2018-05-18 15:57:25 +010070 } else {
David Srbecky6de88332018-06-03 12:00:11 +010071 regs_large_.resize(count, value);
David Srbecky71ec1cc2018-05-18 15:57:25 +010072 }
Roland Levillaina552e1c2015-03-26 15:01:03 +000073 }
74
David Srbecky71ec1cc2018-05-18 15:57:25 +010075 DexRegisterLocation* data() {
76 return count_ <= kSmallCount ? regs_small_.data() : regs_large_.data();
77 }
David Srbeckye1402122018-06-13 18:20:45 +010078 const DexRegisterLocation* data() const {
79 return count_ <= kSmallCount ? regs_small_.data() : regs_large_.data();
80 }
Roland Levillaina552e1c2015-03-26 15:01:03 +000081
David Srbecky6de88332018-06-03 12:00:11 +010082 iterator begin() { return data(); }
83 iterator end() { return data() + count_; }
David Srbeckye1402122018-06-13 18:20:45 +010084 const_iterator begin() const { return data(); }
85 const_iterator end() const { return data() + count_; }
David Srbecky71ec1cc2018-05-18 15:57:25 +010086 size_t size() const { return count_; }
David Srbeckyfd89b072018-06-03 12:00:22 +010087 bool empty() const { return count_ == 0; }
David Srbecky71ec1cc2018-05-18 15:57:25 +010088
David Srbeckye1402122018-06-13 18:20:45 +010089 DexRegisterLocation& operator[](size_t index) {
David Srbecky71ec1cc2018-05-18 15:57:25 +010090 DCHECK_LT(index, count_);
David Srbeckye1402122018-06-13 18:20:45 +010091 return data()[index];
David Srbecky71ec1cc2018-05-18 15:57:25 +010092 }
David Srbeckye1402122018-06-13 18:20:45 +010093 const DexRegisterLocation& operator[](size_t index) const {
94 DCHECK_LT(index, count_);
95 return data()[index];
Roland Levillaina552e1c2015-03-26 15:01:03 +000096 }
97
David Srbecky71ec1cc2018-05-18 15:57:25 +010098 size_t GetNumberOfLiveDexRegisters() const {
David Srbeckye1402122018-06-13 18:20:45 +010099 return std::count_if(begin(), end(), [](auto& loc) { return loc.IsLive(); });
Roland Levillaina552e1c2015-03-26 15:01:03 +0000100 }
101
David Srbecky71ec1cc2018-05-18 15:57:25 +0100102 bool HasAnyLiveDexRegisters() const {
David Srbeckye1402122018-06-13 18:20:45 +0100103 return std::any_of(begin(), end(), [](auto& loc) { return loc.IsLive(); });
David Srbecky21d45b42018-05-30 06:35:05 +0100104 }
105
David Srbeckye1402122018-06-13 18:20:45 +0100106 void Dump(VariableIndentationOutputStream* vios) const;
107
Roland Levillaina552e1c2015-03-26 15:01:03 +0000108 private:
David Srbecky71ec1cc2018-05-18 15:57:25 +0100109 // Store the data inline if the number of registers is small to avoid memory allocations.
110 // If count_ <= kSmallCount, we use the regs_small_ array, and regs_large_ otherwise.
111 static constexpr size_t kSmallCount = 16;
112 size_t count_;
113 std::array<DexRegisterLocation, kSmallCount> regs_small_;
114 dchecked_vector<DexRegisterLocation> regs_large_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000115};
116
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100117/**
118 * A Stack Map holds compilation information for a specific PC necessary for:
119 * - Mapping it to a dex PC,
120 * - Knowing which stack entries are objects,
121 * - Knowing which registers hold objects,
122 * - Knowing the inlining information,
123 * - Knowing the values of dex registers.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100124 */
David Srbeckycf7833e2018-06-14 16:45:22 +0100125class StackMap : public BitTableAccessor<8> {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100126 public:
David Srbecky50fac062018-06-13 18:55:35 +0100127 enum Kind {
128 Default = -1,
129 Catch = 0,
130 OSR = 1,
131 Debug = 2,
132 };
David Srbecky42deda82018-08-10 11:23:27 +0100133 BIT_TABLE_HEADER(StackMap)
David Srbecky50fac062018-06-13 18:55:35 +0100134 BIT_TABLE_COLUMN(0, Kind)
135 BIT_TABLE_COLUMN(1, PackedNativePc)
136 BIT_TABLE_COLUMN(2, DexPc)
137 BIT_TABLE_COLUMN(3, RegisterMaskIndex)
138 BIT_TABLE_COLUMN(4, StackMaskIndex)
139 BIT_TABLE_COLUMN(5, InlineInfoIndex)
140 BIT_TABLE_COLUMN(6, DexRegisterMaskIndex)
141 BIT_TABLE_COLUMN(7, DexRegisterMapIndex)
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100142
David Srbecky052f8ca2018-04-26 15:42:54 +0100143 ALWAYS_INLINE uint32_t GetNativePcOffset(InstructionSet instruction_set) const {
David Srbeckycf7833e2018-06-14 16:45:22 +0100144 return UnpackNativePc(GetPackedNativePc(), instruction_set);
David Brazdilf677ebf2015-05-29 16:29:43 +0100145 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100146
David Srbeckyd97e0822018-06-03 12:00:24 +0100147 ALWAYS_INLINE bool HasInlineInfo() const {
148 return HasInlineInfoIndex();
149 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100150
David Srbeckyd97e0822018-06-03 12:00:24 +0100151 ALWAYS_INLINE bool HasDexRegisterMap() const {
152 return HasDexRegisterMapIndex();
153 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100154
David Srbeckyd02b23f2018-05-29 23:27:22 +0100155 static uint32_t PackNativePc(uint32_t native_pc, InstructionSet isa) {
David Srbeckyd775f962018-05-30 18:12:52 +0100156 DCHECK_ALIGNED_PARAM(native_pc, GetInstructionSetInstructionAlignment(isa));
David Srbeckyd02b23f2018-05-29 23:27:22 +0100157 return native_pc / GetInstructionSetInstructionAlignment(isa);
158 }
159
160 static uint32_t UnpackNativePc(uint32_t packed_native_pc, InstructionSet isa) {
161 uint32_t native_pc = packed_native_pc * GetInstructionSetInstructionAlignment(isa);
162 DCHECK_EQ(native_pc / GetInstructionSetInstructionAlignment(isa), packed_native_pc);
163 return native_pc;
164 }
165
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100166 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100167 const CodeInfo& code_info,
168 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100169 InstructionSet instruction_set) const;
David Srbecky61b28a12016-02-25 21:55:03 +0000170};
171
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100172/**
David Srbecky052f8ca2018-04-26 15:42:54 +0100173 * Inline information for a specific PC.
174 * The row referenced from the StackMap holds information at depth 0.
175 * Following rows hold information for further depths.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100176 */
David Srbeckycf7833e2018-06-14 16:45:22 +0100177class InlineInfo : public BitTableAccessor<6> {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100178 public:
David Srbecky42deda82018-08-10 11:23:27 +0100179 BIT_TABLE_HEADER(InlineInfo)
David Srbeckyd97e0822018-06-03 12:00:24 +0100180 BIT_TABLE_COLUMN(0, IsLast) // Determines if there are further rows for further depths.
181 BIT_TABLE_COLUMN(1, DexPc)
182 BIT_TABLE_COLUMN(2, MethodInfoIndex)
183 BIT_TABLE_COLUMN(3, ArtMethodHi) // High bits of ArtMethod*.
184 BIT_TABLE_COLUMN(4, ArtMethodLo) // Low bits of ArtMethod*.
David Srbecky6de88332018-06-03 12:00:11 +0100185 BIT_TABLE_COLUMN(5, NumberOfDexRegisters) // Includes outer levels and the main method.
David Srbeckyd97e0822018-06-03 12:00:24 +0100186
David Srbecky052f8ca2018-04-26 15:42:54 +0100187 static constexpr uint32_t kLast = -1;
188 static constexpr uint32_t kMore = 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100189
David Srbecky6e69e522018-06-03 12:00:14 +0100190 bool EncodesArtMethod() const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100191 return HasArtMethodLo();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100192 }
193
David Srbecky6e69e522018-06-03 12:00:14 +0100194 ArtMethod* GetArtMethod() const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100195 uint64_t lo = GetArtMethodLo();
196 uint64_t hi = GetArtMethodHi();
David Srbecky71ec1cc2018-05-18 15:57:25 +0100197 return reinterpret_cast<ArtMethod*>((hi << 32) | lo);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100198 }
199
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100200 void Dump(VariableIndentationOutputStream* vios,
David Srbecky61b28a12016-02-25 21:55:03 +0000201 const CodeInfo& info,
David Srbecky8cd54542018-07-15 23:58:44 +0100202 const StackMap& stack_map) const;
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800203};
204
David Srbecky42deda82018-08-10 11:23:27 +0100205class StackMask : public BitTableAccessor<1> {
David Srbecky86decb62018-06-05 06:41:10 +0100206 public:
David Srbecky42deda82018-08-10 11:23:27 +0100207 BIT_TABLE_HEADER(StackMask)
208 BIT_TABLE_COLUMN(0, Mask)
209};
210
211class DexRegisterMask : public BitTableAccessor<1> {
212 public:
213 BIT_TABLE_HEADER(DexRegisterMask)
David Srbecky86decb62018-06-05 06:41:10 +0100214 BIT_TABLE_COLUMN(0, Mask)
215};
216
David Srbeckycf7833e2018-06-14 16:45:22 +0100217class DexRegisterMapInfo : public BitTableAccessor<1> {
David Srbecky86decb62018-06-05 06:41:10 +0100218 public:
David Srbecky42deda82018-08-10 11:23:27 +0100219 BIT_TABLE_HEADER(DexRegisterMapInfo)
David Srbecky86decb62018-06-05 06:41:10 +0100220 BIT_TABLE_COLUMN(0, CatalogueIndex)
221};
222
David Srbeckycf7833e2018-06-14 16:45:22 +0100223class DexRegisterInfo : public BitTableAccessor<2> {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100224 public:
David Srbecky42deda82018-08-10 11:23:27 +0100225 BIT_TABLE_HEADER(DexRegisterInfo)
David Srbeckyd97e0822018-06-03 12:00:24 +0100226 BIT_TABLE_COLUMN(0, Kind)
227 BIT_TABLE_COLUMN(1, PackedValue)
David Srbecky71ec1cc2018-05-18 15:57:25 +0100228
229 ALWAYS_INLINE DexRegisterLocation GetLocation() const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100230 DexRegisterLocation::Kind kind = static_cast<DexRegisterLocation::Kind>(GetKind());
231 return DexRegisterLocation(kind, UnpackValue(kind, GetPackedValue()));
David Srbecky71ec1cc2018-05-18 15:57:25 +0100232 }
233
234 static uint32_t PackValue(DexRegisterLocation::Kind kind, uint32_t value) {
235 uint32_t packed_value = value;
236 if (kind == DexRegisterLocation::Kind::kInStack) {
237 DCHECK(IsAligned<kFrameSlotSize>(packed_value));
238 packed_value /= kFrameSlotSize;
239 }
240 return packed_value;
241 }
242
243 static uint32_t UnpackValue(DexRegisterLocation::Kind kind, uint32_t packed_value) {
244 uint32_t value = packed_value;
245 if (kind == DexRegisterLocation::Kind::kInStack) {
246 value *= kFrameSlotSize;
247 }
248 return value;
249 }
250};
251
David Srbecky4b59d102018-05-29 21:46:10 +0000252// Register masks tend to have many trailing zero bits (caller-saves are usually not encoded),
253// therefore it is worth encoding the mask as value+shift.
David Srbeckycf7833e2018-06-14 16:45:22 +0100254class RegisterMask : public BitTableAccessor<2> {
David Srbecky4b59d102018-05-29 21:46:10 +0000255 public:
David Srbecky42deda82018-08-10 11:23:27 +0100256 BIT_TABLE_HEADER(RegisterMask)
David Srbeckyd97e0822018-06-03 12:00:24 +0100257 BIT_TABLE_COLUMN(0, Value)
258 BIT_TABLE_COLUMN(1, Shift)
David Srbecky4b59d102018-05-29 21:46:10 +0000259
260 ALWAYS_INLINE uint32_t GetMask() const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100261 return GetValue() << GetShift();
David Srbecky4b59d102018-05-29 21:46:10 +0000262 }
263};
264
David Srbecky8cd54542018-07-15 23:58:44 +0100265// Method indices are not very dedup friendly.
266// Separating them greatly improves dedup efficiency of the other tables.
Santiago Aboy Solanes970ba212021-10-21 10:52:47 +0100267class MethodInfo : public BitTableAccessor<3> {
David Srbecky8cd54542018-07-15 23:58:44 +0100268 public:
David Srbecky42deda82018-08-10 11:23:27 +0100269 BIT_TABLE_HEADER(MethodInfo)
David Srbecky8cd54542018-07-15 23:58:44 +0100270 BIT_TABLE_COLUMN(0, MethodIndex)
Santiago Aboy Solanes970ba212021-10-21 10:52:47 +0100271 BIT_TABLE_COLUMN(1, DexFileIndexKind)
272 BIT_TABLE_COLUMN(2, DexFileIndex)
273
274 static constexpr uint32_t kKindNonBCP = -1;
275 static constexpr uint32_t kKindBCP = 0;
Santiago Aboy Solanese43aa3f2021-11-01 09:02:09 +0000276
277 static constexpr uint32_t kSameDexFile = -1;
David Srbecky8cd54542018-07-15 23:58:44 +0100278};
279
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100280/**
281 * Wrapper around all compiler information collected for a method.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100282 * See the Decode method at the end for the precise binary format.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100283 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100284class CodeInfo {
285 public:
David Srbecky0d4567f2019-05-30 22:45:40 +0100286 ALWAYS_INLINE CodeInfo() {}
287 ALWAYS_INLINE explicit CodeInfo(const uint8_t* data, size_t* num_read_bits = nullptr);
288 ALWAYS_INLINE explicit CodeInfo(const OatQuickMethodHeader* header);
David Srbecky6ee06e92018-07-25 21:45:54 +0100289
David Srbecky0d4567f2019-05-30 22:45:40 +0100290 // The following methods decode only part of the data.
David Srbecky0d4567f2019-05-30 22:45:40 +0100291 static CodeInfo DecodeGcMasksOnly(const OatQuickMethodHeader* header);
292 static CodeInfo DecodeInlineInfoOnly(const OatQuickMethodHeader* header);
David Srbecky0983f592021-04-08 16:36:19 +0100293
294 ALWAYS_INLINE static uint32_t DecodeCodeSize(const uint8_t* code_info_data) {
295 return DecodeHeaderOnly(code_info_data).code_size_;
296 }
297
298 ALWAYS_INLINE static QuickMethodFrameInfo DecodeFrameInfo(const uint8_t* code_info_data) {
299 CodeInfo code_info = DecodeHeaderOnly(code_info_data);
300 return QuickMethodFrameInfo(code_info.packed_frame_size_ * kStackAlignment,
301 code_info.core_spill_mask_,
302 code_info.fp_spill_mask_);
303 }
304
305 ALWAYS_INLINE static CodeInfo DecodeHeaderOnly(const uint8_t* code_info_data) {
306 CodeInfo code_info;
307 BitMemoryReader reader(code_info_data);
308 std::array<uint32_t, kNumHeaders> header = reader.ReadInterleavedVarints<kNumHeaders>();
309 ForEachHeaderField([&code_info, &header](size_t i, auto member_pointer) {
310 code_info.*member_pointer = header[i];
311 });
312 return code_info;
313 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000314
David Srbecky93bd3612018-07-02 19:30:18 +0100315 ALWAYS_INLINE const BitTable<StackMap>& GetStackMaps() const {
316 return stack_maps_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100317 }
318
David Srbecky052f8ca2018-04-26 15:42:54 +0100319 ALWAYS_INLINE StackMap GetStackMapAt(size_t index) const {
David Srbeckycf7833e2018-06-14 16:45:22 +0100320 return stack_maps_.GetRow(index);
David Srbecky45aa5982016-03-18 02:15:09 +0000321 }
322
David Srbecky052f8ca2018-04-26 15:42:54 +0100323 BitMemoryRegion GetStackMask(size_t index) const {
David Srbecky4b59d102018-05-29 21:46:10 +0000324 return stack_masks_.GetBitMemoryRegion(index);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800325 }
326
David Srbecky052f8ca2018-04-26 15:42:54 +0100327 BitMemoryRegion GetStackMaskOf(const StackMap& stack_map) const {
David Srbecky4b59d102018-05-29 21:46:10 +0000328 uint32_t index = stack_map.GetStackMaskIndex();
329 return (index == StackMap::kNoValue) ? BitMemoryRegion() : GetStackMask(index);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800330 }
331
David Srbecky052f8ca2018-04-26 15:42:54 +0100332 uint32_t GetRegisterMaskOf(const StackMap& stack_map) const {
David Srbecky4b59d102018-05-29 21:46:10 +0000333 uint32_t index = stack_map.GetRegisterMaskIndex();
David Srbeckycf7833e2018-06-14 16:45:22 +0100334 return (index == StackMap::kNoValue) ? 0 : register_masks_.GetRow(index).GetMask();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100335 }
336
David Srbecky052f8ca2018-04-26 15:42:54 +0100337 uint32_t GetNumberOfLocationCatalogEntries() const {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100338 return dex_register_catalog_.NumRows();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000339 }
340
David Srbecky71ec1cc2018-05-18 15:57:25 +0100341 ALWAYS_INLINE DexRegisterLocation GetDexRegisterCatalogEntry(size_t index) const {
David Srbecky6de88332018-06-03 12:00:11 +0100342 return (index == StackMap::kNoValue)
343 ? DexRegisterLocation::None()
David Srbeckycf7833e2018-06-14 16:45:22 +0100344 : dex_register_catalog_.GetRow(index).GetLocation();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100345 }
346
David Srbecky93bd3612018-07-02 19:30:18 +0100347 bool HasInlineInfo() const {
348 return inline_infos_.NumRows() > 0;
349 }
350
David Srbecky052f8ca2018-04-26 15:42:54 +0100351 uint32_t GetNumberOfStackMaps() const {
352 return stack_maps_.NumRows();
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100353 }
354
Santiago Aboy Solanese43aa3f2021-11-01 09:02:09 +0000355 MethodInfo GetMethodInfoOf(InlineInfo inline_info) const {
356 return method_infos_.GetRow(inline_info.GetMethodInfoIndex());
357 }
358
David Srbecky8cd54542018-07-15 23:58:44 +0100359 uint32_t GetMethodIndexOf(InlineInfo inline_info) const {
Santiago Aboy Solanese43aa3f2021-11-01 09:02:09 +0000360 return GetMethodInfoOf(inline_info).GetMethodIndex();
David Srbecky8cd54542018-07-15 23:58:44 +0100361 }
362
David Srbeckyfd89b072018-06-03 12:00:22 +0100363 ALWAYS_INLINE DexRegisterMap GetDexRegisterMapOf(StackMap stack_map) const {
David Srbecky6de88332018-06-03 12:00:11 +0100364 if (stack_map.HasDexRegisterMap()) {
365 DexRegisterMap map(number_of_dex_registers_, DexRegisterLocation::Invalid());
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700366 DecodeDexRegisterMap(stack_map.Row(), /* first_dex_register= */ 0, &map);
David Srbecky6de88332018-06-03 12:00:11 +0100367 return map;
368 }
369 return DexRegisterMap(0, DexRegisterLocation::None());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100370 }
371
David Srbecky93bd3612018-07-02 19:30:18 +0100372 ALWAYS_INLINE DexRegisterMap GetInlineDexRegisterMapOf(StackMap stack_map,
373 InlineInfo inline_info) const {
David Srbecky6de88332018-06-03 12:00:11 +0100374 if (stack_map.HasDexRegisterMap()) {
David Srbecky93bd3612018-07-02 19:30:18 +0100375 DCHECK(stack_map.HasInlineInfoIndex());
376 uint32_t depth = inline_info.Row() - stack_map.GetInlineInfoIndex();
David Srbecky6de88332018-06-03 12:00:11 +0100377 // The register counts are commutative and include all outer levels.
378 // This allows us to determine the range [first, last) in just two lookups.
379 // If we are at depth 0 (the first inlinee), the count from the main method is used.
David Srbecky93bd3612018-07-02 19:30:18 +0100380 uint32_t first = (depth == 0)
381 ? number_of_dex_registers_
382 : inline_infos_.GetRow(inline_info.Row() - 1).GetNumberOfDexRegisters();
383 uint32_t last = inline_info.GetNumberOfDexRegisters();
David Srbecky6de88332018-06-03 12:00:11 +0100384 DexRegisterMap map(last - first, DexRegisterLocation::Invalid());
385 DecodeDexRegisterMap(stack_map.Row(), first, &map);
386 return map;
387 }
388 return DexRegisterMap(0, DexRegisterLocation::None());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100389 }
390
David Srbecky93bd3612018-07-02 19:30:18 +0100391 BitTableRange<InlineInfo> GetInlineInfosOf(StackMap stack_map) const {
David Srbecky052f8ca2018-04-26 15:42:54 +0100392 uint32_t index = stack_map.GetInlineInfoIndex();
David Srbecky6e69e522018-06-03 12:00:14 +0100393 if (index != StackMap::kNoValue) {
David Srbecky93bd3612018-07-02 19:30:18 +0100394 auto begin = inline_infos_.begin() + index;
395 auto end = begin;
396 while ((*end++).GetIsLast() == InlineInfo::kMore) { }
397 return BitTableRange<InlineInfo>(begin, end);
398 } else {
399 return BitTableRange<InlineInfo>();
David Srbecky6e69e522018-06-03 12:00:14 +0100400 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100401 }
402
David Srbecky052f8ca2018-04-26 15:42:54 +0100403 StackMap GetStackMapForDexPc(uint32_t dex_pc) const {
David Srbecky93bd3612018-07-02 19:30:18 +0100404 for (StackMap stack_map : stack_maps_) {
David Srbecky50fac062018-06-13 18:55:35 +0100405 if (stack_map.GetDexPc() == dex_pc && stack_map.GetKind() != StackMap::Kind::Debug) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100406 return stack_map;
407 }
408 }
David Srbeckya45a85c2018-06-21 16:03:12 +0100409 return stack_maps_.GetInvalidRow();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100410 }
411
David Srbecky50fac062018-06-13 18:55:35 +0100412 // Searches the stack map list backwards because catch stack maps are stored at the end.
David Srbecky052f8ca2018-04-26 15:42:54 +0100413 StackMap GetCatchStackMapForDexPc(uint32_t dex_pc) const {
414 for (size_t i = GetNumberOfStackMaps(); i > 0; --i) {
415 StackMap stack_map = GetStackMapAt(i - 1);
David Srbecky50fac062018-06-13 18:55:35 +0100416 if (stack_map.GetDexPc() == dex_pc && stack_map.GetKind() == StackMap::Kind::Catch) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000417 return stack_map;
418 }
419 }
David Srbeckya45a85c2018-06-21 16:03:12 +0100420 return stack_maps_.GetInvalidRow();
David Brazdil77a48ae2015-09-15 12:34:04 +0000421 }
422
David Srbecky052f8ca2018-04-26 15:42:54 +0100423 StackMap GetOsrStackMapForDexPc(uint32_t dex_pc) const {
David Srbecky93bd3612018-07-02 19:30:18 +0100424 for (StackMap stack_map : stack_maps_) {
David Srbecky50fac062018-06-13 18:55:35 +0100425 if (stack_map.GetDexPc() == dex_pc && stack_map.GetKind() == StackMap::Kind::OSR) {
426 return stack_map;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000427 }
428 }
David Srbeckya45a85c2018-06-21 16:03:12 +0100429 return stack_maps_.GetInvalidRow();
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000430 }
431
Eric Holkf1e1dd12020-08-21 15:38:12 -0700432 StackMap GetStackMapForNativePcOffset(uintptr_t pc, InstructionSet isa = kRuntimeISA) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100433
David Srbecky71ec1cc2018-05-18 15:57:25 +0100434 // Dump this CodeInfo object on `vios`.
435 // `code_offset` is the (absolute) native PC of the compiled method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100436 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100437 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100438 bool verbose,
David Srbecky8cd54542018-07-15 23:58:44 +0100439 InstructionSet instruction_set) const;
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000440
David Srbecky86decb62018-06-05 06:41:10 +0100441 // Accumulate code info size statistics into the given Stats tree.
David Srbecky81b1d782021-03-07 21:33:28 +0000442 static void CollectSizeStats(const uint8_t* code_info, /*out*/ Stats& parent);
David Srbecky86decb62018-06-05 06:41:10 +0100443
David Srbeckye42a4b92019-05-26 00:10:25 +0100444 ALWAYS_INLINE static bool HasInlineInfo(const uint8_t* code_info_data) {
445 return (*code_info_data & kHasInlineInfo) != 0;
446 }
447
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000448 ALWAYS_INLINE static bool IsBaseline(const uint8_t* code_info_data) {
449 return (*code_info_data & kIsBaseline) != 0;
450 }
451
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100452 private:
David Srbecky6de88332018-06-03 12:00:11 +0100453 // Scan backward to determine dex register locations at given stack map.
454 void DecodeDexRegisterMap(uint32_t stack_map_index,
455 uint32_t first_dex_register,
456 /*out*/ DexRegisterMap* map) const;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000457
David Srbecky0d4567f2019-05-30 22:45:40 +0100458 template<typename DecodeCallback> // (size_t index, BitTable<...>*, BitMemoryRegion).
459 ALWAYS_INLINE CodeInfo(const uint8_t* data, size_t* num_read_bits, DecodeCallback callback);
David Srbecky052f8ca2018-04-26 15:42:54 +0100460
David Srbecky697c47a2019-06-16 21:53:07 +0100461 // Invokes the callback with index and member pointer of each header field.
David Srbecky42deda82018-08-10 11:23:27 +0100462 template<typename Callback>
463 ALWAYS_INLINE static void ForEachHeaderField(Callback callback) {
David Srbecky697c47a2019-06-16 21:53:07 +0100464 size_t index = 0;
465 callback(index++, &CodeInfo::flags_);
David Srbecky17b4d2b2021-03-02 18:14:31 +0000466 callback(index++, &CodeInfo::code_size_);
David Srbecky697c47a2019-06-16 21:53:07 +0100467 callback(index++, &CodeInfo::packed_frame_size_);
468 callback(index++, &CodeInfo::core_spill_mask_);
469 callback(index++, &CodeInfo::fp_spill_mask_);
470 callback(index++, &CodeInfo::number_of_dex_registers_);
471 callback(index++, &CodeInfo::bit_table_flags_);
472 DCHECK_EQ(index, kNumHeaders);
David Srbecky42deda82018-08-10 11:23:27 +0100473 }
474
David Srbecky697c47a2019-06-16 21:53:07 +0100475 // Invokes the callback with index and member pointer of each BitTable field.
David Srbecky42deda82018-08-10 11:23:27 +0100476 template<typename Callback>
David Srbecky0d4567f2019-05-30 22:45:40 +0100477 ALWAYS_INLINE static void ForEachBitTableField(Callback callback) {
David Srbecky697c47a2019-06-16 21:53:07 +0100478 size_t index = 0;
479 callback(index++, &CodeInfo::stack_maps_);
480 callback(index++, &CodeInfo::register_masks_);
481 callback(index++, &CodeInfo::stack_masks_);
David Srbecky697c47a2019-06-16 21:53:07 +0100482 callback(index++, &CodeInfo::inline_infos_);
483 callback(index++, &CodeInfo::method_infos_);
David Srbecky697c47a2019-06-16 21:53:07 +0100484 callback(index++, &CodeInfo::dex_register_masks_);
485 callback(index++, &CodeInfo::dex_register_maps_);
486 callback(index++, &CodeInfo::dex_register_catalog_);
487 DCHECK_EQ(index, kNumBitTables);
David Srbecky42deda82018-08-10 11:23:27 +0100488 }
489
David Srbecky697c47a2019-06-16 21:53:07 +0100490 bool HasBitTable(size_t i) { return ((bit_table_flags_ >> i) & 1) != 0; }
491 bool IsBitTableDeduped(size_t i) { return ((bit_table_flags_ >> (kNumBitTables + i)) & 1) != 0; }
492 void SetBitTableDeduped(size_t i) { bit_table_flags_ |= 1 << (kNumBitTables + i); }
Vladimir Markoe4ccbb52022-03-15 08:51:33 +0000493 bool HasDedupedBitTables() { return (bit_table_flags_ >> kNumBitTables) != 0u; }
David Srbecky697c47a2019-06-16 21:53:07 +0100494
David Srbeckye42a4b92019-05-26 00:10:25 +0100495 enum Flags {
496 kHasInlineInfo = 1 << 0,
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000497 kIsBaseline = 1 << 1,
David Srbeckye42a4b92019-05-26 00:10:25 +0100498 };
499
David Srbecky697c47a2019-06-16 21:53:07 +0100500 // The CodeInfo starts with sequence of variable-length bit-encoded integers.
David Srbecky17b4d2b2021-03-02 18:14:31 +0000501 static constexpr size_t kNumHeaders = 7;
502 uint32_t flags_ = 0; // Note that the space is limited to three bits.
503 uint32_t code_size_ = 0; // The size of native PC range in bytes.
David Srbecky2259f1c2019-01-16 23:18:30 +0000504 uint32_t packed_frame_size_ = 0; // Frame size in kStackAlignment units.
505 uint32_t core_spill_mask_ = 0;
506 uint32_t fp_spill_mask_ = 0;
507 uint32_t number_of_dex_registers_ = 0;
David Srbecky697c47a2019-06-16 21:53:07 +0100508 uint32_t bit_table_flags_ = 0;
509
510 // The encoded bit-tables follow the header. Based on the above flags field,
511 // bit-tables might be omitted or replaced by relative bit-offset if deduped.
512 static constexpr size_t kNumBitTables = 8;
David Srbeckycf7833e2018-06-14 16:45:22 +0100513 BitTable<StackMap> stack_maps_;
514 BitTable<RegisterMask> register_masks_;
David Srbecky42deda82018-08-10 11:23:27 +0100515 BitTable<StackMask> stack_masks_;
David Srbeckya2d29a32018-08-03 11:06:38 +0100516 BitTable<InlineInfo> inline_infos_;
517 BitTable<MethodInfo> method_infos_;
David Srbecky42deda82018-08-10 11:23:27 +0100518 BitTable<DexRegisterMask> dex_register_masks_;
David Srbeckycf7833e2018-06-14 16:45:22 +0100519 BitTable<DexRegisterMapInfo> dex_register_maps_;
520 BitTable<DexRegisterInfo> dex_register_catalog_;
David Srbecky697c47a2019-06-16 21:53:07 +0100521
Vladimir Markoe4ccbb52022-03-15 08:51:33 +0000522 friend class linker::CodeInfoTableDeduper;
David Srbecky67ba8722019-05-23 15:32:18 +0100523 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100524};
525
Roland Levillain1c1da432015-07-16 11:54:44 +0100526#undef ELEMENT_BYTE_OFFSET_AFTER
527#undef ELEMENT_BIT_OFFSET_AFTER
528
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100529} // namespace art
530
531#endif // ART_RUNTIME_STACK_MAP_H_