blob: aa19f0941adc6b0ea888f1370c362b907523b53d [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 Sehr1ce2b3b2018-04-05 11:02:03 -070022#include "base/bit_memory_region.h"
David Srbecky052f8ca2018-04-26 15:42:54 +010023#include "base/bit_table.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010024#include "base/bit_utils.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "base/bit_vector.h"
David Sehr67bf42e2018-02-26 16:43:04 -080026#include "base/leb128.h"
David Sehr1ce2b3b2018-04-05 11:02:03 -070027#include "base/memory_region.h"
David Sehr9e734c72018-01-04 17:56:19 -080028#include "dex/dex_file_types.h"
David Srbecky71ec1cc2018-05-18 15:57:25 +010029#include "dex_register_location.h"
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070030#include "method_info.h"
David Srbecky052f8ca2018-04-26 15:42:54 +010031#include "oat_quick_method_header.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010032
33namespace art {
34
Vladimir Marko8f1e08a2015-06-26 12:06:30 +010035class VariableIndentationOutputStream;
36
Roland Levillaina2d8ec62015-03-12 15:25:29 +000037// Size of a frame slot, in bytes. This constant is a signed value,
38// to please the compiler in arithmetic operations involving int32_t
39// (signed) values.
Roland Levillaina552e1c2015-03-26 15:01:03 +000040static constexpr ssize_t kFrameSlotSize = 4;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000041
David Srbecky6de88332018-06-03 12:00:11 +010042// The delta compression of dex register maps means we need to scan the stackmaps backwards.
43// We compress the data in such a way so that there is an upper bound on the search distance.
44// Max distance 0 means each stack map must be fully defined and no scanning back is allowed.
45// If this value is changed, the oat file version should be incremented (for DCHECK to pass).
46static constexpr size_t kMaxDexRegisterMapSearchDistance = 32;
47
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000048class ArtMethod;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000049class CodeInfo;
David Srbecky86decb62018-06-05 06:41:10 +010050class Stats;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000051
David Srbecky71ec1cc2018-05-18 15:57:25 +010052std::ostream& operator<<(std::ostream& stream, const DexRegisterLocation& reg);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010053
David Srbecky71ec1cc2018-05-18 15:57:25 +010054// Information on Dex register locations for a specific PC.
55// Effectively just a convenience wrapper for DexRegisterLocation vector.
56// If the size is small enough, it keeps the data on the stack.
David Srbeckye1402122018-06-13 18:20:45 +010057// TODO: Replace this with generic purpose "small-vector" implementation.
Roland Levillaina552e1c2015-03-26 15:01:03 +000058class DexRegisterMap {
59 public:
David Srbecky6de88332018-06-03 12:00:11 +010060 using iterator = DexRegisterLocation*;
David Srbeckye1402122018-06-13 18:20:45 +010061 using const_iterator = const DexRegisterLocation*;
David Srbecky6de88332018-06-03 12:00:11 +010062
63 // Create map for given number of registers and initialize them to the given value.
64 DexRegisterMap(size_t count, DexRegisterLocation value) : count_(count), regs_small_{} {
David Srbecky71ec1cc2018-05-18 15:57:25 +010065 if (count_ <= kSmallCount) {
David Srbecky6de88332018-06-03 12:00:11 +010066 std::fill_n(regs_small_.begin(), count, value);
David Srbecky71ec1cc2018-05-18 15:57:25 +010067 } else {
David Srbecky6de88332018-06-03 12:00:11 +010068 regs_large_.resize(count, value);
David Srbecky71ec1cc2018-05-18 15:57:25 +010069 }
Roland Levillaina552e1c2015-03-26 15:01:03 +000070 }
71
David Srbecky71ec1cc2018-05-18 15:57:25 +010072 DexRegisterLocation* data() {
73 return count_ <= kSmallCount ? regs_small_.data() : regs_large_.data();
74 }
David Srbeckye1402122018-06-13 18:20:45 +010075 const DexRegisterLocation* data() const {
76 return count_ <= kSmallCount ? regs_small_.data() : regs_large_.data();
77 }
Roland Levillaina552e1c2015-03-26 15:01:03 +000078
David Srbecky6de88332018-06-03 12:00:11 +010079 iterator begin() { return data(); }
80 iterator end() { return data() + count_; }
David Srbeckye1402122018-06-13 18:20:45 +010081 const_iterator begin() const { return data(); }
82 const_iterator end() const { return data() + count_; }
David Srbecky71ec1cc2018-05-18 15:57:25 +010083 size_t size() const { return count_; }
David Srbeckyfd89b072018-06-03 12:00:22 +010084 bool empty() const { return count_ == 0; }
David Srbecky71ec1cc2018-05-18 15:57:25 +010085
David Srbeckye1402122018-06-13 18:20:45 +010086 DexRegisterLocation& operator[](size_t index) {
David Srbecky71ec1cc2018-05-18 15:57:25 +010087 DCHECK_LT(index, count_);
David Srbeckye1402122018-06-13 18:20:45 +010088 return data()[index];
David Srbecky71ec1cc2018-05-18 15:57:25 +010089 }
David Srbeckye1402122018-06-13 18:20:45 +010090 const DexRegisterLocation& operator[](size_t index) const {
91 DCHECK_LT(index, count_);
92 return data()[index];
Roland Levillaina552e1c2015-03-26 15:01:03 +000093 }
94
David Srbecky71ec1cc2018-05-18 15:57:25 +010095 size_t GetNumberOfLiveDexRegisters() const {
David Srbeckye1402122018-06-13 18:20:45 +010096 return std::count_if(begin(), end(), [](auto& loc) { return loc.IsLive(); });
Roland Levillaina552e1c2015-03-26 15:01:03 +000097 }
98
David Srbecky71ec1cc2018-05-18 15:57:25 +010099 bool HasAnyLiveDexRegisters() const {
David Srbeckye1402122018-06-13 18:20:45 +0100100 return std::any_of(begin(), end(), [](auto& loc) { return loc.IsLive(); });
David Srbecky21d45b42018-05-30 06:35:05 +0100101 }
102
David Srbeckye1402122018-06-13 18:20:45 +0100103 void Dump(VariableIndentationOutputStream* vios) const;
104
Roland Levillaina552e1c2015-03-26 15:01:03 +0000105 private:
David Srbecky71ec1cc2018-05-18 15:57:25 +0100106 // Store the data inline if the number of registers is small to avoid memory allocations.
107 // If count_ <= kSmallCount, we use the regs_small_ array, and regs_large_ otherwise.
108 static constexpr size_t kSmallCount = 16;
109 size_t count_;
110 std::array<DexRegisterLocation, kSmallCount> regs_small_;
111 dchecked_vector<DexRegisterLocation> regs_large_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000112};
113
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100114/**
115 * A Stack Map holds compilation information for a specific PC necessary for:
116 * - Mapping it to a dex PC,
117 * - Knowing which stack entries are objects,
118 * - Knowing which registers hold objects,
119 * - Knowing the inlining information,
120 * - Knowing the values of dex registers.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100121 */
David Srbecky71ec1cc2018-05-18 15:57:25 +0100122class StackMap : public BitTable<7>::Accessor {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100123 public:
David Srbeckyd97e0822018-06-03 12:00:24 +0100124 BIT_TABLE_HEADER()
125 BIT_TABLE_COLUMN(0, PackedNativePc)
126 BIT_TABLE_COLUMN(1, DexPc)
127 BIT_TABLE_COLUMN(2, RegisterMaskIndex)
128 BIT_TABLE_COLUMN(3, StackMaskIndex)
129 BIT_TABLE_COLUMN(4, InlineInfoIndex)
130 BIT_TABLE_COLUMN(5, DexRegisterMaskIndex)
131 BIT_TABLE_COLUMN(6, DexRegisterMapIndex)
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100132
David Srbecky052f8ca2018-04-26 15:42:54 +0100133 ALWAYS_INLINE uint32_t GetNativePcOffset(InstructionSet instruction_set) const {
David Srbeckyd02b23f2018-05-29 23:27:22 +0100134 return UnpackNativePc(Get<kPackedNativePc>(), instruction_set);
David Brazdilf677ebf2015-05-29 16:29:43 +0100135 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100136
David Srbeckyd97e0822018-06-03 12:00:24 +0100137 ALWAYS_INLINE bool HasInlineInfo() const {
138 return HasInlineInfoIndex();
139 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100140
David Srbeckyd97e0822018-06-03 12:00:24 +0100141 ALWAYS_INLINE bool HasDexRegisterMap() const {
142 return HasDexRegisterMapIndex();
143 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100144
David Srbeckyd02b23f2018-05-29 23:27:22 +0100145 static uint32_t PackNativePc(uint32_t native_pc, InstructionSet isa) {
David Srbeckyd775f962018-05-30 18:12:52 +0100146 DCHECK_ALIGNED_PARAM(native_pc, GetInstructionSetInstructionAlignment(isa));
David Srbeckyd02b23f2018-05-29 23:27:22 +0100147 return native_pc / GetInstructionSetInstructionAlignment(isa);
148 }
149
150 static uint32_t UnpackNativePc(uint32_t packed_native_pc, InstructionSet isa) {
151 uint32_t native_pc = packed_native_pc * GetInstructionSetInstructionAlignment(isa);
152 DCHECK_EQ(native_pc / GetInstructionSetInstructionAlignment(isa), packed_native_pc);
153 return native_pc;
154 }
155
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100156 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100157 const CodeInfo& code_info,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700158 const MethodInfo& method_info,
Roland Levillainf2650d12015-05-28 14:53:28 +0100159 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100160 InstructionSet instruction_set) const;
David Srbecky61b28a12016-02-25 21:55:03 +0000161};
162
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100163/**
David Srbecky052f8ca2018-04-26 15:42:54 +0100164 * Inline information for a specific PC.
165 * The row referenced from the StackMap holds information at depth 0.
166 * Following rows hold information for further depths.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100167 */
David Srbecky6de88332018-06-03 12:00:11 +0100168class InlineInfo : public BitTable<6>::Accessor {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100169 public:
David Srbeckyd97e0822018-06-03 12:00:24 +0100170 BIT_TABLE_HEADER()
171 BIT_TABLE_COLUMN(0, IsLast) // Determines if there are further rows for further depths.
172 BIT_TABLE_COLUMN(1, DexPc)
173 BIT_TABLE_COLUMN(2, MethodInfoIndex)
174 BIT_TABLE_COLUMN(3, ArtMethodHi) // High bits of ArtMethod*.
175 BIT_TABLE_COLUMN(4, ArtMethodLo) // Low bits of ArtMethod*.
David Srbecky6de88332018-06-03 12:00:11 +0100176 BIT_TABLE_COLUMN(5, NumberOfDexRegisters) // Includes outer levels and the main method.
David Srbeckyd97e0822018-06-03 12:00:24 +0100177 BIT_TABLE_COLUMN(6, DexRegisterMapIndex)
178
David Srbecky052f8ca2018-04-26 15:42:54 +0100179 static constexpr uint32_t kLast = -1;
180 static constexpr uint32_t kMore = 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100181
David Srbecky6e69e522018-06-03 12:00:14 +0100182 uint32_t GetMethodIndex(const MethodInfo& method_info) const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100183 return method_info.GetMethodIndex(GetMethodInfoIndex());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100184 }
185
David Srbecky6e69e522018-06-03 12:00:14 +0100186 bool EncodesArtMethod() const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100187 return HasArtMethodLo();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100188 }
189
David Srbecky6e69e522018-06-03 12:00:14 +0100190 ArtMethod* GetArtMethod() const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100191 uint64_t lo = GetArtMethodLo();
192 uint64_t hi = GetArtMethodHi();
David Srbecky71ec1cc2018-05-18 15:57:25 +0100193 return reinterpret_cast<ArtMethod*>((hi << 32) | lo);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100194 }
195
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100196 void Dump(VariableIndentationOutputStream* vios,
David Srbecky61b28a12016-02-25 21:55:03 +0000197 const CodeInfo& info,
David Srbecky6e69e522018-06-03 12:00:14 +0100198 const StackMap& stack_map,
David Srbeckyfd89b072018-06-03 12:00:22 +0100199 const MethodInfo& method_info) const;
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800200};
201
David Srbecky052f8ca2018-04-26 15:42:54 +0100202class InvokeInfo : public BitTable<3>::Accessor {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800203 public:
David Srbeckyd97e0822018-06-03 12:00:24 +0100204 BIT_TABLE_HEADER()
205 BIT_TABLE_COLUMN(0, PackedNativePc)
206 BIT_TABLE_COLUMN(1, InvokeType)
207 BIT_TABLE_COLUMN(2, MethodInfoIndex)
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800208
David Srbecky052f8ca2018-04-26 15:42:54 +0100209 ALWAYS_INLINE uint32_t GetNativePcOffset(InstructionSet instruction_set) const {
David Srbeckyd02b23f2018-05-29 23:27:22 +0100210 return StackMap::UnpackNativePc(Get<kPackedNativePc>(), instruction_set);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800211 }
212
David Srbecky052f8ca2018-04-26 15:42:54 +0100213 uint32_t GetMethodIndex(MethodInfo method_info) const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100214 return method_info.GetMethodIndex(GetMethodInfoIndex());
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800215 }
David Srbecky09ed0982016-02-12 21:58:43 +0000216};
217
David Srbecky86decb62018-06-05 06:41:10 +0100218class MaskInfo : public BitTable<1>::Accessor {
219 public:
220 BIT_TABLE_HEADER()
221 BIT_TABLE_COLUMN(0, Mask)
222};
223
224class DexRegisterMapInfo : public BitTable<1>::Accessor {
225 public:
226 BIT_TABLE_HEADER()
227 BIT_TABLE_COLUMN(0, CatalogueIndex)
228};
229
David Srbecky71ec1cc2018-05-18 15:57:25 +0100230class DexRegisterInfo : public BitTable<2>::Accessor {
231 public:
David Srbeckyd97e0822018-06-03 12:00:24 +0100232 BIT_TABLE_HEADER()
233 BIT_TABLE_COLUMN(0, Kind)
234 BIT_TABLE_COLUMN(1, PackedValue)
David Srbecky71ec1cc2018-05-18 15:57:25 +0100235
236 ALWAYS_INLINE DexRegisterLocation GetLocation() const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100237 DexRegisterLocation::Kind kind = static_cast<DexRegisterLocation::Kind>(GetKind());
238 return DexRegisterLocation(kind, UnpackValue(kind, GetPackedValue()));
David Srbecky71ec1cc2018-05-18 15:57:25 +0100239 }
240
241 static uint32_t PackValue(DexRegisterLocation::Kind kind, uint32_t value) {
242 uint32_t packed_value = value;
243 if (kind == DexRegisterLocation::Kind::kInStack) {
244 DCHECK(IsAligned<kFrameSlotSize>(packed_value));
245 packed_value /= kFrameSlotSize;
246 }
247 return packed_value;
248 }
249
250 static uint32_t UnpackValue(DexRegisterLocation::Kind kind, uint32_t packed_value) {
251 uint32_t value = packed_value;
252 if (kind == DexRegisterLocation::Kind::kInStack) {
253 value *= kFrameSlotSize;
254 }
255 return value;
256 }
257};
258
David Srbecky4b59d102018-05-29 21:46:10 +0000259// Register masks tend to have many trailing zero bits (caller-saves are usually not encoded),
260// therefore it is worth encoding the mask as value+shift.
261class RegisterMask : public BitTable<2>::Accessor {
262 public:
David Srbeckyd97e0822018-06-03 12:00:24 +0100263 BIT_TABLE_HEADER()
264 BIT_TABLE_COLUMN(0, Value)
265 BIT_TABLE_COLUMN(1, Shift)
David Srbecky4b59d102018-05-29 21:46:10 +0000266
267 ALWAYS_INLINE uint32_t GetMask() const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100268 return GetValue() << GetShift();
David Srbecky4b59d102018-05-29 21:46:10 +0000269 }
270};
271
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100272/**
273 * Wrapper around all compiler information collected for a method.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100274 * See the Decode method at the end for the precise binary format.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100275 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100276class CodeInfo {
277 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100278 explicit CodeInfo(const void* data) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100279 Decode(reinterpret_cast<const uint8_t*>(data));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100280 }
281
David Srbecky052f8ca2018-04-26 15:42:54 +0100282 explicit CodeInfo(MemoryRegion region) : CodeInfo(region.begin()) {
283 DCHECK_EQ(size_, region.size());
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100284 }
285
David Srbecky052f8ca2018-04-26 15:42:54 +0100286 explicit CodeInfo(const OatQuickMethodHeader* header)
287 : CodeInfo(header->GetOptimizedCodeInfoPtr()) {
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100288 }
289
David Srbecky052f8ca2018-04-26 15:42:54 +0100290 size_t Size() const {
291 return size_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000292 }
293
David Srbecky052f8ca2018-04-26 15:42:54 +0100294 bool HasInlineInfo() const {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100295 return inline_infos_.NumRows() > 0;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100296 }
297
David Srbecky052f8ca2018-04-26 15:42:54 +0100298 ALWAYS_INLINE StackMap GetStackMapAt(size_t index) const {
299 return StackMap(&stack_maps_, index);
David Srbecky45aa5982016-03-18 02:15:09 +0000300 }
301
David Srbecky052f8ca2018-04-26 15:42:54 +0100302 BitMemoryRegion GetStackMask(size_t index) const {
David Srbecky4b59d102018-05-29 21:46:10 +0000303 return stack_masks_.GetBitMemoryRegion(index);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800304 }
305
David Srbecky052f8ca2018-04-26 15:42:54 +0100306 BitMemoryRegion GetStackMaskOf(const StackMap& stack_map) const {
David Srbecky4b59d102018-05-29 21:46:10 +0000307 uint32_t index = stack_map.GetStackMaskIndex();
308 return (index == StackMap::kNoValue) ? BitMemoryRegion() : GetStackMask(index);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800309 }
310
David Srbecky052f8ca2018-04-26 15:42:54 +0100311 uint32_t GetRegisterMaskOf(const StackMap& stack_map) const {
David Srbecky4b59d102018-05-29 21:46:10 +0000312 uint32_t index = stack_map.GetRegisterMaskIndex();
313 return (index == StackMap::kNoValue) ? 0 : RegisterMask(&register_masks_, index).GetMask();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100314 }
315
David Srbecky052f8ca2018-04-26 15:42:54 +0100316 uint32_t GetNumberOfLocationCatalogEntries() const {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100317 return dex_register_catalog_.NumRows();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000318 }
319
David Srbecky71ec1cc2018-05-18 15:57:25 +0100320 ALWAYS_INLINE DexRegisterLocation GetDexRegisterCatalogEntry(size_t index) const {
David Srbecky6de88332018-06-03 12:00:11 +0100321 return (index == StackMap::kNoValue)
322 ? DexRegisterLocation::None()
323 : DexRegisterInfo(&dex_register_catalog_, index).GetLocation();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100324 }
325
David Srbecky052f8ca2018-04-26 15:42:54 +0100326 uint32_t GetNumberOfStackMaps() const {
327 return stack_maps_.NumRows();
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100328 }
329
David Srbecky052f8ca2018-04-26 15:42:54 +0100330 InvokeInfo GetInvokeInfo(size_t index) const {
331 return InvokeInfo(&invoke_infos_, index);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800332 }
333
David Srbeckyfd89b072018-06-03 12:00:22 +0100334 ALWAYS_INLINE DexRegisterMap GetDexRegisterMapOf(StackMap stack_map) const {
David Srbecky6de88332018-06-03 12:00:11 +0100335 if (stack_map.HasDexRegisterMap()) {
336 DexRegisterMap map(number_of_dex_registers_, DexRegisterLocation::Invalid());
337 DecodeDexRegisterMap(stack_map.Row(), /* first_dex_register */ 0, &map);
338 return map;
339 }
340 return DexRegisterMap(0, DexRegisterLocation::None());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100341 }
342
David Srbeckyfd89b072018-06-03 12:00:22 +0100343 ALWAYS_INLINE DexRegisterMap GetDexRegisterMapAtDepth(uint8_t depth, StackMap stack_map) const {
David Srbecky6de88332018-06-03 12:00:11 +0100344 if (stack_map.HasDexRegisterMap()) {
345 // The register counts are commutative and include all outer levels.
346 // This allows us to determine the range [first, last) in just two lookups.
347 // If we are at depth 0 (the first inlinee), the count from the main method is used.
348 uint32_t first = (depth == 0) ? number_of_dex_registers_
349 : GetInlineInfoAtDepth(stack_map, depth - 1).GetNumberOfDexRegisters();
350 uint32_t last = GetInlineInfoAtDepth(stack_map, depth).GetNumberOfDexRegisters();
351 DexRegisterMap map(last - first, DexRegisterLocation::Invalid());
352 DecodeDexRegisterMap(stack_map.Row(), first, &map);
353 return map;
354 }
355 return DexRegisterMap(0, DexRegisterLocation::None());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100356 }
357
David Srbecky052f8ca2018-04-26 15:42:54 +0100358 InlineInfo GetInlineInfo(size_t index) const {
359 return InlineInfo(&inline_infos_, index);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800360 }
361
David Srbecky6e69e522018-06-03 12:00:14 +0100362 uint32_t GetInlineDepthOf(StackMap stack_map) const {
363 uint32_t depth = 0;
David Srbecky052f8ca2018-04-26 15:42:54 +0100364 uint32_t index = stack_map.GetInlineInfoIndex();
David Srbecky6e69e522018-06-03 12:00:14 +0100365 if (index != StackMap::kNoValue) {
366 while (GetInlineInfo(index + depth++).GetIsLast() == InlineInfo::kMore) { }
367 }
368 return depth;
369 }
370
371 InlineInfo GetInlineInfoAtDepth(StackMap stack_map, uint32_t depth) const {
372 DCHECK(stack_map.HasInlineInfo());
373 DCHECK_LT(depth, GetInlineDepthOf(stack_map));
374 return GetInlineInfo(stack_map.GetInlineInfoIndex() + depth);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100375 }
376
David Srbecky052f8ca2018-04-26 15:42:54 +0100377 StackMap GetStackMapForDexPc(uint32_t dex_pc) const {
378 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
379 StackMap stack_map = GetStackMapAt(i);
380 if (stack_map.GetDexPc() == dex_pc) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100381 return stack_map;
382 }
383 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100384 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100385 }
386
David Brazdil77a48ae2015-09-15 12:34:04 +0000387 // Searches the stack map list backwards because catch stack maps are stored
388 // at the end.
David Srbecky052f8ca2018-04-26 15:42:54 +0100389 StackMap GetCatchStackMapForDexPc(uint32_t dex_pc) const {
390 for (size_t i = GetNumberOfStackMaps(); i > 0; --i) {
391 StackMap stack_map = GetStackMapAt(i - 1);
392 if (stack_map.GetDexPc() == dex_pc) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000393 return stack_map;
394 }
395 }
396 return StackMap();
397 }
398
David Srbecky052f8ca2018-04-26 15:42:54 +0100399 StackMap GetOsrStackMapForDexPc(uint32_t dex_pc) const {
400 size_t e = GetNumberOfStackMaps();
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000401 if (e == 0) {
402 // There cannot be OSR stack map if there is no stack map.
403 return StackMap();
404 }
405 // Walk over all stack maps. If two consecutive stack maps are identical, then we
406 // have found a stack map suitable for OSR.
407 for (size_t i = 0; i < e - 1; ++i) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100408 StackMap stack_map = GetStackMapAt(i);
409 if (stack_map.GetDexPc() == dex_pc) {
410 StackMap other = GetStackMapAt(i + 1);
411 if (other.GetDexPc() == dex_pc &&
412 other.GetNativePcOffset(kRuntimeISA) ==
413 stack_map.GetNativePcOffset(kRuntimeISA)) {
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000414 if (i < e - 2) {
415 // Make sure there are not three identical stack maps following each other.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800416 DCHECK_NE(
David Srbecky052f8ca2018-04-26 15:42:54 +0100417 stack_map.GetNativePcOffset(kRuntimeISA),
418 GetStackMapAt(i + 2).GetNativePcOffset(kRuntimeISA));
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000419 }
420 return stack_map;
421 }
422 }
423 }
424 return StackMap();
425 }
426
David Srbecky052f8ca2018-04-26 15:42:54 +0100427 StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset) const {
David Brazdil77a48ae2015-09-15 12:34:04 +0000428 // TODO: Safepoint stack maps are sorted by native_pc_offset but catch stack
429 // maps are not. If we knew that the method does not have try/catch,
430 // we could do binary search.
David Srbecky052f8ca2018-04-26 15:42:54 +0100431 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
432 StackMap stack_map = GetStackMapAt(i);
433 if (stack_map.GetNativePcOffset(kRuntimeISA) == native_pc_offset) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100434 return stack_map;
435 }
436 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100437 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100438 }
439
David Srbecky052f8ca2018-04-26 15:42:54 +0100440 InvokeInfo GetInvokeInfoForNativePcOffset(uint32_t native_pc_offset) {
441 for (size_t index = 0; index < invoke_infos_.NumRows(); index++) {
442 InvokeInfo item = GetInvokeInfo(index);
443 if (item.GetNativePcOffset(kRuntimeISA) == native_pc_offset) {
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800444 return item;
445 }
446 }
David Srbeckyd97e0822018-06-03 12:00:24 +0100447 return InvokeInfo();
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800448 }
449
David Srbecky71ec1cc2018-05-18 15:57:25 +0100450 // Dump this CodeInfo object on `vios`.
451 // `code_offset` is the (absolute) native PC of the compiled method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100452 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100453 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100454 bool verbose,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700455 InstructionSet instruction_set,
456 const MethodInfo& method_info) const;
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000457
David Srbecky86decb62018-06-05 06:41:10 +0100458 // Accumulate code info size statistics into the given Stats tree.
459 void AddSizeStats(/*out*/ Stats* parent) const;
460
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100461 private:
David Srbecky6de88332018-06-03 12:00:11 +0100462 // Scan backward to determine dex register locations at given stack map.
463 void DecodeDexRegisterMap(uint32_t stack_map_index,
464 uint32_t first_dex_register,
465 /*out*/ DexRegisterMap* map) const;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000466
David Srbecky052f8ca2018-04-26 15:42:54 +0100467 void Decode(const uint8_t* data) {
468 size_t non_header_size = DecodeUnsignedLeb128(&data);
David Srbecky71ec1cc2018-05-18 15:57:25 +0100469 BitMemoryRegion region(MemoryRegion(const_cast<uint8_t*>(data), non_header_size));
David Srbecky052f8ca2018-04-26 15:42:54 +0100470 size_t bit_offset = 0;
471 size_ = UnsignedLeb128Size(non_header_size) + non_header_size;
David Srbecky71ec1cc2018-05-18 15:57:25 +0100472 stack_maps_.Decode(region, &bit_offset);
473 register_masks_.Decode(region, &bit_offset);
474 stack_masks_.Decode(region, &bit_offset);
475 invoke_infos_.Decode(region, &bit_offset);
476 inline_infos_.Decode(region, &bit_offset);
477 dex_register_masks_.Decode(region, &bit_offset);
478 dex_register_maps_.Decode(region, &bit_offset);
479 dex_register_catalog_.Decode(region, &bit_offset);
David Srbecky6de88332018-06-03 12:00:11 +0100480 number_of_dex_registers_ = DecodeVarintBits(region, &bit_offset);
David Srbecky71ec1cc2018-05-18 15:57:25 +0100481 CHECK_EQ(non_header_size, BitsToBytesRoundUp(bit_offset)) << "Invalid CodeInfo";
David Srbecky052f8ca2018-04-26 15:42:54 +0100482 }
483
484 size_t size_;
David Srbeckyd97e0822018-06-03 12:00:24 +0100485 BitTable<StackMap::kCount> stack_maps_;
486 BitTable<RegisterMask::kCount> register_masks_;
David Srbecky86decb62018-06-05 06:41:10 +0100487 BitTable<MaskInfo::kCount> stack_masks_;
David Srbeckyd97e0822018-06-03 12:00:24 +0100488 BitTable<InvokeInfo::kCount> invoke_infos_;
489 BitTable<InlineInfo::kCount> inline_infos_;
David Srbecky86decb62018-06-05 06:41:10 +0100490 BitTable<MaskInfo::kCount> dex_register_masks_;
491 BitTable<DexRegisterMapInfo::kCount> dex_register_maps_;
David Srbeckyd97e0822018-06-03 12:00:24 +0100492 BitTable<DexRegisterInfo::kCount> dex_register_catalog_;
David Srbecky6de88332018-06-03 12:00:11 +0100493 uint32_t number_of_dex_registers_; // Excludes any inlined methods.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100494};
495
Roland Levillain1c1da432015-07-16 11:54:44 +0100496#undef ELEMENT_BYTE_OFFSET_AFTER
497#undef ELEMENT_BIT_OFFSET_AFTER
498
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100499} // namespace art
500
501#endif // ART_RUNTIME_STACK_MAP_H_