blob: 79bebd2e6421666586ac6148bef4152fe86d1c29 [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 }
132 return size;
133 }
134
135 // Compute the size of all the inline information pieces.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100136 size_t ComputeInlineInfoSize() const {
137 return inline_infos_.Size() * InlineInfo::SingleEntrySize()
138 // For encoding the depth.
139 + (number_of_stack_maps_with_inline_info_ * InlineInfo::kFixedSize);
140 }
141
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100142 size_t ComputeDexRegisterMapStart() const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100143 return CodeInfo::kFixedSize + ComputeStackMapSize();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100144 }
145
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000146 size_t ComputeInlineInfoStart() const {
147 return ComputeDexRegisterMapStart() + ComputeDexRegisterMapsSize();
148 }
149
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100150 void FillIn(MemoryRegion region) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100151 CodeInfo code_info(region);
152 code_info.SetOverallSize(region.size());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100153
154 size_t stack_mask_size = StackMaskEncodingSize(stack_mask_max_);
155 uint8_t* memory_start = region.start();
156
157 MemoryRegion dex_register_maps_region = region.Subregion(
158 ComputeDexRegisterMapStart(),
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000159 ComputeDexRegisterMapsSize());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100160
161 MemoryRegion inline_infos_region = region.Subregion(
162 ComputeInlineInfoStart(),
163 ComputeInlineInfoSize());
164
165 code_info.SetNumberOfStackMaps(stack_maps_.Size());
166 code_info.SetStackMaskSize(stack_mask_size);
167
168 uintptr_t next_dex_register_map_offset = 0;
169 uintptr_t next_inline_info_offset = 0;
170 for (size_t i = 0, e = stack_maps_.Size(); i < e; ++i) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100171 StackMap stack_map = code_info.GetStackMapAt(i);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100172 StackMapEntry entry = stack_maps_.Get(i);
173
174 stack_map.SetDexPc(entry.dex_pc);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100175 stack_map.SetNativePcOffset(entry.native_pc_offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100176 stack_map.SetRegisterMask(entry.register_mask);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100177 if (entry.sp_mask != nullptr) {
178 stack_map.SetStackMask(*entry.sp_mask);
179 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100180
Roland Levillain442b46a2015-02-18 16:54:21 +0000181 if (entry.num_dex_registers != 0) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000182 // Set the Dex register map.
183 MemoryRegion register_region =
184 dex_register_maps_region.Subregion(
185 next_dex_register_map_offset,
186 ComputeDexRegisterMapSize(entry));
Roland Levillain442b46a2015-02-18 16:54:21 +0000187 next_dex_register_map_offset += register_region.size();
188 DexRegisterMap dex_register_map(register_region);
189 stack_map.SetDexRegisterMapOffset(register_region.start() - memory_start);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100190
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000191 // Offset in `dex_register_map` where to store the next register entry.
192 size_t offset = DexRegisterMap::kFixedSize;
Roland Levillain442b46a2015-02-18 16:54:21 +0000193 for (size_t j = 0; j < entry.num_dex_registers; ++j) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000194 DexRegisterLocation dex_register_location =
195 dex_register_maps_.Get(entry.dex_register_maps_start_index + j);
196 dex_register_map.SetRegisterInfo(offset, dex_register_location);
197 offset += DexRegisterMap::EntrySize(dex_register_location);
Roland Levillain442b46a2015-02-18 16:54:21 +0000198 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000199 // Ensure we reached the end of the Dex registers region.
200 DCHECK_EQ(offset, register_region.size());
Roland Levillain442b46a2015-02-18 16:54:21 +0000201 } else {
202 stack_map.SetDexRegisterMapOffset(StackMap::kNoDexRegisterMap);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100203 }
204
205 // Set the inlining info.
206 if (entry.inlining_depth != 0) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800207 MemoryRegion inline_region = inline_infos_region.Subregion(
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100208 next_inline_info_offset,
209 InlineInfo::kFixedSize + entry.inlining_depth * InlineInfo::SingleEntrySize());
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800210 next_inline_info_offset += inline_region.size();
211 InlineInfo inline_info(inline_region);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100212
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800213 stack_map.SetInlineDescriptorOffset(inline_region.start() - memory_start);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100214
215 inline_info.SetDepth(entry.inlining_depth);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800216 for (size_t j = 0; j < entry.inlining_depth; ++j) {
217 InlineInfoEntry inline_entry = inline_infos_.Get(j + entry.inline_infos_start_index);
218 inline_info.SetMethodReferenceIndexAtDepth(j, inline_entry.method_index);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100219 }
220 } else {
Roland Levillain442b46a2015-02-18 16:54:21 +0000221 stack_map.SetInlineDescriptorOffset(StackMap::kNoInlineInfo);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100222 }
223 }
224 }
225
226 private:
227 GrowableArray<StackMapEntry> stack_maps_;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000228 GrowableArray<DexRegisterLocation> dex_register_maps_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100229 GrowableArray<InlineInfoEntry> inline_infos_;
230 int stack_mask_max_;
231 size_t number_of_stack_maps_with_inline_info_;
232
233 DISALLOW_COPY_AND_ASSIGN(StackMapStream);
234};
235
236} // namespace art
237
238#endif // ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_