compiler_driver.h revision 818f2107e6d2d9e80faac8ae8c92faffa83cbd11
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 "base/mutex.h"
25#include "base/timing_logger.h"
26#include "class_reference.h"
27#include "compiled_class.h"
28#include "compiled_method.h"
29#include "compiler_backend.h"
30#include "dex_file.h"
31#include "instruction_set.h"
32#include "invoke_type.h"
33#include "method_reference.h"
34#include "os.h"
35#include "runtime.h"
36#include "safe_map.h"
37#include "thread_pool.h"
38#include "utils/arena_allocator.h"
39#include "utils/dedupe_set.h"
40
41namespace art {
42
43namespace verifier {
44class MethodVerifier;
45}  // namespace verifier
46
47class AOTCompilationStats;
48class DexCompilationUnit;
49class DexFileToMethodInlinerMap;
50class InlineIGetIPutData;
51class OatWriter;
52class ParallelCompilationManager;
53class TimingLogger;
54class VerificationResults;
55class VerifiedMethod;
56
57enum EntryPointCallingConvention {
58  // ABI of invocations to a method's interpreter entry point.
59  kInterpreterAbi,
60  // ABI of calls to a method's native code, only used for native methods.
61  kJniAbi,
62  // ABI of calls to a method's portable code entry point.
63  kPortableAbi,
64  // ABI of calls to a method's quick code entry point.
65  kQuickAbi
66};
67
68enum DexToDexCompilationLevel {
69  kDontDexToDexCompile,   // Only meaning wrt image time interpretation.
70  kRequired,              // Dex-to-dex compilation required for correctness.
71  kOptimize               // Perform required transformation and peep-hole optimizations.
72};
73
74// Thread-local storage compiler worker threads
75class CompilerTls {
76  public:
77    CompilerTls() : llvm_info_(NULL) {}
78    ~CompilerTls() {}
79
80    void* GetLLVMInfo() { return llvm_info_; }
81
82    void SetLLVMInfo(void* llvm_info) { llvm_info_ = llvm_info; }
83
84  private:
85    void* llvm_info_;
86};
87
88class CompilerDriver {
89 public:
90  typedef std::set<std::string> DescriptorSet;
91
92  // Create a compiler targeting the requested "instruction_set".
93  // "image" should be true if image specific optimizations should be
94  // enabled.  "image_classes" lets the compiler know what classes it
95  // can assume will be in the image, with NULL implying all available
96  // classes.
97  explicit CompilerDriver(VerificationResults* verification_results,
98                          DexFileToMethodInlinerMap* method_inliner_map,
99                          CompilerBackend::Kind compiler_backend_kind,
100                          InstructionSet instruction_set,
101                          InstructionSetFeatures instruction_set_features,
102                          bool image, DescriptorSet* image_classes,
103                          size_t thread_count, bool dump_stats, bool dump_passes,
104                          CumulativeLogger* timer);
105
106  ~CompilerDriver();
107
108  void CompileAll(jobject class_loader, const std::vector<const DexFile*>& dex_files,
109                  TimingLogger& timings)
110      LOCKS_EXCLUDED(Locks::mutator_lock_);
111
112  // Compile a single Method.
113  void CompileOne(mirror::ArtMethod* method, TimingLogger& timings)
114      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
115
116  VerificationResults* GetVerificationResults() const {
117    return verification_results_;
118  }
119
120  DexFileToMethodInlinerMap* GetMethodInlinerMap() const {
121    return method_inliner_map_;
122  }
123
124  const InstructionSet& GetInstructionSet() const {
125    return instruction_set_;
126  }
127
128  const InstructionSetFeatures& GetInstructionSetFeatures() const {
129    return instruction_set_features_;
130  }
131
132  CompilerBackend* GetCompilerBackend() const {
133    return compiler_backend_.get();
134  }
135
136  // Are we compiling and creating an image file?
137  bool IsImage() const {
138    return image_;
139  }
140
141  DescriptorSet* GetImageClasses() const {
142    return image_classes_.get();
143  }
144
145  CompilerTls* GetTls();
146
147  // Generate the trampolines that are invoked by unresolved direct methods.
148  const std::vector<uint8_t>* CreateInterpreterToInterpreterBridge() const
149      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
150  const std::vector<uint8_t>* CreateInterpreterToCompiledCodeBridge() const
151      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
152  const std::vector<uint8_t>* CreateJniDlsymLookup() const
153      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
154  const std::vector<uint8_t>* CreatePortableImtConflictTrampoline() const
155      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
156  const std::vector<uint8_t>* CreatePortableResolutionTrampoline() const
157      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
158  const std::vector<uint8_t>* CreatePortableToInterpreterBridge() const
159      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
160  const std::vector<uint8_t>* CreateQuickImtConflictTrampoline() const
161      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
162  const std::vector<uint8_t>* CreateQuickResolutionTrampoline() const
163      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
164  const std::vector<uint8_t>* CreateQuickToInterpreterBridge() const
165      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
166
167  CompiledClass* GetCompiledClass(ClassReference ref) const
168      LOCKS_EXCLUDED(compiled_classes_lock_);
169
170  CompiledMethod* GetCompiledMethod(MethodReference ref) const
171      LOCKS_EXCLUDED(compiled_methods_lock_);
172
173  void AddRequiresConstructorBarrier(Thread* self, const DexFile* dex_file,
174                                     uint16_t class_def_index);
175  bool RequiresConstructorBarrier(Thread* self, const DexFile* dex_file, uint16_t class_def_index);
176
177  // Callbacks from compiler to see what runtime checks must be generated.
178
179  bool CanAssumeTypeIsPresentInDexCache(const DexFile& dex_file, uint32_t type_idx);
180
181  bool CanAssumeStringIsPresentInDexCache(const DexFile& dex_file, uint32_t string_idx)
182      LOCKS_EXCLUDED(Locks::mutator_lock_);
183
184  // Are runtime access checks necessary in the compiled code?
185  bool CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file,
186                                  uint32_t type_idx, bool* type_known_final = NULL,
187                                  bool* type_known_abstract = NULL,
188                                  bool* equals_referrers_class = NULL)
189      LOCKS_EXCLUDED(Locks::mutator_lock_);
190
191  // Are runtime access and instantiable checks necessary in the code?
192  bool CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file,
193                                              uint32_t type_idx)
194     LOCKS_EXCLUDED(Locks::mutator_lock_);
195
196  bool CanEmbedTypeInCode(const DexFile& dex_file, uint32_t type_idx,
197                          bool* is_type_initialized, bool* use_direct_type_ptr,
198                          uintptr_t* direct_type_ptr);
199
200  // Can we fast path instance field access in a verified accessor?
201  // If yes, computes field's offset and volatility and whether the method is static or not.
202  static bool ComputeSpecialAccessorInfo(uint32_t field_idx, bool is_put,
203                                         verifier::MethodVerifier* verifier,
204                                         InlineIGetIPutData* result)
205      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
206
207  // Can we fast path instance field access? Computes field's offset and volatility.
208  bool ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put,
209                                int* field_offset, bool* is_volatile)
210      LOCKS_EXCLUDED(Locks::mutator_lock_);
211
212  // Can we fastpath static field access? Computes field's offset, volatility and whether the
213  // field is within the referrer (which can avoid checking class initialization).
214  bool ComputeStaticFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put,
215                              int* field_offset, int* storage_index,
216                              bool* is_referrers_class, bool* is_volatile, bool* is_initialized)
217      LOCKS_EXCLUDED(Locks::mutator_lock_);
218
219  // Can we fastpath a interface, super class or virtual method call? Computes method's vtable
220  // index.
221  bool ComputeInvokeInfo(const DexCompilationUnit* mUnit, const uint32_t dex_pc,
222                         bool update_stats, bool enable_devirtualization,
223                         InvokeType* type, MethodReference* target_method, int* vtable_idx,
224                         uintptr_t* direct_code, uintptr_t* direct_method)
225      LOCKS_EXCLUDED(Locks::mutator_lock_);
226
227  const VerifiedMethod* GetVerifiedMethod(const DexFile* dex_file, uint32_t method_idx) const;
228  bool IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc);
229
230  // Record patch information for later fix up.
231  void AddCodePatch(const DexFile* dex_file,
232                    uint16_t referrer_class_def_idx,
233                    uint32_t referrer_method_idx,
234                    InvokeType referrer_invoke_type,
235                    uint32_t target_method_idx,
236                    InvokeType target_invoke_type,
237                    size_t literal_offset)
238      LOCKS_EXCLUDED(compiled_methods_lock_);
239  void AddRelativeCodePatch(const DexFile* dex_file,
240                            uint16_t referrer_class_def_idx,
241                            uint32_t referrer_method_idx,
242                            InvokeType referrer_invoke_type,
243                            uint32_t target_method_idx,
244                            InvokeType target_invoke_type,
245                            size_t literal_offset,
246                            int32_t pc_relative_offset)
247      LOCKS_EXCLUDED(compiled_methods_lock_);
248  void AddMethodPatch(const DexFile* dex_file,
249                      uint16_t referrer_class_def_idx,
250                      uint32_t referrer_method_idx,
251                      InvokeType referrer_invoke_type,
252                      uint32_t target_method_idx,
253                      InvokeType target_invoke_type,
254                      size_t literal_offset)
255      LOCKS_EXCLUDED(compiled_methods_lock_);
256  void AddClassPatch(const DexFile* dex_file,
257                     uint16_t referrer_class_def_idx,
258                     uint32_t referrer_method_idx,
259                     uint32_t target_method_idx,
260                     size_t literal_offset)
261      LOCKS_EXCLUDED(compiled_methods_lock_);
262
263  void SetBitcodeFileName(std::string const& filename);
264
265  bool GetSupportBootImageFixup() const {
266    return support_boot_image_fixup_;
267  }
268
269  void SetSupportBootImageFixup(bool support_boot_image_fixup) {
270    support_boot_image_fixup_ = support_boot_image_fixup;
271  }
272
273  ArenaPool& GetArenaPool() {
274    return arena_pool_;
275  }
276
277  bool WriteElf(const std::string& android_root,
278                bool is_host,
279                const std::vector<const DexFile*>& dex_files,
280                OatWriter& oat_writer,
281                File* file);
282
283  // TODO: move to a common home for llvm helpers once quick/portable are merged
284  static void InstructionSetToLLVMTarget(InstructionSet instruction_set,
285                                         std::string& target_triple,
286                                         std::string& target_cpu,
287                                         std::string& target_attr);
288
289  void SetCompilerContext(void* compiler_context) {
290    compiler_context_ = compiler_context;
291  }
292
293  void* GetCompilerContext() const {
294    return compiler_context_;
295  }
296
297  size_t GetThreadCount() const {
298    return thread_count_;
299  }
300
301  class CallPatchInformation;
302  class TypePatchInformation;
303
304  bool GetDumpPasses() const {
305    return dump_passes_;
306  }
307
308  CumulativeLogger& GetTimingsLogger() const {
309    return *timings_logger_;
310  }
311
312  class PatchInformation {
313   public:
314    const DexFile& GetDexFile() const {
315      return *dex_file_;
316    }
317    uint16_t GetReferrerClassDefIdx() const {
318      return referrer_class_def_idx_;
319    }
320    uint32_t GetReferrerMethodIdx() const {
321      return referrer_method_idx_;
322    }
323    size_t GetLiteralOffset() const {
324      return literal_offset_;
325    }
326
327    virtual bool IsCall() const {
328      return false;
329    }
330    virtual bool IsType() const {
331      return false;
332    }
333    virtual const CallPatchInformation* AsCall() const {
334      LOG(FATAL) << "Unreachable";
335      return nullptr;
336    }
337    virtual const TypePatchInformation* AsType() const {
338      LOG(FATAL) << "Unreachable";
339      return nullptr;
340    }
341
342   protected:
343    PatchInformation(const DexFile* dex_file,
344                     uint16_t referrer_class_def_idx,
345                     uint32_t referrer_method_idx,
346                     size_t literal_offset)
347      : dex_file_(dex_file),
348        referrer_class_def_idx_(referrer_class_def_idx),
349        referrer_method_idx_(referrer_method_idx),
350        literal_offset_(literal_offset) {
351      CHECK(dex_file_ != NULL);
352    }
353    virtual ~PatchInformation() {}
354
355    const DexFile* const dex_file_;
356    const uint16_t referrer_class_def_idx_;
357    const uint32_t referrer_method_idx_;
358    const size_t literal_offset_;
359
360    friend class CompilerDriver;
361  };
362
363  class CallPatchInformation : public PatchInformation {
364   public:
365    InvokeType GetReferrerInvokeType() const {
366      return referrer_invoke_type_;
367    }
368    uint32_t GetTargetMethodIdx() const {
369      return target_method_idx_;
370    }
371    InvokeType GetTargetInvokeType() const {
372      return target_invoke_type_;
373    }
374
375    const CallPatchInformation* AsCall() const {
376      return this;
377    }
378    bool IsCall() const {
379      return true;
380    }
381    virtual bool IsRelative() const {
382      return false;
383    }
384    virtual int RelativeOffset() const {
385      return 0;
386    }
387
388   protected:
389    CallPatchInformation(const DexFile* dex_file,
390                         uint16_t referrer_class_def_idx,
391                         uint32_t referrer_method_idx,
392                         InvokeType referrer_invoke_type,
393                         uint32_t target_method_idx,
394                         InvokeType target_invoke_type,
395                         size_t literal_offset)
396        : PatchInformation(dex_file, referrer_class_def_idx,
397                           referrer_method_idx, literal_offset),
398          referrer_invoke_type_(referrer_invoke_type),
399          target_method_idx_(target_method_idx),
400          target_invoke_type_(target_invoke_type) {
401    }
402
403   private:
404    const InvokeType referrer_invoke_type_;
405    const uint32_t target_method_idx_;
406    const InvokeType target_invoke_type_;
407
408    friend class CompilerDriver;
409    DISALLOW_COPY_AND_ASSIGN(CallPatchInformation);
410  };
411
412  class RelativeCallPatchInformation : public CallPatchInformation {
413   public:
414    bool IsRelative() const {
415      return true;
416    }
417    int RelativeOffset() const {
418      return offset_;
419    }
420
421   private:
422    RelativeCallPatchInformation(const DexFile* dex_file,
423                                 uint16_t referrer_class_def_idx,
424                                 uint32_t referrer_method_idx,
425                                 InvokeType referrer_invoke_type,
426                                 uint32_t target_method_idx,
427                                 InvokeType target_invoke_type,
428                                 size_t literal_offset,
429                                 int32_t pc_relative_offset)
430        : CallPatchInformation(dex_file, referrer_class_def_idx,
431                           referrer_method_idx, referrer_invoke_type,
432                           target_method_idx, target_invoke_type, literal_offset),
433          offset_(pc_relative_offset) {
434    }
435
436    const int offset_;
437
438    friend class CompilerDriver;
439    DISALLOW_COPY_AND_ASSIGN(RelativeCallPatchInformation);
440  };
441
442  class TypePatchInformation : public PatchInformation {
443   public:
444    uint32_t GetTargetTypeIdx() const {
445      return target_type_idx_;
446    }
447
448    bool IsType() const {
449      return true;
450    }
451    const TypePatchInformation* AsType() const {
452      return this;
453    }
454
455   private:
456    TypePatchInformation(const DexFile* dex_file,
457                         uint16_t referrer_class_def_idx,
458                         uint32_t referrer_method_idx,
459                         uint32_t target_type_idx,
460                         size_t literal_offset)
461        : PatchInformation(dex_file, referrer_class_def_idx,
462                           referrer_method_idx, literal_offset),
463          target_type_idx_(target_type_idx) {
464    }
465
466    const uint32_t target_type_idx_;
467
468    friend class CompilerDriver;
469    DISALLOW_COPY_AND_ASSIGN(TypePatchInformation);
470  };
471
472  const std::vector<const CallPatchInformation*>& GetCodeToPatch() const {
473    return code_to_patch_;
474  }
475  const std::vector<const CallPatchInformation*>& GetMethodsToPatch() const {
476    return methods_to_patch_;
477  }
478  const std::vector<const TypePatchInformation*>& GetClassesToPatch() const {
479    return classes_to_patch_;
480  }
481
482  // Checks if class specified by type_idx is one of the image_classes_
483  bool IsImageClass(const char* descriptor) const;
484
485  void RecordClassStatus(ClassReference ref, mirror::Class::Status status)
486      LOCKS_EXCLUDED(compiled_classes_lock_);
487
488  std::vector<uint8_t>* DeduplicateCode(const std::vector<uint8_t>& code);
489  std::vector<uint8_t>* DeduplicateMappingTable(const std::vector<uint8_t>& code);
490  std::vector<uint8_t>* DeduplicateVMapTable(const std::vector<uint8_t>& code);
491  std::vector<uint8_t>* DeduplicateGCMap(const std::vector<uint8_t>& code);
492
493 private:
494  // Compute constant code and method pointers when possible
495  void GetCodeAndMethodForDirectCall(InvokeType* type, InvokeType sharp_type,
496                                     bool no_guarantee_of_dex_cache_entry,
497                                     mirror::Class* referrer_class,
498                                     mirror::ArtMethod* method,
499                                     bool update_stats,
500                                     MethodReference* target_method,
501                                     uintptr_t* direct_code, uintptr_t* direct_method)
502      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
503
504  void PreCompile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
505                  ThreadPool& thread_pool, TimingLogger& timings)
506      LOCKS_EXCLUDED(Locks::mutator_lock_);
507
508  void LoadImageClasses(TimingLogger& timings);
509
510  // Attempt to resolve all type, methods, fields, and strings
511  // referenced from code in the dex file following PathClassLoader
512  // ordering semantics.
513  void Resolve(jobject class_loader, const std::vector<const DexFile*>& dex_files,
514               ThreadPool& thread_pool, TimingLogger& timings)
515      LOCKS_EXCLUDED(Locks::mutator_lock_);
516  void ResolveDexFile(jobject class_loader, const DexFile& dex_file,
517                      ThreadPool& thread_pool, TimingLogger& timings)
518      LOCKS_EXCLUDED(Locks::mutator_lock_);
519
520  void Verify(jobject class_loader, const std::vector<const DexFile*>& dex_files,
521              ThreadPool& thread_pool, TimingLogger& timings);
522  void VerifyDexFile(jobject class_loader, const DexFile& dex_file,
523                     ThreadPool& thread_pool, TimingLogger& timings)
524      LOCKS_EXCLUDED(Locks::mutator_lock_);
525
526  void InitializeClasses(jobject class_loader, const std::vector<const DexFile*>& dex_files,
527                         ThreadPool& thread_pool, TimingLogger& timings)
528      LOCKS_EXCLUDED(Locks::mutator_lock_);
529  void InitializeClasses(jobject class_loader, const DexFile& dex_file,
530                         ThreadPool& thread_pool, TimingLogger& timings)
531      LOCKS_EXCLUDED(Locks::mutator_lock_, compiled_classes_lock_);
532
533  void UpdateImageClasses(TimingLogger& timings)
534      LOCKS_EXCLUDED(Locks::mutator_lock_);
535  static void FindClinitImageClassesCallback(mirror::Object* object, void* arg)
536      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
537
538  void Compile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
539               ThreadPool& thread_pool, TimingLogger& timings);
540  void CompileDexFile(jobject class_loader, const DexFile& dex_file,
541                      ThreadPool& thread_pool, TimingLogger& timings)
542      LOCKS_EXCLUDED(Locks::mutator_lock_);
543  void CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
544                     InvokeType invoke_type, uint16_t class_def_idx, uint32_t method_idx,
545                     jobject class_loader, const DexFile& dex_file,
546                     DexToDexCompilationLevel dex_to_dex_compilation_level)
547      LOCKS_EXCLUDED(compiled_methods_lock_);
548
549  static void CompileClass(const ParallelCompilationManager* context, size_t class_def_index)
550      LOCKS_EXCLUDED(Locks::mutator_lock_);
551
552  std::vector<const CallPatchInformation*> code_to_patch_;
553  std::vector<const CallPatchInformation*> methods_to_patch_;
554  std::vector<const TypePatchInformation*> classes_to_patch_;
555
556  VerificationResults* verification_results_;
557  DexFileToMethodInlinerMap* method_inliner_map_;
558
559  UniquePtr<CompilerBackend> compiler_backend_;
560
561  const InstructionSet instruction_set_;
562  const InstructionSetFeatures instruction_set_features_;
563
564  // All class references that require
565  mutable ReaderWriterMutex freezing_constructor_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
566  std::set<ClassReference> freezing_constructor_classes_ GUARDED_BY(freezing_constructor_lock_);
567
568  typedef SafeMap<const ClassReference, CompiledClass*> ClassTable;
569  // All class references that this compiler has compiled.
570  mutable Mutex compiled_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
571  ClassTable compiled_classes_ GUARDED_BY(compiled_classes_lock_);
572
573  typedef SafeMap<const MethodReference, CompiledMethod*, MethodReferenceComparator> MethodTable;
574  // All method references that this compiler has compiled.
575  mutable Mutex compiled_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
576  MethodTable compiled_methods_ GUARDED_BY(compiled_methods_lock_);
577
578  const bool image_;
579
580  // If image_ is true, specifies the classes that will be included in
581  // the image. Note if image_classes_ is NULL, all classes are
582  // included in the image.
583  UniquePtr<DescriptorSet> image_classes_;
584
585  size_t thread_count_;
586  uint64_t start_ns_;
587
588  UniquePtr<AOTCompilationStats> stats_;
589
590  bool dump_stats_;
591  const bool dump_passes_;
592
593  CumulativeLogger* const timings_logger_;
594
595  typedef void (*CompilerCallbackFn)(CompilerDriver& driver);
596  typedef MutexLock* (*CompilerMutexLockFn)(CompilerDriver& driver);
597
598  void* compiler_library_;
599
600  typedef void (*DexToDexCompilerFn)(CompilerDriver& driver,
601                                     const DexFile::CodeItem* code_item,
602                                     uint32_t access_flags, InvokeType invoke_type,
603                                     uint32_t class_dex_idx, uint32_t method_idx,
604                                     jobject class_loader, const DexFile& dex_file,
605                                     DexToDexCompilationLevel dex_to_dex_compilation_level);
606  DexToDexCompilerFn dex_to_dex_compiler_;
607
608  void* compiler_context_;
609
610  pthread_key_t tls_key_;
611
612  // Arena pool used by the compiler.
613  ArenaPool arena_pool_;
614
615  typedef void (*CompilerEnableAutoElfLoadingFn)(CompilerDriver& driver);
616  CompilerEnableAutoElfLoadingFn compiler_enable_auto_elf_loading_;
617
618  typedef const void* (*CompilerGetMethodCodeAddrFn)
619      (const CompilerDriver& driver, const CompiledMethod* cm, const mirror::ArtMethod* method);
620  CompilerGetMethodCodeAddrFn compiler_get_method_code_addr_;
621
622  bool support_boot_image_fixup_;
623
624  // DeDuplication data structures, these own the corresponding byte arrays.
625  class DedupeHashFunc {
626   public:
627    size_t operator()(const std::vector<uint8_t>& array) const {
628      // For small arrays compute a hash using every byte.
629      static const size_t kSmallArrayThreshold = 16;
630      size_t hash = 0x811c9dc5;
631      if (array.size() <= kSmallArrayThreshold) {
632        for (uint8_t b : array) {
633          hash = (hash * 16777619) ^ b;
634        }
635      } else {
636        // For larger arrays use the 2 bytes at 6 bytes (the location of a push registers
637        // instruction field for quick generated code on ARM) and then select a number of other
638        // values at random.
639        static const size_t kRandomHashCount = 16;
640        for (size_t i = 0; i < 2; ++i) {
641          uint8_t b = array[i + 6];
642          hash = (hash * 16777619) ^ b;
643        }
644        for (size_t i = 2; i < kRandomHashCount; ++i) {
645          size_t r = i * 1103515245 + 12345;
646          uint8_t b = array[r % array.size()];
647          hash = (hash * 16777619) ^ b;
648        }
649      }
650      hash += hash << 13;
651      hash ^= hash >> 7;
652      hash += hash << 3;
653      hash ^= hash >> 17;
654      hash += hash << 5;
655      return hash;
656    }
657  };
658  DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc, 4> dedupe_code_;
659  DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc, 4> dedupe_mapping_table_;
660  DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc, 4> dedupe_vmap_table_;
661  DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc, 4> dedupe_gc_map_;
662
663  DISALLOW_COPY_AND_ASSIGN(CompilerDriver);
664};
665
666}  // namespace art
667
668#endif  // ART_COMPILER_DRIVER_COMPILER_DRIVER_H_
669