blob: fc27a2b446501ac9c6ed8a924a8238b40a0b5c8b [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:
62 explicit StackMapStream(ArenaAllocator* allocator)
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000063 : allocator_(allocator),
Vladimir Marko225b6462015-09-28 12:17:40 +010064 stack_maps_(allocator->Adapter(kArenaAllocStackMapStream)),
65 location_catalog_entries_(allocator->Adapter(kArenaAllocStackMapStream)),
Vladimir Marko1f497642015-10-05 20:34:42 +010066 location_catalog_entries_indices_(allocator->Adapter(kArenaAllocStackMapStream)),
Vladimir Marko225b6462015-09-28 12:17:40 +010067 dex_register_locations_(allocator->Adapter(kArenaAllocStackMapStream)),
68 inline_infos_(allocator->Adapter(kArenaAllocStackMapStream)),
Andreas Gampe8eddd2a2014-07-28 14:53:22 -070069 stack_mask_max_(-1),
Nicolas Geoffray004c2302015-03-20 10:06:38 +000070 dex_pc_max_(0),
Nicolas Geoffray896f8f72015-03-30 15:44:25 +010071 register_mask_max_(0),
Calin Juravle6ae70962015-03-18 16:31:28 +000072 number_of_stack_maps_with_inline_info_(0),
Vladimir Marko225b6462015-09-28 12:17:40 +010073 dex_map_hash_to_stack_map_indices_(std::less<uint32_t>(),
74 allocator->Adapter(kArenaAllocStackMapStream)),
Calin Juravle4f46ac52015-04-23 18:47:21 +010075 current_entry_(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010076 current_inline_info_(),
Calin Juravle4f46ac52015-04-23 18:47:21 +010077 stack_mask_size_(0),
78 inline_info_size_(0),
79 dex_register_maps_size_(0),
80 stack_maps_size_(0),
81 dex_register_location_catalog_size_(0),
82 dex_register_location_catalog_start_(0),
83 stack_maps_start_(0),
84 dex_register_maps_start_(0),
85 inline_infos_start_(0),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010086 needed_size_(0),
87 current_dex_register_(0),
Vladimir Marko225b6462015-09-28 12:17:40 +010088 in_inline_frame_(false) {
89 stack_maps_.reserve(10);
90 location_catalog_entries_.reserve(4);
91 dex_register_locations_.reserve(10 * 4);
92 inline_infos_.reserve(2);
93 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010094
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010095 // See runtime/stack_map.h to know what these fields contain.
96 struct StackMapEntry {
97 uint32_t dex_pc;
Nicolas Geoffray39468442014-09-02 15:17:15 +010098 uint32_t native_pc_offset;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010099 uint32_t register_mask;
100 BitVector* sp_mask;
101 uint32_t num_dex_registers;
102 uint8_t inlining_depth;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000103 size_t dex_register_locations_start_index;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100104 size_t inline_infos_start_index;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000105 BitVector* live_dex_registers_mask;
Calin Juravle6ae70962015-03-18 16:31:28 +0000106 uint32_t dex_register_map_hash;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100107 size_t same_dex_register_map_as_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100108 };
109
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100110 struct InlineInfoEntry {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100111 uint32_t dex_pc;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100112 uint32_t method_index;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100113 InvokeType invoke_type;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100114 uint32_t num_dex_registers;
115 BitVector* live_dex_registers_mask;
116 size_t dex_register_locations_start_index;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100117 };
118
Calin Juravle4f46ac52015-04-23 18:47:21 +0100119 void BeginStackMapEntry(uint32_t dex_pc,
120 uint32_t native_pc_offset,
121 uint32_t register_mask,
122 BitVector* sp_mask,
123 uint32_t num_dex_registers,
124 uint8_t inlining_depth);
125 void EndStackMapEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100126
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100127 void AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000128
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100129 void BeginInlineInfoEntry(uint32_t method_index,
130 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100131 InvokeType invoke_type,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100132 uint32_t num_dex_registers);
133 void EndInlineInfoEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100134
Vladimir Markobd8c7252015-06-12 10:06:32 +0100135 size_t GetNumberOfStackMaps() const {
Vladimir Marko225b6462015-09-28 12:17:40 +0100136 return stack_maps_.size();
Vladimir Markobd8c7252015-06-12 10:06:32 +0100137 }
138
139 const StackMapEntry& GetStackMap(size_t i) const {
Vladimir Marko225b6462015-09-28 12:17:40 +0100140 return stack_maps_[i];
Vladimir Markobd8c7252015-06-12 10:06:32 +0100141 }
142
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000143 void SetStackMapNativePcOffset(size_t i, uint32_t native_pc_offset) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100144 stack_maps_[i].native_pc_offset = native_pc_offset;
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000145 }
146
Vladimir Markobd8c7252015-06-12 10:06:32 +0100147 uint32_t ComputeMaxNativePcOffset() const;
148
Calin Juravle4f46ac52015-04-23 18:47:21 +0100149 // Prepares the stream to fill in a memory region. Must be called before FillIn.
150 // Returns the size (in bytes) needed to store this stream.
151 size_t PrepareForFillIn();
Calin Juravlec416d332015-04-23 16:01:43 +0100152 void FillIn(MemoryRegion region);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000153
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000154 private:
Calin Juravle4f46ac52015-04-23 18:47:21 +0100155 size_t ComputeDexRegisterLocationCatalogSize() const;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100156 size_t ComputeDexRegisterMapSize(uint32_t num_dex_registers,
Vladimir Marko225b6462015-09-28 12:17:40 +0100157 const BitVector* live_dex_registers_mask) const;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100158 size_t ComputeDexRegisterMapsSize() const;
159 size_t ComputeInlineInfoSize() const;
160
161 // Returns the index of an entry with the same dex register map as the current_entry,
Calin Juravle6ae70962015-03-18 16:31:28 +0000162 // or kNoSameDexMapFound if no such entry exists.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100163 size_t FindEntryWithTheSameDexMap();
Calin Juravlec416d332015-04-23 16:01:43 +0100164 bool HaveTheSameDexMaps(const StackMapEntry& a, const StackMapEntry& b) const;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100165 void FillInDexRegisterMap(DexRegisterMap dex_register_map,
166 uint32_t num_dex_registers,
167 const BitVector& live_dex_registers_mask,
168 uint32_t start_index_in_dex_register_locations) const;
Calin Juravle6ae70962015-03-18 16:31:28 +0000169
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000170 ArenaAllocator* allocator_;
Vladimir Marko225b6462015-09-28 12:17:40 +0100171 ArenaVector<StackMapEntry> stack_maps_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000172
173 // A catalog of unique [location_kind, register_value] pairs (per method).
Vladimir Marko225b6462015-09-28 12:17:40 +0100174 ArenaVector<DexRegisterLocation> location_catalog_entries_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000175 // Map from Dex register location catalog entries to their indices in the
176 // location catalog.
Vladimir Marko1f497642015-10-05 20:34:42 +0100177 using LocationCatalogEntriesIndices = ArenaHashMap<DexRegisterLocation,
178 size_t,
179 LocationCatalogEntriesIndicesEmptyFn,
180 DexRegisterLocationHashFn>;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000181 LocationCatalogEntriesIndices location_catalog_entries_indices_;
182
Calin Juravlec416d332015-04-23 16:01:43 +0100183 // A set of concatenated maps of Dex register locations indices to `location_catalog_entries_`.
Vladimir Marko225b6462015-09-28 12:17:40 +0100184 ArenaVector<size_t> dex_register_locations_;
185 ArenaVector<InlineInfoEntry> inline_infos_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100186 int stack_mask_max_;
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000187 uint32_t dex_pc_max_;
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100188 uint32_t register_mask_max_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100189 size_t number_of_stack_maps_with_inline_info_;
190
Vladimir Marko225b6462015-09-28 12:17:40 +0100191 ArenaSafeMap<uint32_t, ArenaVector<uint32_t>> dex_map_hash_to_stack_map_indices_;
Calin Juravle6ae70962015-03-18 16:31:28 +0000192
Calin Juravle4f46ac52015-04-23 18:47:21 +0100193 StackMapEntry current_entry_;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100194 InlineInfoEntry current_inline_info_;
David Brazdilf677ebf2015-05-29 16:29:43 +0100195 StackMapEncoding stack_map_encoding_;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100196 size_t stack_mask_size_;
197 size_t inline_info_size_;
198 size_t dex_register_maps_size_;
199 size_t stack_maps_size_;
200 size_t dex_register_location_catalog_size_;
201 size_t dex_register_location_catalog_start_;
202 size_t stack_maps_start_;
203 size_t dex_register_maps_start_;
204 size_t inline_infos_start_;
205 size_t needed_size_;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100206 uint32_t current_dex_register_;
207 bool in_inline_frame_;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100208
Calin Juravle6ae70962015-03-18 16:31:28 +0000209 static constexpr uint32_t kNoSameDexMapFound = -1;
210
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100211 DISALLOW_COPY_AND_ASSIGN(StackMapStream);
212};
213
214} // namespace art
215
216#endif // ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_