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