blob: 45f9931e976e7fdcfd937ce1afb87d62302afaea [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
Elliott Hughes1aa246d2012-12-13 09:29:36 -080019#include "base/stl_util.h"
Shih-wei Liao26e93072012-05-30 19:13:08 -070020#include "backend_options.h"
Shih-wei Liaoc4c98812012-03-10 21:55:51 -080021#include "class_linker.h"
Logan Chienf7015fd2012-03-18 01:19:37 +080022#include "compiled_method.h"
Ian Rogers1212a022013-03-04 10:48:41 -080023#include "compiler/driver/compiler_driver.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080024#include "ir_builder.h"
Ian Rogers02113782013-03-04 12:12:48 -080025#include "compiler/jni/portable/jni_compiler.h"
Brian Carlstrom641ce032013-01-31 15:21:37 -080026#include "llvm_compilation_unit.h"
Logan Chien4dd96f52012-02-29 01:26:58 +080027#include "oat_compilation_unit.h"
Logan Chien0c717dd2012-03-28 18:31:07 +080028#include "oat_file.h"
TDYa127eead4ac2012-06-03 07:15:25 -070029#include "stub_compiler.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070030#include "utils_llvm.h"
TDYa127ce4cc0d2012-11-18 16:59:53 -080031#include "verifier/method_verifier.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
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070039namespace art {
Ian Rogers1212a022013-03-04 10:48:41 -080040void CompileOneMethod(CompilerDriver& driver,
buzbeec531cef2012-10-18 07:09:20 -070041 const CompilerBackend compilerBackend,
42 const DexFile::CodeItem* code_item,
43 uint32_t access_flags, InvokeType invoke_type,
TDYa127dc5daa02013-01-09 21:31:37 +080044 uint32_t class_def_idx, uint32_t method_idx, jobject class_loader,
buzbeec531cef2012-10-18 07:09:20 -070045 const DexFile& dex_file,
46 LLVMInfo* llvm_info);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070047}
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070048
Logan Chien013b6f22012-03-02 17:20:33 +080049namespace llvm {
50 extern bool TimePassesIsEnabled;
51}
52
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 Chienc3f8fa52012-05-11 11:23:39 +080058 // Initialize LLVM internal data structure for multithreading
59 llvm::llvm_start_multithreaded();
60
Logan Chien013b6f22012-03-02 17:20:33 +080061 // NOTE: Uncomment following line to show the time consumption of LLVM passes
62 //llvm::TimePassesIsEnabled = true;
63
Shih-wei Liao26e93072012-05-30 19:13:08 -070064 // Initialize LLVM target-specific options.
65 art::compiler_llvm::InitialBackendOptions();
Logan Chienc3f8fa52012-05-11 11:23:39 +080066
Shih-wei Liao1335a952012-07-23 18:03:00 -070067 // Initialize LLVM target, MC subsystem, asm printer, and asm parser.
68#if defined(ART_TARGET)
69 // Don't initialize all targets on device. Just initialize the device's native target
70 llvm::InitializeNativeTarget();
71 llvm::InitializeNativeTargetAsmPrinter();
72 llvm::InitializeNativeTargetAsmParser();
73#else
Logan Chien8b977d32012-02-21 19:14:55 +080074 llvm::InitializeAllTargets();
75 llvm::InitializeAllTargetMCs();
76 llvm::InitializeAllAsmPrinters();
77 llvm::InitializeAllAsmParsers();
Shih-wei Liao1335a952012-07-23 18:03:00 -070078#endif
Logan Chien8b977d32012-02-21 19:14:55 +080079
Logan Chiendd7cf5b2012-03-01 12:55:19 +080080 // Initialize LLVM optimization passes
81 llvm::PassRegistry &registry = *llvm::PassRegistry::getPassRegistry();
82
83 llvm::initializeCore(registry);
84 llvm::initializeScalarOpts(registry);
85 llvm::initializeIPO(registry);
86 llvm::initializeAnalysis(registry);
87 llvm::initializeIPA(registry);
88 llvm::initializeTransformUtils(registry);
89 llvm::initializeInstCombine(registry);
90 llvm::initializeInstrumentation(registry);
91 llvm::initializeTarget(registry);
Logan Chien8b977d32012-02-21 19:14:55 +080092}
93
Logan Chienf1306552012-03-16 11:17:53 +080094// The Guard to Shutdown LLVM
Logan Chienaeb53032012-03-18 02:29:38 +080095// llvm::llvm_shutdown_obj llvm_guard;
96// TODO: We are commenting out this line because this will cause SEGV from
97// time to time.
98// Two reasons: (1) the order of the destruction of static objects, or
99// (2) dlopen/dlclose side-effect on static objects.
Shih-wei Liaofc34adb2012-03-07 08:51:44 -0800100
101} // anonymous namespace
102
103
104namespace art {
105namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800106
107
Logan Chiene75a8cc2012-02-24 12:26:43 +0800108llvm::Module* makeLLVMModuleContents(llvm::Module* module);
Logan Chien42e0e152012-01-13 15:42:36 +0800109
110
Ian Rogers1212a022013-03-04 10:48:41 -0800111CompilerLLVM::CompilerLLVM(CompilerDriver* driver, InstructionSet insn_set)
112 : compiler_driver_(driver), insn_set_(insn_set),
Logan Chien971bf3f2012-05-01 15:47:55 +0800113 num_cunits_lock_("compilation unit counter lock"), num_cunits_(0),
114 plt_(insn_set) {
Shih-wei Liaofc34adb2012-03-07 08:51:44 -0800115
116 // Initialize LLVM libraries
117 pthread_once(&llvm_initialized, InitializeLLVM);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800118}
119
120
121CompilerLLVM::~CompilerLLVM() {
122}
123
124
Brian Carlstrom641ce032013-01-31 15:21:37 -0800125LlvmCompilationUnit* CompilerLLVM::AllocateCompilationUnit() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700126 MutexLock GUARD(Thread::Current(), num_cunits_lock_);
Logan Chien69307df2013-01-28 09:14:36 +0800127 LlvmCompilationUnit* cunit = new LlvmCompilationUnit(this, ++num_cunits_);
TDYa127b672d1e2012-06-28 21:21:45 -0700128 if (!bitcode_filename_.empty()) {
Brian Carlstrom641ce032013-01-31 15:21:37 -0800129 cunit->SetBitcodeFileName(StringPrintf("%s-%zu", bitcode_filename_.c_str(), cunit->GetIndex()));
TDYa127b672d1e2012-06-28 21:21:45 -0700130 }
131 return cunit;
Logan Chiendf576142012-03-20 17:36:32 +0800132}
133
134
Logan Chien7f767612012-03-01 18:54:49 +0800135CompiledMethod* CompilerLLVM::
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700136CompileDexMethod(OatCompilationUnit* oat_compilation_unit, InvokeType invoke_type) {
Brian Carlstrom641ce032013-01-31 15:21:37 -0800137 UniquePtr<LlvmCompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800138
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700139 std::string methodName(PrettyMethod(oat_compilation_unit->GetDexMethodIndex(),
140 *oat_compilation_unit->GetDexFile()));
Ian Rogersc928de92013-02-27 14:30:44 -0800141 // TODO: consolidate ArtCompileMethods
Ian Rogers1212a022013-03-04 10:48:41 -0800142 CompileOneMethod(*compiler_driver_,
Ian Rogersc928de92013-02-27 14:30:44 -0800143 kPortable,
144 oat_compilation_unit->GetCodeItem(),
145 oat_compilation_unit->access_flags_,
146 invoke_type,
147 oat_compilation_unit->GetClassDefIndex(),
148 oat_compilation_unit->GetDexMethodIndex(),
149 oat_compilation_unit->GetClassLoader(),
150 *oat_compilation_unit->GetDexFile(),
151 cunit->GetQuickContext()
152 );
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700153
Ian Rogers1212a022013-03-04 10:48:41 -0800154 cunit->SetCompiler(compiler_driver_);
Ian Rogersc928de92013-02-27 14:30:44 -0800155 cunit->SetOatCompilationUnit(oat_compilation_unit);
buzbee26f10ee2012-12-21 11:16:29 -0800156
Ian Rogersc928de92013-02-27 14:30:44 -0800157 cunit->Materialize();
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700158
Ian Rogers1212a022013-03-04 10:48:41 -0800159 CompilerDriver::MethodReference mref(oat_compilation_unit->GetDexFile(),
160 oat_compilation_unit->GetDexMethodIndex());
161 return new CompiledMethod(compiler_driver_->GetInstructionSet(),
Ian Rogersc928de92013-02-27 14:30:44 -0800162 cunit->GetCompiledCode(),
163 *verifier::MethodVerifier::GetDexGcMap(mref));
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800164}
Logan Chien83426162011-12-09 09:29:50 +0800165
166
Logan Chien7f767612012-03-01 18:54:49 +0800167CompiledMethod* CompilerLLVM::
168CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
Brian Carlstrom641ce032013-01-31 15:21:37 -0800169 UniquePtr<LlvmCompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800170
Logan Chien8b977d32012-02-21 19:14:55 +0800171 UniquePtr<JniCompiler> jni_compiler(
Ian Rogers1212a022013-03-04 10:48:41 -0800172 new JniCompiler(cunit.get(), *compiler_driver_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800173
Logan Chien7f767612012-03-01 18:54:49 +0800174 return jni_compiler->Compile();
Logan Chien88894ee2012-02-13 16:42:22 +0800175}
176
177
Logan Chienf04364f2012-02-10 12:01:39 +0800178CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
179 char const *shorty) {
Brian Carlstrom641ce032013-01-31 15:21:37 -0800180 UniquePtr<LlvmCompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800181
TDYa127eead4ac2012-06-03 07:15:25 -0700182 UniquePtr<StubCompiler> stub_compiler(
Ian Rogers1212a022013-03-04 10:48:41 -0800183 new StubCompiler(cunit.get(), *compiler_driver_));
Logan Chien8b977d32012-02-21 19:14:55 +0800184
Logan Chien7a2a23a2012-06-06 11:01:00 +0800185 return stub_compiler->CreateInvokeStub(is_static, shorty);
186}
TDYa127eead4ac2012-06-03 07:15:25 -0700187
TDYa127eead4ac2012-06-03 07:15:25 -0700188
Logan Chien7a2a23a2012-06-06 11:01:00 +0800189CompiledInvokeStub* CompilerLLVM::CreateProxyStub(char const *shorty) {
Brian Carlstrom641ce032013-01-31 15:21:37 -0800190 UniquePtr<LlvmCompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien7a2a23a2012-06-06 11:01:00 +0800191
192 UniquePtr<StubCompiler> stub_compiler(
Ian Rogers1212a022013-03-04 10:48:41 -0800193 new StubCompiler(cunit.get(), *compiler_driver_));
Logan Chien7a2a23a2012-06-06 11:01:00 +0800194
195 return stub_compiler->CreateProxyStub(shorty);
Logan Chienf04364f2012-02-10 12:01:39 +0800196}
197
Logan Chien83426162011-12-09 09:29:50 +0800198} // namespace compiler_llvm
199} // namespace art
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800200
Ian Rogers1212a022013-03-04 10:48:41 -0800201inline static art::compiler_llvm::CompilerLLVM* ContextOf(art::CompilerDriver& driver) {
202 void *compiler_context = driver.GetCompilerContext();
Logan Chien106b2a02012-03-18 04:41:38 +0800203 CHECK(compiler_context != NULL);
204 return reinterpret_cast<art::compiler_llvm::CompilerLLVM*>(compiler_context);
205}
206
Ian Rogers1212a022013-03-04 10:48:41 -0800207inline static const art::compiler_llvm::CompilerLLVM* ContextOf(const art::CompilerDriver& driver) {
208 void *compiler_context = driver.GetCompilerContext();
Logan Chien106b2a02012-03-18 04:41:38 +0800209 CHECK(compiler_context != NULL);
210 return reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler_context);
211}
212
Ian Rogers1212a022013-03-04 10:48:41 -0800213extern "C" void ArtInitCompilerContext(art::CompilerDriver& driver) {
214 CHECK(driver.GetCompilerContext() == NULL);
Logan Chien106b2a02012-03-18 04:41:38 +0800215
216 art::compiler_llvm::CompilerLLVM* compiler_llvm =
Ian Rogers1212a022013-03-04 10:48:41 -0800217 new art::compiler_llvm::CompilerLLVM(&driver,
218 driver.GetInstructionSet());
Logan Chien106b2a02012-03-18 04:41:38 +0800219
Ian Rogers1212a022013-03-04 10:48:41 -0800220 driver.SetCompilerContext(compiler_llvm);
Logan Chien106b2a02012-03-18 04:41:38 +0800221}
222
Ian Rogers1212a022013-03-04 10:48:41 -0800223extern "C" void ArtUnInitCompilerContext(art::CompilerDriver& driver) {
224 delete ContextOf(driver);
225 driver.SetCompilerContext(NULL);
Logan Chien971bf3f2012-05-01 15:47:55 +0800226}
Ian Rogers1212a022013-03-04 10:48:41 -0800227extern "C" art::CompiledMethod* ArtCompileMethod(art::CompilerDriver& driver,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800228 const art::DexFile::CodeItem* code_item,
Ian Rogers08f753d2012-08-24 14:35:25 -0700229 uint32_t access_flags,
230 art::InvokeType invoke_type,
Ian Rogersfffdb022013-01-04 15:14:08 -0800231 uint32_t class_def_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700232 uint32_t method_idx,
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700233 jobject class_loader,
Ian Rogers08f753d2012-08-24 14:35:25 -0700234 const art::DexFile& dex_file) {
Ian Rogersfffdb022013-01-04 15:14:08 -0800235 UNUSED(class_def_idx); // TODO: this is used with Compiler::RequiresConstructorBarrier.
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800236 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800237
238 art::OatCompilationUnit oat_compilation_unit(
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700239 class_loader, class_linker, dex_file, code_item,
TDYa127dc5daa02013-01-09 21:31:37 +0800240 class_def_idx, method_idx, access_flags);
Ian Rogers1212a022013-03-04 10:48:41 -0800241 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(driver);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700242 art::CompiledMethod* result = compiler_llvm->CompileDexMethod(&oat_compilation_unit, invoke_type);
TDYa1270200d072012-04-17 20:55:08 -0700243 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800244}
245
Ian Rogers1212a022013-03-04 10:48:41 -0800246extern "C" art::CompiledMethod* ArtLLVMJniCompileMethod(art::CompilerDriver& driver,
Brian Carlstrom00bc1dc2013-02-01 15:56:27 -0800247 uint32_t access_flags, uint32_t method_idx,
248 const art::DexFile& dex_file) {
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800249 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800250
251 art::OatCompilationUnit oat_compilation_unit(
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700252 NULL, class_linker, dex_file, NULL,
TDYa127dc5daa02013-01-09 21:31:37 +0800253 0, method_idx, access_flags);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800254
Ian Rogers1212a022013-03-04 10:48:41 -0800255 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(driver);
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700256 art::CompiledMethod* result = compiler_llvm->CompileNativeMethod(&oat_compilation_unit);
Elliott Hughes13b835a2012-03-13 19:45:22 -0700257 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800258}
259
Ian Rogers1212a022013-03-04 10:48:41 -0800260extern "C" art::CompiledInvokeStub* ArtCreateLLVMInvokeStub(art::CompilerDriver& driver,
buzbee02031b12012-11-23 09:41:35 -0800261 bool is_static,
262 const char* shorty,
263 uint32_t shorty_len) {
Ian Rogers1212a022013-03-04 10:48:41 -0800264 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(driver);
TDYa1270200d072012-04-17 20:55:08 -0700265 art::CompiledInvokeStub* result = compiler_llvm->CreateInvokeStub(is_static, shorty);
TDYa1270200d072012-04-17 20:55:08 -0700266 return result;
Logan Chien106b2a02012-03-18 04:41:38 +0800267}
268
Ian Rogers1212a022013-03-04 10:48:41 -0800269extern "C" art::CompiledInvokeStub* ArtCreateProxyStub(art::CompilerDriver& driver,
Logan Chien7a2a23a2012-06-06 11:01:00 +0800270 const char* shorty,
271 uint32_t shorty_len) {
Ian Rogers1212a022013-03-04 10:48:41 -0800272 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(driver);
Logan Chien7a2a23a2012-06-06 11:01:00 +0800273 art::CompiledInvokeStub* result = compiler_llvm->CreateProxyStub(shorty);
Logan Chien7a2a23a2012-06-06 11:01:00 +0800274 return result;
275}
276
Ian Rogers1212a022013-03-04 10:48:41 -0800277extern "C" void compilerLLVMSetBitcodeFileName(art::CompilerDriver& driver,
Logan Chien106b2a02012-03-18 04:41:38 +0800278 std::string const& filename) {
Ian Rogers1212a022013-03-04 10:48:41 -0800279 ContextOf(driver)->SetBitcodeFileName(filename);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800280}