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