blob: a241d5146830f2cf70106bfaa8140d561c881c40 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "compiler_driver.h"
18
Anwar Ghuloum67f99412013-08-12 14:19:48 -070019#define ATRACE_TAG ATRACE_TAG_DALVIK
20#include <utils/Trace.h>
Brian Carlstrom7940e442013-07-12 13:46:57 -070021
Calin Juravlef6a4cee2014-04-02 17:03:08 +010022#include <fstream>
Anwar Ghuloum67f99412013-08-12 14:19:48 -070023#include <vector>
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include <unistd.h>
Calin Juravlef6a4cee2014-04-02 17:03:08 +010025#include <utility>
Brian Carlstrom7940e442013-07-12 13:46:57 -070026
27#include "base/stl_util.h"
28#include "base/timing_logger.h"
29#include "class_linker.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000030#include "compiler.h"
Vladimir Markobe0e5462014-02-26 11:24:15 +000031#include "compiler_driver-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070032#include "dex_compilation_unit.h"
33#include "dex_file-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000034#include "dex/verification_results.h"
Vladimir Marko2730db02014-01-27 11:15:17 +000035#include "dex/verified_method.h"
Vladimir Marko2bc47802014-02-10 09:43:07 +000036#include "dex/quick/dex_file_method_inliner.h"
Mark Mendellae9fd932014-02-10 16:14:35 -080037#include "driver/compiler_options.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070038#include "jni_internal.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070039#include "object_utils.h"
40#include "runtime.h"
41#include "gc/accounting/card_table-inl.h"
42#include "gc/accounting/heap_bitmap.h"
43#include "gc/space/space.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070044#include "mirror/art_field-inl.h"
45#include "mirror/art_method-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070046#include "mirror/class_loader.h"
47#include "mirror/class-inl.h"
48#include "mirror/dex_cache-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070049#include "mirror/object-inl.h"
50#include "mirror/object_array-inl.h"
51#include "mirror/throwable.h"
52#include "scoped_thread_state_change.h"
53#include "ScopedLocalRef.h"
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080054#include "sirt_ref-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070055#include "thread.h"
56#include "thread_pool.h"
Ian Rogers848871b2013-08-05 10:56:33 -070057#include "trampolines/trampoline_compiler.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010058#include "transaction.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070059#include "verifier/method_verifier.h"
Vladimir Marko2bc47802014-02-10 09:43:07 +000060#include "verifier/method_verifier-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070061
Calin Juravle04ff2262014-04-02 19:08:47 +010062#ifdef HAVE_ANDROID_OS
63#include "cutils/properties.h"
64#endif
65
Brian Carlstrom7940e442013-07-12 13:46:57 -070066namespace art {
67
68static double Percentage(size_t x, size_t y) {
69 return 100.0 * (static_cast<double>(x)) / (static_cast<double>(x + y));
70}
71
72static void DumpStat(size_t x, size_t y, const char* str) {
73 if (x == 0 && y == 0) {
74 return;
75 }
Ian Rogerse732ef12013-10-09 15:22:24 -070076 LOG(INFO) << Percentage(x, y) << "% of " << str << " for " << (x + y) << " cases";
Brian Carlstrom7940e442013-07-12 13:46:57 -070077}
78
Vladimir Markof096aad2014-01-23 15:51:58 +000079class CompilerDriver::AOTCompilationStats {
Brian Carlstrom7940e442013-07-12 13:46:57 -070080 public:
81 AOTCompilationStats()
82 : stats_lock_("AOT compilation statistics lock"),
83 types_in_dex_cache_(0), types_not_in_dex_cache_(0),
84 strings_in_dex_cache_(0), strings_not_in_dex_cache_(0),
85 resolved_types_(0), unresolved_types_(0),
86 resolved_instance_fields_(0), unresolved_instance_fields_(0),
87 resolved_local_static_fields_(0), resolved_static_fields_(0), unresolved_static_fields_(0),
88 type_based_devirtualization_(0),
89 safe_casts_(0), not_safe_casts_(0) {
90 for (size_t i = 0; i <= kMaxInvokeType; i++) {
91 resolved_methods_[i] = 0;
92 unresolved_methods_[i] = 0;
93 virtual_made_direct_[i] = 0;
94 direct_calls_to_boot_[i] = 0;
95 direct_methods_to_boot_[i] = 0;
96 }
97 }
98
99 void Dump() {
100 DumpStat(types_in_dex_cache_, types_not_in_dex_cache_, "types known to be in dex cache");
101 DumpStat(strings_in_dex_cache_, strings_not_in_dex_cache_, "strings known to be in dex cache");
102 DumpStat(resolved_types_, unresolved_types_, "types resolved");
103 DumpStat(resolved_instance_fields_, unresolved_instance_fields_, "instance fields resolved");
104 DumpStat(resolved_local_static_fields_ + resolved_static_fields_, unresolved_static_fields_,
105 "static fields resolved");
106 DumpStat(resolved_local_static_fields_, resolved_static_fields_ + unresolved_static_fields_,
107 "static fields local to a class");
108 DumpStat(safe_casts_, not_safe_casts_, "check-casts removed based on type information");
109 // Note, the code below subtracts the stat value so that when added to the stat value we have
110 // 100% of samples. TODO: clean this up.
111 DumpStat(type_based_devirtualization_,
112 resolved_methods_[kVirtual] + unresolved_methods_[kVirtual] +
113 resolved_methods_[kInterface] + unresolved_methods_[kInterface] -
114 type_based_devirtualization_,
115 "virtual/interface calls made direct based on type information");
116
117 for (size_t i = 0; i <= kMaxInvokeType; i++) {
118 std::ostringstream oss;
119 oss << static_cast<InvokeType>(i) << " methods were AOT resolved";
120 DumpStat(resolved_methods_[i], unresolved_methods_[i], oss.str().c_str());
121 if (virtual_made_direct_[i] > 0) {
122 std::ostringstream oss2;
123 oss2 << static_cast<InvokeType>(i) << " methods made direct";
124 DumpStat(virtual_made_direct_[i],
125 resolved_methods_[i] + unresolved_methods_[i] - virtual_made_direct_[i],
126 oss2.str().c_str());
127 }
128 if (direct_calls_to_boot_[i] > 0) {
129 std::ostringstream oss2;
130 oss2 << static_cast<InvokeType>(i) << " method calls are direct into boot";
131 DumpStat(direct_calls_to_boot_[i],
132 resolved_methods_[i] + unresolved_methods_[i] - direct_calls_to_boot_[i],
133 oss2.str().c_str());
134 }
135 if (direct_methods_to_boot_[i] > 0) {
136 std::ostringstream oss2;
137 oss2 << static_cast<InvokeType>(i) << " method calls have methods in boot";
138 DumpStat(direct_methods_to_boot_[i],
139 resolved_methods_[i] + unresolved_methods_[i] - direct_methods_to_boot_[i],
140 oss2.str().c_str());
141 }
142 }
143 }
144
145// Allow lossy statistics in non-debug builds.
146#ifndef NDEBUG
147#define STATS_LOCK() MutexLock mu(Thread::Current(), stats_lock_)
148#else
149#define STATS_LOCK()
150#endif
151
152 void TypeInDexCache() {
153 STATS_LOCK();
154 types_in_dex_cache_++;
155 }
156
157 void TypeNotInDexCache() {
158 STATS_LOCK();
159 types_not_in_dex_cache_++;
160 }
161
162 void StringInDexCache() {
163 STATS_LOCK();
164 strings_in_dex_cache_++;
165 }
166
167 void StringNotInDexCache() {
168 STATS_LOCK();
169 strings_not_in_dex_cache_++;
170 }
171
172 void TypeDoesntNeedAccessCheck() {
173 STATS_LOCK();
174 resolved_types_++;
175 }
176
177 void TypeNeedsAccessCheck() {
178 STATS_LOCK();
179 unresolved_types_++;
180 }
181
182 void ResolvedInstanceField() {
183 STATS_LOCK();
184 resolved_instance_fields_++;
185 }
186
187 void UnresolvedInstanceField() {
188 STATS_LOCK();
189 unresolved_instance_fields_++;
190 }
191
192 void ResolvedLocalStaticField() {
193 STATS_LOCK();
194 resolved_local_static_fields_++;
195 }
196
197 void ResolvedStaticField() {
198 STATS_LOCK();
199 resolved_static_fields_++;
200 }
201
202 void UnresolvedStaticField() {
203 STATS_LOCK();
204 unresolved_static_fields_++;
205 }
206
207 // Indicate that type information from the verifier led to devirtualization.
208 void PreciseTypeDevirtualization() {
209 STATS_LOCK();
210 type_based_devirtualization_++;
211 }
212
213 // Indicate that a method of the given type was resolved at compile time.
214 void ResolvedMethod(InvokeType type) {
215 DCHECK_LE(type, kMaxInvokeType);
216 STATS_LOCK();
217 resolved_methods_[type]++;
218 }
219
220 // Indicate that a method of the given type was unresolved at compile time as it was in an
221 // unknown dex file.
222 void UnresolvedMethod(InvokeType type) {
223 DCHECK_LE(type, kMaxInvokeType);
224 STATS_LOCK();
225 unresolved_methods_[type]++;
226 }
227
228 // Indicate that a type of virtual method dispatch has been converted into a direct method
229 // dispatch.
230 void VirtualMadeDirect(InvokeType type) {
231 DCHECK(type == kVirtual || type == kInterface || type == kSuper);
232 STATS_LOCK();
233 virtual_made_direct_[type]++;
234 }
235
236 // Indicate that a method of the given type was able to call directly into boot.
237 void DirectCallsToBoot(InvokeType type) {
238 DCHECK_LE(type, kMaxInvokeType);
239 STATS_LOCK();
240 direct_calls_to_boot_[type]++;
241 }
242
243 // Indicate that a method of the given type was able to be resolved directly from boot.
244 void DirectMethodsToBoot(InvokeType type) {
245 DCHECK_LE(type, kMaxInvokeType);
246 STATS_LOCK();
247 direct_methods_to_boot_[type]++;
248 }
249
Vladimir Markof096aad2014-01-23 15:51:58 +0000250 void ProcessedInvoke(InvokeType type, int flags) {
251 STATS_LOCK();
252 if (flags == 0) {
253 unresolved_methods_[type]++;
254 } else {
255 DCHECK_NE((flags & kFlagMethodResolved), 0);
256 resolved_methods_[type]++;
257 if ((flags & kFlagVirtualMadeDirect) != 0) {
258 virtual_made_direct_[type]++;
259 if ((flags & kFlagPreciseTypeDevirtualization) != 0) {
260 type_based_devirtualization_++;
261 }
262 } else {
263 DCHECK_EQ((flags & kFlagPreciseTypeDevirtualization), 0);
264 }
265 if ((flags & kFlagDirectCallToBoot) != 0) {
266 direct_calls_to_boot_[type]++;
267 }
268 if ((flags & kFlagDirectMethodToBoot) != 0) {
269 direct_methods_to_boot_[type]++;
270 }
271 }
272 }
273
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274 // A check-cast could be eliminated due to verifier type analysis.
275 void SafeCast() {
276 STATS_LOCK();
277 safe_casts_++;
278 }
279
280 // A check-cast couldn't be eliminated due to verifier type analysis.
281 void NotASafeCast() {
282 STATS_LOCK();
283 not_safe_casts_++;
284 }
285
286 private:
287 Mutex stats_lock_;
288
289 size_t types_in_dex_cache_;
290 size_t types_not_in_dex_cache_;
291
292 size_t strings_in_dex_cache_;
293 size_t strings_not_in_dex_cache_;
294
295 size_t resolved_types_;
296 size_t unresolved_types_;
297
298 size_t resolved_instance_fields_;
299 size_t unresolved_instance_fields_;
300
301 size_t resolved_local_static_fields_;
302 size_t resolved_static_fields_;
303 size_t unresolved_static_fields_;
304 // Type based devirtualization for invoke interface and virtual.
305 size_t type_based_devirtualization_;
306
307 size_t resolved_methods_[kMaxInvokeType + 1];
308 size_t unresolved_methods_[kMaxInvokeType + 1];
309 size_t virtual_made_direct_[kMaxInvokeType + 1];
310 size_t direct_calls_to_boot_[kMaxInvokeType + 1];
311 size_t direct_methods_to_boot_[kMaxInvokeType + 1];
312
313 size_t safe_casts_;
314 size_t not_safe_casts_;
315
316 DISALLOW_COPY_AND_ASSIGN(AOTCompilationStats);
317};
318
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319
320extern "C" art::CompiledMethod* ArtCompileDEX(art::CompilerDriver& compiler,
321 const art::DexFile::CodeItem* code_item,
322 uint32_t access_flags,
323 art::InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700324 uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700325 uint32_t method_idx,
326 jobject class_loader,
327 const art::DexFile& dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328
Brian Carlstrom6449c622014-02-10 23:48:36 -0800329CompilerDriver::CompilerDriver(const CompilerOptions* compiler_options,
330 VerificationResults* verification_results,
Vladimir Marko5816ed42013-11-27 17:04:20 +0000331 DexFileToMethodInlinerMap* method_inliner_map,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000332 Compiler::Kind compiler_kind,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000333 InstructionSet instruction_set,
Dave Allison70202782013-10-22 17:52:19 -0700334 InstructionSetFeatures instruction_set_features,
buzbeea024a062013-07-31 10:47:37 -0700335 bool image, DescriptorSet* image_classes, size_t thread_count,
Dave Allison39c3bfb2014-01-28 18:33:52 -0800336 bool dump_stats, bool dump_passes, CumulativeLogger* timer,
337 std::string profile_file)
338 : profile_ok_(false), compiler_options_(compiler_options),
Brian Carlstrom6449c622014-02-10 23:48:36 -0800339 verification_results_(verification_results),
Vladimir Marko5816ed42013-11-27 17:04:20 +0000340 method_inliner_map_(method_inliner_map),
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000341 compiler_(Compiler::Create(compiler_kind)),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342 instruction_set_(instruction_set),
Dave Allison70202782013-10-22 17:52:19 -0700343 instruction_set_features_(instruction_set_features),
Ian Rogersdd7624d2014-03-14 17:43:00 -0700344 instruction_set_is_64_bit_(instruction_set == kX86_64 || instruction_set == kArm64),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700345 freezing_constructor_lock_("freezing constructor lock"),
346 compiled_classes_lock_("compiled classes lock"),
347 compiled_methods_lock_("compiled method lock"),
348 image_(image),
349 image_classes_(image_classes),
350 thread_count_(thread_count),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 start_ns_(0),
352 stats_(new AOTCompilationStats),
353 dump_stats_(dump_stats),
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000354 dump_passes_(dump_passes),
355 timings_logger_(timer),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700356 compiler_library_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 compiler_context_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700358 compiler_enable_auto_elf_loading_(NULL),
359 compiler_get_method_code_addr_(NULL),
Mark Mendell55d0eac2014-02-06 11:02:52 -0800360 support_boot_image_fixup_(instruction_set != kMips),
Mark Mendellae9fd932014-02-10 16:14:35 -0800361 cfi_info_(nullptr),
Ian Rogersd133b972013-09-05 11:01:30 -0700362 dedupe_code_("dedupe code"),
363 dedupe_mapping_table_("dedupe mapping table"),
364 dedupe_vmap_table_("dedupe vmap table"),
Mark Mendellae9fd932014-02-10 16:14:35 -0800365 dedupe_gc_map_("dedupe gc map"),
366 dedupe_cfi_info_("dedupe cfi info") {
Brian Carlstrom6449c622014-02-10 23:48:36 -0800367 DCHECK(compiler_options_ != nullptr);
368 DCHECK(verification_results_ != nullptr);
369 DCHECK(method_inliner_map_ != nullptr);
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700370
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 CHECK_PTHREAD_CALL(pthread_key_create, (&tls_key_, NULL), "compiler tls key");
372
Dave Allison39c3bfb2014-01-28 18:33:52 -0800373 // Read the profile file if one is provided.
374 if (profile_file != "") {
375 profile_ok_ = ReadProfile(profile_file);
376 }
377
Sebastien Hertz75021222013-07-16 18:34:50 +0200378 dex_to_dex_compiler_ = reinterpret_cast<DexToDexCompilerFn>(ArtCompileDEX);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700379
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000380 compiler_->Init(*this);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381
382 CHECK(!Runtime::Current()->IsStarted());
383 if (!image_) {
384 CHECK(image_classes_.get() == NULL);
385 }
Mark Mendellae9fd932014-02-10 16:14:35 -0800386
387 // Are we generating CFI information?
388 if (compiler_options->GetGenerateGDBInformation()) {
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000389 cfi_info_.reset(compiler_->GetCallFrameInformationInitialization(*this));
Mark Mendellae9fd932014-02-10 16:14:35 -0800390 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700391}
392
Mathieu Chartier193bad92013-08-29 18:46:00 -0700393std::vector<uint8_t>* CompilerDriver::DeduplicateCode(const std::vector<uint8_t>& code) {
394 return dedupe_code_.Add(Thread::Current(), code);
395}
396
397std::vector<uint8_t>* CompilerDriver::DeduplicateMappingTable(const std::vector<uint8_t>& code) {
398 return dedupe_mapping_table_.Add(Thread::Current(), code);
399}
400
401std::vector<uint8_t>* CompilerDriver::DeduplicateVMapTable(const std::vector<uint8_t>& code) {
402 return dedupe_vmap_table_.Add(Thread::Current(), code);
403}
404
405std::vector<uint8_t>* CompilerDriver::DeduplicateGCMap(const std::vector<uint8_t>& code) {
406 return dedupe_gc_map_.Add(Thread::Current(), code);
407}
408
Mark Mendellae9fd932014-02-10 16:14:35 -0800409std::vector<uint8_t>* CompilerDriver::DeduplicateCFIInfo(const std::vector<uint8_t>* cfi_info) {
410 if (cfi_info == nullptr) {
411 return nullptr;
412 }
413 return dedupe_cfi_info_.Add(Thread::Current(), *cfi_info);
414}
415
Brian Carlstrom7940e442013-07-12 13:46:57 -0700416CompilerDriver::~CompilerDriver() {
417 Thread* self = Thread::Current();
418 {
419 MutexLock mu(self, compiled_classes_lock_);
420 STLDeleteValues(&compiled_classes_);
421 }
422 {
423 MutexLock mu(self, compiled_methods_lock_);
424 STLDeleteValues(&compiled_methods_);
425 }
426 {
427 MutexLock mu(self, compiled_methods_lock_);
428 STLDeleteElements(&code_to_patch_);
429 }
430 {
431 MutexLock mu(self, compiled_methods_lock_);
432 STLDeleteElements(&methods_to_patch_);
433 }
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800434 {
435 MutexLock mu(self, compiled_methods_lock_);
436 STLDeleteElements(&classes_to_patch_);
437 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700438 CHECK_PTHREAD_CALL(pthread_key_delete, (tls_key_), "delete tls key");
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000439 compiler_->UnInit(*this);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700440}
441
442CompilerTls* CompilerDriver::GetTls() {
443 // Lazily create thread-local storage
444 CompilerTls* res = static_cast<CompilerTls*>(pthread_getspecific(tls_key_));
445 if (res == NULL) {
446 res = new CompilerTls();
447 CHECK_PTHREAD_CALL(pthread_setspecific, (tls_key_, res), "compiler tls");
448 }
449 return res;
450}
451
Ian Rogersdd7624d2014-03-14 17:43:00 -0700452#define CREATE_TRAMPOLINE(type, abi, offset) \
453 if (instruction_set_is_64_bit_) { \
454 return CreateTrampoline64(instruction_set_, abi, \
455 type ## _ENTRYPOINT_OFFSET(8, offset)); \
456 } else { \
457 return CreateTrampoline32(instruction_set_, abi, \
458 type ## _ENTRYPOINT_OFFSET(4, offset)); \
459 }
460
Ian Rogers848871b2013-08-05 10:56:33 -0700461const std::vector<uint8_t>* CompilerDriver::CreateInterpreterToInterpreterBridge() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700462 CREATE_TRAMPOLINE(INTERPRETER, kInterpreterAbi, pInterpreterToInterpreterBridge)
Ian Rogers848871b2013-08-05 10:56:33 -0700463}
464
465const std::vector<uint8_t>* CompilerDriver::CreateInterpreterToCompiledCodeBridge() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700466 CREATE_TRAMPOLINE(INTERPRETER, kInterpreterAbi, pInterpreterToCompiledCodeBridge)
Ian Rogers848871b2013-08-05 10:56:33 -0700467}
468
469const std::vector<uint8_t>* CompilerDriver::CreateJniDlsymLookup() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700470 CREATE_TRAMPOLINE(JNI, kJniAbi, pDlsymLookup)
Ian Rogers848871b2013-08-05 10:56:33 -0700471}
472
Jeff Hao88474b42013-10-23 16:24:40 -0700473const std::vector<uint8_t>* CompilerDriver::CreatePortableImtConflictTrampoline() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700474 CREATE_TRAMPOLINE(PORTABLE, kPortableAbi, pPortableImtConflictTrampoline)
Jeff Hao88474b42013-10-23 16:24:40 -0700475}
476
Brian Carlstrom7940e442013-07-12 13:46:57 -0700477const std::vector<uint8_t>* CompilerDriver::CreatePortableResolutionTrampoline() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700478 CREATE_TRAMPOLINE(PORTABLE, kPortableAbi, pPortableResolutionTrampoline)
Ian Rogers848871b2013-08-05 10:56:33 -0700479}
480
481const std::vector<uint8_t>* CompilerDriver::CreatePortableToInterpreterBridge() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700482 CREATE_TRAMPOLINE(PORTABLE, kPortableAbi, pPortableToInterpreterBridge)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483}
484
Andreas Gampe2da88232014-02-27 12:26:20 -0800485const std::vector<uint8_t>* CompilerDriver::CreateQuickGenericJniTrampoline() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700486 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickGenericJniTrampoline)
Andreas Gampe2da88232014-02-27 12:26:20 -0800487}
488
Jeff Hao88474b42013-10-23 16:24:40 -0700489const std::vector<uint8_t>* CompilerDriver::CreateQuickImtConflictTrampoline() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700490 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickImtConflictTrampoline)
Jeff Hao88474b42013-10-23 16:24:40 -0700491}
492
Brian Carlstrom7940e442013-07-12 13:46:57 -0700493const std::vector<uint8_t>* CompilerDriver::CreateQuickResolutionTrampoline() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700494 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickResolutionTrampoline)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700495}
496
Ian Rogers848871b2013-08-05 10:56:33 -0700497const std::vector<uint8_t>* CompilerDriver::CreateQuickToInterpreterBridge() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700498 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickToInterpreterBridge)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499}
Ian Rogersdd7624d2014-03-14 17:43:00 -0700500#undef CREATE_TRAMPOLINE
Brian Carlstrom7940e442013-07-12 13:46:57 -0700501
502void CompilerDriver::CompileAll(jobject class_loader,
Brian Carlstrom45602482013-07-21 22:07:55 -0700503 const std::vector<const DexFile*>& dex_files,
Ian Rogers3d504072014-03-01 09:16:49 -0800504 TimingLogger* timings) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505 DCHECK(!Runtime::Current()->IsStarted());
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -0800506 UniquePtr<ThreadPool> thread_pool(new ThreadPool("Compiler driver thread pool", thread_count_ - 1));
Ian Rogers3d504072014-03-01 09:16:49 -0800507 PreCompile(class_loader, dex_files, thread_pool.get(), timings);
508 Compile(class_loader, dex_files, thread_pool.get(), timings);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 if (dump_stats_) {
510 stats_->Dump();
511 }
512}
513
Mathieu Chartier590fee92013-09-13 13:46:47 -0700514static DexToDexCompilationLevel GetDexToDexCompilationlevel(
Ian Rogers98379392014-02-24 16:53:16 -0800515 Thread* self, SirtRef<mirror::ClassLoader>& class_loader, const DexFile& dex_file,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700516 const DexFile::ClassDef& class_def) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 const char* descriptor = dex_file.GetClassDescriptor(class_def);
518 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -0800519 mirror::Class* klass = class_linker->FindClass(self, descriptor, class_loader);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700520 if (klass == NULL) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700521 CHECK(self->IsExceptionPending());
522 self->ClearException();
Sebastien Hertz75021222013-07-16 18:34:50 +0200523 return kDontDexToDexCompile;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700524 }
Sebastien Hertz75021222013-07-16 18:34:50 +0200525 // The verifier can only run on "quick" instructions at runtime (see usage of
526 // FindAccessedFieldAtDexPc and FindInvokedMethodAtDexPc in ThrowNullPointerExceptionFromDexPC
527 // function). Since image classes can be verified again while compiling an application,
528 // we must prevent the DEX-to-DEX compiler from introducing them.
529 // TODO: find a way to enable "quick" instructions for image classes and remove this check.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700530 bool compiling_image_classes = class_loader.get() == nullptr;
Sebastien Hertz75021222013-07-16 18:34:50 +0200531 if (compiling_image_classes) {
532 return kRequired;
533 } else if (klass->IsVerified()) {
534 // Class is verified so we can enable DEX-to-DEX compilation for performance.
535 return kOptimize;
536 } else if (klass->IsCompileTimeVerified()) {
537 // Class verification has soft-failed. Anyway, ensure at least correctness.
538 DCHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime);
539 return kRequired;
540 } else {
541 // Class verification has failed: do not run DEX-to-DEX compilation.
542 return kDontDexToDexCompile;
543 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700544}
545
Ian Rogers3d504072014-03-01 09:16:49 -0800546void CompilerDriver::CompileOne(mirror::ArtMethod* method, TimingLogger* timings) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700547 DCHECK(!Runtime::Current()->IsStarted());
548 Thread* self = Thread::Current();
549 jobject jclass_loader;
550 const DexFile* dex_file;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700551 uint16_t class_def_idx;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800552 uint32_t method_idx = method->GetDexMethodIndex();
553 uint32_t access_flags = method->GetAccessFlags();
554 InvokeType invoke_type = method->GetInvokeType();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700555 {
556 ScopedObjectAccessUnchecked soa(self);
557 ScopedLocalRef<jobject>
558 local_class_loader(soa.Env(),
559 soa.AddLocalReference<jobject>(method->GetDeclaringClass()->GetClassLoader()));
560 jclass_loader = soa.Env()->NewGlobalRef(local_class_loader.get());
561 // Find the dex_file
562 MethodHelper mh(method);
563 dex_file = &mh.GetDexFile();
564 class_def_idx = mh.GetClassDefIndex();
565 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800566 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700567 self->TransitionFromRunnableToSuspended(kNative);
568
569 std::vector<const DexFile*> dex_files;
570 dex_files.push_back(dex_file);
571
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -0800572 UniquePtr<ThreadPool> thread_pool(new ThreadPool("Compiler driver thread pool", 0U));
Ian Rogers3d504072014-03-01 09:16:49 -0800573 PreCompile(jclass_loader, dex_files, thread_pool.get(), timings);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574
Brian Carlstrom7940e442013-07-12 13:46:57 -0700575 // Can we run DEX-to-DEX compiler on this class ?
Sebastien Hertz75021222013-07-16 18:34:50 +0200576 DexToDexCompilationLevel dex_to_dex_compilation_level = kDontDexToDexCompile;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700577 {
578 ScopedObjectAccess soa(Thread::Current());
579 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700580 SirtRef<mirror::ClassLoader> class_loader(soa.Self(),
581 soa.Decode<mirror::ClassLoader*>(jclass_loader));
Ian Rogers98379392014-02-24 16:53:16 -0800582 dex_to_dex_compilation_level = GetDexToDexCompilationlevel(self, class_loader, *dex_file,
583 class_def);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700584 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800585 CompileMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx, jclass_loader,
586 *dex_file, dex_to_dex_compilation_level);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700587
588 self->GetJniEnv()->DeleteGlobalRef(jclass_loader);
589
590 self->TransitionFromSuspendedToRunnable();
591}
592
593void CompilerDriver::Resolve(jobject class_loader, const std::vector<const DexFile*>& dex_files,
Ian Rogers3d504072014-03-01 09:16:49 -0800594 ThreadPool* thread_pool, TimingLogger* timings) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700595 for (size_t i = 0; i != dex_files.size(); ++i) {
596 const DexFile* dex_file = dex_files[i];
597 CHECK(dex_file != NULL);
598 ResolveDexFile(class_loader, *dex_file, thread_pool, timings);
599 }
600}
601
602void CompilerDriver::PreCompile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
Ian Rogers3d504072014-03-01 09:16:49 -0800603 ThreadPool* thread_pool, TimingLogger* timings) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700604 LoadImageClasses(timings);
605
Jeff Hao4a200f52014-04-01 14:58:49 -0700606 if (!compiler_options_->IsVerificationEnabled()) {
607 VLOG(compiler) << "Verify none mode specified, skipping pre-compilation";
608 return;
609 }
610
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 Resolve(class_loader, dex_files, thread_pool, timings);
612
613 Verify(class_loader, dex_files, thread_pool, timings);
614
615 InitializeClasses(class_loader, dex_files, thread_pool, timings);
616
617 UpdateImageClasses(timings);
618}
619
Ian Rogersdfb325e2013-10-30 01:00:44 -0700620bool CompilerDriver::IsImageClass(const char* descriptor) const {
Ian Rogerse6bb3b22013-08-19 21:51:45 -0700621 if (!IsImage()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700622 return true;
Ian Rogerse6bb3b22013-08-19 21:51:45 -0700623 } else {
Ian Rogersdfb325e2013-10-30 01:00:44 -0700624 return image_classes_->find(descriptor) != image_classes_->end();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700625 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700626}
627
628static void ResolveExceptionsForMethod(MethodHelper* mh,
629 std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve)
630 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
631 const DexFile::CodeItem* code_item = mh->GetCodeItem();
632 if (code_item == NULL) {
633 return; // native or abstract method
634 }
635 if (code_item->tries_size_ == 0) {
636 return; // nothing to process
637 }
638 const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
639 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
640 for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
641 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
642 bool has_catch_all = false;
643 if (encoded_catch_handler_size <= 0) {
644 encoded_catch_handler_size = -encoded_catch_handler_size;
645 has_catch_all = true;
646 }
647 for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
648 uint16_t encoded_catch_handler_handlers_type_idx =
649 DecodeUnsignedLeb128(&encoded_catch_handler_list);
650 // Add to set of types to resolve if not already in the dex cache resolved types
651 if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
652 exceptions_to_resolve.insert(
653 std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx,
654 &mh->GetDexFile()));
655 }
656 // ignore address associated with catch handler
657 DecodeUnsignedLeb128(&encoded_catch_handler_list);
658 }
659 if (has_catch_all) {
660 // ignore catch all address
661 DecodeUnsignedLeb128(&encoded_catch_handler_list);
662 }
663 }
664}
665
666static bool ResolveCatchBlockExceptionsClassVisitor(mirror::Class* c, void* arg)
667 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
668 std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve =
669 reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg);
670 MethodHelper mh;
671 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700672 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700673 mh.ChangeMethod(m);
674 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
675 }
676 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700677 mirror::ArtMethod* m = c->GetDirectMethod(i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700678 mh.ChangeMethod(m);
679 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
680 }
681 return true;
682}
683
684static bool RecordImageClassesVisitor(mirror::Class* klass, void* arg)
685 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
686 CompilerDriver::DescriptorSet* image_classes =
687 reinterpret_cast<CompilerDriver::DescriptorSet*>(arg);
688 image_classes->insert(ClassHelper(klass).GetDescriptor());
689 return true;
690}
691
692// Make a list of descriptors for classes to include in the image
Ian Rogers3d504072014-03-01 09:16:49 -0800693void CompilerDriver::LoadImageClasses(TimingLogger* timings)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700694 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogerse6bb3b22013-08-19 21:51:45 -0700695 if (!IsImage()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700696 return;
697 }
698
Ian Rogers3d504072014-03-01 09:16:49 -0800699 timings->NewSplit("LoadImageClasses");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700700 // Make a first class to load all classes explicitly listed in the file
701 Thread* self = Thread::Current();
702 ScopedObjectAccess soa(self);
703 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700704 for (auto it = image_classes_->begin(), end = image_classes_->end(); it != end;) {
Vladimir Markoe9c36b32013-11-21 15:49:16 +0000705 const std::string& descriptor(*it);
Ian Rogers98379392014-02-24 16:53:16 -0800706 SirtRef<mirror::Class> klass(self, class_linker->FindSystemClass(self, descriptor.c_str()));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700707 if (klass.get() == NULL) {
Ian Rogerse6bb3b22013-08-19 21:51:45 -0700708 VLOG(compiler) << "Failed to find class " << descriptor;
Vladimir Markoe9c36b32013-11-21 15:49:16 +0000709 image_classes_->erase(it++);
Ian Rogersa436fde2013-08-27 23:34:06 -0700710 self->ClearException();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700711 } else {
712 ++it;
713 }
714 }
715
716 // Resolve exception classes referenced by the loaded classes. The catch logic assumes
717 // exceptions are resolved by the verifier when there is a catch block in an interested method.
718 // Do this here so that exception classes appear to have been specified image classes.
719 std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types;
720 SirtRef<mirror::Class> java_lang_Throwable(self,
Ian Rogers98379392014-02-24 16:53:16 -0800721 class_linker->FindSystemClass(self, "Ljava/lang/Throwable;"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700722 do {
723 unresolved_exception_types.clear();
724 class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor,
725 &unresolved_exception_types);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700726 for (const std::pair<uint16_t, const DexFile*>& exception_type : unresolved_exception_types) {
727 uint16_t exception_type_idx = exception_type.first;
728 const DexFile* dex_file = exception_type.second;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700729 SirtRef<mirror::DexCache> dex_cache(self, class_linker->FindDexCache(*dex_file));
730 SirtRef<mirror::ClassLoader> class_loader(self, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700731 SirtRef<mirror::Class> klass(self, class_linker->ResolveType(*dex_file, exception_type_idx,
732 dex_cache, class_loader));
733 if (klass.get() == NULL) {
734 const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
735 const char* descriptor = dex_file->GetTypeDescriptor(type_id);
736 LOG(FATAL) << "Failed to resolve class " << descriptor;
737 }
738 DCHECK(java_lang_Throwable->IsAssignableFrom(klass.get()));
739 }
740 // Resolving exceptions may load classes that reference more exceptions, iterate until no
741 // more are found
742 } while (!unresolved_exception_types.empty());
743
744 // We walk the roots looking for classes so that we'll pick up the
745 // above classes plus any classes them depend on such super
746 // classes, interfaces, and the required ClassLinker roots.
747 class_linker->VisitClasses(RecordImageClassesVisitor, image_classes_.get());
748
749 CHECK_NE(image_classes_->size(), 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700750}
751
752static void MaybeAddToImageClasses(mirror::Class* klass, CompilerDriver::DescriptorSet* image_classes)
753 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
754 while (!klass->IsObjectClass()) {
755 ClassHelper kh(klass);
756 const char* descriptor = kh.GetDescriptor();
757 std::pair<CompilerDriver::DescriptorSet::iterator, bool> result =
758 image_classes->insert(descriptor);
759 if (result.second) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700760 VLOG(compiler) << "Adding " << descriptor << " to image classes";
Brian Carlstrom7940e442013-07-12 13:46:57 -0700761 } else {
762 return;
763 }
764 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
765 MaybeAddToImageClasses(kh.GetDirectInterface(i), image_classes);
766 }
767 if (klass->IsArrayClass()) {
768 MaybeAddToImageClasses(klass->GetComponentType(), image_classes);
769 }
770 klass = klass->GetSuperClass();
771 }
772}
773
774void CompilerDriver::FindClinitImageClassesCallback(mirror::Object* object, void* arg) {
775 DCHECK(object != NULL);
776 DCHECK(arg != NULL);
777 CompilerDriver* compiler_driver = reinterpret_cast<CompilerDriver*>(arg);
778 MaybeAddToImageClasses(object->GetClass(), compiler_driver->image_classes_.get());
779}
780
Ian Rogers3d504072014-03-01 09:16:49 -0800781void CompilerDriver::UpdateImageClasses(TimingLogger* timings) {
Ian Rogerse6bb3b22013-08-19 21:51:45 -0700782 if (IsImage()) {
Ian Rogers3d504072014-03-01 09:16:49 -0800783 timings->NewSplit("UpdateImageClasses");
Ian Rogerse6bb3b22013-08-19 21:51:45 -0700784
785 // Update image_classes_ with classes for objects created by <clinit> methods.
786 Thread* self = Thread::Current();
787 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
788 gc::Heap* heap = Runtime::Current()->GetHeap();
789 // TODO: Image spaces only?
Mathieu Chartier590fee92013-09-13 13:46:47 -0700790 ScopedObjectAccess soa(Thread::Current());
Ian Rogerse6bb3b22013-08-19 21:51:45 -0700791 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700792 heap->VisitObjects(FindClinitImageClassesCallback, this);
Ian Rogerse6bb3b22013-08-19 21:51:45 -0700793 self->EndAssertNoThreadSuspension(old_cause);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700794 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700795}
796
Mathieu Chartier590fee92013-09-13 13:46:47 -0700797bool CompilerDriver::CanAssumeTypeIsPresentInDexCache(const DexFile& dex_file, uint32_t type_idx) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700798 if (IsImage() &&
Ian Rogersdfb325e2013-10-30 01:00:44 -0700799 IsImageClass(dex_file.StringDataByIdx(dex_file.GetTypeId(type_idx).descriptor_idx_))) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700800 if (kIsDebugBuild) {
801 ScopedObjectAccess soa(Thread::Current());
802 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
803 mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
804 CHECK(resolved_class != NULL);
805 }
806 stats_->TypeInDexCache();
807 return true;
808 } else {
809 stats_->TypeNotInDexCache();
810 return false;
811 }
812}
813
814bool CompilerDriver::CanAssumeStringIsPresentInDexCache(const DexFile& dex_file,
815 uint32_t string_idx) {
816 // See also Compiler::ResolveDexFile
817
818 bool result = false;
819 if (IsImage()) {
820 // We resolve all const-string strings when building for the image.
821 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700822 SirtRef<mirror::DexCache> dex_cache(soa.Self(), Runtime::Current()->GetClassLinker()->FindDexCache(dex_file));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700823 Runtime::Current()->GetClassLinker()->ResolveString(dex_file, string_idx, dex_cache);
824 result = true;
825 }
826 if (result) {
827 stats_->StringInDexCache();
828 } else {
829 stats_->StringNotInDexCache();
830 }
831 return result;
832}
833
834bool CompilerDriver::CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file,
835 uint32_t type_idx,
836 bool* type_known_final, bool* type_known_abstract,
837 bool* equals_referrers_class) {
838 if (type_known_final != NULL) {
839 *type_known_final = false;
840 }
841 if (type_known_abstract != NULL) {
842 *type_known_abstract = false;
843 }
844 if (equals_referrers_class != NULL) {
845 *equals_referrers_class = false;
846 }
847 ScopedObjectAccess soa(Thread::Current());
848 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
849 // Get type from dex cache assuming it was populated by the verifier
850 mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
851 if (resolved_class == NULL) {
852 stats_->TypeNeedsAccessCheck();
853 return false; // Unknown class needs access checks.
854 }
855 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
856 if (equals_referrers_class != NULL) {
857 *equals_referrers_class = (method_id.class_idx_ == type_idx);
858 }
859 mirror::Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
860 if (referrer_class == NULL) {
861 stats_->TypeNeedsAccessCheck();
862 return false; // Incomplete referrer knowledge needs access check.
863 }
864 // Perform access check, will return true if access is ok or false if we're going to have to
865 // check this at runtime (for example for class loaders).
866 bool result = referrer_class->CanAccess(resolved_class);
867 if (result) {
868 stats_->TypeDoesntNeedAccessCheck();
869 if (type_known_final != NULL) {
870 *type_known_final = resolved_class->IsFinal() && !resolved_class->IsArrayClass();
871 }
872 if (type_known_abstract != NULL) {
873 *type_known_abstract = resolved_class->IsAbstract() && !resolved_class->IsArrayClass();
874 }
875 } else {
876 stats_->TypeNeedsAccessCheck();
877 }
878 return result;
879}
880
881bool CompilerDriver::CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx,
882 const DexFile& dex_file,
883 uint32_t type_idx) {
884 ScopedObjectAccess soa(Thread::Current());
885 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
886 // Get type from dex cache assuming it was populated by the verifier.
887 mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
888 if (resolved_class == NULL) {
889 stats_->TypeNeedsAccessCheck();
890 return false; // Unknown class needs access checks.
891 }
892 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
893 mirror::Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
894 if (referrer_class == NULL) {
895 stats_->TypeNeedsAccessCheck();
896 return false; // Incomplete referrer knowledge needs access check.
897 }
898 // Perform access and instantiable checks, will return true if access is ok or false if we're
899 // going to have to check this at runtime (for example for class loaders).
900 bool result = referrer_class->CanAccess(resolved_class) && resolved_class->IsInstantiable();
901 if (result) {
902 stats_->TypeDoesntNeedAccessCheck();
903 } else {
904 stats_->TypeNeedsAccessCheck();
905 }
906 return result;
907}
908
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800909bool CompilerDriver::CanEmbedTypeInCode(const DexFile& dex_file, uint32_t type_idx,
910 bool* is_type_initialized, bool* use_direct_type_ptr,
911 uintptr_t* direct_type_ptr) {
912 ScopedObjectAccess soa(Thread::Current());
913 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
914 mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
915 if (resolved_class == nullptr) {
916 return false;
917 }
918 const bool compiling_boot = Runtime::Current()->GetHeap()->IsCompilingBoot();
919 if (compiling_boot) {
920 // boot -> boot class pointers.
921 // True if the class is in the image at boot compiling time.
922 const bool is_image_class = IsImage() && IsImageClass(
923 dex_file.StringDataByIdx(dex_file.GetTypeId(type_idx).descriptor_idx_));
924 // True if pc relative load works.
925 const bool support_boot_image_fixup = GetSupportBootImageFixup();
926 if (is_image_class && support_boot_image_fixup) {
927 *is_type_initialized = resolved_class->IsInitialized();
928 *use_direct_type_ptr = false;
929 *direct_type_ptr = 0;
930 return true;
931 } else {
932 return false;
933 }
934 } else {
935 // True if the class is in the image at app compiling time.
936 const bool class_in_image =
937 Runtime::Current()->GetHeap()->FindSpaceFromObject(resolved_class, false)->IsImageSpace();
938 if (class_in_image) {
939 // boot -> app class pointers.
940 *is_type_initialized = resolved_class->IsInitialized();
941 *use_direct_type_ptr = true;
942 *direct_type_ptr = reinterpret_cast<uintptr_t>(resolved_class);
943 return true;
944 } else {
945 // app -> app class pointers.
946 // Give up because app does not have an image and class
947 // isn't created at compile time. TODO: implement this
948 // if/when each app gets an image.
949 return false;
950 }
951 }
952}
953
Vladimir Markobe0e5462014-02-26 11:24:15 +0000954void CompilerDriver::ProcessedInstanceField(bool resolved) {
955 if (!resolved) {
956 stats_->UnresolvedInstanceField();
957 } else {
958 stats_->ResolvedInstanceField();
959 }
960}
961
962void CompilerDriver::ProcessedStaticField(bool resolved, bool local) {
963 if (!resolved) {
964 stats_->UnresolvedStaticField();
965 } else if (local) {
966 stats_->ResolvedLocalStaticField();
967 } else {
968 stats_->ResolvedStaticField();
969 }
970}
971
Vladimir Markof096aad2014-01-23 15:51:58 +0000972void CompilerDriver::ProcessedInvoke(InvokeType invoke_type, int flags) {
973 stats_->ProcessedInvoke(invoke_type, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700974}
975
976bool CompilerDriver::ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000977 bool is_put, MemberOffset* field_offset,
978 bool* is_volatile) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700979 ScopedObjectAccess soa(Thread::Current());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000980 // Try to resolve the field and compiling method's class.
981 mirror::ArtField* resolved_field;
982 mirror::Class* referrer_class;
983 mirror::DexCache* dex_cache;
984 {
985 SirtRef<mirror::DexCache> dex_cache_sirt(soa.Self(),
986 mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile()));
987 SirtRef<mirror::ClassLoader> class_loader_sirt(soa.Self(),
988 soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader()));
989 SirtRef<mirror::ArtField> resolved_field_sirt(soa.Self(),
990 ResolveField(soa, dex_cache_sirt, class_loader_sirt, mUnit, field_idx, false));
991 referrer_class = (resolved_field_sirt.get() != nullptr)
992 ? ResolveCompilingMethodsClass(soa, dex_cache_sirt, class_loader_sirt, mUnit) : nullptr;
993 resolved_field = resolved_field_sirt.get();
994 dex_cache = dex_cache_sirt.get();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700995 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000996 bool result = false;
997 if (resolved_field != nullptr && referrer_class != nullptr) {
998 *is_volatile = IsFieldVolatile(resolved_field);
999 std::pair<bool, bool> fast_path = IsFastInstanceField(
1000 dex_cache, referrer_class, resolved_field, field_idx, field_offset);
1001 result = is_put ? fast_path.second : fast_path.first;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001002 }
Vladimir Markobe0e5462014-02-26 11:24:15 +00001003 if (!result) {
1004 // Conservative defaults.
1005 *is_volatile = true;
1006 *field_offset = MemberOffset(static_cast<size_t>(-1));
1007 }
1008 ProcessedInstanceField(result);
1009 return result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001010}
1011
1012bool CompilerDriver::ComputeStaticFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit,
Vladimir Markobe0e5462014-02-26 11:24:15 +00001013 bool is_put, MemberOffset* field_offset,
1014 uint32_t* storage_index, bool* is_referrers_class,
1015 bool* is_volatile, bool* is_initialized) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 ScopedObjectAccess soa(Thread::Current());
Vladimir Markobe0e5462014-02-26 11:24:15 +00001017 // Try to resolve the field and compiling method's class.
1018 mirror::ArtField* resolved_field;
1019 mirror::Class* referrer_class;
1020 mirror::DexCache* dex_cache;
1021 {
1022 SirtRef<mirror::DexCache> dex_cache_sirt(soa.Self(),
1023 mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile()));
1024 SirtRef<mirror::ClassLoader> class_loader_sirt(soa.Self(),
1025 soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader()));
1026 SirtRef<mirror::ArtField> resolved_field_sirt(soa.Self(),
1027 ResolveField(soa, dex_cache_sirt, class_loader_sirt, mUnit, field_idx, true));
1028 referrer_class = (resolved_field_sirt.get() != nullptr)
1029 ? ResolveCompilingMethodsClass(soa, dex_cache_sirt, class_loader_sirt, mUnit) : nullptr;
1030 resolved_field = resolved_field_sirt.get();
1031 dex_cache = dex_cache_sirt.get();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001032 }
Vladimir Markobe0e5462014-02-26 11:24:15 +00001033 bool result = false;
1034 if (resolved_field != nullptr && referrer_class != nullptr) {
1035 *is_volatile = IsFieldVolatile(resolved_field);
1036 std::pair<bool, bool> fast_path = IsFastStaticField(
1037 dex_cache, referrer_class, resolved_field, field_idx, field_offset,
1038 storage_index, is_referrers_class, is_initialized);
1039 result = is_put ? fast_path.second : fast_path.first;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001040 }
Vladimir Markobe0e5462014-02-26 11:24:15 +00001041 if (!result) {
1042 // Conservative defaults.
1043 *is_volatile = true;
1044 *field_offset = MemberOffset(static_cast<size_t>(-1));
1045 *storage_index = -1;
1046 *is_referrers_class = false;
1047 *is_initialized = false;
1048 }
1049 ProcessedStaticField(result, *is_referrers_class);
1050 return result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001051}
1052
Ian Rogers83883d72013-10-21 21:07:24 -07001053void CompilerDriver::GetCodeAndMethodForDirectCall(InvokeType* type, InvokeType sharp_type,
1054 bool no_guarantee_of_dex_cache_entry,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001055 mirror::Class* referrer_class,
Brian Carlstromea46f952013-07-30 01:26:50 -07001056 mirror::ArtMethod* method,
Vladimir Markof096aad2014-01-23 15:51:58 +00001057 int* stats_flags,
Ian Rogers83883d72013-10-21 21:07:24 -07001058 MethodReference* target_method,
Ian Rogers65ec92c2013-09-06 10:49:58 -07001059 uintptr_t* direct_code,
1060 uintptr_t* direct_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001061 // For direct and static methods compute possible direct_code and direct_method values, ie
1062 // an address for the Method* being invoked and an address of the code for that Method*.
1063 // For interface calls compute a value for direct_method that is the interface method being
1064 // invoked, so this can be passed to the out-of-line runtime support code.
Ian Rogers65ec92c2013-09-06 10:49:58 -07001065 *direct_code = 0;
1066 *direct_method = 0;
Ian Rogers83883d72013-10-21 21:07:24 -07001067 bool use_dex_cache = false;
Mathieu Chartier590fee92013-09-13 13:46:47 -07001068 const bool compiling_boot = Runtime::Current()->GetHeap()->IsCompilingBoot();
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001069 if (compiler_->IsPortable()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001070 if (sharp_type != kStatic && sharp_type != kDirect) {
1071 return;
1072 }
Ian Rogers83883d72013-10-21 21:07:24 -07001073 use_dex_cache = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001074 } else {
Jeff Hao88474b42013-10-23 16:24:40 -07001075 if (sharp_type != kStatic && sharp_type != kDirect) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001076 return;
1077 }
Ian Rogers83883d72013-10-21 21:07:24 -07001078 // TODO: support patching on all architectures.
1079 use_dex_cache = compiling_boot && !support_boot_image_fixup_;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080 }
Ian Rogers83883d72013-10-21 21:07:24 -07001081 bool method_code_in_boot = (method->GetDeclaringClass()->GetClassLoader() == nullptr);
1082 if (!use_dex_cache) {
1083 if (!method_code_in_boot) {
1084 use_dex_cache = true;
1085 } else {
1086 bool has_clinit_trampoline =
1087 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
1088 if (has_clinit_trampoline && (method->GetDeclaringClass() != referrer_class)) {
1089 // Ensure we run the clinit trampoline unless we are invoking a static method in the same
1090 // class.
1091 use_dex_cache = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001092 }
1093 }
Ian Rogers83883d72013-10-21 21:07:24 -07001094 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001095 if (method_code_in_boot) {
1096 *stats_flags |= kFlagDirectCallToBoot | kFlagDirectMethodToBoot;
Ian Rogers83883d72013-10-21 21:07:24 -07001097 }
1098 if (!use_dex_cache && compiling_boot) {
1099 MethodHelper mh(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -07001100 if (!IsImageClass(mh.GetDeclaringClassDescriptor())) {
Ian Rogers83883d72013-10-21 21:07:24 -07001101 // We can only branch directly to Methods that are resolved in the DexCache.
1102 // Otherwise we won't invoke the resolution trampoline.
1103 use_dex_cache = true;
1104 }
1105 }
1106 // The method is defined not within this dex file. We need a dex cache slot within the current
1107 // dex file or direct pointers.
1108 bool must_use_direct_pointers = false;
1109 if (target_method->dex_file == method->GetDeclaringClass()->GetDexCache()->GetDexFile()) {
1110 target_method->dex_method_index = method->GetDexMethodIndex();
1111 } else {
Ian Rogers83883d72013-10-21 21:07:24 -07001112 if (no_guarantee_of_dex_cache_entry) {
1113 // See if the method is also declared in this dex cache.
1114 uint32_t dex_method_idx = MethodHelper(method).FindDexMethodIndexInOtherDexFile(
Vladimir Markobbcc0c02014-02-03 14:08:42 +00001115 *target_method->dex_file, target_method->dex_method_index);
Ian Rogers83883d72013-10-21 21:07:24 -07001116 if (dex_method_idx != DexFile::kDexNoIndex) {
1117 target_method->dex_method_index = dex_method_idx;
1118 } else {
Jeff Hao49161ce2014-03-12 11:05:25 -07001119 if (compiling_boot) {
1120 target_method->dex_method_index = method->GetDexMethodIndex();
1121 target_method->dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1122 }
Ian Rogers83883d72013-10-21 21:07:24 -07001123 must_use_direct_pointers = true;
1124 }
1125 }
1126 }
1127 if (use_dex_cache) {
1128 if (must_use_direct_pointers) {
1129 // Fail. Test above showed the only safe dispatch was via the dex cache, however, the direct
1130 // pointers are required as the dex cache lacks an appropriate entry.
1131 VLOG(compiler) << "Dex cache devirtualization failed for: " << PrettyMethod(method);
1132 } else {
1133 *type = sharp_type;
1134 }
1135 } else {
1136 if (compiling_boot) {
1137 *type = sharp_type;
1138 *direct_method = -1;
Jeff Hao88474b42013-10-23 16:24:40 -07001139 *direct_code = -1;
Ian Rogers83883d72013-10-21 21:07:24 -07001140 } else {
1141 bool method_in_image =
1142 Runtime::Current()->GetHeap()->FindSpaceFromObject(method, false)->IsImageSpace();
1143 if (method_in_image) {
Jeff Hao88474b42013-10-23 16:24:40 -07001144 CHECK(!method->IsAbstract());
Ian Rogers83883d72013-10-21 21:07:24 -07001145 *type = sharp_type;
1146 *direct_method = reinterpret_cast<uintptr_t>(method);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001147 *direct_code = compiler_->GetEntryPointOf(method);
Ian Rogers83883d72013-10-21 21:07:24 -07001148 target_method->dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1149 target_method->dex_method_index = method->GetDexMethodIndex();
1150 } else if (!must_use_direct_pointers) {
1151 // Set the code and rely on the dex cache for the method.
1152 *type = sharp_type;
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001153 *direct_code = compiler_->GetEntryPointOf(method);
Ian Rogers83883d72013-10-21 21:07:24 -07001154 } else {
1155 // Direct pointers were required but none were available.
1156 VLOG(compiler) << "Dex cache devirtualization failed for: " << PrettyMethod(method);
1157 }
1158 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001159 }
1160}
1161
1162bool CompilerDriver::ComputeInvokeInfo(const DexCompilationUnit* mUnit, const uint32_t dex_pc,
Ian Rogers65ec92c2013-09-06 10:49:58 -07001163 bool update_stats, bool enable_devirtualization,
1164 InvokeType* invoke_type, MethodReference* target_method,
1165 int* vtable_idx, uintptr_t* direct_code,
1166 uintptr_t* direct_method) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001167 InvokeType orig_invoke_type = *invoke_type;
1168 int stats_flags = 0;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001169 ScopedObjectAccess soa(Thread::Current());
Vladimir Markof096aad2014-01-23 15:51:58 +00001170 // Try to resolve the method and compiling method's class.
1171 mirror::ArtMethod* resolved_method;
1172 mirror::Class* referrer_class;
1173 SirtRef<mirror::DexCache> dex_cache(soa.Self(),
1174 mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile()));
1175 SirtRef<mirror::ClassLoader> class_loader(soa.Self(),
1176 soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader()));
1177 {
1178 uint32_t method_idx = target_method->dex_method_index;
1179 SirtRef<mirror::ArtMethod> resolved_method_sirt(soa.Self(),
1180 ResolveMethod(soa, dex_cache, class_loader, mUnit, method_idx, orig_invoke_type));
1181 referrer_class = (resolved_method_sirt.get() != nullptr)
1182 ? ResolveCompilingMethodsClass(soa, dex_cache, class_loader, mUnit) : nullptr;
1183 resolved_method = resolved_method_sirt.get();
1184 }
1185 bool result = false;
1186 if (resolved_method != nullptr) {
1187 *vtable_idx = GetResolvedMethodVTableIndex(resolved_method, orig_invoke_type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001188
Vladimir Markof096aad2014-01-23 15:51:58 +00001189 if (enable_devirtualization) {
1190 DCHECK(mUnit->GetVerifiedMethod() != nullptr);
1191 const MethodReference* devirt_target = mUnit->GetVerifiedMethod()->GetDevirtTarget(dex_pc);
1192
1193 stats_flags = IsFastInvoke(
1194 soa, dex_cache, class_loader, mUnit, referrer_class, resolved_method,
1195 invoke_type, target_method, devirt_target, direct_code, direct_method);
1196 result = stats_flags != 0;
1197 } else {
1198 // Devirtualization not enabled. Inline IsFastInvoke(), dropping the devirtualization parts.
1199 if (UNLIKELY(referrer_class == nullptr) ||
1200 UNLIKELY(!referrer_class->CanAccessResolvedMethod(resolved_method->GetDeclaringClass(),
1201 resolved_method, dex_cache.get(),
1202 target_method->dex_method_index)) ||
1203 *invoke_type == kSuper) {
1204 // Slow path. (Without devirtualization, all super calls go slow path as well.)
1205 } else {
1206 // Sharpening failed so generate a regular resolved method dispatch.
1207 stats_flags = kFlagMethodResolved;
1208 GetCodeAndMethodForDirectCall(invoke_type, *invoke_type, false, referrer_class, resolved_method,
1209 &stats_flags, target_method, direct_code, direct_method);
1210 result = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001211 }
1212 }
1213 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001214 if (!result) {
1215 // Conservative defaults.
1216 *vtable_idx = -1;
1217 *direct_code = 0u;
1218 *direct_method = 0u;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001219 }
1220 if (update_stats) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001221 ProcessedInvoke(orig_invoke_type, stats_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001222 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001223 return result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001224}
1225
Vladimir Marko2730db02014-01-27 11:15:17 +00001226const VerifiedMethod* CompilerDriver::GetVerifiedMethod(const DexFile* dex_file,
1227 uint32_t method_idx) const {
1228 MethodReference ref(dex_file, method_idx);
1229 return verification_results_->GetVerifiedMethod(ref);
1230}
1231
1232bool CompilerDriver::IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc) {
1233 DCHECK(mUnit->GetVerifiedMethod() != nullptr);
1234 bool result = mUnit->GetVerifiedMethod()->IsSafeCast(dex_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001235 if (result) {
1236 stats_->SafeCast();
1237 } else {
1238 stats_->NotASafeCast();
1239 }
1240 return result;
1241}
1242
Brian Carlstrom7940e442013-07-12 13:46:57 -07001243void CompilerDriver::AddCodePatch(const DexFile* dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001244 uint16_t referrer_class_def_idx,
1245 uint32_t referrer_method_idx,
1246 InvokeType referrer_invoke_type,
1247 uint32_t target_method_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -07001248 const DexFile* target_dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001249 InvokeType target_invoke_type,
1250 size_t literal_offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001251 MutexLock mu(Thread::Current(), compiled_methods_lock_);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001252 code_to_patch_.push_back(new CallPatchInformation(dex_file,
1253 referrer_class_def_idx,
1254 referrer_method_idx,
1255 referrer_invoke_type,
1256 target_method_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -07001257 target_dex_file,
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001258 target_invoke_type,
1259 literal_offset));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001260}
Mark Mendell55d0eac2014-02-06 11:02:52 -08001261void CompilerDriver::AddRelativeCodePatch(const DexFile* dex_file,
1262 uint16_t referrer_class_def_idx,
1263 uint32_t referrer_method_idx,
1264 InvokeType referrer_invoke_type,
1265 uint32_t target_method_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -07001266 const DexFile* target_dex_file,
Mark Mendell55d0eac2014-02-06 11:02:52 -08001267 InvokeType target_invoke_type,
1268 size_t literal_offset,
1269 int32_t pc_relative_offset) {
1270 MutexLock mu(Thread::Current(), compiled_methods_lock_);
1271 code_to_patch_.push_back(new RelativeCallPatchInformation(dex_file,
1272 referrer_class_def_idx,
1273 referrer_method_idx,
1274 referrer_invoke_type,
1275 target_method_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -07001276 target_dex_file,
Mark Mendell55d0eac2014-02-06 11:02:52 -08001277 target_invoke_type,
1278 literal_offset,
1279 pc_relative_offset));
1280}
Brian Carlstrom7940e442013-07-12 13:46:57 -07001281void CompilerDriver::AddMethodPatch(const DexFile* dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001282 uint16_t referrer_class_def_idx,
1283 uint32_t referrer_method_idx,
1284 InvokeType referrer_invoke_type,
1285 uint32_t target_method_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -07001286 const DexFile* target_dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001287 InvokeType target_invoke_type,
1288 size_t literal_offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001289 MutexLock mu(Thread::Current(), compiled_methods_lock_);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001290 methods_to_patch_.push_back(new CallPatchInformation(dex_file,
1291 referrer_class_def_idx,
1292 referrer_method_idx,
1293 referrer_invoke_type,
1294 target_method_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -07001295 target_dex_file,
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001296 target_invoke_type,
1297 literal_offset));
1298}
1299void CompilerDriver::AddClassPatch(const DexFile* dex_file,
1300 uint16_t referrer_class_def_idx,
1301 uint32_t referrer_method_idx,
1302 uint32_t target_type_idx,
1303 size_t literal_offset) {
1304 MutexLock mu(Thread::Current(), compiled_methods_lock_);
1305 classes_to_patch_.push_back(new TypePatchInformation(dex_file,
1306 referrer_class_def_idx,
1307 referrer_method_idx,
1308 target_type_idx,
1309 literal_offset));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001310}
1311
1312class ParallelCompilationManager {
1313 public:
1314 typedef void Callback(const ParallelCompilationManager* manager, size_t index);
1315
1316 ParallelCompilationManager(ClassLinker* class_linker,
1317 jobject class_loader,
1318 CompilerDriver* compiler,
1319 const DexFile* dex_file,
Ian Rogers3d504072014-03-01 09:16:49 -08001320 ThreadPool* thread_pool)
Mathieu Chartier0b3eb392013-08-23 14:56:59 -07001321 : index_(0),
1322 class_linker_(class_linker),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001323 class_loader_(class_loader),
1324 compiler_(compiler),
1325 dex_file_(dex_file),
Ian Rogers3d504072014-03-01 09:16:49 -08001326 thread_pool_(thread_pool) {}
Brian Carlstrom7940e442013-07-12 13:46:57 -07001327
1328 ClassLinker* GetClassLinker() const {
1329 CHECK(class_linker_ != NULL);
1330 return class_linker_;
1331 }
1332
1333 jobject GetClassLoader() const {
1334 return class_loader_;
1335 }
1336
1337 CompilerDriver* GetCompiler() const {
1338 CHECK(compiler_ != NULL);
1339 return compiler_;
1340 }
1341
1342 const DexFile* GetDexFile() const {
1343 CHECK(dex_file_ != NULL);
1344 return dex_file_;
1345 }
1346
1347 void ForAll(size_t begin, size_t end, Callback callback, size_t work_units) {
1348 Thread* self = Thread::Current();
1349 self->AssertNoPendingException();
1350 CHECK_GT(work_units, 0U);
1351
Mathieu Chartier0b3eb392013-08-23 14:56:59 -07001352 index_ = begin;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001353 for (size_t i = 0; i < work_units; ++i) {
Sebastien Hertz501baec2013-12-13 12:02:36 +01001354 thread_pool_->AddTask(self, new ForAllClosure(this, end, callback));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001355 }
1356 thread_pool_->StartWorkers(self);
1357
1358 // Ensure we're suspended while we're blocked waiting for the other threads to finish (worker
1359 // thread destructor's called below perform join).
1360 CHECK_NE(self->GetState(), kRunnable);
1361
1362 // Wait for all the worker threads to finish.
1363 thread_pool_->Wait(self, true, false);
1364 }
1365
Mathieu Chartier0b3eb392013-08-23 14:56:59 -07001366 size_t NextIndex() {
Ian Rogersb122a4b2013-11-19 18:00:50 -08001367 return index_.FetchAndAdd(1);
Mathieu Chartier0b3eb392013-08-23 14:56:59 -07001368 }
1369
Brian Carlstrom7940e442013-07-12 13:46:57 -07001370 private:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001371 class ForAllClosure : public Task {
1372 public:
Mathieu Chartier0b3eb392013-08-23 14:56:59 -07001373 ForAllClosure(ParallelCompilationManager* manager, size_t end, Callback* callback)
Brian Carlstrom7940e442013-07-12 13:46:57 -07001374 : manager_(manager),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001375 end_(end),
Mathieu Chartier0b3eb392013-08-23 14:56:59 -07001376 callback_(callback) {}
Brian Carlstrom7940e442013-07-12 13:46:57 -07001377
1378 virtual void Run(Thread* self) {
Mathieu Chartier0b3eb392013-08-23 14:56:59 -07001379 while (true) {
1380 const size_t index = manager_->NextIndex();
1381 if (UNLIKELY(index >= end_)) {
1382 break;
1383 }
1384 callback_(manager_, index);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001385 self->AssertNoPendingException();
1386 }
1387 }
1388
1389 virtual void Finalize() {
1390 delete this;
1391 }
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -07001392
Brian Carlstrom7940e442013-07-12 13:46:57 -07001393 private:
Mathieu Chartier0b3eb392013-08-23 14:56:59 -07001394 ParallelCompilationManager* const manager_;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001395 const size_t end_;
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +01001396 Callback* const callback_;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001397 };
1398
Mathieu Chartier0b3eb392013-08-23 14:56:59 -07001399 AtomicInteger index_;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001400 ClassLinker* const class_linker_;
1401 const jobject class_loader_;
1402 CompilerDriver* const compiler_;
1403 const DexFile* const dex_file_;
1404 ThreadPool* const thread_pool_;
Mathieu Chartier0b3eb392013-08-23 14:56:59 -07001405
1406 DISALLOW_COPY_AND_ASSIGN(ParallelCompilationManager);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001407};
1408
Jeff Hao0e49b422013-11-08 12:16:56 -08001409// Return true if the class should be skipped during compilation.
1410//
1411// The first case where we skip is for redundant class definitions in
1412// the boot classpath. We skip all but the first definition in that case.
1413//
1414// The second case where we skip is when an app bundles classes found
1415// in the boot classpath. Since at runtime we will select the class from
1416// the boot classpath, we ignore the one from the app.
Ian Rogersbe7149f2013-08-20 09:29:39 -07001417static bool SkipClass(ClassLinker* class_linker, jobject class_loader, const DexFile& dex_file,
1418 const DexFile::ClassDef& class_def) {
Jeff Hao0e49b422013-11-08 12:16:56 -08001419 const char* descriptor = dex_file.GetClassDescriptor(class_def);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001420 if (class_loader == NULL) {
Jeff Hao0e49b422013-11-08 12:16:56 -08001421 DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, class_linker->GetBootClassPath());
1422 CHECK(pair.second != NULL);
1423 if (pair.first != &dex_file) {
1424 LOG(WARNING) << "Skipping class " << descriptor << " from " << dex_file.GetLocation()
1425 << " previously found in " << pair.first->GetLocation();
1426 return true;
1427 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001428 return false;
1429 }
Ian Rogersbe7149f2013-08-20 09:29:39 -07001430 return class_linker->IsInBootClassPath(descriptor);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001431}
1432
Jeff Hao0e49b422013-11-08 12:16:56 -08001433// A fast version of SkipClass above if the class pointer is available
1434// that avoids the expensive FindInClassPath search.
1435static bool SkipClass(jobject class_loader, const DexFile& dex_file, mirror::Class* klass)
1436 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1437 DCHECK(klass != NULL);
1438 const DexFile& original_dex_file = *klass->GetDexCache()->GetDexFile();
1439 if (&dex_file != &original_dex_file) {
1440 if (class_loader == NULL) {
1441 LOG(WARNING) << "Skipping class " << PrettyDescriptor(klass) << " from "
1442 << dex_file.GetLocation() << " previously found in "
1443 << original_dex_file.GetLocation();
1444 }
1445 return true;
1446 }
1447 return false;
1448}
1449
Ian Rogerse6bb3b22013-08-19 21:51:45 -07001450static void ResolveClassFieldsAndMethods(const ParallelCompilationManager* manager,
1451 size_t class_def_index)
Brian Carlstrom7940e442013-07-12 13:46:57 -07001452 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -07001453 ATRACE_CALL();
Ian Rogersbe7149f2013-08-20 09:29:39 -07001454 Thread* self = Thread::Current();
1455 jobject jclass_loader = manager->GetClassLoader();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001456 const DexFile& dex_file = *manager->GetDexFile();
Ian Rogersbe7149f2013-08-20 09:29:39 -07001457 ClassLinker* class_linker = manager->GetClassLinker();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001458
Ian Rogerse6bb3b22013-08-19 21:51:45 -07001459 // If an instance field is final then we need to have a barrier on the return, static final
1460 // fields are assigned within the lock held for class initialization. Conservatively assume
1461 // constructor barriers are always required.
1462 bool requires_constructor_barrier = true;
1463
Brian Carlstrom7940e442013-07-12 13:46:57 -07001464 // Method and Field are the worst. We can't resolve without either
1465 // context from the code use (to disambiguate virtual vs direct
1466 // method and instance vs static field) or from class
1467 // definitions. While the compiler will resolve what it can as it
1468 // needs it, here we try to resolve fields and methods used in class
1469 // definitions, since many of them many never be referenced by
1470 // generated code.
1471 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogersbe7149f2013-08-20 09:29:39 -07001472 if (!SkipClass(class_linker, jclass_loader, dex_file, class_def)) {
Brian Carlstromcb5f5e52013-09-23 17:48:16 -07001473 ScopedObjectAccess soa(self);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001474 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), soa.Decode<mirror::ClassLoader*>(jclass_loader));
1475 SirtRef<mirror::DexCache> dex_cache(soa.Self(), class_linker->FindDexCache(dex_file));
Brian Carlstromcb5f5e52013-09-23 17:48:16 -07001476 // Resolve the class.
1477 mirror::Class* klass = class_linker->ResolveType(dex_file, class_def.class_idx_, dex_cache,
1478 class_loader);
Brian Carlstromcb5f5e52013-09-23 17:48:16 -07001479 bool resolve_fields_and_methods;
1480 if (klass == NULL) {
1481 // Class couldn't be resolved, for example, super-class is in a different dex file. Don't
1482 // attempt to resolve methods and fields when there is no declaring class.
1483 CHECK(soa.Self()->IsExceptionPending());
1484 soa.Self()->ClearException();
1485 resolve_fields_and_methods = false;
1486 } else {
1487 resolve_fields_and_methods = manager->GetCompiler()->IsImage();
1488 }
Ian Rogerse6bb3b22013-08-19 21:51:45 -07001489 // Note the class_data pointer advances through the headers,
1490 // static fields, instance fields, direct methods, and virtual
1491 // methods.
1492 const byte* class_data = dex_file.GetClassData(class_def);
1493 if (class_data == NULL) {
1494 // Empty class such as a marker interface.
1495 requires_constructor_barrier = false;
1496 } else {
Ian Rogerse6bb3b22013-08-19 21:51:45 -07001497 ClassDataItemIterator it(dex_file, class_data);
1498 while (it.HasNextStaticField()) {
1499 if (resolve_fields_and_methods) {
1500 mirror::ArtField* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(),
1501 dex_cache, class_loader, true);
1502 if (field == NULL) {
1503 CHECK(soa.Self()->IsExceptionPending());
1504 soa.Self()->ClearException();
1505 }
1506 }
1507 it.Next();
1508 }
1509 // We require a constructor barrier if there are final instance fields.
1510 requires_constructor_barrier = false;
1511 while (it.HasNextInstanceField()) {
1512 if ((it.GetMemberAccessFlags() & kAccFinal) != 0) {
1513 requires_constructor_barrier = true;
1514 }
1515 if (resolve_fields_and_methods) {
1516 mirror::ArtField* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(),
1517 dex_cache, class_loader, false);
1518 if (field == NULL) {
1519 CHECK(soa.Self()->IsExceptionPending());
1520 soa.Self()->ClearException();
1521 }
1522 }
1523 it.Next();
1524 }
1525 if (resolve_fields_and_methods) {
1526 while (it.HasNextDirectMethod()) {
1527 mirror::ArtMethod* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(),
1528 dex_cache, class_loader, NULL,
1529 it.GetMethodInvokeType(class_def));
1530 if (method == NULL) {
1531 CHECK(soa.Self()->IsExceptionPending());
1532 soa.Self()->ClearException();
1533 }
1534 it.Next();
1535 }
1536 while (it.HasNextVirtualMethod()) {
1537 mirror::ArtMethod* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(),
1538 dex_cache, class_loader, NULL,
1539 it.GetMethodInvokeType(class_def));
1540 if (method == NULL) {
1541 CHECK(soa.Self()->IsExceptionPending());
1542 soa.Self()->ClearException();
1543 }
1544 it.Next();
1545 }
1546 DCHECK(!it.HasNext());
1547 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001548 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001549 }
1550 if (requires_constructor_barrier) {
Ian Rogersbe7149f2013-08-20 09:29:39 -07001551 manager->GetCompiler()->AddRequiresConstructorBarrier(self, &dex_file, class_def_index);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001552 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001553}
1554
1555static void ResolveType(const ParallelCompilationManager* manager, size_t type_idx)
1556 LOCKS_EXCLUDED(Locks::mutator_lock_) {
1557 // Class derived values are more complicated, they require the linker and loader.
1558 ScopedObjectAccess soa(Thread::Current());
1559 ClassLinker* class_linker = manager->GetClassLinker();
1560 const DexFile& dex_file = *manager->GetDexFile();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001561 SirtRef<mirror::DexCache> dex_cache(soa.Self(), class_linker->FindDexCache(dex_file));
Mathieu Chartierc528dba2013-11-26 12:00:11 -08001562 SirtRef<mirror::ClassLoader> class_loader(
1563 soa.Self(), soa.Decode<mirror::ClassLoader*>(manager->GetClassLoader()));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001564 mirror::Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
1565
1566 if (klass == NULL) {
1567 CHECK(soa.Self()->IsExceptionPending());
Ian Rogersa436fde2013-08-27 23:34:06 -07001568 mirror::Throwable* exception = soa.Self()->GetException(NULL);
1569 VLOG(compiler) << "Exception during type resolution: " << exception->Dump();
Ian Rogersdfb325e2013-10-30 01:00:44 -07001570 if (strcmp("Ljava/lang/OutOfMemoryError;",
1571 ClassHelper(exception->GetClass()).GetDescriptor()) == 0) {
Ian Rogersa436fde2013-08-27 23:34:06 -07001572 // There's little point continuing compilation if the heap is exhausted.
1573 LOG(FATAL) << "Out of memory during type resolution for compilation";
1574 }
1575 soa.Self()->ClearException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001576 }
1577}
1578
1579void CompilerDriver::ResolveDexFile(jobject class_loader, const DexFile& dex_file,
Ian Rogers3d504072014-03-01 09:16:49 -08001580 ThreadPool* thread_pool, TimingLogger* timings) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001581 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1582
1583 // TODO: we could resolve strings here, although the string table is largely filled with class
1584 // and method names.
1585
1586 ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, thread_pool);
Ian Rogerse6bb3b22013-08-19 21:51:45 -07001587 if (IsImage()) {
1588 // For images we resolve all types, such as array, whereas for applications just those with
1589 // classdefs are resolved by ResolveClassFieldsAndMethods.
Ian Rogers3d504072014-03-01 09:16:49 -08001590 timings->NewSplit("Resolve Types");
Ian Rogerse6bb3b22013-08-19 21:51:45 -07001591 context.ForAll(0, dex_file.NumTypeIds(), ResolveType, thread_count_);
1592 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001593
Ian Rogers3d504072014-03-01 09:16:49 -08001594 timings->NewSplit("Resolve MethodsAndFields");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001595 context.ForAll(0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001596}
1597
1598void CompilerDriver::Verify(jobject class_loader, const std::vector<const DexFile*>& dex_files,
Ian Rogers3d504072014-03-01 09:16:49 -08001599 ThreadPool* thread_pool, TimingLogger* timings) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001600 for (size_t i = 0; i != dex_files.size(); ++i) {
1601 const DexFile* dex_file = dex_files[i];
1602 CHECK(dex_file != NULL);
1603 VerifyDexFile(class_loader, *dex_file, thread_pool, timings);
1604 }
1605}
1606
1607static void VerifyClass(const ParallelCompilationManager* manager, size_t class_def_index)
1608 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Anwar Ghuloum67f99412013-08-12 14:19:48 -07001609 ATRACE_CALL();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001610 ScopedObjectAccess soa(Thread::Current());
Jeff Hao0e49b422013-11-08 12:16:56 -08001611 const DexFile& dex_file = *manager->GetDexFile();
1612 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1613 const char* descriptor = dex_file.GetClassDescriptor(class_def);
1614 ClassLinker* class_linker = manager->GetClassLinker();
1615 jobject jclass_loader = manager->GetClassLoader();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001616 SirtRef<mirror::ClassLoader> class_loader(
1617 soa.Self(), soa.Decode<mirror::ClassLoader*>(jclass_loader));
Ian Rogers98379392014-02-24 16:53:16 -08001618 SirtRef<mirror::Class> klass(soa.Self(), class_linker->FindClass(soa.Self(), descriptor,
1619 class_loader));
Mathieu Chartierc528dba2013-11-26 12:00:11 -08001620 if (klass.get() == nullptr) {
Ian Rogerse6bb3b22013-08-19 21:51:45 -07001621 CHECK(soa.Self()->IsExceptionPending());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001622 soa.Self()->ClearException();
1623
1624 /*
1625 * At compile time, we can still structurally verify the class even if FindClass fails.
1626 * This is to ensure the class is structurally sound for compilation. An unsound class
1627 * will be rejected by the verifier and later skipped during compilation in the compiler.
1628 */
Mathieu Chartier590fee92013-09-13 13:46:47 -07001629 SirtRef<mirror::DexCache> dex_cache(soa.Self(), class_linker->FindDexCache(dex_file));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001630 std::string error_msg;
Mathieu Chartier590fee92013-09-13 13:46:47 -07001631 if (verifier::MethodVerifier::VerifyClass(&dex_file, dex_cache, class_loader, &class_def, true,
1632 &error_msg) ==
Brian Carlstrom7940e442013-07-12 13:46:57 -07001633 verifier::MethodVerifier::kHardFailure) {
Jeff Hao0e49b422013-11-08 12:16:56 -08001634 LOG(ERROR) << "Verification failed on class " << PrettyDescriptor(descriptor)
Brian Carlstrom7940e442013-07-12 13:46:57 -07001635 << " because: " << error_msg;
1636 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -08001637 } else if (!SkipClass(jclass_loader, dex_file, klass.get())) {
1638 CHECK(klass->IsResolved()) << PrettyClass(klass.get());
Jeff Hao0e49b422013-11-08 12:16:56 -08001639 class_linker->VerifyClass(klass);
Ian Rogerse6bb3b22013-08-19 21:51:45 -07001640
1641 if (klass->IsErroneous()) {
1642 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
1643 CHECK(soa.Self()->IsExceptionPending());
1644 soa.Self()->ClearException();
1645 }
1646
1647 CHECK(klass->IsCompileTimeVerified() || klass->IsErroneous())
Mathieu Chartierc528dba2013-11-26 12:00:11 -08001648 << PrettyDescriptor(klass.get()) << ": state=" << klass->GetStatus();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001649 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001650 soa.Self()->AssertNoPendingException();
1651}
1652
1653void CompilerDriver::VerifyDexFile(jobject class_loader, const DexFile& dex_file,
Ian Rogers3d504072014-03-01 09:16:49 -08001654 ThreadPool* thread_pool, TimingLogger* timings) {
1655 timings->NewSplit("Verify Dex File");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001656 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1657 ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, thread_pool);
1658 context.ForAll(0, dex_file.NumClassDefs(), VerifyClass, thread_count_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001659}
1660
Brian Carlstrom7940e442013-07-12 13:46:57 -07001661static void InitializeClass(const ParallelCompilationManager* manager, size_t class_def_index)
1662 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -07001663 ATRACE_CALL();
Jeff Hao0e49b422013-11-08 12:16:56 -08001664 jobject jclass_loader = manager->GetClassLoader();
1665 const DexFile& dex_file = *manager->GetDexFile();
1666 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Jeff Haobcdbbfe2013-11-08 18:03:22 -08001667 const DexFile::TypeId& class_type_id = dex_file.GetTypeId(class_def.class_idx_);
1668 const char* descriptor = dex_file.StringDataByIdx(class_type_id.descriptor_idx_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07001669
Brian Carlstrom7940e442013-07-12 13:46:57 -07001670 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier590fee92013-09-13 13:46:47 -07001671 SirtRef<mirror::ClassLoader> class_loader(soa.Self(),
1672 soa.Decode<mirror::ClassLoader*>(jclass_loader));
Mathieu Chartierc528dba2013-11-26 12:00:11 -08001673 SirtRef<mirror::Class> klass(soa.Self(),
Ian Rogers98379392014-02-24 16:53:16 -08001674 manager->GetClassLinker()->FindClass(soa.Self(), descriptor,
1675 class_loader));
Jeff Hao0e49b422013-11-08 12:16:56 -08001676
Mathieu Chartierc528dba2013-11-26 12:00:11 -08001677 if (klass.get() != nullptr && !SkipClass(jclass_loader, dex_file, klass.get())) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001678 // Only try to initialize classes that were successfully verified.
1679 if (klass->IsVerified()) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -07001680 // Attempt to initialize the class but bail if we either need to initialize the super-class
1681 // or static fields.
1682 manager->GetClassLinker()->EnsureInitialized(klass, false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001683 if (!klass->IsInitialized()) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -07001684 // We don't want non-trivial class initialization occurring on multiple threads due to
1685 // deadlock problems. For example, a parent class is initialized (holding its lock) that
1686 // refers to a sub-class in its static/class initializer causing it to try to acquire the
1687 // sub-class' lock. While on a second thread the sub-class is initialized (holding its lock)
1688 // after first initializing its parents, whose locks are acquired. This leads to a
1689 // parent-to-child and a child-to-parent lock ordering and consequent potential deadlock.
1690 // We need to use an ObjectLock due to potential suspension in the interpreting code. Rather
1691 // than use a special Object for the purpose we use the Class of java.lang.Class.
Mathieu Chartierc528dba2013-11-26 12:00:11 -08001692 SirtRef<mirror::Class> sirt_klass(soa.Self(), klass->GetClass());
1693 ObjectLock<mirror::Class> lock(soa.Self(), &sirt_klass);
Ian Rogers8f3c9ae2013-08-20 17:26:41 -07001694 // Attempt to initialize allowing initialization of parent classes but still not static
1695 // fields.
1696 manager->GetClassLinker()->EnsureInitialized(klass, false, true);
1697 if (!klass->IsInitialized()) {
1698 // We need to initialize static fields, we only do this for image classes that aren't
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001699 // marked with the $NoPreloadHolder (which implies this should not be initialized early).
Ian Rogers8f3c9ae2013-08-20 17:26:41 -07001700 bool can_init_static_fields = manager->GetCompiler()->IsImage() &&
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001701 manager->GetCompiler()->IsImageClass(descriptor) &&
1702 !StringPiece(descriptor).ends_with("$NoPreloadHolder;");
Ian Rogers8f3c9ae2013-08-20 17:26:41 -07001703 if (can_init_static_fields) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001704 VLOG(compiler) << "Initializing: " << descriptor;
1705 if (strcmp("Ljava/lang/Void;", descriptor) == 0) {
1706 // Hand initialize j.l.Void to avoid Dex file operations in un-started runtime.
1707 ObjectLock<mirror::Class> lock(soa.Self(), &klass);
1708 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetSFields();
1709 CHECK_EQ(fields->GetLength(), 1);
1710 fields->Get(0)->SetObj<false>(klass.get(),
1711 manager->GetClassLinker()->FindPrimitiveClass('V'));
1712 klass->SetStatus(mirror::Class::kStatusInitialized, soa.Self());
1713 } else {
1714 // TODO multithreading support. We should ensure the current compilation thread has
1715 // exclusive access to the runtime and the transaction. To achieve this, we could use
1716 // a ReaderWriterMutex but we're holding the mutator lock so we fail mutex sanity
1717 // checks in Thread::AssertThreadSuspensionIsAllowable.
1718 Runtime* const runtime = Runtime::Current();
1719 Transaction transaction;
1720
1721 // Run the class initializer in transaction mode.
1722 runtime->EnterTransactionMode(&transaction);
1723 const mirror::Class::Status old_status = klass->GetStatus();
1724 bool success = manager->GetClassLinker()->EnsureInitialized(klass, true, true);
1725 // TODO we detach transaction from runtime to indicate we quit the transactional
1726 // mode which prevents the GC from visiting objects modified during the transaction.
1727 // Ensure GC is not run so don't access freed objects when aborting transaction.
1728 const char* old_casue = soa.Self()->StartAssertNoThreadSuspension("Transaction end");
1729 runtime->ExitTransactionMode();
1730
1731 if (!success) {
1732 CHECK(soa.Self()->IsExceptionPending());
1733 ThrowLocation throw_location;
1734 mirror::Throwable* exception = soa.Self()->GetException(&throw_location);
1735 VLOG(compiler) << "Initialization of " << descriptor << " aborted because of "
1736 << exception->Dump();
1737 soa.Self()->ClearException();
1738 transaction.Abort();
1739 CHECK_EQ(old_status, klass->GetStatus()) << "Previous class status not restored";
Ian Rogers8f3c9ae2013-08-20 17:26:41 -07001740 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001741 soa.Self()->EndAssertNoThreadSuspension(old_casue);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001742 }
1743 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001744 }
Ian Rogers8f3c9ae2013-08-20 17:26:41 -07001745 soa.Self()->AssertNoPendingException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001746 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001747 }
1748 // Record the final class status if necessary.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001749 ClassReference ref(manager->GetDexFile(), class_def_index);
Ian Rogers8f3c9ae2013-08-20 17:26:41 -07001750 manager->GetCompiler()->RecordClassStatus(ref, klass->GetStatus());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001751 }
1752 // Clear any class not found or verification exceptions.
1753 soa.Self()->ClearException();
1754}
1755
1756void CompilerDriver::InitializeClasses(jobject jni_class_loader, const DexFile& dex_file,
Ian Rogers3d504072014-03-01 09:16:49 -08001757 ThreadPool* thread_pool, TimingLogger* timings) {
1758 timings->NewSplit("InitializeNoClinit");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001759 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1760 ParallelCompilationManager context(class_linker, jni_class_loader, this, &dex_file, thread_pool);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001761 size_t thread_count;
1762 if (IsImage()) {
1763 // TODO: remove this when transactional mode supports multithreading.
1764 thread_count = 1U;
1765 } else {
1766 thread_count = thread_count_;
1767 }
1768 context.ForAll(0, dex_file.NumClassDefs(), InitializeClass, thread_count);
1769 if (IsImage()) {
1770 // Prune garbage objects created during aborted transactions.
1771 Runtime::Current()->GetHeap()->CollectGarbage(true);
1772 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001773}
1774
1775void CompilerDriver::InitializeClasses(jobject class_loader,
1776 const std::vector<const DexFile*>& dex_files,
Ian Rogers3d504072014-03-01 09:16:49 -08001777 ThreadPool* thread_pool, TimingLogger* timings) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001778 for (size_t i = 0; i != dex_files.size(); ++i) {
1779 const DexFile* dex_file = dex_files[i];
1780 CHECK(dex_file != NULL);
1781 InitializeClasses(class_loader, *dex_file, thread_pool, timings);
1782 }
1783}
1784
1785void CompilerDriver::Compile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
Ian Rogers3d504072014-03-01 09:16:49 -08001786 ThreadPool* thread_pool, TimingLogger* timings) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001787 for (size_t i = 0; i != dex_files.size(); ++i) {
1788 const DexFile* dex_file = dex_files[i];
1789 CHECK(dex_file != NULL);
1790 CompileDexFile(class_loader, *dex_file, thread_pool, timings);
1791 }
1792}
1793
1794void CompilerDriver::CompileClass(const ParallelCompilationManager* manager, size_t class_def_index) {
Anwar Ghuloum67f99412013-08-12 14:19:48 -07001795 ATRACE_CALL();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001796 jobject jclass_loader = manager->GetClassLoader();
1797 const DexFile& dex_file = *manager->GetDexFile();
1798 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogersbe7149f2013-08-20 09:29:39 -07001799 ClassLinker* class_linker = manager->GetClassLinker();
1800 if (SkipClass(class_linker, jclass_loader, dex_file, class_def)) {
1801 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001802 }
1803 ClassReference ref(&dex_file, class_def_index);
1804 // Skip compiling classes with generic verifier failures since they will still fail at runtime
Vladimir Markoc7f83202014-01-24 17:55:18 +00001805 if (manager->GetCompiler()->verification_results_->IsClassRejected(ref)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001806 return;
1807 }
1808 const byte* class_data = dex_file.GetClassData(class_def);
1809 if (class_data == NULL) {
1810 // empty class, probably a marker interface
1811 return;
1812 }
Anwar Ghuloum67f99412013-08-12 14:19:48 -07001813
Brian Carlstrom7940e442013-07-12 13:46:57 -07001814 // Can we run DEX-to-DEX compiler on this class ?
Sebastien Hertz75021222013-07-16 18:34:50 +02001815 DexToDexCompilationLevel dex_to_dex_compilation_level = kDontDexToDexCompile;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001816 {
1817 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier590fee92013-09-13 13:46:47 -07001818 SirtRef<mirror::ClassLoader> class_loader(soa.Self(),
1819 soa.Decode<mirror::ClassLoader*>(jclass_loader));
Ian Rogers98379392014-02-24 16:53:16 -08001820 dex_to_dex_compilation_level = GetDexToDexCompilationlevel(soa.Self(), class_loader, dex_file,
1821 class_def);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001822 }
1823 ClassDataItemIterator it(dex_file, class_data);
1824 // Skip fields
1825 while (it.HasNextStaticField()) {
1826 it.Next();
1827 }
1828 while (it.HasNextInstanceField()) {
1829 it.Next();
1830 }
Ian Rogersbe7149f2013-08-20 09:29:39 -07001831 CompilerDriver* driver = manager->GetCompiler();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001832 // Compile direct methods
1833 int64_t previous_direct_method_idx = -1;
1834 while (it.HasNextDirectMethod()) {
1835 uint32_t method_idx = it.GetMemberIndex();
1836 if (method_idx == previous_direct_method_idx) {
1837 // smali can create dex files with two encoded_methods sharing the same method_idx
1838 // http://code.google.com/p/smali/issues/detail?id=119
1839 it.Next();
1840 continue;
1841 }
1842 previous_direct_method_idx = method_idx;
Ian Rogersbe7149f2013-08-20 09:29:39 -07001843 driver->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
1844 it.GetMethodInvokeType(class_def), class_def_index,
1845 method_idx, jclass_loader, dex_file, dex_to_dex_compilation_level);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001846 it.Next();
1847 }
1848 // Compile virtual methods
1849 int64_t previous_virtual_method_idx = -1;
1850 while (it.HasNextVirtualMethod()) {
1851 uint32_t method_idx = it.GetMemberIndex();
1852 if (method_idx == previous_virtual_method_idx) {
1853 // smali can create dex files with two encoded_methods sharing the same method_idx
1854 // http://code.google.com/p/smali/issues/detail?id=119
1855 it.Next();
1856 continue;
1857 }
1858 previous_virtual_method_idx = method_idx;
Ian Rogersbe7149f2013-08-20 09:29:39 -07001859 driver->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
1860 it.GetMethodInvokeType(class_def), class_def_index,
1861 method_idx, jclass_loader, dex_file, dex_to_dex_compilation_level);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001862 it.Next();
1863 }
1864 DCHECK(!it.HasNext());
1865}
1866
1867void CompilerDriver::CompileDexFile(jobject class_loader, const DexFile& dex_file,
Ian Rogers3d504072014-03-01 09:16:49 -08001868 ThreadPool* thread_pool, TimingLogger* timings) {
1869 timings->NewSplit("Compile Dex File");
Ian Rogersbe7149f2013-08-20 09:29:39 -07001870 ParallelCompilationManager context(Runtime::Current()->GetClassLinker(), class_loader, this,
1871 &dex_file, thread_pool);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001872 context.ForAll(0, dex_file.NumClassDefs(), CompilerDriver::CompileClass, thread_count_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001873}
1874
1875void CompilerDriver::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001876 InvokeType invoke_type, uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001877 uint32_t method_idx, jobject class_loader,
1878 const DexFile& dex_file,
Sebastien Hertz75021222013-07-16 18:34:50 +02001879 DexToDexCompilationLevel dex_to_dex_compilation_level) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001880 CompiledMethod* compiled_method = NULL;
1881 uint64_t start_ns = NanoTime();
1882
1883 if ((access_flags & kAccNative) != 0) {
Ian Rogers0188ab72014-03-17 16:51:53 -07001884 // Are we interpreting only and have support for generic JNI down calls?
Jeff Hao4a200f52014-04-01 14:58:49 -07001885 if (!compiler_options_->IsCompilationEnabled() &&
Stuart Monteithb95a5342014-03-12 13:32:32 +00001886 (instruction_set_ == kX86_64 || instruction_set_ == kArm64)) {
Ian Rogers5b271492014-03-14 13:20:26 -07001887 // Leaving this empty will trigger the generic JNI version
1888 } else {
1889 compiled_method = compiler_->JniCompile(*this, access_flags, method_idx, dex_file);
1890 CHECK(compiled_method != NULL);
1891 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001892 } else if ((access_flags & kAccAbstract) != 0) {
1893 } else {
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07001894 MethodReference method_ref(&dex_file, method_idx);
Brian Carlstrom6449c622014-02-10 23:48:36 -08001895 bool compile = verification_results_->IsCandidateForCompilation(method_ref, access_flags);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001896 if (compile) {
buzbeea024a062013-07-31 10:47:37 -07001897 // NOTE: if compiler declines to compile this method, it will return NULL.
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001898 compiled_method = compiler_->Compile(
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +00001899 *this, code_item, access_flags, invoke_type, class_def_idx,
1900 method_idx, class_loader, dex_file);
Sebastien Hertz17965ed2014-04-04 15:59:53 +02001901 }
1902 if (compiled_method == nullptr && dex_to_dex_compilation_level != kDontDexToDexCompile) {
1903 // TODO: add a command-line option to disable DEX-to-DEX compilation ?
Sebastien Hertz75021222013-07-16 18:34:50 +02001904 (*dex_to_dex_compiler_)(*this, code_item, access_flags,
1905 invoke_type, class_def_idx,
1906 method_idx, class_loader, dex_file,
1907 dex_to_dex_compilation_level);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001908 }
1909 }
1910 uint64_t duration_ns = NanoTime() - start_ns;
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001911 if (duration_ns > MsToNs(compiler_->GetMaximumCompilationTimeBeforeWarning())) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001912 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
1913 << " took " << PrettyDuration(duration_ns);
1914 }
1915
1916 Thread* self = Thread::Current();
1917 if (compiled_method != NULL) {
1918 MethodReference ref(&dex_file, method_idx);
Ian Rogers8f3c9ae2013-08-20 17:26:41 -07001919 DCHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001920 {
1921 MutexLock mu(self, compiled_methods_lock_);
1922 compiled_methods_.Put(ref, compiled_method);
1923 }
1924 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
1925 }
1926
1927 if (self->IsExceptionPending()) {
1928 ScopedObjectAccess soa(self);
1929 LOG(FATAL) << "Unexpected exception compiling: " << PrettyMethod(method_idx, dex_file) << "\n"
1930 << self->GetException(NULL)->Dump();
1931 }
1932}
1933
1934CompiledClass* CompilerDriver::GetCompiledClass(ClassReference ref) const {
1935 MutexLock mu(Thread::Current(), compiled_classes_lock_);
1936 ClassTable::const_iterator it = compiled_classes_.find(ref);
1937 if (it == compiled_classes_.end()) {
1938 return NULL;
1939 }
1940 CHECK(it->second != NULL);
1941 return it->second;
1942}
1943
Ian Rogers8f3c9ae2013-08-20 17:26:41 -07001944void CompilerDriver::RecordClassStatus(ClassReference ref, mirror::Class::Status status) {
1945 MutexLock mu(Thread::Current(), compiled_classes_lock_);
1946 auto it = compiled_classes_.find(ref);
1947 if (it == compiled_classes_.end() || it->second->GetStatus() != status) {
1948 // An entry doesn't exist or the status is lower than the new status.
1949 if (it != compiled_classes_.end()) {
1950 CHECK_GT(status, it->second->GetStatus());
1951 delete it->second;
1952 }
1953 switch (status) {
1954 case mirror::Class::kStatusNotReady:
1955 case mirror::Class::kStatusError:
1956 case mirror::Class::kStatusRetryVerificationAtRuntime:
1957 case mirror::Class::kStatusVerified:
1958 case mirror::Class::kStatusInitialized:
1959 break; // Expected states.
1960 default:
1961 LOG(FATAL) << "Unexpected class status for class "
1962 << PrettyDescriptor(ref.first->GetClassDescriptor(ref.first->GetClassDef(ref.second)))
1963 << " of " << status;
1964 }
1965 CompiledClass* compiled_class = new CompiledClass(status);
1966 compiled_classes_.Overwrite(ref, compiled_class);
1967 }
1968}
1969
Brian Carlstrom7940e442013-07-12 13:46:57 -07001970CompiledMethod* CompilerDriver::GetCompiledMethod(MethodReference ref) const {
1971 MutexLock mu(Thread::Current(), compiled_methods_lock_);
1972 MethodTable::const_iterator it = compiled_methods_.find(ref);
1973 if (it == compiled_methods_.end()) {
1974 return NULL;
1975 }
1976 CHECK(it->second != NULL);
1977 return it->second;
1978}
1979
Brian Carlstrom7940e442013-07-12 13:46:57 -07001980void CompilerDriver::AddRequiresConstructorBarrier(Thread* self, const DexFile* dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001981 uint16_t class_def_index) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -07001982 WriterMutexLock mu(self, freezing_constructor_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001983 freezing_constructor_classes_.insert(ClassReference(dex_file, class_def_index));
1984}
1985
1986bool CompilerDriver::RequiresConstructorBarrier(Thread* self, const DexFile* dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001987 uint16_t class_def_index) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -07001988 ReaderMutexLock mu(self, freezing_constructor_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001989 return freezing_constructor_classes_.count(ClassReference(dex_file, class_def_index)) != 0;
1990}
1991
1992bool CompilerDriver::WriteElf(const std::string& android_root,
1993 bool is_host,
1994 const std::vector<const art::DexFile*>& dex_files,
Ian Rogers3d504072014-03-01 09:16:49 -08001995 OatWriter* oat_writer,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001996 art::File* file)
1997 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001998 return compiler_->WriteElf(file, oat_writer, dex_files, android_root, is_host, *this);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001999}
2000void CompilerDriver::InstructionSetToLLVMTarget(InstructionSet instruction_set,
Ian Rogers3d504072014-03-01 09:16:49 -08002001 std::string* target_triple,
2002 std::string* target_cpu,
2003 std::string* target_attr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002004 switch (instruction_set) {
2005 case kThumb2:
Ian Rogers3d504072014-03-01 09:16:49 -08002006 *target_triple = "thumb-none-linux-gnueabi";
2007 *target_cpu = "cortex-a9";
2008 *target_attr = "+thumb2,+neon,+neonfp,+vfp3,+db";
Brian Carlstrom7940e442013-07-12 13:46:57 -07002009 break;
2010
2011 case kArm:
Ian Rogers3d504072014-03-01 09:16:49 -08002012 *target_triple = "armv7-none-linux-gnueabi";
Brian Carlstrom7940e442013-07-12 13:46:57 -07002013 // TODO: Fix for Nexus S.
Ian Rogers3d504072014-03-01 09:16:49 -08002014 *target_cpu = "cortex-a9";
Brian Carlstrom7940e442013-07-12 13:46:57 -07002015 // TODO: Fix for Xoom.
Ian Rogers3d504072014-03-01 09:16:49 -08002016 *target_attr = "+v7,+neon,+neonfp,+vfp3,+db";
Brian Carlstrom7940e442013-07-12 13:46:57 -07002017 break;
2018
2019 case kX86:
Ian Rogers3d504072014-03-01 09:16:49 -08002020 *target_triple = "i386-pc-linux-gnu";
2021 *target_attr = "";
Brian Carlstrom7940e442013-07-12 13:46:57 -07002022 break;
2023
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07002024 case kX86_64:
2025 *target_triple = "x86_64-pc-linux-gnu";
2026 *target_attr = "";
2027 break;
2028
Brian Carlstrom7940e442013-07-12 13:46:57 -07002029 case kMips:
Ian Rogers3d504072014-03-01 09:16:49 -08002030 *target_triple = "mipsel-unknown-linux";
2031 *target_attr = "mips32r2";
Brian Carlstrom7940e442013-07-12 13:46:57 -07002032 break;
2033
2034 default:
2035 LOG(FATAL) << "Unknown instruction set: " << instruction_set;
2036 }
2037 }
Dave Allison39c3bfb2014-01-28 18:33:52 -08002038
2039bool CompilerDriver::ReadProfile(const std::string& filename) {
2040 VLOG(compiler) << "reading profile file " << filename;
2041 struct stat st;
2042 int err = stat(filename.c_str(), &st);
2043 if (err == -1) {
2044 VLOG(compiler) << "not found";
2045 return false;
2046 }
2047 std::ifstream in(filename.c_str());
2048 if (!in) {
2049 VLOG(compiler) << "profile file " << filename << " exists but can't be opened";
2050 VLOG(compiler) << "file owner: " << st.st_uid << ":" << st.st_gid;
2051 VLOG(compiler) << "me: " << getuid() << ":" << getgid();
2052 VLOG(compiler) << "file permissions: " << std::oct << st.st_mode;
2053 VLOG(compiler) << "errno: " << errno;
2054 return false;
2055 }
2056 // The first line contains summary information.
2057 std::string line;
2058 std::getline(in, line);
2059 if (in.eof()) {
2060 return false;
2061 }
2062 std::vector<std::string> summary_info;
2063 Split(line, '/', summary_info);
2064 if (summary_info.size() != 3) {
Calin Juravle04ff2262014-04-02 19:08:47 +01002065 // Bad summary info. It should be count/total/bootpath.
Dave Allison39c3bfb2014-01-28 18:33:52 -08002066 return false;
2067 }
2068 // This is the number of hits in all methods.
2069 uint32_t total_count = 0;
2070 for (int i = 0 ; i < 3; ++i) {
Calin Juravleea1e5202014-04-02 15:41:43 +01002071 total_count += atoi(summary_info[i].c_str());
Dave Allison39c3bfb2014-01-28 18:33:52 -08002072 }
2073
Calin Juravle04ff2262014-04-02 19:08:47 +01002074 // Now read each line until the end of file. Each line consists of 3 fields separated by '/'.
2075 // Store the info in descending order given by the most used methods.
Calin Juravlef6a4cee2014-04-02 17:03:08 +01002076 typedef std::set<std::pair<int, std::vector<std::string>>> ProfileSet;
2077 ProfileSet countSet;
Dave Allison39c3bfb2014-01-28 18:33:52 -08002078 while (!in.eof()) {
2079 std::getline(in, line);
2080 if (in.eof()) {
2081 break;
2082 }
2083 std::vector<std::string> info;
2084 Split(line, '/', info);
2085 if (info.size() != 3) {
2086 // Malformed.
2087 break;
2088 }
Calin Juravlef6a4cee2014-04-02 17:03:08 +01002089 int count = atoi(info[1].c_str());
2090 countSet.insert(std::make_pair(-count, info));
2091 }
2092
2093 uint32_t curTotalCount = 0;
2094 ProfileSet::iterator end = countSet.end();
2095 const ProfileData* prevData = nullptr;
2096 for (ProfileSet::iterator it = countSet.begin(); it != end ; it++) {
2097 const std::string& methodname = it->second[0];
2098 uint32_t count = -it->first;
2099 uint32_t size = atoi(it->second[2].c_str());
2100 double usedPercent = (count * 100.0) / total_count;
2101
2102 curTotalCount += count;
Calin Juravle04ff2262014-04-02 19:08:47 +01002103 // Methods with the same count should be part of the same top K percentage bucket.
Calin Juravlef6a4cee2014-04-02 17:03:08 +01002104 double topKPercentage = (prevData != nullptr) && (prevData->GetCount() == count)
2105 ? prevData->GetTopKUsedPercentage()
2106 : 100 * static_cast<double>(curTotalCount) / static_cast<double>(total_count);
2107
Calin Juravle04ff2262014-04-02 19:08:47 +01002108 // Add it to the profile map.
Calin Juravlef6a4cee2014-04-02 17:03:08 +01002109 ProfileData curData = ProfileData(methodname, count, size, usedPercent, topKPercentage);
2110 profile_map_[methodname] = curData;
2111 prevData = &curData;
Dave Allison39c3bfb2014-01-28 18:33:52 -08002112 }
2113 return true;
2114}
2115
2116bool CompilerDriver::SkipCompilation(const std::string& method_name) {
2117 if (!profile_ok_) {
2118 return true;
2119 }
Calin Juravle04ff2262014-04-02 19:08:47 +01002120 // Methods that comprise topKPercentThreshold % of the total samples will be compiled.
2121 double topKPercentThreshold = 90.0;
2122#ifdef HAVE_ANDROID_OS
2123 char buf[PROP_VALUE_MAX];
2124 property_get("dalvik.vm.profile.compile_thr", buf, "90.0");
2125 topKPercentThreshold = strtod(buf, nullptr);
2126#endif
2127 // Test for reasonable thresholds.
2128 if (topKPercentThreshold < 10.0 || topKPercentThreshold > 90.0) {
2129 topKPercentThreshold = 90.0;
2130 }
Dave Allison39c3bfb2014-01-28 18:33:52 -08002131
2132 // First find the method in the profile map.
2133 ProfileMap::iterator i = profile_map_.find(method_name);
2134 if (i == profile_map_.end()) {
2135 // Not in profile, no information can be determined.
2136 VLOG(compiler) << "not compiling " << method_name << " because it's not in the profile";
2137 return true;
2138 }
2139 const ProfileData& data = i->second;
Calin Juravle04ff2262014-04-02 19:08:47 +01002140
Calin Juravlef6a4cee2014-04-02 17:03:08 +01002141 // Compare against the start of the topK percentage bucket just in case the threshold
Calin Juravle04ff2262014-04-02 19:08:47 +01002142 // falls inside a bucket.
2143 bool compile = data.GetTopKUsedPercentage() - data.GetUsedPercent() <= topKPercentThreshold;
Dave Allison39c3bfb2014-01-28 18:33:52 -08002144 if (compile) {
Calin Juravlef6a4cee2014-04-02 17:03:08 +01002145 LOG(INFO) << "compiling method " << method_name << " because its usage is part of top "
2146 << data.GetTopKUsedPercentage() << "% with a percent of " << data.GetUsedPercent() << "%";
Dave Allison39c3bfb2014-01-28 18:33:52 -08002147 } else {
Calin Juravle04ff2262014-04-02 19:08:47 +01002148 VLOG(compiler) << "not compiling method " << method_name << " because it's not part of leading "
2149 << topKPercentThreshold << "% samples)";
Dave Allison39c3bfb2014-01-28 18:33:52 -08002150 }
2151 return !compile;
2152}
Brian Carlstrom7940e442013-07-12 13:46:57 -07002153} // namespace art