blob: b1d972e44ee77ef7c9ec454538ebab76f72e8ca1 [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;
135 const bool result = MakeExecutable(compiled_method, h_method.Get());
136 // Remove the compiled method to save memory.
137 compiler_driver_->RemoveCompiledMethod(method_ref);
138 return result;
139}
140
141CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const {
142 return callbacks_.get();
143}
144
145uint8_t* JitCompiler::WriteMethodHeaderAndCode(const CompiledMethod* compiled_method,
146 uint8_t* reserve_begin, uint8_t* reserve_end,
147 const uint8_t* mapping_table,
148 const uint8_t* vmap_table,
149 const uint8_t* gc_map) {
150 reserve_begin += sizeof(OatQuickMethodHeader);
151 reserve_begin = reinterpret_cast<uint8_t*>(
152 compiled_method->AlignCode(reinterpret_cast<uintptr_t>(reserve_begin)));
153 const auto* quick_code = compiled_method->GetQuickCode();
154 CHECK_LE(reserve_begin, reserve_end);
155 CHECK_LE(quick_code->size(), static_cast<size_t>(reserve_end - reserve_begin));
156 auto* code_ptr = reserve_begin;
157 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(code_ptr) - 1;
158 // Construct the header last.
159 const auto frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
160 const auto core_spill_mask = compiled_method->GetCoreSpillMask();
161 const auto fp_spill_mask = compiled_method->GetFpSpillMask();
162 const auto code_size = quick_code->size();
163 CHECK_NE(code_size, 0U);
164 std::copy(quick_code->data(), quick_code->data() + code_size, code_ptr);
165 // After we are done writing we need to update the method header.
166 // Write out the method header last.
167 method_header = new(method_header)OatQuickMethodHeader(
168 code_ptr - mapping_table, code_ptr - vmap_table, code_ptr - gc_map, frame_size_in_bytes,
169 core_spill_mask, fp_spill_mask, code_size);
170 // Return the code ptr.
171 return code_ptr;
172}
173
174bool JitCompiler::AddToCodeCache(mirror::ArtMethod* method, const CompiledMethod* compiled_method,
175 OatFile::OatMethod* out_method) {
176 Runtime* runtime = Runtime::Current();
177 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
178 const auto* quick_code = compiled_method->GetQuickCode();
179 if (quick_code == nullptr) {
180 return false;
181 }
182 const auto code_size = quick_code->size();
183 Thread* const self = Thread::Current();
184 const uint8_t* base = code_cache->CodeCachePtr();
185 auto* const mapping_table = compiled_method->GetMappingTable();
186 auto* const vmap_table = compiled_method->GetVmapTable();
187 auto* const gc_map = compiled_method->GetGcMap();
188 // Write out pre-header stuff.
189 uint8_t* const mapping_table_ptr = code_cache->AddDataArray(
190 self, mapping_table->data(), mapping_table->data() + mapping_table->size());
191 if (mapping_table == nullptr) {
192 return false; // Out of data cache.
193 }
194 uint8_t* const vmap_table_ptr = code_cache->AddDataArray(
195 self, vmap_table->data(), vmap_table->data() + vmap_table->size());
196 if (vmap_table == nullptr) {
197 return false; // Out of data cache.
198 }
199 uint8_t* const gc_map_ptr = code_cache->AddDataArray(
200 self, gc_map->data(), gc_map->data() + gc_map->size());
201 if (gc_map == nullptr) {
202 return false; // Out of data cache.
203 }
204 // Don't touch this until you protect / unprotect the code.
205 const size_t reserve_size = sizeof(OatQuickMethodHeader) + quick_code->size() + 32;
206 uint8_t* const code_reserve = code_cache->ReserveCode(self, reserve_size);
207 if (code_reserve == nullptr) {
208 return false;
209 }
210 auto* code_ptr = WriteMethodHeaderAndCode(
211 compiled_method, code_reserve, code_reserve + reserve_size, mapping_table_ptr,
212 vmap_table_ptr, gc_map_ptr);
213
214 const size_t thumb_offset = compiled_method->CodeDelta();
215 const uint32_t code_offset = code_ptr - base + thumb_offset;
216 *out_method = OatFile::OatMethod(base, code_offset);
217 DCHECK_EQ(out_method->GetGcMap(), gc_map_ptr);
218 DCHECK_EQ(out_method->GetMappingTable(), mapping_table_ptr);
219 DCHECK_EQ(out_method->GetVmapTable(), vmap_table_ptr);
220 DCHECK_EQ(out_method->GetFrameSizeInBytes(), compiled_method->GetFrameSizeInBytes());
221 DCHECK_EQ(out_method->GetCoreSpillMask(), compiled_method->GetCoreSpillMask());
222 DCHECK_EQ(out_method->GetFpSpillMask(), compiled_method->GetFpSpillMask());
223 VLOG(jit) << "JIT added " << PrettyMethod(method) << "@" << method << " ccache_size="
224 << PrettySize(code_cache->CodeCacheSize()) << ": " << reinterpret_cast<void*>(code_ptr)
225 << "," << reinterpret_cast<void*>(code_ptr + code_size);
226 return true;
227}
228
229bool JitCompiler::MakeExecutable(CompiledMethod* compiled_method, mirror::ArtMethod* method) {
230 CHECK(method != nullptr);
231 CHECK(compiled_method != nullptr);
232 OatFile::OatMethod oat_method(nullptr, 0);
233 if (!AddToCodeCache(method, compiled_method, &oat_method)) {
234 return false;
235 }
236 // TODO: Flush instruction cache.
237 oat_method.LinkMethod(method);
238 CHECK(Runtime::Current()->GetJit()->GetCodeCache()->ContainsMethod(method))
239 << PrettyMethod(method);
240 return true;
241}
242
243} // namespace jit
244} // namespace art