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