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