blob: 37a9bfc3ca54519ce43be999f42e5bc676ebe3f4 [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)),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070071 method_indices_(allocator->Adapter(kArenaAllocStackMapStream)),
Mathieu Chartier32289082017-02-09 15:57:37 -080072 dex_register_entries_(allocator->Adapter(kArenaAllocStackMapStream)),
David Srbecky052f8ca2018-04-26 15:42:54 +010073 out_(allocator->Adapter(kArenaAllocStackMapStream)),
Vladimir Marko225b6462015-09-28 12:17:40 +010074 dex_map_hash_to_stack_map_indices_(std::less<uint32_t>(),
75 allocator->Adapter(kArenaAllocStackMapStream)),
Calin Juravle4f46ac52015-04-23 18:47:21 +010076 current_entry_(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010077 current_inline_info_(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010078 current_dex_register_(0),
Vladimir Marko225b6462015-09-28 12:17:40 +010079 in_inline_frame_(false) {
80 stack_maps_.reserve(10);
David Srbecky052f8ca2018-04-26 15:42:54 +010081 out_.reserve(64);
Vladimir Marko225b6462015-09-28 12:17:40 +010082 location_catalog_entries_.reserve(4);
83 dex_register_locations_.reserve(10 * 4);
84 inline_infos_.reserve(2);
85 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010086
Mathieu Chartier32289082017-02-09 15:57:37 -080087 // A dex register map entry for a single stack map entry, contains what registers are live as
88 // well as indices into the location catalog.
89 class DexRegisterMapEntry {
90 public:
David Srbecky052f8ca2018-04-26 15:42:54 +010091 static const uint32_t kOffsetUnassigned = -1;
Mathieu Chartier32289082017-02-09 15:57:37 -080092
93 BitVector* live_dex_registers_mask;
94 uint32_t num_dex_registers;
95 size_t locations_start_index;
96 // Computed fields
97 size_t hash = 0;
David Srbecky052f8ca2018-04-26 15:42:54 +010098 uint32_t offset = kOffsetUnassigned;
Mathieu Chartier32289082017-02-09 15:57:37 -080099
100 size_t ComputeSize(size_t catalog_size) const;
101 };
102
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100103 // See runtime/stack_map.h to know what these fields contain.
104 struct StackMapEntry {
105 uint32_t dex_pc;
David Srbeckyd02b23f2018-05-29 23:27:22 +0100106 uint32_t packed_native_pc;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100107 uint32_t register_mask;
108 BitVector* sp_mask;
David Srbecky052f8ca2018-04-26 15:42:54 +0100109 uint32_t inlining_depth;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100110 size_t inline_infos_start_index;
David Srbecky45aa5982016-03-18 02:15:09 +0000111 uint32_t stack_mask_index;
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800112 uint32_t register_mask_index;
Mathieu Chartier32289082017-02-09 15:57:37 -0800113 DexRegisterMapEntry dex_register_entry;
114 size_t dex_register_map_index;
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800115 InvokeType invoke_type;
116 uint32_t dex_method_index;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700117 uint32_t dex_method_index_idx; // Index into dex method index table.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100118 };
119
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100120 struct InlineInfoEntry {
Andreas Gampee2abbc62017-09-15 11:59:26 -0700121 uint32_t dex_pc; // dex::kDexNoIndex for intrinsified native methods.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000122 ArtMethod* method;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100123 uint32_t method_index;
Mathieu Chartier32289082017-02-09 15:57:37 -0800124 DexRegisterMapEntry dex_register_entry;
125 size_t dex_register_map_index;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700126 uint32_t dex_method_index_idx; // Index into the dex method index table.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100127 };
128
Calin Juravle4f46ac52015-04-23 18:47:21 +0100129 void BeginStackMapEntry(uint32_t dex_pc,
130 uint32_t native_pc_offset,
131 uint32_t register_mask,
132 BitVector* sp_mask,
133 uint32_t num_dex_registers,
134 uint8_t inlining_depth);
135 void EndStackMapEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100136
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100137 void AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000138
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800139 void AddInvoke(InvokeType type, uint32_t dex_method_index);
140
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
David Srbeckyd02b23f2018-05-29 23:27:22 +0100151 uint32_t GetStackMapNativePcOffset(size_t i);
152 void SetStackMapNativePcOffset(size_t i, uint32_t native_pc_offset);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000153
Calin Juravle4f46ac52015-04-23 18:47:21 +0100154 // Prepares the stream to fill in a memory region. Must be called before FillIn.
155 // Returns the size (in bytes) needed to store this stream.
156 size_t PrepareForFillIn();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700157 void FillInCodeInfo(MemoryRegion region);
158 void FillInMethodInfo(MemoryRegion region);
159
160 size_t ComputeMethodInfoSize() const;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000161
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000162 private:
Calin Juravle4f46ac52015-04-23 18:47:21 +0100163 size_t ComputeDexRegisterLocationCatalogSize() const;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800164
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700165 // Prepare and deduplicate method indices.
166 void PrepareMethodIndices();
167
Mathieu Chartier32289082017-02-09 15:57:37 -0800168 // Deduplicate entry if possible and return the corresponding index into dex_register_entries_
169 // array. If entry is not a duplicate, a new entry is added to dex_register_entries_.
170 size_t AddDexRegisterMapEntry(const DexRegisterMapEntry& entry);
171
172 // Return true if the two dex register map entries are equal.
173 bool DexRegisterMapEntryEquals(const DexRegisterMapEntry& a, const DexRegisterMapEntry& b) const;
174
175 // Fill in the corresponding entries of a register map.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100176 void FillInDexRegisterMap(DexRegisterMap dex_register_map,
177 uint32_t num_dex_registers,
178 const BitVector& live_dex_registers_mask,
179 uint32_t start_index_in_dex_register_locations) const;
Calin Juravle6ae70962015-03-18 16:31:28 +0000180
David Srbecky1bbdfd72016-02-24 16:39:26 +0000181 void CheckDexRegisterMap(const CodeInfo& code_info,
182 const DexRegisterMap& dex_register_map,
183 size_t num_dex_registers,
184 BitVector* live_dex_registers_mask,
185 size_t dex_register_locations_index) const;
186 void CheckCodeInfo(MemoryRegion region) const;
187
Vladimir Marko174b2e22017-10-12 13:34:49 +0100188 ScopedArenaAllocator* const allocator_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800189 const InstructionSet instruction_set_;
Vladimir Marko174b2e22017-10-12 13:34:49 +0100190 ScopedArenaVector<StackMapEntry> stack_maps_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000191
192 // A catalog of unique [location_kind, register_value] pairs (per method).
Vladimir Marko174b2e22017-10-12 13:34:49 +0100193 ScopedArenaVector<DexRegisterLocation> location_catalog_entries_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000194 // Map from Dex register location catalog entries to their indices in the
195 // location catalog.
Vladimir Marko174b2e22017-10-12 13:34:49 +0100196 using LocationCatalogEntriesIndices = ScopedArenaHashMap<DexRegisterLocation,
197 size_t,
198 LocationCatalogEntriesIndicesEmptyFn,
199 DexRegisterLocationHashFn>;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000200 LocationCatalogEntriesIndices location_catalog_entries_indices_;
201
Calin Juravlec416d332015-04-23 16:01:43 +0100202 // A set of concatenated maps of Dex register locations indices to `location_catalog_entries_`.
Vladimir Marko174b2e22017-10-12 13:34:49 +0100203 ScopedArenaVector<size_t> dex_register_locations_;
204 ScopedArenaVector<InlineInfoEntry> inline_infos_;
Vladimir Marko174b2e22017-10-12 13:34:49 +0100205 ScopedArenaVector<uint32_t> method_indices_;
206 ScopedArenaVector<DexRegisterMapEntry> dex_register_entries_;
David Srbecky052f8ca2018-04-26 15:42:54 +0100207
208 ScopedArenaVector<uint8_t> out_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100209
Vladimir Marko174b2e22017-10-12 13:34:49 +0100210 ScopedArenaSafeMap<uint32_t, ScopedArenaVector<uint32_t>> dex_map_hash_to_stack_map_indices_;
Calin Juravle6ae70962015-03-18 16:31:28 +0000211
Calin Juravle4f46ac52015-04-23 18:47:21 +0100212 StackMapEntry current_entry_;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100213 InlineInfoEntry current_inline_info_;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100214 uint32_t current_dex_register_;
215 bool in_inline_frame_;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100216
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100217 DISALLOW_COPY_AND_ASSIGN(StackMapStream);
218};
219
220} // namespace art
221
222#endif // ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_