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