blob: aa5ec82ad6d6b8a67e04373e8da07f0692360899 [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
buzbeec531cef2012-10-18 07:09:20 -070041#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070042namespace art {
buzbeec531cef2012-10-18 07:09:20 -070043void oatCompileMethod(Compiler& compiler,
44 const CompilerBackend compilerBackend,
45 const DexFile::CodeItem* code_item,
46 uint32_t access_flags, InvokeType invoke_type,
47 uint32_t method_idx, jobject class_loader,
48 const DexFile& dex_file,
49 LLVMInfo* llvm_info);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070050}
51#endif
52
Logan Chien013b6f22012-03-02 17:20:33 +080053namespace llvm {
54 extern bool TimePassesIsEnabled;
55}
56
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080057namespace {
Logan Chien8b977d32012-02-21 19:14:55 +080058
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080059pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT;
60
61void InitializeLLVM() {
Logan Chienc3f8fa52012-05-11 11:23:39 +080062 // Initialize LLVM internal data structure for multithreading
63 llvm::llvm_start_multithreaded();
64
Logan Chien013b6f22012-03-02 17:20:33 +080065 // NOTE: Uncomment following line to show the time consumption of LLVM passes
66 //llvm::TimePassesIsEnabled = true;
67
Shih-wei Liao26e93072012-05-30 19:13:08 -070068 // Initialize LLVM target-specific options.
69 art::compiler_llvm::InitialBackendOptions();
Logan Chienc3f8fa52012-05-11 11:23:39 +080070
Shih-wei Liao1335a952012-07-23 18:03:00 -070071 // Initialize LLVM target, MC subsystem, asm printer, and asm parser.
72#if defined(ART_TARGET)
73 // Don't initialize all targets on device. Just initialize the device's native target
74 llvm::InitializeNativeTarget();
75 llvm::InitializeNativeTargetAsmPrinter();
76 llvm::InitializeNativeTargetAsmParser();
77#else
Logan Chien8b977d32012-02-21 19:14:55 +080078 llvm::InitializeAllTargets();
79 llvm::InitializeAllTargetMCs();
80 llvm::InitializeAllAsmPrinters();
81 llvm::InitializeAllAsmParsers();
Shih-wei Liao1335a952012-07-23 18:03:00 -070082#endif
Logan Chien8b977d32012-02-21 19:14:55 +080083
Logan Chiendd7cf5b2012-03-01 12:55:19 +080084 // Initialize LLVM optimization passes
85 llvm::PassRegistry &registry = *llvm::PassRegistry::getPassRegistry();
86
87 llvm::initializeCore(registry);
88 llvm::initializeScalarOpts(registry);
89 llvm::initializeIPO(registry);
90 llvm::initializeAnalysis(registry);
91 llvm::initializeIPA(registry);
92 llvm::initializeTransformUtils(registry);
93 llvm::initializeInstCombine(registry);
94 llvm::initializeInstrumentation(registry);
95 llvm::initializeTarget(registry);
Logan Chien8b977d32012-02-21 19:14:55 +080096}
97
Logan Chienf1306552012-03-16 11:17:53 +080098// The Guard to Shutdown LLVM
Logan Chienaeb53032012-03-18 02:29:38 +080099// llvm::llvm_shutdown_obj llvm_guard;
100// TODO: We are commenting out this line because this will cause SEGV from
101// time to time.
102// Two reasons: (1) the order of the destruction of static objects, or
103// (2) dlopen/dlclose side-effect on static objects.
Shih-wei Liaofc34adb2012-03-07 08:51:44 -0800104
105} // anonymous namespace
106
107
108namespace art {
109namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800110
111
Logan Chiene75a8cc2012-02-24 12:26:43 +0800112llvm::Module* makeLLVMModuleContents(llvm::Module* module);
Logan Chien42e0e152012-01-13 15:42:36 +0800113
114
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800115CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set)
Logan Chien971bf3f2012-05-01 15:47:55 +0800116 : compiler_(compiler), insn_set_(insn_set),
117 num_cunits_lock_("compilation unit counter lock"), num_cunits_(0),
118 plt_(insn_set) {
Shih-wei Liaofc34adb2012-03-07 08:51:44 -0800119
120 // Initialize LLVM libraries
121 pthread_once(&llvm_initialized, InitializeLLVM);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800122}
123
124
125CompilerLLVM::~CompilerLLVM() {
126}
127
128
Logan Chien971bf3f2012-05-01 15:47:55 +0800129CompilationUnit* CompilerLLVM::AllocateCompilationUnit() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700130 MutexLock GUARD(Thread::Current(), num_cunits_lock_);
TDYa127b672d1e2012-06-28 21:21:45 -0700131 CompilationUnit* cunit = new CompilationUnit(this, num_cunits_++);
132 if (!bitcode_filename_.empty()) {
133 cunit->SetBitcodeFileName(StringPrintf("%s-%zu", bitcode_filename_.c_str(), num_cunits_-1));
134 }
135 return cunit;
Logan Chiendf576142012-03-20 17:36:32 +0800136}
137
138
Logan Chien7f767612012-03-01 18:54:49 +0800139CompiledMethod* CompilerLLVM::
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700140CompileDexMethod(OatCompilationUnit* oat_compilation_unit, InvokeType invoke_type) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800141 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800142
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700143#if defined(ART_USE_DEXLANG_FRONTEND)
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700144 // Run DexLang for Dex to Greenland Bitcode
145 UniquePtr<greenland::DexLang> dex_lang(
146 new greenland::DexLang(*cunit->GetDexLangContext(), *compiler_,
147 *oat_compilation_unit));
148
149 llvm::Function* func = dex_lang->Build();
150 CHECK(func != NULL);
151
152 cunit->Materialize();
153
154 return new CompiledMethod(cunit->GetInstructionSet(),
155 cunit->GetCompiledCode());
buzbeec531cef2012-10-18 07:09:20 -0700156#elif defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700157 std::string methodName(PrettyMethod(oat_compilation_unit->GetDexMethodIndex(),
158 *oat_compilation_unit->GetDexFile()));
TDYa12787caa7e2012-08-25 23:23:27 -0700159 if (insn_set_ == kX86) {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700160 // Use iceland
161 UniquePtr<MethodCompiler> method_compiler(
162 new MethodCompiler(cunit.get(), compiler_, oat_compilation_unit));
163
164 return method_compiler->Compile();
165 } else {
buzbeec531cef2012-10-18 07:09:20 -0700166 // TODO: consolidate ArtCompileMethods
167 oatCompileMethod(*compiler_,
168 kPortable,
169 oat_compilation_unit->GetCodeItem(),
170 oat_compilation_unit->access_flags_,
171 invoke_type,
172 oat_compilation_unit->GetDexMethodIndex(),
173 oat_compilation_unit->GetClassLoader(),
174 *oat_compilation_unit->GetDexFile(),
175 cunit->GetQuickContext()
176 );
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700177
178 cunit->SetCompiler(compiler_);
179 cunit->SetOatCompilationUnit(oat_compilation_unit);
180
181 cunit->Materialize();
182
183 return new CompiledMethod(cunit->GetInstructionSet(),
184 cunit->GetCompiledCode());
185 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700186#else
Logan Chien8b977d32012-02-21 19:14:55 +0800187 UniquePtr<MethodCompiler> method_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800188 new MethodCompiler(cunit.get(), compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800189
Logan Chien7f767612012-03-01 18:54:49 +0800190 return method_compiler->Compile();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700191#endif
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800192}
Logan Chien83426162011-12-09 09:29:50 +0800193
194
Logan Chien7f767612012-03-01 18:54:49 +0800195CompiledMethod* CompilerLLVM::
196CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800197 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800198
Logan Chien8b977d32012-02-21 19:14:55 +0800199 UniquePtr<JniCompiler> jni_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800200 new JniCompiler(cunit.get(), *compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800201
Logan Chien7f767612012-03-01 18:54:49 +0800202 return jni_compiler->Compile();
Logan Chien88894ee2012-02-13 16:42:22 +0800203}
204
205
Logan Chienf04364f2012-02-10 12:01:39 +0800206CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
207 char const *shorty) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800208 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800209
TDYa127eead4ac2012-06-03 07:15:25 -0700210 UniquePtr<StubCompiler> stub_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800211 new StubCompiler(cunit.get(), *compiler_));
Logan Chien8b977d32012-02-21 19:14:55 +0800212
Logan Chien7a2a23a2012-06-06 11:01:00 +0800213 return stub_compiler->CreateInvokeStub(is_static, shorty);
214}
TDYa127eead4ac2012-06-03 07:15:25 -0700215
TDYa127eead4ac2012-06-03 07:15:25 -0700216
Logan Chien7a2a23a2012-06-06 11:01:00 +0800217CompiledInvokeStub* CompilerLLVM::CreateProxyStub(char const *shorty) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800218 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien7a2a23a2012-06-06 11:01:00 +0800219
220 UniquePtr<StubCompiler> stub_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800221 new StubCompiler(cunit.get(), *compiler_));
Logan Chien7a2a23a2012-06-06 11:01:00 +0800222
223 return stub_compiler->CreateProxyStub(shorty);
Logan Chienf04364f2012-02-10 12:01:39 +0800224}
225
Logan Chien83426162011-12-09 09:29:50 +0800226} // namespace compiler_llvm
227} // namespace art
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800228
Logan Chien106b2a02012-03-18 04:41:38 +0800229inline static art::compiler_llvm::CompilerLLVM* ContextOf(art::Compiler& compiler) {
230 void *compiler_context = compiler.GetCompilerContext();
231 CHECK(compiler_context != NULL);
232 return reinterpret_cast<art::compiler_llvm::CompilerLLVM*>(compiler_context);
233}
234
235inline static const art::compiler_llvm::CompilerLLVM* ContextOf(const art::Compiler& compiler) {
236 void *compiler_context = compiler.GetCompilerContext();
237 CHECK(compiler_context != NULL);
238 return reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler_context);
239}
240
241extern "C" void ArtInitCompilerContext(art::Compiler& compiler) {
242 CHECK(compiler.GetCompilerContext() == NULL);
243
244 art::compiler_llvm::CompilerLLVM* compiler_llvm =
245 new art::compiler_llvm::CompilerLLVM(&compiler,
246 compiler.GetInstructionSet());
247
248 compiler.SetCompilerContext(compiler_llvm);
249}
250
Logan Chien971bf3f2012-05-01 15:47:55 +0800251extern "C" void ArtUnInitCompilerContext(art::Compiler& compiler) {
252 delete ContextOf(compiler);
253 compiler.SetCompilerContext(NULL);
254}
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700255extern "C" art::CompiledMethod* ArtCompileMethod(art::Compiler& compiler,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800256 const art::DexFile::CodeItem* code_item,
Ian Rogers08f753d2012-08-24 14:35:25 -0700257 uint32_t access_flags,
258 art::InvokeType invoke_type,
259 uint32_t method_idx,
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700260 jobject class_loader,
Ian Rogers08f753d2012-08-24 14:35:25 -0700261 const art::DexFile& dex_file) {
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800262 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800263
264 art::OatCompilationUnit oat_compilation_unit(
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700265 class_loader, class_linker, dex_file, code_item,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800266 method_idx, access_flags);
TDYa1270200d072012-04-17 20:55:08 -0700267 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700268 art::CompiledMethod* result = compiler_llvm->CompileDexMethod(&oat_compilation_unit, invoke_type);
TDYa1270200d072012-04-17 20:55:08 -0700269 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800270}
271
272extern "C" art::CompiledMethod* ArtJniCompileMethod(art::Compiler& compiler,
273 uint32_t access_flags, uint32_t method_idx,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800274 const art::DexFile& dex_file) {
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800275 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800276
277 art::OatCompilationUnit oat_compilation_unit(
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700278 NULL, class_linker, dex_file, NULL,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800279 method_idx, access_flags);
280
Logan Chien106b2a02012-03-18 04:41:38 +0800281 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700282 art::CompiledMethod* result = compiler_llvm->CompileNativeMethod(&oat_compilation_unit);
Elliott Hughes13b835a2012-03-13 19:45:22 -0700283 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800284}
285
Logan Chien7a2a23a2012-06-06 11:01:00 +0800286extern "C" art::CompiledInvokeStub* ArtCreateInvokeStub(art::Compiler& compiler,
287 bool is_static,
288 const char* shorty,
289 uint32_t shorty_len) {
TDYa1270200d072012-04-17 20:55:08 -0700290 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
291 art::CompiledInvokeStub* result = compiler_llvm->CreateInvokeStub(is_static, shorty);
TDYa1270200d072012-04-17 20:55:08 -0700292 return result;
Logan Chien106b2a02012-03-18 04:41:38 +0800293}
294
Logan Chien7a2a23a2012-06-06 11:01:00 +0800295extern "C" art::CompiledInvokeStub* ArtCreateProxyStub(art::Compiler& compiler,
296 const char* shorty,
297 uint32_t shorty_len) {
298 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
299 art::CompiledInvokeStub* result = compiler_llvm->CreateProxyStub(shorty);
Logan Chien7a2a23a2012-06-06 11:01:00 +0800300 return result;
301}
302
Logan Chien106b2a02012-03-18 04:41:38 +0800303extern "C" void compilerLLVMSetBitcodeFileName(art::Compiler& compiler,
304 std::string const& filename) {
305 ContextOf(compiler)->SetBitcodeFileName(filename);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800306}