blob: e53d63648b3f4dcef98ebc2bd34e25c635db2f4e [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 Marko5c96e6b2013-11-14 15:34:17 +000066QuickCompilerContext::QuickCompilerContext(CompilerDriver& compiler)
Mathieu Chartiere4a50ee2013-11-18 09:49:33 -080067 : inliner_map_(new DexFileToMethodInlinerMap(&compiler)) {
Vladimir Marko5c96e6b2013-11-14 15:34:17 +000068}
69
70QuickCompilerContext::~QuickCompilerContext() {
71}
72
Brian Carlstrom7940e442013-07-12 13:46:57 -070073extern "C" void ArtInitQuickCompilerContext(art::CompilerDriver& compiler) {
74 CHECK(compiler.GetCompilerContext() == NULL);
Vladimir Marko5c96e6b2013-11-14 15:34:17 +000075 compiler.SetCompilerContext(new QuickCompilerContext(compiler));
Brian Carlstrom7940e442013-07-12 13:46:57 -070076}
77
78extern "C" void ArtUnInitQuickCompilerContext(art::CompilerDriver& compiler) {
Vladimir Marko5c96e6b2013-11-14 15:34:17 +000079 delete reinterpret_cast<QuickCompilerContext*>(compiler.GetCompilerContext());
Brian Carlstrom7940e442013-07-12 13:46:57 -070080 compiler.SetCompilerContext(NULL);
81}
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
Ian Rogers677ffa42013-08-21 21:53:05 -0700256#if !defined(ART_USE_PORTABLE_COMPILER)
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700257 if (cu.mir_graph->SkipCompilation(Runtime::Current()->GetCompilerFilter())) {
buzbeeee17e0a2013-07-31 10:47:37 -0700258 return NULL;
259 }
Ian Rogers677ffa42013-08-21 21:53:05 -0700260#endif
buzbeeee17e0a2013-07-31 10:47:37 -0700261
buzbeea61f4952013-08-23 14:27:06 -0700262 cu.NewTimingSplit("MIROpt:CodeLayout");
263
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264 /* Do a code layout pass */
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
275 /* Count uses */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700276 cu.mir_graph->MethodUseCount();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277
278 /* Perform null check elimination */
buzbeea61f4952013-08-23 14:27:06 -0700279 cu.NewTimingSplit("MIROpt:NullCheckElimination");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700280 cu.mir_graph->NullCheckElimination();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281
282 /* Combine basic blocks where possible */
buzbeea61f4952013-08-23 14:27:06 -0700283 cu.NewTimingSplit("MIROpt:BBOpt");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700284 cu.mir_graph->BasicBlockCombine();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285
286 /* Do some basic block optimizations */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700287 cu.mir_graph->BasicBlockOptimization();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700289 if (cu.enable_debug & (1 << kDebugDumpCheckStats)) {
290 cu.mir_graph->DumpCheckStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291 }
292
293 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700294 cu.mir_graph->ShowOpcodeStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700295 }
296
297 /* Set up regLocation[] array to describe values - one for each ssa_name. */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700298 cu.mir_graph->BuildRegLocations();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700299
300 CompiledMethod* result = NULL;
301
302#if defined(ART_USE_PORTABLE_COMPILER)
303 if (compiler_backend == kPortable) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700304 cu.cg.reset(PortableCodeGenerator(&cu, cu.mir_graph.get(), &cu.arena, llvm_compilation_unit));
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700305 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -0700307 switch (compiler.GetInstructionSet()) {
308 case kThumb2:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700309 cu.cg.reset(ArmCodeGenerator(&cu, cu.mir_graph.get(), &cu.arena));
Brian Carlstromf69863b2013-07-17 21:53:13 -0700310 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311 case kMips:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700312 cu.cg.reset(MipsCodeGenerator(&cu, cu.mir_graph.get(), &cu.arena));
Brian Carlstromf69863b2013-07-17 21:53:13 -0700313 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 case kX86:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700315 cu.cg.reset(X86CodeGenerator(&cu, cu.mir_graph.get(), &cu.arena));
Brian Carlstromf69863b2013-07-17 21:53:13 -0700316 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317 default:
318 LOG(FATAL) << "Unexpected instruction set: " << compiler.GetInstructionSet();
319 }
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700320#if defined(ART_USE_PORTABLE_COMPILER)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700321 }
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700322#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700324 cu.cg->Materialize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700325
buzbeea61f4952013-08-23 14:27:06 -0700326 cu.NewTimingSplit("Cleanup");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700327 result = cu.cg->GetCompiledMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328
329 if (result) {
330 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file);
331 } else {
332 VLOG(compiler) << "Deferred " << PrettyMethod(method_idx, dex_file);
333 }
334
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700335 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
336 if (cu.arena.BytesAllocated() > (5 * 1024 *1024)) {
337 MemStats mem_stats(cu.arena);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700338 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
339 }
340 }
341
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700342 if (cu.enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
343 LOG(INFO) << "MEMINFO " << cu.arena.BytesAllocated() << " " << cu.mir_graph->GetNumBlocks()
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344 << " " << PrettyMethod(method_idx, dex_file);
345 }
346
buzbeea61f4952013-08-23 14:27:06 -0700347 cu.EndTiming();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348 return result;
349}
350
351CompiledMethod* CompileOneMethod(CompilerDriver& compiler,
352 const CompilerBackend backend,
353 const DexFile::CodeItem* code_item,
354 uint32_t access_flags,
355 InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700356 uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 uint32_t method_idx,
358 jobject class_loader,
359 const DexFile& dex_file,
360 llvm::LlvmCompilationUnit* llvm_compilation_unit) {
361 return CompileMethod(compiler, backend, code_item, access_flags, invoke_type, class_def_idx,
362 method_idx, class_loader, dex_file
363#if defined(ART_USE_PORTABLE_COMPILER)
364 , llvm_compilation_unit
365#endif
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700366 ); // NOLINT(whitespace/parens)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700367}
368
369} // namespace art
370
371extern "C" art::CompiledMethod*
372 ArtQuickCompileMethod(art::CompilerDriver& compiler,
373 const art::DexFile::CodeItem* code_item,
374 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700375 uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700376 const art::DexFile& dex_file) {
377 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
378 art::CompilerBackend backend = compiler.GetCompilerBackend();
379 return art::CompileOneMethod(compiler, backend, code_item, access_flags, invoke_type,
380 class_def_idx, method_idx, class_loader, dex_file,
381 NULL /* use thread llvm_info */);
382}