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 | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 19 | #include "class_linker.h" |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 20 | #include "compilation_unit.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 21 | #include "compiler.h" |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 22 | #include "dex_cache.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 23 | #include "ir_builder.h" |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 24 | #include "jni_compiler.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 25 | #include "method_compiler.h" |
Logan Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 26 | #include "oat_compilation_unit.h" |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 27 | #include "stl_util.h" |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 28 | #include "upcall_compiler.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 29 | |
Logan Chien | dd7cf5b | 2012-03-01 12:55:19 +0800 | [diff] [blame] | 30 | #include <llvm/LinkAllPasses.h> |
| 31 | #include <llvm/LinkAllVMCore.h> |
Logan Chien | 4c17dff | 2012-03-02 20:15:46 +0800 | [diff] [blame] | 32 | #include <llvm/Support/CommandLine.h> |
Logan Chien | 013b6f2 | 2012-03-02 17:20:33 +0800 | [diff] [blame] | 33 | #include <llvm/Support/ManagedStatic.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 34 | #include <llvm/Support/TargetSelect.h> |
| 35 | #include <llvm/Support/Threading.h> |
| 36 | |
Logan Chien | 013b6f2 | 2012-03-02 17:20:33 +0800 | [diff] [blame] | 37 | namespace llvm { |
| 38 | extern bool TimePassesIsEnabled; |
| 39 | } |
| 40 | |
Logan Chien | 4c17dff | 2012-03-02 20:15:46 +0800 | [diff] [blame] | 41 | extern llvm::cl::opt<bool> EnableARMLongCalls; |
| 42 | // NOTE: Although EnableARMLongCalls is defined in llvm/lib/Target/ARM/ |
| 43 | // ARMISelLowering.cpp, however, it is not in the llvm namespace. |
| 44 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 45 | |
Shih-wei Liao | fc34adb | 2012-03-07 08:51:44 -0800 | [diff] [blame] | 46 | namespace { |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 47 | |
Shih-wei Liao | fc34adb | 2012-03-07 08:51:44 -0800 | [diff] [blame] | 48 | pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT; |
| 49 | |
| 50 | void InitializeLLVM() { |
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 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 54 | // Initialize LLVM target, MC subsystem, asm printer, and asm parser |
| 55 | llvm::InitializeAllTargets(); |
| 56 | llvm::InitializeAllTargetMCs(); |
| 57 | llvm::InitializeAllAsmPrinters(); |
| 58 | llvm::InitializeAllAsmParsers(); |
| 59 | // TODO: Maybe we don't have to initialize "all" targets. |
| 60 | |
Logan Chien | 4c17dff | 2012-03-02 20:15:46 +0800 | [diff] [blame] | 61 | // Enable -arm-long-calls |
| 62 | EnableARMLongCalls = true; |
| 63 | |
Logan Chien | dd7cf5b | 2012-03-01 12:55:19 +0800 | [diff] [blame] | 64 | // Initialize LLVM optimization passes |
| 65 | llvm::PassRegistry ®istry = *llvm::PassRegistry::getPassRegistry(); |
| 66 | |
| 67 | llvm::initializeCore(registry); |
| 68 | llvm::initializeScalarOpts(registry); |
| 69 | llvm::initializeIPO(registry); |
| 70 | llvm::initializeAnalysis(registry); |
| 71 | llvm::initializeIPA(registry); |
| 72 | llvm::initializeTransformUtils(registry); |
| 73 | llvm::initializeInstCombine(registry); |
| 74 | llvm::initializeInstrumentation(registry); |
| 75 | llvm::initializeTarget(registry); |
| 76 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 77 | // Initialize LLVM internal data structure for multithreading |
| 78 | llvm::llvm_start_multithreaded(); |
| 79 | } |
| 80 | |
Shih-wei Liao | 63deaad | 2012-03-06 17:26:37 -0800 | [diff] [blame] | 81 | // Singleton. Otherwise, multiple CompilerLLVM instances may cause crashes if |
| 82 | // one shuts down prematurely. |
Shih-wei Liao | fc34adb | 2012-03-07 08:51:44 -0800 | [diff] [blame] | 83 | llvm::llvm_shutdown_obj llvm_guard; |
| 84 | |
| 85 | } // anonymous namespace |
| 86 | |
| 87 | |
| 88 | namespace art { |
| 89 | namespace compiler_llvm { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 90 | |
| 91 | |
Logan Chien | e75a8cc | 2012-02-24 12:26:43 +0800 | [diff] [blame] | 92 | llvm::Module* makeLLVMModuleContents(llvm::Module* module); |
Logan Chien | 42e0e15 | 2012-01-13 15:42:36 +0800 | [diff] [blame] | 93 | |
| 94 | |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 95 | CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set) |
Shih-wei Liao | 5b8b1ed | 2012-02-23 23:48:21 -0800 | [diff] [blame] | 96 | : compiler_(compiler), compiler_lock_("llvm_compiler_lock"), |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 97 | insn_set_(insn_set), curr_cunit_(NULL) { |
Shih-wei Liao | fc34adb | 2012-03-07 08:51:44 -0800 | [diff] [blame] | 98 | |
| 99 | |
| 100 | // Initialize LLVM libraries |
| 101 | pthread_once(&llvm_initialized, InitializeLLVM); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | |
| 105 | CompilerLLVM::~CompilerLLVM() { |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 106 | STLDeleteElements(&cunits_); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | |
Logan Chien | ce11906 | 2012-03-02 11:57:34 +0800 | [diff] [blame] | 110 | void CompilerLLVM::EnsureCompilationUnit() { |
Logan Chien | ce11906 | 2012-03-02 11:57:34 +0800 | [diff] [blame] | 111 | compiler_lock_.AssertHeld(); |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 112 | |
| 113 | if (curr_cunit_ != NULL) { |
| 114 | return; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 115 | } |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 116 | |
| 117 | // Allocate compilation unit |
| 118 | size_t cunit_idx = cunits_.size(); |
| 119 | |
| 120 | curr_cunit_ = new CompilationUnit(insn_set_); |
| 121 | |
| 122 | // Setup output filename |
| 123 | curr_cunit_->SetElfFileName( |
| 124 | StringPrintf("%s-%zu", elf_filename_.c_str(), cunit_idx)); |
| 125 | |
| 126 | if (IsBitcodeFileNameAvailable()) { |
| 127 | curr_cunit_->SetBitcodeFileName( |
| 128 | StringPrintf("%s-%zu", bitcode_filename_.c_str(), cunit_idx)); |
| 129 | } |
| 130 | |
| 131 | // Register compilation unit |
| 132 | cunits_.push_back(curr_cunit_); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 136 | void CompilerLLVM::MaterializeRemainder() { |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 137 | MutexLock GUARD(compiler_lock_); |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 138 | if (curr_cunit_ != NULL) { |
Logan Chien | ce11906 | 2012-03-02 11:57:34 +0800 | [diff] [blame] | 139 | Materialize(); |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 140 | } |
| 141 | } |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 142 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 143 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 144 | void CompilerLLVM::MaterializeIfThresholdReached() { |
| 145 | MutexLock GUARD(compiler_lock_); |
| 146 | if (curr_cunit_ != NULL && curr_cunit_->IsMaterializeThresholdReached()) { |
Logan Chien | ce11906 | 2012-03-02 11:57:34 +0800 | [diff] [blame] | 147 | Materialize(); |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | |
Logan Chien | ce11906 | 2012-03-02 11:57:34 +0800 | [diff] [blame] | 152 | void CompilerLLVM::Materialize() { |
| 153 | compiler_lock_.AssertHeld(); |
| 154 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 155 | DCHECK(curr_cunit_ != NULL); |
| 156 | DCHECK(!curr_cunit_->IsMaterialized()); |
| 157 | |
| 158 | // Write bitcode to file when filename is set |
| 159 | if (IsBitcodeFileNameAvailable()) { |
| 160 | curr_cunit_->WriteBitcodeToFile(); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 163 | // Materialize the llvm::Module into ELF object file |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 164 | curr_cunit_->Materialize(); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 165 | |
| 166 | // Delete the compilation unit |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 167 | curr_cunit_ = NULL; |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 171 | CompiledMethod* CompilerLLVM:: |
| 172 | CompileDexMethod(OatCompilationUnit* oat_compilation_unit) { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 173 | MutexLock GUARD(compiler_lock_); |
| 174 | |
Logan Chien | ce11906 | 2012-03-02 11:57:34 +0800 | [diff] [blame] | 175 | EnsureCompilationUnit(); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 176 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 177 | UniquePtr<MethodCompiler> method_compiler( |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 178 | new MethodCompiler(curr_cunit_, compiler_, oat_compilation_unit)); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 179 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 180 | return method_compiler->Compile(); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 181 | } |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 182 | |
| 183 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 184 | CompiledMethod* CompilerLLVM:: |
| 185 | CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) { |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 186 | MutexLock GUARD(compiler_lock_); |
| 187 | |
Logan Chien | ce11906 | 2012-03-02 11:57:34 +0800 | [diff] [blame] | 188 | EnsureCompilationUnit(); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 189 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 190 | UniquePtr<JniCompiler> jni_compiler( |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 191 | new JniCompiler(curr_cunit_, *compiler_, oat_compilation_unit)); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 192 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 193 | return jni_compiler->Compile(); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 197 | CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static, |
| 198 | char const *shorty) { |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 199 | MutexLock GUARD(compiler_lock_); |
| 200 | |
Logan Chien | ce11906 | 2012-03-02 11:57:34 +0800 | [diff] [blame] | 201 | EnsureCompilationUnit(); |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 202 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 203 | UniquePtr<UpcallCompiler> upcall_compiler( |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 204 | new UpcallCompiler(curr_cunit_, *compiler_)); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 205 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 206 | return upcall_compiler->CreateStub(is_static, shorty); |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 207 | } |
| 208 | |
Elliott Hughes | 6f4976c | 2012-03-13 21:19:01 -0700 | [diff] [blame] | 209 | CompilerLLVM* EnsureCompilerLLVM(art::Compiler& compiler) { |
| 210 | if (compiler.GetCompilerContext() == NULL) { |
| 211 | compiler.SetCompilerContext(new CompilerLLVM(&compiler, compiler.GetInstructionSet())); |
| 212 | } |
| 213 | CompilerLLVM* compiler_llvm = reinterpret_cast<CompilerLLVM*>(compiler.GetCompilerContext()); |
| 214 | compiler_llvm->SetElfFileName(compiler.GetElfFileName()); |
| 215 | compiler_llvm->SetBitcodeFileName(compiler.GetBitcodeFileName()); |
| 216 | return compiler_llvm; |
| 217 | } |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 218 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 219 | } // namespace compiler_llvm |
| 220 | } // namespace art |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 221 | |
Elliott Hughes | 3fa1b7e | 2012-03-13 17:06:22 -0700 | [diff] [blame] | 222 | extern "C" art::CompiledMethod* ArtCompileMethod(art::Compiler& compiler, |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 223 | const art::DexFile::CodeItem* code_item, |
| 224 | uint32_t access_flags, uint32_t method_idx, |
| 225 | const art::ClassLoader* class_loader, |
| 226 | const art::DexFile& dex_file) |
| 227 | { |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 228 | art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker(); |
| 229 | art::DexCache *dex_cache = class_linker->FindDexCache(dex_file); |
| 230 | |
| 231 | art::OatCompilationUnit oat_compilation_unit( |
| 232 | class_loader, class_linker, dex_file, *dex_cache, code_item, |
| 233 | method_idx, access_flags); |
| 234 | |
Elliott Hughes | 6f4976c | 2012-03-13 21:19:01 -0700 | [diff] [blame] | 235 | return art::compiler_llvm::EnsureCompilerLLVM(compiler)->CompileDexMethod(&oat_compilation_unit); |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | extern "C" art::CompiledMethod* ArtJniCompileMethod(art::Compiler& compiler, |
| 239 | uint32_t access_flags, uint32_t method_idx, |
| 240 | const art::ClassLoader* class_loader, |
| 241 | const art::DexFile& dex_file) { |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 242 | art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker(); |
| 243 | art::DexCache *dex_cache = class_linker->FindDexCache(dex_file); |
| 244 | |
| 245 | art::OatCompilationUnit oat_compilation_unit( |
| 246 | class_loader, class_linker, dex_file, *dex_cache, NULL, |
| 247 | method_idx, access_flags); |
| 248 | |
Elliott Hughes | 6f4976c | 2012-03-13 21:19:01 -0700 | [diff] [blame] | 249 | art::compiler_llvm::CompilerLLVM* compiler_llvm = art::compiler_llvm::EnsureCompilerLLVM(compiler); |
| 250 | art::CompiledMethod* result = compiler_llvm->CompileNativeMethod(&oat_compilation_unit); |
| 251 | compiler_llvm->MaterializeIfThresholdReached(); |
Elliott Hughes | 13b835a | 2012-03-13 19:45:22 -0700 | [diff] [blame] | 252 | return result; |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | extern "C" art::CompiledInvokeStub* ArtCreateInvokeStub(art::Compiler& compiler, bool is_static, |
| 256 | const char* shorty, uint32_t shorty_len) { |
Elliott Hughes | 6f4976c | 2012-03-13 21:19:01 -0700 | [diff] [blame] | 257 | return art::compiler_llvm::EnsureCompilerLLVM(compiler)->CreateInvokeStub(is_static, shorty); |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | extern "C" void compilerLLVMMaterializeRemainder(art::Compiler& compiler) { |
Elliott Hughes | 6f4976c | 2012-03-13 21:19:01 -0700 | [diff] [blame] | 261 | art::compiler_llvm::EnsureCompilerLLVM(compiler)->MaterializeRemainder(); |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 264 | // Note: Using this function carefully!!! This is temporary solution, we will remove it. |
| 265 | extern "C" art::MutexLock* compilerLLVMMutexLock(art::Compiler& compiler) { |
Elliott Hughes | 6f4976c | 2012-03-13 21:19:01 -0700 | [diff] [blame] | 266 | return new art::MutexLock(art::compiler_llvm::EnsureCompilerLLVM(compiler)->compiler_lock_); |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | extern "C" void compilerLLVMDispose(art::Compiler& compiler) { |
Elliott Hughes | 6f4976c | 2012-03-13 21:19:01 -0700 | [diff] [blame] | 270 | delete art::compiler_llvm::EnsureCompilerLLVM(compiler); |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 271 | } |