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 | */ |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 16 | |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 17 | #include "stack_map_stream.h" |
| 18 | |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 19 | #include <unordered_map> |
| 20 | |
| 21 | #include "base/stl_util.h" |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 22 | #include "art_method.h" |
| 23 | #include "runtime.h" |
| 24 | #include "scoped_thread_state_change-inl.h" |
| 25 | |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 26 | namespace art { |
| 27 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 28 | void StackMapStream::BeginStackMapEntry(uint32_t dex_pc, |
| 29 | uint32_t native_pc_offset, |
| 30 | uint32_t register_mask, |
| 31 | BitVector* sp_mask, |
| 32 | uint32_t num_dex_registers, |
| 33 | uint8_t inlining_depth) { |
| 34 | DCHECK_EQ(0u, current_entry_.dex_pc) << "EndStackMapEntry not called after BeginStackMapEntry"; |
Calin Juravle | b5c4693 | 2015-10-06 17:09:49 +0100 | [diff] [blame] | 35 | DCHECK_NE(dex_pc, static_cast<uint32_t>(-1)) << "invalid dex_pc"; |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 36 | current_entry_.dex_pc = dex_pc; |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 37 | current_entry_.native_pc_code_offset = CodeOffset::FromOffset(native_pc_offset, instruction_set_); |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 38 | current_entry_.register_mask = register_mask; |
| 39 | current_entry_.sp_mask = sp_mask; |
| 40 | current_entry_.num_dex_registers = num_dex_registers; |
| 41 | current_entry_.inlining_depth = inlining_depth; |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 42 | current_entry_.dex_register_locations_start_index = dex_register_locations_.size(); |
| 43 | current_entry_.inline_infos_start_index = inline_infos_.size(); |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 44 | current_entry_.dex_register_map_hash = 0; |
| 45 | current_entry_.same_dex_register_map_as_ = kNoSameDexMapFound; |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 46 | current_entry_.stack_mask_index = 0; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 47 | if (num_dex_registers != 0) { |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 48 | current_entry_.live_dex_registers_mask = |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 49 | ArenaBitVector::Create(allocator_, num_dex_registers, true, kArenaAllocStackMapStream); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 50 | } else { |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 51 | current_entry_.live_dex_registers_mask = nullptr; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 52 | } |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 53 | |
| 54 | if (sp_mask != nullptr) { |
| 55 | stack_mask_max_ = std::max(stack_mask_max_, sp_mask->GetHighestBitSet()); |
| 56 | } |
| 57 | if (inlining_depth > 0) { |
| 58 | number_of_stack_maps_with_inline_info_++; |
| 59 | } |
| 60 | |
| 61 | dex_pc_max_ = std::max(dex_pc_max_, dex_pc); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 62 | register_mask_max_ = std::max(register_mask_max_, register_mask); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 63 | current_dex_register_ = 0; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 66 | void StackMapStream::EndStackMapEntry() { |
| 67 | current_entry_.same_dex_register_map_as_ = FindEntryWithTheSameDexMap(); |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 68 | stack_maps_.push_back(current_entry_); |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 69 | current_entry_ = StackMapEntry(); |
| 70 | } |
| 71 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 72 | void StackMapStream::AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value) { |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 73 | if (kind != DexRegisterLocation::Kind::kNone) { |
| 74 | // Ensure we only use non-compressed location kind at this stage. |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 75 | DCHECK(DexRegisterLocation::IsShortLocationKind(kind)) << kind; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 76 | DexRegisterLocation location(kind, value); |
| 77 | |
| 78 | // Look for Dex register `location` in the location catalog (using the |
| 79 | // companion hash map of locations to indices). Use its index if it |
| 80 | // is already in the location catalog. If not, insert it (in the |
| 81 | // location catalog and the hash map) and use the newly created index. |
| 82 | auto it = location_catalog_entries_indices_.Find(location); |
| 83 | if (it != location_catalog_entries_indices_.end()) { |
| 84 | // Retrieve the index from the hash map. |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 85 | dex_register_locations_.push_back(it->second); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 86 | } else { |
| 87 | // Create a new entry in the location catalog and the hash map. |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 88 | size_t index = location_catalog_entries_.size(); |
| 89 | location_catalog_entries_.push_back(location); |
| 90 | dex_register_locations_.push_back(index); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 91 | location_catalog_entries_indices_.Insert(std::make_pair(location, index)); |
| 92 | } |
| 93 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 94 | if (in_inline_frame_) { |
| 95 | // TODO: Support sharing DexRegisterMap across InlineInfo. |
| 96 | DCHECK_LT(current_dex_register_, current_inline_info_.num_dex_registers); |
| 97 | current_inline_info_.live_dex_registers_mask->SetBit(current_dex_register_); |
| 98 | } else { |
| 99 | DCHECK_LT(current_dex_register_, current_entry_.num_dex_registers); |
| 100 | current_entry_.live_dex_registers_mask->SetBit(current_dex_register_); |
| 101 | current_entry_.dex_register_map_hash += (1 << |
| 102 | (current_dex_register_ % (sizeof(current_entry_.dex_register_map_hash) * kBitsPerByte))); |
| 103 | current_entry_.dex_register_map_hash += static_cast<uint32_t>(value); |
| 104 | current_entry_.dex_register_map_hash += static_cast<uint32_t>(kind); |
| 105 | } |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 106 | } |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 107 | current_dex_register_++; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 110 | static bool EncodeArtMethodInInlineInfo(ArtMethod* method ATTRIBUTE_UNUSED) { |
| 111 | // Note: the runtime is null only for unit testing. |
| 112 | return Runtime::Current() == nullptr || !Runtime::Current()->IsAotCompiler(); |
| 113 | } |
| 114 | |
| 115 | void StackMapStream::BeginInlineInfoEntry(ArtMethod* method, |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 116 | uint32_t dex_pc, |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 117 | uint32_t num_dex_registers, |
| 118 | const DexFile* outer_dex_file) { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 119 | DCHECK(!in_inline_frame_); |
| 120 | in_inline_frame_ = true; |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 121 | if (EncodeArtMethodInInlineInfo(method)) { |
| 122 | current_inline_info_.method = method; |
| 123 | } else { |
| 124 | if (dex_pc != static_cast<uint32_t>(-1) && kIsDebugBuild) { |
| 125 | ScopedObjectAccess soa(Thread::Current()); |
| 126 | DCHECK(IsSameDexFile(*outer_dex_file, *method->GetDexFile())); |
| 127 | } |
| 128 | current_inline_info_.method_index = method->GetDexMethodIndexUnchecked(); |
| 129 | } |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 130 | current_inline_info_.dex_pc = dex_pc; |
| 131 | current_inline_info_.num_dex_registers = num_dex_registers; |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 132 | current_inline_info_.dex_register_locations_start_index = dex_register_locations_.size(); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 133 | if (num_dex_registers != 0) { |
| 134 | current_inline_info_.live_dex_registers_mask = |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 135 | ArenaBitVector::Create(allocator_, num_dex_registers, true, kArenaAllocStackMapStream); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 136 | } else { |
| 137 | current_inline_info_.live_dex_registers_mask = nullptr; |
| 138 | } |
| 139 | current_dex_register_ = 0; |
| 140 | } |
| 141 | |
| 142 | void StackMapStream::EndInlineInfoEntry() { |
| 143 | DCHECK(in_inline_frame_); |
| 144 | DCHECK_EQ(current_dex_register_, current_inline_info_.num_dex_registers) |
| 145 | << "Inline information contains less registers than expected"; |
| 146 | in_inline_frame_ = false; |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 147 | inline_infos_.push_back(current_inline_info_); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 148 | current_inline_info_ = InlineInfoEntry(); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 149 | } |
| 150 | |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 151 | CodeOffset StackMapStream::ComputeMaxNativePcCodeOffset() const { |
| 152 | CodeOffset max_native_pc_offset; |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 153 | for (const StackMapEntry& entry : stack_maps_) { |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 154 | max_native_pc_offset = std::max(max_native_pc_offset, entry.native_pc_code_offset); |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 155 | } |
| 156 | return max_native_pc_offset; |
| 157 | } |
| 158 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 159 | size_t StackMapStream::PrepareForFillIn() { |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 160 | const size_t stack_mask_size_in_bits = stack_mask_max_ + 1; // Need room for max element too. |
| 161 | const size_t number_of_stack_masks = PrepareStackMasks(stack_mask_size_in_bits); |
| 162 | const size_t register_mask_size_in_bits = MinimumBitsToStore(register_mask_max_); |
| 163 | const size_t number_of_register_masks = PrepareRegisterMasks(); |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 164 | dex_register_maps_size_ = ComputeDexRegisterMapsSize(); |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 165 | ComputeInlineInfoEncoding(); // needs dex_register_maps_size_. |
| 166 | inline_info_size_ = inline_infos_.size() * inline_info_encoding_.GetEntrySize(); |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 167 | CodeOffset max_native_pc_offset = ComputeMaxNativePcCodeOffset(); |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 168 | // The stack map contains compressed native PC offsets. |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 169 | const size_t stack_map_size = stack_map_encoding_.SetFromSizes( |
| 170 | max_native_pc_offset.CompressedValue(), |
| 171 | dex_pc_max_, |
| 172 | dex_register_maps_size_, |
| 173 | inline_info_size_, |
| 174 | number_of_register_masks, |
| 175 | number_of_stack_masks); |
Mathieu Chartier | 12f1b99 | 2017-01-19 18:00:45 -0800 | [diff] [blame] | 176 | stack_maps_size_ = RoundUp(stack_maps_.size() * stack_map_size, kBitsPerByte) / kBitsPerByte; |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 177 | dex_register_location_catalog_size_ = ComputeDexRegisterLocationCatalogSize(); |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 178 | const size_t stack_masks_bits = number_of_stack_masks * stack_mask_size_in_bits; |
| 179 | const size_t register_masks_bits = number_of_register_masks * register_mask_size_in_bits; |
| 180 | // Register masks are last, stack masks are right before that last. |
| 181 | // They are both bit packed / aligned. |
| 182 | const size_t non_header_size = |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 183 | stack_maps_size_ + |
| 184 | dex_register_location_catalog_size_ + |
| 185 | dex_register_maps_size_ + |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 186 | inline_info_size_ + |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 187 | RoundUp(stack_masks_bits + register_masks_bits, kBitsPerByte) / kBitsPerByte; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 188 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 189 | // Prepare the CodeInfo variable-sized encoding. |
| 190 | CodeInfoEncoding code_info_encoding; |
| 191 | code_info_encoding.non_header_size = non_header_size; |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 192 | code_info_encoding.number_of_stack_maps = stack_maps_.size(); |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 193 | code_info_encoding.number_of_stack_masks = number_of_stack_masks; |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 194 | code_info_encoding.number_of_register_masks = number_of_register_masks; |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 195 | code_info_encoding.stack_mask_size_in_bits = stack_mask_size_in_bits; |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 196 | code_info_encoding.register_mask_size_in_bits = register_mask_size_in_bits; |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 197 | code_info_encoding.stack_map_encoding = stack_map_encoding_; |
| 198 | code_info_encoding.inline_info_encoding = inline_info_encoding_; |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 199 | code_info_encoding.number_of_location_catalog_entries = location_catalog_entries_.size(); |
| 200 | code_info_encoding.Compress(&code_info_encoding_); |
| 201 | |
Nicolas Geoffray | 6530baf | 2015-05-26 15:22:58 +0100 | [diff] [blame] | 202 | // TODO: Move the catalog at the end. It is currently too expensive at runtime |
| 203 | // to compute its size (note that we do not encode that size in the CodeInfo). |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 204 | dex_register_location_catalog_start_ = code_info_encoding_.size() + stack_maps_size_; |
Nicolas Geoffray | 6530baf | 2015-05-26 15:22:58 +0100 | [diff] [blame] | 205 | dex_register_maps_start_ = |
| 206 | dex_register_location_catalog_start_ + dex_register_location_catalog_size_; |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 207 | inline_infos_start_ = dex_register_maps_start_ + dex_register_maps_size_; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 208 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 209 | needed_size_ = code_info_encoding_.size() + non_header_size; |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 210 | return needed_size_; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | size_t StackMapStream::ComputeDexRegisterLocationCatalogSize() const { |
| 214 | size_t size = DexRegisterLocationCatalog::kFixedSize; |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 215 | for (const DexRegisterLocation& dex_register_location : location_catalog_entries_) { |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 216 | size += DexRegisterLocationCatalog::EntrySize(dex_register_location); |
| 217 | } |
| 218 | return size; |
| 219 | } |
| 220 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 221 | size_t StackMapStream::ComputeDexRegisterMapSize(uint32_t num_dex_registers, |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 222 | const BitVector* live_dex_registers_mask) const { |
| 223 | // For num_dex_registers == 0u live_dex_registers_mask may be null. |
| 224 | if (num_dex_registers == 0u) { |
| 225 | return 0u; // No register map will be emitted. |
| 226 | } |
| 227 | DCHECK(live_dex_registers_mask != nullptr); |
| 228 | |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 229 | // Size of the map in bytes. |
| 230 | size_t size = DexRegisterMap::kFixedSize; |
| 231 | // Add the live bit mask for the Dex register liveness. |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 232 | size += DexRegisterMap::GetLiveBitMaskSize(num_dex_registers); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 233 | // Compute the size of the set of live Dex register entries. |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 234 | size_t number_of_live_dex_registers = live_dex_registers_mask->NumSetBits(); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 235 | size_t map_entries_size_in_bits = |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 236 | DexRegisterMap::SingleEntrySizeInBits(location_catalog_entries_.size()) |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 237 | * number_of_live_dex_registers; |
| 238 | size_t map_entries_size_in_bytes = |
| 239 | RoundUp(map_entries_size_in_bits, kBitsPerByte) / kBitsPerByte; |
| 240 | size += map_entries_size_in_bytes; |
| 241 | return size; |
| 242 | } |
| 243 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 244 | size_t StackMapStream::ComputeDexRegisterMapsSize() const { |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 245 | size_t size = 0; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 246 | size_t inline_info_index = 0; |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 247 | for (const StackMapEntry& entry : stack_maps_) { |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 248 | if (entry.same_dex_register_map_as_ == kNoSameDexMapFound) { |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 249 | size += ComputeDexRegisterMapSize(entry.num_dex_registers, entry.live_dex_registers_mask); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 250 | } else { |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 251 | // Entries with the same dex map will have the same offset. |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 252 | } |
| 253 | for (size_t j = 0; j < entry.inlining_depth; ++j) { |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 254 | InlineInfoEntry inline_entry = inline_infos_[inline_info_index++]; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 255 | size += ComputeDexRegisterMapSize(inline_entry.num_dex_registers, |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 256 | inline_entry.live_dex_registers_mask); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | return size; |
| 260 | } |
| 261 | |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 262 | void StackMapStream::ComputeInlineInfoEncoding() { |
| 263 | uint32_t method_index_max = 0; |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 264 | uint32_t dex_pc_max = DexFile::kDexNoIndex; |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 265 | uint32_t extra_data_max = 0; |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 266 | |
| 267 | uint32_t inline_info_index = 0; |
| 268 | for (const StackMapEntry& entry : stack_maps_) { |
| 269 | for (size_t j = 0; j < entry.inlining_depth; ++j) { |
| 270 | InlineInfoEntry inline_entry = inline_infos_[inline_info_index++]; |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 271 | if (inline_entry.method == nullptr) { |
| 272 | method_index_max = std::max(method_index_max, inline_entry.method_index); |
| 273 | extra_data_max = std::max(extra_data_max, 1u); |
| 274 | } else { |
| 275 | method_index_max = std::max( |
| 276 | method_index_max, High32Bits(reinterpret_cast<uintptr_t>(inline_entry.method))); |
| 277 | extra_data_max = std::max( |
| 278 | extra_data_max, Low32Bits(reinterpret_cast<uintptr_t>(inline_entry.method))); |
| 279 | } |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 280 | if (inline_entry.dex_pc != DexFile::kDexNoIndex && |
| 281 | (dex_pc_max == DexFile::kDexNoIndex || dex_pc_max < inline_entry.dex_pc)) { |
| 282 | dex_pc_max = inline_entry.dex_pc; |
| 283 | } |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | DCHECK_EQ(inline_info_index, inline_infos_.size()); |
| 287 | |
| 288 | inline_info_encoding_.SetFromSizes(method_index_max, |
| 289 | dex_pc_max, |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 290 | extra_data_max, |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 291 | dex_register_maps_size_); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 294 | void StackMapStream::FillIn(MemoryRegion region) { |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 295 | DCHECK_EQ(0u, current_entry_.dex_pc) << "EndStackMapEntry not called after BeginStackMapEntry"; |
| 296 | DCHECK_NE(0u, needed_size_) << "PrepareForFillIn not called before FillIn"; |
| 297 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 298 | DCHECK_EQ(region.size(), needed_size_); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 299 | |
| 300 | // Note that the memory region does not have to be zeroed when we JIT code |
| 301 | // because we do not use the arena allocator there. |
| 302 | |
| 303 | // Write the CodeInfo header. |
| 304 | region.CopyFrom(0, MemoryRegion(code_info_encoding_.data(), code_info_encoding_.size())); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 305 | |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 306 | MemoryRegion dex_register_locations_region = region.Subregion( |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 307 | dex_register_maps_start_, dex_register_maps_size_); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 308 | |
| 309 | MemoryRegion inline_infos_region = region.Subregion( |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 310 | inline_infos_start_, inline_info_size_); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 311 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 312 | CodeInfo code_info(region); |
| 313 | CodeInfoEncoding encoding = code_info.ExtractEncoding(); |
| 314 | DCHECK_EQ(code_info.GetStackMapsSize(encoding), stack_maps_size_); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 315 | |
| 316 | // Set the Dex register location catalog. |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 317 | MemoryRegion dex_register_location_catalog_region = region.Subregion( |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 318 | dex_register_location_catalog_start_, dex_register_location_catalog_size_); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 319 | DexRegisterLocationCatalog dex_register_location_catalog(dex_register_location_catalog_region); |
| 320 | // Offset in `dex_register_location_catalog` where to store the next |
| 321 | // register location. |
| 322 | size_t location_catalog_offset = DexRegisterLocationCatalog::kFixedSize; |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 323 | for (DexRegisterLocation dex_register_location : location_catalog_entries_) { |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 324 | dex_register_location_catalog.SetRegisterInfo(location_catalog_offset, dex_register_location); |
| 325 | location_catalog_offset += DexRegisterLocationCatalog::EntrySize(dex_register_location); |
| 326 | } |
| 327 | // Ensure we reached the end of the Dex registers location_catalog. |
| 328 | DCHECK_EQ(location_catalog_offset, dex_register_location_catalog_region.size()); |
| 329 | |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 330 | ArenaBitVector empty_bitmask(allocator_, 0, /* expandable */ false, kArenaAllocStackMapStream); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 331 | uintptr_t next_dex_register_map_offset = 0; |
| 332 | uintptr_t next_inline_info_offset = 0; |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 333 | for (size_t i = 0, e = stack_maps_.size(); i < e; ++i) { |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 334 | StackMap stack_map = code_info.GetStackMapAt(i, encoding); |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 335 | StackMapEntry entry = stack_maps_[i]; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 336 | |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 337 | stack_map.SetDexPc(stack_map_encoding_, entry.dex_pc); |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 338 | stack_map.SetNativePcCodeOffset(stack_map_encoding_, entry.native_pc_code_offset); |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 339 | stack_map.SetRegisterMaskIndex(stack_map_encoding_, entry.register_mask_index); |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 340 | stack_map.SetStackMaskIndex(stack_map_encoding_, entry.stack_mask_index); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 341 | |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 342 | if (entry.num_dex_registers == 0 || (entry.live_dex_registers_mask->NumSetBits() == 0)) { |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 343 | // No dex map available. |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 344 | stack_map.SetDexRegisterMapOffset(stack_map_encoding_, StackMap::kNoDexRegisterMap); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 345 | } else { |
| 346 | // Search for an entry with the same dex map. |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 347 | if (entry.same_dex_register_map_as_ != kNoSameDexMapFound) { |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 348 | // If we have a hit reuse the offset. |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 349 | stack_map.SetDexRegisterMapOffset( |
| 350 | stack_map_encoding_, |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 351 | code_info.GetStackMapAt(entry.same_dex_register_map_as_, encoding) |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 352 | .GetDexRegisterMapOffset(stack_map_encoding_)); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 353 | } else { |
| 354 | // New dex registers maps should be added to the stack map. |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 355 | MemoryRegion register_region = dex_register_locations_region.Subregion( |
| 356 | next_dex_register_map_offset, |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 357 | ComputeDexRegisterMapSize(entry.num_dex_registers, entry.live_dex_registers_mask)); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 358 | next_dex_register_map_offset += register_region.size(); |
| 359 | DexRegisterMap dex_register_map(register_region); |
| 360 | stack_map.SetDexRegisterMapOffset( |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 361 | stack_map_encoding_, register_region.begin() - dex_register_locations_region.begin()); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 362 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 363 | // Set the dex register location. |
| 364 | FillInDexRegisterMap(dex_register_map, |
| 365 | entry.num_dex_registers, |
| 366 | *entry.live_dex_registers_mask, |
| 367 | entry.dex_register_locations_start_index); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
| 371 | // Set the inlining info. |
| 372 | if (entry.inlining_depth != 0) { |
| 373 | MemoryRegion inline_region = inline_infos_region.Subregion( |
| 374 | next_inline_info_offset, |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 375 | entry.inlining_depth * inline_info_encoding_.GetEntrySize()); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 376 | next_inline_info_offset += inline_region.size(); |
| 377 | InlineInfo inline_info(inline_region); |
| 378 | |
| 379 | // Currently relative to the dex register map. |
| 380 | stack_map.SetInlineDescriptorOffset( |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 381 | stack_map_encoding_, inline_region.begin() - dex_register_locations_region.begin()); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 382 | |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 383 | inline_info.SetDepth(inline_info_encoding_, entry.inlining_depth); |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 384 | DCHECK_LE(entry.inline_infos_start_index + entry.inlining_depth, inline_infos_.size()); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 385 | for (size_t depth = 0; depth < entry.inlining_depth; ++depth) { |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 386 | InlineInfoEntry inline_entry = inline_infos_[depth + entry.inline_infos_start_index]; |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 387 | if (inline_entry.method != nullptr) { |
| 388 | inline_info.SetMethodIndexAtDepth( |
| 389 | inline_info_encoding_, |
| 390 | depth, |
| 391 | High32Bits(reinterpret_cast<uintptr_t>(inline_entry.method))); |
| 392 | inline_info.SetExtraDataAtDepth( |
| 393 | inline_info_encoding_, |
| 394 | depth, |
| 395 | Low32Bits(reinterpret_cast<uintptr_t>(inline_entry.method))); |
| 396 | } else { |
| 397 | inline_info.SetMethodIndexAtDepth(inline_info_encoding_, depth, inline_entry.method_index); |
| 398 | inline_info.SetExtraDataAtDepth(inline_info_encoding_, depth, 1); |
| 399 | } |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 400 | inline_info.SetDexPcAtDepth(inline_info_encoding_, depth, inline_entry.dex_pc); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 401 | if (inline_entry.num_dex_registers == 0) { |
| 402 | // No dex map available. |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 403 | inline_info.SetDexRegisterMapOffsetAtDepth(inline_info_encoding_, |
| 404 | depth, |
| 405 | StackMap::kNoDexRegisterMap); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 406 | DCHECK(inline_entry.live_dex_registers_mask == nullptr); |
| 407 | } else { |
| 408 | MemoryRegion register_region = dex_register_locations_region.Subregion( |
| 409 | next_dex_register_map_offset, |
| 410 | ComputeDexRegisterMapSize(inline_entry.num_dex_registers, |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 411 | inline_entry.live_dex_registers_mask)); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 412 | next_dex_register_map_offset += register_region.size(); |
| 413 | DexRegisterMap dex_register_map(register_region); |
| 414 | inline_info.SetDexRegisterMapOffsetAtDepth( |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 415 | inline_info_encoding_, |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 416 | depth, register_region.begin() - dex_register_locations_region.begin()); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 417 | |
| 418 | FillInDexRegisterMap(dex_register_map, |
| 419 | inline_entry.num_dex_registers, |
| 420 | *inline_entry.live_dex_registers_mask, |
| 421 | inline_entry.dex_register_locations_start_index); |
| 422 | } |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 423 | } |
| 424 | } else { |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 425 | if (inline_info_size_ != 0) { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 426 | stack_map.SetInlineDescriptorOffset(stack_map_encoding_, StackMap::kNoInlineInfo); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | } |
David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 430 | |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 431 | // Write stack masks table. |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 432 | size_t stack_mask_bits = encoding.stack_mask_size_in_bits; |
| 433 | if (stack_mask_bits > 0) { |
| 434 | size_t stack_mask_bytes = RoundUp(stack_mask_bits, kBitsPerByte) / kBitsPerByte; |
| 435 | for (size_t i = 0; i < encoding.number_of_stack_masks; ++i) { |
| 436 | MemoryRegion source(&stack_masks_[i * stack_mask_bytes], stack_mask_bytes); |
| 437 | BitMemoryRegion stack_mask = code_info.GetStackMask(encoding, i); |
| 438 | for (size_t bit_index = 0; bit_index < encoding.stack_mask_size_in_bits; ++bit_index) { |
| 439 | stack_mask.StoreBit(bit_index, source.LoadBit(bit_index)); |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 444 | // Write register masks table. |
| 445 | for (size_t i = 0; i < encoding.number_of_register_masks; ++i) { |
| 446 | BitMemoryRegion register_mask = code_info.GetRegisterMask(encoding, i); |
| 447 | register_mask.StoreBits(0, register_masks_[i], encoding.register_mask_size_in_bits); |
| 448 | } |
| 449 | |
David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 450 | // Verify all written data in debug build. |
| 451 | if (kIsDebugBuild) { |
| 452 | CheckCodeInfo(region); |
| 453 | } |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 454 | } |
| 455 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 456 | void StackMapStream::FillInDexRegisterMap(DexRegisterMap dex_register_map, |
| 457 | uint32_t num_dex_registers, |
| 458 | const BitVector& live_dex_registers_mask, |
| 459 | uint32_t start_index_in_dex_register_locations) const { |
| 460 | dex_register_map.SetLiveBitMask(num_dex_registers, live_dex_registers_mask); |
| 461 | // Set the dex register location mapping data. |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 462 | size_t number_of_live_dex_registers = live_dex_registers_mask.NumSetBits(); |
| 463 | DCHECK_LE(number_of_live_dex_registers, dex_register_locations_.size()); |
| 464 | DCHECK_LE(start_index_in_dex_register_locations, |
| 465 | dex_register_locations_.size() - number_of_live_dex_registers); |
| 466 | for (size_t index_in_dex_register_locations = 0; |
| 467 | index_in_dex_register_locations != number_of_live_dex_registers; |
| 468 | ++index_in_dex_register_locations) { |
| 469 | size_t location_catalog_entry_index = dex_register_locations_[ |
| 470 | start_index_in_dex_register_locations + index_in_dex_register_locations]; |
| 471 | dex_register_map.SetLocationCatalogEntryIndex( |
| 472 | index_in_dex_register_locations, |
| 473 | location_catalog_entry_index, |
| 474 | num_dex_registers, |
| 475 | location_catalog_entries_.size()); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 479 | size_t StackMapStream::FindEntryWithTheSameDexMap() { |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 480 | size_t current_entry_index = stack_maps_.size(); |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 481 | 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] | 482 | if (entries_it == dex_map_hash_to_stack_map_indices_.end()) { |
| 483 | // We don't have a perfect hash functions so we need a list to collect all stack maps |
| 484 | // which might have the same dex register map. |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 485 | ArenaVector<uint32_t> stack_map_indices(allocator_->Adapter(kArenaAllocStackMapStream)); |
| 486 | stack_map_indices.push_back(current_entry_index); |
| 487 | dex_map_hash_to_stack_map_indices_.Put(current_entry_.dex_register_map_hash, |
| 488 | std::move(stack_map_indices)); |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 489 | return kNoSameDexMapFound; |
| 490 | } |
| 491 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 492 | // We might have collisions, so we need to check whether or not we really have a match. |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 493 | for (uint32_t test_entry_index : entries_it->second) { |
| 494 | if (HaveTheSameDexMaps(GetStackMap(test_entry_index), current_entry_)) { |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 495 | return test_entry_index; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 496 | } |
| 497 | } |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 498 | entries_it->second.push_back(current_entry_index); |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 499 | return kNoSameDexMapFound; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | bool StackMapStream::HaveTheSameDexMaps(const StackMapEntry& a, const StackMapEntry& b) const { |
| 503 | if (a.live_dex_registers_mask == nullptr && b.live_dex_registers_mask == nullptr) { |
| 504 | return true; |
| 505 | } |
| 506 | if (a.live_dex_registers_mask == nullptr || b.live_dex_registers_mask == nullptr) { |
| 507 | return false; |
| 508 | } |
| 509 | if (a.num_dex_registers != b.num_dex_registers) { |
| 510 | return false; |
| 511 | } |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 512 | if (a.num_dex_registers != 0u) { |
| 513 | DCHECK(a.live_dex_registers_mask != nullptr); |
| 514 | DCHECK(b.live_dex_registers_mask != nullptr); |
| 515 | if (!a.live_dex_registers_mask->Equal(b.live_dex_registers_mask)) { |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 516 | return false; |
| 517 | } |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 518 | size_t number_of_live_dex_registers = a.live_dex_registers_mask->NumSetBits(); |
| 519 | DCHECK_LE(number_of_live_dex_registers, dex_register_locations_.size()); |
| 520 | DCHECK_LE(a.dex_register_locations_start_index, |
| 521 | dex_register_locations_.size() - number_of_live_dex_registers); |
| 522 | DCHECK_LE(b.dex_register_locations_start_index, |
| 523 | dex_register_locations_.size() - number_of_live_dex_registers); |
| 524 | auto a_begin = dex_register_locations_.begin() + a.dex_register_locations_start_index; |
| 525 | auto b_begin = dex_register_locations_.begin() + b.dex_register_locations_start_index; |
| 526 | if (!std::equal(a_begin, a_begin + number_of_live_dex_registers, b_begin)) { |
| 527 | return false; |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | return true; |
| 531 | } |
| 532 | |
David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 533 | // Helper for CheckCodeInfo - check that register map has the expected content. |
| 534 | void StackMapStream::CheckDexRegisterMap(const CodeInfo& code_info, |
| 535 | const DexRegisterMap& dex_register_map, |
| 536 | size_t num_dex_registers, |
| 537 | BitVector* live_dex_registers_mask, |
| 538 | size_t dex_register_locations_index) const { |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 539 | CodeInfoEncoding encoding = code_info.ExtractEncoding(); |
David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 540 | for (size_t reg = 0; reg < num_dex_registers; reg++) { |
| 541 | // Find the location we tried to encode. |
| 542 | DexRegisterLocation expected = DexRegisterLocation::None(); |
| 543 | if (live_dex_registers_mask->IsBitSet(reg)) { |
| 544 | size_t catalog_index = dex_register_locations_[dex_register_locations_index++]; |
| 545 | expected = location_catalog_entries_[catalog_index]; |
| 546 | } |
| 547 | // Compare to the seen location. |
| 548 | if (expected.GetKind() == DexRegisterLocation::Kind::kNone) { |
| 549 | DCHECK(!dex_register_map.IsValid() || !dex_register_map.IsDexRegisterLive(reg)); |
| 550 | } else { |
| 551 | DCHECK(dex_register_map.IsDexRegisterLive(reg)); |
| 552 | DexRegisterLocation seen = dex_register_map.GetDexRegisterLocation( |
| 553 | reg, num_dex_registers, code_info, encoding); |
| 554 | DCHECK_EQ(expected.GetKind(), seen.GetKind()); |
| 555 | DCHECK_EQ(expected.GetValue(), seen.GetValue()); |
| 556 | } |
| 557 | } |
| 558 | if (num_dex_registers == 0) { |
| 559 | DCHECK(!dex_register_map.IsValid()); |
| 560 | } |
| 561 | } |
| 562 | |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 563 | size_t StackMapStream::PrepareRegisterMasks() { |
| 564 | register_masks_.resize(stack_maps_.size(), 0u); |
| 565 | std::unordered_map<uint32_t, size_t> dedupe; |
| 566 | for (StackMapEntry& stack_map : stack_maps_) { |
| 567 | const size_t index = dedupe.size(); |
| 568 | stack_map.register_mask_index = dedupe.emplace(stack_map.register_mask, index).first->second; |
| 569 | register_masks_[index] = stack_map.register_mask; |
| 570 | } |
| 571 | return dedupe.size(); |
| 572 | } |
| 573 | |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 574 | size_t StackMapStream::PrepareStackMasks(size_t entry_size_in_bits) { |
| 575 | // Preallocate memory since we do not want it to move (the dedup map will point into it). |
| 576 | const size_t byte_entry_size = RoundUp(entry_size_in_bits, kBitsPerByte) / kBitsPerByte; |
| 577 | stack_masks_.resize(byte_entry_size * stack_maps_.size(), 0u); |
| 578 | // For deduplicating we store the stack masks as byte packed for simplicity. We can bit pack later |
| 579 | // when copying out from stack_masks_. |
| 580 | std::unordered_map<MemoryRegion, |
| 581 | size_t, |
| 582 | FNVHash<MemoryRegion>, |
| 583 | MemoryRegion::ContentEquals> dedup(stack_maps_.size()); |
| 584 | for (StackMapEntry& stack_map : stack_maps_) { |
| 585 | size_t index = dedup.size(); |
| 586 | MemoryRegion stack_mask(stack_masks_.data() + index * byte_entry_size, byte_entry_size); |
| 587 | for (size_t i = 0; i < entry_size_in_bits; i++) { |
| 588 | stack_mask.StoreBit(i, stack_map.sp_mask != nullptr && stack_map.sp_mask->IsBitSet(i)); |
| 589 | } |
| 590 | stack_map.stack_mask_index = dedup.emplace(stack_mask, index).first->second; |
| 591 | } |
| 592 | return dedup.size(); |
| 593 | } |
| 594 | |
David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 595 | // Check that all StackMapStream inputs are correctly encoded by trying to read them back. |
| 596 | void StackMapStream::CheckCodeInfo(MemoryRegion region) const { |
| 597 | CodeInfo code_info(region); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 598 | CodeInfoEncoding encoding = code_info.ExtractEncoding(); |
| 599 | DCHECK_EQ(code_info.GetNumberOfStackMaps(encoding), stack_maps_.size()); |
David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 600 | for (size_t s = 0; s < stack_maps_.size(); ++s) { |
| 601 | const StackMap stack_map = code_info.GetStackMapAt(s, encoding); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 602 | const StackMapEncoding& stack_map_encoding = encoding.stack_map_encoding; |
David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 603 | StackMapEntry entry = stack_maps_[s]; |
| 604 | |
| 605 | // Check main stack map fields. |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 606 | DCHECK_EQ(stack_map.GetNativePcOffset(stack_map_encoding, instruction_set_), |
| 607 | entry.native_pc_code_offset.Uint32Value(instruction_set_)); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 608 | DCHECK_EQ(stack_map.GetDexPc(stack_map_encoding), entry.dex_pc); |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 609 | DCHECK_EQ(stack_map.GetRegisterMaskIndex(stack_map_encoding), entry.register_mask_index); |
| 610 | DCHECK_EQ(code_info.GetRegisterMaskOf(encoding, stack_map), entry.register_mask); |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 611 | const size_t num_stack_mask_bits = code_info.GetNumberOfStackMaskBits(encoding); |
| 612 | DCHECK_EQ(stack_map.GetStackMaskIndex(stack_map_encoding), entry.stack_mask_index); |
| 613 | BitMemoryRegion stack_mask = code_info.GetStackMaskOf(encoding, stack_map); |
David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 614 | if (entry.sp_mask != nullptr) { |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 615 | DCHECK_GE(stack_mask.size_in_bits(), entry.sp_mask->GetNumberOfBits()); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 616 | for (size_t b = 0; b < num_stack_mask_bits; b++) { |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 617 | DCHECK_EQ(stack_mask.LoadBit(b), entry.sp_mask->IsBitSet(b)); |
David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 618 | } |
| 619 | } else { |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 620 | for (size_t b = 0; b < num_stack_mask_bits; b++) { |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 621 | DCHECK_EQ(stack_mask.LoadBit(b), 0u); |
David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
| 625 | CheckDexRegisterMap(code_info, |
| 626 | code_info.GetDexRegisterMapOf( |
| 627 | stack_map, encoding, entry.num_dex_registers), |
| 628 | entry.num_dex_registers, |
| 629 | entry.live_dex_registers_mask, |
| 630 | entry.dex_register_locations_start_index); |
| 631 | |
| 632 | // Check inline info. |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 633 | DCHECK_EQ(stack_map.HasInlineInfo(stack_map_encoding), (entry.inlining_depth != 0)); |
David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 634 | if (entry.inlining_depth != 0) { |
| 635 | InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding); |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 636 | DCHECK_EQ(inline_info.GetDepth(encoding.inline_info_encoding), entry.inlining_depth); |
David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 637 | for (size_t d = 0; d < entry.inlining_depth; ++d) { |
| 638 | size_t inline_info_index = entry.inline_infos_start_index + d; |
| 639 | DCHECK_LT(inline_info_index, inline_infos_.size()); |
| 640 | InlineInfoEntry inline_entry = inline_infos_[inline_info_index]; |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 641 | DCHECK_EQ(inline_info.GetDexPcAtDepth(encoding.inline_info_encoding, d), |
| 642 | inline_entry.dex_pc); |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 643 | if (inline_info.EncodesArtMethodAtDepth(encoding.inline_info_encoding, d)) { |
| 644 | DCHECK_EQ(inline_info.GetArtMethodAtDepth(encoding.inline_info_encoding, d), |
| 645 | inline_entry.method); |
| 646 | } else { |
| 647 | DCHECK_EQ(inline_info.GetMethodIndexAtDepth(encoding.inline_info_encoding, d), |
| 648 | inline_entry.method_index); |
| 649 | } |
David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 650 | |
| 651 | CheckDexRegisterMap(code_info, |
| 652 | code_info.GetDexRegisterMapAtDepth( |
| 653 | d, inline_info, encoding, inline_entry.num_dex_registers), |
| 654 | inline_entry.num_dex_registers, |
| 655 | inline_entry.live_dex_registers_mask, |
| 656 | inline_entry.dex_register_locations_start_index); |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 662 | } // namespace art |