blob: 28320c74c637b0748a8c978098451eb22e4de4f8 [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
Logan Chien4c17dff2012-03-02 20:15:46 +080045// NOTE: Although EnableARMLongCalls is defined in llvm/lib/Target/ARM/
46// ARMISelLowering.cpp, however, it is not in the llvm namespace.
Shih-wei Liao17765722012-04-17 16:42:19 -070047extern llvm::cl::opt<bool> EnableARMLongCalls;
48
49// ReserveR9 is defined in llvm/lib/Target/ARM/ARMSubtarget.cpp
50extern llvm::cl::opt<bool> ReserveR9;
Logan Chien4c17dff2012-03-02 20:15:46 +080051
Logan Chien8b977d32012-02-21 19:14:55 +080052
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080053namespace {
Logan Chien8b977d32012-02-21 19:14:55 +080054
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080055pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT;
56
57void InitializeLLVM() {
Logan Chien013b6f22012-03-02 17:20:33 +080058 // NOTE: Uncomment following line to show the time consumption of LLVM passes
59 //llvm::TimePassesIsEnabled = true;
60
TDYa127d668a062012-04-13 12:36:57 -070061 // Enable -arm-reserve-r9
62 ReserveR9 = true;
63
Logan Chien8b977d32012-02-21 19:14:55 +080064 // Initialize LLVM target, MC subsystem, asm printer, and asm parser
65 llvm::InitializeAllTargets();
66 llvm::InitializeAllTargetMCs();
67 llvm::InitializeAllAsmPrinters();
68 llvm::InitializeAllAsmParsers();
69 // TODO: Maybe we don't have to initialize "all" targets.
70
Logan Chien4c17dff2012-03-02 20:15:46 +080071 // Enable -arm-long-calls
Shih-wei Liao17765722012-04-17 16:42:19 -070072 EnableARMLongCalls = false;
Logan Chien4c17dff2012-03-02 20:15:46 +080073
Logan Chiendd7cf5b2012-03-01 12:55:19 +080074 // Initialize LLVM optimization passes
75 llvm::PassRegistry &registry = *llvm::PassRegistry::getPassRegistry();
76
77 llvm::initializeCore(registry);
78 llvm::initializeScalarOpts(registry);
79 llvm::initializeIPO(registry);
80 llvm::initializeAnalysis(registry);
81 llvm::initializeIPA(registry);
82 llvm::initializeTransformUtils(registry);
83 llvm::initializeInstCombine(registry);
84 llvm::initializeInstrumentation(registry);
85 llvm::initializeTarget(registry);
86
Logan Chien8b977d32012-02-21 19:14:55 +080087 // Initialize LLVM internal data structure for multithreading
88 llvm::llvm_start_multithreaded();
89}
90
Logan Chienf1306552012-03-16 11:17:53 +080091// The Guard to Shutdown LLVM
Logan Chienaeb53032012-03-18 02:29:38 +080092// llvm::llvm_shutdown_obj llvm_guard;
93// TODO: We are commenting out this line because this will cause SEGV from
94// time to time.
95// Two reasons: (1) the order of the destruction of static objects, or
96// (2) dlopen/dlclose side-effect on static objects.
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080097
98} // anonymous namespace
99
100
101namespace art {
102namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800103
104
Logan Chiene75a8cc2012-02-24 12:26:43 +0800105llvm::Module* makeLLVMModuleContents(llvm::Module* module);
Logan Chien42e0e152012-01-13 15:42:36 +0800106
107
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800108CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set)
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -0800109 : compiler_(compiler), compiler_lock_("llvm_compiler_lock"),
Logan Chien7f767612012-03-01 18:54:49 +0800110 insn_set_(insn_set), curr_cunit_(NULL) {
Shih-wei Liaofc34adb2012-03-07 08:51:44 -0800111
112
113 // Initialize LLVM libraries
114 pthread_once(&llvm_initialized, InitializeLLVM);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800115}
116
117
118CompilerLLVM::~CompilerLLVM() {
Logan Chien7f767612012-03-01 18:54:49 +0800119 STLDeleteElements(&cunits_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800120}
121
122
Logan Chience119062012-03-02 11:57:34 +0800123void CompilerLLVM::EnsureCompilationUnit() {
Logan Chience119062012-03-02 11:57:34 +0800124 compiler_lock_.AssertHeld();
Logan Chien7f767612012-03-01 18:54:49 +0800125
126 if (curr_cunit_ != NULL) {
127 return;
Logan Chien8b977d32012-02-21 19:14:55 +0800128 }
Logan Chien7f767612012-03-01 18:54:49 +0800129
130 // Allocate compilation unit
131 size_t cunit_idx = cunits_.size();
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 Chien7f767612012-03-01 18:54:49 +0800134 // Register compilation unit
135 cunits_.push_back(curr_cunit_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800136}
137
138
Logan Chien7f767612012-03-01 18:54:49 +0800139void CompilerLLVM::MaterializeRemainder() {
Logan Chien8b977d32012-02-21 19:14:55 +0800140 MutexLock GUARD(compiler_lock_);
Logan Chien7f767612012-03-01 18:54:49 +0800141 if (curr_cunit_ != NULL) {
Logan Chience119062012-03-02 11:57:34 +0800142 Materialize();
Logan Chien7f767612012-03-01 18:54:49 +0800143 }
144}
Logan Chien8b977d32012-02-21 19:14:55 +0800145
Logan Chien8b977d32012-02-21 19:14:55 +0800146
Logan Chien7f767612012-03-01 18:54:49 +0800147void CompilerLLVM::MaterializeIfThresholdReached() {
148 MutexLock GUARD(compiler_lock_);
149 if (curr_cunit_ != NULL && curr_cunit_->IsMaterializeThresholdReached()) {
Logan Chience119062012-03-02 11:57:34 +0800150 Materialize();
Logan Chien7f767612012-03-01 18:54:49 +0800151 }
152}
153
154
Logan Chience119062012-03-02 11:57:34 +0800155void CompilerLLVM::Materialize() {
156 compiler_lock_.AssertHeld();
157
Logan Chien7f767612012-03-01 18:54:49 +0800158 DCHECK(curr_cunit_ != NULL);
159 DCHECK(!curr_cunit_->IsMaterialized());
160
161 // Write bitcode to file when filename is set
162 if (IsBitcodeFileNameAvailable()) {
Shih-wei Liaodbd00342012-04-20 14:27:29 -0700163 const size_t cunit_idx = cunits_.size();
164 curr_cunit_->WriteBitcodeToFile(
165 StringPrintf("%s-%zu", bitcode_filename_.c_str(), cunit_idx));
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800166 }
167
Logan Chien8b977d32012-02-21 19:14:55 +0800168 // Materialize the llvm::Module into ELF object file
Logan Chien7f767612012-03-01 18:54:49 +0800169 curr_cunit_->Materialize();
Logan Chien8b977d32012-02-21 19:14:55 +0800170
Logan Chienf7015fd2012-03-18 01:19:37 +0800171 // Load ELF image when automatic ELF loading is enabled
172 if (IsAutoElfLoadingEnabled()) {
173 LoadElfFromCompilationUnit(curr_cunit_);
174 }
175
Logan Chien8b977d32012-02-21 19:14:55 +0800176 // Delete the compilation unit
Logan Chien7f767612012-03-01 18:54:49 +0800177 curr_cunit_ = NULL;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800178}
179
180
Logan Chienf7015fd2012-03-18 01:19:37 +0800181void CompilerLLVM::EnableAutoElfLoading() {
182 MutexLock GUARD(compiler_lock_);
183
184 if (IsAutoElfLoadingEnabled()) {
185 // If there is an existing ELF loader, then do nothing.
186 // Because the existing ELF loader may have returned some code address
187 // already. If we replace the existing ELF loader with
188 // elf_loader_.reset(...), then it is possible to have some dangling
189 // pointer.
190 return;
191 }
192
193 // Create ELF loader and load the materialized CompilationUnit
194 elf_loader_.reset(new ElfLoader());
195
196 for (size_t i = 0; i < cunits_.size(); ++i) {
197 if (cunits_[i]->IsMaterialized()) {
198 LoadElfFromCompilationUnit(cunits_[i]);
199 }
200 }
201}
202
203
204void CompilerLLVM::LoadElfFromCompilationUnit(const CompilationUnit* cunit) {
205 compiler_lock_.AssertHeld();
206 DCHECK(cunit->IsMaterialized()) << cunit->GetElfIndex();
207
Logan Chien0c717dd2012-03-28 18:31:07 +0800208 if (!elf_loader_->LoadElfAt(cunit->GetElfIndex(),
209 cunit->GetElfImage(),
210 OatFile::kRelocAll)) {
Logan Chienf7015fd2012-03-18 01:19:37 +0800211 LOG(ERROR) << "Failed to load ELF from compilation unit "
212 << cunit->GetElfIndex();
213 }
214}
215
216
Logan Chien937105a2012-04-02 02:37:37 +0800217const void* CompilerLLVM::GetMethodCodeAddr(const CompiledMethod* cm) const {
218 return elf_loader_->GetMethodCodeAddr(cm->GetElfIndex(),
219 cm->GetElfFuncIndex());
Logan Chienf7015fd2012-03-18 01:19:37 +0800220}
221
222
223const Method::InvokeStub* CompilerLLVM::
Logan Chien937105a2012-04-02 02:37:37 +0800224GetMethodInvokeStubAddr(const CompiledInvokeStub* cm) const {
225 return elf_loader_->GetMethodInvokeStubAddr(cm->GetElfIndex(),
226 cm->GetElfFuncIndex());
Logan Chienf7015fd2012-03-18 01:19:37 +0800227}
228
229
Logan Chiendf576142012-03-20 17:36:32 +0800230std::vector<ElfImage> CompilerLLVM::GetElfImages() const {
231 std::vector<ElfImage> result;
232
233 for (size_t i = 0; i < cunits_.size(); ++i) {
234 result.push_back(cunits_[i]->GetElfImage());
235 }
236
237 return result;
238}
239
240
Logan Chien7f767612012-03-01 18:54:49 +0800241CompiledMethod* CompilerLLVM::
242CompileDexMethod(OatCompilationUnit* oat_compilation_unit) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800243 MutexLock GUARD(compiler_lock_);
244
Logan Chience119062012-03-02 11:57:34 +0800245 EnsureCompilationUnit();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800246
Logan Chien8b977d32012-02-21 19:14:55 +0800247 UniquePtr<MethodCompiler> method_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800248 new MethodCompiler(curr_cunit_, compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800249
Logan Chien7f767612012-03-01 18:54:49 +0800250 return method_compiler->Compile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800251}
Logan Chien83426162011-12-09 09:29:50 +0800252
253
Logan Chien7f767612012-03-01 18:54:49 +0800254CompiledMethod* CompilerLLVM::
255CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
Logan Chien88894ee2012-02-13 16:42:22 +0800256 MutexLock GUARD(compiler_lock_);
257
Logan Chience119062012-03-02 11:57:34 +0800258 EnsureCompilationUnit();
Logan Chien88894ee2012-02-13 16:42:22 +0800259
Logan Chien8b977d32012-02-21 19:14:55 +0800260 UniquePtr<JniCompiler> jni_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800261 new JniCompiler(curr_cunit_, *compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800262
Logan Chien7f767612012-03-01 18:54:49 +0800263 return jni_compiler->Compile();
Logan Chien88894ee2012-02-13 16:42:22 +0800264}
265
266
Logan Chienf04364f2012-02-10 12:01:39 +0800267CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
268 char const *shorty) {
Logan Chienf04364f2012-02-10 12:01:39 +0800269 MutexLock GUARD(compiler_lock_);
270
Logan Chience119062012-03-02 11:57:34 +0800271 EnsureCompilationUnit();
Logan Chienf04364f2012-02-10 12:01:39 +0800272
Logan Chien8b977d32012-02-21 19:14:55 +0800273 UniquePtr<UpcallCompiler> upcall_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800274 new UpcallCompiler(curr_cunit_, *compiler_));
Logan Chien8b977d32012-02-21 19:14:55 +0800275
Logan Chien7f767612012-03-01 18:54:49 +0800276 return upcall_compiler->CreateStub(is_static, shorty);
Logan Chienf04364f2012-02-10 12:01:39 +0800277}
278
Logan Chien83426162011-12-09 09:29:50 +0800279} // namespace compiler_llvm
280} // namespace art
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800281
Logan Chien106b2a02012-03-18 04:41:38 +0800282inline static art::compiler_llvm::CompilerLLVM* ContextOf(art::Compiler& compiler) {
283 void *compiler_context = compiler.GetCompilerContext();
284 CHECK(compiler_context != NULL);
285 return reinterpret_cast<art::compiler_llvm::CompilerLLVM*>(compiler_context);
286}
287
288inline static const art::compiler_llvm::CompilerLLVM* ContextOf(const art::Compiler& compiler) {
289 void *compiler_context = compiler.GetCompilerContext();
290 CHECK(compiler_context != NULL);
291 return reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler_context);
292}
293
294extern "C" void ArtInitCompilerContext(art::Compiler& compiler) {
295 CHECK(compiler.GetCompilerContext() == NULL);
296
297 art::compiler_llvm::CompilerLLVM* compiler_llvm =
298 new art::compiler_llvm::CompilerLLVM(&compiler,
299 compiler.GetInstructionSet());
300
301 compiler.SetCompilerContext(compiler_llvm);
302}
303
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700304extern "C" art::CompiledMethod* ArtCompileMethod(art::Compiler& compiler,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800305 const art::DexFile::CodeItem* code_item,
306 uint32_t access_flags, uint32_t method_idx,
307 const art::ClassLoader* class_loader,
308 const art::DexFile& dex_file)
309{
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800310 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
311 art::DexCache *dex_cache = class_linker->FindDexCache(dex_file);
312
313 art::OatCompilationUnit oat_compilation_unit(
314 class_loader, class_linker, dex_file, *dex_cache, code_item,
315 method_idx, access_flags);
TDYa1270200d072012-04-17 20:55:08 -0700316 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
317 art::CompiledMethod* result = compiler_llvm->CompileDexMethod(&oat_compilation_unit);
318 compiler_llvm->MaterializeIfThresholdReached();
319 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800320}
321
322extern "C" art::CompiledMethod* ArtJniCompileMethod(art::Compiler& compiler,
323 uint32_t access_flags, uint32_t method_idx,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800324 const art::DexFile& dex_file) {
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800325 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
326 art::DexCache *dex_cache = class_linker->FindDexCache(dex_file);
327
328 art::OatCompilationUnit oat_compilation_unit(
Shih-wei Liaob1ab7df2012-03-29 13:53:46 -0700329 NULL, class_linker, dex_file, *dex_cache, NULL,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800330 method_idx, access_flags);
331
Logan Chien106b2a02012-03-18 04:41:38 +0800332 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700333 art::CompiledMethod* result = compiler_llvm->CompileNativeMethod(&oat_compilation_unit);
334 compiler_llvm->MaterializeIfThresholdReached();
Elliott Hughes13b835a2012-03-13 19:45:22 -0700335 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800336}
337
338extern "C" art::CompiledInvokeStub* ArtCreateInvokeStub(art::Compiler& compiler, bool is_static,
339 const char* shorty, uint32_t shorty_len) {
TDYa1270200d072012-04-17 20:55:08 -0700340 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
341 art::CompiledInvokeStub* result = compiler_llvm->CreateInvokeStub(is_static, shorty);
342 compiler_llvm->MaterializeIfThresholdReached();
343 return result;
Logan Chien106b2a02012-03-18 04:41:38 +0800344}
345
Logan Chien106b2a02012-03-18 04:41:38 +0800346extern "C" void compilerLLVMSetBitcodeFileName(art::Compiler& compiler,
347 std::string const& filename) {
348 ContextOf(compiler)->SetBitcodeFileName(filename);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800349}
350
351extern "C" void compilerLLVMMaterializeRemainder(art::Compiler& compiler) {
Logan Chien106b2a02012-03-18 04:41:38 +0800352 ContextOf(compiler)->MaterializeRemainder();
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800353}
354
Logan Chienf7015fd2012-03-18 01:19:37 +0800355extern "C" void compilerLLVMEnableAutoElfLoading(art::Compiler& compiler) {
356 art::compiler_llvm::CompilerLLVM* compiler_llvm =
357 reinterpret_cast<art::compiler_llvm::CompilerLLVM*>(compiler.GetCompilerContext());
358 return compiler_llvm->EnableAutoElfLoading();
359}
360
361extern "C" const void* compilerLLVMGetMethodCodeAddr(const art::Compiler& compiler,
362 const art::CompiledMethod* cm,
Logan Chien937105a2012-04-02 02:37:37 +0800363 const art::Method*) {
Logan Chienf7015fd2012-03-18 01:19:37 +0800364 const art::compiler_llvm::CompilerLLVM* compiler_llvm =
365 reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler.GetCompilerContext());
Logan Chien937105a2012-04-02 02:37:37 +0800366 return compiler_llvm->GetMethodCodeAddr(cm);
Logan Chienf7015fd2012-03-18 01:19:37 +0800367}
368
369extern "C" const art::Method::InvokeStub* compilerLLVMGetMethodInvokeStubAddr(const art::Compiler& compiler,
370 const art::CompiledInvokeStub* cm,
Logan Chien937105a2012-04-02 02:37:37 +0800371 const art::Method*) {
Logan Chienf7015fd2012-03-18 01:19:37 +0800372 const art::compiler_llvm::CompilerLLVM* compiler_llvm =
373 reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler.GetCompilerContext());
Logan Chien937105a2012-04-02 02:37:37 +0800374 return compiler_llvm->GetMethodInvokeStubAddr(cm);
Logan Chienf7015fd2012-03-18 01:19:37 +0800375}
376
Logan Chiendf576142012-03-20 17:36:32 +0800377extern "C" std::vector<art::ElfImage> compilerLLVMGetElfImages(const art::Compiler& compiler) {
378 return ContextOf(compiler)->GetElfImages();
379}
380
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800381extern "C" void compilerLLVMDispose(art::Compiler& compiler) {
Logan Chien106b2a02012-03-18 04:41:38 +0800382 delete ContextOf(compiler);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800383}