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 
20 #include <set>
21 #include <unordered_map>
22 #include <unordered_set>
23 
24 #include "base/bit_vector.h"
25 #include "dex/dex_file.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 
37 namespace mirror {
38 class ClassLoader;
39 }  // namespace mirror
40 
41 namespace optimizer {
42 
43 class DexToDexCompiler {
44  public:
45   enum class CompilationLevel {
46     kDontDexToDexCompile,   // Only meaning wrt image time interpretation.
47     kOptimize               // Perform peep-hole optimizations.
48   };
49 
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 
64   void ClearState();
65 
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 
GetDriver()70   CompilerDriver* GetDriver() {
71     return driver_;
72   }
73 
74   bool ShouldCompileMethod(const MethodReference& ref);
75 
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);
80 
81  private:
82   // Holds the state for compiling a single method.
83   struct CompilationState;
84 
85   // Quicken state for a code item, may be referenced by multiple methods.
86   struct QuickenState {
87     std::vector<MethodReference> methods_;
88     std::vector<uint8_t> quicken_data_;
89     bool optimized_return_void_ = false;
90     bool conflict_ = false;
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_;
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_;
108   // Blacklisted code items are unquickened in UnquickenConflictingMethods.
109   std::unordered_map<const DexFile::CodeItem*, QuickenState> shared_code_item_quicken_info_
110       GUARDED_BY(lock_);
111   // Number of added code items.
112   size_t num_code_items_ GUARDED_BY(lock_) = 0u;
113 };
114 
115 std::ostream& operator<<(std::ostream& os, const DexToDexCompiler::CompilationLevel& rhs);
116 
117 }  // namespace optimizer
118 
119 }  // namespace art
120 
121 #endif  // ART_COMPILER_DEX_DEX_TO_DEX_COMPILER_H_
122