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