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 size_t thread_count, 90 int swap_fd); 91 92 ~CompilerDriver(); 93 94 void PrepareDexFilesForOatFile(TimingLogger* timings); 95 96 // Set dex files classpath. 97 void SetClasspathDexFiles(const std::vector<const DexFile*>& dex_files); 98 99 // Initialize and destroy thread pools. This is exposed because we do not want 100 // to do this twice, for PreCompile() and CompileAll(). 101 void InitializeThreadPools(); 102 void FreeThreadPools(); 103 104 void PreCompile(jobject class_loader, 105 const std::vector<const DexFile*>& dex_files, 106 TimingLogger* timings, 107 /*inout*/ HashSet<std::string>* image_classes) 108 REQUIRES(!Locks::mutator_lock_); 109 void CompileAll(jobject class_loader, 110 const std::vector<const DexFile*>& dex_files, 111 TimingLogger* timings) 112 REQUIRES(!Locks::mutator_lock_); 113 GetCompilerOptions()114 const CompilerOptions& GetCompilerOptions() const { 115 return *compiler_options_; 116 } 117 GetVerificationResults()118 const VerificationResults* GetVerificationResults() const { 119 return verification_results_; 120 } 121 GetCompiler()122 Compiler* GetCompiler() const { 123 return compiler_.get(); 124 } 125 126 // Generate the trampolines that are invoked by unresolved direct methods. 127 std::unique_ptr<const std::vector<uint8_t>> CreateJniDlsymLookupTrampoline() const; 128 std::unique_ptr<const std::vector<uint8_t>> CreateJniDlsymLookupCriticalTrampoline() const; 129 std::unique_ptr<const std::vector<uint8_t>> CreateQuickGenericJniTrampoline() const; 130 std::unique_ptr<const std::vector<uint8_t>> CreateQuickImtConflictTrampoline() const; 131 std::unique_ptr<const std::vector<uint8_t>> CreateQuickResolutionTrampoline() const; 132 std::unique_ptr<const std::vector<uint8_t>> CreateQuickToInterpreterBridge() const; 133 std::unique_ptr<const std::vector<uint8_t>> CreateNterpTrampoline() const; 134 135 ClassStatus GetClassStatus(const ClassReference& ref) const; 136 bool GetCompiledClass(const ClassReference& ref, ClassStatus* status) const; 137 138 CompiledMethod* GetCompiledMethod(MethodReference ref) const; 139 // Add a compiled method. 140 void AddCompiledMethod(const MethodReference& method_ref, CompiledMethod* const compiled_method); 141 CompiledMethod* RemoveCompiledMethod(const MethodReference& method_ref); 142 143 // Resolve compiling method's class. Returns null on failure. 144 ObjPtr<mirror::Class> ResolveCompilingMethodsClass(const ScopedObjectAccess& soa, 145 Handle<mirror::DexCache> dex_cache, 146 Handle<mirror::ClassLoader> class_loader, 147 const DexCompilationUnit* mUnit) 148 REQUIRES_SHARED(Locks::mutator_lock_); 149 150 ObjPtr<mirror::Class> ResolveClass(const ScopedObjectAccess& soa, 151 Handle<mirror::DexCache> dex_cache, 152 Handle<mirror::ClassLoader> class_loader, 153 dex::TypeIndex type_index, 154 const DexCompilationUnit* mUnit) 155 REQUIRES_SHARED(Locks::mutator_lock_); 156 157 // Resolve a field. Returns null on failure, including incompatible class change. 158 // NOTE: Unlike ClassLinker's ResolveField(), this method enforces is_static. 159 ArtField* ResolveField(const ScopedObjectAccess& soa, 160 Handle<mirror::DexCache> dex_cache, 161 Handle<mirror::ClassLoader> class_loader, 162 uint32_t field_idx, 163 bool is_static) 164 REQUIRES_SHARED(Locks::mutator_lock_); 165 166 // Can we fast-path an IGET/IPUT access to an instance field? If yes, compute the field offset. 167 std::pair<bool, bool> IsFastInstanceField(ObjPtr<mirror::DexCache> dex_cache, 168 ObjPtr<mirror::Class> referrer_class, 169 ArtField* resolved_field, 170 uint16_t field_idx) 171 REQUIRES_SHARED(Locks::mutator_lock_); 172 173 void ProcessedInstanceField(bool resolved); 174 void ProcessedStaticField(bool resolved, bool local); 175 176 // Can we fast path instance field access? Computes field's offset and volatility. 177 bool ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put, 178 MemberOffset* field_offset, bool* is_volatile) 179 REQUIRES(!Locks::mutator_lock_); 180 181 ArtField* ComputeInstanceFieldInfo(uint32_t field_idx, 182 const DexCompilationUnit* mUnit, 183 bool is_put, 184 const ScopedObjectAccess& soa) 185 REQUIRES_SHARED(Locks::mutator_lock_); 186 187 GetThreadCount()188 size_t GetThreadCount() const { 189 return parallel_thread_count_; 190 } 191 SetDedupeEnabled(bool dedupe_enabled)192 void SetDedupeEnabled(bool dedupe_enabled) { 193 compiled_method_storage_.SetDedupeEnabled(dedupe_enabled); 194 } 195 DedupeEnabled()196 bool DedupeEnabled() const { 197 return compiled_method_storage_.DedupeEnabled(); 198 } 199 200 // Checks whether profile guided verification is enabled and if the method should be verified 201 // according to the profile file. 202 bool ShouldVerifyClassBasedOnProfile(const DexFile& dex_file, uint16_t class_idx) const; 203 204 void RecordClassStatus(const ClassReference& ref, ClassStatus status); 205 206 // Get memory usage during compilation. 207 std::string GetMemoryUsageString(bool extended) const; 208 SetHadHardVerifierFailure()209 void SetHadHardVerifierFailure() { 210 had_hard_verifier_failure_ = true; 211 } AddSoftVerifierFailure()212 void AddSoftVerifierFailure() { 213 number_of_soft_verifier_failures_++; 214 } 215 GetCompiledMethodStorage()216 CompiledMethodStorage* GetCompiledMethodStorage() { 217 return &compiled_method_storage_; 218 } 219 GetCompiledMethodStorage()220 const CompiledMethodStorage* GetCompiledMethodStorage() const { 221 return &compiled_method_storage_; 222 } 223 224 private: 225 void LoadImageClasses(TimingLogger* timings, 226 jobject class_loader, 227 /*inout*/ HashSet<std::string>* image_classes) 228 REQUIRES(!Locks::mutator_lock_); 229 230 // Attempt to resolve all type, methods, fields, and strings 231 // referenced from code in the dex file following PathClassLoader 232 // ordering semantics. 233 void Resolve(jobject class_loader, 234 const std::vector<const DexFile*>& dex_files, 235 TimingLogger* timings) 236 REQUIRES(!Locks::mutator_lock_); 237 void ResolveDexFile(jobject class_loader, 238 const DexFile& dex_file, 239 ThreadPool* thread_pool, 240 size_t thread_count, 241 TimingLogger* timings) 242 REQUIRES(!Locks::mutator_lock_); 243 244 // Do fast verification through VerifierDeps if possible. Return whether 245 // verification was successful. 246 bool FastVerify(jobject class_loader, 247 const std::vector<const DexFile*>& dex_files, 248 TimingLogger* timings); 249 250 void Verify(jobject class_loader, 251 const std::vector<const DexFile*>& dex_files, 252 TimingLogger* timings); 253 254 void VerifyDexFile(jobject class_loader, 255 const DexFile& dex_file, 256 ThreadPool* thread_pool, 257 size_t thread_count, 258 TimingLogger* timings) 259 REQUIRES(!Locks::mutator_lock_); 260 261 void SetVerified(jobject class_loader, 262 const std::vector<const DexFile*>& dex_files, 263 TimingLogger* timings); 264 void SetVerifiedDexFile(jobject class_loader, 265 const DexFile& dex_file, 266 ThreadPool* thread_pool, 267 size_t thread_count, 268 TimingLogger* timings) 269 REQUIRES(!Locks::mutator_lock_); 270 271 void InitializeClasses(jobject class_loader, 272 const std::vector<const DexFile*>& dex_files, 273 TimingLogger* timings) 274 REQUIRES(!Locks::mutator_lock_); 275 void InitializeClasses(jobject class_loader, 276 const DexFile& dex_file, 277 TimingLogger* timings) 278 REQUIRES(!Locks::mutator_lock_); 279 280 void UpdateImageClasses(TimingLogger* timings, /*inout*/ HashSet<std::string>* image_classes) 281 REQUIRES(!Locks::mutator_lock_); 282 283 void Compile(jobject class_loader, 284 const std::vector<const DexFile*>& dex_files, 285 TimingLogger* timings); 286 287 void CheckThreadPools(); 288 289 // Resolve const string literals that are loaded from dex code. If only_startup_strings is 290 // specified, only methods that are marked startup in the profile are resolved. 291 void ResolveConstStrings(const std::vector<const DexFile*>& dex_files, 292 bool only_startup_strings, 293 /*inout*/ TimingLogger* timings); 294 295 const CompilerOptions* const compiler_options_; 296 const VerificationResults* const verification_results_; 297 298 std::unique_ptr<Compiler> compiler_; 299 300 // All class references that this compiler has compiled. Indexed by class defs. 301 using ClassStateTable = AtomicDexRefMap<ClassReference, ClassStatus>; 302 ClassStateTable compiled_classes_; 303 // All class references that are in the classpath. Indexed by class defs. 304 ClassStateTable classpath_classes_; 305 306 using MethodTable = AtomicDexRefMap<MethodReference, CompiledMethod*>; 307 308 // All method references that this compiler has compiled. 309 MethodTable compiled_methods_; 310 311 std::atomic<uint32_t> number_of_soft_verifier_failures_; 312 313 bool had_hard_verifier_failure_; 314 315 // A thread pool that can (potentially) run tasks in parallel. 316 size_t parallel_thread_count_; 317 std::unique_ptr<ThreadPool> parallel_thread_pool_; 318 319 // A thread pool that guarantees running single-threaded on the main thread. 320 std::unique_ptr<ThreadPool> single_thread_pool_; 321 322 class AOTCompilationStats; 323 std::unique_ptr<AOTCompilationStats> stats_; 324 325 CompiledMethodStorage compiled_method_storage_; 326 327 size_t max_arena_alloc_; 328 329 friend class CommonCompilerDriverTest; 330 friend class CompileClassVisitor; 331 friend class InitializeClassVisitor; 332 friend class verifier::VerifierDepsTest; 333 DISALLOW_COPY_AND_ASSIGN(CompilerDriver); 334 }; 335 336 } // namespace art 337 338 #endif // ART_DEX2OAT_DRIVER_COMPILER_DRIVER_H_ 339