blob: 72379116a285b18f6fd2cd54a6690eb70fcbc2ee [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
Elliott Hughes1aa246d2012-12-13 09:29:36 -080019#include "base/stl_util.h"
Shih-wei Liao26e93072012-05-30 19:13:08 -070020#include "backend_options.h"
Shih-wei Liaoc4c98812012-03-10 21:55:51 -080021#include "class_linker.h"
Logan Chienf7015fd2012-03-18 01:19:37 +080022#include "compiled_method.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080023#include "compiler.h"
24#include "ir_builder.h"
Logan Chien88894ee2012-02-13 16:42:22 +080025#include "jni_compiler.h"
Brian Carlstrom641ce032013-01-31 15:21:37 -080026#include "llvm_compilation_unit.h"
jeffhao40283122013-01-15 13:15:24 -080027#include "method_compiler.h"
Logan Chien4dd96f52012-02-29 01:26:58 +080028#include "oat_compilation_unit.h"
Logan Chien0c717dd2012-03-28 18:31:07 +080029#include "oat_file.h"
TDYa127eead4ac2012-06-03 07:15:25 -070030#include "stub_compiler.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070031#include "utils_llvm.h"
TDYa127ce4cc0d2012-11-18 16:59:53 -080032#include "verifier/method_verifier.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080033
Logan Chiendd7cf5b2012-03-01 12:55:19 +080034#include <llvm/LinkAllPasses.h>
35#include <llvm/LinkAllVMCore.h>
Logan Chien013b6f22012-03-02 17:20:33 +080036#include <llvm/Support/ManagedStatic.h>
Logan Chien8b977d32012-02-21 19:14:55 +080037#include <llvm/Support/TargetSelect.h>
38#include <llvm/Support/Threading.h>
39
buzbeec531cef2012-10-18 07:09:20 -070040#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070041namespace art {
buzbee52a77fc2012-11-20 19:50:46 -080042void CompileOneMethod(Compiler& compiler,
buzbeec531cef2012-10-18 07:09:20 -070043 const CompilerBackend compilerBackend,
44 const DexFile::CodeItem* code_item,
45 uint32_t access_flags, InvokeType invoke_type,
TDYa127dc5daa02013-01-09 21:31:37 +080046 uint32_t class_def_idx, uint32_t method_idx, jobject class_loader,
buzbeec531cef2012-10-18 07:09:20 -070047 const DexFile& dex_file,
48 LLVMInfo* llvm_info);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070049}
50#endif
51
Logan Chien013b6f22012-03-02 17:20:33 +080052namespace llvm {
53 extern bool TimePassesIsEnabled;
54}
55
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080056namespace {
Logan Chien8b977d32012-02-21 19:14:55 +080057
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080058pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT;
59
60void InitializeLLVM() {
Logan Chienc3f8fa52012-05-11 11:23:39 +080061 // Initialize LLVM internal data structure for multithreading
62 llvm::llvm_start_multithreaded();
63
Logan Chien013b6f22012-03-02 17:20:33 +080064 // NOTE: Uncomment following line to show the time consumption of LLVM passes
65 //llvm::TimePassesIsEnabled = true;
66
Shih-wei Liao26e93072012-05-30 19:13:08 -070067 // Initialize LLVM target-specific options.
68 art::compiler_llvm::InitialBackendOptions();
Logan Chienc3f8fa52012-05-11 11:23:39 +080069
Shih-wei Liao1335a952012-07-23 18:03:00 -070070 // Initialize LLVM target, MC subsystem, asm printer, and asm parser.
71#if defined(ART_TARGET)
72 // Don't initialize all targets on device. Just initialize the device's native target
73 llvm::InitializeNativeTarget();
74 llvm::InitializeNativeTargetAsmPrinter();
75 llvm::InitializeNativeTargetAsmParser();
76#else
Logan Chien8b977d32012-02-21 19:14:55 +080077 llvm::InitializeAllTargets();
78 llvm::InitializeAllTargetMCs();
79 llvm::InitializeAllAsmPrinters();
80 llvm::InitializeAllAsmParsers();
Shih-wei Liao1335a952012-07-23 18:03:00 -070081#endif
Logan Chien8b977d32012-02-21 19:14:55 +080082
Logan Chiendd7cf5b2012-03-01 12:55:19 +080083 // Initialize LLVM optimization passes
84 llvm::PassRegistry &registry = *llvm::PassRegistry::getPassRegistry();
85
86 llvm::initializeCore(registry);
87 llvm::initializeScalarOpts(registry);
88 llvm::initializeIPO(registry);
89 llvm::initializeAnalysis(registry);
90 llvm::initializeIPA(registry);
91 llvm::initializeTransformUtils(registry);
92 llvm::initializeInstCombine(registry);
93 llvm::initializeInstrumentation(registry);
94 llvm::initializeTarget(registry);
Logan Chien8b977d32012-02-21 19:14:55 +080095}
96
Logan Chienf1306552012-03-16 11:17:53 +080097// The Guard to Shutdown LLVM
Logan Chienaeb53032012-03-18 02:29:38 +080098// llvm::llvm_shutdown_obj llvm_guard;
99// TODO: We are commenting out this line because this will cause SEGV from
100// time to time.
101// Two reasons: (1) the order of the destruction of static objects, or
102// (2) dlopen/dlclose side-effect on static objects.
Shih-wei Liaofc34adb2012-03-07 08:51:44 -0800103
104} // anonymous namespace
105
106
107namespace art {
108namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800109
110
Logan Chiene75a8cc2012-02-24 12:26:43 +0800111llvm::Module* makeLLVMModuleContents(llvm::Module* module);
Logan Chien42e0e152012-01-13 15:42:36 +0800112
113
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800114CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set)
Logan Chien971bf3f2012-05-01 15:47:55 +0800115 : compiler_(compiler), insn_set_(insn_set),
116 num_cunits_lock_("compilation unit counter lock"), num_cunits_(0),
117 plt_(insn_set) {
Shih-wei Liaofc34adb2012-03-07 08:51:44 -0800118
119 // Initialize LLVM libraries
120 pthread_once(&llvm_initialized, InitializeLLVM);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800121}
122
123
124CompilerLLVM::~CompilerLLVM() {
125}
126
127
Brian Carlstrom641ce032013-01-31 15:21:37 -0800128LlvmCompilationUnit* CompilerLLVM::AllocateCompilationUnit() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700129 MutexLock GUARD(Thread::Current(), num_cunits_lock_);
Brian Carlstrom641ce032013-01-31 15:21:37 -0800130 LlvmCompilationUnit* cunit = new LlvmCompilationUnit(this, num_cunits_++);
TDYa127b672d1e2012-06-28 21:21:45 -0700131 if (!bitcode_filename_.empty()) {
Brian Carlstrom641ce032013-01-31 15:21:37 -0800132 cunit->SetBitcodeFileName(StringPrintf("%s-%zu", bitcode_filename_.c_str(), cunit->GetIndex()));
TDYa127b672d1e2012-06-28 21:21:45 -0700133 }
134 return cunit;
Logan Chiendf576142012-03-20 17:36:32 +0800135}
136
137
Logan Chien7f767612012-03-01 18:54:49 +0800138CompiledMethod* CompilerLLVM::
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700139CompileDexMethod(OatCompilationUnit* oat_compilation_unit, InvokeType invoke_type) {
Brian Carlstrom641ce032013-01-31 15:21:37 -0800140 UniquePtr<LlvmCompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800141
jeffhao40283122013-01-15 13:15:24 -0800142#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700143 std::string methodName(PrettyMethod(oat_compilation_unit->GetDexMethodIndex(),
144 *oat_compilation_unit->GetDexFile()));
TDYa12787caa7e2012-08-25 23:23:27 -0700145 if (insn_set_ == kX86) {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700146 // Use iceland
147 UniquePtr<MethodCompiler> method_compiler(
148 new MethodCompiler(cunit.get(), compiler_, oat_compilation_unit));
149
150 return method_compiler->Compile();
151 } else {
buzbee26f10ee2012-12-21 11:16:29 -0800152
buzbeec531cef2012-10-18 07:09:20 -0700153 // TODO: consolidate ArtCompileMethods
buzbee52a77fc2012-11-20 19:50:46 -0800154 CompileOneMethod(*compiler_,
buzbeec531cef2012-10-18 07:09:20 -0700155 kPortable,
156 oat_compilation_unit->GetCodeItem(),
157 oat_compilation_unit->access_flags_,
158 invoke_type,
TDYa127dc5daa02013-01-09 21:31:37 +0800159 oat_compilation_unit->GetClassDefIndex(),
buzbeec531cef2012-10-18 07:09:20 -0700160 oat_compilation_unit->GetDexMethodIndex(),
161 oat_compilation_unit->GetClassLoader(),
162 *oat_compilation_unit->GetDexFile(),
163 cunit->GetQuickContext()
164 );
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700165
166 cunit->SetCompiler(compiler_);
167 cunit->SetOatCompilationUnit(oat_compilation_unit);
168
169 cunit->Materialize();
170
buzbee26f10ee2012-12-21 11:16:29 -0800171 Compiler::MethodReference mref(oat_compilation_unit->GetDexFile(),
172 oat_compilation_unit->GetDexMethodIndex());
173 return new CompiledMethod(compiler_->GetInstructionSet(),
174 cunit->GetCompiledCode(),
TDYa127ce4cc0d2012-11-18 16:59:53 -0800175 *verifier::MethodVerifier::GetDexGcMap(mref));
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700176 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700177#else
Logan Chien8b977d32012-02-21 19:14:55 +0800178 UniquePtr<MethodCompiler> method_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800179 new MethodCompiler(cunit.get(), compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800180
Logan Chien7f767612012-03-01 18:54:49 +0800181 return method_compiler->Compile();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700182#endif
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800183}
Logan Chien83426162011-12-09 09:29:50 +0800184
185
Logan Chien7f767612012-03-01 18:54:49 +0800186CompiledMethod* CompilerLLVM::
187CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
Brian Carlstrom641ce032013-01-31 15:21:37 -0800188 UniquePtr<LlvmCompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800189
Logan Chien8b977d32012-02-21 19:14:55 +0800190 UniquePtr<JniCompiler> jni_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800191 new JniCompiler(cunit.get(), *compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800192
Logan Chien7f767612012-03-01 18:54:49 +0800193 return jni_compiler->Compile();
Logan Chien88894ee2012-02-13 16:42:22 +0800194}
195
196
Logan Chienf04364f2012-02-10 12:01:39 +0800197CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
198 char const *shorty) {
Brian Carlstrom641ce032013-01-31 15:21:37 -0800199 UniquePtr<LlvmCompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800200
TDYa127eead4ac2012-06-03 07:15:25 -0700201 UniquePtr<StubCompiler> stub_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800202 new StubCompiler(cunit.get(), *compiler_));
Logan Chien8b977d32012-02-21 19:14:55 +0800203
Logan Chien7a2a23a2012-06-06 11:01:00 +0800204 return stub_compiler->CreateInvokeStub(is_static, shorty);
205}
TDYa127eead4ac2012-06-03 07:15:25 -0700206
TDYa127eead4ac2012-06-03 07:15:25 -0700207
Logan Chien7a2a23a2012-06-06 11:01:00 +0800208CompiledInvokeStub* CompilerLLVM::CreateProxyStub(char const *shorty) {
Brian Carlstrom641ce032013-01-31 15:21:37 -0800209 UniquePtr<LlvmCompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien7a2a23a2012-06-06 11:01:00 +0800210
211 UniquePtr<StubCompiler> stub_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800212 new StubCompiler(cunit.get(), *compiler_));
Logan Chien7a2a23a2012-06-06 11:01:00 +0800213
214 return stub_compiler->CreateProxyStub(shorty);
Logan Chienf04364f2012-02-10 12:01:39 +0800215}
216
Logan Chien83426162011-12-09 09:29:50 +0800217} // namespace compiler_llvm
218} // namespace art
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800219
Logan Chien106b2a02012-03-18 04:41:38 +0800220inline static art::compiler_llvm::CompilerLLVM* ContextOf(art::Compiler& compiler) {
221 void *compiler_context = compiler.GetCompilerContext();
222 CHECK(compiler_context != NULL);
223 return reinterpret_cast<art::compiler_llvm::CompilerLLVM*>(compiler_context);
224}
225
226inline static const art::compiler_llvm::CompilerLLVM* ContextOf(const art::Compiler& compiler) {
227 void *compiler_context = compiler.GetCompilerContext();
228 CHECK(compiler_context != NULL);
229 return reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler_context);
230}
231
232extern "C" void ArtInitCompilerContext(art::Compiler& compiler) {
233 CHECK(compiler.GetCompilerContext() == NULL);
234
235 art::compiler_llvm::CompilerLLVM* compiler_llvm =
236 new art::compiler_llvm::CompilerLLVM(&compiler,
237 compiler.GetInstructionSet());
238
239 compiler.SetCompilerContext(compiler_llvm);
240}
241
Logan Chien971bf3f2012-05-01 15:47:55 +0800242extern "C" void ArtUnInitCompilerContext(art::Compiler& compiler) {
243 delete ContextOf(compiler);
244 compiler.SetCompilerContext(NULL);
245}
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700246extern "C" art::CompiledMethod* ArtCompileMethod(art::Compiler& compiler,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800247 const art::DexFile::CodeItem* code_item,
Ian Rogers08f753d2012-08-24 14:35:25 -0700248 uint32_t access_flags,
249 art::InvokeType invoke_type,
Ian Rogersfffdb022013-01-04 15:14:08 -0800250 uint32_t class_def_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700251 uint32_t method_idx,
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700252 jobject class_loader,
Ian Rogers08f753d2012-08-24 14:35:25 -0700253 const art::DexFile& dex_file) {
Ian Rogersfffdb022013-01-04 15:14:08 -0800254 UNUSED(class_def_idx); // TODO: this is used with Compiler::RequiresConstructorBarrier.
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800255 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800256
257 art::OatCompilationUnit oat_compilation_unit(
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700258 class_loader, class_linker, dex_file, code_item,
TDYa127dc5daa02013-01-09 21:31:37 +0800259 class_def_idx, method_idx, access_flags);
TDYa1270200d072012-04-17 20:55:08 -0700260 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700261 art::CompiledMethod* result = compiler_llvm->CompileDexMethod(&oat_compilation_unit, invoke_type);
TDYa1270200d072012-04-17 20:55:08 -0700262 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800263}
264
265extern "C" art::CompiledMethod* ArtJniCompileMethod(art::Compiler& compiler,
266 uint32_t access_flags, uint32_t method_idx,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800267 const art::DexFile& dex_file) {
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800268 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800269
270 art::OatCompilationUnit oat_compilation_unit(
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700271 NULL, class_linker, dex_file, NULL,
TDYa127dc5daa02013-01-09 21:31:37 +0800272 0, method_idx, access_flags);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800273
Logan Chien106b2a02012-03-18 04:41:38 +0800274 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700275 art::CompiledMethod* result = compiler_llvm->CompileNativeMethod(&oat_compilation_unit);
Elliott Hughes13b835a2012-03-13 19:45:22 -0700276 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800277}
278
buzbee02031b12012-11-23 09:41:35 -0800279extern "C" art::CompiledInvokeStub* ArtCreateLLVMInvokeStub(art::Compiler& compiler,
280 bool is_static,
281 const char* shorty,
282 uint32_t shorty_len) {
TDYa1270200d072012-04-17 20:55:08 -0700283 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
284 art::CompiledInvokeStub* result = compiler_llvm->CreateInvokeStub(is_static, shorty);
TDYa1270200d072012-04-17 20:55:08 -0700285 return result;
Logan Chien106b2a02012-03-18 04:41:38 +0800286}
287
Logan Chien7a2a23a2012-06-06 11:01:00 +0800288extern "C" art::CompiledInvokeStub* ArtCreateProxyStub(art::Compiler& compiler,
289 const char* shorty,
290 uint32_t shorty_len) {
291 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
292 art::CompiledInvokeStub* result = compiler_llvm->CreateProxyStub(shorty);
Logan Chien7a2a23a2012-06-06 11:01:00 +0800293 return result;
294}
295
Logan Chien106b2a02012-03-18 04:41:38 +0800296extern "C" void compilerLLVMSetBitcodeFileName(art::Compiler& compiler,
297 std::string const& filename) {
298 ContextOf(compiler)->SetBitcodeFileName(filename);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800299}