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