Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 1 | /* |
| 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 Liao | 26e9307 | 2012-05-30 19:13:08 -0700 | [diff] [blame] | 19 | #include "backend_options.h" |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 20 | #include "class_linker.h" |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 21 | #include "compilation_unit.h" |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 22 | #include "compiled_method.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 23 | #include "compiler.h" |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 24 | #include "dex_cache.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 25 | #include "ir_builder.h" |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 26 | #include "jni_compiler.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 27 | #include "method_compiler.h" |
Logan Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 28 | #include "oat_compilation_unit.h" |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 29 | #include "oat_file.h" |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 30 | #include "stl_util.h" |
TDYa127 | eead4ac | 2012-06-03 07:15:25 -0700 | [diff] [blame] | 31 | #include "stub_compiler.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 32 | |
Logan Chien | dd7cf5b | 2012-03-01 12:55:19 +0800 | [diff] [blame] | 33 | #include <llvm/LinkAllPasses.h> |
| 34 | #include <llvm/LinkAllVMCore.h> |
Logan Chien | 013b6f2 | 2012-03-02 17:20:33 +0800 | [diff] [blame] | 35 | #include <llvm/Support/ManagedStatic.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 36 | #include <llvm/Support/TargetSelect.h> |
| 37 | #include <llvm/Support/Threading.h> |
| 38 | |
Logan Chien | 013b6f2 | 2012-03-02 17:20:33 +0800 | [diff] [blame] | 39 | namespace llvm { |
| 40 | extern bool TimePassesIsEnabled; |
| 41 | } |
| 42 | |
Shih-wei Liao | fc34adb | 2012-03-07 08:51:44 -0800 | [diff] [blame] | 43 | namespace { |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 44 | |
Shih-wei Liao | fc34adb | 2012-03-07 08:51:44 -0800 | [diff] [blame] | 45 | pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT; |
| 46 | |
| 47 | void InitializeLLVM() { |
Logan Chien | c3f8fa5 | 2012-05-11 11:23:39 +0800 | [diff] [blame] | 48 | // Initialize LLVM internal data structure for multithreading |
| 49 | llvm::llvm_start_multithreaded(); |
| 50 | |
Logan Chien | 013b6f2 | 2012-03-02 17:20:33 +0800 | [diff] [blame] | 51 | // NOTE: Uncomment following line to show the time consumption of LLVM passes |
| 52 | //llvm::TimePassesIsEnabled = true; |
| 53 | |
Shih-wei Liao | 26e9307 | 2012-05-30 19:13:08 -0700 | [diff] [blame] | 54 | // Initialize LLVM target-specific options. |
| 55 | art::compiler_llvm::InitialBackendOptions(); |
Logan Chien | c3f8fa5 | 2012-05-11 11:23:39 +0800 | [diff] [blame] | 56 | |
Shih-wei Liao | 1335a95 | 2012-07-23 18:03:00 -0700 | [diff] [blame^] | 57 | // Initialize LLVM target, MC subsystem, asm printer, and asm parser. |
| 58 | #if defined(ART_TARGET) |
| 59 | // Don't initialize all targets on device. Just initialize the device's native target |
| 60 | llvm::InitializeNativeTarget(); |
| 61 | llvm::InitializeNativeTargetAsmPrinter(); |
| 62 | llvm::InitializeNativeTargetAsmParser(); |
| 63 | #else |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 64 | llvm::InitializeAllTargets(); |
| 65 | llvm::InitializeAllTargetMCs(); |
| 66 | llvm::InitializeAllAsmPrinters(); |
| 67 | llvm::InitializeAllAsmParsers(); |
Shih-wei Liao | 1335a95 | 2012-07-23 18:03:00 -0700 | [diff] [blame^] | 68 | #endif |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 69 | |
Logan Chien | dd7cf5b | 2012-03-01 12:55:19 +0800 | [diff] [blame] | 70 | // Initialize LLVM optimization passes |
| 71 | llvm::PassRegistry ®istry = *llvm::PassRegistry::getPassRegistry(); |
| 72 | |
| 73 | llvm::initializeCore(registry); |
| 74 | llvm::initializeScalarOpts(registry); |
| 75 | llvm::initializeIPO(registry); |
| 76 | llvm::initializeAnalysis(registry); |
| 77 | llvm::initializeIPA(registry); |
| 78 | llvm::initializeTransformUtils(registry); |
| 79 | llvm::initializeInstCombine(registry); |
| 80 | llvm::initializeInstrumentation(registry); |
| 81 | llvm::initializeTarget(registry); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 82 | } |
| 83 | |
Logan Chien | f130655 | 2012-03-16 11:17:53 +0800 | [diff] [blame] | 84 | // The Guard to Shutdown LLVM |
Logan Chien | aeb5303 | 2012-03-18 02:29:38 +0800 | [diff] [blame] | 85 | // llvm::llvm_shutdown_obj llvm_guard; |
| 86 | // TODO: We are commenting out this line because this will cause SEGV from |
| 87 | // time to time. |
| 88 | // Two reasons: (1) the order of the destruction of static objects, or |
| 89 | // (2) dlopen/dlclose side-effect on static objects. |
Shih-wei Liao | fc34adb | 2012-03-07 08:51:44 -0800 | [diff] [blame] | 90 | |
| 91 | } // anonymous namespace |
| 92 | |
| 93 | |
| 94 | namespace art { |
| 95 | namespace compiler_llvm { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 96 | |
| 97 | |
Logan Chien | e75a8cc | 2012-02-24 12:26:43 +0800 | [diff] [blame] | 98 | llvm::Module* makeLLVMModuleContents(llvm::Module* module); |
Logan Chien | 42e0e15 | 2012-01-13 15:42:36 +0800 | [diff] [blame] | 99 | |
| 100 | |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 101 | CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 102 | : compiler_(compiler), insn_set_(insn_set), |
| 103 | num_cunits_lock_("compilation unit counter lock"), num_cunits_(0), |
| 104 | plt_(insn_set) { |
Shih-wei Liao | fc34adb | 2012-03-07 08:51:44 -0800 | [diff] [blame] | 105 | |
| 106 | // Initialize LLVM libraries |
| 107 | pthread_once(&llvm_initialized, InitializeLLVM); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | |
| 111 | CompilerLLVM::~CompilerLLVM() { |
| 112 | } |
| 113 | |
| 114 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 115 | CompilationUnit* CompilerLLVM::AllocateCompilationUnit() { |
| 116 | MutexLock GUARD(num_cunits_lock_); |
| 117 | return new CompilationUnit(this, num_cunits_++); |
Logan Chien | df57614 | 2012-03-20 17:36:32 +0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 121 | CompiledMethod* CompilerLLVM:: |
| 122 | CompileDexMethod(OatCompilationUnit* oat_compilation_unit) { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 123 | UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit()); |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 124 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 125 | UniquePtr<MethodCompiler> method_compiler( |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 126 | new MethodCompiler(cunit.get(), compiler_, oat_compilation_unit)); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 127 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 128 | return method_compiler->Compile(); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 129 | } |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 130 | |
| 131 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 132 | CompiledMethod* CompilerLLVM:: |
| 133 | CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 134 | UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit()); |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 135 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 136 | UniquePtr<JniCompiler> jni_compiler( |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 137 | new JniCompiler(cunit.get(), *compiler_, oat_compilation_unit)); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 138 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 139 | return jni_compiler->Compile(); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 143 | CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static, |
| 144 | char const *shorty) { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 145 | UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit()); |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 146 | |
TDYa127 | eead4ac | 2012-06-03 07:15:25 -0700 | [diff] [blame] | 147 | UniquePtr<StubCompiler> stub_compiler( |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 148 | new StubCompiler(cunit.get(), *compiler_)); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 149 | |
Logan Chien | 7a2a23a | 2012-06-06 11:01:00 +0800 | [diff] [blame] | 150 | return stub_compiler->CreateInvokeStub(is_static, shorty); |
| 151 | } |
TDYa127 | eead4ac | 2012-06-03 07:15:25 -0700 | [diff] [blame] | 152 | |
TDYa127 | eead4ac | 2012-06-03 07:15:25 -0700 | [diff] [blame] | 153 | |
Logan Chien | 7a2a23a | 2012-06-06 11:01:00 +0800 | [diff] [blame] | 154 | CompiledInvokeStub* CompilerLLVM::CreateProxyStub(char const *shorty) { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 155 | UniquePtr<CompilationUnit> cunit(AllocateCompilationUnit()); |
Logan Chien | 7a2a23a | 2012-06-06 11:01:00 +0800 | [diff] [blame] | 156 | |
| 157 | UniquePtr<StubCompiler> stub_compiler( |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 158 | new StubCompiler(cunit.get(), *compiler_)); |
Logan Chien | 7a2a23a | 2012-06-06 11:01:00 +0800 | [diff] [blame] | 159 | |
| 160 | return stub_compiler->CreateProxyStub(shorty); |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 161 | } |
| 162 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 163 | } // namespace compiler_llvm |
| 164 | } // namespace art |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 165 | |
Logan Chien | 106b2a0 | 2012-03-18 04:41:38 +0800 | [diff] [blame] | 166 | inline static art::compiler_llvm::CompilerLLVM* ContextOf(art::Compiler& compiler) { |
| 167 | void *compiler_context = compiler.GetCompilerContext(); |
| 168 | CHECK(compiler_context != NULL); |
| 169 | return reinterpret_cast<art::compiler_llvm::CompilerLLVM*>(compiler_context); |
| 170 | } |
| 171 | |
| 172 | inline static const art::compiler_llvm::CompilerLLVM* ContextOf(const art::Compiler& compiler) { |
| 173 | void *compiler_context = compiler.GetCompilerContext(); |
| 174 | CHECK(compiler_context != NULL); |
| 175 | return reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler_context); |
| 176 | } |
| 177 | |
| 178 | extern "C" void ArtInitCompilerContext(art::Compiler& compiler) { |
| 179 | CHECK(compiler.GetCompilerContext() == NULL); |
| 180 | |
| 181 | art::compiler_llvm::CompilerLLVM* compiler_llvm = |
| 182 | new art::compiler_llvm::CompilerLLVM(&compiler, |
| 183 | compiler.GetInstructionSet()); |
| 184 | |
| 185 | compiler.SetCompilerContext(compiler_llvm); |
| 186 | } |
| 187 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 188 | extern "C" void ArtUnInitCompilerContext(art::Compiler& compiler) { |
| 189 | delete ContextOf(compiler); |
| 190 | compiler.SetCompilerContext(NULL); |
| 191 | } |
| 192 | |
Elliott Hughes | 3fa1b7e | 2012-03-13 17:06:22 -0700 | [diff] [blame] | 193 | extern "C" art::CompiledMethod* ArtCompileMethod(art::Compiler& compiler, |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 194 | const art::DexFile::CodeItem* code_item, |
| 195 | uint32_t access_flags, uint32_t method_idx, |
TDYa127 | 50b69e3 | 2012-06-26 18:11:27 -0700 | [diff] [blame] | 196 | art::ClassLoader* class_loader, |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 197 | const art::DexFile& dex_file) |
| 198 | { |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 199 | art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker(); |
| 200 | art::DexCache *dex_cache = class_linker->FindDexCache(dex_file); |
| 201 | |
| 202 | art::OatCompilationUnit oat_compilation_unit( |
| 203 | class_loader, class_linker, dex_file, *dex_cache, code_item, |
| 204 | method_idx, access_flags); |
TDYa127 | 0200d07 | 2012-04-17 20:55:08 -0700 | [diff] [blame] | 205 | art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler); |
| 206 | art::CompiledMethod* result = compiler_llvm->CompileDexMethod(&oat_compilation_unit); |
TDYa127 | 0200d07 | 2012-04-17 20:55:08 -0700 | [diff] [blame] | 207 | return result; |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | extern "C" art::CompiledMethod* ArtJniCompileMethod(art::Compiler& compiler, |
| 211 | uint32_t access_flags, uint32_t method_idx, |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 212 | const art::DexFile& dex_file) { |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 213 | art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker(); |
| 214 | art::DexCache *dex_cache = class_linker->FindDexCache(dex_file); |
| 215 | |
| 216 | art::OatCompilationUnit oat_compilation_unit( |
Shih-wei Liao | b1ab7df | 2012-03-29 13:53:46 -0700 | [diff] [blame] | 217 | NULL, class_linker, dex_file, *dex_cache, NULL, |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 218 | method_idx, access_flags); |
| 219 | |
Logan Chien | 106b2a0 | 2012-03-18 04:41:38 +0800 | [diff] [blame] | 220 | art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler); |
Elliott Hughes | 6f4976c | 2012-03-13 21:19:01 -0700 | [diff] [blame] | 221 | art::CompiledMethod* result = compiler_llvm->CompileNativeMethod(&oat_compilation_unit); |
Elliott Hughes | 13b835a | 2012-03-13 19:45:22 -0700 | [diff] [blame] | 222 | return result; |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Logan Chien | 7a2a23a | 2012-06-06 11:01:00 +0800 | [diff] [blame] | 225 | extern "C" art::CompiledInvokeStub* ArtCreateInvokeStub(art::Compiler& compiler, |
| 226 | bool is_static, |
| 227 | const char* shorty, |
| 228 | uint32_t shorty_len) { |
TDYa127 | 0200d07 | 2012-04-17 20:55:08 -0700 | [diff] [blame] | 229 | art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler); |
| 230 | art::CompiledInvokeStub* result = compiler_llvm->CreateInvokeStub(is_static, shorty); |
TDYa127 | 0200d07 | 2012-04-17 20:55:08 -0700 | [diff] [blame] | 231 | return result; |
Logan Chien | 106b2a0 | 2012-03-18 04:41:38 +0800 | [diff] [blame] | 232 | } |
| 233 | |
Logan Chien | 7a2a23a | 2012-06-06 11:01:00 +0800 | [diff] [blame] | 234 | extern "C" art::CompiledInvokeStub* ArtCreateProxyStub(art::Compiler& compiler, |
| 235 | const char* shorty, |
| 236 | uint32_t shorty_len) { |
| 237 | art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler); |
| 238 | art::CompiledInvokeStub* result = compiler_llvm->CreateProxyStub(shorty); |
Logan Chien | 7a2a23a | 2012-06-06 11:01:00 +0800 | [diff] [blame] | 239 | return result; |
| 240 | } |
| 241 | |
Logan Chien | 106b2a0 | 2012-03-18 04:41:38 +0800 | [diff] [blame] | 242 | extern "C" void compilerLLVMSetBitcodeFileName(art::Compiler& compiler, |
| 243 | std::string const& filename) { |
| 244 | ContextOf(compiler)->SetBitcodeFileName(filename); |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 245 | } |