blob: 364a8bc55b00b17ec6e9639ec053b76dec1c101d [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"
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080024#include "pass_driver.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include "runtime.h"
26#include "backend.h"
27#include "base/logging.h"
buzbeea61f4952013-08-23 14:27:06 -070028#include "base/timing_logger.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070029
30#if defined(ART_USE_PORTABLE_COMPILER)
31#include "dex/portable/mir_to_gbc.h"
32#include "llvm/llvm_compilation_unit.h"
33#endif
34
Vladimir Marko5c96e6b2013-11-14 15:34:17 +000035#include "dex/quick/dex_file_to_method_inliner_map.h"
36
Brian Carlstrom7940e442013-07-12 13:46:57 -070037namespace {
38#if !defined(ART_USE_PORTABLE_COMPILER)
39 pthread_once_t llvm_multi_init = PTHREAD_ONCE_INIT;
40#endif
41 void InitializeLLVMForQuick() {
42 ::llvm::llvm_start_multithreaded();
43 }
44}
45
46namespace art {
47namespace llvm {
48::llvm::Module* makeLLVMModuleContents(::llvm::Module* module);
49}
50
51LLVMInfo::LLVMInfo() {
52#if !defined(ART_USE_PORTABLE_COMPILER)
53 pthread_once(&llvm_multi_init, InitializeLLVMForQuick);
54#endif
55 // Create context, module, intrinsic helper & ir builder
56 llvm_context_.reset(new ::llvm::LLVMContext());
57 llvm_module_ = new ::llvm::Module("art", *llvm_context_);
58 ::llvm::StructType::create(*llvm_context_, "JavaObject");
59 art::llvm::makeLLVMModuleContents(llvm_module_);
Brian Carlstromdf629502013-07-17 22:39:56 -070060 intrinsic_helper_.reset(new art::llvm::IntrinsicHelper(*llvm_context_, *llvm_module_));
Brian Carlstrom7940e442013-07-12 13:46:57 -070061 ir_builder_.reset(new art::llvm::IRBuilder(*llvm_context_, *llvm_module_, *intrinsic_helper_));
62}
63
64LLVMInfo::~LLVMInfo() {
65}
66
Vladimir Marko867a2b32013-12-10 13:01:13 +000067extern "C" void ArtInitQuickCompilerContext(art::CompilerDriver& driver) {
68 CHECK(driver.GetCompilerContext() == NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -070069}
70
Vladimir Marko867a2b32013-12-10 13:01:13 +000071extern "C" void ArtUnInitQuickCompilerContext(art::CompilerDriver& driver) {
Vladimir Marko5816ed42013-11-27 17:04:20 +000072 CHECK(driver.GetCompilerContext() == NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -070073}
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 -0700144void CompilationUnit::StartTimingSplit(const char* label) {
Nicolas Geoffraydf013172014-01-13 10:11:18 +0000145 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700146 timings.StartSplit(label);
147 }
148}
149
150void CompilationUnit::NewTimingSplit(const char* label) {
Nicolas Geoffraydf013172014-01-13 10:11:18 +0000151 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700152 timings.NewSplit(label);
153 }
154}
155
156void CompilationUnit::EndTiming() {
Nicolas Geoffraydf013172014-01-13 10:11:18 +0000157 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700158 timings.EndSplit();
Nicolas Geoffraydf013172014-01-13 10:11:18 +0000159 if (enable_debug & (1 << kDebugTimings)) {
160 LOG(INFO) << "TIMINGS " << PrettyMethod(method_idx, *dex_file);
161 LOG(INFO) << Dumpable<TimingLogger>(timings);
162 }
buzbeea61f4952013-08-23 14:27:06 -0700163 }
164}
165
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166static CompiledMethod* CompileMethod(CompilerDriver& compiler,
167 const CompilerBackend compiler_backend,
168 const DexFile::CodeItem* code_item,
169 uint32_t access_flags, InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700170 uint16_t class_def_idx, uint32_t method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171 jobject class_loader, const DexFile& dex_file
172#if defined(ART_USE_PORTABLE_COMPILER)
173 , llvm::LlvmCompilationUnit* llvm_compilation_unit
174#endif
175) {
176 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
buzbeeb48819d2013-09-14 16:15:25 -0700177 if (code_item->insns_size_in_code_units_ >= 0x10000) {
178 LOG(INFO) << "Method size exceeds compiler limits: " << code_item->insns_size_in_code_units_
179 << " in " << PrettyMethod(method_idx, dex_file);
180 return NULL;
181 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182
183 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700184 CompilationUnit cu(&compiler.GetArenaPool());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700185
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700186 cu.compiler_driver = &compiler;
187 cu.class_linker = class_linker;
188 cu.instruction_set = compiler.GetInstructionSet();
189 cu.compiler_backend = compiler_backend;
190 DCHECK((cu.instruction_set == kThumb2) ||
191 (cu.instruction_set == kX86) ||
192 (cu.instruction_set == kMips));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700193
194
195 /* Adjust this value accordingly once inlining is performed */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700196 cu.num_dalvik_registers = code_item->registers_size_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700197 // TODO: set this from command line
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700198 cu.compiler_flip_match = false;
199 bool use_match = !cu.compiler_method_match.empty();
200 bool match = use_match && (cu.compiler_flip_match ^
201 (PrettyMethod(method_idx, dex_file).find(cu.compiler_method_match) !=
Brian Carlstrom7940e442013-07-12 13:46:57 -0700202 std::string::npos));
203 if (!use_match || match) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700204 cu.disable_opt = kCompilerOptimizerDisableFlags;
205 cu.enable_debug = kCompilerDebugFlags;
206 cu.verbose = VLOG_IS_ON(compiler) ||
207 (cu.enable_debug & (1 << kDebugVerbose));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700208 }
209
210 /*
211 * TODO: rework handling of optimization and debug flags. Should we split out
212 * MIR and backend flags? Need command-line setting as well.
213 */
214
215 if (compiler_backend == kPortable) {
buzbeeb48819d2013-09-14 16:15:25 -0700216 // Fused long branches not currently useful in bitcode.
buzbee17189ac2013-11-08 11:07:02 -0800217 cu.disable_opt |=
218 (1 << kBranchFusing) |
219 (1 << kSuppressExceptionEdges);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700220 }
221
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700222 if (cu.instruction_set == kMips) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223 // Disable some optimizations for mips for now
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700224 cu.disable_opt |= (
Brian Carlstrom7940e442013-07-12 13:46:57 -0700225 (1 << kLoadStoreElimination) |
226 (1 << kLoadHoisting) |
227 (1 << kSuppressLoads) |
228 (1 << kNullCheckElimination) |
229 (1 << kPromoteRegs) |
230 (1 << kTrackLiveTemps) |
231 (1 << kSafeOptimizations) |
232 (1 << kBBOpt) |
233 (1 << kMatch) |
234 (1 << kPromoteCompilerTemps));
235 }
236
buzbeea61f4952013-08-23 14:27:06 -0700237 cu.StartTimingSplit("BuildMIRGraph");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700238 cu.mir_graph.reset(new MIRGraph(&cu, &cu.arena));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700239
240 /* Gathering opcode stats? */
241 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700242 cu.mir_graph->EnableOpcodeCounting();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700243 }
244
245 /* Build the raw MIR graph */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700246 cu.mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 class_loader, dex_file);
248
buzbee1da1e2f2013-11-15 13:37:01 -0800249 cu.NewTimingSplit("MIROpt:CheckFilters");
Ian Rogers677ffa42013-08-21 21:53:05 -0700250#if !defined(ART_USE_PORTABLE_COMPILER)
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700251 if (cu.mir_graph->SkipCompilation(Runtime::Current()->GetCompilerFilter())) {
buzbeeee17e0a2013-07-31 10:47:37 -0700252 return NULL;
253 }
Ian Rogers677ffa42013-08-21 21:53:05 -0700254#endif
buzbeeee17e0a2013-07-31 10:47:37 -0700255
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800256 /* Create the pass driver and launch it */
257 PassDriver driver(&cu);
258 driver.Launch();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700260 if (cu.enable_debug & (1 << kDebugDumpCheckStats)) {
261 cu.mir_graph->DumpCheckStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 }
263
264 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700265 cu.mir_graph->ShowOpcodeStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 }
267
buzbee1da1e2f2013-11-15 13:37:01 -0800268 /* Reassociate sreg names with original Dalvik vreg names. */
269 cu.mir_graph->RemapRegLocations();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270
271 CompiledMethod* result = NULL;
272
273#if defined(ART_USE_PORTABLE_COMPILER)
274 if (compiler_backend == kPortable) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700275 cu.cg.reset(PortableCodeGenerator(&cu, cu.mir_graph.get(), &cu.arena, llvm_compilation_unit));
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700276 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 switch (compiler.GetInstructionSet()) {
279 case kThumb2:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700280 cu.cg.reset(ArmCodeGenerator(&cu, cu.mir_graph.get(), &cu.arena));
Brian Carlstromf69863b2013-07-17 21:53:13 -0700281 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282 case kMips:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700283 cu.cg.reset(MipsCodeGenerator(&cu, cu.mir_graph.get(), &cu.arena));
Brian Carlstromf69863b2013-07-17 21:53:13 -0700284 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 case kX86:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700286 cu.cg.reset(X86CodeGenerator(&cu, cu.mir_graph.get(), &cu.arena));
Brian Carlstromf69863b2013-07-17 21:53:13 -0700287 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288 default:
289 LOG(FATAL) << "Unexpected instruction set: " << compiler.GetInstructionSet();
290 }
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700291#if defined(ART_USE_PORTABLE_COMPILER)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292 }
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700293#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -0700294
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700295 cu.cg->Materialize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296
buzbee1da1e2f2013-11-15 13:37:01 -0800297 cu.NewTimingSplit("Dedupe"); /* deduping takes up the vast majority of time in GetCompiledMethod(). */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700298 result = cu.cg->GetCompiledMethod();
buzbee1da1e2f2013-11-15 13:37:01 -0800299 cu.NewTimingSplit("Cleanup");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300
301 if (result) {
302 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file);
303 } else {
304 VLOG(compiler) << "Deferred " << PrettyMethod(method_idx, dex_file);
305 }
306
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700307 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
308 if (cu.arena.BytesAllocated() > (5 * 1024 *1024)) {
309 MemStats mem_stats(cu.arena);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
311 }
312 }
313
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700314 if (cu.enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
315 LOG(INFO) << "MEMINFO " << cu.arena.BytesAllocated() << " " << cu.mir_graph->GetNumBlocks()
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316 << " " << PrettyMethod(method_idx, dex_file);
317 }
318
buzbeea61f4952013-08-23 14:27:06 -0700319 cu.EndTiming();
Nicolas Geoffraydf013172014-01-13 10:11:18 +0000320 compiler.GetTimingsLogger().Start();
321 compiler.GetTimingsLogger().AddLogger(cu.timings);
322 compiler.GetTimingsLogger().End();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323 return result;
324}
325
326CompiledMethod* CompileOneMethod(CompilerDriver& compiler,
327 const CompilerBackend backend,
328 const DexFile::CodeItem* code_item,
329 uint32_t access_flags,
330 InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700331 uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332 uint32_t method_idx,
333 jobject class_loader,
334 const DexFile& dex_file,
335 llvm::LlvmCompilationUnit* llvm_compilation_unit) {
336 return CompileMethod(compiler, backend, code_item, access_flags, invoke_type, class_def_idx,
337 method_idx, class_loader, dex_file
338#if defined(ART_USE_PORTABLE_COMPILER)
339 , llvm_compilation_unit
340#endif
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700341 ); // NOLINT(whitespace/parens)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342}
343
344} // namespace art
345
346extern "C" art::CompiledMethod*
347 ArtQuickCompileMethod(art::CompilerDriver& compiler,
348 const art::DexFile::CodeItem* code_item,
349 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700350 uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 const art::DexFile& dex_file) {
352 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
353 art::CompilerBackend backend = compiler.GetCompilerBackend();
354 return art::CompileOneMethod(compiler, backend, code_item, access_flags, invoke_type,
355 class_def_idx, method_idx, class_loader, dex_file,
356 NULL /* use thread llvm_info */);
357}