blob: 3a1bebcd2ea3393199c691442649061fb304ca40 [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
Logan Chien013b6f22012-03-02 17:20:33 +080041namespace llvm {
42 extern bool TimePassesIsEnabled;
43}
44
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080045namespace {
Logan Chien8b977d32012-02-21 19:14:55 +080046
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080047pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT;
48
49void InitializeLLVM() {
Logan Chienc3f8fa52012-05-11 11:23:39 +080050 // Initialize LLVM internal data structure for multithreading
51 llvm::llvm_start_multithreaded();
52
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
Shih-wei Liao26e93072012-05-30 19:13:08 -070056 // Initialize LLVM target-specific options.
57 art::compiler_llvm::InitialBackendOptions();
Logan Chienc3f8fa52012-05-11 11:23:39 +080058
Shih-wei Liao1335a952012-07-23 18:03:00 -070059 // Initialize LLVM target, MC subsystem, asm printer, and asm parser.
60#if defined(ART_TARGET)
61 // Don't initialize all targets on device. Just initialize the device's native target
62 llvm::InitializeNativeTarget();
63 llvm::InitializeNativeTargetAsmPrinter();
64 llvm::InitializeNativeTargetAsmParser();
65#else
Logan Chien8b977d32012-02-21 19:14:55 +080066 llvm::InitializeAllTargets();
67 llvm::InitializeAllTargetMCs();
68 llvm::InitializeAllAsmPrinters();
69 llvm::InitializeAllAsmParsers();
Shih-wei Liao1335a952012-07-23 18:03:00 -070070#endif
Logan Chien8b977d32012-02-21 19:14:55 +080071
Logan Chiendd7cf5b2012-03-01 12:55:19 +080072 // Initialize LLVM optimization passes
73 llvm::PassRegistry &registry = *llvm::PassRegistry::getPassRegistry();
74
75 llvm::initializeCore(registry);
76 llvm::initializeScalarOpts(registry);
77 llvm::initializeIPO(registry);
78 llvm::initializeAnalysis(registry);
79 llvm::initializeIPA(registry);
80 llvm::initializeTransformUtils(registry);
81 llvm::initializeInstCombine(registry);
82 llvm::initializeInstrumentation(registry);
83 llvm::initializeTarget(registry);
Logan Chien8b977d32012-02-21 19:14:55 +080084}
85
Logan Chienf1306552012-03-16 11:17:53 +080086// The Guard to Shutdown LLVM
Logan Chienaeb53032012-03-18 02:29:38 +080087// llvm::llvm_shutdown_obj llvm_guard;
88// TODO: We are commenting out this line because this will cause SEGV from
89// time to time.
90// Two reasons: (1) the order of the destruction of static objects, or
91// (2) dlopen/dlclose side-effect on static objects.
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080092
93} // anonymous namespace
94
95
96namespace art {
97namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080098
99
Logan Chiene75a8cc2012-02-24 12:26:43 +0800100llvm::Module* makeLLVMModuleContents(llvm::Module* module);
Logan Chien42e0e152012-01-13 15:42:36 +0800101
102
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800103CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set)
Logan Chien971bf3f2012-05-01 15:47:55 +0800104 : compiler_(compiler), insn_set_(insn_set),
105 num_cunits_lock_("compilation unit counter lock"), num_cunits_(0),
106 plt_(insn_set) {
Shih-wei Liaofc34adb2012-03-07 08:51:44 -0800107
108 // Initialize LLVM libraries
109 pthread_once(&llvm_initialized, InitializeLLVM);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800110}
111
112
113CompilerLLVM::~CompilerLLVM() {
114}
115
116
Logan Chien971bf3f2012-05-01 15:47:55 +0800117CompilationUnit* CompilerLLVM::AllocateCompilationUnit() {
118 MutexLock GUARD(num_cunits_lock_);
TDYa127b672d1e2012-06-28 21:21:45 -0700119 CompilationUnit* cunit = new CompilationUnit(this, num_cunits_++);
120 if (!bitcode_filename_.empty()) {
121 cunit->SetBitcodeFileName(StringPrintf("%s-%zu", bitcode_filename_.c_str(), num_cunits_-1));
122 }
123 return cunit;
Logan Chiendf576142012-03-20 17:36:32 +0800124}
125
126
Logan Chien7f767612012-03-01 18:54:49 +0800127CompiledMethod* CompilerLLVM::
128CompileDexMethod(OatCompilationUnit* oat_compilation_unit) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800129 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800130
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700131#ifdef ART_USE_DEXLANG_FRONTEND
132 // Run DexLang for Dex to Greenland Bitcode
133 UniquePtr<greenland::DexLang> dex_lang(
134 new greenland::DexLang(*cunit->GetDexLangContext(), *compiler_,
135 *oat_compilation_unit));
136
137 llvm::Function* func = dex_lang->Build();
138 CHECK(func != NULL);
139
140 cunit->Materialize();
141
142 return new CompiledMethod(cunit->GetInstructionSet(),
143 cunit->GetCompiledCode());
144#else
Logan Chien8b977d32012-02-21 19:14:55 +0800145 UniquePtr<MethodCompiler> method_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800146 new MethodCompiler(cunit.get(), compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800147
Logan Chien7f767612012-03-01 18:54:49 +0800148 return method_compiler->Compile();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700149#endif
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800150}
Logan Chien83426162011-12-09 09:29:50 +0800151
152
Logan Chien7f767612012-03-01 18:54:49 +0800153CompiledMethod* CompilerLLVM::
154CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800155 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800156
Logan Chien8b977d32012-02-21 19:14:55 +0800157 UniquePtr<JniCompiler> jni_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800158 new JniCompiler(cunit.get(), *compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800159
Logan Chien7f767612012-03-01 18:54:49 +0800160 return jni_compiler->Compile();
Logan Chien88894ee2012-02-13 16:42:22 +0800161}
162
163
Logan Chienf04364f2012-02-10 12:01:39 +0800164CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
165 char const *shorty) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800166 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800167
TDYa127eead4ac2012-06-03 07:15:25 -0700168 UniquePtr<StubCompiler> stub_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800169 new StubCompiler(cunit.get(), *compiler_));
Logan Chien8b977d32012-02-21 19:14:55 +0800170
Logan Chien7a2a23a2012-06-06 11:01:00 +0800171 return stub_compiler->CreateInvokeStub(is_static, shorty);
172}
TDYa127eead4ac2012-06-03 07:15:25 -0700173
TDYa127eead4ac2012-06-03 07:15:25 -0700174
Logan Chien7a2a23a2012-06-06 11:01:00 +0800175CompiledInvokeStub* CompilerLLVM::CreateProxyStub(char const *shorty) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800176 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien7a2a23a2012-06-06 11:01:00 +0800177
178 UniquePtr<StubCompiler> stub_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800179 new StubCompiler(cunit.get(), *compiler_));
Logan Chien7a2a23a2012-06-06 11:01:00 +0800180
181 return stub_compiler->CreateProxyStub(shorty);
Logan Chienf04364f2012-02-10 12:01:39 +0800182}
183
Logan Chien83426162011-12-09 09:29:50 +0800184} // namespace compiler_llvm
185} // namespace art
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800186
Logan Chien106b2a02012-03-18 04:41:38 +0800187inline static art::compiler_llvm::CompilerLLVM* ContextOf(art::Compiler& compiler) {
188 void *compiler_context = compiler.GetCompilerContext();
189 CHECK(compiler_context != NULL);
190 return reinterpret_cast<art::compiler_llvm::CompilerLLVM*>(compiler_context);
191}
192
193inline static const art::compiler_llvm::CompilerLLVM* ContextOf(const art::Compiler& compiler) {
194 void *compiler_context = compiler.GetCompilerContext();
195 CHECK(compiler_context != NULL);
196 return reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler_context);
197}
198
199extern "C" void ArtInitCompilerContext(art::Compiler& compiler) {
200 CHECK(compiler.GetCompilerContext() == NULL);
201
202 art::compiler_llvm::CompilerLLVM* compiler_llvm =
203 new art::compiler_llvm::CompilerLLVM(&compiler,
204 compiler.GetInstructionSet());
205
206 compiler.SetCompilerContext(compiler_llvm);
207}
208
Logan Chien971bf3f2012-05-01 15:47:55 +0800209extern "C" void ArtUnInitCompilerContext(art::Compiler& compiler) {
210 delete ContextOf(compiler);
211 compiler.SetCompilerContext(NULL);
212}
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700213extern "C" art::CompiledMethod* ArtCompileMethod(art::Compiler& compiler,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800214 const art::DexFile::CodeItem* code_item,
Ian Rogers08f753d2012-08-24 14:35:25 -0700215 uint32_t access_flags,
216 art::InvokeType invoke_type,
217 uint32_t method_idx,
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700218 jobject class_loader,
Ian Rogers08f753d2012-08-24 14:35:25 -0700219 const art::DexFile& dex_file) {
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800220 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800221
222 art::OatCompilationUnit oat_compilation_unit(
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700223 class_loader, class_linker, dex_file, code_item,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800224 method_idx, access_flags);
TDYa1270200d072012-04-17 20:55:08 -0700225 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
226 art::CompiledMethod* result = compiler_llvm->CompileDexMethod(&oat_compilation_unit);
TDYa1270200d072012-04-17 20:55:08 -0700227 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800228}
229
230extern "C" art::CompiledMethod* ArtJniCompileMethod(art::Compiler& compiler,
231 uint32_t access_flags, uint32_t method_idx,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800232 const art::DexFile& dex_file) {
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800233 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800234
235 art::OatCompilationUnit oat_compilation_unit(
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700236 NULL, class_linker, dex_file, NULL,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800237 method_idx, access_flags);
238
Logan Chien106b2a02012-03-18 04:41:38 +0800239 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700240 art::CompiledMethod* result = compiler_llvm->CompileNativeMethod(&oat_compilation_unit);
Elliott Hughes13b835a2012-03-13 19:45:22 -0700241 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800242}
243
Logan Chien7a2a23a2012-06-06 11:01:00 +0800244extern "C" art::CompiledInvokeStub* ArtCreateInvokeStub(art::Compiler& compiler,
245 bool is_static,
246 const char* shorty,
247 uint32_t shorty_len) {
TDYa1270200d072012-04-17 20:55:08 -0700248 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
249 art::CompiledInvokeStub* result = compiler_llvm->CreateInvokeStub(is_static, shorty);
TDYa1270200d072012-04-17 20:55:08 -0700250 return result;
Logan Chien106b2a02012-03-18 04:41:38 +0800251}
252
Logan Chien7a2a23a2012-06-06 11:01:00 +0800253extern "C" art::CompiledInvokeStub* ArtCreateProxyStub(art::Compiler& compiler,
254 const char* shorty,
255 uint32_t shorty_len) {
256 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
257 art::CompiledInvokeStub* result = compiler_llvm->CreateProxyStub(shorty);
Logan Chien7a2a23a2012-06-06 11:01:00 +0800258 return result;
259}
260
Logan Chien106b2a02012-03-18 04:41:38 +0800261extern "C" void compilerLLVMSetBitcodeFileName(art::Compiler& compiler,
262 std::string const& filename) {
263 ContextOf(compiler)->SetBitcodeFileName(filename);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800264}