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