1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_
#define ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_
#include "base/bit_vector.h"
#include "base/value_object.h"
#include "memory_region.h"
#include "stack_map.h"
#include "utils/growable_array.h"
namespace art {
/**
* Collects and builds stack maps for a method. All the stack maps
* for a method are placed in a CodeInfo object.
*/
class StackMapStream : public ValueObject {
public:
explicit StackMapStream(ArenaAllocator* allocator)
: stack_maps_(allocator, 10),
dex_register_maps_(allocator, 10 * 4),
inline_infos_(allocator, 2),
stack_mask_max_(-1),
number_of_stack_maps_with_inline_info_(0) {}
// Compute bytes needed to encode a mask with the given maximum element.
static uint32_t StackMaskEncodingSize(int max_element) {
int number_of_bits = max_element + 1; // Need room for max element too.
return RoundUp(number_of_bits, kBitsPerByte) / kBitsPerByte;
}
// See runtime/stack_map.h to know what these fields contain.
struct StackMapEntry {
uint32_t dex_pc;
uint32_t native_pc_offset;
uint32_t register_mask;
BitVector* sp_mask;
uint32_t num_dex_registers;
uint8_t inlining_depth;
size_t dex_register_maps_start_index;
size_t inline_infos_start_index;
};
struct InlineInfoEntry {
uint32_t method_index;
};
void AddStackMapEntry(uint32_t dex_pc,
uint32_t native_pc_offset,
uint32_t register_mask,
BitVector* sp_mask,
uint32_t num_dex_registers,
uint8_t inlining_depth) {
StackMapEntry entry;
entry.dex_pc = dex_pc;
entry.native_pc_offset = native_pc_offset;
entry.register_mask = register_mask;
entry.sp_mask = sp_mask;
entry.num_dex_registers = num_dex_registers;
entry.inlining_depth = inlining_depth;
entry.dex_register_maps_start_index = dex_register_maps_.Size();
entry.inline_infos_start_index = inline_infos_.Size();
stack_maps_.Add(entry);
if (sp_mask != nullptr) {
stack_mask_max_ = std::max(stack_mask_max_, sp_mask->GetHighestBitSet());
}
if (inlining_depth > 0) {
number_of_stack_maps_with_inline_info_++;
}
}
void AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value) {
// Ensure we only use non-compressed location kind at this stage.
DCHECK(DexRegisterLocation::IsShortLocationKind(kind))
<< DexRegisterLocation::PrettyDescriptor(kind);
dex_register_maps_.Add(DexRegisterLocation(kind, value));
}
void AddInlineInfoEntry(uint32_t method_index) {
InlineInfoEntry entry;
entry.method_index = method_index;
inline_infos_.Add(entry);
}
size_t ComputeNeededSize() const {
size_t size = CodeInfo::kFixedSize
+ ComputeStackMapsSize()
+ ComputeDexRegisterMapsSize()
+ ComputeInlineInfoSize();
// On ARM, CodeInfo data must be 4-byte aligned.
return RoundUp(size, kWordAlignment);
}
size_t ComputeStackMaskSize() const {
return StackMaskEncodingSize(stack_mask_max_);
}
size_t ComputeStackMapsSize() const {
return stack_maps_.Size() * StackMap::ComputeStackMapSize(ComputeStackMaskSize());
}
// Compute the size of the Dex register map of `entry`.
size_t ComputeDexRegisterMapSize(const StackMapEntry& entry) const {
size_t size = DexRegisterMap::kFixedSize;
for (size_t j = 0; j < entry.num_dex_registers; ++j) {
DexRegisterLocation dex_register_location =
dex_register_maps_.Get(entry.dex_register_maps_start_index + j);
size += DexRegisterMap::EntrySize(dex_register_location);
}
return size;
}
// Compute the size of all the Dex register maps.
size_t ComputeDexRegisterMapsSize() const {
size_t size = stack_maps_.Size() * DexRegisterMap::kFixedSize;
// The size of each register location depends on the type of
// the entry.
for (size_t i = 0, e = dex_register_maps_.Size(); i < e; ++i) {
DexRegisterLocation entry = dex_register_maps_.Get(i);
size += DexRegisterMap::EntrySize(entry);
}
return size;
}
// Compute the size of all the inline information pieces.
size_t ComputeInlineInfoSize() const {
return inline_infos_.Size() * InlineInfo::SingleEntrySize()
// For encoding the depth.
+ (number_of_stack_maps_with_inline_info_ * InlineInfo::kFixedSize);
}
size_t ComputeDexRegisterMapsStart() const {
return CodeInfo::kFixedSize + ComputeStackMapsSize();
}
size_t ComputeInlineInfoStart() const {
return ComputeDexRegisterMapsStart() + ComputeDexRegisterMapsSize();
}
void FillIn(MemoryRegion region) {
CodeInfo code_info(region);
DCHECK_EQ(region.size(), ComputeNeededSize());
code_info.SetOverallSize(region.size());
size_t stack_mask_size = ComputeStackMaskSize();
uint8_t* memory_start = region.start();
MemoryRegion dex_register_maps_region = region.Subregion(
ComputeDexRegisterMapsStart(),
ComputeDexRegisterMapsSize());
MemoryRegion inline_infos_region = region.Subregion(
ComputeInlineInfoStart(),
ComputeInlineInfoSize());
code_info.SetNumberOfStackMaps(stack_maps_.Size());
code_info.SetStackMaskSize(stack_mask_size);
DCHECK_EQ(code_info.StackMapsSize(), ComputeStackMapsSize());
uintptr_t next_dex_register_map_offset = 0;
uintptr_t next_inline_info_offset = 0;
for (size_t i = 0, e = stack_maps_.Size(); i < e; ++i) {
StackMap stack_map = code_info.GetStackMapAt(i);
StackMapEntry entry = stack_maps_.Get(i);
stack_map.SetDexPc(entry.dex_pc);
stack_map.SetNativePcOffset(entry.native_pc_offset);
stack_map.SetRegisterMask(entry.register_mask);
if (entry.sp_mask != nullptr) {
stack_map.SetStackMask(*entry.sp_mask);
}
if (entry.num_dex_registers != 0) {
// Set the Dex register map.
MemoryRegion register_region =
dex_register_maps_region.Subregion(
next_dex_register_map_offset,
ComputeDexRegisterMapSize(entry));
next_dex_register_map_offset += register_region.size();
DexRegisterMap dex_register_map(register_region);
stack_map.SetDexRegisterMapOffset(register_region.start() - memory_start);
// Offset in `dex_register_map` where to store the next register entry.
size_t offset = DexRegisterMap::kFixedSize;
for (size_t j = 0; j < entry.num_dex_registers; ++j) {
DexRegisterLocation dex_register_location =
dex_register_maps_.Get(entry.dex_register_maps_start_index + j);
dex_register_map.SetRegisterInfo(offset, dex_register_location);
offset += DexRegisterMap::EntrySize(dex_register_location);
}
// Ensure we reached the end of the Dex registers region.
DCHECK_EQ(offset, register_region.size());
} else {
stack_map.SetDexRegisterMapOffset(StackMap::kNoDexRegisterMap);
}
// Set the inlining info.
if (entry.inlining_depth != 0) {
MemoryRegion inline_region = inline_infos_region.Subregion(
next_inline_info_offset,
InlineInfo::kFixedSize + entry.inlining_depth * InlineInfo::SingleEntrySize());
next_inline_info_offset += inline_region.size();
InlineInfo inline_info(inline_region);
stack_map.SetInlineDescriptorOffset(inline_region.start() - memory_start);
inline_info.SetDepth(entry.inlining_depth);
for (size_t j = 0; j < entry.inlining_depth; ++j) {
InlineInfoEntry inline_entry = inline_infos_.Get(j + entry.inline_infos_start_index);
inline_info.SetMethodReferenceIndexAtDepth(j, inline_entry.method_index);
}
} else {
stack_map.SetInlineDescriptorOffset(StackMap::kNoInlineInfo);
}
}
}
private:
GrowableArray<StackMapEntry> stack_maps_;
GrowableArray<DexRegisterLocation> dex_register_maps_;
GrowableArray<InlineInfoEntry> inline_infos_;
int stack_mask_max_;
size_t number_of_stack_maps_with_inline_info_;
DISALLOW_COPY_AND_ASSIGN(StackMapStream);
};
} // namespace art
#endif // ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_
|