blob: afca223192f6cf11899f854710fd7da5285a1efc [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"
29#include "oat_file.h"
30#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
61 //llvm::TimePassesIsEnabled = true;
62
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
100} // anonymous namespace
101
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());
157 return new CompiledMethod(compiler_driver_->GetInstructionSet(),
158 cunit->GetElfObject(),
159 *verifier::MethodVerifier::GetDexGcMap(mref),
160 cunit->GetDexCompilationUnit()->GetSymbol());
161}
162
163
164CompiledMethod* CompilerLLVM::
165CompileNativeMethod(DexCompilationUnit* dex_compilation_unit) {
166 UniquePtr<LlvmCompilationUnit> cunit(AllocateCompilationUnit());
167
168 UniquePtr<JniCompiler> jni_compiler(
169 new JniCompiler(cunit.get(), *compiler_driver_, dex_compilation_unit));
170
171 return jni_compiler->Compile();
172}
173
174
175} // namespace llvm
176} // namespace art
177
178inline static art::llvm::CompilerLLVM* ContextOf(art::CompilerDriver& driver) {
179 void *compiler_context = driver.GetCompilerContext();
180 CHECK(compiler_context != NULL);
181 return reinterpret_cast<art::llvm::CompilerLLVM*>(compiler_context);
182}
183
184inline static const art::llvm::CompilerLLVM* ContextOf(const art::CompilerDriver& driver) {
185 void *compiler_context = driver.GetCompilerContext();
186 CHECK(compiler_context != NULL);
187 return reinterpret_cast<const art::llvm::CompilerLLVM*>(compiler_context);
188}
189
190extern "C" void ArtInitCompilerContext(art::CompilerDriver& driver) {
191 CHECK(driver.GetCompilerContext() == NULL);
192
193 art::llvm::CompilerLLVM* compiler_llvm = new art::llvm::CompilerLLVM(&driver,
194 driver.GetInstructionSet());
195
196 driver.SetCompilerContext(compiler_llvm);
197}
198
199extern "C" void ArtUnInitCompilerContext(art::CompilerDriver& driver) {
200 delete ContextOf(driver);
201 driver.SetCompilerContext(NULL);
202}
203extern "C" art::CompiledMethod* ArtCompileMethod(art::CompilerDriver& driver,
204 const art::DexFile::CodeItem* code_item,
205 uint32_t access_flags,
206 art::InvokeType invoke_type,
207 uint32_t class_def_idx,
208 uint32_t method_idx,
209 jobject class_loader,
210 const art::DexFile& dex_file) {
211 UNUSED(class_def_idx); // TODO: this is used with Compiler::RequiresConstructorBarrier.
212 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
213
214 art::DexCompilationUnit dex_compilation_unit(
215 NULL, class_loader, class_linker, dex_file, code_item,
216 class_def_idx, method_idx, access_flags);
217 art::llvm::CompilerLLVM* compiler_llvm = ContextOf(driver);
218 art::CompiledMethod* result = compiler_llvm->CompileDexMethod(&dex_compilation_unit, invoke_type);
219 return result;
220}
221
222extern "C" art::CompiledMethod* ArtLLVMJniCompileMethod(art::CompilerDriver& driver,
223 uint32_t access_flags, uint32_t method_idx,
224 const art::DexFile& dex_file) {
225 art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
226
227 art::DexCompilationUnit dex_compilation_unit(
228 NULL, NULL, class_linker, dex_file, NULL,
229 0, method_idx, access_flags);
230
231 art::llvm::CompilerLLVM* compiler_llvm = ContextOf(driver);
232 art::CompiledMethod* result = compiler_llvm->CompileNativeMethod(&dex_compilation_unit);
233 return result;
234}
235
236extern "C" void compilerLLVMSetBitcodeFileName(art::CompilerDriver& driver,
237 std::string const& filename) {
238 ContextOf(driver)->SetBitcodeFileName(filename);
239}