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