| Andreas Gampe | 5eb0d38 | 2015-07-23 01:19:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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_DEX_DEX_TO_DEX_COMPILER_H_ |
| 18 | #define ART_COMPILER_DEX_DEX_TO_DEX_COMPILER_H_ |
| 19 | |
| Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame^] | 20 | #include <set> |
| 21 | #include <unordered_map> |
| 22 | #include <unordered_set> |
| 23 | |
| 24 | #include "base/bit_vector.h" |
| David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 25 | #include "dex/dex_file.h" |
| Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 26 | #include "handle.h" |
| Andreas Gampe | 5eb0d38 | 2015-07-23 01:19:26 -0700 | [diff] [blame] | 27 | #include "invoke_type.h" |
| Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame^] | 28 | #include "method_reference.h" |
| 29 | #include "quicken_info.h" |
| Andreas Gampe | 5eb0d38 | 2015-07-23 01:19:26 -0700 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
| 33 | class CompiledMethod; |
| 34 | class CompilerDriver; |
| Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame^] | 35 | class DexCompilationUnit; |
| Andreas Gampe | 5eb0d38 | 2015-07-23 01:19:26 -0700 | [diff] [blame] | 36 | |
| Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 37 | namespace mirror { |
| 38 | class ClassLoader; |
| 39 | } // namespace mirror |
| 40 | |
| Andreas Gampe | 5eb0d38 | 2015-07-23 01:19:26 -0700 | [diff] [blame] | 41 | namespace optimizer { |
| 42 | |
| Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame^] | 43 | class DexToDexCompiler { |
| 44 | public: |
| 45 | enum class CompilationLevel { |
| 46 | kDontDexToDexCompile, // Only meaning wrt image time interpretation. |
| 47 | kOptimize // Perform peep-hole optimizations. |
| 48 | }; |
| Andreas Gampe | 5eb0d38 | 2015-07-23 01:19:26 -0700 | [diff] [blame] | 49 | |
| Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame^] | 50 | explicit DexToDexCompiler(CompilerDriver* driver); |
| 51 | |
| 52 | CompiledMethod* CompileMethod(const DexFile::CodeItem* code_item, |
| 53 | uint32_t access_flags, |
| 54 | InvokeType invoke_type, |
| 55 | uint16_t class_def_idx, |
| 56 | uint32_t method_idx, |
| 57 | Handle<mirror::ClassLoader> class_loader, |
| 58 | const DexFile& dex_file, |
| 59 | const CompilationLevel compilation_level) WARN_UNUSED; |
| 60 | |
| 61 | void MarkForCompilation(Thread* self, |
| 62 | const MethodReference& method_ref, |
| 63 | const DexFile::CodeItem* code_item); |
| 64 | |
| 65 | void ClearState(); |
| 66 | |
| 67 | CompilerDriver* GetDriver() { |
| 68 | return driver_; |
| 69 | } |
| 70 | |
| 71 | bool ShouldCompileMethod(const MethodReference& ref); |
| 72 | |
| 73 | size_t NumUniqueCodeItems(Thread* self) const; |
| 74 | |
| 75 | private: |
| 76 | // Holds the state for compiling a single method. |
| 77 | struct CompilationState { |
| 78 | struct QuickenedInfo { |
| 79 | QuickenedInfo(uint32_t pc, uint16_t index) : dex_pc(pc), dex_member_index(index) {} |
| 80 | |
| 81 | uint32_t dex_pc; |
| 82 | uint16_t dex_member_index; |
| 83 | }; |
| 84 | |
| 85 | CompilationState(DexToDexCompiler* compiler, |
| 86 | const DexCompilationUnit& unit, |
| 87 | const CompilationLevel compilation_level, |
| 88 | const std::vector<uint8_t>* quicken_data); |
| 89 | |
| 90 | const std::vector<QuickenedInfo>& GetQuickenedInfo() const { |
| 91 | return quickened_info_; |
| 92 | } |
| 93 | |
| 94 | // Returns the quickening info, or an empty array if it was not quickened. |
| 95 | // If already_quickened is true, then don't change anything but still return what the quicken |
| 96 | // data would have been. |
| 97 | std::vector<uint8_t> Compile(); |
| 98 | |
| 99 | const DexFile& GetDexFile() const; |
| 100 | |
| 101 | // Compiles a RETURN-VOID into a RETURN-VOID-BARRIER within a constructor where |
| 102 | // a barrier is required. |
| 103 | void CompileReturnVoid(Instruction* inst, uint32_t dex_pc); |
| 104 | |
| 105 | // Compiles a CHECK-CAST into 2 NOP instructions if it is known to be safe. In |
| 106 | // this case, returns the second NOP instruction pointer. Otherwise, returns |
| 107 | // the given "inst". |
| 108 | Instruction* CompileCheckCast(Instruction* inst, uint32_t dex_pc); |
| 109 | |
| 110 | // Compiles a field access into a quick field access. |
| 111 | // The field index is replaced by an offset within an Object where we can read |
| 112 | // from / write to this field. Therefore, this does not involve any resolution |
| 113 | // at runtime. |
| 114 | // Since the field index is encoded with 16 bits, we can replace it only if the |
| 115 | // field offset can be encoded with 16 bits too. |
| 116 | void CompileInstanceFieldAccess(Instruction* inst, uint32_t dex_pc, |
| 117 | Instruction::Code new_opcode, bool is_put); |
| 118 | |
| 119 | // Compiles a virtual method invocation into a quick virtual method invocation. |
| 120 | // The method index is replaced by the vtable index where the corresponding |
| 121 | // executable can be found. Therefore, this does not involve any resolution |
| 122 | // at runtime. |
| 123 | // Since the method index is encoded with 16 bits, we can replace it only if the |
| 124 | // vtable index can be encoded with 16 bits too. |
| 125 | void CompileInvokeVirtual(Instruction* inst, uint32_t dex_pc, |
| 126 | Instruction::Code new_opcode, bool is_range); |
| 127 | |
| 128 | // Return the next index. |
| 129 | uint16_t NextIndex(); |
| 130 | |
| 131 | // Returns the dequickened index if an instruction is quickened, otherwise return index. |
| 132 | uint16_t GetIndexForInstruction(const Instruction* inst, uint32_t index); |
| 133 | |
| 134 | DexToDexCompiler* const compiler_; |
| 135 | CompilerDriver& driver_; |
| 136 | const DexCompilationUnit& unit_; |
| 137 | const CompilationLevel compilation_level_; |
| 138 | |
| 139 | // Filled by the compiler when quickening, in order to encode that information |
| 140 | // in the .oat file. The runtime will use that information to get to the original |
| 141 | // opcodes. |
| 142 | std::vector<QuickenedInfo> quickened_info_; |
| 143 | |
| 144 | // If the code item was already quickened previously. |
| 145 | const bool already_quickened_; |
| 146 | const QuickenInfoTable existing_quicken_info_; |
| 147 | uint32_t quicken_index_ = 0u; |
| 148 | |
| 149 | DISALLOW_COPY_AND_ASSIGN(CompilationState); |
| 150 | }; |
| 151 | |
| 152 | struct QuickenState { |
| 153 | std::vector<MethodReference> methods_; |
| 154 | std::vector<uint8_t> quicken_data_; |
| 155 | }; |
| 156 | |
| 157 | BitVector* GetOrAddBitVectorForDex(const DexFile* dex_file) REQUIRES(lock_); |
| 158 | |
| 159 | CompilerDriver* const driver_; |
| 160 | |
| 161 | // State for adding methods (should this be in its own class?). |
| 162 | const DexFile* active_dex_file_ = nullptr; |
| 163 | BitVector* active_bit_vector_ = nullptr; |
| 164 | |
| 165 | // Lock that guards duplicate code items and the bitmap. |
| 166 | mutable Mutex lock_; |
| 167 | // Record what method references are going to get quickened. |
| 168 | std::unordered_map<const DexFile*, BitVector> should_quicken_; |
| 169 | // Record what code items are already seen to detect when multiple methods have the same code |
| 170 | // item. |
| 171 | std::unordered_set<const DexFile::CodeItem*> seen_code_items_ GUARDED_BY(lock_); |
| 172 | // Guarded by lock_ during writing, accessed without a lock during quickening. |
| 173 | // This is safe because no thread is adding to the shared code items during the quickening phase. |
| 174 | std::unordered_set<const DexFile::CodeItem*> shared_code_items_; |
| 175 | std::unordered_set<const DexFile::CodeItem*> blacklisted_code_items_ GUARDED_BY(lock_); |
| 176 | std::unordered_map<const DexFile::CodeItem*, QuickenState> shared_code_item_quicken_info_ |
| 177 | GUARDED_BY(lock_); |
| 178 | }; |
| 179 | |
| 180 | std::ostream& operator<<(std::ostream& os, const DexToDexCompiler::CompilationLevel& rhs); |
| Andreas Gampe | 5eb0d38 | 2015-07-23 01:19:26 -0700 | [diff] [blame] | 181 | |
| 182 | } // namespace optimizer |
| 183 | |
| 184 | } // namespace art |
| 185 | |
| 186 | #endif // ART_COMPILER_DEX_DEX_TO_DEX_COMPILER_H_ |