compiler_driver.h revision 85c0f2ac03417f5125bc2ff1dab8109859c67d5c
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_DRIVER_COMPILER_DRIVER_H_
18#define ART_COMPILER_DRIVER_COMPILER_DRIVER_H_
19
20#include <set>
21#include <string>
22#include <unordered_set>
23#include <vector>
24
25#include "arch/instruction_set.h"
26#include "base/arena_allocator.h"
27#include "base/array_ref.h"
28#include "base/bit_utils.h"
29#include "base/mutex.h"
30#include "base/timing_logger.h"
31#include "class_reference.h"
32#include "compiler.h"
33#include "dex_file.h"
34#include "dex_file_types.h"
35#include "driver/compiled_method_storage.h"
36#include "jit/profile_compilation_info.h"
37#include "invoke_type.h"
38#include "method_reference.h"
39#include "mirror/class.h"  // For mirror::Class::Status.
40#include "os.h"
41#include "runtime.h"
42#include "safe_map.h"
43#include "thread_pool.h"
44#include "utils/atomic_method_ref_map.h"
45#include "utils/dex_cache_arrays_layout.h"
46
47namespace art {
48
49namespace mirror {
50class DexCache;
51}  // namespace mirror
52
53namespace verifier {
54class MethodVerifier;
55class VerifierDepsTest;
56}  // namespace verifier
57
58class BitVector;
59class CompiledClass;
60class CompiledMethod;
61class CompilerOptions;
62class DexCompilationUnit;
63struct InlineIGetIPutData;
64class InstructionSetFeatures;
65class ParallelCompilationManager;
66class ScopedObjectAccess;
67template <class Allocator> class SrcMap;
68class SrcMapElem;
69using SwapSrcMap = SrcMap<SwapAllocator<SrcMapElem>>;
70template<class T> class Handle;
71class TimingLogger;
72class VdexFile;
73class VerificationResults;
74class VerifiedMethod;
75
76enum EntryPointCallingConvention {
77  // ABI of invocations to a method's interpreter entry point.
78  kInterpreterAbi,
79  // ABI of calls to a method's native code, only used for native methods.
80  kJniAbi,
81  // ABI of calls to a method's quick code entry point.
82  kQuickAbi
83};
84
85class CompilerDriver {
86 public:
87  // Create a compiler targeting the requested "instruction_set".
88  // "image" should be true if image specific optimizations should be
89  // enabled.  "image_classes" lets the compiler know what classes it
90  // can assume will be in the image, with null implying all available
91  // classes.
92  CompilerDriver(const CompilerOptions* compiler_options,
93                 VerificationResults* verification_results,
94                 Compiler::Kind compiler_kind,
95                 InstructionSet instruction_set,
96                 const InstructionSetFeatures* instruction_set_features,
97                 std::unordered_set<std::string>* image_classes,
98                 std::unordered_set<std::string>* compiled_classes,
99                 std::unordered_set<std::string>* compiled_methods,
100                 size_t thread_count,
101                 bool dump_stats,
102                 bool dump_passes,
103                 CumulativeLogger* timer,
104                 int swap_fd,
105                 const ProfileCompilationInfo* profile_compilation_info);
106
107  ~CompilerDriver();
108
109  // Set dex files that will be stored in the oat file after being compiled.
110  void SetDexFilesForOatFile(const std::vector<const DexFile*>& dex_files) {
111    dex_files_for_oat_file_ = &dex_files;
112  }
113
114  // Get dex file that will be stored in the oat file after being compiled.
115  ArrayRef<const DexFile* const> GetDexFilesForOatFile() const {
116    return (dex_files_for_oat_file_ != nullptr)
117        ? ArrayRef<const DexFile* const>(*dex_files_for_oat_file_)
118        : ArrayRef<const DexFile* const>();
119  }
120
121  void CompileAll(jobject class_loader,
122                  const std::vector<const DexFile*>& dex_files,
123                  TimingLogger* timings)
124      REQUIRES(!Locks::mutator_lock_, !compiled_classes_lock_, !dex_to_dex_references_lock_);
125
126  void CompileAll(jobject class_loader,
127                  const std::vector<const DexFile*>& dex_files,
128                  VdexFile* vdex_file,
129                  TimingLogger* timings)
130      REQUIRES(!Locks::mutator_lock_, !compiled_classes_lock_, !dex_to_dex_references_lock_);
131
132  // Compile a single Method.
133  void CompileOne(Thread* self, ArtMethod* method, TimingLogger* timings)
134      REQUIRES_SHARED(Locks::mutator_lock_)
135      REQUIRES(!compiled_classes_lock_, !dex_to_dex_references_lock_);
136
137  VerificationResults* GetVerificationResults() const {
138    DCHECK(Runtime::Current()->IsAotCompiler());
139    return verification_results_;
140  }
141
142  InstructionSet GetInstructionSet() const {
143    return instruction_set_;
144  }
145
146  const InstructionSetFeatures* GetInstructionSetFeatures() const {
147    return instruction_set_features_;
148  }
149
150  const CompilerOptions& GetCompilerOptions() const {
151    return *compiler_options_;
152  }
153
154  Compiler* GetCompiler() const {
155    return compiler_.get();
156  }
157
158  const std::unordered_set<std::string>* GetImageClasses() const {
159    return image_classes_.get();
160  }
161
162  // Generate the trampolines that are invoked by unresolved direct methods.
163  std::unique_ptr<const std::vector<uint8_t>> CreateJniDlsymLookup() const;
164  std::unique_ptr<const std::vector<uint8_t>> CreateQuickGenericJniTrampoline() const;
165  std::unique_ptr<const std::vector<uint8_t>> CreateQuickImtConflictTrampoline() const;
166  std::unique_ptr<const std::vector<uint8_t>> CreateQuickResolutionTrampoline() const;
167  std::unique_ptr<const std::vector<uint8_t>> CreateQuickToInterpreterBridge() const;
168
169  CompiledClass* GetCompiledClass(ClassReference ref) const
170      REQUIRES(!compiled_classes_lock_);
171
172  CompiledMethod* GetCompiledMethod(MethodReference ref) const;
173  size_t GetNonRelativeLinkerPatchCount() const;
174  // Add a compiled method.
175  void AddCompiledMethod(const MethodReference& method_ref,
176                         CompiledMethod* const compiled_method,
177                         size_t non_relative_linker_patch_count);
178
179  void SetRequiresConstructorBarrier(Thread* self,
180                                     const DexFile* dex_file,
181                                     uint16_t class_def_index,
182                                     bool requires)
183      REQUIRES(!requires_constructor_barrier_lock_);
184  bool RequiresConstructorBarrier(Thread* self,
185                                  const DexFile* dex_file,
186                                  uint16_t class_def_index)
187      REQUIRES(!requires_constructor_barrier_lock_);
188
189  // Are runtime access checks necessary in the compiled code?
190  bool CanAccessTypeWithoutChecks(ObjPtr<mirror::Class> referrer_class,
191                                  ObjPtr<mirror::Class> resolved_class)
192      REQUIRES_SHARED(Locks::mutator_lock_);
193
194  // Are runtime access and instantiable checks necessary in the code?
195  // out_is_finalizable is set to whether the type is finalizable.
196  bool CanAccessInstantiableTypeWithoutChecks(ObjPtr<mirror::Class> referrer_class,
197                                              ObjPtr<mirror::Class> resolved_class,
198                                              bool* out_is_finalizable)
199      REQUIRES_SHARED(Locks::mutator_lock_);
200
201  // Resolve compiling method's class. Returns null on failure.
202  mirror::Class* ResolveCompilingMethodsClass(
203      const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
204      Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit)
205      REQUIRES_SHARED(Locks::mutator_lock_);
206
207  mirror::Class* ResolveClass(
208      const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
209      Handle<mirror::ClassLoader> class_loader, dex::TypeIndex type_index,
210      const DexCompilationUnit* mUnit)
211      REQUIRES_SHARED(Locks::mutator_lock_);
212
213  // Resolve a field. Returns null on failure, including incompatible class change.
214  // NOTE: Unlike ClassLinker's ResolveField(), this method enforces is_static.
215  ArtField* ResolveField(
216      const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
217      Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
218      uint32_t field_idx, bool is_static)
219      REQUIRES_SHARED(Locks::mutator_lock_);
220
221  // Resolve a field with a given dex file.
222  ArtField* ResolveFieldWithDexFile(
223      const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
224      Handle<mirror::ClassLoader> class_loader, const DexFile* dex_file,
225      uint32_t field_idx, bool is_static)
226      REQUIRES_SHARED(Locks::mutator_lock_);
227
228  // Can we fast-path an IGET/IPUT access to an instance field? If yes, compute the field offset.
229  std::pair<bool, bool> IsFastInstanceField(
230      mirror::DexCache* dex_cache, mirror::Class* referrer_class,
231      ArtField* resolved_field, uint16_t field_idx)
232      REQUIRES_SHARED(Locks::mutator_lock_);
233
234  // Resolve a method. Returns null on failure, including incompatible class change.
235  ArtMethod* ResolveMethod(
236      ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
237      Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
238      uint32_t method_idx, InvokeType invoke_type, bool check_incompatible_class_change = true)
239      REQUIRES_SHARED(Locks::mutator_lock_);
240
241  void ProcessedInstanceField(bool resolved);
242  void ProcessedStaticField(bool resolved, bool local);
243
244  // Can we fast path instance field access? Computes field's offset and volatility.
245  bool ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put,
246                                MemberOffset* field_offset, bool* is_volatile)
247      REQUIRES(!Locks::mutator_lock_);
248
249  ArtField* ComputeInstanceFieldInfo(uint32_t field_idx,
250                                             const DexCompilationUnit* mUnit,
251                                             bool is_put,
252                                             const ScopedObjectAccess& soa)
253      REQUIRES_SHARED(Locks::mutator_lock_);
254
255
256  const VerifiedMethod* GetVerifiedMethod(const DexFile* dex_file, uint32_t method_idx) const;
257  bool IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc);
258
259  bool GetSupportBootImageFixup() const {
260    return support_boot_image_fixup_;
261  }
262
263  void SetSupportBootImageFixup(bool support_boot_image_fixup) {
264    support_boot_image_fixup_ = support_boot_image_fixup;
265  }
266
267  void SetCompilerContext(void* compiler_context) {
268    compiler_context_ = compiler_context;
269  }
270
271  void* GetCompilerContext() const {
272    return compiler_context_;
273  }
274
275  size_t GetThreadCount() const {
276    return parallel_thread_count_;
277  }
278
279  bool GetDumpStats() const {
280    return dump_stats_;
281  }
282
283  bool GetDumpPasses() const {
284    return dump_passes_;
285  }
286
287  CumulativeLogger* GetTimingsLogger() const {
288    return timings_logger_;
289  }
290
291  void SetDedupeEnabled(bool dedupe_enabled) {
292    compiled_method_storage_.SetDedupeEnabled(dedupe_enabled);
293  }
294
295  bool DedupeEnabled() const {
296    return compiled_method_storage_.DedupeEnabled();
297  }
298
299  // Checks if class specified by type_idx is one of the image_classes_
300  bool IsImageClass(const char* descriptor) const;
301
302  // Checks whether the provided class should be compiled, i.e., is in classes_to_compile_.
303  bool IsClassToCompile(const char* descriptor) const;
304
305  // Checks whether the provided method should be compiled, i.e., is in method_to_compile_.
306  bool IsMethodToCompile(const MethodReference& method_ref) const;
307
308  // Checks whether profile guided compilation is enabled and if the method should be compiled
309  // according to the profile file.
310  bool ShouldCompileBasedOnProfile(const MethodReference& method_ref) const;
311
312  // Checks whether profile guided verification is enabled and if the method should be verified
313  // according to the profile file.
314  bool ShouldVerifyClassBasedOnProfile(const DexFile& dex_file, uint16_t class_idx) const;
315
316  void RecordClassStatus(ClassReference ref, mirror::Class::Status status)
317      REQUIRES(!compiled_classes_lock_);
318
319  // Checks if the specified method has been verified without failures. Returns
320  // false if the method is not in the verification results (GetVerificationResults).
321  bool IsMethodVerifiedWithoutFailures(uint32_t method_idx,
322                                       uint16_t class_def_idx,
323                                       const DexFile& dex_file) const;
324
325  // Get memory usage during compilation.
326  std::string GetMemoryUsageString(bool extended) const;
327
328  void SetHadHardVerifierFailure() {
329    had_hard_verifier_failure_ = true;
330  }
331
332  Compiler::Kind GetCompilerKind() {
333    return compiler_kind_;
334  }
335
336  CompiledMethodStorage* GetCompiledMethodStorage() {
337    return &compiled_method_storage_;
338  }
339
340  // Can we assume that the klass is loaded?
341  bool CanAssumeClassIsLoaded(mirror::Class* klass)
342      REQUIRES_SHARED(Locks::mutator_lock_);
343
344  bool MayInline(const DexFile* inlined_from, const DexFile* inlined_into) const {
345    if (!kIsTargetBuild) {
346      return MayInlineInternal(inlined_from, inlined_into);
347    }
348    return true;
349  }
350
351  void MarkForDexToDexCompilation(Thread* self, const MethodReference& method_ref)
352      REQUIRES(!dex_to_dex_references_lock_);
353
354  const BitVector* GetCurrentDexToDexMethods() const {
355    return current_dex_to_dex_methods_;
356  }
357
358 private:
359  // Can `referrer_class` access the resolved `member`?
360  // Dispatch call to mirror::Class::CanAccessResolvedField or
361  // mirror::Class::CanAccessResolvedMember depending on the value of
362  // ArtMember.
363  template <typename ArtMember>
364  static bool CanAccessResolvedMember(mirror::Class* referrer_class,
365                                      mirror::Class* access_to,
366                                      ArtMember* member,
367                                      mirror::DexCache* dex_cache,
368                                      uint32_t field_idx)
369      REQUIRES_SHARED(Locks::mutator_lock_);
370
371 private:
372  void PreCompile(jobject class_loader,
373                  const std::vector<const DexFile*>& dex_files,
374                  TimingLogger* timings)
375      REQUIRES(!Locks::mutator_lock_, !compiled_classes_lock_);
376
377  void LoadImageClasses(TimingLogger* timings) REQUIRES(!Locks::mutator_lock_);
378
379  // Attempt to resolve all type, methods, fields, and strings
380  // referenced from code in the dex file following PathClassLoader
381  // ordering semantics.
382  void Resolve(jobject class_loader,
383               const std::vector<const DexFile*>& dex_files,
384               TimingLogger* timings)
385      REQUIRES(!Locks::mutator_lock_);
386  void ResolveDexFile(jobject class_loader,
387                      const DexFile& dex_file,
388                      const std::vector<const DexFile*>& dex_files,
389                      ThreadPool* thread_pool,
390                      size_t thread_count,
391                      TimingLogger* timings)
392      REQUIRES(!Locks::mutator_lock_);
393
394  // Do fast verification through VerifierDeps if possible. Return whether
395  // verification was successful.
396  // NO_THREAD_SAFETY_ANALYSIS as the method accesses a guarded value in a
397  // single-threaded way.
398  bool FastVerify(jobject class_loader,
399                  const std::vector<const DexFile*>& dex_files,
400                  TimingLogger* timings)
401      NO_THREAD_SAFETY_ANALYSIS;
402
403  void Verify(jobject class_loader,
404              const std::vector<const DexFile*>& dex_files,
405              TimingLogger* timings);
406
407  void VerifyDexFile(jobject class_loader,
408                     const DexFile& dex_file,
409                     const std::vector<const DexFile*>& dex_files,
410                     ThreadPool* thread_pool,
411                     size_t thread_count,
412                     TimingLogger* timings)
413      REQUIRES(!Locks::mutator_lock_);
414
415  void SetVerified(jobject class_loader,
416                   const std::vector<const DexFile*>& dex_files,
417                   TimingLogger* timings);
418  void SetVerifiedDexFile(jobject class_loader,
419                          const DexFile& dex_file,
420                          const std::vector<const DexFile*>& dex_files,
421                          ThreadPool* thread_pool,
422                          size_t thread_count,
423                          TimingLogger* timings)
424      REQUIRES(!Locks::mutator_lock_);
425
426  void InitializeClasses(jobject class_loader,
427                         const std::vector<const DexFile*>& dex_files,
428                         TimingLogger* timings)
429      REQUIRES(!Locks::mutator_lock_, !compiled_classes_lock_);
430  void InitializeClasses(jobject class_loader,
431                         const DexFile& dex_file,
432                         const std::vector<const DexFile*>& dex_files,
433                         TimingLogger* timings)
434      REQUIRES(!Locks::mutator_lock_, !compiled_classes_lock_);
435
436  void UpdateImageClasses(TimingLogger* timings) REQUIRES(!Locks::mutator_lock_);
437
438  void Compile(jobject class_loader,
439               const std::vector<const DexFile*>& dex_files,
440               TimingLogger* timings) REQUIRES(!dex_to_dex_references_lock_);
441  void CompileDexFile(jobject class_loader,
442                      const DexFile& dex_file,
443                      const std::vector<const DexFile*>& dex_files,
444                      ThreadPool* thread_pool,
445                      size_t thread_count,
446                      TimingLogger* timings)
447      REQUIRES(!Locks::mutator_lock_);
448
449  bool MayInlineInternal(const DexFile* inlined_from, const DexFile* inlined_into) const;
450
451  void InitializeThreadPools();
452  void FreeThreadPools();
453  void CheckThreadPools();
454
455  bool RequiresConstructorBarrier(const DexFile& dex_file, uint16_t class_def_idx) const;
456
457  const CompilerOptions* const compiler_options_;
458  VerificationResults* const verification_results_;
459
460  std::unique_ptr<Compiler> compiler_;
461  Compiler::Kind compiler_kind_;
462
463  const InstructionSet instruction_set_;
464  const InstructionSetFeatures* const instruction_set_features_;
465
466  // All class references that require constructor barriers. If the class reference is not in the
467  // set then the result has not yet been computed.
468  mutable ReaderWriterMutex requires_constructor_barrier_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
469  std::map<ClassReference, bool> requires_constructor_barrier_
470      GUARDED_BY(requires_constructor_barrier_lock_);
471
472  typedef SafeMap<const ClassReference, CompiledClass*> ClassTable;
473  // All class references that this compiler has compiled.
474  mutable Mutex compiled_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
475  ClassTable compiled_classes_ GUARDED_BY(compiled_classes_lock_);
476
477  typedef AtomicMethodRefMap<CompiledMethod*> MethodTable;
478
479 private:
480  // All method references that this compiler has compiled.
481  MethodTable compiled_methods_;
482
483  // Number of non-relative patches in all compiled methods. These patches need space
484  // in the .oat_patches ELF section if requested in the compiler options.
485  Atomic<size_t> non_relative_linker_patch_count_;
486
487  // If image_ is true, specifies the classes that will be included in the image.
488  // Note if image_classes_ is null, all classes are included in the image.
489  std::unique_ptr<std::unordered_set<std::string>> image_classes_;
490
491  // Specifies the classes that will be compiled. Note that if classes_to_compile_ is null,
492  // all classes are eligible for compilation (duplication filters etc. will still apply).
493  // This option may be restricted to the boot image, depending on a flag in the implementation.
494  std::unique_ptr<std::unordered_set<std::string>> classes_to_compile_;
495
496  // Specifies the methods that will be compiled. Note that if methods_to_compile_ is null,
497  // all methods are eligible for compilation (compilation filters etc. will still apply).
498  // This option may be restricted to the boot image, depending on a flag in the implementation.
499  std::unique_ptr<std::unordered_set<std::string>> methods_to_compile_;
500
501  bool had_hard_verifier_failure_;
502
503  // A thread pool that can (potentially) run tasks in parallel.
504  std::unique_ptr<ThreadPool> parallel_thread_pool_;
505  size_t parallel_thread_count_;
506
507  // A thread pool that guarantees running single-threaded on the main thread.
508  std::unique_ptr<ThreadPool> single_thread_pool_;
509
510  class AOTCompilationStats;
511  std::unique_ptr<AOTCompilationStats> stats_;
512
513  bool dump_stats_;
514  const bool dump_passes_;
515
516  CumulativeLogger* const timings_logger_;
517
518  typedef void (*CompilerCallbackFn)(CompilerDriver& driver);
519  typedef MutexLock* (*CompilerMutexLockFn)(CompilerDriver& driver);
520
521  void* compiler_context_;
522
523  bool support_boot_image_fixup_;
524
525  // List of dex files that will be stored in the oat file.
526  const std::vector<const DexFile*>* dex_files_for_oat_file_;
527
528  CompiledMethodStorage compiled_method_storage_;
529
530  // Info for profile guided compilation.
531  const ProfileCompilationInfo* const profile_compilation_info_;
532
533  size_t max_arena_alloc_;
534
535  // Data for delaying dex-to-dex compilation.
536  Mutex dex_to_dex_references_lock_;
537  // In the first phase, dex_to_dex_references_ collects methods for dex-to-dex compilation.
538  class DexFileMethodSet;
539  std::vector<DexFileMethodSet> dex_to_dex_references_ GUARDED_BY(dex_to_dex_references_lock_);
540  // In the second phase, current_dex_to_dex_methods_ points to the BitVector with method
541  // indexes for dex-to-dex compilation in the current dex file.
542  const BitVector* current_dex_to_dex_methods_;
543
544  friend class CompileClassVisitor;
545  friend class DexToDexDecompilerTest;
546  friend class verifier::VerifierDepsTest;
547  DISALLOW_COPY_AND_ASSIGN(CompilerDriver);
548};
549
550}  // namespace art
551
552#endif  // ART_COMPILER_DRIVER_COMPILER_DRIVER_H_
553