compiler_driver.h revision b5d386118334fa5181c31b83b3fee6332537f4b7
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 AddRequiresNoConstructorBarrier(Thread* self, const DexFile* dex_file,
187                                     uint16_t class_def_index)
188      REQUIRES(!no_barrier_constructor_classes_lock_);
189  bool RequiresConstructorBarrier(Thread* self, const DexFile* dex_file,
190                                  uint16_t class_def_index) const
191      REQUIRES(!no_barrier_constructor_classes_lock_);
192
193  // Callbacks from compiler to see what runtime checks must be generated.
194
195  bool CanAssumeTypeIsPresentInDexCache(const DexFile& dex_file, uint32_t type_idx);
196
197  bool CanAssumeStringIsPresentInDexCache(const DexFile& dex_file, uint32_t string_idx)
198      REQUIRES(!Locks::mutator_lock_);
199
200  // Are runtime access checks necessary in the compiled code?
201  bool CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file,
202                                  uint32_t type_idx, bool* type_known_final = nullptr,
203                                  bool* type_known_abstract = nullptr,
204                                  bool* equals_referrers_class = nullptr)
205      REQUIRES(!Locks::mutator_lock_);
206
207  // Are runtime access and instantiable checks necessary in the code?
208  // out_is_finalizable is set to whether the type is finalizable.
209  bool CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx,
210                                              const DexFile& dex_file,
211                                              uint32_t type_idx,
212                                              bool* out_is_finalizable)
213      REQUIRES(!Locks::mutator_lock_);
214
215  bool CanEmbedTypeInCode(const DexFile& dex_file, uint32_t type_idx,
216                          bool* is_type_initialized, bool* use_direct_type_ptr,
217                          uintptr_t* direct_type_ptr, bool* out_is_finalizable);
218
219  // Query methods for the java.lang.ref.Reference class.
220  bool CanEmbedReferenceTypeInCode(ClassReference* ref,
221                                   bool* use_direct_type_ptr, uintptr_t* direct_type_ptr);
222  uint32_t GetReferenceSlowFlagOffset() const;
223  uint32_t GetReferenceDisableFlagOffset() const;
224
225  // Get the DexCache for the
226  mirror::DexCache* GetDexCache(const DexCompilationUnit* mUnit)
227    SHARED_REQUIRES(Locks::mutator_lock_);
228
229  mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa,
230                                      const DexCompilationUnit* mUnit)
231    SHARED_REQUIRES(Locks::mutator_lock_);
232
233  // Resolve compiling method's class. Returns null on failure.
234  mirror::Class* ResolveCompilingMethodsClass(
235      const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
236      Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit)
237      SHARED_REQUIRES(Locks::mutator_lock_);
238
239  mirror::Class* ResolveClass(
240      const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
241      Handle<mirror::ClassLoader> class_loader, uint16_t type_index,
242      const DexCompilationUnit* mUnit)
243      SHARED_REQUIRES(Locks::mutator_lock_);
244
245  // Resolve a field. Returns null on failure, including incompatible class change.
246  // NOTE: Unlike ClassLinker's ResolveField(), this method enforces is_static.
247  ArtField* ResolveField(
248      const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
249      Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
250      uint32_t field_idx, bool is_static)
251      SHARED_REQUIRES(Locks::mutator_lock_);
252
253  // Resolve a field with a given dex file.
254  ArtField* ResolveFieldWithDexFile(
255      const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
256      Handle<mirror::ClassLoader> class_loader, const DexFile* dex_file,
257      uint32_t field_idx, bool is_static)
258      SHARED_REQUIRES(Locks::mutator_lock_);
259
260  // Get declaration location of a resolved field.
261  void GetResolvedFieldDexFileLocation(
262      ArtField* resolved_field, const DexFile** declaring_dex_file,
263      uint16_t* declaring_class_idx, uint16_t* declaring_field_idx)
264      SHARED_REQUIRES(Locks::mutator_lock_);
265
266  bool IsFieldVolatile(ArtField* field) SHARED_REQUIRES(Locks::mutator_lock_);
267  MemberOffset GetFieldOffset(ArtField* field) SHARED_REQUIRES(Locks::mutator_lock_);
268
269  // Find a dex cache for a dex file.
270  inline mirror::DexCache* FindDexCache(const DexFile* dex_file)
271      SHARED_REQUIRES(Locks::mutator_lock_);
272
273  // Can we fast-path an IGET/IPUT access to an instance field? If yes, compute the field offset.
274  std::pair<bool, bool> IsFastInstanceField(
275      mirror::DexCache* dex_cache, mirror::Class* referrer_class,
276      ArtField* resolved_field, uint16_t field_idx)
277      SHARED_REQUIRES(Locks::mutator_lock_);
278
279  // Can we fast-path an SGET/SPUT access to a static field? If yes, compute the type index
280  // of the declaring class in the referrer's dex file.
281  std::pair<bool, bool> IsFastStaticField(
282      mirror::DexCache* dex_cache, mirror::Class* referrer_class,
283      ArtField* resolved_field, uint16_t field_idx, uint32_t* storage_index)
284      SHARED_REQUIRES(Locks::mutator_lock_);
285
286  // Return whether the declaring class of `resolved_method` is
287  // available to `referrer_class`. If this is true, compute the type
288  // index of the declaring class in the referrer's dex file and
289  // return it through the out argument `storage_index`; otherwise
290  // return DexFile::kDexNoIndex through `storage_index`.
291  bool IsClassOfStaticMethodAvailableToReferrer(mirror::DexCache* dex_cache,
292                                                mirror::Class* referrer_class,
293                                                ArtMethod* resolved_method,
294                                                uint16_t method_idx,
295                                                uint32_t* storage_index)
296      SHARED_REQUIRES(Locks::mutator_lock_);
297
298  // Is static field's in referrer's class?
299  bool IsStaticFieldInReferrerClass(mirror::Class* referrer_class, ArtField* resolved_field)
300      SHARED_REQUIRES(Locks::mutator_lock_);
301
302  // Is static field's class initialized?
303  bool IsStaticFieldsClassInitialized(mirror::Class* referrer_class,
304                                      ArtField* resolved_field)
305      SHARED_REQUIRES(Locks::mutator_lock_);
306
307  // Resolve a method. Returns null on failure, including incompatible class change.
308  ArtMethod* ResolveMethod(
309      ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
310      Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
311      uint32_t method_idx, InvokeType invoke_type, bool check_incompatible_class_change = true)
312      SHARED_REQUIRES(Locks::mutator_lock_);
313
314  // Get declaration location of a resolved field.
315  void GetResolvedMethodDexFileLocation(
316      ArtMethod* resolved_method, const DexFile** declaring_dex_file,
317      uint16_t* declaring_class_idx, uint16_t* declaring_method_idx)
318      SHARED_REQUIRES(Locks::mutator_lock_);
319
320  // Get the index in the vtable of the method.
321  uint16_t GetResolvedMethodVTableIndex(
322      ArtMethod* resolved_method, InvokeType type)
323      SHARED_REQUIRES(Locks::mutator_lock_);
324
325  // Can we fast-path an INVOKE? If no, returns 0. If yes, returns a non-zero opaque flags value
326  // for ProcessedInvoke() and computes the necessary lowering info.
327  int IsFastInvoke(
328      ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
329      Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
330      mirror::Class* referrer_class, ArtMethod* resolved_method, InvokeType* invoke_type,
331      MethodReference* target_method, const MethodReference* devirt_target,
332      uintptr_t* direct_code, uintptr_t* direct_method)
333      SHARED_REQUIRES(Locks::mutator_lock_);
334
335  // Is method's class initialized for an invoke?
336  // For static invokes to determine whether we need to consider potential call to <clinit>().
337  // For non-static invokes, assuming a non-null reference, the class is always initialized.
338  bool IsMethodsClassInitialized(mirror::Class* referrer_class, ArtMethod* resolved_method)
339      SHARED_REQUIRES(Locks::mutator_lock_);
340
341  // Get the layout of dex cache arrays for a dex file. Returns invalid layout if the
342  // dex cache arrays don't have a fixed layout.
343  DexCacheArraysLayout GetDexCacheArraysLayout(const DexFile* dex_file);
344
345  void ProcessedInstanceField(bool resolved);
346  void ProcessedStaticField(bool resolved, bool local);
347  void ProcessedInvoke(InvokeType invoke_type, int flags);
348
349  void ComputeFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit,
350                        const ScopedObjectAccess& soa, bool is_static,
351                        ArtField** resolved_field,
352                        mirror::Class** referrer_class,
353                        mirror::DexCache** dex_cache)
354      SHARED_REQUIRES(Locks::mutator_lock_);
355
356  // Can we fast path instance field access? Computes field's offset and volatility.
357  bool ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put,
358                                MemberOffset* field_offset, bool* is_volatile)
359      REQUIRES(!Locks::mutator_lock_);
360
361  ArtField* ComputeInstanceFieldInfo(uint32_t field_idx,
362                                             const DexCompilationUnit* mUnit,
363                                             bool is_put,
364                                             const ScopedObjectAccess& soa)
365      SHARED_REQUIRES(Locks::mutator_lock_);
366
367
368  // Can we fastpath static field access? Computes field's offset, volatility and whether the
369  // field is within the referrer (which can avoid checking class initialization).
370  bool ComputeStaticFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put,
371                              MemberOffset* field_offset, uint32_t* storage_index,
372                              bool* is_referrers_class, bool* is_volatile, bool* is_initialized,
373                              Primitive::Type* type)
374      REQUIRES(!Locks::mutator_lock_);
375
376  // Can we fastpath a interface, super class or virtual method call? Computes method's vtable
377  // index.
378  bool ComputeInvokeInfo(const DexCompilationUnit* mUnit, const uint32_t dex_pc,
379                         bool update_stats, bool enable_devirtualization,
380                         InvokeType* type, MethodReference* target_method, int* vtable_idx,
381                         uintptr_t* direct_code, uintptr_t* direct_method)
382      REQUIRES(!Locks::mutator_lock_);
383
384  const VerifiedMethod* GetVerifiedMethod(const DexFile* dex_file, uint32_t method_idx) const;
385  bool IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc);
386
387  bool GetSupportBootImageFixup() const {
388    return support_boot_image_fixup_;
389  }
390
391  void SetSupportBootImageFixup(bool support_boot_image_fixup) {
392    support_boot_image_fixup_ = support_boot_image_fixup;
393  }
394
395  void SetCompilerContext(void* compiler_context) {
396    compiler_context_ = compiler_context;
397  }
398
399  void* GetCompilerContext() const {
400    return compiler_context_;
401  }
402
403  size_t GetThreadCount() const {
404    return parallel_thread_count_;
405  }
406
407  bool GetDumpStats() const {
408    return dump_stats_;
409  }
410
411  bool GetDumpPasses() const {
412    return dump_passes_;
413  }
414
415  CumulativeLogger* GetTimingsLogger() const {
416    return timings_logger_;
417  }
418
419  void SetDedupeEnabled(bool dedupe_enabled) {
420    compiled_method_storage_.SetDedupeEnabled(dedupe_enabled);
421  }
422  bool DedupeEnabled() const {
423    return compiled_method_storage_.DedupeEnabled();
424  }
425
426  // Checks if class specified by type_idx is one of the image_classes_
427  bool IsImageClass(const char* descriptor) const;
428
429  // Checks whether the provided class should be compiled, i.e., is in classes_to_compile_.
430  bool IsClassToCompile(const char* descriptor) const;
431
432  // Checks whether the provided method should be compiled, i.e., is in method_to_compile_.
433  bool IsMethodToCompile(const MethodReference& method_ref) const;
434
435  // Checks whether profile guided compilation is enabled and if the method should be compiled
436  // according to the profile file.
437  bool ShouldCompileBasedOnProfile(const MethodReference& method_ref) const;
438
439  // Checks whether profile guided verification is enabled and if the method should be verified
440  // according to the profile file.
441  bool ShouldVerifyClassBasedOnProfile(const DexFile& dex_file, uint16_t class_idx) const;
442
443  void RecordClassStatus(ClassReference ref, mirror::Class::Status status)
444      REQUIRES(!compiled_classes_lock_);
445
446  // Checks if the specified method has been verified without failures. Returns
447  // false if the method is not in the verification results (GetVerificationResults).
448  bool IsMethodVerifiedWithoutFailures(uint32_t method_idx,
449                                       uint16_t class_def_idx,
450                                       const DexFile& dex_file) const;
451
452  // Get memory usage during compilation.
453  std::string GetMemoryUsageString(bool extended) const;
454
455  bool IsStringTypeIndex(uint16_t type_index, const DexFile* dex_file);
456  bool IsStringInit(uint32_t method_index, const DexFile* dex_file, int32_t* offset);
457
458  void SetHadHardVerifierFailure() {
459    had_hard_verifier_failure_ = true;
460  }
461
462  Compiler::Kind GetCompilerKind() {
463    return compiler_kind_;
464  }
465
466  CompiledMethodStorage* GetCompiledMethodStorage() {
467    return &compiled_method_storage_;
468  }
469
470  // Can we assume that the klass is loaded?
471  bool CanAssumeClassIsLoaded(mirror::Class* klass)
472      SHARED_REQUIRES(Locks::mutator_lock_);
473
474  bool MayInline(const DexFile* inlined_from, const DexFile* inlined_into) const {
475    if (!kIsTargetBuild) {
476      return MayInlineInternal(inlined_from, inlined_into);
477    }
478    return true;
479  }
480
481 private:
482  // Return whether the declaring class of `resolved_member` is
483  // available to `referrer_class` for read or write access using two
484  // Boolean values returned as a pair. If is true at least for read
485  // access, compute the type index of the declaring class in the
486  // referrer's dex file and return it through the out argument
487  // `storage_index`; otherwise return DexFile::kDexNoIndex through
488  // `storage_index`.
489  template <typename ArtMember>
490  std::pair<bool, bool> IsClassOfStaticMemberAvailableToReferrer(mirror::DexCache* dex_cache,
491                                                                 mirror::Class* referrer_class,
492                                                                 ArtMember* resolved_member,
493                                                                 uint16_t member_idx,
494                                                                 uint32_t* storage_index)
495      SHARED_REQUIRES(Locks::mutator_lock_);
496
497  // Can `referrer_class` access the resolved `member`?
498  // Dispatch call to mirror::Class::CanAccessResolvedField or
499  // mirror::Class::CanAccessResolvedMember depending on the value of
500  // ArtMember.
501  template <typename ArtMember>
502  static bool CanAccessResolvedMember(mirror::Class* referrer_class,
503                                      mirror::Class* access_to,
504                                      ArtMember* member,
505                                      mirror::DexCache* dex_cache,
506                                      uint32_t field_idx)
507      SHARED_REQUIRES(Locks::mutator_lock_);
508
509  // Can we assume that the klass is initialized?
510  bool CanAssumeClassIsInitialized(mirror::Class* klass)
511      SHARED_REQUIRES(Locks::mutator_lock_);
512  bool CanReferrerAssumeClassIsInitialized(mirror::Class* referrer_class, mirror::Class* klass)
513      SHARED_REQUIRES(Locks::mutator_lock_);
514
515  // These flags are internal to CompilerDriver for collecting INVOKE resolution statistics.
516  // The only external contract is that unresolved method has flags 0 and resolved non-0.
517  enum {
518    kBitMethodResolved = 0,
519    kBitVirtualMadeDirect,
520    kBitPreciseTypeDevirtualization,
521    kBitDirectCallToBoot,
522    kBitDirectMethodToBoot
523  };
524  static constexpr int kFlagMethodResolved              = 1 << kBitMethodResolved;
525  static constexpr int kFlagVirtualMadeDirect           = 1 << kBitVirtualMadeDirect;
526  static constexpr int kFlagPreciseTypeDevirtualization = 1 << kBitPreciseTypeDevirtualization;
527  static constexpr int kFlagDirectCallToBoot            = 1 << kBitDirectCallToBoot;
528  static constexpr int kFlagDirectMethodToBoot          = 1 << kBitDirectMethodToBoot;
529  static constexpr int kFlagsMethodResolvedVirtualMadeDirect =
530      kFlagMethodResolved | kFlagVirtualMadeDirect;
531  static constexpr int kFlagsMethodResolvedPreciseTypeDevirtualization =
532      kFlagsMethodResolvedVirtualMadeDirect | kFlagPreciseTypeDevirtualization;
533
534 public:  // TODO make private or eliminate.
535  // Compute constant code and method pointers when possible.
536  void GetCodeAndMethodForDirectCall(/*out*/InvokeType* type,
537                                     InvokeType sharp_type,
538                                     bool no_guarantee_of_dex_cache_entry,
539                                     const mirror::Class* referrer_class,
540                                     ArtMethod* method,
541                                     /*out*/int* stats_flags,
542                                     MethodReference* target_method,
543                                     uintptr_t* direct_code, uintptr_t* direct_method)
544      SHARED_REQUIRES(Locks::mutator_lock_);
545
546 private:
547  void PreCompile(jobject class_loader,
548                  const std::vector<const DexFile*>& dex_files,
549                  TimingLogger* timings)
550      REQUIRES(!Locks::mutator_lock_, !compiled_classes_lock_);
551
552  void LoadImageClasses(TimingLogger* timings) REQUIRES(!Locks::mutator_lock_);
553
554  // Attempt to resolve all type, methods, fields, and strings
555  // referenced from code in the dex file following PathClassLoader
556  // ordering semantics.
557  void Resolve(jobject class_loader,
558               const std::vector<const DexFile*>& dex_files,
559               TimingLogger* timings)
560      REQUIRES(!Locks::mutator_lock_);
561  void ResolveDexFile(jobject class_loader,
562                      const DexFile& dex_file,
563                      const std::vector<const DexFile*>& dex_files,
564                      ThreadPool* thread_pool,
565                      size_t thread_count,
566                      TimingLogger* timings)
567      REQUIRES(!Locks::mutator_lock_);
568
569  void Verify(jobject class_loader,
570              const std::vector<const DexFile*>& dex_files,
571              TimingLogger* timings);
572  void VerifyDexFile(jobject class_loader,
573                     const DexFile& dex_file,
574                     const std::vector<const DexFile*>& dex_files,
575                     ThreadPool* thread_pool,
576                     size_t thread_count,
577                     TimingLogger* timings)
578      REQUIRES(!Locks::mutator_lock_);
579
580  void SetVerified(jobject class_loader,
581                   const std::vector<const DexFile*>& dex_files,
582                   TimingLogger* timings);
583  void SetVerifiedDexFile(jobject class_loader,
584                          const DexFile& dex_file,
585                          const std::vector<const DexFile*>& dex_files,
586                          ThreadPool* thread_pool,
587                          size_t thread_count,
588                          TimingLogger* timings)
589      REQUIRES(!Locks::mutator_lock_);
590
591  void InitializeClasses(jobject class_loader,
592                         const std::vector<const DexFile*>& dex_files,
593                         TimingLogger* timings)
594      REQUIRES(!Locks::mutator_lock_, !compiled_classes_lock_);
595  void InitializeClasses(jobject class_loader,
596                         const DexFile& dex_file,
597                         const std::vector<const DexFile*>& dex_files,
598                         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,
606               const std::vector<const DexFile*>& dex_files,
607               TimingLogger* timings);
608  void CompileDexFile(jobject class_loader,
609                      const DexFile& dex_file,
610                      const std::vector<const DexFile*>& dex_files,
611                      ThreadPool* thread_pool,
612                      size_t thread_count,
613                      TimingLogger* timings)
614      REQUIRES(!Locks::mutator_lock_);
615
616  bool MayInlineInternal(const DexFile* inlined_from, const DexFile* inlined_into) const;
617
618  void InitializeThreadPools();
619  void FreeThreadPools();
620  void CheckThreadPools();
621
622  bool RequiresConstructorBarrier(const DexFile& dex_file, uint16_t class_def_idx) const;
623
624  const CompilerOptions* const compiler_options_;
625  VerificationResults* const verification_results_;
626  DexFileToMethodInlinerMap* const method_inliner_map_;
627
628  std::unique_ptr<Compiler> compiler_;
629  Compiler::Kind compiler_kind_;
630
631  const InstructionSet instruction_set_;
632  const InstructionSetFeatures* const instruction_set_features_;
633
634  // All class references that do not require constructor barriers. Only filled in if
635  // resolved_classes_ is true.
636  mutable ReaderWriterMutex no_barrier_constructor_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
637  std::set<ClassReference> no_barrier_constructor_classes_
638      GUARDED_BY(no_barrier_constructor_classes_lock_);
639  // resolved_classes_ is true if we performed the resolve phase and filled in
640  // no_barrier_constructor_classes_.
641  bool resolved_classes_;
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