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