blob: b2a6d28315469dd080ee337bc233f71c751decd0 [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 Chien4c17dff2012-03-02 20:15:46 +080030#include <llvm/Support/CommandLine.h>
Logan Chien013b6f22012-03-02 17:20:33 +080031#include <llvm/Support/ManagedStatic.h>
Logan Chien8b977d32012-02-21 19:14:55 +080032#include <llvm/Support/TargetSelect.h>
33#include <llvm/Support/Threading.h>
34
Logan Chien013b6f22012-03-02 17:20:33 +080035namespace llvm {
36 extern bool TimePassesIsEnabled;
37}
38
Logan Chien4c17dff2012-03-02 20:15:46 +080039extern llvm::cl::opt<bool> EnableARMLongCalls;
40// NOTE: Although EnableARMLongCalls is defined in llvm/lib/Target/ARM/
41// ARMISelLowering.cpp, however, it is not in the llvm namespace.
42
Logan Chien8b977d32012-02-21 19:14:55 +080043
44namespace {
45
46pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT;
47
48void InitializeLLVM() {
Logan Chien013b6f22012-03-02 17:20:33 +080049 // NOTE: Uncomment following line to show the time consumption of LLVM passes
50 //llvm::TimePassesIsEnabled = true;
51
Logan Chien8b977d32012-02-21 19:14:55 +080052 // Initialize LLVM target, MC subsystem, asm printer, and asm parser
53 llvm::InitializeAllTargets();
54 llvm::InitializeAllTargetMCs();
55 llvm::InitializeAllAsmPrinters();
56 llvm::InitializeAllAsmParsers();
57 // TODO: Maybe we don't have to initialize "all" targets.
58
Logan Chien4c17dff2012-03-02 20:15:46 +080059 // Enable -arm-long-calls
60 EnableARMLongCalls = true;
61
Logan Chiendd7cf5b2012-03-01 12:55:19 +080062 // Initialize LLVM optimization passes
63 llvm::PassRegistry &registry = *llvm::PassRegistry::getPassRegistry();
64
65 llvm::initializeCore(registry);
66 llvm::initializeScalarOpts(registry);
67 llvm::initializeIPO(registry);
68 llvm::initializeAnalysis(registry);
69 llvm::initializeIPA(registry);
70 llvm::initializeTransformUtils(registry);
71 llvm::initializeInstCombine(registry);
72 llvm::initializeInstrumentation(registry);
73 llvm::initializeTarget(registry);
74
Logan Chien8b977d32012-02-21 19:14:55 +080075 // Initialize LLVM internal data structure for multithreading
76 llvm::llvm_start_multithreaded();
77}
78
79} // anonymous namespace
80
Shih-wei Liaod1fec812012-02-13 09:51:10 -080081
Logan Chien83426162011-12-09 09:29:50 +080082namespace art {
83namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080084
85
Logan Chiene75a8cc2012-02-24 12:26:43 +080086llvm::Module* makeLLVMModuleContents(llvm::Module* module);
Logan Chien42e0e152012-01-13 15:42:36 +080087
88
Shih-wei Liaod1fec812012-02-13 09:51:10 -080089CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set)
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -080090 : compiler_(compiler), compiler_lock_("llvm_compiler_lock"),
Logan Chien7f767612012-03-01 18:54:49 +080091 insn_set_(insn_set), curr_cunit_(NULL) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080092
Logan Chien8b977d32012-02-21 19:14:55 +080093 // Initialize LLVM libraries
94 pthread_once(&llvm_initialized, InitializeLLVM);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080095}
96
97
98CompilerLLVM::~CompilerLLVM() {
Logan Chien7f767612012-03-01 18:54:49 +080099 STLDeleteElements(&cunits_);
Logan Chien013b6f22012-03-02 17:20:33 +0800100 llvm::llvm_shutdown();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800101}
102
103
Logan Chience119062012-03-02 11:57:34 +0800104void CompilerLLVM::EnsureCompilationUnit() {
Logan Chien8b977d32012-02-21 19:14:55 +0800105 DCHECK_NE(llvm_initialized, PTHREAD_ONCE_INIT);
Logan Chience119062012-03-02 11:57:34 +0800106 compiler_lock_.AssertHeld();
Logan Chien7f767612012-03-01 18:54:49 +0800107
108 if (curr_cunit_ != NULL) {
109 return;
Logan Chien8b977d32012-02-21 19:14:55 +0800110 }
Logan Chien7f767612012-03-01 18:54:49 +0800111
112 // Allocate compilation unit
113 size_t cunit_idx = cunits_.size();
114
115 curr_cunit_ = new CompilationUnit(insn_set_);
116
117 // Setup output filename
118 curr_cunit_->SetElfFileName(
119 StringPrintf("%s-%zu", elf_filename_.c_str(), cunit_idx));
120
121 if (IsBitcodeFileNameAvailable()) {
122 curr_cunit_->SetBitcodeFileName(
123 StringPrintf("%s-%zu", bitcode_filename_.c_str(), cunit_idx));
124 }
125
126 // Register compilation unit
127 cunits_.push_back(curr_cunit_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800128}
129
130
Logan Chien7f767612012-03-01 18:54:49 +0800131void CompilerLLVM::MaterializeRemainder() {
Logan Chien8b977d32012-02-21 19:14:55 +0800132 MutexLock GUARD(compiler_lock_);
Logan Chien7f767612012-03-01 18:54:49 +0800133 if (curr_cunit_ != NULL) {
Logan Chience119062012-03-02 11:57:34 +0800134 Materialize();
Logan Chien7f767612012-03-01 18:54:49 +0800135 }
136}
Logan Chien8b977d32012-02-21 19:14:55 +0800137
Logan Chien8b977d32012-02-21 19:14:55 +0800138
Logan Chien7f767612012-03-01 18:54:49 +0800139void CompilerLLVM::MaterializeIfThresholdReached() {
140 MutexLock GUARD(compiler_lock_);
141 if (curr_cunit_ != NULL && curr_cunit_->IsMaterializeThresholdReached()) {
Logan Chience119062012-03-02 11:57:34 +0800142 Materialize();
Logan Chien7f767612012-03-01 18:54:49 +0800143 }
144}
145
146
Logan Chience119062012-03-02 11:57:34 +0800147void CompilerLLVM::Materialize() {
148 compiler_lock_.AssertHeld();
149
Logan Chien7f767612012-03-01 18:54:49 +0800150 DCHECK(curr_cunit_ != NULL);
151 DCHECK(!curr_cunit_->IsMaterialized());
152
153 // Write bitcode to file when filename is set
154 if (IsBitcodeFileNameAvailable()) {
155 curr_cunit_->WriteBitcodeToFile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800156 }
157
Logan Chien8b977d32012-02-21 19:14:55 +0800158 // Materialize the llvm::Module into ELF object file
Logan Chien7f767612012-03-01 18:54:49 +0800159 curr_cunit_->Materialize();
Logan Chien8b977d32012-02-21 19:14:55 +0800160
161 // Delete the compilation unit
Logan Chien7f767612012-03-01 18:54:49 +0800162 curr_cunit_ = NULL;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800163}
164
165
Logan Chien7f767612012-03-01 18:54:49 +0800166CompiledMethod* CompilerLLVM::
167CompileDexMethod(OatCompilationUnit* oat_compilation_unit) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800168 MutexLock GUARD(compiler_lock_);
169
Logan Chience119062012-03-02 11:57:34 +0800170 EnsureCompilationUnit();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800171
Logan Chien8b977d32012-02-21 19:14:55 +0800172 UniquePtr<MethodCompiler> method_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800173 new MethodCompiler(curr_cunit_, compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800174
Logan Chien7f767612012-03-01 18:54:49 +0800175 return method_compiler->Compile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800176}
Logan Chien83426162011-12-09 09:29:50 +0800177
178
Logan Chien7f767612012-03-01 18:54:49 +0800179CompiledMethod* CompilerLLVM::
180CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
Logan Chien88894ee2012-02-13 16:42:22 +0800181 MutexLock GUARD(compiler_lock_);
182
Logan Chience119062012-03-02 11:57:34 +0800183 EnsureCompilationUnit();
Logan Chien88894ee2012-02-13 16:42:22 +0800184
Logan Chien8b977d32012-02-21 19:14:55 +0800185 UniquePtr<JniCompiler> jni_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800186 new JniCompiler(curr_cunit_, *compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800187
Logan Chien7f767612012-03-01 18:54:49 +0800188 return jni_compiler->Compile();
Logan Chien88894ee2012-02-13 16:42:22 +0800189}
190
191
Logan Chienf04364f2012-02-10 12:01:39 +0800192CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
193 char const *shorty) {
Logan Chienf04364f2012-02-10 12:01:39 +0800194 MutexLock GUARD(compiler_lock_);
195
Logan Chience119062012-03-02 11:57:34 +0800196 EnsureCompilationUnit();
Logan Chienf04364f2012-02-10 12:01:39 +0800197
Logan Chien8b977d32012-02-21 19:14:55 +0800198 UniquePtr<UpcallCompiler> upcall_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800199 new UpcallCompiler(curr_cunit_, *compiler_));
Logan Chien8b977d32012-02-21 19:14:55 +0800200
Logan Chien7f767612012-03-01 18:54:49 +0800201 return upcall_compiler->CreateStub(is_static, shorty);
Logan Chienf04364f2012-02-10 12:01:39 +0800202}
203
204
Logan Chien83426162011-12-09 09:29:50 +0800205} // namespace compiler_llvm
206} // namespace art