compiler_driver.h revision 4c0eb42259d790fddcd9978b66328dbb3ab65615
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/mutex.h" 28#include "base/timing_logger.h" 29#include "class_reference.h" 30#include "compiler.h" 31#include "dex_file.h" 32#include "invoke_type.h" 33#include "method_reference.h" 34#include "mirror/class.h" // For mirror::Class::Status. 35#include "os.h" 36#include "profiler.h" 37#include "runtime.h" 38#include "safe_map.h" 39#include "thread_pool.h" 40#include "utils/array_ref.h" 41#include "utils/dedupe_set.h" 42#include "utils/dex_cache_arrays_layout.h" 43#include "utils/swap_space.h" 44#include "utils.h" 45 46namespace art { 47 48namespace mirror { 49class DexCache; 50} // namespace mirror 51 52namespace verifier { 53class MethodVerifier; 54} // namespace verifier 55 56class CompiledClass; 57class CompiledMethod; 58class CompilerOptions; 59class DexCompilationUnit; 60class DexFileToMethodInlinerMap; 61struct InlineIGetIPutData; 62class InstructionSetFeatures; 63class OatWriter; 64class ParallelCompilationManager; 65class ScopedObjectAccess; 66template <class Allocator> class SrcMap; 67class SrcMapElem; 68using SwapSrcMap = SrcMap<SwapAllocator<SrcMapElem>>; 69template<class T> class Handle; 70class TimingLogger; 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 83enum DexToDexCompilationLevel { 84 kDontDexToDexCompile, // Only meaning wrt image time interpretation. 85 kRequired, // Dex-to-dex compilation required for correctness. 86 kOptimize // Perform required transformation and peep-hole optimizations. 87}; 88std::ostream& operator<<(std::ostream& os, const DexToDexCompilationLevel& rhs); 89 90static constexpr bool kUseMurmur3Hash = true; 91 92class CompilerDriver { 93 public: 94 // Create a compiler targeting the requested "instruction_set". 95 // "image" should be true if image specific optimizations should be 96 // enabled. "image_classes" lets the compiler know what classes it 97 // can assume will be in the image, with nullptr implying all available 98 // classes. 99 explicit CompilerDriver(const CompilerOptions* compiler_options, 100 VerificationResults* verification_results, 101 DexFileToMethodInlinerMap* method_inliner_map, 102 Compiler::Kind compiler_kind, 103 InstructionSet instruction_set, 104 const InstructionSetFeatures* instruction_set_features, 105 bool image, std::unordered_set<std::string>* image_classes, 106 std::unordered_set<std::string>* compiled_classes, 107 size_t thread_count, bool dump_stats, bool dump_passes, 108 const std::string& dump_cfg_file_name, 109 CumulativeLogger* timer, int swap_fd, 110 const std::string& profile_file); 111 112 ~CompilerDriver(); 113 114 void CompileAll(jobject class_loader, const std::vector<const DexFile*>& dex_files, 115 TimingLogger* timings) 116 LOCKS_EXCLUDED(Locks::mutator_lock_); 117 118 CompiledMethod* CompileMethod(Thread* self, mirror::ArtMethod*) 119 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) WARN_UNUSED; 120 121 // Compile a single Method. 122 void CompileOne(Thread* self, mirror::ArtMethod* method, TimingLogger* timings) 123 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 124 125 VerificationResults* GetVerificationResults() const { 126 return verification_results_; 127 } 128 129 DexFileToMethodInlinerMap* GetMethodInlinerMap() const { 130 return method_inliner_map_; 131 } 132 133 InstructionSet GetInstructionSet() const { 134 return instruction_set_; 135 } 136 137 const InstructionSetFeatures* GetInstructionSetFeatures() const { 138 return instruction_set_features_; 139 } 140 141 const CompilerOptions& GetCompilerOptions() const { 142 return *compiler_options_; 143 } 144 145 Compiler* GetCompiler() const { 146 return compiler_.get(); 147 } 148 149 bool ProfilePresent() const { 150 return profile_present_; 151 } 152 153 // Are we compiling and creating an image file? 154 bool IsImage() const { 155 return image_; 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 const std::vector<uint8_t>* CreateInterpreterToInterpreterBridge() const 164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 165 const std::vector<uint8_t>* CreateInterpreterToCompiledCodeBridge() const 166 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 167 const std::vector<uint8_t>* CreateJniDlsymLookup() const 168 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 169 const std::vector<uint8_t>* CreateQuickGenericJniTrampoline() const 170 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 171 const std::vector<uint8_t>* CreateQuickImtConflictTrampoline() const 172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 173 const std::vector<uint8_t>* CreateQuickResolutionTrampoline() const 174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 175 const std::vector<uint8_t>* CreateQuickToInterpreterBridge() const 176 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 177 178 CompiledClass* GetCompiledClass(ClassReference ref) const 179 LOCKS_EXCLUDED(compiled_classes_lock_); 180 181 CompiledMethod* GetCompiledMethod(MethodReference ref) const 182 LOCKS_EXCLUDED(compiled_methods_lock_); 183 size_t GetNonRelativeLinkerPatchCount() const 184 LOCKS_EXCLUDED(compiled_methods_lock_); 185 186 // Remove and delete a compiled method. 187 void RemoveCompiledMethod(const MethodReference& method_ref); 188 189 void AddRequiresConstructorBarrier(Thread* self, const DexFile* dex_file, 190 uint16_t class_def_index); 191 bool RequiresConstructorBarrier(Thread* self, const DexFile* dex_file, 192 uint16_t class_def_index) const; 193 194 // Callbacks from compiler to see what runtime checks must be generated. 195 196 bool CanAssumeTypeIsPresentInDexCache(const DexFile& dex_file, uint32_t type_idx); 197 198 bool CanAssumeStringIsPresentInDexCache(const DexFile& dex_file, uint32_t string_idx) 199 LOCKS_EXCLUDED(Locks::mutator_lock_); 200 201 // Are runtime access checks necessary in the compiled code? 202 bool CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file, 203 uint32_t type_idx, bool* type_known_final = nullptr, 204 bool* type_known_abstract = nullptr, 205 bool* equals_referrers_class = nullptr) 206 LOCKS_EXCLUDED(Locks::mutator_lock_); 207 208 // Are runtime access and instantiable checks necessary in the code? 209 bool CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file, 210 uint32_t type_idx) 211 LOCKS_EXCLUDED(Locks::mutator_lock_); 212 213 bool CanEmbedTypeInCode(const DexFile& dex_file, uint32_t type_idx, 214 bool* is_type_initialized, bool* use_direct_type_ptr, 215 uintptr_t* direct_type_ptr, bool* out_is_finalizable); 216 217 // Query methods for the java.lang.ref.Reference class. 218 bool CanEmbedReferenceTypeInCode(ClassReference* ref, 219 bool* use_direct_type_ptr, uintptr_t* direct_type_ptr); 220 uint32_t GetReferenceSlowFlagOffset() const; 221 uint32_t GetReferenceDisableFlagOffset() const; 222 223 // Get the DexCache for the 224 mirror::DexCache* GetDexCache(const DexCompilationUnit* mUnit) 225 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 226 227 mirror::ClassLoader* GetClassLoader(ScopedObjectAccess& soa, const DexCompilationUnit* mUnit) 228 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 229 230 // Resolve compiling method's class. Returns nullptr on failure. 231 mirror::Class* ResolveCompilingMethodsClass( 232 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, 233 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit) 234 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 235 236 mirror::Class* ResolveClass( 237 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, 238 Handle<mirror::ClassLoader> class_loader, uint16_t type_index, 239 const DexCompilationUnit* mUnit) 240 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 241 242 // Resolve a field. Returns nullptr on failure, including incompatible class change. 243 // NOTE: Unlike ClassLinker's ResolveField(), this method enforces is_static. 244 ArtField* ResolveField( 245 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, 246 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit, 247 uint32_t field_idx, bool is_static) 248 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 249 250 // Resolve a field with a given dex file. 251 ArtField* ResolveFieldWithDexFile( 252 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, 253 Handle<mirror::ClassLoader> class_loader, const DexFile* dex_file, 254 uint32_t field_idx, bool is_static) 255 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 256 257 // Get declaration location of a resolved field. 258 void GetResolvedFieldDexFileLocation( 259 ArtField* resolved_field, const DexFile** declaring_dex_file, 260 uint16_t* declaring_class_idx, uint16_t* declaring_field_idx) 261 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 262 263 bool IsFieldVolatile(ArtField* field) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 264 MemberOffset GetFieldOffset(ArtField* field) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 265 266 // Find a dex cache for a dex file. 267 inline mirror::DexCache* FindDexCache(const DexFile* dex_file) 268 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 269 270 // Can we fast-path an IGET/IPUT access to an instance field? If yes, compute the field offset. 271 std::pair<bool, bool> IsFastInstanceField( 272 mirror::DexCache* dex_cache, mirror::Class* referrer_class, 273 ArtField* resolved_field, uint16_t field_idx) 274 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 275 276 // Can we fast-path an SGET/SPUT access to a static field? If yes, compute the type index 277 // of the declaring class in the referrer's dex file. 278 std::pair<bool, bool> IsFastStaticField( 279 mirror::DexCache* dex_cache, mirror::Class* referrer_class, 280 ArtField* resolved_field, uint16_t field_idx, uint32_t* storage_index) 281 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 282 283 // Return whether the declaring class of `resolved_method` is 284 // available to `referrer_class`. If this is true, compute the type 285 // index of the declaring class in the referrer's dex file and 286 // return it through the out argument `storage_index`; otherwise 287 // return DexFile::kDexNoIndex through `storage_index`. 288 bool IsClassOfStaticMethodAvailableToReferrer(mirror::DexCache* dex_cache, 289 mirror::Class* referrer_class, 290 mirror::ArtMethod* resolved_method, 291 uint16_t method_idx, 292 uint32_t* storage_index) 293 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 294 295 // Is static field's in referrer's class? 296 bool IsStaticFieldInReferrerClass(mirror::Class* referrer_class, ArtField* resolved_field) 297 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 298 299 // Is static field's class initialized? 300 bool IsStaticFieldsClassInitialized(mirror::Class* referrer_class, 301 ArtField* resolved_field) 302 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 303 304 // Resolve a method. Returns nullptr on failure, including incompatible class change. 305 mirror::ArtMethod* ResolveMethod( 306 ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, 307 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit, 308 uint32_t method_idx, InvokeType invoke_type, bool check_incompatible_class_change = true) 309 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 310 311 // Get declaration location of a resolved field. 312 void GetResolvedMethodDexFileLocation( 313 mirror::ArtMethod* resolved_method, const DexFile** declaring_dex_file, 314 uint16_t* declaring_class_idx, uint16_t* declaring_method_idx) 315 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 316 317 // Get the index in the vtable of the method. 318 uint16_t GetResolvedMethodVTableIndex( 319 mirror::ArtMethod* resolved_method, InvokeType type) 320 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 321 322 // Can we fast-path an INVOKE? If no, returns 0. If yes, returns a non-zero opaque flags value 323 // for ProcessedInvoke() and computes the necessary lowering info. 324 int IsFastInvoke( 325 ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, 326 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit, 327 mirror::Class* referrer_class, mirror::ArtMethod* resolved_method, InvokeType* invoke_type, 328 MethodReference* target_method, const MethodReference* devirt_target, 329 uintptr_t* direct_code, uintptr_t* direct_method) 330 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 331 332 // Is method's class initialized for an invoke? 333 // For static invokes to determine whether we need to consider potential call to <clinit>(). 334 // For non-static invokes, assuming a non-null reference, the class is always initialized. 335 bool IsMethodsClassInitialized(mirror::Class* referrer_class, mirror::ArtMethod* resolved_method) 336 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 337 338 // Get the layout of dex cache arrays for a dex file. Returns invalid layout if the 339 // dex cache arrays don't have a fixed layout. 340 DexCacheArraysLayout GetDexCacheArraysLayout(const DexFile* dex_file); 341 342 void ProcessedInstanceField(bool resolved); 343 void ProcessedStaticField(bool resolved, bool local); 344 void ProcessedInvoke(InvokeType invoke_type, int flags); 345 346 void ComputeFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, 347 const ScopedObjectAccess& soa, bool is_static, 348 ArtField** resolved_field, 349 mirror::Class** referrer_class, 350 mirror::DexCache** dex_cache) 351 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 352 353 // Can we fast path instance field access? Computes field's offset and volatility. 354 bool ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put, 355 MemberOffset* field_offset, bool* is_volatile) 356 LOCKS_EXCLUDED(Locks::mutator_lock_); 357 358 ArtField* ComputeInstanceFieldInfo(uint32_t field_idx, 359 const DexCompilationUnit* mUnit, 360 bool is_put, 361 const ScopedObjectAccess& soa) 362 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 363 364 365 // Can we fastpath static field access? Computes field's offset, volatility and whether the 366 // field is within the referrer (which can avoid checking class initialization). 367 bool ComputeStaticFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put, 368 MemberOffset* field_offset, uint32_t* storage_index, 369 bool* is_referrers_class, bool* is_volatile, bool* is_initialized, 370 Primitive::Type* type) 371 LOCKS_EXCLUDED(Locks::mutator_lock_); 372 373 // Can we fastpath a interface, super class or virtual method call? Computes method's vtable 374 // index. 375 bool ComputeInvokeInfo(const DexCompilationUnit* mUnit, const uint32_t dex_pc, 376 bool update_stats, bool enable_devirtualization, 377 InvokeType* type, MethodReference* target_method, int* vtable_idx, 378 uintptr_t* direct_code, uintptr_t* direct_method) 379 LOCKS_EXCLUDED(Locks::mutator_lock_); 380 381 const VerifiedMethod* GetVerifiedMethod(const DexFile* dex_file, uint32_t method_idx) const; 382 bool IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc); 383 384 bool GetSupportBootImageFixup() const { 385 return support_boot_image_fixup_; 386 } 387 388 void SetSupportBootImageFixup(bool support_boot_image_fixup) { 389 support_boot_image_fixup_ = support_boot_image_fixup; 390 } 391 392 SwapAllocator<void>& GetSwapSpaceAllocator() { 393 return *swap_space_allocator_.get(); 394 } 395 396 bool WriteElf(const std::string& android_root, 397 bool is_host, 398 const std::vector<const DexFile*>& dex_files, 399 OatWriter* oat_writer, 400 File* file); 401 402 void SetCompilerContext(void* compiler_context) { 403 compiler_context_ = compiler_context; 404 } 405 406 void* GetCompilerContext() const { 407 return compiler_context_; 408 } 409 410 size_t GetThreadCount() const { 411 return thread_count_; 412 } 413 414 bool GetDumpStats() const { 415 return dump_stats_; 416 } 417 418 bool GetDumpPasses() const { 419 return dump_passes_; 420 } 421 422 const std::string& GetDumpCfgFileName() const { 423 return dump_cfg_file_name_; 424 } 425 426 CumulativeLogger* GetTimingsLogger() const { 427 return timings_logger_; 428 } 429 430 void SetDedupeEnabled(bool dedupe_enabled) { 431 dedupe_enabled_ = dedupe_enabled; 432 } 433 bool DedupeEnabled() const { 434 return dedupe_enabled_; 435 } 436 437 // Checks if class specified by type_idx is one of the image_classes_ 438 bool IsImageClass(const char* descriptor) const; 439 440 // Checks whether the provided class should be compiled, i.e., is in classes_to_compile_. 441 bool IsClassToCompile(const char* descriptor) const; 442 443 void RecordClassStatus(ClassReference ref, mirror::Class::Status status) 444 LOCKS_EXCLUDED(compiled_classes_lock_); 445 446 // Checks if the specified method has been verified without failures. Returns 447 // false if the method is not in the verification results (GetVerificationResults). 448 bool IsMethodVerifiedWithoutFailures(uint32_t method_idx, 449 uint16_t class_def_idx, 450 const DexFile& dex_file) const; 451 452 SwapVector<uint8_t>* DeduplicateCode(const ArrayRef<const uint8_t>& code); 453 SwapSrcMap* DeduplicateSrcMappingTable(const ArrayRef<SrcMapElem>& src_map); 454 SwapVector<uint8_t>* DeduplicateMappingTable(const ArrayRef<const uint8_t>& code); 455 SwapVector<uint8_t>* DeduplicateVMapTable(const ArrayRef<const uint8_t>& code); 456 SwapVector<uint8_t>* DeduplicateGCMap(const ArrayRef<const uint8_t>& code); 457 SwapVector<uint8_t>* DeduplicateCFIInfo(const ArrayRef<const uint8_t>& cfi_info); 458 459 // Should the compiler run on this method given profile information? 460 bool SkipCompilation(const std::string& method_name); 461 462 // Get memory usage during compilation. 463 std::string GetMemoryUsageString(bool extended) const; 464 465 void SetHadHardVerifierFailure() { 466 had_hard_verifier_failure_ = true; 467 } 468 469 private: 470 // Return whether the declaring class of `resolved_member` is 471 // available to `referrer_class` for read or write access using two 472 // Boolean values returned as a pair. If is true at least for read 473 // access, compute the type index of the declaring class in the 474 // referrer's dex file and return it through the out argument 475 // `storage_index`; otherwise return DexFile::kDexNoIndex through 476 // `storage_index`. 477 template <typename ArtMember> 478 std::pair<bool, bool> IsClassOfStaticMemberAvailableToReferrer(mirror::DexCache* dex_cache, 479 mirror::Class* referrer_class, 480 ArtMember* resolved_member, 481 uint16_t member_idx, 482 uint32_t* storage_index) 483 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 484 485 // Can `referrer_class` access the resolved `member`? 486 // Dispatch call to mirror::Class::CanAccessResolvedField or 487 // mirror::Class::CanAccessResolvedMember depending on the value of 488 // ArtMember. 489 template <typename ArtMember> 490 static bool CanAccessResolvedMember(mirror::Class* referrer_class, 491 mirror::Class* access_to, 492 ArtMember* member, 493 mirror::DexCache* dex_cache, 494 uint32_t field_idx) 495 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 496 497 // These flags are internal to CompilerDriver for collecting INVOKE resolution statistics. 498 // The only external contract is that unresolved method has flags 0 and resolved non-0. 499 enum { 500 kBitMethodResolved = 0, 501 kBitVirtualMadeDirect, 502 kBitPreciseTypeDevirtualization, 503 kBitDirectCallToBoot, 504 kBitDirectMethodToBoot 505 }; 506 static constexpr int kFlagMethodResolved = 1 << kBitMethodResolved; 507 static constexpr int kFlagVirtualMadeDirect = 1 << kBitVirtualMadeDirect; 508 static constexpr int kFlagPreciseTypeDevirtualization = 1 << kBitPreciseTypeDevirtualization; 509 static constexpr int kFlagDirectCallToBoot = 1 << kBitDirectCallToBoot; 510 static constexpr int kFlagDirectMethodToBoot = 1 << kBitDirectMethodToBoot; 511 static constexpr int kFlagsMethodResolvedVirtualMadeDirect = 512 kFlagMethodResolved | kFlagVirtualMadeDirect; 513 static constexpr int kFlagsMethodResolvedPreciseTypeDevirtualization = 514 kFlagsMethodResolvedVirtualMadeDirect | kFlagPreciseTypeDevirtualization; 515 516 public: // TODO make private or eliminate. 517 // Compute constant code and method pointers when possible. 518 void GetCodeAndMethodForDirectCall(/*out*/InvokeType* type, 519 InvokeType sharp_type, 520 bool no_guarantee_of_dex_cache_entry, 521 const mirror::Class* referrer_class, 522 mirror::ArtMethod* method, 523 /*out*/int* stats_flags, 524 MethodReference* target_method, 525 uintptr_t* direct_code, uintptr_t* direct_method) 526 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 527 528 private: 529 DexToDexCompilationLevel GetDexToDexCompilationlevel( 530 Thread* self, Handle<mirror::ClassLoader> class_loader, const DexFile& dex_file, 531 const DexFile::ClassDef& class_def) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 532 533 void PreCompile(jobject class_loader, const std::vector<const DexFile*>& dex_files, 534 ThreadPool* thread_pool, TimingLogger* timings) 535 LOCKS_EXCLUDED(Locks::mutator_lock_); 536 537 void LoadImageClasses(TimingLogger* timings); 538 539 // Attempt to resolve all type, methods, fields, and strings 540 // referenced from code in the dex file following PathClassLoader 541 // ordering semantics. 542 void Resolve(jobject class_loader, const std::vector<const DexFile*>& dex_files, 543 ThreadPool* thread_pool, TimingLogger* timings) 544 LOCKS_EXCLUDED(Locks::mutator_lock_); 545 void ResolveDexFile(jobject class_loader, const DexFile& dex_file, 546 const std::vector<const DexFile*>& dex_files, 547 ThreadPool* thread_pool, TimingLogger* timings) 548 LOCKS_EXCLUDED(Locks::mutator_lock_); 549 550 void Verify(jobject class_loader, const std::vector<const DexFile*>& dex_files, 551 ThreadPool* thread_pool, TimingLogger* timings); 552 void VerifyDexFile(jobject class_loader, const DexFile& dex_file, 553 const std::vector<const DexFile*>& dex_files, 554 ThreadPool* thread_pool, TimingLogger* timings) 555 LOCKS_EXCLUDED(Locks::mutator_lock_); 556 557 void SetVerified(jobject class_loader, const std::vector<const DexFile*>& dex_files, 558 ThreadPool* thread_pool, TimingLogger* timings); 559 void SetVerifiedDexFile(jobject class_loader, const DexFile& dex_file, 560 const std::vector<const DexFile*>& dex_files, 561 ThreadPool* thread_pool, TimingLogger* timings) 562 LOCKS_EXCLUDED(Locks::mutator_lock_); 563 564 void InitializeClasses(jobject class_loader, const std::vector<const DexFile*>& dex_files, 565 ThreadPool* thread_pool, TimingLogger* timings) 566 LOCKS_EXCLUDED(Locks::mutator_lock_); 567 void InitializeClasses(jobject class_loader, const DexFile& dex_file, 568 const std::vector<const DexFile*>& dex_files, 569 ThreadPool* thread_pool, TimingLogger* timings) 570 LOCKS_EXCLUDED(Locks::mutator_lock_, compiled_classes_lock_); 571 572 void UpdateImageClasses(TimingLogger* timings) LOCKS_EXCLUDED(Locks::mutator_lock_); 573 static void FindClinitImageClassesCallback(mirror::Object* object, void* arg) 574 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 575 576 void Compile(jobject class_loader, const std::vector<const DexFile*>& dex_files, 577 ThreadPool* thread_pool, TimingLogger* timings); 578 void CompileDexFile(jobject class_loader, const DexFile& dex_file, 579 const std::vector<const DexFile*>& dex_files, 580 ThreadPool* thread_pool, TimingLogger* timings) 581 LOCKS_EXCLUDED(Locks::mutator_lock_); 582 void CompileMethod(Thread* self, const DexFile::CodeItem* code_item, uint32_t access_flags, 583 InvokeType invoke_type, uint16_t class_def_idx, uint32_t method_idx, 584 jobject class_loader, const DexFile& dex_file, 585 DexToDexCompilationLevel dex_to_dex_compilation_level, 586 bool compilation_enabled) 587 LOCKS_EXCLUDED(compiled_methods_lock_); 588 589 static void CompileClass(const ParallelCompilationManager* context, size_t class_def_index) 590 LOCKS_EXCLUDED(Locks::mutator_lock_); 591 592 // Swap pool and allocator used for native allocations. May be file-backed. Needs to be first 593 // as other fields rely on this. 594 std::unique_ptr<SwapSpace> swap_space_; 595 std::unique_ptr<SwapAllocator<void> > swap_space_allocator_; 596 597 ProfileFile profile_file_; 598 bool profile_present_; 599 600 const CompilerOptions* const compiler_options_; 601 VerificationResults* const verification_results_; 602 DexFileToMethodInlinerMap* const method_inliner_map_; 603 604 std::unique_ptr<Compiler> compiler_; 605 Compiler::Kind compiler_kind_; 606 607 const InstructionSet instruction_set_; 608 const InstructionSetFeatures* const instruction_set_features_; 609 610 // All class references that require 611 mutable ReaderWriterMutex freezing_constructor_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 612 std::set<ClassReference> freezing_constructor_classes_ GUARDED_BY(freezing_constructor_lock_); 613 614 typedef SafeMap<const ClassReference, CompiledClass*> ClassTable; 615 // All class references that this compiler has compiled. 616 mutable Mutex compiled_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 617 ClassTable compiled_classes_ GUARDED_BY(compiled_classes_lock_); 618 619 typedef SafeMap<const MethodReference, CompiledMethod*, MethodReferenceComparator> MethodTable; 620 // All method references that this compiler has compiled. 621 mutable Mutex compiled_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 622 MethodTable compiled_methods_ GUARDED_BY(compiled_methods_lock_); 623 // Number of non-relative patches in all compiled methods. These patches need space 624 // in the .oat_patches ELF section if requested in the compiler options. 625 size_t non_relative_linker_patch_count_ GUARDED_BY(compiled_methods_lock_); 626 627 const bool image_; 628 629 // If image_ is true, specifies the classes that will be included in 630 // the image. Note if image_classes_ is nullptr, all classes are 631 // included in the image. 632 std::unique_ptr<std::unordered_set<std::string>> image_classes_; 633 634 // Specifies the classes that will be compiled. Note that if classes_to_compile_ is nullptr, 635 // all classes are eligible for compilation (duplication filters etc. will still apply). 636 // This option may be restricted to the boot image, depending on a flag in the implementation. 637 std::unique_ptr<std::unordered_set<std::string>> classes_to_compile_; 638 639 bool had_hard_verifier_failure_; 640 641 size_t thread_count_; 642 643 class AOTCompilationStats; 644 std::unique_ptr<AOTCompilationStats> stats_; 645 646 bool dedupe_enabled_; 647 bool dump_stats_; 648 const bool dump_passes_; 649 const std::string& dump_cfg_file_name_; 650 651 CumulativeLogger* const timings_logger_; 652 653 typedef void (*CompilerCallbackFn)(CompilerDriver& driver); 654 typedef MutexLock* (*CompilerMutexLockFn)(CompilerDriver& driver); 655 656 typedef void (*DexToDexCompilerFn)(CompilerDriver& driver, 657 const DexFile::CodeItem* code_item, 658 uint32_t access_flags, InvokeType invoke_type, 659 uint32_t class_dex_idx, uint32_t method_idx, 660 jobject class_loader, const DexFile& dex_file, 661 DexToDexCompilationLevel dex_to_dex_compilation_level); 662 DexToDexCompilerFn dex_to_dex_compiler_; 663 664 void* compiler_context_; 665 666 bool support_boot_image_fixup_; 667 668 // DeDuplication data structures, these own the corresponding byte arrays. 669 template <typename ContentType> 670 class DedupeHashFunc { 671 public: 672 size_t operator()(const ArrayRef<ContentType>& array) const { 673 const uint8_t* data = reinterpret_cast<const uint8_t*>(array.data()); 674 static_assert(IsPowerOfTwo(sizeof(ContentType)), 675 "ContentType is not power of two, don't know whether array layout is as assumed"); 676 uint32_t len = sizeof(ContentType) * array.size(); 677 if (kUseMurmur3Hash) { 678 static constexpr uint32_t c1 = 0xcc9e2d51; 679 static constexpr uint32_t c2 = 0x1b873593; 680 static constexpr uint32_t r1 = 15; 681 static constexpr uint32_t r2 = 13; 682 static constexpr uint32_t m = 5; 683 static constexpr uint32_t n = 0xe6546b64; 684 685 uint32_t hash = 0; 686 687 const int nblocks = len / 4; 688 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t; 689 const unaligned_uint32_t *blocks = reinterpret_cast<const uint32_t*>(data); 690 int i; 691 for (i = 0; i < nblocks; i++) { 692 uint32_t k = blocks[i]; 693 k *= c1; 694 k = (k << r1) | (k >> (32 - r1)); 695 k *= c2; 696 697 hash ^= k; 698 hash = ((hash << r2) | (hash >> (32 - r2))) * m + n; 699 } 700 701 const uint8_t *tail = reinterpret_cast<const uint8_t*>(data + nblocks * 4); 702 uint32_t k1 = 0; 703 704 switch (len & 3) { 705 case 3: 706 k1 ^= tail[2] << 16; 707 FALLTHROUGH_INTENDED; 708 case 2: 709 k1 ^= tail[1] << 8; 710 FALLTHROUGH_INTENDED; 711 case 1: 712 k1 ^= tail[0]; 713 714 k1 *= c1; 715 k1 = (k1 << r1) | (k1 >> (32 - r1)); 716 k1 *= c2; 717 hash ^= k1; 718 } 719 720 hash ^= len; 721 hash ^= (hash >> 16); 722 hash *= 0x85ebca6b; 723 hash ^= (hash >> 13); 724 hash *= 0xc2b2ae35; 725 hash ^= (hash >> 16); 726 727 return hash; 728 } else { 729 size_t hash = 0x811c9dc5; 730 for (uint32_t i = 0; i < len; ++i) { 731 hash = (hash * 16777619) ^ data[i]; 732 } 733 hash += hash << 13; 734 hash ^= hash >> 7; 735 hash += hash << 3; 736 hash ^= hash >> 17; 737 hash += hash << 5; 738 return hash; 739 } 740 } 741 }; 742 743 DedupeSet<ArrayRef<const uint8_t>, 744 SwapVector<uint8_t>, size_t, DedupeHashFunc<const uint8_t>, 4> dedupe_code_; 745 DedupeSet<ArrayRef<SrcMapElem>, 746 SwapSrcMap, size_t, DedupeHashFunc<SrcMapElem>, 4> dedupe_src_mapping_table_; 747 DedupeSet<ArrayRef<const uint8_t>, 748 SwapVector<uint8_t>, size_t, DedupeHashFunc<const uint8_t>, 4> dedupe_mapping_table_; 749 DedupeSet<ArrayRef<const uint8_t>, 750 SwapVector<uint8_t>, size_t, DedupeHashFunc<const uint8_t>, 4> dedupe_vmap_table_; 751 DedupeSet<ArrayRef<const uint8_t>, 752 SwapVector<uint8_t>, size_t, DedupeHashFunc<const uint8_t>, 4> dedupe_gc_map_; 753 DedupeSet<ArrayRef<const uint8_t>, 754 SwapVector<uint8_t>, size_t, DedupeHashFunc<const uint8_t>, 4> dedupe_cfi_info_; 755 756 DISALLOW_COPY_AND_ASSIGN(CompilerDriver); 757}; 758 759} // namespace art 760 761#endif // ART_COMPILER_DRIVER_COMPILER_DRIVER_H_ 762