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