blob: 5a3725e1843cbdd38fdac851f4269ea11b18c7b7 [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
Shih-wei Liao63deaad2012-03-06 17:26:37 -080081// Singleton. Otherwise, multiple CompilerLLVM instances may cause crashes if
82// one shuts down prematurely.
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080083llvm::llvm_shutdown_obj llvm_guard;
84
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)
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -080096 : compiler_(compiler), compiler_lock_("llvm_compiler_lock"),
Logan Chien7f767612012-03-01 18:54:49 +080097 insn_set_(insn_set), curr_cunit_(NULL) {
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080098
99
100 // Initialize LLVM libraries
101 pthread_once(&llvm_initialized, InitializeLLVM);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800102}
103
104
105CompilerLLVM::~CompilerLLVM() {
Logan Chien7f767612012-03-01 18:54:49 +0800106 STLDeleteElements(&cunits_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800107}
108
109
Logan Chience119062012-03-02 11:57:34 +0800110void CompilerLLVM::EnsureCompilationUnit() {
Logan Chience119062012-03-02 11:57:34 +0800111 compiler_lock_.AssertHeld();
Logan Chien7f767612012-03-01 18:54:49 +0800112
113 if (curr_cunit_ != NULL) {
114 return;
Logan Chien8b977d32012-02-21 19:14:55 +0800115 }
Logan Chien7f767612012-03-01 18:54:49 +0800116
117 // Allocate compilation unit
118 size_t cunit_idx = cunits_.size();
119
120 curr_cunit_ = new CompilationUnit(insn_set_);
121
122 // Setup output filename
123 curr_cunit_->SetElfFileName(
124 StringPrintf("%s-%zu", elf_filename_.c_str(), cunit_idx));
125
126 if (IsBitcodeFileNameAvailable()) {
127 curr_cunit_->SetBitcodeFileName(
128 StringPrintf("%s-%zu", bitcode_filename_.c_str(), cunit_idx));
129 }
130
131 // Register compilation unit
132 cunits_.push_back(curr_cunit_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800133}
134
135
Logan Chien7f767612012-03-01 18:54:49 +0800136void CompilerLLVM::MaterializeRemainder() {
Logan Chien8b977d32012-02-21 19:14:55 +0800137 MutexLock GUARD(compiler_lock_);
Logan Chien7f767612012-03-01 18:54:49 +0800138 if (curr_cunit_ != NULL) {
Logan Chience119062012-03-02 11:57:34 +0800139 Materialize();
Logan Chien7f767612012-03-01 18:54:49 +0800140 }
141}
Logan Chien8b977d32012-02-21 19:14:55 +0800142
Logan Chien8b977d32012-02-21 19:14:55 +0800143
Logan Chien7f767612012-03-01 18:54:49 +0800144void CompilerLLVM::MaterializeIfThresholdReached() {
145 MutexLock GUARD(compiler_lock_);
146 if (curr_cunit_ != NULL && curr_cunit_->IsMaterializeThresholdReached()) {
Logan Chience119062012-03-02 11:57:34 +0800147 Materialize();
Logan Chien7f767612012-03-01 18:54:49 +0800148 }
149}
150
151
Logan Chience119062012-03-02 11:57:34 +0800152void CompilerLLVM::Materialize() {
153 compiler_lock_.AssertHeld();
154
Logan Chien7f767612012-03-01 18:54:49 +0800155 DCHECK(curr_cunit_ != NULL);
156 DCHECK(!curr_cunit_->IsMaterialized());
157
158 // Write bitcode to file when filename is set
159 if (IsBitcodeFileNameAvailable()) {
160 curr_cunit_->WriteBitcodeToFile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800161 }
162
Logan Chien8b977d32012-02-21 19:14:55 +0800163 // Materialize the llvm::Module into ELF object file
Logan Chien7f767612012-03-01 18:54:49 +0800164 curr_cunit_->Materialize();
Logan Chien8b977d32012-02-21 19:14:55 +0800165
166 // Delete the compilation unit
Logan Chien7f767612012-03-01 18:54:49 +0800167 curr_cunit_ = NULL;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800168}
169
170
Logan Chien7f767612012-03-01 18:54:49 +0800171CompiledMethod* CompilerLLVM::
172CompileDexMethod(OatCompilationUnit* oat_compilation_unit) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800173 MutexLock GUARD(compiler_lock_);
174
Logan Chience119062012-03-02 11:57:34 +0800175 EnsureCompilationUnit();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800176
Logan Chien8b977d32012-02-21 19:14:55 +0800177 UniquePtr<MethodCompiler> method_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800178 new MethodCompiler(curr_cunit_, compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800179
Logan Chien7f767612012-03-01 18:54:49 +0800180 return method_compiler->Compile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800181}
Logan Chien83426162011-12-09 09:29:50 +0800182
183
Logan Chien7f767612012-03-01 18:54:49 +0800184CompiledMethod* CompilerLLVM::
185CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
Logan Chien88894ee2012-02-13 16:42:22 +0800186 MutexLock GUARD(compiler_lock_);
187
Logan Chience119062012-03-02 11:57:34 +0800188 EnsureCompilationUnit();
Logan Chien88894ee2012-02-13 16:42:22 +0800189
Logan Chien8b977d32012-02-21 19:14:55 +0800190 UniquePtr<JniCompiler> jni_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800191 new JniCompiler(curr_cunit_, *compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800192
Logan Chien7f767612012-03-01 18:54:49 +0800193 return jni_compiler->Compile();
Logan Chien88894ee2012-02-13 16:42:22 +0800194}
195
196
Logan Chienf04364f2012-02-10 12:01:39 +0800197CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
198 char const *shorty) {
Logan Chienf04364f2012-02-10 12:01:39 +0800199 MutexLock GUARD(compiler_lock_);
200
Logan Chience119062012-03-02 11:57:34 +0800201 EnsureCompilationUnit();
Logan Chienf04364f2012-02-10 12:01:39 +0800202
Logan Chien8b977d32012-02-21 19:14:55 +0800203 UniquePtr<UpcallCompiler> upcall_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800204 new UpcallCompiler(curr_cunit_, *compiler_));
Logan Chien8b977d32012-02-21 19:14:55 +0800205
Logan Chien7f767612012-03-01 18:54:49 +0800206 return upcall_compiler->CreateStub(is_static, shorty);
Logan Chienf04364f2012-02-10 12:01:39 +0800207}
208
209
Logan Chien83426162011-12-09 09:29:50 +0800210} // namespace compiler_llvm
211} // namespace art
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800212
213namespace {
214
215void ensureCompilerLLVM(art::Compiler& compiler) {
216 if (compiler.GetCompilerLLVM() == NULL) {
217 compiler.SetCompilerLLVM(new art::compiler_llvm::CompilerLLVM(&compiler,
218 compiler.GetInstructionSet()));
219 }
220 art::compiler_llvm::CompilerLLVM* compiler_llvm = compiler.GetCompilerLLVM();
221 compiler_llvm->SetElfFileName(compiler.GetElfFileName());
222 compiler_llvm->SetBitcodeFileName(compiler.GetBitcodeFileName());
223}
224
225} // anonymous namespace
226
227extern "C" art::CompiledMethod* oatCompileMethod(art::Compiler& compiler,
228 const art::DexFile::CodeItem* code_item,
229 uint32_t access_flags, uint32_t method_idx,
230 const art::ClassLoader* class_loader,
231 const art::DexFile& dex_file)
232{
233 ensureCompilerLLVM(compiler);
234
235 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
236 art::DexCache *dex_cache = class_linker->FindDexCache(dex_file);
237
238 art::OatCompilationUnit oat_compilation_unit(
239 class_loader, class_linker, dex_file, *dex_cache, code_item,
240 method_idx, access_flags);
241
242 return compiler.GetCompilerLLVM()->CompileDexMethod(&oat_compilation_unit);
243}
244
245extern "C" art::CompiledMethod* ArtJniCompileMethod(art::Compiler& compiler,
246 uint32_t access_flags, uint32_t method_idx,
247 const art::ClassLoader* class_loader,
248 const art::DexFile& dex_file) {
249 ensureCompilerLLVM(compiler);
250
251 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
252 art::DexCache *dex_cache = class_linker->FindDexCache(dex_file);
253
254 art::OatCompilationUnit oat_compilation_unit(
255 class_loader, class_linker, dex_file, *dex_cache, NULL,
256 method_idx, access_flags);
257
258 return compiler.GetCompilerLLVM()->CompileNativeMethod(&oat_compilation_unit);
259}
260
261extern "C" art::CompiledInvokeStub* ArtCreateInvokeStub(art::Compiler& compiler, bool is_static,
262 const char* shorty, uint32_t shorty_len) {
263 ensureCompilerLLVM(compiler);
264 //shorty_len = 0; // To make the compiler happy
265 return compiler.GetCompilerLLVM()->CreateInvokeStub(is_static, shorty);
266}
267
268extern "C" void compilerLLVMMaterializeRemainder(art::Compiler& compiler) {
269 ensureCompilerLLVM(compiler);
270 compiler.GetCompilerLLVM()->MaterializeRemainder();
271}
272
273extern "C" void compilerLLVMMaterializeIfThresholdReached(art::Compiler& compiler) {
274 ensureCompilerLLVM(compiler);
275 compiler.GetCompilerLLVM()->MaterializeIfThresholdReached();
276}
277
278// Note: Using this function carefully!!! This is temporary solution, we will remove it.
279extern "C" art::MutexLock* compilerLLVMMutexLock(art::Compiler& compiler) {
280 ensureCompilerLLVM(compiler);
281 return new art::MutexLock(compiler.GetCompilerLLVM()->compiler_lock_);
282}
283
284extern "C" void compilerLLVMDispose(art::Compiler& compiler) {
285 delete compiler.GetCompilerLLVM();
286}