blob: 35d1ecd783d40a41daf095c1730123988832ea8f [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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
19#include "backend_options.h"
20#include "base/stl_util.h"
21#include "class_linker.h"
22#include "compiled_method.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000023#include "dex/verified_methods_data.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "driver/compiler_driver.h"
25#include "driver/dex_compilation_unit.h"
26#include "globals.h"
27#include "ir_builder.h"
28#include "jni/portable/jni_compiler.h"
29#include "llvm_compilation_unit.h"
Ian Rogers02ed4c02013-09-06 13:10:04 -070030#include "thread-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070031#include "utils_llvm.h"
32#include "verifier/method_verifier.h"
33
34#include <llvm/LinkAllPasses.h>
35#include <llvm/Support/ManagedStatic.h>
36#include <llvm/Support/TargetSelect.h>
37#include <llvm/Support/Threading.h>
38
39namespace art {
40void CompileOneMethod(CompilerDriver& driver,
41 const CompilerBackend compilerBackend,
42 const DexFile::CodeItem* code_item,
43 uint32_t access_flags, InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -070044 uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
Brian Carlstrom7940e442013-07-12 13:46:57 -070045 const DexFile& dex_file,
46 llvm::LlvmCompilationUnit* llvm_info);
47}
48
49namespace llvm {
50 extern bool TimePassesIsEnabled;
51}
52
53namespace {
54
55pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT;
56
57void InitializeLLVM() {
58 // Initialize LLVM internal data structure for multithreading
59 llvm::llvm_start_multithreaded();
60
61 // NOTE: Uncomment following line to show the time consumption of LLVM passes
Brian Carlstrom7934ac22013-07-26 10:54:15 -070062 // llvm::TimePassesIsEnabled = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -070063
64 // Initialize LLVM target-specific options.
65 art::llvm::InitialBackendOptions();
66
67 // Initialize LLVM target, MC subsystem, asm printer, and asm parser.
68 if (art::kIsTargetBuild) {
69 // Don't initialize all targets on device. Just initialize the device's native target
70 llvm::InitializeNativeTarget();
71 llvm::InitializeNativeTargetAsmPrinter();
72 llvm::InitializeNativeTargetAsmParser();
73 } else {
74 llvm::InitializeAllTargets();
75 llvm::InitializeAllTargetMCs();
76 llvm::InitializeAllAsmPrinters();
77 llvm::InitializeAllAsmParsers();
78 }
79
80 // Initialize LLVM optimization passes
81 llvm::PassRegistry &registry = *llvm::PassRegistry::getPassRegistry();
82
83 llvm::initializeCore(registry);
84 llvm::initializeScalarOpts(registry);
85 llvm::initializeIPO(registry);
86 llvm::initializeAnalysis(registry);
87 llvm::initializeIPA(registry);
88 llvm::initializeTransformUtils(registry);
89 llvm::initializeInstCombine(registry);
90 llvm::initializeInstrumentation(registry);
91 llvm::initializeTarget(registry);
92}
93
94// The Guard to Shutdown LLVM
95// llvm::llvm_shutdown_obj llvm_guard;
96// TODO: We are commenting out this line because this will cause SEGV from
97// time to time.
98// Two reasons: (1) the order of the destruction of static objects, or
99// (2) dlopen/dlclose side-effect on static objects.
100
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700101} // anonymous namespace
Brian Carlstrom7940e442013-07-12 13:46:57 -0700102
103
104namespace art {
105namespace llvm {
106
107
108::llvm::Module* makeLLVMModuleContents(::llvm::Module* module);
109
110
111CompilerLLVM::CompilerLLVM(CompilerDriver* driver, InstructionSet insn_set)
112 : compiler_driver_(driver), insn_set_(insn_set),
113 next_cunit_id_lock_("compilation unit id lock"), next_cunit_id_(1) {
114
115 // Initialize LLVM libraries
116 pthread_once(&llvm_initialized, InitializeLLVM);
117}
118
119
120CompilerLLVM::~CompilerLLVM() {
121}
122
123
124LlvmCompilationUnit* CompilerLLVM::AllocateCompilationUnit() {
125 MutexLock GUARD(Thread::Current(), next_cunit_id_lock_);
126 LlvmCompilationUnit* cunit = new LlvmCompilationUnit(this, next_cunit_id_++);
127 if (!bitcode_filename_.empty()) {
128 cunit->SetBitcodeFileName(StringPrintf("%s-%zu",
129 bitcode_filename_.c_str(),
130 cunit->GetCompilationUnitId()));
131 }
132 return cunit;
133}
134
135
136CompiledMethod* CompilerLLVM::
137CompileDexMethod(DexCompilationUnit* dex_compilation_unit, InvokeType invoke_type) {
138 UniquePtr<LlvmCompilationUnit> cunit(AllocateCompilationUnit());
139
140 cunit->SetDexCompilationUnit(dex_compilation_unit);
141 cunit->SetCompilerDriver(compiler_driver_);
142 // TODO: consolidate ArtCompileMethods
143 CompileOneMethod(*compiler_driver_,
144 kPortable,
145 dex_compilation_unit->GetCodeItem(),
146 dex_compilation_unit->GetAccessFlags(),
147 invoke_type,
148 dex_compilation_unit->GetClassDefIndex(),
149 dex_compilation_unit->GetDexMethodIndex(),
150 dex_compilation_unit->GetClassLoader(),
151 *dex_compilation_unit->GetDexFile(),
152 cunit.get());
153
154 cunit->Materialize();
155
156 MethodReference mref(dex_compilation_unit->GetDexFile(),
157 dex_compilation_unit->GetDexMethodIndex());
Mathieu Chartier193bad92013-08-29 18:46:00 -0700158 return new CompiledMethod(*compiler_driver_, compiler_driver_->GetInstructionSet(),
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000159 cunit->GetElfObject(),
160 *compiler_driver_->GetVerifiedMethodsData()->GetDexGcMap(mref),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161 cunit->GetDexCompilationUnit()->GetSymbol());
162}
163
164
165CompiledMethod* CompilerLLVM::
166CompileNativeMethod(DexCompilationUnit* dex_compilation_unit) {
167 UniquePtr<LlvmCompilationUnit> cunit(AllocateCompilationUnit());
168
169 UniquePtr<JniCompiler> jni_compiler(
Ian Rogersd133b972013-09-05 11:01:30 -0700170 new JniCompiler(cunit.get(), compiler_driver_, dex_compilation_unit));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171
172 return jni_compiler->Compile();
173}
174
175
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700176} // namespace llvm
177} // namespace art
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178
179inline static art::llvm::CompilerLLVM* ContextOf(art::CompilerDriver& driver) {
180 void *compiler_context = driver.GetCompilerContext();
181 CHECK(compiler_context != NULL);
182 return reinterpret_cast<art::llvm::CompilerLLVM*>(compiler_context);
183}
184
185inline static const art::llvm::CompilerLLVM* ContextOf(const art::CompilerDriver& driver) {
186 void *compiler_context = driver.GetCompilerContext();
187 CHECK(compiler_context != NULL);
188 return reinterpret_cast<const art::llvm::CompilerLLVM*>(compiler_context);
189}
190
191extern "C" void ArtInitCompilerContext(art::CompilerDriver& driver) {
192 CHECK(driver.GetCompilerContext() == NULL);
193
194 art::llvm::CompilerLLVM* compiler_llvm = new art::llvm::CompilerLLVM(&driver,
195 driver.GetInstructionSet());
196
197 driver.SetCompilerContext(compiler_llvm);
198}
199
200extern "C" void ArtUnInitCompilerContext(art::CompilerDriver& driver) {
201 delete ContextOf(driver);
202 driver.SetCompilerContext(NULL);
203}
204extern "C" art::CompiledMethod* ArtCompileMethod(art::CompilerDriver& driver,
205 const art::DexFile::CodeItem* code_item,
206 uint32_t access_flags,
207 art::InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700208 uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 uint32_t method_idx,
210 jobject class_loader,
211 const art::DexFile& dex_file) {
212 UNUSED(class_def_idx); // TODO: this is used with Compiler::RequiresConstructorBarrier.
213 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
214
215 art::DexCompilationUnit dex_compilation_unit(
216 NULL, class_loader, class_linker, dex_file, code_item,
217 class_def_idx, method_idx, access_flags);
218 art::llvm::CompilerLLVM* compiler_llvm = ContextOf(driver);
219 art::CompiledMethod* result = compiler_llvm->CompileDexMethod(&dex_compilation_unit, invoke_type);
220 return result;
221}
222
223extern "C" art::CompiledMethod* ArtLLVMJniCompileMethod(art::CompilerDriver& driver,
224 uint32_t access_flags, uint32_t method_idx,
225 const art::DexFile& dex_file) {
226 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
227
228 art::DexCompilationUnit dex_compilation_unit(
229 NULL, NULL, class_linker, dex_file, NULL,
230 0, method_idx, access_flags);
231
232 art::llvm::CompilerLLVM* compiler_llvm = ContextOf(driver);
233 art::CompiledMethod* result = compiler_llvm->CompileNativeMethod(&dex_compilation_unit);
234 return result;
235}
236
237extern "C" void compilerLLVMSetBitcodeFileName(art::CompilerDriver& driver,
238 std::string const& filename) {
239 ContextOf(driver)->SetBitcodeFileName(filename);
240}