blob: ea97cf65306a5299ad29c15ebb0daff71aa32946 [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),
David Srbecky052f8ca2018-04-26 15:42:54 +010076 out_(allocator->Adapter(kArenaAllocStackMapStream)),
Vladimir Marko225b6462015-09-28 12:17:40 +010077 dex_map_hash_to_stack_map_indices_(std::less<uint32_t>(),
78 allocator->Adapter(kArenaAllocStackMapStream)),
Calin Juravle4f46ac52015-04-23 18:47:21 +010079 current_entry_(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010080 current_inline_info_(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010081 current_dex_register_(0),
Vladimir Marko225b6462015-09-28 12:17:40 +010082 in_inline_frame_(false) {
83 stack_maps_.reserve(10);
David Srbecky052f8ca2018-04-26 15:42:54 +010084 out_.reserve(64);
Vladimir Marko225b6462015-09-28 12:17:40 +010085 location_catalog_entries_.reserve(4);
86 dex_register_locations_.reserve(10 * 4);
87 inline_infos_.reserve(2);
88 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010089
Mathieu Chartier32289082017-02-09 15:57:37 -080090 // A dex register map entry for a single stack map entry, contains what registers are live as
91 // well as indices into the location catalog.
92 class DexRegisterMapEntry {
93 public:
David Srbecky052f8ca2018-04-26 15:42:54 +010094 static const uint32_t kOffsetUnassigned = -1;
Mathieu Chartier32289082017-02-09 15:57:37 -080095
96 BitVector* live_dex_registers_mask;
97 uint32_t num_dex_registers;
98 size_t locations_start_index;
99 // Computed fields
100 size_t hash = 0;
David Srbecky052f8ca2018-04-26 15:42:54 +0100101 uint32_t offset = kOffsetUnassigned;
Mathieu Chartier32289082017-02-09 15:57:37 -0800102
103 size_t ComputeSize(size_t catalog_size) const;
104 };
105
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100106 // See runtime/stack_map.h to know what these fields contain.
107 struct StackMapEntry {
108 uint32_t dex_pc;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800109 CodeOffset native_pc_code_offset;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100110 uint32_t register_mask;
111 BitVector* sp_mask;
David Srbecky052f8ca2018-04-26 15:42:54 +0100112 uint32_t inlining_depth;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100113 size_t inline_infos_start_index;
David Srbecky45aa5982016-03-18 02:15:09 +0000114 uint32_t stack_mask_index;
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800115 uint32_t register_mask_index;
Mathieu Chartier32289082017-02-09 15:57:37 -0800116 DexRegisterMapEntry dex_register_entry;
117 size_t dex_register_map_index;
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800118 InvokeType invoke_type;
119 uint32_t dex_method_index;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700120 uint32_t dex_method_index_idx; // Index into dex method index table.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100121 };
122
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100123 struct InlineInfoEntry {
Andreas Gampee2abbc62017-09-15 11:59:26 -0700124 uint32_t dex_pc; // dex::kDexNoIndex for intrinsified native methods.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000125 ArtMethod* method;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100126 uint32_t method_index;
Mathieu Chartier32289082017-02-09 15:57:37 -0800127 DexRegisterMapEntry dex_register_entry;
128 size_t dex_register_map_index;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700129 uint32_t dex_method_index_idx; // Index into the dex method index table.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100130 };
131
Calin Juravle4f46ac52015-04-23 18:47:21 +0100132 void BeginStackMapEntry(uint32_t dex_pc,
133 uint32_t native_pc_offset,
134 uint32_t register_mask,
135 BitVector* sp_mask,
136 uint32_t num_dex_registers,
137 uint8_t inlining_depth);
138 void EndStackMapEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100139
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100140 void AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000141
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800142 void AddInvoke(InvokeType type, uint32_t dex_method_index);
143
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000144 void BeginInlineInfoEntry(ArtMethod* method,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100145 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000146 uint32_t num_dex_registers,
147 const DexFile* outer_dex_file = nullptr);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100148 void EndInlineInfoEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100149
Vladimir Markobd8c7252015-06-12 10:06:32 +0100150 size_t GetNumberOfStackMaps() const {
Vladimir Marko225b6462015-09-28 12:17:40 +0100151 return stack_maps_.size();
Vladimir Markobd8c7252015-06-12 10:06:32 +0100152 }
153
154 const StackMapEntry& GetStackMap(size_t i) const {
Vladimir Marko225b6462015-09-28 12:17:40 +0100155 return stack_maps_[i];
Vladimir Markobd8c7252015-06-12 10:06:32 +0100156 }
157
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000158 void SetStackMapNativePcOffset(size_t i, uint32_t native_pc_offset) {
Mathieu Chartier32289082017-02-09 15:57:37 -0800159 stack_maps_[i].native_pc_code_offset =
160 CodeOffset::FromOffset(native_pc_offset, instruction_set_);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000161 }
162
Calin Juravle4f46ac52015-04-23 18:47:21 +0100163 // Prepares the stream to fill in a memory region. Must be called before FillIn.
164 // Returns the size (in bytes) needed to store this stream.
165 size_t PrepareForFillIn();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700166 void FillInCodeInfo(MemoryRegion region);
167 void FillInMethodInfo(MemoryRegion region);
168
169 size_t ComputeMethodInfoSize() const;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000170
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000171 private:
Calin Juravle4f46ac52015-04-23 18:47:21 +0100172 size_t ComputeDexRegisterLocationCatalogSize() const;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800173
David Srbecky45aa5982016-03-18 02:15:09 +0000174 // Returns the number of unique stack masks.
175 size_t PrepareStackMasks(size_t entry_size_in_bits);
176
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800177 // Returns the number of unique register masks.
178 size_t PrepareRegisterMasks();
179
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700180 // Prepare and deduplicate method indices.
181 void PrepareMethodIndices();
182
Mathieu Chartier32289082017-02-09 15:57:37 -0800183 // Deduplicate entry if possible and return the corresponding index into dex_register_entries_
184 // array. If entry is not a duplicate, a new entry is added to dex_register_entries_.
185 size_t AddDexRegisterMapEntry(const DexRegisterMapEntry& entry);
186
187 // Return true if the two dex register map entries are equal.
188 bool DexRegisterMapEntryEquals(const DexRegisterMapEntry& a, const DexRegisterMapEntry& b) const;
189
190 // Fill in the corresponding entries of a register map.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100191 void FillInDexRegisterMap(DexRegisterMap dex_register_map,
192 uint32_t num_dex_registers,
193 const BitVector& live_dex_registers_mask,
194 uint32_t start_index_in_dex_register_locations) const;
Calin Juravle6ae70962015-03-18 16:31:28 +0000195
David Srbecky1bbdfd72016-02-24 16:39:26 +0000196 void CheckDexRegisterMap(const CodeInfo& code_info,
197 const DexRegisterMap& dex_register_map,
198 size_t num_dex_registers,
199 BitVector* live_dex_registers_mask,
200 size_t dex_register_locations_index) const;
201 void CheckCodeInfo(MemoryRegion region) const;
202
Vladimir Marko174b2e22017-10-12 13:34:49 +0100203 ScopedArenaAllocator* const allocator_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800204 const InstructionSet instruction_set_;
Vladimir Marko174b2e22017-10-12 13:34:49 +0100205 ScopedArenaVector<StackMapEntry> stack_maps_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000206
207 // A catalog of unique [location_kind, register_value] pairs (per method).
Vladimir Marko174b2e22017-10-12 13:34:49 +0100208 ScopedArenaVector<DexRegisterLocation> location_catalog_entries_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000209 // Map from Dex register location catalog entries to their indices in the
210 // location catalog.
Vladimir Marko174b2e22017-10-12 13:34:49 +0100211 using LocationCatalogEntriesIndices = ScopedArenaHashMap<DexRegisterLocation,
212 size_t,
213 LocationCatalogEntriesIndicesEmptyFn,
214 DexRegisterLocationHashFn>;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000215 LocationCatalogEntriesIndices location_catalog_entries_indices_;
216
Calin Juravlec416d332015-04-23 16:01:43 +0100217 // A set of concatenated maps of Dex register locations indices to `location_catalog_entries_`.
Vladimir Marko174b2e22017-10-12 13:34:49 +0100218 ScopedArenaVector<size_t> dex_register_locations_;
219 ScopedArenaVector<InlineInfoEntry> inline_infos_;
220 ScopedArenaVector<uint8_t> stack_masks_;
221 ScopedArenaVector<uint32_t> register_masks_;
222 ScopedArenaVector<uint32_t> method_indices_;
223 ScopedArenaVector<DexRegisterMapEntry> dex_register_entries_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100224 int stack_mask_max_;
David Srbecky052f8ca2018-04-26 15:42:54 +0100225
226 ScopedArenaVector<uint8_t> out_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100227
Vladimir Marko174b2e22017-10-12 13:34:49 +0100228 ScopedArenaSafeMap<uint32_t, ScopedArenaVector<uint32_t>> dex_map_hash_to_stack_map_indices_;
Calin Juravle6ae70962015-03-18 16:31:28 +0000229
Calin Juravle4f46ac52015-04-23 18:47:21 +0100230 StackMapEntry current_entry_;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100231 InlineInfoEntry current_inline_info_;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100232 uint32_t current_dex_register_;
233 bool in_inline_frame_;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100234
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100235 DISALLOW_COPY_AND_ASSIGN(StackMapStream);
236};
237
238} // namespace art
239
240#endif // ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_