blob: bba3d51e62dea3eab29b7b51acde1fda5c0f6ce2 [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/arena_containers.h"
21#include "base/bit_vector-inl.h"
Andreas Gampe2a5c4682015-08-14 08:22:54 -070022#include "base/hash_map.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070023#include "base/value_object.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010024#include "memory_region.h"
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000025#include "nodes.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010026#include "stack_map.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010027
28namespace art {
29
Roland Levillaina552e1c2015-03-26 15:01:03 +000030// Helper to build art::StackMapStream::LocationCatalogEntriesIndices.
31class 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.
43class 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 Geoffray99ea58c2014-07-02 15:08:17 +010056/**
Nicolas Geoffray39468442014-09-02 15:17:15 +010057 * Collects and builds stack maps for a method. All the stack maps
58 * for a method are placed in a CodeInfo object.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010059 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010060class StackMapStream : public ValueObject {
61 public:
Mathieu Chartiera2f526f2017-01-19 14:48:48 -080062 explicit StackMapStream(ArenaAllocator* allocator,
63 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 Chartier32289082017-02-09 15:57:37 -080073 dex_register_entries_(allocator->Adapter(kArenaAllocStackMapStream)),
Andreas Gampe8eddd2a2014-07-28 14:53:22 -070074 stack_mask_max_(-1),
Nicolas Geoffray004c2302015-03-20 10:06:38 +000075 dex_pc_max_(0),
Nicolas Geoffray896f8f72015-03-30 15:44:25 +010076 register_mask_max_(0),
Calin Juravle6ae70962015-03-18 16:31:28 +000077 number_of_stack_maps_with_inline_info_(0),
Vladimir Marko225b6462015-09-28 12:17:40 +010078 dex_map_hash_to_stack_map_indices_(std::less<uint32_t>(),
79 allocator->Adapter(kArenaAllocStackMapStream)),
Calin Juravle4f46ac52015-04-23 18:47:21 +010080 current_entry_(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010081 current_inline_info_(),
David Srbecky09ed0982016-02-12 21:58:43 +000082 code_info_encoding_(allocator->Adapter(kArenaAllocStackMapStream)),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010083 needed_size_(0),
84 current_dex_register_(0),
Vladimir Marko225b6462015-09-28 12:17:40 +010085 in_inline_frame_(false) {
86 stack_maps_.reserve(10);
87 location_catalog_entries_.reserve(4);
88 dex_register_locations_.reserve(10 * 4);
89 inline_infos_.reserve(2);
David Srbecky09ed0982016-02-12 21:58:43 +000090 code_info_encoding_.reserve(16);
Vladimir Marko225b6462015-09-28 12:17:40 +010091 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010092
Mathieu Chartier32289082017-02-09 15:57:37 -080093 // A dex register map entry for a single stack map entry, contains what registers are live as
94 // well as indices into the location catalog.
95 class DexRegisterMapEntry {
96 public:
97 static const size_t kOffsetUnassigned = -1;
98
99 BitVector* live_dex_registers_mask;
100 uint32_t num_dex_registers;
101 size_t locations_start_index;
102 // Computed fields
103 size_t hash = 0;
104 size_t offset = kOffsetUnassigned;
105
106 size_t ComputeSize(size_t catalog_size) const;
107 };
108
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100109 // See runtime/stack_map.h to know what these fields contain.
110 struct StackMapEntry {
111 uint32_t dex_pc;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800112 CodeOffset native_pc_code_offset;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100113 uint32_t register_mask;
114 BitVector* sp_mask;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100115 uint8_t inlining_depth;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100116 size_t inline_infos_start_index;
David Srbecky45aa5982016-03-18 02:15:09 +0000117 uint32_t stack_mask_index;
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800118 uint32_t register_mask_index;
Mathieu Chartier32289082017-02-09 15:57:37 -0800119 DexRegisterMapEntry dex_register_entry;
120 size_t dex_register_map_index;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100121 };
122
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100123 struct InlineInfoEntry {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100124 uint32_t dex_pc; // DexFile::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;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100129 };
130
Calin Juravle4f46ac52015-04-23 18:47:21 +0100131 void BeginStackMapEntry(uint32_t dex_pc,
132 uint32_t native_pc_offset,
133 uint32_t register_mask,
134 BitVector* sp_mask,
135 uint32_t num_dex_registers,
136 uint8_t inlining_depth);
137 void EndStackMapEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100138
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100139 void AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000140
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000141 void BeginInlineInfoEntry(ArtMethod* method,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100142 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000143 uint32_t num_dex_registers,
144 const DexFile* outer_dex_file = nullptr);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100145 void EndInlineInfoEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100146
Vladimir Markobd8c7252015-06-12 10:06:32 +0100147 size_t GetNumberOfStackMaps() const {
Vladimir Marko225b6462015-09-28 12:17:40 +0100148 return stack_maps_.size();
Vladimir Markobd8c7252015-06-12 10:06:32 +0100149 }
150
151 const StackMapEntry& GetStackMap(size_t i) const {
Vladimir Marko225b6462015-09-28 12:17:40 +0100152 return stack_maps_[i];
Vladimir Markobd8c7252015-06-12 10:06:32 +0100153 }
154
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000155 void SetStackMapNativePcOffset(size_t i, uint32_t native_pc_offset) {
Mathieu Chartier32289082017-02-09 15:57:37 -0800156 stack_maps_[i].native_pc_code_offset =
157 CodeOffset::FromOffset(native_pc_offset, instruction_set_);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000158 }
159
Calin Juravle4f46ac52015-04-23 18:47:21 +0100160 // Prepares the stream to fill in a memory region. Must be called before FillIn.
161 // Returns the size (in bytes) needed to store this stream.
162 size_t PrepareForFillIn();
Calin Juravlec416d332015-04-23 16:01:43 +0100163 void FillIn(MemoryRegion region);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000164
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000165 private:
Calin Juravle4f46ac52015-04-23 18:47:21 +0100166 size_t ComputeDexRegisterLocationCatalogSize() const;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100167 size_t ComputeDexRegisterMapsSize() const;
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800168 void ComputeInlineInfoEncoding(InlineInfoEncoding* encoding,
169 size_t dex_register_maps_bytes);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100170
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800171 CodeOffset ComputeMaxNativePcCodeOffset() const;
172
David Srbecky45aa5982016-03-18 02:15:09 +0000173 // Returns the number of unique stack masks.
174 size_t PrepareStackMasks(size_t entry_size_in_bits);
175
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800176 // Returns the number of unique register masks.
177 size_t PrepareRegisterMasks();
178
Mathieu Chartier32289082017-02-09 15:57:37 -0800179 // Deduplicate entry if possible and return the corresponding index into dex_register_entries_
180 // array. If entry is not a duplicate, a new entry is added to dex_register_entries_.
181 size_t AddDexRegisterMapEntry(const DexRegisterMapEntry& entry);
182
183 // Return true if the two dex register map entries are equal.
184 bool DexRegisterMapEntryEquals(const DexRegisterMapEntry& a, const DexRegisterMapEntry& b) const;
185
186 // Fill in the corresponding entries of a register map.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100187 void FillInDexRegisterMap(DexRegisterMap dex_register_map,
188 uint32_t num_dex_registers,
189 const BitVector& live_dex_registers_mask,
190 uint32_t start_index_in_dex_register_locations) const;
Calin Juravle6ae70962015-03-18 16:31:28 +0000191
Mathieu Chartier32289082017-02-09 15:57:37 -0800192 // Returns the offset for the dex register inside of the dex register location region. See FillIn.
193 // Only copies the dex register map if the offset for the entry is not already assigned.
194 size_t MaybeCopyDexRegisterMap(DexRegisterMapEntry& entry,
195 size_t* current_offset,
196 MemoryRegion dex_register_locations_region);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000197 void CheckDexRegisterMap(const CodeInfo& code_info,
198 const DexRegisterMap& dex_register_map,
199 size_t num_dex_registers,
200 BitVector* live_dex_registers_mask,
201 size_t dex_register_locations_index) const;
202 void CheckCodeInfo(MemoryRegion region) const;
203
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000204 ArenaAllocator* allocator_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800205 const InstructionSet instruction_set_;
Vladimir Marko225b6462015-09-28 12:17:40 +0100206 ArenaVector<StackMapEntry> stack_maps_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000207
208 // A catalog of unique [location_kind, register_value] pairs (per method).
Vladimir Marko225b6462015-09-28 12:17:40 +0100209 ArenaVector<DexRegisterLocation> location_catalog_entries_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000210 // Map from Dex register location catalog entries to their indices in the
211 // location catalog.
Vladimir Marko1f497642015-10-05 20:34:42 +0100212 using LocationCatalogEntriesIndices = ArenaHashMap<DexRegisterLocation,
213 size_t,
214 LocationCatalogEntriesIndicesEmptyFn,
215 DexRegisterLocationHashFn>;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000216 LocationCatalogEntriesIndices location_catalog_entries_indices_;
217
Calin Juravlec416d332015-04-23 16:01:43 +0100218 // A set of concatenated maps of Dex register locations indices to `location_catalog_entries_`.
Vladimir Marko225b6462015-09-28 12:17:40 +0100219 ArenaVector<size_t> dex_register_locations_;
220 ArenaVector<InlineInfoEntry> inline_infos_;
David Srbecky45aa5982016-03-18 02:15:09 +0000221 ArenaVector<uint8_t> stack_masks_;
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800222 ArenaVector<uint32_t> register_masks_;
Mathieu Chartier32289082017-02-09 15:57:37 -0800223 ArenaVector<DexRegisterMapEntry> dex_register_entries_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100224 int stack_mask_max_;
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000225 uint32_t dex_pc_max_;
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100226 uint32_t register_mask_max_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100227 size_t number_of_stack_maps_with_inline_info_;
228
Vladimir Marko225b6462015-09-28 12:17:40 +0100229 ArenaSafeMap<uint32_t, ArenaVector<uint32_t>> dex_map_hash_to_stack_map_indices_;
Calin Juravle6ae70962015-03-18 16:31:28 +0000230
Calin Juravle4f46ac52015-04-23 18:47:21 +0100231 StackMapEntry current_entry_;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100232 InlineInfoEntry current_inline_info_;
David Srbecky09ed0982016-02-12 21:58:43 +0000233 ArenaVector<uint8_t> code_info_encoding_;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100234 size_t needed_size_;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100235 uint32_t current_dex_register_;
236 bool in_inline_frame_;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100237
Calin Juravle6ae70962015-03-18 16:31:28 +0000238 static constexpr uint32_t kNoSameDexMapFound = -1;
239
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100240 DISALLOW_COPY_AND_ASSIGN(StackMapStream);
241};
242
243} // namespace art
244
245#endif // ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_