blob: d528615f7c53ad8c200cefcceadfb68abe62da19 [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 Liaoc4c98812012-03-10 21:55:51 -080019#include "class_linker.h"
Logan Chien8b977d32012-02-21 19:14:55 +080020#include "compilation_unit.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080021#include "compiler.h"
Shih-wei Liaoc4c98812012-03-10 21:55:51 -080022#include "dex_cache.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080023#include "ir_builder.h"
Logan Chien88894ee2012-02-13 16:42:22 +080024#include "jni_compiler.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080025#include "method_compiler.h"
Logan Chien4dd96f52012-02-29 01:26:58 +080026#include "oat_compilation_unit.h"
Logan Chien7f767612012-03-01 18:54:49 +080027#include "stl_util.h"
Logan Chienf04364f2012-02-10 12:01:39 +080028#include "upcall_compiler.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080029
Logan Chiendd7cf5b2012-03-01 12:55:19 +080030#include <llvm/LinkAllPasses.h>
31#include <llvm/LinkAllVMCore.h>
Logan Chien4c17dff2012-03-02 20:15:46 +080032#include <llvm/Support/CommandLine.h>
Logan Chien013b6f22012-03-02 17:20:33 +080033#include <llvm/Support/ManagedStatic.h>
Logan Chien8b977d32012-02-21 19:14:55 +080034#include <llvm/Support/TargetSelect.h>
35#include <llvm/Support/Threading.h>
36
Logan Chien013b6f22012-03-02 17:20:33 +080037namespace llvm {
38 extern bool TimePassesIsEnabled;
39}
40
Logan Chien4c17dff2012-03-02 20:15:46 +080041extern llvm::cl::opt<bool> EnableARMLongCalls;
42// NOTE: Although EnableARMLongCalls is defined in llvm/lib/Target/ARM/
43// ARMISelLowering.cpp, however, it is not in the llvm namespace.
44
Logan Chien8b977d32012-02-21 19:14:55 +080045
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080046namespace {
Logan Chien8b977d32012-02-21 19:14:55 +080047
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080048pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT;
49
50void InitializeLLVM() {
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
Logan Chien8b977d32012-02-21 19:14:55 +080054 // Initialize LLVM target, MC subsystem, asm printer, and asm parser
55 llvm::InitializeAllTargets();
56 llvm::InitializeAllTargetMCs();
57 llvm::InitializeAllAsmPrinters();
58 llvm::InitializeAllAsmParsers();
59 // TODO: Maybe we don't have to initialize "all" targets.
60
Logan Chien4c17dff2012-03-02 20:15:46 +080061 // Enable -arm-long-calls
62 EnableARMLongCalls = true;
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);
76
Logan Chien8b977d32012-02-21 19:14:55 +080077 // Initialize LLVM internal data structure for multithreading
78 llvm::llvm_start_multithreaded();
79}
80
Logan Chienf1306552012-03-16 11:17:53 +080081// The Guard to Shutdown LLVM
82//llvm::llvm_shutdown_obj llvm_guard;
83// TODO: We are commenting this line because this will cause SEGV. This may
84// related to two possible reasons: (1) the order of the destruction of static
85// objects, and (2) dlopen/dlclose side-effect on static objects.
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080086
87} // anonymous namespace
88
89
90namespace art {
91namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080092
93
Logan Chiene75a8cc2012-02-24 12:26:43 +080094llvm::Module* makeLLVMModuleContents(llvm::Module* module);
Logan Chien42e0e152012-01-13 15:42:36 +080095
96
Shih-wei Liaod1fec812012-02-13 09:51:10 -080097CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set)
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -080098 : compiler_(compiler), compiler_lock_("llvm_compiler_lock"),
Logan Chien7f767612012-03-01 18:54:49 +080099 insn_set_(insn_set), curr_cunit_(NULL) {
Shih-wei Liaofc34adb2012-03-07 08:51:44 -0800100
101
102 // Initialize LLVM libraries
103 pthread_once(&llvm_initialized, InitializeLLVM);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800104}
105
106
107CompilerLLVM::~CompilerLLVM() {
Logan Chien7f767612012-03-01 18:54:49 +0800108 STLDeleteElements(&cunits_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800109}
110
111
Logan Chience119062012-03-02 11:57:34 +0800112void CompilerLLVM::EnsureCompilationUnit() {
Logan Chience119062012-03-02 11:57:34 +0800113 compiler_lock_.AssertHeld();
Logan Chien7f767612012-03-01 18:54:49 +0800114
115 if (curr_cunit_ != NULL) {
116 return;
Logan Chien8b977d32012-02-21 19:14:55 +0800117 }
Logan Chien7f767612012-03-01 18:54:49 +0800118
119 // Allocate compilation unit
120 size_t cunit_idx = cunits_.size();
121
Logan Chien6546ec52012-03-17 20:08:29 +0800122 curr_cunit_ = new CompilationUnit(insn_set_, cunit_idx);
Logan Chien7f767612012-03-01 18:54:49 +0800123
124 // Setup output filename
125 curr_cunit_->SetElfFileName(
126 StringPrintf("%s-%zu", elf_filename_.c_str(), cunit_idx));
127
128 if (IsBitcodeFileNameAvailable()) {
129 curr_cunit_->SetBitcodeFileName(
130 StringPrintf("%s-%zu", bitcode_filename_.c_str(), cunit_idx));
131 }
132
133 // Register compilation unit
134 cunits_.push_back(curr_cunit_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800135}
136
137
Logan Chien7f767612012-03-01 18:54:49 +0800138void CompilerLLVM::MaterializeRemainder() {
Logan Chien8b977d32012-02-21 19:14:55 +0800139 MutexLock GUARD(compiler_lock_);
Logan Chien7f767612012-03-01 18:54:49 +0800140 if (curr_cunit_ != NULL) {
Logan Chience119062012-03-02 11:57:34 +0800141 Materialize();
Logan Chien7f767612012-03-01 18:54:49 +0800142 }
143}
Logan Chien8b977d32012-02-21 19:14:55 +0800144
Logan Chien8b977d32012-02-21 19:14:55 +0800145
Logan Chien7f767612012-03-01 18:54:49 +0800146void CompilerLLVM::MaterializeIfThresholdReached() {
147 MutexLock GUARD(compiler_lock_);
148 if (curr_cunit_ != NULL && curr_cunit_->IsMaterializeThresholdReached()) {
Logan Chience119062012-03-02 11:57:34 +0800149 Materialize();
Logan Chien7f767612012-03-01 18:54:49 +0800150 }
151}
152
153
Logan Chience119062012-03-02 11:57:34 +0800154void CompilerLLVM::Materialize() {
155 compiler_lock_.AssertHeld();
156
Logan Chien7f767612012-03-01 18:54:49 +0800157 DCHECK(curr_cunit_ != NULL);
158 DCHECK(!curr_cunit_->IsMaterialized());
159
160 // Write bitcode to file when filename is set
161 if (IsBitcodeFileNameAvailable()) {
162 curr_cunit_->WriteBitcodeToFile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800163 }
164
Logan Chien8b977d32012-02-21 19:14:55 +0800165 // Materialize the llvm::Module into ELF object file
Logan Chien7f767612012-03-01 18:54:49 +0800166 curr_cunit_->Materialize();
Logan Chien8b977d32012-02-21 19:14:55 +0800167
168 // Delete the compilation unit
Logan Chien7f767612012-03-01 18:54:49 +0800169 curr_cunit_ = NULL;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800170}
171
172
Logan Chien7f767612012-03-01 18:54:49 +0800173CompiledMethod* CompilerLLVM::
174CompileDexMethod(OatCompilationUnit* oat_compilation_unit) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800175 MutexLock GUARD(compiler_lock_);
176
Logan Chience119062012-03-02 11:57:34 +0800177 EnsureCompilationUnit();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800178
Logan Chien8b977d32012-02-21 19:14:55 +0800179 UniquePtr<MethodCompiler> method_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800180 new MethodCompiler(curr_cunit_, compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800181
Logan Chien7f767612012-03-01 18:54:49 +0800182 return method_compiler->Compile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800183}
Logan Chien83426162011-12-09 09:29:50 +0800184
185
Logan Chien7f767612012-03-01 18:54:49 +0800186CompiledMethod* CompilerLLVM::
187CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
Logan Chien88894ee2012-02-13 16:42:22 +0800188 MutexLock GUARD(compiler_lock_);
189
Logan Chience119062012-03-02 11:57:34 +0800190 EnsureCompilationUnit();
Logan Chien88894ee2012-02-13 16:42:22 +0800191
Logan Chien8b977d32012-02-21 19:14:55 +0800192 UniquePtr<JniCompiler> jni_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800193 new JniCompiler(curr_cunit_, *compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800194
Logan Chien7f767612012-03-01 18:54:49 +0800195 return jni_compiler->Compile();
Logan Chien88894ee2012-02-13 16:42:22 +0800196}
197
198
Logan Chienf04364f2012-02-10 12:01:39 +0800199CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
200 char const *shorty) {
Logan Chienf04364f2012-02-10 12:01:39 +0800201 MutexLock GUARD(compiler_lock_);
202
Logan Chience119062012-03-02 11:57:34 +0800203 EnsureCompilationUnit();
Logan Chienf04364f2012-02-10 12:01:39 +0800204
Logan Chien8b977d32012-02-21 19:14:55 +0800205 UniquePtr<UpcallCompiler> upcall_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800206 new UpcallCompiler(curr_cunit_, *compiler_));
Logan Chien8b977d32012-02-21 19:14:55 +0800207
Logan Chien7f767612012-03-01 18:54:49 +0800208 return upcall_compiler->CreateStub(is_static, shorty);
Logan Chienf04364f2012-02-10 12:01:39 +0800209}
210
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700211CompilerLLVM* EnsureCompilerLLVM(art::Compiler& compiler) {
212 if (compiler.GetCompilerContext() == NULL) {
213 compiler.SetCompilerContext(new CompilerLLVM(&compiler, compiler.GetInstructionSet()));
214 }
215 CompilerLLVM* compiler_llvm = reinterpret_cast<CompilerLLVM*>(compiler.GetCompilerContext());
216 compiler_llvm->SetElfFileName(compiler.GetElfFileName());
217 compiler_llvm->SetBitcodeFileName(compiler.GetBitcodeFileName());
218 return compiler_llvm;
219}
Logan Chienf04364f2012-02-10 12:01:39 +0800220
Logan Chien83426162011-12-09 09:29:50 +0800221} // namespace compiler_llvm
222} // namespace art
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800223
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700224extern "C" art::CompiledMethod* ArtCompileMethod(art::Compiler& compiler,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800225 const art::DexFile::CodeItem* code_item,
226 uint32_t access_flags, uint32_t method_idx,
227 const art::ClassLoader* class_loader,
228 const art::DexFile& dex_file)
229{
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800230 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
231 art::DexCache *dex_cache = class_linker->FindDexCache(dex_file);
232
233 art::OatCompilationUnit oat_compilation_unit(
234 class_loader, class_linker, dex_file, *dex_cache, code_item,
235 method_idx, access_flags);
236
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700237 return art::compiler_llvm::EnsureCompilerLLVM(compiler)->CompileDexMethod(&oat_compilation_unit);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800238}
239
240extern "C" art::CompiledMethod* ArtJniCompileMethod(art::Compiler& compiler,
241 uint32_t access_flags, uint32_t method_idx,
242 const art::ClassLoader* class_loader,
243 const art::DexFile& dex_file) {
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800244 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
245 art::DexCache *dex_cache = class_linker->FindDexCache(dex_file);
246
247 art::OatCompilationUnit oat_compilation_unit(
248 class_loader, class_linker, dex_file, *dex_cache, NULL,
249 method_idx, access_flags);
250
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700251 art::compiler_llvm::CompilerLLVM* compiler_llvm = art::compiler_llvm::EnsureCompilerLLVM(compiler);
252 art::CompiledMethod* result = compiler_llvm->CompileNativeMethod(&oat_compilation_unit);
253 compiler_llvm->MaterializeIfThresholdReached();
Elliott Hughes13b835a2012-03-13 19:45:22 -0700254 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800255}
256
257extern "C" art::CompiledInvokeStub* ArtCreateInvokeStub(art::Compiler& compiler, bool is_static,
258 const char* shorty, uint32_t shorty_len) {
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700259 return art::compiler_llvm::EnsureCompilerLLVM(compiler)->CreateInvokeStub(is_static, shorty);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800260}
261
262extern "C" void compilerLLVMMaterializeRemainder(art::Compiler& compiler) {
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700263 art::compiler_llvm::EnsureCompilerLLVM(compiler)->MaterializeRemainder();
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800264}
265
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800266// Note: Using this function carefully!!! This is temporary solution, we will remove it.
267extern "C" art::MutexLock* compilerLLVMMutexLock(art::Compiler& compiler) {
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700268 return new art::MutexLock(art::compiler_llvm::EnsureCompilerLLVM(compiler)->compiler_lock_);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800269}
270
271extern "C" void compilerLLVMDispose(art::Compiler& compiler) {
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700272 delete art::compiler_llvm::EnsureCompilerLLVM(compiler);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800273}