blob: baa6b2feeb560419057b88f04b1b6f502e9cd47a [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"
Logan Chienf7015fd2012-03-18 01:19:37 +080021#include "compiled_method.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080022#include "compiler.h"
Shih-wei Liaoc4c98812012-03-10 21:55:51 -080023#include "dex_cache.h"
Logan Chien0f0899a2012-03-23 10:48:18 +080024#include "elf_image.h"
Logan Chienf7015fd2012-03-18 01:19:37 +080025#include "elf_loader.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080026#include "ir_builder.h"
Logan Chien88894ee2012-02-13 16:42:22 +080027#include "jni_compiler.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080028#include "method_compiler.h"
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"
Logan Chienf04364f2012-02-10 12:01:39 +080032#include "upcall_compiler.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080033
Logan Chiendd7cf5b2012-03-01 12:55:19 +080034#include <llvm/LinkAllPasses.h>
35#include <llvm/LinkAllVMCore.h>
Logan Chien4c17dff2012-03-02 20:15:46 +080036#include <llvm/Support/CommandLine.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
Logan Chien013b6f22012-03-02 17:20:33 +080041namespace llvm {
42 extern bool TimePassesIsEnabled;
43}
44
TDYa127d668a062012-04-13 12:36:57 -070045extern llvm::cl::opt<bool> ReserveR9;
46// ReserveR9 is defined in llvm/lib/Target/ARM/ARMSubtarget.cpp
Logan Chien4c17dff2012-03-02 20:15:46 +080047extern llvm::cl::opt<bool> EnableARMLongCalls;
48// NOTE: Although EnableARMLongCalls is defined in llvm/lib/Target/ARM/
49// ARMISelLowering.cpp, however, it is not in the llvm namespace.
50
Logan Chien8b977d32012-02-21 19:14:55 +080051
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080052namespace {
Logan Chien8b977d32012-02-21 19:14:55 +080053
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080054pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT;
55
56void InitializeLLVM() {
Logan Chien013b6f22012-03-02 17:20:33 +080057 // NOTE: Uncomment following line to show the time consumption of LLVM passes
58 //llvm::TimePassesIsEnabled = true;
59
TDYa127d668a062012-04-13 12:36:57 -070060 // Enable -arm-reserve-r9
61 ReserveR9 = true;
62
Logan Chien8b977d32012-02-21 19:14:55 +080063 // Initialize LLVM target, MC subsystem, asm printer, and asm parser
64 llvm::InitializeAllTargets();
65 llvm::InitializeAllTargetMCs();
66 llvm::InitializeAllAsmPrinters();
67 llvm::InitializeAllAsmParsers();
68 // TODO: Maybe we don't have to initialize "all" targets.
69
Logan Chien4c17dff2012-03-02 20:15:46 +080070 // Enable -arm-long-calls
71 EnableARMLongCalls = true;
72
Logan Chiendd7cf5b2012-03-01 12:55:19 +080073 // Initialize LLVM optimization passes
74 llvm::PassRegistry &registry = *llvm::PassRegistry::getPassRegistry();
75
76 llvm::initializeCore(registry);
77 llvm::initializeScalarOpts(registry);
78 llvm::initializeIPO(registry);
79 llvm::initializeAnalysis(registry);
80 llvm::initializeIPA(registry);
81 llvm::initializeTransformUtils(registry);
82 llvm::initializeInstCombine(registry);
83 llvm::initializeInstrumentation(registry);
84 llvm::initializeTarget(registry);
85
Logan Chien8b977d32012-02-21 19:14:55 +080086 // Initialize LLVM internal data structure for multithreading
87 llvm::llvm_start_multithreaded();
88}
89
Logan Chienf1306552012-03-16 11:17:53 +080090// The Guard to Shutdown LLVM
Logan Chienaeb53032012-03-18 02:29:38 +080091// llvm::llvm_shutdown_obj llvm_guard;
92// TODO: We are commenting out this line because this will cause SEGV from
93// time to time.
94// Two reasons: (1) the order of the destruction of static objects, or
95// (2) dlopen/dlclose side-effect on static objects.
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080096
97} // anonymous namespace
98
99
100namespace art {
101namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800102
103
Logan Chiene75a8cc2012-02-24 12:26:43 +0800104llvm::Module* makeLLVMModuleContents(llvm::Module* module);
Logan Chien42e0e152012-01-13 15:42:36 +0800105
106
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800107CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set)
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -0800108 : compiler_(compiler), compiler_lock_("llvm_compiler_lock"),
Logan Chien7f767612012-03-01 18:54:49 +0800109 insn_set_(insn_set), curr_cunit_(NULL) {
Shih-wei Liaofc34adb2012-03-07 08:51:44 -0800110
111
112 // Initialize LLVM libraries
113 pthread_once(&llvm_initialized, InitializeLLVM);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800114}
115
116
117CompilerLLVM::~CompilerLLVM() {
Logan Chien7f767612012-03-01 18:54:49 +0800118 STLDeleteElements(&cunits_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800119}
120
121
Logan Chience119062012-03-02 11:57:34 +0800122void CompilerLLVM::EnsureCompilationUnit() {
Logan Chience119062012-03-02 11:57:34 +0800123 compiler_lock_.AssertHeld();
Logan Chien7f767612012-03-01 18:54:49 +0800124
125 if (curr_cunit_ != NULL) {
126 return;
Logan Chien8b977d32012-02-21 19:14:55 +0800127 }
Logan Chien7f767612012-03-01 18:54:49 +0800128
129 // Allocate compilation unit
130 size_t cunit_idx = cunits_.size();
131
Logan Chien6546ec52012-03-17 20:08:29 +0800132 curr_cunit_ = new CompilationUnit(insn_set_, cunit_idx);
Logan Chien7f767612012-03-01 18:54:49 +0800133
Logan Chiende08e842012-03-21 00:34:12 +0800134 // Setup bitcode output filename
Logan Chien7f767612012-03-01 18:54:49 +0800135 if (IsBitcodeFileNameAvailable()) {
136 curr_cunit_->SetBitcodeFileName(
137 StringPrintf("%s-%zu", bitcode_filename_.c_str(), cunit_idx));
138 }
139
140 // Register compilation unit
141 cunits_.push_back(curr_cunit_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800142}
143
144
Logan Chien7f767612012-03-01 18:54:49 +0800145void CompilerLLVM::MaterializeRemainder() {
Logan Chien8b977d32012-02-21 19:14:55 +0800146 MutexLock GUARD(compiler_lock_);
Logan Chien7f767612012-03-01 18:54:49 +0800147 if (curr_cunit_ != NULL) {
Logan Chience119062012-03-02 11:57:34 +0800148 Materialize();
Logan Chien7f767612012-03-01 18:54:49 +0800149 }
150}
Logan Chien8b977d32012-02-21 19:14:55 +0800151
Logan Chien8b977d32012-02-21 19:14:55 +0800152
Logan Chien7f767612012-03-01 18:54:49 +0800153void CompilerLLVM::MaterializeIfThresholdReached() {
154 MutexLock GUARD(compiler_lock_);
155 if (curr_cunit_ != NULL && curr_cunit_->IsMaterializeThresholdReached()) {
Logan Chience119062012-03-02 11:57:34 +0800156 Materialize();
Logan Chien7f767612012-03-01 18:54:49 +0800157 }
158}
159
160
Logan Chience119062012-03-02 11:57:34 +0800161void CompilerLLVM::Materialize() {
162 compiler_lock_.AssertHeld();
163
Logan Chien7f767612012-03-01 18:54:49 +0800164 DCHECK(curr_cunit_ != NULL);
165 DCHECK(!curr_cunit_->IsMaterialized());
166
167 // Write bitcode to file when filename is set
168 if (IsBitcodeFileNameAvailable()) {
169 curr_cunit_->WriteBitcodeToFile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800170 }
171
Logan Chien8b977d32012-02-21 19:14:55 +0800172 // Materialize the llvm::Module into ELF object file
Logan Chien7f767612012-03-01 18:54:49 +0800173 curr_cunit_->Materialize();
Logan Chien8b977d32012-02-21 19:14:55 +0800174
Logan Chienf7015fd2012-03-18 01:19:37 +0800175 // Load ELF image when automatic ELF loading is enabled
176 if (IsAutoElfLoadingEnabled()) {
177 LoadElfFromCompilationUnit(curr_cunit_);
178 }
179
Logan Chien8b977d32012-02-21 19:14:55 +0800180 // Delete the compilation unit
Logan Chien7f767612012-03-01 18:54:49 +0800181 curr_cunit_ = NULL;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800182}
183
184
Logan Chienf7015fd2012-03-18 01:19:37 +0800185void CompilerLLVM::EnableAutoElfLoading() {
186 MutexLock GUARD(compiler_lock_);
187
188 if (IsAutoElfLoadingEnabled()) {
189 // If there is an existing ELF loader, then do nothing.
190 // Because the existing ELF loader may have returned some code address
191 // already. If we replace the existing ELF loader with
192 // elf_loader_.reset(...), then it is possible to have some dangling
193 // pointer.
194 return;
195 }
196
197 // Create ELF loader and load the materialized CompilationUnit
198 elf_loader_.reset(new ElfLoader());
199
200 for (size_t i = 0; i < cunits_.size(); ++i) {
201 if (cunits_[i]->IsMaterialized()) {
202 LoadElfFromCompilationUnit(cunits_[i]);
203 }
204 }
205}
206
207
208void CompilerLLVM::LoadElfFromCompilationUnit(const CompilationUnit* cunit) {
209 compiler_lock_.AssertHeld();
210 DCHECK(cunit->IsMaterialized()) << cunit->GetElfIndex();
211
Logan Chien0c717dd2012-03-28 18:31:07 +0800212 if (!elf_loader_->LoadElfAt(cunit->GetElfIndex(),
213 cunit->GetElfImage(),
214 OatFile::kRelocAll)) {
Logan Chienf7015fd2012-03-18 01:19:37 +0800215 LOG(ERROR) << "Failed to load ELF from compilation unit "
216 << cunit->GetElfIndex();
217 }
218}
219
220
Logan Chien937105a2012-04-02 02:37:37 +0800221const void* CompilerLLVM::GetMethodCodeAddr(const CompiledMethod* cm) const {
222 return elf_loader_->GetMethodCodeAddr(cm->GetElfIndex(),
223 cm->GetElfFuncIndex());
Logan Chienf7015fd2012-03-18 01:19:37 +0800224}
225
226
227const Method::InvokeStub* CompilerLLVM::
Logan Chien937105a2012-04-02 02:37:37 +0800228GetMethodInvokeStubAddr(const CompiledInvokeStub* cm) const {
229 return elf_loader_->GetMethodInvokeStubAddr(cm->GetElfIndex(),
230 cm->GetElfFuncIndex());
Logan Chienf7015fd2012-03-18 01:19:37 +0800231}
232
233
Logan Chiendf576142012-03-20 17:36:32 +0800234std::vector<ElfImage> CompilerLLVM::GetElfImages() const {
235 std::vector<ElfImage> result;
236
237 for (size_t i = 0; i < cunits_.size(); ++i) {
238 result.push_back(cunits_[i]->GetElfImage());
239 }
240
241 return result;
242}
243
244
Logan Chien7f767612012-03-01 18:54:49 +0800245CompiledMethod* CompilerLLVM::
246CompileDexMethod(OatCompilationUnit* oat_compilation_unit) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800247 MutexLock GUARD(compiler_lock_);
248
Logan Chience119062012-03-02 11:57:34 +0800249 EnsureCompilationUnit();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800250
Logan Chien8b977d32012-02-21 19:14:55 +0800251 UniquePtr<MethodCompiler> method_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800252 new MethodCompiler(curr_cunit_, compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800253
Logan Chien7f767612012-03-01 18:54:49 +0800254 return method_compiler->Compile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800255}
Logan Chien83426162011-12-09 09:29:50 +0800256
257
Logan Chien7f767612012-03-01 18:54:49 +0800258CompiledMethod* CompilerLLVM::
259CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
Logan Chien88894ee2012-02-13 16:42:22 +0800260 MutexLock GUARD(compiler_lock_);
261
Logan Chience119062012-03-02 11:57:34 +0800262 EnsureCompilationUnit();
Logan Chien88894ee2012-02-13 16:42:22 +0800263
Logan Chien8b977d32012-02-21 19:14:55 +0800264 UniquePtr<JniCompiler> jni_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800265 new JniCompiler(curr_cunit_, *compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800266
Logan Chien7f767612012-03-01 18:54:49 +0800267 return jni_compiler->Compile();
Logan Chien88894ee2012-02-13 16:42:22 +0800268}
269
270
Logan Chienf04364f2012-02-10 12:01:39 +0800271CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
272 char const *shorty) {
Logan Chienf04364f2012-02-10 12:01:39 +0800273 MutexLock GUARD(compiler_lock_);
274
Logan Chience119062012-03-02 11:57:34 +0800275 EnsureCompilationUnit();
Logan Chienf04364f2012-02-10 12:01:39 +0800276
Logan Chien8b977d32012-02-21 19:14:55 +0800277 UniquePtr<UpcallCompiler> upcall_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800278 new UpcallCompiler(curr_cunit_, *compiler_));
Logan Chien8b977d32012-02-21 19:14:55 +0800279
Logan Chien7f767612012-03-01 18:54:49 +0800280 return upcall_compiler->CreateStub(is_static, shorty);
Logan Chienf04364f2012-02-10 12:01:39 +0800281}
282
Logan Chien83426162011-12-09 09:29:50 +0800283} // namespace compiler_llvm
284} // namespace art
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800285
Logan Chien106b2a02012-03-18 04:41:38 +0800286inline static art::compiler_llvm::CompilerLLVM* ContextOf(art::Compiler& compiler) {
287 void *compiler_context = compiler.GetCompilerContext();
288 CHECK(compiler_context != NULL);
289 return reinterpret_cast<art::compiler_llvm::CompilerLLVM*>(compiler_context);
290}
291
292inline static const art::compiler_llvm::CompilerLLVM* ContextOf(const art::Compiler& compiler) {
293 void *compiler_context = compiler.GetCompilerContext();
294 CHECK(compiler_context != NULL);
295 return reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler_context);
296}
297
298extern "C" void ArtInitCompilerContext(art::Compiler& compiler) {
299 CHECK(compiler.GetCompilerContext() == NULL);
300
301 art::compiler_llvm::CompilerLLVM* compiler_llvm =
302 new art::compiler_llvm::CompilerLLVM(&compiler,
303 compiler.GetInstructionSet());
304
305 compiler.SetCompilerContext(compiler_llvm);
306}
307
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700308extern "C" art::CompiledMethod* ArtCompileMethod(art::Compiler& compiler,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800309 const art::DexFile::CodeItem* code_item,
310 uint32_t access_flags, uint32_t method_idx,
311 const art::ClassLoader* class_loader,
312 const art::DexFile& dex_file)
313{
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800314 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
315 art::DexCache *dex_cache = class_linker->FindDexCache(dex_file);
316
317 art::OatCompilationUnit oat_compilation_unit(
318 class_loader, class_linker, dex_file, *dex_cache, code_item,
319 method_idx, access_flags);
320
Logan Chien106b2a02012-03-18 04:41:38 +0800321 return ContextOf(compiler)->CompileDexMethod(&oat_compilation_unit);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800322}
323
324extern "C" art::CompiledMethod* ArtJniCompileMethod(art::Compiler& compiler,
325 uint32_t access_flags, uint32_t method_idx,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800326 const art::DexFile& dex_file) {
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800327 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
328 art::DexCache *dex_cache = class_linker->FindDexCache(dex_file);
329
330 art::OatCompilationUnit oat_compilation_unit(
Shih-wei Liaob1ab7df2012-03-29 13:53:46 -0700331 NULL, class_linker, dex_file, *dex_cache, NULL,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800332 method_idx, access_flags);
333
Logan Chien106b2a02012-03-18 04:41:38 +0800334 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700335 art::CompiledMethod* result = compiler_llvm->CompileNativeMethod(&oat_compilation_unit);
336 compiler_llvm->MaterializeIfThresholdReached();
Elliott Hughes13b835a2012-03-13 19:45:22 -0700337 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800338}
339
340extern "C" art::CompiledInvokeStub* ArtCreateInvokeStub(art::Compiler& compiler, bool is_static,
341 const char* shorty, uint32_t shorty_len) {
Logan Chien106b2a02012-03-18 04:41:38 +0800342 return ContextOf(compiler)->CreateInvokeStub(is_static, shorty);
343}
344
Logan Chien106b2a02012-03-18 04:41:38 +0800345extern "C" void compilerLLVMSetBitcodeFileName(art::Compiler& compiler,
346 std::string const& filename) {
347 ContextOf(compiler)->SetBitcodeFileName(filename);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800348}
349
350extern "C" void compilerLLVMMaterializeRemainder(art::Compiler& compiler) {
Logan Chien106b2a02012-03-18 04:41:38 +0800351 ContextOf(compiler)->MaterializeRemainder();
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800352}
353
Logan Chienf7015fd2012-03-18 01:19:37 +0800354extern "C" void compilerLLVMEnableAutoElfLoading(art::Compiler& compiler) {
355 art::compiler_llvm::CompilerLLVM* compiler_llvm =
356 reinterpret_cast<art::compiler_llvm::CompilerLLVM*>(compiler.GetCompilerContext());
357 return compiler_llvm->EnableAutoElfLoading();
358}
359
360extern "C" const void* compilerLLVMGetMethodCodeAddr(const art::Compiler& compiler,
361 const art::CompiledMethod* cm,
Logan Chien937105a2012-04-02 02:37:37 +0800362 const art::Method*) {
Logan Chienf7015fd2012-03-18 01:19:37 +0800363 const art::compiler_llvm::CompilerLLVM* compiler_llvm =
364 reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler.GetCompilerContext());
Logan Chien937105a2012-04-02 02:37:37 +0800365 return compiler_llvm->GetMethodCodeAddr(cm);
Logan Chienf7015fd2012-03-18 01:19:37 +0800366}
367
368extern "C" const art::Method::InvokeStub* compilerLLVMGetMethodInvokeStubAddr(const art::Compiler& compiler,
369 const art::CompiledInvokeStub* cm,
Logan Chien937105a2012-04-02 02:37:37 +0800370 const art::Method*) {
Logan Chienf7015fd2012-03-18 01:19:37 +0800371 const art::compiler_llvm::CompilerLLVM* compiler_llvm =
372 reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler.GetCompilerContext());
Logan Chien937105a2012-04-02 02:37:37 +0800373 return compiler_llvm->GetMethodInvokeStubAddr(cm);
Logan Chienf7015fd2012-03-18 01:19:37 +0800374}
375
Logan Chiendf576142012-03-20 17:36:32 +0800376extern "C" std::vector<art::ElfImage> compilerLLVMGetElfImages(const art::Compiler& compiler) {
377 return ContextOf(compiler)->GetElfImages();
378}
379
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800380// Note: Using this function carefully!!! This is temporary solution, we will remove it.
381extern "C" art::MutexLock* compilerLLVMMutexLock(art::Compiler& compiler) {
Logan Chien106b2a02012-03-18 04:41:38 +0800382 return new art::MutexLock(ContextOf(compiler)->compiler_lock_);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800383}
384
385extern "C" void compilerLLVMDispose(art::Compiler& compiler) {
Logan Chien106b2a02012-03-18 04:41:38 +0800386 delete ContextOf(compiler);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800387}