blob: 3aeecad78ea96084aa098f80f08c93b04f831a81 [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 Markoc7f83202014-01-24 17:55:18 +000023#include "dex/verification_results.h"
Vladimir Marko2730db02014-01-27 11:15:17 +000024#include "dex/verified_method.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include "driver/compiler_driver.h"
26#include "driver/dex_compilation_unit.h"
27#include "globals.h"
28#include "ir_builder.h"
29#include "jni/portable/jni_compiler.h"
30#include "llvm_compilation_unit.h"
Ian Rogers02ed4c02013-09-06 13:10:04 -070031#include "thread-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070032#include "utils_llvm.h"
33#include "verifier/method_verifier.h"
34
35#include <llvm/LinkAllPasses.h>
36#include <llvm/Support/ManagedStatic.h>
37#include <llvm/Support/TargetSelect.h>
38#include <llvm/Support/Threading.h>
39
40namespace art {
41void CompileOneMethod(CompilerDriver& driver,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000042 Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -070043 const DexFile::CodeItem* code_item,
44 uint32_t access_flags, InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -070045 uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
Brian Carlstrom7940e442013-07-12 13:46:57 -070046 const DexFile& dex_file,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +000047 void* llvm_info);
Brian Carlstrom7940e442013-07-12 13:46:57 -070048}
49
50namespace llvm {
51 extern bool TimePassesIsEnabled;
52}
53
54namespace {
55
56pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT;
57
58void InitializeLLVM() {
59 // Initialize LLVM internal data structure for multithreading
60 llvm::llvm_start_multithreaded();
61
62 // NOTE: Uncomment following line to show the time consumption of LLVM passes
Brian Carlstrom7934ac22013-07-26 10:54:15 -070063 // llvm::TimePassesIsEnabled = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -070064
65 // Initialize LLVM target-specific options.
66 art::llvm::InitialBackendOptions();
67
68 // Initialize LLVM target, MC subsystem, asm printer, and asm parser.
69 if (art::kIsTargetBuild) {
70 // Don't initialize all targets on device. Just initialize the device's native target
71 llvm::InitializeNativeTarget();
72 llvm::InitializeNativeTargetAsmPrinter();
73 llvm::InitializeNativeTargetAsmParser();
74 } else {
75 llvm::InitializeAllTargets();
76 llvm::InitializeAllTargetMCs();
77 llvm::InitializeAllAsmPrinters();
78 llvm::InitializeAllAsmParsers();
79 }
80
81 // Initialize LLVM optimization passes
82 llvm::PassRegistry &registry = *llvm::PassRegistry::getPassRegistry();
83
84 llvm::initializeCore(registry);
85 llvm::initializeScalarOpts(registry);
86 llvm::initializeIPO(registry);
87 llvm::initializeAnalysis(registry);
88 llvm::initializeIPA(registry);
89 llvm::initializeTransformUtils(registry);
90 llvm::initializeInstCombine(registry);
91 llvm::initializeInstrumentation(registry);
92 llvm::initializeTarget(registry);
93}
94
95// The Guard to Shutdown LLVM
96// llvm::llvm_shutdown_obj llvm_guard;
97// TODO: We are commenting out this line because this will cause SEGV from
98// time to time.
99// Two reasons: (1) the order of the destruction of static objects, or
100// (2) dlopen/dlclose side-effect on static objects.
101
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700102} // anonymous namespace
Brian Carlstrom7940e442013-07-12 13:46:57 -0700103
104
105namespace art {
106namespace llvm {
107
108
109::llvm::Module* makeLLVMModuleContents(::llvm::Module* module);
110
111
112CompilerLLVM::CompilerLLVM(CompilerDriver* driver, InstructionSet insn_set)
113 : compiler_driver_(driver), insn_set_(insn_set),
114 next_cunit_id_lock_("compilation unit id lock"), next_cunit_id_(1) {
115
116 // Initialize LLVM libraries
117 pthread_once(&llvm_initialized, InitializeLLVM);
118}
119
120
121CompilerLLVM::~CompilerLLVM() {
122}
123
124
125LlvmCompilationUnit* CompilerLLVM::AllocateCompilationUnit() {
126 MutexLock GUARD(Thread::Current(), next_cunit_id_lock_);
127 LlvmCompilationUnit* cunit = new LlvmCompilationUnit(this, next_cunit_id_++);
128 if (!bitcode_filename_.empty()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800129 cunit->SetBitcodeFileName(StringPrintf("%s-%u",
Brian Carlstrom7940e442013-07-12 13:46:57 -0700130 bitcode_filename_.c_str(),
131 cunit->GetCompilationUnitId()));
132 }
133 return cunit;
134}
135
136
137CompiledMethod* CompilerLLVM::
138CompileDexMethod(DexCompilationUnit* dex_compilation_unit, InvokeType invoke_type) {
Ian Rogers700a4022014-05-19 16:49:03 -0700139 std::unique_ptr<LlvmCompilationUnit> cunit(AllocateCompilationUnit());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140
141 cunit->SetDexCompilationUnit(dex_compilation_unit);
142 cunit->SetCompilerDriver(compiler_driver_);
143 // TODO: consolidate ArtCompileMethods
Andreas Gampe53c913b2014-08-12 23:19:23 -0700144 CompileOneMethod(compiler_driver_,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000145 compiler_driver_->GetCompiler(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700146 dex_compilation_unit->GetCodeItem(),
147 dex_compilation_unit->GetAccessFlags(),
148 invoke_type,
149 dex_compilation_unit->GetClassDefIndex(),
150 dex_compilation_unit->GetDexMethodIndex(),
151 dex_compilation_unit->GetClassLoader(),
152 *dex_compilation_unit->GetDexFile(),
153 cunit.get());
154
155 cunit->Materialize();
156
Mathieu Chartier193bad92013-08-29 18:46:00 -0700157 return new CompiledMethod(*compiler_driver_, compiler_driver_->GetInstructionSet(),
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000158 cunit->GetElfObject(),
Vladimir Marko2730db02014-01-27 11:15:17 +0000159 dex_compilation_unit->GetVerifiedMethod()->GetDexGcMap(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160 cunit->GetDexCompilationUnit()->GetSymbol());
161}
162
163
164CompiledMethod* CompilerLLVM::
165CompileNativeMethod(DexCompilationUnit* dex_compilation_unit) {
Ian Rogers700a4022014-05-19 16:49:03 -0700166 std::unique_ptr<LlvmCompilationUnit> cunit(AllocateCompilationUnit());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700167
Ian Rogers700a4022014-05-19 16:49:03 -0700168 std::unique_ptr<JniCompiler> jni_compiler(
Ian Rogersd133b972013-09-05 11:01:30 -0700169 new JniCompiler(cunit.get(), compiler_driver_, dex_compilation_unit));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170
171 return jni_compiler->Compile();
172}
173
174
Andreas Gampe53c913b2014-08-12 23:19:23 -0700175static CompilerLLVM* ContextOf(art::CompilerDriver* driver) {
Ian Rogers72d32622014-05-06 16:20:11 -0700176 void *compiler_context = driver->GetCompilerContext();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177 CHECK(compiler_context != NULL);
Andreas Gampe53c913b2014-08-12 23:19:23 -0700178 return reinterpret_cast<CompilerLLVM*>(compiler_context);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179}
180
Andreas Gampe53c913b2014-08-12 23:19:23 -0700181static CompilerLLVM* ContextOf(const art::CompilerDriver& driver) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182 void *compiler_context = driver.GetCompilerContext();
183 CHECK(compiler_context != NULL);
Andreas Gampe53c913b2014-08-12 23:19:23 -0700184 return reinterpret_cast<CompilerLLVM*>(compiler_context);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700185}
186
Andreas Gampe53c913b2014-08-12 23:19:23 -0700187void ArtInitCompilerContext(CompilerDriver* driver) {
Ian Rogers72d32622014-05-06 16:20:11 -0700188 CHECK(driver->GetCompilerContext() == nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700189
Andreas Gampe53c913b2014-08-12 23:19:23 -0700190 CompilerLLVM* compiler_llvm = new CompilerLLVM(driver, driver->GetInstructionSet());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191
Ian Rogers72d32622014-05-06 16:20:11 -0700192 driver->SetCompilerContext(compiler_llvm);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700193}
194
Andreas Gampe53c913b2014-08-12 23:19:23 -0700195void ArtUnInitCompilerContext(CompilerDriver* driver) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700196 delete ContextOf(driver);
Ian Rogers72d32622014-05-06 16:20:11 -0700197 driver->SetCompilerContext(nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700198}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700199
200CompiledMethod* ArtCompileMethod(CompilerDriver* driver, const DexFile::CodeItem* code_item,
201 uint32_t access_flags, InvokeType invoke_type,
202 uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
203 const DexFile& dex_file) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700204 UNUSED(class_def_idx); // TODO: this is used with Compiler::RequiresConstructorBarrier.
Andreas Gampe53c913b2014-08-12 23:19:23 -0700205 ClassLinker *class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700206
Andreas Gampe53c913b2014-08-12 23:19:23 -0700207 DexCompilationUnit dex_compilation_unit(nullptr, class_loader, class_linker, dex_file, code_item,
208 class_def_idx, method_idx, access_flags,
209 driver->GetVerifiedMethod(&dex_file, method_idx));
210 CompilerLLVM* compiler_llvm = ContextOf(driver);
211 CompiledMethod* result = compiler_llvm->CompileDexMethod(&dex_compilation_unit, invoke_type);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700212 return result;
213}
214
Andreas Gampe53c913b2014-08-12 23:19:23 -0700215CompiledMethod* ArtLLVMJniCompileMethod(CompilerDriver* driver, uint32_t access_flags,
216 uint32_t method_idx, const DexFile& dex_file) {
217 ClassLinker *class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218
Andreas Gampe53c913b2014-08-12 23:19:23 -0700219 DexCompilationUnit dex_compilation_unit(nullptr, nullptr, class_linker, dex_file, nullptr,
220 0, method_idx, access_flags, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221
Andreas Gampe53c913b2014-08-12 23:19:23 -0700222 CompilerLLVM* compiler_llvm = ContextOf(driver);
223 CompiledMethod* result = compiler_llvm->CompileNativeMethod(&dex_compilation_unit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700224 return result;
225}
226
Andreas Gampe53c913b2014-08-12 23:19:23 -0700227void compilerLLVMSetBitcodeFileName(const CompilerDriver& driver, const std::string& filename) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700228 ContextOf(driver)->SetBitcodeFileName(filename);
229}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700230
231} // namespace llvm
232} // namespace art
233