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 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 19 | #include "compilation_unit.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 20 | #include "compiler.h" |
| 21 | #include "ir_builder.h" |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 22 | #include "jni_compiler.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 23 | #include "method_compiler.h" |
Logan Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 24 | #include "oat_compilation_unit.h" |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 25 | #include "upcall_compiler.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 26 | |
Logan Chien | dd7cf5b | 2012-03-01 12:55:19 +0800 | [diff] [blame] | 27 | #include <llvm/LinkAllPasses.h> |
| 28 | #include <llvm/LinkAllVMCore.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 29 | #include <llvm/Support/TargetSelect.h> |
| 30 | #include <llvm/Support/Threading.h> |
| 31 | |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT; |
| 36 | |
| 37 | void InitializeLLVM() { |
| 38 | // Initialize LLVM target, MC subsystem, asm printer, and asm parser |
| 39 | llvm::InitializeAllTargets(); |
| 40 | llvm::InitializeAllTargetMCs(); |
| 41 | llvm::InitializeAllAsmPrinters(); |
| 42 | llvm::InitializeAllAsmParsers(); |
| 43 | // TODO: Maybe we don't have to initialize "all" targets. |
| 44 | |
Logan Chien | dd7cf5b | 2012-03-01 12:55:19 +0800 | [diff] [blame] | 45 | // Initialize LLVM optimization passes |
| 46 | llvm::PassRegistry ®istry = *llvm::PassRegistry::getPassRegistry(); |
| 47 | |
| 48 | llvm::initializeCore(registry); |
| 49 | llvm::initializeScalarOpts(registry); |
| 50 | llvm::initializeIPO(registry); |
| 51 | llvm::initializeAnalysis(registry); |
| 52 | llvm::initializeIPA(registry); |
| 53 | llvm::initializeTransformUtils(registry); |
| 54 | llvm::initializeInstCombine(registry); |
| 55 | llvm::initializeInstrumentation(registry); |
| 56 | llvm::initializeTarget(registry); |
| 57 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 58 | // Initialize LLVM internal data structure for multithreading |
| 59 | llvm::llvm_start_multithreaded(); |
| 60 | } |
| 61 | |
| 62 | } // anonymous namespace |
| 63 | |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 64 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 65 | namespace art { |
| 66 | namespace compiler_llvm { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 67 | |
| 68 | |
Logan Chien | e75a8cc | 2012-02-24 12:26:43 +0800 | [diff] [blame] | 69 | llvm::Module* makeLLVMModuleContents(llvm::Module* module); |
Logan Chien | 42e0e15 | 2012-01-13 15:42:36 +0800 | [diff] [blame] | 70 | |
| 71 | |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 72 | CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set) |
Shih-wei Liao | 5b8b1ed | 2012-02-23 23:48:21 -0800 | [diff] [blame] | 73 | : compiler_(compiler), compiler_lock_("llvm_compiler_lock"), |
| 74 | insn_set_(insn_set), cunit_counter_(0) { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 75 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 76 | // Initialize LLVM libraries |
| 77 | pthread_once(&llvm_initialized, InitializeLLVM); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | |
| 81 | CompilerLLVM::~CompilerLLVM() { |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 82 | DCHECK(cunit_.get() == NULL); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 86 | void CompilerLLVM::EnsureCompilationUnit() { |
Shih-wei Liao | 5b8b1ed | 2012-02-23 23:48:21 -0800 | [diff] [blame] | 87 | MutexLock GUARD(compiler_lock_); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 88 | DCHECK_NE(llvm_initialized, PTHREAD_ONCE_INIT); |
| 89 | if (cunit_.get() == NULL) { |
| 90 | cunit_.reset(new CompilationUnit(insn_set_)); |
| 91 | } |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 95 | void CompilerLLVM::MaterializeEveryCompilationUnit() { |
| 96 | if (cunit_.get() != NULL) { |
| 97 | MaterializeCompilationUnit(); |
| 98 | } |
| 99 | } |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 100 | |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 101 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 102 | void CompilerLLVM::MaterializeCompilationUnitSafePoint() { |
| 103 | if (cunit_->IsMaterializeThresholdReached()) { |
| 104 | MaterializeCompilationUnit(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
| 109 | void CompilerLLVM::MaterializeCompilationUnit() { |
| 110 | MutexLock GUARD(compiler_lock_); |
| 111 | |
| 112 | cunit_->SetElfFileName(StringPrintf("%s-%u", elf_filename_.c_str(), |
| 113 | cunit_counter_)); |
| 114 | |
| 115 | // Write the translated bitcode for debugging |
| 116 | if (!bitcode_filename_.empty()) { |
| 117 | cunit_->SetBitcodeFileName(StringPrintf("%s-%u", bitcode_filename_.c_str(), |
| 118 | cunit_counter_)); |
| 119 | cunit_->WriteBitcodeToFile(); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 122 | // Materialize the llvm::Module into ELF object file |
| 123 | cunit_->Materialize(); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 124 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 125 | // Increase compilation unit counter |
| 126 | ++cunit_counter_; |
| 127 | |
| 128 | // Delete the compilation unit |
| 129 | cunit_.reset(NULL); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | |
Logan Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 133 | CompiledMethod* CompilerLLVM::CompileDexMethod(OatCompilationUnit* oat_compilation_unit) { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 134 | MutexLock GUARD(compiler_lock_); |
| 135 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 136 | EnsureCompilationUnit(); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 137 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 138 | UniquePtr<MethodCompiler> method_compiler( |
| 139 | new MethodCompiler(cunit_.get(), compiler_, oat_compilation_unit)); |
| 140 | |
| 141 | CompiledMethod* result = method_compiler->Compile(); |
| 142 | |
| 143 | MaterializeCompilationUnitSafePoint(); |
| 144 | |
| 145 | return result; |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 146 | } |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 147 | |
| 148 | |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 149 | CompiledMethod* CompilerLLVM::CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) { |
| 150 | MutexLock GUARD(compiler_lock_); |
| 151 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 152 | EnsureCompilationUnit(); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 153 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 154 | UniquePtr<JniCompiler> jni_compiler( |
| 155 | new JniCompiler(cunit_.get(), *compiler_, oat_compilation_unit)); |
| 156 | |
| 157 | CompiledMethod* result = jni_compiler->Compile(); |
| 158 | |
| 159 | MaterializeCompilationUnitSafePoint(); |
| 160 | |
| 161 | return result; |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 165 | CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static, |
| 166 | char const *shorty) { |
| 167 | |
| 168 | MutexLock GUARD(compiler_lock_); |
| 169 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 170 | EnsureCompilationUnit(); |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 171 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 172 | UniquePtr<UpcallCompiler> upcall_compiler( |
| 173 | new UpcallCompiler(cunit_.get(), *compiler_)); |
| 174 | |
| 175 | CompiledInvokeStub* result = upcall_compiler->CreateStub(is_static, shorty); |
| 176 | |
| 177 | MaterializeCompilationUnitSafePoint(); |
| 178 | |
| 179 | return result; |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 183 | } // namespace compiler_llvm |
| 184 | } // namespace art |