blob: bfe8d9a32d3ad708f13e10b8c51019acd9aedbd2 [file] [log] [blame]
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001/*
2 * Copyright 2014 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 "jit.h"
18
19#include <dlfcn.h>
20
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method-inl.h"
Andreas Gampe2a5c4682015-08-14 08:22:54 -070022#include "debugger.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080023#include "entrypoints/runtime_asm_entrypoints.h"
24#include "interpreter/interpreter.h"
25#include "jit_code_cache.h"
26#include "jit_instrumentation.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010027#include "oat_file_manager.h"
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000028#include "oat_quick_method_header.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010029#include "offline_profiling_info.h"
Calin Juravle4d77b6a2015-12-01 18:38:09 +000030#include "profile_saver.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080031#include "runtime.h"
32#include "runtime_options.h"
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000033#include "stack_map.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080034#include "utils.h"
35
36namespace art {
37namespace jit {
38
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000039static constexpr bool kEnableOnStackReplacement = true;
Nicolas Geoffraye8662132016-02-15 10:00:42 +000040
Mathieu Chartier72918ea2016-03-24 11:07:06 -070041// JIT compiler
42void* Jit::jit_library_handle_= nullptr;
43void* Jit::jit_compiler_handle_ = nullptr;
44void* (*Jit::jit_load_)(bool*) = nullptr;
45void (*Jit::jit_unload_)(void*) = nullptr;
46bool (*Jit::jit_compile_method_)(void*, ArtMethod*, Thread*, bool) = nullptr;
47void (*Jit::jit_types_loaded_)(void*, mirror::Class**, size_t count) = nullptr;
48bool Jit::generate_debug_info_ = false;
49
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080050JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080051 auto* jit_options = new JitOptions;
Mathieu Chartier455f67c2015-03-17 13:48:29 -070052 jit_options->use_jit_ = options.GetOrDefault(RuntimeArgumentMap::UseJIT);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000053
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000054 jit_options->code_cache_initial_capacity_ =
55 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity);
56 jit_options->code_cache_max_capacity_ =
57 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070058 jit_options->dump_info_on_shutdown_ =
59 options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
Calin Juravle31f2c152015-10-23 17:56:15 +010060 jit_options->save_profiling_info_ =
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000061 options.GetOrDefault(RuntimeArgumentMap::JITSaveProfilingInfo);
62
63 jit_options->compile_threshold_ = options.GetOrDefault(RuntimeArgumentMap::JITCompileThreshold);
64 if (jit_options->compile_threshold_ > std::numeric_limits<uint16_t>::max()) {
65 LOG(FATAL) << "Method compilation threshold is above its internal limit.";
66 }
67
68 if (options.Exists(RuntimeArgumentMap::JITWarmupThreshold)) {
69 jit_options->warmup_threshold_ = *options.Get(RuntimeArgumentMap::JITWarmupThreshold);
70 if (jit_options->warmup_threshold_ > std::numeric_limits<uint16_t>::max()) {
71 LOG(FATAL) << "Method warmup threshold is above its internal limit.";
72 }
73 } else {
74 jit_options->warmup_threshold_ = jit_options->compile_threshold_ / 2;
75 }
76
77 if (options.Exists(RuntimeArgumentMap::JITOsrThreshold)) {
78 jit_options->osr_threshold_ = *options.Get(RuntimeArgumentMap::JITOsrThreshold);
79 if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) {
80 LOG(FATAL) << "Method on stack replacement threshold is above its internal limit.";
81 }
82 } else {
83 jit_options->osr_threshold_ = jit_options->compile_threshold_ * 2;
84 if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) {
85 jit_options->osr_threshold_ = std::numeric_limits<uint16_t>::max();
86 }
87 }
88
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080089 return jit_options;
90}
91
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070092void Jit::DumpInfo(std::ostream& os) {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +000093 code_cache_->Dump(os);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070094 cumulative_timings_.Dump(os);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000095 MutexLock mu(Thread::Current(), lock_);
96 memory_use_.PrintMemoryUse(os);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070097}
98
99void Jit::AddTimingLogger(const TimingLogger& logger) {
100 cumulative_timings_.AddLogger(logger);
101}
102
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700103Jit::Jit() : dump_info_on_shutdown_(false),
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000104 cumulative_timings_("JIT timings"),
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000105 memory_use_("Memory used for compilation", 16),
106 lock_("JIT memory use lock"),
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700107 save_profiling_info_(false) {}
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800108
109Jit* Jit::Create(JitOptions* options, std::string* error_msg) {
110 std::unique_ptr<Jit> jit(new Jit);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700111 jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown();
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700112 if (jit_compiler_handle_ == nullptr && !LoadCompiler(error_msg)) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800113 return nullptr;
114 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000115 jit->code_cache_.reset(JitCodeCache::Create(
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000116 options->GetCodeCacheInitialCapacity(),
117 options->GetCodeCacheMaxCapacity(),
118 jit->generate_debug_info_,
119 error_msg));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800120 if (jit->GetCodeCache() == nullptr) {
121 return nullptr;
122 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000123 jit->save_profiling_info_ = options->GetSaveProfilingInfo();
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000124 VLOG(jit) << "JIT created with initial_capacity="
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000125 << PrettySize(options->GetCodeCacheInitialCapacity())
126 << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity())
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000127 << ", compile_threshold=" << options->GetCompileThreshold()
128 << ", save_profiling_info=" << options->GetSaveProfilingInfo();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800129 return jit.release();
130}
131
132bool Jit::LoadCompiler(std::string* error_msg) {
133 jit_library_handle_ = dlopen(
134 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
135 if (jit_library_handle_ == nullptr) {
136 std::ostringstream oss;
137 oss << "JIT could not load libart-compiler.so: " << dlerror();
138 *error_msg = oss.str();
139 return false;
140 }
Nicolas Geoffray5b82d332016-02-18 14:22:32 +0000141 jit_load_ = reinterpret_cast<void* (*)(bool*)>(dlsym(jit_library_handle_, "jit_load"));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800142 if (jit_load_ == nullptr) {
143 dlclose(jit_library_handle_);
144 *error_msg = "JIT couldn't find jit_load entry point";
145 return false;
146 }
147 jit_unload_ = reinterpret_cast<void (*)(void*)>(
148 dlsym(jit_library_handle_, "jit_unload"));
149 if (jit_unload_ == nullptr) {
150 dlclose(jit_library_handle_);
151 *error_msg = "JIT couldn't find jit_unload entry point";
152 return false;
153 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000154 jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*, bool)>(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800155 dlsym(jit_library_handle_, "jit_compile_method"));
156 if (jit_compile_method_ == nullptr) {
157 dlclose(jit_library_handle_);
158 *error_msg = "JIT couldn't find jit_compile_method entry point";
159 return false;
160 }
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000161 jit_types_loaded_ = reinterpret_cast<void (*)(void*, mirror::Class**, size_t)>(
162 dlsym(jit_library_handle_, "jit_types_loaded"));
163 if (jit_types_loaded_ == nullptr) {
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000164 dlclose(jit_library_handle_);
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000165 *error_msg = "JIT couldn't find jit_types_loaded entry point";
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000166 return false;
167 }
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000168 bool will_generate_debug_symbols = false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800169 VLOG(jit) << "Calling JitLoad interpreter_only="
170 << Runtime::Current()->GetInstrumentation()->InterpretOnly();
Nicolas Geoffray5b82d332016-02-18 14:22:32 +0000171 jit_compiler_handle_ = (jit_load_)(&will_generate_debug_symbols);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800172 if (jit_compiler_handle_ == nullptr) {
173 dlclose(jit_library_handle_);
174 *error_msg = "JIT couldn't load compiler";
175 return false;
176 }
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000177 generate_debug_info_ = will_generate_debug_symbols;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800178 return true;
179}
180
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000181bool Jit::CompileMethod(ArtMethod* method, Thread* self, bool osr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800182 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000183
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100184 // Don't compile the method if it has breakpoints.
Mathieu Chartierd8565452015-03-26 09:41:50 -0700185 if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) {
186 VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to breakpoint";
187 return false;
188 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100189
190 // Don't compile the method if we are supposed to be deoptimized.
191 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
192 if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000193 VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to deoptimization";
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100194 return false;
195 }
196
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000197 // If we get a request to compile a proxy method, we pass the actual Java method
198 // of that proxy method, as the compiler does not expect a proxy method.
199 ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(sizeof(void*));
200 if (!code_cache_->NotifyCompilationOf(method_to_compile, self, osr)) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100201 return false;
202 }
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000203 bool success = jit_compile_method_(jit_compiler_handle_, method_to_compile, self, osr);
204 code_cache_->DoneCompiling(method_to_compile, self);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100205 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800206}
207
208void Jit::CreateThreadPool() {
209 CHECK(instrumentation_cache_.get() != nullptr);
210 instrumentation_cache_->CreateThreadPool();
211}
212
213void Jit::DeleteThreadPool() {
214 if (instrumentation_cache_.get() != nullptr) {
Nicolas Geoffray629e9352015-11-04 17:22:16 +0000215 instrumentation_cache_->DeleteThreadPool(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800216 }
217}
218
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000219void Jit::StartProfileSaver(const std::string& filename,
Calin Juravlec90bc922016-02-24 10:13:09 +0000220 const std::vector<std::string>& code_paths,
221 const std::string& foreign_dex_profile_path,
222 const std::string& app_dir) {
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000223 if (save_profiling_info_) {
Calin Juravlec90bc922016-02-24 10:13:09 +0000224 ProfileSaver::Start(filename, code_cache_.get(), code_paths, foreign_dex_profile_path, app_dir);
Calin Juravle31f2c152015-10-23 17:56:15 +0100225 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000226}
227
228void Jit::StopProfileSaver() {
229 if (save_profiling_info_ && ProfileSaver::IsStarted()) {
230 ProfileSaver::Stop();
Calin Juravle31f2c152015-10-23 17:56:15 +0100231 }
232}
233
Siva Chandra05d24152016-01-05 17:43:17 -0800234bool Jit::JitAtFirstUse() {
235 if (instrumentation_cache_ != nullptr) {
236 return instrumentation_cache_->HotMethodThreshold() == 0;
237 }
238 return false;
239}
240
Nicolas Geoffray35122442016-03-02 12:05:30 +0000241bool Jit::CanInvokeCompiledCode(ArtMethod* method) {
242 return code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode());
243}
244
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800245Jit::~Jit() {
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000246 DCHECK(!save_profiling_info_ || !ProfileSaver::IsStarted());
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700247 if (dump_info_on_shutdown_) {
248 DumpInfo(LOG(INFO));
249 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800250 DeleteThreadPool();
251 if (jit_compiler_handle_ != nullptr) {
252 jit_unload_(jit_compiler_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700253 jit_compiler_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800254 }
255 if (jit_library_handle_ != nullptr) {
256 dlclose(jit_library_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700257 jit_library_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800258 }
259}
260
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000261void Jit::CreateInstrumentationCache(size_t compile_threshold,
262 size_t warmup_threshold,
263 size_t osr_threshold) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100264 instrumentation_cache_.reset(
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000265 new jit::JitInstrumentationCache(compile_threshold, warmup_threshold, osr_threshold));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800266}
267
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000268void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) {
269 jit::Jit* jit = Runtime::Current()->GetJit();
270 if (jit != nullptr && jit->generate_debug_info_) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000271 DCHECK(jit->jit_types_loaded_ != nullptr);
272 jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1);
273 }
274}
275
276void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) {
277 struct CollectClasses : public ClassVisitor {
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -0800278 bool operator()(mirror::Class* klass) override {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000279 classes_.push_back(klass);
280 return true;
281 }
Mathieu Chartier9b1c9b72016-02-02 10:09:58 -0800282 std::vector<mirror::Class*> classes_;
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000283 };
284
285 if (generate_debug_info_) {
286 ScopedObjectAccess so(Thread::Current());
287
288 CollectClasses visitor;
289 linker->VisitClasses(&visitor);
290 jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size());
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000291 }
292}
293
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000294extern "C" void art_quick_osr_stub(void** stack,
295 uint32_t stack_size_in_bytes,
296 const uint8_t* native_pc,
297 JValue* result,
298 const char* shorty,
299 Thread* self);
300
301bool Jit::MaybeDoOnStackReplacement(Thread* thread,
302 ArtMethod* method,
303 uint32_t dex_pc,
304 int32_t dex_pc_offset,
305 JValue* result) {
Nicolas Geoffraye8662132016-02-15 10:00:42 +0000306 if (!kEnableOnStackReplacement) {
307 return false;
308 }
309
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000310 Jit* jit = Runtime::Current()->GetJit();
311 if (jit == nullptr) {
312 return false;
313 }
314
315 if (kRuntimeISA == kMips || kRuntimeISA == kMips64) {
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000316 VLOG(jit) << "OSR not supported on this platform: " << kRuntimeISA;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000317 return false;
318 }
319
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000320 if (UNLIKELY(__builtin_frame_address(0) < thread->GetStackEnd())) {
321 // Don't attempt to do an OSR if we are close to the stack limit. Since
322 // the interpreter frames are still on stack, OSR has the potential
323 // to stack overflow even for a simple loop.
324 // b/27094810.
325 return false;
326 }
327
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000328 // Get the actual Java method if this method is from a proxy class. The compiler
329 // and the JIT code cache do not expect methods from proxy classes.
330 method = method->GetInterfaceMethodIfProxy(sizeof(void*));
331
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000332 // Cheap check if the method has been compiled already. That's an indicator that we should
333 // osr into it.
334 if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
335 return false;
336 }
337
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000338 // Fetch some data before looking up for an OSR method. We don't want thread
339 // suspension once we hold an OSR method, as the JIT code cache could delete the OSR
340 // method while we are being suspended.
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000341 const size_t number_of_vregs = method->GetCodeItem()->registers_size_;
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000342 const char* shorty = method->GetShorty();
343 std::string method_name(VLOG_IS_ON(jit) ? PrettyMethod(method) : "");
344 void** memory = nullptr;
345 size_t frame_size = 0;
346 ShadowFrame* shadow_frame = nullptr;
347 const uint8_t* native_pc = nullptr;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000348
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000349 {
350 ScopedAssertNoThreadSuspension sts(thread, "Holding OSR method");
351 const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method);
352 if (osr_method == nullptr) {
353 // No osr method yet, just return to the interpreter.
354 return false;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000355 }
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000356
357 CodeInfo code_info = osr_method->GetOptimizedCodeInfo();
358 StackMapEncoding encoding = code_info.ExtractEncoding();
359
360 // Find stack map starting at the target dex_pc.
361 StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset, encoding);
362 if (!stack_map.IsValid()) {
363 // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the
364 // hope that the next branch has one.
365 return false;
366 }
367
368 // We found a stack map, now fill the frame with dex register values from the interpreter's
369 // shadow frame.
370 DexRegisterMap vreg_map =
371 code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_vregs);
372
373 frame_size = osr_method->GetFrameSizeInBytes();
374
375 // Allocate memory to put shadow frame values. The osr stub will copy that memory to
376 // stack.
377 // Note that we could pass the shadow frame to the stub, and let it copy the values there,
378 // but that is engineering complexity not worth the effort for something like OSR.
379 memory = reinterpret_cast<void**>(malloc(frame_size));
380 CHECK(memory != nullptr);
381 memset(memory, 0, frame_size);
382
383 // Art ABI: ArtMethod is at the bottom of the stack.
384 memory[0] = method;
385
386 shadow_frame = thread->PopShadowFrame();
387 if (!vreg_map.IsValid()) {
388 // If we don't have a dex register map, then there are no live dex registers at
389 // this dex pc.
390 } else {
391 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
392 DexRegisterLocation::Kind location =
393 vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding);
394 if (location == DexRegisterLocation::Kind::kNone) {
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000395 // Dex register is dead or uninitialized.
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000396 continue;
397 }
398
399 if (location == DexRegisterLocation::Kind::kConstant) {
400 // We skip constants because the compiled code knows how to handle them.
401 continue;
402 }
403
David Srbecky7dc11782016-02-25 13:23:56 +0000404 DCHECK_EQ(location, DexRegisterLocation::Kind::kInStack);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000405
406 int32_t vreg_value = shadow_frame->GetVReg(vreg);
407 int32_t slot_offset = vreg_map.GetStackOffsetInBytes(vreg,
408 number_of_vregs,
409 code_info,
410 encoding);
411 DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size));
412 DCHECK_GT(slot_offset, 0);
413 (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value;
414 }
415 }
416
417 native_pc = stack_map.GetNativePcOffset(encoding) + osr_method->GetEntryPoint();
418 VLOG(jit) << "Jumping to "
419 << method_name
420 << "@"
421 << std::hex << reinterpret_cast<uintptr_t>(native_pc);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000422 }
423
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000424 {
425 ManagedStack fragment;
426 thread->PushManagedStackFragment(&fragment);
427 (*art_quick_osr_stub)(memory,
428 frame_size,
429 native_pc,
430 result,
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000431 shorty,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000432 thread);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000433
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000434 if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) {
435 thread->DeoptimizeWithDeoptimizationException(result);
436 }
437 thread->PopManagedStackFragment(fragment);
438 }
439 free(memory);
440 thread->PushShadowFrame(shadow_frame);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000441 VLOG(jit) << "Done running OSR code for " << method_name;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000442 return true;
443}
444
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000445void Jit::AddMemoryUsage(ArtMethod* method, size_t bytes) {
446 if (bytes > 4 * MB) {
447 LOG(INFO) << "Compiler allocated "
448 << PrettySize(bytes)
449 << " to compile "
450 << PrettyMethod(method);
451 }
452 MutexLock mu(Thread::Current(), lock_);
453 memory_use_.AddValue(bytes);
454}
455
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800456} // namespace jit
457} // namespace art