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