compiler_driver.h revision b7248b9f1a6d62a30f0cefc12de63c216b5d46e2
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_DRIVER_COMPILER_DRIVER_H_
18#define ART_COMPILER_DRIVER_COMPILER_DRIVER_H_
19
20#include <set>
21#include <string>
22#include <unordered_set>
23#include <vector>
24
25#include "arch/instruction_set.h"
26#include "base/arena_allocator.h"
27#include "base/bit_utils.h"
28#include "base/mutex.h"
29#include "base/timing_logger.h"
30#include "class_reference.h"
31#include "compiler.h"
32#include "dex_file.h"
33#include "driver/compiled_method_storage.h"
34#include "jit/offline_profiling_info.h"
35#include "invoke_type.h"
36#include "method_reference.h"
37#include "mirror/class.h"  // For mirror::Class::Status.
38#include "os.h"
39#include "runtime.h"
40#include "safe_map.h"
41#include "thread_pool.h"
42#include "utils/array_ref.h"
43#include "utils/dex_cache_arrays_layout.h"
44
45namespace art {
46
47namespace mirror {
48class DexCache;
49}  // namespace mirror
50
51namespace verifier {
52class MethodVerifier;
53}  // namespace verifier
54
55class CompiledClass;
56class CompiledMethod;
57class CompilerOptions;
58class DexCompilationUnit;
59class DexFileToMethodInlinerMap;
60struct InlineIGetIPutData;
61class InstructionSetFeatures;
62class ParallelCompilationManager;
63class ScopedObjectAccess;
64template <class Allocator> class SrcMap;
65class SrcMapElem;
66using SwapSrcMap = SrcMap<SwapAllocator<SrcMapElem>>;
67template<class T> class Handle;
68class TimingLogger;
69class VerificationResults;
70class VerifiedMethod;
71
72enum EntryPointCallingConvention {
73  // ABI of invocations to a method's interpreter entry point.
74  kInterpreterAbi,
75  // ABI of calls to a method's native code, only used for native methods.
76  kJniAbi,
77  // ABI of calls to a method's quick code entry point.
78  kQuickAbi
79};
80
81class CompilerDriver {
82 public:
83  // Create a compiler targeting the requested "instruction_set".
84  // "image" should be true if image specific optimizations should be
85  // enabled.  "image_classes" lets the compiler know what classes it
86  // can assume will be in the image, with null implying all available
87  // classes.
88  CompilerDriver(const CompilerOptions* compiler_options,
89                 VerificationResults* verification_results,
90                 DexFileToMethodInlinerMap* method_inliner_map,
91                 Compiler::Kind compiler_kind,
92                 InstructionSet instruction_set,
93                 const InstructionSetFeatures* instruction_set_features,
94                 bool boot_image,
95                 std::unordered_set<std::string>* image_classes,
96                 std::unordered_set<std::string>* compiled_classes,
97                 std::unordered_set<std::string>* compiled_methods,
98                 size_t thread_count,
99                 bool dump_stats,
100                 bool dump_passes,
101                 CumulativeLogger* timer,
102                 int swap_fd,
103                 const ProfileCompilationInfo* profile_compilation_info);
104
105  ~CompilerDriver();
106
107  // Set dex files that will be stored in the oat file after being compiled.
108  void SetDexFilesForOatFile(const std::vector<const DexFile*>& dex_files) {
109    dex_files_for_oat_file_ = &dex_files;
110  }
111
112  // Get dex file that will be stored in the oat file after being compiled.
113  ArrayRef<const DexFile* const> GetDexFilesForOatFile() const {
114    return (dex_files_for_oat_file_ != nullptr)
115        ? ArrayRef<const DexFile* const>(*dex_files_for_oat_file_)
116        : ArrayRef<const DexFile* const>();
117  }
118
119  void CompileAll(jobject class_loader,
120                  const std::vector<const DexFile*>& dex_files,
121                  TimingLogger* timings)
122      REQUIRES(!Locks::mutator_lock_, !compiled_classes_lock_);
123
124  // Compile a single Method.
125  void CompileOne(Thread* self, ArtMethod* method, TimingLogger* timings)
126      SHARED_REQUIRES(Locks::mutator_lock_)
127      REQUIRES(!compiled_methods_lock_, !compiled_classes_lock_);
128
129  VerificationResults* GetVerificationResults() const {
130    DCHECK(Runtime::Current()->IsAotCompiler());
131    return verification_results_;
132  }
133
134  DexFileToMethodInlinerMap* GetMethodInlinerMap() const {
135    return method_inliner_map_;
136  }
137
138  InstructionSet GetInstructionSet() const {
139    return instruction_set_;
140  }
141
142  const InstructionSetFeatures* GetInstructionSetFeatures() const {
143    return instruction_set_features_;
144  }
145
146  const CompilerOptions& GetCompilerOptions() const {
147    return *compiler_options_;
148  }
149
150  Compiler* GetCompiler() const {
151    return compiler_.get();
152  }
153
154  // Are we compiling and creating an image file?
155  bool IsBootImage() const {
156    return boot_image_;
157  }
158
159  const std::unordered_set<std::string>* GetImageClasses() const {
160    return image_classes_.get();
161  }
162
163  // Generate the trampolines that are invoked by unresolved direct methods.
164  const std::vector<uint8_t>* CreateJniDlsymLookup() const;
165  const std::vector<uint8_t>* CreateQuickGenericJniTrampoline() const;
166  const std::vector<uint8_t>* CreateQuickImtConflictTrampoline() const;
167  const std::vector<uint8_t>* CreateQuickResolutionTrampoline() const;
168  const std::vector<uint8_t>* CreateQuickToInterpreterBridge() const;
169
170  CompiledClass* GetCompiledClass(ClassReference ref) const
171      REQUIRES(!compiled_classes_lock_);
172
173  CompiledMethod* GetCompiledMethod(MethodReference ref) const
174      REQUIRES(!compiled_methods_lock_);
175  size_t GetNonRelativeLinkerPatchCount() const
176      REQUIRES(!compiled_methods_lock_);
177
178  // Add a compiled method.
179  void AddCompiledMethod(const MethodReference& method_ref,
180                         CompiledMethod* const compiled_method,
181                         size_t non_relative_linker_patch_count)
182      REQUIRES(!compiled_methods_lock_);
183  // Remove and delete a compiled method.
184  void RemoveCompiledMethod(const MethodReference& method_ref) REQUIRES(!compiled_methods_lock_);
185
186  void SetRequiresConstructorBarrier(Thread* self,
187                                     const DexFile* dex_file,
188                                     uint16_t class_def_index,
189                                     bool requires)
190      REQUIRES(!requires_constructor_barrier_lock_);
191  bool RequiresConstructorBarrier(Thread* self,
192                                  const DexFile* dex_file,
193                                  uint16_t class_def_index)
194      REQUIRES(!requires_constructor_barrier_lock_);
195
196  // Callbacks from compiler to see what runtime checks must be generated.
197
198  bool CanAssumeTypeIsPresentInDexCache(const DexFile& dex_file, uint32_t type_idx);
199
200  bool CanAssumeStringIsPresentInDexCache(const DexFile& dex_file, uint32_t string_idx)
201      REQUIRES(!Locks::mutator_lock_);
202
203  // Are runtime access checks necessary in the compiled code?
204  bool CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file,
205                                  uint32_t type_idx, bool* type_known_final = nullptr,
206                                  bool* type_known_abstract = nullptr,
207                                  bool* equals_referrers_class = nullptr)
208      REQUIRES(!Locks::mutator_lock_);
209
210  // Are runtime access and instantiable checks necessary in the code?
211  // out_is_finalizable is set to whether the type is finalizable.
212  bool CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx,
213                                              const DexFile& dex_file,
214                                              uint32_t type_idx,
215                                              bool* out_is_finalizable)
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  void SetCompilerContext(void* compiler_context) {
399    compiler_context_ = compiler_context;
400  }
401
402  void* GetCompilerContext() const {
403    return compiler_context_;
404  }
405
406  size_t GetThreadCount() const {
407    return parallel_thread_count_;
408  }
409
410  bool GetDumpStats() const {
411    return dump_stats_;
412  }
413
414  bool GetDumpPasses() const {
415    return dump_passes_;
416  }
417
418  CumulativeLogger* GetTimingsLogger() const {
419    return timings_logger_;
420  }
421
422  void SetDedupeEnabled(bool dedupe_enabled) {
423    compiled_method_storage_.SetDedupeEnabled(dedupe_enabled);
424  }
425  bool DedupeEnabled() const {
426    return compiled_method_storage_.DedupeEnabled();
427  }
428
429  // Checks if class specified by type_idx is one of the image_classes_
430  bool IsImageClass(const char* descriptor) const;
431
432  // Checks whether the provided class should be compiled, i.e., is in classes_to_compile_.
433  bool IsClassToCompile(const char* descriptor) const;
434
435  // Checks whether the provided method should be compiled, i.e., is in method_to_compile_.
436  bool IsMethodToCompile(const MethodReference& method_ref) const;
437
438  // Checks whether profile guided compilation is enabled and if the method should be compiled
439  // according to the profile file.
440  bool ShouldCompileBasedOnProfile(const MethodReference& method_ref) const;
441
442  // Checks whether profile guided verification is enabled and if the method should be verified
443  // according to the profile file.
444  bool ShouldVerifyClassBasedOnProfile(const DexFile& dex_file, uint16_t class_idx) const;
445
446  void RecordClassStatus(ClassReference ref, mirror::Class::Status status)
447      REQUIRES(!compiled_classes_lock_);
448
449  // Checks if the specified method has been verified without failures. Returns
450  // false if the method is not in the verification results (GetVerificationResults).
451  bool IsMethodVerifiedWithoutFailures(uint32_t method_idx,
452                                       uint16_t class_def_idx,
453                                       const DexFile& dex_file) const;
454
455  // Get memory usage during compilation.
456  std::string GetMemoryUsageString(bool extended) const;
457
458  bool IsStringTypeIndex(uint16_t type_index, const DexFile* dex_file);
459  bool IsStringInit(uint32_t method_index, const DexFile* dex_file, int32_t* offset);
460
461  void SetHadHardVerifierFailure() {
462    had_hard_verifier_failure_ = true;
463  }
464
465  Compiler::Kind GetCompilerKind() {
466    return compiler_kind_;
467  }
468
469  CompiledMethodStorage* GetCompiledMethodStorage() {
470    return &compiled_method_storage_;
471  }
472
473  // Can we assume that the klass is loaded?
474  bool CanAssumeClassIsLoaded(mirror::Class* klass)
475      SHARED_REQUIRES(Locks::mutator_lock_);
476
477  bool MayInline(const DexFile* inlined_from, const DexFile* inlined_into) const {
478    if (!kIsTargetBuild) {
479      return MayInlineInternal(inlined_from, inlined_into);
480    }
481    return true;
482  }
483
484 private:
485  // Return whether the declaring class of `resolved_member` is
486  // available to `referrer_class` for read or write access using two
487  // Boolean values returned as a pair. If is true at least for read
488  // access, compute the type index of the declaring class in the
489  // referrer's dex file and return it through the out argument
490  // `storage_index`; otherwise return DexFile::kDexNoIndex through
491  // `storage_index`.
492  template <typename ArtMember>
493  std::pair<bool, bool> IsClassOfStaticMemberAvailableToReferrer(mirror::DexCache* dex_cache,
494                                                                 mirror::Class* referrer_class,
495                                                                 ArtMember* resolved_member,
496                                                                 uint16_t member_idx,
497                                                                 uint32_t* storage_index)
498      SHARED_REQUIRES(Locks::mutator_lock_);
499
500  // Can `referrer_class` access the resolved `member`?
501  // Dispatch call to mirror::Class::CanAccessResolvedField or
502  // mirror::Class::CanAccessResolvedMember depending on the value of
503  // ArtMember.
504  template <typename ArtMember>
505  static bool CanAccessResolvedMember(mirror::Class* referrer_class,
506                                      mirror::Class* access_to,
507                                      ArtMember* member,
508                                      mirror::DexCache* dex_cache,
509                                      uint32_t field_idx)
510      SHARED_REQUIRES(Locks::mutator_lock_);
511
512  // Can we assume that the klass is initialized?
513  bool CanAssumeClassIsInitialized(mirror::Class* klass)
514      SHARED_REQUIRES(Locks::mutator_lock_);
515  bool CanReferrerAssumeClassIsInitialized(mirror::Class* referrer_class, mirror::Class* klass)
516      SHARED_REQUIRES(Locks::mutator_lock_);
517
518  // These flags are internal to CompilerDriver for collecting INVOKE resolution statistics.
519  // The only external contract is that unresolved method has flags 0 and resolved non-0.
520  enum {
521    kBitMethodResolved = 0,
522    kBitVirtualMadeDirect,
523    kBitPreciseTypeDevirtualization,
524    kBitDirectCallToBoot,
525    kBitDirectMethodToBoot
526  };
527  static constexpr int kFlagMethodResolved              = 1 << kBitMethodResolved;
528  static constexpr int kFlagVirtualMadeDirect           = 1 << kBitVirtualMadeDirect;
529  static constexpr int kFlagPreciseTypeDevirtualization = 1 << kBitPreciseTypeDevirtualization;
530  static constexpr int kFlagDirectCallToBoot            = 1 << kBitDirectCallToBoot;
531  static constexpr int kFlagDirectMethodToBoot          = 1 << kBitDirectMethodToBoot;
532  static constexpr int kFlagsMethodResolvedVirtualMadeDirect =
533      kFlagMethodResolved | kFlagVirtualMadeDirect;
534  static constexpr int kFlagsMethodResolvedPreciseTypeDevirtualization =
535      kFlagsMethodResolvedVirtualMadeDirect | kFlagPreciseTypeDevirtualization;
536
537 public:  // TODO make private or eliminate.
538  // Compute constant code and method pointers when possible.
539  void GetCodeAndMethodForDirectCall(/*out*/InvokeType* type,
540                                     InvokeType sharp_type,
541                                     bool no_guarantee_of_dex_cache_entry,
542                                     const mirror::Class* referrer_class,
543                                     ArtMethod* method,
544                                     /*out*/int* stats_flags,
545                                     MethodReference* target_method,
546                                     uintptr_t* direct_code, uintptr_t* direct_method)
547      SHARED_REQUIRES(Locks::mutator_lock_);
548
549 private:
550  void PreCompile(jobject class_loader,
551                  const std::vector<const DexFile*>& dex_files,
552                  TimingLogger* timings)
553      REQUIRES(!Locks::mutator_lock_, !compiled_classes_lock_);
554
555  void LoadImageClasses(TimingLogger* timings) REQUIRES(!Locks::mutator_lock_);
556
557  // Attempt to resolve all type, methods, fields, and strings
558  // referenced from code in the dex file following PathClassLoader
559  // ordering semantics.
560  void Resolve(jobject class_loader,
561               const std::vector<const DexFile*>& dex_files,
562               TimingLogger* timings)
563      REQUIRES(!Locks::mutator_lock_);
564  void ResolveDexFile(jobject class_loader,
565                      const DexFile& dex_file,
566                      const std::vector<const DexFile*>& dex_files,
567                      ThreadPool* thread_pool,
568                      size_t thread_count,
569                      TimingLogger* timings)
570      REQUIRES(!Locks::mutator_lock_);
571
572  void Verify(jobject class_loader,
573              const std::vector<const DexFile*>& dex_files,
574              TimingLogger* timings);
575  void VerifyDexFile(jobject class_loader,
576                     const DexFile& dex_file,
577                     const std::vector<const DexFile*>& dex_files,
578                     ThreadPool* thread_pool,
579                     size_t thread_count,
580                     TimingLogger* timings)
581      REQUIRES(!Locks::mutator_lock_);
582
583  void SetVerified(jobject class_loader,
584                   const std::vector<const DexFile*>& dex_files,
585                   TimingLogger* timings);
586  void SetVerifiedDexFile(jobject class_loader,
587                          const DexFile& dex_file,
588                          const std::vector<const DexFile*>& dex_files,
589                          ThreadPool* thread_pool,
590                          size_t thread_count,
591                          TimingLogger* timings)
592      REQUIRES(!Locks::mutator_lock_);
593
594  void InitializeClasses(jobject class_loader,
595                         const std::vector<const DexFile*>& dex_files,
596                         TimingLogger* timings)
597      REQUIRES(!Locks::mutator_lock_, !compiled_classes_lock_);
598  void InitializeClasses(jobject class_loader,
599                         const DexFile& dex_file,
600                         const std::vector<const DexFile*>& dex_files,
601                         TimingLogger* timings)
602      REQUIRES(!Locks::mutator_lock_, !compiled_classes_lock_);
603
604  void UpdateImageClasses(TimingLogger* timings) REQUIRES(!Locks::mutator_lock_);
605  static void FindClinitImageClassesCallback(mirror::Object* object, void* arg)
606      SHARED_REQUIRES(Locks::mutator_lock_);
607
608  void Compile(jobject class_loader,
609               const std::vector<const DexFile*>& dex_files,
610               TimingLogger* timings);
611  void CompileDexFile(jobject class_loader,
612                      const DexFile& dex_file,
613                      const std::vector<const DexFile*>& dex_files,
614                      ThreadPool* thread_pool,
615                      size_t thread_count,
616                      TimingLogger* timings)
617      REQUIRES(!Locks::mutator_lock_);
618
619  bool MayInlineInternal(const DexFile* inlined_from, const DexFile* inlined_into) const;
620
621  void InitializeThreadPools();
622  void FreeThreadPools();
623  void CheckThreadPools();
624
625  bool RequiresConstructorBarrier(const DexFile& dex_file, uint16_t class_def_idx) const;
626
627  const CompilerOptions* const compiler_options_;
628  VerificationResults* const verification_results_;
629  DexFileToMethodInlinerMap* const method_inliner_map_;
630
631  std::unique_ptr<Compiler> compiler_;
632  Compiler::Kind compiler_kind_;
633
634  const InstructionSet instruction_set_;
635  const InstructionSetFeatures* const instruction_set_features_;
636
637  // All class references that require constructor barriers. If the class reference is not in the
638  // set then the result has not yet been computed.
639  mutable ReaderWriterMutex requires_constructor_barrier_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
640  std::map<ClassReference, bool> requires_constructor_barrier_
641      GUARDED_BY(requires_constructor_barrier_lock_);
642
643  typedef SafeMap<const ClassReference, CompiledClass*> ClassTable;
644  // All class references that this compiler has compiled.
645  mutable Mutex compiled_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
646  ClassTable compiled_classes_ GUARDED_BY(compiled_classes_lock_);
647
648  typedef SafeMap<const MethodReference, CompiledMethod*, MethodReferenceComparator> MethodTable;
649
650 public:
651  // Lock is public so that non-members can have lock annotations.
652  mutable Mutex compiled_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
653
654 private:
655  // All method references that this compiler has compiled.
656  MethodTable compiled_methods_ GUARDED_BY(compiled_methods_lock_);
657  // Number of non-relative patches in all compiled methods. These patches need space
658  // in the .oat_patches ELF section if requested in the compiler options.
659  size_t non_relative_linker_patch_count_ GUARDED_BY(compiled_methods_lock_);
660
661  const bool boot_image_;
662
663  // If image_ is true, specifies the classes that will be included in the image.
664  // Note if image_classes_ is null, all classes are included in the image.
665  std::unique_ptr<std::unordered_set<std::string>> image_classes_;
666
667  // Specifies the classes that will be compiled. Note that if classes_to_compile_ is null,
668  // all classes are eligible for compilation (duplication filters etc. will still apply).
669  // This option may be restricted to the boot image, depending on a flag in the implementation.
670  std::unique_ptr<std::unordered_set<std::string>> classes_to_compile_;
671
672  // Specifies the methods that will be compiled. Note that if methods_to_compile_ is null,
673  // all methods are eligible for compilation (compilation filters etc. will still apply).
674  // This option may be restricted to the boot image, depending on a flag in the implementation.
675  std::unique_ptr<std::unordered_set<std::string>> methods_to_compile_;
676
677  bool had_hard_verifier_failure_;
678
679  // A thread pool that can (potentially) run tasks in parallel.
680  std::unique_ptr<ThreadPool> parallel_thread_pool_;
681  size_t parallel_thread_count_;
682
683  // A thread pool that guarantees running single-threaded on the main thread.
684  std::unique_ptr<ThreadPool> single_thread_pool_;
685
686  class AOTCompilationStats;
687  std::unique_ptr<AOTCompilationStats> stats_;
688
689  bool dump_stats_;
690  const bool dump_passes_;
691
692  CumulativeLogger* const timings_logger_;
693
694  typedef void (*CompilerCallbackFn)(CompilerDriver& driver);
695  typedef MutexLock* (*CompilerMutexLockFn)(CompilerDriver& driver);
696
697  void* compiler_context_;
698
699  bool support_boot_image_fixup_;
700
701  // List of dex files that will be stored in the oat file.
702  const std::vector<const DexFile*>* dex_files_for_oat_file_;
703
704  CompiledMethodStorage compiled_method_storage_;
705
706  // Info for profile guided compilation.
707  const ProfileCompilationInfo* const profile_compilation_info_;
708
709  size_t max_arena_alloc_;
710  friend class CompileClassVisitor;
711  DISALLOW_COPY_AND_ASSIGN(CompilerDriver);
712};
713
714}  // namespace art
715
716#endif  // ART_COMPILER_DRIVER_COMPILER_DRIVER_H_
717