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