Home | History | Annotate | Download | only in driver
      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 
     45 namespace art {
     46 
     47 namespace mirror {
     48 class DexCache;
     49 }  // namespace mirror
     50 
     51 namespace verifier {
     52 class MethodVerifier;
     53 class VerifierDepsTest;
     54 }  // namespace verifier
     55 
     56 class BitVector;
     57 class CompiledMethod;
     58 class CompilerOptions;
     59 class DexCompilationUnit;
     60 struct InlineIGetIPutData;
     61 class InstructionSetFeatures;
     62 class InternTable;
     63 class ParallelCompilationManager;
     64 class ScopedObjectAccess;
     65 template <class Allocator> class SrcMap;
     66 template<class T> class Handle;
     67 class TimingLogger;
     68 class VdexFile;
     69 class VerificationResults;
     70 class VerifiedMethod;
     71 
     72 enum 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 
     81 class 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   bool CanAssumeVerified(ClassReference ref) const;
    381 
    382  private:
    383   void PreCompile(jobject class_loader,
    384                   const std::vector<const DexFile*>& dex_files,
    385                   TimingLogger* timings)
    386       REQUIRES(!Locks::mutator_lock_);
    387 
    388   void LoadImageClasses(TimingLogger* timings) REQUIRES(!Locks::mutator_lock_);
    389 
    390   // Attempt to resolve all type, methods, fields, and strings
    391   // referenced from code in the dex file following PathClassLoader
    392   // ordering semantics.
    393   void Resolve(jobject class_loader,
    394                const std::vector<const DexFile*>& dex_files,
    395                TimingLogger* timings)
    396       REQUIRES(!Locks::mutator_lock_);
    397   void ResolveDexFile(jobject class_loader,
    398                       const DexFile& dex_file,
    399                       const std::vector<const DexFile*>& dex_files,
    400                       ThreadPool* thread_pool,
    401                       size_t thread_count,
    402                       TimingLogger* timings)
    403       REQUIRES(!Locks::mutator_lock_);
    404 
    405   // Do fast verification through VerifierDeps if possible. Return whether
    406   // verification was successful.
    407   bool FastVerify(jobject class_loader,
    408                   const std::vector<const DexFile*>& dex_files,
    409                   TimingLogger* timings);
    410 
    411   void Verify(jobject class_loader,
    412               const std::vector<const DexFile*>& dex_files,
    413               TimingLogger* timings);
    414 
    415   void VerifyDexFile(jobject class_loader,
    416                      const DexFile& dex_file,
    417                      const std::vector<const DexFile*>& dex_files,
    418                      ThreadPool* thread_pool,
    419                      size_t thread_count,
    420                      TimingLogger* timings)
    421       REQUIRES(!Locks::mutator_lock_);
    422 
    423   void SetVerified(jobject class_loader,
    424                    const std::vector<const DexFile*>& dex_files,
    425                    TimingLogger* timings);
    426   void SetVerifiedDexFile(jobject class_loader,
    427                           const DexFile& dex_file,
    428                           const std::vector<const DexFile*>& dex_files,
    429                           ThreadPool* thread_pool,
    430                           size_t thread_count,
    431                           TimingLogger* timings)
    432       REQUIRES(!Locks::mutator_lock_);
    433 
    434   void InitializeClasses(jobject class_loader,
    435                          const std::vector<const DexFile*>& dex_files,
    436                          TimingLogger* timings)
    437       REQUIRES(!Locks::mutator_lock_);
    438   void InitializeClasses(jobject class_loader,
    439                          const DexFile& dex_file,
    440                          const std::vector<const DexFile*>& dex_files,
    441                          TimingLogger* timings)
    442       REQUIRES(!Locks::mutator_lock_);
    443 
    444   void UpdateImageClasses(TimingLogger* timings) REQUIRES(!Locks::mutator_lock_);
    445 
    446   void Compile(jobject class_loader,
    447                const std::vector<const DexFile*>& dex_files,
    448                TimingLogger* timings) REQUIRES(!dex_to_dex_references_lock_);
    449   void CompileDexFile(jobject class_loader,
    450                       const DexFile& dex_file,
    451                       const std::vector<const DexFile*>& dex_files,
    452                       ThreadPool* thread_pool,
    453                       size_t thread_count,
    454                       TimingLogger* timings)
    455       REQUIRES(!Locks::mutator_lock_);
    456 
    457   bool MayInlineInternal(const DexFile* inlined_from, const DexFile* inlined_into) const;
    458 
    459   void InitializeThreadPools();
    460   void FreeThreadPools();
    461   void CheckThreadPools();
    462 
    463   bool RequiresConstructorBarrier(const DexFile& dex_file, uint16_t class_def_idx) const;
    464 
    465   const CompilerOptions* const compiler_options_;
    466   VerificationResults* const verification_results_;
    467 
    468   std::unique_ptr<Compiler> compiler_;
    469   Compiler::Kind compiler_kind_;
    470 
    471   const InstructionSet instruction_set_;
    472   const InstructionSetFeatures* const instruction_set_features_;
    473 
    474   // All class references that require constructor barriers. If the class reference is not in the
    475   // set then the result has not yet been computed.
    476   mutable ReaderWriterMutex requires_constructor_barrier_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
    477   std::map<ClassReference, bool> requires_constructor_barrier_
    478       GUARDED_BY(requires_constructor_barrier_lock_);
    479 
    480   // All class references that this compiler has compiled. Indexed by class defs.
    481   using ClassStateTable = AtomicDexRefMap<mirror::Class::Status>;
    482   ClassStateTable compiled_classes_;
    483 
    484   typedef AtomicDexRefMap<CompiledMethod*> MethodTable;
    485 
    486  private:
    487   // All method references that this compiler has compiled.
    488   MethodTable compiled_methods_;
    489 
    490   // Number of non-relative patches in all compiled methods. These patches need space
    491   // in the .oat_patches ELF section if requested in the compiler options.
    492   Atomic<size_t> non_relative_linker_patch_count_;
    493 
    494   // If image_ is true, specifies the classes that will be included in the image.
    495   // Note if image_classes_ is null, all classes are included in the image.
    496   std::unique_ptr<std::unordered_set<std::string>> image_classes_;
    497 
    498   // Specifies the classes that will be compiled. Note that if classes_to_compile_ is null,
    499   // all classes are eligible for compilation (duplication filters etc. will still apply).
    500   // This option may be restricted to the boot image, depending on a flag in the implementation.
    501   std::unique_ptr<std::unordered_set<std::string>> classes_to_compile_;
    502 
    503   // Specifies the methods that will be compiled. Note that if methods_to_compile_ is null,
    504   // all methods are eligible for compilation (compilation filters etc. will still apply).
    505   // This option may be restricted to the boot image, depending on a flag in the implementation.
    506   std::unique_ptr<std::unordered_set<std::string>> methods_to_compile_;
    507 
    508   bool had_hard_verifier_failure_;
    509 
    510   // A thread pool that can (potentially) run tasks in parallel.
    511   std::unique_ptr<ThreadPool> parallel_thread_pool_;
    512   size_t parallel_thread_count_;
    513 
    514   // A thread pool that guarantees running single-threaded on the main thread.
    515   std::unique_ptr<ThreadPool> single_thread_pool_;
    516 
    517   class AOTCompilationStats;
    518   std::unique_ptr<AOTCompilationStats> stats_;
    519 
    520   bool dump_stats_;
    521   const bool dump_passes_;
    522 
    523   CumulativeLogger* const timings_logger_;
    524 
    525   typedef void (*CompilerCallbackFn)(CompilerDriver& driver);
    526   typedef MutexLock* (*CompilerMutexLockFn)(CompilerDriver& driver);
    527 
    528   void* compiler_context_;
    529 
    530   bool support_boot_image_fixup_;
    531 
    532   // List of dex files that will be stored in the oat file.
    533   std::vector<const DexFile*> dex_files_for_oat_file_;
    534 
    535   CompiledMethodStorage compiled_method_storage_;
    536 
    537   // Info for profile guided compilation.
    538   const ProfileCompilationInfo* const profile_compilation_info_;
    539 
    540   size_t max_arena_alloc_;
    541 
    542   // Data for delaying dex-to-dex compilation.
    543   Mutex dex_to_dex_references_lock_;
    544   // In the first phase, dex_to_dex_references_ collects methods for dex-to-dex compilation.
    545   class DexFileMethodSet;
    546   std::vector<DexFileMethodSet> dex_to_dex_references_ GUARDED_BY(dex_to_dex_references_lock_);
    547   // In the second phase, current_dex_to_dex_methods_ points to the BitVector with method
    548   // indexes for dex-to-dex compilation in the current dex file.
    549   const BitVector* current_dex_to_dex_methods_;
    550 
    551   friend class CompileClassVisitor;
    552   friend class DexToDexDecompilerTest;
    553   friend class verifier::VerifierDepsTest;
    554   DISALLOW_COPY_AND_ASSIGN(CompilerDriver);
    555 };
    556 
    557 }  // namespace art
    558 
    559 #endif  // ART_COMPILER_DRIVER_COMPILER_DRIVER_H_
    560