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