blob: 04efa21f4e1f8c83d9641e996af940324f07d4a1 [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_compiler.h"
18
19#include "arch/instruction_set.h"
20#include "arch/instruction_set_features.h"
21#include "compiler_callbacks.h"
22#include "dex/pass_manager.h"
23#include "dex/quick_compiler_callbacks.h"
24#include "driver/compiler_driver.h"
25#include "driver/compiler_options.h"
26#include "jit/jit.h"
27#include "jit/jit_code_cache.h"
28#include "mirror/art_method-inl.h"
29#include "oat_file-inl.h"
30#include "object_lock.h"
31#include "thread_list.h"
32#include "verifier/method_verifier-inl.h"
33
34namespace art {
35namespace jit {
36
37JitCompiler* JitCompiler::Create() {
38 return new JitCompiler();
39}
40
41extern "C" void* jit_load(CompilerCallbacks** callbacks) {
42 VLOG(jit) << "loading jit compiler";
43 auto* const jit_compiler = JitCompiler::Create();
44 CHECK(jit_compiler != nullptr);
45 *callbacks = jit_compiler->GetCompilerCallbacks();
46 VLOG(jit) << "Done loading jit compiler";
47 return jit_compiler;
48}
49
50extern "C" void jit_unload(void* handle) {
51 DCHECK(handle != nullptr);
52 delete reinterpret_cast<JitCompiler*>(handle);
53}
54
55extern "C" bool jit_compile_method(void* handle, mirror::ArtMethod* method, Thread* self)
56 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
57 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
58 DCHECK(jit_compiler != nullptr);
59 return jit_compiler->CompileMethod(self, method);
60}
61
62JitCompiler::JitCompiler() : total_time_(0) {
63 auto* pass_manager_options = new PassManagerOptions;
64 pass_manager_options->SetDisablePassList("GVN,DCE");
65 compiler_options_.reset(new CompilerOptions(
66 CompilerOptions::kDefaultCompilerFilter,
67 CompilerOptions::kDefaultHugeMethodThreshold,
68 CompilerOptions::kDefaultLargeMethodThreshold,
69 CompilerOptions::kDefaultSmallMethodThreshold,
70 CompilerOptions::kDefaultTinyMethodThreshold,
71 CompilerOptions::kDefaultNumDexMethodsThreshold,
72 false,
73 false,
74 CompilerOptions::kDefaultTopKProfileThreshold,
Andreas Gampe7b2f09e2015-03-02 14:07:33 -080075 false, // TODO: Think about debuggability of JIT-compiled code.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080076 false,
77 false,
78 false,
79 false,
Mathieu Chartierdce71f32015-02-27 14:24:37 -080080 false, // pic
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080081 nullptr,
82 pass_manager_options,
Andreas Gampe6cf49e52015-03-05 13:08:45 -080083 nullptr,
84 false));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080085 const InstructionSet instruction_set = kRuntimeISA;
86 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
87 cumulative_logger_.reset(new CumulativeLogger("jit times"));
88 verification_results_.reset(new VerificationResults(compiler_options_.get()));
89 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
90 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
91 method_inliner_map_.get()));
92 compiler_driver_.reset(new CompilerDriver(
93 compiler_options_.get(), verification_results_.get(), method_inliner_map_.get(),
94 Compiler::kQuick, instruction_set, instruction_set_features_.get(), false,
95 nullptr, new std::set<std::string>, 1, false, true,
96 std::string(), cumulative_logger_.get(), -1, std::string()));
97 // Disable dedupe so we can remove compiled methods.
98 compiler_driver_->SetDedupeEnabled(false);
99 compiler_driver_->SetSupportBootImageFixup(false);
100}
101
102JitCompiler::~JitCompiler() {
103}
104
105bool JitCompiler::CompileMethod(Thread* self, mirror::ArtMethod* method) {
106 uint64_t start_time = NanoTime();
107 StackHandleScope<2> hs(self);
108 self->AssertNoPendingException();
109 Runtime* runtime = Runtime::Current();
110 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
111 if (runtime->GetJit()->GetCodeCache()->ContainsMethod(method)) {
112 VLOG(jit) << "Already compiled " << PrettyMethod(method);
113 return true; // Already compiled
114 }
115 Handle<mirror::Class> h_class(hs.NewHandle(h_method->GetDeclaringClass()));
116 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
117 VLOG(jit) << "JIT failed to initialize " << PrettyMethod(h_method.Get());
118 return false;
119 }
120 const DexFile* dex_file = h_class->GetDexCache()->GetDexFile();
121 MethodReference method_ref(dex_file, h_method->GetDexMethodIndex());
122 // Only verify if we don't already have verification results.
123 if (verification_results_->GetVerifiedMethod(method_ref) == nullptr) {
124 std::string error;
125 if (verifier::MethodVerifier::VerifyMethod(h_method.Get(), true, &error) ==
126 verifier::MethodVerifier::kHardFailure) {
127 VLOG(jit) << "Not compile method " << PrettyMethod(h_method.Get())
128 << " due to verification failure " << error;
129 return false;
130 }
131 }
132 CompiledMethod* compiled_method(compiler_driver_->CompileMethod(self, h_method.Get()));
133 if (compiled_method == nullptr) {
134 return false;
135 }
136 total_time_ += NanoTime() - start_time;
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800137 // Don't add the method if we are supposed to be deoptimized.
138 bool result = false;
139 if (!runtime->GetInstrumentation()->AreAllMethodsDeoptimized()) {
140 const void* code = Runtime::Current()->GetClassLinker()->GetOatMethodQuickCodeFor(
141 h_method.Get());
142 if (code != nullptr) {
143 // Already have some compiled code, just use this instead of linking.
144 // TODO: Fix recompilation.
145 h_method->SetEntryPointFromQuickCompiledCode(code);
146 result = true;
147 } else {
148 result = MakeExecutable(compiled_method, h_method.Get());
149 }
150 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800151 // Remove the compiled method to save memory.
152 compiler_driver_->RemoveCompiledMethod(method_ref);
153 return result;
154}
155
156CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const {
157 return callbacks_.get();
158}
159
160uint8_t* JitCompiler::WriteMethodHeaderAndCode(const CompiledMethod* compiled_method,
161 uint8_t* reserve_begin, uint8_t* reserve_end,
162 const uint8_t* mapping_table,
163 const uint8_t* vmap_table,
164 const uint8_t* gc_map) {
165 reserve_begin += sizeof(OatQuickMethodHeader);
166 reserve_begin = reinterpret_cast<uint8_t*>(
167 compiled_method->AlignCode(reinterpret_cast<uintptr_t>(reserve_begin)));
168 const auto* quick_code = compiled_method->GetQuickCode();
169 CHECK_LE(reserve_begin, reserve_end);
170 CHECK_LE(quick_code->size(), static_cast<size_t>(reserve_end - reserve_begin));
171 auto* code_ptr = reserve_begin;
172 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(code_ptr) - 1;
173 // Construct the header last.
174 const auto frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
175 const auto core_spill_mask = compiled_method->GetCoreSpillMask();
176 const auto fp_spill_mask = compiled_method->GetFpSpillMask();
177 const auto code_size = quick_code->size();
178 CHECK_NE(code_size, 0U);
179 std::copy(quick_code->data(), quick_code->data() + code_size, code_ptr);
180 // After we are done writing we need to update the method header.
181 // Write out the method header last.
182 method_header = new(method_header)OatQuickMethodHeader(
183 code_ptr - mapping_table, code_ptr - vmap_table, code_ptr - gc_map, frame_size_in_bytes,
184 core_spill_mask, fp_spill_mask, code_size);
185 // Return the code ptr.
186 return code_ptr;
187}
188
189bool JitCompiler::AddToCodeCache(mirror::ArtMethod* method, const CompiledMethod* compiled_method,
190 OatFile::OatMethod* out_method) {
191 Runtime* runtime = Runtime::Current();
192 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
193 const auto* quick_code = compiled_method->GetQuickCode();
194 if (quick_code == nullptr) {
195 return false;
196 }
197 const auto code_size = quick_code->size();
198 Thread* const self = Thread::Current();
199 const uint8_t* base = code_cache->CodeCachePtr();
200 auto* const mapping_table = compiled_method->GetMappingTable();
201 auto* const vmap_table = compiled_method->GetVmapTable();
202 auto* const gc_map = compiled_method->GetGcMap();
203 // Write out pre-header stuff.
204 uint8_t* const mapping_table_ptr = code_cache->AddDataArray(
205 self, mapping_table->data(), mapping_table->data() + mapping_table->size());
206 if (mapping_table == nullptr) {
207 return false; // Out of data cache.
208 }
209 uint8_t* const vmap_table_ptr = code_cache->AddDataArray(
210 self, vmap_table->data(), vmap_table->data() + vmap_table->size());
211 if (vmap_table == nullptr) {
212 return false; // Out of data cache.
213 }
214 uint8_t* const gc_map_ptr = code_cache->AddDataArray(
215 self, gc_map->data(), gc_map->data() + gc_map->size());
216 if (gc_map == nullptr) {
217 return false; // Out of data cache.
218 }
219 // Don't touch this until you protect / unprotect the code.
220 const size_t reserve_size = sizeof(OatQuickMethodHeader) + quick_code->size() + 32;
221 uint8_t* const code_reserve = code_cache->ReserveCode(self, reserve_size);
222 if (code_reserve == nullptr) {
223 return false;
224 }
225 auto* code_ptr = WriteMethodHeaderAndCode(
226 compiled_method, code_reserve, code_reserve + reserve_size, mapping_table_ptr,
227 vmap_table_ptr, gc_map_ptr);
228
229 const size_t thumb_offset = compiled_method->CodeDelta();
230 const uint32_t code_offset = code_ptr - base + thumb_offset;
231 *out_method = OatFile::OatMethod(base, code_offset);
232 DCHECK_EQ(out_method->GetGcMap(), gc_map_ptr);
233 DCHECK_EQ(out_method->GetMappingTable(), mapping_table_ptr);
234 DCHECK_EQ(out_method->GetVmapTable(), vmap_table_ptr);
235 DCHECK_EQ(out_method->GetFrameSizeInBytes(), compiled_method->GetFrameSizeInBytes());
236 DCHECK_EQ(out_method->GetCoreSpillMask(), compiled_method->GetCoreSpillMask());
237 DCHECK_EQ(out_method->GetFpSpillMask(), compiled_method->GetFpSpillMask());
238 VLOG(jit) << "JIT added " << PrettyMethod(method) << "@" << method << " ccache_size="
239 << PrettySize(code_cache->CodeCacheSize()) << ": " << reinterpret_cast<void*>(code_ptr)
240 << "," << reinterpret_cast<void*>(code_ptr + code_size);
241 return true;
242}
243
244bool JitCompiler::MakeExecutable(CompiledMethod* compiled_method, mirror::ArtMethod* method) {
245 CHECK(method != nullptr);
246 CHECK(compiled_method != nullptr);
247 OatFile::OatMethod oat_method(nullptr, 0);
248 if (!AddToCodeCache(method, compiled_method, &oat_method)) {
249 return false;
250 }
251 // TODO: Flush instruction cache.
252 oat_method.LinkMethod(method);
253 CHECK(Runtime::Current()->GetJit()->GetCodeCache()->ContainsMethod(method))
254 << PrettyMethod(method);
255 return true;
256}
257
258} // namespace jit
259} // namespace art