compiler_driver.h revision 87f8b4cf0c1d6aab3eb5d1e99cc4e7cf175ef772
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 <vector> 23 24#include "base/mutex.h" 25#include "base/timing_logger.h" 26#include "class_reference.h" 27#include "compiled_class.h" 28#include "compiled_method.h" 29#include "compiler_backend.h" 30#include "dex_file.h" 31#include "instruction_set.h" 32#include "invoke_type.h" 33#include "method_reference.h" 34#include "os.h" 35#include "runtime.h" 36#include "safe_map.h" 37#include "thread_pool.h" 38#include "utils/arena_allocator.h" 39#include "utils/dedupe_set.h" 40 41namespace art { 42 43namespace verifier { 44class MethodVerifier; 45} // namespace verifier 46 47class AOTCompilationStats; 48class CompilerOptions; 49class DexCompilationUnit; 50class DexFileToMethodInlinerMap; 51struct InlineIGetIPutData; 52class OatWriter; 53class ParallelCompilationManager; 54class ScopedObjectAccess; 55template<class T> class SirtRef; 56class TimingLogger; 57class VerificationResults; 58class VerifiedMethod; 59 60enum EntryPointCallingConvention { 61 // ABI of invocations to a method's interpreter entry point. 62 kInterpreterAbi, 63 // ABI of calls to a method's native code, only used for native methods. 64 kJniAbi, 65 // ABI of calls to a method's portable code entry point. 66 kPortableAbi, 67 // ABI of calls to a method's quick code entry point. 68 kQuickAbi 69}; 70 71enum DexToDexCompilationLevel { 72 kDontDexToDexCompile, // Only meaning wrt image time interpretation. 73 kRequired, // Dex-to-dex compilation required for correctness. 74 kOptimize // Perform required transformation and peep-hole optimizations. 75}; 76 77// Thread-local storage compiler worker threads 78class CompilerTls { 79 public: 80 CompilerTls() : llvm_info_(NULL) {} 81 ~CompilerTls() {} 82 83 void* GetLLVMInfo() { return llvm_info_; } 84 85 void SetLLVMInfo(void* llvm_info) { llvm_info_ = llvm_info; } 86 87 private: 88 void* llvm_info_; 89}; 90 91class CompilerDriver { 92 public: 93 typedef std::set<std::string> DescriptorSet; 94 95 // Create a compiler targeting the requested "instruction_set". 96 // "image" should be true if image specific optimizations should be 97 // enabled. "image_classes" lets the compiler know what classes it 98 // can assume will be in the image, with NULL implying all available 99 // classes. 100 explicit CompilerDriver(const CompilerOptions* compiler_options, 101 VerificationResults* verification_results, 102 DexFileToMethodInlinerMap* method_inliner_map, 103 CompilerBackend::Kind compiler_backend_kind, 104 InstructionSet instruction_set, 105 InstructionSetFeatures instruction_set_features, 106 bool image, DescriptorSet* image_classes, 107 size_t thread_count, bool dump_stats, bool dump_passes, 108 CumulativeLogger* timer, 109 std::string profile_file = ""); 110 111 ~CompilerDriver(); 112 113 void CompileAll(jobject class_loader, const std::vector<const DexFile*>& dex_files, 114 TimingLogger* timings) 115 LOCKS_EXCLUDED(Locks::mutator_lock_); 116 117 // Compile a single Method. 118 void CompileOne(mirror::ArtMethod* method, TimingLogger* timings) 119 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 120 121 VerificationResults* GetVerificationResults() const { 122 return verification_results_; 123 } 124 125 DexFileToMethodInlinerMap* GetMethodInlinerMap() const { 126 return method_inliner_map_; 127 } 128 129 InstructionSet GetInstructionSet() const { 130 return instruction_set_; 131 } 132 133 InstructionSetFeatures GetInstructionSetFeatures() const { 134 return instruction_set_features_; 135 } 136 137 const CompilerOptions& GetCompilerOptions() const { 138 return *compiler_options_; 139 } 140 141 CompilerBackend* GetCompilerBackend() const { 142 return compiler_backend_.get(); 143 } 144 145 bool ProfilePresent() const { 146 return profile_ok_; 147 } 148 149 // Are we compiling and creating an image file? 150 bool IsImage() const { 151 return image_; 152 } 153 154 DescriptorSet* GetImageClasses() const { 155 return image_classes_.get(); 156 } 157 158 CompilerTls* GetTls(); 159 160 // Generate the trampolines that are invoked by unresolved direct methods. 161 const std::vector<uint8_t>* CreateInterpreterToInterpreterBridge() const 162 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 163 const std::vector<uint8_t>* CreateInterpreterToCompiledCodeBridge() const 164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 165 const std::vector<uint8_t>* CreateJniDlsymLookup() const 166 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 167 const std::vector<uint8_t>* CreatePortableImtConflictTrampoline() const 168 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 169 const std::vector<uint8_t>* CreatePortableResolutionTrampoline() const 170 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 171 const std::vector<uint8_t>* CreatePortableToInterpreterBridge() const 172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 173 const std::vector<uint8_t>* CreateQuickGenericJniTrampoline() const 174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 175 const std::vector<uint8_t>* CreateQuickImtConflictTrampoline() const 176 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 177 const std::vector<uint8_t>* CreateQuickResolutionTrampoline() const 178 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 179 const std::vector<uint8_t>* CreateQuickToInterpreterBridge() const 180 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 181 182 CompiledClass* GetCompiledClass(ClassReference ref) const 183 LOCKS_EXCLUDED(compiled_classes_lock_); 184 185 CompiledMethod* GetCompiledMethod(MethodReference ref) const 186 LOCKS_EXCLUDED(compiled_methods_lock_); 187 188 void AddRequiresConstructorBarrier(Thread* self, const DexFile* dex_file, 189 uint16_t class_def_index); 190 bool RequiresConstructorBarrier(Thread* self, const DexFile* dex_file, uint16_t class_def_index); 191 192 // Callbacks from compiler to see what runtime checks must be generated. 193 194 bool CanAssumeTypeIsPresentInDexCache(const DexFile& dex_file, uint32_t type_idx); 195 196 bool CanAssumeStringIsPresentInDexCache(const DexFile& dex_file, uint32_t string_idx) 197 LOCKS_EXCLUDED(Locks::mutator_lock_); 198 199 // Are runtime access checks necessary in the compiled code? 200 bool CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file, 201 uint32_t type_idx, bool* type_known_final = NULL, 202 bool* type_known_abstract = NULL, 203 bool* equals_referrers_class = NULL) 204 LOCKS_EXCLUDED(Locks::mutator_lock_); 205 206 // Are runtime access and instantiable checks necessary in the code? 207 bool CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file, 208 uint32_t type_idx) 209 LOCKS_EXCLUDED(Locks::mutator_lock_); 210 211 bool CanEmbedTypeInCode(const DexFile& dex_file, uint32_t type_idx, 212 bool* is_type_initialized, bool* use_direct_type_ptr, 213 uintptr_t* direct_type_ptr); 214 215 // Get the DexCache for the 216 mirror::DexCache* GetDexCache(const DexCompilationUnit* mUnit) 217 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 218 219 mirror::ClassLoader* GetClassLoader(ScopedObjectAccess& soa, const DexCompilationUnit* mUnit) 220 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 221 222 // Resolve compiling method's class. Returns nullptr on failure. 223 mirror::Class* ResolveCompilingMethodsClass( 224 ScopedObjectAccess& soa, const SirtRef<mirror::DexCache>& dex_cache, 225 const SirtRef<mirror::ClassLoader>& class_loader, const DexCompilationUnit* mUnit) 226 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 227 228 // Resolve a field. Returns nullptr on failure, including incompatible class change. 229 // NOTE: Unlike ClassLinker's ResolveField(), this method enforces is_static. 230 mirror::ArtField* ResolveField( 231 ScopedObjectAccess& soa, const SirtRef<mirror::DexCache>& dex_cache, 232 const SirtRef<mirror::ClassLoader>& class_loader, const DexCompilationUnit* mUnit, 233 uint32_t field_idx, bool is_static) 234 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 235 236 // Get declaration location of a resolved field. 237 void GetResolvedFieldDexFileLocation( 238 mirror::ArtField* resolved_field, const DexFile** declaring_dex_file, 239 uint16_t* declaring_class_idx, uint16_t* declaring_field_idx) 240 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 241 242 bool IsFieldVolatile(mirror::ArtField* field) SHARED_LOCKS_REQUIRED(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 mirror::ArtField* resolved_field, uint16_t field_idx, MemberOffset* field_offset) 248 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 249 250 // Can we fast-path an SGET/SPUT access to a static field? If yes, compute the field offset, 251 // the type index of the declaring class in the referrer's dex file and whether the declaring 252 // class is the referrer's class or at least can be assumed to be initialized. 253 std::pair<bool, bool> IsFastStaticField( 254 mirror::DexCache* dex_cache, mirror::Class* referrer_class, 255 mirror::ArtField* resolved_field, uint16_t field_idx, MemberOffset* field_offset, 256 uint32_t* storage_index, bool* is_referrers_class, bool* is_initialized) 257 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 258 259 void ProcessedInstanceField(bool resolved); 260 void ProcessedStaticField(bool resolved, bool local); 261 262 // Can we fast path instance field access in a verified accessor? 263 // If yes, computes field's offset and volatility and whether the method is static or not. 264 static bool ComputeSpecialAccessorInfo(uint32_t field_idx, bool is_put, 265 verifier::MethodVerifier* verifier, 266 InlineIGetIPutData* result) 267 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 268 269 // Can we fast path instance field access? Computes field's offset and volatility. 270 bool ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put, 271 MemberOffset* field_offset, bool* is_volatile) 272 LOCKS_EXCLUDED(Locks::mutator_lock_); 273 274 // Can we fastpath static field access? Computes field's offset, volatility and whether the 275 // field is within the referrer (which can avoid checking class initialization). 276 bool ComputeStaticFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put, 277 MemberOffset* field_offset, uint32_t* storage_index, 278 bool* is_referrers_class, bool* is_volatile, bool* is_initialized) 279 LOCKS_EXCLUDED(Locks::mutator_lock_); 280 281 // Can we fastpath a interface, super class or virtual method call? Computes method's vtable 282 // index. 283 bool ComputeInvokeInfo(const DexCompilationUnit* mUnit, const uint32_t dex_pc, 284 bool update_stats, bool enable_devirtualization, 285 InvokeType* type, MethodReference* target_method, int* vtable_idx, 286 uintptr_t* direct_code, uintptr_t* direct_method) 287 LOCKS_EXCLUDED(Locks::mutator_lock_); 288 289 const VerifiedMethod* GetVerifiedMethod(const DexFile* dex_file, uint32_t method_idx) const; 290 bool IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc); 291 292 // Record patch information for later fix up. 293 void AddCodePatch(const DexFile* dex_file, 294 uint16_t referrer_class_def_idx, 295 uint32_t referrer_method_idx, 296 InvokeType referrer_invoke_type, 297 uint32_t target_method_idx, 298 InvokeType target_invoke_type, 299 size_t literal_offset) 300 LOCKS_EXCLUDED(compiled_methods_lock_); 301 void AddRelativeCodePatch(const DexFile* dex_file, 302 uint16_t referrer_class_def_idx, 303 uint32_t referrer_method_idx, 304 InvokeType referrer_invoke_type, 305 uint32_t target_method_idx, 306 InvokeType target_invoke_type, 307 size_t literal_offset, 308 int32_t pc_relative_offset) 309 LOCKS_EXCLUDED(compiled_methods_lock_); 310 void AddMethodPatch(const DexFile* dex_file, 311 uint16_t referrer_class_def_idx, 312 uint32_t referrer_method_idx, 313 InvokeType referrer_invoke_type, 314 uint32_t target_method_idx, 315 InvokeType target_invoke_type, 316 size_t literal_offset) 317 LOCKS_EXCLUDED(compiled_methods_lock_); 318 void AddClassPatch(const DexFile* dex_file, 319 uint16_t referrer_class_def_idx, 320 uint32_t referrer_method_idx, 321 uint32_t target_method_idx, 322 size_t literal_offset) 323 LOCKS_EXCLUDED(compiled_methods_lock_); 324 325 bool GetSupportBootImageFixup() const { 326 return support_boot_image_fixup_; 327 } 328 329 void SetSupportBootImageFixup(bool support_boot_image_fixup) { 330 support_boot_image_fixup_ = support_boot_image_fixup; 331 } 332 333 ArenaPool* GetArenaPool() { 334 return &arena_pool_; 335 } 336 337 bool WriteElf(const std::string& android_root, 338 bool is_host, 339 const std::vector<const DexFile*>& dex_files, 340 OatWriter* oat_writer, 341 File* file); 342 343 // TODO: move to a common home for llvm helpers once quick/portable are merged. 344 static void InstructionSetToLLVMTarget(InstructionSet instruction_set, 345 std::string* target_triple, 346 std::string* target_cpu, 347 std::string* target_attr); 348 349 void SetCompilerContext(void* compiler_context) { 350 compiler_context_ = compiler_context; 351 } 352 353 void* GetCompilerContext() const { 354 return compiler_context_; 355 } 356 357 size_t GetThreadCount() const { 358 return thread_count_; 359 } 360 361 class CallPatchInformation; 362 class TypePatchInformation; 363 364 bool GetDumpPasses() const { 365 return dump_passes_; 366 } 367 368 CumulativeLogger* GetTimingsLogger() const { 369 return timings_logger_; 370 } 371 372 class PatchInformation { 373 public: 374 const DexFile& GetDexFile() const { 375 return *dex_file_; 376 } 377 uint16_t GetReferrerClassDefIdx() const { 378 return referrer_class_def_idx_; 379 } 380 uint32_t GetReferrerMethodIdx() const { 381 return referrer_method_idx_; 382 } 383 size_t GetLiteralOffset() const { 384 return literal_offset_; 385 } 386 387 virtual bool IsCall() const { 388 return false; 389 } 390 virtual bool IsType() const { 391 return false; 392 } 393 virtual const CallPatchInformation* AsCall() const { 394 LOG(FATAL) << "Unreachable"; 395 return nullptr; 396 } 397 virtual const TypePatchInformation* AsType() const { 398 LOG(FATAL) << "Unreachable"; 399 return nullptr; 400 } 401 402 protected: 403 PatchInformation(const DexFile* dex_file, 404 uint16_t referrer_class_def_idx, 405 uint32_t referrer_method_idx, 406 size_t literal_offset) 407 : dex_file_(dex_file), 408 referrer_class_def_idx_(referrer_class_def_idx), 409 referrer_method_idx_(referrer_method_idx), 410 literal_offset_(literal_offset) { 411 CHECK(dex_file_ != NULL); 412 } 413 virtual ~PatchInformation() {} 414 415 const DexFile* const dex_file_; 416 const uint16_t referrer_class_def_idx_; 417 const uint32_t referrer_method_idx_; 418 const size_t literal_offset_; 419 420 friend class CompilerDriver; 421 }; 422 423 class CallPatchInformation : public PatchInformation { 424 public: 425 InvokeType GetReferrerInvokeType() const { 426 return referrer_invoke_type_; 427 } 428 uint32_t GetTargetMethodIdx() const { 429 return target_method_idx_; 430 } 431 InvokeType GetTargetInvokeType() const { 432 return target_invoke_type_; 433 } 434 435 const CallPatchInformation* AsCall() const { 436 return this; 437 } 438 bool IsCall() const { 439 return true; 440 } 441 virtual bool IsRelative() const { 442 return false; 443 } 444 virtual int RelativeOffset() const { 445 return 0; 446 } 447 448 protected: 449 CallPatchInformation(const DexFile* dex_file, 450 uint16_t referrer_class_def_idx, 451 uint32_t referrer_method_idx, 452 InvokeType referrer_invoke_type, 453 uint32_t target_method_idx, 454 InvokeType target_invoke_type, 455 size_t literal_offset) 456 : PatchInformation(dex_file, referrer_class_def_idx, 457 referrer_method_idx, literal_offset), 458 referrer_invoke_type_(referrer_invoke_type), 459 target_method_idx_(target_method_idx), 460 target_invoke_type_(target_invoke_type) { 461 } 462 463 private: 464 const InvokeType referrer_invoke_type_; 465 const uint32_t target_method_idx_; 466 const InvokeType target_invoke_type_; 467 468 friend class CompilerDriver; 469 DISALLOW_COPY_AND_ASSIGN(CallPatchInformation); 470 }; 471 472 class RelativeCallPatchInformation : public CallPatchInformation { 473 public: 474 bool IsRelative() const { 475 return true; 476 } 477 int RelativeOffset() const { 478 return offset_; 479 } 480 481 private: 482 RelativeCallPatchInformation(const DexFile* dex_file, 483 uint16_t referrer_class_def_idx, 484 uint32_t referrer_method_idx, 485 InvokeType referrer_invoke_type, 486 uint32_t target_method_idx, 487 InvokeType target_invoke_type, 488 size_t literal_offset, 489 int32_t pc_relative_offset) 490 : CallPatchInformation(dex_file, referrer_class_def_idx, 491 referrer_method_idx, referrer_invoke_type, 492 target_method_idx, target_invoke_type, literal_offset), 493 offset_(pc_relative_offset) { 494 } 495 496 const int offset_; 497 498 friend class CompilerDriver; 499 DISALLOW_COPY_AND_ASSIGN(RelativeCallPatchInformation); 500 }; 501 502 class TypePatchInformation : public PatchInformation { 503 public: 504 uint32_t GetTargetTypeIdx() const { 505 return target_type_idx_; 506 } 507 508 bool IsType() const { 509 return true; 510 } 511 const TypePatchInformation* AsType() const { 512 return this; 513 } 514 515 private: 516 TypePatchInformation(const DexFile* dex_file, 517 uint16_t referrer_class_def_idx, 518 uint32_t referrer_method_idx, 519 uint32_t target_type_idx, 520 size_t literal_offset) 521 : PatchInformation(dex_file, referrer_class_def_idx, 522 referrer_method_idx, literal_offset), 523 target_type_idx_(target_type_idx) { 524 } 525 526 const uint32_t target_type_idx_; 527 528 friend class CompilerDriver; 529 DISALLOW_COPY_AND_ASSIGN(TypePatchInformation); 530 }; 531 532 const std::vector<const CallPatchInformation*>& GetCodeToPatch() const { 533 return code_to_patch_; 534 } 535 const std::vector<const CallPatchInformation*>& GetMethodsToPatch() const { 536 return methods_to_patch_; 537 } 538 const std::vector<const TypePatchInformation*>& GetClassesToPatch() const { 539 return classes_to_patch_; 540 } 541 542 // Checks if class specified by type_idx is one of the image_classes_ 543 bool IsImageClass(const char* descriptor) const; 544 545 void RecordClassStatus(ClassReference ref, mirror::Class::Status status) 546 LOCKS_EXCLUDED(compiled_classes_lock_); 547 548 std::vector<uint8_t>* DeduplicateCode(const std::vector<uint8_t>& code); 549 std::vector<uint8_t>* DeduplicateMappingTable(const std::vector<uint8_t>& code); 550 std::vector<uint8_t>* DeduplicateVMapTable(const std::vector<uint8_t>& code); 551 std::vector<uint8_t>* DeduplicateGCMap(const std::vector<uint8_t>& code); 552 std::vector<uint8_t>* DeduplicateCFIInfo(const std::vector<uint8_t>* cfi_info); 553 554 /* 555 * @brief return the pointer to the Call Frame Information. 556 * @return pointer to call frame information for this compilation. 557 */ 558 std::vector<uint8_t>* GetCallFrameInformation() const { 559 return cfi_info_.get(); 560 } 561 562 // Profile data. This is generated from previous runs of the program and stored 563 // in a file. It is used to determine whether to compile a particular method or not. 564 class ProfileData { 565 public: 566 ProfileData() : count_(0), method_size_(0), percent_(0) {} 567 ProfileData(const std::string& method_name, uint32_t count, uint32_t method_size, double percent) : 568 method_name_(method_name), count_(count), method_size_(method_size), percent_(percent) { 569 // TODO: currently method_size_ and count_ are unused. 570 UNUSED(method_size_); 571 UNUSED(count_); 572 } 573 574 bool IsAbove(double v) const { return percent_ >= v; } 575 double GetPercent() const { return percent_; } 576 577 private: 578 std::string method_name_; // Method name. 579 uint32_t count_; // Number number of times it has been called. 580 uint32_t method_size_; // Size of the method on dex instructions. 581 double percent_; // Percentage of time spent in this method. 582 }; 583 584 // Profile data is stored in a map, indexed by the full method name. 585 typedef std::map<const std::string, ProfileData> ProfileMap; 586 ProfileMap profile_map_; 587 bool profile_ok_; 588 589 // Read the profile data from the given file. Calculates the percentage for each method. 590 // Returns false if there was no profile file or it was malformed. 591 bool ReadProfile(const std::string& filename); 592 593 // Should the compiler run on this method given profile information? 594 bool SkipCompilation(const std::string& method_name); 595 596 private: 597 // Compute constant code and method pointers when possible 598 void GetCodeAndMethodForDirectCall(InvokeType* type, InvokeType sharp_type, 599 bool no_guarantee_of_dex_cache_entry, 600 mirror::Class* referrer_class, 601 mirror::ArtMethod* method, 602 bool update_stats, 603 MethodReference* target_method, 604 uintptr_t* direct_code, uintptr_t* direct_method) 605 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 606 607 void PreCompile(jobject class_loader, const std::vector<const DexFile*>& dex_files, 608 ThreadPool* thread_pool, TimingLogger* timings) 609 LOCKS_EXCLUDED(Locks::mutator_lock_); 610 611 void LoadImageClasses(TimingLogger* timings); 612 613 // Attempt to resolve all type, methods, fields, and strings 614 // referenced from code in the dex file following PathClassLoader 615 // ordering semantics. 616 void Resolve(jobject class_loader, const std::vector<const DexFile*>& dex_files, 617 ThreadPool* thread_pool, TimingLogger* timings) 618 LOCKS_EXCLUDED(Locks::mutator_lock_); 619 void ResolveDexFile(jobject class_loader, const DexFile& dex_file, 620 ThreadPool* thread_pool, TimingLogger* timings) 621 LOCKS_EXCLUDED(Locks::mutator_lock_); 622 623 void Verify(jobject class_loader, const std::vector<const DexFile*>& dex_files, 624 ThreadPool* thread_pool, TimingLogger* timings); 625 void VerifyDexFile(jobject class_loader, const DexFile& dex_file, 626 ThreadPool* thread_pool, TimingLogger* timings) 627 LOCKS_EXCLUDED(Locks::mutator_lock_); 628 629 void InitializeClasses(jobject class_loader, const std::vector<const DexFile*>& dex_files, 630 ThreadPool* thread_pool, TimingLogger* timings) 631 LOCKS_EXCLUDED(Locks::mutator_lock_); 632 void InitializeClasses(jobject class_loader, const DexFile& dex_file, 633 ThreadPool* thread_pool, TimingLogger* timings) 634 LOCKS_EXCLUDED(Locks::mutator_lock_, compiled_classes_lock_); 635 636 void UpdateImageClasses(TimingLogger* timings) LOCKS_EXCLUDED(Locks::mutator_lock_); 637 static void FindClinitImageClassesCallback(mirror::Object* object, void* arg) 638 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 639 640 void Compile(jobject class_loader, const std::vector<const DexFile*>& dex_files, 641 ThreadPool* thread_pool, TimingLogger* timings); 642 void CompileDexFile(jobject class_loader, const DexFile& dex_file, 643 ThreadPool* thread_pool, TimingLogger* timings) 644 LOCKS_EXCLUDED(Locks::mutator_lock_); 645 void CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags, 646 InvokeType invoke_type, uint16_t class_def_idx, uint32_t method_idx, 647 jobject class_loader, const DexFile& dex_file, 648 DexToDexCompilationLevel dex_to_dex_compilation_level) 649 LOCKS_EXCLUDED(compiled_methods_lock_); 650 651 static void CompileClass(const ParallelCompilationManager* context, size_t class_def_index) 652 LOCKS_EXCLUDED(Locks::mutator_lock_); 653 654 std::vector<const CallPatchInformation*> code_to_patch_; 655 std::vector<const CallPatchInformation*> methods_to_patch_; 656 std::vector<const TypePatchInformation*> classes_to_patch_; 657 658 const CompilerOptions* const compiler_options_; 659 VerificationResults* const verification_results_; 660 DexFileToMethodInlinerMap* const method_inliner_map_; 661 662 UniquePtr<CompilerBackend> compiler_backend_; 663 664 const InstructionSet instruction_set_; 665 const InstructionSetFeatures instruction_set_features_; 666 667 // All class references that require 668 mutable ReaderWriterMutex freezing_constructor_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 669 std::set<ClassReference> freezing_constructor_classes_ GUARDED_BY(freezing_constructor_lock_); 670 671 typedef SafeMap<const ClassReference, CompiledClass*> ClassTable; 672 // All class references that this compiler has compiled. 673 mutable Mutex compiled_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 674 ClassTable compiled_classes_ GUARDED_BY(compiled_classes_lock_); 675 676 typedef SafeMap<const MethodReference, CompiledMethod*, MethodReferenceComparator> MethodTable; 677 // All method references that this compiler has compiled. 678 mutable Mutex compiled_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 679 MethodTable compiled_methods_ GUARDED_BY(compiled_methods_lock_); 680 681 const bool image_; 682 683 // If image_ is true, specifies the classes that will be included in 684 // the image. Note if image_classes_ is NULL, all classes are 685 // included in the image. 686 UniquePtr<DescriptorSet> image_classes_; 687 688 size_t thread_count_; 689 uint64_t start_ns_; 690 691 UniquePtr<AOTCompilationStats> stats_; 692 693 bool dump_stats_; 694 const bool dump_passes_; 695 696 CumulativeLogger* const timings_logger_; 697 698 typedef void (*CompilerCallbackFn)(CompilerDriver& driver); 699 typedef MutexLock* (*CompilerMutexLockFn)(CompilerDriver& driver); 700 701 void* compiler_library_; 702 703 typedef void (*DexToDexCompilerFn)(CompilerDriver& driver, 704 const DexFile::CodeItem* code_item, 705 uint32_t access_flags, InvokeType invoke_type, 706 uint32_t class_dex_idx, uint32_t method_idx, 707 jobject class_loader, const DexFile& dex_file, 708 DexToDexCompilationLevel dex_to_dex_compilation_level); 709 DexToDexCompilerFn dex_to_dex_compiler_; 710 711 void* compiler_context_; 712 713 pthread_key_t tls_key_; 714 715 // Arena pool used by the compiler. 716 ArenaPool arena_pool_; 717 718 typedef void (*CompilerEnableAutoElfLoadingFn)(CompilerDriver& driver); 719 CompilerEnableAutoElfLoadingFn compiler_enable_auto_elf_loading_; 720 721 typedef const void* (*CompilerGetMethodCodeAddrFn) 722 (const CompilerDriver& driver, const CompiledMethod* cm, const mirror::ArtMethod* method); 723 CompilerGetMethodCodeAddrFn compiler_get_method_code_addr_; 724 725 bool support_boot_image_fixup_; 726 727 // Call Frame Information, which might be generated to help stack tracebacks. 728 UniquePtr<std::vector<uint8_t> > cfi_info_; 729 730 // DeDuplication data structures, these own the corresponding byte arrays. 731 class DedupeHashFunc { 732 public: 733 size_t operator()(const std::vector<uint8_t>& array) const { 734 // For small arrays compute a hash using every byte. 735 static const size_t kSmallArrayThreshold = 16; 736 size_t hash = 0x811c9dc5; 737 if (array.size() <= kSmallArrayThreshold) { 738 for (uint8_t b : array) { 739 hash = (hash * 16777619) ^ b; 740 } 741 } else { 742 // For larger arrays use the 2 bytes at 6 bytes (the location of a push registers 743 // instruction field for quick generated code on ARM) and then select a number of other 744 // values at random. 745 static const size_t kRandomHashCount = 16; 746 for (size_t i = 0; i < 2; ++i) { 747 uint8_t b = array[i + 6]; 748 hash = (hash * 16777619) ^ b; 749 } 750 for (size_t i = 2; i < kRandomHashCount; ++i) { 751 size_t r = i * 1103515245 + 12345; 752 uint8_t b = array[r % array.size()]; 753 hash = (hash * 16777619) ^ b; 754 } 755 } 756 hash += hash << 13; 757 hash ^= hash >> 7; 758 hash += hash << 3; 759 hash ^= hash >> 17; 760 hash += hash << 5; 761 return hash; 762 } 763 }; 764 DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc, 4> dedupe_code_; 765 DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc, 4> dedupe_mapping_table_; 766 DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc, 4> dedupe_vmap_table_; 767 DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc, 4> dedupe_gc_map_; 768 DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc, 4> dedupe_cfi_info_; 769 770 DISALLOW_COPY_AND_ASSIGN(CompilerDriver); 771}; 772 773} // namespace art 774 775#endif // ART_COMPILER_DRIVER_COMPILER_DRIVER_H_ 776