blob: dfd0e326d2e9fd1785650d5f1e1c7d731b538598 [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 Liao26e93072012-05-30 19:13:08 -070019#include "backend_options.h"
Brian Carlstrom265091e2013-01-30 14:08:26 -080020#include "base/stl_util.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"
Ian Rogers89756f22013-03-04 16:40:02 -080024#include "compiler/driver/dex_compilation_unit.h"
Ian Rogers02113782013-03-04 12:12:48 -080025#include "compiler/jni/portable/jni_compiler.h"
Brian Carlstrom265091e2013-01-30 14:08:26 -080026#include "globals.h"
Ian Rogers4c1c2832013-03-04 18:30:13 -080027#include "ir_builder.h"
Brian Carlstrom641ce032013-01-31 15:21:37 -080028#include "llvm_compilation_unit.h"
Logan Chien0c717dd2012-03-28 18:31:07 +080029#include "oat_file.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>
Logan Chien013b6f22012-03-02 17:20:33 +080034#include <llvm/Support/ManagedStatic.h>
Logan Chien8b977d32012-02-21 19:14:55 +080035#include <llvm/Support/TargetSelect.h>
36#include <llvm/Support/Threading.h>
37
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070038namespace art {
Ian Rogers1212a022013-03-04 10:48:41 -080039void CompileOneMethod(CompilerDriver& driver,
buzbeec531cef2012-10-18 07:09:20 -070040 const CompilerBackend compilerBackend,
41 const DexFile::CodeItem* code_item,
42 uint32_t access_flags, InvokeType invoke_type,
TDYa127dc5daa02013-01-09 21:31:37 +080043 uint32_t class_def_idx, uint32_t method_idx, jobject class_loader,
buzbeec531cef2012-10-18 07:09:20 -070044 const DexFile& dex_file,
Brian Carlstrom265091e2013-01-30 14:08:26 -080045 llvm::LlvmCompilationUnit* llvm_info);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070046}
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070047
Logan Chien013b6f22012-03-02 17:20:33 +080048namespace llvm {
49 extern bool TimePassesIsEnabled;
50}
51
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 Chienc3f8fa52012-05-11 11:23:39 +080057 // Initialize LLVM internal data structure for multithreading
58 llvm::llvm_start_multithreaded();
59
Logan Chien013b6f22012-03-02 17:20:33 +080060 // NOTE: Uncomment following line to show the time consumption of LLVM passes
61 //llvm::TimePassesIsEnabled = true;
62
Shih-wei Liao26e93072012-05-30 19:13:08 -070063 // Initialize LLVM target-specific options.
Ian Rogers4c1c2832013-03-04 18:30:13 -080064 art::llvm::InitialBackendOptions();
Logan Chienc3f8fa52012-05-11 11:23:39 +080065
Shih-wei Liao1335a952012-07-23 18:03:00 -070066 // Initialize LLVM target, MC subsystem, asm printer, and asm parser.
Brian Carlstrom265091e2013-01-30 14:08:26 -080067 if (art::kIsTargetBuild) {
68 // Don't initialize all targets on device. Just initialize the device's native target
69 llvm::InitializeNativeTarget();
70 llvm::InitializeNativeTargetAsmPrinter();
71 llvm::InitializeNativeTargetAsmParser();
72 } else {
73 llvm::InitializeAllTargets();
74 llvm::InitializeAllTargetMCs();
75 llvm::InitializeAllAsmPrinters();
76 llvm::InitializeAllAsmParsers();
77 }
Logan Chien8b977d32012-02-21 19:14:55 +080078
Logan Chiendd7cf5b2012-03-01 12:55:19 +080079 // Initialize LLVM optimization passes
80 llvm::PassRegistry &registry = *llvm::PassRegistry::getPassRegistry();
81
82 llvm::initializeCore(registry);
83 llvm::initializeScalarOpts(registry);
84 llvm::initializeIPO(registry);
85 llvm::initializeAnalysis(registry);
86 llvm::initializeIPA(registry);
87 llvm::initializeTransformUtils(registry);
88 llvm::initializeInstCombine(registry);
89 llvm::initializeInstrumentation(registry);
90 llvm::initializeTarget(registry);
Logan Chien8b977d32012-02-21 19:14:55 +080091}
92
Logan Chienf1306552012-03-16 11:17:53 +080093// The Guard to Shutdown LLVM
Logan Chienaeb53032012-03-18 02:29:38 +080094// llvm::llvm_shutdown_obj llvm_guard;
95// TODO: We are commenting out this line because this will cause SEGV from
96// time to time.
97// Two reasons: (1) the order of the destruction of static objects, or
98// (2) dlopen/dlclose side-effect on static objects.
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080099
100} // anonymous namespace
101
102
103namespace art {
Ian Rogers4c1c2832013-03-04 18:30:13 -0800104namespace llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800105
106
Ian Rogers4c1c2832013-03-04 18:30:13 -0800107::llvm::Module* makeLLVMModuleContents(::llvm::Module* module);
Logan Chien42e0e152012-01-13 15:42:36 +0800108
109
Ian Rogers1212a022013-03-04 10:48:41 -0800110CompilerLLVM::CompilerLLVM(CompilerDriver* driver, InstructionSet insn_set)
111 : compiler_driver_(driver), insn_set_(insn_set),
Brian Carlstrom265091e2013-01-30 14:08:26 -0800112 next_cunit_id_lock_("compilation unit id lock"), next_cunit_id_(1) {
Shih-wei Liaofc34adb2012-03-07 08:51:44 -0800113
114 // Initialize LLVM libraries
115 pthread_once(&llvm_initialized, InitializeLLVM);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800116}
117
118
119CompilerLLVM::~CompilerLLVM() {
120}
121
122
Brian Carlstrom641ce032013-01-31 15:21:37 -0800123LlvmCompilationUnit* CompilerLLVM::AllocateCompilationUnit() {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800124 MutexLock GUARD(Thread::Current(), next_cunit_id_lock_);
125 LlvmCompilationUnit* cunit = new LlvmCompilationUnit(this, next_cunit_id_++);
TDYa127b672d1e2012-06-28 21:21:45 -0700126 if (!bitcode_filename_.empty()) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800127 cunit->SetBitcodeFileName(StringPrintf("%s-%zu",
128 bitcode_filename_.c_str(),
129 cunit->GetCompilationUnitId()));
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::
Ian Rogers89756f22013-03-04 16:40:02 -0800136CompileDexMethod(DexCompilationUnit* dex_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
Brian Carlstrom265091e2013-01-30 14:08:26 -0800139 cunit->SetDexCompilationUnit(dex_compilation_unit);
Ian Rogers0d94eb62013-03-06 15:20:50 -0800140 cunit->SetCompilerDriver(compiler_driver_);
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,
Ian Rogers89756f22013-03-04 16:40:02 -0800144 dex_compilation_unit->GetCodeItem(),
145 dex_compilation_unit->GetAccessFlags(),
Ian Rogersc928de92013-02-27 14:30:44 -0800146 invoke_type,
Ian Rogers89756f22013-03-04 16:40:02 -0800147 dex_compilation_unit->GetClassDefIndex(),
148 dex_compilation_unit->GetDexMethodIndex(),
149 dex_compilation_unit->GetClassLoader(),
150 *dex_compilation_unit->GetDexFile(),
Brian Carlstrom265091e2013-01-30 14:08:26 -0800151 cunit.get());
buzbee26f10ee2012-12-21 11:16:29 -0800152
Ian Rogersc928de92013-02-27 14:30:44 -0800153 cunit->Materialize();
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700154
Ian Rogers89756f22013-03-04 16:40:02 -0800155 CompilerDriver::MethodReference mref(dex_compilation_unit->GetDexFile(),
156 dex_compilation_unit->GetDexMethodIndex());
Ian Rogers1212a022013-03-04 10:48:41 -0800157 return new CompiledMethod(compiler_driver_->GetInstructionSet(),
Brian Carlstrom265091e2013-01-30 14:08:26 -0800158 cunit->GetElfObject(),
159 *verifier::MethodVerifier::GetDexGcMap(mref),
160 cunit->GetDexCompilationUnit()->GetSymbol());
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800161}
Logan Chien83426162011-12-09 09:29:50 +0800162
163
Logan Chien7f767612012-03-01 18:54:49 +0800164CompiledMethod* CompilerLLVM::
Ian Rogers89756f22013-03-04 16:40:02 -0800165CompileNativeMethod(DexCompilationUnit* dex_compilation_unit) {
Brian Carlstrom641ce032013-01-31 15:21:37 -0800166 UniquePtr<LlvmCompilationUnit> cunit(AllocateCompilationUnit());
Logan Chien8ba2fc52012-04-23 09:10:46 +0800167
Logan Chien8b977d32012-02-21 19:14:55 +0800168 UniquePtr<JniCompiler> jni_compiler(
Ian Rogers89756f22013-03-04 16:40:02 -0800169 new JniCompiler(cunit.get(), *compiler_driver_, dex_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800170
Logan Chien7f767612012-03-01 18:54:49 +0800171 return jni_compiler->Compile();
Logan Chien88894ee2012-02-13 16:42:22 +0800172}
173
174
Ian Rogers4c1c2832013-03-04 18:30:13 -0800175} // namespace llvm
Logan Chien83426162011-12-09 09:29:50 +0800176} // namespace art
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800177
Ian Rogers4c1c2832013-03-04 18:30:13 -0800178inline static art::llvm::CompilerLLVM* ContextOf(art::CompilerDriver& driver) {
Ian Rogers1212a022013-03-04 10:48:41 -0800179 void *compiler_context = driver.GetCompilerContext();
Logan Chien106b2a02012-03-18 04:41:38 +0800180 CHECK(compiler_context != NULL);
Ian Rogers4c1c2832013-03-04 18:30:13 -0800181 return reinterpret_cast<art::llvm::CompilerLLVM*>(compiler_context);
Logan Chien106b2a02012-03-18 04:41:38 +0800182}
183
Ian Rogers4c1c2832013-03-04 18:30:13 -0800184inline static const art::llvm::CompilerLLVM* ContextOf(const art::CompilerDriver& driver) {
Ian Rogers1212a022013-03-04 10:48:41 -0800185 void *compiler_context = driver.GetCompilerContext();
Logan Chien106b2a02012-03-18 04:41:38 +0800186 CHECK(compiler_context != NULL);
Ian Rogers4c1c2832013-03-04 18:30:13 -0800187 return reinterpret_cast<const art::llvm::CompilerLLVM*>(compiler_context);
Logan Chien106b2a02012-03-18 04:41:38 +0800188}
189
Ian Rogers1212a022013-03-04 10:48:41 -0800190extern "C" void ArtInitCompilerContext(art::CompilerDriver& driver) {
191 CHECK(driver.GetCompilerContext() == NULL);
Logan Chien106b2a02012-03-18 04:41:38 +0800192
Ian Rogers4c1c2832013-03-04 18:30:13 -0800193 art::llvm::CompilerLLVM* compiler_llvm = new art::llvm::CompilerLLVM(&driver,
194 driver.GetInstructionSet());
Logan Chien106b2a02012-03-18 04:41:38 +0800195
Ian Rogers1212a022013-03-04 10:48:41 -0800196 driver.SetCompilerContext(compiler_llvm);
Logan Chien106b2a02012-03-18 04:41:38 +0800197}
198
Ian Rogers1212a022013-03-04 10:48:41 -0800199extern "C" void ArtUnInitCompilerContext(art::CompilerDriver& driver) {
200 delete ContextOf(driver);
201 driver.SetCompilerContext(NULL);
Logan Chien971bf3f2012-05-01 15:47:55 +0800202}
Ian Rogers1212a022013-03-04 10:48:41 -0800203extern "C" art::CompiledMethod* ArtCompileMethod(art::CompilerDriver& driver,
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800204 const art::DexFile::CodeItem* code_item,
Ian Rogers08f753d2012-08-24 14:35:25 -0700205 uint32_t access_flags,
206 art::InvokeType invoke_type,
Ian Rogersfffdb022013-01-04 15:14:08 -0800207 uint32_t class_def_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700208 uint32_t method_idx,
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700209 jobject class_loader,
Ian Rogers08f753d2012-08-24 14:35:25 -0700210 const art::DexFile& dex_file) {
Ian Rogersfffdb022013-01-04 15:14:08 -0800211 UNUSED(class_def_idx); // TODO: this is used with Compiler::RequiresConstructorBarrier.
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800212 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800213
Ian Rogers89756f22013-03-04 16:40:02 -0800214 art::DexCompilationUnit dex_compilation_unit(
Brian Carlstrom265091e2013-01-30 14:08:26 -0800215 NULL, class_loader, class_linker, dex_file, code_item,
TDYa127dc5daa02013-01-09 21:31:37 +0800216 class_def_idx, method_idx, access_flags);
Ian Rogers4c1c2832013-03-04 18:30:13 -0800217 art::llvm::CompilerLLVM* compiler_llvm = ContextOf(driver);
Ian Rogers89756f22013-03-04 16:40:02 -0800218 art::CompiledMethod* result = compiler_llvm->CompileDexMethod(&dex_compilation_unit, invoke_type);
TDYa1270200d072012-04-17 20:55:08 -0700219 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800220}
221
Ian Rogers1212a022013-03-04 10:48:41 -0800222extern "C" art::CompiledMethod* ArtLLVMJniCompileMethod(art::CompilerDriver& driver,
Brian Carlstrom00bc1dc2013-02-01 15:56:27 -0800223 uint32_t access_flags, uint32_t method_idx,
224 const art::DexFile& dex_file) {
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800225 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800226
Ian Rogers89756f22013-03-04 16:40:02 -0800227 art::DexCompilationUnit dex_compilation_unit(
Brian Carlstrom265091e2013-01-30 14:08:26 -0800228 NULL, NULL, class_linker, dex_file, NULL,
TDYa127dc5daa02013-01-09 21:31:37 +0800229 0, method_idx, access_flags);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800230
Ian Rogers4c1c2832013-03-04 18:30:13 -0800231 art::llvm::CompilerLLVM* compiler_llvm = ContextOf(driver);
Ian Rogers89756f22013-03-04 16:40:02 -0800232 art::CompiledMethod* result = compiler_llvm->CompileNativeMethod(&dex_compilation_unit);
Elliott Hughes13b835a2012-03-13 19:45:22 -0700233 return result;
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800234}
235
Ian Rogers1212a022013-03-04 10:48:41 -0800236extern "C" void compilerLLVMSetBitcodeFileName(art::CompilerDriver& driver,
Logan Chien106b2a02012-03-18 04:41:38 +0800237 std::string const& filename) {
Ian Rogers1212a022013-03-04 10:48:41 -0800238 ContextOf(driver)->SetBitcodeFileName(filename);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800239}