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