1 /*
2  * Copyright (C) 2011 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_DRIVER_COMPILER_DRIVER_H_
18 #define ART_DEX2OAT_DRIVER_COMPILER_DRIVER_H_
19 
20 #include <atomic>
21 #include <set>
22 #include <string>
23 #include <vector>
24 
25 #include "arch/instruction_set.h"
26 #include "base/array_ref.h"
27 #include "base/bit_utils.h"
28 #include "base/hash_set.h"
29 #include "base/mutex.h"
30 #include "base/os.h"
31 #include "base/quasi_atomic.h"
32 #include "base/safe_map.h"
33 #include "base/timing_logger.h"
34 #include "class_status.h"
35 #include "compiler.h"
36 #include "dex/class_reference.h"
37 #include "dex/dex_file_types.h"
38 #include "dex/dex_to_dex_compiler.h"
39 #include "dex/method_reference.h"
40 #include "driver/compiled_method_storage.h"
41 #include "thread_pool.h"
42 #include "utils/atomic_dex_ref_map.h"
43 #include "utils/dex_cache_arrays_layout.h"
44 
45 namespace art {
46 
47 namespace dex {
48 struct CodeItem;
49 }  // namespace dex
50 
51 namespace mirror {
52 class Class;
53 class DexCache;
54 }  // namespace mirror
55 
56 namespace verifier {
57 class MethodVerifier;
58 class VerifierDepsTest;
59 }  // namespace verifier
60 
61 class ArtField;
62 class BitVector;
63 class CompiledMethod;
64 class CompilerOptions;
65 class DexCompilationUnit;
66 class DexFile;
67 template<class T> class Handle;
68 struct InlineIGetIPutData;
69 class InstructionSetFeatures;
70 class InternTable;
71 enum InvokeType : uint32_t;
72 class MemberOffset;
73 template<class MirrorType> class ObjPtr;
74 class ParallelCompilationManager;
75 class ProfileCompilationInfo;
76 class ScopedObjectAccess;
77 template <class Allocator> class SrcMap;
78 class TimingLogger;
79 class VdexFile;
80 class VerificationResults;
81 
82 class CompilerDriver {
83  public:
84   // Create a compiler targeting the requested "instruction_set".
85   // "image" should be true if image specific optimizations should be
86   // enabled.  "image_classes" lets the compiler know what classes it
87   // can assume will be in the image, with null implying all available
88   // classes.
89   CompilerDriver(const CompilerOptions* compiler_options,
90                  Compiler::Kind compiler_kind,
91                  size_t thread_count,
92                  int swap_fd);
93 
94   ~CompilerDriver();
95 
96   void PrepareDexFilesForOatFile(TimingLogger* timings);
97 
98   // Set dex files classpath.
99   void SetClasspathDexFiles(const std::vector<const DexFile*>& dex_files);
100 
101   // Initialize and destroy thread pools. This is exposed because we do not want
102   // to do this twice, for PreCompile() and CompileAll().
103   void InitializeThreadPools();
104   void FreeThreadPools();
105 
106   void PreCompile(jobject class_loader,
107                   const std::vector<const DexFile*>& dex_files,
108                   TimingLogger* timings,
109                   /*inout*/ HashSet<std::string>* image_classes,
110                   /*out*/ VerificationResults* verification_results)
111       REQUIRES(!Locks::mutator_lock_);
112   void CompileAll(jobject class_loader,
113                   const std::vector<const DexFile*>& dex_files,
114                   TimingLogger* timings)
115       REQUIRES(!Locks::mutator_lock_);
116 
GetCompilerOptions()117   const CompilerOptions& GetCompilerOptions() const {
118     return *compiler_options_;
119   }
120 
GetCompiler()121   Compiler* GetCompiler() const {
122     return compiler_.get();
123   }
124 
125   // Generate the trampolines that are invoked by unresolved direct methods.
126   std::unique_ptr<const std::vector<uint8_t>> CreateJniDlsymLookupTrampoline() const;
127   std::unique_ptr<const std::vector<uint8_t>> CreateJniDlsymLookupCriticalTrampoline() const;
128   std::unique_ptr<const std::vector<uint8_t>> CreateQuickGenericJniTrampoline() const;
129   std::unique_ptr<const std::vector<uint8_t>> CreateQuickImtConflictTrampoline() const;
130   std::unique_ptr<const std::vector<uint8_t>> CreateQuickResolutionTrampoline() const;
131   std::unique_ptr<const std::vector<uint8_t>> CreateQuickToInterpreterBridge() const;
132 
133   ClassStatus GetClassStatus(const ClassReference& ref) const;
134   bool GetCompiledClass(const ClassReference& ref, ClassStatus* status) const;
135 
136   CompiledMethod* GetCompiledMethod(MethodReference ref) const;
137   // Add a compiled method.
138   void AddCompiledMethod(const MethodReference& method_ref, CompiledMethod* const compiled_method);
139   CompiledMethod* RemoveCompiledMethod(const MethodReference& method_ref);
140 
141   // Resolve compiling method's class. Returns null on failure.
142   ObjPtr<mirror::Class> ResolveCompilingMethodsClass(const ScopedObjectAccess& soa,
143                                                      Handle<mirror::DexCache> dex_cache,
144                                                      Handle<mirror::ClassLoader> class_loader,
145                                                      const DexCompilationUnit* mUnit)
146       REQUIRES_SHARED(Locks::mutator_lock_);
147 
148   ObjPtr<mirror::Class> ResolveClass(const ScopedObjectAccess& soa,
149                                      Handle<mirror::DexCache> dex_cache,
150                                      Handle<mirror::ClassLoader> class_loader,
151                                      dex::TypeIndex type_index,
152                                      const DexCompilationUnit* mUnit)
153       REQUIRES_SHARED(Locks::mutator_lock_);
154 
155   // Resolve a field. Returns null on failure, including incompatible class change.
156   // NOTE: Unlike ClassLinker's ResolveField(), this method enforces is_static.
157   ArtField* ResolveField(const ScopedObjectAccess& soa,
158                          Handle<mirror::DexCache> dex_cache,
159                          Handle<mirror::ClassLoader> class_loader,
160                          uint32_t field_idx,
161                          bool is_static)
162       REQUIRES_SHARED(Locks::mutator_lock_);
163 
164   // Can we fast-path an IGET/IPUT access to an instance field? If yes, compute the field offset.
165   std::pair<bool, bool> IsFastInstanceField(ObjPtr<mirror::DexCache> dex_cache,
166                                             ObjPtr<mirror::Class> referrer_class,
167                                             ArtField* resolved_field,
168                                             uint16_t field_idx)
169       REQUIRES_SHARED(Locks::mutator_lock_);
170 
171   void ProcessedInstanceField(bool resolved);
172   void ProcessedStaticField(bool resolved, bool local);
173 
174   // Can we fast path instance field access? Computes field's offset and volatility.
175   bool ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put,
176                                 MemberOffset* field_offset, bool* is_volatile)
177       REQUIRES(!Locks::mutator_lock_);
178 
179   ArtField* ComputeInstanceFieldInfo(uint32_t field_idx,
180                                      const DexCompilationUnit* mUnit,
181                                      bool is_put,
182                                      const ScopedObjectAccess& soa)
183       REQUIRES_SHARED(Locks::mutator_lock_);
184 
185 
186   bool IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc);
187 
GetThreadCount()188   size_t GetThreadCount() const {
189     return parallel_thread_count_;
190   }
191 
SetDedupeEnabled(bool dedupe_enabled)192   void SetDedupeEnabled(bool dedupe_enabled) {
193     compiled_method_storage_.SetDedupeEnabled(dedupe_enabled);
194   }
195 
DedupeEnabled()196   bool DedupeEnabled() const {
197     return compiled_method_storage_.DedupeEnabled();
198   }
199 
200   // Checks whether profile guided compilation is enabled and if the method should be compiled
201   // according to the profile file.
202   bool ShouldCompileBasedOnProfile(const MethodReference& method_ref) const;
203 
204   // Checks whether profile guided verification is enabled and if the method should be verified
205   // according to the profile file.
206   bool ShouldVerifyClassBasedOnProfile(const DexFile& dex_file, uint16_t class_idx) const;
207 
208   void RecordClassStatus(const ClassReference& ref, ClassStatus status);
209 
210   // Get memory usage during compilation.
211   std::string GetMemoryUsageString(bool extended) const;
212 
SetHadHardVerifierFailure()213   void SetHadHardVerifierFailure() {
214     had_hard_verifier_failure_ = true;
215   }
AddSoftVerifierFailure()216   void AddSoftVerifierFailure() {
217     number_of_soft_verifier_failures_++;
218   }
219 
GetCompilerKind()220   Compiler::Kind GetCompilerKind() {
221     return compiler_kind_;
222   }
223 
GetCompiledMethodStorage()224   CompiledMethodStorage* GetCompiledMethodStorage() {
225     return &compiled_method_storage_;
226   }
227 
GetDexToDexCompiler()228   optimizer::DexToDexCompiler& GetDexToDexCompiler() {
229     return dex_to_dex_compiler_;
230   }
231 
232  private:
233   void LoadImageClasses(TimingLogger* timings, /*inout*/ HashSet<std::string>* image_classes)
234       REQUIRES(!Locks::mutator_lock_);
235 
236   // Attempt to resolve all type, methods, fields, and strings
237   // referenced from code in the dex file following PathClassLoader
238   // ordering semantics.
239   void Resolve(jobject class_loader,
240                const std::vector<const DexFile*>& dex_files,
241                TimingLogger* timings)
242       REQUIRES(!Locks::mutator_lock_);
243   void ResolveDexFile(jobject class_loader,
244                       const DexFile& dex_file,
245                       const std::vector<const DexFile*>& dex_files,
246                       ThreadPool* thread_pool,
247                       size_t thread_count,
248                       TimingLogger* timings)
249       REQUIRES(!Locks::mutator_lock_);
250 
251   // Do fast verification through VerifierDeps if possible. Return whether
252   // verification was successful.
253   bool FastVerify(jobject class_loader,
254                   const std::vector<const DexFile*>& dex_files,
255                   TimingLogger* timings,
256                   /*out*/ VerificationResults* verification_results);
257 
258   void Verify(jobject class_loader,
259               const std::vector<const DexFile*>& dex_files,
260               TimingLogger* timings,
261               /*out*/ VerificationResults* verification_results);
262 
263   void VerifyDexFile(jobject class_loader,
264                      const DexFile& dex_file,
265                      const std::vector<const DexFile*>& dex_files,
266                      ThreadPool* thread_pool,
267                      size_t thread_count,
268                      TimingLogger* timings)
269       REQUIRES(!Locks::mutator_lock_);
270 
271   void SetVerified(jobject class_loader,
272                    const std::vector<const DexFile*>& dex_files,
273                    TimingLogger* timings);
274   void SetVerifiedDexFile(jobject class_loader,
275                           const DexFile& dex_file,
276                           const std::vector<const DexFile*>& dex_files,
277                           ThreadPool* thread_pool,
278                           size_t thread_count,
279                           TimingLogger* timings)
280       REQUIRES(!Locks::mutator_lock_);
281 
282   void InitializeClasses(jobject class_loader,
283                          const std::vector<const DexFile*>& dex_files,
284                          TimingLogger* timings)
285       REQUIRES(!Locks::mutator_lock_);
286   void InitializeClasses(jobject class_loader,
287                          const DexFile& dex_file,
288                          const std::vector<const DexFile*>& dex_files,
289                          TimingLogger* timings)
290       REQUIRES(!Locks::mutator_lock_);
291 
292   void UpdateImageClasses(TimingLogger* timings, /*inout*/ HashSet<std::string>* image_classes)
293       REQUIRES(!Locks::mutator_lock_);
294 
295   void Compile(jobject class_loader,
296                const std::vector<const DexFile*>& dex_files,
297                TimingLogger* timings);
298 
299   void CheckThreadPools();
300 
301   // Resolve const string literals that are loaded from dex code. If only_startup_strings is
302   // specified, only methods that are marked startup in the profile are resolved.
303   void ResolveConstStrings(const std::vector<const DexFile*>& dex_files,
304                            bool only_startup_strings,
305                            /*inout*/ TimingLogger* timings);
306 
307   const CompilerOptions* const compiler_options_;
308 
309   std::unique_ptr<Compiler> compiler_;
310   Compiler::Kind compiler_kind_;
311 
312   // All class references that this compiler has compiled. Indexed by class defs.
313   using ClassStateTable = AtomicDexRefMap<ClassReference, ClassStatus>;
314   ClassStateTable compiled_classes_;
315   // All class references that are in the classpath. Indexed by class defs.
316   ClassStateTable classpath_classes_;
317 
318   typedef AtomicDexRefMap<MethodReference, CompiledMethod*> MethodTable;
319 
320   // All method references that this compiler has compiled.
321   MethodTable compiled_methods_;
322 
323   std::atomic<uint32_t> number_of_soft_verifier_failures_;
324 
325   bool had_hard_verifier_failure_;
326 
327   // A thread pool that can (potentially) run tasks in parallel.
328   size_t parallel_thread_count_;
329   std::unique_ptr<ThreadPool> parallel_thread_pool_;
330 
331   // A thread pool that guarantees running single-threaded on the main thread.
332   std::unique_ptr<ThreadPool> single_thread_pool_;
333 
334   class AOTCompilationStats;
335   std::unique_ptr<AOTCompilationStats> stats_;
336 
337   CompiledMethodStorage compiled_method_storage_;
338 
339   size_t max_arena_alloc_;
340 
341   // Compiler for dex to dex (quickening).
342   optimizer::DexToDexCompiler dex_to_dex_compiler_;
343 
344   friend class CommonCompilerDriverTest;
345   friend class CompileClassVisitor;
346   friend class DexToDexDecompilerTest;
347   friend class InitializeClassVisitor;
348   friend class verifier::VerifierDepsTest;
349   DISALLOW_COPY_AND_ASSIGN(CompilerDriver);
350 };
351 
352 }  // namespace art
353 
354 #endif  // ART_DEX2OAT_DRIVER_COMPILER_DRIVER_H_
355