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" |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 22 | #include "base/value_object.h" |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 23 | #include "memory_region.h" |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 24 | #include "nodes.h" |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 25 | #include "stack_map.h" |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 26 | #include "utils/growable_array.h" |
| 27 | |
| 28 | namespace art { |
| 29 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame^] | 30 | // Helper to build art::StackMapStream::LocationCatalogEntriesIndices. |
| 31 | class LocationCatalogEntriesIndicesEmptyFn { |
| 32 | public: |
| 33 | void MakeEmpty(std::pair<DexRegisterLocation, size_t>& item) const { |
| 34 | item.first = DexRegisterLocation::None(); |
| 35 | } |
| 36 | bool IsEmpty(const std::pair<DexRegisterLocation, size_t>& item) const { |
| 37 | return item.first == DexRegisterLocation::None(); |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | // Hash function for art::StackMapStream::LocationCatalogEntriesIndices. |
| 42 | // This hash function does not create collisions. |
| 43 | class DexRegisterLocationHashFn { |
| 44 | public: |
| 45 | size_t operator()(DexRegisterLocation key) const { |
| 46 | // Concatenate `key`s fields to create a 64-bit value to be hashed. |
| 47 | int64_t kind_and_value = |
| 48 | (static_cast<int64_t>(key.kind_) << 32) | static_cast<int64_t>(key.value_); |
| 49 | return inner_hash_fn_(kind_and_value); |
| 50 | } |
| 51 | private: |
| 52 | std::hash<int64_t> inner_hash_fn_; |
| 53 | }; |
| 54 | |
| 55 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 56 | /** |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 57 | * Collects and builds stack maps for a method. All the stack maps |
| 58 | * for a method are placed in a CodeInfo object. |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 59 | */ |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 60 | class StackMapStream : public ValueObject { |
| 61 | public: |
| 62 | explicit StackMapStream(ArenaAllocator* allocator) |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 63 | : allocator_(allocator), |
| 64 | stack_maps_(allocator, 10), |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame^] | 65 | location_catalog_entries_(allocator, 4), |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 66 | dex_register_locations_(allocator, 10 * 4), |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 67 | inline_infos_(allocator, 2), |
Andreas Gampe | 8eddd2a | 2014-07-28 14:53:22 -0700 | [diff] [blame] | 68 | stack_mask_max_(-1), |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 69 | dex_pc_max_(0), |
| 70 | native_pc_offset_max_(0), |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 71 | number_of_stack_maps_with_inline_info_(0), |
| 72 | dex_map_hash_to_stack_map_indices_(std::less<uint32_t>(), allocator->Adapter()) {} |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 73 | |
| 74 | // Compute bytes needed to encode a mask with the given maximum element. |
| 75 | static uint32_t StackMaskEncodingSize(int max_element) { |
| 76 | int number_of_bits = max_element + 1; // Need room for max element too. |
| 77 | return RoundUp(number_of_bits, kBitsPerByte) / kBitsPerByte; |
| 78 | } |
| 79 | |
| 80 | // See runtime/stack_map.h to know what these fields contain. |
| 81 | struct StackMapEntry { |
| 82 | uint32_t dex_pc; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 83 | uint32_t native_pc_offset; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 84 | uint32_t register_mask; |
| 85 | BitVector* sp_mask; |
| 86 | uint32_t num_dex_registers; |
| 87 | uint8_t inlining_depth; |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 88 | size_t dex_register_locations_start_index; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 89 | size_t inline_infos_start_index; |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 90 | BitVector* live_dex_registers_mask; |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 91 | uint32_t dex_register_map_hash; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 92 | }; |
| 93 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 94 | struct InlineInfoEntry { |
| 95 | uint32_t method_index; |
| 96 | }; |
| 97 | |
| 98 | void AddStackMapEntry(uint32_t dex_pc, |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 99 | uint32_t native_pc_offset, |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 100 | uint32_t register_mask, |
| 101 | BitVector* sp_mask, |
| 102 | uint32_t num_dex_registers, |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 103 | uint8_t inlining_depth) { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 104 | StackMapEntry entry; |
| 105 | entry.dex_pc = dex_pc; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 106 | entry.native_pc_offset = native_pc_offset; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 107 | entry.register_mask = register_mask; |
| 108 | entry.sp_mask = sp_mask; |
| 109 | entry.num_dex_registers = num_dex_registers; |
| 110 | entry.inlining_depth = inlining_depth; |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 111 | entry.dex_register_locations_start_index = dex_register_locations_.Size(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 112 | entry.inline_infos_start_index = inline_infos_.Size(); |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 113 | entry.dex_register_map_hash = 0; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 114 | if (num_dex_registers != 0) { |
| 115 | entry.live_dex_registers_mask = |
| 116 | new (allocator_) ArenaBitVector(allocator_, num_dex_registers, true); |
| 117 | } else { |
| 118 | entry.live_dex_registers_mask = nullptr; |
| 119 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 120 | stack_maps_.Add(entry); |
| 121 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 122 | if (sp_mask != nullptr) { |
| 123 | stack_mask_max_ = std::max(stack_mask_max_, sp_mask->GetHighestBitSet()); |
| 124 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 125 | if (inlining_depth > 0) { |
| 126 | number_of_stack_maps_with_inline_info_++; |
| 127 | } |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 128 | |
| 129 | dex_pc_max_ = std::max(dex_pc_max_, dex_pc); |
| 130 | native_pc_offset_max_ = std::max(native_pc_offset_max_, native_pc_offset); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 131 | } |
| 132 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 133 | void AddInlineInfoEntry(uint32_t method_index) { |
| 134 | InlineInfoEntry entry; |
| 135 | entry.method_index = method_index; |
| 136 | inline_infos_.Add(entry); |
| 137 | } |
| 138 | |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 139 | size_t ComputeNeededSize() { |
Roland Levillain | ede7bf8 | 2015-03-13 12:23:04 +0000 | [diff] [blame] | 140 | size_t size = CodeInfo::kFixedSize |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame^] | 141 | + ComputeDexRegisterLocationCatalogSize() |
Roland Levillain | 29ba1b0 | 2015-03-13 11:45:07 +0000 | [diff] [blame] | 142 | + ComputeStackMapsSize() |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 143 | + ComputeDexRegisterMapsSize() |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 144 | + ComputeInlineInfoSize(); |
Nicolas Geoffray | aec8f93 | 2015-03-18 10:42:22 +0000 | [diff] [blame] | 145 | // Note: use RoundUp to word-size here if you want CodeInfo objects to be word aligned. |
| 146 | return size; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Roland Levillain | 29ba1b0 | 2015-03-13 11:45:07 +0000 | [diff] [blame] | 149 | size_t ComputeStackMaskSize() const { |
| 150 | return StackMaskEncodingSize(stack_mask_max_); |
| 151 | } |
| 152 | |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 153 | size_t ComputeStackMapsSize() { |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 154 | return stack_maps_.Size() * StackMap::ComputeStackMapSize( |
| 155 | ComputeStackMaskSize(), |
| 156 | ComputeInlineInfoSize(), |
| 157 | ComputeDexRegisterMapsSize(), |
| 158 | dex_pc_max_, |
| 159 | native_pc_offset_max_); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame^] | 162 | // Compute the size of the Dex register location catalog of `entry`. |
| 163 | size_t ComputeDexRegisterLocationCatalogSize() const { |
| 164 | size_t size = DexRegisterLocationCatalog::kFixedSize; |
| 165 | for (size_t location_catalog_entry_index = 0; |
| 166 | location_catalog_entry_index < location_catalog_entries_.Size(); |
| 167 | ++location_catalog_entry_index) { |
| 168 | DexRegisterLocation dex_register_location = |
| 169 | location_catalog_entries_.Get(location_catalog_entry_index); |
| 170 | size += DexRegisterLocationCatalog::EntrySize(dex_register_location); |
| 171 | } |
| 172 | return size; |
| 173 | } |
| 174 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 175 | size_t ComputeDexRegisterMapSize(const StackMapEntry& entry) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame^] | 176 | // Size of the map in bytes. |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 177 | size_t size = DexRegisterMap::kFixedSize; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame^] | 178 | // Add the live bit mask for the Dex register liveness. |
| 179 | size += DexRegisterMap::GetLiveBitMaskSize(entry.num_dex_registers); |
| 180 | // Compute the size of the set of live Dex register entries. |
| 181 | size_t number_of_live_dex_registers = 0; |
| 182 | for (size_t dex_register_number = 0; |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 183 | dex_register_number < entry.num_dex_registers; |
| 184 | ++dex_register_number) { |
| 185 | if (entry.live_dex_registers_mask->IsBitSet(dex_register_number)) { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame^] | 186 | ++number_of_live_dex_registers; |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 187 | } |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 188 | } |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame^] | 189 | size_t map_entries_size_in_bits = |
| 190 | DexRegisterMap::SingleEntrySizeInBits(location_catalog_entries_.Size()) |
| 191 | * number_of_live_dex_registers; |
| 192 | size_t map_entries_size_in_bytes = |
| 193 | RoundUp(map_entries_size_in_bits, kBitsPerByte) / kBitsPerByte; |
| 194 | size += map_entries_size_in_bytes; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 195 | return size; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 198 | // Compute the size of all the Dex register maps. |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 199 | size_t ComputeDexRegisterMapsSize() { |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 200 | size_t size = 0; |
| 201 | for (size_t i = 0; i < stack_maps_.Size(); ++i) { |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 202 | if (FindEntryWithTheSameDexMap(i) == kNoSameDexMapFound) { |
| 203 | // Entries with the same dex map will have the same offset. |
| 204 | size += ComputeDexRegisterMapSize(stack_maps_.Get(i)); |
| 205 | } |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 206 | } |
Roland Levillain | ede7bf8 | 2015-03-13 12:23:04 +0000 | [diff] [blame] | 207 | return size; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // Compute the size of all the inline information pieces. |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 211 | size_t ComputeInlineInfoSize() const { |
| 212 | return inline_infos_.Size() * InlineInfo::SingleEntrySize() |
| 213 | // For encoding the depth. |
| 214 | + (number_of_stack_maps_with_inline_info_ * InlineInfo::kFixedSize); |
| 215 | } |
| 216 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame^] | 217 | size_t ComputeDexRegisterLocationCatalogStart() const { |
| 218 | return CodeInfo::kFixedSize; |
| 219 | } |
| 220 | |
| 221 | size_t ComputeStackMapsStart() const { |
| 222 | return ComputeDexRegisterLocationCatalogStart() + ComputeDexRegisterLocationCatalogSize(); |
| 223 | } |
| 224 | |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 225 | size_t ComputeDexRegisterMapsStart() { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame^] | 226 | return ComputeStackMapsStart() + ComputeStackMapsSize(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 229 | size_t ComputeInlineInfoStart() { |
Roland Levillain | 9ac0e4d | 2015-03-12 18:33:05 +0000 | [diff] [blame] | 230 | return ComputeDexRegisterMapsStart() + ComputeDexRegisterMapsSize(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 233 | void FillIn(MemoryRegion region) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 234 | CodeInfo code_info(region); |
Roland Levillain | 29ba1b0 | 2015-03-13 11:45:07 +0000 | [diff] [blame] | 235 | DCHECK_EQ(region.size(), ComputeNeededSize()); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 236 | code_info.SetOverallSize(region.size()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 237 | |
Roland Levillain | 29ba1b0 | 2015-03-13 11:45:07 +0000 | [diff] [blame] | 238 | size_t stack_mask_size = ComputeStackMaskSize(); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 239 | |
| 240 | size_t dex_register_map_size = ComputeDexRegisterMapsSize(); |
| 241 | size_t inline_info_size = ComputeInlineInfoSize(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 242 | |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 243 | MemoryRegion dex_register_locations_region = region.Subregion( |
Roland Levillain | 9ac0e4d | 2015-03-12 18:33:05 +0000 | [diff] [blame] | 244 | ComputeDexRegisterMapsStart(), |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 245 | dex_register_map_size); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 246 | |
| 247 | MemoryRegion inline_infos_region = region.Subregion( |
| 248 | ComputeInlineInfoStart(), |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 249 | inline_info_size); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 250 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 251 | code_info.SetEncoding( |
| 252 | inline_info_size, dex_register_map_size, dex_pc_max_, native_pc_offset_max_); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 253 | code_info.SetNumberOfStackMaps(stack_maps_.Size()); |
| 254 | code_info.SetStackMaskSize(stack_mask_size); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame^] | 255 | DCHECK_EQ(code_info.GetStackMapsSize(), ComputeStackMapsSize()); |
| 256 | |
| 257 | // Set the Dex register location catalog. |
| 258 | code_info.SetNumberOfDexRegisterLocationCatalogEntries( |
| 259 | location_catalog_entries_.Size()); |
| 260 | MemoryRegion dex_register_location_catalog_region = region.Subregion( |
| 261 | ComputeDexRegisterLocationCatalogStart(), |
| 262 | ComputeDexRegisterLocationCatalogSize()); |
| 263 | DexRegisterLocationCatalog dex_register_location_catalog(dex_register_location_catalog_region); |
| 264 | // Offset in `dex_register_location_catalog` where to store the next |
| 265 | // register location. |
| 266 | size_t location_catalog_offset = DexRegisterLocationCatalog::kFixedSize; |
| 267 | for (size_t i = 0, e = location_catalog_entries_.Size(); i < e; ++i) { |
| 268 | DexRegisterLocation dex_register_location = location_catalog_entries_.Get(i); |
| 269 | dex_register_location_catalog.SetRegisterInfo(location_catalog_offset, dex_register_location); |
| 270 | location_catalog_offset += DexRegisterLocationCatalog::EntrySize(dex_register_location); |
| 271 | } |
| 272 | // Ensure we reached the end of the Dex registers location_catalog. |
| 273 | DCHECK_EQ(location_catalog_offset, dex_register_location_catalog_region.size()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 274 | |
| 275 | uintptr_t next_dex_register_map_offset = 0; |
| 276 | uintptr_t next_inline_info_offset = 0; |
| 277 | for (size_t i = 0, e = stack_maps_.Size(); i < e; ++i) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 278 | StackMap stack_map = code_info.GetStackMapAt(i); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 279 | StackMapEntry entry = stack_maps_.Get(i); |
| 280 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 281 | stack_map.SetDexPc(code_info, entry.dex_pc); |
| 282 | stack_map.SetNativePcOffset(code_info, entry.native_pc_offset); |
| 283 | stack_map.SetRegisterMask(code_info, entry.register_mask); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 284 | if (entry.sp_mask != nullptr) { |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 285 | stack_map.SetStackMask(code_info, *entry.sp_mask); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 286 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 287 | |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 288 | if (entry.num_dex_registers == 0) { |
| 289 | // No dex map available. |
| 290 | stack_map.SetDexRegisterMapOffset(code_info, StackMap::kNoDexRegisterMap); |
| 291 | } else { |
| 292 | // Search for an entry with the same dex map. |
| 293 | size_t entry_with_same_map = FindEntryWithTheSameDexMap(i); |
| 294 | if (entry_with_same_map != kNoSameDexMapFound) { |
| 295 | // If we have a hit reuse the offset. |
| 296 | stack_map.SetDexRegisterMapOffset(code_info, |
| 297 | code_info.GetStackMapAt(entry_with_same_map).GetDexRegisterMapOffset(code_info)); |
| 298 | } else { |
| 299 | // New dex registers maps should be added to the stack map. |
| 300 | MemoryRegion register_region = |
| 301 | dex_register_locations_region.Subregion( |
| 302 | next_dex_register_map_offset, |
| 303 | ComputeDexRegisterMapSize(entry)); |
| 304 | next_dex_register_map_offset += register_region.size(); |
| 305 | DexRegisterMap dex_register_map(register_region); |
| 306 | stack_map.SetDexRegisterMapOffset( |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 307 | code_info, register_region.start() - dex_register_locations_region.start()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 308 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame^] | 309 | // Set the live bit mask. |
| 310 | dex_register_map.SetLiveBitMask(entry.num_dex_registers, *entry.live_dex_registers_mask); |
| 311 | |
| 312 | // Set the dex register location mapping data. |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 313 | for (size_t dex_register_number = 0, index_in_dex_register_locations = 0; |
| 314 | dex_register_number < entry.num_dex_registers; |
| 315 | ++dex_register_number) { |
| 316 | if (entry.live_dex_registers_mask->IsBitSet(dex_register_number)) { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame^] | 317 | size_t location_catalog_entry_index = |
| 318 | dex_register_locations_.Get(entry.dex_register_locations_start_index |
| 319 | + index_in_dex_register_locations); |
| 320 | dex_register_map.SetLocationCatalogEntryIndex( |
| 321 | index_in_dex_register_locations, |
| 322 | location_catalog_entry_index, |
| 323 | entry.num_dex_registers, |
| 324 | location_catalog_entries_.Size()); |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 325 | ++index_in_dex_register_locations; |
| 326 | } |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 327 | } |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 328 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | // Set the inlining info. |
| 332 | if (entry.inlining_depth != 0) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 333 | MemoryRegion inline_region = inline_infos_region.Subregion( |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 334 | next_inline_info_offset, |
| 335 | InlineInfo::kFixedSize + entry.inlining_depth * InlineInfo::SingleEntrySize()); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 336 | next_inline_info_offset += inline_region.size(); |
| 337 | InlineInfo inline_info(inline_region); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 338 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 339 | // Currently relative to the dex register map. |
| 340 | stack_map.SetInlineDescriptorOffset( |
| 341 | code_info, inline_region.start() - dex_register_locations_region.start()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 342 | |
| 343 | inline_info.SetDepth(entry.inlining_depth); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 344 | for (size_t j = 0; j < entry.inlining_depth; ++j) { |
| 345 | InlineInfoEntry inline_entry = inline_infos_.Get(j + entry.inline_infos_start_index); |
| 346 | inline_info.SetMethodReferenceIndexAtDepth(j, inline_entry.method_index); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 347 | } |
| 348 | } else { |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 349 | if (inline_info_size != 0) { |
| 350 | stack_map.SetInlineDescriptorOffset(code_info, StackMap::kNoInlineInfo); |
| 351 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 356 | void AddDexRegisterEntry(uint16_t dex_register, DexRegisterLocation::Kind kind, int32_t value) { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame^] | 357 | StackMapEntry entry = stack_maps_.Get(stack_maps_.Size() - 1); |
| 358 | DCHECK_LT(dex_register, entry.num_dex_registers); |
| 359 | |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 360 | if (kind != DexRegisterLocation::Kind::kNone) { |
| 361 | // Ensure we only use non-compressed location kind at this stage. |
| 362 | DCHECK(DexRegisterLocation::IsShortLocationKind(kind)) |
| 363 | << DexRegisterLocation::PrettyDescriptor(kind); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame^] | 364 | DexRegisterLocation location(kind, value); |
| 365 | |
| 366 | // Look for Dex register `location` in the location catalog (using the |
| 367 | // companion hash map of locations to indices). Use its index if it |
| 368 | // is already in the location catalog. If not, insert it (in the |
| 369 | // location catalog and the hash map) and use the newly created index. |
| 370 | auto it = location_catalog_entries_indices_.Find(location); |
| 371 | if (it != location_catalog_entries_indices_.end()) { |
| 372 | // Retrieve the index from the hash map. |
| 373 | dex_register_locations_.Add(it->second); |
| 374 | } else { |
| 375 | // Create a new entry in the location catalog and the hash map. |
| 376 | size_t index = location_catalog_entries_.Size(); |
| 377 | location_catalog_entries_.Add(location); |
| 378 | dex_register_locations_.Add(index); |
| 379 | location_catalog_entries_indices_.Insert(std::make_pair(location, index)); |
| 380 | } |
| 381 | |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 382 | entry.live_dex_registers_mask->SetBit(dex_register); |
| 383 | entry.dex_register_map_hash += (1 << dex_register); |
| 384 | entry.dex_register_map_hash += static_cast<uint32_t>(value); |
| 385 | entry.dex_register_map_hash += static_cast<uint32_t>(kind); |
| 386 | stack_maps_.Put(stack_maps_.Size() - 1, entry); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 387 | } |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 390 | private: |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 391 | // Returns the index of an entry with the same dex register map |
| 392 | // or kNoSameDexMapFound if no such entry exists. |
| 393 | size_t FindEntryWithTheSameDexMap(size_t entry_index) { |
| 394 | StackMapEntry entry = stack_maps_.Get(entry_index); |
| 395 | auto entries_it = dex_map_hash_to_stack_map_indices_.find(entry.dex_register_map_hash); |
| 396 | if (entries_it == dex_map_hash_to_stack_map_indices_.end()) { |
| 397 | // We don't have a perfect hash functions so we need a list to collect all stack maps |
| 398 | // which might have the same dex register map. |
| 399 | GrowableArray<uint32_t> stack_map_indices(allocator_, 1); |
| 400 | stack_map_indices.Add(entry_index); |
| 401 | dex_map_hash_to_stack_map_indices_.Put(entry.dex_register_map_hash, stack_map_indices); |
| 402 | return kNoSameDexMapFound; |
| 403 | } |
| 404 | |
| 405 | // TODO: We don't need to add ourselves to the map if we can guarantee that |
| 406 | // FindEntryWithTheSameDexMap is called just once per stack map entry. |
| 407 | // A good way to do this is to cache the offset in the stack map entry. This |
| 408 | // is easier to do if we add markers when the stack map constructions begins |
| 409 | // and when it ends. |
| 410 | |
| 411 | // We might have collisions, so we need to check whether or not we should |
| 412 | // add the entry to the map. `needs_to_be_added` keeps track of this. |
| 413 | bool needs_to_be_added = true; |
| 414 | size_t result = kNoSameDexMapFound; |
| 415 | for (size_t i = 0; i < entries_it->second.Size(); i++) { |
| 416 | size_t test_entry_index = entries_it->second.Get(i); |
| 417 | if (test_entry_index == entry_index) { |
| 418 | needs_to_be_added = false; |
| 419 | } else if (HaveTheSameDexMaps(stack_maps_.Get(test_entry_index), entry)) { |
| 420 | result = test_entry_index; |
| 421 | needs_to_be_added = false; |
| 422 | break; |
| 423 | } |
| 424 | } |
| 425 | if (needs_to_be_added) { |
| 426 | entries_it->second.Add(entry_index); |
| 427 | } |
| 428 | return result; |
| 429 | } |
| 430 | |
| 431 | bool HaveTheSameDexMaps(const StackMapEntry& a, const StackMapEntry& b) const { |
| 432 | if (a.live_dex_registers_mask == nullptr && b.live_dex_registers_mask == nullptr) { |
| 433 | return true; |
| 434 | } |
| 435 | if (a.live_dex_registers_mask == nullptr || b.live_dex_registers_mask == nullptr) { |
| 436 | return false; |
| 437 | } |
| 438 | if (a.num_dex_registers != b.num_dex_registers) { |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | int index_in_dex_register_locations = 0; |
| 443 | for (uint32_t i = 0; i < a.num_dex_registers; i++) { |
| 444 | if (a.live_dex_registers_mask->IsBitSet(i) != b.live_dex_registers_mask->IsBitSet(i)) { |
| 445 | return false; |
| 446 | } |
| 447 | if (a.live_dex_registers_mask->IsBitSet(i)) { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame^] | 448 | size_t a_loc = dex_register_locations_.Get( |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 449 | a.dex_register_locations_start_index + index_in_dex_register_locations); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame^] | 450 | size_t b_loc = dex_register_locations_.Get( |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 451 | b.dex_register_locations_start_index + index_in_dex_register_locations); |
| 452 | if (a_loc != b_loc) { |
| 453 | return false; |
| 454 | } |
| 455 | ++index_in_dex_register_locations; |
| 456 | } |
| 457 | } |
| 458 | return true; |
| 459 | } |
| 460 | |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 461 | ArenaAllocator* allocator_; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 462 | GrowableArray<StackMapEntry> stack_maps_; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame^] | 463 | |
| 464 | // A catalog of unique [location_kind, register_value] pairs (per method). |
| 465 | GrowableArray<DexRegisterLocation> location_catalog_entries_; |
| 466 | // Map from Dex register location catalog entries to their indices in the |
| 467 | // location catalog. |
| 468 | typedef HashMap<DexRegisterLocation, size_t, LocationCatalogEntriesIndicesEmptyFn, |
| 469 | DexRegisterLocationHashFn> LocationCatalogEntriesIndices; |
| 470 | LocationCatalogEntriesIndices location_catalog_entries_indices_; |
| 471 | |
| 472 | // A set of concatenated maps of Dex register locations indices to |
| 473 | // `location_catalog_entries_`. |
| 474 | GrowableArray<size_t> dex_register_locations_; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 475 | GrowableArray<InlineInfoEntry> inline_infos_; |
| 476 | int stack_mask_max_; |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 477 | uint32_t dex_pc_max_; |
| 478 | uint32_t native_pc_offset_max_; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 479 | size_t number_of_stack_maps_with_inline_info_; |
| 480 | |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 481 | ArenaSafeMap<uint32_t, GrowableArray<uint32_t>> dex_map_hash_to_stack_map_indices_; |
| 482 | |
| 483 | static constexpr uint32_t kNoSameDexMapFound = -1; |
| 484 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 485 | DISALLOW_COPY_AND_ASSIGN(StackMapStream); |
| 486 | }; |
| 487 | |
| 488 | } // namespace art |
| 489 | |
| 490 | #endif // ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_ |