Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1 | /* |
| 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 Gampe | 69489fa | 2017-06-08 18:03:25 -0700 | [diff] [blame] | 20 | #include <limits> |
| 21 | |
David Srbecky | f6ba5b3 | 2018-06-23 22:05:49 +0100 | [diff] [blame] | 22 | #include "arch/instruction_set.h" |
David Sehr | 1ce2b3b | 2018-04-05 11:02:03 -0700 | [diff] [blame] | 23 | #include "base/bit_memory_region.h" |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 24 | #include "base/bit_table.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 25 | #include "base/bit_utils.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 26 | #include "base/bit_vector.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 27 | #include "base/leb128.h" |
David Sehr | 1ce2b3b | 2018-04-05 11:02:03 -0700 | [diff] [blame] | 28 | #include "base/memory_region.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 29 | #include "dex/dex_file_types.h" |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 30 | #include "dex_register_location.h" |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 31 | #include "method_info.h" |
David Srbecky | f6ba5b3 | 2018-06-23 22:05:49 +0100 | [diff] [blame] | 32 | #include "quick/quick_method_frame_info.h" |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | |
David Srbecky | f6ba5b3 | 2018-06-23 22:05:49 +0100 | [diff] [blame] | 36 | class OatQuickMethodHeader; |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 37 | class VariableIndentationOutputStream; |
| 38 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 39 | // Size of a frame slot, in bytes. This constant is a signed value, |
| 40 | // to please the compiler in arithmetic operations involving int32_t |
| 41 | // (signed) values. |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 42 | static constexpr ssize_t kFrameSlotSize = 4; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 43 | |
David Srbecky | 6de8833 | 2018-06-03 12:00:11 +0100 | [diff] [blame] | 44 | // The delta compression of dex register maps means we need to scan the stackmaps backwards. |
| 45 | // We compress the data in such a way so that there is an upper bound on the search distance. |
| 46 | // Max distance 0 means each stack map must be fully defined and no scanning back is allowed. |
| 47 | // If this value is changed, the oat file version should be incremented (for DCHECK to pass). |
| 48 | static constexpr size_t kMaxDexRegisterMapSearchDistance = 32; |
| 49 | |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 50 | class ArtMethod; |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 51 | class CodeInfo; |
David Srbecky | 86decb6 | 2018-06-05 06:41:10 +0100 | [diff] [blame] | 52 | class Stats; |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 53 | |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 54 | std::ostream& operator<<(std::ostream& stream, const DexRegisterLocation& reg); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 55 | |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 56 | // Information on Dex register locations for a specific PC. |
| 57 | // Effectively just a convenience wrapper for DexRegisterLocation vector. |
| 58 | // If the size is small enough, it keeps the data on the stack. |
David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 59 | // TODO: Replace this with generic purpose "small-vector" implementation. |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 60 | class DexRegisterMap { |
| 61 | public: |
David Srbecky | 6de8833 | 2018-06-03 12:00:11 +0100 | [diff] [blame] | 62 | using iterator = DexRegisterLocation*; |
David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 63 | using const_iterator = const DexRegisterLocation*; |
David Srbecky | 6de8833 | 2018-06-03 12:00:11 +0100 | [diff] [blame] | 64 | |
| 65 | // Create map for given number of registers and initialize them to the given value. |
| 66 | DexRegisterMap(size_t count, DexRegisterLocation value) : count_(count), regs_small_{} { |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 67 | if (count_ <= kSmallCount) { |
David Srbecky | 6de8833 | 2018-06-03 12:00:11 +0100 | [diff] [blame] | 68 | std::fill_n(regs_small_.begin(), count, value); |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 69 | } else { |
David Srbecky | 6de8833 | 2018-06-03 12:00:11 +0100 | [diff] [blame] | 70 | regs_large_.resize(count, value); |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 71 | } |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 72 | } |
| 73 | |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 74 | DexRegisterLocation* data() { |
| 75 | return count_ <= kSmallCount ? regs_small_.data() : regs_large_.data(); |
| 76 | } |
David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 77 | const DexRegisterLocation* data() const { |
| 78 | return count_ <= kSmallCount ? regs_small_.data() : regs_large_.data(); |
| 79 | } |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 80 | |
David Srbecky | 6de8833 | 2018-06-03 12:00:11 +0100 | [diff] [blame] | 81 | iterator begin() { return data(); } |
| 82 | iterator end() { return data() + count_; } |
David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 83 | const_iterator begin() const { return data(); } |
| 84 | const_iterator end() const { return data() + count_; } |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 85 | size_t size() const { return count_; } |
David Srbecky | fd89b07 | 2018-06-03 12:00:22 +0100 | [diff] [blame] | 86 | bool empty() const { return count_ == 0; } |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 87 | |
David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 88 | DexRegisterLocation& operator[](size_t index) { |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 89 | DCHECK_LT(index, count_); |
David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 90 | return data()[index]; |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 91 | } |
David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 92 | const DexRegisterLocation& operator[](size_t index) const { |
| 93 | DCHECK_LT(index, count_); |
| 94 | return data()[index]; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 95 | } |
| 96 | |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 97 | size_t GetNumberOfLiveDexRegisters() const { |
David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 98 | return std::count_if(begin(), end(), [](auto& loc) { return loc.IsLive(); }); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 99 | } |
| 100 | |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 101 | bool HasAnyLiveDexRegisters() const { |
David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 102 | return std::any_of(begin(), end(), [](auto& loc) { return loc.IsLive(); }); |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 103 | } |
| 104 | |
David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 105 | void Dump(VariableIndentationOutputStream* vios) const; |
| 106 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 107 | private: |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 108 | // Store the data inline if the number of registers is small to avoid memory allocations. |
| 109 | // If count_ <= kSmallCount, we use the regs_small_ array, and regs_large_ otherwise. |
| 110 | static constexpr size_t kSmallCount = 16; |
| 111 | size_t count_; |
| 112 | std::array<DexRegisterLocation, kSmallCount> regs_small_; |
| 113 | dchecked_vector<DexRegisterLocation> regs_large_; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 116 | /** |
| 117 | * A Stack Map holds compilation information for a specific PC necessary for: |
| 118 | * - Mapping it to a dex PC, |
| 119 | * - Knowing which stack entries are objects, |
| 120 | * - Knowing which registers hold objects, |
| 121 | * - Knowing the inlining information, |
| 122 | * - Knowing the values of dex registers. |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 123 | */ |
David Srbecky | cf7833e | 2018-06-14 16:45:22 +0100 | [diff] [blame] | 124 | class StackMap : public BitTableAccessor<8> { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 125 | public: |
David Srbecky | 50fac06 | 2018-06-13 18:55:35 +0100 | [diff] [blame] | 126 | enum Kind { |
| 127 | Default = -1, |
| 128 | Catch = 0, |
| 129 | OSR = 1, |
| 130 | Debug = 2, |
| 131 | }; |
David Srbecky | d97e082 | 2018-06-03 12:00:24 +0100 | [diff] [blame] | 132 | BIT_TABLE_HEADER() |
David Srbecky | 50fac06 | 2018-06-13 18:55:35 +0100 | [diff] [blame] | 133 | BIT_TABLE_COLUMN(0, Kind) |
| 134 | BIT_TABLE_COLUMN(1, PackedNativePc) |
| 135 | BIT_TABLE_COLUMN(2, DexPc) |
| 136 | BIT_TABLE_COLUMN(3, RegisterMaskIndex) |
| 137 | BIT_TABLE_COLUMN(4, StackMaskIndex) |
| 138 | BIT_TABLE_COLUMN(5, InlineInfoIndex) |
| 139 | BIT_TABLE_COLUMN(6, DexRegisterMaskIndex) |
| 140 | BIT_TABLE_COLUMN(7, DexRegisterMapIndex) |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 141 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 142 | ALWAYS_INLINE uint32_t GetNativePcOffset(InstructionSet instruction_set) const { |
David Srbecky | cf7833e | 2018-06-14 16:45:22 +0100 | [diff] [blame] | 143 | return UnpackNativePc(GetPackedNativePc(), instruction_set); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 144 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 145 | |
David Srbecky | d97e082 | 2018-06-03 12:00:24 +0100 | [diff] [blame] | 146 | ALWAYS_INLINE bool HasInlineInfo() const { |
| 147 | return HasInlineInfoIndex(); |
| 148 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 149 | |
David Srbecky | d97e082 | 2018-06-03 12:00:24 +0100 | [diff] [blame] | 150 | ALWAYS_INLINE bool HasDexRegisterMap() const { |
| 151 | return HasDexRegisterMapIndex(); |
| 152 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 153 | |
David Srbecky | d02b23f | 2018-05-29 23:27:22 +0100 | [diff] [blame] | 154 | static uint32_t PackNativePc(uint32_t native_pc, InstructionSet isa) { |
David Srbecky | d775f96 | 2018-05-30 18:12:52 +0100 | [diff] [blame] | 155 | DCHECK_ALIGNED_PARAM(native_pc, GetInstructionSetInstructionAlignment(isa)); |
David Srbecky | d02b23f | 2018-05-29 23:27:22 +0100 | [diff] [blame] | 156 | return native_pc / GetInstructionSetInstructionAlignment(isa); |
| 157 | } |
| 158 | |
| 159 | static uint32_t UnpackNativePc(uint32_t packed_native_pc, InstructionSet isa) { |
| 160 | uint32_t native_pc = packed_native_pc * GetInstructionSetInstructionAlignment(isa); |
| 161 | DCHECK_EQ(native_pc / GetInstructionSetInstructionAlignment(isa), packed_native_pc); |
| 162 | return native_pc; |
| 163 | } |
| 164 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 165 | void Dump(VariableIndentationOutputStream* vios, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 166 | const CodeInfo& code_info, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 167 | const MethodInfo& method_info, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 168 | uint32_t code_offset, |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 169 | InstructionSet instruction_set) const; |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 172 | /** |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 173 | * 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 Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 176 | */ |
David Srbecky | cf7833e | 2018-06-14 16:45:22 +0100 | [diff] [blame] | 177 | class InlineInfo : public BitTableAccessor<6> { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 178 | public: |
David Srbecky | d97e082 | 2018-06-03 12:00:24 +0100 | [diff] [blame] | 179 | BIT_TABLE_HEADER() |
| 180 | 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 Srbecky | 6de8833 | 2018-06-03 12:00:11 +0100 | [diff] [blame] | 185 | BIT_TABLE_COLUMN(5, NumberOfDexRegisters) // Includes outer levels and the main method. |
David Srbecky | d97e082 | 2018-06-03 12:00:24 +0100 | [diff] [blame] | 186 | BIT_TABLE_COLUMN(6, DexRegisterMapIndex) |
| 187 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 188 | static constexpr uint32_t kLast = -1; |
| 189 | static constexpr uint32_t kMore = 0; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 190 | |
David Srbecky | 6e69e52 | 2018-06-03 12:00:14 +0100 | [diff] [blame] | 191 | uint32_t GetMethodIndex(const MethodInfo& method_info) const { |
David Srbecky | d97e082 | 2018-06-03 12:00:24 +0100 | [diff] [blame] | 192 | return method_info.GetMethodIndex(GetMethodInfoIndex()); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 193 | } |
| 194 | |
David Srbecky | 6e69e52 | 2018-06-03 12:00:14 +0100 | [diff] [blame] | 195 | bool EncodesArtMethod() const { |
David Srbecky | d97e082 | 2018-06-03 12:00:24 +0100 | [diff] [blame] | 196 | return HasArtMethodLo(); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 197 | } |
| 198 | |
David Srbecky | 6e69e52 | 2018-06-03 12:00:14 +0100 | [diff] [blame] | 199 | ArtMethod* GetArtMethod() const { |
David Srbecky | d97e082 | 2018-06-03 12:00:24 +0100 | [diff] [blame] | 200 | uint64_t lo = GetArtMethodLo(); |
| 201 | uint64_t hi = GetArtMethodHi(); |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 202 | return reinterpret_cast<ArtMethod*>((hi << 32) | lo); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 205 | void Dump(VariableIndentationOutputStream* vios, |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 206 | const CodeInfo& info, |
David Srbecky | 6e69e52 | 2018-06-03 12:00:14 +0100 | [diff] [blame] | 207 | const StackMap& stack_map, |
David Srbecky | fd89b07 | 2018-06-03 12:00:22 +0100 | [diff] [blame] | 208 | const MethodInfo& method_info) const; |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 209 | }; |
| 210 | |
David Srbecky | cf7833e | 2018-06-14 16:45:22 +0100 | [diff] [blame] | 211 | class MaskInfo : public BitTableAccessor<1> { |
David Srbecky | 86decb6 | 2018-06-05 06:41:10 +0100 | [diff] [blame] | 212 | public: |
| 213 | BIT_TABLE_HEADER() |
| 214 | BIT_TABLE_COLUMN(0, Mask) |
| 215 | }; |
| 216 | |
David Srbecky | cf7833e | 2018-06-14 16:45:22 +0100 | [diff] [blame] | 217 | class DexRegisterMapInfo : public BitTableAccessor<1> { |
David Srbecky | 86decb6 | 2018-06-05 06:41:10 +0100 | [diff] [blame] | 218 | public: |
| 219 | BIT_TABLE_HEADER() |
| 220 | BIT_TABLE_COLUMN(0, CatalogueIndex) |
| 221 | }; |
| 222 | |
David Srbecky | cf7833e | 2018-06-14 16:45:22 +0100 | [diff] [blame] | 223 | class DexRegisterInfo : public BitTableAccessor<2> { |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 224 | public: |
David Srbecky | d97e082 | 2018-06-03 12:00:24 +0100 | [diff] [blame] | 225 | BIT_TABLE_HEADER() |
| 226 | BIT_TABLE_COLUMN(0, Kind) |
| 227 | BIT_TABLE_COLUMN(1, PackedValue) |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 228 | |
| 229 | ALWAYS_INLINE DexRegisterLocation GetLocation() const { |
David Srbecky | d97e082 | 2018-06-03 12:00:24 +0100 | [diff] [blame] | 230 | DexRegisterLocation::Kind kind = static_cast<DexRegisterLocation::Kind>(GetKind()); |
| 231 | return DexRegisterLocation(kind, UnpackValue(kind, GetPackedValue())); |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 232 | } |
| 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 Srbecky | 4b59d10 | 2018-05-29 21:46:10 +0000 | [diff] [blame] | 252 | // 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 Srbecky | cf7833e | 2018-06-14 16:45:22 +0100 | [diff] [blame] | 254 | class RegisterMask : public BitTableAccessor<2> { |
David Srbecky | 4b59d10 | 2018-05-29 21:46:10 +0000 | [diff] [blame] | 255 | public: |
David Srbecky | d97e082 | 2018-06-03 12:00:24 +0100 | [diff] [blame] | 256 | BIT_TABLE_HEADER() |
| 257 | BIT_TABLE_COLUMN(0, Value) |
| 258 | BIT_TABLE_COLUMN(1, Shift) |
David Srbecky | 4b59d10 | 2018-05-29 21:46:10 +0000 | [diff] [blame] | 259 | |
| 260 | ALWAYS_INLINE uint32_t GetMask() const { |
David Srbecky | d97e082 | 2018-06-03 12:00:24 +0100 | [diff] [blame] | 261 | return GetValue() << GetShift(); |
David Srbecky | 4b59d10 | 2018-05-29 21:46:10 +0000 | [diff] [blame] | 262 | } |
| 263 | }; |
| 264 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 265 | /** |
| 266 | * Wrapper around all compiler information collected for a method. |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 267 | * See the Decode method at the end for the precise binary format. |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 268 | */ |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 269 | class CodeInfo { |
| 270 | public: |
David Srbecky | 6ee06e9 | 2018-07-25 21:45:54 +0100 | [diff] [blame] | 271 | enum DecodeFlags { |
| 272 | Default = 0, |
| 273 | // Limits the decoding only to the main stack map table and inline info table. |
| 274 | // This is sufficient for many use cases and makes the header decoding faster. |
| 275 | InlineInfoOnly = 1, |
| 276 | }; |
| 277 | |
| 278 | explicit CodeInfo(const uint8_t* data, DecodeFlags flags = DecodeFlags::Default) { |
| 279 | Decode(reinterpret_cast<const uint8_t*>(data), flags); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 280 | } |
| 281 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 282 | explicit CodeInfo(MemoryRegion region) : CodeInfo(region.begin()) { |
David Srbecky | a38e6cf | 2018-06-26 18:13:49 +0100 | [diff] [blame] | 283 | DCHECK_EQ(Size(), region.size()); |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 284 | } |
| 285 | |
David Srbecky | 6ee06e9 | 2018-07-25 21:45:54 +0100 | [diff] [blame] | 286 | explicit CodeInfo(const OatQuickMethodHeader* header, DecodeFlags flags = DecodeFlags::Default); |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 287 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 288 | size_t Size() const { |
David Srbecky | a38e6cf | 2018-06-26 18:13:49 +0100 | [diff] [blame] | 289 | return BitsToBytesRoundUp(size_in_bits_); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 290 | } |
| 291 | |
David Srbecky | 93bd361 | 2018-07-02 19:30:18 +0100 | [diff] [blame] | 292 | ALWAYS_INLINE const BitTable<StackMap>& GetStackMaps() const { |
| 293 | return stack_maps_; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 294 | } |
| 295 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 296 | ALWAYS_INLINE StackMap GetStackMapAt(size_t index) const { |
David Srbecky | cf7833e | 2018-06-14 16:45:22 +0100 | [diff] [blame] | 297 | return stack_maps_.GetRow(index); |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 298 | } |
| 299 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 300 | BitMemoryRegion GetStackMask(size_t index) const { |
David Srbecky | 4b59d10 | 2018-05-29 21:46:10 +0000 | [diff] [blame] | 301 | return stack_masks_.GetBitMemoryRegion(index); |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 302 | } |
| 303 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 304 | BitMemoryRegion GetStackMaskOf(const StackMap& stack_map) const { |
David Srbecky | 4b59d10 | 2018-05-29 21:46:10 +0000 | [diff] [blame] | 305 | uint32_t index = stack_map.GetStackMaskIndex(); |
| 306 | return (index == StackMap::kNoValue) ? BitMemoryRegion() : GetStackMask(index); |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 307 | } |
| 308 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 309 | uint32_t GetRegisterMaskOf(const StackMap& stack_map) const { |
David Srbecky | 4b59d10 | 2018-05-29 21:46:10 +0000 | [diff] [blame] | 310 | uint32_t index = stack_map.GetRegisterMaskIndex(); |
David Srbecky | cf7833e | 2018-06-14 16:45:22 +0100 | [diff] [blame] | 311 | return (index == StackMap::kNoValue) ? 0 : register_masks_.GetRow(index).GetMask(); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 312 | } |
| 313 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 314 | uint32_t GetNumberOfLocationCatalogEntries() const { |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 315 | return dex_register_catalog_.NumRows(); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 316 | } |
| 317 | |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 318 | ALWAYS_INLINE DexRegisterLocation GetDexRegisterCatalogEntry(size_t index) const { |
David Srbecky | 6de8833 | 2018-06-03 12:00:11 +0100 | [diff] [blame] | 319 | return (index == StackMap::kNoValue) |
| 320 | ? DexRegisterLocation::None() |
David Srbecky | cf7833e | 2018-06-14 16:45:22 +0100 | [diff] [blame] | 321 | : dex_register_catalog_.GetRow(index).GetLocation(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 322 | } |
| 323 | |
David Srbecky | 93bd361 | 2018-07-02 19:30:18 +0100 | [diff] [blame] | 324 | bool HasInlineInfo() const { |
| 325 | return inline_infos_.NumRows() > 0; |
| 326 | } |
| 327 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 328 | uint32_t GetNumberOfStackMaps() const { |
| 329 | return stack_maps_.NumRows(); |
Nicolas Geoffray | 6530baf | 2015-05-26 15:22:58 +0100 | [diff] [blame] | 330 | } |
| 331 | |
David Srbecky | fd89b07 | 2018-06-03 12:00:22 +0100 | [diff] [blame] | 332 | ALWAYS_INLINE DexRegisterMap GetDexRegisterMapOf(StackMap stack_map) const { |
David Srbecky | 6de8833 | 2018-06-03 12:00:11 +0100 | [diff] [blame] | 333 | if (stack_map.HasDexRegisterMap()) { |
| 334 | DexRegisterMap map(number_of_dex_registers_, DexRegisterLocation::Invalid()); |
| 335 | DecodeDexRegisterMap(stack_map.Row(), /* first_dex_register */ 0, &map); |
| 336 | return map; |
| 337 | } |
| 338 | return DexRegisterMap(0, DexRegisterLocation::None()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 339 | } |
| 340 | |
David Srbecky | 93bd361 | 2018-07-02 19:30:18 +0100 | [diff] [blame] | 341 | ALWAYS_INLINE DexRegisterMap GetInlineDexRegisterMapOf(StackMap stack_map, |
| 342 | InlineInfo inline_info) const { |
David Srbecky | 6de8833 | 2018-06-03 12:00:11 +0100 | [diff] [blame] | 343 | if (stack_map.HasDexRegisterMap()) { |
David Srbecky | 93bd361 | 2018-07-02 19:30:18 +0100 | [diff] [blame] | 344 | DCHECK(stack_map.HasInlineInfoIndex()); |
| 345 | uint32_t depth = inline_info.Row() - stack_map.GetInlineInfoIndex(); |
David Srbecky | 6de8833 | 2018-06-03 12:00:11 +0100 | [diff] [blame] | 346 | // The register counts are commutative and include all outer levels. |
| 347 | // This allows us to determine the range [first, last) in just two lookups. |
| 348 | // If we are at depth 0 (the first inlinee), the count from the main method is used. |
David Srbecky | 93bd361 | 2018-07-02 19:30:18 +0100 | [diff] [blame] | 349 | uint32_t first = (depth == 0) |
| 350 | ? number_of_dex_registers_ |
| 351 | : inline_infos_.GetRow(inline_info.Row() - 1).GetNumberOfDexRegisters(); |
| 352 | uint32_t last = inline_info.GetNumberOfDexRegisters(); |
David Srbecky | 6de8833 | 2018-06-03 12:00:11 +0100 | [diff] [blame] | 353 | DexRegisterMap map(last - first, DexRegisterLocation::Invalid()); |
| 354 | DecodeDexRegisterMap(stack_map.Row(), first, &map); |
| 355 | return map; |
| 356 | } |
| 357 | return DexRegisterMap(0, DexRegisterLocation::None()); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 358 | } |
| 359 | |
David Srbecky | 93bd361 | 2018-07-02 19:30:18 +0100 | [diff] [blame] | 360 | BitTableRange<InlineInfo> GetInlineInfosOf(StackMap stack_map) const { |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 361 | uint32_t index = stack_map.GetInlineInfoIndex(); |
David Srbecky | 6e69e52 | 2018-06-03 12:00:14 +0100 | [diff] [blame] | 362 | if (index != StackMap::kNoValue) { |
David Srbecky | 93bd361 | 2018-07-02 19:30:18 +0100 | [diff] [blame] | 363 | auto begin = inline_infos_.begin() + index; |
| 364 | auto end = begin; |
| 365 | while ((*end++).GetIsLast() == InlineInfo::kMore) { } |
| 366 | return BitTableRange<InlineInfo>(begin, end); |
| 367 | } else { |
| 368 | return BitTableRange<InlineInfo>(); |
David Srbecky | 6e69e52 | 2018-06-03 12:00:14 +0100 | [diff] [blame] | 369 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 370 | } |
| 371 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 372 | StackMap GetStackMapForDexPc(uint32_t dex_pc) const { |
David Srbecky | 93bd361 | 2018-07-02 19:30:18 +0100 | [diff] [blame] | 373 | for (StackMap stack_map : stack_maps_) { |
David Srbecky | 50fac06 | 2018-06-13 18:55:35 +0100 | [diff] [blame] | 374 | if (stack_map.GetDexPc() == dex_pc && stack_map.GetKind() != StackMap::Kind::Debug) { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 375 | return stack_map; |
| 376 | } |
| 377 | } |
David Srbecky | a45a85c | 2018-06-21 16:03:12 +0100 | [diff] [blame] | 378 | return stack_maps_.GetInvalidRow(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 379 | } |
| 380 | |
David Srbecky | 50fac06 | 2018-06-13 18:55:35 +0100 | [diff] [blame] | 381 | // Searches the stack map list backwards because catch stack maps are stored at the end. |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 382 | StackMap GetCatchStackMapForDexPc(uint32_t dex_pc) const { |
| 383 | for (size_t i = GetNumberOfStackMaps(); i > 0; --i) { |
| 384 | StackMap stack_map = GetStackMapAt(i - 1); |
David Srbecky | 50fac06 | 2018-06-13 18:55:35 +0100 | [diff] [blame] | 385 | if (stack_map.GetDexPc() == dex_pc && stack_map.GetKind() == StackMap::Kind::Catch) { |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 386 | return stack_map; |
| 387 | } |
| 388 | } |
David Srbecky | a45a85c | 2018-06-21 16:03:12 +0100 | [diff] [blame] | 389 | return stack_maps_.GetInvalidRow(); |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 390 | } |
| 391 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 392 | StackMap GetOsrStackMapForDexPc(uint32_t dex_pc) const { |
David Srbecky | 93bd361 | 2018-07-02 19:30:18 +0100 | [diff] [blame] | 393 | for (StackMap stack_map : stack_maps_) { |
David Srbecky | 50fac06 | 2018-06-13 18:55:35 +0100 | [diff] [blame] | 394 | if (stack_map.GetDexPc() == dex_pc && stack_map.GetKind() == StackMap::Kind::OSR) { |
| 395 | return stack_map; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
David Srbecky | a45a85c | 2018-06-21 16:03:12 +0100 | [diff] [blame] | 398 | return stack_maps_.GetInvalidRow(); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 399 | } |
| 400 | |
David Srbecky | 0b4e5a3 | 2018-06-11 16:25:29 +0100 | [diff] [blame] | 401 | StackMap GetStackMapForNativePcOffset(uint32_t pc, InstructionSet isa = kRuntimeISA) const; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 402 | |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 403 | // Dump this CodeInfo object on `vios`. |
| 404 | // `code_offset` is the (absolute) native PC of the compiled method. |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 405 | void Dump(VariableIndentationOutputStream* vios, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 406 | uint32_t code_offset, |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 407 | bool verbose, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 408 | InstructionSet instruction_set, |
| 409 | const MethodInfo& method_info) const; |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 410 | |
David Srbecky | 86decb6 | 2018-06-05 06:41:10 +0100 | [diff] [blame] | 411 | // Accumulate code info size statistics into the given Stats tree. |
| 412 | void AddSizeStats(/*out*/ Stats* parent) const; |
| 413 | |
David Srbecky | f6ba5b3 | 2018-06-23 22:05:49 +0100 | [diff] [blame] | 414 | ALWAYS_INLINE static QuickMethodFrameInfo DecodeFrameInfo(const uint8_t* data) { |
David Srbecky | f6ba5b3 | 2018-06-23 22:05:49 +0100 | [diff] [blame] | 415 | return QuickMethodFrameInfo( |
| 416 | DecodeUnsignedLeb128(&data), |
| 417 | DecodeUnsignedLeb128(&data), |
| 418 | DecodeUnsignedLeb128(&data)); |
| 419 | } |
| 420 | |
David Srbecky | b73323c | 2018-07-15 23:58:44 +0100 | [diff] [blame] | 421 | typedef std::map<BitMemoryRegion, uint32_t, BitMemoryRegion::Less> DedupeMap; |
| 422 | |
| 423 | // Copy CodeInfo data while de-duplicating the internal bit tables. |
| 424 | // The 'out' vector must be reused between Dedupe calls (it does not have to be empty). |
| 425 | // The 'dedupe_map' stores the bit offsets of bit tables within the 'out' vector. |
| 426 | // It returns the byte offset of the copied CodeInfo within the 'out' vector. |
| 427 | static size_t Dedupe(std::vector<uint8_t>* out, |
| 428 | const uint8_t* in, |
| 429 | /*inout*/ DedupeMap* dedupe_map); |
| 430 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 431 | private: |
David Srbecky | 0b4e5a3 | 2018-06-11 16:25:29 +0100 | [diff] [blame] | 432 | // Returns lower bound (fist stack map which has pc greater or equal than the desired one). |
| 433 | // It ignores catch stack maps at the end (it is the same as if they had maximum pc value). |
| 434 | BitTable<StackMap>::const_iterator BinarySearchNativePc(uint32_t packed_pc) const; |
| 435 | |
David Srbecky | 6de8833 | 2018-06-03 12:00:11 +0100 | [diff] [blame] | 436 | // Scan backward to determine dex register locations at given stack map. |
| 437 | void DecodeDexRegisterMap(uint32_t stack_map_index, |
| 438 | uint32_t first_dex_register, |
| 439 | /*out*/ DexRegisterMap* map) const; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 440 | |
David Srbecky | 6ee06e9 | 2018-07-25 21:45:54 +0100 | [diff] [blame] | 441 | void Decode(const uint8_t* data, DecodeFlags flags); |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 442 | |
David Srbecky | f6ba5b3 | 2018-06-23 22:05:49 +0100 | [diff] [blame] | 443 | uint32_t frame_size_in_bytes_; |
| 444 | uint32_t core_spill_mask_; |
| 445 | uint32_t fp_spill_mask_; |
| 446 | uint32_t number_of_dex_registers_; |
David Srbecky | cf7833e | 2018-06-14 16:45:22 +0100 | [diff] [blame] | 447 | BitTable<StackMap> stack_maps_; |
David Srbecky | 6ee06e9 | 2018-07-25 21:45:54 +0100 | [diff] [blame] | 448 | BitTable<InlineInfo> inline_infos_; |
David Srbecky | cf7833e | 2018-06-14 16:45:22 +0100 | [diff] [blame] | 449 | BitTable<RegisterMask> register_masks_; |
| 450 | BitTable<MaskInfo> stack_masks_; |
David Srbecky | cf7833e | 2018-06-14 16:45:22 +0100 | [diff] [blame] | 451 | BitTable<MaskInfo> dex_register_masks_; |
| 452 | BitTable<DexRegisterMapInfo> dex_register_maps_; |
| 453 | BitTable<DexRegisterInfo> dex_register_catalog_; |
David Srbecky | 6ee06e9 | 2018-07-25 21:45:54 +0100 | [diff] [blame] | 454 | uint32_t size_in_bits_ = 0; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 455 | }; |
| 456 | |
Roland Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 457 | #undef ELEMENT_BYTE_OFFSET_AFTER |
| 458 | #undef ELEMENT_BIT_OFFSET_AFTER |
| 459 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 460 | } // namespace art |
| 461 | |
| 462 | #endif // ART_RUNTIME_STACK_MAP_H_ |