blob: b793fbda64731d54d8fde0dd80bf665f04399283 [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"
Shih-wei Liaoc4c98812012-03-10 21:55:51 -080024#include "dex_cache.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080025#include "ir_builder.h"
Logan Chien88894ee2012-02-13 16:42:22 +080026#include "jni_compiler.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -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"
Logan Chien7f767612012-03-01 18:54:49 +080030#include "stl_util.h"
TDYa127eead4ac2012-06-03 07:15:25 -070031#include "stub_compiler.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080032
Logan Chiendd7cf5b2012-03-01 12:55:19 +080033#include <llvm/LinkAllPasses.h>
34#include <llvm/LinkAllVMCore.h>
Logan Chien013b6f22012-03-02 17:20:33 +080035#include <llvm/Support/ManagedStatic.h>
Logan Chien8b977d32012-02-21 19:14:55 +080036#include <llvm/Support/TargetSelect.h>
37#include <llvm/Support/Threading.h>
38
Logan Chien013b6f22012-03-02 17:20:33 +080039namespace llvm {
40 extern bool TimePassesIsEnabled;
41}
42
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080043namespace {
Logan Chien8b977d32012-02-21 19:14:55 +080044
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080045pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT;
46
47void InitializeLLVM() {
Logan Chienc3f8fa52012-05-11 11:23:39 +080048 // Initialize LLVM internal data structure for multithreading
49 llvm::llvm_start_multithreaded();
50
Logan Chien013b6f22012-03-02 17:20:33 +080051 // NOTE: Uncomment following line to show the time consumption of LLVM passes
52 //llvm::TimePassesIsEnabled = true;
53
Shih-wei Liao26e93072012-05-30 19:13:08 -070054 // Initialize LLVM target-specific options.
55 art::compiler_llvm::InitialBackendOptions();
Logan Chienc3f8fa52012-05-11 11:23:39 +080056
Logan Chien8b977d32012-02-21 19:14:55 +080057 // Initialize LLVM target, MC subsystem, asm printer, and asm parser
58 llvm::InitializeAllTargets();
59 llvm::InitializeAllTargetMCs();
60 llvm::InitializeAllAsmPrinters();
61 llvm::InitializeAllAsmParsers();
62 // TODO: Maybe we don't have to initialize "all" targets.
63
Logan Chiendd7cf5b2012-03-01 12:55:19 +080064 // Initialize LLVM optimization passes
65 llvm::PassRegistry &registry = *llvm::PassRegistry::getPassRegistry();
66
67 llvm::initializeCore(registry);
68 llvm::initializeScalarOpts(registry);
69 llvm::initializeIPO(registry);
70 llvm::initializeAnalysis(registry);
71 llvm::initializeIPA(registry);
72 llvm::initializeTransformUtils(registry);
73 llvm::initializeInstCombine(registry);
74 llvm::initializeInstrumentation(registry);
75 llvm::initializeTarget(registry);
Logan Chien8b977d32012-02-21 19:14:55 +080076}
77
Logan Chienf1306552012-03-16 11:17:53 +080078// The Guard to Shutdown LLVM
Logan Chienaeb53032012-03-18 02:29:38 +080079// llvm::llvm_shutdown_obj llvm_guard;
80// TODO: We are commenting out this line because this will cause SEGV from
81// time to time.
82// Two reasons: (1) the order of the destruction of static objects, or
83// (2) dlopen/dlclose side-effect on static objects.
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080084
85} // anonymous namespace
86
87
88namespace art {
89namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080090
91
Logan Chiene75a8cc2012-02-24 12:26:43 +080092llvm::Module* makeLLVMModuleContents(llvm::Module* module);
Logan Chien42e0e152012-01-13 15:42:36 +080093
94
Shih-wei Liaod1fec812012-02-13 09:51:10 -080095CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set)
Logan Chien971bf3f2012-05-01 15:47:55 +080096 : compiler_(compiler), insn_set_(insn_set),
97 num_cunits_lock_("compilation unit counter lock"), num_cunits_(0),
98 plt_(insn_set) {
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080099
100 // Initialize LLVM libraries
101 pthread_once(&llvm_initialized, InitializeLLVM);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800102}
103
104
105CompilerLLVM::~CompilerLLVM() {
106}
107
108
Logan Chien971bf3f2012-05-01 15:47:55 +0800109CompilationUnit* CompilerLLVM::AllocateCompilationUnit() {
110 MutexLock GUARD(num_cunits_lock_);
111 return new CompilationUnit(this, num_cunits_++);
Logan Chiendf576142012-03-20 17:36:32 +0800112}
113
114
Logan Chien7f767612012-03-01 18:54:49 +0800115CompiledMethod* CompilerLLVM::
116CompileDexMethod(OatCompilationUnit* oat_compilation_unit) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800117 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800118
Logan Chien8b977d32012-02-21 19:14:55 +0800119 UniquePtr<MethodCompiler> method_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800120 new MethodCompiler(cunit.get(), compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800121
Logan Chien7f767612012-03-01 18:54:49 +0800122 return method_compiler->Compile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800123}
Logan Chien83426162011-12-09 09:29:50 +0800124
125
Logan Chien7f767612012-03-01 18:54:49 +0800126CompiledMethod* CompilerLLVM::
127CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800128 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800129
Logan Chien8b977d32012-02-21 19:14:55 +0800130 UniquePtr<JniCompiler> jni_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800131 new JniCompiler(cunit.get(), *compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800132
Logan Chien7f767612012-03-01 18:54:49 +0800133 return jni_compiler->Compile();
Logan Chien88894ee2012-02-13 16:42:22 +0800134}
135
136
Logan Chienf04364f2012-02-10 12:01:39 +0800137CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
138 char const *shorty) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800139 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800140
TDYa127eead4ac2012-06-03 07:15:25 -0700141 UniquePtr<StubCompiler> stub_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800142 new StubCompiler(cunit.get(), *compiler_));
Logan Chien8b977d32012-02-21 19:14:55 +0800143
Logan Chien7a2a23a2012-06-06 11:01:00 +0800144 return stub_compiler->CreateInvokeStub(is_static, shorty);
145}
TDYa127eead4ac2012-06-03 07:15:25 -0700146
TDYa127eead4ac2012-06-03 07:15:25 -0700147
Logan Chien7a2a23a2012-06-06 11:01:00 +0800148CompiledInvokeStub* CompilerLLVM::CreateProxyStub(char const *shorty) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800149 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien7a2a23a2012-06-06 11:01:00 +0800150
151 UniquePtr<StubCompiler> stub_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800152 new StubCompiler(cunit.get(), *compiler_));
Logan Chien7a2a23a2012-06-06 11:01:00 +0800153
154 return stub_compiler->CreateProxyStub(shorty);
Logan Chienf04364f2012-02-10 12:01:39 +0800155}
156
Logan Chien83426162011-12-09 09:29:50 +0800157} // namespace compiler_llvm
158} // namespace art
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800159
Logan Chien106b2a02012-03-18 04:41:38 +0800160inline static art::compiler_llvm::CompilerLLVM* ContextOf(art::Compiler& compiler) {
161 void *compiler_context = compiler.GetCompilerContext();
162 CHECK(compiler_context != NULL);
163 return reinterpret_cast<art::compiler_llvm::CompilerLLVM*>(compiler_context);
164}
165
166inline static const art::compiler_llvm::CompilerLLVM* ContextOf(const art::Compiler& compiler) {
167 void *compiler_context = compiler.GetCompilerContext();
168 CHECK(compiler_context != NULL);
169 return reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler_context);
170}
171
172extern "C" void ArtInitCompilerContext(art::Compiler& compiler) {
173 CHECK(compiler.GetCompilerContext() == NULL);
174
175 art::compiler_llvm::CompilerLLVM* compiler_llvm =
176 new art::compiler_llvm::CompilerLLVM(&compiler,
177 compiler.GetInstructionSet());
178
179 compiler.SetCompilerContext(compiler_llvm);
180}
181
Logan Chien971bf3f2012-05-01 15:47:55 +0800182extern "C" void ArtUnInitCompilerContext(art::Compiler& compiler) {
183 delete ContextOf(compiler);
184 compiler.SetCompilerContext(NULL);
185}
186
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700187extern "C" art::CompiledMethod* ArtCompileMethod(art::Compiler& compiler,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800188 const art::DexFile::CodeItem* code_item,
189 uint32_t access_flags, uint32_t method_idx,
190 const art::ClassLoader* class_loader,
191 const art::DexFile& dex_file)
192{
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800193 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
194 art::DexCache *dex_cache = class_linker->FindDexCache(dex_file);
195
196 art::OatCompilationUnit oat_compilation_unit(
197 class_loader, class_linker, dex_file, *dex_cache, code_item,
198 method_idx, access_flags);
TDYa1270200d072012-04-17 20:55:08 -0700199 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
200 art::CompiledMethod* result = compiler_llvm->CompileDexMethod(&oat_compilation_unit);
TDYa1270200d072012-04-17 20:55:08 -0700201 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800202}
203
204extern "C" art::CompiledMethod* ArtJniCompileMethod(art::Compiler& compiler,
205 uint32_t access_flags, uint32_t method_idx,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800206 const art::DexFile& dex_file) {
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800207 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
208 art::DexCache *dex_cache = class_linker->FindDexCache(dex_file);
209
210 art::OatCompilationUnit oat_compilation_unit(
Shih-wei Liaob1ab7df2012-03-29 13:53:46 -0700211 NULL, class_linker, dex_file, *dex_cache, NULL,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800212 method_idx, access_flags);
213
Logan Chien106b2a02012-03-18 04:41:38 +0800214 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700215 art::CompiledMethod* result = compiler_llvm->CompileNativeMethod(&oat_compilation_unit);
Elliott Hughes13b835a2012-03-13 19:45:22 -0700216 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800217}
218
Logan Chien7a2a23a2012-06-06 11:01:00 +0800219extern "C" art::CompiledInvokeStub* ArtCreateInvokeStub(art::Compiler& compiler,
220 bool is_static,
221 const char* shorty,
222 uint32_t shorty_len) {
TDYa1270200d072012-04-17 20:55:08 -0700223 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
224 art::CompiledInvokeStub* result = compiler_llvm->CreateInvokeStub(is_static, shorty);
TDYa1270200d072012-04-17 20:55:08 -0700225 return result;
Logan Chien106b2a02012-03-18 04:41:38 +0800226}
227
Logan Chien7a2a23a2012-06-06 11:01:00 +0800228extern "C" art::CompiledInvokeStub* ArtCreateProxyStub(art::Compiler& compiler,
229 const char* shorty,
230 uint32_t shorty_len) {
231 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
232 art::CompiledInvokeStub* result = compiler_llvm->CreateProxyStub(shorty);
Logan Chien7a2a23a2012-06-06 11:01:00 +0800233 return result;
234}
235
Logan Chien106b2a02012-03-18 04:41:38 +0800236extern "C" void compilerLLVMSetBitcodeFileName(art::Compiler& compiler,
237 std::string const& filename) {
238 ContextOf(compiler)->SetBitcodeFileName(filename);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800239}