| 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" |
| David Sehr | 8c0961f | 2018-01-23 16:11:38 -0800 | [diff] [blame] | 26 | #include "dex/invoke_type.h" |
| David Sehr | 312f3b2 | 2018-03-19 08:39:26 -0700 | [diff] [blame] | 27 | #include "dex/method_reference.h" |
| Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 28 | #include "handle.h" |
| Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 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, |
| Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 62 | const MethodReference& method_ref); |
| Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 63 | |
| 64 | void ClearState(); |
| 65 | |
| Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 66 | // Unquicken all methods that have conflicting quicken info. This is not done during the |
| 67 | // quickening process to avoid race conditions. |
| 68 | void UnquickenConflictingMethods(); |
| 69 | |
| Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 70 | CompilerDriver* GetDriver() { |
| 71 | return driver_; |
| 72 | } |
| 73 | |
| 74 | bool ShouldCompileMethod(const MethodReference& ref); |
| 75 | |
| Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 76 | // Return the number of code items to quicken. |
| 77 | size_t NumCodeItemsToQuicken(Thread* self) const; |
| 78 | |
| 79 | void SetDexFiles(const std::vector<const DexFile*>& dex_files); |
| Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 80 | |
| 81 | private: |
| 82 | // Holds the state for compiling a single method. |
| Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 83 | struct CompilationState; |
| Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 84 | |
| Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 85 | // Quicken state for a code item, may be referenced by multiple methods. |
| Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 86 | struct QuickenState { |
| 87 | std::vector<MethodReference> methods_; |
| 88 | std::vector<uint8_t> quicken_data_; |
| Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 89 | bool optimized_return_void_ = false; |
| 90 | bool conflict_ = false; |
| Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | BitVector* GetOrAddBitVectorForDex(const DexFile* dex_file) REQUIRES(lock_); |
| 94 | |
| 95 | CompilerDriver* const driver_; |
| 96 | |
| 97 | // State for adding methods (should this be in its own class?). |
| 98 | const DexFile* active_dex_file_ = nullptr; |
| 99 | BitVector* active_bit_vector_ = nullptr; |
| 100 | |
| 101 | // Lock that guards duplicate code items and the bitmap. |
| 102 | mutable Mutex lock_; |
| 103 | // Record what method references are going to get quickened. |
| 104 | std::unordered_map<const DexFile*, BitVector> should_quicken_; |
| Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 105 | // Guarded by lock_ during writing, accessed without a lock during quickening. |
| 106 | // This is safe because no thread is adding to the shared code items during the quickening phase. |
| 107 | std::unordered_set<const DexFile::CodeItem*> shared_code_items_; |
| Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 108 | // Blacklisted code items are unquickened in UnquickenConflictingMethods. |
| Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 109 | std::unordered_map<const DexFile::CodeItem*, QuickenState> shared_code_item_quicken_info_ |
| 110 | GUARDED_BY(lock_); |
| Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 111 | // Number of added code items. |
| 112 | size_t num_code_items_ GUARDED_BY(lock_) = 0u; |
| Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | std::ostream& operator<<(std::ostream& os, const DexToDexCompiler::CompilationLevel& rhs); |
| Andreas Gampe | 5eb0d38 | 2015-07-23 01:19:26 -0700 | [diff] [blame] | 116 | |
| 117 | } // namespace optimizer |
| 118 | |
| 119 | } // namespace art |
| 120 | |
| 121 | #endif // ART_COMPILER_DEX_DEX_TO_DEX_COMPILER_H_ |