compiler_driver.h revision 956af0f0cb05422e38c1d22cbef309d16b8a1a12
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 <vector>
23
24#include "arch/instruction_set.h"
25#include "base/mutex.h"
26#include "base/timing_logger.h"
27#include "class_reference.h"
28#include "compiled_method.h"
29#include "compiler.h"
30#include "dex_file.h"
31#include "driver/compiler_options.h"
32#include "invoke_type.h"
33#include "method_reference.h"
34#include "mirror/class.h"  // For mirror::Class::Status.
35#include "os.h"
36#include "profiler.h"
37#include "runtime.h"
38#include "safe_map.h"
39#include "thread_pool.h"
40#include "utils/arena_allocator.h"
41#include "utils/dedupe_set.h"
42#include "dex/verified_method.h"
43
44namespace art {
45
46namespace verifier {
47class MethodVerifier;
48}  // namespace verifier
49
50class CompiledClass;
51class CompilerOptions;
52class DexCompilationUnit;
53class DexFileToMethodInlinerMap;
54struct InlineIGetIPutData;
55class InstructionSetFeatures;
56class OatWriter;
57class ParallelCompilationManager;
58class ScopedObjectAccess;
59template<class T> class Handle;
60class TimingLogger;
61class VerificationResults;
62class VerifiedMethod;
63
64enum EntryPointCallingConvention {
65  // ABI of invocations to a method's interpreter entry point.
66  kInterpreterAbi,
67  // ABI of calls to a method's native code, only used for native methods.
68  kJniAbi,
69  // ABI of calls to a method's quick code entry point.
70  kQuickAbi
71};
72
73enum DexToDexCompilationLevel {
74  kDontDexToDexCompile,   // Only meaning wrt image time interpretation.
75  kRequired,              // Dex-to-dex compilation required for correctness.
76  kOptimize               // Perform required transformation and peep-hole optimizations.
77};
78std::ostream& operator<<(std::ostream& os, const DexToDexCompilationLevel& rhs);
79
80class CompilerDriver {
81 public:
82  // Create a compiler targeting the requested "instruction_set".
83  // "image" should be true if image specific optimizations should be
84  // enabled.  "image_classes" lets the compiler know what classes it
85  // can assume will be in the image, with nullptr implying all available
86  // classes.
87  explicit CompilerDriver(const CompilerOptions* compiler_options,
88                          VerificationResults* verification_results,
89                          DexFileToMethodInlinerMap* method_inliner_map,
90                          Compiler::Kind compiler_kind,
91                          InstructionSet instruction_set,
92                          const InstructionSetFeatures* instruction_set_features,
93                          bool image, std::set<std::string>* image_classes,
94                          std::set<std::string>* compiled_classes,
95                          size_t thread_count, bool dump_stats, bool dump_passes,
96                          CumulativeLogger* timer, const std::string& profile_file);
97
98  ~CompilerDriver();
99
100  void CompileAll(jobject class_loader, const std::vector<const DexFile*>& dex_files,
101                  TimingLogger* timings)
102      LOCKS_EXCLUDED(Locks::mutator_lock_);
103
104  // Compile a single Method.
105  void CompileOne(mirror::ArtMethod* method, TimingLogger* timings)
106      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
107
108  VerificationResults* GetVerificationResults() const {
109    return verification_results_;
110  }
111
112  DexFileToMethodInlinerMap* GetMethodInlinerMap() const {
113    return method_inliner_map_;
114  }
115
116  InstructionSet GetInstructionSet() const {
117    return instruction_set_;
118  }
119
120  const InstructionSetFeatures* GetInstructionSetFeatures() const {
121    return instruction_set_features_;
122  }
123
124  const CompilerOptions& GetCompilerOptions() const {
125    return *compiler_options_;
126  }
127
128  Compiler* GetCompiler() const {
129    return compiler_.get();
130  }
131
132  bool ProfilePresent() const {
133    return profile_present_;
134  }
135
136  // Are we compiling and creating an image file?
137  bool IsImage() const {
138    return image_;
139  }
140
141  const std::set<std::string>* GetImageClasses() const {
142    return image_classes_.get();
143  }
144
145  // Generate the trampolines that are invoked by unresolved direct methods.
146  const std::vector<uint8_t>* CreateInterpreterToInterpreterBridge() const
147      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
148  const std::vector<uint8_t>* CreateInterpreterToCompiledCodeBridge() const
149      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
150  const std::vector<uint8_t>* CreateJniDlsymLookup() const
151      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
152  const std::vector<uint8_t>* CreateQuickGenericJniTrampoline() const
153      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
154  const std::vector<uint8_t>* CreateQuickImtConflictTrampoline() const
155      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
156  const std::vector<uint8_t>* CreateQuickResolutionTrampoline() const
157      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
158  const std::vector<uint8_t>* CreateQuickToInterpreterBridge() const
159      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
160
161  CompiledClass* GetCompiledClass(ClassReference ref) const
162      LOCKS_EXCLUDED(compiled_classes_lock_);
163
164  CompiledMethod* GetCompiledMethod(MethodReference ref) const
165      LOCKS_EXCLUDED(compiled_methods_lock_);
166  size_t GetNonRelativeLinkerPatchCount() const
167      LOCKS_EXCLUDED(compiled_methods_lock_);
168
169  void AddRequiresConstructorBarrier(Thread* self, const DexFile* dex_file,
170                                     uint16_t class_def_index);
171  bool RequiresConstructorBarrier(Thread* self, const DexFile* dex_file, uint16_t class_def_index);
172
173  // Callbacks from compiler to see what runtime checks must be generated.
174
175  bool CanAssumeTypeIsPresentInDexCache(const DexFile& dex_file, uint32_t type_idx);
176
177  bool CanAssumeStringIsPresentInDexCache(const DexFile& dex_file, uint32_t string_idx)
178      LOCKS_EXCLUDED(Locks::mutator_lock_);
179
180  // Are runtime access checks necessary in the compiled code?
181  bool CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file,
182                                  uint32_t type_idx, bool* type_known_final = nullptr,
183                                  bool* type_known_abstract = nullptr,
184                                  bool* equals_referrers_class = nullptr)
185      LOCKS_EXCLUDED(Locks::mutator_lock_);
186
187  // Are runtime access and instantiable checks necessary in the code?
188  bool CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file,
189                                              uint32_t type_idx)
190     LOCKS_EXCLUDED(Locks::mutator_lock_);
191
192  bool CanEmbedTypeInCode(const DexFile& dex_file, uint32_t type_idx,
193                          bool* is_type_initialized, bool* use_direct_type_ptr,
194                          uintptr_t* direct_type_ptr, bool* out_is_finalizable);
195
196  // Query methods for the java.lang.ref.Reference class.
197  bool CanEmbedReferenceTypeInCode(ClassReference* ref,
198                                   bool* use_direct_type_ptr, uintptr_t* direct_type_ptr);
199  uint32_t GetReferenceSlowFlagOffset() const;
200  uint32_t GetReferenceDisableFlagOffset() const;
201
202  // Get the DexCache for the
203  mirror::DexCache* GetDexCache(const DexCompilationUnit* mUnit)
204    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
205
206  mirror::ClassLoader* GetClassLoader(ScopedObjectAccess& soa, const DexCompilationUnit* mUnit)
207    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
208
209  // Resolve compiling method's class. Returns nullptr on failure.
210  mirror::Class* ResolveCompilingMethodsClass(
211      const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
212      Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit)
213    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
214
215  // Resolve a field. Returns nullptr on failure, including incompatible class change.
216  // NOTE: Unlike ClassLinker's ResolveField(), this method enforces is_static.
217  mirror::ArtField* ResolveField(
218      const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
219      Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
220      uint32_t field_idx, bool is_static)
221    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
222
223  // Get declaration location of a resolved field.
224  void GetResolvedFieldDexFileLocation(
225      mirror::ArtField* resolved_field, const DexFile** declaring_dex_file,
226      uint16_t* declaring_class_idx, uint16_t* declaring_field_idx)
227    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
228
229  bool IsFieldVolatile(mirror::ArtField* field) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
230  MemberOffset GetFieldOffset(mirror::ArtField* field) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
231
232  // Can we fast-path an IGET/IPUT access to an instance field? If yes, compute the field offset.
233  std::pair<bool, bool> IsFastInstanceField(
234      mirror::DexCache* dex_cache, mirror::Class* referrer_class,
235      mirror::ArtField* resolved_field, uint16_t field_idx)
236    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
237
238  // Can we fast-path an SGET/SPUT access to a static field? If yes, compute the type index
239  // of the declaring class in the referrer's dex file.
240  std::pair<bool, bool> IsFastStaticField(
241      mirror::DexCache* dex_cache, mirror::Class* referrer_class,
242      mirror::ArtField* resolved_field, uint16_t field_idx, uint32_t* storage_index)
243    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
244
245  // Is static field's in referrer's class?
246  bool IsStaticFieldInReferrerClass(mirror::Class* referrer_class, mirror::ArtField* resolved_field)
247    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
248
249  // Is static field's class initialized?
250  bool IsStaticFieldsClassInitialized(mirror::Class* referrer_class,
251                                      mirror::ArtField* resolved_field)
252    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
253
254  // Resolve a method. Returns nullptr on failure, including incompatible class change.
255  mirror::ArtMethod* ResolveMethod(
256      ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
257      Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
258      uint32_t method_idx, InvokeType invoke_type)
259    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
260
261  // Get declaration location of a resolved field.
262  void GetResolvedMethodDexFileLocation(
263      mirror::ArtMethod* resolved_method, const DexFile** declaring_dex_file,
264      uint16_t* declaring_class_idx, uint16_t* declaring_method_idx)
265    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
266
267  // Get the index in the vtable of the method.
268  uint16_t GetResolvedMethodVTableIndex(
269      mirror::ArtMethod* resolved_method, InvokeType type)
270    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
271
272  // Can we fast-path an INVOKE? If no, returns 0. If yes, returns a non-zero opaque flags value
273  // for ProcessedInvoke() and computes the necessary lowering info.
274  int IsFastInvoke(
275      ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
276      Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
277      mirror::Class* referrer_class, mirror::ArtMethod* resolved_method, InvokeType* invoke_type,
278      MethodReference* target_method, const MethodReference* devirt_target,
279      uintptr_t* direct_code, uintptr_t* direct_method)
280    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
281
282  // Is method's class initialized for an invoke?
283  // For static invokes to determine whether we need to consider potential call to <clinit>().
284  // For non-static invokes, assuming a non-null reference, the class is always initialized.
285  bool IsMethodsClassInitialized(mirror::Class* referrer_class, mirror::ArtMethod* resolved_method)
286    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
287
288  void ProcessedInstanceField(bool resolved);
289  void ProcessedStaticField(bool resolved, bool local);
290  void ProcessedInvoke(InvokeType invoke_type, int flags);
291
292  // Can we fast path instance field access? Computes field's offset and volatility.
293  bool ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put,
294                                MemberOffset* field_offset, bool* is_volatile)
295      LOCKS_EXCLUDED(Locks::mutator_lock_);
296
297  mirror::ArtField* ComputeInstanceFieldInfo(uint32_t field_idx,
298                                             const DexCompilationUnit* mUnit,
299                                             bool is_put,
300                                             const ScopedObjectAccess& soa)
301      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
302
303
304  // Can we fastpath static field access? Computes field's offset, volatility and whether the
305  // field is within the referrer (which can avoid checking class initialization).
306  bool ComputeStaticFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put,
307                              MemberOffset* field_offset, uint32_t* storage_index,
308                              bool* is_referrers_class, bool* is_volatile, bool* is_initialized,
309                              Primitive::Type* type)
310      LOCKS_EXCLUDED(Locks::mutator_lock_);
311
312  // Can we fastpath a interface, super class or virtual method call? Computes method's vtable
313  // index.
314  bool ComputeInvokeInfo(const DexCompilationUnit* mUnit, const uint32_t dex_pc,
315                         bool update_stats, bool enable_devirtualization,
316                         InvokeType* type, MethodReference* target_method, int* vtable_idx,
317                         uintptr_t* direct_code, uintptr_t* direct_method)
318      LOCKS_EXCLUDED(Locks::mutator_lock_);
319
320  const VerifiedMethod* GetVerifiedMethod(const DexFile* dex_file, uint32_t method_idx) const;
321  bool IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc);
322
323  bool GetSupportBootImageFixup() const {
324    return support_boot_image_fixup_;
325  }
326
327  void SetSupportBootImageFixup(bool support_boot_image_fixup) {
328    support_boot_image_fixup_ = support_boot_image_fixup;
329  }
330
331  ArenaPool* GetArenaPool() {
332    return &arena_pool_;
333  }
334  const ArenaPool* GetArenaPool() const {
335    return &arena_pool_;
336  }
337
338  bool WriteElf(const std::string& android_root,
339                bool is_host,
340                const std::vector<const DexFile*>& dex_files,
341                OatWriter* oat_writer,
342                File* file);
343
344  // TODO: move to a common home for llvm helpers once quick/portable are merged.
345  static void InstructionSetToLLVMTarget(InstructionSet instruction_set,
346                                         std::string* target_triple,
347                                         std::string* target_cpu,
348                                         std::string* target_attr);
349
350  void SetCompilerContext(void* compiler_context) {
351    compiler_context_ = compiler_context;
352  }
353
354  void* GetCompilerContext() const {
355    return compiler_context_;
356  }
357
358  size_t GetThreadCount() const {
359    return thread_count_;
360  }
361
362  bool GetDumpPasses() const {
363    return dump_passes_;
364  }
365
366  CumulativeLogger* GetTimingsLogger() const {
367    return timings_logger_;
368  }
369
370  // Checks if class specified by type_idx is one of the image_classes_
371  bool IsImageClass(const char* descriptor) const;
372
373  // Checks if the provided class should be compiled, i.e., is in classes_to_compile_.
374  bool IsClassToCompile(const char* descriptor) const;
375
376  void RecordClassStatus(ClassReference ref, mirror::Class::Status status)
377      LOCKS_EXCLUDED(compiled_classes_lock_);
378
379  std::vector<uint8_t>* DeduplicateCode(const std::vector<uint8_t>& code);
380  SrcMap* DeduplicateSrcMappingTable(const SrcMap& src_map);
381  std::vector<uint8_t>* DeduplicateMappingTable(const std::vector<uint8_t>& code);
382  std::vector<uint8_t>* DeduplicateVMapTable(const std::vector<uint8_t>& code);
383  std::vector<uint8_t>* DeduplicateGCMap(const std::vector<uint8_t>& code);
384  std::vector<uint8_t>* DeduplicateCFIInfo(const std::vector<uint8_t>* cfi_info);
385
386  ProfileFile profile_file_;
387  bool profile_present_;
388
389  // Should the compiler run on this method given profile information?
390  bool SkipCompilation(const std::string& method_name);
391
392  // Get memory usage during compilation.
393  std::string GetMemoryUsageString() const;
394
395 private:
396  // These flags are internal to CompilerDriver for collecting INVOKE resolution statistics.
397  // The only external contract is that unresolved method has flags 0 and resolved non-0.
398  enum {
399    kBitMethodResolved = 0,
400    kBitVirtualMadeDirect,
401    kBitPreciseTypeDevirtualization,
402    kBitDirectCallToBoot,
403    kBitDirectMethodToBoot
404  };
405  static constexpr int kFlagMethodResolved              = 1 << kBitMethodResolved;
406  static constexpr int kFlagVirtualMadeDirect           = 1 << kBitVirtualMadeDirect;
407  static constexpr int kFlagPreciseTypeDevirtualization = 1 << kBitPreciseTypeDevirtualization;
408  static constexpr int kFlagDirectCallToBoot            = 1 << kBitDirectCallToBoot;
409  static constexpr int kFlagDirectMethodToBoot          = 1 << kBitDirectMethodToBoot;
410  static constexpr int kFlagsMethodResolvedVirtualMadeDirect =
411      kFlagMethodResolved | kFlagVirtualMadeDirect;
412  static constexpr int kFlagsMethodResolvedPreciseTypeDevirtualization =
413      kFlagsMethodResolvedVirtualMadeDirect | kFlagPreciseTypeDevirtualization;
414
415 public:  // TODO make private or eliminate.
416  // Compute constant code and method pointers when possible.
417  void GetCodeAndMethodForDirectCall(/*out*/InvokeType* type,
418                                     InvokeType sharp_type,
419                                     bool no_guarantee_of_dex_cache_entry,
420                                     const mirror::Class* referrer_class,
421                                     mirror::ArtMethod* method,
422                                     /*out*/int* stats_flags,
423                                     MethodReference* target_method,
424                                     uintptr_t* direct_code, uintptr_t* direct_method)
425      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
426
427 private:
428  void PreCompile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
429                  ThreadPool* thread_pool, TimingLogger* timings)
430      LOCKS_EXCLUDED(Locks::mutator_lock_);
431
432  void LoadImageClasses(TimingLogger* timings);
433
434  // Attempt to resolve all type, methods, fields, and strings
435  // referenced from code in the dex file following PathClassLoader
436  // ordering semantics.
437  void Resolve(jobject class_loader, const std::vector<const DexFile*>& dex_files,
438               ThreadPool* thread_pool, TimingLogger* timings)
439      LOCKS_EXCLUDED(Locks::mutator_lock_);
440  void ResolveDexFile(jobject class_loader, const DexFile& dex_file,
441                      const std::vector<const DexFile*>& dex_files,
442                      ThreadPool* thread_pool, TimingLogger* timings)
443      LOCKS_EXCLUDED(Locks::mutator_lock_);
444
445  void Verify(jobject class_loader, const std::vector<const DexFile*>& dex_files,
446              ThreadPool* thread_pool, TimingLogger* timings);
447  void VerifyDexFile(jobject class_loader, const DexFile& dex_file,
448                     const std::vector<const DexFile*>& dex_files,
449                     ThreadPool* thread_pool, TimingLogger* timings)
450      LOCKS_EXCLUDED(Locks::mutator_lock_);
451
452  void SetVerified(jobject class_loader, const std::vector<const DexFile*>& dex_files,
453                   ThreadPool* thread_pool, TimingLogger* timings);
454  void SetVerifiedDexFile(jobject class_loader, const DexFile& dex_file,
455                          const std::vector<const DexFile*>& dex_files,
456                          ThreadPool* thread_pool, TimingLogger* timings)
457      LOCKS_EXCLUDED(Locks::mutator_lock_);
458
459  void InitializeClasses(jobject class_loader, const std::vector<const DexFile*>& dex_files,
460                         ThreadPool* thread_pool, TimingLogger* timings)
461      LOCKS_EXCLUDED(Locks::mutator_lock_);
462  void InitializeClasses(jobject class_loader, const DexFile& dex_file,
463                         const std::vector<const DexFile*>& dex_files,
464                         ThreadPool* thread_pool, TimingLogger* timings)
465      LOCKS_EXCLUDED(Locks::mutator_lock_, compiled_classes_lock_);
466
467  void UpdateImageClasses(TimingLogger* timings) LOCKS_EXCLUDED(Locks::mutator_lock_);
468  static void FindClinitImageClassesCallback(mirror::Object* object, void* arg)
469      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
470
471  void Compile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
472               ThreadPool* thread_pool, TimingLogger* timings);
473  void CompileDexFile(jobject class_loader, const DexFile& dex_file,
474                      const std::vector<const DexFile*>& dex_files,
475                      ThreadPool* thread_pool, TimingLogger* timings)
476      LOCKS_EXCLUDED(Locks::mutator_lock_);
477  void CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
478                     InvokeType invoke_type, uint16_t class_def_idx, uint32_t method_idx,
479                     jobject class_loader, const DexFile& dex_file,
480                     DexToDexCompilationLevel dex_to_dex_compilation_level,
481                     bool compilation_enabled)
482      LOCKS_EXCLUDED(compiled_methods_lock_);
483
484  static void CompileClass(const ParallelCompilationManager* context, size_t class_def_index)
485      LOCKS_EXCLUDED(Locks::mutator_lock_);
486
487  const CompilerOptions* const compiler_options_;
488  VerificationResults* const verification_results_;
489  DexFileToMethodInlinerMap* const method_inliner_map_;
490
491  std::unique_ptr<Compiler> compiler_;
492
493  const InstructionSet instruction_set_;
494  const InstructionSetFeatures* const instruction_set_features_;
495
496  // All class references that require
497  mutable ReaderWriterMutex freezing_constructor_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
498  std::set<ClassReference> freezing_constructor_classes_ GUARDED_BY(freezing_constructor_lock_);
499
500  typedef SafeMap<const ClassReference, CompiledClass*> ClassTable;
501  // All class references that this compiler has compiled.
502  mutable Mutex compiled_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
503  ClassTable compiled_classes_ GUARDED_BY(compiled_classes_lock_);
504
505  typedef SafeMap<const MethodReference, CompiledMethod*, MethodReferenceComparator> MethodTable;
506  // All method references that this compiler has compiled.
507  mutable Mutex compiled_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
508  MethodTable compiled_methods_ GUARDED_BY(compiled_methods_lock_);
509  // Number of non-relative patches in all compiled methods. These patches need space
510  // in the .oat_patches ELF section if requested in the compiler options.
511  size_t non_relative_linker_patch_count_ GUARDED_BY(compiled_methods_lock_);
512
513  const bool image_;
514
515  // If image_ is true, specifies the classes that will be included in
516  // the image. Note if image_classes_ is nullptr, all classes are
517  // included in the image.
518  std::unique_ptr<std::set<std::string>> image_classes_;
519
520  // If image_ is true, specifies the classes that will be compiled in
521  // the image. Note if classes_to_compile_ is nullptr, all classes are
522  // included in the image.
523  std::unique_ptr<std::set<std::string>> classes_to_compile_;
524
525  size_t thread_count_;
526
527  class AOTCompilationStats;
528  std::unique_ptr<AOTCompilationStats> stats_;
529
530  bool dump_stats_;
531  const bool dump_passes_;
532
533  CumulativeLogger* const timings_logger_;
534
535  typedef void (*CompilerCallbackFn)(CompilerDriver& driver);
536  typedef MutexLock* (*CompilerMutexLockFn)(CompilerDriver& driver);
537
538  typedef void (*DexToDexCompilerFn)(CompilerDriver& driver,
539                                     const DexFile::CodeItem* code_item,
540                                     uint32_t access_flags, InvokeType invoke_type,
541                                     uint32_t class_dex_idx, uint32_t method_idx,
542                                     jobject class_loader, const DexFile& dex_file,
543                                     DexToDexCompilationLevel dex_to_dex_compilation_level);
544  DexToDexCompilerFn dex_to_dex_compiler_;
545
546  void* compiler_context_;
547
548  // Arena pool used by the compiler.
549  ArenaPool arena_pool_;
550
551  bool support_boot_image_fixup_;
552
553  // DeDuplication data structures, these own the corresponding byte arrays.
554  template <typename ByteArray>
555  class DedupeHashFunc {
556   public:
557    size_t operator()(const ByteArray& array) const {
558      // For small arrays compute a hash using every byte.
559      static const size_t kSmallArrayThreshold = 16;
560      size_t hash = 0x811c9dc5;
561      if (array.size() <= kSmallArrayThreshold) {
562        for (auto b : array) {
563          hash = (hash * 16777619) ^ static_cast<uint8_t>(b);
564        }
565      } else {
566        // For larger arrays use the 2 bytes at 6 bytes (the location of a push registers
567        // instruction field for quick generated code on ARM) and then select a number of other
568        // values at random.
569        static const size_t kRandomHashCount = 16;
570        for (size_t i = 0; i < 2; ++i) {
571          uint8_t b = static_cast<uint8_t>(array[i + 6]);
572          hash = (hash * 16777619) ^ b;
573        }
574        for (size_t i = 2; i < kRandomHashCount; ++i) {
575          size_t r = i * 1103515245 + 12345;
576          uint8_t b = static_cast<uint8_t>(array[r % array.size()]);
577          hash = (hash * 16777619) ^ b;
578        }
579      }
580      hash += hash << 13;
581      hash ^= hash >> 7;
582      hash += hash << 3;
583      hash ^= hash >> 17;
584      hash += hash << 5;
585      return hash;
586    }
587  };
588
589  DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc<std::vector<uint8_t>>, 4> dedupe_code_;
590  DedupeSet<SrcMap, size_t, DedupeHashFunc<SrcMap>, 4> dedupe_src_mapping_table_;
591  DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc<std::vector<uint8_t>>, 4> dedupe_mapping_table_;
592  DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc<std::vector<uint8_t>>, 4> dedupe_vmap_table_;
593  DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc<std::vector<uint8_t>>, 4> dedupe_gc_map_;
594  DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc<std::vector<uint8_t>>, 4> dedupe_cfi_info_;
595
596  DISALLOW_COPY_AND_ASSIGN(CompilerDriver);
597};
598
599}  // namespace art
600
601#endif  // ART_COMPILER_DRIVER_COMPILER_DRIVER_H_
602