blob: 56f0b074184968362e68641f2f7713ffdd487ba9 [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)
58: compiler_(compiler), compiler_lock_("llvm_compiler_lock"),
Logan Chien8b977d32012-02-21 19:14:55 +080059 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() {
72 DCHECK_NE(llvm_initialized, PTHREAD_ONCE_INIT);
73 if (cunit_.get() == NULL) {
74 cunit_.reset(new CompilationUnit(insn_set_));
75 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -080076}
77
78
Logan Chien8b977d32012-02-21 19:14:55 +080079void CompilerLLVM::MaterializeEveryCompilationUnit() {
80 if (cunit_.get() != NULL) {
81 MaterializeCompilationUnit();
82 }
83}
Shih-wei Liaod1fec812012-02-13 09:51:10 -080084
Shih-wei Liaod1fec812012-02-13 09:51:10 -080085
Logan Chien8b977d32012-02-21 19:14:55 +080086void CompilerLLVM::MaterializeCompilationUnitSafePoint() {
87 if (cunit_->IsMaterializeThresholdReached()) {
88 MaterializeCompilationUnit();
89 }
90}
91
92
93void CompilerLLVM::MaterializeCompilationUnit() {
94 MutexLock GUARD(compiler_lock_);
95
96 cunit_->SetElfFileName(StringPrintf("%s-%u", elf_filename_.c_str(),
97 cunit_counter_));
98
99 // Write the translated bitcode for debugging
100 if (!bitcode_filename_.empty()) {
101 cunit_->SetBitcodeFileName(StringPrintf("%s-%u", bitcode_filename_.c_str(),
102 cunit_counter_));
103 cunit_->WriteBitcodeToFile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800104 }
105
Logan Chien8b977d32012-02-21 19:14:55 +0800106 // Materialize the llvm::Module into ELF object file
107 cunit_->Materialize();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800108
Logan Chien8b977d32012-02-21 19:14:55 +0800109 // Increase compilation unit counter
110 ++cunit_counter_;
111
112 // Delete the compilation unit
113 cunit_.reset(NULL);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800114}
115
116
Logan Chien4dd96f52012-02-29 01:26:58 +0800117CompiledMethod* CompilerLLVM::CompileDexMethod(OatCompilationUnit* oat_compilation_unit) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800118 MutexLock GUARD(compiler_lock_);
119
Logan Chien8b977d32012-02-21 19:14:55 +0800120 EnsureCompilationUnit();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800121
Logan Chien8b977d32012-02-21 19:14:55 +0800122 UniquePtr<MethodCompiler> method_compiler(
123 new MethodCompiler(cunit_.get(), compiler_, oat_compilation_unit));
124
125 CompiledMethod* result = method_compiler->Compile();
126
127 MaterializeCompilationUnitSafePoint();
128
129 return result;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800130}
Logan Chien83426162011-12-09 09:29:50 +0800131
132
Logan Chien88894ee2012-02-13 16:42:22 +0800133CompiledMethod* CompilerLLVM::CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
134 MutexLock GUARD(compiler_lock_);
135
Logan Chien8b977d32012-02-21 19:14:55 +0800136 EnsureCompilationUnit();
Logan Chien88894ee2012-02-13 16:42:22 +0800137
Logan Chien8b977d32012-02-21 19:14:55 +0800138 UniquePtr<JniCompiler> jni_compiler(
139 new JniCompiler(cunit_.get(), *compiler_, oat_compilation_unit));
140
141 CompiledMethod* result = jni_compiler->Compile();
142
143 MaterializeCompilationUnitSafePoint();
144
145 return result;
Logan Chien88894ee2012-02-13 16:42:22 +0800146}
147
148
Logan Chienf04364f2012-02-10 12:01:39 +0800149CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
150 char const *shorty) {
151
152 MutexLock GUARD(compiler_lock_);
153
Logan Chien8b977d32012-02-21 19:14:55 +0800154 EnsureCompilationUnit();
Logan Chienf04364f2012-02-10 12:01:39 +0800155
Logan Chien8b977d32012-02-21 19:14:55 +0800156 UniquePtr<UpcallCompiler> upcall_compiler(
157 new UpcallCompiler(cunit_.get(), *compiler_));
158
159 CompiledInvokeStub* result = upcall_compiler->CreateStub(is_static, shorty);
160
161 MaterializeCompilationUnitSafePoint();
162
163 return result;
Logan Chienf04364f2012-02-10 12:01:39 +0800164}
165
166
Logan Chien83426162011-12-09 09:29:50 +0800167} // namespace compiler_llvm
168} // namespace art