Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | */ |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 16 | #include "stack_map_stream.h" |
| 17 | |
| 18 | namespace art { |
| 19 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 20 | void StackMapStream::BeginStackMapEntry(uint32_t dex_pc, |
| 21 | uint32_t native_pc_offset, |
| 22 | uint32_t register_mask, |
| 23 | BitVector* sp_mask, |
| 24 | uint32_t num_dex_registers, |
| 25 | uint8_t inlining_depth) { |
| 26 | DCHECK_EQ(0u, current_entry_.dex_pc) << "EndStackMapEntry not called after BeginStackMapEntry"; |
| 27 | current_entry_.dex_pc = dex_pc; |
| 28 | current_entry_.native_pc_offset = native_pc_offset; |
| 29 | current_entry_.register_mask = register_mask; |
| 30 | current_entry_.sp_mask = sp_mask; |
| 31 | current_entry_.num_dex_registers = num_dex_registers; |
| 32 | current_entry_.inlining_depth = inlining_depth; |
| 33 | current_entry_.dex_register_locations_start_index = dex_register_locations_.Size(); |
| 34 | current_entry_.inline_infos_start_index = inline_infos_.Size(); |
| 35 | current_entry_.dex_register_map_hash = 0; |
| 36 | current_entry_.same_dex_register_map_as_ = kNoSameDexMapFound; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 37 | if (num_dex_registers != 0) { |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 38 | current_entry_.live_dex_registers_mask = |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 39 | new (allocator_) ArenaBitVector(allocator_, num_dex_registers, true); |
| 40 | } else { |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 41 | current_entry_.live_dex_registers_mask = nullptr; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 42 | } |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 43 | |
| 44 | if (sp_mask != nullptr) { |
| 45 | stack_mask_max_ = std::max(stack_mask_max_, sp_mask->GetHighestBitSet()); |
| 46 | } |
| 47 | if (inlining_depth > 0) { |
| 48 | number_of_stack_maps_with_inline_info_++; |
| 49 | } |
| 50 | |
| 51 | dex_pc_max_ = std::max(dex_pc_max_, dex_pc); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 52 | register_mask_max_ = std::max(register_mask_max_, register_mask); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 53 | current_dex_register_ = 0; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 56 | void StackMapStream::EndStackMapEntry() { |
| 57 | current_entry_.same_dex_register_map_as_ = FindEntryWithTheSameDexMap(); |
| 58 | stack_maps_.Add(current_entry_); |
| 59 | current_entry_ = StackMapEntry(); |
| 60 | } |
| 61 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 62 | void StackMapStream::AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value) { |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 63 | if (kind != DexRegisterLocation::Kind::kNone) { |
| 64 | // Ensure we only use non-compressed location kind at this stage. |
| 65 | DCHECK(DexRegisterLocation::IsShortLocationKind(kind)) |
| 66 | << DexRegisterLocation::PrettyDescriptor(kind); |
| 67 | DexRegisterLocation location(kind, value); |
| 68 | |
| 69 | // Look for Dex register `location` in the location catalog (using the |
| 70 | // companion hash map of locations to indices). Use its index if it |
| 71 | // is already in the location catalog. If not, insert it (in the |
| 72 | // location catalog and the hash map) and use the newly created index. |
| 73 | auto it = location_catalog_entries_indices_.Find(location); |
| 74 | if (it != location_catalog_entries_indices_.end()) { |
| 75 | // Retrieve the index from the hash map. |
| 76 | dex_register_locations_.Add(it->second); |
| 77 | } else { |
| 78 | // Create a new entry in the location catalog and the hash map. |
| 79 | size_t index = location_catalog_entries_.Size(); |
| 80 | location_catalog_entries_.Add(location); |
| 81 | dex_register_locations_.Add(index); |
| 82 | location_catalog_entries_indices_.Insert(std::make_pair(location, index)); |
| 83 | } |
| 84 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 85 | if (in_inline_frame_) { |
| 86 | // TODO: Support sharing DexRegisterMap across InlineInfo. |
| 87 | DCHECK_LT(current_dex_register_, current_inline_info_.num_dex_registers); |
| 88 | current_inline_info_.live_dex_registers_mask->SetBit(current_dex_register_); |
| 89 | } else { |
| 90 | DCHECK_LT(current_dex_register_, current_entry_.num_dex_registers); |
| 91 | current_entry_.live_dex_registers_mask->SetBit(current_dex_register_); |
| 92 | current_entry_.dex_register_map_hash += (1 << |
| 93 | (current_dex_register_ % (sizeof(current_entry_.dex_register_map_hash) * kBitsPerByte))); |
| 94 | current_entry_.dex_register_map_hash += static_cast<uint32_t>(value); |
| 95 | current_entry_.dex_register_map_hash += static_cast<uint32_t>(kind); |
| 96 | } |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 97 | } |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 98 | current_dex_register_++; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 101 | void StackMapStream::BeginInlineInfoEntry(uint32_t method_index, |
| 102 | uint32_t dex_pc, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 103 | InvokeType invoke_type, |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 104 | uint32_t num_dex_registers) { |
| 105 | DCHECK(!in_inline_frame_); |
| 106 | in_inline_frame_ = true; |
| 107 | current_inline_info_.method_index = method_index; |
| 108 | current_inline_info_.dex_pc = dex_pc; |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 109 | current_inline_info_.invoke_type = invoke_type; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 110 | current_inline_info_.num_dex_registers = num_dex_registers; |
| 111 | current_inline_info_.dex_register_locations_start_index = dex_register_locations_.Size(); |
| 112 | if (num_dex_registers != 0) { |
| 113 | current_inline_info_.live_dex_registers_mask = |
| 114 | new (allocator_) ArenaBitVector(allocator_, num_dex_registers, true); |
| 115 | } else { |
| 116 | current_inline_info_.live_dex_registers_mask = nullptr; |
| 117 | } |
| 118 | current_dex_register_ = 0; |
| 119 | } |
| 120 | |
| 121 | void StackMapStream::EndInlineInfoEntry() { |
| 122 | DCHECK(in_inline_frame_); |
| 123 | DCHECK_EQ(current_dex_register_, current_inline_info_.num_dex_registers) |
| 124 | << "Inline information contains less registers than expected"; |
| 125 | in_inline_frame_ = false; |
| 126 | inline_infos_.Add(current_inline_info_); |
| 127 | current_inline_info_ = InlineInfoEntry(); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 130 | uint32_t StackMapStream::ComputeMaxNativePcOffset() const { |
| 131 | uint32_t max_native_pc_offset = 0u; |
| 132 | for (size_t i = 0, size = stack_maps_.Size(); i != size; ++i) { |
| 133 | max_native_pc_offset = std::max(max_native_pc_offset, stack_maps_.Get(i).native_pc_offset); |
| 134 | } |
| 135 | return max_native_pc_offset; |
| 136 | } |
| 137 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 138 | size_t StackMapStream::PrepareForFillIn() { |
| 139 | int stack_mask_number_of_bits = stack_mask_max_ + 1; // Need room for max element too. |
| 140 | stack_mask_size_ = RoundUp(stack_mask_number_of_bits, kBitsPerByte) / kBitsPerByte; |
| 141 | inline_info_size_ = ComputeInlineInfoSize(); |
| 142 | dex_register_maps_size_ = ComputeDexRegisterMapsSize(); |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 143 | uint32_t max_native_pc_offset = ComputeMaxNativePcOffset(); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 144 | stack_map_encoding_ = StackMapEncoding::CreateFromSizes(stack_mask_size_, |
| 145 | inline_info_size_, |
| 146 | dex_register_maps_size_, |
| 147 | dex_pc_max_, |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 148 | max_native_pc_offset, |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 149 | register_mask_max_); |
| 150 | stack_maps_size_ = stack_maps_.Size() * stack_map_encoding_.ComputeStackMapSize(); |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 151 | dex_register_location_catalog_size_ = ComputeDexRegisterLocationCatalogSize(); |
| 152 | |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 153 | // Note: use RoundUp to word-size here if you want CodeInfo objects to be word aligned. |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 154 | needed_size_ = CodeInfo::kFixedSize |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 155 | + stack_maps_size_ |
Nicolas Geoffray | 6530baf | 2015-05-26 15:22:58 +0100 | [diff] [blame] | 156 | + dex_register_location_catalog_size_ |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 157 | + dex_register_maps_size_ |
| 158 | + inline_info_size_; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 159 | |
Nicolas Geoffray | 6530baf | 2015-05-26 15:22:58 +0100 | [diff] [blame] | 160 | stack_maps_start_ = CodeInfo::kFixedSize; |
| 161 | // TODO: Move the catalog at the end. It is currently too expensive at runtime |
| 162 | // to compute its size (note that we do not encode that size in the CodeInfo). |
| 163 | dex_register_location_catalog_start_ = stack_maps_start_ + stack_maps_size_; |
| 164 | dex_register_maps_start_ = |
| 165 | dex_register_location_catalog_start_ + dex_register_location_catalog_size_; |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 166 | inline_infos_start_ = dex_register_maps_start_ + dex_register_maps_size_; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 167 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 168 | return needed_size_; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | size_t StackMapStream::ComputeDexRegisterLocationCatalogSize() const { |
| 172 | size_t size = DexRegisterLocationCatalog::kFixedSize; |
| 173 | for (size_t location_catalog_entry_index = 0; |
| 174 | location_catalog_entry_index < location_catalog_entries_.Size(); |
| 175 | ++location_catalog_entry_index) { |
| 176 | DexRegisterLocation dex_register_location = |
| 177 | location_catalog_entries_.Get(location_catalog_entry_index); |
| 178 | size += DexRegisterLocationCatalog::EntrySize(dex_register_location); |
| 179 | } |
| 180 | return size; |
| 181 | } |
| 182 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 183 | size_t StackMapStream::ComputeDexRegisterMapSize(uint32_t num_dex_registers, |
| 184 | const BitVector& live_dex_registers_mask) const { |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 185 | // Size of the map in bytes. |
| 186 | size_t size = DexRegisterMap::kFixedSize; |
| 187 | // Add the live bit mask for the Dex register liveness. |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 188 | size += DexRegisterMap::GetLiveBitMaskSize(num_dex_registers); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 189 | // Compute the size of the set of live Dex register entries. |
| 190 | size_t number_of_live_dex_registers = 0; |
| 191 | for (size_t dex_register_number = 0; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 192 | dex_register_number < num_dex_registers; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 193 | ++dex_register_number) { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 194 | if (live_dex_registers_mask.IsBitSet(dex_register_number)) { |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 195 | ++number_of_live_dex_registers; |
| 196 | } |
| 197 | } |
| 198 | size_t map_entries_size_in_bits = |
| 199 | DexRegisterMap::SingleEntrySizeInBits(location_catalog_entries_.Size()) |
| 200 | * number_of_live_dex_registers; |
| 201 | size_t map_entries_size_in_bytes = |
| 202 | RoundUp(map_entries_size_in_bits, kBitsPerByte) / kBitsPerByte; |
| 203 | size += map_entries_size_in_bytes; |
| 204 | return size; |
| 205 | } |
| 206 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 207 | size_t StackMapStream::ComputeDexRegisterMapsSize() const { |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 208 | size_t size = 0; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 209 | size_t inline_info_index = 0; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 210 | for (size_t i = 0; i < stack_maps_.Size(); ++i) { |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 211 | StackMapEntry entry = stack_maps_.Get(i); |
| 212 | if (entry.same_dex_register_map_as_ == kNoSameDexMapFound) { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 213 | size += ComputeDexRegisterMapSize(entry.num_dex_registers, *entry.live_dex_registers_mask); |
| 214 | } else { |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 215 | // Entries with the same dex map will have the same offset. |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 216 | } |
| 217 | for (size_t j = 0; j < entry.inlining_depth; ++j) { |
| 218 | InlineInfoEntry inline_entry = inline_infos_.Get(inline_info_index++); |
| 219 | size += ComputeDexRegisterMapSize(inline_entry.num_dex_registers, |
| 220 | *inline_entry.live_dex_registers_mask); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | return size; |
| 224 | } |
| 225 | |
| 226 | size_t StackMapStream::ComputeInlineInfoSize() const { |
| 227 | return inline_infos_.Size() * InlineInfo::SingleEntrySize() |
| 228 | // For encoding the depth. |
| 229 | + (number_of_stack_maps_with_inline_info_ * InlineInfo::kFixedSize); |
| 230 | } |
| 231 | |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 232 | void StackMapStream::FillIn(MemoryRegion region) { |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 233 | DCHECK_EQ(0u, current_entry_.dex_pc) << "EndStackMapEntry not called after BeginStackMapEntry"; |
| 234 | DCHECK_NE(0u, needed_size_) << "PrepareForFillIn not called before FillIn"; |
| 235 | |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 236 | CodeInfo code_info(region); |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 237 | DCHECK_EQ(region.size(), needed_size_); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 238 | code_info.SetOverallSize(region.size()); |
| 239 | |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 240 | MemoryRegion dex_register_locations_region = region.Subregion( |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 241 | dex_register_maps_start_, dex_register_maps_size_); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 242 | |
| 243 | MemoryRegion inline_infos_region = region.Subregion( |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 244 | inline_infos_start_, inline_info_size_); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 245 | |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 246 | code_info.SetEncoding(stack_map_encoding_); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 247 | code_info.SetNumberOfStackMaps(stack_maps_.Size()); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 248 | DCHECK_EQ(code_info.GetStackMapsSize(code_info.ExtractEncoding()), stack_maps_size_); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 249 | |
| 250 | // Set the Dex register location catalog. |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 251 | code_info.SetNumberOfDexRegisterLocationCatalogEntries(location_catalog_entries_.Size()); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 252 | MemoryRegion dex_register_location_catalog_region = region.Subregion( |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 253 | dex_register_location_catalog_start_, dex_register_location_catalog_size_); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 254 | DexRegisterLocationCatalog dex_register_location_catalog(dex_register_location_catalog_region); |
| 255 | // Offset in `dex_register_location_catalog` where to store the next |
| 256 | // register location. |
| 257 | size_t location_catalog_offset = DexRegisterLocationCatalog::kFixedSize; |
| 258 | for (size_t i = 0, e = location_catalog_entries_.Size(); i < e; ++i) { |
| 259 | DexRegisterLocation dex_register_location = location_catalog_entries_.Get(i); |
| 260 | dex_register_location_catalog.SetRegisterInfo(location_catalog_offset, dex_register_location); |
| 261 | location_catalog_offset += DexRegisterLocationCatalog::EntrySize(dex_register_location); |
| 262 | } |
| 263 | // Ensure we reached the end of the Dex registers location_catalog. |
| 264 | DCHECK_EQ(location_catalog_offset, dex_register_location_catalog_region.size()); |
| 265 | |
| 266 | uintptr_t next_dex_register_map_offset = 0; |
| 267 | uintptr_t next_inline_info_offset = 0; |
| 268 | for (size_t i = 0, e = stack_maps_.Size(); i < e; ++i) { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 269 | StackMap stack_map = code_info.GetStackMapAt(i, stack_map_encoding_); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 270 | StackMapEntry entry = stack_maps_.Get(i); |
| 271 | |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 272 | stack_map.SetDexPc(stack_map_encoding_, entry.dex_pc); |
| 273 | stack_map.SetNativePcOffset(stack_map_encoding_, entry.native_pc_offset); |
| 274 | stack_map.SetRegisterMask(stack_map_encoding_, entry.register_mask); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 275 | if (entry.sp_mask != nullptr) { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 276 | stack_map.SetStackMask(stack_map_encoding_, *entry.sp_mask); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | if (entry.num_dex_registers == 0) { |
| 280 | // No dex map available. |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 281 | stack_map.SetDexRegisterMapOffset(stack_map_encoding_, StackMap::kNoDexRegisterMap); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 282 | } else { |
| 283 | // Search for an entry with the same dex map. |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 284 | if (entry.same_dex_register_map_as_ != kNoSameDexMapFound) { |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 285 | // If we have a hit reuse the offset. |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 286 | stack_map.SetDexRegisterMapOffset( |
| 287 | stack_map_encoding_, |
| 288 | code_info.GetStackMapAt(entry.same_dex_register_map_as_, stack_map_encoding_) |
| 289 | .GetDexRegisterMapOffset(stack_map_encoding_)); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 290 | } else { |
| 291 | // New dex registers maps should be added to the stack map. |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 292 | MemoryRegion register_region = dex_register_locations_region.Subregion( |
| 293 | next_dex_register_map_offset, |
| 294 | ComputeDexRegisterMapSize(entry.num_dex_registers, *entry.live_dex_registers_mask)); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 295 | next_dex_register_map_offset += register_region.size(); |
| 296 | DexRegisterMap dex_register_map(register_region); |
| 297 | stack_map.SetDexRegisterMapOffset( |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 298 | stack_map_encoding_, register_region.start() - dex_register_locations_region.start()); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 299 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 300 | // Set the dex register location. |
| 301 | FillInDexRegisterMap(dex_register_map, |
| 302 | entry.num_dex_registers, |
| 303 | *entry.live_dex_registers_mask, |
| 304 | entry.dex_register_locations_start_index); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
| 308 | // Set the inlining info. |
| 309 | if (entry.inlining_depth != 0) { |
| 310 | MemoryRegion inline_region = inline_infos_region.Subregion( |
| 311 | next_inline_info_offset, |
| 312 | InlineInfo::kFixedSize + entry.inlining_depth * InlineInfo::SingleEntrySize()); |
| 313 | next_inline_info_offset += inline_region.size(); |
| 314 | InlineInfo inline_info(inline_region); |
| 315 | |
| 316 | // Currently relative to the dex register map. |
| 317 | stack_map.SetInlineDescriptorOffset( |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 318 | stack_map_encoding_, inline_region.start() - dex_register_locations_region.start()); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 319 | |
| 320 | inline_info.SetDepth(entry.inlining_depth); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 321 | for (size_t depth = 0; depth < entry.inlining_depth; ++depth) { |
| 322 | InlineInfoEntry inline_entry = inline_infos_.Get(depth + entry.inline_infos_start_index); |
| 323 | inline_info.SetMethodIndexAtDepth(depth, inline_entry.method_index); |
| 324 | inline_info.SetDexPcAtDepth(depth, inline_entry.dex_pc); |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 325 | inline_info.SetInvokeTypeAtDepth(depth, inline_entry.invoke_type); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 326 | if (inline_entry.num_dex_registers == 0) { |
| 327 | // No dex map available. |
| 328 | inline_info.SetDexRegisterMapOffsetAtDepth(depth, StackMap::kNoDexRegisterMap); |
| 329 | DCHECK(inline_entry.live_dex_registers_mask == nullptr); |
| 330 | } else { |
| 331 | MemoryRegion register_region = dex_register_locations_region.Subregion( |
| 332 | next_dex_register_map_offset, |
| 333 | ComputeDexRegisterMapSize(inline_entry.num_dex_registers, |
| 334 | *inline_entry.live_dex_registers_mask)); |
| 335 | next_dex_register_map_offset += register_region.size(); |
| 336 | DexRegisterMap dex_register_map(register_region); |
| 337 | inline_info.SetDexRegisterMapOffsetAtDepth( |
| 338 | depth, register_region.start() - dex_register_locations_region.start()); |
| 339 | |
| 340 | FillInDexRegisterMap(dex_register_map, |
| 341 | inline_entry.num_dex_registers, |
| 342 | *inline_entry.live_dex_registers_mask, |
| 343 | inline_entry.dex_register_locations_start_index); |
| 344 | } |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 345 | } |
| 346 | } else { |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 347 | if (inline_info_size_ != 0) { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 348 | stack_map.SetInlineDescriptorOffset(stack_map_encoding_, StackMap::kNoInlineInfo); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 354 | void StackMapStream::FillInDexRegisterMap(DexRegisterMap dex_register_map, |
| 355 | uint32_t num_dex_registers, |
| 356 | const BitVector& live_dex_registers_mask, |
| 357 | uint32_t start_index_in_dex_register_locations) const { |
| 358 | dex_register_map.SetLiveBitMask(num_dex_registers, live_dex_registers_mask); |
| 359 | // Set the dex register location mapping data. |
| 360 | for (size_t dex_register_number = 0, index_in_dex_register_locations = 0; |
| 361 | dex_register_number < num_dex_registers; |
| 362 | ++dex_register_number) { |
| 363 | if (live_dex_registers_mask.IsBitSet(dex_register_number)) { |
| 364 | size_t location_catalog_entry_index = dex_register_locations_.Get( |
| 365 | start_index_in_dex_register_locations + index_in_dex_register_locations); |
| 366 | dex_register_map.SetLocationCatalogEntryIndex( |
| 367 | index_in_dex_register_locations, |
| 368 | location_catalog_entry_index, |
| 369 | num_dex_registers, |
| 370 | location_catalog_entries_.Size()); |
| 371 | ++index_in_dex_register_locations; |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 376 | size_t StackMapStream::FindEntryWithTheSameDexMap() { |
| 377 | size_t current_entry_index = stack_maps_.Size(); |
| 378 | auto entries_it = dex_map_hash_to_stack_map_indices_.find(current_entry_.dex_register_map_hash); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 379 | if (entries_it == dex_map_hash_to_stack_map_indices_.end()) { |
| 380 | // We don't have a perfect hash functions so we need a list to collect all stack maps |
| 381 | // which might have the same dex register map. |
| 382 | GrowableArray<uint32_t> stack_map_indices(allocator_, 1); |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 383 | stack_map_indices.Add(current_entry_index); |
| 384 | dex_map_hash_to_stack_map_indices_.Put(current_entry_.dex_register_map_hash, stack_map_indices); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 385 | return kNoSameDexMapFound; |
| 386 | } |
| 387 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 388 | // We might have collisions, so we need to check whether or not we really have a match. |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 389 | for (size_t i = 0; i < entries_it->second.Size(); i++) { |
| 390 | size_t test_entry_index = entries_it->second.Get(i); |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 391 | if (HaveTheSameDexMaps(stack_maps_.Get(test_entry_index), current_entry_)) { |
| 392 | return test_entry_index; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 393 | } |
| 394 | } |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 395 | entries_it->second.Add(current_entry_index); |
| 396 | return kNoSameDexMapFound; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | bool StackMapStream::HaveTheSameDexMaps(const StackMapEntry& a, const StackMapEntry& b) const { |
| 400 | if (a.live_dex_registers_mask == nullptr && b.live_dex_registers_mask == nullptr) { |
| 401 | return true; |
| 402 | } |
| 403 | if (a.live_dex_registers_mask == nullptr || b.live_dex_registers_mask == nullptr) { |
| 404 | return false; |
| 405 | } |
| 406 | if (a.num_dex_registers != b.num_dex_registers) { |
| 407 | return false; |
| 408 | } |
| 409 | |
| 410 | int index_in_dex_register_locations = 0; |
| 411 | for (uint32_t i = 0; i < a.num_dex_registers; i++) { |
| 412 | if (a.live_dex_registers_mask->IsBitSet(i) != b.live_dex_registers_mask->IsBitSet(i)) { |
| 413 | return false; |
| 414 | } |
| 415 | if (a.live_dex_registers_mask->IsBitSet(i)) { |
| 416 | size_t a_loc = dex_register_locations_.Get( |
| 417 | a.dex_register_locations_start_index + index_in_dex_register_locations); |
| 418 | size_t b_loc = dex_register_locations_.Get( |
| 419 | b.dex_register_locations_start_index + index_in_dex_register_locations); |
| 420 | if (a_loc != b_loc) { |
| 421 | return false; |
| 422 | } |
| 423 | ++index_in_dex_register_locations; |
| 424 | } |
| 425 | } |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | } // namespace art |