blob: 0c626be89f428042603e99df063b7855ccf28a39 [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"
Ian Rogers0279ebb2014-10-08 17:27:48 -070022#include "base/value_object.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010023#include "memory_region.h"
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000024#include "nodes.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010025#include "stack_map.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010026#include "utils/growable_array.h"
27
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),
64 stack_maps_(allocator, 10),
Roland Levillaina552e1c2015-03-26 15:01:03 +000065 location_catalog_entries_(allocator, 4),
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000066 dex_register_locations_(allocator, 10 * 4),
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010067 inline_infos_(allocator, 2),
Andreas Gampe8eddd2a2014-07-28 14:53:22 -070068 stack_mask_max_(-1),
Nicolas Geoffray004c2302015-03-20 10:06:38 +000069 dex_pc_max_(0),
70 native_pc_offset_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),
Calin Juravle4f46ac52015-04-23 18:47:21 +010073 dex_map_hash_to_stack_map_indices_(std::less<uint32_t>(), allocator->Adapter()),
74 current_entry_(),
75 stack_mask_size_(0),
76 inline_info_size_(0),
77 dex_register_maps_size_(0),
78 stack_maps_size_(0),
79 dex_register_location_catalog_size_(0),
80 dex_register_location_catalog_start_(0),
81 stack_maps_start_(0),
82 dex_register_maps_start_(0),
83 inline_infos_start_(0),
84 needed_size_(0) {}
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010085
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010086 // See runtime/stack_map.h to know what these fields contain.
87 struct StackMapEntry {
88 uint32_t dex_pc;
Nicolas Geoffray39468442014-09-02 15:17:15 +010089 uint32_t native_pc_offset;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010090 uint32_t register_mask;
91 BitVector* sp_mask;
92 uint32_t num_dex_registers;
93 uint8_t inlining_depth;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000094 size_t dex_register_locations_start_index;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010095 size_t inline_infos_start_index;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000096 BitVector* live_dex_registers_mask;
Calin Juravle6ae70962015-03-18 16:31:28 +000097 uint32_t dex_register_map_hash;
Calin Juravle4f46ac52015-04-23 18:47:21 +010098 size_t same_dex_register_map_as_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010099 };
100
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100101 struct InlineInfoEntry {
102 uint32_t method_index;
103 };
104
Calin Juravle4f46ac52015-04-23 18:47:21 +0100105 void BeginStackMapEntry(uint32_t dex_pc,
106 uint32_t native_pc_offset,
107 uint32_t register_mask,
108 BitVector* sp_mask,
109 uint32_t num_dex_registers,
110 uint8_t inlining_depth);
111 void EndStackMapEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100112
Calin Juravlec416d332015-04-23 16:01:43 +0100113 void AddDexRegisterEntry(uint16_t dex_register,
114 DexRegisterLocation::Kind kind,
115 int32_t value);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000116
Calin Juravlec416d332015-04-23 16:01:43 +0100117 void AddInlineInfoEntry(uint32_t method_index);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100118
Calin Juravle4f46ac52015-04-23 18:47:21 +0100119 // Prepares the stream to fill in a memory region. Must be called before FillIn.
120 // Returns the size (in bytes) needed to store this stream.
121 size_t PrepareForFillIn();
Calin Juravlec416d332015-04-23 16:01:43 +0100122 void FillIn(MemoryRegion region);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000123
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000124 private:
Calin Juravle4f46ac52015-04-23 18:47:21 +0100125 size_t ComputeDexRegisterLocationCatalogSize() const;
126 size_t ComputeDexRegisterMapSize(const StackMapEntry& entry) const;
127 size_t ComputeDexRegisterMapsSize() const;
128 size_t ComputeInlineInfoSize() const;
129
130 // Returns the index of an entry with the same dex register map as the current_entry,
Calin Juravle6ae70962015-03-18 16:31:28 +0000131 // or kNoSameDexMapFound if no such entry exists.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100132 size_t FindEntryWithTheSameDexMap();
Calin Juravlec416d332015-04-23 16:01:43 +0100133 bool HaveTheSameDexMaps(const StackMapEntry& a, const StackMapEntry& b) const;
Calin Juravle6ae70962015-03-18 16:31:28 +0000134
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000135 ArenaAllocator* allocator_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100136 GrowableArray<StackMapEntry> stack_maps_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000137
138 // A catalog of unique [location_kind, register_value] pairs (per method).
139 GrowableArray<DexRegisterLocation> location_catalog_entries_;
140 // Map from Dex register location catalog entries to their indices in the
141 // location catalog.
142 typedef HashMap<DexRegisterLocation, size_t, LocationCatalogEntriesIndicesEmptyFn,
143 DexRegisterLocationHashFn> LocationCatalogEntriesIndices;
144 LocationCatalogEntriesIndices location_catalog_entries_indices_;
145
Calin Juravlec416d332015-04-23 16:01:43 +0100146 // A set of concatenated maps of Dex register locations indices to `location_catalog_entries_`.
Roland Levillaina552e1c2015-03-26 15:01:03 +0000147 GrowableArray<size_t> dex_register_locations_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100148 GrowableArray<InlineInfoEntry> inline_infos_;
149 int stack_mask_max_;
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000150 uint32_t dex_pc_max_;
151 uint32_t native_pc_offset_max_;
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100152 uint32_t register_mask_max_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100153 size_t number_of_stack_maps_with_inline_info_;
154
Calin Juravle6ae70962015-03-18 16:31:28 +0000155 ArenaSafeMap<uint32_t, GrowableArray<uint32_t>> dex_map_hash_to_stack_map_indices_;
156
Calin Juravle4f46ac52015-04-23 18:47:21 +0100157 StackMapEntry current_entry_;
158 size_t stack_mask_size_;
159 size_t inline_info_size_;
160 size_t dex_register_maps_size_;
161 size_t stack_maps_size_;
162 size_t dex_register_location_catalog_size_;
163 size_t dex_register_location_catalog_start_;
164 size_t stack_maps_start_;
165 size_t dex_register_maps_start_;
166 size_t inline_infos_start_;
167 size_t needed_size_;
168
Calin Juravle6ae70962015-03-18 16:31:28 +0000169 static constexpr uint32_t kNoSameDexMapFound = -1;
170
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100171 DISALLOW_COPY_AND_ASSIGN(StackMapStream);
172};
173
174} // namespace art
175
176#endif // ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_