compiler_driver.h revision 590fee9e8972f872301c2d16a575d579ee564bee
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 "class_reference.h" 26#include "compiled_class.h" 27#include "compiled_method.h" 28#include "dex_file.h" 29#include "dex/arena_allocator.h" 30#include "instruction_set.h" 31#include "invoke_type.h" 32#include "method_reference.h" 33#include "os.h" 34#include "runtime.h" 35#include "safe_map.h" 36#include "thread_pool.h" 37#include "utils/dedupe_set.h" 38 39namespace art { 40 41class AOTCompilationStats; 42class ParallelCompilationManager; 43class DexCompilationUnit; 44class OatWriter; 45class TimingLogger; 46 47enum CompilerBackend { 48 kQuick, 49 kPortable, 50 kNoBackend 51}; 52 53enum EntryPointCallingConvention { 54 // ABI of invocations to a method's interpreter entry point. 55 kInterpreterAbi, 56 // ABI of calls to a method's native code, only used for native methods. 57 kJniAbi, 58 // ABI of calls to a method's portable code entry point. 59 kPortableAbi, 60 // ABI of calls to a method's quick code entry point. 61 kQuickAbi 62}; 63 64enum DexToDexCompilationLevel { 65 kDontDexToDexCompile, // Only meaning wrt image time interpretation. 66 kRequired, // Dex-to-dex compilation required for correctness. 67 kOptimize // Perform required transformation and peep-hole optimizations. 68}; 69 70// Thread-local storage compiler worker threads 71class CompilerTls { 72 public: 73 CompilerTls() : llvm_info_(NULL) {} 74 ~CompilerTls() {} 75 76 void* GetLLVMInfo() { return llvm_info_; } 77 78 void SetLLVMInfo(void* llvm_info) { llvm_info_ = llvm_info; } 79 80 private: 81 void* llvm_info_; 82}; 83 84class CompilerDriver { 85 public: 86 typedef std::set<std::string> DescriptorSet; 87 88 // Create a compiler targeting the requested "instruction_set". 89 // "image" should be true if image specific optimizations should be 90 // enabled. "image_classes" lets the compiler know what classes it 91 // can assume will be in the image, with NULL implying all available 92 // classes. 93 explicit CompilerDriver(CompilerBackend compiler_backend, InstructionSet instruction_set, 94 InstructionSetFeatures instruction_set_features, 95 bool image, DescriptorSet* image_classes, 96 size_t thread_count, bool dump_stats); 97 98 ~CompilerDriver(); 99 100 void CompileAll(jobject class_loader, const std::vector<const DexFile*>& dex_files, 101 base::TimingLogger& timings) 102 LOCKS_EXCLUDED(Locks::mutator_lock_); 103 104 // Compile a single Method 105 void CompileOne(const mirror::ArtMethod* method, base::TimingLogger& timings) 106 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 107 108 const InstructionSet& GetInstructionSet() const { 109 return instruction_set_; 110 } 111 112 const InstructionSetFeatures& GetInstructionSetFeatures() const { 113 return instruction_set_features_; 114 } 115 116 CompilerBackend GetCompilerBackend() const { 117 return compiler_backend_; 118 } 119 120 // Are we compiling and creating an image file? 121 bool IsImage() const { 122 return image_; 123 } 124 125 DescriptorSet* GetImageClasses() const { 126 return image_classes_.get(); 127 } 128 129 CompilerTls* GetTls(); 130 131 // Generate the trampolines that are invoked by unresolved direct methods. 132 const std::vector<uint8_t>* CreateInterpreterToInterpreterBridge() const 133 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 134 const std::vector<uint8_t>* CreateInterpreterToCompiledCodeBridge() const 135 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 136 const std::vector<uint8_t>* CreateJniDlsymLookup() const 137 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 138 const std::vector<uint8_t>* CreatePortableImtConflictTrampoline() const 139 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 140 const std::vector<uint8_t>* CreatePortableResolutionTrampoline() const 141 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 142 const std::vector<uint8_t>* CreatePortableToInterpreterBridge() const 143 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 144 const std::vector<uint8_t>* CreateQuickImtConflictTrampoline() const 145 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 146 const std::vector<uint8_t>* CreateQuickResolutionTrampoline() const 147 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 148 const std::vector<uint8_t>* CreateQuickToInterpreterBridge() const 149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 150 151 CompiledClass* GetCompiledClass(ClassReference ref) const 152 LOCKS_EXCLUDED(compiled_classes_lock_); 153 154 CompiledMethod* GetCompiledMethod(MethodReference ref) const 155 LOCKS_EXCLUDED(compiled_methods_lock_); 156 157 void AddRequiresConstructorBarrier(Thread* self, const DexFile* dex_file, 158 uint16_t class_def_index); 159 bool RequiresConstructorBarrier(Thread* self, const DexFile* dex_file, uint16_t class_def_index); 160 161 // Callbacks from compiler to see what runtime checks must be generated. 162 163 bool CanAssumeTypeIsPresentInDexCache(const DexFile& dex_file, uint32_t type_idx) 164 LOCKS_EXCLUDED(Locks::mutator_lock_); 165 166 bool CanAssumeStringIsPresentInDexCache(const DexFile& dex_file, uint32_t string_idx) 167 LOCKS_EXCLUDED(Locks::mutator_lock_); 168 169 // Are runtime access checks necessary in the compiled code? 170 bool CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file, 171 uint32_t type_idx, bool* type_known_final = NULL, 172 bool* type_known_abstract = NULL, 173 bool* equals_referrers_class = NULL) 174 LOCKS_EXCLUDED(Locks::mutator_lock_); 175 176 // Are runtime access and instantiable checks necessary in the code? 177 bool CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file, 178 uint32_t type_idx) 179 LOCKS_EXCLUDED(Locks::mutator_lock_); 180 181 // Can we fast path instance field access? Computes field's offset and volatility. 182 bool ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put, 183 int* field_offset, bool* is_volatile) 184 LOCKS_EXCLUDED(Locks::mutator_lock_); 185 186 // Can we fastpath static field access? Computes field's offset, volatility and whether the 187 // field is within the referrer (which can avoid checking class initialization). 188 bool ComputeStaticFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put, 189 int* field_offset, int* ssb_index, 190 bool* is_referrers_class, bool* is_volatile) 191 LOCKS_EXCLUDED(Locks::mutator_lock_); 192 193 // Can we fastpath a interface, super class or virtual method call? Computes method's vtable 194 // index. 195 bool ComputeInvokeInfo(const DexCompilationUnit* mUnit, const uint32_t dex_pc, 196 bool update_stats, bool enable_devirtualization, 197 InvokeType* type, MethodReference* target_method, int* vtable_idx, 198 uintptr_t* direct_code, uintptr_t* direct_method) 199 LOCKS_EXCLUDED(Locks::mutator_lock_); 200 201 bool IsSafeCast(const MethodReference& mr, uint32_t dex_pc); 202 203 // Record patch information for later fix up. 204 void AddCodePatch(const DexFile* dex_file, 205 uint16_t referrer_class_def_idx, 206 uint32_t referrer_method_idx, 207 InvokeType referrer_invoke_type, 208 uint32_t target_method_idx, 209 InvokeType target_invoke_type, 210 size_t literal_offset) 211 LOCKS_EXCLUDED(compiled_methods_lock_); 212 void AddMethodPatch(const DexFile* dex_file, 213 uint16_t referrer_class_def_idx, 214 uint32_t referrer_method_idx, 215 InvokeType referrer_invoke_type, 216 uint32_t target_method_idx, 217 InvokeType target_invoke_type, 218 size_t literal_offset) 219 LOCKS_EXCLUDED(compiled_methods_lock_); 220 221 void SetBitcodeFileName(std::string const& filename); 222 223 bool GetSupportBootImageFixup() const { 224 return support_boot_image_fixup_; 225 } 226 227 void SetSupportBootImageFixup(bool support_boot_image_fixup) { 228 support_boot_image_fixup_ = support_boot_image_fixup; 229 } 230 231 ArenaPool& GetArenaPool() { 232 return arena_pool_; 233 } 234 235 bool WriteElf(const std::string& android_root, 236 bool is_host, 237 const std::vector<const DexFile*>& dex_files, 238 OatWriter& oat_writer, 239 File* file); 240 241 // TODO: move to a common home for llvm helpers once quick/portable are merged 242 static void InstructionSetToLLVMTarget(InstructionSet instruction_set, 243 std::string& target_triple, 244 std::string& target_cpu, 245 std::string& target_attr); 246 247 void SetCompilerContext(void* compiler_context) { 248 compiler_context_ = compiler_context; 249 } 250 251 void* GetCompilerContext() const { 252 return compiler_context_; 253 } 254 255 size_t GetThreadCount() const { 256 return thread_count_; 257 } 258 259 class PatchInformation { 260 public: 261 const DexFile& GetDexFile() const { 262 return *dex_file_; 263 } 264 uint16_t GetReferrerClassDefIdx() const { 265 return referrer_class_def_idx_; 266 } 267 uint32_t GetReferrerMethodIdx() const { 268 return referrer_method_idx_; 269 } 270 InvokeType GetReferrerInvokeType() const { 271 return referrer_invoke_type_; 272 } 273 uint32_t GetTargetMethodIdx() const { 274 return target_method_idx_; 275 } 276 InvokeType GetTargetInvokeType() const { 277 return target_invoke_type_; 278 } 279 size_t GetLiteralOffset() const {; 280 return literal_offset_; 281 } 282 283 private: 284 PatchInformation(const DexFile* dex_file, 285 uint16_t referrer_class_def_idx, 286 uint32_t referrer_method_idx, 287 InvokeType referrer_invoke_type, 288 uint32_t target_method_idx, 289 InvokeType target_invoke_type, 290 size_t literal_offset) 291 : dex_file_(dex_file), 292 referrer_class_def_idx_(referrer_class_def_idx), 293 referrer_method_idx_(referrer_method_idx), 294 referrer_invoke_type_(referrer_invoke_type), 295 target_method_idx_(target_method_idx), 296 target_invoke_type_(target_invoke_type), 297 literal_offset_(literal_offset) { 298 CHECK(dex_file_ != NULL); 299 } 300 301 const DexFile* const dex_file_; 302 const uint16_t referrer_class_def_idx_; 303 const uint32_t referrer_method_idx_; 304 const InvokeType referrer_invoke_type_; 305 const uint32_t target_method_idx_; 306 const InvokeType target_invoke_type_; 307 const size_t literal_offset_; 308 309 friend class CompilerDriver; 310 DISALLOW_COPY_AND_ASSIGN(PatchInformation); 311 }; 312 313 const std::vector<const PatchInformation*>& GetCodeToPatch() const { 314 return code_to_patch_; 315 } 316 const std::vector<const PatchInformation*>& GetMethodsToPatch() const { 317 return methods_to_patch_; 318 } 319 320 // Checks if class specified by type_idx is one of the image_classes_ 321 bool IsImageClass(const char* descriptor) const; 322 323 void RecordClassStatus(ClassReference ref, mirror::Class::Status status) 324 LOCKS_EXCLUDED(compiled_classes_lock_); 325 326 std::vector<uint8_t>* DeduplicateCode(const std::vector<uint8_t>& code); 327 std::vector<uint8_t>* DeduplicateMappingTable(const std::vector<uint8_t>& code); 328 std::vector<uint8_t>* DeduplicateVMapTable(const std::vector<uint8_t>& code); 329 std::vector<uint8_t>* DeduplicateGCMap(const std::vector<uint8_t>& code); 330 331 private: 332 // Compute constant code and method pointers when possible 333 void GetCodeAndMethodForDirectCall(InvokeType* type, InvokeType sharp_type, 334 bool no_guarantee_of_dex_cache_entry, 335 mirror::Class* referrer_class, 336 mirror::ArtMethod* method, 337 bool update_stats, 338 MethodReference* target_method, 339 uintptr_t* direct_code, uintptr_t* direct_method) 340 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 341 342 void PreCompile(jobject class_loader, const std::vector<const DexFile*>& dex_files, 343 ThreadPool& thread_pool, base::TimingLogger& timings) 344 LOCKS_EXCLUDED(Locks::mutator_lock_); 345 346 void LoadImageClasses(base::TimingLogger& timings); 347 348 // Attempt to resolve all type, methods, fields, and strings 349 // referenced from code in the dex file following PathClassLoader 350 // ordering semantics. 351 void Resolve(jobject class_loader, const std::vector<const DexFile*>& dex_files, 352 ThreadPool& thread_pool, base::TimingLogger& timings) 353 LOCKS_EXCLUDED(Locks::mutator_lock_); 354 void ResolveDexFile(jobject class_loader, const DexFile& dex_file, 355 ThreadPool& thread_pool, base::TimingLogger& timings) 356 LOCKS_EXCLUDED(Locks::mutator_lock_); 357 358 void Verify(jobject class_loader, const std::vector<const DexFile*>& dex_files, 359 ThreadPool& thread_pool, base::TimingLogger& timings); 360 void VerifyDexFile(jobject class_loader, const DexFile& dex_file, 361 ThreadPool& thread_pool, base::TimingLogger& timings) 362 LOCKS_EXCLUDED(Locks::mutator_lock_); 363 364 void InitializeClasses(jobject class_loader, const std::vector<const DexFile*>& dex_files, 365 ThreadPool& thread_pool, base::TimingLogger& timings) 366 LOCKS_EXCLUDED(Locks::mutator_lock_); 367 void InitializeClasses(jobject class_loader, const DexFile& dex_file, 368 ThreadPool& thread_pool, base::TimingLogger& timings) 369 LOCKS_EXCLUDED(Locks::mutator_lock_, compiled_classes_lock_); 370 371 void UpdateImageClasses(base::TimingLogger& timings) 372 LOCKS_EXCLUDED(Locks::mutator_lock_); 373 static void FindClinitImageClassesCallback(mirror::Object* object, void* arg) 374 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 375 376 void Compile(jobject class_loader, const std::vector<const DexFile*>& dex_files, 377 ThreadPool& thread_pool, base::TimingLogger& timings); 378 void CompileDexFile(jobject class_loader, const DexFile& dex_file, 379 ThreadPool& thread_pool, base::TimingLogger& timings) 380 LOCKS_EXCLUDED(Locks::mutator_lock_); 381 void CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags, 382 InvokeType invoke_type, uint16_t class_def_idx, uint32_t method_idx, 383 jobject class_loader, const DexFile& dex_file, 384 DexToDexCompilationLevel dex_to_dex_compilation_level) 385 LOCKS_EXCLUDED(compiled_methods_lock_); 386 387 static void CompileClass(const ParallelCompilationManager* context, size_t class_def_index) 388 LOCKS_EXCLUDED(Locks::mutator_lock_); 389 390 std::vector<const PatchInformation*> code_to_patch_; 391 std::vector<const PatchInformation*> methods_to_patch_; 392 393 CompilerBackend compiler_backend_; 394 395 const InstructionSet instruction_set_; 396 const InstructionSetFeatures instruction_set_features_; 397 398 // All class references that require 399 mutable ReaderWriterMutex freezing_constructor_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 400 std::set<ClassReference> freezing_constructor_classes_ GUARDED_BY(freezing_constructor_lock_); 401 402 typedef SafeMap<const ClassReference, CompiledClass*> ClassTable; 403 // All class references that this compiler has compiled. 404 mutable Mutex compiled_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 405 ClassTable compiled_classes_ GUARDED_BY(compiled_classes_lock_); 406 407 typedef SafeMap<const MethodReference, CompiledMethod*, MethodReferenceComparator> MethodTable; 408 // All method references that this compiler has compiled. 409 mutable Mutex compiled_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 410 MethodTable compiled_methods_ GUARDED_BY(compiled_methods_lock_); 411 412 const bool image_; 413 414 // If image_ is true, specifies the classes that will be included in 415 // the image. Note if image_classes_ is NULL, all classes are 416 // included in the image. 417 UniquePtr<DescriptorSet> image_classes_; 418 419 size_t thread_count_; 420 uint64_t start_ns_; 421 422 UniquePtr<AOTCompilationStats> stats_; 423 424 bool dump_stats_; 425 426 typedef void (*CompilerCallbackFn)(CompilerDriver& driver); 427 typedef MutexLock* (*CompilerMutexLockFn)(CompilerDriver& driver); 428 429 void* compiler_library_; 430 431 typedef CompiledMethod* (*CompilerFn)(CompilerDriver& driver, 432 const DexFile::CodeItem* code_item, 433 uint32_t access_flags, InvokeType invoke_type, 434 uint32_t class_dex_idx, uint32_t method_idx, 435 jobject class_loader, const DexFile& dex_file); 436 437 typedef void (*DexToDexCompilerFn)(CompilerDriver& driver, 438 const DexFile::CodeItem* code_item, 439 uint32_t access_flags, InvokeType invoke_type, 440 uint32_t class_dex_idx, uint32_t method_idx, 441 jobject class_loader, const DexFile& dex_file, 442 DexToDexCompilationLevel dex_to_dex_compilation_level); 443 CompilerFn compiler_; 444#ifdef ART_SEA_IR_MODE 445 CompilerFn sea_ir_compiler_; 446#endif 447 448 DexToDexCompilerFn dex_to_dex_compiler_; 449 450 void* compiler_context_; 451 452 typedef CompiledMethod* (*JniCompilerFn)(CompilerDriver& driver, 453 uint32_t access_flags, uint32_t method_idx, 454 const DexFile& dex_file); 455 JniCompilerFn jni_compiler_; 456 457 pthread_key_t tls_key_; 458 459 // Arena pool used by the compiler. 460 ArenaPool arena_pool_; 461 462 typedef void (*CompilerEnableAutoElfLoadingFn)(CompilerDriver& driver); 463 CompilerEnableAutoElfLoadingFn compiler_enable_auto_elf_loading_; 464 465 typedef const void* (*CompilerGetMethodCodeAddrFn) 466 (const CompilerDriver& driver, const CompiledMethod* cm, const mirror::ArtMethod* method); 467 CompilerGetMethodCodeAddrFn compiler_get_method_code_addr_; 468 469 bool support_boot_image_fixup_; 470 471 // DeDuplication data structures, these own the corresponding byte arrays. 472 class DedupeHashFunc { 473 public: 474 size_t operator()(const std::vector<uint8_t>& array) const { 475 // For small arrays compute a hash using every byte. 476 static const size_t kSmallArrayThreshold = 16; 477 size_t hash = 0x811c9dc5; 478 if (array.size() <= kSmallArrayThreshold) { 479 for (uint8_t b : array) { 480 hash = (hash * 16777619) ^ b; 481 } 482 } else { 483 // For larger arrays use the 2 bytes at 6 bytes (the location of a push registers 484 // instruction field for quick generated code on ARM) and then select a number of other 485 // values at random. 486 static const size_t kRandomHashCount = 16; 487 for (size_t i = 0; i < 2; ++i) { 488 uint8_t b = array[i + 6]; 489 hash = (hash * 16777619) ^ b; 490 } 491 for (size_t i = 2; i < kRandomHashCount; ++i) { 492 size_t r = i * 1103515245 + 12345; 493 uint8_t b = array[r % array.size()]; 494 hash = (hash * 16777619) ^ b; 495 } 496 } 497 hash += hash << 13; 498 hash ^= hash >> 7; 499 hash += hash << 3; 500 hash ^= hash >> 17; 501 hash += hash << 5; 502 return hash; 503 } 504 }; 505 DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc, 4> dedupe_code_; 506 DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc, 4> dedupe_mapping_table_; 507 DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc, 4> dedupe_vmap_table_; 508 DedupeSet<std::vector<uint8_t>, size_t, DedupeHashFunc, 4> dedupe_gc_map_; 509 510 DISALLOW_COPY_AND_ASSIGN(CompilerDriver); 511}; 512 513} // namespace art 514 515#endif // ART_COMPILER_DRIVER_COMPILER_DRIVER_H_ 516