blob: 098589459f498e873a8cff9f1c27379e193f6c03 [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 Chien8b977d32012-02-21 19:14:55 +080027#include <llvm/Support/TargetSelect.h>
28#include <llvm/Support/Threading.h>
29
30
31namespace {
32
33pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT;
34
35void 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 Liaod1fec812012-02-13 09:51:10 -080049
Logan Chien83426162011-12-09 09:29:50 +080050namespace art {
51namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080052
53
Logan Chiene75a8cc2012-02-24 12:26:43 +080054llvm::Module* makeLLVMModuleContents(llvm::Module* module);
Logan Chien42e0e152012-01-13 15:42:36 +080055
56
Shih-wei Liaod1fec812012-02-13 09:51:10 -080057CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set)
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -080058 : compiler_(compiler), compiler_lock_("llvm_compiler_lock"),
59 insn_set_(insn_set), cunit_counter_(0) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080060
Logan Chien8b977d32012-02-21 19:14:55 +080061 // Initialize LLVM libraries
62 pthread_once(&llvm_initialized, InitializeLLVM);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080063}
64
65
66CompilerLLVM::~CompilerLLVM() {
Logan Chien8b977d32012-02-21 19:14:55 +080067 DCHECK(cunit_.get() == NULL);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080068}
69
70
Logan Chien8b977d32012-02-21 19:14:55 +080071void CompilerLLVM::EnsureCompilationUnit() {
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -080072 MutexLock GUARD(compiler_lock_);
Logan Chien8b977d32012-02-21 19:14:55 +080073 DCHECK_NE(llvm_initialized, PTHREAD_ONCE_INIT);
74 if (cunit_.get() == NULL) {
75 cunit_.reset(new CompilationUnit(insn_set_));
76 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -080077}
78
79
Logan Chien8b977d32012-02-21 19:14:55 +080080void CompilerLLVM::MaterializeEveryCompilationUnit() {
81 if (cunit_.get() != NULL) {
82 MaterializeCompilationUnit();
83 }
84}
Shih-wei Liaod1fec812012-02-13 09:51:10 -080085
Shih-wei Liaod1fec812012-02-13 09:51:10 -080086
Logan Chien8b977d32012-02-21 19:14:55 +080087void CompilerLLVM::MaterializeCompilationUnitSafePoint() {
88 if (cunit_->IsMaterializeThresholdReached()) {
89 MaterializeCompilationUnit();
90 }
91}
92
93
94void 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 Liaod1fec812012-02-13 09:51:10 -0800105 }
106
Logan Chien8b977d32012-02-21 19:14:55 +0800107 // Materialize the llvm::Module into ELF object file
108 cunit_->Materialize();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800109
Logan Chien8b977d32012-02-21 19:14:55 +0800110 // Increase compilation unit counter
111 ++cunit_counter_;
112
113 // Delete the compilation unit
114 cunit_.reset(NULL);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800115}
116
117
Logan Chien4dd96f52012-02-29 01:26:58 +0800118CompiledMethod* CompilerLLVM::CompileDexMethod(OatCompilationUnit* oat_compilation_unit) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800119 MutexLock GUARD(compiler_lock_);
120
Logan Chien8b977d32012-02-21 19:14:55 +0800121 EnsureCompilationUnit();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800122
Logan Chien8b977d32012-02-21 19:14:55 +0800123 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 Liaod1fec812012-02-13 09:51:10 -0800131}
Logan Chien83426162011-12-09 09:29:50 +0800132
133
Logan Chien88894ee2012-02-13 16:42:22 +0800134CompiledMethod* CompilerLLVM::CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
135 MutexLock GUARD(compiler_lock_);
136
Logan Chien8b977d32012-02-21 19:14:55 +0800137 EnsureCompilationUnit();
Logan Chien88894ee2012-02-13 16:42:22 +0800138
Logan Chien8b977d32012-02-21 19:14:55 +0800139 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 Chien88894ee2012-02-13 16:42:22 +0800147}
148
149
Logan Chienf04364f2012-02-10 12:01:39 +0800150CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
151 char const *shorty) {
152
153 MutexLock GUARD(compiler_lock_);
154
Logan Chien8b977d32012-02-21 19:14:55 +0800155 EnsureCompilationUnit();
Logan Chienf04364f2012-02-10 12:01:39 +0800156
Logan Chien8b977d32012-02-21 19:14:55 +0800157 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 Chienf04364f2012-02-10 12:01:39 +0800165}
166
167
Logan Chien83426162011-12-09 09:29:50 +0800168} // namespace compiler_llvm
169} // namespace art