blob: b6e062e413bbcf8b4219cc98017b79f336615ae7 [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)
67 : inliner_map_(new DexFileToMethodInlinerMap(&compiler))
68{
69}
70
71QuickCompilerContext::~QuickCompilerContext() {
72}
73
Brian Carlstrom7940e442013-07-12 13:46:57 -070074extern "C" void ArtInitQuickCompilerContext(art::CompilerDriver& compiler) {
75 CHECK(compiler.GetCompilerContext() == NULL);
Vladimir Marko5c96e6b2013-11-14 15:34:17 +000076 compiler.SetCompilerContext(new QuickCompilerContext(compiler));
Brian Carlstrom7940e442013-07-12 13:46:57 -070077}
78
79extern "C" void ArtUnInitQuickCompilerContext(art::CompilerDriver& compiler) {
Vladimir Marko5c96e6b2013-11-14 15:34:17 +000080 delete reinterpret_cast<QuickCompilerContext*>(compiler.GetCompilerContext());
Brian Carlstrom7940e442013-07-12 13:46:57 -070081 compiler.SetCompilerContext(NULL);
82}
83
84/* Default optimizer/debug setting for the compiler. */
Brian Carlstrom7934ac22013-07-26 10:54:15 -070085static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations
Brian Carlstrom7940e442013-07-12 13:46:57 -070086 (1 << kLoadStoreElimination) |
Brian Carlstrom7934ac22013-07-26 10:54:15 -070087 // (1 << kLoadHoisting) |
88 // (1 << kSuppressLoads) |
89 // (1 << kNullCheckElimination) |
90 // (1 << kPromoteRegs) |
91 // (1 << kTrackLiveTemps) |
92 // (1 << kSafeOptimizations) |
93 // (1 << kBBOpt) |
94 // (1 << kMatch) |
95 // (1 << kPromoteCompilerTemps) |
buzbee17189ac2013-11-08 11:07:02 -080096 // (1 << kSuppressExceptionEdges) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070097 0;
98
99static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700100 // (1 << kDebugDisplayMissingTargets) |
101 // (1 << kDebugVerbose) |
102 // (1 << kDebugDumpCFG) |
103 // (1 << kDebugSlowFieldPath) |
104 // (1 << kDebugSlowInvokePath) |
105 // (1 << kDebugSlowStringPath) |
106 // (1 << kDebugSlowestFieldPath) |
107 // (1 << kDebugSlowestStringPath) |
108 // (1 << kDebugExerciseResolveMethod) |
109 // (1 << kDebugVerifyDataflow) |
110 // (1 << kDebugShowMemoryUsage) |
111 // (1 << kDebugShowNops) |
112 // (1 << kDebugCountOpcodes) |
113 // (1 << kDebugDumpCheckStats) |
114 // (1 << kDebugDumpBitcodeFile) |
115 // (1 << kDebugVerifyBitcode) |
116 // (1 << kDebugShowSummaryMemoryUsage) |
buzbeeee17e0a2013-07-31 10:47:37 -0700117 // (1 << kDebugShowFilterStats) |
buzbeea61f4952013-08-23 14:27:06 -0700118 // (1 << kDebugTimings) |
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 0;
120
Vladimir Marko25724ef2013-11-12 15:09:20 +0000121CompilationUnit::CompilationUnit(ArenaPool* pool)
122 : compiler_driver(NULL),
123 class_linker(NULL),
124 dex_file(NULL),
125 class_loader(NULL),
126 class_def_idx(0),
127 method_idx(0),
128 code_item(NULL),
129 access_flags(0),
130 invoke_type(kDirect),
131 shorty(NULL),
132 disable_opt(0),
133 enable_debug(0),
134 verbose(false),
135 compiler_backend(kNoBackend),
136 instruction_set(kNone),
137 num_dalvik_registers(0),
138 insns(NULL),
139 num_ins(0),
140 num_outs(0),
141 num_regs(0),
142 num_compiler_temps(0),
143 compiler_flip_match(false),
144 arena(pool),
145 mir_graph(NULL),
146 cg(NULL),
147 timings("QuickCompiler", true, false) {
148}
149
150CompilationUnit::~CompilationUnit() {
151}
152
buzbeea61f4952013-08-23 14:27:06 -0700153// TODO: Add a cumulative version of logging, and combine with dex2oat --dump-timing
154void CompilationUnit::StartTimingSplit(const char* label) {
155 if (enable_debug & (1 << kDebugTimings)) {
156 timings.StartSplit(label);
157 }
158}
159
160void CompilationUnit::NewTimingSplit(const char* label) {
161 if (enable_debug & (1 << kDebugTimings)) {
162 timings.NewSplit(label);
163 }
164}
165
166void CompilationUnit::EndTiming() {
167 if (enable_debug & (1 << kDebugTimings)) {
168 timings.EndSplit();
169 LOG(INFO) << "TIMINGS " << PrettyMethod(method_idx, *dex_file);
Ian Rogers5fe9af72013-11-14 00:17:20 -0800170 LOG(INFO) << Dumpable<TimingLogger>(timings);
buzbeea61f4952013-08-23 14:27:06 -0700171 }
172}
173
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174static CompiledMethod* CompileMethod(CompilerDriver& compiler,
175 const CompilerBackend compiler_backend,
176 const DexFile::CodeItem* code_item,
177 uint32_t access_flags, InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700178 uint16_t class_def_idx, uint32_t method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179 jobject class_loader, const DexFile& dex_file
180#if defined(ART_USE_PORTABLE_COMPILER)
181 , llvm::LlvmCompilationUnit* llvm_compilation_unit
182#endif
183) {
184 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
buzbeeb48819d2013-09-14 16:15:25 -0700185 if (code_item->insns_size_in_code_units_ >= 0x10000) {
186 LOG(INFO) << "Method size exceeds compiler limits: " << code_item->insns_size_in_code_units_
187 << " in " << PrettyMethod(method_idx, dex_file);
188 return NULL;
189 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700190
191 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700192 CompilationUnit cu(&compiler.GetArenaPool());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700193
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700194 cu.compiler_driver = &compiler;
195 cu.class_linker = class_linker;
196 cu.instruction_set = compiler.GetInstructionSet();
197 cu.compiler_backend = compiler_backend;
198 DCHECK((cu.instruction_set == kThumb2) ||
199 (cu.instruction_set == kX86) ||
200 (cu.instruction_set == kMips));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700201
202
203 /* Adjust this value accordingly once inlining is performed */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700204 cu.num_dalvik_registers = code_item->registers_size_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205 // TODO: set this from command line
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700206 cu.compiler_flip_match = false;
207 bool use_match = !cu.compiler_method_match.empty();
208 bool match = use_match && (cu.compiler_flip_match ^
209 (PrettyMethod(method_idx, dex_file).find(cu.compiler_method_match) !=
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210 std::string::npos));
211 if (!use_match || match) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700212 cu.disable_opt = kCompilerOptimizerDisableFlags;
213 cu.enable_debug = kCompilerDebugFlags;
214 cu.verbose = VLOG_IS_ON(compiler) ||
215 (cu.enable_debug & (1 << kDebugVerbose));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216 }
217
218 /*
219 * TODO: rework handling of optimization and debug flags. Should we split out
220 * MIR and backend flags? Need command-line setting as well.
221 */
222
223 if (compiler_backend == kPortable) {
buzbeeb48819d2013-09-14 16:15:25 -0700224 // Fused long branches not currently useful in bitcode.
buzbee17189ac2013-11-08 11:07:02 -0800225 cu.disable_opt |=
226 (1 << kBranchFusing) |
227 (1 << kSuppressExceptionEdges);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700228 }
229
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700230 if (cu.instruction_set == kMips) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700231 // Disable some optimizations for mips for now
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700232 cu.disable_opt |= (
Brian Carlstrom7940e442013-07-12 13:46:57 -0700233 (1 << kLoadStoreElimination) |
234 (1 << kLoadHoisting) |
235 (1 << kSuppressLoads) |
236 (1 << kNullCheckElimination) |
237 (1 << kPromoteRegs) |
238 (1 << kTrackLiveTemps) |
239 (1 << kSafeOptimizations) |
240 (1 << kBBOpt) |
241 (1 << kMatch) |
242 (1 << kPromoteCompilerTemps));
243 }
244
buzbeea61f4952013-08-23 14:27:06 -0700245 cu.StartTimingSplit("BuildMIRGraph");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700246 cu.mir_graph.reset(new MIRGraph(&cu, &cu.arena));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247
248 /* Gathering opcode stats? */
249 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700250 cu.mir_graph->EnableOpcodeCounting();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251 }
252
253 /* Build the raw MIR graph */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700254 cu.mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700255 class_loader, dex_file);
256
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
buzbeea61f4952013-08-23 14:27:06 -0700263 cu.NewTimingSplit("MIROpt:CodeLayout");
264
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 /* Do a code layout pass */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700266 cu.mir_graph->CodeLayout();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267
268 /* Perform SSA transformation for the whole method */
buzbeea61f4952013-08-23 14:27:06 -0700269 cu.NewTimingSplit("MIROpt:SSATransform");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700270 cu.mir_graph->SSATransformation();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271
272 /* Do constant propagation */
buzbeea61f4952013-08-23 14:27:06 -0700273 cu.NewTimingSplit("MIROpt:ConstantProp");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700274 cu.mir_graph->PropagateConstants();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700275
276 /* Count uses */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700277 cu.mir_graph->MethodUseCount();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278
279 /* Perform null check elimination */
buzbeea61f4952013-08-23 14:27:06 -0700280 cu.NewTimingSplit("MIROpt:NullCheckElimination");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700281 cu.mir_graph->NullCheckElimination();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282
283 /* Combine basic blocks where possible */
buzbeea61f4952013-08-23 14:27:06 -0700284 cu.NewTimingSplit("MIROpt:BBOpt");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700285 cu.mir_graph->BasicBlockCombine();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286
287 /* Do some basic block optimizations */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700288 cu.mir_graph->BasicBlockOptimization();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700289
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700290 if (cu.enable_debug & (1 << kDebugDumpCheckStats)) {
291 cu.mir_graph->DumpCheckStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292 }
293
294 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700295 cu.mir_graph->ShowOpcodeStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296 }
297
298 /* Set up regLocation[] array to describe values - one for each ssa_name. */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700299 cu.mir_graph->BuildRegLocations();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300
301 CompiledMethod* result = NULL;
302
303#if defined(ART_USE_PORTABLE_COMPILER)
304 if (compiler_backend == kPortable) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700305 cu.cg.reset(PortableCodeGenerator(&cu, cu.mir_graph.get(), &cu.arena, llvm_compilation_unit));
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700306 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700307#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -0700308 switch (compiler.GetInstructionSet()) {
309 case kThumb2:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700310 cu.cg.reset(ArmCodeGenerator(&cu, cu.mir_graph.get(), &cu.arena));
Brian Carlstromf69863b2013-07-17 21:53:13 -0700311 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 case kMips:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700313 cu.cg.reset(MipsCodeGenerator(&cu, cu.mir_graph.get(), &cu.arena));
Brian Carlstromf69863b2013-07-17 21:53:13 -0700314 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315 case kX86:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700316 cu.cg.reset(X86CodeGenerator(&cu, cu.mir_graph.get(), &cu.arena));
Brian Carlstromf69863b2013-07-17 21:53:13 -0700317 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318 default:
319 LOG(FATAL) << "Unexpected instruction set: " << compiler.GetInstructionSet();
320 }
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700321#if defined(ART_USE_PORTABLE_COMPILER)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322 }
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700323#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -0700324
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700325 cu.cg->Materialize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326
buzbeea61f4952013-08-23 14:27:06 -0700327 cu.NewTimingSplit("Cleanup");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700328 result = cu.cg->GetCompiledMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700329
330 if (result) {
331 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file);
332 } else {
333 VLOG(compiler) << "Deferred " << PrettyMethod(method_idx, dex_file);
334 }
335
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700336 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
337 if (cu.arena.BytesAllocated() > (5 * 1024 *1024)) {
338 MemStats mem_stats(cu.arena);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
340 }
341 }
342
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700343 if (cu.enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
344 LOG(INFO) << "MEMINFO " << cu.arena.BytesAllocated() << " " << cu.mir_graph->GetNumBlocks()
Brian Carlstrom7940e442013-07-12 13:46:57 -0700345 << " " << PrettyMethod(method_idx, dex_file);
346 }
347
buzbeea61f4952013-08-23 14:27:06 -0700348 cu.EndTiming();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700349 return result;
350}
351
352CompiledMethod* CompileOneMethod(CompilerDriver& compiler,
353 const CompilerBackend backend,
354 const DexFile::CodeItem* code_item,
355 uint32_t access_flags,
356 InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700357 uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700358 uint32_t method_idx,
359 jobject class_loader,
360 const DexFile& dex_file,
361 llvm::LlvmCompilationUnit* llvm_compilation_unit) {
362 return CompileMethod(compiler, backend, code_item, access_flags, invoke_type, class_def_idx,
363 method_idx, class_loader, dex_file
364#if defined(ART_USE_PORTABLE_COMPILER)
365 , llvm_compilation_unit
366#endif
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700367 ); // NOLINT(whitespace/parens)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368}
369
370} // namespace art
371
372extern "C" art::CompiledMethod*
373 ArtQuickCompileMethod(art::CompilerDriver& compiler,
374 const art::DexFile::CodeItem* code_item,
375 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700376 uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377 const art::DexFile& dex_file) {
378 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
379 art::CompilerBackend backend = compiler.GetCompilerBackend();
380 return art::CompileOneMethod(compiler, backend, code_item, access_flags, invoke_type,
381 class_def_idx, method_idx, class_loader, dex_file,
382 NULL /* use thread llvm_info */);
383}