| Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 20 | #include "base/allocator.h" |
| 21 | #include "base/arena_bit_vector.h" |
| 22 | #include "base/bit_table.h" |
| Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 23 | #include "base/bit_vector-inl.h" |
| David Sehr | 1ce2b3b | 2018-04-05 11:02:03 -0700 | [diff] [blame] | 24 | #include "base/memory_region.h" |
| Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 25 | #include "base/scoped_arena_containers.h" |
| Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 26 | #include "base/value_object.h" |
| David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 27 | #include "dex_register_location.h" |
| Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 28 | #include "nodes.h" |
| David Srbecky | 50fac06 | 2018-06-13 18:55:35 +0100 | [diff] [blame] | 29 | #include "stack_map.h" |
| Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 30 | |
| Vladimir Marko | 0a51605 | 2019-10-14 13:00:44 +0000 | [diff] [blame] | 31 | namespace art { |
| Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 32 | |
| Santiago Aboy Solanes | 970ba21 | 2021-10-21 10:52:47 +0100 | [diff] [blame] | 33 | class CodeGenerator; |
| 34 | |
| Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 35 | /** |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 36 | * Collects and builds stack maps for a method. All the stack maps |
| 37 | * for a method are placed in a CodeInfo object. |
| Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 38 | */ |
| Vladimir Marko | ced0483 | 2018-07-26 14:42:17 +0100 | [diff] [blame] | 39 | class StackMapStream : public DeletableArenaObject<kArenaAllocStackMapStream> { |
| Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 40 | public: |
| Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 41 | explicit StackMapStream(ScopedArenaAllocator* allocator, InstructionSet instruction_set) |
| David Srbecky | e7a9194 | 2018-08-01 17:23:53 +0100 | [diff] [blame] | 42 | : allocator_(allocator), |
| 43 | instruction_set_(instruction_set), |
| David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 44 | stack_maps_(allocator), |
| 45 | register_masks_(allocator), |
| 46 | stack_masks_(allocator), |
| David Srbecky | 697c47a | 2019-06-16 21:53:07 +0100 | [diff] [blame] | 47 | inline_infos_(allocator), |
| 48 | method_infos_(allocator), |
| David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 49 | dex_register_masks_(allocator), |
| 50 | dex_register_maps_(allocator), |
| 51 | dex_register_catalog_(allocator), |
| David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 52 | lazy_stack_masks_(allocator->Adapter(kArenaAllocStackMapStream)), |
| David Srbecky | f325e28 | 2018-06-13 15:02:32 +0100 | [diff] [blame] | 53 | current_stack_map_(), |
| David Srbecky | 6eb4d5e | 2018-06-03 12:00:20 +0100 | [diff] [blame] | 54 | current_inline_infos_(allocator->Adapter(kArenaAllocStackMapStream)), |
| David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 55 | current_dex_registers_(allocator->Adapter(kArenaAllocStackMapStream)), |
| David Srbecky | 6de8833 | 2018-06-03 12:00:11 +0100 | [diff] [blame] | 56 | previous_dex_registers_(allocator->Adapter(kArenaAllocStackMapStream)), |
| 57 | dex_register_timestamp_(allocator->Adapter(kArenaAllocStackMapStream)), |
| Vladimir Marko | ced0483 | 2018-07-26 14:42:17 +0100 | [diff] [blame] | 58 | expected_num_dex_registers_(0u), |
| David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 59 | temp_dex_register_mask_(allocator, 32, true, kArenaAllocStackMapStream), |
| 60 | temp_dex_register_map_(allocator->Adapter(kArenaAllocStackMapStream)) { |
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 61 | } |
| Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 62 | |
| David Srbecky | f6ba5b3 | 2018-06-23 22:05:49 +0100 | [diff] [blame] | 63 | void BeginMethod(size_t frame_size_in_bytes, |
| 64 | size_t core_spill_mask, |
| 65 | size_t fp_spill_mask, |
| Nicolas Geoffray | a59af8a | 2019-11-27 17:42:32 +0000 | [diff] [blame] | 66 | uint32_t num_dex_registers, |
| 67 | bool baseline = false); |
| David Srbecky | 17b4d2b | 2021-03-02 18:14:31 +0000 | [diff] [blame] | 68 | void EndMethod(size_t code_size); |
| David Srbecky | f6ba5b3 | 2018-06-23 22:05:49 +0100 | [diff] [blame] | 69 | |
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 70 | void BeginStackMapEntry(uint32_t dex_pc, |
| 71 | uint32_t native_pc_offset, |
| David Srbecky | f6ba5b3 | 2018-06-23 22:05:49 +0100 | [diff] [blame] | 72 | uint32_t register_mask = 0, |
| 73 | BitVector* sp_mask = nullptr, |
| Artem Serov | 2808be8 | 2018-12-20 19:15:11 +0000 | [diff] [blame] | 74 | StackMap::Kind kind = StackMap::Kind::Default, |
| 75 | bool needs_vreg_info = true); |
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 76 | void EndStackMapEntry(); |
| Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 77 | |
| David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 78 | void AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value) { |
| 79 | current_dex_registers_.push_back(DexRegisterLocation(kind, value)); |
| 80 | } |
| Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 81 | |
| Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 82 | void BeginInlineInfoEntry(ArtMethod* method, |
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 83 | uint32_t dex_pc, |
| Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 84 | uint32_t num_dex_registers, |
| Santiago Aboy Solanes | 970ba21 | 2021-10-21 10:52:47 +0100 | [diff] [blame] | 85 | const DexFile* outer_dex_file = nullptr, |
| 86 | const CodeGenerator* codegen = nullptr); |
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 87 | void EndInlineInfoEntry(); |
| Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 88 | |
| Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 89 | size_t GetNumberOfStackMaps() const { |
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 90 | return stack_maps_.size(); |
| Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| David Srbecky | d02b23f | 2018-05-29 23:27:22 +0100 | [diff] [blame] | 93 | uint32_t GetStackMapNativePcOffset(size_t i); |
| 94 | void SetStackMapNativePcOffset(size_t i, uint32_t native_pc_offset); |
| Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 95 | |
| David Srbecky | e7a9194 | 2018-08-01 17:23:53 +0100 | [diff] [blame] | 96 | // Encode all stack map data. |
| 97 | // The returned vector is allocated using the allocator passed to the StackMapStream. |
| 98 | ScopedArenaVector<uint8_t> Encode(); |
| Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 99 | |
| Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 100 | private: |
| David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 101 | static constexpr uint32_t kNoValue = -1; |
| Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 102 | |
| David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 103 | void CreateDexRegisterMap(); |
| Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 104 | |
| David Srbecky | 697c47a | 2019-06-16 21:53:07 +0100 | [diff] [blame] | 105 | // Invokes the callback with pointer of each BitTableBuilder field. |
| 106 | template<typename Callback> |
| 107 | void ForEachBitTable(Callback callback) { |
| 108 | size_t index = 0; |
| 109 | callback(index++, &stack_maps_); |
| 110 | callback(index++, ®ister_masks_); |
| 111 | callback(index++, &stack_masks_); |
| 112 | callback(index++, &inline_infos_); |
| 113 | callback(index++, &method_infos_); |
| 114 | callback(index++, &dex_register_masks_); |
| 115 | callback(index++, &dex_register_maps_); |
| 116 | callback(index++, &dex_register_catalog_); |
| 117 | CHECK_EQ(index, CodeInfo::kNumBitTables); |
| 118 | } |
| 119 | |
| David Srbecky | e7a9194 | 2018-08-01 17:23:53 +0100 | [diff] [blame] | 120 | ScopedArenaAllocator* allocator_; |
| Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 121 | const InstructionSet instruction_set_; |
| David Srbecky | 17b4d2b | 2021-03-02 18:14:31 +0000 | [diff] [blame] | 122 | uint32_t code_size_ = 0; |
| David Srbecky | 3aaaa21 | 2018-07-30 16:46:53 +0100 | [diff] [blame] | 123 | uint32_t packed_frame_size_ = 0; |
| David Srbecky | f6ba5b3 | 2018-06-23 22:05:49 +0100 | [diff] [blame] | 124 | uint32_t core_spill_mask_ = 0; |
| 125 | uint32_t fp_spill_mask_ = 0; |
| 126 | uint32_t num_dex_registers_ = 0; |
| Nicolas Geoffray | a59af8a | 2019-11-27 17:42:32 +0000 | [diff] [blame] | 127 | bool baseline_; |
| David Srbecky | cf7833e | 2018-06-14 16:45:22 +0100 | [diff] [blame] | 128 | BitTableBuilder<StackMap> stack_maps_; |
| 129 | BitTableBuilder<RegisterMask> register_masks_; |
| David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 130 | BitmapTableBuilder stack_masks_; |
| David Srbecky | 697c47a | 2019-06-16 21:53:07 +0100 | [diff] [blame] | 131 | BitTableBuilder<InlineInfo> inline_infos_; |
| 132 | BitTableBuilder<MethodInfo> method_infos_; |
| David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 133 | BitmapTableBuilder dex_register_masks_; |
| David Srbecky | 42deda8 | 2018-08-10 11:23:27 +0100 | [diff] [blame] | 134 | BitTableBuilder<DexRegisterMapInfo> dex_register_maps_; |
| David Srbecky | cf7833e | 2018-06-14 16:45:22 +0100 | [diff] [blame] | 135 | BitTableBuilder<DexRegisterInfo> dex_register_catalog_; |
| Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 136 | |
| David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 137 | ScopedArenaVector<BitVector*> lazy_stack_masks_; |
| 138 | |
| 139 | // Variables which track the current state between Begin/End calls; |
| David Srbecky | f6ba5b3 | 2018-06-23 22:05:49 +0100 | [diff] [blame] | 140 | bool in_method_ = false; |
| 141 | bool in_stack_map_ = false; |
| 142 | bool in_inline_info_ = false; |
| David Srbecky | cf7833e | 2018-06-14 16:45:22 +0100 | [diff] [blame] | 143 | BitTableBuilder<StackMap>::Entry current_stack_map_; |
| 144 | ScopedArenaVector<BitTableBuilder<InlineInfo>::Entry> current_inline_infos_; |
| David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 145 | ScopedArenaVector<DexRegisterLocation> current_dex_registers_; |
| David Srbecky | 6de8833 | 2018-06-03 12:00:11 +0100 | [diff] [blame] | 146 | ScopedArenaVector<DexRegisterLocation> previous_dex_registers_; |
| 147 | ScopedArenaVector<uint32_t> dex_register_timestamp_; // Stack map index of last change. |
| David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 148 | size_t expected_num_dex_registers_; |
| 149 | |
| 150 | // Temporary variables used in CreateDexRegisterMap. |
| 151 | // They are here so that we can reuse the reserved memory. |
| 152 | ArenaBitVector temp_dex_register_mask_; |
| David Srbecky | cf7833e | 2018-06-14 16:45:22 +0100 | [diff] [blame] | 153 | ScopedArenaVector<BitTableBuilder<DexRegisterMapInfo>::Entry> temp_dex_register_map_; |
| David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 154 | |
| David Srbecky | 049d681 | 2018-05-18 14:46:49 +0100 | [diff] [blame] | 155 | // A set of lambda functions to be executed at the end to verify |
| 156 | // the encoded data. It is generally only used in debug builds. |
| 157 | std::vector<std::function<void(CodeInfo&)>> dchecks_; |
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 158 | |
| Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 159 | DISALLOW_COPY_AND_ASSIGN(StackMapStream); |
| 160 | }; |
| 161 | |
| 162 | } // namespace art |
| 163 | |
| 164 | #endif // ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_ |