blob: 268e9bd6e0cbbed19190665b2ad1a3ea0f56abd8 [file] [log] [blame]
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001/*
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 Juravle6ae70962015-03-18 16:31:28 +000020#include "base/bit_vector-inl.h"
Andreas Gampe2a5c4682015-08-14 08:22:54 -070021#include "base/hash_map.h"
David Sehr1ce2b3b2018-04-05 11:02:03 -070022#include "base/memory_region.h"
Vladimir Marko174b2e22017-10-12 13:34:49 +010023#include "base/scoped_arena_containers.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070024#include "base/value_object.h"
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070025#include "method_info.h"
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000026#include "nodes.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010027#include "stack_map.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010028
29namespace art {
30
Roland Levillaina552e1c2015-03-26 15:01:03 +000031// Helper to build art::StackMapStream::LocationCatalogEntriesIndices.
32class LocationCatalogEntriesIndicesEmptyFn {
33 public:
34 void MakeEmpty(std::pair<DexRegisterLocation, size_t>& item) const {
35 item.first = DexRegisterLocation::None();
36 }
37 bool IsEmpty(const std::pair<DexRegisterLocation, size_t>& item) const {
38 return item.first == DexRegisterLocation::None();
39 }
40};
41
42// Hash function for art::StackMapStream::LocationCatalogEntriesIndices.
43// This hash function does not create collisions.
44class DexRegisterLocationHashFn {
45 public:
46 size_t operator()(DexRegisterLocation key) const {
47 // Concatenate `key`s fields to create a 64-bit value to be hashed.
48 int64_t kind_and_value =
49 (static_cast<int64_t>(key.kind_) << 32) | static_cast<int64_t>(key.value_);
50 return inner_hash_fn_(kind_and_value);
51 }
52 private:
53 std::hash<int64_t> inner_hash_fn_;
54};
55
56
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010057/**
Nicolas Geoffray39468442014-09-02 15:17:15 +010058 * Collects and builds stack maps for a method. All the stack maps
59 * for a method are placed in a CodeInfo object.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010060 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010061class StackMapStream : public ValueObject {
62 public:
Vladimir Marko174b2e22017-10-12 13:34:49 +010063 explicit StackMapStream(ScopedArenaAllocator* allocator, InstructionSet instruction_set)
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000064 : allocator_(allocator),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -080065 instruction_set_(instruction_set),
Vladimir Marko225b6462015-09-28 12:17:40 +010066 stack_maps_(allocator->Adapter(kArenaAllocStackMapStream)),
67 location_catalog_entries_(allocator->Adapter(kArenaAllocStackMapStream)),
Vladimir Marko1f497642015-10-05 20:34:42 +010068 location_catalog_entries_indices_(allocator->Adapter(kArenaAllocStackMapStream)),
Vladimir Marko225b6462015-09-28 12:17:40 +010069 dex_register_locations_(allocator->Adapter(kArenaAllocStackMapStream)),
70 inline_infos_(allocator->Adapter(kArenaAllocStackMapStream)),
David Srbecky45aa5982016-03-18 02:15:09 +000071 stack_masks_(allocator->Adapter(kArenaAllocStackMapStream)),
Mathieu Chartier1a20b682017-01-31 14:25:16 -080072 register_masks_(allocator->Adapter(kArenaAllocStackMapStream)),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070073 method_indices_(allocator->Adapter(kArenaAllocStackMapStream)),
Mathieu Chartier32289082017-02-09 15:57:37 -080074 dex_register_entries_(allocator->Adapter(kArenaAllocStackMapStream)),
Andreas Gampe8eddd2a2014-07-28 14:53:22 -070075 stack_mask_max_(-1),
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000076 dex_pc_max_(kNoDexPc),
Nicolas Geoffray896f8f72015-03-30 15:44:25 +010077 register_mask_max_(0),
Calin Juravle6ae70962015-03-18 16:31:28 +000078 number_of_stack_maps_with_inline_info_(0),
Vladimir Marko225b6462015-09-28 12:17:40 +010079 dex_map_hash_to_stack_map_indices_(std::less<uint32_t>(),
80 allocator->Adapter(kArenaAllocStackMapStream)),
Calin Juravle4f46ac52015-04-23 18:47:21 +010081 current_entry_(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010082 current_inline_info_(),
David Srbecky09ed0982016-02-12 21:58:43 +000083 code_info_encoding_(allocator->Adapter(kArenaAllocStackMapStream)),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010084 needed_size_(0),
85 current_dex_register_(0),
Vladimir Marko225b6462015-09-28 12:17:40 +010086 in_inline_frame_(false) {
87 stack_maps_.reserve(10);
88 location_catalog_entries_.reserve(4);
89 dex_register_locations_.reserve(10 * 4);
90 inline_infos_.reserve(2);
David Srbecky09ed0982016-02-12 21:58:43 +000091 code_info_encoding_.reserve(16);
Vladimir Marko225b6462015-09-28 12:17:40 +010092 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010093
Mathieu Chartier32289082017-02-09 15:57:37 -080094 // A dex register map entry for a single stack map entry, contains what registers are live as
95 // well as indices into the location catalog.
96 class DexRegisterMapEntry {
97 public:
98 static const size_t kOffsetUnassigned = -1;
99
100 BitVector* live_dex_registers_mask;
101 uint32_t num_dex_registers;
102 size_t locations_start_index;
103 // Computed fields
104 size_t hash = 0;
105 size_t offset = kOffsetUnassigned;
106
107 size_t ComputeSize(size_t catalog_size) const;
108 };
109
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100110 // See runtime/stack_map.h to know what these fields contain.
111 struct StackMapEntry {
112 uint32_t dex_pc;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800113 CodeOffset native_pc_code_offset;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100114 uint32_t register_mask;
115 BitVector* sp_mask;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100116 uint8_t inlining_depth;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100117 size_t inline_infos_start_index;
David Srbecky45aa5982016-03-18 02:15:09 +0000118 uint32_t stack_mask_index;
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800119 uint32_t register_mask_index;
Mathieu Chartier32289082017-02-09 15:57:37 -0800120 DexRegisterMapEntry dex_register_entry;
121 size_t dex_register_map_index;
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800122 InvokeType invoke_type;
123 uint32_t dex_method_index;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700124 uint32_t dex_method_index_idx; // Index into dex method index table.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100125 };
126
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100127 struct InlineInfoEntry {
Andreas Gampee2abbc62017-09-15 11:59:26 -0700128 uint32_t dex_pc; // dex::kDexNoIndex for intrinsified native methods.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000129 ArtMethod* method;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100130 uint32_t method_index;
Mathieu Chartier32289082017-02-09 15:57:37 -0800131 DexRegisterMapEntry dex_register_entry;
132 size_t dex_register_map_index;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700133 uint32_t dex_method_index_idx; // Index into the dex method index table.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100134 };
135
Calin Juravle4f46ac52015-04-23 18:47:21 +0100136 void BeginStackMapEntry(uint32_t dex_pc,
137 uint32_t native_pc_offset,
138 uint32_t register_mask,
139 BitVector* sp_mask,
140 uint32_t num_dex_registers,
141 uint8_t inlining_depth);
142 void EndStackMapEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100143
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100144 void AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000145
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800146 void AddInvoke(InvokeType type, uint32_t dex_method_index);
147
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000148 void BeginInlineInfoEntry(ArtMethod* method,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100149 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000150 uint32_t num_dex_registers,
151 const DexFile* outer_dex_file = nullptr);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100152 void EndInlineInfoEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100153
Vladimir Markobd8c7252015-06-12 10:06:32 +0100154 size_t GetNumberOfStackMaps() const {
Vladimir Marko225b6462015-09-28 12:17:40 +0100155 return stack_maps_.size();
Vladimir Markobd8c7252015-06-12 10:06:32 +0100156 }
157
158 const StackMapEntry& GetStackMap(size_t i) const {
Vladimir Marko225b6462015-09-28 12:17:40 +0100159 return stack_maps_[i];
Vladimir Markobd8c7252015-06-12 10:06:32 +0100160 }
161
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000162 void SetStackMapNativePcOffset(size_t i, uint32_t native_pc_offset) {
Mathieu Chartier32289082017-02-09 15:57:37 -0800163 stack_maps_[i].native_pc_code_offset =
164 CodeOffset::FromOffset(native_pc_offset, instruction_set_);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000165 }
166
Calin Juravle4f46ac52015-04-23 18:47:21 +0100167 // Prepares the stream to fill in a memory region. Must be called before FillIn.
168 // Returns the size (in bytes) needed to store this stream.
169 size_t PrepareForFillIn();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700170 void FillInCodeInfo(MemoryRegion region);
171 void FillInMethodInfo(MemoryRegion region);
172
173 size_t ComputeMethodInfoSize() const;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000174
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000175 private:
Calin Juravle4f46ac52015-04-23 18:47:21 +0100176 size_t ComputeDexRegisterLocationCatalogSize() const;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100177 size_t ComputeDexRegisterMapsSize() const;
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800178 void ComputeInlineInfoEncoding(InlineInfoEncoding* encoding,
179 size_t dex_register_maps_bytes);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100180
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800181 CodeOffset ComputeMaxNativePcCodeOffset() const;
182
David Srbecky45aa5982016-03-18 02:15:09 +0000183 // Returns the number of unique stack masks.
184 size_t PrepareStackMasks(size_t entry_size_in_bits);
185
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800186 // Returns the number of unique register masks.
187 size_t PrepareRegisterMasks();
188
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700189 // Prepare and deduplicate method indices.
190 void PrepareMethodIndices();
191
Mathieu Chartier32289082017-02-09 15:57:37 -0800192 // Deduplicate entry if possible and return the corresponding index into dex_register_entries_
193 // array. If entry is not a duplicate, a new entry is added to dex_register_entries_.
194 size_t AddDexRegisterMapEntry(const DexRegisterMapEntry& entry);
195
196 // Return true if the two dex register map entries are equal.
197 bool DexRegisterMapEntryEquals(const DexRegisterMapEntry& a, const DexRegisterMapEntry& b) const;
198
199 // Fill in the corresponding entries of a register map.
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800200 void ComputeInvokeInfoEncoding(CodeInfoEncoding* encoding);
201
202 // Returns the index of an entry with the same dex register map as the current_entry,
203 // or kNoSameDexMapFound if no such entry exists.
204 size_t FindEntryWithTheSameDexMap();
205 bool HaveTheSameDexMaps(const StackMapEntry& a, const StackMapEntry& b) const;
206
207 // Fill in the corresponding entries of a register map.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100208 void FillInDexRegisterMap(DexRegisterMap dex_register_map,
209 uint32_t num_dex_registers,
210 const BitVector& live_dex_registers_mask,
211 uint32_t start_index_in_dex_register_locations) const;
Calin Juravle6ae70962015-03-18 16:31:28 +0000212
Mathieu Chartier32289082017-02-09 15:57:37 -0800213 // Returns the offset for the dex register inside of the dex register location region. See FillIn.
214 // Only copies the dex register map if the offset for the entry is not already assigned.
215 size_t MaybeCopyDexRegisterMap(DexRegisterMapEntry& entry,
216 size_t* current_offset,
217 MemoryRegion dex_register_locations_region);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000218 void CheckDexRegisterMap(const CodeInfo& code_info,
219 const DexRegisterMap& dex_register_map,
220 size_t num_dex_registers,
221 BitVector* live_dex_registers_mask,
222 size_t dex_register_locations_index) const;
223 void CheckCodeInfo(MemoryRegion region) const;
224
Vladimir Marko174b2e22017-10-12 13:34:49 +0100225 ScopedArenaAllocator* const allocator_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800226 const InstructionSet instruction_set_;
Vladimir Marko174b2e22017-10-12 13:34:49 +0100227 ScopedArenaVector<StackMapEntry> stack_maps_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000228
229 // A catalog of unique [location_kind, register_value] pairs (per method).
Vladimir Marko174b2e22017-10-12 13:34:49 +0100230 ScopedArenaVector<DexRegisterLocation> location_catalog_entries_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000231 // Map from Dex register location catalog entries to their indices in the
232 // location catalog.
Vladimir Marko174b2e22017-10-12 13:34:49 +0100233 using LocationCatalogEntriesIndices = ScopedArenaHashMap<DexRegisterLocation,
234 size_t,
235 LocationCatalogEntriesIndicesEmptyFn,
236 DexRegisterLocationHashFn>;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000237 LocationCatalogEntriesIndices location_catalog_entries_indices_;
238
Calin Juravlec416d332015-04-23 16:01:43 +0100239 // A set of concatenated maps of Dex register locations indices to `location_catalog_entries_`.
Vladimir Marko174b2e22017-10-12 13:34:49 +0100240 ScopedArenaVector<size_t> dex_register_locations_;
241 ScopedArenaVector<InlineInfoEntry> inline_infos_;
242 ScopedArenaVector<uint8_t> stack_masks_;
243 ScopedArenaVector<uint32_t> register_masks_;
244 ScopedArenaVector<uint32_t> method_indices_;
245 ScopedArenaVector<DexRegisterMapEntry> dex_register_entries_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100246 int stack_mask_max_;
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000247 uint32_t dex_pc_max_;
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100248 uint32_t register_mask_max_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100249 size_t number_of_stack_maps_with_inline_info_;
250
Vladimir Marko174b2e22017-10-12 13:34:49 +0100251 ScopedArenaSafeMap<uint32_t, ScopedArenaVector<uint32_t>> dex_map_hash_to_stack_map_indices_;
Calin Juravle6ae70962015-03-18 16:31:28 +0000252
Calin Juravle4f46ac52015-04-23 18:47:21 +0100253 StackMapEntry current_entry_;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100254 InlineInfoEntry current_inline_info_;
Vladimir Marko174b2e22017-10-12 13:34:49 +0100255 ScopedArenaVector<uint8_t> code_info_encoding_;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100256 size_t needed_size_;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100257 uint32_t current_dex_register_;
258 bool in_inline_frame_;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100259
Calin Juravle6ae70962015-03-18 16:31:28 +0000260 static constexpr uint32_t kNoSameDexMapFound = -1;
261
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100262 DISALLOW_COPY_AND_ASSIGN(StackMapStream);
263};
264
265} // namespace art
266
267#endif // ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_