blob: b55b4715eb0d5502b24a1c9b0e8ceaee092c6b4b [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
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +000017#include "compiler_backend.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070018#include "compiler_internals.h"
19#include "driver/compiler_driver.h"
20#include "dataflow_iterator-inl.h"
21#include "leb128.h"
22#include "mirror/object.h"
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080023#include "pass_driver.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#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
Vladimir Marko5c96e6b2013-11-14 15:34:17 +000029#include "dex/quick/dex_file_to_method_inliner_map.h"
30
Brian Carlstrom7940e442013-07-12 13:46:57 -070031namespace art {
Brian Carlstrom7940e442013-07-12 13:46:57 -070032
Vladimir Marko867a2b32013-12-10 13:01:13 +000033extern "C" void ArtInitQuickCompilerContext(art::CompilerDriver& driver) {
34 CHECK(driver.GetCompilerContext() == NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -070035}
36
Vladimir Marko867a2b32013-12-10 13:01:13 +000037extern "C" void ArtUnInitQuickCompilerContext(art::CompilerDriver& driver) {
Vladimir Marko5816ed42013-11-27 17:04:20 +000038 CHECK(driver.GetCompilerContext() == NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -070039}
40
41/* Default optimizer/debug setting for the compiler. */
Brian Carlstrom7934ac22013-07-26 10:54:15 -070042static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations
Brian Carlstrom7940e442013-07-12 13:46:57 -070043 (1 << kLoadStoreElimination) |
Brian Carlstrom7934ac22013-07-26 10:54:15 -070044 // (1 << kLoadHoisting) |
45 // (1 << kSuppressLoads) |
46 // (1 << kNullCheckElimination) |
47 // (1 << kPromoteRegs) |
48 // (1 << kTrackLiveTemps) |
49 // (1 << kSafeOptimizations) |
50 // (1 << kBBOpt) |
51 // (1 << kMatch) |
52 // (1 << kPromoteCompilerTemps) |
buzbee17189ac2013-11-08 11:07:02 -080053 // (1 << kSuppressExceptionEdges) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070054 0;
55
56static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
Brian Carlstrom7934ac22013-07-26 10:54:15 -070057 // (1 << kDebugDisplayMissingTargets) |
58 // (1 << kDebugVerbose) |
59 // (1 << kDebugDumpCFG) |
60 // (1 << kDebugSlowFieldPath) |
61 // (1 << kDebugSlowInvokePath) |
62 // (1 << kDebugSlowStringPath) |
63 // (1 << kDebugSlowestFieldPath) |
64 // (1 << kDebugSlowestStringPath) |
65 // (1 << kDebugExerciseResolveMethod) |
66 // (1 << kDebugVerifyDataflow) |
67 // (1 << kDebugShowMemoryUsage) |
68 // (1 << kDebugShowNops) |
69 // (1 << kDebugCountOpcodes) |
70 // (1 << kDebugDumpCheckStats) |
71 // (1 << kDebugDumpBitcodeFile) |
72 // (1 << kDebugVerifyBitcode) |
73 // (1 << kDebugShowSummaryMemoryUsage) |
buzbeeee17e0a2013-07-31 10:47:37 -070074 // (1 << kDebugShowFilterStats) |
buzbeea61f4952013-08-23 14:27:06 -070075 // (1 << kDebugTimings) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070076 0;
77
Vladimir Marko25724ef2013-11-12 15:09:20 +000078CompilationUnit::CompilationUnit(ArenaPool* pool)
79 : compiler_driver(NULL),
80 class_linker(NULL),
81 dex_file(NULL),
82 class_loader(NULL),
83 class_def_idx(0),
84 method_idx(0),
85 code_item(NULL),
86 access_flags(0),
87 invoke_type(kDirect),
88 shorty(NULL),
89 disable_opt(0),
90 enable_debug(0),
91 verbose(false),
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +000092 compiler_backend(NULL),
Vladimir Marko25724ef2013-11-12 15:09:20 +000093 instruction_set(kNone),
94 num_dalvik_registers(0),
95 insns(NULL),
96 num_ins(0),
97 num_outs(0),
98 num_regs(0),
Vladimir Marko25724ef2013-11-12 15:09:20 +000099 compiler_flip_match(false),
100 arena(pool),
101 mir_graph(NULL),
102 cg(NULL),
103 timings("QuickCompiler", true, false) {
104}
105
106CompilationUnit::~CompilationUnit() {
107}
108
buzbeea61f4952013-08-23 14:27:06 -0700109void CompilationUnit::StartTimingSplit(const char* label) {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000110 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700111 timings.StartSplit(label);
112 }
113}
114
115void CompilationUnit::NewTimingSplit(const char* label) {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000116 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700117 timings.NewSplit(label);
118 }
119}
120
121void CompilationUnit::EndTiming() {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000122 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700123 timings.EndSplit();
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000124 if (enable_debug & (1 << kDebugTimings)) {
125 LOG(INFO) << "TIMINGS " << PrettyMethod(method_idx, *dex_file);
126 LOG(INFO) << Dumpable<TimingLogger>(timings);
127 }
buzbeea61f4952013-08-23 14:27:06 -0700128 }
129}
130
Ian Rogers3d504072014-03-01 09:16:49 -0800131static CompiledMethod* CompileMethod(CompilerDriver& driver,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000132 CompilerBackend* compiler_backend,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 const DexFile::CodeItem* code_item,
134 uint32_t access_flags, InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700135 uint16_t class_def_idx, uint32_t method_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000136 jobject class_loader, const DexFile& dex_file,
137 void* llvm_compilation_unit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700138 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
buzbeeb48819d2013-09-14 16:15:25 -0700139 if (code_item->insns_size_in_code_units_ >= 0x10000) {
140 LOG(INFO) << "Method size exceeds compiler limits: " << code_item->insns_size_in_code_units_
141 << " in " << PrettyMethod(method_idx, dex_file);
142 return NULL;
143 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144
145 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3d504072014-03-01 09:16:49 -0800146 CompilationUnit cu(driver.GetArenaPool());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147
Ian Rogers3d504072014-03-01 09:16:49 -0800148 cu.compiler_driver = &driver;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700149 cu.class_linker = class_linker;
Ian Rogers3d504072014-03-01 09:16:49 -0800150 cu.instruction_set = driver.GetInstructionSet();
Andreas Gampe2da88232014-02-27 12:26:20 -0800151 cu.target64 = cu.instruction_set == kX86_64;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700152 cu.compiler_backend = compiler_backend;
Andreas Gampe2da88232014-02-27 12:26:20 -0800153 // TODO: x86_64 is not yet implemented.
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700154 DCHECK((cu.instruction_set == kThumb2) ||
155 (cu.instruction_set == kX86) ||
156 (cu.instruction_set == kMips));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157
158
159 /* Adjust this value accordingly once inlining is performed */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700160 cu.num_dalvik_registers = code_item->registers_size_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161 // TODO: set this from command line
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700162 cu.compiler_flip_match = false;
163 bool use_match = !cu.compiler_method_match.empty();
164 bool match = use_match && (cu.compiler_flip_match ^
165 (PrettyMethod(method_idx, dex_file).find(cu.compiler_method_match) !=
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166 std::string::npos));
167 if (!use_match || match) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700168 cu.disable_opt = kCompilerOptimizerDisableFlags;
169 cu.enable_debug = kCompilerDebugFlags;
170 cu.verbose = VLOG_IS_ON(compiler) ||
171 (cu.enable_debug & (1 << kDebugVerbose));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700172 }
173
174 /*
175 * TODO: rework handling of optimization and debug flags. Should we split out
176 * MIR and backend flags? Need command-line setting as well.
177 */
178
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000179 compiler_backend->InitCompilationUnit(cu);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700181 if (cu.instruction_set == kMips) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182 // Disable some optimizations for mips for now
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700183 cu.disable_opt |= (
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184 (1 << kLoadStoreElimination) |
185 (1 << kLoadHoisting) |
186 (1 << kSuppressLoads) |
187 (1 << kNullCheckElimination) |
188 (1 << kPromoteRegs) |
189 (1 << kTrackLiveTemps) |
190 (1 << kSafeOptimizations) |
191 (1 << kBBOpt) |
192 (1 << kMatch) |
193 (1 << kPromoteCompilerTemps));
194 }
195
buzbeea61f4952013-08-23 14:27:06 -0700196 cu.StartTimingSplit("BuildMIRGraph");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700197 cu.mir_graph.reset(new MIRGraph(&cu, &cu.arena));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700198
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800199 /*
200 * After creation of the MIR graph, also create the code generator.
201 * The reason we do this is that optimizations on the MIR graph may need to get information
202 * that is only available if a CG exists.
203 */
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000204 cu.cg.reset(compiler_backend->GetCodeGenerator(&cu, llvm_compilation_unit));
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800205
Brian Carlstrom7940e442013-07-12 13:46:57 -0700206 /* Gathering opcode stats? */
207 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700208 cu.mir_graph->EnableOpcodeCounting();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 }
210
211 /* Build the raw MIR graph */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700212 cu.mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700213 class_loader, dex_file);
214
buzbee1da1e2f2013-11-15 13:37:01 -0800215 cu.NewTimingSplit("MIROpt:CheckFilters");
Brian Carlstrom6449c622014-02-10 23:48:36 -0800216 if (cu.mir_graph->SkipCompilation()) {
buzbeeee17e0a2013-07-31 10:47:37 -0700217 return NULL;
218 }
219
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800220 /* Create the pass driver and launch it */
Ian Rogers3d504072014-03-01 09:16:49 -0800221 PassDriver pass_driver(&cu);
222 pass_driver.Launch();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700224 if (cu.enable_debug & (1 << kDebugDumpCheckStats)) {
225 cu.mir_graph->DumpCheckStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700226 }
227
228 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700229 cu.mir_graph->ShowOpcodeStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700230 }
231
buzbee1da1e2f2013-11-15 13:37:01 -0800232 /* Reassociate sreg names with original Dalvik vreg names. */
233 cu.mir_graph->RemapRegLocations();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234
235 CompiledMethod* result = NULL;
236
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700237 cu.cg->Materialize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238
buzbee1da1e2f2013-11-15 13:37:01 -0800239 cu.NewTimingSplit("Dedupe"); /* deduping takes up the vast majority of time in GetCompiledMethod(). */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700240 result = cu.cg->GetCompiledMethod();
buzbee1da1e2f2013-11-15 13:37:01 -0800241 cu.NewTimingSplit("Cleanup");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242
243 if (result) {
244 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file);
245 } else {
246 VLOG(compiler) << "Deferred " << PrettyMethod(method_idx, dex_file);
247 }
248
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700249 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
250 if (cu.arena.BytesAllocated() > (5 * 1024 *1024)) {
251 MemStats mem_stats(cu.arena);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
253 }
254 }
255
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700256 if (cu.enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
257 LOG(INFO) << "MEMINFO " << cu.arena.BytesAllocated() << " " << cu.mir_graph->GetNumBlocks()
Brian Carlstrom7940e442013-07-12 13:46:57 -0700258 << " " << PrettyMethod(method_idx, dex_file);
259 }
260
buzbeea61f4952013-08-23 14:27:06 -0700261 cu.EndTiming();
Ian Rogers3d504072014-03-01 09:16:49 -0800262 driver.GetTimingsLogger()->Start();
263 driver.GetTimingsLogger()->AddLogger(cu.timings);
264 driver.GetTimingsLogger()->End();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 return result;
266}
267
268CompiledMethod* CompileOneMethod(CompilerDriver& compiler,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000269 CompilerBackend* backend,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270 const DexFile::CodeItem* code_item,
271 uint32_t access_flags,
272 InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700273 uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274 uint32_t method_idx,
275 jobject class_loader,
276 const DexFile& dex_file,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000277 void* compilation_unit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 return CompileMethod(compiler, backend, code_item, access_flags, invoke_type, class_def_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000279 method_idx, class_loader, dex_file, compilation_unit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280}
281
282} // namespace art
283
284extern "C" art::CompiledMethod*
285 ArtQuickCompileMethod(art::CompilerDriver& compiler,
286 const art::DexFile::CodeItem* code_item,
287 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700288 uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700289 const art::DexFile& dex_file) {
290 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000291 art::CompilerBackend* backend = compiler.GetCompilerBackend();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292 return art::CompileOneMethod(compiler, backend, code_item, access_flags, invoke_type,
293 class_def_idx, method_idx, class_loader, dex_file,
294 NULL /* use thread llvm_info */);
295}