compiler_driver.h revision 28e012a4af2d710e5e5f824709ffd6432e4f549f
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_COMPILER_DRIVER_COMPILER_DRIVER_H_
18#define ART_COMPILER_DRIVER_COMPILER_DRIVER_H_
19
20#include <atomic>
21#include <set>
22#include <string>
23#include <unordered_set>
24#include <vector>
25
26#include "android-base/strings.h"
27
28#include "arch/instruction_set.h"
29#include "base/array_ref.h"
30#include "base/bit_utils.h"
31#include "base/mutex.h"
32#include "base/timing_logger.h"
33#include "class_reference.h"
34#include "compiler.h"
35#include "dex_file.h"
36#include "dex_file_types.h"
37#include "driver/compiled_method_storage.h"
38#include "jit/profile_compilation_info.h"
39#include "method_reference.h"
40#include "mirror/class.h"  // For mirror::Class::Status.
41#include "os.h"
42#include "safe_map.h"
43#include "thread_pool.h"
44#include "utils/atomic_dex_ref_map.h"
45#include "utils/dex_cache_arrays_layout.h"
46
47namespace art {
48
49namespace mirror {
50class DexCache;
51}  // namespace mirror
52
53namespace verifier {
54class MethodVerifier;
55class VerifierDepsTest;
56}  // namespace verifier
57
58class BitVector;
59class CompiledMethod;
60class CompilerOptions;
61class DexCompilationUnit;
62struct InlineIGetIPutData;
63class InstructionSetFeatures;
64class InternTable;
65enum InvokeType : uint32_t;
66class ParallelCompilationManager;
67class ScopedObjectAccess;
68template <class Allocator> class SrcMap;
69template<class T> class Handle;
70class TimingLogger;
71class VdexFile;
72class VerificationResults;
73class VerifiedMethod;
74
75enum EntryPointCallingConvention {
76  // ABI of invocations to a method's interpreter entry point.
77  kInterpreterAbi,
78  // ABI of calls to a method's native code, only used for native methods.
79  kJniAbi,
80  // ABI of calls to a method's quick code entry point.
81  kQuickAbi
82};
83
84class CompilerDriver {
85 public:
86  // Create a compiler targeting the requested "instruction_set".
87  // "image" should be true if image specific optimizations should be
88  // enabled.  "image_classes" lets the compiler know what classes it
89  // can assume will be in the image, with null implying all available
90  // classes.
91  CompilerDriver(const CompilerOptions* compiler_options,
92                 VerificationResults* verification_results,
93                 Compiler::Kind compiler_kind,
94                 InstructionSet instruction_set,
95                 const InstructionSetFeatures* instruction_set_features,
96                 std::unordered_set<std::string>* image_classes,
97                 std::unordered_set<std::string>* compiled_classes,
98                 std::unordered_set<std::string>* compiled_methods,
99                 size_t thread_count,
100                 int swap_fd,
101                 const ProfileCompilationInfo* profile_compilation_info);
102
103  ~CompilerDriver();
104
105  // Set dex files that will be stored in the oat file after being compiled.
106  void SetDexFilesForOatFile(const std::vector<const DexFile*>& dex_files);
107
108  // Set dex files classpath.
109  void SetClasspathDexFiles(const std::vector<const DexFile*>& dex_files);
110
111  // Get dex file that will be stored in the oat file after being compiled.
112  ArrayRef<const DexFile* const> GetDexFilesForOatFile() const {
113    return ArrayRef<const DexFile* const>(dex_files_for_oat_file_);
114  }
115
116  void CompileAll(jobject class_loader,
117                  const std::vector<const DexFile*>& dex_files,
118                  TimingLogger* timings)
119      REQUIRES(!Locks::mutator_lock_, !dex_to_dex_references_lock_);
120
121  // Compile a single Method.
122  void CompileOne(Thread* self, ArtMethod* method, TimingLogger* timings)
123      REQUIRES_SHARED(Locks::mutator_lock_)
124      REQUIRES(!dex_to_dex_references_lock_);
125
126  VerificationResults* GetVerificationResults() const;
127
128  InstructionSet GetInstructionSet() const {
129    return instruction_set_;
130  }
131
132  const InstructionSetFeatures* GetInstructionSetFeatures() const {
133    return instruction_set_features_;
134  }
135
136  const CompilerOptions& GetCompilerOptions() const {
137    return *compiler_options_;
138  }
139
140  Compiler* GetCompiler() const {
141    return compiler_.get();
142  }
143
144  const std::unordered_set<std::string>* GetImageClasses() const {
145    return image_classes_.get();
146  }
147
148  // Generate the trampolines that are invoked by unresolved direct methods.
149  std::unique_ptr<const std::vector<uint8_t>> CreateJniDlsymLookup() const;
150  std::unique_ptr<const std::vector<uint8_t>> CreateQuickGenericJniTrampoline() const;
151  std::unique_ptr<const std::vector<uint8_t>> CreateQuickImtConflictTrampoline() const;
152  std::unique_ptr<const std::vector<uint8_t>> CreateQuickResolutionTrampoline() const;
153  std::unique_ptr<const std::vector<uint8_t>> CreateQuickToInterpreterBridge() const;
154
155  mirror::Class::Status GetClassStatus(const ClassReference& ref) const;
156  bool GetCompiledClass(const ClassReference& ref, mirror::Class::Status* status) const;
157
158  CompiledMethod* GetCompiledMethod(MethodReference ref) const;
159  size_t GetNonRelativeLinkerPatchCount() const;
160  // Add a compiled method.
161  void AddCompiledMethod(const MethodReference& method_ref,
162                         CompiledMethod* const compiled_method,
163                         size_t non_relative_linker_patch_count);
164
165  void SetRequiresConstructorBarrier(Thread* self,
166                                     const DexFile* dex_file,
167                                     uint16_t class_def_index,
168                                     bool requires)
169      REQUIRES(!requires_constructor_barrier_lock_);
170
171  // Do the <init> methods for this class require a constructor barrier (prior to the return)?
172  // The answer is "yes", if and only if this class has any instance final fields.
173  // (This must not be called for any non-<init> methods; the answer would be "no").
174  //
175  // ---
176  //
177  // JLS 17.5.1 "Semantics of final fields" mandates that all final fields are frozen at the end
178  // of the invoked constructor. The constructor barrier is a conservative implementation means of
179  // enforcing the freezes happen-before the object being constructed is observable by another
180  // thread.
181  //
182  // Note: This question only makes sense for instance constructors;
183  // static constructors (despite possibly having finals) never need
184  // a barrier.
185  //
186  // JLS 12.4.2 "Detailed Initialization Procedure" approximately describes
187  // class initialization as:
188  //
189  //   lock(class.lock)
190  //     class.state = initializing
191  //   unlock(class.lock)
192  //
193  //   invoke <clinit>
194  //
195  //   lock(class.lock)
196  //     class.state = initialized
197  //   unlock(class.lock)              <-- acts as a release
198  //
199  // The last operation in the above example acts as an atomic release
200  // for any stores in <clinit>, which ends up being stricter
201  // than what a constructor barrier needs.
202  //
203  // See also QuasiAtomic::ThreadFenceForConstructor().
204  bool RequiresConstructorBarrier(Thread* self,
205                                  const DexFile* dex_file,
206                                  uint16_t class_def_index)
207      REQUIRES(!requires_constructor_barrier_lock_);
208
209  // Are runtime access checks necessary in the compiled code?
210  bool CanAccessTypeWithoutChecks(ObjPtr<mirror::Class> referrer_class,
211                                  ObjPtr<mirror::Class> resolved_class)
212      REQUIRES_SHARED(Locks::mutator_lock_);
213
214  // Are runtime access and instantiable checks necessary in the code?
215  // out_is_finalizable is set to whether the type is finalizable.
216  bool CanAccessInstantiableTypeWithoutChecks(ObjPtr<mirror::Class> referrer_class,
217                                              ObjPtr<mirror::Class> resolved_class,
218                                              bool* out_is_finalizable)
219      REQUIRES_SHARED(Locks::mutator_lock_);
220
221  // Resolve compiling method's class. Returns null on failure.
222  ObjPtr<mirror::Class> ResolveCompilingMethodsClass(
223      const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
224      Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit)
225      REQUIRES_SHARED(Locks::mutator_lock_);
226
227  ObjPtr<mirror::Class> ResolveClass(
228      const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
229      Handle<mirror::ClassLoader> class_loader, dex::TypeIndex type_index,
230      const DexCompilationUnit* mUnit)
231      REQUIRES_SHARED(Locks::mutator_lock_);
232
233  // Resolve a field. Returns null on failure, including incompatible class change.
234  // NOTE: Unlike ClassLinker's ResolveField(), this method enforces is_static.
235  ArtField* ResolveField(
236      const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
237      Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
238      uint32_t field_idx, bool is_static)
239      REQUIRES_SHARED(Locks::mutator_lock_);
240
241  // Resolve a field with a given dex file.
242  ArtField* ResolveFieldWithDexFile(
243      const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
244      Handle<mirror::ClassLoader> class_loader, const DexFile* dex_file,
245      uint32_t field_idx, bool is_static)
246      REQUIRES_SHARED(Locks::mutator_lock_);
247
248  // Can we fast-path an IGET/IPUT access to an instance field? If yes, compute the field offset.
249  std::pair<bool, bool> IsFastInstanceField(
250      ObjPtr<mirror::DexCache> dex_cache,
251      ObjPtr<mirror::Class> referrer_class,
252      ArtField* resolved_field, uint16_t field_idx)
253      REQUIRES_SHARED(Locks::mutator_lock_);
254
255  // Resolve a method. Returns null on failure, including incompatible class change.
256  ArtMethod* ResolveMethod(
257      ScopedObjectAccess& soa,
258      Handle<mirror::DexCache> dex_cache,
259      Handle<mirror::ClassLoader> class_loader,
260      const DexCompilationUnit* mUnit,
261      uint32_t method_idx,
262      InvokeType invoke_type)
263      REQUIRES_SHARED(Locks::mutator_lock_);
264
265  void ProcessedInstanceField(bool resolved);
266  void ProcessedStaticField(bool resolved, bool local);
267
268  // Can we fast path instance field access? Computes field's offset and volatility.
269  bool ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put,
270                                MemberOffset* field_offset, bool* is_volatile)
271      REQUIRES(!Locks::mutator_lock_);
272
273  ArtField* ComputeInstanceFieldInfo(uint32_t field_idx,
274                                             const DexCompilationUnit* mUnit,
275                                             bool is_put,
276                                             const ScopedObjectAccess& soa)
277      REQUIRES_SHARED(Locks::mutator_lock_);
278
279
280  const VerifiedMethod* GetVerifiedMethod(const DexFile* dex_file, uint32_t method_idx) const;
281  bool IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc);
282
283  bool GetSupportBootImageFixup() const {
284    return support_boot_image_fixup_;
285  }
286
287  void SetSupportBootImageFixup(bool support_boot_image_fixup) {
288    support_boot_image_fixup_ = support_boot_image_fixup;
289  }
290
291  void SetCompilerContext(void* compiler_context) {
292    compiler_context_ = compiler_context;
293  }
294
295  void* GetCompilerContext() const {
296    return compiler_context_;
297  }
298
299  size_t GetThreadCount() const {
300    return parallel_thread_count_;
301  }
302
303  void SetDedupeEnabled(bool dedupe_enabled) {
304    compiled_method_storage_.SetDedupeEnabled(dedupe_enabled);
305  }
306
307  bool DedupeEnabled() const {
308    return compiled_method_storage_.DedupeEnabled();
309  }
310
311  // Checks if class specified by type_idx is one of the image_classes_
312  bool IsImageClass(const char* descriptor) const;
313
314  // Checks whether the provided class should be compiled, i.e., is in classes_to_compile_.
315  bool IsClassToCompile(const char* descriptor) const;
316
317  // Checks whether the provided method should be compiled, i.e., is in method_to_compile_.
318  bool IsMethodToCompile(const MethodReference& method_ref) const;
319
320  // Checks whether profile guided compilation is enabled and if the method should be compiled
321  // according to the profile file.
322  bool ShouldCompileBasedOnProfile(const MethodReference& method_ref) const;
323
324  // Checks whether profile guided verification is enabled and if the method should be verified
325  // according to the profile file.
326  bool ShouldVerifyClassBasedOnProfile(const DexFile& dex_file, uint16_t class_idx) const;
327
328  void RecordClassStatus(const ClassReference& ref, mirror::Class::Status status);
329
330  // Checks if the specified method has been verified without failures. Returns
331  // false if the method is not in the verification results (GetVerificationResults).
332  bool IsMethodVerifiedWithoutFailures(uint32_t method_idx,
333                                       uint16_t class_def_idx,
334                                       const DexFile& dex_file) const;
335
336  // Get memory usage during compilation.
337  std::string GetMemoryUsageString(bool extended) const;
338
339  void SetHadHardVerifierFailure() {
340    had_hard_verifier_failure_ = true;
341  }
342  void AddSoftVerifierFailure() {
343    number_of_soft_verifier_failures_++;
344  }
345
346  Compiler::Kind GetCompilerKind() {
347    return compiler_kind_;
348  }
349
350  CompiledMethodStorage* GetCompiledMethodStorage() {
351    return &compiled_method_storage_;
352  }
353
354  // Can we assume that the klass is loaded?
355  bool CanAssumeClassIsLoaded(mirror::Class* klass)
356      REQUIRES_SHARED(Locks::mutator_lock_);
357
358  bool MayInline(const DexFile* inlined_from, const DexFile* inlined_into) const {
359    if (!kIsTargetBuild) {
360      return MayInlineInternal(inlined_from, inlined_into);
361    }
362    return true;
363  }
364
365  void MarkForDexToDexCompilation(Thread* self, const MethodReference& method_ref)
366      REQUIRES(!dex_to_dex_references_lock_);
367
368  const BitVector* GetCurrentDexToDexMethods() const {
369    return current_dex_to_dex_methods_;
370  }
371
372  const ProfileCompilationInfo* GetProfileCompilationInfo() const {
373    return profile_compilation_info_;
374  }
375
376  // Is `boot_image_filename` the name of a core image (small boot
377  // image used for ART testing only)?
378  static bool IsCoreImageFilename(const std::string& boot_image_filename) {
379    // TODO: This is under-approximating...
380    return android::base::EndsWith(boot_image_filename, "core.art")
381        || android::base::EndsWith(boot_image_filename, "core-optimizing.art");
382  }
383
384 private:
385  void PreCompile(jobject class_loader,
386                  const std::vector<const DexFile*>& dex_files,
387                  TimingLogger* timings)
388      REQUIRES(!Locks::mutator_lock_);
389
390  void LoadImageClasses(TimingLogger* timings) REQUIRES(!Locks::mutator_lock_);
391
392  // Attempt to resolve all type, methods, fields, and strings
393  // referenced from code in the dex file following PathClassLoader
394  // ordering semantics.
395  void Resolve(jobject class_loader,
396               const std::vector<const DexFile*>& dex_files,
397               TimingLogger* timings)
398      REQUIRES(!Locks::mutator_lock_);
399  void ResolveDexFile(jobject class_loader,
400                      const DexFile& dex_file,
401                      const std::vector<const DexFile*>& dex_files,
402                      ThreadPool* thread_pool,
403                      size_t thread_count,
404                      TimingLogger* timings)
405      REQUIRES(!Locks::mutator_lock_);
406
407  // Do fast verification through VerifierDeps if possible. Return whether
408  // verification was successful.
409  bool FastVerify(jobject class_loader,
410                  const std::vector<const DexFile*>& dex_files,
411                  TimingLogger* timings);
412
413  void Verify(jobject class_loader,
414              const std::vector<const DexFile*>& dex_files,
415              TimingLogger* timings);
416
417  void VerifyDexFile(jobject class_loader,
418                     const DexFile& dex_file,
419                     const std::vector<const DexFile*>& dex_files,
420                     ThreadPool* thread_pool,
421                     size_t thread_count,
422                     TimingLogger* timings)
423      REQUIRES(!Locks::mutator_lock_);
424
425  void SetVerified(jobject class_loader,
426                   const std::vector<const DexFile*>& dex_files,
427                   TimingLogger* timings);
428  void SetVerifiedDexFile(jobject class_loader,
429                          const DexFile& dex_file,
430                          const std::vector<const DexFile*>& dex_files,
431                          ThreadPool* thread_pool,
432                          size_t thread_count,
433                          TimingLogger* timings)
434      REQUIRES(!Locks::mutator_lock_);
435
436  void InitializeClasses(jobject class_loader,
437                         const std::vector<const DexFile*>& dex_files,
438                         TimingLogger* timings)
439      REQUIRES(!Locks::mutator_lock_);
440  void InitializeClasses(jobject class_loader,
441                         const DexFile& dex_file,
442                         const std::vector<const DexFile*>& dex_files,
443                         TimingLogger* timings)
444      REQUIRES(!Locks::mutator_lock_);
445
446  void UpdateImageClasses(TimingLogger* timings) REQUIRES(!Locks::mutator_lock_);
447
448  void Compile(jobject class_loader,
449               const std::vector<const DexFile*>& dex_files,
450               TimingLogger* timings) REQUIRES(!dex_to_dex_references_lock_);
451  void CompileDexFile(jobject class_loader,
452                      const DexFile& dex_file,
453                      const std::vector<const DexFile*>& dex_files,
454                      ThreadPool* thread_pool,
455                      size_t thread_count,
456                      TimingLogger* timings)
457      REQUIRES(!Locks::mutator_lock_);
458
459  bool MayInlineInternal(const DexFile* inlined_from, const DexFile* inlined_into) const;
460
461  void InitializeThreadPools();
462  void FreeThreadPools();
463  void CheckThreadPools();
464
465  bool RequiresConstructorBarrier(const DexFile& dex_file, uint16_t class_def_idx) const;
466
467  const CompilerOptions* const compiler_options_;
468  VerificationResults* const verification_results_;
469
470  std::unique_ptr<Compiler> compiler_;
471  Compiler::Kind compiler_kind_;
472
473  const InstructionSet instruction_set_;
474  const InstructionSetFeatures* const instruction_set_features_;
475
476  // All class references that require constructor barriers. If the class reference is not in the
477  // set then the result has not yet been computed.
478  mutable ReaderWriterMutex requires_constructor_barrier_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
479  std::map<ClassReference, bool> requires_constructor_barrier_
480      GUARDED_BY(requires_constructor_barrier_lock_);
481
482  // All class references that this compiler has compiled. Indexed by class defs.
483  using ClassStateTable = AtomicDexRefMap<ClassReference, mirror::Class::Status>;
484  ClassStateTable compiled_classes_;
485  // All class references that are in the classpath. Indexed by class defs.
486  ClassStateTable classpath_classes_;
487
488  typedef AtomicDexRefMap<MethodReference, CompiledMethod*> MethodTable;
489
490 private:
491  // All method references that this compiler has compiled.
492  MethodTable compiled_methods_;
493
494  // Number of non-relative patches in all compiled methods. These patches need space
495  // in the .oat_patches ELF section if requested in the compiler options.
496  Atomic<size_t> non_relative_linker_patch_count_;
497
498  // If image_ is true, specifies the classes that will be included in the image.
499  // Note if image_classes_ is null, all classes are included in the image.
500  std::unique_ptr<std::unordered_set<std::string>> image_classes_;
501
502  // Specifies the classes that will be compiled. Note that if classes_to_compile_ is null,
503  // all classes are eligible for compilation (duplication filters etc. will still apply).
504  // This option may be restricted to the boot image, depending on a flag in the implementation.
505  std::unique_ptr<std::unordered_set<std::string>> classes_to_compile_;
506
507  // Specifies the methods that will be compiled. Note that if methods_to_compile_ is null,
508  // all methods are eligible for compilation (compilation filters etc. will still apply).
509  // This option may be restricted to the boot image, depending on a flag in the implementation.
510  std::unique_ptr<std::unordered_set<std::string>> methods_to_compile_;
511
512  std::atomic<uint32_t> number_of_soft_verifier_failures_;
513  bool had_hard_verifier_failure_;
514
515  // A thread pool that can (potentially) run tasks in parallel.
516  std::unique_ptr<ThreadPool> parallel_thread_pool_;
517  size_t parallel_thread_count_;
518
519  // A thread pool that guarantees running single-threaded on the main thread.
520  std::unique_ptr<ThreadPool> single_thread_pool_;
521
522  class AOTCompilationStats;
523  std::unique_ptr<AOTCompilationStats> stats_;
524
525  typedef void (*CompilerCallbackFn)(CompilerDriver& driver);
526  typedef MutexLock* (*CompilerMutexLockFn)(CompilerDriver& driver);
527
528  void* compiler_context_;
529
530  bool support_boot_image_fixup_;
531
532  // List of dex files that will be stored in the oat file.
533  std::vector<const DexFile*> dex_files_for_oat_file_;
534
535  CompiledMethodStorage compiled_method_storage_;
536
537  // Info for profile guided compilation.
538  const ProfileCompilationInfo* const profile_compilation_info_;
539
540  size_t max_arena_alloc_;
541
542  // Data for delaying dex-to-dex compilation.
543  Mutex dex_to_dex_references_lock_;
544  // In the first phase, dex_to_dex_references_ collects methods for dex-to-dex compilation.
545  class DexFileMethodSet;
546  std::vector<DexFileMethodSet> dex_to_dex_references_ GUARDED_BY(dex_to_dex_references_lock_);
547  // In the second phase, current_dex_to_dex_methods_ points to the BitVector with method
548  // indexes for dex-to-dex compilation in the current dex file.
549  const BitVector* current_dex_to_dex_methods_;
550
551  friend class CompileClassVisitor;
552  friend class DexToDexDecompilerTest;
553  friend class verifier::VerifierDepsTest;
554  DISALLOW_COPY_AND_ASSIGN(CompilerDriver);
555};
556
557}  // namespace art
558
559#endif  // ART_COMPILER_DRIVER_COMPILER_DRIVER_H_
560