blob: a964b4009b4918c22f9d5a08153f195ec43443ff [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 Liao26e93072012-05-30 19:13:08 -070019#include "backend_options.h"
Shih-wei Liaoc4c98812012-03-10 21:55:51 -080020#include "class_linker.h"
Logan Chien8b977d32012-02-21 19:14:55 +080021#include "compilation_unit.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"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070026#ifndef ART_USE_DEXLANG_FRONTEND
27# include "method_compiler.h"
28#endif
Logan Chien4dd96f52012-02-29 01:26:58 +080029#include "oat_compilation_unit.h"
Logan Chien0c717dd2012-03-28 18:31:07 +080030#include "oat_file.h"
Logan Chien7f767612012-03-01 18:54:49 +080031#include "stl_util.h"
TDYa127eead4ac2012-06-03 07:15:25 -070032#include "stub_compiler.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070033#include "utils_llvm.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080034
Logan Chiendd7cf5b2012-03-01 12:55:19 +080035#include <llvm/LinkAllPasses.h>
36#include <llvm/LinkAllVMCore.h>
Logan Chien013b6f22012-03-02 17:20:33 +080037#include <llvm/Support/ManagedStatic.h>
Logan Chien8b977d32012-02-21 19:14:55 +080038#include <llvm/Support/TargetSelect.h>
39#include <llvm/Support/Threading.h>
40
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070041#if defined(ART_USE_QUICK_COMPILER)
42namespace art {
43void oatCompileMethodToGBC(Compiler& compiler,
44 const DexFile::CodeItem* code_item,
45 uint32_t access_flags, InvokeType invoke_type,
46 uint32_t method_idx, jobject class_loader,
47 const DexFile& dex_file,
buzbee4df2bbd2012-10-11 14:46:06 -070048 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
Logan Chien971bf3f2012-05-01 15:47:55 +0800128CompilationUnit* CompilerLLVM::AllocateCompilationUnit() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700129 MutexLock GUARD(Thread::Current(), num_cunits_lock_);
TDYa127b672d1e2012-06-28 21:21:45 -0700130 CompilationUnit* cunit = new CompilationUnit(this, num_cunits_++);
131 if (!bitcode_filename_.empty()) {
132 cunit->SetBitcodeFileName(StringPrintf("%s-%zu", bitcode_filename_.c_str(), num_cunits_-1));
133 }
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) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800140 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800141
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700142#if defined(ART_USE_DEXLANG_FRONTEND)
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700143 // Run DexLang for Dex to Greenland Bitcode
144 UniquePtr<greenland::DexLang> dex_lang(
145 new greenland::DexLang(*cunit->GetDexLangContext(), *compiler_,
146 *oat_compilation_unit));
147
148 llvm::Function* func = dex_lang->Build();
149 CHECK(func != NULL);
150
151 cunit->Materialize();
152
153 return new CompiledMethod(cunit->GetInstructionSet(),
154 cunit->GetCompiledCode());
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700155#elif defined(ART_USE_QUICK_COMPILER)
156 std::string methodName(PrettyMethod(oat_compilation_unit->GetDexMethodIndex(),
157 *oat_compilation_unit->GetDexFile()));
TDYa12787caa7e2012-08-25 23:23:27 -0700158 if (insn_set_ == kX86) {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700159 // Use iceland
160 UniquePtr<MethodCompiler> method_compiler(
161 new MethodCompiler(cunit.get(), compiler_, oat_compilation_unit));
162
163 return method_compiler->Compile();
164 } else {
165 // Use quick
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700166 oatCompileMethodToGBC(*compiler_,
167 oat_compilation_unit->GetCodeItem(),
168 oat_compilation_unit->access_flags_,
169 invoke_type,
170 oat_compilation_unit->GetDexMethodIndex(),
171 oat_compilation_unit->GetClassLoader(),
172 *oat_compilation_unit->GetDexFile(),
TDYa12755e5e6c2012-09-11 15:14:42 -0700173 cunit->GetQuickContext()
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700174 );
175
176 cunit->SetCompiler(compiler_);
177 cunit->SetOatCompilationUnit(oat_compilation_unit);
178
179 cunit->Materialize();
180
181 return new CompiledMethod(cunit->GetInstructionSet(),
182 cunit->GetCompiledCode());
183 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700184#else
Logan Chien8b977d32012-02-21 19:14:55 +0800185 UniquePtr<MethodCompiler> method_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800186 new MethodCompiler(cunit.get(), compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800187
Logan Chien7f767612012-03-01 18:54:49 +0800188 return method_compiler->Compile();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700189#endif
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800190}
Logan Chien83426162011-12-09 09:29:50 +0800191
192
Logan Chien7f767612012-03-01 18:54:49 +0800193CompiledMethod* CompilerLLVM::
194CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800195 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800196
Logan Chien8b977d32012-02-21 19:14:55 +0800197 UniquePtr<JniCompiler> jni_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800198 new JniCompiler(cunit.get(), *compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800199
Logan Chien7f767612012-03-01 18:54:49 +0800200 return jni_compiler->Compile();
Logan Chien88894ee2012-02-13 16:42:22 +0800201}
202
203
Logan Chienf04364f2012-02-10 12:01:39 +0800204CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
205 char const *shorty) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800206 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800207
TDYa127eead4ac2012-06-03 07:15:25 -0700208 UniquePtr<StubCompiler> stub_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800209 new StubCompiler(cunit.get(), *compiler_));
Logan Chien8b977d32012-02-21 19:14:55 +0800210
Logan Chien7a2a23a2012-06-06 11:01:00 +0800211 return stub_compiler->CreateInvokeStub(is_static, shorty);
212}
TDYa127eead4ac2012-06-03 07:15:25 -0700213
TDYa127eead4ac2012-06-03 07:15:25 -0700214
Logan Chien7a2a23a2012-06-06 11:01:00 +0800215CompiledInvokeStub* CompilerLLVM::CreateProxyStub(char const *shorty) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800216 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien7a2a23a2012-06-06 11:01:00 +0800217
218 UniquePtr<StubCompiler> stub_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800219 new StubCompiler(cunit.get(), *compiler_));
Logan Chien7a2a23a2012-06-06 11:01:00 +0800220
221 return stub_compiler->CreateProxyStub(shorty);
Logan Chienf04364f2012-02-10 12:01:39 +0800222}
223
Logan Chien83426162011-12-09 09:29:50 +0800224} // namespace compiler_llvm
225} // namespace art
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800226
Logan Chien106b2a02012-03-18 04:41:38 +0800227inline static art::compiler_llvm::CompilerLLVM* ContextOf(art::Compiler& compiler) {
228 void *compiler_context = compiler.GetCompilerContext();
229 CHECK(compiler_context != NULL);
230 return reinterpret_cast<art::compiler_llvm::CompilerLLVM*>(compiler_context);
231}
232
233inline static const art::compiler_llvm::CompilerLLVM* ContextOf(const art::Compiler& compiler) {
234 void *compiler_context = compiler.GetCompilerContext();
235 CHECK(compiler_context != NULL);
236 return reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler_context);
237}
238
239extern "C" void ArtInitCompilerContext(art::Compiler& compiler) {
240 CHECK(compiler.GetCompilerContext() == NULL);
241
242 art::compiler_llvm::CompilerLLVM* compiler_llvm =
243 new art::compiler_llvm::CompilerLLVM(&compiler,
244 compiler.GetInstructionSet());
245
246 compiler.SetCompilerContext(compiler_llvm);
247}
248
Logan Chien971bf3f2012-05-01 15:47:55 +0800249extern "C" void ArtUnInitCompilerContext(art::Compiler& compiler) {
250 delete ContextOf(compiler);
251 compiler.SetCompilerContext(NULL);
252}
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700253extern "C" art::CompiledMethod* ArtCompileMethod(art::Compiler& compiler,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800254 const art::DexFile::CodeItem* code_item,
Ian Rogers08f753d2012-08-24 14:35:25 -0700255 uint32_t access_flags,
256 art::InvokeType invoke_type,
257 uint32_t method_idx,
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700258 jobject class_loader,
Ian Rogers08f753d2012-08-24 14:35:25 -0700259 const art::DexFile& dex_file) {
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800260 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800261
262 art::OatCompilationUnit oat_compilation_unit(
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700263 class_loader, class_linker, dex_file, code_item,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800264 method_idx, access_flags);
TDYa1270200d072012-04-17 20:55:08 -0700265 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700266 art::CompiledMethod* result = compiler_llvm->CompileDexMethod(&oat_compilation_unit, invoke_type);
TDYa1270200d072012-04-17 20:55:08 -0700267 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800268}
269
270extern "C" art::CompiledMethod* ArtJniCompileMethod(art::Compiler& compiler,
271 uint32_t access_flags, uint32_t method_idx,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800272 const art::DexFile& dex_file) {
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800273 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800274
275 art::OatCompilationUnit oat_compilation_unit(
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700276 NULL, class_linker, dex_file, NULL,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800277 method_idx, access_flags);
278
Logan Chien106b2a02012-03-18 04:41:38 +0800279 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700280 art::CompiledMethod* result = compiler_llvm->CompileNativeMethod(&oat_compilation_unit);
Elliott Hughes13b835a2012-03-13 19:45:22 -0700281 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800282}
283
Logan Chien7a2a23a2012-06-06 11:01:00 +0800284extern "C" art::CompiledInvokeStub* ArtCreateInvokeStub(art::Compiler& compiler,
285 bool is_static,
286 const char* shorty,
287 uint32_t shorty_len) {
TDYa1270200d072012-04-17 20:55:08 -0700288 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
289 art::CompiledInvokeStub* result = compiler_llvm->CreateInvokeStub(is_static, shorty);
TDYa1270200d072012-04-17 20:55:08 -0700290 return result;
Logan Chien106b2a02012-03-18 04:41:38 +0800291}
292
Logan Chien7a2a23a2012-06-06 11:01:00 +0800293extern "C" art::CompiledInvokeStub* ArtCreateProxyStub(art::Compiler& compiler,
294 const char* shorty,
295 uint32_t shorty_len) {
296 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
297 art::CompiledInvokeStub* result = compiler_llvm->CreateProxyStub(shorty);
Logan Chien7a2a23a2012-06-06 11:01:00 +0800298 return result;
299}
300
Logan Chien106b2a02012-03-18 04:41:38 +0800301extern "C" void compilerLLVMSetBitcodeFileName(art::Compiler& compiler,
302 std::string const& filename) {
303 ContextOf(compiler)->SetBitcodeFileName(filename);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800304}