blob: 76ddbf32041d07c6b1d84a33327b38191c5dc556 [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"
23#include "stack_map.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010024#include "utils/growable_array.h"
25
26namespace art {
27
28/**
Nicolas Geoffray39468442014-09-02 15:17:15 +010029 * Collects and builds stack maps for a method. All the stack maps
30 * for a method are placed in a CodeInfo object.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010031 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010032class StackMapStream : public ValueObject {
33 public:
34 explicit StackMapStream(ArenaAllocator* allocator)
35 : stack_maps_(allocator, 10),
36 dex_register_maps_(allocator, 10 * 4),
37 inline_infos_(allocator, 2),
Andreas Gampe8eddd2a2014-07-28 14:53:22 -070038 stack_mask_max_(-1),
39 number_of_stack_maps_with_inline_info_(0) {}
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010040
41 // Compute bytes needed to encode a mask with the given maximum element.
42 static uint32_t StackMaskEncodingSize(int max_element) {
43 int number_of_bits = max_element + 1; // Need room for max element too.
44 return RoundUp(number_of_bits, kBitsPerByte) / kBitsPerByte;
45 }
46
47 // See runtime/stack_map.h to know what these fields contain.
48 struct StackMapEntry {
49 uint32_t dex_pc;
Nicolas Geoffray39468442014-09-02 15:17:15 +010050 uint32_t native_pc_offset;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010051 uint32_t register_mask;
52 BitVector* sp_mask;
53 uint32_t num_dex_registers;
54 uint8_t inlining_depth;
55 size_t dex_register_maps_start_index;
56 size_t inline_infos_start_index;
57 };
58
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010059 struct InlineInfoEntry {
60 uint32_t method_index;
61 };
62
63 void AddStackMapEntry(uint32_t dex_pc,
Nicolas Geoffray39468442014-09-02 15:17:15 +010064 uint32_t native_pc_offset,
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010065 uint32_t register_mask,
66 BitVector* sp_mask,
67 uint32_t num_dex_registers,
68 uint8_t inlining_depth) {
69 StackMapEntry entry;
70 entry.dex_pc = dex_pc;
Nicolas Geoffray39468442014-09-02 15:17:15 +010071 entry.native_pc_offset = native_pc_offset;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010072 entry.register_mask = register_mask;
73 entry.sp_mask = sp_mask;
74 entry.num_dex_registers = num_dex_registers;
75 entry.inlining_depth = inlining_depth;
76 entry.dex_register_maps_start_index = dex_register_maps_.Size();
77 entry.inline_infos_start_index = inline_infos_.Size();
78 stack_maps_.Add(entry);
79
Nicolas Geoffray39468442014-09-02 15:17:15 +010080 if (sp_mask != nullptr) {
81 stack_mask_max_ = std::max(stack_mask_max_, sp_mask->GetHighestBitSet());
82 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010083 if (inlining_depth > 0) {
84 number_of_stack_maps_with_inline_info_++;
85 }
86 }
87
Roland Levillaina2d8ec62015-03-12 15:25:29 +000088 void AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value) {
89 // Ensure we only use non-compressed location kind at this stage.
90 DCHECK(DexRegisterLocation::IsShortLocationKind(kind))
91 << DexRegisterLocation::PrettyDescriptor(kind);
92 dex_register_maps_.Add(DexRegisterLocation(kind, value));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010093 }
94
95 void AddInlineInfoEntry(uint32_t method_index) {
96 InlineInfoEntry entry;
97 entry.method_index = method_index;
98 inline_infos_.Add(entry);
99 }
100
101 size_t ComputeNeededSize() const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100102 return CodeInfo::kFixedSize
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100103 + ComputeStackMapSize()
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000104 + ComputeDexRegisterMapsSize()
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100105 + ComputeInlineInfoSize();
106 }
107
108 size_t ComputeStackMapSize() const {
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000109 return stack_maps_.Size() * StackMap::ComputeAlignedStackMapSize(stack_mask_max_);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100110 }
111
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000112 // Compute the size of the Dex register map of `entry`.
113 size_t ComputeDexRegisterMapSize(const StackMapEntry& entry) const {
114 size_t size = DexRegisterMap::kFixedSize;
115 for (size_t j = 0; j < entry.num_dex_registers; ++j) {
116 DexRegisterLocation dex_register_location =
117 dex_register_maps_.Get(entry.dex_register_maps_start_index + j);
118 size += DexRegisterMap::EntrySize(dex_register_location);
119 }
120 return size;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100121 }
122
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000123 // Compute the size of all the Dex register maps.
124 size_t ComputeDexRegisterMapsSize() const {
125 size_t size = stack_maps_.Size() * DexRegisterMap::kFixedSize;
126 // The size of each register location depends on the type of
127 // the entry.
128 for (size_t i = 0, e = dex_register_maps_.Size(); i < e; ++i) {
129 DexRegisterLocation entry = dex_register_maps_.Get(i);
130 size += DexRegisterMap::EntrySize(entry);
131 }
Roland Levillain9ac0e4d2015-03-12 18:33:05 +0000132 // On ARM, the Dex register maps must be 4-byte aligned.
133 return RoundUp(size, kWordAlignment);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000134 }
135
136 // Compute the size of all the inline information pieces.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100137 size_t ComputeInlineInfoSize() const {
138 return inline_infos_.Size() * InlineInfo::SingleEntrySize()
139 // For encoding the depth.
140 + (number_of_stack_maps_with_inline_info_ * InlineInfo::kFixedSize);
141 }
142
Roland Levillain9ac0e4d2015-03-12 18:33:05 +0000143 size_t ComputeDexRegisterMapsStart() const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100144 return CodeInfo::kFixedSize + ComputeStackMapSize();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100145 }
146
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000147 size_t ComputeInlineInfoStart() const {
Roland Levillain9ac0e4d2015-03-12 18:33:05 +0000148 return ComputeDexRegisterMapsStart() + ComputeDexRegisterMapsSize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000149 }
150
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100151 void FillIn(MemoryRegion region) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100152 CodeInfo code_info(region);
153 code_info.SetOverallSize(region.size());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100154
155 size_t stack_mask_size = StackMaskEncodingSize(stack_mask_max_);
156 uint8_t* memory_start = region.start();
157
158 MemoryRegion dex_register_maps_region = region.Subregion(
Roland Levillain9ac0e4d2015-03-12 18:33:05 +0000159 ComputeDexRegisterMapsStart(),
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000160 ComputeDexRegisterMapsSize());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100161
162 MemoryRegion inline_infos_region = region.Subregion(
163 ComputeInlineInfoStart(),
164 ComputeInlineInfoSize());
165
166 code_info.SetNumberOfStackMaps(stack_maps_.Size());
167 code_info.SetStackMaskSize(stack_mask_size);
168
169 uintptr_t next_dex_register_map_offset = 0;
170 uintptr_t next_inline_info_offset = 0;
171 for (size_t i = 0, e = stack_maps_.Size(); i < e; ++i) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100172 StackMap stack_map = code_info.GetStackMapAt(i);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100173 StackMapEntry entry = stack_maps_.Get(i);
174
175 stack_map.SetDexPc(entry.dex_pc);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100176 stack_map.SetNativePcOffset(entry.native_pc_offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100177 stack_map.SetRegisterMask(entry.register_mask);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100178 if (entry.sp_mask != nullptr) {
179 stack_map.SetStackMask(*entry.sp_mask);
180 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100181
Roland Levillain442b46a2015-02-18 16:54:21 +0000182 if (entry.num_dex_registers != 0) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000183 // Set the Dex register map.
184 MemoryRegion register_region =
185 dex_register_maps_region.Subregion(
186 next_dex_register_map_offset,
187 ComputeDexRegisterMapSize(entry));
Roland Levillain442b46a2015-02-18 16:54:21 +0000188 next_dex_register_map_offset += register_region.size();
189 DexRegisterMap dex_register_map(register_region);
190 stack_map.SetDexRegisterMapOffset(register_region.start() - memory_start);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100191
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000192 // Offset in `dex_register_map` where to store the next register entry.
193 size_t offset = DexRegisterMap::kFixedSize;
Roland Levillain442b46a2015-02-18 16:54:21 +0000194 for (size_t j = 0; j < entry.num_dex_registers; ++j) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000195 DexRegisterLocation dex_register_location =
196 dex_register_maps_.Get(entry.dex_register_maps_start_index + j);
197 dex_register_map.SetRegisterInfo(offset, dex_register_location);
198 offset += DexRegisterMap::EntrySize(dex_register_location);
Roland Levillain442b46a2015-02-18 16:54:21 +0000199 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000200 // Ensure we reached the end of the Dex registers region.
201 DCHECK_EQ(offset, register_region.size());
Roland Levillain442b46a2015-02-18 16:54:21 +0000202 } else {
203 stack_map.SetDexRegisterMapOffset(StackMap::kNoDexRegisterMap);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100204 }
205
206 // Set the inlining info.
207 if (entry.inlining_depth != 0) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800208 MemoryRegion inline_region = inline_infos_region.Subregion(
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100209 next_inline_info_offset,
210 InlineInfo::kFixedSize + entry.inlining_depth * InlineInfo::SingleEntrySize());
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800211 next_inline_info_offset += inline_region.size();
212 InlineInfo inline_info(inline_region);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100213
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800214 stack_map.SetInlineDescriptorOffset(inline_region.start() - memory_start);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100215
216 inline_info.SetDepth(entry.inlining_depth);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800217 for (size_t j = 0; j < entry.inlining_depth; ++j) {
218 InlineInfoEntry inline_entry = inline_infos_.Get(j + entry.inline_infos_start_index);
219 inline_info.SetMethodReferenceIndexAtDepth(j, inline_entry.method_index);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100220 }
221 } else {
Roland Levillain442b46a2015-02-18 16:54:21 +0000222 stack_map.SetInlineDescriptorOffset(StackMap::kNoInlineInfo);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100223 }
224 }
225 }
226
227 private:
228 GrowableArray<StackMapEntry> stack_maps_;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000229 GrowableArray<DexRegisterLocation> dex_register_maps_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100230 GrowableArray<InlineInfoEntry> inline_infos_;
231 int stack_mask_max_;
232 size_t number_of_stack_maps_with_inline_info_;
233
234 DISALLOW_COPY_AND_ASSIGN(StackMapStream);
235};
236
237} // namespace art
238
239#endif // ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_