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