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