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  }
11872397ad72daff0e15a64281bf4b0dd8065551d22fJeff Hao  mirror::Class* declaring_class = method->GetDeclaringClass();
11882397ad72daff0e15a64281bf4b0dd8065551d22fJeff Hao  bool method_code_in_boot = (declaring_class->GetClassLoader() == nullptr);
118983883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers  if (!use_dex_cache) {
119083883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers    if (!method_code_in_boot) {
119183883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      use_dex_cache = true;
119283883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers    } else {
119383883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      bool has_clinit_trampoline =
11942397ad72daff0e15a64281bf4b0dd8065551d22fJeff Hao          method->IsStatic() && !declaring_class->IsInitialized();
11952397ad72daff0e15a64281bf4b0dd8065551d22fJeff Hao      if (has_clinit_trampoline && (declaring_class != referrer_class)) {
119683883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers        // Ensure we run the clinit trampoline unless we are invoking a static method in the same
119783883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers        // class.
119883883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers        use_dex_cache = true;
119983883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      }
120083883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers    }
12012ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers  }
1202f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  if (method_code_in_boot) {
1203f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    *stats_flags |= kFlagDirectCallToBoot | kFlagDirectMethodToBoot;
12042ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers  }
1205a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  if (!use_dex_cache && force_relocations) {
12062397ad72daff0e15a64281bf4b0dd8065551d22fJeff Hao    bool is_in_image;
12072397ad72daff0e15a64281bf4b0dd8065551d22fJeff Hao    if (IsImage()) {
12082397ad72daff0e15a64281bf4b0dd8065551d22fJeff Hao      is_in_image = IsImageClass(method->GetDeclaringClassDescriptor());
12092397ad72daff0e15a64281bf4b0dd8065551d22fJeff Hao    } else {
12102397ad72daff0e15a64281bf4b0dd8065551d22fJeff Hao      is_in_image = instruction_set_ != kX86 && instruction_set_ != kX86_64 &&
12112397ad72daff0e15a64281bf4b0dd8065551d22fJeff Hao                    Runtime::Current()->GetHeap()->FindSpaceFromObject(declaring_class,
12122397ad72daff0e15a64281bf4b0dd8065551d22fJeff Hao                                                                       false)->IsImageSpace();
12132397ad72daff0e15a64281bf4b0dd8065551d22fJeff Hao    }
12142397ad72daff0e15a64281bf4b0dd8065551d22fJeff Hao    if (!is_in_image) {
121583883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      // We can only branch directly to Methods that are resolved in the DexCache.
121683883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      // Otherwise we won't invoke the resolution trampoline.
121783883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      use_dex_cache = true;
1218e3cd2f0e3c3d976ae9c65c8a731003a5aaf71986Ian Rogers    }
1219c468e92d1e18305a5053c8a4a7c88cb297e525afIan Rogers  }
122083883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers  // The method is defined not within this dex file. We need a dex cache slot within the current
122183883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers  // dex file or direct pointers.
122283883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers  bool must_use_direct_pointers = false;
12232397ad72daff0e15a64281bf4b0dd8065551d22fJeff Hao  if (target_method->dex_file == declaring_class->GetDexCache()->GetDexFile()) {
122483883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers    target_method->dex_method_index = method->GetDexMethodIndex();
122583883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers  } else {
122683883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers    if (no_guarantee_of_dex_cache_entry) {
1227bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      StackHandleScope<1> hs(Thread::Current());
1228bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      MethodHelper mh(hs.NewHandle(method));
122983883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      // See if the method is also declared in this dex cache.
1230bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      uint32_t dex_method_idx = mh.FindDexMethodIndexInOtherDexFile(
1231bbcc0c0a17262f3d2a70fc0a82e1783862f708ccVladimir Marko          *target_method->dex_file, target_method->dex_method_index);
123283883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      if (dex_method_idx != DexFile::kDexNoIndex) {
123383883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers        target_method->dex_method_index = dex_method_idx;
123483883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      } else {
1235a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light        if (force_relocations && !use_dex_cache) {
123649161cef10a308aedada18e9aa742498d6e6c8c7Jeff Hao          target_method->dex_method_index = method->GetDexMethodIndex();
12372397ad72daff0e15a64281bf4b0dd8065551d22fJeff Hao          target_method->dex_file = declaring_class->GetDexCache()->GetDexFile();
123849161cef10a308aedada18e9aa742498d6e6c8c7Jeff Hao        }
123983883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers        must_use_direct_pointers = true;
12403fa13791c51985d9956d01bc465de6d36c3390d3Ian Rogers      }
12413fa13791c51985d9956d01bc465de6d36c3390d3Ian Rogers    }
124283883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers  }
124383883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers  if (use_dex_cache) {
124483883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers    if (must_use_direct_pointers) {
124583883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      // Fail. Test above showed the only safe dispatch was via the dex cache, however, the direct
124683883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      // pointers are required as the dex cache lacks an appropriate entry.
124783883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      VLOG(compiler) << "Dex cache devirtualization failed for: " << PrettyMethod(method);
124883883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers    } else {
124983883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      *type = sharp_type;
125083883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers    }
12513fa13791c51985d9956d01bc465de6d36c3390d3Ian Rogers  } else {
1252a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    bool method_in_image =
1253a51a0b0300268b605e3ad71b0e87ff394032c5e7Vladimir Marko        Runtime::Current()->GetHeap()->FindSpaceFromObject(method, false)->IsImageSpace();
1254a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    if (method_in_image || compiling_boot) {
1255a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      // We know we must be able to get to the method in the image, so use that pointer.
1256a51a0b0300268b605e3ad71b0e87ff394032c5e7Vladimir Marko      CHECK(!method->IsAbstract());
125783883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers      *type = sharp_type;
1258a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      *direct_method = force_relocations ? -1 : reinterpret_cast<uintptr_t>(method);
1259a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      *direct_code = force_relocations ? -1 : compiler_->GetEntryPointOf(method);
12602397ad72daff0e15a64281bf4b0dd8065551d22fJeff Hao      target_method->dex_file = declaring_class->GetDexCache()->GetDexFile();
1261a51a0b0300268b605e3ad71b0e87ff394032c5e7Vladimir Marko      target_method->dex_method_index = method->GetDexMethodIndex();
1262a51a0b0300268b605e3ad71b0e87ff394032c5e7Vladimir Marko    } else if (!must_use_direct_pointers) {
1263a51a0b0300268b605e3ad71b0e87ff394032c5e7Vladimir Marko      // Set the code and rely on the dex cache for the method.
1264a51a0b0300268b605e3ad71b0e87ff394032c5e7Vladimir Marko      *type = sharp_type;
1265a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      if (force_relocations) {
1266a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light        *direct_code = -1;
12672397ad72daff0e15a64281bf4b0dd8065551d22fJeff Hao        target_method->dex_file = declaring_class->GetDexCache()->GetDexFile();
1268a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light        target_method->dex_method_index = method->GetDexMethodIndex();
1269a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      } else {
1270a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light        *direct_code = compiler_->GetEntryPointOf(method);
1271a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      }
127283883d7fddf30fdb8b6903560fa1337ab991e74cIan Rogers    } else {
1273a51a0b0300268b605e3ad71b0e87ff394032c5e7Vladimir Marko      // Direct pointers were required but none were available.
1274a51a0b0300268b605e3ad71b0e87ff394032c5e7Vladimir Marko      VLOG(compiler) << "Dex cache devirtualization failed for: " << PrettyMethod(method);
12753fa13791c51985d9956d01bc465de6d36c3390d3Ian Rogers    }
12762ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers  }
12772ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers}
12782ed3b9536ccfd7c7321cc18650820b093b22d6c9Ian Rogers
1279e3cd2f0e3c3d976ae9c65c8a731003a5aaf71986Ian Rogersbool CompilerDriver::ComputeInvokeInfo(const DexCompilationUnit* mUnit, const uint32_t dex_pc,
128065ec92cf13c9d11c83711443a02e4249163d47f1Ian Rogers                                       bool update_stats, bool enable_devirtualization,
128165ec92cf13c9d11c83711443a02e4249163d47f1Ian Rogers                                       InvokeType* invoke_type, MethodReference* target_method,
128265ec92cf13c9d11c83711443a02e4249163d47f1Ian Rogers                                       int* vtable_idx, uintptr_t* direct_code,
128365ec92cf13c9d11c83711443a02e4249163d47f1Ian Rogers                                       uintptr_t* direct_method) {
1284f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  InvokeType orig_invoke_type = *invoke_type;
1285f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  int stats_flags = 0;
128600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
1287f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  // Try to resolve the method and compiling method's class.
1288f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  mirror::ArtMethod* resolved_method;
1289f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  mirror::Class* referrer_class;
1290eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  StackHandleScope<3> hs(soa.Self());
1291eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::DexCache> dex_cache(
1292eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      hs.NewHandle(mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile())));
1293eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1294eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader())));
1295f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  {
1296f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    uint32_t method_idx = target_method->dex_method_index;
1297eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    Handle<mirror::ArtMethod> resolved_method_handle(hs.NewHandle(
1298eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        ResolveMethod(soa, dex_cache, class_loader, mUnit, method_idx, orig_invoke_type)));
1299eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    referrer_class = (resolved_method_handle.Get() != nullptr)
1300f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko        ? ResolveCompilingMethodsClass(soa, dex_cache, class_loader, mUnit) : nullptr;
1301eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    resolved_method = resolved_method_handle.Get();
1302f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  }
1303f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  bool result = false;
1304f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  if (resolved_method != nullptr) {
1305f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    *vtable_idx = GetResolvedMethodVTableIndex(resolved_method, orig_invoke_type);
1306f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko
1307f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    if (enable_devirtualization) {
1308f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      DCHECK(mUnit->GetVerifiedMethod() != nullptr);
1309f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      const MethodReference* devirt_target = mUnit->GetVerifiedMethod()->GetDevirtTarget(dex_pc);
1310f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko
1311f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      stats_flags = IsFastInvoke(
1312f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko          soa, dex_cache, class_loader, mUnit, referrer_class, resolved_method,
1313f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko          invoke_type, target_method, devirt_target, direct_code, direct_method);
1314f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      result = stats_flags != 0;
1315f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    } else {
1316f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      // Devirtualization not enabled. Inline IsFastInvoke(), dropping the devirtualization parts.
1317f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      if (UNLIKELY(referrer_class == nullptr) ||
1318f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko          UNLIKELY(!referrer_class->CanAccessResolvedMethod(resolved_method->GetDeclaringClass(),
1319eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier                                                            resolved_method, dex_cache.Get(),
1320f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko                                                            target_method->dex_method_index)) ||
1321f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko          *invoke_type == kSuper) {
1322f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko        // Slow path. (Without devirtualization, all super calls go slow path as well.)
1323f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko      } else {
1324f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko        // Sharpening failed so generate a regular resolved method dispatch.
1325f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko        stats_flags = kFlagMethodResolved;
1326f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko        GetCodeAndMethodForDirectCall(invoke_type, *invoke_type, false, referrer_class, resolved_method,
1327f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko                                      &stats_flags, target_method, direct_code, direct_method);
1328f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko        result = true;
1329a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers      }
1330a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers    }
1331a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers  }
1332f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  if (!result) {
1333f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    // Conservative defaults.
1334f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    *vtable_idx = -1;
1335f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    *direct_code = 0u;
1336f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    *direct_method = 0u;
1337a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers  }
1338e3cd2f0e3c3d976ae9c65c8a731003a5aaf71986Ian Rogers  if (update_stats) {
1339f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko    ProcessedInvoke(orig_invoke_type, stats_flags);
1340e3cd2f0e3c3d976ae9c65c8a731003a5aaf71986Ian Rogers  }
1341f096aad9203d7c50b2f9cbe1c1215a50c265a059Vladimir Marko  return result;
1342a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers}
1343a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers
13442730db03beee4d6687ddfb5000c33c0370fbc6ebVladimir Markoconst VerifiedMethod* CompilerDriver::GetVerifiedMethod(const DexFile* dex_file,
13452730db03beee4d6687ddfb5000c33c0370fbc6ebVladimir Marko                                                        uint32_t method_idx) const {
13462730db03beee4d6687ddfb5000c33c0370fbc6ebVladimir Marko  MethodReference ref(dex_file, method_idx);
13472730db03beee4d6687ddfb5000c33c0370fbc6ebVladimir Marko  return verification_results_->GetVerifiedMethod(ref);
13482730db03beee4d6687ddfb5000c33c0370fbc6ebVladimir Marko}
13492730db03beee4d6687ddfb5000c33c0370fbc6ebVladimir Marko
13502730db03beee4d6687ddfb5000c33c0370fbc6ebVladimir Markobool CompilerDriver::IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc) {
135135439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (!compiler_options_->IsVerificationEnabled()) {
135235439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    // If we didn't verify, every cast has to be treated as non-safe.
135335439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    return false;
135435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  }
13552730db03beee4d6687ddfb5000c33c0370fbc6ebVladimir Marko  DCHECK(mUnit->GetVerifiedMethod() != nullptr);
13562730db03beee4d6687ddfb5000c33c0370fbc6ebVladimir Marko  bool result = mUnit->GetVerifiedMethod()->IsSafeCast(dex_pc);
1357fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers  if (result) {
1358fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers    stats_->SafeCast();
1359fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers  } else {
1360fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers    stats_->NotASafeCast();
1361fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers  }
1362fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers  return result;
1363fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers}
1364fae370a044f5817f69937cccfd2d12a16b374266Ian Rogers
13651212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::AddCodePatch(const DexFile* dex_file,
13668b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                  uint16_t referrer_class_def_idx,
13678b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                  uint32_t referrer_method_idx,
13688b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                  InvokeType referrer_invoke_type,
13698b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                  uint32_t target_method_idx,
137049161cef10a308aedada18e9aa742498d6e6c8c7Jeff Hao                                  const DexFile* target_dex_file,
13718b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                  InvokeType target_invoke_type,
13728b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                  size_t literal_offset) {
137350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(Thread::Current(), compiled_methods_lock_);
1374be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi  code_to_patch_.push_back(new CallPatchInformation(dex_file,
1375be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                    referrer_class_def_idx,
1376be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                    referrer_method_idx,
1377be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                    referrer_invoke_type,
1378be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                    target_method_idx,
137949161cef10a308aedada18e9aa742498d6e6c8c7Jeff Hao                                                    target_dex_file,
1380be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                    target_invoke_type,
1381be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                    literal_offset));
13823fa13791c51985d9956d01bc465de6d36c3390d3Ian Rogers}
138355d0eac918321e0525f6e6491f36a80977e0d416Mark Mendellvoid CompilerDriver::AddRelativeCodePatch(const DexFile* dex_file,
138455d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                          uint16_t referrer_class_def_idx,
138555d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                          uint32_t referrer_method_idx,
138655d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                          InvokeType referrer_invoke_type,
138755d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                          uint32_t target_method_idx,
138849161cef10a308aedada18e9aa742498d6e6c8c7Jeff Hao                                          const DexFile* target_dex_file,
138955d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                          InvokeType target_invoke_type,
139055d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                          size_t literal_offset,
139155d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                          int32_t pc_relative_offset) {
139255d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell  MutexLock mu(Thread::Current(), compiled_methods_lock_);
139355d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell  code_to_patch_.push_back(new RelativeCallPatchInformation(dex_file,
139455d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                                            referrer_class_def_idx,
139555d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                                            referrer_method_idx,
139655d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                                            referrer_invoke_type,
139755d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                                            target_method_idx,
139849161cef10a308aedada18e9aa742498d6e6c8c7Jeff Hao                                                            target_dex_file,
139955d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                                            target_invoke_type,
140055d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                                            literal_offset,
140155d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell                                                            pc_relative_offset));
140255d0eac918321e0525f6e6491f36a80977e0d416Mark Mendell}
14031212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::AddMethodPatch(const DexFile* dex_file,
14048b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                    uint16_t referrer_class_def_idx,
14058b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                    uint32_t referrer_method_idx,
14068b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                    InvokeType referrer_invoke_type,
14078b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                    uint32_t target_method_idx,
140849161cef10a308aedada18e9aa742498d6e6c8c7Jeff Hao                                    const DexFile* target_dex_file,
14098b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                    InvokeType target_invoke_type,
14108b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                    size_t literal_offset) {
141150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(Thread::Current(), compiled_methods_lock_);
1412be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi  methods_to_patch_.push_back(new CallPatchInformation(dex_file,
1413be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                       referrer_class_def_idx,
1414be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                       referrer_method_idx,
1415be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                       referrer_invoke_type,
1416be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                       target_method_idx,
141749161cef10a308aedada18e9aa742498d6e6c8c7Jeff Hao                                                       target_dex_file,
1418be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                       target_invoke_type,
1419be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                       literal_offset));
1420be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi}
1421be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchivoid CompilerDriver::AddClassPatch(const DexFile* dex_file,
1422be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                    uint16_t referrer_class_def_idx,
1423be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                    uint32_t referrer_method_idx,
1424be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                    uint32_t target_type_idx,
1425be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                    size_t literal_offset) {
1426be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi  MutexLock mu(Thread::Current(), compiled_methods_lock_);
1427be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi  classes_to_patch_.push_back(new TypePatchInformation(dex_file,
1428be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                       referrer_class_def_idx,
1429be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                       referrer_method_idx,
1430be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                       target_type_idx,
1431be1ca55db3362f5b100c4c65da5342fd299520bbHiroshi Yamauchi                                                       literal_offset));
14323fa13791c51985d9956d01bc465de6d36c3390d3Ian Rogers}
1433807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartiervoid CompilerDriver::AddStringPatch(const DexFile* dex_file,
1434807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier                                    uint16_t referrer_class_def_idx,
1435807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier                                    uint32_t referrer_method_idx,
1436807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier                                    uint32_t string_idx,
1437807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier                                    size_t literal_offset) {
1438807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier  MutexLock mu(Thread::Current(), compiled_methods_lock_);
1439807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier  strings_to_patch_.push_back(new StringPatchInformation(dex_file,
1440807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier                                                         referrer_class_def_idx,
1441807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier                                                         referrer_method_idx,
1442807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier                                                         string_idx,
1443807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier                                                         literal_offset));
1444807140048f82a2b87ee5bcf337f23b6a3d1d5269Mathieu Chartier}
14453fa13791c51985d9956d01bc465de6d36c3390d3Ian Rogers
1446219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogersclass ParallelCompilationManager {
1447731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom public:
1448219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers  typedef void Callback(const ParallelCompilationManager* manager, size_t index);
14490e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier
1450219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers  ParallelCompilationManager(ClassLinker* class_linker,
1451219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers                             jobject class_loader,
1452219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers                             CompilerDriver* compiler,
1453219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers                             const DexFile* dex_file,
14540171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe                             const std::vector<const DexFile*>& dex_files,
14553d504075f7c1204d581923460754bf6d3714b13fIan Rogers                             ThreadPool* thread_pool)
14560b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier    : index_(0),
14570b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier      class_linker_(class_linker),
1458731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom      class_loader_(class_loader),
1459731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom      compiler_(compiler),
14600e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier      dex_file_(dex_file),
14610171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe      dex_files_(dex_files),
14623d504075f7c1204d581923460754bf6d3714b13fIan Rogers      thread_pool_(thread_pool) {}
1463731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom
146400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ClassLinker* GetClassLinker() const {
146535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    CHECK(class_linker_ != nullptr);
1466731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom    return class_linker_;
1467731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom  }
146800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
146900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  jobject GetClassLoader() const {
1470731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom    return class_loader_;
1471731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom  }
147200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
14731212a022fa5f8ef9585d765b1809521812af882cIan Rogers  CompilerDriver* GetCompiler() const {
147435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    CHECK(compiler_ != nullptr);
1475731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom    return compiler_;
1476731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom  }
147700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
147800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  const DexFile* GetDexFile() const {
147935439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    CHECK(dex_file_ != nullptr);
1480731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom    return dex_file_;
1481731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom  }
1482731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom
14830171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe  const std::vector<const DexFile*>& GetDexFiles() const {
14840171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe    return dex_files_;
14850171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe  }
14860171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe
14870e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier  void ForAll(size_t begin, size_t end, Callback callback, size_t work_units) {
14880e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    Thread* self = Thread::Current();
14890e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    self->AssertNoPendingException();
14900e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    CHECK_GT(work_units, 0U);
14919ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
14923e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers    index_.StoreRelaxed(begin);
14930e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    for (size_t i = 0; i < work_units; ++i) {
1494501baec5f2de2156bb5ed7f66d23f1b1ad026267Sebastien Hertz      thread_pool_->AddTask(self, new ForAllClosure(this, end, callback));
14950e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    }
14960e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    thread_pool_->StartWorkers(self);
149700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
14980e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    // Ensure we're suspended while we're blocked waiting for the other threads to finish (worker
14990e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    // thread destructor's called below perform join).
15000e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    CHECK_NE(self->GetState(), kRunnable);
1501d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
15020e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    // Wait for all the worker threads to finish.
15031d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    thread_pool_->Wait(self, true, false);
1504d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes  }
1505d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
15060b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier  size_t NextIndex() {
15073e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers    return index_.FetchAndAddSequentiallyConsistent(1);
15080b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier  }
15090b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier
1510d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes private:
151102b6a78038f12c109f95eb31713cfc747f5512f1Mathieu Chartier  class ForAllClosure : public Task {
15120e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier   public:
15130b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier    ForAllClosure(ParallelCompilationManager* manager, size_t end, Callback* callback)
1514219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers        : manager_(manager),
15150e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier          end_(end),
15160b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier          callback_(callback) {}
15179ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
15180e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    virtual void Run(Thread* self) {
15190b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier      while (true) {
15200b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier        const size_t index = manager_->NextIndex();
15210b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier        if (UNLIKELY(index >= end_)) {
15220b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier          break;
15230b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier        }
15240b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier        callback_(manager_, index);
15250e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier        self->AssertNoPendingException();
15260e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier      }
15270e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    }
152802b6a78038f12c109f95eb31713cfc747f5512f1Mathieu Chartier
152902b6a78038f12c109f95eb31713cfc747f5512f1Mathieu Chartier    virtual void Finalize() {
153002b6a78038f12c109f95eb31713cfc747f5512f1Mathieu Chartier      delete this;
153102b6a78038f12c109f95eb31713cfc747f5512f1Mathieu Chartier    }
15320cd7ec2dcd8d7ba30bf3ca420b40dac52849876cBrian Carlstrom
15330e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier   private:
15340b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier    ParallelCompilationManager* const manager_;
15350e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier    const size_t end_;
1536460536209b741bc469f1b0857775449abb2102fbBernhard Rosenkränzer    Callback* const callback_;
15370e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier  };
15381e4092589f1400915e6213014da103aab8728ef6Elliott Hughes
15390b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier  AtomicInteger index_;
15400e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier  ClassLinker* const class_linker_;
15410e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier  const jobject class_loader_;
15421212a022fa5f8ef9585d765b1809521812af882cIan Rogers  CompilerDriver* const compiler_;
15430e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier  const DexFile* const dex_file_;
15440171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe  const std::vector<const DexFile*>& dex_files_;
1545219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers  ThreadPool* const thread_pool_;
15460b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier
15470b3eb39f8bc92f955f875ef50929c439aafe35fbMathieu Chartier  DISALLOW_COPY_AND_ASSIGN(ParallelCompilationManager);
1548d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes};
1549d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
15500e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao// A fast version of SkipClass above if the class pointer is available
15510e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao// that avoids the expensive FindInClassPath search.
15520e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Haostatic bool SkipClass(jobject class_loader, const DexFile& dex_file, mirror::Class* klass)
15530e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
155435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  DCHECK(klass != nullptr);
15550e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  const DexFile& original_dex_file = *klass->GetDexCache()->GetDexFile();
15560e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  if (&dex_file != &original_dex_file) {
155735439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    if (class_loader == nullptr) {
15580e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao      LOG(WARNING) << "Skipping class " << PrettyDescriptor(klass) << " from "
15590e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao                   << dex_file.GetLocation() << " previously found in "
15600e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao                   << original_dex_file.GetLocation();
15610e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao    }
15620e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao    return true;
15630e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  }
15640e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  return false;
15650e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao}
15660e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao
156770b634882b523ec9795475dccc347b423cefaef6Mathieu Chartierstatic void CheckAndClearResolveException(Thread* self)
156870b634882b523ec9795475dccc347b423cefaef6Mathieu Chartier    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
156970b634882b523ec9795475dccc347b423cefaef6Mathieu Chartier  CHECK(self->IsExceptionPending());
157070b634882b523ec9795475dccc347b423cefaef6Mathieu Chartier  mirror::Throwable* exception = self->GetException(nullptr);
1571cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  std::string temp;
1572cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  const char* descriptor = exception->GetClass()->GetDescriptor(&temp);
1573cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  const char* expected_exceptions[] = {
1574cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      "Ljava/lang/IllegalAccessError;",
1575cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      "Ljava/lang/IncompatibleClassChangeError;",
1576cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      "Ljava/lang/InstantiationError;",
1577118785c6f82ac1ec7074b5efe367b3dafd904940Brian Carlstrom      "Ljava/lang/LinkageError;",
1578cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      "Ljava/lang/NoClassDefFoundError;",
1579cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      "Ljava/lang/NoSuchFieldError;",
1580cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      "Ljava/lang/NoSuchMethodError;"
1581cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  };
1582cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  bool found = false;
1583cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  for (size_t i = 0; (found == false) && (i < arraysize(expected_exceptions)); ++i) {
1584cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    if (strcmp(descriptor, expected_exceptions[i]) == 0) {
1585cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      found = true;
1586cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    }
1587cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  }
1588cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  if (!found) {
1589118785c6f82ac1ec7074b5efe367b3dafd904940Brian Carlstrom    LOG(FATAL) << "Unexpected exception " << exception->Dump();
159070b634882b523ec9795475dccc347b423cefaef6Mathieu Chartier  }
159170b634882b523ec9795475dccc347b423cefaef6Mathieu Chartier  self->ClearException();
159270b634882b523ec9795475dccc347b423cefaef6Mathieu Chartier}
159370b634882b523ec9795475dccc347b423cefaef6Mathieu Chartier
1594e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogersstatic void ResolveClassFieldsAndMethods(const ParallelCompilationManager* manager,
1595e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers                                         size_t class_def_index)
1596b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    LOCKS_EXCLUDED(Locks::mutator_lock_) {
15978f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers  ATRACE_CALL();
1598be7149fc2e7cc607937209f2819e3c1d672e2668Ian Rogers  Thread* self = Thread::Current();
1599be7149fc2e7cc607937209f2819e3c1d672e2668Ian Rogers  jobject jclass_loader = manager->GetClassLoader();
1600219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers  const DexFile& dex_file = *manager->GetDexFile();
1601be7149fc2e7cc607937209f2819e3c1d672e2668Ian Rogers  ClassLinker* class_linker = manager->GetClassLinker();
1602d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
1603e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers  // If an instance field is final then we need to have a barrier on the return, static final
1604e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers  // fields are assigned within the lock held for class initialization. Conservatively assume
1605e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers  // constructor barriers are always required.
1606e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers  bool requires_constructor_barrier = true;
1607e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers
1608845490bda68f7d025ea7f45775c847d2932e00dcBrian Carlstrom  // Method and Field are the worst. We can't resolve without either
1609845490bda68f7d025ea7f45775c847d2932e00dcBrian Carlstrom  // context from the code use (to disambiguate virtual vs direct
1610845490bda68f7d025ea7f45775c847d2932e00dcBrian Carlstrom  // method and instance vs static field) or from class
1611845490bda68f7d025ea7f45775c847d2932e00dcBrian Carlstrom  // definitions. While the compiler will resolve what it can as it
1612845490bda68f7d025ea7f45775c847d2932e00dcBrian Carlstrom  // needs it, here we try to resolve fields and methods used in class
1613845490bda68f7d025ea7f45775c847d2932e00dcBrian Carlstrom  // definitions, since many of them many never be referenced by
1614845490bda68f7d025ea7f45775c847d2932e00dcBrian Carlstrom  // generated code.
1615d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes  const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1616cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  ScopedObjectAccess soa(self);
1617cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  StackHandleScope<2> hs(soa.Self());
1618cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  Handle<mirror::ClassLoader> class_loader(
1619cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
1620cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(dex_file)));
1621cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  // Resolve the class.
1622cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  mirror::Class* klass = class_linker->ResolveType(dex_file, class_def.class_idx_, dex_cache,
1623cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers                                                   class_loader);
1624cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  bool resolve_fields_and_methods;
1625cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  if (klass == nullptr) {
1626cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    // Class couldn't be resolved, for example, super-class is in a different dex file. Don't
1627cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    // attempt to resolve methods and fields when there is no declaring class.
1628cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    CheckAndClearResolveException(soa.Self());
1629cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    resolve_fields_and_methods = false;
1630cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  } else {
1631cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    // We successfully resolved a class, should we skip it?
1632cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    if (SkipClass(jclass_loader, dex_file, klass)) {
1633cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      return;
1634cb5f5e53b580023fa2c1d8235c2e9aa1ff67d1dcBrian Carlstrom    }
1635cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    // We want to resolve the methods and fields eagerly.
1636cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    resolve_fields_and_methods = true;
1637cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  }
1638cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  // Note the class_data pointer advances through the headers,
1639cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  // static fields, instance fields, direct methods, and virtual
1640cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  // methods.
1641cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  const byte* class_data = dex_file.GetClassData(class_def);
164235439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (class_data == nullptr) {
1643cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    // Empty class such as a marker interface.
1644cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    requires_constructor_barrier = false;
1645cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers  } else {
1646cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    ClassDataItemIterator it(dex_file, class_data);
1647cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    while (it.HasNextStaticField()) {
1648cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      if (resolve_fields_and_methods) {
1649cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers        mirror::ArtField* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(),
1650cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers                                                             dex_cache, class_loader, true);
165135439baf287b291b67ee406308e17fc6194facbfAndreas Gampe        if (field == nullptr) {
1652cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers          CheckAndClearResolveException(soa.Self());
1653e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers        }
1654e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers      }
1655cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      it.Next();
1656cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    }
1657cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    // We require a constructor barrier if there are final instance fields.
1658cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    requires_constructor_barrier = false;
1659cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    while (it.HasNextInstanceField()) {
16607fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      if (it.MemberIsFinal()) {
1661cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers        requires_constructor_barrier = true;
1662cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      }
1663cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      if (resolve_fields_and_methods) {
1664cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers        mirror::ArtField* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(),
1665cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers                                                             dex_cache, class_loader, false);
166635439baf287b291b67ee406308e17fc6194facbfAndreas Gampe        if (field == nullptr) {
1667cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers          CheckAndClearResolveException(soa.Self());
1668e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers        }
1669cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      }
1670cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      it.Next();
1671cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    }
1672cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers    if (resolve_fields_and_methods) {
1673cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      while (it.HasNextDirectMethod()) {
1674cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers        mirror::ArtMethod* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(),
1675cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers                                                                dex_cache, class_loader,
1676cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers                                                                NullHandle<mirror::ArtMethod>(),
1677cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers                                                                it.GetMethodInvokeType(class_def));
167835439baf287b291b67ee406308e17fc6194facbfAndreas Gampe        if (method == nullptr) {
1679cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers          CheckAndClearResolveException(soa.Self());
1680e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers        }
1681e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers        it.Next();
1682e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers      }
1683cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      while (it.HasNextVirtualMethod()) {
1684cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers        mirror::ArtMethod* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(),
1685cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers                                                                dex_cache, class_loader,
1686cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers                                                                NullHandle<mirror::ArtMethod>(),
1687cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers                                                                it.GetMethodInvokeType(class_def));
168835439baf287b291b67ee406308e17fc6194facbfAndreas Gampe        if (method == nullptr) {
1689cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers          CheckAndClearResolveException(soa.Self());
1690e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers        }
1691cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers        it.Next();
1692e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers      }
1693cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogers      DCHECK(!it.HasNext());
169420cfffabdc9e02b2df798bc4e6b6035d14bf4e36Brian Carlstrom    }
1695d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes  }
1696fffdb023275613612a22ec62b3421ffe4d2b73feIan Rogers  if (requires_constructor_barrier) {
1697be7149fc2e7cc607937209f2819e3c1d672e2668Ian Rogers    manager->GetCompiler()->AddRequiresConstructorBarrier(self, &dex_file, class_def_index);
1698fffdb023275613612a22ec62b3421ffe4d2b73feIan Rogers  }
1699d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes}
1700d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
1701219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogersstatic void ResolveType(const ParallelCompilationManager* manager, size_t type_idx)
1702b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    LOCKS_EXCLUDED(Locks::mutator_lock_) {
1703d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes  // Class derived values are more complicated, they require the linker and loader.
170400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
1705219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers  ClassLinker* class_linker = manager->GetClassLinker();
1706219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers  const DexFile& dex_file = *manager->GetDexFile();
1707eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  StackHandleScope<2> hs(soa.Self());
1708eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(dex_file)));
1709eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::ClassLoader> class_loader(
1710eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      hs.NewHandle(soa.Decode<mirror::ClassLoader*>(manager->GetClassLoader())));
17112dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
171200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
171335439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (klass == nullptr) {
171400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    CHECK(soa.Self()->IsExceptionPending());
171535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    mirror::Throwable* exception = soa.Self()->GetException(nullptr);
1716a436fde2762664a3ecdda5eefcadd20b2e104f59Ian Rogers    VLOG(compiler) << "Exception during type resolution: " << exception->Dump();
1717f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier    if (exception->GetClass()->DescriptorEquals("Ljava/lang/OutOfMemoryError;")) {
1718a436fde2762664a3ecdda5eefcadd20b2e104f59Ian Rogers      // There's little point continuing compilation if the heap is exhausted.
1719a436fde2762664a3ecdda5eefcadd20b2e104f59Ian Rogers      LOG(FATAL) << "Out of memory during type resolution for compilation";
1720a436fde2762664a3ecdda5eefcadd20b2e104f59Ian Rogers    }
1721a436fde2762664a3ecdda5eefcadd20b2e104f59Ian Rogers    soa.Self()->ClearException();
1722d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes  }
1723d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes}
1724d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
17251212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::ResolveDexFile(jobject class_loader, const DexFile& dex_file,
17260171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe                                    const std::vector<const DexFile*>& dex_files,
17273d504075f7c1204d581923460754bf6d3714b13fIan Rogers                                    ThreadPool* thread_pool, TimingLogger* timings) {
1728d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1729d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
173000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // TODO: we could resolve strings here, although the string table is largely filled with class
173100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  //       and method names.
1732d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
17330171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe  ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, dex_files,
17340171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe                                     thread_pool);
1735e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers  if (IsImage()) {
1736e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers    // For images we resolve all types, such as array, whereas for applications just those with
1737e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers    // classdefs are resolved by ResolveClassFieldsAndMethods.
1738f5997b4d3f889569d5a2b724d83d764bfbb8d106Mathieu Chartier    TimingLogger::ScopedTiming t("Resolve Types", timings);
1739e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers    context.ForAll(0, dex_file.NumTypeIds(), ResolveType, thread_count_);
1740e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers  }
1741d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
1742f5997b4d3f889569d5a2b724d83d764bfbb8d106Mathieu Chartier  TimingLogger::ScopedTiming t("Resolve MethodsAndFields", timings);
17430e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier  context.ForAll(0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_);
17449ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom}
17459ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
174635439baf287b291b67ee406308e17fc6194facbfAndreas Gampevoid CompilerDriver::SetVerified(jobject class_loader, const std::vector<const DexFile*>& dex_files,
174735439baf287b291b67ee406308e17fc6194facbfAndreas Gampe                                 ThreadPool* thread_pool, TimingLogger* timings) {
174835439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  for (size_t i = 0; i != dex_files.size(); ++i) {
174935439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    const DexFile* dex_file = dex_files[i];
175035439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    CHECK(dex_file != nullptr);
175135439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    SetVerifiedDexFile(class_loader, *dex_file, dex_files, thread_pool, timings);
175235439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  }
175335439baf287b291b67ee406308e17fc6194facbfAndreas Gampe}
175435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe
17551212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::Verify(jobject class_loader, const std::vector<const DexFile*>& dex_files,
17563d504075f7c1204d581923460754bf6d3714b13fIan Rogers                            ThreadPool* thread_pool, TimingLogger* timings) {
1757ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  for (size_t i = 0; i != dex_files.size(); ++i) {
1758ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    const DexFile* dex_file = dex_files[i];
175935439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    CHECK(dex_file != nullptr);
17600171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe    VerifyDexFile(class_loader, *dex_file, dex_files, thread_pool, timings);
176198eacac683b78e60799323e8c7d59e7214808639jeffhao  }
176298eacac683b78e60799323e8c7d59e7214808639jeffhao}
176398eacac683b78e60799323e8c7d59e7214808639jeffhao
1764219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogersstatic void VerifyClass(const ParallelCompilationManager* manager, size_t class_def_index)
1765b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    LOCKS_EXCLUDED(Locks::mutator_lock_) {
176667f99418f648c3a95256ed3dcd8e8b64eef0b372Anwar Ghuloum  ATRACE_CALL();
176700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
17680e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  const DexFile& dex_file = *manager->GetDexFile();
17690e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
17700e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  const char* descriptor = dex_file.GetClassDescriptor(class_def);
17710e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  ClassLinker* class_linker = manager->GetClassLinker();
17720e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  jobject jclass_loader = manager->GetClassLoader();
1773eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  StackHandleScope<3> hs(soa.Self());
1774eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::ClassLoader> class_loader(
1775eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
1776eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::Class> klass(
1777eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor, class_loader)));
1778eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  if (klass.Get() == nullptr) {
1779e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers    CHECK(soa.Self()->IsExceptionPending());
178062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    soa.Self()->ClearException();
1781f56197c5519395fcf8226a7148cc87367e0a7ad4jeffhao
1782f56197c5519395fcf8226a7148cc87367e0a7ad4jeffhao    /*
1783f56197c5519395fcf8226a7148cc87367e0a7ad4jeffhao     * At compile time, we can still structurally verify the class even if FindClass fails.
1784f56197c5519395fcf8226a7148cc87367e0a7ad4jeffhao     * This is to ensure the class is structurally sound for compilation. An unsound class
1785f56197c5519395fcf8226a7148cc87367e0a7ad4jeffhao     * will be rejected by the verifier and later skipped during compilation in the compiler.
1786f56197c5519395fcf8226a7148cc87367e0a7ad4jeffhao     */
1787eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(dex_file)));
1788f56197c5519395fcf8226a7148cc87367e0a7ad4jeffhao    std::string error_msg;
1789590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    if (verifier::MethodVerifier::VerifyClass(&dex_file, dex_cache, class_loader, &class_def, true,
1790590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier                                              &error_msg) ==
179100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                                  verifier::MethodVerifier::kHardFailure) {
17920e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao      LOG(ERROR) << "Verification failed on class " << PrettyDescriptor(descriptor)
1793f56197c5519395fcf8226a7148cc87367e0a7ad4jeffhao                 << " because: " << error_msg;
1794f56197c5519395fcf8226a7148cc87367e0a7ad4jeffhao    }
1795eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  } else if (!SkipClass(jclass_loader, dex_file, klass.Get())) {
1796eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    CHECK(klass->IsResolved()) << PrettyClass(klass.Get());
17970e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao    class_linker->VerifyClass(klass);
1798d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
1799e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers    if (klass->IsErroneous()) {
1800e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers      // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
1801e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers      CHECK(soa.Self()->IsExceptionPending());
1802e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers      soa.Self()->ClearException();
1803e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers    }
180467f99418f648c3a95256ed3dcd8e8b64eef0b372Anwar Ghuloum
1805e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers    CHECK(klass->IsCompileTimeVerified() || klass->IsErroneous())
1806eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        << PrettyDescriptor(klass.Get()) << ": state=" << klass->GetStatus();
1807e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers  }
180862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  soa.Self()->AssertNoPendingException();
1809d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes}
1810d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes
18111212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::VerifyDexFile(jobject class_loader, const DexFile& dex_file,
18120171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe                                   const std::vector<const DexFile*>& dex_files,
18133d504075f7c1204d581923460754bf6d3714b13fIan Rogers                                   ThreadPool* thread_pool, TimingLogger* timings) {
1814f5997b4d3f889569d5a2b724d83d764bfbb8d106Mathieu Chartier  TimingLogger::ScopedTiming t("Verify Dex File", timings);
1815731b2abfccd8704d129e3b8e46a086660161fef3Brian Carlstrom  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
18160171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe  ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, dex_files,
18170171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe                                     thread_pool);
18180e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier  context.ForAll(0, dex_file.NumClassDefs(), VerifyClass, thread_count_);
1819a5a97a2bc1dfed70869da34650a5a2a3a3a06ac4Brian Carlstrom}
1820a5a97a2bc1dfed70869da34650a5a2a3a3a06ac4Brian Carlstrom
182135439baf287b291b67ee406308e17fc6194facbfAndreas Gampestatic void SetVerifiedClass(const ParallelCompilationManager* manager, size_t class_def_index)
182235439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    LOCKS_EXCLUDED(Locks::mutator_lock_) {
182335439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  ATRACE_CALL();
182435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  ScopedObjectAccess soa(Thread::Current());
182535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  const DexFile& dex_file = *manager->GetDexFile();
182635439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
182735439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  const char* descriptor = dex_file.GetClassDescriptor(class_def);
182835439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  ClassLinker* class_linker = manager->GetClassLinker();
182935439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  jobject jclass_loader = manager->GetClassLoader();
183035439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  StackHandleScope<3> hs(soa.Self());
183135439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  Handle<mirror::ClassLoader> class_loader(
183235439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
183335439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  Handle<mirror::Class> klass(
183435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor, class_loader)));
183535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  // Class might have failed resolution. Then don't set it to verified.
183635439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (klass.Get() != nullptr) {
183735439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    // Only do this if the class is resolved. If even resolution fails, quickening will go very,
183835439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    // very wrong.
183935439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    if (klass->IsResolved()) {
184035439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      if (klass->GetStatus() < mirror::Class::kStatusVerified) {
184135439baf287b291b67ee406308e17fc6194facbfAndreas Gampe        ObjectLock<mirror::Class> lock(soa.Self(), klass);
184235439baf287b291b67ee406308e17fc6194facbfAndreas Gampe        klass->SetStatus(mirror::Class::kStatusVerified, soa.Self());
184335439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      }
184435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      // Record the final class status if necessary.
184535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      ClassReference ref(manager->GetDexFile(), class_def_index);
184635439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      manager->GetCompiler()->RecordClassStatus(ref, klass->GetStatus());
184735439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    }
1848ba354a97a9742774f57bad7f96ccae2dbbd59f75Andreas Gampe  } else {
1849ba354a97a9742774f57bad7f96ccae2dbbd59f75Andreas Gampe    Thread* self = soa.Self();
1850ba354a97a9742774f57bad7f96ccae2dbbd59f75Andreas Gampe    DCHECK(self->IsExceptionPending());
1851ba354a97a9742774f57bad7f96ccae2dbbd59f75Andreas Gampe    self->ClearException();
185235439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  }
185335439baf287b291b67ee406308e17fc6194facbfAndreas Gampe}
185435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe
185535439baf287b291b67ee406308e17fc6194facbfAndreas Gampevoid CompilerDriver::SetVerifiedDexFile(jobject class_loader, const DexFile& dex_file,
185635439baf287b291b67ee406308e17fc6194facbfAndreas Gampe                                        const std::vector<const DexFile*>& dex_files,
185735439baf287b291b67ee406308e17fc6194facbfAndreas Gampe                                        ThreadPool* thread_pool, TimingLogger* timings) {
185835439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  TimingLogger::ScopedTiming t("Verify Dex File", timings);
185935439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
186035439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, dex_files,
186135439baf287b291b67ee406308e17fc6194facbfAndreas Gampe                                     thread_pool);
186235439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  context.ForAll(0, dex_file.NumClassDefs(), SetVerifiedClass, thread_count_);
186335439baf287b291b67ee406308e17fc6194facbfAndreas Gampe}
186435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe
1865219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogersstatic void InitializeClass(const ParallelCompilationManager* manager, size_t class_def_index)
18663d1548debdaf8a1915fa432a4d267f7c0145654dIan Rogers    LOCKS_EXCLUDED(Locks::mutator_lock_) {
18678f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers  ATRACE_CALL();
18680e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  jobject jclass_loader = manager->GetClassLoader();
18690e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  const DexFile& dex_file = *manager->GetDexFile();
18700e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao  const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1871bcdbbfebc8f32566d4cb3f66405e89cdb7351992Jeff Hao  const DexFile::TypeId& class_type_id = dex_file.GetTypeId(class_def.class_idx_);
1872bcdbbfebc8f32566d4cb3f66405e89cdb7351992Jeff Hao  const char* descriptor = dex_file.StringDataByIdx(class_type_id.descriptor_idx_);
1873fc0e94bed3f88ed7e50854fd8dfaf5dcb345250fIan Rogers
187400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
1875eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  StackHandleScope<3> hs(soa.Self());
1876eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::ClassLoader> class_loader(
1877eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
1878eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::Class> klass(
1879eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      hs.NewHandle(manager->GetClassLinker()->FindClass(soa.Self(), descriptor, class_loader)));
18800e49b42e03af56521d8ce2c9c84ac5b79e6241c9Jeff Hao
1881eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  if (klass.Get() != nullptr && !SkipClass(jclass_loader, dex_file, klass.Get())) {
188264b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers    // Only try to initialize classes that were successfully verified.
18833d1548debdaf8a1915fa432a4d267f7c0145654dIan Rogers    if (klass->IsVerified()) {
18848f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      // Attempt to initialize the class but bail if we either need to initialize the super-class
18858f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      // or static fields.
18868f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      manager->GetClassLinker()->EnsureInitialized(klass, false, false);
188764b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers      if (!klass->IsInitialized()) {
18888f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        // We don't want non-trivial class initialization occurring on multiple threads due to
18898f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        // deadlock problems. For example, a parent class is initialized (holding its lock) that
18908f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        // refers to a sub-class in its static/class initializer causing it to try to acquire the
18918f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        // sub-class' lock. While on a second thread the sub-class is initialized (holding its lock)
18928f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        // after first initializing its parents, whose locks are acquired. This leads to a
18938f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        // parent-to-child and a child-to-parent lock ordering and consequent potential deadlock.
18948f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        // We need to use an ObjectLock due to potential suspension in the interpreting code. Rather
18958f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        // than use a special Object for the purpose we use the Class of java.lang.Class.
1896eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        Handle<mirror::Class> h_klass(hs.NewHandle(klass->GetClass()));
1897db2633ce0358c704f97130a94b582602cb01d14aMathieu Chartier        ObjectLock<mirror::Class> lock(soa.Self(), h_klass);
18988f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        // Attempt to initialize allowing initialization of parent classes but still not static
18998f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        // fields.
19008f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        manager->GetClassLinker()->EnsureInitialized(klass, false, true);
19018f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        if (!klass->IsInitialized()) {
19028f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers          // We need to initialize static fields, we only do this for image classes that aren't
1903d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz          // marked with the $NoPreloadHolder (which implies this should not be initialized early).
19048f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers          bool can_init_static_fields = manager->GetCompiler()->IsImage() &&
1905d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz              manager->GetCompiler()->IsImageClass(descriptor) &&
1906d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz              !StringPiece(descriptor).ends_with("$NoPreloadHolder;");
19078f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers          if (can_init_static_fields) {
1908d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz            VLOG(compiler) << "Initializing: " << descriptor;
1909c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            // TODO multithreading support. We should ensure the current compilation thread has
1910c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            // exclusive access to the runtime and the transaction. To achieve this, we could use
1911c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            // a ReaderWriterMutex but we're holding the mutator lock so we fail mutex sanity
1912c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            // checks in Thread::AssertThreadSuspensionIsAllowable.
1913c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            Runtime* const runtime = Runtime::Current();
1914c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            Transaction transaction;
1915c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers
1916c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            // Run the class initializer in transaction mode.
1917c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            runtime->EnterTransactionMode(&transaction);
1918c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            const mirror::Class::Status old_status = klass->GetStatus();
1919c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            bool success = manager->GetClassLinker()->EnsureInitialized(klass, true, true);
1920c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            // TODO we detach transaction from runtime to indicate we quit the transactional
1921c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            // mode which prevents the GC from visiting objects modified during the transaction.
1922c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            // Ensure GC is not run so don't access freed objects when aborting transaction.
1923c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            const char* old_casue = soa.Self()->StartAssertNoThreadSuspension("Transaction end");
1924c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            runtime->ExitTransactionMode();
1925c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers
1926c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            if (!success) {
1927c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers              CHECK(soa.Self()->IsExceptionPending());
1928c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers              ThrowLocation throw_location;
1929c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers              mirror::Throwable* exception = soa.Self()->GetException(&throw_location);
1930c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers              VLOG(compiler) << "Initialization of " << descriptor << " aborted because of "
1931c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers                  << exception->Dump();
1932c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers              soa.Self()->ClearException();
1933c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers              transaction.Abort();
1934c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers              CHECK_EQ(old_status, klass->GetStatus()) << "Previous class status not restored";
193564b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers            }
1936c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers            soa.Self()->EndAssertNoThreadSuspension(old_casue);
193764b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers          }
193864b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers        }
19398f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        soa.Self()->AssertNoPendingException();
194064b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers      }
194127ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom    }
19423d1548debdaf8a1915fa432a4d267f7c0145654dIan Rogers    // Record the final class status if necessary.
194351c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom    ClassReference ref(manager->GetDexFile(), class_def_index);
19448f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers    manager->GetCompiler()->RecordClassStatus(ref, klass->GetStatus());
194598eacac683b78e60799323e8c7d59e7214808639jeffhao  }
19461f5393447b9f45be7918042d9ee7b521376de866Ian Rogers  // Clear any class not found or verification exceptions.
194762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  soa.Self()->ClearException();
194898eacac683b78e60799323e8c7d59e7214808639jeffhao}
194998eacac683b78e60799323e8c7d59e7214808639jeffhao
19501212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::InitializeClasses(jobject jni_class_loader, const DexFile& dex_file,
19510171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe                                       const std::vector<const DexFile*>& dex_files,
19523d504075f7c1204d581923460754bf6d3714b13fIan Rogers                                       ThreadPool* thread_pool, TimingLogger* timings) {
1953f5997b4d3f889569d5a2b724d83d764bfbb8d106Mathieu Chartier  TimingLogger::ScopedTiming t("InitializeNoClinit", timings);
19543d1548debdaf8a1915fa432a4d267f7c0145654dIan Rogers  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
19550171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe  ParallelCompilationManager context(class_linker, jni_class_loader, this, &dex_file, dex_files,
19560171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe                                     thread_pool);
1957d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz  size_t thread_count;
1958d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz  if (IsImage()) {
1959d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz    // TODO: remove this when transactional mode supports multithreading.
1960d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz    thread_count = 1U;
1961d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz  } else {
1962d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz    thread_count = thread_count_;
1963d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz  }
1964d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz  context.ForAll(0, dex_file.NumClassDefs(), InitializeClass, thread_count);
19653d1548debdaf8a1915fa432a4d267f7c0145654dIan Rogers}
196690dc30f4b9967e850d0594e57dfa8e7cb0369575Shih-wei Liao
19671212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::InitializeClasses(jobject class_loader,
19681212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                       const std::vector<const DexFile*>& dex_files,
19693d504075f7c1204d581923460754bf6d3714b13fIan Rogers                                       ThreadPool* thread_pool, TimingLogger* timings) {
19703d1548debdaf8a1915fa432a4d267f7c0145654dIan Rogers  for (size_t i = 0; i != dex_files.size(); ++i) {
19713d1548debdaf8a1915fa432a4d267f7c0145654dIan Rogers    const DexFile* dex_file = dex_files[i];
197235439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    CHECK(dex_file != nullptr);
19730171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe    InitializeClasses(class_loader, *dex_file, dex_files, thread_pool, timings);
197400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
1975a73a8b55415b544355438522524a640378698d85Mathieu Chartier  if (IsImage()) {
1976a73a8b55415b544355438522524a640378698d85Mathieu Chartier    // Prune garbage objects created during aborted transactions.
1977a73a8b55415b544355438522524a640378698d85Mathieu Chartier    Runtime::Current()->GetHeap()->CollectGarbage(true);
1978a73a8b55415b544355438522524a640378698d85Mathieu Chartier  }
197990dc30f4b9967e850d0594e57dfa8e7cb0369575Shih-wei Liao}
198090dc30f4b9967e850d0594e57dfa8e7cb0369575Shih-wei Liao
19811212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::Compile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
19823d504075f7c1204d581923460754bf6d3714b13fIan Rogers                             ThreadPool* thread_pool, TimingLogger* timings) {
1983ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  for (size_t i = 0; i != dex_files.size(); ++i) {
1984ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    const DexFile* dex_file = dex_files[i];
198535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    CHECK(dex_file != nullptr);
19860171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe    CompileDexFile(class_loader, *dex_file, dex_files, thread_pool, timings);
198783db7721aef15df6919c0ec072e087bef6041e2dBrian Carlstrom  }
1988aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe  VLOG(compiler) << "Compile: " << GetMemoryUsageString(false);
198983db7721aef15df6919c0ec072e087bef6041e2dBrian Carlstrom}
199083db7721aef15df6919c0ec072e087bef6041e2dBrian Carlstrom
1991219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogersvoid CompilerDriver::CompileClass(const ParallelCompilationManager* manager, size_t class_def_index) {
199267f99418f648c3a95256ed3dcd8e8b64eef0b372Anwar Ghuloum  ATRACE_CALL();
1993219b5a847ef74be5d3de4c16a29ec6413cc42af1Ian Rogers  const DexFile& dex_file = *manager->GetDexFile();
1994c225caa9715eeaeff87f27d5b6a3e7d4f6b7efadElliott Hughes  const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1995be7149fc2e7cc607937209f2819e3c1d672e2668Ian Rogers  ClassLinker* class_linker = manager->GetClassLinker();
1996cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  jobject jclass_loader = manager->GetClassLoader();
1997cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  {
1998cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    // Use a scoped object access to perform to the quick SkipClass check.
1999cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    const char* descriptor = dex_file.GetClassDescriptor(class_def);
2000cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    ScopedObjectAccess soa(Thread::Current());
2001cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    StackHandleScope<3> hs(soa.Self());
2002cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    Handle<mirror::ClassLoader> class_loader(
2003cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers        hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
2004cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    Handle<mirror::Class> klass(
2005cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers        hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor, class_loader)));
2006cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    if (klass.Get() == nullptr) {
2007cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      CHECK(soa.Self()->IsExceptionPending());
2008cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      soa.Self()->ClearException();
2009cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    } else if (SkipClass(jclass_loader, dex_file, klass.Get())) {
2010cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      return;
2011cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    }
20125ead0950c661761e90e04aefd6ea2205532ce874Brian Carlstrom  }
2013d1224c79631bd1801b067a0f212b91afa961a362jeffhao  ClassReference ref(&dex_file, class_def_index);
2014d1224c79631bd1801b067a0f212b91afa961a362jeffhao  // Skip compiling classes with generic verifier failures since they will still fail at runtime
2015c7f832061fea59fd6abd125f26c8ca1faec695a5Vladimir Marko  if (manager->GetCompiler()->verification_results_->IsClassRejected(ref)) {
2016d1224c79631bd1801b067a0f212b91afa961a362jeffhao    return;
2017d1224c79631bd1801b067a0f212b91afa961a362jeffhao  }
20180571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  const byte* class_data = dex_file.GetClassData(class_def);
201935439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (class_data == nullptr) {
20200571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    // empty class, probably a marker interface
20210571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    return;
20220571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  }
202367f99418f648c3a95256ed3dcd8e8b64eef0b372Anwar Ghuloum
20242d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  // Can we run DEX-to-DEX compiler on this class ?
202575021222d9c03a80fa5c136db0d5fb8d82d04031Sebastien Hertz  DexToDexCompilationLevel dex_to_dex_compilation_level = kDontDexToDexCompile;
20262d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  {
20272d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz    ScopedObjectAccess soa(Thread::Current());
2028eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    StackHandleScope<1> hs(soa.Self());
2029eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    Handle<mirror::ClassLoader> class_loader(
2030eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
20319837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    dex_to_dex_compilation_level = GetDexToDexCompilationlevel(soa.Self(), class_loader, dex_file,
20329837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers                                                               class_def);
20332d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  }
20340571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  ClassDataItemIterator it(dex_file, class_data);
20350571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  // Skip fields
20360571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  while (it.HasNextStaticField()) {
20370571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    it.Next();
20380571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  }
20390571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  while (it.HasNextInstanceField()) {
20400571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    it.Next();
20410571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  }
2042be7149fc2e7cc607937209f2819e3c1d672e2668Ian Rogers  CompilerDriver* driver = manager->GetCompiler();
204326318f722958ac1cba6a812026a1377f37c54941Andreas Gampe
204426318f722958ac1cba6a812026a1377f37c54941Andreas Gampe  bool compilation_enabled = driver->IsClassToCompile(
204526318f722958ac1cba6a812026a1377f37c54941Andreas Gampe      dex_file.StringByTypeIdx(class_def.class_idx_));
204626318f722958ac1cba6a812026a1377f37c54941Andreas Gampe
20470571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  // Compile direct methods
204868adbe41c7d9295da2bfc521d737ba6dabd36c98Brian Carlstrom  int64_t previous_direct_method_idx = -1;
20490571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  while (it.HasNextDirectMethod()) {
20506f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom    uint32_t method_idx = it.GetMemberIndex();
20516f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom    if (method_idx == previous_direct_method_idx) {
20526f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom      // smali can create dex files with two encoded_methods sharing the same method_idx
20536f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom      // http://code.google.com/p/smali/issues/detail?id=119
20546f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom      it.Next();
20556f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom      continue;
20566f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom    }
20576f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom    previous_direct_method_idx = method_idx;
20587fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    driver->CompileMethod(it.GetMethodCodeItem(), it.GetMethodAccessFlags(),
2059be7149fc2e7cc607937209f2819e3c1d672e2668Ian Rogers                          it.GetMethodInvokeType(class_def), class_def_index,
206026318f722958ac1cba6a812026a1377f37c54941Andreas Gampe                          method_idx, jclass_loader, dex_file, dex_to_dex_compilation_level,
206126318f722958ac1cba6a812026a1377f37c54941Andreas Gampe                          compilation_enabled);
20620571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    it.Next();
20639ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  }
20640571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  // Compile virtual methods
206568adbe41c7d9295da2bfc521d737ba6dabd36c98Brian Carlstrom  int64_t previous_virtual_method_idx = -1;
20660571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  while (it.HasNextVirtualMethod()) {
20676f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom    uint32_t method_idx = it.GetMemberIndex();
20686f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom    if (method_idx == previous_virtual_method_idx) {
20696f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom      // smali can create dex files with two encoded_methods sharing the same method_idx
20706f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom      // http://code.google.com/p/smali/issues/detail?id=119
20716f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom      it.Next();
20726f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom      continue;
20736f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom    }
20746f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstrom    previous_virtual_method_idx = method_idx;
20757fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    driver->CompileMethod(it.GetMethodCodeItem(), it.GetMethodAccessFlags(),
2076be7149fc2e7cc607937209f2819e3c1d672e2668Ian Rogers                          it.GetMethodInvokeType(class_def), class_def_index,
207726318f722958ac1cba6a812026a1377f37c54941Andreas Gampe                          method_idx, jclass_loader, dex_file, dex_to_dex_compilation_level,
207826318f722958ac1cba6a812026a1377f37c54941Andreas Gampe                          compilation_enabled);
20790571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    it.Next();
20809ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  }
20810571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  DCHECK(!it.HasNext());
20829ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom}
20839ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
20841212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::CompileDexFile(jobject class_loader, const DexFile& dex_file,
20850171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe                                    const std::vector<const DexFile*>& dex_files,
20863d504075f7c1204d581923460754bf6d3714b13fIan Rogers                                    ThreadPool* thread_pool, TimingLogger* timings) {
2087f5997b4d3f889569d5a2b724d83d764bfbb8d106Mathieu Chartier  TimingLogger::ScopedTiming t("Compile Dex File", timings);
2088be7149fc2e7cc607937209f2819e3c1d672e2668Ian Rogers  ParallelCompilationManager context(Runtime::Current()->GetClassLinker(), class_loader, this,
20890171987facdea1f4e22d0b5880dcde38a816adb1Andreas Gampe                                     &dex_file, dex_files, thread_pool);
20901212a022fa5f8ef9585d765b1809521812af882cIan Rogers  context.ForAll(0, dex_file.NumClassDefs(), CompilerDriver::CompileClass, thread_count_);
2091c225caa9715eeaeff87f27d5b6a3e7d4f6b7efadElliott Hughes}
2092c225caa9715eeaeff87f27d5b6a3e7d4f6b7efadElliott Hughes
20931212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
20948b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                   InvokeType invoke_type, uint16_t class_def_idx,
20951212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                   uint32_t method_idx, jobject class_loader,
20962d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz                                   const DexFile& dex_file,
209726318f722958ac1cba6a812026a1377f37c54941Andreas Gampe                                   DexToDexCompilationLevel dex_to_dex_compilation_level,
209826318f722958ac1cba6a812026a1377f37c54941Andreas Gampe                                   bool compilation_enabled) {
209935439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  CompiledMethod* compiled_method = nullptr;
2100b68c6e578a28a9717d78dfd522d9d9b8befaedf2Mathieu Chartier  uint64_t start_ns = kTimeCompileMethod ? NanoTime() : 0;
210125fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  MethodReference method_ref(&dex_file, method_idx);
21024dd96f56909ec35c83a3d468b0e47769988c1a1dLogan Chien
2103169c9a7f46776b235d0a37d5e0ff27682deffe06Ian Rogers  if ((access_flags & kAccNative) != 0) {
21040188ab739c3ef8bcd521d88f84894db8c0f012c5Ian Rogers    // Are we interpreting only and have support for generic JNI down calls?
21054a200f56b7075309316b04d550c9cc50f8314eddJeff Hao    if (!compiler_options_->IsCompilationEnabled() &&
2106b95a5345ae4217b70ca36f0cced92f68dda7caf5Stuart Monteith        (instruction_set_ == kX86_64 || instruction_set_ == kArm64)) {
21075b27149ce3cbf39f99c9d9c8ba5ebf1657c297e1Ian Rogers      // Leaving this empty will trigger the generic JNI version
21085b27149ce3cbf39f99c9d9c8ba5ebf1657c297e1Ian Rogers    } else {
210972d32629303f8f39362a4099481f48646aed042fIan Rogers      compiled_method = compiler_->JniCompile(access_flags, method_idx, dex_file);
211035439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      CHECK(compiled_method != nullptr);
21115b27149ce3cbf39f99c9d9c8ba5ebf1657c297e1Ian Rogers    }
2112169c9a7f46776b235d0a37d5e0ff27682deffe06Ian Rogers  } else if ((access_flags & kAccAbstract) != 0) {
21132cc022b653e1e84eed2522254ec684bd097572b8Brian Carlstrom  } else {
2114f535c69f115c61ffadca1bd2706244d0aa30f9aaAndreas Gampe    bool has_verified_method = verification_results_->GetVerifiedMethod(method_ref) != nullptr;
211526318f722958ac1cba6a812026a1377f37c54941Andreas Gampe    bool compile = compilation_enabled &&
2116f535c69f115c61ffadca1bd2706244d0aa30f9aaAndreas Gampe                   // Basic checks, e.g., not <clinit>.
2117f535c69f115c61ffadca1bd2706244d0aa30f9aaAndreas Gampe                   verification_results_->IsCandidateForCompilation(method_ref, access_flags) &&
2118f535c69f115c61ffadca1bd2706244d0aa30f9aaAndreas Gampe                   // Did not fail to create VerifiedMethod metadata.
2119f535c69f115c61ffadca1bd2706244d0aa30f9aaAndreas Gampe                   has_verified_method;
21204d4adb1dae07bb7421e863732ab789413a3b43f0Sebastien Hertz    if (compile) {
212135439baf287b291b67ee406308e17fc6194facbfAndreas Gampe      // NOTE: if compiler declines to compile this method, it will return nullptr.
212272d32629303f8f39362a4099481f48646aed042fIan Rogers      compiled_method = compiler_->Compile(code_item, access_flags, invoke_type, class_def_idx,
212372d32629303f8f39362a4099481f48646aed042fIan Rogers                                           method_idx, class_loader, dex_file);
212417965ed6ec29407599a0bcd4ea92532424294fedSebastien Hertz    }
212517965ed6ec29407599a0bcd4ea92532424294fedSebastien Hertz    if (compiled_method == nullptr && dex_to_dex_compilation_level != kDontDexToDexCompile) {
212617965ed6ec29407599a0bcd4ea92532424294fedSebastien Hertz      // TODO: add a command-line option to disable DEX-to-DEX compilation ?
2127f535c69f115c61ffadca1bd2706244d0aa30f9aaAndreas Gampe      // Do not optimize if a VerifiedMethod is missing. SafeCast elision, for example, relies on
2128f535c69f115c61ffadca1bd2706244d0aa30f9aaAndreas Gampe      // it.
212975021222d9c03a80fa5c136db0d5fb8d82d04031Sebastien Hertz      (*dex_to_dex_compiler_)(*this, code_item, access_flags,
213075021222d9c03a80fa5c136db0d5fb8d82d04031Sebastien Hertz                              invoke_type, class_def_idx,
213175021222d9c03a80fa5c136db0d5fb8d82d04031Sebastien Hertz                              method_idx, class_loader, dex_file,
2132f535c69f115c61ffadca1bd2706244d0aa30f9aaAndreas Gampe                              has_verified_method ? dex_to_dex_compilation_level : kRequired);
2133f3e9855ee2000106b54fd479f7a46da2dc2ad079Ian Rogers    }
2134bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes  }
2135b68c6e578a28a9717d78dfd522d9d9b8befaedf2Mathieu Chartier  if (kTimeCompileMethod) {
2136b68c6e578a28a9717d78dfd522d9d9b8befaedf2Mathieu Chartier    uint64_t duration_ns = NanoTime() - start_ns;
2137b68c6e578a28a9717d78dfd522d9d9b8befaedf2Mathieu Chartier    if (duration_ns > MsToNs(compiler_->GetMaximumCompilationTimeBeforeWarning())) {
2138b68c6e578a28a9717d78dfd522d9d9b8befaedf2Mathieu Chartier      LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
2139b68c6e578a28a9717d78dfd522d9d9b8befaedf2Mathieu Chartier                   << " took " << PrettyDuration(duration_ns);
2140b68c6e578a28a9717d78dfd522d9d9b8befaedf2Mathieu Chartier    }
2141f09afe8fbd78943df6a8b10f03c36dcd190dd054Elliott Hughes  }
2142f09afe8fbd78943df6a8b10f03c36dcd190dd054Elliott Hughes
214350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  Thread* self = Thread::Current();
214435439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  if (compiled_method != nullptr) {
214525fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier    DCHECK(GetCompiledMethod(method_ref) == nullptr) << PrettyMethod(method_idx, dex_file);
214600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
214750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      MutexLock mu(self, compiled_methods_lock_);
214825fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier      compiled_methods_.Put(method_ref, compiled_method);
214900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
215025fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier    DCHECK(GetCompiledMethod(method_ref) != nullptr) << PrettyMethod(method_idx, dex_file);
21512cc022b653e1e84eed2522254ec684bd097572b8Brian Carlstrom  }
21529baa4aefc370f48774b6104680193d9a7e4fb631Brian Carlstrom
215325fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  // Done compiling, delete the verified method to reduce native memory usage.
215425fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  verification_results_->RemoveVerifiedMethod(method_ref);
215525fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier
215650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  if (self->IsExceptionPending()) {
215750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    ScopedObjectAccess soa(self);
215800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    LOG(FATAL) << "Unexpected exception compiling: " << PrettyMethod(method_idx, dex_file) << "\n"
215935439baf287b291b67ee406308e17fc6194facbfAndreas Gampe        << self->GetException(nullptr)->Dump();
216000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
21610571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers}
216228ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers
21631212a022fa5f8ef9585d765b1809521812af882cIan RogersCompiledClass* CompilerDriver::GetCompiledClass(ClassReference ref) const {
216450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(Thread::Current(), compiled_classes_lock_);
21650755ec5ea1dce0b549fc1adefeb52d89f119ebecBrian Carlstrom  ClassTable::const_iterator it = compiled_classes_.find(ref);
21660755ec5ea1dce0b549fc1adefeb52d89f119ebecBrian Carlstrom  if (it == compiled_classes_.end()) {
216735439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    return nullptr;
21680755ec5ea1dce0b549fc1adefeb52d89f119ebecBrian Carlstrom  }
216935439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  CHECK(it->second != nullptr);
21700755ec5ea1dce0b549fc1adefeb52d89f119ebecBrian Carlstrom  return it->second;
21710755ec5ea1dce0b549fc1adefeb52d89f119ebecBrian Carlstrom}
21720755ec5ea1dce0b549fc1adefeb52d89f119ebecBrian Carlstrom
21738f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogersvoid CompilerDriver::RecordClassStatus(ClassReference ref, mirror::Class::Status status) {
21748f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers  MutexLock mu(Thread::Current(), compiled_classes_lock_);
21758f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers  auto it = compiled_classes_.find(ref);
21768f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers  if (it == compiled_classes_.end() || it->second->GetStatus() != status) {
21778f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers    // An entry doesn't exist or the status is lower than the new status.
21788f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers    if (it != compiled_classes_.end()) {
21798f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      CHECK_GT(status, it->second->GetStatus());
21808f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      delete it->second;
21818f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers    }
21828f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers    switch (status) {
21838f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      case mirror::Class::kStatusNotReady:
21848f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      case mirror::Class::kStatusError:
21858f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      case mirror::Class::kStatusRetryVerificationAtRuntime:
21868f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      case mirror::Class::kStatusVerified:
21878f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      case mirror::Class::kStatusInitialized:
21888f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        break;  // Expected states.
21898f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers      default:
21908f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers        LOG(FATAL) << "Unexpected class status for class "
21918f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers            << PrettyDescriptor(ref.first->GetClassDescriptor(ref.first->GetClassDef(ref.second)))
21928f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers            << " of " << status;
21938f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers    }
21948f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers    CompiledClass* compiled_class = new CompiledClass(status);
21958f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers    compiled_classes_.Overwrite(ref, compiled_class);
21968f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers  }
21978f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers}
21988f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers
21991212a022fa5f8ef9585d765b1809521812af882cIan RogersCompiledMethod* CompilerDriver::GetCompiledMethod(MethodReference ref) const {
220050b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(Thread::Current(), compiled_methods_lock_);
22010571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  MethodTable::const_iterator it = compiled_methods_.find(ref);
22020571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  if (it == compiled_methods_.end()) {
220335439baf287b291b67ee406308e17fc6194facbfAndreas Gampe    return nullptr;
22042c8f653c98d658419f464b6147c10e11a664d2e6Ian Rogers  }
220535439baf287b291b67ee406308e17fc6194facbfAndreas Gampe  CHECK(it->second != nullptr);
22063320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom  return it->second;
22079ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom}
22089ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
22091212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::AddRequiresConstructorBarrier(Thread* self, const DexFile* dex_file,
22108b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                                   uint16_t class_def_index) {
22118f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers  WriterMutexLock mu(self, freezing_constructor_lock_);
2212fffdb023275613612a22ec62b3421ffe4d2b73feIan Rogers  freezing_constructor_classes_.insert(ClassReference(dex_file, class_def_index));
2213fffdb023275613612a22ec62b3421ffe4d2b73feIan Rogers}
2214fffdb023275613612a22ec62b3421ffe4d2b73feIan Rogers
22151212a022fa5f8ef9585d765b1809521812af882cIan Rogersbool CompilerDriver::RequiresConstructorBarrier(Thread* self, const DexFile* dex_file,
22168b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                                uint16_t class_def_index) {
22178f3c9ae38df2460940a26dff889a84430b6c38d3Ian Rogers  ReaderMutexLock mu(self, freezing_constructor_lock_);
2218fffdb023275613612a22ec62b3421ffe4d2b73feIan Rogers  return freezing_constructor_classes_.count(ClassReference(dex_file, class_def_index)) != 0;
2219fffdb023275613612a22ec62b3421ffe4d2b73feIan Rogers}
2220fffdb023275613612a22ec62b3421ffe4d2b73feIan Rogers
22213f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrombool CompilerDriver::WriteElf(const std::string& android_root,
2222265091e581c9f643b37e7966890911f09e223269Brian Carlstrom                              bool is_host,
222351c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom                              const std::vector<const art::DexFile*>& dex_files,
22243d504075f7c1204d581923460754bf6d3714b13fIan Rogers                              OatWriter* oat_writer,
222551c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom                              art::File* file)
222651c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
222772d32629303f8f39362a4099481f48646aed042fIan Rogers  return compiler_->WriteElf(file, oat_writer, dex_files, android_root, is_host);
2228265091e581c9f643b37e7966890911f09e223269Brian Carlstrom}
22291212a022fa5f8ef9585d765b1809521812af882cIan Rogersvoid CompilerDriver::InstructionSetToLLVMTarget(InstructionSet instruction_set,
22303d504075f7c1204d581923460754bf6d3714b13fIan Rogers                                                std::string* target_triple,
22313d504075f7c1204d581923460754bf6d3714b13fIan Rogers                                                std::string* target_cpu,
22323d504075f7c1204d581923460754bf6d3714b13fIan Rogers                                                std::string* target_attr) {
2233265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  switch (instruction_set) {
2234700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    case kThumb2:
22353d504075f7c1204d581923460754bf6d3714b13fIan Rogers      *target_triple = "thumb-none-linux-gnueabi";
22363d504075f7c1204d581923460754bf6d3714b13fIan Rogers      *target_cpu = "cortex-a9";
22373d504075f7c1204d581923460754bf6d3714b13fIan Rogers      *target_attr = "+thumb2,+neon,+neonfp,+vfp3,+db";
2238700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      break;
2239700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
2240700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    case kArm:
22413d504075f7c1204d581923460754bf6d3714b13fIan Rogers      *target_triple = "armv7-none-linux-gnueabi";
2242700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      // TODO: Fix for Nexus S.
22433d504075f7c1204d581923460754bf6d3714b13fIan Rogers      *target_cpu = "cortex-a9";
2244700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      // TODO: Fix for Xoom.
22453d504075f7c1204d581923460754bf6d3714b13fIan Rogers      *target_attr = "+v7,+neon,+neonfp,+vfp3,+db";
2246700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      break;
2247700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
2248700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    case kX86:
22493d504075f7c1204d581923460754bf6d3714b13fIan Rogers      *target_triple = "i386-pc-linux-gnu";
22503d504075f7c1204d581923460754bf6d3714b13fIan Rogers      *target_attr = "";
2251700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      break;
2252700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
22536a58cb16d803c9a7b3a75ccac8be19dd9d4e520dDmitry Petrochenko    case kX86_64:
22546a58cb16d803c9a7b3a75ccac8be19dd9d4e520dDmitry Petrochenko      *target_triple = "x86_64-pc-linux-gnu";
22556a58cb16d803c9a7b3a75ccac8be19dd9d4e520dDmitry Petrochenko      *target_attr = "";
22566a58cb16d803c9a7b3a75ccac8be19dd9d4e520dDmitry Petrochenko      break;
22576a58cb16d803c9a7b3a75ccac8be19dd9d4e520dDmitry Petrochenko
2258700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    case kMips:
22593d504075f7c1204d581923460754bf6d3714b13fIan Rogers      *target_triple = "mipsel-unknown-linux";
22603d504075f7c1204d581923460754bf6d3714b13fIan Rogers      *target_attr = "mips32r2";
2261700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      break;
2262700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
2263700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    default:
2264700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      LOG(FATAL) << "Unknown instruction set: " << instruction_set;
2265700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    }
2266700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  }
226739c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison
226839c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allisonbool CompilerDriver::SkipCompilation(const std::string& method_name) {
2269c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle  if (!profile_present_) {
2270644789fa0e807a9aa6d6e32d36ce039ec7318a7fDave Allison    return false;
227139c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison  }
2272bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle  // First find the method in the profile file.
2273bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle  ProfileFile::ProfileData data;
2274bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle  if (!profile_file_.GetProfileData(&data, method_name)) {
227539c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison    // Not in profile, no information can be determined.
227608f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle    if (kIsDebugBuild) {
227708f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle      VLOG(compiler) << "not compiling " << method_name << " because it's not in the profile";
227808f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle    }
227939c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison    return true;
228039c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison  }
2281bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle
2282bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle  // Methods that comprise top_k_threshold % of the total samples will be compiled.
2283f6a4cee66a173ee7ef48af5503d9899aa93b3aebCalin Juravle  // Compare against the start of the topK percentage bucket just in case the threshold
228404ff226764b125c79a0bf41e571c8311bac8a868Calin Juravle  // falls inside a bucket.
2285c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle  bool compile = data.GetTopKUsedPercentage() - data.GetUsedPercent()
2286c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle                 <= compiler_options_->GetTopKProfileThreshold();
228708f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle  if (kIsDebugBuild) {
228808f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle    if (compile) {
228908f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle      LOG(INFO) << "compiling method " << method_name << " because its usage is part of top "
229008f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle          << data.GetTopKUsedPercentage() << "% with a percent of " << data.GetUsedPercent() << "%"
229108f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle          << " (topKThreshold=" << compiler_options_->GetTopKProfileThreshold() << ")";
229208f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle    } else {
229308f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle      VLOG(compiler) << "not compiling method " << method_name
229408f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle          << " because it's not part of leading " << compiler_options_->GetTopKProfileThreshold()
229508f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle          << "% samples)";
229608f7a2d06b915a5e21ded648d9feee519afd2f76Calin Juravle    }
229739c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison  }
229839c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison  return !compile;
229939c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison}
230025fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier
2301aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampestd::string CompilerDriver::GetMemoryUsageString(bool extended) const {
230225fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  std::ostringstream oss;
230325fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  const ArenaPool* arena_pool = GetArenaPool();
230425fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  gc::Heap* heap = Runtime::Current()->GetHeap();
230525fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  oss << "arena alloc=" << PrettySize(arena_pool->GetBytesAllocated());
230625fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  oss << " java alloc=" << PrettySize(heap->GetBytesAllocated());
230725fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier#ifdef HAVE_MALLOC_H
230825fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  struct mallinfo info = mallinfo();
230925fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  const size_t allocated_space = static_cast<size_t>(info.uordblks);
231025fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  const size_t free_space = static_cast<size_t>(info.fordblks);
231125fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  oss << " native alloc=" << PrettySize(allocated_space) << " free="
231225fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier      << PrettySize(free_space);
231325fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier#endif
231462746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe  if (swap_space_.get() != nullptr) {
231562746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe    oss << " swap=" << PrettySize(swap_space_->GetSize());
231662746d8d9c4400e4764f162b22bfb1a32be287a9Andreas Gampe  }
2317aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe  if (extended) {
2318aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe    oss << "\nCode dedupe: " << dedupe_code_.DumpStats();
2319aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe    oss << "\nMapping table dedupe: " << dedupe_mapping_table_.DumpStats();
2320aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe    oss << "\nVmap table dedupe: " << dedupe_vmap_table_.DumpStats();
2321aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe    oss << "\nGC map dedupe: " << dedupe_gc_map_.DumpStats();
2322aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe    oss << "\nCFI info dedupe: " << dedupe_cfi_info_.DumpStats();
2323aa3eff991fc34d6434465bf6bf49ef2e2fb286b7Andreas Gampe  }
232425fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier  return oss.str();
232525fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier}
232625fda92083d5b93b38cc1f6b12ac6a44d992d6a4Mathieu Chartier
23279ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom}  // namespace art
2328