blob: 1edbd46df218fba2f57b1645433b0e97c64cdf7b [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 Chience119062012-03-02 11:57:34 +080087void CompilerLLVM::EnsureCompilationUnit() {
Logan Chien8b977d32012-02-21 19:14:55 +080088 DCHECK_NE(llvm_initialized, PTHREAD_ONCE_INIT);
Logan Chience119062012-03-02 11:57:34 +080089 compiler_lock_.AssertHeld();
Logan Chien7f767612012-03-01 18:54:49 +080090
91 if (curr_cunit_ != NULL) {
92 return;
Logan Chien8b977d32012-02-21 19:14:55 +080093 }
Logan Chien7f767612012-03-01 18:54:49 +080094
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 Liaod1fec812012-02-13 09:51:10 -0800111}
112
113
Logan Chien7f767612012-03-01 18:54:49 +0800114void CompilerLLVM::MaterializeRemainder() {
Logan Chien8b977d32012-02-21 19:14:55 +0800115 MutexLock GUARD(compiler_lock_);
Logan Chien7f767612012-03-01 18:54:49 +0800116 if (curr_cunit_ != NULL) {
Logan Chience119062012-03-02 11:57:34 +0800117 Materialize();
Logan Chien7f767612012-03-01 18:54:49 +0800118 }
119}
Logan Chien8b977d32012-02-21 19:14:55 +0800120
Logan Chien8b977d32012-02-21 19:14:55 +0800121
Logan Chien7f767612012-03-01 18:54:49 +0800122void CompilerLLVM::MaterializeIfThresholdReached() {
123 MutexLock GUARD(compiler_lock_);
124 if (curr_cunit_ != NULL && curr_cunit_->IsMaterializeThresholdReached()) {
Logan Chience119062012-03-02 11:57:34 +0800125 Materialize();
Logan Chien7f767612012-03-01 18:54:49 +0800126 }
127}
128
129
Logan Chience119062012-03-02 11:57:34 +0800130void CompilerLLVM::Materialize() {
131 compiler_lock_.AssertHeld();
132
Logan Chien7f767612012-03-01 18:54:49 +0800133 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 Liaod1fec812012-02-13 09:51:10 -0800139 }
140
Logan Chien8b977d32012-02-21 19:14:55 +0800141 // Materialize the llvm::Module into ELF object file
Logan Chien7f767612012-03-01 18:54:49 +0800142 curr_cunit_->Materialize();
Logan Chien8b977d32012-02-21 19:14:55 +0800143
144 // Delete the compilation unit
Logan Chien7f767612012-03-01 18:54:49 +0800145 curr_cunit_ = NULL;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800146}
147
148
Logan Chien7f767612012-03-01 18:54:49 +0800149CompiledMethod* CompilerLLVM::
150CompileDexMethod(OatCompilationUnit* oat_compilation_unit) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800151 MutexLock GUARD(compiler_lock_);
152
Logan Chience119062012-03-02 11:57:34 +0800153 EnsureCompilationUnit();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800154
Logan Chien8b977d32012-02-21 19:14:55 +0800155 UniquePtr<MethodCompiler> method_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800156 new MethodCompiler(curr_cunit_, compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800157
Logan Chien7f767612012-03-01 18:54:49 +0800158 return method_compiler->Compile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800159}
Logan Chien83426162011-12-09 09:29:50 +0800160
161
Logan Chien7f767612012-03-01 18:54:49 +0800162CompiledMethod* CompilerLLVM::
163CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
Logan Chien88894ee2012-02-13 16:42:22 +0800164 MutexLock GUARD(compiler_lock_);
165
Logan Chience119062012-03-02 11:57:34 +0800166 EnsureCompilationUnit();
Logan Chien88894ee2012-02-13 16:42:22 +0800167
Logan Chien8b977d32012-02-21 19:14:55 +0800168 UniquePtr<JniCompiler> jni_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800169 new JniCompiler(curr_cunit_, *compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800170
Logan Chien7f767612012-03-01 18:54:49 +0800171 return jni_compiler->Compile();
Logan Chien88894ee2012-02-13 16:42:22 +0800172}
173
174
Logan Chienf04364f2012-02-10 12:01:39 +0800175CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
176 char const *shorty) {
Logan Chienf04364f2012-02-10 12:01:39 +0800177 MutexLock GUARD(compiler_lock_);
178
Logan Chience119062012-03-02 11:57:34 +0800179 EnsureCompilationUnit();
Logan Chienf04364f2012-02-10 12:01:39 +0800180
Logan Chien8b977d32012-02-21 19:14:55 +0800181 UniquePtr<UpcallCompiler> upcall_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800182 new UpcallCompiler(curr_cunit_, *compiler_));
Logan Chien8b977d32012-02-21 19:14:55 +0800183
Logan Chien7f767612012-03-01 18:54:49 +0800184 return upcall_compiler->CreateStub(is_static, shorty);
Logan Chienf04364f2012-02-10 12:01:39 +0800185}
186
187
Logan Chien83426162011-12-09 09:29:50 +0800188} // namespace compiler_llvm
189} // namespace art