compiler_driver.h revision 666ee3d7c6039c80e75287e311895bd6a9b01e9f
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 <atomic> 21#include <set> 22#include <string> 23#include <unordered_set> 24#include <vector> 25 26#include "android-base/strings.h" 27 28#include "arch/instruction_set.h" 29#include "base/array_ref.h" 30#include "base/bit_utils.h" 31#include "base/mutex.h" 32#include "base/timing_logger.h" 33#include "class_reference.h" 34#include "compiler.h" 35#include "dex_file.h" 36#include "dex_file_types.h" 37#include "driver/compiled_method_storage.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; 65enum InvokeType : uint32_t; 66class ParallelCompilationManager; 67class ScopedObjectAccess; 68template <class Allocator> class SrcMap; 69template<class T> class Handle; 70class TimingLogger; 71class VdexFile; 72class VerificationResults; 73class VerifiedMethod; 74 75enum EntryPointCallingConvention { 76 // ABI of invocations to a method's interpreter entry point. 77 kInterpreterAbi, 78 // ABI of calls to a method's native code, only used for native methods. 79 kJniAbi, 80 // ABI of calls to a method's quick code entry point. 81 kQuickAbi 82}; 83 84class CompilerDriver { 85 public: 86 // Create a compiler targeting the requested "instruction_set". 87 // "image" should be true if image specific optimizations should be 88 // enabled. "image_classes" lets the compiler know what classes it 89 // can assume will be in the image, with null implying all available 90 // classes. 91 CompilerDriver(const CompilerOptions* compiler_options, 92 VerificationResults* verification_results, 93 Compiler::Kind compiler_kind, 94 InstructionSet instruction_set, 95 const InstructionSetFeatures* instruction_set_features, 96 std::unordered_set<std::string>* image_classes, 97 std::unordered_set<std::string>* compiled_classes, 98 std::unordered_set<std::string>* compiled_methods, 99 size_t thread_count, 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 // Set dex files classpath. 109 void SetClasspathDexFiles(const std::vector<const DexFile*>& dex_files); 110 111 // Get dex file that will be stored in the oat file after being compiled. 112 ArrayRef<const DexFile* const> GetDexFilesForOatFile() const { 113 return ArrayRef<const DexFile* const>(dex_files_for_oat_file_); 114 } 115 116 void CompileAll(jobject class_loader, 117 const std::vector<const DexFile*>& dex_files, 118 TimingLogger* timings) 119 REQUIRES(!Locks::mutator_lock_, !dex_to_dex_references_lock_); 120 121 // Compile a single Method. 122 void CompileOne(Thread* self, ArtMethod* method, TimingLogger* timings) 123 REQUIRES_SHARED(Locks::mutator_lock_) 124 REQUIRES(!dex_to_dex_references_lock_); 125 126 VerificationResults* GetVerificationResults() const; 127 128 InstructionSet GetInstructionSet() const { 129 return instruction_set_; 130 } 131 132 const InstructionSetFeatures* GetInstructionSetFeatures() const { 133 return instruction_set_features_; 134 } 135 136 const CompilerOptions& GetCompilerOptions() const { 137 return *compiler_options_; 138 } 139 140 Compiler* GetCompiler() const { 141 return compiler_.get(); 142 } 143 144 const std::unordered_set<std::string>* GetImageClasses() const { 145 return image_classes_.get(); 146 } 147 148 // Generate the trampolines that are invoked by unresolved direct methods. 149 std::unique_ptr<const std::vector<uint8_t>> CreateJniDlsymLookup() const; 150 std::unique_ptr<const std::vector<uint8_t>> CreateQuickGenericJniTrampoline() const; 151 std::unique_ptr<const std::vector<uint8_t>> CreateQuickImtConflictTrampoline() const; 152 std::unique_ptr<const std::vector<uint8_t>> CreateQuickResolutionTrampoline() const; 153 std::unique_ptr<const std::vector<uint8_t>> CreateQuickToInterpreterBridge() const; 154 155 mirror::Class::Status GetClassStatus(const ClassReference& ref) const; 156 bool GetCompiledClass(const ClassReference& ref, mirror::Class::Status* status) const; 157 158 CompiledMethod* GetCompiledMethod(MethodReference ref) const; 159 size_t GetNonRelativeLinkerPatchCount() const; 160 // Add a compiled method. 161 void AddCompiledMethod(const MethodReference& method_ref, 162 CompiledMethod* const compiled_method, 163 size_t non_relative_linker_patch_count); 164 165 void SetRequiresConstructorBarrier(Thread* self, 166 const DexFile* dex_file, 167 uint16_t class_def_index, 168 bool requires) 169 REQUIRES(!requires_constructor_barrier_lock_); 170 171 // Do the <init> methods for this class require a constructor barrier (prior to the return)? 172 // The answer is "yes", if and only if this class has any instance final fields. 173 // (This must not be called for any non-<init> methods; the answer would be "no"). 174 // 175 // --- 176 // 177 // JLS 17.5.1 "Semantics of final fields" mandates that all final fields are frozen at the end 178 // of the invoked constructor. The constructor barrier is a conservative implementation means of 179 // enforcing the freezes happen-before the object being constructed is observable by another 180 // thread. 181 // 182 // Note: This question only makes sense for instance constructors; 183 // static constructors (despite possibly having finals) never need 184 // a barrier. 185 // 186 // JLS 12.4.2 "Detailed Initialization Procedure" approximately describes 187 // class initialization as: 188 // 189 // lock(class.lock) 190 // class.state = initializing 191 // unlock(class.lock) 192 // 193 // invoke <clinit> 194 // 195 // lock(class.lock) 196 // class.state = initialized 197 // unlock(class.lock) <-- acts as a release 198 // 199 // The last operation in the above example acts as an atomic release 200 // for any stores in <clinit>, which ends up being stricter 201 // than what a constructor barrier needs. 202 // 203 // See also QuasiAtomic::ThreadFenceForConstructor(). 204 bool RequiresConstructorBarrier(Thread* self, 205 const DexFile* dex_file, 206 uint16_t class_def_index) 207 REQUIRES(!requires_constructor_barrier_lock_); 208 209 // Are runtime access checks necessary in the compiled code? 210 bool CanAccessTypeWithoutChecks(ObjPtr<mirror::Class> referrer_class, 211 ObjPtr<mirror::Class> resolved_class) 212 REQUIRES_SHARED(Locks::mutator_lock_); 213 214 // Are runtime access and instantiable checks necessary in the code? 215 // out_is_finalizable is set to whether the type is finalizable. 216 bool CanAccessInstantiableTypeWithoutChecks(ObjPtr<mirror::Class> referrer_class, 217 ObjPtr<mirror::Class> resolved_class, 218 bool* out_is_finalizable) 219 REQUIRES_SHARED(Locks::mutator_lock_); 220 221 // Resolve compiling method's class. Returns null on failure. 222 ObjPtr<mirror::Class> ResolveCompilingMethodsClass(const ScopedObjectAccess& soa, 223 Handle<mirror::DexCache> dex_cache, 224 Handle<mirror::ClassLoader> class_loader, 225 const DexCompilationUnit* mUnit) 226 REQUIRES_SHARED(Locks::mutator_lock_); 227 228 ObjPtr<mirror::Class> ResolveClass(const ScopedObjectAccess& soa, 229 Handle<mirror::DexCache> dex_cache, 230 Handle<mirror::ClassLoader> class_loader, 231 dex::TypeIndex type_index, 232 const DexCompilationUnit* mUnit) 233 REQUIRES_SHARED(Locks::mutator_lock_); 234 235 // Resolve a field. Returns null on failure, including incompatible class change. 236 // NOTE: Unlike ClassLinker's ResolveField(), this method enforces is_static. 237 ArtField* ResolveField(const ScopedObjectAccess& soa, 238 Handle<mirror::DexCache> dex_cache, 239 Handle<mirror::ClassLoader> class_loader, 240 uint32_t field_idx, 241 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(ObjPtr<mirror::DexCache> dex_cache, 246 ObjPtr<mirror::Class> referrer_class, 247 ArtField* resolved_field, 248 uint16_t field_idx) 249 REQUIRES_SHARED(Locks::mutator_lock_); 250 251 // Resolve a method. Returns null on failure, including incompatible class change. 252 ArtMethod* ResolveMethod( 253 ScopedObjectAccess& soa, 254 Handle<mirror::DexCache> dex_cache, 255 Handle<mirror::ClassLoader> class_loader, 256 const DexCompilationUnit* mUnit, 257 uint32_t method_idx, 258 InvokeType invoke_type) 259 REQUIRES_SHARED(Locks::mutator_lock_); 260 261 void ProcessedInstanceField(bool resolved); 262 void ProcessedStaticField(bool resolved, bool local); 263 264 // Can we fast path instance field access? Computes field's offset and volatility. 265 bool ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put, 266 MemberOffset* field_offset, bool* is_volatile) 267 REQUIRES(!Locks::mutator_lock_); 268 269 ArtField* ComputeInstanceFieldInfo(uint32_t field_idx, 270 const DexCompilationUnit* mUnit, 271 bool is_put, 272 const ScopedObjectAccess& soa) 273 REQUIRES_SHARED(Locks::mutator_lock_); 274 275 276 const VerifiedMethod* GetVerifiedMethod(const DexFile* dex_file, uint32_t method_idx) const; 277 bool IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc); 278 279 bool GetSupportBootImageFixup() const { 280 return support_boot_image_fixup_; 281 } 282 283 void SetSupportBootImageFixup(bool support_boot_image_fixup) { 284 support_boot_image_fixup_ = support_boot_image_fixup; 285 } 286 287 void SetCompilerContext(void* compiler_context) { 288 compiler_context_ = compiler_context; 289 } 290 291 void* GetCompilerContext() const { 292 return compiler_context_; 293 } 294 295 size_t GetThreadCount() const { 296 return parallel_thread_count_; 297 } 298 299 void SetDedupeEnabled(bool dedupe_enabled) { 300 compiled_method_storage_.SetDedupeEnabled(dedupe_enabled); 301 } 302 303 bool DedupeEnabled() const { 304 return compiled_method_storage_.DedupeEnabled(); 305 } 306 307 // Checks if class specified by type_idx is one of the image_classes_ 308 bool IsImageClass(const char* descriptor) const; 309 310 // Checks whether the provided class should be compiled, i.e., is in classes_to_compile_. 311 bool IsClassToCompile(const char* descriptor) const; 312 313 // Checks whether the provided method should be compiled, i.e., is in method_to_compile_. 314 bool IsMethodToCompile(const MethodReference& method_ref) const; 315 316 // Checks whether profile guided compilation is enabled and if the method should be compiled 317 // according to the profile file. 318 bool ShouldCompileBasedOnProfile(const MethodReference& method_ref) const; 319 320 // Checks whether profile guided verification is enabled and if the method should be verified 321 // according to the profile file. 322 bool ShouldVerifyClassBasedOnProfile(const DexFile& dex_file, uint16_t class_idx) const; 323 324 void RecordClassStatus(const ClassReference& ref, mirror::Class::Status status); 325 326 // Checks if the specified method has been verified without failures. Returns 327 // false if the method is not in the verification results (GetVerificationResults). 328 bool IsMethodVerifiedWithoutFailures(uint32_t method_idx, 329 uint16_t class_def_idx, 330 const DexFile& dex_file) const; 331 332 // Get memory usage during compilation. 333 std::string GetMemoryUsageString(bool extended) const; 334 335 void SetHadHardVerifierFailure() { 336 had_hard_verifier_failure_ = true; 337 } 338 void AddSoftVerifierFailure() { 339 number_of_soft_verifier_failures_++; 340 } 341 342 Compiler::Kind GetCompilerKind() { 343 return compiler_kind_; 344 } 345 346 CompiledMethodStorage* GetCompiledMethodStorage() { 347 return &compiled_method_storage_; 348 } 349 350 // Can we assume that the klass is loaded? 351 bool CanAssumeClassIsLoaded(mirror::Class* klass) 352 REQUIRES_SHARED(Locks::mutator_lock_); 353 354 bool MayInline(const DexFile* inlined_from, const DexFile* inlined_into) const { 355 if (!kIsTargetBuild) { 356 return MayInlineInternal(inlined_from, inlined_into); 357 } 358 return true; 359 } 360 361 void MarkForDexToDexCompilation(Thread* self, const MethodReference& method_ref) 362 REQUIRES(!dex_to_dex_references_lock_); 363 364 const BitVector* GetCurrentDexToDexMethods() const { 365 return current_dex_to_dex_methods_; 366 } 367 368 const ProfileCompilationInfo* GetProfileCompilationInfo() const { 369 return profile_compilation_info_; 370 } 371 372 // Is `boot_image_filename` the name of a core image (small boot 373 // image used for ART testing only)? 374 static bool IsCoreImageFilename(const std::string& boot_image_filename) { 375 // TODO: This is under-approximating... 376 return android::base::EndsWith(boot_image_filename, "core.art") 377 || android::base::EndsWith(boot_image_filename, "core-optimizing.art"); 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<ClassReference, mirror::Class::Status>; 480 ClassStateTable compiled_classes_; 481 // All class references that are in the classpath. Indexed by class defs. 482 ClassStateTable classpath_classes_; 483 484 typedef AtomicDexRefMap<MethodReference, 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 std::atomic<uint32_t> number_of_soft_verifier_failures_; 509 bool had_hard_verifier_failure_; 510 511 // A thread pool that can (potentially) run tasks in parallel. 512 std::unique_ptr<ThreadPool> parallel_thread_pool_; 513 size_t parallel_thread_count_; 514 515 // A thread pool that guarantees running single-threaded on the main thread. 516 std::unique_ptr<ThreadPool> single_thread_pool_; 517 518 class AOTCompilationStats; 519 std::unique_ptr<AOTCompilationStats> stats_; 520 521 typedef void (*CompilerCallbackFn)(CompilerDriver& driver); 522 typedef MutexLock* (*CompilerMutexLockFn)(CompilerDriver& driver); 523 524 void* compiler_context_; 525 526 bool support_boot_image_fixup_; 527 528 // List of dex files that will be stored in the oat file. 529 std::vector<const DexFile*> dex_files_for_oat_file_; 530 531 CompiledMethodStorage compiled_method_storage_; 532 533 // Info for profile guided compilation. 534 const ProfileCompilationInfo* const profile_compilation_info_; 535 536 size_t max_arena_alloc_; 537 538 // Data for delaying dex-to-dex compilation. 539 Mutex dex_to_dex_references_lock_; 540 // In the first phase, dex_to_dex_references_ collects methods for dex-to-dex compilation. 541 class DexFileMethodSet; 542 std::vector<DexFileMethodSet> dex_to_dex_references_ GUARDED_BY(dex_to_dex_references_lock_); 543 // In the second phase, current_dex_to_dex_methods_ points to the BitVector with method 544 // indexes for dex-to-dex compilation in the current dex file. 545 const BitVector* current_dex_to_dex_methods_; 546 547 friend class CompileClassVisitor; 548 friend class DexToDexDecompilerTest; 549 friend class verifier::VerifierDepsTest; 550 DISALLOW_COPY_AND_ASSIGN(CompilerDriver); 551}; 552 553} // namespace art 554 555#endif // ART_COMPILER_DRIVER_COMPILER_DRIVER_H_ 556