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