blob: 2952570436a168c1edc16b98fdb9b2c878c4352d [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 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 <llvm/Support/Threading.h>
18
19#include "compiler_internals.h"
20#include "driver/compiler_driver.h"
21#include "dataflow_iterator-inl.h"
22#include "leb128.h"
23#include "mirror/object.h"
24#include "runtime.h"
25#include "backend.h"
26#include "base/logging.h"
27
28#if defined(ART_USE_PORTABLE_COMPILER)
29#include "dex/portable/mir_to_gbc.h"
30#include "llvm/llvm_compilation_unit.h"
31#endif
32
33namespace {
34#if !defined(ART_USE_PORTABLE_COMPILER)
35 pthread_once_t llvm_multi_init = PTHREAD_ONCE_INIT;
36#endif
37 void InitializeLLVMForQuick() {
38 ::llvm::llvm_start_multithreaded();
39 }
40}
41
42namespace art {
43namespace llvm {
44::llvm::Module* makeLLVMModuleContents(::llvm::Module* module);
45}
46
47LLVMInfo::LLVMInfo() {
48#if !defined(ART_USE_PORTABLE_COMPILER)
49 pthread_once(&llvm_multi_init, InitializeLLVMForQuick);
50#endif
51 // Create context, module, intrinsic helper & ir builder
52 llvm_context_.reset(new ::llvm::LLVMContext());
53 llvm_module_ = new ::llvm::Module("art", *llvm_context_);
54 ::llvm::StructType::create(*llvm_context_, "JavaObject");
55 art::llvm::makeLLVMModuleContents(llvm_module_);
Brian Carlstromdf629502013-07-17 22:39:56 -070056 intrinsic_helper_.reset(new art::llvm::IntrinsicHelper(*llvm_context_, *llvm_module_));
Brian Carlstrom7940e442013-07-12 13:46:57 -070057 ir_builder_.reset(new art::llvm::IRBuilder(*llvm_context_, *llvm_module_, *intrinsic_helper_));
58}
59
60LLVMInfo::~LLVMInfo() {
61}
62
63extern "C" void ArtInitQuickCompilerContext(art::CompilerDriver& compiler) {
64 CHECK(compiler.GetCompilerContext() == NULL);
65 LLVMInfo* llvm_info = new LLVMInfo();
66 compiler.SetCompilerContext(llvm_info);
67}
68
69extern "C" void ArtUnInitQuickCompilerContext(art::CompilerDriver& compiler) {
70 delete reinterpret_cast<LLVMInfo*>(compiler.GetCompilerContext());
71 compiler.SetCompilerContext(NULL);
72}
73
74/* Default optimizer/debug setting for the compiler. */
Brian Carlstrom7934ac22013-07-26 10:54:15 -070075static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations
Brian Carlstrom7940e442013-07-12 13:46:57 -070076 (1 << kLoadStoreElimination) |
Brian Carlstrom7934ac22013-07-26 10:54:15 -070077 // (1 << kLoadHoisting) |
78 // (1 << kSuppressLoads) |
79 // (1 << kNullCheckElimination) |
80 // (1 << kPromoteRegs) |
81 // (1 << kTrackLiveTemps) |
82 // (1 << kSafeOptimizations) |
83 // (1 << kBBOpt) |
84 // (1 << kMatch) |
85 // (1 << kPromoteCompilerTemps) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070086 0;
87
88static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
Brian Carlstrom7934ac22013-07-26 10:54:15 -070089 // (1 << kDebugDisplayMissingTargets) |
90 // (1 << kDebugVerbose) |
91 // (1 << kDebugDumpCFG) |
92 // (1 << kDebugSlowFieldPath) |
93 // (1 << kDebugSlowInvokePath) |
94 // (1 << kDebugSlowStringPath) |
95 // (1 << kDebugSlowestFieldPath) |
96 // (1 << kDebugSlowestStringPath) |
97 // (1 << kDebugExerciseResolveMethod) |
98 // (1 << kDebugVerifyDataflow) |
99 // (1 << kDebugShowMemoryUsage) |
100 // (1 << kDebugShowNops) |
101 // (1 << kDebugCountOpcodes) |
102 // (1 << kDebugDumpCheckStats) |
103 // (1 << kDebugDumpBitcodeFile) |
104 // (1 << kDebugVerifyBitcode) |
105 // (1 << kDebugShowSummaryMemoryUsage) |
buzbeeee17e0a2013-07-31 10:47:37 -0700106 // (1 << kDebugShowFilterStats) |
Brian Carlstrom7940e442013-07-12 13:46:57 -0700107 0;
108
109static CompiledMethod* CompileMethod(CompilerDriver& compiler,
110 const CompilerBackend compiler_backend,
111 const DexFile::CodeItem* code_item,
112 uint32_t access_flags, InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700113 uint16_t class_def_idx, uint32_t method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114 jobject class_loader, const DexFile& dex_file
115#if defined(ART_USE_PORTABLE_COMPILER)
116 , llvm::LlvmCompilationUnit* llvm_compilation_unit
117#endif
118) {
119 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
buzbeeb48819d2013-09-14 16:15:25 -0700120 if (code_item->insns_size_in_code_units_ >= 0x10000) {
121 LOG(INFO) << "Method size exceeds compiler limits: " << code_item->insns_size_in_code_units_
122 << " in " << PrettyMethod(method_idx, dex_file);
123 return NULL;
124 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700125
126 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700127 CompilationUnit cu(&compiler.GetArenaPool());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700128
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700129 cu.compiler_driver = &compiler;
130 cu.class_linker = class_linker;
131 cu.instruction_set = compiler.GetInstructionSet();
132 cu.compiler_backend = compiler_backend;
133 DCHECK((cu.instruction_set == kThumb2) ||
134 (cu.instruction_set == kX86) ||
135 (cu.instruction_set == kMips));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136
137
138 /* Adjust this value accordingly once inlining is performed */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700139 cu.num_dalvik_registers = code_item->registers_size_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 // TODO: set this from command line
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700141 cu.compiler_flip_match = false;
142 bool use_match = !cu.compiler_method_match.empty();
143 bool match = use_match && (cu.compiler_flip_match ^
144 (PrettyMethod(method_idx, dex_file).find(cu.compiler_method_match) !=
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145 std::string::npos));
146 if (!use_match || match) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700147 cu.disable_opt = kCompilerOptimizerDisableFlags;
148 cu.enable_debug = kCompilerDebugFlags;
149 cu.verbose = VLOG_IS_ON(compiler) ||
150 (cu.enable_debug & (1 << kDebugVerbose));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 }
152
153 /*
154 * TODO: rework handling of optimization and debug flags. Should we split out
155 * MIR and backend flags? Need command-line setting as well.
156 */
157
158 if (compiler_backend == kPortable) {
buzbeeb48819d2013-09-14 16:15:25 -0700159 // Fused long branches not currently useful in bitcode.
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700160 cu.disable_opt |= (1 << kBranchFusing);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161 }
162
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700163 if (cu.instruction_set == kMips) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700164 // Disable some optimizations for mips for now
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700165 cu.disable_opt |= (
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166 (1 << kLoadStoreElimination) |
167 (1 << kLoadHoisting) |
168 (1 << kSuppressLoads) |
169 (1 << kNullCheckElimination) |
170 (1 << kPromoteRegs) |
171 (1 << kTrackLiveTemps) |
172 (1 << kSafeOptimizations) |
173 (1 << kBBOpt) |
174 (1 << kMatch) |
175 (1 << kPromoteCompilerTemps));
176 }
177
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700178 cu.mir_graph.reset(new MIRGraph(&cu, &cu.arena));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179
180 /* Gathering opcode stats? */
181 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700182 cu.mir_graph->EnableOpcodeCounting();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183 }
184
185 /* Build the raw MIR graph */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700186 cu.mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700187 class_loader, dex_file);
188
Ian Rogers677ffa42013-08-21 21:53:05 -0700189#if !defined(ART_USE_PORTABLE_COMPILER)
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700190 if (cu.mir_graph->SkipCompilation(Runtime::Current()->GetCompilerFilter())) {
buzbeeee17e0a2013-07-31 10:47:37 -0700191 return NULL;
192 }
Ian Rogers677ffa42013-08-21 21:53:05 -0700193#endif
buzbeeee17e0a2013-07-31 10:47:37 -0700194
Brian Carlstrom7940e442013-07-12 13:46:57 -0700195 /* Do a code layout pass */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700196 cu.mir_graph->CodeLayout();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700197
198 /* Perform SSA transformation for the whole method */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700199 cu.mir_graph->SSATransformation();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200
201 /* Do constant propagation */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700202 cu.mir_graph->PropagateConstants();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700203
204 /* Count uses */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700205 cu.mir_graph->MethodUseCount();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700206
207 /* Perform null check elimination */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700208 cu.mir_graph->NullCheckElimination();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209
210 /* Combine basic blocks where possible */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700211 cu.mir_graph->BasicBlockCombine();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700212
213 /* Do some basic block optimizations */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700214 cu.mir_graph->BasicBlockOptimization();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700215
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700216 if (cu.enable_debug & (1 << kDebugDumpCheckStats)) {
217 cu.mir_graph->DumpCheckStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218 }
219
220 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700221 cu.mir_graph->ShowOpcodeStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700222 }
223
224 /* Set up regLocation[] array to describe values - one for each ssa_name. */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700225 cu.mir_graph->BuildRegLocations();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700226
227 CompiledMethod* result = NULL;
228
229#if defined(ART_USE_PORTABLE_COMPILER)
230 if (compiler_backend == kPortable) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700231 cu.cg.reset(PortableCodeGenerator(&cu, cu.mir_graph.get(), &cu.arena, llvm_compilation_unit));
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700232 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700233#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234 switch (compiler.GetInstructionSet()) {
235 case kThumb2:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700236 cu.cg.reset(ArmCodeGenerator(&cu, cu.mir_graph.get(), &cu.arena));
Brian Carlstromf69863b2013-07-17 21:53:13 -0700237 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238 case kMips:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700239 cu.cg.reset(MipsCodeGenerator(&cu, cu.mir_graph.get(), &cu.arena));
Brian Carlstromf69863b2013-07-17 21:53:13 -0700240 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 case kX86:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700242 cu.cg.reset(X86CodeGenerator(&cu, cu.mir_graph.get(), &cu.arena));
Brian Carlstromf69863b2013-07-17 21:53:13 -0700243 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700244 default:
245 LOG(FATAL) << "Unexpected instruction set: " << compiler.GetInstructionSet();
246 }
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700247#if defined(ART_USE_PORTABLE_COMPILER)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 }
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700249#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -0700250
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700251 cu.cg->Materialize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700253 result = cu.cg->GetCompiledMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700254
255 if (result) {
256 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file);
257 } else {
258 VLOG(compiler) << "Deferred " << PrettyMethod(method_idx, dex_file);
259 }
260
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700261 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
262 if (cu.arena.BytesAllocated() > (5 * 1024 *1024)) {
263 MemStats mem_stats(cu.arena);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
265 }
266 }
267
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700268 if (cu.enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
269 LOG(INFO) << "MEMINFO " << cu.arena.BytesAllocated() << " " << cu.mir_graph->GetNumBlocks()
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270 << " " << PrettyMethod(method_idx, dex_file);
271 }
272
273 return result;
274}
275
276CompiledMethod* CompileOneMethod(CompilerDriver& compiler,
277 const CompilerBackend backend,
278 const DexFile::CodeItem* code_item,
279 uint32_t access_flags,
280 InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700281 uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282 uint32_t method_idx,
283 jobject class_loader,
284 const DexFile& dex_file,
285 llvm::LlvmCompilationUnit* llvm_compilation_unit) {
286 return CompileMethod(compiler, backend, code_item, access_flags, invoke_type, class_def_idx,
287 method_idx, class_loader, dex_file
288#if defined(ART_USE_PORTABLE_COMPILER)
289 , llvm_compilation_unit
290#endif
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700291 ); // NOLINT(whitespace/parens)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292}
293
294} // namespace art
295
296extern "C" art::CompiledMethod*
297 ArtQuickCompileMethod(art::CompilerDriver& compiler,
298 const art::DexFile::CodeItem* code_item,
299 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700300 uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700301 const art::DexFile& dex_file) {
302 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
303 art::CompilerBackend backend = compiler.GetCompilerBackend();
304 return art::CompileOneMethod(compiler, backend, code_item, access_flags, invoke_type,
305 class_def_idx, method_idx, class_loader, dex_file,
306 NULL /* use thread llvm_info */);
307}