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