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/arena_containers.h" |
| 21 | #include "base/bit_vector-inl.h" |
Andreas Gampe | 2a5c468 | 2015-08-14 08:22:54 -0700 | [diff] [blame] | 22 | #include "base/hash_map.h" |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 23 | #include "base/value_object.h" |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 24 | #include "memory_region.h" |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 25 | #include "nodes.h" |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 26 | #include "stack_map.h" |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 27 | #include "utils/growable_array.h" |
| 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: |
| 63 | explicit StackMapStream(ArenaAllocator* allocator) |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 64 | : allocator_(allocator), |
| 65 | stack_maps_(allocator, 10), |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 66 | location_catalog_entries_(allocator, 4), |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 67 | dex_register_locations_(allocator, 10 * 4), |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 68 | inline_infos_(allocator, 2), |
Andreas Gampe | 8eddd2a | 2014-07-28 14:53:22 -0700 | [diff] [blame] | 69 | stack_mask_max_(-1), |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 70 | dex_pc_max_(0), |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 71 | register_mask_max_(0), |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 72 | number_of_stack_maps_with_inline_info_(0), |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 73 | dex_map_hash_to_stack_map_indices_(std::less<uint32_t>(), allocator->Adapter()), |
| 74 | current_entry_(), |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 75 | current_inline_info_(), |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 76 | stack_mask_size_(0), |
| 77 | inline_info_size_(0), |
| 78 | dex_register_maps_size_(0), |
| 79 | stack_maps_size_(0), |
| 80 | dex_register_location_catalog_size_(0), |
| 81 | dex_register_location_catalog_start_(0), |
| 82 | stack_maps_start_(0), |
| 83 | dex_register_maps_start_(0), |
| 84 | inline_infos_start_(0), |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 85 | needed_size_(0), |
| 86 | current_dex_register_(0), |
| 87 | in_inline_frame_(false) {} |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 88 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 89 | // See runtime/stack_map.h to know what these fields contain. |
| 90 | struct StackMapEntry { |
| 91 | uint32_t dex_pc; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 92 | uint32_t native_pc_offset; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 93 | uint32_t register_mask; |
| 94 | BitVector* sp_mask; |
| 95 | uint32_t num_dex_registers; |
| 96 | uint8_t inlining_depth; |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 97 | size_t dex_register_locations_start_index; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 98 | size_t inline_infos_start_index; |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 99 | BitVector* live_dex_registers_mask; |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 100 | uint32_t dex_register_map_hash; |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 101 | size_t same_dex_register_map_as_; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 104 | struct InlineInfoEntry { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 105 | uint32_t dex_pc; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 106 | uint32_t method_index; |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 107 | InvokeType invoke_type; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 108 | uint32_t num_dex_registers; |
| 109 | BitVector* live_dex_registers_mask; |
| 110 | size_t dex_register_locations_start_index; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 111 | }; |
| 112 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 113 | void BeginStackMapEntry(uint32_t dex_pc, |
| 114 | uint32_t native_pc_offset, |
| 115 | uint32_t register_mask, |
| 116 | BitVector* sp_mask, |
| 117 | uint32_t num_dex_registers, |
| 118 | uint8_t inlining_depth); |
| 119 | void EndStackMapEntry(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 120 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 121 | void AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 122 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 123 | void BeginInlineInfoEntry(uint32_t method_index, |
| 124 | uint32_t dex_pc, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 125 | InvokeType invoke_type, |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 126 | uint32_t num_dex_registers); |
| 127 | void EndInlineInfoEntry(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 128 | |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 129 | size_t GetNumberOfStackMaps() const { |
| 130 | return stack_maps_.Size(); |
| 131 | } |
| 132 | |
| 133 | const StackMapEntry& GetStackMap(size_t i) const { |
| 134 | DCHECK_LT(i, stack_maps_.Size()); |
| 135 | return stack_maps_.GetRawStorage()[i]; |
| 136 | } |
| 137 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 138 | void SetStackMapNativePcOffset(size_t i, uint32_t native_pc_offset) { |
| 139 | DCHECK_LT(i, stack_maps_.Size()); |
| 140 | stack_maps_.GetRawStorage()[i].native_pc_offset = native_pc_offset; |
| 141 | } |
| 142 | |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 143 | uint32_t ComputeMaxNativePcOffset() const; |
| 144 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 145 | // Prepares the stream to fill in a memory region. Must be called before FillIn. |
| 146 | // Returns the size (in bytes) needed to store this stream. |
| 147 | size_t PrepareForFillIn(); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 148 | void FillIn(MemoryRegion region); |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 149 | |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 150 | private: |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 151 | size_t ComputeDexRegisterLocationCatalogSize() const; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 152 | size_t ComputeDexRegisterMapSize(uint32_t num_dex_registers, |
| 153 | const BitVector& live_dex_registers_mask) const; |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 154 | size_t ComputeDexRegisterMapsSize() const; |
| 155 | size_t ComputeInlineInfoSize() const; |
| 156 | |
| 157 | // Returns the index of an entry with the same dex register map as the current_entry, |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 158 | // or kNoSameDexMapFound if no such entry exists. |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 159 | size_t FindEntryWithTheSameDexMap(); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 160 | bool HaveTheSameDexMaps(const StackMapEntry& a, const StackMapEntry& b) const; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 161 | void FillInDexRegisterMap(DexRegisterMap dex_register_map, |
| 162 | uint32_t num_dex_registers, |
| 163 | const BitVector& live_dex_registers_mask, |
| 164 | uint32_t start_index_in_dex_register_locations) const; |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 165 | |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 166 | ArenaAllocator* allocator_; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 167 | GrowableArray<StackMapEntry> stack_maps_; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 168 | |
| 169 | // A catalog of unique [location_kind, register_value] pairs (per method). |
| 170 | GrowableArray<DexRegisterLocation> location_catalog_entries_; |
| 171 | // Map from Dex register location catalog entries to their indices in the |
| 172 | // location catalog. |
| 173 | typedef HashMap<DexRegisterLocation, size_t, LocationCatalogEntriesIndicesEmptyFn, |
| 174 | DexRegisterLocationHashFn> LocationCatalogEntriesIndices; |
| 175 | LocationCatalogEntriesIndices location_catalog_entries_indices_; |
| 176 | |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 177 | // A set of concatenated maps of Dex register locations indices to `location_catalog_entries_`. |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 178 | GrowableArray<size_t> dex_register_locations_; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 179 | GrowableArray<InlineInfoEntry> inline_infos_; |
| 180 | int stack_mask_max_; |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 181 | uint32_t dex_pc_max_; |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 182 | uint32_t register_mask_max_; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 183 | size_t number_of_stack_maps_with_inline_info_; |
| 184 | |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 185 | ArenaSafeMap<uint32_t, GrowableArray<uint32_t>> dex_map_hash_to_stack_map_indices_; |
| 186 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 187 | StackMapEntry current_entry_; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 188 | InlineInfoEntry current_inline_info_; |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 189 | StackMapEncoding stack_map_encoding_; |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 190 | size_t stack_mask_size_; |
| 191 | size_t inline_info_size_; |
| 192 | size_t dex_register_maps_size_; |
| 193 | size_t stack_maps_size_; |
| 194 | size_t dex_register_location_catalog_size_; |
| 195 | size_t dex_register_location_catalog_start_; |
| 196 | size_t stack_maps_start_; |
| 197 | size_t dex_register_maps_start_; |
| 198 | size_t inline_infos_start_; |
| 199 | size_t needed_size_; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 200 | uint32_t current_dex_register_; |
| 201 | bool in_inline_frame_; |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 202 | |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 203 | static constexpr uint32_t kNoSameDexMapFound = -1; |
| 204 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 205 | DISALLOW_COPY_AND_ASSIGN(StackMapStream); |
| 206 | }; |
| 207 | |
| 208 | } // namespace art |
| 209 | |
| 210 | #endif // ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_ |