compiler_driver.cc revision bb0b53f58f11c628f077603b56077dfed1a18f11
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#include "compiler_driver.h"
18
19#define ATRACE_TAG ATRACE_TAG_DALVIK
20#include <utils/Trace.h>
21
22#include <vector>
23#include <unistd.h>
24
25#include "base/stl_util.h"
26#include "base/timing_logger.h"
27#include "class_linker.h"
28#include "compiler.h"
29#include "compiler_driver-inl.h"
30#include "dex_compilation_unit.h"
31#include "dex_file-inl.h"
32#include "dex/verification_results.h"
33#include "dex/verified_method.h"
34#include "dex/quick/dex_file_method_inliner.h"
35#include "driver/compiler_options.h"
36#include "jni_internal.h"
37#include "object_utils.h"
38#include "profiler.h"
39#include "runtime.h"
40#include "gc/accounting/card_table-inl.h"
41#include "gc/accounting/heap_bitmap.h"
42#include "gc/space/space.h"
43#include "mirror/art_field-inl.h"
44#include "mirror/art_method-inl.h"
45#include "mirror/class_loader.h"
46#include "mirror/class-inl.h"
47#include "mirror/dex_cache-inl.h"
48#include "mirror/object-inl.h"
49#include "mirror/object_array-inl.h"
50#include "mirror/throwable.h"
51#include "scoped_thread_state_change.h"
52#include "ScopedLocalRef.h"
53#include "handle_scope-inl.h"
54#include "thread.h"
55#include "thread_pool.h"
56#include "trampolines/trampoline_compiler.h"
57#include "transaction.h"
58#include "utils.h"
59#include "verifier/method_verifier.h"
60#include "verifier/method_verifier-inl.h"
61
62namespace art {
63
64static double Percentage(size_t x, size_t y) {
65  return 100.0 * (static_cast<double>(x)) / (static_cast<double>(x + y));
66}
67
68static void DumpStat(size_t x, size_t y, const char* str) {
69  if (x == 0 && y == 0) {
70    return;
71  }
72  LOG(INFO) << Percentage(x, y) << "% of " << str << " for " << (x + y) << " cases";
73}
74
75class CompilerDriver::AOTCompilationStats {
76 public:
77  AOTCompilationStats()
78      : stats_lock_("AOT compilation statistics lock"),
79        types_in_dex_cache_(0), types_not_in_dex_cache_(0),
80        strings_in_dex_cache_(0), strings_not_in_dex_cache_(0),
81        resolved_types_(0), unresolved_types_(0),
82        resolved_instance_fields_(0), unresolved_instance_fields_(0),
83        resolved_local_static_fields_(0), resolved_static_fields_(0), unresolved_static_fields_(0),
84        type_based_devirtualization_(0),
85        safe_casts_(0), not_safe_casts_(0) {
86    for (size_t i = 0; i <= kMaxInvokeType; i++) {
87      resolved_methods_[i] = 0;
88      unresolved_methods_[i] = 0;
89      virtual_made_direct_[i] = 0;
90      direct_calls_to_boot_[i] = 0;
91      direct_methods_to_boot_[i] = 0;
92    }
93  }
94
95  void Dump() {
96    DumpStat(types_in_dex_cache_, types_not_in_dex_cache_, "types known to be in dex cache");
97    DumpStat(strings_in_dex_cache_, strings_not_in_dex_cache_, "strings known to be in dex cache");
98    DumpStat(resolved_types_, unresolved_types_, "types resolved");
99    DumpStat(resolved_instance_fields_, unresolved_instance_fields_, "instance fields resolved");
100    DumpStat(resolved_local_static_fields_ + resolved_static_fields_, unresolved_static_fields_,
101             "static fields resolved");
102    DumpStat(resolved_local_static_fields_, resolved_static_fields_ + unresolved_static_fields_,
103             "static fields local to a class");
104    DumpStat(safe_casts_, not_safe_casts_, "check-casts removed based on type information");
105    // Note, the code below subtracts the stat value so that when added to the stat value we have
106    // 100% of samples. TODO: clean this up.
107    DumpStat(type_based_devirtualization_,
108             resolved_methods_[kVirtual] + unresolved_methods_[kVirtual] +
109             resolved_methods_[kInterface] + unresolved_methods_[kInterface] -
110             type_based_devirtualization_,
111             "virtual/interface calls made direct based on type information");
112
113    for (size_t i = 0; i <= kMaxInvokeType; i++) {
114      std::ostringstream oss;
115      oss << static_cast<InvokeType>(i) << " methods were AOT resolved";
116      DumpStat(resolved_methods_[i], unresolved_methods_[i], oss.str().c_str());
117      if (virtual_made_direct_[i] > 0) {
118        std::ostringstream oss2;
119        oss2 << static_cast<InvokeType>(i) << " methods made direct";
120        DumpStat(virtual_made_direct_[i],
121                 resolved_methods_[i] + unresolved_methods_[i] - virtual_made_direct_[i],
122                 oss2.str().c_str());
123      }
124      if (direct_calls_to_boot_[i] > 0) {
125        std::ostringstream oss2;
126        oss2 << static_cast<InvokeType>(i) << " method calls are direct into boot";
127        DumpStat(direct_calls_to_boot_[i],
128                 resolved_methods_[i] + unresolved_methods_[i] - direct_calls_to_boot_[i],
129                 oss2.str().c_str());
130      }
131      if (direct_methods_to_boot_[i] > 0) {
132        std::ostringstream oss2;
133        oss2 << static_cast<InvokeType>(i) << " method calls have methods in boot";
134        DumpStat(direct_methods_to_boot_[i],
135                 resolved_methods_[i] + unresolved_methods_[i] - direct_methods_to_boot_[i],
136                 oss2.str().c_str());
137      }
138    }
139  }
140
141// Allow lossy statistics in non-debug builds.
142#ifndef NDEBUG
143#define STATS_LOCK() MutexLock mu(Thread::Current(), stats_lock_)
144#else
145#define STATS_LOCK()
146#endif
147
148  void TypeInDexCache() {
149    STATS_LOCK();
150    types_in_dex_cache_++;
151  }
152
153  void TypeNotInDexCache() {
154    STATS_LOCK();
155    types_not_in_dex_cache_++;
156  }
157
158  void StringInDexCache() {
159    STATS_LOCK();
160    strings_in_dex_cache_++;
161  }
162
163  void StringNotInDexCache() {
164    STATS_LOCK();
165    strings_not_in_dex_cache_++;
166  }
167
168  void TypeDoesntNeedAccessCheck() {
169    STATS_LOCK();
170    resolved_types_++;
171  }
172
173  void TypeNeedsAccessCheck() {
174    STATS_LOCK();
175    unresolved_types_++;
176  }
177
178  void ResolvedInstanceField() {
179    STATS_LOCK();
180    resolved_instance_fields_++;
181  }
182
183  void UnresolvedInstanceField() {
184    STATS_LOCK();
185    unresolved_instance_fields_++;
186  }
187
188  void ResolvedLocalStaticField() {
189    STATS_LOCK();
190    resolved_local_static_fields_++;
191  }
192
193  void ResolvedStaticField() {
194    STATS_LOCK();
195    resolved_static_fields_++;
196  }
197
198  void UnresolvedStaticField() {
199    STATS_LOCK();
200    unresolved_static_fields_++;
201  }
202
203  // Indicate that type information from the verifier led to devirtualization.
204  void PreciseTypeDevirtualization() {
205    STATS_LOCK();
206    type_based_devirtualization_++;
207  }
208
209  // Indicate that a method of the given type was resolved at compile time.
210  void ResolvedMethod(InvokeType type) {
211    DCHECK_LE(type, kMaxInvokeType);
212    STATS_LOCK();
213    resolved_methods_[type]++;
214  }
215
216  // Indicate that a method of the given type was unresolved at compile time as it was in an
217  // unknown dex file.
218  void UnresolvedMethod(InvokeType type) {
219    DCHECK_LE(type, kMaxInvokeType);
220    STATS_LOCK();
221    unresolved_methods_[type]++;
222  }
223
224  // Indicate that a type of virtual method dispatch has been converted into a direct method
225  // dispatch.
226  void VirtualMadeDirect(InvokeType type) {
227    DCHECK(type == kVirtual || type == kInterface || type == kSuper);
228    STATS_LOCK();
229    virtual_made_direct_[type]++;
230  }
231
232  // Indicate that a method of the given type was able to call directly into boot.
233  void DirectCallsToBoot(InvokeType type) {
234    DCHECK_LE(type, kMaxInvokeType);
235    STATS_LOCK();
236    direct_calls_to_boot_[type]++;
237  }
238
239  // Indicate that a method of the given type was able to be resolved directly from boot.
240  void DirectMethodsToBoot(InvokeType type) {
241    DCHECK_LE(type, kMaxInvokeType);
242    STATS_LOCK();
243    direct_methods_to_boot_[type]++;
244  }
245
246  void ProcessedInvoke(InvokeType type, int flags) {
247    STATS_LOCK();
248    if (flags == 0) {
249      unresolved_methods_[type]++;
250    } else {
251      DCHECK_NE((flags & kFlagMethodResolved), 0);
252      resolved_methods_[type]++;
253      if ((flags & kFlagVirtualMadeDirect) != 0) {
254        virtual_made_direct_[type]++;
255        if ((flags & kFlagPreciseTypeDevirtualization) != 0) {
256          type_based_devirtualization_++;
257        }
258      } else {
259        DCHECK_EQ((flags & kFlagPreciseTypeDevirtualization), 0);
260      }
261      if ((flags & kFlagDirectCallToBoot) != 0) {
262        direct_calls_to_boot_[type]++;
263      }
264      if ((flags & kFlagDirectMethodToBoot) != 0) {
265        direct_methods_to_boot_[type]++;
266      }
267    }
268  }
269
270  // A check-cast could be eliminated due to verifier type analysis.
271  void SafeCast() {
272    STATS_LOCK();
273    safe_casts_++;
274  }
275
276  // A check-cast couldn't be eliminated due to verifier type analysis.
277  void NotASafeCast() {
278    STATS_LOCK();
279    not_safe_casts_++;
280  }
281
282 private:
283  Mutex stats_lock_;
284
285  size_t types_in_dex_cache_;
286  size_t types_not_in_dex_cache_;
287
288  size_t strings_in_dex_cache_;
289  size_t strings_not_in_dex_cache_;
290
291  size_t resolved_types_;
292  size_t unresolved_types_;
293
294  size_t resolved_instance_fields_;
295  size_t unresolved_instance_fields_;
296
297  size_t resolved_local_static_fields_;
298  size_t resolved_static_fields_;
299  size_t unresolved_static_fields_;
300  // Type based devirtualization for invoke interface and virtual.
301  size_t type_based_devirtualization_;
302
303  size_t resolved_methods_[kMaxInvokeType + 1];
304  size_t unresolved_methods_[kMaxInvokeType + 1];
305  size_t virtual_made_direct_[kMaxInvokeType + 1];
306  size_t direct_calls_to_boot_[kMaxInvokeType + 1];
307  size_t direct_methods_to_boot_[kMaxInvokeType + 1];
308
309  size_t safe_casts_;
310  size_t not_safe_casts_;
311
312  DISALLOW_COPY_AND_ASSIGN(AOTCompilationStats);
313};
314
315
316extern "C" art::CompiledMethod* ArtCompileDEX(art::CompilerDriver& compiler,
317                                              const art::DexFile::CodeItem* code_item,
318                                              uint32_t access_flags,
319                                              art::InvokeType invoke_type,
320                                              uint16_t class_def_idx,
321                                              uint32_t method_idx,
322                                              jobject class_loader,
323                                              const art::DexFile& dex_file);
324
325CompilerDriver::CompilerDriver(const CompilerOptions* compiler_options,
326                               VerificationResults* verification_results,
327                               DexFileToMethodInlinerMap* method_inliner_map,
328                               Compiler::Kind compiler_kind,
329                               InstructionSet instruction_set,
330                               InstructionSetFeatures instruction_set_features,
331                               bool image, DescriptorSet* image_classes, size_t thread_count,
332                               bool dump_stats, bool dump_passes, CumulativeLogger* timer,
333                               std::string profile_file)
334    : profile_ok_(false), compiler_options_(compiler_options),
335      verification_results_(verification_results),
336      method_inliner_map_(method_inliner_map),
337      compiler_(Compiler::Create(this, compiler_kind)),
338      instruction_set_(instruction_set),
339      instruction_set_features_(instruction_set_features),
340      freezing_constructor_lock_("freezing constructor lock"),
341      compiled_classes_lock_("compiled classes lock"),
342      compiled_methods_lock_("compiled method lock"),
343      image_(image),
344      image_classes_(image_classes),
345      thread_count_(thread_count),
346      start_ns_(0),
347      stats_(new AOTCompilationStats),
348      dump_stats_(dump_stats),
349      dump_passes_(dump_passes),
350      timings_logger_(timer),
351      compiler_library_(NULL),
352      compiler_context_(NULL),
353      compiler_enable_auto_elf_loading_(NULL),
354      compiler_get_method_code_addr_(NULL),
355      support_boot_image_fixup_(instruction_set != kMips),
356      cfi_info_(nullptr),
357      dedupe_code_("dedupe code"),
358      dedupe_mapping_table_("dedupe mapping table"),
359      dedupe_vmap_table_("dedupe vmap table"),
360      dedupe_gc_map_("dedupe gc map"),
361      dedupe_cfi_info_("dedupe cfi info") {
362  DCHECK(compiler_options_ != nullptr);
363  DCHECK(verification_results_ != nullptr);
364  DCHECK(method_inliner_map_ != nullptr);
365
366  CHECK_PTHREAD_CALL(pthread_key_create, (&tls_key_, NULL), "compiler tls key");
367
368  // Read the profile file if one is provided.
369  if (profile_file != "") {
370    profile_ok_ = profile_file_.LoadFile(profile_file);
371  }
372
373  dex_to_dex_compiler_ = reinterpret_cast<DexToDexCompilerFn>(ArtCompileDEX);
374
375  compiler_->Init();
376
377  CHECK(!Runtime::Current()->IsStarted());
378  if (image_) {
379    CHECK(image_classes_.get() != nullptr);
380  } else {
381    CHECK(image_classes_.get() == nullptr);
382  }
383
384  // Are we generating CFI information?
385  if (compiler_options->GetGenerateGDBInformation()) {
386    cfi_info_.reset(compiler_->GetCallFrameInformationInitialization(*this));
387  }
388}
389
390std::vector<uint8_t>* CompilerDriver::DeduplicateCode(const std::vector<uint8_t>& code) {
391  return dedupe_code_.Add(Thread::Current(), code);
392}
393
394std::vector<uint8_t>* CompilerDriver::DeduplicateMappingTable(const std::vector<uint8_t>& code) {
395  return dedupe_mapping_table_.Add(Thread::Current(), code);
396}
397
398std::vector<uint8_t>* CompilerDriver::DeduplicateVMapTable(const std::vector<uint8_t>& code) {
399  return dedupe_vmap_table_.Add(Thread::Current(), code);
400}
401
402std::vector<uint8_t>* CompilerDriver::DeduplicateGCMap(const std::vector<uint8_t>& code) {
403  return dedupe_gc_map_.Add(Thread::Current(), code);
404}
405
406std::vector<uint8_t>* CompilerDriver::DeduplicateCFIInfo(const std::vector<uint8_t>* cfi_info) {
407  if (cfi_info == nullptr) {
408    return nullptr;
409  }
410  return dedupe_cfi_info_.Add(Thread::Current(), *cfi_info);
411}
412
413CompilerDriver::~CompilerDriver() {
414  Thread* self = Thread::Current();
415  {
416    MutexLock mu(self, compiled_classes_lock_);
417    STLDeleteValues(&compiled_classes_);
418  }
419  {
420    MutexLock mu(self, compiled_methods_lock_);
421    STLDeleteValues(&compiled_methods_);
422  }
423  {
424    MutexLock mu(self, compiled_methods_lock_);
425    STLDeleteElements(&code_to_patch_);
426  }
427  {
428    MutexLock mu(self, compiled_methods_lock_);
429    STLDeleteElements(&methods_to_patch_);
430  }
431  {
432    MutexLock mu(self, compiled_methods_lock_);
433    STLDeleteElements(&classes_to_patch_);
434  }
435  CHECK_PTHREAD_CALL(pthread_key_delete, (tls_key_), "delete tls key");
436  compiler_->UnInit();
437}
438
439CompilerTls* CompilerDriver::GetTls() {
440  // Lazily create thread-local storage
441  CompilerTls* res = static_cast<CompilerTls*>(pthread_getspecific(tls_key_));
442  if (res == NULL) {
443    res = new CompilerTls();
444    CHECK_PTHREAD_CALL(pthread_setspecific, (tls_key_, res), "compiler tls");
445  }
446  return res;
447}
448
449#define CREATE_TRAMPOLINE(type, abi, offset) \
450    if (Is64BitInstructionSet(instruction_set_)) { \
451      return CreateTrampoline64(instruction_set_, abi, \
452                                type ## _ENTRYPOINT_OFFSET(8, offset)); \
453    } else { \
454      return CreateTrampoline32(instruction_set_, abi, \
455                                type ## _ENTRYPOINT_OFFSET(4, offset)); \
456    }
457
458const std::vector<uint8_t>* CompilerDriver::CreateInterpreterToInterpreterBridge() const {
459  CREATE_TRAMPOLINE(INTERPRETER, kInterpreterAbi, pInterpreterToInterpreterBridge)
460}
461
462const std::vector<uint8_t>* CompilerDriver::CreateInterpreterToCompiledCodeBridge() const {
463  CREATE_TRAMPOLINE(INTERPRETER, kInterpreterAbi, pInterpreterToCompiledCodeBridge)
464}
465
466const std::vector<uint8_t>* CompilerDriver::CreateJniDlsymLookup() const {
467  CREATE_TRAMPOLINE(JNI, kJniAbi, pDlsymLookup)
468}
469
470const std::vector<uint8_t>* CompilerDriver::CreatePortableImtConflictTrampoline() const {
471  CREATE_TRAMPOLINE(PORTABLE, kPortableAbi, pPortableImtConflictTrampoline)
472}
473
474const std::vector<uint8_t>* CompilerDriver::CreatePortableResolutionTrampoline() const {
475  CREATE_TRAMPOLINE(PORTABLE, kPortableAbi, pPortableResolutionTrampoline)
476}
477
478const std::vector<uint8_t>* CompilerDriver::CreatePortableToInterpreterBridge() const {
479  CREATE_TRAMPOLINE(PORTABLE, kPortableAbi, pPortableToInterpreterBridge)
480}
481
482const std::vector<uint8_t>* CompilerDriver::CreateQuickGenericJniTrampoline() const {
483  CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickGenericJniTrampoline)
484}
485
486const std::vector<uint8_t>* CompilerDriver::CreateQuickImtConflictTrampoline() const {
487  CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickImtConflictTrampoline)
488}
489
490const std::vector<uint8_t>* CompilerDriver::CreateQuickResolutionTrampoline() const {
491  CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickResolutionTrampoline)
492}
493
494const std::vector<uint8_t>* CompilerDriver::CreateQuickToInterpreterBridge() const {
495  CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickToInterpreterBridge)
496}
497#undef CREATE_TRAMPOLINE
498
499void CompilerDriver::CompileAll(jobject class_loader,
500                                const std::vector<const DexFile*>& dex_files,
501                                TimingLogger* timings) {
502  DCHECK(!Runtime::Current()->IsStarted());
503  std::unique_ptr<ThreadPool> thread_pool(new ThreadPool("Compiler driver thread pool", thread_count_ - 1));
504  PreCompile(class_loader, dex_files, thread_pool.get(), timings);
505  Compile(class_loader, dex_files, thread_pool.get(), timings);
506  if (dump_stats_) {
507    stats_->Dump();
508  }
509}
510
511static DexToDexCompilationLevel GetDexToDexCompilationlevel(
512    Thread* self, Handle<mirror::ClassLoader> class_loader, const DexFile& dex_file,
513    const DexFile::ClassDef& class_def) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
514  const char* descriptor = dex_file.GetClassDescriptor(class_def);
515  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
516  mirror::Class* klass = class_linker->FindClass(self, descriptor, class_loader);
517  if (klass == NULL) {
518    CHECK(self->IsExceptionPending());
519    self->ClearException();
520    return kDontDexToDexCompile;
521  }
522  // The verifier can only run on "quick" instructions at runtime (see usage of
523  // FindAccessedFieldAtDexPc and FindInvokedMethodAtDexPc in ThrowNullPointerExceptionFromDexPC
524  // function). Since image classes can be verified again while compiling an application,
525  // we must prevent the DEX-to-DEX compiler from introducing them.
526  // TODO: find a way to enable "quick" instructions for image classes and remove this check.
527  bool compiling_image_classes = class_loader.Get() == nullptr;
528  if (compiling_image_classes) {
529    return kRequired;
530  } else if (klass->IsVerified()) {
531    // Class is verified so we can enable DEX-to-DEX compilation for performance.
532    return kOptimize;
533  } else if (klass->IsCompileTimeVerified()) {
534    // Class verification has soft-failed. Anyway, ensure at least correctness.
535    DCHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime);
536    return kRequired;
537  } else {
538    // Class verification has failed: do not run DEX-to-DEX compilation.
539    return kDontDexToDexCompile;
540  }
541}
542
543void CompilerDriver::CompileOne(mirror::ArtMethod* method, TimingLogger* timings) {
544  DCHECK(!Runtime::Current()->IsStarted());
545  Thread* self = Thread::Current();
546  jobject jclass_loader;
547  const DexFile* dex_file;
548  uint16_t class_def_idx;
549  uint32_t method_idx = method->GetDexMethodIndex();
550  uint32_t access_flags = method->GetAccessFlags();
551  InvokeType invoke_type = method->GetInvokeType();
552  {
553    ScopedObjectAccessUnchecked soa(self);
554    ScopedLocalRef<jobject>
555      local_class_loader(soa.Env(),
556                    soa.AddLocalReference<jobject>(method->GetDeclaringClass()->GetClassLoader()));
557    jclass_loader = soa.Env()->NewGlobalRef(local_class_loader.get());
558    // Find the dex_file
559    MethodHelper mh(method);
560    dex_file = &mh.GetDexFile();
561    class_def_idx = mh.GetClassDefIndex();
562  }
563  const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
564  self->TransitionFromRunnableToSuspended(kNative);
565
566  std::vector<const DexFile*> dex_files;
567  dex_files.push_back(dex_file);
568
569  std::unique_ptr<ThreadPool> thread_pool(new ThreadPool("Compiler driver thread pool", 0U));
570  PreCompile(jclass_loader, dex_files, thread_pool.get(), timings);
571
572  // Can we run DEX-to-DEX compiler on this class ?
573  DexToDexCompilationLevel dex_to_dex_compilation_level = kDontDexToDexCompile;
574  {
575    ScopedObjectAccess soa(Thread::Current());
576    const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
577    StackHandleScope<1> hs(soa.Self());
578    Handle<mirror::ClassLoader> class_loader(
579        hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
580    dex_to_dex_compilation_level = GetDexToDexCompilationlevel(self, class_loader, *dex_file,
581                                                               class_def);
582  }
583  CompileMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx, jclass_loader,
584                *dex_file, dex_to_dex_compilation_level);
585
586  self->GetJniEnv()->DeleteGlobalRef(jclass_loader);
587
588  self->TransitionFromSuspendedToRunnable();
589}
590
591void CompilerDriver::Resolve(jobject class_loader, const std::vector<const DexFile*>& dex_files,
592                             ThreadPool* thread_pool, TimingLogger* timings) {
593  for (size_t i = 0; i != dex_files.size(); ++i) {
594    const DexFile* dex_file = dex_files[i];
595    CHECK(dex_file != nullptr);
596    ResolveDexFile(class_loader, *dex_file, thread_pool, timings);
597  }
598}
599
600void CompilerDriver::PreCompile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
601                                ThreadPool* thread_pool, TimingLogger* timings) {
602  LoadImageClasses(timings);
603
604  if (!compiler_options_->IsVerificationEnabled()) {
605    VLOG(compiler) << "Verify none mode specified, skipping pre-compilation";
606    return;
607  }
608
609  Resolve(class_loader, dex_files, thread_pool, timings);
610
611  Verify(class_loader, dex_files, thread_pool, timings);
612
613  InitializeClasses(class_loader, dex_files, thread_pool, timings);
614
615  UpdateImageClasses(timings);
616}
617
618bool CompilerDriver::IsImageClass(const char* descriptor) const {
619  if (!IsImage()) {
620    return true;
621  } else {
622    return image_classes_->find(descriptor) != image_classes_->end();
623  }
624}
625
626static void ResolveExceptionsForMethod(MethodHelper* mh,
627    std::set<std::pair<uint16_t, const DexFile*>>& exceptions_to_resolve)
628    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
629  const DexFile::CodeItem* code_item = mh->GetCodeItem();
630  if (code_item == NULL) {
631    return;  // native or abstract method
632  }
633  if (code_item->tries_size_ == 0) {
634    return;  // nothing to process
635  }
636  const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
637  size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
638  for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
639    int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
640    bool has_catch_all = false;
641    if (encoded_catch_handler_size <= 0) {
642      encoded_catch_handler_size = -encoded_catch_handler_size;
643      has_catch_all = true;
644    }
645    for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
646      uint16_t encoded_catch_handler_handlers_type_idx =
647          DecodeUnsignedLeb128(&encoded_catch_handler_list);
648      // Add to set of types to resolve if not already in the dex cache resolved types
649      if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
650        exceptions_to_resolve.insert(
651            std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx,
652                                                &mh->GetDexFile()));
653      }
654      // ignore address associated with catch handler
655      DecodeUnsignedLeb128(&encoded_catch_handler_list);
656    }
657    if (has_catch_all) {
658      // ignore catch all address
659      DecodeUnsignedLeb128(&encoded_catch_handler_list);
660    }
661  }
662}
663
664static bool ResolveCatchBlockExceptionsClassVisitor(mirror::Class* c, void* arg)
665    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
666  std::set<std::pair<uint16_t, const DexFile*>>* exceptions_to_resolve =
667      reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*>>*>(arg);
668  MethodHelper mh;
669  for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
670    mirror::ArtMethod* m = c->GetVirtualMethod(i);
671    mh.ChangeMethod(m);
672    ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
673  }
674  for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
675    mirror::ArtMethod* m = c->GetDirectMethod(i);
676    mh.ChangeMethod(m);
677    ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
678  }
679  return true;
680}
681
682static bool RecordImageClassesVisitor(mirror::Class* klass, void* arg)
683    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
684  CompilerDriver::DescriptorSet* image_classes =
685      reinterpret_cast<CompilerDriver::DescriptorSet*>(arg);
686  image_classes->insert(klass->GetDescriptor());
687  return true;
688}
689
690// Make a list of descriptors for classes to include in the image
691void CompilerDriver::LoadImageClasses(TimingLogger* timings)
692      LOCKS_EXCLUDED(Locks::mutator_lock_) {
693  CHECK(timings != nullptr);
694  if (!IsImage()) {
695    return;
696  }
697
698  timings->NewSplit("LoadImageClasses");
699  // Make a first class to load all classes explicitly listed in the file
700  Thread* self = Thread::Current();
701  ScopedObjectAccess soa(self);
702  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
703  CHECK(image_classes_.get() != nullptr);
704  for (auto it = image_classes_->begin(), end = image_classes_->end(); it != end;) {
705    const std::string& descriptor(*it);
706    StackHandleScope<1> hs(self);
707    Handle<mirror::Class> klass(
708        hs.NewHandle(class_linker->FindSystemClass(self, descriptor.c_str())));
709    if (klass.Get() == NULL) {
710      VLOG(compiler) << "Failed to find class " << descriptor;
711      image_classes_->erase(it++);
712      self->ClearException();
713    } else {
714      ++it;
715    }
716  }
717
718  // Resolve exception classes referenced by the loaded classes. The catch logic assumes
719  // exceptions are resolved by the verifier when there is a catch block in an interested method.
720  // Do this here so that exception classes appear to have been specified image classes.
721  std::set<std::pair<uint16_t, const DexFile*>> unresolved_exception_types;
722  StackHandleScope<1> hs(self);
723  Handle<mirror::Class> java_lang_Throwable(
724      hs.NewHandle(class_linker->FindSystemClass(self, "Ljava/lang/Throwable;")));
725  do {
726    unresolved_exception_types.clear();
727    class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor,
728                               &unresolved_exception_types);
729    for (const std::pair<uint16_t, const DexFile*>& exception_type : unresolved_exception_types) {
730      uint16_t exception_type_idx = exception_type.first;
731      const DexFile* dex_file = exception_type.second;
732      StackHandleScope<2> hs(self);
733      Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(*dex_file)));
734      Handle<mirror::Class> klass(hs.NewHandle(
735          class_linker->ResolveType(*dex_file, exception_type_idx, dex_cache,
736                                    NullHandle<mirror::ClassLoader>())));
737      if (klass.Get() == NULL) {
738        const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
739        const char* descriptor = dex_file->GetTypeDescriptor(type_id);
740        LOG(FATAL) << "Failed to resolve class " << descriptor;
741      }
742      DCHECK(java_lang_Throwable->IsAssignableFrom(klass.Get()));
743    }
744    // Resolving exceptions may load classes that reference more exceptions, iterate until no
745    // more are found
746  } while (!unresolved_exception_types.empty());
747
748  // We walk the roots looking for classes so that we'll pick up the
749  // above classes plus any classes them depend on such super
750  // classes, interfaces, and the required ClassLinker roots.
751  class_linker->VisitClasses(RecordImageClassesVisitor, image_classes_.get());
752
753  CHECK_NE(image_classes_->size(), 0U);
754}
755
756static void MaybeAddToImageClasses(Handle<mirror::Class> c,
757                                   CompilerDriver::DescriptorSet* image_classes)
758    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
759  Thread* self = Thread::Current();
760  StackHandleScope<1> hs(self);
761  // Make a copy of the handle so that we don't clobber it doing Assign.
762  Handle<mirror::Class> klass(hs.NewHandle(c.Get()));
763  while (!klass->IsObjectClass()) {
764    std::string descriptor(klass->GetDescriptor());
765    std::pair<CompilerDriver::DescriptorSet::iterator, bool> result =
766        image_classes->insert(descriptor);
767    if (result.second) {
768        VLOG(compiler) << "Adding " << descriptor << " to image classes";
769    } else {
770      return;
771    }
772    for (size_t i = 0; i < klass->NumDirectInterfaces(); ++i) {
773      StackHandleScope<1> hs(self);
774      MaybeAddToImageClasses(hs.NewHandle(mirror::Class::GetDirectInterface(self, klass, i)),
775                             image_classes);
776    }
777    if (klass->IsArrayClass()) {
778      StackHandleScope<1> hs(self);
779      MaybeAddToImageClasses(hs.NewHandle(klass->GetComponentType()), image_classes);
780    }
781    klass.Assign(klass->GetSuperClass());
782  }
783}
784
785void CompilerDriver::FindClinitImageClassesCallback(mirror::Object* object, void* arg) {
786  DCHECK(object != NULL);
787  DCHECK(arg != NULL);
788  CompilerDriver* compiler_driver = reinterpret_cast<CompilerDriver*>(arg);
789  StackHandleScope<1> hs(Thread::Current());
790  MaybeAddToImageClasses(hs.NewHandle(object->GetClass()), compiler_driver->image_classes_.get());
791}
792
793void CompilerDriver::UpdateImageClasses(TimingLogger* timings) {
794  if (IsImage()) {
795    timings->NewSplit("UpdateImageClasses");
796
797    // Update image_classes_ with classes for objects created by <clinit> methods.
798    Thread* self = Thread::Current();
799    const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
800    gc::Heap* heap = Runtime::Current()->GetHeap();
801    // TODO: Image spaces only?
802    ScopedObjectAccess soa(Thread::Current());
803    WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
804    heap->VisitObjects(FindClinitImageClassesCallback, this);
805    self->EndAssertNoThreadSuspension(old_cause);
806  }
807}
808
809bool CompilerDriver::CanAssumeTypeIsPresentInDexCache(const DexFile& dex_file, uint32_t type_idx) {
810  if (IsImage() &&
811      IsImageClass(dex_file.StringDataByIdx(dex_file.GetTypeId(type_idx).descriptor_idx_))) {
812    if (kIsDebugBuild) {
813      ScopedObjectAccess soa(Thread::Current());
814      mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
815      mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
816      CHECK(resolved_class != NULL);
817    }
818    stats_->TypeInDexCache();
819    return true;
820  } else {
821    stats_->TypeNotInDexCache();
822    return false;
823  }
824}
825
826bool CompilerDriver::CanAssumeStringIsPresentInDexCache(const DexFile& dex_file,
827                                                        uint32_t string_idx) {
828  // See also Compiler::ResolveDexFile
829
830  bool result = false;
831  if (IsImage()) {
832    // We resolve all const-string strings when building for the image.
833    ScopedObjectAccess soa(Thread::Current());
834    StackHandleScope<1> hs(soa.Self());
835    Handle<mirror::DexCache> dex_cache(
836        hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(dex_file)));
837    Runtime::Current()->GetClassLinker()->ResolveString(dex_file, string_idx, dex_cache);
838    result = true;
839  }
840  if (result) {
841    stats_->StringInDexCache();
842  } else {
843    stats_->StringNotInDexCache();
844  }
845  return result;
846}
847
848bool CompilerDriver::CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file,
849                                                uint32_t type_idx,
850                                                bool* type_known_final, bool* type_known_abstract,
851                                                bool* equals_referrers_class) {
852  if (type_known_final != NULL) {
853    *type_known_final = false;
854  }
855  if (type_known_abstract != NULL) {
856    *type_known_abstract = false;
857  }
858  if (equals_referrers_class != NULL) {
859    *equals_referrers_class = false;
860  }
861  ScopedObjectAccess soa(Thread::Current());
862  mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
863  // Get type from dex cache assuming it was populated by the verifier
864  mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
865  if (resolved_class == NULL) {
866    stats_->TypeNeedsAccessCheck();
867    return false;  // Unknown class needs access checks.
868  }
869  const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
870  if (equals_referrers_class != NULL) {
871    *equals_referrers_class = (method_id.class_idx_ == type_idx);
872  }
873  mirror::Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
874  if (referrer_class == NULL) {
875    stats_->TypeNeedsAccessCheck();
876    return false;  // Incomplete referrer knowledge needs access check.
877  }
878  // Perform access check, will return true if access is ok or false if we're going to have to
879  // check this at runtime (for example for class loaders).
880  bool result = referrer_class->CanAccess(resolved_class);
881  if (result) {
882    stats_->TypeDoesntNeedAccessCheck();
883    if (type_known_final != NULL) {
884      *type_known_final = resolved_class->IsFinal() && !resolved_class->IsArrayClass();
885    }
886    if (type_known_abstract != NULL) {
887      *type_known_abstract = resolved_class->IsAbstract() && !resolved_class->IsArrayClass();
888    }
889  } else {
890    stats_->TypeNeedsAccessCheck();
891  }
892  return result;
893}
894
895bool CompilerDriver::CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx,
896                                                            const DexFile& dex_file,
897                                                            uint32_t type_idx) {
898  ScopedObjectAccess soa(Thread::Current());
899  mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
900  // Get type from dex cache assuming it was populated by the verifier.
901  mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
902  if (resolved_class == NULL) {
903    stats_->TypeNeedsAccessCheck();
904    return false;  // Unknown class needs access checks.
905  }
906  const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
907  mirror::Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
908  if (referrer_class == NULL) {
909    stats_->TypeNeedsAccessCheck();
910    return false;  // Incomplete referrer knowledge needs access check.
911  }
912  // Perform access and instantiable checks, will return true if access is ok or false if we're
913  // going to have to check this at runtime (for example for class loaders).
914  bool result = referrer_class->CanAccess(resolved_class) && resolved_class->IsInstantiable();
915  if (result) {
916    stats_->TypeDoesntNeedAccessCheck();
917  } else {
918    stats_->TypeNeedsAccessCheck();
919  }
920  return result;
921}
922
923bool CompilerDriver::CanEmbedTypeInCode(const DexFile& dex_file, uint32_t type_idx,
924                                        bool* is_type_initialized, bool* use_direct_type_ptr,
925                                        uintptr_t* direct_type_ptr, bool* out_is_finalizable) {
926  ScopedObjectAccess soa(Thread::Current());
927  mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
928  mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
929  if (resolved_class == nullptr) {
930    return false;
931  }
932  *out_is_finalizable = resolved_class->IsFinalizable();
933  const bool compiling_boot = Runtime::Current()->GetHeap()->IsCompilingBoot();
934  if (compiling_boot) {
935    // boot -> boot class pointers.
936    // True if the class is in the image at boot compiling time.
937    const bool is_image_class = IsImage() && IsImageClass(
938        dex_file.StringDataByIdx(dex_file.GetTypeId(type_idx).descriptor_idx_));
939    // True if pc relative load works.
940    const bool support_boot_image_fixup = GetSupportBootImageFixup();
941    if (is_image_class && support_boot_image_fixup) {
942      *is_type_initialized = resolved_class->IsInitialized();
943      *use_direct_type_ptr = false;
944      *direct_type_ptr = 0;
945      return true;
946    } else {
947      return false;
948    }
949  } else {
950    // True if the class is in the image at app compiling time.
951    const bool class_in_image =
952        Runtime::Current()->GetHeap()->FindSpaceFromObject(resolved_class, false)->IsImageSpace();
953    if (class_in_image) {
954      // boot -> app class pointers.
955      *is_type_initialized = resolved_class->IsInitialized();
956      *use_direct_type_ptr = true;
957      *direct_type_ptr = reinterpret_cast<uintptr_t>(resolved_class);
958      return true;
959    } else {
960      // app -> app class pointers.
961      // Give up because app does not have an image and class
962      // isn't created at compile time.  TODO: implement this
963      // if/when each app gets an image.
964      return false;
965    }
966  }
967}
968
969void CompilerDriver::ProcessedInstanceField(bool resolved) {
970  if (!resolved) {
971    stats_->UnresolvedInstanceField();
972  } else {
973    stats_->ResolvedInstanceField();
974  }
975}
976
977void CompilerDriver::ProcessedStaticField(bool resolved, bool local) {
978  if (!resolved) {
979    stats_->UnresolvedStaticField();
980  } else if (local) {
981    stats_->ResolvedLocalStaticField();
982  } else {
983    stats_->ResolvedStaticField();
984  }
985}
986
987void CompilerDriver::ProcessedInvoke(InvokeType invoke_type, int flags) {
988  stats_->ProcessedInvoke(invoke_type, flags);
989}
990
991bool CompilerDriver::ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit,
992                                              bool is_put, MemberOffset* field_offset,
993                                              bool* is_volatile) {
994  ScopedObjectAccess soa(Thread::Current());
995  // Try to resolve the field and compiling method's class.
996  mirror::ArtField* resolved_field;
997  mirror::Class* referrer_class;
998  mirror::DexCache* dex_cache;
999  {
1000    StackHandleScope<3> hs(soa.Self());
1001    Handle<mirror::DexCache> dex_cache_handle(
1002        hs.NewHandle(mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile())));
1003    Handle<mirror::ClassLoader> class_loader_handle(
1004        hs.NewHandle(soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader())));
1005    Handle<mirror::ArtField> resolved_field_handle(hs.NewHandle(
1006        ResolveField(soa, dex_cache_handle, class_loader_handle, mUnit, field_idx, false)));
1007    referrer_class = (resolved_field_handle.Get() != nullptr)
1008        ? ResolveCompilingMethodsClass(soa, dex_cache_handle, class_loader_handle, mUnit) : nullptr;
1009    resolved_field = resolved_field_handle.Get();
1010    dex_cache = dex_cache_handle.Get();
1011  }
1012  bool result = false;
1013  if (resolved_field != nullptr && referrer_class != nullptr) {
1014    *is_volatile = IsFieldVolatile(resolved_field);
1015    std::pair<bool, bool> fast_path = IsFastInstanceField(
1016        dex_cache, referrer_class, resolved_field, field_idx, field_offset);
1017    result = is_put ? fast_path.second : fast_path.first;
1018  }
1019  if (!result) {
1020    // Conservative defaults.
1021    *is_volatile = true;
1022    *field_offset = MemberOffset(static_cast<size_t>(-1));
1023  }
1024  ProcessedInstanceField(result);
1025  return result;
1026}
1027
1028bool CompilerDriver::ComputeStaticFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit,
1029                                            bool is_put, MemberOffset* field_offset,
1030                                            uint32_t* storage_index, bool* is_referrers_class,
1031                                            bool* is_volatile, bool* is_initialized) {
1032  ScopedObjectAccess soa(Thread::Current());
1033  // Try to resolve the field and compiling method's class.
1034  mirror::ArtField* resolved_field;
1035  mirror::Class* referrer_class;
1036  mirror::DexCache* dex_cache;
1037  {
1038    StackHandleScope<3> hs(soa.Self());
1039    Handle<mirror::DexCache> dex_cache_handle(
1040        hs.NewHandle(mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile())));
1041    Handle<mirror::ClassLoader> class_loader_handle(
1042        hs.NewHandle(soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader())));
1043    Handle<mirror::ArtField> resolved_field_handle(hs.NewHandle(
1044        ResolveField(soa, dex_cache_handle, class_loader_handle, mUnit, field_idx, true)));
1045    referrer_class = (resolved_field_handle.Get() != nullptr)
1046        ? ResolveCompilingMethodsClass(soa, dex_cache_handle, class_loader_handle, mUnit) : nullptr;
1047    resolved_field = resolved_field_handle.Get();
1048    dex_cache = dex_cache_handle.Get();
1049  }
1050  bool result = false;
1051  if (resolved_field != nullptr && referrer_class != nullptr) {
1052    *is_volatile = IsFieldVolatile(resolved_field);
1053    std::pair<bool, bool> fast_path = IsFastStaticField(
1054        dex_cache, referrer_class, resolved_field, field_idx, field_offset,
1055        storage_index, is_referrers_class, is_initialized);
1056    result = is_put ? fast_path.second : fast_path.first;
1057  }
1058  if (!result) {
1059    // Conservative defaults.
1060    *is_volatile = true;
1061    *field_offset = MemberOffset(static_cast<size_t>(-1));
1062    *storage_index = -1;
1063    *is_referrers_class = false;
1064    *is_initialized = false;
1065  }
1066  ProcessedStaticField(result, *is_referrers_class);
1067  return result;
1068}
1069
1070void CompilerDriver::GetCodeAndMethodForDirectCall(InvokeType* type, InvokeType sharp_type,
1071                                                   bool no_guarantee_of_dex_cache_entry,
1072                                                   mirror::Class* referrer_class,
1073                                                   mirror::ArtMethod* method,
1074                                                   int* stats_flags,
1075                                                   MethodReference* target_method,
1076                                                   uintptr_t* direct_code,
1077                                                   uintptr_t* direct_method) {
1078  // For direct and static methods compute possible direct_code and direct_method values, ie
1079  // an address for the Method* being invoked and an address of the code for that Method*.
1080  // For interface calls compute a value for direct_method that is the interface method being
1081  // invoked, so this can be passed to the out-of-line runtime support code.
1082  *direct_code = 0;
1083  *direct_method = 0;
1084  bool use_dex_cache = false;
1085  const bool compiling_boot = Runtime::Current()->GetHeap()->IsCompilingBoot();
1086  if (compiler_->IsPortable()) {
1087    if (sharp_type != kStatic && sharp_type != kDirect) {
1088      return;
1089    }
1090    use_dex_cache = true;
1091  } else {
1092    if (sharp_type != kStatic && sharp_type != kDirect) {
1093      return;
1094    }
1095    // TODO: support patching on all architectures.
1096    use_dex_cache = compiling_boot && !support_boot_image_fixup_;
1097  }
1098  bool method_code_in_boot = (method->GetDeclaringClass()->GetClassLoader() == nullptr);
1099  if (!use_dex_cache) {
1100    if (!method_code_in_boot) {
1101      use_dex_cache = true;
1102    } else {
1103      bool has_clinit_trampoline =
1104          method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
1105      if (has_clinit_trampoline && (method->GetDeclaringClass() != referrer_class)) {
1106        // Ensure we run the clinit trampoline unless we are invoking a static method in the same
1107        // class.
1108        use_dex_cache = true;
1109      }
1110    }
1111  }
1112  if (method_code_in_boot) {
1113    *stats_flags |= kFlagDirectCallToBoot | kFlagDirectMethodToBoot;
1114  }
1115  if (!use_dex_cache && compiling_boot) {
1116    MethodHelper mh(method);
1117    if (!IsImageClass(mh.GetDeclaringClassDescriptor())) {
1118      // We can only branch directly to Methods that are resolved in the DexCache.
1119      // Otherwise we won't invoke the resolution trampoline.
1120      use_dex_cache = true;
1121    }
1122  }
1123  // The method is defined not within this dex file. We need a dex cache slot within the current
1124  // dex file or direct pointers.
1125  bool must_use_direct_pointers = false;
1126  if (target_method->dex_file == method->GetDeclaringClass()->GetDexCache()->GetDexFile()) {
1127    target_method->dex_method_index = method->GetDexMethodIndex();
1128  } else {
1129    if (no_guarantee_of_dex_cache_entry) {
1130      // See if the method is also declared in this dex cache.
1131      uint32_t dex_method_idx = MethodHelper(method).FindDexMethodIndexInOtherDexFile(
1132          *target_method->dex_file, target_method->dex_method_index);
1133      if (dex_method_idx != DexFile::kDexNoIndex) {
1134        target_method->dex_method_index = dex_method_idx;
1135      } else {
1136        if (compiling_boot && !use_dex_cache) {
1137          target_method->dex_method_index = method->GetDexMethodIndex();
1138          target_method->dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1139        }
1140        must_use_direct_pointers = true;
1141      }
1142    }
1143  }
1144  if (use_dex_cache) {
1145    if (must_use_direct_pointers) {
1146      // Fail. Test above showed the only safe dispatch was via the dex cache, however, the direct
1147      // pointers are required as the dex cache lacks an appropriate entry.
1148      VLOG(compiler) << "Dex cache devirtualization failed for: " << PrettyMethod(method);
1149    } else {
1150      *type = sharp_type;
1151    }
1152  } else {
1153    bool method_in_image = compiling_boot ||
1154        Runtime::Current()->GetHeap()->FindSpaceFromObject(method, false)->IsImageSpace();
1155    if (method_in_image) {
1156      CHECK(!method->IsAbstract());
1157      *type = sharp_type;
1158      *direct_method = compiling_boot ? -1 : reinterpret_cast<uintptr_t>(method);
1159      *direct_code = compiling_boot ? -1 : compiler_->GetEntryPointOf(method);
1160      target_method->dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1161      target_method->dex_method_index = method->GetDexMethodIndex();
1162    } else if (!must_use_direct_pointers) {
1163      // Set the code and rely on the dex cache for the method.
1164      *type = sharp_type;
1165      *direct_code = compiler_->GetEntryPointOf(method);
1166    } else {
1167      // Direct pointers were required but none were available.
1168      VLOG(compiler) << "Dex cache devirtualization failed for: " << PrettyMethod(method);
1169    }
1170  }
1171}
1172
1173bool CompilerDriver::ComputeInvokeInfo(const DexCompilationUnit* mUnit, const uint32_t dex_pc,
1174                                       bool update_stats, bool enable_devirtualization,
1175                                       InvokeType* invoke_type, MethodReference* target_method,
1176                                       int* vtable_idx, uintptr_t* direct_code,
1177                                       uintptr_t* direct_method) {
1178  InvokeType orig_invoke_type = *invoke_type;
1179  int stats_flags = 0;
1180  ScopedObjectAccess soa(Thread::Current());
1181  // Try to resolve the method and compiling method's class.
1182  mirror::ArtMethod* resolved_method;
1183  mirror::Class* referrer_class;
1184  StackHandleScope<3> hs(soa.Self());
1185  Handle<mirror::DexCache> dex_cache(
1186      hs.NewHandle(mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile())));
1187  Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1188      soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader())));
1189  {
1190    uint32_t method_idx = target_method->dex_method_index;
1191    Handle<mirror::ArtMethod> resolved_method_handle(hs.NewHandle(
1192        ResolveMethod(soa, dex_cache, class_loader, mUnit, method_idx, orig_invoke_type)));
1193    referrer_class = (resolved_method_handle.Get() != nullptr)
1194        ? ResolveCompilingMethodsClass(soa, dex_cache, class_loader, mUnit) : nullptr;
1195    resolved_method = resolved_method_handle.Get();
1196  }
1197  bool result = false;
1198  if (resolved_method != nullptr) {
1199    *vtable_idx = GetResolvedMethodVTableIndex(resolved_method, orig_invoke_type);
1200
1201    if (enable_devirtualization) {
1202      DCHECK(mUnit->GetVerifiedMethod() != nullptr);
1203      const MethodReference* devirt_target = mUnit->GetVerifiedMethod()->GetDevirtTarget(dex_pc);
1204
1205      stats_flags = IsFastInvoke(
1206          soa, dex_cache, class_loader, mUnit, referrer_class, resolved_method,
1207          invoke_type, target_method, devirt_target, direct_code, direct_method);
1208      result = stats_flags != 0;
1209    } else {
1210      // Devirtualization not enabled. Inline IsFastInvoke(), dropping the devirtualization parts.
1211      if (UNLIKELY(referrer_class == nullptr) ||
1212          UNLIKELY(!referrer_class->CanAccessResolvedMethod(resolved_method->GetDeclaringClass(),
1213                                                            resolved_method, dex_cache.Get(),
1214                                                            target_method->dex_method_index)) ||
1215          *invoke_type == kSuper) {
1216        // Slow path. (Without devirtualization, all super calls go slow path as well.)
1217      } else {
1218        // Sharpening failed so generate a regular resolved method dispatch.
1219        stats_flags = kFlagMethodResolved;
1220        GetCodeAndMethodForDirectCall(invoke_type, *invoke_type, false, referrer_class, resolved_method,
1221                                      &stats_flags, target_method, direct_code, direct_method);
1222        result = true;
1223      }
1224    }
1225  }
1226  if (!result) {
1227    // Conservative defaults.
1228    *vtable_idx = -1;
1229    *direct_code = 0u;
1230    *direct_method = 0u;
1231  }
1232  if (update_stats) {
1233    ProcessedInvoke(orig_invoke_type, stats_flags);
1234  }
1235  return result;
1236}
1237
1238const VerifiedMethod* CompilerDriver::GetVerifiedMethod(const DexFile* dex_file,
1239                                                        uint32_t method_idx) const {
1240  MethodReference ref(dex_file, method_idx);
1241  return verification_results_->GetVerifiedMethod(ref);
1242}
1243
1244bool CompilerDriver::IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc) {
1245  DCHECK(mUnit->GetVerifiedMethod() != nullptr);
1246  bool result = mUnit->GetVerifiedMethod()->IsSafeCast(dex_pc);
1247  if (result) {
1248    stats_->SafeCast();
1249  } else {
1250    stats_->NotASafeCast();
1251  }
1252  return result;
1253}
1254
1255void CompilerDriver::AddCodePatch(const DexFile* dex_file,
1256                                  uint16_t referrer_class_def_idx,
1257                                  uint32_t referrer_method_idx,
1258                                  InvokeType referrer_invoke_type,
1259                                  uint32_t target_method_idx,
1260                                  const DexFile* target_dex_file,
1261                                  InvokeType target_invoke_type,
1262                                  size_t literal_offset) {
1263  MutexLock mu(Thread::Current(), compiled_methods_lock_);
1264  code_to_patch_.push_back(new CallPatchInformation(dex_file,
1265                                                    referrer_class_def_idx,
1266                                                    referrer_method_idx,
1267                                                    referrer_invoke_type,
1268                                                    target_method_idx,
1269                                                    target_dex_file,
1270                                                    target_invoke_type,
1271                                                    literal_offset));
1272}
1273void CompilerDriver::AddRelativeCodePatch(const DexFile* dex_file,
1274                                          uint16_t referrer_class_def_idx,
1275                                          uint32_t referrer_method_idx,
1276                                          InvokeType referrer_invoke_type,
1277                                          uint32_t target_method_idx,
1278                                          const DexFile* target_dex_file,
1279                                          InvokeType target_invoke_type,
1280                                          size_t literal_offset,
1281                                          int32_t pc_relative_offset) {
1282  MutexLock mu(Thread::Current(), compiled_methods_lock_);
1283  code_to_patch_.push_back(new RelativeCallPatchInformation(dex_file,
1284                                                            referrer_class_def_idx,
1285                                                            referrer_method_idx,
1286                                                            referrer_invoke_type,
1287                                                            target_method_idx,
1288                                                            target_dex_file,
1289                                                            target_invoke_type,
1290                                                            literal_offset,
1291                                                            pc_relative_offset));
1292}
1293void CompilerDriver::AddMethodPatch(const DexFile* dex_file,
1294                                    uint16_t referrer_class_def_idx,
1295                                    uint32_t referrer_method_idx,
1296                                    InvokeType referrer_invoke_type,
1297                                    uint32_t target_method_idx,
1298                                    const DexFile* target_dex_file,
1299                                    InvokeType target_invoke_type,
1300                                    size_t literal_offset) {
1301  MutexLock mu(Thread::Current(), compiled_methods_lock_);
1302  methods_to_patch_.push_back(new CallPatchInformation(dex_file,
1303                                                       referrer_class_def_idx,
1304                                                       referrer_method_idx,
1305                                                       referrer_invoke_type,
1306                                                       target_method_idx,
1307                                                       target_dex_file,
1308                                                       target_invoke_type,
1309                                                       literal_offset));
1310}
1311void CompilerDriver::AddClassPatch(const DexFile* dex_file,
1312                                    uint16_t referrer_class_def_idx,
1313                                    uint32_t referrer_method_idx,
1314                                    uint32_t target_type_idx,
1315                                    size_t literal_offset) {
1316  MutexLock mu(Thread::Current(), compiled_methods_lock_);
1317  classes_to_patch_.push_back(new TypePatchInformation(dex_file,
1318                                                       referrer_class_def_idx,
1319                                                       referrer_method_idx,
1320                                                       target_type_idx,
1321                                                       literal_offset));
1322}
1323
1324class ParallelCompilationManager {
1325 public:
1326  typedef void Callback(const ParallelCompilationManager* manager, size_t index);
1327
1328  ParallelCompilationManager(ClassLinker* class_linker,
1329                             jobject class_loader,
1330                             CompilerDriver* compiler,
1331                             const DexFile* dex_file,
1332                             ThreadPool* thread_pool)
1333    : index_(0),
1334      class_linker_(class_linker),
1335      class_loader_(class_loader),
1336      compiler_(compiler),
1337      dex_file_(dex_file),
1338      thread_pool_(thread_pool) {}
1339
1340  ClassLinker* GetClassLinker() const {
1341    CHECK(class_linker_ != NULL);
1342    return class_linker_;
1343  }
1344
1345  jobject GetClassLoader() const {
1346    return class_loader_;
1347  }
1348
1349  CompilerDriver* GetCompiler() const {
1350    CHECK(compiler_ != NULL);
1351    return compiler_;
1352  }
1353
1354  const DexFile* GetDexFile() const {
1355    CHECK(dex_file_ != NULL);
1356    return dex_file_;
1357  }
1358
1359  void ForAll(size_t begin, size_t end, Callback callback, size_t work_units) {
1360    Thread* self = Thread::Current();
1361    self->AssertNoPendingException();
1362    CHECK_GT(work_units, 0U);
1363
1364    index_.StoreRelaxed(begin);
1365    for (size_t i = 0; i < work_units; ++i) {
1366      thread_pool_->AddTask(self, new ForAllClosure(this, end, callback));
1367    }
1368    thread_pool_->StartWorkers(self);
1369
1370    // Ensure we're suspended while we're blocked waiting for the other threads to finish (worker
1371    // thread destructor's called below perform join).
1372    CHECK_NE(self->GetState(), kRunnable);
1373
1374    // Wait for all the worker threads to finish.
1375    thread_pool_->Wait(self, true, false);
1376  }
1377
1378  size_t NextIndex() {
1379    return index_.FetchAndAddSequentiallyConsistent(1);
1380  }
1381
1382 private:
1383  class ForAllClosure : public Task {
1384   public:
1385    ForAllClosure(ParallelCompilationManager* manager, size_t end, Callback* callback)
1386        : manager_(manager),
1387          end_(end),
1388          callback_(callback) {}
1389
1390    virtual void Run(Thread* self) {
1391      while (true) {
1392        const size_t index = manager_->NextIndex();
1393        if (UNLIKELY(index >= end_)) {
1394          break;
1395        }
1396        callback_(manager_, index);
1397        self->AssertNoPendingException();
1398      }
1399    }
1400
1401    virtual void Finalize() {
1402      delete this;
1403    }
1404
1405   private:
1406    ParallelCompilationManager* const manager_;
1407    const size_t end_;
1408    Callback* const callback_;
1409  };
1410
1411  AtomicInteger index_;
1412  ClassLinker* const class_linker_;
1413  const jobject class_loader_;
1414  CompilerDriver* const compiler_;
1415  const DexFile* const dex_file_;
1416  ThreadPool* const thread_pool_;
1417
1418  DISALLOW_COPY_AND_ASSIGN(ParallelCompilationManager);
1419};
1420
1421// Return true if the class should be skipped during compilation.
1422//
1423// The first case where we skip is for redundant class definitions in
1424// the boot classpath. We skip all but the first definition in that case.
1425//
1426// The second case where we skip is when an app bundles classes found
1427// in the boot classpath. Since at runtime we will select the class from
1428// the boot classpath, we ignore the one from the app.
1429static bool SkipClass(ClassLinker* class_linker, jobject class_loader, const DexFile& dex_file,
1430                      const DexFile::ClassDef& class_def) {
1431  const char* descriptor = dex_file.GetClassDescriptor(class_def);
1432  if (class_loader == NULL) {
1433    DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, class_linker->GetBootClassPath());
1434    CHECK(pair.second != NULL);
1435    if (pair.first != &dex_file) {
1436      LOG(WARNING) << "Skipping class " << descriptor << " from " << dex_file.GetLocation()
1437                   << " previously found in " << pair.first->GetLocation();
1438      return true;
1439    }
1440    return false;
1441  }
1442  return class_linker->IsInBootClassPath(descriptor);
1443}
1444
1445// A fast version of SkipClass above if the class pointer is available
1446// that avoids the expensive FindInClassPath search.
1447static bool SkipClass(jobject class_loader, const DexFile& dex_file, mirror::Class* klass)
1448    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1449  DCHECK(klass != NULL);
1450  const DexFile& original_dex_file = *klass->GetDexCache()->GetDexFile();
1451  if (&dex_file != &original_dex_file) {
1452    if (class_loader == NULL) {
1453      LOG(WARNING) << "Skipping class " << PrettyDescriptor(klass) << " from "
1454                   << dex_file.GetLocation() << " previously found in "
1455                   << original_dex_file.GetLocation();
1456    }
1457    return true;
1458  }
1459  return false;
1460}
1461
1462static void ResolveClassFieldsAndMethods(const ParallelCompilationManager* manager,
1463                                         size_t class_def_index)
1464    LOCKS_EXCLUDED(Locks::mutator_lock_) {
1465  ATRACE_CALL();
1466  Thread* self = Thread::Current();
1467  jobject jclass_loader = manager->GetClassLoader();
1468  const DexFile& dex_file = *manager->GetDexFile();
1469  ClassLinker* class_linker = manager->GetClassLinker();
1470
1471  // If an instance field is final then we need to have a barrier on the return, static final
1472  // fields are assigned within the lock held for class initialization. Conservatively assume
1473  // constructor barriers are always required.
1474  bool requires_constructor_barrier = true;
1475
1476  // Method and Field are the worst. We can't resolve without either
1477  // context from the code use (to disambiguate virtual vs direct
1478  // method and instance vs static field) or from class
1479  // definitions. While the compiler will resolve what it can as it
1480  // needs it, here we try to resolve fields and methods used in class
1481  // definitions, since many of them many never be referenced by
1482  // generated code.
1483  const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1484  if (!SkipClass(class_linker, jclass_loader, dex_file, class_def)) {
1485    ScopedObjectAccess soa(self);
1486    StackHandleScope<2> hs(soa.Self());
1487    Handle<mirror::ClassLoader> class_loader(
1488        hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
1489    Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(dex_file)));
1490    // Resolve the class.
1491    mirror::Class* klass = class_linker->ResolveType(dex_file, class_def.class_idx_, dex_cache,
1492                                                     class_loader);
1493    bool resolve_fields_and_methods;
1494    if (klass == NULL) {
1495      // Class couldn't be resolved, for example, super-class is in a different dex file. Don't
1496      // attempt to resolve methods and fields when there is no declaring class.
1497      CHECK(soa.Self()->IsExceptionPending());
1498      soa.Self()->ClearException();
1499      resolve_fields_and_methods = false;
1500    } else {
1501      resolve_fields_and_methods = manager->GetCompiler()->IsImage();
1502    }
1503    // Note the class_data pointer advances through the headers,
1504    // static fields, instance fields, direct methods, and virtual
1505    // methods.
1506    const byte* class_data = dex_file.GetClassData(class_def);
1507    if (class_data == NULL) {
1508      // Empty class such as a marker interface.
1509      requires_constructor_barrier = false;
1510    } else {
1511      ClassDataItemIterator it(dex_file, class_data);
1512      while (it.HasNextStaticField()) {
1513        if (resolve_fields_and_methods) {
1514          mirror::ArtField* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(),
1515                                                               dex_cache, class_loader, true);
1516          if (field == NULL) {
1517            CHECK(soa.Self()->IsExceptionPending());
1518            soa.Self()->ClearException();
1519          }
1520        }
1521        it.Next();
1522      }
1523      // We require a constructor barrier if there are final instance fields.
1524      requires_constructor_barrier = false;
1525      while (it.HasNextInstanceField()) {
1526        if ((it.GetMemberAccessFlags() & kAccFinal) != 0) {
1527          requires_constructor_barrier = true;
1528        }
1529        if (resolve_fields_and_methods) {
1530          mirror::ArtField* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(),
1531                                                               dex_cache, class_loader, false);
1532          if (field == NULL) {
1533            CHECK(soa.Self()->IsExceptionPending());
1534            soa.Self()->ClearException();
1535          }
1536        }
1537        it.Next();
1538      }
1539      if (resolve_fields_and_methods) {
1540        while (it.HasNextDirectMethod()) {
1541          mirror::ArtMethod* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(),
1542                                                                  dex_cache, class_loader,
1543                                                                  NullHandle<mirror::ArtMethod>(),
1544                                                                  it.GetMethodInvokeType(class_def));
1545          if (method == NULL) {
1546            CHECK(soa.Self()->IsExceptionPending());
1547            soa.Self()->ClearException();
1548          }
1549          it.Next();
1550        }
1551        while (it.HasNextVirtualMethod()) {
1552          mirror::ArtMethod* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(),
1553                                                                  dex_cache, class_loader,
1554                                                                  NullHandle<mirror::ArtMethod>(),
1555                                                                  it.GetMethodInvokeType(class_def));
1556          if (method == NULL) {
1557            CHECK(soa.Self()->IsExceptionPending());
1558            soa.Self()->ClearException();
1559          }
1560          it.Next();
1561        }
1562        DCHECK(!it.HasNext());
1563      }
1564    }
1565  }
1566  if (requires_constructor_barrier) {
1567    manager->GetCompiler()->AddRequiresConstructorBarrier(self, &dex_file, class_def_index);
1568  }
1569}
1570
1571static void ResolveType(const ParallelCompilationManager* manager, size_t type_idx)
1572    LOCKS_EXCLUDED(Locks::mutator_lock_) {
1573  // Class derived values are more complicated, they require the linker and loader.
1574  ScopedObjectAccess soa(Thread::Current());
1575  ClassLinker* class_linker = manager->GetClassLinker();
1576  const DexFile& dex_file = *manager->GetDexFile();
1577  StackHandleScope<2> hs(soa.Self());
1578  Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(dex_file)));
1579  Handle<mirror::ClassLoader> class_loader(
1580      hs.NewHandle(soa.Decode<mirror::ClassLoader*>(manager->GetClassLoader())));
1581  mirror::Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
1582
1583  if (klass == NULL) {
1584    CHECK(soa.Self()->IsExceptionPending());
1585    mirror::Throwable* exception = soa.Self()->GetException(NULL);
1586    VLOG(compiler) << "Exception during type resolution: " << exception->Dump();
1587    if (exception->GetClass()->DescriptorEquals("Ljava/lang/OutOfMemoryError;")) {
1588      // There's little point continuing compilation if the heap is exhausted.
1589      LOG(FATAL) << "Out of memory during type resolution for compilation";
1590    }
1591    soa.Self()->ClearException();
1592  }
1593}
1594
1595void CompilerDriver::ResolveDexFile(jobject class_loader, const DexFile& dex_file,
1596                                    ThreadPool* thread_pool, TimingLogger* timings) {
1597  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1598
1599  // TODO: we could resolve strings here, although the string table is largely filled with class
1600  //       and method names.
1601
1602  ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, thread_pool);
1603  if (IsImage()) {
1604    // For images we resolve all types, such as array, whereas for applications just those with
1605    // classdefs are resolved by ResolveClassFieldsAndMethods.
1606    timings->NewSplit("Resolve Types");
1607    context.ForAll(0, dex_file.NumTypeIds(), ResolveType, thread_count_);
1608  }
1609
1610  timings->NewSplit("Resolve MethodsAndFields");
1611  context.ForAll(0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_);
1612}
1613
1614void CompilerDriver::Verify(jobject class_loader, const std::vector<const DexFile*>& dex_files,
1615                            ThreadPool* thread_pool, TimingLogger* timings) {
1616  for (size_t i = 0; i != dex_files.size(); ++i) {
1617    const DexFile* dex_file = dex_files[i];
1618    CHECK(dex_file != NULL);
1619    VerifyDexFile(class_loader, *dex_file, thread_pool, timings);
1620  }
1621}
1622
1623static void VerifyClass(const ParallelCompilationManager* manager, size_t class_def_index)
1624    LOCKS_EXCLUDED(Locks::mutator_lock_) {
1625  ATRACE_CALL();
1626  ScopedObjectAccess soa(Thread::Current());
1627  const DexFile& dex_file = *manager->GetDexFile();
1628  const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1629  const char* descriptor = dex_file.GetClassDescriptor(class_def);
1630  ClassLinker* class_linker = manager->GetClassLinker();
1631  jobject jclass_loader = manager->GetClassLoader();
1632  StackHandleScope<3> hs(soa.Self());
1633  Handle<mirror::ClassLoader> class_loader(
1634      hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
1635  Handle<mirror::Class> klass(
1636      hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor, class_loader)));
1637  if (klass.Get() == nullptr) {
1638    CHECK(soa.Self()->IsExceptionPending());
1639    soa.Self()->ClearException();
1640
1641    /*
1642     * At compile time, we can still structurally verify the class even if FindClass fails.
1643     * This is to ensure the class is structurally sound for compilation. An unsound class
1644     * will be rejected by the verifier and later skipped during compilation in the compiler.
1645     */
1646    Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(dex_file)));
1647    std::string error_msg;
1648    if (verifier::MethodVerifier::VerifyClass(&dex_file, dex_cache, class_loader, &class_def, true,
1649                                              &error_msg) ==
1650                                                  verifier::MethodVerifier::kHardFailure) {
1651      LOG(ERROR) << "Verification failed on class " << PrettyDescriptor(descriptor)
1652                 << " because: " << error_msg;
1653    }
1654  } else if (!SkipClass(jclass_loader, dex_file, klass.Get())) {
1655    CHECK(klass->IsResolved()) << PrettyClass(klass.Get());
1656    class_linker->VerifyClass(klass);
1657
1658    if (klass->IsErroneous()) {
1659      // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
1660      CHECK(soa.Self()->IsExceptionPending());
1661      soa.Self()->ClearException();
1662    }
1663
1664    CHECK(klass->IsCompileTimeVerified() || klass->IsErroneous())
1665        << PrettyDescriptor(klass.Get()) << ": state=" << klass->GetStatus();
1666  }
1667  soa.Self()->AssertNoPendingException();
1668}
1669
1670void CompilerDriver::VerifyDexFile(jobject class_loader, const DexFile& dex_file,
1671                                   ThreadPool* thread_pool, TimingLogger* timings) {
1672  timings->NewSplit("Verify Dex File");
1673  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1674  ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, thread_pool);
1675  context.ForAll(0, dex_file.NumClassDefs(), VerifyClass, thread_count_);
1676}
1677
1678static void InitializeClass(const ParallelCompilationManager* manager, size_t class_def_index)
1679    LOCKS_EXCLUDED(Locks::mutator_lock_) {
1680  ATRACE_CALL();
1681  jobject jclass_loader = manager->GetClassLoader();
1682  const DexFile& dex_file = *manager->GetDexFile();
1683  const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1684  const DexFile::TypeId& class_type_id = dex_file.GetTypeId(class_def.class_idx_);
1685  const char* descriptor = dex_file.StringDataByIdx(class_type_id.descriptor_idx_);
1686
1687  ScopedObjectAccess soa(Thread::Current());
1688  StackHandleScope<3> hs(soa.Self());
1689  Handle<mirror::ClassLoader> class_loader(
1690      hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
1691  Handle<mirror::Class> klass(
1692      hs.NewHandle(manager->GetClassLinker()->FindClass(soa.Self(), descriptor, class_loader)));
1693
1694  if (klass.Get() != nullptr && !SkipClass(jclass_loader, dex_file, klass.Get())) {
1695    // Only try to initialize classes that were successfully verified.
1696    if (klass->IsVerified()) {
1697      // Attempt to initialize the class but bail if we either need to initialize the super-class
1698      // or static fields.
1699      manager->GetClassLinker()->EnsureInitialized(klass, false, false);
1700      if (!klass->IsInitialized()) {
1701        // We don't want non-trivial class initialization occurring on multiple threads due to
1702        // deadlock problems. For example, a parent class is initialized (holding its lock) that
1703        // refers to a sub-class in its static/class initializer causing it to try to acquire the
1704        // sub-class' lock. While on a second thread the sub-class is initialized (holding its lock)
1705        // after first initializing its parents, whose locks are acquired. This leads to a
1706        // parent-to-child and a child-to-parent lock ordering and consequent potential deadlock.
1707        // We need to use an ObjectLock due to potential suspension in the interpreting code. Rather
1708        // than use a special Object for the purpose we use the Class of java.lang.Class.
1709        Handle<mirror::Class> h_klass(hs.NewHandle(klass->GetClass()));
1710        ObjectLock<mirror::Class> lock(soa.Self(), h_klass);
1711        // Attempt to initialize allowing initialization of parent classes but still not static
1712        // fields.
1713        manager->GetClassLinker()->EnsureInitialized(klass, false, true);
1714        if (!klass->IsInitialized()) {
1715          // We need to initialize static fields, we only do this for image classes that aren't
1716          // marked with the $NoPreloadHolder (which implies this should not be initialized early).
1717          bool can_init_static_fields = manager->GetCompiler()->IsImage() &&
1718              manager->GetCompiler()->IsImageClass(descriptor) &&
1719              !StringPiece(descriptor).ends_with("$NoPreloadHolder;");
1720          if (can_init_static_fields) {
1721            VLOG(compiler) << "Initializing: " << descriptor;
1722            // TODO multithreading support. We should ensure the current compilation thread has
1723            // exclusive access to the runtime and the transaction. To achieve this, we could use
1724            // a ReaderWriterMutex but we're holding the mutator lock so we fail mutex sanity
1725            // checks in Thread::AssertThreadSuspensionIsAllowable.
1726            Runtime* const runtime = Runtime::Current();
1727            Transaction transaction;
1728
1729            // Run the class initializer in transaction mode.
1730            runtime->EnterTransactionMode(&transaction);
1731            const mirror::Class::Status old_status = klass->GetStatus();
1732            bool success = manager->GetClassLinker()->EnsureInitialized(klass, true, true);
1733            // TODO we detach transaction from runtime to indicate we quit the transactional
1734            // mode which prevents the GC from visiting objects modified during the transaction.
1735            // Ensure GC is not run so don't access freed objects when aborting transaction.
1736            const char* old_casue = soa.Self()->StartAssertNoThreadSuspension("Transaction end");
1737            runtime->ExitTransactionMode();
1738
1739            if (!success) {
1740              CHECK(soa.Self()->IsExceptionPending());
1741              ThrowLocation throw_location;
1742              mirror::Throwable* exception = soa.Self()->GetException(&throw_location);
1743              VLOG(compiler) << "Initialization of " << descriptor << " aborted because of "
1744                  << exception->Dump();
1745              soa.Self()->ClearException();
1746              transaction.Abort();
1747              CHECK_EQ(old_status, klass->GetStatus()) << "Previous class status not restored";
1748            }
1749            soa.Self()->EndAssertNoThreadSuspension(old_casue);
1750          }
1751        }
1752        soa.Self()->AssertNoPendingException();
1753      }
1754    }
1755    // Record the final class status if necessary.
1756    ClassReference ref(manager->GetDexFile(), class_def_index);
1757    manager->GetCompiler()->RecordClassStatus(ref, klass->GetStatus());
1758  }
1759  // Clear any class not found or verification exceptions.
1760  soa.Self()->ClearException();
1761}
1762
1763void CompilerDriver::InitializeClasses(jobject jni_class_loader, const DexFile& dex_file,
1764                                       ThreadPool* thread_pool, TimingLogger* timings) {
1765  timings->NewSplit("InitializeNoClinit");
1766  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1767  ParallelCompilationManager context(class_linker, jni_class_loader, this, &dex_file, thread_pool);
1768  size_t thread_count;
1769  if (IsImage()) {
1770    // TODO: remove this when transactional mode supports multithreading.
1771    thread_count = 1U;
1772  } else {
1773    thread_count = thread_count_;
1774  }
1775  context.ForAll(0, dex_file.NumClassDefs(), InitializeClass, thread_count);
1776  if (IsImage()) {
1777    // Prune garbage objects created during aborted transactions.
1778    Runtime::Current()->GetHeap()->CollectGarbage(true);
1779  }
1780}
1781
1782void CompilerDriver::InitializeClasses(jobject class_loader,
1783                                       const std::vector<const DexFile*>& dex_files,
1784                                       ThreadPool* thread_pool, TimingLogger* timings) {
1785  for (size_t i = 0; i != dex_files.size(); ++i) {
1786    const DexFile* dex_file = dex_files[i];
1787    CHECK(dex_file != NULL);
1788    InitializeClasses(class_loader, *dex_file, thread_pool, timings);
1789  }
1790}
1791
1792void CompilerDriver::Compile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
1793                             ThreadPool* thread_pool, TimingLogger* timings) {
1794  for (size_t i = 0; i != dex_files.size(); ++i) {
1795    const DexFile* dex_file = dex_files[i];
1796    CHECK(dex_file != NULL);
1797    CompileDexFile(class_loader, *dex_file, thread_pool, timings);
1798  }
1799}
1800
1801void CompilerDriver::CompileClass(const ParallelCompilationManager* manager, size_t class_def_index) {
1802  ATRACE_CALL();
1803  jobject jclass_loader = manager->GetClassLoader();
1804  const DexFile& dex_file = *manager->GetDexFile();
1805  const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1806  ClassLinker* class_linker = manager->GetClassLinker();
1807  if (SkipClass(class_linker, jclass_loader, dex_file, class_def)) {
1808    return;
1809  }
1810  ClassReference ref(&dex_file, class_def_index);
1811  // Skip compiling classes with generic verifier failures since they will still fail at runtime
1812  if (manager->GetCompiler()->verification_results_->IsClassRejected(ref)) {
1813    return;
1814  }
1815  const byte* class_data = dex_file.GetClassData(class_def);
1816  if (class_data == NULL) {
1817    // empty class, probably a marker interface
1818    return;
1819  }
1820
1821  // Can we run DEX-to-DEX compiler on this class ?
1822  DexToDexCompilationLevel dex_to_dex_compilation_level = kDontDexToDexCompile;
1823  {
1824    ScopedObjectAccess soa(Thread::Current());
1825    StackHandleScope<1> hs(soa.Self());
1826    Handle<mirror::ClassLoader> class_loader(
1827        hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
1828    dex_to_dex_compilation_level = GetDexToDexCompilationlevel(soa.Self(), class_loader, dex_file,
1829                                                               class_def);
1830  }
1831  ClassDataItemIterator it(dex_file, class_data);
1832  // Skip fields
1833  while (it.HasNextStaticField()) {
1834    it.Next();
1835  }
1836  while (it.HasNextInstanceField()) {
1837    it.Next();
1838  }
1839  CompilerDriver* driver = manager->GetCompiler();
1840  // Compile direct methods
1841  int64_t previous_direct_method_idx = -1;
1842  while (it.HasNextDirectMethod()) {
1843    uint32_t method_idx = it.GetMemberIndex();
1844    if (method_idx == previous_direct_method_idx) {
1845      // smali can create dex files with two encoded_methods sharing the same method_idx
1846      // http://code.google.com/p/smali/issues/detail?id=119
1847      it.Next();
1848      continue;
1849    }
1850    previous_direct_method_idx = method_idx;
1851    driver->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
1852                          it.GetMethodInvokeType(class_def), class_def_index,
1853                          method_idx, jclass_loader, dex_file, dex_to_dex_compilation_level);
1854    it.Next();
1855  }
1856  // Compile virtual methods
1857  int64_t previous_virtual_method_idx = -1;
1858  while (it.HasNextVirtualMethod()) {
1859    uint32_t method_idx = it.GetMemberIndex();
1860    if (method_idx == previous_virtual_method_idx) {
1861      // smali can create dex files with two encoded_methods sharing the same method_idx
1862      // http://code.google.com/p/smali/issues/detail?id=119
1863      it.Next();
1864      continue;
1865    }
1866    previous_virtual_method_idx = method_idx;
1867    driver->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
1868                          it.GetMethodInvokeType(class_def), class_def_index,
1869                          method_idx, jclass_loader, dex_file, dex_to_dex_compilation_level);
1870    it.Next();
1871  }
1872  DCHECK(!it.HasNext());
1873}
1874
1875void CompilerDriver::CompileDexFile(jobject class_loader, const DexFile& dex_file,
1876                                    ThreadPool* thread_pool, TimingLogger* timings) {
1877  timings->NewSplit("Compile Dex File");
1878  ParallelCompilationManager context(Runtime::Current()->GetClassLinker(), class_loader, this,
1879                                     &dex_file, thread_pool);
1880  context.ForAll(0, dex_file.NumClassDefs(), CompilerDriver::CompileClass, thread_count_);
1881}
1882
1883void CompilerDriver::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
1884                                   InvokeType invoke_type, uint16_t class_def_idx,
1885                                   uint32_t method_idx, jobject class_loader,
1886                                   const DexFile& dex_file,
1887                                   DexToDexCompilationLevel dex_to_dex_compilation_level) {
1888  CompiledMethod* compiled_method = NULL;
1889  uint64_t start_ns = NanoTime();
1890
1891  if ((access_flags & kAccNative) != 0) {
1892    // Are we interpreting only and have support for generic JNI down calls?
1893    if (!compiler_options_->IsCompilationEnabled() &&
1894        (instruction_set_ == kX86_64 || instruction_set_ == kArm64)) {
1895      // Leaving this empty will trigger the generic JNI version
1896    } else {
1897      compiled_method = compiler_->JniCompile(access_flags, method_idx, dex_file);
1898      CHECK(compiled_method != NULL);
1899    }
1900  } else if ((access_flags & kAccAbstract) != 0) {
1901  } else {
1902    MethodReference method_ref(&dex_file, method_idx);
1903    bool compile = verification_results_->IsCandidateForCompilation(method_ref, access_flags);
1904    if (compile) {
1905      // NOTE: if compiler declines to compile this method, it will return NULL.
1906      compiled_method = compiler_->Compile(code_item, access_flags, invoke_type, class_def_idx,
1907                                           method_idx, class_loader, dex_file);
1908    }
1909    if (compiled_method == nullptr && dex_to_dex_compilation_level != kDontDexToDexCompile) {
1910      // TODO: add a command-line option to disable DEX-to-DEX compilation ?
1911      (*dex_to_dex_compiler_)(*this, code_item, access_flags,
1912                              invoke_type, class_def_idx,
1913                              method_idx, class_loader, dex_file,
1914                              dex_to_dex_compilation_level);
1915    }
1916  }
1917  uint64_t duration_ns = NanoTime() - start_ns;
1918  if (duration_ns > MsToNs(compiler_->GetMaximumCompilationTimeBeforeWarning())) {
1919    LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
1920                 << " took " << PrettyDuration(duration_ns);
1921  }
1922
1923  Thread* self = Thread::Current();
1924  if (compiled_method != NULL) {
1925    MethodReference ref(&dex_file, method_idx);
1926    DCHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
1927    {
1928      MutexLock mu(self, compiled_methods_lock_);
1929      compiled_methods_.Put(ref, compiled_method);
1930    }
1931    DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
1932  }
1933
1934  if (self->IsExceptionPending()) {
1935    ScopedObjectAccess soa(self);
1936    LOG(FATAL) << "Unexpected exception compiling: " << PrettyMethod(method_idx, dex_file) << "\n"
1937        << self->GetException(NULL)->Dump();
1938  }
1939}
1940
1941CompiledClass* CompilerDriver::GetCompiledClass(ClassReference ref) const {
1942  MutexLock mu(Thread::Current(), compiled_classes_lock_);
1943  ClassTable::const_iterator it = compiled_classes_.find(ref);
1944  if (it == compiled_classes_.end()) {
1945    return NULL;
1946  }
1947  CHECK(it->second != NULL);
1948  return it->second;
1949}
1950
1951void CompilerDriver::RecordClassStatus(ClassReference ref, mirror::Class::Status status) {
1952  MutexLock mu(Thread::Current(), compiled_classes_lock_);
1953  auto it = compiled_classes_.find(ref);
1954  if (it == compiled_classes_.end() || it->second->GetStatus() != status) {
1955    // An entry doesn't exist or the status is lower than the new status.
1956    if (it != compiled_classes_.end()) {
1957      CHECK_GT(status, it->second->GetStatus());
1958      delete it->second;
1959    }
1960    switch (status) {
1961      case mirror::Class::kStatusNotReady:
1962      case mirror::Class::kStatusError:
1963      case mirror::Class::kStatusRetryVerificationAtRuntime:
1964      case mirror::Class::kStatusVerified:
1965      case mirror::Class::kStatusInitialized:
1966        break;  // Expected states.
1967      default:
1968        LOG(FATAL) << "Unexpected class status for class "
1969            << PrettyDescriptor(ref.first->GetClassDescriptor(ref.first->GetClassDef(ref.second)))
1970            << " of " << status;
1971    }
1972    CompiledClass* compiled_class = new CompiledClass(status);
1973    compiled_classes_.Overwrite(ref, compiled_class);
1974  }
1975}
1976
1977CompiledMethod* CompilerDriver::GetCompiledMethod(MethodReference ref) const {
1978  MutexLock mu(Thread::Current(), compiled_methods_lock_);
1979  MethodTable::const_iterator it = compiled_methods_.find(ref);
1980  if (it == compiled_methods_.end()) {
1981    return NULL;
1982  }
1983  CHECK(it->second != NULL);
1984  return it->second;
1985}
1986
1987void CompilerDriver::AddRequiresConstructorBarrier(Thread* self, const DexFile* dex_file,
1988                                                   uint16_t class_def_index) {
1989  WriterMutexLock mu(self, freezing_constructor_lock_);
1990  freezing_constructor_classes_.insert(ClassReference(dex_file, class_def_index));
1991}
1992
1993bool CompilerDriver::RequiresConstructorBarrier(Thread* self, const DexFile* dex_file,
1994                                                uint16_t class_def_index) {
1995  ReaderMutexLock mu(self, freezing_constructor_lock_);
1996  return freezing_constructor_classes_.count(ClassReference(dex_file, class_def_index)) != 0;
1997}
1998
1999bool CompilerDriver::WriteElf(const std::string& android_root,
2000                              bool is_host,
2001                              const std::vector<const art::DexFile*>& dex_files,
2002                              OatWriter* oat_writer,
2003                              art::File* file)
2004    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2005  return compiler_->WriteElf(file, oat_writer, dex_files, android_root, is_host);
2006}
2007void CompilerDriver::InstructionSetToLLVMTarget(InstructionSet instruction_set,
2008                                                std::string* target_triple,
2009                                                std::string* target_cpu,
2010                                                std::string* target_attr) {
2011  switch (instruction_set) {
2012    case kThumb2:
2013      *target_triple = "thumb-none-linux-gnueabi";
2014      *target_cpu = "cortex-a9";
2015      *target_attr = "+thumb2,+neon,+neonfp,+vfp3,+db";
2016      break;
2017
2018    case kArm:
2019      *target_triple = "armv7-none-linux-gnueabi";
2020      // TODO: Fix for Nexus S.
2021      *target_cpu = "cortex-a9";
2022      // TODO: Fix for Xoom.
2023      *target_attr = "+v7,+neon,+neonfp,+vfp3,+db";
2024      break;
2025
2026    case kX86:
2027      *target_triple = "i386-pc-linux-gnu";
2028      *target_attr = "";
2029      break;
2030
2031    case kX86_64:
2032      *target_triple = "x86_64-pc-linux-gnu";
2033      *target_attr = "";
2034      break;
2035
2036    case kMips:
2037      *target_triple = "mipsel-unknown-linux";
2038      *target_attr = "mips32r2";
2039      break;
2040
2041    default:
2042      LOG(FATAL) << "Unknown instruction set: " << instruction_set;
2043    }
2044  }
2045
2046bool CompilerDriver::SkipCompilation(const std::string& method_name) {
2047  if (!profile_ok_) {
2048    return false;
2049  }
2050  // First find the method in the profile file.
2051  ProfileFile::ProfileData data;
2052  if (!profile_file_.GetProfileData(&data, method_name)) {
2053    // Not in profile, no information can be determined.
2054    VLOG(compiler) << "not compiling " << method_name << " because it's not in the profile";
2055    return true;
2056  }
2057
2058  // Methods that comprise top_k_threshold % of the total samples will be compiled.
2059  double top_k_threshold = GetDoubleProperty("dalvik.vm.profiler.compile_thr", 10.0, 90.0, 90.0);
2060
2061  // Compare against the start of the topK percentage bucket just in case the threshold
2062  // falls inside a bucket.
2063  bool compile = data.GetTopKUsedPercentage() - data.GetUsedPercent() <= top_k_threshold;
2064  if (compile) {
2065    LOG(INFO) << "compiling method " << method_name << " because its usage is part of top "
2066        << data.GetTopKUsedPercentage() << "% with a percent of " << data.GetUsedPercent() << "%"
2067        << " (topKThreshold=" << top_k_threshold << ")";
2068  } else {
2069    VLOG(compiler) << "not compiling method " << method_name << " because it's not part of leading "
2070        << top_k_threshold << "% samples)";
2071  }
2072  return !compile;
2073}
2074}  // namespace art
2075