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_COMPILER_DRIVER_COMPILER_DRIVER_H_
18 #define ART_COMPILER_DRIVER_COMPILER_DRIVER_H_
19 
20 #include <set>
21 #include <string>
22 #include <vector>
23 
24 #include "base/mutex.h"
25 #include "base/timing_logger.h"
26 #include "class_reference.h"
27 #include "compiled_method.h"
28 #include "compiler.h"
29 #include "dex_file.h"
30 #include "driver/compiler_options.h"
31 #include "instruction_set.h"
32 #include "invoke_type.h"
33 #include "method_reference.h"
34 #include "mirror/class.h"  // For mirror::Class::Status.
35 #include "os.h"
36 #include "profiler.h"
37 #include "runtime.h"
38 #include "safe_map.h"
39 #include "thread_pool.h"
40 #include "utils/arena_allocator.h"
41 #include "utils/dedupe_set.h"
42 #include "utils/swap_space.h"
43 #include "dex/verified_method.h"
44 
45 namespace art {
46 
47 namespace verifier {
48 class MethodVerifier;
49 }  // namespace verifier
50 
51 class CompiledClass;
52 class CompilerOptions;
53 class DexCompilationUnit;
54 class DexFileToMethodInlinerMap;
55 struct InlineIGetIPutData;
56 class OatWriter;
57 class ParallelCompilationManager;
58 class ScopedObjectAccess;
59 template<class T> class Handle;
60 class TimingLogger;
61 class VerificationResults;
62 class VerifiedMethod;
63 
64 enum EntryPointCallingConvention {
65   // ABI of invocations to a method's interpreter entry point.
66   kInterpreterAbi,
67   // ABI of calls to a method's native code, only used for native methods.
68   kJniAbi,
69   // ABI of calls to a method's portable code entry point.
70   kPortableAbi,
71   // ABI of calls to a method's quick code entry point.
72   kQuickAbi
73 };
74 
75 enum DexToDexCompilationLevel {
76   kDontDexToDexCompile,   // Only meaning wrt image time interpretation.
77   kRequired,              // Dex-to-dex compilation required for correctness.
78   kOptimize               // Perform required transformation and peep-hole optimizations.
79 };
80 
81 // Thread-local storage compiler worker threads
82 class CompilerTls {
83   public:
CompilerTls()84     CompilerTls() : llvm_info_(nullptr) {}
~CompilerTls()85     ~CompilerTls() {}
86 
GetLLVMInfo()87     void* GetLLVMInfo() { return llvm_info_; }
88 
SetLLVMInfo(void * llvm_info)89     void SetLLVMInfo(void* llvm_info) { llvm_info_ = llvm_info; }
90 
91   private:
92     void* llvm_info_;
93 };
94 
95 static constexpr bool kUseMurmur3Hash = true;
96 
97 class CompilerDriver {
98  public:
99   // Create a compiler targeting the requested "instruction_set".
100   // "image" should be true if image specific optimizations should be
101   // enabled.  "image_classes" lets the compiler know what classes it
102   // can assume will be in the image, with nullptr implying all available
103   // classes.
104   explicit CompilerDriver(const CompilerOptions* compiler_options,
105                           VerificationResults* verification_results,
106                           DexFileToMethodInlinerMap* method_inliner_map,
107                           Compiler::Kind compiler_kind,
108                           InstructionSet instruction_set,
109                           InstructionSetFeatures instruction_set_features,
110                           bool image, std::set<std::string>* image_classes,
111                           std::set<std::string>* compiled_classes,
112                           size_t thread_count, bool dump_stats, bool dump_passes,
113                           CumulativeLogger* timer, int swap_fd = -1,
114                           std::string profile_file = "");
115 
116   ~CompilerDriver();
117 
118   void CompileAll(jobject class_loader, const std::vector<const DexFile*>& dex_files,
119                   TimingLogger* timings)
120       LOCKS_EXCLUDED(Locks::mutator_lock_);
121 
122   // Compile a single Method.
123   void CompileOne(mirror::ArtMethod* method, TimingLogger* timings)
124       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
125 
GetVerificationResults()126   VerificationResults* GetVerificationResults() const {
127     return verification_results_;
128   }
129 
GetMethodInlinerMap()130   DexFileToMethodInlinerMap* GetMethodInlinerMap() const {
131     return method_inliner_map_;
132   }
133 
GetInstructionSet()134   InstructionSet GetInstructionSet() const {
135     return instruction_set_;
136   }
137 
GetInstructionSetFeatures()138   InstructionSetFeatures GetInstructionSetFeatures() const {
139     return instruction_set_features_;
140   }
141 
GetCompilerOptions()142   const CompilerOptions& GetCompilerOptions() const {
143     return *compiler_options_;
144   }
145 
GetCompiler()146   Compiler* GetCompiler() const {
147     return compiler_.get();
148   }
149 
ProfilePresent()150   bool ProfilePresent() const {
151     return profile_present_;
152   }
153 
154   // Are we compiling and creating an image file?
IsImage()155   bool IsImage() const {
156     return image_;
157   }
158 
GetImageClasses()159   const std::set<std::string>* GetImageClasses() const {
160     return image_classes_.get();
161   }
162 
163   CompilerTls* GetTls();
164 
165   // Generate the trampolines that are invoked by unresolved direct methods.
166   const std::vector<uint8_t>* CreateInterpreterToInterpreterBridge() const
167       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
168   const std::vector<uint8_t>* CreateInterpreterToCompiledCodeBridge() const
169       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
170   const std::vector<uint8_t>* CreateJniDlsymLookup() const
171       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
172   const std::vector<uint8_t>* CreatePortableImtConflictTrampoline() const
173       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
174   const std::vector<uint8_t>* CreatePortableResolutionTrampoline() const
175       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
176   const std::vector<uint8_t>* CreatePortableToInterpreterBridge() const
177       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
178   const std::vector<uint8_t>* CreateQuickGenericJniTrampoline() const
179       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
180   const std::vector<uint8_t>* CreateQuickImtConflictTrampoline() const
181       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
182   const std::vector<uint8_t>* CreateQuickResolutionTrampoline() const
183       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
184   const std::vector<uint8_t>* CreateQuickToInterpreterBridge() const
185       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
186 
187   CompiledClass* GetCompiledClass(ClassReference ref) const
188       LOCKS_EXCLUDED(compiled_classes_lock_);
189 
190   CompiledMethod* GetCompiledMethod(MethodReference ref) const
191       LOCKS_EXCLUDED(compiled_methods_lock_);
192 
193   void AddRequiresConstructorBarrier(Thread* self, const DexFile* dex_file,
194                                      uint16_t class_def_index);
195   bool RequiresConstructorBarrier(Thread* self, const DexFile* dex_file, uint16_t class_def_index);
196 
197   // Callbacks from compiler to see what runtime checks must be generated.
198 
199   bool CanAssumeTypeIsPresentInDexCache(const DexFile& dex_file, uint32_t type_idx);
200 
201   bool CanAssumeStringIsPresentInDexCache(const DexFile& dex_file, uint32_t string_idx)
202       LOCKS_EXCLUDED(Locks::mutator_lock_);
203 
204   // Are runtime access checks necessary in the compiled code?
205   bool CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file,
206                                   uint32_t type_idx, bool* type_known_final = nullptr,
207                                   bool* type_known_abstract = nullptr,
208                                   bool* equals_referrers_class = nullptr)
209       LOCKS_EXCLUDED(Locks::mutator_lock_);
210 
211   // Are runtime access and instantiable checks necessary in the code?
212   bool CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file,
213                                               uint32_t type_idx)
214      LOCKS_EXCLUDED(Locks::mutator_lock_);
215 
216   bool CanEmbedTypeInCode(const DexFile& dex_file, uint32_t type_idx,
217                           bool* is_type_initialized, bool* use_direct_type_ptr,
218                           uintptr_t* direct_type_ptr, bool* out_is_finalizable);
219 
220   bool CanEmbedStringInCode(const DexFile& dex_file, uint32_t string_idx,
221                             bool* use_direct_type_ptr, uintptr_t* direct_type_ptr);
222 
223   // Get the DexCache for the
224   mirror::DexCache* GetDexCache(const DexCompilationUnit* mUnit)
225     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
226 
227   mirror::ClassLoader* GetClassLoader(ScopedObjectAccess& soa, const DexCompilationUnit* mUnit)
228     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
229 
230   // Resolve compiling method's class. Returns nullptr on failure.
231   mirror::Class* ResolveCompilingMethodsClass(
232       const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
233       Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit)
234     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
235 
236   // Resolve a field. Returns nullptr on failure, including incompatible class change.
237   // NOTE: Unlike ClassLinker's ResolveField(), this method enforces is_static.
238   mirror::ArtField* ResolveField(
239       const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
240       Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
241       uint32_t field_idx, bool is_static)
242     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
243 
244   // Get declaration location of a resolved field.
245   void GetResolvedFieldDexFileLocation(
246       mirror::ArtField* resolved_field, const DexFile** declaring_dex_file,
247       uint16_t* declaring_class_idx, uint16_t* declaring_field_idx)
248     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
249 
250   bool IsFieldVolatile(mirror::ArtField* field) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
251 
252   // Can we fast-path an IGET/IPUT access to an instance field? If yes, compute the field offset.
253   std::pair<bool, bool> IsFastInstanceField(
254       mirror::DexCache* dex_cache, mirror::Class* referrer_class,
255       mirror::ArtField* resolved_field, uint16_t field_idx)
256     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
257 
258   // Can we fast-path an SGET/SPUT access to a static field? If yes, compute the field offset,
259   // the type index of the declaring class in the referrer's dex file and whether the declaring
260   // class is the referrer's class or at least can be assumed to be initialized.
261   std::pair<bool, bool> IsFastStaticField(
262       mirror::DexCache* dex_cache, mirror::Class* referrer_class,
263       mirror::ArtField* resolved_field, uint16_t field_idx, MemberOffset* field_offset,
264       uint32_t* storage_index, bool* is_referrers_class, bool* is_initialized)
265     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
266 
267   // Resolve a method. Returns nullptr on failure, including incompatible class change.
268   mirror::ArtMethod* ResolveMethod(
269       ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
270       Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
271       uint32_t method_idx, InvokeType invoke_type)
272     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
273 
274   // Get declaration location of a resolved field.
275   void GetResolvedMethodDexFileLocation(
276       mirror::ArtMethod* resolved_method, const DexFile** declaring_dex_file,
277       uint16_t* declaring_class_idx, uint16_t* declaring_method_idx)
278     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
279 
280   // Get declaration location of a resolved field.
281   uint16_t GetResolvedMethodVTableIndex(
282       mirror::ArtMethod* resolved_method, InvokeType type)
283     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
284 
285   // Can we fast-path an INVOKE? If no, returns 0. If yes, returns a non-zero opaque flags value
286   // for ProcessedInvoke() and computes the necessary lowering info.
287   int IsFastInvoke(
288       ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
289       Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
290       mirror::Class* referrer_class, mirror::ArtMethod* resolved_method, InvokeType* invoke_type,
291       MethodReference* target_method, const MethodReference* devirt_target,
292       uintptr_t* direct_code, uintptr_t* direct_method)
293     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
294 
295   // Does invokation of the resolved method need class initialization?
296   bool NeedsClassInitialization(mirror::Class* referrer_class, mirror::ArtMethod* resolved_method)
297     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
298 
299   void ProcessedInstanceField(bool resolved);
300   void ProcessedStaticField(bool resolved, bool local);
301   void ProcessedInvoke(InvokeType invoke_type, int flags);
302 
303   // Can we fast path instance field access? Computes field's offset and volatility.
304   bool ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put,
305                                 MemberOffset* field_offset, bool* is_volatile)
306       LOCKS_EXCLUDED(Locks::mutator_lock_);
307 
308   mirror::ArtField* ComputeInstanceFieldInfo(uint32_t field_idx,
309                                              const DexCompilationUnit* mUnit,
310                                              bool is_put,
311                                              const ScopedObjectAccess& soa)
312       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
313 
314 
315   // Can we fastpath static field access? Computes field's offset, volatility and whether the
316   // field is within the referrer (which can avoid checking class initialization).
317   bool ComputeStaticFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put,
318                               MemberOffset* field_offset, uint32_t* storage_index,
319                               bool* is_referrers_class, bool* is_volatile, bool* is_initialized)
320       LOCKS_EXCLUDED(Locks::mutator_lock_);
321 
322   // Can we fastpath a interface, super class or virtual method call? Computes method's vtable
323   // index.
324   bool ComputeInvokeInfo(const DexCompilationUnit* mUnit, const uint32_t dex_pc,
325                          bool update_stats, bool enable_devirtualization,
326                          InvokeType* type, MethodReference* target_method, int* vtable_idx,
327                          uintptr_t* direct_code, uintptr_t* direct_method)
328       LOCKS_EXCLUDED(Locks::mutator_lock_);
329 
330   const VerifiedMethod* GetVerifiedMethod(const DexFile* dex_file, uint32_t method_idx) const;
331   bool IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc);
332 
333   // Record patch information for later fix up.
334   void AddCodePatch(const DexFile* dex_file,
335                     uint16_t referrer_class_def_idx,
336                     uint32_t referrer_method_idx,
337                     InvokeType referrer_invoke_type,
338                     uint32_t target_method_idx,
339                     const DexFile* target_dex_file,
340                     InvokeType target_invoke_type,
341                     size_t literal_offset)
342       LOCKS_EXCLUDED(compiled_methods_lock_);
343   void AddRelativeCodePatch(const DexFile* dex_file,
344                             uint16_t referrer_class_def_idx,
345                             uint32_t referrer_method_idx,
346                             InvokeType referrer_invoke_type,
347                             uint32_t target_method_idx,
348                             const DexFile* target_dex_file,
349                             InvokeType target_invoke_type,
350                             size_t literal_offset,
351                             int32_t pc_relative_offset)
352       LOCKS_EXCLUDED(compiled_methods_lock_);
353   void AddMethodPatch(const DexFile* dex_file,
354                       uint16_t referrer_class_def_idx,
355                       uint32_t referrer_method_idx,
356                       InvokeType referrer_invoke_type,
357                       uint32_t target_method_idx,
358                       const DexFile* target_dex_file,
359                       InvokeType target_invoke_type,
360                       size_t literal_offset)
361       LOCKS_EXCLUDED(compiled_methods_lock_);
362   void AddClassPatch(const DexFile* dex_file,
363                      uint16_t referrer_class_def_idx,
364                      uint32_t referrer_method_idx,
365                      uint32_t target_method_idx,
366                      size_t literal_offset)
367       LOCKS_EXCLUDED(compiled_methods_lock_);
368   void AddStringPatch(const DexFile* dex_file,
369                       uint16_t referrer_class_def_idx,
370                       uint32_t referrer_method_idx,
371                       uint32_t string_idx,
372                       size_t literal_offset)
373       LOCKS_EXCLUDED(compiled_methods_lock_);
374 
GetSupportBootImageFixup()375   bool GetSupportBootImageFixup() const {
376     return support_boot_image_fixup_;
377   }
378 
SetSupportBootImageFixup(bool support_boot_image_fixup)379   void SetSupportBootImageFixup(bool support_boot_image_fixup) {
380     support_boot_image_fixup_ = support_boot_image_fixup;
381   }
382 
GetArenaPool()383   ArenaPool* GetArenaPool() {
384     return &arena_pool_;
385   }
GetArenaPool()386   const ArenaPool* GetArenaPool() const {
387     return &arena_pool_;
388   }
GetSwapSpaceAllocator()389   SwapAllocator<void>& GetSwapSpaceAllocator() {
390     return *swap_space_allocator_.get();
391   }
392 
393   bool WriteElf(const std::string& android_root,
394                 bool is_host,
395                 const std::vector<const DexFile*>& dex_files,
396                 OatWriter* oat_writer,
397                 File* file);
398 
399   // TODO: move to a common home for llvm helpers once quick/portable are merged.
400   static void InstructionSetToLLVMTarget(InstructionSet instruction_set,
401                                          std::string* target_triple,
402                                          std::string* target_cpu,
403                                          std::string* target_attr);
404 
SetCompilerContext(void * compiler_context)405   void SetCompilerContext(void* compiler_context) {
406     compiler_context_ = compiler_context;
407   }
408 
GetCompilerContext()409   void* GetCompilerContext() const {
410     return compiler_context_;
411   }
412 
GetThreadCount()413   size_t GetThreadCount() const {
414     return thread_count_;
415   }
416 
417   class CallPatchInformation;
418   class TypePatchInformation;
419 
GetDumpPasses()420   bool GetDumpPasses() const {
421     return dump_passes_;
422   }
423 
DidIncludeDebugSymbols()424   bool DidIncludeDebugSymbols() const {
425     return compiler_options_->GetIncludeDebugSymbols();
426   }
427 
GetTimingsLogger()428   CumulativeLogger* GetTimingsLogger() const {
429     return timings_logger_;
430   }
431 
432   class PatchInformation {
433    public:
GetDexFile()434     const DexFile& GetDexFile() const {
435       return *dex_file_;
436     }
GetReferrerClassDefIdx()437     uint16_t GetReferrerClassDefIdx() const {
438       return referrer_class_def_idx_;
439     }
GetReferrerMethodIdx()440     uint32_t GetReferrerMethodIdx() const {
441       return referrer_method_idx_;
442     }
GetLiteralOffset()443     size_t GetLiteralOffset() const {
444       return literal_offset_;
445     }
446 
IsCall()447     virtual bool IsCall() const {
448       return false;
449     }
IsType()450     virtual bool IsType() const {
451       return false;
452     }
AsCall()453     virtual const CallPatchInformation* AsCall() const {
454       LOG(FATAL) << "Unreachable";
455       return nullptr;
456     }
AsType()457     virtual const TypePatchInformation* AsType() const {
458       LOG(FATAL) << "Unreachable";
459       return nullptr;
460     }
461 
462    protected:
PatchInformation(const DexFile * dex_file,uint16_t referrer_class_def_idx,uint32_t referrer_method_idx,size_t literal_offset)463     PatchInformation(const DexFile* dex_file,
464                      uint16_t referrer_class_def_idx,
465                      uint32_t referrer_method_idx,
466                      size_t literal_offset)
467       : dex_file_(dex_file),
468         referrer_class_def_idx_(referrer_class_def_idx),
469         referrer_method_idx_(referrer_method_idx),
470         literal_offset_(literal_offset) {
471       CHECK(dex_file_ != nullptr);
472     }
~PatchInformation()473     virtual ~PatchInformation() {}
474 
475     const DexFile* const dex_file_;
476     const uint16_t referrer_class_def_idx_;
477     const uint32_t referrer_method_idx_;
478     const size_t literal_offset_;
479 
480     friend class CompilerDriver;
481   };
482 
483   class CallPatchInformation : public PatchInformation {
484    public:
GetReferrerInvokeType()485     InvokeType GetReferrerInvokeType() const {
486       return referrer_invoke_type_;
487     }
GetTargetMethodIdx()488     uint32_t GetTargetMethodIdx() const {
489       return target_method_idx_;
490     }
GetTargetDexFile()491     const DexFile* GetTargetDexFile() const {
492       return target_dex_file_;
493     }
GetTargetInvokeType()494     InvokeType GetTargetInvokeType() const {
495       return target_invoke_type_;
496     }
497 
AsCall()498     const CallPatchInformation* AsCall() const {
499       return this;
500     }
IsCall()501     bool IsCall() const {
502       return true;
503     }
IsRelative()504     virtual bool IsRelative() const {
505       return false;
506     }
RelativeOffset()507     virtual int RelativeOffset() const {
508       return 0;
509     }
510 
511    protected:
CallPatchInformation(const DexFile * dex_file,uint16_t referrer_class_def_idx,uint32_t referrer_method_idx,InvokeType referrer_invoke_type,uint32_t target_method_idx,const DexFile * target_dex_file,InvokeType target_invoke_type,size_t literal_offset)512     CallPatchInformation(const DexFile* dex_file,
513                          uint16_t referrer_class_def_idx,
514                          uint32_t referrer_method_idx,
515                          InvokeType referrer_invoke_type,
516                          uint32_t target_method_idx,
517                          const DexFile* target_dex_file,
518                          InvokeType target_invoke_type,
519                          size_t literal_offset)
520         : PatchInformation(dex_file, referrer_class_def_idx,
521                            referrer_method_idx, literal_offset),
522           referrer_invoke_type_(referrer_invoke_type),
523           target_method_idx_(target_method_idx),
524           target_dex_file_(target_dex_file),
525           target_invoke_type_(target_invoke_type) {
526     }
527 
528    private:
529     const InvokeType referrer_invoke_type_;
530     const uint32_t target_method_idx_;
531     const DexFile* target_dex_file_;
532     const InvokeType target_invoke_type_;
533 
534     friend class CompilerDriver;
535     DISALLOW_COPY_AND_ASSIGN(CallPatchInformation);
536   };
537 
538   class RelativeCallPatchInformation : public CallPatchInformation {
539    public:
IsRelative()540     bool IsRelative() const {
541       return true;
542     }
RelativeOffset()543     int RelativeOffset() const {
544       return offset_;
545     }
546 
547    private:
RelativeCallPatchInformation(const DexFile * dex_file,uint16_t referrer_class_def_idx,uint32_t referrer_method_idx,InvokeType referrer_invoke_type,uint32_t target_method_idx,const DexFile * target_dex_file,InvokeType target_invoke_type,size_t literal_offset,int32_t pc_relative_offset)548     RelativeCallPatchInformation(const DexFile* dex_file,
549                                  uint16_t referrer_class_def_idx,
550                                  uint32_t referrer_method_idx,
551                                  InvokeType referrer_invoke_type,
552                                  uint32_t target_method_idx,
553                                  const DexFile* target_dex_file,
554                                  InvokeType target_invoke_type,
555                                  size_t literal_offset,
556                                  int32_t pc_relative_offset)
557         : CallPatchInformation(dex_file, referrer_class_def_idx,
558                            referrer_method_idx, referrer_invoke_type, target_method_idx,
559                            target_dex_file, target_invoke_type, literal_offset),
560           offset_(pc_relative_offset) {
561     }
562 
563     const int offset_;
564 
565     friend class CompilerDriver;
566     DISALLOW_COPY_AND_ASSIGN(RelativeCallPatchInformation);
567   };
568 
569   class TypePatchInformation : public PatchInformation {
570    public:
GetTargetTypeIdx()571     uint32_t GetTargetTypeIdx() const {
572       return target_type_idx_;
573     }
574 
IsType()575     bool IsType() const {
576       return true;
577     }
AsType()578     const TypePatchInformation* AsType() const {
579       return this;
580     }
581 
582    private:
TypePatchInformation(const DexFile * dex_file,uint16_t referrer_class_def_idx,uint32_t referrer_method_idx,uint32_t target_type_idx,size_t literal_offset)583     TypePatchInformation(const DexFile* dex_file,
584                          uint16_t referrer_class_def_idx,
585                          uint32_t referrer_method_idx,
586                          uint32_t target_type_idx,
587                          size_t literal_offset)
588         : PatchInformation(dex_file, referrer_class_def_idx,
589                            referrer_method_idx, literal_offset),
590           target_type_idx_(target_type_idx) {
591     }
592 
593     const uint32_t target_type_idx_;
594 
595     friend class CompilerDriver;
596     DISALLOW_COPY_AND_ASSIGN(TypePatchInformation);
597   };
598 
599   class StringPatchInformation : public PatchInformation {
600    public:
GetStringIdx()601     uint32_t GetStringIdx() const {
602       return string_idx_;
603     }
604 
IsType()605     bool IsType() const {
606       return false;
607     }
AsType()608     const TypePatchInformation* AsType() const {
609       return nullptr;
610     }
611 
612    private:
StringPatchInformation(const DexFile * dex_file,uint16_t referrer_class_def_idx,uint32_t referrer_method_idx,uint32_t string_idx,size_t literal_offset)613     StringPatchInformation(const DexFile* dex_file,
614                            uint16_t referrer_class_def_idx,
615                            uint32_t referrer_method_idx,
616                            uint32_t string_idx,
617                            size_t literal_offset)
618         : PatchInformation(dex_file, referrer_class_def_idx, referrer_method_idx, literal_offset),
619           string_idx_(string_idx) {
620     }
621 
622     const uint32_t string_idx_;
623 
624     friend class CompilerDriver;
625     DISALLOW_COPY_AND_ASSIGN(StringPatchInformation);
626   };
627 
GetCodeToPatch()628   const std::vector<const CallPatchInformation*>& GetCodeToPatch() const {
629     return code_to_patch_;
630   }
GetMethodsToPatch()631   const std::vector<const CallPatchInformation*>& GetMethodsToPatch() const {
632     return methods_to_patch_;
633   }
GetClassesToPatch()634   const std::vector<const TypePatchInformation*>& GetClassesToPatch() const {
635     return classes_to_patch_;
636   }
GetStringsToPatch()637   const std::vector<const StringPatchInformation*>& GetStringsToPatch() const {
638     return strings_to_patch_;
639   }
640 
641   // Checks if class specified by type_idx is one of the image_classes_
642   bool IsImageClass(const char* descriptor) const;
643 
644   // Checks if the provided class should be compiled, i.e., is in classes_to_compile_.
645   bool IsClassToCompile(const char* descriptor) const;
646 
647   void RecordClassStatus(ClassReference ref, mirror::Class::Status status)
648       LOCKS_EXCLUDED(compiled_classes_lock_);
649 
650   SwapVector<uint8_t>* DeduplicateCode(const ArrayRef<const uint8_t>& code);
651   SwapVector<uint8_t>* DeduplicateMappingTable(const ArrayRef<const uint8_t>& code);
652   SwapVector<uint8_t>* DeduplicateVMapTable(const ArrayRef<const uint8_t>& code);
653   SwapVector<uint8_t>* DeduplicateGCMap(const ArrayRef<const uint8_t>& code);
654   SwapVector<uint8_t>* DeduplicateCFIInfo(const ArrayRef<const uint8_t>& cfi_info);
655 
656   /*
657    * @brief return the pointer to the Call Frame Information.
658    * @return pointer to call frame information for this compilation.
659    */
GetCallFrameInformation()660   std::vector<uint8_t>* GetCallFrameInformation() const {
661     return cfi_info_.get();
662   }
663 
664   // Should the compiler run on this method given profile information?
665   bool SkipCompilation(const std::string& method_name);
666 
667   // Get memory usage during compilation.
668   std::string GetMemoryUsageString(bool extended) const;
669 
670  private:
671   // These flags are internal to CompilerDriver for collecting INVOKE resolution statistics.
672   // The only external contract is that unresolved method has flags 0 and resolved non-0.
673   enum {
674     kBitMethodResolved = 0,
675     kBitVirtualMadeDirect,
676     kBitPreciseTypeDevirtualization,
677     kBitDirectCallToBoot,
678     kBitDirectMethodToBoot
679   };
680   static constexpr int kFlagMethodResolved              = 1 << kBitMethodResolved;
681   static constexpr int kFlagVirtualMadeDirect           = 1 << kBitVirtualMadeDirect;
682   static constexpr int kFlagPreciseTypeDevirtualization = 1 << kBitPreciseTypeDevirtualization;
683   static constexpr int kFlagDirectCallToBoot            = 1 << kBitDirectCallToBoot;
684   static constexpr int kFlagDirectMethodToBoot          = 1 << kBitDirectMethodToBoot;
685   static constexpr int kFlagsMethodResolvedVirtualMadeDirect =
686       kFlagMethodResolved | kFlagVirtualMadeDirect;
687   static constexpr int kFlagsMethodResolvedPreciseTypeDevirtualization =
688       kFlagsMethodResolvedVirtualMadeDirect | kFlagPreciseTypeDevirtualization;
689 
690  public:  // TODO make private or eliminate.
691   // Compute constant code and method pointers when possible.
692   void GetCodeAndMethodForDirectCall(/*out*/InvokeType* type,
693                                      InvokeType sharp_type,
694                                      bool no_guarantee_of_dex_cache_entry,
695                                      const mirror::Class* referrer_class,
696                                      mirror::ArtMethod* method,
697                                      /*out*/int* stats_flags,
698                                      MethodReference* target_method,
699                                      uintptr_t* direct_code, uintptr_t* direct_method)
700       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
701 
702  private:
703   void PreCompile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
704                   ThreadPool* thread_pool, TimingLogger* timings)
705       LOCKS_EXCLUDED(Locks::mutator_lock_);
706 
707   void LoadImageClasses(TimingLogger* timings);
708 
709   // Attempt to resolve all type, methods, fields, and strings
710   // referenced from code in the dex file following PathClassLoader
711   // ordering semantics.
712   void Resolve(jobject class_loader, const std::vector<const DexFile*>& dex_files,
713                ThreadPool* thread_pool, TimingLogger* timings)
714       LOCKS_EXCLUDED(Locks::mutator_lock_);
715   void ResolveDexFile(jobject class_loader, const DexFile& dex_file,
716                       const std::vector<const DexFile*>& dex_files,
717                       ThreadPool* thread_pool, TimingLogger* timings)
718       LOCKS_EXCLUDED(Locks::mutator_lock_);
719 
720   void Verify(jobject class_loader, const std::vector<const DexFile*>& dex_files,
721               ThreadPool* thread_pool, TimingLogger* timings);
722   void VerifyDexFile(jobject class_loader, const DexFile& dex_file,
723                      const std::vector<const DexFile*>& dex_files,
724                      ThreadPool* thread_pool, TimingLogger* timings)
725       LOCKS_EXCLUDED(Locks::mutator_lock_);
726 
727   void SetVerified(jobject class_loader, const std::vector<const DexFile*>& dex_files,
728                    ThreadPool* thread_pool, TimingLogger* timings);
729   void SetVerifiedDexFile(jobject class_loader, const DexFile& dex_file,
730                           const std::vector<const DexFile*>& dex_files,
731                           ThreadPool* thread_pool, TimingLogger* timings)
732       LOCKS_EXCLUDED(Locks::mutator_lock_);
733 
734   void InitializeClasses(jobject class_loader, const std::vector<const DexFile*>& dex_files,
735                          ThreadPool* thread_pool, TimingLogger* timings)
736       LOCKS_EXCLUDED(Locks::mutator_lock_);
737   void InitializeClasses(jobject class_loader, const DexFile& dex_file,
738                          const std::vector<const DexFile*>& dex_files,
739                          ThreadPool* thread_pool, TimingLogger* timings)
740       LOCKS_EXCLUDED(Locks::mutator_lock_, compiled_classes_lock_);
741 
742   void UpdateImageClasses(TimingLogger* timings) LOCKS_EXCLUDED(Locks::mutator_lock_);
743   static void FindClinitImageClassesCallback(mirror::Object* object, void* arg)
744       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
745 
746   void Compile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
747                ThreadPool* thread_pool, TimingLogger* timings);
748   void CompileDexFile(jobject class_loader, const DexFile& dex_file,
749                       const std::vector<const DexFile*>& dex_files,
750                       ThreadPool* thread_pool, TimingLogger* timings)
751       LOCKS_EXCLUDED(Locks::mutator_lock_);
752   void CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
753                      InvokeType invoke_type, uint16_t class_def_idx, uint32_t method_idx,
754                      jobject class_loader, const DexFile& dex_file,
755                      DexToDexCompilationLevel dex_to_dex_compilation_level,
756                      bool compilation_enabled)
757       LOCKS_EXCLUDED(compiled_methods_lock_);
758 
759   static void CompileClass(const ParallelCompilationManager* context, size_t class_def_index)
760       LOCKS_EXCLUDED(Locks::mutator_lock_);
761 
762   // Swap pool and allocator used for native allocations. May be file-backed. Needs to be first
763   // as other fields rely on this.
764   std::unique_ptr<SwapSpace> swap_space_;
765   std::unique_ptr<SwapAllocator<void> > swap_space_allocator_;
766 
767   ProfileFile profile_file_;
768   bool profile_present_;
769 
770   std::vector<const CallPatchInformation*> code_to_patch_;
771   std::vector<const CallPatchInformation*> methods_to_patch_;
772   std::vector<const TypePatchInformation*> classes_to_patch_;
773   std::vector<const StringPatchInformation*> strings_to_patch_;
774 
775   const CompilerOptions* const compiler_options_;
776   VerificationResults* const verification_results_;
777   DexFileToMethodInlinerMap* const method_inliner_map_;
778 
779   std::unique_ptr<Compiler> compiler_;
780 
781   const InstructionSet instruction_set_;
782   const InstructionSetFeatures instruction_set_features_;
783 
784   // All class references that require
785   mutable ReaderWriterMutex freezing_constructor_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
786   std::set<ClassReference> freezing_constructor_classes_ GUARDED_BY(freezing_constructor_lock_);
787 
788   typedef SafeMap<const ClassReference, CompiledClass*> ClassTable;
789   // All class references that this compiler has compiled.
790   mutable Mutex compiled_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
791   ClassTable compiled_classes_ GUARDED_BY(compiled_classes_lock_);
792 
793   typedef SafeMap<const MethodReference, CompiledMethod*, MethodReferenceComparator> MethodTable;
794   // All method references that this compiler has compiled.
795   mutable Mutex compiled_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
796   MethodTable compiled_methods_ GUARDED_BY(compiled_methods_lock_);
797 
798   const bool image_;
799 
800   // If image_ is true, specifies the classes that will be included in
801   // the image. Note if image_classes_ is nullptr, all classes are
802   // included in the image.
803   std::unique_ptr<std::set<std::string>> image_classes_;
804 
805   // If image_ is true, specifies the classes that will be compiled in
806   // the image. Note if classes_to_compile_ is nullptr, all classes are
807   // included in the image.
808   std::unique_ptr<std::set<std::string>> classes_to_compile_;
809 
810   size_t thread_count_;
811   uint64_t start_ns_;
812 
813   class AOTCompilationStats;
814   std::unique_ptr<AOTCompilationStats> stats_;
815 
816   bool dump_stats_;
817   const bool dump_passes_;
818 
819   CumulativeLogger* const timings_logger_;
820 
821   typedef void (*CompilerCallbackFn)(CompilerDriver& driver);
822   typedef MutexLock* (*CompilerMutexLockFn)(CompilerDriver& driver);
823 
824   void* compiler_library_;
825 
826   typedef void (*DexToDexCompilerFn)(CompilerDriver& driver,
827                                      const DexFile::CodeItem* code_item,
828                                      uint32_t access_flags, InvokeType invoke_type,
829                                      uint32_t class_dex_idx, uint32_t method_idx,
830                                      jobject class_loader, const DexFile& dex_file,
831                                      DexToDexCompilationLevel dex_to_dex_compilation_level);
832   DexToDexCompilerFn dex_to_dex_compiler_;
833 
834   void* compiler_context_;
835 
836   pthread_key_t tls_key_;
837 
838   // Arena pool used by the compiler.
839   ArenaPool arena_pool_;
840 
841   typedef void (*CompilerEnableAutoElfLoadingFn)(CompilerDriver& driver);
842   CompilerEnableAutoElfLoadingFn compiler_enable_auto_elf_loading_;
843 
844   typedef const void* (*CompilerGetMethodCodeAddrFn)
845       (const CompilerDriver& driver, const CompiledMethod* cm, const mirror::ArtMethod* method);
846   CompilerGetMethodCodeAddrFn compiler_get_method_code_addr_;
847 
848   bool support_boot_image_fixup_;
849 
850   // Call Frame Information, which might be generated to help stack tracebacks.
851   std::unique_ptr<std::vector<uint8_t>> cfi_info_;
852 
853   // DeDuplication data structures, these own the corresponding byte arrays.
854   class DedupeHashFunc {
855    public:
operator()856     size_t operator()(const ArrayRef<const uint8_t>& array) const {
857       if (kUseMurmur3Hash) {
858         static constexpr uint32_t c1 = 0xcc9e2d51;
859         static constexpr uint32_t c2 = 0x1b873593;
860         static constexpr uint32_t r1 = 15;
861         static constexpr uint32_t r2 = 13;
862         static constexpr uint32_t m = 5;
863         static constexpr uint32_t n = 0xe6546b64;
864 
865         uint32_t hash = 0;
866         uint32_t len = static_cast<uint32_t>(array.size());
867 
868         const int nblocks = len / 4;
869         typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
870         const unaligned_uint32_t *blocks = reinterpret_cast<const uint32_t*>(array.data());
871         int i;
872         for (i = 0; i < nblocks; i++) {
873           uint32_t k = blocks[i];
874           k *= c1;
875           k = (k << r1) | (k >> (32 - r1));
876           k *= c2;
877 
878           hash ^= k;
879           hash = ((hash << r2) | (hash >> (32 - r2))) * m + n;
880         }
881 
882         const uint8_t *tail = reinterpret_cast<const uint8_t*>(array.data() + nblocks * 4);
883         uint32_t k1 = 0;
884 
885         switch (len & 3) {
886           case 3:
887             k1 ^= tail[2] << 16;
888           case 2:
889             k1 ^= tail[1] << 8;
890           case 1:
891             k1 ^= tail[0];
892 
893             k1 *= c1;
894             k1 = (k1 << r1) | (k1 >> (32 - r1));
895             k1 *= c2;
896             hash ^= k1;
897         }
898 
899         hash ^= len;
900         hash ^= (hash >> 16);
901         hash *= 0x85ebca6b;
902         hash ^= (hash >> 13);
903         hash *= 0xc2b2ae35;
904         hash ^= (hash >> 16);
905 
906         return hash;
907       } else {
908         size_t hash = 0x811c9dc5;
909         for (uint8_t b : array) {
910           hash = (hash * 16777619) ^ b;
911         }
912         hash += hash << 13;
913         hash ^= hash >> 7;
914         hash += hash << 3;
915         hash ^= hash >> 17;
916         hash += hash << 5;
917         return hash;
918       }
919     }
920   };
921   DedupeSet<ArrayRef<const uint8_t>,
922             SwapVector<uint8_t>, size_t, DedupeHashFunc, 4> dedupe_code_;
923   DedupeSet<ArrayRef<const uint8_t>,
924             SwapVector<uint8_t>, size_t, DedupeHashFunc, 4> dedupe_mapping_table_;
925   DedupeSet<ArrayRef<const uint8_t>,
926             SwapVector<uint8_t>, size_t, DedupeHashFunc, 4> dedupe_vmap_table_;
927   DedupeSet<ArrayRef<const uint8_t>,
928             SwapVector<uint8_t>, size_t, DedupeHashFunc, 4> dedupe_gc_map_;
929   DedupeSet<ArrayRef<const uint8_t>,
930             SwapVector<uint8_t>, size_t, DedupeHashFunc, 4> dedupe_cfi_info_;
931 
932   DISALLOW_COPY_AND_ASSIGN(CompilerDriver);
933 };
934 
935 }  // namespace art
936 
937 #endif  // ART_COMPILER_DRIVER_COMPILER_DRIVER_H_
938