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