compiler_driver.cc revision aa3eff991fc34d6434465bf6bf49ef2e2fb286b7
12faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes/*
22faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Copyright (C) 2011 The Android Open Source Project
32faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
42faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
52faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * you may not use this file except in compliance with the License.
62faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * You may obtain a copy of the License at
72faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
82faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
92faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
102faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Unless required by applicable law or agreed to in writing, software
112faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
122faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * See the License for the specific language governing permissions and
142faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * limitations under the License.
152faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes */
169ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
171212a022fa5f8ef9585d765b1809521812af882cIan Rogers#include "compiler_driver.h"
189ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
1967f99418f648c3a95256ed3dcd8e8b64eef0b372Anwar Ghuloum#define ATRACE_TAG ATRACE_TAG_DALVIK
2067f99418f648c3a95256ed3dcd8e8b64eef0b372Anwar Ghuloum#include <utils/Trace.h>
21d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
2267f99418f648c3a95256ed3dcd8e8b64eef0b372Anwar Ghuloum#include <vector>
23d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes#include <unistd.h>
2427ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
2525fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier#ifndef __APPLE__
2625fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier#include <malloc.h>  // For mallinfo
2725fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier#endif
2825fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier
291aa246dec5abe212f699de1413a0c4a191ca364aElliott Hughes#include "base/stl_util.h"
30a84395489098e4531619b1cffd1afc282b14510eSameer Abu Asal#include "base/timing_logger.h"
319ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom#include "class_linker.h"
3298d1cc8033251c93786e2fa8c59a2e555a9493beMingyao Yang#include "compiled_class.h"
33b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#include "compiler.h"
34be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko#include "compiler_driver-inl.h"
3589756f21c107d96e6d1bbc75811bd33078c8ceeeIan Rogers#include "dex_compilation_unit.h"
364f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "dex_file-inl.h"
37c7f832061fea59fd6abd125f26c8ca1faec695a5Vladimir Marko#include "dex/verification_results.h"
382730db03beee4d6687ddfb5000c33c0370fbc6ebVladimir Marko#include "dex/verified_method.h"
392bc47809febcf36369dd40877b8226318642b428Vladimir Marko#include "dex/quick/dex_file_method_inliner.h"
40ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell#include "driver/compiler_options.h"
419baa4aefc370f48774b6104680193d9a7e4fb631Brian Carlstrom#include "jni_internal.h"
4222d5e735f403c57525fe868304c7123f0ce66399Ian Rogers#include "object_lock.h"
43bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle#include "profiler.h"
441f87008b165d26541d832ff805250afdc89c253dBrian Carlstrom#include "runtime.h"
451d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/accounting/card_table-inl.h"
461d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/accounting/heap_bitmap.h"
471d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/space.h"
48ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_field-inl.h"
49ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_method-inl.h"
502dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class_loader.h"
512dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class-inl.h"
5239ebcb800aabedd0ffa6aa4aeac8aa4194c66e61Ian Rogers#include "mirror/dex_cache-inl.h"
532dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object-inl.h"
542dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h"
552dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/throwable.h"
5600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h"
5700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "ScopedLocalRef.h"
58eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier#include "handle_scope-inl.h"
5950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers#include "thread.h"
600e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier#include "thread_pool.h"
61848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers#include "trampolines/trampoline_compiler.h"
62d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz#include "transaction.h"
6362746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe#include "utils/swap_space.h"
64776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#include "verifier/method_verifier.h"
652bc47809febcf36369dd40877b8226318642b428Vladimir Marko#include "verifier/method_verifier-inl.h"
669ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
679ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstromnamespace art {
689ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
696dd35ccbfbb29201c84a54261d75977c598ef2e6Ian Rogersstatic constexpr bool kTimeCompileMethod = !kIsDebugBuild;
70b68c6e578a28a9717d78dfd522d9d9b8befaedf2Mathieu Chartier
71996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogersstatic double Percentage(size_t x, size_t y) {
72398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes  return 100.0 * (static_cast<double>(x)) / (static_cast<double>(x + y));
73996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers}
74996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers
75996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogersstatic void DumpStat(size_t x, size_t y, const char* str) {
76996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers  if (x == 0 && y == 0) {
77996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers    return;
78996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers  }
79e732ef1c0192acd71925bd0ff1ab09640d45531dIan Rogers  LOG(INFO) << Percentage(x, y) << "% of " << str << " for " << (x + y) << " cases";
80996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers}
81996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers
82f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Markoclass CompilerDriver::AOTCompilationStats {
83c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers public:
84ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers  AOTCompilationStats()
85ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers      : stats_lock_("AOT compilation statistics lock"),
86ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers        types_in_dex_cache_(0), types_not_in_dex_cache_(0),
87ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers        strings_in_dex_cache_(0), strings_not_in_dex_cache_(0),
88ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers        resolved_types_(0), unresolved_types_(0),
89ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers        resolved_instance_fields_(0), unresolved_instance_fields_(0),
9002c42237741b5573f9d790a5a0f17f408dceb543Sameer Abu Asal        resolved_local_static_fields_(0), resolved_static_fields_(0), unresolved_static_fields_(0),
91fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers        type_based_devirtualization_(0),
92fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers        safe_casts_(0), not_safe_casts_(0) {
932ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers    for (size_t i = 0; i <= kMaxInvokeType; i++) {
94c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers      resolved_methods_[i] = 0;
95c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers      unresolved_methods_[i] = 0;
962ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers      virtual_made_direct_[i] = 0;
972ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers      direct_calls_to_boot_[i] = 0;
982ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers      direct_methods_to_boot_[i] = 0;
99b25c3f6a86dc634ce44fb2849385b49465caa84dElliott Hughes    }
100c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  }
101c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers
102c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  void Dump() {
103c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    DumpStat(types_in_dex_cache_, types_not_in_dex_cache_, "types known to be in dex cache");
104c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    DumpStat(strings_in_dex_cache_, strings_not_in_dex_cache_, "strings known to be in dex cache");
105c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    DumpStat(resolved_types_, unresolved_types_, "types resolved");
106c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    DumpStat(resolved_instance_fields_, unresolved_instance_fields_, "instance fields resolved");
107c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    DumpStat(resolved_local_static_fields_ + resolved_static_fields_, unresolved_static_fields_,
108c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers             "static fields resolved");
109c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    DumpStat(resolved_local_static_fields_, resolved_static_fields_ + unresolved_static_fields_,
110c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers             "static fields local to a class");
111fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers    DumpStat(safe_casts_, not_safe_casts_, "check-casts removed based on type information");
112fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers    // Note, the code below subtracts the stat value so that when added to the stat value we have
113fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers    // 100% of samples. TODO: clean this up.
114fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers    DumpStat(type_based_devirtualization_,
115fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers             resolved_methods_[kVirtual] + unresolved_methods_[kVirtual] +
116fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers             resolved_methods_[kInterface] + unresolved_methods_[kInterface] -
117fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers             type_based_devirtualization_,
118fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers             "virtual/interface calls made direct based on type information");
119c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers
1202ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers    for (size_t i = 0; i <= kMaxInvokeType; i++) {
121c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers      std::ostringstream oss;
1222ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers      oss << static_cast<InvokeType>(i) << " methods were AOT resolved";
123c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers      DumpStat(resolved_methods_[i], unresolved_methods_[i], oss.str().c_str());
1242ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers      if (virtual_made_direct_[i] > 0) {
1252ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers        std::ostringstream oss2;
1262ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers        oss2 << static_cast<InvokeType>(i) << " methods made direct";
1272ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers        DumpStat(virtual_made_direct_[i],
1282ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers                 resolved_methods_[i] + unresolved_methods_[i] - virtual_made_direct_[i],
1292ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers                 oss2.str().c_str());
1302ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers      }
1312ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers      if (direct_calls_to_boot_[i] > 0) {
1322ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers        std::ostringstream oss2;
1332ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers        oss2 << static_cast<InvokeType>(i) << " method calls are direct into boot";
1342ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers        DumpStat(direct_calls_to_boot_[i],
1352ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers                 resolved_methods_[i] + unresolved_methods_[i] - direct_calls_to_boot_[i],
1362ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers                 oss2.str().c_str());
1372ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers      }
1382ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers      if (direct_methods_to_boot_[i] > 0) {
1392ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers        std::ostringstream oss2;
1402ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers        oss2 << static_cast<InvokeType>(i) << " method calls have methods in boot";
1412ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers        DumpStat(direct_methods_to_boot_[i],
1422ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers                 resolved_methods_[i] + unresolved_methods_[i] - direct_methods_to_boot_[i],
1432ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers                 oss2.str().c_str());
1442ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers      }
145c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    }
146c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  }
147996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers
14850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers// Allow lossy statistics in non-debug builds.
149996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers#ifndef NDEBUG
15050b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers#define STATS_LOCK() MutexLock mu(Thread::Current(), stats_lock_)
151996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers#else
152996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers#define STATS_LOCK()
153996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers#endif
154996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers
155c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  void TypeInDexCache() {
156c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    STATS_LOCK();
157c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    types_in_dex_cache_++;
158c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  }
159996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers
160c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  void TypeNotInDexCache() {
161c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    STATS_LOCK();
162c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    types_not_in_dex_cache_++;
163c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  }
164996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers
165c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  void StringInDexCache() {
166c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    STATS_LOCK();
167c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    strings_in_dex_cache_++;
168c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  }
169996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers
170c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  void StringNotInDexCache() {
171c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    STATS_LOCK();
172c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    strings_not_in_dex_cache_++;
173c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  }
174996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers
175c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  void TypeDoesntNeedAccessCheck() {
176c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    STATS_LOCK();
177c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    resolved_types_++;
178c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  }
179996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers
180c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  void TypeNeedsAccessCheck() {
181c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    STATS_LOCK();
182c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    unresolved_types_++;
183c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  }
184996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers
185c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  void ResolvedInstanceField() {
186c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    STATS_LOCK();
187c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    resolved_instance_fields_++;
188c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  }
189996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers
190b25c3f6a86dc634ce44fb2849385b49465caa84dElliott Hughes  void UnresolvedInstanceField() {
191c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    STATS_LOCK();
192c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    unresolved_instance_fields_++;
193c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  }
194996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers
195c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  void ResolvedLocalStaticField() {
196c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    STATS_LOCK();
197c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    resolved_local_static_fields_++;
198c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  }
199996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers
200c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  void ResolvedStaticField() {
201c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    STATS_LOCK();
202c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    resolved_static_fields_++;
203c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  }
204996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers
205c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  void UnresolvedStaticField() {
206c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    STATS_LOCK();
207c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    unresolved_static_fields_++;
208c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  }
209996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers
210e3cd2f0e3c3d976ae9c65c8a731003a5aaf71986Ian Rogers  // Indicate that type information from the verifier led to devirtualization.
21102c42237741b5573f9d790a5a0f17f408dceb543Sameer Abu Asal  void PreciseTypeDevirtualization() {
21202c42237741b5573f9d790a5a0f17f408dceb543Sameer Abu Asal    STATS_LOCK();
21302c42237741b5573f9d790a5a0f17f408dceb543Sameer Abu Asal    type_based_devirtualization_++;
21402c42237741b5573f9d790a5a0f17f408dceb543Sameer Abu Asal  }
215e3cd2f0e3c3d976ae9c65c8a731003a5aaf71986Ian Rogers
216e3cd2f0e3c3d976ae9c65c8a731003a5aaf71986Ian Rogers  // Indicate that a method of the given type was resolved at compile time.
217c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  void ResolvedMethod(InvokeType type) {
218c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    DCHECK_LE(type, kMaxInvokeType);
219c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    STATS_LOCK();
220c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    resolved_methods_[type]++;
221996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers  }
222996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers
223e3cd2f0e3c3d976ae9c65c8a731003a5aaf71986Ian Rogers  // Indicate that a method of the given type was unresolved at compile time as it was in an
224e3cd2f0e3c3d976ae9c65c8a731003a5aaf71986Ian Rogers  // unknown dex file.
225c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  void UnresolvedMethod(InvokeType type) {
226c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    DCHECK_LE(type, kMaxInvokeType);
227c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    STATS_LOCK();
228c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    unresolved_methods_[type]++;
229996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers  }
230c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers
231e3cd2f0e3c3d976ae9c65c8a731003a5aaf71986Ian Rogers  // Indicate that a type of virtual method dispatch has been converted into a direct method
232e3cd2f0e3c3d976ae9c65c8a731003a5aaf71986Ian Rogers  // dispatch.
2332ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers  void VirtualMadeDirect(InvokeType type) {
234e3cd2f0e3c3d976ae9c65c8a731003a5aaf71986Ian Rogers    DCHECK(type == kVirtual || type == kInterface || type == kSuper);
2352ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers    STATS_LOCK();
2362ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers    virtual_made_direct_[type]++;
2372ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers  }
2382ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers
239e3cd2f0e3c3d976ae9c65c8a731003a5aaf71986Ian Rogers  // Indicate that a method of the given type was able to call directly into boot.
2402ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers  void DirectCallsToBoot(InvokeType type) {
2412ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers    DCHECK_LE(type, kMaxInvokeType);
2422ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers    STATS_LOCK();
2432ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers    direct_calls_to_boot_[type]++;
2442ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers  }
2452ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers
246e3cd2f0e3c3d976ae9c65c8a731003a5aaf71986Ian Rogers  // Indicate that a method of the given type was able to be resolved directly from boot.
2472ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers  void DirectMethodsToBoot(InvokeType type) {
2482ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers    DCHECK_LE(type, kMaxInvokeType);
249fb6adba0d5d5505610fbd325e7911db700a2f1e8Ian Rogers    STATS_LOCK();
2502ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers    direct_methods_to_boot_[type]++;
251fb6adba0d5d5505610fbd325e7911db700a2f1e8Ian Rogers  }
2522ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers
253f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  void ProcessedInvoke(InvokeType type, int flags) {
254f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    STATS_LOCK();
255f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    if (flags == 0) {
256f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      unresolved_methods_[type]++;
257f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    } else {
258f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      DCHECK_NE((flags & kFlagMethodResolved), 0);
259f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      resolved_methods_[type]++;
260f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      if ((flags & kFlagVirtualMadeDirect) != 0) {
261f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko        virtual_made_direct_[type]++;
262f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko        if ((flags & kFlagPreciseTypeDevirtualization) != 0) {
263f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko          type_based_devirtualization_++;
264f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko        }
265f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      } else {
266f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko        DCHECK_EQ((flags & kFlagPreciseTypeDevirtualization), 0);
267f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      }
268f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      if ((flags & kFlagDirectCallToBoot) != 0) {
269f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko        direct_calls_to_boot_[type]++;
270f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      }
271f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      if ((flags & kFlagDirectMethodToBoot) != 0) {
272f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko        direct_methods_to_boot_[type]++;
273f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      }
274f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    }
275f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  }
276f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko
277fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers  // A check-cast could be eliminated due to verifier type analysis.
278fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers  void SafeCast() {
279fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers    STATS_LOCK();
280fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers    safe_casts_++;
281fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers  }
282fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers
283fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers  // A check-cast couldn't be eliminated due to verifier type analysis.
284fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers  void NotASafeCast() {
285fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers    STATS_LOCK();
286fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers    not_safe_casts_++;
287fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers  }
288fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers
289c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers private:
290c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  Mutex stats_lock_;
291c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers
292c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  size_t types_in_dex_cache_;
293c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  size_t types_not_in_dex_cache_;
294c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers
295c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  size_t strings_in_dex_cache_;
296c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  size_t strings_not_in_dex_cache_;
297c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers
298c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  size_t resolved_types_;
299c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  size_t unresolved_types_;
300c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers
301c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  size_t resolved_instance_fields_;
302c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  size_t unresolved_instance_fields_;
303c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers
304c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  size_t resolved_local_static_fields_;
305c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  size_t resolved_static_fields_;
306c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  size_t unresolved_static_fields_;
30702c42237741b5573f9d790a5a0f17f408dceb543Sameer Abu Asal  // Type based devirtualization for invoke interface and virtual.
30802c42237741b5573f9d790a5a0f17f408dceb543Sameer Abu Asal  size_t type_based_devirtualization_;
309c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers
310c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  size_t resolved_methods_[kMaxInvokeType + 1];
311c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers  size_t unresolved_methods_[kMaxInvokeType + 1];
3122ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers  size_t virtual_made_direct_[kMaxInvokeType + 1];
3132ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers  size_t direct_calls_to_boot_[kMaxInvokeType + 1];
3142ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers  size_t direct_methods_to_boot_[kMaxInvokeType + 1];
315c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers
316fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers  size_t safe_casts_;
317fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers  size_t not_safe_casts_;
318fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers
319b25c3f6a86dc634ce44fb2849385b49465caa84dElliott Hughes  DISALLOW_COPY_AND_ASSIGN(AOTCompilationStats);
320c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers};
321996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers
32251c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom
32351c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstromextern "C" art::CompiledMethod* ArtCompileDEX(art::CompilerDriver& compiler,
32451c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom                                              const art::DexFile::CodeItem* code_item,
32551c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom                                              uint32_t access_flags,
32651c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom                                              art::InvokeType invoke_type,
3278b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                              uint16_t class_def_idx,
32851c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom                                              uint32_t method_idx,
32951c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom                                              jobject class_loader,
33051c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom                                              const art::DexFile& dex_file);
33151c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom
3326449c62e40ef3a9bb75f664f922555affb532ee4Brian CarlstromCompilerDriver::CompilerDriver(const CompilerOptions* compiler_options,
3336449c62e40ef3a9bb75f664f922555affb532ee4Brian Carlstrom                               VerificationResults* verification_results,
3345816ed48bc339c983b40dc493e96b97821ce7966Vladimir Marko                               DexFileToMethodInlinerMap* method_inliner_map,
335b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                               Compiler::Kind compiler_kind,
336f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray                               InstructionSet instruction_set,
3377020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison                               InstructionSetFeatures instruction_set_features,
33826318f722958ac1cba6a812026a1377f37c54941Andreas Gampe                               bool image, std::set<std::string>* image_classes,
33926318f722958ac1cba6a812026a1377f37c54941Andreas Gampe                               std::set<std::string>* compiled_classes, size_t thread_count,
34039c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison                               bool dump_stats, bool dump_passes, CumulativeLogger* timer,
34162746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe                               int swap_fd, std::string profile_file)
34262746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe    : swap_space_(swap_fd == -1 ? nullptr : new SwapSpace(swap_fd, 10 * MB)),
34362746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe      swap_space_allocator_(new SwapAllocator<void>(swap_space_.get())),
34462746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe      profile_present_(false), compiler_options_(compiler_options),
3456449c62e40ef3a9bb75f664f922555affb532ee4Brian Carlstrom      verification_results_(verification_results),
3465816ed48bc339c983b40dc493e96b97821ce7966Vladimir Marko      method_inliner_map_(method_inliner_map),
34772d32629303f8f39362a4099481f48646aed042fIan Rogers      compiler_(Compiler::Create(this, compiler_kind)),
348c531cefbfb5394413122e9f57d211ba436cff012buzbee      instruction_set_(instruction_set),
3497020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison      instruction_set_features_(instruction_set_features),
350fffdb023275613612a22ec62b3421ffe4d2b73feIan Rogers      freezing_constructor_lock_("freezing constructor lock"),
351c225caa9715eeaeff87f27d5b6a3e7d4f6b7efadElliott Hughes      compiled_classes_lock_("compiled classes lock"),
352c225caa9715eeaeff87f27d5b6a3e7d4f6b7efadElliott Hughes      compiled_methods_lock_("compiled method lock"),
35362746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe      compiled_methods_(MethodTable::key_compare()),
354aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom      image_(image),
35596391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom      image_classes_(image_classes),
35626318f722958ac1cba6a812026a1377f37c54941Andreas Gampe      classes_to_compile_(compiled_classes),
3575523ee070b005576c6f889415205d49ea77cf243Elliott Hughes      thread_count_(thread_count),
35800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      start_ns_(0),
359c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers      stats_(new AOTCompilationStats),
360ba0668ecd2a6459ed7c77012995ad08d27f88725Brian Carlstrom      dump_stats_(dump_stats),
361ea3fa0b4ba13d7bd7f7c1cd85202ccbe141a35aeNicolas Geoffray      dump_passes_(dump_passes),
362ea3fa0b4ba13d7bd7f7c1cd85202ccbe141a35aeNicolas Geoffray      timings_logger_(timer),
36335439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      compiler_library_(nullptr),
36435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      compiler_context_(nullptr),
36535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      compiler_enable_auto_elf_loading_(nullptr),
36635439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      compiler_get_method_code_addr_(nullptr),
36755d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell      support_boot_image_fixup_(instruction_set != kMips),
368ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell      cfi_info_(nullptr),
36962746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe      // Use actual deduping only if we don't use swap.
37062746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe      dedupe_code_("dedupe code", *swap_space_allocator_),
37162746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe      dedupe_mapping_table_("dedupe mapping table", *swap_space_allocator_),
37262746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe      dedupe_vmap_table_("dedupe vmap table", *swap_space_allocator_),
37362746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe      dedupe_gc_map_("dedupe gc map", *swap_space_allocator_),
37462746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe      dedupe_cfi_info_("dedupe cfi info", *swap_space_allocator_) {
3756449c62e40ef3a9bb75f664f922555affb532ee4Brian Carlstrom  DCHECK(compiler_options_ != nullptr);
3766449c62e40ef3a9bb75f664f922555affb532ee4Brian Carlstrom  DCHECK(verification_results_ != nullptr);
3776449c62e40ef3a9bb75f664f922555affb532ee4Brian Carlstrom  DCHECK(method_inliner_map_ != nullptr);
3782ce745c06271d5223d57dbf08117b20d5b60694aBrian Carlstrom
37935439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  CHECK_PTHREAD_CALL(pthread_key_create, (&tls_key_, nullptr), "compiler tls key");
3804df2bbdfe6602ce5f141b7b44028b95faa0bd8efbuzbee
38175021222d9c03a80fa5c136db0d5fb8d82d04031Sebastien Hertz  dex_to_dex_compiler_ = reinterpret_cast<DexToDexCompilerFn>(ArtCompileDEX);
3822d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz
38372d32629303f8f39362a4099481f48646aed042fIan Rogers  compiler_->Init();
38400bc1dc4f81268d78d7dfeb298b85c56876425a9Brian Carlstrom
38525c3325bf95036bf325fc7cb21b4fd6d40282857Brian Carlstrom  CHECK(!Runtime::Current()->IsStarted());
386d5185344e19d9feb7ac268369e0af6a467d1cb48Kenny Root  if (image_) {
387d5185344e19d9feb7ac268369e0af6a467d1cb48Kenny Root    CHECK(image_classes_.get() != nullptr);
388d5185344e19d9feb7ac268369e0af6a467d1cb48Kenny Root  } else {
389d5185344e19d9feb7ac268369e0af6a467d1cb48Kenny Root    CHECK(image_classes_.get() == nullptr);
390ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  }
391ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell
392ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell  // Are we generating CFI information?
393ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell  if (compiler_options->GetGenerateGDBInformation()) {
394b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray    cfi_info_.reset(compiler_->GetCallFrameInformationInitialization(*this));
395ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell  }
396c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle
397c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle  // Read the profile file if one is provided.
398c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle  if (!profile_file.empty()) {
399c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle    profile_present_ = profile_file_.LoadFile(profile_file);
400c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle    if (profile_present_) {
401c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle      LOG(INFO) << "Using profile data form file " << profile_file;
402c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle    } else {
403c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle      LOG(INFO) << "Failed to load profile file " << profile_file;
404c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle    }
405c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle  }
406c486c11a1c71ca9c118d57152427b741229cda49Shih-wei Liao}
407c486c11a1c71ca9c118d57152427b741229cda49Shih-wei Liao
40862746d8d9c4400e4764f162b22bfb1a32be287a9Andreas GampeSwapVector<uint8_t>* CompilerDriver::DeduplicateCode(const ArrayRef<const uint8_t>& code) {
409193bad9b9cfd10642043fa2ebbfc68bd5f9ede4bMathieu Chartier  return dedupe_code_.Add(Thread::Current(), code);
410193bad9b9cfd10642043fa2ebbfc68bd5f9ede4bMathieu Chartier}
411193bad9b9cfd10642043fa2ebbfc68bd5f9ede4bMathieu Chartier
41262746d8d9c4400e4764f162b22bfb1a32be287a9Andreas GampeSwapVector<uint8_t>* CompilerDriver::DeduplicateMappingTable(const ArrayRef<const uint8_t>& code) {
413193bad9b9cfd10642043fa2ebbfc68bd5f9ede4bMathieu Chartier  return dedupe_mapping_table_.Add(Thread::Current(), code);
414193bad9b9cfd10642043fa2ebbfc68bd5f9ede4bMathieu Chartier}
415193bad9b9cfd10642043fa2ebbfc68bd5f9ede4bMathieu Chartier
41662746d8d9c4400e4764f162b22bfb1a32be287a9Andreas GampeSwapVector<uint8_t>* CompilerDriver::DeduplicateVMapTable(const ArrayRef<const uint8_t>& code) {
417193bad9b9cfd10642043fa2ebbfc68bd5f9ede4bMathieu Chartier  return dedupe_vmap_table_.Add(Thread::Current(), code);
418193bad9b9cfd10642043fa2ebbfc68bd5f9ede4bMathieu Chartier}
419193bad9b9cfd10642043fa2ebbfc68bd5f9ede4bMathieu Chartier
42062746d8d9c4400e4764f162b22bfb1a32be287a9Andreas GampeSwapVector<uint8_t>* CompilerDriver::DeduplicateGCMap(const ArrayRef<const uint8_t>& code) {
421193bad9b9cfd10642043fa2ebbfc68bd5f9ede4bMathieu Chartier  return dedupe_gc_map_.Add(Thread::Current(), code);
422193bad9b9cfd10642043fa2ebbfc68bd5f9ede4bMathieu Chartier}
423193bad9b9cfd10642043fa2ebbfc68bd5f9ede4bMathieu Chartier
42462746d8d9c4400e4764f162b22bfb1a32be287a9Andreas GampeSwapVector<uint8_t>* CompilerDriver::DeduplicateCFIInfo(const ArrayRef<const uint8_t>& cfi_info) {
42562746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe  return dedupe_cfi_info_.Add(Thread::Current(), cfi_info);
426ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell}
427ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell
4281212a022fa5f8ef9585d765b1809521812af882cIan RogersCompilerDriver::~CompilerDriver() {
42950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  Thread* self = Thread::Current();
430c225caa9715eeaeff87f27d5b6a3e7d4f6b7efadElliott Hughes  {
43150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(self, compiled_classes_lock_);
432c225caa9715eeaeff87f27d5b6a3e7d4f6b7efadElliott Hughes    STLDeleteValues(&compiled_classes_);
433f582258f0e296223a091fd64231a203ad71e9649Brian Carlstrom    STLDeleteElements(&code_to_patch_);
434f582258f0e296223a091fd64231a203ad71e9649Brian Carlstrom    STLDeleteElements(&methods_to_patch_);
435be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi    STLDeleteElements(&classes_to_patch_);
436807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    STLDeleteElements(&strings_to_patch_);
43762746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe
43862746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe    for (auto& pair : compiled_methods_) {
43962746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe      CompiledMethod::ReleaseSwapAllocatedCompiledMethod(this, pair.second);
44062746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe    }
441be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi  }
442ed6d5ed3494648780b9e91422c90d6bc22c16b79Mathieu Chartier  CHECK_PTHREAD_CALL(pthread_key_delete, (tls_key_), "delete tls key");
44372d32629303f8f39362a4099481f48646aed042fIan Rogers  compiler_->UnInit();
4443320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom}
4453320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom
4461212a022fa5f8ef9585d765b1809521812af882cIan RogersCompilerTls* CompilerDriver::GetTls() {
4474df2bbdfe6602ce5f141b7b44028b95faa0bd8efbuzbee  // Lazily create thread-local storage
4484df2bbdfe6602ce5f141b7b44028b95faa0bd8efbuzbee  CompilerTls* res = static_cast<CompilerTls*>(pthread_getspecific(tls_key_));
44935439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (res == nullptr) {
4504df2bbdfe6602ce5f141b7b44028b95faa0bd8efbuzbee    res = new CompilerTls();
4514df2bbdfe6602ce5f141b7b44028b95faa0bd8efbuzbee    CHECK_PTHREAD_CALL(pthread_setspecific, (tls_key_, res), "compiler tls");
4524df2bbdfe6602ce5f141b7b44028b95faa0bd8efbuzbee  }
4534df2bbdfe6602ce5f141b7b44028b95faa0bd8efbuzbee  return res;
4544df2bbdfe6602ce5f141b7b44028b95faa0bd8efbuzbee}
4554df2bbdfe6602ce5f141b7b44028b95faa0bd8efbuzbee
456dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers#define CREATE_TRAMPOLINE(type, abi, offset) \
457af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampe    if (Is64BitInstructionSet(instruction_set_)) { \
458dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers      return CreateTrampoline64(instruction_set_, abi, \
459dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers                                type ## _ENTRYPOINT_OFFSET(8, offset)); \
460dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    } else { \
461dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers      return CreateTrampoline32(instruction_set_, abi, \
462dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers                                type ## _ENTRYPOINT_OFFSET(4, offset)); \
463dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    }
464dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers
465848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogersconst std::vector<uint8_t>* CompilerDriver::CreateInterpreterToInterpreterBridge() const {
466dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  CREATE_TRAMPOLINE(INTERPRETER, kInterpreterAbi, pInterpreterToInterpreterBridge)
467848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers}
468848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers
469848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogersconst std::vector<uint8_t>* CompilerDriver::CreateInterpreterToCompiledCodeBridge() const {
470dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  CREATE_TRAMPOLINE(INTERPRETER, kInterpreterAbi, pInterpreterToCompiledCodeBridge)
471848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers}
472848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers
473848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogersconst std::vector<uint8_t>* CompilerDriver::CreateJniDlsymLookup() const {
474dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  CREATE_TRAMPOLINE(JNI, kJniAbi, pDlsymLookup)
475848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers}
476848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers
47788474b416eb257078e590bf9bc7957cee604a186Jeff Haoconst std::vector<uint8_t>* CompilerDriver::CreatePortableImtConflictTrampoline() const {
478dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  CREATE_TRAMPOLINE(PORTABLE, kPortableAbi, pPortableImtConflictTrampoline)
47988474b416eb257078e590bf9bc7957cee604a186Jeff Hao}
48088474b416eb257078e590bf9bc7957cee604a186Jeff Hao
4810aba0ba155bef7346bde19e53581200b89ae8a7aJeff Haoconst std::vector<uint8_t>* CompilerDriver::CreatePortableResolutionTrampoline() const {
482dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  CREATE_TRAMPOLINE(PORTABLE, kPortableAbi, pPortableResolutionTrampoline)
4830aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao}
4840aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao
485848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogersconst std::vector<uint8_t>* CompilerDriver::CreatePortableToInterpreterBridge() const {
486dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  CREATE_TRAMPOLINE(PORTABLE, kPortableAbi, pPortableToInterpreterBridge)
4870aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao}
4880aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao
4892da882315a61072664f7ce3c212307342e907207Andreas Gampeconst std::vector<uint8_t>* CompilerDriver::CreateQuickGenericJniTrampoline() const {
490dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickGenericJniTrampoline)
4912da882315a61072664f7ce3c212307342e907207Andreas Gampe}
4922da882315a61072664f7ce3c212307342e907207Andreas Gampe
49388474b416eb257078e590bf9bc7957cee604a186Jeff Haoconst std::vector<uint8_t>* CompilerDriver::CreateQuickImtConflictTrampoline() const {
494dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickImtConflictTrampoline)
49588474b416eb257078e590bf9bc7957cee604a186Jeff Hao}
49688474b416eb257078e590bf9bc7957cee604a186Jeff Hao
497848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogersconst std::vector<uint8_t>* CompilerDriver::CreateQuickResolutionTrampoline() const {
498dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickResolutionTrampoline)
4990aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao}
5000aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao
501848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogersconst std::vector<uint8_t>* CompilerDriver::CreateQuickToInterpreterBridge() const {
502dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickToInterpreterBridge)
5030aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao}
504dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers#undef CREATE_TRAMPOLINE
5050aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao
5061212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::CompileAll(jobject class_loader,
5074560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                                const std::vector<const DexFile*>& dex_files,
5083d504075f7c1204d581923460754bf6d3714b13fIan Rogers                                TimingLogger* timings) {
50925c3325bf95036bf325fc7cb21b4fd6d40282857Brian Carlstrom  DCHECK(!Runtime::Current()->IsStarted());
510700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<ThreadPool> thread_pool(new ThreadPool("Compiler driver thread pool", thread_count_ - 1));
511aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe  VLOG(compiler) << "Before precompile " << GetMemoryUsageString(false);
5123d504075f7c1204d581923460754bf6d3714b13fIan Rogers  PreCompile(class_loader, dex_files, thread_pool.get(), timings);
5133d504075f7c1204d581923460754bf6d3714b13fIan Rogers  Compile(class_loader, dex_files, thread_pool.get(), timings);
514ba0668ecd2a6459ed7c77012995ad08d27f88725Brian Carlstrom  if (dump_stats_) {
515ba0668ecd2a6459ed7c77012995ad08d27f88725Brian Carlstrom    stats_->Dump();
516ba0668ecd2a6459ed7c77012995ad08d27f88725Brian Carlstrom  }
5178a48741b96ca9cc5835cac72ac133c4ca480930fBrian Carlstrom}
5188a48741b96ca9cc5835cac72ac133c4ca480930fBrian Carlstrom
519590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartierstatic DexToDexCompilationLevel GetDexToDexCompilationlevel(
5200cd81352a7c06e381951cea1b104fd73516f4341Mathieu Chartier    Thread* self, Handle<mirror::ClassLoader> class_loader, const DexFile& dex_file,
521590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    const DexFile::ClassDef& class_def) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
5222d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  const char* descriptor = dex_file.GetClassDescriptor(class_def);
5232d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
5249837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  mirror::Class* klass = class_linker->FindClass(self, descriptor, class_loader);
52535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (klass == nullptr) {
5262d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz    CHECK(self->IsExceptionPending());
5272d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz    self->ClearException();
52875021222d9c03a80fa5c136db0d5fb8d82d04031Sebastien Hertz    return kDontDexToDexCompile;
52975021222d9c03a80fa5c136db0d5fb8d82d04031Sebastien Hertz  }
53035439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  // DexToDex at the kOptimize level may introduce quickened opcodes, which replace symbolic
53135439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  // references with actual offsets. We cannot re-verify such instructions.
53235439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  //
53335439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  // We store the verification information in the class status in the oat file, which the linker
53435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  // can validate (checksums) and use to skip load-time verification. It is thus safe to
53535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  // optimize when a class has been fully verified before.
53635439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (klass->IsVerified()) {
53775021222d9c03a80fa5c136db0d5fb8d82d04031Sebastien Hertz    // Class is verified so we can enable DEX-to-DEX compilation for performance.
53875021222d9c03a80fa5c136db0d5fb8d82d04031Sebastien Hertz    return kOptimize;
53975021222d9c03a80fa5c136db0d5fb8d82d04031Sebastien Hertz  } else if (klass->IsCompileTimeVerified()) {
54075021222d9c03a80fa5c136db0d5fb8d82d04031Sebastien Hertz    // Class verification has soft-failed. Anyway, ensure at least correctness.
54175021222d9c03a80fa5c136db0d5fb8d82d04031Sebastien Hertz    DCHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime);
54275021222d9c03a80fa5c136db0d5fb8d82d04031Sebastien Hertz    return kRequired;
54375021222d9c03a80fa5c136db0d5fb8d82d04031Sebastien Hertz  } else {
54475021222d9c03a80fa5c136db0d5fb8d82d04031Sebastien Hertz    // Class verification has failed: do not run DEX-to-DEX compilation.
54575021222d9c03a80fa5c136db0d5fb8d82d04031Sebastien Hertz    return kDontDexToDexCompile;
5462d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  }
5472d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz}
5482d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz
5493d504075f7c1204d581923460754bf6d3714b13fIan Rogersvoid CompilerDriver::CompileOne(mirror::ArtMethod* method, TimingLogger* timings) {
55025c3325bf95036bf325fc7cb21b4fd6d40282857Brian Carlstrom  DCHECK(!Runtime::Current()->IsStarted());
55100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Thread* self = Thread::Current();
5522d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  jobject jclass_loader;
55300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  const DexFile* dex_file;
5548b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers  uint16_t class_def_idx;
555ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  uint32_t method_idx = method->GetDexMethodIndex();
556ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  uint32_t access_flags = method->GetAccessFlags();
557ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  InvokeType invoke_type = method->GetInvokeType();
55800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
55900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccessUnchecked soa(self);
56000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedLocalRef<jobject>
56100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      local_class_loader(soa.Env(),
56200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                    soa.AddLocalReference<jobject>(method->GetDeclaringClass()->GetClassLoader()));
5632d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz    jclass_loader = soa.Env()->NewGlobalRef(local_class_loader.get());
56400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // Find the dex_file
565bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    dex_file = method->GetDexFile();
566bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    class_def_idx = method->GetClassDefIndex();
56700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
568ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
56900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  self->TransitionFromRunnableToSuspended(kNative);
570ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
571ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  std::vector<const DexFile*> dex_files;
57200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  dex_files.push_back(dex_file);
573ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
574700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<ThreadPool> thread_pool(new ThreadPool("Compiler driver thread pool", 0U));
5753d504075f7c1204d581923460754bf6d3714b13fIan Rogers  PreCompile(jclass_loader, dex_files, thread_pool.get(), timings);
576ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
5772d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  // Can we run DEX-to-DEX compiler on this class ?
57875021222d9c03a80fa5c136db0d5fb8d82d04031Sebastien Hertz  DexToDexCompilationLevel dex_to_dex_compilation_level = kDontDexToDexCompile;
5792d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  {
5802d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz    ScopedObjectAccess soa(Thread::Current());
5812d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz    const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
582eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    StackHandleScope<1> hs(soa.Self());
583eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    Handle<mirror::ClassLoader> class_loader(
584eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
5859837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    dex_to_dex_compilation_level = GetDexToDexCompilationlevel(self, class_loader, *dex_file,
5869837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers                                                               class_def);
5872d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  }
588ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  CompileMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx, jclass_loader,
58926318f722958ac1cba6a812026a1377f37c54941Andreas Gampe                *dex_file, dex_to_dex_compilation_level, true);
590ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
5912d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  self->GetJniEnv()->DeleteGlobalRef(jclass_loader);
59200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
59300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  self->TransitionFromSuspendedToRunnable();
5949ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom}
5959ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
5961212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::Resolve(jobject class_loader, const std::vector<const DexFile*>& dex_files,
5973d504075f7c1204d581923460754bf6d3714b13fIan Rogers                             ThreadPool* thread_pool, TimingLogger* timings) {
598ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  for (size_t i = 0; i != dex_files.size(); ++i) {
599ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    const DexFile* dex_file = dex_files[i];
600d5185344e19d9feb7ac268369e0af6a467d1cb48Kenny Root    CHECK(dex_file != nullptr);
6010171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe    ResolveDexFile(class_loader, *dex_file, dex_files, thread_pool, timings);
6029ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  }
6039ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom}
6049ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
6051212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::PreCompile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
6063d504075f7c1204d581923460754bf6d3714b13fIan Rogers                                ThreadPool* thread_pool, TimingLogger* timings) {
60796391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  LoadImageClasses(timings);
608aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe  VLOG(compiler) << "LoadImageClasses: " << GetMemoryUsageString(false);
60996391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom
61035439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  Resolve(class_loader, dex_files, thread_pool, timings);
611aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe  VLOG(compiler) << "Resolve: " << GetMemoryUsageString(false);
61235439baf287b291b67ee406308e17fc6194facbfAndreas Gampe
6134a200f56b7075309316b04d550c9cc50f8314eddJeff Hao  if (!compiler_options_->IsVerificationEnabled()) {
61425fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier    VLOG(compiler) << "Verify none mode specified, skipping verification.";
61535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    SetVerified(class_loader, dex_files, thread_pool, timings);
6164a200f56b7075309316b04d550c9cc50f8314eddJeff Hao    return;
6174a200f56b7075309316b04d550c9cc50f8314eddJeff Hao  }
6184a200f56b7075309316b04d550c9cc50f8314eddJeff Hao
6192f66382fdb5e98537f724eba43ef1c7162c71b0eBrian Carlstrom  Verify(class_loader, dex_files, thread_pool, timings);
620aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe  VLOG(compiler) << "Verify: " << GetMemoryUsageString(false);
621ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
6222f66382fdb5e98537f724eba43ef1c7162c71b0eBrian Carlstrom  InitializeClasses(class_loader, dex_files, thread_pool, timings);
623aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe  VLOG(compiler) << "InitializeClasses: " << GetMemoryUsageString(false);
62496391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom
62596391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  UpdateImageClasses(timings);
626aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe  VLOG(compiler) << "UpdateImageClasses: " << GetMemoryUsageString(false);
627ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom}
628ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
629dfb325e0ddd746cd8f7c2e3723b3a573eb7cc111Ian Rogersbool CompilerDriver::IsImageClass(const char* descriptor) const {
630e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers  if (!IsImage()) {
63196391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    return true;
632e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers  } else {
633dfb325e0ddd746cd8f7c2e3723b3a573eb7cc111Ian Rogers    return image_classes_->find(descriptor) != image_classes_->end();
634ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  }
635ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom}
636ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
63726318f722958ac1cba6a812026a1377f37c54941Andreas Gampebool CompilerDriver::IsClassToCompile(const char* descriptor) const {
63826318f722958ac1cba6a812026a1377f37c54941Andreas Gampe  if (!IsImage()) {
63926318f722958ac1cba6a812026a1377f37c54941Andreas Gampe    return true;
64026318f722958ac1cba6a812026a1377f37c54941Andreas Gampe  } else {
64126318f722958ac1cba6a812026a1377f37c54941Andreas Gampe    if (classes_to_compile_ == nullptr) {
64226318f722958ac1cba6a812026a1377f37c54941Andreas Gampe      return true;
64326318f722958ac1cba6a812026a1377f37c54941Andreas Gampe    }
64426318f722958ac1cba6a812026a1377f37c54941Andreas Gampe    return classes_to_compile_->find(descriptor) != classes_to_compile_->end();
64526318f722958ac1cba6a812026a1377f37c54941Andreas Gampe  }
64626318f722958ac1cba6a812026a1377f37c54941Andreas Gampe}
64726318f722958ac1cba6a812026a1377f37c54941Andreas Gampe
64896391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstromstatic void ResolveExceptionsForMethod(MethodHelper* mh,
649700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::set<std::pair<uint16_t, const DexFile*>>& exceptions_to_resolve)
65096391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
651bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const DexFile::CodeItem* code_item = mh->GetMethod()->GetCodeItem();
65235439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (code_item == nullptr) {
65396391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    return;  // native or abstract method
65496391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  }
65596391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  if (code_item->tries_size_ == 0) {
65696391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    return;  // nothing to process
65796391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  }
65896391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
65996391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
66096391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
66196391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
66296391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    bool has_catch_all = false;
66396391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    if (encoded_catch_handler_size <= 0) {
66496391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom      encoded_catch_handler_size = -encoded_catch_handler_size;
66596391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom      has_catch_all = true;
66696391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    }
66796391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
66896391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom      uint16_t encoded_catch_handler_handlers_type_idx =
66996391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom          DecodeUnsignedLeb128(&encoded_catch_handler_list);
67096391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom      // Add to set of types to resolve if not already in the dex cache resolved types
671bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      if (!mh->GetMethod()->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
67296391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom        exceptions_to_resolve.insert(
67396391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom            std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx,
674bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                                                mh->GetMethod()->GetDexFile()));
67596391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom      }
67696391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom      // ignore address associated with catch handler
67796391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom      DecodeUnsignedLeb128(&encoded_catch_handler_list);
67896391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    }
67996391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    if (has_catch_all) {
68096391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom      // ignore catch all address
68196391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom      DecodeUnsignedLeb128(&encoded_catch_handler_list);
68296391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    }
68396391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  }
68496391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom}
68596391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom
68696391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstromstatic bool ResolveCatchBlockExceptionsClassVisitor(mirror::Class* c, void* arg)
68796391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
688700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::set<std::pair<uint16_t, const DexFile*>>* exceptions_to_resolve =
689700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers      reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*>>*>(arg);
690bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  StackHandleScope<1> hs(Thread::Current());
691bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  MethodHelper mh(hs.NewHandle<mirror::ArtMethod>(nullptr));
69296391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
693bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    mh.ChangeMethod(c->GetVirtualMethod(i));
69496391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
69596391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  }
69696391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
697bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    mh.ChangeMethod(c->GetDirectMethod(i));
69896391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
69996391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  }
70096391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  return true;
70196391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom}
70296391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom
70396391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstromstatic bool RecordImageClassesVisitor(mirror::Class* klass, void* arg)
70496391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
705cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg);
706cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  std::string temp;
707cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  image_classes->insert(klass->GetDescriptor(&temp));
70896391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  return true;
70996391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom}
71096391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom
71196391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom// Make a list of descriptors for classes to include in the image
7123d504075f7c1204d581923460754bf6d3714b13fIan Rogersvoid CompilerDriver::LoadImageClasses(TimingLogger* timings)
71396391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom      LOCKS_EXCLUDED(Locks::mutator_lock_) {
714d5185344e19d9feb7ac268369e0af6a467d1cb48Kenny Root  CHECK(timings != nullptr);
715e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers  if (!IsImage()) {
71696391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    return;
71796391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  }
71896391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom
719f5997b4d3f889569d5a2b724d83d764bfbb8d106Mathieu Chartier  TimingLogger::ScopedTiming t("LoadImageClasses", timings);
72096391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  // Make a first class to load all classes explicitly listed in the file
72196391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  Thread* self = Thread::Current();
72296391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  ScopedObjectAccess soa(self);
72396391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
724d5185344e19d9feb7ac268369e0af6a467d1cb48Kenny Root  CHECK(image_classes_.get() != nullptr);
72502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier  for (auto it = image_classes_->begin(), end = image_classes_->end(); it != end;) {
726e9c36b34efb7460f59c6766e526c9b0de8da70b3Vladimir Marko    const std::string& descriptor(*it);
727eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    StackHandleScope<1> hs(self);
728eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    Handle<mirror::Class> klass(
729eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        hs.NewHandle(class_linker->FindSystemClass(self, descriptor.c_str())));
73035439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    if (klass.Get() == nullptr) {
731e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers      VLOG(compiler) << "Failed to find class " << descriptor;
732e9c36b34efb7460f59c6766e526c9b0de8da70b3Vladimir Marko      image_classes_->erase(it++);
733a436fde2762664a3ecdda5eefcadd20b2e104f59Ian Rogers      self->ClearException();
73496391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    } else {
73596391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom      ++it;
73696391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    }
73796391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  }
73896391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom
73996391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  // Resolve exception classes referenced by the loaded classes. The catch logic assumes
74096391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  // exceptions are resolved by the verifier when there is a catch block in an interested method.
74196391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  // Do this here so that exception classes appear to have been specified image classes.
742700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::set<std::pair<uint16_t, const DexFile*>> unresolved_exception_types;
743eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  StackHandleScope<1> hs(self);
744eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::Class> java_lang_Throwable(
745eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      hs.NewHandle(class_linker->FindSystemClass(self, "Ljava/lang/Throwable;")));
74696391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  do {
74796391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    unresolved_exception_types.clear();
74896391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor,
74996391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom                               &unresolved_exception_types);
75002e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    for (const std::pair<uint16_t, const DexFile*>& exception_type : unresolved_exception_types) {
75102e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      uint16_t exception_type_idx = exception_type.first;
75202e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      const DexFile* dex_file = exception_type.second;
7530cd81352a7c06e381951cea1b104fd73516f4341Mathieu Chartier      StackHandleScope<2> hs(self);
754eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(*dex_file)));
755eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      Handle<mirror::Class> klass(hs.NewHandle(
7560cd81352a7c06e381951cea1b104fd73516f4341Mathieu Chartier          class_linker->ResolveType(*dex_file, exception_type_idx, dex_cache,
7570cd81352a7c06e381951cea1b104fd73516f4341Mathieu Chartier                                    NullHandle<mirror::ClassLoader>())));
75835439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      if (klass.Get() == nullptr) {
75996391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom        const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
76096391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom        const char* descriptor = dex_file->GetTypeDescriptor(type_id);
76196391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom        LOG(FATAL) << "Failed to resolve class " << descriptor;
76296391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom      }
763eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      DCHECK(java_lang_Throwable->IsAssignableFrom(klass.Get()));
76496391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    }
76596391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    // Resolving exceptions may load classes that reference more exceptions, iterate until no
76696391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    // more are found
76796391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  } while (!unresolved_exception_types.empty());
76896391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom
76996391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  // We walk the roots looking for classes so that we'll pick up the
77096391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  // above classes plus any classes them depend on such super
77196391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  // classes, interfaces, and the required ClassLinker roots.
77296391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  class_linker->VisitClasses(RecordImageClassesVisitor, image_classes_.get());
77396391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom
77496391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  CHECK_NE(image_classes_->size(), 0U);
77596391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom}
77696391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom
777cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogersstatic void MaybeAddToImageClasses(Handle<mirror::Class> c, std::set<std::string>* image_classes)
77896391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
779f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  Thread* self = Thread::Current();
780f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  StackHandleScope<1> hs(self);
781f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  // Make a copy of the handle so that we don't clobber it doing Assign.
782f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  Handle<mirror::Class> klass(hs.NewHandle(c.Get()));
783cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  std::string temp;
78496391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  while (!klass->IsObjectClass()) {
785cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    const char* descriptor = klass->GetDescriptor(&temp);
786cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    std::pair<std::set<std::string>::iterator, bool> result = image_classes->insert(descriptor);
787cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    if (!result.second) {  // Previously inserted.
788cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      break;
78996391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    }
790cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    VLOG(compiler) << "Adding " << descriptor << " to image classes";
791f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier    for (size_t i = 0; i < klass->NumDirectInterfaces(); ++i) {
792f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier      StackHandleScope<1> hs(self);
793f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier      MaybeAddToImageClasses(hs.NewHandle(mirror::Class::GetDirectInterface(self, klass, i)),
794f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier                             image_classes);
79596391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    }
79696391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    if (klass->IsArrayClass()) {
797f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier      StackHandleScope<1> hs(self);
798f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier      MaybeAddToImageClasses(hs.NewHandle(klass->GetComponentType()), image_classes);
79996391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    }
800f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier    klass.Assign(klass->GetSuperClass());
80196391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  }
80296391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom}
80396391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom
80496391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstromvoid CompilerDriver::FindClinitImageClassesCallback(mirror::Object* object, void* arg) {
80535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  DCHECK(object != nullptr);
80635439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  DCHECK(arg != nullptr);
80796391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  CompilerDriver* compiler_driver = reinterpret_cast<CompilerDriver*>(arg);
808f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  StackHandleScope<1> hs(Thread::Current());
809f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  MaybeAddToImageClasses(hs.NewHandle(object->GetClass()), compiler_driver->image_classes_.get());
81096391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom}
81196391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom
8123d504075f7c1204d581923460754bf6d3714b13fIan Rogersvoid CompilerDriver::UpdateImageClasses(TimingLogger* timings) {
813e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers  if (IsImage()) {
814f5997b4d3f889569d5a2b724d83d764bfbb8d106Mathieu Chartier    TimingLogger::ScopedTiming t("UpdateImageClasses", timings);
815e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers    // Update image_classes_ with classes for objects created by <clinit> methods.
816e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers    Thread* self = Thread::Current();
817e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers    const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
818e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers    gc::Heap* heap = Runtime::Current()->GetHeap();
819e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers    // TODO: Image spaces only?
820590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    ScopedObjectAccess soa(Thread::Current());
821e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers    WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
822590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    heap->VisitObjects(FindClinitImageClassesCallback, this);
823e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers    self->EndAssertNoThreadSuspension(old_cause);
824e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers  }
82596391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom}
82696391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom
827590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartierbool CompilerDriver::CanAssumeTypeIsPresentInDexCache(const DexFile& dex_file, uint32_t type_idx) {
828fc0e94bed3f88ed7e50854fd8dfaf5dcb345250fIan Rogers  if (IsImage() &&
829dfb325e0ddd746cd8f7c2e3723b3a573eb7cc111Ian Rogers      IsImageClass(dex_file.StringDataByIdx(dex_file.GetTypeId(type_idx).descriptor_idx_))) {
8304ef12f5b0e26c6016c87866f6a33da5ed8e98d74Andreas Gampe    {
8316fe568e16cea80dfe9ccb856c138a75da5f2a90dIan Rogers      ScopedObjectAccess soa(Thread::Current());
8326fe568e16cea80dfe9ccb856c138a75da5f2a90dIan Rogers      mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
8336fe568e16cea80dfe9ccb856c138a75da5f2a90dIan Rogers      mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
8344ef12f5b0e26c6016c87866f6a33da5ed8e98d74Andreas Gampe      if (resolved_class == nullptr) {
8354ef12f5b0e26c6016c87866f6a33da5ed8e98d74Andreas Gampe        // Erroneous class.
8364ef12f5b0e26c6016c87866f6a33da5ed8e98d74Andreas Gampe        stats_->TypeNotInDexCache();
8374ef12f5b0e26c6016c87866f6a33da5ed8e98d74Andreas Gampe        return false;
8384ef12f5b0e26c6016c87866f6a33da5ed8e98d74Andreas Gampe      }
8396fe568e16cea80dfe9ccb856c138a75da5f2a90dIan Rogers    }
840c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    stats_->TypeInDexCache();
8416fe568e16cea80dfe9ccb856c138a75da5f2a90dIan Rogers    return true;
842996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers  } else {
843c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    stats_->TypeNotInDexCache();
8446fe568e16cea80dfe9ccb856c138a75da5f2a90dIan Rogers    return false;
845996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers  }
8466d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers}
8476d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers
8481212a022fa5f8ef9585d765b1809521812af882cIan Rogersbool CompilerDriver::CanAssumeStringIsPresentInDexCache(const DexFile& dex_file,
8491212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                                        uint32_t string_idx) {
8501bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers  // See also Compiler::ResolveDexFile
8511bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers
8525f7fa551162b9e755fdaf054ffc89411a0e135e3Ian Rogers  bool result = false;
8535f7fa551162b9e755fdaf054ffc89411a0e135e3Ian Rogers  if (IsImage()) {
8545f7fa551162b9e755fdaf054ffc89411a0e135e3Ian Rogers    // We resolve all const-string strings when building for the image.
85500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
856eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    StackHandleScope<1> hs(soa.Self());
857eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    Handle<mirror::DexCache> dex_cache(
858eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(dex_file)));
8595f7fa551162b9e755fdaf054ffc89411a0e135e3Ian Rogers    Runtime::Current()->GetClassLinker()->ResolveString(dex_file, string_idx, dex_cache);
8605f7fa551162b9e755fdaf054ffc89411a0e135e3Ian Rogers    result = true;
86100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
862996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers  if (result) {
863c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    stats_->StringInDexCache();
864996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers  } else {
865c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    stats_->StringNotInDexCache();
866996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers  }
867996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers  return result;
8681bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers}
8691bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers
8701212a022fa5f8ef9585d765b1809521812af882cIan Rogersbool CompilerDriver::CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file,
871c9e463c8aa083a5ed20293f42363ebff93de5f84Ian Rogers                                                uint32_t type_idx,
872c9e463c8aa083a5ed20293f42363ebff93de5f84Ian Rogers                                                bool* type_known_final, bool* type_known_abstract,
873c9e463c8aa083a5ed20293f42363ebff93de5f84Ian Rogers                                                bool* equals_referrers_class) {
87435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (type_known_final != nullptr) {
875c9e463c8aa083a5ed20293f42363ebff93de5f84Ian Rogers    *type_known_final = false;
876c9e463c8aa083a5ed20293f42363ebff93de5f84Ian Rogers  }
87735439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (type_known_abstract != nullptr) {
878c9e463c8aa083a5ed20293f42363ebff93de5f84Ian Rogers    *type_known_abstract = false;
879c9e463c8aa083a5ed20293f42363ebff93de5f84Ian Rogers  }
88035439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (equals_referrers_class != nullptr) {
881c9e463c8aa083a5ed20293f42363ebff93de5f84Ian Rogers    *equals_referrers_class = false;
882c9e463c8aa083a5ed20293f42363ebff93de5f84Ian Rogers  }
88300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
8842dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
8851bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers  // Get type from dex cache assuming it was populated by the verifier
8862dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
88735439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (resolved_class == nullptr) {
888c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    stats_->TypeNeedsAccessCheck();
8891bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers    return false;  // Unknown class needs access checks.
8901bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers  }
8911bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers  const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
89235439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (equals_referrers_class != nullptr) {
893c9e463c8aa083a5ed20293f42363ebff93de5f84Ian Rogers    *equals_referrers_class = (method_id.class_idx_ == type_idx);
894c9e463c8aa083a5ed20293f42363ebff93de5f84Ian Rogers  }
8952dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
89635439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (referrer_class == nullptr) {
897c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    stats_->TypeNeedsAccessCheck();
8981bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers    return false;  // Incomplete referrer knowledge needs access check.
8991bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers  }
9001bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers  // Perform access check, will return true if access is ok or false if we're going to have to
9011bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers  // check this at runtime (for example for class loaders).
902996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers  bool result = referrer_class->CanAccess(resolved_class);
903996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers  if (result) {
904c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    stats_->TypeDoesntNeedAccessCheck();
90535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    if (type_known_final != nullptr) {
906c9e463c8aa083a5ed20293f42363ebff93de5f84Ian Rogers      *type_known_final = resolved_class->IsFinal() && !resolved_class->IsArrayClass();
907c9e463c8aa083a5ed20293f42363ebff93de5f84Ian Rogers    }
90835439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    if (type_known_abstract != nullptr) {
909b19fa800aab1c2731b12ff24696feca13901db3fIan Rogers      *type_known_abstract = resolved_class->IsAbstract() && !resolved_class->IsArrayClass();
910c9e463c8aa083a5ed20293f42363ebff93de5f84Ian Rogers    }
911996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers  } else {
912c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    stats_->TypeNeedsAccessCheck();
913996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers  }
914996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers  return result;
9151bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers}
9161bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers
9171212a022fa5f8ef9585d765b1809521812af882cIan Rogersbool CompilerDriver::CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx,
9181212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                                            const DexFile& dex_file,
9191212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                                            uint32_t type_idx) {
92000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
9212dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
9221bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers  // Get type from dex cache assuming it was populated by the verifier.
9232dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
92435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (resolved_class == nullptr) {
925c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    stats_->TypeNeedsAccessCheck();
9261bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers    return false;  // Unknown class needs access checks.
9271bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers  }
9281bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers  const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
9292dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
93035439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (referrer_class == nullptr) {
931c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    stats_->TypeNeedsAccessCheck();
9321bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers    return false;  // Incomplete referrer knowledge needs access check.
9331bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers  }
9341bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers  // Perform access and instantiable checks, will return true if access is ok or false if we're
9351bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers  // going to have to check this at runtime (for example for class loaders).
936996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers  bool result = referrer_class->CanAccess(resolved_class) && resolved_class->IsInstantiable();
937996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers  if (result) {
938c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    stats_->TypeDoesntNeedAccessCheck();
939996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers  } else {
940c8b306f5221658c7e4b5516be8917dc8c9288e7eIan Rogers    stats_->TypeNeedsAccessCheck();
941996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers  }
942996cc586ce76235ee3efa1eff86de51dabcc5d53Ian Rogers  return result;
9431bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers}
9441bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers
945be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchibool CompilerDriver::CanEmbedTypeInCode(const DexFile& dex_file, uint32_t type_idx,
946be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                        bool* is_type_initialized, bool* use_direct_type_ptr,
9478668c3cbdcf9471bd97e0da68a240051f2973074Mathieu Chartier                                        uintptr_t* direct_type_ptr, bool* out_is_finalizable) {
948807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier  if (GetCompilerOptions().GetCompilePic()) {
949807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    // Do not allow a direct class pointer to be used when compiling for position-independent
950807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    return false;
951807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier  }
952be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi  ScopedObjectAccess soa(Thread::Current());
953be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi  mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
954be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi  mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
955be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi  if (resolved_class == nullptr) {
956be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi    return false;
957be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi  }
9588668c3cbdcf9471bd97e0da68a240051f2973074Mathieu Chartier  *out_is_finalizable = resolved_class->IsFinalizable();
959be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi  const bool compiling_boot = Runtime::Current()->GetHeap()->IsCompilingBoot();
960345c4b19758703793ed31024cfb79940e2c63b75Alex Light  const bool support_boot_image_fixup = GetSupportBootImageFixup();
961be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi  if (compiling_boot) {
962be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi    // boot -> boot class pointers.
963be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi    // True if the class is in the image at boot compiling time.
964be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi    const bool is_image_class = IsImage() && IsImageClass(
965be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi        dex_file.StringDataByIdx(dex_file.GetTypeId(type_idx).descriptor_idx_));
966be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi    // True if pc relative load works.
967be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi    if (is_image_class && support_boot_image_fixup) {
968be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi      *is_type_initialized = resolved_class->IsInitialized();
969be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi      *use_direct_type_ptr = false;
970be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi      *direct_type_ptr = 0;
971be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi      return true;
972be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi    } else {
973be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi      return false;
974be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi    }
975be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi  } else {
976be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi    // True if the class is in the image at app compiling time.
977be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi    const bool class_in_image =
978be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi        Runtime::Current()->GetHeap()->FindSpaceFromObject(resolved_class, false)->IsImageSpace();
979345c4b19758703793ed31024cfb79940e2c63b75Alex Light    if (class_in_image && support_boot_image_fixup) {
980be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi      // boot -> app class pointers.
981be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi      *is_type_initialized = resolved_class->IsInitialized();
982a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      // TODO This is somewhat hacky. We should refactor all of this invoke codepath.
983a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      *use_direct_type_ptr = !GetCompilerOptions().GetIncludePatchInformation();
984be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi      *direct_type_ptr = reinterpret_cast<uintptr_t>(resolved_class);
985be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi      return true;
986be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi    } else {
987be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi      // app -> app class pointers.
988be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi      // Give up because app does not have an image and class
989be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi      // isn't created at compile time.  TODO: implement this
990be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi      // if/when each app gets an image.
991be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi      return false;
992be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi    }
993be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi  }
994be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi}
995be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi
996807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartierbool CompilerDriver::CanEmbedStringInCode(const DexFile& dex_file, uint32_t string_idx,
997807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier                                          bool* use_direct_type_ptr, uintptr_t* direct_type_ptr) {
998807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier  if (GetCompilerOptions().GetCompilePic()) {
999807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    // Do not allow a direct class pointer to be used when compiling for position-independent
1000807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    return false;
1001807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier  }
1002807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier  ScopedObjectAccess soa(Thread::Current());
1003807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier  mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
1004807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier  mirror::String* resolved_string = dex_cache->GetResolvedString(string_idx);
1005807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier  if (resolved_string == nullptr) {
1006807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    return false;
1007807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier  }
1008807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier  const bool compiling_boot = Runtime::Current()->GetHeap()->IsCompilingBoot();
1009807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier  const bool support_boot_image_fixup = GetSupportBootImageFixup();
1010807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier  if (compiling_boot) {
1011807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    // boot -> boot class pointers.
1012807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    // True if the class is in the image at boot compiling time.
1013807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    const bool is_image_string = IsImage();
1014807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    // True if pc relative load works.
1015807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    if (is_image_string && support_boot_image_fixup) {
1016807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier      *use_direct_type_ptr = false;
1017807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier      *direct_type_ptr = 0;
1018807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier      return true;
1019807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    }
1020807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    return false;
1021807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier  } else {
1022807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    // True if the class is in the image at app compiling time.
1023807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    const bool obj_in_image =
1024807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier        false && Runtime::Current()->GetHeap()->FindSpaceFromObject(resolved_string, false)->IsImageSpace();
1025807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    if (obj_in_image && support_boot_image_fixup) {
1026807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier      // boot -> app class pointers.
1027807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier      // TODO This is somewhat hacky. We should refactor all of this invoke codepath.
1028807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier      *use_direct_type_ptr = !GetCompilerOptions().GetIncludePatchInformation();
1029807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier      *direct_type_ptr = reinterpret_cast<uintptr_t>(resolved_string);
1030807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier      return true;
1031807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    }
1032807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier
1033807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    // app -> app class pointers.
1034807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    // Give up because app does not have an image and class
1035807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    // isn't created at compile time.  TODO: implement this
1036807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    // if/when each app gets an image.
1037807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier    return false;
1038807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier  }
1039807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier}
1040807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier
1041be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Markovoid CompilerDriver::ProcessedInstanceField(bool resolved) {
1042be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  if (!resolved) {
1043be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko    stats_->UnresolvedInstanceField();
1044be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  } else {
1045be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko    stats_->ResolvedInstanceField();
1046be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  }
1047be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko}
1048be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko
1049be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Markovoid CompilerDriver::ProcessedStaticField(bool resolved, bool local) {
1050be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  if (!resolved) {
1051be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko    stats_->UnresolvedStaticField();
1052be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  } else if (local) {
1053be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko    stats_->ResolvedLocalStaticField();
1054be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  } else {
1055be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko    stats_->ResolvedStaticField();
1056be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  }
1057be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko}
1058be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko
1059f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Markovoid CompilerDriver::ProcessedInvoke(InvokeType invoke_type, int flags) {
1060f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  stats_->ProcessedInvoke(invoke_type, flags);
1061a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers}
1062a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers
1063e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffraymirror::ArtField* CompilerDriver::ComputeInstanceFieldInfo(uint32_t field_idx,
1064e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray                                                           const DexCompilationUnit* mUnit,
1065e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray                                                           bool is_put,
1066e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray                                                           const ScopedObjectAccess& soa) {
1067be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  // Try to resolve the field and compiling method's class.
1068be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  mirror::ArtField* resolved_field;
1069be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  mirror::Class* referrer_class;
1070be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  mirror::DexCache* dex_cache;
1071be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  {
1072eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    StackHandleScope<3> hs(soa.Self());
1073eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    Handle<mirror::DexCache> dex_cache_handle(
1074eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        hs.NewHandle(mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile())));
1075eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    Handle<mirror::ClassLoader> class_loader_handle(
1076eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        hs.NewHandle(soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader())));
1077eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    Handle<mirror::ArtField> resolved_field_handle(hs.NewHandle(
1078eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        ResolveField(soa, dex_cache_handle, class_loader_handle, mUnit, field_idx, false)));
1079eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    referrer_class = (resolved_field_handle.Get() != nullptr)
1080eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        ? ResolveCompilingMethodsClass(soa, dex_cache_handle, class_loader_handle, mUnit) : nullptr;
1081eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    resolved_field = resolved_field_handle.Get();
1082eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    dex_cache = dex_cache_handle.Get();
10831bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers  }
1084e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray  bool can_link = false;
1085be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  if (resolved_field != nullptr && referrer_class != nullptr) {
1086be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko    std::pair<bool, bool> fast_path = IsFastInstanceField(
1087e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray        dex_cache, referrer_class, resolved_field, field_idx);
1088e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray    can_link = is_put ? fast_path.second : fast_path.first;
1089be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  }
1090e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray  ProcessedInstanceField(can_link);
1091e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray  return can_link ? resolved_field : nullptr;
1092e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray}
1093e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray
1094e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffraybool CompilerDriver::ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit,
1095e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray                                              bool is_put, MemberOffset* field_offset,
1096e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray                                              bool* is_volatile) {
1097e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray  ScopedObjectAccess soa(Thread::Current());
1098e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray  StackHandleScope<1> hs(soa.Self());
1099e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray  Handle<mirror::ArtField> resolved_field =
1100e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray      hs.NewHandle(ComputeInstanceFieldInfo(field_idx, mUnit, is_put, soa));
1101e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray
1102e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray  if (resolved_field.Get() == nullptr) {
1103be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko    // Conservative defaults.
1104be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko    *is_volatile = true;
1105be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko    *field_offset = MemberOffset(static_cast<size_t>(-1));
1106e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray    return false;
1107e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray  } else {
1108e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray    *is_volatile = resolved_field->IsVolatile();
1109e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray    *field_offset = resolved_field->GetOffset();
1110e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray    return true;
1111be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  }
11121bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers}
11131bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers
111489756f21c107d96e6d1bbc75811bd33078c8ceeeIan Rogersbool CompilerDriver::ComputeStaticFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit,
1115be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko                                            bool is_put, MemberOffset* field_offset,
1116be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko                                            uint32_t* storage_index, bool* is_referrers_class,
1117be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko                                            bool* is_volatile, bool* is_initialized) {
111800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
1119be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  // Try to resolve the field and compiling method's class.
1120be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  mirror::ArtField* resolved_field;
1121be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  mirror::Class* referrer_class;
1122be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  mirror::DexCache* dex_cache;
1123be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  {
1124eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    StackHandleScope<3> hs(soa.Self());
1125eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    Handle<mirror::DexCache> dex_cache_handle(
1126eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        hs.NewHandle(mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile())));
1127eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    Handle<mirror::ClassLoader> class_loader_handle(
1128eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        hs.NewHandle(soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader())));
1129eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    Handle<mirror::ArtField> resolved_field_handle(hs.NewHandle(
1130eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        ResolveField(soa, dex_cache_handle, class_loader_handle, mUnit, field_idx, true)));
1131eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    referrer_class = (resolved_field_handle.Get() != nullptr)
1132eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        ? ResolveCompilingMethodsClass(soa, dex_cache_handle, class_loader_handle, mUnit) : nullptr;
1133eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    resolved_field = resolved_field_handle.Get();
1134eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    dex_cache = dex_cache_handle.Get();
11351bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers  }
1136be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  bool result = false;
1137be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  if (resolved_field != nullptr && referrer_class != nullptr) {
1138be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko    *is_volatile = IsFieldVolatile(resolved_field);
1139be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko    std::pair<bool, bool> fast_path = IsFastStaticField(
1140be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko        dex_cache, referrer_class, resolved_field, field_idx, field_offset,
1141be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko        storage_index, is_referrers_class, is_initialized);
1142be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko    result = is_put ? fast_path.second : fast_path.first;
1143be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  }
1144be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  if (!result) {
1145be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko    // Conservative defaults.
1146be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko    *is_volatile = true;
1147be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko    *field_offset = MemberOffset(static_cast<size_t>(-1));
1148be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko    *storage_index = -1;
1149be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko    *is_referrers_class = false;
1150be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko    *is_initialized = false;
1151be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  }
1152be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  ProcessedStaticField(result, *is_referrers_class);
1153be0e546730e532ef0987cd4bde2c6f5a1b14dd2aVladimir Marko  return result;
11541bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers}
11551bddec3a6521f16df37499754000a3b1787a52e9Ian Rogers
115683883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogersvoid CompilerDriver::GetCodeAndMethodForDirectCall(InvokeType* type, InvokeType sharp_type,
115783883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers                                                   bool no_guarantee_of_dex_cache_entry,
1158643b5df2b065ccf5bb19a183573da691e9d0311fIgor Murashkin                                                   const mirror::Class* referrer_class,
1159ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                                                   mirror::ArtMethod* method,
1160f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko                                                   int* stats_flags,
116183883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers                                                   MethodReference* target_method,
116265ec92cf13c9d11c83711443a02e4249163d47f1Ian Rogers                                                   uintptr_t* direct_code,
116365ec92cf13c9d11c83711443a02e4249163d47f1Ian Rogers                                                   uintptr_t* direct_method) {
1164137e88f798857321f4007631fdf052d2830ec2c4Ian Rogers  // For direct and static methods compute possible direct_code and direct_method values, ie
1165137e88f798857321f4007631fdf052d2830ec2c4Ian Rogers  // an address for the Method* being invoked and an address of the code for that Method*.
1166137e88f798857321f4007631fdf052d2830ec2c4Ian Rogers  // For interface calls compute a value for direct_method that is the interface method being
1167137e88f798857321f4007631fdf052d2830ec2c4Ian Rogers  // invoked, so this can be passed to the out-of-line runtime support code.
116865ec92cf13c9d11c83711443a02e4249163d47f1Ian Rogers  *direct_code = 0;
116965ec92cf13c9d11c83711443a02e4249163d47f1Ian Rogers  *direct_method = 0;
1170643b5df2b065ccf5bb19a183573da691e9d0311fIgor Murashkin  bool use_dex_cache = GetCompilerOptions().GetCompilePic();  // Off by default
1171590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  const bool compiling_boot = Runtime::Current()->GetHeap()->IsCompilingBoot();
1172a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  // TODO This is somewhat hacky. We should refactor all of this invoke codepath.
1173a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  const bool force_relocations = (compiling_boot ||
1174a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light                                  GetCompilerOptions().GetIncludePatchInformation());
1175b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  if (compiler_->IsPortable()) {
1176c531cefbfb5394413122e9f57d211ba436cff012buzbee    if (sharp_type != kStatic && sharp_type != kDirect) {
1177c531cefbfb5394413122e9f57d211ba436cff012buzbee      return;
1178c531cefbfb5394413122e9f57d211ba436cff012buzbee    }
117983883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers    use_dex_cache = true;
1180c531cefbfb5394413122e9f57d211ba436cff012buzbee  } else {
118188474b416eb257078e590bf9bc7957cee604a186Jeff Hao    if (sharp_type != kStatic && sharp_type != kDirect) {
1182c531cefbfb5394413122e9f57d211ba436cff012buzbee      return;
1183c531cefbfb5394413122e9f57d211ba436cff012buzbee    }
118483883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers    // TODO: support patching on all architectures.
1185643b5df2b065ccf5bb19a183573da691e9d0311fIgor Murashkin    use_dex_cache = use_dex_cache || (force_relocations && !support_boot_image_fixup_);
1186b8404a7de94c109e3c17b4205b5f8aaae996eb33TDYa  }
118783883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers  bool method_code_in_boot = (method->GetDeclaringClass()->GetClassLoader() == nullptr);
118883883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers  if (!use_dex_cache) {
118983883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers    if (!method_code_in_boot) {
119083883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      use_dex_cache = true;
119183883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers    } else {
119283883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      bool has_clinit_trampoline =
119383883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers          method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
119483883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      if (has_clinit_trampoline && (method->GetDeclaringClass() != referrer_class)) {
119583883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers        // Ensure we run the clinit trampoline unless we are invoking a static method in the same
119683883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers        // class.
119783883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers        use_dex_cache = true;
119883883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      }
119983883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers    }
12002ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers  }
1201f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  if (method_code_in_boot) {
1202f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    *stats_flags |= kFlagDirectCallToBoot | kFlagDirectMethodToBoot;
12032ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers  }
1204a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  if (!use_dex_cache && force_relocations) {
1205a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    if (!IsImage() || !IsImageClass(method->GetDeclaringClassDescriptor())) {
120683883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      // We can only branch directly to Methods that are resolved in the DexCache.
120783883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      // Otherwise we won't invoke the resolution trampoline.
120883883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      use_dex_cache = true;
1209e3cd2f0e3c3d976ae9c65c8a731003a5aaf71986Ian Rogers    }
1210c468e92d1e18305a5053c8a4a7c88cb297e525afIan Rogers  }
121183883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers  // The method is defined not within this dex file. We need a dex cache slot within the current
121283883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers  // dex file or direct pointers.
121383883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers  bool must_use_direct_pointers = false;
121483883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers  if (target_method->dex_file == method->GetDeclaringClass()->GetDexCache()->GetDexFile()) {
121583883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers    target_method->dex_method_index = method->GetDexMethodIndex();
121683883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers  } else {
121783883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers    if (no_guarantee_of_dex_cache_entry) {
1218bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      StackHandleScope<1> hs(Thread::Current());
1219bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      MethodHelper mh(hs.NewHandle(method));
122083883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      // See if the method is also declared in this dex cache.
1221bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      uint32_t dex_method_idx = mh.FindDexMethodIndexInOtherDexFile(
1222bbcc0c0a17262f3d2a70fc0a82e1783862f708ccVladimir Marko          *target_method->dex_file, target_method->dex_method_index);
122383883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      if (dex_method_idx != DexFile::kDexNoIndex) {
122483883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers        target_method->dex_method_index = dex_method_idx;
122583883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      } else {
1226a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light        if (force_relocations && !use_dex_cache) {
122749161cef10a308aedada18e9aa742498d6e6c8c7Jeff Hao          target_method->dex_method_index = method->GetDexMethodIndex();
122849161cef10a308aedada18e9aa742498d6e6c8c7Jeff Hao          target_method->dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
122949161cef10a308aedada18e9aa742498d6e6c8c7Jeff Hao        }
123083883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers        must_use_direct_pointers = true;
12313fa13791c51985d9956d01bc465de6d36c3390d3Ian Rogers      }
12323fa13791c51985d9956d01bc465de6d36c3390d3Ian Rogers    }
123383883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers  }
123483883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers  if (use_dex_cache) {
123583883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers    if (must_use_direct_pointers) {
123683883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      // Fail. Test above showed the only safe dispatch was via the dex cache, however, the direct
123783883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      // pointers are required as the dex cache lacks an appropriate entry.
123883883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      VLOG(compiler) << "Dex cache devirtualization failed for: " << PrettyMethod(method);
123983883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers    } else {
124083883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      *type = sharp_type;
124183883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers    }
12423fa13791c51985d9956d01bc465de6d36c3390d3Ian Rogers  } else {
1243a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    bool method_in_image =
1244a51a0b0300268b605e3ad71b0e87ff394032c5e7Vladimir Marko        Runtime::Current()->GetHeap()->FindSpaceFromObject(method, false)->IsImageSpace();
1245a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    if (method_in_image || compiling_boot) {
1246a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      // We know we must be able to get to the method in the image, so use that pointer.
1247a51a0b0300268b605e3ad71b0e87ff394032c5e7Vladimir Marko      CHECK(!method->IsAbstract());
124883883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      *type = sharp_type;
1249a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      *direct_method = force_relocations ? -1 : reinterpret_cast<uintptr_t>(method);
1250a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      *direct_code = force_relocations ? -1 : compiler_->GetEntryPointOf(method);
1251a51a0b0300268b605e3ad71b0e87ff394032c5e7Vladimir Marko      target_method->dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1252a51a0b0300268b605e3ad71b0e87ff394032c5e7Vladimir Marko      target_method->dex_method_index = method->GetDexMethodIndex();
1253a51a0b0300268b605e3ad71b0e87ff394032c5e7Vladimir Marko    } else if (!must_use_direct_pointers) {
1254a51a0b0300268b605e3ad71b0e87ff394032c5e7Vladimir Marko      // Set the code and rely on the dex cache for the method.
1255a51a0b0300268b605e3ad71b0e87ff394032c5e7Vladimir Marko      *type = sharp_type;
1256a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      if (force_relocations) {
1257a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light        *direct_code = -1;
1258a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light        target_method->dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1259a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light        target_method->dex_method_index = method->GetDexMethodIndex();
1260a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      } else {
1261a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light        *direct_code = compiler_->GetEntryPointOf(method);
1262a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      }
126383883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers    } else {
1264a51a0b0300268b605e3ad71b0e87ff394032c5e7Vladimir Marko      // Direct pointers were required but none were available.
1265a51a0b0300268b605e3ad71b0e87ff394032c5e7Vladimir Marko      VLOG(compiler) << "Dex cache devirtualization failed for: " << PrettyMethod(method);
12663fa13791c51985d9956d01bc465de6d36c3390d3Ian Rogers    }
12672ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers  }
12682ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers}
12692ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers
1270e3cd2f0e3c3d976ae9c65c8a731003a5aaf71986Ian Rogersbool CompilerDriver::ComputeInvokeInfo(const DexCompilationUnit* mUnit, const uint32_t dex_pc,
127165ec92cf13c9d11c83711443a02e4249163d47f1Ian Rogers                                       bool update_stats, bool enable_devirtualization,
127265ec92cf13c9d11c83711443a02e4249163d47f1Ian Rogers                                       InvokeType* invoke_type, MethodReference* target_method,
127365ec92cf13c9d11c83711443a02e4249163d47f1Ian Rogers                                       int* vtable_idx, uintptr_t* direct_code,
127465ec92cf13c9d11c83711443a02e4249163d47f1Ian Rogers                                       uintptr_t* direct_method) {
1275f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  InvokeType orig_invoke_type = *invoke_type;
1276f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  int stats_flags = 0;
127700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
1278f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  // Try to resolve the method and compiling method's class.
1279f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  mirror::ArtMethod* resolved_method;
1280f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  mirror::Class* referrer_class;
1281eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  StackHandleScope<3> hs(soa.Self());
1282eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::DexCache> dex_cache(
1283eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      hs.NewHandle(mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile())));
1284eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1285eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader())));
1286f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  {
1287f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    uint32_t method_idx = target_method->dex_method_index;
1288eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    Handle<mirror::ArtMethod> resolved_method_handle(hs.NewHandle(
1289eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        ResolveMethod(soa, dex_cache, class_loader, mUnit, method_idx, orig_invoke_type)));
1290eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    referrer_class = (resolved_method_handle.Get() != nullptr)
1291f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko        ? ResolveCompilingMethodsClass(soa, dex_cache, class_loader, mUnit) : nullptr;
1292eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    resolved_method = resolved_method_handle.Get();
1293f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  }
1294f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  bool result = false;
1295f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  if (resolved_method != nullptr) {
1296f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    *vtable_idx = GetResolvedMethodVTableIndex(resolved_method, orig_invoke_type);
1297f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko
1298f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    if (enable_devirtualization) {
1299f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      DCHECK(mUnit->GetVerifiedMethod() != nullptr);
1300f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      const MethodReference* devirt_target = mUnit->GetVerifiedMethod()->GetDevirtTarget(dex_pc);
1301f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko
1302f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      stats_flags = IsFastInvoke(
1303f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko          soa, dex_cache, class_loader, mUnit, referrer_class, resolved_method,
1304f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko          invoke_type, target_method, devirt_target, direct_code, direct_method);
1305f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      result = stats_flags != 0;
1306f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    } else {
1307f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      // Devirtualization not enabled. Inline IsFastInvoke(), dropping the devirtualization parts.
1308f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      if (UNLIKELY(referrer_class == nullptr) ||
1309f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko          UNLIKELY(!referrer_class->CanAccessResolvedMethod(resolved_method->GetDeclaringClass(),
1310eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier                                                            resolved_method, dex_cache.Get(),
1311f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko                                                            target_method->dex_method_index)) ||
1312f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko          *invoke_type == kSuper) {
1313f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko        // Slow path. (Without devirtualization, all super calls go slow path as well.)
1314f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      } else {
1315f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko        // Sharpening failed so generate a regular resolved method dispatch.
1316f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko        stats_flags = kFlagMethodResolved;
1317f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko        GetCodeAndMethodForDirectCall(invoke_type, *invoke_type, false, referrer_class, resolved_method,
1318f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko                                      &stats_flags, target_method, direct_code, direct_method);
1319f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko        result = true;
1320a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers      }
1321a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers    }
1322a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers  }
1323f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  if (!result) {
1324f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    // Conservative defaults.
1325f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    *vtable_idx = -1;
1326f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    *direct_code = 0u;
1327f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    *direct_method = 0u;
1328a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers  }
1329e3cd2f0e3c3d976ae9c65c8a731003a5aaf71986Ian Rogers  if (update_stats) {
1330f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    ProcessedInvoke(orig_invoke_type, stats_flags);
1331e3cd2f0e3c3d976ae9c65c8a731003a5aaf71986Ian Rogers  }
1332f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  return result;
1333a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers}
1334a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers
13352730db03beee4d6687ddfb5000c33c0370fbc6ebVladimir Markoconst VerifiedMethod* CompilerDriver::GetVerifiedMethod(const DexFile* dex_file,
13362730db03beee4d6687ddfb5000c33c0370fbc6ebVladimir Marko                                                        uint32_t method_idx) const {
13372730db03beee4d6687ddfb5000c33c0370fbc6ebVladimir Marko  MethodReference ref(dex_file, method_idx);
13382730db03beee4d6687ddfb5000c33c0370fbc6ebVladimir Marko  return verification_results_->GetVerifiedMethod(ref);
13392730db03beee4d6687ddfb5000c33c0370fbc6ebVladimir Marko}
13402730db03beee4d6687ddfb5000c33c0370fbc6ebVladimir Marko
13412730db03beee4d6687ddfb5000c33c0370fbc6ebVladimir Markobool CompilerDriver::IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc) {
134235439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (!compiler_options_->IsVerificationEnabled()) {
134335439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    // If we didn't verify, every cast has to be treated as non-safe.
134435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    return false;
134535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  }
13462730db03beee4d6687ddfb5000c33c0370fbc6ebVladimir Marko  DCHECK(mUnit->GetVerifiedMethod() != nullptr);
13472730db03beee4d6687ddfb5000c33c0370fbc6ebVladimir Marko  bool result = mUnit->GetVerifiedMethod()->IsSafeCast(dex_pc);
1348fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers  if (result) {
1349fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers    stats_->SafeCast();
1350fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers  } else {
1351fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers    stats_->NotASafeCast();
1352fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers  }
1353fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers  return result;
1354fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers}
1355fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers
13561212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::AddCodePatch(const DexFile* dex_file,
13578b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                  uint16_t referrer_class_def_idx,
13588b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                  uint32_t referrer_method_idx,
13598b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                  InvokeType referrer_invoke_type,
13608b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                  uint32_t target_method_idx,
136149161cef10a308aedada18e9aa742498d6e6c8c7Jeff Hao                                  const DexFile* target_dex_file,
13628b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                  InvokeType target_invoke_type,
13638b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                  size_t literal_offset) {
136450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(Thread::Current(), compiled_methods_lock_);
1365be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi  code_to_patch_.push_back(new CallPatchInformation(dex_file,
1366be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                    referrer_class_def_idx,
1367be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                    referrer_method_idx,
1368be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                    referrer_invoke_type,
1369be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                    target_method_idx,
137049161cef10a308aedada18e9aa742498d6e6c8c7Jeff Hao                                                    target_dex_file,
1371be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                    target_invoke_type,
1372be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                    literal_offset));
13733fa13791c51985d9956d01bc465de6d36c3390d3Ian Rogers}
137455d0eac918321e0525f6e6491f36a80977e0d416Mark Mendellvoid CompilerDriver::AddRelativeCodePatch(const DexFile* dex_file,
137555d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                          uint16_t referrer_class_def_idx,
137655d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                          uint32_t referrer_method_idx,
137755d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                          InvokeType referrer_invoke_type,
137855d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                          uint32_t target_method_idx,
137949161cef10a308aedada18e9aa742498d6e6c8c7Jeff Hao                                          const DexFile* target_dex_file,
138055d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                          InvokeType target_invoke_type,
138155d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                          size_t literal_offset,
138255d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                          int32_t pc_relative_offset) {
138355d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell  MutexLock mu(Thread::Current(), compiled_methods_lock_);
138455d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell  code_to_patch_.push_back(new RelativeCallPatchInformation(dex_file,
138555d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                                            referrer_class_def_idx,
138655d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                                            referrer_method_idx,
138755d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                                            referrer_invoke_type,
138855d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                                            target_method_idx,
138949161cef10a308aedada18e9aa742498d6e6c8c7Jeff Hao                                                            target_dex_file,
139055d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                                            target_invoke_type,
139155d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                                            literal_offset,
139255d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                                            pc_relative_offset));
139355d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell}
13941212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::AddMethodPatch(const DexFile* dex_file,
13958b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                    uint16_t referrer_class_def_idx,
13968b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                    uint32_t referrer_method_idx,
13978b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                    InvokeType referrer_invoke_type,
13988b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                    uint32_t target_method_idx,
139949161cef10a308aedada18e9aa742498d6e6c8c7Jeff Hao                                    const DexFile* target_dex_file,
14008b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                    InvokeType target_invoke_type,
14018b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                    size_t literal_offset) {
140250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(Thread::Current(), compiled_methods_lock_);
1403be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi  methods_to_patch_.push_back(new CallPatchInformation(dex_file,
1404be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                       referrer_class_def_idx,
1405be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                       referrer_method_idx,
1406be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                       referrer_invoke_type,
1407be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                       target_method_idx,
140849161cef10a308aedada18e9aa742498d6e6c8c7Jeff Hao                                                       target_dex_file,
1409be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                       target_invoke_type,
1410be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                       literal_offset));
1411be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi}
1412be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchivoid CompilerDriver::AddClassPatch(const DexFile* dex_file,
1413be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                    uint16_t referrer_class_def_idx,
1414be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                    uint32_t referrer_method_idx,
1415be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                    uint32_t target_type_idx,
1416be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                    size_t literal_offset) {
1417be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi  MutexLock mu(Thread::Current(), compiled_methods_lock_);
1418be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi  classes_to_patch_.push_back(new TypePatchInformation(dex_file,
1419be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                       referrer_class_def_idx,
1420be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                       referrer_method_idx,
1421be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                       target_type_idx,
1422be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                       literal_offset));
14233fa13791c51985d9956d01bc465de6d36c3390d3Ian Rogers}
1424807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartiervoid CompilerDriver::AddStringPatch(const DexFile* dex_file,
1425807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier                                    uint16_t referrer_class_def_idx,
1426807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier                                    uint32_t referrer_method_idx,
1427807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier                                    uint32_t string_idx,
1428807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier                                    size_t literal_offset) {
1429807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier  MutexLock mu(Thread::Current(), compiled_methods_lock_);
1430807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier  strings_to_patch_.push_back(new StringPatchInformation(dex_file,
1431807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier                                                         referrer_class_def_idx,
1432807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier                                                         referrer_method_idx,
1433807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier                                                         string_idx,
1434807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier                                                         literal_offset));
1435807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier}
14363fa13791c51985d9956d01bc465de6d36c3390d3Ian Rogers
1437219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogersclass ParallelCompilationManager {
1438731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom public:
1439219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers  typedef void Callback(const ParallelCompilationManager* manager, size_t index);
14400e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier
1441219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers  ParallelCompilationManager(ClassLinker* class_linker,
1442219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers                             jobject class_loader,
1443219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers                             CompilerDriver* compiler,
1444219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers                             const DexFile* dex_file,
14450171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe                             const std::vector<const DexFile*>& dex_files,
14463d504075f7c1204d581923460754bf6d3714b13fIan Rogers                             ThreadPool* thread_pool)
14470b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier    : index_(0),
14480b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier      class_linker_(class_linker),
1449731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom      class_loader_(class_loader),
1450731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom      compiler_(compiler),
14510e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier      dex_file_(dex_file),
14520171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe      dex_files_(dex_files),
14533d504075f7c1204d581923460754bf6d3714b13fIan Rogers      thread_pool_(thread_pool) {}
1454731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom
145500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ClassLinker* GetClassLinker() const {
145635439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    CHECK(class_linker_ != nullptr);
1457731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom    return class_linker_;
1458731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom  }
145900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
146000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  jobject GetClassLoader() const {
1461731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom    return class_loader_;
1462731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom  }
146300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
14641212a022fa5f8ef9585d765b1809521812af882cIan Rogers  CompilerDriver* GetCompiler() const {
146535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    CHECK(compiler_ != nullptr);
1466731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom    return compiler_;
1467731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom  }
146800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
146900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  const DexFile* GetDexFile() const {
147035439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    CHECK(dex_file_ != nullptr);
1471731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom    return dex_file_;
1472731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom  }
1473731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom
14740171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe  const std::vector<const DexFile*>& GetDexFiles() const {
14750171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe    return dex_files_;
14760171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe  }
14770171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe
14780e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier  void ForAll(size_t begin, size_t end, Callback callback, size_t work_units) {
14790e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    Thread* self = Thread::Current();
14800e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    self->AssertNoPendingException();
14810e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    CHECK_GT(work_units, 0U);
14829ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
14833e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers    index_.StoreRelaxed(begin);
14840e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    for (size_t i = 0; i < work_units; ++i) {
1485501baec5f2de2156bb5ed7f66d23f1b1ad026267Sebastien Hertz      thread_pool_->AddTask(self, new ForAllClosure(this, end, callback));
14860e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    }
14870e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    thread_pool_->StartWorkers(self);
148800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
14890e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    // Ensure we're suspended while we're blocked waiting for the other threads to finish (worker
14900e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    // thread destructor's called below perform join).
14910e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    CHECK_NE(self->GetState(), kRunnable);
1492d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
14930e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    // Wait for all the worker threads to finish.
14941d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    thread_pool_->Wait(self, true, false);
1495d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes  }
1496d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
14970b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier  size_t NextIndex() {
14983e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers    return index_.FetchAndAddSequentiallyConsistent(1);
14990b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier  }
15000b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier
1501d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes private:
150202b6a78038f12c109f95eb31713cfc747f5512f1Mathieu Chartier  class ForAllClosure : public Task {
15030e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier   public:
15040b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier    ForAllClosure(ParallelCompilationManager* manager, size_t end, Callback* callback)
1505219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers        : manager_(manager),
15060e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier          end_(end),
15070b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier          callback_(callback) {}
15089ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
15090e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    virtual void Run(Thread* self) {
15100b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier      while (true) {
15110b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier        const size_t index = manager_->NextIndex();
15120b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier        if (UNLIKELY(index >= end_)) {
15130b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier          break;
15140b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier        }
15150b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier        callback_(manager_, index);
15160e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier        self->AssertNoPendingException();
15170e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier      }
15180e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    }
151902b6a78038f12c109f95eb31713cfc747f5512f1Mathieu Chartier
152002b6a78038f12c109f95eb31713cfc747f5512f1Mathieu Chartier    virtual void Finalize() {
152102b6a78038f12c109f95eb31713cfc747f5512f1Mathieu Chartier      delete this;
152202b6a78038f12c109f95eb31713cfc747f5512f1Mathieu Chartier    }
15230cd7ec2dcd8d7ba30bf3ca420b40dac52849876cBrian Carlstrom
15240e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier   private:
15250b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier    ParallelCompilationManager* const manager_;
15260e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    const size_t end_;
1527460536209b741bc469f1b0857775449abb2102fbBernhard Rosenkränzer    Callback* const callback_;
15280e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier  };
15291e4092589f1400915e6213014da103aab8728ef6Elliott Hughes
15300b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier  AtomicInteger index_;
15310e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier  ClassLinker* const class_linker_;
15320e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier  const jobject class_loader_;
15331212a022fa5f8ef9585d765b1809521812af882cIan Rogers  CompilerDriver* const compiler_;
15340e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier  const DexFile* const dex_file_;
15350171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe  const std::vector<const DexFile*>& dex_files_;
1536219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers  ThreadPool* const thread_pool_;
15370b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier
15380b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier  DISALLOW_COPY_AND_ASSIGN(ParallelCompilationManager);
1539d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes};
1540d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
15410e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao// A fast version of SkipClass above if the class pointer is available
15420e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao// that avoids the expensive FindInClassPath search.
15430e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Haostatic bool SkipClass(jobject class_loader, const DexFile& dex_file, mirror::Class* klass)
15440e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
154535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  DCHECK(klass != nullptr);
15460e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  const DexFile& original_dex_file = *klass->GetDexCache()->GetDexFile();
15470e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  if (&dex_file != &original_dex_file) {
154835439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    if (class_loader == nullptr) {
15490e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao      LOG(WARNING) << "Skipping class " << PrettyDescriptor(klass) << " from "
15500e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao                   << dex_file.GetLocation() << " previously found in "
15510e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao                   << original_dex_file.GetLocation();
15520e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao    }
15530e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao    return true;
15540e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  }
15550e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  return false;
15560e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao}
15570e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao
155870b634882b523ec9795475dccc347b423cefaef6Mathieu Chartierstatic void CheckAndClearResolveException(Thread* self)
155970b634882b523ec9795475dccc347b423cefaef6Mathieu Chartier    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
156070b634882b523ec9795475dccc347b423cefaef6Mathieu Chartier  CHECK(self->IsExceptionPending());
156170b634882b523ec9795475dccc347b423cefaef6Mathieu Chartier  mirror::Throwable* exception = self->GetException(nullptr);
1562cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  std::string temp;
1563cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  const char* descriptor = exception->GetClass()->GetDescriptor(&temp);
1564cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  const char* expected_exceptions[] = {
1565cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      "Ljava/lang/IllegalAccessError;",
1566cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      "Ljava/lang/IncompatibleClassChangeError;",
1567cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      "Ljava/lang/InstantiationError;",
1568118785c6f82ac1ec7074b5efe367b3dafd904940Brian Carlstrom      "Ljava/lang/LinkageError;",
1569cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      "Ljava/lang/NoClassDefFoundError;",
1570cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      "Ljava/lang/NoSuchFieldError;",
1571cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      "Ljava/lang/NoSuchMethodError;"
1572cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  };
1573cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  bool found = false;
1574cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  for (size_t i = 0; (found == false) && (i < arraysize(expected_exceptions)); ++i) {
1575cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    if (strcmp(descriptor, expected_exceptions[i]) == 0) {
1576cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      found = true;
1577cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    }
1578cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  }
1579cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  if (!found) {
1580118785c6f82ac1ec7074b5efe367b3dafd904940Brian Carlstrom    LOG(FATAL) << "Unexpected exception " << exception->Dump();
158170b634882b523ec9795475dccc347b423cefaef6Mathieu Chartier  }
158270b634882b523ec9795475dccc347b423cefaef6Mathieu Chartier  self->ClearException();
158370b634882b523ec9795475dccc347b423cefaef6Mathieu Chartier}
158470b634882b523ec9795475dccc347b423cefaef6Mathieu Chartier
1585e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogersstatic void ResolveClassFieldsAndMethods(const ParallelCompilationManager* manager,
1586e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers                                         size_t class_def_index)
1587b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    LOCKS_EXCLUDED(Locks::mutator_lock_) {
15888f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers  ATRACE_CALL();
1589be7149fc2e7cc607937209f2819e3c1d672e2668Ian Rogers  Thread* self = Thread::Current();
1590be7149fc2e7cc607937209f2819e3c1d672e2668Ian Rogers  jobject jclass_loader = manager->GetClassLoader();
1591219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers  const DexFile& dex_file = *manager->GetDexFile();
1592be7149fc2e7cc607937209f2819e3c1d672e2668Ian Rogers  ClassLinker* class_linker = manager->GetClassLinker();
1593d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
1594e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers  // If an instance field is final then we need to have a barrier on the return, static final
1595e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers  // fields are assigned within the lock held for class initialization. Conservatively assume
1596e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers  // constructor barriers are always required.
1597e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers  bool requires_constructor_barrier = true;
1598e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers
1599845490bda68f7d025ea7f45775c847d2932e00dcBrian Carlstrom  // Method and Field are the worst. We can't resolve without either
1600845490bda68f7d025ea7f45775c847d2932e00dcBrian Carlstrom  // context from the code use (to disambiguate virtual vs direct
1601845490bda68f7d025ea7f45775c847d2932e00dcBrian Carlstrom  // method and instance vs static field) or from class
1602845490bda68f7d025ea7f45775c847d2932e00dcBrian Carlstrom  // definitions. While the compiler will resolve what it can as it
1603845490bda68f7d025ea7f45775c847d2932e00dcBrian Carlstrom  // needs it, here we try to resolve fields and methods used in class
1604845490bda68f7d025ea7f45775c847d2932e00dcBrian Carlstrom  // definitions, since many of them many never be referenced by
1605845490bda68f7d025ea7f45775c847d2932e00dcBrian Carlstrom  // generated code.
1606d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes  const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1607cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  ScopedObjectAccess soa(self);
1608cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  StackHandleScope<2> hs(soa.Self());
1609cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  Handle<mirror::ClassLoader> class_loader(
1610cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
1611cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(dex_file)));
1612cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  // Resolve the class.
1613cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  mirror::Class* klass = class_linker->ResolveType(dex_file, class_def.class_idx_, dex_cache,
1614cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers                                                   class_loader);
1615cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  bool resolve_fields_and_methods;
1616cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  if (klass == nullptr) {
1617cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    // Class couldn't be resolved, for example, super-class is in a different dex file. Don't
1618cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    // attempt to resolve methods and fields when there is no declaring class.
1619cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    CheckAndClearResolveException(soa.Self());
1620cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    resolve_fields_and_methods = false;
1621cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  } else {
1622cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    // We successfully resolved a class, should we skip it?
1623cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    if (SkipClass(jclass_loader, dex_file, klass)) {
1624cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      return;
1625cb5f5e53b580023fa2c1d8235c2e9aa1ff67d1dcBrian Carlstrom    }
1626cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    // We want to resolve the methods and fields eagerly.
1627cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    resolve_fields_and_methods = true;
1628cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  }
1629cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  // Note the class_data pointer advances through the headers,
1630cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  // static fields, instance fields, direct methods, and virtual
1631cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  // methods.
1632cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  const byte* class_data = dex_file.GetClassData(class_def);
163335439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (class_data == nullptr) {
1634cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    // Empty class such as a marker interface.
1635cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    requires_constructor_barrier = false;
1636cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  } else {
1637cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    ClassDataItemIterator it(dex_file, class_data);
1638cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    while (it.HasNextStaticField()) {
1639cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      if (resolve_fields_and_methods) {
1640cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers        mirror::ArtField* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(),
1641cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers                                                             dex_cache, class_loader, true);
164235439baf287b291b67ee406308e17fc6194facbfAndreas Gampe        if (field == nullptr) {
1643cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers          CheckAndClearResolveException(soa.Self());
1644e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers        }
1645e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers      }
1646cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      it.Next();
1647cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    }
1648cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    // We require a constructor barrier if there are final instance fields.
1649cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    requires_constructor_barrier = false;
1650cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    while (it.HasNextInstanceField()) {
16517fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      if (it.MemberIsFinal()) {
1652cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers        requires_constructor_barrier = true;
1653cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      }
1654cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      if (resolve_fields_and_methods) {
1655cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers        mirror::ArtField* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(),
1656cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers                                                             dex_cache, class_loader, false);
165735439baf287b291b67ee406308e17fc6194facbfAndreas Gampe        if (field == nullptr) {
1658cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers          CheckAndClearResolveException(soa.Self());
1659e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers        }
1660cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      }
1661cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      it.Next();
1662cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    }
1663cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    if (resolve_fields_and_methods) {
1664cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      while (it.HasNextDirectMethod()) {
1665cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers        mirror::ArtMethod* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(),
1666cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers                                                                dex_cache, class_loader,
1667cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers                                                                NullHandle<mirror::ArtMethod>(),
1668cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers                                                                it.GetMethodInvokeType(class_def));
166935439baf287b291b67ee406308e17fc6194facbfAndreas Gampe        if (method == nullptr) {
1670cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers          CheckAndClearResolveException(soa.Self());
1671e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers        }
1672e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers        it.Next();
1673e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers      }
1674cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      while (it.HasNextVirtualMethod()) {
1675cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers        mirror::ArtMethod* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(),
1676cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers                                                                dex_cache, class_loader,
1677cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers                                                                NullHandle<mirror::ArtMethod>(),
1678cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers                                                                it.GetMethodInvokeType(class_def));
167935439baf287b291b67ee406308e17fc6194facbfAndreas Gampe        if (method == nullptr) {
1680cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers          CheckAndClearResolveException(soa.Self());
1681e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers        }
1682cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers        it.Next();
1683e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers      }
1684cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      DCHECK(!it.HasNext());
168520cfffabdc9e02b2df798bc4e6b6035d14bf4e36Brian Carlstrom    }
1686d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes  }
1687fffdb023275613612a22ec62b3421ffe4d2b73feIan Rogers  if (requires_constructor_barrier) {
1688be7149fc2e7cc607937209f2819e3c1d672e2668Ian Rogers    manager->GetCompiler()->AddRequiresConstructorBarrier(self, &dex_file, class_def_index);
1689fffdb023275613612a22ec62b3421ffe4d2b73feIan Rogers  }
1690d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes}
1691d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
1692219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogersstatic void ResolveType(const ParallelCompilationManager* manager, size_t type_idx)
1693b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    LOCKS_EXCLUDED(Locks::mutator_lock_) {
1694d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes  // Class derived values are more complicated, they require the linker and loader.
169500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
1696219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers  ClassLinker* class_linker = manager->GetClassLinker();
1697219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers  const DexFile& dex_file = *manager->GetDexFile();
1698eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  StackHandleScope<2> hs(soa.Self());
1699eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(dex_file)));
1700eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::ClassLoader> class_loader(
1701eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      hs.NewHandle(soa.Decode<mirror::ClassLoader*>(manager->GetClassLoader())));
17022dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
170300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
170435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (klass == nullptr) {
170500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    CHECK(soa.Self()->IsExceptionPending());
170635439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    mirror::Throwable* exception = soa.Self()->GetException(nullptr);
1707a436fde2762664a3ecdda5eefcadd20b2e104f59Ian Rogers    VLOG(compiler) << "Exception during type resolution: " << exception->Dump();
1708f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier    if (exception->GetClass()->DescriptorEquals("Ljava/lang/OutOfMemoryError;")) {
1709a436fde2762664a3ecdda5eefcadd20b2e104f59Ian Rogers      // There's little point continuing compilation if the heap is exhausted.
1710a436fde2762664a3ecdda5eefcadd20b2e104f59Ian Rogers      LOG(FATAL) << "Out of memory during type resolution for compilation";
1711a436fde2762664a3ecdda5eefcadd20b2e104f59Ian Rogers    }
1712a436fde2762664a3ecdda5eefcadd20b2e104f59Ian Rogers    soa.Self()->ClearException();
1713d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes  }
1714d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes}
1715d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
17161212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::ResolveDexFile(jobject class_loader, const DexFile& dex_file,
17170171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe                                    const std::vector<const DexFile*>& dex_files,
17183d504075f7c1204d581923460754bf6d3714b13fIan Rogers                                    ThreadPool* thread_pool, TimingLogger* timings) {
1719d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1720d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
172100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // TODO: we could resolve strings here, although the string table is largely filled with class
172200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  //       and method names.
1723d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
17240171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe  ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, dex_files,
17250171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe                                     thread_pool);
1726e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers  if (IsImage()) {
1727e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers    // For images we resolve all types, such as array, whereas for applications just those with
1728e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers    // classdefs are resolved by ResolveClassFieldsAndMethods.
1729f5997b4d3f889569d5a2b724d83d764bfbb8d106Mathieu Chartier    TimingLogger::ScopedTiming t("Resolve Types", timings);
1730e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers    context.ForAll(0, dex_file.NumTypeIds(), ResolveType, thread_count_);
1731e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers  }
1732d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
1733f5997b4d3f889569d5a2b724d83d764bfbb8d106Mathieu Chartier  TimingLogger::ScopedTiming t("Resolve MethodsAndFields", timings);
17340e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier  context.ForAll(0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_);
17359ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom}
17369ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
173735439baf287b291b67ee406308e17fc6194facbfAndreas Gampevoid CompilerDriver::SetVerified(jobject class_loader, const std::vector<const DexFile*>& dex_files,
173835439baf287b291b67ee406308e17fc6194facbfAndreas Gampe                                 ThreadPool* thread_pool, TimingLogger* timings) {
173935439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  for (size_t i = 0; i != dex_files.size(); ++i) {
174035439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    const DexFile* dex_file = dex_files[i];
174135439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    CHECK(dex_file != nullptr);
174235439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    SetVerifiedDexFile(class_loader, *dex_file, dex_files, thread_pool, timings);
174335439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  }
174435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe}
174535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe
17461212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::Verify(jobject class_loader, const std::vector<const DexFile*>& dex_files,
17473d504075f7c1204d581923460754bf6d3714b13fIan Rogers                            ThreadPool* thread_pool, TimingLogger* timings) {
1748ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  for (size_t i = 0; i != dex_files.size(); ++i) {
1749ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    const DexFile* dex_file = dex_files[i];
175035439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    CHECK(dex_file != nullptr);
17510171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe    VerifyDexFile(class_loader, *dex_file, dex_files, thread_pool, timings);
175298eacac683b78e60799323e8c7d59e7214808639jeffhao  }
175398eacac683b78e60799323e8c7d59e7214808639jeffhao}
175498eacac683b78e60799323e8c7d59e7214808639jeffhao
1755219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogersstatic void VerifyClass(const ParallelCompilationManager* manager, size_t class_def_index)
1756b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    LOCKS_EXCLUDED(Locks::mutator_lock_) {
175767f99418f648c3a95256ed3dcd8e8b64eef0b372Anwar Ghuloum  ATRACE_CALL();
175800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
17590e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  const DexFile& dex_file = *manager->GetDexFile();
17600e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
17610e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  const char* descriptor = dex_file.GetClassDescriptor(class_def);
17620e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  ClassLinker* class_linker = manager->GetClassLinker();
17630e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  jobject jclass_loader = manager->GetClassLoader();
1764eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  StackHandleScope<3> hs(soa.Self());
1765eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::ClassLoader> class_loader(
1766eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
1767eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::Class> klass(
1768eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor, class_loader)));
1769eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  if (klass.Get() == nullptr) {
1770e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers    CHECK(soa.Self()->IsExceptionPending());
177162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    soa.Self()->ClearException();
1772f56197c5519395fcf8226a7148cc87367e0a7ad4jeffhao
1773f56197c5519395fcf8226a7148cc87367e0a7ad4jeffhao    /*
1774f56197c5519395fcf8226a7148cc87367e0a7ad4jeffhao     * At compile time, we can still structurally verify the class even if FindClass fails.
1775f56197c5519395fcf8226a7148cc87367e0a7ad4jeffhao     * This is to ensure the class is structurally sound for compilation. An unsound class
1776f56197c5519395fcf8226a7148cc87367e0a7ad4jeffhao     * will be rejected by the verifier and later skipped during compilation in the compiler.
1777f56197c5519395fcf8226a7148cc87367e0a7ad4jeffhao     */
1778eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(dex_file)));
1779f56197c5519395fcf8226a7148cc87367e0a7ad4jeffhao    std::string error_msg;
1780590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    if (verifier::MethodVerifier::VerifyClass(&dex_file, dex_cache, class_loader, &class_def, true,
1781590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier                                              &error_msg) ==
178200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                                  verifier::MethodVerifier::kHardFailure) {
17830e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao      LOG(ERROR) << "Verification failed on class " << PrettyDescriptor(descriptor)
1784f56197c5519395fcf8226a7148cc87367e0a7ad4jeffhao                 << " because: " << error_msg;
1785f56197c5519395fcf8226a7148cc87367e0a7ad4jeffhao    }
1786eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  } else if (!SkipClass(jclass_loader, dex_file, klass.Get())) {
1787eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    CHECK(klass->IsResolved()) << PrettyClass(klass.Get());
17880e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao    class_linker->VerifyClass(klass);
1789d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
1790e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers    if (klass->IsErroneous()) {
1791e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers      // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
1792e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers      CHECK(soa.Self()->IsExceptionPending());
1793e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers      soa.Self()->ClearException();
1794e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers    }
179567f99418f648c3a95256ed3dcd8e8b64eef0b372Anwar Ghuloum
1796e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers    CHECK(klass->IsCompileTimeVerified() || klass->IsErroneous())
1797eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        << PrettyDescriptor(klass.Get()) << ": state=" << klass->GetStatus();
1798e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers  }
179962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  soa.Self()->AssertNoPendingException();
1800d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes}
1801d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
18021212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::VerifyDexFile(jobject class_loader, const DexFile& dex_file,
18030171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe                                   const std::vector<const DexFile*>& dex_files,
18043d504075f7c1204d581923460754bf6d3714b13fIan Rogers                                   ThreadPool* thread_pool, TimingLogger* timings) {
1805f5997b4d3f889569d5a2b724d83d764bfbb8d106Mathieu Chartier  TimingLogger::ScopedTiming t("Verify Dex File", timings);
1806731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
18070171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe  ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, dex_files,
18080171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe                                     thread_pool);
18090e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier  context.ForAll(0, dex_file.NumClassDefs(), VerifyClass, thread_count_);
1810a5a97a2bc1dfed70869da34650a5a2a3a3a06ac4Brian Carlstrom}
1811a5a97a2bc1dfed70869da34650a5a2a3a3a06ac4Brian Carlstrom
181235439baf287b291b67ee406308e17fc6194facbfAndreas Gampestatic void SetVerifiedClass(const ParallelCompilationManager* manager, size_t class_def_index)
181335439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    LOCKS_EXCLUDED(Locks::mutator_lock_) {
181435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  ATRACE_CALL();
181535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  ScopedObjectAccess soa(Thread::Current());
181635439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  const DexFile& dex_file = *manager->GetDexFile();
181735439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
181835439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  const char* descriptor = dex_file.GetClassDescriptor(class_def);
181935439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  ClassLinker* class_linker = manager->GetClassLinker();
182035439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  jobject jclass_loader = manager->GetClassLoader();
182135439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  StackHandleScope<3> hs(soa.Self());
182235439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  Handle<mirror::ClassLoader> class_loader(
182335439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
182435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  Handle<mirror::Class> klass(
182535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor, class_loader)));
182635439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  // Class might have failed resolution. Then don't set it to verified.
182735439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (klass.Get() != nullptr) {
182835439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    // Only do this if the class is resolved. If even resolution fails, quickening will go very,
182935439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    // very wrong.
183035439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    if (klass->IsResolved()) {
183135439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      if (klass->GetStatus() < mirror::Class::kStatusVerified) {
183235439baf287b291b67ee406308e17fc6194facbfAndreas Gampe        ObjectLock<mirror::Class> lock(soa.Self(), klass);
183335439baf287b291b67ee406308e17fc6194facbfAndreas Gampe        klass->SetStatus(mirror::Class::kStatusVerified, soa.Self());
183435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      }
183535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      // Record the final class status if necessary.
183635439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      ClassReference ref(manager->GetDexFile(), class_def_index);
183735439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      manager->GetCompiler()->RecordClassStatus(ref, klass->GetStatus());
183835439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    }
1839ba354a97a9742774f57bad7f96ccae2dbbd59f75Andreas Gampe  } else {
1840ba354a97a9742774f57bad7f96ccae2dbbd59f75Andreas Gampe    Thread* self = soa.Self();
1841ba354a97a9742774f57bad7f96ccae2dbbd59f75Andreas Gampe    DCHECK(self->IsExceptionPending());
1842ba354a97a9742774f57bad7f96ccae2dbbd59f75Andreas Gampe    self->ClearException();
184335439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  }
184435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe}
184535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe
184635439baf287b291b67ee406308e17fc6194facbfAndreas Gampevoid CompilerDriver::SetVerifiedDexFile(jobject class_loader, const DexFile& dex_file,
184735439baf287b291b67ee406308e17fc6194facbfAndreas Gampe                                        const std::vector<const DexFile*>& dex_files,
184835439baf287b291b67ee406308e17fc6194facbfAndreas Gampe                                        ThreadPool* thread_pool, TimingLogger* timings) {
184935439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  TimingLogger::ScopedTiming t("Verify Dex File", timings);
185035439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
185135439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, dex_files,
185235439baf287b291b67ee406308e17fc6194facbfAndreas Gampe                                     thread_pool);
185335439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  context.ForAll(0, dex_file.NumClassDefs(), SetVerifiedClass, thread_count_);
185435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe}
185535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe
1856219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogersstatic void InitializeClass(const ParallelCompilationManager* manager, size_t class_def_index)
18573d1548debdaf8a1915fa432a4d267f7c0145654dIan Rogers    LOCKS_EXCLUDED(Locks::mutator_lock_) {
18588f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers  ATRACE_CALL();
18590e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  jobject jclass_loader = manager->GetClassLoader();
18600e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  const DexFile& dex_file = *manager->GetDexFile();
18610e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1862bcdbbfebc8f32566d4cb3f66405e89cdb7351992Jeff Hao  const DexFile::TypeId& class_type_id = dex_file.GetTypeId(class_def.class_idx_);
1863bcdbbfebc8f32566d4cb3f66405e89cdb7351992Jeff Hao  const char* descriptor = dex_file.StringDataByIdx(class_type_id.descriptor_idx_);
1864fc0e94bed3f88ed7e50854fd8dfaf5dcb345250fIan Rogers
186500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
1866eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  StackHandleScope<3> hs(soa.Self());
1867eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::ClassLoader> class_loader(
1868eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
1869eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::Class> klass(
1870eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      hs.NewHandle(manager->GetClassLinker()->FindClass(soa.Self(), descriptor, class_loader)));
18710e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao
1872eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  if (klass.Get() != nullptr && !SkipClass(jclass_loader, dex_file, klass.Get())) {
187364b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers    // Only try to initialize classes that were successfully verified.
18743d1548debdaf8a1915fa432a4d267f7c0145654dIan Rogers    if (klass->IsVerified()) {
18758f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      // Attempt to initialize the class but bail if we either need to initialize the super-class
18768f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      // or static fields.
18778f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      manager->GetClassLinker()->EnsureInitialized(klass, false, false);
187864b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers      if (!klass->IsInitialized()) {
18798f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        // We don't want non-trivial class initialization occurring on multiple threads due to
18808f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        // deadlock problems. For example, a parent class is initialized (holding its lock) that
18818f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        // refers to a sub-class in its static/class initializer causing it to try to acquire the
18828f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        // sub-class' lock. While on a second thread the sub-class is initialized (holding its lock)
18838f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        // after first initializing its parents, whose locks are acquired. This leads to a
18848f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        // parent-to-child and a child-to-parent lock ordering and consequent potential deadlock.
18858f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        // We need to use an ObjectLock due to potential suspension in the interpreting code. Rather
18868f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        // than use a special Object for the purpose we use the Class of java.lang.Class.
1887eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        Handle<mirror::Class> h_klass(hs.NewHandle(klass->GetClass()));
1888db2633ce0358c704f97130a94b582602cb01d14aMathieu Chartier        ObjectLock<mirror::Class> lock(soa.Self(), h_klass);
18898f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        // Attempt to initialize allowing initialization of parent classes but still not static
18908f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        // fields.
18918f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        manager->GetClassLinker()->EnsureInitialized(klass, false, true);
18928f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        if (!klass->IsInitialized()) {
18938f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers          // We need to initialize static fields, we only do this for image classes that aren't
1894d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz          // marked with the $NoPreloadHolder (which implies this should not be initialized early).
18958f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers          bool can_init_static_fields = manager->GetCompiler()->IsImage() &&
1896d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz              manager->GetCompiler()->IsImageClass(descriptor) &&
1897d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz              !StringPiece(descriptor).ends_with("$NoPreloadHolder;");
18988f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers          if (can_init_static_fields) {
1899d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz            VLOG(compiler) << "Initializing: " << descriptor;
1900c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            // TODO multithreading support. We should ensure the current compilation thread has
1901c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            // exclusive access to the runtime and the transaction. To achieve this, we could use
1902c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            // a ReaderWriterMutex but we're holding the mutator lock so we fail mutex sanity
1903c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            // checks in Thread::AssertThreadSuspensionIsAllowable.
1904c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            Runtime* const runtime = Runtime::Current();
1905c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            Transaction transaction;
1906c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers
1907c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            // Run the class initializer in transaction mode.
1908c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            runtime->EnterTransactionMode(&transaction);
1909c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            const mirror::Class::Status old_status = klass->GetStatus();
1910c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            bool success = manager->GetClassLinker()->EnsureInitialized(klass, true, true);
1911c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            // TODO we detach transaction from runtime to indicate we quit the transactional
1912c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            // mode which prevents the GC from visiting objects modified during the transaction.
1913c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            // Ensure GC is not run so don't access freed objects when aborting transaction.
1914c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            const char* old_casue = soa.Self()->StartAssertNoThreadSuspension("Transaction end");
1915c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            runtime->ExitTransactionMode();
1916c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers
1917c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            if (!success) {
1918c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers              CHECK(soa.Self()->IsExceptionPending());
1919c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers              ThrowLocation throw_location;
1920c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers              mirror::Throwable* exception = soa.Self()->GetException(&throw_location);
1921c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers              VLOG(compiler) << "Initialization of " << descriptor << " aborted because of "
1922c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers                  << exception->Dump();
1923c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers              soa.Self()->ClearException();
1924c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers              transaction.Abort();
1925c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers              CHECK_EQ(old_status, klass->GetStatus()) << "Previous class status not restored";
192664b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers            }
1927c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            soa.Self()->EndAssertNoThreadSuspension(old_casue);
192864b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers          }
192964b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers        }
19308f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        soa.Self()->AssertNoPendingException();
193164b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers      }
193227ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom    }
19333d1548debdaf8a1915fa432a4d267f7c0145654dIan Rogers    // Record the final class status if necessary.
193451c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom    ClassReference ref(manager->GetDexFile(), class_def_index);
19358f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers    manager->GetCompiler()->RecordClassStatus(ref, klass->GetStatus());
193698eacac683b78e60799323e8c7d59e7214808639jeffhao  }
19371f5393447b9f45be7918042d9ee7b521376de866Ian Rogers  // Clear any class not found or verification exceptions.
193862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  soa.Self()->ClearException();
193998eacac683b78e60799323e8c7d59e7214808639jeffhao}
194098eacac683b78e60799323e8c7d59e7214808639jeffhao
19411212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::InitializeClasses(jobject jni_class_loader, const DexFile& dex_file,
19420171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe                                       const std::vector<const DexFile*>& dex_files,
19433d504075f7c1204d581923460754bf6d3714b13fIan Rogers                                       ThreadPool* thread_pool, TimingLogger* timings) {
1944f5997b4d3f889569d5a2b724d83d764bfbb8d106Mathieu Chartier  TimingLogger::ScopedTiming t("InitializeNoClinit", timings);
19453d1548debdaf8a1915fa432a4d267f7c0145654dIan Rogers  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
19460171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe  ParallelCompilationManager context(class_linker, jni_class_loader, this, &dex_file, dex_files,
19470171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe                                     thread_pool);
1948d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz  size_t thread_count;
1949d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz  if (IsImage()) {
1950d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz    // TODO: remove this when transactional mode supports multithreading.
1951d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz    thread_count = 1U;
1952d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz  } else {
1953d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz    thread_count = thread_count_;
1954d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz  }
1955d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz  context.ForAll(0, dex_file.NumClassDefs(), InitializeClass, thread_count);
19563d1548debdaf8a1915fa432a4d267f7c0145654dIan Rogers}
195790dc30f4b9967e850d0594e57dfa8e7cb0369575Shih-wei Liao
19581212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::InitializeClasses(jobject class_loader,
19591212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                       const std::vector<const DexFile*>& dex_files,
19603d504075f7c1204d581923460754bf6d3714b13fIan Rogers                                       ThreadPool* thread_pool, TimingLogger* timings) {
19613d1548debdaf8a1915fa432a4d267f7c0145654dIan Rogers  for (size_t i = 0; i != dex_files.size(); ++i) {
19623d1548debdaf8a1915fa432a4d267f7c0145654dIan Rogers    const DexFile* dex_file = dex_files[i];
196335439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    CHECK(dex_file != nullptr);
19640171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe    InitializeClasses(class_loader, *dex_file, dex_files, thread_pool, timings);
196500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
1966a73a8b55415b544355438522524a640378698d85Mathieu Chartier  if (IsImage()) {
1967a73a8b55415b544355438522524a640378698d85Mathieu Chartier    // Prune garbage objects created during aborted transactions.
1968a73a8b55415b544355438522524a640378698d85Mathieu Chartier    Runtime::Current()->GetHeap()->CollectGarbage(true);
1969a73a8b55415b544355438522524a640378698d85Mathieu Chartier  }
197090dc30f4b9967e850d0594e57dfa8e7cb0369575Shih-wei Liao}
197190dc30f4b9967e850d0594e57dfa8e7cb0369575Shih-wei Liao
19721212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::Compile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
19733d504075f7c1204d581923460754bf6d3714b13fIan Rogers                             ThreadPool* thread_pool, TimingLogger* timings) {
1974ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  for (size_t i = 0; i != dex_files.size(); ++i) {
1975ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    const DexFile* dex_file = dex_files[i];
197635439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    CHECK(dex_file != nullptr);
19770171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe    CompileDexFile(class_loader, *dex_file, dex_files, thread_pool, timings);
197883db7721aef15df6919c0ec072e087bef6041e2dBrian Carlstrom  }
1979aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe  VLOG(compiler) << "Compile: " << GetMemoryUsageString(false);
198083db7721aef15df6919c0ec072e087bef6041e2dBrian Carlstrom}
198183db7721aef15df6919c0ec072e087bef6041e2dBrian Carlstrom
1982219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogersvoid CompilerDriver::CompileClass(const ParallelCompilationManager* manager, size_t class_def_index) {
198367f99418f648c3a95256ed3dcd8e8b64eef0b372Anwar Ghuloum  ATRACE_CALL();
1984219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers  const DexFile& dex_file = *manager->GetDexFile();
1985c225caa9715eeaeff87f27d5b6a3e7d4f6b7efadElliott Hughes  const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1986be7149fc2e7cc607937209f2819e3c1d672e2668Ian Rogers  ClassLinker* class_linker = manager->GetClassLinker();
1987cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  jobject jclass_loader = manager->GetClassLoader();
1988cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  {
1989cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    // Use a scoped object access to perform to the quick SkipClass check.
1990cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    const char* descriptor = dex_file.GetClassDescriptor(class_def);
1991cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    ScopedObjectAccess soa(Thread::Current());
1992cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    StackHandleScope<3> hs(soa.Self());
1993cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    Handle<mirror::ClassLoader> class_loader(
1994cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers        hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
1995cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    Handle<mirror::Class> klass(
1996cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers        hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor, class_loader)));
1997cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    if (klass.Get() == nullptr) {
1998cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      CHECK(soa.Self()->IsExceptionPending());
1999cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      soa.Self()->ClearException();
2000cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    } else if (SkipClass(jclass_loader, dex_file, klass.Get())) {
2001cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      return;
2002cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    }
20035ead0950c661761e90e04aefd6ea2205532ce874Brian Carlstrom  }
2004d1224c79631bd1801b067a0f212b91afa961a362jeffhao  ClassReference ref(&dex_file, class_def_index);
2005d1224c79631bd1801b067a0f212b91afa961a362jeffhao  // Skip compiling classes with generic verifier failures since they will still fail at runtime
2006c7f832061fea59fd6abd125f26c8ca1faec695a5Vladimir Marko  if (manager->GetCompiler()->verification_results_->IsClassRejected(ref)) {
2007d1224c79631bd1801b067a0f212b91afa961a362jeffhao    return;
2008d1224c79631bd1801b067a0f212b91afa961a362jeffhao  }
20090571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  const byte* class_data = dex_file.GetClassData(class_def);
201035439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (class_data == nullptr) {
20110571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    // empty class, probably a marker interface
20120571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    return;
20130571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  }
201467f99418f648c3a95256ed3dcd8e8b64eef0b372Anwar Ghuloum
20152d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  // Can we run DEX-to-DEX compiler on this class ?
201675021222d9c03a80fa5c136db0d5fb8d82d04031Sebastien Hertz  DexToDexCompilationLevel dex_to_dex_compilation_level = kDontDexToDexCompile;
20172d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  {
20182d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz    ScopedObjectAccess soa(Thread::Current());
2019eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    StackHandleScope<1> hs(soa.Self());
2020eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    Handle<mirror::ClassLoader> class_loader(
2021eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
20229837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    dex_to_dex_compilation_level = GetDexToDexCompilationlevel(soa.Self(), class_loader, dex_file,
20239837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers                                                               class_def);
20242d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  }
20250571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  ClassDataItemIterator it(dex_file, class_data);
20260571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  // Skip fields
20270571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  while (it.HasNextStaticField()) {
20280571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    it.Next();
20290571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  }
20300571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  while (it.HasNextInstanceField()) {
20310571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    it.Next();
20320571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  }
2033be7149fc2e7cc607937209f2819e3c1d672e2668Ian Rogers  CompilerDriver* driver = manager->GetCompiler();
203426318f722958ac1cba6a812026a1377f37c54941Andreas Gampe
203526318f722958ac1cba6a812026a1377f37c54941Andreas Gampe  bool compilation_enabled = driver->IsClassToCompile(
203626318f722958ac1cba6a812026a1377f37c54941Andreas Gampe      dex_file.StringByTypeIdx(class_def.class_idx_));
203726318f722958ac1cba6a812026a1377f37c54941Andreas Gampe
20380571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  // Compile direct methods
203968adbe41c7d9295da2bfc521d737ba6dabd36c98Brian Carlstrom  int64_t previous_direct_method_idx = -1;
20400571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  while (it.HasNextDirectMethod()) {
20416f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom    uint32_t method_idx = it.GetMemberIndex();
20426f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom    if (method_idx == previous_direct_method_idx) {
20436f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom      // smali can create dex files with two encoded_methods sharing the same method_idx
20446f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom      // http://code.google.com/p/smali/issues/detail?id=119
20456f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom      it.Next();
20466f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom      continue;
20476f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom    }
20486f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom    previous_direct_method_idx = method_idx;
20497fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    driver->CompileMethod(it.GetMethodCodeItem(), it.GetMethodAccessFlags(),
2050be7149fc2e7cc607937209f2819e3c1d672e2668Ian Rogers                          it.GetMethodInvokeType(class_def), class_def_index,
205126318f722958ac1cba6a812026a1377f37c54941Andreas Gampe                          method_idx, jclass_loader, dex_file, dex_to_dex_compilation_level,
205226318f722958ac1cba6a812026a1377f37c54941Andreas Gampe                          compilation_enabled);
20530571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    it.Next();
20549ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  }
20550571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  // Compile virtual methods
205668adbe41c7d9295da2bfc521d737ba6dabd36c98Brian Carlstrom  int64_t previous_virtual_method_idx = -1;
20570571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  while (it.HasNextVirtualMethod()) {
20586f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom    uint32_t method_idx = it.GetMemberIndex();
20596f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom    if (method_idx == previous_virtual_method_idx) {
20606f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom      // smali can create dex files with two encoded_methods sharing the same method_idx
20616f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom      // http://code.google.com/p/smali/issues/detail?id=119
20626f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom      it.Next();
20636f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom      continue;
20646f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom    }
20656f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom    previous_virtual_method_idx = method_idx;
20667fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    driver->CompileMethod(it.GetMethodCodeItem(), it.GetMethodAccessFlags(),
2067be7149fc2e7cc607937209f2819e3c1d672e2668Ian Rogers                          it.GetMethodInvokeType(class_def), class_def_index,
206826318f722958ac1cba6a812026a1377f37c54941Andreas Gampe                          method_idx, jclass_loader, dex_file, dex_to_dex_compilation_level,
206926318f722958ac1cba6a812026a1377f37c54941Andreas Gampe                          compilation_enabled);
20700571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    it.Next();
20719ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  }
20720571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  DCHECK(!it.HasNext());
20739ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom}
20749ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
20751212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::CompileDexFile(jobject class_loader, const DexFile& dex_file,
20760171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe                                    const std::vector<const DexFile*>& dex_files,
20773d504075f7c1204d581923460754bf6d3714b13fIan Rogers                                    ThreadPool* thread_pool, TimingLogger* timings) {
2078f5997b4d3f889569d5a2b724d83d764bfbb8d106Mathieu Chartier  TimingLogger::ScopedTiming t("Compile Dex File", timings);
2079be7149fc2e7cc607937209f2819e3c1d672e2668Ian Rogers  ParallelCompilationManager context(Runtime::Current()->GetClassLinker(), class_loader, this,
20800171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe                                     &dex_file, dex_files, thread_pool);
20811212a022fa5f8ef9585d765b1809521812af882cIan Rogers  context.ForAll(0, dex_file.NumClassDefs(), CompilerDriver::CompileClass, thread_count_);
2082c225caa9715eeaeff87f27d5b6a3e7d4f6b7efadElliott Hughes}
2083c225caa9715eeaeff87f27d5b6a3e7d4f6b7efadElliott Hughes
20841212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
20858b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                   InvokeType invoke_type, uint16_t class_def_idx,
20861212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                   uint32_t method_idx, jobject class_loader,
20872d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz                                   const DexFile& dex_file,
208826318f722958ac1cba6a812026a1377f37c54941Andreas Gampe                                   DexToDexCompilationLevel dex_to_dex_compilation_level,
208926318f722958ac1cba6a812026a1377f37c54941Andreas Gampe                                   bool compilation_enabled) {
209035439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  CompiledMethod* compiled_method = nullptr;
2091b68c6e578a28a9717d78dfd522d9d9b8befaedf2Mathieu Chartier  uint64_t start_ns = kTimeCompileMethod ? NanoTime() : 0;
209225fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  MethodReference method_ref(&dex_file, method_idx);
20934dd96f56909ec35c83a3d468b0e47769988c1a1dLogan Chien
2094169c9a7f46776b235d0a37d5e0ff27682deffe06Ian Rogers  if ((access_flags & kAccNative) != 0) {
20950188ab739c3ef8bcd521d88f84894db8c0f012c5Ian Rogers    // Are we interpreting only and have support for generic JNI down calls?
20964a200f56b7075309316b04d550c9cc50f8314eddJeff Hao    if (!compiler_options_->IsCompilationEnabled() &&
2097b95a5345ae4217b70ca36f0cced92f68dda7caf5Stuart Monteith        (instruction_set_ == kX86_64 || instruction_set_ == kArm64)) {
20985b27149ce3cbf39f99c9d9c8ba5ebf1657c297e1Ian Rogers      // Leaving this empty will trigger the generic JNI version
20995b27149ce3cbf39f99c9d9c8ba5ebf1657c297e1Ian Rogers    } else {
210072d32629303f8f39362a4099481f48646aed042fIan Rogers      compiled_method = compiler_->JniCompile(access_flags, method_idx, dex_file);
210135439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      CHECK(compiled_method != nullptr);
21025b27149ce3cbf39f99c9d9c8ba5ebf1657c297e1Ian Rogers    }
2103169c9a7f46776b235d0a37d5e0ff27682deffe06Ian Rogers  } else if ((access_flags & kAccAbstract) != 0) {
21042cc022b653e1e84eed2522254ec684bd097572b8Brian Carlstrom  } else {
2105f535c69f115c61ffadca1bd2706244d0aa30f9aaAndreas Gampe    bool has_verified_method = verification_results_->GetVerifiedMethod(method_ref) != nullptr;
210626318f722958ac1cba6a812026a1377f37c54941Andreas Gampe    bool compile = compilation_enabled &&
2107f535c69f115c61ffadca1bd2706244d0aa30f9aaAndreas Gampe                   // Basic checks, e.g., not <clinit>.
2108f535c69f115c61ffadca1bd2706244d0aa30f9aaAndreas Gampe                   verification_results_->IsCandidateForCompilation(method_ref, access_flags) &&
2109f535c69f115c61ffadca1bd2706244d0aa30f9aaAndreas Gampe                   // Did not fail to create VerifiedMethod metadata.
2110f535c69f115c61ffadca1bd2706244d0aa30f9aaAndreas Gampe                   has_verified_method;
21114d4adb1dae07bb7421e863732ab789413a3b43f0Sebastien Hertz    if (compile) {
211235439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      // NOTE: if compiler declines to compile this method, it will return nullptr.
211372d32629303f8f39362a4099481f48646aed042fIan Rogers      compiled_method = compiler_->Compile(code_item, access_flags, invoke_type, class_def_idx,
211472d32629303f8f39362a4099481f48646aed042fIan Rogers                                           method_idx, class_loader, dex_file);
211517965ed6ec29407599a0bcd4ea92532424294fedSebastien Hertz    }
211617965ed6ec29407599a0bcd4ea92532424294fedSebastien Hertz    if (compiled_method == nullptr && dex_to_dex_compilation_level != kDontDexToDexCompile) {
211717965ed6ec29407599a0bcd4ea92532424294fedSebastien Hertz      // TODO: add a command-line option to disable DEX-to-DEX compilation ?
2118f535c69f115c61ffadca1bd2706244d0aa30f9aaAndreas Gampe      // Do not optimize if a VerifiedMethod is missing. SafeCast elision, for example, relies on
2119f535c69f115c61ffadca1bd2706244d0aa30f9aaAndreas Gampe      // it.
212075021222d9c03a80fa5c136db0d5fb8d82d04031Sebastien Hertz      (*dex_to_dex_compiler_)(*this, code_item, access_flags,
212175021222d9c03a80fa5c136db0d5fb8d82d04031Sebastien Hertz                              invoke_type, class_def_idx,
212275021222d9c03a80fa5c136db0d5fb8d82d04031Sebastien Hertz                              method_idx, class_loader, dex_file,
2123f535c69f115c61ffadca1bd2706244d0aa30f9aaAndreas Gampe                              has_verified_method ? dex_to_dex_compilation_level : kRequired);
2124f3e9855ee2000106b54fd479f7a46da2dc2ad079Ian Rogers    }
2125bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes  }
2126b68c6e578a28a9717d78dfd522d9d9b8befaedf2Mathieu Chartier  if (kTimeCompileMethod) {
2127b68c6e578a28a9717d78dfd522d9d9b8befaedf2Mathieu Chartier    uint64_t duration_ns = NanoTime() - start_ns;
2128b68c6e578a28a9717d78dfd522d9d9b8befaedf2Mathieu Chartier    if (duration_ns > MsToNs(compiler_->GetMaximumCompilationTimeBeforeWarning())) {
2129b68c6e578a28a9717d78dfd522d9d9b8befaedf2Mathieu Chartier      LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
2130b68c6e578a28a9717d78dfd522d9d9b8befaedf2Mathieu Chartier                   << " took " << PrettyDuration(duration_ns);
2131b68c6e578a28a9717d78dfd522d9d9b8befaedf2Mathieu Chartier    }
2132f09afe8fbd78943df6a8b10f03c36dcd190dd054Elliott Hughes  }
2133f09afe8fbd78943df6a8b10f03c36dcd190dd054Elliott Hughes
213450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  Thread* self = Thread::Current();
213535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (compiled_method != nullptr) {
213625fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier    DCHECK(GetCompiledMethod(method_ref) == nullptr) << PrettyMethod(method_idx, dex_file);
213700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
213850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      MutexLock mu(self, compiled_methods_lock_);
213925fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier      compiled_methods_.Put(method_ref, compiled_method);
214000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
214125fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier    DCHECK(GetCompiledMethod(method_ref) != nullptr) << PrettyMethod(method_idx, dex_file);
21422cc022b653e1e84eed2522254ec684bd097572b8Brian Carlstrom  }
21439baa4aefc370f48774b6104680193d9a7e4fb631Brian Carlstrom
214425fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  // Done compiling, delete the verified method to reduce native memory usage.
214525fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  verification_results_->RemoveVerifiedMethod(method_ref);
214625fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier
214750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  if (self->IsExceptionPending()) {
214850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    ScopedObjectAccess soa(self);
214900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    LOG(FATAL) << "Unexpected exception compiling: " << PrettyMethod(method_idx, dex_file) << "\n"
215035439baf287b291b67ee406308e17fc6194facbfAndreas Gampe        << self->GetException(nullptr)->Dump();
215100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
21520571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers}
215328ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers
21541212a022fa5f8ef9585d765b1809521812af882cIan RogersCompiledClass* CompilerDriver::GetCompiledClass(ClassReference ref) const {
215550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(Thread::Current(), compiled_classes_lock_);
21560755ec5ea1dce0b549fc1adefeb52d89f119ebecBrian Carlstrom  ClassTable::const_iterator it = compiled_classes_.find(ref);
21570755ec5ea1dce0b549fc1adefeb52d89f119ebecBrian Carlstrom  if (it == compiled_classes_.end()) {
215835439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    return nullptr;
21590755ec5ea1dce0b549fc1adefeb52d89f119ebecBrian Carlstrom  }
216035439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  CHECK(it->second != nullptr);
21610755ec5ea1dce0b549fc1adefeb52d89f119ebecBrian Carlstrom  return it->second;
21620755ec5ea1dce0b549fc1adefeb52d89f119ebecBrian Carlstrom}
21630755ec5ea1dce0b549fc1adefeb52d89f119ebecBrian Carlstrom
21648f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogersvoid CompilerDriver::RecordClassStatus(ClassReference ref, mirror::Class::Status status) {
21658f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers  MutexLock mu(Thread::Current(), compiled_classes_lock_);
21668f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers  auto it = compiled_classes_.find(ref);
21678f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers  if (it == compiled_classes_.end() || it->second->GetStatus() != status) {
21688f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers    // An entry doesn't exist or the status is lower than the new status.
21698f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers    if (it != compiled_classes_.end()) {
21708f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      CHECK_GT(status, it->second->GetStatus());
21718f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      delete it->second;
21728f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers    }
21738f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers    switch (status) {
21748f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      case mirror::Class::kStatusNotReady:
21758f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      case mirror::Class::kStatusError:
21768f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      case mirror::Class::kStatusRetryVerificationAtRuntime:
21778f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      case mirror::Class::kStatusVerified:
21788f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      case mirror::Class::kStatusInitialized:
21798f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        break;  // Expected states.
21808f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      default:
21818f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        LOG(FATAL) << "Unexpected class status for class "
21828f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers            << PrettyDescriptor(ref.first->GetClassDescriptor(ref.first->GetClassDef(ref.second)))
21838f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers            << " of " << status;
21848f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers    }
21858f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers    CompiledClass* compiled_class = new CompiledClass(status);
21868f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers    compiled_classes_.Overwrite(ref, compiled_class);
21878f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers  }
21888f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers}
21898f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers
21901212a022fa5f8ef9585d765b1809521812af882cIan RogersCompiledMethod* CompilerDriver::GetCompiledMethod(MethodReference ref) const {
219150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(Thread::Current(), compiled_methods_lock_);
21920571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  MethodTable::const_iterator it = compiled_methods_.find(ref);
21930571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  if (it == compiled_methods_.end()) {
219435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    return nullptr;
21952c8f653c98d658419f464b6147c10e11a664d2e6Ian Rogers  }
219635439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  CHECK(it->second != nullptr);
21973320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom  return it->second;
21989ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom}
21999ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
22001212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::AddRequiresConstructorBarrier(Thread* self, const DexFile* dex_file,
22018b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                                   uint16_t class_def_index) {
22028f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers  WriterMutexLock mu(self, freezing_constructor_lock_);
2203fffdb023275613612a22ec62b3421ffe4d2b73feIan Rogers  freezing_constructor_classes_.insert(ClassReference(dex_file, class_def_index));
2204fffdb023275613612a22ec62b3421ffe4d2b73feIan Rogers}
2205fffdb023275613612a22ec62b3421ffe4d2b73feIan Rogers
22061212a022fa5f8ef9585d765b1809521812af882cIan Rogersbool CompilerDriver::RequiresConstructorBarrier(Thread* self, const DexFile* dex_file,
22078b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                                uint16_t class_def_index) {
22088f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers  ReaderMutexLock mu(self, freezing_constructor_lock_);
2209fffdb023275613612a22ec62b3421ffe4d2b73feIan Rogers  return freezing_constructor_classes_.count(ClassReference(dex_file, class_def_index)) != 0;
2210fffdb023275613612a22ec62b3421ffe4d2b73feIan Rogers}
2211fffdb023275613612a22ec62b3421ffe4d2b73feIan Rogers
22123f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrombool CompilerDriver::WriteElf(const std::string& android_root,
2213265091e581c9f643b37e7966890911f09e223269Brian Carlstrom                              bool is_host,
221451c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom                              const std::vector<const art::DexFile*>& dex_files,
22153d504075f7c1204d581923460754bf6d3714b13fIan Rogers                              OatWriter* oat_writer,
221651c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom                              art::File* file)
221751c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
221872d32629303f8f39362a4099481f48646aed042fIan Rogers  return compiler_->WriteElf(file, oat_writer, dex_files, android_root, is_host);
2219265091e581c9f643b37e7966890911f09e223269Brian Carlstrom}
22201212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::InstructionSetToLLVMTarget(InstructionSet instruction_set,
22213d504075f7c1204d581923460754bf6d3714b13fIan Rogers                                                std::string* target_triple,
22223d504075f7c1204d581923460754bf6d3714b13fIan Rogers                                                std::string* target_cpu,
22233d504075f7c1204d581923460754bf6d3714b13fIan Rogers                                                std::string* target_attr) {
2224265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  switch (instruction_set) {
2225700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    case kThumb2:
22263d504075f7c1204d581923460754bf6d3714b13fIan Rogers      *target_triple = "thumb-none-linux-gnueabi";
22273d504075f7c1204d581923460754bf6d3714b13fIan Rogers      *target_cpu = "cortex-a9";
22283d504075f7c1204d581923460754bf6d3714b13fIan Rogers      *target_attr = "+thumb2,+neon,+neonfp,+vfp3,+db";
2229700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      break;
2230700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
2231700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    case kArm:
22323d504075f7c1204d581923460754bf6d3714b13fIan Rogers      *target_triple = "armv7-none-linux-gnueabi";
2233700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      // TODO: Fix for Nexus S.
22343d504075f7c1204d581923460754bf6d3714b13fIan Rogers      *target_cpu = "cortex-a9";
2235700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      // TODO: Fix for Xoom.
22363d504075f7c1204d581923460754bf6d3714b13fIan Rogers      *target_attr = "+v7,+neon,+neonfp,+vfp3,+db";
2237700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      break;
2238700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
2239700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    case kX86:
22403d504075f7c1204d581923460754bf6d3714b13fIan Rogers      *target_triple = "i386-pc-linux-gnu";
22413d504075f7c1204d581923460754bf6d3714b13fIan Rogers      *target_attr = "";
2242700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      break;
2243700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
22446a58cb16d803c9a7b3a75ccac8be19dd9d4e520dDmitry Petrochenko    case kX86_64:
22456a58cb16d803c9a7b3a75ccac8be19dd9d4e520dDmitry Petrochenko      *target_triple = "x86_64-pc-linux-gnu";
22466a58cb16d803c9a7b3a75ccac8be19dd9d4e520dDmitry Petrochenko      *target_attr = "";
22476a58cb16d803c9a7b3a75ccac8be19dd9d4e520dDmitry Petrochenko      break;
22486a58cb16d803c9a7b3a75ccac8be19dd9d4e520dDmitry Petrochenko
2249700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    case kMips:
22503d504075f7c1204d581923460754bf6d3714b13fIan Rogers      *target_triple = "mipsel-unknown-linux";
22513d504075f7c1204d581923460754bf6d3714b13fIan Rogers      *target_attr = "mips32r2";
2252700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      break;
2253700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
2254700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    default:
2255700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      LOG(FATAL) << "Unknown instruction set: " << instruction_set;
2256700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    }
2257700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  }
225839c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison
225939c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allisonbool CompilerDriver::SkipCompilation(const std::string& method_name) {
2260c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle  if (!profile_present_) {
2261644789fa0e807a9aa6d6e32d36ce039ec7318a7fDave Allison    return false;
226239c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison  }
2263bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle  // First find the method in the profile file.
2264bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle  ProfileFile::ProfileData data;
2265bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle  if (!profile_file_.GetProfileData(&data, method_name)) {
226639c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison    // Not in profile, no information can be determined.
226708f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle    if (kIsDebugBuild) {
226808f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle      VLOG(compiler) << "not compiling " << method_name << " because it's not in the profile";
226908f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle    }
227039c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison    return true;
227139c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison  }
2272bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle
2273bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle  // Methods that comprise top_k_threshold % of the total samples will be compiled.
2274f6a4cee66a173ee7ef48af5503d9899aa93b3aebCalin Juravle  // Compare against the start of the topK percentage bucket just in case the threshold
227504ff226764b125c79a0bf41e571c8311bac8a868Calin Juravle  // falls inside a bucket.
2276c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle  bool compile = data.GetTopKUsedPercentage() - data.GetUsedPercent()
2277c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle                 <= compiler_options_->GetTopKProfileThreshold();
227808f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle  if (kIsDebugBuild) {
227908f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle    if (compile) {
228008f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle      LOG(INFO) << "compiling method " << method_name << " because its usage is part of top "
228108f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle          << data.GetTopKUsedPercentage() << "% with a percent of " << data.GetUsedPercent() << "%"
228208f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle          << " (topKThreshold=" << compiler_options_->GetTopKProfileThreshold() << ")";
228308f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle    } else {
228408f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle      VLOG(compiler) << "not compiling method " << method_name
228508f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle          << " because it's not part of leading " << compiler_options_->GetTopKProfileThreshold()
228608f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle          << "% samples)";
228708f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle    }
228839c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison  }
228939c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison  return !compile;
229039c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison}
229125fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier
2292aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampestd::string CompilerDriver::GetMemoryUsageString(bool extended) const {
229325fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  std::ostringstream oss;
229425fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  const ArenaPool* arena_pool = GetArenaPool();
229525fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  gc::Heap* heap = Runtime::Current()->GetHeap();
229625fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  oss << "arena alloc=" << PrettySize(arena_pool->GetBytesAllocated());
229725fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  oss << " java alloc=" << PrettySize(heap->GetBytesAllocated());
229825fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier#ifdef HAVE_MALLOC_H
229925fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  struct mallinfo info = mallinfo();
230025fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  const size_t allocated_space = static_cast<size_t>(info.uordblks);
230125fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  const size_t free_space = static_cast<size_t>(info.fordblks);
230225fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  oss << " native alloc=" << PrettySize(allocated_space) << " free="
230325fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier      << PrettySize(free_space);
230425fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier#endif
230562746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe  if (swap_space_.get() != nullptr) {
230662746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe    oss << " swap=" << PrettySize(swap_space_->GetSize());
230762746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe  }
2308aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe  if (extended) {
2309aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe    oss << "\nCode dedupe: " << dedupe_code_.DumpStats();
2310aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe    oss << "\nMapping table dedupe: " << dedupe_mapping_table_.DumpStats();
2311aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe    oss << "\nVmap table dedupe: " << dedupe_vmap_table_.DumpStats();
2312aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe    oss << "\nGC map dedupe: " << dedupe_gc_map_.DumpStats();
2313aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe    oss << "\nCFI info dedupe: " << dedupe_cfi_info_.DumpStats();
2314aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe  }
231525fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  return oss.str();
231625fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier}
231725fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier
23189ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom}  // namespace art
2319