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