compiler_driver.h revision 13439f0c4769a0768cf1bbaa7b3f2f9ee5a317c5
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 const ProfileCompilationInfo* GetProfileCompilationInfo() const { 359 return profile_compilation_info_; 360 } 361 362 private: 363 // Can `referrer_class` access the resolved `member`? 364 // Dispatch call to mirror::Class::CanAccessResolvedField or 365 // mirror::Class::CanAccessResolvedMember depending on the value of 366 // ArtMember. 367 template <typename ArtMember> 368 static bool CanAccessResolvedMember(mirror::Class* referrer_class, 369 mirror::Class* access_to, 370 ArtMember* member, 371 mirror::DexCache* dex_cache, 372 uint32_t field_idx) 373 REQUIRES_SHARED(Locks::mutator_lock_); 374 375 private: 376 void PreCompile(jobject class_loader, 377 const std::vector<const DexFile*>& dex_files, 378 TimingLogger* timings) 379 REQUIRES(!Locks::mutator_lock_, !compiled_classes_lock_); 380 381 void LoadImageClasses(TimingLogger* timings) REQUIRES(!Locks::mutator_lock_); 382 383 // Attempt to resolve all type, methods, fields, and strings 384 // referenced from code in the dex file following PathClassLoader 385 // ordering semantics. 386 void Resolve(jobject class_loader, 387 const std::vector<const DexFile*>& dex_files, 388 TimingLogger* timings) 389 REQUIRES(!Locks::mutator_lock_); 390 void ResolveDexFile(jobject class_loader, 391 const DexFile& dex_file, 392 const std::vector<const DexFile*>& dex_files, 393 ThreadPool* thread_pool, 394 size_t thread_count, 395 TimingLogger* timings) 396 REQUIRES(!Locks::mutator_lock_); 397 398 // Do fast verification through VerifierDeps if possible. Return whether 399 // verification was successful. 400 // NO_THREAD_SAFETY_ANALYSIS as the method accesses a guarded value in a 401 // single-threaded way. 402 bool FastVerify(jobject class_loader, 403 const std::vector<const DexFile*>& dex_files, 404 TimingLogger* timings) 405 NO_THREAD_SAFETY_ANALYSIS; 406 407 void Verify(jobject class_loader, 408 const std::vector<const DexFile*>& dex_files, 409 TimingLogger* timings); 410 411 void VerifyDexFile(jobject class_loader, 412 const DexFile& dex_file, 413 const std::vector<const DexFile*>& dex_files, 414 ThreadPool* thread_pool, 415 size_t thread_count, 416 TimingLogger* timings) 417 REQUIRES(!Locks::mutator_lock_); 418 419 void SetVerified(jobject class_loader, 420 const std::vector<const DexFile*>& dex_files, 421 TimingLogger* timings); 422 void SetVerifiedDexFile(jobject class_loader, 423 const DexFile& dex_file, 424 const std::vector<const DexFile*>& dex_files, 425 ThreadPool* thread_pool, 426 size_t thread_count, 427 TimingLogger* timings) 428 REQUIRES(!Locks::mutator_lock_); 429 430 void InitializeClasses(jobject class_loader, 431 const std::vector<const DexFile*>& dex_files, 432 TimingLogger* timings) 433 REQUIRES(!Locks::mutator_lock_, !compiled_classes_lock_); 434 void InitializeClasses(jobject class_loader, 435 const DexFile& dex_file, 436 const std::vector<const DexFile*>& dex_files, 437 TimingLogger* timings) 438 REQUIRES(!Locks::mutator_lock_, !compiled_classes_lock_); 439 440 void UpdateImageClasses(TimingLogger* timings) REQUIRES(!Locks::mutator_lock_); 441 442 void Compile(jobject class_loader, 443 const std::vector<const DexFile*>& dex_files, 444 TimingLogger* timings) REQUIRES(!dex_to_dex_references_lock_); 445 void CompileDexFile(jobject class_loader, 446 const DexFile& dex_file, 447 const std::vector<const DexFile*>& dex_files, 448 ThreadPool* thread_pool, 449 size_t thread_count, 450 TimingLogger* timings) 451 REQUIRES(!Locks::mutator_lock_); 452 453 bool MayInlineInternal(const DexFile* inlined_from, const DexFile* inlined_into) const; 454 455 void InitializeThreadPools(); 456 void FreeThreadPools(); 457 void CheckThreadPools(); 458 459 bool RequiresConstructorBarrier(const DexFile& dex_file, uint16_t class_def_idx) const; 460 461 const CompilerOptions* const compiler_options_; 462 VerificationResults* const verification_results_; 463 464 std::unique_ptr<Compiler> compiler_; 465 Compiler::Kind compiler_kind_; 466 467 const InstructionSet instruction_set_; 468 const InstructionSetFeatures* const instruction_set_features_; 469 470 // All class references that require constructor barriers. If the class reference is not in the 471 // set then the result has not yet been computed. 472 mutable ReaderWriterMutex requires_constructor_barrier_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 473 std::map<ClassReference, bool> requires_constructor_barrier_ 474 GUARDED_BY(requires_constructor_barrier_lock_); 475 476 typedef SafeMap<const ClassReference, CompiledClass*> ClassTable; 477 // All class references that this compiler has compiled. 478 mutable Mutex compiled_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 479 ClassTable compiled_classes_ GUARDED_BY(compiled_classes_lock_); 480 481 typedef AtomicMethodRefMap<CompiledMethod*> MethodTable; 482 483 private: 484 // All method references that this compiler has compiled. 485 MethodTable compiled_methods_; 486 487 // Number of non-relative patches in all compiled methods. These patches need space 488 // in the .oat_patches ELF section if requested in the compiler options. 489 Atomic<size_t> non_relative_linker_patch_count_; 490 491 // If image_ is true, specifies the classes that will be included in the image. 492 // Note if image_classes_ is null, all classes are included in the image. 493 std::unique_ptr<std::unordered_set<std::string>> image_classes_; 494 495 // Specifies the classes that will be compiled. Note that if classes_to_compile_ is null, 496 // all classes are eligible for compilation (duplication filters etc. will still apply). 497 // This option may be restricted to the boot image, depending on a flag in the implementation. 498 std::unique_ptr<std::unordered_set<std::string>> classes_to_compile_; 499 500 // Specifies the methods that will be compiled. Note that if methods_to_compile_ is null, 501 // all methods are eligible for compilation (compilation filters etc. will still apply). 502 // This option may be restricted to the boot image, depending on a flag in the implementation. 503 std::unique_ptr<std::unordered_set<std::string>> methods_to_compile_; 504 505 bool had_hard_verifier_failure_; 506 507 // A thread pool that can (potentially) run tasks in parallel. 508 std::unique_ptr<ThreadPool> parallel_thread_pool_; 509 size_t parallel_thread_count_; 510 511 // A thread pool that guarantees running single-threaded on the main thread. 512 std::unique_ptr<ThreadPool> single_thread_pool_; 513 514 class AOTCompilationStats; 515 std::unique_ptr<AOTCompilationStats> stats_; 516 517 bool dump_stats_; 518 const bool dump_passes_; 519 520 CumulativeLogger* const timings_logger_; 521 522 typedef void (*CompilerCallbackFn)(CompilerDriver& driver); 523 typedef MutexLock* (*CompilerMutexLockFn)(CompilerDriver& driver); 524 525 void* compiler_context_; 526 527 bool support_boot_image_fixup_; 528 529 // List of dex files that will be stored in the oat file. 530 const std::vector<const DexFile*>* dex_files_for_oat_file_; 531 532 CompiledMethodStorage compiled_method_storage_; 533 534 // Info for profile guided compilation. 535 const ProfileCompilationInfo* const profile_compilation_info_; 536 537 size_t max_arena_alloc_; 538 539 // Data for delaying dex-to-dex compilation. 540 Mutex dex_to_dex_references_lock_; 541 // In the first phase, dex_to_dex_references_ collects methods for dex-to-dex compilation. 542 class DexFileMethodSet; 543 std::vector<DexFileMethodSet> dex_to_dex_references_ GUARDED_BY(dex_to_dex_references_lock_); 544 // In the second phase, current_dex_to_dex_methods_ points to the BitVector with method 545 // indexes for dex-to-dex compilation in the current dex file. 546 const BitVector* current_dex_to_dex_methods_; 547 548 friend class CompileClassVisitor; 549 friend class DexToDexDecompilerTest; 550 friend class verifier::VerifierDepsTest; 551 DISALLOW_COPY_AND_ASSIGN(CompilerDriver); 552}; 553 554} // namespace art 555 556#endif // ART_COMPILER_DRIVER_COMPILER_DRIVER_H_ 557