blob: 29c381d14348b373f20376721291289038eb6896 [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 Chien8b977d32012-02-21 19:14:55 +080022#include "compilation_unit.h"
Logan Chienf7015fd2012-03-18 01:19:37 +080023#include "compiled_method.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080024#include "compiler.h"
25#include "ir_builder.h"
Logan Chien88894ee2012-02-13 16:42:22 +080026#include "jni_compiler.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070027#ifndef ART_USE_DEXLANG_FRONTEND
28# include "method_compiler.h"
29#endif
Logan Chien4dd96f52012-02-29 01:26:58 +080030#include "oat_compilation_unit.h"
Logan Chien0c717dd2012-03-28 18:31:07 +080031#include "oat_file.h"
TDYa127eead4ac2012-06-03 07:15:25 -070032#include "stub_compiler.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070033#include "utils_llvm.h"
TDYa127ce4cc0d2012-11-18 16:59:53 -080034#include "verifier/method_verifier.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080035
Logan Chiendd7cf5b2012-03-01 12:55:19 +080036#include <llvm/LinkAllPasses.h>
37#include <llvm/LinkAllVMCore.h>
Logan Chien013b6f22012-03-02 17:20:33 +080038#include <llvm/Support/ManagedStatic.h>
Logan Chien8b977d32012-02-21 19:14:55 +080039#include <llvm/Support/TargetSelect.h>
40#include <llvm/Support/Threading.h>
41
buzbeec531cef2012-10-18 07:09:20 -070042#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070043namespace art {
buzbee52a77fc2012-11-20 19:50:46 -080044void CompileOneMethod(Compiler& compiler,
buzbeec531cef2012-10-18 07:09:20 -070045 const CompilerBackend compilerBackend,
46 const DexFile::CodeItem* code_item,
47 uint32_t access_flags, InvokeType invoke_type,
48 uint32_t method_idx, jobject class_loader,
49 const DexFile& dex_file,
50 LLVMInfo* llvm_info);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070051}
52#endif
53
Logan Chien013b6f22012-03-02 17:20:33 +080054namespace llvm {
55 extern bool TimePassesIsEnabled;
56}
57
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080058namespace {
Logan Chien8b977d32012-02-21 19:14:55 +080059
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080060pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT;
61
62void InitializeLLVM() {
Logan Chienc3f8fa52012-05-11 11:23:39 +080063 // Initialize LLVM internal data structure for multithreading
64 llvm::llvm_start_multithreaded();
65
Logan Chien013b6f22012-03-02 17:20:33 +080066 // NOTE: Uncomment following line to show the time consumption of LLVM passes
67 //llvm::TimePassesIsEnabled = true;
68
Shih-wei Liao26e93072012-05-30 19:13:08 -070069 // Initialize LLVM target-specific options.
70 art::compiler_llvm::InitialBackendOptions();
Logan Chienc3f8fa52012-05-11 11:23:39 +080071
Shih-wei Liao1335a952012-07-23 18:03:00 -070072 // Initialize LLVM target, MC subsystem, asm printer, and asm parser.
73#if defined(ART_TARGET)
74 // Don't initialize all targets on device. Just initialize the device's native target
75 llvm::InitializeNativeTarget();
76 llvm::InitializeNativeTargetAsmPrinter();
77 llvm::InitializeNativeTargetAsmParser();
78#else
Logan Chien8b977d32012-02-21 19:14:55 +080079 llvm::InitializeAllTargets();
80 llvm::InitializeAllTargetMCs();
81 llvm::InitializeAllAsmPrinters();
82 llvm::InitializeAllAsmParsers();
Shih-wei Liao1335a952012-07-23 18:03:00 -070083#endif
Logan Chien8b977d32012-02-21 19:14:55 +080084
Logan Chiendd7cf5b2012-03-01 12:55:19 +080085 // Initialize LLVM optimization passes
86 llvm::PassRegistry &registry = *llvm::PassRegistry::getPassRegistry();
87
88 llvm::initializeCore(registry);
89 llvm::initializeScalarOpts(registry);
90 llvm::initializeIPO(registry);
91 llvm::initializeAnalysis(registry);
92 llvm::initializeIPA(registry);
93 llvm::initializeTransformUtils(registry);
94 llvm::initializeInstCombine(registry);
95 llvm::initializeInstrumentation(registry);
96 llvm::initializeTarget(registry);
Logan Chien8b977d32012-02-21 19:14:55 +080097}
98
Logan Chienf1306552012-03-16 11:17:53 +080099// The Guard to Shutdown LLVM
Logan Chienaeb53032012-03-18 02:29:38 +0800100// llvm::llvm_shutdown_obj llvm_guard;
101// TODO: We are commenting out this line because this will cause SEGV from
102// time to time.
103// Two reasons: (1) the order of the destruction of static objects, or
104// (2) dlopen/dlclose side-effect on static objects.
Shih-wei Liaofc34adb2012-03-07 08:51:44 -0800105
106} // anonymous namespace
107
108
109namespace art {
110namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800111
112
Logan Chiene75a8cc2012-02-24 12:26:43 +0800113llvm::Module* makeLLVMModuleContents(llvm::Module* module);
Logan Chien42e0e152012-01-13 15:42:36 +0800114
115
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800116CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set)
Logan Chien971bf3f2012-05-01 15:47:55 +0800117 : compiler_(compiler), insn_set_(insn_set),
118 num_cunits_lock_("compilation unit counter lock"), num_cunits_(0),
119 plt_(insn_set) {
Shih-wei Liaofc34adb2012-03-07 08:51:44 -0800120
121 // Initialize LLVM libraries
122 pthread_once(&llvm_initialized, InitializeLLVM);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800123}
124
125
126CompilerLLVM::~CompilerLLVM() {
127}
128
129
Logan Chien971bf3f2012-05-01 15:47:55 +0800130CompilationUnit* CompilerLLVM::AllocateCompilationUnit() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700131 MutexLock GUARD(Thread::Current(), num_cunits_lock_);
TDYa127b672d1e2012-06-28 21:21:45 -0700132 CompilationUnit* cunit = new CompilationUnit(this, num_cunits_++);
133 if (!bitcode_filename_.empty()) {
134 cunit->SetBitcodeFileName(StringPrintf("%s-%zu", bitcode_filename_.c_str(), num_cunits_-1));
135 }
136 return cunit;
Logan Chiendf576142012-03-20 17:36:32 +0800137}
138
139
Logan Chien7f767612012-03-01 18:54:49 +0800140CompiledMethod* CompilerLLVM::
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700141CompileDexMethod(OatCompilationUnit* oat_compilation_unit, InvokeType invoke_type) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800142 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800143
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700144#if defined(ART_USE_DEXLANG_FRONTEND)
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700145 // Run DexLang for Dex to Greenland Bitcode
146 UniquePtr<greenland::DexLang> dex_lang(
147 new greenland::DexLang(*cunit->GetDexLangContext(), *compiler_,
148 *oat_compilation_unit));
149
150 llvm::Function* func = dex_lang->Build();
151 CHECK(func != NULL);
152
153 cunit->Materialize();
154
155 return new CompiledMethod(cunit->GetInstructionSet(),
156 cunit->GetCompiledCode());
buzbeec531cef2012-10-18 07:09:20 -0700157#elif defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700158 std::string methodName(PrettyMethod(oat_compilation_unit->GetDexMethodIndex(),
159 *oat_compilation_unit->GetDexFile()));
TDYa12787caa7e2012-08-25 23:23:27 -0700160 if (insn_set_ == kX86) {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700161 // Use iceland
162 UniquePtr<MethodCompiler> method_compiler(
163 new MethodCompiler(cunit.get(), compiler_, oat_compilation_unit));
164
165 return method_compiler->Compile();
166 } else {
buzbee26f10ee2012-12-21 11:16:29 -0800167
168#if 1
169 /*
170 * FIXME: temporary workaround
171 * Until Portable/llvm is fixed, use Iceland.
172 */
173 UniquePtr<MethodCompiler> method_compiler(
174 new MethodCompiler(cunit.get(), compiler_, oat_compilation_unit));
175
176 return method_compiler->Compile();
177#endif
178
buzbeec531cef2012-10-18 07:09:20 -0700179 // TODO: consolidate ArtCompileMethods
buzbee52a77fc2012-11-20 19:50:46 -0800180 CompileOneMethod(*compiler_,
buzbeec531cef2012-10-18 07:09:20 -0700181 kPortable,
182 oat_compilation_unit->GetCodeItem(),
183 oat_compilation_unit->access_flags_,
184 invoke_type,
185 oat_compilation_unit->GetDexMethodIndex(),
186 oat_compilation_unit->GetClassLoader(),
187 *oat_compilation_unit->GetDexFile(),
188 cunit->GetQuickContext()
189 );
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700190
191 cunit->SetCompiler(compiler_);
192 cunit->SetOatCompilationUnit(oat_compilation_unit);
193
194 cunit->Materialize();
195
buzbee26f10ee2012-12-21 11:16:29 -0800196 Compiler::MethodReference mref(oat_compilation_unit->GetDexFile(),
197 oat_compilation_unit->GetDexMethodIndex());
198 return new CompiledMethod(compiler_->GetInstructionSet(),
199 cunit->GetCompiledCode(),
TDYa127ce4cc0d2012-11-18 16:59:53 -0800200 *verifier::MethodVerifier::GetDexGcMap(mref));
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700201 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700202#else
Logan Chien8b977d32012-02-21 19:14:55 +0800203 UniquePtr<MethodCompiler> method_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800204 new MethodCompiler(cunit.get(), compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800205
Logan Chien7f767612012-03-01 18:54:49 +0800206 return method_compiler->Compile();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700207#endif
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800208}
Logan Chien83426162011-12-09 09:29:50 +0800209
210
Logan Chien7f767612012-03-01 18:54:49 +0800211CompiledMethod* CompilerLLVM::
212CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800213 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800214
Logan Chien8b977d32012-02-21 19:14:55 +0800215 UniquePtr<JniCompiler> jni_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800216 new JniCompiler(cunit.get(), *compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800217
Logan Chien7f767612012-03-01 18:54:49 +0800218 return jni_compiler->Compile();
Logan Chien88894ee2012-02-13 16:42:22 +0800219}
220
221
Logan Chienf04364f2012-02-10 12:01:39 +0800222CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
223 char const *shorty) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800224 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800225
TDYa127eead4ac2012-06-03 07:15:25 -0700226 UniquePtr<StubCompiler> stub_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800227 new StubCompiler(cunit.get(), *compiler_));
Logan Chien8b977d32012-02-21 19:14:55 +0800228
Logan Chien7a2a23a2012-06-06 11:01:00 +0800229 return stub_compiler->CreateInvokeStub(is_static, shorty);
230}
TDYa127eead4ac2012-06-03 07:15:25 -0700231
TDYa127eead4ac2012-06-03 07:15:25 -0700232
Logan Chien7a2a23a2012-06-06 11:01:00 +0800233CompiledInvokeStub* CompilerLLVM::CreateProxyStub(char const *shorty) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800234 UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien7a2a23a2012-06-06 11:01:00 +0800235
236 UniquePtr<StubCompiler> stub_compiler(
Logan Chien971bf3f2012-05-01 15:47:55 +0800237 new StubCompiler(cunit.get(), *compiler_));
Logan Chien7a2a23a2012-06-06 11:01:00 +0800238
239 return stub_compiler->CreateProxyStub(shorty);
Logan Chienf04364f2012-02-10 12:01:39 +0800240}
241
Logan Chien83426162011-12-09 09:29:50 +0800242} // namespace compiler_llvm
243} // namespace art
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800244
Logan Chien106b2a02012-03-18 04:41:38 +0800245inline static art::compiler_llvm::CompilerLLVM* ContextOf(art::Compiler& compiler) {
246 void *compiler_context = compiler.GetCompilerContext();
247 CHECK(compiler_context != NULL);
248 return reinterpret_cast<art::compiler_llvm::CompilerLLVM*>(compiler_context);
249}
250
251inline static const art::compiler_llvm::CompilerLLVM* ContextOf(const art::Compiler& compiler) {
252 void *compiler_context = compiler.GetCompilerContext();
253 CHECK(compiler_context != NULL);
254 return reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler_context);
255}
256
257extern "C" void ArtInitCompilerContext(art::Compiler& compiler) {
258 CHECK(compiler.GetCompilerContext() == NULL);
259
260 art::compiler_llvm::CompilerLLVM* compiler_llvm =
261 new art::compiler_llvm::CompilerLLVM(&compiler,
262 compiler.GetInstructionSet());
263
264 compiler.SetCompilerContext(compiler_llvm);
265}
266
Logan Chien971bf3f2012-05-01 15:47:55 +0800267extern "C" void ArtUnInitCompilerContext(art::Compiler& compiler) {
268 delete ContextOf(compiler);
269 compiler.SetCompilerContext(NULL);
270}
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700271extern "C" art::CompiledMethod* ArtCompileMethod(art::Compiler& compiler,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800272 const art::DexFile::CodeItem* code_item,
Ian Rogers08f753d2012-08-24 14:35:25 -0700273 uint32_t access_flags,
274 art::InvokeType invoke_type,
Ian Rogersfffdb022013-01-04 15:14:08 -0800275 uint32_t class_def_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700276 uint32_t method_idx,
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700277 jobject class_loader,
Ian Rogers08f753d2012-08-24 14:35:25 -0700278 const art::DexFile& dex_file) {
Ian Rogersfffdb022013-01-04 15:14:08 -0800279 UNUSED(class_def_idx); // TODO: this is used with Compiler::RequiresConstructorBarrier.
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800280 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800281
282 art::OatCompilationUnit oat_compilation_unit(
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700283 class_loader, class_linker, dex_file, code_item,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800284 method_idx, access_flags);
TDYa1270200d072012-04-17 20:55:08 -0700285 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700286 art::CompiledMethod* result = compiler_llvm->CompileDexMethod(&oat_compilation_unit, invoke_type);
TDYa1270200d072012-04-17 20:55:08 -0700287 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800288}
289
290extern "C" art::CompiledMethod* ArtJniCompileMethod(art::Compiler& compiler,
291 uint32_t access_flags, uint32_t method_idx,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800292 const art::DexFile& dex_file) {
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800293 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800294
295 art::OatCompilationUnit oat_compilation_unit(
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700296 NULL, class_linker, dex_file, NULL,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800297 method_idx, access_flags);
298
Logan Chien106b2a02012-03-18 04:41:38 +0800299 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700300 art::CompiledMethod* result = compiler_llvm->CompileNativeMethod(&oat_compilation_unit);
Elliott Hughes13b835a2012-03-13 19:45:22 -0700301 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800302}
303
buzbee02031b12012-11-23 09:41:35 -0800304extern "C" art::CompiledInvokeStub* ArtCreateLLVMInvokeStub(art::Compiler& compiler,
305 bool is_static,
306 const char* shorty,
307 uint32_t shorty_len) {
TDYa1270200d072012-04-17 20:55:08 -0700308 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
309 art::CompiledInvokeStub* result = compiler_llvm->CreateInvokeStub(is_static, shorty);
TDYa1270200d072012-04-17 20:55:08 -0700310 return result;
Logan Chien106b2a02012-03-18 04:41:38 +0800311}
312
Logan Chien7a2a23a2012-06-06 11:01:00 +0800313extern "C" art::CompiledInvokeStub* ArtCreateProxyStub(art::Compiler& compiler,
314 const char* shorty,
315 uint32_t shorty_len) {
316 art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
317 art::CompiledInvokeStub* result = compiler_llvm->CreateProxyStub(shorty);
Logan Chien7a2a23a2012-06-06 11:01:00 +0800318 return result;
319}
320
Logan Chien106b2a02012-03-18 04:41:38 +0800321extern "C" void compilerLLVMSetBitcodeFileName(art::Compiler& compiler,
322 std::string const& filename) {
323 ContextOf(compiler)->SetBitcodeFileName(filename);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800324}