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_DEX2OAT_DRIVER_COMPILER_DRIVER_H_
     18 #define ART_DEX2OAT_DRIVER_COMPILER_DRIVER_H_
     19 
     20 #include <atomic>
     21 #include <set>
     22 #include <string>
     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/hash_set.h"
     29 #include "base/mutex.h"
     30 #include "base/os.h"
     31 #include "base/quasi_atomic.h"
     32 #include "base/safe_map.h"
     33 #include "base/timing_logger.h"
     34 #include "class_status.h"
     35 #include "compiler.h"
     36 #include "dex/class_reference.h"
     37 #include "dex/dex_file_types.h"
     38 #include "dex/dex_to_dex_compiler.h"
     39 #include "dex/method_reference.h"
     40 #include "driver/compiled_method_storage.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 dex {
     48 struct CodeItem;
     49 }  // namespace dex
     50 
     51 namespace mirror {
     52 class Class;
     53 class DexCache;
     54 }  // namespace mirror
     55 
     56 namespace verifier {
     57 class MethodVerifier;
     58 class VerifierDepsTest;
     59 }  // namespace verifier
     60 
     61 class ArtField;
     62 class BitVector;
     63 class CompiledMethod;
     64 class CompilerOptions;
     65 class DexCompilationUnit;
     66 class DexFile;
     67 template<class T> class Handle;
     68 struct InlineIGetIPutData;
     69 class InstructionSetFeatures;
     70 class InternTable;
     71 enum InvokeType : uint32_t;
     72 class MemberOffset;
     73 template<class MirrorType> class ObjPtr;
     74 class ParallelCompilationManager;
     75 class ProfileCompilationInfo;
     76 class ScopedObjectAccess;
     77 template <class Allocator> class SrcMap;
     78 class TimingLogger;
     79 class VdexFile;
     80 class VerificationResults;
     81 
     82 class CompilerDriver {
     83  public:
     84   // Create a compiler targeting the requested "instruction_set".
     85   // "image" should be true if image specific optimizations should be
     86   // enabled.  "image_classes" lets the compiler know what classes it
     87   // can assume will be in the image, with null implying all available
     88   // classes.
     89   CompilerDriver(const CompilerOptions* compiler_options,
     90                  Compiler::Kind compiler_kind,
     91                  size_t thread_count,
     92                  int swap_fd);
     93 
     94   ~CompilerDriver();
     95 
     96   // Set dex files classpath.
     97   void SetClasspathDexFiles(const std::vector<const DexFile*>& dex_files);
     98 
     99   // Initialize and destroy thread pools. This is exposed because we do not want
    100   // to do this twice, for PreCompile() and CompileAll().
    101   void InitializeThreadPools();
    102   void FreeThreadPools();
    103 
    104   void PreCompile(jobject class_loader,
    105                   const std::vector<const DexFile*>& dex_files,
    106                   TimingLogger* timings,
    107                   /*inout*/ HashSet<std::string>* image_classes,
    108                   /*out*/ VerificationResults* verification_results)
    109       REQUIRES(!Locks::mutator_lock_);
    110   void CompileAll(jobject class_loader,
    111                   const std::vector<const DexFile*>& dex_files,
    112                   TimingLogger* timings)
    113       REQUIRES(!Locks::mutator_lock_);
    114 
    115   const CompilerOptions& GetCompilerOptions() const {
    116     return *compiler_options_;
    117   }
    118 
    119   Compiler* GetCompiler() const {
    120     return compiler_.get();
    121   }
    122 
    123   // Generate the trampolines that are invoked by unresolved direct methods.
    124   std::unique_ptr<const std::vector<uint8_t>> CreateJniDlsymLookup() const;
    125   std::unique_ptr<const std::vector<uint8_t>> CreateQuickGenericJniTrampoline() const;
    126   std::unique_ptr<const std::vector<uint8_t>> CreateQuickImtConflictTrampoline() const;
    127   std::unique_ptr<const std::vector<uint8_t>> CreateQuickResolutionTrampoline() const;
    128   std::unique_ptr<const std::vector<uint8_t>> CreateQuickToInterpreterBridge() const;
    129 
    130   ClassStatus GetClassStatus(const ClassReference& ref) const;
    131   bool GetCompiledClass(const ClassReference& ref, ClassStatus* status) const;
    132 
    133   CompiledMethod* GetCompiledMethod(MethodReference ref) const;
    134   // Add a compiled method.
    135   void AddCompiledMethod(const MethodReference& method_ref, CompiledMethod* const compiled_method);
    136   CompiledMethod* RemoveCompiledMethod(const MethodReference& method_ref);
    137 
    138   // Resolve compiling method's class. Returns null on failure.
    139   ObjPtr<mirror::Class> ResolveCompilingMethodsClass(const ScopedObjectAccess& soa,
    140                                                      Handle<mirror::DexCache> dex_cache,
    141                                                      Handle<mirror::ClassLoader> class_loader,
    142                                                      const DexCompilationUnit* mUnit)
    143       REQUIRES_SHARED(Locks::mutator_lock_);
    144 
    145   ObjPtr<mirror::Class> ResolveClass(const ScopedObjectAccess& soa,
    146                                      Handle<mirror::DexCache> dex_cache,
    147                                      Handle<mirror::ClassLoader> class_loader,
    148                                      dex::TypeIndex type_index,
    149                                      const DexCompilationUnit* mUnit)
    150       REQUIRES_SHARED(Locks::mutator_lock_);
    151 
    152   // Resolve a field. Returns null on failure, including incompatible class change.
    153   // NOTE: Unlike ClassLinker's ResolveField(), this method enforces is_static.
    154   ArtField* ResolveField(const ScopedObjectAccess& soa,
    155                          Handle<mirror::DexCache> dex_cache,
    156                          Handle<mirror::ClassLoader> class_loader,
    157                          uint32_t field_idx,
    158                          bool is_static)
    159       REQUIRES_SHARED(Locks::mutator_lock_);
    160 
    161   // Can we fast-path an IGET/IPUT access to an instance field? If yes, compute the field offset.
    162   std::pair<bool, bool> IsFastInstanceField(ObjPtr<mirror::DexCache> dex_cache,
    163                                             ObjPtr<mirror::Class> referrer_class,
    164                                             ArtField* resolved_field,
    165                                             uint16_t field_idx)
    166       REQUIRES_SHARED(Locks::mutator_lock_);
    167 
    168   void ProcessedInstanceField(bool resolved);
    169   void ProcessedStaticField(bool resolved, bool local);
    170 
    171   // Can we fast path instance field access? Computes field's offset and volatility.
    172   bool ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put,
    173                                 MemberOffset* field_offset, bool* is_volatile)
    174       REQUIRES(!Locks::mutator_lock_);
    175 
    176   ArtField* ComputeInstanceFieldInfo(uint32_t field_idx,
    177                                      const DexCompilationUnit* mUnit,
    178                                      bool is_put,
    179                                      const ScopedObjectAccess& soa)
    180       REQUIRES_SHARED(Locks::mutator_lock_);
    181 
    182 
    183   bool IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc);
    184 
    185   size_t GetThreadCount() const {
    186     return parallel_thread_count_;
    187   }
    188 
    189   void SetDedupeEnabled(bool dedupe_enabled) {
    190     compiled_method_storage_.SetDedupeEnabled(dedupe_enabled);
    191   }
    192 
    193   bool DedupeEnabled() const {
    194     return compiled_method_storage_.DedupeEnabled();
    195   }
    196 
    197   // Checks whether profile guided compilation is enabled and if the method should be compiled
    198   // according to the profile file.
    199   bool ShouldCompileBasedOnProfile(const MethodReference& method_ref) const;
    200 
    201   // Checks whether profile guided verification is enabled and if the method should be verified
    202   // according to the profile file.
    203   bool ShouldVerifyClassBasedOnProfile(const DexFile& dex_file, uint16_t class_idx) const;
    204 
    205   void RecordClassStatus(const ClassReference& ref, ClassStatus status);
    206 
    207   // Get memory usage during compilation.
    208   std::string GetMemoryUsageString(bool extended) const;
    209 
    210   void SetHadHardVerifierFailure() {
    211     had_hard_verifier_failure_ = true;
    212   }
    213   void AddSoftVerifierFailure() {
    214     number_of_soft_verifier_failures_++;
    215   }
    216 
    217   Compiler::Kind GetCompilerKind() {
    218     return compiler_kind_;
    219   }
    220 
    221   CompiledMethodStorage* GetCompiledMethodStorage() {
    222     return &compiled_method_storage_;
    223   }
    224 
    225   optimizer::DexToDexCompiler& GetDexToDexCompiler() {
    226     return dex_to_dex_compiler_;
    227   }
    228 
    229  private:
    230   void LoadImageClasses(TimingLogger* timings, /*inout*/ HashSet<std::string>* image_classes)
    231       REQUIRES(!Locks::mutator_lock_);
    232 
    233   // Attempt to resolve all type, methods, fields, and strings
    234   // referenced from code in the dex file following PathClassLoader
    235   // ordering semantics.
    236   void Resolve(jobject class_loader,
    237                const std::vector<const DexFile*>& dex_files,
    238                TimingLogger* timings)
    239       REQUIRES(!Locks::mutator_lock_);
    240   void ResolveDexFile(jobject class_loader,
    241                       const DexFile& dex_file,
    242                       const std::vector<const DexFile*>& dex_files,
    243                       ThreadPool* thread_pool,
    244                       size_t thread_count,
    245                       TimingLogger* timings)
    246       REQUIRES(!Locks::mutator_lock_);
    247 
    248   // Do fast verification through VerifierDeps if possible. Return whether
    249   // verification was successful.
    250   bool FastVerify(jobject class_loader,
    251                   const std::vector<const DexFile*>& dex_files,
    252                   TimingLogger* timings,
    253                   /*out*/ VerificationResults* verification_results);
    254 
    255   void Verify(jobject class_loader,
    256               const std::vector<const DexFile*>& dex_files,
    257               TimingLogger* timings,
    258               /*out*/ VerificationResults* verification_results);
    259 
    260   void VerifyDexFile(jobject class_loader,
    261                      const DexFile& dex_file,
    262                      const std::vector<const DexFile*>& dex_files,
    263                      ThreadPool* thread_pool,
    264                      size_t thread_count,
    265                      TimingLogger* timings)
    266       REQUIRES(!Locks::mutator_lock_);
    267 
    268   void SetVerified(jobject class_loader,
    269                    const std::vector<const DexFile*>& dex_files,
    270                    TimingLogger* timings);
    271   void SetVerifiedDexFile(jobject class_loader,
    272                           const DexFile& dex_file,
    273                           const std::vector<const DexFile*>& dex_files,
    274                           ThreadPool* thread_pool,
    275                           size_t thread_count,
    276                           TimingLogger* timings)
    277       REQUIRES(!Locks::mutator_lock_);
    278 
    279   void InitializeClasses(jobject class_loader,
    280                          const std::vector<const DexFile*>& dex_files,
    281                          TimingLogger* timings)
    282       REQUIRES(!Locks::mutator_lock_);
    283   void InitializeClasses(jobject class_loader,
    284                          const DexFile& dex_file,
    285                          const std::vector<const DexFile*>& dex_files,
    286                          TimingLogger* timings)
    287       REQUIRES(!Locks::mutator_lock_);
    288 
    289   void UpdateImageClasses(TimingLogger* timings, /*inout*/ HashSet<std::string>* image_classes)
    290       REQUIRES(!Locks::mutator_lock_);
    291 
    292   void Compile(jobject class_loader,
    293                const std::vector<const DexFile*>& dex_files,
    294                TimingLogger* timings);
    295 
    296   void CheckThreadPools();
    297 
    298   // Resolve const string literals that are loaded from dex code. If only_startup_strings is
    299   // specified, only methods that are marked startup in the profile are resolved.
    300   void ResolveConstStrings(const std::vector<const DexFile*>& dex_files,
    301                            bool only_startup_strings,
    302                            /*inout*/ TimingLogger* timings);
    303 
    304   const CompilerOptions* const compiler_options_;
    305 
    306   std::unique_ptr<Compiler> compiler_;
    307   Compiler::Kind compiler_kind_;
    308 
    309   // All class references that this compiler has compiled. Indexed by class defs.
    310   using ClassStateTable = AtomicDexRefMap<ClassReference, ClassStatus>;
    311   ClassStateTable compiled_classes_;
    312   // All class references that are in the classpath. Indexed by class defs.
    313   ClassStateTable classpath_classes_;
    314 
    315   typedef AtomicDexRefMap<MethodReference, CompiledMethod*> MethodTable;
    316 
    317   // All method references that this compiler has compiled.
    318   MethodTable compiled_methods_;
    319 
    320   std::atomic<uint32_t> number_of_soft_verifier_failures_;
    321 
    322   bool had_hard_verifier_failure_;
    323 
    324   // A thread pool that can (potentially) run tasks in parallel.
    325   size_t parallel_thread_count_;
    326   std::unique_ptr<ThreadPool> parallel_thread_pool_;
    327 
    328   // A thread pool that guarantees running single-threaded on the main thread.
    329   std::unique_ptr<ThreadPool> single_thread_pool_;
    330 
    331   class AOTCompilationStats;
    332   std::unique_ptr<AOTCompilationStats> stats_;
    333 
    334   CompiledMethodStorage compiled_method_storage_;
    335 
    336   size_t max_arena_alloc_;
    337 
    338   // Compiler for dex to dex (quickening).
    339   optimizer::DexToDexCompiler dex_to_dex_compiler_;
    340 
    341   friend class CommonCompilerDriverTest;
    342   friend class CompileClassVisitor;
    343   friend class DexToDexDecompilerTest;
    344   friend class InitializeClassVisitor;
    345   friend class verifier::VerifierDepsTest;
    346   DISALLOW_COPY_AND_ASSIGN(CompilerDriver);
    347 };
    348 
    349 }  // namespace art
    350 
    351 #endif  // ART_DEX2OAT_DRIVER_COMPILER_DRIVER_H_
    352