blob: c94e7ea36d9631791610386fb380ee9ea3a20f33 [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 Chien013b6f22012-03-02 17:20:33 +080030#include <llvm/Support/ManagedStatic.h>
Logan Chien8b977d32012-02-21 19:14:55 +080031#include <llvm/Support/TargetSelect.h>
32#include <llvm/Support/Threading.h>
33
Logan Chien013b6f22012-03-02 17:20:33 +080034namespace llvm {
35 extern bool TimePassesIsEnabled;
36}
37
Logan Chien8b977d32012-02-21 19:14:55 +080038
39namespace {
40
41pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT;
42
43void InitializeLLVM() {
Logan Chien013b6f22012-03-02 17:20:33 +080044 // NOTE: Uncomment following line to show the time consumption of LLVM passes
45 //llvm::TimePassesIsEnabled = true;
46
Logan Chien8b977d32012-02-21 19:14:55 +080047 // Initialize LLVM target, MC subsystem, asm printer, and asm parser
48 llvm::InitializeAllTargets();
49 llvm::InitializeAllTargetMCs();
50 llvm::InitializeAllAsmPrinters();
51 llvm::InitializeAllAsmParsers();
52 // TODO: Maybe we don't have to initialize "all" targets.
53
Logan Chiendd7cf5b2012-03-01 12:55:19 +080054 // Initialize LLVM optimization passes
55 llvm::PassRegistry &registry = *llvm::PassRegistry::getPassRegistry();
56
57 llvm::initializeCore(registry);
58 llvm::initializeScalarOpts(registry);
59 llvm::initializeIPO(registry);
60 llvm::initializeAnalysis(registry);
61 llvm::initializeIPA(registry);
62 llvm::initializeTransformUtils(registry);
63 llvm::initializeInstCombine(registry);
64 llvm::initializeInstrumentation(registry);
65 llvm::initializeTarget(registry);
66
Logan Chien8b977d32012-02-21 19:14:55 +080067 // Initialize LLVM internal data structure for multithreading
68 llvm::llvm_start_multithreaded();
69}
70
71} // anonymous namespace
72
Shih-wei Liaod1fec812012-02-13 09:51:10 -080073
Logan Chien83426162011-12-09 09:29:50 +080074namespace art {
75namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080076
77
Logan Chiene75a8cc2012-02-24 12:26:43 +080078llvm::Module* makeLLVMModuleContents(llvm::Module* module);
Logan Chien42e0e152012-01-13 15:42:36 +080079
80
Shih-wei Liaod1fec812012-02-13 09:51:10 -080081CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set)
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -080082 : compiler_(compiler), compiler_lock_("llvm_compiler_lock"),
Logan Chien7f767612012-03-01 18:54:49 +080083 insn_set_(insn_set), curr_cunit_(NULL) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080084
Logan Chien8b977d32012-02-21 19:14:55 +080085 // Initialize LLVM libraries
86 pthread_once(&llvm_initialized, InitializeLLVM);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080087}
88
89
90CompilerLLVM::~CompilerLLVM() {
Logan Chien7f767612012-03-01 18:54:49 +080091 STLDeleteElements(&cunits_);
Logan Chien013b6f22012-03-02 17:20:33 +080092 llvm::llvm_shutdown();
Shih-wei Liaod1fec812012-02-13 09:51:10 -080093}
94
95
Logan Chience119062012-03-02 11:57:34 +080096void CompilerLLVM::EnsureCompilationUnit() {
Logan Chien8b977d32012-02-21 19:14:55 +080097 DCHECK_NE(llvm_initialized, PTHREAD_ONCE_INIT);
Logan Chience119062012-03-02 11:57:34 +080098 compiler_lock_.AssertHeld();
Logan Chien7f767612012-03-01 18:54:49 +080099
100 if (curr_cunit_ != NULL) {
101 return;
Logan Chien8b977d32012-02-21 19:14:55 +0800102 }
Logan Chien7f767612012-03-01 18:54:49 +0800103
104 // Allocate compilation unit
105 size_t cunit_idx = cunits_.size();
106
107 curr_cunit_ = new CompilationUnit(insn_set_);
108
109 // Setup output filename
110 curr_cunit_->SetElfFileName(
111 StringPrintf("%s-%zu", elf_filename_.c_str(), cunit_idx));
112
113 if (IsBitcodeFileNameAvailable()) {
114 curr_cunit_->SetBitcodeFileName(
115 StringPrintf("%s-%zu", bitcode_filename_.c_str(), cunit_idx));
116 }
117
118 // Register compilation unit
119 cunits_.push_back(curr_cunit_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800120}
121
122
Logan Chien7f767612012-03-01 18:54:49 +0800123void CompilerLLVM::MaterializeRemainder() {
Logan Chien8b977d32012-02-21 19:14:55 +0800124 MutexLock GUARD(compiler_lock_);
Logan Chien7f767612012-03-01 18:54:49 +0800125 if (curr_cunit_ != NULL) {
Logan Chience119062012-03-02 11:57:34 +0800126 Materialize();
Logan Chien7f767612012-03-01 18:54:49 +0800127 }
128}
Logan Chien8b977d32012-02-21 19:14:55 +0800129
Logan Chien8b977d32012-02-21 19:14:55 +0800130
Logan Chien7f767612012-03-01 18:54:49 +0800131void CompilerLLVM::MaterializeIfThresholdReached() {
132 MutexLock GUARD(compiler_lock_);
133 if (curr_cunit_ != NULL && curr_cunit_->IsMaterializeThresholdReached()) {
Logan Chience119062012-03-02 11:57:34 +0800134 Materialize();
Logan Chien7f767612012-03-01 18:54:49 +0800135 }
136}
137
138
Logan Chience119062012-03-02 11:57:34 +0800139void CompilerLLVM::Materialize() {
140 compiler_lock_.AssertHeld();
141
Logan Chien7f767612012-03-01 18:54:49 +0800142 DCHECK(curr_cunit_ != NULL);
143 DCHECK(!curr_cunit_->IsMaterialized());
144
145 // Write bitcode to file when filename is set
146 if (IsBitcodeFileNameAvailable()) {
147 curr_cunit_->WriteBitcodeToFile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800148 }
149
Logan Chien8b977d32012-02-21 19:14:55 +0800150 // Materialize the llvm::Module into ELF object file
Logan Chien7f767612012-03-01 18:54:49 +0800151 curr_cunit_->Materialize();
Logan Chien8b977d32012-02-21 19:14:55 +0800152
153 // Delete the compilation unit
Logan Chien7f767612012-03-01 18:54:49 +0800154 curr_cunit_ = NULL;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800155}
156
157
Logan Chien7f767612012-03-01 18:54:49 +0800158CompiledMethod* CompilerLLVM::
159CompileDexMethod(OatCompilationUnit* oat_compilation_unit) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800160 MutexLock GUARD(compiler_lock_);
161
Logan Chience119062012-03-02 11:57:34 +0800162 EnsureCompilationUnit();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800163
Logan Chien8b977d32012-02-21 19:14:55 +0800164 UniquePtr<MethodCompiler> method_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800165 new MethodCompiler(curr_cunit_, compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800166
Logan Chien7f767612012-03-01 18:54:49 +0800167 return method_compiler->Compile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800168}
Logan Chien83426162011-12-09 09:29:50 +0800169
170
Logan Chien7f767612012-03-01 18:54:49 +0800171CompiledMethod* CompilerLLVM::
172CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
Logan Chien88894ee2012-02-13 16:42:22 +0800173 MutexLock GUARD(compiler_lock_);
174
Logan Chience119062012-03-02 11:57:34 +0800175 EnsureCompilationUnit();
Logan Chien88894ee2012-02-13 16:42:22 +0800176
Logan Chien8b977d32012-02-21 19:14:55 +0800177 UniquePtr<JniCompiler> jni_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800178 new JniCompiler(curr_cunit_, *compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800179
Logan Chien7f767612012-03-01 18:54:49 +0800180 return jni_compiler->Compile();
Logan Chien88894ee2012-02-13 16:42:22 +0800181}
182
183
Logan Chienf04364f2012-02-10 12:01:39 +0800184CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
185 char const *shorty) {
Logan Chienf04364f2012-02-10 12:01:39 +0800186 MutexLock GUARD(compiler_lock_);
187
Logan Chience119062012-03-02 11:57:34 +0800188 EnsureCompilationUnit();
Logan Chienf04364f2012-02-10 12:01:39 +0800189
Logan Chien8b977d32012-02-21 19:14:55 +0800190 UniquePtr<UpcallCompiler> upcall_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800191 new UpcallCompiler(curr_cunit_, *compiler_));
Logan Chien8b977d32012-02-21 19:14:55 +0800192
Logan Chien7f767612012-03-01 18:54:49 +0800193 return upcall_compiler->CreateStub(is_static, shorty);
Logan Chienf04364f2012-02-10 12:01:39 +0800194}
195
196
Logan Chien83426162011-12-09 09:29:50 +0800197} // namespace compiler_llvm
198} // namespace art