blob: 67b3f4d50c99cdc96c9c8cc912973b13af0ba751 [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
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080044namespace {
Logan Chien8b977d32012-02-21 19:14:55 +080045
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080046pthread_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
Shih-wei Liao63deaad2012-03-06 17:26:37 -080079// Singleton. Otherwise, multiple CompilerLLVM instances may cause crashes if
80// one shuts down prematurely.
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080081llvm::llvm_shutdown_obj llvm_guard;
82
83} // anonymous namespace
84
85
86namespace art {
87namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080088
89
Logan Chiene75a8cc2012-02-24 12:26:43 +080090llvm::Module* makeLLVMModuleContents(llvm::Module* module);
Logan Chien42e0e152012-01-13 15:42:36 +080091
92
Shih-wei Liaod1fec812012-02-13 09:51:10 -080093CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set)
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -080094 : compiler_(compiler), compiler_lock_("llvm_compiler_lock"),
Logan Chien7f767612012-03-01 18:54:49 +080095 insn_set_(insn_set), curr_cunit_(NULL) {
Shih-wei Liaofc34adb2012-03-07 08:51:44 -080096
97
98 // Initialize LLVM libraries
99 pthread_once(&llvm_initialized, InitializeLLVM);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800100}
101
102
103CompilerLLVM::~CompilerLLVM() {
Logan Chien7f767612012-03-01 18:54:49 +0800104 STLDeleteElements(&cunits_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800105}
106
107
Logan Chience119062012-03-02 11:57:34 +0800108void CompilerLLVM::EnsureCompilationUnit() {
Logan Chience119062012-03-02 11:57:34 +0800109 compiler_lock_.AssertHeld();
Logan Chien7f767612012-03-01 18:54:49 +0800110
111 if (curr_cunit_ != NULL) {
112 return;
Logan Chien8b977d32012-02-21 19:14:55 +0800113 }
Logan Chien7f767612012-03-01 18:54:49 +0800114
115 // Allocate compilation unit
116 size_t cunit_idx = cunits_.size();
117
118 curr_cunit_ = new CompilationUnit(insn_set_);
119
120 // Setup output filename
121 curr_cunit_->SetElfFileName(
122 StringPrintf("%s-%zu", elf_filename_.c_str(), cunit_idx));
123
124 if (IsBitcodeFileNameAvailable()) {
125 curr_cunit_->SetBitcodeFileName(
126 StringPrintf("%s-%zu", bitcode_filename_.c_str(), cunit_idx));
127 }
128
129 // Register compilation unit
130 cunits_.push_back(curr_cunit_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800131}
132
133
Logan Chien7f767612012-03-01 18:54:49 +0800134void CompilerLLVM::MaterializeRemainder() {
Logan Chien8b977d32012-02-21 19:14:55 +0800135 MutexLock GUARD(compiler_lock_);
Logan Chien7f767612012-03-01 18:54:49 +0800136 if (curr_cunit_ != NULL) {
Logan Chience119062012-03-02 11:57:34 +0800137 Materialize();
Logan Chien7f767612012-03-01 18:54:49 +0800138 }
139}
Logan Chien8b977d32012-02-21 19:14:55 +0800140
Logan Chien8b977d32012-02-21 19:14:55 +0800141
Logan Chien7f767612012-03-01 18:54:49 +0800142void CompilerLLVM::MaterializeIfThresholdReached() {
143 MutexLock GUARD(compiler_lock_);
144 if (curr_cunit_ != NULL && curr_cunit_->IsMaterializeThresholdReached()) {
Logan Chience119062012-03-02 11:57:34 +0800145 Materialize();
Logan Chien7f767612012-03-01 18:54:49 +0800146 }
147}
148
149
Logan Chience119062012-03-02 11:57:34 +0800150void CompilerLLVM::Materialize() {
151 compiler_lock_.AssertHeld();
152
Logan Chien7f767612012-03-01 18:54:49 +0800153 DCHECK(curr_cunit_ != NULL);
154 DCHECK(!curr_cunit_->IsMaterialized());
155
156 // Write bitcode to file when filename is set
157 if (IsBitcodeFileNameAvailable()) {
158 curr_cunit_->WriteBitcodeToFile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800159 }
160
Logan Chien8b977d32012-02-21 19:14:55 +0800161 // Materialize the llvm::Module into ELF object file
Logan Chien7f767612012-03-01 18:54:49 +0800162 curr_cunit_->Materialize();
Logan Chien8b977d32012-02-21 19:14:55 +0800163
164 // Delete the compilation unit
Logan Chien7f767612012-03-01 18:54:49 +0800165 curr_cunit_ = NULL;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800166}
167
168
Logan Chien7f767612012-03-01 18:54:49 +0800169CompiledMethod* CompilerLLVM::
170CompileDexMethod(OatCompilationUnit* oat_compilation_unit) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800171 MutexLock GUARD(compiler_lock_);
172
Logan Chience119062012-03-02 11:57:34 +0800173 EnsureCompilationUnit();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800174
Logan Chien8b977d32012-02-21 19:14:55 +0800175 UniquePtr<MethodCompiler> method_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800176 new MethodCompiler(curr_cunit_, compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800177
Logan Chien7f767612012-03-01 18:54:49 +0800178 return method_compiler->Compile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800179}
Logan Chien83426162011-12-09 09:29:50 +0800180
181
Logan Chien7f767612012-03-01 18:54:49 +0800182CompiledMethod* CompilerLLVM::
183CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
Logan Chien88894ee2012-02-13 16:42:22 +0800184 MutexLock GUARD(compiler_lock_);
185
Logan Chience119062012-03-02 11:57:34 +0800186 EnsureCompilationUnit();
Logan Chien88894ee2012-02-13 16:42:22 +0800187
Logan Chien8b977d32012-02-21 19:14:55 +0800188 UniquePtr<JniCompiler> jni_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800189 new JniCompiler(curr_cunit_, *compiler_, oat_compilation_unit));
Logan Chien8b977d32012-02-21 19:14:55 +0800190
Logan Chien7f767612012-03-01 18:54:49 +0800191 return jni_compiler->Compile();
Logan Chien88894ee2012-02-13 16:42:22 +0800192}
193
194
Logan Chienf04364f2012-02-10 12:01:39 +0800195CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
196 char const *shorty) {
Logan Chienf04364f2012-02-10 12:01:39 +0800197 MutexLock GUARD(compiler_lock_);
198
Logan Chience119062012-03-02 11:57:34 +0800199 EnsureCompilationUnit();
Logan Chienf04364f2012-02-10 12:01:39 +0800200
Logan Chien8b977d32012-02-21 19:14:55 +0800201 UniquePtr<UpcallCompiler> upcall_compiler(
Logan Chien7f767612012-03-01 18:54:49 +0800202 new UpcallCompiler(curr_cunit_, *compiler_));
Logan Chien8b977d32012-02-21 19:14:55 +0800203
Logan Chien7f767612012-03-01 18:54:49 +0800204 return upcall_compiler->CreateStub(is_static, shorty);
Logan Chienf04364f2012-02-10 12:01:39 +0800205}
206
207
Logan Chien83426162011-12-09 09:29:50 +0800208} // namespace compiler_llvm
209} // namespace art