Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [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 | |
| 19 | #include "backend_options.h" |
| 20 | #include "base/stl_util.h" |
| 21 | #include "class_linker.h" |
| 22 | #include "compiled_method.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 23 | #include "dex/verified_methods_data.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 24 | #include "driver/compiler_driver.h" |
| 25 | #include "driver/dex_compilation_unit.h" |
| 26 | #include "globals.h" |
| 27 | #include "ir_builder.h" |
| 28 | #include "jni/portable/jni_compiler.h" |
| 29 | #include "llvm_compilation_unit.h" |
Ian Rogers | 02ed4c0 | 2013-09-06 13:10:04 -0700 | [diff] [blame] | 30 | #include "thread-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 31 | #include "utils_llvm.h" |
| 32 | #include "verifier/method_verifier.h" |
| 33 | |
| 34 | #include <llvm/LinkAllPasses.h> |
| 35 | #include <llvm/Support/ManagedStatic.h> |
| 36 | #include <llvm/Support/TargetSelect.h> |
| 37 | #include <llvm/Support/Threading.h> |
| 38 | |
| 39 | namespace art { |
| 40 | void CompileOneMethod(CompilerDriver& driver, |
| 41 | const CompilerBackend compilerBackend, |
| 42 | const DexFile::CodeItem* code_item, |
| 43 | uint32_t access_flags, InvokeType invoke_type, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 44 | uint16_t class_def_idx, uint32_t method_idx, jobject class_loader, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 45 | const DexFile& dex_file, |
| 46 | llvm::LlvmCompilationUnit* llvm_info); |
| 47 | } |
| 48 | |
| 49 | namespace llvm { |
| 50 | extern bool TimePassesIsEnabled; |
| 51 | } |
| 52 | |
| 53 | namespace { |
| 54 | |
| 55 | pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT; |
| 56 | |
| 57 | void InitializeLLVM() { |
| 58 | // Initialize LLVM internal data structure for multithreading |
| 59 | llvm::llvm_start_multithreaded(); |
| 60 | |
| 61 | // NOTE: Uncomment following line to show the time consumption of LLVM passes |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 62 | // llvm::TimePassesIsEnabled = true; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 63 | |
| 64 | // Initialize LLVM target-specific options. |
| 65 | art::llvm::InitialBackendOptions(); |
| 66 | |
| 67 | // Initialize LLVM target, MC subsystem, asm printer, and asm parser. |
| 68 | if (art::kIsTargetBuild) { |
| 69 | // Don't initialize all targets on device. Just initialize the device's native target |
| 70 | llvm::InitializeNativeTarget(); |
| 71 | llvm::InitializeNativeTargetAsmPrinter(); |
| 72 | llvm::InitializeNativeTargetAsmParser(); |
| 73 | } else { |
| 74 | llvm::InitializeAllTargets(); |
| 75 | llvm::InitializeAllTargetMCs(); |
| 76 | llvm::InitializeAllAsmPrinters(); |
| 77 | llvm::InitializeAllAsmParsers(); |
| 78 | } |
| 79 | |
| 80 | // Initialize LLVM optimization passes |
| 81 | llvm::PassRegistry ®istry = *llvm::PassRegistry::getPassRegistry(); |
| 82 | |
| 83 | llvm::initializeCore(registry); |
| 84 | llvm::initializeScalarOpts(registry); |
| 85 | llvm::initializeIPO(registry); |
| 86 | llvm::initializeAnalysis(registry); |
| 87 | llvm::initializeIPA(registry); |
| 88 | llvm::initializeTransformUtils(registry); |
| 89 | llvm::initializeInstCombine(registry); |
| 90 | llvm::initializeInstrumentation(registry); |
| 91 | llvm::initializeTarget(registry); |
| 92 | } |
| 93 | |
| 94 | // The Guard to Shutdown LLVM |
| 95 | // llvm::llvm_shutdown_obj llvm_guard; |
| 96 | // TODO: We are commenting out this line because this will cause SEGV from |
| 97 | // time to time. |
| 98 | // Two reasons: (1) the order of the destruction of static objects, or |
| 99 | // (2) dlopen/dlclose side-effect on static objects. |
| 100 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 101 | } // anonymous namespace |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 102 | |
| 103 | |
| 104 | namespace art { |
| 105 | namespace llvm { |
| 106 | |
| 107 | |
| 108 | ::llvm::Module* makeLLVMModuleContents(::llvm::Module* module); |
| 109 | |
| 110 | |
| 111 | CompilerLLVM::CompilerLLVM(CompilerDriver* driver, InstructionSet insn_set) |
| 112 | : compiler_driver_(driver), insn_set_(insn_set), |
| 113 | next_cunit_id_lock_("compilation unit id lock"), next_cunit_id_(1) { |
| 114 | |
| 115 | // Initialize LLVM libraries |
| 116 | pthread_once(&llvm_initialized, InitializeLLVM); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | CompilerLLVM::~CompilerLLVM() { |
| 121 | } |
| 122 | |
| 123 | |
| 124 | LlvmCompilationUnit* CompilerLLVM::AllocateCompilationUnit() { |
| 125 | MutexLock GUARD(Thread::Current(), next_cunit_id_lock_); |
| 126 | LlvmCompilationUnit* cunit = new LlvmCompilationUnit(this, next_cunit_id_++); |
| 127 | if (!bitcode_filename_.empty()) { |
| 128 | cunit->SetBitcodeFileName(StringPrintf("%s-%zu", |
| 129 | bitcode_filename_.c_str(), |
| 130 | cunit->GetCompilationUnitId())); |
| 131 | } |
| 132 | return cunit; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | CompiledMethod* CompilerLLVM:: |
| 137 | CompileDexMethod(DexCompilationUnit* dex_compilation_unit, InvokeType invoke_type) { |
| 138 | UniquePtr<LlvmCompilationUnit> cunit(AllocateCompilationUnit()); |
| 139 | |
| 140 | cunit->SetDexCompilationUnit(dex_compilation_unit); |
| 141 | cunit->SetCompilerDriver(compiler_driver_); |
| 142 | // TODO: consolidate ArtCompileMethods |
| 143 | CompileOneMethod(*compiler_driver_, |
| 144 | kPortable, |
| 145 | dex_compilation_unit->GetCodeItem(), |
| 146 | dex_compilation_unit->GetAccessFlags(), |
| 147 | invoke_type, |
| 148 | dex_compilation_unit->GetClassDefIndex(), |
| 149 | dex_compilation_unit->GetDexMethodIndex(), |
| 150 | dex_compilation_unit->GetClassLoader(), |
| 151 | *dex_compilation_unit->GetDexFile(), |
| 152 | cunit.get()); |
| 153 | |
| 154 | cunit->Materialize(); |
| 155 | |
| 156 | MethodReference mref(dex_compilation_unit->GetDexFile(), |
| 157 | dex_compilation_unit->GetDexMethodIndex()); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 158 | return new CompiledMethod(*compiler_driver_, compiler_driver_->GetInstructionSet(), |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 159 | cunit->GetElfObject(), |
| 160 | *compiler_driver_->GetVerifiedMethodsData()->GetDexGcMap(mref), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 161 | cunit->GetDexCompilationUnit()->GetSymbol()); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | CompiledMethod* CompilerLLVM:: |
| 166 | CompileNativeMethod(DexCompilationUnit* dex_compilation_unit) { |
| 167 | UniquePtr<LlvmCompilationUnit> cunit(AllocateCompilationUnit()); |
| 168 | |
| 169 | UniquePtr<JniCompiler> jni_compiler( |
Ian Rogers | d133b97 | 2013-09-05 11:01:30 -0700 | [diff] [blame] | 170 | new JniCompiler(cunit.get(), compiler_driver_, dex_compilation_unit)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 171 | |
| 172 | return jni_compiler->Compile(); |
| 173 | } |
| 174 | |
| 175 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 176 | } // namespace llvm |
| 177 | } // namespace art |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 178 | |
| 179 | inline static art::llvm::CompilerLLVM* ContextOf(art::CompilerDriver& driver) { |
| 180 | void *compiler_context = driver.GetCompilerContext(); |
| 181 | CHECK(compiler_context != NULL); |
| 182 | return reinterpret_cast<art::llvm::CompilerLLVM*>(compiler_context); |
| 183 | } |
| 184 | |
| 185 | inline static const art::llvm::CompilerLLVM* ContextOf(const art::CompilerDriver& driver) { |
| 186 | void *compiler_context = driver.GetCompilerContext(); |
| 187 | CHECK(compiler_context != NULL); |
| 188 | return reinterpret_cast<const art::llvm::CompilerLLVM*>(compiler_context); |
| 189 | } |
| 190 | |
| 191 | extern "C" void ArtInitCompilerContext(art::CompilerDriver& driver) { |
| 192 | CHECK(driver.GetCompilerContext() == NULL); |
| 193 | |
| 194 | art::llvm::CompilerLLVM* compiler_llvm = new art::llvm::CompilerLLVM(&driver, |
| 195 | driver.GetInstructionSet()); |
| 196 | |
| 197 | driver.SetCompilerContext(compiler_llvm); |
| 198 | } |
| 199 | |
| 200 | extern "C" void ArtUnInitCompilerContext(art::CompilerDriver& driver) { |
| 201 | delete ContextOf(driver); |
| 202 | driver.SetCompilerContext(NULL); |
| 203 | } |
| 204 | extern "C" art::CompiledMethod* ArtCompileMethod(art::CompilerDriver& driver, |
| 205 | const art::DexFile::CodeItem* code_item, |
| 206 | uint32_t access_flags, |
| 207 | art::InvokeType invoke_type, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 208 | uint16_t class_def_idx, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 209 | uint32_t method_idx, |
| 210 | jobject class_loader, |
| 211 | const art::DexFile& dex_file) { |
| 212 | UNUSED(class_def_idx); // TODO: this is used with Compiler::RequiresConstructorBarrier. |
| 213 | art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker(); |
| 214 | |
| 215 | art::DexCompilationUnit dex_compilation_unit( |
| 216 | NULL, class_loader, class_linker, dex_file, code_item, |
| 217 | class_def_idx, method_idx, access_flags); |
| 218 | art::llvm::CompilerLLVM* compiler_llvm = ContextOf(driver); |
| 219 | art::CompiledMethod* result = compiler_llvm->CompileDexMethod(&dex_compilation_unit, invoke_type); |
| 220 | return result; |
| 221 | } |
| 222 | |
| 223 | extern "C" art::CompiledMethod* ArtLLVMJniCompileMethod(art::CompilerDriver& driver, |
| 224 | uint32_t access_flags, uint32_t method_idx, |
| 225 | const art::DexFile& dex_file) { |
| 226 | art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker(); |
| 227 | |
| 228 | art::DexCompilationUnit dex_compilation_unit( |
| 229 | NULL, NULL, class_linker, dex_file, NULL, |
| 230 | 0, method_idx, access_flags); |
| 231 | |
| 232 | art::llvm::CompilerLLVM* compiler_llvm = ContextOf(driver); |
| 233 | art::CompiledMethod* result = compiler_llvm->CompileNativeMethod(&dex_compilation_unit); |
| 234 | return result; |
| 235 | } |
| 236 | |
| 237 | extern "C" void compilerLLVMSetBitcodeFileName(art::CompilerDriver& driver, |
| 238 | std::string const& filename) { |
| 239 | ContextOf(driver)->SetBitcodeFileName(filename); |
| 240 | } |