compiler_driver.h revision ab972ef472001fa113d54486d7592979e33480b3
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 "arch/instruction_set.h" 25#include "base/mutex.h" 26#include "base/timing_logger.h" 27#include "class_reference.h" 28#include "compiled_method.h" 29#include "compiler.h" 30#include "dex_file.h" 31#include "driver/compiler_options.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#include "dex/verified_method.h" 43 44namespace art { 45 46namespace verifier { 47class MethodVerifier; 48} // namespace verifier 49 50class CompiledClass; 51class CompilerOptions; 52class DexCompilationUnit; 53class DexFileToMethodInlinerMap; 54struct InlineIGetIPutData; 55class InstructionSetFeatures; 56class OatWriter; 57class ParallelCompilationManager; 58class ScopedObjectAccess; 59template<class T> class Handle; 60class TimingLogger; 61class VerificationResults; 62class VerifiedMethod; 63 64enum EntryPointCallingConvention { 65 // ABI of invocations to a method's interpreter entry point. 66 kInterpreterAbi, 67 // ABI of calls to a method's native code, only used for native methods. 68 kJniAbi, 69 // ABI of calls to a method's portable code entry point. 70 kPortableAbi, 71 // ABI of calls to a method's quick code entry point. 72 kQuickAbi 73}; 74 75enum DexToDexCompilationLevel { 76 kDontDexToDexCompile, // Only meaning wrt image time interpretation. 77 kRequired, // Dex-to-dex compilation required for correctness. 78 kOptimize // Perform required transformation and peep-hole optimizations. 79}; 80std::ostream& operator<<(std::ostream& os, const DexToDexCompilationLevel& rhs); 81 82class CompilerDriver { 83 public: 84 // Create a compiler targeting the requested "instruction_set". 85 // "image" should be true if image specific optimizations should be 86 // enabled. "image_classes" lets the compiler know what classes it 87 // can assume will be in the image, with nullptr implying all available 88 // classes. 89 explicit CompilerDriver(const CompilerOptions* compiler_options, 90 VerificationResults* verification_results, 91 DexFileToMethodInlinerMap* method_inliner_map, 92 Compiler::Kind compiler_kind, 93 InstructionSet instruction_set, 94 const InstructionSetFeatures* instruction_set_features, 95 bool image, std::set<std::string>* image_classes, 96 std::set<std::string>* compiled_classes, 97 size_t thread_count, bool dump_stats, bool dump_passes, 98 CumulativeLogger* timer, const std::string& profile_file); 99 100 ~CompilerDriver(); 101 102 void CompileAll(jobject class_loader, const std::vector<const DexFile*>& dex_files, 103 TimingLogger* timings) 104 LOCKS_EXCLUDED(Locks::mutator_lock_); 105 106 // Compile a single Method. 107 void CompileOne(mirror::ArtMethod* method, TimingLogger* timings) 108 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 109 110 VerificationResults* GetVerificationResults() const { 111 return verification_results_; 112 } 113 114 DexFileToMethodInlinerMap* GetMethodInlinerMap() const { 115 return method_inliner_map_; 116 } 117 118 InstructionSet GetInstructionSet() const { 119 return instruction_set_; 120 } 121 122 const InstructionSetFeatures* GetInstructionSetFeatures() const { 123 return instruction_set_features_; 124 } 125 126 const CompilerOptions& GetCompilerOptions() const { 127 return *compiler_options_; 128 } 129 130 Compiler* GetCompiler() const { 131 return compiler_.get(); 132 } 133 134 bool ProfilePresent() const { 135 return profile_present_; 136 } 137 138 // Are we compiling and creating an image file? 139 bool IsImage() const { 140 return image_; 141 } 142 143 const std::set<std::string>* GetImageClasses() const { 144 return image_classes_.get(); 145 } 146 147 CompilerTls* GetTls(); 148 149 // Generate the trampolines that are invoked by unresolved direct methods. 150 const std::vector<uint8_t>* CreateInterpreterToInterpreterBridge() const 151 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 152 const std::vector<uint8_t>* CreateInterpreterToCompiledCodeBridge() const 153 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 154 const std::vector<uint8_t>* CreateJniDlsymLookup() const 155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 156 const std::vector<uint8_t>* CreatePortableImtConflictTrampoline() const 157 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 158 const std::vector<uint8_t>* CreatePortableResolutionTrampoline() const 159 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 160 const std::vector<uint8_t>* CreatePortableToInterpreterBridge() const 161 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 162 const std::vector<uint8_t>* CreateQuickGenericJniTrampoline() const 163 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 164 const std::vector<uint8_t>* CreateQuickImtConflictTrampoline() const 165 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 166 const std::vector<uint8_t>* CreateQuickResolutionTrampoline() const 167 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 168 const std::vector<uint8_t>* CreateQuickToInterpreterBridge() const 169 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 170 171 CompiledClass* GetCompiledClass(ClassReference ref) const 172 LOCKS_EXCLUDED(compiled_classes_lock_); 173 174 CompiledMethod* GetCompiledMethod(MethodReference ref) const 175 LOCKS_EXCLUDED(compiled_methods_lock_); 176 size_t GetNonRelativeLinkerPatchCount() const 177 LOCKS_EXCLUDED(compiled_methods_lock_); 178 179 void AddRequiresConstructorBarrier(Thread* self, const DexFile* dex_file, 180 uint16_t class_def_index); 181 bool RequiresConstructorBarrier(Thread* self, const DexFile* dex_file, uint16_t class_def_index); 182 183 // Callbacks from compiler to see what runtime checks must be generated. 184 185 bool CanAssumeTypeIsPresentInDexCache(const DexFile& dex_file, uint32_t type_idx); 186 187 bool CanAssumeStringIsPresentInDexCache(const DexFile& dex_file, uint32_t string_idx) 188 LOCKS_EXCLUDED(Locks::mutator_lock_); 189 190 // Are runtime access checks necessary in the compiled code? 191 bool CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file, 192 uint32_t type_idx, bool* type_known_final = nullptr, 193 bool* type_known_abstract = nullptr, 194 bool* equals_referrers_class = nullptr) 195 LOCKS_EXCLUDED(Locks::mutator_lock_); 196 197 // Are runtime access and instantiable checks necessary in the code? 198 bool CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file, 199 uint32_t type_idx) 200 LOCKS_EXCLUDED(Locks::mutator_lock_); 201 202 bool CanEmbedTypeInCode(const DexFile& dex_file, uint32_t type_idx, 203 bool* is_type_initialized, bool* use_direct_type_ptr, 204 uintptr_t* direct_type_ptr, bool* out_is_finalizable); 205 206 // Query methods for the java.lang.ref.Reference class. 207 bool CanEmbedReferenceTypeInCode(ClassReference* ref, 208 bool* use_direct_type_ptr, uintptr_t* direct_type_ptr); 209 uint32_t GetReferenceSlowFlagOffset() const; 210 uint32_t GetReferenceDisableFlagOffset() const; 211 212 // Get the DexCache for the 213 mirror::DexCache* GetDexCache(const DexCompilationUnit* mUnit) 214 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 215 216 mirror::ClassLoader* GetClassLoader(ScopedObjectAccess& soa, const DexCompilationUnit* mUnit) 217 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 218 219 // Resolve compiling method's class. Returns nullptr on failure. 220 mirror::Class* ResolveCompilingMethodsClass( 221 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, 222 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit) 223 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 224 225 // Resolve a field. Returns nullptr on failure, including incompatible class change. 226 // NOTE: Unlike ClassLinker's ResolveField(), this method enforces is_static. 227 mirror::ArtField* ResolveField( 228 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, 229 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit, 230 uint32_t field_idx, bool is_static) 231 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 232 233 // Get declaration location of a resolved field. 234 void GetResolvedFieldDexFileLocation( 235 mirror::ArtField* resolved_field, const DexFile** declaring_dex_file, 236 uint16_t* declaring_class_idx, uint16_t* declaring_field_idx) 237 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 238 239 bool IsFieldVolatile(mirror::ArtField* field) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 240 MemberOffset GetFieldOffset(mirror::ArtField* field) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 241 242 // Can we fast-path an IGET/IPUT access to an instance field? If yes, compute the field offset. 243 std::pair<bool, bool> IsFastInstanceField( 244 mirror::DexCache* dex_cache, mirror::Class* referrer_class, 245 mirror::ArtField* resolved_field, uint16_t field_idx) 246 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 247 248 // Can we fast-path an SGET/SPUT access to a static field? If yes, compute the type index 249 // of the declaring class in the referrer's dex file. 250 std::pair<bool, bool> IsFastStaticField( 251 mirror::DexCache* dex_cache, mirror::Class* referrer_class, 252 mirror::ArtField* resolved_field, uint16_t field_idx, uint32_t* storage_index) 253 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 254 255 // Is static field's in referrer's class? 256 bool IsStaticFieldInReferrerClass(mirror::Class* referrer_class, mirror::ArtField* resolved_field) 257 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 258 259 // Is static field's class initialized? 260 bool IsStaticFieldsClassInitialized(mirror::Class* referrer_class, 261 mirror::ArtField* resolved_field) 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 the index in the vtable of the method. 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 // Is method's class initialized for an invoke? 293 // For static invokes to determine whether we need to consider potential call to <clinit>(). 294 // For non-static invokes, assuming a non-null reference, the class is always initialized. 295 bool IsMethodsClassInitialized(mirror::Class* referrer_class, mirror::ArtMethod* resolved_method) 296 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 297 298 void ProcessedInstanceField(bool resolved); 299 void ProcessedStaticField(bool resolved, bool local); 300 void ProcessedInvoke(InvokeType invoke_type, int flags); 301 302 // Can we fast path instance field access? Computes field's offset and volatility. 303 bool ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put, 304 MemberOffset* field_offset, bool* is_volatile) 305 LOCKS_EXCLUDED(Locks::mutator_lock_); 306 307 mirror::ArtField* ComputeInstanceFieldInfo(uint32_t field_idx, 308 const DexCompilationUnit* mUnit, 309 bool is_put, 310 const ScopedObjectAccess& soa) 311 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 312 313 314 // Can we fastpath static field access? Computes field's offset, volatility and whether the 315 // field is within the referrer (which can avoid checking class initialization). 316 bool ComputeStaticFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put, 317 MemberOffset* field_offset, uint32_t* storage_index, 318 bool* is_referrers_class, bool* is_volatile, bool* is_initialized, 319 Primitive::Type* type) 320 LOCKS_EXCLUDED(Locks::mutator_lock_); 321 322 // Can we fastpath a interface, super class or virtual method call? Computes method's vtable 323 // index. 324 bool ComputeInvokeInfo(const DexCompilationUnit* mUnit, const uint32_t dex_pc, 325 bool update_stats, bool enable_devirtualization, 326 InvokeType* type, MethodReference* target_method, int* vtable_idx, 327 uintptr_t* direct_code, uintptr_t* direct_method) 328 LOCKS_EXCLUDED(Locks::mutator_lock_); 329 330 const VerifiedMethod* GetVerifiedMethod(const DexFile* dex_file, uint32_t method_idx) const; 331 bool IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc); 332 333 bool GetSupportBootImageFixup() const { 334 return support_boot_image_fixup_; 335 } 336 337 void SetSupportBootImageFixup(bool support_boot_image_fixup) { 338 support_boot_image_fixup_ = support_boot_image_fixup; 339 } 340 341 ArenaPool* GetArenaPool() { 342 return &arena_pool_; 343 } 344 const ArenaPool* GetArenaPool() const { 345 return &arena_pool_; 346 } 347 348 bool WriteElf(const std::string& android_root, 349 bool is_host, 350 const std::vector<const DexFile*>& dex_files, 351 OatWriter* oat_writer, 352 File* file); 353 354 // TODO: move to a common home for llvm helpers once quick/portable are merged. 355 static void InstructionSetToLLVMTarget(InstructionSet instruction_set, 356 std::string* target_triple, 357 std::string* target_cpu, 358 std::string* target_attr); 359 360 void SetCompilerContext(void* compiler_context) { 361 compiler_context_ = compiler_context; 362 } 363 364 void* GetCompilerContext() const { 365 return compiler_context_; 366 } 367 368 size_t GetThreadCount() const { 369 return thread_count_; 370 } 371 372 bool GetDumpPasses() const { 373 return dump_passes_; 374 } 375 376 CumulativeLogger* GetTimingsLogger() const { 377 return timings_logger_; 378 } 379 380 // Checks if class specified by type_idx is one of the image_classes_ 381 bool IsImageClass(const char* descriptor) const; 382 383 // Checks if the provided class should be compiled, i.e., is in classes_to_compile_. 384 bool IsClassToCompile(const char* descriptor) const; 385 386 void RecordClassStatus(ClassReference ref, mirror::Class::Status status) 387 LOCKS_EXCLUDED(compiled_classes_lock_); 388 389 std::vector<uint8_t>* DeduplicateCode(const std::vector<uint8_t>& code); 390 SrcMap* DeduplicateSrcMappingTable(const SrcMap& src_map); 391 std::vector<uint8_t>* DeduplicateMappingTable(const std::vector<uint8_t>& code); 392 std::vector<uint8_t>* DeduplicateVMapTable(const std::vector<uint8_t>& code); 393 std::vector<uint8_t>* DeduplicateGCMap(const std::vector<uint8_t>& code); 394 std::vector<uint8_t>* DeduplicateCFIInfo(const std::vector<uint8_t>* cfi_info); 395 396 ProfileFile profile_file_; 397 bool profile_present_; 398 399 // Should the compiler run on this method given profile information? 400 bool SkipCompilation(const std::string& method_name); 401 402 // Get memory usage during compilation. 403 std::string GetMemoryUsageString() const; 404 405 private: 406 // These flags are internal to CompilerDriver for collecting INVOKE resolution statistics. 407 // The only external contract is that unresolved method has flags 0 and resolved non-0. 408 enum { 409 kBitMethodResolved = 0, 410 kBitVirtualMadeDirect, 411 kBitPreciseTypeDevirtualization, 412 kBitDirectCallToBoot, 413 kBitDirectMethodToBoot 414 }; 415 static constexpr int kFlagMethodResolved = 1 << kBitMethodResolved; 416 static constexpr int kFlagVirtualMadeDirect = 1 << kBitVirtualMadeDirect; 417 static constexpr int kFlagPreciseTypeDevirtualization = 1 << kBitPreciseTypeDevirtualization; 418 static constexpr int kFlagDirectCallToBoot = 1 << kBitDirectCallToBoot; 419 static constexpr int kFlagDirectMethodToBoot = 1 << kBitDirectMethodToBoot; 420 static constexpr int kFlagsMethodResolvedVirtualMadeDirect = 421 kFlagMethodResolved | kFlagVirtualMadeDirect; 422 static constexpr int kFlagsMethodResolvedPreciseTypeDevirtualization = 423 kFlagsMethodResolvedVirtualMadeDirect | kFlagPreciseTypeDevirtualization; 424 425 public: // TODO make private or eliminate. 426 // Compute constant code and method pointers when possible. 427 void GetCodeAndMethodForDirectCall(/*out*/InvokeType* type, 428 InvokeType sharp_type, 429 bool no_guarantee_of_dex_cache_entry, 430 const mirror::Class* referrer_class, 431 mirror::ArtMethod* method, 432 /*out*/int* stats_flags, 433 MethodReference* target_method, 434 uintptr_t* direct_code, uintptr_t* direct_method) 435 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 436 437 private: 438 void PreCompile(jobject class_loader, const std::vector<const DexFile*>& dex_files, 439 ThreadPool* thread_pool, TimingLogger* timings) 440 LOCKS_EXCLUDED(Locks::mutator_lock_); 441 442 void LoadImageClasses(TimingLogger* timings); 443 444 // Attempt to resolve all type, methods, fields, and strings 445 // referenced from code in the dex file following PathClassLoader 446 // ordering semantics. 447 void Resolve(jobject class_loader, const std::vector<const DexFile*>& dex_files, 448 ThreadPool* thread_pool, TimingLogger* timings) 449 LOCKS_EXCLUDED(Locks::mutator_lock_); 450 void ResolveDexFile(jobject class_loader, const DexFile& dex_file, 451 const std::vector<const DexFile*>& dex_files, 452 ThreadPool* thread_pool, TimingLogger* timings) 453 LOCKS_EXCLUDED(Locks::mutator_lock_); 454 455 void Verify(jobject class_loader, const std::vector<const DexFile*>& dex_files, 456 ThreadPool* thread_pool, TimingLogger* timings); 457 void VerifyDexFile(jobject class_loader, const DexFile& dex_file, 458 const std::vector<const DexFile*>& dex_files, 459 ThreadPool* thread_pool, TimingLogger* timings) 460 LOCKS_EXCLUDED(Locks::mutator_lock_); 461 462 void SetVerified(jobject class_loader, const std::vector<const DexFile*>& dex_files, 463 ThreadPool* thread_pool, TimingLogger* timings); 464 void SetVerifiedDexFile(jobject class_loader, const DexFile& dex_file, 465 const std::vector<const DexFile*>& dex_files, 466 ThreadPool* thread_pool, TimingLogger* timings) 467 LOCKS_EXCLUDED(Locks::mutator_lock_); 468 469 void InitializeClasses(jobject class_loader, const std::vector<const DexFile*>& dex_files, 470 ThreadPool* thread_pool, TimingLogger* timings) 471 LOCKS_EXCLUDED(Locks::mutator_lock_); 472 void InitializeClasses(jobject class_loader, const DexFile& dex_file, 473 const std::vector<const DexFile*>& dex_files, 474 ThreadPool* thread_pool, TimingLogger* timings) 475 LOCKS_EXCLUDED(Locks::mutator_lock_, compiled_classes_lock_); 476 477 void UpdateImageClasses(TimingLogger* timings) LOCKS_EXCLUDED(Locks::mutator_lock_); 478 static void FindClinitImageClassesCallback(mirror::Object* object, void* arg) 479 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 480 481 void Compile(jobject class_loader, const std::vector<const DexFile*>& dex_files, 482 ThreadPool* thread_pool, TimingLogger* timings); 483 void CompileDexFile(jobject class_loader, const DexFile& dex_file, 484 const std::vector<const DexFile*>& dex_files, 485 ThreadPool* thread_pool, TimingLogger* timings) 486 LOCKS_EXCLUDED(Locks::mutator_lock_); 487 void CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags, 488 InvokeType invoke_type, uint16_t class_def_idx, uint32_t method_idx, 489 jobject class_loader, const DexFile& dex_file, 490 DexToDexCompilationLevel dex_to_dex_compilation_level, 491 bool compilation_enabled) 492 LOCKS_EXCLUDED(compiled_methods_lock_); 493 494 static void CompileClass(const ParallelCompilationManager* context, size_t class_def_index) 495 LOCKS_EXCLUDED(Locks::mutator_lock_); 496 497 const CompilerOptions* const compiler_options_; 498 VerificationResults* const verification_results_; 499 DexFileToMethodInlinerMap* const method_inliner_map_; 500 501 std::unique_ptr<Compiler> compiler_; 502 503 const InstructionSet instruction_set_; 504 const InstructionSetFeatures* const instruction_set_features_; 505 506 // All class references that require 507 mutable ReaderWriterMutex freezing_constructor_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 508 std::set<ClassReference> freezing_constructor_classes_ GUARDED_BY(freezing_constructor_lock_); 509 510 typedef SafeMap<const ClassReference, CompiledClass*> ClassTable; 511 // All class references that this compiler has compiled. 512 mutable Mutex compiled_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 513 ClassTable compiled_classes_ GUARDED_BY(compiled_classes_lock_); 514 515 typedef SafeMap<const MethodReference, CompiledMethod*, MethodReferenceComparator> MethodTable; 516 // All method references that this compiler has compiled. 517 mutable Mutex compiled_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 518 MethodTable compiled_methods_ GUARDED_BY(compiled_methods_lock_); 519 // Number of non-relative patches in all compiled methods. These patches need space 520 // in the .oat_patches ELF section if requested in the compiler options. 521 size_t non_relative_linker_patch_count_ GUARDED_BY(compiled_methods_lock_); 522 523 const bool image_; 524 525 // If image_ is true, specifies the classes that will be included in 526 // the image. Note if image_classes_ is nullptr, all classes are 527 // included in the image. 528 std::unique_ptr<std::set<std::string>> image_classes_; 529 530 // If image_ is true, specifies the classes that will be compiled in 531 // the image. Note if classes_to_compile_ is nullptr, all classes are 532 // included in the image. 533 std::unique_ptr<std::set<std::string>> classes_to_compile_; 534 535 size_t thread_count_; 536 537 class AOTCompilationStats; 538 std::unique_ptr<AOTCompilationStats> stats_; 539 540 bool dump_stats_; 541 const bool dump_passes_; 542 543 CumulativeLogger* const timings_logger_; 544 545 typedef void (*CompilerCallbackFn)(CompilerDriver& driver); 546 typedef MutexLock* (*CompilerMutexLockFn)(CompilerDriver& driver); 547 548 typedef void (*DexToDexCompilerFn)(CompilerDriver& driver, 549 const DexFile::CodeItem* code_item, 550 uint32_t access_flags, InvokeType invoke_type, 551 uint32_t class_dex_idx, uint32_t method_idx, 552 jobject class_loader, const DexFile& dex_file, 553 DexToDexCompilationLevel dex_to_dex_compilation_level); 554 DexToDexCompilerFn dex_to_dex_compiler_; 555 556 void* compiler_context_; 557 558 pthread_key_t tls_key_; 559 560 // Arena pool used by the compiler. 561 ArenaPool arena_pool_; 562 563 bool support_boot_image_fixup_; 564 565 // DeDuplication data structures, these own the corresponding byte arrays. 566 template <typename ByteArray> 567 class DedupeHashFunc { 568 public: 569 size_t operator()(const ByteArray& array) const { 570 // For small arrays compute a hash using every byte. 571 static const size_t kSmallArrayThreshold = 16; 572 size_t hash = 0x811c9dc5; 573 if (array.size() <= kSmallArrayThreshold) { 574 for (auto b : array) { 575 hash = (hash * 16777619) ^ static_cast<uint8_t>(b); 576 } 577 } else { 578 // For larger arrays use the 2 bytes at 6 bytes (the location of a push registers 579 // instruction field for quick generated code on ARM) and then select a number of other 580 // values at random. 581 static const size_t kRandomHashCount = 16; 582 for (size_t i = 0; i < 2; ++i) { 583 uint8_t b = static_cast<uint8_t>(array[i + 6]); 584 hash = (hash * 16777619) ^ b; 585 } 586 for (size_t i = 2; i < kRandomHashCount; ++i) { 587 size_t r = i * 1103515245 + 12345; 588 uint8_t b = static_cast<uint8_t>(array[r % array.size()]); 589 hash = (hash * 16777619) ^ b; 590 } 591 } 592 hash += hash << 13; 593 hash ^= hash >> 7; 594 hash += hash << 3; 595 hash ^= hash >> 17; 596 hash += hash << 5; 597 return hash; 598 } 599 }; 600 601 DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc<std::vector<uint8_t>>, 4> dedupe_code_; 602 DedupeSet<SrcMap, size_t, DedupeHashFunc<SrcMap>, 4> dedupe_src_mapping_table_; 603 DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc<std::vector<uint8_t>>, 4> dedupe_mapping_table_; 604 DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc<std::vector<uint8_t>>, 4> dedupe_vmap_table_; 605 DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc<std::vector<uint8_t>>, 4> dedupe_gc_map_; 606 DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc<std::vector<uint8_t>>, 4> dedupe_cfi_info_; 607 608 DISALLOW_COPY_AND_ASSIGN(CompilerDriver); 609}; 610 611} // namespace art 612 613#endif // ART_COMPILER_DRIVER_COMPILER_DRIVER_H_ 614