blob: 345872e3ee7760723dbaca8051681ffe6cae836c [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 Chien7f767612012-03-01 18:54:49 +080025#include "stl_util.h"
Logan Chienf04364f2012-02-10 12:01:39 +080026#include "upcall_compiler.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080027
Logan Chiendd7cf5b2012-03-01 12:55:19 +080028#include <llvm/LinkAllPasses.h>
29#include <llvm/LinkAllVMCore.h>
Logan Chien8b977d32012-02-21 19:14:55 +080030#include <llvm/Support/TargetSelect.h>
31#include <llvm/Support/Threading.h>
32
33
34namespace {
35
36pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT;
37
38void 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 Chiendd7cf5b2012-03-01 12:55:19 +080046 // Initialize LLVM optimization passes
47 llvm::PassRegistry &registry = *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 Chien8b977d32012-02-21 19:14:55 +080059 // Initialize LLVM internal data structure for multithreading
60 llvm::llvm_start_multithreaded();
61}
62
63} // anonymous namespace
64
Shih-wei Liaod1fec812012-02-13 09:51:10 -080065
Logan Chien83426162011-12-09 09:29:50 +080066namespace art {
67namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080068
69
Logan Chiene75a8cc2012-02-24 12:26:43 +080070llvm::Module* makeLLVMModuleContents(llvm::Module* module);
Logan Chien42e0e152012-01-13 15:42:36 +080071
72
Shih-wei Liaod1fec812012-02-13 09:51:10 -080073CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set)
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -080074 : compiler_(compiler), compiler_lock_("llvm_compiler_lock"),
Logan Chien7f767612012-03-01 18:54:49 +080075 insn_set_(insn_set), curr_cunit_(NULL) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080076
Logan Chien8b977d32012-02-21 19:14:55 +080077 // Initialize LLVM libraries
78 pthread_once(&llvm_initialized, InitializeLLVM);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080079}
80
81
82CompilerLLVM::~CompilerLLVM() {
Logan Chien7f767612012-03-01 18:54:49 +080083 STLDeleteElements(&cunits_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080084}
85
86
Logan Chien7f767612012-03-01 18:54:49 +080087void CompilerLLVM::EnsureCompilationUnit(MutexLock& GUARD) {
Logan Chien8b977d32012-02-21 19:14:55 +080088 DCHECK_NE(llvm_initialized, PTHREAD_ONCE_INIT);
Logan Chien7f767612012-03-01 18:54:49 +080089
90 if (curr_cunit_ != NULL) {
91 return;
Logan Chien8b977d32012-02-21 19:14:55 +080092 }
Logan Chien7f767612012-03-01 18:54:49 +080093
94 // Allocate compilation unit
95 size_t cunit_idx = cunits_.size();
96
97 curr_cunit_ = new CompilationUnit(insn_set_);
98
99 // Setup output filename
100 curr_cunit_->SetElfFileName(
101 StringPrintf("%s-%zu", elf_filename_.c_str(), cunit_idx));
102
103 if (IsBitcodeFileNameAvailable()) {
104 curr_cunit_->SetBitcodeFileName(
105 StringPrintf("%s-%zu", bitcode_filename_.c_str(), cunit_idx));
106 }
107
108 // Register compilation unit
109 cunits_.push_back(curr_cunit_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800110}
111
112
Logan Chien7f767612012-03-01 18:54:49 +0800113void CompilerLLVM::MaterializeRemainder() {
Logan Chien8b977d32012-02-21 19:14:55 +0800114 MutexLock GUARD(compiler_lock_);
Logan Chien7f767612012-03-01 18:54:49 +0800115 if (curr_cunit_ != NULL) {
116 Materialize(GUARD);
117 }
118}
Logan Chien8b977d32012-02-21 19:14:55 +0800119
Logan Chien8b977d32012-02-21 19:14:55 +0800120
Logan Chien7f767612012-03-01 18:54:49 +0800121void CompilerLLVM::MaterializeIfThresholdReached() {
122 MutexLock GUARD(compiler_lock_);
123 if (curr_cunit_ != NULL && curr_cunit_->IsMaterializeThresholdReached()) {
124 Materialize(GUARD);
125 }
126}
127
128
129void CompilerLLVM::Materialize(MutexLock& GUARD) {
130 DCHECK(curr_cunit_ != NULL);
131 DCHECK(!curr_cunit_->IsMaterialized());
132
133 // Write bitcode to file when filename is set
134 if (IsBitcodeFileNameAvailable()) {
135 curr_cunit_->WriteBitcodeToFile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800136 }
137
Logan Chien8b977d32012-02-21 19:14:55 +0800138 // Materialize the llvm::Module into ELF object file
Logan Chien7f767612012-03-01 18:54:49 +0800139 curr_cunit_->Materialize();
Logan Chien8b977d32012-02-21 19:14:55 +0800140
141 // Delete the compilation unit
Logan Chien7f767612012-03-01 18:54:49 +0800142 curr_cunit_ = NULL;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800143}
144
145
Logan Chien7f767612012-03-01 18:54:49 +0800146CompiledMethod* CompilerLLVM::
147CompileDexMethod(OatCompilationUnit* oat_compilation_unit) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800148 MutexLock GUARD(compiler_lock_);
149
Logan Chien7f767612012-03-01 18:54:49 +0800150 EnsureCompilationUnit(GUARD);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800151
Logan Chien8b977d32012-02-21 19:14:55 +0800152 UniquePtr<MethodCompiler> method_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800153 new MethodCompiler(curr_cunit_, compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800154
Logan Chien7f767612012-03-01 18:54:49 +0800155 return method_compiler->Compile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800156}
Logan Chien83426162011-12-09 09:29:50 +0800157
158
Logan Chien7f767612012-03-01 18:54:49 +0800159CompiledMethod* CompilerLLVM::
160CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
Logan Chien88894ee2012-02-13 16:42:22 +0800161 MutexLock GUARD(compiler_lock_);
162
Logan Chien7f767612012-03-01 18:54:49 +0800163 EnsureCompilationUnit(GUARD);
Logan Chien88894ee2012-02-13 16:42:22 +0800164
Logan Chien8b977d32012-02-21 19:14:55 +0800165 UniquePtr<JniCompiler> jni_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800166 new JniCompiler(curr_cunit_, *compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800167
Logan Chien7f767612012-03-01 18:54:49 +0800168 return jni_compiler->Compile();
Logan Chien88894ee2012-02-13 16:42:22 +0800169}
170
171
Logan Chienf04364f2012-02-10 12:01:39 +0800172CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
173 char const *shorty) {
Logan Chienf04364f2012-02-10 12:01:39 +0800174 MutexLock GUARD(compiler_lock_);
175
Logan Chien7f767612012-03-01 18:54:49 +0800176 EnsureCompilationUnit(GUARD);
Logan Chienf04364f2012-02-10 12:01:39 +0800177
Logan Chien8b977d32012-02-21 19:14:55 +0800178 UniquePtr<UpcallCompiler> upcall_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800179 new UpcallCompiler(curr_cunit_, *compiler_));
Logan Chien8b977d32012-02-21 19:14:55 +0800180
Logan Chien7f767612012-03-01 18:54:49 +0800181 return upcall_compiler->CreateStub(is_static, shorty);
Logan Chienf04364f2012-02-10 12:01:39 +0800182}
183
184
Logan Chien83426162011-12-09 09:29:50 +0800185} // namespace compiler_llvm
186} // namespace art