blob: 057177b9984f0e9ef338df26ecf9b7eac3e9c842 [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
34namespace {
35#if !defined(ART_USE_PORTABLE_COMPILER)
36 pthread_once_t llvm_multi_init = PTHREAD_ONCE_INIT;
37#endif
38 void InitializeLLVMForQuick() {
39 ::llvm::llvm_start_multithreaded();
40 }
41}
42
43namespace art {
44namespace llvm {
45::llvm::Module* makeLLVMModuleContents(::llvm::Module* module);
46}
47
48LLVMInfo::LLVMInfo() {
49#if !defined(ART_USE_PORTABLE_COMPILER)
50 pthread_once(&llvm_multi_init, InitializeLLVMForQuick);
51#endif
52 // Create context, module, intrinsic helper & ir builder
53 llvm_context_.reset(new ::llvm::LLVMContext());
54 llvm_module_ = new ::llvm::Module("art", *llvm_context_);
55 ::llvm::StructType::create(*llvm_context_, "JavaObject");
56 art::llvm::makeLLVMModuleContents(llvm_module_);
Brian Carlstromdf629502013-07-17 22:39:56 -070057 intrinsic_helper_.reset(new art::llvm::IntrinsicHelper(*llvm_context_, *llvm_module_));
Brian Carlstrom7940e442013-07-12 13:46:57 -070058 ir_builder_.reset(new art::llvm::IRBuilder(*llvm_context_, *llvm_module_, *intrinsic_helper_));
59}
60
61LLVMInfo::~LLVMInfo() {
62}
63
64extern "C" void ArtInitQuickCompilerContext(art::CompilerDriver& compiler) {
65 CHECK(compiler.GetCompilerContext() == NULL);
66 LLVMInfo* llvm_info = new LLVMInfo();
67 compiler.SetCompilerContext(llvm_info);
68}
69
70extern "C" void ArtUnInitQuickCompilerContext(art::CompilerDriver& compiler) {
71 delete reinterpret_cast<LLVMInfo*>(compiler.GetCompilerContext());
72 compiler.SetCompilerContext(NULL);
73}
74
75/* Default optimizer/debug setting for the compiler. */
Brian Carlstrom7934ac22013-07-26 10:54:15 -070076static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations
Brian Carlstrom7940e442013-07-12 13:46:57 -070077 (1 << kLoadStoreElimination) |
Brian Carlstrom7934ac22013-07-26 10:54:15 -070078 // (1 << kLoadHoisting) |
79 // (1 << kSuppressLoads) |
80 // (1 << kNullCheckElimination) |
81 // (1 << kPromoteRegs) |
82 // (1 << kTrackLiveTemps) |
83 // (1 << kSafeOptimizations) |
84 // (1 << kBBOpt) |
85 // (1 << kMatch) |
86 // (1 << kPromoteCompilerTemps) |
buzbee17189ac2013-11-08 11:07:02 -080087 // (1 << kSuppressExceptionEdges) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070088 0;
89
90static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
Brian Carlstrom7934ac22013-07-26 10:54:15 -070091 // (1 << kDebugDisplayMissingTargets) |
92 // (1 << kDebugVerbose) |
93 // (1 << kDebugDumpCFG) |
94 // (1 << kDebugSlowFieldPath) |
95 // (1 << kDebugSlowInvokePath) |
96 // (1 << kDebugSlowStringPath) |
97 // (1 << kDebugSlowestFieldPath) |
98 // (1 << kDebugSlowestStringPath) |
99 // (1 << kDebugExerciseResolveMethod) |
100 // (1 << kDebugVerifyDataflow) |
101 // (1 << kDebugShowMemoryUsage) |
102 // (1 << kDebugShowNops) |
103 // (1 << kDebugCountOpcodes) |
104 // (1 << kDebugDumpCheckStats) |
105 // (1 << kDebugDumpBitcodeFile) |
106 // (1 << kDebugVerifyBitcode) |
107 // (1 << kDebugShowSummaryMemoryUsage) |
buzbeeee17e0a2013-07-31 10:47:37 -0700108 // (1 << kDebugShowFilterStats) |
buzbeea61f4952013-08-23 14:27:06 -0700109 // (1 << kDebugTimings) |
Brian Carlstrom7940e442013-07-12 13:46:57 -0700110 0;
111
Vladimir Marko25724ef2013-11-12 15:09:20 +0000112CompilationUnit::CompilationUnit(ArenaPool* pool)
113 : compiler_driver(NULL),
114 class_linker(NULL),
115 dex_file(NULL),
116 class_loader(NULL),
117 class_def_idx(0),
118 method_idx(0),
119 code_item(NULL),
120 access_flags(0),
121 invoke_type(kDirect),
122 shorty(NULL),
123 disable_opt(0),
124 enable_debug(0),
125 verbose(false),
126 compiler_backend(kNoBackend),
127 instruction_set(kNone),
128 num_dalvik_registers(0),
129 insns(NULL),
130 num_ins(0),
131 num_outs(0),
132 num_regs(0),
133 num_compiler_temps(0),
134 compiler_flip_match(false),
135 arena(pool),
136 mir_graph(NULL),
137 cg(NULL),
138 timings("QuickCompiler", true, false) {
139}
140
141CompilationUnit::~CompilationUnit() {
142}
143
buzbeea61f4952013-08-23 14:27:06 -0700144// TODO: Add a cumulative version of logging, and combine with dex2oat --dump-timing
145void CompilationUnit::StartTimingSplit(const char* label) {
146 if (enable_debug & (1 << kDebugTimings)) {
147 timings.StartSplit(label);
148 }
149}
150
151void CompilationUnit::NewTimingSplit(const char* label) {
152 if (enable_debug & (1 << kDebugTimings)) {
153 timings.NewSplit(label);
154 }
155}
156
157void CompilationUnit::EndTiming() {
158 if (enable_debug & (1 << kDebugTimings)) {
159 timings.EndSplit();
160 LOG(INFO) << "TIMINGS " << PrettyMethod(method_idx, *dex_file);
161 LOG(INFO) << Dumpable<base::TimingLogger>(timings);
162 }
163}
164
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165static CompiledMethod* CompileMethod(CompilerDriver& compiler,
166 const CompilerBackend compiler_backend,
167 const DexFile::CodeItem* code_item,
168 uint32_t access_flags, InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700169 uint16_t class_def_idx, uint32_t method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170 jobject class_loader, const DexFile& dex_file
171#if defined(ART_USE_PORTABLE_COMPILER)
172 , llvm::LlvmCompilationUnit* llvm_compilation_unit
173#endif
174) {
175 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
buzbeeb48819d2013-09-14 16:15:25 -0700176 if (code_item->insns_size_in_code_units_ >= 0x10000) {
177 LOG(INFO) << "Method size exceeds compiler limits: " << code_item->insns_size_in_code_units_
178 << " in " << PrettyMethod(method_idx, dex_file);
179 return NULL;
180 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181
182 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700183 CompilationUnit cu(&compiler.GetArenaPool());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700185 cu.compiler_driver = &compiler;
186 cu.class_linker = class_linker;
187 cu.instruction_set = compiler.GetInstructionSet();
188 cu.compiler_backend = compiler_backend;
189 DCHECK((cu.instruction_set == kThumb2) ||
190 (cu.instruction_set == kX86) ||
191 (cu.instruction_set == kMips));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192
193
194 /* Adjust this value accordingly once inlining is performed */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700195 cu.num_dalvik_registers = code_item->registers_size_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700196 // TODO: set this from command line
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700197 cu.compiler_flip_match = false;
198 bool use_match = !cu.compiler_method_match.empty();
199 bool match = use_match && (cu.compiler_flip_match ^
200 (PrettyMethod(method_idx, dex_file).find(cu.compiler_method_match) !=
Brian Carlstrom7940e442013-07-12 13:46:57 -0700201 std::string::npos));
202 if (!use_match || match) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700203 cu.disable_opt = kCompilerOptimizerDisableFlags;
204 cu.enable_debug = kCompilerDebugFlags;
205 cu.verbose = VLOG_IS_ON(compiler) ||
206 (cu.enable_debug & (1 << kDebugVerbose));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700207 }
208
209 /*
210 * TODO: rework handling of optimization and debug flags. Should we split out
211 * MIR and backend flags? Need command-line setting as well.
212 */
213
214 if (compiler_backend == kPortable) {
buzbeeb48819d2013-09-14 16:15:25 -0700215 // Fused long branches not currently useful in bitcode.
buzbee17189ac2013-11-08 11:07:02 -0800216 cu.disable_opt |=
217 (1 << kBranchFusing) |
218 (1 << kSuppressExceptionEdges);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700219 }
220
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700221 if (cu.instruction_set == kMips) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700222 // Disable some optimizations for mips for now
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700223 cu.disable_opt |= (
Brian Carlstrom7940e442013-07-12 13:46:57 -0700224 (1 << kLoadStoreElimination) |
225 (1 << kLoadHoisting) |
226 (1 << kSuppressLoads) |
227 (1 << kNullCheckElimination) |
228 (1 << kPromoteRegs) |
229 (1 << kTrackLiveTemps) |
230 (1 << kSafeOptimizations) |
231 (1 << kBBOpt) |
232 (1 << kMatch) |
233 (1 << kPromoteCompilerTemps));
234 }
235
buzbeea61f4952013-08-23 14:27:06 -0700236 cu.StartTimingSplit("BuildMIRGraph");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700237 cu.mir_graph.reset(new MIRGraph(&cu, &cu.arena));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238
239 /* Gathering opcode stats? */
240 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700241 cu.mir_graph->EnableOpcodeCounting();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 }
243
244 /* Build the raw MIR graph */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700245 cu.mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 class_loader, dex_file);
247
Ian Rogers677ffa42013-08-21 21:53:05 -0700248#if !defined(ART_USE_PORTABLE_COMPILER)
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700249 if (cu.mir_graph->SkipCompilation(Runtime::Current()->GetCompilerFilter())) {
buzbeeee17e0a2013-07-31 10:47:37 -0700250 return NULL;
251 }
Ian Rogers677ffa42013-08-21 21:53:05 -0700252#endif
buzbeeee17e0a2013-07-31 10:47:37 -0700253
buzbeea61f4952013-08-23 14:27:06 -0700254 cu.NewTimingSplit("MIROpt:CodeLayout");
255
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256 /* Do a code layout pass */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700257 cu.mir_graph->CodeLayout();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700258
259 /* Perform SSA transformation for the whole method */
buzbeea61f4952013-08-23 14:27:06 -0700260 cu.NewTimingSplit("MIROpt:SSATransform");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700261 cu.mir_graph->SSATransformation();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262
263 /* Do constant propagation */
buzbeea61f4952013-08-23 14:27:06 -0700264 cu.NewTimingSplit("MIROpt:ConstantProp");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700265 cu.mir_graph->PropagateConstants();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266
267 /* Count uses */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700268 cu.mir_graph->MethodUseCount();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700269
270 /* Perform null check elimination */
buzbeea61f4952013-08-23 14:27:06 -0700271 cu.NewTimingSplit("MIROpt:NullCheckElimination");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700272 cu.mir_graph->NullCheckElimination();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273
274 /* Combine basic blocks where possible */
buzbeea61f4952013-08-23 14:27:06 -0700275 cu.NewTimingSplit("MIROpt:BBOpt");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700276 cu.mir_graph->BasicBlockCombine();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277
278 /* Do some basic block optimizations */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700279 cu.mir_graph->BasicBlockOptimization();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700281 if (cu.enable_debug & (1 << kDebugDumpCheckStats)) {
282 cu.mir_graph->DumpCheckStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 }
284
285 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700286 cu.mir_graph->ShowOpcodeStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 }
288
289 /* Set up regLocation[] array to describe values - one for each ssa_name. */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700290 cu.mir_graph->BuildRegLocations();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291
292 CompiledMethod* result = NULL;
293
294#if defined(ART_USE_PORTABLE_COMPILER)
295 if (compiler_backend == kPortable) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700296 cu.cg.reset(PortableCodeGenerator(&cu, cu.mir_graph.get(), &cu.arena, llvm_compilation_unit));
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700297 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700298#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -0700299 switch (compiler.GetInstructionSet()) {
300 case kThumb2:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700301 cu.cg.reset(ArmCodeGenerator(&cu, cu.mir_graph.get(), &cu.arena));
Brian Carlstromf69863b2013-07-17 21:53:13 -0700302 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303 case kMips:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700304 cu.cg.reset(MipsCodeGenerator(&cu, cu.mir_graph.get(), &cu.arena));
Brian Carlstromf69863b2013-07-17 21:53:13 -0700305 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306 case kX86:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700307 cu.cg.reset(X86CodeGenerator(&cu, cu.mir_graph.get(), &cu.arena));
Brian Carlstromf69863b2013-07-17 21:53:13 -0700308 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700309 default:
310 LOG(FATAL) << "Unexpected instruction set: " << compiler.GetInstructionSet();
311 }
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700312#if defined(ART_USE_PORTABLE_COMPILER)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700313 }
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700314#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700316 cu.cg->Materialize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317
buzbeea61f4952013-08-23 14:27:06 -0700318 cu.NewTimingSplit("Cleanup");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700319 result = cu.cg->GetCompiledMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320
321 if (result) {
322 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file);
323 } else {
324 VLOG(compiler) << "Deferred " << PrettyMethod(method_idx, dex_file);
325 }
326
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700327 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
328 if (cu.arena.BytesAllocated() > (5 * 1024 *1024)) {
329 MemStats mem_stats(cu.arena);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
331 }
332 }
333
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700334 if (cu.enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
335 LOG(INFO) << "MEMINFO " << cu.arena.BytesAllocated() << " " << cu.mir_graph->GetNumBlocks()
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336 << " " << PrettyMethod(method_idx, dex_file);
337 }
338
buzbeea61f4952013-08-23 14:27:06 -0700339 cu.EndTiming();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340 return result;
341}
342
343CompiledMethod* CompileOneMethod(CompilerDriver& compiler,
344 const CompilerBackend backend,
345 const DexFile::CodeItem* code_item,
346 uint32_t access_flags,
347 InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700348 uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700349 uint32_t method_idx,
350 jobject class_loader,
351 const DexFile& dex_file,
352 llvm::LlvmCompilationUnit* llvm_compilation_unit) {
353 return CompileMethod(compiler, backend, code_item, access_flags, invoke_type, class_def_idx,
354 method_idx, class_loader, dex_file
355#if defined(ART_USE_PORTABLE_COMPILER)
356 , llvm_compilation_unit
357#endif
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700358 ); // NOLINT(whitespace/parens)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700359}
360
361} // namespace art
362
363extern "C" art::CompiledMethod*
364 ArtQuickCompileMethod(art::CompilerDriver& compiler,
365 const art::DexFile::CodeItem* code_item,
366 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700367 uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 const art::DexFile& dex_file) {
369 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
370 art::CompilerBackend backend = compiler.GetCompilerBackend();
371 return art::CompileOneMethod(compiler, backend, code_item, access_flags, invoke_type,
372 class_def_idx, method_idx, class_loader, dex_file,
373 NULL /* use thread llvm_info */);
374}