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