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