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