compiler_driver.h revision 762869dee6e0eadab5be1c606792d6693bbabf4e
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 <set>
21#include <string>
22#include <unordered_set>
23#include <vector>
24
25#include "arch/instruction_set.h"
26#include "base/arena_allocator.h"
27#include "base/array_ref.h"
28#include "base/bit_utils.h"
29#include "base/mutex.h"
30#include "base/timing_logger.h"
31#include "class_reference.h"
32#include "compiler.h"
33#include "dex_file.h"
34#include "driver/compiled_method_storage.h"
35#include "jit/offline_profiling_info.h"
36#include "invoke_type.h"
37#include "method_reference.h"
38#include "mirror/class.h"  // For mirror::Class::Status.
39#include "os.h"
40#include "runtime.h"
41#include "safe_map.h"
42#include "thread_pool.h"
43#include "utils/dex_cache_arrays_layout.h"
44
45namespace art {
46
47namespace mirror {
48class DexCache;
49}  // namespace mirror
50
51namespace verifier {
52class MethodVerifier;
53}  // namespace verifier
54
55class BitVector;
56class CompiledClass;
57class CompiledMethod;
58class CompilerOptions;
59class DexCompilationUnit;
60struct InlineIGetIPutData;
61class InstructionSetFeatures;
62class ParallelCompilationManager;
63class ScopedObjectAccess;
64template <class Allocator> class SrcMap;
65class SrcMapElem;
66using SwapSrcMap = SrcMap<SwapAllocator<SrcMapElem>>;
67template<class T> class Handle;
68class TimingLogger;
69class VerificationResults;
70class VerifiedMethod;
71
72enum EntryPointCallingConvention {
73  // ABI of invocations to a method's interpreter entry point.
74  kInterpreterAbi,
75  // ABI of calls to a method's native code, only used for native methods.
76  kJniAbi,
77  // ABI of calls to a method's quick code entry point.
78  kQuickAbi
79};
80
81class CompilerDriver {
82 public:
83  // Create a compiler targeting the requested "instruction_set".
84  // "image" should be true if image specific optimizations should be
85  // enabled.  "image_classes" lets the compiler know what classes it
86  // can assume will be in the image, with null implying all available
87  // classes.
88  CompilerDriver(const CompilerOptions* compiler_options,
89                 VerificationResults* verification_results,
90                 Compiler::Kind compiler_kind,
91                 InstructionSet instruction_set,
92                 const InstructionSetFeatures* instruction_set_features,
93                 bool boot_image,
94                 bool app_image,
95                 std::unordered_set<std::string>* image_classes,
96                 std::unordered_set<std::string>* compiled_classes,
97                 std::unordered_set<std::string>* compiled_methods,
98                 size_t thread_count,
99                 bool dump_stats,
100                 bool dump_passes,
101                 CumulativeLogger* timer,
102                 int swap_fd,
103                 const ProfileCompilationInfo* profile_compilation_info);
104
105  ~CompilerDriver();
106
107  // Set dex files that will be stored in the oat file after being compiled.
108  void SetDexFilesForOatFile(const std::vector<const DexFile*>& dex_files) {
109    dex_files_for_oat_file_ = &dex_files;
110  }
111
112  // Get dex file that will be stored in the oat file after being compiled.
113  ArrayRef<const DexFile* const> GetDexFilesForOatFile() const {
114    return (dex_files_for_oat_file_ != nullptr)
115        ? ArrayRef<const DexFile* const>(*dex_files_for_oat_file_)
116        : ArrayRef<const DexFile* const>();
117  }
118
119  void CompileAll(jobject class_loader,
120                  const std::vector<const DexFile*>& dex_files,
121                  TimingLogger* timings)
122      REQUIRES(!Locks::mutator_lock_, !compiled_classes_lock_, !dex_to_dex_references_lock_);
123
124  // Compile a single Method.
125  void CompileOne(Thread* self, ArtMethod* method, TimingLogger* timings)
126      REQUIRES_SHARED(Locks::mutator_lock_)
127      REQUIRES(!compiled_methods_lock_, !compiled_classes_lock_, !dex_to_dex_references_lock_);
128
129  VerificationResults* GetVerificationResults() const {
130    DCHECK(Runtime::Current()->IsAotCompiler());
131    return verification_results_;
132  }
133
134  InstructionSet GetInstructionSet() const {
135    return instruction_set_;
136  }
137
138  const InstructionSetFeatures* GetInstructionSetFeatures() const {
139    return instruction_set_features_;
140  }
141
142  const CompilerOptions& GetCompilerOptions() const {
143    return *compiler_options_;
144  }
145
146  Compiler* GetCompiler() const {
147    return compiler_.get();
148  }
149
150  // Are we compiling and creating an image file?
151  bool IsBootImage() const {
152    return boot_image_;
153  }
154
155  const std::unordered_set<std::string>* GetImageClasses() const {
156    return image_classes_.get();
157  }
158
159  // Generate the trampolines that are invoked by unresolved direct methods.
160  std::unique_ptr<const std::vector<uint8_t>> CreateJniDlsymLookup() const;
161  std::unique_ptr<const std::vector<uint8_t>> CreateQuickGenericJniTrampoline() const;
162  std::unique_ptr<const std::vector<uint8_t>> CreateQuickImtConflictTrampoline() const;
163  std::unique_ptr<const std::vector<uint8_t>> CreateQuickResolutionTrampoline() const;
164  std::unique_ptr<const std::vector<uint8_t>> CreateQuickToInterpreterBridge() const;
165
166  CompiledClass* GetCompiledClass(ClassReference ref) const
167      REQUIRES(!compiled_classes_lock_);
168
169  CompiledMethod* GetCompiledMethod(MethodReference ref) const
170      REQUIRES(!compiled_methods_lock_);
171  size_t GetNonRelativeLinkerPatchCount() const
172      REQUIRES(!compiled_methods_lock_);
173
174  // Add a compiled method.
175  void AddCompiledMethod(const MethodReference& method_ref,
176                         CompiledMethod* const compiled_method,
177                         size_t non_relative_linker_patch_count)
178      REQUIRES(!compiled_methods_lock_);
179  // Remove and delete a compiled method.
180  void RemoveCompiledMethod(const MethodReference& method_ref) REQUIRES(!compiled_methods_lock_);
181
182  void SetRequiresConstructorBarrier(Thread* self,
183                                     const DexFile* dex_file,
184                                     uint16_t class_def_index,
185                                     bool requires)
186      REQUIRES(!requires_constructor_barrier_lock_);
187  bool RequiresConstructorBarrier(Thread* self,
188                                  const DexFile* dex_file,
189                                  uint16_t class_def_index)
190      REQUIRES(!requires_constructor_barrier_lock_);
191
192  // Callbacks from compiler to see what runtime checks must be generated.
193
194  bool CanAssumeTypeIsPresentInDexCache(Handle<mirror::DexCache> dex_cache,
195                                        uint32_t type_idx)
196      REQUIRES_SHARED(Locks::mutator_lock_);
197
198  bool CanAssumeStringIsPresentInDexCache(const DexFile& dex_file, uint32_t string_idx)
199      REQUIRES(!Locks::mutator_lock_);
200
201  // Are runtime access checks necessary in the compiled code?
202  bool CanAccessTypeWithoutChecks(uint32_t referrer_idx,
203                                  Handle<mirror::DexCache> dex_cache,
204                                  uint32_t type_idx)
205      REQUIRES_SHARED(Locks::mutator_lock_);
206
207  // Are runtime access and instantiable checks necessary in the code?
208  // out_is_finalizable is set to whether the type is finalizable.
209  bool CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx,
210                                              Handle<mirror::DexCache> dex_cache,
211                                              uint32_t type_idx,
212                                              bool* out_is_finalizable)
213      REQUIRES_SHARED(Locks::mutator_lock_);
214
215  bool CanEmbedTypeInCode(const DexFile& dex_file, uint32_t type_idx,
216                          bool* is_type_initialized, bool* use_direct_type_ptr,
217                          uintptr_t* direct_type_ptr, bool* out_is_finalizable);
218
219  // Query methods for the java.lang.ref.Reference class.
220  bool CanEmbedReferenceTypeInCode(ClassReference* ref,
221                                   bool* use_direct_type_ptr, uintptr_t* direct_type_ptr);
222  uint32_t GetReferenceSlowFlagOffset() const;
223  uint32_t GetReferenceDisableFlagOffset() const;
224
225  // Get the DexCache for the
226  mirror::DexCache* GetDexCache(const DexCompilationUnit* mUnit)
227    REQUIRES_SHARED(Locks::mutator_lock_);
228
229  mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa,
230                                      const DexCompilationUnit* mUnit)
231    REQUIRES_SHARED(Locks::mutator_lock_);
232
233  // Resolve compiling method's class. Returns null on failure.
234  mirror::Class* ResolveCompilingMethodsClass(
235      const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
236      Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit)
237      REQUIRES_SHARED(Locks::mutator_lock_);
238
239  mirror::Class* ResolveClass(
240      const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
241      Handle<mirror::ClassLoader> class_loader, uint16_t type_index,
242      const DexCompilationUnit* mUnit)
243      REQUIRES_SHARED(Locks::mutator_lock_);
244
245  // Resolve a field. Returns null on failure, including incompatible class change.
246  // NOTE: Unlike ClassLinker's ResolveField(), this method enforces is_static.
247  ArtField* ResolveField(
248      const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
249      Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
250      uint32_t field_idx, bool is_static)
251      REQUIRES_SHARED(Locks::mutator_lock_);
252
253  // Resolve a field with a given dex file.
254  ArtField* ResolveFieldWithDexFile(
255      const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
256      Handle<mirror::ClassLoader> class_loader, const DexFile* dex_file,
257      uint32_t field_idx, bool is_static)
258      REQUIRES_SHARED(Locks::mutator_lock_);
259
260  // Get declaration location of a resolved field.
261  void GetResolvedFieldDexFileLocation(
262      ArtField* resolved_field, const DexFile** declaring_dex_file,
263      uint16_t* declaring_class_idx, uint16_t* declaring_field_idx)
264      REQUIRES_SHARED(Locks::mutator_lock_);
265
266  bool IsFieldVolatile(ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_);
267  MemberOffset GetFieldOffset(ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_);
268
269  // Find a dex cache for a dex file.
270  inline mirror::DexCache* FindDexCache(const DexFile* dex_file)
271      REQUIRES_SHARED(Locks::mutator_lock_);
272
273  // Can we fast-path an IGET/IPUT access to an instance field? If yes, compute the field offset.
274  std::pair<bool, bool> IsFastInstanceField(
275      mirror::DexCache* dex_cache, mirror::Class* referrer_class,
276      ArtField* resolved_field, uint16_t field_idx)
277      REQUIRES_SHARED(Locks::mutator_lock_);
278
279  // Can we fast-path an SGET/SPUT access to a static field? If yes, compute the type index
280  // of the declaring class in the referrer's dex file.
281  std::pair<bool, bool> IsFastStaticField(
282      mirror::DexCache* dex_cache, mirror::Class* referrer_class,
283      ArtField* resolved_field, uint16_t field_idx, uint32_t* storage_index)
284      REQUIRES_SHARED(Locks::mutator_lock_);
285
286  // Return whether the declaring class of `resolved_method` is
287  // available to `referrer_class`. If this is true, compute the type
288  // index of the declaring class in the referrer's dex file and
289  // return it through the out argument `storage_index`; otherwise
290  // return DexFile::kDexNoIndex through `storage_index`.
291  bool IsClassOfStaticMethodAvailableToReferrer(mirror::DexCache* dex_cache,
292                                                mirror::Class* referrer_class,
293                                                ArtMethod* resolved_method,
294                                                uint16_t method_idx,
295                                                uint32_t* storage_index)
296      REQUIRES_SHARED(Locks::mutator_lock_);
297
298  // Is static field's in referrer's class?
299  bool IsStaticFieldInReferrerClass(mirror::Class* referrer_class, ArtField* resolved_field)
300      REQUIRES_SHARED(Locks::mutator_lock_);
301
302  // Is static field's class initialized?
303  bool IsStaticFieldsClassInitialized(mirror::Class* referrer_class,
304                                      ArtField* resolved_field)
305      REQUIRES_SHARED(Locks::mutator_lock_);
306
307  // Resolve a method. Returns null on failure, including incompatible class change.
308  ArtMethod* ResolveMethod(
309      ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
310      Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
311      uint32_t method_idx, InvokeType invoke_type, bool check_incompatible_class_change = true)
312      REQUIRES_SHARED(Locks::mutator_lock_);
313
314  // Get declaration location of a resolved field.
315  void GetResolvedMethodDexFileLocation(
316      ArtMethod* resolved_method, const DexFile** declaring_dex_file,
317      uint16_t* declaring_class_idx, uint16_t* declaring_method_idx)
318      REQUIRES_SHARED(Locks::mutator_lock_);
319
320  // Get the index in the vtable of the method.
321  uint16_t GetResolvedMethodVTableIndex(
322      ArtMethod* resolved_method, InvokeType type)
323      REQUIRES_SHARED(Locks::mutator_lock_);
324
325  // Is method's class initialized for an invoke?
326  // For static invokes to determine whether we need to consider potential call to <clinit>().
327  // For non-static invokes, assuming a non-null reference, the class is always initialized.
328  bool IsMethodsClassInitialized(mirror::Class* referrer_class, ArtMethod* resolved_method)
329      REQUIRES_SHARED(Locks::mutator_lock_);
330
331  // Get the layout of dex cache arrays for a dex file. Returns invalid layout if the
332  // dex cache arrays don't have a fixed layout.
333  DexCacheArraysLayout GetDexCacheArraysLayout(const DexFile* dex_file);
334
335  void ProcessedInstanceField(bool resolved);
336  void ProcessedStaticField(bool resolved, bool local);
337  void ProcessedInvoke(InvokeType invoke_type, int flags);
338
339  void ComputeFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit,
340                        const ScopedObjectAccess& soa, bool is_static,
341                        ArtField** resolved_field,
342                        mirror::Class** referrer_class,
343                        mirror::DexCache** dex_cache)
344      REQUIRES_SHARED(Locks::mutator_lock_);
345
346  // Can we fast path instance field access? Computes field's offset and volatility.
347  bool ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put,
348                                MemberOffset* field_offset, bool* is_volatile)
349      REQUIRES(!Locks::mutator_lock_);
350
351  ArtField* ComputeInstanceFieldInfo(uint32_t field_idx,
352                                             const DexCompilationUnit* mUnit,
353                                             bool is_put,
354                                             const ScopedObjectAccess& soa)
355      REQUIRES_SHARED(Locks::mutator_lock_);
356
357
358  const VerifiedMethod* GetVerifiedMethod(const DexFile* dex_file, uint32_t method_idx) const;
359  bool IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc);
360
361  bool GetSupportBootImageFixup() const {
362    return support_boot_image_fixup_;
363  }
364
365  void SetSupportBootImageFixup(bool support_boot_image_fixup) {
366    support_boot_image_fixup_ = support_boot_image_fixup;
367  }
368
369  void SetCompilerContext(void* compiler_context) {
370    compiler_context_ = compiler_context;
371  }
372
373  void* GetCompilerContext() const {
374    return compiler_context_;
375  }
376
377  size_t GetThreadCount() const {
378    return parallel_thread_count_;
379  }
380
381  bool GetDumpStats() const {
382    return dump_stats_;
383  }
384
385  bool GetDumpPasses() const {
386    return dump_passes_;
387  }
388
389  CumulativeLogger* GetTimingsLogger() const {
390    return timings_logger_;
391  }
392
393  void SetDedupeEnabled(bool dedupe_enabled) {
394    compiled_method_storage_.SetDedupeEnabled(dedupe_enabled);
395  }
396  bool DedupeEnabled() const {
397    return compiled_method_storage_.DedupeEnabled();
398  }
399
400  // Checks if class specified by type_idx is one of the image_classes_
401  bool IsImageClass(const char* descriptor) const;
402
403  // Checks whether the provided class should be compiled, i.e., is in classes_to_compile_.
404  bool IsClassToCompile(const char* descriptor) const;
405
406  // Checks whether the provided method should be compiled, i.e., is in method_to_compile_.
407  bool IsMethodToCompile(const MethodReference& method_ref) const;
408
409  // Checks whether profile guided compilation is enabled and if the method should be compiled
410  // according to the profile file.
411  bool ShouldCompileBasedOnProfile(const MethodReference& method_ref) const;
412
413  // Checks whether profile guided verification is enabled and if the method should be verified
414  // according to the profile file.
415  bool ShouldVerifyClassBasedOnProfile(const DexFile& dex_file, uint16_t class_idx) const;
416
417  void RecordClassStatus(ClassReference ref, mirror::Class::Status status)
418      REQUIRES(!compiled_classes_lock_);
419
420  // Checks if the specified method has been verified without failures. Returns
421  // false if the method is not in the verification results (GetVerificationResults).
422  bool IsMethodVerifiedWithoutFailures(uint32_t method_idx,
423                                       uint16_t class_def_idx,
424                                       const DexFile& dex_file) const;
425
426  // Get memory usage during compilation.
427  std::string GetMemoryUsageString(bool extended) const;
428
429  void SetHadHardVerifierFailure() {
430    had_hard_verifier_failure_ = true;
431  }
432
433  Compiler::Kind GetCompilerKind() {
434    return compiler_kind_;
435  }
436
437  CompiledMethodStorage* GetCompiledMethodStorage() {
438    return &compiled_method_storage_;
439  }
440
441  // Can we assume that the klass is loaded?
442  bool CanAssumeClassIsLoaded(mirror::Class* klass)
443      REQUIRES_SHARED(Locks::mutator_lock_);
444
445  bool MayInline(const DexFile* inlined_from, const DexFile* inlined_into) const {
446    if (!kIsTargetBuild) {
447      return MayInlineInternal(inlined_from, inlined_into);
448    }
449    return true;
450  }
451
452  void MarkForDexToDexCompilation(Thread* self, const MethodReference& method_ref)
453      REQUIRES(!dex_to_dex_references_lock_);
454
455  const BitVector* GetCurrentDexToDexMethods() const {
456    return current_dex_to_dex_methods_;
457  }
458
459 private:
460  // Return whether the declaring class of `resolved_member` is
461  // available to `referrer_class` for read or write access using two
462  // Boolean values returned as a pair. If is true at least for read
463  // access, compute the type index of the declaring class in the
464  // referrer's dex file and return it through the out argument
465  // `storage_index`; otherwise return DexFile::kDexNoIndex through
466  // `storage_index`.
467  template <typename ArtMember>
468  std::pair<bool, bool> IsClassOfStaticMemberAvailableToReferrer(mirror::DexCache* dex_cache,
469                                                                 mirror::Class* referrer_class,
470                                                                 ArtMember* resolved_member,
471                                                                 uint16_t member_idx,
472                                                                 uint32_t* storage_index)
473      REQUIRES_SHARED(Locks::mutator_lock_);
474
475  // Can `referrer_class` access the resolved `member`?
476  // Dispatch call to mirror::Class::CanAccessResolvedField or
477  // mirror::Class::CanAccessResolvedMember depending on the value of
478  // ArtMember.
479  template <typename ArtMember>
480  static bool CanAccessResolvedMember(mirror::Class* referrer_class,
481                                      mirror::Class* access_to,
482                                      ArtMember* member,
483                                      mirror::DexCache* dex_cache,
484                                      uint32_t field_idx)
485      REQUIRES_SHARED(Locks::mutator_lock_);
486
487  // Can we assume that the klass is initialized?
488  bool CanAssumeClassIsInitialized(mirror::Class* klass)
489      REQUIRES_SHARED(Locks::mutator_lock_);
490  bool CanReferrerAssumeClassIsInitialized(mirror::Class* referrer_class, mirror::Class* klass)
491      REQUIRES_SHARED(Locks::mutator_lock_);
492
493  // These flags are internal to CompilerDriver for collecting INVOKE resolution statistics.
494  // The only external contract is that unresolved method has flags 0 and resolved non-0.
495  enum {
496    kBitMethodResolved = 0,
497    kBitVirtualMadeDirect,
498    kBitPreciseTypeDevirtualization,
499    kBitDirectCallToBoot,
500    kBitDirectMethodToBoot
501  };
502  static constexpr int kFlagMethodResolved              = 1 << kBitMethodResolved;
503  static constexpr int kFlagVirtualMadeDirect           = 1 << kBitVirtualMadeDirect;
504  static constexpr int kFlagPreciseTypeDevirtualization = 1 << kBitPreciseTypeDevirtualization;
505  static constexpr int kFlagDirectCallToBoot            = 1 << kBitDirectCallToBoot;
506  static constexpr int kFlagDirectMethodToBoot          = 1 << kBitDirectMethodToBoot;
507  static constexpr int kFlagsMethodResolvedVirtualMadeDirect =
508      kFlagMethodResolved | kFlagVirtualMadeDirect;
509  static constexpr int kFlagsMethodResolvedPreciseTypeDevirtualization =
510      kFlagsMethodResolvedVirtualMadeDirect | kFlagPreciseTypeDevirtualization;
511
512 public:  // TODO make private or eliminate.
513  // Compute constant code and method pointers when possible.
514  void GetCodeAndMethodForDirectCall(const mirror::Class* referrer_class,
515                                     ArtMethod* method,
516                                     /* out */ uintptr_t* direct_code,
517                                     /* out */ uintptr_t* direct_method)
518      REQUIRES_SHARED(Locks::mutator_lock_);
519
520 private:
521  void PreCompile(jobject class_loader,
522                  const std::vector<const DexFile*>& dex_files,
523                  TimingLogger* timings)
524      REQUIRES(!Locks::mutator_lock_, !compiled_classes_lock_);
525
526  void LoadImageClasses(TimingLogger* timings) REQUIRES(!Locks::mutator_lock_);
527
528  // Attempt to resolve all type, methods, fields, and strings
529  // referenced from code in the dex file following PathClassLoader
530  // ordering semantics.
531  void Resolve(jobject class_loader,
532               const std::vector<const DexFile*>& dex_files,
533               TimingLogger* timings)
534      REQUIRES(!Locks::mutator_lock_);
535  void ResolveDexFile(jobject class_loader,
536                      const DexFile& dex_file,
537                      const std::vector<const DexFile*>& dex_files,
538                      ThreadPool* thread_pool,
539                      size_t thread_count,
540                      TimingLogger* timings)
541      REQUIRES(!Locks::mutator_lock_);
542
543  void Verify(jobject class_loader,
544              const std::vector<const DexFile*>& dex_files,
545              TimingLogger* timings);
546  void VerifyDexFile(jobject class_loader,
547                     const DexFile& dex_file,
548                     const std::vector<const DexFile*>& dex_files,
549                     ThreadPool* thread_pool,
550                     size_t thread_count,
551                     TimingLogger* timings)
552      REQUIRES(!Locks::mutator_lock_);
553
554  void SetVerified(jobject class_loader,
555                   const std::vector<const DexFile*>& dex_files,
556                   TimingLogger* timings);
557  void SetVerifiedDexFile(jobject class_loader,
558                          const DexFile& dex_file,
559                          const std::vector<const DexFile*>& dex_files,
560                          ThreadPool* thread_pool,
561                          size_t thread_count,
562                          TimingLogger* timings)
563      REQUIRES(!Locks::mutator_lock_);
564
565  void InitializeClasses(jobject class_loader,
566                         const std::vector<const DexFile*>& dex_files,
567                         TimingLogger* timings)
568      REQUIRES(!Locks::mutator_lock_, !compiled_classes_lock_);
569  void InitializeClasses(jobject class_loader,
570                         const DexFile& dex_file,
571                         const std::vector<const DexFile*>& dex_files,
572                         TimingLogger* timings)
573      REQUIRES(!Locks::mutator_lock_, !compiled_classes_lock_);
574
575  void UpdateImageClasses(TimingLogger* timings) REQUIRES(!Locks::mutator_lock_);
576  static void FindClinitImageClassesCallback(mirror::Object* object, void* arg)
577      REQUIRES_SHARED(Locks::mutator_lock_);
578
579  void Compile(jobject class_loader,
580               const std::vector<const DexFile*>& dex_files,
581               TimingLogger* timings) REQUIRES(!dex_to_dex_references_lock_);
582  void CompileDexFile(jobject class_loader,
583                      const DexFile& dex_file,
584                      const std::vector<const DexFile*>& dex_files,
585                      ThreadPool* thread_pool,
586                      size_t thread_count,
587                      TimingLogger* timings)
588      REQUIRES(!Locks::mutator_lock_);
589
590  bool MayInlineInternal(const DexFile* inlined_from, const DexFile* inlined_into) const;
591
592  void InitializeThreadPools();
593  void FreeThreadPools();
594  void CheckThreadPools();
595
596  bool RequiresConstructorBarrier(const DexFile& dex_file, uint16_t class_def_idx) const;
597
598  const CompilerOptions* const compiler_options_;
599  VerificationResults* const verification_results_;
600
601  std::unique_ptr<Compiler> compiler_;
602  Compiler::Kind compiler_kind_;
603
604  const InstructionSet instruction_set_;
605  const InstructionSetFeatures* const instruction_set_features_;
606
607  // All class references that require constructor barriers. If the class reference is not in the
608  // set then the result has not yet been computed.
609  mutable ReaderWriterMutex requires_constructor_barrier_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
610  std::map<ClassReference, bool> requires_constructor_barrier_
611      GUARDED_BY(requires_constructor_barrier_lock_);
612
613  typedef SafeMap<const ClassReference, CompiledClass*> ClassTable;
614  // All class references that this compiler has compiled.
615  mutable Mutex compiled_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
616  ClassTable compiled_classes_ GUARDED_BY(compiled_classes_lock_);
617
618  typedef SafeMap<const MethodReference, CompiledMethod*, MethodReferenceComparator> MethodTable;
619
620 public:
621  // Lock is public so that non-members can have lock annotations.
622  mutable Mutex compiled_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
623
624 private:
625  // All method references that this compiler has compiled.
626  MethodTable compiled_methods_ GUARDED_BY(compiled_methods_lock_);
627  // Number of non-relative patches in all compiled methods. These patches need space
628  // in the .oat_patches ELF section if requested in the compiler options.
629  size_t non_relative_linker_patch_count_ GUARDED_BY(compiled_methods_lock_);
630
631  const bool boot_image_;
632  const bool app_image_;
633
634  // If image_ is true, specifies the classes that will be included in the image.
635  // Note if image_classes_ is null, all classes are included in the image.
636  std::unique_ptr<std::unordered_set<std::string>> image_classes_;
637
638  // Specifies the classes that will be compiled. Note that if classes_to_compile_ is null,
639  // all classes are eligible for compilation (duplication filters etc. will still apply).
640  // This option may be restricted to the boot image, depending on a flag in the implementation.
641  std::unique_ptr<std::unordered_set<std::string>> classes_to_compile_;
642
643  // Specifies the methods that will be compiled. Note that if methods_to_compile_ is null,
644  // all methods are eligible for compilation (compilation filters etc. will still apply).
645  // This option may be restricted to the boot image, depending on a flag in the implementation.
646  std::unique_ptr<std::unordered_set<std::string>> methods_to_compile_;
647
648  bool had_hard_verifier_failure_;
649
650  // A thread pool that can (potentially) run tasks in parallel.
651  std::unique_ptr<ThreadPool> parallel_thread_pool_;
652  size_t parallel_thread_count_;
653
654  // A thread pool that guarantees running single-threaded on the main thread.
655  std::unique_ptr<ThreadPool> single_thread_pool_;
656
657  class AOTCompilationStats;
658  std::unique_ptr<AOTCompilationStats> stats_;
659
660  bool dump_stats_;
661  const bool dump_passes_;
662
663  CumulativeLogger* const timings_logger_;
664
665  typedef void (*CompilerCallbackFn)(CompilerDriver& driver);
666  typedef MutexLock* (*CompilerMutexLockFn)(CompilerDriver& driver);
667
668  void* compiler_context_;
669
670  bool support_boot_image_fixup_;
671
672  // List of dex files that will be stored in the oat file.
673  const std::vector<const DexFile*>* dex_files_for_oat_file_;
674
675  CompiledMethodStorage compiled_method_storage_;
676
677  // Info for profile guided compilation.
678  const ProfileCompilationInfo* const profile_compilation_info_;
679
680  size_t max_arena_alloc_;
681
682  // Data for delaying dex-to-dex compilation.
683  Mutex dex_to_dex_references_lock_;
684  // In the first phase, dex_to_dex_references_ collects methods for dex-to-dex compilation.
685  class DexFileMethodSet;
686  std::vector<DexFileMethodSet> dex_to_dex_references_ GUARDED_BY(dex_to_dex_references_lock_);
687  // In the second phase, current_dex_to_dex_methods_ points to the BitVector with method
688  // indexes for dex-to-dex compilation in the current dex file.
689  const BitVector* current_dex_to_dex_methods_;
690
691  friend class CompileClassVisitor;
692  DISALLOW_COPY_AND_ASSIGN(CompilerDriver);
693};
694
695}  // namespace art
696
697#endif  // ART_COMPILER_DRIVER_COMPILER_DRIVER_H_
698