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