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_DEX2OAT_DRIVER_COMPILER_DRIVER_H_ 18 #define ART_DEX2OAT_DRIVER_COMPILER_DRIVER_H_ 19 20 #include <atomic> 21 #include <set> 22 #include <string> 23 #include <vector> 24 25 #include "arch/instruction_set.h" 26 #include "base/array_ref.h" 27 #include "base/bit_utils.h" 28 #include "base/hash_set.h" 29 #include "base/mutex.h" 30 #include "base/os.h" 31 #include "base/quasi_atomic.h" 32 #include "base/safe_map.h" 33 #include "base/timing_logger.h" 34 #include "class_status.h" 35 #include "compiler.h" 36 #include "dex/class_reference.h" 37 #include "dex/dex_file_types.h" 38 #include "dex/method_reference.h" 39 #include "driver/compiled_method_storage.h" 40 #include "thread_pool.h" 41 #include "utils/atomic_dex_ref_map.h" 42 43 namespace art { 44 45 namespace dex { 46 struct CodeItem; 47 } // namespace dex 48 49 namespace mirror { 50 class Class; 51 class DexCache; 52 } // namespace mirror 53 54 namespace verifier { 55 class MethodVerifier; 56 class VerifierDepsTest; 57 } // namespace verifier 58 59 class ArtField; 60 class BitVector; 61 class CompiledMethod; 62 class CompilerOptions; 63 class DexCompilationUnit; 64 class DexFile; 65 template<class T> class Handle; 66 struct InlineIGetIPutData; 67 class InstructionSetFeatures; 68 class InternTable; 69 enum InvokeType : uint32_t; 70 class MemberOffset; 71 template<class MirrorType> class ObjPtr; 72 class ParallelCompilationManager; 73 class ProfileCompilationInfo; 74 class ScopedObjectAccess; 75 template <class Allocator> class SrcMap; 76 class TimingLogger; 77 class VdexFile; 78 class VerificationResults; 79 80 class CompilerDriver { 81 public: 82 // Create a compiler targeting the requested "instruction_set". 83 // "image" should be true if image specific optimizations should be 84 // enabled. "image_classes" lets the compiler know what classes it 85 // can assume will be in the image, with null implying all available 86 // classes. 87 CompilerDriver(const CompilerOptions* compiler_options, 88 const VerificationResults* verification_results, 89 Compiler::Kind compiler_kind, 90 size_t thread_count, 91 int swap_fd); 92 93 ~CompilerDriver(); 94 95 void PrepareDexFilesForOatFile(TimingLogger* timings); 96 97 // Set dex files classpath. 98 void SetClasspathDexFiles(const std::vector<const DexFile*>& dex_files); 99 100 // Initialize and destroy thread pools. This is exposed because we do not want 101 // to do this twice, for PreCompile() and CompileAll(). 102 void InitializeThreadPools(); 103 void FreeThreadPools(); 104 105 void PreCompile(jobject class_loader, 106 const std::vector<const DexFile*>& dex_files, 107 TimingLogger* timings, 108 /*inout*/ HashSet<std::string>* image_classes) 109 REQUIRES(!Locks::mutator_lock_); 110 void CompileAll(jobject class_loader, 111 const std::vector<const DexFile*>& dex_files, 112 TimingLogger* timings) 113 REQUIRES(!Locks::mutator_lock_); 114 GetCompilerOptions()115 const CompilerOptions& GetCompilerOptions() const { 116 return *compiler_options_; 117 } 118 GetVerificationResults()119 const VerificationResults* GetVerificationResults() const { 120 return verification_results_; 121 } 122 GetCompiler()123 Compiler* GetCompiler() const { 124 return compiler_.get(); 125 } 126 127 // Generate the trampolines that are invoked by unresolved direct methods. 128 std::unique_ptr<const std::vector<uint8_t>> CreateJniDlsymLookupTrampoline() const; 129 std::unique_ptr<const std::vector<uint8_t>> CreateJniDlsymLookupCriticalTrampoline() const; 130 std::unique_ptr<const std::vector<uint8_t>> CreateQuickGenericJniTrampoline() const; 131 std::unique_ptr<const std::vector<uint8_t>> CreateQuickImtConflictTrampoline() const; 132 std::unique_ptr<const std::vector<uint8_t>> CreateQuickResolutionTrampoline() const; 133 std::unique_ptr<const std::vector<uint8_t>> CreateQuickToInterpreterBridge() const; 134 std::unique_ptr<const std::vector<uint8_t>> CreateNterpTrampoline() const; 135 136 ClassStatus GetClassStatus(const ClassReference& ref) const; 137 bool GetCompiledClass(const ClassReference& ref, ClassStatus* status) const; 138 139 CompiledMethod* GetCompiledMethod(MethodReference ref) const; 140 // Add a compiled method. 141 void AddCompiledMethod(const MethodReference& method_ref, CompiledMethod* const compiled_method); 142 CompiledMethod* RemoveCompiledMethod(const MethodReference& method_ref); 143 144 // Resolve compiling method's class. Returns null on failure. 145 ObjPtr<mirror::Class> ResolveCompilingMethodsClass(const ScopedObjectAccess& soa, 146 Handle<mirror::DexCache> dex_cache, 147 Handle<mirror::ClassLoader> class_loader, 148 const DexCompilationUnit* mUnit) 149 REQUIRES_SHARED(Locks::mutator_lock_); 150 151 ObjPtr<mirror::Class> ResolveClass(const ScopedObjectAccess& soa, 152 Handle<mirror::DexCache> dex_cache, 153 Handle<mirror::ClassLoader> class_loader, 154 dex::TypeIndex type_index, 155 const DexCompilationUnit* mUnit) 156 REQUIRES_SHARED(Locks::mutator_lock_); 157 158 // Resolve a field. Returns null on failure, including incompatible class change. 159 // NOTE: Unlike ClassLinker's ResolveField(), this method enforces is_static. 160 ArtField* ResolveField(const ScopedObjectAccess& soa, 161 Handle<mirror::DexCache> dex_cache, 162 Handle<mirror::ClassLoader> class_loader, 163 uint32_t field_idx, 164 bool is_static) 165 REQUIRES_SHARED(Locks::mutator_lock_); 166 167 // Can we fast-path an IGET/IPUT access to an instance field? If yes, compute the field offset. 168 std::pair<bool, bool> IsFastInstanceField(ObjPtr<mirror::DexCache> dex_cache, 169 ObjPtr<mirror::Class> referrer_class, 170 ArtField* resolved_field, 171 uint16_t field_idx) 172 REQUIRES_SHARED(Locks::mutator_lock_); 173 174 void ProcessedInstanceField(bool resolved); 175 void ProcessedStaticField(bool resolved, bool local); 176 177 // Can we fast path instance field access? Computes field's offset and volatility. 178 bool ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put, 179 MemberOffset* field_offset, bool* is_volatile) 180 REQUIRES(!Locks::mutator_lock_); 181 182 ArtField* ComputeInstanceFieldInfo(uint32_t field_idx, 183 const DexCompilationUnit* mUnit, 184 bool is_put, 185 const ScopedObjectAccess& soa) 186 REQUIRES_SHARED(Locks::mutator_lock_); 187 188 GetThreadCount()189 size_t GetThreadCount() const { 190 return parallel_thread_count_; 191 } 192 SetDedupeEnabled(bool dedupe_enabled)193 void SetDedupeEnabled(bool dedupe_enabled) { 194 compiled_method_storage_.SetDedupeEnabled(dedupe_enabled); 195 } 196 DedupeEnabled()197 bool DedupeEnabled() const { 198 return compiled_method_storage_.DedupeEnabled(); 199 } 200 201 // Checks whether profile guided verification is enabled and if the method should be verified 202 // according to the profile file. 203 bool ShouldVerifyClassBasedOnProfile(const DexFile& dex_file, uint16_t class_idx) const; 204 205 void RecordClassStatus(const ClassReference& ref, ClassStatus status); 206 207 // Get memory usage during compilation. 208 std::string GetMemoryUsageString(bool extended) const; 209 SetHadHardVerifierFailure()210 void SetHadHardVerifierFailure() { 211 had_hard_verifier_failure_ = true; 212 } AddSoftVerifierFailure()213 void AddSoftVerifierFailure() { 214 number_of_soft_verifier_failures_++; 215 } 216 GetCompilerKind()217 Compiler::Kind GetCompilerKind() { 218 return compiler_kind_; 219 } 220 GetCompiledMethodStorage()221 CompiledMethodStorage* GetCompiledMethodStorage() { 222 return &compiled_method_storage_; 223 } 224 GetCompiledMethodStorage()225 const CompiledMethodStorage* GetCompiledMethodStorage() const { 226 return &compiled_method_storage_; 227 } 228 229 private: 230 void LoadImageClasses(TimingLogger* timings, /*inout*/ HashSet<std::string>* image_classes) 231 REQUIRES(!Locks::mutator_lock_); 232 233 // Attempt to resolve all type, methods, fields, and strings 234 // referenced from code in the dex file following PathClassLoader 235 // ordering semantics. 236 void Resolve(jobject class_loader, 237 const std::vector<const DexFile*>& dex_files, 238 TimingLogger* timings) 239 REQUIRES(!Locks::mutator_lock_); 240 void ResolveDexFile(jobject class_loader, 241 const DexFile& dex_file, 242 const std::vector<const DexFile*>& dex_files, 243 ThreadPool* thread_pool, 244 size_t thread_count, 245 TimingLogger* timings) 246 REQUIRES(!Locks::mutator_lock_); 247 248 // Do fast verification through VerifierDeps if possible. Return whether 249 // verification was successful. 250 bool FastVerify(jobject class_loader, 251 const std::vector<const DexFile*>& dex_files, 252 TimingLogger* timings); 253 254 void Verify(jobject class_loader, 255 const std::vector<const DexFile*>& dex_files, 256 TimingLogger* timings); 257 258 void VerifyDexFile(jobject class_loader, 259 const DexFile& dex_file, 260 const std::vector<const DexFile*>& dex_files, 261 ThreadPool* thread_pool, 262 size_t thread_count, 263 TimingLogger* timings) 264 REQUIRES(!Locks::mutator_lock_); 265 266 void SetVerified(jobject class_loader, 267 const std::vector<const DexFile*>& dex_files, 268 TimingLogger* timings); 269 void SetVerifiedDexFile(jobject class_loader, 270 const DexFile& dex_file, 271 const std::vector<const DexFile*>& dex_files, 272 ThreadPool* thread_pool, 273 size_t thread_count, 274 TimingLogger* timings) 275 REQUIRES(!Locks::mutator_lock_); 276 277 void InitializeClasses(jobject class_loader, 278 const std::vector<const DexFile*>& dex_files, 279 TimingLogger* timings) 280 REQUIRES(!Locks::mutator_lock_); 281 void InitializeClasses(jobject class_loader, 282 const DexFile& dex_file, 283 const std::vector<const DexFile*>& dex_files, 284 TimingLogger* timings) 285 REQUIRES(!Locks::mutator_lock_); 286 287 void UpdateImageClasses(TimingLogger* timings, /*inout*/ HashSet<std::string>* image_classes) 288 REQUIRES(!Locks::mutator_lock_); 289 290 void Compile(jobject class_loader, 291 const std::vector<const DexFile*>& dex_files, 292 TimingLogger* timings); 293 294 void CheckThreadPools(); 295 296 // Resolve const string literals that are loaded from dex code. If only_startup_strings is 297 // specified, only methods that are marked startup in the profile are resolved. 298 void ResolveConstStrings(const std::vector<const DexFile*>& dex_files, 299 bool only_startup_strings, 300 /*inout*/ TimingLogger* timings); 301 302 const CompilerOptions* const compiler_options_; 303 const VerificationResults* const verification_results_; 304 305 std::unique_ptr<Compiler> compiler_; 306 Compiler::Kind compiler_kind_; 307 308 // All class references that this compiler has compiled. Indexed by class defs. 309 using ClassStateTable = AtomicDexRefMap<ClassReference, ClassStatus>; 310 ClassStateTable compiled_classes_; 311 // All class references that are in the classpath. Indexed by class defs. 312 ClassStateTable classpath_classes_; 313 314 using MethodTable = AtomicDexRefMap<MethodReference, CompiledMethod*>; 315 316 // All method references that this compiler has compiled. 317 MethodTable compiled_methods_; 318 319 std::atomic<uint32_t> number_of_soft_verifier_failures_; 320 321 bool had_hard_verifier_failure_; 322 323 // A thread pool that can (potentially) run tasks in parallel. 324 size_t parallel_thread_count_; 325 std::unique_ptr<ThreadPool> parallel_thread_pool_; 326 327 // A thread pool that guarantees running single-threaded on the main thread. 328 std::unique_ptr<ThreadPool> single_thread_pool_; 329 330 class AOTCompilationStats; 331 std::unique_ptr<AOTCompilationStats> stats_; 332 333 CompiledMethodStorage compiled_method_storage_; 334 335 size_t max_arena_alloc_; 336 337 friend class CommonCompilerDriverTest; 338 friend class CompileClassVisitor; 339 friend class InitializeClassVisitor; 340 friend class verifier::VerifierDepsTest; 341 DISALLOW_COPY_AND_ASSIGN(CompilerDriver); 342 }; 343 344 } // namespace art 345 346 #endif // ART_DEX2OAT_DRIVER_COMPILER_DRIVER_H_ 347