compiler_driver.cc revision d9c90373d640a5e08072cf469c372e24a8c0fc35
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#include <unordered_set>
20#include <vector>
21#include <unistd.h>
22
23#ifndef __APPLE__
24#include <malloc.h>  // For mallinfo
25#endif
26
27#include "art_field-inl.h"
28#include "art_method-inl.h"
29#include "base/array_ref.h"
30#include "base/bit_vector.h"
31#include "base/enums.h"
32#include "base/stl_util.h"
33#include "base/systrace.h"
34#include "base/time_utils.h"
35#include "base/timing_logger.h"
36#include "class_linker-inl.h"
37#include "compiled_class.h"
38#include "compiled_method.h"
39#include "compiler.h"
40#include "compiler_driver-inl.h"
41#include "dex_compilation_unit.h"
42#include "dex_file-inl.h"
43#include "dex_instruction-inl.h"
44#include "dex/dex_to_dex_compiler.h"
45#include "dex/verification_results.h"
46#include "dex/verified_method.h"
47#include "dex/quick/dex_file_method_inliner.h"
48#include "dex/quick/dex_file_to_method_inliner_map.h"
49#include "driver/compiler_options.h"
50#include "jni_internal.h"
51#include "object_lock.h"
52#include "runtime.h"
53#include "gc/accounting/card_table-inl.h"
54#include "gc/accounting/heap_bitmap.h"
55#include "gc/space/image_space.h"
56#include "gc/space/space.h"
57#include "mirror/class_loader.h"
58#include "mirror/class-inl.h"
59#include "mirror/dex_cache-inl.h"
60#include "mirror/object-inl.h"
61#include "mirror/object_array-inl.h"
62#include "mirror/throwable.h"
63#include "scoped_thread_state_change.h"
64#include "ScopedLocalRef.h"
65#include "handle_scope-inl.h"
66#include "thread.h"
67#include "thread_list.h"
68#include "thread_pool.h"
69#include "trampolines/trampoline_compiler.h"
70#include "transaction.h"
71#include "utils/dex_cache_arrays_layout-inl.h"
72#include "utils/swap_space.h"
73#include "verifier/method_verifier.h"
74#include "verifier/method_verifier-inl.h"
75#include "verifier/verifier_log_mode.h"
76
77namespace art {
78
79static constexpr bool kTimeCompileMethod = !kIsDebugBuild;
80
81// Print additional info during profile guided compilation.
82static constexpr bool kDebugProfileGuidedCompilation = false;
83
84static double Percentage(size_t x, size_t y) {
85  return 100.0 * (static_cast<double>(x)) / (static_cast<double>(x + y));
86}
87
88static void DumpStat(size_t x, size_t y, const char* str) {
89  if (x == 0 && y == 0) {
90    return;
91  }
92  LOG(INFO) << Percentage(x, y) << "% of " << str << " for " << (x + y) << " cases";
93}
94
95class CompilerDriver::AOTCompilationStats {
96 public:
97  AOTCompilationStats()
98      : stats_lock_("AOT compilation statistics lock"),
99        types_in_dex_cache_(0), types_not_in_dex_cache_(0),
100        strings_in_dex_cache_(0), strings_not_in_dex_cache_(0),
101        resolved_types_(0), unresolved_types_(0),
102        resolved_instance_fields_(0), unresolved_instance_fields_(0),
103        resolved_local_static_fields_(0), resolved_static_fields_(0), unresolved_static_fields_(0),
104        type_based_devirtualization_(0),
105        safe_casts_(0), not_safe_casts_(0) {
106    for (size_t i = 0; i <= kMaxInvokeType; i++) {
107      resolved_methods_[i] = 0;
108      unresolved_methods_[i] = 0;
109      virtual_made_direct_[i] = 0;
110      direct_calls_to_boot_[i] = 0;
111      direct_methods_to_boot_[i] = 0;
112    }
113  }
114
115  void Dump() {
116    DumpStat(types_in_dex_cache_, types_not_in_dex_cache_, "types known to be in dex cache");
117    DumpStat(strings_in_dex_cache_, strings_not_in_dex_cache_, "strings known to be in dex cache");
118    DumpStat(resolved_types_, unresolved_types_, "types resolved");
119    DumpStat(resolved_instance_fields_, unresolved_instance_fields_, "instance fields resolved");
120    DumpStat(resolved_local_static_fields_ + resolved_static_fields_, unresolved_static_fields_,
121             "static fields resolved");
122    DumpStat(resolved_local_static_fields_, resolved_static_fields_ + unresolved_static_fields_,
123             "static fields local to a class");
124    DumpStat(safe_casts_, not_safe_casts_, "check-casts removed based on type information");
125    // Note, the code below subtracts the stat value so that when added to the stat value we have
126    // 100% of samples. TODO: clean this up.
127    DumpStat(type_based_devirtualization_,
128             resolved_methods_[kVirtual] + unresolved_methods_[kVirtual] +
129             resolved_methods_[kInterface] + unresolved_methods_[kInterface] -
130             type_based_devirtualization_,
131             "virtual/interface calls made direct based on type information");
132
133    for (size_t i = 0; i <= kMaxInvokeType; i++) {
134      std::ostringstream oss;
135      oss << static_cast<InvokeType>(i) << " methods were AOT resolved";
136      DumpStat(resolved_methods_[i], unresolved_methods_[i], oss.str().c_str());
137      if (virtual_made_direct_[i] > 0) {
138        std::ostringstream oss2;
139        oss2 << static_cast<InvokeType>(i) << " methods made direct";
140        DumpStat(virtual_made_direct_[i],
141                 resolved_methods_[i] + unresolved_methods_[i] - virtual_made_direct_[i],
142                 oss2.str().c_str());
143      }
144      if (direct_calls_to_boot_[i] > 0) {
145        std::ostringstream oss2;
146        oss2 << static_cast<InvokeType>(i) << " method calls are direct into boot";
147        DumpStat(direct_calls_to_boot_[i],
148                 resolved_methods_[i] + unresolved_methods_[i] - direct_calls_to_boot_[i],
149                 oss2.str().c_str());
150      }
151      if (direct_methods_to_boot_[i] > 0) {
152        std::ostringstream oss2;
153        oss2 << static_cast<InvokeType>(i) << " method calls have methods in boot";
154        DumpStat(direct_methods_to_boot_[i],
155                 resolved_methods_[i] + unresolved_methods_[i] - direct_methods_to_boot_[i],
156                 oss2.str().c_str());
157      }
158    }
159  }
160
161// Allow lossy statistics in non-debug builds.
162#ifndef NDEBUG
163#define STATS_LOCK() MutexLock mu(Thread::Current(), stats_lock_)
164#else
165#define STATS_LOCK()
166#endif
167
168  void TypeInDexCache() REQUIRES(!stats_lock_) {
169    STATS_LOCK();
170    types_in_dex_cache_++;
171  }
172
173  void TypeNotInDexCache() REQUIRES(!stats_lock_) {
174    STATS_LOCK();
175    types_not_in_dex_cache_++;
176  }
177
178  void StringInDexCache() REQUIRES(!stats_lock_) {
179    STATS_LOCK();
180    strings_in_dex_cache_++;
181  }
182
183  void StringNotInDexCache() REQUIRES(!stats_lock_) {
184    STATS_LOCK();
185    strings_not_in_dex_cache_++;
186  }
187
188  void TypeDoesntNeedAccessCheck() REQUIRES(!stats_lock_) {
189    STATS_LOCK();
190    resolved_types_++;
191  }
192
193  void TypeNeedsAccessCheck() REQUIRES(!stats_lock_) {
194    STATS_LOCK();
195    unresolved_types_++;
196  }
197
198  void ResolvedInstanceField() REQUIRES(!stats_lock_) {
199    STATS_LOCK();
200    resolved_instance_fields_++;
201  }
202
203  void UnresolvedInstanceField() REQUIRES(!stats_lock_) {
204    STATS_LOCK();
205    unresolved_instance_fields_++;
206  }
207
208  void ResolvedLocalStaticField() REQUIRES(!stats_lock_) {
209    STATS_LOCK();
210    resolved_local_static_fields_++;
211  }
212
213  void ResolvedStaticField() REQUIRES(!stats_lock_) {
214    STATS_LOCK();
215    resolved_static_fields_++;
216  }
217
218  void UnresolvedStaticField() REQUIRES(!stats_lock_) {
219    STATS_LOCK();
220    unresolved_static_fields_++;
221  }
222
223  // Indicate that type information from the verifier led to devirtualization.
224  void PreciseTypeDevirtualization() REQUIRES(!stats_lock_) {
225    STATS_LOCK();
226    type_based_devirtualization_++;
227  }
228
229  // Indicate that a method of the given type was resolved at compile time.
230  void ResolvedMethod(InvokeType type) REQUIRES(!stats_lock_) {
231    DCHECK_LE(type, kMaxInvokeType);
232    STATS_LOCK();
233    resolved_methods_[type]++;
234  }
235
236  // Indicate that a method of the given type was unresolved at compile time as it was in an
237  // unknown dex file.
238  void UnresolvedMethod(InvokeType type) REQUIRES(!stats_lock_) {
239    DCHECK_LE(type, kMaxInvokeType);
240    STATS_LOCK();
241    unresolved_methods_[type]++;
242  }
243
244  // Indicate that a type of virtual method dispatch has been converted into a direct method
245  // dispatch.
246  void VirtualMadeDirect(InvokeType type) REQUIRES(!stats_lock_) {
247    DCHECK(type == kVirtual || type == kInterface || type == kSuper);
248    STATS_LOCK();
249    virtual_made_direct_[type]++;
250  }
251
252  // Indicate that a method of the given type was able to call directly into boot.
253  void DirectCallsToBoot(InvokeType type) REQUIRES(!stats_lock_) {
254    DCHECK_LE(type, kMaxInvokeType);
255    STATS_LOCK();
256    direct_calls_to_boot_[type]++;
257  }
258
259  // Indicate that a method of the given type was able to be resolved directly from boot.
260  void DirectMethodsToBoot(InvokeType type) REQUIRES(!stats_lock_) {
261    DCHECK_LE(type, kMaxInvokeType);
262    STATS_LOCK();
263    direct_methods_to_boot_[type]++;
264  }
265
266  void ProcessedInvoke(InvokeType type, int flags) REQUIRES(!stats_lock_) {
267    STATS_LOCK();
268    if (flags == 0) {
269      unresolved_methods_[type]++;
270    } else {
271      DCHECK_NE((flags & kFlagMethodResolved), 0);
272      resolved_methods_[type]++;
273      if ((flags & kFlagVirtualMadeDirect) != 0) {
274        virtual_made_direct_[type]++;
275        if ((flags & kFlagPreciseTypeDevirtualization) != 0) {
276          type_based_devirtualization_++;
277        }
278      } else {
279        DCHECK_EQ((flags & kFlagPreciseTypeDevirtualization), 0);
280      }
281      if ((flags & kFlagDirectCallToBoot) != 0) {
282        direct_calls_to_boot_[type]++;
283      }
284      if ((flags & kFlagDirectMethodToBoot) != 0) {
285        direct_methods_to_boot_[type]++;
286      }
287    }
288  }
289
290  // A check-cast could be eliminated due to verifier type analysis.
291  void SafeCast() REQUIRES(!stats_lock_) {
292    STATS_LOCK();
293    safe_casts_++;
294  }
295
296  // A check-cast couldn't be eliminated due to verifier type analysis.
297  void NotASafeCast() REQUIRES(!stats_lock_) {
298    STATS_LOCK();
299    not_safe_casts_++;
300  }
301
302 private:
303  Mutex stats_lock_;
304
305  size_t types_in_dex_cache_;
306  size_t types_not_in_dex_cache_;
307
308  size_t strings_in_dex_cache_;
309  size_t strings_not_in_dex_cache_;
310
311  size_t resolved_types_;
312  size_t unresolved_types_;
313
314  size_t resolved_instance_fields_;
315  size_t unresolved_instance_fields_;
316
317  size_t resolved_local_static_fields_;
318  size_t resolved_static_fields_;
319  size_t unresolved_static_fields_;
320  // Type based devirtualization for invoke interface and virtual.
321  size_t type_based_devirtualization_;
322
323  size_t resolved_methods_[kMaxInvokeType + 1];
324  size_t unresolved_methods_[kMaxInvokeType + 1];
325  size_t virtual_made_direct_[kMaxInvokeType + 1];
326  size_t direct_calls_to_boot_[kMaxInvokeType + 1];
327  size_t direct_methods_to_boot_[kMaxInvokeType + 1];
328
329  size_t safe_casts_;
330  size_t not_safe_casts_;
331
332  DISALLOW_COPY_AND_ASSIGN(AOTCompilationStats);
333};
334
335class CompilerDriver::DexFileMethodSet {
336 public:
337  explicit DexFileMethodSet(const DexFile& dex_file)
338    : dex_file_(dex_file),
339      method_indexes_(dex_file.NumMethodIds(), false, Allocator::GetMallocAllocator()) {
340  }
341  DexFileMethodSet(DexFileMethodSet&& other) = default;
342
343  const DexFile& GetDexFile() const { return dex_file_; }
344
345  BitVector& GetMethodIndexes() { return method_indexes_; }
346  const BitVector& GetMethodIndexes() const { return method_indexes_; }
347
348 private:
349  const DexFile& dex_file_;
350  BitVector method_indexes_;
351};
352
353CompilerDriver::CompilerDriver(
354    const CompilerOptions* compiler_options,
355    VerificationResults* verification_results,
356    DexFileToMethodInlinerMap* method_inliner_map,
357    Compiler::Kind compiler_kind,
358    InstructionSet instruction_set,
359    const InstructionSetFeatures* instruction_set_features,
360    bool boot_image,
361    bool app_image,
362    std::unordered_set<std::string>* image_classes,
363    std::unordered_set<std::string>* compiled_classes,
364    std::unordered_set<std::string>* compiled_methods,
365    size_t thread_count,
366    bool dump_stats,
367    bool dump_passes,
368    CumulativeLogger* timer,
369    int swap_fd,
370    const ProfileCompilationInfo* profile_compilation_info)
371    : compiler_options_(compiler_options),
372      verification_results_(verification_results),
373      method_inliner_map_(method_inliner_map),
374      compiler_(Compiler::Create(this, compiler_kind)),
375      compiler_kind_(compiler_kind),
376      instruction_set_(instruction_set == kArm ? kThumb2: instruction_set),
377      instruction_set_features_(instruction_set_features),
378      requires_constructor_barrier_lock_("constructor barrier lock"),
379      compiled_classes_lock_("compiled classes lock"),
380      compiled_methods_lock_("compiled method lock"),
381      compiled_methods_(MethodTable::key_compare()),
382      non_relative_linker_patch_count_(0u),
383      boot_image_(boot_image),
384      app_image_(app_image),
385      image_classes_(image_classes),
386      classes_to_compile_(compiled_classes),
387      methods_to_compile_(compiled_methods),
388      had_hard_verifier_failure_(false),
389      parallel_thread_count_(thread_count),
390      stats_(new AOTCompilationStats),
391      dump_stats_(dump_stats),
392      dump_passes_(dump_passes),
393      timings_logger_(timer),
394      compiler_context_(nullptr),
395      support_boot_image_fixup_(instruction_set != kMips64),
396      dex_files_for_oat_file_(nullptr),
397      compiled_method_storage_(swap_fd),
398      profile_compilation_info_(profile_compilation_info),
399      max_arena_alloc_(0),
400      dex_to_dex_references_lock_("dex-to-dex references lock"),
401      dex_to_dex_references_(),
402      current_dex_to_dex_methods_(nullptr) {
403  DCHECK(compiler_options_ != nullptr);
404  DCHECK(method_inliner_map_ != nullptr);
405
406  compiler_->Init();
407
408  if (compiler_options->VerifyOnlyProfile()) {
409    CHECK(profile_compilation_info_ != nullptr) << "Requires profile";
410  }
411  if (boot_image_) {
412    CHECK(image_classes_.get() != nullptr) << "Expected image classes for boot image";
413  }
414}
415
416CompilerDriver::~CompilerDriver() {
417  Thread* self = Thread::Current();
418  {
419    MutexLock mu(self, compiled_classes_lock_);
420    STLDeleteValues(&compiled_classes_);
421  }
422  {
423    MutexLock mu(self, compiled_methods_lock_);
424    for (auto& pair : compiled_methods_) {
425      CompiledMethod::ReleaseSwapAllocatedCompiledMethod(this, pair.second);
426    }
427  }
428  compiler_->UnInit();
429}
430
431
432#define CREATE_TRAMPOLINE(type, abi, offset) \
433    if (Is64BitInstructionSet(instruction_set_)) { \
434      return CreateTrampoline64(instruction_set_, abi, \
435                                type ## _ENTRYPOINT_OFFSET(PointerSize::k64, offset)); \
436    } else { \
437      return CreateTrampoline32(instruction_set_, abi, \
438                                type ## _ENTRYPOINT_OFFSET(PointerSize::k32, offset)); \
439    }
440
441std::unique_ptr<const std::vector<uint8_t>> CompilerDriver::CreateJniDlsymLookup() const {
442  CREATE_TRAMPOLINE(JNI, kJniAbi, pDlsymLookup)
443}
444
445std::unique_ptr<const std::vector<uint8_t>> CompilerDriver::CreateQuickGenericJniTrampoline()
446    const {
447  CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickGenericJniTrampoline)
448}
449
450std::unique_ptr<const std::vector<uint8_t>> CompilerDriver::CreateQuickImtConflictTrampoline()
451    const {
452  CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickImtConflictTrampoline)
453}
454
455std::unique_ptr<const std::vector<uint8_t>> CompilerDriver::CreateQuickResolutionTrampoline()
456    const {
457  CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickResolutionTrampoline)
458}
459
460std::unique_ptr<const std::vector<uint8_t>> CompilerDriver::CreateQuickToInterpreterBridge()
461    const {
462  CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickToInterpreterBridge)
463}
464#undef CREATE_TRAMPOLINE
465
466void CompilerDriver::CompileAll(jobject class_loader,
467                                const std::vector<const DexFile*>& dex_files,
468                                TimingLogger* timings) {
469  DCHECK(!Runtime::Current()->IsStarted());
470
471  InitializeThreadPools();
472
473  VLOG(compiler) << "Before precompile " << GetMemoryUsageString(false);
474  // Precompile:
475  // 1) Load image classes
476  // 2) Resolve all classes
477  // 3) Attempt to verify all classes
478  // 4) Attempt to initialize image classes, and trivially initialized classes
479  PreCompile(class_loader, dex_files, timings);
480  // Compile:
481  // 1) Compile all classes and methods enabled for compilation. May fall back to dex-to-dex
482  //    compilation.
483  if (!GetCompilerOptions().VerifyAtRuntime()) {
484    Compile(class_loader, dex_files, timings);
485  }
486  if (dump_stats_) {
487    stats_->Dump();
488  }
489
490  FreeThreadPools();
491}
492
493static optimizer::DexToDexCompilationLevel GetDexToDexCompilationLevel(
494    Thread* self, const CompilerDriver& driver, Handle<mirror::ClassLoader> class_loader,
495    const DexFile& dex_file, const DexFile::ClassDef& class_def)
496    REQUIRES_SHARED(Locks::mutator_lock_) {
497  auto* const runtime = Runtime::Current();
498  if (runtime->UseJitCompilation() || driver.GetCompilerOptions().VerifyAtRuntime()) {
499    // Verify at runtime shouldn't dex to dex since we didn't resolve of verify.
500    return optimizer::DexToDexCompilationLevel::kDontDexToDexCompile;
501  }
502  const char* descriptor = dex_file.GetClassDescriptor(class_def);
503  ClassLinker* class_linker = runtime->GetClassLinker();
504  mirror::Class* klass = class_linker->FindClass(self, descriptor, class_loader);
505  if (klass == nullptr) {
506    CHECK(self->IsExceptionPending());
507    self->ClearException();
508    return optimizer::DexToDexCompilationLevel::kDontDexToDexCompile;
509  }
510  // DexToDex at the kOptimize level may introduce quickened opcodes, which replace symbolic
511  // references with actual offsets. We cannot re-verify such instructions.
512  //
513  // We store the verification information in the class status in the oat file, which the linker
514  // can validate (checksums) and use to skip load-time verification. It is thus safe to
515  // optimize when a class has been fully verified before.
516  if (klass->IsVerified()) {
517    // Class is verified so we can enable DEX-to-DEX compilation for performance.
518    return optimizer::DexToDexCompilationLevel::kOptimize;
519  } else if (klass->IsCompileTimeVerified()) {
520    // Class verification has soft-failed. Anyway, ensure at least correctness.
521    DCHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime);
522    return optimizer::DexToDexCompilationLevel::kRequired;
523  } else {
524    // Class verification has failed: do not run DEX-to-DEX compilation.
525    return optimizer::DexToDexCompilationLevel::kDontDexToDexCompile;
526  }
527}
528
529static optimizer::DexToDexCompilationLevel GetDexToDexCompilationLevel(
530    Thread* self,
531    const CompilerDriver& driver,
532    jobject jclass_loader,
533    const DexFile& dex_file,
534    const DexFile::ClassDef& class_def) {
535  ScopedObjectAccess soa(self);
536  StackHandleScope<1> hs(soa.Self());
537  Handle<mirror::ClassLoader> class_loader(
538      hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
539  return GetDexToDexCompilationLevel(self, driver, class_loader, dex_file, class_def);
540}
541
542// Does the runtime for the InstructionSet provide an implementation returned by
543// GetQuickGenericJniStub allowing down calls that aren't compiled using a JNI compiler?
544static bool InstructionSetHasGenericJniStub(InstructionSet isa) {
545  switch (isa) {
546    case kArm:
547    case kArm64:
548    case kThumb2:
549    case kMips:
550    case kMips64:
551    case kX86:
552    case kX86_64: return true;
553    default: return false;
554  }
555}
556
557static void CompileMethod(Thread* self,
558                          CompilerDriver* driver,
559                          const DexFile::CodeItem* code_item,
560                          uint32_t access_flags,
561                          InvokeType invoke_type,
562                          uint16_t class_def_idx,
563                          uint32_t method_idx,
564                          jobject class_loader,
565                          const DexFile& dex_file,
566                          optimizer::DexToDexCompilationLevel dex_to_dex_compilation_level,
567                          bool compilation_enabled,
568                          Handle<mirror::DexCache> dex_cache)
569    REQUIRES(!driver->compiled_methods_lock_) {
570  DCHECK(driver != nullptr);
571  CompiledMethod* compiled_method = nullptr;
572  uint64_t start_ns = kTimeCompileMethod ? NanoTime() : 0;
573  MethodReference method_ref(&dex_file, method_idx);
574
575  if (driver->GetCurrentDexToDexMethods() != nullptr) {
576    // This is the second pass when we dex-to-dex compile previously marked methods.
577    // TODO: Refactor the compilation to avoid having to distinguish the two passes
578    // here. That should be done on a higher level. http://b/29089975
579    if (driver->GetCurrentDexToDexMethods()->IsBitSet(method_idx)) {
580      const VerifiedMethod* verified_method =
581          driver->GetVerificationResults()->GetVerifiedMethod(method_ref);
582      // Do not optimize if a VerifiedMethod is missing. SafeCast elision,
583      // for example, relies on it.
584      compiled_method = optimizer::ArtCompileDEX(
585          driver,
586          code_item,
587          access_flags,
588          invoke_type,
589          class_def_idx,
590          method_idx,
591          class_loader,
592          dex_file,
593          (verified_method != nullptr)
594              ? dex_to_dex_compilation_level
595              : optimizer::DexToDexCompilationLevel::kRequired);
596    }
597  } else if ((access_flags & kAccNative) != 0) {
598    // Are we extracting only and have support for generic JNI down calls?
599    if (!driver->GetCompilerOptions().IsJniCompilationEnabled() &&
600        InstructionSetHasGenericJniStub(driver->GetInstructionSet())) {
601      // Leaving this empty will trigger the generic JNI version
602    } else {
603      // Look-up the ArtMethod associated with this code_item (if any)
604      // -- It is later used to lookup any [optimization] annotations for this method.
605      ScopedObjectAccess soa(self);
606      StackHandleScope<1> hs(soa.Self());
607      Handle<mirror::ClassLoader> class_loader_handle(hs.NewHandle(
608          soa.Decode<mirror::ClassLoader*>(class_loader)));
609
610      // TODO: Lookup annotation from DexFile directly without resolving method.
611      ArtMethod* method =
612          Runtime::Current()->GetClassLinker()->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
613              dex_file,
614              method_idx,
615              dex_cache,
616              class_loader_handle,
617              /* referrer */ nullptr,
618              invoke_type);
619
620      // Query any JNI optimization annotations such as @FastNative or @CriticalNative.
621      Compiler::JniOptimizationFlags optimization_flags = Compiler::kNone;
622      if (UNLIKELY(method == nullptr)) {
623        // Failed method resolutions happen very rarely, e.g. ancestor class cannot be resolved.
624        DCHECK(self->IsExceptionPending());
625        self->ClearException();
626      } else if (method->IsAnnotatedWithFastNative()) {
627        // TODO: Will no longer need this CHECK once we have verifier checking this.
628        CHECK(!method->IsAnnotatedWithCriticalNative());
629        optimization_flags = Compiler::kFastNative;
630      } else if (method->IsAnnotatedWithCriticalNative()) {
631        // TODO: Will no longer need this CHECK once we have verifier checking this.
632        CHECK(!method->IsAnnotatedWithFastNative());
633        optimization_flags = Compiler::kCriticalNative;
634      }
635
636      compiled_method = driver->GetCompiler()->JniCompile(access_flags,
637                                                          method_idx,
638                                                          dex_file,
639                                                          optimization_flags);
640      CHECK(compiled_method != nullptr);
641    }
642  } else if ((access_flags & kAccAbstract) != 0) {
643    // Abstract methods don't have code.
644  } else {
645    const VerifiedMethod* verified_method =
646        driver->GetVerificationResults()->GetVerifiedMethod(method_ref);
647    bool compile = compilation_enabled &&
648        // Basic checks, e.g., not <clinit>.
649        driver->GetVerificationResults()
650            ->IsCandidateForCompilation(method_ref, access_flags) &&
651        // Did not fail to create VerifiedMethod metadata.
652        verified_method != nullptr &&
653        // Do not have failures that should punt to the interpreter.
654        !verified_method->HasRuntimeThrow() &&
655        (verified_method->GetEncounteredVerificationFailures() &
656            (verifier::VERIFY_ERROR_FORCE_INTERPRETER | verifier::VERIFY_ERROR_LOCKING)) == 0 &&
657        // Is eligable for compilation by methods-to-compile filter.
658        driver->IsMethodToCompile(method_ref) &&
659        driver->ShouldCompileBasedOnProfile(method_ref);
660
661    if (compile) {
662      // NOTE: if compiler declines to compile this method, it will return null.
663      compiled_method = driver->GetCompiler()->Compile(code_item, access_flags, invoke_type,
664                                                       class_def_idx, method_idx, class_loader,
665                                                       dex_file, dex_cache);
666    }
667    if (compiled_method == nullptr &&
668        dex_to_dex_compilation_level != optimizer::DexToDexCompilationLevel::kDontDexToDexCompile) {
669      DCHECK(!Runtime::Current()->UseJitCompilation());
670      // TODO: add a command-line option to disable DEX-to-DEX compilation ?
671      driver->MarkForDexToDexCompilation(self, method_ref);
672    }
673  }
674  if (kTimeCompileMethod) {
675    uint64_t duration_ns = NanoTime() - start_ns;
676    if (duration_ns > MsToNs(driver->GetCompiler()->GetMaximumCompilationTimeBeforeWarning())) {
677      LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
678                   << " took " << PrettyDuration(duration_ns);
679    }
680  }
681
682  if (compiled_method != nullptr) {
683    // Count non-relative linker patches.
684    size_t non_relative_linker_patch_count = 0u;
685    for (const LinkerPatch& patch : compiled_method->GetPatches()) {
686      if (!patch.IsPcRelative()) {
687        ++non_relative_linker_patch_count;
688      }
689    }
690    bool compile_pic = driver->GetCompilerOptions().GetCompilePic();  // Off by default
691    // When compiling with PIC, there should be zero non-relative linker patches
692    CHECK(!compile_pic || non_relative_linker_patch_count == 0u);
693
694    driver->AddCompiledMethod(method_ref, compiled_method, non_relative_linker_patch_count);
695  }
696
697  if (self->IsExceptionPending()) {
698    ScopedObjectAccess soa(self);
699    LOG(FATAL) << "Unexpected exception compiling: " << PrettyMethod(method_idx, dex_file) << "\n"
700        << self->GetException()->Dump();
701  }
702}
703
704void CompilerDriver::CompileOne(Thread* self, ArtMethod* method, TimingLogger* timings) {
705  DCHECK(!Runtime::Current()->IsStarted());
706  jobject jclass_loader;
707  const DexFile* dex_file;
708  uint16_t class_def_idx;
709  uint32_t method_idx = method->GetDexMethodIndex();
710  uint32_t access_flags = method->GetAccessFlags();
711  InvokeType invoke_type = method->GetInvokeType();
712  StackHandleScope<1> hs(self);
713  Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
714  {
715    ScopedObjectAccessUnchecked soa(self);
716    ScopedLocalRef<jobject> local_class_loader(
717        soa.Env(), soa.AddLocalReference<jobject>(method->GetDeclaringClass()->GetClassLoader()));
718    jclass_loader = soa.Env()->NewGlobalRef(local_class_loader.get());
719    // Find the dex_file
720    dex_file = method->GetDexFile();
721    class_def_idx = method->GetClassDefIndex();
722  }
723  const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
724
725  // Go to native so that we don't block GC during compilation.
726  ScopedThreadSuspension sts(self, kNative);
727
728  std::vector<const DexFile*> dex_files;
729  dex_files.push_back(dex_file);
730
731  InitializeThreadPools();
732
733  PreCompile(jclass_loader, dex_files, timings);
734
735  // Can we run DEX-to-DEX compiler on this class ?
736  optimizer::DexToDexCompilationLevel dex_to_dex_compilation_level =
737      GetDexToDexCompilationLevel(self,
738                                  *this,
739                                  jclass_loader,
740                                  *dex_file,
741                                  dex_file->GetClassDef(class_def_idx));
742
743  DCHECK(current_dex_to_dex_methods_ == nullptr);
744  CompileMethod(self,
745                this,
746                code_item,
747                access_flags,
748                invoke_type,
749                class_def_idx,
750                method_idx,
751                jclass_loader,
752                *dex_file,
753                dex_to_dex_compilation_level,
754                true,
755                dex_cache);
756
757  ArrayRef<DexFileMethodSet> dex_to_dex_references;
758  {
759    // From this point on, we shall not modify dex_to_dex_references_, so
760    // just grab a reference to it that we use without holding the mutex.
761    MutexLock lock(Thread::Current(), dex_to_dex_references_lock_);
762    dex_to_dex_references = ArrayRef<DexFileMethodSet>(dex_to_dex_references_);
763  }
764  if (!dex_to_dex_references.empty()) {
765    DCHECK_EQ(dex_to_dex_references.size(), 1u);
766    DCHECK(&dex_to_dex_references[0].GetDexFile() == dex_file);
767    current_dex_to_dex_methods_ = &dex_to_dex_references.front().GetMethodIndexes();
768    DCHECK(current_dex_to_dex_methods_->IsBitSet(method_idx));
769    DCHECK_EQ(current_dex_to_dex_methods_->NumSetBits(), 1u);
770    CompileMethod(self,
771                  this,
772                  code_item,
773                  access_flags,
774                  invoke_type,
775                  class_def_idx,
776                  method_idx,
777                  jclass_loader,
778                  *dex_file,
779                  dex_to_dex_compilation_level,
780                  true,
781                  dex_cache);
782    current_dex_to_dex_methods_ = nullptr;
783  }
784
785  FreeThreadPools();
786
787  self->GetJniEnv()->DeleteGlobalRef(jclass_loader);
788}
789
790void CompilerDriver::Resolve(jobject class_loader,
791                             const std::vector<const DexFile*>& dex_files,
792                             TimingLogger* timings) {
793  // Resolution allocates classes and needs to run single-threaded to be deterministic.
794  bool force_determinism = GetCompilerOptions().IsForceDeterminism();
795  ThreadPool* resolve_thread_pool = force_determinism
796                                     ? single_thread_pool_.get()
797                                     : parallel_thread_pool_.get();
798  size_t resolve_thread_count = force_determinism ? 1U : parallel_thread_count_;
799
800  for (size_t i = 0; i != dex_files.size(); ++i) {
801    const DexFile* dex_file = dex_files[i];
802    CHECK(dex_file != nullptr);
803    ResolveDexFile(class_loader,
804                   *dex_file,
805                   dex_files,
806                   resolve_thread_pool,
807                   resolve_thread_count,
808                   timings);
809  }
810}
811
812// Resolve const-strings in the code. Done to have deterministic allocation behavior. Right now
813// this is single-threaded for simplicity.
814// TODO: Collect the relevant string indices in parallel, then allocate them sequentially in a
815//       stable order.
816
817static void ResolveConstStrings(CompilerDriver* driver,
818                                const DexFile& dex_file,
819                                const DexFile::CodeItem* code_item) {
820  if (code_item == nullptr) {
821    // Abstract or native method.
822    return;
823  }
824
825  const uint16_t* code_ptr = code_item->insns_;
826  const uint16_t* code_end = code_item->insns_ + code_item->insns_size_in_code_units_;
827
828  while (code_ptr < code_end) {
829    const Instruction* inst = Instruction::At(code_ptr);
830    switch (inst->Opcode()) {
831      case Instruction::CONST_STRING: {
832        uint32_t string_index = inst->VRegB_21c();
833        driver->CanAssumeStringIsPresentInDexCache(dex_file, string_index);
834        break;
835      }
836      case Instruction::CONST_STRING_JUMBO: {
837        uint32_t string_index = inst->VRegB_31c();
838        driver->CanAssumeStringIsPresentInDexCache(dex_file, string_index);
839        break;
840      }
841
842      default:
843        break;
844    }
845
846    code_ptr += inst->SizeInCodeUnits();
847  }
848}
849
850static void ResolveConstStrings(CompilerDriver* driver,
851                                const std::vector<const DexFile*>& dex_files,
852                                TimingLogger* timings) {
853  for (const DexFile* dex_file : dex_files) {
854    TimingLogger::ScopedTiming t("Resolve const-string Strings", timings);
855
856    size_t class_def_count = dex_file->NumClassDefs();
857    for (size_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
858      const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
859
860      const uint8_t* class_data = dex_file->GetClassData(class_def);
861      if (class_data == nullptr) {
862        // empty class, probably a marker interface
863        continue;
864      }
865
866      ClassDataItemIterator it(*dex_file, class_data);
867      // Skip fields
868      while (it.HasNextStaticField()) {
869        it.Next();
870      }
871      while (it.HasNextInstanceField()) {
872        it.Next();
873      }
874
875      bool compilation_enabled = driver->IsClassToCompile(
876          dex_file->StringByTypeIdx(class_def.class_idx_));
877      if (!compilation_enabled) {
878        // Compilation is skipped, do not resolve const-string in code of this class.
879        // TODO: Make sure that inlining honors this.
880        continue;
881      }
882
883      // Direct methods.
884      int64_t previous_direct_method_idx = -1;
885      while (it.HasNextDirectMethod()) {
886        uint32_t method_idx = it.GetMemberIndex();
887        if (method_idx == previous_direct_method_idx) {
888          // smali can create dex files with two encoded_methods sharing the same method_idx
889          // http://code.google.com/p/smali/issues/detail?id=119
890          it.Next();
891          continue;
892        }
893        previous_direct_method_idx = method_idx;
894        ResolveConstStrings(driver, *dex_file, it.GetMethodCodeItem());
895        it.Next();
896      }
897      // Virtual methods.
898      int64_t previous_virtual_method_idx = -1;
899      while (it.HasNextVirtualMethod()) {
900        uint32_t method_idx = it.GetMemberIndex();
901        if (method_idx == previous_virtual_method_idx) {
902          // smali can create dex files with two encoded_methods sharing the same method_idx
903          // http://code.google.com/p/smali/issues/detail?id=119
904          it.Next();
905          continue;
906        }
907        previous_virtual_method_idx = method_idx;
908        ResolveConstStrings(driver, *dex_file, it.GetMethodCodeItem());
909        it.Next();
910      }
911      DCHECK(!it.HasNext());
912    }
913  }
914}
915
916inline void CompilerDriver::CheckThreadPools() {
917  DCHECK(parallel_thread_pool_ != nullptr);
918  DCHECK(single_thread_pool_ != nullptr);
919}
920
921void CompilerDriver::PreCompile(jobject class_loader,
922                                const std::vector<const DexFile*>& dex_files,
923                                TimingLogger* timings) {
924  CheckThreadPools();
925
926  LoadImageClasses(timings);
927  VLOG(compiler) << "LoadImageClasses: " << GetMemoryUsageString(false);
928
929  const bool verification_enabled = compiler_options_->IsVerificationEnabled();
930  const bool never_verify = compiler_options_->NeverVerify();
931  const bool verify_only_profile = compiler_options_->VerifyOnlyProfile();
932
933  // We need to resolve for never_verify since it needs to run dex to dex to add the
934  // RETURN_VOID_NO_BARRIER.
935  // Let the verifier resolve as needed for the verify_only_profile case.
936  if ((never_verify || verification_enabled) && !verify_only_profile) {
937    Resolve(class_loader, dex_files, timings);
938    VLOG(compiler) << "Resolve: " << GetMemoryUsageString(false);
939  }
940
941  if (never_verify) {
942    VLOG(compiler) << "Verify none mode specified, skipping verification.";
943    SetVerified(class_loader, dex_files, timings);
944  }
945
946  if (!verification_enabled) {
947    return;
948  }
949
950  if (GetCompilerOptions().IsForceDeterminism() && IsBootImage()) {
951    // Resolve strings from const-string. Do this now to have a deterministic image.
952    ResolveConstStrings(this, dex_files, timings);
953    VLOG(compiler) << "Resolve const-strings: " << GetMemoryUsageString(false);
954  }
955
956  Verify(class_loader, dex_files, timings);
957  VLOG(compiler) << "Verify: " << GetMemoryUsageString(false);
958
959  if (had_hard_verifier_failure_ && GetCompilerOptions().AbortOnHardVerifierFailure()) {
960    LOG(FATAL) << "Had a hard failure verifying all classes, and was asked to abort in such "
961               << "situations. Please check the log.";
962  }
963
964  InitializeClasses(class_loader, dex_files, timings);
965  VLOG(compiler) << "InitializeClasses: " << GetMemoryUsageString(false);
966
967  UpdateImageClasses(timings);
968  VLOG(compiler) << "UpdateImageClasses: " << GetMemoryUsageString(false);
969}
970
971bool CompilerDriver::IsImageClass(const char* descriptor) const {
972  if (image_classes_ != nullptr) {
973    // If we have a set of image classes, use those.
974    return image_classes_->find(descriptor) != image_classes_->end();
975  }
976  // No set of image classes, assume we include all the classes.
977  // NOTE: Currently only reachable from InitImageMethodVisitor for the app image case.
978  return !IsBootImage();
979}
980
981bool CompilerDriver::IsClassToCompile(const char* descriptor) const {
982  if (classes_to_compile_ == nullptr) {
983    return true;
984  }
985  return classes_to_compile_->find(descriptor) != classes_to_compile_->end();
986}
987
988bool CompilerDriver::IsMethodToCompile(const MethodReference& method_ref) const {
989  if (methods_to_compile_ == nullptr) {
990    return true;
991  }
992
993  std::string tmp = PrettyMethod(method_ref.dex_method_index, *method_ref.dex_file, true);
994  return methods_to_compile_->find(tmp.c_str()) != methods_to_compile_->end();
995}
996
997bool CompilerDriver::ShouldCompileBasedOnProfile(const MethodReference& method_ref) const {
998  if (profile_compilation_info_ == nullptr) {
999    // If we miss profile information it means that we don't do a profile guided compilation.
1000    // Return true, and let the other filters decide if the method should be compiled.
1001    return true;
1002  }
1003  bool result = profile_compilation_info_->ContainsMethod(method_ref);
1004
1005  if (kDebugProfileGuidedCompilation) {
1006    LOG(INFO) << "[ProfileGuidedCompilation] "
1007        << (result ? "Compiled" : "Skipped") << " method:"
1008        << PrettyMethod(method_ref.dex_method_index, *method_ref.dex_file, true);
1009  }
1010  return result;
1011}
1012
1013bool CompilerDriver::ShouldVerifyClassBasedOnProfile(const DexFile& dex_file,
1014                                                     uint16_t class_idx) const {
1015  if (!compiler_options_->VerifyOnlyProfile()) {
1016    // No profile, verify everything.
1017    return true;
1018  }
1019  DCHECK(profile_compilation_info_ != nullptr);
1020  bool result = profile_compilation_info_->ContainsClass(dex_file, class_idx);
1021  if (kDebugProfileGuidedCompilation) {
1022    LOG(INFO) << "[ProfileGuidedCompilation] "
1023        << (result ? "Verified" : "Skipped") << " method:"
1024        << dex_file.GetClassDescriptor(dex_file.GetClassDef(class_idx));
1025  }
1026  return result;
1027}
1028
1029class ResolveCatchBlockExceptionsClassVisitor : public ClassVisitor {
1030 public:
1031  explicit ResolveCatchBlockExceptionsClassVisitor(
1032      std::set<std::pair<uint16_t, const DexFile*>>& exceptions_to_resolve)
1033     : exceptions_to_resolve_(exceptions_to_resolve) {}
1034
1035  virtual bool operator()(mirror::Class* c) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
1036    const auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
1037    for (auto& m : c->GetMethods(pointer_size)) {
1038      ResolveExceptionsForMethod(&m, pointer_size);
1039    }
1040    return true;
1041  }
1042
1043 private:
1044  void ResolveExceptionsForMethod(ArtMethod* method_handle, PointerSize pointer_size)
1045      REQUIRES_SHARED(Locks::mutator_lock_) {
1046    const DexFile::CodeItem* code_item = method_handle->GetCodeItem();
1047    if (code_item == nullptr) {
1048      return;  // native or abstract method
1049    }
1050    if (code_item->tries_size_ == 0) {
1051      return;  // nothing to process
1052    }
1053    const uint8_t* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
1054    size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
1055    for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
1056      int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
1057      bool has_catch_all = false;
1058      if (encoded_catch_handler_size <= 0) {
1059        encoded_catch_handler_size = -encoded_catch_handler_size;
1060        has_catch_all = true;
1061      }
1062      for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
1063        uint16_t encoded_catch_handler_handlers_type_idx =
1064            DecodeUnsignedLeb128(&encoded_catch_handler_list);
1065        // Add to set of types to resolve if not already in the dex cache resolved types
1066        if (!method_handle->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx,
1067                                              pointer_size)) {
1068          exceptions_to_resolve_.emplace(encoded_catch_handler_handlers_type_idx,
1069                                         method_handle->GetDexFile());
1070        }
1071        // ignore address associated with catch handler
1072        DecodeUnsignedLeb128(&encoded_catch_handler_list);
1073      }
1074      if (has_catch_all) {
1075        // ignore catch all address
1076        DecodeUnsignedLeb128(&encoded_catch_handler_list);
1077      }
1078    }
1079  }
1080
1081  std::set<std::pair<uint16_t, const DexFile*>>& exceptions_to_resolve_;
1082};
1083
1084class RecordImageClassesVisitor : public ClassVisitor {
1085 public:
1086  explicit RecordImageClassesVisitor(std::unordered_set<std::string>* image_classes)
1087      : image_classes_(image_classes) {}
1088
1089  bool operator()(mirror::Class* klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
1090    std::string temp;
1091    image_classes_->insert(klass->GetDescriptor(&temp));
1092    return true;
1093  }
1094
1095 private:
1096  std::unordered_set<std::string>* const image_classes_;
1097};
1098
1099// Make a list of descriptors for classes to include in the image
1100void CompilerDriver::LoadImageClasses(TimingLogger* timings) {
1101  CHECK(timings != nullptr);
1102  if (!IsBootImage()) {
1103    return;
1104  }
1105
1106  TimingLogger::ScopedTiming t("LoadImageClasses", timings);
1107  // Make a first class to load all classes explicitly listed in the file
1108  Thread* self = Thread::Current();
1109  ScopedObjectAccess soa(self);
1110  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1111  CHECK(image_classes_.get() != nullptr);
1112  for (auto it = image_classes_->begin(), end = image_classes_->end(); it != end;) {
1113    const std::string& descriptor(*it);
1114    StackHandleScope<1> hs(self);
1115    Handle<mirror::Class> klass(
1116        hs.NewHandle(class_linker->FindSystemClass(self, descriptor.c_str())));
1117    if (klass.Get() == nullptr) {
1118      VLOG(compiler) << "Failed to find class " << descriptor;
1119      image_classes_->erase(it++);
1120      self->ClearException();
1121    } else {
1122      ++it;
1123    }
1124  }
1125
1126  // Resolve exception classes referenced by the loaded classes. The catch logic assumes
1127  // exceptions are resolved by the verifier when there is a catch block in an interested method.
1128  // Do this here so that exception classes appear to have been specified image classes.
1129  std::set<std::pair<uint16_t, const DexFile*>> unresolved_exception_types;
1130  StackHandleScope<1> hs(self);
1131  Handle<mirror::Class> java_lang_Throwable(
1132      hs.NewHandle(class_linker->FindSystemClass(self, "Ljava/lang/Throwable;")));
1133  do {
1134    unresolved_exception_types.clear();
1135    ResolveCatchBlockExceptionsClassVisitor visitor(unresolved_exception_types);
1136    class_linker->VisitClasses(&visitor);
1137    for (const std::pair<uint16_t, const DexFile*>& exception_type : unresolved_exception_types) {
1138      uint16_t exception_type_idx = exception_type.first;
1139      const DexFile* dex_file = exception_type.second;
1140      StackHandleScope<2> hs2(self);
1141      Handle<mirror::DexCache> dex_cache(hs2.NewHandle(class_linker->RegisterDexFile(*dex_file,
1142                                                                                     nullptr)));
1143      Handle<mirror::Class> klass(hs2.NewHandle(
1144          class_linker->ResolveType(*dex_file,
1145                                    exception_type_idx,
1146                                    dex_cache,
1147                                    ScopedNullHandle<mirror::ClassLoader>())));
1148      if (klass.Get() == nullptr) {
1149        const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
1150        const char* descriptor = dex_file->GetTypeDescriptor(type_id);
1151        LOG(FATAL) << "Failed to resolve class " << descriptor;
1152      }
1153      DCHECK(java_lang_Throwable->IsAssignableFrom(klass.Get()));
1154    }
1155    // Resolving exceptions may load classes that reference more exceptions, iterate until no
1156    // more are found
1157  } while (!unresolved_exception_types.empty());
1158
1159  // We walk the roots looking for classes so that we'll pick up the
1160  // above classes plus any classes them depend on such super
1161  // classes, interfaces, and the required ClassLinker roots.
1162  RecordImageClassesVisitor visitor(image_classes_.get());
1163  class_linker->VisitClasses(&visitor);
1164
1165  CHECK_NE(image_classes_->size(), 0U);
1166}
1167
1168static void MaybeAddToImageClasses(Handle<mirror::Class> c,
1169                                   std::unordered_set<std::string>* image_classes)
1170    REQUIRES_SHARED(Locks::mutator_lock_) {
1171  Thread* self = Thread::Current();
1172  StackHandleScope<1> hs(self);
1173  // Make a copy of the handle so that we don't clobber it doing Assign.
1174  MutableHandle<mirror::Class> klass(hs.NewHandle(c.Get()));
1175  std::string temp;
1176  const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
1177  while (!klass->IsObjectClass()) {
1178    const char* descriptor = klass->GetDescriptor(&temp);
1179    std::pair<std::unordered_set<std::string>::iterator, bool> result =
1180        image_classes->insert(descriptor);
1181    if (!result.second) {  // Previously inserted.
1182      break;
1183    }
1184    VLOG(compiler) << "Adding " << descriptor << " to image classes";
1185    for (size_t i = 0; i < klass->NumDirectInterfaces(); ++i) {
1186      StackHandleScope<1> hs2(self);
1187      MaybeAddToImageClasses(hs2.NewHandle(mirror::Class::GetDirectInterface(self, klass, i)),
1188                             image_classes);
1189    }
1190    for (auto& m : c->GetVirtualMethods(pointer_size)) {
1191      StackHandleScope<1> hs2(self);
1192      MaybeAddToImageClasses(hs2.NewHandle(m.GetDeclaringClass()), image_classes);
1193    }
1194    if (klass->IsArrayClass()) {
1195      StackHandleScope<1> hs2(self);
1196      MaybeAddToImageClasses(hs2.NewHandle(klass->GetComponentType()), image_classes);
1197    }
1198    klass.Assign(klass->GetSuperClass());
1199  }
1200}
1201
1202// Keeps all the data for the update together. Also doubles as the reference visitor.
1203// Note: we can use object pointers because we suspend all threads.
1204class ClinitImageUpdate {
1205 public:
1206  static ClinitImageUpdate* Create(std::unordered_set<std::string>* image_class_descriptors,
1207                                   Thread* self, ClassLinker* linker, std::string* error_msg) {
1208    std::unique_ptr<ClinitImageUpdate> res(new ClinitImageUpdate(image_class_descriptors, self,
1209                                                                 linker));
1210    if (res->dex_cache_class_ == nullptr) {
1211      *error_msg = "Could not find DexCache class.";
1212      return nullptr;
1213    }
1214
1215    return res.release();
1216  }
1217
1218  ~ClinitImageUpdate() {
1219    // Allow others to suspend again.
1220    self_->EndAssertNoThreadSuspension(old_cause_);
1221  }
1222
1223  // Visitor for VisitReferences.
1224  void operator()(mirror::Object* object, MemberOffset field_offset, bool /* is_static */) const
1225      REQUIRES_SHARED(Locks::mutator_lock_) {
1226    mirror::Object* ref = object->GetFieldObject<mirror::Object>(field_offset);
1227    if (ref != nullptr) {
1228      VisitClinitClassesObject(ref);
1229    }
1230  }
1231
1232  // java.lang.Reference visitor for VisitReferences.
1233  void operator()(mirror::Class* klass ATTRIBUTE_UNUSED, mirror::Reference* ref ATTRIBUTE_UNUSED)
1234      const {}
1235
1236  // Ignore class native roots.
1237  void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED)
1238      const {}
1239  void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {}
1240
1241  void Walk() REQUIRES_SHARED(Locks::mutator_lock_) {
1242    // Use the initial classes as roots for a search.
1243    for (mirror::Class* klass_root : image_classes_) {
1244      VisitClinitClassesObject(klass_root);
1245    }
1246  }
1247
1248 private:
1249  class FindImageClassesVisitor : public ClassVisitor {
1250   public:
1251    explicit FindImageClassesVisitor(ClinitImageUpdate* data) : data_(data) {}
1252
1253    bool operator()(mirror::Class* klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
1254      std::string temp;
1255      const char* name = klass->GetDescriptor(&temp);
1256      if (data_->image_class_descriptors_->find(name) != data_->image_class_descriptors_->end()) {
1257        data_->image_classes_.push_back(klass);
1258      } else {
1259        // Check whether it is initialized and has a clinit. They must be kept, too.
1260        if (klass->IsInitialized() && klass->FindClassInitializer(
1261            Runtime::Current()->GetClassLinker()->GetImagePointerSize()) != nullptr) {
1262          data_->image_classes_.push_back(klass);
1263        }
1264      }
1265      return true;
1266    }
1267
1268   private:
1269    ClinitImageUpdate* const data_;
1270  };
1271
1272  ClinitImageUpdate(std::unordered_set<std::string>* image_class_descriptors, Thread* self,
1273                    ClassLinker* linker)
1274      REQUIRES_SHARED(Locks::mutator_lock_) :
1275      image_class_descriptors_(image_class_descriptors), self_(self) {
1276    CHECK(linker != nullptr);
1277    CHECK(image_class_descriptors != nullptr);
1278
1279    // Make sure nobody interferes with us.
1280    old_cause_ = self->StartAssertNoThreadSuspension("Boot image closure");
1281
1282    // Find the interesting classes.
1283    dex_cache_class_ = linker->LookupClass(self, "Ljava/lang/DexCache;",
1284        ComputeModifiedUtf8Hash("Ljava/lang/DexCache;"), nullptr);
1285
1286    // Find all the already-marked classes.
1287    WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1288    FindImageClassesVisitor visitor(this);
1289    linker->VisitClasses(&visitor);
1290  }
1291
1292  void VisitClinitClassesObject(mirror::Object* object) const
1293      REQUIRES_SHARED(Locks::mutator_lock_) {
1294    DCHECK(object != nullptr);
1295    if (marked_objects_.find(object) != marked_objects_.end()) {
1296      // Already processed.
1297      return;
1298    }
1299
1300    // Mark it.
1301    marked_objects_.insert(object);
1302
1303    if (object->IsClass()) {
1304      // If it is a class, add it.
1305      StackHandleScope<1> hs(self_);
1306      MaybeAddToImageClasses(hs.NewHandle(object->AsClass()), image_class_descriptors_);
1307    } else {
1308      // Else visit the object's class.
1309      VisitClinitClassesObject(object->GetClass());
1310    }
1311
1312    // If it is not a DexCache, visit all references.
1313    mirror::Class* klass = object->GetClass();
1314    if (klass != dex_cache_class_) {
1315      object->VisitReferences(*this, *this);
1316    }
1317  }
1318
1319  mutable std::unordered_set<mirror::Object*> marked_objects_;
1320  std::unordered_set<std::string>* const image_class_descriptors_;
1321  std::vector<mirror::Class*> image_classes_;
1322  const mirror::Class* dex_cache_class_;
1323  Thread* const self_;
1324  const char* old_cause_;
1325
1326  DISALLOW_COPY_AND_ASSIGN(ClinitImageUpdate);
1327};
1328
1329void CompilerDriver::UpdateImageClasses(TimingLogger* timings) {
1330  if (IsBootImage()) {
1331    TimingLogger::ScopedTiming t("UpdateImageClasses", timings);
1332
1333    Runtime* runtime = Runtime::Current();
1334
1335    // Suspend all threads.
1336    ScopedSuspendAll ssa(__FUNCTION__);
1337
1338    std::string error_msg;
1339    std::unique_ptr<ClinitImageUpdate> update(ClinitImageUpdate::Create(image_classes_.get(),
1340                                                                        Thread::Current(),
1341                                                                        runtime->GetClassLinker(),
1342                                                                        &error_msg));
1343    CHECK(update.get() != nullptr) << error_msg;  // TODO: Soft failure?
1344
1345    // Do the marking.
1346    update->Walk();
1347  }
1348}
1349
1350bool CompilerDriver::CanAssumeClassIsLoaded(mirror::Class* klass) {
1351  Runtime* runtime = Runtime::Current();
1352  if (!runtime->IsAotCompiler()) {
1353    DCHECK(runtime->UseJitCompilation());
1354    // Having the klass reference here implies that the klass is already loaded.
1355    return true;
1356  }
1357  if (!IsBootImage()) {
1358    // Assume loaded only if klass is in the boot image. App classes cannot be assumed
1359    // loaded because we don't even know what class loader will be used to load them.
1360    bool class_in_image = runtime->GetHeap()->FindSpaceFromObject(klass, false)->IsImageSpace();
1361    return class_in_image;
1362  }
1363  std::string temp;
1364  const char* descriptor = klass->GetDescriptor(&temp);
1365  return IsImageClass(descriptor);
1366}
1367
1368void CompilerDriver::MarkForDexToDexCompilation(Thread* self, const MethodReference& method_ref) {
1369  MutexLock lock(self, dex_to_dex_references_lock_);
1370  // Since we're compiling one dex file at a time, we need to look for the
1371  // current dex file entry only at the end of dex_to_dex_references_.
1372  if (dex_to_dex_references_.empty() ||
1373      &dex_to_dex_references_.back().GetDexFile() != method_ref.dex_file) {
1374    dex_to_dex_references_.emplace_back(*method_ref.dex_file);
1375  }
1376  dex_to_dex_references_.back().GetMethodIndexes().SetBit(method_ref.dex_method_index);
1377}
1378
1379bool CompilerDriver::CanAssumeTypeIsPresentInDexCache(Handle<mirror::DexCache> dex_cache,
1380                                                      uint32_t type_idx) {
1381  bool result = false;
1382  if ((IsBootImage() &&
1383       IsImageClass(dex_cache->GetDexFile()->StringDataByIdx(
1384           dex_cache->GetDexFile()->GetTypeId(type_idx).descriptor_idx_))) ||
1385      Runtime::Current()->UseJitCompilation()) {
1386    mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
1387    result = (resolved_class != nullptr);
1388  }
1389
1390  if (result) {
1391    stats_->TypeInDexCache();
1392  } else {
1393    stats_->TypeNotInDexCache();
1394  }
1395  return result;
1396}
1397
1398bool CompilerDriver::CanAssumeStringIsPresentInDexCache(const DexFile& dex_file,
1399                                                        uint32_t string_idx) {
1400  // See also Compiler::ResolveDexFile
1401
1402  bool result = false;
1403  if (IsBootImage() || Runtime::Current()->UseJitCompilation()) {
1404    ScopedObjectAccess soa(Thread::Current());
1405    StackHandleScope<1> hs(soa.Self());
1406    ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
1407    Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(
1408        soa.Self(), dex_file, false)));
1409    if (IsBootImage()) {
1410      // We resolve all const-string strings when building for the image.
1411      class_linker->ResolveString(dex_file, string_idx, dex_cache);
1412      result = true;
1413    } else {
1414      // Just check whether the dex cache already has the string.
1415      DCHECK(Runtime::Current()->UseJitCompilation());
1416      result = (dex_cache->GetResolvedString(string_idx) != nullptr);
1417    }
1418  }
1419  if (result) {
1420    stats_->StringInDexCache();
1421  } else {
1422    stats_->StringNotInDexCache();
1423  }
1424  return result;
1425}
1426
1427bool CompilerDriver::CanAccessTypeWithoutChecks(uint32_t referrer_idx,
1428                                                Handle<mirror::DexCache> dex_cache,
1429                                                uint32_t type_idx) {
1430  // Get type from dex cache assuming it was populated by the verifier
1431  mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
1432  if (resolved_class == nullptr) {
1433    stats_->TypeNeedsAccessCheck();
1434    return false;  // Unknown class needs access checks.
1435  }
1436  const DexFile::MethodId& method_id = dex_cache->GetDexFile()->GetMethodId(referrer_idx);
1437  bool is_accessible = resolved_class->IsPublic();  // Public classes are always accessible.
1438  if (!is_accessible) {
1439    mirror::Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
1440    if (referrer_class == nullptr) {
1441      stats_->TypeNeedsAccessCheck();
1442      return false;  // Incomplete referrer knowledge needs access check.
1443    }
1444    // Perform access check, will return true if access is ok or false if we're going to have to
1445    // check this at runtime (for example for class loaders).
1446    is_accessible = referrer_class->CanAccess(resolved_class);
1447  }
1448  if (is_accessible) {
1449    stats_->TypeDoesntNeedAccessCheck();
1450  } else {
1451    stats_->TypeNeedsAccessCheck();
1452  }
1453  return is_accessible;
1454}
1455
1456bool CompilerDriver::CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx,
1457                                                            Handle<mirror::DexCache> dex_cache,
1458                                                            uint32_t type_idx,
1459                                                            bool* finalizable) {
1460  // Get type from dex cache assuming it was populated by the verifier.
1461  mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
1462  if (resolved_class == nullptr) {
1463    stats_->TypeNeedsAccessCheck();
1464    // Be conservative.
1465    *finalizable = true;
1466    return false;  // Unknown class needs access checks.
1467  }
1468  *finalizable = resolved_class->IsFinalizable();
1469  const DexFile::MethodId& method_id = dex_cache->GetDexFile()->GetMethodId(referrer_idx);
1470  bool is_accessible = resolved_class->IsPublic();  // Public classes are always accessible.
1471  if (!is_accessible) {
1472    mirror::Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
1473    if (referrer_class == nullptr) {
1474      stats_->TypeNeedsAccessCheck();
1475      return false;  // Incomplete referrer knowledge needs access check.
1476    }
1477    // Perform access and instantiable checks, will return true if access is ok or false if we're
1478    // going to have to check this at runtime (for example for class loaders).
1479    is_accessible = referrer_class->CanAccess(resolved_class);
1480  }
1481  bool result = is_accessible && resolved_class->IsInstantiable();
1482  if (result) {
1483    stats_->TypeDoesntNeedAccessCheck();
1484  } else {
1485    stats_->TypeNeedsAccessCheck();
1486  }
1487  return result;
1488}
1489
1490bool CompilerDriver::CanEmbedTypeInCode(const DexFile& dex_file, uint32_t type_idx,
1491                                        bool* is_type_initialized, bool* use_direct_type_ptr,
1492                                        uintptr_t* direct_type_ptr, bool* out_is_finalizable) {
1493  ScopedObjectAccess soa(Thread::Current());
1494  Runtime* runtime = Runtime::Current();
1495  mirror::DexCache* dex_cache = runtime->GetClassLinker()->FindDexCache(
1496      soa.Self(), dex_file, false);
1497  mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
1498  if (resolved_class == nullptr) {
1499    return false;
1500  }
1501  if (GetCompilerOptions().GetCompilePic()) {
1502    // Do not allow a direct class pointer to be used when compiling for position-independent
1503    return false;
1504  }
1505  *out_is_finalizable = resolved_class->IsFinalizable();
1506  gc::Heap* heap = runtime->GetHeap();
1507  const bool compiling_boot = heap->IsCompilingBoot();
1508  const bool support_boot_image_fixup = GetSupportBootImageFixup();
1509  if (compiling_boot) {
1510    // boot -> boot class pointers.
1511    // True if the class is in the image at boot compiling time.
1512    const bool is_image_class = IsBootImage() && IsImageClass(
1513        dex_file.StringDataByIdx(dex_file.GetTypeId(type_idx).descriptor_idx_));
1514    // True if pc relative load works.
1515    if (is_image_class && support_boot_image_fixup) {
1516      *is_type_initialized = resolved_class->IsInitialized();
1517      *use_direct_type_ptr = false;
1518      *direct_type_ptr = 0;
1519      return true;
1520    } else {
1521      return false;
1522    }
1523  } else if (runtime->UseJitCompilation() && !heap->IsMovableObject(resolved_class)) {
1524    *is_type_initialized = resolved_class->IsInitialized();
1525    // If the class may move around, then don't embed it as a direct pointer.
1526    *use_direct_type_ptr = true;
1527    *direct_type_ptr = reinterpret_cast<uintptr_t>(resolved_class);
1528    return true;
1529  } else {
1530    // True if the class is in the image at app compiling time.
1531    const bool class_in_image = heap->FindSpaceFromObject(resolved_class, false)->IsImageSpace();
1532    if (class_in_image && support_boot_image_fixup) {
1533      // boot -> app class pointers.
1534      *is_type_initialized = resolved_class->IsInitialized();
1535      // TODO This is somewhat hacky. We should refactor all of this invoke codepath.
1536      *use_direct_type_ptr = !GetCompilerOptions().GetIncludePatchInformation();
1537      *direct_type_ptr = reinterpret_cast<uintptr_t>(resolved_class);
1538      return true;
1539    } else {
1540      // app -> app class pointers.
1541      // Give up because app does not have an image and class
1542      // isn't created at compile time.  TODO: implement this
1543      // if/when each app gets an image.
1544      return false;
1545    }
1546  }
1547}
1548
1549bool CompilerDriver::CanEmbedReferenceTypeInCode(ClassReference* ref,
1550                                                 bool* use_direct_ptr,
1551                                                 uintptr_t* direct_type_ptr) {
1552  CHECK(ref != nullptr);
1553  CHECK(use_direct_ptr != nullptr);
1554  CHECK(direct_type_ptr != nullptr);
1555
1556  ScopedObjectAccess soa(Thread::Current());
1557  mirror::Class* reference_class = mirror::Reference::GetJavaLangRefReference();
1558  bool is_initialized = false;
1559  bool unused_finalizable;
1560  // Make sure we have a finished Reference class object before attempting to use it.
1561  if (!CanEmbedTypeInCode(*reference_class->GetDexCache()->GetDexFile(),
1562                          reference_class->GetDexTypeIndex(), &is_initialized,
1563                          use_direct_ptr, direct_type_ptr, &unused_finalizable) ||
1564      !is_initialized) {
1565    return false;
1566  }
1567  ref->first = &reference_class->GetDexFile();
1568  ref->second = reference_class->GetDexClassDefIndex();
1569  return true;
1570}
1571
1572uint32_t CompilerDriver::GetReferenceSlowFlagOffset() const {
1573  ScopedObjectAccess soa(Thread::Current());
1574  mirror::Class* klass = mirror::Reference::GetJavaLangRefReference();
1575  DCHECK(klass->IsInitialized());
1576  return klass->GetSlowPathFlagOffset().Uint32Value();
1577}
1578
1579uint32_t CompilerDriver::GetReferenceDisableFlagOffset() const {
1580  ScopedObjectAccess soa(Thread::Current());
1581  mirror::Class* klass = mirror::Reference::GetJavaLangRefReference();
1582  DCHECK(klass->IsInitialized());
1583  return klass->GetDisableIntrinsicFlagOffset().Uint32Value();
1584}
1585
1586DexCacheArraysLayout CompilerDriver::GetDexCacheArraysLayout(const DexFile* dex_file) {
1587  return ContainsElement(GetDexFilesForOatFile(), dex_file)
1588      ? DexCacheArraysLayout(GetInstructionSetPointerSize(instruction_set_), dex_file)
1589      : DexCacheArraysLayout();
1590}
1591
1592void CompilerDriver::ProcessedInstanceField(bool resolved) {
1593  if (!resolved) {
1594    stats_->UnresolvedInstanceField();
1595  } else {
1596    stats_->ResolvedInstanceField();
1597  }
1598}
1599
1600void CompilerDriver::ProcessedStaticField(bool resolved, bool local) {
1601  if (!resolved) {
1602    stats_->UnresolvedStaticField();
1603  } else if (local) {
1604    stats_->ResolvedLocalStaticField();
1605  } else {
1606    stats_->ResolvedStaticField();
1607  }
1608}
1609
1610void CompilerDriver::ProcessedInvoke(InvokeType invoke_type, int flags) {
1611  stats_->ProcessedInvoke(invoke_type, flags);
1612}
1613
1614ArtField* CompilerDriver::ComputeInstanceFieldInfo(uint32_t field_idx,
1615                                                   const DexCompilationUnit* mUnit, bool is_put,
1616                                                   const ScopedObjectAccess& soa) {
1617  // Try to resolve the field and compiling method's class.
1618  ArtField* resolved_field;
1619  mirror::Class* referrer_class;
1620  Handle<mirror::DexCache> dex_cache(mUnit->GetDexCache());
1621  {
1622    StackHandleScope<1> hs(soa.Self());
1623    Handle<mirror::ClassLoader> class_loader_handle(
1624        hs.NewHandle(soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader())));
1625    resolved_field = ResolveField(soa, dex_cache, class_loader_handle, mUnit, field_idx, false);
1626    referrer_class = resolved_field != nullptr
1627        ? ResolveCompilingMethodsClass(soa, dex_cache, class_loader_handle, mUnit) : nullptr;
1628  }
1629  bool can_link = false;
1630  if (resolved_field != nullptr && referrer_class != nullptr) {
1631    std::pair<bool, bool> fast_path = IsFastInstanceField(
1632        dex_cache.Get(), referrer_class, resolved_field, field_idx);
1633    can_link = is_put ? fast_path.second : fast_path.first;
1634  }
1635  ProcessedInstanceField(can_link);
1636  return can_link ? resolved_field : nullptr;
1637}
1638
1639bool CompilerDriver::ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit,
1640                                              bool is_put, MemberOffset* field_offset,
1641                                              bool* is_volatile) {
1642  ScopedObjectAccess soa(Thread::Current());
1643  ArtField* resolved_field = ComputeInstanceFieldInfo(field_idx, mUnit, is_put, soa);
1644
1645  if (resolved_field == nullptr) {
1646    // Conservative defaults.
1647    *is_volatile = true;
1648    *field_offset = MemberOffset(static_cast<size_t>(-1));
1649    return false;
1650  } else {
1651    *is_volatile = resolved_field->IsVolatile();
1652    *field_offset = resolved_field->GetOffset();
1653    return true;
1654  }
1655}
1656
1657void CompilerDriver::GetCodeAndMethodForDirectCall(InvokeType* type, InvokeType sharp_type,
1658                                                   bool no_guarantee_of_dex_cache_entry,
1659                                                   const mirror::Class* referrer_class,
1660                                                   ArtMethod* method,
1661                                                   int* stats_flags,
1662                                                   MethodReference* target_method,
1663                                                   uintptr_t* direct_code,
1664                                                   uintptr_t* direct_method) {
1665  // For direct and static methods compute possible direct_code and direct_method values, ie
1666  // an address for the Method* being invoked and an address of the code for that Method*.
1667  // For interface calls compute a value for direct_method that is the interface method being
1668  // invoked, so this can be passed to the out-of-line runtime support code.
1669  *direct_code = 0;
1670  *direct_method = 0;
1671  Runtime* const runtime = Runtime::Current();
1672  gc::Heap* const heap = runtime->GetHeap();
1673  auto* cl = runtime->GetClassLinker();
1674  const auto pointer_size = cl->GetImagePointerSize();
1675  bool use_dex_cache = GetCompilerOptions().GetCompilePic();  // Off by default
1676  const bool compiling_boot = heap->IsCompilingBoot();
1677  // TODO This is somewhat hacky. We should refactor all of this invoke codepath.
1678  const bool force_relocations = (compiling_boot ||
1679                                  GetCompilerOptions().GetIncludePatchInformation());
1680  if (sharp_type != kStatic && sharp_type != kDirect) {
1681    return;
1682  }
1683  // TODO: support patching on all architectures.
1684  use_dex_cache = use_dex_cache || (force_relocations && !support_boot_image_fixup_);
1685  mirror::Class* declaring_class = method->GetDeclaringClass();
1686  bool method_code_in_boot = declaring_class->GetClassLoader() == nullptr;
1687  if (!use_dex_cache) {
1688    if (!method_code_in_boot) {
1689      use_dex_cache = true;
1690    } else {
1691      bool has_clinit_trampoline =
1692          method->IsStatic() && !declaring_class->IsInitialized();
1693      if (has_clinit_trampoline && declaring_class != referrer_class) {
1694        // Ensure we run the clinit trampoline unless we are invoking a static method in the same
1695        // class.
1696        use_dex_cache = true;
1697      }
1698    }
1699  }
1700  if (runtime->UseJitCompilation()) {
1701    // If we are the JIT, then don't allow a direct call to the interpreter bridge since this will
1702    // never be updated even after we compile the method.
1703    if (cl->IsQuickToInterpreterBridge(
1704        reinterpret_cast<const void*>(compiler_->GetEntryPointOf(method)))) {
1705      use_dex_cache = true;
1706    }
1707  }
1708  if (method_code_in_boot) {
1709    *stats_flags |= kFlagDirectCallToBoot | kFlagDirectMethodToBoot;
1710  }
1711  if (!use_dex_cache && force_relocations) {
1712    bool is_in_image;
1713    if (IsBootImage()) {
1714      is_in_image = IsImageClass(method->GetDeclaringClassDescriptor());
1715    } else {
1716      is_in_image = instruction_set_ != kX86 && instruction_set_ != kX86_64 &&
1717                    heap->FindSpaceFromObject(method->GetDeclaringClass(), false)->IsImageSpace() &&
1718                    !cl->IsQuickToInterpreterBridge(
1719                        reinterpret_cast<const void*>(compiler_->GetEntryPointOf(method)));
1720    }
1721    if (!is_in_image) {
1722      // We can only branch directly to Methods that are resolved in the DexCache.
1723      // Otherwise we won't invoke the resolution trampoline.
1724      use_dex_cache = true;
1725    }
1726  }
1727  // The method is defined not within this dex file. We need a dex cache slot within the current
1728  // dex file or direct pointers.
1729  bool must_use_direct_pointers = false;
1730  mirror::DexCache* dex_cache = declaring_class->GetDexCache();
1731  if (target_method->dex_file == dex_cache->GetDexFile() &&
1732    !(runtime->UseJitCompilation() && dex_cache->GetResolvedMethod(
1733        method->GetDexMethodIndex(), pointer_size) == nullptr)) {
1734    target_method->dex_method_index = method->GetDexMethodIndex();
1735  } else {
1736    if (no_guarantee_of_dex_cache_entry) {
1737      // See if the method is also declared in this dex cache.
1738      uint32_t dex_method_idx = method->FindDexMethodIndexInOtherDexFile(
1739          *target_method->dex_file, target_method->dex_method_index);
1740      if (dex_method_idx != DexFile::kDexNoIndex) {
1741        target_method->dex_method_index = dex_method_idx;
1742      } else {
1743        if (force_relocations && !use_dex_cache) {
1744          target_method->dex_method_index = method->GetDexMethodIndex();
1745          target_method->dex_file = dex_cache->GetDexFile();
1746        }
1747        must_use_direct_pointers = true;
1748      }
1749    }
1750  }
1751  if (use_dex_cache) {
1752    if (must_use_direct_pointers) {
1753      // Fail. Test above showed the only safe dispatch was via the dex cache, however, the direct
1754      // pointers are required as the dex cache lacks an appropriate entry.
1755      VLOG(compiler) << "Dex cache devirtualization failed for: " << PrettyMethod(method);
1756    } else {
1757      *type = sharp_type;
1758    }
1759  } else {
1760    bool method_in_image = false;
1761    const std::vector<gc::space::ImageSpace*> image_spaces = heap->GetBootImageSpaces();
1762    for (gc::space::ImageSpace* image_space : image_spaces) {
1763      const auto& method_section = image_space->GetImageHeader().GetMethodsSection();
1764      if (method_section.Contains(reinterpret_cast<uint8_t*>(method) - image_space->Begin())) {
1765        method_in_image = true;
1766        break;
1767      }
1768    }
1769    if (method_in_image || compiling_boot || runtime->UseJitCompilation()) {
1770      // We know we must be able to get to the method in the image, so use that pointer.
1771      // In the case where we are the JIT, we can always use direct pointers since we know where
1772      // the method and its code are / will be. We don't sharpen to interpreter bridge since we
1773      // check IsQuickToInterpreterBridge above.
1774      CHECK(!method->IsAbstract());
1775      *type = sharp_type;
1776      *direct_method = force_relocations ? -1 : reinterpret_cast<uintptr_t>(method);
1777      *direct_code = force_relocations ? -1 : compiler_->GetEntryPointOf(method);
1778      target_method->dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1779      target_method->dex_method_index = method->GetDexMethodIndex();
1780    } else if (!must_use_direct_pointers) {
1781      // Set the code and rely on the dex cache for the method.
1782      *type = sharp_type;
1783      if (force_relocations) {
1784        *direct_code = -1;
1785        target_method->dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1786        target_method->dex_method_index = method->GetDexMethodIndex();
1787      } else {
1788        *direct_code = compiler_->GetEntryPointOf(method);
1789      }
1790    } else {
1791      // Direct pointers were required but none were available.
1792      VLOG(compiler) << "Dex cache devirtualization failed for: " << PrettyMethod(method);
1793    }
1794  }
1795}
1796
1797bool CompilerDriver::ComputeInvokeInfo(const DexCompilationUnit* mUnit, const uint32_t dex_pc,
1798                                       bool update_stats, bool enable_devirtualization,
1799                                       InvokeType* invoke_type, MethodReference* target_method,
1800                                       int* vtable_idx, uintptr_t* direct_code,
1801                                       uintptr_t* direct_method) {
1802  InvokeType orig_invoke_type = *invoke_type;
1803  int stats_flags = 0;
1804  ScopedObjectAccess soa(Thread::Current());
1805  // Try to resolve the method and compiling method's class.
1806  StackHandleScope<2> hs(soa.Self());
1807  Handle<mirror::DexCache> dex_cache(mUnit->GetDexCache());
1808  Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1809      soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader())));
1810  uint32_t method_idx = target_method->dex_method_index;
1811  ArtMethod* resolved_method = ResolveMethod(
1812      soa, dex_cache, class_loader, mUnit, method_idx, orig_invoke_type);
1813  auto h_referrer_class = hs.NewHandle(resolved_method != nullptr ?
1814      ResolveCompilingMethodsClass(soa, dex_cache, class_loader, mUnit) : nullptr);
1815  bool result = false;
1816  if (resolved_method != nullptr) {
1817    *vtable_idx = GetResolvedMethodVTableIndex(resolved_method, orig_invoke_type);
1818
1819    if (enable_devirtualization && mUnit->GetVerifiedMethod() != nullptr) {
1820      const MethodReference* devirt_target = mUnit->GetVerifiedMethod()->GetDevirtTarget(dex_pc);
1821
1822      stats_flags = IsFastInvoke(
1823          soa, dex_cache, class_loader, mUnit, h_referrer_class.Get(), resolved_method,
1824          invoke_type, target_method, devirt_target, direct_code, direct_method);
1825      result = stats_flags != 0;
1826    } else {
1827      // Devirtualization not enabled. Inline IsFastInvoke(), dropping the devirtualization parts.
1828      if (UNLIKELY(h_referrer_class.Get() == nullptr) ||
1829          UNLIKELY(!h_referrer_class->CanAccessResolvedMethod(resolved_method->GetDeclaringClass(),
1830                                                            resolved_method, dex_cache.Get(),
1831                                                            target_method->dex_method_index)) ||
1832          *invoke_type == kSuper) {
1833        // Slow path. (Without devirtualization, all super calls go slow path as well.)
1834      } else {
1835        // Sharpening failed so generate a regular resolved method dispatch.
1836        stats_flags = kFlagMethodResolved;
1837        GetCodeAndMethodForDirectCall(
1838            invoke_type, *invoke_type, false, h_referrer_class.Get(), resolved_method, &stats_flags,
1839            target_method, direct_code, direct_method);
1840        result = true;
1841      }
1842    }
1843  }
1844  if (!result) {
1845    // Conservative defaults.
1846    *vtable_idx = -1;
1847    *direct_code = 0u;
1848    *direct_method = 0u;
1849  }
1850  if (update_stats) {
1851    ProcessedInvoke(orig_invoke_type, stats_flags);
1852  }
1853  return result;
1854}
1855
1856const VerifiedMethod* CompilerDriver::GetVerifiedMethod(const DexFile* dex_file,
1857                                                        uint32_t method_idx) const {
1858  MethodReference ref(dex_file, method_idx);
1859  return verification_results_->GetVerifiedMethod(ref);
1860}
1861
1862bool CompilerDriver::IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc) {
1863  if (!compiler_options_->IsVerificationEnabled()) {
1864    // If we didn't verify, every cast has to be treated as non-safe.
1865    return false;
1866  }
1867  DCHECK(mUnit->GetVerifiedMethod() != nullptr);
1868  bool result = mUnit->GetVerifiedMethod()->IsSafeCast(dex_pc);
1869  if (result) {
1870    stats_->SafeCast();
1871  } else {
1872    stats_->NotASafeCast();
1873  }
1874  return result;
1875}
1876
1877class CompilationVisitor {
1878 public:
1879  virtual ~CompilationVisitor() {}
1880  virtual void Visit(size_t index) = 0;
1881};
1882
1883class ParallelCompilationManager {
1884 public:
1885  ParallelCompilationManager(ClassLinker* class_linker,
1886                             jobject class_loader,
1887                             CompilerDriver* compiler,
1888                             const DexFile* dex_file,
1889                             const std::vector<const DexFile*>& dex_files,
1890                             ThreadPool* thread_pool)
1891    : index_(0),
1892      class_linker_(class_linker),
1893      class_loader_(class_loader),
1894      compiler_(compiler),
1895      dex_file_(dex_file),
1896      dex_files_(dex_files),
1897      thread_pool_(thread_pool) {}
1898
1899  ClassLinker* GetClassLinker() const {
1900    CHECK(class_linker_ != nullptr);
1901    return class_linker_;
1902  }
1903
1904  jobject GetClassLoader() const {
1905    return class_loader_;
1906  }
1907
1908  CompilerDriver* GetCompiler() const {
1909    CHECK(compiler_ != nullptr);
1910    return compiler_;
1911  }
1912
1913  const DexFile* GetDexFile() const {
1914    CHECK(dex_file_ != nullptr);
1915    return dex_file_;
1916  }
1917
1918  const std::vector<const DexFile*>& GetDexFiles() const {
1919    return dex_files_;
1920  }
1921
1922  void ForAll(size_t begin, size_t end, CompilationVisitor* visitor, size_t work_units)
1923      REQUIRES(!*Locks::mutator_lock_) {
1924    Thread* self = Thread::Current();
1925    self->AssertNoPendingException();
1926    CHECK_GT(work_units, 0U);
1927
1928    index_.StoreRelaxed(begin);
1929    for (size_t i = 0; i < work_units; ++i) {
1930      thread_pool_->AddTask(self, new ForAllClosure(this, end, visitor));
1931    }
1932    thread_pool_->StartWorkers(self);
1933
1934    // Ensure we're suspended while we're blocked waiting for the other threads to finish (worker
1935    // thread destructor's called below perform join).
1936    CHECK_NE(self->GetState(), kRunnable);
1937
1938    // Wait for all the worker threads to finish.
1939    thread_pool_->Wait(self, true, false);
1940
1941    // And stop the workers accepting jobs.
1942    thread_pool_->StopWorkers(self);
1943  }
1944
1945  size_t NextIndex() {
1946    return index_.FetchAndAddSequentiallyConsistent(1);
1947  }
1948
1949 private:
1950  class ForAllClosure : public Task {
1951   public:
1952    ForAllClosure(ParallelCompilationManager* manager, size_t end, CompilationVisitor* visitor)
1953        : manager_(manager),
1954          end_(end),
1955          visitor_(visitor) {}
1956
1957    virtual void Run(Thread* self) {
1958      while (true) {
1959        const size_t index = manager_->NextIndex();
1960        if (UNLIKELY(index >= end_)) {
1961          break;
1962        }
1963        visitor_->Visit(index);
1964        self->AssertNoPendingException();
1965      }
1966    }
1967
1968    virtual void Finalize() {
1969      delete this;
1970    }
1971
1972   private:
1973    ParallelCompilationManager* const manager_;
1974    const size_t end_;
1975    CompilationVisitor* const visitor_;
1976  };
1977
1978  AtomicInteger index_;
1979  ClassLinker* const class_linker_;
1980  const jobject class_loader_;
1981  CompilerDriver* const compiler_;
1982  const DexFile* const dex_file_;
1983  const std::vector<const DexFile*>& dex_files_;
1984  ThreadPool* const thread_pool_;
1985
1986  DISALLOW_COPY_AND_ASSIGN(ParallelCompilationManager);
1987};
1988
1989// A fast version of SkipClass above if the class pointer is available
1990// that avoids the expensive FindInClassPath search.
1991static bool SkipClass(jobject class_loader, const DexFile& dex_file, mirror::Class* klass)
1992    REQUIRES_SHARED(Locks::mutator_lock_) {
1993  DCHECK(klass != nullptr);
1994  const DexFile& original_dex_file = *klass->GetDexCache()->GetDexFile();
1995  if (&dex_file != &original_dex_file) {
1996    if (class_loader == nullptr) {
1997      LOG(WARNING) << "Skipping class " << PrettyDescriptor(klass) << " from "
1998                   << dex_file.GetLocation() << " previously found in "
1999                   << original_dex_file.GetLocation();
2000    }
2001    return true;
2002  }
2003  return false;
2004}
2005
2006static void CheckAndClearResolveException(Thread* self)
2007    REQUIRES_SHARED(Locks::mutator_lock_) {
2008  CHECK(self->IsExceptionPending());
2009  mirror::Throwable* exception = self->GetException();
2010  std::string temp;
2011  const char* descriptor = exception->GetClass()->GetDescriptor(&temp);
2012  const char* expected_exceptions[] = {
2013      "Ljava/lang/IllegalAccessError;",
2014      "Ljava/lang/IncompatibleClassChangeError;",
2015      "Ljava/lang/InstantiationError;",
2016      "Ljava/lang/LinkageError;",
2017      "Ljava/lang/NoClassDefFoundError;",
2018      "Ljava/lang/NoSuchFieldError;",
2019      "Ljava/lang/NoSuchMethodError;"
2020  };
2021  bool found = false;
2022  for (size_t i = 0; (found == false) && (i < arraysize(expected_exceptions)); ++i) {
2023    if (strcmp(descriptor, expected_exceptions[i]) == 0) {
2024      found = true;
2025    }
2026  }
2027  if (!found) {
2028    LOG(FATAL) << "Unexpected exception " << exception->Dump();
2029  }
2030  self->ClearException();
2031}
2032
2033bool CompilerDriver::RequiresConstructorBarrier(const DexFile& dex_file,
2034                                                uint16_t class_def_idx) const {
2035  const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_idx);
2036  const uint8_t* class_data = dex_file.GetClassData(class_def);
2037  if (class_data == nullptr) {
2038    // Empty class such as a marker interface.
2039    return false;
2040  }
2041  ClassDataItemIterator it(dex_file, class_data);
2042  while (it.HasNextStaticField()) {
2043    it.Next();
2044  }
2045  // We require a constructor barrier if there are final instance fields.
2046  while (it.HasNextInstanceField()) {
2047    if (it.MemberIsFinal()) {
2048      return true;
2049    }
2050    it.Next();
2051  }
2052  return false;
2053}
2054
2055class ResolveClassFieldsAndMethodsVisitor : public CompilationVisitor {
2056 public:
2057  explicit ResolveClassFieldsAndMethodsVisitor(const ParallelCompilationManager* manager)
2058      : manager_(manager) {}
2059
2060  void Visit(size_t class_def_index) OVERRIDE REQUIRES(!Locks::mutator_lock_) {
2061    ATRACE_CALL();
2062    Thread* const self = Thread::Current();
2063    jobject jclass_loader = manager_->GetClassLoader();
2064    const DexFile& dex_file = *manager_->GetDexFile();
2065    ClassLinker* class_linker = manager_->GetClassLinker();
2066
2067    // If an instance field is final then we need to have a barrier on the return, static final
2068    // fields are assigned within the lock held for class initialization. Conservatively assume
2069    // constructor barriers are always required.
2070    bool requires_constructor_barrier = true;
2071
2072    // Method and Field are the worst. We can't resolve without either
2073    // context from the code use (to disambiguate virtual vs direct
2074    // method and instance vs static field) or from class
2075    // definitions. While the compiler will resolve what it can as it
2076    // needs it, here we try to resolve fields and methods used in class
2077    // definitions, since many of them many never be referenced by
2078    // generated code.
2079    const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
2080    ScopedObjectAccess soa(self);
2081    StackHandleScope<2> hs(soa.Self());
2082    Handle<mirror::ClassLoader> class_loader(
2083        hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
2084    Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(
2085        soa.Self(), dex_file, false)));
2086    // Resolve the class.
2087    mirror::Class* klass = class_linker->ResolveType(dex_file, class_def.class_idx_, dex_cache,
2088                                                     class_loader);
2089    bool resolve_fields_and_methods;
2090    if (klass == nullptr) {
2091      // Class couldn't be resolved, for example, super-class is in a different dex file. Don't
2092      // attempt to resolve methods and fields when there is no declaring class.
2093      CheckAndClearResolveException(soa.Self());
2094      resolve_fields_and_methods = false;
2095    } else {
2096      // We successfully resolved a class, should we skip it?
2097      if (SkipClass(jclass_loader, dex_file, klass)) {
2098        return;
2099      }
2100      // We want to resolve the methods and fields eagerly.
2101      resolve_fields_and_methods = true;
2102    }
2103    // Note the class_data pointer advances through the headers,
2104    // static fields, instance fields, direct methods, and virtual
2105    // methods.
2106    const uint8_t* class_data = dex_file.GetClassData(class_def);
2107    if (class_data == nullptr) {
2108      // Empty class such as a marker interface.
2109      requires_constructor_barrier = false;
2110    } else {
2111      ClassDataItemIterator it(dex_file, class_data);
2112      while (it.HasNextStaticField()) {
2113        if (resolve_fields_and_methods) {
2114          ArtField* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(),
2115                                                               dex_cache, class_loader, true);
2116          if (field == nullptr) {
2117            CheckAndClearResolveException(soa.Self());
2118          }
2119        }
2120        it.Next();
2121      }
2122      // We require a constructor barrier if there are final instance fields.
2123      requires_constructor_barrier = false;
2124      while (it.HasNextInstanceField()) {
2125        if (it.MemberIsFinal()) {
2126          requires_constructor_barrier = true;
2127        }
2128        if (resolve_fields_and_methods) {
2129          ArtField* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(),
2130                                                               dex_cache, class_loader, false);
2131          if (field == nullptr) {
2132            CheckAndClearResolveException(soa.Self());
2133          }
2134        }
2135        it.Next();
2136      }
2137      if (resolve_fields_and_methods) {
2138        while (it.HasNextDirectMethod()) {
2139          ArtMethod* method = class_linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
2140              dex_file, it.GetMemberIndex(), dex_cache, class_loader, nullptr,
2141              it.GetMethodInvokeType(class_def));
2142          if (method == nullptr) {
2143            CheckAndClearResolveException(soa.Self());
2144          }
2145          it.Next();
2146        }
2147        while (it.HasNextVirtualMethod()) {
2148          ArtMethod* method = class_linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
2149              dex_file, it.GetMemberIndex(), dex_cache, class_loader, nullptr,
2150              it.GetMethodInvokeType(class_def));
2151          if (method == nullptr) {
2152            CheckAndClearResolveException(soa.Self());
2153          }
2154          it.Next();
2155        }
2156        DCHECK(!it.HasNext());
2157      }
2158    }
2159    manager_->GetCompiler()->SetRequiresConstructorBarrier(self,
2160                                                           &dex_file,
2161                                                           class_def_index,
2162                                                           requires_constructor_barrier);
2163  }
2164
2165 private:
2166  const ParallelCompilationManager* const manager_;
2167};
2168
2169class ResolveTypeVisitor : public CompilationVisitor {
2170 public:
2171  explicit ResolveTypeVisitor(const ParallelCompilationManager* manager) : manager_(manager) {
2172  }
2173  virtual void Visit(size_t type_idx) OVERRIDE REQUIRES(!Locks::mutator_lock_) {
2174  // Class derived values are more complicated, they require the linker and loader.
2175    ScopedObjectAccess soa(Thread::Current());
2176    ClassLinker* class_linker = manager_->GetClassLinker();
2177    const DexFile& dex_file = *manager_->GetDexFile();
2178    StackHandleScope<2> hs(soa.Self());
2179    Handle<mirror::ClassLoader> class_loader(
2180        hs.NewHandle(soa.Decode<mirror::ClassLoader*>(manager_->GetClassLoader())));
2181    Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->RegisterDexFile(
2182        dex_file,
2183        class_loader.Get())));
2184    mirror::Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
2185
2186    if (klass == nullptr) {
2187      soa.Self()->AssertPendingException();
2188      mirror::Throwable* exception = soa.Self()->GetException();
2189      VLOG(compiler) << "Exception during type resolution: " << exception->Dump();
2190      if (exception->GetClass()->DescriptorEquals("Ljava/lang/OutOfMemoryError;")) {
2191        // There's little point continuing compilation if the heap is exhausted.
2192        LOG(FATAL) << "Out of memory during type resolution for compilation";
2193      }
2194      soa.Self()->ClearException();
2195    }
2196  }
2197
2198 private:
2199  const ParallelCompilationManager* const manager_;
2200};
2201
2202void CompilerDriver::ResolveDexFile(jobject class_loader,
2203                                    const DexFile& dex_file,
2204                                    const std::vector<const DexFile*>& dex_files,
2205                                    ThreadPool* thread_pool,
2206                                    size_t thread_count,
2207                                    TimingLogger* timings) {
2208  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2209
2210  // TODO: we could resolve strings here, although the string table is largely filled with class
2211  //       and method names.
2212
2213  ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, dex_files,
2214                                     thread_pool);
2215  if (IsBootImage()) {
2216    // For images we resolve all types, such as array, whereas for applications just those with
2217    // classdefs are resolved by ResolveClassFieldsAndMethods.
2218    TimingLogger::ScopedTiming t("Resolve Types", timings);
2219    ResolveTypeVisitor visitor(&context);
2220    context.ForAll(0, dex_file.NumTypeIds(), &visitor, thread_count);
2221  }
2222
2223  TimingLogger::ScopedTiming t("Resolve MethodsAndFields", timings);
2224  ResolveClassFieldsAndMethodsVisitor visitor(&context);
2225  context.ForAll(0, dex_file.NumClassDefs(), &visitor, thread_count);
2226}
2227
2228void CompilerDriver::SetVerified(jobject class_loader,
2229                                 const std::vector<const DexFile*>& dex_files,
2230                                 TimingLogger* timings) {
2231  // This can be run in parallel.
2232  for (const DexFile* dex_file : dex_files) {
2233    CHECK(dex_file != nullptr);
2234    SetVerifiedDexFile(class_loader,
2235                       *dex_file,
2236                       dex_files,
2237                       parallel_thread_pool_.get(),
2238                       parallel_thread_count_,
2239                       timings);
2240  }
2241}
2242
2243void CompilerDriver::Verify(jobject class_loader,
2244                            const std::vector<const DexFile*>& dex_files,
2245                            TimingLogger* timings) {
2246  // Note: verification should not be pulling in classes anymore when compiling the boot image,
2247  //       as all should have been resolved before. As such, doing this in parallel should still
2248  //       be deterministic.
2249  for (const DexFile* dex_file : dex_files) {
2250    CHECK(dex_file != nullptr);
2251    VerifyDexFile(class_loader,
2252                  *dex_file,
2253                  dex_files,
2254                  parallel_thread_pool_.get(),
2255                  parallel_thread_count_,
2256                  timings);
2257  }
2258}
2259
2260class VerifyClassVisitor : public CompilationVisitor {
2261 public:
2262  VerifyClassVisitor(const ParallelCompilationManager* manager, verifier::HardFailLogMode log_level)
2263     : manager_(manager), log_level_(log_level) {}
2264
2265  virtual void Visit(size_t class_def_index) REQUIRES(!Locks::mutator_lock_) OVERRIDE {
2266    ATRACE_CALL();
2267    ScopedObjectAccess soa(Thread::Current());
2268    const DexFile& dex_file = *manager_->GetDexFile();
2269    if (!manager_->GetCompiler()->ShouldVerifyClassBasedOnProfile(dex_file, class_def_index)) {
2270      // Skip verification since the class is not in the profile.
2271      return;
2272    }
2273    const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
2274    const char* descriptor = dex_file.GetClassDescriptor(class_def);
2275    ClassLinker* class_linker = manager_->GetClassLinker();
2276    jobject jclass_loader = manager_->GetClassLoader();
2277    StackHandleScope<3> hs(soa.Self());
2278    Handle<mirror::ClassLoader> class_loader(
2279        hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
2280    Handle<mirror::Class> klass(
2281        hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor, class_loader)));
2282    if (klass.Get() == nullptr) {
2283      CHECK(soa.Self()->IsExceptionPending());
2284      soa.Self()->ClearException();
2285
2286      /*
2287       * At compile time, we can still structurally verify the class even if FindClass fails.
2288       * This is to ensure the class is structurally sound for compilation. An unsound class
2289       * will be rejected by the verifier and later skipped during compilation in the compiler.
2290       */
2291      Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(
2292          soa.Self(), dex_file, false)));
2293      std::string error_msg;
2294      if (verifier::MethodVerifier::VerifyClass(soa.Self(),
2295                                                &dex_file,
2296                                                dex_cache,
2297                                                class_loader,
2298                                                class_def,
2299                                                Runtime::Current()->GetCompilerCallbacks(),
2300                                                true /* allow soft failures */,
2301                                                log_level_,
2302                                                &error_msg) ==
2303                                                    verifier::MethodVerifier::kHardFailure) {
2304        LOG(ERROR) << "Verification failed on class " << PrettyDescriptor(descriptor)
2305                   << " because: " << error_msg;
2306        manager_->GetCompiler()->SetHadHardVerifierFailure();
2307      }
2308    } else if (!SkipClass(jclass_loader, dex_file, klass.Get())) {
2309      CHECK(klass->IsResolved()) << PrettyClass(klass.Get());
2310      class_linker->VerifyClass(soa.Self(), klass, log_level_);
2311
2312      if (klass->IsErroneous()) {
2313        // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
2314        CHECK(soa.Self()->IsExceptionPending());
2315        soa.Self()->ClearException();
2316        manager_->GetCompiler()->SetHadHardVerifierFailure();
2317      }
2318
2319      CHECK(klass->IsCompileTimeVerified() || klass->IsErroneous())
2320          << PrettyDescriptor(klass.Get()) << ": state=" << klass->GetStatus();
2321
2322      // It is *very* problematic if there are verification errors in the boot classpath. For example,
2323      // we rely on things working OK without verification when the decryption dialog is brought up.
2324      // So abort in a debug build if we find this violated.
2325      DCHECK(!manager_->GetCompiler()->IsBootImage() || klass->IsVerified())
2326          << "Boot classpath class " << PrettyClass(klass.Get()) << " failed to fully verify.";
2327    }
2328    soa.Self()->AssertNoPendingException();
2329  }
2330
2331 private:
2332  const ParallelCompilationManager* const manager_;
2333  const verifier::HardFailLogMode log_level_;
2334};
2335
2336void CompilerDriver::VerifyDexFile(jobject class_loader,
2337                                   const DexFile& dex_file,
2338                                   const std::vector<const DexFile*>& dex_files,
2339                                   ThreadPool* thread_pool,
2340                                   size_t thread_count,
2341                                   TimingLogger* timings) {
2342  TimingLogger::ScopedTiming t("Verify Dex File", timings);
2343  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2344  ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, dex_files,
2345                                     thread_pool);
2346  verifier::HardFailLogMode log_level = GetCompilerOptions().AbortOnHardVerifierFailure()
2347                              ? verifier::HardFailLogMode::kLogInternalFatal
2348                              : verifier::HardFailLogMode::kLogWarning;
2349  VerifyClassVisitor visitor(&context, log_level);
2350  context.ForAll(0, dex_file.NumClassDefs(), &visitor, thread_count);
2351}
2352
2353class SetVerifiedClassVisitor : public CompilationVisitor {
2354 public:
2355  explicit SetVerifiedClassVisitor(const ParallelCompilationManager* manager) : manager_(manager) {}
2356
2357  virtual void Visit(size_t class_def_index) REQUIRES(!Locks::mutator_lock_) OVERRIDE {
2358    ATRACE_CALL();
2359    ScopedObjectAccess soa(Thread::Current());
2360    const DexFile& dex_file = *manager_->GetDexFile();
2361    const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
2362    const char* descriptor = dex_file.GetClassDescriptor(class_def);
2363    ClassLinker* class_linker = manager_->GetClassLinker();
2364    jobject jclass_loader = manager_->GetClassLoader();
2365    StackHandleScope<3> hs(soa.Self());
2366    Handle<mirror::ClassLoader> class_loader(
2367        hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
2368    Handle<mirror::Class> klass(
2369        hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor, class_loader)));
2370    // Class might have failed resolution. Then don't set it to verified.
2371    if (klass.Get() != nullptr) {
2372      // Only do this if the class is resolved. If even resolution fails, quickening will go very,
2373      // very wrong.
2374      if (klass->IsResolved()) {
2375        if (klass->GetStatus() < mirror::Class::kStatusVerified) {
2376          ObjectLock<mirror::Class> lock(soa.Self(), klass);
2377          // Set class status to verified.
2378          mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, soa.Self());
2379          // Mark methods as pre-verified. If we don't do this, the interpreter will run with
2380          // access checks.
2381          klass->SetSkipAccessChecksFlagOnAllMethods(
2382              GetInstructionSetPointerSize(manager_->GetCompiler()->GetInstructionSet()));
2383          klass->SetVerificationAttempted();
2384        }
2385        // Record the final class status if necessary.
2386        ClassReference ref(manager_->GetDexFile(), class_def_index);
2387        manager_->GetCompiler()->RecordClassStatus(ref, klass->GetStatus());
2388      }
2389    } else {
2390      Thread* self = soa.Self();
2391      DCHECK(self->IsExceptionPending());
2392      self->ClearException();
2393    }
2394  }
2395
2396 private:
2397  const ParallelCompilationManager* const manager_;
2398};
2399
2400void CompilerDriver::SetVerifiedDexFile(jobject class_loader,
2401                                        const DexFile& dex_file,
2402                                        const std::vector<const DexFile*>& dex_files,
2403                                        ThreadPool* thread_pool,
2404                                        size_t thread_count,
2405                                        TimingLogger* timings) {
2406  TimingLogger::ScopedTiming t("Verify Dex File", timings);
2407  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2408  ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, dex_files,
2409                                     thread_pool);
2410  SetVerifiedClassVisitor visitor(&context);
2411  context.ForAll(0, dex_file.NumClassDefs(), &visitor, thread_count);
2412}
2413
2414class InitializeClassVisitor : public CompilationVisitor {
2415 public:
2416  explicit InitializeClassVisitor(const ParallelCompilationManager* manager) : manager_(manager) {}
2417
2418  virtual void Visit(size_t class_def_index) REQUIRES(!Locks::mutator_lock_) OVERRIDE {
2419    ATRACE_CALL();
2420    jobject jclass_loader = manager_->GetClassLoader();
2421    const DexFile& dex_file = *manager_->GetDexFile();
2422    const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
2423    const DexFile::TypeId& class_type_id = dex_file.GetTypeId(class_def.class_idx_);
2424    const char* descriptor = dex_file.StringDataByIdx(class_type_id.descriptor_idx_);
2425
2426    ScopedObjectAccess soa(Thread::Current());
2427    StackHandleScope<3> hs(soa.Self());
2428    Handle<mirror::ClassLoader> class_loader(
2429        hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
2430    Handle<mirror::Class> klass(
2431        hs.NewHandle(manager_->GetClassLinker()->FindClass(soa.Self(), descriptor, class_loader)));
2432
2433    if (klass.Get() != nullptr && !SkipClass(jclass_loader, dex_file, klass.Get())) {
2434      // Only try to initialize classes that were successfully verified.
2435      if (klass->IsVerified()) {
2436        // Attempt to initialize the class but bail if we either need to initialize the super-class
2437        // or static fields.
2438        manager_->GetClassLinker()->EnsureInitialized(soa.Self(), klass, false, false);
2439        if (!klass->IsInitialized()) {
2440          // We don't want non-trivial class initialization occurring on multiple threads due to
2441          // deadlock problems. For example, a parent class is initialized (holding its lock) that
2442          // refers to a sub-class in its static/class initializer causing it to try to acquire the
2443          // sub-class' lock. While on a second thread the sub-class is initialized (holding its lock)
2444          // after first initializing its parents, whose locks are acquired. This leads to a
2445          // parent-to-child and a child-to-parent lock ordering and consequent potential deadlock.
2446          // We need to use an ObjectLock due to potential suspension in the interpreting code. Rather
2447          // than use a special Object for the purpose we use the Class of java.lang.Class.
2448          Handle<mirror::Class> h_klass(hs.NewHandle(klass->GetClass()));
2449          ObjectLock<mirror::Class> lock(soa.Self(), h_klass);
2450          // Attempt to initialize allowing initialization of parent classes but still not static
2451          // fields.
2452          manager_->GetClassLinker()->EnsureInitialized(soa.Self(), klass, false, true);
2453          if (!klass->IsInitialized()) {
2454            // We need to initialize static fields, we only do this for image classes that aren't
2455            // marked with the $NoPreloadHolder (which implies this should not be initialized early).
2456            bool can_init_static_fields = manager_->GetCompiler()->IsBootImage() &&
2457                manager_->GetCompiler()->IsImageClass(descriptor) &&
2458                !StringPiece(descriptor).ends_with("$NoPreloadHolder;");
2459            if (can_init_static_fields) {
2460              VLOG(compiler) << "Initializing: " << descriptor;
2461              // TODO multithreading support. We should ensure the current compilation thread has
2462              // exclusive access to the runtime and the transaction. To achieve this, we could use
2463              // a ReaderWriterMutex but we're holding the mutator lock so we fail mutex sanity
2464              // checks in Thread::AssertThreadSuspensionIsAllowable.
2465              Runtime* const runtime = Runtime::Current();
2466              Transaction transaction;
2467
2468              // Run the class initializer in transaction mode.
2469              runtime->EnterTransactionMode(&transaction);
2470              const mirror::Class::Status old_status = klass->GetStatus();
2471              bool success = manager_->GetClassLinker()->EnsureInitialized(soa.Self(), klass, true,
2472                                                                           true);
2473              // TODO we detach transaction from runtime to indicate we quit the transactional
2474              // mode which prevents the GC from visiting objects modified during the transaction.
2475              // Ensure GC is not run so don't access freed objects when aborting transaction.
2476
2477              ScopedAssertNoThreadSuspension ants(soa.Self(), "Transaction end");
2478              runtime->ExitTransactionMode();
2479
2480              if (!success) {
2481                CHECK(soa.Self()->IsExceptionPending());
2482                mirror::Throwable* exception = soa.Self()->GetException();
2483                VLOG(compiler) << "Initialization of " << descriptor << " aborted because of "
2484                    << exception->Dump();
2485                std::ostream* file_log = manager_->GetCompiler()->
2486                    GetCompilerOptions().GetInitFailureOutput();
2487                if (file_log != nullptr) {
2488                  *file_log << descriptor << "\n";
2489                  *file_log << exception->Dump() << "\n";
2490                }
2491                soa.Self()->ClearException();
2492                transaction.Rollback();
2493                CHECK_EQ(old_status, klass->GetStatus()) << "Previous class status not restored";
2494              }
2495            }
2496          }
2497          soa.Self()->AssertNoPendingException();
2498        }
2499      }
2500      // Record the final class status if necessary.
2501      ClassReference ref(manager_->GetDexFile(), class_def_index);
2502      manager_->GetCompiler()->RecordClassStatus(ref, klass->GetStatus());
2503    }
2504    // Clear any class not found or verification exceptions.
2505    soa.Self()->ClearException();
2506  }
2507
2508 private:
2509  const ParallelCompilationManager* const manager_;
2510};
2511
2512void CompilerDriver::InitializeClasses(jobject jni_class_loader,
2513                                       const DexFile& dex_file,
2514                                       const std::vector<const DexFile*>& dex_files,
2515                                       TimingLogger* timings) {
2516  TimingLogger::ScopedTiming t("InitializeNoClinit", timings);
2517
2518  // Initialization allocates objects and needs to run single-threaded to be deterministic.
2519  bool force_determinism = GetCompilerOptions().IsForceDeterminism();
2520  ThreadPool* init_thread_pool = force_determinism
2521                                     ? single_thread_pool_.get()
2522                                     : parallel_thread_pool_.get();
2523  size_t init_thread_count = force_determinism ? 1U : parallel_thread_count_;
2524
2525  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2526  ParallelCompilationManager context(class_linker, jni_class_loader, this, &dex_file, dex_files,
2527                                     init_thread_pool);
2528  if (IsBootImage()) {
2529    // TODO: remove this when transactional mode supports multithreading.
2530    init_thread_count = 1U;
2531  }
2532  InitializeClassVisitor visitor(&context);
2533  context.ForAll(0, dex_file.NumClassDefs(), &visitor, init_thread_count);
2534}
2535
2536class InitializeArrayClassesAndCreateConflictTablesVisitor : public ClassVisitor {
2537 public:
2538  virtual bool operator()(mirror::Class* klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
2539    if (Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) {
2540      return true;
2541    }
2542    if (klass->IsArrayClass()) {
2543      StackHandleScope<1> hs(Thread::Current());
2544      Runtime::Current()->GetClassLinker()->EnsureInitialized(hs.Self(),
2545                                                              hs.NewHandle(klass),
2546                                                              true,
2547                                                              true);
2548    }
2549    // Create the conflict tables.
2550    FillIMTAndConflictTables(klass);
2551    return true;
2552  }
2553
2554 private:
2555  void FillIMTAndConflictTables(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_) {
2556    if (!klass->ShouldHaveImt()) {
2557      return;
2558    }
2559    if (visited_classes_.find(klass) != visited_classes_.end()) {
2560      return;
2561    }
2562    if (klass->HasSuperClass()) {
2563      FillIMTAndConflictTables(klass->GetSuperClass());
2564    }
2565    if (!klass->IsTemp()) {
2566      Runtime::Current()->GetClassLinker()->FillIMTAndConflictTables(klass);
2567    }
2568    visited_classes_.insert(klass);
2569  }
2570
2571  std::set<mirror::Class*> visited_classes_;
2572};
2573
2574void CompilerDriver::InitializeClasses(jobject class_loader,
2575                                       const std::vector<const DexFile*>& dex_files,
2576                                       TimingLogger* timings) {
2577  for (size_t i = 0; i != dex_files.size(); ++i) {
2578    const DexFile* dex_file = dex_files[i];
2579    CHECK(dex_file != nullptr);
2580    InitializeClasses(class_loader, *dex_file, dex_files, timings);
2581  }
2582  if (boot_image_ || app_image_) {
2583    // Make sure that we call EnsureIntiailized on all the array classes to call
2584    // SetVerificationAttempted so that the access flags are set. If we do not do this they get
2585    // changed at runtime resulting in more dirty image pages.
2586    // Also create conflict tables.
2587    // Only useful if we are compiling an image (image_classes_ is not null).
2588    ScopedObjectAccess soa(Thread::Current());
2589    InitializeArrayClassesAndCreateConflictTablesVisitor visitor;
2590    Runtime::Current()->GetClassLinker()->VisitClassesWithoutClassesLock(&visitor);
2591  }
2592  if (IsBootImage()) {
2593    // Prune garbage objects created during aborted transactions.
2594    Runtime::Current()->GetHeap()->CollectGarbage(true);
2595  }
2596}
2597
2598void CompilerDriver::Compile(jobject class_loader,
2599                             const std::vector<const DexFile*>& dex_files,
2600                             TimingLogger* timings) {
2601  if (kDebugProfileGuidedCompilation) {
2602    LOG(INFO) << "[ProfileGuidedCompilation] " <<
2603        ((profile_compilation_info_ == nullptr)
2604            ? "null"
2605            : profile_compilation_info_->DumpInfo(&dex_files));
2606  }
2607
2608  DCHECK(current_dex_to_dex_methods_ == nullptr);
2609  for (const DexFile* dex_file : dex_files) {
2610    CHECK(dex_file != nullptr);
2611    CompileDexFile(class_loader,
2612                   *dex_file,
2613                   dex_files,
2614                   parallel_thread_pool_.get(),
2615                   parallel_thread_count_,
2616                   timings);
2617    const ArenaPool* const arena_pool = Runtime::Current()->GetArenaPool();
2618    const size_t arena_alloc = arena_pool->GetBytesAllocated();
2619    max_arena_alloc_ = std::max(arena_alloc, max_arena_alloc_);
2620    Runtime::Current()->ReclaimArenaPoolMemory();
2621  }
2622
2623  ArrayRef<DexFileMethodSet> dex_to_dex_references;
2624  {
2625    // From this point on, we shall not modify dex_to_dex_references_, so
2626    // just grab a reference to it that we use without holding the mutex.
2627    MutexLock lock(Thread::Current(), dex_to_dex_references_lock_);
2628    dex_to_dex_references = ArrayRef<DexFileMethodSet>(dex_to_dex_references_);
2629  }
2630  for (const auto& method_set : dex_to_dex_references) {
2631    current_dex_to_dex_methods_ = &method_set.GetMethodIndexes();
2632    CompileDexFile(class_loader,
2633                   method_set.GetDexFile(),
2634                   dex_files,
2635                   parallel_thread_pool_.get(),
2636                   parallel_thread_count_,
2637                   timings);
2638  }
2639  current_dex_to_dex_methods_ = nullptr;
2640
2641  VLOG(compiler) << "Compile: " << GetMemoryUsageString(false);
2642}
2643
2644class CompileClassVisitor : public CompilationVisitor {
2645 public:
2646  explicit CompileClassVisitor(const ParallelCompilationManager* manager) : manager_(manager) {}
2647
2648  virtual void Visit(size_t class_def_index) REQUIRES(!Locks::mutator_lock_) OVERRIDE {
2649    ATRACE_CALL();
2650    const DexFile& dex_file = *manager_->GetDexFile();
2651    const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
2652    ClassLinker* class_linker = manager_->GetClassLinker();
2653    jobject jclass_loader = manager_->GetClassLoader();
2654    ClassReference ref(&dex_file, class_def_index);
2655    // Skip compiling classes with generic verifier failures since they will still fail at runtime
2656    if (manager_->GetCompiler()->verification_results_->IsClassRejected(ref)) {
2657      return;
2658    }
2659    // Use a scoped object access to perform to the quick SkipClass check.
2660    const char* descriptor = dex_file.GetClassDescriptor(class_def);
2661    ScopedObjectAccess soa(Thread::Current());
2662    StackHandleScope<3> hs(soa.Self());
2663    Handle<mirror::ClassLoader> class_loader(
2664        hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
2665    Handle<mirror::Class> klass(
2666        hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor, class_loader)));
2667    Handle<mirror::DexCache> dex_cache;
2668    if (klass.Get() == nullptr) {
2669      soa.Self()->AssertPendingException();
2670      soa.Self()->ClearException();
2671      dex_cache = hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
2672    } else if (SkipClass(jclass_loader, dex_file, klass.Get())) {
2673      return;
2674    } else {
2675      dex_cache = hs.NewHandle(klass->GetDexCache());
2676    }
2677
2678    const uint8_t* class_data = dex_file.GetClassData(class_def);
2679    if (class_data == nullptr) {
2680      // empty class, probably a marker interface
2681      return;
2682    }
2683
2684    // Go to native so that we don't block GC during compilation.
2685    ScopedThreadSuspension sts(soa.Self(), kNative);
2686
2687    CompilerDriver* const driver = manager_->GetCompiler();
2688
2689    // Can we run DEX-to-DEX compiler on this class ?
2690    optimizer::DexToDexCompilationLevel dex_to_dex_compilation_level =
2691        GetDexToDexCompilationLevel(soa.Self(), *driver, jclass_loader, dex_file, class_def);
2692
2693    ClassDataItemIterator it(dex_file, class_data);
2694    // Skip fields
2695    while (it.HasNextStaticField()) {
2696      it.Next();
2697    }
2698    while (it.HasNextInstanceField()) {
2699      it.Next();
2700    }
2701
2702    bool compilation_enabled = driver->IsClassToCompile(
2703        dex_file.StringByTypeIdx(class_def.class_idx_));
2704
2705    // Compile direct methods
2706    int64_t previous_direct_method_idx = -1;
2707    while (it.HasNextDirectMethod()) {
2708      uint32_t method_idx = it.GetMemberIndex();
2709      if (method_idx == previous_direct_method_idx) {
2710        // smali can create dex files with two encoded_methods sharing the same method_idx
2711        // http://code.google.com/p/smali/issues/detail?id=119
2712        it.Next();
2713        continue;
2714      }
2715      previous_direct_method_idx = method_idx;
2716      CompileMethod(soa.Self(), driver, it.GetMethodCodeItem(), it.GetMethodAccessFlags(),
2717                    it.GetMethodInvokeType(class_def), class_def_index,
2718                    method_idx, jclass_loader, dex_file, dex_to_dex_compilation_level,
2719                    compilation_enabled, dex_cache);
2720      it.Next();
2721    }
2722    // Compile virtual methods
2723    int64_t previous_virtual_method_idx = -1;
2724    while (it.HasNextVirtualMethod()) {
2725      uint32_t method_idx = it.GetMemberIndex();
2726      if (method_idx == previous_virtual_method_idx) {
2727        // smali can create dex files with two encoded_methods sharing the same method_idx
2728        // http://code.google.com/p/smali/issues/detail?id=119
2729        it.Next();
2730        continue;
2731      }
2732      previous_virtual_method_idx = method_idx;
2733      CompileMethod(soa.Self(), driver, it.GetMethodCodeItem(), it.GetMethodAccessFlags(),
2734                    it.GetMethodInvokeType(class_def), class_def_index,
2735                    method_idx, jclass_loader, dex_file, dex_to_dex_compilation_level,
2736                    compilation_enabled, dex_cache);
2737      it.Next();
2738    }
2739    DCHECK(!it.HasNext());
2740  }
2741
2742 private:
2743  const ParallelCompilationManager* const manager_;
2744};
2745
2746void CompilerDriver::CompileDexFile(jobject class_loader,
2747                                    const DexFile& dex_file,
2748                                    const std::vector<const DexFile*>& dex_files,
2749                                    ThreadPool* thread_pool,
2750                                    size_t thread_count,
2751                                    TimingLogger* timings) {
2752  TimingLogger::ScopedTiming t("Compile Dex File", timings);
2753  ParallelCompilationManager context(Runtime::Current()->GetClassLinker(), class_loader, this,
2754                                     &dex_file, dex_files, thread_pool);
2755  CompileClassVisitor visitor(&context);
2756  context.ForAll(0, dex_file.NumClassDefs(), &visitor, thread_count);
2757}
2758
2759void CompilerDriver::AddCompiledMethod(const MethodReference& method_ref,
2760                                       CompiledMethod* const compiled_method,
2761                                       size_t non_relative_linker_patch_count) {
2762  DCHECK(GetCompiledMethod(method_ref) == nullptr)
2763      << PrettyMethod(method_ref.dex_method_index, *method_ref.dex_file);
2764  {
2765    MutexLock mu(Thread::Current(), compiled_methods_lock_);
2766    compiled_methods_.Put(method_ref, compiled_method);
2767    non_relative_linker_patch_count_ += non_relative_linker_patch_count;
2768  }
2769  DCHECK(GetCompiledMethod(method_ref) != nullptr)
2770      << PrettyMethod(method_ref.dex_method_index, *method_ref.dex_file);
2771}
2772
2773void CompilerDriver::RemoveCompiledMethod(const MethodReference& method_ref) {
2774  CompiledMethod* compiled_method = nullptr;
2775  {
2776    MutexLock mu(Thread::Current(), compiled_methods_lock_);
2777    auto it = compiled_methods_.find(method_ref);
2778    if (it != compiled_methods_.end()) {
2779      compiled_method = it->second;
2780      compiled_methods_.erase(it);
2781    }
2782  }
2783  if (compiled_method != nullptr) {
2784    CompiledMethod::ReleaseSwapAllocatedCompiledMethod(this, compiled_method);
2785  }
2786}
2787
2788CompiledClass* CompilerDriver::GetCompiledClass(ClassReference ref) const {
2789  MutexLock mu(Thread::Current(), compiled_classes_lock_);
2790  ClassTable::const_iterator it = compiled_classes_.find(ref);
2791  if (it == compiled_classes_.end()) {
2792    return nullptr;
2793  }
2794  CHECK(it->second != nullptr);
2795  return it->second;
2796}
2797
2798void CompilerDriver::RecordClassStatus(ClassReference ref, mirror::Class::Status status) {
2799  MutexLock mu(Thread::Current(), compiled_classes_lock_);
2800  auto it = compiled_classes_.find(ref);
2801  if (it == compiled_classes_.end() || it->second->GetStatus() != status) {
2802    // An entry doesn't exist or the status is lower than the new status.
2803    if (it != compiled_classes_.end()) {
2804      CHECK_GT(status, it->second->GetStatus());
2805      delete it->second;
2806    }
2807    switch (status) {
2808      case mirror::Class::kStatusNotReady:
2809      case mirror::Class::kStatusError:
2810      case mirror::Class::kStatusRetryVerificationAtRuntime:
2811      case mirror::Class::kStatusVerified:
2812      case mirror::Class::kStatusInitialized:
2813      case mirror::Class::kStatusResolved:
2814        break;  // Expected states.
2815      default:
2816        LOG(FATAL) << "Unexpected class status for class "
2817            << PrettyDescriptor(ref.first->GetClassDescriptor(ref.first->GetClassDef(ref.second)))
2818            << " of " << status;
2819    }
2820    CompiledClass* compiled_class = new CompiledClass(status);
2821    compiled_classes_.Overwrite(ref, compiled_class);
2822  }
2823}
2824
2825CompiledMethod* CompilerDriver::GetCompiledMethod(MethodReference ref) const {
2826  MutexLock mu(Thread::Current(), compiled_methods_lock_);
2827  MethodTable::const_iterator it = compiled_methods_.find(ref);
2828  if (it == compiled_methods_.end()) {
2829    return nullptr;
2830  }
2831  CHECK(it->second != nullptr);
2832  return it->second;
2833}
2834
2835bool CompilerDriver::IsMethodVerifiedWithoutFailures(uint32_t method_idx,
2836                                                     uint16_t class_def_idx,
2837                                                     const DexFile& dex_file) const {
2838  const VerifiedMethod* verified_method = GetVerifiedMethod(&dex_file, method_idx);
2839  if (verified_method != nullptr) {
2840    return !verified_method->HasVerificationFailures();
2841  }
2842
2843  // If we can't find verification metadata, check if this is a system class (we trust that system
2844  // classes have their methods verified). If it's not, be conservative and assume the method
2845  // has not been verified successfully.
2846
2847  // TODO: When compiling the boot image it should be safe to assume that everything is verified,
2848  // even if methods are not found in the verification cache.
2849  const char* descriptor = dex_file.GetClassDescriptor(dex_file.GetClassDef(class_def_idx));
2850  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2851  Thread* self = Thread::Current();
2852  ScopedObjectAccess soa(self);
2853  bool is_system_class = class_linker->FindSystemClass(self, descriptor) != nullptr;
2854  if (!is_system_class) {
2855    self->ClearException();
2856  }
2857  return is_system_class;
2858}
2859
2860size_t CompilerDriver::GetNonRelativeLinkerPatchCount() const {
2861  MutexLock mu(Thread::Current(), compiled_methods_lock_);
2862  return non_relative_linker_patch_count_;
2863}
2864
2865void CompilerDriver::SetRequiresConstructorBarrier(Thread* self,
2866                                                   const DexFile* dex_file,
2867                                                   uint16_t class_def_index,
2868                                                   bool requires) {
2869  WriterMutexLock mu(self, requires_constructor_barrier_lock_);
2870  requires_constructor_barrier_.emplace(ClassReference(dex_file, class_def_index), requires);
2871}
2872
2873bool CompilerDriver::RequiresConstructorBarrier(Thread* self,
2874                                                const DexFile* dex_file,
2875                                                uint16_t class_def_index) {
2876  ClassReference class_ref(dex_file, class_def_index);
2877  {
2878    ReaderMutexLock mu(self, requires_constructor_barrier_lock_);
2879    auto it = requires_constructor_barrier_.find(class_ref);
2880    if (it != requires_constructor_barrier_.end()) {
2881      return it->second;
2882    }
2883  }
2884  WriterMutexLock mu(self, requires_constructor_barrier_lock_);
2885  const bool requires = RequiresConstructorBarrier(*dex_file, class_def_index);
2886  requires_constructor_barrier_.emplace(class_ref, requires);
2887  return requires;
2888}
2889
2890std::string CompilerDriver::GetMemoryUsageString(bool extended) const {
2891  std::ostringstream oss;
2892  const gc::Heap* const heap = Runtime::Current()->GetHeap();
2893  const size_t java_alloc = heap->GetBytesAllocated();
2894  oss << "arena alloc=" << PrettySize(max_arena_alloc_) << " (" << max_arena_alloc_ << "B)";
2895  oss << " java alloc=" << PrettySize(java_alloc) << " (" << java_alloc << "B)";
2896#if defined(__BIONIC__) || defined(__GLIBC__)
2897  const struct mallinfo info = mallinfo();
2898  const size_t allocated_space = static_cast<size_t>(info.uordblks);
2899  const size_t free_space = static_cast<size_t>(info.fordblks);
2900  oss << " native alloc=" << PrettySize(allocated_space) << " (" << allocated_space << "B)"
2901      << " free=" << PrettySize(free_space) << " (" << free_space << "B)";
2902#endif
2903  compiled_method_storage_.DumpMemoryUsage(oss, extended);
2904  return oss.str();
2905}
2906
2907bool CompilerDriver::IsStringTypeIndex(uint16_t type_index, const DexFile* dex_file) {
2908  const char* type = dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_index));
2909  return strcmp(type, "Ljava/lang/String;") == 0;
2910}
2911
2912bool CompilerDriver::IsStringInit(uint32_t method_index, const DexFile* dex_file, int32_t* offset) {
2913  DexFileMethodInliner* inliner = GetMethodInlinerMap()->GetMethodInliner(dex_file);
2914  const PointerSize pointer_size = InstructionSetPointerSize(GetInstructionSet());
2915  *offset = inliner->GetOffsetForStringInit(method_index, pointer_size);
2916  return inliner->IsStringInitMethodIndex(method_index);
2917}
2918
2919bool CompilerDriver::MayInlineInternal(const DexFile* inlined_from,
2920                                       const DexFile* inlined_into) const {
2921  // We're not allowed to inline across dex files if we're the no-inline-from dex file.
2922  if (inlined_from != inlined_into &&
2923      compiler_options_->GetNoInlineFromDexFile() != nullptr &&
2924      ContainsElement(*compiler_options_->GetNoInlineFromDexFile(), inlined_from)) {
2925    return false;
2926  }
2927
2928  return true;
2929}
2930
2931void CompilerDriver::InitializeThreadPools() {
2932  size_t parallel_count = parallel_thread_count_ > 0 ? parallel_thread_count_ - 1 : 0;
2933  parallel_thread_pool_.reset(
2934      new ThreadPool("Compiler driver thread pool", parallel_count));
2935  single_thread_pool_.reset(new ThreadPool("Single-threaded Compiler driver thread pool", 0));
2936}
2937
2938void CompilerDriver::FreeThreadPools() {
2939  parallel_thread_pool_.reset();
2940  single_thread_pool_.reset();
2941}
2942
2943}  // namespace art
2944