blob: 63a02862b457f408f69188ec18c47dc6979ba104 [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
20#include "base/bit_vector.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070021#include "base/value_object.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010022#include "memory_region.h"
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000023#include "nodes.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010024#include "stack_map.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010025#include "utils/growable_array.h"
26
27namespace art {
28
29/**
Nicolas Geoffray39468442014-09-02 15:17:15 +010030 * Collects and builds stack maps for a method. All the stack maps
31 * for a method are placed in a CodeInfo object.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010032 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010033class StackMapStream : public ValueObject {
34 public:
35 explicit StackMapStream(ArenaAllocator* allocator)
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000036 : allocator_(allocator),
37 stack_maps_(allocator, 10),
38 dex_register_locations_(allocator, 10 * 4),
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010039 inline_infos_(allocator, 2),
Andreas Gampe8eddd2a2014-07-28 14:53:22 -070040 stack_mask_max_(-1),
41 number_of_stack_maps_with_inline_info_(0) {}
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010042
43 // Compute bytes needed to encode a mask with the given maximum element.
44 static uint32_t StackMaskEncodingSize(int max_element) {
45 int number_of_bits = max_element + 1; // Need room for max element too.
46 return RoundUp(number_of_bits, kBitsPerByte) / kBitsPerByte;
47 }
48
49 // See runtime/stack_map.h to know what these fields contain.
50 struct StackMapEntry {
51 uint32_t dex_pc;
Nicolas Geoffray39468442014-09-02 15:17:15 +010052 uint32_t native_pc_offset;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010053 uint32_t register_mask;
54 BitVector* sp_mask;
55 uint32_t num_dex_registers;
56 uint8_t inlining_depth;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000057 size_t dex_register_locations_start_index;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010058 size_t inline_infos_start_index;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000059 BitVector* live_dex_registers_mask;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010060 };
61
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010062 struct InlineInfoEntry {
63 uint32_t method_index;
64 };
65
66 void AddStackMapEntry(uint32_t dex_pc,
Nicolas Geoffray39468442014-09-02 15:17:15 +010067 uint32_t native_pc_offset,
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010068 uint32_t register_mask,
69 BitVector* sp_mask,
70 uint32_t num_dex_registers,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000071 uint8_t inlining_depth) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010072 StackMapEntry entry;
73 entry.dex_pc = dex_pc;
Nicolas Geoffray39468442014-09-02 15:17:15 +010074 entry.native_pc_offset = native_pc_offset;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010075 entry.register_mask = register_mask;
76 entry.sp_mask = sp_mask;
77 entry.num_dex_registers = num_dex_registers;
78 entry.inlining_depth = inlining_depth;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000079 entry.dex_register_locations_start_index = dex_register_locations_.Size();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010080 entry.inline_infos_start_index = inline_infos_.Size();
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000081 if (num_dex_registers != 0) {
82 entry.live_dex_registers_mask =
83 new (allocator_) ArenaBitVector(allocator_, num_dex_registers, true);
84 } else {
85 entry.live_dex_registers_mask = nullptr;
86 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010087 stack_maps_.Add(entry);
88
Nicolas Geoffray39468442014-09-02 15:17:15 +010089 if (sp_mask != nullptr) {
90 stack_mask_max_ = std::max(stack_mask_max_, sp_mask->GetHighestBitSet());
91 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010092 if (inlining_depth > 0) {
93 number_of_stack_maps_with_inline_info_++;
94 }
95 }
96
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010097 void AddInlineInfoEntry(uint32_t method_index) {
98 InlineInfoEntry entry;
99 entry.method_index = method_index;
100 inline_infos_.Add(entry);
101 }
102
103 size_t ComputeNeededSize() const {
Roland Levillainede7bf82015-03-13 12:23:04 +0000104 size_t size = CodeInfo::kFixedSize
Roland Levillain29ba1b02015-03-13 11:45:07 +0000105 + ComputeStackMapsSize()
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000106 + ComputeDexRegisterMapsSize()
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100107 + ComputeInlineInfoSize();
Roland Levillainede7bf82015-03-13 12:23:04 +0000108 // On ARM, CodeInfo data must be 4-byte aligned.
109 return RoundUp(size, kWordAlignment);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100110 }
111
Roland Levillain29ba1b02015-03-13 11:45:07 +0000112 size_t ComputeStackMaskSize() const {
113 return StackMaskEncodingSize(stack_mask_max_);
114 }
115
116 size_t ComputeStackMapsSize() const {
Roland Levillainede7bf82015-03-13 12:23:04 +0000117 return stack_maps_.Size() * StackMap::ComputeStackMapSize(ComputeStackMaskSize());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100118 }
119
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000120 // Compute the size of the Dex register map of `entry`.
121 size_t ComputeDexRegisterMapSize(const StackMapEntry& entry) const {
122 size_t size = DexRegisterMap::kFixedSize;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000123 // Add the bit mask for the dex register liveness.
124 size += DexRegisterMap::LiveBitMaskSize(entry.num_dex_registers);
125 for (size_t dex_register_number = 0, index_in_dex_register_locations = 0;
126 dex_register_number < entry.num_dex_registers;
127 ++dex_register_number) {
128 if (entry.live_dex_registers_mask->IsBitSet(dex_register_number)) {
129 DexRegisterLocation dex_register_location = dex_register_locations_.Get(
130 entry.dex_register_locations_start_index + index_in_dex_register_locations);
131 size += DexRegisterMap::EntrySize(dex_register_location);
132 index_in_dex_register_locations++;
133 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000134 }
135 return size;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100136 }
137
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000138 // Compute the size of all the Dex register maps.
139 size_t ComputeDexRegisterMapsSize() const {
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000140 size_t size = 0;
141 for (size_t i = 0; i < stack_maps_.Size(); ++i) {
142 size += ComputeDexRegisterMapSize(stack_maps_.Get(i));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000143 }
Roland Levillainede7bf82015-03-13 12:23:04 +0000144 return size;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000145 }
146
147 // Compute the size of all the inline information pieces.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100148 size_t ComputeInlineInfoSize() const {
149 return inline_infos_.Size() * InlineInfo::SingleEntrySize()
150 // For encoding the depth.
151 + (number_of_stack_maps_with_inline_info_ * InlineInfo::kFixedSize);
152 }
153
Roland Levillain9ac0e4d2015-03-12 18:33:05 +0000154 size_t ComputeDexRegisterMapsStart() const {
Roland Levillain29ba1b02015-03-13 11:45:07 +0000155 return CodeInfo::kFixedSize + ComputeStackMapsSize();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100156 }
157
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000158 size_t ComputeInlineInfoStart() const {
Roland Levillain9ac0e4d2015-03-12 18:33:05 +0000159 return ComputeDexRegisterMapsStart() + ComputeDexRegisterMapsSize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000160 }
161
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100162 void FillIn(MemoryRegion region) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100163 CodeInfo code_info(region);
Roland Levillain29ba1b02015-03-13 11:45:07 +0000164 DCHECK_EQ(region.size(), ComputeNeededSize());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100165 code_info.SetOverallSize(region.size());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100166
Roland Levillain29ba1b02015-03-13 11:45:07 +0000167 size_t stack_mask_size = ComputeStackMaskSize();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100168 uint8_t* memory_start = region.start();
169
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000170 MemoryRegion dex_register_locations_region = region.Subregion(
Roland Levillain9ac0e4d2015-03-12 18:33:05 +0000171 ComputeDexRegisterMapsStart(),
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000172 ComputeDexRegisterMapsSize());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100173
174 MemoryRegion inline_infos_region = region.Subregion(
175 ComputeInlineInfoStart(),
176 ComputeInlineInfoSize());
177
178 code_info.SetNumberOfStackMaps(stack_maps_.Size());
179 code_info.SetStackMaskSize(stack_mask_size);
Roland Levillain29ba1b02015-03-13 11:45:07 +0000180 DCHECK_EQ(code_info.StackMapsSize(), ComputeStackMapsSize());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100181
182 uintptr_t next_dex_register_map_offset = 0;
183 uintptr_t next_inline_info_offset = 0;
184 for (size_t i = 0, e = stack_maps_.Size(); i < e; ++i) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100185 StackMap stack_map = code_info.GetStackMapAt(i);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100186 StackMapEntry entry = stack_maps_.Get(i);
187
188 stack_map.SetDexPc(entry.dex_pc);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100189 stack_map.SetNativePcOffset(entry.native_pc_offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100190 stack_map.SetRegisterMask(entry.register_mask);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100191 if (entry.sp_mask != nullptr) {
192 stack_map.SetStackMask(*entry.sp_mask);
193 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100194
Roland Levillain442b46a2015-02-18 16:54:21 +0000195 if (entry.num_dex_registers != 0) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000196 // Set the Dex register map.
197 MemoryRegion register_region =
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000198 dex_register_locations_region.Subregion(
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000199 next_dex_register_map_offset,
200 ComputeDexRegisterMapSize(entry));
Roland Levillain442b46a2015-02-18 16:54:21 +0000201 next_dex_register_map_offset += register_region.size();
202 DexRegisterMap dex_register_map(register_region);
203 stack_map.SetDexRegisterMapOffset(register_region.start() - memory_start);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100204
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000205 // Offset in `dex_register_map` where to store the next register entry.
206 size_t offset = DexRegisterMap::kFixedSize;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000207 dex_register_map.SetLiveBitMask(offset,
208 entry.num_dex_registers,
209 *entry.live_dex_registers_mask);
210 offset += DexRegisterMap::LiveBitMaskSize(entry.num_dex_registers);
211 for (size_t dex_register_number = 0, index_in_dex_register_locations = 0;
212 dex_register_number < entry.num_dex_registers;
213 ++dex_register_number) {
214 if (entry.live_dex_registers_mask->IsBitSet(dex_register_number)) {
215 DexRegisterLocation dex_register_location = dex_register_locations_.Get(
216 entry.dex_register_locations_start_index + index_in_dex_register_locations);
217 dex_register_map.SetRegisterInfo(offset, dex_register_location);
218 offset += DexRegisterMap::EntrySize(dex_register_location);
219 ++index_in_dex_register_locations;
220 }
Roland Levillain442b46a2015-02-18 16:54:21 +0000221 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000222 // Ensure we reached the end of the Dex registers region.
223 DCHECK_EQ(offset, register_region.size());
Roland Levillain442b46a2015-02-18 16:54:21 +0000224 } else {
225 stack_map.SetDexRegisterMapOffset(StackMap::kNoDexRegisterMap);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100226 }
227
228 // Set the inlining info.
229 if (entry.inlining_depth != 0) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800230 MemoryRegion inline_region = inline_infos_region.Subregion(
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100231 next_inline_info_offset,
232 InlineInfo::kFixedSize + entry.inlining_depth * InlineInfo::SingleEntrySize());
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800233 next_inline_info_offset += inline_region.size();
234 InlineInfo inline_info(inline_region);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100235
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800236 stack_map.SetInlineDescriptorOffset(inline_region.start() - memory_start);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100237
238 inline_info.SetDepth(entry.inlining_depth);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800239 for (size_t j = 0; j < entry.inlining_depth; ++j) {
240 InlineInfoEntry inline_entry = inline_infos_.Get(j + entry.inline_infos_start_index);
241 inline_info.SetMethodReferenceIndexAtDepth(j, inline_entry.method_index);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100242 }
243 } else {
Roland Levillain442b46a2015-02-18 16:54:21 +0000244 stack_map.SetInlineDescriptorOffset(StackMap::kNoInlineInfo);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100245 }
246 }
247 }
248
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000249 void AddDexRegisterEntry(uint16_t dex_register, DexRegisterLocation::Kind kind, int32_t value) {
250 if (kind != DexRegisterLocation::Kind::kNone) {
251 // Ensure we only use non-compressed location kind at this stage.
252 DCHECK(DexRegisterLocation::IsShortLocationKind(kind))
253 << DexRegisterLocation::PrettyDescriptor(kind);
254 dex_register_locations_.Add(DexRegisterLocation(kind, value));
255 stack_maps_.Get(stack_maps_.Size() - 1).live_dex_registers_mask->SetBit(dex_register);
256 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000257 }
258
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000259 private:
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000260 ArenaAllocator* allocator_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100261 GrowableArray<StackMapEntry> stack_maps_;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000262 GrowableArray<DexRegisterLocation> dex_register_locations_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100263 GrowableArray<InlineInfoEntry> inline_infos_;
264 int stack_mask_max_;
265 size_t number_of_stack_maps_with_inline_info_;
266
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000267 ART_FRIEND_TEST(StackMapTest, Test1);
268 ART_FRIEND_TEST(StackMapTest, Test2);
269 ART_FRIEND_TEST(StackMapTest, TestNonLiveDexRegisters);
270
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100271 DISALLOW_COPY_AND_ASSIGN(StackMapStream);
272};
273
274} // namespace art
275
276#endif // ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_