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