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_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_ |
| 18 | #define ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_ |
| 19 | |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 20 | #include "base/bit_vector-inl.h" |
Andreas Gampe | 2a5c468 | 2015-08-14 08:22:54 -0700 | [diff] [blame] | 21 | #include "base/hash_map.h" |
David Sehr | 1ce2b3b | 2018-04-05 11:02:03 -0700 | [diff] [blame] | 22 | #include "base/memory_region.h" |
Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 23 | #include "base/scoped_arena_containers.h" |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 24 | #include "base/value_object.h" |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 25 | #include "method_info.h" |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 26 | #include "nodes.h" |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 27 | #include "stack_map.h" |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 31 | // Helper to build art::StackMapStream::LocationCatalogEntriesIndices. |
| 32 | class LocationCatalogEntriesIndicesEmptyFn { |
| 33 | public: |
| 34 | void MakeEmpty(std::pair<DexRegisterLocation, size_t>& item) const { |
| 35 | item.first = DexRegisterLocation::None(); |
| 36 | } |
| 37 | bool IsEmpty(const std::pair<DexRegisterLocation, size_t>& item) const { |
| 38 | return item.first == DexRegisterLocation::None(); |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | // Hash function for art::StackMapStream::LocationCatalogEntriesIndices. |
| 43 | // This hash function does not create collisions. |
| 44 | class DexRegisterLocationHashFn { |
| 45 | public: |
| 46 | size_t operator()(DexRegisterLocation key) const { |
| 47 | // Concatenate `key`s fields to create a 64-bit value to be hashed. |
| 48 | int64_t kind_and_value = |
| 49 | (static_cast<int64_t>(key.kind_) << 32) | static_cast<int64_t>(key.value_); |
| 50 | return inner_hash_fn_(kind_and_value); |
| 51 | } |
| 52 | private: |
| 53 | std::hash<int64_t> inner_hash_fn_; |
| 54 | }; |
| 55 | |
| 56 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 57 | /** |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 58 | * Collects and builds stack maps for a method. All the stack maps |
| 59 | * for a method are placed in a CodeInfo object. |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 60 | */ |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 61 | class StackMapStream : public ValueObject { |
| 62 | public: |
Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 63 | explicit StackMapStream(ScopedArenaAllocator* allocator, InstructionSet instruction_set) |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 64 | : allocator_(allocator), |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 65 | instruction_set_(instruction_set), |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 66 | stack_maps_(allocator->Adapter(kArenaAllocStackMapStream)), |
| 67 | location_catalog_entries_(allocator->Adapter(kArenaAllocStackMapStream)), |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 68 | location_catalog_entries_indices_(allocator->Adapter(kArenaAllocStackMapStream)), |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 69 | dex_register_locations_(allocator->Adapter(kArenaAllocStackMapStream)), |
| 70 | inline_infos_(allocator->Adapter(kArenaAllocStackMapStream)), |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 71 | method_indices_(allocator->Adapter(kArenaAllocStackMapStream)), |
Mathieu Chartier | 3228908 | 2017-02-09 15:57:37 -0800 | [diff] [blame] | 72 | dex_register_entries_(allocator->Adapter(kArenaAllocStackMapStream)), |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 73 | out_(allocator->Adapter(kArenaAllocStackMapStream)), |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 74 | dex_map_hash_to_stack_map_indices_(std::less<uint32_t>(), |
| 75 | allocator->Adapter(kArenaAllocStackMapStream)), |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 76 | current_entry_(), |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 77 | current_inline_info_(), |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 78 | current_dex_register_(0), |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 79 | in_inline_frame_(false) { |
| 80 | stack_maps_.reserve(10); |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 81 | out_.reserve(64); |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 82 | location_catalog_entries_.reserve(4); |
| 83 | dex_register_locations_.reserve(10 * 4); |
| 84 | inline_infos_.reserve(2); |
| 85 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 86 | |
Mathieu Chartier | 3228908 | 2017-02-09 15:57:37 -0800 | [diff] [blame] | 87 | // A dex register map entry for a single stack map entry, contains what registers are live as |
| 88 | // well as indices into the location catalog. |
| 89 | class DexRegisterMapEntry { |
| 90 | public: |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 91 | static const uint32_t kOffsetUnassigned = -1; |
Mathieu Chartier | 3228908 | 2017-02-09 15:57:37 -0800 | [diff] [blame] | 92 | |
| 93 | BitVector* live_dex_registers_mask; |
| 94 | uint32_t num_dex_registers; |
| 95 | size_t locations_start_index; |
| 96 | // Computed fields |
| 97 | size_t hash = 0; |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 98 | uint32_t offset = kOffsetUnassigned; |
Mathieu Chartier | 3228908 | 2017-02-09 15:57:37 -0800 | [diff] [blame] | 99 | |
| 100 | size_t ComputeSize(size_t catalog_size) const; |
| 101 | }; |
| 102 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 103 | // See runtime/stack_map.h to know what these fields contain. |
| 104 | struct StackMapEntry { |
| 105 | uint32_t dex_pc; |
David Srbecky | d02b23f | 2018-05-29 23:27:22 +0100 | [diff] [blame^] | 106 | uint32_t packed_native_pc; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 107 | uint32_t register_mask; |
| 108 | BitVector* sp_mask; |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 109 | uint32_t inlining_depth; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 110 | size_t inline_infos_start_index; |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 111 | uint32_t stack_mask_index; |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 112 | uint32_t register_mask_index; |
Mathieu Chartier | 3228908 | 2017-02-09 15:57:37 -0800 | [diff] [blame] | 113 | DexRegisterMapEntry dex_register_entry; |
| 114 | size_t dex_register_map_index; |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 115 | InvokeType invoke_type; |
| 116 | uint32_t dex_method_index; |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 117 | uint32_t dex_method_index_idx; // Index into dex method index table. |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 118 | }; |
| 119 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 120 | struct InlineInfoEntry { |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 121 | uint32_t dex_pc; // dex::kDexNoIndex for intrinsified native methods. |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 122 | ArtMethod* method; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 123 | uint32_t method_index; |
Mathieu Chartier | 3228908 | 2017-02-09 15:57:37 -0800 | [diff] [blame] | 124 | DexRegisterMapEntry dex_register_entry; |
| 125 | size_t dex_register_map_index; |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 126 | uint32_t dex_method_index_idx; // Index into the dex method index table. |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 127 | }; |
| 128 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 129 | void BeginStackMapEntry(uint32_t dex_pc, |
| 130 | uint32_t native_pc_offset, |
| 131 | uint32_t register_mask, |
| 132 | BitVector* sp_mask, |
| 133 | uint32_t num_dex_registers, |
| 134 | uint8_t inlining_depth); |
| 135 | void EndStackMapEntry(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 136 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 137 | void AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 138 | |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 139 | void AddInvoke(InvokeType type, uint32_t dex_method_index); |
| 140 | |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 141 | void BeginInlineInfoEntry(ArtMethod* method, |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 142 | uint32_t dex_pc, |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 143 | uint32_t num_dex_registers, |
| 144 | const DexFile* outer_dex_file = nullptr); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 145 | void EndInlineInfoEntry(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 146 | |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 147 | size_t GetNumberOfStackMaps() const { |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 148 | return stack_maps_.size(); |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 149 | } |
| 150 | |
David Srbecky | d02b23f | 2018-05-29 23:27:22 +0100 | [diff] [blame^] | 151 | uint32_t GetStackMapNativePcOffset(size_t i); |
| 152 | void SetStackMapNativePcOffset(size_t i, uint32_t native_pc_offset); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 153 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 154 | // Prepares the stream to fill in a memory region. Must be called before FillIn. |
| 155 | // Returns the size (in bytes) needed to store this stream. |
| 156 | size_t PrepareForFillIn(); |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 157 | void FillInCodeInfo(MemoryRegion region); |
| 158 | void FillInMethodInfo(MemoryRegion region); |
| 159 | |
| 160 | size_t ComputeMethodInfoSize() const; |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 161 | |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 162 | private: |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 163 | size_t ComputeDexRegisterLocationCatalogSize() const; |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 164 | |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 165 | // Prepare and deduplicate method indices. |
| 166 | void PrepareMethodIndices(); |
| 167 | |
Mathieu Chartier | 3228908 | 2017-02-09 15:57:37 -0800 | [diff] [blame] | 168 | // Deduplicate entry if possible and return the corresponding index into dex_register_entries_ |
| 169 | // array. If entry is not a duplicate, a new entry is added to dex_register_entries_. |
| 170 | size_t AddDexRegisterMapEntry(const DexRegisterMapEntry& entry); |
| 171 | |
| 172 | // Return true if the two dex register map entries are equal. |
| 173 | bool DexRegisterMapEntryEquals(const DexRegisterMapEntry& a, const DexRegisterMapEntry& b) const; |
| 174 | |
| 175 | // Fill in the corresponding entries of a register map. |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 176 | void FillInDexRegisterMap(DexRegisterMap dex_register_map, |
| 177 | uint32_t num_dex_registers, |
| 178 | const BitVector& live_dex_registers_mask, |
| 179 | uint32_t start_index_in_dex_register_locations) const; |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 180 | |
David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 181 | void CheckDexRegisterMap(const CodeInfo& code_info, |
| 182 | const DexRegisterMap& dex_register_map, |
| 183 | size_t num_dex_registers, |
| 184 | BitVector* live_dex_registers_mask, |
| 185 | size_t dex_register_locations_index) const; |
| 186 | void CheckCodeInfo(MemoryRegion region) const; |
| 187 | |
Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 188 | ScopedArenaAllocator* const allocator_; |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 189 | const InstructionSet instruction_set_; |
Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 190 | ScopedArenaVector<StackMapEntry> stack_maps_; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 191 | |
| 192 | // A catalog of unique [location_kind, register_value] pairs (per method). |
Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 193 | ScopedArenaVector<DexRegisterLocation> location_catalog_entries_; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 194 | // Map from Dex register location catalog entries to their indices in the |
| 195 | // location catalog. |
Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 196 | using LocationCatalogEntriesIndices = ScopedArenaHashMap<DexRegisterLocation, |
| 197 | size_t, |
| 198 | LocationCatalogEntriesIndicesEmptyFn, |
| 199 | DexRegisterLocationHashFn>; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 200 | LocationCatalogEntriesIndices location_catalog_entries_indices_; |
| 201 | |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 202 | // A set of concatenated maps of Dex register locations indices to `location_catalog_entries_`. |
Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 203 | ScopedArenaVector<size_t> dex_register_locations_; |
| 204 | ScopedArenaVector<InlineInfoEntry> inline_infos_; |
Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 205 | ScopedArenaVector<uint32_t> method_indices_; |
| 206 | ScopedArenaVector<DexRegisterMapEntry> dex_register_entries_; |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 207 | |
| 208 | ScopedArenaVector<uint8_t> out_; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 209 | |
Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 210 | ScopedArenaSafeMap<uint32_t, ScopedArenaVector<uint32_t>> dex_map_hash_to_stack_map_indices_; |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 211 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 212 | StackMapEntry current_entry_; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 213 | InlineInfoEntry current_inline_info_; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 214 | uint32_t current_dex_register_; |
| 215 | bool in_inline_frame_; |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 216 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 217 | DISALLOW_COPY_AND_ASSIGN(StackMapStream); |
| 218 | }; |
| 219 | |
| 220 | } // namespace art |
| 221 | |
| 222 | #endif // ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_ |