blob: 1d878c901cba07c0aea7a87e21fe161767cc459e [file] [log] [blame]
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001/*
2 * Copyright (C) 2012 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_llvm.h"
18
Shih-wei Liaoc4c98812012-03-10 21:55:51 -080019#include "class_linker.h"
Logan Chien8b977d32012-02-21 19:14:55 +080020#include "compilation_unit.h"
Logan Chienf7015fd2012-03-18 01:19:37 +080021#include "compiled_method.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080022#include "compiler.h"
Shih-wei Liaoc4c98812012-03-10 21:55:51 -080023#include "dex_cache.h"
Logan Chienf7015fd2012-03-18 01:19:37 +080024#include "elf_loader.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080025#include "ir_builder.h"
Logan Chien88894ee2012-02-13 16:42:22 +080026#include "jni_compiler.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080027#include "method_compiler.h"
Logan Chien4dd96f52012-02-29 01:26:58 +080028#include "oat_compilation_unit.h"
Logan Chien7f767612012-03-01 18:54:49 +080029#include "stl_util.h"
Logan Chienf04364f2012-02-10 12:01:39 +080030#include "upcall_compiler.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080031
Logan Chiendd7cf5b2012-03-01 12:55:19 +080032#include <llvm/LinkAllPasses.h>
33#include <llvm/LinkAllVMCore.h>
Logan Chien4c17dff2012-03-02 20:15:46 +080034#include <llvm/Support/CommandLine.h>
Logan Chien013b6f22012-03-02 17:20:33 +080035#include <llvm/Support/ManagedStatic.h>
Logan Chien8b977d32012-02-21 19:14:55 +080036#include <llvm/Support/TargetSelect.h>
37#include <llvm/Support/Threading.h>
38
Logan Chien013b6f22012-03-02 17:20:33 +080039namespace llvm {
40 extern bool TimePassesIsEnabled;
41}
42
Logan Chien4c17dff2012-03-02 20:15:46 +080043extern llvm::cl::opt<bool> EnableARMLongCalls;
44// NOTE: Although EnableARMLongCalls is defined in llvm/lib/Target/ARM/
45// ARMISelLowering.cpp, however, it is not in the llvm namespace.
46
Logan Chien8b977d32012-02-21 19:14:55 +080047
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080048namespace {
Logan Chien8b977d32012-02-21 19:14:55 +080049
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080050pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT;
51
52void InitializeLLVM() {
Logan Chien013b6f22012-03-02 17:20:33 +080053 // NOTE: Uncomment following line to show the time consumption of LLVM passes
54 //llvm::TimePassesIsEnabled = true;
55
Logan Chien8b977d32012-02-21 19:14:55 +080056 // Initialize LLVM target, MC subsystem, asm printer, and asm parser
57 llvm::InitializeAllTargets();
58 llvm::InitializeAllTargetMCs();
59 llvm::InitializeAllAsmPrinters();
60 llvm::InitializeAllAsmParsers();
61 // TODO: Maybe we don't have to initialize "all" targets.
62
Logan Chien4c17dff2012-03-02 20:15:46 +080063 // Enable -arm-long-calls
64 EnableARMLongCalls = true;
65
Logan Chiendd7cf5b2012-03-01 12:55:19 +080066 // Initialize LLVM optimization passes
67 llvm::PassRegistry &registry = *llvm::PassRegistry::getPassRegistry();
68
69 llvm::initializeCore(registry);
70 llvm::initializeScalarOpts(registry);
71 llvm::initializeIPO(registry);
72 llvm::initializeAnalysis(registry);
73 llvm::initializeIPA(registry);
74 llvm::initializeTransformUtils(registry);
75 llvm::initializeInstCombine(registry);
76 llvm::initializeInstrumentation(registry);
77 llvm::initializeTarget(registry);
78
Logan Chien8b977d32012-02-21 19:14:55 +080079 // Initialize LLVM internal data structure for multithreading
80 llvm::llvm_start_multithreaded();
81}
82
Logan Chienf1306552012-03-16 11:17:53 +080083// The Guard to Shutdown LLVM
Logan Chienaeb53032012-03-18 02:29:38 +080084// llvm::llvm_shutdown_obj llvm_guard;
85// TODO: We are commenting out this line because this will cause SEGV from
86// time to time.
87// Two reasons: (1) the order of the destruction of static objects, or
88// (2) dlopen/dlclose side-effect on static objects.
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080089
90} // anonymous namespace
91
92
93namespace art {
94namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080095
96
Logan Chiene75a8cc2012-02-24 12:26:43 +080097llvm::Module* makeLLVMModuleContents(llvm::Module* module);
Logan Chien42e0e152012-01-13 15:42:36 +080098
99
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800100CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set)
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -0800101 : compiler_(compiler), compiler_lock_("llvm_compiler_lock"),
Logan Chien7f767612012-03-01 18:54:49 +0800102 insn_set_(insn_set), curr_cunit_(NULL) {
Shih-wei Liaofc34adb2012-03-07 08:51:44 -0800103
104
105 // Initialize LLVM libraries
106 pthread_once(&llvm_initialized, InitializeLLVM);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800107}
108
109
110CompilerLLVM::~CompilerLLVM() {
Logan Chien7f767612012-03-01 18:54:49 +0800111 STLDeleteElements(&cunits_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800112}
113
114
Logan Chience119062012-03-02 11:57:34 +0800115void CompilerLLVM::EnsureCompilationUnit() {
Logan Chience119062012-03-02 11:57:34 +0800116 compiler_lock_.AssertHeld();
Logan Chien7f767612012-03-01 18:54:49 +0800117
118 if (curr_cunit_ != NULL) {
119 return;
Logan Chien8b977d32012-02-21 19:14:55 +0800120 }
Logan Chien7f767612012-03-01 18:54:49 +0800121
122 // Allocate compilation unit
123 size_t cunit_idx = cunits_.size();
124
Logan Chien6546ec52012-03-17 20:08:29 +0800125 curr_cunit_ = new CompilationUnit(insn_set_, cunit_idx);
Logan Chien7f767612012-03-01 18:54:49 +0800126
127 // Setup output filename
128 curr_cunit_->SetElfFileName(
129 StringPrintf("%s-%zu", elf_filename_.c_str(), cunit_idx));
130
131 if (IsBitcodeFileNameAvailable()) {
132 curr_cunit_->SetBitcodeFileName(
133 StringPrintf("%s-%zu", bitcode_filename_.c_str(), cunit_idx));
134 }
135
136 // Register compilation unit
137 cunits_.push_back(curr_cunit_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800138}
139
140
Logan Chien7f767612012-03-01 18:54:49 +0800141void CompilerLLVM::MaterializeRemainder() {
Logan Chien8b977d32012-02-21 19:14:55 +0800142 MutexLock GUARD(compiler_lock_);
Logan Chien7f767612012-03-01 18:54:49 +0800143 if (curr_cunit_ != NULL) {
Logan Chience119062012-03-02 11:57:34 +0800144 Materialize();
Logan Chien7f767612012-03-01 18:54:49 +0800145 }
146}
Logan Chien8b977d32012-02-21 19:14:55 +0800147
Logan Chien8b977d32012-02-21 19:14:55 +0800148
Logan Chien7f767612012-03-01 18:54:49 +0800149void CompilerLLVM::MaterializeIfThresholdReached() {
150 MutexLock GUARD(compiler_lock_);
151 if (curr_cunit_ != NULL && curr_cunit_->IsMaterializeThresholdReached()) {
Logan Chience119062012-03-02 11:57:34 +0800152 Materialize();
Logan Chien7f767612012-03-01 18:54:49 +0800153 }
154}
155
156
Logan Chience119062012-03-02 11:57:34 +0800157void CompilerLLVM::Materialize() {
158 compiler_lock_.AssertHeld();
159
Logan Chien7f767612012-03-01 18:54:49 +0800160 DCHECK(curr_cunit_ != NULL);
161 DCHECK(!curr_cunit_->IsMaterialized());
162
163 // Write bitcode to file when filename is set
164 if (IsBitcodeFileNameAvailable()) {
165 curr_cunit_->WriteBitcodeToFile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800166 }
167
Logan Chien8b977d32012-02-21 19:14:55 +0800168 // Materialize the llvm::Module into ELF object file
Logan Chien7f767612012-03-01 18:54:49 +0800169 curr_cunit_->Materialize();
Logan Chien8b977d32012-02-21 19:14:55 +0800170
Logan Chienf7015fd2012-03-18 01:19:37 +0800171 // Load ELF image when automatic ELF loading is enabled
172 if (IsAutoElfLoadingEnabled()) {
173 LoadElfFromCompilationUnit(curr_cunit_);
174 }
175
Logan Chien8b977d32012-02-21 19:14:55 +0800176 // Delete the compilation unit
Logan Chien7f767612012-03-01 18:54:49 +0800177 curr_cunit_ = NULL;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800178}
179
180
Logan Chienf7015fd2012-03-18 01:19:37 +0800181void CompilerLLVM::EnableAutoElfLoading() {
182 MutexLock GUARD(compiler_lock_);
183
184 if (IsAutoElfLoadingEnabled()) {
185 // If there is an existing ELF loader, then do nothing.
186 // Because the existing ELF loader may have returned some code address
187 // already. If we replace the existing ELF loader with
188 // elf_loader_.reset(...), then it is possible to have some dangling
189 // pointer.
190 return;
191 }
192
193 // Create ELF loader and load the materialized CompilationUnit
194 elf_loader_.reset(new ElfLoader());
195
196 for (size_t i = 0; i < cunits_.size(); ++i) {
197 if (cunits_[i]->IsMaterialized()) {
198 LoadElfFromCompilationUnit(cunits_[i]);
199 }
200 }
201}
202
203
204void CompilerLLVM::LoadElfFromCompilationUnit(const CompilationUnit* cunit) {
205 compiler_lock_.AssertHeld();
206 DCHECK(cunit->IsMaterialized()) << cunit->GetElfIndex();
207
208 if (!elf_loader_->LoadElfAt(cunit->GetElfIndex(),
209 cunit->GetElfImage(),
210 cunit->GetElfSize())) {
211 LOG(ERROR) << "Failed to load ELF from compilation unit "
212 << cunit->GetElfIndex();
213 }
214}
215
216
217const void* CompilerLLVM::GetMethodCodeAddr(const CompiledMethod* cm,
218 const Method* method) const {
219 return elf_loader_->GetMethodCodeAddr(cm->GetElfIndex(), method);
220}
221
222
223const Method::InvokeStub* CompilerLLVM::
224GetMethodInvokeStubAddr(const CompiledInvokeStub* cm, const Method* method) const {
225 return elf_loader_->GetMethodInvokeStubAddr(cm->GetElfIndex(), method);
226}
227
228
Logan Chien7f767612012-03-01 18:54:49 +0800229CompiledMethod* CompilerLLVM::
230CompileDexMethod(OatCompilationUnit* oat_compilation_unit) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800231 MutexLock GUARD(compiler_lock_);
232
Logan Chience119062012-03-02 11:57:34 +0800233 EnsureCompilationUnit();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800234
Logan Chien8b977d32012-02-21 19:14:55 +0800235 UniquePtr<MethodCompiler> method_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800236 new MethodCompiler(curr_cunit_, compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800237
Logan Chien7f767612012-03-01 18:54:49 +0800238 return method_compiler->Compile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800239}
Logan Chien83426162011-12-09 09:29:50 +0800240
241
Logan Chien7f767612012-03-01 18:54:49 +0800242CompiledMethod* CompilerLLVM::
243CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
Logan Chien88894ee2012-02-13 16:42:22 +0800244 MutexLock GUARD(compiler_lock_);
245
Logan Chience119062012-03-02 11:57:34 +0800246 EnsureCompilationUnit();
Logan Chien88894ee2012-02-13 16:42:22 +0800247
Logan Chien8b977d32012-02-21 19:14:55 +0800248 UniquePtr<JniCompiler> jni_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800249 new JniCompiler(curr_cunit_, *compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800250
Logan Chien7f767612012-03-01 18:54:49 +0800251 return jni_compiler->Compile();
Logan Chien88894ee2012-02-13 16:42:22 +0800252}
253
254
Logan Chienf04364f2012-02-10 12:01:39 +0800255CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
256 char const *shorty) {
Logan Chienf04364f2012-02-10 12:01:39 +0800257 MutexLock GUARD(compiler_lock_);
258
Logan Chience119062012-03-02 11:57:34 +0800259 EnsureCompilationUnit();
Logan Chienf04364f2012-02-10 12:01:39 +0800260
Logan Chien8b977d32012-02-21 19:14:55 +0800261 UniquePtr<UpcallCompiler> upcall_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800262 new UpcallCompiler(curr_cunit_, *compiler_));
Logan Chien8b977d32012-02-21 19:14:55 +0800263
Logan Chien7f767612012-03-01 18:54:49 +0800264 return upcall_compiler->CreateStub(is_static, shorty);
Logan Chienf04364f2012-02-10 12:01:39 +0800265}
266
Logan Chien83426162011-12-09 09:29:50 +0800267} // namespace compiler_llvm
268} // namespace art
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800269
Logan Chien106b2a02012-03-18 04:41:38 +0800270inline static art::compiler_llvm::CompilerLLVM* ContextOf(art::Compiler& compiler) {
271 void *compiler_context = compiler.GetCompilerContext();
272 CHECK(compiler_context != NULL);
273 return reinterpret_cast<art::compiler_llvm::CompilerLLVM*>(compiler_context);
274}
275
276inline static const art::compiler_llvm::CompilerLLVM* ContextOf(const art::Compiler& compiler) {
277 void *compiler_context = compiler.GetCompilerContext();
278 CHECK(compiler_context != NULL);
279 return reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler_context);
280}
281
282extern "C" void ArtInitCompilerContext(art::Compiler& compiler) {
283 CHECK(compiler.GetCompilerContext() == NULL);
284
285 art::compiler_llvm::CompilerLLVM* compiler_llvm =
286 new art::compiler_llvm::CompilerLLVM(&compiler,
287 compiler.GetInstructionSet());
288
289 compiler.SetCompilerContext(compiler_llvm);
290}
291
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700292extern "C" art::CompiledMethod* ArtCompileMethod(art::Compiler& compiler,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800293 const art::DexFile::CodeItem* code_item,
294 uint32_t access_flags, uint32_t method_idx,
295 const art::ClassLoader* class_loader,
296 const art::DexFile& dex_file)
297{
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800298 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
299 art::DexCache *dex_cache = class_linker->FindDexCache(dex_file);
300
301 art::OatCompilationUnit oat_compilation_unit(
302 class_loader, class_linker, dex_file, *dex_cache, code_item,
303 method_idx, access_flags);
304
Logan Chien106b2a02012-03-18 04:41:38 +0800305 return ContextOf(compiler)->CompileDexMethod(&oat_compilation_unit);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800306}
307
308extern "C" art::CompiledMethod* ArtJniCompileMethod(art::Compiler& compiler,
309 uint32_t access_flags, uint32_t method_idx,
310 const art::ClassLoader* class_loader,
311 const art::DexFile& dex_file) {
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800312 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
313 art::DexCache *dex_cache = class_linker->FindDexCache(dex_file);
314
315 art::OatCompilationUnit oat_compilation_unit(
316 class_loader, class_linker, dex_file, *dex_cache, NULL,
317 method_idx, access_flags);
318
Logan Chien106b2a02012-03-18 04:41:38 +0800319 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700320 art::CompiledMethod* result = compiler_llvm->CompileNativeMethod(&oat_compilation_unit);
321 compiler_llvm->MaterializeIfThresholdReached();
Elliott Hughes13b835a2012-03-13 19:45:22 -0700322 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800323}
324
325extern "C" art::CompiledInvokeStub* ArtCreateInvokeStub(art::Compiler& compiler, bool is_static,
326 const char* shorty, uint32_t shorty_len) {
Logan Chien106b2a02012-03-18 04:41:38 +0800327 return ContextOf(compiler)->CreateInvokeStub(is_static, shorty);
328}
329
330extern "C" void compilerLLVMSetElfFileName(art::Compiler& compiler,
331 std::string const& filename) {
332 ContextOf(compiler)->SetElfFileName(filename);
333}
334
335extern "C" void compilerLLVMSetBitcodeFileName(art::Compiler& compiler,
336 std::string const& filename) {
337 ContextOf(compiler)->SetBitcodeFileName(filename);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800338}
339
340extern "C" void compilerLLVMMaterializeRemainder(art::Compiler& compiler) {
Logan Chien106b2a02012-03-18 04:41:38 +0800341 ContextOf(compiler)->MaterializeRemainder();
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800342}
343
Logan Chienf7015fd2012-03-18 01:19:37 +0800344extern "C" void compilerLLVMEnableAutoElfLoading(art::Compiler& compiler) {
345 art::compiler_llvm::CompilerLLVM* compiler_llvm =
346 reinterpret_cast<art::compiler_llvm::CompilerLLVM*>(compiler.GetCompilerContext());
347 return compiler_llvm->EnableAutoElfLoading();
348}
349
350extern "C" const void* compilerLLVMGetMethodCodeAddr(const art::Compiler& compiler,
351 const art::CompiledMethod* cm,
352 const art::Method* method) {
353 const art::compiler_llvm::CompilerLLVM* compiler_llvm =
354 reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler.GetCompilerContext());
355 return compiler_llvm->GetMethodCodeAddr(cm, method);
356}
357
358extern "C" const art::Method::InvokeStub* compilerLLVMGetMethodInvokeStubAddr(const art::Compiler& compiler,
359 const art::CompiledInvokeStub* cm,
360 const art::Method* method) {
361 const art::compiler_llvm::CompilerLLVM* compiler_llvm =
362 reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler.GetCompilerContext());
363 return compiler_llvm->GetMethodInvokeStubAddr(cm, method);
364}
365
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800366// Note: Using this function carefully!!! This is temporary solution, we will remove it.
367extern "C" art::MutexLock* compilerLLVMMutexLock(art::Compiler& compiler) {
Logan Chien106b2a02012-03-18 04:41:38 +0800368 return new art::MutexLock(ContextOf(compiler)->compiler_lock_);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800369}
370
371extern "C" void compilerLLVMDispose(art::Compiler& compiler) {
Logan Chien106b2a02012-03-18 04:41:38 +0800372 delete ContextOf(compiler);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800373}