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 | |
| 20 | #include "base/bit_vector.h" |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 21 | #include "base/value_object.h" |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 22 | #include "memory_region.h" |
| 23 | #include "stack_map.h" |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 24 | #include "utils/growable_array.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | /** |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 29 | * Collects and builds stack maps for a method. All the stack maps |
| 30 | * for a method are placed in a CodeInfo object. |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 31 | */ |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 32 | class StackMapStream : public ValueObject { |
| 33 | public: |
| 34 | explicit StackMapStream(ArenaAllocator* allocator) |
| 35 | : stack_maps_(allocator, 10), |
| 36 | dex_register_maps_(allocator, 10 * 4), |
| 37 | inline_infos_(allocator, 2), |
Andreas Gampe | 8eddd2a | 2014-07-28 14:53:22 -0700 | [diff] [blame] | 38 | stack_mask_max_(-1), |
| 39 | number_of_stack_maps_with_inline_info_(0) {} |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 40 | |
| 41 | // Compute bytes needed to encode a mask with the given maximum element. |
| 42 | static uint32_t StackMaskEncodingSize(int max_element) { |
| 43 | int number_of_bits = max_element + 1; // Need room for max element too. |
| 44 | return RoundUp(number_of_bits, kBitsPerByte) / kBitsPerByte; |
| 45 | } |
| 46 | |
| 47 | // See runtime/stack_map.h to know what these fields contain. |
| 48 | struct StackMapEntry { |
| 49 | uint32_t dex_pc; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 50 | uint32_t native_pc_offset; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 51 | uint32_t register_mask; |
| 52 | BitVector* sp_mask; |
| 53 | uint32_t num_dex_registers; |
| 54 | uint8_t inlining_depth; |
| 55 | size_t dex_register_maps_start_index; |
| 56 | size_t inline_infos_start_index; |
| 57 | }; |
| 58 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 59 | struct InlineInfoEntry { |
| 60 | uint32_t method_index; |
| 61 | }; |
| 62 | |
| 63 | void AddStackMapEntry(uint32_t dex_pc, |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 64 | uint32_t native_pc_offset, |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 65 | uint32_t register_mask, |
| 66 | BitVector* sp_mask, |
| 67 | uint32_t num_dex_registers, |
| 68 | uint8_t inlining_depth) { |
| 69 | StackMapEntry entry; |
| 70 | entry.dex_pc = dex_pc; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 71 | entry.native_pc_offset = native_pc_offset; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 72 | entry.register_mask = register_mask; |
| 73 | entry.sp_mask = sp_mask; |
| 74 | entry.num_dex_registers = num_dex_registers; |
| 75 | entry.inlining_depth = inlining_depth; |
| 76 | entry.dex_register_maps_start_index = dex_register_maps_.Size(); |
| 77 | entry.inline_infos_start_index = inline_infos_.Size(); |
| 78 | stack_maps_.Add(entry); |
| 79 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 80 | if (sp_mask != nullptr) { |
| 81 | stack_mask_max_ = std::max(stack_mask_max_, sp_mask->GetHighestBitSet()); |
| 82 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 83 | if (inlining_depth > 0) { |
| 84 | number_of_stack_maps_with_inline_info_++; |
| 85 | } |
| 86 | } |
| 87 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 88 | void AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value) { |
| 89 | // Ensure we only use non-compressed location kind at this stage. |
| 90 | DCHECK(DexRegisterLocation::IsShortLocationKind(kind)) |
| 91 | << DexRegisterLocation::PrettyDescriptor(kind); |
| 92 | dex_register_maps_.Add(DexRegisterLocation(kind, value)); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void AddInlineInfoEntry(uint32_t method_index) { |
| 96 | InlineInfoEntry entry; |
| 97 | entry.method_index = method_index; |
| 98 | inline_infos_.Add(entry); |
| 99 | } |
| 100 | |
| 101 | size_t ComputeNeededSize() const { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 102 | return CodeInfo::kFixedSize |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 103 | + ComputeStackMapSize() |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 104 | + ComputeDexRegisterMapsSize() |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 105 | + ComputeInlineInfoSize(); |
| 106 | } |
| 107 | |
| 108 | size_t ComputeStackMapSize() const { |
Nicolas Geoffray | 376b2bb | 2014-12-09 14:26:32 +0000 | [diff] [blame] | 109 | return stack_maps_.Size() * StackMap::ComputeAlignedStackMapSize(stack_mask_max_); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 112 | // Compute the size of the Dex register map of `entry`. |
| 113 | size_t ComputeDexRegisterMapSize(const StackMapEntry& entry) const { |
| 114 | size_t size = DexRegisterMap::kFixedSize; |
| 115 | for (size_t j = 0; j < entry.num_dex_registers; ++j) { |
| 116 | DexRegisterLocation dex_register_location = |
| 117 | dex_register_maps_.Get(entry.dex_register_maps_start_index + j); |
| 118 | size += DexRegisterMap::EntrySize(dex_register_location); |
| 119 | } |
| 120 | return size; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 123 | // Compute the size of all the Dex register maps. |
| 124 | size_t ComputeDexRegisterMapsSize() const { |
| 125 | size_t size = stack_maps_.Size() * DexRegisterMap::kFixedSize; |
| 126 | // The size of each register location depends on the type of |
| 127 | // the entry. |
| 128 | for (size_t i = 0, e = dex_register_maps_.Size(); i < e; ++i) { |
| 129 | DexRegisterLocation entry = dex_register_maps_.Get(i); |
| 130 | size += DexRegisterMap::EntrySize(entry); |
| 131 | } |
| 132 | return size; |
| 133 | } |
| 134 | |
| 135 | // Compute the size of all the inline information pieces. |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 136 | size_t ComputeInlineInfoSize() const { |
| 137 | return inline_infos_.Size() * InlineInfo::SingleEntrySize() |
| 138 | // For encoding the depth. |
| 139 | + (number_of_stack_maps_with_inline_info_ * InlineInfo::kFixedSize); |
| 140 | } |
| 141 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 142 | size_t ComputeDexRegisterMapStart() const { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 143 | return CodeInfo::kFixedSize + ComputeStackMapSize(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 146 | size_t ComputeInlineInfoStart() const { |
| 147 | return ComputeDexRegisterMapStart() + ComputeDexRegisterMapsSize(); |
| 148 | } |
| 149 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 150 | void FillIn(MemoryRegion region) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 151 | CodeInfo code_info(region); |
| 152 | code_info.SetOverallSize(region.size()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 153 | |
| 154 | size_t stack_mask_size = StackMaskEncodingSize(stack_mask_max_); |
| 155 | uint8_t* memory_start = region.start(); |
| 156 | |
| 157 | MemoryRegion dex_register_maps_region = region.Subregion( |
| 158 | ComputeDexRegisterMapStart(), |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 159 | ComputeDexRegisterMapsSize()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 160 | |
| 161 | MemoryRegion inline_infos_region = region.Subregion( |
| 162 | ComputeInlineInfoStart(), |
| 163 | ComputeInlineInfoSize()); |
| 164 | |
| 165 | code_info.SetNumberOfStackMaps(stack_maps_.Size()); |
| 166 | code_info.SetStackMaskSize(stack_mask_size); |
| 167 | |
| 168 | uintptr_t next_dex_register_map_offset = 0; |
| 169 | uintptr_t next_inline_info_offset = 0; |
| 170 | for (size_t i = 0, e = stack_maps_.Size(); i < e; ++i) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 171 | StackMap stack_map = code_info.GetStackMapAt(i); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 172 | StackMapEntry entry = stack_maps_.Get(i); |
| 173 | |
| 174 | stack_map.SetDexPc(entry.dex_pc); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 175 | stack_map.SetNativePcOffset(entry.native_pc_offset); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 176 | stack_map.SetRegisterMask(entry.register_mask); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 177 | if (entry.sp_mask != nullptr) { |
| 178 | stack_map.SetStackMask(*entry.sp_mask); |
| 179 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 180 | |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 181 | if (entry.num_dex_registers != 0) { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 182 | // Set the Dex register map. |
| 183 | MemoryRegion register_region = |
| 184 | dex_register_maps_region.Subregion( |
| 185 | next_dex_register_map_offset, |
| 186 | ComputeDexRegisterMapSize(entry)); |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 187 | next_dex_register_map_offset += register_region.size(); |
| 188 | DexRegisterMap dex_register_map(register_region); |
| 189 | stack_map.SetDexRegisterMapOffset(register_region.start() - memory_start); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 190 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 191 | // Offset in `dex_register_map` where to store the next register entry. |
| 192 | size_t offset = DexRegisterMap::kFixedSize; |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 193 | for (size_t j = 0; j < entry.num_dex_registers; ++j) { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 194 | DexRegisterLocation dex_register_location = |
| 195 | dex_register_maps_.Get(entry.dex_register_maps_start_index + j); |
| 196 | dex_register_map.SetRegisterInfo(offset, dex_register_location); |
| 197 | offset += DexRegisterMap::EntrySize(dex_register_location); |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 198 | } |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 199 | // Ensure we reached the end of the Dex registers region. |
| 200 | DCHECK_EQ(offset, register_region.size()); |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 201 | } else { |
| 202 | stack_map.SetDexRegisterMapOffset(StackMap::kNoDexRegisterMap); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | // Set the inlining info. |
| 206 | if (entry.inlining_depth != 0) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 207 | MemoryRegion inline_region = inline_infos_region.Subregion( |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 208 | next_inline_info_offset, |
| 209 | InlineInfo::kFixedSize + entry.inlining_depth * InlineInfo::SingleEntrySize()); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 210 | next_inline_info_offset += inline_region.size(); |
| 211 | InlineInfo inline_info(inline_region); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 212 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 213 | stack_map.SetInlineDescriptorOffset(inline_region.start() - memory_start); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 214 | |
| 215 | inline_info.SetDepth(entry.inlining_depth); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 216 | for (size_t j = 0; j < entry.inlining_depth; ++j) { |
| 217 | InlineInfoEntry inline_entry = inline_infos_.Get(j + entry.inline_infos_start_index); |
| 218 | inline_info.SetMethodReferenceIndexAtDepth(j, inline_entry.method_index); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 219 | } |
| 220 | } else { |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 221 | stack_map.SetInlineDescriptorOffset(StackMap::kNoInlineInfo); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | private: |
| 227 | GrowableArray<StackMapEntry> stack_maps_; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 228 | GrowableArray<DexRegisterLocation> dex_register_maps_; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 229 | GrowableArray<InlineInfoEntry> inline_infos_; |
| 230 | int stack_mask_max_; |
| 231 | size_t number_of_stack_maps_with_inline_info_; |
| 232 | |
| 233 | DISALLOW_COPY_AND_ASSIGN(StackMapStream); |
| 234 | }; |
| 235 | |
| 236 | } // namespace art |
| 237 | |
| 238 | #endif // ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_ |