blob: e52478bc1ae3c286f8ec06f899b1c215f1dfe0ae [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 Liaod1fec812012-02-13 09:51:10 -080026#include "method_compiler.h"
Logan Chien4dd96f52012-02-29 01:26:58 +080027#include "oat_compilation_unit.h"
Logan Chien0c717dd2012-03-28 18:31:07 +080028#include "oat_file.h"
Logan Chien7f767612012-03-01 18:54:49 +080029#include "stl_util.h"
TDYa127eead4ac2012-06-03 07:15:25 -070030#include "stub_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 Chien013b6f22012-03-02 17:20:33 +080034#include <llvm/Support/ManagedStatic.h>
Logan Chien8b977d32012-02-21 19:14:55 +080035#include <llvm/Support/TargetSelect.h>
36#include <llvm/Support/Threading.h>
37
Logan Chien013b6f22012-03-02 17:20:33 +080038namespace llvm {
39 extern bool TimePassesIsEnabled;
40}
41
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080042namespace {
Logan Chien8b977d32012-02-21 19:14:55 +080043
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080044pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT;
45
46void InitializeLLVM() {
Logan Chienc3f8fa52012-05-11 11:23:39 +080047 // Initialize LLVM internal data structure for multithreading
48 llvm::llvm_start_multithreaded();
49
Logan Chien013b6f22012-03-02 17:20:33 +080050 // NOTE: Uncomment following line to show the time consumption of LLVM passes
51 //llvm::TimePassesIsEnabled = true;
52
Shih-wei Liao26e93072012-05-30 19:13:08 -070053 // Initialize LLVM target-specific options.
54 art::compiler_llvm::InitialBackendOptions();
Logan Chienc3f8fa52012-05-11 11:23:39 +080055
Shih-wei Liao1335a952012-07-23 18:03:00 -070056 // Initialize LLVM target, MC subsystem, asm printer, and asm parser.
57#if defined(ART_TARGET)
58 // Don't initialize all targets on device. Just initialize the device's native target
59 llvm::InitializeNativeTarget();
60 llvm::InitializeNativeTargetAsmPrinter();
61 llvm::InitializeNativeTargetAsmParser();
62#else
Logan Chien8b977d32012-02-21 19:14:55 +080063 llvm::InitializeAllTargets();
64 llvm::InitializeAllTargetMCs();
65 llvm::InitializeAllAsmPrinters();
66 llvm::InitializeAllAsmParsers();
Shih-wei Liao1335a952012-07-23 18:03:00 -070067#endif
Logan Chien8b977d32012-02-21 19:14:55 +080068
Logan Chiendd7cf5b2012-03-01 12:55:19 +080069 // Initialize LLVM optimization passes
70 llvm::PassRegistry &registry = *llvm::PassRegistry::getPassRegistry();
71
72 llvm::initializeCore(registry);
73 llvm::initializeScalarOpts(registry);
74 llvm::initializeIPO(registry);
75 llvm::initializeAnalysis(registry);
76 llvm::initializeIPA(registry);
77 llvm::initializeTransformUtils(registry);
78 llvm::initializeInstCombine(registry);
79 llvm::initializeInstrumentation(registry);
80 llvm::initializeTarget(registry);
Logan Chien8b977d32012-02-21 19:14:55 +080081}
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)
Logan Chien971bf3f2012-05-01 15:47:55 +0800101 : compiler_(compiler), insn_set_(insn_set),
102 num_cunits_lock_("compilation unit counter lock"), num_cunits_(0),
103 plt_(insn_set) {
Shih-wei Liaofc34adb2012-03-07 08:51:44 -0800104
105 // Initialize LLVM libraries
106 pthread_once(&llvm_initialized, InitializeLLVM);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800107}
108
109
110CompilerLLVM::~CompilerLLVM() {
111}
112
113
Logan Chien971bf3f2012-05-01 15:47:55 +0800114CompilationUnit* CompilerLLVM::AllocateCompilationUnit() {
115 MutexLock GUARD(num_cunits_lock_);
116 return new CompilationUnit(this, num_cunits_++);
Logan Chiendf576142012-03-20 17:36:32 +0800117}
118
119
Logan Chien7f767612012-03-01 18:54:49 +0800120CompiledMethod* CompilerLLVM::
121CompileDexMethod(OatCompilationUnit* oat_compilation_unit) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800122 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800123
Logan Chien8b977d32012-02-21 19:14:55 +0800124 UniquePtr<MethodCompiler> method_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800125 new MethodCompiler(cunit.get(), compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800126
Logan Chien7f767612012-03-01 18:54:49 +0800127 return method_compiler->Compile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800128}
Logan Chien83426162011-12-09 09:29:50 +0800129
130
Logan Chien7f767612012-03-01 18:54:49 +0800131CompiledMethod* CompilerLLVM::
132CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800133 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800134
Logan Chien8b977d32012-02-21 19:14:55 +0800135 UniquePtr<JniCompiler> jni_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800136 new JniCompiler(cunit.get(), *compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800137
Logan Chien7f767612012-03-01 18:54:49 +0800138 return jni_compiler->Compile();
Logan Chien88894ee2012-02-13 16:42:22 +0800139}
140
141
Logan Chienf04364f2012-02-10 12:01:39 +0800142CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
143 char const *shorty) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800144 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800145
TDYa127eead4ac2012-06-03 07:15:25 -0700146 UniquePtr<StubCompiler> stub_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800147 new StubCompiler(cunit.get(), *compiler_));
Logan Chien8b977d32012-02-21 19:14:55 +0800148
Logan Chien7a2a23a2012-06-06 11:01:00 +0800149 return stub_compiler->CreateInvokeStub(is_static, shorty);
150}
TDYa127eead4ac2012-06-03 07:15:25 -0700151
TDYa127eead4ac2012-06-03 07:15:25 -0700152
Logan Chien7a2a23a2012-06-06 11:01:00 +0800153CompiledInvokeStub* CompilerLLVM::CreateProxyStub(char const *shorty) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800154 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien7a2a23a2012-06-06 11:01:00 +0800155
156 UniquePtr<StubCompiler> stub_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800157 new StubCompiler(cunit.get(), *compiler_));
Logan Chien7a2a23a2012-06-06 11:01:00 +0800158
159 return stub_compiler->CreateProxyStub(shorty);
Logan Chienf04364f2012-02-10 12:01:39 +0800160}
161
Logan Chien83426162011-12-09 09:29:50 +0800162} // namespace compiler_llvm
163} // namespace art
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800164
Logan Chien106b2a02012-03-18 04:41:38 +0800165inline static art::compiler_llvm::CompilerLLVM* ContextOf(art::Compiler& compiler) {
166 void *compiler_context = compiler.GetCompilerContext();
167 CHECK(compiler_context != NULL);
168 return reinterpret_cast<art::compiler_llvm::CompilerLLVM*>(compiler_context);
169}
170
171inline static const art::compiler_llvm::CompilerLLVM* ContextOf(const art::Compiler& compiler) {
172 void *compiler_context = compiler.GetCompilerContext();
173 CHECK(compiler_context != NULL);
174 return reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler_context);
175}
176
177extern "C" void ArtInitCompilerContext(art::Compiler& compiler) {
178 CHECK(compiler.GetCompilerContext() == NULL);
179
180 art::compiler_llvm::CompilerLLVM* compiler_llvm =
181 new art::compiler_llvm::CompilerLLVM(&compiler,
182 compiler.GetInstructionSet());
183
184 compiler.SetCompilerContext(compiler_llvm);
185}
186
Logan Chien971bf3f2012-05-01 15:47:55 +0800187extern "C" void ArtUnInitCompilerContext(art::Compiler& compiler) {
188 delete ContextOf(compiler);
189 compiler.SetCompilerContext(NULL);
190}
191
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700192extern "C" art::CompiledMethod* ArtCompileMethod(art::Compiler& compiler,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800193 const art::DexFile::CodeItem* code_item,
194 uint32_t access_flags, uint32_t method_idx,
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700195 jobject class_loader,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800196 const art::DexFile& dex_file)
197{
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800198 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800199
200 art::OatCompilationUnit oat_compilation_unit(
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700201 class_loader, class_linker, dex_file, code_item,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800202 method_idx, access_flags);
TDYa1270200d072012-04-17 20:55:08 -0700203 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
204 art::CompiledMethod* result = compiler_llvm->CompileDexMethod(&oat_compilation_unit);
TDYa1270200d072012-04-17 20:55:08 -0700205 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800206}
207
208extern "C" art::CompiledMethod* ArtJniCompileMethod(art::Compiler& compiler,
209 uint32_t access_flags, uint32_t method_idx,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800210 const art::DexFile& dex_file) {
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800211 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800212
213 art::OatCompilationUnit oat_compilation_unit(
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700214 NULL, class_linker, dex_file, NULL,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800215 method_idx, access_flags);
216
Logan Chien106b2a02012-03-18 04:41:38 +0800217 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700218 art::CompiledMethod* result = compiler_llvm->CompileNativeMethod(&oat_compilation_unit);
Elliott Hughes13b835a2012-03-13 19:45:22 -0700219 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800220}
221
Logan Chien7a2a23a2012-06-06 11:01:00 +0800222extern "C" art::CompiledInvokeStub* ArtCreateInvokeStub(art::Compiler& compiler,
223 bool is_static,
224 const char* shorty,
225 uint32_t shorty_len) {
TDYa1270200d072012-04-17 20:55:08 -0700226 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
227 art::CompiledInvokeStub* result = compiler_llvm->CreateInvokeStub(is_static, shorty);
TDYa1270200d072012-04-17 20:55:08 -0700228 return result;
Logan Chien106b2a02012-03-18 04:41:38 +0800229}
230
Logan Chien7a2a23a2012-06-06 11:01:00 +0800231extern "C" art::CompiledInvokeStub* ArtCreateProxyStub(art::Compiler& compiler,
232 const char* shorty,
233 uint32_t shorty_len) {
234 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
235 art::CompiledInvokeStub* result = compiler_llvm->CreateProxyStub(shorty);
Logan Chien7a2a23a2012-06-06 11:01:00 +0800236 return result;
237}
238
Logan Chien106b2a02012-03-18 04:41:38 +0800239extern "C" void compilerLLVMSetBitcodeFileName(art::Compiler& compiler,
240 std::string const& filename) {
241 ContextOf(compiler)->SetBitcodeFileName(filename);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800242}