compiler_driver.h revision 4560248d4c85cade7f4fc7b30c3fb41b95a04a7f
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 "class_reference.h"
26#include "compiled_class.h"
27#include "compiled_method.h"
28#include "dex_file.h"
29#include "instruction_set.h"
30#include "invoke_type.h"
31#include "method_reference.h"
32#include "os.h"
33#include "runtime.h"
34#include "safe_map.h"
35#include "thread_pool.h"
36
37namespace art {
38
39class AOTCompilationStats;
40class ParallelCompilationManager;
41class DexCompilationUnit;
42class TimingLogger;
43
44enum CompilerBackend {
45  kQuick,
46  kPortable,
47  kNoBackend
48};
49
50// Thread-local storage compiler worker threads
51class CompilerTls {
52  public:
53    CompilerTls() : llvm_info_(NULL) {}
54    ~CompilerTls() {}
55
56    void* GetLLVMInfo() { return llvm_info_; }
57
58    void SetLLVMInfo(void* llvm_info) { llvm_info_ = llvm_info; }
59
60  private:
61    void* llvm_info_;
62};
63
64class CompilerDriver {
65 public:
66  typedef std::set<std::string> DescriptorSet;
67
68  // Create a compiler targeting the requested "instruction_set".
69  // "image" should be true if image specific optimizations should be
70  // enabled.  "image_classes" lets the compiler know what classes it
71  // can assume will be in the image, with NULL implying all available
72  // classes.
73  explicit CompilerDriver(CompilerBackend compiler_backend, InstructionSet instruction_set,
74                          bool image, DescriptorSet* image_classes,
75                          size_t thread_count, bool dump_stats);
76
77  ~CompilerDriver();
78
79  void CompileAll(jobject class_loader, const std::vector<const DexFile*>& dex_files,
80                  TimingLogger& timings)
81      LOCKS_EXCLUDED(Locks::mutator_lock_);
82
83  // Compile a single Method
84  void CompileOne(const mirror::AbstractMethod* method, TimingLogger& timings)
85      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
86
87  InstructionSet GetInstructionSet() const {
88    return instruction_set_;
89  }
90
91  CompilerBackend GetCompilerBackend() const {
92    return compiler_backend_;
93  }
94
95  bool IsImage() const {
96    return image_;
97  }
98
99  DescriptorSet* GetImageClasses() const {
100    return image_classes_.get();
101  }
102
103  CompilerTls* GetTls();
104
105  // Generate the trampolines that are invoked by unresolved direct methods.
106  const std::vector<uint8_t>* CreatePortableResolutionTrampoline() const
107      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
108  const std::vector<uint8_t>* CreateQuickResolutionTrampoline() const
109      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
110  const std::vector<uint8_t>* CreateInterpreterToInterpreterEntry() const
111      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
112  const std::vector<uint8_t>* CreateInterpreterToQuickEntry() const
113      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
114
115  CompiledClass* GetCompiledClass(ClassReference ref) const
116      LOCKS_EXCLUDED(compiled_classes_lock_);
117
118  CompiledMethod* GetCompiledMethod(MethodReference ref) const
119      LOCKS_EXCLUDED(compiled_methods_lock_);
120
121  void AddRequiresConstructorBarrier(Thread* self, const DexFile* dex_file, size_t class_def_index);
122  bool RequiresConstructorBarrier(Thread* self, const DexFile* dex_file, size_t class_def_index);
123
124  // Callbacks from compiler to see what runtime checks must be generated.
125
126  bool CanAssumeTypeIsPresentInDexCache(const DexFile& dex_file, uint32_t type_idx)
127      LOCKS_EXCLUDED(Locks::mutator_lock_);
128
129  bool CanAssumeStringIsPresentInDexCache(const DexFile& dex_file, uint32_t string_idx)
130      LOCKS_EXCLUDED(Locks::mutator_lock_);
131
132  // Are runtime access checks necessary in the compiled code?
133  bool CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file,
134                                  uint32_t type_idx, bool* type_known_final = NULL,
135                                  bool* type_known_abstract = NULL,
136                                  bool* equals_referrers_class = NULL)
137      LOCKS_EXCLUDED(Locks::mutator_lock_);
138
139  // Are runtime access and instantiable checks necessary in the code?
140  bool CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file,
141                                              uint32_t type_idx)
142     LOCKS_EXCLUDED(Locks::mutator_lock_);
143
144  // Can we fast path instance field access? Computes field's offset and volatility.
145  bool ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit,
146                                int& field_offset, bool& is_volatile, bool is_put)
147      LOCKS_EXCLUDED(Locks::mutator_lock_);
148
149  // Can we fastpath static field access? Computes field's offset, volatility and whether the
150  // field is within the referrer (which can avoid checking class initialization).
151  bool ComputeStaticFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit,
152                              int& field_offset, int& ssb_index,
153                              bool& is_referrers_class, bool& is_volatile, bool is_put)
154      LOCKS_EXCLUDED(Locks::mutator_lock_);
155
156  // Can we fastpath a interface, super class or virtual method call? Computes method's vtable
157  // index.
158  bool ComputeInvokeInfo(const DexCompilationUnit* mUnit, const uint32_t dex_pc,
159                         InvokeType& type, MethodReference& target_method, int& vtable_idx,
160                         uintptr_t& direct_code, uintptr_t& direct_method, bool update_stats)
161      LOCKS_EXCLUDED(Locks::mutator_lock_);
162
163  bool IsSafeCast(const MethodReference& mr, uint32_t dex_pc);
164
165  // Record patch information for later fix up.
166  void AddCodePatch(const DexFile* dex_file,
167                    uint32_t referrer_method_idx,
168                    InvokeType referrer_invoke_type,
169                    uint32_t target_method_idx,
170                    InvokeType target_invoke_type,
171                    size_t literal_offset)
172      LOCKS_EXCLUDED(compiled_methods_lock_);
173  void AddMethodPatch(const DexFile* dex_file,
174                      uint32_t referrer_method_idx,
175                      InvokeType referrer_invoke_type,
176                      uint32_t target_method_idx,
177                      InvokeType target_invoke_type,
178                      size_t literal_offset)
179      LOCKS_EXCLUDED(compiled_methods_lock_);
180
181  void SetBitcodeFileName(std::string const& filename);
182
183  bool GetSupportBootImageFixup() const {
184    return support_boot_image_fixup_;
185  }
186
187  void SetSupportBootImageFixup(bool support_boot_image_fixup) {
188    support_boot_image_fixup_ = support_boot_image_fixup;
189  }
190
191
192  bool WriteElf(const std::string& android_root,
193                bool is_host,
194                const std::vector<const DexFile*>& dex_files,
195                std::vector<uint8_t>& oat_contents,
196                File* file);
197
198  // TODO: move to a common home for llvm helpers once quick/portable are merged
199  static void InstructionSetToLLVMTarget(InstructionSet instruction_set,
200                                         std::string& target_triple,
201                                         std::string& target_cpu,
202                                         std::string& target_attr);
203
204  void SetCompilerContext(void* compiler_context) {
205    compiler_context_ = compiler_context;
206  }
207
208  void* GetCompilerContext() const {
209    return compiler_context_;
210  }
211
212  size_t GetThreadCount() const {
213    return thread_count_;
214  }
215
216  class PatchInformation {
217   public:
218    const DexFile& GetDexFile() const {
219      return *dex_file_;
220    }
221    uint32_t GetReferrerMethodIdx() const {
222      return referrer_method_idx_;
223    }
224    InvokeType GetReferrerInvokeType() const {
225      return referrer_invoke_type_;
226    }
227    uint32_t GetTargetMethodIdx() const {
228      return target_method_idx_;
229    }
230    InvokeType GetTargetInvokeType() const {
231      return target_invoke_type_;
232    }
233    size_t GetLiteralOffset() const {;
234      return literal_offset_;
235    }
236
237   private:
238    PatchInformation(const DexFile* dex_file,
239                     uint32_t referrer_method_idx,
240                     InvokeType referrer_invoke_type,
241                     uint32_t target_method_idx,
242                     InvokeType target_invoke_type,
243                     size_t literal_offset)
244      : dex_file_(dex_file),
245        referrer_method_idx_(referrer_method_idx),
246        referrer_invoke_type_(referrer_invoke_type),
247        target_method_idx_(target_method_idx),
248        target_invoke_type_(target_invoke_type),
249        literal_offset_(literal_offset) {
250      CHECK(dex_file_ != NULL);
251    }
252
253    const DexFile* dex_file_;
254    uint32_t referrer_method_idx_;
255    InvokeType referrer_invoke_type_;
256    uint32_t target_method_idx_;
257    InvokeType target_invoke_type_;
258    size_t literal_offset_;
259
260    friend class CompilerDriver;
261    DISALLOW_COPY_AND_ASSIGN(PatchInformation);
262  };
263
264  const std::vector<const PatchInformation*>& GetCodeToPatch() const {
265    return code_to_patch_;
266  }
267  const std::vector<const PatchInformation*>& GetMethodsToPatch() const {
268    return methods_to_patch_;
269  }
270
271  // Checks if class specified by type_idx is one of the image_classes_
272  bool IsImageClass(const char* descriptor) const;
273
274  void RecordClassStatus(ClassReference ref, CompiledClass* compiled_class);
275
276 private:
277  // Compute constant code and method pointers when possible
278  void GetCodeAndMethodForDirectCall(InvokeType type, InvokeType sharp_type,
279                                     mirror::Class* referrer_class,
280                                     mirror::AbstractMethod* method,
281                                     uintptr_t& direct_code, uintptr_t& direct_method,
282                                     bool update_stats)
283      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
284
285  void PreCompile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
286                  ThreadPool& thread_pool, TimingLogger& timings)
287      LOCKS_EXCLUDED(Locks::mutator_lock_);
288
289  void LoadImageClasses(TimingLogger& timings);
290
291  // Attempt to resolve all type, methods, fields, and strings
292  // referenced from code in the dex file following PathClassLoader
293  // ordering semantics.
294  void Resolve(jobject class_loader, const std::vector<const DexFile*>& dex_files,
295               ThreadPool& thread_pool, TimingLogger& timings)
296      LOCKS_EXCLUDED(Locks::mutator_lock_);
297  void ResolveDexFile(jobject class_loader, const DexFile& dex_file,
298                      ThreadPool& thread_pool, TimingLogger& timings)
299      LOCKS_EXCLUDED(Locks::mutator_lock_);
300
301  void Verify(jobject class_loader, const std::vector<const DexFile*>& dex_files,
302              ThreadPool& thread_pool, TimingLogger& timings);
303  void VerifyDexFile(jobject class_loader, const DexFile& dex_file,
304                     ThreadPool& thread_pool, TimingLogger& timings)
305      LOCKS_EXCLUDED(Locks::mutator_lock_);
306
307  void InitializeClasses(jobject class_loader, const std::vector<const DexFile*>& dex_files,
308                         ThreadPool& thread_pool, TimingLogger& timings)
309      LOCKS_EXCLUDED(Locks::mutator_lock_);
310  void InitializeClasses(jobject class_loader, const DexFile& dex_file,
311                         ThreadPool& thread_pool, TimingLogger& timings)
312      LOCKS_EXCLUDED(Locks::mutator_lock_, compiled_classes_lock_);
313
314  void UpdateImageClasses(TimingLogger& timings);
315  static void FindClinitImageClassesCallback(mirror::Object* object, void* arg)
316      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
317
318  void Compile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
319               ThreadPool& thread_pool, TimingLogger& timings);
320  void CompileDexFile(jobject class_loader, const DexFile& dex_file,
321                      ThreadPool& thread_pool, TimingLogger& timings)
322      LOCKS_EXCLUDED(Locks::mutator_lock_);
323  void CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
324                     InvokeType invoke_type, uint32_t class_def_idx, uint32_t method_idx,
325                     jobject class_loader, const DexFile& dex_file,
326                     bool allow_dex_to_dex_compilation)
327      LOCKS_EXCLUDED(compiled_methods_lock_);
328
329  static void CompileClass(const ParallelCompilationManager* context, size_t class_def_index)
330      LOCKS_EXCLUDED(Locks::mutator_lock_);
331
332  std::vector<const PatchInformation*> code_to_patch_;
333  std::vector<const PatchInformation*> methods_to_patch_;
334
335  CompilerBackend compiler_backend_;
336
337  InstructionSet instruction_set_;
338
339  // All class references that require
340  mutable Mutex freezing_constructor_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
341  std::set<ClassReference> freezing_constructor_classes_ GUARDED_BY(freezing_constructor_lock_);
342
343  typedef SafeMap<const ClassReference, CompiledClass*> ClassTable;
344  // All class references that this compiler has compiled.
345  mutable Mutex compiled_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
346  ClassTable compiled_classes_ GUARDED_BY(compiled_classes_lock_);
347
348  typedef SafeMap<const MethodReference, CompiledMethod*, MethodReferenceComparator> MethodTable;
349  // All method references that this compiler has compiled.
350  mutable Mutex compiled_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
351  MethodTable compiled_methods_ GUARDED_BY(compiled_methods_lock_);
352
353  const bool image_;
354
355  // If image_ is true, specifies the classes that will be included in
356  // the image. Note if image_classes_ is NULL, all classes are
357  // included in the image.
358  UniquePtr<DescriptorSet> image_classes_;
359
360  size_t thread_count_;
361  uint64_t start_ns_;
362
363  UniquePtr<AOTCompilationStats> stats_;
364
365  bool dump_stats_;
366
367  typedef void (*CompilerCallbackFn)(CompilerDriver& driver);
368  typedef MutexLock* (*CompilerMutexLockFn)(CompilerDriver& driver);
369
370  void* compiler_library_;
371
372  typedef CompiledMethod* (*CompilerFn)(CompilerDriver& driver,
373                                        const DexFile::CodeItem* code_item,
374                                        uint32_t access_flags, InvokeType invoke_type,
375                                        uint32_t class_dex_idx, uint32_t method_idx,
376                                        jobject class_loader, const DexFile& dex_file);
377  CompilerFn compiler_;
378#ifdef ART_SEA_IR_MODE
379  CompilerFn sea_ir_compiler_;
380#endif
381
382  CompilerFn dex_to_dex_compiler_;
383
384  void* compiler_context_;
385
386  typedef CompiledMethod* (*JniCompilerFn)(CompilerDriver& driver,
387                                           uint32_t access_flags, uint32_t method_idx,
388                                           const DexFile& dex_file);
389  JniCompilerFn jni_compiler_;
390
391  pthread_key_t tls_key_;
392
393  typedef void (*CompilerEnableAutoElfLoadingFn)(CompilerDriver& driver);
394  CompilerEnableAutoElfLoadingFn compiler_enable_auto_elf_loading_;
395
396  typedef const void* (*CompilerGetMethodCodeAddrFn)
397      (const CompilerDriver& driver, const CompiledMethod* cm, const mirror::AbstractMethod* method);
398  CompilerGetMethodCodeAddrFn compiler_get_method_code_addr_;
399
400  bool support_boot_image_fixup_;
401
402  DISALLOW_COPY_AND_ASSIGN(CompilerDriver);
403};
404
405}  // namespace art
406
407#endif  // ART_COMPILER_DRIVER_COMPILER_DRIVER_H_
408