Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_ |
| 18 | #define ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_ |
| 19 | |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 20 | #include "base/arena_containers.h" |
| 21 | #include "base/bit_vector-inl.h" |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 22 | #include "base/value_object.h" |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 23 | #include "memory_region.h" |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 24 | #include "nodes.h" |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 25 | #include "stack_map.h" |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 26 | #include "utils/growable_array.h" |
| 27 | |
| 28 | namespace art { |
| 29 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 30 | // Helper to build art::StackMapStream::LocationCatalogEntriesIndices. |
| 31 | class LocationCatalogEntriesIndicesEmptyFn { |
| 32 | public: |
| 33 | void MakeEmpty(std::pair<DexRegisterLocation, size_t>& item) const { |
| 34 | item.first = DexRegisterLocation::None(); |
| 35 | } |
| 36 | bool IsEmpty(const std::pair<DexRegisterLocation, size_t>& item) const { |
| 37 | return item.first == DexRegisterLocation::None(); |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | // Hash function for art::StackMapStream::LocationCatalogEntriesIndices. |
| 42 | // This hash function does not create collisions. |
| 43 | class DexRegisterLocationHashFn { |
| 44 | public: |
| 45 | size_t operator()(DexRegisterLocation key) const { |
| 46 | // Concatenate `key`s fields to create a 64-bit value to be hashed. |
| 47 | int64_t kind_and_value = |
| 48 | (static_cast<int64_t>(key.kind_) << 32) | static_cast<int64_t>(key.value_); |
| 49 | return inner_hash_fn_(kind_and_value); |
| 50 | } |
| 51 | private: |
| 52 | std::hash<int64_t> inner_hash_fn_; |
| 53 | }; |
| 54 | |
| 55 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 56 | /** |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 57 | * Collects and builds stack maps for a method. All the stack maps |
| 58 | * for a method are placed in a CodeInfo object. |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 59 | */ |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 60 | class StackMapStream : public ValueObject { |
| 61 | public: |
| 62 | explicit StackMapStream(ArenaAllocator* allocator) |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 63 | : allocator_(allocator), |
| 64 | stack_maps_(allocator, 10), |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 65 | location_catalog_entries_(allocator, 4), |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 66 | dex_register_locations_(allocator, 10 * 4), |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 67 | inline_infos_(allocator, 2), |
Andreas Gampe | 8eddd2a | 2014-07-28 14:53:22 -0700 | [diff] [blame] | 68 | stack_mask_max_(-1), |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 69 | dex_pc_max_(0), |
| 70 | native_pc_offset_max_(0), |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 71 | register_mask_max_(0), |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 72 | number_of_stack_maps_with_inline_info_(0), |
| 73 | dex_map_hash_to_stack_map_indices_(std::less<uint32_t>(), allocator->Adapter()) {} |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 74 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 75 | // See runtime/stack_map.h to know what these fields contain. |
| 76 | struct StackMapEntry { |
| 77 | uint32_t dex_pc; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 78 | uint32_t native_pc_offset; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 79 | uint32_t register_mask; |
| 80 | BitVector* sp_mask; |
| 81 | uint32_t num_dex_registers; |
| 82 | uint8_t inlining_depth; |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 83 | size_t dex_register_locations_start_index; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 84 | size_t inline_infos_start_index; |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 85 | BitVector* live_dex_registers_mask; |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 86 | uint32_t dex_register_map_hash; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 87 | }; |
| 88 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 89 | struct InlineInfoEntry { |
| 90 | uint32_t method_index; |
| 91 | }; |
| 92 | |
| 93 | void AddStackMapEntry(uint32_t dex_pc, |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 94 | uint32_t native_pc_offset, |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 95 | uint32_t register_mask, |
| 96 | BitVector* sp_mask, |
| 97 | uint32_t num_dex_registers, |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame^] | 98 | uint8_t inlining_depth); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 99 | |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame^] | 100 | void AddDexRegisterEntry(uint16_t dex_register, |
| 101 | DexRegisterLocation::Kind kind, |
| 102 | int32_t value); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 103 | |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame^] | 104 | void AddInlineInfoEntry(uint32_t method_index); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 105 | |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame^] | 106 | size_t ComputeNeededSize(); |
| 107 | size_t ComputeStackMaskSize() const; |
| 108 | size_t ComputeStackMapsSize(); |
| 109 | size_t ComputeDexRegisterLocationCatalogSize() const; |
| 110 | size_t ComputeDexRegisterMapSize(const StackMapEntry& entry) const; |
| 111 | size_t ComputeDexRegisterMapsSize(); |
| 112 | size_t ComputeInlineInfoSize() const; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 113 | |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame^] | 114 | size_t ComputeDexRegisterLocationCatalogStart() const; |
| 115 | size_t ComputeStackMapsStart() const; |
| 116 | size_t ComputeDexRegisterMapsStart(); |
| 117 | size_t ComputeInlineInfoStart(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 118 | |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame^] | 119 | void FillIn(MemoryRegion region); |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 120 | |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 121 | private: |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 122 | // Returns the index of an entry with the same dex register map |
| 123 | // or kNoSameDexMapFound if no such entry exists. |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame^] | 124 | size_t FindEntryWithTheSameDexMap(size_t entry_index); |
| 125 | bool HaveTheSameDexMaps(const StackMapEntry& a, const StackMapEntry& b) const; |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 126 | |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 127 | ArenaAllocator* allocator_; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 128 | GrowableArray<StackMapEntry> stack_maps_; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 129 | |
| 130 | // A catalog of unique [location_kind, register_value] pairs (per method). |
| 131 | GrowableArray<DexRegisterLocation> location_catalog_entries_; |
| 132 | // Map from Dex register location catalog entries to their indices in the |
| 133 | // location catalog. |
| 134 | typedef HashMap<DexRegisterLocation, size_t, LocationCatalogEntriesIndicesEmptyFn, |
| 135 | DexRegisterLocationHashFn> LocationCatalogEntriesIndices; |
| 136 | LocationCatalogEntriesIndices location_catalog_entries_indices_; |
| 137 | |
Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame^] | 138 | // A set of concatenated maps of Dex register locations indices to `location_catalog_entries_`. |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 139 | GrowableArray<size_t> dex_register_locations_; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 140 | GrowableArray<InlineInfoEntry> inline_infos_; |
| 141 | int stack_mask_max_; |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 142 | uint32_t dex_pc_max_; |
| 143 | uint32_t native_pc_offset_max_; |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 144 | uint32_t register_mask_max_; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 145 | size_t number_of_stack_maps_with_inline_info_; |
| 146 | |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 147 | ArenaSafeMap<uint32_t, GrowableArray<uint32_t>> dex_map_hash_to_stack_map_indices_; |
| 148 | |
| 149 | static constexpr uint32_t kNoSameDexMapFound = -1; |
| 150 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 151 | DISALLOW_COPY_AND_ASSIGN(StackMapStream); |
| 152 | }; |
| 153 | |
| 154 | } // namespace art |
| 155 | |
| 156 | #endif // ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_ |