Nicolas Geoffray | dbc3387 | 2023-06-12 13:01:02 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2023 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_CODE_GENERATION_DATA_H_ |
| 18 | #define ART_COMPILER_OPTIMIZING_CODE_GENERATION_DATA_H_ |
| 19 | |
| 20 | #include "arch/instruction_set.h" |
| 21 | #include "base/scoped_arena_allocator.h" |
| 22 | #include "base/scoped_arena_containers.h" |
| 23 | #include "dex/string_reference.h" |
| 24 | #include "dex/type_reference.h" |
| 25 | #include "handle.h" |
| 26 | #include "mirror/class.h" |
| 27 | #include "mirror/object.h" |
| 28 | #include "mirror/string.h" |
| 29 | #include "stack_map_stream.h" |
| 30 | |
| 31 | namespace art HIDDEN { |
| 32 | |
| 33 | class SlowPathCode; |
| 34 | |
| 35 | class CodeGenerationData : public DeletableArenaObject<kArenaAllocCodeGenerator> { |
| 36 | public: |
| 37 | static std::unique_ptr<CodeGenerationData> Create(ArenaStack* arena_stack, |
| 38 | InstructionSet instruction_set) { |
| 39 | ScopedArenaAllocator allocator(arena_stack); |
| 40 | void* memory = allocator.Alloc<CodeGenerationData>(kArenaAllocCodeGenerator); |
| 41 | return std::unique_ptr<CodeGenerationData>( |
| 42 | ::new (memory) CodeGenerationData(std::move(allocator), instruction_set)); |
| 43 | } |
| 44 | |
| 45 | ScopedArenaAllocator* GetScopedAllocator() { |
| 46 | return &allocator_; |
| 47 | } |
| 48 | |
| 49 | void AddSlowPath(SlowPathCode* slow_path) { |
| 50 | slow_paths_.emplace_back(std::unique_ptr<SlowPathCode>(slow_path)); |
| 51 | } |
| 52 | |
| 53 | ArrayRef<const std::unique_ptr<SlowPathCode>> GetSlowPaths() const { |
| 54 | return ArrayRef<const std::unique_ptr<SlowPathCode>>(slow_paths_); |
| 55 | } |
| 56 | |
| 57 | StackMapStream* GetStackMapStream() { return &stack_map_stream_; } |
| 58 | |
| 59 | void ReserveJitStringRoot(StringReference string_reference, Handle<mirror::String> string) { |
| 60 | jit_string_roots_.Overwrite(string_reference, |
| 61 | reinterpret_cast64<uint64_t>(string.GetReference())); |
| 62 | } |
| 63 | |
| 64 | uint64_t GetJitStringRootIndex(StringReference string_reference) const { |
| 65 | return jit_string_roots_.Get(string_reference); |
| 66 | } |
| 67 | |
| 68 | size_t GetNumberOfJitStringRoots() const { |
| 69 | return jit_string_roots_.size(); |
| 70 | } |
| 71 | |
| 72 | void ReserveJitClassRoot(TypeReference type_reference, Handle<mirror::Class> klass) { |
| 73 | jit_class_roots_.Overwrite(type_reference, reinterpret_cast64<uint64_t>(klass.GetReference())); |
| 74 | } |
| 75 | |
| 76 | uint64_t GetJitClassRootIndex(TypeReference type_reference) const { |
| 77 | return jit_class_roots_.Get(type_reference); |
| 78 | } |
| 79 | |
| 80 | size_t GetNumberOfJitClassRoots() const { |
| 81 | return jit_class_roots_.size(); |
| 82 | } |
| 83 | |
| 84 | size_t GetNumberOfJitRoots() const { |
| 85 | return GetNumberOfJitStringRoots() + GetNumberOfJitClassRoots(); |
| 86 | } |
| 87 | |
| 88 | void EmitJitRoots(/*out*/std::vector<Handle<mirror::Object>>* roots) |
| 89 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 90 | |
| 91 | private: |
| 92 | CodeGenerationData(ScopedArenaAllocator&& allocator, InstructionSet instruction_set) |
| 93 | : allocator_(std::move(allocator)), |
| 94 | stack_map_stream_(&allocator_, instruction_set), |
| 95 | slow_paths_(allocator_.Adapter(kArenaAllocCodeGenerator)), |
| 96 | jit_string_roots_(StringReferenceValueComparator(), |
| 97 | allocator_.Adapter(kArenaAllocCodeGenerator)), |
| 98 | jit_class_roots_(TypeReferenceValueComparator(), |
| 99 | allocator_.Adapter(kArenaAllocCodeGenerator)) { |
| 100 | slow_paths_.reserve(kDefaultSlowPathsCapacity); |
| 101 | } |
| 102 | |
| 103 | static constexpr size_t kDefaultSlowPathsCapacity = 8; |
| 104 | |
| 105 | ScopedArenaAllocator allocator_; |
| 106 | StackMapStream stack_map_stream_; |
| 107 | ScopedArenaVector<std::unique_ptr<SlowPathCode>> slow_paths_; |
| 108 | |
| 109 | // Maps a StringReference (dex_file, string_index) to the index in the literal table. |
| 110 | // Entries are initially added with a pointer in the handle zone, and `EmitJitRoots` |
| 111 | // will compute all the indices. |
| 112 | ScopedArenaSafeMap<StringReference, uint64_t, StringReferenceValueComparator> jit_string_roots_; |
| 113 | |
| 114 | // Maps a ClassReference (dex_file, type_index) to the index in the literal table. |
| 115 | // Entries are initially added with a pointer in the handle zone, and `EmitJitRoots` |
| 116 | // will compute all the indices. |
| 117 | ScopedArenaSafeMap<TypeReference, uint64_t, TypeReferenceValueComparator> jit_class_roots_; |
| 118 | }; |
| 119 | |
| 120 | } // namespace art |
| 121 | |
| 122 | #endif // ART_COMPILER_OPTIMIZING_CODE_GENERATION_DATA_H_ |