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_DEX2OAT_DEX_DEX_TO_DEX_COMPILER_H_ 18 #define ART_DEX2OAT_DEX_DEX_TO_DEX_COMPILER_H_ 19 20 #include <set> 21 #include <unordered_map> 22 #include <unordered_set> 23 24 #include "base/bit_vector.h" 25 #include "base/mutex.h" 26 #include "dex/invoke_type.h" 27 #include "dex/method_reference.h" 28 #include "handle.h" 29 #include "quicken_info.h" 30 31 namespace art { 32 33 class CompiledMethod; 34 class CompilerDriver; 35 class DexCompilationUnit; 36 class DexFile; 37 38 namespace dex { 39 struct CodeItem; 40 } // namespace dex 41 42 namespace mirror { 43 class ClassLoader; 44 } // namespace mirror 45 46 namespace optimizer { 47 48 class DexToDexCompiler { 49 public: 50 enum class CompilationLevel { 51 kDontDexToDexCompile, // Only meaning wrt image time interpretation. 52 kOptimize // Perform peep-hole optimizations. 53 }; 54 55 explicit DexToDexCompiler(CompilerDriver* driver); 56 57 CompiledMethod* CompileMethod(const dex::CodeItem* code_item, 58 uint32_t access_flags, 59 InvokeType invoke_type, 60 uint16_t class_def_idx, 61 uint32_t method_idx, 62 Handle<mirror::ClassLoader> class_loader, 63 const DexFile& dex_file, 64 const CompilationLevel compilation_level) WARN_UNUSED; 65 66 void MarkForCompilation(Thread* self, 67 const MethodReference& method_ref); 68 69 void ClearState(); 70 71 // Unquicken all methods that have conflicting quicken info. This is not done during the 72 // quickening process to avoid race conditions. 73 void UnquickenConflictingMethods(); 74 GetDriver()75 CompilerDriver* GetDriver() { 76 return driver_; 77 } 78 79 bool ShouldCompileMethod(const MethodReference& ref); 80 81 // Return the number of code items to quicken. 82 size_t NumCodeItemsToQuicken(Thread* self) const; 83 84 void SetDexFiles(const std::vector<const DexFile*>& dex_files); 85 86 private: 87 // Holds the state for compiling a single method. 88 struct CompilationState; 89 90 // Quicken state for a code item, may be referenced by multiple methods. 91 struct QuickenState { 92 std::vector<MethodReference> methods_; 93 std::vector<uint8_t> quicken_data_; 94 bool optimized_return_void_ = false; 95 bool conflict_ = false; 96 }; 97 98 BitVector* GetOrAddBitVectorForDex(const DexFile* dex_file) REQUIRES(lock_); 99 100 CompilerDriver* const driver_; 101 102 // State for adding methods (should this be in its own class?). 103 const DexFile* active_dex_file_ = nullptr; 104 BitVector* active_bit_vector_ = nullptr; 105 106 // Lock that guards duplicate code items and the bitmap. 107 mutable Mutex lock_; 108 // Record what method references are going to get quickened. 109 std::unordered_map<const DexFile*, BitVector> should_quicken_; 110 // Guarded by lock_ during writing, accessed without a lock during quickening. 111 // This is safe because no thread is adding to the shared code items during the quickening phase. 112 std::unordered_set<const dex::CodeItem*> shared_code_items_; 113 // Blacklisted code items are unquickened in UnquickenConflictingMethods. 114 std::unordered_map<const dex::CodeItem*, QuickenState> shared_code_item_quicken_info_ 115 GUARDED_BY(lock_); 116 // Number of added code items. 117 size_t num_code_items_ GUARDED_BY(lock_) = 0u; 118 }; 119 120 std::ostream& operator<<(std::ostream& os, const DexToDexCompiler::CompilationLevel& rhs); 121 122 } // namespace optimizer 123 124 } // namespace art 125 126 #endif // ART_DEX2OAT_DEX_DEX_TO_DEX_COMPILER_H_ 127