blob: 6aabb2ae89407691fdeb04e74993f15fea1a61f8 [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"
buzbeea61f4952013-08-23 14:27:06 -070027#include "base/timing_logger.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028
29#if defined(ART_USE_PORTABLE_COMPILER)
30#include "dex/portable/mir_to_gbc.h"
31#include "llvm/llvm_compilation_unit.h"
32#endif
33
Vladimir Marko5c96e6b2013-11-14 15:34:17 +000034#include "dex/quick/dex_file_to_method_inliner_map.h"
35
Brian Carlstrom7940e442013-07-12 13:46:57 -070036namespace {
37#if !defined(ART_USE_PORTABLE_COMPILER)
38 pthread_once_t llvm_multi_init = PTHREAD_ONCE_INIT;
39#endif
40 void InitializeLLVMForQuick() {
41 ::llvm::llvm_start_multithreaded();
42 }
43}
44
45namespace art {
46namespace llvm {
47::llvm::Module* makeLLVMModuleContents(::llvm::Module* module);
48}
49
50LLVMInfo::LLVMInfo() {
51#if !defined(ART_USE_PORTABLE_COMPILER)
52 pthread_once(&llvm_multi_init, InitializeLLVMForQuick);
53#endif
54 // Create context, module, intrinsic helper & ir builder
55 llvm_context_.reset(new ::llvm::LLVMContext());
56 llvm_module_ = new ::llvm::Module("art", *llvm_context_);
57 ::llvm::StructType::create(*llvm_context_, "JavaObject");
58 art::llvm::makeLLVMModuleContents(llvm_module_);
Brian Carlstromdf629502013-07-17 22:39:56 -070059 intrinsic_helper_.reset(new art::llvm::IntrinsicHelper(*llvm_context_, *llvm_module_));
Brian Carlstrom7940e442013-07-12 13:46:57 -070060 ir_builder_.reset(new art::llvm::IRBuilder(*llvm_context_, *llvm_module_, *intrinsic_helper_));
61}
62
63LLVMInfo::~LLVMInfo() {
64}
65
Vladimir Marko867a2b32013-12-10 13:01:13 +000066QuickCompilerContext::QuickCompilerContext()
67 : inliner_map_(new DexFileToMethodInlinerMap()) {
Vladimir Marko5c96e6b2013-11-14 15:34:17 +000068}
69
70QuickCompilerContext::~QuickCompilerContext() {
71}
72
Vladimir Marko867a2b32013-12-10 13:01:13 +000073extern "C" void ArtInitQuickCompilerContext(art::CompilerDriver& driver) {
74 CHECK(driver.GetCompilerContext() == NULL);
75 driver.SetCompilerContext(new QuickCompilerContext());
Brian Carlstrom7940e442013-07-12 13:46:57 -070076}
77
Vladimir Marko867a2b32013-12-10 13:01:13 +000078extern "C" void ArtUnInitQuickCompilerContext(art::CompilerDriver& driver) {
79 delete reinterpret_cast<QuickCompilerContext*>(driver.GetCompilerContext());
80 driver.SetCompilerContext(NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -070081}
82
83/* Default optimizer/debug setting for the compiler. */
Brian Carlstrom7934ac22013-07-26 10:54:15 -070084static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations
Brian Carlstrom7940e442013-07-12 13:46:57 -070085 (1 << kLoadStoreElimination) |
Brian Carlstrom7934ac22013-07-26 10:54:15 -070086 // (1 << kLoadHoisting) |
87 // (1 << kSuppressLoads) |
88 // (1 << kNullCheckElimination) |
89 // (1 << kPromoteRegs) |
90 // (1 << kTrackLiveTemps) |
91 // (1 << kSafeOptimizations) |
92 // (1 << kBBOpt) |
93 // (1 << kMatch) |
94 // (1 << kPromoteCompilerTemps) |
buzbee17189ac2013-11-08 11:07:02 -080095 // (1 << kSuppressExceptionEdges) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070096 0;
97
98static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
Brian Carlstrom7934ac22013-07-26 10:54:15 -070099 // (1 << kDebugDisplayMissingTargets) |
100 // (1 << kDebugVerbose) |
101 // (1 << kDebugDumpCFG) |
102 // (1 << kDebugSlowFieldPath) |
103 // (1 << kDebugSlowInvokePath) |
104 // (1 << kDebugSlowStringPath) |
105 // (1 << kDebugSlowestFieldPath) |
106 // (1 << kDebugSlowestStringPath) |
107 // (1 << kDebugExerciseResolveMethod) |
108 // (1 << kDebugVerifyDataflow) |
109 // (1 << kDebugShowMemoryUsage) |
110 // (1 << kDebugShowNops) |
111 // (1 << kDebugCountOpcodes) |
112 // (1 << kDebugDumpCheckStats) |
113 // (1 << kDebugDumpBitcodeFile) |
114 // (1 << kDebugVerifyBitcode) |
115 // (1 << kDebugShowSummaryMemoryUsage) |
buzbeeee17e0a2013-07-31 10:47:37 -0700116 // (1 << kDebugShowFilterStats) |
buzbeea61f4952013-08-23 14:27:06 -0700117 // (1 << kDebugTimings) |
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118 0;
119
Vladimir Marko25724ef2013-11-12 15:09:20 +0000120CompilationUnit::CompilationUnit(ArenaPool* pool)
121 : compiler_driver(NULL),
122 class_linker(NULL),
123 dex_file(NULL),
124 class_loader(NULL),
125 class_def_idx(0),
126 method_idx(0),
127 code_item(NULL),
128 access_flags(0),
129 invoke_type(kDirect),
130 shorty(NULL),
131 disable_opt(0),
132 enable_debug(0),
133 verbose(false),
134 compiler_backend(kNoBackend),
135 instruction_set(kNone),
136 num_dalvik_registers(0),
137 insns(NULL),
138 num_ins(0),
139 num_outs(0),
140 num_regs(0),
141 num_compiler_temps(0),
142 compiler_flip_match(false),
143 arena(pool),
144 mir_graph(NULL),
145 cg(NULL),
146 timings("QuickCompiler", true, false) {
147}
148
149CompilationUnit::~CompilationUnit() {
150}
151
buzbeea61f4952013-08-23 14:27:06 -0700152// TODO: Add a cumulative version of logging, and combine with dex2oat --dump-timing
153void CompilationUnit::StartTimingSplit(const char* label) {
154 if (enable_debug & (1 << kDebugTimings)) {
155 timings.StartSplit(label);
156 }
157}
158
159void CompilationUnit::NewTimingSplit(const char* label) {
160 if (enable_debug & (1 << kDebugTimings)) {
161 timings.NewSplit(label);
162 }
163}
164
165void CompilationUnit::EndTiming() {
166 if (enable_debug & (1 << kDebugTimings)) {
167 timings.EndSplit();
168 LOG(INFO) << "TIMINGS " << PrettyMethod(method_idx, *dex_file);
Ian Rogers5fe9af72013-11-14 00:17:20 -0800169 LOG(INFO) << Dumpable<TimingLogger>(timings);
buzbeea61f4952013-08-23 14:27:06 -0700170 }
171}
172
Brian Carlstrom7940e442013-07-12 13:46:57 -0700173static CompiledMethod* CompileMethod(CompilerDriver& compiler,
174 const CompilerBackend compiler_backend,
175 const DexFile::CodeItem* code_item,
176 uint32_t access_flags, InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700177 uint16_t class_def_idx, uint32_t method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178 jobject class_loader, const DexFile& dex_file
179#if defined(ART_USE_PORTABLE_COMPILER)
180 , llvm::LlvmCompilationUnit* llvm_compilation_unit
181#endif
182) {
183 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
buzbeeb48819d2013-09-14 16:15:25 -0700184 if (code_item->insns_size_in_code_units_ >= 0x10000) {
185 LOG(INFO) << "Method size exceeds compiler limits: " << code_item->insns_size_in_code_units_
186 << " in " << PrettyMethod(method_idx, dex_file);
187 return NULL;
188 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700189
190 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700191 CompilationUnit cu(&compiler.GetArenaPool());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700193 cu.compiler_driver = &compiler;
194 cu.class_linker = class_linker;
195 cu.instruction_set = compiler.GetInstructionSet();
196 cu.compiler_backend = compiler_backend;
197 DCHECK((cu.instruction_set == kThumb2) ||
198 (cu.instruction_set == kX86) ||
199 (cu.instruction_set == kMips));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200
201
202 /* Adjust this value accordingly once inlining is performed */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700203 cu.num_dalvik_registers = code_item->registers_size_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700204 // TODO: set this from command line
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700205 cu.compiler_flip_match = false;
206 bool use_match = !cu.compiler_method_match.empty();
207 bool match = use_match && (cu.compiler_flip_match ^
208 (PrettyMethod(method_idx, dex_file).find(cu.compiler_method_match) !=
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 std::string::npos));
210 if (!use_match || match) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700211 cu.disable_opt = kCompilerOptimizerDisableFlags;
212 cu.enable_debug = kCompilerDebugFlags;
213 cu.verbose = VLOG_IS_ON(compiler) ||
214 (cu.enable_debug & (1 << kDebugVerbose));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700215 }
216
217 /*
218 * TODO: rework handling of optimization and debug flags. Should we split out
219 * MIR and backend flags? Need command-line setting as well.
220 */
221
222 if (compiler_backend == kPortable) {
buzbeeb48819d2013-09-14 16:15:25 -0700223 // Fused long branches not currently useful in bitcode.
buzbee17189ac2013-11-08 11:07:02 -0800224 cu.disable_opt |=
225 (1 << kBranchFusing) |
226 (1 << kSuppressExceptionEdges);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227 }
228
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700229 if (cu.instruction_set == kMips) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700230 // Disable some optimizations for mips for now
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700231 cu.disable_opt |= (
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232 (1 << kLoadStoreElimination) |
233 (1 << kLoadHoisting) |
234 (1 << kSuppressLoads) |
235 (1 << kNullCheckElimination) |
236 (1 << kPromoteRegs) |
237 (1 << kTrackLiveTemps) |
238 (1 << kSafeOptimizations) |
239 (1 << kBBOpt) |
240 (1 << kMatch) |
241 (1 << kPromoteCompilerTemps));
242 }
243
buzbeea61f4952013-08-23 14:27:06 -0700244 cu.StartTimingSplit("BuildMIRGraph");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700245 cu.mir_graph.reset(new MIRGraph(&cu, &cu.arena));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246
247 /* Gathering opcode stats? */
248 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700249 cu.mir_graph->EnableOpcodeCounting();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700250 }
251
252 /* Build the raw MIR graph */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700253 cu.mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700254 class_loader, dex_file);
255
buzbee1da1e2f2013-11-15 13:37:01 -0800256 cu.NewTimingSplit("MIROpt:CheckFilters");
Ian Rogers677ffa42013-08-21 21:53:05 -0700257#if !defined(ART_USE_PORTABLE_COMPILER)
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700258 if (cu.mir_graph->SkipCompilation(Runtime::Current()->GetCompilerFilter())) {
buzbeeee17e0a2013-07-31 10:47:37 -0700259 return NULL;
260 }
Ian Rogers677ffa42013-08-21 21:53:05 -0700261#endif
buzbeeee17e0a2013-07-31 10:47:37 -0700262
Brian Carlstrom7940e442013-07-12 13:46:57 -0700263 /* Do a code layout pass */
buzbee1da1e2f2013-11-15 13:37:01 -0800264 cu.NewTimingSplit("MIROpt:CodeLayout");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700265 cu.mir_graph->CodeLayout();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266
267 /* Perform SSA transformation for the whole method */
buzbeea61f4952013-08-23 14:27:06 -0700268 cu.NewTimingSplit("MIROpt:SSATransform");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700269 cu.mir_graph->SSATransformation();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270
271 /* Do constant propagation */
buzbeea61f4952013-08-23 14:27:06 -0700272 cu.NewTimingSplit("MIROpt:ConstantProp");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700273 cu.mir_graph->PropagateConstants();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274
buzbee1da1e2f2013-11-15 13:37:01 -0800275 cu.NewTimingSplit("MIROpt:InitRegLoc");
276 cu.mir_graph->InitRegLocations();
277
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 /* Count uses */
buzbee1da1e2f2013-11-15 13:37:01 -0800279 cu.NewTimingSplit("MIROpt:UseCount");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700280 cu.mir_graph->MethodUseCount();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281
buzbee1da1e2f2013-11-15 13:37:01 -0800282 /* Perform null check elimination and type inference*/
283 cu.NewTimingSplit("MIROpt:NCE_TypeInference");
284 cu.mir_graph->NullCheckEliminationAndTypeInference();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285
286 /* Combine basic blocks where possible */
buzbee1da1e2f2013-11-15 13:37:01 -0800287 cu.NewTimingSplit("MIROpt:BBCombine");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700288 cu.mir_graph->BasicBlockCombine();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700289
290 /* Do some basic block optimizations */
buzbee1da1e2f2013-11-15 13:37:01 -0800291 cu.NewTimingSplit("MIROpt:BBOpt");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700292 cu.mir_graph->BasicBlockOptimization();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700294 if (cu.enable_debug & (1 << kDebugDumpCheckStats)) {
295 cu.mir_graph->DumpCheckStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296 }
297
298 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700299 cu.mir_graph->ShowOpcodeStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300 }
301
buzbee1da1e2f2013-11-15 13:37:01 -0800302 /* Reassociate sreg names with original Dalvik vreg names. */
303 cu.mir_graph->RemapRegLocations();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700304
305 CompiledMethod* result = NULL;
306
307#if defined(ART_USE_PORTABLE_COMPILER)
308 if (compiler_backend == kPortable) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700309 cu.cg.reset(PortableCodeGenerator(&cu, cu.mir_graph.get(), &cu.arena, llvm_compilation_unit));
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700310 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 switch (compiler.GetInstructionSet()) {
313 case kThumb2:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700314 cu.cg.reset(ArmCodeGenerator(&cu, cu.mir_graph.get(), &cu.arena));
Brian Carlstromf69863b2013-07-17 21:53:13 -0700315 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316 case kMips:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700317 cu.cg.reset(MipsCodeGenerator(&cu, cu.mir_graph.get(), &cu.arena));
Brian Carlstromf69863b2013-07-17 21:53:13 -0700318 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319 case kX86:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700320 cu.cg.reset(X86CodeGenerator(&cu, cu.mir_graph.get(), &cu.arena));
Brian Carlstromf69863b2013-07-17 21:53:13 -0700321 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322 default:
323 LOG(FATAL) << "Unexpected instruction set: " << compiler.GetInstructionSet();
324 }
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700325#if defined(ART_USE_PORTABLE_COMPILER)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 }
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700327#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700329 cu.cg->Materialize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330
buzbee1da1e2f2013-11-15 13:37:01 -0800331 cu.NewTimingSplit("Dedupe"); /* deduping takes up the vast majority of time in GetCompiledMethod(). */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700332 result = cu.cg->GetCompiledMethod();
buzbee1da1e2f2013-11-15 13:37:01 -0800333 cu.NewTimingSplit("Cleanup");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700334
335 if (result) {
336 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file);
337 } else {
338 VLOG(compiler) << "Deferred " << PrettyMethod(method_idx, dex_file);
339 }
340
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700341 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
342 if (cu.arena.BytesAllocated() > (5 * 1024 *1024)) {
343 MemStats mem_stats(cu.arena);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
345 }
346 }
347
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700348 if (cu.enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
349 LOG(INFO) << "MEMINFO " << cu.arena.BytesAllocated() << " " << cu.mir_graph->GetNumBlocks()
Brian Carlstrom7940e442013-07-12 13:46:57 -0700350 << " " << PrettyMethod(method_idx, dex_file);
351 }
352
buzbeea61f4952013-08-23 14:27:06 -0700353 cu.EndTiming();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700354 return result;
355}
356
357CompiledMethod* CompileOneMethod(CompilerDriver& compiler,
358 const CompilerBackend backend,
359 const DexFile::CodeItem* code_item,
360 uint32_t access_flags,
361 InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700362 uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700363 uint32_t method_idx,
364 jobject class_loader,
365 const DexFile& dex_file,
366 llvm::LlvmCompilationUnit* llvm_compilation_unit) {
367 return CompileMethod(compiler, backend, code_item, access_flags, invoke_type, class_def_idx,
368 method_idx, class_loader, dex_file
369#if defined(ART_USE_PORTABLE_COMPILER)
370 , llvm_compilation_unit
371#endif
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700372 ); // NOLINT(whitespace/parens)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373}
374
375} // namespace art
376
377extern "C" art::CompiledMethod*
378 ArtQuickCompileMethod(art::CompilerDriver& compiler,
379 const art::DexFile::CodeItem* code_item,
380 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700381 uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382 const art::DexFile& dex_file) {
383 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
384 art::CompilerBackend backend = compiler.GetCompilerBackend();
385 return art::CompileOneMethod(compiler, backend, code_item, access_flags, invoke_type,
386 class_def_idx, method_idx, class_loader, dex_file,
387 NULL /* use thread llvm_info */);
388}