blob: 257739134b747f55c85d6105cc7c70eec8ea83de [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,
75 false,
76 false,
77 false,
78 false,
79 true, // pic
80 nullptr,
81 pass_manager_options,
82 nullptr));
83 const InstructionSet instruction_set = kRuntimeISA;
84 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
85 cumulative_logger_.reset(new CumulativeLogger("jit times"));
86 verification_results_.reset(new VerificationResults(compiler_options_.get()));
87 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
88 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
89 method_inliner_map_.get()));
90 compiler_driver_.reset(new CompilerDriver(
91 compiler_options_.get(), verification_results_.get(), method_inliner_map_.get(),
92 Compiler::kQuick, instruction_set, instruction_set_features_.get(), false,
93 nullptr, new std::set<std::string>, 1, false, true,
94 std::string(), cumulative_logger_.get(), -1, std::string()));
95 // Disable dedupe so we can remove compiled methods.
96 compiler_driver_->SetDedupeEnabled(false);
97 compiler_driver_->SetSupportBootImageFixup(false);
98}
99
100JitCompiler::~JitCompiler() {
101}
102
103bool JitCompiler::CompileMethod(Thread* self, mirror::ArtMethod* method) {
104 uint64_t start_time = NanoTime();
105 StackHandleScope<2> hs(self);
106 self->AssertNoPendingException();
107 Runtime* runtime = Runtime::Current();
108 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
109 if (runtime->GetJit()->GetCodeCache()->ContainsMethod(method)) {
110 VLOG(jit) << "Already compiled " << PrettyMethod(method);
111 return true; // Already compiled
112 }
113 Handle<mirror::Class> h_class(hs.NewHandle(h_method->GetDeclaringClass()));
114 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
115 VLOG(jit) << "JIT failed to initialize " << PrettyMethod(h_method.Get());
116 return false;
117 }
118 const DexFile* dex_file = h_class->GetDexCache()->GetDexFile();
119 MethodReference method_ref(dex_file, h_method->GetDexMethodIndex());
120 // Only verify if we don't already have verification results.
121 if (verification_results_->GetVerifiedMethod(method_ref) == nullptr) {
122 std::string error;
123 if (verifier::MethodVerifier::VerifyMethod(h_method.Get(), true, &error) ==
124 verifier::MethodVerifier::kHardFailure) {
125 VLOG(jit) << "Not compile method " << PrettyMethod(h_method.Get())
126 << " due to verification failure " << error;
127 return false;
128 }
129 }
130 CompiledMethod* compiled_method(compiler_driver_->CompileMethod(self, h_method.Get()));
131 if (compiled_method == nullptr) {
132 return false;
133 }
134 total_time_ += NanoTime() - start_time;
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800135 // Don't add the method if we are supposed to be deoptimized.
136 bool result = false;
137 if (!runtime->GetInstrumentation()->AreAllMethodsDeoptimized()) {
138 const void* code = Runtime::Current()->GetClassLinker()->GetOatMethodQuickCodeFor(
139 h_method.Get());
140 if (code != nullptr) {
141 // Already have some compiled code, just use this instead of linking.
142 // TODO: Fix recompilation.
143 h_method->SetEntryPointFromQuickCompiledCode(code);
144 result = true;
145 } else {
146 result = MakeExecutable(compiled_method, h_method.Get());
147 }
148 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800149 // Remove the compiled method to save memory.
150 compiler_driver_->RemoveCompiledMethod(method_ref);
151 return result;
152}
153
154CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const {
155 return callbacks_.get();
156}
157
158uint8_t* JitCompiler::WriteMethodHeaderAndCode(const CompiledMethod* compiled_method,
159 uint8_t* reserve_begin, uint8_t* reserve_end,
160 const uint8_t* mapping_table,
161 const uint8_t* vmap_table,
162 const uint8_t* gc_map) {
163 reserve_begin += sizeof(OatQuickMethodHeader);
164 reserve_begin = reinterpret_cast<uint8_t*>(
165 compiled_method->AlignCode(reinterpret_cast<uintptr_t>(reserve_begin)));
166 const auto* quick_code = compiled_method->GetQuickCode();
167 CHECK_LE(reserve_begin, reserve_end);
168 CHECK_LE(quick_code->size(), static_cast<size_t>(reserve_end - reserve_begin));
169 auto* code_ptr = reserve_begin;
170 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(code_ptr) - 1;
171 // Construct the header last.
172 const auto frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
173 const auto core_spill_mask = compiled_method->GetCoreSpillMask();
174 const auto fp_spill_mask = compiled_method->GetFpSpillMask();
175 const auto code_size = quick_code->size();
176 CHECK_NE(code_size, 0U);
177 std::copy(quick_code->data(), quick_code->data() + code_size, code_ptr);
178 // After we are done writing we need to update the method header.
179 // Write out the method header last.
180 method_header = new(method_header)OatQuickMethodHeader(
181 code_ptr - mapping_table, code_ptr - vmap_table, code_ptr - gc_map, frame_size_in_bytes,
182 core_spill_mask, fp_spill_mask, code_size);
183 // Return the code ptr.
184 return code_ptr;
185}
186
187bool JitCompiler::AddToCodeCache(mirror::ArtMethod* method, const CompiledMethod* compiled_method,
188 OatFile::OatMethod* out_method) {
189 Runtime* runtime = Runtime::Current();
190 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
191 const auto* quick_code = compiled_method->GetQuickCode();
192 if (quick_code == nullptr) {
193 return false;
194 }
195 const auto code_size = quick_code->size();
196 Thread* const self = Thread::Current();
197 const uint8_t* base = code_cache->CodeCachePtr();
198 auto* const mapping_table = compiled_method->GetMappingTable();
199 auto* const vmap_table = compiled_method->GetVmapTable();
200 auto* const gc_map = compiled_method->GetGcMap();
201 // Write out pre-header stuff.
202 uint8_t* const mapping_table_ptr = code_cache->AddDataArray(
203 self, mapping_table->data(), mapping_table->data() + mapping_table->size());
204 if (mapping_table == nullptr) {
205 return false; // Out of data cache.
206 }
207 uint8_t* const vmap_table_ptr = code_cache->AddDataArray(
208 self, vmap_table->data(), vmap_table->data() + vmap_table->size());
209 if (vmap_table == nullptr) {
210 return false; // Out of data cache.
211 }
212 uint8_t* const gc_map_ptr = code_cache->AddDataArray(
213 self, gc_map->data(), gc_map->data() + gc_map->size());
214 if (gc_map == nullptr) {
215 return false; // Out of data cache.
216 }
217 // Don't touch this until you protect / unprotect the code.
218 const size_t reserve_size = sizeof(OatQuickMethodHeader) + quick_code->size() + 32;
219 uint8_t* const code_reserve = code_cache->ReserveCode(self, reserve_size);
220 if (code_reserve == nullptr) {
221 return false;
222 }
223 auto* code_ptr = WriteMethodHeaderAndCode(
224 compiled_method, code_reserve, code_reserve + reserve_size, mapping_table_ptr,
225 vmap_table_ptr, gc_map_ptr);
226
227 const size_t thumb_offset = compiled_method->CodeDelta();
228 const uint32_t code_offset = code_ptr - base + thumb_offset;
229 *out_method = OatFile::OatMethod(base, code_offset);
230 DCHECK_EQ(out_method->GetGcMap(), gc_map_ptr);
231 DCHECK_EQ(out_method->GetMappingTable(), mapping_table_ptr);
232 DCHECK_EQ(out_method->GetVmapTable(), vmap_table_ptr);
233 DCHECK_EQ(out_method->GetFrameSizeInBytes(), compiled_method->GetFrameSizeInBytes());
234 DCHECK_EQ(out_method->GetCoreSpillMask(), compiled_method->GetCoreSpillMask());
235 DCHECK_EQ(out_method->GetFpSpillMask(), compiled_method->GetFpSpillMask());
236 VLOG(jit) << "JIT added " << PrettyMethod(method) << "@" << method << " ccache_size="
237 << PrettySize(code_cache->CodeCacheSize()) << ": " << reinterpret_cast<void*>(code_ptr)
238 << "," << reinterpret_cast<void*>(code_ptr + code_size);
239 return true;
240}
241
242bool JitCompiler::MakeExecutable(CompiledMethod* compiled_method, mirror::ArtMethod* method) {
243 CHECK(method != nullptr);
244 CHECK(compiled_method != nullptr);
245 OatFile::OatMethod oat_method(nullptr, 0);
246 if (!AddToCodeCache(method, compiled_method, &oat_method)) {
247 return false;
248 }
249 // TODO: Flush instruction cache.
250 oat_method.LinkMethod(method);
251 CHECK(Runtime::Current()->GetJit()->GetCodeCache()->ContainsMethod(method))
252 << PrettyMethod(method);
253 return true;
254}
255
256} // namespace jit
257} // namespace art