blob: 146f577e95cfa2c21ca96904ce62de534fccb2df [file] [log] [blame]
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001/*
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 Chien8b977d32012-02-21 19:14:55 +080019#include "compilation_unit.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080020#include "compiler.h"
21#include "ir_builder.h"
Logan Chien88894ee2012-02-13 16:42:22 +080022#include "jni_compiler.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080023#include "method_compiler.h"
Logan Chien4dd96f52012-02-29 01:26:58 +080024#include "oat_compilation_unit.h"
Logan Chienf04364f2012-02-10 12:01:39 +080025#include "upcall_compiler.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080026
Logan Chiendd7cf5b2012-03-01 12:55:19 +080027#include <llvm/LinkAllPasses.h>
28#include <llvm/LinkAllVMCore.h>
Logan Chien8b977d32012-02-21 19:14:55 +080029#include <llvm/Support/TargetSelect.h>
30#include <llvm/Support/Threading.h>
31
32
33namespace {
34
35pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT;
36
37void 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 Chiendd7cf5b2012-03-01 12:55:19 +080045 // Initialize LLVM optimization passes
46 llvm::PassRegistry &registry = *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 Chien8b977d32012-02-21 19:14:55 +080058 // Initialize LLVM internal data structure for multithreading
59 llvm::llvm_start_multithreaded();
60}
61
62} // anonymous namespace
63
Shih-wei Liaod1fec812012-02-13 09:51:10 -080064
Logan Chien83426162011-12-09 09:29:50 +080065namespace art {
66namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080067
68
Logan Chiene75a8cc2012-02-24 12:26:43 +080069llvm::Module* makeLLVMModuleContents(llvm::Module* module);
Logan Chien42e0e152012-01-13 15:42:36 +080070
71
Shih-wei Liaod1fec812012-02-13 09:51:10 -080072CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set)
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -080073 : compiler_(compiler), compiler_lock_("llvm_compiler_lock"),
74 insn_set_(insn_set), cunit_counter_(0) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080075
Logan Chien8b977d32012-02-21 19:14:55 +080076 // Initialize LLVM libraries
77 pthread_once(&llvm_initialized, InitializeLLVM);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080078}
79
80
81CompilerLLVM::~CompilerLLVM() {
Logan Chien8b977d32012-02-21 19:14:55 +080082 DCHECK(cunit_.get() == NULL);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080083}
84
85
Logan Chien8b977d32012-02-21 19:14:55 +080086void CompilerLLVM::EnsureCompilationUnit() {
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -080087 MutexLock GUARD(compiler_lock_);
Logan Chien8b977d32012-02-21 19:14:55 +080088 DCHECK_NE(llvm_initialized, PTHREAD_ONCE_INIT);
89 if (cunit_.get() == NULL) {
90 cunit_.reset(new CompilationUnit(insn_set_));
91 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -080092}
93
94
Logan Chien8b977d32012-02-21 19:14:55 +080095void CompilerLLVM::MaterializeEveryCompilationUnit() {
96 if (cunit_.get() != NULL) {
97 MaterializeCompilationUnit();
98 }
99}
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800100
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800101
Logan Chien8b977d32012-02-21 19:14:55 +0800102void CompilerLLVM::MaterializeCompilationUnitSafePoint() {
103 if (cunit_->IsMaterializeThresholdReached()) {
104 MaterializeCompilationUnit();
105 }
106}
107
108
109void 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 Liaod1fec812012-02-13 09:51:10 -0800120 }
121
Logan Chien8b977d32012-02-21 19:14:55 +0800122 // Materialize the llvm::Module into ELF object file
123 cunit_->Materialize();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800124
Logan Chien8b977d32012-02-21 19:14:55 +0800125 // Increase compilation unit counter
126 ++cunit_counter_;
127
128 // Delete the compilation unit
129 cunit_.reset(NULL);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800130}
131
132
Logan Chien4dd96f52012-02-29 01:26:58 +0800133CompiledMethod* CompilerLLVM::CompileDexMethod(OatCompilationUnit* oat_compilation_unit) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800134 MutexLock GUARD(compiler_lock_);
135
Logan Chien8b977d32012-02-21 19:14:55 +0800136 EnsureCompilationUnit();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800137
Logan Chien8b977d32012-02-21 19:14:55 +0800138 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 Liaod1fec812012-02-13 09:51:10 -0800146}
Logan Chien83426162011-12-09 09:29:50 +0800147
148
Logan Chien88894ee2012-02-13 16:42:22 +0800149CompiledMethod* CompilerLLVM::CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
150 MutexLock GUARD(compiler_lock_);
151
Logan Chien8b977d32012-02-21 19:14:55 +0800152 EnsureCompilationUnit();
Logan Chien88894ee2012-02-13 16:42:22 +0800153
Logan Chien8b977d32012-02-21 19:14:55 +0800154 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 Chien88894ee2012-02-13 16:42:22 +0800162}
163
164
Logan Chienf04364f2012-02-10 12:01:39 +0800165CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
166 char const *shorty) {
167
168 MutexLock GUARD(compiler_lock_);
169
Logan Chien8b977d32012-02-21 19:14:55 +0800170 EnsureCompilationUnit();
Logan Chienf04364f2012-02-10 12:01:39 +0800171
Logan Chien8b977d32012-02-21 19:14:55 +0800172 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 Chienf04364f2012-02-10 12:01:39 +0800180}
181
182
Logan Chien83426162011-12-09 09:29:50 +0800183} // namespace compiler_llvm
184} // namespace art